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