1import type { ThreadId } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4
5import type { RemoteEnvironmentAuthorization } from "../authorization/service.ts";
6import type { PreparedConnection } from "../connection/model.ts";
7import * as ManagedRelay from "../relay/managedRelay.ts";
8import {
9 executeAuthenticatedEnvironmentHttpRequest,
10 withOrchestrationProtocolHeader,
11} from "./environmentHttpAuth.ts";
12
13const DEFAULT_THREAD_HISTORY_TIMEOUT_MS = 6_000;
14
15export const fetchEnvironmentThreadHistoryPage = Effect.fn(
16 "clientRuntime.state.fetchEnvironmentThreadHistoryPage",
17)(function* (input: {
18 readonly prepared: PreparedConnection;
19 readonly threadId: ThreadId;
20 readonly cursor: string;
21 readonly throughEntryId?: string | undefined;
22 readonly view?: "conversation" | "activity" | undefined;
23 readonly signer: Option.Option<ManagedRelay.ManagedRelayDpopSigner["Service"]>;
24 readonly remoteAuthorization?: Option.Option<RemoteEnvironmentAuthorization["Service"]>;
25 readonly timeoutMs?: number;
26}) {
27 const endpoint = {
28 params: { threadId: input.threadId },
29 query: {
30 cursor: input.cursor,
31 ...(input.view === undefined ? {} : { view: input.view }),
32 ...(input.throughEntryId === undefined ? {} : { throughEntryId: input.throughEntryId }),
33 },
34 };
35 return yield* executeAuthenticatedEnvironmentHttpRequest({
36 ...input,
37 group: "orchestration",
38 method: "GET",
39 url: (urls) => urls.threadHistoryPage(endpoint),
40 timeoutMs: input.timeoutMs ?? DEFAULT_THREAD_HISTORY_TIMEOUT_MS,
41 request: ({ client, headers }) =>
42 client.threadHistoryPage({ ...endpoint, headers: withOrchestrationProtocolHeader(headers) }),
43 });
44});