@librechat/agents 4.1.0 → 4.2.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/dist/cjs/agents/AgentContext.cjs +9 -1
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/constants.cjs +2 -0
- package/dist/cjs/common/constants.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +15 -12
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +1 -1
- package/dist/cjs/hitl/approvalReview.cjs +10 -2
- package/dist/cjs/hitl/approvalReview.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/defaults.cjs +12 -0
- package/dist/cjs/llm/anthropic/defaults.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/index.cjs +6 -0
- package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +1 -1
- package/dist/cjs/llm/google/utils/common.cjs +1 -1
- package/dist/cjs/llm/init.cjs +5 -2
- package/dist/cjs/llm/init.cjs.map +1 -1
- package/dist/cjs/llm/invoke.cjs +17 -15
- package/dist/cjs/llm/invoke.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +1 -1
- package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
- package/dist/cjs/llm/request.cjs +13 -0
- package/dist/cjs/llm/request.cjs.map +1 -1
- package/dist/cjs/llm/stream/chunkAdapters.cjs +2 -3
- package/dist/cjs/llm/stream/chunkAdapters.cjs.map +1 -1
- package/dist/cjs/llm/truncation.cjs +12 -2
- package/dist/cjs/llm/truncation.cjs.map +1 -1
- package/dist/cjs/main.cjs +7 -4
- package/dist/cjs/messages/core.cjs +1 -1
- package/dist/cjs/messages/format.cjs +1 -1
- package/dist/cjs/messages/index.cjs +1 -1
- package/dist/cjs/messages/prune.cjs +2 -2
- package/dist/cjs/messages/reasoningTypes.cjs +20 -0
- package/dist/cjs/messages/reasoningTypes.cjs.map +1 -1
- package/dist/cjs/messages/recency.cjs +68 -7
- package/dist/cjs/messages/recency.cjs.map +1 -1
- package/dist/cjs/run.cjs +135 -54
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/session/AgentSession.cjs +7 -10
- package/dist/cjs/session/AgentSession.cjs.map +1 -1
- package/dist/cjs/stream.cjs +3 -2
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/summarization/input.cjs +87 -0
- package/dist/cjs/summarization/input.cjs.map +1 -0
- package/dist/cjs/summarization/node.cjs +470 -135
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/summarization/retry.cjs +172 -0
- package/dist/cjs/summarization/retry.cjs.map +1 -0
- package/dist/cjs/summarization/shared.cjs +6 -2
- package/dist/cjs/summarization/shared.cjs.map +1 -1
- package/dist/cjs/summarization/toolBudget.cjs +45 -0
- package/dist/cjs/summarization/toolBudget.cjs.map +1 -0
- package/dist/cjs/summarization/usage.cjs +70 -0
- package/dist/cjs/summarization/usage.cjs.map +1 -0
- package/dist/cjs/tools/ToolNode.cjs +46 -16
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/cloudflare/CloudflareBridgeRuntime.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +155 -18
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentReplay.cjs +6 -1
- package/dist/cjs/tools/subagent/SubagentReplay.cjs.map +1 -1
- package/dist/cjs/tools/subagent/childGraphConfig.cjs +2 -1
- package/dist/cjs/tools/subagent/childGraphConfig.cjs.map +1 -1
- package/dist/cjs/tools/toolBatchReplay.cjs +182 -6
- package/dist/cjs/tools/toolBatchReplay.cjs.map +1 -1
- package/dist/cjs/utils/errors.cjs +21 -0
- package/dist/cjs/utils/errors.cjs.map +1 -1
- package/dist/cjs/utils/index.cjs +1 -1
- package/dist/cjs/utils/schema.cjs +17 -0
- package/dist/cjs/utils/schema.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +9 -1
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/constants.mjs +2 -1
- package/dist/esm/common/constants.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +16 -13
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +1 -1
- package/dist/esm/hitl/approvalReview.mjs +10 -2
- package/dist/esm/hitl/approvalReview.mjs.map +1 -1
- package/dist/esm/llm/anthropic/defaults.mjs +12 -0
- package/dist/esm/llm/anthropic/defaults.mjs.map +1 -0
- package/dist/esm/llm/anthropic/index.mjs +6 -0
- package/dist/esm/llm/anthropic/index.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +1 -1
- package/dist/esm/llm/google/utils/common.mjs +1 -1
- package/dist/esm/llm/init.mjs +5 -2
- package/dist/esm/llm/init.mjs.map +1 -1
- package/dist/esm/llm/invoke.mjs +18 -16
- package/dist/esm/llm/invoke.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +1 -1
- package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
- package/dist/esm/llm/request.mjs +13 -1
- package/dist/esm/llm/request.mjs.map +1 -1
- package/dist/esm/llm/stream/chunkAdapters.mjs +2 -3
- package/dist/esm/llm/stream/chunkAdapters.mjs.map +1 -1
- package/dist/esm/llm/truncation.mjs +12 -3
- package/dist/esm/llm/truncation.mjs.map +1 -1
- package/dist/esm/main.mjs +8 -8
- package/dist/esm/messages/core.mjs +1 -1
- package/dist/esm/messages/format.mjs +1 -1
- package/dist/esm/messages/index.mjs +1 -1
- package/dist/esm/messages/prune.mjs +2 -2
- package/dist/esm/messages/reasoningTypes.mjs +19 -1
- package/dist/esm/messages/reasoningTypes.mjs.map +1 -1
- package/dist/esm/messages/recency.mjs +68 -8
- package/dist/esm/messages/recency.mjs.map +1 -1
- package/dist/esm/run.mjs +135 -54
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/session/AgentSession.mjs +7 -10
- package/dist/esm/session/AgentSession.mjs.map +1 -1
- package/dist/esm/stream.mjs +3 -2
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/summarization/input.mjs +84 -0
- package/dist/esm/summarization/input.mjs.map +1 -0
- package/dist/esm/summarization/node.mjs +469 -133
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/summarization/retry.mjs +169 -0
- package/dist/esm/summarization/retry.mjs.map +1 -0
- package/dist/esm/summarization/shared.mjs +6 -2
- package/dist/esm/summarization/shared.mjs.map +1 -1
- package/dist/esm/summarization/toolBudget.mjs +45 -0
- package/dist/esm/summarization/toolBudget.mjs.map +1 -0
- package/dist/esm/summarization/usage.mjs +69 -0
- package/dist/esm/summarization/usage.mjs.map +1 -0
- package/dist/esm/tools/ToolNode.mjs +46 -16
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/cloudflare/CloudflareBridgeRuntime.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +156 -19
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentReplay.mjs +6 -1
- package/dist/esm/tools/subagent/SubagentReplay.mjs.map +1 -1
- package/dist/esm/tools/subagent/childGraphConfig.mjs +2 -1
- package/dist/esm/tools/subagent/childGraphConfig.mjs.map +1 -1
- package/dist/esm/tools/toolBatchReplay.mjs +178 -7
- package/dist/esm/tools/toolBatchReplay.mjs.map +1 -1
- package/dist/esm/utils/errors.mjs +21 -1
- package/dist/esm/utils/errors.mjs.map +1 -1
- package/dist/esm/utils/index.mjs +1 -1
- package/dist/esm/utils/schema.mjs +17 -0
- package/dist/esm/utils/schema.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +2 -0
- package/dist/types/common/constants.d.ts +2 -0
- package/dist/types/hitl/approvalReview.d.ts +1 -1
- package/dist/types/llm/anthropic/defaults.d.ts +11 -0
- package/dist/types/llm/init.d.ts +2 -1
- package/dist/types/llm/invoke.d.ts +8 -1
- package/dist/types/llm/request.d.ts +12 -0
- package/dist/types/llm/stream/chunkAdapters.d.ts +1 -1
- package/dist/types/llm/truncation.d.ts +2 -0
- package/dist/types/messages/reasoningTypes.d.ts +6 -0
- package/dist/types/messages/recency.d.ts +2 -0
- package/dist/types/run.d.ts +3 -0
- package/dist/types/summarization/input.d.ts +16 -0
- package/dist/types/summarization/node.d.ts +2 -1
- package/dist/types/summarization/retry.d.ts +22 -0
- package/dist/types/summarization/shared.d.ts +3 -3
- package/dist/types/summarization/toolBudget.d.ts +8 -0
- package/dist/types/summarization/usage.d.ts +9 -0
- package/dist/types/tools/subagent/SubagentReplay.d.ts +3 -1
- package/dist/types/tools/toolBatchReplay.d.ts +14 -1
- package/dist/types/types/graph.d.ts +2 -1
- package/dist/types/types/hitl.d.ts +2 -0
- package/dist/types/types/llm.d.ts +3 -0
- package/dist/types/types/run.d.ts +1 -1
- package/dist/types/types/summarize.d.ts +15 -3
- package/dist/types/utils/errors.d.ts +2 -0
- package/dist/types/utils/schema.d.ts +1 -1
- package/package.json +13 -4
- package/src/agents/AgentContext.ts +7 -0
- package/src/common/constants.ts +3 -0
- package/src/graphs/Graph.ts +16 -21
- package/src/hitl/approvalReview.ts +10 -2
- package/src/llm/anthropic/defaults.ts +22 -0
- package/src/llm/anthropic/index.ts +15 -2
- package/src/llm/init.ts +16 -2
- package/src/llm/invoke.ts +33 -14
- package/src/llm/openai/utils/index.ts +2 -4
- package/src/llm/request.ts +44 -0
- package/src/llm/stream/chunkAdapters.ts +14 -18
- package/src/llm/truncation.ts +33 -17
- package/src/messages/reasoningTypes.ts +71 -0
- package/src/messages/recency.ts +78 -0
- package/src/run.ts +190 -81
- package/src/session/AgentSession.ts +8 -11
- package/src/stream.ts +1 -0
- package/src/summarization/input.ts +98 -0
- package/src/summarization/node.ts +749 -356
- package/src/summarization/retry.ts +294 -0
- package/src/summarization/shared.ts +9 -2
- package/src/summarization/toolBudget.ts +87 -0
- package/src/summarization/usage.ts +117 -0
- package/src/tools/ToolNode.ts +84 -23
- package/src/tools/cloudflare/CloudflareBridgeRuntime.ts +2 -2
- package/src/tools/subagent/SubagentExecutor.ts +227 -32
- package/src/tools/subagent/SubagentReplay.ts +30 -1
- package/src/tools/subagent/childGraphConfig.ts +9 -2
- package/src/tools/toolBatchReplay.ts +237 -7
- package/src/types/graph.ts +2 -1
- package/src/types/hitl.ts +4 -0
- package/src/types/llm.ts +4 -0
- package/src/types/run.ts +6 -3
- package/src/types/summarize.ts +22 -3
- package/src/utils/errors.ts +45 -0
- package/src/utils/schema.ts +26 -2
package/src/run.ts
CHANGED
|
@@ -22,10 +22,21 @@ import type {
|
|
|
22
22
|
} from '@langchain/core/messages';
|
|
23
23
|
import type { StringPromptValue } from '@langchain/core/prompt_values';
|
|
24
24
|
import type { RunnableConfig } from '@langchain/core/runnables';
|
|
25
|
+
import type { SubagentResumeManifest } from '@/tools/subagent/SubagentReplay';
|
|
25
26
|
import type { AggregatedHookResult, HookRegistry } from '@/hooks';
|
|
26
27
|
import type { MultiAgentGraph } from '@/graphs/MultiAgentGraph';
|
|
27
28
|
import type { StandardGraph } from '@/graphs/Graph';
|
|
28
29
|
import type * as t from '@/types';
|
|
30
|
+
import {
|
|
31
|
+
TOOL_BATCH_REPLAY_KEY,
|
|
32
|
+
TOOL_REPLAY_CONFIGS_KEY,
|
|
33
|
+
TOOL_PENDING_APPROVAL_KEY,
|
|
34
|
+
restoreToolReplayConfig,
|
|
35
|
+
restoreToolReplayConfigs,
|
|
36
|
+
getToolBatchReplayScope,
|
|
37
|
+
stripToolBatchReplayState,
|
|
38
|
+
getPublicToolInterruptPayload,
|
|
39
|
+
} from '@/tools/toolBatchReplay';
|
|
29
40
|
import {
|
|
30
41
|
Callback,
|
|
31
42
|
GraphEvents,
|
|
@@ -64,12 +75,6 @@ import {
|
|
|
64
75
|
buildReasoningLabelPrompt,
|
|
65
76
|
normalizeReasoningLabel,
|
|
66
77
|
} from '@/prompts/reasoningLabel';
|
|
67
|
-
import {
|
|
68
|
-
TOOL_BATCH_REPLAY_KEY,
|
|
69
|
-
restoreToolReplayConfig,
|
|
70
|
-
stripToolBatchReplayState,
|
|
71
|
-
getPublicToolInterruptPayload,
|
|
72
|
-
} from '@/tools/toolBatchReplay';
|
|
73
78
|
import {
|
|
74
79
|
hasToolOutputTracingConfig,
|
|
75
80
|
resolveLangfuseConfig,
|
|
@@ -268,12 +273,17 @@ function getToolCompletion(
|
|
|
268
273
|
|
|
269
274
|
function isLangGraphResumeMapForInterrupt(
|
|
270
275
|
value: unknown,
|
|
271
|
-
interruptId: string
|
|
276
|
+
interruptId: string,
|
|
277
|
+
pendingIds: ReadonlySet<string>
|
|
272
278
|
): value is Record<string, unknown> {
|
|
273
279
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
274
280
|
return false;
|
|
275
281
|
}
|
|
276
|
-
|
|
282
|
+
if (Object.prototype.hasOwnProperty.call(value, interruptId)) return true;
|
|
283
|
+
for (const id of pendingIds) {
|
|
284
|
+
if (Object.prototype.hasOwnProperty.call(value, id)) return true;
|
|
285
|
+
}
|
|
286
|
+
return false;
|
|
277
287
|
}
|
|
278
288
|
|
|
279
289
|
function getInterruptHookSessionId(payload: unknown): string | undefined {
|
|
@@ -294,12 +304,14 @@ function getInterruptHookSessionId(payload: unknown): string | undefined {
|
|
|
294
304
|
|
|
295
305
|
type InterruptStateSnapshot = {
|
|
296
306
|
config?: RunnableConfig;
|
|
307
|
+
next?: string[];
|
|
297
308
|
values?: {
|
|
298
309
|
handoffState?: t.HandoffState;
|
|
299
310
|
messages?: BaseMessage[];
|
|
300
311
|
runStepState?: t.RunStepResumeState;
|
|
301
312
|
};
|
|
302
313
|
tasks?: Array<{
|
|
314
|
+
name?: string;
|
|
303
315
|
interrupts?: Array<{ id?: string; value?: unknown }>;
|
|
304
316
|
state?: RunnableConfig | InterruptStateSnapshot;
|
|
305
317
|
}>;
|
|
@@ -386,29 +398,28 @@ async function resolveCompletedSegmentConfig(
|
|
|
386
398
|
|
|
387
399
|
function getFirstPersistedInterrupt(
|
|
388
400
|
snapshot: InterruptStateSnapshot
|
|
389
|
-
): { id: string; value: unknown } | undefined {
|
|
401
|
+
): { id: string; value: unknown; pending: Array<{ id: string; value: unknown }> } | undefined {
|
|
402
|
+
const pending: Array<{ id: string; value: unknown }> = [];
|
|
403
|
+
const nestedStates: InterruptStateSnapshot[] = [];
|
|
404
|
+
const next = snapshot.next == null ? undefined : new Set(snapshot.next);
|
|
390
405
|
for (const task of snapshot.tasks ?? []) {
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
) {
|
|
396
|
-
return { id: pendingInterrupt.id, value: pendingInterrupt.value };
|
|
406
|
+
if (next != null && (task.name == null || !next.has(task.name))) continue;
|
|
407
|
+
for (const entry of task.interrupts ?? []) {
|
|
408
|
+
if (typeof entry.id === 'string' && entry.id.length > 0) {
|
|
409
|
+
pending.push({ id: entry.id, value: entry.value });
|
|
397
410
|
}
|
|
398
411
|
}
|
|
399
412
|
const nestedState = task.state;
|
|
400
|
-
if (
|
|
401
|
-
nestedState
|
|
402
|
-
'tasks' in nestedState &&
|
|
403
|
-
Array.isArray(nestedState.tasks)
|
|
404
|
-
) {
|
|
405
|
-
const nestedInterrupt = getFirstPersistedInterrupt(nestedState);
|
|
406
|
-
if (nestedInterrupt != null) {
|
|
407
|
-
return nestedInterrupt;
|
|
408
|
-
}
|
|
413
|
+
if (nestedState != null && 'tasks' in nestedState && Array.isArray(nestedState.tasks)) {
|
|
414
|
+
nestedStates.push(nestedState);
|
|
409
415
|
}
|
|
410
416
|
}
|
|
411
|
-
|
|
417
|
+
for (const nestedState of nestedStates) {
|
|
418
|
+
const nested = getFirstPersistedInterrupt(nestedState);
|
|
419
|
+
if (nested != null) pending.push(...nested.pending);
|
|
420
|
+
}
|
|
421
|
+
const unique = [...new Map(pending.map((entry) => [entry.id, entry])).values()];
|
|
422
|
+
return unique.length === 0 ? undefined : { ...unique[0], pending: unique };
|
|
412
423
|
}
|
|
413
424
|
|
|
414
425
|
function getPersistedMessages(
|
|
@@ -434,11 +445,19 @@ function overwriteResumeRunStepState(
|
|
|
434
445
|
['runStepState', overwrite] as [string, unknown],
|
|
435
446
|
]
|
|
436
447
|
: { ...(command.update ?? {}), runStepState: overwrite };
|
|
448
|
+
return copyResumeCommand(command, { update });
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function copyResumeCommand(
|
|
452
|
+
command: Command,
|
|
453
|
+
overrides: Pick<ConstructorParameters<typeof Command>[0], 'resume' | 'update'>
|
|
454
|
+
): Command {
|
|
437
455
|
const resumed = new Command({
|
|
438
456
|
...(command.graph == null ? {} : { graph: command.graph }),
|
|
439
457
|
...(command.resume === undefined ? {} : { resume: command.resume }),
|
|
440
|
-
update,
|
|
458
|
+
...(command.update === undefined ? {} : { update: command.update }),
|
|
441
459
|
...(command.goto === undefined ? {} : { goto: command.goto }),
|
|
460
|
+
...overrides,
|
|
442
461
|
});
|
|
443
462
|
resumed.lc_direct_tool_output = command.lc_direct_tool_output;
|
|
444
463
|
return resumed;
|
|
@@ -1307,30 +1326,41 @@ export class Run<_T extends t.BaseGraphState> {
|
|
|
1307
1326
|
(callerConfig.recursionLimit ?? DEFAULT_RECURSION_LIMIT) +
|
|
1308
1327
|
(this.preemption != null ? resolveMaxSeals(this.preemption.maxSeals) : 0);
|
|
1309
1328
|
|
|
1310
|
-
|
|
1329
|
+
let config: t.RunStreamConfig = {
|
|
1311
1330
|
...callerConfig,
|
|
1312
1331
|
recursionLimit,
|
|
1313
1332
|
configurable: { ...callerConfig.configurable },
|
|
1314
1333
|
};
|
|
1315
1334
|
delete config.configurable?.[TOOL_APPROVAL_REVIEW_CONFIG_KEY];
|
|
1316
1335
|
delete config.configurable?.[TOOL_BATCH_REPLAY_KEY];
|
|
1336
|
+
delete config.configurable?.[TOOL_REPLAY_CONFIGS_KEY];
|
|
1337
|
+
delete config.configurable?.[TOOL_PENDING_APPROVAL_KEY];
|
|
1317
1338
|
if (!isResume) {
|
|
1318
1339
|
delete config.configurable?.[SUBAGENT_RESUME_ATTEMPT_CONFIG_KEY];
|
|
1319
1340
|
delete config.configurable?.[SUBAGENT_RESUME_MANIFEST_CONFIG_KEY];
|
|
1320
1341
|
}
|
|
1321
1342
|
let overwriteLegacyResumeState = false;
|
|
1322
1343
|
if (isResume) {
|
|
1323
|
-
await this.
|
|
1344
|
+
config = await this.resolveInterruptResumeConfig(
|
|
1324
1345
|
config,
|
|
1325
|
-
(inputs as Command).update
|
|
1346
|
+
(inputs as Command).update,
|
|
1347
|
+
(inputs as Command).resume
|
|
1326
1348
|
);
|
|
1327
|
-
const
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1349
|
+
const interruptId = this._interrupt?.interruptId;
|
|
1350
|
+
const command = inputs as Command;
|
|
1351
|
+
if (
|
|
1352
|
+
command.resume !== undefined &&
|
|
1353
|
+
typeof interruptId === 'string' &&
|
|
1354
|
+
interruptId.length > 0 &&
|
|
1355
|
+
!isLangGraphResumeMapForInterrupt(
|
|
1356
|
+
command.resume,
|
|
1357
|
+
interruptId,
|
|
1358
|
+
this.getPendingInterruptState().ids
|
|
1359
|
+
)
|
|
1360
|
+
) {
|
|
1361
|
+
inputs = copyResumeCommand(command, {
|
|
1362
|
+
resume: { [interruptId]: command.resume },
|
|
1363
|
+
});
|
|
1334
1364
|
}
|
|
1335
1365
|
if (graph.getStopContinuationExecutionId() === '') {
|
|
1336
1366
|
graph.startStopContinuationExecution(nanoid());
|
|
@@ -1570,17 +1600,25 @@ export class Run<_T extends t.BaseGraphState> {
|
|
|
1570
1600
|
}
|
|
1571
1601
|
|
|
1572
1602
|
if (
|
|
1573
|
-
this._interrupt == null &&
|
|
1574
1603
|
data.chunk != null &&
|
|
1575
1604
|
isInterrupted<unknown>(data.chunk)
|
|
1576
1605
|
) {
|
|
1577
1606
|
const interrupts = data.chunk[INTERRUPT];
|
|
1578
1607
|
if (interrupts.length > 0) {
|
|
1579
|
-
const
|
|
1608
|
+
const pending = new Map(
|
|
1609
|
+
(this._interrupt?.pendingInterrupts ?? []).map((entry) => [entry.interruptId, entry])
|
|
1610
|
+
);
|
|
1611
|
+
for (const entry of interrupts) pending.set(entry.id ?? '', {
|
|
1612
|
+
interruptId: entry.id ?? '', payload: entry.value,
|
|
1613
|
+
});
|
|
1614
|
+
const first = this._interrupt ?? {
|
|
1615
|
+
interruptId: interrupts[0].id ?? '',
|
|
1616
|
+
payload: cloneToolApprovalInterruptPayload(interrupts[0].value),
|
|
1617
|
+
};
|
|
1580
1618
|
this._interrupt = {
|
|
1581
|
-
|
|
1619
|
+
...first,
|
|
1582
1620
|
threadId,
|
|
1583
|
-
|
|
1621
|
+
pendingInterrupts: [...pending.values()],
|
|
1584
1622
|
};
|
|
1585
1623
|
}
|
|
1586
1624
|
}
|
|
@@ -1933,6 +1971,38 @@ export class Run<_T extends t.BaseGraphState> {
|
|
|
1933
1971
|
return this._streamResult;
|
|
1934
1972
|
}
|
|
1935
1973
|
|
|
1974
|
+
private getPendingInterruptState(): {
|
|
1975
|
+
pending: NonNullable<t.RunInterruptResult<unknown>['pendingInterrupts']>;
|
|
1976
|
+
ids: Set<string>;
|
|
1977
|
+
} {
|
|
1978
|
+
const roots = this._interrupt?.pendingInterrupts ??
|
|
1979
|
+
(this._interrupt == null ? [] : [this._interrupt]);
|
|
1980
|
+
const pending = new Map<
|
|
1981
|
+
string, Pick<t.RunInterruptResult<unknown>, 'interruptId' | 'payload'>
|
|
1982
|
+
>();
|
|
1983
|
+
const ids = new Set<string>();
|
|
1984
|
+
const executions: SubagentResumeManifest['executions'] = [];
|
|
1985
|
+
for (const root of roots) {
|
|
1986
|
+
pending.set(root.interruptId, root);
|
|
1987
|
+
ids.add(root.interruptId);
|
|
1988
|
+
const payload = stripRunStepResumeState(root.payload);
|
|
1989
|
+
const manifest =
|
|
1990
|
+
requireValidSubagentResumeManifest(payload) ??
|
|
1991
|
+
requireValidSubagentResumeManifest(stripToolBatchReplayState(payload));
|
|
1992
|
+
executions.push(...(manifest?.executions ?? []));
|
|
1993
|
+
}
|
|
1994
|
+
for (let index = 0; index < executions.length; index++) {
|
|
1995
|
+
const execution = executions[index];
|
|
1996
|
+
for (const id of execution.pendingInterruptIds ?? []) ids.add(id);
|
|
1997
|
+
for (const entry of execution.pendingInterrupts ?? []) {
|
|
1998
|
+
ids.add(entry.interruptId);
|
|
1999
|
+
pending.set(entry.interruptId, entry);
|
|
2000
|
+
}
|
|
2001
|
+
executions.push(...(execution.descendant?.executions ?? []));
|
|
2002
|
+
}
|
|
2003
|
+
return { pending: [...pending.values()], ids };
|
|
2004
|
+
}
|
|
2005
|
+
|
|
1936
2006
|
/**
|
|
1937
2007
|
* Returns the pending interrupt captured during the most recent
|
|
1938
2008
|
* `processStream` (or `resume`) invocation. `undefined` when the run
|
|
@@ -1960,11 +2030,16 @@ export class Run<_T extends t.BaseGraphState> {
|
|
|
1960
2030
|
if (this._interrupt == null) {
|
|
1961
2031
|
return undefined;
|
|
1962
2032
|
}
|
|
2033
|
+
const { pending } = this.getPendingInterruptState();
|
|
1963
2034
|
return {
|
|
1964
2035
|
...this._interrupt,
|
|
1965
2036
|
payload: cloneToolApprovalInterruptPayload(
|
|
1966
2037
|
getPublicToolInterruptPayload(this._interrupt.payload)
|
|
1967
2038
|
),
|
|
2039
|
+
pendingInterrupts: pending.map((pending) => ({
|
|
2040
|
+
interruptId: pending.interruptId,
|
|
2041
|
+
payload: cloneToolApprovalInterruptPayload(getPublicToolInterruptPayload(pending.payload)),
|
|
2042
|
+
})),
|
|
1968
2043
|
} as t.RunInterruptResult<TPayload>;
|
|
1969
2044
|
}
|
|
1970
2045
|
|
|
@@ -2076,22 +2151,15 @@ export class Run<_T extends t.BaseGraphState> {
|
|
|
2076
2151
|
'update' | 'goto'
|
|
2077
2152
|
>
|
|
2078
2153
|
): Promise<MessageContentComplex[] | undefined> {
|
|
2079
|
-
|
|
2154
|
+
await this.restoreInterruptFromCheckpoint(
|
|
2080
2155
|
callerConfig,
|
|
2081
2156
|
commandOptions?.update
|
|
2082
2157
|
);
|
|
2083
|
-
const interruptId = this._interrupt?.interruptId;
|
|
2084
|
-
const scopedResume =
|
|
2085
|
-
typeof interruptId === 'string' &&
|
|
2086
|
-
interruptId.length > 0 &&
|
|
2087
|
-
!isLangGraphResumeMapForInterrupt(resumeValue, interruptId)
|
|
2088
|
-
? { [interruptId]: resumeValue }
|
|
2089
|
-
: resumeValue;
|
|
2090
2158
|
// langgraph 1.4.5 applies resume + state update + reroute in one superstep
|
|
2091
2159
|
// (single checkpoint). `update`/`goto` are omitted unless the caller sets them.
|
|
2092
2160
|
return this.processStream(
|
|
2093
2161
|
new Command({
|
|
2094
|
-
resume:
|
|
2162
|
+
resume: resumeValue,
|
|
2095
2163
|
...(commandOptions?.update !== undefined
|
|
2096
2164
|
? { update: commandOptions.update }
|
|
2097
2165
|
: {}),
|
|
@@ -2099,14 +2167,68 @@ export class Run<_T extends t.BaseGraphState> {
|
|
|
2099
2167
|
? { goto: commandOptions.goto }
|
|
2100
2168
|
: {}),
|
|
2101
2169
|
}),
|
|
2102
|
-
|
|
2170
|
+
callerConfig,
|
|
2103
2171
|
streamOptions
|
|
2104
2172
|
);
|
|
2105
2173
|
}
|
|
2106
2174
|
|
|
2175
|
+
private restoreRootReplayConfig(
|
|
2176
|
+
configurable: NonNullable<RunnableConfig['configurable']>,
|
|
2177
|
+
resumeValue?: unknown
|
|
2178
|
+
): void {
|
|
2179
|
+
const interrupt = this._interrupt;
|
|
2180
|
+
const pending = interrupt?.pendingInterrupts;
|
|
2181
|
+
if (pending == null || pending.length <= 1) {
|
|
2182
|
+
restoreToolReplayConfig(
|
|
2183
|
+
configurable,
|
|
2184
|
+
interrupt?.interruptId,
|
|
2185
|
+
stripRunStepResumeState(interrupt?.payload)
|
|
2186
|
+
);
|
|
2187
|
+
return;
|
|
2188
|
+
}
|
|
2189
|
+
const addressed = new Set(pending.flatMap((entry) =>
|
|
2190
|
+
resumeValue != null && typeof resumeValue === 'object' &&
|
|
2191
|
+
Object.prototype.hasOwnProperty.call(resumeValue, entry.interruptId)
|
|
2192
|
+
? [entry.interruptId] : []
|
|
2193
|
+
));
|
|
2194
|
+
if (addressed.size === 0 && interrupt != null) addressed.add(interrupt.interruptId);
|
|
2195
|
+
const scope = getToolBatchReplayScope({ configurable }) ?? '';
|
|
2196
|
+
const thread = typeof configurable.thread_id === 'string' ? configurable.thread_id : '';
|
|
2197
|
+
restoreToolReplayConfigs(
|
|
2198
|
+
configurable,
|
|
2199
|
+
pending.map((entry) => ({ id: entry.interruptId, value: entry.payload })),
|
|
2200
|
+
scope,
|
|
2201
|
+
scope,
|
|
2202
|
+
thread,
|
|
2203
|
+
addressed
|
|
2204
|
+
);
|
|
2205
|
+
}
|
|
2206
|
+
|
|
2207
|
+
private selectInterruptCheckpoint(
|
|
2208
|
+
config: t.RunStreamConfig,
|
|
2209
|
+
checkpointId: string,
|
|
2210
|
+
checkpointNs?: string
|
|
2211
|
+
): t.RunStreamConfig {
|
|
2212
|
+
const configurable: NonNullable<RunnableConfig['configurable']> = {
|
|
2213
|
+
...config.configurable,
|
|
2214
|
+
checkpoint_id: checkpointId,
|
|
2215
|
+
...(checkpointNs == null ? {} : { checkpoint_ns: checkpointNs }),
|
|
2216
|
+
};
|
|
2217
|
+
if ((this.Graph?.agentContexts.size ?? 0) > 1) {
|
|
2218
|
+
/** A pinned checkpoint_id replays completed Pregel tasks; the map selects it without replay. */
|
|
2219
|
+
delete configurable.checkpoint_id;
|
|
2220
|
+
configurable.checkpoint_map = {
|
|
2221
|
+
...configurable.checkpoint_map,
|
|
2222
|
+
[checkpointNs ?? '']: checkpointId,
|
|
2223
|
+
};
|
|
2224
|
+
}
|
|
2225
|
+
return { ...config, configurable };
|
|
2226
|
+
}
|
|
2227
|
+
|
|
2107
2228
|
private async resolveInterruptResumeConfig(
|
|
2108
2229
|
callerConfig: t.RunStreamConfig,
|
|
2109
|
-
resumeUpdate?: ResumeCommandUpdate
|
|
2230
|
+
resumeUpdate?: ResumeCommandUpdate,
|
|
2231
|
+
resumeValue?: unknown
|
|
2110
2232
|
): Promise<t.RunStreamConfig> {
|
|
2111
2233
|
await this.restoreInterruptFromCheckpoint(callerConfig, resumeUpdate);
|
|
2112
2234
|
const interrupt = this._interrupt;
|
|
@@ -2119,42 +2241,32 @@ export class Run<_T extends t.BaseGraphState> {
|
|
|
2119
2241
|
const resumeConfigurable = { ...callerConfig.configurable };
|
|
2120
2242
|
delete resumeConfigurable[TOOL_APPROVAL_REVIEW_CONFIG_KEY];
|
|
2121
2243
|
delete resumeConfigurable[TOOL_BATCH_REPLAY_KEY];
|
|
2244
|
+
delete resumeConfigurable[TOOL_REPLAY_CONFIGS_KEY];
|
|
2245
|
+
delete resumeConfigurable[TOOL_PENDING_APPROVAL_KEY];
|
|
2122
2246
|
delete resumeConfigurable[SUBAGENT_RESUME_ATTEMPT_CONFIG_KEY];
|
|
2123
2247
|
delete resumeConfigurable[SUBAGENT_RESUME_MANIFEST_CONFIG_KEY];
|
|
2124
2248
|
resumeConfigurable[SUBAGENT_RESUME_ATTEMPT_CONFIG_KEY] = nanoid();
|
|
2125
2249
|
if (resumeManifest != null) {
|
|
2126
2250
|
resumeConfigurable[SUBAGENT_RESUME_MANIFEST_CONFIG_KEY] = resumeManifest;
|
|
2127
2251
|
}
|
|
2128
|
-
|
|
2129
|
-
restoreToolReplayConfig(
|
|
2130
|
-
resumeConfigurable,
|
|
2131
|
-
interrupt?.interruptId,
|
|
2132
|
-
publicPayload
|
|
2133
|
-
);
|
|
2252
|
+
this.restoreRootReplayConfig(resumeConfigurable, resumeValue);
|
|
2134
2253
|
const manifestConfig = {
|
|
2135
2254
|
...callerConfig,
|
|
2136
2255
|
configurable: resumeConfigurable,
|
|
2137
2256
|
};
|
|
2138
|
-
const
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2257
|
+
const hookSessions = new Set(
|
|
2258
|
+
(interrupt?.pendingInterrupts ?? (interrupt == null ? [] : [interrupt]))
|
|
2259
|
+
.map((entry) => getInterruptHookSessionId(entry.payload))
|
|
2260
|
+
.filter((id): id is string => id != null)
|
|
2261
|
+
);
|
|
2262
|
+
for (const hookSessionId of hookSessions) this.hookRegistry?.copySession(hookSessionId, this.id);
|
|
2142
2263
|
const interruptId = interrupt?.interruptId;
|
|
2143
2264
|
const workflow = this.graphRunnable as
|
|
2144
2265
|
| (t.CompiledStateWorkflow & WorkflowWithStateHistory)
|
|
2145
2266
|
| undefined;
|
|
2146
2267
|
const stateHistory = workflow?.getStateHistory;
|
|
2147
2268
|
if (interrupt?.checkpointId != null && interrupt.checkpointId.length > 0) {
|
|
2148
|
-
return
|
|
2149
|
-
...manifestConfig,
|
|
2150
|
-
configurable: {
|
|
2151
|
-
...manifestConfig.configurable,
|
|
2152
|
-
checkpoint_id: interrupt.checkpointId,
|
|
2153
|
-
...(typeof interrupt.checkpointNs === 'string'
|
|
2154
|
-
? { checkpoint_ns: interrupt.checkpointNs }
|
|
2155
|
-
: {}),
|
|
2156
|
-
},
|
|
2157
|
-
};
|
|
2269
|
+
return this.selectInterruptCheckpoint(manifestConfig, interrupt.checkpointId, interrupt.checkpointNs);
|
|
2158
2270
|
}
|
|
2159
2271
|
if (
|
|
2160
2272
|
interrupt == null ||
|
|
@@ -2189,16 +2301,12 @@ export class Run<_T extends t.BaseGraphState> {
|
|
|
2189
2301
|
checkpointId,
|
|
2190
2302
|
...(typeof checkpointNs === 'string' ? { checkpointNs } : {}),
|
|
2191
2303
|
};
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
? { checkpoint_ns: checkpointNs }
|
|
2199
|
-
: {}),
|
|
2200
|
-
},
|
|
2201
|
-
};
|
|
2304
|
+
this.restoreRootReplayConfig(resumeConfigurable, resumeValue);
|
|
2305
|
+
return this.selectInterruptCheckpoint(
|
|
2306
|
+
manifestConfig,
|
|
2307
|
+
checkpointId,
|
|
2308
|
+
typeof checkpointNs === 'string' ? checkpointNs : undefined
|
|
2309
|
+
);
|
|
2202
2310
|
}
|
|
2203
2311
|
}
|
|
2204
2312
|
|
|
@@ -2249,6 +2357,7 @@ export class Run<_T extends t.BaseGraphState> {
|
|
|
2249
2357
|
this._interrupt = {
|
|
2250
2358
|
interruptId: persistedInterrupt.id,
|
|
2251
2359
|
payload: cloneToolApprovalInterruptPayload(persistedInterrupt.value),
|
|
2360
|
+
pendingInterrupts: persistedInterrupt.pending.map((entry) => ({ interruptId: entry.id, payload: entry.value })),
|
|
2252
2361
|
...(typeof threadId === 'string' ? { threadId } : {}),
|
|
2253
2362
|
...(typeof checkpointId === 'string' ? { checkpointId } : {}),
|
|
2254
2363
|
...(typeof checkpointNs === 'string' ? { checkpointNs } : {}),
|
|
@@ -660,10 +660,12 @@ function createAgentInputFromGraphConfig(
|
|
|
660
660
|
...(instructions != null && instructions !== ''
|
|
661
661
|
? { prompt: instructions }
|
|
662
662
|
: {}),
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
663
|
+
...(retainRecentTurns != null ? {
|
|
664
|
+
retainRecent: {
|
|
665
|
+
...agent.summarizationConfig?.retainRecent,
|
|
666
|
+
turns: retainRecentTurns,
|
|
667
|
+
},
|
|
668
|
+
} : {}),
|
|
667
669
|
};
|
|
668
670
|
return {
|
|
669
671
|
...applyInitialSummaryToAgent(agent, initialSummary),
|
|
@@ -1767,13 +1769,8 @@ export class AgentSession {
|
|
|
1767
1769
|
throw error;
|
|
1768
1770
|
}
|
|
1769
1771
|
await graph.closeOpenSteps('completed');
|
|
1770
|
-
const
|
|
1771
|
-
|
|
1772
|
-
let summaryText = completedSummaryText;
|
|
1773
|
-
if (summaryText === '' && contextSummaryText != null) {
|
|
1774
|
-
summaryText = contextSummaryText;
|
|
1775
|
-
}
|
|
1776
|
-
if (summaryText === '') {
|
|
1772
|
+
const summaryText = getSummaryText(graph.completedSummary);
|
|
1773
|
+
if (summaryText === '' || summarizedState.messages == null) {
|
|
1777
1774
|
return undefined;
|
|
1778
1775
|
}
|
|
1779
1776
|
const retainedMessages = filterRemoveMessages(
|
package/src/stream.ts
CHANGED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { ToolMessage } from '@langchain/core/messages';
|
|
2
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
3
|
+
import type { ProviderName, TokenCounter } from '@/types';
|
|
4
|
+
import {
|
|
5
|
+
cloneToolMessageWithContent,
|
|
6
|
+
compactToolContent,
|
|
7
|
+
} from '@/utils/toolContent';
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_SUMMARY_OUTPUT_TOKENS = 4_096;
|
|
10
|
+
export const DEFAULT_RETAIN_RECENT_TOKENS = 16_000;
|
|
11
|
+
|
|
12
|
+
export class SummarizationInputTooLargeError extends Error {
|
|
13
|
+
constructor() {
|
|
14
|
+
super('Summarization context exceeds the available token budget');
|
|
15
|
+
this.name = 'SummarizationInputTooLargeError';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Compresses tool output, then omits only the tail retained verbatim after compaction. */
|
|
20
|
+
export function boundSummarizationInput({
|
|
21
|
+
messages,
|
|
22
|
+
tokenCounter,
|
|
23
|
+
budget,
|
|
24
|
+
requiredHeadLength = messages.length,
|
|
25
|
+
shrink = false,
|
|
26
|
+
}: {
|
|
27
|
+
messages: BaseMessage[];
|
|
28
|
+
tokenCounter: TokenCounter;
|
|
29
|
+
budget: number;
|
|
30
|
+
provider: ProviderName;
|
|
31
|
+
requiredHeadLength?: number;
|
|
32
|
+
shrink?: boolean;
|
|
33
|
+
}): BaseMessage[] {
|
|
34
|
+
const counts = new WeakMap<BaseMessage, number>();
|
|
35
|
+
const count = (message: BaseMessage): number => {
|
|
36
|
+
const cached = counts.get(message);
|
|
37
|
+
if (cached != null) return cached;
|
|
38
|
+
const tokens = tokenCounter(message);
|
|
39
|
+
counts.set(message, tokens);
|
|
40
|
+
return tokens;
|
|
41
|
+
};
|
|
42
|
+
if (!Number.isSafeInteger(requiredHeadLength) || requiredHeadLength < 0 || requiredHeadLength > messages.length) {
|
|
43
|
+
throw new SummarizationInputTooLargeError();
|
|
44
|
+
}
|
|
45
|
+
let total = 0;
|
|
46
|
+
for (const message of messages) total += count(message);
|
|
47
|
+
if (!shrink && total <= budget) return messages;
|
|
48
|
+
if (budget <= 0) throw new SummarizationInputTooLargeError();
|
|
49
|
+
|
|
50
|
+
const minimal = messages.map((message) => {
|
|
51
|
+
if (!(message instanceof ToolMessage)) return message;
|
|
52
|
+
const result = compactToolContent(message.content, 0);
|
|
53
|
+
if (!result.changed) return message;
|
|
54
|
+
const candidate = cloneToolMessageWithContent(message, result.content);
|
|
55
|
+
return count(candidate) < count(message) ? candidate : message;
|
|
56
|
+
});
|
|
57
|
+
let minimumTokens = 0;
|
|
58
|
+
let requiredTokens = 0;
|
|
59
|
+
let toolCount = 0;
|
|
60
|
+
let requiredToolCount = 0;
|
|
61
|
+
for (let i = 0; i < messages.length; i++) {
|
|
62
|
+
const tokens = count(minimal[i]);
|
|
63
|
+
minimumTokens += tokens;
|
|
64
|
+
if (messages[i] instanceof ToolMessage) toolCount++;
|
|
65
|
+
if (i < requiredHeadLength) {
|
|
66
|
+
requiredTokens += tokens;
|
|
67
|
+
if (messages[i] instanceof ToolMessage) requiredToolCount++;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const includesTail = minimumTokens <= budget;
|
|
71
|
+
if (!includesTail && requiredTokens > budget) throw new SummarizationInputTooLargeError();
|
|
72
|
+
const end = includesTail ? messages.length : requiredHeadLength;
|
|
73
|
+
const additionalTokens = budget - (includesTail ? minimumTokens : requiredTokens);
|
|
74
|
+
const maxChars = Math.max(0, Math.floor((additionalTokens * 2) / Math.max(1, includesTail ? toolCount : requiredToolCount)));
|
|
75
|
+
for (const scale of [1, 0.5, 0.25, 0.125, 0]) {
|
|
76
|
+
let tokens = 0;
|
|
77
|
+
const candidate: BaseMessage[] = [];
|
|
78
|
+
for (let i = 0; i < end; i++) {
|
|
79
|
+
const original = messages[i];
|
|
80
|
+
let message = original;
|
|
81
|
+
if (original instanceof ToolMessage) {
|
|
82
|
+
message = minimal[i];
|
|
83
|
+
if (scale > 0) {
|
|
84
|
+
const result = compactToolContent(original.content, Math.floor(maxChars * scale));
|
|
85
|
+
if (!result.changed) message = original;
|
|
86
|
+
else {
|
|
87
|
+
const compacted = cloneToolMessageWithContent(original, result.content);
|
|
88
|
+
message = count(compacted) < count(original) ? compacted : original;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
tokens += count(message);
|
|
93
|
+
candidate.push(message);
|
|
94
|
+
}
|
|
95
|
+
if (tokens <= budget) return candidate;
|
|
96
|
+
}
|
|
97
|
+
throw new SummarizationInputTooLargeError();
|
|
98
|
+
}
|