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
| 1 | import { AIGateway } from "@alchemy.run/neon/AIGateway"; |
| 2 | import { Branch } from "@alchemy.run/neon/Branch"; |
| 3 | import { Credential } from "@alchemy.run/neon/Credential"; |
| 4 | import { Project } from "@alchemy.run/neon/Project"; |
| 5 | import { providers } from "@alchemy.run/neon/Providers"; |
| 6 | import { expect } from "@alchemy.run/test"; |
| 7 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 8 | import * as SDK from "@distilled.cloud/neon"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as HttpClient from "effect/http/HttpClient"; |
| 11 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 12 | import * as Redacted from "effect/Redacted"; |
| 13 | import * as Schema from "effect/Schema"; |
| 14 | |
| 15 | const { test } = Test.make({ providers: providers() }); |
| 16 | |
| 17 | test.provider( |
| 18 | "gateway discovery performs no fake CRUD and scoped credentials are revoked", |
| 19 | (stack) => |
| 20 | Effect.gen(function* () { |
| 21 | yield* stack.destroy(); |
| 22 | const deployed = yield* stack.deploy( |
| 23 | Effect.gen(function* () { |
| 24 | const project = yield* Project("GatewayProject", { |
| 25 | region: "aws-us-east-2", |
| 26 | }); |
| 27 | const branch = yield* Branch("GatewayBranch", { project }); |
| 28 | const credential = yield* Credential("GatewayCredential", { |
| 29 | branch, |
| 30 | scopes: ["ai_gateway:invoke"], |
| 31 | }); |
| 32 | const gateway = yield* AIGateway("Gateway", { branch, credential }); |
| 33 | return { branch, credential, baseUrl: gateway.baseUrl }; |
| 34 | }), |
| 35 | ); |
| 36 | const request = { |
| 37 | project_id: deployed.branch.projectId, |
| 38 | branch_id: deployed.branch.branchId, |
| 39 | }; |
| 40 | expect((yield* SDK.getProjectBranchAiGateway(request)).base_url.replace(/\/$/, "")).toBe( |
| 41 | deployed.baseUrl, |
| 42 | ); |
| 43 | const http = yield* HttpClient.HttpClient; |
| 44 | const models = yield* http.execute( |
| 45 | HttpClientRequest.get(`${deployed.baseUrl}/v1/models`).pipe( |
| 46 | HttpClientRequest.bearerToken(deployed.credential.apiToken), |
| 47 | ), |
| 48 | ); |
| 49 | expect(models.status).toBe(200); |
| 50 | const catalog = yield* models.json.pipe( |
| 51 | Effect.flatMap( |
| 52 | Schema.decodeUnknownEffect( |
| 53 | Schema.Struct({ |
| 54 | data: Schema.Array(Schema.Struct({ id: Schema.String, enabled: Schema.Boolean })), |
| 55 | }), |
| 56 | ), |
| 57 | ), |
| 58 | ); |
| 59 | expect(catalog.data.length).toBeGreaterThan(0); |
| 60 | yield* Effect.log("Enabled Neon AI Gateway models", { |
| 61 | models: catalog.data.filter((model) => model.enabled).map((model) => model.id), |
| 62 | }); |
| 63 | const list = yield* SDK.listCredentials(request); |
| 64 | expect( |
| 65 | list.credentials.find((entry) => entry.token_id === deployed.credential.tokenId)?.scopes, |
| 66 | ).toContain("ai_gateway:invoke"); |
| 67 | yield* stack.destroy(); |
| 68 | expect( |
| 69 | yield* SDK.getProjectBranch(request).pipe( |
| 70 | Effect.as(false), |
| 71 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 72 | ), |
| 73 | ).toBe(true); |
| 74 | yield* stack.destroy(); |
| 75 | }), |
| 76 | { |
| 77 | tags: [ |
| 78 | "provider:neon", |
| 79 | "provider:neon:aigateway", |
| 80 | "provider:neon:branch", |
| 81 | "provider:neon:credential", |
| 82 | "provider:neon:project", |
| 83 | "live", |
| 84 | ], |
| 85 | timeout: 120_000, |
| 86 | }, |
| 87 | ); |
| 88 | |
| 89 | test.provider.skipIf(process.env.NEON_TEST_AI_PAID !== "1" || !process.env.NEON_TEST_AI_MODEL)( |
| 90 | "paid model invocation is explicitly gated and never purchases credits", |
| 91 | (stack) => |
| 92 | Effect.gen(function* () { |
| 93 | yield* stack.destroy(); |
| 94 | const deployed = yield* stack.deploy( |
| 95 | Effect.gen(function* () { |
| 96 | const project = yield* Project("PaidGatewayProject", { |
| 97 | region: "aws-us-east-2", |
| 98 | }); |
| 99 | const credential = yield* Credential("PaidGatewayCredential", { |
| 100 | project, |
| 101 | scopes: ["ai_gateway:invoke"], |
| 102 | }); |
| 103 | const gateway = yield* AIGateway("PaidGateway", { |
| 104 | project, |
| 105 | credential, |
| 106 | }); |
| 107 | return { credential, baseUrl: gateway.baseUrl }; |
| 108 | }), |
| 109 | ); |
| 110 | const http = yield* HttpClient.HttpClient; |
| 111 | const response = yield* http.execute( |
| 112 | HttpClientRequest.post(`${deployed.baseUrl}/v1/chat/completions`).pipe( |
| 113 | HttpClientRequest.bearerToken(deployed.credential.apiToken), |
| 114 | HttpClientRequest.bodyJsonUnsafe({ |
| 115 | model: process.env.NEON_TEST_AI_MODEL, |
| 116 | messages: [{ role: "user", content: "Reply with hello." }], |
| 117 | max_tokens: 8, |
| 118 | }), |
| 119 | ), |
| 120 | ); |
| 121 | if (response.status !== 200) { |
| 122 | const body = yield* response.json; |
| 123 | const error = |
| 124 | typeof body === "object" && body !== null && "error" in body ? body.error : body; |
| 125 | const message = |
| 126 | typeof error === "object" && error !== null && "message" in error |
| 127 | ? error.message |
| 128 | : undefined; |
| 129 | yield* Effect.log("Neon AI Gateway inference rejected", { |
| 130 | status: response.status, |
| 131 | message: |
| 132 | typeof message === "string" |
| 133 | ? message |
| 134 | .replaceAll(Redacted.value(deployed.credential.apiToken), "[REDACTED]") |
| 135 | .slice(0, 1000) |
| 136 | : "No error message", |
| 137 | }); |
| 138 | } |
| 139 | expect(response.status).toBe(200); |
| 140 | yield* stack.destroy(); |
| 141 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))), |
| 142 | { |
| 143 | tags: [ |
| 144 | "provider:neon", |
| 145 | "provider:neon:aigateway", |
| 146 | "provider:neon:credential", |
| 147 | "provider:neon:project", |
| 148 | "live", |
| 149 | ], |
| 150 | timeout: 90_000, |
| 151 | }, |
| 152 | ); |