@theokit/sdk 4.16.3 → 4.16.4
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/dist/cron.cjs +249 -146
- package/dist/cron.cjs.map +1 -1
- package/dist/cron.js +250 -147
- package/dist/cron.js.map +1 -1
- package/dist/eval.cjs +249 -146
- package/dist/eval.cjs.map +1 -1
- package/dist/eval.js +250 -147
- package/dist/eval.js.map +1 -1
- package/dist/index.cjs +245 -146
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +245 -146
- package/dist/index.js.map +1 -1
- package/dist/internal/session/agent-session.d.ts +12 -0
- package/dist/provider-catalog.json +493 -144
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 4.16.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- fix(session): M50 adversarial-review fixes — (F1 BLOCKER) compaction now INVALIDATES the in-memory session cache, so the live process feels the compact on the very next send (it used to keep sending the full pre-compact history until restart); (F3) the auto-trigger uses the LAST request's usage as the active-context proxy (the across-rounds aggregate fired prematurely on agentic turns); (F5) `Agent.compact` serializes on the per-agent write chain (a manual compact can no longer interleave with an in-flight turn — race test added); (F6) the summarizer routes through the model-prefix provider PROFILE when registered (oauth `openai-chatgpt` builtin owns its auth; M45 fleet resolves its own env) before falling back to key/env inference; (F2) once-per-process WARN when the model has no catalog context window + gpt-5.x family added to the catalog (400k) with the `openai-chatgpt` alias so the product default can actually trigger; (F8) the user message that overflows the 20k preservation budget is TRUNCATED and kept (Codex parity) instead of dropped.
|
|
8
|
+
|
|
3
9
|
## 4.16.3
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/dist/cron.cjs
CHANGED
|
@@ -1606,6 +1606,78 @@ var init_run_events = __esm({
|
|
|
1606
1606
|
}
|
|
1607
1607
|
});
|
|
1608
1608
|
|
|
1609
|
+
// src/internal/session/agent-session-store.ts
|
|
1610
|
+
function seedTranscript(prior, opts) {
|
|
1611
|
+
return SessionTranscript.fromRecords(prior, opts);
|
|
1612
|
+
}
|
|
1613
|
+
function mapAgentTurn(steps) {
|
|
1614
|
+
const assistant = {};
|
|
1615
|
+
const toolResults = [];
|
|
1616
|
+
const toolCalls = [];
|
|
1617
|
+
for (const step of steps) {
|
|
1618
|
+
if (step.type === "thinkingMessage") assistant.thinking = step.message.text;
|
|
1619
|
+
else if (step.type === "assistantMessage") assistant.text = step.message.text;
|
|
1620
|
+
else if (step.type === "toolCall")
|
|
1621
|
+
toolCalls.push({
|
|
1622
|
+
id: step.message.callId,
|
|
1623
|
+
name: step.message.name,
|
|
1624
|
+
input: step.message.args ?? {}
|
|
1625
|
+
});
|
|
1626
|
+
else
|
|
1627
|
+
toolResults.push({
|
|
1628
|
+
toolUseId: step.message.callId,
|
|
1629
|
+
content: step.message.result,
|
|
1630
|
+
isError: step.message.isError
|
|
1631
|
+
});
|
|
1632
|
+
}
|
|
1633
|
+
if (toolCalls.length > 0) assistant.toolCalls = toolCalls;
|
|
1634
|
+
return { assistant, toolResults };
|
|
1635
|
+
}
|
|
1636
|
+
function hasAssistantContent(a) {
|
|
1637
|
+
return a.text !== void 0 || a.thinking !== void 0 || (a.toolCalls?.length ?? 0) > 0;
|
|
1638
|
+
}
|
|
1639
|
+
function appendConversation(transcript, conversation) {
|
|
1640
|
+
for (const ct of conversation) {
|
|
1641
|
+
if (ct.type !== "agentConversationTurn") continue;
|
|
1642
|
+
const { assistant, toolResults } = mapAgentTurn(ct.turn.steps);
|
|
1643
|
+
if (hasAssistantContent(assistant)) transcript.appendAssistantTurn(assistant);
|
|
1644
|
+
if (toolResults.length > 0) transcript.appendToolResults(toolResults);
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
async function readSessionMessages(store, agentId) {
|
|
1648
|
+
const records = await store.readRecords(agentId);
|
|
1649
|
+
return reconstructMessages(records).map(narrowToSessionMessage);
|
|
1650
|
+
}
|
|
1651
|
+
function partToText(p) {
|
|
1652
|
+
if (p.type === "text") return p.text ?? "";
|
|
1653
|
+
if (p.type === "tool_use") return `[tool call] ${p.name ?? ""}`;
|
|
1654
|
+
if (p.type === "tool_result") {
|
|
1655
|
+
const body = typeof p.content === "string" ? p.content : JSON.stringify(p.content);
|
|
1656
|
+
return `[tool result] ${body}`;
|
|
1657
|
+
}
|
|
1658
|
+
return "";
|
|
1659
|
+
}
|
|
1660
|
+
function narrowToSessionMessage(m) {
|
|
1661
|
+
const role = m.role === "user" ? "user" : "assistant";
|
|
1662
|
+
const text = m.content.map(partToText).filter((s) => s.length > 0).join("\n");
|
|
1663
|
+
return { role, text };
|
|
1664
|
+
}
|
|
1665
|
+
function deltaRecords(transcript, priorLength) {
|
|
1666
|
+
return transcript.records().slice(priorLength);
|
|
1667
|
+
}
|
|
1668
|
+
async function persistTurn(store, loc, sessionId, turn) {
|
|
1669
|
+
const prior = await store.readRecords(loc.agentId);
|
|
1670
|
+
const transcript = seedTranscript(prior, { cwd: loc.cwd, sessionId, model: loc.model });
|
|
1671
|
+
transcript.appendUserTurn(turn.userText);
|
|
1672
|
+
appendConversation(transcript, turn.conversation);
|
|
1673
|
+
await store.appendRecords(loc.agentId, deltaRecords(transcript, prior.length));
|
|
1674
|
+
}
|
|
1675
|
+
var init_agent_session_store = __esm({
|
|
1676
|
+
"src/internal/session/agent-session-store.ts"() {
|
|
1677
|
+
init_session_transcript();
|
|
1678
|
+
}
|
|
1679
|
+
});
|
|
1680
|
+
|
|
1609
1681
|
// src/compaction.ts
|
|
1610
1682
|
function estimateTokens(text) {
|
|
1611
1683
|
return Math.ceil(text.length / 4);
|
|
@@ -1806,6 +1878,13 @@ function getProviderProfile(name) {
|
|
|
1806
1878
|
const canonical = ALIASES.get(name) ?? name;
|
|
1807
1879
|
return REGISTRY.get(canonical);
|
|
1808
1880
|
}
|
|
1881
|
+
function listProviders() {
|
|
1882
|
+
return Array.from(REGISTRY.values());
|
|
1883
|
+
}
|
|
1884
|
+
function _resetProvidersForTests() {
|
|
1885
|
+
REGISTRY.clear();
|
|
1886
|
+
ALIASES.clear();
|
|
1887
|
+
}
|
|
1809
1888
|
var REGISTRY, ALIASES;
|
|
1810
1889
|
var init_registry = __esm({
|
|
1811
1890
|
"src/internal/providers/registry.ts"() {
|
|
@@ -2738,6 +2817,9 @@ function registerBuiltins() {
|
|
|
2738
2817
|
registerProvider(CEREBRAS);
|
|
2739
2818
|
registerCatalogProviders();
|
|
2740
2819
|
}
|
|
2820
|
+
function _resetBuiltinsRegistered() {
|
|
2821
|
+
_registeredState.done = false;
|
|
2822
|
+
}
|
|
2741
2823
|
var _registeredState;
|
|
2742
2824
|
var init_builtin = __esm({
|
|
2743
2825
|
"src/internal/providers/builtin/index.ts"() {
|
|
@@ -2864,6 +2946,9 @@ async function loadOne(dir, entryName) {
|
|
|
2864
2946
|
}
|
|
2865
2947
|
}
|
|
2866
2948
|
}
|
|
2949
|
+
function _resetDiscovery() {
|
|
2950
|
+
discoveryState.done = false;
|
|
2951
|
+
}
|
|
2867
2952
|
var discoveryState;
|
|
2868
2953
|
var init_discovery = __esm({
|
|
2869
2954
|
"src/internal/providers/discovery.ts"() {
|
|
@@ -2875,9 +2960,25 @@ var init_discovery = __esm({
|
|
|
2875
2960
|
});
|
|
2876
2961
|
|
|
2877
2962
|
// src/internal/providers/index.ts
|
|
2963
|
+
var providers_exports = {};
|
|
2964
|
+
__export(providers_exports, {
|
|
2965
|
+
ANTHROPIC: () => ANTHROPIC,
|
|
2966
|
+
GEMINI: () => GEMINI,
|
|
2967
|
+
OPENAI: () => OPENAI,
|
|
2968
|
+
OPENROUTER: () => OPENROUTER,
|
|
2969
|
+
_resetBuiltinsRegistered: () => _resetBuiltinsRegistered,
|
|
2970
|
+
_resetDiscovery: () => _resetDiscovery,
|
|
2971
|
+
_resetProvidersForTests: () => _resetProvidersForTests,
|
|
2972
|
+
discoverProviderPlugins: () => discoverProviderPlugins,
|
|
2973
|
+
getProviderProfile: () => getProviderProfile,
|
|
2974
|
+
listProviders: () => listProviders,
|
|
2975
|
+
registerBuiltins: () => registerBuiltins,
|
|
2976
|
+
registerProvider: () => registerProvider
|
|
2977
|
+
});
|
|
2878
2978
|
var init_providers = __esm({
|
|
2879
2979
|
"src/internal/providers/index.ts"() {
|
|
2880
2980
|
init_builtin();
|
|
2981
|
+
init_discovery();
|
|
2881
2982
|
init_registry();
|
|
2882
2983
|
}
|
|
2883
2984
|
});
|
|
@@ -5936,7 +6037,14 @@ async function compactSessionTranscript(opts) {
|
|
|
5936
6037
|
const m = compressible[i];
|
|
5937
6038
|
if (m === void 0 || m.role !== "user" || isCompactSummary(m.content)) continue;
|
|
5938
6039
|
const cost = estimateTokens(m.content);
|
|
5939
|
-
if (budget + cost > COMPACT_USER_MESSAGE_MAX_TOKENS)
|
|
6040
|
+
if (budget + cost > COMPACT_USER_MESSAGE_MAX_TOKENS) {
|
|
6041
|
+
const remaining = COMPACT_USER_MESSAGE_MAX_TOKENS - budget;
|
|
6042
|
+
if (remaining > 50) {
|
|
6043
|
+
preserved.unshift(`${m.content.slice(0, remaining * 4)}
|
|
6044
|
+
[...truncated for compaction...]`);
|
|
6045
|
+
}
|
|
6046
|
+
break;
|
|
6047
|
+
}
|
|
5940
6048
|
budget += cost;
|
|
5941
6049
|
preserved.unshift(m.content);
|
|
5942
6050
|
}
|
|
@@ -5952,6 +6060,8 @@ ${summaryBody}`;
|
|
|
5952
6060
|
transcript.appendUserTurn(summary);
|
|
5953
6061
|
const delta = transcript.records().slice(prior.length);
|
|
5954
6062
|
await opts.store.appendRecords(opts.loc.agentId, delta);
|
|
6063
|
+
const { invalidateSessionCache: invalidateSessionCache2 } = await Promise.resolve().then(() => (init_agent_session(), agent_session_exports));
|
|
6064
|
+
invalidateSessionCache2(opts.loc.cwd, opts.loc.agentId);
|
|
5955
6065
|
const postTokens = estimateTokens([...preserved, summary].join("\n"));
|
|
5956
6066
|
return { preTokens, postTokens };
|
|
5957
6067
|
}
|
|
@@ -5963,7 +6073,10 @@ function buildDefaultSummarizer(opts) {
|
|
|
5963
6073
|
const { inferProviderFromApiKey: inferProviderFromApiKey2, detectPrimaryProvider: detectPrimaryProvider2 } = await Promise.resolve().then(() => (init_real_local_run_provider(), real_local_run_provider_exports));
|
|
5964
6074
|
const keyProvider = inferProviderFromApiKey2(opts.apiKey);
|
|
5965
6075
|
const modelPrefix = opts.agentModel.includes("/") ? opts.agentModel.slice(0, opts.agentModel.indexOf("/")) : void 0;
|
|
5966
|
-
const
|
|
6076
|
+
const { registerBuiltins: registerBuiltins2, getProviderProfile: getProviderProfile2 } = await Promise.resolve().then(() => (init_providers(), providers_exports));
|
|
6077
|
+
registerBuiltins2();
|
|
6078
|
+
const prefixProfile = modelPrefix !== void 0 ? getProviderProfile2(modelPrefix) : void 0;
|
|
6079
|
+
const provider = prefixProfile !== void 0 ? modelPrefix : keyProvider ?? detectPrimaryProvider2();
|
|
5967
6080
|
let model;
|
|
5968
6081
|
if (provider !== modelPrefix) {
|
|
5969
6082
|
model = opts.agentModel;
|
|
@@ -6040,6 +6153,122 @@ var init_compact_session = __esm({
|
|
|
6040
6153
|
})();
|
|
6041
6154
|
}
|
|
6042
6155
|
});
|
|
6156
|
+
|
|
6157
|
+
// src/internal/session/agent-session.ts
|
|
6158
|
+
var agent_session_exports = {};
|
|
6159
|
+
__export(agent_session_exports, {
|
|
6160
|
+
appendSessionMessage: () => appendSessionMessage,
|
|
6161
|
+
clearAllSessions: () => clearAllSessions,
|
|
6162
|
+
clearSession: () => clearSession,
|
|
6163
|
+
enqueueSessionWrite: () => enqueueSessionWrite,
|
|
6164
|
+
flushSessionWrites: () => flushSessionWrites,
|
|
6165
|
+
getSessionMessages: () => getSessionMessages,
|
|
6166
|
+
hydrateSession: () => hydrateSession,
|
|
6167
|
+
invalidateSessionCache: () => invalidateSessionCache,
|
|
6168
|
+
persistTurnToTranscript: () => persistTurnToTranscript
|
|
6169
|
+
});
|
|
6170
|
+
function transcriptKey(cwd, agentId) {
|
|
6171
|
+
return `${cwd}::${agentId}`;
|
|
6172
|
+
}
|
|
6173
|
+
function appendSessionMessage(agentId, message) {
|
|
6174
|
+
const existing = sessions.get(agentId) ?? [];
|
|
6175
|
+
existing.push(message);
|
|
6176
|
+
sessions.set(agentId, existing);
|
|
6177
|
+
}
|
|
6178
|
+
function getSessionMessages(agentId) {
|
|
6179
|
+
return sessions.get(agentId) ?? [];
|
|
6180
|
+
}
|
|
6181
|
+
function persistTurnToTranscript(store, loc, sessionId, turn, onCompact) {
|
|
6182
|
+
const key = transcriptKey(loc.cwd, loc.agentId);
|
|
6183
|
+
const chained = (pendingWrites.get(key) ?? Promise.resolve()).then(async () => {
|
|
6184
|
+
try {
|
|
6185
|
+
await persistTurn(store, loc, sessionId, turn);
|
|
6186
|
+
const count = (recordCounts.get(key) ?? 0) + 1;
|
|
6187
|
+
recordCounts.set(key, count);
|
|
6188
|
+
if (turn.autoCompact !== void 0) {
|
|
6189
|
+
const { autoCompactIfNeeded: autoCompactIfNeeded2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
6190
|
+
const fired = await autoCompactIfNeeded2({
|
|
6191
|
+
store,
|
|
6192
|
+
loc,
|
|
6193
|
+
sessionId,
|
|
6194
|
+
usageTotal: turn.autoCompact.usageTotal,
|
|
6195
|
+
contextWindow: turn.autoCompact.contextWindow,
|
|
6196
|
+
turnCount: count,
|
|
6197
|
+
summarize: turn.autoCompact.summarize
|
|
6198
|
+
});
|
|
6199
|
+
if (fired) onCompact?.();
|
|
6200
|
+
}
|
|
6201
|
+
} catch (cause) {
|
|
6202
|
+
const msg = cause instanceof Error ? cause.message : String(cause);
|
|
6203
|
+
process.stderr.write(
|
|
6204
|
+
`[theokit-sdk] session transcript write failed (${loc.agentId}): ${msg}
|
|
6205
|
+
`
|
|
6206
|
+
);
|
|
6207
|
+
}
|
|
6208
|
+
});
|
|
6209
|
+
pendingWrites.set(
|
|
6210
|
+
key,
|
|
6211
|
+
chained.then(
|
|
6212
|
+
() => void 0,
|
|
6213
|
+
() => void 0
|
|
6214
|
+
)
|
|
6215
|
+
);
|
|
6216
|
+
}
|
|
6217
|
+
async function hydrateSession(agentId, loc) {
|
|
6218
|
+
const key = transcriptKey(loc.cwd, agentId);
|
|
6219
|
+
if (hydratedKeys.has(key)) return;
|
|
6220
|
+
hydratedKeys.add(key);
|
|
6221
|
+
const persisted = await readSessionMessages(loc.store, agentId);
|
|
6222
|
+
if (persisted.length === 0) return;
|
|
6223
|
+
if (!sessions.has(agentId) || sessions.get(agentId)?.length === 0) {
|
|
6224
|
+
sessions.set(agentId, persisted);
|
|
6225
|
+
}
|
|
6226
|
+
}
|
|
6227
|
+
async function flushSessionWrites() {
|
|
6228
|
+
while (pendingWrites.size > 0) {
|
|
6229
|
+
const all = Array.from(pendingWrites.values());
|
|
6230
|
+
pendingWrites.clear();
|
|
6231
|
+
await Promise.all(all);
|
|
6232
|
+
}
|
|
6233
|
+
}
|
|
6234
|
+
function clearSession(agentId) {
|
|
6235
|
+
sessions.delete(agentId);
|
|
6236
|
+
}
|
|
6237
|
+
function invalidateSessionCache(cwd, agentId) {
|
|
6238
|
+
sessions.delete(agentId);
|
|
6239
|
+
hydratedKeys.delete(transcriptKey(cwd, agentId));
|
|
6240
|
+
}
|
|
6241
|
+
function enqueueSessionWrite(cwd, agentId, fn) {
|
|
6242
|
+
const key = transcriptKey(cwd, agentId);
|
|
6243
|
+
const prior = pendingWrites.get(key) ?? Promise.resolve();
|
|
6244
|
+
const result = prior.then(fn);
|
|
6245
|
+
pendingWrites.set(
|
|
6246
|
+
key,
|
|
6247
|
+
result.then(
|
|
6248
|
+
() => void 0,
|
|
6249
|
+
() => void 0
|
|
6250
|
+
)
|
|
6251
|
+
);
|
|
6252
|
+
return result;
|
|
6253
|
+
}
|
|
6254
|
+
function clearAllSessions() {
|
|
6255
|
+
sessions.clear();
|
|
6256
|
+
hydratedKeys.clear();
|
|
6257
|
+
recordCounts.clear();
|
|
6258
|
+
const g = globalThis;
|
|
6259
|
+
const sym = /* @__PURE__ */ Symbol.for("theokit-sdk.session.auto-compact-attempts");
|
|
6260
|
+
g[sym]?.clear();
|
|
6261
|
+
}
|
|
6262
|
+
var sessions, hydratedKeys, pendingWrites, recordCounts;
|
|
6263
|
+
var init_agent_session = __esm({
|
|
6264
|
+
"src/internal/session/agent-session.ts"() {
|
|
6265
|
+
init_agent_session_store();
|
|
6266
|
+
sessions = /* @__PURE__ */ new Map();
|
|
6267
|
+
hydratedKeys = /* @__PURE__ */ new Set();
|
|
6268
|
+
pendingWrites = /* @__PURE__ */ new Map();
|
|
6269
|
+
recordCounts = /* @__PURE__ */ new Map();
|
|
6270
|
+
}
|
|
6271
|
+
});
|
|
6043
6272
|
async function withToolWhitelist(whitelist, fn) {
|
|
6044
6273
|
return toolWhitelistStore.run(whitelist, fn);
|
|
6045
6274
|
}
|
|
@@ -10419,147 +10648,6 @@ async function writeSessionSummary(input) {
|
|
|
10419
10648
|
await replaceFileAtomic(path, body);
|
|
10420
10649
|
}
|
|
10421
10650
|
|
|
10422
|
-
// src/internal/session/agent-session-store.ts
|
|
10423
|
-
init_session_transcript();
|
|
10424
|
-
function seedTranscript(prior, opts) {
|
|
10425
|
-
return SessionTranscript.fromRecords(prior, opts);
|
|
10426
|
-
}
|
|
10427
|
-
function mapAgentTurn(steps) {
|
|
10428
|
-
const assistant = {};
|
|
10429
|
-
const toolResults = [];
|
|
10430
|
-
const toolCalls = [];
|
|
10431
|
-
for (const step of steps) {
|
|
10432
|
-
if (step.type === "thinkingMessage") assistant.thinking = step.message.text;
|
|
10433
|
-
else if (step.type === "assistantMessage") assistant.text = step.message.text;
|
|
10434
|
-
else if (step.type === "toolCall")
|
|
10435
|
-
toolCalls.push({
|
|
10436
|
-
id: step.message.callId,
|
|
10437
|
-
name: step.message.name,
|
|
10438
|
-
input: step.message.args ?? {}
|
|
10439
|
-
});
|
|
10440
|
-
else
|
|
10441
|
-
toolResults.push({
|
|
10442
|
-
toolUseId: step.message.callId,
|
|
10443
|
-
content: step.message.result,
|
|
10444
|
-
isError: step.message.isError
|
|
10445
|
-
});
|
|
10446
|
-
}
|
|
10447
|
-
if (toolCalls.length > 0) assistant.toolCalls = toolCalls;
|
|
10448
|
-
return { assistant, toolResults };
|
|
10449
|
-
}
|
|
10450
|
-
function hasAssistantContent(a) {
|
|
10451
|
-
return a.text !== void 0 || a.thinking !== void 0 || (a.toolCalls?.length ?? 0) > 0;
|
|
10452
|
-
}
|
|
10453
|
-
function appendConversation(transcript, conversation) {
|
|
10454
|
-
for (const ct of conversation) {
|
|
10455
|
-
if (ct.type !== "agentConversationTurn") continue;
|
|
10456
|
-
const { assistant, toolResults } = mapAgentTurn(ct.turn.steps);
|
|
10457
|
-
if (hasAssistantContent(assistant)) transcript.appendAssistantTurn(assistant);
|
|
10458
|
-
if (toolResults.length > 0) transcript.appendToolResults(toolResults);
|
|
10459
|
-
}
|
|
10460
|
-
}
|
|
10461
|
-
async function readSessionMessages(store, agentId) {
|
|
10462
|
-
const records = await store.readRecords(agentId);
|
|
10463
|
-
return reconstructMessages(records).map(narrowToSessionMessage);
|
|
10464
|
-
}
|
|
10465
|
-
function partToText(p) {
|
|
10466
|
-
if (p.type === "text") return p.text ?? "";
|
|
10467
|
-
if (p.type === "tool_use") return `[tool call] ${p.name ?? ""}`;
|
|
10468
|
-
if (p.type === "tool_result") {
|
|
10469
|
-
const body = typeof p.content === "string" ? p.content : JSON.stringify(p.content);
|
|
10470
|
-
return `[tool result] ${body}`;
|
|
10471
|
-
}
|
|
10472
|
-
return "";
|
|
10473
|
-
}
|
|
10474
|
-
function narrowToSessionMessage(m) {
|
|
10475
|
-
const role = m.role === "user" ? "user" : "assistant";
|
|
10476
|
-
const text = m.content.map(partToText).filter((s) => s.length > 0).join("\n");
|
|
10477
|
-
return { role, text };
|
|
10478
|
-
}
|
|
10479
|
-
function deltaRecords(transcript, priorLength) {
|
|
10480
|
-
return transcript.records().slice(priorLength);
|
|
10481
|
-
}
|
|
10482
|
-
async function persistTurn(store, loc, sessionId, turn) {
|
|
10483
|
-
const prior = await store.readRecords(loc.agentId);
|
|
10484
|
-
const transcript = seedTranscript(prior, { cwd: loc.cwd, sessionId, model: loc.model });
|
|
10485
|
-
transcript.appendUserTurn(turn.userText);
|
|
10486
|
-
appendConversation(transcript, turn.conversation);
|
|
10487
|
-
await store.appendRecords(loc.agentId, deltaRecords(transcript, prior.length));
|
|
10488
|
-
}
|
|
10489
|
-
|
|
10490
|
-
// src/internal/session/agent-session.ts
|
|
10491
|
-
var sessions = /* @__PURE__ */ new Map();
|
|
10492
|
-
var hydratedKeys = /* @__PURE__ */ new Set();
|
|
10493
|
-
var pendingWrites = /* @__PURE__ */ new Map();
|
|
10494
|
-
var recordCounts = /* @__PURE__ */ new Map();
|
|
10495
|
-
function transcriptKey(cwd, agentId) {
|
|
10496
|
-
return `${cwd}::${agentId}`;
|
|
10497
|
-
}
|
|
10498
|
-
function appendSessionMessage(agentId, message) {
|
|
10499
|
-
const existing = sessions.get(agentId) ?? [];
|
|
10500
|
-
existing.push(message);
|
|
10501
|
-
sessions.set(agentId, existing);
|
|
10502
|
-
}
|
|
10503
|
-
function getSessionMessages(agentId) {
|
|
10504
|
-
return sessions.get(agentId) ?? [];
|
|
10505
|
-
}
|
|
10506
|
-
function persistTurnToTranscript(store, loc, sessionId, turn, onCompact) {
|
|
10507
|
-
const key = transcriptKey(loc.cwd, loc.agentId);
|
|
10508
|
-
const chained = (pendingWrites.get(key) ?? Promise.resolve()).then(async () => {
|
|
10509
|
-
try {
|
|
10510
|
-
await persistTurn(store, loc, sessionId, turn);
|
|
10511
|
-
const count = (recordCounts.get(key) ?? 0) + 1;
|
|
10512
|
-
recordCounts.set(key, count);
|
|
10513
|
-
if (turn.autoCompact !== void 0) {
|
|
10514
|
-
const { autoCompactIfNeeded: autoCompactIfNeeded2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
10515
|
-
const fired = await autoCompactIfNeeded2({
|
|
10516
|
-
store,
|
|
10517
|
-
loc,
|
|
10518
|
-
sessionId,
|
|
10519
|
-
usageTotal: turn.autoCompact.usageTotal,
|
|
10520
|
-
contextWindow: turn.autoCompact.contextWindow,
|
|
10521
|
-
turnCount: count,
|
|
10522
|
-
summarize: turn.autoCompact.summarize
|
|
10523
|
-
});
|
|
10524
|
-
if (fired) onCompact?.();
|
|
10525
|
-
}
|
|
10526
|
-
} catch (cause) {
|
|
10527
|
-
const msg = cause instanceof Error ? cause.message : String(cause);
|
|
10528
|
-
process.stderr.write(
|
|
10529
|
-
`[theokit-sdk] session transcript write failed (${loc.agentId}): ${msg}
|
|
10530
|
-
`
|
|
10531
|
-
);
|
|
10532
|
-
}
|
|
10533
|
-
});
|
|
10534
|
-
pendingWrites.set(
|
|
10535
|
-
key,
|
|
10536
|
-
chained.then(
|
|
10537
|
-
() => void 0,
|
|
10538
|
-
() => void 0
|
|
10539
|
-
)
|
|
10540
|
-
);
|
|
10541
|
-
}
|
|
10542
|
-
async function hydrateSession(agentId, loc) {
|
|
10543
|
-
const key = transcriptKey(loc.cwd, agentId);
|
|
10544
|
-
if (hydratedKeys.has(key)) return;
|
|
10545
|
-
hydratedKeys.add(key);
|
|
10546
|
-
const persisted = await readSessionMessages(loc.store, agentId);
|
|
10547
|
-
if (persisted.length === 0) return;
|
|
10548
|
-
if (!sessions.has(agentId) || sessions.get(agentId)?.length === 0) {
|
|
10549
|
-
sessions.set(agentId, persisted);
|
|
10550
|
-
}
|
|
10551
|
-
}
|
|
10552
|
-
async function flushSessionWrites() {
|
|
10553
|
-
while (pendingWrites.size > 0) {
|
|
10554
|
-
const all = Array.from(pendingWrites.values());
|
|
10555
|
-
pendingWrites.clear();
|
|
10556
|
-
await Promise.all(all);
|
|
10557
|
-
}
|
|
10558
|
-
}
|
|
10559
|
-
function clearSession(agentId) {
|
|
10560
|
-
sessions.delete(agentId);
|
|
10561
|
-
}
|
|
10562
|
-
|
|
10563
10651
|
// src/internal/runtime/memory/memory-path-selector.ts
|
|
10564
10652
|
var PORT_MEMORY_PATH_ENV_VAR = "THEOKIT_PORT_MEMORY_PATH";
|
|
10565
10653
|
function shouldUsePortMemoryPath() {
|
|
@@ -10610,6 +10698,20 @@ async function runPostRunLifecycle(inputs) {
|
|
|
10610
10698
|
const { getCatalogModelInfo: getCatalogModelInfo2 } = await Promise.resolve().then(() => (init_catalog_loader(), catalog_loader_exports));
|
|
10611
10699
|
const { buildDefaultSummarizer: buildDefaultSummarizer2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
10612
10700
|
const contextWindow = getCatalogModelInfo2(model)?.limit?.context;
|
|
10701
|
+
if (contextWindow === void 0) {
|
|
10702
|
+
const g = globalThis;
|
|
10703
|
+
const sym = /* @__PURE__ */ Symbol.for("theokit-sdk.compact.no-cw-warned");
|
|
10704
|
+
const warned3 = g[sym] ??= /* @__PURE__ */ new Set();
|
|
10705
|
+
if (!warned3.has(model)) {
|
|
10706
|
+
warned3.add(model);
|
|
10707
|
+
process.stderr.write(
|
|
10708
|
+
`[theokit-sdk] auto-compaction disabled: model "${model}" has no context-window entry in the catalog
|
|
10709
|
+
`
|
|
10710
|
+
);
|
|
10711
|
+
}
|
|
10712
|
+
}
|
|
10713
|
+
const lastRequestUsage = result.usage?.requests?.at(-1)?.totalTokens;
|
|
10714
|
+
const usageForTrigger = lastRequestUsage ?? result.usage?.totalTokens;
|
|
10613
10715
|
persistTurnToTranscript(
|
|
10614
10716
|
sessionStore,
|
|
10615
10717
|
{ cwd: workspaceCwd, agentId, model },
|
|
@@ -10618,7 +10720,7 @@ async function runPostRunLifecycle(inputs) {
|
|
|
10618
10720
|
userText,
|
|
10619
10721
|
conversation,
|
|
10620
10722
|
autoCompact: {
|
|
10621
|
-
usageTotal:
|
|
10723
|
+
usageTotal: usageForTrigger,
|
|
10622
10724
|
contextWindow,
|
|
10623
10725
|
summarize: buildDefaultSummarizer2({
|
|
10624
10726
|
agentModel: model,
|
|
@@ -19902,7 +20004,8 @@ var Agent = class _Agent {
|
|
|
19902
20004
|
const { defaultBaseDir: defaultBaseDir2, expandTilde: expandTilde2 } = await Promise.resolve().then(() => (init_session_transcript(), session_transcript_exports));
|
|
19903
20005
|
const baseDir = reg.options.local?.baseDir !== void 0 ? expandTilde2(reg.options.local.baseDir) : defaultBaseDir2();
|
|
19904
20006
|
const store = new FsSessionStore2({ baseDir, cwd });
|
|
19905
|
-
|
|
20007
|
+
const { enqueueSessionWrite: enqueueSessionWrite2 } = await Promise.resolve().then(() => (init_agent_session(), agent_session_exports));
|
|
20008
|
+
return enqueueSessionWrite2(cwd, agentId, () => compactSessionTranscript2({
|
|
19906
20009
|
store,
|
|
19907
20010
|
loc: { cwd, agentId, model },
|
|
19908
20011
|
sessionId: agentId,
|
|
@@ -19911,7 +20014,7 @@ var Agent = class _Agent {
|
|
|
19911
20014
|
agentModel: model,
|
|
19912
20015
|
...reg.options.apiKey !== void 0 ? { apiKey: reg.options.apiKey } : {}
|
|
19913
20016
|
})
|
|
19914
|
-
});
|
|
20017
|
+
}));
|
|
19915
20018
|
}
|
|
19916
20019
|
/**
|
|
19917
20020
|
* Permanently delete a cloud agent.
|