@librechat/agents 3.1.78-dev.0 → 3.1.79
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/graphs/Graph.cjs +7 -0
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/index.cjs +44 -55
- package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +33 -21
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +0 -4
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
- package/dist/cjs/messages/anthropicToolCache.cjs +48 -15
- package/dist/cjs/messages/anthropicToolCache.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +97 -14
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalExecutionEngine.cjs +14 -16
- package/dist/cjs/tools/local/LocalExecutionEngine.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +30 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +7 -0
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/anthropic/index.mjs +43 -54
- package/dist/esm/llm/anthropic/index.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +33 -21
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs +0 -4
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
- package/dist/esm/messages/anthropicToolCache.mjs +48 -15
- package/dist/esm/messages/anthropicToolCache.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +97 -14
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/tools/local/LocalExecutionEngine.mjs +14 -16
- package/dist/esm/tools/local/LocalExecutionEngine.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +30 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/types/llm/anthropic/index.d.ts +1 -9
- package/dist/types/messages/anthropicToolCache.d.ts +5 -5
- package/dist/types/tools/subagent/SubagentExecutor.d.ts +29 -0
- package/package.json +1 -1
- package/src/graphs/Graph.ts +9 -0
- package/src/llm/anthropic/index.ts +55 -64
- package/src/llm/anthropic/llm.spec.ts +585 -0
- package/src/llm/anthropic/utils/message_inputs.ts +36 -21
- package/src/llm/anthropic/utils/message_outputs.ts +0 -4
- package/src/llm/anthropic/utils/server-tool-inputs.test.ts +95 -13
- package/src/messages/__tests__/anthropicToolCache.test.ts +46 -0
- package/src/messages/anthropicToolCache.ts +70 -25
- package/src/messages/format.ts +117 -18
- package/src/messages/formatAgentMessages.test.ts +202 -1
- package/src/scripts/subagent-configurable-inheritance.ts +252 -0
- package/src/specs/summarization.test.ts +3 -3
- package/src/tools/__tests__/LocalExecutionRoots.test.ts +8 -0
- package/src/tools/__tests__/SubagentExecutor.test.ts +148 -0
- package/src/tools/local/LocalExecutionEngine.ts +55 -54
- package/src/tools/subagent/SubagentExecutor.ts +60 -0
- package/src/types/diff.d.ts +15 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ChatAnthropicMessages } from '@langchain/anthropic';
|
|
2
2
|
import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
3
3
|
import type { BaseChatModelParams } from '@langchain/core/language_models/chat_models';
|
|
4
|
-
import type { BaseMessage
|
|
4
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
5
5
|
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
6
6
|
import type { AnthropicInput } from '@langchain/anthropic';
|
|
7
7
|
import type { Anthropic } from '@anthropic-ai/sdk';
|
|
@@ -31,10 +31,7 @@ type CustomAnthropicInvocationParams = {
|
|
|
31
31
|
};
|
|
32
32
|
export declare class CustomAnthropic extends ChatAnthropicMessages {
|
|
33
33
|
_lc_stream_delay: number;
|
|
34
|
-
private message_start;
|
|
35
|
-
private message_delta;
|
|
36
34
|
private tools_in_params?;
|
|
37
|
-
private emitted_usage?;
|
|
38
35
|
top_k: number | undefined;
|
|
39
36
|
outputConfig?: AnthropicOutputConfig;
|
|
40
37
|
inferenceGeo?: string;
|
|
@@ -45,11 +42,6 @@ export declare class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
45
42
|
* Get the parameters used to invoke the model
|
|
46
43
|
*/
|
|
47
44
|
invocationParams(options?: this['ParsedCallOptions'] & CustomAnthropicCallOptions): Omit<AnthropicMessageCreateParams | AnthropicStreamingMessageCreateParams, 'messages'> & CustomAnthropicInvocationParams;
|
|
48
|
-
/**
|
|
49
|
-
* Get stream usage as returned by this client's API response.
|
|
50
|
-
* @returns The stream usage object.
|
|
51
|
-
*/
|
|
52
|
-
getStreamUsage(): UsageMetadata | undefined;
|
|
53
45
|
resetTokenEvents(): void;
|
|
54
46
|
setDirectFields(fields?: CustomAnthropicInput): void;
|
|
55
47
|
private createGenerationChunk;
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
* the breakpoint.
|
|
20
20
|
*
|
|
21
21
|
* LangChain's Anthropic adapter passes the marker through via
|
|
22
|
-
* `tool.extras.cache_control`
|
|
23
|
-
*
|
|
24
|
-
* mutating the original tool instance, since callers may share them
|
|
22
|
+
* `tool.extras.cache_control` for custom tools, while Anthropic built-ins
|
|
23
|
+
* require direct `cache_control`. Either way, we stamp a fresh wrapper —
|
|
24
|
+
* never mutating the original tool instance, since callers may share them
|
|
25
25
|
* across runs.
|
|
26
26
|
*/
|
|
27
27
|
import type { GraphTools } from '@/types';
|
|
@@ -45,7 +45,7 @@ export declare function makeIsDeferred(toolDefinitions: ReadonlyArray<{
|
|
|
45
45
|
*
|
|
46
46
|
* The original tool instances are never mutated. The marked entry is a
|
|
47
47
|
* shallow wrapper that preserves the prototype chain so downstream
|
|
48
|
-
* `instanceof` checks still pass. `extras` is merged
|
|
49
|
-
* `providerToolDefinition` / other extras
|
|
48
|
+
* `instanceof` checks still pass. For custom tools, `extras` is merged
|
|
49
|
+
* so any existing `providerToolDefinition` / other extras are kept.
|
|
50
50
|
*/
|
|
51
51
|
export declare function partitionAndMarkAnthropicToolCache(tools: GraphTools | undefined, isDeferred: (toolName: string) => boolean): GraphTools | undefined;
|
|
@@ -15,6 +15,35 @@ export type SubagentExecuteParams = {
|
|
|
15
15
|
* without relying on event ordering heuristics.
|
|
16
16
|
*/
|
|
17
17
|
parentToolCallId?: string;
|
|
18
|
+
/**
|
|
19
|
+
* Snapshot of the parent invocation's `config.configurable` at the
|
|
20
|
+
* spawn-tool call site. Inherited verbatim into the child workflow's
|
|
21
|
+
* `configurable` so host-set fields (`requestBody`, `user`,
|
|
22
|
+
* `userMCPAuthMap`, etc.) propagate — fixing MCP body-placeholder
|
|
23
|
+
* substitution and per-user lookups for subagent tool calls.
|
|
24
|
+
*
|
|
25
|
+
* Inheritance details (verified empirically against LangGraph):
|
|
26
|
+
* - host-set keys propagate as-is into the child's tool dispatches;
|
|
27
|
+
* - `thread_id` propagates (with `childRunId` as a fallback when
|
|
28
|
+
* parent did not supply one) — matches the "subagent is part of
|
|
29
|
+
* the same conversation" mental model and aligns with the
|
|
30
|
+
* `sessionId: this.parentRunId` convention this executor already
|
|
31
|
+
* uses for `SubagentStart` / `SubagentStop` hooks;
|
|
32
|
+
* - `parent_run_id` propagates when the host put it on parent's
|
|
33
|
+
* configurable;
|
|
34
|
+
* - `run_id` is *overwritten by the LangGraph runtime* at child
|
|
35
|
+
* invoke time regardless of what we forward — child's tool
|
|
36
|
+
* dispatches see the child graph's runtime runId in
|
|
37
|
+
* `configurable.run_id`, not the parent's. Hosts that need
|
|
38
|
+
* parent-scoped run identity for downstream consumers should
|
|
39
|
+
* plumb it via a host-defined key (e.g. `requestBody.messageId`),
|
|
40
|
+
* not `run_id`.
|
|
41
|
+
*
|
|
42
|
+
* A future revision will likely make this inheritance configurable
|
|
43
|
+
* per spawn type — background / async subagents may want isolation
|
|
44
|
+
* rather than sharing parent's host context.
|
|
45
|
+
*/
|
|
46
|
+
parentConfigurable?: Record<string, unknown>;
|
|
18
47
|
};
|
|
19
48
|
export type SubagentExecuteResult = {
|
|
20
49
|
content: string;
|
package/package.json
CHANGED
package/src/graphs/Graph.ts
CHANGED
|
@@ -1471,6 +1471,15 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
1471
1471
|
subagentType,
|
|
1472
1472
|
threadId,
|
|
1473
1473
|
parentToolCallId,
|
|
1474
|
+
/**
|
|
1475
|
+
* Forward the parent's `configurable` so host-set fields
|
|
1476
|
+
* (`requestBody`, `user`, etc.) propagate into the child
|
|
1477
|
+
* workflow. The executor scrubs run-identity fields before
|
|
1478
|
+
* forwarding — see `SubagentExecuteParams.parentConfigurable`.
|
|
1479
|
+
*/
|
|
1480
|
+
parentConfigurable: config.configurable as
|
|
1481
|
+
| Record<string, unknown>
|
|
1482
|
+
| undefined,
|
|
1474
1483
|
});
|
|
1475
1484
|
return result.content;
|
|
1476
1485
|
}, buildSubagentToolParams(resolvedConfigs));
|
|
@@ -4,7 +4,6 @@ import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
|
4
4
|
import type { BaseChatModelParams } from '@langchain/core/language_models/chat_models';
|
|
5
5
|
import type {
|
|
6
6
|
BaseMessage,
|
|
7
|
-
UsageMetadata,
|
|
8
7
|
MessageContentComplex,
|
|
9
8
|
} from '@langchain/core/messages';
|
|
10
9
|
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
@@ -13,18 +12,13 @@ import type { Anthropic } from '@anthropic-ai/sdk';
|
|
|
13
12
|
import type {
|
|
14
13
|
AnthropicMessageCreateParams,
|
|
15
14
|
AnthropicStreamingMessageCreateParams,
|
|
16
|
-
AnthropicMessageStartEvent,
|
|
17
|
-
AnthropicMessageDeltaEvent,
|
|
18
15
|
AnthropicOutputConfig,
|
|
19
16
|
AnthropicBeta,
|
|
20
17
|
ChatAnthropicToolType,
|
|
21
18
|
AnthropicMCPServerURLDefinition,
|
|
22
19
|
AnthropicContextManagementConfigParam,
|
|
23
20
|
} from '@/llm/anthropic/types';
|
|
24
|
-
import {
|
|
25
|
-
_makeMessageChunkFromAnthropicEvent,
|
|
26
|
-
getAnthropicUsageMetadata,
|
|
27
|
-
} from './utils/message_outputs';
|
|
21
|
+
import { _makeMessageChunkFromAnthropicEvent } from './utils/message_outputs';
|
|
28
22
|
import { _convertMessagesToAnthropicPayload } from './utils/message_inputs';
|
|
29
23
|
import { handleToolChoice } from './utils/tools';
|
|
30
24
|
import { TextStream } from '@/llm/text';
|
|
@@ -53,13 +47,17 @@ export function _documentsInParams(
|
|
|
53
47
|
continue;
|
|
54
48
|
}
|
|
55
49
|
for (const block of message.content) {
|
|
50
|
+
const maybeBlock: unknown = block;
|
|
56
51
|
if (
|
|
57
|
-
typeof
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
52
|
+
typeof maybeBlock === 'object' &&
|
|
53
|
+
maybeBlock !== null &&
|
|
54
|
+
'type' in maybeBlock &&
|
|
55
|
+
maybeBlock.type === 'document' &&
|
|
56
|
+
'citations' in maybeBlock &&
|
|
57
|
+
maybeBlock.citations != null &&
|
|
58
|
+
typeof maybeBlock.citations === 'object' &&
|
|
59
|
+
'enabled' in maybeBlock.citations &&
|
|
60
|
+
maybeBlock.citations.enabled === true
|
|
63
61
|
) {
|
|
64
62
|
return true;
|
|
65
63
|
}
|
|
@@ -307,6 +305,30 @@ function cloneChunk(
|
|
|
307
305
|
return chunk;
|
|
308
306
|
}
|
|
309
307
|
|
|
308
|
+
function withIncrementalMessageDeltaUsage(
|
|
309
|
+
chunk: AIMessageChunk,
|
|
310
|
+
previousOutputTokens: number
|
|
311
|
+
): { chunk: AIMessageChunk; outputTokens: number } {
|
|
312
|
+
const usage = chunk.usage_metadata;
|
|
313
|
+
if (usage == null) {
|
|
314
|
+
return { chunk, outputTokens: previousOutputTokens };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const outputTokens = Math.max(0, usage.output_tokens - previousOutputTokens);
|
|
318
|
+
return {
|
|
319
|
+
chunk: new AIMessageChunk(
|
|
320
|
+
Object.assign({}, chunk, {
|
|
321
|
+
usage_metadata: {
|
|
322
|
+
...usage,
|
|
323
|
+
output_tokens: outputTokens,
|
|
324
|
+
total_tokens: usage.input_tokens + outputTokens,
|
|
325
|
+
},
|
|
326
|
+
})
|
|
327
|
+
),
|
|
328
|
+
outputTokens: usage.output_tokens,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
310
332
|
export type CustomAnthropicInput = AnthropicInput & {
|
|
311
333
|
_lc_stream_delay?: number;
|
|
312
334
|
outputConfig?: AnthropicOutputConfig;
|
|
@@ -334,10 +356,7 @@ type CustomAnthropicInvocationParams = {
|
|
|
334
356
|
|
|
335
357
|
export class CustomAnthropic extends ChatAnthropicMessages {
|
|
336
358
|
_lc_stream_delay: number;
|
|
337
|
-
private message_start: AnthropicMessageStartEvent | undefined;
|
|
338
|
-
private message_delta: AnthropicMessageDeltaEvent | undefined;
|
|
339
359
|
private tools_in_params?: boolean;
|
|
340
|
-
private emitted_usage?: boolean;
|
|
341
360
|
top_k: number | undefined;
|
|
342
361
|
outputConfig?: AnthropicOutputConfig;
|
|
343
362
|
inferenceGeo?: string;
|
|
@@ -436,33 +455,7 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
436
455
|
};
|
|
437
456
|
}
|
|
438
457
|
|
|
439
|
-
/**
|
|
440
|
-
* Get stream usage as returned by this client's API response.
|
|
441
|
-
* @returns The stream usage object.
|
|
442
|
-
*/
|
|
443
|
-
getStreamUsage(): UsageMetadata | undefined {
|
|
444
|
-
if (this.emitted_usage === true) {
|
|
445
|
-
return;
|
|
446
|
-
}
|
|
447
|
-
const inputUsage = this.message_start?.message.usage;
|
|
448
|
-
const outputUsage = this.message_delta?.usage;
|
|
449
|
-
if (!outputUsage) {
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
this.emitted_usage = true;
|
|
454
|
-
return getAnthropicUsageMetadata({
|
|
455
|
-
input_tokens: inputUsage?.input_tokens,
|
|
456
|
-
output_tokens: outputUsage.output_tokens,
|
|
457
|
-
cache_creation_input_tokens: inputUsage?.cache_creation_input_tokens,
|
|
458
|
-
cache_read_input_tokens: inputUsage?.cache_read_input_tokens,
|
|
459
|
-
});
|
|
460
|
-
}
|
|
461
|
-
|
|
462
458
|
resetTokenEvents(): void {
|
|
463
|
-
this.message_start = undefined;
|
|
464
|
-
this.message_delta = undefined;
|
|
465
|
-
this.emitted_usage = undefined;
|
|
466
459
|
this.tools_in_params = undefined;
|
|
467
460
|
}
|
|
468
461
|
|
|
@@ -484,17 +477,13 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
484
477
|
private createGenerationChunk({
|
|
485
478
|
token,
|
|
486
479
|
chunk,
|
|
487
|
-
usageMetadata,
|
|
488
480
|
shouldStreamUsage,
|
|
489
481
|
}: {
|
|
490
482
|
token?: string;
|
|
491
483
|
chunk: AIMessageChunk;
|
|
492
484
|
shouldStreamUsage: boolean;
|
|
493
|
-
usageMetadata?: UsageMetadata;
|
|
494
485
|
}): ChatGenerationChunk {
|
|
495
|
-
const usage_metadata = shouldStreamUsage
|
|
496
|
-
? (usageMetadata ?? chunk.usage_metadata)
|
|
497
|
-
: undefined;
|
|
486
|
+
const usage_metadata = shouldStreamUsage ? chunk.usage_metadata : undefined;
|
|
498
487
|
return new ChatGenerationChunk({
|
|
499
488
|
message: new AIMessageChunk({
|
|
500
489
|
// Just yield chunk as it is and tool_use will be concat by BaseChatModel._generateUncached().
|
|
@@ -534,6 +523,7 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
534
523
|
});
|
|
535
524
|
|
|
536
525
|
const shouldStreamUsage = options.streamUsage ?? this.streamUsage;
|
|
526
|
+
let messageDeltaOutputTokens = 0;
|
|
537
527
|
|
|
538
528
|
for await (const data of stream) {
|
|
539
529
|
if (options.signal?.aborted === true) {
|
|
@@ -541,17 +531,6 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
541
531
|
throw new Error('AbortError: User aborted the request.');
|
|
542
532
|
}
|
|
543
533
|
|
|
544
|
-
if (data.type === 'message_start') {
|
|
545
|
-
this.message_start = data as AnthropicMessageStartEvent;
|
|
546
|
-
} else if (data.type === 'message_delta') {
|
|
547
|
-
this.message_delta = data as AnthropicMessageDeltaEvent;
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
let usageMetadata: UsageMetadata | undefined;
|
|
551
|
-
if (this.tools_in_params !== true && this.emitted_usage !== true) {
|
|
552
|
-
usageMetadata = this.getStreamUsage();
|
|
553
|
-
}
|
|
554
|
-
|
|
555
534
|
const result = _makeMessageChunkFromAnthropicEvent(
|
|
556
535
|
data as Anthropic.Beta.Messages.BetaRawMessageStreamEvent,
|
|
557
536
|
{
|
|
@@ -561,18 +540,25 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
561
540
|
);
|
|
562
541
|
if (!result) continue;
|
|
563
542
|
|
|
564
|
-
|
|
543
|
+
let { chunk } = result;
|
|
544
|
+
if (data.type === 'message_delta') {
|
|
545
|
+
const incremental = withIncrementalMessageDeltaUsage(
|
|
546
|
+
chunk,
|
|
547
|
+
messageDeltaOutputTokens
|
|
548
|
+
);
|
|
549
|
+
chunk = incremental.chunk;
|
|
550
|
+
messageDeltaOutputTokens = incremental.outputTokens;
|
|
551
|
+
}
|
|
565
552
|
const [token = '', tokenType] = extractToken(chunk);
|
|
566
553
|
|
|
567
554
|
if (
|
|
568
555
|
!tokenType ||
|
|
569
556
|
tokenType === 'input' ||
|
|
570
|
-
(token === '' && (
|
|
557
|
+
(token === '' && (chunk.usage_metadata != null || chunk.id != null))
|
|
571
558
|
) {
|
|
572
559
|
const generationChunk = this.createGenerationChunk({
|
|
573
560
|
token,
|
|
574
561
|
chunk,
|
|
575
|
-
usageMetadata,
|
|
576
562
|
shouldStreamUsage,
|
|
577
563
|
});
|
|
578
564
|
yield generationChunk;
|
|
@@ -602,15 +588,20 @@ export class CustomAnthropic extends ChatAnthropicMessages {
|
|
|
602
588
|
break;
|
|
603
589
|
}
|
|
604
590
|
const newChunk = cloneChunk(currentToken, tokenType, chunk);
|
|
591
|
+
const chunkForToken =
|
|
592
|
+
emittedUsage && newChunk.usage_metadata != null
|
|
593
|
+
? new AIMessageChunk(
|
|
594
|
+
Object.assign({}, newChunk, { usage_metadata: undefined })
|
|
595
|
+
)
|
|
596
|
+
: newChunk;
|
|
605
597
|
|
|
606
598
|
const generationChunk = this.createGenerationChunk({
|
|
607
599
|
token: currentToken,
|
|
608
|
-
chunk:
|
|
609
|
-
usageMetadata: emittedUsage ? undefined : usageMetadata,
|
|
600
|
+
chunk: chunkForToken,
|
|
610
601
|
shouldStreamUsage,
|
|
611
602
|
});
|
|
612
603
|
|
|
613
|
-
if (
|
|
604
|
+
if (newChunk.usage_metadata != null && !emittedUsage) {
|
|
614
605
|
emittedUsage = true;
|
|
615
606
|
}
|
|
616
607
|
yield generationChunk;
|