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

main
ts 138 lines 4.96 KB
Raw
1 import * as Neon from "@alchemy.run/neon";
2 import type { RuntimeContext } from "alchemy/RuntimeContext";
3 import * as LanguageModel from "effect/ai/LanguageModel";
4 import * as Config from "effect/Config";
5 import * as Effect from "effect/Effect";
6 import * as Sse from "effect/encoding/Sse";
7 import * as HttpServerRequest from "effect/http/HttpServerRequest";
8 import * as HttpServerResponse from "effect/http/HttpServerResponse";
9 import * as Schema from "effect/Schema";
10 import type * as Scope from "effect/Scope";
11 import * as Stream from "effect/Stream";
12 import { resources } from "../resources.ts";
13 import { gateway } from "./resources.ts";
14
15 /** Authenticated, opt-in text streaming backed by the bound Effect model. */
16 export const chat = (
17 ai: Pick<Neon.QueryAIGatewayClient, "model">,
18 environment: Readonly<Record<string, string | undefined>>,
19 ) =>
20 Effect.gen(function* () {
21 const request = yield* HttpServerRequest.HttpServerRequest;
22 if (
23 !environment.NEON_EXAMPLE_API_KEY ||
24 request.headers.authorization !== `Bearer ${environment.NEON_EXAMPLE_API_KEY}`
25 ) {
26 return HttpServerResponse.text("Unauthorized", { status: 401 });
27 }
28 const body = yield* request.json.pipe(
29 Effect.flatMap(Schema.decodeUnknownEffect(Schema.Struct({ prompt: Schema.String }))),
30 Effect.catch(() => Effect.succeed(undefined)),
31 );
32 if (!body)
33 return HttpServerResponse.text("A JSON prompt is required", {
34 status: 400,
35 });
36 if (body.prompt.trim().length === 0 || body.prompt.length > 4000) {
37 return HttpServerResponse.text("Expected a nonempty prompt of at most 4000 characters", {
38 status: 400,
39 });
40 }
41 if (environment.NEON_AI_ALLOW_PAID !== "true" || !environment.NEON_AI_MODEL) {
42 return HttpServerResponse.text(
43 "Explicit paid inference opt-in and a configured model are required",
44 { status: 503 },
45 );
46 }
47
48 const native = yield* Neon.FunctionRequest;
49 const model = ai.model({
50 model: environment.NEON_AI_MODEL,
51 parameters: { maxTokens: 128 },
52 });
53 // The response body is consumed after the handler returns, in the transferred request scope.
54 const context = yield* Effect.context<RuntimeContext | Scope.Scope>();
55 const events = LanguageModel.streamText({ prompt: body.prompt }).pipe(
56 Stream.flatMap((part): Stream.Stream<Record<string, unknown>> => {
57 switch (part.type) {
58 case "text-start":
59 case "text-end":
60 return Stream.succeed({ type: part.type, id: part.id });
61 case "text-delta":
62 return Stream.succeed({
63 type: "text-delta",
64 id: part.id,
65 delta: part.delta,
66 });
67 case "finish":
68 return Stream.make(
69 { type: "finish-step" },
70 { type: "finish", finishReason: part.reason },
71 );
72 default:
73 return Stream.empty;
74 }
75 }),
76 Stream.provide(model),
77 Stream.provideContext(context),
78 Stream.catchTag("AiError", () =>
79 Stream.succeed({
80 type: "error",
81 errorText:
82 "Gateway rejected the request. Check model access, paid-plan entitlement and prepaid credits. No credits were purchased.",
83 }),
84 ),
85 );
86 const stream = Stream.concat(
87 Stream.make({ type: "start" }, { type: "start-step" }),
88 events,
89 ).pipe(
90 Stream.map((event) => JSON.stringify(event)),
91 Stream.concat(Stream.succeed("[DONE]")),
92 Stream.map((data): Sse.Event => ({
93 _tag: "Event",
94 event: "message",
95 id: undefined,
96 data,
97 })),
98 Stream.pipeThroughChannel(Sse.encode<never, unknown>()),
99 Stream.encodeText,
100 Stream.interruptWhen(
101 Effect.callback<void>((resume) => {
102 const abort = () => resume(Effect.void);
103 if (native.signal.aborted) abort();
104 else native.signal.addEventListener("abort", abort, { once: true });
105 return Effect.sync(() => native.signal.removeEventListener("abort", abort));
106 }),
107 ),
108 );
109 return HttpServerResponse.stream(stream, {
110 headers: {
111 "content-type": "text/event-stream",
112 "cache-control": "no-cache",
113 "x-vercel-ai-ui-message-stream": "v1",
114 },
115 });
116 });
117
118 export default class EffectApi extends Neon.Function<EffectApi>()(
119 "EffectAI",
120 Effect.gen(function* () {
121 return {
122 branch: (yield* resources).branch,
123 main: import.meta.url,
124 env: {
125 NEON_EXAMPLE_API_KEY: yield* Config.Redacted("NEON_EXAMPLE_API_KEY"),
126 NEON_AI_MODEL: yield* Config.String("NEON_AI_MODEL"),
127 NEON_AI_ALLOW_PAID: yield* Config.String("NEON_AI_ALLOW_PAID").pipe(
128 Config.withDefault("false"),
129 ),
130 },
131 };
132 }),
133 Effect.gen(function* () {
134 const ai = yield* Neon.QueryAIGateway(gateway);
135 const environment = yield* Neon.FunctionEnvironment;
136 return { fetch: chat(ai, environment) };
137 }).pipe(Effect.provide(Neon.QueryAIGatewayHttp)),
138 ) {}