@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 156 lines 5.14 KB
Raw
1 import type { AIGateway } from "@alchemy.run/neon/AIGateway";
2 import { backendEnvKey } from "@alchemy.run/neon/BackendConnection";
3 import { FunctionEnvironment } from "@alchemy.run/neon/FunctionEnvironment";
4 import { QueryAIGateway, QueryAIGatewayHttp } from "@alchemy.run/neon/QueryAIGateway";
5 import { expect, test } from "@alchemy.run/test";
6 import * as Output from "alchemy/Output";
7 import { RuntimeContext } from "alchemy/RuntimeContext";
8 import * as Effect from "effect/Effect";
9 import * as Layer from "effect/Layer";
10 import * as Redacted from "effect/Redacted";
11 import * as Result from "effect/Result";
12
13 const gateway: AIGateway = {
14 FQN: "Gateway",
15 LogicalId: "Gateway",
16 Props: { branch: { projectId: "project", branchId: "branch" } },
17 projectId: Output.literal("project"),
18 branchId: Output.literal("branch"),
19 baseUrl: Output.literal("https://branch.invalid"),
20 credential: undefined,
21 };
22
23 const runtimeMode = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
24 Effect.gen(function* () {
25 const previous = yield* Effect.sync(() => {
26 const previous = globalThis.__ALCHEMY_RUNTIME__;
27 globalThis.__ALCHEMY_RUNTIME__ = true;
28 return previous;
29 });
30 return yield* effect.pipe(
31 Effect.ensuring(
32 Effect.sync(() => {
33 globalThis.__ALCHEMY_RUNTIME__ = previous;
34 }),
35 ),
36 );
37 });
38
39 // The deployed host's RuntimeContext, holding what the binding bound at deploy.
40 const hostRuntime = (bound: Record<string, string>) =>
41 Layer.succeed(RuntimeContext, {
42 Type: "Neon.Function",
43 id: "Fn",
44 env: {},
45 get: <T>(key: string) => Effect.succeed(bound[key] as T | undefined),
46 set: (key: string) => Effect.succeed(key),
47 });
48
49 const services = (injected: boolean, environment?: Record<string, string>) =>
50 QueryAIGatewayHttp.pipe(
51 Layer.provideMerge(
52 Layer.mergeAll(
53 hostRuntime({
54 [backendEnvKey(gateway.FQN, "AI_GATEWAY_URL")]: "https://branch.invalid",
55 [backendEnvKey(gateway.FQN, "AI_GATEWAY_TOKEN")]: "managed-scoped-token",
56 [backendEnvKey(gateway.FQN, "AI_GATEWAY_INJECTED")]: injected ? "yes" : "no",
57 }),
58 environment ? Layer.succeed(FunctionEnvironment, environment) : Layer.empty,
59 ),
60 ),
61 );
62
63 for (const mode of ["injected", "managed", "managed-with-injection"] as const) {
64 test.effect(
65 `HTTP AI client uses ${mode} credentials without account keys`,
66 () =>
67 runtimeMode(
68 Effect.gen(function* () {
69 const client = yield* QueryAIGateway(gateway);
70 expect(Layer.isLayer(client.model({ model: "gpt-5-mini" }))).toBe(true);
71 expect(yield* client.chatBaseUrl).toBe("https://branch.invalid/v1");
72 expect(yield* client.responsesBaseUrl).toBe("https://branch.invalid/openai/v1");
73 expect(yield* client.anthropicBaseUrl).toBe("https://branch.invalid/anthropic");
74 const token = yield* client.token;
75 expect(Redacted.isRedacted(token)).toBe(true);
76 expect(Redacted.value(token)).toBe(
77 mode === "injected" ? "injected-scoped-token" : "managed-scoped-token",
78 );
79 expect(JSON.stringify(token)).not.toContain("scoped-token");
80 }).pipe(
81 Effect.provide(
82 services(
83 mode === "injected",
84 mode === "managed"
85 ? undefined
86 : {
87 NEON_AI_GATEWAY_BASE_URL: "https://branch.invalid/",
88 NEON_AI_GATEWAY_TOKEN: "injected-scoped-token",
89 NEON_API_KEY: "DO_NOT_BIND_ACCOUNT_KEY",
90 },
91 ),
92 ),
93 ),
94 ),
95 {
96 tags: [
97 "unit",
98 "provider:neon",
99 "provider:neon:aigateway",
100 "provider:neon:backendconnection",
101 "provider:neon:function",
102 "provider:neon:languagemodel",
103 "local",
104 ],
105 exclusive: true,
106 },
107 );
108 }
109
110 for (const [name, environment, message] of [
111 [
112 "wrong branch",
113 {
114 NEON_AI_GATEWAY_BASE_URL: "https://other.invalid",
115 NEON_AI_GATEWAY_TOKEN: "other-token",
116 },
117 "does not match",
118 ],
119 [
120 "missing grant",
121 {
122 NEON_AI_GATEWAY_BASE_URL: "https://branch.invalid",
123 NEON_API_KEY: "DO_NOT_BIND_ACCOUNT_KEY",
124 },
125 "did not inject",
126 ],
127 ["missing environment", undefined, "does not match"],
128 ] as const) {
129 test.effect(
130 `HTTP AI client rejects ${name} instead of falling back to another token`,
131 () =>
132 runtimeMode(
133 Effect.gen(function* () {
134 const client = yield* QueryAIGateway(gateway);
135 const result = yield* Effect.result(Effect.sandbox(client.token));
136 expect(Result.isFailure(result)).toBe(true);
137 if (Result.isFailure(result)) {
138 expect(String(result.failure)).toContain(message);
139 expect(String(result.failure)).not.toContain("DO_NOT_BIND_ACCOUNT_KEY");
140 }
141 }).pipe(Effect.provide(services(true, environment))),
142 ),
143 {
144 tags: [
145 "unit",
146 "provider:neon",
147 "provider:neon:aigateway",
148 "provider:neon:backendconnection",
149 "provider:neon:function",
150 "provider:neon:languagemodel",
151 "local",
152 ],
153 exclusive: true,
154 },
155 );
156 }