@warmdrift/kgauto-compiler 2.0.0-alpha.34 → 2.0.0-alpha.35

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,149 @@
1
+ import { i as Adapter, v as SectionKind } from './ir-B_ygNURd.js';
2
+
3
+ /**
4
+ * Internal config + hook types for createGlassboxRoutes().
5
+ *
6
+ * The public contract lives on `GlassboxRoutesConfig` in ./index.ts; these
7
+ * are the narrower per-handler shapes consumed by proxy.ts and stream.ts.
8
+ */
9
+
10
+ /**
11
+ * Wire contract for the Glass-Box Chrome extension's brain-poll endpoint.
12
+ *
13
+ * The list mode of `proxy(req)` returns `{ traces: TraceSummary[] }`. The
14
+ * detail mode (`?traceId=<id>`) returns a single `TraceDetail`. These are
15
+ * the camelCase shapes the extension renderer expects — distinct from the
16
+ * snake_case `compile_outcomes` row shape that PostgREST returns. The
17
+ * factory's typed `rowToSummary` / `rowToDetail` transformer is the single
18
+ * canonical boundary between the DB shape and the wire shape (see
19
+ * `feedback_typed_boundary_transformers.md` in kgauto memory for the rule).
20
+ */
21
+ interface TraceSummary {
22
+ traceId: string;
23
+ appId: string;
24
+ archetype: string;
25
+ target: string;
26
+ createdAt: string;
27
+ tokensIn: number;
28
+ tokensOut: number;
29
+ estimatedCostUsd: number;
30
+ }
31
+
32
+ interface AdvisoryRecord {
33
+ level: 'info' | 'warn' | 'critical';
34
+ /** Stable advisory identifier, e.g. "caching-off-on-claude". */
35
+ code: string;
36
+ /** Consumer-renderable message — no internal jargon ("L-040", "R3" etc.). */
37
+ message: string;
38
+ /** Optional secondary one-liner. Renders below `message` in italics. */
39
+ suggestion?: string;
40
+ /** Deep link to the relevant section of `interfaces/kgauto.md` or docs. */
41
+ docsUrl?: string;
42
+ /**
43
+ * alpha.28+ — closed-union adaptation hint surfaced by the advisor when
44
+ * the advisory can be auto-mitigated by a config knob. Renderer surfaces
45
+ * as `→ try toolOrchestration: 'sequential'` on the advisory row.
46
+ *
47
+ * MUST stay byte-identical to Builder C's
48
+ * `BestPracticeAdvisory.suggestedAdaptation` shape (verified at Phase 2
49
+ * integration; the Adapter type itself is the contract).
50
+ */
51
+ suggestedAdaptation?: Adapter;
52
+ }
53
+ /**
54
+ * Cost-equivalent alternative the chain could have served. Computed at
55
+ * detail-view time by `computeCounterfactuals()` against the served row's
56
+ * observed token counts + archetype + cache state. Up to 2 entries, sorted
57
+ * cheapest first.
58
+ */
59
+ interface TraceCounterfactual {
60
+ modelId: string;
61
+ estimatedCostUsd: number;
62
+ /** servedCostUsd - estimatedCostUsd (always > 0; only ≥10% savings kept). */
63
+ savingsUsd: number;
64
+ /** 0-100. */
65
+ savingsPercent: number;
66
+ /** Plain-English rationale tying archetype + perf score. */
67
+ reason: string;
68
+ }
69
+ /**
70
+ * Derived axis-health tri-state for the three Glass-Box dots
71
+ * (input-ratio · cache · fallback). Renderer reads; transformer computes.
72
+ *
73
+ * Thresholds (locked in design contract Phase 0):
74
+ * - inputRatio: green ≤ 0.65 · yellow 0.65–0.85 · red > 0.85
75
+ * - cache (only when historyCacheableTokens > 1000):
76
+ * green if inputCacheHitRatio ≥ 0.5
77
+ * yellow if 0.1 ≤ inputCacheHitRatio < 0.5
78
+ * red if inputCacheHitRatio < 0.1
79
+ * na if historyCacheableTokens ≤ 1000
80
+ * - fallback: red iff fellOverFrom !== undefined && fellOverFrom !== target
81
+ */
82
+ interface TraceHealth {
83
+ inputRatioStatus: 'green' | 'yellow' | 'red';
84
+ cacheStatus: 'green' | 'yellow' | 'red' | 'na';
85
+ fallbackStatus: 'green' | 'red';
86
+ }
87
+ /**
88
+ * alpha.29+ — wire-boundary representation of a translator section-rewrite.
89
+ *
90
+ * Distinct from the package-internal `SectionRewrite` type in `ir.ts`: this
91
+ * shape drops `originalText` + `transformedText` because those may carry
92
+ * consumer PII. The renderer shows the `rule` + `summary` only. Full text
93
+ * stays on `compile_outcomes.section_rewrites_applied` (Supabase row), gated
94
+ * by RLS for brain-side cross-app learning.
95
+ */
96
+ interface TraceSectionRewrite {
97
+ /** Stable id of the rewritten section. */
98
+ sectionId: string;
99
+ /** Section-kind discriminator that triggered the rewrite. */
100
+ kind: SectionKind;
101
+ /** Stable rule identifier (e.g. `'sequential-tool-cliff-below-floor'`). */
102
+ rule: string;
103
+ /**
104
+ * Plain-English one-liner describing what fired and why. Renderer surfaces
105
+ * this on the Coaching card; no internal jargon ("L-040", "below floor").
106
+ */
107
+ summary: string;
108
+ }
109
+ interface TraceDetail extends TraceSummary {
110
+ mutationsApplied: string[];
111
+ advisories: AdvisoryRecord[];
112
+ rawRequest?: string;
113
+ rawResponse?: string;
114
+ /** Set when consumer passed a forceModel / fallback fired. */
115
+ requestedModel?: string;
116
+ /** Provider finish reason — 'stop' / 'max_tokens' / 'tool_use' / etc. */
117
+ finishReason?: string;
118
+ /** Time to first token (ms); populated when provider surfaces it. */
119
+ ttftMs?: number;
120
+ /** End-to-end wall-clock (ms); from migration 018. */
121
+ totalMs?: number;
122
+ /** Tools kept after relevance pass. */
123
+ toolsCount?: number;
124
+ /** Number of history messages at compile time. */
125
+ historyDepth?: number;
126
+ /** Rendered system prompt size in characters. */
127
+ systemPromptChars?: number;
128
+ cacheReadInputTokens: number;
129
+ cacheCreationInputTokens: number;
130
+ historyCacheableTokens: number;
131
+ /** Derived: cacheReadInputTokens / max(tokensIn, 1). 0-1. */
132
+ inputCacheHitRatio: number;
133
+ fellOverFrom?: string;
134
+ fallbackReason?: 'rate_limit' | 'provider_auth_failed' | 'provider_error' | 'cliff' | 'cost_cap' | 'contract_violation';
135
+ /** Up to 2 alternatives. Empty array (not undefined) when none qualify. */
136
+ counterfactuals?: TraceCounterfactual[];
137
+ /** Undefined when 7d volume < 5/day (insufficient data). */
138
+ projectedDailyCostUsd?: number;
139
+ /**
140
+ * alpha.29+ — translator activity for this trace. Empty array (not
141
+ * undefined) when no rewrites fired or pre-019 row. Surfaced in the
142
+ * Glass-Box Coaching card as synthetic info-level rows. PII-safe by
143
+ * construction: only sectionId + kind + rule + summary cross the wire.
144
+ */
145
+ sectionRewritesApplied: TraceSectionRewrite[];
146
+ health: TraceHealth;
147
+ }
148
+
149
+ export type { AdvisoryRecord as A, TraceHealth as T, TraceDetail as a, TraceSummary as b, TraceCounterfactual as c, TraceSectionRewrite as d };
@@ -0,0 +1,149 @@
1
+ import { i as Adapter, v as SectionKind } from './ir-BAAHLfFL.mjs';
2
+
3
+ /**
4
+ * Internal config + hook types for createGlassboxRoutes().
5
+ *
6
+ * The public contract lives on `GlassboxRoutesConfig` in ./index.ts; these
7
+ * are the narrower per-handler shapes consumed by proxy.ts and stream.ts.
8
+ */
9
+
10
+ /**
11
+ * Wire contract for the Glass-Box Chrome extension's brain-poll endpoint.
12
+ *
13
+ * The list mode of `proxy(req)` returns `{ traces: TraceSummary[] }`. The
14
+ * detail mode (`?traceId=<id>`) returns a single `TraceDetail`. These are
15
+ * the camelCase shapes the extension renderer expects — distinct from the
16
+ * snake_case `compile_outcomes` row shape that PostgREST returns. The
17
+ * factory's typed `rowToSummary` / `rowToDetail` transformer is the single
18
+ * canonical boundary between the DB shape and the wire shape (see
19
+ * `feedback_typed_boundary_transformers.md` in kgauto memory for the rule).
20
+ */
21
+ interface TraceSummary {
22
+ traceId: string;
23
+ appId: string;
24
+ archetype: string;
25
+ target: string;
26
+ createdAt: string;
27
+ tokensIn: number;
28
+ tokensOut: number;
29
+ estimatedCostUsd: number;
30
+ }
31
+
32
+ interface AdvisoryRecord {
33
+ level: 'info' | 'warn' | 'critical';
34
+ /** Stable advisory identifier, e.g. "caching-off-on-claude". */
35
+ code: string;
36
+ /** Consumer-renderable message — no internal jargon ("L-040", "R3" etc.). */
37
+ message: string;
38
+ /** Optional secondary one-liner. Renders below `message` in italics. */
39
+ suggestion?: string;
40
+ /** Deep link to the relevant section of `interfaces/kgauto.md` or docs. */
41
+ docsUrl?: string;
42
+ /**
43
+ * alpha.28+ — closed-union adaptation hint surfaced by the advisor when
44
+ * the advisory can be auto-mitigated by a config knob. Renderer surfaces
45
+ * as `→ try toolOrchestration: 'sequential'` on the advisory row.
46
+ *
47
+ * MUST stay byte-identical to Builder C's
48
+ * `BestPracticeAdvisory.suggestedAdaptation` shape (verified at Phase 2
49
+ * integration; the Adapter type itself is the contract).
50
+ */
51
+ suggestedAdaptation?: Adapter;
52
+ }
53
+ /**
54
+ * Cost-equivalent alternative the chain could have served. Computed at
55
+ * detail-view time by `computeCounterfactuals()` against the served row's
56
+ * observed token counts + archetype + cache state. Up to 2 entries, sorted
57
+ * cheapest first.
58
+ */
59
+ interface TraceCounterfactual {
60
+ modelId: string;
61
+ estimatedCostUsd: number;
62
+ /** servedCostUsd - estimatedCostUsd (always > 0; only ≥10% savings kept). */
63
+ savingsUsd: number;
64
+ /** 0-100. */
65
+ savingsPercent: number;
66
+ /** Plain-English rationale tying archetype + perf score. */
67
+ reason: string;
68
+ }
69
+ /**
70
+ * Derived axis-health tri-state for the three Glass-Box dots
71
+ * (input-ratio · cache · fallback). Renderer reads; transformer computes.
72
+ *
73
+ * Thresholds (locked in design contract Phase 0):
74
+ * - inputRatio: green ≤ 0.65 · yellow 0.65–0.85 · red > 0.85
75
+ * - cache (only when historyCacheableTokens > 1000):
76
+ * green if inputCacheHitRatio ≥ 0.5
77
+ * yellow if 0.1 ≤ inputCacheHitRatio < 0.5
78
+ * red if inputCacheHitRatio < 0.1
79
+ * na if historyCacheableTokens ≤ 1000
80
+ * - fallback: red iff fellOverFrom !== undefined && fellOverFrom !== target
81
+ */
82
+ interface TraceHealth {
83
+ inputRatioStatus: 'green' | 'yellow' | 'red';
84
+ cacheStatus: 'green' | 'yellow' | 'red' | 'na';
85
+ fallbackStatus: 'green' | 'red';
86
+ }
87
+ /**
88
+ * alpha.29+ — wire-boundary representation of a translator section-rewrite.
89
+ *
90
+ * Distinct from the package-internal `SectionRewrite` type in `ir.ts`: this
91
+ * shape drops `originalText` + `transformedText` because those may carry
92
+ * consumer PII. The renderer shows the `rule` + `summary` only. Full text
93
+ * stays on `compile_outcomes.section_rewrites_applied` (Supabase row), gated
94
+ * by RLS for brain-side cross-app learning.
95
+ */
96
+ interface TraceSectionRewrite {
97
+ /** Stable id of the rewritten section. */
98
+ sectionId: string;
99
+ /** Section-kind discriminator that triggered the rewrite. */
100
+ kind: SectionKind;
101
+ /** Stable rule identifier (e.g. `'sequential-tool-cliff-below-floor'`). */
102
+ rule: string;
103
+ /**
104
+ * Plain-English one-liner describing what fired and why. Renderer surfaces
105
+ * this on the Coaching card; no internal jargon ("L-040", "below floor").
106
+ */
107
+ summary: string;
108
+ }
109
+ interface TraceDetail extends TraceSummary {
110
+ mutationsApplied: string[];
111
+ advisories: AdvisoryRecord[];
112
+ rawRequest?: string;
113
+ rawResponse?: string;
114
+ /** Set when consumer passed a forceModel / fallback fired. */
115
+ requestedModel?: string;
116
+ /** Provider finish reason — 'stop' / 'max_tokens' / 'tool_use' / etc. */
117
+ finishReason?: string;
118
+ /** Time to first token (ms); populated when provider surfaces it. */
119
+ ttftMs?: number;
120
+ /** End-to-end wall-clock (ms); from migration 018. */
121
+ totalMs?: number;
122
+ /** Tools kept after relevance pass. */
123
+ toolsCount?: number;
124
+ /** Number of history messages at compile time. */
125
+ historyDepth?: number;
126
+ /** Rendered system prompt size in characters. */
127
+ systemPromptChars?: number;
128
+ cacheReadInputTokens: number;
129
+ cacheCreationInputTokens: number;
130
+ historyCacheableTokens: number;
131
+ /** Derived: cacheReadInputTokens / max(tokensIn, 1). 0-1. */
132
+ inputCacheHitRatio: number;
133
+ fellOverFrom?: string;
134
+ fallbackReason?: 'rate_limit' | 'provider_auth_failed' | 'provider_error' | 'cliff' | 'cost_cap' | 'contract_violation';
135
+ /** Up to 2 alternatives. Empty array (not undefined) when none qualify. */
136
+ counterfactuals?: TraceCounterfactual[];
137
+ /** Undefined when 7d volume < 5/day (insufficient data). */
138
+ projectedDailyCostUsd?: number;
139
+ /**
140
+ * alpha.29+ — translator activity for this trace. Empty array (not
141
+ * undefined) when no rewrites fired or pre-019 row. Surfaced in the
142
+ * Glass-Box Coaching card as synthetic info-level rows. PII-safe by
143
+ * construction: only sectionId + kind + rule + summary cross the wire.
144
+ */
145
+ sectionRewritesApplied: TraceSectionRewrite[];
146
+ health: TraceHealth;
147
+ }
148
+
149
+ export type { AdvisoryRecord as A, TraceHealth as T, TraceDetail as a, TraceSummary as b, TraceCounterfactual as c, TraceSectionRewrite as d };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@warmdrift/kgauto-compiler",
3
- "version": "2.0.0-alpha.34",
3
+ "version": "2.0.0-alpha.35",
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",
@@ -30,11 +30,24 @@
30
30
  "types": "./dist/glassbox-routes/index.d.ts",
