blun-king-cli 9.1.639 → 9.1.641
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 +12 -0
- package/bin/blun.js +0 -0
- package/bin/core-bootstrap.js +0 -0
- package/bin/king.js +0 -0
- package/bin/launcher-mode.js +0 -0
- package/bin/launcher-runtime.js +0 -0
- package/bin/managed-node.js +0 -0
- package/bin/native-command-runner.js +0 -0
- package/bin/native-module-repair.js +0 -0
- package/bin/node-runtime.js +0 -0
- package/bin/node-version.js +0 -0
- package/bin/plugin-bootstrap.js +0 -0
- package/bin/private-paths.js +0 -0
- package/bin/standard-tools-bootstrap.js +0 -0
- package/bin/update-copy.js +0 -0
- package/bin/update-lease.js +0 -0
- package/bin/update-notice.js +0 -0
- package/blun.mjs +1074 -716
- package/package.json +1 -1
- package/worker-host.mjs +1022 -665
package/blun.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// BLUN_BUILD_INPUT_SHA256:
|
|
2
|
+
// BLUN_BUILD_INPUT_SHA256:d4df2815be55011d12f15cf3f9b01e9c4c1ba80d84ef9322ad9e8fd20b13e018
|
|
3
3
|
/*! wasm-webp 0.1.0
|
|
4
4
|
MIT License
|
|
5
5
|
|
|
@@ -2751,21 +2751,23 @@ var init_blun = __esmMin((() => {
|
|
|
2751
2751
|
_clientFactory;
|
|
2752
2752
|
_files;
|
|
2753
2753
|
constructor(options) {
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2754
|
+
const apiKey = options.apiKey ?? process.env["BLUN_API_KEY"];
|
|
2755
|
+
this._apiKey = apiKey === void 0 || apiKey.length === 0 ? void 0 : apiKey;
|
|
2756
|
+
this._baseUrl = options.baseUrl ?? process.env["BLUN_BASE_URL"] ?? "https://api.blun.ai/v1";
|
|
2757
|
+
this._defaultHeaders = options.defaultHeaders;
|
|
2758
|
+
this._clientFactory = options.clientFactory;
|
|
2759
|
+
this._requestBodyByteLimit = options.requestBodyByteLimit;
|
|
2760
|
+
this._requestBodyByteLimitStore = options.requestBodyByteLimitStore;
|
|
2761
|
+
this._model = options.model;
|
|
2762
|
+
this._stream = options.stream ?? true;
|
|
2763
|
+
this._generationKwargs = { ...options.generationKwargs };
|
|
2764
|
+
this._supportEfforts = options.supportEfforts ?? [];
|
|
2765
|
+
this._client = this._apiKey === void 0 ? void 0 : createFetchHttpClient({
|
|
2766
|
+
apiKey: this._apiKey,
|
|
2767
|
+
baseUrl: this._baseUrl,
|
|
2768
|
+
defaultHeaders: this._defaultHeaders
|
|
2769
|
+
});
|
|
2770
|
+
}
|
|
2769
2771
|
get modelName() {
|
|
2770
2772
|
return this._model;
|
|
2771
2773
|
}
|
|
@@ -2802,116 +2804,139 @@ var init_blun = __esmMin((() => {
|
|
|
2802
2804
|
};
|
|
2803
2805
|
}
|
|
2804
2806
|
async generate(systemPrompt, tools, history, options) {
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2865
|
-
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2807
|
+
const messages = [];
|
|
2808
|
+
if (systemPrompt)
|
|
2809
|
+
messages.push({
|
|
2810
|
+
role: "system",
|
|
2811
|
+
content: systemPrompt
|
|
2812
|
+
});
|
|
2813
|
+
const normalizedHistory = normalizeToolCallIdsForProvider(history, BLUN_TOOL_CALL_ID_POLICY);
|
|
2814
|
+
for (const msg of normalizedHistory) {
|
|
2815
|
+
if (isEmptyAssistantHistoryMessage(msg))
|
|
2816
|
+
continue;
|
|
2817
|
+
messages.push(convertMessage(msg));
|
|
2818
|
+
}
|
|
2819
|
+
const kwargs = { ...this._generationKwargs };
|
|
2820
|
+
for (const key of Object.keys(kwargs))
|
|
2821
|
+
if (kwargs[key] === void 0)
|
|
2822
|
+
delete kwargs[key];
|
|
2823
|
+
if (kwargs["max_completion_tokens"] === void 0 && kwargs["max_tokens"] !== void 0)
|
|
2824
|
+
kwargs["max_completion_tokens"] = kwargs["max_tokens"];
|
|
2825
|
+
delete kwargs["max_tokens"];
|
|
2826
|
+
const { extra_body: extraBody, ...requestKwargs } = kwargs;
|
|
2827
|
+
const extraBodyRecord = typeof extraBody === "object" && extraBody !== null ? { ...extraBody } : {};
|
|
2828
|
+
const thinking = sanitizeThinkingConfig(extraBodyRecord.thinking);
|
|
2829
|
+
const thinkingRecord = thinking;
|
|
2830
|
+
const rawThinkingType = thinkingRecord?.["type"];
|
|
2831
|
+
const thinkingType = rawThinkingType === "enabled" || rawThinkingType === "disabled" ? rawThinkingType : void 0;
|
|
2832
|
+
const rawThinkingEffort = thinkingRecord?.["effort"];
|
|
2833
|
+
const thinkingEffort = typeof rawThinkingEffort === "string" ? rawThinkingEffort : void 0;
|
|
2834
|
+
const rawReasoningEffort = requestKwargs["reasoning_effort"];
|
|
2835
|
+
const extraReasoningEffort = extraBodyRecord["reasoning_effort"];
|
|
2836
|
+
const effectiveThinkingEffort = thinkingEffort ?? (thinkingType === void 0 && typeof extraReasoningEffort === "string" ? extraReasoningEffort : void 0) ?? (thinkingType === void 0 && typeof rawReasoningEffort === "string" ? rawReasoningEffort : void 0);
|
|
2837
|
+
const configuredTemplateKwargs = requestKwargs["chat_template_kwargs"];
|
|
2838
|
+
const extraTemplateKwargs = extraBodyRecord["chat_template_kwargs"];
|
|
2839
|
+
let safeTemplateKwargs = {};
|
|
2840
|
+
let templateThinking;
|
|
2841
|
+
if (typeof configuredTemplateKwargs === "object" && configuredTemplateKwargs !== null) {
|
|
2842
|
+
const { reasoning_effort: _nestedReasoningEffort, enable_thinking: configuredEnableThinking, thinking: configuredThinking, ...rest } = configuredTemplateKwargs;
|
|
2843
|
+
safeTemplateKwargs = rest;
|
|
2844
|
+
if (typeof configuredThinking === "boolean")
|
|
2845
|
+
templateThinking = configuredThinking;
|
|
2846
|
+
else if (typeof configuredEnableThinking === "boolean")
|
|
2847
|
+
templateThinking = configuredEnableThinking;
|
|
2848
|
+
}
|
|
2849
|
+
if (typeof extraTemplateKwargs === "object" && extraTemplateKwargs !== null) {
|
|
2850
|
+
const { reasoning_effort: _extraNestedReasoningEffort, enable_thinking: extraEnableThinking, thinking: extraThinking, ...rest } = extraTemplateKwargs;
|
|
2851
|
+
safeTemplateKwargs = {
|
|
2852
|
+
...safeTemplateKwargs,
|
|
2853
|
+
...rest
|
|
2854
|
+
};
|
|
2855
|
+
if (typeof extraThinking === "boolean")
|
|
2856
|
+
templateThinking = extraThinking;
|
|
2857
|
+
else if (typeof extraEnableThinking === "boolean")
|
|
2858
|
+
templateThinking = extraEnableThinking;
|
|
2859
|
+
}
|
|
2860
|
+
const hasThinkingIntent = thinkingType !== void 0 || effectiveThinkingEffort !== void 0 || templateThinking !== void 0;
|
|
2861
|
+
const thinkingDisabled = thinkingType === "disabled" || effectiveThinkingEffort === "off" || effectiveThinkingEffort === "none" || thinkingType === void 0 && effectiveThinkingEffort === void 0 && templateThinking === false;
|
|
2862
|
+
const templateReasoningEffort = !thinkingDisabled && typeof thinkingEffort === "string" && this._supportEfforts.includes(thinkingEffort) ? toBlunTemplateEffort(thinkingEffort) : void 0;
|
|
2863
|
+
delete requestKwargs["reasoning_effort"];
|
|
2864
|
+
delete requestKwargs["thinking"];
|
|
2865
|
+
delete requestKwargs["enable_thinking"];
|
|
2866
|
+
delete requestKwargs["stream"];
|
|
2867
|
+
delete requestKwargs["model"];
|
|
2868
|
+
delete requestKwargs["messages"];
|
|
2869
|
+
if (hasThinkingIntent)
|
|
2870
|
+
requestKwargs["chat_template_kwargs"] = {
|
|
2871
|
+
...safeTemplateKwargs,
|
|
2872
|
+
thinking: !thinkingDisabled,
|
|
2873
|
+
...templateReasoningEffort === void 0 ? {} : { reasoning_effort: templateReasoningEffort }
|
|
2874
|
+
};
|
|
2875
|
+
else if (typeof configuredTemplateKwargs === "object" || typeof extraTemplateKwargs === "object")
|
|
2876
|
+
if (Object.keys(safeTemplateKwargs).length > 0)
|
|
2877
|
+
requestKwargs["chat_template_kwargs"] = safeTemplateKwargs;
|
|
2878
|
+
else
|
|
2879
|
+
delete requestKwargs["chat_template_kwargs"];
|
|
2880
|
+
const requestExtraBody = extraBodyRecord;
|
|
2881
|
+
delete requestExtraBody["reasoning_effort"];
|
|
2882
|
+
delete requestExtraBody["chat_template_kwargs"];
|
|
2883
|
+
delete requestExtraBody["enable_thinking"];
|
|
2884
|
+
delete requestExtraBody["thinking"];
|
|
2885
|
+
delete requestExtraBody["stream"];
|
|
2886
|
+
delete requestExtraBody["model"];
|
|
2887
|
+
delete requestExtraBody["messages"];
|
|
2888
|
+
if (!thinkingDisabled && thinking?.keep !== void 0)
|
|
2889
|
+
requestExtraBody["thinking"] = { keep: thinking.keep };
|
|
2890
|
+
const effectiveTemplateKwargs = requestKwargs["chat_template_kwargs"];
|
|
2891
|
+
const reasoningRequested = typeof effectiveTemplateKwargs === "object" && effectiveTemplateKwargs !== null && effectiveTemplateKwargs.thinking === true;
|
|
2892
|
+
const createParams = {
|
|
2893
|
+
model: this._model,
|
|
2894
|
+
messages,
|
|
2895
|
+
stream: this._stream,
|
|
2896
|
+
...requestKwargs,
|
|
2897
|
+
...requestExtraBody
|
|
2898
|
+
};
|
|
2899
|
+
const visionAttachments = collectBlunVisionAttachments(normalizedHistory);
|
|
2900
|
+
if (visionAttachments.length > 0)
|
|
2901
|
+
createParams["visionAttachments"] = visionAttachments;
|
|
2902
|
+
if (tools.length > 0)
|
|
2903
|
+
createParams["tools"] = tools.map((t) => convertTool(t));
|
|
2904
|
+
if (this._stream)
|
|
2905
|
+
createParams["stream_options"] = { include_usage: true };
|
|
2906
|
+
try {
|
|
2907
|
+
const measured = await t3458Measure(this, createParams, options);
|
|
2908
|
+
const client = this._createClient(options?.auth);
|
|
2909
|
+
const requestOptions = options?.signal !== void 0 || options?.transportMaxRetries !== void 0 || options?.onTransportAttempt !== void 0 ? {
|
|
2910
|
+
...options.signal !== void 0 ? { signal: options.signal } : {},
|
|
2911
|
+
...options.transportMaxRetries !== void 0 ? { maxRetries: options.transportMaxRetries } : {},
|
|
2912
|
+
...options.onTransportAttempt !== void 0 ? { onTransportAttempt: options.onTransportAttempt } : {}
|
|
2913
|
+
} : void 0;
|
|
2914
|
+
options?.onRequestSent?.();
|
|
2915
|
+
let response;
|
|
2916
|
+
try {
|
|
2917
|
+
response = await client.chat.completions.create(createParams, requestOptions);
|
|
2918
|
+
}
|
|
2919
|
+
catch (error) {
|
|
2920
|
+
await t3458Learn(this, error, { key: t3458Key(this._baseUrl) }, options);
|
|
2921
|
+
const rejection = normalizeFetchHttpError(error);
|
|
2922
|
+
const cap = rejection instanceof APIContextOverflowError ? contextOverflowOutputCap(rejection, createParams["max_completion_tokens"]) : void 0;
|
|
2923
|
+
if (cap === void 0)
|
|
2924
|
+
throw rejection;
|
|
2925
|
+
options?.signal?.throwIfAborted();
|
|
2926
|
+
options?.onRequestSent?.();
|
|
2927
|
+
response = await client.chat.completions.create({
|
|
2928
|
+
...createParams,
|
|
2929
|
+
max_completion_tokens: cap
|
|
2930
|
+
}, requestOptions);
|
|
2931
|
+
}
|
|
2932
|
+
return new BlunStreamedMessage(response, this._stream, reasoningRequested);
|
|
2933
|
+
}
|
|
2934
|
+
catch (error) {
|
|
2935
|
+
if (options?.signal?.aborted === true)
|
|
2936
|
+
throw options.signal.reason ?? error;
|
|
2937
|
+
throw normalizeFetchHttpError(error);
|
|
2938
|
+
}
|
|
2939
|
+
}
|
|
2915
2940
|
withThinking(effort) {
|
|
2916
2941
|
if (typeof effort !== "string") return this._clone();
|
|
2917
2942
|
let thinking;
|
|
@@ -16094,13 +16119,25 @@ function transformLoopControlData(data) {
|
|
|
16094
16119
|
return out;
|
|
16095
16120
|
}
|
|
16096
16121
|
async function writeConfigFile(filePath, config) {
|
|
16097
|
-
|
|
16098
|
-
|
|
16099
|
-
|
|
16100
|
-
|
|
16101
|
-
|
|
16102
|
-
|
|
16103
|
-
|
|
16122
|
+
return t3458WithConfigLock(filePath, async () => {
|
|
16123
|
+
let latestTable;
|
|
16124
|
+
try {
|
|
16125
|
+
latestTable = parse$7(await t3458Fs.readFile(filePath, "utf8")).request_body_byte_limits;
|
|
16126
|
+
}
|
|
16127
|
+
catch (error) {
|
|
16128
|
+
/* Unreadable or invalid previous TOML: preserve the Registry639 overwrite behavior. */
|
|
16129
|
+
}
|
|
16130
|
+
if (latestTable !== undefined)
|
|
16131
|
+
config = { ...config, raw: { ...config.raw, request_body_byte_limits: latestTable } };
|
|
16132
|
+
const validated = validateConfig(stripEnvModelConfig(config));
|
|
16133
|
+
const configDirectory = dirname$3(filePath);
|
|
16134
|
+
if (await mkdir(configDirectory, {
|
|
16135
|
+
recursive: true,
|
|
16136
|
+
mode: 448
|
|
16137
|
+
}) !== void 0)
|
|
16138
|
+
await hardenPrivatePath(configDirectory, "directory");
|
|
16139
|
+
await atomicWritePrivate$1(filePath, `${stringify$1(configToTomlData(validated))}\n`);
|
|
16140
|
+
});
|
|
16104
16141
|
}
|
|
16105
16142
|
function configToTomlData(config) {
|
|
16106
16143
|
const out = cloneRecord$2(config.raw);
|
|
@@ -229695,8 +229732,8 @@ var init_full = __esmMin((() => {
|
|
|
229695
229732
|
};
|
|
229696
229733
|
}
|
|
229697
229734
|
shouldRecoverFromContextOverflow(error) {
|
|
229698
|
-
|
|
229699
|
-
|
|
229735
|
+
return error instanceof APIContextOverflowError || error?.requestBodyPressure === true;
|
|
229736
|
+
}
|
|
229700
229737
|
observeContextOverflow(error, modelAlias = this.agent.activeResponderModel ?? this.agent.config.modelAlias) {
|
|
229701
229738
|
if (!(error instanceof APIContextOverflowError)) return;
|
|
229702
229739
|
const observed = error.maxContextTokens;
|
|
@@ -229712,63 +229749,70 @@ var init_full = __esmMin((() => {
|
|
|
229712
229749
|
if (this.lastCompactedState?.modelAlias === modelAlias) this.lastCompactedState = null;
|
|
229713
229750
|
}
|
|
229714
229751
|
begin(data) {
|
|
229715
|
-
|
|
229716
|
-
|
|
229717
|
-
|
|
229718
|
-
|
|
229719
|
-
|
|
229720
|
-
|
|
229721
|
-
|
|
229722
|
-
|
|
229723
|
-
|
|
229724
|
-
|
|
229725
|
-
|
|
229726
|
-
|
|
229727
|
-
|
|
229728
|
-
|
|
229729
|
-
|
|
229730
|
-
|
|
229731
|
-
|
|
229732
|
-
|
|
229733
|
-
|
|
229734
|
-
|
|
229735
|
-
|
|
229736
|
-
|
|
229737
|
-
|
|
229738
|
-
|
|
229739
|
-
|
|
229740
|
-
|
|
229741
|
-
|
|
229742
|
-
|
|
229743
|
-
|
|
229744
|
-
|
|
229745
|
-
|
|
229746
|
-
|
|
229747
|
-
|
|
229748
|
-
|
|
229749
|
-
|
|
229750
|
-
|
|
229751
|
-
|
|
229752
|
-
|
|
229753
|
-
|
|
229754
|
-
|
|
229755
|
-
|
|
229756
|
-
|
|
229757
|
-
|
|
229758
|
-
|
|
229759
|
-
|
|
229760
|
-
|
|
229761
|
-
|
|
229762
|
-
|
|
229763
|
-
|
|
229764
|
-
|
|
229765
|
-
|
|
229766
|
-
|
|
229767
|
-
|
|
229768
|
-
|
|
229769
|
-
|
|
229770
|
-
|
|
229771
|
-
|
|
229752
|
+
this.assertAdmissionOpen();
|
|
229753
|
+
if (this.compacting)
|
|
229754
|
+
return;
|
|
229755
|
+
if (this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES)
|
|
229756
|
+
throw this.sessionStoppedError(ErrorCodes.COMPACTION_FAILED);
|
|
229757
|
+
if (data.source === "manual")
|
|
229758
|
+
this.compactionCountInTurn = 0;
|
|
229759
|
+
else if (!data.requestBodyPressure)
|
|
229760
|
+
this.compactionCountInTurn += 1;
|
|
229761
|
+
if (this.compactionCountInTurn > this.strategy.maxCompactionPerTurn)
|
|
229762
|
+
return;
|
|
229763
|
+
if (this.agent.records.restoring) {
|
|
229764
|
+
this.agent.replayBuilder.push({
|
|
229765
|
+
type: "compaction",
|
|
229766
|
+
instruction: data.instruction
|
|
229767
|
+
});
|
|
229768
|
+
return;
|
|
229769
|
+
}
|
|
229770
|
+
if (this.agent.context.history.length === 0)
|
|
229771
|
+
throw new BlunError(ErrorCodes.COMPACTION_UNABLE, "No messages to compact in current history.");
|
|
229772
|
+
if (data.source === "manual" && this.agent.turn.hasActiveTurn)
|
|
229773
|
+
throw new BlunError(ErrorCodes.COMPACTION_UNABLE, "Cannot compact while a turn is active. Wait for it to finish, then retry.");
|
|
229774
|
+
this.agent.records.logRecord({
|
|
229775
|
+
type: "full_compaction.begin",
|
|
229776
|
+
...data
|
|
229777
|
+
});
|
|
229778
|
+
this.assertAdmissionOpen();
|
|
229779
|
+
const abortController = new AbortController();
|
|
229780
|
+
const executionId = ++this.nextExecutionId;
|
|
229781
|
+
let resolve;
|
|
229782
|
+
let reject;
|
|
229783
|
+
const promise = new Promise((yes, no) => {
|
|
229784
|
+
resolve = yes;
|
|
229785
|
+
reject = no;
|
|
229786
|
+
});
|
|
229787
|
+
this.executions.track({
|
|
229788
|
+
kind: "worker",
|
|
229789
|
+
id: executionId,
|
|
229790
|
+
source: data.source
|
|
229791
|
+
}, promise);
|
|
229792
|
+
this.executingControllers.add(abortController);
|
|
229793
|
+
this.compacting = {
|
|
229794
|
+
abortController,
|
|
229795
|
+
promise,
|
|
229796
|
+
blockedByTurn: false
|
|
229797
|
+
};
|
|
229798
|
+
const started = {
|
|
229799
|
+
type: "compaction.started",
|
|
229800
|
+
trigger: data.source,
|
|
229801
|
+
instruction: data.instruction
|
|
229802
|
+
};
|
|
229803
|
+
this.agent.emitEvent(this.agent.experimentalFlags.enabled("queue_policies") ? {
|
|
229804
|
+
...started,
|
|
229805
|
+
executionId,
|
|
229806
|
+
contextTurnId: this.agent.turn.currentId
|
|
229807
|
+
} : started);
|
|
229808
|
+
this.compactionWorker(abortController.signal, data, executionId).then(() => {
|
|
229809
|
+
this.executingControllers.delete(abortController);
|
|
229810
|
+
resolve();
|
|
229811
|
+
}, (error) => {
|
|
229812
|
+
this.executingControllers.delete(abortController);
|
|
229813
|
+
reject(error);
|
|
229814
|
+
});
|
|
229815
|
+
}
|
|
229772
229816
|
cancel() {
|
|
229773
229817
|
this.agent.replayBuilder.patchLast("compaction", { result: "cancelled" });
|
|
229774
229818
|
if (!this.compacting) return;
|
|
@@ -229801,12 +229845,28 @@ var init_full = __esmMin((() => {
|
|
|
229801
229845
|
this.sessionCompactionFailures = Math.max(this.sessionCompactionFailures, state.sessionCompactionFailures);
|
|
229802
229846
|
}
|
|
229803
229847
|
async handleOverflowError(signal, error) {
|
|
229804
|
-
|
|
229805
|
-
|
|
229806
|
-
|
|
229807
|
-
|
|
229808
|
-
|
|
229809
|
-
|
|
229848
|
+
if (error?.requestBodyPressure === true && error.byteLength < error.limitBytes) {
|
|
229849
|
+
signal.throwIfAborted();
|
|
229850
|
+
if (!this.hasShrinkableHistory(this.agent.context.history) || this.isSessionCompactionStopped)
|
|
229851
|
+
return;
|
|
229852
|
+
try {
|
|
229853
|
+
this.begin({ source: "auto", instruction: undefined, requestBodyPressure: true });
|
|
229854
|
+
await this.block(signal);
|
|
229855
|
+
}
|
|
229856
|
+
catch (failure) {
|
|
229857
|
+
signal.throwIfAborted();
|
|
229858
|
+
this.agent.log.warn("optional request-body compaction failed; valid request remains sendable", { error: failure });
|
|
229859
|
+
}
|
|
229860
|
+
return;
|
|
229861
|
+
}
|
|
229862
|
+
this.consecutiveOverflowCompactions += 1;
|
|
229863
|
+
const maxAttempts = this.strategy.maxOverflowCompactionAttempts;
|
|
229864
|
+
if (this.consecutiveOverflowCompactions > maxAttempts)
|
|
229865
|
+
throw new BlunError(ErrorCodes.CONTEXT_OVERFLOW, `Compaction failed to bring the context under the model window after ${String(maxAttempts)} attempts.`, { cause: error instanceof Error ? error : void 0 });
|
|
229866
|
+
if (!this.beginAutoCompaction() && !this.compacting)
|
|
229867
|
+
throw error;
|
|
229868
|
+
await this.block(signal);
|
|
229869
|
+
}
|
|
229810
229870
|
async beforeStep(signal, requestContext = {}) {
|
|
229811
229871
|
signal.throwIfAborted();
|
|
229812
229872
|
const historyBefore = this.agent.context.history;
|
|
@@ -229859,10 +229919,28 @@ var init_full = __esmMin((() => {
|
|
|
229859
229919
|
if (this.requiresShrinkableHistory && !this.hasShrinkableHistory(messages)) return false;
|
|
229860
229920
|
return this.beginAutoCompaction(throwOnLimit);
|
|
229861
229921
|
}
|
|
229862
|
-
|
|
229863
|
-
|
|
229864
|
-
|
|
229865
|
-
|
|
229922
|
+
shouldCompactRequestBody(endpoint, bytes, limit) {
|
|
229923
|
+
if (bytes >= limit)
|
|
229924
|
+
return true;
|
|
229925
|
+
const attempts = this.t3458PressureAttempts ??= new Map();
|
|
229926
|
+
const key = endpoint + "\0" + String(limit);
|
|
229927
|
+
if (bytes < limit * 0.75) {
|
|
229928
|
+
attempts.delete(key);
|
|
229929
|
+
return false;
|
|
229930
|
+
}
|
|
229931
|
+
if (this.isSessionCompactionStopped || !this.hasShrinkableHistory(this.agent.context.history))
|
|
229932
|
+
return false;
|
|
229933
|
+
const previous = attempts.get(key);
|
|
229934
|
+
if (previous !== undefined && bytes < previous + Math.ceil(previous / 10))
|
|
229935
|
+
return false;
|
|
229936
|
+
attempts.set(key, bytes);
|
|
229937
|
+
return true;
|
|
229938
|
+
}
|
|
229939
|
+
hasShrinkableHistory(messages) {
|
|
229940
|
+
if (messages.some((message) => message.role === "tool" || message.role === "assistant" && ((message.toolCalls?.length ?? 0) > 0 || message.content.some(part => part.type !== "text" || part.text.trim().length > 0))))
|
|
229941
|
+
return true;
|
|
229942
|
+
return estimateTokensForMessages(messages.filter(isRealUserInput)) > COMPACT_USER_MESSAGE_MAX_TOKENS;
|
|
229943
|
+
}
|
|
229866
229944
|
beginAutoCompaction(throwOnLimit = true) {
|
|
229867
229945
|
if (this.compacting) return true;
|
|
229868
229946
|
if (this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES) {
|
|
@@ -229906,52 +229984,57 @@ var init_full = __esmMin((() => {
|
|
|
229906
229984
|
}
|
|
229907
229985
|
}
|
|
229908
229986
|
async compactionWorker(signal, data, executionId) {
|
|
229909
|
-
|
|
229910
|
-
|
|
229911
|
-
|
|
229912
|
-
|
|
229913
|
-
|
|
229914
|
-
|
|
229915
|
-
|
|
229916
|
-
|
|
229917
|
-
|
|
229918
|
-
|
|
229919
|
-
|
|
229920
|
-
|
|
229921
|
-
|
|
229922
|
-
|
|
229923
|
-
|
|
229924
|
-
|
|
229925
|
-
|
|
229926
|
-
|
|
229927
|
-
|
|
229928
|
-
|
|
229929
|
-
|
|
229930
|
-
|
|
229931
|
-
|
|
229932
|
-
|
|
229933
|
-
|
|
229934
|
-
|
|
229935
|
-
|
|
229936
|
-
|
|
229937
|
-
|
|
229938
|
-
|
|
229939
|
-
|
|
229940
|
-
|
|
229941
|
-
|
|
229942
|
-
|
|
229943
|
-
|
|
229944
|
-
|
|
229945
|
-
|
|
229946
|
-
|
|
229947
|
-
|
|
229948
|
-
|
|
229949
|
-
|
|
229950
|
-
|
|
229951
|
-
|
|
229952
|
-
|
|
229953
|
-
|
|
229954
|
-
|
|
229987
|
+
try {
|
|
229988
|
+
const output = await this.compactionRound(signal, data, executionId);
|
|
229989
|
+
if (!output)
|
|
229990
|
+
return;
|
|
229991
|
+
const { result, stageCount } = output;
|
|
229992
|
+
try {
|
|
229993
|
+
await this.agent.refreshSystemPrompt();
|
|
229994
|
+
}
|
|
229995
|
+
catch (error) {
|
|
229996
|
+
this.agent.log.error("failed to refresh system prompt after compaction", { error });
|
|
229997
|
+
}
|
|
229998
|
+
await this.agent.injection.injectAfterCompaction();
|
|
229999
|
+
this.fitRetainedInput({}, result.keptHeadUserMessageCount !== void 0);
|
|
230000
|
+
this.lastCompactedState = {
|
|
230001
|
+
tokenCount: this.tokenCountWithPending,
|
|
230002
|
+
modelAlias: this.agent.activeResponderModel ?? this.agent.config.modelAlias,
|
|
230003
|
+
maxContextTokens: this.getEffectiveMaxContextTokens()
|
|
230004
|
+
};
|
|
230005
|
+
this.markCompleted();
|
|
230006
|
+
const { contextSummary: _contextSummary, ...eventResult } = result;
|
|
230007
|
+
const projectedContextTokens = this.estimateCurrentRequestTokens();
|
|
230008
|
+
this.agent.emitEvent({
|
|
230009
|
+
type: "compaction.completed",
|
|
230010
|
+
result: eventResult,
|
|
230011
|
+
projectedContextTokens,
|
|
230012
|
+
...stageCount > 1 ? { stageCount } : {}
|
|
230013
|
+
});
|
|
230014
|
+
this.triggerPostCompactHook(data, result);
|
|
230015
|
+
}
|
|
230016
|
+
catch (error) {
|
|
230017
|
+
if (isAbortError$4(error) || signal.aborted)
|
|
230018
|
+
return;
|
|
230019
|
+
if (!data.requestBodyPressure) {
|
|
230020
|
+
this.sessionCompactionFailures += 1;
|
|
230021
|
+
this.agent.records.logRecord({ type: "full_compaction.session_failures", count: this.sessionCompactionFailures });
|
|
230022
|
+
}
|
|
230023
|
+
const blockedByTurn = this.compacting?.blockedByTurn === true;
|
|
230024
|
+
this.cancel();
|
|
230025
|
+
this.agent.log.error("compaction failed", { error });
|
|
230026
|
+
const visibleError = this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES ? this.sessionStoppedError(ErrorCodes.COMPACTION_FAILED, error) : error;
|
|
230027
|
+
if (blockedByTurn)
|
|
230028
|
+
throw visibleError;
|
|
230029
|
+
this.agent.emitEvent({
|
|
230030
|
+
type: "error",
|
|
230031
|
+
...toBlunErrorPayload(visibleError)
|
|
230032
|
+
});
|
|
230033
|
+
}
|
|
230034
|
+
finally {
|
|
230035
|
+
this.agent.turn.onCompactionFinished();
|
|
230036
|
+
}
|
|
230037
|
+
}
|
|
229955
230038
|
fitRetainedInput(context = {}, freshBudgetedSelection = false) {
|
|
229956
230039
|
const budget = this.getCompactionBudgetTokens();
|
|
229957
230040
|
const window = this.getEffectiveMaxContextTokens();
|
|
@@ -229975,336 +230058,367 @@ var init_full = __esmMin((() => {
|
|
|
229975
230058
|
return `${summary.trim()}\n\n${todoMarkdown}`;
|
|
229976
230059
|
}
|
|
229977
230060
|
async compactionRound(signal, data, executionId) {
|
|
229978
|
-
|
|
229979
|
-
|
|
229980
|
-
|
|
229981
|
-
|
|
229982
|
-
|
|
229983
|
-
|
|
229984
|
-
|
|
229985
|
-
|
|
229986
|
-
|
|
229987
|
-
|
|
229988
|
-
|
|
229989
|
-
|
|
229990
|
-
|
|
229991
|
-
|
|
229992
|
-
|
|
229993
|
-
|
|
229994
|
-
|
|
229995
|
-
|
|
229996
|
-
|
|
229997
|
-
|
|
229998
|
-
|
|
229999
|
-
|
|
230000
|
-
|
|
230001
|
-
|
|
230002
|
-
|
|
230003
|
-
|
|
230004
|
-
|
|
230005
|
-
|
|
230006
|
-
|
|
230007
|
-
|
|
230008
|
-
|
|
230009
|
-
|
|
230010
|
-
|
|
230011
|
-
|
|
230012
|
-
|
|
230013
|
-
|
|
230014
|
-
|
|
230015
|
-
|
|
230016
|
-
|
|
230017
|
-
|
|
230018
|
-
|
|
230019
|
-
|
|
230020
|
-
|
|
230021
|
-
|
|
230022
|
-
|
|
230023
|
-
|
|
230024
|
-
|
|
230025
|
-
|
|
230026
|
-
|
|
230027
|
-
|
|
230028
|
-
|
|
230029
|
-
|
|
230030
|
-
|
|
230031
|
-
|
|
230032
|
-
|
|
230033
|
-
|
|
230034
|
-
|
|
230035
|
-
|
|
230036
|
-
|
|
230037
|
-
|
|
230038
|
-
|
|
230039
|
-
|
|
230040
|
-
|
|
230041
|
-
|
|
230042
|
-
|
|
230043
|
-
|
|
230044
|
-
|
|
230045
|
-
|
|
230046
|
-
|
|
230047
|
-
|
|
230048
|
-
|
|
230049
|
-
|
|
230050
|
-
|
|
230051
|
-
|
|
230052
|
-
|
|
230053
|
-
|
|
230054
|
-
|
|
230055
|
-
|
|
230056
|
-
|
|
230057
|
-
|
|
230058
|
-
|
|
230059
|
-
|
|
230060
|
-
|
|
230061
|
-
|
|
230062
|
-
|
|
230063
|
-
|
|
230064
|
-
|
|
230065
|
-
|
|
230066
|
-
|
|
230067
|
-
|
|
230068
|
-
|
|
230069
|
-
|
|
230070
|
-
|
|
230071
|
-
|
|
230072
|
-
|
|
230073
|
-
|
|
230074
|
-
|
|
230075
|
-
|
|
230076
|
-
|
|
230077
|
-
|
|
230078
|
-
|
|
230079
|
-
|
|
230080
|
-
|
|
230081
|
-
|
|
230082
|
-
|
|
230083
|
-
|
|
230084
|
-
|
|
230085
|
-
|
|
230086
|
-
|
|
230087
|
-
|
|
230088
|
-
|
|
230089
|
-
|
|
230090
|
-
|
|
230091
|
-
|
|
230092
|
-
|
|
230093
|
-
|
|
230094
|
-
|
|
230095
|
-
|
|
230096
|
-
|
|
230097
|
-
|
|
230098
|
-
|
|
230099
|
-
|
|
230100
|
-
|
|
230101
|
-
|
|
230102
|
-
|
|
230103
|
-
|
|
230104
|
-
|
|
230105
|
-
|
|
230106
|
-
|
|
230107
|
-
|
|
230108
|
-
|
|
230109
|
-
|
|
230110
|
-
|
|
230111
|
-
|
|
230112
|
-
|
|
230113
|
-
|
|
230114
|
-
|
|
230115
|
-
|
|
230116
|
-
|
|
230117
|
-
|
|
230118
|
-
|
|
230119
|
-
|
|
230120
|
-
|
|
230121
|
-
|
|
230122
|
-
|
|
230123
|
-
|
|
230124
|
-
|
|
230125
|
-
|
|
230126
|
-
|
|
230127
|
-
|
|
230128
|
-
|
|
230129
|
-
|
|
230130
|
-
|
|
230131
|
-
|
|
230132
|
-
|
|
230133
|
-
|
|
230134
|
-
|
|
230135
|
-
|
|
230136
|
-
|
|
230137
|
-
|
|
230138
|
-
|
|
230139
|
-
|
|
230140
|
-
|
|
230141
|
-
|
|
230142
|
-
|
|
230143
|
-
|
|
230144
|
-
|
|
230145
|
-
|
|
230146
|
-
|
|
230147
|
-
|
|
230148
|
-
|
|
230149
|
-
|
|
230150
|
-
|
|
230151
|
-
|
|
230152
|
-
|
|
230153
|
-
|
|
230154
|
-
|
|
230155
|
-
|
|
230156
|
-
|
|
230157
|
-
|
|
230158
|
-
|
|
230159
|
-
|
|
230160
|
-
|
|
230161
|
-
|
|
230162
|
-
|
|
230163
|
-
|
|
230164
|
-
|
|
230165
|
-
|
|
230166
|
-
|
|
230167
|
-
|
|
230168
|
-
|
|
230169
|
-
|
|
230170
|
-
|
|
230171
|
-
|
|
230172
|
-
|
|
230173
|
-
|
|
230174
|
-
|
|
230175
|
-
|
|
230176
|
-
|
|
230177
|
-
|
|
230178
|
-
|
|
230179
|
-
|
|
230180
|
-
|
|
230181
|
-
|
|
230182
|
-
|
|
230183
|
-
|
|
230184
|
-
|
|
230185
|
-
|
|
230186
|
-
|
|
230187
|
-
|
|
230188
|
-
|
|
230189
|
-
|
|
230190
|
-
|
|
230191
|
-
|
|
230192
|
-
|
|
230193
|
-
|
|
230194
|
-
|
|
230195
|
-
|
|
230196
|
-
|
|
230197
|
-
|
|
230198
|
-
|
|
230199
|
-
|
|
230200
|
-
|
|
230201
|
-
|
|
230202
|
-
|
|
230203
|
-
|
|
230204
|
-
|
|
230205
|
-
|
|
230206
|
-
|
|
230207
|
-
|
|
230208
|
-
|
|
230209
|
-
|
|
230210
|
-
|
|
230211
|
-
|
|
230212
|
-
|
|
230213
|
-
|
|
230214
|
-
|
|
230215
|
-
|
|
230216
|
-
|
|
230217
|
-
|
|
230218
|
-
|
|
230219
|
-
|
|
230220
|
-
|
|
230221
|
-
|
|
230222
|
-
|
|
230223
|
-
|
|
230224
|
-
|
|
230225
|
-
|
|
230226
|
-
|
|
230227
|
-
|
|
230228
|
-
|
|
230229
|
-
|
|
230230
|
-
|
|
230231
|
-
|
|
230232
|
-
|
|
230233
|
-
|
|
230234
|
-
|
|
230235
|
-
|
|
230236
|
-
|
|
230237
|
-
|
|
230238
|
-
|
|
230239
|
-
|
|
230240
|
-
|
|
230241
|
-
|
|
230242
|
-
|
|
230243
|
-
|
|
230244
|
-
|
|
230245
|
-
|
|
230246
|
-
|
|
230247
|
-
|
|
230248
|
-
|
|
230249
|
-
|
|
230250
|
-
|
|
230251
|
-
|
|
230252
|
-
|
|
230253
|
-
|
|
230254
|
-
|
|
230255
|
-
|
|
230256
|
-
|
|
230257
|
-
|
|
230258
|
-
|
|
230259
|
-
|
|
230260
|
-
|
|
230261
|
-
|
|
230262
|
-
|
|
230263
|
-
|
|
230264
|
-
|
|
230265
|
-
|
|
230266
|
-
|
|
230267
|
-
|
|
230268
|
-
|
|
230269
|
-
|
|
230270
|
-
|
|
230271
|
-
|
|
230272
|
-
|
|
230273
|
-
|
|
230274
|
-
|
|
230275
|
-
|
|
230276
|
-
|
|
230277
|
-
|
|
230278
|
-
|
|
230279
|
-
|
|
230280
|
-
|
|
230281
|
-
|
|
230282
|
-
|
|
230283
|
-
|
|
230284
|
-
|
|
230285
|
-
|
|
230286
|
-
|
|
230287
|
-
|
|
230288
|
-
|
|
230289
|
-
|
|
230290
|
-
|
|
230291
|
-
|
|
230292
|
-
|
|
230293
|
-
|
|
230294
|
-
|
|
230295
|
-
|
|
230296
|
-
|
|
230297
|
-
|
|
230298
|
-
|
|
230299
|
-
|
|
230300
|
-
|
|
230301
|
-
|
|
230302
|
-
|
|
230303
|
-
|
|
230304
|
-
|
|
230305
|
-
|
|
230306
|
-
|
|
230307
|
-
|
|
230061
|
+
const startedAt = Date.now();
|
|
230062
|
+
const originalHistory = [...this.agent.context.history];
|
|
230063
|
+
const tokensBefore = estimateTokensForMessages(originalHistory);
|
|
230064
|
+
let retryCount = 0;
|
|
230065
|
+
try {
|
|
230066
|
+
await this.triggerPreCompactHook(data, tokensBefore, signal);
|
|
230067
|
+
const model = resolveCompactionModelAlias(this.agent.blunConfig?.loopControl?.compactionModel, this.agent.activeResponderModel ?? this.agent.config.model);
|
|
230068
|
+
const runtimeModel = this.agent.config.runtimeModel(model);
|
|
230069
|
+
const capability = runtimeModel.modelCapabilities;
|
|
230070
|
+
const buildCompactionProvider = (usedContextTokens) => {
|
|
230071
|
+
const currentMaxContextTokens = this.getEffectiveMaxContextTokens(model);
|
|
230072
|
+
const defaultCompactionCap = currentMaxContextTokens > 0 ? Math.min(currentMaxContextTokens, DEFAULT_COMPACTION_MAX_COMPLETION_TOKENS) : void 0;
|
|
230073
|
+
const requestCapability = currentMaxContextTokens > 0 ? {
|
|
230074
|
+
...capability,
|
|
230075
|
+
max_context_tokens: currentMaxContextTokens
|
|
230076
|
+
} : capability;
|
|
230077
|
+
return applyCompletionBudget({
|
|
230078
|
+
provider: runtimeModel.provider,
|
|
230079
|
+
budget: resolveCompletionBudget({
|
|
230080
|
+
maxOutputSize: runtimeModel.maxOutputSize ?? defaultCompactionCap,
|
|
230081
|
+
reservedContextSize: this.agent.blunConfig?.loopControl?.reservedContextSize
|
|
230082
|
+
}),
|
|
230083
|
+
capability: requestCapability,
|
|
230084
|
+
usedContextTokens
|
|
230085
|
+
});
|
|
230086
|
+
};
|
|
230087
|
+
let provider = buildCompactionProvider();
|
|
230088
|
+
const instruction = this.buildInstruction(data.instruction);
|
|
230089
|
+
const delays = retryBackoffDelays(5);
|
|
230090
|
+
let usage = null;
|
|
230091
|
+
let summary;
|
|
230092
|
+
let historyForModel = originalHistory.filter((message) => {
|
|
230093
|
+
const origin = message.origin;
|
|
230094
|
+
return !(origin?.kind === "injection" && REBUILT_AFTER_COMPACTION_INJECTION_VARIANTS.has(origin.variant));
|
|
230095
|
+
});
|
|
230096
|
+
let droppedCount = 0;
|
|
230097
|
+
let overflowShrinkCount = 0;
|
|
230098
|
+
let incompleteSummaryAttempts = 0;
|
|
230099
|
+
let hierarchicalPassCount = 0;
|
|
230100
|
+
const typicalDurationMs = readTypicalCompactionDuration(provider.name, provider.modelName, this.agent.blunHomeDir)?.typicalDurationMs;
|
|
230101
|
+
let attemptCount = 0;
|
|
230102
|
+
let charsReceived = 0;
|
|
230103
|
+
let lastProgressEmitAt = 0;
|
|
230104
|
+
let initialCompactionRequestTokens;
|
|
230105
|
+
const compactionTools = [];
|
|
230106
|
+
const buildRequestMessages = (history, requestInstruction) => {
|
|
230107
|
+
return stripMediaForCompaction(downgradeUnsupportedMedia([...this.agent.context.project(history, {
|
|
230108
|
+
synthesizeMissing: true,
|
|
230109
|
+
dropOrphanResults: true
|
|
230110
|
+
}), createUserMessage(requestInstruction)], capability));
|
|
230111
|
+
};
|
|
230112
|
+
while (true) {
|
|
230113
|
+
signal.throwIfAborted();
|
|
230114
|
+
if (hierarchicalPassCount >= MAX_HIERARCHICAL_COMPACTION_PASSES)
|
|
230115
|
+
throw new BlunError(ErrorCodes.COMPACTION_FAILED, `Compaction stopped after ${String(MAX_HIERARCHICAL_COMPACTION_PASSES)} staged requests without reaching a final summary. The conversation history was not changed.`, { details: {
|
|
230116
|
+
contextUnchanged: true,
|
|
230117
|
+
hierarchicalPassCount
|
|
230118
|
+
} });
|
|
230119
|
+
const compactionRequestLimit = this.getEffectiveMaxContextTokens(model);
|
|
230120
|
+
const safeCompactionRequestLimit = compactionRequestLimit > 0 ? Math.max(1, Math.floor(compactionRequestLimit * (1 - COMPACTION_SUMMARY_RESERVE_RATIO))) : compactionRequestLimit;
|
|
230121
|
+
let messages = buildRequestMessages(historyForModel, instruction);
|
|
230122
|
+
let estimatedCompactionRequestTokens = this.estimateRequestTokens(messages, this.agent.effectiveSystemPrompt, compactionTools);
|
|
230123
|
+
initialCompactionRequestTokens ??= estimatedCompactionRequestTokens;
|
|
230124
|
+
let hierarchicalChunkEnd;
|
|
230125
|
+
if (safeCompactionRequestLimit > 0 && estimatedCompactionRequestTokens >= safeCompactionRequestLimit) {
|
|
230126
|
+
if (historyForModel.length > 1) {
|
|
230127
|
+
const chunkInstruction = `${instruction}\n\nThis request contains only the oldest chronological segment. Summarize every fact needed by a later merge pass. Do not assume later messages are visible.`;
|
|
230128
|
+
const chunk = selectHierarchicalCompactionChunk(historyForModel, safeCompactionRequestLimit, safeCompactionRequestLimit, (candidate) => {
|
|
230129
|
+
const candidateMessages = buildRequestMessages(candidate, chunkInstruction);
|
|
230130
|
+
return {
|
|
230131
|
+
messages: candidateMessages,
|
|
230132
|
+
estimatedTokens: this.estimateRequestTokens(candidateMessages, this.agent.effectiveSystemPrompt, compactionTools)
|
|
230133
|
+
};
|
|
230134
|
+
});
|
|
230135
|
+
if (chunk !== void 0) {
|
|
230136
|
+
hierarchicalChunkEnd = chunk.end;
|
|
230137
|
+
messages = chunk.messages;
|
|
230138
|
+
estimatedCompactionRequestTokens = chunk.estimatedTokens;
|
|
230139
|
+
}
|
|
230140
|
+
}
|
|
230141
|
+
if (hierarchicalChunkEnd === void 0)
|
|
230142
|
+
throw new BlunError(ErrorCodes.CONTEXT_OVERFLOW, `Compaction stopped before upload: the conversation (${String(tokensBefore)} tokens) could not be divided into a request below the active model window (${String(compactionRequestLimit)} tokens).`, { details: {
|
|
230143
|
+
contextBrakeBlocked: true,
|
|
230144
|
+
estimatedRequestTokens: estimatedCompactionRequestTokens,
|
|
230145
|
+
maxContextTokens: compactionRequestLimit,
|
|
230146
|
+
requestBudgetTokens: safeCompactionRequestLimit,
|
|
230147
|
+
summaryReserveTokens: compactionRequestLimit - safeCompactionRequestLimit,
|
|
230148
|
+
contextUnchanged: true
|
|
230149
|
+
} });
|
|
230150
|
+
}
|
|
230151
|
+
provider = buildCompactionProvider(estimatedCompactionRequestTokens);
|
|
230152
|
+
attemptCount += 1;
|
|
230153
|
+
charsReceived = 0;
|
|
230154
|
+
const stage = hierarchicalPassCount + 1;
|
|
230155
|
+
const estimatedStageCount = estimateCompactionStageCount(initialCompactionRequestTokens, safeCompactionRequestLimit, stage);
|
|
230156
|
+
const estimatedProgressPercent = estimateCompactionProgressPercent(stage, estimatedStageCount);
|
|
230157
|
+
const windowUsagePercent = estimateCompactionWindowUsagePercent(estimatedCompactionRequestTokens, compactionRequestLimit);
|
|
230158
|
+
this.agent.log.info("compaction stage request", {
|
|
230159
|
+
source: data.source,
|
|
230160
|
+
stage,
|
|
230161
|
+
estimatedStageCount,
|
|
230162
|
+
activeContextTokens: this.estimateProjectedRequestTokens(),
|
|
230163
|
+
estimatedInputTokens: estimatedCompactionRequestTokens,
|
|
230164
|
+
windowUsagePercent,
|
|
230165
|
+
safeInputLimitTokens: safeCompactionRequestLimit,
|
|
230166
|
+
maxContextTokens: compactionRequestLimit
|
|
230167
|
+
});
|
|
230168
|
+
const emitCompactionProgress = (force = false) => {
|
|
230169
|
+
const now = Date.now();
|
|
230170
|
+
if (!force && now - lastProgressEmitAt < 150)
|
|
230171
|
+
return;
|
|
230172
|
+
lastProgressEmitAt = now;
|
|
230173
|
+
this.agent.emitEvent({
|
|
230174
|
+
type: "compaction.progress",
|
|
230175
|
+
stage,
|
|
230176
|
+
estimatedStageCount,
|
|
230177
|
+
estimatedInputTokens: estimatedCompactionRequestTokens,
|
|
230178
|
+
estimatedProgressPercent,
|
|
230179
|
+
...windowUsagePercent === void 0 ? {} : { windowUsagePercent },
|
|
230180
|
+
charsReceived,
|
|
230181
|
+
attempt: attemptCount,
|
|
230182
|
+
...typicalDurationMs !== void 0 ? { typicalDurationMs } : {}
|
|
230183
|
+
});
|
|
230184
|
+
};
|
|
230185
|
+
emitCompactionProgress(true);
|
|
230186
|
+
const timingKey = compactionTimingKey(provider);
|
|
230187
|
+
const stallPolicy = this.measuredStallPolicy(timingKey);
|
|
230188
|
+
let lastProgressAt = Date.now();
|
|
230189
|
+
let maxObservedIdleMs = 0;
|
|
230190
|
+
const attemptController = new AbortController();
|
|
230191
|
+
const onParentAbort = () => {
|
|
230192
|
+
attemptController.abort();
|
|
230193
|
+
};
|
|
230194
|
+
signal.addEventListener("abort", onParentAbort, { once: true });
|
|
230195
|
+
let stalled = false;
|
|
230196
|
+
let stallTimer;
|
|
230197
|
+
const armStallTimer = () => {
|
|
230198
|
+
if (stallPolicy === void 0)
|
|
230199
|
+
return;
|
|
230200
|
+
if (stallTimer !== void 0)
|
|
230201
|
+
clearTimeout(stallTimer);
|
|
230202
|
+
stallTimer = setTimeout(() => {
|
|
230203
|
+
stalled = true;
|
|
230204
|
+
attemptController.abort();
|
|
230205
|
+
}, stallPolicy.timeoutMs);
|
|
230206
|
+
stallTimer.unref?.();
|
|
230207
|
+
};
|
|
230208
|
+
const observeProgress = (part) => {
|
|
230209
|
+
const now = Date.now();
|
|
230210
|
+
maxObservedIdleMs = Math.max(maxObservedIdleMs, now - lastProgressAt);
|
|
230211
|
+
lastProgressAt = now;
|
|
230212
|
+
if (part.type === "text")
|
|
230213
|
+
charsReceived += part.text.length;
|
|
230214
|
+
emitCompactionProgress();
|
|
230215
|
+
armStallTimer();
|
|
230216
|
+
};
|
|
230217
|
+
armStallTimer();
|
|
230218
|
+
try {
|
|
230219
|
+
const response = await this.agent.generate(provider, this.agent.config.systemPrompt, compactionTools, messages, { onMessagePart: observeProgress }, {
|
|
230220
|
+
signal: attemptController.signal,
|
|
230221
|
+
isCompactionRequest: true,
|
|
230222
|
+
onExecution: (completion) => {
|
|
230223
|
+
this.executions.track({
|
|
230224
|
+
kind: "provider",
|
|
230225
|
+
id: executionId,
|
|
230226
|
+
source: data.source
|
|
230227
|
+
}, completion);
|
|
230228
|
+
}
|
|
230229
|
+
});
|
|
230230
|
+
signal.throwIfAborted();
|
|
230231
|
+
if (stalled && !signal.aborted && stallPolicy !== void 0)
|
|
230232
|
+
throw new CompactionStallError(stallPolicy.timeoutMs, stallPolicy.measuredIdleMs);
|
|
230233
|
+
maxObservedIdleMs = Math.max(maxObservedIdleMs, Date.now() - lastProgressAt);
|
|
230234
|
+
if (response.finishReason === "truncated")
|
|
230235
|
+
throw new CompactionTruncatedError();
|
|
230236
|
+
if (response.usage !== null)
|
|
230237
|
+
usage = usage === null ? response.usage : addUsage$1(usage, response.usage);
|
|
230238
|
+
const extractedSummary = extractCompactionSummary(response);
|
|
230239
|
+
this.observeCompactionTiming(timingKey, maxObservedIdleMs);
|
|
230240
|
+
if (hierarchicalChunkEnd !== void 0) {
|
|
230241
|
+
hierarchicalPassCount += 1;
|
|
230242
|
+
const previousTokens = estimateTokensForMessages(historyForModel);
|
|
230243
|
+
const nextHistory = [createUserMessage(`${HIERARCHICAL_COMPACTION_PREFIX}\n${extractedSummary}`), ...historyForModel.slice(hierarchicalChunkEnd)];
|
|
230244
|
+
const nextTokens = estimateTokensForMessages(nextHistory);
|
|
230245
|
+
if (nextTokens >= previousTokens)
|
|
230246
|
+
throw new BlunError(ErrorCodes.COMPACTION_FAILED, "A staged compaction response did not reduce the pending history. The conversation history was not changed.", { details: {
|
|
230247
|
+
contextUnchanged: true,
|
|
230248
|
+
hierarchicalPassCount,
|
|
230249
|
+
previousTokens,
|
|
230250
|
+
nextTokens
|
|
230251
|
+
} });
|
|
230252
|
+
historyForModel = nextHistory;
|
|
230253
|
+
incompleteSummaryAttempts = 0;
|
|
230254
|
+
retryCount = 0;
|
|
230255
|
+
continue;
|
|
230256
|
+
}
|
|
230257
|
+
summary = extractedSummary;
|
|
230258
|
+
appendCompactionTiming({
|
|
230259
|
+
ts: Date.now(),
|
|
230260
|
+
provider: provider.name,
|
|
230261
|
+
model: provider.modelName,
|
|
230262
|
+
estimatedInputTokens: tokensBefore,
|
|
230263
|
+
durationMs: Date.now() - startedAt
|
|
230264
|
+
}, this.agent.blunHomeDir);
|
|
230265
|
+
break;
|
|
230266
|
+
}
|
|
230267
|
+
catch (error) {
|
|
230268
|
+
if (stalled && !signal.aborted && stallPolicy !== void 0)
|
|
230269
|
+
throw new CompactionStallError(stallPolicy.timeoutMs, stallPolicy.measuredIdleMs);
|
|
230270
|
+
if (isModelFallbackStatus(error))
|
|
230271
|
+
throw error;
|
|
230272
|
+
const isContextOverflow = this.shouldRecoverFromContextOverflow(error);
|
|
230273
|
+
if (isContextOverflow)
|
|
230274
|
+
this.observeContextOverflow(error, model);
|
|
230275
|
+
if (isContextOverflow && historyForModel.length > 1) {
|
|
230276
|
+
if (data.source === "auto") {
|
|
230277
|
+
const learnedMaxContextTokens = this.getEffectiveMaxContextTokens(model);
|
|
230278
|
+
if (learnedMaxContextTokens > 0 && learnedMaxContextTokens < compactionRequestLimit) {
|
|
230279
|
+
retryCount = 0;
|
|
230280
|
+
continue;
|
|
230281
|
+
}
|
|
230282
|
+
throw new BlunError(ErrorCodes.CONTEXT_OVERFLOW, "The active model rejected the full compaction request; refusing to drop unsummarized history.", {
|
|
230283
|
+
cause: error,
|
|
230284
|
+
details: {
|
|
230285
|
+
contextBrakeBlocked: true,
|
|
230286
|
+
estimatedRequestTokens: estimatedCompactionRequestTokens,
|
|
230287
|
+
maxContextTokens: learnedMaxContextTokens,
|
|
230288
|
+
requestBudgetTokens: learnedMaxContextTokens,
|
|
230289
|
+
contextUnchanged: true
|
|
230290
|
+
}
|
|
230291
|
+
});
|
|
230292
|
+
}
|
|
230293
|
+
overflowShrinkCount += 1;
|
|
230294
|
+
if (overflowShrinkCount > MAX_COMPACTION_OVERFLOW_SHRINK_ATTEMPTS)
|
|
230295
|
+
throw error;
|
|
230296
|
+
const before = historyForModel.length;
|
|
230297
|
+
historyForModel = shrinkCompactionHistoryAfterOverflow(historyForModel, overflowShrinkCount);
|
|
230298
|
+
droppedCount += before - historyForModel.length;
|
|
230299
|
+
retryCount = 0;
|
|
230300
|
+
continue;
|
|
230301
|
+
}
|
|
230302
|
+
if (error instanceof CompactionTruncatedError || error instanceof APIEmptyResponseError) {
|
|
230303
|
+
incompleteSummaryAttempts += 1;
|
|
230304
|
+
if (incompleteSummaryAttempts === 1)
|
|
230305
|
+
continue;
|
|
230306
|
+
throw new BlunError(ErrorCodes.COMPACTION_FAILED, "The provider returned an empty or truncated compaction response twice. The conversation history was not changed.", {
|
|
230307
|
+
cause: error,
|
|
230308
|
+
details: {
|
|
230309
|
+
contextUnchanged: true,
|
|
230310
|
+
lossyRetryPrevented: true,
|
|
230311
|
+
compactionFailure: error.name,
|
|
230312
|
+
incompleteSummaryAttempts
|
|
230313
|
+
}
|
|
230314
|
+
});
|
|
230315
|
+
}
|
|
230316
|
+
if (!isRetryableGenerateError(error))
|
|
230317
|
+
throw error;
|
|
230318
|
+
if (retryCount + 1 >= 5)
|
|
230319
|
+
throw error;
|
|
230320
|
+
await sleepForRetry(delays[retryCount], signal);
|
|
230321
|
+
retryCount += 1;
|
|
230322
|
+
}
|
|
230323
|
+
finally {
|
|
230324
|
+
if (stallTimer !== void 0)
|
|
230325
|
+
clearTimeout(stallTimer);
|
|
230326
|
+
signal.removeEventListener("abort", onParentAbort);
|
|
230327
|
+
}
|
|
230328
|
+
}
|
|
230329
|
+
if (usage !== null)
|
|
230330
|
+
this.agent.usage.record(model, usage);
|
|
230331
|
+
const newHistory = this.agent.context.history;
|
|
230332
|
+
for (let i = 0; i < originalHistory.length; i++)
|
|
230333
|
+
if (newHistory[i] !== originalHistory[i]) {
|
|
230334
|
+
this.cancel();
|
|
230335
|
+
return;
|
|
230336
|
+
}
|
|
230337
|
+
if (newHistory.slice(originalHistory.length).some((message) => !isRealUserInput(message))) {
|
|
230338
|
+
this.cancel();
|
|
230339
|
+
return;
|
|
230340
|
+
}
|
|
230341
|
+
const rawSummary = this.postProcessSummary(summary ?? "");
|
|
230342
|
+
const contextSummary = buildCompactionSummaryText(rawSummary);
|
|
230343
|
+
const result = this.agent.context.applyCompaction({
|
|
230344
|
+
summary: rawSummary,
|
|
230345
|
+
contextSummary,
|
|
230346
|
+
compactedCount: originalHistory.length,
|
|
230347
|
+
tokensBefore,
|
|
230348
|
+
droppedCount: droppedCount === 0 ? void 0 : droppedCount
|
|
230349
|
+
});
|
|
230350
|
+
this.agent.telemetry.track("compaction_finished", {
|
|
230351
|
+
source: data.source,
|
|
230352
|
+
tokens_before: result.tokensBefore,
|
|
230353
|
+
tokens_after: result.tokensAfter,
|
|
230354
|
+
duration_ms: Date.now() - startedAt,
|
|
230355
|
+
compacted_count: result.compactedCount,
|
|
230356
|
+
dropped_count: result.droppedCount,
|
|
230357
|
+
retry_count: retryCount,
|
|
230358
|
+
round: 1,
|
|
230359
|
+
thinking_effort: this.agent.config.thinkingEffort,
|
|
230360
|
+
...usage === null ? {} : {
|
|
230361
|
+
input_tokens: inputTotal(usage),
|
|
230362
|
+
output_tokens: usage.output
|
|
230363
|
+
}
|
|
230364
|
+
});
|
|
230365
|
+
return {
|
|
230366
|
+
result,
|
|
230367
|
+
stageCount: hierarchicalPassCount + 1
|
|
230368
|
+
};
|
|
230369
|
+
}
|
|
230370
|
+
catch (error) {
|
|
230371
|
+
if (isAbortError$4(error) || signal.aborted)
|
|
230372
|
+
return void 0;
|
|
230373
|
+
this.agent.telemetry.track("compaction_failed", {
|
|
230374
|
+
source: data.source,
|
|
230375
|
+
tokens_before: tokensBefore,
|
|
230376
|
+
duration_ms: Date.now() - startedAt,
|
|
230377
|
+
round: 1,
|
|
230378
|
+
retry_count: retryCount,
|
|
230379
|
+
thinking_effort: this.agent.config.thinkingEffort,
|
|
230380
|
+
error_type: error instanceof Error ? error.name : "Unknown"
|
|
230381
|
+
});
|
|
230382
|
+
if (isBlunError(error) && (error.code === ErrorCodes.AUTH_LOGIN_REQUIRED || error.code === ErrorCodes.COMPACTION_FAILED))
|
|
230383
|
+
throw error;
|
|
230384
|
+
if (isBlunError(error) && error.code === ErrorCodes.CONTEXT_OVERFLOW && error.details?.["contextBrakeBlocked"] === true)
|
|
230385
|
+
throw error;
|
|
230386
|
+
if (isModelFallbackStatus(error))
|
|
230387
|
+
throw error;
|
|
230388
|
+
if (error instanceof CompactionStallError)
|
|
230389
|
+
throw new BlunError(ErrorCodes.COMPACTION_STALLED, `Compaction was aborted because no provider data arrived for ${String(error.timeoutMs)}ms. The conversation history was not changed.`, {
|
|
230390
|
+
cause: error,
|
|
230391
|
+
details: {
|
|
230392
|
+
reason: "no_provider_data",
|
|
230393
|
+
timeoutSource: "measured_provider_behavior",
|
|
230394
|
+
measuredIdleMs: error.measuredIdleMs,
|
|
230395
|
+
timeoutMs: error.timeoutMs,
|
|
230396
|
+
contextUnchanged: true
|
|
230397
|
+
}
|
|
230398
|
+
});
|
|
230399
|
+
if (error instanceof APIStatusError && error.statusCode === 402)
|
|
230400
|
+
throw new BlunError(ErrorCodes.COMPACTION_FAILED, "The selected provider rejected compaction because its usage quota or payment is unavailable. Switch to an available model and run /compact again. The conversation history was not changed.", {
|
|
230401
|
+
cause: error,
|
|
230402
|
+
details: {
|
|
230403
|
+
statusCode: error.statusCode,
|
|
230404
|
+
requestId: error.requestId,
|
|
230405
|
+
action: "switch_model_and_retry",
|
|
230406
|
+
contextUnchanged: true
|
|
230407
|
+
}
|
|
230408
|
+
});
|
|
230409
|
+
if (error instanceof APIStatusError && error.statusCode === 408)
|
|
230410
|
+
throw new BlunError(ErrorCodes.COMPACTION_FAILED, `Compaction stopped: the conversation (${String(tokensBefore)} tokens) could not be sent completely. The conversation history was not changed.`, {
|
|
230411
|
+
cause: error,
|
|
230412
|
+
details: {
|
|
230413
|
+
statusCode: error.statusCode,
|
|
230414
|
+
requestId: error.requestId,
|
|
230415
|
+
estimatedHistoryTokens: tokensBefore,
|
|
230416
|
+
contextUnchanged: true
|
|
230417
|
+
}
|
|
230418
|
+
});
|
|
230419
|
+
throw new BlunError(ErrorCodes.COMPACTION_FAILED, String(error), { cause: error });
|
|
230420
|
+
}
|
|
230421
|
+
}
|
|
230308
230422
|
measuredStallPolicy(_timingKey) {}
|
|
230309
230423
|
observeCompactionTiming(timingKey, maxIdleMs) {
|
|
230310
230424
|
if (!Number.isFinite(maxIdleMs) || maxIdleMs <= 0) return;
|
|
@@ -256541,6 +256655,43 @@ function convertMCPContentBlock(block) {
|
|
|
256541
256655
|
* `mcp__github__create_pr`) — embedded into the `<mcp_tool_result name="…">`
|
|
256542
256656
|
* wrap when the result is media-only, so the model can attribute binary parts.
|
|
256543
256657
|
*/
|
|
256658
|
+
async function retainOriginalText(parts, toolName, homedir) {
|
|
256659
|
+
const textParts = parts.flatMap((part) => {
|
|
256660
|
+
if (part.type !== "text") return [];
|
|
256661
|
+
const chunks = [];
|
|
256662
|
+
for (let offset = 0; offset < part.text.length; offset += 256) chunks.push(part.text.slice(offset, offset + 256));
|
|
256663
|
+
return [chunks];
|
|
256664
|
+
});
|
|
256665
|
+
const archive = JSON.stringify({
|
|
256666
|
+
version: 1,
|
|
256667
|
+
textParts
|
|
256668
|
+
}, null, 2);
|
|
256669
|
+
const outputPath = homedir === void 0 ? void 0 : await saveToolResult({
|
|
256670
|
+
homedir,
|
|
256671
|
+
toolName,
|
|
256672
|
+
toolCallId: "mcp-original",
|
|
256673
|
+
directoryPolicy: "private-retention"
|
|
256674
|
+
}, archive);
|
|
256675
|
+
if (outputPath === void 0) return "\n\n[Originaltext nicht gespeichert: Sitzungsverzeichnis fehlt oder Schreiben fehlgeschlagen. Die Ausgabe bleibt begrenzt.]";
|
|
256676
|
+
return [
|
|
256677
|
+
"",
|
|
256678
|
+
"",
|
|
256679
|
+
"MCP-Originaltext vor Kürzung gespeichert (JSON, version: 1, textParts: Zeichenabschnitte je Textblock; ohne Binärdaten).",
|
|
256680
|
+
`output_path: ${outputPath}`,
|
|
256681
|
+
"Mit Read seitenweise nachlesen. Zeichenabschnitte je Block ohne Trennzeichen verbinden; Blockreihenfolge erhalten."
|
|
256682
|
+
].join("\n");
|
|
256683
|
+
}
|
|
256684
|
+
function appendTextNotice(parts, text) {
|
|
256685
|
+
const last = parts.at(-1);
|
|
256686
|
+
if (last?.type === "text") parts[parts.length - 1] = {
|
|
256687
|
+
...last,
|
|
256688
|
+
text: last.text + text
|
|
256689
|
+
};
|
|
256690
|
+
else parts.push({
|
|
256691
|
+
type: "text",
|
|
256692
|
+
text
|
|
256693
|
+
});
|
|
256694
|
+
}
|
|
256544
256695
|
async function mcpResultToExecutableOutput(result, qualifiedToolName, options = {}) {
|
|
256545
256696
|
const converted = [];
|
|
256546
256697
|
for (const block of result.content) {
|
|
@@ -256548,8 +256699,10 @@ async function mcpResultToExecutableOutput(result, qualifiedToolName, options =
|
|
|
256548
256699
|
if (part !== null) converted.push(part);
|
|
256549
256700
|
}
|
|
256550
256701
|
const budgeted = applyTextBudget(wrapMediaOnly(converted, qualifiedToolName));
|
|
256702
|
+
const retentionNotice = budgeted.truncated ? await retainOriginalText(converted, qualifiedToolName, options.homedir) : void 0;
|
|
256551
256703
|
const capped = applyBinaryPartCap(await compressImageContentParts(budgeted.parts, { annotate: { persistOriginal: (bytes, mimeType) => persistOriginalImage(bytes, mimeType, options.originalsDir === void 0 ? {} : { dir: options.originalsDir }) } }));
|
|
256552
256704
|
const truncated = budgeted.truncated || capped.truncated;
|
|
256705
|
+
if (retentionNotice !== void 0) appendTextNotice(capped.parts, retentionNotice);
|
|
256553
256706
|
const output = collapseSingleText(capped.parts);
|
|
256554
256707
|
return truncated ? {
|
|
256555
256708
|
output,
|
|
@@ -256695,6 +256848,7 @@ function collapseSingleText(parts) {
|
|
|
256695
256848
|
}
|
|
256696
256849
|
var MCP_MAX_OUTPUT_CHARS, MCP_OUTPUT_TRUNCATED_TEXT, MCP_MAX_BINARY_PART_BYTES, MCP_MAX_BINARY_PART_CHARS;
|
|
256697
256850
|
var init_output = __esmMin((() => {
|
|
256851
|
+
init_tool_result_budget();
|
|
256698
256852
|
init_image_compress();
|
|
256699
256853
|
init_image_originals();
|
|
256700
256854
|
MCP_MAX_OUTPUT_CHARS = 1e5;
|
|
@@ -264951,7 +265105,8 @@ async function budgetToolResultForModel(options) {
|
|
|
264951
265105
|
const outputPath = await saveToolResult({
|
|
264952
265106
|
homedir: options.homedir,
|
|
264953
265107
|
toolName: options.toolName,
|
|
264954
|
-
toolCallId: options.toolCallId
|
|
265108
|
+
toolCallId: options.toolCallId,
|
|
265109
|
+
directoryPolicy: "compatible-spill"
|
|
264955
265110
|
}, text);
|
|
264956
265111
|
if (outputPath === void 0) return options.result;
|
|
264957
265112
|
const output = renderPersistedToolResult(options.toolName, options.toolCallId, text, outputPath);
|
|
@@ -264970,19 +265125,33 @@ function persistableToolResultText(output) {
|
|
|
264970
265125
|
return output.map((part) => part.text).join("");
|
|
264971
265126
|
}
|
|
264972
265127
|
async function saveToolResult(options, text) {
|
|
265128
|
+
if (!isAbsolute$2(options.homedir)) return void 0;
|
|
265129
|
+
let createdPath;
|
|
264973
265130
|
try {
|
|
264974
265131
|
const dir = join$4(options.homedir, "tool-results");
|
|
264975
265132
|
await mkdir(dir, {
|
|
264976
265133
|
recursive: true,
|
|
264977
265134
|
mode: 448
|
|
264978
265135
|
});
|
|
265136
|
+
const info = await lstat(dir);
|
|
265137
|
+
if (info.isSymbolicLink()) return void 0;
|
|
265138
|
+
if (process.platform !== "win32") {
|
|
265139
|
+
if (info.uid !== process.getuid?.()) return void 0;
|
|
265140
|
+
if (options.directoryPolicy === "private-retention" && (info.mode & 63) !== 0) return;
|
|
265141
|
+
}
|
|
265142
|
+
const home = await realpath(options.homedir);
|
|
265143
|
+
if (normalize$4(await realpath(dir)) !== join$4(home, "tool-results")) return void 0;
|
|
264979
265144
|
const outputPath = join$4(dir, `${safeToolResultFileStem(options.toolName, options.toolCallId)}-${randomUUID()}.txt`);
|
|
264980
|
-
await
|
|
264981
|
-
|
|
264982
|
-
|
|
264983
|
-
|
|
265145
|
+
const file = await open(outputPath, "wx", 384);
|
|
265146
|
+
createdPath = outputPath;
|
|
265147
|
+
try {
|
|
265148
|
+
await file.writeFile(text, { encoding: "utf8" });
|
|
265149
|
+
} finally {
|
|
265150
|
+
await file.close();
|
|
265151
|
+
}
|
|
264984
265152
|
return outputPath;
|
|
264985
265153
|
} catch {
|
|
265154
|
+
if (createdPath !== void 0) await unlink(createdPath).catch(() => void 0);
|
|
264986
265155
|
return;
|
|
264987
265156
|
}
|
|
264988
265157
|
}
|
|
@@ -269228,63 +269397,66 @@ var init_tool$1 = __esmMin((() => {
|
|
|
269228
269397
|
}
|
|
269229
269398
|
}
|
|
269230
269399
|
registerMcpServer(serverName, client, tools, enabledTools) {
|
|
269231
|
-
|
|
269232
|
-
|
|
269233
|
-
|
|
269234
|
-
|
|
269235
|
-
|
|
269236
|
-
|
|
269237
|
-
|
|
269238
|
-
|
|
269239
|
-
|
|
269240
|
-
|
|
269241
|
-
|
|
269242
|
-
|
|
269243
|
-
|
|
269244
|
-
|
|
269245
|
-
|
|
269246
|
-
|
|
269247
|
-
|
|
269248
|
-
|
|
269249
|
-
|
|
269250
|
-
|
|
269251
|
-
|
|
269252
|
-
|
|
269253
|
-
|
|
269254
|
-
|
|
269255
|
-
|
|
269256
|
-
|
|
269257
|
-
|
|
269258
|
-
}
|
|
269259
|
-
});
|
|
269260
|
-
continue;
|
|
269261
|
-
}
|
|
269262
|
-
seenInThisCall.set(qualified, tool.name);
|
|
269263
|
-
const wrapped = {
|
|
269264
|
-
name: qualified,
|
|
269265
|
-
description: tool.description,
|
|
269266
|
-
parameters: tool.parameters,
|
|
269267
|
-
resolveExecution: (args) => {
|
|
269268
|
-
return {
|
|
269269
|
-
approvalRule: qualified,
|
|
269270
|
-
execute: async (context) => {
|
|
269271
|
-
return mcpResultToExecutableOutput(await client.callTool(tool.name, args ?? {}, context.signal), qualified, { originalsDir: this.agent.mediaOriginalsDir });
|
|
269272
|
-
}
|
|
269273
|
-
};
|
|
269400
|
+
this.unregisterMcpServer(serverName);
|
|
269401
|
+
const qualifiedNames = [];
|
|
269402
|
+
const collisions = [];
|
|
269403
|
+
const seenInThisCall = /* @__PURE__ */ new Map();
|
|
269404
|
+
for (const tool of tools) {
|
|
269405
|
+
if (enabledTools !== void 0 && !enabledTools.has(tool.name)) continue;
|
|
269406
|
+
const qualified = qualifyMcpToolName(serverName, tool.name);
|
|
269407
|
+
const firstInThisCall = seenInThisCall.get(qualified);
|
|
269408
|
+
if (firstInThisCall !== void 0) {
|
|
269409
|
+
collisions.push({
|
|
269410
|
+
qualified,
|
|
269411
|
+
toolName: tool.name,
|
|
269412
|
+
collidesWith: {
|
|
269413
|
+
kind: "same_server",
|
|
269414
|
+
toolName: firstInThisCall
|
|
269415
|
+
}
|
|
269416
|
+
});
|
|
269417
|
+
continue;
|
|
269418
|
+
}
|
|
269419
|
+
const existingEntry = this.mcpTools.get(qualified);
|
|
269420
|
+
if (existingEntry !== void 0) {
|
|
269421
|
+
collisions.push({
|
|
269422
|
+
qualified,
|
|
269423
|
+
toolName: tool.name,
|
|
269424
|
+
collidesWith: {
|
|
269425
|
+
kind: "other_server",
|
|
269426
|
+
serverName: existingEntry.serverName
|
|
269274
269427
|
}
|
|
269275
|
-
};
|
|
269276
|
-
this.mcpTools.set(qualified, {
|
|
269277
|
-
tool: wrapped,
|
|
269278
|
-
serverName
|
|
269279
269428
|
});
|
|
269280
|
-
|
|
269429
|
+
continue;
|
|
269281
269430
|
}
|
|
269282
|
-
|
|
269283
|
-
|
|
269284
|
-
|
|
269285
|
-
|
|
269431
|
+
seenInThisCall.set(qualified, tool.name);
|
|
269432
|
+
const wrapped = {
|
|
269433
|
+
name: qualified,
|
|
269434
|
+
description: tool.description,
|
|
269435
|
+
parameters: tool.parameters,
|
|
269436
|
+
resolveExecution: (args) => {
|
|
269437
|
+
return {
|
|
269438
|
+
approvalRule: qualified,
|
|
269439
|
+
execute: async (context) => {
|
|
269440
|
+
return mcpResultToExecutableOutput(await client.callTool(tool.name, args ?? {}, context.signal), qualified, {
|
|
269441
|
+
originalsDir: this.agent.mediaOriginalsDir,
|
|
269442
|
+
homedir: this.agent.homedir
|
|
269443
|
+
});
|
|
269444
|
+
}
|
|
269445
|
+
};
|
|
269446
|
+
}
|
|
269286
269447
|
};
|
|
269448
|
+
this.mcpTools.set(qualified, {
|
|
269449
|
+
tool: wrapped,
|
|
269450
|
+
serverName
|
|
269451
|
+
});
|
|
269452
|
+
qualifiedNames.push(qualified);
|
|
269287
269453
|
}
|
|
269454
|
+
this.mcpToolsByServer.set(serverName, qualifiedNames);
|
|
269455
|
+
return {
|
|
269456
|
+
registered: qualifiedNames,
|
|
269457
|
+
collisions
|
|
269458
|
+
};
|
|
269459
|
+
}
|
|
269288
269460
|
unregisterMcpServer(serverName) {
|
|
269289
269461
|
const existing = this.mcpToolsByServer.get(serverName);
|
|
269290
269462
|
if (existing === void 0) return false;
|
|
@@ -270360,54 +270532,67 @@ var init_agent = __esmMin((() => {
|
|
|
270360
270532
|
return this.generateForModel(this.activeResponderModel ?? this.config.modelAlias);
|
|
270361
270533
|
}
|
|
270362
270534
|
generateForModel(modelAlias) {
|
|
270363
|
-
|
|
270364
|
-
|
|
270365
|
-
|
|
270366
|
-
|
|
270367
|
-
|
|
270368
|
-
|
|
270369
|
-
|
|
270370
|
-
|
|
270371
|
-
|
|
270372
|
-
|
|
270373
|
-
|
|
270374
|
-
|
|
270375
|
-
|
|
270376
|
-
|
|
270377
|
-
|
|
270378
|
-
|
|
270379
|
-
|
|
270380
|
-
|
|
270381
|
-
|
|
270382
|
-
|
|
270383
|
-
|
|
270384
|
-
|
|
270385
|
-
|
|
270386
|
-
|
|
270387
|
-
|
|
270388
|
-
|
|
270389
|
-
|
|
270390
|
-
|
|
270391
|
-
|
|
270392
|
-
|
|
270393
|
-
|
|
270394
|
-
|
|
270395
|
-
|
|
270396
|
-
|
|
270397
|
-
|
|
270398
|
-
|
|
270399
|
-
|
|
270400
|
-
|
|
270401
|
-
|
|
270402
|
-
|
|
270403
|
-
|
|
270404
|
-
|
|
270405
|
-
|
|
270406
|
-
|
|
270407
|
-
|
|
270408
|
-
|
|
270409
|
-
|
|
270410
|
-
|
|
270535
|
+
return async (provider, systemPrompt, tools, history, callbacks, options) => {
|
|
270536
|
+
const { requestLogFields, generateOptions } = splitGenerateOptions(options);
|
|
270537
|
+
const retryScope = providerRetryScope(modelAlias, provider);
|
|
270538
|
+
await this.providerRetryWait.wait(retryScope, generateOptions?.signal);
|
|
270539
|
+
const callerTransportAttempt = generateOptions?.onTransportAttempt;
|
|
270540
|
+
const ownedGenerateOptions = {
|
|
270541
|
+
...generateOptions,
|
|
270542
|
+
transportMaxRetries: 0,
|
|
270543
|
+
requestBodyBudget: {
|
|
270544
|
+
...generateOptions?.requestBodyBudget,
|
|
270545
|
+
onMeasured: (bytes, limit, endpoint) => {
|
|
270546
|
+
generateOptions?.requestBodyBudget?.onMeasured?.(bytes, limit, endpoint);
|
|
270547
|
+
if (!generateOptions?.isCompactionRequest && limit !== undefined && this.fullCompaction.shouldCompactRequestBody(endpoint, bytes, limit))
|
|
270548
|
+
throw t3458Pressure(bytes, limit, endpoint);
|
|
270549
|
+
}
|
|
270550
|
+
},
|
|
270551
|
+
onTransportAttempt: (stats) => {
|
|
270552
|
+
callerTransportAttempt?.(stats);
|
|
270553
|
+
this.llmRequestLogger.logTransportAttempt(stats, requestLogFields);
|
|
270554
|
+
}
|
|
270555
|
+
};
|
|
270556
|
+
const run = async (requestOptions) => {
|
|
270557
|
+
let requestStarted = false;
|
|
270558
|
+
this.llmRequestLogger.logRequest({
|
|
270559
|
+
provider,
|
|
270560
|
+
modelAlias,
|
|
270561
|
+
systemPrompt,
|
|
270562
|
+
tools,
|
|
270563
|
+
messages: history,
|
|
270564
|
+
fields: requestLogFields
|
|
270565
|
+
});
|
|
270566
|
+
try {
|
|
270567
|
+
return await this.rawGenerate(provider, systemPrompt, tools, history, callbacks, {
|
|
270568
|
+
...requestOptions,
|
|
270569
|
+
onRequestStart: () => {
|
|
270570
|
+
requestOptions?.onRequestStart?.();
|
|
270571
|
+
requestStarted = true;
|
|
270572
|
+
}
|
|
270573
|
+
});
|
|
270574
|
+
}
|
|
270575
|
+
catch (error) {
|
|
270576
|
+
if (error instanceof APIProviderRateLimitError && error.retryAfterMs !== null)
|
|
270577
|
+
await this.providerRetryWait.defer(retryScope, error.retryAfterMs);
|
|
270578
|
+
if (requestStarted && requestOptions?.signal?.aborted !== true)
|
|
270579
|
+
this.turn.noteProviderRequestFailure(requestLogFields?.turnStep, error);
|
|
270580
|
+
throw error;
|
|
270581
|
+
}
|
|
270582
|
+
};
|
|
270583
|
+
if (ownedGenerateOptions.auth !== void 0)
|
|
270584
|
+
return run(ownedGenerateOptions);
|
|
270585
|
+
const withAuth = modelAlias === void 0 ? void 0 : this.modelProvider?.resolveAuth?.(modelAlias, { log: this.log });
|
|
270586
|
+
if (withAuth === void 0)
|
|
270587
|
+
return run(ownedGenerateOptions);
|
|
270588
|
+
return withAuth((auth) => {
|
|
270589
|
+
return run({
|
|
270590
|
+
...ownedGenerateOptions,
|
|
270591
|
+
auth
|
|
270592
|
+
});
|
|
270593
|
+
});
|
|
270594
|
+
};
|
|
270595
|
+
}
|
|
270411
270596
|
get llm() {
|
|
270412
270597
|
return this.llmForTurn();
|
|
270413
270598
|
}
|
|
@@ -301723,30 +301908,36 @@ var init_provider_manager = __esmMin((() => {
|
|
|
301723
301908
|
return typeof config === "function" ? config() : config;
|
|
301724
301909
|
}
|
|
301725
301910
|
resolveProviderConfig(model) {
|
|
301726
|
-
|
|
301727
|
-
|
|
301728
|
-
|
|
301729
|
-
|
|
301730
|
-
|
|
301731
|
-
|
|
301732
|
-
|
|
301733
|
-
|
|
301734
|
-
|
|
301735
|
-
|
|
301736
|
-
|
|
301737
|
-
|
|
301738
|
-
|
|
301739
|
-
|
|
301740
|
-
|
|
301741
|
-
|
|
301742
|
-
|
|
301743
|
-
|
|
301744
|
-
|
|
301745
|
-
|
|
301746
|
-
|
|
301747
|
-
|
|
301748
|
-
|
|
301749
|
-
|
|
301911
|
+
const alias = this.config.models?.[model];
|
|
301912
|
+
if (alias === void 0)
|
|
301913
|
+
throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" is not configured in config.toml. Add a [models."${model}"] entry with max_context_size.`);
|
|
301914
|
+
const effectiveAlias = effectiveModelAlias(alias);
|
|
301915
|
+
const providerName = alias.provider ?? this.config.defaultProvider;
|
|
301916
|
+
if (providerName === void 0)
|
|
301917
|
+
throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a provider in config.toml.`);
|
|
301918
|
+
const providerConfig = this.config.providers[providerName];
|
|
301919
|
+
if (providerConfig === void 0)
|
|
301920
|
+
throw new BlunError(ErrorCodes.CONFIG_INVALID, `Provider "${providerName}" for model "${model}" is not configured.`);
|
|
301921
|
+
assertBlunProviderType(providerConfig);
|
|
301922
|
+
const runtimeContext = this.runtimeModelContexts.get(model);
|
|
301923
|
+
const maxContextSize = runtimeContext?.providerName === providerName && runtimeContext.model === alias.model ? runtimeContext.maxContextTokens : effectiveAlias.maxContextSize;
|
|
301924
|
+
if (!Number.isInteger(maxContextSize) || maxContextSize <= 0)
|
|
301925
|
+
throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a positive max_context_size in config.toml.`);
|
|
301926
|
+
const provider = toKosongProviderConfig(providerConfig, alias.model, this.options.blunRequestHeaders, this.options.promptCacheKey, effectiveAlias.supportEfforts);
|
|
301927
|
+
if (this.options.configPath !== undefined)
|
|
301928
|
+
provider.requestBodyByteLimitStore = t3458Store(this.options.configPath, parse$7, stringify$1, atomicWritePrivate$1);
|
|
301929
|
+
return {
|
|
301930
|
+
providerName,
|
|
301931
|
+
provider,
|
|
301932
|
+
modelCapabilities: resolveModelCapabilities({
|
|
301933
|
+
...effectiveAlias,
|
|
301934
|
+
maxContextSize
|
|
301935
|
+
}, provider),
|
|
301936
|
+
alwaysThinking: (effectiveAlias.capabilities ?? []).some((c) => c.trim().toLowerCase() === "always_thinking"),
|
|
301937
|
+
maxOutputSize: effectiveAlias.maxOutputSize,
|
|
301938
|
+
type: "blun"
|
|
301939
|
+
};
|
|
301940
|
+
}
|
|
301750
301941
|
setRuntimeModelContext(input) {
|
|
301751
301942
|
if (!MANAGED_BLUN_MODEL_ALIASES$1.has(input.modelAlias)) throw new BlunError(ErrorCodes.CONFIG_INVALID, "Runtime context metadata is supported only for internal BLUN model aliases.");
|
|
301752
301943
|
if (!Number.isSafeInteger(input.maxContextTokens) || input.maxContextTokens <= 0) throw new BlunError(ErrorCodes.CONFIG_INVALID, "Runtime model context must be a positive integer.");
|
|
@@ -338157,14 +338348,15 @@ var init_core_impl = __esmMin((() => {
|
|
|
338157
338348
|
};
|
|
338158
338349
|
}
|
|
338159
338350
|
resolveProviderManager(sessionId) {
|
|
338160
|
-
|
|
338161
|
-
|
|
338162
|
-
|
|
338163
|
-
|
|
338164
|
-
|
|
338165
|
-
|
|
338166
|
-
|
|
338167
|
-
|
|
338351
|
+
return new ProviderManager({
|
|
338352
|
+
config: () => this.config,
|
|
338353
|
+
configPath: this.configPath,
|
|
338354
|
+
blunRequestHeaders: this.blunRequestHeaders,
|
|
338355
|
+
resolveOAuthTokenProvider: this.resolveOAuthTokenProvider,
|
|
338356
|
+
managedApiKeyRevocationStore: this.managedApiKeyRevocationStore,
|
|
338357
|
+
promptCacheKey: sessionId
|
|
338358
|
+
});
|
|
338359
|
+
}
|
|
338168
338360
|
mergePluginMcpConfig(base) {
|
|
338169
338361
|
const pluginEntries = this.plugins.mcpServerConfigs();
|
|
338170
338362
|
if (pluginEntries.length === 0) return base;
|
|
@@ -368239,7 +368431,7 @@ function optionalBuildString(value) {
|
|
|
368239
368431
|
var BLUN_BUILD_INFO;
|
|
368240
368432
|
var init_build_info = __esmMin((() => {
|
|
368241
368433
|
BLUN_BUILD_INFO = {
|
|
368242
|
-
version: optionalBuildString("9.1.
|
|
368434
|
+
version: optionalBuildString("9.1.640"),
|
|
368243
368435
|
channel: typeof __BLUN_CHANNEL__ === "string" ? optionalBuildString(__BLUN_CHANNEL__) : void 0,
|
|
368244
368436
|
commit: typeof __BLUN_COMMIT__ === "string" ? optionalBuildString(__BLUN_COMMIT__) : void 0,
|
|
368245
368437
|
buildTarget: typeof __BLUN_BUILD_TARGET__ === "string" ? optionalBuildString(__BLUN_BUILD_TARGET__) : void 0
|
|
@@ -562515,3 +562707,169 @@ async function logStartupFailure(operation, error) {
|
|
|
562515
562707
|
} catch {}
|
|
562516
562708
|
}
|
|
562517
562709
|
export { configureTelegramPluginLaunchMode, handleMainCommand, handleUpgradeCommand, main };
|
|
562710
|
+
|
|
562711
|
+
import * as t3458Fs from "node:fs/promises";
|
|
562712
|
+
import * as t3458Path from "node:path";
|
|
562713
|
+
import * as t3458Crypto from "node:crypto";
|
|
562714
|
+
// Neue, selektive Verdichtungshelfer. Keine Quellübernahme aus Arbeitsbaum oder v2/v3.
|
|
562715
|
+
function t3458ValidLimit(value) {
|
|
562716
|
+
return Number.isSafeInteger(value) && value > 0 ? value : undefined;
|
|
562717
|
+
}
|
|
562718
|
+
function t3458Learned(value) {
|
|
562719
|
+
return t3458ValidLimit(value) !== undefined && value >= 65536 ? value : undefined;
|
|
562720
|
+
}
|
|
562721
|
+
function t3458Endpoint(baseUrl) {
|
|
562722
|
+
try {
|
|
562723
|
+
const url = new URL(baseUrl);
|
|
562724
|
+
if (!['https:', 'http:'].includes(url.protocol) || url.username || url.password || url.search || url.hash)
|
|
562725
|
+
return undefined;
|
|
562726
|
+
return `${url.origin}${url.pathname.replace(/\/+$/, '')}`;
|
|
562727
|
+
}
|
|
562728
|
+
catch {
|
|
562729
|
+
return undefined;
|
|
562730
|
+
}
|
|
562731
|
+
}
|
|
562732
|
+
function t3458Key(baseUrl) {
|
|
562733
|
+
const endpoint = t3458Endpoint(baseUrl);
|
|
562734
|
+
return endpoint === undefined ? undefined : t3458Crypto.createHash('sha256').update(endpoint).digest('hex');
|
|
562735
|
+
}
|
|
562736
|
+
async function t3458WithConfigLock(filePath, action, learning = false) {
|
|
562737
|
+
await t3458Fs.mkdir(t3458Path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
562738
|
+
const lock = `${filePath}.request-body.lock`;
|
|
562739
|
+
const token = t3458Crypto.randomUUID();
|
|
562740
|
+
// Small scheduling reserve: bounded contention attempts stay below 2s / 12s.
|
|
562741
|
+
const deadline = performance.now() + (learning ? 1950 : 11950);
|
|
562742
|
+
let handle;
|
|
562743
|
+
while (handle === undefined) {
|
|
562744
|
+
if (performance.now() >= deadline)
|
|
562745
|
+
return learning ? undefined : await action();
|
|
562746
|
+
try {
|
|
562747
|
+
handle = await t3458Fs.open(lock, 'wx', 0o600);
|
|
562748
|
+
}
|
|
562749
|
+
catch (error) {
|
|
562750
|
+
if (error.code !== 'EEXIST' && !(process.platform === 'win32' && ['EPERM', 'EACCES', 'EBUSY'].includes(error.code)))
|
|
562751
|
+
throw error;
|
|
562752
|
+
try {
|
|
562753
|
+
const observed = await t3458Fs.stat(lock);
|
|
562754
|
+
if (Date.now() - observed.mtimeMs > 10000) {
|
|
562755
|
+
const owner = await t3458Fs.readFile(lock, 'utf8');
|
|
562756
|
+
const current = await t3458Fs.stat(lock);
|
|
562757
|
+
// Best effort only: the accepted check/unlink race is not fencing.
|
|
562758
|
+
if (current.mtimeMs === observed.mtimeMs && await t3458Fs.readFile(lock, 'utf8') === owner) {
|
|
562759
|
+
await t3458Fs.unlink(lock);
|
|
562760
|
+
continue;
|
|
562761
|
+
}
|
|
562762
|
+
}
|
|
562763
|
+
}
|
|
562764
|
+
catch (failure) {
|
|
562765
|
+
if (failure.code !== 'ENOENT')
|
|
562766
|
+
throw failure;
|
|
562767
|
+
}
|
|
562768
|
+
const remaining = deadline - performance.now();
|
|
562769
|
+
if (remaining > 0)
|
|
562770
|
+
await new Promise(resolve => setTimeout(resolve, Math.min(25, remaining)));
|
|
562771
|
+
}
|
|
562772
|
+
}
|
|
562773
|
+
try {
|
|
562774
|
+
await handle.writeFile(token, 'utf8');
|
|
562775
|
+
return await action();
|
|
562776
|
+
}
|
|
562777
|
+
finally {
|
|
562778
|
+
await handle.close();
|
|
562779
|
+
try {
|
|
562780
|
+
if (await t3458Fs.readFile(lock, 'utf8') === token)
|
|
562781
|
+
await t3458Fs.unlink(lock);
|
|
562782
|
+
}
|
|
562783
|
+
catch (error) {
|
|
562784
|
+
if (error.code !== 'ENOENT')
|
|
562785
|
+
throw error;
|
|
562786
|
+
}
|
|
562787
|
+
}
|
|
562788
|
+
}
|
|
562789
|
+
function t3458Store(filePath, parseToml, stringifyToml, atomicWrite) {
|
|
562790
|
+
return {
|
|
562791
|
+
async load(key) {
|
|
562792
|
+
try {
|
|
562793
|
+
return t3458Learned(parseToml(await t3458Fs.readFile(filePath, 'utf8')).request_body_byte_limits?.[key]);
|
|
562794
|
+
}
|
|
562795
|
+
catch (error) {
|
|
562796
|
+
if (error.code === 'ENOENT')
|
|
562797
|
+
return undefined;
|
|
562798
|
+
throw error;
|
|
562799
|
+
}
|
|
562800
|
+
},
|
|
562801
|
+
async save(key, value) {
|
|
562802
|
+
if (!/^[a-f0-9]{64}$/.test(key) || t3458Learned(value) === undefined)
|
|
562803
|
+
return;
|
|
562804
|
+
await t3458WithConfigLock(filePath, async () => {
|
|
562805
|
+
let data;
|
|
562806
|
+
try {
|
|
562807
|
+
data = parseToml(await t3458Fs.readFile(filePath, 'utf8'));
|
|
562808
|
+
}
|
|
562809
|
+
catch (error) {
|
|
562810
|
+
if (error.code !== 'ENOENT')
|
|
562811
|
+
throw error;
|
|
562812
|
+
data = {};
|
|
562813
|
+
}
|
|
562814
|
+
const table = data.request_body_byte_limits ?? {};
|
|
562815
|
+
const previous = t3458Learned(table[key]);
|
|
562816
|
+
data.request_body_byte_limits = { ...table, [key]: previous === undefined ? value : Math.min(previous, value) };
|
|
562817
|
+
await atomicWrite(filePath, `${stringifyToml(data)}\n`);
|
|
562818
|
+
}, true);
|
|
562819
|
+
}
|
|
562820
|
+
};
|
|
562821
|
+
}
|
|
562822
|
+
async function t3458Measure(provider, params, options) {
|
|
562823
|
+
const endpoint = t3458Endpoint(provider._baseUrl);
|
|
562824
|
+
const key = t3458Key(provider._baseUrl);
|
|
562825
|
+
const option = typeof provider._requestBodyByteLimit === 'function' ? provider._requestBodyByteLimit() : provider._requestBodyByteLimit;
|
|
562826
|
+
const configured = t3458ValidLimit(options?.requestBodyBudget?.limitBytes) ?? t3458ValidLimit(option) ?? t3458ValidLimit(Number(process.env.BLUN_REQUEST_BODY_BYTE_LIMIT));
|
|
562827
|
+
let persisted;
|
|
562828
|
+
if (key !== undefined && provider._requestBodyByteLimitStore !== undefined) {
|
|
562829
|
+
try {
|
|
562830
|
+
persisted = t3458Learned(await provider._requestBodyByteLimitStore.load(key));
|
|
562831
|
+
}
|
|
562832
|
+
catch { }
|
|
562833
|
+
}
|
|
562834
|
+
const learned = t3458Learned(provider._t3458ObservedLimit);
|
|
562835
|
+
const observed = learned === undefined ? persisted : persisted === undefined ? learned : Math.min(learned, persisted);
|
|
562836
|
+
const fallback = endpoint === 'https://api.blun.ai/coding/v1' ? 500000 : undefined;
|
|
562837
|
+
const limit = configured === undefined ? observed ?? fallback : observed === undefined ? configured : Math.min(configured, observed);
|
|
562838
|
+
const bytes = Buffer.byteLength(JSON.stringify(params), 'utf8');
|
|
562839
|
+
options?.requestBodyBudget?.onMeasured?.(bytes, limit, endpoint);
|
|
562840
|
+
if (!options?.isCompactionRequest && limit !== undefined && bytes >= limit)
|
|
562841
|
+
throw t3458Pressure(bytes, limit, endpoint);
|
|
562842
|
+
return { key, limit, bytes };
|
|
562843
|
+
}
|
|
562844
|
+
function t3458Pressure(byteLength, limitBytes, endpoint) {
|
|
562845
|
+
const error = new ChatProviderError(`Request body ${byteLength} bytes reaches limit ${limitBytes} bytes.`);
|
|
562846
|
+
error.name = 'APIRequestBodyPressureError';
|
|
562847
|
+
error.requestBodyPressure = true;
|
|
562848
|
+
error.byteLength = byteLength;
|
|
562849
|
+
error.limitBytes = limitBytes;
|
|
562850
|
+
error.endpoint = endpoint;
|
|
562851
|
+
return error;
|
|
562852
|
+
}
|
|
562853
|
+
function t3458Observed(error) {
|
|
562854
|
+
const message = error instanceof Error ? error.message : '';
|
|
562855
|
+
if (!/(?:request\s*body|payload|body\s*size)/i.test(message))
|
|
562856
|
+
return undefined;
|
|
562857
|
+
const comparison = /\(\s*\d+\s*>\s*(\d+)\s*bytes?\s*\)/i.exec(message);
|
|
562858
|
+
if (comparison !== null)
|
|
562859
|
+
return t3458Learned(Number(comparison[1]));
|
|
562860
|
+
const match = /(?:limit|maximum|max|up to)[^\d]{0,30}(\d{2,12})\s*(?:bytes?|b)\b/i.exec(message);
|
|
562861
|
+
return match === null ? undefined : t3458Learned(Number(match[1]));
|
|
562862
|
+
}
|
|
562863
|
+
async function t3458Learn(provider, error, measured, options) {
|
|
562864
|
+
const limit = t3458Observed(error);
|
|
562865
|
+
if (limit === undefined)
|
|
562866
|
+
return;
|
|
562867
|
+
provider._t3458ObservedLimit = Math.min(t3458Learned(provider._t3458ObservedLimit) ?? Infinity, limit);
|
|
562868
|
+
options?.requestBodyBudget?.onLimitObserved?.(limit);
|
|
562869
|
+
if (measured.key !== undefined && provider._requestBodyByteLimitStore !== undefined) {
|
|
562870
|
+
try {
|
|
562871
|
+
await provider._requestBodyByteLimitStore.save(measured.key, limit);
|
|
562872
|
+
}
|
|
562873
|
+
catch { }
|
|
562874
|
+
}
|
|
562875
|
+
}
|