| 635 | } |
| 636 | |
| 637 | export class TestLLMServer extends Context.Service<TestLLMServer, TestLLMServer.Service>()("@test/LLMServer") { |
| 638 | static readonly layer = Layer.effect( |
| 639 | TestLLMServer, |
| 640 | Effect.gen(function* () { |
| 641 | const server = yield* HttpServer.HttpServer |
| 642 | const router = yield* HttpRouter.HttpRouter |
| 643 | |
| 644 | let hits: Hit[] = [] |
| 645 | let list: Queue[] = [] |
| 646 | let waits: Wait[] = [] |
| 647 | let misses: Hit[] = [] |
| 648 | |
| 649 | const queue = (...input: (Item | Reply)[]) => { |
| 650 | list = [...list, ...input.map((value) => ({ item: item(value) }))] |
| 651 | } |
| 652 | |
| 653 | const queueMatch = (match: Match, ...input: (Item | Reply)[]) => { |
| 654 | list = [...list, ...input.map((value) => ({ item: item(value), match }))] |
| 655 | } |
| 656 | |
| 657 | const notify = Effect.fnUntraced(function* () { |
| 658 | const ready = waits.filter((item) => hits.length >= item.count) |
| 659 | if (!ready.length) return |
| 660 | waits = waits.filter((item) => hits.length < item.count) |
| 661 | yield* Effect.forEach(ready, (item) => Deferred.succeed(item.ready, void 0)) |
| 662 | }) |
| 663 | |
| 664 | const pull = (hit: Hit) => { |
| 665 | const index = list.findIndex((entry) => !entry.match || entry.match(hit)) |
| 666 | if (index === -1) return |
| 667 | const first = list[index] |
| 668 | list = [...list.slice(0, index), ...list.slice(index + 1)] |
| 669 | return first.item |
| 670 | } |
| 671 | |
| 672 | const handle = Effect.fn("TestLLMServer.handle")(function* (mode: "chat" | "responses") { |
| 673 | const req = yield* HttpServerRequest.HttpServerRequest |
| 674 | const body = yield* req.json.pipe(Effect.orElseSucceed(() => ({}))) |
| 675 | const current = hit(req.originalUrl, body) |
| 676 | if (isTitleRequest(body)) { |
| 677 | hits = [...hits, current] |
| 678 | yield* notify() |
| 679 | const auto: Sse = { type: "sse", head: [role()], tail: [textLine("E2E Title"), finishLine("stop")] } |
| 680 | if (mode === "responses") return send(responses(auto, modelFrom(body))) |
| 681 | return send(auto) |
| 682 | } |
| 683 | const next = pull(current) |
| 684 | if (!next) { |
| 685 | hits = [...hits, current] |
| 686 | yield* notify() |
| 687 | const auto: Sse = { type: "sse", head: [role()], tail: [textLine("ok"), finishLine("stop")] } |
| 688 | if (mode === "responses") return send(responses(auto, modelFrom(body))) |
| 689 | return send(auto) |
| 690 | } |
| 691 | hits = [...hits, current] |
| 692 | yield* notify() |
| 693 | if (next.type !== "sse") return fail(next) |
| 694 | if (mode === "responses") return send(responses(next, modelFrom(body))) |
nothing calls this directly
no test coverage detected