@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.
@@ -0,0 +1,133 @@
1
+ import { Schema } from "effect";
2
+ import { Turn } from "@effect-uai/core/Turn";
3
+ import { Item } from "@effect-uai/core/Items";
4
+
5
+ //#region src/codec.d.ts
6
+ declare namespace codec_d_exports {
7
+ export { WireOutputItem, WireResponseCompleted, itemsToInput, turnFromCompleted, wireItemToItem };
8
+ }
9
+ declare const WireOutputItem: Schema.Union<readonly [Schema.Struct<{
10
+ readonly type: Schema.Literal<"message">;
11
+ readonly id: Schema.optional<Schema.String>;
12
+ readonly role: Schema.Literal<"assistant">;
13
+ readonly content: Schema.$Array<Schema.Union<readonly [Schema.Struct<{
14
+ readonly type: Schema.Literal<"output_text">;
15
+ readonly text: Schema.String;
16
+ readonly annotations: Schema.optional<Schema.NullOr<Schema.$Array<Schema.Union<readonly [Schema.Struct<{
17
+ readonly type: Schema.Literal<"url_citation">;
18
+ readonly url: Schema.String;
19
+ readonly start_index: Schema.Number;
20
+ readonly end_index: Schema.Number;
21
+ readonly title: Schema.String;
22
+ }>, Schema.Struct<{
23
+ readonly type: Schema.Literal<"file_citation">;
24
+ readonly file_id: Schema.String;
25
+ readonly index: Schema.Number;
26
+ }>, Schema.Struct<{
27
+ readonly type: Schema.Literal<"container_file_citation">;
28
+ readonly container_id: Schema.String;
29
+ readonly file_id: Schema.String;
30
+ readonly start_index: Schema.Number;
31
+ readonly end_index: Schema.Number;
32
+ }>, Schema.Struct<{
33
+ readonly type: Schema.Literal<"file_path">;
34
+ readonly file_id: Schema.String;
35
+ readonly index: Schema.Number;
36
+ }>]>>>>;
37
+ }>, Schema.Struct<{
38
+ readonly type: Schema.Literal<"refusal">;
39
+ readonly refusal: Schema.String;
40
+ }>]>>;
41
+ }>, Schema.Struct<{
42
+ readonly type: Schema.Literal<"function_call">;
43
+ readonly id: Schema.optional<Schema.String>;
44
+ readonly call_id: Schema.String;
45
+ readonly name: Schema.String;
46
+ readonly arguments: Schema.String;
47
+ }>, Schema.Struct<{
48
+ readonly type: Schema.Literal<"reasoning">;
49
+ readonly id: Schema.optional<Schema.String>;
50
+ readonly summary: Schema.optional<Schema.$Array<Schema.Struct<{
51
+ readonly type: Schema.Literal<"summary_text">;
52
+ readonly text: Schema.String;
53
+ }>>>;
54
+ readonly encrypted_content: Schema.optional<Schema.String>;
55
+ }>]>;
56
+ type WireOutputItem = typeof WireOutputItem.Type;
57
+ declare const WireResponseCompleted: Schema.Struct<{
58
+ readonly id: Schema.optional<Schema.NullOr<Schema.String>>;
59
+ readonly status: Schema.optional<Schema.NullOr<Schema.String>>;
60
+ readonly output: Schema.optional<Schema.NullOr<Schema.$Array<Schema.Union<readonly [Schema.Struct<{
61
+ readonly type: Schema.Literal<"message">;
62
+ readonly id: Schema.optional<Schema.String>;
63
+ readonly role: Schema.Literal<"assistant">;
64
+ readonly content: Schema.$Array<Schema.Union<readonly [Schema.Struct<{
65
+ readonly type: Schema.Literal<"output_text">;
66
+ readonly text: Schema.String;
67
+ readonly annotations: Schema.optional<Schema.NullOr<Schema.$Array<Schema.Union<readonly [Schema.Struct<{
68
+ readonly type: Schema.Literal<"url_citation">;
69
+ readonly url: Schema.String;
70
+ readonly start_index: Schema.Number;
71
+ readonly end_index: Schema.Number;
72
+ readonly title: Schema.String;
73
+ }>, Schema.Struct<{
74
+ readonly type: Schema.Literal<"file_citation">;
75
+ readonly file_id: Schema.String;
76
+ readonly index: Schema.Number;
77
+ }>, Schema.Struct<{
78
+ readonly type: Schema.Literal<"container_file_citation">;
79
+ readonly container_id: Schema.String;
80
+ readonly file_id: Schema.String;
81
+ readonly start_index: Schema.Number;
82
+ readonly end_index: Schema.Number;
83
+ }>, Schema.Struct<{
84
+ readonly type: Schema.Literal<"file_path">;
85
+ readonly file_id: Schema.String;
86
+ readonly index: Schema.Number;
87
+ }>]>>>>;
88
+ }>, Schema.Struct<{
89
+ readonly type: Schema.Literal<"refusal">;
90
+ readonly refusal: Schema.String;
91
+ }>]>>;
92
+ }>, Schema.Struct<{
93
+ readonly type: Schema.Literal<"function_call">;
94
+ readonly id: Schema.optional<Schema.String>;
95
+ readonly call_id: Schema.String;
96
+ readonly name: Schema.String;
97
+ readonly arguments: Schema.String;
98
+ }>, Schema.Struct<{
99
+ readonly type: Schema.Literal<"reasoning">;
100
+ readonly id: Schema.optional<Schema.String>;
101
+ readonly summary: Schema.optional<Schema.$Array<Schema.Struct<{
102
+ readonly type: Schema.Literal<"summary_text">;
103
+ readonly text: Schema.String;
104
+ }>>>;
105
+ readonly encrypted_content: Schema.optional<Schema.String>;
106
+ }>]>>>>;
107
+ readonly usage: Schema.optional<Schema.NullOr<Schema.Struct<{
108
+ readonly input_tokens: Schema.optional<Schema.Number>;
109
+ readonly output_tokens: Schema.optional<Schema.Number>;
110
+ readonly total_tokens: Schema.optional<Schema.Number>;
111
+ readonly input_tokens_details: Schema.optional<Schema.NullOr<Schema.Struct<{
112
+ readonly cached_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
113
+ }>>>;
114
+ readonly output_tokens_details: Schema.optional<Schema.NullOr<Schema.Struct<{
115
+ readonly reasoning_tokens: Schema.optional<Schema.NullOr<Schema.Number>>;
116
+ }>>>;
117
+ }>>>;
118
+ readonly incomplete_details: Schema.optional<Schema.NullOr<Schema.Struct<{
119
+ readonly reason: Schema.optional<Schema.NullOr<Schema.String>>;
120
+ }>>>;
121
+ readonly error: Schema.optional<Schema.NullOr<Schema.Struct<{
122
+ readonly code: Schema.optional<Schema.NullOr<Schema.String>>;
123
+ readonly message: Schema.optional<Schema.NullOr<Schema.String>>;
124
+ }>>>;
125
+ }>;
126
+ type WireResponseCompleted = typeof WireResponseCompleted.Type;
127
+ /** Convert our `Item[]` history into the Responses API `input` array. */
128
+ declare const itemsToInput: (items: ReadonlyArray<Item>) => ReadonlyArray<Record<string, unknown>>;
129
+ declare const wireItemToItem: (wire: WireOutputItem) => Item;
130
+ declare const turnFromCompleted: (payload: WireResponseCompleted) => Turn;
131
+ //#endregion
132
+ export { WireOutputItem, WireResponseCompleted, itemsToInput, codec_d_exports as t, turnFromCompleted, wireItemToItem };
133
+ //# sourceMappingURL=codec.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec.d.mts","names":[],"sources":["../src/codec.ts"],"mappings":";;;;;;;;cAoFa,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;;cA0EpD,YAAA,GAAgB,KAAA,EAAO,aAAA,CAAc,IAAA,MAAQ,aAAA,CAAc,MAAA;AAAA,cAkB3D,cAAA,GAAkB,IAAA,EAAM,cAAA,KAAiB,IAAA;AAAA,cA8DzC,iBAAA,GAAqB,OAAA,EAAS,qBAAA,KAAwB,IAAA"}
package/dist/codec.mjs ADDED
@@ -0,0 +1,218 @@
1
+ import { t as __exportAll } from "./chunk-CfYAbeIz.mjs";
2
+ import { Encoding, Match, Option, Schema } from "effect";
3
+ //#region src/codec.ts
4
+ var codec_exports = /* @__PURE__ */ __exportAll({
5
+ WireOutputItem: () => WireOutputItem,
6
+ WireResponseCompleted: () => WireResponseCompleted,
7
+ itemsToInput: () => itemsToInput,
8
+ turnFromCompleted: () => turnFromCompleted,
9
+ wireItemToItem: () => wireItemToItem
10
+ });
11
+ const WireUrlCitation = Schema.Struct({
12
+ type: Schema.Literal("url_citation"),
13
+ url: Schema.String,
14
+ start_index: Schema.Number,
15
+ end_index: Schema.Number,
16
+ title: Schema.String
17
+ });
18
+ const WireFileCitation = Schema.Struct({
19
+ type: Schema.Literal("file_citation"),
20
+ file_id: Schema.String,
21
+ index: Schema.Number
22
+ });
23
+ const WireContainerFileCitation = Schema.Struct({
24
+ type: Schema.Literal("container_file_citation"),
25
+ container_id: Schema.String,
26
+ file_id: Schema.String,
27
+ start_index: Schema.Number,
28
+ end_index: Schema.Number
29
+ });
30
+ const WireFilePath = Schema.Struct({
31
+ type: Schema.Literal("file_path"),
32
+ file_id: Schema.String,
33
+ index: Schema.Number
34
+ });
35
+ const WireAnnotation = Schema.Union([
36
+ WireUrlCitation,
37
+ WireFileCitation,
38
+ WireContainerFileCitation,
39
+ WireFilePath
40
+ ]);
41
+ const WireOutputTextContent = Schema.Struct({
42
+ type: Schema.Literal("output_text"),
43
+ text: Schema.String,
44
+ annotations: Schema.optional(Schema.NullOr(Schema.Array(WireAnnotation)))
45
+ });
46
+ const WireRefusalContent = Schema.Struct({
47
+ type: Schema.Literal("refusal"),
48
+ refusal: Schema.String
49
+ });
50
+ const WireMessageContent = Schema.Union([WireOutputTextContent, WireRefusalContent]);
51
+ const WireSummaryText = Schema.Struct({
52
+ type: Schema.Literal("summary_text"),
53
+ text: Schema.String
54
+ });
55
+ const WireMessage = Schema.Struct({
56
+ type: Schema.Literal("message"),
57
+ id: Schema.optional(Schema.String),
58
+ role: Schema.Literal("assistant"),
59
+ content: Schema.Array(WireMessageContent)
60
+ });
61
+ const WireFunctionCall = Schema.Struct({
62
+ type: Schema.Literal("function_call"),
63
+ id: Schema.optional(Schema.String),
64
+ call_id: Schema.String,
65
+ name: Schema.String,
66
+ arguments: Schema.String
67
+ });
68
+ const WireReasoning = Schema.Struct({
69
+ type: Schema.Literal("reasoning"),
70
+ id: Schema.optional(Schema.String),
71
+ summary: Schema.optional(Schema.Array(WireSummaryText)),
72
+ encrypted_content: Schema.optional(Schema.String)
73
+ });
74
+ const WireOutputItem = Schema.Union([
75
+ WireMessage,
76
+ WireFunctionCall,
77
+ WireReasoning
78
+ ]);
79
+ const WireInputTokensDetails = Schema.Struct({ cached_tokens: Schema.optional(Schema.NullOr(Schema.Number)) });
80
+ const WireOutputTokensDetails = Schema.Struct({ reasoning_tokens: Schema.optional(Schema.NullOr(Schema.Number)) });
81
+ const WireUsage = Schema.Struct({
82
+ input_tokens: Schema.optional(Schema.Number),
83
+ output_tokens: Schema.optional(Schema.Number),
84
+ total_tokens: Schema.optional(Schema.Number),
85
+ input_tokens_details: Schema.optional(Schema.NullOr(WireInputTokensDetails)),
86
+ output_tokens_details: Schema.optional(Schema.NullOr(WireOutputTokensDetails))
87
+ });
88
+ const WireResponseError = Schema.Struct({
89
+ code: Schema.optional(Schema.NullOr(Schema.String)),
90
+ message: Schema.optional(Schema.NullOr(Schema.String))
91
+ });
92
+ const WireResponseCompleted = Schema.Struct({
93
+ id: Schema.optional(Schema.NullOr(Schema.String)),
94
+ status: Schema.optional(Schema.NullOr(Schema.String)),
95
+ output: Schema.optional(Schema.NullOr(Schema.Array(WireOutputItem))),
96
+ usage: Schema.optional(Schema.NullOr(WireUsage)),
97
+ incomplete_details: Schema.optional(Schema.NullOr(Schema.Struct({ reason: Schema.optional(Schema.NullOr(Schema.String)) }))),
98
+ error: Schema.optional(Schema.NullOr(WireResponseError))
99
+ });
100
+ /**
101
+ * If an item carries `providerData` from a prior OpenAI turn, re-emit it
102
+ * verbatim - preserves `encrypted_content`, item ids, and any field this
103
+ * codec doesn't model.
104
+ */
105
+ const passthrough = (item) => item.providerData !== void 0 && typeof item.providerData === "object" && item.providerData !== null ? item.providerData : void 0;
106
+ /**
107
+ * OpenAI's `input_image` content block carries a single `image_url` field;
108
+ * inline bytes get encoded as a `data:` URI. Either form (URL or data URI)
109
+ * is fine - the model just dereferences `image_url`.
110
+ */
111
+ const imageSourceToUrl = Match.type().pipe(Match.tag("url", (s) => s.url), Match.tag("base64", (s) => `data:${s.mimeType};base64,${s.base64}`), Match.tag("bytes", (s) => `data:${s.mimeType};base64,${Encoding.encodeBase64(s.bytes)}`), Match.exhaustive);
112
+ const contentBlockToInput = Match.type().pipe(Match.discriminatorsExhaustive("type")({
113
+ input_text: (b) => ({
114
+ type: "input_text",
115
+ text: b.text
116
+ }),
117
+ input_image: (b) => ({
118
+ type: "input_image",
119
+ image_url: imageSourceToUrl(b.source)
120
+ }),
121
+ output_text: (b) => ({
122
+ type: "output_text",
123
+ text: b.text
124
+ }),
125
+ refusal: (b) => ({
126
+ type: "refusal",
127
+ refusal: b.text
128
+ })
129
+ }));
130
+ const itemToInput = (item) => passthrough(item) ?? Match.value(item).pipe(Match.discriminatorsExhaustive("type")({
131
+ message: (m) => ({
132
+ type: "message",
133
+ role: m.role,
134
+ content: m.content.map(contentBlockToInput)
135
+ }),
136
+ function_call: (f) => ({
137
+ type: "function_call",
138
+ call_id: f.call_id,
139
+ name: f.name,
140
+ arguments: f.arguments
141
+ }),
142
+ function_call_output: (o) => ({
143
+ type: "function_call_output",
144
+ call_id: o.call_id,
145
+ output: o.output
146
+ }),
147
+ reasoning: (r) => ({
148
+ type: "reasoning",
149
+ ...r.id !== void 0 && { id: r.id },
150
+ ...r.summary !== void 0 && { summary: [{
151
+ type: "summary_text",
152
+ text: r.summary
153
+ }] },
154
+ ...r.signature !== void 0 && { encrypted_content: r.signature }
155
+ })
156
+ }));
157
+ /** Convert our `Item[]` history into the Responses API `input` array. */
158
+ const itemsToInput = (items) => items.map(itemToInput);
159
+ const wireMessageContentToBlock = Match.type().pipe(Match.discriminatorsExhaustive("type")({
160
+ output_text: (c) => ({
161
+ type: "output_text",
162
+ text: c.text,
163
+ ...c.annotations !== void 0 && c.annotations !== null && { annotations: c.annotations }
164
+ }),
165
+ refusal: (c) => ({
166
+ type: "refusal",
167
+ text: c.refusal
168
+ })
169
+ }));
170
+ const wireItemToItem = (wire) => Match.value(wire).pipe(Match.discriminatorsExhaustive("type")({
171
+ message: (m) => ({
172
+ type: "message",
173
+ role: m.role,
174
+ content: m.content.map(wireMessageContentToBlock),
175
+ providerData: m
176
+ }),
177
+ function_call: (f) => ({
178
+ type: "function_call",
179
+ call_id: f.call_id,
180
+ name: f.name,
181
+ arguments: f.arguments,
182
+ providerData: f
183
+ }),
184
+ reasoning: (r) => ({
185
+ type: "reasoning",
186
+ ...r.id !== void 0 && { id: r.id },
187
+ ...r.summary !== void 0 && { summary: r.summary.map((s) => s.text).join("\n") },
188
+ ...r.encrypted_content !== void 0 && { signature: r.encrypted_content },
189
+ providerData: r
190
+ })
191
+ }));
192
+ 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);
193
+ const hasRefusalContent = (payload) => (payload.output ?? []).some((i) => i.type === "message" && i.content.some((c) => c.type === "refusal"));
194
+ const stopReasonFromCompleted = (payload) => Option.match(reasonToStop(payload.incomplete_details?.reason ?? ""), {
195
+ onSome: (s) => s,
196
+ onNone: () => hasRefusalContent(payload) ? "refusal" : (payload.output ?? []).some((i) => i.type === "function_call") ? "tool_calls" : "stop"
197
+ });
198
+ const cachedTokens = (payload) => payload.usage?.input_tokens_details?.cached_tokens ?? void 0;
199
+ const reasoningTokens = (payload) => payload.usage?.output_tokens_details?.reasoning_tokens ?? void 0;
200
+ const turnFromCompleted = (payload) => {
201
+ const cached = cachedTokens(payload);
202
+ const reasoning = reasoningTokens(payload);
203
+ return {
204
+ items: (payload.output ?? []).map(wireItemToItem),
205
+ usage: {
206
+ ...payload.usage?.input_tokens !== void 0 && { input_tokens: payload.usage.input_tokens },
207
+ ...payload.usage?.output_tokens !== void 0 && { output_tokens: payload.usage.output_tokens },
208
+ ...payload.usage?.total_tokens !== void 0 && { total_tokens: payload.usage.total_tokens },
209
+ ...cached !== void 0 && { input_tokens_details: { cached_tokens: cached } },
210
+ ...reasoning !== void 0 && { output_tokens_details: { reasoning_tokens: reasoning } }
211
+ },
212
+ stop_reason: stopReasonFromCompleted(payload)
213
+ };
214
+ };
215
+ //#endregion
216
+ export { WireOutputItem, WireResponseCompleted, itemsToInput, codec_exports as t, turnFromCompleted, wireItemToItem };
217
+
218
+ //# sourceMappingURL=codec.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec.mjs","names":[],"sources":["../src/codec.ts"],"sourcesContent":["import { Encoding, Match, Option, Schema } from \"effect\"\nimport type { ContentBlock, InputImage, Item } from \"@effect-uai/core/Items\"\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\n/**\n * OpenAI's `input_image` content block carries a single `image_url` field;\n * inline bytes get encoded as a `data:` URI. Either form (URL or data URI)\n * is fine - the model just dereferences `image_url`.\n */\nconst imageSourceToUrl = Match.type<InputImage[\"source\"]>().pipe(\n Match.tag(\"url\", (s) => s.url),\n Match.tag(\"base64\", (s) => `data:${s.mimeType};base64,${s.base64}`),\n Match.tag(\"bytes\", (s) => `data:${s.mimeType};base64,${Encoding.encodeBase64(s.bytes)}`),\n Match.exhaustive,\n)\n\nconst contentBlockToInput = Match.type<ContentBlock>().pipe(\n Match.discriminatorsExhaustive(\"type\")({\n input_text: (b) => ({ type: \"input_text\", text: b.text }),\n input_image: (b) => ({\n type: \"input_image\",\n image_url: imageSourceToUrl(b.source),\n }),\n output_text: (b) => ({ type: \"output_text\", text: b.text }),\n refusal: (b) => ({ type: \"refusal\", refusal: b.text }),\n }),\n)\n\nconst itemToInput = (item: Item): Record<string, unknown> =>\n passthrough(item) ??\n Match.value(item).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n message: (m) => ({\n type: \"message\",\n role: m.role,\n content: m.content.map(contentBlockToInput),\n }),\n function_call: (f) => ({\n type: \"function_call\",\n call_id: f.call_id,\n name: f.name,\n arguments: f.arguments,\n }),\n function_call_output: (o) => ({\n type: \"function_call_output\",\n call_id: o.call_id,\n output: o.output,\n }),\n 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 }),\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 Match.discriminatorsExhaustive(\"type\")({\n output_text: (c): ContentBlock => ({\n type: \"output_text\",\n text: c.text,\n ...(c.annotations !== undefined && c.annotations !== null && { annotations: c.annotations }),\n }),\n refusal: (c): ContentBlock => ({ type: \"refusal\", text: c.refusal }),\n }),\n)\n\nexport const wireItemToItem = (wire: WireOutputItem): Item =>\n Match.value(wire).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n message: (m) => ({\n type: \"message\" as const,\n role: m.role,\n content: m.content.map(wireMessageContentToBlock),\n providerData: m,\n }),\n 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 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 }),\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"],"mappings":";;;;;;;;;;AASA,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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