@assistant-ui/react-statewire 0.5.0 → 0.6.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.
@@ -33,6 +33,7 @@ type LangGraphState = {
33
33
  messages: LangGraphMessage[];
34
34
  todos: string[];
35
35
  inputRequests?: AssistantTransportThread.InputRequestState[];
36
+ isRunning?: boolean;
36
37
  };
37
38
 
38
39
  const INITIAL_STATE: LangGraphState = { messages: [], todos: [] };
@@ -64,9 +65,29 @@ const createConverter = () => {
64
65
  return id;
65
66
  };
66
67
 
68
+ // The optimistic fold stamps isRunning and inserts the pending user
69
+ // message unless the server already echoed its trackingId.
70
+ const optimistic: AssistantTransportThread.Optimistic<LangGraphState> = (
71
+ draft,
72
+ command,
73
+ ) => {
74
+ draft.isRunning = true;
75
+ if (!isUserMessageCommand(command)) return;
76
+ const trackingId = trackingIdOf(command);
77
+ const echoed = draft.messages.some(
78
+ (m) => m.type === "human" && m.trackingId === trackingId,
79
+ );
80
+ if (echoed) return;
81
+ draft.messages.push({
82
+ id: `optimistic-${trackingId}`,
83
+ type: "human",
84
+ content: command.message.parts.find((p) => p.type === "text")?.text ?? "",
85
+ trackingId,
86
+ });
87
+ };
88
+
67
89
  const converter: AssistantTransportThread.StateConverter<LangGraphState> = (
68
90
  state,
69
- meta,
70
91
  ) => {
71
92
  const toolResults = new Map<string, string>();
72
93
  for (const m of state.messages) {
@@ -115,38 +136,19 @@ const createConverter = () => {
115
136
  }
116
137
  }
117
138
 
118
- for (const c of meta.pendingCommands) {
119
- if (!isUserMessageCommand(c)) continue;
120
- const trackingId = trackingIdOf(c);
121
- const echoed = state.messages.some(
122
- (m) => m.type === "human" && m.trackingId === trackingId,
123
- );
124
- if (echoed) continue;
125
- messages.push({
126
- id: `optimistic-${trackingId}`,
127
- role: "user",
128
- createdAt: new Date(0),
129
- content: c.message.parts.flatMap((p) =>
130
- p.type === "text" ? [{ type: "text", text: p.text }] : [],
131
- ),
132
- attachments: [],
133
- metadata: { custom: { trackingId } },
134
- } as unknown as ExternalThreadMessage);
135
- }
136
-
137
139
  return {
138
140
  messages,
139
- isRunning: meta.isSending,
141
+ isRunning: state.isRunning ?? false,
140
142
  state: { todos: state.todos },
141
143
  inputRequests: state.inputRequests ?? [],
142
144
  };
143
145
  };
144
146
 
145
- return { converter, trackingIdOf };
147
+ return { converter, optimistic, trackingIdOf };
146
148
  };
147
149
 
