@ai-sdk-tool/parser 4.1.21 → 4.1.22
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/{chunk-NU2CVJEB.js → chunk-2I56ZDEY.js} +2 -2
- package/dist/{chunk-NU2CVJEB.js.map → chunk-2I56ZDEY.js.map} +1 -1
- package/dist/{chunk-GAJX7HXU.js → chunk-DKZ4GAQM.js} +314 -318
- package/dist/chunk-DKZ4GAQM.js.map +1 -0
- package/dist/community.cjs +313 -317
- package/dist/community.cjs.map +1 -1
- package/dist/community.js +2 -2
- package/dist/index.cjs +313 -317
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +2 -2
- package/dist/rxml.cjs +1 -1
- package/dist/rxml.cjs.map +1 -1
- package/dist/rxml.js +1 -1
- package/package.json +13 -13
- package/dist/chunk-GAJX7HXU.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -2385,10 +2385,10 @@ function findTopLevelPropertyValueStart(text, property) {
|
|
|
2385
2385
|
function extractTopLevelStringProperty(text, property) {
|
|
2386
2386
|
const valueStart = findTopLevelPropertyValueStart(text, property);
|
|
2387
2387
|
if (valueStart == null || valueStart >= text.length) {
|
|
2388
|
-
return
|
|
2388
|
+
return;
|
|
2389
2389
|
}
|
|
2390
2390
|
if (text.charAt(valueStart) !== '"') {
|
|
2391
|
-
return
|
|
2391
|
+
return;
|
|
2392
2392
|
}
|
|
2393
2393
|
let valueEnd = valueStart + 1;
|
|
2394
2394
|
let escaped = false;
|
|
@@ -2403,7 +2403,7 @@ function extractTopLevelStringProperty(text, property) {
|
|
|
2403
2403
|
}
|
|
2404
2404
|
valueEnd += 1;
|
|
2405
2405
|
}
|
|
2406
|
-
return
|
|
2406
|
+
return;
|
|
2407
2407
|
}
|
|
2408
2408
|
function extractJsonValueSlice(text, valueStart) {
|
|
2409
2409
|
if (valueStart >= text.length) {
|
|
@@ -2567,7 +2567,7 @@ function emitToolCallFromParsed(state, controller, parsedToolCall, tools) {
|
|
|
2567
2567
|
}
|
|
2568
2568
|
function canonicalizeArgumentsProgressInput(progress, toolName, tools) {
|
|
2569
2569
|
if (progress.argumentsText === void 0 || !progress.argumentsComplete) {
|
|
2570
|
-
return
|
|
2570
|
+
return;
|
|
2571
2571
|
}
|
|
2572
2572
|
try {
|
|
2573
2573
|
const parsedArguments = parse(
|
|
@@ -2580,7 +2580,7 @@ function canonicalizeArgumentsProgressInput(progress, toolName, tools) {
|
|
|
2580
2580
|
fallback: canonicalizeToolInput
|
|
2581
2581
|
});
|
|
2582
2582
|
} catch (e) {
|
|
2583
|
-
return
|
|
2583
|
+
return;
|
|
2584
2584
|
}
|
|
2585
2585
|
}
|
|
2586
2586
|
function emitToolInputProgress(state, controller, tools) {
|
|
@@ -5135,7 +5135,7 @@ function handleOpeningTagSegment(src, lt, out, stack) {
|
|
|
5135
5135
|
function extractSchemaProperties(schema) {
|
|
5136
5136
|
const unwrapped = unwrapJsonSchema(schema);
|
|
5137
5137
|
if (!unwrapped || typeof unwrapped !== "object") {
|
|
5138
|
-
return
|
|
5138
|
+
return;
|
|
5139
5139
|
}
|
|
5140
5140
|
return unwrapped.properties;
|
|
5141
5141
|
}
|
|
@@ -7500,7 +7500,7 @@ function parseQwen3CoderToolParserClosedMatches(inner, outerNameAttr) {
|
|
|
7500
7500
|
var _a, _b;
|
|
7501
7501
|
const callBlockMatches = Array.from(inner.matchAll(CALL_BLOCK_RE));
|
|
7502
7502
|
if (callBlockMatches.length === 0) {
|
|
7503
|
-
return
|
|
7503
|
+
return;
|
|
7504
7504
|
}
|
|
7505
7505
|
const closedBlocks = [];
|
|
7506
7506
|
let lastClosedEnd = 0;
|
|
@@ -7544,11 +7544,11 @@ function extractQwen3CoderToolNameFromMarkup(markup) {
|
|
|
7544
7544
|
var _a, _b, _c, _d, _e;
|
|
7545
7545
|
const match = markup.match(QWEN3CODER_TOOL_NAME_SALVAGE_REGEX);
|
|
7546
7546
|
if (!match) {
|
|
7547
|
-
return
|
|
7547
|
+
return;
|
|
7548
7548
|
}
|
|
7549
7549
|
const name = (_e = (_d = (_c = (_b = (_a = match[1]) != null ? _a : match[2]) != null ? _b : match[3]) != null ? _c : match[4]) != null ? _d : match[5]) != null ? _e : match[6];
|
|
7550
7550
|
if (!name) {
|
|
7551
|
-
return
|
|
7551
|
+
return;
|
|
7552
7552
|
}
|
|
7553
7553
|
const trimmed = name.trim();
|
|
7554
7554
|
return trimmed.length > 0 ? trimmed : void 0;
|
|
@@ -7846,9 +7846,7 @@ var qwen3CoderProtocol = () => ({
|
|
|
7846
7846
|
tryEmitToolCallSegment(synthetic, trailing);
|
|
7847
7847
|
return true;
|
|
7848
7848
|
};
|
|
7849
|
-
const tryParseCallBlocksWithoutWrapper = () =>
|
|
7850
|
-
return tryParseCallBlocksWithoutWrapperText(text);
|
|
7851
|
-
};
|
|
7849
|
+
const tryParseCallBlocksWithoutWrapper = () => tryParseCallBlocksWithoutWrapperText(text);
|
|
7852
7850
|
const tryParseSingleFunctionCall = () => {
|
|
7853
7851
|
const lowerText = text.toLowerCase();
|
|
7854
7852
|
const startIndex = lowerText.indexOf("<function");
|
|
@@ -8993,7 +8991,14 @@ function processToolCallMatch(text, tc, currentIndex, processedElements, options
|
|
|
8993
8991
|
}
|
|
8994
8992
|
addTextSegment(text.slice(currentIndex, tc.startIndex), processedElements);
|
|
8995
8993
|
const result = parseYamlContent(tc.content);
|
|
8996
|
-
if (
|
|
8994
|
+
if (result.ok) {
|
|
8995
|
+
processedElements.push({
|
|
8996
|
+
type: "tool-call",
|
|
8997
|
+
toolCallId: generateToolCallId(),
|
|
8998
|
+
toolName: tc.toolName,
|
|
8999
|
+
input: JSON.stringify(result.value)
|
|
9000
|
+
});
|
|
9001
|
+
} else {
|
|
8997
9002
|
const originalText = text.slice(tc.startIndex, tc.endIndex);
|
|
8998
9003
|
const cause = yamlFailureCause(result.failure);
|
|
8999
9004
|
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, "Could not parse YAML tool call", {
|
|
@@ -9004,13 +9009,6 @@ function processToolCallMatch(text, tc, currentIndex, processedElements, options
|
|
|
9004
9009
|
cause
|
|
9005
9010
|
});
|
|
9006
9011
|
processedElements.push({ type: "text", text: originalText });
|
|
9007
|
-
} else {
|
|
9008
|
-
processedElements.push({
|
|
9009
|
-
type: "tool-call",
|
|
9010
|
-
toolCallId: generateToolCallId(),
|
|
9011
|
-
toolName: tc.toolName,
|
|
9012
|
-
input: JSON.stringify(result.value)
|
|
9013
|
-
});
|
|
9014
9012
|
}
|
|
9015
9013
|
return tc.endIndex;
|
|
9016
9014
|
}
|
|
@@ -9037,337 +9035,335 @@ function stripTrailingPartialCloseTag(content, toolName) {
|
|
|
9037
9035
|
)}${preservedLeadingWhitespace}`;
|
|
9038
9036
|
return contentWithoutPartial.trimEnd();
|
|
9039
9037
|
}
|
|
9040
|
-
var yamlXmlProtocol = (_protocolOptions) => {
|
|
9041
|
-
|
|
9042
|
-
|
|
9043
|
-
|
|
9044
|
-
|
|
9045
|
-
|
|
9046
|
-
|
|
9047
|
-
|
|
9048
|
-
|
|
9049
|
-
|
|
9050
|
-
|
|
9051
|
-
args = { value: toolCall.input };
|
|
9052
|
-
}
|
|
9038
|
+
var yamlXmlProtocol = (_protocolOptions) => ({
|
|
9039
|
+
formatTools({ tools, toolSystemPromptTemplate }) {
|
|
9040
|
+
return formatToolsWithPromptTemplate({ tools, toolSystemPromptTemplate });
|
|
9041
|
+
},
|
|
9042
|
+
formatToolCall(toolCall) {
|
|
9043
|
+
let args = {};
|
|
9044
|
+
if (toolCall.input != null) {
|
|
9045
|
+
try {
|
|
9046
|
+
args = JSON.parse(toolCall.input);
|
|
9047
|
+
} catch (e) {
|
|
9048
|
+
args = { value: toolCall.input };
|
|
9053
9049
|
}
|
|
9054
|
-
|
|
9055
|
-
|
|
9050
|
+
}
|
|
9051
|
+
const yamlContent = import_yaml.default.stringify(args);
|
|
9052
|
+
return `<${toolCall.toolName}>
|
|
9056
9053
|
${yamlContent}</${toolCall.toolName}>`;
|
|
9057
|
-
|
|
9058
|
-
|
|
9059
|
-
|
|
9060
|
-
|
|
9061
|
-
|
|
9062
|
-
|
|
9063
|
-
|
|
9064
|
-
|
|
9065
|
-
|
|
9066
|
-
|
|
9067
|
-
|
|
9068
|
-
|
|
9069
|
-
|
|
9070
|
-
|
|
9071
|
-
|
|
9072
|
-
|
|
9073
|
-
|
|
9074
|
-
|
|
9075
|
-
|
|
9076
|
-
|
|
9077
|
-
}
|
|
9054
|
+
},
|
|
9055
|
+
parseGeneratedText({ text, tools, options }) {
|
|
9056
|
+
const toolNames = extractToolNames(tools);
|
|
9057
|
+
if (toolNames.length === 0) {
|
|
9058
|
+
return [{ type: "text", text }];
|
|
9059
|
+
}
|
|
9060
|
+
const processedElements = [];
|
|
9061
|
+
let currentIndex = 0;
|
|
9062
|
+
let parseText = text;
|
|
9063
|
+
let toolCalls = findToolCalls2(parseText, toolNames);
|
|
9064
|
+
if (toolCalls.length === 0) {
|
|
9065
|
+
const repaired = tryRepairXmlSelfClosingRootWithBody(
|
|
9066
|
+
parseText,
|
|
9067
|
+
toolNames
|
|
9068
|
+
);
|
|
9069
|
+
if (repaired) {
|
|
9070
|
+
const repairedCalls = findToolCalls2(repaired, toolNames);
|
|
9071
|
+
if (repairedCalls.length > 0) {
|
|
9072
|
+
parseText = repaired;
|
|
9073
|
+
toolCalls = repairedCalls;
|
|
9078
9074
|
}
|
|
9079
9075
|
}
|
|
9080
|
-
|
|
9081
|
-
|
|
9082
|
-
|
|
9083
|
-
|
|
9084
|
-
|
|
9085
|
-
|
|
9086
|
-
|
|
9087
|
-
|
|
9076
|
+
}
|
|
9077
|
+
for (const tc of toolCalls) {
|
|
9078
|
+
currentIndex = processToolCallMatch(
|
|
9079
|
+
parseText,
|
|
9080
|
+
tc,
|
|
9081
|
+
currentIndex,
|
|
9082
|
+
processedElements,
|
|
9083
|
+
options
|
|
9084
|
+
);
|
|
9085
|
+
}
|
|
9086
|
+
if (currentIndex < parseText.length) {
|
|
9087
|
+
addTextSegment(parseText.slice(currentIndex), processedElements);
|
|
9088
|
+
}
|
|
9089
|
+
return processedElements;
|
|
9090
|
+
},
|
|
9091
|
+
createStreamParser({ tools, options }) {
|
|
9092
|
+
const toolNames = extractToolNames(tools);
|
|
9093
|
+
let buffer = "";
|
|
9094
|
+
let currentToolCall = null;
|
|
9095
|
+
let currentTextId = null;
|
|
9096
|
+
let hasEmittedTextStart = false;
|
|
9097
|
+
const flushText = createFlushTextHandler(
|
|
9098
|
+
() => currentTextId,
|
|
9099
|
+
(newId) => {
|
|
9100
|
+
currentTextId = newId;
|
|
9101
|
+
},
|
|
9102
|
+
() => hasEmittedTextStart,
|
|
9103
|
+
(value) => {
|
|
9104
|
+
hasEmittedTextStart = value;
|
|
9088
9105
|
}
|
|
9089
|
-
|
|
9090
|
-
|
|
9106
|
+
);
|
|
9107
|
+
const emitToolInputProgress2 = (controller, toolContent) => {
|
|
9108
|
+
if (!currentToolCall) {
|
|
9109
|
+
return;
|
|
9091
9110
|
}
|
|
9092
|
-
|
|
9093
|
-
|
|
9094
|
-
|
|
9095
|
-
|
|
9096
|
-
|
|
9097
|
-
|
|
9098
|
-
|
|
9099
|
-
|
|
9100
|
-
|
|
9101
|
-
|
|
9102
|
-
|
|
9103
|
-
|
|
9104
|
-
|
|
9105
|
-
|
|
9106
|
-
|
|
9107
|
-
|
|
9108
|
-
|
|
9109
|
-
);
|
|
9110
|
-
|
|
9111
|
-
|
|
9112
|
-
|
|
9113
|
-
|
|
9114
|
-
|
|
9115
|
-
|
|
9116
|
-
|
|
9117
|
-
|
|
9118
|
-
|
|
9119
|
-
toolName: currentToolCall.name,
|
|
9120
|
-
args: parsedArgs,
|
|
9111
|
+
const parsedArgs = parseYamlContentForStreamProgress(toolContent);
|
|
9112
|
+
if (parsedArgs === null) {
|
|
9113
|
+
return;
|
|
9114
|
+
}
|
|
9115
|
+
const fullInput = stringifyToolInputWithSchema({
|
|
9116
|
+
toolName: currentToolCall.name,
|
|
9117
|
+
args: parsedArgs,
|
|
9118
|
+
tools
|
|
9119
|
+
});
|
|
9120
|
+
if (fullInput === "{}" && toolContent.trim().length === 0) {
|
|
9121
|
+
return;
|
|
9122
|
+
}
|
|
9123
|
+
emitToolInputProgressDelta({
|
|
9124
|
+
controller,
|
|
9125
|
+
id: currentToolCall.toolCallId,
|
|
9126
|
+
state: currentToolCall,
|
|
9127
|
+
fullInput
|
|
9128
|
+
});
|
|
9129
|
+
};
|
|
9130
|
+
const processToolCallEnd = (controller, toolContent, toolName, toolCallId) => {
|
|
9131
|
+
var _a;
|
|
9132
|
+
const result = parseYamlContent(toolContent);
|
|
9133
|
+
flushText(controller);
|
|
9134
|
+
if (result.ok) {
|
|
9135
|
+
const finalInput = stringifyToolInputWithSchema({
|
|
9136
|
+
toolName,
|
|
9137
|
+
args: result.value,
|
|
9121
9138
|
tools
|
|
9122
9139
|
});
|
|
9123
|
-
if (
|
|
9124
|
-
|
|
9125
|
-
}
|
|
9126
|
-
emitToolInputProgressDelta({
|
|
9127
|
-
controller,
|
|
9128
|
-
id: currentToolCall.toolCallId,
|
|
9129
|
-
state: currentToolCall,
|
|
9130
|
-
fullInput
|
|
9131
|
-
});
|
|
9132
|
-
};
|
|
9133
|
-
const processToolCallEnd = (controller, toolContent, toolName, toolCallId) => {
|
|
9134
|
-
var _a;
|
|
9135
|
-
const result = parseYamlContent(toolContent);
|
|
9136
|
-
flushText(controller);
|
|
9137
|
-
if (!result.ok) {
|
|
9138
|
-
const original = `<${toolName}>${toolContent}</${toolName}>`;
|
|
9139
|
-
const emitRawFallback = shouldEmitRawToolCallTextOnError(options);
|
|
9140
|
-
emitFailedToolInputLifecycle({
|
|
9140
|
+
if (currentToolCall && currentToolCall.toolCallId === toolCallId) {
|
|
9141
|
+
emitFinalizedToolInputLifecycle({
|
|
9141
9142
|
controller,
|
|
9142
9143
|
id: toolCallId,
|
|
9143
|
-
|
|
9144
|
-
rawToolCallText: original,
|
|
9145
|
-
emitRawText: (rawText) => {
|
|
9146
|
-
flushText(controller, rawText);
|
|
9147
|
-
}
|
|
9148
|
-
});
|
|
9149
|
-
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, "Could not parse streaming YAML tool call", {
|
|
9150
|
-
toolCall: original,
|
|
9151
|
-
toolName,
|
|
9152
|
-
toolCallId,
|
|
9153
|
-
dropReason: "malformed-tool-call-body",
|
|
9154
|
-
cause: yamlFailureCause(result.failure)
|
|
9155
|
-
});
|
|
9156
|
-
} else {
|
|
9157
|
-
const finalInput = stringifyToolInputWithSchema({
|
|
9144
|
+
state: currentToolCall,
|
|
9158
9145
|
toolName,
|
|
9159
|
-
|
|
9160
|
-
|
|
9161
|
-
});
|
|
9162
|
-
if (currentToolCall && currentToolCall.toolCallId === toolCallId) {
|
|
9163
|
-
emitFinalizedToolInputLifecycle({
|
|
9164
|
-
controller,
|
|
9165
|
-
id: toolCallId,
|
|
9166
|
-
state: currentToolCall,
|
|
9167
|
-
toolName,
|
|
9168
|
-
finalInput,
|
|
9169
|
-
onMismatch: options == null ? void 0 : options.onError
|
|
9170
|
-
});
|
|
9171
|
-
} else {
|
|
9172
|
-
enqueueToolInputEndAndCall({
|
|
9173
|
-
controller,
|
|
9174
|
-
id: toolCallId,
|
|
9175
|
-
toolName,
|
|
9176
|
-
input: finalInput
|
|
9177
|
-
});
|
|
9178
|
-
}
|
|
9179
|
-
}
|
|
9180
|
-
};
|
|
9181
|
-
const finalizeUnclosedToolCall = (controller) => {
|
|
9182
|
-
var _a;
|
|
9183
|
-
if (!currentToolCall) {
|
|
9184
|
-
return;
|
|
9185
|
-
}
|
|
9186
|
-
emitToolInputProgress2(controller, buffer);
|
|
9187
|
-
const { name: toolName, toolCallId } = currentToolCall;
|
|
9188
|
-
const reconciledBuffer = stripTrailingPartialCloseTag(buffer, toolName);
|
|
9189
|
-
const result = parseYamlContent(reconciledBuffer);
|
|
9190
|
-
flushText(controller);
|
|
9191
|
-
if (!result.ok) {
|
|
9192
|
-
const unfinishedContent = `<${toolName}>${buffer}`;
|
|
9193
|
-
const emitRawFallback = shouldEmitRawToolCallTextOnError(options);
|
|
9194
|
-
emitFailedToolInputLifecycle({
|
|
9195
|
-
controller,
|
|
9196
|
-
id: toolCallId,
|
|
9197
|
-
emitRawToolCallTextOnError: emitRawFallback,
|
|
9198
|
-
rawToolCallText: unfinishedContent,
|
|
9199
|
-
emitRawText: (rawText) => {
|
|
9200
|
-
flushText(controller, rawText);
|
|
9201
|
-
}
|
|
9146
|
+
finalInput,
|
|
9147
|
+
onMismatch: options == null ? void 0 : options.onError
|
|
9202
9148
|
});
|
|
9203
|
-
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
|
|
9204
|
-
options,
|
|
9205
|
-
"Could not complete streaming YAML tool call at finish.",
|
|
9206
|
-
{
|
|
9207
|
-
toolCall: unfinishedContent,
|
|
9208
|
-
toolCallId,
|
|
9209
|
-
toolName,
|
|
9210
|
-
dropReason: "unfinished-tool-call",
|
|
9211
|
-
cause: yamlFailureCause(result.failure)
|
|
9212
|
-
}
|
|
9213
|
-
);
|
|
9214
9149
|
} else {
|
|
9215
|
-
|
|
9216
|
-
toolName,
|
|
9217
|
-
args: result.value,
|
|
9218
|
-
tools
|
|
9219
|
-
});
|
|
9220
|
-
emitFinalizedToolInputLifecycle({
|
|
9150
|
+
enqueueToolInputEndAndCall({
|
|
9221
9151
|
controller,
|
|
9222
9152
|
id: toolCallId,
|
|
9223
|
-
state: currentToolCall,
|
|
9224
9153
|
toolName,
|
|
9225
|
-
finalInput
|
|
9226
|
-
onMismatch: options == null ? void 0 : options.onError
|
|
9154
|
+
input: finalInput
|
|
9227
9155
|
});
|
|
9228
9156
|
}
|
|
9229
|
-
|
|
9230
|
-
|
|
9231
|
-
|
|
9232
|
-
|
|
9233
|
-
var _a;
|
|
9234
|
-
const endIdx = buffer.indexOf(endTag);
|
|
9235
|
-
if (endIdx === -1) {
|
|
9236
|
-
emitToolInputProgress2(controller, buffer);
|
|
9237
|
-
return false;
|
|
9238
|
-
}
|
|
9239
|
-
const content = buffer.slice(0, endIdx);
|
|
9240
|
-
emitToolInputProgress2(controller, content);
|
|
9241
|
-
buffer = buffer.slice(endIdx + endTag.length);
|
|
9242
|
-
processToolCallEnd(
|
|
9157
|
+
} else {
|
|
9158
|
+
const original = `<${toolName}>${toolContent}</${toolName}>`;
|
|
9159
|
+
const emitRawFallback = shouldEmitRawToolCallTextOnError(options);
|
|
9160
|
+
emitFailedToolInputLifecycle({
|
|
9243
9161
|
controller,
|
|
9244
|
-
|
|
9162
|
+
id: toolCallId,
|
|
9163
|
+
emitRawToolCallTextOnError: emitRawFallback,
|
|
9164
|
+
rawToolCallText: original,
|
|
9165
|
+
emitRawText: (rawText) => {
|
|
9166
|
+
flushText(controller, rawText);
|
|
9167
|
+
}
|
|
9168
|
+
});
|
|
9169
|
+
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, "Could not parse streaming YAML tool call", {
|
|
9170
|
+
toolCall: original,
|
|
9245
9171
|
toolName,
|
|
9246
|
-
|
|
9172
|
+
toolCallId,
|
|
9173
|
+
dropReason: "malformed-tool-call-body",
|
|
9174
|
+
cause: yamlFailureCause(result.failure)
|
|
9175
|
+
});
|
|
9176
|
+
}
|
|
9177
|
+
};
|
|
9178
|
+
const finalizeUnclosedToolCall = (controller) => {
|
|
9179
|
+
var _a;
|
|
9180
|
+
if (!currentToolCall) {
|
|
9181
|
+
return;
|
|
9182
|
+
}
|
|
9183
|
+
emitToolInputProgress2(controller, buffer);
|
|
9184
|
+
const { name: toolName, toolCallId } = currentToolCall;
|
|
9185
|
+
const reconciledBuffer = stripTrailingPartialCloseTag(buffer, toolName);
|
|
9186
|
+
const result = parseYamlContent(reconciledBuffer);
|
|
9187
|
+
flushText(controller);
|
|
9188
|
+
if (result.ok) {
|
|
9189
|
+
const finalInput = stringifyToolInputWithSchema({
|
|
9190
|
+
toolName,
|
|
9191
|
+
args: result.value,
|
|
9192
|
+
tools
|
|
9193
|
+
});
|
|
9194
|
+
emitFinalizedToolInputLifecycle({
|
|
9195
|
+
controller,
|
|
9196
|
+
id: toolCallId,
|
|
9197
|
+
state: currentToolCall,
|
|
9198
|
+
toolName,
|
|
9199
|
+
finalInput,
|
|
9200
|
+
onMismatch: options == null ? void 0 : options.onError
|
|
9201
|
+
});
|
|
9202
|
+
} else {
|
|
9203
|
+
const unfinishedContent = `<${toolName}>${buffer}`;
|
|
9204
|
+
const emitRawFallback = shouldEmitRawToolCallTextOnError(options);
|
|
9205
|
+
emitFailedToolInputLifecycle({
|
|
9206
|
+
controller,
|
|
9207
|
+
id: toolCallId,
|
|
9208
|
+
emitRawToolCallTextOnError: emitRawFallback,
|
|
9209
|
+
rawToolCallText: unfinishedContent,
|
|
9210
|
+
emitRawText: (rawText) => {
|
|
9211
|
+
flushText(controller, rawText);
|
|
9212
|
+
}
|
|
9213
|
+
});
|
|
9214
|
+
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(
|
|
9215
|
+
options,
|
|
9216
|
+
"Could not complete streaming YAML tool call at finish.",
|
|
9217
|
+
{
|
|
9218
|
+
toolCall: unfinishedContent,
|
|
9219
|
+
toolCallId,
|
|
9220
|
+
toolName,
|
|
9221
|
+
dropReason: "unfinished-tool-call",
|
|
9222
|
+
cause: yamlFailureCause(result.failure)
|
|
9223
|
+
}
|
|
9247
9224
|
);
|
|
9225
|
+
}
|
|
9226
|
+
buffer = "";
|
|
9227
|
+
currentToolCall = null;
|
|
9228
|
+
};
|
|
9229
|
+
const handlePendingToolCall = (controller, endTag, toolName) => {
|
|
9230
|
+
var _a;
|
|
9231
|
+
const endIdx = buffer.indexOf(endTag);
|
|
9232
|
+
if (endIdx === -1) {
|
|
9233
|
+
emitToolInputProgress2(controller, buffer);
|
|
9234
|
+
return false;
|
|
9235
|
+
}
|
|
9236
|
+
const content = buffer.slice(0, endIdx);
|
|
9237
|
+
emitToolInputProgress2(controller, content);
|
|
9238
|
+
buffer = buffer.slice(endIdx + endTag.length);
|
|
9239
|
+
processToolCallEnd(
|
|
9240
|
+
controller,
|
|
9241
|
+
content,
|
|
9242
|
+
toolName,
|
|
9243
|
+
(_a = currentToolCall == null ? void 0 : currentToolCall.toolCallId) != null ? _a : generateToolCallId()
|
|
9244
|
+
);
|
|
9245
|
+
currentToolCall = null;
|
|
9246
|
+
return true;
|
|
9247
|
+
};
|
|
9248
|
+
const flushSafeText = (controller) => {
|
|
9249
|
+
const maxTagLen = toolNames.length ? Math.max(...toolNames.map((n) => `<${n} />`.length)) : 0;
|
|
9250
|
+
const tail = Math.max(0, maxTagLen - 1);
|
|
9251
|
+
const safeLen = Math.max(0, buffer.length - tail);
|
|
9252
|
+
if (safeLen > 0) {
|
|
9253
|
+
flushText(controller, buffer.slice(0, safeLen));
|
|
9254
|
+
buffer = buffer.slice(safeLen);
|
|
9255
|
+
}
|
|
9256
|
+
};
|
|
9257
|
+
const handleNewToolTag = (controller, tagIndex, tagName, selfClosing, tagLength) => {
|
|
9258
|
+
if (tagIndex > 0) {
|
|
9259
|
+
flushText(controller, buffer.slice(0, tagIndex));
|
|
9260
|
+
}
|
|
9261
|
+
flushText(controller);
|
|
9262
|
+
if (selfClosing) {
|
|
9263
|
+
buffer = buffer.slice(tagIndex + tagLength);
|
|
9264
|
+
const toolCallId = generateToolCallId();
|
|
9265
|
+
currentToolCall = {
|
|
9266
|
+
name: tagName,
|
|
9267
|
+
toolCallId,
|
|
9268
|
+
emittedInput: ""
|
|
9269
|
+
};
|
|
9270
|
+
controller.enqueue({
|
|
9271
|
+
type: "tool-input-start",
|
|
9272
|
+
id: toolCallId,
|
|
9273
|
+
toolName: tagName
|
|
9274
|
+
});
|
|
9275
|
+
processToolCallEnd(controller, "", tagName, toolCallId);
|
|
9248
9276
|
currentToolCall = null;
|
|
9249
|
-
|
|
9250
|
-
|
|
9251
|
-
|
|
9252
|
-
|
|
9253
|
-
|
|
9254
|
-
|
|
9255
|
-
|
|
9256
|
-
|
|
9257
|
-
|
|
9258
|
-
|
|
9259
|
-
|
|
9260
|
-
|
|
9261
|
-
|
|
9262
|
-
|
|
9263
|
-
|
|
9264
|
-
|
|
9265
|
-
|
|
9266
|
-
|
|
9267
|
-
const
|
|
9268
|
-
|
|
9269
|
-
|
|
9270
|
-
|
|
9271
|
-
|
|
9272
|
-
};
|
|
9273
|
-
controller.enqueue({
|
|
9274
|
-
type: "tool-input-start",
|
|
9275
|
-
id: toolCallId,
|
|
9276
|
-
toolName: tagName
|
|
9277
|
-
});
|
|
9278
|
-
processToolCallEnd(controller, "", tagName, toolCallId);
|
|
9279
|
-
currentToolCall = null;
|
|
9277
|
+
} else {
|
|
9278
|
+
const startTag = `<${tagName}>`;
|
|
9279
|
+
buffer = buffer.slice(tagIndex + startTag.length);
|
|
9280
|
+
currentToolCall = {
|
|
9281
|
+
name: tagName,
|
|
9282
|
+
toolCallId: generateToolCallId(),
|
|
9283
|
+
emittedInput: ""
|
|
9284
|
+
};
|
|
9285
|
+
controller.enqueue({
|
|
9286
|
+
type: "tool-input-start",
|
|
9287
|
+
id: currentToolCall.toolCallId,
|
|
9288
|
+
toolName: tagName
|
|
9289
|
+
});
|
|
9290
|
+
}
|
|
9291
|
+
};
|
|
9292
|
+
const processBuffer = (controller) => {
|
|
9293
|
+
while (true) {
|
|
9294
|
+
if (currentToolCall) {
|
|
9295
|
+
const toolName = currentToolCall.name;
|
|
9296
|
+
const endTag = `</${toolName}>`;
|
|
9297
|
+
if (!handlePendingToolCall(controller, endTag, toolName)) {
|
|
9298
|
+
break;
|
|
9299
|
+
}
|
|
9280
9300
|
} else {
|
|
9281
|
-
const
|
|
9282
|
-
|
|
9283
|
-
|
|
9284
|
-
|
|
9285
|
-
|
|
9286
|
-
|
|
9287
|
-
|
|
9288
|
-
controller.enqueue({
|
|
9289
|
-
type: "tool-input-start",
|
|
9290
|
-
id: currentToolCall.toolCallId,
|
|
9291
|
-
toolName: tagName
|
|
9292
|
-
});
|
|
9293
|
-
}
|
|
9294
|
-
};
|
|
9295
|
-
const processBuffer = (controller) => {
|
|
9296
|
-
while (true) {
|
|
9297
|
-
if (currentToolCall) {
|
|
9298
|
-
const toolName = currentToolCall.name;
|
|
9299
|
-
const endTag = `</${toolName}>`;
|
|
9300
|
-
if (!handlePendingToolCall(controller, endTag, toolName)) {
|
|
9301
|
-
break;
|
|
9302
|
-
}
|
|
9303
|
-
} else {
|
|
9304
|
-
const { index, name, selfClosing, tagLength } = findEarliestToolTag(
|
|
9305
|
-
buffer,
|
|
9306
|
-
toolNames
|
|
9307
|
-
);
|
|
9308
|
-
if (index === -1) {
|
|
9309
|
-
flushSafeText(controller);
|
|
9310
|
-
break;
|
|
9311
|
-
}
|
|
9312
|
-
handleNewToolTag(controller, index, name, selfClosing, tagLength);
|
|
9301
|
+
const { index, name, selfClosing, tagLength } = findEarliestToolTag(
|
|
9302
|
+
buffer,
|
|
9303
|
+
toolNames
|
|
9304
|
+
);
|
|
9305
|
+
if (index === -1) {
|
|
9306
|
+
flushSafeText(controller);
|
|
9307
|
+
break;
|
|
9313
9308
|
}
|
|
9309
|
+
handleNewToolTag(controller, index, name, selfClosing, tagLength);
|
|
9314
9310
|
}
|
|
9315
|
-
}
|
|
9316
|
-
|
|
9317
|
-
|
|
9318
|
-
|
|
9319
|
-
|
|
9320
|
-
|
|
9321
|
-
finalizeUnclosedToolCall(controller);
|
|
9322
|
-
} else if (buffer) {
|
|
9323
|
-
flushText(controller, buffer);
|
|
9324
|
-
buffer = "";
|
|
9325
|
-
}
|
|
9326
|
-
flushText(controller);
|
|
9327
|
-
controller.enqueue(chunk);
|
|
9328
|
-
return;
|
|
9329
|
-
}
|
|
9330
|
-
if (chunk.type !== "text-delta") {
|
|
9331
|
-
if (!currentToolCall && buffer) {
|
|
9332
|
-
flushText(controller, buffer);
|
|
9333
|
-
buffer = "";
|
|
9334
|
-
}
|
|
9335
|
-
controller.enqueue(chunk);
|
|
9336
|
-
return;
|
|
9337
|
-
}
|
|
9338
|
-
const textContent = (_a = chunk.delta) != null ? _a : "";
|
|
9339
|
-
buffer += textContent;
|
|
9340
|
-
processBuffer(controller);
|
|
9341
|
-
},
|
|
9342
|
-
flush(controller) {
|
|
9311
|
+
}
|
|
9312
|
+
};
|
|
9313
|
+
return new TransformStream({
|
|
9314
|
+
transform(chunk, controller) {
|
|
9315
|
+
var _a;
|
|
9316
|
+
if (chunk.type === "finish") {
|
|
9343
9317
|
if (currentToolCall) {
|
|
9344
9318
|
finalizeUnclosedToolCall(controller);
|
|
9345
9319
|
} else if (buffer) {
|
|
9346
9320
|
flushText(controller, buffer);
|
|
9347
9321
|
buffer = "";
|
|
9348
9322
|
}
|
|
9349
|
-
|
|
9350
|
-
|
|
9351
|
-
|
|
9352
|
-
|
|
9353
|
-
|
|
9354
|
-
|
|
9355
|
-
|
|
9323
|
+
flushText(controller);
|
|
9324
|
+
controller.enqueue(chunk);
|
|
9325
|
+
return;
|
|
9326
|
+
}
|
|
9327
|
+
if (chunk.type !== "text-delta") {
|
|
9328
|
+
if (!currentToolCall && buffer) {
|
|
9329
|
+
flushText(controller, buffer);
|
|
9330
|
+
buffer = "";
|
|
9356
9331
|
}
|
|
9332
|
+
controller.enqueue(chunk);
|
|
9333
|
+
return;
|
|
9334
|
+
}
|
|
9335
|
+
const textContent = (_a = chunk.delta) != null ? _a : "";
|
|
9336
|
+
buffer += textContent;
|
|
9337
|
+
processBuffer(controller);
|
|
9338
|
+
},
|
|
9339
|
+
flush(controller) {
|
|
9340
|
+
if (currentToolCall) {
|
|
9341
|
+
finalizeUnclosedToolCall(controller);
|
|
9342
|
+
} else if (buffer) {
|
|
9343
|
+
flushText(controller, buffer);
|
|
9344
|
+
buffer = "";
|
|
9345
|
+
}
|
|
9346
|
+
if (currentTextId && hasEmittedTextStart) {
|
|
9347
|
+
controller.enqueue({
|
|
9348
|
+
type: "text-end",
|
|
9349
|
+
id: currentTextId
|
|
9350
|
+
});
|
|
9351
|
+
hasEmittedTextStart = false;
|
|
9352
|
+
currentTextId = null;
|
|
9357
9353
|
}
|
|
9358
|
-
});
|
|
9359
|
-
},
|
|
9360
|
-
extractToolCallSegments({ text, tools }) {
|
|
9361
|
-
const toolNames = tools.map((t) => t.name).filter(Boolean);
|
|
9362
|
-
if (toolNames.length === 0) {
|
|
9363
|
-
return [];
|
|
9364
9354
|
}
|
|
9365
|
-
|
|
9366
|
-
|
|
9367
|
-
|
|
9355
|
+
});
|
|
9356
|
+
},
|
|
9357
|
+
extractToolCallSegments({ text, tools }) {
|
|
9358
|
+
const toolNames = tools.map((t) => t.name).filter(Boolean);
|
|
9359
|
+
if (toolNames.length === 0) {
|
|
9360
|
+
return [];
|
|
9368
9361
|
}
|
|
9369
|
-
|
|
9370
|
-
}
|
|
9362
|
+
return findToolCalls2(text, toolNames).map(
|
|
9363
|
+
(tc) => `<${tc.toolName}>${tc.content}</${tc.toolName}>`
|
|
9364
|
+
);
|
|
9365
|
+
}
|
|
9366
|
+
});
|
|
9371
9367
|
|
|
9372
9368
|
// src/core/utils/dynamic-tool-schema.ts
|
|
9373
9369
|
function createDynamicIfThenElseSchema(tools) {
|
|
@@ -9563,7 +9559,7 @@ function parseJsonCandidate(candidateText) {
|
|
|
9563
9559
|
try {
|
|
9564
9560
|
return parse(candidateText);
|
|
9565
9561
|
} catch (e) {
|
|
9566
|
-
return
|
|
9562
|
+
return;
|
|
9567
9563
|
}
|
|
9568
9564
|
}
|
|
9569
9565
|
function extractCodeBlockCandidates(text) {
|