@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/eval.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { z, toJSONSchema } from 'zod';
|
|
2
2
|
import { createRequire } from 'module';
|
|
3
3
|
import { randomUUID, randomBytes, createHash } from 'crypto';
|
|
4
|
-
import { mkdir, readFile, open, rename, unlink,
|
|
4
|
+
import { mkdir, readFile, open, rename, unlink, writeFile, statfs, readdir, stat, access } from 'fs/promises';
|
|
5
5
|
import { join, dirname, resolve, sep, relative, isAbsolute } from 'path';
|
|
6
|
-
import { readFileSync, existsSync, mkdirSync, appendFileSync,
|
|
6
|
+
import { readFileSync, existsSync, mkdirSync, appendFileSync, realpathSync, lstatSync, readlinkSync, readdirSync, statSync, chmodSync, openSync, writeFileSync, fsyncSync, closeSync, renameSync, unlinkSync } from 'fs';
|
|
7
7
|
import { homedir } from 'os';
|
|
8
8
|
import { fileURLToPath, pathToFileURL } from 'url';
|
|
9
9
|
import { AsyncLocalStorage } from 'async_hooks';
|
|
@@ -1601,212 +1601,6 @@ var init_run_events = __esm({
|
|
|
1601
1601
|
"src/types/run-events.ts"() {
|
|
1602
1602
|
}
|
|
1603
1603
|
});
|
|
1604
|
-
|
|
1605
|
-
// src/internal/session/agent-session-store.ts
|
|
1606
|
-
function seedTranscript(prior, opts) {
|
|
1607
|
-
return SessionTranscript.fromRecords(prior, opts);
|
|
1608
|
-
}
|
|
1609
|
-
function mapAgentTurn(steps) {
|
|
1610
|
-
const assistant = {};
|
|
1611
|
-
const toolResults = [];
|
|
1612
|
-
const toolCalls = [];
|
|
1613
|
-
for (const step of steps) {
|
|
1614
|
-
if (step.type === "thinkingMessage") assistant.thinking = step.message.text;
|
|
1615
|
-
else if (step.type === "assistantMessage") assistant.text = step.message.text;
|
|
1616
|
-
else if (step.type === "toolCall")
|
|
1617
|
-
toolCalls.push({
|
|
1618
|
-
id: step.message.callId,
|
|
1619
|
-
name: step.message.name,
|
|
1620
|
-
input: step.message.args ?? {}
|
|
1621
|
-
});
|
|
1622
|
-
else
|
|
1623
|
-
toolResults.push({
|
|
1624
|
-
toolUseId: step.message.callId,
|
|
1625
|
-
content: step.message.result,
|
|
1626
|
-
isError: step.message.isError
|
|
1627
|
-
});
|
|
1628
|
-
}
|
|
1629
|
-
if (toolCalls.length > 0) assistant.toolCalls = toolCalls;
|
|
1630
|
-
return { assistant, toolResults };
|
|
1631
|
-
}
|
|
1632
|
-
function hasAssistantContent(a) {
|
|
1633
|
-
return a.text !== void 0 || a.thinking !== void 0 || (a.toolCalls?.length ?? 0) > 0;
|
|
1634
|
-
}
|
|
1635
|
-
function appendConversation(transcript, conversation) {
|
|
1636
|
-
for (const ct of conversation) {
|
|
1637
|
-
if (ct.type !== "agentConversationTurn") continue;
|
|
1638
|
-
const { assistant, toolResults } = mapAgentTurn(ct.turn.steps);
|
|
1639
|
-
if (hasAssistantContent(assistant)) transcript.appendAssistantTurn(assistant);
|
|
1640
|
-
if (toolResults.length > 0) transcript.appendToolResults(toolResults);
|
|
1641
|
-
}
|
|
1642
|
-
}
|
|
1643
|
-
async function readSessionMessages(store, agentId) {
|
|
1644
|
-
const records = await store.readRecords(agentId);
|
|
1645
|
-
return reconstructMessages(records).map(narrowToSessionMessage);
|
|
1646
|
-
}
|
|
1647
|
-
function partToText(p) {
|
|
1648
|
-
if (p.type === "text") return p.text ?? "";
|
|
1649
|
-
if (p.type === "tool_use") return `[tool call] ${p.name ?? ""}`;
|
|
1650
|
-
if (p.type === "tool_result") {
|
|
1651
|
-
const body = typeof p.content === "string" ? p.content : JSON.stringify(p.content);
|
|
1652
|
-
return `[tool result] ${body}`;
|
|
1653
|
-
}
|
|
1654
|
-
return "";
|
|
1655
|
-
}
|
|
1656
|
-
function narrowToSessionMessage(m) {
|
|
1657
|
-
const role = m.role === "user" ? "user" : "assistant";
|
|
1658
|
-
const text = m.content.map(partToText).filter((s) => s.length > 0).join("\n");
|
|
1659
|
-
return { role, text };
|
|
1660
|
-
}
|
|
1661
|
-
function deltaRecords(transcript, priorLength) {
|
|
1662
|
-
return transcript.records().slice(priorLength);
|
|
1663
|
-
}
|
|
1664
|
-
async function persistTurn(store, loc, sessionId, turn) {
|
|
1665
|
-
const prior = await store.readRecords(loc.agentId);
|
|
1666
|
-
const transcript = seedTranscript(prior, { cwd: loc.cwd, sessionId, model: loc.model });
|
|
1667
|
-
transcript.appendUserTurn(turn.userText);
|
|
1668
|
-
appendConversation(transcript, turn.conversation);
|
|
1669
|
-
await store.appendRecords(loc.agentId, deltaRecords(transcript, prior.length));
|
|
1670
|
-
}
|
|
1671
|
-
var init_agent_session_store = __esm({
|
|
1672
|
-
"src/internal/session/agent-session-store.ts"() {
|
|
1673
|
-
init_session_transcript();
|
|
1674
|
-
}
|
|
1675
|
-
});
|
|
1676
|
-
|
|
1677
|
-
// src/compaction.ts
|
|
1678
|
-
function estimateTokens(text) {
|
|
1679
|
-
return Math.ceil(text.length / 4);
|
|
1680
|
-
}
|
|
1681
|
-
var init_compaction = __esm({
|
|
1682
|
-
"src/compaction.ts"() {
|
|
1683
|
-
}
|
|
1684
|
-
});
|
|
1685
|
-
|
|
1686
|
-
// src/internal/runtime/compression/compression-summarizer.ts
|
|
1687
|
-
var compression_summarizer_exports = {};
|
|
1688
|
-
__export(compression_summarizer_exports, {
|
|
1689
|
-
CompressionFailedError: () => CompressionFailedError,
|
|
1690
|
-
buildCompressionPrompt: () => buildCompressionPrompt,
|
|
1691
|
-
compressConversationWindow: () => compressConversationWindow
|
|
1692
|
-
});
|
|
1693
|
-
function buildCompressionPrompt(messages) {
|
|
1694
|
-
const formatted = messages.map((m) => `[${m.role}]: ${m.content}`).join("\n\n");
|
|
1695
|
-
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.
|
|
1696
|
-
|
|
1697
|
-
--- CONVERSATION TO SUMMARIZE ---
|
|
1698
|
-
${formatted}
|
|
1699
|
-
--- END ---`;
|
|
1700
|
-
}
|
|
1701
|
-
async function compressConversationWindow(opts) {
|
|
1702
|
-
const userPrompt = buildCompressionPrompt(opts.messages);
|
|
1703
|
-
let summary;
|
|
1704
|
-
try {
|
|
1705
|
-
summary = await opts.callLlm(opts.model, COMPRESSION_SYSTEM, userPrompt);
|
|
1706
|
-
} catch (cause) {
|
|
1707
|
-
throw new CompressionFailedError(
|
|
1708
|
-
`Compression LLM call failed: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
1709
|
-
{ cause: cause instanceof Error ? cause : void 0 }
|
|
1710
|
-
);
|
|
1711
|
-
}
|
|
1712
|
-
if (!summary || summary.trim().length === 0) {
|
|
1713
|
-
throw new CompressionFailedError(
|
|
1714
|
-
"Compression LLM returned empty summary \u2014 reduction ineffective."
|
|
1715
|
-
);
|
|
1716
|
-
}
|
|
1717
|
-
return {
|
|
1718
|
-
role: "system",
|
|
1719
|
-
content: `[Compressed conversation summary]: ${summary.trim()}`
|
|
1720
|
-
};
|
|
1721
|
-
}
|
|
1722
|
-
var CompressionFailedError, COMPRESSION_SYSTEM;
|
|
1723
|
-
var init_compression_summarizer = __esm({
|
|
1724
|
-
"src/internal/runtime/compression/compression-summarizer.ts"() {
|
|
1725
|
-
CompressionFailedError = class extends Error {
|
|
1726
|
-
name = "CompressionFailedError";
|
|
1727
|
-
};
|
|
1728
|
-
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.";
|
|
1729
|
-
}
|
|
1730
|
-
});
|
|
1731
|
-
|
|
1732
|
-
// src/internal/runtime/compression/compression-model-registry.ts
|
|
1733
|
-
var compression_model_registry_exports = {};
|
|
1734
|
-
__export(compression_model_registry_exports, {
|
|
1735
|
-
CompressionModelUnresolvedError: () => CompressionModelUnresolvedError,
|
|
1736
|
-
resolveCompressionModel: () => resolveCompressionModel
|
|
1737
|
-
});
|
|
1738
|
-
function resolveCompressionModel(agentModel) {
|
|
1739
|
-
const exact = EXACT_REGISTRY.get(agentModel);
|
|
1740
|
-
if (exact !== void 0) return exact;
|
|
1741
|
-
const wildcard = matchWildcard(agentModel);
|
|
1742
|
-
if (wildcard !== null) return wildcard;
|
|
1743
|
-
if (isNoAuthProvider(agentModel)) return agentModel;
|
|
1744
|
-
throw new CompressionModelUnresolvedError(agentModel);
|
|
1745
|
-
}
|
|
1746
|
-
function matchWildcard(agentModel) {
|
|
1747
|
-
for (const [pattern, replacement] of WILDCARD_REGISTRY) {
|
|
1748
|
-
const prefix = pattern.endsWith("*") ? pattern.slice(0, -1) : pattern;
|
|
1749
|
-
if (!agentModel.startsWith(prefix)) continue;
|
|
1750
|
-
const suffix = agentModel.slice(prefix.length);
|
|
1751
|
-
const replPrefix = replacement.endsWith("*") ? replacement.slice(0, -1) : replacement;
|
|
1752
|
-
return `${replPrefix}${suffix}`;
|
|
1753
|
-
}
|
|
1754
|
-
return null;
|
|
1755
|
-
}
|
|
1756
|
-
function isNoAuthProvider(agentModel) {
|
|
1757
|
-
const slashIdx = agentModel.indexOf("/");
|
|
1758
|
-
if (slashIdx <= 0) return false;
|
|
1759
|
-
return NO_AUTH_PROVIDERS.has(agentModel.slice(0, slashIdx));
|
|
1760
|
-
}
|
|
1761
|
-
var EXACT_REGISTRY, WILDCARD_REGISTRY, NO_AUTH_PROVIDERS, CompressionModelUnresolvedError;
|
|
1762
|
-
var init_compression_model_registry = __esm({
|
|
1763
|
-
"src/internal/runtime/compression/compression-model-registry.ts"() {
|
|
1764
|
-
EXACT_REGISTRY = /* @__PURE__ */ new Map([
|
|
1765
|
-
// OpenAI family
|
|
1766
|
-
["openai/gpt-4o", "openai/gpt-4o-mini"],
|
|
1767
|
-
["openai/gpt-4-turbo", "openai/gpt-4o-mini"],
|
|
1768
|
-
["openai/gpt-4", "openai/gpt-4o-mini"],
|
|
1769
|
-
["openai/o1-preview", "openai/gpt-4o-mini"],
|
|
1770
|
-
["openai/o1", "openai/gpt-4o-mini"],
|
|
1771
|
-
["openai/o3", "openai/gpt-4o-mini"],
|
|
1772
|
-
["openai/o3-mini", "openai/gpt-4o-mini"],
|
|
1773
|
-
// Anthropic family
|
|
1774
|
-
["anthropic/claude-opus-4", "anthropic/claude-3-5-haiku-latest"],
|
|
1775
|
-
["anthropic/claude-sonnet-4", "anthropic/claude-3-5-haiku-latest"],
|
|
1776
|
-
["anthropic/claude-3-5-sonnet", "anthropic/claude-3-5-haiku-latest"],
|
|
1777
|
-
["anthropic/claude-3-5-sonnet-latest", "anthropic/claude-3-5-haiku-latest"],
|
|
1778
|
-
["anthropic/claude-3-opus", "anthropic/claude-3-haiku"],
|
|
1779
|
-
["anthropic/claude-3-sonnet", "anthropic/claude-3-haiku"],
|
|
1780
|
-
// Vertex (Gemini + Anthropic-on-Vertex)
|
|
1781
|
-
["vertex/gemini-1.5-pro", "vertex/gemini-1.5-flash"],
|
|
1782
|
-
["vertex/gemini-2.0-pro", "vertex/gemini-1.5-flash"],
|
|
1783
|
-
["vertex/claude-3-5-sonnet", "vertex/claude-3-5-haiku"],
|
|
1784
|
-
["vertex/claude-3-opus", "vertex/claude-3-haiku"],
|
|
1785
|
-
// OpenRouter (preserve openrouter prefix, swap tier within same vendor)
|
|
1786
|
-
["openrouter/openai/gpt-4o", "openrouter/openai/gpt-4o-mini"],
|
|
1787
|
-
["openrouter/openai/gpt-4-turbo", "openrouter/openai/gpt-4o-mini"],
|
|
1788
|
-
["openrouter/anthropic/claude-3-5-sonnet", "openrouter/anthropic/claude-3-5-haiku"],
|
|
1789
|
-
["openrouter/anthropic/claude-opus-4", "openrouter/anthropic/claude-3-5-haiku"]
|
|
1790
|
-
]);
|
|
1791
|
-
WILDCARD_REGISTRY = [
|
|
1792
|
-
// Bedrock Anthropic: us.anthropic.claude-sonnet-* → us.anthropic.claude-3-haiku-*
|
|
1793
|
-
["bedrock/anthropic.claude-sonnet*", "bedrock/anthropic.claude-3-haiku*"],
|
|
1794
|
-
["bedrock/anthropic.claude-opus*", "bedrock/anthropic.claude-3-haiku*"],
|
|
1795
|
-
["bedrock/anthropic.claude-3-5-sonnet*", "bedrock/anthropic.claude-3-5-haiku*"]
|
|
1796
|
-
];
|
|
1797
|
-
NO_AUTH_PROVIDERS = /* @__PURE__ */ new Set(["ollama", "lmstudio", "llamacpp"]);
|
|
1798
|
-
CompressionModelUnresolvedError = class extends Error {
|
|
1799
|
-
name = "CompressionModelUnresolvedError";
|
|
1800
|
-
agentModel;
|
|
1801
|
-
constructor(agentModel) {
|
|
1802
|
-
super(
|
|
1803
|
-
`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).`
|
|
1804
|
-
);
|
|
1805
|
-
this.agentModel = agentModel;
|
|
1806
|
-
}
|
|
1807
|
-
};
|
|
1808
|
-
}
|
|
1809
|
-
});
|
|
1810
1604
|
var MODALITIES, costSchema, limitSchema, modalitiesSchema, catalogModelSchema;
|
|
1811
1605
|
var init_catalog_schema = __esm({
|
|
1812
1606
|
"src/internal/providers/catalog-schema.ts"() {
|
|
@@ -1874,13 +1668,6 @@ function getProviderProfile(name) {
|
|
|
1874
1668
|
const canonical = ALIASES.get(name) ?? name;
|
|
1875
1669
|
return REGISTRY.get(canonical);
|
|
1876
1670
|
}
|
|
1877
|
-
function listProviders() {
|
|
1878
|
-
return Array.from(REGISTRY.values());
|
|
1879
|
-
}
|
|
1880
|
-
function _resetProvidersForTests() {
|
|
1881
|
-
REGISTRY.clear();
|
|
1882
|
-
ALIASES.clear();
|
|
1883
|
-
}
|
|
1884
1671
|
var REGISTRY, ALIASES;
|
|
1885
1672
|
var init_registry = __esm({
|
|
1886
1673
|
"src/internal/providers/registry.ts"() {
|
|
@@ -1891,19 +1678,6 @@ var init_registry = __esm({
|
|
|
1891
1678
|
ALIASES = globalSingleton("theokit-sdk.providers.aliases", () => /* @__PURE__ */ new Map());
|
|
1892
1679
|
}
|
|
1893
1680
|
});
|
|
1894
|
-
|
|
1895
|
-
// src/internal/providers/catalog-loader.ts
|
|
1896
|
-
var catalog_loader_exports = {};
|
|
1897
|
-
__export(catalog_loader_exports, {
|
|
1898
|
-
_resetModelInfoIndexForTests: () => _resetModelInfoIndexForTests,
|
|
1899
|
-
getCatalogCapabilities: () => getCatalogCapabilities,
|
|
1900
|
-
getCatalogModelInfo: () => getCatalogModelInfo,
|
|
1901
|
-
isPatchedModelKey: () => isPatchedModelKey,
|
|
1902
|
-
listModelInfoKeys: () => listModelInfoKeys,
|
|
1903
|
-
loadProviderCatalog: () => loadProviderCatalog,
|
|
1904
|
-
patchModelInfo: () => patchModelInfo,
|
|
1905
|
-
registerCatalogProviders: () => registerCatalogProviders
|
|
1906
|
-
});
|
|
1907
1681
|
function globalSingleton2(key, create) {
|
|
1908
1682
|
const g = globalThis;
|
|
1909
1683
|
const sym = Symbol.for(key);
|
|
@@ -1917,16 +1691,6 @@ function getCatalogModelInfo(key) {
|
|
|
1917
1691
|
function isPatchedModelKey(key) {
|
|
1918
1692
|
return patchedModelKeys.has(key);
|
|
1919
1693
|
}
|
|
1920
|
-
function patchModelInfo(key, model) {
|
|
1921
|
-
ensureModelIndexLoaded();
|
|
1922
|
-
const existing = modelInfoIndex.get(key);
|
|
1923
|
-
modelInfoIndex.set(key, existing === void 0 ? model : { ...existing, ...model });
|
|
1924
|
-
patchedModelKeys.add(key);
|
|
1925
|
-
}
|
|
1926
|
-
function listModelInfoKeys() {
|
|
1927
|
-
ensureModelIndexLoaded();
|
|
1928
|
-
return [...modelInfoIndex.keys()];
|
|
1929
|
-
}
|
|
1930
1694
|
function ensureModelIndexLoaded() {
|
|
1931
1695
|
if (indexState.loaded) return;
|
|
1932
1696
|
indexState.loaded = true;
|
|
@@ -1960,11 +1724,6 @@ function indexEntryModels(entry) {
|
|
|
1960
1724
|
}
|
|
1961
1725
|
}
|
|
1962
1726
|
}
|
|
1963
|
-
function _resetModelInfoIndexForTests() {
|
|
1964
|
-
modelInfoIndex.clear();
|
|
1965
|
-
patchedModelKeys.clear();
|
|
1966
|
-
indexState.loaded = false;
|
|
1967
|
-
}
|
|
1968
1727
|
function validateEntry(raw) {
|
|
1969
1728
|
if (typeof raw.id !== "string" || typeof raw.displayName !== "string" || typeof raw.apiMode !== "string" || typeof raw.authType !== "string" || typeof raw.baseUrl !== "string" || !Array.isArray(raw.envVars) || !Array.isArray(raw.fallbackModels) || raw.capabilities == null || typeof raw.capabilities !== "object") {
|
|
1970
1729
|
return null;
|
|
@@ -1975,12 +1734,6 @@ function loadProviderCatalog(opts) {
|
|
|
1975
1734
|
const catalogPath = join(__dirname_resolved, "provider-catalog.json");
|
|
1976
1735
|
const rawText = readFileSync(catalogPath, "utf-8");
|
|
1977
1736
|
let entries = JSON.parse(rawText);
|
|
1978
|
-
if (opts?._testInjectMalformed) {
|
|
1979
|
-
entries = [
|
|
1980
|
-
...entries,
|
|
1981
|
-
{ id: "malformed-provider", displayName: "Bad" }
|
|
1982
|
-
];
|
|
1983
|
-
}
|
|
1984
1737
|
const result = {};
|
|
1985
1738
|
for (const raw of entries) {
|
|
1986
1739
|
const validated = validateEntry(raw);
|
|
@@ -1995,21 +1748,8 @@ function loadProviderCatalog(opts) {
|
|
|
1995
1748
|
}
|
|
1996
1749
|
return result;
|
|
1997
1750
|
}
|
|
1998
|
-
function getCatalogCapabilities(providerId) {
|
|
1999
|
-
if (_capabilitiesCache === null) {
|
|
2000
|
-
const catalog = loadProviderCatalog();
|
|
2001
|
-
_capabilitiesCache = {};
|
|
2002
|
-
for (const entry of Object.values(catalog)) {
|
|
2003
|
-
_capabilitiesCache[entry.id] = entry.capabilities;
|
|
2004
|
-
for (const alias of entry.aliases ?? []) {
|
|
2005
|
-
if (_capabilitiesCache[alias] === void 0) _capabilitiesCache[alias] = entry.capabilities;
|
|
2006
|
-
}
|
|
2007
|
-
}
|
|
2008
|
-
}
|
|
2009
|
-
return _capabilitiesCache[providerId];
|
|
2010
|
-
}
|
|
2011
1751
|
function registerCatalogProviders(opts) {
|
|
2012
|
-
const catalog = loadProviderCatalog(
|
|
1752
|
+
const catalog = loadProviderCatalog();
|
|
2013
1753
|
for (const entry of Object.values(catalog)) {
|
|
2014
1754
|
if (getProviderProfile(entry.id) !== void 0) continue;
|
|
2015
1755
|
if (entry.aliases?.some((a) => getProviderProfile(a) !== void 0)) continue;
|
|
@@ -2029,7 +1769,7 @@ function registerCatalogProviders(opts) {
|
|
|
2029
1769
|
registerProvider(profile);
|
|
2030
1770
|
}
|
|
2031
1771
|
}
|
|
2032
|
-
var __dirname_resolved, modelInfoIndex, patchedModelKeys, indexState
|
|
1772
|
+
var __dirname_resolved, modelInfoIndex, patchedModelKeys, indexState;
|
|
2033
1773
|
var init_catalog_loader = __esm({
|
|
2034
1774
|
"src/internal/providers/catalog-loader.ts"() {
|
|
2035
1775
|
init_catalog_schema();
|
|
@@ -2046,7 +1786,15 @@ var init_catalog_loader = __esm({
|
|
|
2046
1786
|
indexState = globalSingleton2("theokit-sdk.providers.model-info-loaded", () => ({
|
|
2047
1787
|
loaded: false
|
|
2048
1788
|
}));
|
|
2049
|
-
|
|
1789
|
+
}
|
|
1790
|
+
});
|
|
1791
|
+
|
|
1792
|
+
// src/compaction.ts
|
|
1793
|
+
function estimateTokens(text) {
|
|
1794
|
+
return Math.ceil(text.length / 4);
|
|
1795
|
+
}
|
|
1796
|
+
var init_compaction = __esm({
|
|
1797
|
+
"src/compaction.ts"() {
|
|
2050
1798
|
}
|
|
2051
1799
|
});
|
|
2052
1800
|
|
|
@@ -2813,9 +2561,6 @@ function registerBuiltins() {
|
|
|
2813
2561
|
registerProvider(CEREBRAS);
|
|
2814
2562
|
registerCatalogProviders();
|
|
2815
2563
|
}
|
|
2816
|
-
function _resetBuiltinsRegistered() {
|
|
2817
|
-
_registeredState.done = false;
|
|
2818
|
-
}
|
|
2819
2564
|
var _registeredState;
|
|
2820
2565
|
var init_builtin = __esm({
|
|
2821
2566
|
"src/internal/providers/builtin/index.ts"() {
|
|
@@ -2942,9 +2687,6 @@ async function loadOne(dir, entryName) {
|
|
|
2942
2687
|
}
|
|
2943
2688
|
}
|
|
2944
2689
|
}
|
|
2945
|
-
function _resetDiscovery() {
|
|
2946
|
-
discoveryState.done = false;
|
|
2947
|
-
}
|
|
2948
2690
|
var discoveryState;
|
|
2949
2691
|
var init_discovery = __esm({
|
|
2950
2692
|
"src/internal/providers/discovery.ts"() {
|
|
@@ -2956,25 +2698,9 @@ var init_discovery = __esm({
|
|
|
2956
2698
|
});
|
|
2957
2699
|
|
|
2958
2700
|
// src/internal/providers/index.ts
|
|
2959
|
-
var providers_exports = {};
|
|
2960
|
-
__export(providers_exports, {
|
|
2961
|
-
ANTHROPIC: () => ANTHROPIC,
|
|
2962
|
-
GEMINI: () => GEMINI,
|
|
2963
|
-
OPENAI: () => OPENAI,
|
|
2964
|
-
OPENROUTER: () => OPENROUTER,
|
|
2965
|
-
_resetBuiltinsRegistered: () => _resetBuiltinsRegistered,
|
|
2966
|
-
_resetDiscovery: () => _resetDiscovery,
|
|
2967
|
-
_resetProvidersForTests: () => _resetProvidersForTests,
|
|
2968
|
-
discoverProviderPlugins: () => discoverProviderPlugins,
|
|
2969
|
-
getProviderProfile: () => getProviderProfile,
|
|
2970
|
-
listProviders: () => listProviders,
|
|
2971
|
-
registerBuiltins: () => registerBuiltins,
|
|
2972
|
-
registerProvider: () => registerProvider
|
|
2973
|
-
});
|
|
2974
2701
|
var init_providers = __esm({
|
|
2975
2702
|
"src/internal/providers/index.ts"() {
|
|
2976
2703
|
init_builtin();
|
|
2977
|
-
init_discovery();
|
|
2978
2704
|
init_registry();
|
|
2979
2705
|
}
|
|
2980
2706
|
});
|
|
@@ -5715,12 +5441,6 @@ var init_vertex_router = __esm({
|
|
|
5715
5441
|
});
|
|
5716
5442
|
|
|
5717
5443
|
// src/internal/llm/router.ts
|
|
5718
|
-
var router_exports = {};
|
|
5719
|
-
__export(router_exports, {
|
|
5720
|
-
_resetCredentialPoolWarnings: () => _resetCredentialPoolWarnings,
|
|
5721
|
-
_resetNoAuthApiKeyWarnings: () => _resetNoAuthApiKeyWarnings,
|
|
5722
|
-
resolveProviderChain: () => resolveProviderChain
|
|
5723
|
-
});
|
|
5724
5444
|
function resolveProviderChain(options) {
|
|
5725
5445
|
registerBuiltins();
|
|
5726
5446
|
return buildChain(options);
|
|
@@ -5818,9 +5538,6 @@ function warnNoAuthApiKeysIgnoredOnce(provider) {
|
|
|
5818
5538
|
`
|
|
5819
5539
|
);
|
|
5820
5540
|
}
|
|
5821
|
-
function _resetNoAuthApiKeyWarnings() {
|
|
5822
|
-
warnedNoAuthApiKeys.clear();
|
|
5823
|
-
}
|
|
5824
5541
|
function sentinelForNoAuth(profile) {
|
|
5825
5542
|
return profile.authType === "none" ? profile.name : void 0;
|
|
5826
5543
|
}
|
|
@@ -5851,9 +5568,6 @@ function warnUnknownProvidersInApiKeys(apiKeys) {
|
|
|
5851
5568
|
}
|
|
5852
5569
|
}
|
|
5853
5570
|
}
|
|
5854
|
-
function _resetCredentialPoolWarnings() {
|
|
5855
|
-
warnedProviders.clear();
|
|
5856
|
-
}
|
|
5857
5571
|
function resolveApiKey2(envVars) {
|
|
5858
5572
|
for (const v of envVars) {
|
|
5859
5573
|
const value = process.env[v];
|
|
@@ -5937,61 +5651,338 @@ function selectTransport(profile, apiKey) {
|
|
|
5937
5651
|
{ code: "transport_unavailable" }
|
|
5938
5652
|
);
|
|
5939
5653
|
}
|
|
5940
|
-
var warnedNoAuthApiKeys, warnedProviders;
|
|
5941
|
-
var init_router = __esm({
|
|
5942
|
-
"src/internal/llm/router.ts"() {
|
|
5943
|
-
init_errors();
|
|
5944
|
-
init_providers();
|
|
5945
|
-
init_anthropic3();
|
|
5946
|
-
init_bedrock_anthropic();
|
|
5947
|
-
init_credential_pool();
|
|
5948
|
-
init_credential_pool_context();
|
|
5949
|
-
init_fault_injection();
|
|
5950
|
-
init_ollama_native();
|
|
5951
|
-
init_openai2();
|
|
5952
|
-
init_pool_aware_client();
|
|
5953
|
-
init_responses();
|
|
5954
|
-
init_vertex_router();
|
|
5955
|
-
warnedNoAuthApiKeys = /* @__PURE__ */ new Set();
|
|
5956
|
-
warnedProviders = /* @__PURE__ */ new Set();
|
|
5654
|
+
var warnedNoAuthApiKeys, warnedProviders;
|
|
5655
|
+
var init_router = __esm({
|
|
5656
|
+
"src/internal/llm/router.ts"() {
|
|
5657
|
+
init_errors();
|
|
5658
|
+
init_providers();
|
|
5659
|
+
init_anthropic3();
|
|
5660
|
+
init_bedrock_anthropic();
|
|
5661
|
+
init_credential_pool();
|
|
5662
|
+
init_credential_pool_context();
|
|
5663
|
+
init_fault_injection();
|
|
5664
|
+
init_ollama_native();
|
|
5665
|
+
init_openai2();
|
|
5666
|
+
init_pool_aware_client();
|
|
5667
|
+
init_responses();
|
|
5668
|
+
init_vertex_router();
|
|
5669
|
+
warnedNoAuthApiKeys = /* @__PURE__ */ new Set();
|
|
5670
|
+
warnedProviders = /* @__PURE__ */ new Set();
|
|
5671
|
+
}
|
|
5672
|
+
});
|
|
5673
|
+
|
|
5674
|
+
// src/internal/local-agent/real-local-run-provider.ts
|
|
5675
|
+
function inferProviderFromApiKey(apiKey) {
|
|
5676
|
+
if (apiKey === void 0 || apiKey.length === 0) return void 0;
|
|
5677
|
+
const byPrefix = [
|
|
5678
|
+
{ provider: "openrouter", prefix: "sk-or-" },
|
|
5679
|
+
{ provider: "anthropic", prefix: "sk-ant-" },
|
|
5680
|
+
{ provider: "openai", prefix: "sk-" }
|
|
5681
|
+
];
|
|
5682
|
+
for (const { provider, prefix } of byPrefix) {
|
|
5683
|
+
if (apiKey.startsWith(prefix) && getProviderProfile(provider) !== void 0) {
|
|
5684
|
+
return provider;
|
|
5685
|
+
}
|
|
5686
|
+
}
|
|
5687
|
+
return void 0;
|
|
5688
|
+
}
|
|
5689
|
+
function detectPrimaryProvider() {
|
|
5690
|
+
if (process.env.ANTHROPIC_API_KEY !== void 0 && process.env.ANTHROPIC_API_KEY.length > 0) {
|
|
5691
|
+
return "anthropic";
|
|
5692
|
+
}
|
|
5693
|
+
if (process.env.OPENAI_API_KEY !== void 0 && process.env.OPENAI_API_KEY.length > 0) {
|
|
5694
|
+
return "openai";
|
|
5695
|
+
}
|
|
5696
|
+
if (process.env.OPENROUTER_API_KEY !== void 0 && process.env.OPENROUTER_API_KEY.length > 0) {
|
|
5697
|
+
return "openrouter";
|
|
5698
|
+
}
|
|
5699
|
+
return "openai";
|
|
5700
|
+
}
|
|
5701
|
+
var init_real_local_run_provider = __esm({
|
|
5702
|
+
"src/internal/local-agent/real-local-run-provider.ts"() {
|
|
5703
|
+
init_providers();
|
|
5704
|
+
}
|
|
5705
|
+
});
|
|
5706
|
+
|
|
5707
|
+
// src/internal/runtime/compression/compression-model-registry.ts
|
|
5708
|
+
function resolveCompressionModel(agentModel) {
|
|
5709
|
+
const exact = EXACT_REGISTRY.get(agentModel);
|
|
5710
|
+
if (exact !== void 0) return exact;
|
|
5711
|
+
const wildcard = matchWildcard(agentModel);
|
|
5712
|
+
if (wildcard !== null) return wildcard;
|
|
5713
|
+
if (isNoAuthProvider(agentModel)) return agentModel;
|
|
5714
|
+
throw new CompressionModelUnresolvedError(agentModel);
|
|
5715
|
+
}
|
|
5716
|
+
function matchWildcard(agentModel) {
|
|
5717
|
+
for (const [pattern, replacement] of WILDCARD_REGISTRY) {
|
|
5718
|
+
const prefix = pattern.endsWith("*") ? pattern.slice(0, -1) : pattern;
|
|
5719
|
+
if (!agentModel.startsWith(prefix)) continue;
|
|
5720
|
+
const suffix = agentModel.slice(prefix.length);
|
|
5721
|
+
const replPrefix = replacement.endsWith("*") ? replacement.slice(0, -1) : replacement;
|
|
5722
|
+
return `${replPrefix}${suffix}`;
|
|
5723
|
+
}
|
|
5724
|
+
return null;
|
|
5725
|
+
}
|
|
5726
|
+
function isNoAuthProvider(agentModel) {
|
|
5727
|
+
const slashIdx = agentModel.indexOf("/");
|
|
5728
|
+
if (slashIdx <= 0) return false;
|
|
5729
|
+
return NO_AUTH_PROVIDERS.has(agentModel.slice(0, slashIdx));
|
|
5730
|
+
}
|
|
5731
|
+
var EXACT_REGISTRY, WILDCARD_REGISTRY, NO_AUTH_PROVIDERS, CompressionModelUnresolvedError;
|
|
5732
|
+
var init_compression_model_registry = __esm({
|
|
5733
|
+
"src/internal/runtime/compression/compression-model-registry.ts"() {
|
|
5734
|
+
EXACT_REGISTRY = /* @__PURE__ */ new Map([
|
|
5735
|
+
// OpenAI family
|
|
5736
|
+
["openai/gpt-4o", "openai/gpt-4o-mini"],
|
|
5737
|
+
["openai/gpt-4-turbo", "openai/gpt-4o-mini"],
|
|
5738
|
+
["openai/gpt-4", "openai/gpt-4o-mini"],
|
|
5739
|
+
["openai/o1-preview", "openai/gpt-4o-mini"],
|
|
5740
|
+
["openai/o1", "openai/gpt-4o-mini"],
|
|
5741
|
+
["openai/o3", "openai/gpt-4o-mini"],
|
|
5742
|
+
["openai/o3-mini", "openai/gpt-4o-mini"],
|
|
5743
|
+
// Anthropic family
|
|
5744
|
+
["anthropic/claude-opus-4", "anthropic/claude-3-5-haiku-latest"],
|
|
5745
|
+
["anthropic/claude-sonnet-4", "anthropic/claude-3-5-haiku-latest"],
|
|
5746
|
+
["anthropic/claude-3-5-sonnet", "anthropic/claude-3-5-haiku-latest"],
|
|
5747
|
+
["anthropic/claude-3-5-sonnet-latest", "anthropic/claude-3-5-haiku-latest"],
|
|
5748
|
+
["anthropic/claude-3-opus", "anthropic/claude-3-haiku"],
|
|
5749
|
+
["anthropic/claude-3-sonnet", "anthropic/claude-3-haiku"],
|
|
5750
|
+
// Vertex (Gemini + Anthropic-on-Vertex)
|
|
5751
|
+
["vertex/gemini-1.5-pro", "vertex/gemini-1.5-flash"],
|
|
5752
|
+
["vertex/gemini-2.0-pro", "vertex/gemini-1.5-flash"],
|
|
5753
|
+
["vertex/claude-3-5-sonnet", "vertex/claude-3-5-haiku"],
|
|
5754
|
+
["vertex/claude-3-opus", "vertex/claude-3-haiku"],
|
|
5755
|
+
// OpenRouter (preserve openrouter prefix, swap tier within same vendor)
|
|
5756
|
+
["openrouter/openai/gpt-4o", "openrouter/openai/gpt-4o-mini"],
|
|
5757
|
+
["openrouter/openai/gpt-4-turbo", "openrouter/openai/gpt-4o-mini"],
|
|
5758
|
+
["openrouter/anthropic/claude-3-5-sonnet", "openrouter/anthropic/claude-3-5-haiku"],
|
|
5759
|
+
["openrouter/anthropic/claude-opus-4", "openrouter/anthropic/claude-3-5-haiku"]
|
|
5760
|
+
]);
|
|
5761
|
+
WILDCARD_REGISTRY = [
|
|
5762
|
+
// Bedrock Anthropic: us.anthropic.claude-sonnet-* → us.anthropic.claude-3-haiku-*
|
|
5763
|
+
["bedrock/anthropic.claude-sonnet*", "bedrock/anthropic.claude-3-haiku*"],
|
|
5764
|
+
["bedrock/anthropic.claude-opus*", "bedrock/anthropic.claude-3-haiku*"],
|
|
5765
|
+
["bedrock/anthropic.claude-3-5-sonnet*", "bedrock/anthropic.claude-3-5-haiku*"]
|
|
5766
|
+
];
|
|
5767
|
+
NO_AUTH_PROVIDERS = /* @__PURE__ */ new Set(["ollama", "lmstudio", "llamacpp"]);
|
|
5768
|
+
CompressionModelUnresolvedError = class extends Error {
|
|
5769
|
+
name = "CompressionModelUnresolvedError";
|
|
5770
|
+
agentModel;
|
|
5771
|
+
constructor(agentModel) {
|
|
5772
|
+
super(
|
|
5773
|
+
`Could not resolve a same-family-cheaper-tier compression model for "${agentModel}". Provide Agent.create({compression: {model: "<your-cheaper-model>"}}) OR add "${agentModel}" to the compression-model-registry (see ADR D440).`
|
|
5774
|
+
);
|
|
5775
|
+
this.agentModel = agentModel;
|
|
5776
|
+
}
|
|
5777
|
+
};
|
|
5778
|
+
}
|
|
5779
|
+
});
|
|
5780
|
+
|
|
5781
|
+
// src/internal/runtime/compression/compression-summarizer.ts
|
|
5782
|
+
function buildCompressionPrompt(messages) {
|
|
5783
|
+
const formatted = messages.map((m) => `[${m.role}]: ${m.content}`).join("\n\n");
|
|
5784
|
+
return `Summarize the following ${messages.length} conversation messages into a concise summary that preserves ALL facts, decisions, user preferences, and context needed for the conversation to continue naturally. The summary will replace these messages in the context window. Be thorough but concise.
|
|
5785
|
+
|
|
5786
|
+
--- CONVERSATION TO SUMMARIZE ---
|
|
5787
|
+
${formatted}
|
|
5788
|
+
--- END ---`;
|
|
5789
|
+
}
|
|
5790
|
+
async function compressConversationWindow(opts) {
|
|
5791
|
+
const userPrompt = buildCompressionPrompt(opts.messages);
|
|
5792
|
+
let summary;
|
|
5793
|
+
try {
|
|
5794
|
+
summary = await opts.callLlm(opts.model, COMPRESSION_SYSTEM, userPrompt);
|
|
5795
|
+
} catch (cause) {
|
|
5796
|
+
throw new CompressionFailedError(
|
|
5797
|
+
`Compression LLM call failed: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
5798
|
+
{ cause: cause instanceof Error ? cause : void 0 }
|
|
5799
|
+
);
|
|
5800
|
+
}
|
|
5801
|
+
if (!summary || summary.trim().length === 0) {
|
|
5802
|
+
throw new CompressionFailedError(
|
|
5803
|
+
"Compression LLM returned empty summary \u2014 reduction ineffective."
|
|
5804
|
+
);
|
|
5805
|
+
}
|
|
5806
|
+
return {
|
|
5807
|
+
role: "system",
|
|
5808
|
+
content: `[Compressed conversation summary]: ${summary.trim()}`
|
|
5809
|
+
};
|
|
5810
|
+
}
|
|
5811
|
+
var CompressionFailedError, COMPRESSION_SYSTEM;
|
|
5812
|
+
var init_compression_summarizer = __esm({
|
|
5813
|
+
"src/internal/runtime/compression/compression-summarizer.ts"() {
|
|
5814
|
+
CompressionFailedError = class extends Error {
|
|
5815
|
+
name = "CompressionFailedError";
|
|
5816
|
+
};
|
|
5817
|
+
COMPRESSION_SYSTEM = "You are a conversation summarizer. Produce a concise factual summary. Preserve all decisions, preferences, code snippets, and action items. Do not add commentary or opinions. Output ONLY the summary text.";
|
|
5818
|
+
}
|
|
5819
|
+
});
|
|
5820
|
+
|
|
5821
|
+
// src/internal/session/agent-session-store.ts
|
|
5822
|
+
function seedTranscript(prior, opts) {
|
|
5823
|
+
return SessionTranscript.fromRecords(prior, opts);
|
|
5824
|
+
}
|
|
5825
|
+
function mapAgentTurn(steps) {
|
|
5826
|
+
const assistant = {};
|
|
5827
|
+
const toolResults = [];
|
|
5828
|
+
const toolCalls = [];
|
|
5829
|
+
for (const step of steps) {
|
|
5830
|
+
if (step.type === "thinkingMessage") assistant.thinking = step.message.text;
|
|
5831
|
+
else if (step.type === "assistantMessage") assistant.text = step.message.text;
|
|
5832
|
+
else if (step.type === "toolCall")
|
|
5833
|
+
toolCalls.push({
|
|
5834
|
+
id: step.message.callId,
|
|
5835
|
+
name: step.message.name,
|
|
5836
|
+
input: step.message.args ?? {}
|
|
5837
|
+
});
|
|
5838
|
+
else
|
|
5839
|
+
toolResults.push({
|
|
5840
|
+
toolUseId: step.message.callId,
|
|
5841
|
+
content: step.message.result,
|
|
5842
|
+
isError: step.message.isError
|
|
5843
|
+
});
|
|
5844
|
+
}
|
|
5845
|
+
if (toolCalls.length > 0) assistant.toolCalls = toolCalls;
|
|
5846
|
+
return { assistant, toolResults };
|
|
5847
|
+
}
|
|
5848
|
+
function hasAssistantContent(a) {
|
|
5849
|
+
return a.text !== void 0 || a.thinking !== void 0 || (a.toolCalls?.length ?? 0) > 0;
|
|
5850
|
+
}
|
|
5851
|
+
function appendConversation(transcript, conversation) {
|
|
5852
|
+
for (const ct of conversation) {
|
|
5853
|
+
if (ct.type !== "agentConversationTurn") continue;
|
|
5854
|
+
const { assistant, toolResults } = mapAgentTurn(ct.turn.steps);
|
|
5855
|
+
if (hasAssistantContent(assistant)) transcript.appendAssistantTurn(assistant);
|
|
5856
|
+
if (toolResults.length > 0) transcript.appendToolResults(toolResults);
|
|
5857
|
+
}
|
|
5858
|
+
}
|
|
5859
|
+
async function readSessionMessages(store, agentId) {
|
|
5860
|
+
const records = await store.readRecords(agentId);
|
|
5861
|
+
return reconstructMessages(records).map(narrowToSessionMessage);
|
|
5862
|
+
}
|
|
5863
|
+
function partToText(p) {
|
|
5864
|
+
if (p.type === "text") return p.text ?? "";
|
|
5865
|
+
if (p.type === "tool_use") return `[tool call] ${p.name ?? ""}`;
|
|
5866
|
+
if (p.type === "tool_result") {
|
|
5867
|
+
const body = typeof p.content === "string" ? p.content : JSON.stringify(p.content);
|
|
5868
|
+
return `[tool result] ${body}`;
|
|
5869
|
+
}
|
|
5870
|
+
return "";
|
|
5871
|
+
}
|
|
5872
|
+
function narrowToSessionMessage(m) {
|
|
5873
|
+
const role = m.role === "user" ? "user" : "assistant";
|
|
5874
|
+
const text = m.content.map(partToText).filter((s) => s.length > 0).join("\n");
|
|
5875
|
+
return { role, text };
|
|
5876
|
+
}
|
|
5877
|
+
function deltaRecords(transcript, priorLength) {
|
|
5878
|
+
return transcript.records().slice(priorLength);
|
|
5879
|
+
}
|
|
5880
|
+
async function persistTurn(store, loc, sessionId, turn) {
|
|
5881
|
+
const prior = await store.readRecords(loc.agentId);
|
|
5882
|
+
const transcript = seedTranscript(prior, { cwd: loc.cwd, sessionId, model: loc.model });
|
|
5883
|
+
transcript.appendUserTurn(turn.userText);
|
|
5884
|
+
appendConversation(transcript, turn.conversation);
|
|
5885
|
+
await store.appendRecords(loc.agentId, deltaRecords(transcript, prior.length));
|
|
5886
|
+
}
|
|
5887
|
+
var init_agent_session_store = __esm({
|
|
5888
|
+
"src/internal/session/agent-session-store.ts"() {
|
|
5889
|
+
init_session_transcript();
|
|
5957
5890
|
}
|
|
5958
5891
|
});
|
|
5959
5892
|
|
|
5960
|
-
// src/internal/
|
|
5961
|
-
|
|
5962
|
-
|
|
5963
|
-
|
|
5964
|
-
|
|
5965
|
-
|
|
5966
|
-
|
|
5967
|
-
|
|
5968
|
-
|
|
5969
|
-
|
|
5970
|
-
|
|
5971
|
-
|
|
5972
|
-
|
|
5973
|
-
|
|
5974
|
-
|
|
5975
|
-
|
|
5893
|
+
// src/internal/session/agent-session.ts
|
|
5894
|
+
function transcriptKey(cwd, agentId) {
|
|
5895
|
+
return `${cwd}::${agentId}`;
|
|
5896
|
+
}
|
|
5897
|
+
function appendSessionMessage(agentId, message) {
|
|
5898
|
+
const existing = sessions.get(agentId) ?? [];
|
|
5899
|
+
existing.push(message);
|
|
5900
|
+
sessions.set(agentId, existing);
|
|
5901
|
+
}
|
|
5902
|
+
function getSessionMessages(agentId) {
|
|
5903
|
+
return sessions.get(agentId) ?? [];
|
|
5904
|
+
}
|
|
5905
|
+
function persistTurnToTranscript(store, loc, sessionId, turn, onCompact) {
|
|
5906
|
+
const key = transcriptKey(loc.cwd, loc.agentId);
|
|
5907
|
+
const chained = (pendingWrites.get(key) ?? Promise.resolve()).then(async () => {
|
|
5908
|
+
try {
|
|
5909
|
+
await persistTurn(store, loc, sessionId, turn);
|
|
5910
|
+
const count = (recordCounts.get(key) ?? 0) + 1;
|
|
5911
|
+
recordCounts.set(key, count);
|
|
5912
|
+
if (turn.autoCompact !== void 0) {
|
|
5913
|
+
const { autoCompactIfNeeded: autoCompactIfNeeded2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
5914
|
+
const fired = await autoCompactIfNeeded2({
|
|
5915
|
+
store,
|
|
5916
|
+
loc,
|
|
5917
|
+
sessionId,
|
|
5918
|
+
usageTotal: turn.autoCompact.usageTotal,
|
|
5919
|
+
contextWindow: turn.autoCompact.contextWindow,
|
|
5920
|
+
turnCount: count,
|
|
5921
|
+
summarize: turn.autoCompact.summarize
|
|
5922
|
+
});
|
|
5923
|
+
if (fired) onCompact?.();
|
|
5924
|
+
}
|
|
5925
|
+
} catch (cause) {
|
|
5926
|
+
const msg = cause instanceof Error ? cause.message : String(cause);
|
|
5927
|
+
process.stderr.write(
|
|
5928
|
+
`[theokit-sdk] session transcript write failed (${loc.agentId}): ${msg}
|
|
5929
|
+
`
|
|
5930
|
+
);
|
|
5976
5931
|
}
|
|
5977
|
-
}
|
|
5978
|
-
|
|
5932
|
+
});
|
|
5933
|
+
pendingWrites.set(
|
|
5934
|
+
key,
|
|
5935
|
+
chained.then(
|
|
5936
|
+
() => void 0,
|
|
5937
|
+
() => void 0
|
|
5938
|
+
)
|
|
5939
|
+
);
|
|
5979
5940
|
}
|
|
5980
|
-
function
|
|
5981
|
-
|
|
5982
|
-
|
|
5983
|
-
|
|
5984
|
-
|
|
5985
|
-
|
|
5941
|
+
async function hydrateSession(agentId, loc) {
|
|
5942
|
+
const key = transcriptKey(loc.cwd, agentId);
|
|
5943
|
+
if (hydratedKeys.has(key)) return;
|
|
5944
|
+
hydratedKeys.add(key);
|
|
5945
|
+
const persisted = await readSessionMessages(loc.store, agentId);
|
|
5946
|
+
if (persisted.length === 0) return;
|
|
5947
|
+
if (!sessions.has(agentId) || sessions.get(agentId)?.length === 0) {
|
|
5948
|
+
sessions.set(agentId, persisted);
|
|
5986
5949
|
}
|
|
5987
|
-
|
|
5988
|
-
|
|
5950
|
+
}
|
|
5951
|
+
async function flushSessionWrites() {
|
|
5952
|
+
while (pendingWrites.size > 0) {
|
|
5953
|
+
const all = Array.from(pendingWrites.values());
|
|
5954
|
+
pendingWrites.clear();
|
|
5955
|
+
await Promise.all(all);
|
|
5989
5956
|
}
|
|
5990
|
-
return "openai";
|
|
5991
5957
|
}
|
|
5992
|
-
|
|
5993
|
-
|
|
5994
|
-
|
|
5958
|
+
function clearSession(agentId) {
|
|
5959
|
+
sessions.delete(agentId);
|
|
5960
|
+
}
|
|
5961
|
+
function invalidateSessionCache(cwd, agentId) {
|
|
5962
|
+
sessions.delete(agentId);
|
|
5963
|
+
hydratedKeys.delete(transcriptKey(cwd, agentId));
|
|
5964
|
+
}
|
|
5965
|
+
function enqueueSessionWrite(cwd, agentId, fn) {
|
|
5966
|
+
const key = transcriptKey(cwd, agentId);
|
|
5967
|
+
const prior = pendingWrites.get(key) ?? Promise.resolve();
|
|
5968
|
+
const result = prior.then(fn);
|
|
5969
|
+
pendingWrites.set(
|
|
5970
|
+
key,
|
|
5971
|
+
result.then(
|
|
5972
|
+
() => void 0,
|
|
5973
|
+
() => void 0
|
|
5974
|
+
)
|
|
5975
|
+
);
|
|
5976
|
+
return result;
|
|
5977
|
+
}
|
|
5978
|
+
var sessions, hydratedKeys, pendingWrites, recordCounts;
|
|
5979
|
+
var init_agent_session = __esm({
|
|
5980
|
+
"src/internal/session/agent-session.ts"() {
|
|
5981
|
+
init_agent_session_store();
|
|
5982
|
+
sessions = /* @__PURE__ */ new Map();
|
|
5983
|
+
hydratedKeys = /* @__PURE__ */ new Set();
|
|
5984
|
+
pendingWrites = /* @__PURE__ */ new Map();
|
|
5985
|
+
recordCounts = /* @__PURE__ */ new Map();
|
|
5995
5986
|
}
|
|
5996
5987
|
});
|
|
5997
5988
|
|
|
@@ -6004,6 +5995,7 @@ __export(compact_session_exports, {
|
|
|
6004
5995
|
buildDefaultSummarizer: () => buildDefaultSummarizer,
|
|
6005
5996
|
compactSessionTranscript: () => compactSessionTranscript,
|
|
6006
5997
|
isCompactSummary: () => isCompactSummary,
|
|
5998
|
+
resolveSummarizerRoute: () => resolveSummarizerRoute,
|
|
6007
5999
|
shouldAutoCompact: () => shouldAutoCompact
|
|
6008
6000
|
});
|
|
6009
6001
|
function isCompactSummary(content) {
|
|
@@ -6056,35 +6048,37 @@ ${summaryBody}`;
|
|
|
6056
6048
|
transcript.appendUserTurn(summary);
|
|
6057
6049
|
const delta = transcript.records().slice(prior.length);
|
|
6058
6050
|
await opts.store.appendRecords(opts.loc.agentId, delta);
|
|
6059
|
-
|
|
6060
|
-
invalidateSessionCache2(opts.loc.cwd, opts.loc.agentId);
|
|
6051
|
+
invalidateSessionCache(opts.loc.cwd, opts.loc.agentId);
|
|
6061
6052
|
const postTokens = estimateTokens([...preserved, summary].join("\n"));
|
|
6062
6053
|
return { preTokens, postTokens };
|
|
6063
6054
|
}
|
|
6055
|
+
function resolveSummarizerRoute(opts) {
|
|
6056
|
+
const provider = opts.keyProvider ?? (opts.prefixHasProfile && opts.modelPrefix !== void 0 ? opts.modelPrefix : opts.envProvider);
|
|
6057
|
+
return { provider, fullSlug: provider !== opts.modelPrefix };
|
|
6058
|
+
}
|
|
6064
6059
|
function buildDefaultSummarizer(opts) {
|
|
6065
6060
|
return async (messages) => {
|
|
6066
|
-
|
|
6067
|
-
const { resolveCompressionModel: resolveCompressionModel2 } = await Promise.resolve().then(() => (init_compression_model_registry(), compression_model_registry_exports));
|
|
6068
|
-
const { resolveProviderChain: resolveProviderChain2 } = await Promise.resolve().then(() => (init_router(), router_exports));
|
|
6069
|
-
const { inferProviderFromApiKey: inferProviderFromApiKey2, detectPrimaryProvider: detectPrimaryProvider2 } = await Promise.resolve().then(() => (init_real_local_run_provider(), real_local_run_provider_exports));
|
|
6070
|
-
const keyProvider = inferProviderFromApiKey2(opts.apiKey);
|
|
6061
|
+
registerBuiltins();
|
|
6071
6062
|
const modelPrefix = opts.agentModel.includes("/") ? opts.agentModel.slice(0, opts.agentModel.indexOf("/")) : void 0;
|
|
6072
|
-
const
|
|
6073
|
-
|
|
6074
|
-
|
|
6075
|
-
|
|
6063
|
+
const route = resolveSummarizerRoute({
|
|
6064
|
+
keyProvider: inferProviderFromApiKey(opts.apiKey),
|
|
6065
|
+
modelPrefix,
|
|
6066
|
+
prefixHasProfile: modelPrefix !== void 0 && getProviderProfile(modelPrefix) !== void 0,
|
|
6067
|
+
envProvider: detectPrimaryProvider()
|
|
6068
|
+
});
|
|
6069
|
+
const provider = route.provider;
|
|
6076
6070
|
let model;
|
|
6077
|
-
if (
|
|
6071
|
+
if (route.fullSlug) {
|
|
6078
6072
|
model = opts.agentModel;
|
|
6079
6073
|
} else {
|
|
6080
6074
|
try {
|
|
6081
|
-
model =
|
|
6075
|
+
model = resolveCompressionModel(opts.agentModel);
|
|
6082
6076
|
} catch {
|
|
6083
6077
|
model = opts.agentModel;
|
|
6084
6078
|
}
|
|
6085
6079
|
}
|
|
6086
6080
|
const callLlm = async (m, system, user) => {
|
|
6087
|
-
const chain =
|
|
6081
|
+
const chain = resolveProviderChain({
|
|
6088
6082
|
primary: provider,
|
|
6089
6083
|
...opts.apiKey !== void 0 ? { apiKeys: { [provider]: [opts.apiKey] } } : {}
|
|
6090
6084
|
});
|
|
@@ -6104,7 +6098,7 @@ function buildDefaultSummarizer(opts) {
|
|
|
6104
6098
|
}
|
|
6105
6099
|
return text;
|
|
6106
6100
|
};
|
|
6107
|
-
const summary = await
|
|
6101
|
+
const summary = await compressConversationWindow({ messages: [...messages], model, callLlm });
|
|
6108
6102
|
return summary.content;
|
|
6109
6103
|
};
|
|
6110
6104
|
}
|
|
@@ -6138,6 +6132,12 @@ var COMPACT_SUMMARY_MARKER, COMPACT_USER_MESSAGE_MAX_TOKENS, autoCompactAttempts
|
|
|
6138
6132
|
var init_compact_session = __esm({
|
|
6139
6133
|
"src/internal/session/compact-session.ts"() {
|
|
6140
6134
|
init_compaction();
|
|
6135
|
+
init_router();
|
|
6136
|
+
init_real_local_run_provider();
|
|
6137
|
+
init_providers();
|
|
6138
|
+
init_compression_model_registry();
|
|
6139
|
+
init_compression_summarizer();
|
|
6140
|
+
init_agent_session();
|
|
6141
6141
|
init_session_transcript();
|
|
6142
6142
|
COMPACT_SUMMARY_MARKER = "[[theokit:compact-summary]]";
|
|
6143
6143
|
COMPACT_USER_MESSAGE_MAX_TOKENS = 2e4;
|
|
@@ -6149,122 +6149,6 @@ var init_compact_session = __esm({
|
|
|
6149
6149
|
})();
|
|
6150
6150
|
}
|
|
6151
6151
|
});
|
|
6152
|
-
|
|
6153
|
-
// src/internal/session/agent-session.ts
|
|
6154
|
-
var agent_session_exports = {};
|
|
6155
|
-
__export(agent_session_exports, {
|
|
6156
|
-
appendSessionMessage: () => appendSessionMessage,
|
|
6157
|
-
clearAllSessions: () => clearAllSessions,
|
|
6158
|
-
clearSession: () => clearSession,
|
|
6159
|
-
enqueueSessionWrite: () => enqueueSessionWrite,
|
|
6160
|
-
flushSessionWrites: () => flushSessionWrites,
|
|
6161
|
-
getSessionMessages: () => getSessionMessages,
|
|
6162
|
-
hydrateSession: () => hydrateSession,
|
|
6163
|
-
invalidateSessionCache: () => invalidateSessionCache,
|
|
6164
|
-
persistTurnToTranscript: () => persistTurnToTranscript
|
|
6165
|
-
});
|
|
6166
|
-
function transcriptKey(cwd, agentId) {
|
|
6167
|
-
return `${cwd}::${agentId}`;
|
|
6168
|
-
}
|
|
6169
|
-
function appendSessionMessage(agentId, message) {
|
|
6170
|
-
const existing = sessions.get(agentId) ?? [];
|
|
6171
|
-
existing.push(message);
|
|
6172
|
-
sessions.set(agentId, existing);
|
|
6173
|
-
}
|
|
6174
|
-
function getSessionMessages(agentId) {
|
|
6175
|
-
return sessions.get(agentId) ?? [];
|
|
6176
|
-
}
|
|
6177
|
-
function persistTurnToTranscript(store, loc, sessionId, turn, onCompact) {
|
|
6178
|
-
const key = transcriptKey(loc.cwd, loc.agentId);
|
|
6179
|
-
const chained = (pendingWrites.get(key) ?? Promise.resolve()).then(async () => {
|
|
6180
|
-
try {
|
|
6181
|
-
await persistTurn(store, loc, sessionId, turn);
|
|
6182
|
-
const count = (recordCounts.get(key) ?? 0) + 1;
|
|
6183
|
-
recordCounts.set(key, count);
|
|
6184
|
-
if (turn.autoCompact !== void 0) {
|
|
6185
|
-
const { autoCompactIfNeeded: autoCompactIfNeeded2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
6186
|
-
const fired = await autoCompactIfNeeded2({
|
|
6187
|
-
store,
|
|
6188
|
-
loc,
|
|
6189
|
-
sessionId,
|
|
6190
|
-
usageTotal: turn.autoCompact.usageTotal,
|
|
6191
|
-
contextWindow: turn.autoCompact.contextWindow,
|
|
6192
|
-
turnCount: count,
|
|
6193
|
-
summarize: turn.autoCompact.summarize
|
|
6194
|
-
});
|
|
6195
|
-
if (fired) onCompact?.();
|
|
6196
|
-
}
|
|
6197
|
-
} catch (cause) {
|
|
6198
|
-
const msg = cause instanceof Error ? cause.message : String(cause);
|
|
6199
|
-
process.stderr.write(
|
|
6200
|
-
`[theokit-sdk] session transcript write failed (${loc.agentId}): ${msg}
|
|
6201
|
-
`
|
|
6202
|
-
);
|
|
6203
|
-
}
|
|
6204
|
-
});
|
|
6205
|
-
pendingWrites.set(
|
|
6206
|
-
key,
|
|
6207
|
-
chained.then(
|
|
6208
|
-
() => void 0,
|
|
6209
|
-
() => void 0
|
|
6210
|
-
)
|
|
6211
|
-
);
|
|
6212
|
-
}
|
|
6213
|
-
async function hydrateSession(agentId, loc) {
|
|
6214
|
-
const key = transcriptKey(loc.cwd, agentId);
|
|
6215
|
-
if (hydratedKeys.has(key)) return;
|
|
6216
|
-
hydratedKeys.add(key);
|
|
6217
|
-
const persisted = await readSessionMessages(loc.store, agentId);
|
|
6218
|
-
if (persisted.length === 0) return;
|
|
6219
|
-
if (!sessions.has(agentId) || sessions.get(agentId)?.length === 0) {
|
|
6220
|
-
sessions.set(agentId, persisted);
|
|
6221
|
-
}
|
|
6222
|
-
}
|
|
6223
|
-
async function flushSessionWrites() {
|
|
6224
|
-
while (pendingWrites.size > 0) {
|
|
6225
|
-
const all = Array.from(pendingWrites.values());
|
|
6226
|
-
pendingWrites.clear();
|
|
6227
|
-
await Promise.all(all);
|
|
6228
|
-
}
|
|
6229
|
-
}
|
|
6230
|
-
function clearSession(agentId) {
|
|
6231
|
-
sessions.delete(agentId);
|
|
6232
|
-
}
|
|
6233
|
-
function invalidateSessionCache(cwd, agentId) {
|
|
6234
|
-
sessions.delete(agentId);
|
|
6235
|
-
hydratedKeys.delete(transcriptKey(cwd, agentId));
|
|
6236
|
-
}
|
|
6237
|
-
function enqueueSessionWrite(cwd, agentId, fn) {
|
|
6238
|
-
const key = transcriptKey(cwd, agentId);
|
|
6239
|
-
const prior = pendingWrites.get(key) ?? Promise.resolve();
|
|
6240
|
-
const result = prior.then(fn);
|
|
6241
|
-
pendingWrites.set(
|
|
6242
|
-
key,
|
|
6243
|
-
result.then(
|
|
6244
|
-
() => void 0,
|
|
6245
|
-
() => void 0
|
|
6246
|
-
)
|
|
6247
|
-
);
|
|
6248
|
-
return result;
|
|
6249
|
-
}
|
|
6250
|
-
function clearAllSessions() {
|
|
6251
|
-
sessions.clear();
|
|
6252
|
-
hydratedKeys.clear();
|
|
6253
|
-
recordCounts.clear();
|
|
6254
|
-
const g = globalThis;
|
|
6255
|
-
const sym = /* @__PURE__ */ Symbol.for("theokit-sdk.session.auto-compact-attempts");
|
|
6256
|
-
g[sym]?.clear();
|
|
6257
|
-
}
|
|
6258
|
-
var sessions, hydratedKeys, pendingWrites, recordCounts;
|
|
6259
|
-
var init_agent_session = __esm({
|
|
6260
|
-
"src/internal/session/agent-session.ts"() {
|
|
6261
|
-
init_agent_session_store();
|
|
6262
|
-
sessions = /* @__PURE__ */ new Map();
|
|
6263
|
-
hydratedKeys = /* @__PURE__ */ new Set();
|
|
6264
|
-
pendingWrites = /* @__PURE__ */ new Map();
|
|
6265
|
-
recordCounts = /* @__PURE__ */ new Map();
|
|
6266
|
-
}
|
|
6267
|
-
});
|
|
6268
6152
|
async function withToolWhitelist(whitelist, fn) {
|
|
6269
6153
|
return toolWhitelistStore.run(whitelist, fn);
|
|
6270
6154
|
}
|
|
@@ -10640,6 +10524,10 @@ async function writeSessionSummary(input) {
|
|
|
10640
10524
|
await replaceFileAtomic(path, body);
|
|
10641
10525
|
}
|
|
10642
10526
|
|
|
10527
|
+
// src/internal/runtime/lifecycle/post-run-lifecycle.ts
|
|
10528
|
+
init_catalog_loader();
|
|
10529
|
+
init_compact_session();
|
|
10530
|
+
|
|
10643
10531
|
// src/internal/runtime/memory/memory-path-selector.ts
|
|
10644
10532
|
var PORT_MEMORY_PATH_ENV_VAR = "THEOKIT_PORT_MEMORY_PATH";
|
|
10645
10533
|
function shouldUsePortMemoryPath() {
|
|
@@ -10687,9 +10575,7 @@ async function runPostRunLifecycle(inputs) {
|
|
|
10687
10575
|
appendSessionMessage(agentId, { role: "assistant", text: result.result });
|
|
10688
10576
|
}
|
|
10689
10577
|
const conversation = await safeConversation(run);
|
|
10690
|
-
const
|
|
10691
|
-
const { buildDefaultSummarizer: buildDefaultSummarizer2 } = await Promise.resolve().then(() => (init_compact_session(), compact_session_exports));
|
|
10692
|
-
const contextWindow = getCatalogModelInfo2(model)?.limit?.context;
|
|
10578
|
+
const contextWindow = getCatalogModelInfo(model)?.limit?.context;
|
|
10693
10579
|
if (contextWindow === void 0) {
|
|
10694
10580
|
const g = globalThis;
|
|
10695
10581
|
const sym = /* @__PURE__ */ Symbol.for("theokit-sdk.compact.no-cw-warned");
|
|
@@ -10714,7 +10600,7 @@ async function runPostRunLifecycle(inputs) {
|
|
|
10714
10600
|
autoCompact: {
|
|
10715
10601
|
usageTotal: usageForTrigger,
|
|
10716
10602
|
contextWindow,
|
|
10717
|
-
summarize:
|
|
10603
|
+
summarize: buildDefaultSummarizer({
|
|
10718
10604
|
agentModel: model,
|
|
10719
10605
|
...inputs.apiKey !== void 0 ? { apiKey: inputs.apiKey } : {}
|
|
10720
10606
|
})
|
|
@@ -19694,6 +19580,7 @@ async function getRegisteredAgentOrThrow(agentId) {
|
|
|
19694
19580
|
// src/agent.ts
|
|
19695
19581
|
init_errors();
|
|
19696
19582
|
init_discovery();
|
|
19583
|
+
init_agent_session();
|
|
19697
19584
|
init_agent_factory_registry();
|
|
19698
19585
|
var streamObjectImport;
|
|
19699
19586
|
var Agent = class _Agent {
|
|
@@ -19996,8 +19883,7 @@ var Agent = class _Agent {
|
|
|
19996
19883
|
const { defaultBaseDir: defaultBaseDir2, expandTilde: expandTilde2 } = await Promise.resolve().then(() => (init_session_transcript(), session_transcript_exports));
|
|
19997
19884
|
const baseDir = reg.options.local?.baseDir !== void 0 ? expandTilde2(reg.options.local.baseDir) : defaultBaseDir2();
|
|
19998
19885
|
const store = new FsSessionStore2({ baseDir, cwd });
|
|
19999
|
-
|
|
20000
|
-
return enqueueSessionWrite2(cwd, agentId, () => compactSessionTranscript2({
|
|
19886
|
+
return enqueueSessionWrite(cwd, agentId, () => compactSessionTranscript2({
|
|
20001
19887
|
store,
|
|
20002
19888
|
loc: { cwd, agentId, model },
|
|
20003
19889
|
sessionId: agentId,
|