Admin -> Personalizar -> Texto se comportando mal

Estou encontrando vários problemas semelhantes a este ao tentar filtrar os prompts do meu chatbot.

A parte realmente estranha é que o comportamento pode estar bom em uma instalação, mas não em outra.

Pegue o filtro muito simples de chatbot.prompt em Admin → Personalizar → Texto:

Instalação funcionando:

Instalação com falha:

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/middleware/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'

O arquivo de localidade em questão é este:

Estou fazendo algo ilegal?

3 curtidas

ok

parece que o método em questão está esperando texto:

mas de alguma forma está recebendo um Hash:

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>

Fazendo com que ele caia… Continuarei a investigação

Está sendo chamado daqui, acredito:

Posso reproduzir o problema com:

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

Acho que esta linha deveria impedir que o método recebesse um Hash, mas não está funcionando, por quê?:

1 curtida

Movendo isto para Bug, porque acho que este comportamento está incorreto e, por algum motivo, não é confiável.

De acordo com o código, se o valor for um Hash, ele não deve ser enviado para o método record_for (onde causaria uma exceção, pois ele espera texto), mas em algumas instâncias, ele é.

Ficarei feliz em ser corrigido.

1 curtida

Isso acontece quando há uma substituição de tradução para uma chave que anteriormente era uma string traduzível, mas foi convertida em um Hash posteriormente. É exatamente isso que aconteceu com chatbot.prompt.system.

Isso parece uma regressão. Tenho certeza de que isso funcionava no passado.

Posso reproduzir isso facilmente com um arquivo de localidade como este:

en:
  foo: "this is foo"

Vá para Customize –> Text e substitua “foo” por um valor diferente.

Em seguida, altere o arquivo de localidade para que fique assim:

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

Visite Customize –> Text novamente e procure por “foo”. O Discourse registrará o seguinte erro.

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'

O Discourse não deve carregar substituições de tradução se não houver uma string em inglês correspondente.

4 curtidas

Obrigado @gerhard

Foi exatamente o que aconteceu: adicionei outra chave final no mesmo bucket.

… E isso não afeta novas instalações.

Existe algum tipo de solução alternativa no console do Rails?

1 curtida
TranslationOverride.where(translation_key: "chatbot.prompt.system").delete_all
TranslationOverride.send(:reload_locale!)
5 curtidas

Perfeito. Sim, eu ia tentar isso, mas não no :laptop:

Acho que poderia adicionar isso a uma migração?!

2 curtidas

Acredito que este bug também foi resolvido junto com este: :tada:

2 curtidas