@warmdrift/kgauto-compiler 2.0.0-alpha.16 → 2.0.0-alpha.18

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.
@@ -1,2 +1,137 @@
1
- export { g as ALIASES, h as CacheStrategy, k as CliffRule, L as LoweringSpec, M as ModelProfile, q as RecoveryRule, S as StructuredOutputCapability, r as SystemPromptMode, _ as _setProfileBrainHook, t as allProfiles, x as allProfilesRaw, u as getProfile, v as profilesByProvider, w as tryGetProfile } from './profiles-CVB2_5C8.js';
2
- import './dialect.js';
1
+ import { f as Provider } from './ir-CFHU3BUT.js';
2
+ import { IntentArchetypeName } from './dialect.js';
3
+
4
+ /**
5
+ * Model profiles — executable knowledge about each provider/model.
6
+ *
7
+ * Unlike v1 which carried `known_failures` as prose strings, v2 makes them
8
+ * executable: cliffs trigger guards, lowering describes the wire format,
9
+ * recovery handlers describe what to do after specific failures.
10
+ *
11
+ * Each profile is the answer to "if I want to call THIS model with THIS
12
+ * shape of work, what does it need from me, and what should I do when it
13
+ * fails?"
14
+ */
15
+
16
+ type StructuredOutputCapability = 'native' | 'grammar' | 'none';
17
+ type SystemPromptMode = 'inline' | 'separate' | 'as_developer' | 'unsupported';
18
+ type CacheStrategy = 'cache_control' | 'cachedContent' | 'unsupported';
19
+ interface CliffRule {
20
+ /** What metric triggers this cliff. */
21
+ metric: 'input_tokens' | 'tool_count' | 'history_turns' | 'thinking_with_short_output';
22
+ /** Threshold — meaning depends on metric. */
23
+ threshold: number;
24
+ /** What action to take when triggered. */
25
+ action: 'downgrade_quality_warning' | 'drop_to_top_relevant' | 'force_thinking_budget_zero' | 'force_terse_output' | 'escalate_target' | 'strip_tools';
26
+ /**
27
+ * Optional: only fire this cliff when the IR's intent.archetype matches.
28
+ * Used for archetype-specific failure modes (e.g. Gemini Flash returns
29
+ * empty when summarize is offered tools).
30
+ */
31
+ whenIntent?: IntentArchetypeName;
32
+ /** Human-readable reason for digest reporting. */
33
+ reason: string;
34
+ }
35
+ interface RecoveryRule {
36
+ /** What signal triggers recovery. */
37
+ signal: 'empty_response_after_tool' | 'empty_response' | 'malformed_function_call' | 'rate_limit' | 'model_not_found' | 'context_overflow';
38
+ /** Action: retry with adjusted params, or escalate to next fallback. */
39
+ action: 'retry_with_params' | 'escalate' | 'log_only';
40
+ /** When action=retry_with_params, the param adjustments to apply. */
41
+ retryParams?: Record<string, unknown>;
42
+ /** Max retries with this rule. */
43
+ maxRetries?: number;
44
+ /** Human-readable reason for digest reporting. */
45
+ reason: string;
46
+ }
47
+ interface LoweringSpec {
48
+ /** Where the system prompt goes. */
49
+ system: {
50
+ mode: SystemPromptMode;
51
+ field?: string;
52
+ };
53
+ /** Cache strategy + parameters. */
54
+ cache: {
55
+ strategy: CacheStrategy;
56
+ /** Min tokens before caching is worth it (provider rules). */
57
+ minTokens?: number;
58
+ /** Discount factor on cached input (0.1 = 10% of normal price). */
59
+ discount?: number;
60
+ /** TTL hint in seconds. */
61
+ ttlSeconds?: number;
62
+ };
63
+ /** Tool format identifier — see lower.ts for supported formats. */
64
+ tools?: {
65
+ format: 'anthropic' | 'google' | 'openai' | 'deepseek';
66
+ };
67
+ /** Thinking config — present iff this model has a thinking knob. */
68
+ thinking?: {
69
+ /** Field path on the request. */
70
+ field: string;
71
+ /** Default value when caller hasn't specified. */
72
+ default?: number | 'auto' | 'off';
73
+ };
74
+ }
75
+ interface ModelProfile {
76
+ id: string;
77
+ provider: Provider;
78
+ status: 'current' | 'preview' | 'legacy';
79
+ maxContextTokens: number;
80
+ maxOutputTokens: number;
81
+ maxTools: number;
82
+ parallelToolCalls: boolean;
83
+ structuredOutput: StructuredOutputCapability;
84
+ systemPromptMode: SystemPromptMode;
85
+ streaming: boolean;
86
+ cliffs: CliffRule[];
87
+ costInputPer1m: number;
88
+ costOutputPer1m: number;
89
+ lowering: LoweringSpec;
90
+ recovery: RecoveryRule[];
91
+ strengths: string[];
92
+ weaknesses: string[];
93
+ notes?: string;
94
+ verifiedAgainstDocs?: string;
95
+ /**
96
+ * Hand-curated per-archetype performance score on a 0-10 scale.
97
+ *
98
+ * 10 = frontier on this archetype (e.g. Opus 4.7 on critique)
99
+ * 8 = strong second tier (Sonnet on plan, Pro on extract)
100
+ * 7 = competent (Haiku on classify, Flash on hunt)
101
+ * 5 = acceptable for tolerant archetypes (Flash-Lite on classify)
102
+ * 3 = degraded (Flash on critique, DeepSeek on hunt)
103
+ *
104
+ * Missing archetypes default to `5` (no data, neutral). Each non-default
105
+ * value should carry a one-line rationale in the profile's note or inline
106
+ * comment citing brain evidence, family prior, or "starter hypothesis —
107
+ * verify with telemetry."
108
+ *
109
+ * Source today: hand-curated from master plan §3.3 + §6.2 starter tables.
110
+ * Source tomorrow (alpha.10+): brain `archetype_model_evidence` view.
111
+ *
112
+ * Anti-hallucination guardrail (master plan §2.5): when the watcher's
113
+ * `--audit-fields` flag flags a profile stale (>90 days since
114
+ * verifiedAgainstDocs), the archetypePerf values get re-audited
115
+ * alongside capability fields. AI-trained intuition is NOT a valid
116
+ * source — only docs or brain evidence.
117
+ *
118
+ * alpha.9.
119
+ */
120
+ archetypePerf?: Partial<Record<IntentArchetypeName, number>>;
121
+ }
122
+ declare const ALIASES: Record<string, string>;
123
+ interface ProfileBrainHook {
124
+ getProfile?: (canonicalId: string) => ModelProfile | undefined;
125
+ resolveAlias?: (id: string) => string | undefined;
126
+ }
127
+ /** @internal — called by models-brain.ts at module load. */
128
+ declare function _setProfileBrainHook(hook: ProfileBrainHook): void;
129
+ declare function getProfile(id: string): ModelProfile;
130
+ declare function tryGetProfile(id: string): ModelProfile | undefined;
131
+ declare function allProfiles(): readonly ModelProfile[];
132
+ /** @internal — bundled-only access for adapters that need a non-brain
133
+ * fallback baseline (avoids a brain → profiles → brain re-entry). */
134
+ declare function allProfilesRaw(): readonly ModelProfile[];
135
+ declare function profilesByProvider(provider: Provider): readonly ModelProfile[];
136
+
137
+ export { ALIASES, type CacheStrategy, type CliffRule, type LoweringSpec, type ModelProfile, type RecoveryRule, type StructuredOutputCapability, type SystemPromptMode, _setProfileBrainHook, allProfiles, allProfilesRaw, getProfile, profilesByProvider, tryGetProfile };
@@ -0,0 +1,122 @@
1
+ import { j as MutationApplied, B as BestPracticeAdvisory, F as FallbackReason, g as CallAttempt } from './ir-C3P4gDt0.mjs';
2
+
3
+ /**
4
+ * Glass-Box observability types (alpha.17).
5
+ *
6
+ * The substrate for kgauto's in-flight observability surface — a Chrome MV3
7
+ * side panel that renders compile + execute + advisor outputs in real-time.
8
+ * See design doc:
9
+ * ~/.gstack/projects/stue-kgauto/stgreen-claude-serene-williamson-51a105-design-20260518-175356.md
10
+ *
11
+ * Wire shape is intentionally minimal:
12
+ * { kind, at, data } — discriminated union by `kind`.
13
+ *
14
+ * Critical-path safety (L-086 derived): emit MUST NEVER fail the user's
15
+ * `call()` invocation. emit.ts wraps every adapter write in try/catch +
16
+ * silently drops on error. Consumers in Vercel Edge runtimes can lose one
17
+ * event without losing the response.
18
+ */
19
+
20
+ /**
21
+ * Discriminator. Six event kinds cover the v1 substrate. New kinds added
22
+ * additively; the side-panel renderer treats unknown kinds as "ignore".
23
+ */
24
+ type GlassboxEventKind = 'compile.start' | 'compile.done' | 'execute.attempt' | 'execute.success' | 'advisory.fired' | 'fallback.walked';
25
+ /**
26
+ * Wire envelope. Every emit lands as one of these.
27
+ *
28
+ * - kind: discriminator
29
+ * - at: unix milliseconds (Date.now())
30
+ * - data: kind-specific payload
31
+ *
32
+ * `data` typed loosely as Record<string, unknown> for serialization-friendliness;
33
+ * the per-kind builders below populate it with the structured shape expected
34
+ * by the renderer. We deliberately do NOT typescript-narrow `data` by kind on
35
+ * the wire type — the side panel reads JSON from SSE and only the renderer
36
+ * needs the narrowed shape. Internal callers (emit.ts) get type assistance via
37
+ * the typed factory helpers in emit.ts.
38
+ */
39
+ interface GlassboxEvent {
40
+ kind: GlassboxEventKind;
41
+ at: number;
42
+ data: Record<string, unknown>;
43
+ }
44
+ /**
45
+ * Per-kind data shapes. Surfaced as type aids; not enforced at the
46
+ * GlassboxEvent boundary (intentionally — see comment above).
47
+ */
48
+ interface CompileStartData {
49
+ appId: string;
50
+ archetype: string;
51
+ models: string[];
52
+ }
53
+ interface CompileDoneData {
54
+ target: string;
55
+ provider: string;
56
+ fallbackChain: string[];
57
+ tokensIn: number;
58
+ estimatedCostUsd: number;
59
+ mutationsApplied: MutationApplied[];
60
+ advisories: BestPracticeAdvisory[];
61
+ }
62
+ interface ExecuteAttemptData {
63
+ model: string;
64
+ attemptIndex: number;
65
+ }
66
+ interface ExecuteSuccessData {
67
+ model: string;
68
+ tokensIn: number;
69
+ tokensOut: number;
70
+ latencyMs: number;
71
+ }
72
+ interface AdvisoryFiredData {
73
+ code: string;
74
+ message: string;
75
+ }
76
+ interface FallbackWalkedData {
77
+ from: string;
78
+ to: string;
79
+ reason: FallbackReason | 'unknown';
80
+ attempt: CallAttempt;
81
+ }
82
+ /**
83
+ * Pub/sub backend interface. Two adapters implement it: in-memory (dev /
84
+ * single-process) and Upstash Redis Streams (Vercel Edge multi-instance).
85
+ *
86
+ * The choice is made at module load (NOT per-call) based on env-var presence:
87
+ * if (UPSTASH_REDIS_URL && UPSTASH_REDIS_TOKEN) → Upstash
88
+ * else → in-memory
89
+ *
90
+ * Stream TTL: 60s after last event. After that, subscriber stream closes
91
+ * cleanly. The adapter owns its own TTL clock; subscribe() is agnostic.
92
+ */
93
+ interface GlassboxPubSub {
94
+ /**
95
+ * Publish a single event for a traceId. Returns a promise that resolves
96
+ * after the event is durably appended (or rejects on adapter failure —
97
+ * callers in emit.ts always swallow rejections).
98
+ */
99
+ publish(traceId: string, event: GlassboxEvent): Promise<void>;
100
+ /**
101
+ * Subscribe to events for a traceId. Returns a ReadableStream that emits
102
+ * GlassboxEvent objects as they're published. The stream closes cleanly
103
+ * after the per-trace TTL elapses since the LAST event (rolling 60s).
104
+ *
105
+ * If the traceId has no publisher within 60s of subscription, the stream
106
+ * closes empty.
107
+ */
108
+ subscribe(traceId: string): ReadableStream<GlassboxEvent>;
109
+ /**
110
+ * Test-only escape hatch. Clears any in-memory state. Upstash adapter
111
+ * no-ops (server-side state isn't accessible from here). Tests reach for
112
+ * this between cases to keep adapters hermetic.
113
+ */
114
+ _reset?(): void;
115
+ }
116
+ /**
117
+ * TTL applied to a stream after each event. The design doc names 60s; this
118
+ * constant centralizes it so both adapters + tests agree.
119
+ */
120
+ declare const GLASSBOX_STREAM_TTL_MS = 60000;
121
+
122
+ export { type AdvisoryFiredData as A, type CompileDoneData as C, type ExecuteAttemptData as E, type FallbackWalkedData as F, type GlassboxEvent as G, type CompileStartData as a, type ExecuteSuccessData as b, GLASSBOX_STREAM_TTL_MS as c, type GlassboxEventKind as d, type GlassboxPubSub as e };
@@ -0,0 +1,122 @@
1
+ import { j as MutationApplied, B as BestPracticeAdvisory, F as FallbackReason, g as CallAttempt } from './ir-CFHU3BUT.js';
2
+
3
+ /**
4
+ * Glass-Box observability types (alpha.17).
5
+ *
6
+ * The substrate for kgauto's in-flight observability surface — a Chrome MV3
7
+ * side panel that renders compile + execute + advisor outputs in real-time.
8
+ * See design doc:
9
+ * ~/.gstack/projects/stue-kgauto/stgreen-claude-serene-williamson-51a105-design-20260518-175356.md
10
+ *
11
+ * Wire shape is intentionally minimal:
12
+ * { kind, at, data } — discriminated union by `kind`.
13
+ *
14
+ * Critical-path safety (L-086 derived): emit MUST NEVER fail the user's
15
+ * `call()` invocation. emit.ts wraps every adapter write in try/catch +
16
+ * silently drops on error. Consumers in Vercel Edge runtimes can lose one
17
+ * event without losing the response.
18
+ */
19
+
20
+ /**
21
+ * Discriminator. Six event kinds cover the v1 substrate. New kinds added
22
+ * additively; the side-panel renderer treats unknown kinds as "ignore".
23
+ */
24
+ type GlassboxEventKind = 'compile.start' | 'compile.done' | 'execute.attempt' | 'execute.success' | 'advisory.fired' | 'fallback.walked';
25
+ /**
26
+ * Wire envelope. Every emit lands as one of these.
27
+ *
28
+ * - kind: discriminator
29
+ * - at: unix milliseconds (Date.now())
30
+ * - data: kind-specific payload
31
+ *
32
+ * `data` typed loosely as Record<string, unknown> for serialization-friendliness;
33
+ * the per-kind builders below populate it with the structured shape expected
34
+ * by the renderer. We deliberately do NOT typescript-narrow `data` by kind on
35
+ * the wire type — the side panel reads JSON from SSE and only the renderer
36
+ * needs the narrowed shape. Internal callers (emit.ts) get type assistance via
37
+ * the typed factory helpers in emit.ts.
38
+ */
39
+ interface GlassboxEvent {
40
+ kind: GlassboxEventKind;
41
+ at: number;
42
+ data: Record<string, unknown>;
43
+ }
44
+ /**
45
+ * Per-kind data shapes. Surfaced as type aids; not enforced at the
46
+ * GlassboxEvent boundary (intentionally — see comment above).
47
+ */
48
+ interface CompileStartData {
49
+ appId: string;
50
+ archetype: string;
51
+ models: string[];
52
+ }
53
+ interface CompileDoneData {
54
+ target: string;
55
+ provider: string;
56
+ fallbackChain: string[];
57
+ tokensIn: number;
58
+ estimatedCostUsd: number;
59
+ mutationsApplied: MutationApplied[];
60
+ advisories: BestPracticeAdvisory[];
61
+ }
62
+ interface ExecuteAttemptData {
63
+ model: string;
64
+ attemptIndex: number;
65
+ }
66
+ interface ExecuteSuccessData {
67
+ model: string;
68
+ tokensIn: number;
69
+ tokensOut: number;
70
+ latencyMs: number;
71
+ }
72
+ interface AdvisoryFiredData {
73
+ code: string;
74
+ message: string;
75
+ }
76
+ interface FallbackWalkedData {
77
+ from: string;
78
+ to: string;
79
+ reason: FallbackReason | 'unknown';
80
+ attempt: CallAttempt;
81
+ }
82
+ /**
83
+ * Pub/sub backend interface. Two adapters implement it: in-memory (dev /
84
+ * single-process) and Upstash Redis Streams (Vercel Edge multi-instance).
85
+ *
86
+ * The choice is made at module load (NOT per-call) based on env-var presence:
87
+ * if (UPSTASH_REDIS_URL && UPSTASH_REDIS_TOKEN) → Upstash
88
+ * else → in-memory
89
+ *
90
+ * Stream TTL: 60s after last event. After that, subscriber stream closes
91
+ * cleanly. The adapter owns its own TTL clock; subscribe() is agnostic.
92
+ */
93
+ interface GlassboxPubSub {
94
+ /**
95
+ * Publish a single event for a traceId. Returns a promise that resolves
96
+ * after the event is durably appended (or rejects on adapter failure —
97
+ * callers in emit.ts always swallow rejections).
98
+ */
99
+ publish(traceId: string, event: GlassboxEvent): Promise<void>;
100
+ /**
101
+ * Subscribe to events for a traceId. Returns a ReadableStream that emits
102
+ * GlassboxEvent objects as they're published. The stream closes cleanly
103
+ * after the per-trace TTL elapses since the LAST event (rolling 60s).
104
+ *
105
+ * If the traceId has no publisher within 60s of subscription, the stream
106
+ * closes empty.
107
+ */
108
+ subscribe(traceId: string): ReadableStream<GlassboxEvent>;
109
+ /**
110
+ * Test-only escape hatch. Clears any in-memory state. Upstash adapter
111
+ * no-ops (server-side state isn't accessible from here). Tests reach for
112
+ * this between cases to keep adapters hermetic.
113
+ */
114
+ _reset?(): void;
115
+ }
116
+ /**
117
+ * TTL applied to a stream after each event. The design doc names 60s; this
118
+ * constant centralizes it so both adapters + tests agree.
119
+ */
120
+ declare const GLASSBOX_STREAM_TTL_MS = 60000;
121
+
122
+ export { type AdvisoryFiredData as A, type CompileDoneData as C, type ExecuteAttemptData as E, type FallbackWalkedData as F, type GlassboxEvent as G, type CompileStartData as a, type ExecuteSuccessData as b, GLASSBOX_STREAM_TTL_MS as c, type GlassboxEventKind as d, type GlassboxPubSub as e };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@warmdrift/kgauto-compiler",
3
- "version": "2.0.0-alpha.16",
3
+ "version": "2.0.0-alpha.18",
4
4
  "description": "Prompt compiler + central learning brain for multi-model AI apps. Swap models without rewriting prompts.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -20,11 +20,21 @@
20
20
  "types": "./dist/profiles.d.ts",
21
21
  "import": "./dist/profiles.mjs",
22
22
  "require": "./dist/profiles.js"
23
+ },
24
+ "./glassbox": {
25
+ "types": "./dist/glassbox/index.d.ts",
26
+ "import": "./dist/glassbox/index.mjs",
27
+ "require": "./dist/glassbox/index.js"
28
+ },
29
+ "./glassbox-routes": {
30
+ "types": "./dist/glassbox-routes/index.d.ts",
31
+ "import": "./dist/glassbox-routes/index.mjs",
32
+ "require": "./dist/glassbox-routes/index.js"
23
33
  }
24
34
  },
25
35
  "files": ["dist", "README.md"],
26
36
  "scripts": {
27
- "build": "tsup src/index.ts src/dialect.ts src/profiles.ts --format cjs,esm --dts --clean",
37
+ "build": "tsup src/index.ts src/dialect.ts src/profiles.ts src/glassbox/index.ts src/glassbox-routes/index.ts --format cjs,esm --dts --clean",
28
38
  "test": "vitest run",
29
39
  "test:watch": "vitest",
30
40
  "typecheck": "tsc --noEmit",