The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { serveProbes } from "../../bindingHost.ts"; |
| 4 | |
| 5 | // The gates are decided at deploy time and passed to the deployed host |
| 6 | // through its env, so the host serves exactly the probes it was bound for. |
| 7 | const gate = (name: string, atDeploy: boolean) => { |
| 8 | const passed = process.env[name]; |
| 9 | // Host env values arrive JSON-encoded (`"1"`). |
| 10 | return passed === undefined ? atDeploy : passed.replace(/"/g, "") === "1"; |
| 11 | }; |
| 12 | |
| 13 | /** |
| 14 | * Sandbox environments and templates take 1-3 minutes to provision. |
| 15 | */ |
| 16 | export const runSandboxes = gate("AIPLATFORM_SANDBOXES", !process.env.FAST); |
| 17 | |
| 18 | /** |
| 19 | * pauseReasoningEnginesSandboxEnvironments does not work on the testing |
| 20 | * project (the sandbox itself creates fine): it has answered |
| 21 | * InternalServerError "Internal error encountered.", and from the host the |
| 22 | * first calls drop the connection (Cloud Run 503) after which the sandbox |
| 23 | * answers FAILED_PRECONDITION "is not in RUNNING, PAUSING or STOPPING |
| 24 | * state, cannot be paused.". Set GCP_TEST_AIPLATFORM_SANDBOX_PAUSE=1 where |
| 25 | * pausing works. |
| 26 | */ |
| 27 | export const runSandboxPause = gate( |
| 28 | "AIPLATFORM_SANDBOX_PAUSE", |
| 29 | runSandboxes && !!process.env.GCP_TEST_AIPLATFORM_SANDBOX_PAUSE, |
| 30 | ); |
| 31 | |
| 32 | /** Reasoning engine without a deployed package (queries are refused). */ |
| 33 | export const Agent = GCP.AIPlatform.ReasoningEngine("Agent", { |
| 34 | location: "us-central1", |
| 35 | displayName: "alchemy-binding-engine", |
| 36 | spec: { agentFramework: "custom" }, |
| 37 | }); |
| 38 | |
| 39 | /** Custom training pipeline the host reads and cancels. */ |
| 40 | export const Train = GCP.AIPlatform.TrainingPipeline("Train", { |
| 41 | location: "us-central1", |
| 42 | displayName: "alchemy-binding-pipeline", |
| 43 | trainingTaskDefinition: |
| 44 | "gs://google-cloud-aiplatform/schema/trainingjob/definition/custom_task_1.0.0.yaml", |
| 45 | trainingTaskInputs: { |
| 46 | workerPoolSpecs: [ |
| 47 | { |
| 48 | machineSpec: { machineType: "n1-standard-4" }, |
| 49 | replicaCount: "1", |
| 50 | containerSpec: { |
| 51 | imageUri: "us-docker.pkg.dev/vertex-ai/training/tf-cpu.2-12.py310:latest", |
| 52 | // Runs long enough for the host to cancel it. |
| 53 | command: ["sleep", "1800"], |
| 54 | }, |
| 55 | }, |
| 56 | ], |
| 57 | }, |
| 58 | }); |
| 59 | |
| 60 | const sandbox = (id: string, displayName: string) => |
| 61 | Effect.gen(function* () { |
| 62 | const engine = yield* Agent; |
| 63 | return yield* GCP.AIPlatform.ReasoningEnginesSandboxEnvironment(id, { |
| 64 | reasoningEngine: engine.name, |
| 65 | displayName, |
| 66 | ttl: "600s", |
| 67 | spec: { |
| 68 | codeExecutionEnvironment: { |
| 69 | codeLanguage: "LANGUAGE_PYTHON", |
| 70 | machineConfig: "MACHINE_CONFIG_VCPU4_RAM4GIB", |
| 71 | }, |
| 72 | }, |
| 73 | }); |
| 74 | }); |
| 75 | |
| 76 | /** Sandbox the host reads. */ |
| 77 | export const Code = sandbox("Code", "code"); |
| 78 | /** Sandbox the host pauses. */ |
| 79 | export const Paused = sandbox("Paused", "paused"); |
| 80 | /** Sandbox the host pauses and resumes. */ |
| 81 | export const Resumed = sandbox("Resumed", "resumed"); |
| 82 | |
| 83 | /** Sandbox template the host reads. */ |
| 84 | export const Browser = Effect.gen(function* () { |
| 85 | const engine = yield* Agent; |
| 86 | return yield* GCP.AIPlatform.ReasoningEnginesSandboxEnvironmentTemplate("Browser", { |
| 87 | reasoningEngine: engine.name, |
| 88 | displayName: "browser", |
| 89 | defaultContainerEnvironment: { |
| 90 | defaultContainerCategory: "DEFAULT_CONTAINER_CATEGORY_COMPUTER_USE", |
| 91 | }, |
| 92 | }); |
| 93 | }); |
| 94 | |
| 95 | /** Sandbox probes, declared only when their resources are deployed. */ |
| 96 | const sandboxProbes = Effect.gen(function* () { |
| 97 | if (!runSandboxes) return {}; |
| 98 | const getSandbox = yield* GCP.AIPlatform.GetSandboxEnvironment(Code); |
| 99 | const getTemplate = yield* GCP.AIPlatform.GetSandboxEnvironmentTemplate(Browser); |
| 100 | const probes = { |
| 101 | getSandboxEnvironment: getSandbox(), |
| 102 | getSandboxEnvironmentTemplate: getTemplate(), |
| 103 | }; |
| 104 | if (!runSandboxPause) return probes; |
| 105 | const pause = yield* GCP.AIPlatform.PauseSandboxEnvironment(Paused); |
| 106 | const pauseResumed = yield* GCP.AIPlatform.PauseSandboxEnvironment(Resumed); |
| 107 | const resume = yield* GCP.AIPlatform.ResumeSandboxEnvironment(Resumed); |
| 108 | return { |
| 109 | ...probes, |
| 110 | pauseSandboxEnvironment: pause({ body: {} }), |
| 111 | resumeSandboxEnvironment: Effect.gen(function* () { |
| 112 | yield* pauseResumed({ body: {} }); |
| 113 | return yield* resume({ body: {} }); |
| 114 | }), |
| 115 | }; |
| 116 | }); |
| 117 | |
| 118 | /** |
| 119 | * Effect-native Cloud Run service exercising every AI Platform binding as |
| 120 | * its own runtime service account. Deployed from |
| 121 | * {@link ../Bindings.test.ts}. |
| 122 | */ |
| 123 | export default class AIPlatformBindingsHost extends GCP.Function<AIPlatformBindingsHost>()( |
| 124 | "AIPlatformBindingsHost", |
| 125 | { |
| 126 | main: import.meta.url, |
| 127 | invokerIamDisabled: true, |
| 128 | env: { |
| 129 | AIPLATFORM_SANDBOXES: runSandboxes ? "1" : "0", |
| 130 | AIPLATFORM_SANDBOX_PAUSE: runSandboxPause ? "1" : "0", |
| 131 | }, |
| 132 | }, |
| 133 | Effect.gen(function* () { |
| 134 | const gemini = yield* GCP.AIPlatform.GenerateContent("gemini-2.5-flash"); |
| 135 | const regional = yield* GCP.AIPlatform.GenerateContent({ |
| 136 | model: "gemini-2.5-flash", |
| 137 | location: "us-central1", |
| 138 | }); |
| 139 | const getEngine = yield* GCP.AIPlatform.GetReasoningEngine(Agent); |
| 140 | const queryEngine = yield* GCP.AIPlatform.QueryReasoningEngine(Agent); |
| 141 | const getPipeline = yield* GCP.AIPlatform.GetTrainingPipeline(Train); |
| 142 | const cancelPipeline = yield* GCP.AIPlatform.CancelTrainingPipeline(Train); |
| 143 | const sandboxes = yield* sandboxProbes; |
| 144 | |
| 145 | return { |
| 146 | fetch: serveProbes({ |
| 147 | generateContent: Effect.gen(function* () { |
| 148 | const text = yield* gemini.text("Reply with exactly: pong"); |
| 149 | const response = yield* regional.generate({ |
| 150 | contents: [{ role: "user", parts: [{ text: "Reply with exactly: ping" }] }], |
| 151 | }); |
| 152 | return { |
| 153 | text, |
| 154 | regional: |
| 155 | response.candidates?.[0]?.content?.parts?.map((part) => part.text ?? "").join("") ?? |
| 156 | "", |
| 157 | modelVersion: response.modelVersion, |
| 158 | }; |
| 159 | }), |
| 160 | getReasoningEngine: getEngine(), |
| 161 | queryReasoningEngine: queryEngine({ |
| 162 | body: { input: { input: "hello" } }, |
| 163 | }), |
| 164 | getTrainingPipeline: getPipeline(), |
| 165 | cancelTrainingPipeline: cancelPipeline({ body: {} }), |
| 166 | ...sandboxes, |
| 167 | }), |
| 168 | }; |
| 169 | }).pipe( |
| 170 | Effect.provide(GCP.AIPlatform.GenerateContentHttp), |
| 171 | Effect.provide(GCP.AIPlatform.GetReasoningEngineHttp), |
| 172 | Effect.provide(GCP.AIPlatform.QueryReasoningEngineHttp), |
| 173 | Effect.provide(GCP.AIPlatform.GetTrainingPipelineHttp), |
| 174 | Effect.provide(GCP.AIPlatform.CancelTrainingPipelineHttp), |
| 175 | Effect.provide(GCP.AIPlatform.GetSandboxEnvironmentHttp), |
| 176 | Effect.provide(GCP.AIPlatform.GetSandboxEnvironmentTemplateHttp), |
| 177 | Effect.provide(GCP.AIPlatform.PauseSandboxEnvironmentHttp), |
| 178 | Effect.provide(GCP.AIPlatform.ResumeSandboxEnvironmentHttp), |
| 179 | ), |
| 180 | ) {} |