import type { ManagedReActAgentInterceptor } from '../managed-interceptor'; import type { FinishReason } from '@agforge/core'; /** * FinishReason 拦截器配置选项 */ export type FinishReasonInterceptorOptions = { /** * 当上一轮 finishReason 为 'length' 时,注入到下一轮消息中的上下文提示。 * 引导 LLM 直接继续、不要道歉、分解工作。 * * 默认提示词借鉴 Claude Code 的设计,强调: * - 直接继续,不要道歉 * - 如果在思考中途被切断,就从中途继续 * - 将剩余工作分解成更小的部分 * * 默认:"Output token limit hit. Resume directly — no apology, no recap of what * you were doing. Pick up mid-thought if that is where the cut happened. * Break remaining work into smaller pieces." */ truncationContextPrompt?: string; /** * 当 finishReason 为 content_filter 时,注入的上下文提示。 * 引导 LLM 调整内容以通过安全过滤。 * 默认:"Your previous output was blocked by the content filter. Please rephrase * your response to comply with content policies." */ contentFilterContextPrompt?: string; /** * 最大恢复尝试次数。 * 当连续遇到 'length' 或 'content_filter' 的次数达到此限制时, * 抛出 FinishReasonError。 * 设置为 0 禁用恢复(直接抛出错误)。 * 默认:3 */ maxRecoveryAttempts?: number; }; /** * 创建 FinishReason 处理拦截器 * * 增强型拦截器,利用 LLM 返回的 finishReason 信息为 LLM 提供精确的上下文引导, * 帮助 LLM 在面对 token 截断(length)、内容过滤(content_filter)、模型拒绝(refusal)等场景时更快调整策略。 * * ## 设计策略 * * 采用"纯后处理 + 下一轮注入"的策略: * 1. llmRequest:从 metadata chunk 提取 finishReason,存入 scoped data * 2. eachLoop:下一轮开始时检查上一轮 finishReason,若为异常值则注入上下文引导或终止执行 * * ## 流式并行执行约束 * * ReActAgent 采用流式并行执行架构,工具在 LLM 响应流式过程中就开始执行。 * 由于 resolveTool/executeTool 被调用时 finishReason 可能尚未设置(存在竞争条件), * 因此不在这些拦截点中使用 finishReason,只在 eachLoop 完成后(下一轮开始时)使用。 * * ## 处理策略 * * - `length`: 注入截断上下文,引导 LLM 直接继续、分解工作 * - `content_filter`: 注入过滤上下文,引导 LLM 调整内容 * - `refusal`: 抛出 FinishReasonError,由上层根据 finishReason 属性自定义用户提示 * * @param options - 配置选项 * @returns ManagedReActAgentInterceptor 实例 */ export declare const createFinishReasonInterceptor: (options?: FinishReasonInterceptorOptions) => ManagedReActAgentInterceptor<{ finishReason?: FinishReason; recoveryAttempts?: number; }>;