@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/index.cjs
CHANGED
|
@@ -1760,212 +1760,6 @@ var init_session_summary_writer = __esm({
|
|
|
1760
1760
|
MAX_TURN_CHARS = 2e3;
|
|
1761
1761
|
}
|
|
1762
1762
|
});
|
|
1763
|
-
|
|
1764
|
-
// src/internal/session/agent-session-store.ts
|
|
1765
|
-
function seedTranscript(prior, opts) {
|
|
1766
|
-
return SessionTranscript.fromRecords(prior, opts);
|
|
1767
|
-
}
|
|
1768
|
-
function mapAgentTurn(steps) {
|
|
1769
|
-
const assistant = {};
|
|
1770
|
-
const toolResults = [];
|
|
1771
|
-
const toolCalls = [];
|
|
1772
|
-
for (const step of steps) {
|
|
1773
|
-
if (step.type === "thinkingMessage") assistant.thinking = step.message.text;
|
|
1774
|
-
else if (step.type === "assistantMessage") assistant.text = step.message.text;
|
|
1775
|
-
else if (step.type === "toolCall")
|
|
1776
|
-
toolCalls.push({
|
|
1777
|
-
id: step.message.callId,
|
|
1778
|
-
name: step.message.name,
|
|
1779
|
-
input: step.message.args ?? {}
|
|
1780
|
-
});
|
|
1781
|
-
else
|
|
1782
|
-
toolResults.push({
|
|
1783
|
-
toolUseId: step.message.callId,
|
|
1784
|
-
content: step.message.result,
|
|
1785
|
-
isError: step.message.isError
|
|
1786
|
-
});
|
|
1787
|
-
}
|
|
1788
|
-
if (toolCalls.length > 0) assistant.toolCalls = toolCalls;
|
|
1789
|
-
return { assistant, toolResults };
|
|
1790
|
-
}
|
|
1791
|
-
function hasAssistantContent(a) {
|
|
1792
|
-
return a.text !== void 0 || a.thinking !== void 0 || (a.toolCalls?.length ?? 0) > 0;
|
|
1793
|
-
}
|
|
1794
|
-
function appendConversation(transcript, conversation) {
|
|
1795
|
-
for (const ct of conversation) {
|
|
1796
|
-
if (ct.type !== "agentConversationTurn") continue;
|
|
1797
|
-
const { assistant, toolResults } = mapAgentTurn(ct.turn.steps);
|
|
1798
|
-
if (hasAssistantContent(assistant)) transcript.appendAssistantTurn(assistant);
|
|
1799
|
-
if (toolResults.length > 0) transcript.appendToolResults(toolResults);
|
|
1800
|
-
}
|
|
1801
|
-
}
|
|
1802
|
-
async function readSessionMessages(store, agentId) {
|
|
1803
|
-
const records = await store.readRecords(agentId);
|
|
1804
|
-
return reconstructMessages(records).map(narrowToSessionMessage);
|
|
1805
|
-
}
|
|
1806
|
-
function partToText(p) {
|
|
1807
|
-
if (p.type === "text") return p.text ?? "";
|
|
1808
|
-
if (p.type === "tool_use") return `[tool call] ${p.name ?? ""}`;
|
|
1809
|
-
if (p.type === "tool_result") {
|
|
1810
|
-
const body = typeof p.content === "string" ? p.content : JSON.stringify(p.content);
|
|
1811
|
-
return `[tool result] ${body}`;
|
|
1812
|
-
}
|
|
1813
|
-
return "";
|
|
1814
|
-
}
|
|
1815
|
-
function narrowToSessionMessage(m) {
|
|
1816
|
-
const role = m.role === "user" ? "user" : "assistant";
|
|
1817
|
-
const text = m.content.map(partToText).filter((s) => s.length > 0).join("\n");
|
|
1818
|
-
return { role, text };
|
|
1819
|
-
}
|
|
1820
|
-
function deltaRecords(transcript, priorLength) {
|
|
1821
|
-
return transcript.records().slice(priorLength);
|
|
1822
|
-
}
|
|
1823
|
-
async function persistTurn(store, loc, sessionId, turn) {
|
|
1824
|
-
const prior = await store.readRecords(loc.agentId);
|
|
1825
|
-
const transcript = seedTranscript(prior, { cwd: loc.cwd, sessionId, model: loc.model });
|
|
1826
|
-
transcript.appendUserTurn(turn.userText);
|
|
1827
|
-
appendConversation(transcript, turn.conversation);
|
|
1828
|
-
await store.appendRecords(loc.agentId, deltaRecords(transcript, prior.length));
|
|
1829
|
-
}
|
|
1830
|
-
var init_agent_session_store = __esm({
|
|
1831
|
-
"src/internal/session/agent-session-store.ts"() {
|
|
1832
|
-
init_session_transcript();
|
|
1833
|
-
}
|
|
1834
|
-
});
|
|
1835
|
-
|
|
1836
|
-
// src/compaction.ts
|
|
1837
|
-
function estimateTokens(text) {
|
|
1838
|
-
return Math.ceil(text.length / 4);
|
|
1839
|
-
}
|
|
1840
|
-
var init_compaction = __esm({
|
|
1841
|
-
"src/compaction.ts"() {
|
|
1842
|
-
}
|
|
1843
|
-
});
|
|
1844
|
-
|
|
1845
|
-
// src/internal/runtime/compression/compression-summarizer.ts
|
|
1846
|
-
var compression_summarizer_exports = {};
|
|
1847
|
-
__export(compression_summarizer_exports, {
|
|
1848
|
-
CompressionFailedError: () => CompressionFailedError,
|
|
1849
|
-
buildCompressionPrompt: () => buildCompressionPrompt,
|
|
1850
|
-
compressConversationWindow: () => compressConversationWindow
|
|
1851
|
-
});
|
|
1852
|
-
function buildCompressionPrompt(messages) {
|
|
1853
|
-
const formatted = messages.map((m) => `[${m.role}]: ${m.content}`).join("\n\n");
|
|
1854
|
-
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.
|
|
1855
|
-
|
|
1856
|
-
--- CONVERSATION TO SUMMARIZE ---
|
|
1857
|
-
${formatted}
|
|
1858
|
-
--- END ---`;
|
|
1859
|
-
}
|
|
1860
|
-
async function compressConversationWindow(opts) {
|
|
1861
|
-
const userPrompt = buildCompressionPrompt(opts.messages);
|
|
1862
|
-
let summary;
|
|
1863
|
-
try {
|
|
1864
|
-
summary = await opts.callLlm(opts.model, COMPRESSION_SYSTEM, userPrompt);
|
|
1865
|
-
} catch (cause) {
|
|
1866
|
-
throw new CompressionFailedError(
|
|
1867
|
-
`Compression LLM call failed: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
1868
|
-
{ cause: cause instanceof Error ? cause : void 0 }
|
|
1869
|
-
);
|
|
1870
|
-
}
|
|
1871
|
-
if (!summary || summary.trim().length === 0) {
|
|
1872
|
-
throw new CompressionFailedError(
|
|
1873
|
-
"Compression LLM returned empty summary \u2014 reduction ineffective."
|
|
1874
|
-
);
|
|
1875
|
-
}
|
|
1876
|
-
return {
|
|
1877
|
-
role: "system",
|
|
1878
|
-
content: `[Compressed conversation summary]: ${summary.trim()}`
|
|
1879
|
-
};
|
|
1880
|
-
}
|
|
1881
|
-
var CompressionFailedError, COMPRESSION_SYSTEM;
|
|
1882
|
-
var init_compression_summarizer = __esm({
|
|
1883
|
-
"src/internal/runtime/compression/compression-summarizer.ts"() {
|
|
1884
|
-
CompressionFailedError = class extends Error {
|
|
1885
|
-
name = "CompressionFailedError";
|
|
1886
|
-
};
|
|
1887
|
-
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.";
|
|
1888
|
-
}
|
|
1889
|
-
});
|
|
1890
|
-
|
|
1891
|
-
// src/internal/runtime/compression/compression-model-registry.ts
|
|
1892
|
-
var compression_model_registry_exports = {};
|
|
1893
|
-
__export(compression_model_registry_exports, {
|
|
1894
|
-
CompressionModelUnresolvedError: () => CompressionModelUnresolvedError,
|
|
1895
|
-
resolveCompressionModel: () => resolveCompressionModel
|
|
1896
|
-
});
|
|
1897
|
-
function resolveCompressionModel(agentModel) {
|
|
1898
|
-
const exact = EXACT_REGISTRY.get(agentModel);
|
|
1899
|
-
if (exact !== void 0) return exact;
|
|
1900
|
-
const wildcard = matchWildcard(agentModel);
|
|
1901
|
-
if (wildcard !== null) return wildcard;
|
|
1902
|
-
if (isNoAuthProvider(agentModel)) return agentModel;
|
|
1903
|
-
throw new CompressionModelUnresolvedError(agentModel);
|
|
1904
|
-
}
|
|
1905
|
-
function matchWildcard(agentModel) {
|
|
1906
|
-
for (const [pattern, replacement] of WILDCARD_REGISTRY) {
|
|
1907
|
-
const prefix = pattern.endsWith("*") ? pattern.slice(0, -1) : pattern;
|
|
1908
|
-
if (!agentModel.startsWith(prefix)) continue;
|
|
1909
|
-
const suffix = agentModel.slice(prefix.length);
|
|
1910
|
-
const replPrefix = replacement.endsWith("*") ? replacement.slice(0, -1) : replacement;
|
|
1911
|
-
return `${replPrefix}${suffix}`;
|
|
1912
|
-
}
|
|
1913
|
-
return null;
|
|
1914
|
-
}
|
|
1915
|
-
function isNoAuthProvider(agentModel) {
|
|
1916
|
-
const slashIdx = agentModel.indexOf("/");
|
|
1917
|
-
if (slashIdx <= 0) return false;
|
|
1918
|
-
return NO_AUTH_PROVIDERS.has(agentModel.slice(0, slashIdx));
|
|
1919
|
-
}
|
|
1920
|
-
var EXACT_REGISTRY, WILDCARD_REGISTRY, NO_AUTH_PROVIDERS, CompressionModelUnresolvedError;
|
|
1921
|
-
var init_compression_model_registry = __esm({
|
|
1922
|
-
"src/internal/runtime/compression/compression-model-registry.ts"() {
|
|
1923
|
-
EXACT_REGISTRY = /* @__PURE__ */ new Map([
|
|
1924
|
-
// OpenAI family
|
|
1925
|
-
["openai/gpt-4o", "openai/gpt-4o-mini"],
|
|
1926
|
-
["openai/gpt-4-turbo", "openai/gpt-4o-mini"],
|
|
1927
|
-
["openai/gpt-4", "openai/gpt-4o-mini"],
|
|
1928
|
-
["openai/o1-preview", "openai/gpt-4o-mini"],
|
|
1929
|
-
["openai/o1", "openai/gpt-4o-mini"],
|
|
1930
|
-
["openai/o3", "openai/gpt-4o-mini"],
|
|
1931
|
-
["openai/o3-mini", "openai/gpt-4o-mini"],
|
|
1932
|
-
// Anthropic family
|
|
1933
|
-
["anthropic/claude-opus-4", "anthropic/claude-3-5-haiku-latest"],
|
|
1934
|
-
["anthropic/claude-sonnet-4", "anthropic/claude-3-5-haiku-latest"],
|
|
1935
|
-
["anthropic/claude-3-5-sonnet", "anthropic/claude-3-5-haiku-latest"],
|
|
1936
|
-
["anthropic/claude-3-5-sonnet-latest", "anthropic/claude-3-5-haiku-latest"],
|
|
1937
|
-
["anthropic/claude-3-opus", "anthropic/claude-3-haiku"],
|
|
1938
|
-
["anthropic/claude-3-sonnet", "anthropic/claude-3-haiku"],
|
|
1939
|
-
// Vertex (Gemini + Anthropic-on-Vertex)
|
|
1940
|
-
["vertex/gemini-1.5-pro", "vertex/gemini-1.5-flash"],
|
|
1941
|
-
["vertex/gemini-2.0-pro", "vertex/gemini-1.5-flash"],
|
|
1942
|
-
["vertex/claude-3-5-sonnet", "vertex/claude-3-5-haiku"],
|
|
1943
|
-
["vertex/claude-3-opus", "vertex/claude-3-haiku"],
|
|
1944
|
-
// OpenRouter (preserve openrouter prefix, swap tier within same vendor)
|
|
1945
|
-
["openrouter/openai/gpt-4o", "openrouter/openai/gpt-4o-mini"],
|
|
1946
|
-
["openrouter/openai/gpt-4-turbo", "openrouter/openai/gpt-4o-mini"],
|
|
1947
|
-
["openrouter/anthropic/claude-3-5-sonnet", "openrouter/anthropic/claude-3-5-haiku"],
|
|
1948
|
-
["openrouter/anthropic/claude-opus-4", "openrouter/anthropic/claude-3-5-haiku"]
|
|
1949
|
-
]);
|
|
1950
|
-
WILDCARD_REGISTRY = [
|
|
1951
|
-
// Bedrock Anthropic: us.anthropic.claude-sonnet-* → us.anthropic.claude-3-haiku-*
|
|
1952
|
-
["bedrock/anthropic.claude-sonnet*", "bedrock/anthropic.claude-3-haiku*"],
|
|
1953
|
-
["bedrock/anthropic.claude-opus*", "bedrock/anthropic.claude-3-haiku*"],
|
|
1954
|
-
["bedrock/anthropic.claude-3-5-sonnet*", "bedrock/anthropic.claude-3-5-haiku*"]
|
|
1955
|
-
];
|
|
1956
|
-
NO_AUTH_PROVIDERS = /* @__PURE__ */ new Set(["ollama", "lmstudio", "llamacpp"]);
|
|
1957
|
-
CompressionModelUnresolvedError = class extends Error {
|
|
1958
|
-
name = "CompressionModelUnresolvedError";
|
|
1959
|
-
agentModel;
|
|
1960
|
-
constructor(agentModel) {
|
|
1961
|
-
super(
|
|
1962
|
-
`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).`
|
|
1963
|
-
);
|
|
1964
|
-
this.agentModel = agentModel;
|
|
1965
|
-
}
|
|
1966
|
-
};
|
|
1967
|
-
}
|
|
1968
|
-
});
|
|
1969
1763
|
var MODALITIES, costSchema, limitSchema, modalitiesSchema, catalogModelSchema;
|
|
1970
1764
|
var init_catalog_schema = __esm({
|
|
1971
1765
|
"src/internal/providers/catalog-schema.ts"() {
|
|
@@ -2036,10 +1830,6 @@ function getProviderProfile(name) {
|
|
|
2036
1830
|
function listProviders() {
|
|
2037
1831
|
return Array.from(REGISTRY.values());
|
|
2038
1832
|
}
|
|
2039
|
-
function _resetProvidersForTests() {
|
|
2040
|
-
REGISTRY.clear();
|
|
2041
|
-
ALIASES.clear();
|
|
2042
|
-
}
|
|
2043
1833
|
var REGISTRY, ALIASES;
|
|
2044
1834
|
var init_registry = __esm({
|
|
2045
1835
|
"src/internal/providers/registry.ts"() {
|
|
@@ -2050,19 +1840,6 @@ var init_registry = __esm({
|
|
|
2050
1840
|
ALIASES = globalSingleton("theokit-sdk.providers.aliases", () => /* @__PURE__ */ new Map());
|
|
2051
1841
|
}
|
|
2052
1842
|
});
|
|
2053
|
-
|
|
2054
|
-
// src/internal/providers/catalog-loader.ts
|
|
2055
|
-
var catalog_loader_exports = {};
|
|
2056
|
-
__export(catalog_loader_exports, {
|
|
2057
|
-
_resetModelInfoIndexForTests: () => _resetModelInfoIndexForTests,
|
|
2058
|
-
getCatalogCapabilities: () => getCatalogCapabilities,
|
|
2059
|
-
getCatalogModelInfo: () => getCatalogModelInfo,
|
|
2060
|
-
isPatchedModelKey: () => isPatchedModelKey,
|
|
2061
|
-
listModelInfoKeys: () => listModelInfoKeys,
|
|
2062
|
-
loadProviderCatalog: () => loadProviderCatalog,
|
|
2063
|
-
patchModelInfo: () => patchModelInfo,
|
|
2064
|
-
registerCatalogProviders: () => registerCatalogProviders
|
|
2065
|
-
});
|
|
2066
1843
|
function globalSingleton2(key2, create) {
|
|
2067
1844
|
const g = globalThis;
|
|
2068
1845
|
const sym = Symbol.for(key2);
|
|
@@ -2076,16 +1853,6 @@ function getCatalogModelInfo(key2) {
|
|
|
2076
1853
|
function isPatchedModelKey(key2) {
|
|
2077
1854
|
return patchedModelKeys.has(key2);
|
|
2078
1855
|
}
|
|
2079
|
-
function patchModelInfo(key2, model) {
|
|
2080
|
-
ensureModelIndexLoaded();
|
|
2081
|
-
const existing = modelInfoIndex.get(key2);
|
|
2082
|
-
modelInfoIndex.set(key2, existing === void 0 ? model : { ...existing, ...model });
|
|
2083
|
-
patchedModelKeys.add(key2);
|
|
2084
|
-
}
|
|
2085
|
-
function listModelInfoKeys() {
|
|
2086
|
-
ensureModelIndexLoaded();
|
|
2087
|
-
return [...modelInfoIndex.keys()];
|
|
2088
|
-
}
|
|
2089
1856
|
function ensureModelIndexLoaded() {
|
|
2090
1857
|
if (indexState.loaded) return;
|
|
2091
1858
|
indexState.loaded = true;
|
|
@@ -2119,11 +1886,6 @@ function indexEntryModels(entry) {
|
|
|
2119
1886
|
}
|
|
2120
1887
|
}
|
|
2121
1888
|
}
|
|
2122
|
-
function _resetModelInfoIndexForTests() {
|
|
2123
|
-
modelInfoIndex.clear();
|
|
2124
|
-
patchedModelKeys.clear();
|
|
2125
|
-
indexState.loaded = false;
|
|
2126
|
-
}
|
|
2127
1889
|
function validateEntry(raw) {
|
|
2128
1890
|
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") {
|
|
2129
1891
|
return null;
|
|
@@ -2134,12 +1896,6 @@ function loadProviderCatalog(opts) {
|
|
|
2134
1896
|
const catalogPath = path.join(__dirname_resolved, "provider-catalog.json");
|
|
2135
1897
|
const rawText = fs.readFileSync(catalogPath, "utf-8");
|
|
2136
1898
|
let entries = JSON.parse(rawText);
|
|
2137
|
-
if (opts?._testInjectMalformed) {
|
|
2138
|
-
entries = [
|
|
2139
|
-
...entries,
|
|
2140
|
-
{ id: "malformed-provider", displayName: "Bad" }
|
|
2141
|
-
];
|
|
2142
|
-
}
|
|
2143
1899
|
const result = {};
|
|
2144
1900
|
for (const raw of entries) {
|
|
2145
1901
|
const validated = validateEntry(raw);
|
|
@@ -2168,7 +1924,7 @@ function getCatalogCapabilities(providerId) {
|
|
|
2168
1924
|
return _capabilitiesCache[providerId];
|
|
2169
1925
|
}
|
|
2170
1926
|
function registerCatalogProviders(opts) {
|
|
2171
|
-
const catalog = loadProviderCatalog(
|
|
1927
|
+
const catalog = loadProviderCatalog();
|
|
2172
1928
|
for (const entry of Object.values(catalog)) {
|
|
2173
1929
|
if (getProviderProfile(entry.id) !== void 0) continue;
|
|
2174
1930
|
if (entry.aliases?.some((a) => getProviderProfile(a) !== void 0)) continue;
|
|
@@ -2209,6 +1965,15 @@ var init_catalog_loader = __esm({
|
|
|
2209
1965
|
}
|
|
2210
1966
|
});
|
|
2211
1967
|
|
|
1968
|
+
// src/compaction.ts
|
|
1969
|
+
function estimateTokens(text) {
|
|
1970
|
+
return Math.ceil(text.length / 4);
|
|
1971
|
+
}
|
|
1972
|
+
var init_compaction = __esm({
|
|
1973
|
+
"src/compaction.ts"() {
|
|
1974
|
+
}
|
|
1975
|
+
});
|
|
1976
|
+
|
|
2212
1977
|
// src/internal/providers/builtin/anthropic.ts
|
|
2213
1978
|
var ANTHROPIC;
|
|
2214
1979
|
var init_anthropic = __esm({
|
|
@@ -2972,9 +2737,6 @@ function registerBuiltins() {
|
|
|
2972
2737
|
registerProvider(CEREBRAS);
|
|
2973
2738
|
registerCatalogProviders();
|
|
2974
2739
|
}
|
|
2975
|
-
function _resetBuiltinsRegistered() {
|
|
2976
|
-
_registeredState.done = false;
|
|
2977
|
-
}
|
|
2978
2740
|
var _registeredState;
|
|
2979
2741
|
var init_builtin = __esm({
|
|
2980
2742
|
"src/internal/providers/builtin/index.ts"() {
|
|
@@ -3101,9 +2863,6 @@ async function loadOne(dir, entryName) {
|
|
|
3101
2863
|
}
|
|
3102
2864
|
}
|
|
3103
2865
|
}
|
|
3104
|
-
function _resetDiscovery() {
|
|
3105
|
-
discoveryState.done = false;
|
|
3106
|
-
}
|
|
3107
2866
|
var discoveryState;
|
|
3108
2867
|
var init_discovery = __esm({
|
|
3109
2868
|
"src/internal/providers/discovery.ts"() {
|
|
@@ -3115,21 +2874,6 @@ var init_discovery = __esm({
|
|
|
3115
2874
|
});
|
|
3116
2875
|
|
|
3117
2876
|
// src/internal/providers/index.ts
|
|
3118
|
-
var providers_exports = {};
|
|
3119
|
-
__export(providers_exports, {
|
|
3120
|
-
ANTHROPIC: () => ANTHROPIC,
|
|
3121
|
-
GEMINI: () => GEMINI,
|
|
3122
|
-
OPENAI: () => OPENAI,
|
|
3123
|
-
OPENROUTER: () => OPENROUTER,
|
|
3124
|
-
_resetBuiltinsRegistered: () => _resetBuiltinsRegistered,
|
|
3125
|
-
_resetDiscovery: () => _resetDiscovery,
|
|
3126
|
-
_resetProvidersForTests: () => _resetProvidersForTests,
|
|
3127
|
-
discoverProviderPlugins: () => discoverProviderPlugins,
|
|
3128
|
-
getProviderProfile: () => getProviderProfile,
|
|
3129
|
-
listProviders: () => listProviders,
|
|
3130
|
-
registerBuiltins: () => registerBuiltins,
|
|
3131
|
-
registerProvider: () => registerProvider
|
|
3132
|
-
});
|
|
3133
2877
|
var init_providers = __esm({
|
|
3134
2878
|
"src/internal/providers/index.ts"() {
|
|
3135
2879
|
init_builtin();
|
|
@@ -5874,12 +5618,6 @@ var init_vertex_router = __esm({
|
|
|
5874
5618
|
});
|
|
5875
5619
|
|
|
5876
5620
|
// src/internal/llm/router.ts
|
|
5877
|
-
var router_exports = {};
|
|
5878
|
-
__export(router_exports, {
|
|
5879
|
-
_resetCredentialPoolWarnings: () => _resetCredentialPoolWarnings,
|
|
5880
|
-
_resetNoAuthApiKeyWarnings: () => _resetNoAuthApiKeyWarnings,
|
|
5881
|
-
resolveProviderChain: () => resolveProviderChain
|
|
5882
|
-
});
|
|
5883
5621
|
function resolveProviderChain(options) {
|
|
5884
5622
|
registerBuiltins();
|
|
5885
5623
|
return buildChain(options);
|
|
@@ -5977,9 +5715,6 @@ function warnNoAuthApiKeysIgnoredOnce(provider) {
|
|
|
5977
5715
|
`
|
|
5978
5716
|
);
|
|
5979
5717
|
}
|
|
5980
|
-
function _resetNoAuthApiKeyWarnings() {
|
|
5981
|
-
warnedNoAuthApiKeys.clear();
|
|
5982
|
-
}
|
|
5983
5718
|
function sentinelForNoAuth(profile) {
|
|
5984
5719
|
return profile.authType === "none" ? profile.name : void 0;
|
|
5985
5720
|
}
|
|
@@ -6010,9 +5745,6 @@ function warnUnknownProvidersInApiKeys(apiKeys) {
|
|
|
6010
5745
|
}
|
|
6011
5746
|
}
|
|
6012
5747
|
}
|
|
6013
|
-
function _resetCredentialPoolWarnings() {
|
|
6014
|
-
warnedProviders.clear();
|
|
6015
|
-
}
|
|
6016
5748
|
function resolveApiKey2(envVars) {
|
|
6017
5749
|
for (const v of envVars) {
|
|
6018
5750
|
const value = process.env[v];
|
|
@@ -6096,61 +5828,338 @@ function selectTransport(profile, apiKey) {
|
|
|
6096
5828
|
{ code: "transport_unavailable" }
|
|
6097
5829
|
);
|
|
6098
5830
|
}
|
|
6099
|
-
var warnedNoAuthApiKeys, warnedProviders;
|
|
6100
|
-
var init_router = __esm({
|
|
6101
|
-
"src/internal/llm/router.ts"() {
|
|
6102
|
-
init_errors();
|
|
6103
|
-
init_providers();
|
|
6104
|
-
init_anthropic3();
|
|
6105
|
-
init_bedrock_anthropic();
|
|
6106
|
-
init_credential_pool();
|
|
6107
|
-
init_credential_pool_context();
|
|
6108
|
-
init_fault_injection();
|
|
6109
|
-
init_ollama_native();
|
|
6110
|
-
init_openai2();
|
|
6111
|
-
init_pool_aware_client();
|
|
6112
|
-
init_responses();
|
|
6113
|
-
init_vertex_router();
|
|
6114
|
-
warnedNoAuthApiKeys = /* @__PURE__ */ new Set();
|
|
6115
|
-
warnedProviders = /* @__PURE__ */ new Set();
|
|
5831
|
+
var warnedNoAuthApiKeys, warnedProviders;
|
|
5832
|
+
var init_router = __esm({
|
|
5833
|
+
"src/internal/llm/router.ts"() {
|
|
5834
|
+
init_errors();
|
|
5835
|
+
init_providers();
|
|
5836
|
+
init_anthropic3();
|
|
5837
|
+
init_bedrock_anthropic();
|
|
5838
|
+
init_credential_pool();
|
|
5839
|
+
init_credential_pool_context();
|
|
5840
|
+
init_fault_injection();
|
|
5841
|
+
init_ollama_native();
|
|
5842
|
+
init_openai2();
|
|
5843
|
+
init_pool_aware_client();
|
|
5844
|
+
init_responses();
|
|
5845
|
+
init_vertex_router();
|
|
5846
|
+
warnedNoAuthApiKeys = /* @__PURE__ */ new Set();
|
|
5847
|
+
warnedProviders = /* @__PURE__ */ new Set();
|
|
5848
|
+
}
|
|
5849
|
+
});
|
|
5850
|
+
|
|
5851
|
+
// src/internal/local-agent/real-local-run-provider.ts
|
|
5852
|
+
function inferProviderFromApiKey(apiKey) {
|
|
5853
|
+
if (apiKey === void 0 || apiKey.length === 0) return void 0;
|
|
5854
|
+
const byPrefix = [
|
|
5855
|
+
{ provider: "openrouter", prefix: "sk-or-" },
|
|
5856
|
+
{ provider: "anthropic", prefix: "sk-ant-" },
|
|
5857
|
+
{ provider: "openai", prefix: "sk-" }
|
|
5858
|
+
];
|
|
5859
|
+
for (const { provider, prefix } of byPrefix) {
|
|
5860
|
+
if (apiKey.startsWith(prefix) && getProviderProfile(provider) !== void 0) {
|
|
5861
|
+
return provider;
|
|
5862
|
+
}
|
|
5863
|
+
}
|
|
5864
|
+
return void 0;
|
|
5865
|
+
}
|
|
5866
|
+
function detectPrimaryProvider() {
|
|
5867
|
+
if (process.env.ANTHROPIC_API_KEY !== void 0 && process.env.ANTHROPIC_API_KEY.length > 0) {
|
|
5868
|
+
return "anthropic";
|
|
5869
|
+
}
|
|
5870
|
+
if (process.env.OPENAI_API_KEY !== void 0 && process.env.OPENAI_API_KEY.length > 0) {
|
|
5871
|
+
return "openai";
|
|
5872
|
+
}
|
|
5873
|
+
if (process.env.OPENROUTER_API_KEY !== void 0 && process.env.OPENROUTER_API_KEY.length > 0) {
|
|
5874
|
+
return "openrouter";
|
|
5875
|
+
}
|
|
5876
|
+
return "openai";
|
|
5877
|
+
}
|
|
5878
|
+
var init_real_local_run_provider = __esm({
|
|
5879
|
+
"src/internal/local-agent/real-local-run-provider.ts"() {
|
|
5880
|
+
init_providers();
|
|
5881
|
+
}
|
|
5882
|
+
});
|
|
5883
|
+
|
|
5884
|
+
// src/internal/runtime/compression/compression-model-registry.ts
|
|
5885
|
+
function resolveCompressionModel(agentModel) {
|
|
5886
|
+
const exact = EXACT_REGISTRY.get(agentModel);
|
|
5887
|
+
if (exact !== void 0) return exact;
|
|
5888
|
+
const wildcard = matchWildcard(agentModel);
|
|
5889
|
+
if (wildcard !== null) return wildcard;
|
|
5890
|
+
if (isNoAuthProvider(agentModel)) return agentModel;
|
|
5891
|
+
throw new CompressionModelUnresolvedError(agentModel);
|
|
5892
|
+
}
|
|
5893
|
+
function matchWildcard(agentModel) {
|
|
5894
|
+
for (const [pattern, replacement] of WILDCARD_REGISTRY) {
|
|
5895
|
+
const prefix = pattern.endsWith("*") ? pattern.slice(0, -1) : pattern;
|
|
5896
|
+
if (!agentModel.startsWith(prefix)) continue;
|
|
5897
|
+
const suffix = agentModel.slice(prefix.length);
|
|
5898
|
+
const replPrefix = replacement.endsWith("*") ? replacement.slice(0, -1) : replacement;
|
|
5899
|
+
return `${replPrefix}${suffix}`;
|
|
5900
|
+
}
|
|
5901
|
+
return null;
|
|
5902
|
+
}
|
|
5903
|
+
function isNoAuthProvider(agentModel) {
|
|
5904
|
+
const slashIdx = agentModel.indexOf("/");
|
|
5905
|
+
if (slashIdx <= 0) return false;
|
|
5906
|
+
return NO_AUTH_PROVIDERS.has(agentModel.slice(0, slashIdx));
|
|
5907
|
+
}
|
|
5908
|
+
var EXACT_REGISTRY, WILDCARD_REGISTRY, NO_AUTH_PROVIDERS, CompressionModelUnresolvedError;
|
|
5909
|
+
var init_compression_model_registry = __esm({
|
|
5910
|
+
"src/internal/runtime/compression/compression-model-registry.ts"() {
|
|
5911
|
+
EXACT_REGISTRY = /* @__PURE__ */ new Map([
|
|
5912
|
+
// OpenAI family
|
|
5913
|
+
["openai/gpt-4o", "openai/gpt-4o-mini"],
|
|
5914
|
+
["openai/gpt-4-turbo", "openai/gpt-4o-mini"],
|
|
5915
|
+
["openai/gpt-4", "openai/gpt-4o-mini"],
|
|
5916
|
+
["openai/o1-preview", "openai/gpt-4o-mini"],
|
|
5917
|
+
["openai/o1", "openai/gpt-4o-mini"],
|
|
5918
|
+
["openai/o3", "openai/gpt-4o-mini"],
|
|
5919
|
+
["openai/o3-mini", "openai/gpt-4o-mini"],
|
|
5920
|
+
// Anthropic family
|
|
5921
|
+
["anthropic/claude-opus-4", "anthropic/claude-3-5-haiku-latest"],
|
|
5922
|
+
["anthropic/claude-sonnet-4", "anthropic/claude-3-5-haiku-latest"],
|
|
5923
|
+
["anthropic/claude-3-5-sonnet", "anthropic/claude-3-5-haiku-latest"],
|
|
5924
|
+
["anthropic/claude-3-5-sonnet-latest", "anthropic/claude-3-5-haiku-latest"],
|
|
5925
|
+
["anthropic/claude-3-opus", "anthropic/claude-3-haiku"],
|
|
5926
|
+
["anthropic/claude-3-sonnet", "anthropic/claude-3-haiku"],
|
|
5927
|
+
// Vertex (Gemini + Anthropic-on-Vertex)
|
|
5928
|
+
["vertex/gemini-1.5-pro", "vertex/gemini-1.5-flash"],
|
|
5929
|
+
["vertex/gemini-2.0-pro", "vertex/gemini-1.5-flash"],
|
|
5930
|
+
["vertex/claude-3-5-sonnet", "vertex/claude-3-5-haiku"],
|
|
5931
|
+
["vertex/claude-3-opus", "vertex/claude-3-haiku"],
|
|
5932
|
+
// OpenRouter (preserve openrouter prefix, swap tier within same vendor)
|
|
5933
|
+
["openrouter/openai/gpt-4o", "openrouter/openai/gpt-4o-mini"],
|
|
5934
|
+
["openrouter/openai/gpt-4-turbo", "openrouter/openai/gpt-4o-mini"],
|
|
5935
|
+
["openrouter/anthropic/claude-3-5-sonnet", "openrouter/anthropic/claude-3-5-haiku"],
|
|
5936
|
+
["openrouter/anthropic/claude-opus-4", "openrouter/anthropic/claude-3-5-haiku"]
|
|
5937
|
+
]);
|
|
5938
|
+
WILDCARD_REGISTRY = [
|
|
5939
|
+
// Bedrock Anthropic: us.anthropic.claude-sonnet-* → us.anthropic.claude-3-haiku-*
|
|
5940
|
+
["bedrock/anthropic.claude-sonnet*", "bedrock/anthropic.claude-3-haiku*"],
|
|
5941
|
+
["bedrock/anthropic.claude-opus*", "bedrock/anthropic.claude-3-haiku*"],
|
|
5942
|
+
["bedrock/anthropic.claude-3-5-sonnet*", "bedrock/anthropic.claude-3-5-haiku*"]
|
|
5943
|
+
];
|
|
5944
|
+
NO_AUTH_PROVIDERS = /* @__PURE__ */ new Set(["ollama", "lmstudio", "llamacpp"]);
|
|
5945
|
+
CompressionModelUnresolvedError = class extends Error {
|
|
5946
|
+
name = "CompressionModelUnresolvedError";
|
|
5947
|
+
agentModel;
|
|
5948
|
+
constructor(agentModel) {
|
|
5949
|
+
super(
|
|
5950
|
+
`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).`
|
|
5951
|
+
);
|
|
5952
|
+
this.agentModel = agentModel;
|
|
5953
|
+
}
|
|
5954
|
+
};
|
|
5955
|
+
}
|
|
5956
|
+
});
|
|
5957
|
+
|
|
5958
|
+
// src/internal/runtime/compression/compression-summarizer.ts
|
|
5959
|
+
function buildCompressionPrompt(messages) {
|
|
5960
|
+
const formatted = messages.map((m) => `[${m.role}]: ${m.content}`).join("\n\n");
|
|
5961
|
+
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.
|
|
5962
|
+
|
|
5963
|
+
--- CONVERSATION TO SUMMARIZE ---
|
|
5964
|
+
${formatted}
|
|
5965
|
+
--- END ---`;
|
|
5966
|
+
}
|
|
5967
|
+
async function compressConversationWindow(opts) {
|
|
5968
|
+
const userPrompt = buildCompressionPrompt(opts.messages);
|
|
5969
|
+
let summary;
|
|
5970
|
+
try {
|
|
5971
|
+
summary = await opts.callLlm(opts.model, COMPRESSION_SYSTEM, userPrompt);
|
|
5972
|
+
} catch (cause) {
|
|
5973
|
+
throw new CompressionFailedError(
|
|
5974
|
+
`Compression LLM call failed: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
5975
|
+
{ cause: cause instanceof Error ? cause : void 0 }
|
|
5976
|
+
);
|
|
5977
|
+
}
|
|
5978
|
+
if (!summary || summary.trim().length === 0) {
|
|
5979
|
+
throw new CompressionFailedError(
|
|
5980
|
+
"Compression LLM returned empty summary \u2014 reduction ineffective."
|
|
5981
|
+
);
|
|
5982
|
+
}
|
|
5983
|
+
return {
|
|
5984
|
+
role: "system",
|
|
5985
|
+
content: `[Compressed conversation summary]: ${summary.trim()}`
|
|
5986
|
+
};
|
|
5987
|
+
}
|
|
5988
|
+
var CompressionFailedError, COMPRESSION_SYSTEM;
|
|
5989
|
+
var init_compression_summarizer = __esm({
|
|
5990
|
+
"src/internal/runtime/compression/compression-summarizer.ts"() {
|
|
5991
|
+
CompressionFailedError = class extends Error {
|
|
5992
|
+
name = "CompressionFailedError";
|
|
5993
|
+
};
|
|
5994
|
+
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.";
|
|
5995
|
+
}
|
|
5996
|
+
});
|
|
5997
|
+
|
|
5998
|
+
// src/internal/session/agent-session-store.ts
|
|
5999
|
+
function seedTranscript(prior, opts) {
|
|
6000
|
+
return SessionTranscript.fromRecords(prior, opts);
|
|
6001
|
+
}
|
|
6002
|
+
function mapAgentTurn(steps) {
|
|
6003
|
+
const assistant = {};
|
|
6004
|
+
const toolResults = [];
|
|
6005
|
+
const toolCalls = [];
|
|
6006
|
+
for (const step of steps) {
|
|
6007
|
+
if (step.type === "thinkingMessage") assistant.thinking = step.message.text;
|
|
6008
|
+
else if (step.type === "assistantMessage") assistant.text = step.message.text;
|
|
6009
|
+
else if (step.type === "toolCall")
|
|
6010
|
+
toolCalls.push({
|
|
6011
|
+
id: step.message.callId,
|
|
6012
|
+
name: step.message.name,
|
|
6013
|
+
input: step.message.args ?? {}
|
|
6014
|
+
});
|
|
6015
|
+
else
|
|
6016
|
+
toolResults.push({
|
|
6017
|
+
toolUseId: step.message.callId,
|
|
6018
|
+
content: step.message.result,
|
|
6019
|
+
isError: step.message.isError
|
|
6020
|
+
});
|
|
6021
|
+
}
|
|
6022
|
+
if (toolCalls.length > 0) assistant.toolCalls = toolCalls;
|
|
6023
|
+
return { assistant, toolResults };
|
|
6024
|
+
}
|
|
6025
|
+
function hasAssistantContent(a) {
|
|
6026
|
+
return a.text !== void 0 || a.thinking !== void 0 || (a.toolCalls?.length ?? 0) > 0;
|
|
6027
|
+
}
|
|
6028
|
+
function appendConversation(transcript, conversation) {
|
|
6029
|
+
for (const ct of conversation) {
|
|
6030
|
+
if (ct.type !== "agentConversationTurn") continue;
|
|
6031
|
+
const { assistant, toolResults } = mapAgentTurn(ct.turn.steps);
|
|
6032
|
+
if (hasAssistantContent(assistant)) transcript.appendAssistantTurn(assistant);
|
|
6033
|
+
if (toolResults.length > 0) transcript.appendToolResults(toolResults);
|
|
6034
|
+
}
|
|
6035
|
+
}
|
|
6036
|
+
async function readSessionMessages(store, agentId) {
|
|
6037
|
+
const records = await store.readRecords(agentId);
|
|
6038
|
+
return reconstructMessages(records).map(narrowToSessionMessage);
|
|
6039
|
+
}
|
|
6040
|
+
function partToText(p) {
|
|
6041
|
+
if (p.type === "text") return p.text ?? "";
|
|
6042
|
+
if (p.type === "tool_use") return `[tool call] ${p.name ?? ""}`;
|
|
6043
|
+
if (p.type === "tool_result") {
|
|
6044
|
+
const body = typeof p.content === "string" ? p.content : JSON.stringify(p.content);
|
|
6045
|
+
return `[tool result] ${body}`;
|
|
6046
|
+
}
|
|
6047
|
+
return "";
|
|
6048
|
+
}
|
|
6049
|
+
function narrowToSessionMessage(m) {
|
|
6050
|
+
const role = m.role === "user" ? "user" : "assistant";
|
|
6051
|
+
const text = m.content.map(partToText).filter((s) => s.length > 0).join("\n");
|
|
6052
|
+
return { role, text };
|
|
6053
|
+
}
|
|
6054
|
+
function deltaRecords(transcript, priorLength) {
|
|
6055
|
+
return transcript.records().slice(priorLength);
|
|
6056
|
+
}
|
|
6057
|
+
async function persistTurn(store, loc, sessionId, turn) {
|
|
6058
|
+
const prior = await store.readRecords(loc.agentId);
|
|
6059
|
+
const transcript = seedTranscript(prior, { cwd: loc.cwd, sessionId, model: loc.model });
|
|
6060
|
+
transcript.appendUserTurn(turn.userText);
|
|
6061
|
+
appendConversation(transcript, turn.conversation);
|
|
6062
|
+
await store.appendRecords(loc.agentId, deltaRecords(transcript, prior.length));
|
|
6063
|
+
}
|
|
6064
|
+
var init_agent_session_store = __esm({
|
|
6065
|
+
"src/internal/session/agent-session-store.ts"() {
|
|
6066
|
+
init_session_transcript();
|
|
6116
6067
|
}
|
|
6117
6068
|
});
|
|
6118
6069
|
|
|
6119
|
-
// src/internal/
|
|
6120
|
-
|
|
6121
|
-
|
|
6122
|
-
|
|
6123
|
-
|
|
6124
|
-
|
|
6125
|
-
|
|
6126
|
-
|
|
6127
|
-
|
|
6128
|
-
|
|
6129
|
-
|
|
6130
|
-
|
|
6131
|
-
|
|
6132
|
-
|
|
6133
|
-
|
|
6134
|
-
|
|
6070
|
+
// src/internal/session/agent-session.ts
|
|
6071
|
+
function transcriptKey(cwd, agentId) {
|
|
6072
|
+
return `${cwd}::${agentId}`;
|
|
6073
|
+
}
|
|
6074
|
+
function appendSessionMessage(agentId, message) {
|
|
6075
|
+
const existing = sessions.get(agentId) ?? [];
|
|
6076
|
+
existing.push(message);
|
|
6077
|
+
sessions.set(agentId, existing);
|
|
6078
|
+
}
|
|
6079
|
+
function getSessionMessages(agentId) {
|
|
6080
|
+
return sessions.get(agentId) ?? [];
|
|
6081
|
+
}
|
|
6082
|
+
function persistTurnToTranscript(store, loc, sessionId, turn, onCompact) {
|
|
6083
|
+
const key2 = transcriptKey(loc.cwd, loc.agentId);
|
|
6084
|
+
const chained = (pendingWrites.get(key2) ?? Promise.resolve()).then(async () => {
|
|
6085
|
+
try {
|
|
6086
|
+
await persistTurn(store, loc, sessionId, turn);
|
|
6087
|
+
const count = (recordCounts.get(key2) ?? 0) + 1;
|
|
6088
|
+
recordCounts.set(key2, count);
|
|
6089
|
+
if (turn.autoCompact !== void 0) {
|
|
6090
|
+
const { autoCompactIfNeeded: autoCompactIfNeeded2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
6091
|
+
const fired = await autoCompactIfNeeded2({
|
|
6092
|
+
store,
|
|
6093
|
+
loc,
|
|
6094
|
+
sessionId,
|
|
6095
|
+
usageTotal: turn.autoCompact.usageTotal,
|
|
6096
|
+
contextWindow: turn.autoCompact.contextWindow,
|
|
6097
|
+
turnCount: count,
|
|
6098
|
+
summarize: turn.autoCompact.summarize
|
|
6099
|
+
});
|
|
6100
|
+
if (fired) onCompact?.();
|
|
6101
|
+
}
|
|
6102
|
+
} catch (cause) {
|
|
6103
|
+
const msg = cause instanceof Error ? cause.message : String(cause);
|
|
6104
|
+
process.stderr.write(
|
|
6105
|
+
`[theokit-sdk] session transcript write failed (${loc.agentId}): ${msg}
|
|
6106
|
+
`
|
|
6107
|
+
);
|
|
6135
6108
|
}
|
|
6136
|
-
}
|
|
6137
|
-
|
|
6109
|
+
});
|
|
6110
|
+
pendingWrites.set(
|
|
6111
|
+
key2,
|
|
6112
|
+
chained.then(
|
|
6113
|
+
() => void 0,
|
|
6114
|
+
() => void 0
|
|
6115
|
+
)
|
|
6116
|
+
);
|
|
6138
6117
|
}
|
|
6139
|
-
function
|
|
6140
|
-
|
|
6141
|
-
|
|
6142
|
-
|
|
6143
|
-
|
|
6144
|
-
|
|
6118
|
+
async function hydrateSession(agentId, loc) {
|
|
6119
|
+
const key2 = transcriptKey(loc.cwd, agentId);
|
|
6120
|
+
if (hydratedKeys.has(key2)) return;
|
|
6121
|
+
hydratedKeys.add(key2);
|
|
6122
|
+
const persisted = await readSessionMessages(loc.store, agentId);
|
|
6123
|
+
if (persisted.length === 0) return;
|
|
6124
|
+
if (!sessions.has(agentId) || sessions.get(agentId)?.length === 0) {
|
|
6125
|
+
sessions.set(agentId, persisted);
|
|
6145
6126
|
}
|
|
6146
|
-
|
|
6147
|
-
|
|
6127
|
+
}
|
|
6128
|
+
async function flushSessionWrites() {
|
|
6129
|
+
while (pendingWrites.size > 0) {
|
|
6130
|
+
const all = Array.from(pendingWrites.values());
|
|
6131
|
+
pendingWrites.clear();
|
|
6132
|
+
await Promise.all(all);
|
|
6148
6133
|
}
|
|
6149
|
-
return "openai";
|
|
6150
6134
|
}
|
|
6151
|
-
|
|
6152
|
-
|
|
6153
|
-
|
|
6135
|
+
function clearSession(agentId) {
|
|
6136
|
+
sessions.delete(agentId);
|
|
6137
|
+
}
|
|
6138
|
+
function invalidateSessionCache(cwd, agentId) {
|
|
6139
|
+
sessions.delete(agentId);
|
|
6140
|
+
hydratedKeys.delete(transcriptKey(cwd, agentId));
|
|
6141
|
+
}
|
|
6142
|
+
function enqueueSessionWrite(cwd, agentId, fn) {
|
|
6143
|
+
const key2 = transcriptKey(cwd, agentId);
|
|
6144
|
+
const prior = pendingWrites.get(key2) ?? Promise.resolve();
|
|
6145
|
+
const result = prior.then(fn);
|
|
6146
|
+
pendingWrites.set(
|
|
6147
|
+
key2,
|
|
6148
|
+
result.then(
|
|
6149
|
+
() => void 0,
|
|
6150
|
+
() => void 0
|
|
6151
|
+
)
|
|
6152
|
+
);
|
|
6153
|
+
return result;
|
|
6154
|
+
}
|
|
6155
|
+
var sessions, hydratedKeys, pendingWrites, recordCounts;
|
|
6156
|
+
var init_agent_session = __esm({
|
|
6157
|
+
"src/internal/session/agent-session.ts"() {
|
|
6158
|
+
init_agent_session_store();
|
|
6159
|
+
sessions = /* @__PURE__ */ new Map();
|
|
6160
|
+
hydratedKeys = /* @__PURE__ */ new Set();
|
|
6161
|
+
pendingWrites = /* @__PURE__ */ new Map();
|
|
6162
|
+
recordCounts = /* @__PURE__ */ new Map();
|
|
6154
6163
|
}
|
|
6155
6164
|
});
|
|
6156
6165
|
|
|
@@ -6163,6 +6172,7 @@ __export(compact_session_exports, {
|
|
|
6163
6172
|
buildDefaultSummarizer: () => buildDefaultSummarizer,
|
|
6164
6173
|
compactSessionTranscript: () => compactSessionTranscript,
|
|
6165
6174
|
isCompactSummary: () => isCompactSummary,
|
|
6175
|
+
resolveSummarizerRoute: () => resolveSummarizerRoute,
|
|
6166
6176
|
shouldAutoCompact: () => shouldAutoCompact
|
|
6167
6177
|
});
|
|
6168
6178
|
function isCompactSummary(content) {
|
|
@@ -6215,35 +6225,37 @@ ${summaryBody}`;
|
|
|
6215
6225
|
transcript.appendUserTurn(summary);
|
|
6216
6226
|
const delta = transcript.records().slice(prior.length);
|
|
6217
6227
|
await opts.store.appendRecords(opts.loc.agentId, delta);
|
|
6218
|
-
|
|
6219
|
-
invalidateSessionCache2(opts.loc.cwd, opts.loc.agentId);
|
|
6228
|
+
invalidateSessionCache(opts.loc.cwd, opts.loc.agentId);
|
|
6220
6229
|
const postTokens = estimateTokens([...preserved, summary].join("\n"));
|
|
6221
6230
|
return { preTokens, postTokens };
|
|
6222
6231
|
}
|
|
6232
|
+
function resolveSummarizerRoute(opts) {
|
|
6233
|
+
const provider = opts.keyProvider ?? (opts.prefixHasProfile && opts.modelPrefix !== void 0 ? opts.modelPrefix : opts.envProvider);
|
|
6234
|
+
return { provider, fullSlug: provider !== opts.modelPrefix };
|
|
6235
|
+
}
|
|
6223
6236
|
function buildDefaultSummarizer(opts) {
|
|
6224
6237
|
return async (messages) => {
|
|
6225
|
-
|
|
6226
|
-
const { resolveCompressionModel: resolveCompressionModel2 } = await Promise.resolve().then(() => (init_compression_model_registry(), compression_model_registry_exports));
|
|
6227
|
-
const { resolveProviderChain: resolveProviderChain2 } = await Promise.resolve().then(() => (init_router(), router_exports));
|
|
6228
|
-
const { inferProviderFromApiKey: inferProviderFromApiKey2, detectPrimaryProvider: detectPrimaryProvider2 } = await Promise.resolve().then(() => (init_real_local_run_provider(), real_local_run_provider_exports));
|
|
6229
|
-
const keyProvider = inferProviderFromApiKey2(opts.apiKey);
|
|
6238
|
+
registerBuiltins();
|
|
6230
6239
|
const modelPrefix = opts.agentModel.includes("/") ? opts.agentModel.slice(0, opts.agentModel.indexOf("/")) : void 0;
|
|
6231
|
-
const
|
|
6232
|
-
|
|
6233
|
-
|
|
6234
|
-
|
|
6240
|
+
const route = resolveSummarizerRoute({
|
|
6241
|
+
keyProvider: inferProviderFromApiKey(opts.apiKey),
|
|
6242
|
+
modelPrefix,
|
|
6243
|
+
prefixHasProfile: modelPrefix !== void 0 && getProviderProfile(modelPrefix) !== void 0,
|
|
6244
|
+
envProvider: detectPrimaryProvider()
|
|
6245
|
+
});
|
|
6246
|
+
const provider = route.provider;
|
|
6235
6247
|
let model;
|
|
6236
|
-
if (
|
|
6248
|
+
if (route.fullSlug) {
|
|
6237
6249
|
model = opts.agentModel;
|
|
6238
6250
|
} else {
|
|
6239
6251
|
try {
|
|
6240
|
-
model =
|
|
6252
|
+
model = resolveCompressionModel(opts.agentModel);
|
|
6241
6253
|
} catch {
|
|
6242
6254
|
model = opts.agentModel;
|
|
6243
6255
|
}
|
|
6244
6256
|
}
|
|
6245
6257
|
const callLlm = async (m, system, user) => {
|
|
6246
|
-
const chain =
|
|
6258
|
+
const chain = resolveProviderChain({
|
|
6247
6259
|
primary: provider,
|
|
6248
6260
|
...opts.apiKey !== void 0 ? { apiKeys: { [provider]: [opts.apiKey] } } : {}
|
|
6249
6261
|
});
|
|
@@ -6263,7 +6275,7 @@ function buildDefaultSummarizer(opts) {
|
|
|
6263
6275
|
}
|
|
6264
6276
|
return text;
|
|
6265
6277
|
};
|
|
6266
|
-
const summary = await
|
|
6278
|
+
const summary = await compressConversationWindow({ messages: [...messages], model, callLlm });
|
|
6267
6279
|
return summary.content;
|
|
6268
6280
|
};
|
|
6269
6281
|
}
|
|
@@ -6297,6 +6309,12 @@ var COMPACT_SUMMARY_MARKER, COMPACT_USER_MESSAGE_MAX_TOKENS, autoCompactAttempts
|
|
|
6297
6309
|
var init_compact_session = __esm({
|
|
6298
6310
|
"src/internal/session/compact-session.ts"() {
|
|
6299
6311
|
init_compaction();
|
|
6312
|
+
init_router();
|
|
6313
|
+
init_real_local_run_provider();
|
|
6314
|
+
init_providers();
|
|
6315
|
+
init_compression_model_registry();
|
|
6316
|
+
init_compression_summarizer();
|
|
6317
|
+
init_agent_session();
|
|
6300
6318
|
init_session_transcript();
|
|
6301
6319
|
COMPACT_SUMMARY_MARKER = "[[theokit:compact-summary]]";
|
|
6302
6320
|
COMPACT_USER_MESSAGE_MAX_TOKENS = 2e4;
|
|
@@ -6308,122 +6326,6 @@ var init_compact_session = __esm({
|
|
|
6308
6326
|
})();
|
|
6309
6327
|
}
|
|
6310
6328
|
});
|
|
6311
|
-
|
|
6312
|
-
// src/internal/session/agent-session.ts
|
|
6313
|
-
var agent_session_exports = {};
|
|
6314
|
-
__export(agent_session_exports, {
|
|
6315
|
-
appendSessionMessage: () => appendSessionMessage,
|
|
6316
|
-
clearAllSessions: () => clearAllSessions,
|
|
6317
|
-
clearSession: () => clearSession,
|
|
6318
|
-
enqueueSessionWrite: () => enqueueSessionWrite,
|
|
6319
|
-
flushSessionWrites: () => flushSessionWrites,
|
|
6320
|
-
getSessionMessages: () => getSessionMessages,
|
|
6321
|
-
hydrateSession: () => hydrateSession,
|
|
6322
|
-
invalidateSessionCache: () => invalidateSessionCache,
|
|
6323
|
-
persistTurnToTranscript: () => persistTurnToTranscript
|
|
6324
|
-
});
|
|
6325
|
-
function transcriptKey(cwd, agentId) {
|
|
6326
|
-
return `${cwd}::${agentId}`;
|
|
6327
|
-
}
|
|
6328
|
-
function appendSessionMessage(agentId, message) {
|
|
6329
|
-
const existing = sessions.get(agentId) ?? [];
|
|
6330
|
-
existing.push(message);
|
|
6331
|
-
sessions.set(agentId, existing);
|
|
6332
|
-
}
|
|
6333
|
-
function getSessionMessages(agentId) {
|
|
6334
|
-
return sessions.get(agentId) ?? [];
|
|
6335
|
-
}
|
|
6336
|
-
function persistTurnToTranscript(store, loc, sessionId, turn, onCompact) {
|
|
6337
|
-
const key2 = transcriptKey(loc.cwd, loc.agentId);
|
|
6338
|
-
const chained = (pendingWrites.get(key2) ?? Promise.resolve()).then(async () => {
|
|
6339
|
-
try {
|
|
6340
|
-
await persistTurn(store, loc, sessionId, turn);
|
|
6341
|
-
const count = (recordCounts.get(key2) ?? 0) + 1;
|
|
6342
|
-
recordCounts.set(key2, count);
|
|
6343
|
-
if (turn.autoCompact !== void 0) {
|
|
6344
|
-
const { autoCompactIfNeeded: autoCompactIfNeeded2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
6345
|
-
const fired = await autoCompactIfNeeded2({
|
|
6346
|
-
store,
|
|
6347
|
-
loc,
|
|
6348
|
-
sessionId,
|
|
6349
|
-
usageTotal: turn.autoCompact.usageTotal,
|
|
6350
|
-
contextWindow: turn.autoCompact.contextWindow,
|
|
6351
|
-
turnCount: count,
|
|
6352
|
-
summarize: turn.autoCompact.summarize
|
|
6353
|
-
});
|
|
6354
|
-
if (fired) onCompact?.();
|
|
6355
|
-
}
|
|
6356
|
-
} catch (cause) {
|
|
6357
|
-
const msg = cause instanceof Error ? cause.message : String(cause);
|
|
6358
|
-
process.stderr.write(
|
|
6359
|
-
`[theokit-sdk] session transcript write failed (${loc.agentId}): ${msg}
|
|
6360
|
-
`
|
|
6361
|
-
);
|
|
6362
|
-
}
|
|
6363
|
-
});
|
|
6364
|
-
pendingWrites.set(
|
|
6365
|
-
key2,
|
|
6366
|
-
chained.then(
|
|
6367
|
-
() => void 0,
|
|
6368
|
-
() => void 0
|
|
6369
|
-
)
|
|
6370
|
-
);
|
|
6371
|
-
}
|
|
6372
|
-
async function hydrateSession(agentId, loc) {
|
|
6373
|
-
const key2 = transcriptKey(loc.cwd, agentId);
|
|
6374
|
-
if (hydratedKeys.has(key2)) return;
|
|
6375
|
-
hydratedKeys.add(key2);
|
|
6376
|
-
const persisted = await readSessionMessages(loc.store, agentId);
|
|
6377
|
-
if (persisted.length === 0) return;
|
|
6378
|
-
if (!sessions.has(agentId) || sessions.get(agentId)?.length === 0) {
|
|
6379
|
-
sessions.set(agentId, persisted);
|
|
6380
|
-
}
|
|
6381
|
-
}
|
|
6382
|
-
async function flushSessionWrites() {
|
|
6383
|
-
while (pendingWrites.size > 0) {
|
|
6384
|
-
const all = Array.from(pendingWrites.values());
|
|
6385
|
-
pendingWrites.clear();
|
|
6386
|
-
await Promise.all(all);
|
|
6387
|
-
}
|
|
6388
|
-
}
|
|
6389
|
-
function clearSession(agentId) {
|
|
6390
|
-
sessions.delete(agentId);
|
|
6391
|
-
}
|
|
6392
|
-
function invalidateSessionCache(cwd, agentId) {
|
|
6393
|
-
sessions.delete(agentId);
|
|
6394
|
-
hydratedKeys.delete(transcriptKey(cwd, agentId));
|
|
6395
|
-
}
|
|
6396
|
-
function enqueueSessionWrite(cwd, agentId, fn) {
|
|
6397
|
-
const key2 = transcriptKey(cwd, agentId);
|
|
6398
|
-
const prior = pendingWrites.get(key2) ?? Promise.resolve();
|
|
6399
|
-
const result = prior.then(fn);
|
|
6400
|
-
pendingWrites.set(
|
|
6401
|
-
key2,
|
|
6402
|
-
result.then(
|
|
6403
|
-
() => void 0,
|
|
6404
|
-
() => void 0
|
|
6405
|
-
)
|
|
6406
|
-
);
|
|
6407
|
-
return result;
|
|
6408
|
-
}
|
|
6409
|
-
function clearAllSessions() {
|
|
6410
|
-
sessions.clear();
|
|
6411
|
-
hydratedKeys.clear();
|
|
6412
|
-
recordCounts.clear();
|
|
6413
|
-
const g = globalThis;
|
|
6414
|
-
const sym = /* @__PURE__ */ Symbol.for("theokit-sdk.session.auto-compact-attempts");
|
|
6415
|
-
g[sym]?.clear();
|
|
6416
|
-
}
|
|
6417
|
-
var sessions, hydratedKeys, pendingWrites, recordCounts;
|
|
6418
|
-
var init_agent_session = __esm({
|
|
6419
|
-
"src/internal/session/agent-session.ts"() {
|
|
6420
|
-
init_agent_session_store();
|
|
6421
|
-
sessions = /* @__PURE__ */ new Map();
|
|
6422
|
-
hydratedKeys = /* @__PURE__ */ new Set();
|
|
6423
|
-
pendingWrites = /* @__PURE__ */ new Map();
|
|
6424
|
-
recordCounts = /* @__PURE__ */ new Map();
|
|
6425
|
-
}
|
|
6426
|
-
});
|
|
6427
6329
|
async function withToolWhitelist(whitelist, fn) {
|
|
6428
6330
|
return toolWhitelistStore.run(whitelist, fn);
|
|
6429
6331
|
}
|
|
@@ -13243,6 +13145,8 @@ function parseDecisionFromStdout(stdout) {
|
|
|
13243
13145
|
// src/internal/runtime/lifecycle/post-run-lifecycle.ts
|
|
13244
13146
|
init_run_events();
|
|
13245
13147
|
init_session_summary_writer();
|
|
13148
|
+
init_catalog_loader();
|
|
13149
|
+
init_compact_session();
|
|
13246
13150
|
|
|
13247
13151
|
// src/internal/runtime/memory/memory-path-selector.ts
|
|
13248
13152
|
var PORT_MEMORY_PATH_ENV_VAR = "THEOKIT_PORT_MEMORY_PATH";
|
|
@@ -13291,9 +13195,7 @@ async function runPostRunLifecycle(inputs) {
|
|
|
13291
13195
|
appendSessionMessage(agentId, { role: "assistant", text: result.result });
|
|
13292
13196
|
}
|
|
13293
13197
|
const conversation = await safeConversation(run);
|
|
13294
|
-
const
|
|
13295
|
-
const { buildDefaultSummarizer: buildDefaultSummarizer2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
13296
|
-
const contextWindow = getCatalogModelInfo2(model)?.limit?.context;
|
|
13198
|
+
const contextWindow = getCatalogModelInfo(model)?.limit?.context;
|
|
13297
13199
|
if (contextWindow === void 0) {
|
|
13298
13200
|
const g = globalThis;
|
|
13299
13201
|
const sym = /* @__PURE__ */ Symbol.for("theokit-sdk.compact.no-cw-warned");
|
|
@@ -13318,7 +13220,7 @@ async function runPostRunLifecycle(inputs) {
|
|
|
13318
13220
|
autoCompact: {
|
|
13319
13221
|
usageTotal: usageForTrigger,
|
|
13320
13222
|
contextWindow,
|
|
13321
|
-
summarize:
|
|
13223
|
+
summarize: buildDefaultSummarizer({
|
|
13322
13224
|
agentModel: model,
|
|
13323
13225
|
...inputs.apiKey !== void 0 ? { apiKey: inputs.apiKey } : {}
|
|
13324
13226
|
})
|
|
@@ -21313,6 +21215,7 @@ async function getRegisteredAgentOrThrow(agentId) {
|
|
|
21313
21215
|
// src/agent.ts
|
|
21314
21216
|
init_errors();
|
|
21315
21217
|
init_discovery();
|
|
21218
|
+
init_agent_session();
|
|
21316
21219
|
init_agent_factory_registry();
|
|
21317
21220
|
var streamObjectImport;
|
|
21318
21221
|
var Agent = class _Agent {
|
|
@@ -21615,8 +21518,7 @@ var Agent = class _Agent {
|
|
|
21615
21518
|
const { defaultBaseDir: defaultBaseDir2, expandTilde: expandTilde2 } = await Promise.resolve().then(() => (init_session_transcript(), session_transcript_exports));
|
|
21616
21519
|
const baseDir = reg.options.local?.baseDir !== void 0 ? expandTilde2(reg.options.local.baseDir) : defaultBaseDir2();
|
|
21617
21520
|
const store = new FsSessionStore2({ baseDir, cwd });
|
|
21618
|
-
|
|
21619
|
-
return enqueueSessionWrite2(cwd, agentId, () => compactSessionTranscript2({
|
|
21521
|
+
return enqueueSessionWrite(cwd, agentId, () => compactSessionTranscript2({
|
|
21620
21522
|
store,
|
|
21621
21523
|
loc: { cwd, agentId, model },
|
|
21622
21524
|
sessionId: agentId,
|