@librechat/agents 4.2.5 → 4.2.6
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/harness/identity.cjs +26 -0
- package/dist/cjs/harness/identity.cjs.map +1 -0
- package/dist/cjs/harness/index.cjs +3 -15
- package/dist/cjs/harness/index.cjs.map +1 -1
- package/dist/cjs/harness/providerText.cjs +79 -0
- package/dist/cjs/harness/providerText.cjs.map +1 -0
- package/dist/cjs/llm/contextPressureMeter.cjs +1 -1
- package/dist/cjs/llm/contextPressureMeter.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +21 -12
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/streamMetadata.cjs +4 -1
- package/dist/cjs/llm/openai/streamMetadata.cjs.map +1 -1
- package/dist/cjs/main.cjs +5 -0
- package/dist/cjs/stream.cjs +4 -1
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/BashExecutor.cjs +2 -1
- package/dist/cjs/tools/BashExecutor.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +141 -94
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/toolOutputReferences.cjs +39 -17
- package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -1
- package/dist/esm/harness/identity.mjs +24 -0
- package/dist/esm/harness/identity.mjs.map +1 -0
- package/dist/esm/harness/index.mjs +3 -15
- package/dist/esm/harness/index.mjs.map +1 -1
- package/dist/esm/harness/providerText.mjs +78 -0
- package/dist/esm/harness/providerText.mjs.map +1 -0
- package/dist/esm/llm/contextPressureMeter.mjs +1 -1
- package/dist/esm/llm/contextPressureMeter.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +21 -12
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/streamMetadata.mjs +4 -1
- package/dist/esm/llm/openai/streamMetadata.mjs.map +1 -1
- package/dist/esm/main.mjs +3 -1
- package/dist/esm/stream.mjs +4 -1
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/BashExecutor.mjs +2 -1
- package/dist/esm/tools/BashExecutor.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +142 -95
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/toolOutputReferences.mjs +39 -18
- package/dist/esm/tools/toolOutputReferences.mjs.map +1 -1
- package/dist/types/harness/identity.d.ts +14 -0
- package/dist/types/harness/index.d.ts +5 -9
- package/dist/types/harness/providerText.d.ts +32 -0
- package/dist/types/llm/openai/streamMetadata.d.ts +3 -3
- package/dist/types/tools/ToolNode.d.ts +7 -7
- package/dist/types/tools/toolOutputReferences.d.ts +16 -4
- package/package.json +1 -1
- package/src/harness/identity.ts +34 -0
- package/src/harness/index.ts +15 -23
- package/src/harness/providerText.ts +128 -0
- package/src/llm/contextPressureMeter.ts +2 -4
- package/src/llm/openai/index.ts +60 -20
- package/src/llm/openai/streamMetadata.ts +14 -3
- package/src/stream.ts +10 -1
- package/src/tools/BashExecutor.ts +2 -1
- package/src/tools/ToolNode.ts +333 -256
- package/src/tools/toolOutputReferences.ts +101 -32
|
@@ -10,7 +10,12 @@
|
|
|
10
10
|
* Subsequent tool calls can pipe a previous output into their args by
|
|
11
11
|
* embedding `{{tool<idx>turn<turn>}}` inside any string argument;
|
|
12
12
|
* {@link ToolOutputReferenceRegistry.resolve} walks the args and
|
|
13
|
-
* substitutes the placeholders immediately before invocation.
|
|
13
|
+
* substitutes the placeholders immediately before invocation. Backslashes
|
|
14
|
+
* directly before a placeholder follow the usual escape rule: each pair
|
|
15
|
+
* collapses to one literal backslash, and a remaining single backslash is
|
|
16
|
+
* removed and escapes the placeholder, which then passes through as literal
|
|
17
|
+
* text without a registry lookup. So `\{{tool0turn0}}` is the literal
|
|
18
|
+
* placeholder and `\\{{tool0turn0}}` is a backslash followed by the output.
|
|
14
19
|
*
|
|
15
20
|
* The registry stores the *raw, untruncated* tool output so a later
|
|
16
21
|
* `{{…}}` substitution pipes the full payload into the next tool —
|
|
@@ -77,6 +82,7 @@ export type ResolveResult<T> = {
|
|
|
77
82
|
* point in time, ignoring any subsequent registrations.
|
|
78
83
|
*/
|
|
79
84
|
export interface ToolOutputResolveView {
|
|
85
|
+
readonly availableKeys?: readonly string[];
|
|
80
86
|
resolve<T>(args: T, options?: ResolveOptions): ResolveResult<T>;
|
|
81
87
|
}
|
|
82
88
|
/**
|
|
@@ -88,6 +94,8 @@ export interface ToolOutputResolveView {
|
|
|
88
94
|
*/
|
|
89
95
|
export type ResolveOptions = {
|
|
90
96
|
substituteIntentKey?: boolean;
|
|
97
|
+
/** Detached pre-editor input; unchanged string leaves stay literal. */
|
|
98
|
+
previouslyResolved?: Record<string, unknown> | string;
|
|
91
99
|
};
|
|
92
100
|
/**
|
|
93
101
|
* Pre-resolved arg map keyed by `toolCallId`. Used by the mixed
|
|
@@ -122,9 +130,11 @@ export declare class ToolOutputReferenceRegistry {
|
|
|
122
130
|
private readonly maxTotalSize;
|
|
123
131
|
private readonly maxActiveRuns;
|
|
124
132
|
/**
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
133
|
+
* Global matcher used only by `replaceInString`. Kept off-module so
|
|
134
|
+
* callers of the exported `TOOL_OUTPUT_REF_PATTERN` never see a stale
|
|
135
|
+
* `lastIndex`. Leading backslashes are counted by hand rather than
|
|
136
|
+
* matched: a `\\*` prefix would backtrack quadratically over a long
|
|
137
|
+
* backslash run that no placeholder follows.
|
|
128
138
|
*/
|
|
129
139
|
private static readonly PLACEHOLDER_MATCHER;
|
|
130
140
|
constructor(options?: ToolOutputReferenceRegistryOptions);
|
|
@@ -208,6 +218,8 @@ export declare class ToolOutputReferenceRegistry {
|
|
|
208
218
|
private replaceInString;
|
|
209
219
|
private evictWithinBucket;
|
|
210
220
|
}
|
|
221
|
+
/** Detached editor baseline; reference-free inputs need no retained values. */
|
|
222
|
+
export declare function snapshotResolvedArgs(args: Record<string, unknown>): Record<string, unknown>;
|
|
211
223
|
/**
|
|
212
224
|
* Annotates `content` with a reference key and/or unresolved-ref
|
|
213
225
|
* warnings so the LLM sees both alongside the tool output.
|
package/package.json
CHANGED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
const requiredIdentityField = z
|
|
4
|
+
.string()
|
|
5
|
+
.refine((value) => value.trim().length > 0);
|
|
6
|
+
|
|
7
|
+
/** Established by the authenticated host, never inferred from model arguments. */
|
|
8
|
+
export const executionIdentitySchema = z.object({
|
|
9
|
+
runtimeId: requiredIdentityField,
|
|
10
|
+
agentId: requiredIdentityField,
|
|
11
|
+
runId: requiredIdentityField,
|
|
12
|
+
userId: requiredIdentityField,
|
|
13
|
+
tenantId: z.string().optional(),
|
|
14
|
+
conversationId: z.string().optional(),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
export type ExecutionIdentity = z.infer<typeof executionIdentitySchema>;
|
|
18
|
+
const identityKeys = executionIdentitySchema.keyof().options;
|
|
19
|
+
|
|
20
|
+
/** Validates and snapshots identity at the host binding boundary. */
|
|
21
|
+
export function snapshotExecutionIdentity(
|
|
22
|
+
source: Readonly<ExecutionIdentity>
|
|
23
|
+
): Readonly<ExecutionIdentity> {
|
|
24
|
+
const result = executionIdentitySchema.safeParse(source);
|
|
25
|
+
if (!result.success) throw new Error('Incomplete execution identity');
|
|
26
|
+
return Object.freeze(result.data);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function matchesExecutionIdentity(
|
|
30
|
+
a: Readonly<ExecutionIdentity>,
|
|
31
|
+
b: Readonly<ExecutionIdentity>
|
|
32
|
+
): boolean {
|
|
33
|
+
return identityKeys.every((key) => a[key] === b[key]);
|
|
34
|
+
}
|
package/src/harness/index.ts
CHANGED
|
@@ -5,17 +5,11 @@ import type {
|
|
|
5
5
|
ToolExecuteBatchRequest,
|
|
6
6
|
ToolExecuteResult,
|
|
7
7
|
} from '@/types';
|
|
8
|
+
import type { ExecutionIdentity } from './identity';
|
|
9
|
+
import { snapshotExecutionIdentity } from './identity';
|
|
8
10
|
import { GraphEvents } from '@/common';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export interface ExecutionIdentity {
|
|
12
|
-
runtimeId: string;
|
|
13
|
-
agentId: string;
|
|
14
|
-
runId: string;
|
|
15
|
-
userId: string;
|
|
16
|
-
tenantId?: string;
|
|
17
|
-
conversationId?: string;
|
|
18
|
-
}
|
|
11
|
+
export { snapshotExecutionIdentity } from './identity';
|
|
12
|
+
export type { ExecutionIdentity } from './identity';
|
|
19
13
|
|
|
20
14
|
/** A host reference selects an authorized implementation, not a URL or executable name. */
|
|
21
15
|
export interface HostToolDescriptor {
|
|
@@ -81,19 +75,7 @@ export function createHostToolBindings(options: {
|
|
|
81
75
|
}): HostToolBindings {
|
|
82
76
|
if (Number(options.service.version) !== 1)
|
|
83
77
|
throw new Error('Unsupported host tool service version');
|
|
84
|
-
const
|
|
85
|
-
const identity = Object.freeze({
|
|
86
|
-
runtimeId: source.runtimeId,
|
|
87
|
-
agentId: source.agentId,
|
|
88
|
-
runId: source.runId,
|
|
89
|
-
userId: source.userId,
|
|
90
|
-
tenantId: source.tenantId,
|
|
91
|
-
conversationId: source.conversationId,
|
|
92
|
-
});
|
|
93
|
-
for (const key of ['runtimeId', 'agentId', 'runId', 'userId'] as const) {
|
|
94
|
-
if (identity[key].trim().length === 0)
|
|
95
|
-
throw new Error('Incomplete execution identity');
|
|
96
|
-
}
|
|
78
|
+
const identity = snapshotExecutionIdentity(options.identity);
|
|
97
79
|
const references = new Map<string, string>();
|
|
98
80
|
const toolDefinitions: LCTool[] = [];
|
|
99
81
|
for (const descriptor of options.tools) {
|
|
@@ -224,3 +206,13 @@ export function createHostToolBindings(options: {
|
|
|
224
206
|
},
|
|
225
207
|
};
|
|
226
208
|
}
|
|
209
|
+
|
|
210
|
+
export {
|
|
211
|
+
createHostProviderTextService,
|
|
212
|
+
createHostProviderTextProtection,
|
|
213
|
+
} from './providerText';
|
|
214
|
+
export type {
|
|
215
|
+
HostProviderTextDescriptor,
|
|
216
|
+
HostProviderTextRequest,
|
|
217
|
+
HostProviderTextService,
|
|
218
|
+
} from './providerText';
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { ProviderTextProtection } from '@/protection/providerText';
|
|
3
|
+
import type { ExecutionIdentity } from './identity';
|
|
4
|
+
import {
|
|
5
|
+
ProviderTextAttempt,
|
|
6
|
+
ProviderTextProtectionError,
|
|
7
|
+
validateProviderTextProtection,
|
|
8
|
+
} from '@/protection/providerText';
|
|
9
|
+
import {
|
|
10
|
+
executionIdentitySchema,
|
|
11
|
+
snapshotExecutionIdentity,
|
|
12
|
+
matchesExecutionIdentity,
|
|
13
|
+
} from './identity';
|
|
14
|
+
|
|
15
|
+
/** Host-owned limits, negotiated before executing a graph. Contains no credentials or callbacks. */
|
|
16
|
+
export type HostProviderTextDescriptor = Pick<
|
|
17
|
+
ProviderTextProtection,
|
|
18
|
+
'version' | 'timeoutMs' | 'maxAttemptBytes' | 'maxBufferedBytes'
|
|
19
|
+
>;
|
|
20
|
+
|
|
21
|
+
export interface HostProviderTextRequest {
|
|
22
|
+
readonly version: 1;
|
|
23
|
+
readonly identity: Readonly<ExecutionIdentity>;
|
|
24
|
+
readonly content: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Transport implementations authenticate callers and validate incoming data before dispatch. */
|
|
28
|
+
export interface HostProviderTextService {
|
|
29
|
+
readonly version: 1;
|
|
30
|
+
inspect(
|
|
31
|
+
request: HostProviderTextRequest,
|
|
32
|
+
signal?: AbortSignal
|
|
33
|
+
): Promise<string>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const requestSchema = z.object({
|
|
37
|
+
version: z.literal(1),
|
|
38
|
+
content: z.string(),
|
|
39
|
+
identity: executionIdentitySchema,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
function boundIdentity(
|
|
43
|
+
source: Readonly<ExecutionIdentity>
|
|
44
|
+
): Readonly<ExecutionIdentity> {
|
|
45
|
+
try {
|
|
46
|
+
return snapshotExecutionIdentity(source);
|
|
47
|
+
} catch {
|
|
48
|
+
throw new ProviderTextProtectionError('incompatible');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Classification, inspection, deadlines and shared raw-text budgets remain authoritative on the host. */
|
|
53
|
+
export function createHostProviderTextService(options: {
|
|
54
|
+
identity: Readonly<ExecutionIdentity>;
|
|
55
|
+
policy: ProviderTextProtection;
|
|
56
|
+
}): {
|
|
57
|
+
descriptor: HostProviderTextDescriptor;
|
|
58
|
+
service: HostProviderTextService;
|
|
59
|
+
} {
|
|
60
|
+
const identity = boundIdentity(options.identity);
|
|
61
|
+
const policy = options.policy;
|
|
62
|
+
validateProviderTextProtection(policy);
|
|
63
|
+
return {
|
|
64
|
+
descriptor: Object.freeze({
|
|
65
|
+
version: 1,
|
|
66
|
+
timeoutMs: policy.timeoutMs,
|
|
67
|
+
maxAttemptBytes: policy.maxAttemptBytes,
|
|
68
|
+
maxBufferedBytes: policy.maxBufferedBytes,
|
|
69
|
+
}),
|
|
70
|
+
service: {
|
|
71
|
+
version: 1,
|
|
72
|
+
inspect: async (request, signal): Promise<string> => {
|
|
73
|
+
const parsed = requestSchema.safeParse(request);
|
|
74
|
+
if (
|
|
75
|
+
!parsed.success ||
|
|
76
|
+
!matchesExecutionIdentity(parsed.data.identity, identity)
|
|
77
|
+
)
|
|
78
|
+
throw new ProviderTextProtectionError('incompatible');
|
|
79
|
+
const attempt = new ProviderTextAttempt(policy, signal);
|
|
80
|
+
try {
|
|
81
|
+
attempt.append(parsed.data.content);
|
|
82
|
+
return await attempt.release();
|
|
83
|
+
} finally {
|
|
84
|
+
attempt.finish();
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Installs the SDK's existing release boundary around a transport-neutral host service.
|
|
93
|
+
* The SDK release boundary buffers each attempt before one host request.
|
|
94
|
+
* Classification and inspection statistics belong to the host. The adapter returns only canonical text.
|
|
95
|
+
*/
|
|
96
|
+
export function createHostProviderTextProtection(options: {
|
|
97
|
+
identity: Readonly<ExecutionIdentity>;
|
|
98
|
+
descriptor: HostProviderTextDescriptor;
|
|
99
|
+
service: HostProviderTextService;
|
|
100
|
+
}): ProviderTextProtection {
|
|
101
|
+
const identity = boundIdentity(options.identity);
|
|
102
|
+
const service = options.service;
|
|
103
|
+
const serviceVersion: number = service.version;
|
|
104
|
+
const descriptorVersion: number = options.descriptor.version;
|
|
105
|
+
if (serviceVersion !== 1 || descriptorVersion !== 1)
|
|
106
|
+
throw new ProviderTextProtectionError('incompatible');
|
|
107
|
+
const policy: ProviderTextProtection = {
|
|
108
|
+
version: 1,
|
|
109
|
+
timeoutMs: options.descriptor.timeoutMs,
|
|
110
|
+
maxAttemptBytes: options.descriptor.maxAttemptBytes,
|
|
111
|
+
maxBufferedBytes: options.descriptor.maxBufferedBytes,
|
|
112
|
+
// The synchronous SDK classification gate defers to authoritative host classification.
|
|
113
|
+
classify: () => 'prose',
|
|
114
|
+
inspect: async ({ content, signal }) => {
|
|
115
|
+
const canonical = await service.inspect(
|
|
116
|
+
{ version: 1, identity, content },
|
|
117
|
+
signal
|
|
118
|
+
);
|
|
119
|
+
return {
|
|
120
|
+
version: 1,
|
|
121
|
+
ok: true,
|
|
122
|
+
value: { content: canonical, replacements: 0, categories: [] },
|
|
123
|
+
};
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
validateProviderTextProtection(policy);
|
|
127
|
+
return Object.freeze(policy);
|
|
128
|
+
}
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import { isProxy } from 'node:util/types';
|
|
2
2
|
import { ToolMessage } from '@langchain/core/messages';
|
|
3
|
-
|
|
4
3
|
import type { AIMessage, BaseMessage } from '@langchain/core/messages';
|
|
5
4
|
import type { ProviderPayloadMeasurement } from '@/llm/prepareProviderRequest';
|
|
6
5
|
import type * as t from '@/types';
|
|
7
|
-
|
|
8
6
|
import {
|
|
9
7
|
REPLY_PRIMER_TOKENS,
|
|
10
8
|
isSyntheticProviderContextMessage,
|
|
11
9
|
} from '@/messages';
|
|
12
|
-
import { createToolMessageUsageAccumulator } from '@/messages/budget';
|
|
13
10
|
import { readTokenMetadataProperty, apportionTokenCounts } from '@/utils';
|
|
11
|
+
import { createToolMessageUsageAccumulator } from '@/messages/budget';
|
|
14
12
|
|
|
15
13
|
interface ContextPressureUsage {
|
|
16
14
|
contextBudget?: number;
|
|
@@ -409,7 +407,7 @@ export function createContextPressureMeter({
|
|
|
409
407
|
baselineRemaining != null &&
|
|
410
408
|
Number.isFinite(baselineRemaining)
|
|
411
409
|
? availableMessageTokens -
|
|
412
|
-
Math.min(availableMessageTokens,
|
|
410
|
+
Math.min(availableMessageTokens, baselineRemaining)
|
|
413
411
|
: undefined;
|
|
414
412
|
const toolUsage = createToolMessageUsageAccumulator(provider);
|
|
415
413
|
const toolUsageState: { error?: Error } = {};
|
package/src/llm/openai/index.ts
CHANGED
|
@@ -1147,7 +1147,10 @@ type ResponsesAnnotationsBoundaryEvent = {
|
|
|
1147
1147
|
export function ensureResponsesOutputAnnotations(
|
|
1148
1148
|
event: ResponsesAnnotationsBoundaryEvent
|
|
1149
1149
|
): void {
|
|
1150
|
-
if (
|
|
1150
|
+
if (
|
|
1151
|
+
event.type !== 'response.completed' &&
|
|
1152
|
+
event.type !== 'response.incomplete'
|
|
1153
|
+
) {
|
|
1151
1154
|
return;
|
|
1152
1155
|
}
|
|
1153
1156
|
const output = event.response?.output;
|
|
@@ -1418,7 +1421,8 @@ async function completionWithFilteredOpenAIStream(
|
|
|
1418
1421
|
|
|
1419
1422
|
function attachLibreChatDeltaFields(
|
|
1420
1423
|
chunk: BaseMessageChunk,
|
|
1421
|
-
delta: Record<string, unknown
|
|
1424
|
+
delta: Record<string, unknown>,
|
|
1425
|
+
rawResponse?: unknown
|
|
1422
1426
|
): BaseMessageChunk {
|
|
1423
1427
|
if (!AIMessageChunk.isInstance(chunk)) {
|
|
1424
1428
|
return chunk;
|
|
@@ -1439,12 +1443,32 @@ function attachLibreChatDeltaFields(
|
|
|
1439
1443
|
chunk.additional_kwargs.provider_specific_fields =
|
|
1440
1444
|
libreChatDelta.provider_specific_fields;
|
|
1441
1445
|
}
|
|
1446
|
+
if ('usage' in chunk.response_metadata) {
|
|
1447
|
+
delete chunk.response_metadata.usage;
|
|
1448
|
+
}
|
|
1449
|
+
const rawRecord = rawResponse as
|
|
1450
|
+
| {
|
|
1451
|
+
provider?: unknown;
|
|
1452
|
+
choices?: Array<{ finish_reason?: unknown }>;
|
|
1453
|
+
}
|
|
1454
|
+
| undefined;
|
|
1455
|
+
const provider = rawRecord?.provider;
|
|
1456
|
+
if (
|
|
1457
|
+
typeof provider === 'string' &&
|
|
1458
|
+
provider.length > 0 &&
|
|
1459
|
+
(chunk.response_metadata.finish_reason != null ||
|
|
1460
|
+
rawRecord?.choices?.some((choice) => choice.finish_reason != null) ===
|
|
1461
|
+
true)
|
|
1462
|
+
) {
|
|
1463
|
+
chunk.response_metadata.provider = provider;
|
|
1464
|
+
}
|
|
1442
1465
|
return chunk;
|
|
1443
1466
|
}
|
|
1444
1467
|
|
|
1445
1468
|
function attachLibreChatMessageFields(
|
|
1446
1469
|
message: BaseMessage,
|
|
1447
|
-
rawMessage: Record<string, unknown
|
|
1470
|
+
rawMessage: Record<string, unknown>,
|
|
1471
|
+
rawResponse?: unknown
|
|
1448
1472
|
): BaseMessage {
|
|
1449
1473
|
if (!isAIMessage(message)) {
|
|
1450
1474
|
return message;
|
|
@@ -1462,6 +1486,11 @@ function attachLibreChatMessageFields(
|
|
|
1462
1486
|
message.additional_kwargs.provider_specific_fields =
|
|
1463
1487
|
rawMessage.provider_specific_fields;
|
|
1464
1488
|
}
|
|
1489
|
+
const provider = (rawResponse as { provider?: unknown } | undefined)
|
|
1490
|
+
?.provider;
|
|
1491
|
+
if (typeof provider === 'string' && provider.length > 0) {
|
|
1492
|
+
message.response_metadata.provider = provider;
|
|
1493
|
+
}
|
|
1465
1494
|
return message;
|
|
1466
1495
|
}
|
|
1467
1496
|
|
|
@@ -2036,7 +2065,8 @@ class LibreChatOpenAICompletions extends OriginalChatOpenAICompletions {
|
|
|
2036
2065
|
rawResponse,
|
|
2037
2066
|
defaultRole
|
|
2038
2067
|
),
|
|
2039
|
-
delta
|
|
2068
|
+
delta,
|
|
2069
|
+
rawResponse
|
|
2040
2070
|
);
|
|
2041
2071
|
if (isOfficialOpenAIBaseURL(this.clientConfig.baseURL)) {
|
|
2042
2072
|
return stampSequentialStreamedToolCallAdapter(message);
|
|
@@ -2050,7 +2080,8 @@ class LibreChatOpenAICompletions extends OriginalChatOpenAICompletions {
|
|
|
2050
2080
|
): BaseMessage {
|
|
2051
2081
|
return attachLibreChatMessageFields(
|
|
2052
2082
|
super._convertCompletionsMessageToBaseMessage(message, rawResponse),
|
|
2053
|
-
message as unknown as Record<string, unknown
|
|
2083
|
+
message as unknown as Record<string, unknown>,
|
|
2084
|
+
rawResponse
|
|
2054
2085
|
);
|
|
2055
2086
|
}
|
|
2056
2087
|
|
|
@@ -2059,31 +2090,23 @@ class LibreChatOpenAICompletions extends OriginalChatOpenAICompletions {
|
|
|
2059
2090
|
options: this['ParsedCallOptions'],
|
|
2060
2091
|
runManager?: CallbackManagerForLLMRun
|
|
2061
2092
|
): Promise<ChatResult> {
|
|
2093
|
+
options.signal?.throwIfAborted();
|
|
2094
|
+
const params = this.invocationParams(options);
|
|
2062
2095
|
if (
|
|
2096
|
+
params.stream !== true &&
|
|
2063
2097
|
this.includeReasoningContent !== true &&
|
|
2064
2098
|
this.includeReasoningDetails !== true
|
|
2065
2099
|
) {
|
|
2066
2100
|
return super._generate(messages, options, runManager);
|
|
2067
2101
|
}
|
|
2068
2102
|
|
|
2069
|
-
options.signal?.throwIfAborted();
|
|
2070
2103
|
const usageMetadata: Partial<UsageMetadata> = {};
|
|
2071
|
-
const params = this.invocationParams(options);
|
|
2072
|
-
const messagesMapped = _convertMessagesToOpenAIParams(
|
|
2073
|
-
messages,
|
|
2074
|
-
this.model,
|
|
2075
|
-
{
|
|
2076
|
-
includeReasoningContent: this.includeReasoningContent,
|
|
2077
|
-
includeReasoningDetails: this.includeReasoningDetails,
|
|
2078
|
-
convertReasoningDetailsToContent: this.convertReasoningDetailsToContent,
|
|
2079
|
-
preserveToolCacheControl: this.preserveToolCacheControl,
|
|
2080
|
-
}
|
|
2081
|
-
);
|
|
2082
|
-
|
|
2083
2104
|
if (params.stream === true) {
|
|
2084
2105
|
const stream = this._streamResponseChunks(messages, options, runManager);
|
|
2085
2106
|
const finalChunks = new Map<number, ChatGenerationChunk>();
|
|
2107
|
+
const seenScalarMetadata: SeenScalarMetadata = new Map();
|
|
2086
2108
|
for await (const chunk of stream) {
|
|
2109
|
+
dropRepeatedScalarMetadata(chunk, seenScalarMetadata);
|
|
2087
2110
|
chunk.message.response_metadata = {
|
|
2088
2111
|
...chunk.generationInfo,
|
|
2089
2112
|
...chunk.message.response_metadata,
|
|
@@ -2125,6 +2148,16 @@ class LibreChatOpenAICompletions extends OriginalChatOpenAICompletions {
|
|
|
2125
2148
|
};
|
|
2126
2149
|
}
|
|
2127
2150
|
|
|
2151
|
+
const messagesMapped = _convertMessagesToOpenAIParams(
|
|
2152
|
+
messages,
|
|
2153
|
+
this.model,
|
|
2154
|
+
{
|
|
2155
|
+
includeReasoningContent: this.includeReasoningContent,
|
|
2156
|
+
includeReasoningDetails: this.includeReasoningDetails,
|
|
2157
|
+
convertReasoningDetailsToContent: this.convertReasoningDetailsToContent,
|
|
2158
|
+
preserveToolCacheControl: this.preserveToolCacheControl,
|
|
2159
|
+
}
|
|
2160
|
+
);
|
|
2128
2161
|
const data = await this.completionWithRetry(
|
|
2129
2162
|
{
|
|
2130
2163
|
...params,
|
|
@@ -2440,7 +2473,9 @@ class LibreChatOpenAIResponses extends OriginalChatOpenAIResponses {
|
|
|
2440
2473
|
cache_control: cacheControl,
|
|
2441
2474
|
}),
|
|
2442
2475
|
};
|
|
2443
|
-
if (
|
|
2476
|
+
if (
|
|
2477
|
+
shouldIncludeEncryptedReasoning(this.model, params, this.astraRulesApply)
|
|
2478
|
+
) {
|
|
2444
2479
|
params.include = [
|
|
2445
2480
|
...new Set([
|
|
2446
2481
|
...(params.include ?? []),
|
|
@@ -2730,7 +2765,9 @@ class LibreChatAzureOpenAIResponses extends OriginalAzureChatOpenAIResponses {
|
|
|
2730
2765
|
promptCacheExplicit: this.promptCacheExplicit,
|
|
2731
2766
|
safetyIdentifier: this.safetyIdentifier,
|
|
2732
2767
|
});
|
|
2733
|
-
if (
|
|
2768
|
+
if (
|
|
2769
|
+
shouldIncludeEncryptedReasoning(this.model, params, this.astraRulesApply)
|
|
2770
|
+
) {
|
|
2734
2771
|
params.include = [
|
|
2735
2772
|
...new Set([
|
|
2736
2773
|
...(params.include ?? []),
|
|
@@ -3476,6 +3513,9 @@ export class ChatDeepSeek extends OriginalChatDeepSeek {
|
|
|
3476
3513
|
data,
|
|
3477
3514
|
defaultRole
|
|
3478
3515
|
);
|
|
3516
|
+
if ('usage' in messageChunk.response_metadata) {
|
|
3517
|
+
delete messageChunk.response_metadata.usage;
|
|
3518
|
+
}
|
|
3479
3519
|
defaultRole = delta.role ?? defaultRole;
|
|
3480
3520
|
|
|
3481
3521
|
if (typeof messageChunk.content !== 'string') {
|
|
@@ -14,6 +14,8 @@ const REPEATED_SCALAR_METADATA_FIELDS = [
|
|
|
14
14
|
'finish_reason',
|
|
15
15
|
'service_tier',
|
|
16
16
|
'system_fingerprint',
|
|
17
|
+
'provider',
|
|
18
|
+
'usage',
|
|
17
19
|
] as const;
|
|
18
20
|
|
|
19
21
|
/** Per-completion-index set of scalar fields already emitted this stream. */
|
|
@@ -29,9 +31,9 @@ function completionIndex(
|
|
|
29
31
|
/**
|
|
30
32
|
* Strip scalar `response_metadata`/`generationInfo` fields that have already
|
|
31
33
|
* appeared on an earlier chunk of the same completion (tracked in `seen`),
|
|
32
|
-
* keeping the first occurrence
|
|
33
|
-
* value once. Scoped per completion index
|
|
34
|
-
*
|
|
34
|
+
* keeping the first occurrence (first nonempty snapshot for usage) so the
|
|
35
|
+
* aggregated message still carries the value once. Scoped per completion index
|
|
36
|
+
* so multi-choice (`n > 1`) responses keep each choice's own metadata.
|
|
35
37
|
*
|
|
36
38
|
* Deletes from a shallow clone rather than in place: providers such as
|
|
37
39
|
* `ChatDeepSeek` split one raw chunk into several synthetic pieces that share
|
|
@@ -66,6 +68,15 @@ export function dropRepeatedScalarMetadata(
|
|
|
66
68
|
if (!inGenerationInfo && !inResponseMetadata) {
|
|
67
69
|
continue;
|
|
68
70
|
}
|
|
71
|
+
if (
|
|
72
|
+
field === 'usage' &&
|
|
73
|
+
(!inGenerationInfo ||
|
|
74
|
+
Object.keys(generationInfo.usage ?? {}).length === 0) &&
|
|
75
|
+
(!inResponseMetadata ||
|
|
76
|
+
Object.keys(responseMetadata.usage ?? {}).length === 0)
|
|
77
|
+
) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
69
80
|
if (!seenFields.has(field)) {
|
|
70
81
|
seenFields.add(field);
|
|
71
82
|
continue;
|
package/src/stream.ts
CHANGED
|
@@ -176,6 +176,12 @@ function hasToolOutputReference(value: unknown): boolean {
|
|
|
176
176
|
return false;
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
+
function hasAnthropicServerToolId(toolCall: ToolCall): boolean {
|
|
180
|
+
return (
|
|
181
|
+
toolCall.id?.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) ?? false
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
179
185
|
function isEagerExecutionExcludedTool(
|
|
180
186
|
name: string,
|
|
181
187
|
graph: StandardGraph,
|
|
@@ -728,9 +734,12 @@ function createEagerToolExecutionPlan(args: {
|
|
|
728
734
|
// Drop host-excluded tools only AFTER the batch-level guards above have run
|
|
729
735
|
// against the full batch, so excluding a call never hides a sibling direct
|
|
730
736
|
// tool from `hasDirectToolCallInBatch`. Excluded calls fall through to normal
|
|
731
|
-
// ToolNode execution; siblings may still eager-execute.
|
|
737
|
+
// ToolNode execution; siblings may still eager-execute. Provider-executed
|
|
738
|
+
// server tool calls (`srvtoolu_`, e.g. Anthropic web_search) already ran on
|
|
739
|
+
// the provider side, so they are never dispatched to the host.
|
|
732
740
|
const candidateToolCalls = unstartedToolCalls.filter(
|
|
733
741
|
(toolCall) =>
|
|
742
|
+
!hasAnthropicServerToolId(toolCall) &&
|
|
734
743
|
!isEagerExecutionExcludedTool(toolCall.name, graph, agentContext)
|
|
735
744
|
);
|
|
736
745
|
if (candidateToolCalls.length === 0) {
|
|
@@ -128,7 +128,8 @@ Referencing previous tool outputs:
|
|
|
128
128
|
- Example (simple ASCII output): \`echo '{{tool0turn0}}' | jq '.foo'\` takes the full output of the first tool from the first turn and pipes it into jq.
|
|
129
129
|
- For payloads that may contain quotes, parentheses, backticks, or arbitrary bytes (random/binary data, JSON with embedded quotes, multi-line strings), prefer a quoted-delimiter heredoc over \`echo '…'\`. The heredoc body is not interpreted by the shell, so substituted payloads pass through unchanged.
|
|
130
130
|
- Heredoc example: \`wc -c << 'EOF'\\n{{tool0turn0}}\\nEOF\` (the quotes around \`'EOF'\` disable interpolation inside the body).
|
|
131
|
-
-
|
|
131
|
+
- A call that uses an unknown reference key is rejected before anything runs. The error names the unknown keys and lists the most recent available ones; correct the key or fetch the output again.
|
|
132
|
+
- To pass a placeholder as literal text, prefix it with a backslash: \`\\{{tool0turn0}}\` reaches the tool as \`{{tool0turn0}}\` with no substitution. For a real backslash directly before a reference (for example a Windows path), double it: \`C:\\out\\\\{{tool0turn0}}\`.
|
|
132
133
|
`.trim();
|
|
133
134
|
|
|
134
135
|
/**
|