Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadCommands.ts

104 lines3.4 KB

1import * as Crypto from "effect/Crypto";
2import { Atom } from "effect/unstable/reactivity";
3import { WS_METHODS } from "@t3tools/contracts";
4​
5import {
6 createAtomCommandScheduler,
7 createEnvironmentCommand,
8 createEnvironmentRpcCommand,
9} from "./runtime.ts";
10import {
11 archiveThread,
12 createThread,
13 deleteThread,
14 interruptThreadTurn,
15 respondToThreadApproval,
16 respondToThreadUserInput,
17 revertThreadCheckpoint,
18 setThreadInteractionMode,
19 setThreadRuntimeMode,
20 pinThread,
21 reorderPinnedThread,
22 settleThread,
23 snoozeThread,
24 startThreadTurn,
25 stopThreadSession,
26 unarchiveThread,
27 unpinThread,
28 unsettleThread,
29 unsnoozeThread,
30 updateThreadMetadata,
31} from "../operations/commands.ts";
32import type { EnvironmentRegistry } from "../connection/registry.ts";
33​
34export type {
35 ArchiveThreadInput,
36 CreateThreadInput,
37 DeleteThreadInput,
38 InterruptThreadTurnInput,
39 RespondToThreadApprovalInput,
40 RespondToThreadUserInputInput,
41 RevertThreadCheckpointInput,
42 SetThreadInteractionModeInput,
43 SetThreadRuntimeModeInput,
44 PinThreadInput,
45 ReorderPinnedThreadInput,
46 SettleThreadInput,
47 SnoozeThreadInput,
48 StartThreadTurnInput,
49 StopThreadSessionInput,
50 UnarchiveThreadInput,
51 UnpinThreadInput,
52 UnsettleThreadInput,
53 UnsnoozeThreadInput,
54 UpdateThreadMetadataInput,
55} from "../operations/commands.ts";
56​
57export function createThreadEnvironmentAtoms<R, E>(
58 runtime: Atom.AtomRuntime<EnvironmentRegistry | Crypto.Crypto | R, E>,
59) {
60 const scheduler = createAtomCommandScheduler();
61 const concurrency = {
62 mode: "serial" as const,
63 key: ({ environmentId, input }: { environmentId: string; input: { threadId: string } }) =>
64 JSON.stringify([environmentId, input.threadId]),
65 };
66 const command = <Input extends { readonly threadId: string }>(
67 label: string,
68 execute: (input: Input) => ReturnType<typeof createThread>,
69 ) =>
70 createEnvironmentCommand(runtime, {
71 label: `environment-data:commands:thread:${label}`,
72 execute,
73 scheduler,
74 concurrency,
75 });
76 return {
77 create: command("create", createThread),
78 delete: command("delete", deleteThread),
79 archive: command("archive", archiveThread),
80 unarchive: command("unarchive", unarchiveThread),
81 settle: command("settle", settleThread),
82 unsettle: command("unsettle", unsettleThread),
83 snooze: command("snooze", snoozeThread),
84 unsnooze: command("unsnooze", unsnoozeThread),
85 pin: command("pin", pinThread),
86 unpin: command("unpin", unpinThread),
87 reorderPin: command("reorder-pin", reorderPinnedThread),
88 updateMetadata: command("update-metadata", updateThreadMetadata),
89 setRuntimeMode: command("set-runtime-mode", setThreadRuntimeMode),
90 setInteractionMode: command("set-interaction-mode", setThreadInteractionMode),
91 startTurn: command("start-turn", startThreadTurn),
92 interruptTurn: command("interrupt-turn", interruptThreadTurn),
93 respondToApproval: command("respond-to-approval", respondToThreadApproval),
94 respondToUserInput: command("respond-to-user-input", respondToThreadUserInput),
95 revertCheckpoint: command("revert-checkpoint", revertThreadCheckpoint),
96 stopSession: command("stop-session", stopThreadSession),
97 uploadFeedback: createEnvironmentRpcCommand(runtime, {
98 label: "environment-data:commands:thread:upload-feedback",
99 tag: WS_METHODS.providerUploadFeedback,
100 scheduler,
101 concurrency,
102 }),
103 };