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