-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.svelte
More file actions
152 lines (133 loc) · 5.1 KB
/
Copy pathApp.svelte
File metadata and controls
152 lines (133 loc) · 5.1 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
<script lang="ts">
// search-results — the queue where every agent search lands (spec D4): a
// persistent right rail rendering the workspace service's search registry
// as cards. Collapsed while running (status · elapsed vs typical), signal
// on arrival, expand to act, dismiss when dealt with. No polling — the
// rail refetches on search.updated WS events (+ once on mount, spec D3).
// See context-v/specs/Search-Results-Queue-Remote.md.
import { onMount } from 'svelte';
import { workspace, resolveWsUrl } from '@augment-it/workspace';
import SearchCard from './SearchCard.svelte';
import { dismissSearch, listSearches } from './lib/search-client';
import type { SearchCard as SearchCardT } from './lib/types';
const TOKEN_KEY = 'augment-it:session-token';
const WS_URL = resolveWsUrl();
let status = $state<'connecting' | 'open' | 'closed' | 'error' | 'auth_required'>('connecting');
let client = $state<string>('reach-edu');
let cards = $state<SearchCardT[]>([]);
let loadError = $state<string | null>(null);
let loaded = $state(false);
const doneCount = $derived(cards.filter((c) => c.status === 'done').length);
const runningCount = $derived(cards.filter((c) => c.status === 'queued' || c.status === 'running').length);
// A 1s tick drives the elapsed readouts while anything is in flight —
// display-only; registry truth still arrives exclusively via events.
let now = $state(Date.now());
onMount(() => {
const tick = setInterval(() => {
if (runningCount > 0) now = Date.now();
}, 1_000);
return () => clearInterval(tick);
});
async function load() {
try {
cards = await listSearches(client);
loadError = null;
loaded = true;
} catch (err) {
loadError = err instanceof Error ? err.message : String(err);
}
}
// Registry liveness — search.updated broadcasts land on workspace.events;
// dedup by seq (the corpora-curator / record-collector pattern) and
// refetch. Cheap: the list is card-shaped, no results ride it.
let lastProcessedSeq = -1;
$effect(() => {
const ev = workspace.events[workspace.events.length - 1];
if (!ev || ev.seq <= lastProcessedSeq) return;
lastProcessedSeq = ev.seq;
if (ev.subject !== 'search.updated') return;
const payload = ev.payload as { client?: string };
if (payload.client && payload.client !== client) return;
void load();
});
async function dismiss(card: SearchCardT) {
try {
await dismissSearch(card.search_id);
cards = cards.filter((c) => c.search_id !== card.search_id);
} catch (err) {
loadError = err instanceof Error ? err.message : String(err);
}
}
function clearDone() {
for (const c of cards.filter((c) => c.status === 'done')) void dismiss(c);
}
async function loadActiveClient() {
try {
const r = (await workspace.invoke('workspace.active', {})) as { active_client_id?: string };
if (r?.active_client_id) client = r.active_client_id;
} catch {
/* keep default */
}
}
function onWorkspaceChanged(e: Event) {
const detail = (e as CustomEvent).detail as { client_id?: string } | undefined;
if (detail?.client_id) client = detail.client_id;
else void loadActiveClient();
}
// A different tenant sees a different queue slice — refetch on switch.
$effect(() => {
void client;
if (status === 'open') void load();
});
let bootstrapped = $state(false);
$effect(() => {
if (status === 'open' && !bootstrapped) {
bootstrapped = true;
void (async () => {
await loadActiveClient();
await load();
})();
}
});
onMount(() => {
workspace.connect({
url: WS_URL,
getToken: () => localStorage.getItem(TOKEN_KEY),
saveToken: (t) => localStorage.setItem(TOKEN_KEY, t),
onStatus: (s) => (status = s),
});
window.addEventListener('augment-it:workspace-changed', onWorkspaceChanged);
return () => {
window.removeEventListener('augment-it:workspace-changed', onWorkspaceChanged);
};
});
</script>
<div class="srq-app">
<header class="srq-header">
<h1 class="srq-title">🔎 Search queue</h1>
{#if doneCount > 0}
<span class="srq-badge" title="{doneCount} finished search{doneCount === 1 ? '' : 'es'} waiting for triage">{doneCount}</span>
{/if}
{#if runningCount > 0}
<span class="srq-running-note">{runningCount} in flight</span>
{/if}
<span class="srq-right">
{#if doneCount > 1}
<button type="button" class="srq-clear" title="Dismiss every done card" onclick={clearDone}>clear done</button>
{/if}
<span class="srq-ws status-{status}">{status}</span>
</span>
</header>
{#if loadError}<div class="srq-error">{loadError}</div>{/if}
{#if cards.length === 0 && loaded}
<p class="srq-empty">
No searches in the queue. Fire a 🤖 on any org card — links, streams, or
team — and the search lands here while you keep working.
</p>
{/if}
<ul class="srq-list">
{#each cards as card (card.search_id)}
<SearchCard {card} {client} {now} ondismiss={() => dismiss(card)} />
{/each}
</ul>
</div>