几个 AI 插件 Text Generator, Smart Connections, Copilot 的简单调查

Hi, 我看了下代码,chunk 报错是因为 runJSInSandbox 对入参做了处理,只接受函数和对象,不过我们可以用 this 引用到它。

所以B这一步可以不用做,把流式处理预设脚本用到chunk那行改成下面这样就可以:

const lines = this.chunk.split("\ndata: ");

完整代码:

// catch error
if (res.status >= 300) {
  const err = data?.error?.message || JSON.stringify(data);
  throw err;
}
let resultTexts = [];
const lines = this.chunk.split("\ndata: ");

const parsedLines = lines
    .map((line) => line.replace(/^data: /, "").trim()) // Remove the "data: " prefix
    .filter((line) => line !== "" && line !== "[DONE]") // Remove empty lines and "[DONE]"
    .map((line) => {
        try {
            return JSON.parse(line)
        } catch { }
    }) // Parse the JSON string
    .filter(Boolean);

for (const parsedLine of parsedLines) {
    const { choices } = parsedLine;
    const { delta } = choices[0];
    const { content } = delta;
    // Update the UI with the new content
    if (content) {
        resultTexts.push(content);
    }
}
return resultTexts.join("");

稍后我给上游提个 PR :smiley:

1 个赞