@theokit/sdk 4.16.3 → 4.16.5
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/dist/cron.cjs +363 -383
- package/dist/cron.cjs.map +1 -1
- package/dist/cron.js +364 -384
- package/dist/cron.js.map +1 -1
- package/dist/eval.cjs +363 -383
- package/dist/eval.cjs.map +1 -1
- package/dist/eval.js +364 -384
- package/dist/eval.js.map +1 -1
- package/dist/index.cjs +361 -369
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +362 -370
- 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
|
@@ -3,7 +3,7 @@ import { createRequire } from 'module';
|
|
|
3
3
|
import { randomUUID, randomBytes, createHash } from 'crypto';
|
|
4
4
|
import { mkdir, readFile, open, rename, unlink, statfs, readdir, stat, access } from 'fs/promises';
|
|
5
5
|
import { join, dirname, resolve, sep, relative, isAbsolute } from 'path';
|
|
6
|
-
import {
|
|
6
|
+
import { existsSync, readFileSync, realpathSync, mkdirSync, lstatSync, readlinkSync, readdirSync, statSync, chmodSync, openSync, writeFileSync, fsyncSync, closeSync, renameSync, unlinkSync } from 'fs';
|
|
7
7
|
import { homedir } from 'os';
|
|
8
8
|
import { fileURLToPath, pathToFileURL } from 'url';
|
|
9
9
|
import { AsyncLocalStorage } from 'async_hooks';
|
|
@@ -1602,140 +1602,6 @@ var init_run_events = __esm({
|
|
|
1602
1602
|
"src/types/run-events.ts"() {
|
|
1603
1603
|
}
|
|
1604
1604
|
});
|
|
1605
|
-
|
|
1606
|
-
// src/compaction.ts
|
|
1607
|
-
function estimateTokens(text) {
|
|
1608
|
-
return Math.ceil(text.length / 4);
|
|
1609
|
-
}
|
|
1610
|
-
var init_compaction = __esm({
|
|
1611
|
-
"src/compaction.ts"() {
|
|
1612
|
-
}
|
|
1613
|
-
});
|
|
1614
|
-
|
|
1615
|
-
// src/internal/runtime/compression/compression-summarizer.ts
|
|
1616
|
-
var compression_summarizer_exports = {};
|
|
1617
|
-
__export(compression_summarizer_exports, {
|
|
1618
|
-
CompressionFailedError: () => CompressionFailedError,
|
|
1619
|
-
buildCompressionPrompt: () => buildCompressionPrompt,
|
|
1620
|
-
compressConversationWindow: () => compressConversationWindow
|
|
1621
|
-
});
|
|
1622
|
-
function buildCompressionPrompt(messages) {
|
|
1623
|
-
const formatted = messages.map((m) => `[${m.role}]: ${m.content}`).join("\n\n");
|
|
1624
|
-
return `Summarize the following ${messages.length} conversation messages into a concise summary that preserves ALL facts, decisions, user preferences, and context needed for the conversation to continue naturally. The summary will replace these messages in the context window. Be thorough but concise.
|
|
1625
|
-
|
|
1626
|
-
--- CONVERSATION TO SUMMARIZE ---
|
|
1627
|
-
${formatted}
|
|
1628
|
-
--- END ---`;
|
|
1629
|
-
}
|
|
1630
|
-
async function compressConversationWindow(opts) {
|
|
1631
|
-
const userPrompt = buildCompressionPrompt(opts.messages);
|
|
1632
|
-
let summary;
|
|
1633
|
-
try {
|
|
1634
|
-
summary = await opts.callLlm(opts.model, COMPRESSION_SYSTEM, userPrompt);
|
|
1635
|
-
} catch (cause) {
|
|
1636
|
-
throw new CompressionFailedError(
|
|
1637
|
-
`Compression LLM call failed: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
1638
|
-
{ cause: cause instanceof Error ? cause : void 0 }
|
|
1639
|
-
);
|
|
1640
|
-
}
|
|
1641
|
-
if (!summary || summary.trim().length === 0) {
|
|
1642
|
-
throw new CompressionFailedError(
|
|
1643
|
-
"Compression LLM returned empty summary \u2014 reduction ineffective."
|
|
1644
|
-
);
|
|
1645
|
-
}
|
|
1646
|
-
return {
|
|
1647
|
-
role: "system",
|
|
1648
|
-
content: `[Compressed conversation summary]: ${summary.trim()}`
|
|
1649
|
-
};
|
|
1650
|
-
}
|
|
1651
|
-
var CompressionFailedError, COMPRESSION_SYSTEM;
|
|
1652
|
-
var init_compression_summarizer = __esm({
|
|
1653
|
-
"src/internal/runtime/compression/compression-summarizer.ts"() {
|
|
1654
|
-
CompressionFailedError = class extends Error {
|
|
1655
|
-
name = "CompressionFailedError";
|
|
1656
|
-
};
|
|
1657
|
-
COMPRESSION_SYSTEM = "You are a conversation summarizer. Produce a concise factual summary. Preserve all decisions, preferences, code snippets, and action items. Do not add commentary or opinions. Output ONLY the summary text.";
|
|
1658
|
-
}
|
|
1659
|
-
});
|
|
1660
|
-
|
|
1661
|
-
// src/internal/runtime/compression/compression-model-registry.ts
|
|
1662
|
-
var compression_model_registry_exports = {};
|
|
1663
|
-
__export(compression_model_registry_exports, {
|
|
1664
|
-
CompressionModelUnresolvedError: () => CompressionModelUnresolvedError,
|
|
1665
|
-
resolveCompressionModel: () => resolveCompressionModel
|
|
1666
|
-
});
|
|
1667
|
-
function resolveCompressionModel(agentModel) {
|
|
1668
|
-
const exact = EXACT_REGISTRY.get(agentModel);
|
|
1669
|
-
if (exact !== void 0) return exact;
|
|
1670
|
-
const wildcard = matchWildcard(agentModel);
|
|
1671
|
-
if (wildcard !== null) return wildcard;
|
|
1672
|
-
if (isNoAuthProvider(agentModel)) return agentModel;
|
|
1673
|
-
throw new CompressionModelUnresolvedError(agentModel);
|
|
1674
|
-
}
|
|
1675
|
-
function matchWildcard(agentModel) {
|
|
1676
|
-
for (const [pattern, replacement] of WILDCARD_REGISTRY) {
|
|
1677
|
-
const prefix = pattern.endsWith("*") ? pattern.slice(0, -1) : pattern;
|
|
1678
|
-
if (!agentModel.startsWith(prefix)) continue;
|
|
1679
|
-
const suffix = agentModel.slice(prefix.length);
|
|
1680
|
-
const replPrefix = replacement.endsWith("*") ? replacement.slice(0, -1) : replacement;
|
|
1681
|
-
return `${replPrefix}${suffix}`;
|
|
1682
|
-
}
|
|
1683
|
-
return null;
|
|
1684
|
-
}
|
|
1685
|
-
function isNoAuthProvider(agentModel) {
|
|
1686
|
-
const slashIdx = agentModel.indexOf("/");
|
|
1687
|
-
if (slashIdx <= 0) return false;
|
|
1688
|
-
return NO_AUTH_PROVIDERS.has(agentModel.slice(0, slashIdx));
|
|
1689
|
-
}
|
|
1690
|
-
var EXACT_REGISTRY, WILDCARD_REGISTRY, NO_AUTH_PROVIDERS, CompressionModelUnresolvedError;
|
|
1691
|
-
var init_compression_model_registry = __esm({
|
|
1692
|
-
"src/internal/runtime/compression/compression-model-registry.ts"() {
|
|
1693
|
-
EXACT_REGISTRY = /* @__PURE__ */ new Map([
|
|
1694
|
-
// OpenAI family
|
|
1695
|
-
["openai/gpt-4o", "openai/gpt-4o-mini"],
|
|
1696
|
-
["openai/gpt-4-turbo", "openai/gpt-4o-mini"],
|
|
1697
|
-
["openai/gpt-4", "openai/gpt-4o-mini"],
|
|
1698
|
-
["openai/o1-preview", "openai/gpt-4o-mini"],
|
|
1699
|
-
["openai/o1", "openai/gpt-4o-mini"],
|
|
1700
|
-
["openai/o3", "openai/gpt-4o-mini"],
|
|
1701
|
-
["openai/o3-mini", "openai/gpt-4o-mini"],
|
|
1702
|
-
// Anthropic family
|
|
1703
|
-
["anthropic/claude-opus-4", "anthropic/claude-3-5-haiku-latest"],
|
|
1704
|
-
["anthropic/claude-sonnet-4", "anthropic/claude-3-5-haiku-latest"],
|
|
1705
|
-
["anthropic/claude-3-5-sonnet", "anthropic/claude-3-5-haiku-latest"],
|
|
1706
|
-
["anthropic/claude-3-5-sonnet-latest", "anthropic/claude-3-5-haiku-latest"],
|
|
1707
|
-
["anthropic/claude-3-opus", "anthropic/claude-3-haiku"],
|
|
1708
|
-
["anthropic/claude-3-sonnet", "anthropic/claude-3-haiku"],
|
|
1709
|
-
// Vertex (Gemini + Anthropic-on-Vertex)
|
|
1710
|
-
["vertex/gemini-1.5-pro", "vertex/gemini-1.5-flash"],
|
|
1711
|
-
["vertex/gemini-2.0-pro", "vertex/gemini-1.5-flash"],
|
|
1712
|
-
["vertex/claude-3-5-sonnet", "vertex/claude-3-5-haiku"],
|
|
1713
|
-
["vertex/claude-3-opus", "vertex/claude-3-haiku"],
|
|
1714
|
-
// OpenRouter (preserve openrouter prefix, swap tier within same vendor)
|
|
1715
|
-
["openrouter/openai/gpt-4o", "openrouter/openai/gpt-4o-mini"],
|
|
1716
|
-
["openrouter/openai/gpt-4-turbo", "openrouter/openai/gpt-4o-mini"],
|
|
1717
|
-
["openrouter/anthropic/claude-3-5-sonnet", "openrouter/anthropic/claude-3-5-haiku"],
|
|
1718
|
-
["openrouter/anthropic/claude-opus-4", "openrouter/anthropic/claude-3-5-haiku"]
|
|
1719
|
-
]);
|
|
1720
|
-
WILDCARD_REGISTRY = [
|
|
1721
|
-
// Bedrock Anthropic: us.anthropic.claude-sonnet-* → us.anthropic.claude-3-haiku-*
|
|
1722
|
-
["bedrock/anthropic.claude-sonnet*", "bedrock/anthropic.claude-3-haiku*"],
|
|
1723
|
-
["bedrock/anthropic.claude-opus*", "bedrock/anthropic.claude-3-haiku*"],
|
|
1724
|
-
["bedrock/anthropic.claude-3-5-sonnet*", "bedrock/anthropic.claude-3-5-haiku*"]
|
|
1725
|
-
];
|
|
1726
|
-
NO_AUTH_PROVIDERS = /* @__PURE__ */ new Set(["ollama", "lmstudio", "llamacpp"]);
|
|
1727
|
-
CompressionModelUnresolvedError = class extends Error {
|
|
1728
|
-
name = "CompressionModelUnresolvedError";
|
|
1729
|
-
agentModel;
|
|
1730
|
-
constructor(agentModel) {
|
|
1731
|
-
super(
|
|
1732
|
-
`Could not resolve a same-family-cheaper-tier compression model for "${agentModel}". Provide Agent.create({compression: {model: "<your-cheaper-model>"}}) OR add "${agentModel}" to the compression-model-registry (see ADR D440).`
|
|
1733
|
-
);
|
|
1734
|
-
this.agentModel = agentModel;
|
|
1735
|
-
}
|
|
1736
|
-
};
|
|
1737
|
-
}
|
|
1738
|
-
});
|
|
1739
1605
|
var MODALITIES, costSchema, limitSchema, modalitiesSchema, catalogModelSchema;
|
|
1740
1606
|
var init_catalog_schema = __esm({
|
|
1741
1607
|
"src/internal/providers/catalog-schema.ts"() {
|
|
@@ -1813,19 +1679,6 @@ var init_registry = __esm({
|
|
|
1813
1679
|
ALIASES = globalSingleton("theokit-sdk.providers.aliases", () => /* @__PURE__ */ new Map());
|
|
1814
1680
|
}
|
|
1815
1681
|
});
|
|
1816
|
-
|
|
1817
|
-
// src/internal/providers/catalog-loader.ts
|
|
1818
|
-
var catalog_loader_exports = {};
|
|
1819
|
-
__export(catalog_loader_exports, {
|
|
1820
|
-
_resetModelInfoIndexForTests: () => _resetModelInfoIndexForTests,
|
|
1821
|
-
getCatalogCapabilities: () => getCatalogCapabilities,
|
|
1822
|
-
getCatalogModelInfo: () => getCatalogModelInfo,
|
|
1823
|
-
isPatchedModelKey: () => isPatchedModelKey,
|
|
1824
|
-
listModelInfoKeys: () => listModelInfoKeys,
|
|
1825
|
-
loadProviderCatalog: () => loadProviderCatalog,
|
|
1826
|
-
patchModelInfo: () => patchModelInfo,
|
|
1827
|
-
registerCatalogProviders: () => registerCatalogProviders
|
|
1828
|
-
});
|
|
1829
1682
|
function globalSingleton2(key, create) {
|
|
1830
1683
|
const g = globalThis;
|
|
1831
1684
|
const sym = Symbol.for(key);
|
|
@@ -1839,16 +1692,6 @@ function getCatalogModelInfo(key) {
|
|
|
1839
1692
|
function isPatchedModelKey(key) {
|
|
1840
1693
|
return patchedModelKeys.has(key);
|
|
1841
1694
|
}
|
|
1842
|
-
function patchModelInfo(key, model) {
|
|
1843
|
-
ensureModelIndexLoaded();
|
|
1844
|
-
const existing = modelInfoIndex.get(key);
|
|
1845
|
-
modelInfoIndex.set(key, existing === void 0 ? model : { ...existing, ...model });
|
|
1846
|
-
patchedModelKeys.add(key);
|
|
1847
|
-
}
|
|
1848
|
-
function listModelInfoKeys() {
|
|
1849
|
-
ensureModelIndexLoaded();
|
|
1850
|
-
return [...modelInfoIndex.keys()];
|
|
1851
|
-
}
|
|
1852
1695
|
function ensureModelIndexLoaded() {
|
|
1853
1696
|
if (indexState.loaded) return;
|
|
1854
1697
|
indexState.loaded = true;
|
|
@@ -1882,11 +1725,6 @@ function indexEntryModels(entry) {
|
|
|
1882
1725
|
}
|
|
1883
1726
|
}
|
|
1884
1727
|
}
|
|
1885
|
-
function _resetModelInfoIndexForTests() {
|
|
1886
|
-
modelInfoIndex.clear();
|
|
1887
|
-
patchedModelKeys.clear();
|
|
1888
|
-
indexState.loaded = false;
|
|
1889
|
-
}
|
|
1890
1728
|
function validateEntry(raw) {
|
|
1891
1729
|
if (typeof raw.id !== "string" || typeof raw.displayName !== "string" || typeof raw.apiMode !== "string" || typeof raw.authType !== "string" || typeof raw.baseUrl !== "string" || !Array.isArray(raw.envVars) || !Array.isArray(raw.fallbackModels) || raw.capabilities == null || typeof raw.capabilities !== "object") {
|
|
1892
1730
|
return null;
|
|
@@ -1897,12 +1735,6 @@ function loadProviderCatalog(opts) {
|
|
|
1897
1735
|
const catalogPath = join(__dirname_resolved, "provider-catalog.json");
|
|
1898
1736
|
const rawText = readFileSync(catalogPath, "utf-8");
|
|
1899
1737
|
let entries = JSON.parse(rawText);
|
|
1900
|
-
if (opts?._testInjectMalformed) {
|
|
1901
|
-
entries = [
|
|
1902
|
-
...entries,
|
|
1903
|
-
{ id: "malformed-provider", displayName: "Bad" }
|
|
1904
|
-
];
|
|
1905
|
-
}
|
|
1906
1738
|
const result = {};
|
|
1907
1739
|
for (const raw of entries) {
|
|
1908
1740
|
const validated = validateEntry(raw);
|
|
@@ -1917,21 +1749,8 @@ function loadProviderCatalog(opts) {
|
|
|
1917
1749
|
}
|
|
1918
1750
|
return result;
|
|
1919
1751
|
}
|
|
1920
|
-
function getCatalogCapabilities(providerId) {
|
|
1921
|
-
if (_capabilitiesCache === null) {
|
|
1922
|
-
const catalog = loadProviderCatalog();
|
|
1923
|
-
_capabilitiesCache = {};
|
|
1924
|
-
for (const entry of Object.values(catalog)) {
|
|
1925
|
-
_capabilitiesCache[entry.id] = entry.capabilities;
|
|
1926
|
-
for (const alias of entry.aliases ?? []) {
|
|
1927
|
-
if (_capabilitiesCache[alias] === void 0) _capabilitiesCache[alias] = entry.capabilities;
|
|
1928
|
-
}
|
|
1929
|
-
}
|
|
1930
|
-
}
|
|
1931
|
-
return _capabilitiesCache[providerId];
|
|
1932
|
-
}
|
|
1933
1752
|
function registerCatalogProviders(opts) {
|
|
1934
|
-
const catalog = loadProviderCatalog(
|
|
1753
|
+
const catalog = loadProviderCatalog();
|
|
1935
1754
|
for (const entry of Object.values(catalog)) {
|
|
1936
1755
|
if (getProviderProfile(entry.id) !== void 0) continue;
|
|
1937
1756
|
if (entry.aliases?.some((a) => getProviderProfile(a) !== void 0)) continue;
|
|
@@ -1951,7 +1770,7 @@ function registerCatalogProviders(opts) {
|
|
|
1951
1770
|
registerProvider(profile);
|
|
1952
1771
|
}
|
|
1953
1772
|
}
|
|
1954
|
-
var __dirname_resolved, modelInfoIndex, patchedModelKeys, indexState
|
|
1773
|
+
var __dirname_resolved, modelInfoIndex, patchedModelKeys, indexState;
|
|
1955
1774
|
var init_catalog_loader = __esm({
|
|
1956
1775
|
"src/internal/providers/catalog-loader.ts"() {
|
|
1957
1776
|
init_catalog_schema();
|
|
@@ -1968,7 +1787,15 @@ var init_catalog_loader = __esm({
|
|
|
1968
1787
|
indexState = globalSingleton2("theokit-sdk.providers.model-info-loaded", () => ({
|
|
1969
1788
|
loaded: false
|
|
1970
1789
|
}));
|
|
1971
|
-
|
|
1790
|
+
}
|
|
1791
|
+
});
|
|
1792
|
+
|
|
1793
|
+
// src/compaction.ts
|
|
1794
|
+
function estimateTokens(text) {
|
|
1795
|
+
return Math.ceil(text.length / 4);
|
|
1796
|
+
}
|
|
1797
|
+
var init_compaction = __esm({
|
|
1798
|
+
"src/compaction.ts"() {
|
|
1972
1799
|
}
|
|
1973
1800
|
});
|
|
1974
1801
|
|
|
@@ -5615,12 +5442,6 @@ var init_vertex_router = __esm({
|
|
|
5615
5442
|
});
|
|
5616
5443
|
|
|
5617
5444
|
// src/internal/llm/router.ts
|
|
5618
|
-
var router_exports = {};
|
|
5619
|
-
__export(router_exports, {
|
|
5620
|
-
_resetCredentialPoolWarnings: () => _resetCredentialPoolWarnings,
|
|
5621
|
-
_resetNoAuthApiKeyWarnings: () => _resetNoAuthApiKeyWarnings,
|
|
5622
|
-
resolveProviderChain: () => resolveProviderChain
|
|
5623
|
-
});
|
|
5624
5445
|
function resolveProviderChain(options) {
|
|
5625
5446
|
registerBuiltins();
|
|
5626
5447
|
return buildChain(options);
|
|
@@ -5718,9 +5539,6 @@ function warnNoAuthApiKeysIgnoredOnce(provider) {
|
|
|
5718
5539
|
`
|
|
5719
5540
|
);
|
|
5720
5541
|
}
|
|
5721
|
-
function _resetNoAuthApiKeyWarnings() {
|
|
5722
|
-
warnedNoAuthApiKeys.clear();
|
|
5723
|
-
}
|
|
5724
5542
|
function sentinelForNoAuth(profile) {
|
|
5725
5543
|
return profile.authType === "none" ? profile.name : void 0;
|
|
5726
5544
|
}
|
|
@@ -5751,9 +5569,6 @@ function warnUnknownProvidersInApiKeys(apiKeys) {
|
|
|
5751
5569
|
}
|
|
5752
5570
|
}
|
|
5753
5571
|
}
|
|
5754
|
-
function _resetCredentialPoolWarnings() {
|
|
5755
|
-
warnedProviders.clear();
|
|
5756
|
-
}
|
|
5757
5572
|
function resolveApiKey2(envVars) {
|
|
5758
5573
|
for (const v of envVars) {
|
|
5759
5574
|
const value = process.env[v];
|
|
@@ -5857,41 +5672,318 @@ var init_router = __esm({
|
|
|
5857
5672
|
}
|
|
5858
5673
|
});
|
|
5859
5674
|
|
|
5860
|
-
// src/internal/local-agent/real-local-run-provider.ts
|
|
5861
|
-
|
|
5862
|
-
|
|
5863
|
-
|
|
5864
|
-
|
|
5865
|
-
}
|
|
5866
|
-
|
|
5867
|
-
|
|
5868
|
-
const byPrefix
|
|
5869
|
-
|
|
5870
|
-
|
|
5871
|
-
|
|
5872
|
-
|
|
5873
|
-
|
|
5874
|
-
|
|
5875
|
-
|
|
5675
|
+
// src/internal/local-agent/real-local-run-provider.ts
|
|
5676
|
+
function inferProviderFromApiKey(apiKey) {
|
|
5677
|
+
if (apiKey === void 0 || apiKey.length === 0) return void 0;
|
|
5678
|
+
const byPrefix = [
|
|
5679
|
+
{ provider: "openrouter", prefix: "sk-or-" },
|
|
5680
|
+
{ provider: "anthropic", prefix: "sk-ant-" },
|
|
5681
|
+
{ provider: "openai", prefix: "sk-" }
|
|
5682
|
+
];
|
|
5683
|
+
for (const { provider, prefix } of byPrefix) {
|
|
5684
|
+
if (apiKey.startsWith(prefix) && getProviderProfile(provider) !== void 0) {
|
|
5685
|
+
return provider;
|
|
5686
|
+
}
|
|
5687
|
+
}
|
|
5688
|
+
return void 0;
|
|
5689
|
+
}
|
|
5690
|
+
function detectPrimaryProvider() {
|
|
5691
|
+
if (process.env.ANTHROPIC_API_KEY !== void 0 && process.env.ANTHROPIC_API_KEY.length > 0) {
|
|
5692
|
+
return "anthropic";
|
|
5693
|
+
}
|
|
5694
|
+
if (process.env.OPENAI_API_KEY !== void 0 && process.env.OPENAI_API_KEY.length > 0) {
|
|
5695
|
+
return "openai";
|
|
5696
|
+
}
|
|
5697
|
+
if (process.env.OPENROUTER_API_KEY !== void 0 && process.env.OPENROUTER_API_KEY.length > 0) {
|
|
5698
|
+
return "openrouter";
|
|
5699
|
+
}
|
|
5700
|
+
return "openai";
|
|
5701
|
+
}
|
|
5702
|
+
var init_real_local_run_provider = __esm({
|
|
5703
|
+
"src/internal/local-agent/real-local-run-provider.ts"() {
|
|
5704
|
+
init_providers();
|
|
5705
|
+
}
|
|
5706
|
+
});
|
|
5707
|
+
|
|
5708
|
+
// src/internal/runtime/compression/compression-model-registry.ts
|
|
5709
|
+
function resolveCompressionModel(agentModel) {
|
|
5710
|
+
const exact = EXACT_REGISTRY.get(agentModel);
|
|
5711
|
+
if (exact !== void 0) return exact;
|
|
5712
|
+
const wildcard = matchWildcard(agentModel);
|
|
5713
|
+
if (wildcard !== null) return wildcard;
|
|
5714
|
+
if (isNoAuthProvider(agentModel)) return agentModel;
|
|
5715
|
+
throw new CompressionModelUnresolvedError(agentModel);
|
|
5716
|
+
}
|
|
5717
|
+
function matchWildcard(agentModel) {
|
|
5718
|
+
for (const [pattern, replacement] of WILDCARD_REGISTRY) {
|
|
5719
|
+
const prefix = pattern.endsWith("*") ? pattern.slice(0, -1) : pattern;
|
|
5720
|
+
if (!agentModel.startsWith(prefix)) continue;
|
|
5721
|
+
const suffix = agentModel.slice(prefix.length);
|
|
5722
|
+
const replPrefix = replacement.endsWith("*") ? replacement.slice(0, -1) : replacement;
|
|
5723
|
+
return `${replPrefix}${suffix}`;
|
|
5724
|
+
}
|
|
5725
|
+
return null;
|
|
5726
|
+
}
|
|
5727
|
+
function isNoAuthProvider(agentModel) {
|
|
5728
|
+
const slashIdx = agentModel.indexOf("/");
|
|
5729
|
+
if (slashIdx <= 0) return false;
|
|
5730
|
+
return NO_AUTH_PROVIDERS.has(agentModel.slice(0, slashIdx));
|
|
5731
|
+
}
|
|
5732
|
+
var EXACT_REGISTRY, WILDCARD_REGISTRY, NO_AUTH_PROVIDERS, CompressionModelUnresolvedError;
|
|
5733
|
+
var init_compression_model_registry = __esm({
|
|
5734
|
+
"src/internal/runtime/compression/compression-model-registry.ts"() {
|
|
5735
|
+
EXACT_REGISTRY = /* @__PURE__ */ new Map([
|
|
5736
|
+
// OpenAI family
|
|
5737
|
+
["openai/gpt-4o", "openai/gpt-4o-mini"],
|
|
5738
|
+
["openai/gpt-4-turbo", "openai/gpt-4o-mini"],
|
|
5739
|
+
["openai/gpt-4", "openai/gpt-4o-mini"],
|
|
5740
|
+
["openai/o1-preview", "openai/gpt-4o-mini"],
|
|
5741
|
+
["openai/o1", "openai/gpt-4o-mini"],
|
|
5742
|
+
["openai/o3", "openai/gpt-4o-mini"],
|
|
5743
|
+
["openai/o3-mini", "openai/gpt-4o-mini"],
|
|
5744
|
+
// Anthropic family
|
|
5745
|
+
["anthropic/claude-opus-4", "anthropic/claude-3-5-haiku-latest"],
|
|
5746
|
+
["anthropic/claude-sonnet-4", "anthropic/claude-3-5-haiku-latest"],
|
|
5747
|
+
["anthropic/claude-3-5-sonnet", "anthropic/claude-3-5-haiku-latest"],
|
|
5748
|
+
["anthropic/claude-3-5-sonnet-latest", "anthropic/claude-3-5-haiku-latest"],
|
|
5749
|
+
["anthropic/claude-3-opus", "anthropic/claude-3-haiku"],
|
|
5750
|
+
["anthropic/claude-3-sonnet", "anthropic/claude-3-haiku"],
|
|
5751
|
+
// Vertex (Gemini + Anthropic-on-Vertex)
|
|
5752
|
+
["vertex/gemini-1.5-pro", "vertex/gemini-1.5-flash"],
|
|
5753
|
+
["vertex/gemini-2.0-pro", "vertex/gemini-1.5-flash"],
|
|
5754
|
+
["vertex/claude-3-5-sonnet", "vertex/claude-3-5-haiku"],
|
|
5755
|
+
["vertex/claude-3-opus", "vertex/claude-3-haiku"],
|
|
5756
|
+
// OpenRouter (preserve openrouter prefix, swap tier within same vendor)
|
|
5757
|
+
["openrouter/openai/gpt-4o", "openrouter/openai/gpt-4o-mini"],
|
|
5758
|
+
["openrouter/openai/gpt-4-turbo", "openrouter/openai/gpt-4o-mini"],
|
|
5759
|
+
["openrouter/anthropic/claude-3-5-sonnet", "openrouter/anthropic/claude-3-5-haiku"],
|
|
5760
|
+
["openrouter/anthropic/claude-opus-4", "openrouter/anthropic/claude-3-5-haiku"]
|
|
5761
|
+
]);
|
|
5762
|
+
WILDCARD_REGISTRY = [
|
|
5763
|
+
// Bedrock Anthropic: us.anthropic.claude-sonnet-* → us.anthropic.claude-3-haiku-*
|
|
5764
|
+
["bedrock/anthropic.claude-sonnet*", "bedrock/anthropic.claude-3-haiku*"],
|
|
5765
|
+
["bedrock/anthropic.claude-opus*", "bedrock/anthropic.claude-3-haiku*"],
|
|
5766
|
+
["bedrock/anthropic.claude-3-5-sonnet*", "bedrock/anthropic.claude-3-5-haiku*"]
|
|
5767
|
+
];
|
|
5768
|
+
NO_AUTH_PROVIDERS = /* @__PURE__ */ new Set(["ollama", "lmstudio", "llamacpp"]);
|
|
5769
|
+
CompressionModelUnresolvedError = class extends Error {
|
|
5770
|
+
name = "CompressionModelUnresolvedError";
|
|
5771
|
+
agentModel;
|
|
5772
|
+
constructor(agentModel) {
|
|
5773
|
+
super(
|
|
5774
|
+
`Could not resolve a same-family-cheaper-tier compression model for "${agentModel}". Provide Agent.create({compression: {model: "<your-cheaper-model>"}}) OR add "${agentModel}" to the compression-model-registry (see ADR D440).`
|
|
5775
|
+
);
|
|
5776
|
+
this.agentModel = agentModel;
|
|
5777
|
+
}
|
|
5778
|
+
};
|
|
5779
|
+
}
|
|
5780
|
+
});
|
|
5781
|
+
|
|
5782
|
+
// src/internal/runtime/compression/compression-summarizer.ts
|
|
5783
|
+
function buildCompressionPrompt(messages) {
|
|
5784
|
+
const formatted = messages.map((m) => `[${m.role}]: ${m.content}`).join("\n\n");
|
|
5785
|
+
return `Summarize the following ${messages.length} conversation messages into a concise summary that preserves ALL facts, decisions, user preferences, and context needed for the conversation to continue naturally. The summary will replace these messages in the context window. Be thorough but concise.
|
|
5786
|
+
|
|
5787
|
+
--- CONVERSATION TO SUMMARIZE ---
|
|
5788
|
+
${formatted}
|
|
5789
|
+
--- END ---`;
|
|
5790
|
+
}
|
|
5791
|
+
async function compressConversationWindow(opts) {
|
|
5792
|
+
const userPrompt = buildCompressionPrompt(opts.messages);
|
|
5793
|
+
let summary;
|
|
5794
|
+
try {
|
|
5795
|
+
summary = await opts.callLlm(opts.model, COMPRESSION_SYSTEM, userPrompt);
|
|
5796
|
+
} catch (cause) {
|
|
5797
|
+
throw new CompressionFailedError(
|
|
5798
|
+
`Compression LLM call failed: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
5799
|
+
{ cause: cause instanceof Error ? cause : void 0 }
|
|
5800
|
+
);
|
|
5801
|
+
}
|
|
5802
|
+
if (!summary || summary.trim().length === 0) {
|
|
5803
|
+
throw new CompressionFailedError(
|
|
5804
|
+
"Compression LLM returned empty summary \u2014 reduction ineffective."
|
|
5805
|
+
);
|
|
5806
|
+
}
|
|
5807
|
+
return {
|
|
5808
|
+
role: "system",
|
|
5809
|
+
content: `[Compressed conversation summary]: ${summary.trim()}`
|
|
5810
|
+
};
|
|
5811
|
+
}
|
|
5812
|
+
var CompressionFailedError, COMPRESSION_SYSTEM;
|
|
5813
|
+
var init_compression_summarizer = __esm({
|
|
5814
|
+
"src/internal/runtime/compression/compression-summarizer.ts"() {
|
|
5815
|
+
CompressionFailedError = class extends Error {
|
|
5816
|
+
name = "CompressionFailedError";
|
|
5817
|
+
};
|
|
5818
|
+
COMPRESSION_SYSTEM = "You are a conversation summarizer. Produce a concise factual summary. Preserve all decisions, preferences, code snippets, and action items. Do not add commentary or opinions. Output ONLY the summary text.";
|
|
5819
|
+
}
|
|
5820
|
+
});
|
|
5821
|
+
|
|
5822
|
+
// src/internal/session/agent-session-store.ts
|
|
5823
|
+
function seedTranscript(prior, opts) {
|
|
5824
|
+
return SessionTranscript.fromRecords(prior, opts);
|
|
5825
|
+
}
|
|
5826
|
+
function mapAgentTurn(steps) {
|
|
5827
|
+
const assistant = {};
|
|
5828
|
+
const toolResults = [];
|
|
5829
|
+
const toolCalls = [];
|
|
5830
|
+
for (const step of steps) {
|
|
5831
|
+
if (step.type === "thinkingMessage") assistant.thinking = step.message.text;
|
|
5832
|
+
else if (step.type === "assistantMessage") assistant.text = step.message.text;
|
|
5833
|
+
else if (step.type === "toolCall")
|
|
5834
|
+
toolCalls.push({
|
|
5835
|
+
id: step.message.callId,
|
|
5836
|
+
name: step.message.name,
|
|
5837
|
+
input: step.message.args ?? {}
|
|
5838
|
+
});
|
|
5839
|
+
else
|
|
5840
|
+
toolResults.push({
|
|
5841
|
+
toolUseId: step.message.callId,
|
|
5842
|
+
content: step.message.result,
|
|
5843
|
+
isError: step.message.isError
|
|
5844
|
+
});
|
|
5845
|
+
}
|
|
5846
|
+
if (toolCalls.length > 0) assistant.toolCalls = toolCalls;
|
|
5847
|
+
return { assistant, toolResults };
|
|
5848
|
+
}
|
|
5849
|
+
function hasAssistantContent(a) {
|
|
5850
|
+
return a.text !== void 0 || a.thinking !== void 0 || (a.toolCalls?.length ?? 0) > 0;
|
|
5851
|
+
}
|
|
5852
|
+
function appendConversation(transcript, conversation) {
|
|
5853
|
+
for (const ct of conversation) {
|
|
5854
|
+
if (ct.type !== "agentConversationTurn") continue;
|
|
5855
|
+
const { assistant, toolResults } = mapAgentTurn(ct.turn.steps);
|
|
5856
|
+
if (hasAssistantContent(assistant)) transcript.appendAssistantTurn(assistant);
|
|
5857
|
+
if (toolResults.length > 0) transcript.appendToolResults(toolResults);
|
|
5858
|
+
}
|
|
5859
|
+
}
|
|
5860
|
+
async function readSessionMessages(store, agentId) {
|
|
5861
|
+
const records = await store.readRecords(agentId);
|
|
5862
|
+
return reconstructMessages(records).map(narrowToSessionMessage);
|
|
5863
|
+
}
|
|
5864
|
+
function partToText(p) {
|
|
5865
|
+
if (p.type === "text") return p.text ?? "";
|
|
5866
|
+
if (p.type === "tool_use") return `[tool call] ${p.name ?? ""}`;
|
|
5867
|
+
if (p.type === "tool_result") {
|
|
5868
|
+
const body = typeof p.content === "string" ? p.content : JSON.stringify(p.content);
|
|
5869
|
+
return `[tool result] ${body}`;
|
|
5870
|
+
}
|
|
5871
|
+
return "";
|
|
5872
|
+
}
|
|
5873
|
+
function narrowToSessionMessage(m) {
|
|
5874
|
+
const role = m.role === "user" ? "user" : "assistant";
|
|
5875
|
+
const text = m.content.map(partToText).filter((s) => s.length > 0).join("\n");
|
|
5876
|
+
return { role, text };
|
|
5877
|
+
}
|
|
5878
|
+
function deltaRecords(transcript, priorLength) {
|
|
5879
|
+
return transcript.records().slice(priorLength);
|
|
5880
|
+
}
|
|
5881
|
+
async function persistTurn(store, loc, sessionId, turn) {
|
|
5882
|
+
const prior = await store.readRecords(loc.agentId);
|
|
5883
|
+
const transcript = seedTranscript(prior, { cwd: loc.cwd, sessionId, model: loc.model });
|
|
5884
|
+
transcript.appendUserTurn(turn.userText);
|
|
5885
|
+
appendConversation(transcript, turn.conversation);
|
|
5886
|
+
await store.appendRecords(loc.agentId, deltaRecords(transcript, prior.length));
|
|
5887
|
+
}
|
|
5888
|
+
var init_agent_session_store = __esm({
|
|
5889
|
+
"src/internal/session/agent-session-store.ts"() {
|
|
5890
|
+
init_session_transcript();
|
|
5891
|
+
}
|
|
5892
|
+
});
|
|
5893
|
+
|
|
5894
|
+
// src/internal/session/agent-session.ts
|
|
5895
|
+
function transcriptKey(cwd, agentId) {
|
|
5896
|
+
return `${cwd}::${agentId}`;
|
|
5897
|
+
}
|
|
5898
|
+
function appendSessionMessage(agentId, message) {
|
|
5899
|
+
const existing = sessions.get(agentId) ?? [];
|
|
5900
|
+
existing.push(message);
|
|
5901
|
+
sessions.set(agentId, existing);
|
|
5902
|
+
}
|
|
5903
|
+
function getSessionMessages(agentId) {
|
|
5904
|
+
return sessions.get(agentId) ?? [];
|
|
5905
|
+
}
|
|
5906
|
+
function persistTurnToTranscript(store, loc, sessionId, turn, onCompact) {
|
|
5907
|
+
const key = transcriptKey(loc.cwd, loc.agentId);
|
|
5908
|
+
const chained = (pendingWrites.get(key) ?? Promise.resolve()).then(async () => {
|
|
5909
|
+
try {
|
|
5910
|
+
await persistTurn(store, loc, sessionId, turn);
|
|
5911
|
+
const count = (recordCounts.get(key) ?? 0) + 1;
|
|
5912
|
+
recordCounts.set(key, count);
|
|
5913
|
+
if (turn.autoCompact !== void 0) {
|
|
5914
|
+
const { autoCompactIfNeeded: autoCompactIfNeeded2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
5915
|
+
const fired = await autoCompactIfNeeded2({
|
|
5916
|
+
store,
|
|
5917
|
+
loc,
|
|
5918
|
+
sessionId,
|
|
5919
|
+
usageTotal: turn.autoCompact.usageTotal,
|
|
5920
|
+
contextWindow: turn.autoCompact.contextWindow,
|
|
5921
|
+
turnCount: count,
|
|
5922
|
+
summarize: turn.autoCompact.summarize
|
|
5923
|
+
});
|
|
5924
|
+
if (fired) onCompact?.();
|
|
5925
|
+
}
|
|
5926
|
+
} catch (cause) {
|
|
5927
|
+
const msg = cause instanceof Error ? cause.message : String(cause);
|
|
5928
|
+
process.stderr.write(
|
|
5929
|
+
`[theokit-sdk] session transcript write failed (${loc.agentId}): ${msg}
|
|
5930
|
+
`
|
|
5931
|
+
);
|
|
5876
5932
|
}
|
|
5877
|
-
}
|
|
5878
|
-
|
|
5933
|
+
});
|
|
5934
|
+
pendingWrites.set(
|
|
5935
|
+
key,
|
|
5936
|
+
chained.then(
|
|
5937
|
+
() => void 0,
|
|
5938
|
+
() => void 0
|
|
5939
|
+
)
|
|
5940
|
+
);
|
|
5879
5941
|
}
|
|
5880
|
-
function
|
|
5881
|
-
|
|
5882
|
-
|
|
5883
|
-
|
|
5884
|
-
|
|
5885
|
-
|
|
5942
|
+
async function hydrateSession(agentId, loc) {
|
|
5943
|
+
const key = transcriptKey(loc.cwd, agentId);
|
|
5944
|
+
if (hydratedKeys.has(key)) return;
|
|
5945
|
+
hydratedKeys.add(key);
|
|
5946
|
+
const persisted = await readSessionMessages(loc.store, agentId);
|
|
5947
|
+
if (persisted.length === 0) return;
|
|
5948
|
+
if (!sessions.has(agentId) || sessions.get(agentId)?.length === 0) {
|
|
5949
|
+
sessions.set(agentId, persisted);
|
|
5886
5950
|
}
|
|
5887
|
-
|
|
5888
|
-
|
|
5951
|
+
}
|
|
5952
|
+
async function flushSessionWrites() {
|
|
5953
|
+
while (pendingWrites.size > 0) {
|
|
5954
|
+
const all = Array.from(pendingWrites.values());
|
|
5955
|
+
pendingWrites.clear();
|
|
5956
|
+
await Promise.all(all);
|
|
5889
5957
|
}
|
|
5890
|
-
return "openai";
|
|
5891
5958
|
}
|
|
5892
|
-
|
|
5893
|
-
|
|
5894
|
-
|
|
5959
|
+
function clearSession(agentId) {
|
|
5960
|
+
sessions.delete(agentId);
|
|
5961
|
+
}
|
|
5962
|
+
function invalidateSessionCache(cwd, agentId) {
|
|
5963
|
+
sessions.delete(agentId);
|
|
5964
|
+
hydratedKeys.delete(transcriptKey(cwd, agentId));
|
|
5965
|
+
}
|
|
5966
|
+
function enqueueSessionWrite(cwd, agentId, fn) {
|
|
5967
|
+
const key = transcriptKey(cwd, agentId);
|
|
5968
|
+
const prior = pendingWrites.get(key) ?? Promise.resolve();
|
|
5969
|
+
const result = prior.then(fn);
|
|
5970
|
+
pendingWrites.set(
|
|
5971
|
+
key,
|
|
5972
|
+
result.then(
|
|
5973
|
+
() => void 0,
|
|
5974
|
+
() => void 0
|
|
5975
|
+
)
|
|
5976
|
+
);
|
|
5977
|
+
return result;
|
|
5978
|
+
}
|
|
5979
|
+
var sessions, hydratedKeys, pendingWrites, recordCounts;
|
|
5980
|
+
var init_agent_session = __esm({
|
|
5981
|
+
"src/internal/session/agent-session.ts"() {
|
|
5982
|
+
init_agent_session_store();
|
|
5983
|
+
sessions = /* @__PURE__ */ new Map();
|
|
5984
|
+
hydratedKeys = /* @__PURE__ */ new Set();
|
|
5985
|
+
pendingWrites = /* @__PURE__ */ new Map();
|
|
5986
|
+
recordCounts = /* @__PURE__ */ new Map();
|
|
5895
5987
|
}
|
|
5896
5988
|
});
|
|
5897
5989
|
|
|
@@ -5933,7 +6025,14 @@ async function compactSessionTranscript(opts) {
|
|
|
5933
6025
|
const m = compressible[i];
|
|
5934
6026
|
if (m === void 0 || m.role !== "user" || isCompactSummary(m.content)) continue;
|
|
5935
6027
|
const cost = estimateTokens(m.content);
|
|
5936
|
-
if (budget + cost > COMPACT_USER_MESSAGE_MAX_TOKENS)
|
|
6028
|
+
if (budget + cost > COMPACT_USER_MESSAGE_MAX_TOKENS) {
|
|
6029
|
+
const remaining = COMPACT_USER_MESSAGE_MAX_TOKENS - budget;
|
|
6030
|
+
if (remaining > 50) {
|
|
6031
|
+
preserved.unshift(`${m.content.slice(0, remaining * 4)}
|
|
6032
|
+
[...truncated for compaction...]`);
|
|
6033
|
+
}
|
|
6034
|
+
break;
|
|
6035
|
+
}
|
|
5937
6036
|
budget += cost;
|
|
5938
6037
|
preserved.unshift(m.content);
|
|
5939
6038
|
}
|
|
@@ -5949,30 +6048,29 @@ ${summaryBody}`;
|
|
|
5949
6048
|
transcript.appendUserTurn(summary);
|
|
5950
6049
|
const delta = transcript.records().slice(prior.length);
|
|
5951
6050
|
await opts.store.appendRecords(opts.loc.agentId, delta);
|
|
6051
|
+
invalidateSessionCache(opts.loc.cwd, opts.loc.agentId);
|
|
5952
6052
|
const postTokens = estimateTokens([...preserved, summary].join("\n"));
|
|
5953
6053
|
return { preTokens, postTokens };
|
|
5954
6054
|
}
|
|
5955
6055
|
function buildDefaultSummarizer(opts) {
|
|
5956
6056
|
return async (messages) => {
|
|
5957
|
-
const
|
|
5958
|
-
const { resolveCompressionModel: resolveCompressionModel2 } = await Promise.resolve().then(() => (init_compression_model_registry(), compression_model_registry_exports));
|
|
5959
|
-
const { resolveProviderChain: resolveProviderChain2 } = await Promise.resolve().then(() => (init_router(), router_exports));
|
|
5960
|
-
const { inferProviderFromApiKey: inferProviderFromApiKey2, detectPrimaryProvider: detectPrimaryProvider2 } = await Promise.resolve().then(() => (init_real_local_run_provider(), real_local_run_provider_exports));
|
|
5961
|
-
const keyProvider = inferProviderFromApiKey2(opts.apiKey);
|
|
6057
|
+
const keyProvider = inferProviderFromApiKey(opts.apiKey);
|
|
5962
6058
|
const modelPrefix = opts.agentModel.includes("/") ? opts.agentModel.slice(0, opts.agentModel.indexOf("/")) : void 0;
|
|
5963
|
-
|
|
6059
|
+
registerBuiltins();
|
|
6060
|
+
const prefixProfile = modelPrefix !== void 0 ? getProviderProfile(modelPrefix) : void 0;
|
|
6061
|
+
const provider = prefixProfile !== void 0 ? modelPrefix : keyProvider ?? detectPrimaryProvider();
|
|
5964
6062
|
let model;
|
|
5965
6063
|
if (provider !== modelPrefix) {
|
|
5966
6064
|
model = opts.agentModel;
|
|
5967
6065
|
} else {
|
|
5968
6066
|
try {
|
|
5969
|
-
model =
|
|
6067
|
+
model = resolveCompressionModel(opts.agentModel);
|
|
5970
6068
|
} catch {
|
|
5971
6069
|
model = opts.agentModel;
|
|
5972
6070
|
}
|
|
5973
6071
|
}
|
|
5974
6072
|
const callLlm = async (m, system, user) => {
|
|
5975
|
-
const chain =
|
|
6073
|
+
const chain = resolveProviderChain({
|
|
5976
6074
|
primary: provider,
|
|
5977
6075
|
...opts.apiKey !== void 0 ? { apiKeys: { [provider]: [opts.apiKey] } } : {}
|
|
5978
6076
|
});
|
|
@@ -5992,7 +6090,7 @@ function buildDefaultSummarizer(opts) {
|
|
|
5992
6090
|
}
|
|
5993
6091
|
return text;
|
|
5994
6092
|
};
|
|
5995
|
-
const summary = await
|
|
6093
|
+
const summary = await compressConversationWindow({ messages: [...messages], model, callLlm });
|
|
5996
6094
|
return summary.content;
|
|
5997
6095
|
};
|
|
5998
6096
|
}
|
|
@@ -6026,6 +6124,12 @@ var COMPACT_SUMMARY_MARKER, COMPACT_USER_MESSAGE_MAX_TOKENS, autoCompactAttempts
|
|
|
6026
6124
|
var init_compact_session = __esm({
|
|
6027
6125
|
"src/internal/session/compact-session.ts"() {
|
|
6028
6126
|
init_compaction();
|
|
6127
|
+
init_router();
|
|
6128
|
+
init_real_local_run_provider();
|
|
6129
|
+
init_providers();
|
|
6130
|
+
init_compression_model_registry();
|
|
6131
|
+
init_compression_summarizer();
|
|
6132
|
+
init_agent_session();
|
|
6029
6133
|
init_session_transcript();
|
|
6030
6134
|
COMPACT_SUMMARY_MARKER = "[[theokit:compact-summary]]";
|
|
6031
6135
|
COMPACT_USER_MESSAGE_MAX_TOKENS = 2e4;
|
|
@@ -10416,146 +10520,9 @@ async function writeSessionSummary(input) {
|
|
|
10416
10520
|
await replaceFileAtomic(path, body);
|
|
10417
10521
|
}
|
|
10418
10522
|
|
|
10419
|
-
// src/internal/
|
|
10420
|
-
|
|
10421
|
-
|
|
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
|
-
}
|
|
10523
|
+
// src/internal/runtime/lifecycle/post-run-lifecycle.ts
|
|
10524
|
+
init_catalog_loader();
|
|
10525
|
+
init_compact_session();
|
|
10559
10526
|
|
|
10560
10527
|
// src/internal/runtime/memory/memory-path-selector.ts
|
|
10561
10528
|
var PORT_MEMORY_PATH_ENV_VAR = "THEOKIT_PORT_MEMORY_PATH";
|
|
@@ -10604,9 +10571,21 @@ async function runPostRunLifecycle(inputs) {
|
|
|
10604
10571
|
appendSessionMessage(agentId, { role: "assistant", text: result.result });
|
|
10605
10572
|
}
|
|
10606
10573
|
const conversation = await safeConversation(run);
|
|
10607
|
-
const
|
|
10608
|
-
|
|
10609
|
-
|
|
10574
|
+
const contextWindow = getCatalogModelInfo(model)?.limit?.context;
|
|
10575
|
+
if (contextWindow === void 0) {
|
|
10576
|
+
const g = globalThis;
|
|
10577
|
+
const sym = /* @__PURE__ */ Symbol.for("theokit-sdk.compact.no-cw-warned");
|
|
10578
|
+
const warned3 = g[sym] ??= /* @__PURE__ */ new Set();
|
|
10579
|
+
if (!warned3.has(model)) {
|
|
10580
|
+
warned3.add(model);
|
|
10581
|
+
process.stderr.write(
|
|
10582
|
+
`[theokit-sdk] auto-compaction disabled: model "${model}" has no context-window entry in the catalog
|
|
10583
|
+
`
|
|
10584
|
+
);
|
|
10585
|
+
}
|
|
10586
|
+
}
|
|
10587
|
+
const lastRequestUsage = result.usage?.requests?.at(-1)?.totalTokens;
|
|
10588
|
+
const usageForTrigger = lastRequestUsage ?? result.usage?.totalTokens;
|
|
10610
10589
|
persistTurnToTranscript(
|
|
10611
10590
|
sessionStore,
|
|
10612
10591
|
{ cwd: workspaceCwd, agentId, model },
|
|
@@ -10615,9 +10594,9 @@ async function runPostRunLifecycle(inputs) {
|
|
|
10615
10594
|
userText,
|
|
10616
10595
|
conversation,
|
|
10617
10596
|
autoCompact: {
|
|
10618
|
-
usageTotal:
|
|
10597
|
+
usageTotal: usageForTrigger,
|
|
10619
10598
|
contextWindow,
|
|
10620
|
-
summarize:
|
|
10599
|
+
summarize: buildDefaultSummarizer({
|
|
10621
10600
|
agentModel: model,
|
|
10622
10601
|
...inputs.apiKey !== void 0 ? { apiKey: inputs.apiKey } : {}
|
|
10623
10602
|
})
|
|
@@ -19597,6 +19576,7 @@ async function getRegisteredAgentOrThrow(agentId) {
|
|
|
19597
19576
|
// src/agent.ts
|
|
19598
19577
|
init_errors();
|
|
19599
19578
|
init_discovery();
|
|
19579
|
+
init_agent_session();
|
|
19600
19580
|
init_agent_factory_registry();
|
|
19601
19581
|
var streamObjectImport;
|
|
19602
19582
|
var Agent = class _Agent {
|
|
@@ -19899,7 +19879,7 @@ var Agent = class _Agent {
|
|
|
19899
19879
|
const { defaultBaseDir: defaultBaseDir2, expandTilde: expandTilde2 } = await Promise.resolve().then(() => (init_session_transcript(), session_transcript_exports));
|
|
19900
19880
|
const baseDir = reg.options.local?.baseDir !== void 0 ? expandTilde2(reg.options.local.baseDir) : defaultBaseDir2();
|
|
19901
19881
|
const store = new FsSessionStore2({ baseDir, cwd });
|
|
19902
|
-
return compactSessionTranscript2({
|
|
19882
|
+
return enqueueSessionWrite(cwd, agentId, () => compactSessionTranscript2({
|
|
19903
19883
|
store,
|
|
19904
19884
|
loc: { cwd, agentId, model },
|
|
19905
19885
|
sessionId: agentId,
|
|
@@ -19908,7 +19888,7 @@ var Agent = class _Agent {
|
|
|
19908
19888
|
agentModel: model,
|
|
19909
19889
|
...reg.options.apiKey !== void 0 ? { apiKey: reg.options.apiKey } : {}
|
|
19910
19890
|
})
|
|
19911
|
-
});
|
|
19891
|
+
}));
|
|
19912
19892
|
}
|
|
19913
19893
|
/**
|
|
19914
19894
|
* Permanently delete a cloud agent.
|