@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, 06:00 UTC

main
ts 366 lines 12.4 KB
Raw
1 import { expect, test as bunTest } from "bun:test";
2 import type { QueryAIGatewayClient, LanguageModelOptions } from "@alchemy.run/neon";
3 import { FunctionRequest } from "@alchemy.run/neon";
4 import * as NodeServices from "@effect/platform-node/NodeServices";
5 import { RuntimeContext } from "alchemy/RuntimeContext";
6 import { AiError, LanguageModel, Response } from "effect/ai";
7 import * as Deferred from "effect/Deferred";
8 import * as Effect from "effect/Effect";
9 import * as Sse from "effect/encoding/Sse";
10 import * as Fiber from "effect/Fiber";
11 import * as FileSystem from "effect/FileSystem";
12 import * as HttpServerRequest from "effect/http/HttpServerRequest";
13 import type * as HttpServerResponse from "effect/http/HttpServerResponse";
14 import * as Layer from "effect/Layer";
15 import * as Path from "effect/Path";
16 import * as Schema from "effect/Schema";
17 import * as Stream from "effect/Stream";
18 import { chat } from "../src/ai/EffectApi.ts";
19
20 const test = {
21 effect: (
22 name: string,
23 body: () => Effect.Effect<unknown, unknown>,
24 options?: { timeout: number },
25 ) => bunTest(name, () => Effect.runPromise(body()), options?.timeout),
26 };
27
28 const environment = {
29 NEON_EXAMPLE_API_KEY: "example-secret",
30 NEON_AI_MODEL: "mock-model",
31 NEON_AI_ALLOW_PAID: "false",
32 };
33 const usage = new Response.Usage({
34 inputTokens: {
35 total: 1,
36 uncached: 1,
37 cacheRead: undefined,
38 cacheWrite: undefined,
39 },
40 outputTokens: { total: 1, text: 1, reasoning: undefined },
41 });
42 const textParts: Array<Response.StreamPartEncoded> = [
43 { type: "text-start", id: "text" },
44 { type: "text-delta", id: "text", delta: "Hello 🌍" },
45 { type: "text-end", id: "text" },
46 { type: "finish", reason: "stop", usage },
47 ];
48 const mockModel = (
49 stream: Stream.Stream<Response.StreamPartEncoded, AiError.AiError> = Stream.fromIterable(
50 textParts,
51 ),
52 ) => {
53 const calls: Array<LanguageModelOptions> = [];
54 const prompts: Array<LanguageModel.ProviderOptions["prompt"]> = [];
55 const runtimeIds: Array<string> = [];
56 const ai: Pick<QueryAIGatewayClient, "model"> = {
57 model: (options) => {
58 calls.push(options);
59 return Layer.effect(
60 LanguageModel.LanguageModel,
61 Effect.gen(function* () {
62 const runtime = yield* RuntimeContext;
63 runtimeIds.push(runtime.id);
64 return yield* LanguageModel.make({
65 generateText: () => Effect.die("Example should use LanguageModel.streamText"),
66 streamText: (options) => {
67 prompts.push(options.prompt);
68 return stream;
69 },
70 });
71 }),
72 );
73 },
74 };
75 return { ai, calls, prompts, runtimeIds };
76 };
77 const invoke = (
78 ai: Pick<QueryAIGatewayClient, "model">,
79 env: Readonly<Record<string, string | undefined>> = environment,
80 body = '{"prompt":"Hello"}',
81 authorization: string | null | undefined = "Bearer example-secret",
82 signal?: AbortSignal,
83 ) =>
84 Effect.gen(function* () {
85 const request = yield* Effect.sync(
86 () =>
87 new Request("https://example.invalid/chat", {
88 method: "POST",
89 body,
90 signal,
91 headers: {
92 "content-type": "application/json",
93 ...(authorization == null ? {} : { authorization }),
94 },
95 }),
96 );
97 return yield* chat(ai, env).pipe(
98 Effect.provideService(
99 HttpServerRequest.HttpServerRequest,
100 HttpServerRequest.fromWeb(request),
101 ),
102 Effect.provideService(FunctionRequest, request),
103 Effect.provideService(RuntimeContext, {
104 Type: "test",
105 id: "captured-request",
106 env: {},
107 get: () => Effect.succeed(undefined),
108 set: (id) => Effect.succeed(id),
109 }),
110 );
111 });
112 const bodyStream = (response: HttpServerResponse.HttpServerResponse) => {
113 if (response.body._tag !== "Stream") return Stream.die("Expected SSE response body");
114 return response.body.stream;
115 };
116 const text = (response: HttpServerResponse.HttpServerResponse) =>
117 bodyStream(response).pipe(
118 Stream.decodeText(),
119 Stream.runCollect,
120 Effect.map((chunks) => chunks.join("")),
121 );
122 const UiEvent = Schema.Struct({
123 type: Schema.String,
124 id: Schema.optional(Schema.String),
125 delta: Schema.optional(Schema.String),
126 errorText: Schema.optional(Schema.String),
127 finishReason: Schema.optional(Schema.String),
128 });
129 const events = (wire: string) =>
130 Stream.succeed(wire).pipe(
131 Stream.pipeThroughChannel(Sse.decode<never, unknown>()),
132 Stream.map((event) => event.data),
133 Stream.filter((data) => data !== "[DONE]"),
134 Stream.mapEffect((data) => Schema.decodeUnknownEffect(Schema.fromJsonString(UiEvent))(data)),
135 Stream.runCollect,
136 );
137
138 for (const [name, authorization] of [
139 ["missing", undefined],
140 ["wrong", "Bearer wrong"],
141 ] as const) {
142 test.effect(`Effect example returns 401 for ${name} authorization before validation`, () =>
143 Effect.gen(function* () {
144 const mock = mockModel();
145 const response = yield* invoke(mock.ai, environment, "not JSON", authorization ?? null);
146 expect(response.status).toBe(401);
147 expect(mock.calls).toHaveLength(0);
148 }).pipe(Effect.scoped),
149 );
150 }
151
152 for (const [name, body] of [
153 ["malformed JSON", "{"],
154 ["missing prompt", "{}"],
155 ["null", "null"],
156 ["non-string prompt", '{"prompt":4}'],
157 ["blank prompt", '{"prompt":" "}'],
158 ["oversized prompt", JSON.stringify({ prompt: "x".repeat(4001) })],
159 ] as const) {
160 test.effect(`Effect example returns 400 for ${name} before the paid gate`, () =>
161 Effect.gen(function* () {
162 const mock = mockModel();
163 const response = yield* invoke(mock.ai, environment, body);
164 expect(response.status).toBe(400);
165 expect(mock.calls).toHaveLength(0);
166 }).pipe(Effect.scoped),
167 );
168 }
169
170 for (const [name, env] of [
171 ["disabled inference", environment],
172 ["missing model", { ...environment, NEON_AI_ALLOW_PAID: "true", NEON_AI_MODEL: undefined }],
173 ["non-explicit opt-in", { ...environment, NEON_AI_ALLOW_PAID: "1" }],
174 ] as const) {
175 test.effect(`Effect example returns 503 for ${name} without constructing a model`, () =>
176 Effect.gen(function* () {
177 const mock = mockModel();
178 const response = yield* invoke(mock.ai, env);
179 expect(response.status).toBe(503);
180 expect(mock.calls).toHaveLength(0);
181 }).pipe(Effect.scoped),
182 );
183 }
184
185 test.effect(
186 "Effect example calls the bound LanguageModel and preserves context in its deferred SSE body",
187 () =>
188 Effect.gen(function* () {
189 const mock = mockModel();
190 const response = yield* invoke(mock.ai, {
191 ...environment,
192 NEON_AI_ALLOW_PAID: "true",
193 });
194 expect(response.status).toBe(200);
195 expect(response.headers["content-type"]).toBe("text/event-stream");
196 expect(response.headers["x-vercel-ai-ui-message-stream"]).toBe("v1");
197 expect(mock.calls).toEqual([{ model: "mock-model", parameters: { maxTokens: 128 } }]);
198 expect(mock.prompts).toHaveLength(0);
199 // Consume after invoke's request-service provision has ended.
200 const wire = yield* text(response);
201 expect(mock.runtimeIds).toEqual(["captured-request"]);
202 expect(mock.prompts).toHaveLength(1);
203 expect(mock.prompts[0]?.content).toMatchObject([
204 { role: "user", content: [{ type: "text", text: "Hello" }] },
205 ]);
206 const parsed = yield* events(wire);
207 expect(parsed.map((event) => event.type)).toEqual([
208 "start",
209 "start-step",
210 "text-start",
211 "text-delta",
212 "text-end",
213 "finish-step",
214 "finish",
215 ]);
216 expect(parsed.find((event) => event.type === "text-delta")?.delta).toBe("Hello 🌍");
217 expect(wire).toContain("data: [DONE]");
218 }).pipe(Effect.scoped),
219 );
220
221 test.effect(
222 "Effect example sanitizes AiError SSE failures without retrying or leaking credentials",
223 () =>
224 Effect.gen(function* () {
225 const mock = mockModel(
226 Stream.concat(
227 Stream.fromIterable(textParts.slice(0, 2)),
228 Stream.fail(
229 AiError.make({
230 module: "test",
231 method: "streamText",
232 reason: new AiError.InvalidRequestError({
233 description: "UPSTREAM_TOKEN_SENTINEL",
234 }),
235 }),
236 ),
237 ),
238 );
239 const response = yield* invoke(mock.ai, {
240 ...environment,
241 NEON_AI_ALLOW_PAID: "true",
242 });
243 const wire = yield* text(response);
244 const parsed = yield* events(wire);
245 expect(parsed.filter((event) => event.type === "error")).toHaveLength(1);
246 expect(parsed.find((event) => event.type === "error")?.errorText).toContain(
247 "Gateway rejected the request",
248 );
249 expect(parsed.some((event) => event.type === "finish")).toBe(false);
250 expect(wire).not.toContain("UPSTREAM_TOKEN_SENTINEL");
251 expect(wire).not.toContain("example-secret");
252 expect(wire).toContain("data: [DONE]");
253 expect(mock.prompts).toHaveLength(1);
254 }).pipe(Effect.scoped),
255 );
256
257 test.effect(
258 "Effect example request abort interrupts upstream model consumption",
259 () =>
260 Effect.gen(function* () {
261 const controller = yield* Effect.sync(() => new AbortController());
262 const started = yield* Deferred.make<void>();
263 let finalized = false;
264 const mock = mockModel(
265 Stream.fromEffect(Deferred.succeed(started, undefined)).pipe(
266 Stream.drain,
267 Stream.concat(Stream.never),
268 Stream.ensuring(
269 Effect.sync(() => {
270 finalized = true;
271 }),
272 ),
273 ),
274 );
275 const response = yield* invoke(
276 mock.ai,
277 { ...environment, NEON_AI_ALLOW_PAID: "true" },
278 undefined,
279 undefined,
280 controller.signal,
281 );
282 const consumer = yield* bodyStream(response).pipe(Stream.runDrain, Effect.forkChild);
283 yield* Deferred.await(started);
284 yield* Effect.sync(() => controller.abort());
285 yield* Fiber.join(consumer);
286 expect(finalized).toBe(true);
287 }).pipe(Effect.scoped),
288 { timeout: 5000 },
289 );
290
291 test.effect(
292 "Effect example does not start model inference for an already-aborted request",
293 () =>
294 Effect.gen(function* () {
295 const controller = yield* Effect.sync(() => new AbortController());
296 yield* Effect.sync(() => controller.abort());
297 const mock = mockModel();
298 const response = yield* invoke(
299 mock.ai,
300 { ...environment, NEON_AI_ALLOW_PAID: "true" },
301 undefined,
302 undefined,
303 controller.signal,
304 );
305 yield* bodyStream(response).pipe(Stream.runDrain);
306 expect(mock.prompts).toHaveLength(0);
307 }).pipe(Effect.scoped),
308 { timeout: 5000 },
309 );
310
311 test.effect(
312 "Effect example downstream cancellation closes the model stream",
313 () =>
314 Effect.gen(function* () {
315 let finalized = false;
316 const mock = mockModel(
317 Stream.fromIterable(textParts.slice(0, 2)).pipe(
318 Stream.concat(Stream.never),
319 Stream.ensuring(
320 Effect.sync(() => {
321 finalized = true;
322 }),
323 ),
324 ),
325 );
326 const response = yield* invoke(mock.ai, {
327 ...environment,
328 NEON_AI_ALLOW_PAID: "true",
329 });
330 yield* bodyStream(response).pipe(
331 Stream.decodeText(),
332 Stream.takeUntil((chunk) => chunk.includes("text-delta")),
333 Stream.runDrain,
334 );
335 expect(finalized).toBe(true);
336 }).pipe(Effect.scoped),
337 { timeout: 5000 },
338 );
339
340 test.effect(
341 "example source uses the canonical Effect model and gateway reference documentation is discoverable",
342 () =>
343 Effect.gen(function* () {
344 const fs = yield* FileSystem.FileSystem;
345 const path = yield* Path.Path;
346 const examplePath = yield* path.fromFileUrl(
347 new URL("../src/ai/EffectApi.ts", import.meta.url),
348 );
349 const gatewayPath = yield* path.fromFileUrl(
350 new URL("../../../packages/neon/src/AIGateway.ts", import.meta.url),
351 );
352 const source = yield* fs.readFileString(examplePath);
353 expect(source).toContain("const model = ai.model(");
354 expect(source).toContain("LanguageModel.streamText(");
355 expect(source).toContain("Stream.provideContext(context)");
356 expect(source).not.toContain("@neon/ai-sdk-provider");
357 expect(source).not.toContain('from "ai"');
358 for (const name of ["NEON_EXAMPLE_API_KEY", "NEON_AI_MODEL", "NEON_AI_ALLOW_PAID"]) {
359 expect(source).toContain(`environment.${name}`);
360 expect(source).toContain(`("${name}")`);
361 }
362 expect(yield* fs.readFileString(gatewayPath)).toContain(
363 " * @resource\n * @category AI Gateway",
364 );
365 }).pipe(Effect.provide(NodeServices.layer)),
366 );