@truefoundry/assistant-ui-runtime 0.1.6-rc.0 → 0.1.7

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 (38) hide show
  1. package/README.md +193 -579
  2. package/dist/chunk-3A2EPLQG.js +93 -0
  3. package/dist/chunk-3A2EPLQG.js.map +1 -0
  4. package/dist/chunk-SQDOTGP2.js +292 -0
  5. package/dist/chunk-SQDOTGP2.js.map +1 -0
  6. package/dist/index.d.ts +24 -36
  7. package/dist/index.js +276 -249
  8. package/dist/index.js.map +1 -1
  9. package/dist/plugins/truefoundry-agent-server-adapter/index.d.ts +134 -5
  10. package/dist/plugins/truefoundry-agent-server-adapter/index.js +16 -195
  11. package/dist/plugins/truefoundry-agent-server-adapter/index.js.map +1 -1
  12. package/dist/server/index.d.ts +17 -0
  13. package/dist/server/index.js +9 -0
  14. package/dist/server/index.js.map +1 -0
  15. package/dist/{types-VUBzoJT2.d.ts → types-DbNsU075.d.ts} +212 -19
  16. package/package.json +10 -5
  17. package/src/{private → draft}/agentSpec.ts +14 -17
  18. package/src/{private → draft}/draftSessionBridge.ts +1 -2
  19. package/src/{private → draft}/truefoundryDraftThreadListAdapter.test.ts +1 -1
  20. package/src/{private → draft}/truefoundryDraftThreadListAdapter.ts +2 -1
  21. package/src/{private → draft}/useDraftAgentSpec.ts +16 -5
  22. package/src/draftAgentConfig.test.ts +2 -1
  23. package/src/index.ts +71 -7
  24. package/src/plugins/truefoundry-agent-server-adapter/README.md +178 -0
  25. package/src/plugins/truefoundry-agent-server-adapter/guards.test.ts +113 -0
  26. package/src/plugins/truefoundry-agent-server-adapter/guards.ts +130 -0
  27. package/src/plugins/truefoundry-agent-server-adapter/index.ts +154 -40
  28. package/src/plugins/truefoundry-agent-server-adapter/types.ts +137 -0
  29. package/src/plugins/truefoundry-agent-server-adapter/types.typecheck.ts +164 -0
  30. package/src/server/index.ts +23 -0
  31. package/src/server/types.ts +272 -21
  32. package/src/truefoundryExtras.ts +4 -1
  33. package/src/truefoundryOwnedSessionsThreadListAdapter.ts +1 -1
  34. package/src/types.ts +1 -2
  35. package/src/useTrueFoundryAgentMessages.test.tsx +261 -1
  36. package/src/useTrueFoundryAgentMessages.ts +284 -176
  37. package/src/useTrueFoundryAgentRuntime.ts +31 -21
  38. /package/src/{private → draft}/useDraftAgentSpec.test.tsx +0 -0
@@ -14,7 +14,7 @@ import { useAui, useAuiState } from "@assistant-ui/store";
14
14
  import type { MutableRefObject } from "react";
15
15
  import { useCallback, useMemo, useRef, useState } from "react";
16
16
 
17
- import type { AgentSpec } from "./private/agentSpec.js";
17
+ import type { AgentSpec } from "./server/types.js";
18
18
  import {
19
19
  collectPendingApprovals,
20
20
  collectPendingToolResponses,
@@ -25,18 +25,19 @@ import {
25
25
  buildUserMessageContent,
26
26
  extractEditedText,
27
27
  parseTurnIdFromMessageId,
28
+ userMessageContentToText,
28
29
  } from "./convertTurnMessages.js";
29
30
  import {
30
31
  createDraftSessionBridge,
31
32
  DRAFT_SESSION_LAST_UPDATED_AT_HEADER,
32
- } from "./private/draftSessionBridge.js";
33
+ } from "./draft/draftSessionBridge.js";
33
34
  import { MCP_AUTH_RESUME_RUN_CUSTOM_KEY } from "./mcpAuth.js";
34
- import { createTrueFoundryDraftThreadListAdapter } from "./private/truefoundryDraftThreadListAdapter.js";
35
+ import { createTrueFoundryDraftThreadListAdapter } from "./draft/truefoundryDraftThreadListAdapter.js";
35
36
  import { trueFoundryExtras } from "./truefoundryExtras.js";
36
37
  import { createTrueFoundryThreadListAdapter } from "./truefoundryThreadListAdapter.js";
37
38
  import type { UseTrueFoundryAgentRuntimeOptions } from "./types.js";
38
39
  import { resolveTrueFoundryAgentRuntimeOptions } from "./types.js";
