From a068bc7edd18a5884ba5005bf8718c55abf36653 Mon Sep 17 00:00:00 2001 From: WuYongJie <549727955@qq.com> Date: Tue, 17 Nov 2026 14:47:20 +0800 Subject: [PATCH] fix(client): streamline chat and load authorized model catalog --- apps/detail-agent-pi/src/agent.mjs | 72 ++++++++---- apps/detail-agent-pi/src/context-budget.mjs | 4 +- apps/detail-agent-pi/src/desktop-host.mjs | 2 +- apps/detail-agent-pi/src/workflow-context.mjs | 34 ++++++ .../test/conversation.test.mjs | 99 +++++++++++++++- .../apps/photocraft/src/jiansun_backend.rs | 107 +++++++++++++----- .../apps/photocraft/src/jiansun_creation.rs | 22 +--- .../apps/photocraft/src/jiansun_models.rs | 48 ++++++++ .../apps/photocraft/src/jiansun_pi.rs | 9 +- 9 files changed, 314 insertions(+), 83 deletions(-) diff --git a/apps/detail-agent-pi/src/agent.mjs b/apps/detail-agent-pi/src/agent.mjs index e4497e85..0fde1308 100644 --- a/apps/detail-agent-pi/src/agent.mjs +++ b/apps/detail-agent-pi/src/agent.mjs @@ -16,7 +16,7 @@ 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 { workflowContext, workflowResult } from './workflow-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'; @@ -104,7 +104,7 @@ function toolsFor(host) { 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)), + 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 => { @@ -273,18 +273,19 @@ function toolsFor(host) { /** Coding Agent SDK owns the transcript/compaction; our host owns all I/O boundaries. */ export class DetailPiAgent { - static async open({ store, broker, canvas, transport, projectEvidence, documentId, protocolMode = 'native-tools', callBudget = 12, onEvent = () => {} }) { + 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 (!Number.isInteger(callBudget) || callBudget < 1 || callBudget > 12) 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 ?? await broker.info(); + 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; @@ -328,8 +329,18 @@ export class DetailPiAgent { return; } if (host.recallRepeats >= 2) throw Error('模型重复检索同一证据,已停止继续调用;历史和结果已保留'); - if (liveCanvas && typeof host.canvas.workflowRead === 'function') await host.readWorkflow({ section: host.currentWorkflow?.canvasFirst ? 'slots' : 'overview' }); - if (++host.rounds > host.callBudget) throw Error(`本轮已达${host.callBudget}次调用预算,停止继续调用`); + 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; @@ -367,8 +378,11 @@ export class DetailPiAgent { 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', @@ -387,6 +401,7 @@ export class DetailPiAgent { }); 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); @@ -409,7 +424,11 @@ export class DetailPiAgent { ? 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 }); + 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}结果待核查;不会重发`); @@ -471,7 +490,7 @@ export class DetailPiAgent { 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) && Date.now() - (host.lastDelta ?? 0) > 45 && (parts.text || parts.thought)) { + 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)); } @@ -513,18 +532,23 @@ export class DetailPiAgent { } this.currentWorkflow = latest.workflow; this.productEvidence = latest.product; - if (latest.workflow.template?.enterprise && !this.compacting) { - this.enterpriseContext ??= new EnterpriseContext(this.store); - this.knowledgeContext = await this.enterpriseContext.load({task:this.task || 'chat', - brandName:latest.product.product.brandName,categoryName:latest.product.product.categoryName, - templateId:latest.workflow.template.id,releaseId:latest.workflow.template.version,turn:this.turnEpoch}); - } + if (this.task && this.task !== 'chat' && !this.compacting) this.knowledgeContext = await this.readEnterpriseRules(); return latest.workflow; } - async modelContext(options = {}) { + 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}); - if (this.knowledgeContext) { - value.enterpriseKnowledge=this.knowledgeContext; + 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; @@ -620,7 +644,8 @@ export class DetailPiAgent { 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.broker ? await this.broker.info() : this.info; + 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('用户已更换模型,请重新打开会话后再调用'); @@ -628,7 +653,8 @@ export class DetailPiAgent { if (this.nativeToolActive) throw Error('原操作仍在执行,请先等待或核查;不会重发'); if (requiredTool && !this.session.getActiveToolNames().includes(requiredTool)) throw Error('指定工具未开放'); this.requiredTool = null; - this.task = taskFor(text); + 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; @@ -661,10 +687,10 @@ export class DetailPiAgent { 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 = await this.projectedContext(prompt, images); - const projectedChars = contextTextLength(projection); + 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 (projectedChars > 80000 || exceedsModelBudget(projection, this.info.model, 0.8)) { + 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 diff --git a/apps/detail-agent-pi/src/context-budget.mjs b/apps/detail-agent-pi/src/context-budget.mjs index cb5d7798..768a139e 100644 --- a/apps/detail-agent-pi/src/context-budget.mjs +++ b/apps/detail-agent-pi/src/context-budget.mjs @@ -82,7 +82,7 @@ export function projectContext(context,{bindingHash,templateOriginal}={}) { } } const ruleResults = context.messages.map((m, index) => m.role === 'toolResult' && m.toolName === 'context_read' && !m.isError ? index : -1).filter(i => i >= 0); - const newestRule = ruleResults.at(-1); + const newestRule = ruleResults.findLast(index => index > currentTurn); const messages = context.messages.map((m, index) => { if (templateOriginal && index < currentTurn && m.role === 'user' && Array.isArray(m.content) && m.content.some(block => block.type === 'image')) { return { ...m, content: [...m.content.filter(block => block.type !== 'image'), @@ -99,7 +99,7 @@ export function projectContext(context,{bindingHash,templateOriginal}={}) { if(ruleResults.includes(index)&&bindingHash){ try{const old=JSON.parse(m.content.filter(c=>c.type==='text').map(c=>c.text).join(''));staleBinding=typeof old.bindingHash==='string'&&old.bindingHash!==bindingHash;}catch{} } - if (ruleResults.includes(index) && (index !== newestRule||staleBinding)) return { ...m, content: [{ type: 'text', text: JSON.stringify({ archivedRuleResult: m.toolCallId, note: staleBinding?'此为旧规则域历史,不能作为当前店铺/品类规则;本轮权威规则已重新注入。':'原规则在项目快照;当前核心规则已注入,额外细节按topic读取。' }) }] }; + if (ruleResults.includes(index) && (index !== newestRule||staleBinding)) return { ...m, content: [{ type: 'text', text: JSON.stringify({ archivedRuleResult: m.toolCallId, note: staleBinding?'此为旧规则域历史,不能作为当前店铺或品类的依据。':'原规则读取结果保存在会话记录中,需要当前依据时按主题读取。' }) }] }; if (!canvasResults.includes(index) || index === newest) return m; const summary = summaries.get(index); const compact = summary && !detailed.has(index) ? { ...summary, diff --git a/apps/detail-agent-pi/src/desktop-host.mjs b/apps/detail-agent-pi/src/desktop-host.mjs index d2be0167..78dcbb44 100644 --- a/apps/detail-agent-pi/src/desktop-host.mjs +++ b/apps/detail-agent-pi/src/desktop-host.mjs @@ -119,7 +119,7 @@ export async function runSession(config, call, sharedBroker, recovery) { store = await ProjectStore.open(config.root, { scope: info.accountScope, project: config.project }, binding, { continuous:true }); const canvas = new StdioCanvas(call, config.documentId); const usage = []; - host = await DetailPiAgent.open({ store, broker, canvas, projectEvidence:config.projectEvidence, documentId:config.documentId, onEvent: event => { + host = await DetailPiAgent.open({ store, broker, brokerInfo:info, canvas, projectEvidence:config.projectEvidence, documentId:config.documentId, onEvent: event => { if (event.type === 'usage') usage.push(event); } }); releaseRecovery = bindRecovery(host, recovery, {scope:info.accountScope, project:config.project, documentId:config.documentId, diff --git a/apps/detail-agent-pi/src/workflow-context.mjs b/apps/detail-agent-pi/src/workflow-context.mjs index 28c12f5c..19b835de 100644 --- a/apps/detail-agent-pi/src/workflow-context.mjs +++ b/apps/detail-agent-pi/src/workflow-context.mjs @@ -118,3 +118,37 @@ export function workflowHistory(context, { version } = {}) { return next; }) }; } + +export function imageProgressQuestion(text) { + const question = text.trim().replace(/[。?!?s]+$/u, ''); + return /^(?:请问|帮我看看)?(?:这套详情(?:页|图)|这套图)?(?:现在|目前|后面|接下来)?(?:还(?:有|剩)|剩下|剩余)(?:几|多少)(?:张(?:图|图片)?|个(?:图位|图片|格子))(?:还)?(?:没(?:有)?(?:完成|做完|做|回填)|未(?:完成|回填))(?:的|了|啊|呢)?$/u.test(question) + || /^(?:请问|帮我看看)?(?:这套详情(?:页|图)|这套图)?(?:现在|目前|后面)?(?:还(?:有|剩)|剩下)(?:几|多少)(?:张(?:图|图片)?|个(?:图位|格子))(?:了|呢)?$/u.test(question); +} + +export async function readImageProgress(readPage, overview) { + if (!overview?.available || !overview.canvasFirst || !overview.outputId) return '当前画布暂无可核对的图位。'; + const {outputId, productVariantId, version} = overview; + const seen = new Set(); + let total, applied = 0, ready = 0, pending = 0; + do { + const page = await readPage({section: 'slots', outputId, offset: seen.size}); + total ??= page?.totalSlots; + if (!page?.available || page.outputId !== outputId || page.version !== version || + page.productVariantId !== productVariantId || !Number.isSafeInteger(total) || + page.totalSlots !== total || !Array.isArray(page.slots) || !page.slots.length && seen.size < total) { + return '图位状态正在更新,暂时无法核对完整数量。'; + } + for (const slot of page.slots) { + if (!slot.id || seen.has(slot.id)) return '图位状态正在更新,暂时无法核对完整数量。'; + seen.add(slot.id); + if (slot.selectedCandidateId) applied++; + else if (slot.candidates?.some(candidate => candidate.status === 'complete')) ready++; + else if (slot.candidates?.some(candidate => ['unknown','running','dispatched','prepared'].includes(candidate.status))) pending++; + } + } while (seen.size < total); + if (seen.size !== total) return '图位状态正在更新,暂时无法核对完整数量。'; + if (!total) return '当前画布暂无可核对的图位。'; + const remaining = total - applied; + return `当前画布有 ${total} 个图位,已回填 ${applied} 个,还有 ${remaining} 个未回填。` + + (remaining ? `其中 ${ready} 个已有候选,${pending} 个正在生成或待核查,${remaining - ready - pending} 个暂无可用候选。` : ''); +} diff --git a/apps/detail-agent-pi/test/conversation.test.mjs b/apps/detail-agent-pi/test/conversation.test.mjs index 42e7b287..092129c6 100644 --- a/apps/detail-agent-pi/test/conversation.test.mjs +++ b/apps/detail-agent-pi/test/conversation.test.mjs @@ -21,12 +21,16 @@ function canvas(){ async edit(edit,revision){assert.ok(this.allowed&&!this.writeBlocked);assert.equal(revision,this.revision);this.writes++;this.text=edit.params.text;this.revision++;return this.read();}, async delta(){},async activity(){},async end(){this.allowed=false;}}; } -async function fixture(reply,{shop='unconfigured'}={}){ +async function fixture(reply,{shop='unconfigured',reuseOpeningInfo=false}={}){ const root=await fs.mkdtemp(path.join(os.tmpdir(),'jiansun-pi-')),project=randomUUID(),identity={scope,project},store=await ProjectStore.open(root,identity,binding(shop),{continuous:true}); const requests=[],native=canvas(),events=[]; const broker={info:async()=>({accountScope:scope,model:{id:'qa',protocol:'openai-chat'},pending:[]}),round(){const id=randomUUID();return {id,fetch:async(_url,init)=>{const payload=JSON.parse(init.body);requests.push(payload);const response=reply(payload,requests.length,native);return response instanceof Response?response:new Response(response,{headers:{'content-type':'text/event-stream'}});},finish:async()=>({phase:'complete'})};}}; - let host=await DetailPiAgent.open({store,broker,canvas:native,projectEvidence:evidence(project),documentId:7,onEvent:e=>events.push(e)}); - return {root,identity,store,broker,native,requests,events,get host(){return host;},async reopen(shop,color='胡桃色'){await host.dispose();const next=await ProjectStore.open(root,identity,binding(shop),{continuous:true});host=await DetailPiAgent.open({store:next,broker,canvas:native,projectEvidence:evidence(project,color),documentId:7,onEvent:e=>events.push(e)});return next;},async close(){await host.dispose();await cleanup(root);}}; + let infoCalls=0; + const readInfo=broker.info; + broker.info=async()=>{infoCalls++;return readInfo();}; + const brokerInfo=reuseOpeningInfo?await broker.info():undefined; + let host=await DetailPiAgent.open({store,broker,brokerInfo,canvas:native,projectEvidence:evidence(project),documentId:7,onEvent:e=>events.push(e)}); + return {root,identity,store,broker,native,requests,events,get infoCalls(){return infoCalls;},get host(){return host;},async reopen(shop,color='胡桃色'){await host.dispose();const next=await ProjectStore.open(root,identity,binding(shop),{continuous:true});host=await DetailPiAgent.open({store:next,broker,canvas:native,projectEvidence:evidence(project,color),documentId:7,onEvent:e=>events.push(e)});return next;},async close(){await host.dispose();await cleanup(root);}}; } test('three plain native-wire turns discuss product facts without JSON or a canvas write grant',async()=>{ @@ -45,6 +49,47 @@ test('three plain native-wire turns discuss product facts without JSON or a canv }finally{await x.close();} }); +test('an enterprise project supports immediate conversation and fetches rules only when asked',async()=>{ + const provenance='当前企业模板条款正文'; + const x=await fixture((_payload,n)=>n===2?tool('context_read',{topic:'sources'}):sse({content:'使用当前项目资料。'})); + const sections=[]; + x.native.workflowRead=async({section})=>{ + sections.push(section); + return {available:true,version:1,section,canvasFirst:true,outputId:'detail', + template:{id:'selected-template',version:'fixed-release',enterprise:true}, + productEvidence:evidence(x.identity.project)}; + }; + let knowledgeReads=0; + x.host.enterpriseContext={async load(){knowledgeReads++;return {skills:[{text:provenance}],rules:[]};}}; + try{ + await x.host.prompt('你好,知道现在在做什么吗?'); + assert.equal(knowledgeReads,0); + assert.ok(JSON.stringify(x.requests[0].messages).includes('测试电脑桌')); + await x.host.prompt('请查一下这个模板的规则来源'); + assert.equal(knowledgeReads,1); + assert.ok(x.requests[2].messages.some(m=>m.role==='tool'&&m.content.includes(provenance))); + await x.host.prompt('谢谢,继续聊一下'); + assert.equal(knowledgeReads,1); + assert.ok(!JSON.stringify(x.requests[3].messages).includes(provenance)); + assert.ok(sections.every(section=>section==='overview')); + assert.equal(x.native.writes,0); + }finally{await x.close();} +}); + +test('the first desktop turn reuses its opening model check and logs first text timing',async()=>{ + const x=await fixture(()=>sse({content:'可以继续聊天。'}),{reuseOpeningInfo:true}); + try{ + await x.host.prompt('你好'); + assert.equal(x.infoCalls,1); + const events=(await fs.readFile(path.join(x.store.dir,'events.jsonl'),'utf8')).trim().split('\n').map(JSON.parse); + const finished=events.find(event=>event.type==='provider_finished'); + assert.ok(Number.isFinite(finished.responseHeadersMs)); + assert.ok(finished.firstTextMs>=finished.responseHeadersMs); + await x.host.prompt('继续聊'); + assert.equal(x.infoCalls,2); + }finally{await x.close();} +}); + test('project conversation survives restart and first rule binding while old-domain memory is not reinjected',async()=>{ const x=await fixture(()=>sse({content:'当前资料以本轮注入为准。'})); try{ @@ -292,3 +337,51 @@ test('legacy migration rejects a pre-existing target with different bytes and ne await assert.rejects(fs.access(path.join(stableDir,'session.json')),error=>error.code==='ENOENT'); }finally{await cleanup(root);} }); + +test('a plain image count reads every slot without dispatching to the selected model',async()=>{ + const x=await fixture(()=>sse({content:'可以讨论这些图。'})); + const slots=Array.from({length:27},(_,index)=>({ + id:'slot-'+index, + selectedCandidateId:index<2?'accepted-'+index:null, + candidates:index===2?[{status:'complete'}]:index===3?[{status:'unknown'}]:[], + })); + const offsets=[]; + x.native.workflowRead=async({section,offset=0})=>{ + if(section==='slots')offsets.push(offset); + return {available:true,version:18,section,canvasFirst:true,outputId:'detail',productVariantId:'brown', + totalSlots:slots.length,slots:slots.slice(offset,offset+12),productEvidence:evidence(x.identity.project)}; + }; + try{ + const result=await x.host.prompt('后面还有几张图没有完成的'); + assert.match(result.text,/27 个图位,已回填 2 个,还有 25 个未回填/); + assert.match(result.text,/1 个已有候选,1 个正在生成或待核查,23 个暂无可用候选/); + assert.deepEqual(offsets,[0,12,24]); + assert.equal(result.rounds,0); + assert.equal(x.requests.length,0); + assert.equal(x.native.writes,0); + await x.host.prompt('后面还有几张图没有完成,麻烦先生成首图'); + assert.equal(x.requests.length,1); + }finally{await x.close();} +}); + +test('a desktop turn continues past 12 useful model calls',async()=>{ + const x=await fixture((_payload,n)=>n<=12?tool('workflow_read',{section:'slots',offset:n-1}):sse({content:'逐个核对完了。'})); + x.native.workflowRead=async({section,offset=0})=>({available:true,version:1,section, + canvasFirst:true,outputId:'detail',offset,productEvidence:evidence(x.identity.project)}); + try{ + assert.equal((await x.host.prompt('请逐个核对所有图位的描述')).text,'逐个核对完了。'); + assert.equal(x.requests.length,13); + }finally{await x.close();} +}); + +test('first reply text reaches the canvas even when a reasoning update just arrived',async()=>{ + const chunk=delta=>'data: '+JSON.stringify({id:'qa',model:'qa',choices:[{index:0,delta,finish_reason:null}]})+'\n\n'; + const x=await fixture(()=>chunk({reasoning_content:'检查资料'})+chunk({content:'第一段'})+sse({content:'。第二段。'})); + const presented=[]; + x.native.delta=async text=>{presented.push(text);}; + try{ + assert.equal((await x.host.prompt('用两句短话介绍一下你自己')).text,'第一段。第二段。'); + assert.ok(presented.includes('第一段')); + assert.ok(presented.includes('第一段。第二段。')); + }finally{await x.close();} +}); diff --git a/apps/detail-photocraft/apps/photocraft/src/jiansun_backend.rs b/apps/detail-photocraft/apps/photocraft/src/jiansun_backend.rs index b511997f..6f8ef529 100644 --- a/apps/detail-photocraft/apps/photocraft/src/jiansun_backend.rs +++ b/apps/detail-photocraft/apps/photocraft/src/jiansun_backend.rs @@ -602,16 +602,53 @@ impl Backend { } Ok(out) } - fn refresh_models(&mut self) -> R<()> { - self.models = self.json("agent-models")?["models"].as_array().cloned().ok_or("对话模型目录无效")?; - pi_runtime::enrich_models(&mut self.models)?; - match self.json("execution-models").and_then(|catalog| { - let allowed=catalog["models"].as_array().ok_or("文案模型目录无效")?; - Ok(self.models.iter().filter(|model|allowed.iter().any(|item|item["stage"]=="copy"&&item["key"]==model["key"])).cloned().collect()) - }) {Ok(models)=>{self.copy_models=models;self.copy_error.clear();},Err(error)=>{self.copy_models.clear();self.copy_error=error;}} + fn legacy_text_choices(&self, saved: &Value) -> Vec { + let keys = [&saved["model"], crate::jiansun_models::copy_selection(saved)]; + if !keys.iter().any(|key| key.as_str().is_some_and(|id| Uuid::parse_str(id).is_ok())) { + return vec![]; + } + self.json("agent-models").ok().and_then(|value| value["models"].as_array().cloned()).unwrap_or_default() + } + + fn text_models(&self, catalog: &Value, saved: &Value) -> R> { + let aliases = self.legacy_text_choices(saved); + let active = [&saved["model"], crate::jiansun_models::copy_selection(saved)]; + let mut models = Vec::new(); + let mut seen = std::collections::HashSet::new(); + for row in crate::jiansun_image_protocol::catalog_models(catalog)? { + let alias = aliases.iter().find(|option| option["id"] == row["id"] && active.contains(&&option["key"])); + if let Some(model) = crate::jiansun_models::text_model(row, alias.and_then(|item| item["key"].as_str())) { + if seen.insert(model["id"].as_str().unwrap_or_default().to_owned()) { + models.push(model); + } + } + } + Ok(models) + } + + fn live_text_choice(&self, choice: &str) -> R { + let id = if Uuid::parse_str(choice).is_ok() { + self.legacy_text_choices(&json!({"model":choice})).into_iter().find(|row| row["key"] == choice) + .and_then(|row| row["id"].as_str().map(str::to_owned)).ok_or("原对话模型无法识别,请在设置中重新选择")? + } else { + choice.to_owned() + }; + let catalog = self.public_model_catalog()?; + crate::jiansun_image_protocol::catalog_models(&catalog)? + .filter(|row| row["id"] == id) + .find_map(|row| crate::jiansun_models::text_model(row, Some(choice))) + .ok_or_else(|| "98K 当前未发布此模型的可用对话契约,请重新选择".into()) + } + + fn refresh_models(&mut self, catalog: &Value, saved: &Value) -> R<()> { + self.models = self.text_models(catalog, saved)?; + // The bundled provider database supplies optional capacity metadata only. + let _ = pi_runtime::enrich_models(&mut self.models); + self.copy_models = self.models.clone(); + self.copy_error.clear(); Ok(()) } - fn public_image_catalog(&self) -> R { + fn public_model_catalog(&self) -> R { let response = self.http.get(format!("{UPSTREAM}/api/v2/catalog/models")).send().map_err(|_| "98k公开模型目录暂不可用")?; let (status, catalog) = body(response, 16 * 1024 * 1024)?; if status != 200 { return Err(format!("98k公开模型目录暂不可用(HTTP {status})")); } @@ -630,16 +667,16 @@ impl Backend { if !permitted.contains(&id) { return Err("个人Key未开放此模型,本次生图未提交".into()); } - let catalog = self.public_image_catalog() + let catalog = self.public_model_catalog() .map_err(|error| format!("本次生图未提交。{error}"))?; let contract = crate::jiansun_image_protocol::select(&catalog, &id)?; Ok((id, contract)) } - fn refresh_image_models(&mut self, saved: &Value, available: Option<&[String]>) { + fn refresh_image_models(&mut self, catalog: &Value, saved: &Value, available: Option<&[String]>) { let choice = saved["imageModel"].as_str().unwrap_or_default(); let legacy = Uuid::parse_str(choice).is_ok().then(|| self.image_model_id(choice)); let previous = legacy.as_ref().and_then(|result| result.as_deref().ok()).map(|id| (choice, id)); - match self.public_image_catalog().and_then(|catalog| crate::jiansun_models::image_choices(&catalog, available, previous)) { + match crate::jiansun_models::image_choices(catalog, available, previous) { Ok(items) => { self.image_models = items; self.image_error = legacy.and_then(Result::err).unwrap_or_default(); @@ -684,9 +721,6 @@ impl Backend { Ok(()) => { library_status = "connected"; library_http_status = Some(200); - if let Err(error) = self.refresh_models() { - message = error; - } } Err(failure) => { library_status = failure.state; @@ -697,20 +731,32 @@ impl Backend { } } let saved = self.settings().unwrap_or_else(|_| json!({})); - if !self.auth.is_null() { + if library_status == "connected" { let has_key = saved["key"].as_str().filter(|key| !key.is_empty()); - let mut key_error = None; if let Some(key) = has_key { match self.key_catalog(key) { Ok(items) => available = Some(items), - Err(error) => key_error = Some(error), + Err(error) => { + self.image_error = error.clone(); + message = error; + } } } - if has_key.is_none() || available.is_some() { - self.refresh_image_models(&saved, available.as_deref()); - } else if let Some(error) = key_error { - if message.is_empty() { message = error.clone(); } - self.image_error = error; + match self.public_model_catalog() { + Ok(catalog) => { + if let Err(error) = self.refresh_models(&catalog, &saved) { + self.copy_error.clone_from(&error); + if message.is_empty() { message = error; } + } + if has_key.is_none() || available.is_some() { + self.refresh_image_models(&catalog, &saved, available.as_deref()); + } + } + Err(error) => { + self.image_error.clone_from(&error); + self.copy_error.clone_from(&error); + if message.is_empty() { message = error; } + } } } self.model = saved["model"].as_str().unwrap_or("").into(); @@ -982,12 +1028,11 @@ impl Backend { if file.exists() && matches!(read_json(&file)?["phase"].as_str(), Some("dispatched" | "unknown")) { return Err("原请求待核查,不能再次生成".into()); } - let models = self.json("agent-models")?["models"].as_array().cloned().ok_or("模型目录无效")?; let chosen = input["model"].as_str().filter(|s| !s.is_empty()).or(settings["model"].as_str()).ok_or("请选择模型")?; - let model = models.iter().find(|m| m["key"].as_str() == Some(chosen)).ok_or("管理员已关闭此模型,请重新选择")?; if settings["model"].as_str() != Some(chosen) { return Err("请先保存对话模型选择".into()); } + let model = self.live_text_choice(chosen)?; let available = self.key_catalog(key)?; if !available.iter().any(|id| model["id"] == id.as_str()) { return Err("此Key未开放所选对话模型,请在对话栏重新选择".into()); @@ -1161,8 +1206,9 @@ impl Backend { "project-state" => self.project_state(input), "configure" => { self.validate()?; - self.refresh_models()?; let saved = self.settings().unwrap_or_else(|_| json!({})); + let catalog = self.public_model_catalog()?; + self.refresh_models(&catalog, &saved)?; let key = input["key"] .as_str() .filter(|s| !s.is_empty()) @@ -1170,7 +1216,7 @@ impl Backend { .filter(|s| (12..=500).contains(&s.len())) .ok_or("请填写个人98k Key")?; let available = self.key_catalog(key)?; - self.refresh_image_models(&saved, Some(&available)); + self.refresh_image_models(&catalog, &saved, Some(&available)); let settings = crate::jiansun_models::selection(input, &saved, &self.models, &self.copy_models, &self.image_models, &available)?; vault(&format!("models:{}", self.scope()))?.set_password(&settings.to_string()).map_err(|_| "个人Key未能保存到系统凭据库")?; self.model = settings["model"].as_str().unwrap_or("").into(); @@ -1962,10 +2008,15 @@ mod tests { let root = std::env::var_os("JIANSUN_LIVE_PROFILE").expect("explicit local acceptance profile"); let mut b = Backend::new(PathBuf::from(root)).unwrap(); b.validate().unwrap(); - b.refresh_models().unwrap(); let saved = b.settings().unwrap(); + let catalog = b.public_model_catalog().unwrap(); + b.refresh_models(&catalog, &saved).unwrap(); let available = b.key_catalog(saved["key"].as_str().unwrap()).unwrap(); - b.refresh_image_models(&saved, Some(&available)); + b.refresh_image_models(&catalog, &saved, Some(&available)); + let selectable_text: Vec<_> = b.models.iter().filter(|m| m["id"].as_str().is_some_and(|id| available.iter().any(|item| item == id))) + .filter_map(|m| m["id"].as_str()).collect(); + assert!(!selectable_text.is_empty()); + println!("{} {}", "可选对话模型", selectable_text.join(", ")); let public = b.connection_data(&saved, Some(&available)); assert!(!b.image_models.is_empty(), "{}", b.image_error); assert_eq!(public["keySaved"], true); diff --git a/apps/detail-photocraft/apps/photocraft/src/jiansun_creation.rs b/apps/detail-photocraft/apps/photocraft/src/jiansun_creation.rs index f51abbdc..da816466 100644 --- a/apps/detail-photocraft/apps/photocraft/src/jiansun_creation.rs +++ b/apps/detail-photocraft/apps/photocraft/src/jiansun_creation.rs @@ -742,24 +742,10 @@ impl Backend { self.validate()?; let settings = self.settings()?; let key = settings["key"].as_str().ok_or("请连接个人98k Key")?; - let selected = self.json("agent-models")?["models"] - .as_array() - .and_then(|a| a.iter().find(|m| m["key"] == *crate::jiansun_models::text_selection(&settings,stage))) - .cloned() - .ok_or(if stage=="facts" {"所选对话模型已关闭"} else {"请选择已开放的文案模型"})?; - if !matches!( - (selected["protocol"].as_str(), selected["path"].as_str()), - (Some("openai-chat"), Some("/v1/chat/completions")) | (Some("anthropic-messages"), Some("/v1/messages")) - ) { - return Err("所选模型对话协议尚未适配".into()); - } - let purpose = if stage == "facts" { "analysis" } else { "copy" }; - let catalog = self.json("execution-models")?; - let allowed = catalog["models"] - .as_array() - .and_then(|a| a.iter().find(|m| m["key"] == selected["key"] && m["stage"] == purpose)) - .ok_or("所选模型尚未开放此用途,请在小壹设置中切换对应模型")?; - if stage == "facts" && allowed["protocol"]["supportsImageInput"] != true { + let choice = crate::jiansun_models::text_selection(&settings,stage).as_str().filter(|key| !key.is_empty()) + .ok_or(if stage == "facts" { "请选择可读图的对话模型" } else { "请选择文案模型" })?; + let selected = self.live_text_choice(choice)?; + if stage == "facts" && selected["supportsImageInput"] != true { return Err("资料分析需要可读图的对话模型".into()); } if !self.key_catalog(key)?.iter().any(|id| selected["id"] == id.as_str()) { diff --git a/apps/detail-photocraft/apps/photocraft/src/jiansun_models.rs b/apps/detail-photocraft/apps/photocraft/src/jiansun_models.rs index c9b83ed7..e1e7024f 100644 --- a/apps/detail-photocraft/apps/photocraft/src/jiansun_models.rs +++ b/apps/detail-photocraft/apps/photocraft/src/jiansun_models.rs @@ -29,6 +29,33 @@ pub fn image_choices(catalog: &Value, available: Option<&[String]>, previous: Op Ok(out) } +/// Convert the published 98K text contracts to the two protocols used by Pi. +pub(super) fn text_model(row: &Value, key: Option<&str>) -> Option { + let id = row["id"].as_str().filter(|id| !id.is_empty() && id.len() <= 200)?; + if row["status"] != "available" || row["available"] != true + || !matches!(row["modality"].as_str(), Some("text" | "multimodal")) + { + return None; + } + let invocation = &row["invocation"]; + if invocation["method"] != "POST" || invocation["content_type"] != "application/json" { + return None; + } + let auth = &invocation["authentication"]; + let path = invocation["path"].as_str()?; + let protocol = match path { + "/v1/chat/completions" if auth["type"] == "bearer" && auth["header"] == "Authorization" => "openai-chat", + "/v1/messages" if auth["type"] == "api_key" && auth["header"] == "X-Api-Key" => "anthropic-messages", + _ => return None, + }; + let properties = invocation["input_schema"]["properties"].as_object()?; + if !["model", "messages", "stream", "tools"].iter().all(|field| properties.contains_key(*field)) { + return None; + } + let image = row["input_modes"].as_array().map_or(row["modality"] == "multimodal", |modes| modes.iter().any(|mode| mode == "image")); + Some(json!({"key":key.unwrap_or(id),"id":id,"label":id,"protocol":protocol,"path":path,"supportsImageInput":image})) +} + pub fn key_models(catalog: &Value) -> R> { let rows = catalog["data"].as_array().filter(|a| a.len() <= 2000).ok_or("98k没有返回有效的模型目录")?; let mut out = Vec::new(); @@ -103,6 +130,27 @@ pub(super) fn apply_pi_capacity(model:&mut Value,catalog:&[Value]) { mod tests { use super::*; + #[test] + fn a_new_98k_text_contract_needs_no_platform_model_row() { + let mut published = json!({"id":"new-text-model","status":"available","available":true, + "modality":"text","input_modes":["text"],"invocation":{ + "method":"POST","content_type":"application/json","path":"/v1/chat/completions", + "authentication":{"type":"bearer","header":"Authorization"}, + "input_schema":{"properties":{"model":{},"messages":{},"stream":{},"tools":{}}}}}); + let model = text_model(&published, None).unwrap(); + assert_eq!(model["key"], "new-text-model"); + assert_eq!(model["protocol"], "openai-chat"); + assert_eq!(model["supportsImageInput"], false); + assert_eq!(text_model(&published, Some("legacy-selection")).unwrap()["key"], "legacy-selection"); + published["input_modes"] = json!(["text","image"]); + assert_eq!(text_model(&published, None).unwrap()["supportsImageInput"], true); + published["invocation"]["path"] = json!("/v1/responses"); + assert!(text_model(&published, None).is_none()); + published["invocation"]["path"] = json!("/v1/chat/completions"); + published["status"] = json!("maintenance"); + assert!(text_model(&published, None).is_none()); + } + #[test] fn personal_copy_model_is_independent_and_respects_its_role_and_key() { let chat=vec![json!({"key":"talk","id":"talk-model"})];let copy=vec![json!({"key":"copy","id":"copy-model"})];let images=vec![json!({"key":"image","id":"image-model"})]; diff --git a/apps/detail-photocraft/apps/photocraft/src/jiansun_pi.rs b/apps/detail-photocraft/apps/photocraft/src/jiansun_pi.rs index 1be7fa11..03a6b0fc 100644 --- a/apps/detail-photocraft/apps/photocraft/src/jiansun_pi.rs +++ b/apps/detail-photocraft/apps/photocraft/src/jiansun_pi.rs @@ -391,14 +391,7 @@ fn selected(backend: &mut Backend, role:&str) -> R<(Value, Value)> { backend.validate()?; let settings = backend.settings()?; let key = settings["key"].as_str().ok_or("请先连接个人98k Key")?; - let models = backend.json("agent-models")?; - let model = models["models"].as_array().and_then(|a| a.iter().find(|m| m["key"] == settings["model"])).ok_or("当前对话模型未开放")?.clone(); - if !matches!( - (model["protocol"].as_str(), model["path"].as_str()), - (Some("openai-chat"), Some("/v1/chat/completions")) | (Some("anthropic-messages"), Some("/v1/messages")) - ) { - return Err("模型协议未适配".into()); - } + let model = backend.live_text_choice(settings["model"].as_str().filter(|key| !key.is_empty()).ok_or("请选择对话模型")?)?; if !backend.key_catalog(key)?.iter().any(|id| model["id"] == id.as_str()) { return Err("个人Key未开放此模型".into()); }