@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.
Files changed (48) hide show
  1. package/internal/store/conversation-store.d.ts +9 -2
  2. package/internal/store/conversation-store.d.ts.map +1 -1
  3. package/internal/store/conversation-store.js +25 -11
  4. package/internal/store/conversation-store.js.map +1 -1
  5. package/internal.d.ts +1 -0
  6. package/internal.d.ts.map +1 -1
  7. package/internal.js +1 -0
  8. package/internal.js.map +1 -1
  9. package/models/registry.d.ts +1 -1
  10. package/package.json +4 -4
  11. package/run/FileReviewContext.d.ts +17 -2
  12. package/run/FileReviewContext.d.ts.map +1 -1
  13. package/run/FileReviewContext.js +25 -0
  14. package/run/FileReviewContext.js.map +1 -1
  15. package/run/tool-categories.d.ts +13 -16
  16. package/run/tool-categories.d.ts.map +1 -1
  17. package/run/tool-categories.js +17 -30
  18. package/run/tool-categories.js.map +1 -1
  19. package/run/useReviewFileChanges.d.ts +2 -1
  20. package/run/useReviewFileChanges.d.ts.map +1 -1
  21. package/run/useReviewFileChanges.js +2 -1
  22. package/run/useReviewFileChanges.js.map +1 -1
  23. package/run/useRunActions.d.ts +10 -2
  24. package/run/useRunActions.d.ts.map +1 -1
  25. package/run/useRunActions.js +6 -2
  26. package/run/useRunActions.js.map +1 -1
  27. package/session/useSessionConversation.d.ts +19 -9
  28. package/session/useSessionConversation.d.ts.map +1 -1
  29. package/session/useSessionConversation.js +110 -48
  30. package/session/useSessionConversation.js.map +1 -1
  31. package/session/useSessionEvents.d.ts +2 -1
  32. package/session/useSessionEvents.d.ts.map +1 -1
  33. package/session/useSessionEvents.js +28 -12
  34. package/session/useSessionEvents.js.map +1 -1
  35. package/src/internal/store/conversation-store.ts +25 -10
  36. package/src/internal.ts +1 -0
  37. package/src/models/registry.ts +1 -1
  38. package/src/run/FileReviewContext.ts +27 -2
  39. package/src/run/tool-categories.ts +30 -46
  40. package/src/run/useReviewFileChanges.ts +2 -1
  41. package/src/run/useRunActions.ts +24 -2
  42. package/src/session/useSessionConversation.ts +153 -62
  43. package/src/session/useSessionEvents.ts +29 -13
  44. package/src/test/samples.ts +85 -1
  45. package/test/samples.d.ts +50 -0
  46. package/test/samples.d.ts.map +1 -1
  47. package/test/samples.js +53 -1
  48. package/test/samples.js.map +1 -1
