feat: integrate approved MCP tools and recovery for 1.12
This commit is contained in:
@@ -5,6 +5,7 @@ import { workingCandidates, workingDocument, workingEntry, workingMessage, verif
|
||||
import { latestToolDocument, latestToolMessage, verifyLatestToolDocument } from './tool-feedback.mjs';
|
||||
import { projectMemoryMessage, MEMORY_CONTEXT_LIMITS } from '../memory/project-memory.mjs';
|
||||
import { skillCatalogMessage, SKILL_LIMITS } from '../skills/skill-registry.mjs';
|
||||
import { mcpCatalogMessage, MCP_LIMITS } from '../tools/mcp-contract.mjs';
|
||||
|
||||
const hash = value => createHash('sha256').update(typeof value === 'string' ? value : JSON.stringify(value), 'utf8').digest('hex');
|
||||
const PLACEHOLDER_ID = '00000000-0000-0000-0000-000000000000';
|
||||
@@ -155,7 +156,7 @@ export class RequestPlanner {
|
||||
return { artifact_id: artifact.id, sha256: artifact.sha256, group_sha256: group.sha256, bytes: Buffer.byteLength(text, 'utf8'), chunks: chunks.length };
|
||||
}
|
||||
|
||||
prepare(session, run, config, { system, tools, stepId, projectMemory = null, skillCatalog = null }) {
|
||||
prepare(session, run, config, { system, tools, stepId, projectMemory = null, skillCatalog = null, mcpCatalog = null }) {
|
||||
const task = this.store.owned('task', run.taskId, session.id);
|
||||
this.store.owned('run', run.id, session.id);
|
||||
const groups = historyGroups(session.providerMessages), budget = budgetFor(config, run, task);
|
||||
@@ -248,6 +249,20 @@ export class RequestPlanner {
|
||||
...(latestReference ? [latestToolMessage(latestReference, latestDocument)] : []),
|
||||
...(workingReference ? [workingMessage(workingReference, workingDocument(session, run, entries))] : []),
|
||||
...structuredClone(groups.filter(group => selected.has(group.index)).flatMap(group => group.messages))];
|
||||
let mcpReference = null, mcpDocument = null;
|
||||
if (mcpCatalog?.projectId === session.projectId && mcpCatalog.entries?.length) {
|
||||
mcpDocument = { ...mcpCatalog, sessionId: session.id, taskId: run.taskId, runId: run.id, entries: [] };
|
||||
for (const entry of mcpCatalog.entries.slice(0, MCP_LIMITS.allowedTools)) {
|
||||
mcpDocument.entries.push(entry);
|
||||
const candidate = [fixed, mcpCatalogMessage({ id: PLACEHOLDER_ID, sha256: PLACEHOLDER_SHA }, mcpDocument), ...messages.slice(1)];
|
||||
if (Buffer.byteLength(JSON.stringify(mcpDocument), 'utf8') > 24000
|
||||
|| requestMetrics(completionBody(config, candidate, tools)).estimatedInputTokens > Math.min(budget.inputBudget, budget.targetInputTokens)) mcpDocument.entries.pop();
|
||||
}
|
||||
if (mcpDocument.entries.length) {
|
||||
mcpReference = this.artifact(session, run, mcpDocument, 'context-mcp-catalog');
|
||||
messages.splice(1, 0, mcpCatalogMessage(mcpReference, mcpDocument));
|
||||
}
|
||||
}
|
||||
let skillReference = null, skillDocument = null;
|
||||
if (skillCatalog?.projectId === session.projectId && skillCatalog.entries?.length) {
|
||||
skillDocument = { ...skillCatalog, sessionId: session.id, taskId: run.taskId, runId: run.id, entries: [] };
|
||||
@@ -295,7 +310,7 @@ export class RequestPlanner {
|
||||
workingRetentionTokens: Math.min(2048, Math.max(0, metricsFor(new Set(lastUser ? [lastUser.index] : []), true,
|
||||
entries.filter(entry => entry.observation.kind === 'file' && !entry.truncated && entry.total_content_characters <= 2000)).estimatedInputTokens
|
||||
- metricsFor(new Set(lastUser ? [lastUser.index] : []), true, []).estimatedInputTokens)),
|
||||
originalMessages: session.providerMessages.length, retainedMessages: messages.length - 1 - (reference ? 1 : 0) - (workingReference ? 1 : 0) - (latestReference ? 1 : 0) - (memoryReference ? 1 : 0) - (skillReference ? 1 : 0),
|
||||
originalMessages: session.providerMessages.length, retainedMessages: messages.length - 1 - (reference ? 1 : 0) - (workingReference ? 1 : 0) - (latestReference ? 1 : 0) - (memoryReference ? 1 : 0) - (skillReference ? 1 : 0) - (mcpReference ? 1 : 0),
|
||||
archivedGroups: omitted.length, retainedGroups: groups.filter(group => selected.has(group.index)).map(group => group.index),
|
||||
historyIndexArtifactId: reference?.id ?? null, historyIndexSha256: reference?.sha256 ?? null, actualUsage: null,
|
||||
workingSetArtifactId: workingReference?.id ?? null, workingSetSha256: workingReference?.sha256 ?? null, workingSetEntries: entries.length,
|
||||
@@ -305,6 +320,8 @@ export class RequestPlanner {
|
||||
projectMemoryEntries: memoryReference ? memoryDocument.entries.length : 0,
|
||||
skillCatalogArtifactId: skillReference?.id ?? null, skillCatalogSha256: skillReference?.sha256 ?? null,
|
||||
skillCatalogEntries: skillReference ? skillDocument.entries.length : 0,
|
||||
mcpCatalogArtifactId: mcpReference?.id ?? null, mcpCatalogSha256: mcpReference?.sha256 ?? null,
|
||||
mcpCatalogEntries: mcpReference ? mcpDocument.entries.length : 0,
|
||||
requestAttempts: 0, responseSha256: null, source: 'workbench' };
|
||||
const artifact = this.artifact(session, run, record, 'context-plan'); record.artifactId = artifact.id;
|
||||
run.currentContextPlanId = record.id; session.estimatedTokens = record.estimatedInputTokens;
|
||||
@@ -374,6 +391,19 @@ export function verifyPlanEvidence(store, session, plan, cache = new Map()) {
|
||||
const message = skillCatalogMessage(artifact, document);
|
||||
if (body.messages.filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('技能目录与实际发送内容不匹配。');
|
||||
}
|
||||
if ((frozen.mcpCatalogArtifactId ?? null) !== (plan.mcpCatalogArtifactId ?? null)
|
||||
|| (frozen.mcpCatalogSha256 ?? null) !== (plan.mcpCatalogSha256 ?? null)
|
||||
|| (frozen.mcpCatalogEntries ?? 0) !== (plan.mcpCatalogEntries ?? 0)) throw new Error('MCP目录与发送前的封存记录不匹配。');
|
||||
if (plan.mcpCatalogArtifactId) {
|
||||
const artifact = get(plan.mcpCatalogArtifactId, 'context-mcp-catalog', plan.mcpCatalogSha256);
|
||||
const document = JSON.parse(store.artifactStore.read(artifact));
|
||||
if (artifact.taskId !== plan.taskId || artifact.runId !== plan.runId || document.projectId !== session.projectId
|
||||
|| document.sessionId !== session.id || document.taskId !== plan.taskId || document.runId !== plan.runId
|
||||
|| document.kind !== 'registered-mcp-catalog' || !Array.isArray(document.entries) || document.entries.length !== plan.mcpCatalogEntries
|
||||
|| document.entries.length > MCP_LIMITS.allowedTools || Buffer.byteLength(JSON.stringify(document), 'utf8') > 24000) throw new Error('MCP目录归属或范围不匹配。');
|
||||
const message = mcpCatalogMessage(artifact, document);
|
||||
if (body.messages.filter(value => value.role === 'system' && value.content === message.content).length !== 1) throw new Error('MCP目录与实际发送内容不匹配。');
|
||||
}
|
||||
if (plan.latestToolsArtifactId) {
|
||||
const artifact = get(plan.latestToolsArtifactId, 'context-latest-tools', plan.latestToolsSha256);
|
||||
const document = JSON.parse(store.artifactStore.read(artifact));
|
||||
|
||||
Reference in New Issue
Block a user