r1tsuu / payload-enchants

Payload 3.0 enchantment packages
MIT License
117 stars 7 forks source link

Some fields are emptied on translate #22

Closed genox closed 3 months ago

genox commented 4 months ago

Hi,

I have a rather complex collection structure. I can post it here if you would like to test.

This is what I do:

  1. Create new entry in base lang
  2. Switch to secondary lang
  3. "Copy from other locale"
  4. Choose base lang
  5. Press "Copy all"

Similarly, when I use "AI Translate":

  1. Create new entry in base lang
  2. Switch to secondary lang
  3. "AI Translate"
  4. Choose base lang
  5. Press "Translate all"

=> All fields in an array structure that were previously containing text are now empty, including those marked as localised: true. Number fields seem to be unaffected by this.

When I reload the page, the fields contain the values they should contain.

Could it be that arrays are not handled yet?

r1tsuu commented 4 months ago

All fields should work, can you give me the config of your array field so i can check? (as you can see in the video from Readme i have array field as well)

r1tsuu commented 4 months ago

Okay, ive just checked with latest Payload 3.0 and it doesn't work with array field. I'm gonna quickly fix that, seems like change in Payload caused it.

r1tsuu commented 4 months ago

Hi! Fixed this issue and as well updated this repo https://github.com/r1tsuu/payload-enchants because i want to write more packages/plugins i maintain the same repo. translator docs - https://github.com/r1tsuu/payload-enchants/tree/master/packages/translator npm https://www.npmjs.com/package/@payload-enchants/translator there's only 1 breaking change that you need to change import a bit. pnpm add @payload-enchants/translator

Let me know if it works for you now.

genox commented 4 months ago

Seems to work fine now, however I get an error when using the openAI translator now. It complains about receiving null instead of an array.

r1tsuu commented 4 months ago

Seems to work fine now, however I get an error when using the openAI translator now. It complains about receiving null instead of an array.

i can't reproduce it

https://github.com/r1tsuu/payload-enchants/assets/64744993/85318c96-60f6-4cc9-8711-d62ad097b914

are you getting this error every time or sometimes? i can see that ChatGPT isn't consistent in responses causes to invalid structure, even though my prompt is clear. also is your document large? could be the case that here ChatGPT loses even more consistency

genox commented 4 months ago

I get the error every time I hit the translation button. It worked before you moved to mono repo. Maybe my config is off.

[13:19:15] INFO:
    data: null
    message: "An error occurred when trying to translate the data using OpenAI API - parsed content is not an array"
 POST /api/translator/translate 200 in 1169ms
genox commented 3 months ago

@r1tsuu Hi again

I had some time to look into it and it seems that one of the source fields has a null value which is translated to null and therefore causes an error while parsing the response as it specifically parses for typeof string.

I think it is safe to assume that null (and boolean values or purely numerical values?) should not be sent as part of the translation request, even though they end up in a text field - it could be an error on the side of the integrator that builds a schema wrongly or a component that nulls something. It would also add to the resilience of the translation function to handle such cases gracefully, e.g. by notification about the fact that there is an unexpected value present that might not be intended.

siobahnschuck commented 3 months ago

I was running into the same null error. This was occuring for me when content that had localized: true set in the schema but was not required field was left empty. Instead of saving as an empty string the data returns null. I fixed this by updating the condition for strings in packages/translator/src/resolvers/openAI.ts line 121:

if (text !== null && typeof text !== 'string')

genox commented 3 months ago

This should be resolved now. Thank you!

r1tsuu commented 3 months ago

This should be resolved now. Thank you!

Just to know, released one more after this https://github.com/r1tsuu/payload-enchants/releases/tag/1.1.32 there was an issue that you couldn't save the doc after translate as it wasn't modified