@happyvertical/smrt-prompts 0.76.0 → 0.76.1

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,223 @@
1
+ <script lang="ts">
2
+ import { useI18n } from '@happyvertical/smrt-ui/i18n';
3
+ import { M } from '../arena.messages.js';
4
+ import type {
5
+ PromptArenaRunnerView,
6
+ PromptArenaRunRequest,
7
+ PromptArenaScoreView,
8
+ PromptArenaView,
9
+ } from '../arena-types.js';
10
+
11
+ const { t } = useI18n();
12
+
13
+ let {
14
+ runners = [],
15
+ contextKey,
16
+ promotionScopes = [],
17
+ onExecute,
18
+ onJudge,
19
+ onPromote,
20
+ }: {
21
+ runners?: PromptArenaRunnerView[];
22
+ /** Change when the authenticated user or tenant changes; clears drafts/results. */
23
+ contextKey: unknown;
24
+ promotionScopes?: Array<'app' | 'tenant'>;
25
+ onExecute?: (request: PromptArenaRunRequest) => Promise<PromptArenaView>;
26
+ onJudge?: (
27
+ id: string,
28
+ pairId: string,
29
+ decision: 'A' | 'B' | 'tie',
30
+ ) => Promise<{
31
+ experiment: PromptArenaView;
32
+ metrics: PromptArenaScoreView[];
33
+ }>;
34
+ onPromote?: (id: string, scope: 'app' | 'tenant') => Promise<unknown>;
35
+ } = $props();
36
+ let key = $state('');
37
+ let draft = $state('');
38
+ let fixture = $state('');
39
+ let input = $state('{"text":""}');
40
+ let experiment = $state<PromptArenaView>();
41
+ let metrics = $state<PromptArenaScoreView[]>([]);
42
+ let busy = $state(false);
43
+ let message = $state('');
44
+ let generation = 0;
45
+ let runner = $derived(runners.find((item) => item.key === key));
46
+ let winning = $derived(
47
+ metrics.length === 2 && metrics[0].wins !== metrics[1].wins,
48
+ );
49
+
50
+ function variantLabel(variant: PromptArenaScoreView['variant'] | undefined) {
51
+ if (variant === 'current') return t(M['prompts.arena.current_variant']);
52
+ if (variant === 'draft') return t(M['prompts.arena.draft_variant']);
53
+ return '';
54
+ }
55
+
56
+ $effect(() => {
57
+ contextKey;
58
+ generation += 1;
59
+ key = '';
60
+ draft = '';
61
+ fixture = '';
62
+ input = '{"text":""}';
63
+ experiment = undefined;
64
+ metrics = [];
65
+ busy = false;
66
+ message = '';
67
+ });
68
+
69
+ function selectRunner() {
70
+ generation += 1;
71
+ const next = runners.find((item) => item.key === key);
72
+ draft = next?.template ?? '';
73
+ fixture = next?.fixtures[0]?.id ?? '';
74
+ experiment = undefined;
75
+ metrics = [];
76
+ message = '';
77
+ busy = false;
78
+ }
79
+
80
+ async function perform(work: () => Promise<void>) {
81
+ const current = generation;
82
+ busy = true;
83
+ message = '';
84
+ try {
85
+ await work();
86
+ } catch (error) {
87
+ if (current === generation)
88
+ message =
89
+ error instanceof Error
90
+ ? error.message
91
+ : t(M['prompts.arena.request_failed']);
92
+ } finally {
93
+ if (current === generation) busy = false;
94
+ }
95
+ }
96
+ async function execute(event: SubmitEvent) {
97
+ event.preventDefault();
98
+ if (!onExecute || !runner || busy) return;
99
+ const current = generation;
100
+ await perform(async () => {
101
+ const selection = fixture
102
+ ? { fixtureId: fixture }
103
+ : { input: JSON.parse(input) };
104
+ const result = await onExecute!({ key, draft, inputs: [selection] });
105
+ if (current === generation) {
106
+ experiment = result;
107
+ metrics = [];
108
+ }
109
+ });
110
+ }
111
+ async function judge(pairId: string, decision: 'A' | 'B' | 'tie') {
112
+ if (!experiment || !onJudge || busy) return;
113
+ const id = experiment.id;
114
+ const current = generation;
115
+ await perform(async () => {
116
+ const result = await onJudge!(id, pairId, decision);
117
+ if (current === generation) {
118
+ experiment = result.experiment;
119
+ metrics = result.metrics;
120
+ }
121
+ });
122
+ }
123
+ async function promote(scope: 'app' | 'tenant') {
124
+ if (!experiment || !onPromote || busy || !winning) return;
125
+ const id = experiment.id;
126
+ const current = generation;
127
+ await perform(async () => {
128
+ await onPromote!(id, scope);
129
+ if (current === generation)
130
+ message = t(
131
+ scope === 'app'
132
+ ? M['prompts.arena.promoted_app']
133
+ : M['prompts.arena.promoted_tenant'],
134
+ );
135
+ });
136
+ }
137
+ </script>
138
+
139
+ <section class="smrt-prompt-arena__root" aria-label={t(M['prompts.arena.heading'])} data-smrt-component="PromptArenaPanel">
140
+ <h2>{t(M['prompts.arena.heading'])}</h2>
141
+ {#if runners.length === 0}<p>{t(M['prompts.arena.empty'])}</p>{/if}
142
+ <form onsubmit={execute}>
143
+ <label>{t(M['prompts.arena.prompt'])}
144
+ <select bind:value={key} onchange={selectRunner} disabled={busy}>
145
+ <option value="">{t(M['prompts.arena.choose_prompt'])}</option>
146
+ {#each runners as item (item.key)}<option value={item.key}>{item.key}</option>{/each}
147
+ </select>
148
+ </label>
149
+ {#if runner}
150
+ <label>{t(M['prompts.arena.alternative_template'])}<textarea bind:value={draft} required disabled={busy}></textarea></label>
151
+ <label>{t(M['prompts.arena.input'])}
152
+ <select bind:value={fixture} disabled={busy}>
153
+ {#each runner.fixtures as item (item.id)}<option value={item.id}>{item.label}</option>{/each}
154
+ <option value="">{t(M['prompts.arena.ad_hoc_json'])}</option>
155
+ </select>
156
+ </label>
157
+ {#if !fixture}<label>{t(M['prompts.arena.application_input_json'])}<textarea bind:value={input} required disabled={busy}></textarea></label>{/if}
158
+ <button type="submit" disabled={busy || !onExecute}>{t(M['prompts.arena.run'])}</button>
159
+ {/if}
160
+ </form>
161
+ {#if busy}<p role="status">{t(M['prompts.arena.working'])}</p>{/if}
162
+ {#if message}<p role="status">{message}</p>{/if}
163
+ {#if experiment}
164
+ {#each experiment.pairs as pair (pair.id)}
165
+ <section aria-label={t(M['prompts.arena.output_comparison'])}>
166
+ <details><summary>{t(M['prompts.arena.application_input'])}</summary><pre>{JSON.stringify(pair.input, null, 2)}</pre></details>
167
+ <div class="smrt-prompt-arena__outputs">
168
+ {#each pair.outputs as output (output.side)}
169
+ <article>
170
+ <h3>{t(M['prompts.arena.output'], { side: output.side })}</h3>
171
+ {#if output.failed}<p>{t(M['prompts.arena.execution_failed'])}</p>{:else}<pre>{output.text}</pre>{/if}
172
+ {#if pair.decision}<p>{variantLabel(pair.variants?.find((variant) => variant.side === output.side)?.variant)}</p>{/if}
173
+ </article>
174
+ {/each}
175
+ </div>
176
+ {#if pair.decision}<p>{t(M['prompts.arena.judgement'], { decision: pair.decision === 'tie' ? t(M['prompts.arena.tie_result']) : pair.decision })}</p>
177
+ {:else if pair.status === 'complete'}
178
+ <div aria-label={t(M['prompts.arena.judge_outputs'])}>
179
+ <button type="button" disabled={busy || !onJudge} onclick={() => judge(pair.id, 'A')}>{t(M['prompts.arena.prefer'], { side: 'A' })}</button>
180
+ <button type="button" disabled={busy || !onJudge} onclick={() => judge(pair.id, 'tie')}>{t(M['prompts.arena.tie'])}</button>
181
+ <button type="button" disabled={busy || !onJudge} onclick={() => judge(pair.id, 'B')}>{t(M['prompts.arena.prefer'], { side: 'B' })}</button>
182
+ </div>
183
+ {/if}
184
+ </section>
185
+ {/each}
186
+ {/if}
187
+ {#if metrics.length}
188
+ <table>
189
+ <caption>{t(M['prompts.arena.scoreboard'])}</caption>
190
+ <thead><tr><th>{t(M['prompts.arena.variant'])}</th><th>{t(M['prompts.arena.wins_judged'])}</th><th>{t(M['prompts.arena.win_rate'])}</th><th>{t(M['prompts.arena.cost'])}</th><th>{t(M['prompts.arena.latency'])}</th><th>{t(M['prompts.arena.tokens'])}</th></tr></thead>
191
+ <tbody>{#each metrics as row (row.variant)}<tr>
192
+ <th>{variantLabel(row.variant)}</th><td>{row.wins} / {row.judgedPairs}</td>
193
+ <td>{row.winRate === null ? '—' : `${(row.winRate * 100).toFixed(1)}%`}</td>
194
+ <td>{row.cost.toFixed(6)}{row.uncostedCalls ? t(M['prompts.arena.uncosted'], { calls: row.uncostedCalls }) : ''}</td>
195
+ <td>{row.latencyMs}</td><td>{row.tokens}</td>
196
+ </tr>{/each}</tbody>
197
+ </table>
198
+ {#each promotionScopes as scope (scope)}
199
+ <button type="button" disabled={busy || !onPromote || !winning} onclick={() => promote(scope)}>{t(scope === 'app' ? M['prompts.arena.promote_app'] : M['prompts.arena.promote_tenant'])}</button>
200
+ {/each}
201
+ {/if}
202
+ </section>
203
+
204
+ <style>
205
+ @layer smrt.reset, smrt.structure, smrt.theme;
206
+ @layer smrt.structure {
207
+ .smrt-prompt-arena__root { display: grid; gap: 1rem; }
208
+ .smrt-prompt-arena__root form,
209
+ .smrt-prompt-arena__root label { display: grid; gap: .5rem; }
210
+ .smrt-prompt-arena__root textarea { min-height: 8rem; }
211
+ .smrt-prompt-arena__outputs { display: grid; grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr)); gap: 1rem; }
212
+ .smrt-prompt-arena__root article { padding: 1rem; }
213
+ .smrt-prompt-arena__root pre { white-space: pre-wrap; overflow-wrap: anywhere; }
214
+ .smrt-prompt-arena__root th,
215
+ .smrt-prompt-arena__root td { padding: .5rem; }
216
+ }
217
+ @layer smrt.theme {
218
+ .smrt-prompt-arena__root th,
219
+ .smrt-prompt-arena__root td { text-align: left; }
220
+ .smrt-prompt-arena__root textarea { font: inherit; }
221
+ .smrt-prompt-arena__root article { border: var(--smrt-hairline); border-radius: .5rem; }
222
+ }
223
+ </style>
@@ -0,0 +1,17 @@
1
+ import type { PromptArenaRunnerView, PromptArenaRunRequest, PromptArenaScoreView, PromptArenaView } from '../arena-types.js';
2
+ type $$ComponentProps = {
3
+ runners?: PromptArenaRunnerView[];
4
+ /** Change when the authenticated user or tenant changes; clears drafts/results. */
5
+ contextKey: unknown;
6
+ promotionScopes?: Array<'app' | 'tenant'>;
7
+ onExecute?: (request: PromptArenaRunRequest) => Promise<PromptArenaView>;
8
+ onJudge?: (id: string, pairId: string, decision: 'A' | 'B' | 'tie') => Promise<{
9
+ experiment: PromptArenaView;
10
+ metrics: PromptArenaScoreView[];
11
+ }>;
12
+ onPromote?: (id: string, scope: 'app' | 'tenant') => Promise<unknown>;
13
+ };
14
+ declare const PromptArenaPanel: import("svelte").Component<$$ComponentProps, {}, "">;
15
+ type PromptArenaPanel = ReturnType<typeof PromptArenaPanel>;
16
+ export default PromptArenaPanel;
17
+ //# sourceMappingURL=PromptArenaPanel.svelte.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PromptArenaPanel.svelte.d.ts","sourceRoot":"","sources":["../../../src/svelte/components/PromptArenaPanel.svelte.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,qBAAqB,EACrB,qBAAqB,EACrB,oBAAoB,EACpB,eAAe,EAChB,MAAM,mBAAmB,CAAC;AAE1B,KAAK,gBAAgB,GAAI;IACxB,OAAO,CAAC,EAAE,qBAAqB,EAAE,CAAC;IAClC,mFAAmF;IACnF,UAAU,EAAE,OAAO,CAAC;IACpB,eAAe,CAAC,EAAE,KAAK,CAAC,KAAK,GAAG,QAAQ,CAAC,CAAC;IAC1C,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,qBAAqB,KAAK,OAAO,CAAC,eAAe,CAAC,CAAC;IACzE,OAAO,CAAC,EAAE,CACR,EAAE,EAAE,MAAM,EACV,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,GAAG,GAAG,GAAG,GAAG,KAAK,KACxB,OAAO,CAAC;QACX,UAAU,EAAE,eAAe,CAAC;QAC5B,OAAO,EAAE,oBAAoB,EAAE,CAAC;KACjC,CAAC,CAAC;IACH,SAAS,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,QAAQ,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;CACvE,CAAC;AA4LF,QAAA,MAAM,gBAAgB,sDAAwC,CAAC;AAC/D,KAAK,gBAAgB,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAC5D,eAAe,gBAAgB,CAAC"}
@@ -13,5 +13,7 @@ import type { ComponentProps } from 'svelte';
13
13
  import PromptSettingsPanel from './components/PromptSettingsPanel.svelte';
14
14
  export { PromptSettingsPanel };
15
15
  export type PromptSettingsPanelProps = ComponentProps<typeof PromptSettingsPanel>;
16
+ export type { PromptArenaJsonInput, PromptArenaRunnerView, PromptArenaRunRequest, PromptArenaScoreView, PromptArenaView, } from './arena-types.js';
17
+ export { default as PromptArenaPanel } from './components/PromptArenaPanel.svelte';
16
18
  export { appRevertPreview, isTemplateEditable, type PromptSettingsChange, type PromptSettingsSupplyingLevel, type PromptSettingsView, supplyingLevelLabel, tenantRevertPreview, } from './types.js';
17
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/svelte/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AAC7C,OAAO,mBAAmB,MAAM,yCAAyC,CAAC;AAE1E,OAAO,EAAE,mBAAmB,EAAE,CAAC;AAC/B,MAAM,MAAM,wBAAwB,GAAG,cAAc,CACnD,OAAO,mBAAmB,CAC3B,CAAC;AAEF,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,KAAK,oBAAoB,EACzB,KAAK,4BAA4B,EACjC,KAAK,kBAAkB,EACvB,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/svelte/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AAC7C,OAAO,mBAAmB,MAAM,yCAAyC,CAAC;AAE1E,OAAO,EAAE,mBAAmB,EAAE,CAAC;AAC/B,MAAM,MAAM,wBAAwB,GAAG,cAAc,CACnD,OAAO,mBAAmB,CAC3B,CAAC;AAEF,YAAY,EACV,oBAAoB,EACpB,qBAAqB,EACrB,qBAAqB,EACrB,oBAAoB,EACpB,eAAe,GAChB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,OAAO,IAAI,gBAAgB,EAAE,MAAM,sCAAsC,CAAC;AACnF,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,KAAK,oBAAoB,EACzB,KAAK,4BAA4B,EACjC,KAAK,kBAAkB,EACvB,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,YAAY,CAAC"}
@@ -11,4 +11,5 @@
11
11
  */
12
12
  import PromptSettingsPanel from './components/PromptSettingsPanel.svelte';
13
13
  export { PromptSettingsPanel };
14
+ export { default as PromptArenaPanel } from './components/PromptArenaPanel.svelte';
14
15
  export { appRevertPreview, isTemplateEditable, supplyingLevelLabel, tenantRevertPreview, } from './types.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@happyvertical/smrt-prompts",
3
- "version": "0.76.0",
3
+ "version": "0.76.1",
4
4
  "description": "Typed prompt registry, override resolution, and tenant-aware prompt settings for SMRT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -28,14 +28,14 @@
28
28
  }
29
29
  },
30
30
  "dependencies": {
31
- "@happyvertical/smrt-config": "0.76.0",
32
- "@happyvertical/smrt-core": "0.76.0",
33
- "@happyvertical/smrt-tenancy": "0.76.0",
34
- "@happyvertical/smrt-ui": "0.76.0",
31
+ "@happyvertical/smrt-config": "0.76.1",
32
+ "@happyvertical/smrt-core": "0.76.1",
33
+ "@happyvertical/smrt-tenancy": "0.76.1",
34
+ "@happyvertical/smrt-ui": "0.76.1",
35
35
  "@happyvertical/sql": "^0.123.0"
36
36
  },
37
37
  "devDependencies": {
38
- "@happyvertical/smrt-vitest": "0.76.0",
38
+ "@happyvertical/smrt-vitest": "0.76.1",
39
39
  "@sveltejs/package": "^2.5.8",
40
40
  "@sveltejs/vite-plugin-svelte": "^7.3.0",
41
41
  "@types/node": "24.13.6",