-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.svelte
More file actions
297 lines (272 loc) · 9.83 KB
/
Copy pathApp.svelte
File metadata and controls
297 lines (272 loc) · 9.83 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<script lang="ts">
import { onMount } from 'svelte';
import { workspace, type PromptTemplate, type PromptTool, resolveWsUrl } from '@augment-it/workspace';
const TOKEN_KEY = 'augment-it:session-token';
const WS_URL = resolveWsUrl();
const TOKEN_RE = /\{\{\s*([^{}]+?)\s*\}\}/g;
let status = $state<'connecting' | 'open' | 'closed' | 'error' | 'auth_required'>('connecting');
// editor state — selectedPromptId null means "new, unsaved"
let selectedPromptId = $state<string | null>(null);
let editName = $state('');
let editDescription = $state('');
let editContent = $state('');
let editOutputColumn = $state('');
let editWebSearch = $state(false);
let saveStatus = $state('');
// Snapshot of the last-saved version of the editor fields, so we can tell
// when the form is dirty. For an unsaved new draft this stays null.
type EditorSnapshot = {
name: string;
description: string;
content: string;
output_column: string;
web_search: boolean;
};
let savedSnapshot = $state<EditorSnapshot | null>(null);
const prompts = $derived(Object.values(workspace.prompts) as PromptTemplate[]);
const hasRequiredContent = $derived(
editName.trim() !== '' && editContent.trim() !== '' && editOutputColumn.trim() !== '',
);
const isDirty = $derived.by(() => {
if (savedSnapshot === null) return hasRequiredContent; // new draft is "dirty" once it has content
return (
editName !== savedSnapshot.name ||
editDescription !== savedSnapshot.description ||
editContent !== savedSnapshot.content ||
editOutputColumn !== savedSnapshot.output_column ||
editWebSearch !== savedSnapshot.web_search
);
});
const canApply = $derived(selectedPromptId !== null && !isDirty);
// {{tokens}} referenced by the editor body, distinct, first-seen order
const tokens = $derived.by(() => {
const seen = new Set<string>();
for (const m of editContent.matchAll(TOKEN_RE)) seen.add(m[1].trim());
return [...seen];
});
onMount(() => {
workspace.connect({
url: WS_URL,
getToken: () => localStorage.getItem(TOKEN_KEY),
saveToken: (t) => localStorage.setItem(TOKEN_KEY, t),
onStatus: (s) => (status = s),
});
void refreshPrompts();
});
// prompt CRUD events — seq-cursor dedup so a stale event re-fire on any
// reactive change doesn't re-process (the record-collector lesson).
let lastProcessedSeq = -1;
$effect(() => {
const ev = workspace.events[workspace.events.length - 1];
if (!ev || ev.seq <= lastProcessedSeq) return;
lastProcessedSeq = ev.seq;
if (
ev.subject === 'prompt.created' ||
ev.subject === 'prompt.updated' ||
ev.subject === 'prompt.deleted'
) {
void refreshPrompts();
}
});
// Handoff to the request-reviewer remote is now an explicit "Apply" action,
// not a side-effect of selection. Authoring lives here; firing lives there;
// crossing the boundary is a deliberate click so the user knows when it
// happens.
function applyToRequestReviewer() {
if (!canApply || !selectedPromptId) return;
window.dispatchEvent(
new CustomEvent('augment-it:review-request', { detail: { prompt_id: selectedPromptId } }),
);
saveStatus = 'applied to Request Reviewer';
}
async function refreshPrompts() {
try {
const result = (await workspace.invoke('prompt.list', {})) as { prompts: PromptTemplate[] };
const next: Record<string, PromptTemplate> = {};
for (const p of result.prompts) next[p.prompt_id] = p;
workspace.prompts = next;
} catch (err: unknown) {
console.error('prompt.list', err);
}
}
function loadIntoEditor(p: PromptTemplate) {
selectedPromptId = p.prompt_id;
editName = p.name;
editDescription = p.description;
editContent = p.content;
editOutputColumn = p.output_column;
editWebSearch = p.tools.includes('web_search');
savedSnapshot = {
name: p.name,
description: p.description,
content: p.content,
output_column: p.output_column,
web_search: editWebSearch,
};
saveStatus = '';
}
function newPrompt() {
selectedPromptId = null;
editName = '';
editDescription = '';
editContent = '';
editOutputColumn = '';
editWebSearch = false;
savedSnapshot = null;
saveStatus = '';
}
async function savePrompt() {
if (!hasRequiredContent) {
saveStatus = 'name, content and output column are all required';
return;
}
if (!isDirty) return;
const tools: PromptTool[] = editWebSearch ? ['web_search'] : [];
try {
if (selectedPromptId) {
await workspace.invoke('prompt.update', {
prompt_id: selectedPromptId,
patch: {
name: editName,
description: editDescription,
content: editContent,
output_column: editOutputColumn,
tools,
},
});
saveStatus = 'saved';
} else {
const result = (await workspace.invoke('prompt.create', {
name: editName,
description: editDescription,
content: editContent,
output_column: editOutputColumn,
tools,
})) as { prompt: PromptTemplate };
selectedPromptId = result.prompt.prompt_id;
saveStatus = 'created';
}
savedSnapshot = {
name: editName,
description: editDescription,
content: editContent,
output_column: editOutputColumn,
web_search: editWebSearch,
};
await refreshPrompts();
} catch (err: unknown) {
saveStatus = `error: ${err instanceof Error ? err.message : String(err)}`;
}
}
async function deletePromptById(prompt_id: string, name: string) {
if (!window.confirm(`Delete prompt "${name}"?`)) return;
try {
await workspace.invoke('prompt.delete', { prompt_id });
if (selectedPromptId === prompt_id) newPrompt();
await refreshPrompts();
} catch (err: unknown) {
saveStatus = `error: ${err instanceof Error ? err.message : String(err)}`;
}
}
async function deletePrompt() {
if (!selectedPromptId) return;
await deletePromptById(selectedPromptId, editName);
}
</script>
<div class="ptm-app">
<div class="ptm-status-bar">
<span class="muted">
consumes <code>@augment-it/workspace</code> · {WS_URL} ·
<span class="status status-{status}">{status}</span>
</span>
</div>
<div class="ptm-layout">
<aside>
<h2>Prompts</h2>
<button onclick={newPrompt}>+ new prompt</button>
<ul class="prompts">
{#each prompts as p (p.prompt_id)}
<li class:selected={p.prompt_id === selectedPromptId}>
<button type="button" class="prompt-select" onclick={() => loadIntoEditor(p)}>
<strong>{p.name}</strong>
<span class="muted">→ {p.output_column}{p.tools.includes('web_search') ? ' · web' : ''}</span>
</button>
<button
type="button"
class="prompt-delete"
title="Delete this prompt"
aria-label="delete {p.name}"
onclick={() => void deletePromptById(p.prompt_id, p.name)}
>×</button>
</li>
{/each}
{#if prompts.length === 0}
<li class="muted empty">no prompts yet</li>
{/if}
</ul>
</aside>
<section>
<h2>{selectedPromptId ? 'Edit prompt' : 'New prompt'}</h2>
<label>Name
<input type="text" bind:value={editName} placeholder="Find Organisation URL" />
</label>
<label>Description
<input type="text" bind:value={editDescription} placeholder="what this prompt does" />
</label>
<label>Prompt body — use <code>{'{{Column Name}}'}</code> to insert a record's column value
<textarea bind:value={editContent} rows="7" placeholder={'Find the website for {{Prospect / Organization}}. Respond with only the URL.'}></textarea>
</label>
<label>Output column — the column name the response populates
<input type="text" bind:value={editOutputColumn} placeholder="url" />
</label>
<label class="checkbox">
<input type="checkbox" bind:checked={editWebSearch} />
Enable web search for this prompt
</label>
<div class="tokens">
<span class="muted">tokens in body:</span>
{#if tokens.length === 0}
<span class="muted">none</span>
{:else}
{#each tokens as t (t)}<code class="token">{t}</code>{/each}
{/if}
</div>
<div class="row">
<button
class="primary"
onclick={savePrompt}
disabled={!isDirty || !hasRequiredContent}
title={!hasRequiredContent
? 'Name, prompt body, and output column are required'
: !isDirty
? 'No unsaved changes'
: selectedPromptId
? 'Save changes to this prompt'
: 'Create this prompt'}
>{selectedPromptId ? 'save' : 'create'}</button>
<button
class="apply"
onclick={applyToRequestReviewer}
disabled={!canApply}
title={!selectedPromptId
? 'Save the prompt first, then Apply'
: isDirty
? 'Save your changes before applying'
: 'Send this prompt to Request Reviewer'}
>apply →</button>
{#if selectedPromptId}
<button class="danger" onclick={deletePrompt}>delete</button>
{/if}
<span class="muted">{saveStatus}</span>
</div>
{#if selectedPromptId}
<p class="muted handoff-note">
<strong>Save</strong> lights up only when you've changed something.
<strong>Apply →</strong> sends this prompt to Request Reviewer, where
you review the resolved request, pick the model, and fire.
Authoring lives here; firing lives there.
</p>
{/if}
</section>
</div>
</div>