Commit 5fe74b39d72816ac980cf54ec6581e3a2dbb9f5e
1 parent
dc48ebe1
fix(open-api): 全局 agent invoke 端点补写会话历史
之前 invoke 端点只把结果落 job(.data/jobs-openapi.json),从不生成 conv-*
会话、也不写 meta.json/history.json,导致网页 AgentChat 看不到任何 API
调用产生的对话记录。现在复用 agent-chat.js 的会话存储函数,写到同一份
system/agents/{agentId}/_global/conversations/{convId}/ 下。
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Showing
2 changed files
with
40 additions
and
6 deletions
server/routes/agent-chat.js
| ... | ... | @@ -2872,3 +2872,10 @@ ${resultsText.join('\n\n')} |
| 2872 | 2872 | }); |
| 2873 | 2873 | |
| 2874 | 2874 | module.exports = router; |
| 2875 | +// 供 open-api.js 的 invoke 端点复用——外部 API 调用也要写进同一份会话存储, | |
| 2876 | +// 否则网页 AgentChat 看不到 API 触发的对话(见 docs/done/open-api-manual.md 的 invoke 端点)。 | |
| 2877 | +module.exports.loadMeta = loadMeta; | |
| 2878 | +module.exports.saveMeta = saveMeta; | |
| 2879 | +module.exports.appendHistory = appendHistory; | |
| 2880 | +module.exports.logMessage = logMessage; | |
| 2881 | +module.exports.flushLogBufferForConv = flushLogBufferForConv; | ... | ... |
server/routes/open-api.js
| ... | ... | @@ -607,9 +607,26 @@ async function _executeAgentInvocation(job, { agentDir, namespacedId, task, cont |
| 607 | 607 | const { loadConfigFromSkills } = require('../src/core/skill-config-loader'); |
| 608 | 608 | const AgentManager = require('../src/core/agent-manager'); |
| 609 | 609 | const config = require('../config'); |
| 610 | + // 复用 agent-chat.js 的会话存储,让 API 调用也能在网页 AgentChat 里追溯 | |
| 611 | + // (否则 job 结果只落 .data/jobs-openapi.json,网页永远看不到)。 | |
| 612 | + const agentChat = require('./agent-chat'); | |
| 613 | + const convId = `conv-${Date.now().toString(36)}`; | |
| 610 | 614 | |
| 611 | 615 | jobManager.updateJob(job.jobId, { status: 'running', progress: 'Initializing agent session...' }); |
| 612 | 616 | |
| 617 | + agentChat.saveMeta(namespacedId, convId, { | |
| 618 | + conversationId: convId, | |
| 619 | + agentId: namespacedId, | |
| 620 | + title: task.slice(0, 50), | |
| 621 | + type: 'api_invoke', | |
| 622 | + createdAt: new Date().toISOString(), | |
| 623 | + status: 'running', | |
| 624 | + stats: { messageCount: 1, toolCallCount: 0 }, | |
| 625 | + jobId: job.jobId, | |
| 626 | + }); | |
| 627 | + agentChat.logMessage(namespacedId, convId, 'user', task); | |
| 628 | + agentChat.appendHistory(namespacedId, convId, { role: 'user', content: task, messageId: `job-${job.jobId}`, timestamp: new Date().toISOString() }); | |
| 629 | + | |
| 613 | 630 | let enrichedTask = task; |
| 614 | 631 | |
| 615 | 632 | // Process Context Files |
| ... | ... | @@ -662,18 +679,28 @@ async function _executeAgentInvocation(job, { agentDir, namespacedId, task, cont |
| 662 | 679 | |
| 663 | 680 | try { |
| 664 | 681 | const result = await manager.callAgent(namespacedId, enrichedTask, { onData: onData(namespacedId) }); |
| 665 | - | |
| 666 | - jobManager.updateJob(job.jobId, { | |
| 667 | - status: 'completed', | |
| 668 | - progress: 'Invocation completed', | |
| 669 | - result: { | |
| 670 | - output: result.output || fullOutput || '(Empty Output)' | |
| 682 | + const output = result.output || fullOutput || '(Empty Output)'; | |
| 683 | + | |
| 684 | + jobManager.updateJob(job.jobId, { | |
| 685 | + status: 'completed', | |
| 686 | + progress: 'Invocation completed', | |
| 687 | + result: { | |
| 688 | + output | |
| 671 | 689 | } |
| 672 | 690 | }); |
| 673 | 691 | |
| 692 | + agentChat.logMessage(namespacedId, convId, 'agent', output); | |
| 693 | + agentChat.appendHistory(namespacedId, convId, { role: 'assistant', content: output, messageId: `job-${job.jobId}-out`, timestamp: new Date().toISOString() }); | |
| 694 | + const meta = agentChat.loadMeta(namespacedId, convId); | |
| 695 | + if (meta) { meta.status = 'completed'; agentChat.saveMeta(namespacedId, convId, meta); } | |
| 696 | + agentChat.flushLogBufferForConv(namespacedId, convId); | |
| 697 | + | |
| 674 | 698 | try { await manager.closeAllSessions(); } catch {} |
| 675 | 699 | } catch (err) { |
| 676 | 700 | jobManager.updateJob(job.jobId, { status: 'failed', error: err.message, progress: `Agent execution failed: ${err.message}` }); |
| 701 | + const meta = agentChat.loadMeta(namespacedId, convId); | |
| 702 | + if (meta) { meta.status = 'failed'; agentChat.saveMeta(namespacedId, convId, meta); } | |
| 703 | + agentChat.flushLogBufferForConv(namespacedId, convId); | |
| 677 | 704 | try { await manager.closeAllSessions(); } catch {} |
| 678 | 705 | throw err; |
| 679 | 706 | } | ... | ... |