@warmdrift/kgauto-compiler 2.0.0-alpha.3

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.
@@ -0,0 +1,238 @@
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 } from './profiles-C5lVqF8_.js';
2
+ export { f as ALIASES, g as CacheStrategy, h as CallAttempt, i as CallError, j as CliffRule, k as Constraints, I as IntentDeclaration, L as LoweringSpec, l as Message, m as MutationApplied, n as NormalizedTokens, o as PromptSection, p as Provider, 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-C5lVqF8_.js';
3
+ export { ALL_ARCHETYPES, ContextBucket, DIALECT_VERSION, HistoryDepth, INTENT_ARCHETYPES, IntentArchetypeName, OutputMode, ShapeSignature, ToolCountBucket, bucketContext, bucketHistory, bucketToolCount, hashShape, isArchetype, learningKey } from './dialect.js';
4
+
5
+ /**
6
+ * compile() — the main orchestrator.
7
+ *
8
+ * Runs all passes in order, picks a target, lowers to wire format, returns
9
+ * a CompileResult the caller uses to make the actual provider call.
10
+ *
11
+ * Pure function in v2.0.0-alpha.1: no network, no I/O, no mutation cache yet.
12
+ * Mutation cache will be added in v2.1 when the brain has data to push.
13
+ */
14
+
15
+ interface CompileOptions {
16
+ /** Custom profile resolver — for tests or custom profile sets. */
17
+ profileResolver?: (id: string) => ModelProfile;
18
+ /** Tool relevance threshold (default 0.2). */
19
+ toolRelevanceThreshold?: number;
20
+ /** History compression — turns count threshold (default 8). */
21
+ compressHistoryAfter?: number;
22
+ /**
23
+ * Consumer-declared policy. Filters blocked models, enforces cost
24
+ * ceiling, boosts preferred. See CompilePolicy in ir.ts.
25
+ */
26
+ policy?: CompilePolicy;
27
+ }
28
+
29
+ /**
30
+ * execute() — fire a CompiledRequest at the right provider, normalize the
31
+ * response shape across providers.
32
+ *
33
+ * This is what call() composes with compile() to own the network round-trip.
34
+ * Pure-fetch consumers stop reinventing per-provider parsing.
35
+ *
36
+ * Error classification follows L-061: 429/5xx/timeout/model_not_found are
37
+ * retryable; auth/validation are terminal. The orchestrator (call.ts) walks
38
+ * fallbackChain on retryable; surfaces terminal immediately.
39
+ */
40
+
41
+ interface ExecuteOptions {
42
+ apiKeys?: ApiKeys;
43
+ fetchImpl?: typeof fetch;
44
+ providerOverrides?: ProviderOverrides;
45
+ }
46
+ interface ExecuteOk {
47
+ ok: true;
48
+ status: number;
49
+ response: NormalizedResponse;
50
+ }
51
+ interface ExecuteErr {
52
+ ok: false;
53
+ status: number;
54
+ errorType: 'retryable' | 'terminal';
55
+ errorCode: string;
56
+ message: string;
57
+ raw: unknown;
58
+ }
59
+ type ExecuteResult = ExecuteOk | ExecuteErr;
60
+ declare function execute(request: CompiledRequest, opts?: ExecuteOptions): Promise<ExecuteResult>;
61
+
62
+ /**
63
+ * call() — the high-level orchestrator that owns the network round-trip.
64
+ *
65
+ * call(ir) ≈ compile(ir) → execute(request) → normalize(response) → record(...)
66
+ *
67
+ * Plain-fetch consumers (playbacksam, future incantato/glass-box/ivypass) use
68
+ * call() instead of importing fetch and reinventing per-provider response
69
+ * parsing. AI-SDK consumers can keep using compile() unchanged — call() is a
70
+ * sibling, not a replacement.
71
+ *
72
+ * Retry/fallback: on a retryable error (429, 5xx, 404, network), walk
73
+ * CompileResult.fallbackChain by re-running compile() with forceModel set on
74
+ * each fallback target. Each fallback gets fresh cliffs + fresh lowering.
75
+ * Hard cap at fallbackChain.length + 1 attempts. Terminal errors short-circuit.
76
+ */
77
+
78
+ /**
79
+ * Compile, execute, normalize, record. Returns a CallResult once a provider
80
+ * actually serves the request. Throws CallError if the fallback chain is
81
+ * exhausted without success.
82
+ */
83
+ declare function call(ir: PromptIR, opts?: CallOptions): Promise<CallResult>;
84
+
85
+ /**
86
+ * Brain client — fire-and-forget telemetry to the central kgauto Supabase.
87
+ *
88
+ * The brain is the centralized learning store. Apps POST outcomes here;
89
+ * mutations flow back through a separate pull (in v2.1).
90
+ *
91
+ * Design: never blocks the caller. Failures are silent (logged via optional
92
+ * onError hook). Uses fetch() — works in Node 18+, Edge runtimes, and browsers.
93
+ */
94
+
95
+ interface BrainConfig {
96
+ /** Brain HTTP endpoint base URL (e.g., https://kgauto-brain.vercel.app/api). */
97
+ endpoint: string;
98
+ /** Bearer token for auth. */
99
+ apiKey?: string;
100
+ /** Optional error hook for debugging. Defaults to console.warn. */
101
+ onError?: (err: unknown) => void;
102
+ /** If true, records are sent synchronously and awaited (test mode). */
103
+ sync?: boolean;
104
+ /** Optional fetch override (for tests). */
105
+ fetchImpl?: typeof fetch;
106
+ }
107
+ declare function configureBrain(config: BrainConfig): void;
108
+ declare function clearBrain(): void;
109
+ /**
110
+ * Record the outcome of a compiled call. Fire-and-forget by default.
111
+ *
112
+ * Returns a Promise so callers in `sync` mode can await; in async mode the
113
+ * promise resolves immediately (after the request is queued) and any
114
+ * network error is swallowed/forwarded to onError.
115
+ */
116
+ declare function record(input: RecordInput): Promise<void>;
117
+
118
+ /**
119
+ * Oracle contract — how an app tells the brain whether a response was good.
120
+ *
121
+ * The brain learns from oracle scores. Apps provide an oracle function per
122
+ * intent (or one default). Without oracle scores the brain can still record
123
+ * structural metrics (tokens, latency, success) but mutations cannot fire —
124
+ * structural metrics aren't quality.
125
+ *
126
+ * Two oracle styles supported:
127
+ * 1. App-provided synchronous scorer: cheap, deterministic, app-specific.
128
+ * Examples: "did the SQL filter return non-empty?", "did the JSON parse?",
129
+ * "did the response cite a known source?".
130
+ * 2. LLM-as-judge async scorer: built into kgauto for cold-start use. Calls
131
+ * a frontier model with a comparison prompt and parses a score.
132
+ */
133
+
134
+ interface OracleContext {
135
+ /** App identifier. */
136
+ appId: string;
137
+ /** App-local intent name. */
138
+ intentName: string;
139
+ /** Canonical archetype. */
140
+ archetype: string;
141
+ /** The original user turn text. */
142
+ userTurn?: string;
143
+ /** The model's text response. */
144
+ response: string;
145
+ /** Whether tool calls were involved. */
146
+ hadTools: boolean;
147
+ /** Free-form metadata the caller can attach. */
148
+ meta?: Record<string, unknown>;
149
+ }
150
+ /** App-provided oracle: pure function (sync or async). */
151
+ type AppOracle = (ctx: OracleContext) => OracleScore | Promise<OracleScore>;
152
+ interface LLMJudgeOptions {
153
+ /** Function that calls the judge model. Caller wires their own SDK. */
154
+ judgeCall: (prompt: string) => Promise<string>;
155
+ /**
156
+ * The dimensions to score. Default: 4-dimension rubric (correctness,
157
+ * completeness, conciseness, format).
158
+ */
159
+ dimensions?: string[];
160
+ /** Hard cap on judge calls per minute (rate limiting). */
161
+ rateLimitPerMin?: number;
162
+ }
163
+ /**
164
+ * Build an LLM-as-judge oracle. Returns an AppOracle that asks the judge
165
+ * model to rate the response and parses a JSON score.
166
+ */
167
+ declare function buildLLMJudge(opts: LLMJudgeOptions): AppOracle;
168
+
169
+ /**
170
+ * Tokenizer abstraction.
171
+ *
172
+ * Default: char-based fallback (~4 chars/token, ±20% accuracy).
173
+ * Recommended: caller wires js-tiktoken or a provider-specific tokenizer
174
+ * via setTokenizer(). The compiler always calls countTokens(); the rest of
175
+ * the system never touches the implementation directly.
176
+ */
177
+ /**
178
+ * Wire a real tokenizer. Caller is responsible for picking one accurate
179
+ * enough for their dominant provider.
180
+ */
181
+ declare function setTokenizer(impl: (text: string) => number): void;
182
+ /**
183
+ * Reset to the char-based fallback. Useful in tests.
184
+ */
185
+ declare function resetTokenizer(): void;
186
+ /**
187
+ * Count tokens for a string. Wraps the configured implementation with
188
+ * safety (never throws, always returns ≥ 0 for non-empty input).
189
+ */
190
+ declare function countTokens(text: string): number;
191
+
192
+ /**
193
+ * @warmdrift/kgauto v2 — prompt compiler + central learning brain.
194
+ *
195
+ * Two functions you'll use most:
196
+ * compile(ir, opts?) → CompileResult
197
+ * record(input) → Promise<void>
198
+ *
199
+ * Plus:
200
+ * configureBrain({ endpoint, apiKey }) — point at the central brain
201
+ * buildLLMJudge({ judgeCall }) — cold-start oracle
202
+ *
203
+ * Quickstart:
204
+ *
205
+ * import { compile, record, configureBrain } from '@warmdrift/kgauto';
206
+ *
207
+ * configureBrain({ endpoint: 'https://kgauto-brain.vercel.app/api', apiKey: '...' });
208
+ *
209
+ * const r = compile({
210
+ * appId: 'my-app',
211
+ * intent: { name: 'search', archetype: 'ask' },
212
+ * sections: [
213
+ * { id: 'role', text: 'You are an assistant.', cacheable: true },
214
+ * { id: 'task', text: userQuestion },
215
+ * ],
216
+ * models: ['claude-sonnet-4-6', 'gemini-2.5-flash'],
217
+ * });
218
+ *
219
+ * const start = Date.now();
220
+ * const response = await callProvider(r.target, r.request);
221
+ *
222
+ * await record({
223
+ * handle: r.handle,
224
+ * tokensIn: response.usage.input,
225
+ * tokensOut: response.usage.output,
226
+ * latencyMs: Date.now() - start,
227
+ * success: true,
228
+ * oracleScore: { score: 0.85 },
229
+ * });
230
+ */
231
+
232
+ /**
233
+ * Compile a request. Wraps the pure compile() with brain registration so
234
+ * record() can find call metadata later.
235
+ */
236
+ declare function compile(ir: PromptIR, opts?: CompileOptions): CompileResult;
237
+
238
+ export { ApiKeys, type AppOracle, type BrainConfig, CallOptions, CallResult, type CompileOptions, CompilePolicy, CompileResult, CompiledRequest, type ExecuteErr, type ExecuteOk, type ExecuteOptions, type ExecuteResult, type LLMJudgeOptions, ModelProfile, NormalizedResponse, type OracleContext, OracleScore, PromptIR, ProviderOverrides, RecordInput, buildLLMJudge, call, clearBrain, compile, configureBrain, countTokens, execute, record, resetTokenizer, setTokenizer };