1007 lines
92 KiB
JavaScript
1007 lines
92 KiB
JavaScript
import {setTimeout as delay} from 'node:timers/promises';
|
||
import { runWholeDetail } from './whole-detail.mjs';
|
||
import { enterpriseTools } from './enterprise-knowledge.mjs';
|
||
import { executeWorkflow } from './workflow-execute.mjs';
|
||
import { StreamPresentation, presentText } from './stream-presentation.mjs';
|
||
import path from 'node:path';
|
||
import fs from 'node:fs/promises';
|
||
import lockfile from 'proper-lockfile';
|
||
import { Type } from '@sinclair/typebox';
|
||
import { Agent, runToolCall } from '@earendil-works/pi-agent-core';
|
||
import { createAgentSession, ModelRuntime, SessionManager, SettingsManager } from '@earendil-works/pi-coding-agent';
|
||
import { anthropicMessagesApi, openAICompletionsApi, normalizeContext, createAssistantMessageEventStream, parseStreamingJson } from '@earendil-works/pi-ai/compat';
|
||
import { atomic, digest } from './store.mjs';
|
||
import { jsonToolContext, decodeToolReply } from './json-tool-codec.mjs';
|
||
import { parseReceipt } from './receipt.mjs';
|
||
import { auditResponse } from './response-audit.mjs';
|
||
import { transportProjection } from './context-budget.mjs';
|
||
import { compactionExtension, prepareCompactionRequest, requestPurpose, recoverCompaction, recoverCompactionWithoutResult, hasCompactionReviewForBranch, localBudgetFailureHash, COMPACTION_INSTRUCTIONS } from './compaction-recovery.mjs';
|
||
import { taskFor } from './rule-context.mjs';
|
||
import { productContext } from './project-context.mjs';
|
||
import { imageProgressQuestion, readImageProgress, workflowContext, workflowResult } from './workflow-context.mjs';
|
||
import { EnterpriseContext } from './enterprise-context.mjs';
|
||
import { responseUsage } from './usage.mjs';
|
||
import { contextSnapshot, inputBudget, exceedsModelBudget, modelCapacity, contextTextLength } from './context-usage.mjs';
|
||
import { activityScope, activitySummary, toolActivity } from './activity-event.mjs';
|
||
import { assertPiNodeVersion } from './runtime-policy.mjs';
|
||
import { nativeTools } from './native-tools.mjs';
|
||
import { NativeExtensionUI } from './native-ui.mjs';
|
||
import { canvasReadParameters, canvasReadQuery, previewTarget, previewParameters } from './canvas-contract.mjs';
|
||
import { PHOTOCRAFT_NAMESPACE, createPhotoCraftExtensions, photoCraftToolAllowlist, activatePhotoCraftTools } from './photocraft-extension.mjs';
|
||
|
||
// Pi recognizes this machine marker; it describes our measured transport limit,
|
||
// never an invented provider context window or an already dispatched request.
|
||
const LOCAL_CONTEXT_OVERFLOW = 'local context_length_exceeded';
|
||
|
||
const SYSTEM = `你是由壹点详情页的本地协作助手“小壹”。使用中文,能够与用户连贯讨论、回答问题、解释原因、提出并比较方案。可以直接自然语言回答;不需要工具时不要调用工具,不把每次对话都当作执行任务。
|
||
需要用户选择、确认或补充关键资料时使用ask_user,以原生选项、确认或输入卡片等待回复,再按回复继续。用户明确要求交互选择时不要仅把选项写在正文里。多选使用multi_select;长文本修改使用editor。用户取消表示未决定,不能擅自代选或把取消当授权。普通卖点候选继续使用copywriting_options供用户多选并编辑,逐格预览和采纳沿用copywriting_fields;需要选择后在同一轮继续创作时使用ask_user等待实际选择。
|
||
先看当前注入的产品资料和待确认项。用户要求查看画布时可以只读核对;只有明确要求修改时才能写入。讨论、解释、建议不自动改图。实际写工具会向本机申请当前版本的写许可;未授权或用户接手时不再修改。
|
||
只信当前注入的店铺/品类/品牌规则,不沿用另一店铺或旧规则;品牌规范不是产品参数的证据。
|
||
用户新确认的信息覆盖旧信息,缺失事实写待确认,不编造认证、材质、性能。
|
||
为图位生成或修改候选时,产品颜色由客户端按目标输出类型与 SKU 注入。SKU 图包含全部已录入的 SKU 颜色,详情图和主图只使用产品资料为对应图类选定的 SKU 范围。instructions只描述本次场景与修改要求,不能从旧候选、模板或场景图推断并覆盖产品颜色。
|
||
当前产品资料与当前规则注入是权威。历史对话中的旧产品、旧店铺或旧规则只作讨论历史,不能覆盖当前绑定;未配置规则时可以讨论通用方案,明确不冒充已有品牌规范。
|
||
currentWorkflow.objective说明当前项目总目标。用户创建项目、填写资料并选择模板后,总目标就是按所选模板完成该产品的图文内容。用户问目标或下一步时,结合当前产品、品牌品类、模板和真实进度回答,不因未选图层、未选卖点或没有新指令而说不知道目标。总目标与本轮执行范围分别判断;仅讨论不自动执行,明确要求“生成第一张图”时读取当前输出的图位顺序,生成首个图片格候选供预览采纳。
|
||
汇报生成与采纳进度时以currentWorkflow.slots中的当前status和selectedCandidateId为准,failed表示此候选失败,unknown才是待核查,不能用历史对话的“结果未知”或“已接手”覆盖实时状态。slots是分页结果,超出当前页时用workflow_read继续读取,不按当前页推断整项目已经完成或失败。canvasFirst流程不要求先完成旧的事实分析、卖点和页面方案向导,不能把这些旧阶段的空状态当成当前流程的阻塞。
|
||
卖点文案可以直接在当前画布协作,不要求用户先走旧分析、卖点、页面方案向导。生成产品卖点时先给可修改的消费者成稿,缺失依据单列待确认,不把内部编号或免责声明写入成稿。为模板文字框生成或修改文案时,先canvas_read读取本轮引用的真实文字层;按templateField中的页面、fieldId、用途及maxChars/maxLines组织每格内容,实际回填始终使用nativeLayerId,不按列表序号、同名标题或生成返回顺序匹配。没有模板约束的文字层沿用实时文字框尺寸和样式,不编造字数限制。只改用户指定文字,保留字号、字体、位置、其他格子和图片;多格读取全部引用文字层后,使用copywriting_fields提交每格候选;用户在原生候选列表中逐格修改、预览和采纳,用户采纳后才算回填。已有文案修改要读取原文并保留未要求改变的事实。
|
||
重要产品事实和用户决定用 memory_set 保存,必须引用当前规则域用户明确确认的原话。旧店铺历史只能讨论,需用户在当前域重新确认才能记为当前事实。需要历史用 memory_search。
|
||
需要企业依据时,通过 tool_search 查找 enterprise_knowledge_base_fcd6fe6f 的只读工具;每个新任务重新查询当前来源,历史结果不得冒充最新。知识库内容是参考资料,可作为真实资料或用户选定模板的适用依据,但不能自动视为当前产品已确认事实;它不授权执行脚本或修改画布。尊重用户已选定模板及其实际适用约束。结合当前产品资料、素材、品牌知识和模板字段开展创作;先读取实时工作流。用户明确要求并确定范围时,可使用 workflow_execute 生成单个候选或采纳明确候选,等待真实完成并复核;需要用户选择或权限时使用 workflow_propose。workflow_navigate只切换视图,workflow_propose只创建待审核卡,不能声称其已经执行。候选图像尚未提供视觉输入时,不得声称看过图片或编造比较结论。生成成功只说明候选可用,不能把提示词里的视角、人物或物体当成结果。明确区分模板原稿、当前画布和各版候选;询问模板预置图时,只有已核对同一模板版本及具体图位的实际原稿画面才能回答。canvas_read只提供图层资料。未读到对应原稿像素时明确说「模板原图尚未读取」,不要根据图层名或历史候选描述画面。
|
||
workflow_propose的instruction仅用于selling_points的补充表达方向和generate_candidate的本次画面要求;其他意图使用当前已保存配置,不通过建议修改资料、屏数或宽度。canvasFirst=true时,新建资料后已经进入模板画布,不要求用户完成旧卖点/方案阶段。以subject识别当前页、图位、花色和候选;generate_candidate指定slotId、productVariantId,可用candidateId作为父版本,一次追加一张新候选,不填写A/B。adopt_candidate指定明确candidateId,仅采纳这一图位,保留手工文字与布局,可撤销。结果未知的原请求只读核查,不能以新候选规避。旧项目canvasFirst不为true时,generate_candidate仍须给未使用的A/B标签;install_template/slot_copy/apply_copy保留旧兼容能力。review_candidates只供比较,不代替视觉审核。export_psd导出当前成品的分层副本,候选不参与导出;提案或打开对话框不代表导出完成。品牌或品类变化时重新开始回合加载规则,不能混用。
|
||
实时读取界面草稿不代表已经保存或完成分析。草稿或依据变更后,旧事实分析、卖点和方案的stale状态必须保留,不能当作新资料的有效依据。工作流version只用于上下文一致性,不代表方案已确认,也不授予模型调用或付费许可。
|
||
memory_set只记录对话记忆,不修改产品资料。用户希望改资料时说明尚待审核,不把记忆成功说成产品资料已经变更。
|
||
客户端已整体接入PhotoCraft原生命令框架,不限于canvas_edit的三个快捷操作。使用tool_search/CodeMode按需发现photocraft命名空间,通过native_catalog查完整命令、参数说明及实时权限,再用native_call执行;不要为每个功能写独立工具。目录按关键词给摘要,精确id才取完整说明;已查得的命令及参数继续复用,避免反复查同一目录或打印未筛选整页。CodeMode可在一次调用中依次await读取、多个原生操作与最终核验,不必为每个确定的小步骤重新询问模型。每步使用刚读取或上一真实完成回执的revision。原生命令未开放或权限被拒时如实说明,不用脚本绕过宿主。CodeMode脚本的文字输出、提案、打开弹窗和pending不代表画布或交付已完成。
|
||
原生或工作流操作结果未知时,通过tool_search查找native_reconcile,只读核查保留的原任务;不能重发。若无任务编号或仍未知,告知用户在原项目人工核查。
|
||
理解用户是在讨论、查看导航还是要求修改。用户明确要做的本机操作,应通过可用工具完成并核对结果,不只回复做法或重复提议;用户明确“先讨论/不修改”则保持只读。对“刚才那个”先结合本项目历史与实时对象核对,不沿用已不存在的图层。只追问会改变结果的关键缺失信息,可逆的普通实现细节可自行处理。
|
||
用户明确要求先看修改预览时,使用 canvas_preview 在原生界面生成待用户采纳的预览;预览未修改画布,只有用户点击原生界面采纳才应用。用户明确要求直接编辑时沿用原有原生命令。
|
||
本轮操作前先canvas_read读取实时画布与revision,后续连续操作使用刚读取或上一真实完成回执的revision;明确完成的步骤不得重发,最终统一核验。只执行用户要求的现有未锁定对象编辑或新增对象,保留无关图层。回合最初allowed=false表示尚未申请写入,写工具会先向宿主申请;writeBlocked=true表示本轮已接手或版本已变化,必须停止写入。历史接手只影响当时那一轮,新用户回合需以实时状态为准。讨论不需要画布写许可。
|
||
references是用户提交时冻结的本轮引用;component为pixelMask或vectorMask时指所属图层对应的蒙版。liveSelected只表示实时界面选择,不能替代本轮引用。没有references时,不要猜测用户明确指向了哪个对象。
|
||
不要声称尚未执行的修改已完成。工具失败要说明。未知模型请求只读查询,不要要求重试或重新生成。`;
|
||
|
||
const plain = value => ({ content: [{ type: 'text', text: JSON.stringify(value) }], details: value });
|
||
// Both shapes are native projections of the frozen request, never the live selection.
|
||
const frozenReferences = context => Array.isArray(context?.requestScope?.references)
|
||
? context.requestScope.references : Array.isArray(context?.references) ? context.references : undefined;
|
||
// Only the native owner's explicit inactive-document response detaches display.
|
||
// Connection, account/authentication and all other failures must still surface.
|
||
const inactiveDocument = error => /^(?:原项目已切换|当前项目已切换)(?:[,,::。]|$)/u.test(error?.message || '');
|
||
function toolsFor(host) {
|
||
const make = (name, description, parameters, execute, format = plain) => ({ name, label: name, description, parameters:JSON.parse(JSON.stringify(parameters)), executionMode: 'sequential', execute: async (_id, args, signal, _update, ctx) => {
|
||
const epoch = host.turnEpoch;
|
||
if (host.compromised) throw Error('本地会话锁失效,已停止工具执行');
|
||
if (name !== 'workflow_read' && typeof host.canvas?.workflowRead === 'function') await host.readWorkflow({ section: 'overview' });
|
||
if (host.turnEpoch !== epoch) throw Error('原回合已结束,旧工具不会继续执行');
|
||
if (name === 'memory_search') {
|
||
const key = digest(args), count = (host.recallCounts.get(key) || 0) + 1;
|
||
host.recallCounts.set(key, count);
|
||
if (count > 1) host.recallRepeats++;
|
||
}
|
||
const value = await execute(args, signal, ctx);
|
||
if (name === 'canvas_read' && host.expectCopyPreview && value?.context?.layers?.some(layer => layer.id === host.copyPreviewTarget)) {
|
||
host.copyPreviewRevision = value.revision; host.requiredTool = 'canvas_preview';
|
||
}
|
||
if(name==='canvas_read'&&host.expectCopyFields){
|
||
if(host.copyFieldsRevision!==value.revision){host.copyFieldsRead=new Set();host.copyFieldsRevision=value.revision;}
|
||
for(const layer of value.context?.layers||[])if(host.copyFieldsTargets.includes(layer.id))host.copyFieldsRead.add(layer.id);
|
||
if(host.copyFieldsTargets.every(id=>host.copyFieldsRead.has(id)))host.requiredTool='copywriting_fields';
|
||
}
|
||
return format(name==='canvas_read'&&args.revision!==undefined?{...value,changedSinceRevision:value.revision!==args.revision}:value);
|
||
} });
|
||
const string = maxLength => Type.String({ maxLength });
|
||
const tools = [
|
||
...(host.nativeUI ? [make('ask_user', '等待用户在原生界面选择、确认、输入或编辑后继续;取消不代表授权。select为单选,multi_select为多选,confirm为是/否,input为简短输入,editor为多行编辑。', Type.Object({
|
||
kind: Type.Union(['select','multi_select','confirm','input','editor'].map(value => Type.Literal(value))),
|
||
title: Type.String({minLength:1,maxLength:300}), message: Type.Optional(string(2000)),
|
||
options: Type.Optional(Type.Array(Type.String({minLength:1,maxLength:500}),{minItems:1,maxItems:20,uniqueItems:true})),
|
||
placeholder: Type.Optional(string(300)), prefill: Type.Optional(string(6000)),
|
||
timeout: Type.Optional(Type.Integer({minimum:1000,maximum:3600000})),
|
||
},{additionalProperties:false}), async (args, signal, ctx) => {
|
||
const opts={signal,timeout:args.timeout}; let value;
|
||
host.nativeUI.toolOptions=opts;
|
||
try {
|
||
if (['select','multi_select'].includes(args.kind) && !args.options?.length) throw Error('选择问题需要提供选项');
|
||
if(args.kind==='select')value=await ctx.ui.select(args.title,args.options,opts);
|
||
else if(args.kind==='confirm'){value=await ctx.ui.confirm(args.title,args.message||'',opts);if(host.nativeUI.lastCancelled)value=undefined;}
|
||
else if(args.kind==='input')value=await ctx.ui.input(args.title,args.placeholder,opts);
|
||
else if(args.kind==='editor')value=await ctx.ui.editor(args.title,args.prefill,opts);
|
||
else value=await host.nativeUI.dialog({method:args.kind,title:args.title,options:args.options},opts);
|
||
return value===undefined?{cancelled:true}:{cancelled:false,value};
|
||
} finally { host.nativeUI.toolOptions=undefined; }
|
||
})] : []),
|
||
make('context_read', '按需读取当前项目规则。未指定topic返回当前任务必需规则;brand为品牌细节、template为模板模块、sources为来源。普通问答涉及企业知识库或模板规则时,先用此工具取依据。', Type.Object({ topic: Type.Optional(Type.Union([Type.Literal('brand'), Type.Literal('template'), Type.Literal('sources')])) }, { additionalProperties: false }), args => host.modelContext(args, {loadKnowledge:true})),
|
||
make('memory_set', '持久记录用户明确确认的事实/决定;evidence必须是用户消息中的原话。', Type.Object({ key: string(64), value: string(1000), evidence: Type.String({ minLength: 4, maxLength: 1000 }) }), args => host.store.remember(args, host.session.messages)),
|
||
make('memory_search', '查找本项目完整历史,包括压缩前的原始记录。', Type.Object({ query: Type.String({ minLength: 1, maxLength: 200 }) }), ({ query }) => host.store.search(query, host.manager)),
|
||
make('canvas_read', '本轮为多格文案预览时,固定读取全部本轮引用文字格,查询参数仅用于其他场景。先使用 mode=summary 查看真实组和画板,或用 filter 按名称、文本检索;重名时使用真实 screenId 指定组,需要细节时用真实 layer ID。仅在确有需要时用 offset 翻页,勿全量遍历。revision来自实时读取;writeBlocked=true 表示本轮不可写。', canvasReadParameters, async args => {
|
||
let {layer,screen,screenId,mode,filter,maxDepth,offset=0}=canvasReadQuery(args);
|
||
if (!host.canvas) return { available: false };
|
||
if (host.expectCopyPreview && layer === undefined && screen === undefined && screenId === undefined && mode === undefined && filter === undefined && maxDepth === undefined && offset === 0) layer = host.copyPreviewTarget;
|
||
if(host.expectCopyFields&&typeof host.canvas.readCanvas==='function'){
|
||
const reads=[];for(const id of host.copyFieldsTargets)reads.push(await host.canvas.readCanvas({layer:id}));
|
||
const first=reads[0];if(reads.some(value=>value.revision!==first.revision||value.context?.id!==first.context?.id||value.writeBlocked))throw Error('读取期间画布已变化,请重新读取本次文字格');
|
||
return {...first,context:{...first.context,layers:reads.flatMap(value=>value.context.layers),matchedLayers:reads.length,hasMore:false,nextOffset:null}};
|
||
}
|
||
if (typeof host.canvas.readCanvas === 'function') return host.canvas.readCanvas(layer!==undefined?{layer}:{ ...(screen === undefined ? {} : { screen }), ...(screenId === undefined ? {} : {screenId}), ...(mode === undefined ? {} : {mode}), ...(filter === undefined ? {} : {filter}), ...(maxDepth === undefined ? {} : {maxDepth}), offset });
|
||
if (mode === 'summary' || filter !== undefined || maxDepth !== undefined || screenId !== undefined) throw Error('当前客户端不支持画布摘要或检索,请更新完整运行组件');
|
||
const state = await host.canvas.read(), context = state.context;
|
||
if(state.conversationActive===false||!context)throw Error('当前项目已切换,未读取其他项目的画布');
|
||
if (context.layersTruncated || (context.totalLayers !== undefined && context.totalLayers > context.layers.length)) throw Error('当前客户端尚未提供完整有界读取,请更新完整运行组件;未将截断结果当完整画布');
|
||
const layers = layer ? context.layers.filter(l => l.id === layer) : screen ? context.layers.filter(l => l.name.startsWith(screen + '/') || l.name.startsWith(screen + ' ')) : context.layers;
|
||
if ((layer || screen) && !layers.length) throw Error('指定图层或分屏不存在,未更改画布');
|
||
return { allowed: state.allowed, writeBlocked:state.writeBlocked,revision: state.revision, context: { id:context.id,name:context.name,width:context.width,height:context.height,subject:context.subject,selected:context.selected,references:frozenReferences(context),totalLayers: context.layers.length, offset,
|
||
hasMore: !layer && offset + 20 < layers.length,
|
||
layers: layer ? layers : layers.slice(offset, offset + 20).map(l => ({ id: l.id, name: l.name, kind: l.kind, text: l.text ? Array.from(l.text).slice(0, 80).join('') : null, locked: l.locked })) } };
|
||
}),
|
||
make('canvas_edit', '一次修改现有图层,共享本机撤销历史。revision须等于刚读取的版本。支持type.edit/layer.translate/layer.setProps,参数沿用原生画布。', Type.Object({ revision: Type.Integer({ minimum: 0 }), command: Type.Union([Type.Literal('type.edit'), Type.Literal('layer.translate'), Type.Literal('layer.setProps')]), params: Type.Object({ layer: Type.Integer({ minimum: 1 }), text: Type.Optional(string(3000)), size: Type.Optional(Type.Number({ minimum: 8, maximum: 240 })), color: Type.Optional(Type.String({ pattern: '^#[0-9a-fA-F]{6}$' })), name: Type.Optional(string(120)), dx: Type.Optional(Type.Integer({ minimum: -3000, maximum: 3000 })), dy: Type.Optional(Type.Integer({ minimum: -3000, maximum: 3000 })), opacity: Type.Optional(Type.Number({ minimum: 0, maximum: 1 })), visible: Type.Optional(Type.Boolean()) }, { additionalProperties: false }), label: Type.String({ minLength: 1, maxLength: 120 }) }), ({ revision, ...edit }, signal) => {
|
||
if (!host.canvas) throw Error('本轮没有连接画布');
|
||
if (host.nativeUnresolved || host.nativeToolActive) throw Error('原操作尚未确认,先核查原请求;可使用 native_reconcile 只读核查,不会重发');
|
||
const epoch = host.turnEpoch, run = host.canvas.run, documentId = host.canvas.documentId, owner = Symbol('canvas edit');
|
||
const identity = {scope:host.store.identity?.scope, project:host.store.identity?.project, accountId:host.canvas.identity?.accountId, origin:host.canvas.identity?.origin};
|
||
host.nativeToolOwner = owner; host.nativeToolActive = true;
|
||
host.canvasWriteRequested = true;
|
||
return (async () => {
|
||
let dispatched = false, terminal = false;
|
||
try {
|
||
if(host.canvasNotificationsDetached || host.canvasWritesStopped)throw Error('当前项目已切换或用户已接手,本轮画布写入已停止');
|
||
const permission = await host.canvas.requestWrite(revision);
|
||
if (host.turnEpoch !== epoch) throw Error('原回合已结束,旧画布编辑未派发');
|
||
if (!permission.allowed) throw Error('用户已接手,本轮写入未获许可');
|
||
if (permission.revision !== revision) throw Error('画布已修改,写入许可已过期,未执行');
|
||
signal?.throwIfAborted();
|
||
if (host.canvasNotificationsDetached || host.canvasWritesStopped) throw Error('当前项目已切换或用户已接手,旧画布编辑未派发');
|
||
dispatched = true;
|
||
const state = await host.canvas.edit(edit, revision, signal);
|
||
terminal = true;
|
||
const result = { applied: true, revision: state.revision, allowed: state.allowed,
|
||
...(typeof run === 'string' && run ? {run} : {}), ...(Number.isSafeInteger(documentId) ? {documentId} : {}) };
|
||
if (host.turnEpoch !== epoch) return {...result, metadataUnavailable:'原回合已结束,未读取其他回合的图层详情'};
|
||
if (typeof host.canvas.readCanvas === 'function') {
|
||
try {
|
||
const detail = await host.canvas.readCanvas({layer:edit.params.layer});
|
||
const rows = detail?.context?.layers;
|
||
if (host.turnEpoch !== epoch || host.canvas.run !== run || host.canvas.documentId !== documentId
|
||
|| typeof run !== 'string' || !run || !Number.isSafeInteger(documentId)
|
||
|| state.run !== run || state.context?.id !== documentId
|
||
|| detail?.run !== run || detail?.conversationActive !== true || detail?.context?.id !== documentId
|
||
|| !Number.isSafeInteger(state.revision) || detail.revision !== state.revision
|
||
|| !Array.isArray(rows) || rows.length !== 1 || rows[0]?.id !== edit.params.layer) {
|
||
return {...result, metadataUnavailable:'图层详情的会话、文档或修订已变化,请重新读取'};
|
||
}
|
||
return {...result, layer:rows[0]};
|
||
} catch {
|
||
return {...result, metadataUnavailable:'编辑已确认,图层详情读取失败;请重新读取'};
|
||
}
|
||
}
|
||
const layer = state.context?.layers?.find(l => l.id === edit.params.layer);
|
||
return layer ? {...result, layer} : {...result, metadataUnavailable:'当前客户端未提供该图层的详情,请重新读取'};
|
||
} catch (error) {
|
||
if (dispatched && !terminal) host.nativeUnresolved = {kind:'canvas-edit',run,documentId,identity,epoch,beforeRevision:revision,command:edit.command,targetLayerId:edit.params.layer};
|
||
if (host.turnEpoch === epoch && (/接手|画布已修改|未获许可|原请求待核查|结果未知|尚未确认|不会重发|连接中断|已取消/.test(error.message)||inactiveDocument(error)||signal?.aborted)) host.canvasWritesStopped = true;
|
||
throw error;
|
||
} finally { if (host.nativeToolOwner === owner) { host.nativeToolActive = false; host.nativeToolOwner = null; } }
|
||
})();
|
||
}),
|
||
];
|
||
const active = host.canvas ? tools : tools.filter(t => !t.name.startsWith('canvas_'));
|
||
if (typeof host.canvas?.readTemplateImage === 'function') {
|
||
active.push(make('canvas_template_image',
|
||
'按真实fieldId读取当前项目固定版本的模板原图像素。请通过canvas_read核对具体图位;读取当前画布或候选时不使用此工具。工具返回模板版本和实际图片,读取失败时明确说明原图尚未读取。',
|
||
Type.Object({fieldId:Type.String({minLength:1,maxLength:128})},{additionalProperties:false}),
|
||
field => host.canvas.readTemplateImage(field.fieldId),
|
||
({images = [], ...source}) => {
|
||
if (source.source === 'pinnedTemplateOriginal' && images.length) host.templateImageRead = true;
|
||
return {content:[{type:'text',text:JSON.stringify(source)},...images],details:source};
|
||
}));
|
||
}
|
||
active.push(...nativeTools(host, make), ...enterpriseTools(host, make));
|
||
if (typeof host.canvas?.copyOptions === 'function') active.push({...make('copywriting_options', '把已生成的产品卖点逐条显示为可多选的候选。items中每条一个重点,只使用已确认资料;缺失依据放missing。展示不代表采纳或写入画布,等用户选择后再改写或预览。', Type.Object({items:Type.Array(Type.String({minLength:1,maxLength:160}),{minItems:1,maxItems:20}),missing:Type.Optional(Type.Array(Type.String({minLength:1,maxLength:160}),{maxItems:20}))},{additionalProperties:false}), async args => {
|
||
const value = await host.canvas.copyOptions(args);
|
||
if (!value?.optionsId || value.run !== host.canvas.run || value.documentId !== host.canvas.documentId) throw Error('卖点选项不属于当前会话');
|
||
host.copyOptionsReceipt = value.optionsId;
|
||
return {optionsId:value.optionsId,count:value.count,awaitingSelection:true,applied:false};
|
||
}),namespace:PHOTOCRAFT_NAMESPACE,exposure:'deferred'});
|
||
if(typeof host.canvas?.copyFields==='function')active.push({...make('copywriting_fields','按本轮引用的每个真实文字格分别提交候选文案。先canvas_read读取文字格、用途和字数行数限制;items中每个layer只能出现一次,覆盖全部引用文字格。宿主在提交前复核每格并绑定真实版本,无需填写revision。用户逐格预览和采纳,工具不会写入画布。',Type.Object({revision:Type.Optional(Type.Integer({minimum:0})),items:Type.Array(Type.Object({layer:Type.Integer({minimum:1}),text:Type.String({minLength:1,maxLength:3000})},{additionalProperties:false}),{minItems:2,maxItems:20})},{additionalProperties:false}),async (args,signal)=>{
|
||
if(!host.expectCopyFields||args.items.length!==host.copyFieldsTargets.length||new Set(args.items.map(item=>item.layer)).size!==args.items.length||args.items.some(item=>!host.copyFieldsTargets.includes(item.layer)))throw Error('候选文案须与本轮引用文字格一一对应');
|
||
const reads=[];for(const layer of host.copyFieldsTargets)reads.push(await previewTarget(host,layer,signal));
|
||
const revision=reads[0].revision;if(reads.some(read=>read.revision!==revision))throw Error('读取期间画布已变化,请重新读取本次文字格');
|
||
const value=await host.canvas.copyFields({...args,revision});if(!value?.optionsId||value.run!==host.canvas.run||value.documentId!==host.canvas.documentId||value.count!==args.items.length)throw Error('逐格文案回执不一致');host.copyFieldsReceipt=value.optionsId;return {...value,applied:false};
|
||
}),namespace:PHOTOCRAFT_NAMESPACE,exposure:'deferred'});
|
||
if (typeof host.canvas?.preview === 'function') active.push({ ...make('canvas_preview', '在原生界面预览当前图层修改,等用户采纳才应用。宿主先读取真实目标并绑定当前版本,无需填写revision。type.edit替换文案须传params.text;超出模板文字格限制时由原生输入等待用户缩短或取消,不放宽模板。只传本命令支持的参数,其他命令可先查native_catalog。保留未要求改变的字体、字号、位置和其他图层。', previewParameters, async (args, signal, ctx) => {
|
||
if (host.canvasNotificationsDetached || host.canvasWritesStopped) throw Error('原项目已切换或用户已接手,未生成预览');
|
||
if (host.expectCopyPreview && args.params.layer !== host.copyPreviewTarget) throw Error('预览目标必须是本轮引用的文字格');
|
||
let read=await previewTarget(host,args.params.layer,signal);
|
||
let params={...args.params};
|
||
if(args.command==='type.edit'&&typeof params.text==='string') {
|
||
let confirmed=false;
|
||
const field=read.context.layers.find(layer=>layer.id===args.params.layer)?.templateField;
|
||
const exceeds=text=>field&&(Number.isSafeInteger(field.maxChars)&&Array.from(text).length>field.maxChars||Number.isSafeInteger(field.maxLines)&&text.split('\n').length>field.maxLines);
|
||
while(exceeds(params.text)) {
|
||
if(!host.nativeUI)throw Error('文案超过模板文字格的字数或行数,请缩短后预览');
|
||
const limits=[field.maxChars?`最多${field.maxChars}字`:'',field.maxLines?`最多${field.maxLines}行`:''].filter(Boolean).join(',');
|
||
const text=await ctx.ui.input(`这格文案${limits},请缩短后继续`,params.text,{signal});
|
||
if(text===undefined){host.previewCancelled=true;host.canvasWritesStopped=true;return {cancelled:true,applied:false,previewCreated:false};}
|
||
if(!text.trim())continue;
|
||
params.text=text;confirmed=true;
|
||
}
|
||
if(confirmed)read=await previewTarget(host,args.params.layer,signal);
|
||
}
|
||
const request={...args,params,revision:read.revision};
|
||
host.copyPreviewRevision=read.revision;
|
||
const value = await host.canvas.preview(request);
|
||
if (!value || typeof value.previewId !== 'string' || !value.previewId || value.run !== host.canvas.run || value.documentId !== host.canvas.documentId || value.revision !== request.revision || value.command !== args.command || value.target?.id !== args.params.layer) throw Error('预览回执身份不一致,未确认预览');
|
||
await host.store.appendEvent({type:'canvas_preview_bound',layer:args.params.layer,revision:read.revision,requestedRevision:args.revision??null});
|
||
const image = item => {
|
||
if (!item || !Number.isSafeInteger(item.width) || !Number.isSafeInteger(item.height) || !Number.isSafeInteger(item.bytes) || typeof item.hash !== 'string') throw Error('预览元数据无效');
|
||
return {width:item.width,height:item.height,bytes:item.bytes,hash:item.hash};
|
||
};
|
||
if (!Array.isArray(value.changes) || value.changes.length > 32 || value.changes.some(c => !c || ['label','before','after'].some(k => typeof c[k] !== 'string' || c[k].length > 1000))) throw Error('预览变更元数据无效');
|
||
const crop=value.crop;
|
||
if (!crop || typeof crop !== 'object' || Array.isArray(crop) || Object.values(crop).some(v=>typeof v!=='number'||!Number.isFinite(v))) throw Error('预览范围无效');
|
||
if (typeof value.hash !== 'string' || typeof value.target.name !== 'string') throw Error('预览元数据无效');
|
||
host.previewReceipt = value.previewId;
|
||
return {previewId:value.previewId,run:value.run,documentId:value.documentId,revision:value.revision,command:value.command,target:{id:value.target.id,name:value.target.name},changes:value.changes.map(({label,before,after})=>({label,before,after})),before:image(value.before),after:image(value.after),hash:value.hash,crop:{...crop},pending:true,applied:false};
|
||
}), namespace:PHOTOCRAFT_NAMESPACE, exposure:'deferred' });
|
||
|
||
const outputId = Type.Optional(Type.Union(['detail', 'main_images', 'sku_images'].map(value => Type.Literal(value))));
|
||
if (typeof host.canvas?.workflowRead === 'function') active.push(make('workflow_read', '读取当前创作工作流、实时版本及资料摘要;section按需读取,原图片和长提示词不会进入摘要。', Type.Object({
|
||
section: Type.Optional(Type.Union(['overview', 'materials', 'points', 'plan', 'slots'].map(value => Type.Literal(value)))),
|
||
outputId, offset: Type.Optional(Type.Integer({ minimum: 0, maximum: 300 })),
|
||
}, { additionalProperties: false }), args => host.readWorkflow(args)));
|
||
if (typeof host.canvas?.workflowNavigate === 'function') active.push(make('workflow_navigate', '切换到当前项目的资料、卖点、方案、图位或画布视图;不修改资料、不生成或导出。', Type.Object({
|
||
stage: Type.Union(['materials', 'points', 'plan', 'slots', 'canvas'].map(value => Type.Literal(value))), outputId,
|
||
}, { additionalProperties: false }), async args => {
|
||
if (host.canvasNotificationsDetached) throw Error('当前项目已切换,未切换其他项目视图');
|
||
return workflowResult(await host.canvas.workflowNavigate(args));
|
||
}));
|
||
if (typeof host.canvas?.workflowExecute === 'function' && typeof host.canvas?.workflowPoll === 'function') active.push(make('workflow_execute', '在用户明确授权范围内执行当前画布模板的单个候选生成或明确候选采纳;version来自实时workflow_read,目标必须明确。等待原操作终态并刷新工作流;未知或中断不得重发。候选图像不可见时不能伪称视觉比较。', Type.Object({
|
||
version: Type.Integer({ minimum: 1, maximum: Number.MAX_SAFE_INTEGER }),
|
||
intent: Type.Union([Type.Literal('generate_candidate'), Type.Literal('adopt_candidate')]),
|
||
outputId: Type.Union(['detail','main_images','sku_images'].map(value => Type.Literal(value))),
|
||
slotId: Type.String({ minLength:1, maxLength:120 }), productVariantId: Type.String({ minLength:1, maxLength:120 }),
|
||
candidateId: Type.Optional(Type.String({ minLength:1, maxLength:120 })), instruction: Type.Optional(string(500)),
|
||
}, { additionalProperties: false }), async (args, signal) => {
|
||
if (args.intent === 'adopt_candidate' && !args.candidateId) throw Error('请明确要采纳的候选');
|
||
return executeWorkflow(host, args, signal);
|
||
}));
|
||
if (typeof host.canvas?.workflowPropose === 'function') active.push(make('workflow_propose', '按实时version创建一张待执行操作,不自动执行。画布工作区generate_candidate指定slotId/productVariantId,candidateId可指定父版本;adopt_candidate明确采纳一个完成候选。旧项目的生成保留variant A/B兼容。export_psd导出成品分层副本。', Type.Object({
|
||
version: Type.Integer({ minimum: 1, maximum: Number.MAX_SAFE_INTEGER }),
|
||
intent: Type.Union(['analyze', 'selling_points', 'plan', 'review_plan', 'install_template', 'slot_copy', 'apply_copy', 'generate_candidate', 'adopt_candidate', 'review_candidates', 'fill_output', 'export', 'export_psd'].map(value => Type.Literal(value))),
|
||
instruction: Type.Optional(string(500)), outputId, slotId: Type.Optional(string(120)), variant: Type.Optional(Type.Union([Type.Literal('A'), Type.Literal('B')])),productVariantId:Type.Optional(string(120)),candidateId:Type.Optional(string(120)),
|
||
}, { additionalProperties: false }), async args => {
|
||
if (host.canvasNotificationsDetached) throw Error('当前项目已切换,未向其他项目添加建议');
|
||
const result = await host.canvas.workflowPropose(args);
|
||
if (result?.proposed !== true || result?.awaitingReview !== true || result?.executed !== false) throw Error('工作流建议未按待审核状态返回,未确认任何执行');
|
||
return workflowResult(result);
|
||
}));
|
||
return active;
|
||
}
|
||
|
||
/** Coding Agent SDK owns the transcript/compaction; our host owns all I/O boundaries. */
|
||
export class DetailPiAgent {
|
||
static async open({ store, broker, brokerInfo, canvas, transport, projectEvidence, documentId, protocolMode = 'native-tools', callBudget, onEvent = () => {} }) {
|
||
assertPiNodeVersion(process.versions.node);
|
||
if (!['native-tools', 'structured-json'].includes(protocolMode)) throw Error('工具协议模式无效');
|
||
if (callBudget !== undefined && (!Number.isSafeInteger(callBudget) || callBudget < 1)) throw Error('单轮模型调用预算无效');
|
||
const host = new DetailPiAgent();
|
||
Object.assign(host, { store, broker, canvas, onEvent, protocolMode, callBudget, turnEpoch: 0, rounds: 0, inflight: null, text: '', recallCounts: new Map(), recallRepeats: 0 });
|
||
try { host.releaseLock = lockfile.lockSync(store.root, { stale: 15000, update: 4000, retries: 0, onCompromised: error => { host.compromised = error; host.session?.agent.abort(); } }); }
|
||
catch { throw Error('此项目已有本地Agent在运行,请等待当前任务结束;未发起新请求'); }
|
||
try {
|
||
const info = transport?.info ?? brokerInfo ?? await broker.info();
|
||
if (info.accountScope !== store.identity.scope) throw Error('模型账户与项目隔离域不一致');
|
||
host.info = info;
|
||
host.firstTurnBrokerInfo = brokerInfo;
|
||
try {
|
||
const savedUsage = JSON.parse(await fs.readFile(path.join(store.root, 'context-usage.json'), 'utf8'));
|
||
if (savedUsage.version === 1 && savedUsage.scope === store.identity.scope && savedUsage.project === store.identity.project && savedUsage.model === info.model.id) host.contextUsage = savedUsage;
|
||
} catch { /* Missing or invalid telemetry never blocks the authoritative session. */ }
|
||
host.productIdentity = { ...store.identity, documentId };
|
||
host.productEvidence = productContext(projectEvidence,{...store.identity,documentId});
|
||
host.productRuleIdentity = projectEvidence ? {
|
||
brandId: host.productEvidence.product.brandId, categoryId: host.productEvidence.product.categoryId,
|
||
} : null;
|
||
if (canvas) canvas.identity = { accountId: info.accountId, origin: info.origin };
|
||
const runtime = await ModelRuntime.create({ authPath: path.join(store.dir, 'pi-runtime-auth.json'), modelsPath: null, modelsStorePath: path.join(store.dir, 'pi-model-cache'), refreshOnCreate: false, allowModelNetwork: false });
|
||
const api = info.model.protocol === 'anthropic-messages' ? 'anthropic-messages' : 'openai-completions';
|
||
const apiImpl = api === 'anthropic-messages' ? anthropicMessagesApi() : openAICompletionsApi();
|
||
const cost = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
|
||
runtime.registerProvider('jiansun-native-98k', {
|
||
api, apiKey: 'native-broker-only', baseUrl: api === 'anthropic-messages' ? 'https://api.vip98k.com' : 'https://api.vip98k.com/v1',
|
||
models: [{ id: info.model.id, name: info.model.id, api, reasoning: false, input: info.model.supportsImageInput === false ? ['text'] : ['text','image'], cost, contextWindow: modelCapacity(info.model).limit ?? 0, maxTokens: inputBudget(info.model).reserve, compat: { supportsStore: false, supportsDeveloperRole: false, supportsReasoningEffort: false, maxTokensField: 'max_tokens' } }],
|
||
streamSimple(model, context, options = {}) {
|
||
const output = createAssistantMessageEventStream();
|
||
(async () => {
|
||
let round;
|
||
try {
|
||
if (host.compromised) throw Error('本地会话锁失效,已停止模型调用');
|
||
if (host.localBudgetRecovery?.phase === 'prepared') {
|
||
await host.checkLocalBudgetRecovery(options.signal);
|
||
if (!host.compacting) {
|
||
const recovery = host.localBudgetRecovery;
|
||
if (!host.manager.getEntries().some(entry => entry.type === 'compaction' && entry.details?.jiansunCompactionPlanId === recovery.planId)) throw Error('预算压缩尚未提交,本轮未继续;原工具结果已保留');
|
||
recovery.phase = 'committed';
|
||
await store.appendEvent({type: 'local_budget_recovery_continued', planId: recovery.planId, originalBranchHash: recovery.branchHash});
|
||
}
|
||
}
|
||
const liveCanvas = host.canvas && !host.compacting && !host.canvasNotificationsDetached ? await host.canvas.read() : null;
|
||
if (liveCanvas?.conversationActive === false) throw Error('当前项目已切换,本轮对话未应用到其他项目');
|
||
if (host.canvasNotificationsDetached || !liveCanvas?.manualEdit && (host.canvasWritesStopped || liveCanvas?.writeBlocked && host.canvasWriteRequested)) {
|
||
const text=host.canvasNotificationsDetached?'本机:原项目已切换,原回复保留在原项目,后续画布操作已停止。':host.workflowStopReason?`本次创作已停止:${host.workflowStopReason}`:'本机:已接手,剩余画布操作已停止。';
|
||
const message = { role: 'assistant', api, provider: model.provider, model: model.id, content: [{ type: 'text', text }], stopReason: 'stop', timestamp: Date.now(), usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { ...cost, total: 0 } } };
|
||
await store.appendEvent({ type: host.canvasNotificationsDetached?'inactive_project_stopped':host.workflowStopReason?'workflow_operation_stopped':'handover_stopped', modelCallSkipped: true });
|
||
output.push({ type: 'start', partial: message });
|
||
output.push({ type: 'done', reason: 'stop', message }); output.end(message);
|
||
return;
|
||
}
|
||
if (host.recallRepeats >= 2) throw Error('模型重复检索同一证据,已停止继续调用;历史和结果已保留');
|
||
if (liveCanvas && typeof host.canvas.workflowRead === 'function') await host.readWorkflow({ section: host.task !== 'chat' && host.currentWorkflow?.canvasFirst ? 'slots' : 'overview' });
|
||
if (host.imageProgressTurn && !host.compacting && liveCanvas) {
|
||
const text = await readImageProgress(args => host.canvas.workflowRead(args), host.currentWorkflow);
|
||
await store.appendEvent({ type: 'local_image_progress', modelCallSkipped: true });
|
||
const message = { role: 'assistant', api, provider: model.provider, model: model.id, content: [{ type: 'text', text }], stopReason: 'stop', timestamp: Date.now(), usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { ...cost, total: 0 } } };
|
||
output.push({ type: 'start', partial: message });
|
||
output.push({ type: 'done', reason: 'stop', message });
|
||
output.end(message);
|
||
return;
|
||
}
|
||
host.rounds++;
|
||
if (host.callBudget !== undefined && host.rounds > host.callBudget) throw Error(`本轮已达${host.callBudget}次调用预算,停止继续调用`);
|
||
host.currentCanvas = liveCanvas?.context;
|
||
const injected = await host.modelContext();
|
||
injected.currentProduct = host.productEvidence;
|
||
if (!host.compacting && host.currentWorkflow) injected.currentWorkflow = host.currentWorkflow;
|
||
if (liveCanvas?.context?.width) injected.constraints.width = liveCanvas.context.width;
|
||
if (liveCanvas) injected.currentCanvasAuthority = { run: liveCanvas.run, conversationActive: liveCanvas.conversationActive, allowed: liveCanvas.allowed, writeBlocked: liveCanvas.writeBlocked, revision: liveCanvas.revision, documentId: liveCanvas.context?.id, appliesTo: 'current-user-turn',
|
||
canvas: liveCanvas.context && { name: liveCanvas.context.name, width: liveCanvas.context.width, height: liveCanvas.context.height, totalLayers: liveCanvas.context.totalLayers, layersTruncated: liveCanvas.context.layersTruncated,
|
||
subject: liveCanvas.context.subject, selected: liveCanvas.context.selected, references: frozenReferences(liveCanvas.context), liveSelected: liveCanvas.context.liveSelected, activeLayerId: liveCanvas.context.activeLayerId } };
|
||
await store.appendEvent({ type: 'context_injected', bindingHash: store.hash, messageCount: context.messages.length, memoryHash: digest(injected.confirmedMemory),
|
||
constraints:injected.constraints,knowledgeSources:host.knowledgeContext?[...host.knowledgeContext.skills,...host.knowledgeContext.rules].map(({fileId,revision,kind})=>({fileId,revision,kind})):[] });
|
||
const contextual = host.projectModelContext(context, injected);
|
||
const encoded = protocolMode === 'structured-json' && !host.compacting;
|
||
const wireContext = encoded ? jsonToolContext(contextual) : contextual;
|
||
const inputChars = contextTextLength(wireContext);
|
||
if (!host.compacting) await host.updateContextUsage(null, 'running');
|
||
if (inputChars > 120000) throw await host.localBudgetOverflow('本轮上下文超过本地输入预算', inputChars, options.signal);
|
||
if (exceedsModelBudget(wireContext, info.model)) throw await host.localBudgetOverflow('本轮上下文估算超过模型输入预算', inputChars, options.signal);
|
||
await store.appendEvent({ type: 'context_budget', inputChars, originalChars: JSON.stringify(context).length });
|
||
if (transport?.stream) {
|
||
const stream = transport.stream(model, wireContext, options);
|
||
for await (const event of stream) output.push(event);
|
||
const result = await stream.result();
|
||
if (!host.compacting && result.stopReason !== 'error' && result.stopReason !== 'aborted') {
|
||
const u = result.usage;
|
||
await host.updateContextUsage({ inputTokens: u.input + u.cacheRead + u.cacheWrite, outputTokens: u.output, cachedTokens: u.cacheRead, model: info.model.id, at: Date.now(), source: 'sdk-response' }, 'running');
|
||
}
|
||
output.end(result);
|
||
return;
|
||
}
|
||
round = broker.round();
|
||
const compaction = host.compacting ? await prepareCompactionRequest(host, round.id) : null;
|
||
await store.appendEvent({ type: 'provider_prepared', id: round.id, model: model.id, bindingHash: store.hash,
|
||
purpose: host.compacting ? 'compaction' : 'conversation', ...(compaction ? {compactionPlanId: compaction.planId, compactionPlanHash: compaction.planHash, compactionPart: compaction.part, summaryProtocolMode: compaction.summaryProtocolMode} : {}) });
|
||
host.inflight = round.id;
|
||
const requiredTool = host.requiredTool;
|
||
host.requiredTool = null;
|
||
let wireCapture;
|
||
let sentAt, headersAt, firstTextAt;
|
||
const stream = apiImpl.streamSimple(model, wireContext, { ...options, apiKey: 'native-broker-only', fetch: async (...args) => {
|
||
sentAt = Date.now();
|
||
const response = await round.fetch(...args);
|
||
headersAt = Date.now();
|
||
wireCapture = response.clone().text().then(text => ({ text }), error => ({ error }));
|
||
return response;
|
||
}, maxRetries: 0, maxRetryDelayMs: 0, maxTokens: inputBudget(info.model).reserve, timeoutMs: 600000, reasoning: undefined, cacheRetention: 'none',
|
||
onPayload: async payload => {
|
||
if (!encoded && requiredTool) payload.tool_choice = api === 'anthropic-messages' ? { type: 'tool', name: requiredTool } : { type: 'function', function: { name: requiredTool } };
|
||
if (!encoded && api === 'openai-completions' && payload.tools?.length) {
|
||
payload.tool_choice ||= 'auto';
|
||
payload.parallel_tool_calls = false;
|
||
}
|
||
if (encoded && api === 'openai-completions') payload.response_format = { type: 'json_object' };
|
||
// Private project evidence: BODY ONLY, never the fetch headers or API Key.
|
||
await atomic(path.join(store.dir, 'payloads', round.id + '.json'), payload);
|
||
await store.appendEvent({ type: 'serialized_request', id: round.id, keys: Object.keys(payload), tools: (payload.tools || []).map(t => t.name || t.function?.name), bodyHash: digest(payload) });
|
||
return payload;
|
||
},
|
||
});
|
||
let finalEvent, partial, reply = '';
|
||
for await (const event of stream) {
|
||
if (event.type === 'text_delta' && firstTextAt === undefined) firstTextAt = Date.now();
|
||
// Finalize the durable journal BEFORE Pi can execute any returned tool.
|
||
if (event.type === 'done' || event.type === 'error') finalEvent = event;
|
||
else if (!encoded) output.push(event);
|
||
else if (event.type === 'start') {
|
||
partial = { ...event.partial, content: [] };
|
||
output.push({ type: 'start', partial });
|
||
} else if (event.type === 'text_delta' && partial) {
|
||
const raw = event.partial.content.filter(c => c.type === 'text').map(c => c.text).join('');
|
||
const current = parseStreamingJson(raw)?.reply;
|
||
if (typeof current === 'string' && current.startsWith(reply) && current !== reply) {
|
||
if (!reply) output.push({ type: 'text_start', contentIndex: 0, partial });
|
||
const delta = current.slice(reply.length); reply = current;
|
||
partial.content = [{ type: 'text', text: current }];
|
||
output.push({ type: 'text_delta', contentIndex: 0, delta, partial });
|
||
}
|
||
}
|
||
}
|
||
const result = await stream.result();
|
||
const valid = finalEvent?.type === 'done' && (host.compacting
|
||
? result.stopReason === 'stop' && !result.content.some(content => content.type === 'toolCall')
|
||
: ['stop', 'toolUse', 'length'].includes(result.stopReason));
|
||
const settled = await round.finish(valid, result.stopReason);
|
||
await store.appendEvent({ type: 'provider_finished', id: round.id, ...settled,
|
||
...(sentAt === undefined ? {} : {
|
||
...(headersAt === undefined ? {} : {responseHeadersMs:headersAt-sentAt}),
|
||
...(firstTextAt === undefined ? {} : {firstTextMs:firstTextAt-sentAt}),
|
||
}) });
|
||
host.inflight = ['complete', 'failed'].includes(settled.phase) ? null : round.id;
|
||
if (settled.failureCode === 'rate_limit_exceeded') throw Error('模型服务暂时限流,这条回复没有完成。稍后可继续对话。');
|
||
if (!valid || settled.phase !== 'complete') throw Error(`请求${round.id}结果待核查;不会重发`);
|
||
if (result.stopReason === 'length') throw Error('模型已达到输出上限,未执行这一轮工具建议;已完成的改动保留');
|
||
const wire = await wireCapture;
|
||
if (!wire || wire.error) throw Error('原始响应未完整取到,未执行工具;请只读核查原请求');
|
||
const usage = responseUsage(wire.text);
|
||
await store.appendEvent({ type: 'usage', id: round.id, model: model.id, ...usage });
|
||
await onEvent({ type: 'usage', id: round.id, model: model.id, ...usage });
|
||
if (!host.compacting) await host.updateContextUsage({ ...usage, id: round.id, model: model.id, at: Date.now(), source: 'provider-response' }, 'running');
|
||
const audit = auditResponse(wire.text, info.model.protocol);
|
||
await store.appendEvent({ type: 'response_audit', id: round.id, ...audit, calls: audit.calls.map(c => ({ name: c.name })) });
|
||
if (!encoded && requiredTool && !audit.calls.some(c => c.name === requiredTool)) throw Error('模型未返回要求的标准函数调用;本轮操作未执行,不会自动改用其他协议');
|
||
if (encoded) {
|
||
const content = decodeToolReply(result.content.filter(c => c.type === 'text').map(c => c.text).join(''), contextual, round.id);
|
||
const message = { ...result, content, stopReason: content.some(c => c.type === 'toolCall') ? 'toolUse' : 'stop' };
|
||
if (!partial) output.push({ type: 'start', partial: message });
|
||
if (reply) output.push({ type: 'text_end', contentIndex: 0, content: reply, partial: message });
|
||
output.push({ type: 'done', reason: message.stopReason, message }); output.end(message);
|
||
} else { output.push(finalEvent); output.end(result); }
|
||
} catch (error) {
|
||
const message = { role: 'assistant', api, provider: 'jiansun-native-98k', model: info.model.id, content: [], stopReason: 'error', errorMessage: String(error.message).slice(0, 2000), usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { ...cost, total: 0 } }, timestamp: Date.now() };
|
||
if (error.localBudgetRecovery === host.localBudgetRecovery && host.localBudgetRecovery) host.localBudgetRecovery.errorHash = localBudgetFailureHash(message);
|
||
await store.appendEvent({ type: 'provider_error', id: round?.id, reason: message.errorMessage });
|
||
output.push({ type: 'error', reason: 'error', error: message }); output.end(message);
|
||
}
|
||
})();
|
||
return output;
|
||
},
|
||
});
|
||
const saved = await store.get('session.json', null);
|
||
host.manager = saved ? SessionManager.open(path.join(store.dir,'sessions',path.basename(saved.file)), store.dir, store.dir) : SessionManager.create(store.dir, path.join(store.dir, 'sessions'));
|
||
// Stale bodies are archived only in transportProjection. Opening/recovering
|
||
// a session must not append context edits that change a checkpoint's branch.
|
||
if (typeof canvas?.uiRequest === 'function') host.nativeUI = new NativeExtensionUI(canvas, { abort: () => { void host.session?.abort(); } });
|
||
const customTools = toolsFor(host);
|
||
const extensions = await createPhotoCraftExtensions(host, [compactionExtension(host)]);
|
||
const loader = {
|
||
getExtensions: () => extensions,
|
||
getSkills: () => ({ skills: [], diagnostics: [] }), getPrompts: () => ({ prompts: [], diagnostics: [] }), getThemes: () => ({ themes: [], diagnostics: [] }),
|
||
getAgentsFiles: () => ({ agentsFiles: [] }), getSystemPrompt: () => SYSTEM, getSystemPromptSource: () => undefined,
|
||
getAppendSystemPrompt: () => [], getAppendSystemPromptSources: () => [], extendResources() {}, async reload() {},
|
||
};
|
||
const { session } = await createAgentSession({ cwd: store.dir, agentDir: store.dir, modelRuntime: runtime,
|
||
model: runtime.getModel('jiansun-native-98k', info.model.id), thinkingLevel: 'off',
|
||
resourceLoader: loader, customTools, tools: photoCraftToolAllowlist(customTools),
|
||
settingsManager: SettingsManager.inMemory({ compaction: { enabled: false, keepRecentTokens: 2048, reserveTokens: 4096 }, retry: { enabled: false, maxRetries: 0 }, summarization: { enabled: false } }), sessionManager: host.manager,
|
||
});
|
||
host.runDetailAgent = async (slotId, systemPrompt, prompt, tools) => {
|
||
const model = runtime.getModel('jiansun-native-98k', info.model.id);
|
||
const scoped = broker?.forDetailSlot?.(slotId);
|
||
if (!model || !scoped && !transport?.stream) throw Error('详情页模型连接未就绪');
|
||
const used = new Map();
|
||
const agent = new Agent({
|
||
initialState:{model,systemPrompt,tools,thinkingLevel:'off'},
|
||
toolExecution:'sequential',
|
||
streamFn:apiImpl.streamSimple,
|
||
});
|
||
agent.beforeToolCall=async ({toolCall})=>{
|
||
const key=JSON.stringify([toolCall.name,toolCall.arguments]);
|
||
const count=(used.get(key)??0)+1;
|
||
used.set(key,count);
|
||
if(count>2)return {block:true,terminate:true,reason:'同一操作已返回结果,请保留当前图位记录'};
|
||
};
|
||
agent.streamFunction=(selected,context,options={})=>{
|
||
if(transport?.stream) return transport.stream(selected,context,options);
|
||
const output=createAssistantMessageEventStream();
|
||
(async()=>{
|
||
let round;
|
||
try {
|
||
const camera=await host.canvas.read();
|
||
if(camera?.conversationActive!==true||camera.writeBlocked)throw Error('用户已暂停详情页制作');
|
||
if(exceedsModelBudget(context,info.model))throw Error('该图位的证据超出模型容量');
|
||
round=scoped.round();
|
||
await store.appendEvent({type:'provider_prepared',id:round.id,model:selected.id,purpose:'whole-detail-worker',slotId});
|
||
let wire;
|
||
const stream=apiImpl.streamSimple(selected,context,{...options,apiKey:'native-broker-only',
|
||
fetch:async (...args)=>{
|
||
const response=await round.fetch(...args);
|
||
wire=response.clone().text().catch(()=>null);
|
||
return response;
|
||
},maxRetries:0,maxRetryDelayMs:0,timeoutMs:180000});
|
||
let finalEvent;
|
||
for await (const event of stream) {
|
||
if(event.type==='done'||event.type==='error')finalEvent=event;
|
||
else output.push(event);
|
||
}
|
||
const message=await stream.result();
|
||
const valid=finalEvent?.type==='done' && ['stop','toolUse'].includes(message.stopReason);
|
||
const receipt=await round.finish(valid,message.stopReason);
|
||
await store.appendEvent({type:'provider_finished',id:round.id,slotId,...receipt});
|
||
if(!valid||receipt.phase!=='complete')throw Error('原模型请求尚未核实,工具未执行');
|
||
const raw=await wire;
|
||
if(!raw)throw Error('原始模型响应尚未读完,工具未执行');
|
||
const audit=auditResponse(raw,info.model.protocol);
|
||
if(message.content.filter(item=>item.type==='toolCall').length!==audit.calls.length)throw Error('原模型工具调用未完整返回');
|
||
await onEvent({type:'usage',id:round.id,model:selected.id,...responseUsage(raw)});
|
||
output.push(finalEvent);output.end(message);
|
||
} catch(error){
|
||
if(round && scoped.hasUnconfirmedRequest){
|
||
const original=await scoped.read(round.id).catch(()=>null);
|
||
if(original?.requestId){
|
||
const recovered=await scoped.recover(round.id).catch(()=>null);
|
||
if(recovered?.phase==='received'){
|
||
const stored=await scoped.read(round.id).catch(()=>null);
|
||
if(stored){
|
||
const record={id:round.id,...stored};
|
||
const saved=await parseReceipt(record,selected,'native-tools',[]).catch(()=>null);
|
||
if(saved&&['stop','toolUse'].includes(saved.stopReason))await scoped.confirm(round.id,record.bodyHash,true,saved.stopReason).catch(()=>null);
|
||
}
|
||
}
|
||
await store.appendEvent({type:'whole_detail_original_checked',id:round.id,slotId,phase:recovered?.phase??'unknown'}).catch(()=>{});
|
||
}
|
||
}
|
||
const failure={role:'assistant',api,provider:selected.provider,model:selected.id,content:[],stopReason:'error',
|
||
errorMessage:error.message,timestamp:Date.now(),usage:{input:0,output:0,cacheRead:0,cacheWrite:0,totalTokens:0,cost:{...cost,total:0}}};
|
||
output.push({type:'error',reason:'error',error:failure});output.end(failure);
|
||
}
|
||
})();
|
||
return output;
|
||
};
|
||
try {
|
||
await agent.prompt(prompt);
|
||
if(agent.state.errorMessage)throw Error(agent.state.errorMessage);
|
||
const result=agent.state.messages.filter(message=>message.role==='assistant').at(-1);
|
||
if(result?.stopReason==='error'||result?.stopReason==='aborted')throw Error(result.errorMessage||'当前制作步骤未完成');
|
||
} finally { if(scoped)await scoped.close(); }
|
||
};
|
||
host.session = session;
|
||
if (host.nativeUI) await session.bindExtensions({ uiContext:host.nativeUI.context, mode:'rpc', abortHandler:() => { void session.abort(); } });
|
||
activatePhotoCraftTools(session, customTools);
|
||
session.agent.toolExecution = 'sequential';
|
||
session.setAutoRetryEnabled(false);
|
||
// AgentSession UI listeners are fire-and-forget. Core listeners are awaited:
|
||
// the native UI, durable activity journal and handover gates must settle first.
|
||
session.agent.subscribe(async event => {
|
||
if (event.type === 'message_update' && !host.compacting) {
|
||
const update = event.assistantMessageEvent;
|
||
if (update?.type === 'text_delta' || update?.type === 'thinking_delta') {
|
||
host.presentation ??= new StreamPresentation();
|
||
const parts = update.type === 'text_delta' ? host.presentation.push(update.delta) : { text: '', thought: host.presentation.thinking(update.delta) };
|
||
if (canvas && (!host.templateOriginal || host.templateImageRead) && (parts.text || parts.thought && Date.now() - (host.lastDelta ?? 0) > 45)) {
|
||
host.lastDelta = Date.now();
|
||
await host.notifyCanvas('delta', host.presentation.text.slice(-65000), host.presentation.thought.slice(-65000));
|
||
}
|
||
if (parts.text) await onEvent({ type: 'text_delta', delta: parts.text });
|
||
if (parts.thought) await onEvent({ type: 'thinking_delta', delta: parts.thought });
|
||
} else if (update?.type === 'text_end') host.presentation?.endText();
|
||
}
|
||
if (event.type === 'tool_execution_start' || event.type === 'tool_execution_end') {
|
||
const activity = toolActivity(event, host.activityScope || activityScope(store.identity));
|
||
if (canvas) await host.notifyCanvas('activity', activitySummary(event), activity);
|
||
await store.appendEvent({ type: event.type, toolName: event.toolName, toolCallId: event.toolCallId, isError: event.isError ?? false });
|
||
await onEvent({ type: event.type, toolName: event.toolName, isError: event.isError ?? false, ...(activity ? { activity } : {}) });
|
||
}
|
||
});
|
||
// Restore the meter from Pi's durable session, not a cached local estimate.
|
||
// This only reads the session and publishes telemetry; it never prompts.
|
||
await host.updateContextUsage(null, 'ready');
|
||
return host;
|
||
} catch (error) { host.releaseLock(); host.releaseLock = null; throw error; }
|
||
}
|
||
async exclusive(operation) {
|
||
if (this.disposed || this.operationActive) throw Error('此会话已有操作或已关闭,未发起新请求');
|
||
this.operationActive = true;
|
||
try { return await operation(); } finally { this.operationActive = false; }
|
||
}
|
||
async readWorkflow(args) {
|
||
const epoch = this.turnEpoch;
|
||
if (this.canvasNotificationsDetached) throw Error('当前项目已切换,未读取其他项目工作流');
|
||
if (this.workflowBindingChanged) throw Error('品牌或品类已变化,请重新开始一轮对话以加载对应规则;未继续执行当前回合');
|
||
const latest = workflowContext(await this.canvas.workflowRead(args), this.productIdentity);
|
||
if (this.turnEpoch !== epoch) throw Error('原回合已结束,旧工作流读取不更新当前回合');
|
||
if (latest.workflow.available) {
|
||
const identity = { brandId: latest.product.product.brandId, categoryId: latest.product.product.categoryId };
|
||
if (this.productRuleIdentity && (identity.brandId !== this.productRuleIdentity.brandId || identity.categoryId !== this.productRuleIdentity.categoryId)) {
|
||
this.workflowBindingChanged = true;
|
||
throw Error('品牌或品类已变化,请重新开始一轮对话以加载对应规则;未记录事实或修改画布');
|
||
}
|
||
this.productRuleIdentity ??= identity;
|
||
}
|
||
this.currentWorkflow = latest.workflow;
|
||
this.productEvidence = latest.product;
|
||
if (this.task && this.task !== 'chat' && !this.compacting && !this.wholeDetailActive) this.knowledgeContext = await this.readEnterpriseRules();
|
||
return latest.workflow;
|
||
}
|
||
async readEnterpriseRules() {
|
||
const template = this.currentWorkflow?.template;
|
||
if (!this.currentWorkflow?.available || !template?.enterprise) return null;
|
||
const product = this.productEvidence.product;
|
||
this.enterpriseContext ??= new EnterpriseContext(this.store);
|
||
return this.enterpriseContext.load({task:this.task || 'chat',
|
||
brandName:product.brandName,categoryName:product.categoryName,
|
||
templateId:template.id,releaseId:template.version,turn:this.turnEpoch});
|
||
}
|
||
async modelContext(options = {}, {loadKnowledge = false} = {}) {
|
||
const value=await this.store.modelContext({task:this.task || 'chat',workflow:this.currentWorkflow,canvas:this.currentCanvas,...options});
|
||
const knowledge = this.knowledgeContext ?? (loadKnowledge ? await this.readEnterpriseRules() : null);
|
||
if (knowledge) {
|
||
value.enterpriseKnowledge=knowledge;
|
||
value.snapshot.rules=value.snapshot.rules.filter(rule=>/原稿画布|源模板约束/.test(rule));
|
||
}
|
||
return value;
|
||
}
|
||
async notifyCanvas(method,...args) {
|
||
const epoch = this.turnEpoch;
|
||
if(!this.canvas)return;
|
||
if(method==='end'){
|
||
if(this.canvasEndAttempted)return;
|
||
this.canvasEndAttempted=true;
|
||
}else if(this.canvasNotificationsDetached)return;
|
||
try{return await this.canvas[method](...args);}
|
||
catch(error){
|
||
if(!inactiveDocument(error))throw error;
|
||
if(this.turnEpoch !== epoch)return;
|
||
this.canvasNotificationsDetached=true;
|
||
await this.store.appendEvent({type:'inactive_ui_delivery',method,reason:error.message,originalResponseRetained:true});
|
||
}
|
||
}
|
||
async prompt(text, options = {}) {
|
||
return this.exclusive(async () => {
|
||
try { return await this.promptTurn(text, options); }
|
||
catch (error) {
|
||
if (this.localBudgetRecovery) this.localBudgetRecovery.phase = 'failed';
|
||
throw error;
|
||
}
|
||
finally { this.session.setAutoCompactionEnabled(false); }
|
||
});
|
||
}
|
||
generatedCandidateReply() {
|
||
if (!this.generatedCandidates.size) return null;
|
||
const adopted = [...this.generatedCandidates.values()].filter(Boolean).length;
|
||
let reply = `已生成 ${this.generatedCandidates.size} 张候选图,可以在画布对应图位或对话中的「生成图片」查看。`;
|
||
if (adopted) reply += `其中 ${adopted} 张已采纳到画布。`;
|
||
if (this.workflowStopReason) reply += `\n\n后续操作已停止。${this.workflowStopReason}`;
|
||
return reply;
|
||
}
|
||
async checkLocalBudgetRecovery(signal) {
|
||
const recovery = this.localBudgetRecovery;
|
||
if (signal?.aborted || recovery?.signal?.aborted || this.disposed) throw Error('预算压缩已停止;原工具结果与历史已保留');
|
||
if (this.compromised) throw Error('本地会话锁失效,已停止预算压缩');
|
||
if (this.canvasNotificationsDetached || this.canvasWritesStopped || this.workflowBindingChanged) throw Error('原项目状态已变化,预算压缩未继续;原工具结果已保留');
|
||
const latest = this.broker ? await this.broker.info() : this.info;
|
||
if (this.inflight || latest.pending?.length) throw Error('本项目仍有未知请求,预算压缩未继续;请先核查原请求');
|
||
if (latest.accountScope !== this.store.identity.scope || latest.model.id !== this.info.model.id || latest.model.protocol !== this.info.model.protocol) throw Error('账户或模型已变化,预算压缩未继续;原记录已保留');
|
||
if ((await this.store.get('recovered-tools.json', [])).some(tool => tool.phase === 'review')) throw Error('原工具建议尚待核对,预算压缩未继续');
|
||
if (await hasCompactionReviewForBranch(this, recovery?.branchHash)) throw Error('原分支摘要尚待核对,不能重复摘要;原记录已保留');
|
||
if (this.canvas) {
|
||
const state = await this.canvas.read();
|
||
if (state.conversationActive === false || state.writeBlocked || (this.productIdentity.documentId && state.context?.id !== this.productIdentity.documentId)) throw Error('原项目已切换或接手,预算压缩未继续;原记录已保留');
|
||
}
|
||
if (signal?.aborted || recovery?.signal?.aborted || this.disposed) throw Error('预算压缩已停止;原工具结果与历史已保留');
|
||
}
|
||
async localBudgetOverflow(reason, inputChars, signal) {
|
||
const failure = Error(`${reason},本轮未完成;未发起该次模型请求,原工具结果与历史已保留`);
|
||
// Summary requests and a second overflow never arm another recovery.
|
||
if (this.compacting || this.localBudgetRecovery) return failure;
|
||
await this.checkLocalBudgetRecovery(signal);
|
||
const branch = this.manager.getBranch();
|
||
const recovery = {phase: 'armed', branchHash: digest(branch), branchLength: branch.length, leafId: this.manager.getLeafId(), signal, inputChars, reason};
|
||
this.localBudgetRecovery = recovery;
|
||
await this.store.appendEvent({type: 'local_budget_recovery_armed', inputChars, originalBranchHash: recovery.branchHash, originalLeafId: recovery.leafId, modelCallSkipped: true});
|
||
signal?.throwIfAborted();
|
||
this.session.setAutoCompactionEnabled(true);
|
||
const overflow = Error(`${LOCAL_CONTEXT_OVERFLOW}: ${reason}(${inputChars} chars);本地派发前拦截,未发起该次模型请求`);
|
||
overflow.localBudgetRecovery = recovery;
|
||
return overflow;
|
||
}
|
||
projectModelContext(context, injected) {
|
||
return transportProjection(normalizeContext({messages: [...context.messages, {role: 'system', content: '当前项目规则快照与已确认事实(每次调用重新注入,以此为准):\n' + JSON.stringify(injected), timestamp: 0}]}),
|
||
{bindingHash: this.store.hash, version: this.currentWorkflow?.version, templateOriginal: this.templateOriginal});
|
||
}
|
||
async projectedPromptChars(text) {
|
||
return contextTextLength(await this.projectedContext(text));
|
||
}
|
||
async projectedContext(text, images = []) {
|
||
const injected = await this.modelContext();
|
||
injected.currentProduct = this.productEvidence;
|
||
if (this.currentWorkflow) injected.currentWorkflow = this.currentWorkflow;
|
||
const context = normalizeContext({systemPrompt: this.session.agent.state.systemPrompt, tools: this.session.agent.state.tools,
|
||
messages: [...this.session.messages, ...(text ? [{role: 'user', content: images.length ? [{ type: 'text', text }, ...images] : text, timestamp: 0}] : [])]});
|
||
const projected = this.projectModelContext(context, injected);
|
||
return this.protocolMode === 'structured-json' ? jsonToolContext(projected) : projected;
|
||
}
|
||
async updateContextUsage(receipt = null, phase = 'ready') {
|
||
this.contextUsage = contextSnapshot(this.store.identity, this.info.model, this.session, this.contextUsage, receipt, phase);
|
||
await atomic(path.join(this.store.root, 'context-usage.json'), this.contextUsage);
|
||
await this.onEvent({ type: 'context_usage', value: this.contextUsage });
|
||
if (this.canvas?.contextUsage && this.canvas.run && !this.canvasEndAttempted) await this.notifyCanvas('contextUsage', this.contextUsage);
|
||
return this.contextUsage;
|
||
}
|
||
async promptTurn(text, { requiredTool, referenceContent = [] } = {}) {
|
||
if (this.needsReopen) throw Error('原响应已恢复,请重新打开此会话;未发起新请求');
|
||
if ((await this.store.get('recovered-tools.json', [])).some(t => t.phase === 'review')) throw Error('原请求的工具建议已取回,需先核对画布;不会重跑工具或模型');
|
||
if (typeof text !== 'string' || !text.trim() || text.length > 6000) throw Error('请输入6000字以内的要求');
|
||
const latest = this.firstTurnBrokerInfo ?? (this.broker ? await this.broker.info() : this.info);
|
||
this.firstTurnBrokerInfo = null;
|
||
if (latest.pending?.length) throw Error('本项目仍有未知请求,先核查原请求;不会重发');
|
||
if (latest.accountScope !== this.store.identity.scope) throw Error('账户与项目不一致;原草稿已保留');
|
||
if (latest.model.id !== this.info.model.id || latest.model.protocol !== this.info.model.protocol) throw Error('用户已更换模型,请重新打开会话后再调用');
|
||
if (this.session.isStreaming) throw Error('本轮尚未结束');
|
||
if (this.nativeToolActive) throw Error('原操作仍在执行,请先等待或核查;不会重发');
|
||
if (requiredTool && !this.session.getActiveToolNames().includes(requiredTool)) throw Error('指定工具未开放');
|
||
this.requiredTool = null;
|
||
this.imageProgressTurn = !requiredTool && !referenceContent.length && typeof this.canvas?.workflowRead === 'function' && imageProgressQuestion(text);
|
||
this.task = this.imageProgressTurn ? 'chat' : taskFor(text);
|
||
this.templateOriginal = /模板.{0,12}(?:原图|预置|原稿)|原稿.{0,8}(?:图|视角)/u.test(text)
|
||
&& /看|视角|构图|是什么|读|描述|确认|核对/u.test(text);
|
||
this.templateImageRead = false;
|
||
const copyPreviewRequested = ['copy','canvas'].includes(this.task) && text.includes('预览') && !/(?:不要|不用|无需|不需要|不做|不生成)预览/.test(text);
|
||
this.previewReceipt = null; this.previewCancelled=false; this.expectCopyPreview = false; this.copyPreviewRevision = null; this.copyPreviewTarget = null;
|
||
this.expectCopyFields=false;this.copyFieldsReceipt=null;this.copyFieldsTargets=[];this.copyFieldsRead=new Set();this.copyFieldsRevision=null;
|
||
this.copyOptionsReceipt = null;
|
||
this.expectCopyOptions = this.task === 'copy' && /卖点/.test(text) && /生成|写|给|提供/.test(text) && !text.includes('预览') && !!this.session.getToolDefinition('copywriting_options');
|
||
if (this.expectCopyOptions) this.session.setActiveToolsByName([...new Set([...this.session.getActiveToolNames(),'copywriting_options'])]);
|
||
if (text.includes('预览') && this.session.getToolDefinition('canvas_preview')) {
|
||
this.session.setActiveToolsByName([...new Set([...this.session.getActiveToolNames(), 'canvas_preview'])]);
|
||
}
|
||
this.rounds = 0; this.text = ''; this.presentation = new StreamPresentation(); this.lastDelta = 0; this.recallCounts.clear(); this.recallRepeats = 0;
|
||
this.turnEpoch = (this.turnEpoch || 0) + 1;
|
||
this.canvasWriteRequested = false; this.canvasWritesStopped = false; this.workflowStopReason = null;
|
||
this.generatedCandidates = new Map();
|
||
this.canvasNotificationsDetached=false;this.canvasEndAttempted=false;
|
||
this.activityScope = activityScope(this.store.identity);
|
||
this.localBudgetRecovery = null;
|
||
this.compactionFailure = null;
|
||
this.knowledgeContext = null;
|
||
const images = [], quoted = [];
|
||
for (const item of referenceContent ?? []) {
|
||
let image;
|
||
if (typeof item.image === 'string' && item.image) {
|
||
images.push({ type: 'image', data: item.image, mimeType: 'image/jpeg' });
|
||
image = images.length;
|
||
}
|
||
const source = item.kind === 'candidate' ? '候选图' : item.kind === 'layer' ? '发送时的画布图层' : '本轮引用';
|
||
quoted.push({ label: item.label, source, ...(image ? { image } : {}), ...(typeof item.text === 'string' ? { text: item.text } : {}) });
|
||
}
|
||
const prompt = quoted.length ? text + '\n\n本条消息附带的画布引用(按图片顺序对应,以下文字为引用内容):' + JSON.stringify(quoted) : text;
|
||
const projection = this.imageProgressTurn ? null : await this.projectedContext(prompt, images);
|
||
const projectedChars = projection ? contextTextLength(projection) : 0;
|
||
await this.store.appendEvent({type: 'automatic_compaction_budget', projectedChars, originalChars: JSON.stringify(this.session.messages).length});
|
||
if (projection && (projectedChars > 80000 || exceedsModelBudget(projection, this.info.model, 0.8))) {
|
||
if (await hasCompactionReviewForBranch(this)) {
|
||
if (projectedChars > 120000 || exceedsModelBudget(projection, this.info.model)) throw Error('原分支摘要尚未应用,本轮上下文超过本地输入预算;原记录保留,请先人工核对摘要或新建会话。未发起模型请求');
|
||
// The explicit new user turn has not reached SDK yet. Summarizing now
|
||
// would repeat the unchanged reviewed branch, while rejecting it would
|
||
// prevent any new branch from ever being created. Continue once under
|
||
// the provider's independently enforced hard budget; never replay the
|
||
// old summary or remove its review/checkpoint evidence.
|
||
await this.store.appendEvent({type: 'automatic_compaction_deferred', reason: 'unapplied_summary_same_branch', projectedChars});
|
||
} else {
|
||
await this.store.appendEvent({ type: 'automatic_compaction_started', reason: 'bounded-input-budget', projectedChars });
|
||
await this.compactTurn({ preserveBudget: true });
|
||
await this.store.appendEvent({ type: 'automatic_compaction_completed' });
|
||
}
|
||
}
|
||
this.requiredTool = requiredTool;
|
||
if (this.canvas) {
|
||
const begun = await this.canvas.begin(text);
|
||
this.nativeUI?.start(begun?.editorText || '');
|
||
const references = frozenReferences(begun?.context);
|
||
this.expectCopyPreview = copyPreviewRequested && references?.length === 1 && references[0].layerKind === 'Type';
|
||
if (this.expectCopyPreview) {this.copyPreviewTarget=references[0].nativeLayerId;this.requiredTool ??= 'canvas_read';}
|
||
this.expectCopyFields=copyPreviewRequested&&references?.length>1&&references.length<=20&&references.every(ref=>ref.layerKind==='Type'&&ref.component==='layer');
|
||
if(this.expectCopyFields){if(!this.session.getToolDefinition('copywriting_fields'))throw Error('请更新完整客户端以使用逐格文案');this.copyFieldsTargets=references.map(ref=>ref.nativeLayerId);this.session.setActiveToolsByName([...new Set([...this.session.getActiveToolNames(),'copywriting_fields'])]);this.requiredTool ??='canvas_read';}
|
||
if (this.expectCopyOptions) this.requiredTool ??= 'copywriting_options';
|
||
this.activityScope = activityScope(this.store.identity, this.canvas, begun);
|
||
this.heartbeat = setInterval(() => { this.canvas.read().then(state => { if(this.nativeUI && typeof state?.editorText==='string')this.nativeUI.editorText=state.editorText; }).catch(() => {}); }, 2000);
|
||
this.heartbeat.unref();
|
||
}
|
||
try {
|
||
if (typeof this.canvas?.workflowRead === 'function') await this.readWorkflow({ section: 'overview' });
|
||
await this.store.recordUserEvidence(text);
|
||
const running = this.session.prompt(prompt, images.length ? { images } : undefined);
|
||
// SDK flushes the user entry before dispatch. Persist the authoritative file location.
|
||
if (this.manager.getSessionFile()) await atomic(path.join(this.store.dir, 'session.json'), { file: this.manager.getSessionFile() });
|
||
await running;
|
||
await atomic(path.join(this.store.dir, 'session.json'), { file: this.manager.getSessionFile() });
|
||
if (this.localBudgetRecovery && this.localBudgetRecovery.phase !== 'committed') throw this.compactionFailure || Error('本轮预算压缩未完成或已停止;未继续模型请求,原工具结果与历史已保留');
|
||
const last = this.session.messages.filter(m => m.role === 'assistant').at(-1);
|
||
if (last?.stopReason === 'error' || last?.stopReason === 'aborted') throw Error(last.errorMessage || '本轮未完成;保留原请求');
|
||
if (this.localBudgetRecovery && last?.stopReason !== 'stop') throw Error('本轮预算压缩后尚未得到完整回复;原工具结果与历史已保留');
|
||
if (this.localBudgetRecovery) {
|
||
this.localBudgetRecovery.phase = 'completed';
|
||
await this.store.appendEvent({type: 'local_budget_recovery_completed', planId: this.localBudgetRecovery.planId});
|
||
}
|
||
const textResult = this.generatedCandidateReply() ?? (this.templateOriginal && !this.templateImageRead
|
||
? '模板原图尚未读取,暂时无法确认视角。可以引用画布里的具体图位,再继续核对。'
|
||
: this.previewCancelled&&!this.previewReceipt
|
||
? '已取消本次预览,没有应用这项修改。' : presentText(this.session.getLastAssistantText() || ''));
|
||
if (!textResult.trim()) throw Error('模型没有返回正文,请更换模型后再试;本轮工具结果已保留');
|
||
if (this.expectCopyPreview && !this.previewReceipt && !this.previewCancelled) throw Error('本轮尚未生成可采纳的修改预览;文案候选和工具结果已保留');
|
||
if(this.expectCopyFields&&!this.copyFieldsReceipt)throw Error('本轮尚未生成与文字格对应的候选文案;原文和画布未改变');
|
||
if (this.expectCopyOptions && !this.copyOptionsReceipt) throw Error('本轮尚未生成可选择的卖点;文案与工具结果已保留');
|
||
if (this.canvas?.delta && this.presentation && (!this.templateOriginal || this.templateImageRead)) {
|
||
await this.notifyCanvas('delta', this.presentation.text.slice(-65000), this.presentation.thought.slice(-65000));
|
||
}
|
||
await this.updateContextUsage(null, 'ready');
|
||
await this.nativeUI?.stop();
|
||
if (this.canvas) await this.notifyCanvas('end',textResult);
|
||
return { text: textResult, rounds: this.rounds, messages: this.session.messages.length, sessionFile: this.manager.getSessionFile() };
|
||
} catch (error) {
|
||
await this.nativeUI?.stop().catch(() => {});
|
||
const generated = this.generatedCandidateReply();
|
||
if (this.canvas) await this.notifyCanvas('end', generated && !this.workflowStopReason
|
||
? `${generated}\n\n${error.message}` : generated ?? error.message).catch(() => {});
|
||
throw error;
|
||
} finally {
|
||
if (this.heartbeat) clearInterval(this.heartbeat);
|
||
}
|
||
}
|
||
async wholeDetail(text) {
|
||
return this.exclusive(async () => {
|
||
if (!this.canvas?.detailImages || this.info.model.supportsImageInput===false) {
|
||
throw Error('整套详情页需要可识图的对话模型及完整的小壹客户端');
|
||
}
|
||
if ((await this.broker?.info())?.pending?.length) throw Error('有原模型请求待核查,处理后可继续');
|
||
const begun=await this.canvas.begin(text);
|
||
this.activityScope=activityScope(this.store.identity,this.canvas,begun);
|
||
const heartbeat=setInterval(()=>{this.canvas.read().catch(()=>{});},2000);
|
||
heartbeat.unref();
|
||
this.wholeDetailActive=true;
|
||
try {
|
||
const result=await runWholeDetail(this,this.runDetailAgent,async plan=>{
|
||
if(!plan.template.enterprise)return null;
|
||
this.enterpriseContext??=new EnterpriseContext(this.store);
|
||
const brief=plan.productEvidence?.brief;
|
||
return this.enterpriseContext.load({task:'image',brandName:brief?.brandName,categoryName:brief?.categoryName,
|
||
templateId:plan.template.id,releaseId:plan.template.version,resourceSetId:plan.template.resourceSetId,turn:this.canvas.run},
|
||
{read:async read=>{
|
||
for(;;){
|
||
const state=await this.canvas.read();
|
||
if(state.conversationActive===false||state.writeBlocked)throw Error('资料读取已取消,已完成图位保留');
|
||
try{return await read();}
|
||
catch(error){
|
||
if(error.status!==429||!(error.retryAfterSeconds>0))throw error;
|
||
const seconds=Math.ceil(error.retryAfterSeconds);
|
||
await this.canvas.delta('知识库访问限流,约 '+seconds+' 秒后继续');
|
||
const until=Date.now()+seconds*1000;
|
||
while(Date.now()<until){
|
||
await delay(Math.min(1000,until-Date.now()));
|
||
const latest=await this.canvas.read();
|
||
if(latest.conversationActive===false||latest.writeBlocked)throw Error('资料读取已取消,已完成图位保留');
|
||
}
|
||
}
|
||
}
|
||
}});
|
||
});
|
||
await this.canvas.end(result.text);
|
||
return result;
|
||
} catch(error) {
|
||
await this.canvas.end(error.message).catch(()=>{});
|
||
throw error;
|
||
} finally {
|
||
this.wholeDetailActive=false;
|
||
clearInterval(heartbeat);
|
||
}
|
||
});
|
||
}
|
||
|
||
async compact() { return this.exclusive(() => this.compactTurn()); }
|
||
async compactTurn({ preserveBudget = false } = {}) {
|
||
const pending = this.broker ? (await this.broker.info()).pending : [];
|
||
if (pending?.length) throw Error('有未知请求,不能启动压缩调用');
|
||
if ((await this.store.get('recovered-tools.json', [])).some(t => t.phase === 'review')) throw Error('原工具建议尚待核对,不能启动新模型调用');
|
||
if (await hasCompactionReviewForBranch(this)) throw Error('当前分支的原内部摘要尚未应用,不能重复摘要;原会话保留');
|
||
if (!preserveBudget) this.rounds = 0;
|
||
this.compacting = true;
|
||
this.compactionFailure = null;
|
||
try {
|
||
const result = await this.session.compact(COMPACTION_INSTRUCTIONS);
|
||
await atomic(path.join(this.store.dir, 'session.json'), { file: this.manager.getSessionFile() });
|
||
return result;
|
||
} catch (error) {
|
||
throw this.compactionFailure || error;
|
||
} finally { this.compacting = false; this.activeCompaction = null; }
|
||
}
|
||
async recover(id, options = {}) { return this.exclusive(() => this.recoverTurn(id, options)); }
|
||
async recoverTurn(id, { executeReadOnly = false } = {}) {
|
||
if (this.session.isStreaming) throw Error('先等待当前操作结束');
|
||
if (!await this.store.ownsRequest(id)) throw Error('原请求不属于当前店铺/品类会话,未查询或恢复');
|
||
const purpose = await requestPurpose(this, id);
|
||
const internalCompaction = ['compaction','legacy-compaction'].includes(purpose.kind);
|
||
let record = { id, ...await this.broker.read(id) };
|
||
if (record.phase === 'unknown' || record.phase === 'dispatched') {
|
||
try { await this.broker.recover(id); }
|
||
catch (error) {
|
||
await this.store.appendEvent({ type: 'recovery_unknown', id, requestId: record.requestId, reason: error.message });
|
||
return { phase: 'unknown', requestId: record.requestId, reason: error.message, reposted: false };
|
||
}
|
||
record = { id, ...await this.broker.read(id) };
|
||
}
|
||
if (purpose.kind === 'whole-detail-worker') {
|
||
if(['failed','rejected'].includes(record.phase))return {phase:'failed',requestId:record.requestId,reposted:false};
|
||
if(!['complete','received'].includes(record.phase))return {phase:'unknown',requestId:record.requestId,reposted:false};
|
||
const original=await parseReceipt(record,this.session.model,'native-tools',[]);
|
||
if(!['stop','toolUse'].includes(original.stopReason))return {phase:'unknown',requestId:record.requestId,reposted:false};
|
||
if(record.phase==='received')await this.broker.confirm(id,record.bodyHash,true,original.stopReason);
|
||
return {phase:'complete',requestId:record.requestId,reposted:false,canvasWrites:0};
|
||
}
|
||
if (purpose.kind === 'whole-detail-review') {
|
||
if (['failed','rejected'].includes(record.phase)) return {phase:'failed',requestId:record.requestId,reposted:false,canvasWrites:0};
|
||
if (!['complete','received'].includes(record.phase)) return {phase:'unknown',requestId:record.requestId,reposted:false,canvasWrites:0};
|
||
const original=await parseReceipt(record,this.session.model,'native-tools',[]);
|
||
if (original.stopReason!=='stop' || original.content.some(part=>part.type==='toolCall')) {
|
||
return {phase:'unknown',requestId:record.requestId,reposted:false,canvasWrites:0};
|
||
}
|
||
if (record.phase==='received') await this.broker.confirm(id,record.bodyHash,true,original.stopReason);
|
||
return {phase:'complete',requestId:record.requestId,reposted:false,canvasWrites:0,
|
||
restoredText:'已核查原验图结果。继续制作整套时,小壹会从原图位接着处理。'};
|
||
}
|
||
if (['unavailable', 'failed', 'rejected'].includes(record.phase) && internalCompaction) return recoverCompactionWithoutResult(this, id, record, purpose);
|
||
if (record.phase === 'failed') return { phase: 'failed', requestId: record.requestId, reposted: false };
|
||
if (record.phase === 'unavailable') return {phase: 'unknown', requestId: record.requestId, reposted: false,
|
||
needsManualReview: true, reason: '原对话结果不可用,保留待核查,未自动放弃或重发', toolsExecuted: 0, canvasWrites: 0};
|
||
// New summaries have an explicit persisted no-tools wire mode. Legacy
|
||
// summaries retain the original host mode; prose is never guessed to be a
|
||
// valid structured-json envelope and recovered calls are never executed.
|
||
const originalMode = internalCompaction && purpose.event.summaryProtocolMode === 'native-tools' ? 'native-tools' : this.protocolMode;
|
||
const message = await parseReceipt(record, this.session.model, originalMode, internalCompaction ? [] : this.session.agent.state.tools);
|
||
if (internalCompaction && (message.stopReason !== 'stop' || message.content.some(content => content.type === 'toolCall'))) {
|
||
return {phase: 'unknown', requestId: record.requestId, internalCompaction: true, reason: '原摘要不完整或包含工具,未应用压缩或执行工具', reposted: false};
|
||
}
|
||
if (record.phase === 'received') await this.broker.confirm(id, record.bodyHash, true, message.stopReason);
|
||
if (internalCompaction) return recoverCompaction(this, id, record, message, purpose);
|
||
if (message.stopReason === 'length') return { phase: 'complete', usable: false, reason: 'output_limit', toolsExecuted: 0, reposted: false };
|
||
const entries = this.manager.getEntries(), calls = message.content.filter(c => c.type === 'toolCall');
|
||
const recorded = message.responseId && entries.some(e => e.type === 'message' && e.message.role === 'assistant' && e.message.responseId === message.responseId);
|
||
const completed = calls.every(call => entries.some(e => e.type === 'message' && e.message.role === 'toolResult' && e.message.toolCallId === call.id));
|
||
if (recorded && completed) return { phase: 'complete', requestId: record.requestId, alreadyApplied: true, reposted: false };
|
||
if (calls.length && executeReadOnly && calls.every(call => ['context_read', 'memory_search', 'canvas_read', 'workflow_read'].includes(call.name))) {
|
||
const last = this.manager.getEntries().filter(e => e.type === 'message').at(-1);
|
||
if (!recorded && last?.message.role === 'assistant' && ['error', 'aborted'].includes(last.message.stopReason)) this.manager.appendContextEdit(last.id, null);
|
||
if (!recorded) this.manager.appendMessage(message);
|
||
if (this.canvas) await this.canvas.begin('取回原请求,恢复只读步骤');
|
||
let recovered = 0;
|
||
try {
|
||
for (const call of calls) {
|
||
if (entries.some(e => e.type === 'message' && e.message.role === 'toolResult' && e.message.toolCallId === call.id)) continue;
|
||
const outcome = await runToolCall(call, { tools: this.session.agent.state.tools, assistantMessage: message, context: { messages: this.session.messages, tools: this.session.agent.state.tools }, beforeToolCall: this.session.agent.beforeToolCall, afterToolCall: this.session.agent.afterToolCall });
|
||
this.manager.appendMessage({ role: 'toolResult', toolCallId: call.id, toolName: call.name, content: outcome.result.content, isError: outcome.isError, timestamp: Date.now() });
|
||
recovered++;
|
||
}
|
||
} finally { if (this.canvas) await this.canvas.end('原请求已取回,未重发;画布未被修改。'); }
|
||
const pending = await this.store.get('recovered-tools.json', []);
|
||
await atomic(path.join(this.store.dir, 'recovered-tools.json'), pending.map(t => t.id === id ? { ...t, phase: 'read-only-complete' } : t));
|
||
await atomic(path.join(this.store.dir, 'session.json'), { file: this.manager.getSessionFile() });
|
||
this.needsReopen = true;
|
||
return { phase: 'complete', requestId: record.requestId, reposted: false, readOnlyToolsRecovered: recovered, canvasWrites: 0, reopenRequired: true };
|
||
}
|
||
if (calls.length) {
|
||
const pending = await this.store.get('recovered-tools.json', []);
|
||
if (!pending.some(t => t.id === id)) pending.push({ id, requestId: record.requestId, phase: 'review', message });
|
||
await atomic(path.join(this.store.dir, 'recovered-tools.json'), pending);
|
||
return { phase: 'complete', needsToolReview: true, requestId: record.requestId, reposted: false, toolsExecuted: 0 };
|
||
}
|
||
const last = this.manager.getEntries().filter(e => e.type === 'message').at(-1);
|
||
if (last?.message.role === 'assistant' && ['error', 'aborted'].includes(last.message.stopReason)) this.manager.appendContextEdit(last.id, null);
|
||
this.manager.appendMessage(message);
|
||
this.manager.appendCustomEntry('pi-recovered-receipt', { id, requestId: record.requestId, restoredAt: Date.now() });
|
||
await atomic(path.join(this.store.dir, 'session.json'), { file: this.manager.getSessionFile() });
|
||
this.needsReopen = true;
|
||
return { phase: 'complete', requestId: record.requestId, reposted: false, reopenRequired: true, toolsExecuted: 0,
|
||
restoredText: presentText(message.content.filter(c => c.type === 'text').map(c => c.text).join('\n')) };
|
||
}
|
||
async dispose() {
|
||
this.disposed = true;
|
||
if (this.nativeUI) this.nativeUI.active = false;
|
||
if (this.heartbeat) clearInterval(this.heartbeat);
|
||
await this.session.abort(); this.session.dispose();
|
||
this.releaseLock?.(); this.releaseLock = null;
|
||
}
|
||
}
|