91 lines
2.6 KiB
TypeScript

import type { ModelMessage } from "ai";
export type CanvasAgentRunStatus =
| "running"
| "completed"
| "cancelled"
| "needs_review"
| "interrupted";
export class AgentRunControlChangedError extends Error {}
export type AgentCall = { id: string; name: string; input: Record<string, unknown> };
export type AgentCallRecord = AgentCall & {
status: "queued" | "started" | "succeeded" | "failed";
uncertain?: boolean;
output?: Record<string, unknown>;
error?: string;
};
export type AgentRunInput = { id: string; text: string; applied: boolean };
export type AgentRunSnapshot = {
version: 1;
nativeRuntime?: {
engine: "zcode";
commit?: string;
sandbox?: string;
state?: "starting" | "running" | "stopped";
sessionId?: string;
traceId?: string;
};
workspace?: "canvas" | "video";
videoSettings?: import("./video-protocol").VideoAgentSettings;
videoRequirements?: import("./video-protocol").VideoTaskRequirements;
objective: string;
messages: ModelMessage[];
calls: AgentCallRecord[];
inputs: AgentRunInput[];
steps: number;
inputTokens: number;
outputTokens: number;
repairs: number;
summary: string;
message: string;
resultNodeIds: string[];
sourceNodeIds?: string[];
requirements?: { expectedImageCount?: number; aspectRatio?: string; sourceCount?: number };
verification?: {
passed: boolean;
issues: string[];
failedNodeIds?: string[];
retryable?: boolean;
};
};
export type AgentRunView = {
runtime?: { engine: "zcode"; sandbox?: string; state?: "starting" | "running" | "stopped" };
videoSettings?: import("./video-protocol").VideoAgentSettings;
id: string;
chatId: string;
projectId: string;
status: CanvasAgentRunStatus;
message: string;
steps: number;
toolCount: number;
resultNodeIds: string[];
inputs: AgentRunInput[];
updatedAt: string;
cancelRequested: boolean;
resumable: boolean;
};
export function newAgentRunSnapshot(objective: string, messages: ModelMessage[]): AgentRunSnapshot {
return {
version: 1,
objective,
messages,
calls: [],
inputs: [],
steps: 0,
inputTokens: 0,
outputTokens: 0,
repairs: 0,
summary: "",
message: "正在理解任务",
resultNodeIds: [],
};
}
export function publicAgentError(error: unknown): string {
const text = error instanceof Error ? error.message : String(error || "处理失败");
if (/<(?:!doctype|html|head|body)\b/i.test(text) || /cloudflare|ray id/i.test(text)) {
return "模型服务暂时不可用,已保留完成的步骤,请稍后继续。";
}
return text.replace(/(?:sk-[\w-]{12,}|Bearer\s+[\w.-]+)/gi, "[已隐藏]").slice(0, 400);
}