@theokit/sdk 4.16.4 → 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 +6 -0
- package/dist/cron.cjs +357 -480
- package/dist/cron.cjs.map +1 -1
- package/dist/cron.js +359 -482
- package/dist/cron.js.map +1 -1
- package/dist/eval.cjs +357 -480
- package/dist/eval.cjs.map +1 -1
- package/dist/eval.js +359 -482
- package/dist/eval.js.map +1 -1
- package/dist/index.cjs +358 -465
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +359 -466
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/eval.cjs
CHANGED
|
@@ -1604,212 +1604,6 @@ var init_run_events = __esm({
|
|
|
1604
1604
|
"src/types/run-events.ts"() {
|
|
1605
1605
|
}
|
|
1606
1606
|
});
|
|
1607
|
-
|
|
1608
|
-
// src/internal/session/agent-session-store.ts
|
|
1609
|
-
function seedTranscript(prior, opts) {
|
|
1610
|
-
return SessionTranscript.fromRecords(prior, opts);
|
|
1611
|
-
}
|
|
1612
|
-
function mapAgentTurn(steps) {
|
|
1613
|
-
const assistant = {};
|
|
1614
|
-
const toolResults = [];
|
|
1615
|
-
const toolCalls = [];
|
|
1616
|
-
for (const step of steps) {
|
|
1617
|
-
if (step.type === "thinkingMessage") assistant.thinking = step.message.text;
|
|
1618
|
-
else if (step.type === "assistantMessage") assistant.text = step.message.text;
|
|
1619
|
-
else if (step.type === "toolCall")
|
|
1620
|
-
toolCalls.push({
|
|
1621
|
-
id: step.message.callId,
|
|
1622
|
-
name: step.message.name,
|
|
1623
|
-
input: step.message.args ?? {}
|
|
1624
|
-
});
|
|
1625
|
-
else
|
|
1626
|
-
toolResults.push({
|
|
1627
|
-
toolUseId: step.message.callId,
|
|
1628
|
-
content: step.message.result,
|
|
1629
|
-
isError: step.message.isError
|
|
1630
|
-
});
|
|
1631
|
-
}
|
|
1632
|
-
if (toolCalls.length > 0) assistant.toolCalls = toolCalls;
|
|
1633
|
-
return { assistant, toolResults };
|
|
1634
|
-
}
|
|
1635
|
-
function hasAssistantContent(a) {
|
|
1636
|
-
return a.text !== void 0 || a.thinking !== void 0 || (a.toolCalls?.length ?? 0) > 0;
|
|
1637
|
-
}
|
|
1638
|
-
function appendConversation(transcript, conversation) {
|
|
1639
|
-
for (const ct of conversation) {
|
|
1640
|
-
if (ct.type !== "agentConversationTurn") continue;
|
|
1641
|
-
const { assistant, toolResults } = mapAgentTurn(ct.turn.steps);
|
|
1642
|
-
if (hasAssistantContent(assistant)) transcript.appendAssistantTurn(assistant);
|
|
1643
|
-
if (toolResults.length > 0) transcript.appendToolResults(toolResults);
|
|
1644
|
-
}
|
|
1645
|
-
}
|
|
1646
|
-
async function readSessionMessages(store, agentId) {
|
|
1647
|
-
const records = await store.readRecords(agentId);
|
|
1648
|
-
return reconstructMessages(records).map(narrowToSessionMessage);
|
|
1649
|
-
}
|
|
1650
|
-
function partToText(p) {
|
|
1651
|
-
if (p.type === "text") return p.text ?? "";
|
|
1652
|
-
if (p.type === "tool_use") return `[tool call] ${p.name ?? ""}`;
|
|
1653
|
-
if (p.type === "tool_result") {
|
|
1654
|
-
const body = typeof p.content === "string" ? p.content : JSON.stringify(p.content);
|
|
1655
|
-
return `[tool result] ${body}`;
|
|
1656
|
-
}
|
|
1657
|
-
return "";
|
|
1658
|
-
}
|
|
1659
|
-
function narrowToSessionMessage(m) {
|
|
1660
|
-
const role = m.role === "user" ? "user" : "assistant";
|
|
1661
|
-
const text = m.content.map(partToText).filter((s) => s.length > 0).join("\n");
|
|
1662
|
-
return { role, text };
|
|
1663
|
-
}
|
|
1664
|
-
function deltaRecords(transcript, priorLength) {
|
|
1665
|
-
return transcript.records().slice(priorLength);
|
|
1666
|
-
}
|
|
1667
|
-
async function persistTurn(store, loc, sessionId, turn) {
|
|
1668
|
-
const prior = await store.readRecords(loc.agentId);
|
|
1669
|
-
const transcript = seedTranscript(prior, { cwd: loc.cwd, sessionId, model: loc.model });
|
|
1670
|
-
transcript.appendUserTurn(turn.userText);
|
|
1671
|
-
appendConversation(transcript, turn.conversation);
|
|
1672
|
-
await store.appendRecords(loc.agentId, deltaRecords(transcript, prior.length));
|
|
1673
|
-
}
|
|
1674
|
-
var init_agent_session_store = __esm({
|
|
1675
|
-
"src/internal/session/agent-session-store.ts"() {
|
|
1676
|
-
init_session_transcript();
|
|
1677
|
-
}
|
|
1678
|
-
});
|
|
1679
|
-
|
|
1680
|
-
// src/compaction.ts
|
|
1681
|
-
function estimateTokens(text) {
|
|
1682
|
-
return Math.ceil(text.length / 4);
|
|
1683
|
-
}
|
|
1684
|
-
var init_compaction = __esm({
|
|
1685
|
-
"src/compaction.ts"() {
|
|
1686
|
-
}
|
|
1687
|
-
});
|
|
1688
|
-
|
|
1689
|
-
// src/internal/runtime/compression/compression-summarizer.ts
|
|
1690
|
-
var compression_summarizer_exports = {};
|
|
1691
|
-
__export(compression_summarizer_exports, {
|
|
1692
|
-
CompressionFailedError: () => CompressionFailedError,
|
|
1693
|
-
buildCompressionPrompt: () => buildCompressionPrompt,
|
|
1694
|
-
compressConversationWindow: () => compressConversationWindow
|
|
1695
|
-
});
|
|
1696
|
-
function buildCompressionPrompt(messages) {
|
|
1697
|
-
const formatted = messages.map((m) => `[${m.role}]: ${m.content}`).join("\n\n");
|
|
1698
|
-
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.
|
|
1699
|
-
|
|
1700
|
-
--- CONVERSATION TO SUMMARIZE ---
|
|
1701
|
-
${formatted}
|
|
1702
|
-
--- END ---`;
|
|
1703
|
-
}
|
|
1704
|
-
async function compressConversationWindow(opts) {
|
|
1705
|
-
const userPrompt = buildCompressionPrompt(opts.messages);
|
|
1706
|
-
let summary;
|
|
1707
|
-
try {
|
|
1708
|
-
summary = await opts.callLlm(opts.model, COMPRESSION_SYSTEM, userPrompt);
|
|
1709
|
-
} catch (cause) {
|
|
1710
|
-
throw new CompressionFailedError(
|
|
1711
|
-
`Compression LLM call failed: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
1712
|
-
{ cause: cause instanceof Error ? cause : void 0 }
|
|
1713
|
-
);
|
|
1714
|
-
}
|
|
1715
|
-
if (!summary || summary.trim().length === 0) {
|
|
1716
|
-
throw new CompressionFailedError(
|
|
1717
|
-
"Compression LLM returned empty summary \u2014 reduction ineffective."
|
|
1718
|
-
);
|
|
1719
|
-
}
|
|
1720
|
-
return {
|
|
1721
|
-
role: "system",
|
|
1722
|
-
content: `[Compressed conversation summary]: ${summary.trim()}`
|
|
1723
|
-
};
|
|
1724
|
-
}
|
|
1725
|
-
var CompressionFailedError, COMPRESSION_SYSTEM;
|
|
1726
|
-
var init_compression_summarizer = __esm({
|
|
1727
|
-
"src/internal/runtime/compression/compression-summarizer.ts"() {
|
|
1728
|
-
CompressionFailedError = class extends Error {
|
|
1729
|
-
name = "CompressionFailedError";
|
|
1730
|
-
};
|
|
1731
|
-
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.";
|
|
1732
|
-
}
|
|
1733
|
-
});
|
|
1734
|
-
|
|
1735
|
-
// src/internal/runtime/compression/compression-model-registry.ts
|
|
1736
|
-
var compression_model_registry_exports = {};
|
|
1737
|
-
__export(compression_model_registry_exports, {
|
|
1738
|
-
CompressionModelUnresolvedError: () => CompressionModelUnresolvedError,
|
|
1739
|
-
resolveCompressionModel: () => resolveCompressionModel
|
|
1740
|
-
});
|
|
1741
|
-
function resolveCompressionModel(agentModel) {
|
|
1742
|
-
const exact = EXACT_REGISTRY.get(agentModel);
|
|
1743
|
-
if (exact !== void 0) return exact;
|
|
1744
|
-
const wildcard = matchWildcard(agentModel);
|
|
1745
|
-
if (wildcard !== null) return wildcard;
|
|
1746
|
-
if (isNoAuthProvider(agentModel)) return agentModel;
|
|
1747
|
-
throw new CompressionModelUnresolvedError(agentModel);
|
|
1748
|
-
}
|
|
1749
|
-
function matchWildcard(agentModel) {
|
|
1750
|
-
for (const [pattern, replacement] of WILDCARD_REGISTRY) {
|
|
1751
|
-
const prefix = pattern.endsWith("*") ? pattern.slice(0, -1) : pattern;
|
|
1752
|
-
if (!agentModel.startsWith(prefix)) continue;
|
|
1753
|
-
const suffix = agentModel.slice(prefix.length);
|
|
1754
|
-
const replPrefix = replacement.endsWith("*") ? replacement.slice(0, -1) : replacement;
|
|
1755
|
-
return `${replPrefix}${suffix}`;
|
|
1756
|
-
}
|
|
1757
|
-
return null;
|
|
1758
|
-
}
|
|
1759
|
-
function isNoAuthProvider(agentModel) {
|
|
1760
|
-
const slashIdx = agentModel.indexOf("/");
|
|
1761
|
-
if (slashIdx <= 0) return false;
|
|
1762
|
-
return NO_AUTH_PROVIDERS.has(agentModel.slice(0, slashIdx));
|
|
1763
|
-
}
|
|
1764
|
-
var EXACT_REGISTRY, WILDCARD_REGISTRY, NO_AUTH_PROVIDERS, CompressionModelUnresolvedError;
|
|
1765
|
-
var init_compression_model_registry = __esm({
|
|
1766
|
-
"src/internal/runtime/compression/compression-model-registry.ts"() {
|
|
1767
|
-
EXACT_REGISTRY = /* @__PURE__ */ new Map([
|
|
1768
|
-
// OpenAI family
|
|
1769
|
-
["openai/gpt-4o", "openai/gpt-4o-mini"],
|
|
1770
|
-
["openai/gpt-4-turbo", "openai/gpt-4o-mini"],
|
|
1771
|
-
["openai/gpt-4", "openai/gpt-4o-mini"],
|
|
1772
|
-
["openai/o1-preview", "openai/gpt-4o-mini"],
|
|
1773
|
-
["openai/o1", "openai/gpt-4o-mini"],
|
|
1774
|
-
["openai/o3", "openai/gpt-4o-mini"],
|
|
1775
|
-
["openai/o3-mini", "openai/gpt-4o-mini"],
|
|
1776
|
-
// Anthropic family
|
|
1777
|
-
["anthropic/claude-opus-4", "anthropic/claude-3-5-haiku-latest"],
|
|
1778
|
-
["anthropic/claude-sonnet-4", "anthropic/claude-3-5-haiku-latest"],
|
|
1779
|
-
["anthropic/claude-3-5-sonnet", "anthropic/claude-3-5-haiku-latest"],
|
|
1780
|
-
["anthropic/claude-3-5-sonnet-latest", "anthropic/claude-3-5-haiku-latest"],
|
|
1781
|
-
["anthropic/claude-3-opus", "anthropic/claude-3-haiku"],
|
|
1782
|
-
["anthropic/claude-3-sonnet", "anthropic/claude-3-haiku"],
|
|
1783
|
-
// Vertex (Gemini + Anthropic-on-Vertex)
|
|
1784
|
-
["vertex/gemini-1.5-pro", "vertex/gemini-1.5-flash"],
|
|
1785
|
-
["vertex/gemini-2.0-pro", "vertex/gemini-1.5-flash"],
|
|
1786
|
-
["vertex/claude-3-5-sonnet", "vertex/claude-3-5-haiku"],
|
|
1787
|
-
["vertex/claude-3-opus", "vertex/claude-3-haiku"],
|
|
1788
|
-
// OpenRouter (preserve openrouter prefix, swap tier within same vendor)
|
|
1789
|
-
["openrouter/openai/gpt-4o", "openrouter/openai/gpt-4o-mini"],
|
|
1790
|
-
["openrouter/openai/gpt-4-turbo", "openrouter/openai/gpt-4o-mini"],
|
|
1791
|
-
["openrouter/anthropic/claude-3-5-sonnet", "openrouter/anthropic/claude-3-5-haiku"],
|
|
1792
|
-
["openrouter/anthropic/claude-opus-4", "openrouter/anthropic/claude-3-5-haiku"]
|
|
1793
|
-
]);
|
|
1794
|
-
WILDCARD_REGISTRY = [
|
|
1795
|
-
// Bedrock Anthropic: us.anthropic.claude-sonnet-* → us.anthropic.claude-3-haiku-*
|
|
1796
|
-
["bedrock/anthropic.claude-sonnet*", "bedrock/anthropic.claude-3-haiku*"],
|
|
1797
|
-
["bedrock/anthropic.claude-opus*", "bedrock/anthropic.claude-3-haiku*"],
|
|
1798
|
-
["bedrock/anthropic.claude-3-5-sonnet*", "bedrock/anthropic.claude-3-5-haiku*"]
|
|
1799
|
-
];
|
|
1800
|
-
NO_AUTH_PROVIDERS = /* @__PURE__ */ new Set(["ollama", "lmstudio", "llamacpp"]);
|
|
1801
|
-
CompressionModelUnresolvedError = class extends Error {
|
|
1802
|
-
name = "CompressionModelUnresolvedError";
|
|
1803
|
-
agentModel;
|
|
1804
|
-
constructor(agentModel) {
|
|
1805
|
-
super(
|
|
1806
|
-
`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).`
|
|
1807
|
-
);
|
|
1808
|
-
this.agentModel = agentModel;
|
|
1809
|
-
}
|
|
1810
|
-
};
|
|
1811
|
-
}
|
|
1812
|
-
});
|
|
1813
1607
|
var MODALITIES, costSchema, limitSchema, modalitiesSchema, catalogModelSchema;
|
|
1814
1608
|
var init_catalog_schema = __esm({
|
|
1815
1609
|
"src/internal/providers/catalog-schema.ts"() {
|
|
@@ -1877,13 +1671,6 @@ function getProviderProfile(name) {
|
|
|
1877
1671
|
const canonical = ALIASES.get(name) ?? name;
|
|
1878
1672
|
return REGISTRY.get(canonical);
|
|
1879
1673
|
}
|
|
1880
|
-
function listProviders() {
|
|
1881
|
-
return Array.from(REGISTRY.values());
|
|
1882
|
-
}
|
|
1883
|
-
function _resetProvidersForTests() {
|
|
1884
|
-
REGISTRY.clear();
|
|
1885
|
-
ALIASES.clear();
|
|
1886
|
-
}
|
|
1887
1674
|
var REGISTRY, ALIASES;
|
|
1888
1675
|
var init_registry = __esm({
|
|
1889
1676
|
"src/internal/providers/registry.ts"() {
|
|
@@ -1894,19 +1681,6 @@ var init_registry = __esm({
|
|
|
1894
1681
|
ALIASES = globalSingleton("theokit-sdk.providers.aliases", () => /* @__PURE__ */ new Map());
|
|
1895
1682
|
}
|
|
1896
1683
|
});
|
|
1897
|
-
|
|
1898
|
-
// src/internal/providers/catalog-loader.ts
|
|
1899
|
-
var catalog_loader_exports = {};
|
|
1900
|
-
__export(catalog_loader_exports, {
|
|
1901
|
-
_resetModelInfoIndexForTests: () => _resetModelInfoIndexForTests,
|
|
1902
|
-
getCatalogCapabilities: () => getCatalogCapabilities,
|
|
1903
|
-
getCatalogModelInfo: () => getCatalogModelInfo,
|
|
1904
|
-
isPatchedModelKey: () => isPatchedModelKey,
|
|
1905
|
-
listModelInfoKeys: () => listModelInfoKeys,
|
|
1906
|
-
loadProviderCatalog: () => loadProviderCatalog,
|
|
1907
|
-
patchModelInfo: () => patchModelInfo,
|
|
1908
|
-
registerCatalogProviders: () => registerCatalogProviders
|
|
1909
|
-
});
|
|
1910
1684
|
function globalSingleton2(key, create) {
|
|
1911
1685
|
const g = globalThis;
|
|
1912
1686
|
const sym = Symbol.for(key);
|
|
@@ -1920,16 +1694,6 @@ function getCatalogModelInfo(key) {
|
|
|
1920
1694
|
function isPatchedModelKey(key) {
|
|
1921
1695
|
return patchedModelKeys.has(key);
|
|
1922
1696
|
}
|
|
1923
|
-
function patchModelInfo(key, model) {
|
|
1924
|
-
ensureModelIndexLoaded();
|
|
1925
|
-
const existing = modelInfoIndex.get(key);
|
|
1926
|
-
modelInfoIndex.set(key, existing === void 0 ? model : { ...existing, ...model });
|
|
1927
|
-
patchedModelKeys.add(key);
|
|
1928
|
-
}
|
|
1929
|
-
function listModelInfoKeys() {
|
|
1930
|
-
ensureModelIndexLoaded();
|
|
1931
|
-
return [...modelInfoIndex.keys()];
|
|
1932
|
-
}
|
|
1933
1697
|
function ensureModelIndexLoaded() {
|
|
1934
1698
|
if (indexState.loaded) return;
|
|
1935
1699
|
indexState.loaded = true;
|
|
@@ -1963,11 +1727,6 @@ function indexEntryModels(entry) {
|
|
|
1963
1727
|
}
|
|
1964
1728
|
}
|
|
1965
1729
|
}
|
|
1966
|
-
function _resetModelInfoIndexForTests() {
|
|
1967
|
-
modelInfoIndex.clear();
|
|
1968
|
-
patchedModelKeys.clear();
|
|
1969
|
-
indexState.loaded = false;
|
|
1970
|
-
}
|
|
1971
1730
|
function validateEntry(raw) {
|
|
1972
1731
|
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") {
|
|
1973
1732
|
return null;
|
|
@@ -1978,12 +1737,6 @@ function loadProviderCatalog(opts) {
|
|
|
1978
1737
|
const catalogPath = path.join(__dirname_resolved, "provider-catalog.json");
|
|
1979
1738
|
const rawText = fs.readFileSync(catalogPath, "utf-8");
|
|
1980
1739
|
let entries = JSON.parse(rawText);
|
|
1981
|
-
if (opts?._testInjectMalformed) {
|
|
1982
|
-
entries = [
|
|
1983
|
-
...entries,
|
|
1984
|
-
{ id: "malformed-provider", displayName: "Bad" }
|
|
1985
|
-
];
|
|
1986
|
-
}
|
|
1987
1740
|
const result = {};
|
|
1988
1741
|
for (const raw of entries) {
|
|
1989
1742
|
const validated = validateEntry(raw);
|
|
@@ -1998,21 +1751,8 @@ function loadProviderCatalog(opts) {
|
|
|
1998
1751
|
}
|
|
1999
1752
|
return result;
|
|
2000
1753
|
}
|
|
2001
|
-
function getCatalogCapabilities(providerId) {
|
|
2002
|
-
if (_capabilitiesCache === null) {
|
|
2003
|
-
const catalog = loadProviderCatalog();
|
|
2004
|
-
_capabilitiesCache = {};
|
|
2005
|
-
for (const entry of Object.values(catalog)) {
|
|
2006
|
-
_capabilitiesCache[entry.id] = entry.capabilities;
|
|
2007
|
-
for (const alias of entry.aliases ?? []) {
|
|
2008
|
-
if (_capabilitiesCache[alias] === void 0) _capabilitiesCache[alias] = entry.capabilities;
|
|
2009
|
-
}
|
|
2010
|
-
}
|
|
2011
|
-
}
|
|
2012
|
-
return _capabilitiesCache[providerId];
|
|
2013
|
-
}
|
|
2014
1754
|
function registerCatalogProviders(opts) {
|
|
2015
|
-
const catalog = loadProviderCatalog(
|
|
1755
|
+
const catalog = loadProviderCatalog();
|
|
2016
1756
|
for (const entry of Object.values(catalog)) {
|
|
2017
1757
|
if (getProviderProfile(entry.id) !== void 0) continue;
|
|
2018
1758
|
if (entry.aliases?.some((a) => getProviderProfile(a) !== void 0)) continue;
|
|
@@ -2032,7 +1772,7 @@ function registerCatalogProviders(opts) {
|
|
|
2032
1772
|
registerProvider(profile);
|
|
2033
1773
|
}
|
|
2034
1774
|
}
|
|
2035
|
-
var __dirname_resolved, modelInfoIndex, patchedModelKeys, indexState
|
|
1775
|
+
var __dirname_resolved, modelInfoIndex, patchedModelKeys, indexState;
|
|
2036
1776
|
var init_catalog_loader = __esm({
|
|
2037
1777
|
"src/internal/providers/catalog-loader.ts"() {
|
|
2038
1778
|
init_catalog_schema();
|
|
@@ -2049,7 +1789,15 @@ var init_catalog_loader = __esm({
|
|
|
2049
1789
|
indexState = globalSingleton2("theokit-sdk.providers.model-info-loaded", () => ({
|
|
2050
1790
|
loaded: false
|
|
2051
1791
|
}));
|
|
2052
|
-
|
|
1792
|
+
}
|
|
1793
|
+
});
|
|
1794
|
+
|
|
1795
|
+
// src/compaction.ts
|
|
1796
|
+
function estimateTokens(text) {
|
|
1797
|
+
return Math.ceil(text.length / 4);
|
|
1798
|
+
}
|
|
1799
|
+
var init_compaction = __esm({
|
|
1800
|
+
"src/compaction.ts"() {
|
|
2053
1801
|
}
|
|
2054
1802
|
});
|
|
2055
1803
|
|
|
@@ -2816,9 +2564,6 @@ function registerBuiltins() {
|
|
|
2816
2564
|
registerProvider(CEREBRAS);
|
|
2817
2565
|
registerCatalogProviders();
|
|
2818
2566
|
}
|
|
2819
|
-
function _resetBuiltinsRegistered() {
|
|
2820
|
-
_registeredState.done = false;
|
|
2821
|
-
}
|
|
2822
2567
|
var _registeredState;
|
|
2823
2568
|
var init_builtin = __esm({
|
|
2824
2569
|
"src/internal/providers/builtin/index.ts"() {
|
|
@@ -2945,9 +2690,6 @@ async function loadOne(dir, entryName) {
|
|
|
2945
2690
|
}
|
|
2946
2691
|
}
|
|
2947
2692
|
}
|
|
2948
|
-
function _resetDiscovery() {
|
|
2949
|
-
discoveryState.done = false;
|
|
2950
|
-
}
|
|
2951
2693
|
var discoveryState;
|
|
2952
2694
|
var init_discovery = __esm({
|
|
2953
2695
|
"src/internal/providers/discovery.ts"() {
|
|
@@ -2959,25 +2701,9 @@ var init_discovery = __esm({
|
|
|
2959
2701
|
});
|
|
2960
2702
|
|
|
2961
2703
|
// src/internal/providers/index.ts
|
|
2962
|
-
var providers_exports = {};
|
|
2963
|
-
__export(providers_exports, {
|
|
2964
|
-
ANTHROPIC: () => ANTHROPIC,
|
|
2965
|
-
GEMINI: () => GEMINI,
|
|
2966
|
-
OPENAI: () => OPENAI,
|
|
2967
|
-
OPENROUTER: () => OPENROUTER,
|
|
2968
|
-
_resetBuiltinsRegistered: () => _resetBuiltinsRegistered,
|
|
2969
|
-
_resetDiscovery: () => _resetDiscovery,
|
|
2970
|
-
_resetProvidersForTests: () => _resetProvidersForTests,
|
|
2971
|
-
discoverProviderPlugins: () => discoverProviderPlugins,
|
|
2972
|
-
getProviderProfile: () => getProviderProfile,
|
|
2973
|
-
listProviders: () => listProviders,
|
|
2974
|
-
registerBuiltins: () => registerBuiltins,
|
|
2975
|
-
registerProvider: () => registerProvider
|
|
2976
|
-
});
|
|
2977
2704
|
var init_providers = __esm({
|
|
2978
2705
|
"src/internal/providers/index.ts"() {
|
|
2979
2706
|
init_builtin();
|
|
2980
|
-
init_discovery();
|
|
2981
2707
|
init_registry();
|
|
2982
2708
|
}
|
|
2983
2709
|
});
|
|
@@ -5718,12 +5444,6 @@ var init_vertex_router = __esm({
|
|
|
5718
5444
|
});
|
|
5719
5445
|
|
|
5720
5446
|
// src/internal/llm/router.ts
|
|
5721
|
-
var router_exports = {};
|
|
5722
|
-
__export(router_exports, {
|
|
5723
|
-
_resetCredentialPoolWarnings: () => _resetCredentialPoolWarnings,
|
|
5724
|
-
_resetNoAuthApiKeyWarnings: () => _resetNoAuthApiKeyWarnings,
|
|
5725
|
-
resolveProviderChain: () => resolveProviderChain
|
|
5726
|
-
});
|
|
5727
5447
|
function resolveProviderChain(options) {
|
|
5728
5448
|
registerBuiltins();
|
|
5729
5449
|
return buildChain(options);
|
|
@@ -5821,9 +5541,6 @@ function warnNoAuthApiKeysIgnoredOnce(provider) {
|
|
|
5821
5541
|
`
|
|
5822
5542
|
);
|
|
5823
5543
|
}
|
|
5824
|
-
function _resetNoAuthApiKeyWarnings() {
|
|
5825
|
-
warnedNoAuthApiKeys.clear();
|
|
5826
|
-
}
|
|
5827
5544
|
function sentinelForNoAuth(profile) {
|
|
5828
5545
|
return profile.authType === "none" ? profile.name : void 0;
|
|
5829
5546
|
}
|
|
@@ -5854,9 +5571,6 @@ function warnUnknownProvidersInApiKeys(apiKeys) {
|
|
|
5854
5571
|
}
|
|
5855
5572
|
}
|
|
5856
5573
|
}
|
|
5857
|
-
function _resetCredentialPoolWarnings() {
|
|
5858
|
-
warnedProviders.clear();
|
|
5859
|
-
}
|
|
5860
5574
|
function resolveApiKey2(envVars) {
|
|
5861
5575
|
for (const v of envVars) {
|
|
5862
5576
|
const value = process.env[v];
|
|
@@ -5940,61 +5654,338 @@ function selectTransport(profile, apiKey) {
|
|
|
5940
5654
|
{ code: "transport_unavailable" }
|
|
5941
5655
|
);
|
|
5942
5656
|
}
|
|
5943
|
-
var warnedNoAuthApiKeys, warnedProviders;
|
|
5944
|
-
var init_router = __esm({
|
|
5945
|
-
"src/internal/llm/router.ts"() {
|
|
5946
|
-
init_errors();
|
|
5947
|
-
init_providers();
|
|
5948
|
-
init_anthropic3();
|
|
5949
|
-
init_bedrock_anthropic();
|
|
5950
|
-
init_credential_pool();
|
|
5951
|
-
init_credential_pool_context();
|
|
5952
|
-
init_fault_injection();
|
|
5953
|
-
init_ollama_native();
|
|
5954
|
-
init_openai2();
|
|
5955
|
-
init_pool_aware_client();
|
|
5956
|
-
init_responses();
|
|
5957
|
-
init_vertex_router();
|
|
5958
|
-
warnedNoAuthApiKeys = /* @__PURE__ */ new Set();
|
|
5959
|
-
warnedProviders = /* @__PURE__ */ new Set();
|
|
5657
|
+
var warnedNoAuthApiKeys, warnedProviders;
|
|
5658
|
+
var init_router = __esm({
|
|
5659
|
+
"src/internal/llm/router.ts"() {
|
|
5660
|
+
init_errors();
|
|
5661
|
+
init_providers();
|
|
5662
|
+
init_anthropic3();
|
|
5663
|
+
init_bedrock_anthropic();
|
|
5664
|
+
init_credential_pool();
|
|
5665
|
+
init_credential_pool_context();
|
|
5666
|
+
init_fault_injection();
|
|
5667
|
+
init_ollama_native();
|
|
5668
|
+
init_openai2();
|
|
5669
|
+
init_pool_aware_client();
|
|
5670
|
+
init_responses();
|
|
5671
|
+
init_vertex_router();
|
|
5672
|
+
warnedNoAuthApiKeys = /* @__PURE__ */ new Set();
|
|
5673
|
+
warnedProviders = /* @__PURE__ */ new Set();
|
|
5674
|
+
}
|
|
5675
|
+
});
|
|
5676
|
+
|
|
5677
|
+
// src/internal/local-agent/real-local-run-provider.ts
|
|
5678
|
+
function inferProviderFromApiKey(apiKey) {
|
|
5679
|
+
if (apiKey === void 0 || apiKey.length === 0) return void 0;
|
|
5680
|
+
const byPrefix = [
|
|
5681
|
+
{ provider: "openrouter", prefix: "sk-or-" },
|
|
5682
|
+
{ provider: "anthropic", prefix: "sk-ant-" },
|
|
5683
|
+
{ provider: "openai", prefix: "sk-" }
|
|
5684
|
+
];
|
|
5685
|
+
for (const { provider, prefix } of byPrefix) {
|
|
5686
|
+
if (apiKey.startsWith(prefix) && getProviderProfile(provider) !== void 0) {
|
|
5687
|
+
return provider;
|
|
5688
|
+
}
|
|
5689
|
+
}
|
|
5690
|
+
return void 0;
|
|
5691
|
+
}
|
|
5692
|
+
function detectPrimaryProvider() {
|
|
5693
|
+
if (process.env.ANTHROPIC_API_KEY !== void 0 && process.env.ANTHROPIC_API_KEY.length > 0) {
|
|
5694
|
+
return "anthropic";
|
|
5695
|
+
}
|
|
5696
|
+
if (process.env.OPENAI_API_KEY !== void 0 && process.env.OPENAI_API_KEY.length > 0) {
|
|
5697
|
+
return "openai";
|
|
5698
|
+
}
|
|
5699
|
+
if (process.env.OPENROUTER_API_KEY !== void 0 && process.env.OPENROUTER_API_KEY.length > 0) {
|
|
5700
|
+
return "openrouter";
|
|
5701
|
+
}
|
|
5702
|
+
return "openai";
|
|
5703
|
+
}
|
|
5704
|
+
var init_real_local_run_provider = __esm({
|
|
5705
|
+
"src/internal/local-agent/real-local-run-provider.ts"() {
|
|
5706
|
+
init_providers();
|
|
5707
|
+
}
|
|
5708
|
+
});
|
|
5709
|
+
|
|
5710
|
+
// src/internal/runtime/compression/compression-model-registry.ts
|
|
5711
|
+
function resolveCompressionModel(agentModel) {
|
|
5712
|
+
const exact = EXACT_REGISTRY.get(agentModel);
|
|
5713
|
+
if (exact !== void 0) return exact;
|
|
5714
|
+
const wildcard = matchWildcard(agentModel);
|
|
5715
|
+
if (wildcard !== null) return wildcard;
|
|
5716
|
+
if (isNoAuthProvider(agentModel)) return agentModel;
|
|
5717
|
+
throw new CompressionModelUnresolvedError(agentModel);
|
|
5718
|
+
}
|
|
5719
|
+
function matchWildcard(agentModel) {
|
|
5720
|
+
for (const [pattern, replacement] of WILDCARD_REGISTRY) {
|
|
5721
|
+
const prefix = pattern.endsWith("*") ? pattern.slice(0, -1) : pattern;
|
|
5722
|
+
if (!agentModel.startsWith(prefix)) continue;
|
|
5723
|
+
const suffix = agentModel.slice(prefix.length);
|
|
5724
|
+
const replPrefix = replacement.endsWith("*") ? replacement.slice(0, -1) : replacement;
|
|
5725
|
+
return `${replPrefix}${suffix}`;
|
|
5726
|
+
}
|
|
5727
|
+
return null;
|
|
5728
|
+
}
|
|
5729
|
+
function isNoAuthProvider(agentModel) {
|
|
5730
|
+
const slashIdx = agentModel.indexOf("/");
|
|
5731
|
+
if (slashIdx <= 0) return false;
|
|
5732
|
+
return NO_AUTH_PROVIDERS.has(agentModel.slice(0, slashIdx));
|
|
5733
|
+
}
|
|
5734
|
+
var EXACT_REGISTRY, WILDCARD_REGISTRY, NO_AUTH_PROVIDERS, CompressionModelUnresolvedError;
|
|
5735
|
+
var init_compression_model_registry = __esm({
|
|
5736
|
+
"src/internal/runtime/compression/compression-model-registry.ts"() {
|
|
5737
|
+
EXACT_REGISTRY = /* @__PURE__ */ new Map([
|
|
5738
|
+
// OpenAI family
|
|
5739
|
+
["openai/gpt-4o", "openai/gpt-4o-mini"],
|
|
5740
|
+
["openai/gpt-4-turbo", "openai/gpt-4o-mini"],
|
|
5741
|
+
["openai/gpt-4", "openai/gpt-4o-mini"],
|
|
5742
|
+
["openai/o1-preview", "openai/gpt-4o-mini"],
|
|
5743
|
+
["openai/o1", "openai/gpt-4o-mini"],
|
|
5744
|
+
["openai/o3", "openai/gpt-4o-mini"],
|
|
5745
|
+
["openai/o3-mini", "openai/gpt-4o-mini"],
|
|
5746
|
+
// Anthropic family
|
|
5747
|
+
["anthropic/claude-opus-4", "anthropic/claude-3-5-haiku-latest"],
|
|
5748
|
+
["anthropic/claude-sonnet-4", "anthropic/claude-3-5-haiku-latest"],
|
|
5749
|
+
["anthropic/claude-3-5-sonnet", "anthropic/claude-3-5-haiku-latest"],
|
|
5750
|
+
["anthropic/claude-3-5-sonnet-latest", "anthropic/claude-3-5-haiku-latest"],
|
|
5751
|
+
["anthropic/claude-3-opus", "anthropic/claude-3-haiku"],
|
|
5752
|
+
["anthropic/claude-3-sonnet", "anthropic/claude-3-haiku"],
|
|
5753
|
+
// Vertex (Gemini + Anthropic-on-Vertex)
|
|
5754
|
+
["vertex/gemini-1.5-pro", "vertex/gemini-1.5-flash"],
|
|
5755
|
+
["vertex/gemini-2.0-pro", "vertex/gemini-1.5-flash"],
|
|
5756
|
+
["vertex/claude-3-5-sonnet", "vertex/claude-3-5-haiku"],
|
|
5757
|
+
["vertex/claude-3-opus", "vertex/claude-3-haiku"],
|
|
5758
|
+
// OpenRouter (preserve openrouter prefix, swap tier within same vendor)
|
|
5759
|
+
["openrouter/openai/gpt-4o", "openrouter/openai/gpt-4o-mini"],
|
|
5760
|
+
["openrouter/openai/gpt-4-turbo", "openrouter/openai/gpt-4o-mini"],
|
|
5761
|
+
["openrouter/anthropic/claude-3-5-sonnet", "openrouter/anthropic/claude-3-5-haiku"],
|
|
5762
|
+
["openrouter/anthropic/claude-opus-4", "openrouter/anthropic/claude-3-5-haiku"]
|
|
5763
|
+
]);
|
|
5764
|
+
WILDCARD_REGISTRY = [
|
|
5765
|
+
// Bedrock Anthropic: us.anthropic.claude-sonnet-* → us.anthropic.claude-3-haiku-*
|
|
5766
|
+
["bedrock/anthropic.claude-sonnet*", "bedrock/anthropic.claude-3-haiku*"],
|
|
5767
|
+
["bedrock/anthropic.claude-opus*", "bedrock/anthropic.claude-3-haiku*"],
|
|
5768
|
+
["bedrock/anthropic.claude-3-5-sonnet*", "bedrock/anthropic.claude-3-5-haiku*"]
|
|
5769
|
+
];
|
|
5770
|
+
NO_AUTH_PROVIDERS = /* @__PURE__ */ new Set(["ollama", "lmstudio", "llamacpp"]);
|
|
5771
|
+
CompressionModelUnresolvedError = class extends Error {
|
|
5772
|
+
name = "CompressionModelUnresolvedError";
|
|
5773
|
+
agentModel;
|
|
5774
|
+
constructor(agentModel) {
|
|
5775
|
+
super(
|
|
5776
|
+
`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).`
|
|
5777
|
+
);
|
|
5778
|
+
this.agentModel = agentModel;
|
|
5779
|
+
}
|
|
5780
|
+
};
|
|
5781
|
+
}
|
|
5782
|
+
});
|
|
5783
|
+
|
|
5784
|
+
// src/internal/runtime/compression/compression-summarizer.ts
|
|
5785
|
+
function buildCompressionPrompt(messages) {
|
|
5786
|
+
const formatted = messages.map((m) => `[${m.role}]: ${m.content}`).join("\n\n");
|
|
5787
|
+
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.
|
|
5788
|
+
|
|
5789
|
+
--- CONVERSATION TO SUMMARIZE ---
|
|
5790
|
+
${formatted}
|
|
5791
|
+
--- END ---`;
|
|
5792
|
+
}
|
|
5793
|
+
async function compressConversationWindow(opts) {
|
|
5794
|
+
const userPrompt = buildCompressionPrompt(opts.messages);
|
|
5795
|
+
let summary;
|
|
5796
|
+
try {
|
|
5797
|
+
summary = await opts.callLlm(opts.model, COMPRESSION_SYSTEM, userPrompt);
|
|
5798
|
+
} catch (cause) {
|
|
5799
|
+
throw new CompressionFailedError(
|
|
5800
|
+
`Compression LLM call failed: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
5801
|
+
{ cause: cause instanceof Error ? cause : void 0 }
|
|
5802
|
+
);
|
|
5803
|
+
}
|
|
5804
|
+
if (!summary || summary.trim().length === 0) {
|
|
5805
|
+
throw new CompressionFailedError(
|
|
5806
|
+
"Compression LLM returned empty summary \u2014 reduction ineffective."
|
|
5807
|
+
);
|
|
5808
|
+
}
|
|
5809
|
+
return {
|
|
5810
|
+
role: "system",
|
|
5811
|
+
content: `[Compressed conversation summary]: ${summary.trim()}`
|
|
5812
|
+
};
|
|
5813
|
+
}
|
|
5814
|
+
var CompressionFailedError, COMPRESSION_SYSTEM;
|
|
5815
|
+
var init_compression_summarizer = __esm({
|
|
5816
|
+
"src/internal/runtime/compression/compression-summarizer.ts"() {
|
|
5817
|
+
CompressionFailedError = class extends Error {
|
|
5818
|
+
name = "CompressionFailedError";
|
|
5819
|
+
};
|
|
5820
|
+
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.";
|
|
5821
|
+
}
|
|
5822
|
+
});
|
|
5823
|
+
|
|
5824
|
+
// src/internal/session/agent-session-store.ts
|
|
5825
|
+
function seedTranscript(prior, opts) {
|
|
5826
|
+
return SessionTranscript.fromRecords(prior, opts);
|
|
5827
|
+
}
|
|
5828
|
+
function mapAgentTurn(steps) {
|
|
5829
|
+
const assistant = {};
|
|
5830
|
+
const toolResults = [];
|
|
5831
|
+
const toolCalls = [];
|
|
5832
|
+
for (const step of steps) {
|
|
5833
|
+
if (step.type === "thinkingMessage") assistant.thinking = step.message.text;
|
|
5834
|
+
else if (step.type === "assistantMessage") assistant.text = step.message.text;
|
|
5835
|
+
else if (step.type === "toolCall")
|
|
5836
|
+
toolCalls.push({
|
|
5837
|
+
id: step.message.callId,
|
|
5838
|
+
name: step.message.name,
|
|
5839
|
+
input: step.message.args ?? {}
|
|
5840
|
+
});
|
|
5841
|
+
else
|
|
5842
|
+
toolResults.push({
|
|
5843
|
+
toolUseId: step.message.callId,
|
|
5844
|
+
content: step.message.result,
|
|
5845
|
+
isError: step.message.isError
|
|
5846
|
+
});
|
|
5847
|
+
}
|
|
5848
|
+
if (toolCalls.length > 0) assistant.toolCalls = toolCalls;
|
|
5849
|
+
return { assistant, toolResults };
|
|
5850
|
+
}
|
|
5851
|
+
function hasAssistantContent(a) {
|
|
5852
|
+
return a.text !== void 0 || a.thinking !== void 0 || (a.toolCalls?.length ?? 0) > 0;
|
|
5853
|
+
}
|
|
5854
|
+
function appendConversation(transcript, conversation) {
|
|
5855
|
+
for (const ct of conversation) {
|
|
5856
|
+
if (ct.type !== "agentConversationTurn") continue;
|
|
5857
|
+
const { assistant, toolResults } = mapAgentTurn(ct.turn.steps);
|
|
5858
|
+
if (hasAssistantContent(assistant)) transcript.appendAssistantTurn(assistant);
|
|
5859
|
+
if (toolResults.length > 0) transcript.appendToolResults(toolResults);
|
|
5860
|
+
}
|
|
5861
|
+
}
|
|
5862
|
+
async function readSessionMessages(store, agentId) {
|
|
5863
|
+
const records = await store.readRecords(agentId);
|
|
5864
|
+
return reconstructMessages(records).map(narrowToSessionMessage);
|
|
5865
|
+
}
|
|
5866
|
+
function partToText(p) {
|
|
5867
|
+
if (p.type === "text") return p.text ?? "";
|
|
5868
|
+
if (p.type === "tool_use") return `[tool call] ${p.name ?? ""}`;
|
|
5869
|
+
if (p.type === "tool_result") {
|
|
5870
|
+
const body = typeof p.content === "string" ? p.content : JSON.stringify(p.content);
|
|
5871
|
+
return `[tool result] ${body}`;
|
|
5872
|
+
}
|
|
5873
|
+
return "";
|
|
5874
|
+
}
|
|
5875
|
+
function narrowToSessionMessage(m) {
|
|
5876
|
+
const role = m.role === "user" ? "user" : "assistant";
|
|
5877
|
+
const text = m.content.map(partToText).filter((s) => s.length > 0).join("\n");
|
|
5878
|
+
return { role, text };
|
|
5879
|
+
}
|
|
5880
|
+
function deltaRecords(transcript, priorLength) {
|
|
5881
|
+
return transcript.records().slice(priorLength);
|
|
5882
|
+
}
|
|
5883
|
+
async function persistTurn(store, loc, sessionId, turn) {
|
|
5884
|
+
const prior = await store.readRecords(loc.agentId);
|
|
5885
|
+
const transcript = seedTranscript(prior, { cwd: loc.cwd, sessionId, model: loc.model });
|
|
5886
|
+
transcript.appendUserTurn(turn.userText);
|
|
5887
|
+
appendConversation(transcript, turn.conversation);
|
|
5888
|
+
await store.appendRecords(loc.agentId, deltaRecords(transcript, prior.length));
|
|
5889
|
+
}
|
|
5890
|
+
var init_agent_session_store = __esm({
|
|
5891
|
+
"src/internal/session/agent-session-store.ts"() {
|
|
5892
|
+
init_session_transcript();
|
|
5960
5893
|
}
|
|
5961
5894
|
});
|
|
5962
5895
|
|
|
5963
|
-
// src/internal/
|
|
5964
|
-
|
|
5965
|
-
|
|
5966
|
-
|
|
5967
|
-
|
|
5968
|
-
|
|
5969
|
-
|
|
5970
|
-
|
|
5971
|
-
|
|
5972
|
-
|
|
5973
|
-
|
|
5974
|
-
|
|
5975
|
-
|
|
5976
|
-
|
|
5977
|
-
|
|
5978
|
-
|
|
5896
|
+
// src/internal/session/agent-session.ts
|
|
5897
|
+
function transcriptKey(cwd, agentId) {
|
|
5898
|
+
return `${cwd}::${agentId}`;
|
|
5899
|
+
}
|
|
5900
|
+
function appendSessionMessage(agentId, message) {
|
|
5901
|
+
const existing = sessions.get(agentId) ?? [];
|
|
5902
|
+
existing.push(message);
|
|
5903
|
+
sessions.set(agentId, existing);
|
|
5904
|
+
}
|
|
5905
|
+
function getSessionMessages(agentId) {
|
|
5906
|
+
return sessions.get(agentId) ?? [];
|
|
5907
|
+
}
|
|
5908
|
+
function persistTurnToTranscript(store, loc, sessionId, turn, onCompact) {
|
|
5909
|
+
const key = transcriptKey(loc.cwd, loc.agentId);
|
|
5910
|
+
const chained = (pendingWrites.get(key) ?? Promise.resolve()).then(async () => {
|
|
5911
|
+
try {
|
|
5912
|
+
await persistTurn(store, loc, sessionId, turn);
|
|
5913
|
+
const count = (recordCounts.get(key) ?? 0) + 1;
|
|
5914
|
+
recordCounts.set(key, count);
|
|
5915
|
+
if (turn.autoCompact !== void 0) {
|
|
5916
|
+
const { autoCompactIfNeeded: autoCompactIfNeeded2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
5917
|
+
const fired = await autoCompactIfNeeded2({
|
|
5918
|
+
store,
|
|
5919
|
+
loc,
|
|
5920
|
+
sessionId,
|
|
5921
|
+
usageTotal: turn.autoCompact.usageTotal,
|
|
5922
|
+
contextWindow: turn.autoCompact.contextWindow,
|
|
5923
|
+
turnCount: count,
|
|
5924
|
+
summarize: turn.autoCompact.summarize
|
|
5925
|
+
});
|
|
5926
|
+
if (fired) onCompact?.();
|
|
5927
|
+
}
|
|
5928
|
+
} catch (cause) {
|
|
5929
|
+
const msg = cause instanceof Error ? cause.message : String(cause);
|
|
5930
|
+
process.stderr.write(
|
|
5931
|
+
`[theokit-sdk] session transcript write failed (${loc.agentId}): ${msg}
|
|
5932
|
+
`
|
|
5933
|
+
);
|
|
5979
5934
|
}
|
|
5980
|
-
}
|
|
5981
|
-
|
|
5935
|
+
});
|
|
5936
|
+
pendingWrites.set(
|
|
5937
|
+
key,
|
|
5938
|
+
chained.then(
|
|
5939
|
+
() => void 0,
|
|
5940
|
+
() => void 0
|
|
5941
|
+
)
|
|
5942
|
+
);
|
|
5982
5943
|
}
|
|
5983
|
-
function
|
|
5984
|
-
|
|
5985
|
-
|
|
5986
|
-
|
|
5987
|
-
|
|
5988
|
-
|
|
5944
|
+
async function hydrateSession(agentId, loc) {
|
|
5945
|
+
const key = transcriptKey(loc.cwd, agentId);
|
|
5946
|
+
if (hydratedKeys.has(key)) return;
|
|
5947
|
+
hydratedKeys.add(key);
|
|
5948
|
+
const persisted = await readSessionMessages(loc.store, agentId);
|
|
5949
|
+
if (persisted.length === 0) return;
|
|
5950
|
+
if (!sessions.has(agentId) || sessions.get(agentId)?.length === 0) {
|
|
5951
|
+
sessions.set(agentId, persisted);
|
|
5989
5952
|
}
|
|
5990
|
-
|
|
5991
|
-
|
|
5953
|
+
}
|
|
5954
|
+
async function flushSessionWrites() {
|
|
5955
|
+
while (pendingWrites.size > 0) {
|
|
5956
|
+
const all = Array.from(pendingWrites.values());
|
|
5957
|
+
pendingWrites.clear();
|
|
5958
|
+
await Promise.all(all);
|
|
5992
5959
|
}
|
|
5993
|
-
return "openai";
|
|
5994
5960
|
}
|
|
5995
|
-
|
|
5996
|
-
|
|
5997
|
-
|
|
5961
|
+
function clearSession(agentId) {
|
|
5962
|
+
sessions.delete(agentId);
|
|
5963
|
+
}
|
|
5964
|
+
function invalidateSessionCache(cwd, agentId) {
|
|
5965
|
+
sessions.delete(agentId);
|
|
5966
|
+
hydratedKeys.delete(transcriptKey(cwd, agentId));
|
|
5967
|
+
}
|
|
5968
|
+
function enqueueSessionWrite(cwd, agentId, fn) {
|
|
5969
|
+
const key = transcriptKey(cwd, agentId);
|
|
5970
|
+
const prior = pendingWrites.get(key) ?? Promise.resolve();
|
|
5971
|
+
const result = prior.then(fn);
|
|
5972
|
+
pendingWrites.set(
|
|
5973
|
+
key,
|
|
5974
|
+
result.then(
|
|
5975
|
+
() => void 0,
|
|
5976
|
+
() => void 0
|
|
5977
|
+
)
|
|
5978
|
+
);
|
|
5979
|
+
return result;
|
|
5980
|
+
}
|
|
5981
|
+
var sessions, hydratedKeys, pendingWrites, recordCounts;
|
|
5982
|
+
var init_agent_session = __esm({
|
|
5983
|
+
"src/internal/session/agent-session.ts"() {
|
|
5984
|
+
init_agent_session_store();
|
|
5985
|
+
sessions = /* @__PURE__ */ new Map();
|
|
5986
|
+
hydratedKeys = /* @__PURE__ */ new Set();
|
|
5987
|
+
pendingWrites = /* @__PURE__ */ new Map();
|
|
5988
|
+
recordCounts = /* @__PURE__ */ new Map();
|
|
5998
5989
|
}
|
|
5999
5990
|
});
|
|
6000
5991
|
|
|
@@ -6059,35 +6050,29 @@ ${summaryBody}`;
|
|
|
6059
6050
|
transcript.appendUserTurn(summary);
|
|
6060
6051
|
const delta = transcript.records().slice(prior.length);
|
|
6061
6052
|
await opts.store.appendRecords(opts.loc.agentId, delta);
|
|
6062
|
-
|
|
6063
|
-
invalidateSessionCache2(opts.loc.cwd, opts.loc.agentId);
|
|
6053
|
+
invalidateSessionCache(opts.loc.cwd, opts.loc.agentId);
|
|
6064
6054
|
const postTokens = estimateTokens([...preserved, summary].join("\n"));
|
|
6065
6055
|
return { preTokens, postTokens };
|
|
6066
6056
|
}
|
|
6067
6057
|
function buildDefaultSummarizer(opts) {
|
|
6068
6058
|
return async (messages) => {
|
|
6069
|
-
const
|
|
6070
|
-
const { resolveCompressionModel: resolveCompressionModel2 } = await Promise.resolve().then(() => (init_compression_model_registry(), compression_model_registry_exports));
|
|
6071
|
-
const { resolveProviderChain: resolveProviderChain2 } = await Promise.resolve().then(() => (init_router(), router_exports));
|
|
6072
|
-
const { inferProviderFromApiKey: inferProviderFromApiKey2, detectPrimaryProvider: detectPrimaryProvider2 } = await Promise.resolve().then(() => (init_real_local_run_provider(), real_local_run_provider_exports));
|
|
6073
|
-
const keyProvider = inferProviderFromApiKey2(opts.apiKey);
|
|
6059
|
+
const keyProvider = inferProviderFromApiKey(opts.apiKey);
|
|
6074
6060
|
const modelPrefix = opts.agentModel.includes("/") ? opts.agentModel.slice(0, opts.agentModel.indexOf("/")) : void 0;
|
|
6075
|
-
|
|
6076
|
-
|
|
6077
|
-
const
|
|
6078
|
-
const provider = prefixProfile !== void 0 ? modelPrefix : keyProvider ?? detectPrimaryProvider2();
|
|
6061
|
+
registerBuiltins();
|
|
6062
|
+
const prefixProfile = modelPrefix !== void 0 ? getProviderProfile(modelPrefix) : void 0;
|
|
6063
|
+
const provider = prefixProfile !== void 0 ? modelPrefix : keyProvider ?? detectPrimaryProvider();
|
|
6079
6064
|
let model;
|
|
6080
6065
|
if (provider !== modelPrefix) {
|
|
6081
6066
|
model = opts.agentModel;
|
|
6082
6067
|
} else {
|
|
6083
6068
|
try {
|
|
6084
|
-
model =
|
|
6069
|
+
model = resolveCompressionModel(opts.agentModel);
|
|
6085
6070
|
} catch {
|
|
6086
6071
|
model = opts.agentModel;
|
|
6087
6072
|
}
|
|
6088
6073
|
}
|
|
6089
6074
|
const callLlm = async (m, system, user) => {
|
|
6090
|
-
const chain =
|
|
6075
|
+
const chain = resolveProviderChain({
|
|
6091
6076
|
primary: provider,
|
|
6092
6077
|
...opts.apiKey !== void 0 ? { apiKeys: { [provider]: [opts.apiKey] } } : {}
|
|
6093
6078
|
});
|
|
@@ -6107,7 +6092,7 @@ function buildDefaultSummarizer(opts) {
|
|
|
6107
6092
|
}
|
|
6108
6093
|
return text;
|
|
6109
6094
|
};
|
|
6110
|
-
const summary = await
|
|
6095
|
+
const summary = await compressConversationWindow({ messages: [...messages], model, callLlm });
|
|
6111
6096
|
return summary.content;
|
|
6112
6097
|
};
|
|
6113
6098
|
}
|
|
@@ -6141,6 +6126,12 @@ var COMPACT_SUMMARY_MARKER, COMPACT_USER_MESSAGE_MAX_TOKENS, autoCompactAttempts
|
|
|
6141
6126
|
var init_compact_session = __esm({
|
|
6142
6127
|
"src/internal/session/compact-session.ts"() {
|
|
6143
6128
|
init_compaction();
|
|
6129
|
+
init_router();
|
|
6130
|
+
init_real_local_run_provider();
|
|
6131
|
+
init_providers();
|
|
6132
|
+
init_compression_model_registry();
|
|
6133
|
+
init_compression_summarizer();
|
|
6134
|
+
init_agent_session();
|
|
6144
6135
|
init_session_transcript();
|
|
6145
6136
|
COMPACT_SUMMARY_MARKER = "[[theokit:compact-summary]]";
|
|
6146
6137
|
COMPACT_USER_MESSAGE_MAX_TOKENS = 2e4;
|
|
@@ -6152,122 +6143,6 @@ var init_compact_session = __esm({
|
|
|
6152
6143
|
})();
|
|
6153
6144
|
}
|
|
6154
6145
|
});
|
|
6155
|
-
|
|
6156
|
-
// src/internal/session/agent-session.ts
|
|
6157
|
-
var agent_session_exports = {};
|
|
6158
|
-
__export(agent_session_exports, {
|
|
6159
|
-
appendSessionMessage: () => appendSessionMessage,
|
|
6160
|
-
clearAllSessions: () => clearAllSessions,
|
|
6161
|
-
clearSession: () => clearSession,
|
|
6162
|
-
enqueueSessionWrite: () => enqueueSessionWrite,
|
|
6163
|
-
flushSessionWrites: () => flushSessionWrites,
|
|
6164
|
-
getSessionMessages: () => getSessionMessages,
|
|
6165
|
-
hydrateSession: () => hydrateSession,
|
|
6166
|
-
invalidateSessionCache: () => invalidateSessionCache,
|
|
6167
|
-
persistTurnToTranscript: () => persistTurnToTranscript
|
|
6168
|
-
});
|
|
6169
|
-
function transcriptKey(cwd, agentId) {
|
|
6170
|
-
return `${cwd}::${agentId}`;
|
|
6171
|
-
}
|
|
6172
|
-
function appendSessionMessage(agentId, message) {
|
|
6173
|
-
const existing = sessions.get(agentId) ?? [];
|
|
6174
|
-
existing.push(message);
|
|
6175
|
-
sessions.set(agentId, existing);
|
|
6176
|
-
}
|
|
6177
|
-
function getSessionMessages(agentId) {
|
|
6178
|
-
return sessions.get(agentId) ?? [];
|
|
6179
|
-
}
|
|
6180
|
-
function persistTurnToTranscript(store, loc, sessionId, turn, onCompact) {
|
|
6181
|
-
const key = transcriptKey(loc.cwd, loc.agentId);
|
|
6182
|
-
const chained = (pendingWrites.get(key) ?? Promise.resolve()).then(async () => {
|
|
6183
|
-
try {
|
|
6184
|
-
await persistTurn(store, loc, sessionId, turn);
|
|
6185
|
-
const count = (recordCounts.get(key) ?? 0) + 1;
|
|
6186
|
-
recordCounts.set(key, count);
|
|
6187
|
-
if (turn.autoCompact !== void 0) {
|
|
6188
|
-
const { autoCompactIfNeeded: autoCompactIfNeeded2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
6189
|
-
const fired = await autoCompactIfNeeded2({
|
|
6190
|
-
store,
|
|
6191
|
-
loc,
|
|
6192
|
-
sessionId,
|
|
6193
|
-
usageTotal: turn.autoCompact.usageTotal,
|
|
6194
|
-
contextWindow: turn.autoCompact.contextWindow,
|
|
6195
|
-
turnCount: count,
|
|
6196
|
-
summarize: turn.autoCompact.summarize
|
|
6197
|
-
});
|
|
6198
|
-
if (fired) onCompact?.();
|
|
6199
|
-
}
|
|
6200
|
-
} catch (cause) {
|
|
6201
|
-
const msg = cause instanceof Error ? cause.message : String(cause);
|
|
6202
|
-
process.stderr.write(
|
|
6203
|
-
`[theokit-sdk] session transcript write failed (${loc.agentId}): ${msg}
|
|
6204
|
-
`
|
|
6205
|
-
);
|
|
6206
|
-
}
|
|
6207
|
-
});
|
|
6208
|
-
pendingWrites.set(
|
|
6209
|
-
key,
|
|
6210
|
-
chained.then(
|
|
6211
|
-
() => void 0,
|
|
6212
|
-
() => void 0
|
|
6213
|
-
)
|
|
6214
|
-
);
|
|
6215
|
-
}
|
|
6216
|
-
async function hydrateSession(agentId, loc) {
|
|
6217
|
-
const key = transcriptKey(loc.cwd, agentId);
|
|
6218
|
-
if (hydratedKeys.has(key)) return;
|
|
6219
|
-
hydratedKeys.add(key);
|
|
6220
|
-
const persisted = await readSessionMessages(loc.store, agentId);
|
|
6221
|
-
if (persisted.length === 0) return;
|
|
6222
|
-
if (!sessions.has(agentId) || sessions.get(agentId)?.length === 0) {
|
|
6223
|
-
sessions.set(agentId, persisted);
|
|
6224
|
-
}
|
|
6225
|
-
}
|
|
6226
|
-
async function flushSessionWrites() {
|
|
6227
|
-
while (pendingWrites.size > 0) {
|
|
6228
|
-
const all = Array.from(pendingWrites.values());
|
|
6229
|
-
pendingWrites.clear();
|
|
6230
|
-
await Promise.all(all);
|
|
6231
|
-
}
|
|
6232
|
-
}
|
|
6233
|
-
function clearSession(agentId) {
|
|
6234
|
-
sessions.delete(agentId);
|
|
6235
|
-
}
|
|
6236
|
-
function invalidateSessionCache(cwd, agentId) {
|
|
6237
|
-
sessions.delete(agentId);
|
|
6238
|
-
hydratedKeys.delete(transcriptKey(cwd, agentId));
|
|
6239
|
-
}
|
|
6240
|
-
function enqueueSessionWrite(cwd, agentId, fn) {
|
|
6241
|
-
const key = transcriptKey(cwd, agentId);
|
|
6242
|
-
const prior = pendingWrites.get(key) ?? Promise.resolve();
|
|
6243
|
-
const result = prior.then(fn);
|
|
6244
|
-
pendingWrites.set(
|
|
6245
|
-
key,
|
|
6246
|
-
result.then(
|
|
6247
|
-
() => void 0,
|
|
6248
|
-
() => void 0
|
|
6249
|
-
)
|
|
6250
|
-
);
|
|
6251
|
-
return result;
|
|
6252
|
-
}
|
|
6253
|
-
function clearAllSessions() {
|
|
6254
|
-
sessions.clear();
|
|
6255
|
-
hydratedKeys.clear();
|
|
6256
|
-
recordCounts.clear();
|
|
6257
|
-
const g = globalThis;
|
|
6258
|
-
const sym = /* @__PURE__ */ Symbol.for("theokit-sdk.session.auto-compact-attempts");
|
|
6259
|
-
g[sym]?.clear();
|
|
6260
|
-
}
|
|
6261
|
-
var sessions, hydratedKeys, pendingWrites, recordCounts;
|
|
6262
|
-
var init_agent_session = __esm({
|
|
6263
|
-
"src/internal/session/agent-session.ts"() {
|
|
6264
|
-
init_agent_session_store();
|
|
6265
|
-
sessions = /* @__PURE__ */ new Map();
|
|
6266
|
-
hydratedKeys = /* @__PURE__ */ new Set();
|
|
6267
|
-
pendingWrites = /* @__PURE__ */ new Map();
|
|
6268
|
-
recordCounts = /* @__PURE__ */ new Map();
|
|
6269
|
-
}
|
|
6270
|
-
});
|
|
6271
6146
|
async function withToolWhitelist(whitelist, fn) {
|
|
6272
6147
|
return toolWhitelistStore.run(whitelist, fn);
|
|
6273
6148
|
}
|
|
@@ -10643,6 +10518,10 @@ async function writeSessionSummary(input) {
|
|
|
10643
10518
|
await replaceFileAtomic(path, body);
|
|
10644
10519
|
}
|
|
10645
10520
|
|
|
10521
|
+
// src/internal/runtime/lifecycle/post-run-lifecycle.ts
|
|
10522
|
+
init_catalog_loader();
|
|
10523
|
+
init_compact_session();
|
|
10524
|
+
|
|
10646
10525
|
// src/internal/runtime/memory/memory-path-selector.ts
|
|
10647
10526
|
var PORT_MEMORY_PATH_ENV_VAR = "THEOKIT_PORT_MEMORY_PATH";
|
|
10648
10527
|
function shouldUsePortMemoryPath() {
|
|
@@ -10690,9 +10569,7 @@ async function runPostRunLifecycle(inputs) {
|
|
|
10690
10569
|
appendSessionMessage(agentId, { role: "assistant", text: result.result });
|
|
10691
10570
|
}
|
|
10692
10571
|
const conversation = await safeConversation(run);
|
|
10693
|
-
const
|
|
10694
|
-
const { buildDefaultSummarizer: buildDefaultSummarizer2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
10695
|
-
const contextWindow = getCatalogModelInfo2(model)?.limit?.context;
|
|
10572
|
+
const contextWindow = getCatalogModelInfo(model)?.limit?.context;
|
|
10696
10573
|
if (contextWindow === void 0) {
|
|
10697
10574
|
const g = globalThis;
|
|
10698
10575
|
const sym = /* @__PURE__ */ Symbol.for("theokit-sdk.compact.no-cw-warned");
|
|
@@ -10717,7 +10594,7 @@ async function runPostRunLifecycle(inputs) {
|
|
|
10717
10594
|
autoCompact: {
|
|
10718
10595
|
usageTotal: usageForTrigger,
|
|
10719
10596
|
contextWindow,
|
|
10720
|
-
summarize:
|
|
10597
|
+
summarize: buildDefaultSummarizer({
|
|
10721
10598
|
agentModel: model,
|
|
10722
10599
|
...inputs.apiKey !== void 0 ? { apiKey: inputs.apiKey } : {}
|
|
10723
10600
|
})
|
|
@@ -19697,6 +19574,7 @@ async function getRegisteredAgentOrThrow(agentId) {
|
|
|
19697
19574
|
// src/agent.ts
|
|
19698
19575
|
init_errors();
|
|
19699
19576
|
init_discovery();
|
|
19577
|
+
init_agent_session();
|
|
19700
19578
|
init_agent_factory_registry();
|
|
19701
19579
|
var streamObjectImport;
|
|
19702
19580
|
var Agent = class _Agent {
|
|
@@ -19999,8 +19877,7 @@ var Agent = class _Agent {
|
|
|
19999
19877
|
const { defaultBaseDir: defaultBaseDir2, expandTilde: expandTilde2 } = await Promise.resolve().then(() => (init_session_transcript(), session_transcript_exports));
|
|
20000
19878
|
const baseDir = reg.options.local?.baseDir !== void 0 ? expandTilde2(reg.options.local.baseDir) : defaultBaseDir2();
|
|
20001
19879
|
const store = new FsSessionStore2({ baseDir, cwd });
|
|
20002
|
-
|
|
20003
|
-
return enqueueSessionWrite2(cwd, agentId, () => compactSessionTranscript2({
|
|
19880
|
+
return enqueueSessionWrite(cwd, agentId, () => compactSessionTranscript2({
|
|
20004
19881
|
store,
|
|
20005
19882
|
loc: { cwd, agentId, model },
|
|
20006
19883
|
sessionId: agentId,
|