latitudegames / Scripting

61 stars 16 forks source link

reimplementAuthorsNote.js truncation at the end is nonsensical/bugged. #18

Open Slight0 opened 3 years ago

Slight0 commented 3 years ago

Towards the end of the context modifier example reimplementAuthorsNote.js some code tries to truncate the output so as to not exceed info.memoryLength or info.maxChars, but ends up needlessly truncating the most recent actions instead.

Edit Note: (Fixed an issue with my reasoning. The bug is there, but for a different set of reasons)

// Make sure the new context isn't too long, or it will get truncated by the server. const combinedLines = lines.join("\n").slice(-(info.maxChars - info.memoryLength)) const finalText = [contextMemory, combinedLines].join("") return { text: finalText }

So at this point the function has split the incoming text into contextMemory and context where context is the length of memoryLength representing the AI's memory window. lines.join("\n") is context with the author's notes injected (3 lines back from front). It's assumed that the input text was an arbitrary length that could have exceeded maxChars. I believe the above code is trying to truncate the modified output text so that finalText is truncated from the front (ie oldest player actions) to maxChars. (otherwise the server would truncate the back losing the newest actions).

So the mistakes it makes are that it assumes lines.join("\n") is already info.memoryLength, which is false because it is context (which is length memoryLength) with the author's notes injected, pushing it over. Further, truncation is only happening on the context portion of the text when the intent here is to truncate all of text; context portion should be untouched. Last, truncation is weird when maxChars is greater than memoryLength. Like, if memoryLength is 20 and maxChars is 21, that's slice(-1) which results in the very last char of the most recent player actions making it through. Why would we throw away the most recent actions right? We want to truncate the oldest actions.

Below is the fixed code that should replace the above.

// Make sure the final context isn't too long, or it will get truncated by the server. const finalText = (contextMemory + lines.join("\n")).slice(-info.maxChars); return { text: finalText }