@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/dist/cron.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { z, toJSONSchema } from 'zod';
|
|
2
2
|
import { createRequire } from 'module';
|
|
3
3
|
import { randomUUID, randomBytes, createHash } from 'crypto';
|
|
4
|
-
import { mkdir, readFile, open, rename, unlink,
|
|
4
|
+
import { mkdir, readFile, open, rename, unlink, readdir, statfs, stat, access } from 'fs/promises';
|
|
5
5
|
import { join, dirname, resolve, sep, relative, isAbsolute } from 'path';
|
|
6
6
|
import { readFileSync, existsSync, statSync, realpathSync, mkdirSync, chmodSync, openSync, writeFileSync, fsyncSync, closeSync, renameSync, unlinkSync, lstatSync, readlinkSync, readdirSync } from 'fs';
|
|
7
7
|
import { homedir } from 'os';
|
|
@@ -1603,6 +1603,78 @@ var init_run_events = __esm({
|
|
|
1603
1603
|
}
|
|
1604
1604
|
});
|
|
1605
1605
|
|
|
1606
|
+
// src/internal/session/agent-session-store.ts
|
|
1607
|
+
function seedTranscript(prior, opts) {
|
|
1608
|
+
return SessionTranscript.fromRecords(prior, opts);
|
|
1609
|
+
}
|
|
1610
|
+
function mapAgentTurn(steps) {
|
|
1611
|
+
const assistant = {};
|
|
1612
|
+
const toolResults = [];
|
|
1613
|
+
const toolCalls = [];
|
|
1614
|
+
for (const step of steps) {
|
|
1615
|
+
if (step.type === "thinkingMessage") assistant.thinking = step.message.text;
|
|
1616
|
+
else if (step.type === "assistantMessage") assistant.text = step.message.text;
|
|
1617
|
+
else if (step.type === "toolCall")
|
|
1618
|
+
toolCalls.push({
|
|
1619
|
+
id: step.message.callId,
|
|
1620
|
+
name: step.message.name,
|
|
1621
|
+
input: step.message.args ?? {}
|
|
1622
|
+
});
|
|
1623
|
+
else
|
|
1624
|
+
toolResults.push({
|
|
1625
|
+
toolUseId: step.message.callId,
|
|
1626
|
+
content: step.message.result,
|
|
1627
|
+
isError: step.message.isError
|
|
1628
|
+
});
|
|
1629
|
+
}
|
|
1630
|
+
if (toolCalls.length > 0) assistant.toolCalls = toolCalls;
|
|
1631
|
+
return { assistant, toolResults };
|
|
1632
|
+
}
|
|
1633
|
+
function hasAssistantContent(a) {
|
|
1634
|
+
return a.text !== void 0 || a.thinking !== void 0 || (a.toolCalls?.length ?? 0) > 0;
|
|
1635
|
+
}
|
|
1636
|
+
function appendConversation(transcript, conversation) {
|
|
1637
|
+
for (const ct of conversation) {
|
|
1638
|
+
if (ct.type !== "agentConversationTurn") continue;
|
|
1639
|
+
const { assistant, toolResults } = mapAgentTurn(ct.turn.steps);
|
|
1640
|
+
if (hasAssistantContent(assistant)) transcript.appendAssistantTurn(assistant);
|
|
1641
|
+
if (toolResults.length > 0) transcript.appendToolResults(toolResults);
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
async function readSessionMessages(store, agentId) {
|
|
1645
|
+
const records = await store.readRecords(agentId);
|
|
1646
|
+
return reconstructMessages(records).map(narrowToSessionMessage);
|
|
1647
|
+
}
|
|
1648
|
+
function partToText(p) {
|
|
1649
|
+
if (p.type === "text") return p.text ?? "";
|
|
1650
|
+
if (p.type === "tool_use") return `[tool call] ${p.name ?? ""}`;
|
|
1651
|
+
if (p.type === "tool_result") {
|
|
1652
|
+
const body = typeof p.content === "string" ? p.content : JSON.stringify(p.content);
|
|
1653
|
+
return `[tool result] ${body}`;
|
|
1654
|
+
}
|
|
1655
|
+
return "";
|
|
1656
|
+
}
|
|
1657
|
+
function narrowToSessionMessage(m) {
|
|
1658
|
+
const role = m.role === "user" ? "user" : "assistant";
|
|
1659
|
+
const text = m.content.map(partToText).filter((s) => s.length > 0).join("\n");
|
|
1660
|
+
return { role, text };
|
|
1661
|
+
}
|
|
1662
|
+
function deltaRecords(transcript, priorLength) {
|
|
1663
|
+
return transcript.records().slice(priorLength);
|
|
1664
|
+
}
|
|
1665
|
+
async function persistTurn(store, loc, sessionId, turn) {
|
|
1666
|
+
const prior = await store.readRecords(loc.agentId);
|
|
1667
|
+
const transcript = seedTranscript(prior, { cwd: loc.cwd, sessionId, model: loc.model });
|
|
1668
|
+
transcript.appendUserTurn(turn.userText);
|
|
1669
|
+
appendConversation(transcript, turn.conversation);
|
|
1670
|
+
await store.appendRecords(loc.agentId, deltaRecords(transcript, prior.length));
|
|
1671
|
+
}
|
|
1672
|
+
var init_agent_session_store = __esm({
|
|
1673
|
+
"src/internal/session/agent-session-store.ts"() {
|
|
1674
|
+
init_session_transcript();
|
|
1675
|
+
}
|
|
1676
|
+
});
|
|
1677
|
+
|
|
1606
1678
|
// src/compaction.ts
|
|
1607
1679
|
function estimateTokens(text) {
|
|
1608
1680
|
return Math.ceil(text.length / 4);
|
|
@@ -1803,6 +1875,13 @@ function getProviderProfile(name) {
|
|
|
1803
1875
|
const canonical = ALIASES.get(name) ?? name;
|
|
1804
1876
|
return REGISTRY.get(canonical);
|
|
1805
1877
|
}
|
|
1878
|
+
function listProviders() {
|
|
1879
|
+
return Array.from(REGISTRY.values());
|
|
1880
|
+
}
|
|
1881
|
+
function _resetProvidersForTests() {
|
|
1882
|
+
REGISTRY.clear();
|
|
1883
|
+
ALIASES.clear();
|
|
1884
|
+
}
|
|
1806
1885
|
var REGISTRY, ALIASES;
|
|
1807
1886
|
var init_registry = __esm({
|
|
1808
1887
|
"src/internal/providers/registry.ts"() {
|
|
@@ -2735,6 +2814,9 @@ function registerBuiltins() {
|
|
|
2735
2814
|
registerProvider(CEREBRAS);
|
|
2736
2815
|
registerCatalogProviders();
|
|
2737
2816
|
}
|
|
2817
|
+
function _resetBuiltinsRegistered() {
|
|
2818
|
+
_registeredState.done = false;
|
|
2819
|
+
}
|
|
2738
2820
|
var _registeredState;
|
|
2739
2821
|
var init_builtin = __esm({
|
|
2740
2822
|
"src/internal/providers/builtin/index.ts"() {
|
|
@@ -2861,6 +2943,9 @@ async function loadOne(dir, entryName) {
|
|
|
2861
2943
|
}
|
|
2862
2944
|
}
|
|
2863
2945
|
}
|
|
2946
|
+
function _resetDiscovery() {
|
|
2947
|
+
discoveryState.done = false;
|
|
2948
|
+
}
|
|
2864
2949
|
var discoveryState;
|
|
2865
2950
|
var init_discovery = __esm({
|
|
2866
2951
|
"src/internal/providers/discovery.ts"() {
|
|
@@ -2872,9 +2957,25 @@ var init_discovery = __esm({
|
|
|
2872
2957
|
});
|
|
2873
2958
|
|
|
2874
2959
|
// src/internal/providers/index.ts
|
|
2960
|
+
var providers_exports = {};
|
|
2961
|
+
__export(providers_exports, {
|
|
2962
|
+
ANTHROPIC: () => ANTHROPIC,
|
|
2963
|
+
GEMINI: () => GEMINI,
|
|
2964
|
+
OPENAI: () => OPENAI,
|
|
2965
|
+
OPENROUTER: () => OPENROUTER,
|
|
2966
|
+
_resetBuiltinsRegistered: () => _resetBuiltinsRegistered,
|
|
2967
|
+
_resetDiscovery: () => _resetDiscovery,
|
|
2968
|
+
_resetProvidersForTests: () => _resetProvidersForTests,
|
|
2969
|
+
discoverProviderPlugins: () => discoverProviderPlugins,
|
|
2970
|
+
getProviderProfile: () => getProviderProfile,
|
|
2971
|
+
listProviders: () => listProviders,
|
|
2972
|
+
registerBuiltins: () => registerBuiltins,
|
|
2973
|
+
registerProvider: () => registerProvider
|
|
2974
|
+
});
|
|
2875
2975
|
var init_providers = __esm({
|
|
2876
2976
|
"src/internal/providers/index.ts"() {
|
|
2877
2977
|
init_builtin();
|
|
2978
|
+
init_discovery();
|
|
2878
2979
|
init_registry();
|
|
2879
2980
|
}
|
|
2880
2981
|
});
|
|
@@ -5933,7 +6034,14 @@ async function compactSessionTranscript(opts) {
|
|
|
5933
6034
|
const m = compressible[i];
|
|
5934
6035
|
if (m === void 0 || m.role !== "user" || isCompactSummary(m.content)) continue;
|
|
5935
6036
|
const cost = estimateTokens(m.content);
|
|
5936
|
-
if (budget + cost > COMPACT_USER_MESSAGE_MAX_TOKENS)
|
|
6037
|
+
if (budget + cost > COMPACT_USER_MESSAGE_MAX_TOKENS) {
|
|
6038
|
+
const remaining = COMPACT_USER_MESSAGE_MAX_TOKENS - budget;
|
|
6039
|
+
if (remaining > 50) {
|
|
6040
|
+
preserved.unshift(`${m.content.slice(0, remaining * 4)}
|
|
6041
|
+
[...truncated for compaction...]`);
|
|
6042
|
+
}
|
|
6043
|
+
break;
|
|
6044
|
+
}
|
|
5937
6045
|
budget += cost;
|
|
5938
6046
|
preserved.unshift(m.content);
|
|
5939
6047
|
}
|
|
@@ -5949,6 +6057,8 @@ ${summaryBody}`;
|
|
|
5949
6057
|
transcript.appendUserTurn(summary);
|
|
5950
6058
|
const delta = transcript.records().slice(prior.length);
|
|
5951
6059
|
await opts.store.appendRecords(opts.loc.agentId, delta);
|
|
6060
|
+
const { invalidateSessionCache: invalidateSessionCache2 } = await Promise.resolve().then(() => (init_agent_session(), agent_session_exports));
|
|
6061
|
+
invalidateSessionCache2(opts.loc.cwd, opts.loc.agentId);
|
|
5952
6062
|
const postTokens = estimateTokens([...preserved, summary].join("\n"));
|
|
5953
6063
|
return { preTokens, postTokens };
|
|
5954
6064
|
}
|
|
@@ -5960,7 +6070,10 @@ function buildDefaultSummarizer(opts) {
|
|
|
5960
6070
|
const { inferProviderFromApiKey: inferProviderFromApiKey2, detectPrimaryProvider: detectPrimaryProvider2 } = await Promise.resolve().then(() => (init_real_local_run_provider(), real_local_run_provider_exports));
|
|
5961
6071
|
const keyProvider = inferProviderFromApiKey2(opts.apiKey);
|
|
5962
6072
|
const modelPrefix = opts.agentModel.includes("/") ? opts.agentModel.slice(0, opts.agentModel.indexOf("/")) : void 0;
|
|
5963
|
-
const
|
|
6073
|
+
const { registerBuiltins: registerBuiltins2, getProviderProfile: getProviderProfile2 } = await Promise.resolve().then(() => (init_providers(), providers_exports));
|
|
6074
|
+
registerBuiltins2();
|
|
6075
|
+
const prefixProfile = modelPrefix !== void 0 ? getProviderProfile2(modelPrefix) : void 0;
|
|
6076
|
+
const provider = prefixProfile !== void 0 ? modelPrefix : keyProvider ?? detectPrimaryProvider2();
|
|
5964
6077
|
let model;
|
|
5965
6078
|
if (provider !== modelPrefix) {
|
|
5966
6079
|
model = opts.agentModel;
|
|
@@ -6037,6 +6150,122 @@ var init_compact_session = __esm({
|
|
|
6037
6150
|
})();
|
|
6038
6151
|
}
|
|
6039
6152
|
});
|
|
6153
|
+
|
|
6154
|
+
// src/internal/session/agent-session.ts
|
|
6155
|
+
var agent_session_exports = {};
|
|
6156
|
+
__export(agent_session_exports, {
|
|
6157
|
+
appendSessionMessage: () => appendSessionMessage,
|
|
6158
|
+
clearAllSessions: () => clearAllSessions,
|
|
6159
|
+
clearSession: () => clearSession,
|
|
6160
|
+
enqueueSessionWrite: () => enqueueSessionWrite,
|
|
6161
|
+
flushSessionWrites: () => flushSessionWrites,
|
|
6162
|
+
getSessionMessages: () => getSessionMessages,
|
|
6163
|
+
hydrateSession: () => hydrateSession,
|
|
6164
|
+
invalidateSessionCache: () => invalidateSessionCache,
|
|
6165
|
+
persistTurnToTranscript: () => persistTurnToTranscript
|
|
6166
|
+
});
|
|
6167
|
+
function transcriptKey(cwd, agentId) {
|
|
6168
|
+
return `${cwd}::${agentId}`;
|
|
6169
|
+
}
|
|
6170
|
+
function appendSessionMessage(agentId, message) {
|
|
6171
|
+
const existing = sessions.get(agentId) ?? [];
|
|
6172
|
+
existing.push(message);
|
|
6173
|
+
sessions.set(agentId, existing);
|
|
6174
|
+
}
|
|
6175
|
+
function getSessionMessages(agentId) {
|
|
6176
|
+
return sessions.get(agentId) ?? [];
|
|
6177
|
+
}
|
|
6178
|
+
function persistTurnToTranscript(store, loc, sessionId, turn, onCompact) {
|
|
6179
|
+
const key = transcriptKey(loc.cwd, loc.agentId);
|
|
6180
|
+
const chained = (pendingWrites.get(key) ?? Promise.resolve()).then(async () => {
|
|
6181
|
+
try {
|
|
6182
|
+
await persistTurn(store, loc, sessionId, turn);
|
|
6183
|
+
const count = (recordCounts.get(key) ?? 0) + 1;
|
|
6184
|
+
recordCounts.set(key, count);
|
|
6185
|
+
if (turn.autoCompact !== void 0) {
|
|
6186
|
+
const { autoCompactIfNeeded: autoCompactIfNeeded2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
6187
|
+
const fired = await autoCompactIfNeeded2({
|
|
6188
|
+
store,
|
|
6189
|
+
loc,
|
|
6190
|
+
sessionId,
|
|
6191
|
+
usageTotal: turn.autoCompact.usageTotal,
|
|
6192
|
+
contextWindow: turn.autoCompact.contextWindow,
|
|
6193
|
+
turnCount: count,
|
|
6194
|
+
summarize: turn.autoCompact.summarize
|
|
6195
|
+
});
|
|
6196
|
+
if (fired) onCompact?.();
|
|
6197
|
+
}
|
|
6198
|
+
} catch (cause) {
|
|
6199
|
+
const msg = cause instanceof Error ? cause.message : String(cause);
|
|
6200
|
+
process.stderr.write(
|
|
6201
|
+
`[theokit-sdk] session transcript write failed (${loc.agentId}): ${msg}
|
|
6202
|
+
`
|
|
6203
|
+
);
|
|
6204
|
+
}
|
|
6205
|
+
});
|
|
6206
|
+
pendingWrites.set(
|
|
6207
|
+
key,
|
|
6208
|
+
chained.then(
|
|
6209
|
+
() => void 0,
|
|
6210
|
+
() => void 0
|
|
6211
|
+
)
|
|
6212
|
+
);
|
|
6213
|
+
}
|
|
6214
|
+
async function hydrateSession(agentId, loc) {
|
|
6215
|
+
const key = transcriptKey(loc.cwd, agentId);
|
|
6216
|
+
if (hydratedKeys.has(key)) return;
|
|
6217
|
+
hydratedKeys.add(key);
|
|
6218
|
+
const persisted = await readSessionMessages(loc.store, agentId);
|
|
6219
|
+
if (persisted.length === 0) return;
|
|
6220
|
+
if (!sessions.has(agentId) || sessions.get(agentId)?.length === 0) {
|
|
6221
|
+
sessions.set(agentId, persisted);
|
|
6222
|
+
}
|
|
6223
|
+
}
|
|
6224
|
+
async function flushSessionWrites() {
|
|
6225
|
+
while (pendingWrites.size > 0) {
|
|
6226
|
+
const all = Array.from(pendingWrites.values());
|
|
6227
|
+
pendingWrites.clear();
|
|
6228
|
+
await Promise.all(all);
|
|
6229
|
+
}
|
|
6230
|
+
}
|
|
6231
|
+
function clearSession(agentId) {
|
|
6232
|
+
sessions.delete(agentId);
|
|
6233
|
+
}
|
|
6234
|
+
function invalidateSessionCache(cwd, agentId) {
|
|
6235
|
+
sessions.delete(agentId);
|
|
6236
|
+
hydratedKeys.delete(transcriptKey(cwd, agentId));
|
|
6237
|
+
}
|
|
6238
|
+
function enqueueSessionWrite(cwd, agentId, fn) {
|
|
6239
|
+
const key = transcriptKey(cwd, agentId);
|
|
6240
|
+
const prior = pendingWrites.get(key) ?? Promise.resolve();
|
|
6241
|
+
const result = prior.then(fn);
|
|
6242
|
+
pendingWrites.set(
|
|
6243
|
+
key,
|
|
6244
|
+
result.then(
|
|
6245
|
+
() => void 0,
|
|
6246
|
+
() => void 0
|
|
6247
|
+
)
|
|
6248
|
+
);
|
|
6249
|
+
return result;
|
|
6250
|
+
}
|
|
6251
|
+
function clearAllSessions() {
|
|
6252
|
+
sessions.clear();
|
|
6253
|
+
hydratedKeys.clear();
|
|
6254
|
+
recordCounts.clear();
|
|
6255
|
+
const g = globalThis;
|
|
6256
|
+
const sym = /* @__PURE__ */ Symbol.for("theokit-sdk.session.auto-compact-attempts");
|
|
6257
|
+
g[sym]?.clear();
|
|
6258
|
+
}
|
|
6259
|
+
var sessions, hydratedKeys, pendingWrites, recordCounts;
|
|
6260
|
+
var init_agent_session = __esm({
|
|
6261
|
+
"src/internal/session/agent-session.ts"() {
|
|
6262
|
+
init_agent_session_store();
|
|
6263
|
+
sessions = /* @__PURE__ */ new Map();
|
|
6264
|
+
hydratedKeys = /* @__PURE__ */ new Set();
|
|
6265
|
+
pendingWrites = /* @__PURE__ */ new Map();
|
|
6266
|
+
recordCounts = /* @__PURE__ */ new Map();
|
|
6267
|
+
}
|
|
6268
|
+
});
|
|
6040
6269
|
async function withToolWhitelist(whitelist, fn) {
|
|
6041
6270
|
return toolWhitelistStore.run(whitelist, fn);
|
|
6042
6271
|
}
|
|
@@ -10416,147 +10645,6 @@ async function writeSessionSummary(input) {
|
|
|
10416
10645
|
await replaceFileAtomic(path, body);
|
|
10417
10646
|
}
|
|
10418
10647
|
|
|
10419
|
-
// src/internal/session/agent-session-store.ts
|
|
10420
|
-
init_session_transcript();
|
|
10421
|
-
function seedTranscript(prior, opts) {
|
|
10422
|
-
return SessionTranscript.fromRecords(prior, opts);
|
|
10423
|
-
}
|
|
10424
|
-
function mapAgentTurn(steps) {
|
|
10425
|
-
const assistant = {};
|
|
10426
|
-
const toolResults = [];
|
|
10427
|
-
const toolCalls = [];
|
|
10428
|
-
for (const step of steps) {
|
|
10429
|
-
if (step.type === "thinkingMessage") assistant.thinking = step.message.text;
|
|
10430
|
-
else if (step.type === "assistantMessage") assistant.text = step.message.text;
|
|
10431
|
-
else if (step.type === "toolCall")
|
|
10432
|
-
toolCalls.push({
|
|
10433
|
-
id: step.message.callId,
|
|
10434
|
-
name: step.message.name,
|
|
10435
|
-
input: step.message.args ?? {}
|
|
10436
|
-
});
|
|
10437
|
-
else
|
|
10438
|
-
toolResults.push({
|
|
10439
|
-
toolUseId: step.message.callId,
|
|
10440
|
-
content: step.message.result,
|
|
10441
|
-
isError: step.message.isError
|
|
10442
|
-
});
|
|
10443
|
-
}
|
|
10444
|
-
if (toolCalls.length > 0) assistant.toolCalls = toolCalls;
|
|
10445
|
-
return { assistant, toolResults };
|
|
10446
|
-
}
|
|
10447
|
-
function hasAssistantContent(a) {
|
|
10448
|
-
return a.text !== void 0 || a.thinking !== void 0 || (a.toolCalls?.length ?? 0) > 0;
|
|
10449
|
-
}
|
|
10450
|
-
function appendConversation(transcript, conversation) {
|
|
10451
|
-
for (const ct of conversation) {
|
|
10452
|
-
if (ct.type !== "agentConversationTurn") continue;
|
|
10453
|
-
const { assistant, toolResults } = mapAgentTurn(ct.turn.steps);
|
|
10454
|
-
if (hasAssistantContent(assistant)) transcript.appendAssistantTurn(assistant);
|
|
10455
|
-
if (toolResults.length > 0) transcript.appendToolResults(toolResults);
|
|
10456
|
-
}
|
|
10457
|
-
}
|
|
10458
|
-
async function readSessionMessages(store, agentId) {
|
|
10459
|
-
const records = await store.readRecords(agentId);
|
|
10460
|
-
return reconstructMessages(records).map(narrowToSessionMessage);
|
|
10461
|
-
}
|
|
10462
|
-
function partToText(p) {
|
|
10463
|
-
if (p.type === "text") return p.text ?? "";
|
|
10464
|
-
if (p.type === "tool_use") return `[tool call] ${p.name ?? ""}`;
|
|
10465
|
-
if (p.type === "tool_result") {
|
|
10466
|
-
const body = typeof p.content === "string" ? p.content : JSON.stringify(p.content);
|
|
10467
|
-
return `[tool result] ${body}`;
|
|
10468
|
-
}
|
|
10469
|
-
return "";
|
|
10470
|
-
}
|
|
10471
|
-
function narrowToSessionMessage(m) {
|
|
10472
|
-
const role = m.role === "user" ? "user" : "assistant";
|
|
10473
|
-
const text = m.content.map(partToText).filter((s) => s.length > 0).join("\n");
|
|
10474
|
-
return { role, text };
|
|
10475
|
-
}
|
|
10476
|
-
function deltaRecords(transcript, priorLength) {
|
|
10477
|
-
return transcript.records().slice(priorLength);
|
|
10478
|
-
}
|
|
10479
|
-
async function persistTurn(store, loc, sessionId, turn) {
|
|
10480
|
-
const prior = await store.readRecords(loc.agentId);
|
|
10481
|
-
const transcript = seedTranscript(prior, { cwd: loc.cwd, sessionId, model: loc.model });
|
|
10482
|
-
transcript.appendUserTurn(turn.userText);
|
|
10483
|
-
appendConversation(transcript, turn.conversation);
|
|
10484
|
-
await store.appendRecords(loc.agentId, deltaRecords(transcript, prior.length));
|
|
10485
|
-
}
|
|
10486
|
-
|
|
10487
|
-
// src/internal/session/agent-session.ts
|
|
10488
|
-
var sessions = /* @__PURE__ */ new Map();
|
|
10489
|
-
var hydratedKeys = /* @__PURE__ */ new Set();
|
|
10490
|
-
var pendingWrites = /* @__PURE__ */ new Map();
|
|
10491
|
-
var recordCounts = /* @__PURE__ */ new Map();
|
|
10492
|
-
function transcriptKey(cwd, agentId) {
|
|
10493
|
-
return `${cwd}::${agentId}`;
|
|
10494
|
-
}
|
|
10495
|
-
function appendSessionMessage(agentId, message) {
|
|
10496
|
-
const existing = sessions.get(agentId) ?? [];
|
|
10497
|
-
existing.push(message);
|
|
10498
|
-
sessions.set(agentId, existing);
|
|
10499
|
-
}
|
|
10500
|
-
function getSessionMessages(agentId) {
|
|
10501
|
-
return sessions.get(agentId) ?? [];
|
|
10502
|
-
}
|
|
10503
|
-
function persistTurnToTranscript(store, loc, sessionId, turn, onCompact) {
|
|
10504
|
-
const key = transcriptKey(loc.cwd, loc.agentId);
|
|
10505
|
-
const chained = (pendingWrites.get(key) ?? Promise.resolve()).then(async () => {
|
|
10506
|
-
try {
|
|
10507
|
-
await persistTurn(store, loc, sessionId, turn);
|
|
10508
|
-
const count = (recordCounts.get(key) ?? 0) + 1;
|
|
10509
|
-
recordCounts.set(key, count);
|
|
10510
|
-
if (turn.autoCompact !== void 0) {
|
|
10511
|
-
const { autoCompactIfNeeded: autoCompactIfNeeded2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
10512
|
-
const fired = await autoCompactIfNeeded2({
|
|
10513
|
-
store,
|
|
10514
|
-
loc,
|
|
10515
|
-
sessionId,
|
|
10516
|
-
usageTotal: turn.autoCompact.usageTotal,
|
|
10517
|
-
contextWindow: turn.autoCompact.contextWindow,
|
|
10518
|
-
turnCount: count,
|
|
10519
|
-
summarize: turn.autoCompact.summarize
|
|
10520
|
-
});
|
|
10521
|
-
if (fired) onCompact?.();
|
|
10522
|
-
}
|
|
10523
|
-
} catch (cause) {
|
|
10524
|
-
const msg = cause instanceof Error ? cause.message : String(cause);
|
|
10525
|
-
process.stderr.write(
|
|
10526
|
-
`[theokit-sdk] session transcript write failed (${loc.agentId}): ${msg}
|
|
10527
|
-
`
|
|
10528
|
-
);
|
|
10529
|
-
}
|
|
10530
|
-
});
|
|
10531
|
-
pendingWrites.set(
|
|
10532
|
-
key,
|
|
10533
|
-
chained.then(
|
|
10534
|
-
() => void 0,
|
|
10535
|
-
() => void 0
|
|
10536
|
-
)
|
|
10537
|
-
);
|
|
10538
|
-
}
|
|
10539
|
-
async function hydrateSession(agentId, loc) {
|
|
10540
|
-
const key = transcriptKey(loc.cwd, agentId);
|
|
10541
|
-
if (hydratedKeys.has(key)) return;
|
|
10542
|
-
hydratedKeys.add(key);
|
|
10543
|
-
const persisted = await readSessionMessages(loc.store, agentId);
|
|
10544
|
-
if (persisted.length === 0) return;
|
|
10545
|
-
if (!sessions.has(agentId) || sessions.get(agentId)?.length === 0) {
|
|
10546
|
-
sessions.set(agentId, persisted);
|
|
10547
|
-
}
|
|
10548
|
-
}
|
|
10549
|
-
async function flushSessionWrites() {
|
|
10550
|
-
while (pendingWrites.size > 0) {
|
|
10551
|
-
const all = Array.from(pendingWrites.values());
|
|
10552
|
-
pendingWrites.clear();
|
|
10553
|
-
await Promise.all(all);
|
|
10554
|
-
}
|
|
10555
|
-
}
|
|
10556
|
-
function clearSession(agentId) {
|
|
10557
|
-
sessions.delete(agentId);
|
|
10558
|
-
}
|
|
10559
|
-
|
|
10560
10648
|
// src/internal/runtime/memory/memory-path-selector.ts
|
|
10561
10649
|
var PORT_MEMORY_PATH_ENV_VAR = "THEOKIT_PORT_MEMORY_PATH";
|
|
10562
10650
|
function shouldUsePortMemoryPath() {
|
|
@@ -10607,6 +10695,20 @@ async function runPostRunLifecycle(inputs) {
|
|
|
10607
10695
|
const { getCatalogModelInfo: getCatalogModelInfo2 } = await Promise.resolve().then(() => (init_catalog_loader(), catalog_loader_exports));
|
|
10608
10696
|
const { buildDefaultSummarizer: buildDefaultSummarizer2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
10609
10697
|
const contextWindow = getCatalogModelInfo2(model)?.limit?.context;
|
|
10698
|
+
if (contextWindow === void 0) {
|
|
10699
|
+
const g = globalThis;
|
|
10700
|
+
const sym = /* @__PURE__ */ Symbol.for("theokit-sdk.compact.no-cw-warned");
|
|
10701
|
+
const warned3 = g[sym] ??= /* @__PURE__ */ new Set();
|
|
10702
|
+
if (!warned3.has(model)) {
|
|
10703
|
+
warned3.add(model);
|
|
10704
|
+
process.stderr.write(
|
|
10705
|
+
`[theokit-sdk] auto-compaction disabled: model "${model}" has no context-window entry in the catalog
|
|
10706
|
+
`
|
|
10707
|
+
);
|
|
10708
|
+
}
|
|
10709
|
+
}
|
|
10710
|
+
const lastRequestUsage = result.usage?.requests?.at(-1)?.totalTokens;
|
|
10711
|
+
const usageForTrigger = lastRequestUsage ?? result.usage?.totalTokens;
|
|
10610
10712
|
persistTurnToTranscript(
|
|
10611
10713
|
sessionStore,
|
|
10612
10714
|
{ cwd: workspaceCwd, agentId, model },
|
|
@@ -10615,7 +10717,7 @@ async function runPostRunLifecycle(inputs) {
|
|
|
10615
10717
|
userText,
|
|
10616
10718
|
conversation,
|
|
10617
10719
|
autoCompact: {
|
|
10618
|
-
usageTotal:
|
|
10720
|
+
usageTotal: usageForTrigger,
|
|
10619
10721
|
contextWindow,
|
|
10620
10722
|
summarize: buildDefaultSummarizer2({
|
|
10621
10723
|
agentModel: model,
|
|
@@ -19899,7 +20001,8 @@ var Agent = class _Agent {
|
|
|
19899
20001
|
const { defaultBaseDir: defaultBaseDir2, expandTilde: expandTilde2 } = await Promise.resolve().then(() => (init_session_transcript(), session_transcript_exports));
|
|
19900
20002
|
const baseDir = reg.options.local?.baseDir !== void 0 ? expandTilde2(reg.options.local.baseDir) : defaultBaseDir2();
|
|
19901
20003
|
const store = new FsSessionStore2({ baseDir, cwd });
|
|
19902
|
-
|
|
20004
|
+
const { enqueueSessionWrite: enqueueSessionWrite2 } = await Promise.resolve().then(() => (init_agent_session(), agent_session_exports));
|
|
20005
|
+
return enqueueSessionWrite2(cwd, agentId, () => compactSessionTranscript2({
|
|
19903
20006
|
store,
|
|
19904
20007
|
loc: { cwd, agentId, model },
|
|
19905
20008
|
sessionId: agentId,
|
|
@@ -19908,7 +20011,7 @@ var Agent = class _Agent {
|
|
|
19908
20011
|
agentModel: model,
|
|
19909
20012
|
...reg.options.apiKey !== void 0 ? { apiKey: reg.options.apiKey } : {}
|
|
19910
20013
|
})
|
|
19911
|
-
});
|
|
20014
|
+
}));
|
|
19912
20015
|
}
|
|
19913
20016
|
/**
|
|
19914
20017
|
* Permanently delete a cloud agent.
|