@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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@librechat/agents",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"reova": {
|
|
5
5
|
"enabled": true,
|
|
6
6
|
"endpoint": "https://telemetry.reo.dev/data"
|
|
@@ -215,6 +215,8 @@
|
|
|
215
215
|
"prepublishOnly": "npm run build",
|
|
216
216
|
"build": "tsdown && tsc -p tsconfig.build.json",
|
|
217
217
|
"build:dev": "tsdown",
|
|
218
|
+
"typecheck": "node scripts/typecheck.ts",
|
|
219
|
+
"test:typecheck": "node --test scripts/typecheck.test.ts",
|
|
218
220
|
"check:circular-deps": "node config/circular-deps.mjs",
|
|
219
221
|
"test:circular-deps": "node --test config/circular-deps.test.mjs",
|
|
220
222
|
"test:light-entries": "node --test config/light-entries.test.mjs",
|
|
@@ -310,11 +312,16 @@
|
|
|
310
312
|
"openai": "$openai"
|
|
311
313
|
},
|
|
312
314
|
"uuid": "$uuid",
|
|
315
|
+
"zod": "$zod",
|
|
313
316
|
"ajv": "6.14.0",
|
|
314
317
|
"@opentelemetry/core": "^2.9.0",
|
|
315
318
|
"js-yaml": "^4.3.1",
|
|
316
319
|
"minimatch": "^10.2.2",
|
|
317
|
-
"test-exclude": "^8.0.0"
|
|
320
|
+
"test-exclude": "^8.0.0",
|
|
321
|
+
"follow-redirects": "^1.16.2",
|
|
322
|
+
"ip-address": "^10.7.5",
|
|
323
|
+
"node-forge": "^1.4.2",
|
|
324
|
+
"undici": "^7.30.1"
|
|
318
325
|
},
|
|
319
326
|
"dependencies": {
|
|
320
327
|
"@anthropic-ai/sdk": "^0.115.0",
|
|
@@ -351,7 +358,9 @@
|
|
|
351
358
|
"openai": "^6.46.0",
|
|
352
359
|
"reova": "^0.4.1",
|
|
353
360
|
"socks-proxy-agent": "^8.0.5",
|
|
354
|
-
"uuid": "^11.1.1"
|
|
361
|
+
"uuid": "^11.1.1",
|
|
362
|
+
"zod": "4.6.7",
|
|
363
|
+
"zod-to-json-schema": "3.25.2"
|
|
355
364
|
},
|
|
356
365
|
"peerDependencies": {
|
|
357
366
|
"@anthropic-ai/sandbox-runtime": "^0.0.78"
|
|
@@ -384,7 +393,7 @@
|
|
|
384
393
|
"mongodb": "^6.21.0",
|
|
385
394
|
"mongodb-memory-server-core": "^11.2.0",
|
|
386
395
|
"prettier": "^3.6.2",
|
|
387
|
-
"ts-jest": "^29.4.
|
|
396
|
+
"ts-jest": "^29.4.15",
|
|
388
397
|
"tsc-alias": "^1.9.7",
|
|
389
398
|
"tsdown": "^0.22.2",
|
|
390
399
|
"tslib": "^2.6.3",
|
|
@@ -472,6 +472,11 @@ export class AgentContext {
|
|
|
472
472
|
* disturbing a `maxContextTokens` that no correction ever touched.
|
|
473
473
|
*/
|
|
474
474
|
private _preOverflowMaxContextTokens?: number;
|
|
475
|
+
private correctedContextBudgetMode?: t.ContextBudgetMode;
|
|
476
|
+
|
|
477
|
+
get summarizationBudgetMode(): t.ContextBudgetMode {
|
|
478
|
+
return this.correctedContextBudgetMode ?? this.summarizationConfig?.contextBudgetMode ?? 'input';
|
|
479
|
+
}
|
|
475
480
|
/**
|
|
476
481
|
* Prompt size, normalized into the local counter's uncalibrated units, at
|
|
477
482
|
* the last overflow correction. Keeping both measurements in the same units
|
|
@@ -1772,6 +1777,7 @@ export class AgentContext {
|
|
|
1772
1777
|
}
|
|
1773
1778
|
if (budgetTokens != null) {
|
|
1774
1779
|
this.maxContextTokens = budgetTokens;
|
|
1780
|
+
this.correctedContextBudgetMode = 'input';
|
|
1775
1781
|
}
|
|
1776
1782
|
this.pruneMessages = undefined;
|
|
1777
1783
|
this.clearProviderProjection(true);
|
|
@@ -1981,6 +1987,7 @@ export class AgentContext {
|
|
|
1981
1987
|
}
|
|
1982
1988
|
this.maxContextTokens = this._preOverflowMaxContextTokens;
|
|
1983
1989
|
this._preOverflowMaxContextTokens = undefined;
|
|
1990
|
+
this.correctedContextBudgetMode = undefined;
|
|
1984
1991
|
this._lastOverflowPromptTokens = undefined;
|
|
1985
1992
|
this._overflowRecoveryAttempts = 0;
|
|
1986
1993
|
}
|
package/src/common/constants.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
/** Host context preparation and trusted subagent execution identity are supported. */
|
|
2
2
|
export const SUBAGENT_CONTEXT_VERSION = 1;
|
|
3
3
|
|
|
4
|
+
/** Foreground graph-subagent approval and durable resume support. */
|
|
5
|
+
export const GRAPH_SUBAGENT_HITL_VERSION = 1;
|
|
6
|
+
|
|
4
7
|
/**
|
|
5
8
|
* Anthropic direct API tool schema overhead multiplier.
|
|
6
9
|
* Empirically calibrated against real MCP tool sets (29 tools).
|
package/src/graphs/Graph.ts
CHANGED
|
@@ -176,6 +176,10 @@ import {
|
|
|
176
176
|
createRemoveAllMessage,
|
|
177
177
|
messagesStateReducer,
|
|
178
178
|
} from '@/messages/reducer';
|
|
179
|
+
import {
|
|
180
|
+
isThinkingEnabled,
|
|
181
|
+
getConfiguredCompletionTokens,
|
|
182
|
+
} from '@/llm/request';
|
|
179
183
|
import { createToolHistoryPreparation } from '@/messages/toolHistoryProjection';
|
|
180
184
|
import { ToolNode as CustomToolNode, toolsCondition } from '@/tools/ToolNode';
|
|
181
185
|
import { shouldTraceToolNodeForLangfuse } from '@/langfuseToolOutputTracing';
|
|
@@ -200,7 +204,6 @@ import { AgentContext } from '@/agents/AgentContext';
|
|
|
200
204
|
import { createFakeStreamingLLM } from '@/llm/fake';
|
|
201
205
|
import { handoffStateAnnotation } from './handoff';
|
|
202
206
|
import { handleToolCalls } from '@/tools/handlers';
|
|
203
|
-
import { isThinkingEnabled } from '@/llm/request';
|
|
204
207
|
import { resolveMaxSeals } from '@/llm/preempt';
|
|
205
208
|
import { initializeModel } from '@/llm/init';
|
|
206
209
|
import { HandlerRegistry } from '@/events';
|
|
@@ -485,26 +488,6 @@ function clearCurrentDeltaStepMarkers({
|
|
|
485
488
|
}
|
|
486
489
|
}
|
|
487
490
|
|
|
488
|
-
/**
|
|
489
|
-
* The completion allowance the caller configured, under whichever key the
|
|
490
|
-
* provider's client uses. Providers count it against the same ceiling as the
|
|
491
|
-
* prompt, so overflow recovery has to reserve it when the error did not
|
|
492
|
-
* itemize the total.
|
|
493
|
-
*/
|
|
494
|
-
function getConfiguredCompletionTokens(
|
|
495
|
-
clientOptions: t.ClientOptions | undefined
|
|
496
|
-
): number | undefined {
|
|
497
|
-
const options = clientOptions as
|
|
498
|
-
| { maxTokens?: unknown; maxOutputTokens?: unknown }
|
|
499
|
-
| undefined;
|
|
500
|
-
for (const value of [options?.maxTokens, options?.maxOutputTokens]) {
|
|
501
|
-
if (typeof value === 'number' && Number.isFinite(value) && value > 0) {
|
|
502
|
-
return value;
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
return undefined;
|
|
506
|
-
}
|
|
507
|
-
|
|
508
491
|
/**
|
|
509
492
|
* Our own estimate of the prompt that was actually sent, derived from the
|
|
510
493
|
* pre-invoke usage snapshot. Used to corroborate ambiguous provider errors
|
|
@@ -3133,6 +3116,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
3133
3116
|
config,
|
|
3134
3117
|
originalToolContent,
|
|
3135
3118
|
estimatedPromptTokens,
|
|
3119
|
+
windowMessages,
|
|
3136
3120
|
}: {
|
|
3137
3121
|
recovery: OverflowRecoveryPlan;
|
|
3138
3122
|
agentContext: AgentContext;
|
|
@@ -3142,6 +3126,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
3142
3126
|
originalToolContent?: Map<number, string>;
|
|
3143
3127
|
/** Size of the rejected prompt, recorded to detect a correction that changed nothing. */
|
|
3144
3128
|
estimatedPromptTokens?: number;
|
|
3129
|
+
windowMessages: BaseMessage[];
|
|
3145
3130
|
}): Partial<t.AgentSubgraphState> {
|
|
3146
3131
|
const previousBudget = agentContext.maxContextTokens;
|
|
3147
3132
|
/**
|
|
@@ -3189,6 +3174,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
3189
3174
|
remainingContextTokens: 0,
|
|
3190
3175
|
agentId: agentId || agentContext.agentId,
|
|
3191
3176
|
reason: 'overflow',
|
|
3177
|
+
windowMessages,
|
|
3192
3178
|
allowSummarization,
|
|
3193
3179
|
},
|
|
3194
3180
|
};
|
|
@@ -3225,12 +3211,14 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
3225
3211
|
agentContext,
|
|
3226
3212
|
agentId,
|
|
3227
3213
|
messageCount,
|
|
3214
|
+
windowMessages,
|
|
3228
3215
|
contextUsage,
|
|
3229
3216
|
config,
|
|
3230
3217
|
}: {
|
|
3231
3218
|
agentContext: AgentContext;
|
|
3232
3219
|
agentId: string;
|
|
3233
3220
|
messageCount: number;
|
|
3221
|
+
windowMessages: BaseMessage[];
|
|
3234
3222
|
contextUsage: t.ContextUsageEvent | null;
|
|
3235
3223
|
config: RunnableConfig;
|
|
3236
3224
|
}): Promise<Partial<t.AgentSubgraphState> | null> {
|
|
@@ -3262,6 +3250,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
3262
3250
|
remainingContextTokens: contextUsage?.remainingContextTokens ?? 0,
|
|
3263
3251
|
agentId: agentId || agentContext.agentId,
|
|
3264
3252
|
reason: 'manual',
|
|
3253
|
+
windowMessages,
|
|
3265
3254
|
},
|
|
3266
3255
|
/** A checkpointed thread may still hold the previous compaction's
|
|
3267
3256
|
* summary; only the one this run produces may be reported. */
|
|
@@ -3484,6 +3473,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
3484
3473
|
agentContext,
|
|
3485
3474
|
agentId,
|
|
3486
3475
|
messageCount: messages.length,
|
|
3476
|
+
windowMessages: context,
|
|
3487
3477
|
contextUsage,
|
|
3488
3478
|
config,
|
|
3489
3479
|
});
|
|
@@ -3541,6 +3531,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
3541
3531
|
return {
|
|
3542
3532
|
summarizationRequest: {
|
|
3543
3533
|
remainingContextTokens: remainingContextTokens ?? 0,
|
|
3534
|
+
windowMessages: context,
|
|
3544
3535
|
agentId: agentId || agentContext.agentId,
|
|
3545
3536
|
},
|
|
3546
3537
|
};
|
|
@@ -3570,6 +3561,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
3570
3561
|
agentContext,
|
|
3571
3562
|
agentId,
|
|
3572
3563
|
messageCount: messages.length,
|
|
3564
|
+
windowMessages: messagesToUse,
|
|
3573
3565
|
contextUsage: null,
|
|
3574
3566
|
config,
|
|
3575
3567
|
});
|
|
@@ -4554,6 +4546,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
4554
4546
|
instructionTokens: agentContext.instructionTokens,
|
|
4555
4547
|
canSummarize: agentContext.summarizationEnabled === true,
|
|
4556
4548
|
configuredCompletionTokens: getConfiguredCompletionTokens(
|
|
4549
|
+
fallbackContext?.provider ?? agentContext.provider,
|
|
4557
4550
|
fallbackContext?.clientOptions ?? agentContext.clientOptions
|
|
4558
4551
|
),
|
|
4559
4552
|
attemptsSoFar: agentContext.overflowRecoveryAttempts,
|
|
@@ -4596,6 +4589,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
4596
4589
|
agentId,
|
|
4597
4590
|
config,
|
|
4598
4591
|
originalToolContent: prunedOriginalToolContent,
|
|
4592
|
+
windowMessages: messagesToUse,
|
|
4599
4593
|
estimatedPromptTokens: recoveryPromptEstimate,
|
|
4600
4594
|
});
|
|
4601
4595
|
}
|
|
@@ -4830,6 +4824,7 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
4830
4824
|
agentId,
|
|
4831
4825
|
config,
|
|
4832
4826
|
originalToolContent: prunedOriginalToolContent,
|
|
4827
|
+
windowMessages: messagesToUse,
|
|
4833
4828
|
estimatedPromptTokens: fallbackRecoveryPromptEstimate,
|
|
4834
4829
|
});
|
|
4835
4830
|
}
|
|
@@ -27,9 +27,17 @@ export interface ReviewedToolApproval {
|
|
|
27
27
|
|
|
28
28
|
const APPROVAL_DECISIONS = new Set(['approve', 'reject', 'edit', 'respond']);
|
|
29
29
|
|
|
30
|
-
/** Detach approval
|
|
30
|
+
/** Detach approval fields without deep-cloning checkpoint-owned metadata. */
|
|
31
31
|
export function cloneToolApprovalInterruptPayload<T>(payload: T): T {
|
|
32
|
-
|
|
32
|
+
if (!isToolApprovalInterrupt(payload)) return payload;
|
|
33
|
+
return {
|
|
34
|
+
...payload,
|
|
35
|
+
...structuredClone({
|
|
36
|
+
action_requests: payload.action_requests,
|
|
37
|
+
review_configs: payload.review_configs,
|
|
38
|
+
...(payload.subagent == null ? {} : { subagent: payload.subagent }),
|
|
39
|
+
}),
|
|
40
|
+
};
|
|
33
41
|
}
|
|
34
42
|
|
|
35
43
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const CLAUDE_GENERATION_PATTERN = /^claude-[a-z]+-(\d+)(?:-|$)/;
|
|
2
|
+
const CURRENT_CLAUDE_DEFAULT_MAX_TOKENS = 16384;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The `max_tokens` floor for current-generation Claude models
|
|
6
|
+
* (`claude-<family>-<N>`, N >= 4) when the caller sets none.
|
|
7
|
+
*
|
|
8
|
+
* Upstream's default table is keyed by model prefix and falls back to 4096 for
|
|
9
|
+
* any Claude name it does not list yet, which caps newer models (and thinking,
|
|
10
|
+
* which shares that budget) far below the 16384 it gives every other Claude 4+
|
|
11
|
+
* model. Raising the default further would trip the SDK's non-streaming guard,
|
|
12
|
+
* which rejects `max_tokens` above ~21K.
|
|
13
|
+
*/
|
|
14
|
+
export function getCurrentClaudeDefaultMaxTokens(
|
|
15
|
+
model: string | undefined
|
|
16
|
+
): number | undefined {
|
|
17
|
+
const generation = CLAUDE_GENERATION_PATTERN.exec(model ?? '')?.[1];
|
|
18
|
+
if (generation == null || Number(generation) < 4) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
return CURRENT_CLAUDE_DEFAULT_MAX_TOKENS;
|
|
22
|
+
}
|
|
@@ -21,15 +21,20 @@ import type {
|
|
|
21
21
|
AnthropicRequestOptions,
|
|
22
22
|
AnthropicMessageStreamEvent,
|
|
23
23
|
} from '@/llm/anthropic/types';
|
|
24
|
-
import type { SmoothItem } from '@/llm/stream/smoother';
|
|
25
24
|
import type { AnthropicUsageData } from './utils/message_outputs';
|
|
25
|
+
import type { SmoothItem } from '@/llm/stream/smoother';
|
|
26
26
|
import {
|
|
27
27
|
_convertMessagesToAnthropicPayload,
|
|
28
28
|
stripUnsupportedAssistantPrefill,
|
|
29
29
|
} from './utils/message_inputs';
|
|
30
|
+
import {
|
|
31
|
+
smoothStream,
|
|
32
|
+
resolveStreamDelay,
|
|
33
|
+
isSignalAborted,
|
|
34
|
+
} from '@/llm/stream/smoother';
|
|
30
35
|
import { _makeMessageChunkFromAnthropicEvent } from './utils/message_outputs';
|
|
31
|
-
import { smoothStream, resolveStreamDelay, isSignalAborted } from '@/llm/stream/smoother';
|
|
32
36
|
import { convertAnthropicStream } from './utils/stream_events';
|
|
37
|
+
import { getCurrentClaudeDefaultMaxTokens } from './defaults';
|
|
33
38
|
import { handleToolChoice } from './utils/tools';
|
|
34
39
|
|
|
35
40
|
type StreamTokenType = 'string' | 'input' | 'content';
|
|
@@ -126,6 +131,11 @@ function isOpus47Model(model?: string): boolean {
|
|
|
126
131
|
return /^claude-opus-4-7(?:-|$)/.test(model ?? '');
|
|
127
132
|
}
|
|
128
133
|
|
|
134
|
+
function resolveDefaultMaxTokens(model: string, inherited: number): number {
|
|
135
|
+
const current = getCurrentClaudeDefaultMaxTokens(model);
|
|
136
|
+
return current == null ? inherited : Math.max(inherited, current);
|
|
137
|
+
}
|
|
138
|
+
|
|
129
139
|
function combineBetas(
|
|
130
140
|
...betaGroups: (AnthropicBeta[] | undefined)[]
|
|
131
141
|
): AnthropicBeta[] {
|
|
@@ -458,6 +468,9 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
458
468
|
contextManagement?: AnthropicContextManagementConfigParam;
|
|
459
469
|
constructor(fields?: CustomAnthropicInput) {
|
|
460
470
|
super(fields);
|
|
471
|
+
if (fields?.maxTokens == null) {
|
|
472
|
+
this.maxTokens = resolveDefaultMaxTokens(this.model, this.maxTokens);
|
|
473
|
+
}
|
|
461
474
|
this.resetTokenEvents();
|
|
462
475
|
this.setDirectFields(fields);
|
|
463
476
|
this._lc_stream_delay = resolveStreamDelay(fields?._lc_stream_delay);
|
package/src/llm/init.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import type { Runnable } from '@langchain/core/runnables';
|
|
2
2
|
import type * as t from '@/types';
|
|
3
|
+
import {
|
|
4
|
+
isOpenAILike,
|
|
5
|
+
isLibreChatOpenAIModel,
|
|
6
|
+
constructorChainHasLcName,
|
|
7
|
+
} from '@/utils';
|
|
8
|
+
import { getProviderFamily } from '@/llm/providerRegistry';
|
|
3
9
|
import { getChatModelClass } from '@/llm/providers';
|
|
4
|
-
import { isOpenAILike, isLibreChatOpenAIModel, constructorChainHasLcName } from '@/utils';
|
|
5
10
|
import { Providers } from '@/common';
|
|
6
11
|
|
|
7
12
|
type InitializeModelParams<P extends t.ProviderName> = {
|
|
8
13
|
provider: P;
|
|
9
14
|
tools?: t.GraphTools;
|
|
15
|
+
disableToolCalling?: boolean;
|
|
10
16
|
} & (
|
|
11
17
|
| {
|
|
12
18
|
override: t.ChatModelInstance;
|
|
@@ -49,6 +55,7 @@ export function initializeModel<P extends t.ProviderName>({
|
|
|
49
55
|
provider,
|
|
50
56
|
clientOptions,
|
|
51
57
|
tools,
|
|
58
|
+
disableToolCalling,
|
|
52
59
|
override,
|
|
53
60
|
}: InitializeModelParams<P>): Runnable {
|
|
54
61
|
const model =
|
|
@@ -89,5 +96,12 @@ export function initializeModel<P extends t.ProviderName>({
|
|
|
89
96
|
);
|
|
90
97
|
}
|
|
91
98
|
|
|
92
|
-
|
|
99
|
+
const family = getProviderFamily(provider);
|
|
100
|
+
const canDisableTools =
|
|
101
|
+
family === 'openai' ||
|
|
102
|
+
family === 'google' ||
|
|
103
|
+
provider === Providers.MOONSHOT;
|
|
104
|
+
if (disableToolCalling !== true || !canDisableTools)
|
|
105
|
+
return model.bindTools(tools);
|
|
106
|
+
return model.bindTools(tools, { tool_choice: 'none' });
|
|
93
107
|
}
|
package/src/llm/invoke.ts
CHANGED
|
@@ -61,6 +61,7 @@ import {
|
|
|
61
61
|
getProviderFamily,
|
|
62
62
|
providerUsesManualToolStream,
|
|
63
63
|
} from '@/llm/providers';
|
|
64
|
+
import { getContextOverflowInfo, isProviderCancellation } from '@/utils/errors';
|
|
64
65
|
import { ChatModelStreamHandler, dispatchesChatModelStream } from '@/stream';
|
|
65
66
|
import { Constants, ContentTypes, GraphEvents, Providers } from '@/common';
|
|
66
67
|
import { withProviderTextBoundary } from '@/llm/providerTextBoundary';
|
|
@@ -68,7 +69,6 @@ import { PreparedSubagentError } from '@/tools/preparedSubagents';
|
|
|
68
69
|
import { assertNotTruncatedToolCall } from '@/llm/truncation';
|
|
69
70
|
import { resolveClientOptionsModel } from '@/llm/request';
|
|
70
71
|
import { safeDispatchCustomEvent } from '@/utils/events';
|
|
71
|
-
import { getContextOverflowInfo } from '@/utils/errors';
|
|
72
72
|
import { appendCallbacks } from '@/utils/callbacks';
|
|
73
73
|
import { composeAbortSignals } from '@/utils/misc';
|
|
74
74
|
import { initializeModel } from '@/llm/init';
|
|
@@ -633,6 +633,8 @@ interface AttemptInvokeCommonParams {
|
|
|
633
633
|
context?: InvokeContext;
|
|
634
634
|
providerTextProtection?: ProviderTextProtection;
|
|
635
635
|
onChunk?: OnChunk;
|
|
636
|
+
/** Observes completed model output synchronously, before terminal validation. */
|
|
637
|
+
onModelComplete?: (message: BaseMessage) => void;
|
|
636
638
|
/**
|
|
637
639
|
* The agent lane this attempt belongs to, as the model node names it. Only
|
|
638
640
|
* read to record a discarded turn, which the node cannot otherwise detect —
|
|
@@ -804,6 +806,7 @@ export async function attemptInvoke(
|
|
|
804
806
|
request,
|
|
805
807
|
context: params.context,
|
|
806
808
|
onChunk: params.onChunk,
|
|
809
|
+
onModelComplete: params.onModelComplete,
|
|
807
810
|
preemptAgentId: params.preemptAgentId,
|
|
808
811
|
providerTextProtection: policy,
|
|
809
812
|
},
|
|
@@ -846,11 +849,16 @@ async function attemptInvokeBody(
|
|
|
846
849
|
request,
|
|
847
850
|
context,
|
|
848
851
|
onChunk,
|
|
852
|
+
onModelComplete,
|
|
849
853
|
preemptAgentId,
|
|
850
854
|
providerTextProtection,
|
|
851
855
|
}: Pick<
|
|
852
856
|
AttemptInvokeCommonParams,
|
|
853
|
-
|
|
857
|
+
| 'context'
|
|
858
|
+
| 'onChunk'
|
|
859
|
+
| 'onModelComplete'
|
|
860
|
+
| 'preemptAgentId'
|
|
861
|
+
| 'providerTextProtection'
|
|
854
862
|
> & {
|
|
855
863
|
request: PreparedProviderRequest;
|
|
856
864
|
},
|
|
@@ -1521,6 +1529,9 @@ async function attemptInvokeBody(
|
|
|
1521
1529
|
throw new Error(EMPTY_PROVIDER_RESPONSE_MESSAGE);
|
|
1522
1530
|
}
|
|
1523
1531
|
|
|
1532
|
+
if (preemptAction === 'none') {
|
|
1533
|
+
onModelComplete?.(snapshotValidatedModelChunk(finalChunk));
|
|
1534
|
+
}
|
|
1524
1535
|
detachValidatedModelToolCalls(finalChunk);
|
|
1525
1536
|
if ((finalChunk.tool_calls?.length ?? 0) > 0) {
|
|
1526
1537
|
finalChunk.tool_calls = finalChunk.tool_calls?.filter(
|
|
@@ -1536,6 +1547,7 @@ async function attemptInvokeBody(
|
|
|
1536
1547
|
await model.invoke(messagesForProvider, invocationConfig),
|
|
1537
1548
|
false
|
|
1538
1549
|
);
|
|
1550
|
+
onModelComplete?.(finalMessage);
|
|
1539
1551
|
if ((finalMessage.tool_calls?.length ?? 0) > 0) {
|
|
1540
1552
|
finalMessage.tool_calls = finalMessage.tool_calls?.filter(
|
|
1541
1553
|
(tool_call: ToolCall) => !!tool_call.name
|
|
@@ -1610,6 +1622,7 @@ export function getFallbackOverflowCandidates(
|
|
|
1610
1622
|
export async function tryFallbackProviders({
|
|
1611
1623
|
fallbacks,
|
|
1612
1624
|
tools,
|
|
1625
|
+
disableToolCalling,
|
|
1613
1626
|
messages,
|
|
1614
1627
|
config,
|
|
1615
1628
|
primaryError,
|
|
@@ -1621,9 +1634,13 @@ export async function tryFallbackProviders({
|
|
|
1621
1634
|
overflowContext,
|
|
1622
1635
|
prepareProviderRequest: prepareFallbackRequest,
|
|
1623
1636
|
prepareProviderMessages,
|
|
1637
|
+
invokeModel = attemptInvoke,
|
|
1624
1638
|
}: {
|
|
1625
1639
|
fallbacks: t.FallbackConfig[];
|
|
1640
|
+
/** Allows callers to retry or validate a completed provider attempt. */
|
|
1641
|
+
invokeModel?: typeof attemptInvoke;
|
|
1626
1642
|
tools?: t.GraphTools;
|
|
1643
|
+
disableToolCalling?: boolean;
|
|
1627
1644
|
messages: BaseMessage[];
|
|
1628
1645
|
config?: RunnableConfig;
|
|
1629
1646
|
primaryError: unknown;
|
|
@@ -1655,6 +1672,7 @@ export async function tryFallbackProviders({
|
|
|
1655
1672
|
provider: t.ProviderName;
|
|
1656
1673
|
clientOptions?: t.ClientOptions;
|
|
1657
1674
|
maxContextTokens?: number;
|
|
1675
|
+
contextBudgetMode?: t.ContextBudgetMode;
|
|
1658
1676
|
config?: RunnableConfig;
|
|
1659
1677
|
}) => PreparedProviderRequest | Promise<PreparedProviderRequest>;
|
|
1660
1678
|
/** @deprecated Return a `PreparedProviderRequest` instead. */
|
|
@@ -1664,10 +1682,12 @@ export async function tryFallbackProviders({
|
|
|
1664
1682
|
provider: t.ProviderName;
|
|
1665
1683
|
clientOptions?: t.ClientOptions;
|
|
1666
1684
|
maxContextTokens?: number;
|
|
1685
|
+
contextBudgetMode?: t.ContextBudgetMode;
|
|
1667
1686
|
config?: RunnableConfig;
|
|
1668
1687
|
}) => BaseMessage[] | Promise<BaseMessage[]>;
|
|
1669
1688
|
}): Promise<Partial<t.BaseGraphState> | undefined> {
|
|
1670
|
-
if (primaryError instanceof ProviderTextProtectionError) throw primaryError;
|
|
1689
|
+
if (primaryError instanceof ProviderTextProtectionError || isProviderCancellation(primaryError)) throw primaryError;
|
|
1690
|
+
config?.signal?.throwIfAborted();
|
|
1671
1691
|
const isOverflow = (
|
|
1672
1692
|
error: unknown,
|
|
1673
1693
|
contextOverride = overflowContext
|
|
@@ -1685,10 +1705,12 @@ export async function tryFallbackProviders({
|
|
|
1685
1705
|
: undefined;
|
|
1686
1706
|
for (const fb of fallbacks) {
|
|
1687
1707
|
try {
|
|
1708
|
+
config?.signal?.throwIfAborted();
|
|
1688
1709
|
const fbModel = initializeModel({
|
|
1689
1710
|
provider: fb.provider,
|
|
1690
1711
|
clientOptions: fb.clientOptions,
|
|
1691
1712
|
tools,
|
|
1713
|
+
...(disableToolCalling == null ? {} : {disableToolCalling}),
|
|
1692
1714
|
});
|
|
1693
1715
|
/**
|
|
1694
1716
|
* Stamp the fallback's configured model onto callback metadata so
|
|
@@ -1714,6 +1736,7 @@ export async function tryFallbackProviders({
|
|
|
1714
1736
|
provider: fb.provider,
|
|
1715
1737
|
clientOptions: fb.clientOptions,
|
|
1716
1738
|
maxContextTokens: fb.maxContextTokens,
|
|
1739
|
+
contextBudgetMode: fb.contextBudgetMode,
|
|
1717
1740
|
config: fbConfig,
|
|
1718
1741
|
};
|
|
1719
1742
|
const preparedRequest = await prepareFallbackRequest?.(preparationInput);
|
|
@@ -1728,14 +1751,7 @@ export async function tryFallbackProviders({
|
|
|
1728
1751
|
let fallbackMessages = messages;
|
|
1729
1752
|
if (preparedRequest == null) {
|
|
1730
1753
|
fallbackMessages =
|
|
1731
|
-
(await prepareProviderMessages?.(
|
|
1732
|
-
model: fbModel as t.ChatModel,
|
|
1733
|
-
messages,
|
|
1734
|
-
provider: fb.provider,
|
|
1735
|
-
clientOptions: fb.clientOptions,
|
|
1736
|
-
maxContextTokens: fb.maxContextTokens,
|
|
1737
|
-
config: fbConfig,
|
|
1738
|
-
})) ?? messages;
|
|
1754
|
+
(await prepareProviderMessages?.(preparationInput)) ?? messages;
|
|
1739
1755
|
}
|
|
1740
1756
|
/** A sibling can trip the breaker while the preparation above is
|
|
1741
1757
|
* awaited — and the catch below only sees attempts that THROW, so a
|
|
@@ -1749,8 +1765,9 @@ export async function tryFallbackProviders({
|
|
|
1749
1765
|
) {
|
|
1750
1766
|
throw config.signal.reason;
|
|
1751
1767
|
}
|
|
1768
|
+
config?.signal?.throwIfAborted();
|
|
1752
1769
|
if (preparedRequest != null) {
|
|
1753
|
-
return await
|
|
1770
|
+
return await invokeModel(
|
|
1754
1771
|
{
|
|
1755
1772
|
request: preparedRequest,
|
|
1756
1773
|
context,
|
|
@@ -1762,7 +1779,7 @@ export async function tryFallbackProviders({
|
|
|
1762
1779
|
fbConfig
|
|
1763
1780
|
);
|
|
1764
1781
|
}
|
|
1765
|
-
return await
|
|
1782
|
+
return await invokeModel(
|
|
1766
1783
|
{
|
|
1767
1784
|
model: fbModel as t.ChatModel,
|
|
1768
1785
|
messages: fallbackMessages,
|
|
@@ -1785,7 +1802,8 @@ export async function tryFallbackProviders({
|
|
|
1785
1802
|
e instanceof StreamLimitExceededError ||
|
|
1786
1803
|
e instanceof PreparedSubagentError ||
|
|
1787
1804
|
e instanceof InvalidModelToolCallError ||
|
|
1788
|
-
e instanceof ProviderTextProtectionError
|
|
1805
|
+
e instanceof ProviderTextProtectionError ||
|
|
1806
|
+
isProviderCancellation(e)
|
|
1789
1807
|
) {
|
|
1790
1808
|
throw e;
|
|
1791
1809
|
}
|
|
@@ -1801,6 +1819,7 @@ export async function tryFallbackProviders({
|
|
|
1801
1819
|
) {
|
|
1802
1820
|
throw config.signal.reason;
|
|
1803
1821
|
}
|
|
1822
|
+
config?.signal?.throwIfAborted();
|
|
1804
1823
|
lastError = e;
|
|
1805
1824
|
const fallbackOverflowContext: ContextOverflowContext = {
|
|
1806
1825
|
provider: fb.provider,
|
|
@@ -560,7 +560,7 @@ function _convertReasoningSummaryToOpenAIResponsesParams(
|
|
|
560
560
|
reasoning: ChatOpenAIReasoningSummary
|
|
561
561
|
): OpenAIClient.Responses.ResponseReasoningItem {
|
|
562
562
|
// combine summary parts that have the the same index and then remove the indexes
|
|
563
|
-
const summary = (
|
|
563
|
+
const summary: OpenAIClient.Responses.ResponseReasoningItem.Summary[] = (
|
|
564
564
|
reasoning.summary.length > 1
|
|
565
565
|
? reasoning.summary.reduce(
|
|
566
566
|
(acc, curr) => {
|
|
@@ -576,9 +576,7 @@ function _convertReasoningSummaryToOpenAIResponsesParams(
|
|
|
576
576
|
[{ ...reasoning.summary[0] }]
|
|
577
577
|
)
|
|
578
578
|
: reasoning.summary
|
|
579
|
-
).map((
|
|
580
|
-
Object.fromEntries(Object.entries(s).filter(([k]) => k !== 'index'))
|
|
581
|
-
) as OpenAIClient.Responses.ResponseReasoningItem.Summary[];
|
|
579
|
+
).map(({ index: _index, ...part }) => part);
|
|
582
580
|
|
|
583
581
|
return {
|
|
584
582
|
...reasoning,
|
package/src/llm/request.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type * as t from '@/types';
|
|
2
|
+
import { getCurrentClaudeDefaultMaxTokens } from '@/llm/anthropic/defaults';
|
|
2
3
|
import { getProviderFamily } from '@/llm/providerRegistry';
|
|
3
4
|
import { Providers } from '@/common';
|
|
4
5
|
|
|
@@ -81,3 +82,46 @@ export function getMaxOutputTokensKey(
|
|
|
81
82
|
? 'maxOutputTokens'
|
|
82
83
|
: 'maxTokens';
|
|
83
84
|
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The completion allowance the provider will reserve, under whichever key its
|
|
88
|
+
* client uses. Providers count it against the same ceiling as the prompt, so
|
|
89
|
+
* overflow recovery has to reserve it when the error did not itemize the
|
|
90
|
+
* total. Anthropic clients spread `invocationKwargs` over `max_tokens`, so a
|
|
91
|
+
* `max_tokens` there is the value actually sent. Without an explicit value, the
|
|
92
|
+
* built-in Anthropic client still sends its model default, so that default is
|
|
93
|
+
* reserved instead. Host providers in the Anthropic family may wrap a client
|
|
94
|
+
* with a different default, so they keep reserving nothing they did not
|
|
95
|
+
* configure.
|
|
96
|
+
*/
|
|
97
|
+
export function getConfiguredCompletionTokens(
|
|
98
|
+
provider: t.ProviderName,
|
|
99
|
+
clientOptions: t.ClientOptions | undefined
|
|
100
|
+
): number | undefined {
|
|
101
|
+
const options = clientOptions as
|
|
102
|
+
| {
|
|
103
|
+
maxTokens?: unknown;
|
|
104
|
+
maxOutputTokens?: unknown;
|
|
105
|
+
invocationKwargs?: { max_tokens?: unknown };
|
|
106
|
+
}
|
|
107
|
+
| undefined;
|
|
108
|
+
const explicit = [
|
|
109
|
+
options?.invocationKwargs?.max_tokens,
|
|
110
|
+
options?.maxTokens,
|
|
111
|
+
options?.maxOutputTokens,
|
|
112
|
+
];
|
|
113
|
+
for (const value of explicit) {
|
|
114
|
+
if (typeof value === 'number' && Number.isFinite(value) && value > 0) {
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (
|
|
119
|
+
explicit.some((value) => value != null) ||
|
|
120
|
+
provider !== Providers.ANTHROPIC
|
|
121
|
+
) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
return getCurrentClaudeDefaultMaxTokens(
|
|
125
|
+
resolveClientOptionsModel(clientOptions)
|
|
126
|
+
);
|
|
127
|
+
}
|
|
@@ -2,6 +2,7 @@ import { AIMessageChunk } from '@langchain/core/messages';
|
|
|
2
2
|
import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
3
3
|
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
4
4
|
import type { SmoothItem, SmoothPiece } from '@/llm/stream/smoother';
|
|
5
|
+
import { hasMeaningfulReasoningPayload } from '@/messages/reasoningTypes';
|
|
5
6
|
import { smoothStream } from '@/llm/stream/smoother';
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -138,7 +139,8 @@ function hasFinishReason(
|
|
|
138
139
|
generationInfo: ChatGenerationChunk['generationInfo']
|
|
139
140
|
): boolean {
|
|
140
141
|
return (
|
|
141
|
-
generationInfo?.finish_reason != null ||
|
|
142
|
+
generationInfo?.finish_reason != null ||
|
|
143
|
+
generationInfo?.finishReason != null
|
|
142
144
|
);
|
|
143
145
|
}
|
|
144
146
|
|
|
@@ -159,21 +161,16 @@ export function hasToolCallChunks(message: AIMessageChunk): boolean {
|
|
|
159
161
|
return (message.tool_call_chunks?.length ?? 0) > 0;
|
|
160
162
|
}
|
|
161
163
|
|
|
162
|
-
export function hasReasoningKwargs(
|
|
164
|
+
export function hasReasoningKwargs(
|
|
165
|
+
message: Pick<AIMessageChunk, 'additional_kwargs'>
|
|
166
|
+
): boolean {
|
|
163
167
|
const kwargs = message.additional_kwargs;
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
kwargs.
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (
|
|
171
|
-
Array.isArray(kwargs.reasoning_details) &&
|
|
172
|
-
kwargs.reasoning_details.length > 0
|
|
173
|
-
) {
|
|
174
|
-
return true;
|
|
175
|
-
}
|
|
176
|
-
return kwargs.reasoning != null;
|
|
168
|
+
return (
|
|
169
|
+
hasMeaningfulReasoningPayload(kwargs.reasoning_content) ||
|
|
170
|
+
hasMeaningfulReasoningPayload(kwargs.reasoning) ||
|
|
171
|
+
(Array.isArray(kwargs.reasoning_details) &&
|
|
172
|
+
kwargs.reasoning_details.some(hasMeaningfulReasoningPayload))
|
|
173
|
+
);
|
|
177
174
|
}
|
|
178
175
|
|
|
179
176
|
/**
|
|
@@ -195,9 +192,8 @@ export function getReasoningKwargsText(message: AIMessageChunk): string {
|
|
|
195
192
|
return reasoning;
|
|
196
193
|
}
|
|
197
194
|
if (reasoning != null && typeof reasoning === 'object') {
|
|
198
|
-
const summaryText = (
|
|
199
|
-
|
|
200
|
-
).summary?.[0]?.text;
|
|
195
|
+
const summaryText = (reasoning as { summary?: { text?: unknown }[] })
|
|
196
|
+
.summary?.[0]?.text;
|
|
201
197
|
if (typeof summaryText === 'string' && summaryText !== '') {
|
|
202
198
|
return summaryText;
|
|
203
199
|
}
|