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