@librechat/agents 3.3.5 → 3.3.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/hooks/createWorkspacePolicyHook.cjs +1 -1
- package/dist/cjs/llm/openai/index.cjs +51 -6
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +14 -0
- package/dist/cjs/session/JsonlSessionStore.cjs +1 -1
- package/dist/cjs/stream.cjs +6 -1
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/BashExecutor.cjs +3 -1
- package/dist/cjs/tools/BashExecutor.cjs.map +1 -1
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +2 -0
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/CodeExecutor.cjs +19 -16
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +2 -0
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ReadFile.cjs +8 -4
- package/dist/cjs/tools/ReadFile.cjs.map +1 -1
- package/dist/cjs/tools/SkillTool.cjs +2 -0
- package/dist/cjs/tools/SkillTool.cjs.map +1 -1
- package/dist/cjs/tools/SubagentTool.cjs +3 -0
- package/dist/cjs/tools/SubagentTool.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +88 -27
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/ToolSearch.cjs +5 -1
- package/dist/cjs/tools/ToolSearch.cjs.map +1 -1
- package/dist/cjs/tools/intentArg.cjs +230 -0
- package/dist/cjs/tools/intentArg.cjs.map +1 -0
- package/dist/cjs/tools/local/CompileCheckTool.cjs +3 -2
- package/dist/cjs/tools/local/CompileCheckTool.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalCodingTools.cjs +13 -12
- package/dist/cjs/tools/local/LocalCodingTools.cjs.map +1 -1
- package/dist/cjs/tools/local/LocalExecutionEngine.cjs +2 -2
- package/dist/cjs/tools/ptcTimeout.cjs.map +1 -1
- package/dist/cjs/tools/search/schema.cjs +2 -0
- package/dist/cjs/tools/search/schema.cjs.map +1 -1
- package/dist/cjs/tools/search/tool.cjs +31 -1
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +1 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/tools/toolOutputReferences.cjs +41 -7
- package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -1
- package/dist/esm/hooks/createWorkspacePolicyHook.mjs +1 -1
- package/dist/esm/llm/openai/index.mjs +51 -6
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/main.mjs +4 -3
- package/dist/esm/session/JsonlSessionStore.mjs +1 -1
- package/dist/esm/stream.mjs +6 -1
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/BashExecutor.mjs +3 -1
- package/dist/esm/tools/BashExecutor.mjs.map +1 -1
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs +2 -0
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/CodeExecutor.mjs +19 -16
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +2 -0
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ReadFile.mjs +8 -4
- package/dist/esm/tools/ReadFile.mjs.map +1 -1
- package/dist/esm/tools/SkillTool.mjs +2 -0
- package/dist/esm/tools/SkillTool.mjs.map +1 -1
- package/dist/esm/tools/SubagentTool.mjs +3 -0
- package/dist/esm/tools/SubagentTool.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +88 -28
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/ToolSearch.mjs +5 -1
- package/dist/esm/tools/ToolSearch.mjs.map +1 -1
- package/dist/esm/tools/intentArg.mjs +220 -0
- package/dist/esm/tools/intentArg.mjs.map +1 -0
- package/dist/esm/tools/local/CompileCheckTool.mjs +3 -2
- package/dist/esm/tools/local/CompileCheckTool.mjs.map +1 -1
- package/dist/esm/tools/local/LocalCodingTools.mjs +13 -12
- package/dist/esm/tools/local/LocalCodingTools.mjs.map +1 -1
- package/dist/esm/tools/local/LocalExecutionEngine.mjs +2 -2
- package/dist/esm/tools/ptcTimeout.mjs.map +1 -1
- package/dist/esm/tools/search/schema.mjs +2 -0
- package/dist/esm/tools/search/schema.mjs.map +1 -1
- package/dist/esm/tools/search/tool.mjs +31 -2
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +1 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/tools/toolOutputReferences.mjs +41 -7
- package/dist/esm/tools/toolOutputReferences.mjs.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/llm/openai/index.d.ts +0 -11
- package/dist/types/tools/BashExecutor.d.ts +18 -0
- package/dist/types/tools/CodeExecutor.d.ts +18 -0
- package/dist/types/tools/ReadFile.d.ts +18 -0
- package/dist/types/tools/SkillTool.d.ts +18 -0
- package/dist/types/tools/SubagentTool.d.ts +9 -0
- package/dist/types/tools/ToolNode.d.ts +37 -5
- package/dist/types/tools/ToolSearch.d.ts +18 -0
- package/dist/types/tools/intentArg.d.ts +119 -0
- package/dist/types/tools/ptcTimeout.d.ts +2 -0
- package/dist/types/tools/search/schema.d.ts +18 -0
- package/dist/types/tools/search/tool.d.ts +18 -0
- package/dist/types/tools/toolOutputReferences.d.ts +12 -2
- package/dist/types/types/stream.d.ts +12 -0
- package/dist/types/types/tools.d.ts +27 -2
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/llm/openai/index.ts +74 -10
- package/src/llm/openai/llm.spec.ts +105 -0
- package/src/stream.ts +12 -1
- package/src/tools/BashExecutor.ts +6 -0
- package/src/tools/BashProgrammaticToolCalling.ts +2 -0
- package/src/tools/CodeExecutor.ts +6 -0
- package/src/tools/ProgrammaticToolCalling.ts +2 -0
- package/src/tools/ReadFile.ts +2 -0
- package/src/tools/SkillTool.ts +2 -0
- package/src/tools/SubagentTool.ts +3 -0
- package/src/tools/ToolNode.ts +190 -43
- package/src/tools/ToolSearch.ts +3 -0
- package/src/tools/__tests__/ToolNode.onResultCompletion.test.ts +174 -0
- package/src/tools/__tests__/intentArg.test.ts +281 -0
- package/src/tools/__tests__/intentCoverage.test.ts +139 -0
- package/src/tools/__tests__/toolOutputReferences.test.ts +83 -0
- package/src/tools/intentArg.ts +323 -0
- package/src/tools/local/CompileCheckTool.ts +4 -3
- package/src/tools/local/LocalCodingTools.ts +14 -13
- package/src/tools/ptcTimeout.ts +2 -0
- package/src/tools/search/outcome.test.ts +90 -0
- package/src/tools/search/schema.ts +3 -0
- package/src/tools/search/tool.ts +47 -1
- package/src/tools/subagent/SubagentExecutor.ts +2 -1
- package/src/tools/toolOutputReferences.ts +77 -9
- package/src/types/stream.ts +12 -0
- package/src/types/tools.ts +24 -2
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Tool intent labels.
|
|
3
|
+
*
|
|
4
|
+
* Lets a tool declare, as the FIRST property of its input schema, an `intent`
|
|
5
|
+
* string: one model-authored sentence stating what that specific call is about
|
|
6
|
+
* to do ("Searching for OAuth handling in the callback router"). Because the
|
|
7
|
+
* property is first, it is the first key providers stream in the tool-call
|
|
8
|
+
* args, so a host UI can render it as the call's live status label before the
|
|
9
|
+
* rest of the args exist. When the call settles, {@link applyOutcome} edits
|
|
10
|
+
* the sentence in place into its outcome form — a tool-supplied replacement
|
|
11
|
+
* (`outcome`), a tool-supplied span edit (`outcome_patch`), or a mechanical
|
|
12
|
+
* present-progressive→past-tense transform of the leading verb.
|
|
13
|
+
*
|
|
14
|
+
* The arg is always optional (never listed in `required`): the same schemas
|
|
15
|
+
* are callable from programmatic tool calling, where no UI renders a label
|
|
16
|
+
* and forcing generated code to fabricate one would be pure cost. Tool bodies
|
|
17
|
+
* must call {@link stripIntent} before using their args so no tool receives a
|
|
18
|
+
* parameter it did not declare.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { JsonSchemaType, OutcomePatch } from '@/types';
|
|
22
|
+
|
|
23
|
+
/** Argument carrying the model-authored label for a tool call. */
|
|
24
|
+
export const INTENT_ARG = 'intent';
|
|
25
|
+
|
|
26
|
+
/** Model-facing instruction for the injected `intent` property. */
|
|
27
|
+
export const INTENT_DESCRIPTION =
|
|
28
|
+
'ALWAYS write this field FIRST, before any other argument. One short sentence, ' +
|
|
29
|
+
'present progressive, stating what this specific call is about to do: ' +
|
|
30
|
+
'"Searching for OAuth handling in the callback router". It is shown to the user ' +
|
|
31
|
+
'as the live status label for this call while it runs, so write it for a human ' +
|
|
32
|
+
'reading a progress line. Do not restate the tool name. Do not exceed one sentence. ' +
|
|
33
|
+
'When you make several calls to the same tool in one turn, each intent must ' +
|
|
34
|
+
'distinguish that call from its siblings.';
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Canonical (frozen) shape of the injected property. Always embed a COPY
|
|
38
|
+
* (`{ ...INTENT_PROPERTY }`): LangChain's JSON-schema validator stamps a
|
|
39
|
+
* `__absolute_uri__` marker onto every subschema it dereferences, which
|
|
40
|
+
* throws on a frozen object — and a single shared instance would be stamped
|
|
41
|
+
* with one schema's URI while embedded in many.
|
|
42
|
+
*/
|
|
43
|
+
export const INTENT_PROPERTY: JsonSchemaType = Object.freeze<JsonSchemaType>({
|
|
44
|
+
type: 'string',
|
|
45
|
+
description: INTENT_DESCRIPTION,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Discriminates the intent LABEL property from a tool's own business
|
|
50
|
+
* parameter that merely shares the name: the label contract always opens
|
|
51
|
+
* with the same instruction. Removal/sanitize passes must never strip a
|
|
52
|
+
* parameter the tool actually needs.
|
|
53
|
+
*/
|
|
54
|
+
export function isIntentLabelProperty(property: unknown): boolean {
|
|
55
|
+
if (property == null || typeof property !== 'object') {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
const record = property as { type?: unknown; description?: unknown };
|
|
59
|
+
return (
|
|
60
|
+
record.type === 'string' &&
|
|
61
|
+
typeof record.description === 'string' &&
|
|
62
|
+
record.description.startsWith('ALWAYS write this field FIRST')
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Returns a copy of the parameters schema with `intent` prepended as the
|
|
68
|
+
* FIRST property (object key order is insertion order and every provider
|
|
69
|
+
* serializer preserves it — first key in the schema means first key in the
|
|
70
|
+
* streamed input). Never mutates the input; no-op when the schema already
|
|
71
|
+
* declares `intent`. The property is not added to `required`.
|
|
72
|
+
*/
|
|
73
|
+
export function withIntent(parameters?: JsonSchemaType): JsonSchemaType {
|
|
74
|
+
const existingProps = parameters?.properties ?? {};
|
|
75
|
+
if (INTENT_ARG in existingProps) {
|
|
76
|
+
return parameters as JsonSchemaType;
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
...parameters,
|
|
80
|
+
type: 'object',
|
|
81
|
+
properties: { [INTENT_ARG]: { ...INTENT_PROPERTY }, ...existingProps },
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Coerces tool-call args to an object, parsing a stringified JSON object
|
|
87
|
+
* (some providers deliver args as a string). Returns undefined otherwise.
|
|
88
|
+
*/
|
|
89
|
+
function coerceArgsObject(args: unknown): Record<string, unknown> | undefined {
|
|
90
|
+
if (typeof args === 'object' && args !== null && !Array.isArray(args)) {
|
|
91
|
+
return args as Record<string, unknown>;
|
|
92
|
+
}
|
|
93
|
+
if (typeof args === 'string' && args.trim().startsWith('{')) {
|
|
94
|
+
try {
|
|
95
|
+
const parsed = JSON.parse(args) as unknown;
|
|
96
|
+
if (parsed != null && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
97
|
+
return parsed as Record<string, unknown>;
|
|
98
|
+
}
|
|
99
|
+
} catch {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Reads the model-authored intent from tool-call args (handles stringified
|
|
108
|
+
* args). Returns undefined when absent, empty, or not a string.
|
|
109
|
+
*/
|
|
110
|
+
export function readIntent(args: unknown): string | undefined {
|
|
111
|
+
const value = coerceArgsObject(args)?.[INTENT_ARG];
|
|
112
|
+
if (typeof value !== 'string') {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
const trimmed = value.trim();
|
|
116
|
+
return trimmed === '' ? undefined : trimmed;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Returns the args without the `intent` key so downstream consumers that did
|
|
121
|
+
* not declare it never receive it. Parses stringified JSON object args;
|
|
122
|
+
* returns the value unchanged when the key is absent.
|
|
123
|
+
*/
|
|
124
|
+
export function stripIntent(args: unknown): unknown {
|
|
125
|
+
const obj = coerceArgsObject(args);
|
|
126
|
+
if (!obj || !(INTENT_ARG in obj)) {
|
|
127
|
+
return args;
|
|
128
|
+
}
|
|
129
|
+
const { [INTENT_ARG]: _omit, ...rest } = obj;
|
|
130
|
+
return rest;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Leading-verb map for the mechanical outcome transform, keyed by the
|
|
135
|
+
* lowercased first word of the intent. Deliberately small: an unknown leading
|
|
136
|
+
* word leaves the intent unchanged rather than mangling it.
|
|
137
|
+
*/
|
|
138
|
+
const OUTCOME_VERB_MAP: ReadonlyMap<string, string> = new Map([
|
|
139
|
+
['searching', 'Searched'],
|
|
140
|
+
['reading', 'Read'],
|
|
141
|
+
['writing', 'Wrote'],
|
|
142
|
+
['editing', 'Edited'],
|
|
143
|
+
['running', 'Ran'],
|
|
144
|
+
['creating', 'Created'],
|
|
145
|
+
['checking', 'Checked'],
|
|
146
|
+
['fetching', 'Fetched'],
|
|
147
|
+
['listing', 'Listed'],
|
|
148
|
+
['looking', 'Looked'],
|
|
149
|
+
['building', 'Built'],
|
|
150
|
+
['deleting', 'Deleted'],
|
|
151
|
+
['updating', 'Updated'],
|
|
152
|
+
['adding', 'Added'],
|
|
153
|
+
['removing', 'Removed'],
|
|
154
|
+
['verifying', 'Verified'],
|
|
155
|
+
['analyzing', 'Analyzed'],
|
|
156
|
+
['generating', 'Generated'],
|
|
157
|
+
['delegating', 'Delegated'],
|
|
158
|
+
['spawning', 'Spawned'],
|
|
159
|
+
['compiling', 'Compiled'],
|
|
160
|
+
['grepping', 'Grepped'],
|
|
161
|
+
]);
|
|
162
|
+
|
|
163
|
+
function matchLeadingCase(replacement: string, original: string): string {
|
|
164
|
+
if (original.charAt(0) === original.charAt(0).toLowerCase()) {
|
|
165
|
+
return replacement.charAt(0).toLowerCase() + replacement.slice(1);
|
|
166
|
+
}
|
|
167
|
+
return replacement;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function transformLeadingVerb(intent: string): string {
|
|
171
|
+
const spaceIdx = intent.search(/\s/);
|
|
172
|
+
const leading = spaceIdx === -1 ? intent : intent.slice(0, spaceIdx);
|
|
173
|
+
const mapped = OUTCOME_VERB_MAP.get(leading.toLowerCase());
|
|
174
|
+
if (mapped == null) {
|
|
175
|
+
return intent;
|
|
176
|
+
}
|
|
177
|
+
return matchLeadingCase(mapped, leading) + intent.slice(leading.length);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Resolves the settled label for a call from its model-authored `intent` and
|
|
182
|
+
* the tool's result fields, in precedence order:
|
|
183
|
+
*
|
|
184
|
+
* 1. `outcome` — full replacement authored by the tool.
|
|
185
|
+
* 2. `outcome_patch` — first occurrence of `from` in the intent replaced
|
|
186
|
+
* with `to` (case-sensitive); no-op when `from` is absent or empty.
|
|
187
|
+
* 3. Mechanical transform — the leading word mapped present-progressive →
|
|
188
|
+
* past tense; an unknown leading word leaves the intent unchanged.
|
|
189
|
+
*
|
|
190
|
+
* Returns undefined when there is neither an intent nor an outcome, so
|
|
191
|
+
* callers fall back to their default label. Pure and dependency-free — host
|
|
192
|
+
* UIs needing identical logic can import or mirror it.
|
|
193
|
+
*/
|
|
194
|
+
export function applyOutcome(
|
|
195
|
+
intent: string | undefined,
|
|
196
|
+
result?: { outcome?: string; outcome_patch?: OutcomePatch },
|
|
197
|
+
): string | undefined {
|
|
198
|
+
const outcome = result?.outcome;
|
|
199
|
+
if (typeof outcome === 'string' && outcome.trim() !== '') {
|
|
200
|
+
return outcome;
|
|
201
|
+
}
|
|
202
|
+
if (intent == null || intent === '') {
|
|
203
|
+
return undefined;
|
|
204
|
+
}
|
|
205
|
+
const patch = result?.outcome_patch;
|
|
206
|
+
if (patch != null && patch.from !== '' && intent.includes(patch.from)) {
|
|
207
|
+
/** Replacement callback keeps `to` verbatim — a direct string second
|
|
208
|
+
* argument would interpret `$&`/`$'`-style tokens in tool-authored
|
|
209
|
+
* text (e.g. labels derived from shell syntax). */
|
|
210
|
+
return intent.replace(patch.from, () => patch.to);
|
|
211
|
+
}
|
|
212
|
+
return transformLeadingVerb(intent);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Hard cap on an emitted outcome label. The label is a single progress line
|
|
217
|
+
* in UI chrome; a tool that derives it from data (or a malformed patch)
|
|
218
|
+
* must not be able to inflate completion events or persisted parts.
|
|
219
|
+
*/
|
|
220
|
+
const MAX_OUTCOME_CHARS = 256;
|
|
221
|
+
|
|
222
|
+
function boundOutcomeLabel(label: string | undefined): string | undefined {
|
|
223
|
+
if (label == null) {
|
|
224
|
+
return undefined;
|
|
225
|
+
}
|
|
226
|
+
const singleLine = label.replace(/\s+/g, ' ').trim();
|
|
227
|
+
if (singleLine === '') {
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
if (singleLine.length <= MAX_OUTCOME_CHARS) {
|
|
231
|
+
return singleLine;
|
|
232
|
+
}
|
|
233
|
+
return `${singleLine.slice(0, MAX_OUTCOME_CHARS - 1)}…`;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Resolves the settled label to emit on a completion event: only when the
|
|
238
|
+
* tool actually authored `outcome`/`outcome_patch` fields. Returns undefined
|
|
239
|
+
* otherwise — the mechanical transform of a bare intent is left to the host
|
|
240
|
+
* so the wire never carries a label the host can derive itself. The result
|
|
241
|
+
* is collapsed to a bounded single line before emission.
|
|
242
|
+
*
|
|
243
|
+
* For failed calls (`isError`), only tool-AUTHORED text may label the call:
|
|
244
|
+
* an explicit `outcome`, or a patch whose `from` actually matches the
|
|
245
|
+
* intent. An unmatched patch must not fall through to the mechanical
|
|
246
|
+
* past-tense transform — wording drift in a failure patch would otherwise
|
|
247
|
+
* render a success-looking label for an error.
|
|
248
|
+
*/
|
|
249
|
+
export function resolveToolOutcome(
|
|
250
|
+
args: unknown,
|
|
251
|
+
fields?: { outcome?: string; outcome_patch?: OutcomePatch } | null,
|
|
252
|
+
options?: { isError?: boolean },
|
|
253
|
+
): string | undefined {
|
|
254
|
+
if (fields == null || (fields.outcome == null && fields.outcome_patch == null)) {
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
if (options?.isError !== true) {
|
|
258
|
+
return boundOutcomeLabel(applyOutcome(readIntent(args), fields));
|
|
259
|
+
}
|
|
260
|
+
const outcome = fields.outcome;
|
|
261
|
+
if (typeof outcome === 'string' && outcome.trim() !== '') {
|
|
262
|
+
return boundOutcomeLabel(outcome);
|
|
263
|
+
}
|
|
264
|
+
const intent = readIntent(args);
|
|
265
|
+
const patch = fields.outcome_patch;
|
|
266
|
+
if (
|
|
267
|
+
intent != null &&
|
|
268
|
+
patch != null &&
|
|
269
|
+
patch.from !== '' &&
|
|
270
|
+
intent.includes(patch.from)
|
|
271
|
+
) {
|
|
272
|
+
return boundOutcomeLabel(intent.replace(patch.from, () => patch.to));
|
|
273
|
+
}
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Reads the outcome fields off a tool-execution result: the typed
|
|
279
|
+
* `outcome`/`outcome_patch` fields when present, else the artifact channel
|
|
280
|
+
* (see {@link readOutcomeFields}) — so a `content_and_artifact` tool authors
|
|
281
|
+
* its label the same way on the direct and event-driven paths.
|
|
282
|
+
*/
|
|
283
|
+
export function outcomeFieldsFromResult(result: {
|
|
284
|
+
outcome?: string;
|
|
285
|
+
outcome_patch?: OutcomePatch;
|
|
286
|
+
artifact?: unknown;
|
|
287
|
+
}): { outcome?: string; outcome_patch?: OutcomePatch } | undefined {
|
|
288
|
+
if (result.outcome != null || result.outcome_patch != null) {
|
|
289
|
+
return result;
|
|
290
|
+
}
|
|
291
|
+
return readOutcomeFields(result.artifact);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Extracts validated `outcome`/`outcome_patch` fields from an arbitrary
|
|
296
|
+
* value — the artifact channel through which an in-process
|
|
297
|
+
* `content_and_artifact` tool authors its settled label. Returns undefined
|
|
298
|
+
* when neither field is usable.
|
|
299
|
+
*/
|
|
300
|
+
export function readOutcomeFields(
|
|
301
|
+
source: unknown,
|
|
302
|
+
): { outcome?: string; outcome_patch?: OutcomePatch } | undefined {
|
|
303
|
+
if (source == null || typeof source !== 'object' || Array.isArray(source)) {
|
|
304
|
+
return undefined;
|
|
305
|
+
}
|
|
306
|
+
const record = source as Record<string, unknown>;
|
|
307
|
+
const outcome =
|
|
308
|
+
typeof record.outcome === 'string' && record.outcome.trim() !== ''
|
|
309
|
+
? record.outcome
|
|
310
|
+
: undefined;
|
|
311
|
+
let outcome_patch: OutcomePatch | undefined;
|
|
312
|
+
const rawPatch = record.outcome_patch;
|
|
313
|
+
if (rawPatch != null && typeof rawPatch === 'object' && !Array.isArray(rawPatch)) {
|
|
314
|
+
const patch = rawPatch as Record<string, unknown>;
|
|
315
|
+
if (typeof patch.from === 'string' && typeof patch.to === 'string') {
|
|
316
|
+
outcome_patch = { from: patch.from, to: patch.to };
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
if (outcome == null && outcome_patch == null) {
|
|
320
|
+
return undefined;
|
|
321
|
+
}
|
|
322
|
+
return { outcome, outcome_patch };
|
|
323
|
+
}
|
|
@@ -27,7 +27,6 @@ import { resolve } from 'path';
|
|
|
27
27
|
import { tool } from '@langchain/core/tools';
|
|
28
28
|
import type { DynamicStructuredTool } from '@langchain/core/tools';
|
|
29
29
|
import type { WorkspaceFS } from './workspaceFS';
|
|
30
|
-
import { isWorkspaceClientTimeoutError } from './workspaceFS';
|
|
31
30
|
import type * as t from '@/types';
|
|
32
31
|
import {
|
|
33
32
|
getLocalCwd,
|
|
@@ -36,12 +35,14 @@ import {
|
|
|
36
35
|
truncateLocalOutput,
|
|
37
36
|
validateBashCommand,
|
|
38
37
|
} from './LocalExecutionEngine';
|
|
38
|
+
import { isWorkspaceClientTimeoutError } from './workspaceFS';
|
|
39
|
+
import { withIntent } from '@/tools/intentArg';
|
|
39
40
|
import { Constants } from '@/common';
|
|
40
41
|
|
|
41
42
|
/** Back-compat alias; canonical name lives on `Constants.COMPILE_CHECK`. */
|
|
42
43
|
export const CompileCheckToolName = Constants.COMPILE_CHECK;
|
|
43
44
|
|
|
44
|
-
const CompileCheckSchema: t.JsonSchemaType = {
|
|
45
|
+
const CompileCheckSchema: t.JsonSchemaType = withIntent({
|
|
45
46
|
type: 'object',
|
|
46
47
|
properties: {
|
|
47
48
|
command: {
|
|
@@ -55,7 +56,7 @@ const CompileCheckSchema: t.JsonSchemaType = {
|
|
|
55
56
|
'Optional timeout in milliseconds. Defaults to 120000 (2 min).',
|
|
56
57
|
},
|
|
57
58
|
},
|
|
58
|
-
};
|
|
59
|
+
});
|
|
59
60
|
|
|
60
61
|
type DetectedKind =
|
|
61
62
|
| 'typescript'
|
|
@@ -3,7 +3,6 @@ import { createTwoFilesPatch } from 'diff';
|
|
|
3
3
|
import { tool } from '@langchain/core/tools';
|
|
4
4
|
import type { DynamicStructuredTool } from '@langchain/core/tools';
|
|
5
5
|
import type * as t from '@/types';
|
|
6
|
-
import { isWorkspaceClientTimeoutError } from './workspaceFS';
|
|
7
6
|
import {
|
|
8
7
|
createLocalBashProgrammaticToolCallingTool,
|
|
9
8
|
createLocalProgrammaticToolCallingTool,
|
|
@@ -25,9 +24,11 @@ import {
|
|
|
25
24
|
} from './CompileCheckTool';
|
|
26
25
|
import { classifyAttachment, imageAttachmentContent } from './attachments';
|
|
27
26
|
import { createLocalFileCheckpointer } from './FileCheckpointer';
|
|
27
|
+
import { isWorkspaceClientTimeoutError } from './workspaceFS';
|
|
28
28
|
import { applyEdit, locateEdit } from './editStrategies';
|
|
29
29
|
import { decodeFile, encodeFile } from './textEncoding';
|
|
30
30
|
import { runPostEditSyntaxCheck } from './syntaxCheck';
|
|
31
|
+
import { withIntent } from '@/tools/intentArg';
|
|
31
32
|
import { Constants } from '@/common';
|
|
32
33
|
|
|
33
34
|
const MAX_READ_CHARS = 256000;
|
|
@@ -47,7 +48,7 @@ export const LocalGrepSearchToolName = Constants.GREP_SEARCH;
|
|
|
47
48
|
export const LocalGlobSearchToolName = Constants.GLOB_SEARCH;
|
|
48
49
|
export const LocalListDirectoryToolName = Constants.LIST_DIRECTORY;
|
|
49
50
|
|
|
50
|
-
export const LocalReadFileToolSchema: t.JsonSchemaType = {
|
|
51
|
+
export const LocalReadFileToolSchema: t.JsonSchemaType = withIntent({
|
|
51
52
|
type: 'object',
|
|
52
53
|
properties: {
|
|
53
54
|
path: {
|
|
@@ -65,9 +66,9 @@ export const LocalReadFileToolSchema: t.JsonSchemaType = {
|
|
|
65
66
|
},
|
|
66
67
|
},
|
|
67
68
|
required: ['path'],
|
|
68
|
-
};
|
|
69
|
+
});
|
|
69
70
|
|
|
70
|
-
export const LocalWriteFileToolSchema: t.JsonSchemaType = {
|
|
71
|
+
export const LocalWriteFileToolSchema: t.JsonSchemaType = withIntent({
|
|
71
72
|
type: 'object',
|
|
72
73
|
properties: {
|
|
73
74
|
path: {
|
|
@@ -81,9 +82,9 @@ export const LocalWriteFileToolSchema: t.JsonSchemaType = {
|
|
|
81
82
|
},
|
|
82
83
|
},
|
|
83
84
|
required: ['path', 'content'],
|
|
84
|
-
};
|
|
85
|
+
});
|
|
85
86
|
|
|
86
|
-
export const LocalEditFileToolSchema: t.JsonSchemaType = {
|
|
87
|
+
export const LocalEditFileToolSchema: t.JsonSchemaType = withIntent({
|
|
87
88
|
type: 'object',
|
|
88
89
|
properties: {
|
|
89
90
|
path: {
|
|
@@ -114,9 +115,9 @@ export const LocalEditFileToolSchema: t.JsonSchemaType = {
|
|
|
114
115
|
},
|
|
115
116
|
},
|
|
116
117
|
required: ['path'],
|
|
117
|
-
};
|
|
118
|
+
});
|
|
118
119
|
|
|
119
|
-
export const LocalGrepSearchToolSchema: t.JsonSchemaType = {
|
|
120
|
+
export const LocalGrepSearchToolSchema: t.JsonSchemaType = withIntent({
|
|
120
121
|
type: 'object',
|
|
121
122
|
properties: {
|
|
122
123
|
pattern: {
|
|
@@ -137,9 +138,9 @@ export const LocalGrepSearchToolSchema: t.JsonSchemaType = {
|
|
|
137
138
|
},
|
|
138
139
|
},
|
|
139
140
|
required: ['pattern'],
|
|
140
|
-
};
|
|
141
|
+
});
|
|
141
142
|
|
|
142
|
-
export const LocalGlobSearchToolSchema: t.JsonSchemaType = {
|
|
143
|
+
export const LocalGlobSearchToolSchema: t.JsonSchemaType = withIntent({
|
|
143
144
|
type: 'object',
|
|
144
145
|
properties: {
|
|
145
146
|
pattern: {
|
|
@@ -156,9 +157,9 @@ export const LocalGlobSearchToolSchema: t.JsonSchemaType = {
|
|
|
156
157
|
},
|
|
157
158
|
},
|
|
158
159
|
required: ['pattern'],
|
|
159
|
-
};
|
|
160
|
+
});
|
|
160
161
|
|
|
161
|
-
export const LocalListDirectoryToolSchema: t.JsonSchemaType = {
|
|
162
|
+
export const LocalListDirectoryToolSchema: t.JsonSchemaType = withIntent({
|
|
162
163
|
type: 'object',
|
|
163
164
|
properties: {
|
|
164
165
|
path: {
|
|
@@ -166,7 +167,7 @@ export const LocalListDirectoryToolSchema: t.JsonSchemaType = {
|
|
|
166
167
|
description: 'Directory to list. Defaults to cwd.',
|
|
167
168
|
},
|
|
168
169
|
},
|
|
169
|
-
};
|
|
170
|
+
});
|
|
170
171
|
|
|
171
172
|
function lineWindow(
|
|
172
173
|
content: string,
|
package/src/tools/ptcTimeout.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { JsonSchemaType } from '@/types';
|
|
1
2
|
import { EnvVar } from '@/common';
|
|
2
3
|
|
|
3
4
|
export const DEFAULT_CODE_API_RUN_TIMEOUT_MS = 15_000;
|
|
@@ -15,6 +16,7 @@ type TimeoutSchema = {
|
|
|
15
16
|
export type ProgrammaticToolCallingJsonSchema = {
|
|
16
17
|
type: 'object';
|
|
17
18
|
properties: {
|
|
19
|
+
intent: JsonSchemaType;
|
|
18
20
|
code: {
|
|
19
21
|
type: 'string';
|
|
20
22
|
minLength: number;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, it, expect } from '@jest/globals';
|
|
2
|
+
import type * as t from './types';
|
|
3
|
+
import { resolveSearchOutcome } from './tool';
|
|
4
|
+
|
|
5
|
+
const data = (partial: Partial<t.SearchResultData>): t.SearchResultData =>
|
|
6
|
+
({ turn: 0, ...partial }) as t.SearchResultData;
|
|
7
|
+
|
|
8
|
+
describe('resolveSearchOutcome', () => {
|
|
9
|
+
it('authors a FAILURE label when the processor caught an error', () => {
|
|
10
|
+
expect(
|
|
11
|
+
resolveSearchOutcome(
|
|
12
|
+
data({ error: 'provider timeout', organic: [] }),
|
|
13
|
+
'oauth handling'
|
|
14
|
+
)
|
|
15
|
+
).toBe('Search failed for "oauth handling"');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('prefers the failure label over any partial results', () => {
|
|
19
|
+
expect(
|
|
20
|
+
resolveSearchOutcome(
|
|
21
|
+
data({
|
|
22
|
+
error: 'partial failure',
|
|
23
|
+
organic: [{ link: 'a' }] as t.SearchResultData['organic'],
|
|
24
|
+
}),
|
|
25
|
+
'oauth'
|
|
26
|
+
)
|
|
27
|
+
).toBe('Search failed for "oauth"');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('ignores an empty-string error', () => {
|
|
31
|
+
expect(
|
|
32
|
+
resolveSearchOutcome(
|
|
33
|
+
data({
|
|
34
|
+
error: '',
|
|
35
|
+
organic: [{ link: 'a' }] as t.SearchResultData['organic'],
|
|
36
|
+
}),
|
|
37
|
+
'oauth'
|
|
38
|
+
)
|
|
39
|
+
).toBe('Found 1 result for "oauth"');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('counts every rendered collection kind', () => {
|
|
43
|
+
expect(
|
|
44
|
+
resolveSearchOutcome(
|
|
45
|
+
data({
|
|
46
|
+
organic: [
|
|
47
|
+
{ link: 'a' },
|
|
48
|
+
{ link: 'b' },
|
|
49
|
+
] as t.SearchResultData['organic'],
|
|
50
|
+
topStories: [{ link: 'c' }] as t.SearchResultData['topStories'],
|
|
51
|
+
images: [{ imageUrl: 'd' }] as t.SearchResultData['images'],
|
|
52
|
+
videos: [{ link: 'e' }] as t.SearchResultData['videos'],
|
|
53
|
+
places: [{ name: 'f' }] as t.SearchResultData['places'],
|
|
54
|
+
peopleAlsoAsk: [
|
|
55
|
+
{ question: 'g' },
|
|
56
|
+
] as t.SearchResultData['peopleAlsoAsk'],
|
|
57
|
+
}),
|
|
58
|
+
'oauth'
|
|
59
|
+
)
|
|
60
|
+
).toBe('Found 7 results for "oauth"');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('counts singleton structured results (knowledge graph / answer box)', () => {
|
|
64
|
+
expect(
|
|
65
|
+
resolveSearchOutcome(
|
|
66
|
+
data({
|
|
67
|
+
knowledgeGraph: {
|
|
68
|
+
title: 'OAuth',
|
|
69
|
+
} as t.SearchResultData['knowledgeGraph'],
|
|
70
|
+
}),
|
|
71
|
+
'oauth'
|
|
72
|
+
)
|
|
73
|
+
).toBe('Found 1 result for "oauth"');
|
|
74
|
+
expect(
|
|
75
|
+
resolveSearchOutcome(
|
|
76
|
+
data({
|
|
77
|
+
answerBox: { snippet: 'x' } as t.SearchResultData['answerBox'],
|
|
78
|
+
knowledgeGraph: {
|
|
79
|
+
title: 'OAuth',
|
|
80
|
+
} as t.SearchResultData['knowledgeGraph'],
|
|
81
|
+
}),
|
|
82
|
+
'oauth'
|
|
83
|
+
)
|
|
84
|
+
).toBe('Found 2 results for "oauth"');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('leaves a genuine zero-result search to the mechanical transform', () => {
|
|
88
|
+
expect(resolveSearchOutcome(data({ organic: [] }), 'oauth')).toBeUndefined();
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { INTENT_PROPERTY } from '@/tools/intentArg';
|
|
2
|
+
|
|
1
3
|
export enum DATE_RANGE {
|
|
2
4
|
PAST_HOUR = 'h',
|
|
3
5
|
PAST_24_HOURS = 'd',
|
|
@@ -71,6 +73,7 @@ export const newsSchema = {
|
|
|
71
73
|
export const WebSearchToolSchema = {
|
|
72
74
|
type: 'object',
|
|
73
75
|
properties: {
|
|
76
|
+
intent: { ...INTENT_PROPERTY },
|
|
74
77
|
query: querySchema,
|
|
75
78
|
date: dateSchema,
|
|
76
79
|
country: countrySchema,
|
package/src/tools/search/tool.ts
CHANGED
|
@@ -17,6 +17,7 @@ import { createKeenableScraper } from './keenable-scraper';
|
|
|
17
17
|
import { createSerperScraper } from './serper-scraper';
|
|
18
18
|
import { createTavilyScraper } from './tavily-scraper';
|
|
19
19
|
import { createFirecrawlScraper } from './firecrawl';
|
|
20
|
+
import { INTENT_PROPERTY } from '@/tools/intentArg';
|
|
20
21
|
import { createCrwScraper } from './crw-scraper';
|
|
21
22
|
import { expandHighlights } from './highlights';
|
|
22
23
|
import { formatResultsForLLM } from './format';
|
|
@@ -24,6 +25,46 @@ import { createDefaultLogger } from './utils';
|
|
|
24
25
|
import { createReranker } from './rerankers';
|
|
25
26
|
import { Constants } from '@/common';
|
|
26
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Settled label for a `web_search` call's intent (see `intentArg.ts`).
|
|
30
|
+
*
|
|
31
|
+
* Counts the result kinds `formatResultsForLLM` actually renders —
|
|
32
|
+
* `references` only tracks links embedded in extracted highlights, so it
|
|
33
|
+
* undercounts ordinary results and can overcount when one highlight embeds
|
|
34
|
+
* several links.
|
|
35
|
+
*
|
|
36
|
+
* A caught provider or processing failure is reported through `data.error`
|
|
37
|
+
* while the tool still returns NORMALLY, so that case must author its own
|
|
38
|
+
* label: the `ToolMessage` carries success status, and a bare intent would
|
|
39
|
+
* otherwise settle mechanically from "Searching…" to "Searched…" and present
|
|
40
|
+
* a failed search as a successful one.
|
|
41
|
+
*
|
|
42
|
+
* Returns undefined for a genuine zero-result search, leaving the host's
|
|
43
|
+
* mechanical past-tense transform to label it.
|
|
44
|
+
*/
|
|
45
|
+
export function resolveSearchOutcome(
|
|
46
|
+
data: t.SearchResultData,
|
|
47
|
+
query: string
|
|
48
|
+
): string | undefined {
|
|
49
|
+
if (data.error != null && data.error !== '') {
|
|
50
|
+
return `Search failed for "${query}"`;
|
|
51
|
+
}
|
|
52
|
+
const count =
|
|
53
|
+
(data.organic?.length ?? 0) +
|
|
54
|
+
(data.topStories?.length ?? 0) +
|
|
55
|
+
(data.news?.length ?? 0) +
|
|
56
|
+
(data.images?.length ?? 0) +
|
|
57
|
+
(data.videos?.length ?? 0) +
|
|
58
|
+
(data.places?.length ?? 0) +
|
|
59
|
+
(data.peopleAlsoAsk?.length ?? 0) +
|
|
60
|
+
(data.knowledgeGraph != null ? 1 : 0) +
|
|
61
|
+
(data.answerBox != null ? 1 : 0);
|
|
62
|
+
if (count === 0) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
return `Found ${count} result${count === 1 ? '' : 's'} for "${query}"`;
|
|
66
|
+
}
|
|
67
|
+
|
|
27
68
|
/**
|
|
28
69
|
* Executes parallel searches and merges the results,
|
|
29
70
|
* deduplicating top stories by link
|
|
@@ -335,7 +376,11 @@ function createTool({
|
|
|
335
376
|
maxOutputChars
|
|
336
377
|
);
|
|
337
378
|
const data: t.SearchResultData = { turn, ...searchResult, references };
|
|
338
|
-
|
|
379
|
+
const outcome = resolveSearchOutcome(data, query);
|
|
380
|
+
return [
|
|
381
|
+
output,
|
|
382
|
+
{ [Constants.WEB_SEARCH]: data, ...(outcome != null && { outcome }) },
|
|
383
|
+
];
|
|
339
384
|
},
|
|
340
385
|
{
|
|
341
386
|
name: WebSearchToolName,
|
|
@@ -421,6 +466,7 @@ export const createSearchTool = (
|
|
|
421
466
|
: tavilySearchOptions;
|
|
422
467
|
|
|
423
468
|
const schemaProperties: Record<string, unknown> = {
|
|
469
|
+
intent: { ...INTENT_PROPERTY },
|
|
424
470
|
query: querySchema,
|
|
425
471
|
date: dateSchema,
|
|
426
472
|
images: imagesSchema,
|
|
@@ -115,7 +115,7 @@ type SanitizedStepCompleted =
|
|
|
115
115
|
};
|
|
116
116
|
|
|
117
117
|
type SanitizedProcessedToolCall = Partial<
|
|
118
|
-
Pick<ProcessedToolCall, 'args' | 'id' | 'name' | 'output' | 'progress'>
|
|
118
|
+
Pick<ProcessedToolCall, 'args' | 'id' | 'name' | 'output' | 'progress' | 'outcome'>
|
|
119
119
|
>;
|
|
120
120
|
|
|
121
121
|
type SanitizedRunStepCompleted = {
|
|
@@ -1195,6 +1195,7 @@ function sanitizeProcessedToolCall(
|
|
|
1195
1195
|
sanitized.args = call.args;
|
|
1196
1196
|
}
|
|
1197
1197
|
assignString(sanitized, 'output', call.output);
|
|
1198
|
+
assignString(sanitized, 'outcome', call.outcome);
|
|
1198
1199
|
assignNumber(sanitized, 'progress', call.progress);
|
|
1199
1200
|
return sanitized;
|
|
1200
1201
|
}
|