@truefoundry/assistant-ui-runtime 0.1.14 → 0.1.16
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.d.ts +0 -1
- package/dist/index.js +73 -61
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/convertTurnMessages.test.ts +506 -44
- package/src/convertTurnMessages.ts +133 -85
- package/src/types.ts +0 -1
- package/src/useTrueFoundryAgentMessages.test.tsx +30 -26
- package/src/useTrueFoundryAgentMessages.ts +6 -13
- package/src/useTrueFoundryAgentRuntime.ts +0 -2
package/dist/index.d.ts
CHANGED
|
@@ -25,7 +25,6 @@ type TrueFoundryAgentRuntimeBaseOptions = ExternalStoreSharedOptions & {
|
|
|
25
25
|
threadId?: string | undefined;
|
|
26
26
|
onThreadIdChange?: ((threadId: string | undefined) => void) | undefined;
|
|
27
27
|
onError?: ((error: unknown) => void) | undefined;
|
|
28
|
-
listEventsConcurrency?: number | undefined;
|
|
29
28
|
/**
|
|
30
29
|
* Optional filter forwarded to `listSessions({ agentId })`.
|
|
31
30
|
* Omit for all chats; hosts that key agents by name pass that name as the id.
|
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
|
-
|
|
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
|
|
1555
|
-
|
|
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
|
-
|
|
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;
|
|
@@ -2082,56 +2140,20 @@ async function buildSnapshotFromSession(server, sessionId, concurrency = DEFAULT
|
|
|
2082
2140
|
pendingUser: void 0
|
|
2083
2141
|
});
|
|
2084
2142
|
}
|
|
2085
|
-
async function
|
|
2086
|
-
|
|
2087
|
-
const beforeIndex = turns.findIndex((turn) => turn.id === beforeTurnId);
|
|
2088
|
-
if (beforeIndex === -1) {
|
|
2089
|
-
throw new Error(`Turn ${beforeTurnId} not found in session`);
|
|
2090
|
-
}
|
|
2091
|
-
return buildSnapshotBeforeTurnIndex(
|
|
2092
|
-
server,
|
|
2093
|
-
sessionId,
|
|
2094
|
-
beforeIndex,
|
|
2095
|
-
concurrency,
|
|
2096
|
-
turns
|
|
2097
|
-
);
|
|
2098
|
-
}
|
|
2099
|
-
async function buildSnapshotBeforeTurnIndex(server, sessionId, turnIndex, _concurrency = DEFAULT_LIST_EVENTS_CONCURRENCY, orderedTurns) {
|
|
2100
|
-
if (turnIndex <= 0) {
|
|
2143
|
+
async function buildSnapshotThroughTurn(server, sessionId, anchorTurnId) {
|
|
2144
|
+
if (anchorTurnId == null) {
|
|
2101
2145
|
return createEmptySessionSnapshot();
|
|
2102
2146
|
}
|
|
2103
|
-
const
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
if (lastTurnId == null) {
|
|
2107
|
-
return createEmptySessionSnapshot();
|
|
2108
|
-
}
|
|
2109
|
-
const items = await fetchAllSessionEvents(server, sessionId, { lastTurnId });
|
|
2147
|
+
const items = await fetchAllSessionEvents(server, sessionId, {
|
|
2148
|
+
lastTurnId: anchorTurnId
|
|
2149
|
+
});
|
|
2110
2150
|
const snapshot = createEmptySessionSnapshot();
|
|
2111
2151
|
ingestSessionEventsIntoSnapshot(snapshot, items);
|
|
2112
2152
|
return snapshot;
|
|
2113
2153
|
}
|
|
2114
|
-
async function resolveGatewayBranchPreviousTurnId(server, sessionId, turnIndex, orderedTurns) {
|
|
2115
|
-
if (turnIndex <= 0) {
|
|
2116
|
-
return "none";
|
|
2117
|
-
}
|
|
2118
|
-
const turns = orderedTurns ?? await listSessionTurnsOrdered(server, sessionId);
|
|
2119
|
-
return turns[turnIndex - 1]?.id ?? "none";
|
|
2120
|
-
}
|
|
2121
2154
|
async function resolveGatewayBranchPreviousTurnIdForTurn(server, sessionId, turnId) {
|
|
2122
|
-
const
|
|
2123
|
-
|
|
2124
|
-
return resolveGatewayBranchPreviousTurnId(server, sessionId, turnIndex, turns);
|
|
2125
|
-
}
|
|
2126
|
-
async function listSessionTurnsOrdered(server, sessionId) {
|
|
2127
|
-
const turns = await drainListPages(
|
|
2128
|
-
(pageToken) => server.listTurns({
|
|
2129
|
-
sessionId,
|
|
2130
|
-
...pageToken != null ? { pageToken } : {}
|
|
2131
|
-
})
|
|
2132
|
-
);
|
|
2133
|
-
turns.reverse();
|
|
2134
|
-
return turns;
|
|
2155
|
+
const turn = await server.getTurn({ sessionId, turnId });
|
|
2156
|
+
return turn.previousTurnId ?? "none";
|
|
2135
2157
|
}
|
|
2136
2158
|
async function buildTurnAssistantContent(server, sessionId, turn, foldState) {
|
|
2137
2159
|
const state = foldState ?? new PeerThreadFoldState();
|
|
@@ -3127,7 +3149,6 @@ function useTrueFoundryAgentMessages({
|
|
|
3127
3149
|
sessionId,
|
|
3128
3150
|
isMain,
|
|
3129
3151
|
isInitialSession,
|
|
3130
|
-
listEventsConcurrency,
|
|
3131
3152
|
onError,
|
|
3132
3153
|
initializeSession,
|
|
3133
3154
|
resolveConversationSessionId,
|
|
@@ -3658,11 +3679,10 @@ function useTrueFoundryAgentMessages({
|
|
|
3658
3679
|
conversationSessionId,
|
|
3659
3680
|
turnId
|
|
3660
3681
|
);
|
|
3661
|
-
rewound = await
|
|
3682
|
+
rewound = await buildSnapshotThroughTurn(
|
|
3662
3683
|
server,
|
|
3663
3684
|
conversationSessionId,
|
|
3664
|
-
|
|
3665
|
-
listEventsConcurrency
|
|
3685
|
+
previousTurnId === "none" ? null : previousTurnId
|
|
3666
3686
|
);
|
|
3667
3687
|
createdAtByMessageIdRef.current = /* @__PURE__ */ new Map();
|
|
3668
3688
|
snapshotRef.current = rewound;
|
|
@@ -3678,13 +3698,7 @@ function useTrueFoundryAgentMessages({
|
|
|
3678
3698
|
branchRollbackSnapshot: committed
|
|
3679
3699
|
});
|
|
3680
3700
|
},
|
|
3681
|
-
[
|
|
3682
|
-
cancel,
|
|
3683
|
-
server,
|
|
3684
|
-
listEventsConcurrency,
|
|
3685
|
-
sendTurn,
|
|
3686
|
-
sessionId
|
|
3687
|
-
]
|
|
3701
|
+
[cancel, server, sendTurn, sessionId]
|
|
3688
3702
|
);
|
|
3689
3703
|
const resetFromTurn = useCallback2(
|
|
3690
3704
|
async (turnId) => {
|
|
@@ -3794,7 +3808,6 @@ function useTrueFoundryAgentRuntimeImpl(options, pendingAgentSpecRef) {
|
|
|
3794
3808
|
agent,
|
|
3795
3809
|
adapters,
|
|
3796
3810
|
onError,
|
|
3797
|
-
listEventsConcurrency,
|
|
3798
3811
|
...sharedOptions
|
|
3799
3812
|
} = options;
|
|
3800
3813
|
const draftBridgeRef = useRef3(
|
|
@@ -3855,7 +3868,6 @@ function useTrueFoundryAgentRuntimeImpl(options, pendingAgentSpecRef) {
|
|
|
3855
3868
|
sessionId,
|
|
3856
3869
|
isMain,
|
|
3857
3870
|
isInitialSession,
|
|
3858
|
-
listEventsConcurrency,
|
|
3859
3871
|
onError,
|
|
3860
3872
|
initializeSession,
|
|
3861
3873
|
getTurnHeaders: agent.mode === "draft" ? getTurnHeaders : void 0
|