@robota-sdk/agent-provider-openai 3.0.0-beta.63 → 3.0.0-beta.82
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -21
- package/dist/node/index.cjs +1 -2
- package/dist/node/index.d.cts +271 -267
- package/dist/node/index.d.cts.map +1 -0
- package/dist/node/index.d.ts +271 -267
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +2 -2
- package/dist/node/index.js.map +1 -0
- package/dist/node/loggers/index.cjs +1 -0
- package/dist/node/loggers/index.d.cts +94 -0
- package/dist/node/loggers/index.d.cts.map +1 -0
- package/dist/node/loggers/index.d.ts +94 -0
- package/dist/node/loggers/index.d.ts.map +1 -0
- package/dist/node/loggers/index.js +2 -0
- package/dist/node/loggers/index.js.map +1 -0
- package/dist/node/payload-logger-BaW0K8yI.d.cts +61 -0
- package/dist/node/payload-logger-BaW0K8yI.d.cts.map +1 -0
- package/dist/node/payload-logger-BaW0K8yI.d.ts +61 -0
- package/dist/node/payload-logger-BaW0K8yI.d.ts.map +1 -0
- package/dist/node/rolldown-runtime-CMqjfN_6.cjs +1 -0
- package/package.json +69 -86
- package/src/index.ts +1 -0
- package/src/openai/__tests__/endpoint-provenance.test.ts +44 -0
- package/src/openai/__tests__/provider-errors.test.ts +114 -0
- package/src/openai/__tests__/request-format.test.ts +59 -0
- package/src/openai/__tests__/strict-tools-closure.test.ts +136 -0
- package/src/openai/__tests__/tool-schema-projection.test.ts +302 -0
- package/src/openai/__tests__/trace-context-wire.test.ts +114 -0
- package/src/openai/adapter.test.ts +494 -0
- package/src/openai/adapter.ts +145 -0
- package/src/openai/chat-completions-chat.ts +225 -0
- package/src/openai/executor-integration.test.ts +213 -0
- package/src/openai/index.ts +19 -0
- package/src/openai/interfaces/payload-logger.ts +48 -0
- package/src/openai/loggers/console-payload-logger.test.ts +173 -0
- package/src/openai/loggers/console-payload-logger.ts +96 -0
- package/src/openai/loggers/console.ts +9 -0
- package/src/openai/loggers/file-payload-logger.test.ts +243 -0
- package/src/openai/loggers/file-payload-logger.ts +124 -0
- package/src/openai/loggers/file.ts +9 -0
- package/src/openai/loggers/index.ts +12 -0
- package/src/openai/loggers/sanitize-openai-log-data.test.ts +89 -0
- package/src/openai/loggers/sanitize-openai-log-data.ts +14 -0
- package/src/openai/message-converter.ts +23 -0
- package/src/openai/model-effort-table.ts +19 -0
- package/src/openai/model-effort-verification-config.test.ts +45 -0
- package/src/openai/model-effort-verification-config.ts +35 -0
- package/src/openai/openai-request-format.ts +116 -0
- package/src/openai/parsers/response-parser.test.ts +407 -0
- package/src/openai/parsers/response-parser.ts +48 -0
- package/src/openai/provider-definition.test.ts +74 -0
- package/src/openai/provider-definition.ts +139 -0
- package/src/openai/provider.test.ts +1686 -0
- package/src/openai/provider.ts +392 -0
- package/src/openai/reasoning-effort.test.ts +272 -0
- package/src/openai/reasoning-effort.ts +49 -0
- package/src/openai/request-id.ts +39 -0
- package/src/openai/request-options.ts +14 -0
- package/src/openai/responses-chat.ts +285 -0
- package/src/openai/responses-converter.ts +122 -0
- package/src/openai/responses-parser.ts +297 -0
- package/src/openai/responses-stream-utils.ts +45 -0
- package/src/openai/responses-types.ts +193 -0
- package/src/openai/streaming/stream-assembler.ts +3 -0
- package/src/openai/types/api-types.ts +113 -0
- package/src/openai/types.ts +235 -0
- package/CHANGELOG.md +0 -614
- package/README.md +0 -467
- package/dist/browser/index.d.ts +0 -307
- package/dist/browser/index.js +0 -2
package/README.md
DELETED
|
@@ -1,467 +0,0 @@
|
|
|
1
|
-
# @robota-sdk/agent-provider-openai
|
|
2
|
-
|
|
3
|
-
OpenAI Provider for Robota SDK - type-safe integration with the official OpenAI Responses API, structured outputs, streaming, and function calling.
|
|
4
|
-
|
|
5
|
-
## 🚀 Features
|
|
6
|
-
|
|
7
|
-
### Core Capabilities
|
|
8
|
-
|
|
9
|
-
- **🎯 Type-Safe Integration**: Complete TypeScript support with zero `any` types
|
|
10
|
-
- **🤖 OpenAI Model Support**: Official OpenAI models through the Responses API by default
|
|
11
|
-
- **⚡ Real-Time Streaming**: Asynchronous streaming responses with proper error handling
|
|
12
|
-
- **🛠️ Function Calling**: Native OpenAI function calling with type validation
|
|
13
|
-
- **🧩 Structured Outputs**: JSON object and JSON Schema response formats
|
|
14
|
-
- **🌐 Capability Reporting**: Explicit native web search/fetch capability state without hidden local-tool fallback
|
|
15
|
-
- **Provider-Owned Model Catalog Refresh**: `/model` can discover OpenAI models through the provider definition without CLI/TUI-owned model lists
|
|
16
|
-
- **🔄 Provider-Agnostic Design**: Seamless integration with other Robota providers
|
|
17
|
-
- **📊 Payload Logging**: Optional API request/response logging for debugging
|
|
18
|
-
|
|
19
|
-
### Architecture Highlights
|
|
20
|
-
|
|
21
|
-
- **Provider Base Contract**: `AbstractAIProvider` implementation for the Robota provider interface
|
|
22
|
-
- **Facade Pattern**: Modular design with separated concerns
|
|
23
|
-
- **Error Safety**: Comprehensive error handling without any-type compromises
|
|
24
|
-
- **OpenAI SDK Compatibility**: Direct integration with official OpenAI SDK types
|
|
25
|
-
|
|
26
|
-
## 📦 Installation
|
|
27
|
-
|
|
28
|
-
```bash
|
|
29
|
-
npm install @robota-sdk/agent-provider-openai @robota-sdk/agent-core openai
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
## 🔧 Basic Usage
|
|
33
|
-
|
|
34
|
-
### Simple Chat Integration
|
|
35
|
-
|
|
36
|
-
```typescript
|
|
37
|
-
import { Robota } from '@robota-sdk/agent-core';
|
|
38
|
-
import { OpenAIProvider } from '@robota-sdk/agent-provider-openai';
|
|
39
|
-
|
|
40
|
-
// Create type-safe OpenAI provider
|
|
41
|
-
const provider = new OpenAIProvider({
|
|
42
|
-
apiKey: process.env.OPENAI_API_KEY,
|
|
43
|
-
defaultModel: 'gpt-4o',
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
// Create Robota agent with OpenAI provider
|
|
47
|
-
const agent = new Robota({
|
|
48
|
-
name: 'MyAgent',
|
|
49
|
-
aiProviders: [provider],
|
|
50
|
-
defaultModel: {
|
|
51
|
-
provider: 'openai',
|
|
52
|
-
model: 'gpt-4o',
|
|
53
|
-
temperature: 0.7,
|
|
54
|
-
systemMessage: 'You are a helpful AI assistant specialized in technical topics.',
|
|
55
|
-
},
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
// Execute conversation
|
|
59
|
-
const response = await agent.run('Explain the benefits of TypeScript over JavaScript');
|
|
60
|
-
console.log(response);
|
|
61
|
-
|
|
62
|
-
// Clean up
|
|
63
|
-
await agent.destroy();
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
### OpenAI-Compatible Endpoints
|
|
67
|
-
|
|
68
|
-
The official OpenAI profile uses the Responses API by default. Set `baseURL`, or set `apiSurface: 'chat-completions'`, only when you intentionally need an OpenAI-compatible Chat Completions endpoint. For LM Studio, the local API typically listens on `http://localhost:1234/v1` and accepts a local placeholder API key:
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
|
-
import { OpenAIProvider } from '@robota-sdk/agent-provider-openai';
|
|
72
|
-
|
|
73
|
-
const provider = new OpenAIProvider({
|
|
74
|
-
apiKey: 'lm-studio',
|
|
75
|
-
baseURL: 'http://localhost:1234/v1',
|
|
76
|
-
defaultModel: '<local-openai-compatible-model>',
|
|
77
|
-
});
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
Gemma-family local models should use `@robota-sdk/agent-provider-gemma` instead of this
|
|
81
|
-
OpenAI provider so Gemma chat-template channel markers are projected out of user-facing
|
|
82
|
-
streamed text.
|
|
83
|
-
|
|
84
|
-
For provider packages that share OpenAI-compatible transport code, the reusable primitives live in `@robota-sdk/agent-provider-openai-compatible`. This package owns OpenAI product semantics and an explicit compatibility mode; model-family behavior such as Gemma reasoning/tool-call projection belongs in that model-family provider.
|
|
85
|
-
|
|
86
|
-
OpenAI-compatible Chat Completions profiles, including LM Studio-style `baseURL` profiles, are custom function-tool capable but are not treated as provider-native hosted web search/fetch providers. Use Robota local `WebSearch`/`WebFetch` tools for explicit local web access unless a concrete provider package documents hosted web support.
|
|
87
|
-
|
|
88
|
-
### Model Catalog Refresh
|
|
89
|
-
|
|
90
|
-
`createOpenAIProviderDefinition()` exposes provider-owned setup help links and a
|
|
91
|
-
provider-owned `refreshModelCatalog` hook. SDK command
|
|
92
|
-
common APIs may call this hook to query the OpenAI Models API with the effective provider profile and
|
|
93
|
-
surface catalog freshness in `/model` output. CLI/TUI layers must render that result rather than
|
|
94
|
-
owning OpenAI model metadata.
|
|
95
|
-
|
|
96
|
-
### Streaming Responses
|
|
97
|
-
|
|
98
|
-
```typescript
|
|
99
|
-
// Real-time streaming for immediate feedback
|
|
100
|
-
const stream = await agent.runStream('Write a detailed explanation of machine learning');
|
|
101
|
-
|
|
102
|
-
for await (const chunk of stream) {
|
|
103
|
-
if (chunk.content) {
|
|
104
|
-
process.stdout.write(chunk.content);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Handle streaming metadata
|
|
108
|
-
if (chunk.metadata?.isComplete) {
|
|
109
|
-
console.log('\n✓ Stream completed');
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
When `chat()` receives an `onTextDelta` callback, the provider uses the selected API surface's streaming path internally, forwards text deltas to the callback, assembles streamed tool-call chunks, and returns the final assistant message.
|
|
115
|
-
|
|
116
|
-
### Native Replay Payload Capture
|
|
117
|
-
|
|
118
|
-
When `IChatOptions.onProviderNativeRawPayload` is provided, the provider emits exact OpenAI SDK request, response, and stream event payloads with `apiSurface` set to either `responses` or `chat-completions`. `agent-core` routes these provider-owned callbacks into provider-neutral `provider_native_raw_payload` execution events for replay-grade session logs.
|
|
119
|
-
|
|
120
|
-
## 🛠️ Function Calling
|
|
121
|
-
|
|
122
|
-
OpenAI Provider supports type-safe function calling with automatic parameter validation:
|
|
123
|
-
|
|
124
|
-
```typescript
|
|
125
|
-
import { FunctionTool } from '@robota-sdk/agent-core';
|
|
126
|
-
import { z } from 'zod';
|
|
127
|
-
|
|
128
|
-
// Define type-safe function tools
|
|
129
|
-
const weatherTool = new FunctionTool({
|
|
130
|
-
name: 'getWeather',
|
|
131
|
-
description: 'Get current weather information for a location',
|
|
132
|
-
parameters: z.object({
|
|
133
|
-
location: z.string().describe('City name'),
|
|
134
|
-
unit: z.enum(['celsius', 'fahrenheit']).default('celsius'),
|
|
135
|
-
}),
|
|
136
|
-
handler: async ({ location, unit }) => {
|
|
137
|
-
// Type-safe handler implementation
|
|
138
|
-
const weatherData = await fetchWeatherAPI(location, unit);
|
|
139
|
-
return {
|
|
140
|
-
temperature: weatherData.temp,
|
|
141
|
-
condition: weatherData.condition,
|
|
142
|
-
location,
|
|
143
|
-
unit,
|
|
144
|
-
};
|
|
145
|
-
},
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
const calculatorTool = new FunctionTool({
|
|
149
|
-
name: 'calculate',
|
|
150
|
-
description: 'Perform mathematical operations',
|
|
151
|
-
parameters: z.object({
|
|
152
|
-
operation: z.enum(['add', 'subtract', 'multiply', 'divide']),
|
|
153
|
-
a: z.number(),
|
|
154
|
-
b: z.number(),
|
|
155
|
-
}),
|
|
156
|
-
handler: async ({ operation, a, b }) => {
|
|
157
|
-
const operations = {
|
|
158
|
-
add: a + b,
|
|
159
|
-
subtract: a - b,
|
|
160
|
-
multiply: a * b,
|
|
161
|
-
divide: a / b,
|
|
162
|
-
};
|
|
163
|
-
return { result: operations[operation] };
|
|
164
|
-
},
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
// Register tools with the agent
|
|
168
|
-
agent.registerTool(weatherTool);
|
|
169
|
-
agent.registerTool(calculatorTool);
|
|
170
|
-
|
|
171
|
-
// Execute with function calling
|
|
172
|
-
const result = await agent.run("What's the weather in Tokyo and what's 25 * 4?");
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
## 🔄 Multi-Provider Architecture
|
|
176
|
-
|
|
177
|
-
Seamlessly integrate with other providers:
|
|
178
|
-
|
|
179
|
-
```typescript
|
|
180
|
-
import { AnthropicProvider } from '@robota-sdk/agent-provider-anthropic';
|
|
181
|
-
import { GoogleProvider } from '@robota-sdk/agent-provider-google';
|
|
182
|
-
|
|
183
|
-
const openaiProvider = new OpenAIProvider({
|
|
184
|
-
apiKey: process.env.OPENAI_API_KEY,
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
const anthropicProvider = new AnthropicProvider({
|
|
188
|
-
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
const googleProvider = new GoogleProvider({
|
|
192
|
-
apiKey: process.env.GOOGLE_AI_API_KEY,
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
const agent = new Robota({
|
|
196
|
-
name: 'MultiProviderAgent',
|
|
197
|
-
aiProviders: [openaiProvider, anthropicProvider, googleProvider],
|
|
198
|
-
defaultModel: {
|
|
199
|
-
provider: 'openai',
|
|
200
|
-
model: 'gpt-4o',
|
|
201
|
-
},
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
// Dynamic provider switching
|
|
205
|
-
const openaiResponse = await agent.run('Respond using GPT-4o');
|
|
206
|
-
|
|
207
|
-
agent.setModel({ provider: 'anthropic', model: 'claude-3-sonnet-20240229' });
|
|
208
|
-
const claudeResponse = await agent.run('Respond using Claude');
|
|
209
|
-
```
|
|
210
|
-
|
|
211
|
-
## ⚙️ Configuration Options
|
|
212
|
-
|
|
213
|
-
```typescript
|
|
214
|
-
interface IOpenAIProviderOptions {
|
|
215
|
-
// Model Configuration
|
|
216
|
-
defaultModel?: string; // Used when chat options do not provide a model
|
|
217
|
-
|
|
218
|
-
// API Configuration
|
|
219
|
-
apiKey?: string; // API key (if not set in client)
|
|
220
|
-
client?: OpenAI; // OpenAI SDK client instance
|
|
221
|
-
organization?: string; // OpenAI organization ID
|
|
222
|
-
timeout?: number; // Request timeout (ms)
|
|
223
|
-
baseURL?: string; // Custom API base URL; defaults to Chat Completions compatibility
|
|
224
|
-
apiSurface?: 'responses' | 'chat-completions';
|
|
225
|
-
|
|
226
|
-
// Response Configuration
|
|
227
|
-
responseFormat?: 'text' | 'json_object' | 'json_schema';
|
|
228
|
-
jsonSchema?: {
|
|
229
|
-
// For structured outputs
|
|
230
|
-
name: string;
|
|
231
|
-
description?: string;
|
|
232
|
-
schema?: Record<string, string | number | boolean | object>;
|
|
233
|
-
strict?: boolean;
|
|
234
|
-
};
|
|
235
|
-
reasoning?: {
|
|
236
|
-
effort?: 'low' | 'medium' | 'high';
|
|
237
|
-
summary?: 'auto' | 'concise' | 'detailed';
|
|
238
|
-
};
|
|
239
|
-
store?: boolean;
|
|
240
|
-
includeEncryptedReasoning?: boolean;
|
|
241
|
-
strictTools?: boolean;
|
|
242
|
-
|
|
243
|
-
// Debugging & Logging
|
|
244
|
-
payloadLogger?: IPayloadLogger; // Environment-specific payload logger
|
|
245
|
-
|
|
246
|
-
// Interface-based logger implementations:
|
|
247
|
-
// - FilePayloadLogger: Node.js file-based logging
|
|
248
|
-
// - ConsolePayloadLogger: Browser console-based logging
|
|
249
|
-
// - Custom: Implement IPayloadLogger interface
|
|
250
|
-
}
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
## 📋 Model Guidance
|
|
254
|
-
|
|
255
|
-
| Model | Description | Use Cases |
|
|
256
|
-
| --------- | --------------------- | ------------------------------------------- |
|
|
257
|
-
| `gpt-4o` | General-purpose model | Multimodal chat, tool use, balanced latency |
|
|
258
|
-
| `gpt-4.1` | Strong coding model | Code generation, refactoring, analysis |
|
|
259
|
-
| `o3` | Reasoning model | Complex reasoning and planning |
|
|
260
|
-
|
|
261
|
-
## 🔍 API Reference
|
|
262
|
-
|
|
263
|
-
### OpenAIProvider Class
|
|
264
|
-
|
|
265
|
-
```typescript
|
|
266
|
-
class OpenAIProvider extends AbstractAIProvider {
|
|
267
|
-
// Core methods
|
|
268
|
-
async chat(messages: TUniversalMessage[], options?: IChatOptions): Promise<TUniversalMessage>;
|
|
269
|
-
async chatStream(
|
|
270
|
-
messages: TUniversalMessage[],
|
|
271
|
-
options?: IChatOptions,
|
|
272
|
-
): AsyncIterable<TUniversalMessage>;
|
|
273
|
-
|
|
274
|
-
// Provider information
|
|
275
|
-
readonly name: string = 'openai';
|
|
276
|
-
readonly version: string = '1.0.0';
|
|
277
|
-
|
|
278
|
-
// Utility methods
|
|
279
|
-
supportsTools(): boolean;
|
|
280
|
-
validateConfig(): boolean;
|
|
281
|
-
async dispose(): Promise<void>;
|
|
282
|
-
}
|
|
283
|
-
```
|
|
284
|
-
|
|
285
|
-
### Type Definitions
|
|
286
|
-
|
|
287
|
-
```typescript
|
|
288
|
-
// Chat Options
|
|
289
|
-
interface IChatOptions {
|
|
290
|
-
tools?: IToolSchema[];
|
|
291
|
-
maxTokens?: number;
|
|
292
|
-
temperature?: number;
|
|
293
|
-
model?: string;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// OpenAI-specific types
|
|
297
|
-
interface OpenAIToolCall {
|
|
298
|
-
id: string;
|
|
299
|
-
type: 'function';
|
|
300
|
-
function: {
|
|
301
|
-
name: string;
|
|
302
|
-
arguments: string;
|
|
303
|
-
};
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
interface IOpenAILogData {
|
|
307
|
-
model: string;
|
|
308
|
-
messagesCount: number;
|
|
309
|
-
hasTools: boolean;
|
|
310
|
-
temperature?: number;
|
|
311
|
-
maxTokens?: number;
|
|
312
|
-
timestamp: string;
|
|
313
|
-
requestId?: string;
|
|
314
|
-
}
|
|
315
|
-
```
|
|
316
|
-
|
|
317
|
-
## 🐛 Debugging & Logging
|
|
318
|
-
|
|
319
|
-
### Environment-Specific Payload Logging
|
|
320
|
-
|
|
321
|
-
The OpenAI Provider supports environment-specific payload logging through interface-based dependency injection:
|
|
322
|
-
|
|
323
|
-
#### Node.js Environment (File-Based Logging)
|
|
324
|
-
|
|
325
|
-
```typescript
|
|
326
|
-
import { OpenAIProvider } from '@robota-sdk/agent-provider-openai';
|
|
327
|
-
import { FilePayloadLogger } from '@robota-sdk/agent-provider-openai/loggers/file';
|
|
328
|
-
|
|
329
|
-
const provider = new OpenAIProvider({
|
|
330
|
-
client: openaiClient,
|
|
331
|
-
defaultModel: 'gpt-4o',
|
|
332
|
-
payloadLogger: new FilePayloadLogger({
|
|
333
|
-
logDir: './logs/openai-api',
|
|
334
|
-
enabled: true,
|
|
335
|
-
includeTimestamp: true,
|
|
336
|
-
}),
|
|
337
|
-
});
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
#### Browser Environment (Console-Based Logging)
|
|
341
|
-
|
|
342
|
-
```typescript
|
|
343
|
-
import { OpenAIProvider } from '@robota-sdk/agent-provider-openai';
|
|
344
|
-
import { ConsolePayloadLogger } from '@robota-sdk/agent-provider-openai/loggers/console';
|
|
345
|
-
|
|
346
|
-
const provider = new OpenAIProvider({
|
|
347
|
-
client: openaiClient,
|
|
348
|
-
defaultModel: 'gpt-4o',
|
|
349
|
-
payloadLogger: new ConsolePayloadLogger({
|
|
350
|
-
enabled: true,
|
|
351
|
-
includeTimestamp: true,
|
|
352
|
-
}),
|
|
353
|
-
});
|
|
354
|
-
```
|
|
355
|
-
|
|
356
|
-
#### No Logging (Both Environments)
|
|
357
|
-
|
|
358
|
-
```typescript
|
|
359
|
-
const provider = new OpenAIProvider({
|
|
360
|
-
client: openaiClient,
|
|
361
|
-
defaultModel: 'gpt-4o',
|
|
362
|
-
// payloadLogger: undefined (default - no logging)
|
|
363
|
-
});
|
|
364
|
-
```
|
|
365
|
-
|
|
366
|
-
### Custom Logger Implementation
|
|
367
|
-
|
|
368
|
-
You can create custom logger implementations by implementing the IPayloadLogger interface:
|
|
369
|
-
|
|
370
|
-
```typescript
|
|
371
|
-
import type { IPayloadLogger } from '@robota-sdk/agent-provider-openai';
|
|
372
|
-
|
|
373
|
-
type OpenAILogPayload = Parameters<IPayloadLogger['logPayload']>[0];
|
|
374
|
-
|
|
375
|
-
class CustomPayloadLogger implements IPayloadLogger {
|
|
376
|
-
isEnabled(): boolean {
|
|
377
|
-
return true;
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
async logPayload(payload: OpenAILogPayload, type: 'chat' | 'stream'): Promise<void> {
|
|
381
|
-
// Custom logging implementation
|
|
382
|
-
console.log(`[Custom Logger] ${type}:`, payload);
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
const provider = new OpenAIProvider({
|
|
387
|
-
client: openaiClient,
|
|
388
|
-
payloadLogger: new CustomPayloadLogger(),
|
|
389
|
-
});
|
|
390
|
-
```
|
|
391
|
-
|
|
392
|
-
This creates detailed logs of all API requests and responses for debugging purposes.
|
|
393
|
-
|
|
394
|
-
## 🔒 Security Best Practices
|
|
395
|
-
|
|
396
|
-
### API Key Management
|
|
397
|
-
|
|
398
|
-
```typescript
|
|
399
|
-
// ✅ Good: Use environment variables
|
|
400
|
-
const client = new OpenAI({
|
|
401
|
-
apiKey: process.env.OPENAI_API_KEY,
|
|
402
|
-
});
|
|
403
|
-
|
|
404
|
-
// ❌ Bad: Hardcoded keys
|
|
405
|
-
const client = new OpenAI({
|
|
406
|
-
apiKey: 'sk-...', // Never do this!
|
|
407
|
-
});
|
|
408
|
-
```
|
|
409
|
-
|
|
410
|
-
### Error Handling
|
|
411
|
-
|
|
412
|
-
```typescript
|
|
413
|
-
try {
|
|
414
|
-
const response = await agent.run('Your query');
|
|
415
|
-
} catch (error) {
|
|
416
|
-
if (error instanceof Error) {
|
|
417
|
-
console.error('AI Error:', error.message);
|
|
418
|
-
}
|
|
419
|
-
// Handle specific OpenAI errors
|
|
420
|
-
}
|
|
421
|
-
```
|
|
422
|
-
|
|
423
|
-
## 📊 Performance Optimization
|
|
424
|
-
|
|
425
|
-
### Token Management
|
|
426
|
-
|
|
427
|
-
```typescript
|
|
428
|
-
const provider = new OpenAIProvider({
|
|
429
|
-
client: openaiClient,
|
|
430
|
-
defaultModel: 'gpt-4o',
|
|
431
|
-
});
|
|
432
|
-
|
|
433
|
-
const response = await provider.chat(messages, {
|
|
434
|
-
maxTokens: 1000, // Limit response length
|
|
435
|
-
temperature: 0.3, // More deterministic responses
|
|
436
|
-
});
|
|
437
|
-
```
|
|
438
|
-
|
|
439
|
-
### Model Selection Strategy
|
|
440
|
-
|
|
441
|
-
- Use `gpt-4o` for balanced multimodal chat and tool use
|
|
442
|
-
- Use `gpt-4.1` for coding-heavy workflows
|
|
443
|
-
- Use reasoning models such as `o3` when explicit reasoning controls are required
|
|
444
|
-
|
|
445
|
-
## 🤝 Contributing
|
|
446
|
-
|
|
447
|
-
This package follows strict type safety guidelines:
|
|
448
|
-
|
|
449
|
-
- Zero `any` or `unknown` types allowed
|
|
450
|
-
- Complete TypeScript coverage
|
|
451
|
-
- Comprehensive error handling
|
|
452
|
-
- Provider-agnostic design principles
|
|
453
|
-
|
|
454
|
-
## 📄 License
|
|
455
|
-
|
|
456
|
-
MIT License - see LICENSE file for details.
|
|
457
|
-
|
|
458
|
-
## 🔗 Related Packages
|
|
459
|
-
|
|
460
|
-
- **[@robota-sdk/agent-core](../agents/)**: Core agent framework
|
|
461
|
-
- **[@robota-sdk/agent-provider-anthropic](../anthropic/)**: Anthropic Claude provider
|
|
462
|
-
- **[@robota-sdk/agent-provider-google](../google/)**: Google AI provider
|
|
463
|
-
- **[@robota-sdk/agent-team](../team/)**: assignTask MCP tool collection (team creation removed)
|
|
464
|
-
|
|
465
|
-
---
|
|
466
|
-
|
|
467
|
-
For complete documentation and examples, visit the [Robota SDK Documentation](https://robota.io).
|