@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 89 lines 2.9 KB
Raw
1 import * as aiplatform from "@distilled.cloud/gcp/aiplatform_v1";
2 import { Credentials } from "@distilled.cloud/gcp/Credentials";
3 import * as Context from "effect/Context";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import { GcpProjectMissing } from "../Environment.ts";
7 import * as GCPBindings from "../GCPBindings.ts";
8 import {
9 GenerateContent,
10 type GenerateContentClient,
11 type GenerateContentRequest,
12 type PublisherModel,
13 } from "./GenerateContent.ts";
14
15 const TAG = "GCP.AIPlatform.GenerateContent";
16
17 /**
18 * HTTP implementation of {@link GenerateContent}.
19 *
20 * @layer
21 * @provides GCP.AIPlatform.GenerateContent
22 */
23 export const GenerateContentHttp = Layer.effect(
24 GenerateContent,
25 Effect.gen(function* () {
26 const bind = yield* GCPBindings.Bind;
27 const generateContent = yield* GCPBindings.withCredentials(
28 aiplatform.generateContentProjectsLocationsPublishersModels,
29 );
30 // The project is the caller's own: deploy credentials at plan time,
31 // the metadata server inside Cloud Run / Cloud Functions.
32 const credentials = Context.get(
33 yield* GCPBindings.withCredentials(Effect.context<Credentials>()),
34 Credentials,
35 );
36 const project = credentials.pipe(
37 Effect.flatMap(({ project }) =>
38 project
39 ? Effect.succeed(project)
40 : Effect.fail(
41 new GcpProjectMissing({
42 message: `${TAG}: no GCP project id in the runtime credentials`,
43 }),
44 ),
45 ),
46 );
47
48 return Effect.fn(function* (target: string | PublisherModel) {
49 const {
50 model,
51 location = "global",
52 publisher = "google",
53 } = typeof target === "string" ? { model: target } : target;
54 const id = `${publisher}/${model}@${location}`;
55
56 if (!globalThis.__ALCHEMY_RUNTIME__) {
57 yield* bind`${TAG}(${{ LogicalId: id }})`({
58 // Publisher models have no resource IAM policy; aiplatform.user is
59 // the narrowest predefined role carrying aiplatform.endpoints.predict.
60 iam: [{ role: "roles/aiplatform.user" }],
61 });
62 }
63
64 const generate = Effect.fn(`${TAG}(${id})`)(function* (request: GenerateContentRequest) {
65 return yield* generateContent({
66 model: `projects/${yield* project}/locations/${location}/publishers/${publisher}/models/${model}`,
67 body: request,
68 });
69 });
70
71 const client: GenerateContentClient = {
72 generate,
73 text: (prompt, options) =>
74 generate({
75 ...options,
76 contents: [{ role: "user", parts: [{ text: prompt }] }],
77 }).pipe(
78 Effect.map((response) =>
79 (response.candidates?.[0]?.content?.parts ?? [])
80 .filter((part) => !part.thought)
81 .map((part) => part.text ?? "")
82 .join(""),
83 ),
84 ),
85 };
86 return client;
87 });
88 }),
89 );