|
1 | 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
2 | 2 | import { assert, describe, it } from "@effect/vitest"; |
| 3 | +import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts"; |
| 4 | +import * as Cause from "effect/Cause"; |
3 | 5 | import * as Effect from "effect/Effect"; |
| 6 | +import * as Queue from "effect/Queue"; |
| 7 | +import * as Schema from "effect/Schema"; |
4 | 8 | import * as Sink from "effect/Sink"; |
5 | 9 | import * as Stream from "effect/Stream"; |
6 | 10 | import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; |
7 | 11 |
|
8 | 12 | import { checkPiProviderStatus, MINIMUM_PI_VERSION } from "./PiProvider.ts"; |
| 13 | +import { mergeProviderSnapshot } from "./ProviderRegistry.ts"; |
9 | 14 |
|
10 | 15 | const encoder = new TextEncoder(); |
| 16 | +const decodeRpcRequest = Schema.decodeSync( |
| 17 | + Schema.fromJsonString(Schema.Struct({ id: Schema.String, type: Schema.String })), |
| 18 | +); |
| 19 | +const encodeJsonLine = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown)); |
11 | 20 |
|
12 | 21 | function processHandle(input: { |
13 | 22 | readonly stdout?: string; |
@@ -44,6 +53,67 @@ function piProbeSpawner(version: string) { |
44 | 53 | }); |
45 | 54 | } |
46 | 55 |
|
| 56 | +const piDiscoverySpawner = (inventory: unknown) => |
| 57 | + Effect.gen(function* () { |
| 58 | + const stdout = yield* Queue.unbounded<Uint8Array, Cause.Done>(); |
| 59 | + let stdinBuffer = ""; |
| 60 | + const stdin = Sink.forEach((chunk: Uint8Array) => |
| 61 | + Effect.gen(function* () { |
| 62 | + stdinBuffer += new TextDecoder().decode(chunk); |
| 63 | + while (true) { |
| 64 | + const newline = stdinBuffer.indexOf("\n"); |
| 65 | + if (newline === -1) return; |
| 66 | + const line = stdinBuffer.slice(0, newline); |
| 67 | + stdinBuffer = stdinBuffer.slice(newline + 1); |
| 68 | + if (line.length === 0) continue; |
| 69 | + const request = decodeRpcRequest(line); |
| 70 | + const data = |
| 71 | + request.type === "get_available_models" |
| 72 | + ? inventory |
| 73 | + : request.type === "get_commands" |
| 74 | + ? { commands: [] } |
| 75 | + : {}; |
| 76 | + yield* Queue.offer( |
| 77 | + stdout, |
| 78 | + encoder.encode( |
| 79 | + `${encodeJsonLine({ type: "response", id: request.id, command: request.type, success: true, data })}\n`, |
| 80 | + ), |
| 81 | + ); |
| 82 | + } |
| 83 | + }), |
| 84 | + ); |
| 85 | + return ChildProcessSpawner.make((command) => { |
| 86 | + const args = ChildProcess.isStandardCommand(command) ? command.args : []; |
| 87 | + return Effect.succeed( |
| 88 | + args.includes("--version") |
| 89 | + ? processHandle({ stdout: "pi 0.84.3\n" }) |
| 90 | + : ChildProcessSpawner.makeHandle({ |
| 91 | + ...processHandle({}), |
| 92 | + exitCode: Effect.never, |
| 93 | + isRunning: Effect.succeed(true), |
| 94 | + stdin, |
| 95 | + stdout: Stream.fromQueue(stdout), |
| 96 | + }), |
| 97 | + ); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | +const piInstance = { |
| 102 | + instanceId: ProviderInstanceId.make("pi-personal"), |
| 103 | + driver: ProviderDriverKind.make("pi"), |
| 104 | +}; |
| 105 | + |
| 106 | +const previousModels = [ |
| 107 | + { slug: "default", name: "Pi default", isCustom: false, capabilities: null }, |
| 108 | + { slug: "openrouter/openai/gpt-5", name: "GPT-5", isCustom: false, capabilities: null }, |
| 109 | + { |
| 110 | + slug: "anthropic/claude-sonnet-4", |
| 111 | + name: "Claude Sonnet 4", |
| 112 | + isCustom: false, |
| 113 | + capabilities: null, |
| 114 | + }, |
| 115 | +] as const satisfies ServerProvider["models"]; |
| 116 | + |
47 | 117 | const settings = { |
48 | 118 | enabled: true, |
49 | 119 | binaryPath: "pi", |
@@ -75,6 +145,80 @@ describe("PiProvider", () => { |
75 | 145 | ["default"], |
76 | 146 | ); |
77 | 147 | assert.include(snapshot.message ?? "", "could not refresh its models and commands"); |
| 148 | + const nextProvider = { ...snapshot, ...piInstance }; |
| 149 | + assert.deepEqual( |
| 150 | + mergeProviderSnapshot({ ...nextProvider, models: previousModels }, nextProvider).models.map( |
| 151 | + (model) => model.slug, |
| 152 | + ), |
| 153 | + previousModels.map((model) => model.slug), |
| 154 | + ); |
| 155 | + }).pipe(Effect.provide(NodeServices.layer)), |
| 156 | + ); |
| 157 | + |
| 158 | + it.effect("removes logged-out OpenRouter models after successful Pi discovery", () => |
| 159 | + Effect.gen(function* () { |
| 160 | + const spawner = yield* piDiscoverySpawner({ |
| 161 | + models: [{ provider: "anthropic", id: "claude-sonnet-4" }], |
| 162 | + }); |
| 163 | + const snapshot = yield* checkPiProviderStatus(settings).pipe( |
| 164 | + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 165 | + ); |
| 166 | + assert.equal(snapshot.status, "ready"); |
| 167 | + assert.equal(snapshot.auth.status, "authenticated"); |
| 168 | + const nextProvider = { ...snapshot, ...piInstance }; |
| 169 | + assert.deepEqual( |
| 170 | + mergeProviderSnapshot({ ...nextProvider, models: previousModels }, nextProvider).models.map( |
| 171 | + (model) => model.slug, |
| 172 | + ), |
| 173 | + ["default", "anthropic/claude-sonnet-4"], |
| 174 | + ); |
| 175 | + }).pipe(Effect.provide(NodeServices.layer)), |
| 176 | + ); |
| 177 | + |
| 178 | + it.effect("clears discovered models when successful Pi discovery returns no usable models", () => |
| 179 | + Effect.gen(function* () { |
| 180 | + const spawner = yield* piDiscoverySpawner({ models: [] }); |
| 181 | + const snapshot = yield* checkPiProviderStatus(settings).pipe( |
| 182 | + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 183 | + ); |
| 184 | + assert.equal(snapshot.status, "warning"); |
| 185 | + assert.equal(snapshot.auth.status, "unauthenticated"); |
| 186 | + const nextProvider = { ...snapshot, ...piInstance }; |
| 187 | + assert.deepEqual( |
| 188 | + mergeProviderSnapshot({ ...nextProvider, models: previousModels }, nextProvider).models.map( |
| 189 | + (model) => model.slug, |
| 190 | + ), |
| 191 | + ["default"], |
| 192 | + ); |
| 193 | + }).pipe(Effect.provide(NodeServices.layer)), |
| 194 | + ); |
| 195 | + |
| 196 | + it.effect.each([ |
| 197 | + ["missing models", {}], |
| 198 | + ["non-array models", { models: {} }], |
| 199 | + ["missing model provider", { models: [{ id: "claude-sonnet-4" }] }], |
| 200 | + ["missing model id", { models: [{ provider: "anthropic" }] }], |
| 201 | + ["empty model provider", { models: [{ provider: "", id: "claude-sonnet-4" }] }], |
| 202 | + ["empty model id", { models: [{ provider: "anthropic", id: "" }] }], |
| 203 | + [ |
| 204 | + "mixed valid and invalid models", |
| 205 | + { models: [{ provider: "anthropic", id: "claude-sonnet-4" }, { provider: "openrouter" }] }, |
| 206 | + ], |
| 207 | + ] as const)("retains cached models when Pi discovery returns %s", (_label, inventory) => |
| 208 | + Effect.gen(function* () { |
| 209 | + const spawner = yield* piDiscoverySpawner(inventory); |
| 210 | + const snapshot = yield* checkPiProviderStatus(settings).pipe( |
| 211 | + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), |
| 212 | + ); |
| 213 | + const nextProvider = { ...snapshot, ...piInstance }; |
| 214 | + assert.deepEqual( |
| 215 | + mergeProviderSnapshot({ ...nextProvider, models: previousModels }, nextProvider).models.map( |
| 216 | + (model) => model.slug, |
| 217 | + ), |
| 218 | + previousModels.map((model) => model.slug), |
| 219 | + ); |
| 220 | + assert.equal(snapshot.status, "ready"); |
| 221 | + assert.equal(snapshot.auth.status, "unknown"); |
78 | 222 | }).pipe(Effect.provide(NodeServices.layer)), |
79 | 223 | ); |
80 | 224 | }); |
0 commit comments