@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
|
@@ -9,6 +9,7 @@ interface ResolveEffectivePromptBudgetInput {
|
|
|
9
9
|
}
|
|
10
10
|
interface EffectivePromptBudget {
|
|
11
11
|
effectiveWindowTokens: number;
|
|
12
|
+
/** 构建期的输出能力/策略上限;最终请求还须按实际 Prompt 占用收窄。 */
|
|
12
13
|
outputLimitTokens: number;
|
|
13
14
|
inputBudgetTokens: number;
|
|
14
15
|
toolDefinitionTokens: number;
|
|
@@ -17,8 +18,9 @@ interface EffectivePromptBudget {
|
|
|
17
18
|
/**
|
|
18
19
|
* 统一解析一次 LLM 调用的有效 Prompt 预算。
|
|
19
20
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
21
|
+
* 模型输出能力不是必须预留的空间。只有显式响应 policy(或无 route 的独立 host
|
|
22
|
+
* fallback)才预留固定响应预算;其余情况为生成至少保留一个 token,最终计量后
|
|
23
|
+
* 再收窄实际输出上限。prepared tools 必须先于 messages 占用输入预算。
|
|
22
24
|
* 该纯函数位于 framework shared 层,让 Context Manager 与 Graph inference 使用同一个数学 owner。
|
|
23
25
|
*/
|
|
24
26
|
declare function resolveEffectivePromptBudget(input: ResolveEffectivePromptBudgetInput): EffectivePromptBudget;
|
|
@@ -9,6 +9,7 @@ interface ResolveEffectivePromptBudgetInput {
|
|
|
9
9
|
}
|
|
10
10
|
interface EffectivePromptBudget {
|
|
11
11
|
effectiveWindowTokens: number;
|
|
12
|
+
/** 构建期的输出能力/策略上限;最终请求还须按实际 Prompt 占用收窄。 */
|
|
12
13
|
outputLimitTokens: number;
|
|
13
14
|
inputBudgetTokens: number;
|
|
14
15
|
toolDefinitionTokens: number;
|
|
@@ -17,8 +18,9 @@ interface EffectivePromptBudget {
|
|
|
17
18
|
/**
|
|
18
19
|
* 统一解析一次 LLM 调用的有效 Prompt 预算。
|
|
19
20
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
21
|
+
* 模型输出能力不是必须预留的空间。只有显式响应 policy(或无 route 的独立 host
|
|
22
|
+
* fallback)才预留固定响应预算;其余情况为生成至少保留一个 token,最终计量后
|
|
23
|
+
* 再收窄实际输出上限。prepared tools 必须先于 messages 占用输入预算。
|
|
22
24
|
* 该纯函数位于 framework shared 层,让 Context Manager 与 Graph inference 使用同一个数学 owner。
|
|
23
25
|
*/
|
|
24
26
|
declare function resolveEffectivePromptBudget(input: ResolveEffectivePromptBudgetInput): EffectivePromptBudget;
|
package/dist/testkit.cjs
CHANGED
|
@@ -5226,6 +5226,88 @@ function evaluatePrimaryPromptCapacity(snapshot) {
|
|
|
5226
5226
|
};
|
|
5227
5227
|
}
|
|
5228
5228
|
|
|
5229
|
+
// src/shared/prompt-budget.ts
|
|
5230
|
+
var DEFAULT_PROMPT_CONTEXT_WINDOW_TOKENS = 256e3;
|
|
5231
|
+
var DEFAULT_PROMPT_MAX_OUTPUT_TOKENS = 16384;
|
|
5232
|
+
function resolveEffectivePromptBudget(input) {
|
|
5233
|
+
assertOptionalPositiveSafeInteger(input.policyMaxTokens, "policyMaxTokens");
|
|
5234
|
+
assertOptionalPositiveSafeInteger(
|
|
5235
|
+
input.policyReservedForResponse,
|
|
5236
|
+
"policyReservedForResponse"
|
|
5237
|
+
);
|
|
5238
|
+
assertOptionalPositiveSafeInteger(
|
|
5239
|
+
input.modelContextWindowTokens,
|
|
5240
|
+
"modelContextWindowTokens"
|
|
5241
|
+
);
|
|
5242
|
+
assertOptionalPositiveSafeInteger(input.modelMaxOutputTokens, "modelMaxOutputTokens");
|
|
5243
|
+
assertOptionalPositiveSafeInteger(
|
|
5244
|
+
input.fallbackContextWindowTokens,
|
|
5245
|
+
"fallbackContextWindowTokens"
|
|
5246
|
+
);
|
|
5247
|
+
assertOptionalPositiveSafeInteger(
|
|
5248
|
+
input.fallbackMaxOutputTokens,
|
|
5249
|
+
"fallbackMaxOutputTokens"
|
|
5250
|
+
);
|
|
5251
|
+
assertNonNegativeSafeInteger(input.toolDefinitionTokens, "toolDefinitionTokens");
|
|
5252
|
+
const modelContextWindowTokens = input.modelContextWindowTokens ?? input.fallbackContextWindowTokens ?? DEFAULT_PROMPT_CONTEXT_WINDOW_TOKENS;
|
|
5253
|
+
const modelMaxOutputTokens = input.modelMaxOutputTokens ?? input.fallbackMaxOutputTokens ?? DEFAULT_PROMPT_MAX_OUTPUT_TOKENS;
|
|
5254
|
+
const effectiveWindowTokens = input.policyMaxTokens === void 0 ? modelContextWindowTokens : Math.min(input.policyMaxTokens, modelContextWindowTokens);
|
|
5255
|
+
const outputLimitTokens = input.policyReservedForResponse === void 0 ? modelMaxOutputTokens : Math.min(input.policyReservedForResponse, modelMaxOutputTokens);
|
|
5256
|
+
const responseReservationTokens = input.policyReservedForResponse !== void 0 || input.modelContextWindowTokens === void 0 ? outputLimitTokens : 1;
|
|
5257
|
+
const inputBudgetTokens = effectiveWindowTokens - responseReservationTokens;
|
|
5258
|
+
if (inputBudgetTokens < 1) {
|
|
5259
|
+
throw new Error(
|
|
5260
|
+
"\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"
|
|
5261
|
+
);
|
|
5262
|
+
}
|
|
5263
|
+
const messageBudgetTokens = inputBudgetTokens - input.toolDefinitionTokens;
|
|
5264
|
+
if (messageBudgetTokens < 1) {
|
|
5265
|
+
throw new Error(
|
|
5266
|
+
"Tool definitions \u5DF2\u5360\u6EE1 Prompt \u8F93\u5165\u9884\u7B97\uFF0C\u65E0\u6CD5\u4E3A messages \u4FDD\u7559 token\u3002"
|
|
5267
|
+
);
|
|
5268
|
+
}
|
|
5269
|
+
return {
|
|
5270
|
+
effectiveWindowTokens,
|
|
5271
|
+
outputLimitTokens,
|
|
5272
|
+
inputBudgetTokens,
|
|
5273
|
+
toolDefinitionTokens: input.toolDefinitionTokens,
|
|
5274
|
+
messageBudgetTokens
|
|
5275
|
+
};
|
|
5276
|
+
}
|
|
5277
|
+
function assertOptionalPositiveSafeInteger(value, field) {
|
|
5278
|
+
if (value !== void 0) {
|
|
5279
|
+
assertPositiveSafeInteger(value, field);
|
|
5280
|
+
}
|
|
5281
|
+
}
|
|
5282
|
+
function assertPositiveSafeInteger(value, field) {
|
|
5283
|
+
if (!Number.isSafeInteger(value) || value < 1) {
|
|
5284
|
+
throw new Error(`${field} \u5FC5\u987B\u662F\u6B63\u5B89\u5168\u6574\u6570\u3002`);
|
|
5285
|
+
}
|
|
5286
|
+
}
|
|
5287
|
+
function assertNonNegativeSafeInteger(value, field) {
|
|
5288
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
5289
|
+
throw new Error(`${field} \u5FC5\u987B\u662F\u975E\u8D1F\u5B89\u5168\u6574\u6570\u3002`);
|
|
5290
|
+
}
|
|
5291
|
+
}
|
|
5292
|
+
|
|
5293
|
+
// src/runtime-kernel/graph-engine/functions/resolveFallbackPromptBudget.ts
|
|
5294
|
+
function resolveFallbackPromptBudget(primaryBudget, requiredOutputLimitTokens, route) {
|
|
5295
|
+
if (!route) throw new Error("Fallback \u6A21\u578B\u7F3A\u5C11\u6B63\u5F0F inference route\u3002");
|
|
5296
|
+
const budget = resolveEffectivePromptBudget({
|
|
5297
|
+
policyMaxTokens: primaryBudget.effectiveWindowTokens,
|
|
5298
|
+
policyReservedForResponse: requiredOutputLimitTokens,
|
|
5299
|
+
modelContextWindowTokens: route.context_window_tokens,
|
|
5300
|
+
modelMaxOutputTokens: route.max_output_tokens,
|
|
5301
|
+
toolDefinitionTokens: primaryBudget.toolDefinitionTokens
|
|
5302
|
+
});
|
|
5303
|
+
const inputBudgetTokens = Math.min(primaryBudget.inputBudgetTokens, budget.inputBudgetTokens);
|
|
5304
|
+
return {
|
|
5305
|
+
...budget,
|
|
5306
|
+
inputBudgetTokens,
|
|
5307
|
+
messageBudgetTokens: inputBudgetTokens - budget.toolDefinitionTokens
|
|
5308
|
+
};
|
|
5309
|
+
}
|
|
5310
|
+
|
|
5229
5311
|
// src/runtime-kernel/graph-engine/tick-pipeline/stages/executeLlmStage.ts
|
|
5230
5312
|
function createExecuteLlmStage(dependencies) {
|
|
5231
5313
|
return defineTickStage({
|
|
@@ -5304,7 +5386,11 @@ function createExecuteLlmStage(dependencies) {
|
|
|
5304
5386
|
servedModelId: activeModelId,
|
|
5305
5387
|
messages,
|
|
5306
5388
|
llmOptions: ctx.llmOptions,
|
|
5307
|
-
promptBudget
|
|
5389
|
+
promptBudget: activeModelId === ctx.modelId ? promptBudget : resolveFallbackPromptBudget(
|
|
5390
|
+
promptBudget,
|
|
5391
|
+
ctx.promptUsageCandidate?.output_limit_tokens ?? promptBudget.outputLimitTokens,
|
|
5392
|
+
dependencies.modelCatalog.getModelById(activeModelId)?.inference_route
|
|
5393
|
+
),
|
|
5308
5394
|
measurementPolicy: promptUsageMeasurementPolicy,
|
|
5309
5395
|
imageInputTokens: ctx.imageInputAdmissionEvidence?.attachments.reduce(
|
|
5310
5396
|
(total, attachment) => total + attachment.estimatedTokens,
|
|
@@ -5334,7 +5420,7 @@ function createExecuteLlmStage(dependencies) {
|
|
|
5334
5420
|
return evaluateFallbackPromptCapacity({
|
|
5335
5421
|
contextWindowTokens: route?.context_window_tokens,
|
|
5336
5422
|
maxOutputTokens: route?.max_output_tokens,
|
|
5337
|
-
requiredOutputLimitTokens: promptBudget.outputLimitTokens,
|
|
5423
|
+
requiredOutputLimitTokens: ctx.promptUsageCandidate?.output_limit_tokens ?? promptBudget.outputLimitTokens,
|
|
5338
5424
|
estimatedPromptTokens: weights.systemPromptTokens + weights.conversationTokens + weights.toolDefinitionTokens
|
|
5339
5425
|
});
|
|
5340
5426
|
}
|
|
@@ -5622,7 +5708,7 @@ async function executeContextCompaction(dependencies, input) {
|
|
|
5622
5708
|
response = await dependencies.llmCaller.call(
|
|
5623
5709
|
input.modelId,
|
|
5624
5710
|
messages,
|
|
5625
|
-
compactionOptions,
|
|
5711
|
+
{ ...compactionOptions, max_tokens: compactionPromptUsage.output_limit_tokens },
|
|
5626
5712
|
input.signal,
|
|
5627
5713
|
{ imageInputAdmissionEvidence: input.imageInputAdmissionEvidence }
|
|
5628
5714
|
);
|
|
@@ -5685,7 +5771,7 @@ async function executeContextCompaction(dependencies, input) {
|
|
|
5685
5771
|
conversationId: input.conversationId,
|
|
5686
5772
|
turnId: input.turnId,
|
|
5687
5773
|
timestamp: Date.now(),
|
|
5688
|
-
maxOutputTokens
|
|
5774
|
+
maxOutputTokens: compactionPromptUsage.output_limit_tokens
|
|
5689
5775
|
});
|
|
5690
5776
|
} catch (error) {
|
|
5691
5777
|
if (error instanceof Error && error.name === "AbortError") throw error;
|
|
@@ -5870,14 +5956,11 @@ function buildCompactionLlmOptions(input) {
|
|
|
5870
5956
|
};
|
|
5871
5957
|
}
|
|
5872
5958
|
function buildCompactionPromptBudget(mainBudget, maxOutputTokens) {
|
|
5873
|
-
|
|
5874
|
-
|
|
5875
|
-
|
|
5876
|
-
|
|
5877
|
-
|
|
5878
|
-
toolDefinitionTokens: mainBudget.toolDefinitionTokens,
|
|
5879
|
-
messageBudgetTokens: inputBudgetTokens - mainBudget.toolDefinitionTokens
|
|
5880
|
-
};
|
|
5959
|
+
return resolveEffectivePromptBudget({
|
|
5960
|
+
modelContextWindowTokens: mainBudget.effectiveWindowTokens,
|
|
5961
|
+
modelMaxOutputTokens: maxOutputTokens,
|
|
5962
|
+
toolDefinitionTokens: mainBudget.toolDefinitionTokens
|
|
5963
|
+
});
|
|
5881
5964
|
}
|
|
5882
5965
|
|
|
5883
5966
|
// src/runtime-kernel/graph-engine/tick-pipeline/stages/compactContextStage.ts
|
|
@@ -6454,6 +6537,10 @@ function createPromptUsageMeasurer(dependencies) {
|
|
|
6454
6537
|
});
|
|
6455
6538
|
const usedTokens = remoteResult?.inputTokens ?? calibratedLocalTotal;
|
|
6456
6539
|
const components = normalizePromptUsageComponents(usedTokens, weights);
|
|
6540
|
+
const outputLimitTokens = usedTokens <= input.promptBudget.inputBudgetTokens ? Math.min(
|
|
6541
|
+
input.promptBudget.outputLimitTokens,
|
|
6542
|
+
input.promptBudget.effectiveWindowTokens - usedTokens
|
|
6543
|
+
) : input.promptBudget.outputLimitTokens;
|
|
6457
6544
|
return ContextUsageSnapshot.parse({
|
|
6458
6545
|
basis: "last_completed_llm_prompt",
|
|
6459
6546
|
budget_model_id: input.budgetModelId,
|
|
@@ -6463,7 +6550,7 @@ function createPromptUsageMeasurer(dependencies) {
|
|
|
6463
6550
|
component_attribution: "normalized_local_estimate",
|
|
6464
6551
|
input_budget_tokens: input.promptBudget.inputBudgetTokens,
|
|
6465
6552
|
remaining_tokens: input.promptBudget.inputBudgetTokens - usedTokens,
|
|
6466
|
-
output_limit_tokens:
|
|
6553
|
+
output_limit_tokens: outputLimitTokens,
|
|
6467
6554
|
source: remoteResult?.source ?? "local-estimate",
|
|
6468
6555
|
confidence: remoteResult?.confidence ?? "estimate",
|
|
6469
6556
|
measured_at: now()
|
|
@@ -9952,69 +10039,6 @@ function createHostToolCallBootstrap(input) {
|
|
|
9952
10039
|
}
|
|
9953
10040
|
};
|
|
9954
10041
|
}
|
|
9955
|
-
|
|
9956
|
-
// src/shared/prompt-budget.ts
|
|
9957
|
-
var DEFAULT_PROMPT_CONTEXT_WINDOW_TOKENS = 256e3;
|
|
9958
|
-
var DEFAULT_PROMPT_MAX_OUTPUT_TOKENS = 16384;
|
|
9959
|
-
function resolveEffectivePromptBudget(input) {
|
|
9960
|
-
assertOptionalPositiveSafeInteger(input.policyMaxTokens, "policyMaxTokens");
|
|
9961
|
-
assertOptionalPositiveSafeInteger(
|
|
9962
|
-
input.policyReservedForResponse,
|
|
9963
|
-
"policyReservedForResponse"
|
|
9964
|
-
);
|
|
9965
|
-
assertOptionalPositiveSafeInteger(
|
|
9966
|
-
input.modelContextWindowTokens,
|
|
9967
|
-
"modelContextWindowTokens"
|
|
9968
|
-
);
|
|
9969
|
-
assertOptionalPositiveSafeInteger(input.modelMaxOutputTokens, "modelMaxOutputTokens");
|
|
9970
|
-
assertOptionalPositiveSafeInteger(
|
|
9971
|
-
input.fallbackContextWindowTokens,
|
|
9972
|
-
"fallbackContextWindowTokens"
|
|
9973
|
-
);
|
|
9974
|
-
assertOptionalPositiveSafeInteger(
|
|
9975
|
-
input.fallbackMaxOutputTokens,
|
|
9976
|
-
"fallbackMaxOutputTokens"
|
|
9977
|
-
);
|
|
9978
|
-
assertNonNegativeSafeInteger(input.toolDefinitionTokens, "toolDefinitionTokens");
|
|
9979
|
-
const modelContextWindowTokens = input.modelContextWindowTokens ?? input.fallbackContextWindowTokens ?? DEFAULT_PROMPT_CONTEXT_WINDOW_TOKENS;
|
|
9980
|
-
const modelMaxOutputTokens = input.modelMaxOutputTokens ?? input.fallbackMaxOutputTokens ?? DEFAULT_PROMPT_MAX_OUTPUT_TOKENS;
|
|
9981
|
-
const effectiveWindowTokens = input.policyMaxTokens === void 0 ? modelContextWindowTokens : Math.min(input.policyMaxTokens, modelContextWindowTokens);
|
|
9982
|
-
const outputLimitTokens = input.policyReservedForResponse === void 0 ? modelMaxOutputTokens : Math.min(input.policyReservedForResponse, modelMaxOutputTokens);
|
|
9983
|
-
const inputBudgetTokens = effectiveWindowTokens - outputLimitTokens;
|
|
9984
|
-
if (inputBudgetTokens < 1) {
|
|
9985
|
-
throw new Error(
|
|
9986
|
-
"\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"
|
|
9987
|
-
);
|
|
9988
|
-
}
|
|
9989
|
-
const messageBudgetTokens = inputBudgetTokens - input.toolDefinitionTokens;
|
|
9990
|
-
if (messageBudgetTokens < 1) {
|
|
9991
|
-
throw new Error(
|
|
9992
|
-
"Tool definitions \u5DF2\u5360\u6EE1 Prompt \u8F93\u5165\u9884\u7B97\uFF0C\u65E0\u6CD5\u4E3A messages \u4FDD\u7559 token\u3002"
|
|
9993
|
-
);
|
|
9994
|
-
}
|
|
9995
|
-
return {
|
|
9996
|
-
effectiveWindowTokens,
|
|
9997
|
-
outputLimitTokens,
|
|
9998
|
-
inputBudgetTokens,
|
|
9999
|
-
toolDefinitionTokens: input.toolDefinitionTokens,
|
|
10000
|
-
messageBudgetTokens
|
|
10001
|
-
};
|
|
10002
|
-
}
|
|
10003
|
-
function assertOptionalPositiveSafeInteger(value, field) {
|
|
10004
|
-
if (value !== void 0) {
|
|
10005
|
-
assertPositiveSafeInteger(value, field);
|
|
10006
|
-
}
|
|
10007
|
-
}
|
|
10008
|
-
function assertPositiveSafeInteger(value, field) {
|
|
10009
|
-
if (!Number.isSafeInteger(value) || value < 1) {
|
|
10010
|
-
throw new Error(`${field} \u5FC5\u987B\u662F\u6B63\u5B89\u5168\u6574\u6570\u3002`);
|
|
10011
|
-
}
|
|
10012
|
-
}
|
|
10013
|
-
function assertNonNegativeSafeInteger(value, field) {
|
|
10014
|
-
if (!Number.isSafeInteger(value) || value < 0) {
|
|
10015
|
-
throw new Error(`${field} \u5FC5\u987B\u662F\u975E\u8D1F\u5B89\u5168\u6574\u6570\u3002`);
|
|
10016
|
-
}
|
|
10017
|
-
}
|
|
10018
10042
|
var counter = zod.z.number().int().nonnegative();
|
|
10019
10043
|
var executor = zod.z.object({
|
|
10020
10044
|
stepCount: counter,
|
|
@@ -10107,6 +10131,51 @@ __export(tools_exports, {
|
|
|
10107
10131
|
stripRuntimeReservedToolContextPatch: () => stripRuntimeReservedToolContextPatch
|
|
10108
10132
|
});
|
|
10109
10133
|
|
|
10134
|
+
// src/runtime-kernel/tools/schema/functions/formatUnknownToolArguments.ts
|
|
10135
|
+
function spellingDistance(left, right) {
|
|
10136
|
+
let previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
10137
|
+
for (let row = 1; row <= left.length; row += 1) {
|
|
10138
|
+
const current = [row];
|
|
10139
|
+
for (let column = 1; column <= right.length; column += 1) {
|
|
10140
|
+
current[column] = Math.min(
|
|
10141
|
+
current[column - 1] + 1,
|
|
10142
|
+
previous[column] + 1,
|
|
10143
|
+
previous[column - 1] + (left[row - 1] === right[column - 1] ? 0 : 1)
|
|
10144
|
+
);
|
|
10145
|
+
}
|
|
10146
|
+
previous = current;
|
|
10147
|
+
}
|
|
10148
|
+
return previous[right.length];
|
|
10149
|
+
}
|
|
10150
|
+
function nearestField(field, allowedFields) {
|
|
10151
|
+
const threshold = field.length < 5 ? 1 : 2;
|
|
10152
|
+
let nearest;
|
|
10153
|
+
let bestDistance = threshold + 1;
|
|
10154
|
+
for (const candidate of allowedFields) {
|
|
10155
|
+
if (Math.abs(field.length - candidate.length) > threshold) continue;
|
|
10156
|
+
const distance = spellingDistance(field, candidate);
|
|
10157
|
+
if (distance < bestDistance) {
|
|
10158
|
+
bestDistance = distance;
|
|
10159
|
+
nearest = candidate;
|
|
10160
|
+
} else if (distance === bestDistance) {
|
|
10161
|
+
nearest = void 0;
|
|
10162
|
+
}
|
|
10163
|
+
}
|
|
10164
|
+
return bestDistance <= threshold ? nearest : void 0;
|
|
10165
|
+
}
|
|
10166
|
+
function formatUnknownToolArguments(fields, allowedFields) {
|
|
10167
|
+
const suggestions = fields.flatMap((field) => {
|
|
10168
|
+
const candidate = nearestField(field, allowedFields);
|
|
10169
|
+
return candidate ? [`${JSON.stringify(field)} \u2192 ${JSON.stringify(candidate)}`] : [];
|
|
10170
|
+
});
|
|
10171
|
+
return [
|
|
10172
|
+
`Additional properties not allowed: ${fields.map((field) => JSON.stringify(field)).join(", ")}.`,
|
|
10173
|
+
`Allowed parameters: ${allowedFields.map((field) => JSON.stringify(field)).join(", ") || "(none)"}.`,
|
|
10174
|
+
...suggestions.length > 0 ? [`Possible spelling corrections: ${suggestions.join("; ")}.`] : [],
|
|
10175
|
+
"Remove unsupported parameters before retrying; no arguments were changed or executed."
|
|
10176
|
+
].join(" ");
|
|
10177
|
+
}
|
|
10178
|
+
|
|
10110
10179
|
// src/runtime-kernel/tools/toolContracts.ts
|
|
10111
10180
|
var BaseTool = class {
|
|
10112
10181
|
idempotency;
|
|
@@ -10142,7 +10211,7 @@ var BaseTool = class {
|
|
|
10142
10211
|
if (unknownFields.length > 0) {
|
|
10143
10212
|
return {
|
|
10144
10213
|
success: false,
|
|
10145
|
-
error:
|
|
10214
|
+
error: formatUnknownToolArguments(unknownFields, [...allowed])
|
|
10146
10215
|
};
|
|
10147
10216
|
}
|
|
10148
10217
|
}
|
|
@@ -11862,7 +11931,15 @@ async function callWithRetryFallback(params) {
|
|
|
11862
11931
|
eventHandler,
|
|
11863
11932
|
requirement
|
|
11864
11933
|
});
|
|
11865
|
-
const
|
|
11934
|
+
const measuredContextUsage = invocationContext?.measurePromptUsage ? await invocationContext.measurePromptUsage(activeModelId, messages) : void 0;
|
|
11935
|
+
const attemptContextUsage = measuredContextUsage ? {
|
|
11936
|
+
...measuredContextUsage,
|
|
11937
|
+
output_limit_tokens: Math.min(
|
|
11938
|
+
measuredContextUsage.output_limit_tokens,
|
|
11939
|
+
options.max_tokens ?? measuredContextUsage.output_limit_tokens
|
|
11940
|
+
)
|
|
11941
|
+
} : void 0;
|
|
11942
|
+
const attemptOptions = attemptContextUsage ? { ...options, max_tokens: attemptContextUsage.output_limit_tokens } : options;
|
|
11866
11943
|
const isRetry = attempt > 0;
|
|
11867
11944
|
let pendingErrorEvent = null;
|
|
11868
11945
|
let streamedErrorClassification;
|
|
@@ -11879,7 +11956,7 @@ async function callWithRetryFallback(params) {
|
|
|
11879
11956
|
inferencePort: deps.inferencePort,
|
|
11880
11957
|
modelId: activeModelId,
|
|
11881
11958
|
messages: resolvedMessages,
|
|
11882
|
-
options,
|
|
11959
|
+
options: attemptOptions,
|
|
11883
11960
|
eventHandler: attemptTracker.handle,
|
|
11884
11961
|
onErrorClassification(classification) {
|
|
11885
11962
|
streamedErrorClassification = classification;
|
|
@@ -11891,7 +11968,7 @@ async function callWithRetryFallback(params) {
|
|
|
11891
11968
|
deps.inferencePort,
|
|
11892
11969
|
activeModelId,
|
|
11893
11970
|
resolvedMessages,
|
|
11894
|
-
|
|
11971
|
+
attemptOptions,
|
|
11895
11972
|
signal,
|
|
11896
11973
|
traceId
|
|
11897
11974
|
);
|