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