@truefoundry/assistant-ui-runtime 0.1.14 → 0.1.15

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/dist/index.js CHANGED
@@ -1534,7 +1534,6 @@ function attachRunningTurn(snapshot, runningTurn) {
1534
1534
  if (runningTurn == null) {
1535
1535
  return snapshot;
1536
1536
  }
1537
- applyUserToolResponsesToFold(snapshot.fold, runningTurn.input ?? []);
1538
1537
  const pendingUserText = extractTurnUserText(runningTurn.input);
1539
1538
  return replaceSessionSnapshot(snapshot, {
1540
1539
  runningTurn,
@@ -1549,10 +1548,63 @@ function attachRunningTurn(snapshot, runningTurn) {
1549
1548
  } : {}
1550
1549
  });
1551
1550
  }
1552
- async function buildSnapshotFromSessionEvents(server, sessionId, onProgress) {
1551
+ function findOpenTurnCreated(itemsAsc) {
1552
+ let open;
1553
+ for (const item of itemsAsc) {
1554
+ if (item.event.type === "turn.created") {
1555
+ open = { turnId: item.turnId, event: item.event };
1556
+ } else if (item.event.type === "turn.done") {
1557
+ open = void 0;
1558
+ }
1559
+ }
1560
+ return open;
1561
+ }
1562
+ function turnFromCreatedEvent(options) {
1563
+ const { sessionId, turnId, event } = options;
1564
+ return {
1565
+ id: turnId,
1566
+ sessionId,
1567
+ state: { status: "running" },
1568
+ createdAt: event.createdAt,
1569
+ ...event.input != null ? { input: event.input } : {},
1570
+ ...event.previousTurnId === void 0 ? {} : { previousTurnId: event.previousTurnId }
1571
+ };
1572
+ }
1573
+ async function resolveSessionTip(options) {
1574
+ const { server, sessionId, itemsAsc } = options;
1575
+ const open = findOpenTurnCreated(itemsAsc);
1576
+ if (open != null) {
1577
+ const continuationInput = open.event.input ?? [];
1578
+ if (typeof server.getTurn === "function") {
1579
+ try {
1580
+ const turn = await server.getTurn({
1581
+ sessionId,
1582
+ turnId: open.turnId
1583
+ });
1584
+ if (turn.state.status === "running") {
1585
+ return {
1586
+ runningTurn: turn,
1587
+ continuationInput: turn.input ?? continuationInput
1588
+ };
1589
+ }
1590
+ return { continuationInput: turn.input ?? continuationInput };
1591
+ } catch {
1592
+ }
1593
+ }
1594
+ return {
1595
+ runningTurn: turnFromCreatedEvent({
1596
+ sessionId,
1597
+ turnId: open.turnId,
1598
+ event: open.event
1599
+ }),
1600
+ continuationInput
1601
+ };
1602
+ }
1553
1603
  const turnsPage = await server.listTurns({ sessionId, limit: 1 });
1554
- const newestTurn = turnsPage.data[0];
1555
- const runningTurn = newestTurn?.state?.status === "running" ? newestTurn : void 0;
1604
+ const tip = turnsPage.data[0];
1605
+ return tip?.state?.status === "running" ? { runningTurn: tip, continuationInput: tip.input ?? [] } : { continuationInput: [] };
1606
+ }
1607
+ async function buildSnapshotFromSessionEvents(server, sessionId, onProgress) {
1556
1608
  const window = await fetchSessionEventsWindow(server, sessionId);
1557
1609
  const historyPagination = {
1558
1610
  hasOlder: window.hasOlder,
@@ -1564,7 +1616,13 @@ async function buildSnapshotFromSessionEvents(server, sessionId, onProgress) {
1564
1616
  historyEvents: window.itemsAsc,
1565
1617
  historyPagination
1566
1618
  });
1567
- return attachRunningTurn(withHistory, runningTurn);
1619
+ const tip = await resolveSessionTip({
1620
+ server,
1621
+ sessionId,
1622
+ itemsAsc: window.itemsAsc
1623
+ });
1624
+ applyUserToolResponsesToFold(withHistory.fold, tip.continuationInput);
1625
+ return attachRunningTurn(withHistory, tip.runningTurn);
1568
1626
  }
1569
1627
  async function prependOlderSessionHistory(server, sessionId, snapshot) {
1570
1628
  const pagination = snapshot.historyPagination;