@truefoundry/assistant-ui-runtime 0.1.5 → 0.1.6

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 (70) hide show
  1. package/README.md +198 -401
  2. package/dist/chunk-3A2EPLQG.js +93 -0
  3. package/dist/chunk-3A2EPLQG.js.map +1 -0
  4. package/dist/chunk-Q2SHKMLM.js +270 -0
  5. package/dist/chunk-Q2SHKMLM.js.map +1 -0
  6. package/dist/index.d.ts +24 -29
  7. package/dist/index.js +264 -237
  8. package/dist/index.js.map +1 -1
  9. package/dist/plugins/truefoundry-agent-server-adapter/index.d.ts +162 -0
  10. package/dist/plugins/truefoundry-agent-server-adapter/index.js +19 -0
  11. package/dist/plugins/truefoundry-agent-server-adapter/index.js.map +1 -0
  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-BfiFf8O1.d.ts +468 -0
  16. package/package.json +19 -6
  17. package/src/askUserQuestion.ts +3 -3
  18. package/src/buildEditedUserMessageContent.test.ts +2 -2
  19. package/src/collectPending.ts +1 -1
  20. package/src/convertTurnMessages.test.ts +141 -196
  21. package/src/convertTurnMessages.ts +130 -76
  22. package/src/createSubAgent.ts +1 -1
  23. package/src/draftAgentConfig.test.ts +26 -29
  24. package/src/extractTurnUserText.ts +1 -1
  25. package/src/foldPeerThreads.test.ts +1 -1
  26. package/src/foldPeerThreads.ts +3 -2
  27. package/src/index.ts +80 -4
  28. package/src/listPages.ts +21 -0
  29. package/src/loadSessionSnapshot.test.ts +9 -8
  30. package/src/loadSessionSnapshot.ts +9 -14
  31. package/src/mcpAuth.ts +6 -3
  32. package/src/messageCustomMetadata.ts +1 -1
  33. package/src/modelMessageContent.ts +1 -1
  34. package/src/modelMessageImageContent.test.ts +1 -1
  35. package/src/modelMessageImageContent.ts +7 -6
  36. package/src/plugins/truefoundry-agent-server-adapter/README.md +178 -0
  37. package/src/plugins/truefoundry-agent-server-adapter/guards.test.ts +113 -0
  38. package/src/plugins/truefoundry-agent-server-adapter/guards.ts +130 -0
  39. package/src/plugins/truefoundry-agent-server-adapter/index.ts +359 -0
  40. package/src/plugins/truefoundry-agent-server-adapter/types.ts +135 -0
  41. package/src/plugins/truefoundry-agent-server-adapter/types.typecheck.ts +164 -0
  42. package/src/private/agentSpec.ts +8 -3
  43. package/src/private/draftSessionBridge.ts +14 -13
  44. package/src/private/truefoundryDraftThreadListAdapter.test.ts +44 -49
  45. package/src/private/truefoundryDraftThreadListAdapter.ts +22 -16
  46. package/src/requiredActionInputs.ts +1 -1
  47. package/src/requiredActionsFromActiveUpdate.test.ts +1 -1
  48. package/src/server/eventUtils.ts +120 -0
  49. package/src/server/events.ts +246 -0
  50. package/src/server/index.ts +66 -0
  51. package/src/server/types.ts +319 -0
  52. package/src/sessionSnapshot.ts +1 -1
  53. package/src/sessions.ts +5 -21
  54. package/src/streamTurn.test.ts +172 -155
  55. package/src/streamTurn.ts +51 -48
  56. package/src/toolApproval.ts +4 -4
  57. package/src/toolResponse.ts +4 -4
  58. package/src/truefoundryExtras.ts +1 -1
  59. package/src/truefoundryOwnedSessionsThreadListAdapter.test.ts +26 -29
  60. package/src/truefoundryOwnedSessionsThreadListAdapter.ts +18 -23
  61. package/src/truefoundryThreadListAdapter.test.ts +16 -18
  62. package/src/truefoundryThreadListAdapter.ts +7 -7
  63. package/src/turnEventHelpers.ts +1 -1
  64. package/src/types.ts +2 -16
  65. package/src/useTrueFoundryAgentMessages.test.tsx +38 -70
  66. package/src/useTrueFoundryAgentMessages.ts +32 -44
  67. package/src/useTrueFoundryAgentRuntime.ts +11 -28
  68. package/src/private/bindDraftAgentSession.test.ts +0 -54
  69. package/src/private/bindDraftAgentSession.ts +0 -28
  70. package/src/private/getGatewayFromPrivateClient.ts +0 -13
package/dist/index.js CHANGED
@@ -1,3 +1,17 @@
1
+ import {
2
+ isEventDelta,
3
+ mergeEventDelta
4
+ } from "./chunk-3A2EPLQG.js";
5
+ import {
6
+ createTrueFoundryChatServer,
7
+ getTfyMcpInitServers,
8
+ getTfyThreadState,
9
+ getTfyUsage,
10
+ isTfyMcpToolInfo,
11
+ isTfySystemToolInfo,
12
+ isTfyToolInfo
13
+ } from "./chunk-Q2SHKMLM.js";
14
+
1
15
  // src/useTrueFoundryAgentRuntime.ts