31
31
  "import": "./dist/glassbox-routes/index.mjs",
32
32
  "require": "./dist/glassbox-routes/index.js"
33
+ },
34
+ "./glassbox-routes/format": {
35
+ "types": "./dist/glassbox-routes/format.d.ts",
36
+ "import": "./dist/glassbox-routes/format.mjs",
37
+ "require": "./dist/glassbox-routes/format.js"
38
+ },
39
+ "./glassbox-routes/react": {
40
+ "types": "./dist/glassbox-routes/react/index.d.ts",
41
+ "import": "./dist/glassbox-routes/react/index.mjs",
42
+ "require": "./dist/glassbox-routes/react/index.js"
33
43
  }
34
44
  },
35
- "files": ["dist", "README.md"],
45
+ "files": [
46
+ "dist",
47
+ "README.md"
48
+ ],
36
49
  "scripts": {
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",
50
+ "build": "tsup src/index.ts src/dialect.ts src/profiles.ts src/glassbox/index.ts src/glassbox-routes/index.ts src/glassbox-routes/format.ts src/glassbox-routes/react/index.ts --format cjs,esm --dts --clean --external react --external react-dom",
38
51
  "test": "vitest run",
39
52
  "test:watch": "vitest",
40
53
  "typecheck": "tsc --noEmit",
@@ -53,15 +66,28 @@
53
66
  "self-improving"
54
67
  ],
55
68
  "license": "MIT",
56
- "dependencies": {},
57
69
  "peerDependencies": {
58
- "js-tiktoken": ">=1.0.0"
70
+ "js-tiktoken": ">=1.0.0",
71
+ "react": ">=18.0.0",
72
+ "react-dom": ">=18.0.0"
59
73
  },
60
74
  "peerDependenciesMeta": {
61
- "js-tiktoken": { "optional": true }
75
+ "js-tiktoken": {
76
+ "optional": true
77
+ },
78
+ "react": {
79
+ "optional": true
80
+ },
81
+ "react-dom": {
82
+ "optional": true
83
+ }
62
84
  },
63
85
  "devDependencies": {
64
86
  "@types/node": "^22.10.0",
87
+ "@types/react": "^19.2.15",
88
+ "@types/react-dom": "^19.2.3",
89
+ "react": "^19.2.6",
90
+ "react-dom": "^19.2.6",
65
91
  "tsup": "^8.4.0",
66
92
  "typescript": "^5.7.0",
67
93
  "vitest": "^3.1.0"