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

threadCommands.ts

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