2
16
  import {
3
17
  pickExternalStoreSharedOptions
@@ -88,9 +102,10 @@ function findApprovalRequiredInTurn(turn) {
88
102
  if (turn.state.status !== "done") {
89
103
  return void 0;
90
104
  }
91
- return turn.state.requiredActions?.find(
105
+ const found = turn.state.requiredActions?.find(
92
106
  (action) => action.type === "tool.approval_required"
93
107
  );
108
+ return found?.type === "tool.approval_required" ? found : void 0;
94
109
  }
95
110
  function toolCallPartHasPendingApproval(part) {
96
111
  return hasPendingToolApproval(part.approval);
@@ -250,11 +265,6 @@ function toTrueFoundryApprovalInputs(message, response, defaultThreadId = ROOT_T
250
265
  return collectApprovalInputs(updated, defaultThreadId);
251
266
  }
252
267
 
253
- // src/foldPeerThreads.ts
254
- import {
255
- isEventDelta as isEventDelta2
256
- } from "truefoundry-gateway-sdk/agents";
257
-
258
268
  // src/askUserQuestion.ts
259
269
  function parseAskUserQuestionArgs(argsText) {
260
270
  if (!argsText) {
@@ -283,7 +293,6 @@ function isCreateSubAgentToolCall(toolCall) {
283
293
  }
284
294
 
285
295
  // src/modelMessageImageContent.ts
286
- import { isEventDelta, mergeEventDelta } from "truefoundry-gateway-sdk/agents";
287
296
  function parseDataUriMime(data) {
288
297
  if (!data.startsWith("data:")) {
289
298
  return "image/png";
@@ -545,7 +554,7 @@ function ingestEventIntoBucket(bucket, message) {
545
554
  if (isTurnScopedEvent(message)) {
546
555
  return;
547
556
  }
548
- if (isEventDelta2(message)) {
557
+ if (isEventDelta(message)) {
549
558
  const base = bucket.events.get(message.id);
550
559
  if (base != null) {
551
560
  mergeStreamEventDelta(base, message);
@@ -899,9 +908,10 @@ function findResponseRequiredInTurn(turn) {
899
908
  if (turn.state.status !== "done") {
900
909
  return void 0;
901
910
  }
902
- return turn.state.requiredActions?.find(
911
+ const found = turn.state.requiredActions?.find(
903
912
  (action) => action.type === "tool.response_required"
904
913
  );
914
+ return found?.type === "tool.response_required" ? found : void 0;
905
915
  }
906
916
  function applyToolResponseToToolCall(part, content) {
907
917
  return { ...part, result: content };
@@ -1150,6 +1160,21 @@ function deriveSandboxId(messages) {
1150
1160
  return void 0;
1151
1161
  }
1152
1162
 
1163
+ // src/listPages.ts
1164
+ async function drainListPages(fetchPage) {
1165
+ const items = [];
1166
+ let pageToken;
1167
+ for (; ; ) {
1168
+ const page = await fetchPage(pageToken);
1169
+ items.push(...page.data);
1170
+ if (page.nextPageToken == null || page.nextPageToken === "") {
1171
+ break;
1172
+ }
1173
+ pageToken = page.nextPageToken;
1174
+ }
1175
+ return items;
1176
+ }
1177
+
1153
1178
  // src/extractTurnUserText.ts
1154
1179
  function extractTurnUserText(input) {
1155
1180
  const parts = [];
@@ -1187,9 +1212,10 @@ function buildMcpAuthTextParts(servers) {
1187
1212
  return [{ type: "text", text }];
1188
1213
  }
1189
1214
  function findMcpAuthRequired(requiredActions) {
1190
- return requiredActions?.find(
1215
+ const found = requiredActions?.find(
1191
1216
  (action) => action.type === "mcp.auth_required"
1192
1217
  );
1218
+ return found?.type === "mcp.auth_required" ? found : void 0;
1193
1219
  }
1194
1220
  function mcpAuthAssistantStatus() {
1195
1221
  return { type: "requires-action", reason: "interrupt" };
@@ -1329,28 +1355,28 @@ function oldestCompleteTurnGroupState(itemsAsc) {
1329
1355
  }
1330
1356
  return extractTurnUserText(created.event.input) != null ? "user-group" : "continuation";
1331
1357
  }
1332
- async function fetchSessionEventsPage(session, options) {
1333
- const page = await session.listEvents({
1358
+ async function fetchSessionEventsPage(server, sessionId, options) {
1359
+ const page = await server.listEvents({
1360
+ sessionId,
1334
1361
  limit: SESSION_EVENTS_PAGE_SIZE,
1335
1362
  ...options?.lastTurnId != null ? { lastTurnId: options.lastTurnId } : {},
1336
1363
  ...options?.pageToken != null ? { pageToken: options.pageToken } : {}
1337
1364
  });
1338
- const response = page.response;
1339
- const olderPageToken = response.pagination?.nextPageToken;
1340
- const hasOlder = typeof page.hasNextPage === "function" ? page.hasNextPage() : olderPageToken != null && olderPageToken !== "";
1365
+ const olderPageToken = page.nextPageToken;
1366
+ const hasOlder = olderPageToken != null && olderPageToken !== "";
1341
1367
  return {
1342
1368
  itemsNewestFirst: page.data,
1343
1369
  ...olderPageToken != null && olderPageToken !== "" ? { olderPageToken } : {},
1344
1370
  hasOlder
1345
1371
  };
1346
1372
  }
1347
- async function fetchSessionEventsWindow(session, options) {
1373
+ async function fetchSessionEventsWindow(server, sessionId, options) {
1348
1374
  let itemsNewestFirst = [];
1349
1375
  let pageToken = options?.pageToken;
1350
1376
  let olderPageToken;
1351
1377
  let hasOlder = false;
1352
1378
  for (let pageCount = 0; pageCount < MAX_HISTORY_BOUNDARY_PAGES; pageCount++) {
1353
- const page = await fetchSessionEventsPage(session, {
1379
+ const page = await fetchSessionEventsPage(server, sessionId, {
1354
1380
  ...options,
1355
1381
  ...pageToken != null ? { pageToken } : {}
1356
1382
  });
@@ -1378,14 +1404,15 @@ async function fetchSessionEventsWindow(session, options) {
1378
1404
  hasOlder
1379
1405
  };
1380
1406
  }
1381
- async function fetchAllSessionEvents(session, options) {
1382
- const items = [];
1383
- for await (const item of await session.listEvents({
1384
- limit: SESSION_EVENTS_PAGE_SIZE,
1385
- ...options?.lastTurnId != null ? { lastTurnId: options.lastTurnId } : {}
1386
- })) {
1387
- items.push(item);
1388
- }
1407
+ async function fetchAllSessionEvents(server, sessionId, options) {
1408
+ const items = await drainListPages(
1409
+ (pageToken) => server.listEvents({
1410
+ sessionId,
1411
+ limit: SESSION_EVENTS_PAGE_SIZE,
1412
+ ...options?.lastTurnId != null ? { lastTurnId: options.lastTurnId } : {},
1413
+ ...pageToken != null ? { pageToken } : {}
1414
+ })
1415
+ );
1389
1416
  items.reverse();
1390
1417
  return items;
1391
1418
  }
@@ -1520,11 +1547,11 @@ function attachRunningTurn(snapshot, runningTurn) {
1520
1547
  } : {}
1521
1548
  });
1522
1549
  }
1523
- async function buildSnapshotFromSessionEvents(session, onProgress) {
1524
- const turnsPage = await session.listTurns({ limit: 1 });
1550
+ async function buildSnapshotFromSessionEvents(server, sessionId, onProgress) {
1551
+ const turnsPage = await server.listTurns({ sessionId, limit: 1 });
1525
1552
  const newestTurn = turnsPage.data[0];
1526
1553
  const runningTurn = newestTurn?.state?.status === "running" ? newestTurn : void 0;
1527
- const window = await fetchSessionEventsWindow(session);
1554
+ const window = await fetchSessionEventsWindow(server, sessionId);
1528
1555
  const historyPagination = {
1529
1556
  hasOlder: window.hasOlder,
1530
1557
  ...window.olderPageToken != null ? { olderPageToken: window.olderPageToken } : {}
@@ -1537,12 +1564,12 @@ async function buildSnapshotFromSessionEvents(session, onProgress) {
1537
1564
  });
1538
1565
  return attachRunningTurn(withHistory, runningTurn);
1539
1566
  }
1540
- async function prependOlderSessionHistory(session, snapshot) {
1567
+ async function prependOlderSessionHistory(server, sessionId, snapshot) {
1541
1568
  const pagination = snapshot.historyPagination;
1542
1569
  if (pagination?.hasOlder !== true || pagination.olderPageToken == null) {
1543
1570
  return snapshot;
1544
1571
  }
1545
- const window = await fetchSessionEventsWindow(session, {
1572
+ const window = await fetchSessionEventsWindow(server, sessionId, {
1546
1573
  pageToken: pagination.olderPageToken
1547
1574
  });
1548
1575
  if (window.itemsAsc.length === 0) {
@@ -1694,22 +1721,36 @@ function buildAssistantMessage(turnId, content, createdAt, status, custom = {},
1694
1721
  }
1695
1722
  };
1696
1723
  }
1697
- async function ingestTurnEventsIntoFold(foldState, turn) {
1698
- for await (const event of await turn.listEvents({
1699
- order: "asc",
1700
- limit: TURN_EVENTS_PAGE_SIZE
1701
- })) {
1724
+ async function ingestTurnEventsIntoFold(server, sessionId, turnId, foldState) {
1725
+ if (server.listTurnEvents == null) {
1726
+ return;
1727
+ }
1728
+ const events = await drainListPages(
1729
+ (pageToken) => server.listTurnEvents({
1730
+ sessionId,
1731
+ turnId,
1732
+ order: "asc",
1733
+ limit: TURN_EVENTS_PAGE_SIZE,
1734
+ ...pageToken != null ? { pageToken } : {}
1735
+ })
1736
+ );
1737
+ for (const event of events) {
1702
1738
  ingestTurnEvent(foldState, event);
1703
1739
  }
1704
1740
  }
1705
- async function fetchTurnEvents(turn) {
1706
- const events = [];
1707
- for await (const event of await turn.listEvents({
1708
- order: "asc",
1709
- limit: TURN_EVENTS_PAGE_SIZE
1710
- })) {
1711
- events.push(event);
1741
+ async function fetchTurnEvents(server, sessionId, turnId) {
1742
+ if (server.listTurnEvents == null) {
1743
+ return [];
1712
1744
  }
1745
+ const events = await drainListPages(
1746
+ (pageToken) => server.listTurnEvents({
1747
+ sessionId,
1748
+ turnId,
1749
+ order: "asc",
1750
+ limit: TURN_EVENTS_PAGE_SIZE,
1751
+ ...pageToken != null ? { pageToken } : {}
1752
+ })
1753
+ );
1713
1754
  return events;
1714
1755
  }
1715
1756
  function ingestCollectedEventsIntoFold(foldState, events) {
@@ -1717,16 +1758,18 @@ function ingestCollectedEventsIntoFold(foldState, events) {
1717
1758
  ingestTurnEvent(foldState, event);
1718
1759
  }
1719
1760
  }
1720
- async function fetchAllTurnEventsWithConcurrency(turns, concurrency) {
1761
+ async function fetchAllTurnEventsWithConcurrency(server, sessionId, turns, concurrency) {
1721
1762
  const results = new Array(turns.length);
1722
1763
  const pool = /* @__PURE__ */ new Set();
1723
1764
  for (let i = 0; i < turns.length; i++) {
1724
1765
  const idx = i;
1725
1766
  const turn = turns[idx];
1726
- const p = fetchTurnEvents(turn).then((events) => {
1727
- results[idx] = events;
1728
- pool.delete(p);
1729
- });
1767
+ const p = fetchTurnEvents(server, sessionId, turn.id).then(
1768
+ (events) => {
1769
+ results[idx] = events;
1770
+ pool.delete(p);
1771
+ }
1772
+ );
1730
1773
  pool.add(p);
1731
1774
  if (pool.size >= concurrency) await Promise.race(pool);
1732
1775
  }
@@ -2011,13 +2054,18 @@ function ingestTurnsIntoSnapshot(snapshot, turns, eventArrays) {
2011
2054
  }
2012
2055
  return runningTurn;
2013
2056
  }
2014
- async function buildSnapshotFromSession(session, concurrency = DEFAULT_LIST_EVENTS_CONCURRENCY) {
2015
- const snapshot = await buildSnapshotFromSessionEvents(session);
2057
+ async function buildSnapshotFromSession(server, sessionId, concurrency = DEFAULT_LIST_EVENTS_CONCURRENCY) {
2058
+ const snapshot = await buildSnapshotFromSessionEvents(server, sessionId);
2016
2059
  if (snapshot.runningTurn == null) {
2017
2060
  return snapshot;
2018
2061
  }
2019
2062
  const turn = snapshot.runningTurn;
2020
- const eventArrays = await fetchAllTurnEventsWithConcurrency([turn], concurrency);
2063
+ const eventArrays = await fetchAllTurnEventsWithConcurrency(
2064
+ server,
2065
+ sessionId,
2066
+ [turn],
2067
+ concurrency
2068
+ );
2021
2069
  ingestTurnsIntoSnapshot(snapshot, [turn], eventArrays);
2022
2070
  return replaceSessionSnapshot(snapshot, {
2023
2071
  runningTurn: turn,
@@ -2026,59 +2074,67 @@ async function buildSnapshotFromSession(session, concurrency = DEFAULT_LIST_EVEN
2026
2074
  pendingUser: void 0
2027
2075
  });
2028
2076
  }
2029
- async function buildSnapshotBeforeTurn(session, beforeTurnId, concurrency = DEFAULT_LIST_EVENTS_CONCURRENCY) {
2030
- const turns = await listSessionTurnsOrdered(session);
2077
+ async function buildSnapshotBeforeTurn(server, sessionId, beforeTurnId, concurrency = DEFAULT_LIST_EVENTS_CONCURRENCY) {
2078
+ const turns = await listSessionTurnsOrdered(server, sessionId);
2031
2079
  const beforeIndex = turns.findIndex((turn) => turn.id === beforeTurnId);
2032
2080
  if (beforeIndex === -1) {
2033
2081
  throw new Error(`Turn ${beforeTurnId} not found in session`);
2034
2082
  }
2035
- return buildSnapshotBeforeTurnIndex(session, beforeIndex, concurrency, turns);
2083
+ return buildSnapshotBeforeTurnIndex(
2084
+ server,
2085
+ sessionId,
2086
+ beforeIndex,
2087
+ concurrency,
2088
+ turns
2089
+ );
2036
2090
  }
2037
- async function buildSnapshotBeforeTurnIndex(session, turnIndex, _concurrency = DEFAULT_LIST_EVENTS_CONCURRENCY, orderedTurns) {
2091
+ async function buildSnapshotBeforeTurnIndex(server, sessionId, turnIndex, _concurrency = DEFAULT_LIST_EVENTS_CONCURRENCY, orderedTurns) {
2038
2092
  if (turnIndex <= 0) {
2039
2093
  return createEmptySessionSnapshot();
2040
2094
  }
2041
- const turns = orderedTurns ?? await listSessionTurnsOrdered(session);
2095
+ const turns = orderedTurns ?? await listSessionTurnsOrdered(server, sessionId);
2042
2096
  const turnsToInclude = turns.slice(0, turnIndex);
2043
2097
  const lastTurnId = turnsToInclude.at(-1)?.id;
2044
2098
  if (lastTurnId == null) {
2045
2099
  return createEmptySessionSnapshot();
2046
2100
  }
2047
- const items = await fetchAllSessionEvents(session, { lastTurnId });
2101
+ const items = await fetchAllSessionEvents(server, sessionId, { lastTurnId });
2048
2102
  const snapshot = createEmptySessionSnapshot();
2049
2103
  ingestSessionEventsIntoSnapshot(snapshot, items);
2050
2104
  return snapshot;
2051
2105
  }
2052
- async function resolveGatewayBranchPreviousTurnId(session, turnIndex, orderedTurns) {
2106
+ async function resolveGatewayBranchPreviousTurnId(server, sessionId, turnIndex, orderedTurns) {
2053
2107
  if (turnIndex <= 0) {
2054
2108
  return "none";
2055
2109
  }
2056
- const turns = orderedTurns ?? await listSessionTurnsOrdered(session);
2110
+ const turns = orderedTurns ?? await listSessionTurnsOrdered(server, sessionId);
2057
2111
  return turns[turnIndex - 1]?.id ?? "none";
2058
2112
  }
2059
- async function resolveGatewayBranchPreviousTurnIdForTurn(session, turnId) {
2060
- const turns = await listSessionTurnsOrdered(session);
2113
+ async function resolveGatewayBranchPreviousTurnIdForTurn(server, sessionId, turnId) {
2114
+ const turns = await listSessionTurnsOrdered(server, sessionId);
2061
2115
  const turnIndex = turns.findIndex((turn) => turn.id === turnId);
2062
- return resolveGatewayBranchPreviousTurnId(session, turnIndex, turns);
2116
+ return resolveGatewayBranchPreviousTurnId(server, sessionId, turnIndex, turns);
2063
2117
  }
2064
- async function listSessionTurnsOrdered(session) {
2065
- const turns = [];
2066
- for await (const turn of await session.listTurns()) {
2067
- turns.push(turn);
2068
- }
2118
+ async function listSessionTurnsOrdered(server, sessionId) {
2119
+ const turns = await drainListPages(
2120
+ (pageToken) => server.listTurns({
2121
+ sessionId,
2122
+ ...pageToken != null ? { pageToken } : {}
2123
+ })
2124
+ );
2069
2125
  turns.reverse();
2070
2126
  return turns;
2071
2127
  }
2072
- async function buildTurnAssistantContent(turn, foldState) {
2128
+ async function buildTurnAssistantContent(server, sessionId, turn, foldState) {
2073
2129
  const state = foldState ?? new PeerThreadFoldState();
2074
2130
  const beforeCount = state.threads.get(ROOT_THREAD_ID)?.modelMessageIds.length ?? 0;
2075
- await ingestTurnEventsIntoFold(state, turn);
2131
+ await ingestTurnEventsIntoFold(server, sessionId, turn.id, state);
2076
2132
  const afterIds = state.threads.get(ROOT_THREAD_ID)?.modelMessageIds ?? [];
2077
2133
  const rootModelMessageIds = afterIds.slice(beforeCount);
2078
2134
  return buildTurnUpdateFromFold(state, turn, rootModelMessageIds).content;
2079
2135
  }
2080
- async function convertTurnsToThreadMessages(session) {
2081
- const snapshot = await buildSnapshotFromSession(session);
2136
+ async function convertTurnsToThreadMessages(server, sessionId) {
2137
+ const snapshot = await buildSnapshotFromSession(server, sessionId);
2082
2138
  const messages = projectSessionMessages(snapshot);
2083
2139
  return {
2084
2140
  messages,
@@ -2218,7 +2274,7 @@ function buildMcpAuthUpdate(pendingMcpAuth, foldState, groupRootBaseline) {
2218
2274
  metadata: { custom: mcpAuthMessageCustom(pendingMcpAuth.mcpServers) }
2219
2275
  };
2220
2276
  }
2221
- async function* streamTurnEvents(stream, foldState, groupRootBaseline) {
2277
+ async function* streamTurnEvents(stream, foldState, groupRootBaseline, onTurnIdAvailable) {
2222
2278
  let pendingMcpAuth;
2223
2279
  let sandboxId;
2224
2280
  let sandboxIdYielded = false;
@@ -2239,6 +2295,10 @@ async function* streamTurnEvents(stream, foldState, groupRootBaseline) {
2239
2295
  };
2240
2296
  for await (const data of stream) {
2241
2297
  const event = data.event;
2298
+ if (event.type === "turn.created") {
2299
+ onTurnIdAvailable?.(event.turnId);
2300
+ continue;
2301
+ }
2242
2302
  if (event.type === "sandbox.created") {
2243
2303
  sandboxId = event.sandboxId;
2244
2304
  continue;
@@ -2326,30 +2386,25 @@ function repositoryItemsFromMessages(messages) {
2326
2386
  return items;
2327
2387
  }
2328
2388
 
2329
- // src/private/getGatewayFromPrivateClient.ts
2330
- function getGatewayFromPrivateClient(privateClient) {
2331
- const internal = privateClient;
2332
- if (internal.client == null) {
2333
- throw new Error("PrivateAgentSessionClient is missing an internal gateway client.");
2334
- }
2335
- return internal.client;
2336
- }
2337
-
2338
2389
  // src/private/draftSessionBridge.ts
2339
2390
  var DRAFT_SESSION_LAST_UPDATED_AT_HEADER = "x-tfy-session-last-updated-at";
2340
- function createDraftSessionBridge(privateClient) {
2391
+ function createDraftSessionBridge(server) {
2341
2392
  return {
2342
2393
  async getDraftAgentSpec(draftSessionId) {
2343
- const draft = await privateClient.getDraftSession({ draftSessionId });
2344
- return draft.agentSpec;
2394
+ const session = await server.getSession({ sessionId: draftSessionId });
2395
+ if (session.agentSpec == null) {
2396
+ throw new Error(
2397
+ `Session ${draftSessionId} has no agentSpec (isMutable=${session.isMutable}).`
2398
+ );
2399
+ }
2400
+ return session.agentSpec;
2345
2401
  },
2346
2402
  async syncAgentSpec(draftSessionId, agentSpec) {
2347
- const gateway = getGatewayFromPrivateClient(privateClient);
2348
- const response = await gateway.agents.private.draftSessions.update(
2349
- draftSessionId,
2350
- { agentSpec }
2351
- );
2352
- return response.data.updatedAt;
2403
+ const updated = await server.updateSession({
2404
+ sessionId: draftSessionId,
2405
+ agentSpec
2406
+ });
2407
+ return updated.updatedAt;
2353
2408
  }
2354
2409
  };
2355
2410
  }
@@ -2385,39 +2440,43 @@ function sessionListStartTimestamp() {
2385
2440
  // src/private/truefoundryDraftThreadListAdapter.ts
2386
2441
  var THREAD_LIST_PAGE_SIZE = 20;
2387
2442
  function createTrueFoundryDraftThreadListAdapter(options) {
2388
- const { privateClient, defaultAgentSpec, getAgentSpec } = options;
2443
+ const { server, defaultAgentSpec, getAgentSpec } = options;
2389
2444
  return {
2390
2445
  async list({ after } = {}) {
2391
- const page = await privateClient.listDraftSessions({
2446
+ const page = await server.listSessions({
2392
2447
  limit: THREAD_LIST_PAGE_SIZE,
2393
2448
  pageToken: after,
2394
2449
  startTimestamp: sessionListStartTimestamp()
2395
2450
  });
2396
- const threads = page.data.map((draft) => ({
2451
+ const threads = page.data.filter((session) => session.isMutable).map((draft) => ({
2397
2452
  status: "regular",
2398
2453
  remoteId: draft.id,
2399
- title: draftSessionTitle(draft),
2454
+ title: draftSessionTitle({
2455
+ title: draft.title,
2456
+ agentSpec: draft.agentSpec ?? defaultAgentSpec
2457
+ }),
2400
2458
  lastMessageAt: new Date(draft.updatedAt)
2401
2459
  }));
2402
2460
  return {
2403
2461
  threads,
2404
- nextCursor: page.response.pagination.nextPageToken ?? void 0
2462
+ nextCursor: page.nextPageToken ?? void 0
2405
2463
  };
2406
2464
  },
2407
2465
  async initialize(_threadId) {
2408
- const draft = await privateClient.createDraftSession({
2466
+ const draft = await server.createSession({
2409
2467
  agentSpec: getAgentSpec?.() ?? defaultAgentSpec
2410
2468
  });
2411
2469
  return { remoteId: draft.id, externalId: void 0 };
2412
2470
  },
2413
2471
  async fetch(remoteId) {
2414
- const draft = await privateClient.getDraftSession({
2415
- draftSessionId: remoteId
2416
- });
2472
+ const draft = await server.getSession({ sessionId: remoteId });
2417
2473
  return {
2418
2474
  status: "regular",
2419
2475
  remoteId: draft.id,
2420
- title: draftSessionTitle(draft),
2476
+ title: draftSessionTitle({
2477
+ title: draft.title,
2478
+ agentSpec: draft.agentSpec ?? defaultAgentSpec
2479
+ }),
2421
2480
  lastMessageAt: new Date(draft.updatedAt)
2422
2481
  };
2423
2482
  },
@@ -2450,30 +2509,12 @@ var EMPTY_DRAFT_EXTRAS = {
2450
2509
  }
2451
2510
  };
2452
2511
 
2453
- // src/private/bindDraftAgentSession.ts
2454
- var inflightByDraftId = /* @__PURE__ */ new Map();
2455
- async function bindDraftAgentSession(privateClient, draftSessionId) {
2456
- let inflight = inflightByDraftId.get(draftSessionId);
2457
- if (inflight == null) {
2458
- inflight = privateClient.getDraftSession({ draftSessionId }).then((draft) => draft).finally(() => {
2459
- if (inflightByDraftId.get(draftSessionId) === inflight) {
2460
- inflightByDraftId.delete(draftSessionId);
2461
- }
2462
- });
2463
- inflightByDraftId.set(draftSessionId, inflight);
2464
- }
2465
- return inflight;
2466
- }
2467
-
2468
2512
  // src/sessions.ts
2469
2513
  var inflightBySessionId = /* @__PURE__ */ new Map();
2470
- function getSession(client, sessionId, options) {
2471
- if (options?.privateClient != null) {
2472
- return bindDraftAgentSession(options.privateClient, sessionId);
2473
- }
2514
+ function getSession(server, sessionId) {
2474
2515
  let inflight = inflightBySessionId.get(sessionId);
2475
2516
  if (inflight == null) {
2476
- inflight = client.getSession({ sessionId }).finally(() => {
2517
+ inflight = server.getSession({ sessionId }).finally(() => {
2477
2518
  if (inflightBySessionId.get(sessionId) === inflight) {
2478
2519
  inflightBySessionId.delete(sessionId);
2479
2520
  }
@@ -2486,10 +2527,10 @@ function getSession(client, sessionId, options) {
2486
2527
  // src/truefoundryThreadListAdapter.ts
2487
2528
  var THREAD_LIST_PAGE_SIZE2 = 20;
2488
2529
  function createTrueFoundryThreadListAdapter(options) {
2489
- const { client, agentName } = options;
2530
+ const { server, agentName } = options;
2490
2531
  return {
2491
2532
  async list({ after } = {}) {
2492
- const page = await client.listSessions({
2533
+ const page = await server.listSessions({
2493
2534
  agentName,
2494
2535
  limit: THREAD_LIST_PAGE_SIZE2,
2495
2536
  pageToken: after,
@@ -2503,15 +2544,15 @@ function createTrueFoundryThreadListAdapter(options) {
2503
2544
  }));
2504
2545
  return {
2505
2546
  threads,
2506
- nextCursor: page.response.pagination.nextPageToken ?? void 0
2547
+ nextCursor: page.nextPageToken ?? void 0
2507
2548
  };
2508
2549
  },
2509
2550
  async initialize(_threadId) {
2510
- const session = await client.createSession({ agentName });
2551
+ const session = await server.createSession({ agentName });
2511
2552
  return { remoteId: session.id, externalId: void 0 };
2512
2553
  },
2513
2554
  async fetch(remoteId) {
2514
- const session = await getSession(client, remoteId);
2555
+ const session = await getSession(server, remoteId);
2515
2556
  return {
2516
2557
  status: "regular",
2517
2558
  remoteId: session.id,
@@ -2550,15 +2591,9 @@ function resolveTrueFoundryAgentConfig(options) {
2550
2591
  }
2551
2592
  function resolveTrueFoundryAgentRuntimeOptions(options) {
2552
2593
  const agent = resolveTrueFoundryAgentConfig(options);
2553
- if (agent.mode === "draft" && options.privateClient == null) {
2554
- throw new Error(
2555
- "Draft agent mode requires a `privateClient` PrivateAgentSessionClient."
2556
- );
2557
- }
2558
2594
  return {
2559
2595
  ...options,
2560
- agent,
2561
- privateClient: options.privateClient
2596
+ agent
2562
2597
  };
2563
2598
  }
2564
2599
 
@@ -2769,18 +2804,15 @@ import { useCallback as useCallback2, useEffect as useEffect2, useMemo as useMem
2769
2804
 
2770
2805
  // src/loadSessionSnapshot.ts
2771
2806
  var inflightBySessionId2 = /* @__PURE__ */ new Map();
2772
- function loadSessionSnapshot(client, sessionId, sessionOptions, onProgress) {
2773
- const cacheKey = sessionOptions?.privateClient != null ? `draft:${sessionId}` : sessionId;
2774
- let inflight = inflightBySessionId2.get(cacheKey);
2807
+ function loadSessionSnapshot(server, sessionId, onProgress) {
2808
+ let inflight = inflightBySessionId2.get(sessionId);
2775
2809
  if (inflight == null) {
2776
- inflight = getSession(client, sessionId, sessionOptions).then(
2777
- (session) => buildSnapshotFromSessionEvents(session, onProgress)
2778
- ).finally(() => {
2779
- if (inflightBySessionId2.get(cacheKey) === inflight) {
2780
- inflightBySessionId2.delete(cacheKey);
2810
+ inflight = getSession(server, sessionId).then(() => buildSnapshotFromSessionEvents(server, sessionId, onProgress)).finally(() => {
2811
+ if (inflightBySessionId2.get(sessionId) === inflight) {
2812
+ inflightBySessionId2.delete(sessionId);
2781
2813
  }
2782
2814
  });
2783
- inflightBySessionId2.set(cacheKey, inflight);
2815
+ inflightBySessionId2.set(sessionId, inflight);
2784
2816
  }
2785
2817
  return inflight;
2786
2818
  }
@@ -2818,9 +2850,9 @@ function buildTurnInput(options) {
2818
2850
  }
2819
2851
  return [{ type: "user.message", content: options.userMessage ?? "" }];
2820
2852
  }
2821
- function bindAbort(session, abortSignal) {
2853
+ function bindAbort(server, sessionId, abortSignal) {
2822
2854
  const onAbort = () => {
2823
- void session.cancel().catch(() => void 0);
2855
+ void server.cancelSession({ sessionId }).catch(() => void 0);
2824
2856
  };
2825
2857
  if (abortSignal.aborted) {
2826
2858
  onAbort();
@@ -2829,40 +2861,35 @@ function bindAbort(session, abortSignal) {
2829
2861
  abortSignal.addEventListener("abort", onAbort, { once: true });
2830
2862
  return onAbort;
2831
2863
  }
2832
- async function* streamTurnContent(session, foldState, options, abortSignal, groupRootBaseline, onTurnIdAvailable) {
2833
- const turn = session.prepareTurn({
2834
- input: buildTurnInput(options),
2835
- previousTurnId: options.previousTurnId ?? "auto"
2836
- });
2837
- const onAbort = bindAbort(session, abortSignal);
2864
+ async function* streamTurnContent(server, sessionId, foldState, options, abortSignal, groupRootBaseline, onTurnIdAvailable) {
2865
+ const onAbort = bindAbort(server, sessionId, abortSignal);
2838
2866
  if (abortSignal.aborted) {
2839
2867
  return;
2840
2868
  }
2841
2869
  let turnIdNotified = false;
2842
- const notifyTurnIdIfAvailable = () => {
2843
- if (!turnIdNotified && turn.id != null) {
2844
- onTurnIdAvailable?.(turn.id);
2870
+ const notifyTurnId = (turnId) => {
2871
+ if (!turnIdNotified) {
2872
+ onTurnIdAvailable?.(turnId);
2845
2873
  turnIdNotified = true;
2846
2874
  }
2847
2875
  };
2876
+ const stream = server.prepareAndExecuteTurn({
2877
+ sessionId,
2878
+ input: buildTurnInput(options),
2879
+ previousTurnId: options.previousTurnId ?? "auto",
2880
+ abortSignal,
2881
+ ...options.headers != null ? { headers: options.headers } : {}
2882
+ });
2848
2883
  try {
2849
2884
  for await (const update of streamTurnEvents(
2850
- turn.execute(
2851
- { stream: true },
2852
- {
2853
- abortSignal,
2854
- ...options.headers != null ? { headers: options.headers } : {}
2855
- }
2856
- ),
2885
+ stream,
2857
2886
  foldState,
2858
- groupRootBaseline
2887
+ groupRootBaseline,
2888
+ notifyTurnId
2859
2889
  )) {
2860
- notifyTurnIdIfAvailable();
2861
2890
  yield update;
2862
2891
  }
2863
- notifyTurnIdIfAvailable();
2864
2892
  } catch (error) {
2865
- notifyTurnIdIfAvailable();
2866
2893
  if (error instanceof Error && error.name === "AbortError") {
2867
2894
  return;
2868
2895
  }
@@ -2871,9 +2898,14 @@ async function* streamTurnContent(session, foldState, options, abortSignal, grou
2871
2898
  abortSignal.removeEventListener("abort", onAbort);
2872
2899
  }
2873
2900
  }
2874
- async function* resumeTurnStream(turn, foldState, abortSignal, afterSequenceNumber, groupRootBaseline) {
2901
+ async function* resumeTurnStream(server, sessionId, turnId, foldState, abortSignal, afterSequenceNumber, groupRootBaseline) {
2902
+ if (server.subscribeToTurn == null) {
2903
+ throw new Error(
2904
+ "resumeTurnStream requires AgentChatServer.subscribeToTurn"
2905
+ );
2906
+ }
2875
2907
  const onAbort = () => {
2876
- void turn.session.cancel().catch(() => void 0);
2908
+ void server.cancelSession({ sessionId }).catch(() => void 0);
2877
2909
  };
2878
2910
  if (abortSignal.aborted) {
2879
2911
  onAbort();
@@ -2882,10 +2914,12 @@ async function* resumeTurnStream(turn, foldState, abortSignal, afterSequenceNumb
2882
2914
  abortSignal.addEventListener("abort", onAbort, { once: true });
2883
2915
  try {
2884
2916
  yield* streamTurnEvents(
2885
- turn.stream(
2886
- afterSequenceNumber != null ? { afterSequenceNumber } : {},
2887
- { abortSignal }
2888
- ),
2917
+ server.subscribeToTurn({
2918
+ sessionId,
2919
+ turnId,
2920
+ ...afterSequenceNumber != null ? { afterSequenceNumber } : {},
2921
+ abortSignal
2922
+ }),
2889
2923
  foldState,
2890
2924
  groupRootBaseline
2891
2925
  );
@@ -3021,20 +3055,15 @@ function resolveTurnInput(snapshot, turnId) {
3021
3055
  return void 0;
3022
3056
  }
3023
3057
  function useTrueFoundryAgentMessages({
3024
- client,
3058
+ server,
3025
3059
  sessionId,
3026
3060
  isMain,
3027
3061
  listEventsConcurrency,
3028
3062
  onError,
3029
3063
  initializeSession,
3030
3064
  resolveConversationSessionId,
3031
- privateClient,
3032
3065
  getTurnHeaders
3033
3066
  }) {
3034
- const sessionOptions = useMemo2(
3035
- () => privateClient != null ? { privateClient } : void 0,
3036
- [privateClient]
3037
- );
3038
3067
  const [snapshot, setSnapshot] = useState2(createEmptySessionSnapshot);
3039
3068
  const [isRunning, setIsRunning] = useState2(false);
3040
3069
  const [isLoading, setIsLoading] = useState2(false);
@@ -3194,9 +3223,8 @@ function useTrueFoundryAgentMessages({
3194
3223
  resolveConversationSessionIdRef.current
3195
3224
  );
3196
3225
  const loadedSnapshot = await loadSessionSnapshot(
3197
- client,
3226
+ server,
3198
3227
  conversationSessionId,
3199
- sessionOptions,
3200
3228
  (snap) => {
3201
3229
  if (generation === loadGenerationRef.current) {
3202
3230
  setSnapshot(snap);
@@ -3215,7 +3243,9 @@ function useTrueFoundryAgentMessages({
3215
3243
  const isContinuation = !extractTurnUserText(turn.input);
3216
3244
  void runStream(
3217
3245
  (signal) => resumeTurnStream(
3218
- turn,
3246
+ server,
3247
+ conversationSessionId,
3248
+ turn.id,
3219
3249
  loadedSnapshot.fold,
3220
3250
  signal,
3221
3251
  void 0,
@@ -3235,7 +3265,7 @@ function useTrueFoundryAgentMessages({
3235
3265
  setIsLoading(false);
3236
3266
  }
3237
3267
  }
3238
- }, [client, runStream, sessionId, sessionOptions, loadRetryTrigger, isMain]);
3268
+ }, [server, runStream, sessionId, loadRetryTrigger, isMain]);
3239
3269
  useEffect2(() => {
3240
3270
  void load().catch(() => void 0);
3241
3271
  }, [load]);
@@ -3254,7 +3284,6 @@ function useTrueFoundryAgentMessages({
3254
3284
  activeSessionId,
3255
3285
  resolveConversationSessionIdRef.current
3256
3286
  );
3257
- const session = await getSession(client, conversationSessionId, sessionOptions);
3258
3287
  const turnHeaders = await getTurnHeadersRef.current?.();
3259
3288
  const streamHeaders = turnHeaders != null ? { headers: turnHeaders } : {};
3260
3289
  const isContinuation = "inputs" in options || "resumeMcpAuth" in options && options.resumeMcpAuth === true;
@@ -3315,7 +3344,8 @@ function useTrueFoundryAgentMessages({
3315
3344
  (signal) => {
3316
3345
  if ("inputs" in options) {
3317
3346
  return streamTurnContent(
3318
- session,
3347
+ server,
3348
+ conversationSessionId,
3319
3349
  snapshotRef.current.fold,
3320
3350
  { inputs: options.inputs, ...streamHeaders },
3321
3351
  signal,
@@ -3324,7 +3354,8 @@ function useTrueFoundryAgentMessages({
3324
3354
  }
3325
3355
  if ("resumeMcpAuth" in options) {
3326
3356
  return streamTurnContent(
3327
- session,
3357
+ server,
3358
+ conversationSessionId,
3328
3359
  snapshotRef.current.fold,
3329
3360
  { resumeMcpAuth: true, ...streamHeaders },
3330
3361
  signal,
@@ -3332,11 +3363,12 @@ function useTrueFoundryAgentMessages({
3332
3363
  );
3333
3364
  }
3334
3365
  return streamTurnContent(
3335
- session,
3366
+ server,
3367
+ conversationSessionId,
3336
3368
  snapshotRef.current.fold,
3337
3369
  {
3338
3370
  userMessage: options.userMessage,
3339
- ...options.previousTurnId !== void 0 ? { previousTurnId: options.previousTurnId } : isFirstTurnInSession ? { previousTurnId: "none" } : {},
3371
+ ...options.previousTurnId !== void 0 ? { previousTurnId: options.previousTurnId ?? "none" } : isFirstTurnInSession ? { previousTurnId: "none" } : {},
3340
3372
  ...streamHeaders
3341
3373
  },
3342
3374
  signal,
@@ -3362,7 +3394,7 @@ function useTrueFoundryAgentMessages({
3362
3394
  isContinuation
3363
3395
  );
3364
3396
  },
3365
- [client, runStream, sessionId, sessionOptions]
3397
+ [server, runStream, sessionId]
3366
3398
  );
3367
3399
  const cancel = useCallback2(async () => {
3368
3400
  if (sessionId == null) {
@@ -3373,10 +3405,9 @@ function useTrueFoundryAgentMessages({
3373
3405
  sessionId,
3374
3406
  resolveConversationSessionIdRef.current
3375
3407
  );
3376
- const session = await getSession(client, conversationSessionId, sessionOptions);
3377
- await session.cancel().catch(() => void 0);
3408
+ await server.cancelSession({ sessionId: conversationSessionId }).catch(() => void 0);
3378
3409
  await activeRunRef.current?.catch(() => void 0);
3379
- }, [client, sessionId, sessionOptions]);
3410
+ }, [server, sessionId]);
3380
3411
  const isRunningRef = useRef2(isRunning);
3381
3412
  isRunningRef.current = isRunning;
3382
3413
  const trySendCollectedRequiredActions = useCallback2(
@@ -3438,7 +3469,9 @@ function useTrueFoundryAgentMessages({
3438
3469
  }
3439
3470
  await runStream(
3440
3471
  (signal) => resumeTurnStream(
3441
- turn,
3472
+ server,
3473
+ turn.sessionId,
3474
+ turn.id,
3442
3475
  snapshotRef.current.fold,
3443
3476
  signal,
3444
3477
  void 0,
@@ -3447,7 +3480,7 @@ function useTrueFoundryAgentMessages({
3447
3480
  { current: turn.id },
3448
3481
  true
3449
3482
  );
3450
- }, [runStream]);
3483
+ }, [runStream, server]);
3451
3484
  const branchFromTurn = useCallback2(
3452
3485
  async (turnId, userMessage) => {
3453
3486
  let activeSessionId = sessionId;
@@ -3461,13 +3494,14 @@ function useTrueFoundryAgentMessages({
3461
3494
  activeSessionId,
3462
3495
  resolveConversationSessionIdRef.current
3463
3496
  );
3464
- const session = await getSession(client, conversationSessionId, sessionOptions);
3465
3497
  const previousTurnId = await resolveGatewayBranchPreviousTurnIdForTurn(
3466
- session,
3498
+ server,
3499
+ conversationSessionId,
3467
3500
  turnId
3468
3501
  );
3469
3502
  const rewound = await buildSnapshotBeforeTurn(
3470
- session,
3503
+ server,
3504
+ conversationSessionId,
3471
3505
  turnId,
3472
3506
  listEventsConcurrency
3473
3507
  );
@@ -3482,11 +3516,10 @@ function useTrueFoundryAgentMessages({
3482
3516
  },
3483
3517
  [
3484
3518
  cancel,
3485
- client,
3519
+ server,
3486
3520
  listEventsConcurrency,
3487
3521
  sendTurn,
3488
- sessionId,
3489
- sessionOptions
3522
+ sessionId
3490
3523
  ]
3491
3524
  );
3492
3525
  const resetFromTurn = useCallback2(
@@ -3542,13 +3575,9 @@ function useTrueFoundryAgentMessages({
3542
3575
  sessionId,
3543
3576
  resolveConversationSessionIdRef.current
3544
3577
  );
3545
- const session = await getSession(
3546
- client,
3547
- conversationSessionId,
3548
- sessionOptions
3549
- );
3550
3578
  const next = await prependOlderSessionHistory(
3551
- session,
3579
+ server,
3580
+ conversationSessionId,
3552
3581
  snapshotRef.current
3553
3582
  );
3554
3583
  if (generation !== loadGenerationRef.current) {
@@ -3569,7 +3598,7 @@ function useTrueFoundryAgentMessages({
3569
3598
  })();
3570
3599
  loadOlderInflightRef.current = run;
3571
3600
  return run;
3572
- }, [client, isMain, sessionId, sessionOptions]);
3601
+ }, [server, isMain, sessionId]);
3573
3602
  return {
3574
3603
  messages,
3575
3604
  isRunning,
@@ -3592,16 +3621,15 @@ function useTrueFoundryAgentMessages({
3592
3621
  // src/useTrueFoundryAgentRuntime.ts
3593
3622
  function useTrueFoundryAgentRuntimeImpl(options, pendingAgentSpecRef) {
3594
3623
  const {
3595
- client,
3624
+ server,
3596
3625
  agent,
3597
- privateClient,
3598
3626
  adapters,
3599
3627
  onError,
3600
3628
  listEventsConcurrency,
3601
3629
  ...sharedOptions
3602
3630
  } = options;
3603
3631
  const draftBridgeRef = useRef3(
3604
- agent.mode === "draft" && privateClient != null ? createDraftSessionBridge(privateClient) : null
3632
+ agent.mode === "draft" ? createDraftSessionBridge(server) : null
3605
3633
  );
3606
3634
  const draftSessionId = useAuiState(
3607
3635
  (state) => agent.mode === "draft" ? state.threadListItem.remoteId ?? void 0 : void 0
@@ -3652,13 +3680,12 @@ function useTrueFoundryAgentRuntimeImpl(options, pendingAgentSpecRef) {
3652
3680
  resetFromTurn,
3653
3681
  retryLoad
3654
3682
  } = useTrueFoundryAgentMessages({
3655
- client,
3683
+ server,
3656
3684
  sessionId,
3657
3685
  isMain,
3658
3686
  listEventsConcurrency,
3659
3687
  onError,
3660
3688
  initializeSession,
3661
- privateClient: agent.mode === "draft" ? privateClient : void 0,
3662
3689
  getTurnHeaders: agent.mode === "draft" ? getTurnHeaders : void 0
3663
3690
  });
3664
3691
  if (agent.mode === "draft" && draftSpec.agentSpec != null) {
@@ -3680,9 +3707,9 @@ function useTrueFoundryAgentRuntimeImpl(options, pendingAgentSpecRef) {
3680
3707
  );
3681
3708
  const downloadSandboxFile = useCallback3(
3682
3709
  async (path) => {
3683
- if (privateClient == null) {
3710
+ if (server.downloadSandboxFile == null) {
3684
3711
  const error = new Error(
3685
- "Downloading a sandbox file requires a `privateClient` PrivateAgentSessionClient."
3712
+ "Downloading a sandbox file requires AgentChatServer.downloadSandboxFile."
3686
3713
  );
3687
3714
  onError?.(error);
3688
3715
  throw error;
@@ -3692,10 +3719,9 @@ function useTrueFoundryAgentRuntimeImpl(options, pendingAgentSpecRef) {
3692
3719
  onError?.(error);
3693
3720
  throw error;
3694
3721
  }
3695
- const response = await privateClient.downloadSandboxFile(sandboxId, { path });
3696
- return await response.blob();
3722
+ return await server.downloadSandboxFile(sandboxId, { path });
3697
3723
  },
3698
- [privateClient, sandboxId, onError]
3724
+ [server, sandboxId, onError]
3699
3725
  );
3700
3726
  const draftExtras = useMemo3(() => {
3701
3727
  if (agent.mode !== "draft") {
@@ -3780,7 +3806,7 @@ function useTrueFoundryAgentRuntimeImpl(options, pendingAgentSpecRef) {
3780
3806
  }
3781
3807
  function useTrueFoundryAgentRuntime(options) {
3782
3808
  const resolved = resolveTrueFoundryAgentRuntimeOptions(options);
3783
- const { client, agent, privateClient } = resolved;
3809
+ const { server, agent } = resolved;
3784
3810
  const pendingAgentSpecRef = useRef3(
3785
3811
  agent.mode === "draft" ? agent.defaultAgentSpec : void 0
3786
3812
  );
@@ -3788,23 +3814,18 @@ function useTrueFoundryAgentRuntime(options) {
3788
3814
  const namedAgentName = agent.mode === "named" ? agent.agentName : void 0;
3789
3815
  const threadListAdapter = useMemo3(() => {
3790
3816
  if (agentMode === "draft") {
3791
- if (privateClient == null) {
3792
- throw new Error(
3793
- "Draft agent mode requires a `privateClient` PrivateAgentSessionClient."
3794
- );
3795
- }
3796
3817
  const draftAgent = agent;
3797
3818
  return createTrueFoundryDraftThreadListAdapter({
3798
- privateClient,
3819
+ server,
3799
3820
  defaultAgentSpec: draftAgent.defaultAgentSpec,
3800
3821
  getAgentSpec: () => pendingAgentSpecRef.current ?? draftAgent.defaultAgentSpec
3801
3822
  });
3802
3823
  }
3803
3824
  return createTrueFoundryThreadListAdapter({
3804
- client,
3825
+ server,
3805
3826
  agentName: namedAgentName
3806
3827
  });
3807
- }, [agentMode, namedAgentName, client, privateClient]);
3828
+ }, [agentMode, namedAgentName, server]);
3808
3829
  return useRemoteThreadListRuntime({
3809
3830
  allowNesting: true,
3810
3831
  adapter: threadListAdapter,
@@ -3818,16 +3839,19 @@ function useTrueFoundryAgentRuntime(options) {
3818
3839
  // src/truefoundryOwnedSessionsThreadListAdapter.ts
3819
3840
  var THREAD_LIST_PAGE_SIZE3 = 20;
3820
3841
  function ownedSessionTitle(session) {
3821
- if (session.type === "session/draft") {
3822
- return draftSessionTitle(session);
3842
+ if (session.isMutable && session.agentSpec != null) {
3843
+ return draftSessionTitle({
3844
+ title: session.title,
3845
+ agentSpec: session.agentSpec
3846
+ });
3823
3847
  }
3824
- return session.title ?? session.agentName;
3848
+ return session.title ?? session.agentName ?? session.id;
3825
3849
  }
3826
3850
  function createTrueFoundryOwnedSessionsThreadListAdapter(options) {
3827
- const { privateClient } = options;
3851
+ const { server } = options;
3828
3852
  return {
3829
3853
  async list({ after } = {}) {
3830
- const page = await privateClient.listOwnedSessions({
3854
+ const page = await server.listSessions({
3831
3855
  limit: THREAD_LIST_PAGE_SIZE3,
3832
3856
  pageToken: after,
3833
3857
  startTimestamp: sessionListStartTimestamp()
@@ -3840,7 +3864,7 @@ function createTrueFoundryOwnedSessionsThreadListAdapter(options) {
3840
3864
  }));
3841
3865
  return {
3842
3866
  threads,
3843
- nextCursor: page.response.pagination.nextPageToken ?? void 0
3867
+ nextCursor: page.nextPageToken ?? void 0
3844
3868
  };
3845
3869
  },
3846
3870
  async initialize() {
@@ -3849,14 +3873,12 @@ function createTrueFoundryOwnedSessionsThreadListAdapter(options) {
3849
3873
  );
3850
3874
  },
3851
3875
  async fetch(remoteId) {
3852
- const draft = await privateClient.getDraftSession({
3853
- draftSessionId: remoteId
3854
- });
3876
+ const session = await server.getSession({ sessionId: remoteId });
3855
3877
  return {
3856
3878
  status: "regular",
3857
- remoteId: draft.id,
3858
- title: draftSessionTitle(draft),
3859
- lastMessageAt: new Date(draft.updatedAt)
3879
+ remoteId: session.id,
3880
+ title: ownedSessionTitle(session),
3881
+ lastMessageAt: new Date(session.updatedAt)
3860
3882
  };
3861
3883
  },
3862
3884
  async rename() {
@@ -3873,9 +3895,6 @@ function createTrueFoundryOwnedSessionsThreadListAdapter(options) {
3873
3895
  };
3874
3896
  }
3875
3897
 
3876
- // src/index.ts
3877
- import { PrivateAgentSessionClient } from "truefoundry-gateway-sdk/agents/private";
3878
-
3879
3898
  // src/hooks.ts
3880
3899
  import { useMemo as useMemo4 } from "react";
3881
3900
  import { useAui as useAui2 } from "@assistant-ui/store";
@@ -4020,7 +4039,6 @@ var trueFoundryAttachmentAdapter = {
4020
4039
  }
4021
4040
  };
4022
4041
  export {
4023
- PrivateAgentSessionClient,
4024
4042
  ROOT_THREAD_ID,
4025
4043
  TOOL_RESPONSE_THREAD_ID_CUSTOM_KEY,
4026
4044
  buildEditedUserMessageContent,
@@ -4031,14 +4049,23 @@ export {
4031
4049
  collectResponseInputs,
4032
4050
  convertTurnsToThreadMessages,
4033
4051
  createDraftSessionBridge,
4052
+ createTrueFoundryChatServer,
4034
4053
  createTrueFoundryDraftThreadListAdapter,
4035
4054
  createTrueFoundryOwnedSessionsThreadListAdapter,
4036
4055
  createTrueFoundryThreadListAdapter,
4037
4056
  draftSessionTitle,
4038
4057
  findPausedAssistantMessage,
4039
4058
  getSession,
4059
+ getTfyMcpInitServers,
4060
+ getTfyThreadState,
4061
+ getTfyUsage,
4040
4062
  getTurnMessageContent,
4063
+ isEventDelta,
4064
+ isTfyMcpToolInfo,
4065
+ isTfySystemToolInfo,
4066
+ isTfyToolInfo,
4041
4067
  mergeAgentSpec,
4068
+ mergeEventDelta,
4042
4069
  messageHasPendingApprovals,
4043
4070
  messageHasPendingRequiredActions,
4044
4071
  messageHasPendingResponses,