@mcp-abap-adt/llm-agent-libs 33.1.0 → 34.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -22
- package/dist/adapters/llm-adapter.d.ts +2 -2
- package/dist/adapters/llm-adapter.d.ts.map +1 -1
- package/dist/adapters/llm-adapter.js +39 -14
- package/dist/adapters/llm-adapter.js.map +1 -1
- package/dist/adapters/llm-provider-bridge.d.ts.map +1 -1
- package/dist/adapters/llm-provider-bridge.js +10 -12
- package/dist/adapters/llm-provider-bridge.js.map +1 -1
- package/dist/agent/rag-helpers.d.ts +4 -6
- package/dist/agent/rag-helpers.d.ts.map +1 -1
- package/dist/agent/rag-helpers.js +3 -43
- package/dist/agent/rag-helpers.js.map +1 -1
- package/dist/agent/rag-orchestrator-types.d.ts +3 -0
- package/dist/agent/rag-orchestrator-types.d.ts.map +1 -1
- package/dist/agent/rag-orchestrator.d.ts +0 -1
- package/dist/agent/rag-orchestrator.d.ts.map +1 -1
- package/dist/agent/rag-orchestrator.js +9 -29
- package/dist/agent/rag-orchestrator.js.map +1 -1
- package/dist/agent.d.ts +11 -2
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +169 -236
- package/dist/agent.js.map +1 -1
- package/dist/builder-types.d.ts +1 -1
- package/dist/builder-types.d.ts.map +1 -1
- package/dist/builder.d.ts +7 -7
- package/dist/builder.d.ts.map +1 -1
- package/dist/builder.js +8 -17
- package/dist/builder.js.map +1 -1
- package/dist/config/config-watcher.d.ts +3 -0
- package/dist/config/config-watcher.d.ts.map +1 -1
- package/dist/config/config-watcher.js +1 -0
- package/dist/config/config-watcher.js.map +1 -1
- package/dist/context/context-assembler.d.ts.map +1 -1
- package/dist/context/context-assembler.js +23 -23
- package/dist/context/context-assembler.js.map +1 -1
- package/dist/coordinator/adaptive/compose-step-input.d.ts +1 -5
- package/dist/coordinator/adaptive/compose-step-input.d.ts.map +1 -1
- package/dist/coordinator/adaptive/compose-step-input.js +6 -17
- package/dist/coordinator/adaptive/compose-step-input.js.map +1 -1
- package/dist/coordinator/adaptive/index.d.ts +1 -1
- package/dist/coordinator/adaptive/index.d.ts.map +1 -1
- package/dist/coordinator/adaptive/index.js +1 -1
- package/dist/coordinator/adaptive/index.js.map +1 -1
- package/dist/coordinator/adaptive/llm-adaptive-controller.d.ts.map +1 -1
- package/dist/coordinator/adaptive/llm-adaptive-controller.js +3 -9
- package/dist/coordinator/adaptive/llm-adaptive-controller.js.map +1 -1
- package/dist/coordinator/dag/dag-plan-interpreter.js +1 -1
- package/dist/coordinator/dag/dag-plan-interpreter.js.map +1 -1
- package/dist/coordinator/dialogue/dialogue-session.d.ts +0 -3
- package/dist/coordinator/dialogue/dialogue-session.d.ts.map +1 -1
- package/dist/coordinator/dialogue/dialogue-session.js +15 -59
- package/dist/coordinator/dialogue/dialogue-session.js.map +1 -1
- package/dist/coordinator/dialogue/index.d.ts +2 -2
- package/dist/coordinator/dialogue/index.d.ts.map +1 -1
- package/dist/coordinator/dialogue/index.js +2 -2
- package/dist/coordinator/dialogue/index.js.map +1 -1
- package/dist/coordinator/dialogue/planner-context-builder.d.ts +2 -3
- package/dist/coordinator/dialogue/planner-context-builder.d.ts.map +1 -1
- package/dist/coordinator/dialogue/planner-context-builder.js +13 -73
- package/dist/coordinator/dialogue/planner-context-builder.js.map +1 -1
- package/dist/coordinator/dialogue/planner-tools-client.d.ts.map +1 -1
- package/dist/coordinator/dialogue/planner-tools-client.js +7 -19
- package/dist/coordinator/dialogue/planner-tools-client.js.map +1 -1
- package/dist/coordinator/dialogue/structured-plan.d.ts +1 -1
- package/dist/coordinator/dialogue/structured-plan.d.ts.map +1 -1
- package/dist/coordinator/dialogue/structured-plan.js +6 -7
- package/dist/coordinator/dialogue/structured-plan.js.map +1 -1
- package/dist/coordinator/dialogue/submit-report-tool.d.ts +1 -1
- package/dist/coordinator/dialogue/submit-report-tool.d.ts.map +1 -1
- package/dist/coordinator/dialogue/submit-report-tool.js +4 -17
- package/dist/coordinator/dialogue/submit-report-tool.js.map +1 -1
- package/dist/coordinator/dialogue/task-board.d.ts +0 -1
- package/dist/coordinator/dialogue/task-board.d.ts.map +1 -1
- package/dist/coordinator/dialogue/task-board.js +6 -7
- package/dist/coordinator/dialogue/task-board.js.map +1 -1
- package/dist/coordinator/dialogue/task-reference.d.ts +1 -3
- package/dist/coordinator/dialogue/task-reference.d.ts.map +1 -1
- package/dist/coordinator/dialogue/task-reference.js +3 -10
- package/dist/coordinator/dialogue/task-reference.js.map +1 -1
- package/dist/coordinator/dialogue/tool-trace-render.d.ts.map +1 -1
- package/dist/coordinator/dialogue/tool-trace-render.js +9 -29
- package/dist/coordinator/dialogue/tool-trace-render.js.map +1 -1
- package/dist/coordinator/dispatch/self.d.ts.map +1 -1
- package/dist/coordinator/dispatch/self.js +15 -21
- package/dist/coordinator/dispatch/self.js.map +1 -1
- package/dist/coordinator/planning/replan-on-error.d.ts.map +1 -1
- package/dist/coordinator/planning/replan-on-error.js +1 -1
- package/dist/coordinator/planning/replan-on-error.js.map +1 -1
- package/dist/coordinator/stepper/cyclic-react-executor.d.ts.map +1 -1
- package/dist/coordinator/stepper/cyclic-react-executor.js +33 -33
- package/dist/coordinator/stepper/cyclic-react-executor.js.map +1 -1
- package/dist/coordinator/stepper/stepper-interpreter.d.ts.map +1 -1
- package/dist/coordinator/stepper/stepper-interpreter.js +3 -17
- package/dist/coordinator/stepper/stepper-interpreter.js.map +1 -1
- package/dist/history/history-memory.d.ts +1 -0
- package/dist/history/history-memory.d.ts.map +1 -1
- package/dist/history/history-memory.js +5 -1
- package/dist/history/history-memory.js.map +1 -1
- package/dist/history/history-summarizer.js +1 -1
- package/dist/history/history-summarizer.js.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/interfaces/pipeline.d.ts +1 -1
- package/dist/interfaces/pipeline.d.ts.map +1 -1
- package/dist/pipeline/context/tool-loop-context/window-context-strategy.d.ts +1 -1
- package/dist/pipeline/context/tool-loop-context/window-context-strategy.d.ts.map +1 -1
- package/dist/pipeline/context/tool-loop-context/window-context-strategy.js +6 -3
- package/dist/pipeline/context/tool-loop-context/window-context-strategy.js.map +1 -1
- package/dist/pipeline/context/tool-loop-control-history.d.ts +15 -0
- package/dist/pipeline/context/tool-loop-control-history.d.ts.map +1 -0
- package/dist/pipeline/context/tool-loop-control-history.js +72 -0
- package/dist/pipeline/context/tool-loop-control-history.js.map +1 -0
- package/dist/pipeline/context.d.ts +5 -2
- package/dist/pipeline/context.d.ts.map +1 -1
- package/dist/pipeline/default-pipeline.d.ts +2 -2
- package/dist/pipeline/default-pipeline.d.ts.map +1 -1
- package/dist/pipeline/default-pipeline.js +7 -7
- package/dist/pipeline/default-pipeline.js.map +1 -1
- package/dist/pipeline/executor.d.ts.map +1 -1
- package/dist/pipeline/executor.js +6 -0
- package/dist/pipeline/executor.js.map +1 -1
- package/dist/pipeline/handlers/adaptive-coordinator.d.ts.map +1 -1
- package/dist/pipeline/handlers/adaptive-coordinator.js +9 -6
- package/dist/pipeline/handlers/adaptive-coordinator.js.map +1 -1
- package/dist/pipeline/handlers/assemble.d.ts +0 -9
- package/dist/pipeline/handlers/assemble.d.ts.map +1 -1
- package/dist/pipeline/handlers/assemble.js +7 -11
- package/dist/pipeline/handlers/assemble.js.map +1 -1
- package/dist/pipeline/handlers/history-upsert.d.ts +2 -0
- package/dist/pipeline/handlers/history-upsert.d.ts.map +1 -1
- package/dist/pipeline/handlers/history-upsert.js +6 -1
- package/dist/pipeline/handlers/history-upsert.js.map +1 -1
- package/dist/pipeline/handlers/pass-through.d.ts +3 -1
- package/dist/pipeline/handlers/pass-through.d.ts.map +1 -1
- package/dist/pipeline/handlers/pass-through.js +72 -5
- package/dist/pipeline/handlers/pass-through.js.map +1 -1
- package/dist/pipeline/handlers/summarize.d.ts +2 -12
- package/dist/pipeline/handlers/summarize.d.ts.map +1 -1
- package/dist/pipeline/handlers/summarize.js +2 -54
- package/dist/pipeline/handlers/summarize.js.map +1 -1
- package/dist/pipeline/handlers/tool-loop-core.d.ts +15 -9
- package/dist/pipeline/handlers/tool-loop-core.d.ts.map +1 -1
- package/dist/pipeline/handlers/tool-loop-core.js +128 -59
- package/dist/pipeline/handlers/tool-loop-core.js.map +1 -1
- package/dist/pipeline/handlers/tool-loop.d.ts.map +1 -1
- package/dist/pipeline/handlers/tool-loop.js +112 -293
- package/dist/pipeline/handlers/tool-loop.js.map +1 -1
- package/dist/pipeline/handlers/tool-response-round.d.ts +30 -0
- package/dist/pipeline/handlers/tool-response-round.d.ts.map +1 -0
- package/dist/pipeline/handlers/tool-response-round.js +85 -0
- package/dist/pipeline/handlers/tool-response-round.js.map +1 -0
- package/dist/pipeline/handlers/tool-select.d.ts.map +1 -1
- package/dist/pipeline/handlers/tool-select.js +5 -1
- package/dist/pipeline/handlers/tool-select.js.map +1 -1
- package/dist/policy/caller-identity.d.ts +29 -0
- package/dist/policy/caller-identity.d.ts.map +1 -0
- package/dist/policy/caller-identity.js +79 -0
- package/dist/policy/caller-identity.js.map +1 -0
- package/dist/policy/mixed-tool-call-handler.d.ts +9 -3
- package/dist/policy/mixed-tool-call-handler.d.ts.map +1 -1
- package/dist/policy/mixed-tool-call-handler.js +29 -7
- package/dist/policy/mixed-tool-call-handler.js.map +1 -1
- package/dist/policy/pending-tool-results-registry.d.ts +46 -4
- package/dist/policy/pending-tool-results-registry.d.ts.map +1 -1
- package/dist/policy/pending-tool-results-registry.js +203 -18
- package/dist/policy/pending-tool-results-registry.js.map +1 -1
- package/dist/policy/tool-availability-registry.d.ts +6 -5
- package/dist/policy/tool-availability-registry.d.ts.map +1 -1
- package/dist/policy/tool-availability-registry.js +13 -2
- package/dist/policy/tool-availability-registry.js.map +1 -1
- package/dist/subagent/direct-llm-subagent.js +1 -1
- package/dist/subagent/direct-llm-subagent.js.map +1 -1
- package/dist/subagent/smart-agent-subagent.d.ts.map +1 -1
- package/dist/subagent/smart-agent-subagent.js +5 -5
- package/dist/subagent/smart-agent-subagent.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import { addCacheTokenUsage,
|
|
1
|
+
import { addCacheTokenUsage, toModelToolMessages, } from '@mcp-abap-adt/llm-agent';
|
|
2
2
|
import { normalizeLoopLimit } from '../../config/normalize-max-iterations.js';
|
|
3
3
|
import { withSubmitReport } from '../../coordinator/dialogue/submit-report-tool.js';
|
|
4
4
|
import { denyRagAccess, ownerBoundQuery } from '../../rag/owner-bound-query.js';
|
|
5
|
-
import {
|
|
5
|
+
import { ToolResponseRound } from './tool-response-round.js';
|
|
6
|
+
import { buildNamespacedTools, defaultToolNamespace, mergeOfferedTools, scopedUsage, toolNameFromRecord, toToolCallDelta, } from '@mcp-abap-adt/llm-agent';
|
|
6
7
|
import { OrchestratorError } from '../../agent.js';
|
|
8
|
+
import { resolveCallerIdentity } from '../../policy/caller-identity.js';
|
|
7
9
|
import { fireInternalToolsAsync } from '../../policy/mixed-tool-call-handler.js';
|
|
8
10
|
import { LegacyAccumulateContextStrategy } from '../context/tool-loop-context/index.js';
|
|
11
|
+
import { ToolLoopControlHistory } from '../context/tool-loop-control-history.js';
|
|
9
12
|
import { normalizeHeartbeatMs } from './normalize-heartbeat-ms.js';
|
|
10
|
-
import {
|
|
13
|
+
import { classifyToolCalls, executeToolBatchWithHeartbeat, filterAvailableTools, guardToolRound, injectPendingResults, injectToolPriority, pendingSourceMessages, readTaskFinish, runOutputValidationReprompt, } from './tool-loop-core.js';
|
|
11
14
|
import { wrapFinalResponse } from './wrap-final-response.js';
|
|
12
15
|
function summarizeIterationMessages(messages) {
|
|
13
16
|
const totalChars = messages.reduce((sum, msg) => {
|
|
@@ -71,6 +74,7 @@ function describeCause(error) {
|
|
|
71
74
|
}
|
|
72
75
|
export class ToolLoopHandler {
|
|
73
76
|
async execute(ctx, config, parentSpan) {
|
|
77
|
+
const identity = ctx.callerResolution ?? resolveCallerIdentity(ctx.options, ctx.sessionId);
|
|
74
78
|
const maxIterations = normalizeLoopLimit('maxIterations' in config
|
|
75
79
|
? config.maxIterations
|
|
76
80
|
: ctx.config.maxIterations, 'maxIterations');
|
|
@@ -103,29 +107,19 @@ export class ToolLoopHandler {
|
|
|
103
107
|
const timingLog = [];
|
|
104
108
|
const loopStart = Date.now();
|
|
105
109
|
let currentTools = ctx.activeTools;
|
|
110
|
+
const localBlocks = new Set();
|
|
106
111
|
messages = injectToolPriority(messages, externalTools);
|
|
107
112
|
// staticPrefix is immutable across all iterations — re-emitted every round.
|
|
108
|
-
const staticPrefix = messages;
|
|
113
|
+
const staticPrefix = await injectPendingResults(messages, ctx.pendingToolResults, identity, ctx.options, ctx.externalResults, ctx.textOrMessages);
|
|
109
114
|
const strategy = (ctx.toolLoopContextStrategyFactory ??
|
|
110
115
|
(() => new LegacyAccumulateContextStrategy()))({ run: undefined });
|
|
111
|
-
//
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const withPending = await injectPendingResults(messages, ctx.pendingToolResults, ctx.sessionId, ctx.options);
|
|
115
|
-
if (withPending.length > staticPrefix.length) {
|
|
116
|
-
// Injected pending results form an assistant+tool group → record as round.
|
|
117
|
-
const injected = withPending.slice(staticPrefix.length);
|
|
118
|
-
const pendingRound = {
|
|
119
|
-
assistant: injected[0],
|
|
120
|
-
results: injected.slice(1),
|
|
121
|
-
};
|
|
122
|
-
await strategy.record(pendingRound);
|
|
123
|
-
}
|
|
124
|
-
messages = (await strategy.form({
|
|
116
|
+
// Preserve validation exchanges between their original tool rounds.
|
|
117
|
+
const controlHistory = new ToolLoopControlHistory(staticPrefix.length);
|
|
118
|
+
messages = controlHistory.restore(await strategy.form({
|
|
125
119
|
prefix: staticPrefix,
|
|
126
120
|
toolTrust: ctx.config.toolTrust,
|
|
127
121
|
queryText: ctx.inputText,
|
|
128
|
-
}))
|
|
122
|
+
}));
|
|
129
123
|
const finalize = (content, finishReason) => {
|
|
130
124
|
ctx.assistantText = content;
|
|
131
125
|
const summary = ctx.requestLogger.getSummary();
|
|
@@ -355,7 +349,7 @@ export class ToolLoopHandler {
|
|
|
355
349
|
}
|
|
356
350
|
currentTools = await withSubmitReport(currentTools, ctx.toolClientMap, ctx.options, externalTools);
|
|
357
351
|
// Filter tools per iteration
|
|
358
|
-
currentTools = filterAvailableTools(ctx.toolAvailabilityRegistry,
|
|
352
|
+
currentTools = filterAvailableTools(ctx.toolAvailabilityRegistry, identity, currentTools, iteration, ctx.options, localBlocks);
|
|
359
353
|
if (maxToolCalls !== undefined && toolCallCount >= maxToolCalls) {
|
|
360
354
|
currentTools = [];
|
|
361
355
|
ctx.yield({
|
|
@@ -408,7 +402,6 @@ export class ToolLoopHandler {
|
|
|
408
402
|
const toolCallsMap = new Map();
|
|
409
403
|
// Track which streaming indices belong to external tools so that
|
|
410
404
|
// argument-only continuation deltas (no name field) are forwarded too.
|
|
411
|
-
const externalToolIndices = new Set();
|
|
412
405
|
const available = ctx.mcpTools.length + externalTools.length;
|
|
413
406
|
if (currentTools.length !== lastToolsSelected ||
|
|
414
407
|
available !== lastToolsAvailable) {
|
|
@@ -479,7 +472,6 @@ export class ToolLoopHandler {
|
|
|
479
472
|
ctx.onPartial?.({ kind: 'iteration-end', outcome: 'retry' });
|
|
480
473
|
content = '';
|
|
481
474
|
toolCallsMap.clear();
|
|
482
|
-
externalToolIndices.clear();
|
|
483
475
|
finishReason = undefined;
|
|
484
476
|
iterPromptTokens = 0;
|
|
485
477
|
iterCompletionTokens = 0;
|
|
@@ -502,18 +494,7 @@ export class ToolLoopHandler {
|
|
|
502
494
|
}
|
|
503
495
|
}
|
|
504
496
|
if (chunk.toolCalls) {
|
|
505
|
-
//
|
|
506
|
-
// tool-call deltas are BUFFERED (accumulated into toolCallsMap) and
|
|
507
|
-
// NOT yielded live: an external call is surfaced only AFTER the stream
|
|
508
|
-
// settles — and only if it has no resume result (miss). This avoids
|
|
509
|
-
// leaking a call we may instead resolve from `ctx.externalResults`.
|
|
510
|
-
for (const tc of chunk.toolCalls) {
|
|
511
|
-
const name = getStreamToolCallName(tc);
|
|
512
|
-
if (name && externalToolNames.has(name)) {
|
|
513
|
-
const delta = toToolCallDelta(tc, 0);
|
|
514
|
-
externalToolIndices.add(delta.index);
|
|
515
|
-
}
|
|
516
|
-
}
|
|
497
|
+
// Buffer calls until the complete response can be classified atomically.
|
|
517
498
|
for (const [fallbackIndex, rawToolCall,] of chunk.toolCalls.entries()) {
|
|
518
499
|
const tc = toToolCallDelta(rawToolCall, fallbackIndex);
|
|
519
500
|
if (!toolCallsMap.has(tc.index)) {
|
|
@@ -576,21 +557,14 @@ export class ToolLoopHandler {
|
|
|
576
557
|
durationMs: llmCallDuration,
|
|
577
558
|
requestId: ctx.options?.trace?.traceId,
|
|
578
559
|
});
|
|
579
|
-
const
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
args = JSON.parse(tc.arguments);
|
|
583
|
-
}
|
|
584
|
-
catch {
|
|
585
|
-
args = {};
|
|
586
|
-
}
|
|
587
|
-
return { id: tc.id, name: tc.name, arguments: args };
|
|
588
|
-
});
|
|
560
|
+
const hasToolCalls = finishReason === 'tool_calls' && toolCallsMap.size > 0;
|
|
561
|
+
const responseRound = new ToolResponseRound(content, toolCallsMap.values(), (name) => externalToolNames.has(name) && !ctx.toolClientMap.has(name));
|
|
562
|
+
const toolCalls = hasToolCalls ? responseRound.executableCalls() : [];
|
|
589
563
|
ctx.options?.sessionLogger?.logStep(`llm_response_iter_${iteration + 1}`, { content, toolCalls, finishReason }, 'llm');
|
|
590
|
-
const val =
|
|
564
|
+
const val = hasToolCalls
|
|
591
565
|
? { reprompt: false, messages }
|
|
592
566
|
: await runOutputValidationReprompt(ctx.outputValidator, content, messages, currentTools, ctx.options, ctx.config.toolTrust);
|
|
593
|
-
const iterationEnd =
|
|
567
|
+
const iterationEnd = hasToolCalls
|
|
594
568
|
? 'tool_calls'
|
|
595
569
|
: val.reprompt
|
|
596
570
|
? 'retry'
|
|
@@ -607,262 +581,117 @@ export class ToolLoopHandler {
|
|
|
607
581
|
ctx.yield({ ok: true, value: { content: '', iterationEnd } });
|
|
608
582
|
ctx.onPartial?.({ kind: 'iteration-end', outcome: iterationEnd });
|
|
609
583
|
// -- No tool calls: validate and finish --------------------------------
|
|
610
|
-
if (
|
|
584
|
+
if (!hasToolCalls) {
|
|
611
585
|
if (val.reprompt) {
|
|
612
|
-
//
|
|
613
|
-
// into the strategy — they are ephemeral and survive only until the
|
|
614
|
-
// next recorded tool round, at which point controlTail is pruned.
|
|
586
|
+
// Anchor the rejected reply and correction before any newer tool rounds.
|
|
615
587
|
const appended = val.messages.slice(messages.length);
|
|
616
|
-
|
|
617
|
-
messages = (await strategy.form({
|
|
588
|
+
controlHistory.append(appended);
|
|
589
|
+
messages = controlHistory.restore(await strategy.form({
|
|
618
590
|
prefix: staticPrefix,
|
|
619
591
|
toolTrust: ctx.config.toolTrust,
|
|
620
592
|
queryText: ctx.inputText,
|
|
621
|
-
}))
|
|
593
|
+
}));
|
|
622
594
|
continue;
|
|
623
595
|
}
|
|
624
596
|
return finalize(content, finishReason);
|
|
625
597
|
}
|
|
626
598
|
// -- Classify tool calls -----------------------------------------------
|
|
627
599
|
const roundRejections = guardToolRound(toolCalls, ctx.options) ?? new Map();
|
|
628
|
-
const { internalCalls, validExternalCalls, blockedCalls, hallucinations, } = classifyToolCalls(toolCalls, ctx.toolClientMap, externalToolNames, ctx.toolAvailabilityRegistry,
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
const rejectedExternal = validExternalCalls.filter((tc) => roundRejections.has(toolCalls.indexOf(tc)));
|
|
632
|
-
if (rejectedExternal.length > 0) {
|
|
633
|
-
await strategy.record({
|
|
634
|
-
assistant: {
|
|
635
|
-
role: 'assistant',
|
|
636
|
-
content: content || null,
|
|
637
|
-
tool_calls: rejectedExternal.map((tc) => ({
|
|
638
|
-
id: tc.id,
|
|
639
|
-
type: 'function',
|
|
640
|
-
function: {
|
|
641
|
-
name: tc.name,
|
|
642
|
-
arguments: JSON.stringify(tc.arguments),
|
|
643
|
-
},
|
|
644
|
-
})),
|
|
645
|
-
},
|
|
646
|
-
results: rejectedExternal.map((tc) => ({
|
|
647
|
-
role: 'tool',
|
|
648
|
-
tool_call_id: tc.id,
|
|
649
|
-
content: `Error: ${roundRejections.get(toolCalls.indexOf(tc))}`,
|
|
650
|
-
})),
|
|
651
|
-
meta: rejectedExternal.map(() => ({ isError: true })),
|
|
652
|
-
});
|
|
653
|
-
for (const tc of rejectedExternal) {
|
|
654
|
-
validExternalCalls.splice(validExternalCalls.indexOf(tc), 1);
|
|
655
|
-
}
|
|
656
|
-
controlTail.length = 0;
|
|
657
|
-
messages = (await strategy.form({
|
|
658
|
-
prefix: staticPrefix,
|
|
659
|
-
toolTrust: ctx.config.toolTrust,
|
|
660
|
-
queryText: ctx.inputText,
|
|
661
|
-
})).concat(controlTail);
|
|
662
|
-
if (validExternalCalls.length === 0 &&
|
|
663
|
-
internalCalls.length === 0 &&
|
|
664
|
-
blockedCalls.length === 0 &&
|
|
665
|
-
hallucinations.length === 0)
|
|
666
|
-
continue;
|
|
600
|
+
const { internalCalls, validExternalCalls, blockedCalls, hallucinations, } = classifyToolCalls(toolCalls, ctx.toolClientMap, externalToolNames, ctx.toolAvailabilityRegistry, identity, localBlocks);
|
|
601
|
+
for (const tc of blockedCalls) {
|
|
602
|
+
responseRound.resolve(tc, `Error: Tool "${tc.name}" is temporarily unavailable in this session.`, true);
|
|
667
603
|
}
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
await strategy.record({
|
|
671
|
-
assistant: group.assistant,
|
|
672
|
-
results: group.results,
|
|
673
|
-
});
|
|
674
|
-
controlTail.length = 0;
|
|
675
|
-
messages = (await strategy.form({
|
|
676
|
-
prefix: staticPrefix,
|
|
677
|
-
toolTrust: ctx.config.toolTrust,
|
|
678
|
-
queryText: ctx.inputText,
|
|
679
|
-
})).concat(controlTail);
|
|
680
|
-
continue;
|
|
604
|
+
for (const tc of hallucinations) {
|
|
605
|
+
responseRound.resolve(tc, `Error: Tool "${tc.name}" not found.`, true);
|
|
681
606
|
}
|
|
607
|
+
const externalCalls = validExternalCalls.filter((tc) => {
|
|
608
|
+
const rejection = roundRejections.get(toolCalls.indexOf(tc));
|
|
609
|
+
if (rejection === undefined)
|
|
610
|
+
return true;
|
|
611
|
+
responseRound.resolve(tc, `Error: ${rejection}`, true);
|
|
612
|
+
return false;
|
|
613
|
+
});
|
|
682
614
|
// Check tool call budget
|
|
683
615
|
const remaining = maxToolCalls !== undefined
|
|
684
616
|
? maxToolCalls - toolCallCount
|
|
685
617
|
: internalCalls.length;
|
|
686
618
|
const batch = internalCalls.slice(0, Math.max(0, remaining));
|
|
687
619
|
const skipped = internalCalls.slice(batch.length);
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
},
|
|
704
|
-
results: unknownCalls.map((tc) => ({
|
|
705
|
-
role: 'tool',
|
|
706
|
-
tool_call_id: tc.id,
|
|
707
|
-
content: `Error: Tool "${tc.name}" not found.`,
|
|
708
|
-
})),
|
|
709
|
-
meta: unknownCalls.map(() => ({ isError: true })),
|
|
620
|
+
if (externalCalls.length > 0) {
|
|
621
|
+
const pending = fireInternalToolsAsync(content, batch, ctx.pendingToolResults, identity, {
|
|
622
|
+
toolClientMap: ctx.toolClientMap,
|
|
623
|
+
toolCache: ctx.toolCache,
|
|
624
|
+
metrics: ctx.metrics,
|
|
625
|
+
options: ctx.options,
|
|
626
|
+
roundCalls: toolCalls,
|
|
627
|
+
roundRejections,
|
|
628
|
+
skippedCalls: skipped,
|
|
629
|
+
maxToolCalls: maxToolCalls,
|
|
630
|
+
assistantMessage: responseRound.assistant,
|
|
631
|
+
resolvedResults: responseRound.resolved(),
|
|
632
|
+
precedingMessages: messages,
|
|
633
|
+
sourceMessages: pendingSourceMessages(ctx.textOrMessages, staticPrefix, messages),
|
|
634
|
+
externalCalls,
|
|
710
635
|
});
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
queryText: ctx.inputText,
|
|
716
|
-
})).concat(controlTail);
|
|
717
|
-
}
|
|
718
|
-
// -- Handle external (client-provided) tool calls (spec D1) ------------
|
|
719
|
-
// Each external call is rewritten to a deterministic content-addressed id
|
|
720
|
-
// (`externalToolCallId`). Two paths:
|
|
721
|
-
// HIT — `ctx.externalResults` already has the result (stateless resume
|
|
722
|
-
// from the client's prior round-trip): inject a MATCHED
|
|
723
|
-
// assistant(tool_calls=[extId]) -> tool(tool_call_id=extId) pair
|
|
724
|
-
// and CONTINUE the loop (no re-surface).
|
|
725
|
-
// MISS — no result yet: surface the call (with extId) to the consumer
|
|
726
|
-
// and END the worker turn (`finish_reason: tool_calls`); never
|
|
727
|
-
// execute it server-side.
|
|
728
|
-
if (validExternalCalls.length > 0) {
|
|
729
|
-
const externalWithId = validExternalCalls.map((tc) => ({
|
|
730
|
-
...tc,
|
|
731
|
-
extId: externalToolCallId(tc.name, tc.arguments),
|
|
732
|
-
}));
|
|
733
|
-
const hits = externalWithId.filter((tc) => ctx.externalResults?.has(tc.extId));
|
|
734
|
-
const misses = externalWithId.filter((tc) => !ctx.externalResults?.has(tc.extId));
|
|
735
|
-
// HIT path: inject matched pairs (assistant tool_call + tool result),
|
|
736
|
-
// each correlated by the SAME extId, and resolve them in-conversation.
|
|
737
|
-
if (hits.length > 0) {
|
|
738
|
-
markExternalTraceUnavailable(ctx.options?.onToolTrace);
|
|
739
|
-
const hitAssistant = {
|
|
740
|
-
role: 'assistant',
|
|
741
|
-
content: null,
|
|
742
|
-
tool_calls: hits.map((tc) => ({
|
|
743
|
-
id: tc.extId,
|
|
744
|
-
type: 'function',
|
|
745
|
-
function: {
|
|
746
|
-
name: tc.name,
|
|
747
|
-
arguments: JSON.stringify(tc.arguments),
|
|
748
|
-
},
|
|
749
|
-
})),
|
|
750
|
-
};
|
|
751
|
-
const hitResults = hits.map((tc) => ({
|
|
752
|
-
role: 'tool',
|
|
753
|
-
content: ctx.externalResults?.get(tc.extId) ?? '',
|
|
754
|
-
tool_call_id: tc.extId,
|
|
755
|
-
}));
|
|
756
|
-
await strategy.record({
|
|
757
|
-
assistant: hitAssistant,
|
|
758
|
-
results: hitResults,
|
|
759
|
-
});
|
|
760
|
-
controlTail.length = 0;
|
|
761
|
-
messages = (await strategy.form({
|
|
762
|
-
prefix: staticPrefix,
|
|
763
|
-
toolTrust: ctx.config.toolTrust,
|
|
764
|
-
queryText: ctx.inputText,
|
|
765
|
-
})).concat(controlTail);
|
|
766
|
-
ctx.options?.sessionLogger?.logStep('external_results_resumed', {
|
|
767
|
-
extIds: hits.map((tc) => tc.extId),
|
|
768
|
-
toolNames: hits.map((tc) => tc.name),
|
|
769
|
-
});
|
|
770
|
-
}
|
|
771
|
-
// MISS path: surface the unresolved external call(s) and end the turn.
|
|
772
|
-
if (misses.length > 0) {
|
|
773
|
-
if (internalCalls.length > 0) {
|
|
774
|
-
fireInternalToolsAsync(content, batch, ctx.pendingToolResults, ctx.sessionId, {
|
|
775
|
-
toolClientMap: ctx.toolClientMap,
|
|
776
|
-
toolCache: ctx.toolCache,
|
|
777
|
-
metrics: ctx.metrics,
|
|
778
|
-
options: ctx.options,
|
|
779
|
-
roundCalls: toolCalls,
|
|
780
|
-
roundRejections,
|
|
781
|
-
skippedCalls: skipped,
|
|
782
|
-
maxToolCalls: maxToolCalls,
|
|
783
|
-
});
|
|
784
|
-
toolCallCount += batch.length;
|
|
785
|
-
if (batch.length > 0) {
|
|
786
|
-
try {
|
|
787
|
-
ctx.options?.onToolCallCount?.(toolCallCount, maxToolCalls);
|
|
788
|
-
}
|
|
789
|
-
catch {
|
|
790
|
-
/* Observers cannot affect execution. */
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
if (maxToolCalls !== undefined && toolCallCount >= maxToolCalls) {
|
|
794
|
-
ctx.yield({
|
|
795
|
-
ok: true,
|
|
796
|
-
value: {
|
|
797
|
-
content: '',
|
|
798
|
-
budgetExhausted: true,
|
|
799
|
-
toolCallCount,
|
|
800
|
-
maxToolCalls: maxToolCalls,
|
|
801
|
-
},
|
|
802
|
-
});
|
|
803
|
-
}
|
|
804
|
-
ctx.options?.sessionLogger?.logStep('mixed_tool_calls', {
|
|
805
|
-
internal: internalCalls.map((tc) => tc.name),
|
|
806
|
-
external: misses.map((tc) => tc.name),
|
|
807
|
-
});
|
|
636
|
+
toolCallCount += batch.length;
|
|
637
|
+
if (batch.length > 0) {
|
|
638
|
+
try {
|
|
639
|
+
ctx.options?.onToolCallCount?.(toolCallCount, maxToolCalls);
|
|
808
640
|
}
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
value: {
|
|
815
|
-
content: '',
|
|
816
|
-
toolCalls: misses.map((tc, index) => ({
|
|
817
|
-
index,
|
|
818
|
-
id: tc.extId,
|
|
819
|
-
name: tc.name,
|
|
820
|
-
arguments: JSON.stringify(tc.arguments),
|
|
821
|
-
})),
|
|
822
|
-
},
|
|
823
|
-
});
|
|
824
|
-
timingLog.push({ phase: 'total', duration: Date.now() - loopStart });
|
|
825
|
-
ctx.timing.push(...timingLog);
|
|
641
|
+
catch {
|
|
642
|
+
/* Observers cannot affect execution. */
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
if (maxToolCalls !== undefined && toolCallCount >= maxToolCalls) {
|
|
826
646
|
ctx.yield({
|
|
827
647
|
ok: true,
|
|
828
648
|
value: {
|
|
829
649
|
content: '',
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
models: ctx.requestLogger.getSummary().byModel,
|
|
834
|
-
components: ctx.requestLogger.getSummary().byComponent,
|
|
835
|
-
},
|
|
836
|
-
timing: timingLog,
|
|
650
|
+
budgetExhausted: true,
|
|
651
|
+
toolCallCount,
|
|
652
|
+
maxToolCalls: maxToolCalls,
|
|
837
653
|
},
|
|
838
654
|
});
|
|
839
|
-
return true;
|
|
840
|
-
}
|
|
841
|
-
// Only hits (no misses): if there are no internal calls to execute,
|
|
842
|
-
// continue the loop directly so the worker consumes the injected
|
|
843
|
-
// results. With internal calls present, fall through to execute them.
|
|
844
|
-
if (internalCalls.length === 0) {
|
|
845
|
-
continue;
|
|
846
655
|
}
|
|
656
|
+
ctx.options?.sessionLogger?.logStep('mixed_tool_calls', {
|
|
657
|
+
internal: internalCalls.map((tc) => tc.name),
|
|
658
|
+
external: externalCalls.map((tc) => tc.name),
|
|
659
|
+
});
|
|
660
|
+
// Return this suspension's unique IDs; keep model IDs internal.
|
|
661
|
+
emitProgressFallback();
|
|
662
|
+
ctx.yield({
|
|
663
|
+
ok: true,
|
|
664
|
+
value: {
|
|
665
|
+
content: '',
|
|
666
|
+
toolCalls: externalCalls.map((tc, index) => ({
|
|
667
|
+
index,
|
|
668
|
+
id: pending.externalCalls[index].returnedId,
|
|
669
|
+
name: tc.name,
|
|
670
|
+
arguments: JSON.stringify(tc.arguments),
|
|
671
|
+
})),
|
|
672
|
+
},
|
|
673
|
+
});
|
|
674
|
+
timingLog.push({ phase: 'total', duration: Date.now() - loopStart });
|
|
675
|
+
ctx.timing.push(...timingLog);
|
|
676
|
+
ctx.yield({
|
|
677
|
+
ok: true,
|
|
678
|
+
value: {
|
|
679
|
+
content: '',
|
|
680
|
+
finishReason: 'tool_calls',
|
|
681
|
+
usage: {
|
|
682
|
+
...usage,
|
|
683
|
+
models: ctx.requestLogger.getSummary().byModel,
|
|
684
|
+
components: ctx.requestLogger.getSummary().byComponent,
|
|
685
|
+
},
|
|
686
|
+
timing: timingLog,
|
|
687
|
+
},
|
|
688
|
+
});
|
|
689
|
+
return true;
|
|
847
690
|
}
|
|
848
691
|
// -- Execute internal MCP tool calls -----------------------------------
|
|
849
692
|
// Capture the assistant message now; record the full round (assistant +
|
|
850
693
|
// tool results) atomically after the batch completes so the strategy
|
|
851
694
|
// always sees a complete ToolRound.
|
|
852
|
-
const batchAssistantMsg = {
|
|
853
|
-
role: 'assistant',
|
|
854
|
-
content: content || null,
|
|
855
|
-
tool_calls: toolCalls
|
|
856
|
-
.filter((tc) => internalCalls.includes(tc) || hallucinations.includes(tc))
|
|
857
|
-
.map((tc) => ({
|
|
858
|
-
id: tc.id,
|
|
859
|
-
type: 'function',
|
|
860
|
-
function: {
|
|
861
|
-
name: tc.name,
|
|
862
|
-
arguments: JSON.stringify(tc.arguments),
|
|
863
|
-
},
|
|
864
|
-
})),
|
|
865
|
-
};
|
|
866
695
|
// Yield progress messages
|
|
867
696
|
for (const tc of batch) {
|
|
868
697
|
ctx.yield({
|
|
@@ -875,6 +704,8 @@ export class ToolLoopHandler {
|
|
|
875
704
|
}
|
|
876
705
|
// Execute tool calls concurrently with heartbeat (shared core).
|
|
877
706
|
const batchGen = executeToolBatchWithHeartbeat({
|
|
707
|
+
callerResolution: identity,
|
|
708
|
+
localBlocks,
|
|
878
709
|
batch,
|
|
879
710
|
roundCalls: toolCalls,
|
|
880
711
|
roundRejections,
|
|
@@ -891,6 +722,16 @@ export class ToolLoopHandler {
|
|
|
891
722
|
timingLog,
|
|
892
723
|
heartbeatMs,
|
|
893
724
|
options: ctx.options,
|
|
725
|
+
onBatchComplete: async (outcome) => {
|
|
726
|
+
outcome.toolMessages.forEach((message, index) => {
|
|
727
|
+
responseRound.add(message, outcome.resultMeta[index]);
|
|
728
|
+
});
|
|
729
|
+
for (const tc of skipped) {
|
|
730
|
+
responseRound.resolve(tc, `Error: not executed: tool-call budget of ${maxToolCalls} for this request is exhausted; return what you have and report what is missing`, true);
|
|
731
|
+
}
|
|
732
|
+
const batchRound = responseRound.finish();
|
|
733
|
+
await strategy.record(controlHistory.record(batchRound));
|
|
734
|
+
},
|
|
894
735
|
mcpFailureClassifier: ctx.mcpFailureClassifier,
|
|
895
736
|
maxToolCalls,
|
|
896
737
|
onToolExecuted: (r) => {
|
|
@@ -956,33 +797,11 @@ export class ToolLoopHandler {
|
|
|
956
797
|
},
|
|
957
798
|
});
|
|
958
799
|
}
|
|
959
|
-
|
|
960
|
-
assistant: batchAssistantMsg,
|
|
961
|
-
results: [
|
|
962
|
-
...outcome.toolMessages,
|
|
963
|
-
...hallucinations.map((tc) => ({
|
|
964
|
-
role: 'tool',
|
|
965
|
-
tool_call_id: tc.id,
|
|
966
|
-
content: `Error: Tool "${tc.name}" not found.`,
|
|
967
|
-
})),
|
|
968
|
-
...skipped.map((tc) => ({
|
|
969
|
-
role: 'tool',
|
|
970
|
-
tool_call_id: tc.id,
|
|
971
|
-
content: `Error: not executed: tool-call budget of ${maxToolCalls} for this request is exhausted; return what you have and report what is missing`,
|
|
972
|
-
})),
|
|
973
|
-
],
|
|
974
|
-
meta: [
|
|
975
|
-
...outcome.resultMeta,
|
|
976
|
-
...[...hallucinations, ...skipped].map(() => ({ isError: true })),
|
|
977
|
-
],
|
|
978
|
-
};
|
|
979
|
-
await strategy.record(batchRound);
|
|
980
|
-
controlTail.length = 0;
|
|
981
|
-
messages = (await strategy.form({
|
|
800
|
+
messages = controlHistory.restore(await strategy.form({
|
|
982
801
|
prefix: staticPrefix,
|
|
983
802
|
toolTrust: ctx.config.toolTrust,
|
|
984
803
|
queryText: ctx.inputText,
|
|
985
|
-
}))
|
|
804
|
+
}));
|
|
986
805
|
}
|
|
987
806
|
}
|
|
988
807
|
}
|