|
51 | 51 | import * as Chunk from "effect/Chunk" |
52 | 52 | import * as Context from "effect/Context" |
53 | 53 | import * as Effect from "effect/Effect" |
| 54 | +import * as Mailbox from "effect/Mailbox" |
54 | 55 | import * as Option from "effect/Option" |
55 | 56 | import * as ParseResult from "effect/ParseResult" |
56 | 57 | import * as Predicate from "effect/Predicate" |
57 | 58 | import * as Schema from "effect/Schema" |
| 59 | +import type * as Scope from "effect/Scope" |
58 | 60 | import * as Stream from "effect/Stream" |
59 | 61 | import type { Span } from "effect/Tracer" |
60 | 62 | import type { Concurrency, Mutable, NoExcessProperties } from "effect/Types" |
@@ -797,7 +799,9 @@ export const make: (params: ConstructorParams) => Effect.Effect<Service> = Effec |
797 | 799 | >(options: Options & GenerateTextOptions<Tools>, providerOptions: Mutable<ProviderOptions>) => Effect.Effect< |
798 | 800 | Stream.Stream<Response.StreamPart<Tools>, AiError.AiError | ParseResult.ParseError, IdGenerator>, |
799 | 801 | Options extends { readonly toolkit: Effect.Effect<Toolkit.WithHandler<Tools>, infer _E, infer _R> } ? _E : never, |
800 | | - Options extends { readonly toolkit: Effect.Effect<Toolkit.WithHandler<Tools>, infer _E, infer _R> } ? _R : never |
| 802 | + | (Options extends { readonly toolkit: Effect.Effect<Toolkit.WithHandler<Tools>, infer _E, infer _R> } ? _R |
| 803 | + : never) |
| 804 | + | Scope.Scope |
801 | 805 | > = Effect.fnUntraced( |
802 | 806 | function*< |
803 | 807 | Tools extends Record<string, Tool.Any>, |
@@ -842,16 +846,21 @@ export const make: (params: ConstructorParams) => Effect.Effect<Service> = Effec |
842 | 846 | ) as Stream.Stream<Response.StreamPart<Tools>, AiError.AiError | ParseResult.ParseError, IdGenerator> |
843 | 847 | } |
844 | 848 |
|
845 | | - const ResponseSchema = Schema.Chunk(Response.StreamPart(toolkit)) |
| 849 | + const mailbox = yield* Mailbox.make<Response.StreamPart<Tools>, AiError.AiError | ParseResult.ParseError>() |
| 850 | + const ResponseSchema = Schema.Array(Response.StreamPart(toolkit)) |
846 | 851 | const decode = Schema.decode(ResponseSchema) |
847 | | - return params.streamText(providerOptions).pipe( |
848 | | - Stream.mapChunksEffect(Effect.fnUntraced(function*(chunk) { |
| 852 | + yield* params.streamText(providerOptions).pipe( |
| 853 | + Stream.runForEachChunk(Effect.fnUntraced(function*(chunk) { |
849 | 854 | const rawContent = Chunk.toArray(chunk) |
850 | | - const toolResults = yield* resolveToolCalls(rawContent, toolkit, options.concurrency) |
851 | 855 | const content = yield* decode(rawContent) |
852 | | - return Chunk.unsafeFromArray([...content, ...toolResults]) |
853 | | - })) |
854 | | - ) as Stream.Stream<Response.StreamPart<Tools>, AiError.AiError | ParseResult.ParseError, IdGenerator> |
| 856 | + yield* mailbox.offerAll(content) |
| 857 | + const toolResults = yield* resolveToolCalls(rawContent, toolkit, options.concurrency) |
| 858 | + yield* mailbox.offerAll(toolResults as any) |
| 859 | + })), |
| 860 | + Mailbox.into(mailbox), |
| 861 | + Effect.forkScoped |
| 862 | + ) |
| 863 | + return Mailbox.toStream(mailbox) |
855 | 864 | } |
856 | 865 | ) |
857 | 866 |
|
|
0 commit comments