@ank1015/agents-provider-openai 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @ank1015/agents-provider-openai
2
+
3
+ ## 0.1.0
4
+
5
+ - Added support for direct-value API key secret references when creating the default OpenAI SDK client.
6
+ - Initial OpenAI Responses API runtime adapter.
7
+ - Added request mapping, stream translation, error normalization, factory helpers, and unit tests.
package/README.md ADDED
@@ -0,0 +1,159 @@
1
+ # @ank1015/agents-provider-openai
2
+
3
+ OpenAI Responses API runtime adapter for the `@ank1015/agents` packages.
4
+
5
+ This package turns normalized `LLMRequest<"openai">` objects into OpenAI Responses API streaming calls and translates OpenAI stream events back into normalized assistant events and messages. It also re-exports `@ank1015/agents-provider-openai-spec` so consumers can import the provider id, config schema, model catalog, and OpenAI-specific types from one package.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ pnpm add @ank1015/agents-provider-openai
11
+ ```
12
+
13
+ ## What This Package Provides
14
+
15
+ - `OpenAIProviderAdapter`, a provider adapter for direct OpenAI API calls.
16
+ - `createOpenAIProviderAdapter` helper for app wiring.
17
+ - `createOpenAIProviderAdapterFactory` for transport/container integration.
18
+ - `OpenAIResponsesClient` and `OpenAIProviderAdapterDeps` test/injection types.
19
+ - Re-exports from `@ank1015/agents-provider-openai-spec`, including `OPENAI_PROVIDER`, `OPENAI_MODELS`, and config/model types.
20
+
21
+ ## Import Paths
22
+
23
+ ```ts
24
+ import {
25
+ OPENAI_PROVIDER,
26
+ createOpenAIProviderAdapter,
27
+ } from '@ank1015/agents-provider-openai';
28
+
29
+ import { OpenAIProviderAdapter } from '@ank1015/agents-provider-openai/adapter';
30
+ ```
31
+
32
+ The root import is the normal application entrypoint. The `./adapter` subpath exposes the runtime adapter APIs without making internal mapping helpers public.
33
+
34
+ ## Basic Usage
35
+
36
+ ```ts
37
+ import { createAdapterTransport } from '@ank1015/agents-core/transport';
38
+ import {
39
+ OPENAI_PROVIDER,
40
+ createOpenAIProviderAdapter,
41
+ } from '@ank1015/agents-provider-openai';
42
+
43
+ const openai = createOpenAIProviderAdapter({
44
+ provider: OPENAI_PROVIDER,
45
+ apiKey: {
46
+ type: 'env',
47
+ name: 'OPENAI_API_KEY',
48
+ },
49
+ });
50
+
51
+ const transport = createAdapterTransport([openai]);
52
+
53
+ const stream = transport.stream({
54
+ provider: OPENAI_PROVIDER,
55
+ modelId: 'gpt-5.4-mini',
56
+ messages: [
57
+ {
58
+ role: 'user',
59
+ id: 'user_1',
60
+ timestamp: Date.now(),
61
+ content: [{ type: 'text', content: 'Write a short haiku about types.' }],
62
+ },
63
+ ],
64
+ });
65
+
66
+ for await (const event of stream) {
67
+ if (event.type === 'text_delta') {
68
+ process.stdout.write(event.delta);
69
+ }
70
+ }
71
+
72
+ const message = await stream.result();
73
+ ```
74
+
75
+ ## Configuration
76
+
77
+ The adapter accepts `OpenAIProviderConfig` from the spec package:
78
+
79
+ ```ts
80
+ import { createOpenAIProviderAdapter } from '@ank1015/agents-provider-openai';
81
+
82
+ const adapter = createOpenAIProviderAdapter({
83
+ provider: 'openai',
84
+ apiKey: {
85
+ type: 'env',
86
+ name: 'OPENAI_API_KEY',
87
+ },
88
+ baseUrl: 'https://api.openai.com/v1',
89
+ headers: {
90
+ 'x-app': 'agents',
91
+ },
92
+ organization: 'org_123',
93
+ project: 'proj_123',
94
+ });
95
+ ```
96
+
97
+ `apiKey` is a secret reference. The adapter resolves environment references from `process.env`, or uses a direct value reference when the key comes from another secret source:
98
+
99
+ ```ts
100
+ createOpenAIProviderAdapter({
101
+ provider: 'openai',
102
+ apiKey: {
103
+ type: 'value',
104
+ value: openAIKey,
105
+ },
106
+ });
107
+ ```
108
+
109
+ ## Request Mapping
110
+
111
+ The adapter maps the normalized request shape into OpenAI Responses API streaming parameters:
112
+
113
+ - `instructions` becomes OpenAI `instructions`.
114
+ - Text and image user content becomes OpenAI response input content.
115
+ - Tool result messages become function or custom tool call output items.
116
+ - Prior OpenAI-native assistant responses are reused when available.
117
+ - Normalized assistant text, thinking, and tool calls are converted back into OpenAI response input history.
118
+ - Function tools become OpenAI function tools with JSON Schema parameters.
119
+ - `strict` is preserved when a function tool sets it to `true` or `false`.
120
+ - Custom grammar tools become OpenAI custom tools.
121
+
122
+ `providerOptions` are forwarded to OpenAI after the generic fields owned by the adapter are removed. The adapter does not default `max_output_tokens`; callers can set it through `providerOptions`.
123
+
124
+ ## Streaming Behavior
125
+
126
+ `adapter.stream(request)` returns an `AssistantMessageEventStreamSource<"openai">`.
127
+
128
+ The stream may emit:
129
+
130
+ - `start`
131
+ - `text_start`, `text_delta`, `text_end`
132
+ - `thinking_start`, `thinking_delta`, `thinking_end`
133
+ - `toolcall_start`, `toolcall_delta`, `toolcall_end`
134
+ - `done`
135
+ - `error`
136
+ - `aborted`
137
+
138
+ Successful messages include the OpenAI native response, normalized assistant content, usage, cost estimates from the static model catalog, duration, and mapped stop reason. Failed or aborted messages preserve any normalized content that was produced before the stream ended.
139
+
140
+ ## Testing And Injection
141
+
142
+ The adapter accepts dependency overrides for unit tests and custom runtime containers:
143
+
144
+ ```ts
145
+ import { OpenAIProviderAdapter } from '@ank1015/agents-provider-openai/adapter';
146
+
147
+ const adapter = new OpenAIProviderAdapter(config, {
148
+ client: fakeOpenAIClient,
149
+ createMessageId: () => 'msg_test',
150
+ now: () => 1_700_000_000_000,
151
+ resolveModel: (modelId) => modelRegistry.get('openai', modelId),
152
+ });
153
+ ```
154
+
155
+ If no client override is provided, the adapter creates an `openai` SDK client from the provider config.
156
+
157
+ ## Versioning
158
+
159
+ This package is currently `0.1.0`. Until `1.0.0`, adapter APIs and event mapping behavior may evolve as the surrounding agent runtime settles.
@@ -0,0 +1,33 @@
1
+ import type { AssistantMessageEventStreamSource, LLMProviderAdapter, LLMProviderAdapterFactory, LLMRequest, Model } from '@ank1015/agents-contracts';
2
+ import type { ResponseCreateParamsStreaming, ResponseStreamEvent } from 'openai/resources/responses/responses.js';
3
+ import type { OpenAIProvider, OpenAIProviderConfig } from '@ank1015/agents-provider-openai-spec';
4
+ export interface OpenAIResponsesClient {
5
+ responses: {
6
+ create(params: ResponseCreateParamsStreaming, options?: {
7
+ signal?: AbortSignal;
8
+ }): AsyncIterable<ResponseStreamEvent> | Promise<AsyncIterable<ResponseStreamEvent>>;
9
+ };
10
+ }
11
+ export interface OpenAIProviderAdapterDeps {
12
+ readonly client?: OpenAIResponsesClient;
13
+ readonly createClient?: (config: OpenAIProviderConfig) => OpenAIResponsesClient;
14
+ readonly createMessageId?: () => string;
15
+ readonly now?: () => number;
16
+ /** Resolves a model id to its descriptor. Defaults to the package's static catalog. */
17
+ readonly resolveModel?: (modelId: string) => Model<OpenAIProvider> | undefined;
18
+ }
19
+ export declare class OpenAIProviderAdapter implements LLMProviderAdapter<OpenAIProvider> {
20
+ #private;
21
+ readonly provider: "openai";
22
+ constructor(config: OpenAIProviderConfig, deps?: OpenAIProviderAdapterDeps);
23
+ stream(request: LLMRequest<OpenAIProvider>): AssistantMessageEventStreamSource<OpenAIProvider>;
24
+ }
25
+ export declare class OpenAIProviderAdapterFactory implements LLMProviderAdapterFactory<OpenAIProvider> {
26
+ #private;
27
+ readonly provider: "openai";
28
+ constructor(deps?: OpenAIProviderAdapterDeps);
29
+ create(config: OpenAIProviderConfig): OpenAIProviderAdapter;
30
+ }
31
+ export declare function createOpenAIProviderAdapter(config: OpenAIProviderConfig, deps?: OpenAIProviderAdapterDeps): OpenAIProviderAdapter;
32
+ export declare function createOpenAIProviderAdapterFactory(deps?: OpenAIProviderAdapterDeps): OpenAIProviderAdapterFactory;
33
+ //# sourceMappingURL=adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAKV,iCAAiC,EAMjC,kBAAkB,EAClB,yBAAyB,EACzB,UAAU,EACV,KAAK,EAIN,MAAM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