🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(channel): keep raw JSON out of the reply acknowledgement (#23491)
Arvin Xu9d7e2864855c
diff --git a/apps/server/src/services/channel/opener.test.ts b/apps/server/src/services/channel/opener.test.tsindex 4811b5a9640..e6454dd3205 100644--- a/apps/server/src/services/channel/opener.test.ts+++ b/apps/server/src/services/channel/opener.test.ts@@ -113,4 +113,25 @@ describe('normalizeChannelReplyOpening', () => {normalizeChannelReplyOpening({ ...answer, placement: 'thread' }, { canOpenThread: true }),).toEqual({ placement: 'channel' });});++ it('posts only the sentence when the model writes its whole answer into the acknowledgement', () => {+ const leaked =+ '{"acknowledge":true,"ack":"我先查一下 lobot 目前好友添加和联系人列表的实际入口。","placement":"channel","thread":"","title":""}”】【注:需要输出 JSON object';+ expect(+ normalizeChannelReplyOpening(+ { ...answer, ack: leaked, acknowledge: true },+ { canOpenThread: true },+ ),+ ).toEqual({+ ack: '我先查一下 lobot 目前好友添加和联系人列表的实际入口。',+ placement: 'channel',+ });++ expect(+ normalizeChannelReplyOpening(+ { ...answer, ack: '{"acknowledge":true,"placement":"channel"', acknowledge: true },+ { canOpenThread: true },+ ),+ ).toEqual({ placement: 'channel' });+ });});diff --git a/apps/server/src/services/channel/opener.ts b/apps/server/src/services/channel/opener.tsindex 0c056bc1069..c48c1c6db19 100644--- a/apps/server/src/services/channel/opener.ts+++ b/apps/server/src/services/channel/opener.ts@@ -61,16 +61,38 @@ export interface CreateChannelReplyOpenerOptions {const oneLine = (text: string, max: number) => text.replaceAll(/\s+/g, ' ').trim().slice(0, max);+const NESTED_ACK = /"ack"\s*:\s*"((?:[^"\\]|\\.)*)"/;++/**+ * The sentence to post, from the model's `ack`. A model sometimes writes its whole answer, JSON and+ * all, into that one field, often with stray text after it; posting it would put raw JSON in the+ * Channel. Only the sentence inside is kept, and an `ack` that is JSON without one posts nothing.+ */+const ackSentence = (ack: string) => {+ const text = ack.trim();+ if (!text.startsWith('{')) return text;++ const nested = NESTED_ACK.exec(text)?.[1];+ if (!nested) return '';+ try {+ return JSON.parse(`"${nested}"`) as string;+ } catch {+ return '';+ }+};+/*** Turns the model's answer into something safe to act on: a new Thread needs a title, an existing- * one must be among those the opener was shown, and an acknowledgement needs words. Anything else- * answers in place.+ * one must be among those the opener was shown, and an acknowledgement needs words, not JSON.+ * Anything else answers in place.*/export const normalizeChannelReplyOpening = (answer: z.infer<typeof OpenerSchema>,input: Pick<ChannelReplyOpenerInput, 'canOpenThread' | 'coAddressed' | 'threads'>,): ChannelReplyOpening => {- const ack = answer.acknowledge ? oneLine(answer.ack, CHANNEL_REPLY_OPENER_MAX_ACK_LENGTH) : '';+ const ack = answer.acknowledge+ ? oneLine(ackSentence(answer.ack), CHANNEL_REPLY_OPENER_MAX_ACK_LENGTH)+ : '';const opening = { ...(ack && { ack }) };if (!input.canOpenThread) return { ...opening, placement: 'channel' };