

















@@ -36,6 +36,8 @@ type AgentTaskCompletionInternalEvent = {
3636replyInstruction: string;
3737};
383839+type TaskCompletionPromptMode = "plain" | "protected";
40+3941/** Internal event variants that can be rendered into agent prompt context. */
4042export type AgentInternalEvent = AgentTaskCompletionInternalEvent;
4143@@ -86,7 +88,10 @@ function formatGeneratedMediaDirectiveLines(event: AgentTaskCompletionInternalEv
8688return ["Generated media:", ...mediaUrls.map((mediaUrl) => `MEDIA:${mediaUrl}`)];
8789}
889089-function formatTaskCompletionEvent(event: AgentTaskCompletionInternalEvent): string {
91+function formatTaskCompletionEvent(
92+event: AgentTaskCompletionInternalEvent,
93+mode: TaskCompletionPromptMode,
94+): string {
9095const sessionKey = sanitizeSingleLineField(event.childSessionKey, "unknown");
9196const sessionId = sanitizeSingleLineField(event.childSessionId ?? "unknown", "unknown");
9297const announceType = sanitizeSingleLineField(event.announceType, "unknown");
@@ -95,8 +100,14 @@ function formatTaskCompletionEvent(event: AgentTaskCompletionInternalEvent): str
95100const result = formatChildResultDataBlock(event.result);
96101const attachmentLines = formatGeneratedAttachmentLines(event.attachments);
97102const mediaDirectiveLines = formatGeneratedMediaDirectiveLines(event);
98-const lines = [
99-"[Internal task completion event]",
103+const lines =
104+mode === "protected"
105+ ? ["[Internal task completion event]"]
106+ : [
107+"A background task completed. Use this result to reply to the user in your normal assistant voice.",
108+"",
109+];
110+lines.push(
100111`source: ${event.source}`,
101112`session_key: ${sessionKey}`,
102113`session_id: ${sessionId}`,
@@ -105,7 +116,7 @@ function formatTaskCompletionEvent(event: AgentTaskCompletionInternalEvent): str
105116`status: ${statusLabel}`,
106117"",
107118result,
108-];
119+);
109120if (attachmentLines.length > 0) {
110121lines.push("", ...attachmentLines);
111122}
@@ -115,41 +126,11 @@ function formatTaskCompletionEvent(event: AgentTaskCompletionInternalEvent): str
115126if (event.statsLine?.trim()) {
116127lines.push("", sanitizeMultilineField(event.statsLine, ""));
117128}
118-lines.push("", "Action:", sanitizeMultilineField(event.replyInstruction, ""));
119-return lines.join("\n");
120-}
121-122-function formatTaskCompletionEventForPlainPrompt(event: AgentTaskCompletionInternalEvent): string {
123-const sessionKey = sanitizeSingleLineField(event.childSessionKey, "unknown");
124-const sessionId = sanitizeSingleLineField(event.childSessionId ?? "unknown", "unknown");
125-const announceType = sanitizeSingleLineField(event.announceType, "unknown");
126-const taskLabel = sanitizeSingleLineField(event.taskLabel, "unnamed task");
127-const statusLabel = sanitizeSingleLineField(event.statusLabel, event.status);
128-const result = formatChildResultDataBlock(event.result);
129-const attachmentLines = formatGeneratedAttachmentLines(event.attachments);
130-const mediaDirectiveLines = formatGeneratedMediaDirectiveLines(event);
131-const lines = [
132-"A background task completed. Use this result to reply to the user in your normal assistant voice.",
129+lines.push(
133130"",
134-`source: ${event.source}`,
135-`session_key: ${sessionKey}`,
136-`session_id: ${sessionId}`,
137-`type: ${announceType}`,
138-`task: ${taskLabel}`,
139-`status: ${statusLabel}`,
140-"",
141-result,
142-];
143-if (attachmentLines.length > 0) {
144-lines.push("", ...attachmentLines);
145-}
146-if (mediaDirectiveLines.length > 0) {
147-lines.push("", ...mediaDirectiveLines);
148-}
149-if (event.statsLine?.trim()) {
150-lines.push("", sanitizeMultilineField(event.statsLine, ""));
151-}
152-lines.push("", "Instruction:", sanitizeMultilineField(event.replyInstruction, ""));
131+mode === "protected" ? "Action:" : "Instruction:",
132+sanitizeMultilineField(event.replyInstruction, ""),
133+);
153134return lines.join("\n");
154135}
155136@@ -161,7 +142,7 @@ export function formatAgentInternalEventsForPrompt(events?: AgentInternalEvent[]
161142const blocks = events
162143.map((event) => {
163144if (event.type === "task_completion") {
164-return formatTaskCompletionEvent(event);
145+return formatTaskCompletionEvent(event, "protected");
165146}
166147return "";
167148})
@@ -187,7 +168,7 @@ export function formatAgentInternalEventsForPlainPrompt(events?: AgentInternalEv
187168return events
188169.map((event) => {
189170if (event.type === "task_completion") {
190-return formatTaskCompletionEventForPlainPrompt(event);
171+return formatTaskCompletionEvent(event, "plain");
191172}
192173return "";
193174})
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。