1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
<script lang="ts">
import type { SystemPromptVariable } from "@dispatch/transport-contract";
import { tick, untrack } from "svelte";
import {
buildTag,
groupVariables,
insertTag,
isDynamicVariable,
type LoadSystemPrompt,
type LoadSystemPromptVariables,
type SaveSystemPrompt,
} from "../logic/view-model";
let {
loadPrompt,
savePrompt,
loadVariables,
onClose,
}: {
loadPrompt: LoadSystemPrompt;
savePrompt: SaveSystemPrompt;
loadVariables: LoadSystemPromptVariables;
onClose: () => void;
} = $props();
let value = $state("");
let loadedTemplate = $state("");
let loading = $state(false);
let saving = $state(false);
let error = $state<string | null>(null);
let justSaved = $state(false);
let variables = $state<readonly SystemPromptVariable[]>([]);
let filePath = $state("");
let textarea: HTMLTextAreaElement | null = null;
const groups = $derived(groupVariables(variables));
const hasChanges = $derived(value !== loadedTemplate);
async function load() {
untrack(() => {
loading = true;
error = null;
});
const [templateResult, variablesResult] = await Promise.all([loadPrompt(), loadVariables()]);
loading = false;
if (!templateResult.ok || !variablesResult.ok) {
const parts: string[] = [];
if (!templateResult.ok) parts.push(templateResult.error);
if (!variablesResult.ok) parts.push(variablesResult.error);
error = parts.join("; ");
return;
}
value = templateResult.template;
loadedTemplate = templateResult.template;
variables = variablesResult.variables;
}
async function save() {
if (saving || loading) return;
saving = true;
error = null;
justSaved = false;
const result = await savePrompt(value);
saving = false;
if (!result.ok) {
error = result.error;
return;
}
loadedTemplate = result.template;
value = result.template;
justSaved = true;
}
function reset() {
value = loadedTemplate;
error = null;
justSaved = false;
}
async function insertTagAtCursor(tag: string) {
if (textarea === null) return;
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
const insertion = insertTag(value, tag, start, end);
value = insertion.template;
await tick();
textarea.focus();
textarea.setSelectionRange(insertion.cursor, insertion.cursor);
}
async function insertFileVariable(type: string) {
const path = filePath.trim();
if (path.length === 0) return;
await insertTagAtCursor(buildTag(type, path));
filePath = "";
}
function onKeydown(e: KeyboardEvent) {
if (e.key === "Escape") onClose();
}
// Load on mount once.
$effect(() => {
void load();
});
</script>
<svelte:window onkeydown={onKeydown} />
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div
class="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-4"
role="dialog"
aria-modal="true"
aria-label="System prompt builder"
tabindex="-1"
onclick={onClose}
onkeydown={onKeydown}
>
<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions -->
<div
class="flex h-[85vh] w-full max-w-6xl flex-col overflow-hidden rounded-box bg-base-100 shadow-2xl"
onclick={(e) => e.stopPropagation()}
>
<!-- Header -->
<div class="flex shrink-0 items-center justify-between border-b border-base-300 px-4 py-3">
<div class="flex items-center gap-2">
<h2 class="text-sm font-semibold">System Prompt</h2>
{#if loading}
<span class="loading loading-spinner loading-xs"></span>
{/if}
</div>
<button
type="button"
class="btn btn-ghost btn-sm btn-square"
onclick={onClose}
aria-label="Close system prompt builder"
>
✕
</button>
</div>
<!-- Body: half editor / half variables -->
<div class="flex min-h-0 flex-1">
<!-- Left: template editor -->
<div class="flex w-1/2 min-w-0 flex-col gap-2 border-r border-base-300 p-4">
<textarea
bind:this={textarea}
bind:value
class="textarea textarea-bordered min-h-0 w-full flex-1 resize-none font-mono text-xs"
placeholder={loading
? "Loading template..."
: "Edit the global system prompt template..."}
disabled={loading}
aria-label="System prompt template"></textarea>
<div class="flex shrink-0 items-center gap-2">
<button
type="button"
class="btn btn-primary btn-sm"
disabled={loading || saving || !hasChanges}
onclick={save}
>
{#if saving}
<span class="loading loading-spinner loading-xs"></span>
{:else}
Save
{/if}
</button>
<button
type="button"
class="btn btn-ghost btn-sm"
disabled={loading || !hasChanges}
onclick={reset}
>
Reset
</button>
{#if justSaved && !hasChanges}
<span class="text-xs text-success">Saved.</span>
{:else if hasChanges}
<span class="text-xs opacity-60">Unsaved changes</span>
{/if}
</div>
{#if error}
<p class="shrink-0 text-xs text-error">{error}</p>
{/if}
</div>
<!-- Right: variable palette -->
<div class="flex w-1/2 min-w-0 flex-col overflow-y-auto p-4">
<h3 class="mb-2 shrink-0 text-xs font-semibold uppercase opacity-60">Variables</h3>
{#if groups.length === 0 && !loading}
<p class="text-xs opacity-60">No variables available.</p>
{/if}
<div class="flex flex-col gap-3">
{#each groups as group (group.type)}
<div class="rounded-box bg-base-200 p-3">
<span class="text-xs font-semibold uppercase opacity-70">{group.type}</span>
<div class="mt-2 flex flex-wrap gap-1">
{#each group.variables as variable (variable.type + variable.name)}
{#if isDynamicVariable(variable)}
<div class="flex items-center gap-1">
<input
type="text"
class="input input-bordered input-xs w-32 font-mono"
bind:value={filePath}
placeholder={variable.name}
onkeydown={(e) => {
if (e.key === "Enter") void insertFileVariable(variable.type);
}}
/>
<button
type="button"
class="btn btn-xs"
onclick={() => void insertFileVariable(variable.type)}
>
Insert
</button>
</div>
{:else}
<button
type="button"
class="btn btn-xs"
title={variable.description}
onclick={() => void insertTagAtCursor(buildTag(variable.type, variable.name))}
>
{variable.name}
</button>
{/if}
{/each}
</div>
</div>
{/each}
</div>
</div>
</div>
</div>
</div>
|