| 355 | }) |
| 356 | |
| 357 | const stream: Interface["stream"] = (input) => |
| 358 | Stream.scoped( |
| 359 | Stream.unwrap( |
| 360 | Effect.gen(function* () { |
| 361 | const ctrl = yield* Effect.acquireRelease( |
| 362 | Effect.sync(() => new AbortController()), |
| 363 | (ctrl) => Effect.sync(() => ctrl.abort()), |
| 364 | ) |
| 365 | |
| 366 | const result = yield* run({ ...input, abort: ctrl.signal }) |
| 367 | |
| 368 | if (result.type === "native") return result.stream |
| 369 | |
| 370 | // Adapter seam: both runtimes expose the same LLMEvent stream. Native |
| 371 | // already returns one; AI SDK streams are converted here. |
| 372 | const state = LLMAISDK.adapterState() |
| 373 | return Stream.fromAsyncIterable(result.result.fullStream, (e) => |
| 374 | e instanceof Error ? e : new Error(String(e)), |
| 375 | ).pipe( |
| 376 | Stream.mapEffect((event) => LLMAISDK.toLLMEvents(state, event)), |
| 377 | Stream.flatMap((events) => Stream.fromIterable(events)), |
| 378 | ) |
| 379 | }), |
| 380 | ), |
| 381 | ) |
| 382 | |
| 383 | return Service.of({ stream }) |
| 384 | }), |