@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 244 lines 6.73 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as Layer from "effect/Layer";
3
4 import * as NodeServices from "@effect/platform-node/NodeServices";
5 import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
6
7 import * as AcpAgent from "../../src/agent.ts";
8
9 if (process.env.ACP_MOCK_STDOUT_PREFIX !== undefined) {
10 process.stdout.write(process.env.ACP_MOCK_STDOUT_PREFIX);
11 }
12
13 if (process.env.ACP_MOCK_MALFORMED_OUTPUT === "1") {
14 process.stdout.write("{not-json}\n");
15 process.exit(Number(process.env.ACP_MOCK_MALFORMED_OUTPUT_EXIT_CODE ?? "0"));
16 }
17
18 if (process.env.ACP_MOCK_EXIT_IMMEDIATELY_CODE !== undefined) {
19 process.exit(Number(process.env.ACP_MOCK_EXIT_IMMEDIATELY_CODE));
20 }
21
22 const sessionId = "mock-session-1";
23 const v2Management = process.env.ACP_MOCK_V2_MANAGEMENT === "1";
24 const unknownVariants = process.env.ACP_MOCK_UNKNOWN_VARIANTS === "1";
25 const mcpOverAcp = process.env.ACP_MOCK_MCP_OVER_ACP === "1";
26 const v2Diff = process.env.ACP_MOCK_V2_DIFF === "1";
27 const badTypedRequest = process.env.ACP_MOCK_BAD_TYPED_REQUEST === "1";
28
29 const program = Effect.gen(function* () {
30 const agent = yield* AcpAgent.AcpAgent;
31
32 yield* agent.handleInitialize(() =>
33 Effect.succeed({
34 protocolVersion: 2,
35 capabilities: {
36 session: {
37 ...(v2Management ? { delete: {} } : {}),
38 ...(mcpOverAcp ? { mcp: { acp: {} } } : {}),
39 },
40 ...(v2Management ? { providers: {} } : {}),
41 },
42 info: {
43 name: "mock-agent",
44 version: "0.0.0",
45 },
46 ...(process.env.ACP_MOCK_CUSTOM_AUTH === "1"
47 ? { authMethods: [{ type: "custom_future", methodId: "custom", name: "Future auth" }] }
48 : {}),
49 ...(process.env.ACP_MOCK_ENV_VAR_AUTH === "1"
50 ? {
51 authMethods: [
52 {
53 type: "env_var",
54 methodId: "api_key",
55 name: "API key",
56 vars: [{ name: "MOCK_API_KEY", label: "Mock API key" }],
57 link: "https://example.test/keys",
58 },
59 ],
60 }
61 : {}),
62 }),
63 );
64
65 yield* agent.handleAuthenticate(() => Effect.succeed({}));
66 yield* agent.handleLogout(() => Effect.succeed({}));
67 yield* agent.handleCreateSession(() =>
68 Effect.succeed({
69 sessionId,
70 }),
71 );
72 yield* agent.handleLoadSession(() => Effect.succeed({}));
73 yield* agent.handleListSessions(() =>
74 Effect.succeed({
75 sessions: [
76 {
77 sessionId,
78 cwd: process.cwd(),
79 },
80 ],
81 }),
82 );
83 yield* agent.handleDeleteSession(() => Effect.succeed({}));
84 yield* agent.handleListProviders(() =>
85 Effect.succeed({
86 providers: [
87 {
88 providerId: "mock-provider",
89 supported: ["openai"],
90 required: false,
91 current: null,
92 },
93 ],
94 }),
95 );
96 yield* agent.handleSetProvider(() => Effect.succeed({}));
97 yield* agent.handleDisableProvider(() => Effect.succeed({}));
98
99 yield* agent.handlePrompt(() =>
100 Effect.gen(function* () {
101 if (mcpOverAcp) {
102 const connected = yield* agent.client.connectMcp({ serverId: "t3-code" });
103 yield* agent.client.messageMcp({
104 connectionId: connected.connectionId,
105 method: "tools/list",
106 });
107 yield* agent.client.notifyMcp({
108 connectionId: connected.connectionId,
109 method: "notifications/initialized",
110 });
111 yield* agent.client.disconnectMcp({ connectionId: connected.connectionId });
112 }
113
114 if (unknownVariants) {
115 yield* agent.client.sessionUpdate({
116 sessionId,
117 update: {
118 sessionUpdate: "agent_message_chunk",
119 messageId: "future-content",
120 content: { type: "chart", points: [] },
121 },
122 });
123 yield* agent.client.sessionUpdate({
124 sessionId,
125 update: { sessionUpdate: "timeline_update", entries: [] },
126 });
127 }
128
129 yield* agent.client.requestPermission({
130 sessionId,
131 title: "Read project files",
132 options: [
133 {
134 optionId: "allow",
135 name: "Allow",
136 kind: "allow_once",
137 },
138 ],
139 subject: {
140 type: "tool_call",
141 toolCall: {
142 toolCallId: "tool-1",
143 title: "Read project files",
144 },
145 },
146 });
147
148 yield* agent.client.elicit({
149 sessionId,
150 message: "Need confirmation before continuing.",
151 mode: "form",
152 requestedSchema: {
153 type: "object",
154 title: "Need confirmation",
155 properties: {
156 approved: {
157 type: "boolean",
158 title: "Approved",
159 },
160 },
161 required: ["approved"],
162 },
163 });
164
165 yield* agent.client.sessionUpdate({
166 sessionId,
167 update: {
168 sessionUpdate: "plan_update",
169 plan: {
170 type: "items",
171 planId: "mock-plan",
172 entries: [
173 {
174 content: "Inspect the repository",
175 priority: "high",
176 status: "in_progress",
177 },
178 ],
179 },
180 },
181 });
182
183 if (v2Diff) {
184 yield* agent.client.sessionUpdate({
185 sessionId,
186 update: {
187 sessionUpdate: "tool_call_update",
188 toolCallId: "tool-diff",
189 content: [
190 {
191 type: "diff",
192 changes: [
193 {
194 operation: "move",
195 oldPath: "/workspace/old.ts",
196 path: "/workspace/new.ts",
197 fileType: "text",
198 mimeType: "text/typescript",
199 },
200 ],
201 patch: {
202 format: "git_patch",
203 text: "diff --git a/old.ts b/new.ts\nsimilarity index 100%\nrename from old.ts\nrename to new.ts\n",
204 },
205 },
206 ],
207 },
208 });
209 }
210
211 yield* agent.client.elicitationComplete({
212 elicitationId: "elicitation-1",
213 });
214
215 yield* agent.client.extRequest("x/typed_request", {
216 message: badTypedRequest ? 123 : "hello from typed request",
217 });
218
219 yield* agent.client.extNotification("x/typed_notification", {
220 count: 2,
221 });
222
223 yield* agent.client.sessionUpdate({
224 sessionId,
225 update: { sessionUpdate: "state_update", state: "idle", stopReason: "end_turn" },
226 });
227 return {};
228 }),
229 );
230
231 yield* agent.handleUnknownExtRequest((method, params) =>
232 Effect.succeed({
233 echoedMethod: method,
234 echoedParams: params ?? null,
235 }),
236 );
237
238 return yield* Effect.never;
239 });
240
241 program.pipe(
242 Effect.provide(Layer.provide(AcpAgent.layerStdio(), NodeServices.layer)),
243 NodeRuntime.runMain,
244 );