@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 100 lines 3.76 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import * as Effect from "effect/Effect";
3 import { HttpServerRequest } from "effect/http/HttpServerRequest";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5
6 // Gemini 2.5 Flash on the `global` endpoint — cheap, fast, and routed to
7 // any region with capacity. Any Gemini model id works here; pass
8 // `{ model, location: "us-central1" }` to pin a region instead.
9 const MODEL = "gemini-2.5-flash";
10
11 interface ChatRequest {
12 prompt?: string;
13 system?: string;
14 temperature?: number;
15 maxOutputTokens?: number;
16 }
17
18 /**
19 * A public chat endpoint on Cloud Run that answers with Gemini on
20 * Vertex AI.
21 *
22 * - `GET /` — health check.
23 * - `POST /chat` — `{ prompt, system?, temperature?, maxOutputTokens? }`
24 * in, `{ text, finishReason, modelVersion, usage }` out.
25 *
26 * There is no API key anywhere: the container calls Vertex AI as its own
27 * runtime service account, which the binding grants
28 * `roles/aiplatform.user` at deploy time.
29 *
30 * `invokerIamDisabled: true` makes the service publicly reachable. Drop it
31 * and Cloud Run requires a signed Google identity token on every request —
32 * worth doing before this endpoint spends real money on strangers' prompts.
33 */
34 export default class Chat extends GCP.Function<Chat>()(
35 "Chat",
36 {
37 main: import.meta.url,
38 invokerIamDisabled: true,
39 },
40 Effect.gen(function* () {
41 // Init: bind the model. Grants the runtime service account
42 // roles/aiplatform.user and returns a typed Gemini client.
43 const gemini = yield* GCP.AIPlatform.GenerateContent(MODEL);
44
45 return {
46 fetch: Effect.gen(function* () {
47 const request = yield* HttpServerRequest;
48 const url = new URL(request.originalUrl);
49
50 if (request.method === "GET" && url.pathname === "/") {
51 return HttpServerResponse.text("ok");
52 }
53
54 if (request.method !== "POST" || url.pathname !== "/chat") {
55 return yield* HttpServerResponse.json({ error: "not found" }, { status: 404 });
56 }
57
58 const body = (yield* request.json) as ChatRequest;
59 if (!body.prompt) {
60 return yield* HttpServerResponse.json({ error: "prompt is required" }, { status: 400 });
61 }
62
63 // Model access is bound at deploy time; generation settings are a
64 // per-request decision.
65 const response = yield* gemini.generate({
66 contents: [{ role: "user", parts: [{ text: body.prompt }] }],
67 ...(body.system ? { systemInstruction: { parts: [{ text: body.system }] } } : {}),
68 generationConfig: {
69 ...(body.temperature !== undefined ? { temperature: body.temperature } : {}),
70 ...(body.maxOutputTokens !== undefined
71 ? { maxOutputTokens: body.maxOutputTokens }
72 : {}),
73 },
74 });
75
76 const candidate = response.candidates?.[0];
77 return yield* HttpServerResponse.json({
78 // Gemini 2.5 may return "thought" parts; answer with the rest.
79 text: (candidate?.content?.parts ?? [])
80 .filter((part) => !part.thought)
81 .map((part) => part.text ?? "")
82 .join(""),
83 finishReason: candidate?.finishReason,
84 modelVersion: response.modelVersion,
85 usage: {
86 inputTokens: response.usageMetadata?.promptTokenCount,
87 outputTokens: response.usageMetadata?.candidatesTokenCount,
88 },
89 });
90 }).pipe(
91 // A blocked prompt or bad generation config is the caller's
92 // problem; surface Vertex AI's message instead of a bare 500.
93 Effect.catchTag("BadRequest", (error) =>
94 HttpServerResponse.json({ error: error.message }, { status: 400 }),
95 ),
96 Effect.orDie,
97 ),
98 };
99 }).pipe(Effect.provide(GCP.AIPlatform.GenerateContentHttp)),
100 ) {}