Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

Merge pull request #7665 from Eapp1e/fix/rich-answer-malformed-drafts

Lizi20254f502905
diff --cc packages/maker-shared/src/richAnswer.ts
index 8853c01335,39dd9f2985..7dadfd28b5
--- a/packages/maker-shared/src/richAnswer.ts
+++ b/packages/maker-shared/src/richAnswer.ts
@@@ -337,14 -328,89 +344,91 @@@ export class RichAnswerStream
}
} else throw Error("operation");
this.revision++;
+ return true;
} catch {
this.issues++;
+ return false;
}
}
- private props(v: unknown): Record<string, unknown> {
+ /** #7627: children written inline in `props` become the child nodes they were meant to be. */
+ private attachInlineChildren(
+ parentId: string,
+ drafts: unknown[],
+ childDepth: number,
+ ) {
+ const parent = this.nodes.get(parentId);
+ if (!parent) return;
+ const children = [...parent.children];
+ drafts.forEach((draft, offset) => {
+ // Inline children are already part of the document the reader sees, so a
+ // drop here is missing content: report it like a rejected operation.
+ const dropped = () => {
+ this.issues++;
+ };
+ if (
+ !record(draft) ||
+ !RICH_ANSWER_KINDS.includes(draft.kind as RichAnswerKind) ||
+ childDepth > MAX_DEPTH ||
+ this.nodes.size >= MAX_NODES
+ ) {
+ dropped();
+ return;
+ }
+ // One unreadable draft must not take its siblings down with it.
+ let props: Record<string, unknown>;
+ try {
+ props = this.props(
+ draft.props ?? (typeof draft.text === "string" ? { text: draft.text } : {}),
+ draft.kind as RichAnswerKind,
+ );
+ } catch {
+ dropped();
+ return;
+ }
+ // A draft may name itself (`{id:"second"}`); `tabs.props.default` points at
+ // that id, so a declared id wins while it is still free.
+ const declared =
+ typeof draft.id === "string" && ID.test(draft.id) && !this.nodes.has(draft.id);
+ const id = declared ? (draft.id as string) : this.inlineChildId(parentId, offset + 1);
+ this.nodes.set(id, {
+ id,
+ kind: draft.kind as RichAnswerKind,
+ props,
+ children: [],
+ });
+ children.push(id);
+ // `children` may sit on the draft itself (`{kind:"stack",children:[...]}`) or
+ // inside its props; both mean the same thing and both keep nesting bounded.
+ const nested = Array.isArray(draft.children) ? draft.children : props.children;
+ if (Array.isArray(nested) && CONTAINER_KINDS.includes(draft.kind as RichAnswerKind)) {
+ delete props.children;
+ this.attachInlineChildren(id, nested, childDepth + 1);
+ }
+ });
+ this.nodes.set(parentId, { ...parent, children });
+ }
+ private inlineChildId(parentId: string, ordinal: number) {
+ // The id grammar caps ids at 80 characters, so the suffix gets the room first.
+ for (let attempt = 1; ; attempt++) {
+ const suffix = attempt === 1 ? `-${ordinal}` : `-${ordinal}-${attempt}`;
+ const id = `${parentId.slice(0, 80 - suffix.length)}${suffix}`;
+ if (!this.nodes.has(id)) return id;
+ }
+ }
+ private props(v: unknown, kind?: RichAnswerKind): Record<string, unknown> {
if (v === undefined) return {};
+ // Models write the text straight into props (`"props": "hello"`); the protocol
+ // default keeps that block readable instead of dropping the whole node (#7627).
+ if (typeof v === "string") {
+ // Only kinds that render `props.text` take a bare string; anywhere else the
+ // text would be stored and never shown, so the draft counts as unreadable.
+ if (kind && !TEXT_PROP_KINDS.includes(kind)) throw Error("props");
+ // The limit is on the node that gets stored and replayed, which is the
+ // object below: escaping can make it longer than the incoming string.
+ const text = { text: v };
+ if (JSON.stringify(text).length > 65_536) throw Error("props");
+ return text;
+ }
if (!record(v) || JSON.stringify(v).length > 65_536) throw Error("props");
return Object.fromEntries(
Object.entries(v).filter(