@gemies/sdk 10.3.0 → 10.5.0
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/CHANGELOG.md +60 -0
- package/package.json +1 -1
- package/src/components/ApprovalBar.tsx +131 -0
- package/src/components/GemieChatContent.tsx +54 -20
- package/src/components/GemieComposer.tsx +7 -0
- package/src/components/thread/AgentWork.tsx +262 -0
- package/src/components/thread/ApprovalCard.tsx +73 -71
- package/src/components/thread/CommandOutput.tsx +106 -0
- package/src/components/thread/GemieThread.tsx +4 -3
- package/src/components/thread/ThreadRow.tsx +21 -1
- package/src/hooks/useApprovalShortcuts.ts +93 -0
- package/src/lib/copy-en.ts +31 -4
- package/src/lib/copy-es.ts +31 -4
- package/src/realtime/contract.ts +18 -1
- package/src/realtime/react.tsx +8 -5
- package/src/runtime/approvals.ts +150 -13
- package/src/runtime/config.tsx +16 -4
- package/src/runtime/index.ts +9 -1
- package/src/runtime/model.ts +2 -0
- package/src/runtime/thread/agent-work.ts +192 -0
- package/src/runtime/thread/command-output.ts +51 -0
- package/src/runtime/use-approval-actions.ts +162 -0
- package/src/runtime/use-conversation-stream.ts +21 -7
- package/src/runtime/use-step-output.ts +42 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
ApprovalExpiredError,
|
|
4
|
+
type ApprovalDecision,
|
|
5
|
+
type ApprovalDetails,
|
|
6
|
+
type ApprovalEntry,
|
|
7
|
+
type ApprovalGrant,
|
|
8
|
+
type ApprovalOutcome,
|
|
9
|
+
firstPending,
|
|
10
|
+
} from './approvals';
|
|
11
|
+
|
|
12
|
+
/** `set` with `id` in it, or without. */
|
|
13
|
+
function mark(set: ReadonlySet<string>, id: string, on: boolean): ReadonlySet<string> {
|
|
14
|
+
const next = new Set(set);
|
|
15
|
+
if (on) next.add(id);
|
|
16
|
+
else next.delete(id);
|
|
17
|
+
return next;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** What the thread and the composer's bar are given to show and answer the questions. */
|
|
21
|
+
export interface ApprovalActions {
|
|
22
|
+
readonly entries: readonly ApprovalEntry[];
|
|
23
|
+
/** Who is asking: the agent's name, as the thread names it. */
|
|
24
|
+
readonly agentName: string;
|
|
25
|
+
/** False when the host cannot carry an answer: a question is shown with nothing to press. */
|
|
26
|
+
readonly canAnswer: boolean;
|
|
27
|
+
/** Questions whose answer is on its way: nothing is offered for them until it arrives. */
|
|
28
|
+
readonly sending: ReadonlySet<string>;
|
|
29
|
+
/** Questions whose answer could not be sent: offered again, with a line saying so. */
|
|
30
|
+
readonly failed: ReadonlySet<string>;
|
|
31
|
+
/** The oldest question still waiting: the one the bar and the shortcuts are about. */
|
|
32
|
+
readonly pending: ApprovalEntry | undefined;
|
|
33
|
+
/** What the person allowed for this session, each to be taken back. Empty until there is one. */
|
|
34
|
+
readonly grants: readonly ApprovalGrant[];
|
|
35
|
+
readonly revokeGrant: (grantId: string) => Promise<void>;
|
|
36
|
+
/** Answers one question, once however often it is pressed. Resolves true when it was sent. */
|
|
37
|
+
readonly decide: (
|
|
38
|
+
entry: ApprovalEntry,
|
|
39
|
+
decision: ApprovalDecision,
|
|
40
|
+
details?: ApprovalDetails,
|
|
41
|
+
) => Promise<boolean>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface UseApprovalActionsInput {
|
|
45
|
+
readonly entries: readonly ApprovalEntry[];
|
|
46
|
+
readonly agentName: string;
|
|
47
|
+
/** Carries the answer to Gemies; rejects with `ApprovalExpiredError` when it is no longer open. */
|
|
48
|
+
readonly onDecide:
|
|
49
|
+
| ((
|
|
50
|
+
entry: ApprovalEntry,
|
|
51
|
+
decision: ApprovalDecision,
|
|
52
|
+
details?: ApprovalDetails,
|
|
53
|
+
) => Promise<void>)
|
|
54
|
+
| undefined;
|
|
55
|
+
/** The answer is known here: show it now. */
|
|
56
|
+
readonly onSettled: (requestId: string, outcome: ApprovalOutcome, feedback?: string) => void;
|
|
57
|
+
/** Stops the running turn, for the answer that says so (`cancel`). */
|
|
58
|
+
readonly onStopTurn?: () => void;
|
|
59
|
+
/** Where the session's allowances are read and taken back; omitted, none are listed. */
|
|
60
|
+
readonly grants?: {
|
|
61
|
+
readonly agentId: string | undefined;
|
|
62
|
+
readonly conversationId: string | undefined;
|
|
63
|
+
readonly list:
|
|
64
|
+
| ((agentId: string, conversationId: string) => Promise<readonly ApprovalGrant[]>)
|
|
65
|
+
| undefined;
|
|
66
|
+
readonly revoke: ((agentId: string, grantId: string) => Promise<void>) | undefined;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The answering of the CLI's questions, shared by every place that shows them — the thread's row
|
|
72
|
+
* and the composer's bar — so a question answered in one is answered in both, and pressing it twice,
|
|
73
|
+
* or once in each, sends one answer.
|
|
74
|
+
*/
|
|
75
|
+
export function useApprovalActions({
|
|
76
|
+
entries,
|
|
77
|
+
agentName,
|
|
78
|
+
onDecide,
|
|
79
|
+
onSettled,
|
|
80
|
+
onStopTurn,
|
|
81
|
+
grants: source,
|
|
82
|
+
}: UseApprovalActionsInput): ApprovalActions {
|
|
83
|
+
const [sending, setSending] = useState<ReadonlySet<string>>(() => new Set());
|
|
84
|
+
const [failed, setFailed] = useState<ReadonlySet<string>>(() => new Set());
|
|
85
|
+
// The guard is a ref: two presses in one frame both see the same render's state.
|
|
86
|
+
const inFlight = useRef(new Set<string>());
|
|
87
|
+
|
|
88
|
+
const decide = useCallback(
|
|
89
|
+
async (entry: ApprovalEntry, decision: ApprovalDecision, details?: ApprovalDetails) => {
|
|
90
|
+
const id = entry.requestId;
|
|
91
|
+
if (!onDecide || entry.outcome !== 'pending' || inFlight.current.has(id)) return false;
|
|
92
|
+
inFlight.current.add(id);
|
|
93
|
+
setSending((s) => mark(s, id, true));
|
|
94
|
+
setFailed((f) => mark(f, id, false));
|
|
95
|
+
try {
|
|
96
|
+
await onDecide(entry, decision, details);
|
|
97
|
+
onSettled(id, decision, details?.feedback);
|
|
98
|
+
if (decision === 'cancel') onStopTurn?.();
|
|
99
|
+
return true;
|
|
100
|
+
} catch (error: unknown) {
|
|
101
|
+
if (error instanceof ApprovalExpiredError) onSettled(id, 'expired');
|
|
102
|
+
else setFailed((f) => mark(f, id, true));
|
|
103
|
+
return false;
|
|
104
|
+
} finally {
|
|
105
|
+
inFlight.current.delete(id);
|
|
106
|
+
setSending((s) => mark(s, id, false));
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
[onDecide, onSettled, onStopTurn],
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const pending = useMemo(() => firstPending(entries), [entries]);
|
|
113
|
+
|
|
114
|
+
// The allowances are read when this conversation has one to show: after the person allowed
|
|
115
|
+
// something for the session, or a question was answered by what they allowed earlier.
|
|
116
|
+
const [grants, setGrants] = useState<readonly ApprovalGrant[]>([]);
|
|
117
|
+
const allowed = entries.filter((e) => e.outcome === 'allow_session').length;
|
|
118
|
+
const { agentId, conversationId, list, revoke } = source ?? {};
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
if (allowed === 0 || !list || !agentId || !conversationId) {
|
|
121
|
+
// Nothing to list: keep the same empty list, so nothing is drawn again for it.
|
|
122
|
+
setGrants((current) => (current.length === 0 ? current : []));
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
let current = true;
|
|
126
|
+
list(agentId, conversationId).then(
|
|
127
|
+
(found) => current && setGrants(found),
|
|
128
|
+
// Not being able to list them is not a reason to hide the conversation.
|
|
129
|
+
() => current && setGrants((now) => (now.length === 0 ? now : [])),
|
|
130
|
+
);
|
|
131
|
+
return () => {
|
|
132
|
+
current = false;
|
|
133
|
+
};
|
|
134
|
+
}, [allowed, list, agentId, conversationId]);
|
|
135
|
+
const revokeGrant = useCallback(
|
|
136
|
+
async (grantId: string) => {
|
|
137
|
+
if (!revoke || !agentId) return;
|
|
138
|
+
try {
|
|
139
|
+
await revoke(agentId, grantId);
|
|
140
|
+
setGrants((current) => current.filter((g) => g.id !== grantId));
|
|
141
|
+
} catch {
|
|
142
|
+
// Still allowed, and still listed: the person can press it again.
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
[revoke, agentId],
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
return useMemo(
|
|
149
|
+
() => ({
|
|
150
|
+
entries,
|
|
151
|
+
agentName,
|
|
152
|
+
canAnswer: onDecide !== undefined,
|
|
153
|
+
sending,
|
|
154
|
+
failed,
|
|
155
|
+
pending,
|
|
156
|
+
grants,
|
|
157
|
+
revokeGrant,
|
|
158
|
+
decide,
|
|
159
|
+
}),
|
|
160
|
+
[entries, agentName, onDecide, sending, failed, pending, grants, revokeGrant, decide],
|
|
161
|
+
);
|
|
162
|
+
}
|
|
@@ -33,7 +33,7 @@ import {
|
|
|
33
33
|
type ApprovalOutcome,
|
|
34
34
|
parseApprovalRequest,
|
|
35
35
|
parseApprovalResolved,
|
|
36
|
-
|
|
36
|
+
settlePending,
|
|
37
37
|
withApprovalOutcome,
|
|
38
38
|
withApprovalRequest,
|
|
39
39
|
} from './approvals';
|
|
@@ -447,8 +447,8 @@ export function useConversationStream({
|
|
|
447
447
|
// A new send answers the old failure, whether it is the retry or a fresh
|
|
448
448
|
// message: either way the card comes down.
|
|
449
449
|
setFailedTurn(null);
|
|
450
|
-
// The last turn's
|
|
451
|
-
setApprovals(
|
|
450
|
+
// The last turn's questions are history: it is over, and none of them is waiting on anyone.
|
|
451
|
+
setApprovals([]);
|
|
452
452
|
retryRef.current = null;
|
|
453
453
|
|
|
454
454
|
// Everything the send is about to change, so a turn that produces no real
|
|
@@ -1237,7 +1237,9 @@ export function useConversationStream({
|
|
|
1237
1237
|
const resolved = parseApprovalResolved(parsed);
|
|
1238
1238
|
if (resolved) {
|
|
1239
1239
|
setApprovals((prev) =>
|
|
1240
|
-
withApprovalOutcome(prev, resolved.requestId, resolved.outcome
|
|
1240
|
+
withApprovalOutcome(prev, resolved.requestId, resolved.outcome, {
|
|
1241
|
+
auto: resolved.auto,
|
|
1242
|
+
}),
|
|
1241
1243
|
);
|
|
1242
1244
|
}
|
|
1243
1245
|
continue;
|
|
@@ -1608,6 +1610,13 @@ export function useConversationStream({
|
|
|
1608
1610
|
// Settle only this turn's message ids, including when a new send has already
|
|
1609
1611
|
// started in the same conversation. Error paths may have settled them first.
|
|
1610
1612
|
settleAssistant(controller?.signal.aborted === true ? 'cancelled' : 'completed');
|
|
1613
|
+
// However the turn ended — answered, stopped, failed, the connection lost — the agent is no
|
|
1614
|
+
// longer waiting on any question it asked: none stays pressable. A newer turn's own
|
|
1615
|
+
// questions are not this one's to close.
|
|
1616
|
+
if (turnGenerationRef.current === turnGeneration) {
|
|
1617
|
+
const outcome = controller?.signal.aborted === true ? 'cancel' : 'ended';
|
|
1618
|
+
setApprovals((prev) => settlePending(prev, outcome));
|
|
1619
|
+
}
|
|
1611
1620
|
// Retain this control while its ACK drains so repeated Stop remains deduplicated.
|
|
1612
1621
|
// Local transport cleanup is required even after the thread has been replaced.
|
|
1613
1622
|
if (turnControl) await turnControl.finish();
|
|
@@ -1718,9 +1727,14 @@ export function useConversationStream({
|
|
|
1718
1727
|
}, [isLoading, setComposerPhase]);
|
|
1719
1728
|
|
|
1720
1729
|
/** The person's own answer, shown at once; the stream's `alia.cli_permission_resolved` repeats it. */
|
|
1721
|
-
const settleApproval = useCallback(
|
|
1722
|
-
|
|
1723
|
-
|
|
1730
|
+
const settleApproval = useCallback(
|
|
1731
|
+
(requestId: string, outcome: ApprovalOutcome, feedback?: string) => {
|
|
1732
|
+
setApprovals((prev) =>
|
|
1733
|
+
withApprovalOutcome(prev, requestId, outcome, feedback ? { feedback } : {}),
|
|
1734
|
+
);
|
|
1735
|
+
},
|
|
1736
|
+
[],
|
|
1737
|
+
);
|
|
1724
1738
|
|
|
1725
1739
|
/** Put the offer away. It wrote nothing, so there is nothing else to undo. */
|
|
1726
1740
|
const dismissSuggestedNewConversation = useCallback(() => {
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
2
|
+
import { stepOutputTopic } from '../realtime/contract';
|
|
3
|
+
import { useRealtimeTopic } from '../realtime/react';
|
|
4
|
+
import type { RealtimeFrame } from '../realtime/contract';
|
|
5
|
+
import type { StepOutput } from './thread/agent-work';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* What a running command has printed, live, while a person has its step open.
|
|
9
|
+
*
|
|
10
|
+
* The step's topic is followed ONLY while `enabled`: a collapsed step costs the connection nothing
|
|
11
|
+
* (its status arrives with the turn's own stream), and collapsing a step releases its topic. Each
|
|
12
|
+
* frame is the tail of the output so far, whole, so the latest frame is the whole truth and a
|
|
13
|
+
* missed one costs nothing. `null` until the first frame (a command that has printed nothing yet,
|
|
14
|
+
* or one the server did not let this person follow): the step then shows what was stored, if any.
|
|
15
|
+
*
|
|
16
|
+
* @param conversationId - The conversation the step is in; `null` for one not created yet.
|
|
17
|
+
* @param toolCallId - The step's call id.
|
|
18
|
+
* @param enabled - The step is open and still running.
|
|
19
|
+
*/
|
|
20
|
+
export function useStepOutput(
|
|
21
|
+
conversationId: string | null | undefined,
|
|
22
|
+
toolCallId: string,
|
|
23
|
+
enabled: boolean,
|
|
24
|
+
): StepOutput | null {
|
|
25
|
+
const topic = enabled && conversationId ? stepOutputTopic(conversationId, toolCallId) : null;
|
|
26
|
+
const [live, setLive] = useState<StepOutput | null>(null);
|
|
27
|
+
|
|
28
|
+
// A different step, or the same one reopened, starts from nothing: the next frame is whole.
|
|
29
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: `topic` is the reset key.
|
|
30
|
+
useEffect(() => setLive(null), [topic]);
|
|
31
|
+
|
|
32
|
+
const onFrame = useCallback(
|
|
33
|
+
(frame: RealtimeFrame) => {
|
|
34
|
+
const data = frame.data as Partial<RealtimeFrame<'step.output'>['data']> | undefined;
|
|
35
|
+
if (data?.toolCallId !== toolCallId || typeof data.text !== 'string') return;
|
|
36
|
+
setLive({ text: data.text, truncated: data.truncated === true });
|
|
37
|
+
},
|
|
38
|
+
[toolCallId],
|
|
39
|
+
);
|
|
40
|
+
useRealtimeTopic(topic, onFrame, 'step.output');
|
|
41
|
+
return live;
|
|
42
|
+
}
|