@effect-uai/responses 0.2.0 → 0.4.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/dist/OpenAIEmbedding.d.mts +53 -0
- package/dist/OpenAIEmbedding.d.mts.map +1 -0
- package/dist/OpenAIEmbedding.mjs +157 -0
- package/dist/OpenAIEmbedding.mjs.map +1 -0
- package/dist/Responses.d.mts +96 -0
- package/dist/Responses.d.mts.map +1 -0
- package/dist/Responses.mjs +194 -0
- package/dist/Responses.mjs.map +1 -0
- package/dist/codec.d.mts +133 -0
- package/dist/codec.d.mts.map +1 -0
- package/dist/codec.mjs +218 -0
- package/dist/codec.mjs.map +1 -0
- package/dist/index.d.mts +6 -828
- package/dist/index.mjs +6 -525
- package/dist/models.d.mts +25 -0
- package/dist/models.d.mts.map +1 -0
- package/dist/models.mjs +1 -0
- package/dist/streamEvents.d.mts +607 -0
- package/dist/streamEvents.d.mts.map +1 -0
- package/dist/streamEvents.mjs +170 -0
- package/dist/streamEvents.mjs.map +1 -0
- package/package.json +20 -12
- package/src/OpenAIEmbedding.ts +275 -0
- package/src/Responses.ts +15 -17
- package/src/codec.ts +75 -66
- package/src/index.ts +3 -2
- package/src/models.ts +18 -0
- package/src/streamEvents.ts +48 -52
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,525 +1,6 @@
|
|
|
1
|
-
import { t as
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
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
|
|
1
|
+
import { t as OpenAIEmbedding_exports } from "./OpenAIEmbedding.mjs";
|
|
2
|
+
import { t as codec_exports } from "./codec.mjs";
|
|
3
|
+
import { t as streamEvents_exports } from "./streamEvents.mjs";
|
|
4
|
+
import { t as Responses_exports } from "./Responses.mjs";
|
|
5
|
+
import "./models.mjs";
|
|
6
|
+
export { OpenAIEmbedding_exports as OpenAIEmbedding, Responses_exports as Responses, codec_exports as codec, streamEvents_exports as streamEvents };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/models.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Known OpenAI model identifiers usable via the Responses API (as of
|
|
4
|
+
* April 2026). The `(string & {})` tail keeps autocomplete on the literals
|
|
5
|
+
* while still accepting any string, so newly-released models work without
|
|
6
|
+
* an SDK update.
|
|
7
|
+
*
|
|
8
|
+
* Reference: https://developers.openai.com/api/docs/models/all
|
|
9
|
+
*/
|
|
10
|
+
type OpenAIModel = "gpt-5.5" | "gpt-5.5-pro" | "gpt-5.4" | "gpt-5.4-pro" | "gpt-5.4-mini" | "gpt-5.4-nano" | "gpt-5" | "gpt-5-mini" | "gpt-5-nano" | "gpt-5.3-codex" | "gpt-4.1" | "gpt-4.1-mini" | "gpt-4o-mini" | (string & {});
|
|
11
|
+
/**
|
|
12
|
+
* Known OpenAI embedding model identifiers. The `-3-` line is still the
|
|
13
|
+
* current general-purpose lineup; `dimensions` parameter supports
|
|
14
|
+
* Matryoshka truncation up to the model's native dimensionality.
|
|
15
|
+
*
|
|
16
|
+
* - `text-embedding-3-small`: 1..1536 dimensions, default 1536
|
|
17
|
+
* - `text-embedding-3-large`: 1..3072 dimensions, default 3072
|
|
18
|
+
* - `text-embedding-ada-002`: legacy, 1536 dim, no Matryoshka
|
|
19
|
+
*
|
|
20
|
+
* Reference: https://developers.openai.com/api/docs/guides/embeddings
|
|
21
|
+
*/
|
|
22
|
+
type OpenAIEmbeddingModel = "text-embedding-3-small" | "text-embedding-3-large" | "text-embedding-ada-002" | (string & {});
|
|
23
|
+
//#endregion
|
|
24
|
+
export { OpenAIEmbeddingModel, OpenAIModel };
|
|
25
|
+
//# sourceMappingURL=models.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"models.d.mts","names":[],"sources":["../src/models.ts"],"mappings":";;AAQA;;;;;AA4BA;;KA5BY,WAAA;;;;;;;;;;;;KA4BA,oBAAA"}
|
package/dist/models.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|