import type { ContextBundle } from '../context/contextBundle.js'; import type { chat as defaultChat } from '../llm/client.js'; import type { AgentRuntimeContext } from '../runtime/agentRuntimeContext.js'; import type { InvocationPolicy } from '../runtime/invocationPolicy.js'; import type { SessionState } from '../session/sessionState.js'; import type { ToolGate } from '../toolGate.js'; import type { DeliverableManifest, ImageDetail, ImageInputSpec, Message, Provider, Tool, ToolCallContext, ToolResult, } from '../types.js'; export interface RunQueryOptions { agentHandoff?: ToolCallContext['agentHandoff']; /** Generic embedded-task policy; omitted for ordinary conversation turns. */ invocationPolicy?: InvocationPolicy; executionServices?: ToolCallContext['executionServices']; provider: Provider; /** Canonical host-issued trust context. Legacy fields below remain adapters. */ runtimeContext?: AgentRuntimeContext; /** 历史会被 mutate(追加新消息、压缩时整体替换内容);调用方持同一引用 */ history: Message[]; /** Host mailbox: consume at model boundaries, never mutate an in-flight request. */ takePendingMessages?: () => Message[]; /** False when a host command precedes the pending human supplement. */ canConsumePendingMessages?: () => boolean; steering?: ToolCallContext['steering']; /** 最多多少轮(防止失控调工具) */ maxTurns?: number; /** * 工作流 agent/llm 步的工具白名单:只把这些工具下发给模型并允许执行。 * 缺省 = 当前宿主工具全集;空数组 = 无工具。 */ allowedTools?: string[]; /** 调用方工具边界名称,仅用于准确解释越界调用;缺省表示宿主能力边界。 */ toolBoundaryLabel?: string; /** False turns off the Advisor reminders for an explicitly read-only embedded node. */ advisorReminders?: boolean; /** * 实质回答发布后仍未经顾问复核时,再发一次请求让模型自己补问(回答不撤回)。 * 缺省读配置 `advisor.enforcement === 'followup'`;测试和内嵌节点可显式指定。 */ advisorFollowUp?: boolean; /** Tools the host/workflow explicitly requires; emergency schema reduction preserves them. */ requiredTools?: string[]; /** * provider 原生结构化输出(`response_format.json_schema`),原样透传给 `chat()`。 * * ⚠️ **只在本轮没有工具时才该传**:response_format 会强制**每一条** assistant * 消息都是 JSON,而工具调用轮的 assistant 消息带的是 tool_calls —— 两者互斥, * 同时给会让带工具的循环直接崩。工作流引擎因此只在 `llm()`(零工具)和 * schema 修复轮(零工具)上传它;`agent()` 的结构化输出走「跑完再修复」那条路。 * * Anthropic 格式的 provider 不支持,`chat()` 会在发请求前抛错 —— 所以它是 * **加成**而不是地基,地基是引擎侧的「校验 + 有界修复」。 */ responseFormat?: { type: 'json_schema'; json_schema: { name: string; schema: Record; strict?: boolean; }; }; /** * Host-provided tool catalog. Production hosts normally omit this and use ALL_TOOLS; * tests and embedders with custom executors must provide matching schemas so the * model-visible catalog and execution boundary remain identical. */ availableTools?: readonly Tool[]; /** 用户取消 */ signal?: AbortSignal; /** * 回调:宿主检查是否有排队输入需要在安全点处理。 * loop只在安全检查点调用:每轮while开始时、工具全部执行完成后。 * 未提供 takePendingMessages 时保持旧的结束回合契约;提供时暂缓旧调用, * 在下一模型边界消费信箱,保留 deliveryId。普通输入不取消 signal。 */ hasQueuedInput?: () => boolean; /** Concurrent-safe tool throughput; defaults to the safe segment size, host may tune upward. */ toolConcurrency?: number; /** * 回调:工具开始/结束执行,用于宿主跟踪运行中工具状态。 */ onToolStart?: (toolName: string, toolCallId: string) => void; onToolEnd?: (toolCallId: string, wasAborted: boolean) => void; /** * 多租户场景下注入隔离的 session 级状态;缺省走模块单例。 * TUI / print(单进程单 session)不传即可;HTTP server per-request 必须传。 */ sessionState?: SessionState; /** * 注入 chat 实现(仅测试用)。 * * 之前用 bun:test 的 mock.module 替换 llm/client.js 会进程全局污染所有跨文件 * import;改用显式 DI 后,测试传 stub 进来即可,零污染。生产代码不需要传。 */ chat?: typeof defaultChat; /** * 注入 executeTool 实现(仅测试用)。同上,DI 而非 mock.module,零污染。 */ executeTool?: (name: string, rawArgsJson: string, signal?: AbortSignal, ctx?: ToolCallContext) => Promise; /** * Trusted host persistence boundary for formal results. It runs after * PresentResult validates the files but before tool_end is published or the * result is appended to history. Throwing turns the tool call into a failure, * so a UI can never display an uncommitted delivery as durable. */ // Invocation is the acceptance boundary: the manifest was validated against // input admitted before this point. Later steering cannot roll back a durable // commit; the next model request must reconcile it, never replay generation. commitFormalDelivery?: (input: { toolCallId: string; turnId: string; manifest: DeliverableManifest; }) => Promise; /** * 工具执行闸门(通用机制,见 src/toolGate.ts):loop 在执行每个工具前 * await 它拿放行/拒绝;拒绝时把 reason 当 tool_result 回填。内核零策略——审批语义 * 全在宿主(如 gateway 跨机指令同意闸门)。缺省不传 = 全放行,行为不变。 */ toolGate?: ToolGate; /** * 仅对本次回合可见的系统上下文。它会随每次模型调用注入,但不会写入 history、 * 会话文件或压缩摘要,适合项目事实、远程委托和附件概要等外部上下文。 * * 注入到本轮用户消息的模型副本,不改变 UI 或持久化历史。 */ transientContext?: string; /** Structured transient fragments allow deterministic request-time degradation. */ transientBundle?: ContextBundle; /** Provider cache routing for this conversation; never persisted. */ promptCache?: import('../types.js').PromptCacheContext; /** 仅供宿主 UI 恢复附件卡片;不会发送给 LLM。 */ userAttachments?: import('../types.js').MessageAttachmentRef[]; /** Host-authorized files for typed workflow inputs; runtime-only paths. */ inputResources?: readonly import('../types.js').InvocationResource[]; /** Stable host-provided identity for the user message of this turn. */ userMessageId?: string; /** * Explicit delivery target chosen by the host (e.g. the user opened an open * task and asked to continue it). It wins over any textual inference; when it * is absent the turn binds only to an unambiguous open target. */ deliveryId?: string; /** Trusted host continuation; never inferred from process output. */ backgroundContinuation?: import('../tools/tasks/backgroundTaskService.js').BackgroundContinuation; agentNoticeDelivery?: import('../context/persistContext.js').AgentNoticeDelivery; /** Host owns an idle continuation queue; do not consume other tasks in this turn. */ deferBackgroundNotifications?: boolean; /** Trusted local origin for every message created by this run. */ messageProvenance?: import('../types.js').ConversationProvenance; /** Browser element chips are persisted for UI but stripped before provider calls. */ userBrowserReferences?: import('../types.js').MessageBrowserReference[]; /** 显式传入的图片附件(-p --image / stream-json images)。 */ imageInputs?: ImageInputSpec[]; /** 是否解析用户文本里的 @./image.png / 图片 URL。TUI 默认开启。 */ parseInlineImages?: boolean; /** 图片 detail 默认值:low/default/high。 */ imageDetail?: ImageDetail; /** OpenAI 兼容扩展:图片最长边像素。 */ imageMaxLongSidePixel?: number; /** Host-provided browser ownership scope; never exposed to the model. */ browserRun?: ToolCallContext['browserRun']; /** Host-provided native desktop-control scope; absent means ComputerUse is not exposed. */ computerRun?: ToolCallContext['computerRun']; /** Trusted host-only human handoff bridge for native Computer Use. */ computerIntervention?: ToolCallContext['computerIntervention']; memoryContext?: { access: import('../runtime/agentRuntimeContext.js').MemoryAccessInput; workspaceId?: string; sessionId?: string; }; }