@effect-uai/responses 0.9.0 → 0.11.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/OpenAIDeepResearch.d.mts +54 -0
- package/dist/OpenAIDeepResearch.d.mts.map +1 -0
- package/dist/OpenAIDeepResearch.mjs +121 -0
- package/dist/OpenAIDeepResearch.mjs.map +1 -0
- package/dist/OpenAIEmbedding.mjs +1 -1
- package/dist/OpenAIEmbedding.mjs.map +1 -1
- package/dist/Responses.d.mts +13 -3
- package/dist/Responses.d.mts.map +1 -1
- package/dist/Responses.mjs +68 -33
- package/dist/Responses.mjs.map +1 -1
- package/dist/ResponsesTools.d.mts +85 -0
- package/dist/ResponsesTools.d.mts.map +1 -0
- package/dist/ResponsesTools.mjs +126 -0
- package/dist/ResponsesTools.mjs.map +1 -0
- package/dist/codec.d.mts +42 -3
- package/dist/codec.d.mts.map +1 -1
- package/dist/codec.mjs +28 -9
- package/dist/codec.mjs.map +1 -1
- package/dist/index.d.mts +5 -3
- package/dist/index.mjs +5 -3
- package/dist/{magic-string.es-PAZgLX4h.mjs → magic-string.es-CaWgF-3p.mjs} +1 -1
- package/dist/{magic-string.es-PAZgLX4h.mjs.map → magic-string.es-CaWgF-3p.mjs.map} +1 -1
- package/dist/models.d.mts +10 -1
- package/dist/models.d.mts.map +1 -1
- package/dist/region.test.mjs +1 -13630
- package/dist/region.test.mjs.map +1 -1
- package/dist/responsesTools.test.d.mts +1 -0
- package/dist/responsesTools.test.mjs +138 -0
- package/dist/responsesTools.test.mjs.map +1 -0
- package/dist/streamEvents.d.mts +142 -0
- package/dist/streamEvents.d.mts.map +1 -1
- package/dist/streamEvents.mjs +27 -2
- package/dist/streamEvents.mjs.map +1 -1
- package/dist/test.DNmyFkvJ-CPKukVWS.mjs +13633 -0
- package/dist/test.DNmyFkvJ-CPKukVWS.mjs.map +1 -0
- package/package.json +8 -4
- package/src/OpenAIDeepResearch.ts +266 -0
- package/src/OpenAIEmbedding.ts +1 -1
- package/src/Responses.ts +94 -36
- package/src/ResponsesTools.ts +158 -0
- package/src/codec.ts +62 -26
- package/src/index.ts +2 -0
- package/src/models.ts +14 -0
- package/src/responsesTools.test.ts +146 -0
- package/src/streamEvents.ts +58 -1
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Result, Schema } from "effect";
|
|
2
|
+
import * as Tool from "@effect-uai/core/Tool";
|
|
3
|
+
|
|
4
|
+
//#region src/ResponsesTools.d.ts
|
|
5
|
+
declare namespace ResponsesTools_d_exports {
|
|
6
|
+
export { ResponsesToolConfig, ToolRenderError, UserLocation, WebSearchFilters, WebSearchOptions, codeInterpreterTool, fileSearchTool, renderProviderTools, webSearchTool };
|
|
7
|
+
}
|
|
8
|
+
declare const UserLocation: Schema.Struct<{
|
|
9
|
+
readonly city: Schema.optional<Schema.String>;
|
|
10
|
+
readonly region: Schema.optional<Schema.String>;
|
|
11
|
+
readonly country: Schema.optional<Schema.String>;
|
|
12
|
+
readonly timezone: Schema.optional<Schema.String>;
|
|
13
|
+
}>;
|
|
14
|
+
type UserLocation = typeof UserLocation.Type;
|
|
15
|
+
declare const WebSearchFilters: Schema.Struct<{
|
|
16
|
+
readonly allowedDomains: Schema.optional<Schema.$Array<Schema.String>>;
|
|
17
|
+
readonly blockedDomains: Schema.optional<Schema.$Array<Schema.String>>;
|
|
18
|
+
}>;
|
|
19
|
+
type WebSearchFilters = typeof WebSearchFilters.Type;
|
|
20
|
+
/** Discriminated config for a Responses hosted tool. */
|
|
21
|
+
declare const ResponsesToolConfig: Schema.Union<readonly [Schema.Struct<{
|
|
22
|
+
readonly kind: Schema.Literal<"web_search">;
|
|
23
|
+
readonly filters: Schema.optional<Schema.Struct<{
|
|
24
|
+
readonly allowedDomains: Schema.optional<Schema.$Array<Schema.String>>;
|
|
25
|
+
readonly blockedDomains: Schema.optional<Schema.$Array<Schema.String>>;
|
|
26
|
+
}>>;
|
|
27
|
+
readonly userLocation: Schema.optional<Schema.Struct<{
|
|
28
|
+
readonly city: Schema.optional<Schema.String>;
|
|
29
|
+
readonly region: Schema.optional<Schema.String>;
|
|
30
|
+
readonly country: Schema.optional<Schema.String>;
|
|
31
|
+
readonly timezone: Schema.optional<Schema.String>;
|
|
32
|
+
}>>;
|
|
33
|
+
readonly searchContextSize: Schema.optional<Schema.Literals<readonly ["low", "medium", "high"]>>;
|
|
34
|
+
}>, Schema.Struct<{
|
|
35
|
+
readonly kind: Schema.Literal<"code_interpreter">;
|
|
36
|
+
readonly fileIds: Schema.optional<Schema.$Array<Schema.String>>;
|
|
37
|
+
}>, Schema.Struct<{
|
|
38
|
+
readonly kind: Schema.Literal<"file_search">;
|
|
39
|
+
readonly vectorStoreIds: Schema.$Array<Schema.String>;
|
|
40
|
+
}>]>;
|
|
41
|
+
type ResponsesToolConfig = typeof ResponsesToolConfig.Type;
|
|
42
|
+
type WebSearchOptions = {
|
|
43
|
+
readonly filters?: WebSearchFilters;
|
|
44
|
+
readonly userLocation?: UserLocation;
|
|
45
|
+
readonly searchContextSize?: "low" | "medium" | "high";
|
|
46
|
+
};
|
|
47
|
+
/** OpenAI-hosted web search. */
|
|
48
|
+
declare const webSearchTool: (opts?: WebSearchOptions) => Tool.ProviderTool<"web_search", Record<string, never>, "openai", {
|
|
49
|
+
readonly filters?: WebSearchFilters;
|
|
50
|
+
readonly userLocation?: UserLocation;
|
|
51
|
+
readonly searchContextSize?: "low" | "medium" | "high";
|
|
52
|
+
readonly kind: "web_search";
|
|
53
|
+
}>;
|
|
54
|
+
/** OpenAI-hosted code interpreter (Python sandbox). */
|
|
55
|
+
declare const codeInterpreterTool: (opts?: {
|
|
56
|
+
readonly fileIds?: ReadonlyArray<string>;
|
|
57
|
+
}) => Tool.ProviderTool<"code_interpreter", Record<string, never>, "openai", {
|
|
58
|
+
readonly fileIds?: ReadonlyArray<string>;
|
|
59
|
+
readonly kind: "code_interpreter";
|
|
60
|
+
}>;
|
|
61
|
+
/** OpenAI-hosted file search over the given vector stores. */
|
|
62
|
+
declare const fileSearchTool: (opts: {
|
|
63
|
+
readonly vectorStoreIds: ReadonlyArray<string>;
|
|
64
|
+
}) => Tool.ProviderTool<"file_search", Record<string, never>, "openai", {
|
|
65
|
+
readonly kind: "file_search";
|
|
66
|
+
readonly vectorStoreIds: readonly string[];
|
|
67
|
+
}>;
|
|
68
|
+
/**
|
|
69
|
+
* A provider tool this adapter cannot render — a foreign `provider` or an
|
|
70
|
+
* unrecognized `config`. Carried as data so `Responses.ts` can lift it into a
|
|
71
|
+
* typed `AiError.Unsupported`.
|
|
72
|
+
*/
|
|
73
|
+
type ToolRenderError = {
|
|
74
|
+
readonly capability: string;
|
|
75
|
+
readonly reason: string;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Render a toolkit's Responses provider tools to native `tools` entries. Fails
|
|
79
|
+
* with a `ToolRenderError` on the first foreign or unrecognized tool rather
|
|
80
|
+
* than dropping it.
|
|
81
|
+
*/
|
|
82
|
+
declare const renderProviderTools: (tools: ReadonlyArray<Tool.ProviderTool<string, any, string, any>>) => Result.Result<ReadonlyArray<Record<string, unknown>>, ToolRenderError>;
|
|
83
|
+
//#endregion
|
|
84
|
+
export { ResponsesToolConfig, ToolRenderError, UserLocation, WebSearchFilters, WebSearchOptions, codeInterpreterTool, fileSearchTool, renderProviderTools, ResponsesTools_d_exports as t, webSearchTool };
|
|
85
|
+
//# sourceMappingURL=ResponsesTools.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ResponsesTools.d.mts","names":[],"sources":["../src/ResponsesTools.ts"],"mappings":";;;;;;;cAYM,YAAA,EAAY,MAAA,CAAA,MAAA;EAAA;;;;;KAMN,YAAA,UAAsB,YAAA,CAAa,IAAA;AAAA,cAEzC,gBAAA,EAAgB,MAAA,CAAA,MAAA;EAAA;;;KAIV,gBAAA,UAA0B,gBAAA,CAAiB,IAAA;;cAoB1C,mBAAA,EAAmB,MAAA,CAAA,KAAA,WAAA,MAAA,CAAA,MAAA;EAAA;;;;;;;;;;;;;;;;;;;KAKpB,mBAAA,UAA6B,mBAAA,CAAoB,IAAA;AAAA,KAEjD,gBAAA;EAAA,SACD,OAAA,GAAU,gBAAA;EAAA,SACV,YAAA,GAAe,YAAA;EAAA,SACf,iBAAA;AAAA;;cAIE,aAAA,GAAiB,IAAA,GAAM,gBAAA,KAAqB,IAAA,CAAA,YAAA,eAAA,MAAA;EAAA,mBANpC,gBAAA;EAAA,wBACK,YAAA;EAAA;;;;cAeb,mBAAA,GAAuB,IAAA;EAAA,SAAiB,OAAA,GAAU,aAAA;AAAA,MAA4B,IAAA,CAAA,YAAA,qBAAA,MAAA;EAAA,mBAA5B,aAAA;EAAA;;;cAUlD,cAAA,GAAkB,IAAA;EAAA,SAAiB,cAAA,EAAgB,aAAA;AAAA,MAAuB,IAAA,CAAA,YAAA,gBAAA,MAAA;EAAA;;;;;;;;KAc3E,eAAA;EAAA,SACD,UAAA;EAAA,SACA,MAAA;AAAA;;;;;;cA4DE,mBAAA,GACX,KAAA,EAAO,aAAA,CAAc,IAAA,CAAK,YAAA,gCACzB,MAAA,CAAO,MAAA,CAAO,aAAA,CAAc,MAAA,oBAA0B,eAAA"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { t as __exportAll } from "./chunk-CfYAbeIz.mjs";
|
|
2
|
+
import { Match, Result, Schema } from "effect";
|
|
3
|
+
import * as Tool from "@effect-uai/core/Tool";
|
|
4
|
+
//#region src/ResponsesTools.ts
|
|
5
|
+
var ResponsesTools_exports = /* @__PURE__ */ __exportAll({
|
|
6
|
+
ResponsesToolConfig: () => ResponsesToolConfig,
|
|
7
|
+
codeInterpreterTool: () => codeInterpreterTool,
|
|
8
|
+
fileSearchTool: () => fileSearchTool,
|
|
9
|
+
renderProviderTools: () => renderProviderTools,
|
|
10
|
+
webSearchTool: () => webSearchTool
|
|
11
|
+
});
|
|
12
|
+
const UserLocation = Schema.Struct({
|
|
13
|
+
city: Schema.optional(Schema.String),
|
|
14
|
+
region: Schema.optional(Schema.String),
|
|
15
|
+
country: Schema.optional(Schema.String),
|
|
16
|
+
timezone: Schema.optional(Schema.String)
|
|
17
|
+
});
|
|
18
|
+
const WebSearchFilters = Schema.Struct({
|
|
19
|
+
allowedDomains: Schema.optional(Schema.Array(Schema.String)),
|
|
20
|
+
blockedDomains: Schema.optional(Schema.Array(Schema.String))
|
|
21
|
+
});
|
|
22
|
+
const WebSearchConfig = Schema.Struct({
|
|
23
|
+
kind: Schema.Literal("web_search"),
|
|
24
|
+
filters: Schema.optional(WebSearchFilters),
|
|
25
|
+
userLocation: Schema.optional(UserLocation),
|
|
26
|
+
searchContextSize: Schema.optional(Schema.Literals([
|
|
27
|
+
"low",
|
|
28
|
+
"medium",
|
|
29
|
+
"high"
|
|
30
|
+
]))
|
|
31
|
+
});
|
|
32
|
+
const CodeInterpreterConfig = Schema.Struct({
|
|
33
|
+
kind: Schema.Literal("code_interpreter"),
|
|
34
|
+
fileIds: Schema.optional(Schema.Array(Schema.String))
|
|
35
|
+
});
|
|
36
|
+
const FileSearchConfig = Schema.Struct({
|
|
37
|
+
kind: Schema.Literal("file_search"),
|
|
38
|
+
vectorStoreIds: Schema.Array(Schema.String)
|
|
39
|
+
});
|
|
40
|
+
/** Discriminated config for a Responses hosted tool. */
|
|
41
|
+
const ResponsesToolConfig = Schema.Union([
|
|
42
|
+
WebSearchConfig,
|
|
43
|
+
CodeInterpreterConfig,
|
|
44
|
+
FileSearchConfig
|
|
45
|
+
]);
|
|
46
|
+
/** OpenAI-hosted web search. */
|
|
47
|
+
const webSearchTool = (opts = {}) => Tool.provider({
|
|
48
|
+
name: "web_search",
|
|
49
|
+
description: "Search the web, executed by OpenAI.",
|
|
50
|
+
inputSchema: Tool.noInput,
|
|
51
|
+
provider: "openai",
|
|
52
|
+
config: {
|
|
53
|
+
kind: "web_search",
|
|
54
|
+
...opts
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
/** OpenAI-hosted code interpreter (Python sandbox). */
|
|
58
|
+
const codeInterpreterTool = (opts = {}) => Tool.provider({
|
|
59
|
+
name: "code_interpreter",
|
|
60
|
+
description: "Run Python in a sandbox, executed by OpenAI.",
|
|
61
|
+
inputSchema: Tool.noInput,
|
|
62
|
+
provider: "openai",
|
|
63
|
+
config: {
|
|
64
|
+
kind: "code_interpreter",
|
|
65
|
+
...opts
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
/** OpenAI-hosted file search over the given vector stores. */
|
|
69
|
+
const fileSearchTool = (opts) => Tool.provider({
|
|
70
|
+
name: "file_search",
|
|
71
|
+
description: "Retrieve from uploaded files, executed by OpenAI.",
|
|
72
|
+
inputSchema: Tool.noInput,
|
|
73
|
+
provider: "openai",
|
|
74
|
+
config: {
|
|
75
|
+
kind: "file_search",
|
|
76
|
+
vectorStoreIds: opts.vectorStoreIds
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
const filtersWire = (f) => ({
|
|
80
|
+
...f.allowedDomains !== void 0 && { allowed_domains: f.allowedDomains },
|
|
81
|
+
...f.blockedDomains !== void 0 && { blocked_domains: f.blockedDomains }
|
|
82
|
+
});
|
|
83
|
+
const userLocationWire = (loc) => ({
|
|
84
|
+
type: "approximate",
|
|
85
|
+
...loc.city !== void 0 && { city: loc.city },
|
|
86
|
+
...loc.region !== void 0 && { region: loc.region },
|
|
87
|
+
...loc.country !== void 0 && { country: loc.country },
|
|
88
|
+
...loc.timezone !== void 0 && { timezone: loc.timezone }
|
|
89
|
+
});
|
|
90
|
+
const configToWire = Match.type().pipe(Match.discriminatorsExhaustive("kind")({
|
|
91
|
+
web_search: (c) => ({
|
|
92
|
+
type: "web_search",
|
|
93
|
+
...c.filters !== void 0 && { filters: filtersWire(c.filters) },
|
|
94
|
+
...c.userLocation !== void 0 && { user_location: userLocationWire(c.userLocation) },
|
|
95
|
+
...c.searchContextSize !== void 0 && { search_context_size: c.searchContextSize }
|
|
96
|
+
}),
|
|
97
|
+
code_interpreter: (c) => ({
|
|
98
|
+
type: "code_interpreter",
|
|
99
|
+
container: c.fileIds !== void 0 ? {
|
|
100
|
+
type: "auto",
|
|
101
|
+
file_ids: c.fileIds
|
|
102
|
+
} : { type: "auto" }
|
|
103
|
+
}),
|
|
104
|
+
file_search: (c) => ({
|
|
105
|
+
type: "file_search",
|
|
106
|
+
vector_store_ids: c.vectorStoreIds
|
|
107
|
+
})
|
|
108
|
+
}));
|
|
109
|
+
const decodeConfig = Schema.decodeUnknownResult(ResponsesToolConfig);
|
|
110
|
+
const renderOne = (tool) => tool.provider !== "openai" ? Result.fail({
|
|
111
|
+
capability: `providerTool:${tool.provider}`,
|
|
112
|
+
reason: `tool "${tool.name}" is hosted by "${tool.provider}", not openai`
|
|
113
|
+
}) : decodeConfig(tool.config).pipe(Result.map(configToWire), Result.mapError(() => ({
|
|
114
|
+
capability: "providerTool",
|
|
115
|
+
reason: `unrecognized Responses provider tool config on "${tool.name}"`
|
|
116
|
+
})));
|
|
117
|
+
/**
|
|
118
|
+
* Render a toolkit's Responses provider tools to native `tools` entries. Fails
|
|
119
|
+
* with a `ToolRenderError` on the first foreign or unrecognized tool rather
|
|
120
|
+
* than dropping it.
|
|
121
|
+
*/
|
|
122
|
+
const renderProviderTools = (tools) => Result.all(tools.map(renderOne));
|
|
123
|
+
//#endregion
|
|
124
|
+
export { ResponsesToolConfig, codeInterpreterTool, fileSearchTool, renderProviderTools, ResponsesTools_exports as t, webSearchTool };
|
|
125
|
+
|
|
126
|
+
//# sourceMappingURL=ResponsesTools.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ResponsesTools.mjs","names":[],"sources":["../src/ResponsesTools.ts"],"sourcesContent":["import { Match, Result, Schema } from \"effect\"\nimport * as Tool from \"@effect-uai/core/Tool\"\n\n// ---------------------------------------------------------------------------\n// OpenAI Responses provider-hosted tools. Each is a `Tool.provider` whose\n// `config` carries the wire `type` string plus its options; the renderer maps\n// it to a native entry appended to the `tools` array alongside function tools.\n// The `inputSchema` is never sent (provider tools are partitioned out of the\n// function declarations), so `Tool.noInput` suffices.\n// Reference: https://developers.openai.com/api/docs/guides/tools\n// ---------------------------------------------------------------------------\n\nconst UserLocation = Schema.Struct({\n city: Schema.optional(Schema.String),\n region: Schema.optional(Schema.String),\n country: Schema.optional(Schema.String),\n timezone: Schema.optional(Schema.String),\n})\nexport type UserLocation = typeof UserLocation.Type\n\nconst WebSearchFilters = Schema.Struct({\n allowedDomains: Schema.optional(Schema.Array(Schema.String)),\n blockedDomains: Schema.optional(Schema.Array(Schema.String)),\n})\nexport type WebSearchFilters = typeof WebSearchFilters.Type\n\nconst WebSearchConfig = Schema.Struct({\n kind: Schema.Literal(\"web_search\"),\n filters: Schema.optional(WebSearchFilters),\n userLocation: Schema.optional(UserLocation),\n searchContextSize: Schema.optional(Schema.Literals([\"low\", \"medium\", \"high\"])),\n})\n\nconst CodeInterpreterConfig = Schema.Struct({\n kind: Schema.Literal(\"code_interpreter\"),\n fileIds: Schema.optional(Schema.Array(Schema.String)),\n})\n\nconst FileSearchConfig = Schema.Struct({\n kind: Schema.Literal(\"file_search\"),\n vectorStoreIds: Schema.Array(Schema.String),\n})\n\n/** Discriminated config for a Responses hosted tool. */\nexport const ResponsesToolConfig = Schema.Union([\n WebSearchConfig,\n CodeInterpreterConfig,\n FileSearchConfig,\n])\nexport type ResponsesToolConfig = typeof ResponsesToolConfig.Type\n\nexport type WebSearchOptions = {\n readonly filters?: WebSearchFilters\n readonly userLocation?: UserLocation\n readonly searchContextSize?: \"low\" | \"medium\" | \"high\"\n}\n\n/** OpenAI-hosted web search. */\nexport const webSearchTool = (opts: WebSearchOptions = {}) =>\n Tool.provider({\n name: \"web_search\",\n description: \"Search the web, executed by OpenAI.\",\n inputSchema: Tool.noInput,\n provider: \"openai\",\n config: { kind: \"web_search\", ...opts } as const,\n })\n\n/** OpenAI-hosted code interpreter (Python sandbox). */\nexport const codeInterpreterTool = (opts: { readonly fileIds?: ReadonlyArray<string> } = {}) =>\n Tool.provider({\n name: \"code_interpreter\",\n description: \"Run Python in a sandbox, executed by OpenAI.\",\n inputSchema: Tool.noInput,\n provider: \"openai\",\n config: { kind: \"code_interpreter\", ...opts } as const,\n })\n\n/** OpenAI-hosted file search over the given vector stores. */\nexport const fileSearchTool = (opts: { readonly vectorStoreIds: ReadonlyArray<string> }) =>\n Tool.provider({\n name: \"file_search\",\n description: \"Retrieve from uploaded files, executed by OpenAI.\",\n inputSchema: Tool.noInput,\n provider: \"openai\",\n config: { kind: \"file_search\", vectorStoreIds: opts.vectorStoreIds } as const,\n })\n\n/**\n * A provider tool this adapter cannot render — a foreign `provider` or an\n * unrecognized `config`. Carried as data so `Responses.ts` can lift it into a\n * typed `AiError.Unsupported`.\n */\nexport type ToolRenderError = {\n readonly capability: string\n readonly reason: string\n}\n\nconst filtersWire = (f: WebSearchFilters): Record<string, unknown> => ({\n ...(f.allowedDomains !== undefined && { allowed_domains: f.allowedDomains }),\n ...(f.blockedDomains !== undefined && { blocked_domains: f.blockedDomains }),\n})\n\nconst userLocationWire = (loc: UserLocation): Record<string, unknown> => ({\n type: \"approximate\",\n ...(loc.city !== undefined && { city: loc.city }),\n ...(loc.region !== undefined && { region: loc.region }),\n ...(loc.country !== undefined && { country: loc.country }),\n ...(loc.timezone !== undefined && { timezone: loc.timezone }),\n})\n\nconst configToWire = Match.type<ResponsesToolConfig>().pipe(\n Match.discriminatorsExhaustive(\"kind\")({\n web_search: (c): Record<string, unknown> => ({\n type: \"web_search\",\n ...(c.filters !== undefined && { filters: filtersWire(c.filters) }),\n ...(c.userLocation !== undefined && { user_location: userLocationWire(c.userLocation) }),\n ...(c.searchContextSize !== undefined && { search_context_size: c.searchContextSize }),\n }),\n code_interpreter: (c): Record<string, unknown> => ({\n type: \"code_interpreter\",\n container: c.fileIds !== undefined ? { type: \"auto\", file_ids: c.fileIds } : { type: \"auto\" },\n }),\n file_search: (c): Record<string, unknown> => ({\n type: \"file_search\",\n vector_store_ids: c.vectorStoreIds,\n }),\n }),\n)\n\nconst decodeConfig = Schema.decodeUnknownResult(ResponsesToolConfig)\n\nconst renderOne = (\n tool: Tool.ProviderTool<string, any, string, any>,\n): Result.Result<Record<string, unknown>, ToolRenderError> =>\n tool.provider !== \"openai\"\n ? Result.fail({\n capability: `providerTool:${tool.provider}`,\n reason: `tool \"${tool.name}\" is hosted by \"${tool.provider}\", not openai`,\n })\n : decodeConfig(tool.config).pipe(\n Result.map(configToWire),\n Result.mapError(\n (): ToolRenderError => ({\n capability: \"providerTool\",\n reason: `unrecognized Responses provider tool config on \"${tool.name}\"`,\n }),\n ),\n )\n\n/**\n * Render a toolkit's Responses provider tools to native `tools` entries. Fails\n * with a `ToolRenderError` on the first foreign or unrecognized tool rather\n * than dropping it.\n */\nexport const renderProviderTools = (\n tools: ReadonlyArray<Tool.ProviderTool<string, any, string, any>>,\n): Result.Result<ReadonlyArray<Record<string, unknown>>, ToolRenderError> =>\n Result.all(tools.map(renderOne))\n"],"mappings":";;;;;;;;;;;AAYA,MAAM,eAAe,OAAO,OAAO;CACjC,MAAM,OAAO,SAAS,OAAO,OAAO;CACpC,QAAQ,OAAO,SAAS,OAAO,OAAO;CACtC,SAAS,OAAO,SAAS,OAAO,OAAO;CACvC,UAAU,OAAO,SAAS,OAAO,OAAO;CACzC,CAAC;AAGF,MAAM,mBAAmB,OAAO,OAAO;CACrC,gBAAgB,OAAO,SAAS,OAAO,MAAM,OAAO,OAAO,CAAC;CAC5D,gBAAgB,OAAO,SAAS,OAAO,MAAM,OAAO,OAAO,CAAC;CAC7D,CAAC;AAGF,MAAM,kBAAkB,OAAO,OAAO;CACpC,MAAM,OAAO,QAAQ,aAAa;CAClC,SAAS,OAAO,SAAS,iBAAiB;CAC1C,cAAc,OAAO,SAAS,aAAa;CAC3C,mBAAmB,OAAO,SAAS,OAAO,SAAS;EAAC;EAAO;EAAU;EAAO,CAAC,CAAC;CAC/E,CAAC;AAEF,MAAM,wBAAwB,OAAO,OAAO;CAC1C,MAAM,OAAO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KACrD,MAAM,yBAAyB,OAAO,CAAC;CACrC,aAAa,OAAgC;EAC3C,MAAM;EACN,GAAI,EAAE,YAAY,KAAA,KAAa,EAAE,SAAS,YAAY,EAAE,QAAQ,EAAE;EAClE,GAAI,EAAE,iBAAiB,KAAA,KAAa,EAAE,eAAe,iBAAiB,EAAE,aAAa,EAAE;EACvF,GAAI,EAAE,sBAAsB,KAAA,KAAa,EAAE,qBAAqB,EAAE,mBAAmB;EACtF;CACD,mBAAmB,OAAgC;EACjD,MAAM;EACN,WAAW,EAAE,YAAY,KAAA,IAAY;GAAE,MAAM;GAAQ,UAAU,EAAE;GAAS,GAAG,EAAE,MAAM,QAAQ;EAC9F;CACD,cAAc,OAAgC;EAC5C,MAAM;EACN,kBAAkB,EAAE;EACrB;CACF,CAAC,CACH;AAED,MAAM,eAAe,OAAO,oBAAoB,oBAAoB;AAEpE,MAAM,aACJ,SAEA,KAAK,aAAa,WACd,OAAO,KAAK;CACV,YAAY,gBAAgB,KAAK;CACjC,QAAQ,SAAS,KAAK,KAAK,kBAAkB,KAAK,SAAS;CAC5D,CAAC,GACF,aAAa,KAAK,OAAO,CAAC,KACxB,OAAO,IAAI,aAAa,EACxB,OAAO,gBACmB;CACtB,YAAY;CACZ,QAAQ,mDAAmD,KAAK,KAAK;CACtE,EACF,CACF;;;;;;AAOP,MAAa,uBACX,UAEA,OAAO,IAAI,MAAM,IAAI,UAAU,CAAC"}
|
package/dist/codec.d.mts
CHANGED
|
@@ -4,8 +4,29 @@ import { HistoryItem } from "@effect-uai/core/Items";
|
|
|
4
4
|
|
|
5
5
|
//#region src/codec.d.ts
|
|
6
6
|
declare namespace codec_d_exports {
|
|
7
|
-
export { WireOutputItem, WireResponseCompleted, itemsToInput, turnFromCompleted, wireItemToItem };
|
|
7
|
+
export { WireAnnotation, WireOutputItem, WireResponseCompleted, itemsToInput, turnFromCompleted, wireItemToItem };
|
|
8
8
|
}
|
|
9
|
+
declare const WireAnnotation: Schema.Union<readonly [Schema.Struct<{
|
|
10
|
+
readonly type: Schema.Literal<"url_citation">;
|
|
11
|
+
readonly url: Schema.String;
|
|
12
|
+
readonly start_index: Schema.Number;
|
|
13
|
+
readonly end_index: Schema.Number;
|
|
14
|
+
readonly title: Schema.String;
|
|
15
|
+
}>, Schema.Struct<{
|
|
16
|
+
readonly type: Schema.Literal<"file_citation">;
|
|
17
|
+
readonly file_id: Schema.String;
|
|
18
|
+
readonly index: Schema.Number;
|
|
19
|
+
}>, Schema.Struct<{
|
|
20
|
+
readonly type: Schema.Literal<"container_file_citation">;
|
|
21
|
+
readonly container_id: Schema.String;
|
|
22
|
+
readonly file_id: Schema.String;
|
|
23
|
+
readonly start_index: Schema.Number;
|
|
24
|
+
readonly end_index: Schema.Number;
|
|
25
|
+
}>, Schema.Struct<{
|
|
26
|
+
readonly type: Schema.Literal<"file_path">;
|
|
27
|
+
readonly file_id: Schema.String;
|
|
28
|
+
readonly index: Schema.Number;
|
|
29
|
+
}>]>;
|
|
9
30
|
declare const WireOutputItem: Schema.Union<readonly [Schema.Struct<{
|
|
10
31
|
readonly type: Schema.Literal<"message">;
|
|
11
32
|
readonly id: Schema.optional<Schema.String>;
|
|
@@ -52,6 +73,15 @@ declare const WireOutputItem: Schema.Union<readonly [Schema.Struct<{
|
|
|
52
73
|
readonly text: Schema.String;
|
|
53
74
|
}>>>;
|
|
54
75
|
readonly encrypted_content: Schema.optional<Schema.String>;
|
|
76
|
+
}>, Schema.Struct<{
|
|
77
|
+
readonly type: Schema.Literal<"web_search_call">;
|
|
78
|
+
readonly id: Schema.optional<Schema.String>;
|
|
79
|
+
}>, Schema.Struct<{
|
|
80
|
+
readonly type: Schema.Literal<"code_interpreter_call">;
|
|
81
|
+
readonly id: Schema.optional<Schema.String>;
|
|
82
|
+
}>, Schema.Struct<{
|
|
83
|
+
readonly type: Schema.Literal<"file_search_call">;
|
|
84
|
+
readonly id: Schema.optional<Schema.String>;
|
|
55
85
|
}>]>;
|
|
56
86
|
type WireOutputItem = typeof WireOutputItem.Type;
|
|
57
87
|
declare const WireResponseCompleted: Schema.Struct<{
|
|
@@ -103,6 +133,15 @@ declare const WireResponseCompleted: Schema.Struct<{
|
|
|
103
133
|
readonly text: Schema.String;
|
|
104
134
|
}>>>;
|
|
105
135
|
readonly encrypted_content: Schema.optional<Schema.String>;
|
|
136
|
+
}>, Schema.Struct<{
|
|
137
|
+
readonly type: Schema.Literal<"web_search_call">;
|
|
138
|
+
readonly id: Schema.optional<Schema.String>;
|
|
139
|
+
}>, Schema.Struct<{
|
|
140
|
+
readonly type: Schema.Literal<"code_interpreter_call">;
|
|
141
|
+
readonly id: Schema.optional<Schema.String>;
|
|
142
|
+
}>, Schema.Struct<{
|
|
143
|
+
readonly type: Schema.Literal<"file_search_call">;
|
|
144
|
+
readonly id: Schema.optional<Schema.String>;
|
|
106
145
|
}>]>>>>;
|
|
107
146
|
readonly usage: Schema.optional<Schema.NullOr<Schema.Struct<{
|
|
108
147
|
readonly input_tokens: Schema.optional<Schema.Number>;
|
|
@@ -126,8 +165,8 @@ declare const WireResponseCompleted: Schema.Struct<{
|
|
|
126
165
|
type WireResponseCompleted = typeof WireResponseCompleted.Type;
|
|
127
166
|
/** Convert our `HistoryItem[]` history into the Responses API `input` array. */
|
|
128
167
|
declare const itemsToInput: (items: ReadonlyArray<HistoryItem>) => ReadonlyArray<Record<string, unknown>>;
|
|
129
|
-
declare const wireItemToItem: (wire: WireOutputItem) => HistoryItem
|
|
168
|
+
declare const wireItemToItem: (wire: WireOutputItem) => ReadonlyArray<HistoryItem>;
|
|
130
169
|
declare const turnFromCompleted: (payload: WireResponseCompleted) => Turn;
|
|
131
170
|
//#endregion
|
|
132
|
-
export { WireOutputItem, WireResponseCompleted, itemsToInput, codec_d_exports as t, turnFromCompleted, wireItemToItem };
|
|
171
|
+
export { WireAnnotation, WireOutputItem, WireResponseCompleted, itemsToInput, codec_d_exports as t, turnFromCompleted, wireItemToItem };
|
|
133
172
|
//# sourceMappingURL=codec.d.mts.map
|
package/dist/codec.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codec.d.mts","names":[],"sources":["../src/codec.ts"],"mappings":";;;;;;;;
|
|
1
|
+
{"version":3,"file":"codec.d.mts","names":[],"sources":["../src/codec.ts"],"mappings":";;;;;;;;cAqCa,cAAA,EAAc,MAAA,CAAA,KAAA,WAAA,MAAA,CAAA,MAAA;EAAA;;;;;;;;;;;;;;;;;;;;cAkEd,cAAA,EAAc,MAAA,CAAA,KAAA,WAAA,MAAA,CAAA,MAAA;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAQf,cAAA,UAAwB,cAAA,CAAe,IAAA;AAAA,cA6BtC,qBAAA,EAAqB,MAAA,CAAA,MAAA;EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KActB,qBAAA,UAA+B,qBAAA,CAAsB,IAAA;;cA0EpD,YAAA,GACX,KAAA,EAAO,aAAA,CAAc,WAAA,MACpB,aAAA,CAAc,MAAA;AAAA,cAiBJ,cAAA,GAAkB,IAAA,EAAM,cAAA,KAAiB,aAAA,CAAc,WAAA;AAAA,cAwEvD,iBAAA,GAAqB,OAAA,EAAS,qBAAA,KAAwB,IAAA"}
|
package/dist/codec.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import { t as __exportAll } from "./chunk-CfYAbeIz.mjs";
|
|
|
2
2
|
import { Encoding, Match, Option, Schema } from "effect";
|
|
3
3
|
//#region src/codec.ts
|
|
4
4
|
var codec_exports = /* @__PURE__ */ __exportAll({
|
|
5
|
+
WireAnnotation: () => WireAnnotation,
|
|
5
6
|
WireOutputItem: () => WireOutputItem,
|
|
6
7
|
WireResponseCompleted: () => WireResponseCompleted,
|
|
7
8
|
itemsToInput: () => itemsToInput,
|
|
@@ -71,10 +72,25 @@ const WireReasoning = Schema.Struct({
|
|
|
71
72
|
summary: Schema.optional(Schema.Array(WireSummaryText)),
|
|
72
73
|
encrypted_content: Schema.optional(Schema.String)
|
|
73
74
|
});
|
|
75
|
+
const WireWebSearchCall = Schema.Struct({
|
|
76
|
+
type: Schema.Literal("web_search_call"),
|
|
77
|
+
id: Schema.optional(Schema.String)
|
|
78
|
+
});
|
|
79
|
+
const WireCodeInterpreterCall = Schema.Struct({
|
|
80
|
+
type: Schema.Literal("code_interpreter_call"),
|
|
81
|
+
id: Schema.optional(Schema.String)
|
|
82
|
+
});
|
|
83
|
+
const WireFileSearchCall = Schema.Struct({
|
|
84
|
+
type: Schema.Literal("file_search_call"),
|
|
85
|
+
id: Schema.optional(Schema.String)
|
|
86
|
+
});
|
|
74
87
|
const WireOutputItem = Schema.Union([
|
|
75
88
|
WireMessage,
|
|
76
89
|
WireFunctionCall,
|
|
77
|
-
WireReasoning
|
|
90
|
+
WireReasoning,
|
|
91
|
+
WireWebSearchCall,
|
|
92
|
+
WireCodeInterpreterCall,
|
|
93
|
+
WireFileSearchCall
|
|
78
94
|
]);
|
|
79
95
|
const WireInputTokensDetails = Schema.Struct({ cached_tokens: Schema.optional(Schema.NullOr(Schema.Number)) });
|
|
80
96
|
const WireOutputTokensDetails = Schema.Struct({ reasoning_tokens: Schema.optional(Schema.NullOr(Schema.Number)) });
|
|
@@ -168,26 +184,29 @@ const wireMessageContentToBlock = Match.type().pipe(Match.discriminatorsExhausti
|
|
|
168
184
|
})
|
|
169
185
|
}));
|
|
170
186
|
const wireItemToItem = (wire) => Match.value(wire).pipe(Match.discriminatorsExhaustive("type")({
|
|
171
|
-
message: (m) =>
|
|
187
|
+
message: (m) => [{
|
|
172
188
|
type: "message",
|
|
173
189
|
role: m.role,
|
|
174
190
|
content: m.content.map(wireMessageContentToBlock),
|
|
175
191
|
providerData: m
|
|
176
|
-
}
|
|
177
|
-
function_call: (f) =>
|
|
192
|
+
}],
|
|
193
|
+
function_call: (f) => [{
|
|
178
194
|
type: "function_call",
|
|
179
195
|
call_id: f.call_id,
|
|
180
196
|
name: f.name,
|
|
181
197
|
arguments: f.arguments,
|
|
182
198
|
providerData: f
|
|
183
|
-
}
|
|
184
|
-
reasoning: (r) =>
|
|
199
|
+
}],
|
|
200
|
+
reasoning: (r) => [{
|
|
185
201
|
type: "reasoning",
|
|
186
202
|
...r.id !== void 0 && { id: r.id },
|
|
187
203
|
...r.summary !== void 0 && { summary: r.summary.map((s) => s.text).join("\n") },
|
|
188
204
|
...r.encrypted_content !== void 0 && { signature: r.encrypted_content },
|
|
189
205
|
providerData: r
|
|
190
|
-
}
|
|
206
|
+
}],
|
|
207
|
+
web_search_call: () => [],
|
|
208
|
+
code_interpreter_call: () => [],
|
|
209
|
+
file_search_call: () => []
|
|
191
210
|
}));
|
|
192
211
|
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
212
|
const hasRefusalContent = (payload) => (payload.output ?? []).some((i) => i.type === "message" && i.content.some((c) => c.type === "refusal"));
|
|
@@ -201,7 +220,7 @@ const turnFromCompleted = (payload) => {
|
|
|
201
220
|
const cached = cachedTokens(payload);
|
|
202
221
|
const reasoning = reasoningTokens(payload);
|
|
203
222
|
return {
|
|
204
|
-
items: (payload.output ?? []).
|
|
223
|
+
items: (payload.output ?? []).flatMap(wireItemToItem),
|
|
205
224
|
usage: {
|
|
206
225
|
...payload.usage?.input_tokens !== void 0 && { input_tokens: payload.usage.input_tokens },
|
|
207
226
|
...payload.usage?.output_tokens !== void 0 && { output_tokens: payload.usage.output_tokens },
|
|
@@ -213,6 +232,6 @@ const turnFromCompleted = (payload) => {
|
|
|
213
232
|
};
|
|
214
233
|
};
|
|
215
234
|
//#endregion
|
|
216
|
-
export { WireOutputItem, WireResponseCompleted, itemsToInput, codec_exports as t, turnFromCompleted, wireItemToItem };
|
|
235
|
+
export { WireAnnotation, WireOutputItem, WireResponseCompleted, itemsToInput, codec_exports as t, turnFromCompleted, wireItemToItem };
|
|
217
236
|
|
|
218
237
|
//# sourceMappingURL=codec.mjs.map
|
package/dist/codec.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codec.mjs","names":[],"sources":["../src/codec.ts"],"sourcesContent":["import { Encoding, Match, Option, Schema } from \"effect\"\nimport type { ContentBlock, InputImage, HistoryItem } 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: HistoryItem): 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: HistoryItem): 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 `HistoryItem[]` history into the Responses API `input` array. */\nexport const itemsToInput = (\n items: ReadonlyArray<HistoryItem>,\n): ReadonlyArray<Record<string, unknown>> => 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): HistoryItem =>\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,C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Q,MAAM,MAAM,EAAE,SAAS,UAAU,CAC3E;AAEH,MAAM,2BAA2B,YAC/B,OAAO,MAAM,aAAa,QAAQ,oBAAoB,UAAU,GAAG,EAAE;CACnE,SAAS,MAAM;CACf,cACE,kBAAkB,QAAQ,GACrB,aACA,QAAQ,UAAU,EAAE,EAAE,MAAM,MAAM,EAAE,SAAS,gBAAgB,GAC3D,eACA;CACV,CAAC;AAEJ,MAAM,gBAAgB,YACpB,QAAQ,OAAO,sBAAsB,iBAAiB,KAAA;AAExD,MAAM,mBAAmB,YACvB,QAAQ,OAAO,uBAAuB,oBAAoB,KAAA;AAE5D,MAAa,qBAAqB,YAAyC;CACzE,MAAM,SAAS,aAAa,QAAQ;CACpC,MAAM,YAAY,gBAAgB,QAAQ;AAC1C,QAAO;EACL,QAAQ,QAAQ,UAAU,EAAE,EAAE,IAAI,eAAe;EACjD,OAAO;GACL,GAAI,QAAQ,OAAO,iBAAiB,KAAA,KAAa,EAC/C,cAAc,QAAQ,MAAM,cAC7B;GACD,GAAI,QAAQ,OAAO,kBAAkB,KAAA,KAAa,EAChD,eAAe,QAAQ,MAAM,eAC9B;GACD,GAAI,QAAQ,OAAO,iBAAiB,KAAA,KAAa,EAC/C,cAAc,QAAQ,MAAM,cAC7B;GACD,GAAI,WAAW,KAAA,KAAa,EAC1B,sBAAsB,EAAE,eAAe,QAAQ,EAChD;GACD,GAAI,cAAc,KAAA,KAAa,EAC7B,uBAAuB,EAAE,kBAAkB,WAAW,EACvD;GACF;EACD,aAAa,wBAAwB,QAAQ;EAC9C"}
|
|
1
|
+
{"version":3,"file":"codec.mjs","names":[],"sources":["../src/codec.ts"],"sourcesContent":["import { Encoding, Match, Option, Schema } from \"effect\"\nimport type { ContentBlock, InputImage, HistoryItem } 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\nexport const 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\n// Provider-hosted tool call items (web search, code interpreter, file search).\n// The provider executes them, not us — we model them only so a grounded\n// `response.completed` still decodes and assembles a Turn; they are dropped\n// from the turn's items in Phase 1. Surfacing them is a follow-up.\nconst WireWebSearchCall = Schema.Struct({\n type: Schema.Literal(\"web_search_call\"),\n id: Schema.optional(Schema.String),\n})\n\nconst WireCodeInterpreterCall = Schema.Struct({\n type: Schema.Literal(\"code_interpreter_call\"),\n id: Schema.optional(Schema.String),\n})\n\nconst WireFileSearchCall = Schema.Struct({\n type: Schema.Literal(\"file_search_call\"),\n id: Schema.optional(Schema.String),\n})\n\nexport const WireOutputItem = Schema.Union([\n WireMessage,\n WireFunctionCall,\n WireReasoning,\n WireWebSearchCall,\n WireCodeInterpreterCall,\n WireFileSearchCall,\n])\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: HistoryItem): 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: HistoryItem): 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 `HistoryItem[]` history into the Responses API `input` array. */\nexport const itemsToInput = (\n items: ReadonlyArray<HistoryItem>,\n): ReadonlyArray<Record<string, unknown>> => 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): ReadonlyArray<HistoryItem> =>\n Match.value(wire).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n message: (m): ReadonlyArray<HistoryItem> => [\n {\n type: \"message\" as const,\n role: m.role,\n content: m.content.map(wireMessageContentToBlock),\n providerData: m,\n },\n ],\n function_call: (f): ReadonlyArray<HistoryItem> => [\n {\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 ],\n reasoning: (r): ReadonlyArray<HistoryItem> => [\n {\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 // Hosted-tool calls are provider-executed; drop from the turn (Phase 1).\n web_search_call: (): ReadonlyArray<HistoryItem> => [],\n code_interpreter_call: (): ReadonlyArray<HistoryItem> => [],\n file_search_call: (): ReadonlyArray<HistoryItem> => [],\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 ?? []).flatMap(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":";;;;;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,EAAE,QAAQ,eAAe;EACrD,OAAO;GACL,GAAI,QAAQ,OAAO,iBAAiB,KAAA,KAAa,EAC/C,cAAc,QAAQ,MAAM,cAC7B;GACD,GAAI,QAAQ,OAAO,kBAAkB,KAAA,KAAa,EAChD,eAAe,QAAQ,MAAM,eAC9B;GACD,GAAI,QAAQ,OAAO,iBAAiB,KAAA,KAAa,EAC/C,cAAc,QAAQ,MAAM,cAC7B;GACD,GAAI,WAAW,KAAA,KAAa,EAC1B,sBAAsB,EAAE,eAAe,QAAQ,EAChD;GACD,GAAI,cAAc,KAAA,KAAa,EAC7B,uBAAuB,EAAE,kBAAkB,WAAW,EACvD;GACF;EACD,aAAa,wBAAwB,QAAQ;EAC9C"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as codec_d_exports } from "./codec.mjs";
|
|
2
|
+
import { OpenAIDeepResearchModel, OpenAIEmbeddingModel, OpenAIModel } from "./models.mjs";
|
|
2
3
|
import { OpenAiRegion, resolveHost } from "./region.mjs";
|
|
4
|
+
import { t as OpenAIDeepResearch_d_exports } from "./OpenAIDeepResearch.mjs";
|
|
3
5
|
import { t as OpenAIEmbedding_d_exports } from "./OpenAIEmbedding.mjs";
|
|
6
|
+
import { t as ResponsesTools_d_exports } from "./ResponsesTools.mjs";
|
|
4
7
|
import { t as streamEvents_d_exports } from "./streamEvents.mjs";
|
|
5
8
|
import { t as Responses_d_exports } from "./Responses.mjs";
|
|
6
|
-
|
|
7
|
-
export { OpenAIEmbedding_d_exports as OpenAIEmbedding, OpenAIEmbeddingModel, OpenAIModel, OpenAiRegion, Responses_d_exports as Responses, codec_d_exports as codec, resolveHost, streamEvents_d_exports as streamEvents };
|
|
9
|
+
export { OpenAIDeepResearch_d_exports as OpenAIDeepResearch, OpenAIDeepResearchModel, OpenAIEmbedding_d_exports as OpenAIEmbedding, OpenAIEmbeddingModel, OpenAIModel, OpenAiRegion, Responses_d_exports as Responses, ResponsesTools_d_exports as ResponsesTools, codec_d_exports as codec, resolveHost, streamEvents_d_exports as streamEvents };
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { resolveHost } from "./region.mjs";
|
|
2
|
-
import { t as OpenAIEmbedding_exports } from "./OpenAIEmbedding.mjs";
|
|
3
1
|
import { t as codec_exports } from "./codec.mjs";
|
|
2
|
+
import { resolveHost } from "./region.mjs";
|
|
3
|
+
import { t as ResponsesTools_exports } from "./ResponsesTools.mjs";
|
|
4
4
|
import { t as streamEvents_exports } from "./streamEvents.mjs";
|
|
5
5
|
import { t as Responses_exports } from "./Responses.mjs";
|
|
6
|
+
import { t as OpenAIDeepResearch_exports } from "./OpenAIDeepResearch.mjs";
|
|
7
|
+
import { t as OpenAIEmbedding_exports } from "./OpenAIEmbedding.mjs";
|
|
6
8
|
import "./models.mjs";
|
|
7
|
-
export { OpenAIEmbedding_exports as OpenAIEmbedding, Responses_exports as Responses, codec_exports as codec, resolveHost, streamEvents_exports as streamEvents };
|
|
9
|
+
export { OpenAIDeepResearch_exports as OpenAIDeepResearch, OpenAIEmbedding_exports as OpenAIEmbedding, Responses_exports as Responses, ResponsesTools_exports as ResponsesTools, codec_exports as codec, resolveHost, streamEvents_exports as streamEvents };
|