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