@warmdrift/kgauto-compiler 2.0.0-alpha.10
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/README.md +240 -0
- package/dist/chunk-3KVKELZN.mjs +657 -0
- package/dist/chunk-5TI6PNSK.mjs +95 -0
- package/dist/dialect.d.mts +99 -0
- package/dist/dialect.d.ts +99 -0
- package/dist/dialect.js +127 -0
- package/dist/dialect.mjs +22 -0
- package/dist/index.d.mts +509 -0
- package/dist/index.d.ts +509 -0
- package/dist/index.js +2559 -0
- package/dist/index.mjs +1784 -0
- package/dist/profiles-Bgri1pe7.d.ts +728 -0
- package/dist/profiles-DO6R9moS.d.mts +728 -0
- package/dist/profiles.d.mts +2 -0
- package/dist/profiles.d.ts +2 -0
- package/dist/profiles.js +685 -0
- package/dist/profiles.mjs +14 -0
- package/package.json +59 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
import { M as ModelProfile, C as CompilePolicy, N as NormalizedResponse, A as ApiKeys, P as ProviderOverrides, a as CompiledRequest, b as PromptIR, c as CallOptions, d as CallResult, R as RecordInput, O as OracleScore, e as CompileResult, B as BestPracticeAdvisory, f as Provider } from './profiles-Bgri1pe7.js';
|
|
2
|
+
export { g as ALIASES, h as CacheStrategy, i as CallAttempt, j as CallError, k as CliffRule, l as Constraints, F as FallbackReason, H as HistoryCachePolicy, I as IntentDeclaration, L as LoweringSpec, m as Message, n as MutationApplied, o as NormalizedTokens, p as PromptSection, q as RecoveryRule, S as StructuredOutputCapability, r as SystemPromptMode, T as ToolCall, s as ToolDefinition, t as allProfiles, u as getProfile, v as profilesByProvider, w as tryGetProfile } from './profiles-Bgri1pe7.js';
|
|
3
|
+
import { IntentArchetypeName } from './dialect.js';
|
|
4
|
+
export { ALL_ARCHETYPES, ContextBucket, DIALECT_VERSION, HistoryDepth, INTENT_ARCHETYPES, OutputMode, ShapeSignature, ToolCountBucket, bucketContext, bucketHistory, bucketToolCount, hashShape, isArchetype, learningKey } from './dialect.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* compile() — the main orchestrator.
|
|
8
|
+
*
|
|
9
|
+
* Runs all passes in order, picks a target, lowers to wire format, returns
|
|
10
|
+
* a CompileResult the caller uses to make the actual provider call.
|
|
11
|
+
*
|
|
12
|
+
* Pure function in v2.0.0-alpha.1: no network, no I/O, no mutation cache yet.
|
|
13
|
+
* Mutation cache will be added in v2.1 when the brain has data to push.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
interface CompileOptions {
|
|
17
|
+
/** Custom profile resolver — for tests or custom profile sets. */
|
|
18
|
+
profileResolver?: (id: string) => ModelProfile;
|
|
19
|
+
/** Tool relevance threshold (default 0.2). */
|
|
20
|
+
toolRelevanceThreshold?: number;
|
|
21
|
+
/** History compression — turns count threshold (default 8). */
|
|
22
|
+
compressHistoryAfter?: number;
|
|
23
|
+
/**
|
|
24
|
+
* History compression — token threshold (alpha.7). When total history
|
|
25
|
+
* tokens exceed this AND there are more recent turns to keep, compress
|
|
26
|
+
* even when count threshold is below `compressHistoryAfter`. Catches
|
|
27
|
+
* fat-message bloat (tool-using agents pack many tool-call/result pairs
|
|
28
|
+
* into single assistant messages — count stays low, tokens explode).
|
|
29
|
+
* Default undefined (disabled — backward-compatible).
|
|
30
|
+
*/
|
|
31
|
+
compressHistoryAboveTokens?: number;
|
|
32
|
+
/**
|
|
33
|
+
* Consumer-declared policy. Filters blocked models, enforces cost
|
|
34
|
+
* ceiling, boosts preferred. See CompilePolicy in ir.ts.
|
|
35
|
+
*/
|
|
36
|
+
policy?: CompilePolicy;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* execute() — fire a CompiledRequest at the right provider, normalize the
|
|
41
|
+
* response shape across providers.
|
|
42
|
+
*
|
|
43
|
+
* This is what call() composes with compile() to own the network round-trip.
|
|
44
|
+
* Pure-fetch consumers stop reinventing per-provider parsing.
|
|
45
|
+
*
|
|
46
|
+
* Error classification follows L-061: 429/5xx/timeout/model_not_found are
|
|
47
|
+
* retryable; auth/validation are terminal. The orchestrator (call.ts) walks
|
|
48
|
+
* fallbackChain on retryable; surfaces terminal immediately.
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
interface ExecuteOptions {
|
|
52
|
+
apiKeys?: ApiKeys;
|
|
53
|
+
fetchImpl?: typeof fetch;
|
|
54
|
+
providerOverrides?: ProviderOverrides;
|
|
55
|
+
}
|
|
56
|
+
interface ExecuteOk {
|
|
57
|
+
ok: true;
|
|
58
|
+
status: number;
|
|
59
|
+
response: NormalizedResponse;
|
|
60
|
+
}
|
|
61
|
+
interface ExecuteErr {
|
|
62
|
+
ok: false;
|
|
63
|
+
status: number;
|
|
64
|
+
errorType: 'retryable' | 'terminal';
|
|
65
|
+
errorCode: string;
|
|
66
|
+
message: string;
|
|
67
|
+
raw: unknown;
|
|
68
|
+
}
|
|
69
|
+
type ExecuteResult = ExecuteOk | ExecuteErr;
|
|
70
|
+
declare function execute(request: CompiledRequest, opts?: ExecuteOptions): Promise<ExecuteResult>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* call() — the high-level orchestrator that owns the network round-trip.
|
|
74
|
+
*
|
|
75
|
+
* call(ir) ≈ compile(ir) → execute(request) → normalize(response) → record(...)
|
|
76
|
+
*
|
|
77
|
+
* Plain-fetch consumers (playbacksam, future incantato/glass-box/ivypass) use
|
|
78
|
+
* call() instead of importing fetch and reinventing per-provider response
|
|
79
|
+
* parsing. AI-SDK consumers can keep using compile() unchanged — call() is a
|
|
80
|
+
* sibling, not a replacement.
|
|
81
|
+
*
|
|
82
|
+
* Retry/fallback: on a retryable error (429, 5xx, 404, network), walk
|
|
83
|
+
* CompileResult.fallbackChain by re-running compile() with forceModel set on
|
|
84
|
+
* each fallback target. Each fallback gets fresh cliffs + fresh lowering.
|
|
85
|
+
* Hard cap at fallbackChain.length + 1 attempts. Terminal errors short-circuit.
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Compile, execute, normalize, record. Returns a CallResult once a provider
|
|
90
|
+
* actually serves the request. Throws CallError if the fallback chain is
|
|
91
|
+
* exhausted without success.
|
|
92
|
+
*/
|
|
93
|
+
declare function call(ir: PromptIR, opts?: CallOptions): Promise<CallResult>;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Brain client — fire-and-forget telemetry to the central kgauto Supabase.
|
|
97
|
+
*
|
|
98
|
+
* The brain is the centralized learning store. Apps POST outcomes here;
|
|
99
|
+
* mutations flow back through a separate pull (in v2.1).
|
|
100
|
+
*
|
|
101
|
+
* Design: never blocks the caller. Failures are silent (logged via optional
|
|
102
|
+
* onError hook). Uses fetch() — works in Node 18+, Edge runtimes, and browsers.
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
interface BrainConfig {
|
|
106
|
+
/** Brain HTTP endpoint base URL (e.g., https://kgauto-brain.vercel.app/api). */
|
|
107
|
+
endpoint: string;
|
|
108
|
+
/** Bearer token for auth. */
|
|
109
|
+
apiKey?: string;
|
|
110
|
+
/** Optional error hook for debugging. Defaults to console.warn. */
|
|
111
|
+
onError?: (err: unknown) => void;
|
|
112
|
+
/** If true, records are sent synchronously and awaited (test mode). */
|
|
113
|
+
sync?: boolean;
|
|
114
|
+
/** Optional fetch override (for tests). */
|
|
115
|
+
fetchImpl?: typeof fetch;
|
|
116
|
+
}
|
|
117
|
+
declare function configureBrain(config: BrainConfig): void;
|
|
118
|
+
declare function clearBrain(): void;
|
|
119
|
+
/**
|
|
120
|
+
* Record the outcome of a compiled call. Fire-and-forget by default.
|
|
121
|
+
*
|
|
122
|
+
* Returns a Promise so callers in `sync` mode can await; in async mode the
|
|
123
|
+
* promise resolves immediately (after the request is queued) and any
|
|
124
|
+
* network error is swallowed/forwarded to onError.
|
|
125
|
+
*/
|
|
126
|
+
declare function record(input: RecordInput): Promise<void>;
|
|
127
|
+
/**
|
|
128
|
+
* Wire shape POSTed by `record()` to the brain proxy's `/outcomes` endpoint.
|
|
129
|
+
*
|
|
130
|
+
* Exported so consumer proxies can `import { OutcomePayload } from
|
|
131
|
+
* '@warmdrift/kgauto-compiler'` instead of redefining the shape — that way
|
|
132
|
+
* TypeScript catches future schema additions (cache fields, advisory
|
|
133
|
+
* telemetry, etc.) at consumer build time, not silently at runtime.
|
|
134
|
+
*
|
|
135
|
+
* **Forward-compat rule:** consumer proxies should pass the body through to
|
|
136
|
+
* Supabase rather than reconstructing field-by-field. The recommended shape
|
|
137
|
+
* is `const row = { ...body }` (or `await supabase.from('compile_outcomes')
|
|
138
|
+
* .insert(body)` directly). Filtering proxies break schema evolution
|
|
139
|
+
* silently — see s17 root-cause investigation 2026-05-10.
|
|
140
|
+
*/
|
|
141
|
+
interface OutcomePayload {
|
|
142
|
+
handle: string;
|
|
143
|
+
app_id?: string;
|
|
144
|
+
intent_archetype?: string;
|
|
145
|
+
/** The model that ACTUALLY RAN (post-fallback). */
|
|
146
|
+
model?: string;
|
|
147
|
+
/** The model v2 compile() originally targeted. NULL when no fallback. */
|
|
148
|
+
requested_model?: string;
|
|
149
|
+
provider?: string;
|
|
150
|
+
shape_key?: string;
|
|
151
|
+
learning_key?: string;
|
|
152
|
+
mutations_applied: string[];
|
|
153
|
+
tokens_in: number;
|
|
154
|
+
tokens_out: number;
|
|
155
|
+
estimated_tokens_in?: number;
|
|
156
|
+
latency_ms: number;
|
|
157
|
+
success: boolean;
|
|
158
|
+
empty_response: boolean;
|
|
159
|
+
error_type?: string;
|
|
160
|
+
tools_called?: string[];
|
|
161
|
+
oracle_score?: number;
|
|
162
|
+
oracle_dimensions?: Record<string, number>;
|
|
163
|
+
oracle_rationale?: string;
|
|
164
|
+
prompt_preview?: string;
|
|
165
|
+
response_preview?: string;
|
|
166
|
+
dialect_version: string;
|
|
167
|
+
cache_read_input_tokens?: number;
|
|
168
|
+
cache_creation_input_tokens?: number;
|
|
169
|
+
cost_usd_actual?: number;
|
|
170
|
+
ttft_ms?: number;
|
|
171
|
+
history_cacheable_tokens?: number;
|
|
172
|
+
history_tokens_at_compile?: number;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Oracle contract — how an app tells the brain whether a response was good.
|
|
177
|
+
*
|
|
178
|
+
* The brain learns from oracle scores. Apps provide an oracle function per
|
|
179
|
+
* intent (or one default). Without oracle scores the brain can still record
|
|
180
|
+
* structural metrics (tokens, latency, success) but mutations cannot fire —
|
|
181
|
+
* structural metrics aren't quality.
|
|
182
|
+
*
|
|
183
|
+
* Two oracle styles supported:
|
|
184
|
+
* 1. App-provided synchronous scorer: cheap, deterministic, app-specific.
|
|
185
|
+
* Examples: "did the SQL filter return non-empty?", "did the JSON parse?",
|
|
186
|
+
* "did the response cite a known source?".
|
|
187
|
+
* 2. LLM-as-judge async scorer: built into kgauto for cold-start use. Calls
|
|
188
|
+
* a frontier model with a comparison prompt and parses a score.
|
|
189
|
+
*/
|
|
190
|
+
|
|
191
|
+
interface OracleContext {
|
|
192
|
+
/** App identifier. */
|
|
193
|
+
appId: string;
|
|
194
|
+
/** App-local intent name. */
|
|
195
|
+
intentName: string;
|
|
196
|
+
/** Canonical archetype. */
|
|
197
|
+
archetype: string;
|
|
198
|
+
/** The original user turn text. */
|
|
199
|
+
userTurn?: string;
|
|
200
|
+
/** The model's text response. */
|
|
201
|
+
response: string;
|
|
202
|
+
/** Whether tool calls were involved. */
|
|
203
|
+
hadTools: boolean;
|
|
204
|
+
/** Free-form metadata the caller can attach. */
|
|
205
|
+
meta?: Record<string, unknown>;
|
|
206
|
+
}
|
|
207
|
+
/** App-provided oracle: pure function (sync or async). */
|
|
208
|
+
type AppOracle = (ctx: OracleContext) => OracleScore | Promise<OracleScore>;
|
|
209
|
+
interface LLMJudgeOptions {
|
|
210
|
+
/** Function that calls the judge model. Caller wires their own SDK. */
|
|
211
|
+
judgeCall: (prompt: string) => Promise<string>;
|
|
212
|
+
/**
|
|
213
|
+
* The dimensions to score. Default: 4-dimension rubric (correctness,
|
|
214
|
+
* completeness, conciseness, format).
|
|
215
|
+
*/
|
|
216
|
+
dimensions?: string[];
|
|
217
|
+
/** Hard cap on judge calls per minute (rate limiting). */
|
|
218
|
+
rateLimitPerMin?: number;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Build an LLM-as-judge oracle. Returns an AppOracle that asks the judge
|
|
222
|
+
* model to rate the response and parses a JSON score.
|
|
223
|
+
*/
|
|
224
|
+
declare function buildLLMJudge(opts: LLMJudgeOptions): AppOracle;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Tokenizer abstraction.
|
|
228
|
+
*
|
|
229
|
+
* Default: char-based fallback (~4 chars/token, ±20% accuracy).
|
|
230
|
+
* Recommended: caller wires js-tiktoken or a provider-specific tokenizer
|
|
231
|
+
* via setTokenizer(). The compiler always calls countTokens(); the rest of
|
|
232
|
+
* the system never touches the implementation directly.
|
|
233
|
+
*/
|
|
234
|
+
/**
|
|
235
|
+
* Wire a real tokenizer. Caller is responsible for picking one accurate
|
|
236
|
+
* enough for their dominant provider.
|
|
237
|
+
*/
|
|
238
|
+
declare function setTokenizer(impl: (text: string) => number): void;
|
|
239
|
+
/**
|
|
240
|
+
* Reset to the char-based fallback. Useful in tests.
|
|
241
|
+
*/
|
|
242
|
+
declare function resetTokenizer(): void;
|
|
243
|
+
/**
|
|
244
|
+
* Count tokens for a string. Wraps the configured implementation with
|
|
245
|
+
* safety (never throws, always returns ≥ 0 for non-empty input).
|
|
246
|
+
*/
|
|
247
|
+
declare function countTokens(text: string): number;
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Best-practice advisor — alpha.6 Phase 1.
|
|
251
|
+
*
|
|
252
|
+
* Inspects an IR + the selected profile + compile diagnostics and emits a
|
|
253
|
+
* list of `BestPracticeAdvisory` entries describing detected gaps. Runs
|
|
254
|
+
* after `lower()` in the compile pipeline; the result lands on
|
|
255
|
+
* `CompileResult.advisories` for the consumer to log, surface, or filter.
|
|
256
|
+
*
|
|
257
|
+
* Driven by interfaces/kgauto.md `best-practice-advisories` (IC, 2026-05-07).
|
|
258
|
+
* Phase 1 ships 4 starter rules sourced from the s14 kgauto comment +
|
|
259
|
+
* s15 empirical seed of brain anti-patterns:
|
|
260
|
+
*
|
|
261
|
+
* 1. `caching-off-on-claude` system >2000 chars on Anthropic, no cacheable=true
|
|
262
|
+
* 2. `single-chunk-system` Anthropic, only one PromptSection >1000 chars
|
|
263
|
+
* 3. `tool-bloat` >10 tools on a short-output archetype
|
|
264
|
+
* 4. `history-uncached-on-claude` Anthropic, ≥2 history messages, no historyCachePolicy
|
|
265
|
+
*
|
|
266
|
+
* Each rule is a pure function: (ir, result, profile) → BestPracticeAdvisory[].
|
|
267
|
+
* No side effects. No randomness. Deterministic for a given IR.
|
|
268
|
+
*
|
|
269
|
+
* The thresholds (2000 chars, 1000 chars, 10 tools, 2 history) are chosen
|
|
270
|
+
* to balance noise vs. signal — too low fires on innocuous calls, too high
|
|
271
|
+
* misses real waste. They may tune with brain evidence over time; for now
|
|
272
|
+
* they're literals in the rule bodies. Make them configurable when the
|
|
273
|
+
* cost-watcher's R-rules graduate to here.
|
|
274
|
+
*/
|
|
275
|
+
|
|
276
|
+
/** Subset of CompileResult fields the advisor needs. */
|
|
277
|
+
type AdvisorContext = Pick<CompileResult, 'target' | 'provider' | 'tokensIn' | 'diagnostics'>;
|
|
278
|
+
/**
|
|
279
|
+
* Run all phased rules and return collected advisories. Order is fixed so
|
|
280
|
+
* output is stable across runs. The `policy` argument is alpha.9 — the
|
|
281
|
+
* `single-model-array` rule needs to know whether the consumer explicitly
|
|
282
|
+
* declared `posture: 'locked'` (in which case single-model is intentional
|
|
283
|
+
* and shouldn't warn).
|
|
284
|
+
*/
|
|
285
|
+
declare function runAdvisor(ir: PromptIR, result: AdvisorContext, profile: ModelProfile, policy?: CompilePolicy): BestPracticeAdvisory[];
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* env.ts — provider env-key resolution + reachability predicates.
|
|
289
|
+
*
|
|
290
|
+
* Centralizes the per-provider env var names that kgauto checks for
|
|
291
|
+
* reachability. Used by:
|
|
292
|
+
*
|
|
293
|
+
* - execute.ts — to find an API key when one isn't passed via apiKeys
|
|
294
|
+
* - call.ts — to auto-filter unreachable models from the fallback walk
|
|
295
|
+
* - getDefaultFallbackChain — opt-in chain filter when consumer passes `reachability`
|
|
296
|
+
* - operator scripts — getReachabilityDiagnostic() prints what's wired up
|
|
297
|
+
*
|
|
298
|
+
* Keeping the map in ONE place means execute() and the reachability check
|
|
299
|
+
* always agree. Without this, kgauto could declare a model "reachable" because
|
|
300
|
+
* env.ts found GOOGLE_GENERATIVE_AI_API_KEY, while execute() looks at
|
|
301
|
+
* GOOGLE_API_KEY only and 401s — shipping the bug we're trying to fix.
|
|
302
|
+
*
|
|
303
|
+
* Resolution order (apiKeys takes precedence):
|
|
304
|
+
* 1. opts.apiKeys?.[provider]
|
|
305
|
+
* 2. process.env[name] for each name in PROVIDER_ENV_KEYS[provider] (first-present wins)
|
|
306
|
+
*
|
|
307
|
+
* alpha.10 (2026-05-14). Resolves the auto-filter-unreachable-models-silently
|
|
308
|
+
* request from PB after first-deploy 401 on the alpha.9 summarize chain.
|
|
309
|
+
*/
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Providers kgauto can resolve keys for today. Subset of `Provider` — `mistral`
|
|
313
|
+
* and `xai` are declared in the type union but not yet executable (no profiles,
|
|
314
|
+
* no execute() handler, no env-var convention). Narrowing here keeps the
|
|
315
|
+
* reachability check structurally honest.
|
|
316
|
+
*/
|
|
317
|
+
type SupportedProvider = 'anthropic' | 'google' | 'openai' | 'deepseek';
|
|
318
|
+
/**
|
|
319
|
+
* Per-provider env var names kgauto recognizes. Order doesn't matter —
|
|
320
|
+
* first-present wins. Multiple names per provider supported because Google
|
|
321
|
+
* has historical drift (`GOOGLE_API_KEY` from older Google Cloud SDKs,
|
|
322
|
+
* `GEMINI_API_KEY` in many examples, `GOOGLE_GENERATIVE_AI_API_KEY` is the
|
|
323
|
+
* Vercel AI SDK convention used by IC + tt-intel adapters).
|
|
324
|
+
*
|
|
325
|
+
* Frozen so consumers/tests can't mutate (would break the cache invariant
|
|
326
|
+
* that execute() and reachability checks agree).
|
|
327
|
+
*/
|
|
328
|
+
declare const PROVIDER_ENV_KEYS: Readonly<Record<SupportedProvider, readonly string[]>>;
|
|
329
|
+
interface ReachabilityOpts {
|
|
330
|
+
/** Explicit keys (alpha.3 ApiKeys). Checked first; takes precedence over env. */
|
|
331
|
+
apiKeys?: ApiKeys;
|
|
332
|
+
/**
|
|
333
|
+
* Override env source. Defaults to `process.env` in Node-shaped runtimes,
|
|
334
|
+
* `{}` everywhere else. Pass `{}` explicitly in tests for hermetic runs.
|
|
335
|
+
*/
|
|
336
|
+
envSource?: Record<string, string | undefined>;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Resolve a usable API key for the provider. Returns the key string, or
|
|
340
|
+
* undefined if neither apiKeys nor any of the env names are set.
|
|
341
|
+
*
|
|
342
|
+
* Used internally by execute.ts so the reachability check and the actual
|
|
343
|
+
* call check stay in sync.
|
|
344
|
+
*/
|
|
345
|
+
declare function resolveProviderKey(provider: Provider, opts?: ReachabilityOpts): string | undefined;
|
|
346
|
+
/**
|
|
347
|
+
* True iff the provider has a usable key — either via explicit `apiKeys`
|
|
348
|
+
* or one of the `PROVIDER_ENV_KEYS[provider]` names is set in envSource.
|
|
349
|
+
*/
|
|
350
|
+
declare function isProviderReachable(provider: Provider, opts?: ReachabilityOpts): boolean;
|
|
351
|
+
/**
|
|
352
|
+
* True iff the model's profile exists AND its provider is reachable.
|
|
353
|
+
* Unknown model id returns false (treat as unreachable; the chain walker
|
|
354
|
+
* will surface "no reachable models" if everything filters out).
|
|
355
|
+
*/
|
|
356
|
+
declare function isModelReachable(modelId: string, opts?: ReachabilityOpts): boolean;
|
|
357
|
+
interface ProviderReachability {
|
|
358
|
+
reachable: boolean;
|
|
359
|
+
/** How the key was found. `null` when unreachable. */
|
|
360
|
+
via: 'apiKeys' | 'env' | null;
|
|
361
|
+
/** Which env var name supplied the key (only when via === 'env'). */
|
|
362
|
+
envKeyFound?: string;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Snapshot of which providers are reachable from the current env / apiKeys.
|
|
366
|
+
* Useful for operator scripts ("kgauto diagnose"), startup-time logging,
|
|
367
|
+
* and the cost-watcher's "which consumer is missing what" report.
|
|
368
|
+
*
|
|
369
|
+
* Does NOT log the key value itself — only the env var name that supplied it.
|
|
370
|
+
*/
|
|
371
|
+
declare function getReachabilityDiagnostic(opts?: ReachabilityOpts): Record<SupportedProvider, ProviderReachability>;
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* getDefaultFallbackChain — the alpha.9 cascading ship.
|
|
375
|
+
*
|
|
376
|
+
* Returns a per-archetype fallback chain that walks the cost/performance
|
|
377
|
+
* Pareto frontier (master plan §1.3 + §3). Three customer postures:
|
|
378
|
+
*
|
|
379
|
+
* locked — caller passes [theOneModel]; never call this function
|
|
380
|
+
* preferred — caller passes `primary`; chain returned is [primary, ...fallbacks]
|
|
381
|
+
* open — caller passes no `primary`; chain returned is [best, ...fallbacks]
|
|
382
|
+
*
|
|
383
|
+
* The chain at each step:
|
|
384
|
+
* 1. Costs strictly less than the previous (no expensive sideways moves)
|
|
385
|
+
* 2. Comes from a different provider than the previous step where possible
|
|
386
|
+
* (correlated outages don't kill consecutive attempts)
|
|
387
|
+
* 3. Stays above the archetype's perf floor (skip models scored <baseline
|
|
388
|
+
* for archetypes where degradation would be unacceptable)
|
|
389
|
+
*
|
|
390
|
+
* In alpha.9 the chain is **hand-curated** per archetype (§3.3 starter
|
|
391
|
+
* table). Brain-query mode lands in alpha.10. Policy.blockedModels filters
|
|
392
|
+
* the result; policy.maxCostPerCallUsd is NOT applied here because the
|
|
393
|
+
* function doesn't see the IR's token counts — that filtering happens at
|
|
394
|
+
* `passScoreTargets()` time inside compile().
|
|
395
|
+
*
|
|
396
|
+
* The function is **pure** — no brain query, no I/O, no randomness. Same
|
|
397
|
+
* inputs always produce the same chain.
|
|
398
|
+
*/
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Posture passed into `getDefaultFallbackChain`. The chain function only
|
|
402
|
+
* sees `'open'` and `'preferred'` — callers in `'locked'` posture should
|
|
403
|
+
* pass `models: [theOneModel]` directly and skip this function entirely.
|
|
404
|
+
*
|
|
405
|
+
* Equivalent to `CompilePolicy.posture` minus `'locked'`. Kept distinct so
|
|
406
|
+
* the type system enforces "don't ask for a chain when you don't want one."
|
|
407
|
+
*/
|
|
408
|
+
type FallbackPosture = 'open' | 'preferred';
|
|
409
|
+
interface GetDefaultFallbackChainOpts {
|
|
410
|
+
/** The archetype the call is performing. Drives chain shape. */
|
|
411
|
+
archetype: IntentArchetypeName;
|
|
412
|
+
/**
|
|
413
|
+
* The user-selected or caller-anchored primary model. When provided, it
|
|
414
|
+
* appears at position 0 of the returned chain and fallbacks follow.
|
|
415
|
+
* When omitted, the function picks the best-perf model for the archetype
|
|
416
|
+
* as position 0 (open posture).
|
|
417
|
+
*/
|
|
418
|
+
primary?: string;
|
|
419
|
+
/**
|
|
420
|
+
* Informational. `'preferred'` and `'open'` produce the same chain shape
|
|
421
|
+
* given the same `primary`/no-primary input — posture is a tag the brain
|
|
422
|
+
* uses to distinguish "user-anchored" from "library-anchored" telemetry.
|
|
423
|
+
*/
|
|
424
|
+
posture?: FallbackPosture;
|
|
425
|
+
/**
|
|
426
|
+
* Cap on chain length. Default 3. Min 1. Useful when the consumer wants
|
|
427
|
+
* to keep the worst-case latency low (each fallback adds a round-trip).
|
|
428
|
+
*/
|
|
429
|
+
maxDepth?: number;
|
|
430
|
+
/**
|
|
431
|
+
* Consumer-side gating. `blockedModels` are filtered from the chain.
|
|
432
|
+
* `preferredModels` is informational (no boost applied at this layer —
|
|
433
|
+
* compile()'s `passScoreTargets` handles preference ranking).
|
|
434
|
+
* `maxCostPerCallUsd` is NOT applied here — needs IR-level token
|
|
435
|
+
* estimation. Use compile()'s policy plumbing instead.
|
|
436
|
+
*/
|
|
437
|
+
policy?: CompilePolicy;
|
|
438
|
+
/**
|
|
439
|
+
* alpha.10. When provided, the chain is filtered to models whose provider
|
|
440
|
+
* has a reachable API key (via `apiKeys` or one of `PROVIDER_ENV_KEYS[provider]`).
|
|
441
|
+
* Models whose provider can't be reached are silently dropped. If filtering
|
|
442
|
+
* leaves the chain empty, returns `[]` — caller decides what to do (call()
|
|
443
|
+
* throws CallError; this function stays pure).
|
|
444
|
+
*
|
|
445
|
+
* Pass `{}` to opt in with `process.env` as the env source. Pass `{ apiKeys, envSource }`
|
|
446
|
+
* for explicit control (tests, non-Node runtimes). Omit entirely for the
|
|
447
|
+
* legacy unfiltered behavior — preserves alpha.9 callers byte-for-byte.
|
|
448
|
+
*/
|
|
449
|
+
reachability?: ReachabilityOpts;
|
|
450
|
+
}
|
|
451
|
+
declare function getDefaultFallbackChain(opts: GetDefaultFallbackChainOpts): string[];
|
|
452
|
+
/**
|
|
453
|
+
* Returns a shallow copy of the hand-curated starter chain for an archetype.
|
|
454
|
+
* Useful for tests + the `scripts/digest.mjs` operator readout.
|
|
455
|
+
*/
|
|
456
|
+
declare function getStarterChain(archetype: IntentArchetypeName): string[];
|
|
457
|
+
/**
|
|
458
|
+
* Returns a shallow copy of all starter chains keyed by archetype.
|
|
459
|
+
* Useful for the `digest.mjs` readout and consumer audits.
|
|
460
|
+
*/
|
|
461
|
+
declare function getAllStarterChains(): Record<IntentArchetypeName, string[]>;
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* @warmdrift/kgauto v2 — prompt compiler + central learning brain.
|
|
465
|
+
*
|
|
466
|
+
* Two functions you'll use most:
|
|
467
|
+
* compile(ir, opts?) → CompileResult
|
|
468
|
+
* record(input) → Promise<void>
|
|
469
|
+
*
|
|
470
|
+
* Plus:
|
|
471
|
+
* configureBrain({ endpoint, apiKey }) — point at the central brain
|
|
472
|
+
* buildLLMJudge({ judgeCall }) — cold-start oracle
|
|
473
|
+
*
|
|
474
|
+
* Quickstart:
|
|
475
|
+
*
|
|
476
|
+
* import { compile, record, configureBrain } from '@warmdrift/kgauto';
|
|
477
|
+
*
|
|
478
|
+
* configureBrain({ endpoint: 'https://kgauto-brain.vercel.app/api', apiKey: '...' });
|
|
479
|
+
*
|
|
480
|
+
* const r = compile({
|
|
481
|
+
* appId: 'my-app',
|
|
482
|
+
* intent: { name: 'search', archetype: 'ask' },
|
|
483
|
+
* sections: [
|
|
484
|
+
* { id: 'role', text: 'You are an assistant.', cacheable: true },
|
|
485
|
+
* { id: 'task', text: userQuestion },
|
|
486
|
+
* ],
|
|
487
|
+
* models: ['claude-sonnet-4-6', 'gemini-2.5-flash'],
|
|
488
|
+
* });
|
|
489
|
+
*
|
|
490
|
+
* const start = Date.now();
|
|
491
|
+
* const response = await callProvider(r.target, r.request);
|
|
492
|
+
*
|
|
493
|
+
* await record({
|
|
494
|
+
* handle: r.handle,
|
|
495
|
+
* tokensIn: response.usage.input,
|
|
496
|
+
* tokensOut: response.usage.output,
|
|
497
|
+
* latencyMs: Date.now() - start,
|
|
498
|
+
* success: true,
|
|
499
|
+
* oracleScore: { score: 0.85 },
|
|
500
|
+
* });
|
|
501
|
+
*/
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Compile a request. Wraps the pure compile() with brain registration so
|
|
505
|
+
* record() can find call metadata later.
|
|
506
|
+
*/
|
|
507
|
+
declare function compile(ir: PromptIR, opts?: CompileOptions): CompileResult;
|
|
508
|
+
|
|
509
|
+
export { ApiKeys, type AppOracle, BestPracticeAdvisory, type BrainConfig, CallOptions, CallResult, type CompileOptions, CompilePolicy, CompileResult, CompiledRequest, type ExecuteErr, type ExecuteOk, type ExecuteOptions, type ExecuteResult, type FallbackPosture, type GetDefaultFallbackChainOpts, IntentArchetypeName, type LLMJudgeOptions, ModelProfile, NormalizedResponse, type OracleContext, OracleScore, type OutcomePayload, PROVIDER_ENV_KEYS, PromptIR, Provider, ProviderOverrides, type ProviderReachability, type ReachabilityOpts, RecordInput, type SupportedProvider, buildLLMJudge, call, clearBrain, compile, configureBrain, countTokens, execute, getAllStarterChains, getDefaultFallbackChain, getReachabilityDiagnostic, getStarterChain, isModelReachable, isProviderReachable, record, resetTokenizer, resolveProviderKey, runAdvisor, setTokenizer };
|