管理者 -> カスタマイズ -> テキストの表示がおかしい

チャットボットのプロンプトでフィルタリングしようとすると、同様の問題が多数発生しています。

さらに奇妙なのは、あるインストールでは正常に動作するのに、別のインストールでは動作しないことです。

管理画面の「カスタマイズ」→「テキスト」で、chatbot.prompt という非常に単純なフィルタを試してみてください。

正常に動作するインストール:

失敗するインストール:

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'

問題のロケールファイルはこちらです。

何か不正なことをしていますか?

「いいね!」 3

はい

問題のメソッドはテキストを期待しているようですが、

なぜかハッシュを受け取っています。

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.", :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):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>

これにより、エラーが発生しています…さらに調査を続けます。

ここから呼び出されているようです。

レールコンソールから次を実行すると、問題を再現できます。

I18nInterpolationKeysFinder.find(I18n.overrides_disabled { I18n.t("chatbot.prompt.system.", locale: :en) })

この行がメソッドがハッシュを受け取るのを防ぐはずですが、なぜ機能しないのでしょうか?

「いいね!」 1

これを Bug に移動します。この動作は正しくなく、何らかの理由で信頼性が低いと思われるためです。

コードによると、値が Hash の場合、record_for メソッドに送信されるべきではありません(このメソッドはテキストを期待しているため、例外が発生します)。しかし、実際には送信されてしまう場合があります。

訂正があれば喜んで受け入れます。

「いいね!」 1

これは、以前は翻訳可能な文字列だったキーに対して翻訳の上書きが存在し、その後ハッシュに変換された場合に発生します。これはまさに chatbot.prompt.system で発生したことです。

これはリグレッションのように思えます。以前は機能していたと確信しています。

次のようなロケールファイルで簡単に再現できます。

en:
  foo: "this is foo"

「カスタマイズ」->「テキスト」に移動し、「foo」を別の値で上書きします。

次に、ロケールファイルを次のように変更します。

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

再度「カスタマイズ」->「テキスト」にアクセスし、「foo」を検索します。Discourse は次のエラーをログに記録します。

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 は翻訳の上書きを読み込むべきではありません。

「いいね!」 4

@gerhardさん、ありがとうございます。

まさにその通りでした。同じバケットに別の最終キーを追加しました。

…そして、新規インストールには影響しません。

何かRailsコンソールでの回避策はありますか?

「いいね!」 1
TranslationOverride.where(translation_key: "chatbot.prompt.system").delete_all
TranslationOverride.send(:reload_locale!)
「いいね!」 5

完璧です。はい、それを試そうと思っていましたが、:laptop:ではできませんでした。

移行に追加できるでしょうか?!

「いいね!」 2

これも一緒に修正されたと思います :tada:

「いいね!」 2