@gemies/sdk 10.2.0 → 10.4.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 +53 -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/ApprovalCard.tsx +73 -71
- package/src/components/thread/GemieThread.tsx +4 -3
- package/src/hooks/useApprovalShortcuts.ts +93 -0
- package/src/lib/copy-en.ts +16 -4
- package/src/lib/copy-es.ts +16 -4
- package/src/runtime/approvals.ts +150 -13
- package/src/runtime/config.tsx +16 -4
- package/src/runtime/index.ts +9 -1
- package/src/runtime/resumable-stream.ts +306 -0
- package/src/runtime/routes.ts +14 -0
- package/src/runtime/sse-frame-reader.ts +20 -1
- package/src/runtime/stream-events.ts +68 -0
- package/src/runtime/use-approval-actions.ts +162 -0
- package/src/runtime/use-conversation-stream.ts +79 -36
|
@@ -31,6 +31,11 @@ export interface SseFrame {
|
|
|
31
31
|
readonly event: string;
|
|
32
32
|
/** The `data:` payload, trimmed. `[DONE]` is passed through as-is. */
|
|
33
33
|
readonly data: string;
|
|
34
|
+
/**
|
|
35
|
+
* The frame's `id:`, the turn's sequence number (`resumable-stream.ts`), when it has one. A frame
|
|
36
|
+
* of a server that numbers nothing has none.
|
|
37
|
+
*/
|
|
38
|
+
readonly id?: number;
|
|
34
39
|
}
|
|
35
40
|
|
|
36
41
|
export interface SseFrameReader {
|
|
@@ -41,6 +46,7 @@ export interface SseFrameReader {
|
|
|
41
46
|
export function createSseFrameReader(): SseFrameReader {
|
|
42
47
|
let buffer = '';
|
|
43
48
|
let currentEventType = '';
|
|
49
|
+
let currentId: number | undefined;
|
|
44
50
|
|
|
45
51
|
return {
|
|
46
52
|
push(chunk: string): SseFrame[] {
|
|
@@ -63,19 +69,32 @@ export function createSseFrameReader(): SseFrameReader {
|
|
|
63
69
|
continue;
|
|
64
70
|
}
|
|
65
71
|
|
|
72
|
+
// Only a whole number is a sequence number; anything else is a field this client ignores.
|
|
73
|
+
if (line.startsWith('id: ')) {
|
|
74
|
+
const id = line.slice(4).trim();
|
|
75
|
+
currentId = /^\d{1,15}$/.test(id) ? Number(id) : undefined;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
|
|
66
79
|
// A blank line closes a frame. Resetting here is what stops a `data:`
|
|
67
80
|
// line in the NEXT frame inheriting this frame's event name.
|
|
68
81
|
if (line === '') {
|
|
69
82
|
currentEventType = '';
|
|
83
|
+
currentId = undefined;
|
|
70
84
|
continue;
|
|
71
85
|
}
|
|
72
86
|
|
|
73
87
|
if (line.startsWith('data: ')) {
|
|
74
|
-
frames.push({
|
|
88
|
+
frames.push({
|
|
89
|
+
event: currentEventType,
|
|
90
|
+
data: line.slice(6).trim(),
|
|
91
|
+
...(currentId === undefined ? {} : { id: currentId }),
|
|
92
|
+
});
|
|
75
93
|
// One `data:` line is one frame for this protocol. The event name is
|
|
76
94
|
// consumed with it, so a second `data:` without its own `event:` is
|
|
77
95
|
// correctly unnamed rather than inheriting.
|
|
78
96
|
currentEventType = '';
|
|
97
|
+
currentId = undefined;
|
|
79
98
|
}
|
|
80
99
|
|
|
81
100
|
// Comments (`: keep-alive`) and any field this client does not read
|
|
@@ -155,3 +155,71 @@ export function lockedFeatureOf(error: unknown): string | null {
|
|
|
155
155
|
if (!isObject(error) || error.code !== FEATURE_ENTITLEMENT_REQUIRED) return null;
|
|
156
156
|
return isText(error.feature) ? error.feature : null;
|
|
157
157
|
}
|
|
158
|
+
|
|
159
|
+
/** The event name of a resume's snapshot of the turn (`resumable-stream.ts`). */
|
|
160
|
+
export const TURN_SNAPSHOT_EVENT = 'alia.snapshot';
|
|
161
|
+
|
|
162
|
+
/** One tool call of a snapshot; `arguments` is the JSON string the wire sends them as. */
|
|
163
|
+
export interface SnapshotToolInvocation {
|
|
164
|
+
toolCallId: string;
|
|
165
|
+
toolName: string;
|
|
166
|
+
state: 'call' | 'result';
|
|
167
|
+
arguments: string;
|
|
168
|
+
result?: unknown;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* The turn's state the server sends when a reconnect cannot be given the frames it missed: the
|
|
173
|
+
* words, the reasoning and the tool calls so far, and the turn's other frames. `seq` is the number
|
|
174
|
+
* the state runs through; `reasoning` is absent when the reader should keep its own.
|
|
175
|
+
*/
|
|
176
|
+
export interface TurnSnapshot {
|
|
177
|
+
seq?: number;
|
|
178
|
+
content: string;
|
|
179
|
+
reasoning?: string;
|
|
180
|
+
toolInvocations: SnapshotToolInvocation[];
|
|
181
|
+
frames: Array<{ seq: number; event: string; data: string }>;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** The snapshot a `alia.snapshot` frame carries, or `null` when it is not well-formed. */
|
|
185
|
+
export function parseTurnSnapshot(data: unknown): TurnSnapshot | null {
|
|
186
|
+
if (!isObject(data) || data.eventVersion !== 1 || typeof data.content !== 'string') return null;
|
|
187
|
+
if (data.reasoning !== undefined && typeof data.reasoning !== 'string') return null;
|
|
188
|
+
if (data.seq !== undefined && !(Number.isSafeInteger(data.seq) && (data.seq as number) >= 0)) {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
if (!Array.isArray(data.toolInvocations) || !Array.isArray(data.frames)) return null;
|
|
192
|
+
|
|
193
|
+
const toolInvocations: SnapshotToolInvocation[] = [];
|
|
194
|
+
for (const call of data.toolInvocations) {
|
|
195
|
+
if (!isObject(call) || !isText(call.toolCallId) || !isText(call.toolName)) return null;
|
|
196
|
+
if (call.state !== 'call' && call.state !== 'result') return null;
|
|
197
|
+
if (typeof call.arguments !== 'string') return null;
|
|
198
|
+
toolInvocations.push({
|
|
199
|
+
toolCallId: call.toolCallId,
|
|
200
|
+
toolName: call.toolName,
|
|
201
|
+
state: call.state,
|
|
202
|
+
arguments: call.arguments,
|
|
203
|
+
...(call.result === undefined ? {} : { result: call.result }),
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
const frames: TurnSnapshot['frames'] = [];
|
|
207
|
+
for (const frame of data.frames) {
|
|
208
|
+
if (
|
|
209
|
+
!isObject(frame) ||
|
|
210
|
+
!Number.isSafeInteger(frame.seq) ||
|
|
211
|
+
typeof frame.event !== 'string' ||
|
|
212
|
+
typeof frame.data !== 'string'
|
|
213
|
+
) {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
frames.push({ seq: frame.seq as number, event: frame.event, data: frame.data });
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
...(data.seq === undefined ? {} : { seq: data.seq as number }),
|
|
220
|
+
content: data.content,
|
|
221
|
+
...(data.reasoning === undefined ? {} : { reasoning: data.reasoning }),
|
|
222
|
+
toolInvocations,
|
|
223
|
+
frames,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
@@ -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,13 +33,13 @@ import {
|
|
|
33
33
|
type ApprovalOutcome,
|
|
34
34
|
parseApprovalRequest,
|
|
35
35
|
parseApprovalResolved,
|
|
36
|
-
|
|
36
|
+
settlePending,
|
|
37
37
|
withApprovalOutcome,
|
|
38
38
|
withApprovalRequest,
|
|
39
39
|
} from './approvals';
|
|
40
40
|
import { type GemieHostTools, HostOutcomeQueue, hostRequest } from './host-tools';
|
|
41
41
|
import { API_ROUTES } from './routes';
|
|
42
|
-
import {
|
|
42
|
+
import { createResumableFrameSource, TURN_RESET_EVENT } from './resumable-stream';
|
|
43
43
|
import {
|
|
44
44
|
type MemberTurnEvent,
|
|
45
45
|
lockedFeatureOf,
|
|
@@ -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
|
|
@@ -949,34 +949,47 @@ export function useConversationStream({
|
|
|
949
949
|
// The server has the outcomes this request carried: they are its calls' results now.
|
|
950
950
|
hostOutcomesRef.current.delivered(outcomes);
|
|
951
951
|
|
|
952
|
-
const reader = response.body.getReader();
|
|
953
|
-
const decoder = new TextDecoder();
|
|
954
952
|
/**
|
|
955
|
-
* Frame reassembly lives in `sse-frame-reader.ts
|
|
953
|
+
* Frame reassembly lives in `sse-frame-reader.ts` and the seam across a dropped connection
|
|
954
|
+
* in `resumable-stream.ts`, which reads the turn's frames, drops the ones already applied
|
|
955
|
+
* by their number, and resumes the turn from its last frame when the connection ends
|
|
956
|
+
* before the server said the turn was over.
|
|
956
957
|
*
|
|
957
|
-
*
|
|
958
|
-
* `buffer` was declared outside the read loop and survived a chunk
|
|
959
|
-
*
|
|
960
|
-
*
|
|
961
|
-
*
|
|
962
|
-
*
|
|
963
|
-
*
|
|
964
|
-
*
|
|
965
|
-
* `alia.plan_preview`, `alia.agent_start`, `alia.host_action`.
|
|
966
|
-
*
|
|
967
|
-
* Two pieces of state that must both outlive a chunk are fields of one
|
|
968
|
-
* object now, and its test feeds a stream split at every byte offset —
|
|
969
|
-
* which is not something a test of this hook could do.
|
|
958
|
+
* Frame reassembly used to be loose variables here, and that is how the bug happened:
|
|
959
|
+
* `buffer` was declared outside the read loop and survived a chunk boundary, but the
|
|
960
|
+
* current event name was declared INSIDE it and reset on every `reader.read()`. A frame is
|
|
961
|
+
* `event: X\ndata: {…}\n\n`, so any frame split between those two lines lost its name,
|
|
962
|
+
* fell through to the OpenAI-shaped branch below, found no `choices[0]`, and was dropped in
|
|
963
|
+
* silence — most often for the largest payloads, which are the ones that do not fit in one
|
|
964
|
+
* read: `alia.title`, `alia.tool_result`, `alia.plan_preview`, `alia.agent_start`,
|
|
965
|
+
* `alia.host_action`. Its test feeds a stream split at every byte offset.
|
|
970
966
|
*/
|
|
971
|
-
const
|
|
967
|
+
const turnId = turnControl.id;
|
|
968
|
+
const client = linked.client;
|
|
969
|
+
const source = createResumableFrameSource({
|
|
970
|
+
response,
|
|
971
|
+
signal: controller.signal,
|
|
972
|
+
reconnect: async (cursor, signal) =>
|
|
973
|
+
client.requestAuthenticatedResponse({
|
|
974
|
+
method: 'GET',
|
|
975
|
+
url: API_ROUTES.chatTurnStream(turnId, {
|
|
976
|
+
cursor,
|
|
977
|
+
...(conversationId && { conversationId }),
|
|
978
|
+
assistantMessageId: assistantMessage.id,
|
|
979
|
+
}),
|
|
980
|
+
headers: { ...(await appAssertionHeaders()), Accept: 'text/event-stream' },
|
|
981
|
+
signal,
|
|
982
|
+
...(streamFetch ? { fetch: streamFetch } : {}),
|
|
983
|
+
}),
|
|
984
|
+
});
|
|
972
985
|
let lastOutputAt = 0;
|
|
973
986
|
/** The server closed the turn: `[DONE]`, or a chunk that carries a finish reason. */
|
|
974
987
|
let sawTerminal = false;
|
|
975
988
|
|
|
976
989
|
while (true) {
|
|
977
|
-
const
|
|
990
|
+
const next = await source.read();
|
|
978
991
|
|
|
979
|
-
if (done) {
|
|
992
|
+
if (next.done) {
|
|
980
993
|
// Flush any remaining batched content before checking
|
|
981
994
|
flushPendingUpdates();
|
|
982
995
|
|
|
@@ -1000,11 +1013,7 @@ export function useConversationStream({
|
|
|
1000
1013
|
break;
|
|
1001
1014
|
}
|
|
1002
1015
|
|
|
1003
|
-
|
|
1004
|
-
// decodes to U+FFFD without it.
|
|
1005
|
-
const chunk = decoder.decode(value, { stream: true });
|
|
1006
|
-
|
|
1007
|
-
for (const frame of sse.push(chunk)) {
|
|
1016
|
+
for (const frame of next.frames) {
|
|
1008
1017
|
const data = frame.data;
|
|
1009
1018
|
const currentEventType = frame.event;
|
|
1010
1019
|
|
|
@@ -1036,6 +1045,26 @@ export function useConversationStream({
|
|
|
1036
1045
|
setMessages((prev) => prev.filter((m) => m.id !== assistantMessage.id));
|
|
1037
1046
|
continue;
|
|
1038
1047
|
}
|
|
1048
|
+
case TURN_RESET_EVENT: {
|
|
1049
|
+
// A snapshot follows (`resumable-stream.ts`): it carries the words, so the
|
|
1050
|
+
// ones this connection built are emptied first and the snapshot applies once.
|
|
1051
|
+
dropPendingUpdates();
|
|
1052
|
+
outputEvidence.realOutputChars = 0;
|
|
1053
|
+
replyText = '';
|
|
1054
|
+
const clearsReasoning = parsed.reasoning === true;
|
|
1055
|
+
setMessages((prev) => {
|
|
1056
|
+
const last = prev[prev.length - 1];
|
|
1057
|
+
if (last?.role !== 'assistant') return prev;
|
|
1058
|
+
const updated = [...prev];
|
|
1059
|
+
updated[updated.length - 1] = {
|
|
1060
|
+
...last,
|
|
1061
|
+
content: '',
|
|
1062
|
+
...(clearsReasoning ? { thinking: '' } : {}),
|
|
1063
|
+
};
|
|
1064
|
+
return updated;
|
|
1065
|
+
});
|
|
1066
|
+
continue;
|
|
1067
|
+
}
|
|
1039
1068
|
case 'alia.reasoning': {
|
|
1040
1069
|
const content = parsed.content;
|
|
1041
1070
|
if (content) {
|
|
@@ -1208,7 +1237,9 @@ export function useConversationStream({
|
|
|
1208
1237
|
const resolved = parseApprovalResolved(parsed);
|
|
1209
1238
|
if (resolved) {
|
|
1210
1239
|
setApprovals((prev) =>
|
|
1211
|
-
withApprovalOutcome(prev, resolved.requestId, resolved.outcome
|
|
1240
|
+
withApprovalOutcome(prev, resolved.requestId, resolved.outcome, {
|
|
1241
|
+
auto: resolved.auto,
|
|
1242
|
+
}),
|
|
1212
1243
|
);
|
|
1213
1244
|
}
|
|
1214
1245
|
continue;
|
|
@@ -1243,7 +1274,7 @@ export function useConversationStream({
|
|
|
1243
1274
|
flushPendingUpdates();
|
|
1244
1275
|
settleAssistant('cancelled');
|
|
1245
1276
|
abortThisTurn();
|
|
1246
|
-
|
|
1277
|
+
source.cancel();
|
|
1247
1278
|
return 'aborted';
|
|
1248
1279
|
}
|
|
1249
1280
|
// An agent's feature gate refused the turn before anything was spent: the host
|
|
@@ -1252,7 +1283,7 @@ export function useConversationStream({
|
|
|
1252
1283
|
if (lockedFeature !== null) {
|
|
1253
1284
|
setIsLoading(false);
|
|
1254
1285
|
abortThisTurn();
|
|
1255
|
-
|
|
1286
|
+
source.cancel();
|
|
1256
1287
|
host.onFeatureLocked?.(lockedFeature);
|
|
1257
1288
|
return settleError();
|
|
1258
1289
|
}
|
|
@@ -1298,7 +1329,7 @@ export function useConversationStream({
|
|
|
1298
1329
|
setError(new Error(msg));
|
|
1299
1330
|
setIsLoading(false);
|
|
1300
1331
|
abortThisTurn();
|
|
1301
|
-
|
|
1332
|
+
source.cancel();
|
|
1302
1333
|
// A turn that failed after it started carries a run reference:
|
|
1303
1334
|
// its card says code and reference, never the server's English
|
|
1304
1335
|
// prose. A refusal without one (a gate, a bad request) keeps its
|
|
@@ -1473,7 +1504,7 @@ export function useConversationStream({
|
|
|
1473
1504
|
abortThisTurn();
|
|
1474
1505
|
|
|
1475
1506
|
// Break out of the streaming loop
|
|
1476
|
-
|
|
1507
|
+
source.cancel();
|
|
1477
1508
|
return keepFailedTurn(true, errMsg);
|
|
1478
1509
|
}
|
|
1479
1510
|
} catch (frameError: unknown) {
|
|
@@ -1579,6 +1610,13 @@ export function useConversationStream({
|
|
|
1579
1610
|
// Settle only this turn's message ids, including when a new send has already
|
|
1580
1611
|
// started in the same conversation. Error paths may have settled them first.
|
|
1581
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
|
+
}
|
|
1582
1620
|
// Retain this control while its ACK drains so repeated Stop remains deduplicated.
|
|
1583
1621
|
// Local transport cleanup is required even after the thread has been replaced.
|
|
1584
1622
|
if (turnControl) await turnControl.finish();
|
|
@@ -1689,9 +1727,14 @@ export function useConversationStream({
|
|
|
1689
1727
|
}, [isLoading, setComposerPhase]);
|
|
1690
1728
|
|
|
1691
1729
|
/** The person's own answer, shown at once; the stream's `alia.cli_permission_resolved` repeats it. */
|
|
1692
|
-
const settleApproval = useCallback(
|
|
1693
|
-
|
|
1694
|
-
|
|
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
|
+
);
|
|
1695
1738
|
|
|
1696
1739
|
/** Put the offer away. It wrote nothing, so there is nothing else to undo. */
|
|
1697
1740
|
const dismissSuggestedNewConversation = useCallback(() => {
|