@librechat/agents 4.2.8 → 4.2.10
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 +32 -2
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/harness/fileBindings.cjs +93 -0
- package/dist/cjs/harness/fileBindings.cjs.map +1 -0
- package/dist/cjs/harness/fileService.cjs +141 -0
- package/dist/cjs/harness/fileService.cjs.map +1 -0
- package/dist/cjs/harness/fileStream.cjs +76 -0
- package/dist/cjs/harness/fileStream.cjs.map +1 -0
- package/dist/cjs/harness/files.cjs +106 -0
- package/dist/cjs/harness/files.cjs.map +1 -0
- package/dist/cjs/harness/index.cjs +3 -0
- package/dist/cjs/harness/index.cjs.map +1 -1
- package/dist/cjs/harness/model.cjs +6 -3
- package/dist/cjs/harness/model.cjs.map +1 -1
- package/dist/cjs/llm/google/utils/signatures.cjs +5 -3
- package/dist/cjs/llm/google/utils/signatures.cjs.map +1 -1
- package/dist/cjs/llm/invocation.cjs +19 -0
- package/dist/cjs/llm/invocation.cjs.map +1 -0
- package/dist/cjs/llm/providerTextBoundary.cjs +6 -5
- package/dist/cjs/llm/providerTextBoundary.cjs.map +1 -1
- package/dist/cjs/main.cjs +13 -0
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +3 -2
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticCallerPolicy.cjs +24 -1
- package/dist/cjs/tools/ProgrammaticCallerPolicy.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +3 -2
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs +2 -1
- package/dist/cjs/tools/local/LocalProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +15 -3
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/tools/subagent/childGraphConfig.cjs +13 -1
- package/dist/cjs/tools/subagent/childGraphConfig.cjs.map +1 -1
- package/dist/cjs/tools/subagent/hostArgs.cjs +2 -0
- package/dist/cjs/tools/subagent/hostArgs.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +32 -2
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/harness/fileBindings.mjs +93 -0
- package/dist/esm/harness/fileBindings.mjs.map +1 -0
- package/dist/esm/harness/fileService.mjs +140 -0
- package/dist/esm/harness/fileService.mjs.map +1 -0
- package/dist/esm/harness/fileStream.mjs +76 -0
- package/dist/esm/harness/fileStream.mjs.map +1 -0
- package/dist/esm/harness/files.mjs +97 -0
- package/dist/esm/harness/files.mjs.map +1 -0
- package/dist/esm/harness/index.mjs +3 -0
- package/dist/esm/harness/index.mjs.map +1 -1
- package/dist/esm/harness/model.mjs +6 -3
- package/dist/esm/harness/model.mjs.map +1 -1
- package/dist/esm/llm/google/utils/signatures.mjs +5 -3
- package/dist/esm/llm/google/utils/signatures.mjs.map +1 -1
- package/dist/esm/llm/invocation.mjs +18 -0
- package/dist/esm/llm/invocation.mjs.map +1 -0
- package/dist/esm/llm/providerTextBoundary.mjs +6 -5
- package/dist/esm/llm/providerTextBoundary.mjs.map +1 -1
- package/dist/esm/main.mjs +5 -2
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs +3 -2
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticCallerPolicy.mjs +24 -1
- package/dist/esm/tools/ProgrammaticCallerPolicy.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +3 -2
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs +2 -1
- package/dist/esm/tools/local/LocalProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +15 -3
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/tools/subagent/childGraphConfig.mjs +13 -1
- package/dist/esm/tools/subagent/childGraphConfig.mjs.map +1 -1
- package/dist/esm/tools/subagent/hostArgs.mjs +2 -1
- package/dist/esm/tools/subagent/hostArgs.mjs.map +1 -1
- package/dist/types/harness/fileBindings.d.ts +13 -0
- package/dist/types/harness/fileService.d.ts +37 -0
- package/dist/types/harness/fileStream.d.ts +7 -0
- package/dist/types/harness/files.d.ts +108 -0
- package/dist/types/harness/index.d.ts +7 -0
- package/dist/types/harness/model.d.ts +5 -3
- package/dist/types/llm/invocation.d.ts +9 -0
- package/dist/types/tools/ProgrammaticCallerPolicy.d.ts +1 -0
- package/dist/types/tools/subagent/hostArgs.d.ts +6 -0
- package/dist/types/tools/subagent/index.d.ts +1 -1
- package/dist/types/types/graph.d.ts +6 -3
- package/package.json +1 -1
- package/src/agents/AgentContext.ts +68 -2
- package/src/harness/fileBindings.ts +156 -0
- package/src/harness/fileService.ts +217 -0
- package/src/harness/fileStream.ts +99 -0
- package/src/harness/files.ts +185 -0
- package/src/harness/index.ts +25 -0
- package/src/harness/model.ts +20 -5
- package/src/llm/google/utils/signatures.ts +10 -3
- package/src/llm/invocation.ts +29 -0
- package/src/llm/providerTextBoundary.ts +12 -3
- package/src/tools/BashProgrammaticToolCalling.ts +5 -2
- package/src/tools/ProgrammaticCallerPolicy.ts +42 -3
- package/src/tools/ProgrammaticToolCalling.ts +4 -1
- package/src/tools/local/LocalProgrammaticToolCalling.ts +4 -0
- package/src/tools/subagent/SubagentExecutor.ts +98 -32
- package/src/tools/subagent/childGraphConfig.ts +33 -1
- package/src/tools/subagent/hostArgs.ts +7 -0
- package/src/tools/subagent/index.ts +1 -0
- package/src/types/graph.ts +6 -3
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { executionIdentitySchema } from './identity';
|
|
3
|
+
|
|
4
|
+
const reference = z
|
|
5
|
+
.string()
|
|
6
|
+
.min(1)
|
|
7
|
+
.max(512)
|
|
8
|
+
.refine((value) => value.trim().length > 0);
|
|
9
|
+
const count = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER);
|
|
10
|
+
const digest = z.string().regex(/^[a-f0-9]{64}$/);
|
|
11
|
+
|
|
12
|
+
/** Opaque host references and immutable revisions, never filesystem paths or signed URLs. */
|
|
13
|
+
export const hostFileDescriptorSchema = z.object({
|
|
14
|
+
reference,
|
|
15
|
+
revision: reference,
|
|
16
|
+
filename: z.string().min(1).max(255),
|
|
17
|
+
mediaType: z.string().min(1).max(255),
|
|
18
|
+
bytes: count,
|
|
19
|
+
sha256: digest.optional(),
|
|
20
|
+
});
|
|
21
|
+
export type HostFileDescriptor = Readonly<
|
|
22
|
+
z.infer<typeof hostFileDescriptorSchema>
|
|
23
|
+
>;
|
|
24
|
+
|
|
25
|
+
export const hostFileServiceDescriptorSchema = z.object({
|
|
26
|
+
version: z.literal(1),
|
|
27
|
+
operations: z.array(z.enum(['read', 'stage', 'publish'])).max(3),
|
|
28
|
+
maxFileBytes: count.positive(),
|
|
29
|
+
maxChunkBytes: count.positive(),
|
|
30
|
+
});
|
|
31
|
+
export type HostFileServiceDescriptor = Readonly<
|
|
32
|
+
Omit<z.infer<typeof hostFileServiceDescriptorSchema>, 'operations'>
|
|
33
|
+
> & { readonly operations: readonly ('read' | 'stage' | 'publish')[] };
|
|
34
|
+
|
|
35
|
+
const request = z.object({
|
|
36
|
+
version: z.literal(1),
|
|
37
|
+
identity: executionIdentitySchema,
|
|
38
|
+
});
|
|
39
|
+
export const hostFileReadRequestSchema = request.extend({
|
|
40
|
+
reference,
|
|
41
|
+
revision: reference,
|
|
42
|
+
offset: count,
|
|
43
|
+
length: count.positive(),
|
|
44
|
+
});
|
|
45
|
+
export const hostFileStageRequestSchema = request.extend({
|
|
46
|
+
operationId: reference,
|
|
47
|
+
filename: z.string().min(1).max(255),
|
|
48
|
+
mediaType: z.string().min(1).max(255),
|
|
49
|
+
bytes: count,
|
|
50
|
+
sha256: digest.optional(),
|
|
51
|
+
});
|
|
52
|
+
export const hostFilePublishRequestSchema = request.extend({
|
|
53
|
+
operationId: reference,
|
|
54
|
+
reference,
|
|
55
|
+
revision: reference,
|
|
56
|
+
});
|
|
57
|
+
export type HostFileReadRequest = Readonly<
|
|
58
|
+
z.infer<typeof hostFileReadRequestSchema>
|
|
59
|
+
>;
|
|
60
|
+
export type HostFileStageRequest = Readonly<
|
|
61
|
+
z.infer<typeof hostFileStageRequestSchema>
|
|
62
|
+
>;
|
|
63
|
+
export type HostFilePublishRequest = Readonly<
|
|
64
|
+
z.infer<typeof hostFilePublishRequestSchema>
|
|
65
|
+
>;
|
|
66
|
+
export interface HostFileChunk {
|
|
67
|
+
readonly reference: string;
|
|
68
|
+
readonly revision: string;
|
|
69
|
+
readonly offset: number;
|
|
70
|
+
readonly data: Uint8Array;
|
|
71
|
+
}
|
|
72
|
+
export interface HostFilePublication {
|
|
73
|
+
readonly operationId: string;
|
|
74
|
+
readonly file: HostFileDescriptor;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Authenticate the transport separately. Each operation must authorize the bound identity,
|
|
79
|
+
* reference and revision against canonical host storage. Staging is private; only publish
|
|
80
|
+
* enters the host's native attachment/event pipeline. Mutations are never automatically retried.
|
|
81
|
+
*/
|
|
82
|
+
export interface HostFileService {
|
|
83
|
+
readonly version: 1;
|
|
84
|
+
read(
|
|
85
|
+
request: HostFileReadRequest,
|
|
86
|
+
signal?: AbortSignal
|
|
87
|
+
): Promise<HostFileChunk>;
|
|
88
|
+
stage(
|
|
89
|
+
request: HostFileStageRequest,
|
|
90
|
+
content: AsyncIterable<Uint8Array>,
|
|
91
|
+
signal?: AbortSignal
|
|
92
|
+
): Promise<HostFileDescriptor>;
|
|
93
|
+
publish(
|
|
94
|
+
request: HostFilePublishRequest,
|
|
95
|
+
signal?: AbortSignal
|
|
96
|
+
): Promise<HostFilePublication>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const codes = [
|
|
100
|
+
'incompatible',
|
|
101
|
+
'forbidden',
|
|
102
|
+
'conflict',
|
|
103
|
+
'overflow',
|
|
104
|
+
'cancelled',
|
|
105
|
+
'timeout',
|
|
106
|
+
'unavailable',
|
|
107
|
+
] as const;
|
|
108
|
+
export type HostFileErrorCode = (typeof codes)[number];
|
|
109
|
+
export class HostFileServiceError extends Error {
|
|
110
|
+
readonly code: HostFileErrorCode;
|
|
111
|
+
constructor(code: HostFileErrorCode) {
|
|
112
|
+
const safe = codes.includes(code) ? code : 'incompatible';
|
|
113
|
+
super(`Host file service: ${safe}`);
|
|
114
|
+
this.name = 'HostFileServiceError';
|
|
115
|
+
this.code = safe;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** @internal Never forward adapter error text or abort reasons over the service boundary. */
|
|
120
|
+
export function normalizeHostFileError(
|
|
121
|
+
error: unknown,
|
|
122
|
+
signal?: AbortSignal
|
|
123
|
+
): HostFileServiceError {
|
|
124
|
+
try {
|
|
125
|
+
if (signal?.aborted === true) {
|
|
126
|
+
return new HostFileServiceError(
|
|
127
|
+
signal.reason instanceof HostFileServiceError &&
|
|
128
|
+
signal.reason.code === 'timeout'
|
|
129
|
+
? 'timeout'
|
|
130
|
+
: 'cancelled'
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
if (error instanceof HostFileServiceError)
|
|
134
|
+
return new HostFileServiceError(error.code);
|
|
135
|
+
} catch {
|
|
136
|
+
/** Adapter errors may contain throwing accessors; never expose them. */
|
|
137
|
+
}
|
|
138
|
+
return new HostFileServiceError('unavailable');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** @internal Snapshot only public metadata, dropping host-specific fields. */
|
|
142
|
+
export function checkedHostFile(value: HostFileDescriptor): HostFileDescriptor {
|
|
143
|
+
const parsed = hostFileDescriptorSchema.safeParse(value);
|
|
144
|
+
if (!parsed.success) throw new HostFileServiceError('incompatible');
|
|
145
|
+
return Object.freeze(parsed.data);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** @internal Reject mismatched/stale ranges before bytes enter a graph. */
|
|
149
|
+
export function checkedHostFileChunk(
|
|
150
|
+
value: HostFileChunk,
|
|
151
|
+
input: HostFileReadRequest
|
|
152
|
+
): HostFileChunk {
|
|
153
|
+
if (
|
|
154
|
+
value.reference !== input.reference ||
|
|
155
|
+
value.revision !== input.revision ||
|
|
156
|
+
value.offset !== input.offset ||
|
|
157
|
+
!(value.data instanceof Uint8Array) ||
|
|
158
|
+
value.data.byteLength !== input.length
|
|
159
|
+
)
|
|
160
|
+
throw new HostFileServiceError('incompatible');
|
|
161
|
+
return {
|
|
162
|
+
reference: input.reference,
|
|
163
|
+
revision: input.revision,
|
|
164
|
+
offset: input.offset,
|
|
165
|
+
data: Uint8Array.from(value.data),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** @internal Negotiation validates limits before constructing any graph adapter. */
|
|
170
|
+
export function checkedHostFileService(
|
|
171
|
+
value: HostFileServiceDescriptor
|
|
172
|
+
): HostFileServiceDescriptor {
|
|
173
|
+
const parsed = hostFileServiceDescriptorSchema.safeParse(value);
|
|
174
|
+
if (
|
|
175
|
+
!parsed.success ||
|
|
176
|
+
parsed.data.maxChunkBytes > parsed.data.maxFileBytes ||
|
|
177
|
+
parsed.data.maxChunkBytes > Number.MAX_SAFE_INTEGER / 2 ||
|
|
178
|
+
new Set(parsed.data.operations).size !== parsed.data.operations.length
|
|
179
|
+
)
|
|
180
|
+
throw new HostFileServiceError('incompatible');
|
|
181
|
+
return Object.freeze({
|
|
182
|
+
...parsed.data,
|
|
183
|
+
operations: Object.freeze([...parsed.data.operations]),
|
|
184
|
+
});
|
|
185
|
+
}
|
package/src/harness/index.ts
CHANGED
|
@@ -218,6 +218,7 @@ export type {
|
|
|
218
218
|
} from './providerText';
|
|
219
219
|
|
|
220
220
|
export { HostChatModel, HostModelServiceError } from './model';
|
|
221
|
+
export type { ModelInvocationContext } from '@/llm/invocation';
|
|
221
222
|
export type {
|
|
222
223
|
HostModelDescriptor,
|
|
223
224
|
HostModelRequest,
|
|
@@ -228,3 +229,27 @@ export type {
|
|
|
228
229
|
|
|
229
230
|
export { createHostModelService, HostModelAdmission } from './modelService';
|
|
230
231
|
export type { HostModelLimits } from './modelService';
|
|
232
|
+
|
|
233
|
+
export { createHostFileService, HostFileAdmission } from './fileService';
|
|
234
|
+
export type { HostFileStore } from './fileService';
|
|
235
|
+
export { createHostFileBindings } from './fileBindings';
|
|
236
|
+
export type { HostFileBindings } from './fileBindings';
|
|
237
|
+
export {
|
|
238
|
+
HostFileServiceError,
|
|
239
|
+
hostFileDescriptorSchema,
|
|
240
|
+
hostFileServiceDescriptorSchema,
|
|
241
|
+
hostFileReadRequestSchema,
|
|
242
|
+
hostFileStageRequestSchema,
|
|
243
|
+
hostFilePublishRequestSchema,
|
|
244
|
+
} from './files';
|
|
245
|
+
export type {
|
|
246
|
+
HostFileDescriptor,
|
|
247
|
+
HostFileServiceDescriptor,
|
|
248
|
+
HostFileService,
|
|
249
|
+
HostFileReadRequest,
|
|
250
|
+
HostFileStageRequest,
|
|
251
|
+
HostFilePublishRequest,
|
|
252
|
+
HostFileChunk,
|
|
253
|
+
HostFilePublication,
|
|
254
|
+
HostFileErrorCode,
|
|
255
|
+
} from './files';
|
package/src/harness/model.ts
CHANGED
|
@@ -16,10 +16,12 @@ import type { BaseLanguageModelInput } from '@langchain/core/language_models/bas
|
|
|
16
16
|
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
17
17
|
import type { Runnable } from '@langchain/core/runnables';
|
|
18
18
|
import type { ChatResult } from '@langchain/core/outputs';
|
|
19
|
+
import type { ModelInvocationContext } from '@/llm/invocation';
|
|
19
20
|
import type { ExecutionIdentity } from './identity';
|
|
20
21
|
import { PreparedSubagentError } from '@/tools/preparedSubagents';
|
|
21
22
|
import { stableStringify } from '@/tools/eagerEventExecution';
|
|
22
23
|
import { StreamLimitExceededError } from '@/llm/streamLimits';
|
|
24
|
+
import { getModelInvocationContext } from '@/llm/invocation';
|
|
23
25
|
import { snapshotExecutionIdentity } from './identity';
|
|
24
26
|
|
|
25
27
|
type FunctionTool = ReturnType<typeof convertToOpenAITool>;
|
|
@@ -87,10 +89,15 @@ export interface HostModelRequest {
|
|
|
87
89
|
*/
|
|
88
90
|
export interface HostModelService {
|
|
89
91
|
readonly version: 1;
|
|
90
|
-
invoke(
|
|
92
|
+
invoke(
|
|
93
|
+
request: HostModelRequest,
|
|
94
|
+
signal?: AbortSignal,
|
|
95
|
+
context?: Readonly<ModelInvocationContext>
|
|
96
|
+
): Promise<AIMessage>;
|
|
91
97
|
stream(
|
|
92
98
|
request: HostModelRequest,
|
|
93
|
-
signal?: AbortSignal
|
|
99
|
+
signal?: AbortSignal,
|
|
100
|
+
context?: Readonly<ModelInvocationContext>
|
|
94
101
|
): AsyncIterable<AIMessageChunk>;
|
|
95
102
|
}
|
|
96
103
|
|
|
@@ -205,6 +212,7 @@ function waitForService<T>(
|
|
|
205
212
|
|
|
206
213
|
/** Native LangChain model facade; no provider/model options or credentials cross to the host. */
|
|
207
214
|
export class HostChatModel extends BaseChatModel<HostModelCallOptions> {
|
|
215
|
+
static readonly invocationContextVersion = 1;
|
|
208
216
|
private readonly identity: Readonly<ExecutionIdentity>;
|
|
209
217
|
private readonly descriptor: HostModelDescriptor;
|
|
210
218
|
private readonly service: HostModelService;
|
|
@@ -322,13 +330,19 @@ export class HostChatModel extends BaseChatModel<HostModelCallOptions> {
|
|
|
322
330
|
|
|
323
331
|
async _generate(
|
|
324
332
|
messages: BaseMessage[],
|
|
325
|
-
options: this['ParsedCallOptions']
|
|
333
|
+
options: this['ParsedCallOptions'],
|
|
334
|
+
runManager?: CallbackManagerForLLMRun
|
|
326
335
|
): Promise<ChatResult> {
|
|
327
336
|
const request = this.request(messages, options);
|
|
328
337
|
try {
|
|
329
338
|
options.signal?.throwIfAborted();
|
|
330
339
|
const message = await waitForService(
|
|
331
|
-
() =>
|
|
340
|
+
() =>
|
|
341
|
+
this.service.invoke(
|
|
342
|
+
request,
|
|
343
|
+
options.signal,
|
|
344
|
+
getModelInvocationContext(options, runManager)
|
|
345
|
+
),
|
|
332
346
|
options.signal
|
|
333
347
|
);
|
|
334
348
|
options.signal?.throwIfAborted();
|
|
@@ -353,7 +367,8 @@ export class HostChatModel extends BaseChatModel<HostModelCallOptions> {
|
|
|
353
367
|
let iterator: AsyncIterator<AIMessageChunk> | undefined;
|
|
354
368
|
try {
|
|
355
369
|
signal.throwIfAborted();
|
|
356
|
-
|
|
370
|
+
const context = getModelInvocationContext(options, runManager);
|
|
371
|
+
iterator = this.service.stream(request, signal, context)[Symbol.asyncIterator]();
|
|
357
372
|
for (;;) {
|
|
358
373
|
const next = await waitForService(() => iterator!.next(), signal);
|
|
359
374
|
signal.throwIfAborted();
|
|
@@ -152,21 +152,28 @@ export function restoreGeminiReplayParts(
|
|
|
152
152
|
)
|
|
153
153
|
return undefined;
|
|
154
154
|
let text = '';
|
|
155
|
-
let contentReasoning
|
|
155
|
+
let contentReasoning: string | undefined;
|
|
156
156
|
if (typeof message.content === 'string') text = message.content;
|
|
157
157
|
else
|
|
158
158
|
for (const block of message.content) {
|
|
159
159
|
if (block.type === 'text' && typeof block.text === 'string')
|
|
160
160
|
text += block.text;
|
|
161
161
|
else if (block.type === 'thinking' && typeof block.thinking === 'string')
|
|
162
|
-
contentReasoning
|
|
162
|
+
contentReasoning = (contentReasoning ?? '') + block.thinking;
|
|
163
|
+
else if (
|
|
164
|
+
block.type === 'reasoning' &&
|
|
165
|
+
typeof block.reasoning === 'string'
|
|
166
|
+
)
|
|
167
|
+
contentReasoning = (contentReasoning ?? '') + block.reasoning;
|
|
163
168
|
else if (!('functionCall' in block)) return undefined;
|
|
164
169
|
}
|
|
165
|
-
let reasoning = contentReasoning;
|
|
170
|
+
let reasoning = contentReasoning ?? '';
|
|
166
171
|
if (typeof message.additional_kwargs.reasoning_content === 'string')
|
|
167
172
|
reasoning = message.additional_kwargs.reasoning_content;
|
|
168
173
|
if (typeof message.additional_kwargs.reasoning === 'string')
|
|
169
174
|
reasoning = message.additional_kwargs.reasoning;
|
|
175
|
+
if (contentReasoning != null && contentReasoning !== reasoning)
|
|
176
|
+
return undefined;
|
|
170
177
|
const calls = new Map(
|
|
171
178
|
(message.tool_calls ?? []).map((call) => [call.id, call])
|
|
172
179
|
);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
2
|
+
|
|
3
|
+
/** Correlation only: never an authorization identity or a callback/output capability. */
|
|
4
|
+
export interface ModelInvocationContext {
|
|
5
|
+
readonly version: 1;
|
|
6
|
+
readonly runId: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const contexts = new WeakMap<object, Readonly<ModelInvocationContext>>();
|
|
10
|
+
|
|
11
|
+
export function getModelInvocationContext(
|
|
12
|
+
options: object,
|
|
13
|
+
manager?: CallbackManagerForLLMRun
|
|
14
|
+
): Readonly<ModelInvocationContext> | undefined {
|
|
15
|
+
if (manager != null)
|
|
16
|
+
return Object.freeze({ version: 1, runId: manager.runId });
|
|
17
|
+
return contexts.get(options);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Internal option identity survives callback suppression without entering provider parameters. */
|
|
21
|
+
export function preserveModelInvocationContext<T extends object>(
|
|
22
|
+
source: object,
|
|
23
|
+
target: T,
|
|
24
|
+
manager?: CallbackManagerForLLMRun
|
|
25
|
+
): T {
|
|
26
|
+
const context = getModelInvocationContext(source, manager);
|
|
27
|
+
if (context != null) contexts.set(target, context);
|
|
28
|
+
return target;
|
|
29
|
+
}
|
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
} from '@/protection/providerText';
|
|
21
21
|
import { isProviderTextInput } from '@/protection/providerTextInput';
|
|
22
22
|
import { createProviderTextChunk } from '@/llm/providerTextChunk';
|
|
23
|
+
import { preserveModelInvocationContext } from '@/llm/invocation';
|
|
23
24
|
import { getChatModelClass } from '@/llm/providers';
|
|
24
25
|
import { Providers } from '@/common';
|
|
25
26
|
|
|
@@ -365,7 +366,11 @@ export function withProviderTextBoundary(
|
|
|
365
366
|
try {
|
|
366
367
|
source = model._streamResponseChunks(
|
|
367
368
|
messages,
|
|
368
|
-
|
|
369
|
+
preserveModelInvocationContext(
|
|
370
|
+
options,
|
|
371
|
+
{ ...options, signal: attempt.signal },
|
|
372
|
+
runManager
|
|
373
|
+
),
|
|
369
374
|
undefined
|
|
370
375
|
);
|
|
371
376
|
for (;;) {
|
|
@@ -407,14 +412,18 @@ export function withProviderTextBoundary(
|
|
|
407
412
|
};
|
|
408
413
|
protectedChat._streamChatModelEvents =
|
|
409
414
|
BaseChatModel.prototype._streamChatModelEvents;
|
|
410
|
-
protectedChat._generate = async function (messages, options): Promise<ChatResult> {
|
|
415
|
+
protectedChat._generate = async function (messages, options, runManager): Promise<ChatResult> {
|
|
411
416
|
if (usesInternalStreaming(model, options)) throw new ProviderTextProtectionError('unsupported');
|
|
412
417
|
const attempt = new ProviderTextAttempt(policy, options.signal);
|
|
413
418
|
try {
|
|
414
419
|
const result = await attempt.wait(
|
|
415
420
|
model._generate(
|
|
416
421
|
messages,
|
|
417
|
-
|
|
422
|
+
preserveModelInvocationContext(
|
|
423
|
+
options,
|
|
424
|
+
{ ...options, signal: attempt.signal },
|
|
425
|
+
runManager
|
|
426
|
+
),
|
|
418
427
|
undefined
|
|
419
428
|
)
|
|
420
429
|
);
|
|
@@ -32,8 +32,8 @@ import {
|
|
|
32
32
|
runPlainExecution,
|
|
33
33
|
formatCompletedResponse,
|
|
34
34
|
} from './ProgrammaticToolCalling';
|
|
35
|
-
import { logCodeApiDiagnostic } from '@/tools/diagnostics';
|
|
36
35
|
import { resolveAttachedWorkspaceInstanceId } from '@/tools/workspaceIdentity';
|
|
36
|
+
import { logCodeApiDiagnostic } from '@/tools/diagnostics';
|
|
37
37
|
import { INTENT_PROPERTY } from '@/tools/intentArg';
|
|
38
38
|
import { Constants } from '@/common';
|
|
39
39
|
|
|
@@ -155,7 +155,9 @@ ${EXAMPLES}
|
|
|
155
155
|
${CORE_RULES}`;
|
|
156
156
|
|
|
157
157
|
const TOOL_MANIFEST_DESCRIPTION =
|
|
158
|
-
'
|
|
158
|
+
'Registered tool names or their normalized sandbox function names used by the code. ' +
|
|
159
|
+
'Use sandbox names in code, but registered names in tool_manifest when sandbox names collide. ' +
|
|
160
|
+
'Required when direct-only tools are configured; ' +
|
|
159
161
|
'validated before execution starts. Pass [] when the code calls no tools at all.';
|
|
160
162
|
|
|
161
163
|
// ============================================================================
|
|
@@ -433,6 +435,7 @@ export function createBashProgrammaticToolCallingTool(
|
|
|
433
435
|
allowedToolDefs: toolDefs,
|
|
434
436
|
disallowedToolDefs,
|
|
435
437
|
programmaticToolName,
|
|
438
|
+
normalizeIdentifier: normalizeToBashIdentifier,
|
|
436
439
|
});
|
|
437
440
|
|
|
438
441
|
/* These guard the replay path only. A call that selected no tools never
|
|
@@ -79,6 +79,7 @@ export function selectProgrammaticTools(args: {
|
|
|
79
79
|
allowedToolDefs?: t.LCTool[];
|
|
80
80
|
disallowedToolDefs?: t.LCTool[];
|
|
81
81
|
programmaticToolName: string;
|
|
82
|
+
normalizeIdentifier?: (name: string) => string;
|
|
82
83
|
}): t.LCTool[] {
|
|
83
84
|
const allowedToolDefs = args.allowedToolDefs ?? [];
|
|
84
85
|
const disallowedToolDefs = args.disallowedToolDefs ?? [];
|
|
@@ -111,6 +112,42 @@ export function selectProgrammaticTools(args: {
|
|
|
111
112
|
const unknownNames = new Set(
|
|
112
113
|
requestedToolNames.filter((name) => !allowedByName.has(name))
|
|
113
114
|
);
|
|
115
|
+
if (unknownNames.size > 0 && args.normalizeIdentifier != null) {
|
|
116
|
+
const candidatesByIdentifier = new Map<string, t.LCTool[]>();
|
|
117
|
+
for (const toolDef of allowedToolDefs) {
|
|
118
|
+
const identifier = args.normalizeIdentifier(toolDef.name);
|
|
119
|
+
if (!unknownNames.has(identifier)) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
const candidates = candidatesByIdentifier.get(identifier) ?? [];
|
|
123
|
+
candidates.push(toolDef);
|
|
124
|
+
candidatesByIdentifier.set(identifier, candidates);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const disallowedAliases = new Set<string>();
|
|
128
|
+
for (const toolDef of disallowedToolDefs) {
|
|
129
|
+
const identifier = args.normalizeIdentifier(toolDef.name);
|
|
130
|
+
if (unknownNames.has(identifier)) {
|
|
131
|
+
disallowedAliases.add(toolDef.name);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
assertDisallowedToolUsage(disallowedAliases, args.programmaticToolName);
|
|
135
|
+
|
|
136
|
+
for (const name of unknownNames) {
|
|
137
|
+
const candidates = candidatesByIdentifier.get(name);
|
|
138
|
+
if (candidates == null) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
assertUnambiguousIdentifiers(
|
|
142
|
+
candidates,
|
|
143
|
+
args.normalizeIdentifier,
|
|
144
|
+
args.programmaticToolName
|
|
145
|
+
);
|
|
146
|
+
allowedByName.set(name, candidates[0]);
|
|
147
|
+
unknownNames.delete(name);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
114
151
|
if (unknownNames.size > 0) {
|
|
115
152
|
throw new Error(
|
|
116
153
|
`Tool${unknownNames.size === 1 ? '' : 's'} ${[...unknownNames]
|
|
@@ -122,7 +159,9 @@ export function selectProgrammaticTools(args: {
|
|
|
122
159
|
);
|
|
123
160
|
}
|
|
124
161
|
|
|
125
|
-
return [
|
|
126
|
-
|
|
127
|
-
|
|
162
|
+
return [
|
|
163
|
+
...new Set(
|
|
164
|
+
requestedToolNames.map((name) => allowedByName.get(name) as t.LCTool)
|
|
165
|
+
),
|
|
166
|
+
];
|
|
128
167
|
}
|
|
@@ -112,7 +112,9 @@ ${EXAMPLES}
|
|
|
112
112
|
${CORE_RULES}`;
|
|
113
113
|
|
|
114
114
|
const TOOL_MANIFEST_DESCRIPTION =
|
|
115
|
-
'
|
|
115
|
+
'Registered tool names or their normalized sandbox function names used by the code. ' +
|
|
116
|
+
'Use sandbox names in code, but registered names in tool_manifest when sandbox names collide. ' +
|
|
117
|
+
'Required when direct-only tools are configured; ' +
|
|
116
118
|
'validated before execution starts. Pass [] when the code calls no tools at all.';
|
|
117
119
|
|
|
118
120
|
export function createProgrammaticToolCallingSchema(
|
|
@@ -1127,6 +1129,7 @@ export function createProgrammaticToolCallingTool(
|
|
|
1127
1129
|
allowedToolDefs: toolDefs,
|
|
1128
1130
|
disallowedToolDefs,
|
|
1129
1131
|
programmaticToolName,
|
|
1132
|
+
normalizeIdentifier: normalizeToPythonIdentifier,
|
|
1130
1133
|
});
|
|
1131
1134
|
|
|
1132
1135
|
/* These guard the replay path only. A call that selected no tools never
|
|
@@ -600,6 +600,10 @@ async function runLocalProgrammaticTool(args: {
|
|
|
600
600
|
allowedToolDefs: toolDefs,
|
|
601
601
|
disallowedToolDefs,
|
|
602
602
|
programmaticToolName: runnerName,
|
|
603
|
+
normalizeIdentifier:
|
|
604
|
+
args.runtime === 'bash'
|
|
605
|
+
? normalizeToBashIdentifier
|
|
606
|
+
: normalizeToPythonIdentifier,
|
|
603
607
|
});
|
|
604
608
|
|
|
605
609
|
/* The local program builder supports zero stubs, so a call that selected no
|