kimiflare 0.92.0 → 0.93.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +112 -3
- package/dist/index.js.map +1 -1
- package/dist/sdk/index.d.ts +4 -0
- package/dist/sdk/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -34066,6 +34066,117 @@ ${conflicts.join("\n")}` }
|
|
|
34066
34066
|
}
|
|
34067
34067
|
}
|
|
34068
34068
|
}
|
|
34069
|
+
const autoCompactTokenThreshold = cfg?.autoCompactTokenThreshold ?? 5e5;
|
|
34070
|
+
const autoFreshTokenThreshold = cfg?.autoFreshTokenThreshold ?? 2e6;
|
|
34071
|
+
const estimatedTokens = estimatePromptTokens(messagesRef.current);
|
|
34072
|
+
if (estimatedTokens > autoCompactTokenThreshold) {
|
|
34073
|
+
let didAggressiveCompact = false;
|
|
34074
|
+
if (compiledContextRef.current) {
|
|
34075
|
+
const store = artifactStoreRef.current;
|
|
34076
|
+
const result = compactMessagesViaArtifacts({
|
|
34077
|
+
messages: messagesRef.current,
|
|
34078
|
+
state: sessionStateRef.current,
|
|
34079
|
+
store,
|
|
34080
|
+
keepLastTurns: 1
|
|
34081
|
+
});
|
|
34082
|
+
if (result.metrics.rawTurnsRemoved > 0) {
|
|
34083
|
+
messagesRef.current = result.newMessages;
|
|
34084
|
+
sessionStateRef.current = result.newState;
|
|
34085
|
+
setEvents((e) => [
|
|
34086
|
+
...e,
|
|
34087
|
+
{
|
|
34088
|
+
kind: "info",
|
|
34089
|
+
key: mkKey(),
|
|
34090
|
+
text: `auto-compacted (aggressive): ${result.metrics.estimatedTokensBefore.toLocaleString()} \u2192 ${result.metrics.estimatedTokensAfter.toLocaleString()} tokens (${result.metrics.archivedArtifacts} artifacts)`
|
|
34091
|
+
}
|
|
34092
|
+
]);
|
|
34093
|
+
await saveSessionSafe();
|
|
34094
|
+
didAggressiveCompact = true;
|
|
34095
|
+
}
|
|
34096
|
+
} else {
|
|
34097
|
+
try {
|
|
34098
|
+
const result = await summarizeMessagesViaLlm({
|
|
34099
|
+
accountId: cfg.accountId,
|
|
34100
|
+
apiToken: cfg.apiToken,
|
|
34101
|
+
model: cfg.model,
|
|
34102
|
+
messages: messagesRef.current,
|
|
34103
|
+
keepLastTurns: 1,
|
|
34104
|
+
signal: turnScope.signal,
|
|
34105
|
+
gateway: gatewayFromConfig(cfg)
|
|
34106
|
+
});
|
|
34107
|
+
if (result.replacedCount > 0) {
|
|
34108
|
+
messagesRef.current = result.newMessages;
|
|
34109
|
+
setEvents((e) => [
|
|
34110
|
+
...e,
|
|
34111
|
+
{
|
|
34112
|
+
kind: "info",
|
|
34113
|
+
key: mkKey(),
|
|
34114
|
+
text: `auto-compacted (aggressive): ${result.replacedCount} messages summarized`
|
|
34115
|
+
}
|
|
34116
|
+
]);
|
|
34117
|
+
await saveSessionSafe();
|
|
34118
|
+
didAggressiveCompact = true;
|
|
34119
|
+
}
|
|
34120
|
+
} catch (compactErr) {
|
|
34121
|
+
if (compactErr.name !== "AbortError") {
|
|
34122
|
+
setEvents((es) => [
|
|
34123
|
+
...es,
|
|
34124
|
+
{
|
|
34125
|
+
kind: "info",
|
|
34126
|
+
key: mkKey(),
|
|
34127
|
+
text: `aggressive auto-compact failed: ${compactErr.message ?? String(compactErr)}`
|
|
34128
|
+
}
|
|
34129
|
+
]);
|
|
34130
|
+
}
|
|
34131
|
+
}
|
|
34132
|
+
}
|
|
34133
|
+
const tokensAfterCompact = estimatePromptTokens(messagesRef.current);
|
|
34134
|
+
if (tokensAfterCompact > autoFreshTokenThreshold) {
|
|
34135
|
+
try {
|
|
34136
|
+
const turnIndex = messagesRef.current.length;
|
|
34137
|
+
if (turnIndex > 0) {
|
|
34138
|
+
const cp = {
|
|
34139
|
+
id: `cp_prefresh_${Date.now()}`,
|
|
34140
|
+
label: "pre-fresh auto-save",
|
|
34141
|
+
turnIndex,
|
|
34142
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
34143
|
+
sessionState: compiledContextRef.current ? sessionStateRef.current : void 0,
|
|
34144
|
+
artifactStore: serializeArtifactStore(artifactStoreRef.current)
|
|
34145
|
+
};
|
|
34146
|
+
ensureSessionId();
|
|
34147
|
+
const { sessionsDir: sessionsDir3 } = await Promise.resolve().then(() => (init_sessions(), sessions_exports));
|
|
34148
|
+
const filePath = join38(sessionsDir3(), `${sessionIdRef.current}.json`);
|
|
34149
|
+
await addCheckpoint(filePath, cp);
|
|
34150
|
+
}
|
|
34151
|
+
const summary = await generateContinuationSummary({
|
|
34152
|
+
messages: messagesRef.current,
|
|
34153
|
+
mode: modeRef.current,
|
|
34154
|
+
accountId: cfg.accountId,
|
|
34155
|
+
apiToken: cfg.apiToken,
|
|
34156
|
+
model: cfg.plumbingModel ?? "@cf/moonshotai/kimi-k2.5",
|
|
34157
|
+
gateway: gatewayFromConfig(cfg),
|
|
34158
|
+
memoryManager: memoryManagerRef.current,
|
|
34159
|
+
memoryEnabled: cfg.memoryEnabled
|
|
34160
|
+
});
|
|
34161
|
+
if (summary) {
|
|
34162
|
+
executeFreshStart(buildSlashContext(), summary);
|
|
34163
|
+
setEvents((e) => [
|
|
34164
|
+
...e,
|
|
34165
|
+
{
|
|
34166
|
+
kind: "info",
|
|
34167
|
+
key: mkKey(),
|
|
34168
|
+
text: `Auto-fresh triggered at ~${tokensAfterCompact.toLocaleString()} tokens: starting new session with continuation context\u2026`
|
|
34169
|
+
}
|
|
34170
|
+
]);
|
|
34171
|
+
}
|
|
34172
|
+
} catch (e) {
|
|
34173
|
+
setEvents((es) => [
|
|
34174
|
+
...es,
|
|
34175
|
+
{ kind: "error", key: mkKey(), text: `Auto-fresh failed: ${e.message}` }
|
|
34176
|
+
]);
|
|
34177
|
+
}
|
|
34178
|
+
}
|
|
34179
|
+
}
|
|
34069
34180
|
})();
|
|
34070
34181
|
cleanupTurn();
|
|
34071
34182
|
if (modeRef.current === "plan") {
|
|
@@ -34108,7 +34219,6 @@ ${conflicts.join("\n")}` }
|
|
|
34108
34219
|
const did = cloudDeviceId ?? initialCloudDeviceId;
|
|
34109
34220
|
let used = 0;
|
|
34110
34221
|
let limit = 0;
|
|
34111
|
-
let expiresAt = "";
|
|
34112
34222
|
if (token) {
|
|
34113
34223
|
try {
|
|
34114
34224
|
const { fetchCloudUsage: fetchCloudUsage2 } = await Promise.resolve().then(() => (init_auth(), auth_exports));
|
|
@@ -34116,7 +34226,6 @@ ${conflicts.join("\n")}` }
|
|
|
34116
34226
|
if (usage2) {
|
|
34117
34227
|
used = usage2.input_tokens_used;
|
|
34118
34228
|
limit = usage2.input_token_limit;
|
|
34119
|
-
expiresAt = usage2.expires_at;
|
|
34120
34229
|
}
|
|
34121
34230
|
} catch {
|
|
34122
34231
|
}
|
|
@@ -34130,7 +34239,7 @@ ${conflicts.join("\n")}` }
|
|
|
34130
34239
|
}
|
|
34131
34240
|
setEvents((es) => [
|
|
34132
34241
|
...es,
|
|
34133
|
-
{ kind: "cloud_quota_exhausted", key: mkKey(), used, limit
|
|
34242
|
+
{ kind: "cloud_quota_exhausted", key: mkKey(), used, limit }
|
|
34134
34243
|
]);
|
|
34135
34244
|
} else if (e instanceof KimiApiError && (e.httpStatus === 429 || e.code === 3040 || e.httpStatus !== void 0 && e.httpStatus >= 500)) {
|
|
34136
34245
|
const err = { httpStatus: e.httpStatus, code: e.code, message: humanizeCloudflareError(e) };
|