@linnlabs/linnkit 0.38.0 → 0.39.0
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/CHANGELOG.md +33 -1
- package/dist/cli.cjs +111 -79
- package/dist/cli.cjs.map +1 -1
- package/dist/cli.js +111 -79
- package/dist/cli.js.map +1 -1
- package/dist/context-manager.cjs +23 -17
- package/dist/context-manager.cjs.map +1 -1
- package/dist/context-manager.js +23 -17
- package/dist/context-manager.js.map +1 -1
- package/dist/contracts/contextPolicy.d.cts +2 -1
- package/dist/contracts/contextPolicy.d.ts +2 -1
- package/dist/contracts/token-accounting.d.cts +8 -8
- package/dist/contracts/token-accounting.d.ts +8 -8
- package/dist/contracts.cjs.map +1 -1
- package/dist/contracts.js.map +1 -1
- package/dist/index.cjs +157 -80
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +157 -80
- package/dist/index.js.map +1 -1
- package/dist/quickstart.cjs +111 -79
- package/dist/quickstart.cjs.map +1 -1
- package/dist/quickstart.js +111 -79
- package/dist/quickstart.js.map +1 -1
- package/dist/runtime-kernel/events.cjs.map +1 -1
- package/dist/runtime-kernel/events.js.map +1 -1
- package/dist/runtime-kernel/llm/definitions/llmCallInvocationContext.d.cts +4 -1
- package/dist/runtime-kernel/llm/definitions/llmCallInvocationContext.d.ts +4 -1
- package/dist/runtime-kernel.cjs +157 -80
- package/dist/runtime-kernel.cjs.map +1 -1
- package/dist/runtime-kernel.js +157 -80
- package/dist/runtime-kernel.js.map +1 -1
- package/dist/shared/prompt-budget.d.cts +4 -2
- package/dist/shared/prompt-budget.d.ts +4 -2
- package/dist/testkit.cjs +157 -80
- package/dist/testkit.cjs.map +1 -1
- package/dist/testkit.js +157 -80
- package/dist/testkit.js.map +1 -1
- package/docs/integration/agent-registration-guide.md +1 -1
- package/docs/integration/context-engineering.md +12 -8
- package/docs/integration/token-management.md +6 -1
- package/docs/integration/tools.md +4 -0
- package/package.json +2 -2
|
@@ -20,7 +20,10 @@ interface LlmCallInvocationContext {
|
|
|
20
20
|
* 该调用上下文不会进入 provider options,请求适配器不得接触此字段。
|
|
21
21
|
*/
|
|
22
22
|
readonly toolCallStreamingPolicies?: Readonly<Record<string, ToolCallStreamingPolicy>>;
|
|
23
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* 每个真实 Provider attempt 发送前按 active route 测量、接纳最终 Prompt。
|
|
25
|
+
* 返回快照的 output_limit_tokens 是该次实际请求上限,caller 必须使用它。
|
|
26
|
+
*/
|
|
24
27
|
readonly measurePromptUsage?: (activeModelId: string, messages: readonly LlmRequestMessage[]) => Promise<ContextUsageSnapshot>;
|
|
25
28
|
/** fallback candidate 在真实 attempt 前的本地容量 admission。 */
|
|
26
29
|
readonly evaluateFallbackPromptCapacity?: (candidateModelId: string) => FallbackPromptCapacityAdmission;
|
|
@@ -20,7 +20,10 @@ interface LlmCallInvocationContext {
|
|
|
20
20
|
* 该调用上下文不会进入 provider options,请求适配器不得接触此字段。
|
|
21
21
|
*/
|
|
22
22
|
readonly toolCallStreamingPolicies?: Readonly<Record<string, ToolCallStreamingPolicy>>;
|
|
23
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* 每个真实 Provider attempt 发送前按 active route 测量、接纳最终 Prompt。
|
|
25
|
+
* 返回快照的 output_limit_tokens 是该次实际请求上限,caller 必须使用它。
|
|
26
|
+
*/
|
|
24
27
|
readonly measurePromptUsage?: (activeModelId: string, messages: readonly LlmRequestMessage[]) => Promise<ContextUsageSnapshot>;
|
|
25
28
|
/** fallback candidate 在真实 attempt 前的本地容量 admission。 */
|
|
26
29
|
readonly evaluateFallbackPromptCapacity?: (candidateModelId: string) => FallbackPromptCapacityAdmission;
|
package/dist/runtime-kernel.cjs
CHANGED
|
@@ -5038,6 +5038,88 @@ function evaluatePrimaryPromptCapacity(snapshot) {
|
|
|
5038
5038
|
};
|
|
5039
5039
|
}
|
|
5040
5040
|
|
|
5041
|
+
// src/shared/prompt-budget.ts
|
|
5042
|
+
var DEFAULT_PROMPT_CONTEXT_WINDOW_TOKENS = 256e3;
|
|
5043
|
+
var DEFAULT_PROMPT_MAX_OUTPUT_TOKENS = 16384;
|
|
5044
|
+
function resolveEffectivePromptBudget(input) {
|
|
5045
|
+
assertOptionalPositiveSafeInteger(input.policyMaxTokens, "policyMaxTokens");
|
|
5046
|
+
assertOptionalPositiveSafeInteger(
|
|
5047
|
+
input.policyReservedForResponse,
|
|
5048
|
+
"policyReservedForResponse"
|
|
5049
|
+
);
|
|
5050
|
+
assertOptionalPositiveSafeInteger(
|
|
5051
|
+
input.modelContextWindowTokens,
|
|
5052
|
+
"modelContextWindowTokens"
|
|
5053
|
+
);
|
|
5054
|
+
assertOptionalPositiveSafeInteger(input.modelMaxOutputTokens, "modelMaxOutputTokens");
|
|
5055
|
+
assertOptionalPositiveSafeInteger(
|
|
5056
|
+
input.fallbackContextWindowTokens,
|
|
5057
|
+
"fallbackContextWindowTokens"
|
|
5058
|
+
);
|
|
5059
|
+
assertOptionalPositiveSafeInteger(
|
|
5060
|
+
input.fallbackMaxOutputTokens,
|
|
5061
|
+
"fallbackMaxOutputTokens"
|
|
5062
|
+
);
|
|
5063
|
+
assertNonNegativeSafeInteger(input.toolDefinitionTokens, "toolDefinitionTokens");
|
|
5064
|
+
const modelContextWindowTokens = input.modelContextWindowTokens ?? input.fallbackContextWindowTokens ?? DEFAULT_PROMPT_CONTEXT_WINDOW_TOKENS;
|
|
5065
|
+
const modelMaxOutputTokens = input.modelMaxOutputTokens ?? input.fallbackMaxOutputTokens ?? DEFAULT_PROMPT_MAX_OUTPUT_TOKENS;
|
|
5066
|
+
const effectiveWindowTokens = input.policyMaxTokens === void 0 ? modelContextWindowTokens : Math.min(input.policyMaxTokens, modelContextWindowTokens);
|
|
5067
|
+
const outputLimitTokens = input.policyReservedForResponse === void 0 ? modelMaxOutputTokens : Math.min(input.policyReservedForResponse, modelMaxOutputTokens);
|
|
5068
|
+
const responseReservationTokens = input.policyReservedForResponse !== void 0 || input.modelContextWindowTokens === void 0 ? outputLimitTokens : 1;
|
|
5069
|
+
const inputBudgetTokens = effectiveWindowTokens - responseReservationTokens;
|
|
5070
|
+
if (inputBudgetTokens < 1) {
|
|
5071
|
+
throw new Error(
|
|
5072
|
+
"\u6709\u6548\u6A21\u578B\u7A97\u53E3\u5FC5\u987B\u5927\u4E8E\u8F93\u51FA\u9884\u7559\uFF0C\u624D\u80FD\u5F62\u6210\u6B63\u6570 Prompt \u8F93\u5165\u9884\u7B97\u3002"
|
|
5073
|
+
);
|
|
5074
|
+
}
|
|
5075
|
+
const messageBudgetTokens = inputBudgetTokens - input.toolDefinitionTokens;
|
|
5076
|
+
if (messageBudgetTokens < 1) {
|
|
5077
|
+
throw new Error(
|
|
5078
|
+
"Tool definitions \u5DF2\u5360\u6EE1 Prompt \u8F93\u5165\u9884\u7B97\uFF0C\u65E0\u6CD5\u4E3A messages \u4FDD\u7559 token\u3002"
|
|
5079
|
+
);
|
|
5080
|
+
}
|
|
5081
|
+
return {
|
|
5082
|
+
effectiveWindowTokens,
|
|
5083
|
+
outputLimitTokens,
|
|
5084
|
+
inputBudgetTokens,
|
|
5085
|
+
toolDefinitionTokens: input.toolDefinitionTokens,
|
|
5086
|
+
messageBudgetTokens
|
|
5087
|
+
};
|
|
5088
|
+
}
|
|
5089
|
+
function assertOptionalPositiveSafeInteger(value, field) {
|
|
5090
|
+
if (value !== void 0) {
|
|
5091
|
+
assertPositiveSafeInteger(value, field);
|
|
5092
|
+
}
|
|
5093
|
+
}
|
|
5094
|
+
function assertPositiveSafeInteger(value, field) {
|
|
5095
|
+
if (!Number.isSafeInteger(value) || value < 1) {
|
|
5096
|
+
throw new Error(`${field} \u5FC5\u987B\u662F\u6B63\u5B89\u5168\u6574\u6570\u3002`);
|
|
5097
|
+
}
|
|
5098
|
+
}
|
|
5099
|
+
function assertNonNegativeSafeInteger(value, field) {
|
|
5100
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
5101
|
+
throw new Error(`${field} \u5FC5\u987B\u662F\u975E\u8D1F\u5B89\u5168\u6574\u6570\u3002`);
|
|
5102
|
+
}
|
|
5103
|
+
}
|
|
5104
|
+
|
|
5105
|
+
// src/runtime-kernel/graph-engine/functions/resolveFallbackPromptBudget.ts
|
|
5106
|
+
function resolveFallbackPromptBudget(primaryBudget, requiredOutputLimitTokens, route) {
|
|
5107
|
+
if (!route) throw new Error("Fallback \u6A21\u578B\u7F3A\u5C11\u6B63\u5F0F inference route\u3002");
|
|
5108
|
+
const budget = resolveEffectivePromptBudget({
|
|
5109
|
+
policyMaxTokens: primaryBudget.effectiveWindowTokens,
|
|
5110
|
+
policyReservedForResponse: requiredOutputLimitTokens,
|
|
5111
|
+
modelContextWindowTokens: route.context_window_tokens,
|
|
5112
|
+
modelMaxOutputTokens: route.max_output_tokens,
|
|
5113
|
+
toolDefinitionTokens: primaryBudget.toolDefinitionTokens
|
|
5114
|
+
});
|
|
5115
|
+
const inputBudgetTokens = Math.min(primaryBudget.inputBudgetTokens, budget.inputBudgetTokens);
|
|
5116
|
+
return {
|
|
5117
|
+
...budget,
|
|
5118
|
+
inputBudgetTokens,
|
|
5119
|
+
messageBudgetTokens: inputBudgetTokens - budget.toolDefinitionTokens
|
|
5120
|
+
};
|
|
5121
|
+
}
|
|
5122
|
+
|
|
5041
5123
|
// src/runtime-kernel/graph-engine/tick-pipeline/stages/executeLlmStage.ts
|
|
5042
5124
|
function createExecuteLlmStage(dependencies) {
|
|
5043
5125
|
return defineTickStage({
|
|
@@ -5116,7 +5198,11 @@ function createExecuteLlmStage(dependencies) {
|
|
|
5116
5198
|
servedModelId: activeModelId,
|
|
5117
5199
|
messages,
|
|
5118
5200
|
llmOptions: ctx.llmOptions,
|
|
5119
|
-
promptBudget
|
|
5201
|
+
promptBudget: activeModelId === ctx.modelId ? promptBudget : resolveFallbackPromptBudget(
|
|
5202
|
+
promptBudget,
|
|
5203
|
+
ctx.promptUsageCandidate?.output_limit_tokens ?? promptBudget.outputLimitTokens,
|
|
5204
|
+
dependencies.modelCatalog.getModelById(activeModelId)?.inference_route
|
|
5205
|
+
),
|
|
5120
5206
|
measurementPolicy: promptUsageMeasurementPolicy,
|
|
5121
5207
|
imageInputTokens: ctx.imageInputAdmissionEvidence?.attachments.reduce(
|
|
5122
5208
|
(total, attachment) => total + attachment.estimatedTokens,
|
|
@@ -5146,7 +5232,7 @@ function createExecuteLlmStage(dependencies) {
|
|
|
5146
5232
|
return evaluateFallbackPromptCapacity({
|
|
5147
5233
|
contextWindowTokens: route?.context_window_tokens,
|
|
5148
5234
|
maxOutputTokens: route?.max_output_tokens,
|
|
5149
|
-
requiredOutputLimitTokens: promptBudget.outputLimitTokens,
|
|
5235
|
+
requiredOutputLimitTokens: ctx.promptUsageCandidate?.output_limit_tokens ?? promptBudget.outputLimitTokens,
|
|
5150
5236
|
estimatedPromptTokens: weights.systemPromptTokens + weights.conversationTokens + weights.toolDefinitionTokens
|
|
5151
5237
|
});
|
|
5152
5238
|
}
|
|
@@ -5434,7 +5520,7 @@ async function executeContextCompaction(dependencies, input) {
|
|
|
5434
5520
|
response = await dependencies.llmCaller.call(
|
|
5435
5521
|
input.modelId,
|
|
5436
5522
|
messages,
|
|
5437
|
-
compactionOptions,
|
|
5523
|
+
{ ...compactionOptions, max_tokens: compactionPromptUsage.output_limit_tokens },
|
|
5438
5524
|
input.signal,
|
|
5439
5525
|
{ imageInputAdmissionEvidence: input.imageInputAdmissionEvidence }
|
|
5440
5526
|
);
|
|
@@ -5497,7 +5583,7 @@ async function executeContextCompaction(dependencies, input) {
|
|
|
5497
5583
|
conversationId: input.conversationId,
|
|
5498
5584
|
turnId: input.turnId,
|
|
5499
5585
|
timestamp: Date.now(),
|
|
5500
|
-
maxOutputTokens
|
|
5586
|
+
maxOutputTokens: compactionPromptUsage.output_limit_tokens
|
|
5501
5587
|
});
|
|
5502
5588
|
} catch (error) {
|
|
5503
5589
|
if (error instanceof Error && error.name === "AbortError") throw error;
|
|
@@ -5682,14 +5768,11 @@ function buildCompactionLlmOptions(input) {
|
|
|
5682
5768
|
};
|
|
5683
5769
|
}
|
|
5684
5770
|
function buildCompactionPromptBudget(mainBudget, maxOutputTokens) {
|
|
5685
|
-
|
|
5686
|
-
|
|
5687
|
-
|
|
5688
|
-
|
|
5689
|
-
|
|
5690
|
-
toolDefinitionTokens: mainBudget.toolDefinitionTokens,
|
|
5691
|
-
messageBudgetTokens: inputBudgetTokens - mainBudget.toolDefinitionTokens
|
|
5692
|
-
};
|
|
5771
|
+
return resolveEffectivePromptBudget({
|
|
5772
|
+
modelContextWindowTokens: mainBudget.effectiveWindowTokens,
|
|
5773
|
+
modelMaxOutputTokens: maxOutputTokens,
|
|
5774
|
+
toolDefinitionTokens: mainBudget.toolDefinitionTokens
|
|
5775
|
+
});
|
|
5693
5776
|
}
|
|
5694
5777
|
|
|
5695
5778
|
// src/runtime-kernel/graph-engine/tick-pipeline/stages/compactContextStage.ts
|
|
@@ -6266,6 +6349,10 @@ function createPromptUsageMeasurer(dependencies) {
|
|
|
6266
6349
|
});
|
|
6267
6350
|
const usedTokens = remoteResult?.inputTokens ?? calibratedLocalTotal;
|
|
6268
6351
|
const components = normalizePromptUsageComponents(usedTokens, weights);
|
|
6352
|
+
const outputLimitTokens = usedTokens <= input.promptBudget.inputBudgetTokens ? Math.min(
|
|
6353
|
+
input.promptBudget.outputLimitTokens,
|
|
6354
|
+
input.promptBudget.effectiveWindowTokens - usedTokens
|
|
6355
|
+
) : input.promptBudget.outputLimitTokens;
|
|
6269
6356
|
return ContextUsageSnapshot.parse({
|
|
6270
6357
|
basis: "last_completed_llm_prompt",
|
|
6271
6358
|
budget_model_id: input.budgetModelId,
|
|
@@ -6275,7 +6362,7 @@ function createPromptUsageMeasurer(dependencies) {
|
|
|
6275
6362
|
component_attribution: "normalized_local_estimate",
|
|
6276
6363
|
input_budget_tokens: input.promptBudget.inputBudgetTokens,
|
|
6277
6364
|
remaining_tokens: input.promptBudget.inputBudgetTokens - usedTokens,
|
|
6278
|
-
output_limit_tokens:
|
|
6365
|
+
output_limit_tokens: outputLimitTokens,
|
|
6279
6366
|
source: remoteResult?.source ?? "local-estimate",
|
|
6280
6367
|
confidence: remoteResult?.confidence ?? "estimate",
|
|
6281
6368
|
measured_at: now()
|
|
@@ -9764,69 +9851,6 @@ function createHostToolCallBootstrap(input) {
|
|
|
9764
9851
|
}
|
|
9765
9852
|
};
|
|
9766
9853
|
}
|
|
9767
|
-
|
|
9768
|
-
// src/shared/prompt-budget.ts
|
|
9769
|
-
var DEFAULT_PROMPT_CONTEXT_WINDOW_TOKENS = 256e3;
|
|
9770
|
-
var DEFAULT_PROMPT_MAX_OUTPUT_TOKENS = 16384;
|
|
9771
|
-
function resolveEffectivePromptBudget(input) {
|
|
9772
|
-
assertOptionalPositiveSafeInteger(input.policyMaxTokens, "policyMaxTokens");
|
|
9773
|
-
assertOptionalPositiveSafeInteger(
|
|
9774
|
-
input.policyReservedForResponse,
|
|
9775
|
-
"policyReservedForResponse"
|
|
9776
|
-
);
|
|
9777
|
-
assertOptionalPositiveSafeInteger(
|
|
9778
|
-
input.modelContextWindowTokens,
|
|
9779
|
-
"modelContextWindowTokens"
|
|
9780
|
-
);
|
|
9781
|
-
assertOptionalPositiveSafeInteger(input.modelMaxOutputTokens, "modelMaxOutputTokens");
|
|
9782
|
-
assertOptionalPositiveSafeInteger(
|
|
9783
|
-
input.fallbackContextWindowTokens,
|
|
9784
|
-
"fallbackContextWindowTokens"
|
|
9785
|
-
);
|
|
9786
|
-
assertOptionalPositiveSafeInteger(
|
|
9787
|
-
input.fallbackMaxOutputTokens,
|
|
9788
|
-
"fallbackMaxOutputTokens"
|
|
9789
|
-
);
|
|
9790
|
-
assertNonNegativeSafeInteger(input.toolDefinitionTokens, "toolDefinitionTokens");
|
|
9791
|
-
const modelContextWindowTokens = input.modelContextWindowTokens ?? input.fallbackContextWindowTokens ?? DEFAULT_PROMPT_CONTEXT_WINDOW_TOKENS;
|
|
9792
|
-
const modelMaxOutputTokens = input.modelMaxOutputTokens ?? input.fallbackMaxOutputTokens ?? DEFAULT_PROMPT_MAX_OUTPUT_TOKENS;
|
|
9793
|
-
const effectiveWindowTokens = input.policyMaxTokens === void 0 ? modelContextWindowTokens : Math.min(input.policyMaxTokens, modelContextWindowTokens);
|
|
9794
|
-
const outputLimitTokens = input.policyReservedForResponse === void 0 ? modelMaxOutputTokens : Math.min(input.policyReservedForResponse, modelMaxOutputTokens);
|
|
9795
|
-
const inputBudgetTokens = effectiveWindowTokens - outputLimitTokens;
|
|
9796
|
-
if (inputBudgetTokens < 1) {
|
|
9797
|
-
throw new Error(
|
|
9798
|
-
"\u6709\u6548\u6A21\u578B\u7A97\u53E3\u5FC5\u987B\u5927\u4E8E\u8F93\u51FA\u9884\u7559\uFF0C\u624D\u80FD\u5F62\u6210\u6B63\u6570 Prompt \u8F93\u5165\u9884\u7B97\u3002"
|
|
9799
|
-
);
|
|
9800
|
-
}
|
|
9801
|
-
const messageBudgetTokens = inputBudgetTokens - input.toolDefinitionTokens;
|
|
9802
|
-
if (messageBudgetTokens < 1) {
|
|
9803
|
-
throw new Error(
|
|
9804
|
-
"Tool definitions \u5DF2\u5360\u6EE1 Prompt \u8F93\u5165\u9884\u7B97\uFF0C\u65E0\u6CD5\u4E3A messages \u4FDD\u7559 token\u3002"
|
|
9805
|
-
);
|
|
9806
|
-
}
|
|
9807
|
-
return {
|
|
9808
|
-
effectiveWindowTokens,
|
|
9809
|
-
outputLimitTokens,
|
|
9810
|
-
inputBudgetTokens,
|
|
9811
|
-
toolDefinitionTokens: input.toolDefinitionTokens,
|
|
9812
|
-
messageBudgetTokens
|
|
9813
|
-
};
|
|
9814
|
-
}
|
|
9815
|
-
function assertOptionalPositiveSafeInteger(value, field) {
|
|
9816
|
-
if (value !== void 0) {
|
|
9817
|
-
assertPositiveSafeInteger(value, field);
|
|
9818
|
-
}
|
|
9819
|
-
}
|
|
9820
|
-
function assertPositiveSafeInteger(value, field) {
|
|
9821
|
-
if (!Number.isSafeInteger(value) || value < 1) {
|
|
9822
|
-
throw new Error(`${field} \u5FC5\u987B\u662F\u6B63\u5B89\u5168\u6574\u6570\u3002`);
|
|
9823
|
-
}
|
|
9824
|
-
}
|
|
9825
|
-
function assertNonNegativeSafeInteger(value, field) {
|
|
9826
|
-
if (!Number.isSafeInteger(value) || value < 0) {
|
|
9827
|
-
throw new Error(`${field} \u5FC5\u987B\u662F\u975E\u8D1F\u5B89\u5168\u6574\u6570\u3002`);
|
|
9828
|
-
}
|
|
9829
|
-
}
|
|
9830
9854
|
var counter = zod.z.number().int().nonnegative();
|
|
9831
9855
|
var executor = zod.z.object({
|
|
9832
9856
|
stepCount: counter,
|
|
@@ -9919,6 +9943,51 @@ __export(tools_exports, {
|
|
|
9919
9943
|
stripRuntimeReservedToolContextPatch: () => stripRuntimeReservedToolContextPatch
|
|
9920
9944
|
});
|
|
9921
9945
|
|
|
9946
|
+
// src/runtime-kernel/tools/schema/functions/formatUnknownToolArguments.ts
|
|
9947
|
+
function spellingDistance(left, right) {
|
|
9948
|
+
let previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
9949
|
+
for (let row = 1; row <= left.length; row += 1) {
|
|
9950
|
+
const current = [row];
|
|
9951
|
+
for (let column = 1; column <= right.length; column += 1) {
|
|
9952
|
+
current[column] = Math.min(
|
|
9953
|
+
current[column - 1] + 1,
|
|
9954
|
+
previous[column] + 1,
|
|
9955
|
+
previous[column - 1] + (left[row - 1] === right[column - 1] ? 0 : 1)
|
|
9956
|
+
);
|
|
9957
|
+
}
|
|
9958
|
+
previous = current;
|
|
9959
|
+
}
|
|
9960
|
+
return previous[right.length];
|
|
9961
|
+
}
|
|
9962
|
+
function nearestField(field, allowedFields) {
|
|
9963
|
+
const threshold = field.length < 5 ? 1 : 2;
|
|
9964
|
+
let nearest;
|
|
9965
|
+
let bestDistance = threshold + 1;
|
|
9966
|
+
for (const candidate of allowedFields) {
|
|
9967
|
+
if (Math.abs(field.length - candidate.length) > threshold) continue;
|
|
9968
|
+
const distance = spellingDistance(field, candidate);
|
|
9969
|
+
if (distance < bestDistance) {
|
|
9970
|
+
bestDistance = distance;
|
|
9971
|
+
nearest = candidate;
|
|
9972
|
+
} else if (distance === bestDistance) {
|
|
9973
|
+
nearest = void 0;
|
|
9974
|
+
}
|
|
9975
|
+
}
|
|
9976
|
+
return bestDistance <= threshold ? nearest : void 0;
|
|
9977
|
+
}
|
|
9978
|
+
function formatUnknownToolArguments(fields, allowedFields) {
|
|
9979
|
+
const suggestions = fields.flatMap((field) => {
|
|
9980
|
+
const candidate = nearestField(field, allowedFields);
|
|
9981
|
+
return candidate ? [`${JSON.stringify(field)} \u2192 ${JSON.stringify(candidate)}`] : [];
|
|
9982
|
+
});
|
|
9983
|
+
return [
|
|
9984
|
+
`Additional properties not allowed: ${fields.map((field) => JSON.stringify(field)).join(", ")}.`,
|
|
9985
|
+
`Allowed parameters: ${allowedFields.map((field) => JSON.stringify(field)).join(", ") || "(none)"}.`,
|
|
9986
|
+
...suggestions.length > 0 ? [`Possible spelling corrections: ${suggestions.join("; ")}.`] : [],
|
|
9987
|
+
"Remove unsupported parameters before retrying; no arguments were changed or executed."
|
|
9988
|
+
].join(" ");
|
|
9989
|
+
}
|
|
9990
|
+
|
|
9922
9991
|
// src/runtime-kernel/tools/toolContracts.ts
|
|
9923
9992
|
var BaseTool = class {
|
|
9924
9993
|
idempotency;
|
|
@@ -9954,7 +10023,7 @@ var BaseTool = class {
|
|
|
9954
10023
|
if (unknownFields.length > 0) {
|
|
9955
10024
|
return {
|
|
9956
10025
|
success: false,
|
|
9957
|
-
error:
|
|
10026
|
+
error: formatUnknownToolArguments(unknownFields, [...allowed])
|
|
9958
10027
|
};
|
|
9959
10028
|
}
|
|
9960
10029
|
}
|
|
@@ -11674,7 +11743,15 @@ async function callWithRetryFallback(params) {
|
|
|
11674
11743
|
eventHandler,
|
|
11675
11744
|
requirement
|
|
11676
11745
|
});
|
|
11677
|
-
const
|
|
11746
|
+
const measuredContextUsage = invocationContext?.measurePromptUsage ? await invocationContext.measurePromptUsage(activeModelId, messages) : void 0;
|
|
11747
|
+
const attemptContextUsage = measuredContextUsage ? {
|
|
11748
|
+
...measuredContextUsage,
|
|
11749
|
+
output_limit_tokens: Math.min(
|
|
11750
|
+
measuredContextUsage.output_limit_tokens,
|
|
11751
|
+
options.max_tokens ?? measuredContextUsage.output_limit_tokens
|
|
11752
|
+
)
|
|
11753
|
+
} : void 0;
|
|
11754
|
+
const attemptOptions = attemptContextUsage ? { ...options, max_tokens: attemptContextUsage.output_limit_tokens } : options;
|
|
11678
11755
|
const isRetry = attempt > 0;
|
|
11679
11756
|
let pendingErrorEvent = null;
|
|
11680
11757
|
let streamedErrorClassification;
|
|
@@ -11691,7 +11768,7 @@ async function callWithRetryFallback(params) {
|
|
|
11691
11768
|
inferencePort: deps.inferencePort,
|
|
11692
11769
|
modelId: activeModelId,
|
|
11693
11770
|
messages: resolvedMessages,
|
|
11694
|
-
options,
|
|
11771
|
+
options: attemptOptions,
|
|
11695
11772
|
eventHandler: attemptTracker.handle,
|
|
11696
11773
|
onErrorClassification(classification) {
|
|
11697
11774
|
streamedErrorClassification = classification;
|
|
@@ -11703,7 +11780,7 @@ async function callWithRetryFallback(params) {
|
|
|
11703
11780
|
deps.inferencePort,
|
|
11704
11781
|
activeModelId,
|
|
11705
11782
|
resolvedMessages,
|
|
11706
|
-
|
|
11783
|
+
attemptOptions,
|
|
11707
11784
|
signal,
|
|
11708
11785
|
traceId
|
|
11709
11786
|
);
|