|
1 | 1 | import { |
2 | 2 | EnvironmentId, |
3 | 3 | GUEST_MESSAGE_MAX_LENGTH, |
| 4 | + GUEST_HISTORY_ROUTE, |
4 | 5 | GUEST_NAME_MAX_LENGTH, |
5 | 6 | GUEST_ROUTE_PREFIX, |
6 | 7 | GUEST_SHARE_PARAM, |
7 | 8 | GuestMessageId, |
8 | 9 | type GuestRedeemRequest, |
9 | 10 | type GuestShareView, |
10 | 11 | type GuestThreadModel, |
| 12 | + OrchestrationV2ThreadHistoryPage, |
11 | 13 | type OrchestrationV2ThreadProjection, |
12 | 14 | type ThreadGuestMessage, |
13 | 15 | type ThreadGuestMessageStatus, |
@@ -46,7 +48,7 @@ import { GuestMessageAttachments } from "../chat/GuestMessageAttachments"; |
46 | 48 | import { HostImageUrlContext } from "../chat/hostImageUrl"; |
47 | 49 | import { PierreEntryIcon } from "../chat/PierreEntryIcon"; |
48 | 50 | import { GuestMessageName, guestBubbleClassName } from "../chat/guestMessageStyle"; |
49 | | -import { MessagesTimeline } from "../chat/MessagesTimeline"; |
| 51 | +import { MessagesTimeline, type MessagesTimelineHistoryControls } from "../chat/MessagesTimeline"; |
50 | 52 | import { ProviderInstanceIcon } from "../chat/ProviderInstanceIcon"; |
51 | 53 | import { getDisplayModelName, getTriggerDisplayModelName } from "../chat/providerIconUtils"; |
52 | 54 | import { ScrollToEndButton } from "../chat/TimelineOverlays"; |
@@ -131,6 +133,32 @@ function takeGuestEntry(): GuestEntry { |
131 | 133 | } |
132 | 134 |
|
133 | 135 | const decodeErrorBody = Schema.decodeUnknownOption(Schema.Struct({ message: Schema.String })); |
| 136 | +const decodeHistoryPage = Schema.decodeUnknownOption( |
| 137 | + Schema.toCodecJson(OrchestrationV2ThreadHistoryPage), |
| 138 | +); |
| 139 | + |
| 140 | +/** Resolves to the page, or to a message the guest can read. */ |
| 141 | +async function fetchGuestHistoryPage( |
| 142 | + shareId: string, |
| 143 | + cursor: string, |
| 144 | +): Promise<OrchestrationV2ThreadHistoryPage | string> { |
| 145 | + const failed = "Could not load earlier turns. Try again."; |
| 146 | + try { |
| 147 | + const response = await fetch( |
| 148 | + `${GUEST_HISTORY_ROUTE}?${GUEST_SHARE_PARAM}=${encodeURIComponent(shareId)}&cursor=${encodeURIComponent(cursor)}`, |
| 149 | + ); |
| 150 | + const json: unknown = await response.json().catch(() => null); |
| 151 | + if (!response.ok) { |
| 152 | + return Option.match(decodeErrorBody(json), { |
| 153 | + onNone: () => failed, |
| 154 | + onSome: ({ message }) => message, |
| 155 | + }); |
| 156 | + } |
| 157 | + return Option.getOrElse(decodeHistoryPage(json), () => failed); |
| 158 | + } catch { |
| 159 | + return "Could not reach the server."; |
| 160 | + } |
| 161 | +} |
134 | 162 |
|
135 | 163 | /** Resolves to null on success, or to a message the guest can read. */ |
136 | 164 | async function postGuest( |
@@ -241,6 +269,32 @@ function GuestSession({ shareId }: { readonly shareId: string }) { |
241 | 269 | // missing, invalid, or ended session. Network drops reconnect on their own. |
242 | 270 | const [rejected, setRejected] = useState(false); |
243 | 271 |
|
| 272 | + const [historyLoad, setHistoryLoad] = useState<{ |
| 273 | + readonly cursor: string; |
| 274 | + readonly error: string | null; |
| 275 | + } | null>(null); |
| 276 | + const historyCursor = stream.historyCursor; |
| 277 | + // A load for an older cursor is stale: a page or a reconnect snapshot moved the cursor on. |
| 278 | + const currentLoad = historyLoad?.cursor === historyCursor ? historyLoad : null; |
| 279 | + const loadingHistory = currentLoad !== null && currentLoad.error === null; |
| 280 | + const historyError = currentLoad?.error ?? null; |
| 281 | + const loadEarlier = async () => { |
| 282 | + if (historyCursor === null || loadingHistory) return; |
| 283 | + setHistoryLoad({ cursor: historyCursor, error: null }); |
| 284 | + const result = await fetchGuestHistoryPage(shareId, historyCursor); |
| 285 | + if (typeof result === "string") setHistoryLoad({ cursor: historyCursor, error: result }); |
| 286 | + else dispatch({ type: "history-page", cursor: historyCursor, page: result }); |
| 287 | + }; |
| 288 | + const historyControls: MessagesTimelineHistoryControls | undefined = |
| 289 | + historyCursor === null |
| 290 | + ? undefined |
| 291 | + : { |
| 292 | + hasMoreHistory: true, |
| 293 | + loading: loadingHistory, |
| 294 | + error: historyError, |
| 295 | + onLoadEarlier: () => void loadEarlier(), |
| 296 | + }; |
| 297 | + |
244 | 298 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- sets document.title to the shared thread's title |
245 | 299 | useEffect(() => { |
246 | 300 | if (stream.view) document.title = stream.view.title; |
@@ -273,6 +327,7 @@ function GuestSession({ shareId }: { readonly shareId: string }) { |
273 | 327 | view={stream.view} |
274 | 328 | projection={stream.projection} |
275 | 329 | awaitingRowIds={stream.awaitingRowIds} |
| 330 | + historyControls={historyControls} |
276 | 331 | /> |
277 | 332 | ); |
278 | 333 | } |
@@ -358,10 +413,12 @@ function GuestThread({ |
358 | 413 | view: { shareId, title, expiresAt, messages, model }, |
359 | 414 | projection, |
360 | 415 | awaitingRowIds, |
| 416 | + historyControls, |
361 | 417 | }: { |
362 | 418 | readonly view: GuestShareView; |
363 | 419 | readonly projection: OrchestrationV2ThreadProjection; |
364 | 420 | readonly awaitingRowIds: GuestStreamState["awaitingRowIds"]; |
| 421 | + readonly historyControls: MessagesTimelineHistoryControls | undefined; |
365 | 422 | }) { |
366 | 423 | // No ":" so the timeline finds no environment thread behind the key. |
367 | 424 | const threadKey = `guest-${shareId}`; |
@@ -467,6 +524,7 @@ function GuestThread({ |
467 | 524 | onFileDownload={openFile} |
468 | 525 | renderHtmlRender={renderHtmlRender} |
469 | 526 | turnItemSource={turnItemSource} |
| 527 | + historyControls={historyControls} |
470 | 528 | activeThreadEnvironmentId={GUEST_ENVIRONMENT_ID} |
471 | 529 | markdownCwd={undefined} |
472 | 530 | resolvedTheme={resolvedTheme} |
|
0 commit comments