@stigmer/react-core 0.2.0 → 0.3.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/internal/store/conversation-store.d.ts +9 -2
- package/internal/store/conversation-store.d.ts.map +1 -1
- package/internal/store/conversation-store.js +25 -11
- package/internal/store/conversation-store.js.map +1 -1
- package/package.json +4 -4
- package/session/useSessionConversation.d.ts +17 -7
- package/session/useSessionConversation.d.ts.map +1 -1
- package/session/useSessionConversation.js +110 -48
- package/session/useSessionConversation.js.map +1 -1
- package/session/useSessionEvents.d.ts +2 -1
- package/session/useSessionEvents.d.ts.map +1 -1
- package/session/useSessionEvents.js +28 -12
- package/session/useSessionEvents.js.map +1 -1
- package/src/internal/store/conversation-store.ts +25 -10
- package/src/session/useSessionConversation.ts +151 -60
- package/src/session/useSessionEvents.ts +29 -13
- package/src/test/samples.ts +17 -1
- package/test/samples.d.ts +8 -0
- package/test/samples.d.ts.map +1 -1
- package/test/samples.js +9 -1
- package/test/samples.js.map +1 -1
|
@@ -33,7 +33,8 @@ const INTERRUPTED_MESSAGE = "The connection to the conversation was interrupted.
|
|
|
33
33
|
* A session follow never completes on its own: a turn ending is read from
|
|
34
34
|
* the fold (`Turn.ended`), never from the stream.
|
|
35
35
|
*
|
|
36
|
-
* Pass `null` to follow nothing. A new id
|
|
36
|
+
* Pass `null` to follow nothing. A new id starts from an empty fold, from
|
|
37
|
+
* the very render that carries it: no render returns another session's turns.
|
|
37
38
|
*
|
|
38
39
|
* @example
|
|
39
40
|
* ```tsx
|
|
@@ -108,25 +109,20 @@ export function useSessionEvents(sessionId, options) {
|
|
|
108
109
|
const reconnectOptions = options?.reconnectOptions;
|
|
109
110
|
const configRef = useRef({ autoReconnect, reconnectOptions });
|
|
110
111
|
configRef.current = { autoReconnect, reconnectOptions };
|
|
111
|
-
// The session the store holds, so another session resets it while a
|
|
112
|
-
// reconnect of the same one keeps the conversation on screen.
|
|
113
|
-
const prevSessionIdRef = useRef(null);
|
|
114
112
|
// controller, store and streamRate are ref-backed and deliberately absent
|
|
115
113
|
// from the dependencies: useStreamRate returns a new object per render.
|
|
116
114
|
// reconnect changes only with the store, so it is absent too.
|
|
117
115
|
useEffect(() => {
|
|
116
|
+
// The store holds one session: another session (or none) starts it over,
|
|
117
|
+
// while a reconnect of the same one keeps the conversation on screen.
|
|
118
|
+
if (store.getSessionId() !== sessionId) {
|
|
119
|
+
store.reset(sessionId);
|
|
120
|
+
connectAutoRetriedRef.current = false;
|
|
121
|
+
}
|
|
118
122
|
if (!sessionId) {
|
|
119
123
|
controller.reset();
|
|
120
|
-
store.reset();
|
|
121
|
-
prevSessionIdRef.current = null;
|
|
122
|
-
connectAutoRetriedRef.current = false;
|
|
123
124
|
return;
|
|
124
125
|
}
|
|
125
|
-
if (prevSessionIdRef.current !== null && prevSessionIdRef.current !== sessionId) {
|
|
126
|
-
store.reset();
|
|
127
|
-
connectAutoRetriedRef.current = false;
|
|
128
|
-
}
|
|
129
|
-
prevSessionIdRef.current = sessionId;
|
|
130
126
|
const abort = new AbortController();
|
|
131
127
|
const { autoReconnect: auto, reconnectOptions: backoff } = configRef.current;
|
|
132
128
|
const maxAttempts = backoff?.maxAttempts ?? DEFAULT_RECONNECT_MAX_ATTEMPTS;
|
|
@@ -211,10 +207,30 @@ export function useSessionEvents(sessionId, options) {
|
|
|
211
207
|
return;
|
|
212
208
|
return onPageReturn(() => wakeRef.current());
|
|
213
209
|
}, [sessionId]);
|
|
210
|
+
const heldSessionId = useSyncExternalStore(store.subscribe, store.getSessionId);
|
|
214
211
|
const fold = useSyncExternalStore(store.subscribe, store.getFold);
|
|
215
212
|
const streamState = useSyncExternalStore(store.subscribe, store.getStreamState);
|
|
216
213
|
const connectTimedOut = useSyncExternalStore(store.subscribe, store.getConnectTimedOut);
|
|
217
214
|
const quiet = useSyncExternalStore(store.subscribe, store.getSlow);
|
|
215
|
+
// The render that carries another session id runs before the effect that
|
|
216
|
+
// starts the store over, so it returns what that effect is about to leave:
|
|
217
|
+
// no fold, and a follow starting (or none, for no session). Never the
|
|
218
|
+
// previous session's turns under the new id (stigmer/stigmer#2921), the
|
|
219
|
+
// rule useFetch keeps for its own identity changes.
|
|
220
|
+
if (heldSessionId !== sessionId) {
|
|
221
|
+
return {
|
|
222
|
+
fold: null,
|
|
223
|
+
isStreaming: false,
|
|
224
|
+
isConnecting: sessionId !== null,
|
|
225
|
+
isReconnecting: false,
|
|
226
|
+
reconnectAttempt: 0,
|
|
227
|
+
error: null,
|
|
228
|
+
connectTimedOut: false,
|
|
229
|
+
isSlow: false,
|
|
230
|
+
reconnect,
|
|
231
|
+
wake,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
218
234
|
// A quiet idle session is not slow: only a turn that is working is.
|
|
219
235
|
const newest = fold?.turns.at(-1);
|
|
220
236
|
const working = newest !== undefined &&
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSessionEvents.js","sourceRoot":"","sources":["../src/session/useSessionEvents.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EACL,eAAe,EACf,WAAW,EACX,SAAS,EACT,MAAM,EACN,QAAQ,EACR,oBAAoB,GACrB,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAoB,MAAM,iBAAiB,CAAC;AACxF,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAC;AACnE,OAAO,EACL,gBAAgB,EAChB,0BAA0B,EAC1B,yBAAyB,GAE1B,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EACL,mBAAmB,EACnB,KAAK,EACL,8BAA8B,EAC9B,iBAAiB,GAElB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,iBAAiB,EAAE,MAAM,yCAAyC,CAAC;AAC5E,OAAO,EAAE,YAAY,EAA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-
// The conversation's live state for one session:
|
|
2
|
-
// folded (`SessionLog` from @stigmer/client), plus the
|
|
3
|
-
// and its two watchdog signals.
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
1
|
+
// The conversation's live state for one session: which session it holds, the
|
|
2
|
+
// session's event log, folded (`SessionLog` from @stigmer/client), plus the
|
|
3
|
+
// follow's lifecycle stage and its two watchdog signals. The session id is
|
|
4
|
+
// read with the rest, so a reader can tell state that belongs to the session
|
|
5
|
+
// it renders from state still left over from another (stigmer/stigmer#2921).
|
|
6
|
+
// Frames go into the log as they arrive; the fold is published to React only
|
|
7
|
+
// when the stream controller flushes, so a burst of frames commits once. The
|
|
8
|
+
// fold itself keeps every turn, item and thread object no event touched, so a
|
|
9
|
+
// published snapshot shares unchanged objects by reference with the one before
|
|
10
|
+
// it and only the changed rows re-render. Implements the contract
|
|
11
|
+
// `useSyncExternalStore` reads.
|
|
9
12
|
|
|
10
13
|
import { useRef } from "react";
|
|
11
14
|
import { SessionLog, type SessionFold } from "@stigmer/client";
|
|
@@ -56,6 +59,7 @@ export type Listener = () => void;
|
|
|
56
59
|
* they can read changed; every getter returns a stable reference.
|
|
57
60
|
*/
|
|
58
61
|
export class ConversationStore {
|
|
62
|
+
private _sessionId: string | null = null;
|
|
59
63
|
private _log = new SessionLog();
|
|
60
64
|
private _fold: SessionFold | null = null;
|
|
61
65
|
private _streamState: StreamState = IDLE_STATE;
|
|
@@ -119,13 +123,19 @@ export class ConversationStore {
|
|
|
119
123
|
this._notify();
|
|
120
124
|
}
|
|
121
125
|
|
|
122
|
-
/**
|
|
123
|
-
|
|
126
|
+
/**
|
|
127
|
+
* Start over with an empty log, holding `sessionId` (none when omitted).
|
|
128
|
+
* Listeners hear it when anything they can read changed, the held session
|
|
129
|
+
* included, even for a store that was already empty.
|
|
130
|
+
*/
|
|
131
|
+
reset(sessionId: string | null = null): void {
|
|
124
132
|
const wasClean =
|
|
133
|
+
this._sessionId === sessionId &&
|
|
125
134
|
this._fold === null &&
|
|
126
135
|
this._streamState.stage === "idle" &&
|
|
127
136
|
!this._connectTimedOut &&
|
|
128
137
|
!this._isSlow;
|
|
138
|
+
this._sessionId = sessionId;
|
|
129
139
|
this._log = new SessionLog();
|
|
130
140
|
this._fold = null;
|
|
131
141
|
this._streamState = IDLE_STATE;
|
|
@@ -144,6 +154,11 @@ export class ConversationStore {
|
|
|
144
154
|
};
|
|
145
155
|
};
|
|
146
156
|
|
|
157
|
+
/** The session the store holds, as the last {@link reset} named it. */
|
|
158
|
+
getSessionId = (): string | null => {
|
|
159
|
+
return this._sessionId;
|
|
160
|
+
};
|
|
161
|
+
|
|
147
162
|
/** The published fold, or `null` before the first publish. */
|
|
148
163
|
getFold = (): SessionFold | null => {
|
|
149
164
|
return this._fold;
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
supersededRunIds,
|
|
14
14
|
buildSessionProto,
|
|
15
15
|
getErrorReason,
|
|
16
|
+
resolvedSubject,
|
|
16
17
|
type FileReviewSet,
|
|
17
18
|
type QueuedMessage,
|
|
18
19
|
type ResourceRef,
|
|
@@ -56,6 +57,14 @@ import { useLocalSessionWorker } from "./useLocalSessionWorker.js";
|
|
|
56
57
|
*/
|
|
57
58
|
const LIVE_RUN_REFRESH_MS = 3_000;
|
|
58
59
|
|
|
60
|
+
/**
|
|
61
|
+
* How long after a turn ends its session is read once more for the generated
|
|
62
|
+
* subject, when the turn's phase changes did not bring it: the title is made
|
|
63
|
+
* by one short model call that starts with the turn, so a turn that ends
|
|
64
|
+
* first is usually overtaken within seconds.
|
|
65
|
+
*/
|
|
66
|
+
const SUBJECT_SETTLE_MS = 6_000;
|
|
67
|
+
|
|
59
68
|
/** The phases a turn can be stopped from: every unfinished one (the session's interrupt stops them all). */
|
|
60
69
|
const STOPPABLE_PHASES: ReadonlySet<TurnPhase> = new Set<TurnPhase>([
|
|
61
70
|
"pending",
|
|
@@ -72,6 +81,55 @@ const STOPPABLE_PHASES: ReadonlySet<TurnPhase> = new Set<TurnPhase>([
|
|
|
72
81
|
*/
|
|
73
82
|
const SESSION_EDIT_ATTEMPTS = 3;
|
|
74
83
|
|
|
84
|
+
/**
|
|
85
|
+
* One conversation's send, from submit until its turn shows in the events,
|
|
86
|
+
* or until it fails and the person retries or moves on: what the thread
|
|
87
|
+
* shows before the events carry it, and what Retry sends again. Held per
|
|
88
|
+
* conversation, because the session viewer stays mounted while the person
|
|
89
|
+
* moves between conversations: a send belongs to the conversation it was
|
|
90
|
+
* written in, whichever one is on screen when it settles
|
|
91
|
+
* (stigmer/stigmer#3225).
|
|
92
|
+
*/
|
|
93
|
+
interface PendingSend {
|
|
94
|
+
/** Between submit and the server holding the message. */
|
|
95
|
+
readonly inFlight: boolean;
|
|
96
|
+
/** The run the send created, until its turn shows in the events. */
|
|
97
|
+
readonly runId: string | null;
|
|
98
|
+
/** The message the send queued for a working turn, until the events show it. */
|
|
99
|
+
readonly messageId: string | null;
|
|
100
|
+
/** The text shown before the events carry it; kept when the send fails. */
|
|
101
|
+
readonly userMessage: string | null;
|
|
102
|
+
/** The files sent with it, in lockstep with the text. */
|
|
103
|
+
readonly files: readonly MessageFileView[] | null;
|
|
104
|
+
/** Why the send failed, on either path: the session update or the send. */
|
|
105
|
+
readonly error: Error | null;
|
|
106
|
+
/** The send's arguments, for an exact Retry. */
|
|
107
|
+
readonly last: { readonly message: string; readonly options?: SendFollowUpOptions } | null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const NO_PENDING_SEND: PendingSend = {
|
|
111
|
+
inFlight: false,
|
|
112
|
+
runId: null,
|
|
113
|
+
messageId: null,
|
|
114
|
+
userMessage: null,
|
|
115
|
+
files: null,
|
|
116
|
+
error: null,
|
|
117
|
+
last: null,
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const NO_PENDING_SENDS: ReadonlyMap<string, PendingSend> = new Map();
|
|
121
|
+
|
|
122
|
+
/** Whether nothing of a send is left to show or retry: its slot can go. */
|
|
123
|
+
function isSettled(send: PendingSend): boolean {
|
|
124
|
+
return (
|
|
125
|
+
!send.inFlight &&
|
|
126
|
+
send.runId === null &&
|
|
127
|
+
send.messageId === null &&
|
|
128
|
+
send.userMessage === null &&
|
|
129
|
+
send.error === null
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
75
133
|
const EMPTY_ASKS: readonly ToolUse[] = [];
|
|
76
134
|
const EMPTY_SETS: readonly FileReviewSet[] = [];
|
|
77
135
|
const EMPTY_QUEUED: readonly QueuedMessage[] = [];
|
|
@@ -288,9 +346,16 @@ export interface UseSessionConversationReturn {
|
|
|
288
346
|
message: string,
|
|
289
347
|
options?: SendFollowUpOptions,
|
|
290
348
|
) => Promise<void>;
|
|
291
|
-
/**
|
|
349
|
+
/**
|
|
350
|
+
* True when the input should be enabled: this conversation's send is not
|
|
351
|
+
* on its way, and no turn waits for an answer or is paused.
|
|
352
|
+
*/
|
|
292
353
|
readonly canSendFollowUp: boolean;
|
|
293
|
-
/**
|
|
354
|
+
/**
|
|
355
|
+
* True while this conversation's send is on its way: from submit until the
|
|
356
|
+
* server holds the message, the session update a send may carry included.
|
|
357
|
+
* Another conversation's send never shows here.
|
|
358
|
+
*/
|
|
294
359
|
readonly isSending: boolean;
|
|
295
360
|
/**
|
|
296
361
|
* Error from the last `sendFollowUp` attempt, or `null`.
|
|
@@ -298,15 +363,18 @@ export interface UseSessionConversationReturn {
|
|
|
298
363
|
* Covers **both** failing paths — the optional `session.update()` and the
|
|
299
364
|
* `create()` RPC — so a follow-up never fails silently. When set, the user's
|
|
300
365
|
* message is preserved (see {@link pendingUserMessage}) and can be re-sent
|
|
301
|
-
* via {@link retryLastSend}.
|
|
366
|
+
* via {@link retryLastSend}. It belongs to the conversation the send was
|
|
367
|
+
* written in: it waits there while the person is in another, and shows
|
|
368
|
+
* again on their return.
|
|
302
369
|
*/
|
|
303
370
|
readonly sendError: Error | null;
|
|
304
371
|
/** Reset `sendError` to `null` (keeps the preserved pending message). */
|
|
305
372
|
readonly clearSendError: () => void;
|
|
306
373
|
/**
|
|
307
|
-
* Re-send
|
|
308
|
-
*
|
|
309
|
-
* turn
|
|
374
|
+
* Re-send this conversation's most recent `sendFollowUp` (same message and
|
|
375
|
+
* options), into this conversation. No-op when it has none to retry: nothing
|
|
376
|
+
* sent, or the sent turn already arrived. Use as the "Retry" affordance on a
|
|
377
|
+
* failed turn; clears {@link sendError} for the new attempt.
|
|
310
378
|
*/
|
|
311
379
|
readonly retryLastSend: () => void;
|
|
312
380
|
|
|
@@ -325,7 +393,7 @@ export interface UseSessionConversationReturn {
|
|
|
325
393
|
* The user's message text, shown in the thread before the session's
|
|
326
394
|
* events carry it. Retained when a send fails so the typed message is
|
|
327
395
|
* never lost — pair with {@link sendError} to render the turn as failed
|
|
328
|
-
* with a retry control.
|
|
396
|
+
* with a retry control. This conversation's own, like the send's error.
|
|
329
397
|
*/
|
|
330
398
|
readonly pendingUserMessage: string | null;
|
|
331
399
|
/**
|
|
@@ -567,7 +635,6 @@ export function useSessionConversation(
|
|
|
567
635
|
});
|
|
568
636
|
const {
|
|
569
637
|
send: sendMessage,
|
|
570
|
-
isCreating,
|
|
571
638
|
clearError: clearCreateError,
|
|
572
639
|
} = useCreateRun();
|
|
573
640
|
const { update: updateSession } = useUpdateSession();
|
|
@@ -603,29 +670,25 @@ export function useSessionConversation(
|
|
|
603
670
|
const foldTurns = events.fold?.turns;
|
|
604
671
|
const wakeFollow = events.wake;
|
|
605
672
|
|
|
606
|
-
//
|
|
607
|
-
//
|
|
608
|
-
|
|
609
|
-
//
|
|
610
|
-
//
|
|
611
|
-
const [
|
|
612
|
-
const
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
const
|
|
625
|
-
const lastSendRef = useRef<{
|
|
626
|
-
message: string;
|
|
627
|
-
options?: SendFollowUpOptions;
|
|
628
|
-
} | null>(null);
|
|
673
|
+
// Each conversation's send, by session id (PendingSend). A render reads
|
|
674
|
+
// the slot of the session it shows, so another conversation's send never
|
|
675
|
+
// shows here; a write names the session it belongs to, so a send that
|
|
676
|
+
// settles after a switch lands on its own conversation. A slot goes once
|
|
677
|
+
// nothing of its send is left to show or retry.
|
|
678
|
+
const [pendingSends, setPendingSends] = useState(NO_PENDING_SENDS);
|
|
679
|
+
const updatePendingSend = useCallback((id: string, patch: Partial<PendingSend> | null) => {
|
|
680
|
+
setPendingSends((held) => {
|
|
681
|
+
const next = patch === null ? NO_PENDING_SEND : { ...(held.get(id) ?? NO_PENDING_SEND), ...patch };
|
|
682
|
+
if (isSettled(next) && !held.has(id)) return held;
|
|
683
|
+
const sends = new Map(held);
|
|
684
|
+
if (isSettled(next)) sends.delete(id);
|
|
685
|
+
else sends.set(id, next);
|
|
686
|
+
return sends;
|
|
687
|
+
});
|
|
688
|
+
}, []);
|
|
689
|
+
const pendingSend = (sessionId !== null ? pendingSends.get(sessionId) : undefined) ?? NO_PENDING_SEND;
|
|
690
|
+
const pendingRunId = pendingSend.runId;
|
|
691
|
+
const pendingMessageId = pendingSend.messageId;
|
|
629
692
|
|
|
630
693
|
const listedById = useMemo(() => {
|
|
631
694
|
const map = new Map<string, Run>();
|
|
@@ -671,13 +734,9 @@ export function useSessionConversation(
|
|
|
671
734
|
(pendingRunId !== null && (foldTurns ?? []).some((turn) => turn.runId === pendingRunId)) ||
|
|
672
735
|
(pendingMessageId !== null && holdsMessage(foldTurns, foldQueued, pendingMessageId));
|
|
673
736
|
useEffect(() => {
|
|
674
|
-
if (!sentTurnArrived) return;
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
setPendingUserMessage(null);
|
|
678
|
-
setPendingFiles(null);
|
|
679
|
-
setSendError(null);
|
|
680
|
-
}, [sentTurnArrived]);
|
|
737
|
+
if (!sentTurnArrived || sessionId === null) return;
|
|
738
|
+
updatePendingSend(sessionId, null);
|
|
739
|
+
}, [sentTurnArrived, sessionId, updatePendingSend]);
|
|
681
740
|
|
|
682
741
|
// The run list is read again when, after it has loaded, the events show a
|
|
683
742
|
// turn it does not hold (another client's message; a send reads the list
|
|
@@ -776,7 +835,7 @@ export function useSessionConversation(
|
|
|
776
835
|
// queue, and the working turn reads it at its next step. Only a turn that
|
|
777
836
|
// waits for an answer or is paused holds the composer closed (the server
|
|
778
837
|
// takes only the answer, or the resume, then).
|
|
779
|
-
const canSendFollowUp = !
|
|
838
|
+
const canSendFollowUp = !pendingSend.inFlight && activePhase !== "waiting" && activePhase !== "paused";
|
|
780
839
|
const queued = foldQueued ?? EMPTY_QUEUED;
|
|
781
840
|
|
|
782
841
|
const resources = useMemo<readonly SessionResource[]>(
|
|
@@ -802,18 +861,22 @@ export function useSessionConversation(
|
|
|
802
861
|
// events arrive as they are written.
|
|
803
862
|
wakeFollow();
|
|
804
863
|
|
|
864
|
+
// Every write below names `sessionId`, the conversation this send was
|
|
865
|
+
// written in (this callback's own), so a send that settles after the
|
|
866
|
+
// person moved to another conversation lands on its own.
|
|
867
|
+
//
|
|
805
868
|
// Capture for retry and clear any prior failure before the new attempt.
|
|
806
|
-
lastSendRef.current = { message, options };
|
|
807
|
-
setSendError(null);
|
|
808
869
|
// A Build-from-plan turn shows no optimistic bubble: its message is a
|
|
809
870
|
// machine-written label, not user prose, and the thread hides the turn
|
|
810
871
|
// entirely (the plan card's "Starting build…" state covers the send
|
|
811
872
|
// window). It is still set on FAILURE below, so a failed build send
|
|
812
873
|
// renders the failed-with-retry bubble instead of vanishing.
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
874
|
+
updatePendingSend(sessionId, {
|
|
875
|
+
inFlight: true,
|
|
876
|
+
error: null,
|
|
877
|
+
last: { message, options },
|
|
878
|
+
...(options?.buildFromPlan ? {} : { userMessage: message, files: options?.files ?? null }),
|
|
879
|
+
});
|
|
817
880
|
|
|
818
881
|
try {
|
|
819
882
|
const needsSessionUpdate =
|
|
@@ -867,10 +930,10 @@ export function useSessionConversation(
|
|
|
867
930
|
supersedesRunId: options?.supersedesRunId,
|
|
868
931
|
});
|
|
869
932
|
if (result.runId === null) {
|
|
870
|
-
|
|
933
|
+
updatePendingSend(sessionId, { messageId: result.messageId });
|
|
871
934
|
} else {
|
|
872
935
|
requestedRunIdsRef.current.add(result.runId);
|
|
873
|
-
|
|
936
|
+
updatePendingSend(sessionId, { runId: result.runId });
|
|
874
937
|
refetch();
|
|
875
938
|
}
|
|
876
939
|
// The run exists and names the Files: the originals the
|
|
@@ -883,15 +946,19 @@ export function useSessionConversation(
|
|
|
883
946
|
// build turn this is the FIRST time the message is set — failure is
|
|
884
947
|
// the one case where its label must become visible. The files
|
|
885
948
|
// ride along: the failed bubble's chips are what Retry will re-send.
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
949
|
+
updatePendingSend(sessionId, {
|
|
950
|
+
userMessage: message,
|
|
951
|
+
files: options?.files ?? null,
|
|
952
|
+
error: toError(err),
|
|
953
|
+
});
|
|
889
954
|
if (process.env.NODE_ENV !== "production") {
|
|
890
955
|
console.error("[useSessionConversation] sendFollowUp failed:", err);
|
|
891
956
|
}
|
|
957
|
+
} finally {
|
|
958
|
+
updatePendingSend(sessionId, { inFlight: false });
|
|
892
959
|
}
|
|
893
960
|
},
|
|
894
|
-
[sessionId, session, turnOrg, stigmer, sendMessage, updateSession, refetch, refetchSession, wakeFollow],
|
|
961
|
+
[sessionId, session, turnOrg, stigmer, sendMessage, updateSession, refetch, refetchSession, wakeFollow, updatePendingSend],
|
|
895
962
|
);
|
|
896
963
|
|
|
897
964
|
const moveToCurrentAgentVersion = useCallback(async (): Promise<void> => {
|
|
@@ -908,15 +975,15 @@ export function useSessionConversation(
|
|
|
908
975
|
refetchSession();
|
|
909
976
|
}, [sessionId, stigmer, updateSession, refetchSession]);
|
|
910
977
|
|
|
978
|
+
const lastSend = pendingSend.last;
|
|
911
979
|
const retryLastSend = useCallback(() => {
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
}, [sendFollowUp]);
|
|
980
|
+
if (lastSend) void sendFollowUp(lastSend.message, lastSend.options);
|
|
981
|
+
}, [lastSend, sendFollowUp]);
|
|
915
982
|
|
|
916
983
|
const clearSendError = useCallback(() => {
|
|
917
|
-
|
|
984
|
+
if (sessionId !== null) updatePendingSend(sessionId, { error: null });
|
|
918
985
|
clearCreateError();
|
|
919
|
-
}, [clearCreateError]);
|
|
986
|
+
}, [sessionId, updatePendingSend, clearCreateError]);
|
|
920
987
|
|
|
921
988
|
const asks = liveTurn?.asks ?? EMPTY_ASKS;
|
|
922
989
|
|
|
@@ -983,6 +1050,30 @@ export function useSessionConversation(
|
|
|
983
1050
|
}
|
|
984
1051
|
}, [liveRunId, livePhase, refetchLiveRun]);
|
|
985
1052
|
|
|
1053
|
+
// The session's subject is written by an activity the run's workflow
|
|
1054
|
+
// starts beside the turn and never waits on, and no stream carries it, so a
|
|
1055
|
+
// conversation left open would keep the placeholder (stigmer#3199). While
|
|
1056
|
+
// the session still holds it, the session is read again whenever the live
|
|
1057
|
+
// turn changes phase, and once more SUBJECT_SETTLE_MS after a turn seen
|
|
1058
|
+
// live ends; then it is left alone.
|
|
1059
|
+
const subjectPending = session !== null && resolvedSubject(session.spec?.subject) === null;
|
|
1060
|
+
const refetchSessionRef = useRef(refetchSession);
|
|
1061
|
+
refetchSessionRef.current = refetchSession;
|
|
1062
|
+
const previousSubjectPhaseRef = useRef<TurnPhase | undefined>(undefined);
|
|
1063
|
+
useEffect(() => {
|
|
1064
|
+
const previous = previousSubjectPhaseRef.current;
|
|
1065
|
+
previousSubjectPhaseRef.current = livePhase;
|
|
1066
|
+
if (subjectPending && livePhase !== undefined && previous !== livePhase) refetchSessionRef.current();
|
|
1067
|
+
}, [livePhase, subjectPending]);
|
|
1068
|
+
const previousSubjectRunRef = useRef<string | null>(null);
|
|
1069
|
+
useEffect(() => {
|
|
1070
|
+
const previous = previousSubjectRunRef.current;
|
|
1071
|
+
previousSubjectRunRef.current = liveRunId;
|
|
1072
|
+
if (!subjectPending || previous === null || previous === liveRunId) return;
|
|
1073
|
+
const timer = setTimeout(() => refetchSessionRef.current(), SUBJECT_SETTLE_MS);
|
|
1074
|
+
return () => clearTimeout(timer);
|
|
1075
|
+
}, [liveRunId, subjectPending]);
|
|
1076
|
+
|
|
986
1077
|
// A session whose runs are listed has events to show: it loads until the
|
|
987
1078
|
// first of them is folded (or the follow fails or times out). A session
|
|
988
1079
|
// with no runs has none to wait for.
|
|
@@ -1008,14 +1099,14 @@ export function useSessionConversation(
|
|
|
1008
1099
|
|
|
1009
1100
|
sendFollowUp,
|
|
1010
1101
|
canSendFollowUp,
|
|
1011
|
-
isSending:
|
|
1012
|
-
sendError,
|
|
1102
|
+
isSending: pendingSend.inFlight,
|
|
1103
|
+
sendError: pendingSend.error,
|
|
1013
1104
|
clearSendError,
|
|
1014
1105
|
retryLastSend,
|
|
1015
1106
|
moveToCurrentAgentVersion,
|
|
1016
1107
|
|
|
1017
|
-
pendingUserMessage,
|
|
1018
|
-
pendingFiles,
|
|
1108
|
+
pendingUserMessage: pendingSend.userMessage,
|
|
1109
|
+
pendingFiles: pendingSend.files,
|
|
1019
1110
|
queued,
|
|
1020
1111
|
|
|
1021
1112
|
resources,
|
|
@@ -149,7 +149,8 @@ const INTERRUPTED_MESSAGE = "The connection to the conversation was interrupted.
|
|
|
149
149
|
* A session follow never completes on its own: a turn ending is read from
|
|
150
150
|
* the fold (`Turn.ended`), never from the stream.
|
|
151
151
|
*
|
|
152
|
-
* Pass `null` to follow nothing. A new id
|
|
152
|
+
* Pass `null` to follow nothing. A new id starts from an empty fold, from
|
|
153
|
+
* the very render that carries it: no render returns another session's turns.
|
|
153
154
|
*
|
|
154
155
|
* @example
|
|
155
156
|
* ```tsx
|
|
@@ -235,26 +236,20 @@ export function useSessionEvents(
|
|
|
235
236
|
const configRef = useRef({ autoReconnect, reconnectOptions });
|
|
236
237
|
configRef.current = { autoReconnect, reconnectOptions };
|
|
237
238
|
|
|
238
|
-
// The session the store holds, so another session resets it while a
|
|
239
|
-
// reconnect of the same one keeps the conversation on screen.
|
|
240
|
-
const prevSessionIdRef = useRef<string | null>(null);
|
|
241
|
-
|
|
242
239
|
// controller, store and streamRate are ref-backed and deliberately absent
|
|
243
240
|
// from the dependencies: useStreamRate returns a new object per render.
|
|
244
241
|
// reconnect changes only with the store, so it is absent too.
|
|
245
242
|
useEffect(() => {
|
|
243
|
+
// The store holds one session: another session (or none) starts it over,
|
|
244
|
+
// while a reconnect of the same one keeps the conversation on screen.
|
|
245
|
+
if (store.getSessionId() !== sessionId) {
|
|
246
|
+
store.reset(sessionId);
|
|
247
|
+
connectAutoRetriedRef.current = false;
|
|
248
|
+
}
|
|
246
249
|
if (!sessionId) {
|
|
247
250
|
controller.reset();
|
|
248
|
-
store.reset();
|
|
249
|
-
prevSessionIdRef.current = null;
|
|
250
|
-
connectAutoRetriedRef.current = false;
|
|
251
251
|
return;
|
|
252
252
|
}
|
|
253
|
-
if (prevSessionIdRef.current !== null && prevSessionIdRef.current !== sessionId) {
|
|
254
|
-
store.reset();
|
|
255
|
-
connectAutoRetriedRef.current = false;
|
|
256
|
-
}
|
|
257
|
-
prevSessionIdRef.current = sessionId;
|
|
258
253
|
|
|
259
254
|
const abort = new AbortController();
|
|
260
255
|
const { autoReconnect: auto, reconnectOptions: backoff } = configRef.current;
|
|
@@ -338,11 +333,32 @@ export function useSessionEvents(
|
|
|
338
333
|
return onPageReturn(() => wakeRef.current());
|
|
339
334
|
}, [sessionId]);
|
|
340
335
|
|
|
336
|
+
const heldSessionId = useSyncExternalStore(store.subscribe, store.getSessionId);
|
|
341
337
|
const fold = useSyncExternalStore(store.subscribe, store.getFold);
|
|
342
338
|
const streamState = useSyncExternalStore(store.subscribe, store.getStreamState);
|
|
343
339
|
const connectTimedOut = useSyncExternalStore(store.subscribe, store.getConnectTimedOut);
|
|
344
340
|
const quiet = useSyncExternalStore(store.subscribe, store.getSlow);
|
|
345
341
|
|
|
342
|
+
// The render that carries another session id runs before the effect that
|
|
343
|
+
// starts the store over, so it returns what that effect is about to leave:
|
|
344
|
+
// no fold, and a follow starting (or none, for no session). Never the
|
|
345
|
+
// previous session's turns under the new id (stigmer/stigmer#2921), the
|
|
346
|
+
// rule useFetch keeps for its own identity changes.
|
|
347
|
+
if (heldSessionId !== sessionId) {
|
|
348
|
+
return {
|
|
349
|
+
fold: null,
|
|
350
|
+
isStreaming: false,
|
|
351
|
+
isConnecting: sessionId !== null,
|
|
352
|
+
isReconnecting: false,
|
|
353
|
+
reconnectAttempt: 0,
|
|
354
|
+
error: null,
|
|
355
|
+
connectTimedOut: false,
|
|
356
|
+
isSlow: false,
|
|
357
|
+
reconnect,
|
|
358
|
+
wake,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
|
|
346
362
|
// A quiet idle session is not slow: only a turn that is working is.
|
|
347
363
|
const newest = fold?.turns.at(-1);
|
|
348
364
|
const working =
|
package/src/test/samples.ts
CHANGED
|
@@ -283,6 +283,14 @@ export interface TurnOverrides {
|
|
|
283
283
|
readonly reply?: string;
|
|
284
284
|
/** The turn's phase (`stigmer.run.status`). Defaults to `completed`. */
|
|
285
285
|
readonly phase?: TurnPhase;
|
|
286
|
+
/**
|
|
287
|
+
* How long the turn took, in milliseconds. When set, its events are
|
|
288
|
+
* spread evenly from {@link SAMPLE_INSTANT} to that span after it, the
|
|
289
|
+
* person's message first and the last event at its end, so a finished
|
|
290
|
+
* turn reads its time ("42s") and its steps theirs. Unset, every event
|
|
291
|
+
* is stamped at {@link SAMPLE_INSTANT}.
|
|
292
|
+
*/
|
|
293
|
+
readonly durationMs?: number;
|
|
286
294
|
}
|
|
287
295
|
|
|
288
296
|
export interface SkillOverrides {
|
|
@@ -378,6 +386,11 @@ function numbered(events: readonly SessionEvent[]): SessionEvent[] {
|
|
|
378
386
|
// Resource factories
|
|
379
387
|
// ---------------------------------------------------------------------------
|
|
380
388
|
|
|
389
|
+
/** `e` with its event's `processedAt` set to `at`, every other field as it was (every event a turn is built from carries one). */
|
|
390
|
+
function stampedAt(e: SessionEvent, at: string): SessionEvent {
|
|
391
|
+
return { ...e, event: { ...e.event, value: { ...e.event.value, processedAt: at } } } as SessionEvent;
|
|
392
|
+
}
|
|
393
|
+
|
|
381
394
|
/**
|
|
382
395
|
* Sample data factories for building demo fixtures.
|
|
383
396
|
*
|
|
@@ -750,7 +763,10 @@ export const samples = {
|
|
|
750
763
|
const reply = o?.reply ?? SAMPLE_REPLY;
|
|
751
764
|
if (reply !== "") events.push(samples.agentMessageEvent(reply, { runId }));
|
|
752
765
|
if (phase !== "running") events.push(samples.runStatusEvent(phase, { runId }));
|
|
753
|
-
|
|
766
|
+
const span = o?.durationMs;
|
|
767
|
+
if (span === undefined || events.length < 2) return events;
|
|
768
|
+
const step = span / (events.length - 1);
|
|
769
|
+
return events.map((e, index) => stampedAt(e, sampleInstant(Math.round(index * step))));
|
|
754
770
|
},
|
|
755
771
|
|
|
756
772
|
/** A session's events folded (`foldSession`), numbered in order. */
|
package/test/samples.d.ts
CHANGED
|
@@ -187,6 +187,14 @@ export interface TurnOverrides {
|
|
|
187
187
|
readonly reply?: string;
|
|
188
188
|
/** The turn's phase (`stigmer.run.status`). Defaults to `completed`. */
|
|
189
189
|
readonly phase?: TurnPhase;
|
|
190
|
+
/**
|
|
191
|
+
* How long the turn took, in milliseconds. When set, its events are
|
|
192
|
+
* spread evenly from {@link SAMPLE_INSTANT} to that span after it, the
|
|
193
|
+
* person's message first and the last event at its end, so a finished
|
|
194
|
+
* turn reads its time ("42s") and its steps theirs. Unset, every event
|
|
195
|
+
* is stamped at {@link SAMPLE_INSTANT}.
|
|
196
|
+
*/
|
|
197
|
+
readonly durationMs?: number;
|
|
190
198
|
}
|
|
191
199
|
export interface SkillOverrides {
|
|
192
200
|
readonly id?: string;
|