blun-king-cli 9.1.639 → 9.1.640
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 +6 -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 +959 -659
- package/package.json +1 -1
- package/worker-host.mjs +959 -659
package/worker-host.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// BLUN_BUILD_INPUT_SHA256:
|
|
2
|
+
// BLUN_BUILD_INPUT_SHA256:0b1614e40c5a323438b4f9cd742ea9a101c55f926d094603efc957fad265637a
|
|
3
3
|
/*! wasm-webp 0.1.0
|
|
4
4
|
MIT License
|
|
5
5
|
|
|
@@ -2751,21 +2751,23 @@ var BlunChatProvider = class {
|
|
|
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 BlunChatProvider = class {
|
|
|
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;
|
|
@@ -15871,13 +15896,25 @@ function transformLoopControlData(data) {
|
|
|
15871
15896
|
return out;
|
|
15872
15897
|
}
|
|
15873
15898
|
async function writeConfigFile(filePath, config) {
|
|
15874
|
-
|
|
15875
|
-
|
|
15876
|
-
|
|
15877
|
-
|
|
15878
|
-
|
|
15879
|
-
|
|
15880
|
-
|
|
15899
|
+
return t3458WithConfigLock(filePath, async () => {
|
|
15900
|
+
let latestTable;
|
|
15901
|
+
try {
|
|
15902
|
+
latestTable = parse$6(await t3458Fs.readFile(filePath, "utf8")).request_body_byte_limits;
|
|
15903
|
+
}
|
|
15904
|
+
catch (error) {
|
|
15905
|
+
/* Unreadable or invalid previous TOML: preserve the Registry639 overwrite behavior. */
|
|
15906
|
+
}
|
|
15907
|
+
if (latestTable !== undefined)
|
|
15908
|
+
config = { ...config, raw: { ...config.raw, request_body_byte_limits: latestTable } };
|
|
15909
|
+
const validated = validateConfig(stripEnvModelConfig(config));
|
|
15910
|
+
const configDirectory = dirname$3(filePath);
|
|
15911
|
+
if (await mkdir(configDirectory, {
|
|
15912
|
+
recursive: true,
|
|
15913
|
+
mode: 448
|
|
15914
|
+
}) !== void 0)
|
|
15915
|
+
await hardenPrivatePath(configDirectory, "directory");
|
|
15916
|
+
await atomicWritePrivate(filePath, `${stringify$1(configToTomlData(validated))}\n`);
|
|
15917
|
+
});
|
|
15881
15918
|
}
|
|
15882
15919
|
function configToTomlData(config) {
|
|
15883
15920
|
const out = cloneRecord$1(config.raw);
|
|
@@ -229222,8 +229259,8 @@ var FullCompaction = class {
|
|
|
229222
229259
|
};
|
|
229223
229260
|
}
|
|
229224
229261
|
shouldRecoverFromContextOverflow(error) {
|
|
229225
|
-
|
|
229226
|
-
|
|
229262
|
+
return error instanceof APIContextOverflowError || error?.requestBodyPressure === true;
|
|
229263
|
+
}
|
|
229227
229264
|
observeContextOverflow(error, modelAlias = this.agent.activeResponderModel ?? this.agent.config.modelAlias) {
|
|
229228
229265
|
if (!(error instanceof APIContextOverflowError)) return;
|
|
229229
229266
|
const observed = error.maxContextTokens;
|
|
@@ -229239,63 +229276,70 @@ var FullCompaction = class {
|
|
|
229239
229276
|
if (this.lastCompactedState?.modelAlias === modelAlias) this.lastCompactedState = null;
|
|
229240
229277
|
}
|
|
229241
229278
|
begin(data) {
|
|
229242
|
-
|
|
229243
|
-
|
|
229244
|
-
|
|
229245
|
-
|
|
229246
|
-
|
|
229247
|
-
|
|
229248
|
-
|
|
229249
|
-
|
|
229250
|
-
|
|
229251
|
-
|
|
229252
|
-
|
|
229253
|
-
|
|
229254
|
-
|
|
229255
|
-
|
|
229256
|
-
|
|
229257
|
-
|
|
229258
|
-
|
|
229259
|
-
|
|
229260
|
-
|
|
229261
|
-
|
|
229262
|
-
|
|
229263
|
-
|
|
229264
|
-
|
|
229265
|
-
|
|
229266
|
-
|
|
229267
|
-
|
|
229268
|
-
|
|
229269
|
-
|
|
229270
|
-
|
|
229271
|
-
|
|
229272
|
-
|
|
229273
|
-
|
|
229274
|
-
|
|
229275
|
-
|
|
229276
|
-
|
|
229277
|
-
|
|
229278
|
-
|
|
229279
|
-
|
|
229280
|
-
|
|
229281
|
-
|
|
229282
|
-
|
|
229283
|
-
|
|
229284
|
-
|
|
229285
|
-
|
|
229286
|
-
|
|
229287
|
-
|
|
229288
|
-
|
|
229289
|
-
|
|
229290
|
-
|
|
229291
|
-
|
|
229292
|
-
|
|
229293
|
-
|
|
229294
|
-
|
|
229295
|
-
|
|
229296
|
-
|
|
229297
|
-
|
|
229298
|
-
|
|
229279
|
+
this.assertAdmissionOpen();
|
|
229280
|
+
if (this.compacting)
|
|
229281
|
+
return;
|
|
229282
|
+
if (this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES)
|
|
229283
|
+
throw this.sessionStoppedError(ErrorCodes.COMPACTION_FAILED);
|
|
229284
|
+
if (data.source === "manual")
|
|
229285
|
+
this.compactionCountInTurn = 0;
|
|
229286
|
+
else if (!data.requestBodyPressure)
|
|
229287
|
+
this.compactionCountInTurn += 1;
|
|
229288
|
+
if (this.compactionCountInTurn > this.strategy.maxCompactionPerTurn)
|
|
229289
|
+
return;
|
|
229290
|
+
if (this.agent.records.restoring) {
|
|
229291
|
+
this.agent.replayBuilder.push({
|
|
229292
|
+
type: "compaction",
|
|
229293
|
+
instruction: data.instruction
|
|
229294
|
+
});
|
|
229295
|
+
return;
|
|
229296
|
+
}
|
|
229297
|
+
if (this.agent.context.history.length === 0)
|
|
229298
|
+
throw new BlunError(ErrorCodes.COMPACTION_UNABLE, "No messages to compact in current history.");
|
|
229299
|
+
if (data.source === "manual" && this.agent.turn.hasActiveTurn)
|
|
229300
|
+
throw new BlunError(ErrorCodes.COMPACTION_UNABLE, "Cannot compact while a turn is active. Wait for it to finish, then retry.");
|
|
229301
|
+
this.agent.records.logRecord({
|
|
229302
|
+
type: "full_compaction.begin",
|
|
229303
|
+
...data
|
|
229304
|
+
});
|
|
229305
|
+
this.assertAdmissionOpen();
|
|
229306
|
+
const abortController = new AbortController();
|
|
229307
|
+
const executionId = ++this.nextExecutionId;
|
|
229308
|
+
let resolve;
|
|
229309
|
+
let reject;
|
|
229310
|
+
const promise = new Promise((yes, no) => {
|
|
229311
|
+
resolve = yes;
|
|
229312
|
+
reject = no;
|
|
229313
|
+
});
|
|
229314
|
+
this.executions.track({
|
|
229315
|
+
kind: "worker",
|
|
229316
|
+
id: executionId,
|
|
229317
|
+
source: data.source
|
|
229318
|
+
}, promise);
|
|
229319
|
+
this.executingControllers.add(abortController);
|
|
229320
|
+
this.compacting = {
|
|
229321
|
+
abortController,
|
|
229322
|
+
promise,
|
|
229323
|
+
blockedByTurn: false
|
|
229324
|
+
};
|
|
229325
|
+
const started = {
|
|
229326
|
+
type: "compaction.started",
|
|
229327
|
+
trigger: data.source,
|
|
229328
|
+
instruction: data.instruction
|
|
229329
|
+
};
|
|
229330
|
+
this.agent.emitEvent(this.agent.experimentalFlags.enabled("queue_policies") ? {
|
|
229331
|
+
...started,
|
|
229332
|
+
executionId,
|
|
229333
|
+
contextTurnId: this.agent.turn.currentId
|
|
229334
|
+
} : started);
|
|
229335
|
+
this.compactionWorker(abortController.signal, data, executionId).then(() => {
|
|
229336
|
+
this.executingControllers.delete(abortController);
|
|
229337
|
+
resolve();
|
|
229338
|
+
}, (error) => {
|
|
229339
|
+
this.executingControllers.delete(abortController);
|
|
229340
|
+
reject(error);
|
|
229341
|
+
});
|
|
229342
|
+
}
|
|
229299
229343
|
cancel() {
|
|
229300
229344
|
this.agent.replayBuilder.patchLast("compaction", { result: "cancelled" });
|
|
229301
229345
|
if (!this.compacting) return;
|
|
@@ -229328,12 +229372,28 @@ var FullCompaction = class {
|
|
|
229328
229372
|
this.sessionCompactionFailures = Math.max(this.sessionCompactionFailures, state.sessionCompactionFailures);
|
|
229329
229373
|
}
|
|
229330
229374
|
async handleOverflowError(signal, error) {
|
|
229331
|
-
|
|
229332
|
-
|
|
229333
|
-
|
|
229334
|
-
|
|
229335
|
-
|
|
229336
|
-
|
|
229375
|
+
if (error?.requestBodyPressure === true && error.byteLength < error.limitBytes) {
|
|
229376
|
+
signal.throwIfAborted();
|
|
229377
|
+
if (!this.hasShrinkableHistory(this.agent.context.history) || this.isSessionCompactionStopped)
|
|
229378
|
+
return;
|
|
229379
|
+
try {
|
|
229380
|
+
this.begin({ source: "auto", instruction: undefined, requestBodyPressure: true });
|
|
229381
|
+
await this.block(signal);
|
|
229382
|
+
}
|
|
229383
|
+
catch (failure) {
|
|
229384
|
+
signal.throwIfAborted();
|
|
229385
|
+
this.agent.log.warn("optional request-body compaction failed; valid request remains sendable", { error: failure });
|
|
229386
|
+
}
|
|
229387
|
+
return;
|
|
229388
|
+
}
|
|
229389
|
+
this.consecutiveOverflowCompactions += 1;
|
|
229390
|
+
const maxAttempts = this.strategy.maxOverflowCompactionAttempts;
|
|
229391
|
+
if (this.consecutiveOverflowCompactions > maxAttempts)
|
|
229392
|
+
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 });
|
|
229393
|
+
if (!this.beginAutoCompaction() && !this.compacting)
|
|
229394
|
+
throw error;
|
|
229395
|
+
await this.block(signal);
|
|
229396
|
+
}
|
|
229337
229397
|
async beforeStep(signal, requestContext = {}) {
|
|
229338
229398
|
signal.throwIfAborted();
|
|
229339
229399
|
const historyBefore = this.agent.context.history;
|
|
@@ -229386,10 +229446,28 @@ var FullCompaction = class {
|
|
|
229386
229446
|
if (this.requiresShrinkableHistory && !this.hasShrinkableHistory(messages)) return false;
|
|
229387
229447
|
return this.beginAutoCompaction(throwOnLimit);
|
|
229388
229448
|
}
|
|
229389
|
-
|
|
229390
|
-
|
|
229391
|
-
|
|
229392
|
-
|
|
229449
|
+
shouldCompactRequestBody(endpoint, bytes, limit) {
|
|
229450
|
+
if (bytes >= limit)
|
|
229451
|
+
return true;
|
|
229452
|
+
const attempts = this.t3458PressureAttempts ??= new Map();
|
|
229453
|
+
const key = endpoint + "\0" + String(limit);
|
|
229454
|
+
if (bytes < limit * 0.75) {
|
|
229455
|
+
attempts.delete(key);
|
|
229456
|
+
return false;
|
|
229457
|
+
}
|
|
229458
|
+
if (this.isSessionCompactionStopped || !this.hasShrinkableHistory(this.agent.context.history))
|
|
229459
|
+
return false;
|
|
229460
|
+
const previous = attempts.get(key);
|
|
229461
|
+
if (previous !== undefined && bytes < previous + Math.ceil(previous / 10))
|
|
229462
|
+
return false;
|
|
229463
|
+
attempts.set(key, bytes);
|
|
229464
|
+
return true;
|
|
229465
|
+
}
|
|
229466
|
+
hasShrinkableHistory(messages) {
|
|
229467
|
+
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))))
|
|
229468
|
+
return true;
|
|
229469
|
+
return estimateTokensForMessages(messages.filter(isRealUserInput)) > COMPACT_USER_MESSAGE_MAX_TOKENS;
|
|
229470
|
+
}
|
|
229393
229471
|
beginAutoCompaction(throwOnLimit = true) {
|
|
229394
229472
|
if (this.compacting) return true;
|
|
229395
229473
|
if (this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES) {
|
|
@@ -229433,52 +229511,57 @@ var FullCompaction = class {
|
|
|
229433
229511
|
}
|
|
229434
229512
|
}
|
|
229435
229513
|
async compactionWorker(signal, data, executionId) {
|
|
229436
|
-
|
|
229437
|
-
|
|
229438
|
-
|
|
229439
|
-
|
|
229440
|
-
|
|
229441
|
-
|
|
229442
|
-
|
|
229443
|
-
|
|
229444
|
-
|
|
229445
|
-
|
|
229446
|
-
|
|
229447
|
-
|
|
229448
|
-
|
|
229449
|
-
|
|
229450
|
-
|
|
229451
|
-
|
|
229452
|
-
|
|
229453
|
-
|
|
229454
|
-
|
|
229455
|
-
|
|
229456
|
-
|
|
229457
|
-
|
|
229458
|
-
|
|
229459
|
-
|
|
229460
|
-
|
|
229461
|
-
|
|
229462
|
-
|
|
229463
|
-
|
|
229464
|
-
|
|
229465
|
-
|
|
229466
|
-
|
|
229467
|
-
|
|
229468
|
-
|
|
229469
|
-
|
|
229470
|
-
|
|
229471
|
-
|
|
229472
|
-
|
|
229473
|
-
|
|
229474
|
-
|
|
229475
|
-
|
|
229476
|
-
|
|
229477
|
-
|
|
229478
|
-
|
|
229479
|
-
|
|
229480
|
-
|
|
229481
|
-
|
|
229514
|
+
try {
|
|
229515
|
+
const output = await this.compactionRound(signal, data, executionId);
|
|
229516
|
+
if (!output)
|
|
229517
|
+
return;
|
|
229518
|
+
const { result, stageCount } = output;
|
|
229519
|
+
try {
|
|
229520
|
+
await this.agent.refreshSystemPrompt();
|
|
229521
|
+
}
|
|
229522
|
+
catch (error) {
|
|
229523
|
+
this.agent.log.error("failed to refresh system prompt after compaction", { error });
|
|
229524
|
+
}
|
|
229525
|
+
await this.agent.injection.injectAfterCompaction();
|
|
229526
|
+
this.fitRetainedInput({}, result.keptHeadUserMessageCount !== void 0);
|
|
229527
|
+
this.lastCompactedState = {
|
|
229528
|
+
tokenCount: this.tokenCountWithPending,
|
|
229529
|
+
modelAlias: this.agent.activeResponderModel ?? this.agent.config.modelAlias,
|
|
229530
|
+
maxContextTokens: this.getEffectiveMaxContextTokens()
|
|
229531
|
+
};
|
|
229532
|
+
this.markCompleted();
|
|
229533
|
+
const { contextSummary: _contextSummary, ...eventResult } = result;
|
|
229534
|
+
const projectedContextTokens = this.estimateCurrentRequestTokens();
|
|
229535
|
+
this.agent.emitEvent({
|
|
229536
|
+
type: "compaction.completed",
|
|
229537
|
+
result: eventResult,
|
|
229538
|
+
projectedContextTokens,
|
|
229539
|
+
...stageCount > 1 ? { stageCount } : {}
|
|
229540
|
+
});
|
|
229541
|
+
this.triggerPostCompactHook(data, result);
|
|
229542
|
+
}
|
|
229543
|
+
catch (error) {
|
|
229544
|
+
if (isAbortError(error) || signal.aborted)
|
|
229545
|
+
return;
|
|
229546
|
+
if (!data.requestBodyPressure) {
|
|
229547
|
+
this.sessionCompactionFailures += 1;
|
|
229548
|
+
this.agent.records.logRecord({ type: "full_compaction.session_failures", count: this.sessionCompactionFailures });
|
|
229549
|
+
}
|
|
229550
|
+
const blockedByTurn = this.compacting?.blockedByTurn === true;
|
|
229551
|
+
this.cancel();
|
|
229552
|
+
this.agent.log.error("compaction failed", { error });
|
|
229553
|
+
const visibleError = this.sessionCompactionFailures >= MAX_SESSION_COMPACTION_FAILURES ? this.sessionStoppedError(ErrorCodes.COMPACTION_FAILED, error) : error;
|
|
229554
|
+
if (blockedByTurn)
|
|
229555
|
+
throw visibleError;
|
|
229556
|
+
this.agent.emitEvent({
|
|
229557
|
+
type: "error",
|
|
229558
|
+
...toBlunErrorPayload(visibleError)
|
|
229559
|
+
});
|
|
229560
|
+
}
|
|
229561
|
+
finally {
|
|
229562
|
+
this.agent.turn.onCompactionFinished();
|
|
229563
|
+
}
|
|
229564
|
+
}
|
|
229482
229565
|
fitRetainedInput(context = {}, freshBudgetedSelection = false) {
|
|
229483
229566
|
const budget = this.getCompactionBudgetTokens();
|
|
229484
229567
|
const window = this.getEffectiveMaxContextTokens();
|
|
@@ -229502,336 +229585,367 @@ var FullCompaction = class {
|
|
|
229502
229585
|
return `${summary.trim()}\n\n${todoMarkdown}`;
|
|
229503
229586
|
}
|
|
229504
229587
|
async compactionRound(signal, data, executionId) {
|
|
229505
|
-
|
|
229506
|
-
|
|
229507
|
-
|
|
229508
|
-
|
|
229509
|
-
|
|
229510
|
-
|
|
229511
|
-
|
|
229512
|
-
|
|
229513
|
-
|
|
229514
|
-
|
|
229515
|
-
|
|
229516
|
-
|
|
229517
|
-
|
|
229518
|
-
|
|
229519
|
-
|
|
229520
|
-
|
|
229521
|
-
|
|
229522
|
-
|
|
229523
|
-
|
|
229524
|
-
|
|
229525
|
-
|
|
229526
|
-
|
|
229527
|
-
|
|
229528
|
-
|
|
229529
|
-
|
|
229530
|
-
|
|
229531
|
-
|
|
229532
|
-
|
|
229533
|
-
|
|
229534
|
-
|
|
229535
|
-
|
|
229536
|
-
|
|
229537
|
-
|
|
229538
|
-
|
|
229539
|
-
|
|
229540
|
-
|
|
229541
|
-
|
|
229542
|
-
|
|
229543
|
-
|
|
229544
|
-
|
|
229545
|
-
|
|
229546
|
-
|
|
229547
|
-
|
|
229548
|
-
|
|
229549
|
-
|
|
229550
|
-
|
|
229551
|
-
|
|
229552
|
-
|
|
229553
|
-
|
|
229554
|
-
|
|
229555
|
-
|
|
229556
|
-
|
|
229557
|
-
|
|
229558
|
-
|
|
229559
|
-
|
|
229560
|
-
|
|
229561
|
-
|
|
229562
|
-
|
|
229563
|
-
|
|
229564
|
-
|
|
229565
|
-
|
|
229566
|
-
|
|
229567
|
-
|
|
229568
|
-
|
|
229569
|
-
|
|
229570
|
-
|
|
229571
|
-
|
|
229572
|
-
|
|
229573
|
-
|
|
229574
|
-
|
|
229575
|
-
|
|
229576
|
-
|
|
229577
|
-
|
|
229578
|
-
|
|
229579
|
-
|
|
229580
|
-
|
|
229581
|
-
|
|
229582
|
-
|
|
229583
|
-
|
|
229584
|
-
|
|
229585
|
-
|
|
229586
|
-
|
|
229587
|
-
|
|
229588
|
-
|
|
229589
|
-
|
|
229590
|
-
|
|
229591
|
-
|
|
229592
|
-
|
|
229593
|
-
|
|
229594
|
-
|
|
229595
|
-
|
|
229596
|
-
|
|
229597
|
-
|
|
229598
|
-
|
|
229599
|
-
|
|
229600
|
-
|
|
229601
|
-
|
|
229602
|
-
|
|
229603
|
-
|
|
229604
|
-
|
|
229605
|
-
|
|
229606
|
-
|
|
229607
|
-
|
|
229608
|
-
|
|
229609
|
-
|
|
229610
|
-
|
|
229611
|
-
|
|
229612
|
-
|
|
229613
|
-
|
|
229614
|
-
|
|
229615
|
-
|
|
229616
|
-
|
|
229617
|
-
|
|
229618
|
-
|
|
229619
|
-
|
|
229620
|
-
|
|
229621
|
-
|
|
229622
|
-
|
|
229623
|
-
|
|
229624
|
-
|
|
229625
|
-
|
|
229626
|
-
|
|
229627
|
-
|
|
229628
|
-
|
|
229629
|
-
|
|
229630
|
-
|
|
229631
|
-
|
|
229632
|
-
|
|
229633
|
-
|
|
229634
|
-
|
|
229635
|
-
|
|
229636
|
-
|
|
229637
|
-
|
|
229638
|
-
|
|
229639
|
-
|
|
229640
|
-
|
|
229641
|
-
|
|
229642
|
-
|
|
229643
|
-
|
|
229644
|
-
|
|
229645
|
-
|
|
229646
|
-
|
|
229647
|
-
|
|
229648
|
-
|
|
229649
|
-
|
|
229650
|
-
|
|
229651
|
-
|
|
229652
|
-
|
|
229653
|
-
|
|
229654
|
-
|
|
229655
|
-
|
|
229656
|
-
|
|
229657
|
-
|
|
229658
|
-
|
|
229659
|
-
|
|
229660
|
-
|
|
229661
|
-
|
|
229662
|
-
|
|
229663
|
-
|
|
229664
|
-
|
|
229665
|
-
|
|
229666
|
-
|
|
229667
|
-
|
|
229668
|
-
|
|
229669
|
-
|
|
229670
|
-
|
|
229671
|
-
|
|
229672
|
-
|
|
229673
|
-
|
|
229674
|
-
|
|
229675
|
-
|
|
229676
|
-
|
|
229677
|
-
|
|
229678
|
-
|
|
229679
|
-
|
|
229680
|
-
|
|
229681
|
-
|
|
229682
|
-
|
|
229683
|
-
|
|
229684
|
-
|
|
229685
|
-
|
|
229686
|
-
|
|
229687
|
-
|
|
229688
|
-
|
|
229689
|
-
|
|
229690
|
-
|
|
229691
|
-
|
|
229692
|
-
|
|
229693
|
-
|
|
229694
|
-
|
|
229695
|
-
|
|
229696
|
-
|
|
229697
|
-
|
|
229698
|
-
|
|
229699
|
-
|
|
229700
|
-
|
|
229701
|
-
|
|
229702
|
-
|
|
229703
|
-
|
|
229704
|
-
|
|
229705
|
-
|
|
229706
|
-
|
|
229707
|
-
|
|
229708
|
-
|
|
229709
|
-
|
|
229710
|
-
|
|
229711
|
-
|
|
229712
|
-
|
|
229713
|
-
|
|
229714
|
-
|
|
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
|
-
|
|
229772
|
-
|
|
229773
|
-
|
|
229774
|
-
|
|
229775
|
-
|
|
229776
|
-
|
|
229777
|
-
|
|
229778
|
-
|
|
229779
|
-
|
|
229780
|
-
|
|
229781
|
-
|
|
229782
|
-
|
|
229783
|
-
|
|
229784
|
-
|
|
229785
|
-
|
|
229786
|
-
|
|
229787
|
-
|
|
229788
|
-
|
|
229789
|
-
|
|
229790
|
-
|
|
229791
|
-
|
|
229792
|
-
|
|
229793
|
-
|
|
229794
|
-
|
|
229795
|
-
|
|
229796
|
-
|
|
229797
|
-
|
|
229798
|
-
|
|
229799
|
-
|
|
229800
|
-
|
|
229801
|
-
|
|
229802
|
-
|
|
229803
|
-
|
|
229804
|
-
|
|
229805
|
-
|
|
229806
|
-
|
|
229807
|
-
|
|
229808
|
-
|
|
229809
|
-
|
|
229810
|
-
|
|
229811
|
-
|
|
229812
|
-
|
|
229813
|
-
|
|
229814
|
-
|
|
229815
|
-
|
|
229816
|
-
|
|
229817
|
-
|
|
229818
|
-
|
|
229819
|
-
|
|
229820
|
-
|
|
229821
|
-
|
|
229822
|
-
|
|
229823
|
-
|
|
229824
|
-
|
|
229825
|
-
|
|
229826
|
-
|
|
229827
|
-
|
|
229828
|
-
|
|
229829
|
-
|
|
229830
|
-
|
|
229831
|
-
|
|
229832
|
-
|
|
229833
|
-
|
|
229834
|
-
|
|
229588
|
+
const startedAt = Date.now();
|
|
229589
|
+
const originalHistory = [...this.agent.context.history];
|
|
229590
|
+
const tokensBefore = estimateTokensForMessages(originalHistory);
|
|
229591
|
+
let retryCount = 0;
|
|
229592
|
+
try {
|
|
229593
|
+
await this.triggerPreCompactHook(data, tokensBefore, signal);
|
|
229594
|
+
const model = resolveCompactionModelAlias(this.agent.blunConfig?.loopControl?.compactionModel, this.agent.activeResponderModel ?? this.agent.config.model);
|
|
229595
|
+
const runtimeModel = this.agent.config.runtimeModel(model);
|
|
229596
|
+
const capability = runtimeModel.modelCapabilities;
|
|
229597
|
+
const buildCompactionProvider = (usedContextTokens) => {
|
|
229598
|
+
const currentMaxContextTokens = this.getEffectiveMaxContextTokens(model);
|
|
229599
|
+
const defaultCompactionCap = currentMaxContextTokens > 0 ? Math.min(currentMaxContextTokens, DEFAULT_COMPACTION_MAX_COMPLETION_TOKENS) : void 0;
|
|
229600
|
+
const requestCapability = currentMaxContextTokens > 0 ? {
|
|
229601
|
+
...capability,
|
|
229602
|
+
max_context_tokens: currentMaxContextTokens
|
|
229603
|
+
} : capability;
|
|
229604
|
+
return applyCompletionBudget({
|
|
229605
|
+
provider: runtimeModel.provider,
|
|
229606
|
+
budget: resolveCompletionBudget({
|
|
229607
|
+
maxOutputSize: runtimeModel.maxOutputSize ?? defaultCompactionCap,
|
|
229608
|
+
reservedContextSize: this.agent.blunConfig?.loopControl?.reservedContextSize
|
|
229609
|
+
}),
|
|
229610
|
+
capability: requestCapability,
|
|
229611
|
+
usedContextTokens
|
|
229612
|
+
});
|
|
229613
|
+
};
|
|
229614
|
+
let provider = buildCompactionProvider();
|
|
229615
|
+
const instruction = this.buildInstruction(data.instruction);
|
|
229616
|
+
const delays = retryBackoffDelays(5);
|
|
229617
|
+
let usage = null;
|
|
229618
|
+
let summary;
|
|
229619
|
+
let historyForModel = originalHistory.filter((message) => {
|
|
229620
|
+
const origin = message.origin;
|
|
229621
|
+
return !(origin?.kind === "injection" && REBUILT_AFTER_COMPACTION_INJECTION_VARIANTS.has(origin.variant));
|
|
229622
|
+
});
|
|
229623
|
+
let droppedCount = 0;
|
|
229624
|
+
let overflowShrinkCount = 0;
|
|
229625
|
+
let incompleteSummaryAttempts = 0;
|
|
229626
|
+
let hierarchicalPassCount = 0;
|
|
229627
|
+
const typicalDurationMs = readTypicalCompactionDuration(provider.name, provider.modelName, this.agent.blunHomeDir)?.typicalDurationMs;
|
|
229628
|
+
let attemptCount = 0;
|
|
229629
|
+
let charsReceived = 0;
|
|
229630
|
+
let lastProgressEmitAt = 0;
|
|
229631
|
+
let initialCompactionRequestTokens;
|
|
229632
|
+
const compactionTools = [];
|
|
229633
|
+
const buildRequestMessages = (history, requestInstruction) => {
|
|
229634
|
+
return stripMediaForCompaction(downgradeUnsupportedMedia([...this.agent.context.project(history, {
|
|
229635
|
+
synthesizeMissing: true,
|
|
229636
|
+
dropOrphanResults: true
|
|
229637
|
+
}), createUserMessage(requestInstruction)], capability));
|
|
229638
|
+
};
|
|
229639
|
+
while (true) {
|
|
229640
|
+
signal.throwIfAborted();
|
|
229641
|
+
if (hierarchicalPassCount >= MAX_HIERARCHICAL_COMPACTION_PASSES)
|
|
229642
|
+
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: {
|
|
229643
|
+
contextUnchanged: true,
|
|
229644
|
+
hierarchicalPassCount
|
|
229645
|
+
} });
|
|
229646
|
+
const compactionRequestLimit = this.getEffectiveMaxContextTokens(model);
|
|
229647
|
+
const safeCompactionRequestLimit = compactionRequestLimit > 0 ? Math.max(1, Math.floor(compactionRequestLimit * (1 - COMPACTION_SUMMARY_RESERVE_RATIO))) : compactionRequestLimit;
|
|
229648
|
+
let messages = buildRequestMessages(historyForModel, instruction);
|
|
229649
|
+
let estimatedCompactionRequestTokens = this.estimateRequestTokens(messages, this.agent.effectiveSystemPrompt, compactionTools);
|
|
229650
|
+
initialCompactionRequestTokens ??= estimatedCompactionRequestTokens;
|
|
229651
|
+
let hierarchicalChunkEnd;
|
|
229652
|
+
if (safeCompactionRequestLimit > 0 && estimatedCompactionRequestTokens >= safeCompactionRequestLimit) {
|
|
229653
|
+
if (historyForModel.length > 1) {
|
|
229654
|
+
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.`;
|
|
229655
|
+
const chunk = selectHierarchicalCompactionChunk(historyForModel, safeCompactionRequestLimit, safeCompactionRequestLimit, (candidate) => {
|
|
229656
|
+
const candidateMessages = buildRequestMessages(candidate, chunkInstruction);
|
|
229657
|
+
return {
|
|
229658
|
+
messages: candidateMessages,
|
|
229659
|
+
estimatedTokens: this.estimateRequestTokens(candidateMessages, this.agent.effectiveSystemPrompt, compactionTools)
|
|
229660
|
+
};
|
|
229661
|
+
});
|
|
229662
|
+
if (chunk !== void 0) {
|
|
229663
|
+
hierarchicalChunkEnd = chunk.end;
|
|
229664
|
+
messages = chunk.messages;
|
|
229665
|
+
estimatedCompactionRequestTokens = chunk.estimatedTokens;
|
|
229666
|
+
}
|
|
229667
|
+
}
|
|
229668
|
+
if (hierarchicalChunkEnd === void 0)
|
|
229669
|
+
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: {
|
|
229670
|
+
contextBrakeBlocked: true,
|
|
229671
|
+
estimatedRequestTokens: estimatedCompactionRequestTokens,
|
|
229672
|
+
maxContextTokens: compactionRequestLimit,
|
|
229673
|
+
requestBudgetTokens: safeCompactionRequestLimit,
|
|
229674
|
+
summaryReserveTokens: compactionRequestLimit - safeCompactionRequestLimit,
|
|
229675
|
+
contextUnchanged: true
|
|
229676
|
+
} });
|
|
229677
|
+
}
|
|
229678
|
+
provider = buildCompactionProvider(estimatedCompactionRequestTokens);
|
|
229679
|
+
attemptCount += 1;
|
|
229680
|
+
charsReceived = 0;
|
|
229681
|
+
const stage = hierarchicalPassCount + 1;
|
|
229682
|
+
const estimatedStageCount = estimateCompactionStageCount(initialCompactionRequestTokens, safeCompactionRequestLimit, stage);
|
|
229683
|
+
const estimatedProgressPercent = estimateCompactionProgressPercent(stage, estimatedStageCount);
|
|
229684
|
+
const windowUsagePercent = estimateCompactionWindowUsagePercent(estimatedCompactionRequestTokens, compactionRequestLimit);
|
|
229685
|
+
this.agent.log.info("compaction stage request", {
|
|
229686
|
+
source: data.source,
|
|
229687
|
+
stage,
|
|
229688
|
+
estimatedStageCount,
|
|
229689
|
+
activeContextTokens: this.estimateProjectedRequestTokens(),
|
|
229690
|
+
estimatedInputTokens: estimatedCompactionRequestTokens,
|
|
229691
|
+
windowUsagePercent,
|
|
229692
|
+
safeInputLimitTokens: safeCompactionRequestLimit,
|
|
229693
|
+
maxContextTokens: compactionRequestLimit
|
|
229694
|
+
});
|
|
229695
|
+
const emitCompactionProgress = (force = false) => {
|
|
229696
|
+
const now = Date.now();
|
|
229697
|
+
if (!force && now - lastProgressEmitAt < 150)
|
|
229698
|
+
return;
|
|
229699
|
+
lastProgressEmitAt = now;
|
|
229700
|
+
this.agent.emitEvent({
|
|
229701
|
+
type: "compaction.progress",
|
|
229702
|
+
stage,
|
|
229703
|
+
estimatedStageCount,
|
|
229704
|
+
estimatedInputTokens: estimatedCompactionRequestTokens,
|
|
229705
|
+
estimatedProgressPercent,
|
|
229706
|
+
...windowUsagePercent === void 0 ? {} : { windowUsagePercent },
|
|
229707
|
+
charsReceived,
|
|
229708
|
+
attempt: attemptCount,
|
|
229709
|
+
...typicalDurationMs !== void 0 ? { typicalDurationMs } : {}
|
|
229710
|
+
});
|
|
229711
|
+
};
|
|
229712
|
+
emitCompactionProgress(true);
|
|
229713
|
+
const timingKey = compactionTimingKey(provider);
|
|
229714
|
+
const stallPolicy = this.measuredStallPolicy(timingKey);
|
|
229715
|
+
let lastProgressAt = Date.now();
|
|
229716
|
+
let maxObservedIdleMs = 0;
|
|
229717
|
+
const attemptController = new AbortController();
|
|
229718
|
+
const onParentAbort = () => {
|
|
229719
|
+
attemptController.abort();
|
|
229720
|
+
};
|
|
229721
|
+
signal.addEventListener("abort", onParentAbort, { once: true });
|
|
229722
|
+
let stalled = false;
|
|
229723
|
+
let stallTimer;
|
|
229724
|
+
const armStallTimer = () => {
|
|
229725
|
+
if (stallPolicy === void 0)
|
|
229726
|
+
return;
|
|
229727
|
+
if (stallTimer !== void 0)
|
|
229728
|
+
clearTimeout(stallTimer);
|
|
229729
|
+
stallTimer = setTimeout(() => {
|
|
229730
|
+
stalled = true;
|
|
229731
|
+
attemptController.abort();
|
|
229732
|
+
}, stallPolicy.timeoutMs);
|
|
229733
|
+
stallTimer.unref?.();
|
|
229734
|
+
};
|
|
229735
|
+
const observeProgress = (part) => {
|
|
229736
|
+
const now = Date.now();
|
|
229737
|
+
maxObservedIdleMs = Math.max(maxObservedIdleMs, now - lastProgressAt);
|
|
229738
|
+
lastProgressAt = now;
|
|
229739
|
+
if (part.type === "text")
|
|
229740
|
+
charsReceived += part.text.length;
|
|
229741
|
+
emitCompactionProgress();
|
|
229742
|
+
armStallTimer();
|
|
229743
|
+
};
|
|
229744
|
+
armStallTimer();
|
|
229745
|
+
try {
|
|
229746
|
+
const response = await this.agent.generate(provider, this.agent.config.systemPrompt, compactionTools, messages, { onMessagePart: observeProgress }, {
|
|
229747
|
+
signal: attemptController.signal,
|
|
229748
|
+
isCompactionRequest: true,
|
|
229749
|
+
onExecution: (completion) => {
|
|
229750
|
+
this.executions.track({
|
|
229751
|
+
kind: "provider",
|
|
229752
|
+
id: executionId,
|
|
229753
|
+
source: data.source
|
|
229754
|
+
}, completion);
|
|
229755
|
+
}
|
|
229756
|
+
});
|
|
229757
|
+
signal.throwIfAborted();
|
|
229758
|
+
if (stalled && !signal.aborted && stallPolicy !== void 0)
|
|
229759
|
+
throw new CompactionStallError(stallPolicy.timeoutMs, stallPolicy.measuredIdleMs);
|
|
229760
|
+
maxObservedIdleMs = Math.max(maxObservedIdleMs, Date.now() - lastProgressAt);
|
|
229761
|
+
if (response.finishReason === "truncated")
|
|
229762
|
+
throw new CompactionTruncatedError();
|
|
229763
|
+
if (response.usage !== null)
|
|
229764
|
+
usage = usage === null ? response.usage : addUsage(usage, response.usage);
|
|
229765
|
+
const extractedSummary = extractCompactionSummary(response);
|
|
229766
|
+
this.observeCompactionTiming(timingKey, maxObservedIdleMs);
|
|
229767
|
+
if (hierarchicalChunkEnd !== void 0) {
|
|
229768
|
+
hierarchicalPassCount += 1;
|
|
229769
|
+
const previousTokens = estimateTokensForMessages(historyForModel);
|
|
229770
|
+
const nextHistory = [createUserMessage(`${HIERARCHICAL_COMPACTION_PREFIX}\n${extractedSummary}`), ...historyForModel.slice(hierarchicalChunkEnd)];
|
|
229771
|
+
const nextTokens = estimateTokensForMessages(nextHistory);
|
|
229772
|
+
if (nextTokens >= previousTokens)
|
|
229773
|
+
throw new BlunError(ErrorCodes.COMPACTION_FAILED, "A staged compaction response did not reduce the pending history. The conversation history was not changed.", { details: {
|
|
229774
|
+
contextUnchanged: true,
|
|
229775
|
+
hierarchicalPassCount,
|
|
229776
|
+
previousTokens,
|
|
229777
|
+
nextTokens
|
|
229778
|
+
} });
|
|
229779
|
+
historyForModel = nextHistory;
|
|
229780
|
+
incompleteSummaryAttempts = 0;
|
|
229781
|
+
retryCount = 0;
|
|
229782
|
+
continue;
|
|
229783
|
+
}
|
|
229784
|
+
summary = extractedSummary;
|
|
229785
|
+
appendCompactionTiming({
|
|
229786
|
+
ts: Date.now(),
|
|
229787
|
+
provider: provider.name,
|
|
229788
|
+
model: provider.modelName,
|
|
229789
|
+
estimatedInputTokens: tokensBefore,
|
|
229790
|
+
durationMs: Date.now() - startedAt
|
|
229791
|
+
}, this.agent.blunHomeDir);
|
|
229792
|
+
break;
|
|
229793
|
+
}
|
|
229794
|
+
catch (error) {
|
|
229795
|
+
if (stalled && !signal.aborted && stallPolicy !== void 0)
|
|
229796
|
+
throw new CompactionStallError(stallPolicy.timeoutMs, stallPolicy.measuredIdleMs);
|
|
229797
|
+
if (isModelFallbackStatus(error))
|
|
229798
|
+
throw error;
|
|
229799
|
+
const isContextOverflow = this.shouldRecoverFromContextOverflow(error);
|
|
229800
|
+
if (isContextOverflow)
|
|
229801
|
+
this.observeContextOverflow(error, model);
|
|
229802
|
+
if (isContextOverflow && historyForModel.length > 1) {
|
|
229803
|
+
if (data.source === "auto") {
|
|
229804
|
+
const learnedMaxContextTokens = this.getEffectiveMaxContextTokens(model);
|
|
229805
|
+
if (learnedMaxContextTokens > 0 && learnedMaxContextTokens < compactionRequestLimit) {
|
|
229806
|
+
retryCount = 0;
|
|
229807
|
+
continue;
|
|
229808
|
+
}
|
|
229809
|
+
throw new BlunError(ErrorCodes.CONTEXT_OVERFLOW, "The active model rejected the full compaction request; refusing to drop unsummarized history.", {
|
|
229810
|
+
cause: error,
|
|
229811
|
+
details: {
|
|
229812
|
+
contextBrakeBlocked: true,
|
|
229813
|
+
estimatedRequestTokens: estimatedCompactionRequestTokens,
|
|
229814
|
+
maxContextTokens: learnedMaxContextTokens,
|
|
229815
|
+
requestBudgetTokens: learnedMaxContextTokens,
|
|
229816
|
+
contextUnchanged: true
|
|
229817
|
+
}
|
|
229818
|
+
});
|
|
229819
|
+
}
|
|
229820
|
+
overflowShrinkCount += 1;
|
|
229821
|
+
if (overflowShrinkCount > MAX_COMPACTION_OVERFLOW_SHRINK_ATTEMPTS)
|
|
229822
|
+
throw error;
|
|
229823
|
+
const before = historyForModel.length;
|
|
229824
|
+
historyForModel = shrinkCompactionHistoryAfterOverflow(historyForModel, overflowShrinkCount);
|
|
229825
|
+
droppedCount += before - historyForModel.length;
|
|
229826
|
+
retryCount = 0;
|
|
229827
|
+
continue;
|
|
229828
|
+
}
|
|
229829
|
+
if (error instanceof CompactionTruncatedError || error instanceof APIEmptyResponseError) {
|
|
229830
|
+
incompleteSummaryAttempts += 1;
|
|
229831
|
+
if (incompleteSummaryAttempts === 1)
|
|
229832
|
+
continue;
|
|
229833
|
+
throw new BlunError(ErrorCodes.COMPACTION_FAILED, "The provider returned an empty or truncated compaction response twice. The conversation history was not changed.", {
|
|
229834
|
+
cause: error,
|
|
229835
|
+
details: {
|
|
229836
|
+
contextUnchanged: true,
|
|
229837
|
+
lossyRetryPrevented: true,
|
|
229838
|
+
compactionFailure: error.name,
|
|
229839
|
+
incompleteSummaryAttempts
|
|
229840
|
+
}
|
|
229841
|
+
});
|
|
229842
|
+
}
|
|
229843
|
+
if (!isRetryableGenerateError(error))
|
|
229844
|
+
throw error;
|
|
229845
|
+
if (retryCount + 1 >= 5)
|
|
229846
|
+
throw error;
|
|
229847
|
+
await sleepForRetry(delays[retryCount], signal);
|
|
229848
|
+
retryCount += 1;
|
|
229849
|
+
}
|
|
229850
|
+
finally {
|
|
229851
|
+
if (stallTimer !== void 0)
|
|
229852
|
+
clearTimeout(stallTimer);
|
|
229853
|
+
signal.removeEventListener("abort", onParentAbort);
|
|
229854
|
+
}
|
|
229855
|
+
}
|
|
229856
|
+
if (usage !== null)
|
|
229857
|
+
this.agent.usage.record(model, usage);
|
|
229858
|
+
const newHistory = this.agent.context.history;
|
|
229859
|
+
for (let i = 0; i < originalHistory.length; i++)
|
|
229860
|
+
if (newHistory[i] !== originalHistory[i]) {
|
|
229861
|
+
this.cancel();
|
|
229862
|
+
return;
|
|
229863
|
+
}
|
|
229864
|
+
if (newHistory.slice(originalHistory.length).some((message) => !isRealUserInput(message))) {
|
|
229865
|
+
this.cancel();
|
|
229866
|
+
return;
|
|
229867
|
+
}
|
|
229868
|
+
const rawSummary = this.postProcessSummary(summary ?? "");
|
|
229869
|
+
const contextSummary = buildCompactionSummaryText(rawSummary);
|
|
229870
|
+
const result = this.agent.context.applyCompaction({
|
|
229871
|
+
summary: rawSummary,
|
|
229872
|
+
contextSummary,
|
|
229873
|
+
compactedCount: originalHistory.length,
|
|
229874
|
+
tokensBefore,
|
|
229875
|
+
droppedCount: droppedCount === 0 ? void 0 : droppedCount
|
|
229876
|
+
});
|
|
229877
|
+
this.agent.telemetry.track("compaction_finished", {
|
|
229878
|
+
source: data.source,
|
|
229879
|
+
tokens_before: result.tokensBefore,
|
|
229880
|
+
tokens_after: result.tokensAfter,
|
|
229881
|
+
duration_ms: Date.now() - startedAt,
|
|
229882
|
+
compacted_count: result.compactedCount,
|
|
229883
|
+
dropped_count: result.droppedCount,
|
|
229884
|
+
retry_count: retryCount,
|
|
229885
|
+
round: 1,
|
|
229886
|
+
thinking_effort: this.agent.config.thinkingEffort,
|
|
229887
|
+
...usage === null ? {} : {
|
|
229888
|
+
input_tokens: inputTotal(usage),
|
|
229889
|
+
output_tokens: usage.output
|
|
229890
|
+
}
|
|
229891
|
+
});
|
|
229892
|
+
return {
|
|
229893
|
+
result,
|
|
229894
|
+
stageCount: hierarchicalPassCount + 1
|
|
229895
|
+
};
|
|
229896
|
+
}
|
|
229897
|
+
catch (error) {
|
|
229898
|
+
if (isAbortError(error) || signal.aborted)
|
|
229899
|
+
return void 0;
|
|
229900
|
+
this.agent.telemetry.track("compaction_failed", {
|
|
229901
|
+
source: data.source,
|
|
229902
|
+
tokens_before: tokensBefore,
|
|
229903
|
+
duration_ms: Date.now() - startedAt,
|
|
229904
|
+
round: 1,
|
|
229905
|
+
retry_count: retryCount,
|
|
229906
|
+
thinking_effort: this.agent.config.thinkingEffort,
|
|
229907
|
+
error_type: error instanceof Error ? error.name : "Unknown"
|
|
229908
|
+
});
|
|
229909
|
+
if (isBlunError(error) && (error.code === ErrorCodes.AUTH_LOGIN_REQUIRED || error.code === ErrorCodes.COMPACTION_FAILED))
|
|
229910
|
+
throw error;
|
|
229911
|
+
if (isBlunError(error) && error.code === ErrorCodes.CONTEXT_OVERFLOW && error.details?.["contextBrakeBlocked"] === true)
|
|
229912
|
+
throw error;
|
|
229913
|
+
if (isModelFallbackStatus(error))
|
|
229914
|
+
throw error;
|
|
229915
|
+
if (error instanceof CompactionStallError)
|
|
229916
|
+
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.`, {
|
|
229917
|
+
cause: error,
|
|
229918
|
+
details: {
|
|
229919
|
+
reason: "no_provider_data",
|
|
229920
|
+
timeoutSource: "measured_provider_behavior",
|
|
229921
|
+
measuredIdleMs: error.measuredIdleMs,
|
|
229922
|
+
timeoutMs: error.timeoutMs,
|
|
229923
|
+
contextUnchanged: true
|
|
229924
|
+
}
|
|
229925
|
+
});
|
|
229926
|
+
if (error instanceof APIStatusError && error.statusCode === 402)
|
|
229927
|
+
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.", {
|
|
229928
|
+
cause: error,
|
|
229929
|
+
details: {
|
|
229930
|
+
statusCode: error.statusCode,
|
|
229931
|
+
requestId: error.requestId,
|
|
229932
|
+
action: "switch_model_and_retry",
|
|
229933
|
+
contextUnchanged: true
|
|
229934
|
+
}
|
|
229935
|
+
});
|
|
229936
|
+
if (error instanceof APIStatusError && error.statusCode === 408)
|
|
229937
|
+
throw new BlunError(ErrorCodes.COMPACTION_FAILED, `Compaction stopped: the conversation (${String(tokensBefore)} tokens) could not be sent completely. The conversation history was not changed.`, {
|
|
229938
|
+
cause: error,
|
|
229939
|
+
details: {
|
|
229940
|
+
statusCode: error.statusCode,
|
|
229941
|
+
requestId: error.requestId,
|
|
229942
|
+
estimatedHistoryTokens: tokensBefore,
|
|
229943
|
+
contextUnchanged: true
|
|
229944
|
+
}
|
|
229945
|
+
});
|
|
229946
|
+
throw new BlunError(ErrorCodes.COMPACTION_FAILED, String(error), { cause: error });
|
|
229947
|
+
}
|
|
229948
|
+
}
|
|
229835
229949
|
measuredStallPolicy(_timingKey) {}
|
|
229836
229950
|
observeCompactionTiming(timingKey, maxIdleMs) {
|
|
229837
229951
|
if (!Number.isFinite(maxIdleMs) || maxIdleMs <= 0) return;
|
|
@@ -269642,54 +269756,67 @@ var Agent$1 = class {
|
|
|
269642
269756
|
return this.generateForModel(this.activeResponderModel ?? this.config.modelAlias);
|
|
269643
269757
|
}
|
|
269644
269758
|
generateForModel(modelAlias) {
|
|
269645
|
-
|
|
269646
|
-
|
|
269647
|
-
|
|
269648
|
-
|
|
269649
|
-
|
|
269650
|
-
|
|
269651
|
-
|
|
269652
|
-
|
|
269653
|
-
|
|
269654
|
-
|
|
269655
|
-
|
|
269656
|
-
|
|
269657
|
-
|
|
269658
|
-
|
|
269659
|
-
|
|
269660
|
-
|
|
269661
|
-
|
|
269662
|
-
|
|
269663
|
-
|
|
269664
|
-
|
|
269665
|
-
|
|
269666
|
-
|
|
269667
|
-
|
|
269668
|
-
|
|
269669
|
-
|
|
269670
|
-
|
|
269671
|
-
|
|
269672
|
-
|
|
269673
|
-
|
|
269674
|
-
|
|
269675
|
-
|
|
269676
|
-
|
|
269677
|
-
|
|
269678
|
-
|
|
269679
|
-
|
|
269680
|
-
|
|
269681
|
-
|
|
269682
|
-
|
|
269683
|
-
|
|
269684
|
-
|
|
269685
|
-
|
|
269686
|
-
|
|
269687
|
-
|
|
269688
|
-
|
|
269689
|
-
|
|
269690
|
-
|
|
269691
|
-
|
|
269692
|
-
|
|
269759
|
+
return async (provider, systemPrompt, tools, history, callbacks, options) => {
|
|
269760
|
+
const { requestLogFields, generateOptions } = splitGenerateOptions(options);
|
|
269761
|
+
const retryScope = providerRetryScope(modelAlias, provider);
|
|
269762
|
+
await this.providerRetryWait.wait(retryScope, generateOptions?.signal);
|
|
269763
|
+
const callerTransportAttempt = generateOptions?.onTransportAttempt;
|
|
269764
|
+
const ownedGenerateOptions = {
|
|
269765
|
+
...generateOptions,
|
|
269766
|
+
transportMaxRetries: 0,
|
|
269767
|
+
requestBodyBudget: {
|
|
269768
|
+
...generateOptions?.requestBodyBudget,
|
|
269769
|
+
onMeasured: (bytes, limit, endpoint) => {
|
|
269770
|
+
generateOptions?.requestBodyBudget?.onMeasured?.(bytes, limit, endpoint);
|
|
269771
|
+
if (!generateOptions?.isCompactionRequest && limit !== undefined && this.fullCompaction.shouldCompactRequestBody(endpoint, bytes, limit))
|
|
269772
|
+
throw t3458Pressure(bytes, limit, endpoint);
|
|
269773
|
+
}
|
|
269774
|
+
},
|
|
269775
|
+
onTransportAttempt: (stats) => {
|
|
269776
|
+
callerTransportAttempt?.(stats);
|
|
269777
|
+
this.llmRequestLogger.logTransportAttempt(stats, requestLogFields);
|
|
269778
|
+
}
|
|
269779
|
+
};
|
|
269780
|
+
const run = async (requestOptions) => {
|
|
269781
|
+
let requestStarted = false;
|
|
269782
|
+
this.llmRequestLogger.logRequest({
|
|
269783
|
+
provider,
|
|
269784
|
+
modelAlias,
|
|
269785
|
+
systemPrompt,
|
|
269786
|
+
tools,
|
|
269787
|
+
messages: history,
|
|
269788
|
+
fields: requestLogFields
|
|
269789
|
+
});
|
|
269790
|
+
try {
|
|
269791
|
+
return await this.rawGenerate(provider, systemPrompt, tools, history, callbacks, {
|
|
269792
|
+
...requestOptions,
|
|
269793
|
+
onRequestStart: () => {
|
|
269794
|
+
requestOptions?.onRequestStart?.();
|
|
269795
|
+
requestStarted = true;
|
|
269796
|
+
}
|
|
269797
|
+
});
|
|
269798
|
+
}
|
|
269799
|
+
catch (error) {
|
|
269800
|
+
if (error instanceof APIProviderRateLimitError && error.retryAfterMs !== null)
|
|
269801
|
+
await this.providerRetryWait.defer(retryScope, error.retryAfterMs);
|
|
269802
|
+
if (requestStarted && requestOptions?.signal?.aborted !== true)
|
|
269803
|
+
this.turn.noteProviderRequestFailure(requestLogFields?.turnStep, error);
|
|
269804
|
+
throw error;
|
|
269805
|
+
}
|
|
269806
|
+
};
|
|
269807
|
+
if (ownedGenerateOptions.auth !== void 0)
|
|
269808
|
+
return run(ownedGenerateOptions);
|
|
269809
|
+
const withAuth = modelAlias === void 0 ? void 0 : this.modelProvider?.resolveAuth?.(modelAlias, { log: this.log });
|
|
269810
|
+
if (withAuth === void 0)
|
|
269811
|
+
return run(ownedGenerateOptions);
|
|
269812
|
+
return withAuth((auth) => {
|
|
269813
|
+
return run({
|
|
269814
|
+
...ownedGenerateOptions,
|
|
269815
|
+
auth
|
|
269816
|
+
});
|
|
269817
|
+
});
|
|
269818
|
+
};
|
|
269819
|
+
}
|
|
269693
269820
|
get llm() {
|
|
269694
269821
|
return this.llmForTurn();
|
|
269695
269822
|
}
|
|
@@ -300629,30 +300756,36 @@ var ProviderManager = class {
|
|
|
300629
300756
|
return typeof config === "function" ? config() : config;
|
|
300630
300757
|
}
|
|
300631
300758
|
resolveProviderConfig(model) {
|
|
300632
|
-
|
|
300633
|
-
|
|
300634
|
-
|
|
300635
|
-
|
|
300636
|
-
|
|
300637
|
-
|
|
300638
|
-
|
|
300639
|
-
|
|
300640
|
-
|
|
300641
|
-
|
|
300642
|
-
|
|
300643
|
-
|
|
300644
|
-
|
|
300645
|
-
|
|
300646
|
-
|
|
300647
|
-
|
|
300648
|
-
|
|
300649
|
-
|
|
300650
|
-
|
|
300651
|
-
|
|
300652
|
-
|
|
300653
|
-
|
|
300654
|
-
|
|
300655
|
-
|
|
300759
|
+
const alias = this.config.models?.[model];
|
|
300760
|
+
if (alias === void 0)
|
|
300761
|
+
throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" is not configured in config.toml. Add a [models."${model}"] entry with max_context_size.`);
|
|
300762
|
+
const effectiveAlias = effectiveModelAlias(alias);
|
|
300763
|
+
const providerName = alias.provider ?? this.config.defaultProvider;
|
|
300764
|
+
if (providerName === void 0)
|
|
300765
|
+
throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a provider in config.toml.`);
|
|
300766
|
+
const providerConfig = this.config.providers[providerName];
|
|
300767
|
+
if (providerConfig === void 0)
|
|
300768
|
+
throw new BlunError(ErrorCodes.CONFIG_INVALID, `Provider "${providerName}" for model "${model}" is not configured.`);
|
|
300769
|
+
assertBlunProviderType(providerConfig);
|
|
300770
|
+
const runtimeContext = this.runtimeModelContexts.get(model);
|
|
300771
|
+
const maxContextSize = runtimeContext?.providerName === providerName && runtimeContext.model === alias.model ? runtimeContext.maxContextTokens : effectiveAlias.maxContextSize;
|
|
300772
|
+
if (!Number.isInteger(maxContextSize) || maxContextSize <= 0)
|
|
300773
|
+
throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a positive max_context_size in config.toml.`);
|
|
300774
|
+
const provider = toKosongProviderConfig(providerConfig, alias.model, this.options.blunRequestHeaders, this.options.promptCacheKey, effectiveAlias.supportEfforts);
|
|
300775
|
+
if (this.options.configPath !== undefined)
|
|
300776
|
+
provider.requestBodyByteLimitStore = t3458Store(this.options.configPath, parse$6, stringify$1, atomicWritePrivate);
|
|
300777
|
+
return {
|
|
300778
|
+
providerName,
|
|
300779
|
+
provider,
|
|
300780
|
+
modelCapabilities: resolveModelCapabilities({
|
|
300781
|
+
...effectiveAlias,
|
|
300782
|
+
maxContextSize
|
|
300783
|
+
}, provider),
|
|
300784
|
+
alwaysThinking: (effectiveAlias.capabilities ?? []).some((c) => c.trim().toLowerCase() === "always_thinking"),
|
|
300785
|
+
maxOutputSize: effectiveAlias.maxOutputSize,
|
|
300786
|
+
type: "blun"
|
|
300787
|
+
};
|
|
300788
|
+
}
|
|
300656
300789
|
setRuntimeModelContext(input) {
|
|
300657
300790
|
if (!MANAGED_BLUN_MODEL_ALIASES.has(input.modelAlias)) throw new BlunError(ErrorCodes.CONFIG_INVALID, "Runtime context metadata is supported only for internal BLUN model aliases.");
|
|
300658
300791
|
if (!Number.isSafeInteger(input.maxContextTokens) || input.maxContextTokens <= 0) throw new BlunError(ErrorCodes.CONFIG_INVALID, "Runtime model context must be a positive integer.");
|
|
@@ -336332,14 +336465,15 @@ var BlunCore = class {
|
|
|
336332
336465
|
};
|
|
336333
336466
|
}
|
|
336334
336467
|
resolveProviderManager(sessionId) {
|
|
336335
|
-
|
|
336336
|
-
|
|
336337
|
-
|
|
336338
|
-
|
|
336339
|
-
|
|
336340
|
-
|
|
336341
|
-
|
|
336342
|
-
|
|
336468
|
+
return new ProviderManager({
|
|
336469
|
+
config: () => this.config,
|
|
336470
|
+
configPath: this.configPath,
|
|
336471
|
+
blunRequestHeaders: this.blunRequestHeaders,
|
|
336472
|
+
resolveOAuthTokenProvider: this.resolveOAuthTokenProvider,
|
|
336473
|
+
managedApiKeyRevocationStore: this.managedApiKeyRevocationStore,
|
|
336474
|
+
promptCacheKey: sessionId
|
|
336475
|
+
});
|
|
336476
|
+
}
|
|
336343
336477
|
mergePluginMcpConfig(base) {
|
|
336344
336478
|
const pluginEntries = this.plugins.mcpServerConfigs();
|
|
336345
336479
|
if (pluginEntries.length === 0) return base;
|
|
@@ -350032,7 +350166,7 @@ function optionalBuildString(value) {
|
|
|
350032
350166
|
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
350033
350167
|
}
|
|
350034
350168
|
const BLUN_BUILD_INFO = {
|
|
350035
|
-
version: optionalBuildString("9.1.
|
|
350169
|
+
version: optionalBuildString("9.1.640"),
|
|
350036
350170
|
channel: typeof __BLUN_CHANNEL__ === "string" ? optionalBuildString(__BLUN_CHANNEL__) : void 0,
|
|
350037
350171
|
commit: typeof __BLUN_COMMIT__ === "string" ? optionalBuildString(__BLUN_COMMIT__) : void 0,
|
|
350038
350172
|
buildTarget: typeof __BLUN_BUILD_TARGET__ === "string" ? optionalBuildString(__BLUN_BUILD_TARGET__) : void 0
|
|
@@ -350084,3 +350218,169 @@ function createKingWorkerHarness(options) {
|
|
|
350084
350218
|
});
|
|
350085
350219
|
}
|
|
350086
350220
|
export { LocalKaos, createKingWorkerHarness };
|
|
350221
|
+
|
|
350222
|
+
import * as t3458Fs from "node:fs/promises";
|
|
350223
|
+
import * as t3458Path from "node:path";
|
|
350224
|
+
import * as t3458Crypto from "node:crypto";
|
|
350225
|
+
// Neue, selektive Verdichtungshelfer. Keine Quellübernahme aus Arbeitsbaum oder v2/v3.
|
|
350226
|
+
function t3458ValidLimit(value) {
|
|
350227
|
+
return Number.isSafeInteger(value) && value > 0 ? value : undefined;
|
|
350228
|
+
}
|
|
350229
|
+
function t3458Learned(value) {
|
|
350230
|
+
return t3458ValidLimit(value) !== undefined && value >= 65536 ? value : undefined;
|
|
350231
|
+
}
|
|
350232
|
+
function t3458Endpoint(baseUrl) {
|
|
350233
|
+
try {
|
|
350234
|
+
const url = new URL(baseUrl);
|
|
350235
|
+
if (!['https:', 'http:'].includes(url.protocol) || url.username || url.password || url.search || url.hash)
|
|
350236
|
+
return undefined;
|
|
350237
|
+
return `${url.origin}${url.pathname.replace(/\/+$/, '')}`;
|
|
350238
|
+
}
|
|
350239
|
+
catch {
|
|
350240
|
+
return undefined;
|
|
350241
|
+
}
|
|
350242
|
+
}
|
|
350243
|
+
function t3458Key(baseUrl) {
|
|
350244
|
+
const endpoint = t3458Endpoint(baseUrl);
|
|
350245
|
+
return endpoint === undefined ? undefined : t3458Crypto.createHash('sha256').update(endpoint).digest('hex');
|
|
350246
|
+
}
|
|
350247
|
+
async function t3458WithConfigLock(filePath, action, learning = false) {
|
|
350248
|
+
await t3458Fs.mkdir(t3458Path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
350249
|
+
const lock = `${filePath}.request-body.lock`;
|
|
350250
|
+
const token = t3458Crypto.randomUUID();
|
|
350251
|
+
// Small scheduling reserve: bounded contention attempts stay below 2s / 12s.
|
|
350252
|
+
const deadline = performance.now() + (learning ? 1950 : 11950);
|
|
350253
|
+
let handle;
|
|
350254
|
+
while (handle === undefined) {
|
|
350255
|
+
if (performance.now() >= deadline)
|
|
350256
|
+
return learning ? undefined : await action();
|
|
350257
|
+
try {
|
|
350258
|
+
handle = await t3458Fs.open(lock, 'wx', 0o600);
|
|
350259
|
+
}
|
|
350260
|
+
catch (error) {
|
|
350261
|
+
if (error.code !== 'EEXIST' && !(process.platform === 'win32' && ['EPERM', 'EACCES', 'EBUSY'].includes(error.code)))
|
|
350262
|
+
throw error;
|
|
350263
|
+
try {
|
|
350264
|
+
const observed = await t3458Fs.stat(lock);
|
|
350265
|
+
if (Date.now() - observed.mtimeMs > 10000) {
|
|
350266
|
+
const owner = await t3458Fs.readFile(lock, 'utf8');
|
|
350267
|
+
const current = await t3458Fs.stat(lock);
|
|
350268
|
+
// Best effort only: the accepted check/unlink race is not fencing.
|
|
350269
|
+
if (current.mtimeMs === observed.mtimeMs && await t3458Fs.readFile(lock, 'utf8') === owner) {
|
|
350270
|
+
await t3458Fs.unlink(lock);
|
|
350271
|
+
continue;
|
|
350272
|
+
}
|
|
350273
|
+
}
|
|
350274
|
+
}
|
|
350275
|
+
catch (failure) {
|
|
350276
|
+
if (failure.code !== 'ENOENT')
|
|
350277
|
+
throw failure;
|
|
350278
|
+
}
|
|
350279
|
+
const remaining = deadline - performance.now();
|
|
350280
|
+
if (remaining > 0)
|
|
350281
|
+
await new Promise(resolve => setTimeout(resolve, Math.min(25, remaining)));
|
|
350282
|
+
}
|
|
350283
|
+
}
|
|
350284
|
+
try {
|
|
350285
|
+
await handle.writeFile(token, 'utf8');
|
|
350286
|
+
return await action();
|
|
350287
|
+
}
|
|
350288
|
+
finally {
|
|
350289
|
+
await handle.close();
|
|
350290
|
+
try {
|
|
350291
|
+
if (await t3458Fs.readFile(lock, 'utf8') === token)
|
|
350292
|
+
await t3458Fs.unlink(lock);
|
|
350293
|
+
}
|
|
350294
|
+
catch (error) {
|
|
350295
|
+
if (error.code !== 'ENOENT')
|
|
350296
|
+
throw error;
|
|
350297
|
+
}
|
|
350298
|
+
}
|
|
350299
|
+
}
|
|
350300
|
+
function t3458Store(filePath, parseToml, stringifyToml, atomicWrite) {
|
|
350301
|
+
return {
|
|
350302
|
+
async load(key) {
|
|
350303
|
+
try {
|
|
350304
|
+
return t3458Learned(parseToml(await t3458Fs.readFile(filePath, 'utf8')).request_body_byte_limits?.[key]);
|
|
350305
|
+
}
|
|
350306
|
+
catch (error) {
|
|
350307
|
+
if (error.code === 'ENOENT')
|
|
350308
|
+
return undefined;
|
|
350309
|
+
throw error;
|
|
350310
|
+
}
|
|
350311
|
+
},
|
|
350312
|
+
async save(key, value) {
|
|
350313
|
+
if (!/^[a-f0-9]{64}$/.test(key) || t3458Learned(value) === undefined)
|
|
350314
|
+
return;
|
|
350315
|
+
await t3458WithConfigLock(filePath, async () => {
|
|
350316
|
+
let data;
|
|
350317
|
+
try {
|
|
350318
|
+
data = parseToml(await t3458Fs.readFile(filePath, 'utf8'));
|
|
350319
|
+
}
|
|
350320
|
+
catch (error) {
|
|
350321
|
+
if (error.code !== 'ENOENT')
|
|
350322
|
+
throw error;
|
|
350323
|
+
data = {};
|
|
350324
|
+
}
|
|
350325
|
+
const table = data.request_body_byte_limits ?? {};
|
|
350326
|
+
const previous = t3458Learned(table[key]);
|
|
350327
|
+
data.request_body_byte_limits = { ...table, [key]: previous === undefined ? value : Math.min(previous, value) };
|
|
350328
|
+
await atomicWrite(filePath, `${stringifyToml(data)}\n`);
|
|
350329
|
+
}, true);
|
|
350330
|
+
}
|
|
350331
|
+
};
|
|
350332
|
+
}
|
|
350333
|
+
async function t3458Measure(provider, params, options) {
|
|
350334
|
+
const endpoint = t3458Endpoint(provider._baseUrl);
|
|
350335
|
+
const key = t3458Key(provider._baseUrl);
|
|
350336
|
+
const option = typeof provider._requestBodyByteLimit === 'function' ? provider._requestBodyByteLimit() : provider._requestBodyByteLimit;
|
|
350337
|
+
const configured = t3458ValidLimit(options?.requestBodyBudget?.limitBytes) ?? t3458ValidLimit(option) ?? t3458ValidLimit(Number(process.env.BLUN_REQUEST_BODY_BYTE_LIMIT));
|
|
350338
|
+
let persisted;
|
|
350339
|
+
if (key !== undefined && provider._requestBodyByteLimitStore !== undefined) {
|
|
350340
|
+
try {
|
|
350341
|
+
persisted = t3458Learned(await provider._requestBodyByteLimitStore.load(key));
|
|
350342
|
+
}
|
|
350343
|
+
catch { }
|
|
350344
|
+
}
|
|
350345
|
+
const learned = t3458Learned(provider._t3458ObservedLimit);
|
|
350346
|
+
const observed = learned === undefined ? persisted : persisted === undefined ? learned : Math.min(learned, persisted);
|
|
350347
|
+
const fallback = endpoint === 'https://api.blun.ai/coding/v1' ? 500000 : undefined;
|
|
350348
|
+
const limit = configured === undefined ? observed ?? fallback : observed === undefined ? configured : Math.min(configured, observed);
|
|
350349
|
+
const bytes = Buffer.byteLength(JSON.stringify(params), 'utf8');
|
|
350350
|
+
options?.requestBodyBudget?.onMeasured?.(bytes, limit, endpoint);
|
|
350351
|
+
if (!options?.isCompactionRequest && limit !== undefined && bytes >= limit)
|
|
350352
|
+
throw t3458Pressure(bytes, limit, endpoint);
|
|
350353
|
+
return { key, limit, bytes };
|
|
350354
|
+
}
|
|
350355
|
+
function t3458Pressure(byteLength, limitBytes, endpoint) {
|
|
350356
|
+
const error = new ChatProviderError(`Request body ${byteLength} bytes reaches limit ${limitBytes} bytes.`);
|
|
350357
|
+
error.name = 'APIRequestBodyPressureError';
|
|
350358
|
+
error.requestBodyPressure = true;
|
|
350359
|
+
error.byteLength = byteLength;
|
|
350360
|
+
error.limitBytes = limitBytes;
|
|
350361
|
+
error.endpoint = endpoint;
|
|
350362
|
+
return error;
|
|
350363
|
+
}
|
|
350364
|
+
function t3458Observed(error) {
|
|
350365
|
+
const message = error instanceof Error ? error.message : '';
|
|
350366
|
+
if (!/(?:request\s*body|payload|body\s*size)/i.test(message))
|
|
350367
|
+
return undefined;
|
|
350368
|
+
const comparison = /\(\s*\d+\s*>\s*(\d+)\s*bytes?\s*\)/i.exec(message);
|
|
350369
|
+
if (comparison !== null)
|
|
350370
|
+
return t3458Learned(Number(comparison[1]));
|
|
350371
|
+
const match = /(?:limit|maximum|max|up to)[^\d]{0,30}(\d{2,12})\s*(?:bytes?|b)\b/i.exec(message);
|
|
350372
|
+
return match === null ? undefined : t3458Learned(Number(match[1]));
|
|
350373
|
+
}
|
|
350374
|
+
async function t3458Learn(provider, error, measured, options) {
|
|
350375
|
+
const limit = t3458Observed(error);
|
|
350376
|
+
if (limit === undefined)
|
|
350377
|
+
return;
|
|
350378
|
+
provider._t3458ObservedLimit = Math.min(t3458Learned(provider._t3458ObservedLimit) ?? Infinity, limit);
|
|
350379
|
+
options?.requestBodyBudget?.onLimitObserved?.(limit);
|
|
350380
|
+
if (measured.key !== undefined && provider._requestBodyByteLimitStore !== undefined) {
|
|
350381
|
+
try {
|
|
350382
|
+
await provider._requestBodyByteLimitStore.save(measured.key, limit);
|
|
350383
|
+
}
|
|
350384
|
+
catch { }
|
|
350385
|
+
}
|
|
350386
|
+
}
|