The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Neon from "@alchemy.run/neon"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as SDK from "@distilled.cloud/neon"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as HttpClient from "effect/http/HttpClient"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | import HttpLanguageModel from "./fixtures/language-model-http.ts"; |
| 10 | import NativeLanguageModel from "./fixtures/language-model-native.ts"; |
| 11 | import { languageModelBranch } from "./fixtures/language-model-resources.ts"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: Neon.providers() }); |
| 14 | const Text = Schema.Struct({ text: Schema.String }); |
| 15 | |
| 16 | test.provider.skipIf(process.env.NEON_TEST_AI_PAID !== "1" || !process.env.NEON_TEST_AI_MODEL)( |
| 17 | "injected and explicit credentials use the same HTTP Effect AI client for generation, streaming, tools and objects", |
| 18 | (stack) => |
| 19 | Effect.gen(function* () { |
| 20 | yield* stack.destroy(); |
| 21 | const deployed = yield* stack.deploy( |
| 22 | Effect.gen(function* () { |
| 23 | const branch = yield* languageModelBranch; |
| 24 | const native = yield* NativeLanguageModel; |
| 25 | const http = yield* HttpLanguageModel; |
| 26 | return { branch, native, http }; |
| 27 | }), |
| 28 | ); |
| 29 | yield* Effect.gen(function* () { |
| 30 | const client = (yield* HttpClient.HttpClient).pipe( |
| 31 | HttpClient.transformResponse( |
| 32 | Effect.tap((response) => |
| 33 | response.status < 400 |
| 34 | ? Effect.void |
| 35 | : response.json.pipe( |
| 36 | Effect.flatMap( |
| 37 | Schema.decodeUnknownEffect(Schema.Struct({ reason: Schema.String })), |
| 38 | ), |
| 39 | Effect.flatMap(({ reason }) => |
| 40 | Effect.log("Neon AI fixture rejected the request", { |
| 41 | status: response.status, |
| 42 | reason, |
| 43 | }), |
| 44 | ), |
| 45 | ), |
| 46 | ), |
| 47 | ), |
| 48 | HttpClient.filterStatusOk, |
| 49 | ); |
| 50 | for (const fn of [deployed.native, deployed.http]) { |
| 51 | const generated = yield* client.get(`${fn.url}/generate`).pipe( |
| 52 | Effect.retry({ times: 3, schedule: Schedule.spaced("1 second") }), |
| 53 | Effect.flatMap((response) => response.json), |
| 54 | Effect.flatMap(Schema.decodeUnknownEffect(Text)), |
| 55 | ); |
| 56 | expect(generated.text.length).toBeGreaterThan(0); |
| 57 | const streamed = yield* client.get(`${fn.url}/stream`).pipe( |
| 58 | Effect.flatMap((response) => response.json), |
| 59 | Effect.flatMap( |
| 60 | Schema.decodeUnknownEffect( |
| 61 | Schema.Struct({ |
| 62 | text: Schema.String, |
| 63 | finished: Schema.Boolean, |
| 64 | }), |
| 65 | ), |
| 66 | ), |
| 67 | ); |
| 68 | expect(streamed.text.length).toBeGreaterThan(0); |
| 69 | expect(streamed.finished).toBe(true); |
| 70 | const tool = yield* client.get(`${fn.url}/tool`).pipe( |
| 71 | Effect.flatMap((response) => response.json), |
| 72 | Effect.flatMap( |
| 73 | Schema.decodeUnknownEffect(Schema.Struct({ results: Schema.Array(Schema.Number) })), |
| 74 | ), |
| 75 | ); |
| 76 | expect(tool.results).toContain(5); |
| 77 | const object = yield* client.get(`${fn.url}/object`).pipe( |
| 78 | Effect.flatMap((response) => response.json), |
| 79 | Effect.flatMap(Schema.decodeUnknownEffect(Schema.Struct({ greeting: Schema.String }))), |
| 80 | ); |
| 81 | expect(object.greeting.length).toBeGreaterThan(0); |
| 82 | } |
| 83 | const credentials = yield* SDK.listCredentials({ |
| 84 | project_id: deployed.branch.projectId, |
| 85 | branch_id: deployed.branch.branchId, |
| 86 | }); |
| 87 | expect( |
| 88 | credentials.credentials.some( |
| 89 | (entry) => !entry.revoked_at && entry.scopes.includes("ai_gateway:invoke"), |
| 90 | ), |
| 91 | ).toBe(true); |
| 92 | }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))); |
| 93 | expect( |
| 94 | yield* SDK.getProjectBranch({ |
| 95 | project_id: deployed.branch.projectId, |
| 96 | branch_id: deployed.branch.branchId, |
| 97 | }).pipe( |
| 98 | Effect.as(false), |
| 99 | Effect.catchTag("NotFound", () => Effect.succeed(true)), |
| 100 | ), |
| 101 | ).toBe(true); |
| 102 | yield* stack.destroy(); |
| 103 | }), |
| 104 | { |
| 105 | tags: [ |
| 106 | "provider:neon", |
| 107 | "provider:neon:aigateway", |
| 108 | "provider:neon:branch", |
| 109 | "provider:neon:credential", |
| 110 | "provider:neon:function", |
| 111 | "provider:neon:languagemodel", |
| 112 | "provider:neon:project", |
| 113 | "live", |
| 114 | ], |
| 115 | timeout: 120_000, |
| 116 | }, |
| 117 | ); |