The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | applyCommandOutputFrame, |
| 3 | EMPTY_COMMAND_OUTPUT, |
| 4 | } from "@t3tools/client-runtime/state/command-output"; |
| 5 | import { |
| 6 | GUEST_COMMAND_OUTPUT_ROUTE, |
| 7 | GUEST_ITEM_ROUTE, |
| 8 | GUEST_SHARE_PARAM, |
| 9 | OrchestrationV2CommandOutputFrame, |
| 10 | OrchestrationV2GetTurnItemResult, |
| 11 | } from "@t3tools/contracts"; |
| 12 | import * as Cause from "effect/Cause"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import * as Option from "effect/Option"; |
| 15 | import * as Queue from "effect/Queue"; |
| 16 | import * as Schema from "effect/Schema"; |
| 17 | import * as Stream from "effect/Stream"; |
| 18 | import { Atom } from "effect/reactivity"; |
| 19 | import { msg } from "gt-react"; |
| 20 | |
| 21 | import { m } from "~/i18n/messages"; |
| 22 | |
| 23 | import type { TurnItemSource } from "../chat/timelineRowContext"; |
| 24 | |
| 25 | /** What an expanded guest row says when it cannot read the item. */ |
| 26 | class GuestItemReadError extends Schema.TaggedError<GuestItemReadError>()("GuestItemReadError", { |
| 27 | message: Schema.String, |
| 28 | }) {} |
| 29 | |
| 30 | const decodeDetail = Schema.decodeUnknownSync(Schema.toCodecJson(OrchestrationV2GetTurnItemResult)); |
| 31 | const decodeFrame = Schema.decodeUnknownOption( |
| 32 | Schema.fromJsonString(OrchestrationV2CommandOutputFrame), |
| 33 | ); |
| 34 | |
| 35 | const itemUrl = (route: string, shareId: string, itemId: string) => |
| 36 | `${route}?${GUEST_SHARE_PARAM}=${encodeURIComponent(shareId)}&item=${encodeURIComponent(itemId)}`; |
| 37 | |
| 38 | /** `item` is null when the thread no longer has the item, or the share ended. */ |
| 39 | async function fetchGuestItemDetail( |
| 40 | shareId: string, |
| 41 | itemId: string, |
| 42 | ): Promise<OrchestrationV2GetTurnItemResult> { |
| 43 | const response = await fetch(itemUrl(GUEST_ITEM_ROUTE, shareId, itemId)); |
| 44 | if (response.status === 403) return { item: null }; |
| 45 | if (!response.ok) throw new Error(`Guest item request failed with ${response.status}.`); |
| 46 | return decodeDetail(await response.json()); |
| 47 | } |
| 48 | |
| 49 | // Keys are JSON so a share id or item id can never run into the next part. |
| 50 | const guestItemDetailAtom = Atom.family((key: string) => { |
| 51 | const [shareId, itemId] = JSON.parse(key) as [string, string, string]; |
| 52 | return Atom.make( |
| 53 | Effect.tryPromise({ |
| 54 | try: () => fetchGuestItemDetail(shareId, itemId), |
| 55 | catch: () => new GuestItemReadError({ message: m(msg("Could not load this item.")) }), |
| 56 | }), |
| 57 | ); |
| 58 | }); |
| 59 | |
| 60 | /** Ends after the frame of a settled command, so the browser does not reconnect to it. */ |
| 61 | const guestCommandOutputAtom = Atom.family((key: string) => { |
| 62 | const [shareId, itemId] = JSON.parse(key) as [string, string]; |
| 63 | const frames = Stream.callback<OrchestrationV2CommandOutputFrame, GuestItemReadError>((queue) => |
| 64 | Effect.acquireRelease( |
| 65 | Effect.sync(() => { |
| 66 | const source = new EventSource(itemUrl(GUEST_COMMAND_OUTPUT_ROUTE, shareId, itemId)); |
| 67 | source.addEventListener("message", (message) => { |
| 68 | const frame = decodeFrame(message.data); |
| 69 | if (Option.isNone(frame)) return; |
| 70 | Queue.offerUnsafe(queue, frame.value); |
| 71 | if (!frame.value.running) { |
| 72 | source.close(); |
| 73 | Queue.endUnsafe(queue); |
| 74 | } |
| 75 | }); |
| 76 | // A non-200 response closes the source for good: the share ended or the item is gone. |
| 77 | source.addEventListener("error", () => { |
| 78 | if (source.readyState === EventSource.CLOSED) { |
| 79 | Queue.failCauseUnsafe( |
| 80 | queue, |
| 81 | Cause.fail( |
| 82 | new GuestItemReadError({ message: m(msg("Output is no longer available.")) }), |
| 83 | ), |
| 84 | ); |
| 85 | } |
| 86 | }); |
| 87 | return source; |
| 88 | }), |
| 89 | (source) => Effect.sync(() => source.close()), |
| 90 | ), |
| 91 | ); |
| 92 | // `scan` emits its seed first; skipping it keeps the row "loading" until the first frame. |
| 93 | return Atom.make( |
| 94 | frames.pipe( |
| 95 | Stream.scan(() => EMPTY_COMMAND_OUTPUT, applyCommandOutputFrame), |
| 96 | Stream.filter((view) => view !== EMPTY_COMMAND_OUTPUT), |
| 97 | ), |
| 98 | ); |
| 99 | }); |
| 100 | |
| 101 | /** Expanded rows on the guest page read through the guest routes, with the page's share cookie. */ |
| 102 | export const guestTurnItemSource = (shareId: string): TurnItemSource => ({ |
| 103 | detail: ({ itemId, revision }) => |
| 104 | guestItemDetailAtom(JSON.stringify([shareId, itemId, revision])), |
| 105 | commandOutput: ({ itemId }) => guestCommandOutputAtom(JSON.stringify([shareId, itemId])), |
| 106 | }); |