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

threadCommands.ts

149 lines5.1 KB

1import * as Crypto from "effect/Crypto";
2import { Atom } from "effect/unstable/reactivity";
3​
4import { createAtomCommandScheduler, createEnvironmentCommand } from "./runtime.ts";
5import {
6 type ArchiveThreadInput,
7 type CreateThreadInput,
8 type DeleteThreadInput,
9 type InterruptThreadTurnInput,
10 type RespondToThreadApprovalInput,
11 type RespondToThreadUserInputInput,
12 type RevertThreadCheckpointInput,
13 type SetThreadInteractionModeInput,
14 type SetThreadRuntimeModeInput,
15 type StartThreadTurnInput,
16 type StopThreadSessionInput,
17 type StopThreadTaskInput,
18 type UnarchiveThreadInput,
19 type UpdateThreadMetadataInput,
20 archiveThread,
21 createThread,
22 deleteThread,
23 interruptThreadTurn,
24 stopThreadTask,
25 respondToThreadApproval,
26 respondToThreadUserInput,
27 revertThreadCheckpoint,
28 setThreadInteractionMode,
29 setThreadRuntimeMode,
30 startThreadTurn,
31 stopThreadSession,
32 unarchiveThread,
33 updateThreadMetadata,
34} from "../operations/commands.ts";
35import type { EnvironmentRegistry } from "../connection/registry.ts";
36​
37export type {
38 ArchiveThreadInput,
39 CreateThreadInput,
40 DeleteThreadInput,
41 InterruptThreadTurnInput,
42 RespondToThreadApprovalInput,
43 RespondToThreadUserInputInput,
44 RevertThreadCheckpointInput,
45 SetThreadInteractionModeInput,
46 SetThreadRuntimeModeInput,
47 StartThreadTurnInput,
48 StopThreadSessionInput,
49 StopThreadTaskInput,
50 UnarchiveThreadInput,
51 UpdateThreadMetadataInput,
52} from "../operations/commands.ts";
53​
54export function createThreadEnvironmentAtoms<R, E>(
55 runtime: Atom.AtomRuntime<EnvironmentRegistry | Crypto.Crypto | R, E>,
56) {
57 const scheduler = createAtomCommandScheduler();
58 const concurrency = {
59 mode: "serial" as const,
60 key: ({ environmentId, input }: { environmentId: string; input: { threadId: string } }) =>
61 JSON.stringify([environmentId, input.threadId]),
62 };
63 return {
64 create: createEnvironmentCommand(runtime, {
65 label: "environment-data:commands:thread:create",
66 execute: (input: CreateThreadInput) => createThread(input),
67 scheduler,
68 concurrency,
69 }),
70 delete: createEnvironmentCommand(runtime, {
71 label: "environment-data:commands:thread:delete",
72 execute: (input: DeleteThreadInput) => deleteThread(input),
73 scheduler,
74 concurrency,
75 }),
76 archive: createEnvironmentCommand(runtime, {
77 label: "environment-data:commands:thread:archive",
78 execute: (input: ArchiveThreadInput) => archiveThread(input),
79 scheduler,
80 concurrency,
81 }),
82 unarchive: createEnvironmentCommand(runtime, {
83 label: "environment-data:commands:thread:unarchive",
84 execute: (input: UnarchiveThreadInput) => unarchiveThread(input),
85 scheduler,
86 concurrency,
87 }),
88 updateMetadata: createEnvironmentCommand(runtime, {
89 label: "environment-data:commands:thread:update-metadata",
90 execute: (input: UpdateThreadMetadataInput) => updateThreadMetadata(input),
91 scheduler,
92 concurrency,
93 }),
94 setRuntimeMode: createEnvironmentCommand(runtime, {
95 label: "environment-data:commands:thread:set-runtime-mode",
96 execute: (input: SetThreadRuntimeModeInput) => setThreadRuntimeMode(input),
97 scheduler,
98 concurrency,
99 }),
100 setInteractionMode: createEnvironmentCommand(runtime, {
101 label: "environment-data:commands:thread:set-interaction-mode",
102 execute: (input: SetThreadInteractionModeInput) => setThreadInteractionMode(input),
103 scheduler,
104 concurrency,
105 }),
106 startTurn: createEnvironmentCommand(runtime, {
107 label: "environment-data:commands:thread:start-turn",
108 execute: (input: StartThreadTurnInput) => startThreadTurn(input),
109 scheduler,
110 concurrency,
111 }),
112 interruptTurn: createEnvironmentCommand(runtime, {
113 label: "environment-data:commands:thread:interrupt-turn",
114 execute: (input: InterruptThreadTurnInput) => interruptThreadTurn(input),
115 scheduler,
116 concurrency,
117 }),
118 respondToApproval: createEnvironmentCommand(runtime, {
119 label: "environment-data:commands:thread:respond-to-approval",
120 execute: (input: RespondToThreadApprovalInput) => respondToThreadApproval(input),
121 scheduler,
122 concurrency,
123 }),
124 respondToUserInput: createEnvironmentCommand(runtime, {
125 label: "environment-data:commands:thread:respond-to-user-input",
126 execute: (input: RespondToThreadUserInputInput) => respondToThreadUserInput(input),
127 scheduler,
128 concurrency,
129 }),
130 revertCheckpoint: createEnvironmentCommand(runtime, {
131 label: "environment-data:commands:thread:revert-checkpoint",
132 execute: (input: RevertThreadCheckpointInput) => revertThreadCheckpoint(input),
133 scheduler,
134 concurrency,
135 }),
136 stopSession: createEnvironmentCommand(runtime, {
137 label: "environment-data:commands:thread:stop-session",
138 execute: (input: StopThreadSessionInput) => stopThreadSession(input),
139 scheduler,
140 concurrency,
141 }),
142 stopTask: createEnvironmentCommand(runtime, {
143 label: "environment-data:commands:thread:stop-task",
144 execute: (input: StopThreadTaskInput) => stopThreadTask(input),
145 scheduler,
146 concurrency,
147 }),
148 };