@stigmer/react-core 0.1.3 → 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/internal.d.ts +1 -0
- package/internal.d.ts.map +1 -1
- package/internal.js +1 -0
- package/internal.js.map +1 -1
- package/models/registry.d.ts +1 -1
- package/package.json +4 -4
- package/run/FileReviewContext.d.ts +17 -2
- package/run/FileReviewContext.d.ts.map +1 -1
- package/run/FileReviewContext.js +25 -0
- package/run/FileReviewContext.js.map +1 -1
- package/run/tool-categories.d.ts +13 -16
- package/run/tool-categories.d.ts.map +1 -1
- package/run/tool-categories.js +17 -30
- package/run/tool-categories.js.map +1 -1
- package/run/useReviewFileChanges.d.ts +2 -1
- package/run/useReviewFileChanges.d.ts.map +1 -1
- package/run/useReviewFileChanges.js +2 -1
- package/run/useReviewFileChanges.js.map +1 -1
- package/run/useRunActions.d.ts +10 -2
- package/run/useRunActions.d.ts.map +1 -1
- package/run/useRunActions.js +6 -2
- package/run/useRunActions.js.map +1 -1
- package/session/useSessionConversation.d.ts +19 -9
- 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/internal.ts +1 -0
- package/src/models/registry.ts +1 -1
- package/src/run/FileReviewContext.ts +27 -2
- package/src/run/tool-categories.ts +30 -46
- package/src/run/useReviewFileChanges.ts +2 -1
- package/src/run/useRunActions.ts +24 -2
- package/src/session/useSessionConversation.ts +153 -62
- package/src/session/useSessionEvents.ts +29 -13
- package/src/test/samples.ts +85 -1
- package/test/samples.d.ts +50 -0
- package/test/samples.d.ts.map +1 -1
- package/test/samples.js +53 -1
- package/test/samples.js.map +1 -1
|
@@ -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
|
/**
|
|
@@ -428,8 +496,8 @@ export interface UseSessionConversationReturn {
|
|
|
428
496
|
/** Decision keys ({@link fileDecisionKey}) currently being submitted. */
|
|
429
497
|
readonly submittingFileDecisionKeys: ReadonlySet<string>;
|
|
430
498
|
/**
|
|
431
|
-
* Per-decision failures, keyed by {@link fileDecisionKey}
|
|
432
|
-
*
|
|
499
|
+
* Per-decision failures, keyed by {@link fileDecisionKey}: the React kit's
|
|
500
|
+
* session viewer says Couldn't undo on the file, with Try again.
|
|
433
501
|
*/
|
|
434
502
|
readonly fileDecisionErrors: ReadonlyMap<string, Error>;
|
|
435
503
|
/** Clear the error for one decision key (e.g. before a retry of that target). */
|
|
@@ -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
|
@@ -239,6 +239,27 @@ export interface PermissionAskedOverrides extends SessionEventOverrides {
|
|
|
239
239
|
export interface RunStatusEventOverrides extends SessionEventOverrides {
|
|
240
240
|
readonly stopCause?: string;
|
|
241
241
|
readonly error?: string;
|
|
242
|
+
/** Which kind of ending the cause was (`StigmerStopDetail`): its kind, and the model or connected app it names. */
|
|
243
|
+
readonly stopDetail?: { readonly kind: string; readonly model?: string; readonly app?: string };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* A background command's status (`stigmer.background_command.status`): it
|
|
248
|
+
* started (`running`, with its time limit), or it ended, how, and by whom.
|
|
249
|
+
*/
|
|
250
|
+
export interface BackgroundCommandStatusOverrides extends SessionEventOverrides {
|
|
251
|
+
/** `running` (the default) when the command starts; `ended` when it stops. */
|
|
252
|
+
readonly phase?: "running" | "ended";
|
|
253
|
+
/** How long it may run, in seconds. Defaults to 600. */
|
|
254
|
+
readonly timeoutSeconds?: number;
|
|
255
|
+
/** How an ended command ended. Defaults to `exited`. */
|
|
256
|
+
readonly ending?: "exited" | "timed_out" | "stopped" | "failed_to_start";
|
|
257
|
+
/** The exit code of a command that exited. */
|
|
258
|
+
readonly exitCode?: number;
|
|
259
|
+
/** Who stopped a stopped command. */
|
|
260
|
+
readonly stoppedBy?: "agent" | "person" | "approval" | "turn";
|
|
261
|
+
/** The output it ended with. */
|
|
262
|
+
readonly output?: string;
|
|
242
263
|
}
|
|
243
264
|
|
|
244
265
|
/** One tool call of a {@link samples.turnEvents} turn: its use and, when it returned, its result. */
|
|
@@ -262,6 +283,14 @@ export interface TurnOverrides {
|
|
|
262
283
|
readonly reply?: string;
|
|
263
284
|
/** The turn's phase (`stigmer.run.status`). Defaults to `completed`. */
|
|
264
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;
|
|
265
294
|
}
|
|
266
295
|
|
|
267
296
|
export interface SkillOverrides {
|
|
@@ -357,6 +386,11 @@ function numbered(events: readonly SessionEvent[]): SessionEvent[] {
|
|
|
357
386
|
// Resource factories
|
|
358
387
|
// ---------------------------------------------------------------------------
|
|
359
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
|
+
|
|
360
394
|
/**
|
|
361
395
|
* Sample data factories for building demo fixtures.
|
|
362
396
|
*
|
|
@@ -693,6 +727,7 @@ export const samples = {
|
|
|
693
727
|
stopCause: o?.stopCause ?? "",
|
|
694
728
|
error: o?.error ?? "",
|
|
695
729
|
processedAt: at(o),
|
|
730
|
+
...(o?.stopDetail === undefined ? {} : { stopDetail: o.stopDetail }),
|
|
696
731
|
},
|
|
697
732
|
});
|
|
698
733
|
},
|
|
@@ -728,7 +763,10 @@ export const samples = {
|
|
|
728
763
|
const reply = o?.reply ?? SAMPLE_REPLY;
|
|
729
764
|
if (reply !== "") events.push(samples.agentMessageEvent(reply, { runId }));
|
|
730
765
|
if (phase !== "running") events.push(samples.runStatusEvent(phase, { runId }));
|
|
731
|
-
|
|
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))));
|
|
732
770
|
},
|
|
733
771
|
|
|
734
772
|
/** A session's events folded (`foldSession`), numbered in order. */
|
|
@@ -766,6 +804,52 @@ export const samples = {
|
|
|
766
804
|
return create(SessionEventListSchema, { events: numbered(events) });
|
|
767
805
|
},
|
|
768
806
|
|
|
807
|
+
/**
|
|
808
|
+
* A background command's status (`stigmer.background_command.status`)
|
|
809
|
+
* for the tool use that started it. Id: `<tool use>.bg.<phase>`.
|
|
810
|
+
*/
|
|
811
|
+
backgroundCommandStatusEvent(toolUseId: string, o?: BackgroundCommandStatusOverrides): SessionEvent {
|
|
812
|
+
const phase = o?.phase ?? "running";
|
|
813
|
+
const ended = phase === "ended";
|
|
814
|
+
return sessionEvent(o, {
|
|
815
|
+
case: "stigmerBackgroundCommandStatus",
|
|
816
|
+
value: {
|
|
817
|
+
id: o?.id ?? `${toolUseId}.bg.${phase}`,
|
|
818
|
+
processedAt: at(o),
|
|
819
|
+
toolUseId,
|
|
820
|
+
sessionThreadId: o?.threadId ?? "",
|
|
821
|
+
phase,
|
|
822
|
+
timeoutSeconds: o?.timeoutSeconds ?? 600,
|
|
823
|
+
ending: ended ? (o?.ending ?? "exited") : "",
|
|
824
|
+
...(o?.exitCode === undefined ? {} : { exitCode: o.exitCode }),
|
|
825
|
+
stoppedBy: ended ? (o?.stoppedBy ?? "") : "",
|
|
826
|
+
output: o?.output ?? "",
|
|
827
|
+
endedAt: ended ? at(o) : "",
|
|
828
|
+
},
|
|
829
|
+
});
|
|
830
|
+
},
|
|
831
|
+
|
|
832
|
+
/**
|
|
833
|
+
* A stream frame carrying a fragment of a running tool's output
|
|
834
|
+
* (`stigmer.tool_output_delta`): streamed, never stored, so a page that
|
|
835
|
+
* replays a session sends it after the stored events.
|
|
836
|
+
*/
|
|
837
|
+
toolOutputDeltaFrame(toolUseId: string, delta: string): StreamSessionEventsResponse {
|
|
838
|
+
return create(StreamSessionEventsResponseSchema, { frame: { case: "stigmerToolOutputDelta", value: { toolUseId, delta } } });
|
|
839
|
+
},
|
|
840
|
+
|
|
841
|
+
/**
|
|
842
|
+
* A stream frame opening a message or thought the agent has begun and
|
|
843
|
+
* not yet stored (`event_start`): streamed, never stored, so a page that
|
|
844
|
+
* replays a session sends it after the stored events. `id` is the one the
|
|
845
|
+
* stored event will carry (`<run>.root.m<index>` or `<run>.root.t<index>`).
|
|
846
|
+
*/
|
|
847
|
+
draftFrame(id: string, kind: "message" | "thinking"): StreamSessionEventsResponse {
|
|
848
|
+
return create(StreamSessionEventsResponseSchema, {
|
|
849
|
+
frame: { case: "eventStart", value: { event: { id, type: kind === "thinking" ? "agent.thinking" : "agent.message" } } },
|
|
850
|
+
});
|
|
851
|
+
},
|
|
852
|
+
|
|
769
853
|
/** A stream frame carrying one stored event (`streamEvents`). */
|
|
770
854
|
eventFrame(event: SessionEvent): StreamSessionEventsResponse {
|
|
771
855
|
return create(StreamSessionEventsResponseSchema, { frame: { case: "event", value: event } });
|
package/test/samples.d.ts
CHANGED
|
@@ -142,6 +142,30 @@ export interface PermissionAskedOverrides extends SessionEventOverrides {
|
|
|
142
142
|
export interface RunStatusEventOverrides extends SessionEventOverrides {
|
|
143
143
|
readonly stopCause?: string;
|
|
144
144
|
readonly error?: string;
|
|
145
|
+
/** Which kind of ending the cause was (`StigmerStopDetail`): its kind, and the model or connected app it names. */
|
|
146
|
+
readonly stopDetail?: {
|
|
147
|
+
readonly kind: string;
|
|
148
|
+
readonly model?: string;
|
|
149
|
+
readonly app?: string;
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* A background command's status (`stigmer.background_command.status`): it
|
|
154
|
+
* started (`running`, with its time limit), or it ended, how, and by whom.
|
|
155
|
+
*/
|
|
156
|
+
export interface BackgroundCommandStatusOverrides extends SessionEventOverrides {
|
|
157
|
+
/** `running` (the default) when the command starts; `ended` when it stops. */
|
|
158
|
+
readonly phase?: "running" | "ended";
|
|
159
|
+
/** How long it may run, in seconds. Defaults to 600. */
|
|
160
|
+
readonly timeoutSeconds?: number;
|
|
161
|
+
/** How an ended command ended. Defaults to `exited`. */
|
|
162
|
+
readonly ending?: "exited" | "timed_out" | "stopped" | "failed_to_start";
|
|
163
|
+
/** The exit code of a command that exited. */
|
|
164
|
+
readonly exitCode?: number;
|
|
165
|
+
/** Who stopped a stopped command. */
|
|
166
|
+
readonly stoppedBy?: "agent" | "person" | "approval" | "turn";
|
|
167
|
+
/** The output it ended with. */
|
|
168
|
+
readonly output?: string;
|
|
145
169
|
}
|
|
146
170
|
/** One tool call of a {@link samples.turnEvents} turn: its use and, when it returned, its result. */
|
|
147
171
|
export interface TurnToolOverrides {
|
|
@@ -163,6 +187,14 @@ export interface TurnOverrides {
|
|
|
163
187
|
readonly reply?: string;
|
|
164
188
|
/** The turn's phase (`stigmer.run.status`). Defaults to `completed`. */
|
|
165
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;
|
|
166
198
|
}
|
|
167
199
|
export interface SkillOverrides {
|
|
168
200
|
readonly id?: string;
|
|
@@ -335,6 +367,24 @@ export declare const samples: {
|
|
|
335
367
|
}) => ToolUse;
|
|
336
368
|
/** A session event list response (`listEvents`), its events numbered in order. */
|
|
337
369
|
readonly sessionEventList: (events: readonly SessionEvent[]) => SessionEventList;
|
|
370
|
+
/**
|
|
371
|
+
* A background command's status (`stigmer.background_command.status`)
|
|
372
|
+
* for the tool use that started it. Id: `<tool use>.bg.<phase>`.
|
|
373
|
+
*/
|
|
374
|
+
readonly backgroundCommandStatusEvent: (toolUseId: string, o?: BackgroundCommandStatusOverrides) => SessionEvent;
|
|
375
|
+
/**
|
|
376
|
+
* A stream frame carrying a fragment of a running tool's output
|
|
377
|
+
* (`stigmer.tool_output_delta`): streamed, never stored, so a page that
|
|
378
|
+
* replays a session sends it after the stored events.
|
|
379
|
+
*/
|
|
380
|
+
readonly toolOutputDeltaFrame: (toolUseId: string, delta: string) => StreamSessionEventsResponse;
|
|
381
|
+
/**
|
|
382
|
+
* A stream frame opening a message or thought the agent has begun and
|
|
383
|
+
* not yet stored (`event_start`): streamed, never stored, so a page that
|
|
384
|
+
* replays a session sends it after the stored events. `id` is the one the
|
|
385
|
+
* stored event will carry (`<run>.root.m<index>` or `<run>.root.t<index>`).
|
|
386
|
+
*/
|
|
387
|
+
readonly draftFrame: (id: string, kind: "message" | "thinking") => StreamSessionEventsResponse;
|
|
338
388
|
/** A stream frame carrying one stored event (`streamEvents`). */
|
|
339
389
|
readonly eventFrame: (event: SessionEvent) => StreamSessionEventsResponse;
|
|
340
390
|
/**
|