bb-plugin-goal 0.0.0-stage → 0.1.3
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.
- package/LICENSE +21 -0
- package/README.md +95 -2
- package/app.tsx +805 -0
- package/components/ui/button.tsx +60 -0
- package/components/ui/coarse-pointer-sizing.ts +76 -0
- package/components/ui/input.tsx +31 -0
- package/components/ui/motion.ts +21 -0
- package/dist/app.css +2 -0
- package/dist/app.js +1 -0
- package/dist/app.meta.json +19 -0
- package/dist/package.json +1 -0
- package/dist/server.js +442 -0
- package/dist/server.js.map +6 -0
- package/dist/server.meta.json +11 -0
- package/lib/utils.ts +24 -0
- package/package.json +72 -4
- package/server.ts +1758 -0
- package/src/coordinator.ts +892 -0
- package/src/domain.ts +407 -0
- package/src/failure.ts +202 -0
- package/src/history-cursor.ts +46 -0
- package/src/progress.ts +167 -0
- package/src/repository.ts +1735 -0
- package/src/runtime.ts +189 -0
- package/src/usage-wait.ts +21 -0
package/app.tsx
ADDED
|
@@ -0,0 +1,805 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useRef,
|
|
5
|
+
useState,
|
|
6
|
+
type FormEvent,
|
|
7
|
+
} from "react";
|
|
8
|
+
import {
|
|
9
|
+
definePluginApp,
|
|
10
|
+
experimental_useAppPanel,
|
|
11
|
+
useBbContext,
|
|
12
|
+
useRealtime,
|
|
13
|
+
useRealtimeConnectionState,
|
|
14
|
+
useRpc,
|
|
15
|
+
} from "@get-bb/plugin-sdk/app";
|
|
16
|
+
import { Button } from "@/components/ui/button";
|
|
17
|
+
import { Input } from "@/components/ui/input";
|
|
18
|
+
import type { rpcContract } from "./server";
|
|
19
|
+
|
|
20
|
+
const GOAL_REALTIME_CHANNEL = "goal.changed";
|
|
21
|
+
|
|
22
|
+
type GoalState =
|
|
23
|
+
| "active"
|
|
24
|
+
| "paused"
|
|
25
|
+
| "waiting"
|
|
26
|
+
| "completed"
|
|
27
|
+
| "blocked"
|
|
28
|
+
| "canceled";
|
|
29
|
+
type GoalMutation = "edit" | "pause" | "resume" | "cancel";
|
|
30
|
+
|
|
31
|
+
interface GoalView {
|
|
32
|
+
readonly id: string;
|
|
33
|
+
readonly threadId: string;
|
|
34
|
+
readonly objective: string;
|
|
35
|
+
readonly state: GoalState;
|
|
36
|
+
readonly revision: number;
|
|
37
|
+
readonly createdAt: string;
|
|
38
|
+
readonly updatedAt: string;
|
|
39
|
+
readonly finishedAt: string | null;
|
|
40
|
+
readonly completionSummary: string | null;
|
|
41
|
+
readonly verificationEvidence: string | null;
|
|
42
|
+
readonly blockageExternalAction: string | null;
|
|
43
|
+
readonly blockageEvidence: string | null;
|
|
44
|
+
readonly blockageRepeatedTurns: number | null;
|
|
45
|
+
readonly pauseReasonCode:
|
|
46
|
+
| "manual"
|
|
47
|
+
| "archived"
|
|
48
|
+
| "failure"
|
|
49
|
+
| "usage-limit"
|
|
50
|
+
| "no-progress"
|
|
51
|
+
| null;
|
|
52
|
+
readonly pauseReason: string | null;
|
|
53
|
+
readonly usageLimitKind:
|
|
54
|
+
| "subscription-window"
|
|
55
|
+
| "credits"
|
|
56
|
+
| "spend-control"
|
|
57
|
+
| "unknown"
|
|
58
|
+
| null;
|
|
59
|
+
readonly usageResetAt: string | null;
|
|
60
|
+
readonly noProgressConsecutiveCount: number;
|
|
61
|
+
readonly noProgressLastContinuationId: string | null;
|
|
62
|
+
readonly noProgressAssistantResultFingerprint: string | null;
|
|
63
|
+
readonly noProgressEvidence: {
|
|
64
|
+
readonly turnId: string;
|
|
65
|
+
readonly terminalSeq: number;
|
|
66
|
+
readonly signals: readonly string[];
|
|
67
|
+
readonly assessment: "progress" | "no-progress";
|
|
68
|
+
} | null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
type ViewState =
|
|
72
|
+
| { readonly kind: "loading" }
|
|
73
|
+
| { readonly kind: "empty" }
|
|
74
|
+
| { readonly kind: "current"; readonly goal: GoalView }
|
|
75
|
+
| { readonly kind: "error"; readonly message: string };
|
|
76
|
+
|
|
77
|
+
type HistoryViewState =
|
|
78
|
+
| { readonly kind: "loading" }
|
|
79
|
+
| { readonly kind: "error"; readonly message: string }
|
|
80
|
+
| {
|
|
81
|
+
readonly kind: "ready";
|
|
82
|
+
readonly goals: readonly GoalView[];
|
|
83
|
+
readonly nextCursor: string | null;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
function messageFromCause(cause: unknown): string {
|
|
87
|
+
return cause instanceof Error ? cause.message : "Goal could not be loaded.";
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function goalStateLabel(state: GoalState): string {
|
|
91
|
+
switch (state) {
|
|
92
|
+
case "active":
|
|
93
|
+
return "Active Goal";
|
|
94
|
+
case "paused":
|
|
95
|
+
return "Paused Goal";
|
|
96
|
+
case "waiting":
|
|
97
|
+
return "Waiting for usage reset";
|
|
98
|
+
case "completed":
|
|
99
|
+
return "Completed Goal";
|
|
100
|
+
case "blocked":
|
|
101
|
+
return "Blocked Goal";
|
|
102
|
+
case "canceled":
|
|
103
|
+
return "Canceled Goal";
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function GoalHistoryPanel({ threadId }: { readonly threadId: string }) {
|
|
108
|
+
const rpc = useRpc<typeof rpcContract>();
|
|
109
|
+
const connectionState = useRealtimeConnectionState();
|
|
110
|
+
const previousConnectionState = useRef(connectionState);
|
|
111
|
+
const [view, setView] = useState<HistoryViewState>({ kind: "loading" });
|
|
112
|
+
const [loadingMore, setLoadingMore] = useState(false);
|
|
113
|
+
const [confirming, setConfirming] = useState<GoalView | null>(null);
|
|
114
|
+
const [deletingId, setDeletingId] = useState<string | null>(null);
|
|
115
|
+
const [actionError, setActionError] = useState<string | null>(null);
|
|
116
|
+
|
|
117
|
+
const load = useCallback(async () => {
|
|
118
|
+
try {
|
|
119
|
+
const page = await rpc.call("history", {
|
|
120
|
+
threadId,
|
|
121
|
+
limit: 20,
|
|
122
|
+
cursor: null,
|
|
123
|
+
});
|
|
124
|
+
setView({ kind: "ready", ...page });
|
|
125
|
+
setConfirming(null);
|
|
126
|
+
setActionError(null);
|
|
127
|
+
} catch (cause) {
|
|
128
|
+
setView({ kind: "error", message: messageFromCause(cause) });
|
|
129
|
+
}
|
|
130
|
+
}, [rpc, threadId]);
|
|
131
|
+
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
setView({ kind: "loading" });
|
|
134
|
+
void load();
|
|
135
|
+
}, [load]);
|
|
136
|
+
|
|
137
|
+
useEffect(() => {
|
|
138
|
+
const previous = previousConnectionState.current;
|
|
139
|
+
previousConnectionState.current = connectionState;
|
|
140
|
+
if (connectionState === "connected" && previous !== "connected") {
|
|
141
|
+
void load();
|
|
142
|
+
}
|
|
143
|
+
}, [connectionState, load]);
|
|
144
|
+
|
|
145
|
+
useRealtime(GOAL_REALTIME_CHANNEL, (payload) => {
|
|
146
|
+
if (
|
|
147
|
+
typeof payload === "object" &&
|
|
148
|
+
payload !== null &&
|
|
149
|
+
"threadId" in payload &&
|
|
150
|
+
payload.threadId === threadId
|
|
151
|
+
) {
|
|
152
|
+
if ("lifecycle" in payload && payload.lifecycle === "deleted") {
|
|
153
|
+
setView({ kind: "ready", goals: [], nextCursor: null });
|
|
154
|
+
setConfirming(null);
|
|
155
|
+
setActionError(null);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
void load();
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
async function loadMore() {
|
|
163
|
+
if (view.kind !== "ready" || view.nextCursor === null) return;
|
|
164
|
+
setLoadingMore(true);
|
|
165
|
+
setActionError(null);
|
|
166
|
+
try {
|
|
167
|
+
const page = await rpc.call("history", {
|
|
168
|
+
threadId,
|
|
169
|
+
limit: 20,
|
|
170
|
+
cursor: view.nextCursor,
|
|
171
|
+
});
|
|
172
|
+
setView({
|
|
173
|
+
kind: "ready",
|
|
174
|
+
goals: [...view.goals, ...page.goals],
|
|
175
|
+
nextCursor: page.nextCursor,
|
|
176
|
+
});
|
|
177
|
+
} catch (cause) {
|
|
178
|
+
setActionError(messageFromCause(cause));
|
|
179
|
+
} finally {
|
|
180
|
+
setLoadingMore(false);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function deleteGoal(goal: GoalView) {
|
|
185
|
+
setDeletingId(goal.id);
|
|
186
|
+
setActionError(null);
|
|
187
|
+
try {
|
|
188
|
+
await rpc.call("delete", {
|
|
189
|
+
threadId,
|
|
190
|
+
goalId: goal.id,
|
|
191
|
+
expectedRevision: goal.revision,
|
|
192
|
+
});
|
|
193
|
+
await load();
|
|
194
|
+
} catch (cause) {
|
|
195
|
+
setActionError(messageFromCause(cause));
|
|
196
|
+
} finally {
|
|
197
|
+
setDeletingId(null);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (view.kind === "loading") {
|
|
202
|
+
return (
|
|
203
|
+
<div className="text-sm text-muted-foreground" role="status">
|
|
204
|
+
Loading Goal history
|
|
205
|
+
</div>
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (view.kind === "error") {
|
|
210
|
+
return (
|
|
211
|
+
<div className="space-y-3 text-sm">
|
|
212
|
+
<p className="text-destructive">{view.message}</p>
|
|
213
|
+
<Button size="sm" variant="outline" onClick={() => void load()}>
|
|
214
|
+
Retry
|
|
215
|
+
</Button>
|
|
216
|
+
</div>
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return (
|
|
221
|
+
<section aria-label="Goal history" className="space-y-4 text-sm">
|
|
222
|
+
<header>
|
|
223
|
+
<h2 className="font-medium text-foreground">Goal history</h2>
|
|
224
|
+
<p className="mt-1 text-muted-foreground">
|
|
225
|
+
Newest first. Current and finished objectives stay with this thread.
|
|
226
|
+
</p>
|
|
227
|
+
</header>
|
|
228
|
+
|
|
229
|
+
{view.goals.length === 0 ? (
|
|
230
|
+
<div className="rounded-lg border border-dashed border-border p-4 text-muted-foreground">
|
|
231
|
+
No Goals have been recorded for this thread.
|
|
232
|
+
</div>
|
|
233
|
+
) : (
|
|
234
|
+
<ol className="space-y-3">
|
|
235
|
+
{view.goals.map((goal) => {
|
|
236
|
+
const deleting = deletingId === goal.id;
|
|
237
|
+
const isConfirming = confirming?.id === goal.id;
|
|
238
|
+
return (
|
|
239
|
+
<li
|
|
240
|
+
key={goal.id}
|
|
241
|
+
className="rounded-lg border border-border bg-card p-3"
|
|
242
|
+
>
|
|
243
|
+
<div className="flex items-start justify-between gap-3">
|
|
244
|
+
<div className="min-w-0">
|
|
245
|
+
<div className="flex flex-wrap items-center gap-2">
|
|
246
|
+
<span className="font-medium text-foreground">
|
|
247
|
+
{goalStateLabel(goal.state)}
|
|
248
|
+
</span>
|
|
249
|
+
<span className="rounded-full bg-muted px-2 py-0.5 text-xs text-muted-foreground">
|
|
250
|
+
{goal.finishedAt === null ? "Current" : "Historical"}
|
|
251
|
+
</span>
|
|
252
|
+
</div>
|
|
253
|
+
<p className="mt-2 whitespace-pre-wrap text-foreground">
|
|
254
|
+
{goal.objective}
|
|
255
|
+
</p>
|
|
256
|
+
{goal.completionSummary !== null ? (
|
|
257
|
+
<div className="mt-3 rounded-md bg-muted/50 p-2 text-sm">
|
|
258
|
+
<p className="font-medium text-foreground">
|
|
259
|
+
Completion summary
|
|
260
|
+
</p>
|
|
261
|
+
<p className="mt-1 whitespace-pre-wrap text-muted-foreground">
|
|
262
|
+
{goal.completionSummary}
|
|
263
|
+
</p>
|
|
264
|
+
<p className="mt-2 font-medium text-foreground">
|
|
265
|
+
Verification evidence
|
|
266
|
+
</p>
|
|
267
|
+
<p className="mt-1 whitespace-pre-wrap text-muted-foreground">
|
|
268
|
+
{goal.verificationEvidence}
|
|
269
|
+
</p>
|
|
270
|
+
</div>
|
|
271
|
+
) : null}
|
|
272
|
+
{goal.pauseReason !== null ? (
|
|
273
|
+
<div className="mt-3 rounded-md bg-muted/50 p-2 text-sm">
|
|
274
|
+
<p className="font-medium text-foreground">
|
|
275
|
+
Why Goal work paused
|
|
276
|
+
</p>
|
|
277
|
+
<p className="mt-1 whitespace-pre-wrap text-muted-foreground">
|
|
278
|
+
{goal.pauseReason}
|
|
279
|
+
</p>
|
|
280
|
+
{goal.usageLimitKind !== null ? (
|
|
281
|
+
<p className="mt-1 text-xs text-muted-foreground">
|
|
282
|
+
Usage limit: {goal.usageLimitKind}
|
|
283
|
+
{goal.usageResetAt === null
|
|
284
|
+
? " (manual resume required)"
|
|
285
|
+
: ` · resets ${goal.usageResetAt}`}
|
|
286
|
+
</p>
|
|
287
|
+
) : null}
|
|
288
|
+
{goal.noProgressEvidence !== null ? (
|
|
289
|
+
<p className="mt-1 text-xs text-muted-foreground">
|
|
290
|
+
No-progress count: {goal.noProgressConsecutiveCount}
|
|
291
|
+
{` · turn ${goal.noProgressEvidence.turnId} · terminal sequence ${goal.noProgressEvidence.terminalSeq}`}
|
|
292
|
+
{goal.noProgressEvidence.signals.length === 0
|
|
293
|
+
? " · no qualifying signals"
|
|
294
|
+
: ` · signals ${goal.noProgressEvidence.signals.join(", ")}`}
|
|
295
|
+
</p>
|
|
296
|
+
) : null}
|
|
297
|
+
</div>
|
|
298
|
+
) : null}
|
|
299
|
+
{goal.blockageExternalAction !== null ? (
|
|
300
|
+
<div className="mt-3 rounded-md bg-muted/50 p-2 text-sm">
|
|
301
|
+
<p className="font-medium text-foreground">
|
|
302
|
+
External action required
|
|
303
|
+
</p>
|
|
304
|
+
<p className="mt-1 whitespace-pre-wrap text-muted-foreground">
|
|
305
|
+
{goal.blockageExternalAction}
|
|
306
|
+
</p>
|
|
307
|
+
<p className="mt-2 font-medium text-foreground">
|
|
308
|
+
Blockage evidence
|
|
309
|
+
</p>
|
|
310
|
+
<p className="mt-1 whitespace-pre-wrap text-muted-foreground">
|
|
311
|
+
{goal.blockageEvidence}
|
|
312
|
+
</p>
|
|
313
|
+
<p className="mt-2 text-xs text-muted-foreground">
|
|
314
|
+
Same blocker reported for {goal.blockageRepeatedTurns} Goal turns
|
|
315
|
+
</p>
|
|
316
|
+
</div>
|
|
317
|
+
) : null}
|
|
318
|
+
<div className="mt-2 flex flex-wrap gap-x-3 gap-y-1 text-xs text-muted-foreground">
|
|
319
|
+
<code>{goal.id}</code>
|
|
320
|
+
<span>Revision {goal.revision}</span>
|
|
321
|
+
<time dateTime={goal.createdAt}>{goal.createdAt}</time>
|
|
322
|
+
</div>
|
|
323
|
+
</div>
|
|
324
|
+
{!isConfirming ? (
|
|
325
|
+
<Button
|
|
326
|
+
size="sm"
|
|
327
|
+
variant="ghost"
|
|
328
|
+
disabled={deletingId !== null}
|
|
329
|
+
onClick={() => {
|
|
330
|
+
setConfirming(goal);
|
|
331
|
+
setActionError(null);
|
|
332
|
+
}}
|
|
333
|
+
>
|
|
334
|
+
Delete
|
|
335
|
+
</Button>
|
|
336
|
+
) : null}
|
|
337
|
+
</div>
|
|
338
|
+
|
|
339
|
+
{isConfirming ? (
|
|
340
|
+
<div
|
|
341
|
+
className="mt-3 flex flex-wrap items-center justify-between gap-2 border-t border-border pt-3"
|
|
342
|
+
role="alert"
|
|
343
|
+
>
|
|
344
|
+
<span className="text-destructive">
|
|
345
|
+
Delete this Goal permanently?
|
|
346
|
+
</span>
|
|
347
|
+
<div className="flex gap-1">
|
|
348
|
+
<Button
|
|
349
|
+
size="sm"
|
|
350
|
+
variant="ghost"
|
|
351
|
+
disabled={deleting}
|
|
352
|
+
onClick={() => setConfirming(null)}
|
|
353
|
+
>
|
|
354
|
+
Keep Goal
|
|
355
|
+
</Button>
|
|
356
|
+
<Button
|
|
357
|
+
size="sm"
|
|
358
|
+
variant="destructive"
|
|
359
|
+
disabled={deleting}
|
|
360
|
+
onClick={() => void deleteGoal(goal)}
|
|
361
|
+
>
|
|
362
|
+
{deleting ? "Deleting" : "Confirm delete"}
|
|
363
|
+
</Button>
|
|
364
|
+
</div>
|
|
365
|
+
</div>
|
|
366
|
+
) : null}
|
|
367
|
+
</li>
|
|
368
|
+
);
|
|
369
|
+
})}
|
|
370
|
+
</ol>
|
|
371
|
+
)}
|
|
372
|
+
|
|
373
|
+
{view.nextCursor !== null ? (
|
|
374
|
+
<Button
|
|
375
|
+
size="sm"
|
|
376
|
+
variant="outline"
|
|
377
|
+
disabled={loadingMore}
|
|
378
|
+
onClick={() => void loadMore()}
|
|
379
|
+
>
|
|
380
|
+
{loadingMore ? "Loading" : "Load older Goals"}
|
|
381
|
+
</Button>
|
|
382
|
+
) : null}
|
|
383
|
+
|
|
384
|
+
{actionError !== null ? (
|
|
385
|
+
<div className="flex items-center justify-between gap-3 text-destructive">
|
|
386
|
+
<span>{actionError}</span>
|
|
387
|
+
<Button size="sm" variant="ghost" onClick={() => void load()}>
|
|
388
|
+
Refresh
|
|
389
|
+
</Button>
|
|
390
|
+
</div>
|
|
391
|
+
) : null}
|
|
392
|
+
</section>
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
export function GoalComposerRow() {
|
|
397
|
+
const { threadId } = useBbContext();
|
|
398
|
+
const rpc = useRpc<typeof rpcContract>();
|
|
399
|
+
const appPanel = experimental_useAppPanel();
|
|
400
|
+
const connectionState = useRealtimeConnectionState();
|
|
401
|
+
const previousConnectionState = useRef(connectionState);
|
|
402
|
+
const [view, setView] = useState<ViewState>({ kind: "loading" });
|
|
403
|
+
const [starting, setStarting] = useState(false);
|
|
404
|
+
const [editing, setEditing] = useState(false);
|
|
405
|
+
const [objective, setObjective] = useState("");
|
|
406
|
+
const [pending, setPending] = useState<"start" | GoalMutation | null>(null);
|
|
407
|
+
const [actionError, setActionError] = useState<string | null>(null);
|
|
408
|
+
|
|
409
|
+
const load = useCallback(async () => {
|
|
410
|
+
if (threadId === null) {
|
|
411
|
+
setView({ kind: "error", message: "A Goal needs an existing thread." });
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
try {
|
|
415
|
+
const result = await rpc.call("status", { threadId });
|
|
416
|
+
setView(
|
|
417
|
+
result.goal === null
|
|
418
|
+
? { kind: "empty" }
|
|
419
|
+
: { kind: "current", goal: result.goal },
|
|
420
|
+
);
|
|
421
|
+
setActionError(null);
|
|
422
|
+
setEditing(false);
|
|
423
|
+
} catch (cause) {
|
|
424
|
+
setView({ kind: "error", message: messageFromCause(cause) });
|
|
425
|
+
}
|
|
426
|
+
}, [rpc, threadId]);
|
|
427
|
+
|
|
428
|
+
useEffect(() => {
|
|
429
|
+
setView({ kind: "loading" });
|
|
430
|
+
void load();
|
|
431
|
+
}, [load]);
|
|
432
|
+
|
|
433
|
+
useEffect(() => {
|
|
434
|
+
const previous = previousConnectionState.current;
|
|
435
|
+
previousConnectionState.current = connectionState;
|
|
436
|
+
if (connectionState === "connected" && previous !== "connected") {
|
|
437
|
+
void load();
|
|
438
|
+
}
|
|
439
|
+
}, [connectionState, load]);
|
|
440
|
+
|
|
441
|
+
useRealtime(GOAL_REALTIME_CHANNEL, (payload) => {
|
|
442
|
+
if (
|
|
443
|
+
typeof payload === "object" &&
|
|
444
|
+
payload !== null &&
|
|
445
|
+
"threadId" in payload &&
|
|
446
|
+
payload.threadId === threadId
|
|
447
|
+
) {
|
|
448
|
+
if ("lifecycle" in payload && payload.lifecycle === "deleted") {
|
|
449
|
+
setView({ kind: "empty" });
|
|
450
|
+
setActionError(null);
|
|
451
|
+
setEditing(false);
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
void load();
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
async function startGoal(event: FormEvent<HTMLFormElement>) {
|
|
459
|
+
event.preventDefault();
|
|
460
|
+
if (threadId === null || objective.trim().length === 0) return;
|
|
461
|
+
setPending("start");
|
|
462
|
+
setActionError(null);
|
|
463
|
+
try {
|
|
464
|
+
const result = await rpc.call("start", { threadId, objective });
|
|
465
|
+
setView({ kind: "current", goal: result.goal });
|
|
466
|
+
setObjective("");
|
|
467
|
+
setStarting(false);
|
|
468
|
+
} catch (cause) {
|
|
469
|
+
setActionError(messageFromCause(cause));
|
|
470
|
+
} finally {
|
|
471
|
+
setPending(null);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
async function editGoal(event: FormEvent<HTMLFormElement>) {
|
|
476
|
+
event.preventDefault();
|
|
477
|
+
if (
|
|
478
|
+
threadId === null ||
|
|
479
|
+
view.kind !== "current" ||
|
|
480
|
+
objective.trim().length === 0
|
|
481
|
+
) {
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
setPending("edit");
|
|
485
|
+
setActionError(null);
|
|
486
|
+
try {
|
|
487
|
+
const result = await rpc.call("edit", {
|
|
488
|
+
threadId,
|
|
489
|
+
goalId: view.goal.id,
|
|
490
|
+
expectedRevision: view.goal.revision,
|
|
491
|
+
objective,
|
|
492
|
+
});
|
|
493
|
+
setView({ kind: "current", goal: result.goal });
|
|
494
|
+
setEditing(false);
|
|
495
|
+
setObjective("");
|
|
496
|
+
} catch (cause) {
|
|
497
|
+
setActionError(messageFromCause(cause));
|
|
498
|
+
} finally {
|
|
499
|
+
setPending(null);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
async function mutateGoal(type: Exclude<GoalMutation, "edit">) {
|
|
504
|
+
if (threadId === null || view.kind !== "current") return;
|
|
505
|
+
setPending(type);
|
|
506
|
+
setActionError(null);
|
|
507
|
+
try {
|
|
508
|
+
const result = await rpc.call(type, {
|
|
509
|
+
threadId,
|
|
510
|
+
goalId: view.goal.id,
|
|
511
|
+
expectedRevision: view.goal.revision,
|
|
512
|
+
});
|
|
513
|
+
if (type === "cancel") {
|
|
514
|
+
setView({ kind: "empty" });
|
|
515
|
+
} else {
|
|
516
|
+
setView({ kind: "current", goal: result.goal });
|
|
517
|
+
}
|
|
518
|
+
} catch (cause) {
|
|
519
|
+
setActionError(messageFromCause(cause));
|
|
520
|
+
} finally {
|
|
521
|
+
setPending(null);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function openHistory() {
|
|
526
|
+
const opened = appPanel.openTab({
|
|
527
|
+
tab: { id: "history", context: { kind: "thread" }, availability: "action" },
|
|
528
|
+
title: "Goal history",
|
|
529
|
+
});
|
|
530
|
+
if (!opened) {
|
|
531
|
+
setActionError("Goal history is unavailable in this view.");
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
const busy = pending !== null;
|
|
536
|
+
const actionClass = "h-7 rounded-md px-2 text-xs";
|
|
537
|
+
|
|
538
|
+
return (
|
|
539
|
+
<section
|
|
540
|
+
aria-label="Goal"
|
|
541
|
+
className="w-full max-w-[760px] min-w-0 overflow-hidden rounded-xl border border-border bg-card text-xs shadow-sm"
|
|
542
|
+
>
|
|
543
|
+
{view.kind === "loading" ? (
|
|
544
|
+
<div className="flex min-h-8 items-center gap-2 px-3 text-muted-foreground">
|
|
545
|
+
<span className="size-1.5 rounded-full bg-muted-foreground/60" />
|
|
546
|
+
Loading Goal
|
|
547
|
+
</div>
|
|
548
|
+
) : null}
|
|
549
|
+
|
|
550
|
+
{view.kind === "empty" && !starting ? (
|
|
551
|
+
<div className="flex min-h-8 items-center justify-between gap-3 px-3">
|
|
552
|
+
<div className="min-w-0">
|
|
553
|
+
<span className="font-medium text-foreground">Goal</span>
|
|
554
|
+
<span className="ml-2 text-muted-foreground">
|
|
555
|
+
No unfinished objective
|
|
556
|
+
</span>
|
|
557
|
+
</div>
|
|
558
|
+
<div className="flex shrink-0 items-center gap-1">
|
|
559
|
+
<Button
|
|
560
|
+
size="sm"
|
|
561
|
+
variant="ghost"
|
|
562
|
+
className={actionClass}
|
|
563
|
+
onClick={openHistory}
|
|
564
|
+
>
|
|
565
|
+
History
|
|
566
|
+
</Button>
|
|
567
|
+
<Button
|
|
568
|
+
size="sm"
|
|
569
|
+
variant="secondary"
|
|
570
|
+
className={actionClass}
|
|
571
|
+
onClick={() => setStarting(true)}
|
|
572
|
+
>
|
|
573
|
+
Start Goal
|
|
574
|
+
</Button>
|
|
575
|
+
</div>
|
|
576
|
+
</div>
|
|
577
|
+
) : null}
|
|
578
|
+
|
|
579
|
+
{view.kind === "empty" && starting ? (
|
|
580
|
+
<form className="flex items-center gap-2 p-2" onSubmit={startGoal}>
|
|
581
|
+
<label className="sr-only" htmlFor="goal-objective">
|
|
582
|
+
Goal objective
|
|
583
|
+
</label>
|
|
584
|
+
<Input
|
|
585
|
+
id="goal-objective"
|
|
586
|
+
autoFocus
|
|
587
|
+
value={objective}
|
|
588
|
+
onChange={(event) => setObjective(event.target.value)}
|
|
589
|
+
placeholder="What should this thread finish?"
|
|
590
|
+
className="h-8"
|
|
591
|
+
/>
|
|
592
|
+
<Button
|
|
593
|
+
size="sm"
|
|
594
|
+
type="submit"
|
|
595
|
+
className={actionClass}
|
|
596
|
+
disabled={busy || objective.trim().length === 0}
|
|
597
|
+
>
|
|
598
|
+
{pending === "start" ? "Starting" : "Start"}
|
|
599
|
+
</Button>
|
|
600
|
+
<Button
|
|
601
|
+
size="sm"
|
|
602
|
+
type="button"
|
|
603
|
+
variant="ghost"
|
|
604
|
+
className={actionClass}
|
|
605
|
+
disabled={busy}
|
|
606
|
+
onClick={() => {
|
|
607
|
+
setStarting(false);
|
|
608
|
+
setObjective("");
|
|
609
|
+
setActionError(null);
|
|
610
|
+
}}
|
|
611
|
+
>
|
|
612
|
+
Cancel
|
|
613
|
+
</Button>
|
|
614
|
+
</form>
|
|
615
|
+
) : null}
|
|
616
|
+
|
|
617
|
+
{view.kind === "current" && editing ? (
|
|
618
|
+
<form className="flex items-center gap-2 p-2" onSubmit={editGoal}>
|
|
619
|
+
<label className="sr-only" htmlFor="goal-objective-edit">
|
|
620
|
+
Edit Goal objective
|
|
621
|
+
</label>
|
|
622
|
+
<Input
|
|
623
|
+
id="goal-objective-edit"
|
|
624
|
+
autoFocus
|
|
625
|
+
value={objective}
|
|
626
|
+
onChange={(event) => setObjective(event.target.value)}
|
|
627
|
+
className="h-8"
|
|
628
|
+
/>
|
|
629
|
+
<Button
|
|
630
|
+
size="sm"
|
|
631
|
+
type="submit"
|
|
632
|
+
className={actionClass}
|
|
633
|
+
disabled={busy || objective.trim().length === 0}
|
|
634
|
+
>
|
|
635
|
+
{pending === "edit" ? "Saving" : "Save"}
|
|
636
|
+
</Button>
|
|
637
|
+
<Button
|
|
638
|
+
size="sm"
|
|
639
|
+
type="button"
|
|
640
|
+
variant="ghost"
|
|
641
|
+
className={actionClass}
|
|
642
|
+
disabled={busy}
|
|
643
|
+
onClick={() => {
|
|
644
|
+
setEditing(false);
|
|
645
|
+
setObjective("");
|
|
646
|
+
setActionError(null);
|
|
647
|
+
}}
|
|
648
|
+
>
|
|
649
|
+
Cancel
|
|
650
|
+
</Button>
|
|
651
|
+
</form>
|
|
652
|
+
) : null}
|
|
653
|
+
|
|
654
|
+
{view.kind === "current" && !editing ? (
|
|
655
|
+
<details className="group">
|
|
656
|
+
<summary
|
|
657
|
+
className="flex min-h-8 min-w-0 cursor-pointer list-none items-center gap-2 px-3 [&::-webkit-details-marker]:hidden"
|
|
658
|
+
aria-label="Goal details"
|
|
659
|
+
>
|
|
660
|
+
<span
|
|
661
|
+
className={`size-2 shrink-0 rounded-full ${
|
|
662
|
+
view.goal.state === "active"
|
|
663
|
+
? "bg-success"
|
|
664
|
+
: "bg-muted-foreground"
|
|
665
|
+
}`}
|
|
666
|
+
/>
|
|
667
|
+
<span className="shrink-0 font-medium text-foreground">
|
|
668
|
+
{goalStateLabel(view.goal.state)}
|
|
669
|
+
</span>
|
|
670
|
+
<span
|
|
671
|
+
className="min-w-0 flex-1 truncate text-muted-foreground"
|
|
672
|
+
title={view.goal.pauseReason ?? view.goal.objective}
|
|
673
|
+
>
|
|
674
|
+
{view.goal.objective}
|
|
675
|
+
</span>
|
|
676
|
+
<span
|
|
677
|
+
aria-hidden="true"
|
|
678
|
+
className="shrink-0 text-muted-foreground transition-transform group-open:rotate-180"
|
|
679
|
+
>
|
|
680
|
+
⌄
|
|
681
|
+
</span>
|
|
682
|
+
</summary>
|
|
683
|
+
<div className="border-t border-border/50 bg-muted/20">
|
|
684
|
+
<p className="whitespace-pre-wrap px-3 py-2.5 text-sm leading-relaxed text-foreground/90">
|
|
685
|
+
{view.goal.objective}
|
|
686
|
+
{view.goal.pauseReason === null ? "" : ` · ${view.goal.pauseReason}`}
|
|
687
|
+
</p>
|
|
688
|
+
{view.goal.noProgressConsecutiveCount > 0 ? (
|
|
689
|
+
<p className="px-3 pb-2 text-muted-foreground">
|
|
690
|
+
No-progress {view.goal.noProgressConsecutiveCount}/3
|
|
691
|
+
</p>
|
|
692
|
+
) : null}
|
|
693
|
+
<div className="flex flex-wrap items-center justify-between gap-1 border-t border-border/35 px-2 py-1">
|
|
694
|
+
<Button
|
|
695
|
+
size="sm"
|
|
696
|
+
variant="ghost"
|
|
697
|
+
className={actionClass}
|
|
698
|
+
disabled={busy}
|
|
699
|
+
onClick={openHistory}
|
|
700
|
+
>
|
|
701
|
+
History
|
|
702
|
+
</Button>
|
|
703
|
+
<div className="flex flex-wrap items-center justify-end gap-1">
|
|
704
|
+
<Button
|
|
705
|
+
size="sm"
|
|
706
|
+
variant="ghost"
|
|
707
|
+
className={actionClass}
|
|
708
|
+
aria-label="Edit Goal"
|
|
709
|
+
disabled={busy}
|
|
710
|
+
onClick={() => {
|
|
711
|
+
setObjective(view.goal.objective);
|
|
712
|
+
setEditing(true);
|
|
713
|
+
setActionError(null);
|
|
714
|
+
}}
|
|
715
|
+
>
|
|
716
|
+
Edit
|
|
717
|
+
</Button>
|
|
718
|
+
{view.goal.state === "active" || view.goal.state === "waiting" ? (
|
|
719
|
+
<Button
|
|
720
|
+
size="sm"
|
|
721
|
+
variant="ghost"
|
|
722
|
+
className={actionClass}
|
|
723
|
+
aria-label="Pause Goal"
|
|
724
|
+
disabled={busy}
|
|
725
|
+
onClick={() => void mutateGoal("pause")}
|
|
726
|
+
>
|
|
727
|
+
{pending === "pause" ? "Pausing" : "Pause"}
|
|
728
|
+
</Button>
|
|
729
|
+
) : null}
|
|
730
|
+
{view.goal.state === "paused" || view.goal.state === "waiting" ? (
|
|
731
|
+
<Button
|
|
732
|
+
size="sm"
|
|
733
|
+
variant="ghost"
|
|
734
|
+
className={actionClass}
|
|
735
|
+
aria-label="Resume Goal"
|
|
736
|
+
disabled={busy}
|
|
737
|
+
onClick={() => void mutateGoal("resume")}
|
|
738
|
+
>
|
|
739
|
+
{pending === "resume" ? "Resuming" : "Resume"}
|
|
740
|
+
</Button>
|
|
741
|
+
) : null}
|
|
742
|
+
<Button
|
|
743
|
+
size="sm"
|
|
744
|
+
variant="ghost"
|
|
745
|
+
className={actionClass}
|
|
746
|
+
aria-label="Cancel Goal"
|
|
747
|
+
disabled={busy}
|
|
748
|
+
onClick={() => void mutateGoal("cancel")}
|
|
749
|
+
>
|
|
750
|
+
{pending === "cancel" ? "Canceling" : "Cancel"}
|
|
751
|
+
</Button>
|
|
752
|
+
</div>
|
|
753
|
+
</div>
|
|
754
|
+
</div>
|
|
755
|
+
</details>
|
|
756
|
+
) : null}
|
|
757
|
+
|
|
758
|
+
{actionError !== null ? (
|
|
759
|
+
<div className="flex items-center justify-between gap-3 border-t border-border/35 px-3 py-1 text-destructive">
|
|
760
|
+
<span className="truncate">{actionError}</span>
|
|
761
|
+
<Button size="sm" variant="ghost" onClick={() => void load()}>
|
|
762
|
+
Refresh
|
|
763
|
+
</Button>
|
|
764
|
+
</div>
|
|
765
|
+
) : null}
|
|
766
|
+
|
|
767
|
+
{view.kind === "error" ? (
|
|
768
|
+
<div className="flex min-h-8 items-center justify-between gap-3 px-3 text-destructive">
|
|
769
|
+
<span className="truncate">{view.message}</span>
|
|
770
|
+
<Button
|
|
771
|
+
size="sm"
|
|
772
|
+
variant="ghost"
|
|
773
|
+
className={actionClass}
|
|
774
|
+
onClick={() => void load()}
|
|
775
|
+
>
|
|
776
|
+
Retry
|
|
777
|
+
</Button>
|
|
778
|
+
</div>
|
|
779
|
+
) : null}
|
|
780
|
+
</section>
|
|
781
|
+
);
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
export default definePluginApp((app) => {
|
|
785
|
+
app.slots.experimental_tab({
|
|
786
|
+
context: { kind: "thread" },
|
|
787
|
+
availability: "action",
|
|
788
|
+
id: "history",
|
|
789
|
+
title: "Goal history",
|
|
790
|
+
icon: "History",
|
|
791
|
+
component: GoalHistoryPanel,
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
app.composer.customize({
|
|
795
|
+
id: "goal-row",
|
|
796
|
+
scopes: ["thread"],
|
|
797
|
+
banners: [
|
|
798
|
+
{
|
|
799
|
+
id: "current-goal",
|
|
800
|
+
chrome: "bare",
|
|
801
|
+
component: GoalComposerRow,
|
|
802
|
+
},
|
|
803
|
+
],
|
|
804
|
+
});
|
|
805
|
+
});
|