MCPcopy
hub / github.com/Effect-TS/effect / streamRequest

Function streamRequest

packages/ai/openrouter/src/OpenRouterClient.ts:122–155  ·  view source on GitHub ↗
(
    request: HttpClientRequest.HttpClientRequest,
    schema: Schema.Schema<A, I, R>
  )

Source from the content-addressed store, hash-verified

120 })
121
122 const streamRequest = <A, I, R>(
123 request: HttpClientRequest.HttpClientRequest,
124 schema: Schema.Schema<A, I, R>
125 ): Stream.Stream<A, AiError.AiError, R> => {
126 const decodeEvent = Schema.decode(Schema.parseJson(schema))
127 return httpClientOk.execute(request).pipe(
128 Effect.map((r) => r.stream),
129 Stream.unwrapScoped,
130 Stream.decodeText(),
131 Stream.pipeThroughChannel(Sse.makeChannel()),
132 Stream.takeWhile((event) => event.data !== "[DONE]"),
133 Stream.mapEffect((event) => decodeEvent(event.data)),
134 Stream.catchTags({
135 RequestError: (error) =>
136 AiError.HttpRequestError.fromRequestError({
137 module: "OpenRouterClient",
138 method: "streamRequest",
139 error
140 }),
141 ResponseError: (error) =>
142 AiError.HttpResponseError.fromResponseError({
143 module: "OpenRouterClient",
144 method: "streamRequest",
145 error
146 }),
147 ParseError: (error) =>
148 AiError.MalformedOutput.fromParseError({
149 module: "OpenRouterClient",
150 method: "streamRequest",
151 error
152 })
153 })
154 )
155 }
156
157 const createChatCompletion: (
158 options: typeof Generated.ChatGenerationParams.Encoded

Callers 1

Calls 7

decodeMethod · 0.80
fromRequestErrorMethod · 0.80
fromResponseErrorMethod · 0.80
pipeMethod · 0.65
mapMethod · 0.65
executeMethod · 0.45
fromParseErrorMethod · 0.45

Tested by

no test coverage detected