|
1 | 1 | export * as TestLLM from "./testing.js" |
2 | 2 |
|
3 | | -import { LLMClient, type Interface as LLMClientShape } from "./route/client.js" |
| 3 | +import { LLMClient } from "./route/client.js" |
4 | 4 | import { |
5 | 5 | LLMEvent, |
6 | 6 | LLMResponse, |
@@ -16,13 +16,33 @@ export type Response = readonly LLMEvent[] | Stream.Stream<LLMEvent, AIError> |
16 | 16 |
|
17 | 17 | export type Gate = Readonly<{ started: Effect.Effect<void>; release: Effect.Effect<void> }> |
18 | 18 |
|
| 19 | +type ClientInterface = Context.Service.Shape<typeof LLMClient.Service> |
| 20 | + |
| 21 | +export type Responder = (request: LLMRequest) => Response |
| 22 | + |
| 23 | +export interface TestInterface extends ClientInterface { |
| 24 | + /** Returns a snapshot of requests observed at execution time. */ |
| 25 | + readonly requests: () => Effect.Effect<readonly LLMRequest[]> |
| 26 | + readonly push: (...responses: readonly Response[]) => Effect.Effect<void> |
| 27 | + /** Replaces the fallback without changing queued responses. */ |
| 28 | + readonly always: (response: Response) => Effect.Effect<void> |
| 29 | + /** Answers requests after the one-shot queue is exhausted; receives the original request. */ |
| 30 | + readonly serve: (responder: Responder) => Effect.Effect<void> |
| 31 | + /** Waits for request arrivals, not output or completion. */ |
| 32 | + readonly wait: (count: number) => Effect.Effect<void> |
| 33 | + readonly gate: () => Effect.Effect<Gate, never, Scope.Scope> |
| 34 | +} |
| 35 | + |
| 36 | +export class Test extends Context.Service<Test, TestInterface>()("@opencode/ai/TestLLM/Test") {} |
| 37 | + |
| 38 | +/** @deprecated Use TestInterface through Test and testLayer. */ |
19 | 39 | export interface Interface { |
20 | 40 | readonly requests: LLMRequest[] |
21 | 41 | readonly push: (...responses: readonly Response[]) => Effect.Effect<void> |
22 | 42 | readonly always: (response: Response) => Effect.Effect<void> |
23 | 43 | readonly wait: (count: number) => Effect.Effect<void> |
24 | 44 | readonly gate: Effect.Effect<Gate, never, Scope.Scope> |
25 | | - readonly client: LLMClientShape |
| 45 | + readonly client: ClientInterface |
26 | 46 | } |
27 | 47 |
|
28 | 48 | export interface LayerOptions { |
@@ -31,6 +51,7 @@ export interface LayerOptions { |
31 | 51 | readonly fallback?: Response |
32 | 52 | } |
33 | 53 |
|
| 54 | +/** @deprecated Use Test and testLayer for normal client methods and test controls. */ |
34 | 55 | export class Service extends Context.Service<Service, Interface>()("@opencode/ai/TestLLM") {} |
35 | 56 |
|
36 | 57 | export const complete = ( |
@@ -80,59 +101,64 @@ export const hangAfter = (...events: readonly LLMEvent[]) => Stream.concat(Strea |
80 | 101 |
|
81 | 102 | const toStream = (response: Response) => (Stream.isStream(response) ? response : Stream.fromIterable(response)) |
82 | 103 |
|
83 | | -export const layer = (options: LayerOptions = {}) => |
84 | | - Layer.effect( |
85 | | - Service, |
86 | | - Effect.gen(function* () { |
87 | | - const requests: LLMRequest[] = [] |
88 | | - const responses: Response[] = [] |
89 | | - let started = Deferred.makeUnsafe<void>() |
90 | | - let fallback = options.fallback |
91 | | - let activeGate: { readonly started: Queue.Queue<void>; readonly release: Latch.Latch } | undefined |
92 | | - const wait = (count: number): Effect.Effect<void> => |
93 | | - Effect.suspend(() => |
94 | | - requests.length >= count ? Effect.void : Deferred.await(started).pipe(Effect.andThen(wait(count))), |
95 | | - ) |
96 | | - |
97 | | - const stream = ((request: LLMRequest) => { |
98 | | - requests.push(options.transformRequest?.(request) ?? request) |
| 104 | +const make = (options: LayerOptions) => |
| 105 | + Effect.sync(() => { |
| 106 | + const requests: LLMRequest[] = [] |
| 107 | + const responses: Response[] = [] |
| 108 | + let started = Deferred.makeUnsafe<void>() |
| 109 | + let fallback: Response | Responder | undefined = options.fallback |
| 110 | + let activeGate: { readonly started: Queue.Queue<void>; readonly release: Latch.Latch } | undefined |
| 111 | + const wait = (count: number): Effect.Effect<void> => |
| 112 | + Effect.suspend(() => |
| 113 | + requests.length >= count ? Effect.void : Deferred.await(started).pipe(Effect.andThen(wait(count))), |
| 114 | + ) |
| 115 | + |
| 116 | + const stream: ClientInterface["stream"] = (request) => |
| 117 | + Stream.suspend(() => { |
| 118 | + const count = requests.push(options.transformRequest?.(request) ?? request) |
99 | 119 | const waiting = started |
100 | 120 | started = Deferred.makeUnsafe() |
101 | | - Deferred.doneUnsafe(waiting, Effect.void) |
102 | | - const response = responses.shift() ?? fallback |
103 | | - if (!response) return Stream.die(new Error(`TestLLM has no response for request ${requests.length}`)) |
104 | | - const streamed = toStream(response) |
105 | 121 | const gate = activeGate |
106 | | - if (!gate) return streamed |
107 | | - return Stream.unwrap( |
108 | | - Queue.offer(gate.started, undefined).pipe(Effect.andThen(gate.release.await), Effect.as(streamed)), |
109 | | - ) |
110 | | - }) as LLMClientShape["stream"] |
111 | | - const client = LLMClient.Service.of({ |
112 | | - stream, |
113 | | - generate: (request) => |
114 | | - stream(request).pipe( |
115 | | - Stream.runFold(LLMResponse.empty, LLMResponse.reduce), |
116 | | - Effect.flatMap((state) => { |
117 | | - const response = LLMResponse.complete(state) |
118 | | - if (response) return Effect.succeed(response) |
119 | | - return Effect.die("TestLLM response ended without a terminal finish event") |
120 | | - }), |
121 | | - ), |
| 122 | + try { |
| 123 | + const response = responses.shift() ?? (typeof fallback === "function" ? fallback(request) : fallback) |
| 124 | + if (!response) return Stream.die(new Error(`TestLLM has no response for request ${count}`)) |
| 125 | + const streamed = toStream(response) |
| 126 | + if (!gate) return streamed |
| 127 | + return Stream.unwrap( |
| 128 | + Queue.offer(gate.started, undefined).pipe(Effect.andThen(gate.release.await), Effect.as(streamed)), |
| 129 | + ) |
| 130 | + } finally { |
| 131 | + // Waiters can resume synchronously; assign the reply and gate before notifying them. |
| 132 | + Deferred.doneUnsafe(waiting, Effect.void) |
| 133 | + } |
122 | 134 | }) |
123 | | - |
124 | | - return Service.of({ |
125 | | - requests, |
126 | | - push: (...input) => |
127 | | - Effect.sync(() => { |
128 | | - responses.push(...input) |
| 135 | + const test = Test.of({ |
| 136 | + stream, |
| 137 | + generate: (request) => |
| 138 | + stream(request).pipe( |
| 139 | + Stream.runFold(LLMResponse.empty, LLMResponse.reduce), |
| 140 | + Effect.flatMap((state) => { |
| 141 | + const response = LLMResponse.complete(state) |
| 142 | + if (response) return Effect.succeed(response) |
| 143 | + return Effect.die("TestLLM response ended without a terminal finish event") |
129 | 144 | }), |
130 | | - always: (response) => |
131 | | - Effect.sync(() => { |
132 | | - fallback = response |
133 | | - }), |
134 | | - wait, |
135 | | - gate: Effect.gen(function* () { |
| 145 | + ), |
| 146 | + requests: () => Effect.sync(() => [...requests]), |
| 147 | + push: (...input) => |
| 148 | + Effect.sync(() => { |
| 149 | + responses.push(...input) |
| 150 | + }), |
| 151 | + always: (response) => |
| 152 | + Effect.sync(() => { |
| 153 | + fallback = response |
| 154 | + }), |
| 155 | + serve: (responder) => |
| 156 | + Effect.sync(() => { |
| 157 | + fallback = responder |
| 158 | + }), |
| 159 | + wait, |
| 160 | + gate: () => |
| 161 | + Effect.gen(function* () { |
136 | 162 | const gate = { |
137 | 163 | started: yield* Effect.acquireRelease(Queue.unbounded<void>(), Queue.shutdown), |
138 | 164 | release: yield* Latch.make(), |
@@ -147,11 +173,36 @@ export const layer = (options: LayerOptions = {}) => |
147 | 173 | release, |
148 | 174 | } |
149 | 175 | }), |
150 | | - client, |
151 | | - }) |
152 | | - }), |
| 176 | + }) |
| 177 | + |
| 178 | + return { test, requests } |
| 179 | + }) |
| 180 | + |
| 181 | +/** Provides one shared implementation under the normal client and test-control tags. */ |
| 182 | +export const testLayer = (options: LayerOptions = {}) => |
| 183 | + Layer.effectContext( |
| 184 | + Effect.map(make(options), (implementation) => |
| 185 | + Context.make(LLMClient.Service, implementation.test).pipe(Context.add(Test, implementation.test)), |
| 186 | + ), |
| 187 | + ) |
| 188 | + |
| 189 | +/** @deprecated Use testLayer; retained for published callers of the legacy control interface. */ |
| 190 | +export const layer = (options: LayerOptions = {}) => |
| 191 | + Layer.effect( |
| 192 | + Service, |
| 193 | + Effect.map(make(options), (implementation) => |
| 194 | + Service.of({ |
| 195 | + requests: implementation.requests, |
| 196 | + push: implementation.test.push, |
| 197 | + always: implementation.test.always, |
| 198 | + wait: implementation.test.wait, |
| 199 | + gate: implementation.test.gate(), |
| 200 | + client: implementation.test, |
| 201 | + }), |
| 202 | + ), |
153 | 203 | ) |
154 | 204 |
|
| 205 | +/** @deprecated testLayer provides LLMClient.Service directly. */ |
155 | 206 | export const clientLayer = Layer.effect( |
156 | 207 | LLMClient.Service, |
157 | 208 | Effect.map(Service, (service) => service.client), |
|