blun-king-cli 9.1.638 → 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 +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 +977 -662
- package/package.json +1 -1
- package/telegram-plugin/dist/bridge.mjs +4 -1
- package/telegram-plugin/dist/mcp-server.mjs +6 -3
- package/worker-host.mjs +959 -659
package/blun.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// BLUN_BUILD_INPUT_SHA256:
|
|
2
|
+
// BLUN_BUILD_INPUT_SHA256:4ddd869ff27cd786caab65fef2e4a43ddeb636eb25fdea7ff03b52a5a716faab
|
|
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;
|
|
@@ -270360,54 +270474,67 @@ var init_agent = __esmMin((() => {
|
|
|
270360
270474
|
return this.generateForModel(this.activeResponderModel ?? this.config.modelAlias);
|
|
270361
270475
|
}
|
|
270362
270476
|
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
|
-
|
|
270477
|
+
return async (provider, systemPrompt, tools, history, callbacks, options) => {
|
|
270478
|
+
const { requestLogFields, generateOptions } = splitGenerateOptions(options);
|
|
270479
|
+
const retryScope = providerRetryScope(modelAlias, provider);
|
|
270480
|
+
await this.providerRetryWait.wait(retryScope, generateOptions?.signal);
|
|
270481
|
+
const callerTransportAttempt = generateOptions?.onTransportAttempt;
|
|
270482
|
+
const ownedGenerateOptions = {
|
|
270483
|
+
...generateOptions,
|
|
270484
|
+
transportMaxRetries: 0,
|
|
270485
|
+
requestBodyBudget: {
|
|
270486
|
+
...generateOptions?.requestBodyBudget,
|
|
270487
|
+
onMeasured: (bytes, limit, endpoint) => {
|
|
270488
|
+
generateOptions?.requestBodyBudget?.onMeasured?.(bytes, limit, endpoint);
|
|
270489
|
+
if (!generateOptions?.isCompactionRequest && limit !== undefined && this.fullCompaction.shouldCompactRequestBody(endpoint, bytes, limit))
|
|
270490
|
+
throw t3458Pressure(bytes, limit, endpoint);
|
|
270491
|
+
}
|
|
270492
|
+
},
|
|
270493
|
+
onTransportAttempt: (stats) => {
|
|
270494
|
+
callerTransportAttempt?.(stats);
|
|
270495
|
+
this.llmRequestLogger.logTransportAttempt(stats, requestLogFields);
|
|
270496
|
+
}
|
|
270497
|
+
};
|
|
270498
|
+
const run = async (requestOptions) => {
|
|
270499
|
+
let requestStarted = false;
|
|
270500
|
+
this.llmRequestLogger.logRequest({
|
|
270501
|
+
provider,
|
|
270502
|
+
modelAlias,
|
|
270503
|
+
systemPrompt,
|
|
270504
|
+
tools,
|
|
270505
|
+
messages: history,
|
|
270506
|
+
fields: requestLogFields
|
|
270507
|
+
});
|
|
270508
|
+
try {
|
|
270509
|
+
return await this.rawGenerate(provider, systemPrompt, tools, history, callbacks, {
|
|
270510
|
+
...requestOptions,
|
|
270511
|
+
onRequestStart: () => {
|
|
270512
|
+
requestOptions?.onRequestStart?.();
|
|
270513
|
+
requestStarted = true;
|
|
270514
|
+
}
|
|
270515
|
+
});
|
|
270516
|
+
}
|
|
270517
|
+
catch (error) {
|
|
270518
|
+
if (error instanceof APIProviderRateLimitError && error.retryAfterMs !== null)
|
|
270519
|
+
await this.providerRetryWait.defer(retryScope, error.retryAfterMs);
|
|
270520
|
+
if (requestStarted && requestOptions?.signal?.aborted !== true)
|
|
270521
|
+
this.turn.noteProviderRequestFailure(requestLogFields?.turnStep, error);
|
|
270522
|
+
throw error;
|
|
270523
|
+
}
|
|
270524
|
+
};
|
|
270525
|
+
if (ownedGenerateOptions.auth !== void 0)
|
|
270526
|
+
return run(ownedGenerateOptions);
|
|
270527
|
+
const withAuth = modelAlias === void 0 ? void 0 : this.modelProvider?.resolveAuth?.(modelAlias, { log: this.log });
|
|
270528
|
+
if (withAuth === void 0)
|
|
270529
|
+
return run(ownedGenerateOptions);
|
|
270530
|
+
return withAuth((auth) => {
|
|
270531
|
+
return run({
|
|
270532
|
+
...ownedGenerateOptions,
|
|
270533
|
+
auth
|
|
270534
|
+
});
|
|
270535
|
+
});
|
|
270536
|
+
};
|
|
270537
|
+
}
|
|
270411
270538
|
get llm() {
|
|
270412
270539
|
return this.llmForTurn();
|
|
270413
270540
|
}
|
|
@@ -301723,30 +301850,36 @@ var init_provider_manager = __esmMin((() => {
|
|
|
301723
301850
|
return typeof config === "function" ? config() : config;
|
|
301724
301851
|
}
|
|
301725
301852
|
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
|
-
|
|
301853
|
+
const alias = this.config.models?.[model];
|
|
301854
|
+
if (alias === void 0)
|
|
301855
|
+
throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" is not configured in config.toml. Add a [models."${model}"] entry with max_context_size.`);
|
|
301856
|
+
const effectiveAlias = effectiveModelAlias(alias);
|
|
301857
|
+
const providerName = alias.provider ?? this.config.defaultProvider;
|
|
301858
|
+
if (providerName === void 0)
|
|
301859
|
+
throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a provider in config.toml.`);
|
|
301860
|
+
const providerConfig = this.config.providers[providerName];
|
|
301861
|
+
if (providerConfig === void 0)
|
|
301862
|
+
throw new BlunError(ErrorCodes.CONFIG_INVALID, `Provider "${providerName}" for model "${model}" is not configured.`);
|
|
301863
|
+
assertBlunProviderType(providerConfig);
|
|
301864
|
+
const runtimeContext = this.runtimeModelContexts.get(model);
|
|
301865
|
+
const maxContextSize = runtimeContext?.providerName === providerName && runtimeContext.model === alias.model ? runtimeContext.maxContextTokens : effectiveAlias.maxContextSize;
|
|
301866
|
+
if (!Number.isInteger(maxContextSize) || maxContextSize <= 0)
|
|
301867
|
+
throw new BlunError(ErrorCodes.CONFIG_INVALID, `Model "${model}" must define a positive max_context_size in config.toml.`);
|
|
301868
|
+
const provider = toKosongProviderConfig(providerConfig, alias.model, this.options.blunRequestHeaders, this.options.promptCacheKey, effectiveAlias.supportEfforts);
|
|
301869
|
+
if (this.options.configPath !== undefined)
|
|
301870
|
+
provider.requestBodyByteLimitStore = t3458Store(this.options.configPath, parse$7, stringify$1, atomicWritePrivate$1);
|
|
301871
|
+
return {
|
|
301872
|
+
providerName,
|
|
301873
|
+
provider,
|
|
301874
|
+
modelCapabilities: resolveModelCapabilities({
|
|
301875
|
+
...effectiveAlias,
|
|
301876
|
+
maxContextSize
|
|
301877
|
+
}, provider),
|
|
301878
|
+
alwaysThinking: (effectiveAlias.capabilities ?? []).some((c) => c.trim().toLowerCase() === "always_thinking"),
|
|
301879
|
+
maxOutputSize: effectiveAlias.maxOutputSize,
|
|
301880
|
+
type: "blun"
|
|
301881
|
+
};
|
|
301882
|
+
}
|
|
301750
301883
|
setRuntimeModelContext(input) {
|
|
301751
301884
|
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
301885
|
if (!Number.isSafeInteger(input.maxContextTokens) || input.maxContextTokens <= 0) throw new BlunError(ErrorCodes.CONFIG_INVALID, "Runtime model context must be a positive integer.");
|
|
@@ -338157,14 +338290,15 @@ var init_core_impl = __esmMin((() => {
|
|
|
338157
338290
|
};
|
|
338158
338291
|
}
|
|
338159
338292
|
resolveProviderManager(sessionId) {
|
|
338160
|
-
|
|
338161
|
-
|
|
338162
|
-
|
|
338163
|
-
|
|
338164
|
-
|
|
338165
|
-
|
|
338166
|
-
|
|
338167
|
-
|
|
338293
|
+
return new ProviderManager({
|
|
338294
|
+
config: () => this.config,
|
|
338295
|
+
configPath: this.configPath,
|
|
338296
|
+
blunRequestHeaders: this.blunRequestHeaders,
|
|
338297
|
+
resolveOAuthTokenProvider: this.resolveOAuthTokenProvider,
|
|
338298
|
+
managedApiKeyRevocationStore: this.managedApiKeyRevocationStore,
|
|
338299
|
+
promptCacheKey: sessionId
|
|
338300
|
+
});
|
|
338301
|
+
}
|
|
338168
338302
|
mergePluginMcpConfig(base) {
|
|
338169
338303
|
const pluginEntries = this.plugins.mcpServerConfigs();
|
|
338170
338304
|
if (pluginEntries.length === 0) return base;
|
|
@@ -368239,7 +368373,7 @@ function optionalBuildString(value) {
|
|
|
368239
368373
|
var BLUN_BUILD_INFO;
|
|
368240
368374
|
var init_build_info = __esmMin((() => {
|
|
368241
368375
|
BLUN_BUILD_INFO = {
|
|
368242
|
-
version: optionalBuildString("9.1.
|
|
368376
|
+
version: optionalBuildString("9.1.640"),
|
|
368243
368377
|
channel: typeof __BLUN_CHANNEL__ === "string" ? optionalBuildString(__BLUN_CHANNEL__) : void 0,
|
|
368244
368378
|
commit: typeof __BLUN_COMMIT__ === "string" ? optionalBuildString(__BLUN_COMMIT__) : void 0,
|
|
368245
368379
|
buildTarget: typeof __BLUN_BUILD_TARGET__ === "string" ? optionalBuildString(__BLUN_BUILD_TARGET__) : void 0
|
|
@@ -379760,6 +379894,18 @@ function extractCompleteSequences(buffer) {
|
|
|
379760
379894
|
let pos = 0;
|
|
379761
379895
|
while (pos < buffer.length) {
|
|
379762
379896
|
const remaining = buffer.slice(pos);
|
|
379897
|
+
// Keep an interrupted keyboard CSI while delivering an embedded terminal reply.
|
|
379898
|
+
const nestedEscape = remaining.indexOf(ESC$1, 2);
|
|
379899
|
+
if (nestedEscape > 1 && /^\x1b\[[0-9:;]*$/.test(remaining.slice(0, nestedEscape))) {
|
|
379900
|
+
const nested = remaining.slice(nestedEscape);
|
|
379901
|
+
const reply = nested.match(/^\x1b\[(?:8;\d+;\d+t|\?[\d;]*[cu])/);
|
|
379902
|
+
if (reply !== null) {
|
|
379903
|
+
sequences.push(reply[0]);
|
|
379904
|
+
buffer = buffer.slice(0, pos) + remaining.slice(0, nestedEscape) + nested.slice(reply[0].length);
|
|
379905
|
+
continue;
|
|
379906
|
+
}
|
|
379907
|
+
if (/^\x1b(?:\[(?:\?[\d;]*|8(?:;\d*){0,2})?)?$/.test(nested)) return { sequences, remainder: remaining };
|
|
379908
|
+
}
|
|
379763
379909
|
if (remaining.startsWith(ESC$1)) {
|
|
379764
379910
|
let seqEnd = 1;
|
|
379765
379911
|
while (seqEnd <= remaining.length) {
|
|
@@ -379874,7 +380020,7 @@ var init_stdin_buffer = __esmMin((() => {
|
|
|
379874
380020
|
if (this.buffer.length > 0) this.timeout = setTimeout(() => {
|
|
379875
380021
|
const flushed = this.flush();
|
|
379876
380022
|
for (const sequence of flushed) this.emitDataSequence(sequence);
|
|
379877
|
-
}, this.timeoutMs);
|
|
380023
|
+
}, this.buffer.startsWith(`${ESC$1}[`) ? Math.max(this.timeoutMs, 100) : this.timeoutMs);
|
|
379878
380024
|
}
|
|
379879
380025
|
emitDataSequence(sequence) {
|
|
379880
380026
|
const rawCodepoint = sequence.length === 1 ? sequence.codePointAt(0) : void 0;
|
|
@@ -536295,7 +536441,7 @@ var init_session_event_handler = __esmMin((() => {
|
|
|
536295
536441
|
});
|
|
536296
536442
|
this.host.resetLivePane();
|
|
536297
536443
|
this.host.requestQueueDrain();
|
|
536298
|
-
} else this.host.setAppState({ isCompacting: false });
|
|
536444
|
+
} else { this.host.setAppState({ isCompacting: false }); this.host.steerQueueFlushPrefix(); }
|
|
536299
536445
|
}
|
|
536300
536446
|
handleBackgroundTaskEvent(event) {
|
|
536301
536447
|
const { state } = this.host;
|
|
@@ -542716,7 +542862,10 @@ function isGroupSuppressed(text, _contextOnly) {
|
|
|
542716
542862
|
return isGroupNoiseReply(text);
|
|
542717
542863
|
}
|
|
542718
542864
|
async function sendReplyFallback(chatId, text, contextOnly = false, requireDelivery = false, progress) {
|
|
542719
|
-
if (typeof text === "string"
|
|
542865
|
+
if (typeof text === "string") {
|
|
542866
|
+
text = text.split("[[BLUN_TELEGRAM_SILENT]]").join("");
|
|
542867
|
+
if (text.trim().length === 0) return true;
|
|
542868
|
+
}
|
|
542720
542869
|
if (isGroupChat(chatId) && isGroupSuppressed(text, contextOnly)) {
|
|
542721
542870
|
try {
|
|
542722
542871
|
appendFileSync(outboxPath(), `${JSON.stringify({
|
|
@@ -562500,3 +562649,169 @@ async function logStartupFailure(operation, error) {
|
|
|
562500
562649
|
} catch {}
|
|
562501
562650
|
}
|
|
562502
562651
|
export { configureTelegramPluginLaunchMode, handleMainCommand, handleUpgradeCommand, main };
|
|
562652
|
+
|
|
562653
|
+
import * as t3458Fs from "node:fs/promises";
|
|
562654
|
+
import * as t3458Path from "node:path";
|
|
562655
|
+
import * as t3458Crypto from "node:crypto";
|
|
562656
|
+
// Neue, selektive Verdichtungshelfer. Keine Quellübernahme aus Arbeitsbaum oder v2/v3.
|
|
562657
|
+
function t3458ValidLimit(value) {
|
|
562658
|
+
return Number.isSafeInteger(value) && value > 0 ? value : undefined;
|
|
562659
|
+
}
|
|
562660
|
+
function t3458Learned(value) {
|
|
562661
|
+
return t3458ValidLimit(value) !== undefined && value >= 65536 ? value : undefined;
|
|
562662
|
+
}
|
|
562663
|
+
function t3458Endpoint(baseUrl) {
|
|
562664
|
+
try {
|
|
562665
|
+
const url = new URL(baseUrl);
|
|
562666
|
+
if (!['https:', 'http:'].includes(url.protocol) || url.username || url.password || url.search || url.hash)
|
|
562667
|
+
return undefined;
|
|
562668
|
+
return `${url.origin}${url.pathname.replace(/\/+$/, '')}`;
|
|
562669
|
+
}
|
|
562670
|
+
catch {
|
|
562671
|
+
return undefined;
|
|
562672
|
+
}
|
|
562673
|
+
}
|
|
562674
|
+
function t3458Key(baseUrl) {
|
|
562675
|
+
const endpoint = t3458Endpoint(baseUrl);
|
|
562676
|
+
return endpoint === undefined ? undefined : t3458Crypto.createHash('sha256').update(endpoint).digest('hex');
|
|
562677
|
+
}
|
|
562678
|
+
async function t3458WithConfigLock(filePath, action, learning = false) {
|
|
562679
|
+
await t3458Fs.mkdir(t3458Path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
562680
|
+
const lock = `${filePath}.request-body.lock`;
|
|
562681
|
+
const token = t3458Crypto.randomUUID();
|
|
562682
|
+
// Small scheduling reserve: bounded contention attempts stay below 2s / 12s.
|
|
562683
|
+
const deadline = performance.now() + (learning ? 1950 : 11950);
|
|
562684
|
+
let handle;
|
|
562685
|
+
while (handle === undefined) {
|
|
562686
|
+
if (performance.now() >= deadline)
|
|
562687
|
+
return learning ? undefined : await action();
|
|
562688
|
+
try {
|
|
562689
|
+
handle = await t3458Fs.open(lock, 'wx', 0o600);
|
|
562690
|
+
}
|
|
562691
|
+
catch (error) {
|
|
562692
|
+
if (error.code !== 'EEXIST' && !(process.platform === 'win32' && ['EPERM', 'EACCES', 'EBUSY'].includes(error.code)))
|
|
562693
|
+
throw error;
|
|
562694
|
+
try {
|
|
562695
|
+
const observed = await t3458Fs.stat(lock);
|
|
562696
|
+
if (Date.now() - observed.mtimeMs > 10000) {
|
|
562697
|
+
const owner = await t3458Fs.readFile(lock, 'utf8');
|
|
562698
|
+
const current = await t3458Fs.stat(lock);
|
|
562699
|
+
// Best effort only: the accepted check/unlink race is not fencing.
|
|
562700
|
+
if (current.mtimeMs === observed.mtimeMs && await t3458Fs.readFile(lock, 'utf8') === owner) {
|
|
562701
|
+
await t3458Fs.unlink(lock);
|
|
562702
|
+
continue;
|
|
562703
|
+
}
|
|
562704
|
+
}
|
|
562705
|
+
}
|
|
562706
|
+
catch (failure) {
|
|
562707
|
+
if (failure.code !== 'ENOENT')
|
|
562708
|
+
throw failure;
|
|
562709
|
+
}
|
|
562710
|
+
const remaining = deadline - performance.now();
|
|
562711
|
+
if (remaining > 0)
|
|
562712
|
+
await new Promise(resolve => setTimeout(resolve, Math.min(25, remaining)));
|
|
562713
|
+
}
|
|
562714
|
+
}
|
|
562715
|
+
try {
|
|
562716
|
+
await handle.writeFile(token, 'utf8');
|
|
562717
|
+
return await action();
|
|
562718
|
+
}
|
|
562719
|
+
finally {
|
|
562720
|
+
await handle.close();
|
|
562721
|
+
try {
|
|
562722
|
+
if (await t3458Fs.readFile(lock, 'utf8') === token)
|
|
562723
|
+
await t3458Fs.unlink(lock);
|
|
562724
|
+
}
|
|
562725
|
+
catch (error) {
|
|
562726
|
+
if (error.code !== 'ENOENT')
|
|
562727
|
+
throw error;
|
|
562728
|
+
}
|
|
562729
|
+
}
|
|
562730
|
+
}
|
|
562731
|
+
function t3458Store(filePath, parseToml, stringifyToml, atomicWrite) {
|
|
562732
|
+
return {
|
|
562733
|
+
async load(key) {
|
|
562734
|
+
try {
|
|
562735
|
+
return t3458Learned(parseToml(await t3458Fs.readFile(filePath, 'utf8')).request_body_byte_limits?.[key]);
|
|
562736
|
+
}
|
|
562737
|
+
catch (error) {
|
|
562738
|
+
if (error.code === 'ENOENT')
|
|
562739
|
+
return undefined;
|
|
562740
|
+
throw error;
|
|
562741
|
+
}
|
|
562742
|
+
},
|
|
562743
|
+
async save(key, value) {
|
|
562744
|
+
if (!/^[a-f0-9]{64}$/.test(key) || t3458Learned(value) === undefined)
|
|
562745
|
+
return;
|
|
562746
|
+
await t3458WithConfigLock(filePath, async () => {
|
|
562747
|
+
let data;
|
|
562748
|
+
try {
|
|
562749
|
+
data = parseToml(await t3458Fs.readFile(filePath, 'utf8'));
|
|
562750
|
+
}
|
|
562751
|
+
catch (error) {
|
|
562752
|
+
if (error.code !== 'ENOENT')
|
|
562753
|
+
throw error;
|
|
562754
|
+
data = {};
|
|
562755
|
+
}
|
|
562756
|
+
const table = data.request_body_byte_limits ?? {};
|
|
562757
|
+
const previous = t3458Learned(table[key]);
|
|
562758
|
+
data.request_body_byte_limits = { ...table, [key]: previous === undefined ? value : Math.min(previous, value) };
|
|
562759
|
+
await atomicWrite(filePath, `${stringifyToml(data)}\n`);
|
|
562760
|
+
}, true);
|
|
562761
|
+
}
|
|
562762
|
+
};
|
|
562763
|
+
}
|
|
562764
|
+
async function t3458Measure(provider, params, options) {
|
|
562765
|
+
const endpoint = t3458Endpoint(provider._baseUrl);
|
|
562766
|
+
const key = t3458Key(provider._baseUrl);
|
|
562767
|
+
const option = typeof provider._requestBodyByteLimit === 'function' ? provider._requestBodyByteLimit() : provider._requestBodyByteLimit;
|
|
562768
|
+
const configured = t3458ValidLimit(options?.requestBodyBudget?.limitBytes) ?? t3458ValidLimit(option) ?? t3458ValidLimit(Number(process.env.BLUN_REQUEST_BODY_BYTE_LIMIT));
|
|
562769
|
+
let persisted;
|
|
562770
|
+
if (key !== undefined && provider._requestBodyByteLimitStore !== undefined) {
|
|
562771
|
+
try {
|
|
562772
|
+
persisted = t3458Learned(await provider._requestBodyByteLimitStore.load(key));
|
|
562773
|
+
}
|
|
562774
|
+
catch { }
|
|
562775
|
+
}
|
|
562776
|
+
const learned = t3458Learned(provider._t3458ObservedLimit);
|
|
562777
|
+
const observed = learned === undefined ? persisted : persisted === undefined ? learned : Math.min(learned, persisted);
|
|
562778
|
+
const fallback = endpoint === 'https://api.blun.ai/coding/v1' ? 500000 : undefined;
|
|
562779
|
+
const limit = configured === undefined ? observed ?? fallback : observed === undefined ? configured : Math.min(configured, observed);
|
|
562780
|
+
const bytes = Buffer.byteLength(JSON.stringify(params), 'utf8');
|
|
562781
|
+
options?.requestBodyBudget?.onMeasured?.(bytes, limit, endpoint);
|
|
562782
|
+
if (!options?.isCompactionRequest && limit !== undefined && bytes >= limit)
|
|
562783
|
+
throw t3458Pressure(bytes, limit, endpoint);
|
|
562784
|
+
return { key, limit, bytes };
|
|
562785
|
+
}
|
|
562786
|
+
function t3458Pressure(byteLength, limitBytes, endpoint) {
|
|
562787
|
+
const error = new ChatProviderError(`Request body ${byteLength} bytes reaches limit ${limitBytes} bytes.`);
|
|
562788
|
+
error.name = 'APIRequestBodyPressureError';
|
|
562789
|
+
error.requestBodyPressure = true;
|
|
562790
|
+
error.byteLength = byteLength;
|
|
562791
|
+
error.limitBytes = limitBytes;
|
|
562792
|
+
error.endpoint = endpoint;
|
|
562793
|
+
return error;
|
|
562794
|
+
}
|
|
562795
|
+
function t3458Observed(error) {
|
|
562796
|
+
const message = error instanceof Error ? error.message : '';
|
|
562797
|
+
if (!/(?:request\s*body|payload|body\s*size)/i.test(message))
|
|
562798
|
+
return undefined;
|
|
562799
|
+
const comparison = /\(\s*\d+\s*>\s*(\d+)\s*bytes?\s*\)/i.exec(message);
|
|
562800
|
+
if (comparison !== null)
|
|
562801
|
+
return t3458Learned(Number(comparison[1]));
|
|
562802
|
+
const match = /(?:limit|maximum|max|up to)[^\d]{0,30}(\d{2,12})\s*(?:bytes?|b)\b/i.exec(message);
|
|
562803
|
+
return match === null ? undefined : t3458Learned(Number(match[1]));
|
|
562804
|
+
}
|
|
562805
|
+
async function t3458Learn(provider, error, measured, options) {
|
|
562806
|
+
const limit = t3458Observed(error);
|
|
562807
|
+
if (limit === undefined)
|
|
562808
|
+
return;
|
|
562809
|
+
provider._t3458ObservedLimit = Math.min(t3458Learned(provider._t3458ObservedLimit) ?? Infinity, limit);
|
|
562810
|
+
options?.requestBodyBudget?.onLimitObserved?.(limit);
|
|
562811
|
+
if (measured.key !== undefined && provider._requestBodyByteLimitStore !== undefined) {
|
|
562812
|
+
try {
|
|
562813
|
+
await provider._requestBodyByteLimitStore.save(measured.key, limit);
|
|
562814
|
+
}
|
|
562815
|
+
catch { }
|
|
562816
|
+
}
|
|
562817
|
+
}
|