39
- import { useDraftAgentSpec } from "./private/useDraftAgentSpec.js";
40
+ import { useDraftAgentSpec } from "./draft/useDraftAgentSpec.js";
40
41
  import { useTrueFoundryAgentMessages } from "./useTrueFoundryAgentMessages.js";
41
42
 
42
43
  function useTrueFoundryAgentRuntimeImpl(
@@ -66,6 +67,10 @@ function useTrueFoundryAgentRuntimeImpl(
66
67
  const isMain = useAuiState(
67
68
  (state) => state.threads.mainThreadId === state.threadListItem.id,
68
69
  );
70
+ // On a hard refresh, the URL session runtime mounts before assistant-ui
71
+ // promotes it to the main thread. Allow that one session to hydrate early.
72
+ const isInitialSession =
73
+ sessionId != null && sessionId === options.initialSessionId;
69
74
 
70
75
  const draftSpec = useDraftAgentSpec({
71
76
  draftSessionId,
@@ -119,6 +124,7 @@ function useTrueFoundryAgentRuntimeImpl(
119
124
  server,
120
125
  sessionId,
121
126
  isMain,
127
+ isInitialSession,
122
128
  listEventsConcurrency,
123
129
  onError,
124
130
  initializeSession,
@@ -148,20 +154,16 @@ function useTrueFoundryAgentRuntimeImpl(
148
154
  const downloadSandboxFile = useCallback(
149
155
  async (path: string) => {
150
156
  if (server.downloadSandboxFile == null) {
151
- const error = new Error(
157
+ throw new Error(
152
158
  "Downloading a sandbox file requires AgentChatServer.downloadSandboxFile.",
153
159
  );
154
- onError?.(error);
155
- throw error;
156
160
  }
157
161
  if (sandboxId == null) {
158
- const error = new Error("No sandbox is available yet for this session.");
159
- onError?.(error);
160
- throw error;
162
+ throw new Error("No sandbox is available yet for this session.");
161
163
  }
162
164
  return await server.downloadSandboxFile(sandboxId, { path });
163
165
  },
164
- [server, sandboxId, onError],
166
+ [server, sandboxId],
165
167
  );
166
168
 
167
169
  const draftExtras = useMemo(() => {
@@ -171,6 +173,7 @@ function useTrueFoundryAgentRuntimeImpl(
171
173
  return {
172
174
  agentSpec: draftSpec.agentSpec,
173
175
  draftSessionId: draftSpec.draftSessionId,
176
+ isSpecLoading: draftSpec.isSpecLoading,
174
177
  isSpecSyncing: draftSpec.isSpecSyncing,
175
178
  specError: draftSpec.specError,
176
179
  updateAgentSpec: draftSpec.updateAgentSpec,
@@ -192,10 +195,9 @@ function useTrueFoundryAgentRuntimeImpl(
192
195
  resumeMcpAuth,
193
196
  downloadSandboxFile,
194
197
  cancel,
198
+ // resetFromTurn/branchFromTurn/sendTurn already report via onError.
195
199
  resetFromTurn: (turnId: string) =>
196
- resetFromTurn(turnId).catch((error) => {
197
- onError?.(error);
198
- }),
200
+ resetFromTurn(turnId).catch(() => undefined),
199
201
  reload: retryLoad,
200
202
  hasOlderHistory,
201
203
  isLoadingOlderHistory,
@@ -224,7 +226,19 @@ function useTrueFoundryAgentRuntimeImpl(
224
226
  return;
225
227
  }
226
228
 
227
- await sendTurn({ userMessage: buildUserMessageContent(message) });
229
+ const userMessage = buildUserMessageContent(message);
230
+ await sendTurn({
231
+ userMessage,
232
+ // The composer clears before onNew runs. Restore its text only when
233
+ // the turn failed before turn.created registered it in the backend.
234
+ onPreTurnFailure: () => {
235
+ const text = userMessageContentToText(userMessage);
236
+ const composer = aui.thread().composer();
237
+ if (text && !composer.getState().text.trim()) {
238
+ composer.setText(text);
239
+ }
240
+ },
241
+ });
228
242
  },
229
243
  onCancel: async () => {
230
244
  await cancel();
@@ -242,12 +256,8 @@ function useTrueFoundryAgentRuntimeImpl(
242
256
  }
243
257
  const turnId = parseTurnIdFromMessageId(sourceId);
244
258
  const editedText = extractEditedText(message);
245
- try {
246
- await editFromTurn(turnId, editedText);
247
- } catch (error) {
248
- onError?.(error);
249
- throw error;
250
- }
259
+ // editFromTurn/branchFromTurn/sendTurn already report via onError.
260
+ await editFromTurn(turnId, editedText);
251
261
  },
252
262
  });
253
263
  }