@effect-uai/responses 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +45 -0
- package/dist/chunk-CfYAbeIz.mjs +13 -0
- package/dist/index.d.mts +828 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +525 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +54 -0
- package/src/Responses.ts +316 -0
- package/src/codec.ts +298 -0
- package/src/index.ts +4 -0
- package/src/models.ts +24 -0
- package/src/streamEvents.ts +199 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/models.ts","../src/streamEvents.ts","../src/Responses.ts","../src/codec.ts"],"mappings":";;;;;;;;;;;;;;;;KAQY,WAAA;AAAA;;;;;;;cC2EC,kBAAA,EAAkB,MAAA,CAAA,KAAA,WAAA,MAAA,CAAA,MAAA;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAmBlB,aAAA,EAAa,MAAA,CAAA,KAAA,WAAA,MAAA,CAAA,MAAA;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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SACN,MAAA,EAAQ,QAAA,CAAS,QAAA;EAAA,SACjB,OAAA;AAAA;;;;;;cAgLE,WAAA,SACX,CAAA,EAAG,MAAA,CAAO,MAAA,CAAO,aAAA,EAAe,CAAA,EAAG,CAAA,MAClC,MAAA,CAAO,MAAA,CAAO,SAAA,EAAW,CAAA,EAAG,CAAA;;;;cAelB,IAAA,GAAQ,GAAA,EAAK,MAAA,KAAS,MAAA,CAAO,MAAA,CAAO,gBAAA,SAAyB,UAAA,CAAW,UAAA;;;;;;;;cAkBxE,KAAA,GACX,GAAA,EAAK,MAAA,KACJ,KAAA,CAAM,KAAA,CAAM,SAAA,GAAY,aAAA,SAAsB,UAAA,CAAW,UAAA;AAAA;;;cC1N/C,cAAA,EAAc,MAAA,CAAA,KAAA,WAAA,MAAA,CAAA,MAAA;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KACf,cAAA,UAAwB,cAAA,CAAe,IAAA;AAAA,cA6BtC,qBAAA,EAAqB,MAAA,CAAA,MAAA;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KActB,qBAAA,UAA+B,qBAAA,CAAsB,IAAA;;cA+DpD,YAAA,GAAgB,KAAA,EAAO,aAAA,CAAc,IAAA,MAAQ,aAAA,CAAc,MAAA;AAAA,cAoB3D,cAAA,GAAkB,IAAA,EAAM,cAAA,KAAiB,IAAA;AAAA,cA6DzC,iBAAA,GAAqB,OAAA,EAAS,qBAAA,KAAwB,IAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
import { t as __exportAll } from "./chunk-CfYAbeIz.mjs";
|
|
2
|
+
import { Context, Effect, Layer, Match, Option, Schema, Stream } from "effect";
|
|
3
|
+
import { HttpClient, HttpClientRequest } from "effect/unstable/http";
|
|
4
|
+
import * as AiError from "@effect-uai/core/AiError";
|
|
5
|
+
import { matchType } from "@effect-uai/core/Match";
|
|
6
|
+
import { LanguageModel } from "@effect-uai/core/LanguageModel";
|
|
7
|
+
import * as SSE from "@effect-uai/core/SSE";
|
|
8
|
+
//#region src/codec.ts
|
|
9
|
+
var codec_exports = /* @__PURE__ */ __exportAll({
|
|
10
|
+
WireOutputItem: () => WireOutputItem,
|
|
11
|
+
WireResponseCompleted: () => WireResponseCompleted,
|
|
12
|
+
itemsToInput: () => itemsToInput,
|
|
13
|
+
turnFromCompleted: () => turnFromCompleted,
|
|
14
|
+
wireItemToItem: () => wireItemToItem
|
|
15
|
+
});
|
|
16
|
+
const WireUrlCitation = Schema.Struct({
|
|
17
|
+
type: Schema.Literal("url_citation"),
|
|
18
|
+
url: Schema.String,
|
|
19
|
+
start_index: Schema.Number,
|
|
20
|
+
end_index: Schema.Number,
|
|
21
|
+
title: Schema.String
|
|
22
|
+
});
|
|
23
|
+
const WireFileCitation = Schema.Struct({
|
|
24
|
+
type: Schema.Literal("file_citation"),
|
|
25
|
+
file_id: Schema.String,
|
|
26
|
+
index: Schema.Number
|
|
27
|
+
});
|
|
28
|
+
const WireContainerFileCitation = Schema.Struct({
|
|
29
|
+
type: Schema.Literal("container_file_citation"),
|
|
30
|
+
container_id: Schema.String,
|
|
31
|
+
file_id: Schema.String,
|
|
32
|
+
start_index: Schema.Number,
|
|
33
|
+
end_index: Schema.Number
|
|
34
|
+
});
|
|
35
|
+
const WireFilePath = Schema.Struct({
|
|
36
|
+
type: Schema.Literal("file_path"),
|
|
37
|
+
file_id: Schema.String,
|
|
38
|
+
index: Schema.Number
|
|
39
|
+
});
|
|
40
|
+
const WireAnnotation = Schema.Union([
|
|
41
|
+
WireUrlCitation,
|
|
42
|
+
WireFileCitation,
|
|
43
|
+
WireContainerFileCitation,
|
|
44
|
+
WireFilePath
|
|
45
|
+
]);
|
|
46
|
+
const WireOutputTextContent = Schema.Struct({
|
|
47
|
+
type: Schema.Literal("output_text"),
|
|
48
|
+
text: Schema.String,
|
|
49
|
+
annotations: Schema.optional(Schema.NullOr(Schema.Array(WireAnnotation)))
|
|
50
|
+
});
|
|
51
|
+
const WireRefusalContent = Schema.Struct({
|
|
52
|
+
type: Schema.Literal("refusal"),
|
|
53
|
+
refusal: Schema.String
|
|
54
|
+
});
|
|
55
|
+
const WireMessageContent = Schema.Union([WireOutputTextContent, WireRefusalContent]);
|
|
56
|
+
const WireSummaryText = Schema.Struct({
|
|
57
|
+
type: Schema.Literal("summary_text"),
|
|
58
|
+
text: Schema.String
|
|
59
|
+
});
|
|
60
|
+
const WireMessage = Schema.Struct({
|
|
61
|
+
type: Schema.Literal("message"),
|
|
62
|
+
id: Schema.optional(Schema.String),
|
|
63
|
+
role: Schema.Literal("assistant"),
|
|
64
|
+
content: Schema.Array(WireMessageContent)
|
|
65
|
+
});
|
|
66
|
+
const WireFunctionCall = Schema.Struct({
|
|
67
|
+
type: Schema.Literal("function_call"),
|
|
68
|
+
id: Schema.optional(Schema.String),
|
|
69
|
+
call_id: Schema.String,
|
|
70
|
+
name: Schema.String,
|
|
71
|
+
arguments: Schema.String
|
|
72
|
+
});
|
|
73
|
+
const WireReasoning = Schema.Struct({
|
|
74
|
+
type: Schema.Literal("reasoning"),
|
|
75
|
+
id: Schema.optional(Schema.String),
|
|
76
|
+
summary: Schema.optional(Schema.Array(WireSummaryText)),
|
|
77
|
+
encrypted_content: Schema.optional(Schema.String)
|
|
78
|
+
});
|
|
79
|
+
const WireOutputItem = Schema.Union([
|
|
80
|
+
WireMessage,
|
|
81
|
+
WireFunctionCall,
|
|
82
|
+
WireReasoning
|
|
83
|
+
]);
|
|
84
|
+
const WireInputTokensDetails = Schema.Struct({ cached_tokens: Schema.optional(Schema.NullOr(Schema.Number)) });
|
|
85
|
+
const WireOutputTokensDetails = Schema.Struct({ reasoning_tokens: Schema.optional(Schema.NullOr(Schema.Number)) });
|
|
86
|
+
const WireUsage = Schema.Struct({
|
|
87
|
+
input_tokens: Schema.optional(Schema.Number),
|
|
88
|
+
output_tokens: Schema.optional(Schema.Number),
|
|
89
|
+
total_tokens: Schema.optional(Schema.Number),
|
|
90
|
+
input_tokens_details: Schema.optional(Schema.NullOr(WireInputTokensDetails)),
|
|
91
|
+
output_tokens_details: Schema.optional(Schema.NullOr(WireOutputTokensDetails))
|
|
92
|
+
});
|
|
93
|
+
const WireResponseError = Schema.Struct({
|
|
94
|
+
code: Schema.optional(Schema.NullOr(Schema.String)),
|
|
95
|
+
message: Schema.optional(Schema.NullOr(Schema.String))
|
|
96
|
+
});
|
|
97
|
+
const WireResponseCompleted = Schema.Struct({
|
|
98
|
+
id: Schema.optional(Schema.NullOr(Schema.String)),
|
|
99
|
+
status: Schema.optional(Schema.NullOr(Schema.String)),
|
|
100
|
+
output: Schema.optional(Schema.NullOr(Schema.Array(WireOutputItem))),
|
|
101
|
+
usage: Schema.optional(Schema.NullOr(WireUsage)),
|
|
102
|
+
incomplete_details: Schema.optional(Schema.NullOr(Schema.Struct({ reason: Schema.optional(Schema.NullOr(Schema.String)) }))),
|
|
103
|
+
error: Schema.optional(Schema.NullOr(WireResponseError))
|
|
104
|
+
});
|
|
105
|
+
/**
|
|
106
|
+
* If an item carries `providerData` from a prior OpenAI turn, re-emit it
|
|
107
|
+
* verbatim - preserves `encrypted_content`, item ids, and any field this
|
|
108
|
+
* codec doesn't model.
|
|
109
|
+
*/
|
|
110
|
+
const passthrough = (item) => item.providerData !== void 0 && typeof item.providerData === "object" && item.providerData !== null ? item.providerData : void 0;
|
|
111
|
+
const contentBlockToInput = Match.type().pipe(matchType("input_text", (b) => ({
|
|
112
|
+
type: "input_text",
|
|
113
|
+
text: b.text
|
|
114
|
+
})), matchType("input_image", (b) => ({
|
|
115
|
+
type: "input_image",
|
|
116
|
+
image_url: b.source._tag === "url" ? b.source.url : `data:${b.source.media_type};base64,${b.source.data}`
|
|
117
|
+
})), matchType("output_text", (b) => ({
|
|
118
|
+
type: "output_text",
|
|
119
|
+
text: b.text
|
|
120
|
+
})), matchType("refusal", (b) => ({
|
|
121
|
+
type: "refusal",
|
|
122
|
+
refusal: b.text
|
|
123
|
+
})), Match.exhaustive);
|
|
124
|
+
const itemToInput = (item) => passthrough(item) ?? Match.value(item).pipe(matchType("message", (m) => ({
|
|
125
|
+
type: "message",
|
|
126
|
+
role: m.role,
|
|
127
|
+
content: m.content.map(contentBlockToInput)
|
|
128
|
+
})), matchType("function_call", (f) => ({
|
|
129
|
+
type: "function_call",
|
|
130
|
+
call_id: f.call_id,
|
|
131
|
+
name: f.name,
|
|
132
|
+
arguments: f.arguments
|
|
133
|
+
})), matchType("function_call_output", (o) => ({
|
|
134
|
+
type: "function_call_output",
|
|
135
|
+
call_id: o.call_id,
|
|
136
|
+
output: o.output
|
|
137
|
+
})), matchType("reasoning", (r) => ({
|
|
138
|
+
type: "reasoning",
|
|
139
|
+
...r.id !== void 0 && { id: r.id },
|
|
140
|
+
...r.summary !== void 0 && { summary: [{
|
|
141
|
+
type: "summary_text",
|
|
142
|
+
text: r.summary
|
|
143
|
+
}] },
|
|
144
|
+
...r.signature !== void 0 && { encrypted_content: r.signature }
|
|
145
|
+
})), Match.exhaustive);
|
|
146
|
+
/** Convert our `Item[]` history into the Responses API `input` array. */
|
|
147
|
+
const itemsToInput = (items) => items.map(itemToInput);
|
|
148
|
+
const wireMessageContentToBlock = Match.type().pipe(matchType("output_text", (c) => ({
|
|
149
|
+
type: "output_text",
|
|
150
|
+
text: c.text,
|
|
151
|
+
...c.annotations !== void 0 && c.annotations !== null && { annotations: c.annotations }
|
|
152
|
+
})), matchType("refusal", (c) => ({
|
|
153
|
+
type: "refusal",
|
|
154
|
+
text: c.refusal
|
|
155
|
+
})), Match.exhaustive);
|
|
156
|
+
const wireItemToItem = (wire) => Match.value(wire).pipe(matchType("message", (m) => ({
|
|
157
|
+
type: "message",
|
|
158
|
+
role: m.role,
|
|
159
|
+
content: m.content.map(wireMessageContentToBlock),
|
|
160
|
+
providerData: m
|
|
161
|
+
})), matchType("function_call", (f) => ({
|
|
162
|
+
type: "function_call",
|
|
163
|
+
call_id: f.call_id,
|
|
164
|
+
name: f.name,
|
|
165
|
+
arguments: f.arguments,
|
|
166
|
+
providerData: f
|
|
167
|
+
})), matchType("reasoning", (r) => ({
|
|
168
|
+
type: "reasoning",
|
|
169
|
+
...r.id !== void 0 && { id: r.id },
|
|
170
|
+
...r.summary !== void 0 && { summary: r.summary.map((s) => s.text).join("\n") },
|
|
171
|
+
...r.encrypted_content !== void 0 && { signature: r.encrypted_content },
|
|
172
|
+
providerData: r
|
|
173
|
+
})), Match.exhaustive);
|
|
174
|
+
const reasonToStop = Match.type().pipe(Match.when("max_output_tokens", () => "max_tokens"), Match.when("refusal", () => "refusal"), Match.when("content_filter", () => "content_filter"), Match.when("max_tool_calls", () => "max_tool_calls"), Match.option);
|
|
175
|
+
const hasRefusalContent = (payload) => (payload.output ?? []).some((i) => i.type === "message" && i.content.some((c) => c.type === "refusal"));
|
|
176
|
+
const stopReasonFromCompleted = (payload) => Option.match(reasonToStop(payload.incomplete_details?.reason ?? ""), {
|
|
177
|
+
onSome: (s) => s,
|
|
178
|
+
onNone: () => hasRefusalContent(payload) ? "refusal" : (payload.output ?? []).some((i) => i.type === "function_call") ? "tool_calls" : "stop"
|
|
179
|
+
});
|
|
180
|
+
const cachedTokens = (payload) => payload.usage?.input_tokens_details?.cached_tokens ?? void 0;
|
|
181
|
+
const reasoningTokens = (payload) => payload.usage?.output_tokens_details?.reasoning_tokens ?? void 0;
|
|
182
|
+
const turnFromCompleted = (payload) => {
|
|
183
|
+
const cached = cachedTokens(payload);
|
|
184
|
+
const reasoning = reasoningTokens(payload);
|
|
185
|
+
return {
|
|
186
|
+
items: (payload.output ?? []).map(wireItemToItem),
|
|
187
|
+
usage: {
|
|
188
|
+
...payload.usage?.input_tokens !== void 0 && { input_tokens: payload.usage.input_tokens },
|
|
189
|
+
...payload.usage?.output_tokens !== void 0 && { output_tokens: payload.usage.output_tokens },
|
|
190
|
+
...payload.usage?.total_tokens !== void 0 && { total_tokens: payload.usage.total_tokens },
|
|
191
|
+
...cached !== void 0 && { input_tokens_details: { cached_tokens: cached } },
|
|
192
|
+
...reasoning !== void 0 && { output_tokens_details: { reasoning_tokens: reasoning } }
|
|
193
|
+
},
|
|
194
|
+
stop_reason: stopReasonFromCompleted(payload)
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
//#endregion
|
|
198
|
+
//#region src/streamEvents.ts
|
|
199
|
+
var streamEvents_exports = /* @__PURE__ */ __exportAll({
|
|
200
|
+
KnownProviderEvent: () => KnownProviderEvent,
|
|
201
|
+
ProviderEvent: () => ProviderEvent,
|
|
202
|
+
eventToDeltas: () => eventToDeltas,
|
|
203
|
+
makeCallIdLookup: () => makeCallIdLookup
|
|
204
|
+
});
|
|
205
|
+
const OutputItemAdded = Schema.Struct({
|
|
206
|
+
type: Schema.Literal("response.output_item.added"),
|
|
207
|
+
item: WireOutputItem
|
|
208
|
+
});
|
|
209
|
+
const OutputTextDelta = Schema.Struct({
|
|
210
|
+
type: Schema.Literal("response.output_text.delta"),
|
|
211
|
+
delta: Schema.String
|
|
212
|
+
});
|
|
213
|
+
const FunctionArgsDelta = Schema.Struct({
|
|
214
|
+
type: Schema.Literal("response.function_call_arguments.delta"),
|
|
215
|
+
item_id: Schema.String,
|
|
216
|
+
delta: Schema.String
|
|
217
|
+
});
|
|
218
|
+
const ReasoningTextDelta = Schema.Struct({
|
|
219
|
+
type: Schema.Literal("response.reasoning_text.delta"),
|
|
220
|
+
delta: Schema.String
|
|
221
|
+
});
|
|
222
|
+
const ReasoningSummaryDelta = Schema.Struct({
|
|
223
|
+
type: Schema.Literal("response.reasoning_summary_text.delta"),
|
|
224
|
+
delta: Schema.String
|
|
225
|
+
});
|
|
226
|
+
const RefusalDelta = Schema.Struct({
|
|
227
|
+
type: Schema.Literal("response.refusal.delta"),
|
|
228
|
+
delta: Schema.String
|
|
229
|
+
});
|
|
230
|
+
const RefusalDone = Schema.Struct({
|
|
231
|
+
type: Schema.Literal("response.refusal.done"),
|
|
232
|
+
refusal: Schema.optional(Schema.String)
|
|
233
|
+
});
|
|
234
|
+
const Completed = Schema.Struct({
|
|
235
|
+
type: Schema.Literal("response.completed"),
|
|
236
|
+
response: WireResponseCompleted
|
|
237
|
+
});
|
|
238
|
+
const Incomplete = Schema.Struct({
|
|
239
|
+
type: Schema.Literal("response.incomplete"),
|
|
240
|
+
response: WireResponseCompleted
|
|
241
|
+
});
|
|
242
|
+
const Failed = Schema.Struct({
|
|
243
|
+
type: Schema.Literal("response.failed"),
|
|
244
|
+
response: WireResponseCompleted
|
|
245
|
+
});
|
|
246
|
+
const ErrorEvent = Schema.Struct({
|
|
247
|
+
type: Schema.Literal("error"),
|
|
248
|
+
message: Schema.optional(Schema.String),
|
|
249
|
+
code: Schema.optional(Schema.String)
|
|
250
|
+
});
|
|
251
|
+
/**
|
|
252
|
+
* Catch-all variant for wire events that fail to decode against any known
|
|
253
|
+
* schema, plus events that fail to JSON-parse. The decoder never produces
|
|
254
|
+
* this directly - it's synthesized by `sseEventToProviderEvent` when
|
|
255
|
+
* `decodeKnown` fails.
|
|
256
|
+
*/
|
|
257
|
+
const Unknown = Schema.Struct({
|
|
258
|
+
type: Schema.Literal("_unknown"),
|
|
259
|
+
raw: Schema.Unknown
|
|
260
|
+
});
|
|
261
|
+
/**
|
|
262
|
+
* Internal: union of variants we actually know how to decode from the wire.
|
|
263
|
+
* Used as the decode target; failures are caught and re-emitted as `Unknown`.
|
|
264
|
+
*/
|
|
265
|
+
const KnownProviderEvent = Schema.Union([
|
|
266
|
+
OutputItemAdded,
|
|
267
|
+
OutputTextDelta,
|
|
268
|
+
FunctionArgsDelta,
|
|
269
|
+
ReasoningTextDelta,
|
|
270
|
+
ReasoningSummaryDelta,
|
|
271
|
+
RefusalDelta,
|
|
272
|
+
RefusalDone,
|
|
273
|
+
Completed,
|
|
274
|
+
Incomplete,
|
|
275
|
+
Failed,
|
|
276
|
+
ErrorEvent
|
|
277
|
+
]);
|
|
278
|
+
/**
|
|
279
|
+
* Public: every event the native stream can emit. Discriminated on `type`.
|
|
280
|
+
* The `_unknown` branch closes the cardinality so downstream `Match.exhaustive`
|
|
281
|
+
* cannot silently miss a wire event we didn't model.
|
|
282
|
+
*/
|
|
283
|
+
const ProviderEvent = Schema.Union([
|
|
284
|
+
OutputItemAdded,
|
|
285
|
+
OutputTextDelta,
|
|
286
|
+
FunctionArgsDelta,
|
|
287
|
+
ReasoningTextDelta,
|
|
288
|
+
ReasoningSummaryDelta,
|
|
289
|
+
RefusalDelta,
|
|
290
|
+
RefusalDone,
|
|
291
|
+
Completed,
|
|
292
|
+
Incomplete,
|
|
293
|
+
Failed,
|
|
294
|
+
ErrorEvent,
|
|
295
|
+
Unknown
|
|
296
|
+
]);
|
|
297
|
+
const makeCallIdLookup = () => {
|
|
298
|
+
const map = /* @__PURE__ */ new Map();
|
|
299
|
+
return {
|
|
300
|
+
resolve: (id) => map.get(id),
|
|
301
|
+
remember: (id, callId) => {
|
|
302
|
+
map.set(id, callId);
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
};
|
|
306
|
+
/**
|
|
307
|
+
* Translate a decoded provider event into zero-or-more `TurnEvent`s.
|
|
308
|
+
* Mutates `lookup` to record `(item_id → call_id)` from `output_item.added`
|
|
309
|
+
* so subsequent `function_call_arguments.delta` events can be tagged with
|
|
310
|
+
* the right call id.
|
|
311
|
+
*/
|
|
312
|
+
const eventToDeltas = (event, lookup) => Match.value(event).pipe(matchType("response.output_item.added", ({ item }) => {
|
|
313
|
+
if (item.type !== "function_call") return [];
|
|
314
|
+
if (item.id !== void 0) lookup.remember(item.id, item.call_id);
|
|
315
|
+
return [{
|
|
316
|
+
type: "tool_call_start",
|
|
317
|
+
call_id: item.call_id,
|
|
318
|
+
name: item.name
|
|
319
|
+
}];
|
|
320
|
+
}), matchType("response.output_text.delta", ({ delta }) => [{
|
|
321
|
+
type: "text_delta",
|
|
322
|
+
text: delta
|
|
323
|
+
}]), matchType("response.function_call_arguments.delta", ({ item_id, delta }) => {
|
|
324
|
+
const call_id = lookup.resolve(item_id);
|
|
325
|
+
return call_id === void 0 ? [] : [{
|
|
326
|
+
type: "tool_call_args_delta",
|
|
327
|
+
call_id,
|
|
328
|
+
delta
|
|
329
|
+
}];
|
|
330
|
+
}), matchType("response.reasoning_text.delta", ({ delta }) => [{
|
|
331
|
+
type: "reasoning_delta",
|
|
332
|
+
text: delta,
|
|
333
|
+
kind: "trace"
|
|
334
|
+
}]), matchType("response.reasoning_summary_text.delta", ({ delta }) => [{
|
|
335
|
+
type: "reasoning_delta",
|
|
336
|
+
text: delta,
|
|
337
|
+
kind: "summary"
|
|
338
|
+
}]), matchType("response.refusal.delta", ({ delta }) => [{
|
|
339
|
+
type: "refusal_delta",
|
|
340
|
+
text: delta
|
|
341
|
+
}]), matchType("response.refusal.done", () => []), matchType("response.completed", ({ response }) => [{
|
|
342
|
+
type: "turn_complete",
|
|
343
|
+
turn: turnFromCompleted(response)
|
|
344
|
+
}]), matchType("response.incomplete", ({ response }) => [{
|
|
345
|
+
type: "turn_complete",
|
|
346
|
+
turn: turnFromCompleted(response)
|
|
347
|
+
}]), matchType("response.failed", () => []), matchType("error", () => []), matchType("_unknown", () => []), Match.exhaustive);
|
|
348
|
+
//#endregion
|
|
349
|
+
//#region src/Responses.ts
|
|
350
|
+
/**
|
|
351
|
+
* Provider-typed service tag. Yield this when you want Responses-specific
|
|
352
|
+
* options (`reasoning.effort`, `store`, `previousResponseId`) at the call
|
|
353
|
+
* site. Yield the generic `LanguageModel` tag for provider-portable code -
|
|
354
|
+
* both are registered by `layer`.
|
|
355
|
+
*/
|
|
356
|
+
var Responses = class extends Context.Service()("@betalyra/effect-uai/providers/responses/Responses") {};
|
|
357
|
+
const jsonSchemaFormat = (format) => ({
|
|
358
|
+
type: "json_schema",
|
|
359
|
+
name: format.name,
|
|
360
|
+
schema: format.schema["~standard"].jsonSchema.input({ target: "draft-2020-12" }),
|
|
361
|
+
...format.description !== void 0 && { description: format.description },
|
|
362
|
+
...format.strict !== void 0 && { strict: format.strict }
|
|
363
|
+
});
|
|
364
|
+
const buildText = (request) => {
|
|
365
|
+
const format = request.structured !== void 0 ? jsonSchemaFormat(request.structured) : request.responseFormat;
|
|
366
|
+
const text = {};
|
|
367
|
+
if (format !== void 0) text.format = format;
|
|
368
|
+
if (request.verbosity !== void 0) text.verbosity = request.verbosity;
|
|
369
|
+
return Object.keys(text).length === 0 ? void 0 : text;
|
|
370
|
+
};
|
|
371
|
+
const buildBody = (request) => {
|
|
372
|
+
const text = buildText(request);
|
|
373
|
+
return {
|
|
374
|
+
model: request.model,
|
|
375
|
+
input: itemsToInput(request.history),
|
|
376
|
+
stream: true,
|
|
377
|
+
...request.tools !== void 0 && request.tools.length > 0 && { tools: request.tools.map((t) => ({
|
|
378
|
+
type: "function",
|
|
379
|
+
name: t.name,
|
|
380
|
+
description: t.description,
|
|
381
|
+
parameters: t.inputSchema,
|
|
382
|
+
...t.strict !== void 0 && { strict: t.strict }
|
|
383
|
+
})) },
|
|
384
|
+
...request.toolChoice !== void 0 && { tool_choice: request.toolChoice },
|
|
385
|
+
...request.temperature !== void 0 && { temperature: request.temperature },
|
|
386
|
+
...request.maxOutputTokens !== void 0 && { max_output_tokens: request.maxOutputTokens },
|
|
387
|
+
...request.reasoning !== void 0 && { reasoning: request.reasoning },
|
|
388
|
+
...request.store !== void 0 && { store: request.store },
|
|
389
|
+
...request.previousResponseId !== void 0 && { previous_response_id: request.previousResponseId },
|
|
390
|
+
...request.instructions !== void 0 && { instructions: request.instructions },
|
|
391
|
+
...request.topP !== void 0 && { top_p: request.topP },
|
|
392
|
+
...request.parallelToolCalls !== void 0 && { parallel_tool_calls: request.parallelToolCalls },
|
|
393
|
+
...request.metadata !== void 0 && { metadata: request.metadata },
|
|
394
|
+
...request.user !== void 0 && { user: request.user },
|
|
395
|
+
...request.safetyIdentifier !== void 0 && { safety_identifier: request.safetyIdentifier },
|
|
396
|
+
...request.promptCacheKey !== void 0 && { prompt_cache_key: request.promptCacheKey },
|
|
397
|
+
...request.truncation !== void 0 && { truncation: request.truncation },
|
|
398
|
+
...text !== void 0 && { text }
|
|
399
|
+
};
|
|
400
|
+
};
|
|
401
|
+
const decodeKnown = Schema.decodeUnknownEffect(KnownProviderEvent);
|
|
402
|
+
const makeUnknown = (raw) => ({
|
|
403
|
+
type: "_unknown",
|
|
404
|
+
raw
|
|
405
|
+
});
|
|
406
|
+
/**
|
|
407
|
+
* Lift events that carry a terminal failure signal (`error`,
|
|
408
|
+
* `response.failed`) to a typed `AiError`. Other events are not failures
|
|
409
|
+
* and produce `Option.none`.
|
|
410
|
+
*/
|
|
411
|
+
const eventToError = Match.type().pipe(matchType("error", (e) => new AiError.Unavailable({
|
|
412
|
+
provider: "responses",
|
|
413
|
+
raw: e
|
|
414
|
+
})), matchType("response.failed", (e) => {
|
|
415
|
+
const code = e.response.error?.code;
|
|
416
|
+
const message = e.response.error?.message;
|
|
417
|
+
return new AiError.GenerationFailed({
|
|
418
|
+
provider: "responses",
|
|
419
|
+
...code !== void 0 && code !== null && { code },
|
|
420
|
+
...message !== void 0 && message !== null && { message },
|
|
421
|
+
raw: e
|
|
422
|
+
});
|
|
423
|
+
}), Match.option);
|
|
424
|
+
const sseEventToProviderEvent = (ev) => Effect.try({
|
|
425
|
+
try: () => JSON.parse(ev.data),
|
|
426
|
+
catch: () => ev.data
|
|
427
|
+
}).pipe(Effect.flatMap((parsed) => decodeKnown(parsed).pipe(Effect.orElseSucceed(() => makeUnknown(parsed)))), Effect.orElseSucceed(() => makeUnknown(ev.data)));
|
|
428
|
+
const httpStatusError = (status, body) => {
|
|
429
|
+
const provider = "responses";
|
|
430
|
+
const raw = body;
|
|
431
|
+
if (status === 429) return new AiError.RateLimited({
|
|
432
|
+
provider,
|
|
433
|
+
raw
|
|
434
|
+
});
|
|
435
|
+
if (status === 408 || status === 504) return new AiError.Timeout({
|
|
436
|
+
provider,
|
|
437
|
+
raw
|
|
438
|
+
});
|
|
439
|
+
if (status === 401) return new AiError.AuthFailed({
|
|
440
|
+
provider,
|
|
441
|
+
subtype: "auth",
|
|
442
|
+
raw
|
|
443
|
+
});
|
|
444
|
+
if (status === 403) return new AiError.AuthFailed({
|
|
445
|
+
provider,
|
|
446
|
+
subtype: "permission",
|
|
447
|
+
raw
|
|
448
|
+
});
|
|
449
|
+
if (status === 402) return new AiError.AuthFailed({
|
|
450
|
+
provider,
|
|
451
|
+
subtype: "billing",
|
|
452
|
+
raw
|
|
453
|
+
});
|
|
454
|
+
if (status === 413) return new AiError.ContextLengthExceeded({
|
|
455
|
+
provider,
|
|
456
|
+
raw
|
|
457
|
+
});
|
|
458
|
+
if (status >= 500) return new AiError.Unavailable({
|
|
459
|
+
provider,
|
|
460
|
+
status,
|
|
461
|
+
raw
|
|
462
|
+
});
|
|
463
|
+
return new AiError.InvalidRequest({
|
|
464
|
+
provider,
|
|
465
|
+
raw
|
|
466
|
+
});
|
|
467
|
+
};
|
|
468
|
+
const buildNativeStream = (cfg) => {
|
|
469
|
+
const url = `${cfg.baseUrl ?? "https://api.openai.com/v1"}/responses`;
|
|
470
|
+
return (request) => Stream.unwrap(Effect.gen(function* () {
|
|
471
|
+
const client = yield* HttpClient.HttpClient;
|
|
472
|
+
const httpRequest = HttpClientRequest.post(url).pipe(HttpClientRequest.bearerToken(cfg.apiKey), HttpClientRequest.bodyJsonUnsafe(buildBody(request)), HttpClientRequest.accept("text/event-stream"));
|
|
473
|
+
const response = yield* client.execute(httpRequest).pipe(Effect.mapError((cause) => new AiError.Unavailable({
|
|
474
|
+
provider: "responses",
|
|
475
|
+
raw: cause
|
|
476
|
+
})));
|
|
477
|
+
if (response.status >= 400) {
|
|
478
|
+
const body = yield* response.text.pipe(Effect.orElseSucceed(() => ""));
|
|
479
|
+
return Stream.fail(httpStatusError(response.status, body));
|
|
480
|
+
}
|
|
481
|
+
return response.stream.pipe(Stream.mapError((cause) => new AiError.Unavailable({
|
|
482
|
+
provider: "responses",
|
|
483
|
+
raw: cause
|
|
484
|
+
})), SSE.fromBytes, Stream.mapEffect(sseEventToProviderEvent), Stream.flatMap((event) => Option.match(eventToError(event), {
|
|
485
|
+
onNone: () => Stream.succeed(event),
|
|
486
|
+
onSome: Stream.fail
|
|
487
|
+
})));
|
|
488
|
+
}));
|
|
489
|
+
};
|
|
490
|
+
/**
|
|
491
|
+
* Project a stream of native `ProviderEvent`s into canonical `TurnEvent`s.
|
|
492
|
+
* Threads a fresh `CallIdLookup` per stream so `function_call_arguments.delta`
|
|
493
|
+
* can be tagged with the right `call_id`.
|
|
494
|
+
*/
|
|
495
|
+
const toCanonical = (s) => Stream.unwrap(Effect.sync(() => {
|
|
496
|
+
const lookup = makeCallIdLookup();
|
|
497
|
+
return s.pipe(Stream.flatMap((event) => Stream.fromIterable(eventToDeltas(event, lookup))));
|
|
498
|
+
}));
|
|
499
|
+
/**
|
|
500
|
+
* Build a `ResponsesService` value. For Layer-based setup, prefer `layer`.
|
|
501
|
+
*/
|
|
502
|
+
const make = (cfg) => Effect.map(HttpClient.HttpClient.asEffect(), (client) => {
|
|
503
|
+
const streamNative = (request) => buildNativeStream(cfg)(request).pipe(Stream.provideService(HttpClient.HttpClient, client));
|
|
504
|
+
return {
|
|
505
|
+
streamNative,
|
|
506
|
+
streamTurn: (request) => toCanonical(streamNative(request)),
|
|
507
|
+
toCanonical
|
|
508
|
+
};
|
|
509
|
+
});
|
|
510
|
+
/**
|
|
511
|
+
* Layer that registers both the provider-specific `Responses` tag and the
|
|
512
|
+
* generic `LanguageModel` tag, sharing one underlying implementation.
|
|
513
|
+
*
|
|
514
|
+
* The generic tag accepts `CommonRequest`; the typed tag accepts the full
|
|
515
|
+
* `ResponsesRequest` surface.
|
|
516
|
+
*/
|
|
517
|
+
const layer = (cfg) => {
|
|
518
|
+
const typed = Layer.effect(Responses, make(cfg));
|
|
519
|
+
const generic = Layer.effect(LanguageModel, Effect.map(make(cfg), (s) => ({ streamTurn: (request) => s.streamTurn(request) })));
|
|
520
|
+
return Layer.merge(typed, generic);
|
|
521
|
+
};
|
|
522
|
+
//#endregion
|
|
523
|
+
export { Responses, codec_exports as codec, layer, make, streamEvents_exports as streamEvents, toCanonical };
|
|
524
|
+
|
|
525
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/codec.ts","../src/streamEvents.ts","../src/Responses.ts"],"sourcesContent":["import { Match, Option, Schema } from \"effect\"\nimport type { ContentBlock, Item } from \"@effect-uai/core/Items\"\nimport { matchType } from \"@effect-uai/core/Match\"\nimport type { Turn } from \"@effect-uai/core/Turn\"\n\n// ---------------------------------------------------------------------------\n// Wire schemas - minimal subset of the Responses API output we consume.\n// Reference: https://www.openresponses.org/specification\n// ---------------------------------------------------------------------------\n\nconst WireUrlCitation = Schema.Struct({\n type: Schema.Literal(\"url_citation\"),\n url: Schema.String,\n start_index: Schema.Number,\n end_index: Schema.Number,\n title: Schema.String,\n})\n\nconst WireFileCitation = Schema.Struct({\n type: Schema.Literal(\"file_citation\"),\n file_id: Schema.String,\n index: Schema.Number,\n})\n\nconst WireContainerFileCitation = Schema.Struct({\n type: Schema.Literal(\"container_file_citation\"),\n container_id: Schema.String,\n file_id: Schema.String,\n start_index: Schema.Number,\n end_index: Schema.Number,\n})\n\nconst WireFilePath = Schema.Struct({\n type: Schema.Literal(\"file_path\"),\n file_id: Schema.String,\n index: Schema.Number,\n})\n\nconst WireAnnotation = Schema.Union([\n WireUrlCitation,\n WireFileCitation,\n WireContainerFileCitation,\n WireFilePath,\n])\n\nconst WireOutputTextContent = Schema.Struct({\n type: Schema.Literal(\"output_text\"),\n text: Schema.String,\n annotations: Schema.optional(Schema.NullOr(Schema.Array(WireAnnotation))),\n})\n\nconst WireRefusalContent = Schema.Struct({\n type: Schema.Literal(\"refusal\"),\n refusal: Schema.String,\n})\n\nconst WireMessageContent = Schema.Union([WireOutputTextContent, WireRefusalContent])\n\nconst WireSummaryText = Schema.Struct({\n type: Schema.Literal(\"summary_text\"),\n text: Schema.String,\n})\n\nconst WireMessage = Schema.Struct({\n type: Schema.Literal(\"message\"),\n id: Schema.optional(Schema.String),\n role: Schema.Literal(\"assistant\"),\n content: Schema.Array(WireMessageContent),\n})\n\nconst WireFunctionCall = Schema.Struct({\n type: Schema.Literal(\"function_call\"),\n id: Schema.optional(Schema.String),\n call_id: Schema.String,\n name: Schema.String,\n arguments: Schema.String,\n})\n\nconst WireReasoning = Schema.Struct({\n type: Schema.Literal(\"reasoning\"),\n id: Schema.optional(Schema.String),\n summary: Schema.optional(Schema.Array(WireSummaryText)),\n encrypted_content: Schema.optional(Schema.String),\n})\n\nexport const WireOutputItem = Schema.Union([WireMessage, WireFunctionCall, WireReasoning])\nexport type WireOutputItem = typeof WireOutputItem.Type\n\nconst WireInputTokensDetails = Schema.Struct({\n cached_tokens: Schema.optional(Schema.NullOr(Schema.Number)),\n})\n\nconst WireOutputTokensDetails = Schema.Struct({\n reasoning_tokens: Schema.optional(Schema.NullOr(Schema.Number)),\n})\n\nconst WireUsage = Schema.Struct({\n input_tokens: Schema.optional(Schema.Number),\n output_tokens: Schema.optional(Schema.Number),\n total_tokens: Schema.optional(Schema.Number),\n input_tokens_details: Schema.optional(Schema.NullOr(WireInputTokensDetails)),\n output_tokens_details: Schema.optional(Schema.NullOr(WireOutputTokensDetails)),\n})\n\nconst WireResponseError = Schema.Struct({\n code: Schema.optional(Schema.NullOr(Schema.String)),\n message: Schema.optional(Schema.NullOr(Schema.String)),\n})\n\n// Many Responses-API fields are emitted as explicit `null` rather than\n// missing - `Schema.optional` alone (T | undefined) doesn't cover that, so\n// we lift each through `NullOr` first. Used by `response.completed`,\n// `response.incomplete`, and `response.failed` events; not every field is\n// populated on every event (e.g. `error` only on failed, `incomplete_details`\n// only on incomplete).\nexport const WireResponseCompleted = Schema.Struct({\n id: Schema.optional(Schema.NullOr(Schema.String)),\n status: Schema.optional(Schema.NullOr(Schema.String)),\n output: Schema.optional(Schema.NullOr(Schema.Array(WireOutputItem))),\n usage: Schema.optional(Schema.NullOr(WireUsage)),\n incomplete_details: Schema.optional(\n Schema.NullOr(\n Schema.Struct({\n reason: Schema.optional(Schema.NullOr(Schema.String)),\n }),\n ),\n ),\n error: Schema.optional(Schema.NullOr(WireResponseError)),\n})\nexport type WireResponseCompleted = typeof WireResponseCompleted.Type\n\n// ---------------------------------------------------------------------------\n// History → request body input\n// ---------------------------------------------------------------------------\n\n/**\n * If an item carries `providerData` from a prior OpenAI turn, re-emit it\n * verbatim - preserves `encrypted_content`, item ids, and any field this\n * codec doesn't model.\n */\nconst passthrough = (item: Item): Record<string, unknown> | undefined =>\n item.providerData !== undefined &&\n typeof item.providerData === \"object\" &&\n item.providerData !== null\n ? (item.providerData as Record<string, unknown>)\n : undefined\n\nconst contentBlockToInput = Match.type<ContentBlock>().pipe(\n matchType(\"input_text\", (b) => ({ type: \"input_text\", text: b.text })),\n matchType(\"input_image\", (b) => ({\n type: \"input_image\",\n image_url:\n b.source._tag === \"url\"\n ? b.source.url\n : `data:${b.source.media_type};base64,${b.source.data}`,\n })),\n matchType(\"output_text\", (b) => ({ type: \"output_text\", text: b.text })),\n matchType(\"refusal\", (b) => ({ type: \"refusal\", refusal: b.text })),\n Match.exhaustive,\n)\n\nconst itemToInput = (item: Item): Record<string, unknown> =>\n passthrough(item) ??\n Match.value(item).pipe(\n matchType(\"message\", (m) => ({\n type: \"message\",\n role: m.role,\n content: m.content.map(contentBlockToInput),\n })),\n matchType(\"function_call\", (f) => ({\n type: \"function_call\",\n call_id: f.call_id,\n name: f.name,\n arguments: f.arguments,\n })),\n matchType(\"function_call_output\", (o) => ({\n type: \"function_call_output\",\n call_id: o.call_id,\n output: o.output,\n })),\n matchType(\"reasoning\", (r) => ({\n type: \"reasoning\",\n ...(r.id !== undefined && { id: r.id }),\n ...(r.summary !== undefined && {\n summary: [{ type: \"summary_text\", text: r.summary }],\n }),\n ...(r.signature !== undefined && { encrypted_content: r.signature }),\n })),\n Match.exhaustive,\n )\n\n/** Convert our `Item[]` history into the Responses API `input` array. */\nexport const itemsToInput = (items: ReadonlyArray<Item>): ReadonlyArray<Record<string, unknown>> =>\n items.map(itemToInput)\n\n// ---------------------------------------------------------------------------\n// Wire output items → our Items\n// ---------------------------------------------------------------------------\n\nconst wireMessageContentToBlock = Match.type<typeof WireMessageContent.Type>().pipe(\n matchType(\n \"output_text\",\n (c): ContentBlock => ({\n type: \"output_text\",\n text: c.text,\n ...(c.annotations !== undefined && c.annotations !== null && { annotations: c.annotations }),\n }),\n ),\n matchType(\"refusal\", (c): ContentBlock => ({ type: \"refusal\", text: c.refusal })),\n Match.exhaustive,\n)\n\nexport const wireItemToItem = (wire: WireOutputItem): Item =>\n Match.value(wire).pipe(\n matchType(\"message\", (m) => ({\n type: \"message\" as const,\n role: m.role,\n content: m.content.map(wireMessageContentToBlock),\n providerData: m,\n })),\n matchType(\"function_call\", (f) => ({\n type: \"function_call\" as const,\n call_id: f.call_id,\n name: f.name,\n arguments: f.arguments,\n providerData: f,\n })),\n matchType(\"reasoning\", (r) => ({\n type: \"reasoning\" as const,\n ...(r.id !== undefined && { id: r.id }),\n ...(r.summary !== undefined && {\n summary: r.summary.map((s) => s.text).join(\"\\n\"),\n }),\n ...(r.encrypted_content !== undefined && { signature: r.encrypted_content }),\n providerData: r,\n })),\n Match.exhaustive,\n )\n\n// ---------------------------------------------------------------------------\n// response.completed → Turn\n// ---------------------------------------------------------------------------\n\nconst reasonToStop = Match.type<string>().pipe(\n Match.when(\"max_output_tokens\", () => \"max_tokens\" as const),\n Match.when(\"refusal\", () => \"refusal\" as const),\n Match.when(\"content_filter\", () => \"content_filter\" as const),\n Match.when(\"max_tool_calls\", () => \"max_tool_calls\" as const),\n Match.option,\n)\n\nconst hasRefusalContent = (payload: WireResponseCompleted): boolean =>\n (payload.output ?? []).some(\n (i) => i.type === \"message\" && i.content.some((c) => c.type === \"refusal\"),\n )\n\nconst stopReasonFromCompleted = (payload: WireResponseCompleted): Turn[\"stop_reason\"] =>\n Option.match(reasonToStop(payload.incomplete_details?.reason ?? \"\"), {\n onSome: (s) => s,\n onNone: () =>\n hasRefusalContent(payload)\n ? (\"refusal\" as const)\n : (payload.output ?? []).some((i) => i.type === \"function_call\")\n ? (\"tool_calls\" as const)\n : (\"stop\" as const),\n })\n\nconst cachedTokens = (payload: WireResponseCompleted): number | undefined =>\n payload.usage?.input_tokens_details?.cached_tokens ?? undefined\n\nconst reasoningTokens = (payload: WireResponseCompleted): number | undefined =>\n payload.usage?.output_tokens_details?.reasoning_tokens ?? undefined\n\nexport const turnFromCompleted = (payload: WireResponseCompleted): Turn => {\n const cached = cachedTokens(payload)\n const reasoning = reasoningTokens(payload)\n return {\n items: (payload.output ?? []).map(wireItemToItem),\n usage: {\n ...(payload.usage?.input_tokens !== undefined && {\n input_tokens: payload.usage.input_tokens,\n }),\n ...(payload.usage?.output_tokens !== undefined && {\n output_tokens: payload.usage.output_tokens,\n }),\n ...(payload.usage?.total_tokens !== undefined && {\n total_tokens: payload.usage.total_tokens,\n }),\n ...(cached !== undefined && {\n input_tokens_details: { cached_tokens: cached },\n }),\n ...(reasoning !== undefined && {\n output_tokens_details: { reasoning_tokens: reasoning },\n }),\n },\n stop_reason: stopReasonFromCompleted(payload),\n }\n}\n","import { Match, Schema } from \"effect\"\nimport { matchType } from \"@effect-uai/core/Match\"\nimport type { TurnEvent } from \"@effect-uai/core/Turn\"\nimport { WireOutputItem, WireResponseCompleted, turnFromCompleted } from \"./codec.js\"\n\n// ---------------------------------------------------------------------------\n// Schemas for the SSE event payloads we care about. The Responses API ships\n// many event types; we model only the ones that map to a `TurnEvent`. All\n// others are silently ignored at the dispatch site.\n// ---------------------------------------------------------------------------\n\nconst OutputItemAdded = Schema.Struct({\n type: Schema.Literal(\"response.output_item.added\"),\n item: WireOutputItem,\n})\n\nconst OutputTextDelta = Schema.Struct({\n type: Schema.Literal(\"response.output_text.delta\"),\n delta: Schema.String,\n})\n\nconst FunctionArgsDelta = Schema.Struct({\n type: Schema.Literal(\"response.function_call_arguments.delta\"),\n item_id: Schema.String,\n delta: Schema.String,\n})\n\nconst ReasoningTextDelta = Schema.Struct({\n type: Schema.Literal(\"response.reasoning_text.delta\"),\n delta: Schema.String,\n})\n\nconst ReasoningSummaryDelta = Schema.Struct({\n type: Schema.Literal(\"response.reasoning_summary_text.delta\"),\n delta: Schema.String,\n})\n\nconst RefusalDelta = Schema.Struct({\n type: Schema.Literal(\"response.refusal.delta\"),\n delta: Schema.String,\n})\n\nconst RefusalDone = Schema.Struct({\n type: Schema.Literal(\"response.refusal.done\"),\n refusal: Schema.optional(Schema.String),\n})\n\nconst Completed = Schema.Struct({\n type: Schema.Literal(\"response.completed\"),\n response: WireResponseCompleted,\n})\n\nconst Incomplete = Schema.Struct({\n type: Schema.Literal(\"response.incomplete\"),\n response: WireResponseCompleted,\n})\n\nconst Failed = Schema.Struct({\n type: Schema.Literal(\"response.failed\"),\n response: WireResponseCompleted,\n})\n\nconst ErrorEvent = Schema.Struct({\n type: Schema.Literal(\"error\"),\n message: Schema.optional(Schema.String),\n code: Schema.optional(Schema.String),\n})\n\n/**\n * Catch-all variant for wire events that fail to decode against any known\n * schema, plus events that fail to JSON-parse. The decoder never produces\n * this directly - it's synthesized by `sseEventToProviderEvent` when\n * `decodeKnown` fails.\n */\nconst Unknown = Schema.Struct({\n type: Schema.Literal(\"_unknown\"),\n raw: Schema.Unknown,\n})\n\n/**\n * Internal: union of variants we actually know how to decode from the wire.\n * Used as the decode target; failures are caught and re-emitted as `Unknown`.\n */\nexport const KnownProviderEvent = Schema.Union([\n OutputItemAdded,\n OutputTextDelta,\n FunctionArgsDelta,\n ReasoningTextDelta,\n ReasoningSummaryDelta,\n RefusalDelta,\n RefusalDone,\n Completed,\n Incomplete,\n Failed,\n ErrorEvent,\n])\n\n/**\n * Public: every event the native stream can emit. Discriminated on `type`.\n * The `_unknown` branch closes the cardinality so downstream `Match.exhaustive`\n * cannot silently miss a wire event we didn't model.\n */\nexport const ProviderEvent = Schema.Union([\n OutputItemAdded,\n OutputTextDelta,\n FunctionArgsDelta,\n ReasoningTextDelta,\n ReasoningSummaryDelta,\n RefusalDelta,\n RefusalDone,\n Completed,\n Incomplete,\n Failed,\n ErrorEvent,\n Unknown,\n])\nexport type ProviderEvent = typeof ProviderEvent.Type\n\n// `call_id` lookup for `function_call_arguments.delta`. The SSE event\n// references the *item id*, not the call id; we keep a small map populated\n// from the `output_item.added` events.\nexport interface CallIdLookup {\n readonly resolve: (itemId: string) => string | undefined\n readonly remember: (itemId: string, callId: string) => void\n}\n\nexport const makeCallIdLookup = (): CallIdLookup => {\n const map = new Map<string, string>()\n return {\n resolve: (id) => map.get(id),\n remember: (id, callId) => {\n map.set(id, callId)\n },\n }\n}\n\n/**\n * Translate a decoded provider event into zero-or-more `TurnEvent`s.\n * Mutates `lookup` to record `(item_id → call_id)` from `output_item.added`\n * so subsequent `function_call_arguments.delta` events can be tagged with\n * the right call id.\n */\nexport const eventToDeltas = (\n event: ProviderEvent,\n lookup: CallIdLookup,\n): ReadonlyArray<TurnEvent> =>\n Match.value(event).pipe(\n matchType(\"response.output_item.added\", ({ item }) => {\n if (item.type !== \"function_call\") return []\n if (item.id !== undefined) lookup.remember(item.id, item.call_id)\n return [\n {\n type: \"tool_call_start\" as const,\n call_id: item.call_id,\n name: item.name,\n },\n ]\n }),\n matchType(\"response.output_text.delta\", ({ delta }) => [\n { type: \"text_delta\" as const, text: delta },\n ]),\n matchType(\"response.function_call_arguments.delta\", ({ item_id, delta }) => {\n const call_id = lookup.resolve(item_id)\n return call_id === undefined\n ? []\n : [{ type: \"tool_call_args_delta\" as const, call_id, delta }]\n }),\n matchType(\"response.reasoning_text.delta\", ({ delta }) => [\n { type: \"reasoning_delta\" as const, text: delta, kind: \"trace\" as const },\n ]),\n matchType(\"response.reasoning_summary_text.delta\", ({ delta }) => [\n { type: \"reasoning_delta\" as const, text: delta, kind: \"summary\" as const },\n ]),\n matchType(\"response.refusal.delta\", ({ delta }) => [\n { type: \"refusal_delta\" as const, text: delta },\n ]),\n // Terminal marker for the refusal stream; the deltas already covered\n // the text, so no canonical event is emitted here.\n matchType(\"response.refusal.done\", () => []),\n matchType(\"response.completed\", ({ response }) => [\n { type: \"turn_complete\" as const, turn: turnFromCompleted(response) },\n ]),\n // Incomplete = the model stopped early but produced a usable partial\n // turn (max_tokens, content_filter, max_tool_calls, refusal). Same\n // payload shape as `response.completed`; the stop_reason carries the\n // reason.\n matchType(\"response.incomplete\", ({ response }) => [\n { type: \"turn_complete\" as const, turn: turnFromCompleted(response) },\n ]),\n // Failed and error are surfaced separately as `AiError` by the stream\n // pipeline, not as canonical events.\n matchType(\"response.failed\", () => []),\n matchType(\"error\", () => []),\n // No silent drops: unknown wire events flow through `streamNative` but\n // produce no canonical delta. Step 3 (canonical `other` event) replaces\n // this with a forwarded `other` delta on `TurnEvent`.\n matchType(\"_unknown\", () => []),\n Match.exhaustive,\n )\n","import { Context, Effect, Layer, Match, Option, Redacted, Schema, Stream } from \"effect\"\nimport { HttpClient, HttpClientRequest } from \"effect/unstable/http\"\nimport * as AiError from \"@effect-uai/core/AiError\"\nimport { matchType } from \"@effect-uai/core/Match\"\nimport * as StructuredFormat from \"@effect-uai/core/StructuredFormat\"\nimport {\n type CommonRequest,\n LanguageModel,\n type LanguageModelService,\n} from \"@effect-uai/core/LanguageModel\"\nimport * as SSE from \"@effect-uai/core/SSE\"\nimport type { TurnEvent } from \"@effect-uai/core/Turn\"\nimport { itemsToInput } from \"./codec.js\"\nimport type { OpenAIModel } from \"./models.js\"\nimport {\n KnownProviderEvent,\n ProviderEvent,\n eventToDeltas,\n makeCallIdLookup,\n} from \"./streamEvents.js\"\n\n// ---------------------------------------------------------------------------\n// Public types\n// ---------------------------------------------------------------------------\n\nexport interface ResponsesRequest extends Omit<CommonRequest, \"model\"> {\n /**\n * Narrows `CommonRequest.model` (`string`) to the typed `OpenAIModel`\n * literal union for autocomplete.\n */\n readonly model: OpenAIModel\n readonly reasoning?: { readonly effort: \"low\" | \"medium\" | \"high\" }\n readonly store?: boolean\n readonly previousResponseId?: string\n readonly instructions?: string\n readonly parallelToolCalls?: boolean\n readonly metadata?: Readonly<Record<string, string>>\n readonly user?: string\n readonly safetyIdentifier?: string\n readonly promptCacheKey?: string\n readonly truncation?: \"auto\" | \"disabled\"\n /**\n * Free-form JSON output: model emits valid JSON without schema\n * constraints. Mutually exclusive with `structured` (schema wins if\n * both are set).\n */\n readonly responseFormat?: { readonly type: \"json_object\" }\n /**\n * Answer length / level-of-detail hint. GPT-5+ models honour it;\n * earlier models ignore.\n */\n readonly verbosity?: \"low\" | \"medium\" | \"high\"\n}\n\nexport interface ResponsesService {\n /**\n * Stream the provider's native event vocabulary (post-SSE-decode).\n * Use this when you need full vendor fidelity. For provider-portable\n * code, use `streamTurn` instead.\n */\n readonly streamNative: (\n request: ResponsesRequest,\n ) => Stream.Stream<ProviderEvent, AiError.AiError>\n /**\n * Stream canonical `TurnEvent`s. Implemented as\n * `streamNative |> toCanonical`.\n */\n readonly streamTurn: (request: ResponsesRequest) => Stream.Stream<TurnEvent, AiError.AiError>\n /**\n * Project a stream of native `ProviderEvent`s into canonical `TurnEvent`s.\n * Exposed for cases where consumers want to compose with `streamNative`\n * directly (e.g. tap into natives + still get canonical downstream).\n */\n readonly toCanonical: <E, R>(\n s: Stream.Stream<ProviderEvent, E, R>,\n ) => Stream.Stream<TurnEvent, E, R>\n}\n\n/**\n * Provider-typed service tag. Yield this when you want Responses-specific\n * options (`reasoning.effort`, `store`, `previousResponseId`) at the call\n * site. Yield the generic `LanguageModel` tag for provider-portable code -\n * both are registered by `layer`.\n */\nexport class Responses extends Context.Service<Responses, ResponsesService>()(\n \"@betalyra/effect-uai/providers/responses/Responses\",\n) {}\n\nexport interface Config {\n readonly apiKey: Redacted.Redacted\n readonly baseUrl?: string\n}\n\n// ---------------------------------------------------------------------------\n// Request body\n// ---------------------------------------------------------------------------\n\nconst jsonSchemaFormat = (\n format: StructuredFormat.StructuredFormat<unknown>,\n): Record<string, unknown> => ({\n type: \"json_schema\",\n name: format.name,\n schema: format.schema[\"~standard\"].jsonSchema.input({ target: \"draft-2020-12\" }),\n ...(format.description !== undefined && { description: format.description }),\n ...(format.strict !== undefined && { strict: format.strict }),\n})\n\nconst buildText = (request: ResponsesRequest): Record<string, unknown> | undefined => {\n const format =\n request.structured !== undefined ? jsonSchemaFormat(request.structured) : request.responseFormat\n const text: Record<string, unknown> = {}\n if (format !== undefined) text.format = format\n if (request.verbosity !== undefined) text.verbosity = request.verbosity\n return Object.keys(text).length === 0 ? undefined : text\n}\n\nconst buildBody = (request: ResponsesRequest): Record<string, unknown> => {\n const text = buildText(request)\n return {\n model: request.model,\n input: itemsToInput(request.history),\n stream: true,\n ...(request.tools !== undefined &&\n request.tools.length > 0 && {\n tools: request.tools.map((t) => ({\n type: \"function\",\n name: t.name,\n description: t.description,\n parameters: t.inputSchema,\n ...(t.strict !== undefined && { strict: t.strict }),\n })),\n }),\n ...(request.toolChoice !== undefined && { tool_choice: request.toolChoice }),\n ...(request.temperature !== undefined && { temperature: request.temperature }),\n ...(request.maxOutputTokens !== undefined && {\n max_output_tokens: request.maxOutputTokens,\n }),\n ...(request.reasoning !== undefined && { reasoning: request.reasoning }),\n ...(request.store !== undefined && { store: request.store }),\n ...(request.previousResponseId !== undefined && {\n previous_response_id: request.previousResponseId,\n }),\n ...(request.instructions !== undefined && { instructions: request.instructions }),\n ...(request.topP !== undefined && { top_p: request.topP }),\n ...(request.parallelToolCalls !== undefined && {\n parallel_tool_calls: request.parallelToolCalls,\n }),\n ...(request.metadata !== undefined && { metadata: request.metadata }),\n ...(request.user !== undefined && { user: request.user }),\n ...(request.safetyIdentifier !== undefined && {\n safety_identifier: request.safetyIdentifier,\n }),\n ...(request.promptCacheKey !== undefined && {\n prompt_cache_key: request.promptCacheKey,\n }),\n ...(request.truncation !== undefined && { truncation: request.truncation }),\n ...(text !== undefined && { text }),\n }\n}\n\n// ---------------------------------------------------------------------------\n// SSE event → provider event\n// ---------------------------------------------------------------------------\n\nconst decodeKnown = Schema.decodeUnknownEffect(KnownProviderEvent)\n\nconst makeUnknown = (raw: unknown): ProviderEvent => ({ type: \"_unknown\", raw })\n\n/**\n * Lift events that carry a terminal failure signal (`error`,\n * `response.failed`) to a typed `AiError`. Other events are not failures\n * and produce `Option.none`.\n */\nconst eventToError = Match.type<ProviderEvent>().pipe(\n matchType(\n \"error\",\n (e): AiError.AiError => new AiError.Unavailable({ provider: \"responses\", raw: e }),\n ),\n matchType(\"response.failed\", (e): AiError.AiError => {\n const code = e.response.error?.code\n const message = e.response.error?.message\n return new AiError.GenerationFailed({\n provider: \"responses\",\n ...(code !== undefined && code !== null && { code }),\n ...(message !== undefined && message !== null && { message }),\n raw: e,\n })\n }),\n Match.option,\n)\n\nconst sseEventToProviderEvent = (ev: SSE.Event): Effect.Effect<ProviderEvent> =>\n Effect.try({\n try: () => JSON.parse(ev.data) as unknown,\n catch: () => ev.data,\n }).pipe(\n Effect.flatMap((parsed) =>\n decodeKnown(parsed).pipe(Effect.orElseSucceed(() => makeUnknown(parsed))),\n ),\n Effect.orElseSucceed(() => makeUnknown(ev.data)),\n )\n\n// ---------------------------------------------------------------------------\n// Service implementation\n// ---------------------------------------------------------------------------\n\nconst httpStatusError = (status: number, body: string): AiError.AiError => {\n const provider = \"responses\"\n const raw = body\n if (status === 429) return new AiError.RateLimited({ provider, raw })\n if (status === 408 || status === 504) return new AiError.Timeout({ provider, raw })\n if (status === 401) return new AiError.AuthFailed({ provider, subtype: \"auth\", raw })\n if (status === 403) return new AiError.AuthFailed({ provider, subtype: \"permission\", raw })\n if (status === 402) return new AiError.AuthFailed({ provider, subtype: \"billing\", raw })\n if (status === 413) return new AiError.ContextLengthExceeded({ provider, raw })\n if (status >= 500) return new AiError.Unavailable({ provider, status, raw })\n return new AiError.InvalidRequest({ provider, raw })\n}\n\nconst buildNativeStream = (cfg: Config) => {\n const url = `${cfg.baseUrl ?? \"https://api.openai.com/v1\"}/responses`\n return (\n request: ResponsesRequest,\n ): Stream.Stream<ProviderEvent, AiError.AiError, HttpClient.HttpClient> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const client = yield* HttpClient.HttpClient\n const httpRequest = HttpClientRequest.post(url).pipe(\n HttpClientRequest.bearerToken(cfg.apiKey),\n HttpClientRequest.bodyJsonUnsafe(buildBody(request)),\n HttpClientRequest.accept(\"text/event-stream\"),\n )\n const response = yield* client\n .execute(httpRequest)\n .pipe(\n Effect.mapError(\n (cause) => new AiError.Unavailable({ provider: \"responses\", raw: cause }),\n ),\n )\n if (response.status >= 400) {\n const body = yield* response.text.pipe(Effect.orElseSucceed(() => \"\"))\n return Stream.fail(httpStatusError(response.status, body))\n }\n\n return response.stream.pipe(\n Stream.mapError(\n (cause) => new AiError.Unavailable({ provider: \"responses\", raw: cause }),\n ),\n SSE.fromBytes,\n Stream.mapEffect(sseEventToProviderEvent),\n Stream.flatMap((event) =>\n Option.match(eventToError(event), {\n onNone: () => Stream.succeed(event),\n onSome: Stream.fail,\n }),\n ),\n )\n }),\n )\n}\n\n/**\n * Project a stream of native `ProviderEvent`s into canonical `TurnEvent`s.\n * Threads a fresh `CallIdLookup` per stream so `function_call_arguments.delta`\n * can be tagged with the right `call_id`.\n */\nexport const toCanonical = <E, R>(\n s: Stream.Stream<ProviderEvent, E, R>,\n): Stream.Stream<TurnEvent, E, R> =>\n Stream.unwrap(\n Effect.sync(() => {\n const lookup = makeCallIdLookup()\n return s.pipe(Stream.flatMap((event) => Stream.fromIterable(eventToDeltas(event, lookup))))\n }),\n )\n\n// ---------------------------------------------------------------------------\n// Constructors\n// ---------------------------------------------------------------------------\n\n/**\n * Build a `ResponsesService` value. For Layer-based setup, prefer `layer`.\n */\nexport const make = (cfg: Config): Effect.Effect<ResponsesService, never, HttpClient.HttpClient> =>\n Effect.map(HttpClient.HttpClient.asEffect(), (client) => {\n const streamNative: ResponsesService[\"streamNative\"] = (request) =>\n buildNativeStream(cfg)(request).pipe(Stream.provideService(HttpClient.HttpClient, client))\n return {\n streamNative,\n streamTurn: (request) => toCanonical(streamNative(request)),\n toCanonical,\n }\n })\n\n/**\n * Layer that registers both the provider-specific `Responses` tag and the\n * generic `LanguageModel` tag, sharing one underlying implementation.\n *\n * The generic tag accepts `CommonRequest`; the typed tag accepts the full\n * `ResponsesRequest` surface.\n */\nexport const layer = (\n cfg: Config,\n): Layer.Layer<Responses | LanguageModel, never, HttpClient.HttpClient> => {\n const typed = Layer.effect(Responses, make(cfg))\n const generic = Layer.effect(\n LanguageModel,\n Effect.map(\n make(cfg),\n (s): LanguageModelService => ({\n streamTurn: (request) => s.streamTurn(request as ResponsesRequest),\n }),\n ),\n )\n return Layer.merge(typed, generic)\n}\n"],"mappings":";;;;;;;;;;;;;;;AAUA,MAAM,kBAAkB,OAAO,OAAO;CACpC,MAAM,OAAO,QAAQ,eAAe;CACpC,KAAK,OAAO;CACZ,aAAa,OAAO;CACpB,WAAW,OAAO;CAClB,OAAO,OAAO;CACf,CAAC;AAEF,MAAM,mBAAmB,OAAO,OAAO;CACrC,MAAM,OAAO,QAAQ,gBAAgB;CACrC,SAAS,OAAO;CAChB,OAAO,OAAO;CACf,CAAC;AAEF,MAAM,4BAA4B,OAAO,OAAO;CAC9C,MAAM,OAAO,QAAQ,0BAA0B;CAC/C,cAAc,OAAO;CACrB,SAAS,OAAO;CAChB,aAAa,OAAO;CACpB,WAAW,OAAO;CACnB,CAAC;AAEF,MAAM,eAAe,OAAO,OAAO;CACjC,MAAM,OAAO,QAAQ,YAAY;CACjC,SAAS,OAAO;CAChB,OAAO,OAAO;CACf,CAAC;AAEF,MAAM,iBAAiB,OAAO,MAAM;CAClC;CACA;CACA;CACA;CACD,CAAC;AAEF,MAAM,wBAAwB,OAAO,OAAO;CAC1C,MAAM,OAAO,QAAQ,cAAc;CACnC,MAAM,OAAO;CACb,aAAa,OAAO,SAAS,OAAO,OAAO,OAAO,MAAM,eAAe,CAAC,CAAC;CAC1E,CAAC;AAEF,MAAM,qBAAqB,OAAO,OAAO;CACvC,MA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