Admin -> Anpassen -> Text verhält sich unerwartet

Beim Versuch, meine Chatbot-Prompts zu filtern, treten ähnliche Probleme auf.

Das wirklich Seltsame ist, dass das Verhalten bei einer Installation einwandfrei sein kann, bei einer anderen jedoch nicht.

Nehmen Sie den sehr einfachen Filter chatbot.prompt unter Admin → Anpassen → Text:

Funktionierende Installation:

Fehlgeschlagene Installation:

NoMethodError (undefined method `scan' for {:basic=>"You are a helpful assistant.", :agent=>"You are a helpful assistant. You have great tools in the form of functions that give you the power to get newer information. Only use the functions you have been provided with. The current date and time is %{current_date_time}. Only respond to the last question, using the prior information as context, if appropriate."}:Hash)
lib/i18n/i18n_interpolation_keys_finder.rb:6:in `find'
app/controllers/admin/site_texts_controller.rb:183:in `record_for'
app/controllers/admin/site_texts_controller.rb:249:in `block in find_translations'
app/controllers/admin/site_texts_controller.rb:234:in `each'
app/controllers/admin/site_texts_controller.rb:234:in `find_translations'
app/controllers/admin/site_texts_controller.rb:34:in `index'
app/controllers/application_controller.rb:422:in `block in with_resolved_locale'
app/controllers/application_controller.rb:422:in `with_resolved_locale'
lib/middleware/omniauth_bypass_middleware.rb:64:in `call'
lib/content_security_policy/middleware.rb:12:in `call'
lib/middleware/anonymous_cache.rb:389:in `call'
lib/middleware/gtm_script_nonce_injector.rb:10:in `call'
config/initializers/100-quiet_logger.rb:20:in `call'
config/initializers/100-silence_logger.rb:29:in `call'
lib/middleware/enforce_hostname.rb:24:in `call'
lib/middleware/request_tracker.rb:233:in `call'

Die betreffende Locale-Datei ist diese:

Mache ich etwas Illegales?

3 „Gefällt mir“

ok

es scheint, dass die betreffende Methode Text erwartet:

aber irgendwie einen Hash erhält:

irb(main):001:0> item = {:basic=>"You are a helpful assistant.", :agent=>"You are a helpful assistant.  You have great tools in the form of functions that give you the power to get newer information. Only use the functions you have been provided with.  The current date and time is %{current_date_time}.  Only respond to the last question, using the prior information as context, if appropriate."}
=>
{:basic=>"You are a helpful assistant.",
irb(main):002:0> item
=>
{:basic=>"You are a helpful assistant.",
 :agent=>
  "You are a helpful assistant.  You have great tools in the form of functions that give you the power to get newer information. Only use the functions you have been provided with.  The current date and time is %{current_date_time}.  Only respond to the last question, using the prior information as context, if appropriate."}
irb(main):003:0> item.scan
(irb):3:in `<main>': undefined method `scan' for {:basic=>"You are a helpful assistant.", :agent=>"You are a helpful assistant.  You have great tools in the form of functions that give you the power to get newer information. Only use the functions you have been provided with.  The current date and time is %{current_date_time}.  Only respond to the last question, using the prior information as context, if appropriate."}:Hash (NoMethodError)
        from /home/robert/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/irb-1.6.2/exe/irb:11:in `<top (required)>'
        from /home/robert/.rbenv/versions/3.2.1/bin/irb:25:in `load'
        from /home/robert/.rbenv/versions/3.2.1/bin/irb:25:in `<main>'
irb(main):004:0

was dazu führt, dass es abstürzt … Ich werde die Untersuchung fortsetzen

Ich glaube, es wird von hier aufgerufen:

Ich kann das Problem mit Folgendem reproduzieren:

I18nInterpolationKeysFinder.find(I18n.overrides_disabled { I18n.t("chatbot.prompt.system.", locale: :en) })
aus der Rails-Konsole.

Ich glaube, diese Zeile soll verhindern, dass die Methode einen Hash erhält, aber sie funktioniert nicht, warum?:

1 „Gefällt mir“

Dies verschiebe ich nach Bug, da ich dieses Verhalten für falsch und aus irgendeinem Grund unzuverlässig halte.

Laut Code sollte der Wert nicht an die record_for-Methode gesendet werden, wenn es sich um einen Hash handelt (wo er eine Ausnahme verursachen würde, da diese Text erwartet), aber in einigen Fällen wird er es.

Ich lasse mich gerne korrigieren.

1 „Gefällt mir“

Dies geschieht, wenn es eine Übersetzung-Überschreibung für einen Schlüssel gibt, der zuvor eine übersetzbare Zeichenfolge war, aber später in einen Hash konvertiert wurde. Genau das ist mit chatbot.prompt.system passiert.

Dies scheint ein Rückschritt zu sein. Ich bin ziemlich sicher, dass dies in der Vergangenheit funktioniert hat.

Ich kann dies leicht mit einer Locale-Datei wie dieser reproduzieren:

en:
  foo: "this is foo"

Gehen Sie zu Anpassen → Text und überschreiben Sie “foo” mit einem anderen Wert.

Ändern Sie dann die Locale-Datei wie folgt:

en:
  foo:
    basic: "Basic Foo"
    advanced: "Advanced Foo"

Besuchen Sie Anpassen → Text erneut und suchen Sie nach “foo”. Discourse wird den folgenden Fehler protokollieren.

NoMethodError (undefined method `scan' for {:basic=>"Basic Foo", :advanced=>"Advanced Foo"}:Hash)
lib/i18n/i18n_interpolation_keys_finder.rb:6:in `find'
app/controllers/admin/site_texts_controller.rb:183:in `record_for'
app/controllers/admin/site_texts_controller.rb:249:in `block in find_translations'
app/controllers/admin/site_texts_controller.rb:234:in `each'
app/controllers/admin/site_texts_controller.rb:234:in `find_translations'
app/controllers/admin/site_texts_controller.rb:34:in `index'

Discourse sollte keine Übersetzung-Überschreibungen laden, wenn keine entsprechende englische Zeichenfolge vorhanden ist.

4 „Gefällt mir“

Danke @gerhard

Das ist genau das, was passiert ist: Ich habe einen weiteren finalen Schlüssel im selben Bucket hinzugefügt.

… Und es hat keine Auswirkungen auf Neuinstallationen.

Gibt es eine Art Rails-Konsolen-Workaround?

1 „Gefällt mir“
TranslationOverride.where(translation_key: "chatbot.prompt.system").delete_all
TranslationOverride.send(:reload_locale!)
5 „Gefällt mir“

Perfekt. Ja, das wollte ich auch versuchen, aber nicht am :laptop:.

Ich könnte das wohl einer Migration hinzufügen?!

2 „Gefällt mir“

Ich glaube, dieser Fehler wurde zusammen mit diesem behoben: :tada:

2 „Gefällt mir“