Open History to see the last commit
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 ForkThreadInput,
10 type InterruptThreadTurnInput,
11 type RespondToThreadApprovalInput,
12 type RespondToThreadUserInputInput,
13 type RevertThreadCheckpointInput,
14 type SetThreadInteractionModeInput,
15 type SetThreadRuntimeModeInput,
16 type StartThreadTurnInput,
17 type StopThreadSessionInput,
18 type UnarchiveThreadInput,
19 type UpdateThreadMetadataInput,
20 archiveThread,
21 createThread,
22 deleteThread,
23 forkThread,
24 interruptThreadTurn,
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 ForkThreadInput,
42 InterruptThreadTurnInput,
43 RespondToThreadApprovalInput,
44 RespondToThreadUserInputInput,
45 RevertThreadCheckpointInput,
46 SetThreadInteractionModeInput,
47 SetThreadRuntimeModeInput,
48 StartThreadTurnInput,
49 StopThreadSessionInput,
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 fork: createEnvironmentCommand(runtime, {
71 label: "environment-data:commands:thread:fork",
72 execute: (input: ForkThreadInput) => forkThread(input),
73 scheduler,
74 concurrency,
75 }),
76 delete: createEnvironmentCommand(runtime, {
77 label: "environment-data:commands:thread:delete",
78 execute: (input: DeleteThreadInput) => deleteThread(input),
79 scheduler,
80 concurrency,
81 }),
82 archive: createEnvironmentCommand(runtime, {
83 label: "environment-data:commands:thread:archive",
84 execute: (input: ArchiveThreadInput) => archiveThread(input),
85 scheduler,
86 concurrency,
87 }),
88 unarchive: createEnvironmentCommand(runtime, {
89 label: "environment-data:commands:thread:unarchive",
90 execute: (input: UnarchiveThreadInput) => unarchiveThread(input),
91 scheduler,
92 concurrency,
93 }),
94 updateMetadata: createEnvironmentCommand(runtime, {
95 label: "environment-data:commands:thread:update-metadata",
96 execute: (input: UpdateThreadMetadataInput) => updateThreadMetadata(input),
97 scheduler,
98 concurrency,
99 }),
100 setRuntimeMode: createEnvironmentCommand(runtime, {
101 label: "environment-data:commands:thread:set-runtime-mode",
102 execute: (input: SetThreadRuntimeModeInput) => setThreadRuntimeMode(input),
103 scheduler,
104 concurrency,
105 }),
106 setInteractionMode: createEnvironmentCommand(runtime, {
107 label: "environment-data:commands:thread:set-interaction-mode",
108 execute: (input: SetThreadInteractionModeInput) => setThreadInteractionMode(input),
109 scheduler,
110 concurrency,
111 }),
112 startTurn: createEnvironmentCommand(runtime, {
113 label: "environment-data:commands:thread:start-turn",
114 execute: (input: StartThreadTurnInput) => startThreadTurn(input),
115 scheduler,
116 concurrency,
117 }),
118 interruptTurn: createEnvironmentCommand(runtime, {
119 label: "environment-data:commands:thread:interrupt-turn",
120 execute: (input: InterruptThreadTurnInput) => interruptThreadTurn(input),
121 scheduler,
122 concurrency,
123 }),
124 respondToApproval: createEnvironmentCommand(runtime, {
125 label: "environment-data:commands:thread:respond-to-approval",
126 execute: (input: RespondToThreadApprovalInput) => respondToThreadApproval(input),
127 scheduler,
128 concurrency,
129 }),
130 respondToUserInput: createEnvironmentCommand(runtime, {
131 label: "environment-data:commands:thread:respond-to-user-input",
132 execute: (input: RespondToThreadUserInputInput) => respondToThreadUserInput(input),
133 scheduler,
134 concurrency,
135 }),
136 revertCheckpoint: createEnvironmentCommand(runtime, {
137 label: "environment-data:commands:thread:revert-checkpoint",
138 execute: (input: RevertThreadCheckpointInput) => revertThreadCheckpoint(input),
139 scheduler,
140 concurrency,
141 }),
142 stopSession: createEnvironmentCommand(runtime, {
143 label: "environment-data:commands:thread:stop-session",
144 execute: (input: StopThreadSessionInput) => stopThreadSession(input),
145 scheduler,
146 concurrency,
147 }),
148 };
149}