import { canvasAgentProgressLabel } from "./progress"; import type { ModelMessage } from "ai"; export type CanvasAgentRunStatus = | "running" | "completed" | "cancelled" | "needs_review" | "interrupted"; export class AgentRunControlChangedError extends Error {} /** Server confirms validation/auth rejection before a business operation starts. */ export class AgentToolRejectedError extends Error {} export type AgentCall = { id: string; name: string; input: Record }; export type AgentCallRecord = AgentCall & { title?: string; status: "queued" | "started" | "succeeded" | "failed"; uncertain?: boolean; rejected?: boolean; output?: Record; 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"; imagePreferences?: import("./image-preferences").AgentImagePreferences; 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?: { method?: "alpha-channel"; passed: boolean; issues: string[]; failedNodeIds?: string[]; retryable?: boolean; }; }; export type AgentRunView = { assistantMessageId?: string; activities?: Array<{ id: string; title: string; status: AgentCallRecord["status"] }>; 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 canvasAgentProgressLabel(text.replace(/(?:sk-[\w-]{12,}|Bearer\s+[\w.-]+)/gi, "[已隐藏]").slice(0, 400)); }