148
150
  const mountDeepAgent = () => {
149
- const { converter, trackingIdOf } = createConverter();
151
+ const { converter, optimistic, trackingIdOf } = createConverter();
150
152
  const cancelled: AssistantTransportThread.Command[][] = [];
151
153
  const errored: AssistantTransportThread.Command[][] = [];
152
154
  const responses: (string | null)[] = [];
@@ -168,6 +170,7 @@ const mountDeepAgent = () => {
168
170
  resumeApi: "https://sync.example.com/api/resume",
169
171
  protocol: "assistant-transport",
170
172
  converter,
173
+ optimistic,
171
174
  capabilities: { edit: true },
172
175
  headers: async () => ({
173
176
  Authorization: "Bearer test-token",
@@ -25,47 +25,32 @@ const INITIAL_STATE: TestState = { messages: [] };
25
25
 
26
26
  const converter: AssistantTransportThread.StateConverter<TestState> = (
27
27
  state,
28
- meta,
29
28
  ) => ({
30
- messages: [
31
- ...state.messages.map(
32
- (m) =>
33
- ({
34
- id: m.id,
35
- role: m.role,
36
- createdAt: new Date(0),
37
- content: [{ type: "text", text: m.text }],
38
- attachments: [],
39
- metadata: { custom: {} },
40
- ...(m.role === "assistant" && {
41
- status: { type: "complete", reason: "stop" },
42
- }),
43
- }) as unknown as ExternalThreadMessage,
44
- ),
45
- ...meta.pendingCommands.flatMap((c, i) =>
46
- // The legacy runtime still surfaces add-message pending commands.
47
- (c.type === "run/steer" || (c.type as string) === "add-message") &&
48
- "message" in c &&
49
- c.message.role === "user"
50
- ? [
51
- {
52
- id: `optimistic-${i}`,
53
- role: "user",
54
- createdAt: new Date(0),
55
- content: [
56
- {
57
- type: "text",
58
- text:
59
- c.message.parts.find((p) => p.type === "text")?.text ?? "",
60
- },
61
- ],
62
- attachments: [],
63
- metadata: { custom: {} },
64
- } as unknown as ExternalThreadMessage,
65
- ]
66
- : [],
67
- ),
68
- ],
29
+ messages: state.messages.map(
30
+ (m) =>
31
+ ({
32
+ id: m.id,
33
+ role: m.role,
34
+ createdAt: new Date(0),
35
+ content: [{ type: "text", text: m.text }],
36
+ attachments: [],
37
+ metadata: { custom: {} },
38
+ ...(m.role === "assistant" && {
39
+ status: { type: "complete", reason: "stop" },
40
+ }),
41
+ }) as unknown as ExternalThreadMessage,
42
+ ),
43
+ isRunning: false,
44
+ });
45
+
46
+ // The legacy runtime still feeds connection metadata into its converter.
47
+ type LegacyMeta = {
48
+ pendingCommands: { type: string; message?: { role: string } }[];
49
+ isSending: boolean;
50
+ };
51
+
52
+ const legacyConverter = (state: TestState, meta: LegacyMeta) => ({
53
+ ...converter(state),
69
54
  isRunning: meta.isSending,
70
55
  });
71
56
 
@@ -184,7 +169,7 @@ const observeLegacy = async (): Promise<{
184
169
  api: "https://example.com/api",
185
170
  protocol: "assistant-transport",
186
171
  headers: {},
187
- converter: converter as never,
172
+ converter: legacyConverter as never,
188
173
  });
189
174
  return (
190
175
  <AssistantRuntimeProvider runtime={captured.runtime}>
@@ -287,10 +272,9 @@ type ToolState = TestState & {
287
272
 
288
273
  const toolConverter: AssistantTransportThread.StateConverter<ToolState> = (
289
274
  state,
290
- meta,
291
275
  ) => {
292
276
  const base = {
293
- ...converter(state, meta),
277
+ ...converter(state),
294
278
  inputRequests: state.inputRequests ?? [],
295
279
  };
296
280
  if (!state.toolCall) return base;
@@ -316,10 +300,14 @@ const toolConverter: AssistantTransportThread.StateConverter<ToolState> = (
316
300
  status: { type: "requires-action", reason: "tool-call" },
317
301
  } as unknown as ExternalThreadMessage,
318
302
  ],
319
- isRunning: meta.isSending,
320
303
  };
321
304
  };
322
305
 
306
+ const legacyToolConverter = (state: ToolState, meta: LegacyMeta) => ({
307
+ ...toolConverter(state),
308
+ isRunning: meta.isSending,
309
+ });
310
+
323
311
  const TOOL_TRANSCRIPT: string[][] = [
324
312
  [
325
313
  sseEvent({
@@ -472,9 +460,10 @@ const observeLegacyToolRun = async (): Promise<ToolObserved> => {
472
460
  runtime?: ReturnType<typeof useAssistantTransportRuntime>;
473
461
  } = {};
474
462
  const App: FC = () => {
475
- captured.runtime = useAssistantTransportRuntime<ToolState>(
476
- toolOptions(events) as never,
477
- );
463
+ captured.runtime = useAssistantTransportRuntime<ToolState>({
464
+ ...toolOptions(events),
465
+ converter: legacyToolConverter,
466
+ } as never);
478
467
  return (
479
468
  <AssistantRuntimeProvider runtime={captured.runtime}>
480
469
  {null}