@@ -1,11 +1,14 @@
1
- // The conversation's live state for one session: the session's event log,
2
- // folded (`SessionLog` from @stigmer/client), plus the follow's lifecycle stage
3
- // and its two watchdog signals. Frames go into the log as they arrive; the
4
- // fold is published to React only when the stream controller flushes, so a
5
- // burst of frames commits once. The fold itself keeps every turn, item and
6
- // thread object no event touched, so a published snapshot shares unchanged
7
- // objects by reference with the one before it and only the changed rows
8
- // re-render. Implements the contract `useSyncExternalStore` reads.
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
- /** Start over with an empty log, for another session. */
123
- reset(): void {
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;
package/src/internal.ts CHANGED
@@ -58,6 +58,7 @@ export {
58
58
  export type { ToolChrome } from "./run/tool-categories.js";
59
59
  export { runIdFromStorageKey } from "./run/useFileChangeContent.js";
60
60
  export { RunnerAdapterContext } from "./runner-adapter.js";
61
+ export { useFileReviewRowFile } from "./run/FileReviewContext.js";
61
62
  export { useResourceCount, useResourceList, useResourceSearch } from "./search/index.js";
62
63
  export { fromProtoExecutionTarget, toProtoExecutionTarget } from "./session/execution-target.js";
63
64
  export { mapPatchHunks } from "./version-history/diff-hunks.js";
@@ -69,7 +69,7 @@ export const DISABLED_PROVIDERS: ReadonlySet<Provider> = new Set([
69
69
  * ```
70
70
  */
71
71
  export interface ModelInfo {
72
- /** Unique model identifier sent to the backend (e.g. `"claude-sonnet-4.5"`). */
72
+ /** Unique model identifier sent to the backend (e.g. `"claude-sonnet-4.6"`). */
73
73
  readonly modelId: string;
74
74
  /** LLM provider that serves this model. */
75
75
  readonly provider: Provider;
@@ -3,7 +3,7 @@ import type {
3
3
  CapturedFileChange,
4
4
  FileChangeSet,
5
5
  } from "@stigmer/apis/ai/stigmer/agentic/run/v1/filereview_pb";
6
- import type { FileReviewSet } from "@stigmer/client";
6
+ import type { FileReviewFile, FileReviewSet } from "@stigmer/client";
7
7
  import {
8
8
  fileForRowPath,
9
9
  fileReviewRowChange,
@@ -28,7 +28,8 @@ import {
28
28
  * without a provider render no badge.
29
29
  *
30
30
  * Read-only by design: the row badge is presentation, never a decision
31
- * surface — decisions live on the set's decision bar ({@link FileReviewCard}).
31
+ * surface; a turn's files are undone from its Changed group (the React kit's
32
+ * `TurnOutcome`).
32
33
  */
33
34
  export interface FileReviewContextValue {
34
35
  /** Each turn's file reviews from the session's fold, keyed by the turn's run id. */
@@ -93,3 +94,27 @@ export function useFileReviewRowChange(
93
94
  }
94
95
  return null;
95
96
  }
97
+
98
+ /**
99
+ * The file of its turn's review a transcript row's path refers to, or `null`
100
+ * when the turn has no review (review off, an embed's end user) or the path
101
+ * is not among its files. Its `change` says what the turn did to the file
102
+ * (`added` for a file the turn made), which nothing in the call itself
103
+ * records, so a write row reads "Created" from it.
104
+ *
105
+ * The file sibling of {@link useFileReviewRowChange}: the same newest-first
106
+ * walk over the turn's reviews, through the same path match.
107
+ *
108
+ * @param runId The row's turn (`ToolUse.runId`).
109
+ * @param rowPath The row's file path; `null` for a row that names no file.
110
+ */
111
+ export function useFileReviewRowFile(runId: string, rowPath: string | null): FileReviewFile | null {
112
+ const { fileReviewByRunId } = useContext(FileReviewContext);
113
+ if (!rowPath) return null;
114
+ const sets = fileReviewByRunId.get(runId) ?? NO_SETS;
115
+ for (let i = sets.length - 1; i >= 0; i--) {
116
+ const file = fileForRowPath(sets[i]!, rowPath);
117
+ if (file) return file;
118
+ }
119
+ return null;
120
+ }
@@ -43,22 +43,19 @@ export type ToolCategory =
43
43
  /**
44
44
  * How much of a settled tool call to reveal inline by default in the thread.
45
45
  *
46
- * - `"summary"` — a compact one-line row. Used when the label + primary arg +
47
- * result summary already tell the story (`Read foo.ts`, `Edit bar.ts +40 -0`,
48
- * `Search … 2 matches`). The full detail is one click away.
49
- * - `"preview"` — the row renders its result body **always** (no card chevron),
50
- * because the one-liner cannot convey what happened so the content itself IS
51
- * the information (an MCP result such as a screenshot, a fetched page, a
52
- * web-search answer, or an unrecognised tool's output). The body's own
53
- * in-place reveal expands it beyond the height budget.
54
- * - `"tail"` — live like `"preview"`, settled like a teaser: while the tool
55
- * runs (or awaits approval) the full body streams in view; once it settles
56
- * successfully the row collapses to a dimmed tail of its last output lines —
57
- * at-a-glance evidence that it ran and what it printed last — expanding on
58
- * demand via the header chevron. A settled *failure* stays open: error
59
- * output is content, not context. Shell is the canonical tail category — a
60
- * working session's compile/validate loops would otherwise dominate the
61
- * transcript at full contrast (Cursor's chat is the reference UX).
46
+ * - `"summary"` — a closed one-line step that opens to its body on a click.
47
+ * The line says what the step did (`Read foo.ts`, `Edited bar.ts +40 −0`,
48
+ * `Searched for … 2 matches`); the diff, the matches or the output are one
49
+ * click away. Every category but a memory proposal and shell.
50
+ * - `"preview"` — the row renders its result body **always** (no card
51
+ * chevron), because the body is a decision surface: a memory proposal's
52
+ * fact and its Confirm/Reject actions. A host may ask for it per kind
53
+ * ({@link registerToolPresenter}).
54
+ * - `"tail"` — a summary step that stays open while it shows a failure: the
55
+ * full body streams in view while the tool runs (or awaits approval), it
56
+ * closes once it settles successfully, and a settled failure (an error or
57
+ * a non-zero exit) stays open, because error output is the content the
58
+ * person needs next. Shell is the tail category.
62
59
  *
63
60
  * This is the *disclosure* axis (how much of a result to show); it is
64
61
  * orthogonal to `isRunGroupableCategory` (whether consecutive same-kind
@@ -117,38 +114,24 @@ export function defaultChromeForCategory(category: ToolCategory): ToolChrome {
117
114
  }
118
115
 
119
116
  /**
120
- * Categories whose result is foregrounded as a persistent bounded preview by
121
- * default — those for which the compact one-liner cannot convey what happened,
122
- * so the output itself carries the meaning. All other categories default to
123
- * `"summary"`.
117
+ * Categories whose result is foregrounded as a persistent preview by default.
118
+ * Every other category is a closed one-line step that opens to its body on a
119
+ * click: the line already says what the step did ("Edited invoice.ts
120
+ * +2 −2"), and a turn of many steps reads as a list, not a wall of bodies.
124
121
  *
125
- * File edits and writes are previewed: a `+N -M` summary says how much changed
126
- * but not *what* — and the change is the decision-relevant content. Surfacing a
127
- * bounded diff inline (Cursor-style) is the point of a code agent. The body is
128
- * height-clamped with a single in-place reveal for the rest, so density stays
129
- * under control even in turns with many edits.
122
+ * A memory proposal is the exception: it is a decision surface, and the
123
+ * verbatim fact and its Confirm/Reject actions ARE the content, so it never
124
+ * folds behind a chevron (a proposal the user scrolled past is caught by the
125
+ * memory page, but the chip must not hide itself while in view).
130
126
  */
131
- const PREVIEW_CATEGORIES: ReadonlySet<ToolCategory> = new Set<ToolCategory>([
132
- "edit",
133
- "write",
134
- "mcp",
135
- "web-search",
136
- "fetch",
137
- // A memory proposal is a decision surface: the verbatim fact and the
138
- // Confirm/Reject actions ARE the content — never fold it behind a chevron
139
- // (a proposal the user scrolled past is caught by the memory
140
- // page, but the chip must not hide itself while in view).
141
- "memory",
142
- "unknown",
143
- ]);
127
+ const PREVIEW_CATEGORIES: ReadonlySet<ToolCategory> = new Set<ToolCategory>(["memory"]);
144
128
 
145
129
  /**
146
- * Categories that stream in full while live but settle to a dimmed tail of
147
- * their last output lines (see the `"tail"` arm of {@link ToolDisclosure}).
148
- * Today that is shell alone: its output is evidence worth keeping at a
149
- * glance, but rarely worth reading line by line once the command succeeded.
150
- * Consumers can restore the always-expanded preview per kind via
151
- * {@link registerToolPresenter} (`disclosure: () => "preview"`).
130
+ * Categories that stream in full while live, close once they settle
131
+ * successfully, and stay open on a failure (see the `"tail"` arm of
132
+ * {@link ToolDisclosure}). Today that is shell alone. Consumers can restore
133
+ * the always-expanded preview per kind via {@link registerToolPresenter}
134
+ * (`disclosure: () => "preview"`).
152
135
  */
153
136
  const TAIL_CATEGORIES: ReadonlySet<ToolCategory> = new Set<ToolCategory>([
154
137
  "shell",
@@ -159,12 +142,13 @@ const TAIL_CATEGORIES: ReadonlySet<ToolCategory> = new Set<ToolCategory>([
159
142
  * "Read 5 files" chip in the thread. These are the read-only, repetitive
160
143
  * operations whose individual rows are noise in bulk; everything else renders
161
144
  * as a persistent row. This is the *run-grouping* axis, orthogonal to
162
- * {@link ToolDisclosure} — see `isRunGroupableCategory`.
145
+ * {@link ToolDisclosure} — see `isRunGroupableCategory`. A search is not one
146
+ * of them: each names its own pattern and what it found, so two searches
147
+ * folded into "Searched 2 times" would hide both.
163
148
  */
164
149
  const RUN_GROUPABLE_CATEGORIES: ReadonlySet<ToolCategory> = new Set<ToolCategory>([
165
150
  "read",
166
151
  "list",
167
- "search",
168
152
  ]);
169
153
 
170
154
  /**
@@ -84,7 +84,8 @@ const NO_DECISION_ERRORS: ReadonlyMap<string, Error> = new Map();
84
84
  * session's events (`FileReviewSet`).
85
85
  *
86
86
  * Platform builders who render their own review UI use this hook directly;
87
- * {@link FileReviewCard} takes the decision callback it produces.
87
+ * the React kit's session viewer sends its Undo through the same call
88
+ * (`useSessionConversation().reviewFileChanges`).
88
89
  *
89
90
  * @example
90
91
  * ```tsx
@@ -6,6 +6,7 @@ import { create } from "@bufbuild/protobuf";
6
6
  import {
7
7
  TerminateRunInputSchema,
8
8
  PauseRunInputSchema,
9
+ RecoverRunInputSchema,
9
10
  ResumeRunInputSchema,
10
11
  } from "@stigmer/apis/ai/stigmer/agentic/run/v1/io_pb";
11
12
  import { useStigmer } from "../hooks.js";
@@ -14,7 +15,7 @@ import { toError } from "../internal/toError.js";
14
15
  /** Options for {@link useRunActions}. */
15
16
  export interface UseRunActionsOptions {
16
17
  /**
17
- * Called after any lifecycle action (terminate, pause, resume)
18
+ * Called after any lifecycle action (terminate, pause, resume, recover)
18
19
  * succeeds. Receives the updated run returned by the server.
19
20
  * Useful for triggering a refetch so the UI reflects the new phase.
20
21
  */
@@ -29,6 +30,14 @@ export interface UseRunActionsReturn {
29
30
  readonly pause: (reason?: string) => Promise<Run | null>;
30
31
  /** Resume a paused run. */
31
32
  readonly resume: () => Promise<Run | null>;
33
+ /**
34
+ * Pick a FAILED run up where it stopped: the same turn runs again, steps
35
+ * that worked are not run again, and its values (a key fixed meanwhile)
36
+ * are read again. Needs `can_edit` on the run; refused for a run that was
37
+ * stopped (TERMINATED or CANCELLED) or while another turn of its session
38
+ * is unfinished.
39
+ */
40
+ readonly recover: () => Promise<Run | null>;
32
41
  /** `true` while any action is in flight. */
33
42
  readonly isSubmitting: boolean;
34
43
  /** Error from the last failed action, or `null`. */
@@ -39,7 +48,7 @@ export interface UseRunActionsReturn {
39
48
 
40
49
  /**
41
50
  * Behavior hook that encapsulates one run's lifecycle actions: terminate,
42
- * pause and resume.
51
+ * pause, resume and recover.
43
52
  *
44
53
  * Each action calls the corresponding RPC and returns the updated run, or
45
54
  * `null` on failure (with `error` populated).
@@ -139,10 +148,23 @@ export function useRunActions(
139
148
  [wrap],
140
149
  );
141
150
 
151
+ const recover = useCallback(
152
+ () =>
153
+ wrap(() =>
154
+ stigmerRef.current.run.recover(
155
+ create(RecoverRunInputSchema, {
156
+ id: executionIdRef.current!,
157
+ }),
158
+ ),
159
+ ),
160
+ [wrap],
161
+ );
162
+
142
163
  return {
143
164
  terminate,
144
165
  pause,
145
166
  resume,
167
+ recover,
146
168
  isSubmitting,
147
169
  error,
148
170
  clearError,