@elizaos/plugin-openai 2.0.0-alpha.4 → 2.0.0-alpha.537

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.
@@ -2,35 +2,45 @@ var __defProp = Object.defineProperty;
2
2
  var __getOwnPropNames = Object.getOwnPropertyNames;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __moduleCache = /* @__PURE__ */ new WeakMap;
5
+ function __accessProp(key) {
6
+ return this[key];
7
+ }
6
8
  var __toCommonJS = (from) => {
7
- var entry = __moduleCache.get(from), desc;
8
- if (entry)
9
- return entry;
10
- entry = __defProp({}, "__esModule", { value: true });
11
- if (from && typeof from === "object" || typeof from === "function")
12
- __getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
13
- get: () => from[key],
14
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
- }));
16
- __moduleCache.set(from, entry);
17
- return entry;
9
+ var entry = (__moduleCache ??= new WeakMap()).get(from),
10
+ desc;
11
+ if (entry) return entry;
12
+ entry = __defProp({}, "__esModule", { value: true });
13
+ if ((from && typeof from === "object") || typeof from === "function") {
14
+ for (var key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(entry, key))
16
+ __defProp(entry, key, {
17
+ get: __accessProp.bind(from, key),
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
19
+ });
20
+ }
21
+ __moduleCache.set(from, entry);
22
+ return entry;
18
23
  };
24
+ var __moduleCache;
25
+ var __returnValue = (v) => v;
26
+ function __exportSetter(name, newValue) {
27
+ this[name] = __returnValue.bind(null, newValue);
28
+ }
19
29
  var __export = (target, all) => {
20
- for (var name in all)
21
- __defProp(target, name, {
22
- get: all[name],
23
- enumerable: true,
24
- configurable: true,
25
- set: (newValue) => all[name] = () => newValue
26
- });
30
+ for (var name in all)
31
+ __defProp(target, name, {
32
+ get: all[name],
33
+ enumerable: true,
34
+ configurable: true,
35
+ set: __exportSetter.bind(all, name),
36
+ });
27
37
  };
28
38
 
29
39
  // index.node.ts
30
40
  var exports_index_node = {};
31
41
  __export(exports_index_node, {
32
- openaiPlugin: () => openaiPlugin,
33
- default: () => typescript_default
42
+ openaiPlugin: () => openaiPlugin,
43
+ default: () => index_node_default,
34
44
  });
35
45
  module.exports = __toCommonJS(exports_index_node);
36
46
 
@@ -43,149 +53,231 @@ var import_core2 = require("@elizaos/core");
43
53
  // utils/config.ts
44
54
  var import_core = require("@elizaos/core");
45
55
  function getEnvValue(key) {
46
- if (typeof process === "undefined" || !process.env) {
47
- return;
48
- }
49
- const value = process.env[key];
50
- return value === undefined ? undefined : String(value);
56
+ if (typeof process === "undefined" || !process.env) {
57
+ return;
58
+ }
59
+ const value = process.env[key];
60
+ return value === undefined ? undefined : String(value);
51
61
  }
52
62
  function getSetting(runtime, key, defaultValue) {
53
- const value = runtime.getSetting(key);
54
- if (value !== undefined && value !== null) {
55
- return String(value);
56
- }
57
- return getEnvValue(key) ?? defaultValue;
63
+ const value = runtime.getSetting(key);
64
+ if (value !== undefined && value !== null) {
65
+ return String(value);
66
+ }
67
+ return getEnvValue(key) ?? defaultValue;
58
68
  }
59
69
  function getNumericSetting(runtime, key, defaultValue) {
60
- const value = getSetting(runtime, key);
61
- if (value === undefined) {
62
- return defaultValue;
63
- }
64
- const parsed = Number.parseInt(value, 10);
65
- if (!Number.isFinite(parsed)) {
66
- throw new Error(`Setting '${key}' must be a valid integer, got: ${value}`);
67
- }
68
- return parsed;
70
+ const value = getSetting(runtime, key);
71
+ if (value === undefined) {
72
+ return defaultValue;
73
+ }
74
+ const parsed = Number.parseInt(value, 10);
75
+ if (!Number.isFinite(parsed)) {
76
+ throw new Error(`Setting '${key}' must be a valid integer, got: ${value}`);
77
+ }
78
+ return parsed;
69
79
  }
70
80
  function getBooleanSetting(runtime, key, defaultValue) {
71
- const value = getSetting(runtime, key);
72
- if (value === undefined) {
73
- return defaultValue;
74
- }
75
- const normalized = value.toLowerCase();
76
- return normalized === "true" || normalized === "1" || normalized === "yes";
81
+ const value = getSetting(runtime, key);
82
+ if (value === undefined) {
83
+ return defaultValue;
84
+ }
85
+ const normalized = value.toLowerCase();
86
+ return normalized === "true" || normalized === "1" || normalized === "yes";
77
87
  }
78
88
  function isBrowser() {
79
- return typeof globalThis !== "undefined" && typeof globalThis.document !== "undefined";
89
+ return (
90
+ typeof globalThis !== "undefined" &&
91
+ typeof globalThis.document !== "undefined"
92
+ );
80
93
  }
81
94
  function isProxyMode(runtime) {
82
- return isBrowser() && !!getSetting(runtime, "OPENAI_BROWSER_BASE_URL");
95
+ return isBrowser() && !!getSetting(runtime, "OPENAI_BROWSER_BASE_URL");
83
96
  }
84
97
  function getApiKey(runtime) {
85
- return getSetting(runtime, "OPENAI_API_KEY");
98
+ return getSetting(runtime, "OPENAI_API_KEY");
86
99
  }
87
100
  function getEmbeddingApiKey(runtime) {
88
- const embeddingApiKey = getSetting(runtime, "OPENAI_EMBEDDING_API_KEY");
89
- if (embeddingApiKey) {
90
- import_core.logger.debug("[OpenAI] Using specific embedding API key");
91
- return embeddingApiKey;
92
- }
93
- import_core.logger.debug("[OpenAI] Falling back to general API key for embeddings");
94
- return getApiKey(runtime);
101
+ const embeddingApiKey = getSetting(runtime, "OPENAI_EMBEDDING_API_KEY");
102
+ if (embeddingApiKey) {
103
+ import_core.logger.debug("[OpenAI] Using specific embedding API key");
104
+ return embeddingApiKey;
105
+ }
106
+ import_core.logger.debug(
107
+ "[OpenAI] Falling back to general API key for embeddings",
108
+ );
109
+ return getApiKey(runtime);
95
110
  }
96
111
  function getAuthHeader(runtime, forEmbedding = false) {
97
- if (isBrowser() && !getBooleanSetting(runtime, "OPENAI_ALLOW_BROWSER_API_KEY", false)) {
98
- return {};
99
- }
100
- const key = forEmbedding ? getEmbeddingApiKey(runtime) : getApiKey(runtime);
101
- return key ? { Authorization: `Bearer ${key}` } : {};
112
+ if (
113
+ isBrowser() &&
114
+ !getBooleanSetting(runtime, "OPENAI_ALLOW_BROWSER_API_KEY", false)
115
+ ) {
116
+ return {};
117
+ }
118
+ const key = forEmbedding ? getEmbeddingApiKey(runtime) : getApiKey(runtime);
119
+ return key ? { Authorization: `Bearer ${key}` } : {};
102
120
  }
103
121
  function getBaseURL(runtime) {
104
- const browserURL = getSetting(runtime, "OPENAI_BROWSER_BASE_URL");
105
- const baseURL = isBrowser() && browserURL ? browserURL : getSetting(runtime, "OPENAI_BASE_URL") ?? "https://api.openai.com/v1";
106
- import_core.logger.debug(`[OpenAI] Base URL: ${baseURL}`);
107
- return baseURL;
122
+ const browserURL = getSetting(runtime, "OPENAI_BROWSER_BASE_URL");
123
+ const baseURL =
124
+ isBrowser() && browserURL
125
+ ? browserURL
126
+ : (getSetting(runtime, "OPENAI_BASE_URL") ?? "https://api.openai.com/v1");
127
+ import_core.logger.debug(`[OpenAI] Base URL: ${baseURL}`);
128
+ return baseURL;
108
129
  }
109
130
  function getEmbeddingBaseURL(runtime) {
110
- const embeddingURL = isBrowser() ? getSetting(runtime, "OPENAI_BROWSER_EMBEDDING_URL") ?? getSetting(runtime, "OPENAI_BROWSER_BASE_URL") : getSetting(runtime, "OPENAI_EMBEDDING_URL");
111
- if (embeddingURL) {
112
- import_core.logger.debug(`[OpenAI] Using embedding base URL: ${embeddingURL}`);
113
- return embeddingURL;
114
- }
115
- import_core.logger.debug("[OpenAI] Falling back to general base URL for embeddings");
116
- return getBaseURL(runtime);
131
+ const embeddingURL = isBrowser()
132
+ ? (getSetting(runtime, "OPENAI_BROWSER_EMBEDDING_URL") ??
133
+ getSetting(runtime, "OPENAI_BROWSER_BASE_URL"))
134
+ : getSetting(runtime, "OPENAI_EMBEDDING_URL");
135
+ if (embeddingURL) {
136
+ import_core.logger.debug(
137
+ `[OpenAI] Using embedding base URL: ${embeddingURL}`,
138
+ );
139
+ return embeddingURL;
140
+ }
141
+ import_core.logger.debug(
142
+ "[OpenAI] Falling back to general base URL for embeddings",
143
+ );
144
+ return getBaseURL(runtime);
117
145
  }
118
146
  function getSmallModel(runtime) {
119
- return getSetting(runtime, "OPENAI_SMALL_MODEL") ?? getSetting(runtime, "SMALL_MODEL") ?? "gpt-5-mini";
147
+ return (
148
+ getSetting(runtime, "OPENAI_SMALL_MODEL") ??
149
+ getSetting(runtime, "SMALL_MODEL") ??
150
+ "gpt-5-mini"
151
+ );
152
+ }
153
+ function getNanoModel(runtime) {
154
+ return (
155
+ getSetting(runtime, "OPENAI_NANO_MODEL") ??
156
+ getSetting(runtime, "NANO_MODEL") ??
157
+ getSmallModel(runtime)
158
+ );
159
+ }
160
+ function getMediumModel(runtime) {
161
+ return (
162
+ getSetting(runtime, "OPENAI_MEDIUM_MODEL") ??
163
+ getSetting(runtime, "MEDIUM_MODEL") ??
164
+ getSmallModel(runtime)
165
+ );
120
166
  }
121
167
  function getLargeModel(runtime) {
122
- return getSetting(runtime, "OPENAI_LARGE_MODEL") ?? getSetting(runtime, "LARGE_MODEL") ?? "gpt-5";
168
+ return (
169
+ getSetting(runtime, "OPENAI_LARGE_MODEL") ??
170
+ getSetting(runtime, "LARGE_MODEL") ??
171
+ "gpt-5"
172
+ );
173
+ }
174
+ function getMegaModel(runtime) {
175
+ return (
176
+ getSetting(runtime, "OPENAI_MEGA_MODEL") ??
177
+ getSetting(runtime, "MEGA_MODEL") ??
178
+ getLargeModel(runtime)
179
+ );
180
+ }
181
+ function getResponseHandlerModel(runtime) {
182
+ return (
183
+ getSetting(runtime, "OPENAI_RESPONSE_HANDLER_MODEL") ??
184
+ getSetting(runtime, "OPENAI_SHOULD_RESPOND_MODEL") ??
185
+ getSetting(runtime, "RESPONSE_HANDLER_MODEL") ??
186
+ getSetting(runtime, "SHOULD_RESPOND_MODEL") ??
187
+ getNanoModel(runtime)
188
+ );
189
+ }
190
+ function getActionPlannerModel(runtime) {
191
+ return (
192
+ getSetting(runtime, "OPENAI_ACTION_PLANNER_MODEL") ??
193
+ getSetting(runtime, "OPENAI_PLANNER_MODEL") ??
194
+ getSetting(runtime, "ACTION_PLANNER_MODEL") ??
195
+ getSetting(runtime, "PLANNER_MODEL") ??
196
+ getMediumModel(runtime)
197
+ );
123
198
  }
124
199
  function getEmbeddingModel(runtime) {
125
- return getSetting(runtime, "OPENAI_EMBEDDING_MODEL") ?? "text-embedding-3-small";
200
+ return (
201
+ getSetting(runtime, "OPENAI_EMBEDDING_MODEL") ?? "text-embedding-3-small"
202
+ );
126
203
  }
127
204
  function getImageDescriptionModel(runtime) {
128
- return getSetting(runtime, "OPENAI_IMAGE_DESCRIPTION_MODEL") ?? "gpt-5-mini";
205
+ return getSetting(runtime, "OPENAI_IMAGE_DESCRIPTION_MODEL") ?? "gpt-5-mini";
129
206
  }
130
207
  function getTranscriptionModel(runtime) {
131
- return getSetting(runtime, "OPENAI_TRANSCRIPTION_MODEL") ?? "gpt-5-mini-transcribe";
208
+ return (
209
+ getSetting(runtime, "OPENAI_TRANSCRIPTION_MODEL") ?? "gpt-5-mini-transcribe"
210
+ );
132
211
  }
133
212
  function getTTSModel(runtime) {
134
- return getSetting(runtime, "OPENAI_TTS_MODEL") ?? "tts-1";
213
+ return getSetting(runtime, "OPENAI_TTS_MODEL") ?? "tts-1";
135
214
  }
136
215
  function getTTSVoice(runtime) {
137
- return getSetting(runtime, "OPENAI_TTS_VOICE") ?? "nova";
216
+ return getSetting(runtime, "OPENAI_TTS_VOICE") ?? "nova";
138
217
  }
139
218
  function getTTSInstructions(runtime) {
140
- return getSetting(runtime, "OPENAI_TTS_INSTRUCTIONS") ?? "";
219
+ return getSetting(runtime, "OPENAI_TTS_INSTRUCTIONS") ?? "";
141
220
  }
142
221
  function getImageModel(runtime) {
143
- return getSetting(runtime, "OPENAI_IMAGE_MODEL") ?? "dall-e-3";
222
+ return getSetting(runtime, "OPENAI_IMAGE_MODEL") ?? "dall-e-3";
144
223
  }
145
224
  function getExperimentalTelemetry(runtime) {
146
- return getBooleanSetting(runtime, "OPENAI_EXPERIMENTAL_TELEMETRY", false);
225
+ return getBooleanSetting(runtime, "OPENAI_EXPERIMENTAL_TELEMETRY", false);
147
226
  }
148
227
  function getEmbeddingDimensions(runtime) {
149
- return getNumericSetting(runtime, "OPENAI_EMBEDDING_DIMENSIONS", 1536);
228
+ return getNumericSetting(runtime, "OPENAI_EMBEDDING_DIMENSIONS", 1536);
150
229
  }
151
230
  function getImageDescriptionMaxTokens(runtime) {
152
- return getNumericSetting(runtime, "OPENAI_IMAGE_DESCRIPTION_MAX_TOKENS", 8192);
231
+ return getNumericSetting(
232
+ runtime,
233
+ "OPENAI_IMAGE_DESCRIPTION_MAX_TOKENS",
234
+ 8192,
235
+ );
153
236
  }
154
237
  function getResearchModel(runtime) {
155
- return getSetting(runtime, "OPENAI_RESEARCH_MODEL") ?? "o3-deep-research";
238
+ return getSetting(runtime, "OPENAI_RESEARCH_MODEL") ?? "o3-deep-research";
156
239
  }
157
240
  function getResearchTimeout(runtime) {
158
- return getNumericSetting(runtime, "OPENAI_RESEARCH_TIMEOUT", 3600000);
241
+ return getNumericSetting(runtime, "OPENAI_RESEARCH_TIMEOUT", 3600000);
159
242
  }
160
243
 
161
244
  // init.ts
162
245
  globalThis.AI_SDK_LOG_WARNINGS ??= false;
163
246
  function initializeOpenAI(_config, runtime) {
164
- validateOpenAIConfiguration(runtime);
247
+ validateOpenAIConfiguration(runtime);
165
248
  }
166
249
  async function validateOpenAIConfiguration(runtime) {
167
- if (isBrowser()) {
168
- import_core2.logger.debug("[OpenAI] Skipping API validation in browser environment");
169
- return;
170
- }
171
- const apiKey = getApiKey(runtime);
172
- if (!apiKey) {
173
- import_core2.logger.warn("[OpenAI] OPENAI_API_KEY is not configured. " + "OpenAI functionality will fail until a valid API key is provided.");
174
- return;
175
- }
176
- try {
177
- const baseURL = getBaseURL(runtime);
178
- const response = await fetch(`${baseURL}/models`, {
179
- headers: getAuthHeader(runtime)
180
- });
181
- if (!response.ok) {
182
- import_core2.logger.warn(`[OpenAI] API key validation failed: ${response.status} ${response.statusText}. ` + "Please verify your OPENAI_API_KEY is correct.");
183
- return;
184
- }
185
- } catch (error) {
186
- const message = error instanceof Error ? error.message : String(error);
187
- import_core2.logger.warn(`[OpenAI] API validation error: ${message}. OpenAI functionality may be limited.`);
188
- }
250
+ if (isBrowser()) {
251
+ import_core2.logger.debug(
252
+ "[OpenAI] Skipping API validation in browser environment",
253
+ );
254
+ return;
255
+ }
256
+ const apiKey = getApiKey(runtime);
257
+ if (!apiKey) {
258
+ import_core2.logger.warn(
259
+ "[OpenAI] OPENAI_API_KEY is not configured. " +
260
+ "OpenAI functionality will fail until a valid API key is provided.",
261
+ );
262
+ return;
263
+ }
264
+ try {
265
+ const baseURL = getBaseURL(runtime);
266
+ const response = await fetch(`${baseURL}/models`, {
267
+ headers: getAuthHeader(runtime),
268
+ });
269
+ if (!response.ok) {
270
+ import_core2.logger.warn(
271
+ `[OpenAI] API key validation failed: ${response.status} ${response.statusText}. Please verify your OPENAI_API_KEY is correct.`,
272
+ );
273
+ return;
274
+ }
275
+ } catch (error) {
276
+ const message = error instanceof Error ? error.message : String(error);
277
+ import_core2.logger.warn(
278
+ `[OpenAI] API validation error: ${message}. OpenAI functionality may be limited.`,
279
+ );
280
+ }
189
281
  }
190
282
 
191
283
  // models/audio.ts
@@ -194,224 +286,260 @@ var import_core4 = require("@elizaos/core");
194
286
  // utils/audio.ts
195
287
  var import_core3 = require("@elizaos/core");
196
288
  var MAGIC_BYTES = {
197
- WAV: {
198
- HEADER: [82, 73, 70, 70],
199
- IDENTIFIER: [87, 65, 86, 69]
200
- },
201
- MP3_ID3: [73, 68, 51],
202
- OGG: [79, 103, 103, 83],
203
- FLAC: [102, 76, 97, 67],
204
- FTYP: [102, 116, 121, 112],
205
- WEBM_EBML: [26, 69, 223, 163]
289
+ WAV: {
290
+ HEADER: [82, 73, 70, 70],
291
+ IDENTIFIER: [87, 65, 86, 69],
292
+ },
293
+ MP3_ID3: [73, 68, 51],
294
+ OGG: [79, 103, 103, 83],
295
+ FLAC: [102, 76, 97, 67],
296
+ FTYP: [102, 116, 121, 112],
297
+ WEBM_EBML: [26, 69, 223, 163],
206
298
  };
207
299
  var MIN_DETECTION_BUFFER_SIZE = 12;
208
300
  function matchBytes(buffer, offset, expected) {
209
- for (let i = 0;i < expected.length; i++) {
210
- const expectedByte = expected[i];
211
- if (expectedByte === undefined || buffer[offset + i] !== expectedByte) {
212
- return false;
213
- }
214
- }
215
- return true;
301
+ for (let i = 0; i < expected.length; i++) {
302
+ const expectedByte = expected[i];
303
+ if (expectedByte === undefined || buffer[offset + i] !== expectedByte) {
304
+ return false;
305
+ }
306
+ }
307
+ return true;
216
308
  }
217
309
  function detectAudioMimeType(buffer) {
218
- if (buffer.length < MIN_DETECTION_BUFFER_SIZE) {
219
- return "application/octet-stream";
220
- }
221
- if (matchBytes(buffer, 0, MAGIC_BYTES.WAV.HEADER) && matchBytes(buffer, 8, MAGIC_BYTES.WAV.IDENTIFIER)) {
222
- return "audio/wav";
223
- }
224
- const firstByte = buffer[0];
225
- const secondByte = buffer[1];
226
- if (matchBytes(buffer, 0, MAGIC_BYTES.MP3_ID3) || firstByte === 255 && secondByte !== undefined && (secondByte & 224) === 224) {
227
- return "audio/mpeg";
228
- }
229
- if (matchBytes(buffer, 0, MAGIC_BYTES.OGG)) {
230
- return "audio/ogg";
231
- }
232
- if (matchBytes(buffer, 0, MAGIC_BYTES.FLAC)) {
233
- return "audio/flac";
234
- }
235
- if (matchBytes(buffer, 4, MAGIC_BYTES.FTYP)) {
236
- return "audio/mp4";
237
- }
238
- if (matchBytes(buffer, 0, MAGIC_BYTES.WEBM_EBML)) {
239
- return "audio/webm";
240
- }
241
- import_core3.logger.warn("Could not detect audio format from buffer, using generic binary type");
242
- return "application/octet-stream";
310
+ if (buffer.length < MIN_DETECTION_BUFFER_SIZE) {
311
+ return "application/octet-stream";
312
+ }
313
+ if (
314
+ matchBytes(buffer, 0, MAGIC_BYTES.WAV.HEADER) &&
315
+ matchBytes(buffer, 8, MAGIC_BYTES.WAV.IDENTIFIER)
316
+ ) {
317
+ return "audio/wav";
318
+ }
319
+ const firstByte = buffer[0];
320
+ const secondByte = buffer[1];
321
+ if (
322
+ matchBytes(buffer, 0, MAGIC_BYTES.MP3_ID3) ||
323
+ (firstByte === 255 &&
324
+ secondByte !== undefined &&
325
+ (secondByte & 224) === 224)
326
+ ) {
327
+ return "audio/mpeg";
328
+ }
329
+ if (matchBytes(buffer, 0, MAGIC_BYTES.OGG)) {
330
+ return "audio/ogg";
331
+ }
332
+ if (matchBytes(buffer, 0, MAGIC_BYTES.FLAC)) {
333
+ return "audio/flac";
334
+ }
335
+ if (matchBytes(buffer, 4, MAGIC_BYTES.FTYP)) {
336
+ return "audio/mp4";
337
+ }
338
+ if (matchBytes(buffer, 0, MAGIC_BYTES.WEBM_EBML)) {
339
+ return "audio/webm";
340
+ }
341
+ import_core3.logger.warn(
342
+ "Could not detect audio format from buffer, using generic binary type",
343
+ );
344
+ return "application/octet-stream";
243
345
  }
244
346
  function getExtensionForMimeType(mimeType) {
245
- switch (mimeType) {
246
- case "audio/wav":
247
- return "wav";
248
- case "audio/mpeg":
249
- return "mp3";
250
- case "audio/ogg":
251
- return "ogg";
252
- case "audio/flac":
253
- return "flac";
254
- case "audio/mp4":
255
- return "m4a";
256
- case "audio/webm":
257
- return "webm";
258
- case "application/octet-stream":
259
- return "bin";
260
- }
347
+ switch (mimeType) {
348
+ case "audio/wav":
349
+ return "wav";
350
+ case "audio/mpeg":
351
+ return "mp3";
352
+ case "audio/ogg":
353
+ return "ogg";
354
+ case "audio/flac":
355
+ return "flac";
356
+ case "audio/mp4":
357
+ return "m4a";
358
+ case "audio/webm":
359
+ return "webm";
360
+ case "application/octet-stream":
361
+ return "bin";
362
+ }
261
363
  }
262
364
  function getFilenameForMimeType(mimeType) {
263
- const ext = getExtensionForMimeType(mimeType);
264
- return `recording.${ext}`;
365
+ const ext = getExtensionForMimeType(mimeType);
366
+ return `recording.${ext}`;
265
367
  }
266
368
 
267
369
  // models/audio.ts
268
370
  function isBlobOrFile(value) {
269
- return value instanceof Blob || value instanceof File;
371
+ return value instanceof Blob || value instanceof File;
270
372
  }
271
373
  function isBuffer(value) {
272
- return Buffer.isBuffer(value);
374
+ return Buffer.isBuffer(value);
273
375
  }
274
376
  function isLocalTranscriptionParams(value) {
275
- return typeof value === "object" && value !== null && "audio" in value && (isBlobOrFile(value.audio) || isBuffer(value.audio));
377
+ return (
378
+ typeof value === "object" &&
379
+ value !== null &&
380
+ "audio" in value &&
381
+ (isBlobOrFile(value.audio) || isBuffer(value.audio))
382
+ );
276
383
  }
277
384
  function isCoreTranscriptionParams(value) {
278
- return typeof value === "object" && value !== null && "audioUrl" in value && typeof value.audioUrl === "string";
385
+ return (
386
+ typeof value === "object" &&
387
+ value !== null &&
388
+ "audioUrl" in value &&
389
+ typeof value.audioUrl === "string"
390
+ );
279
391
  }
280
392
  async function fetchAudioFromUrl(url) {
281
- const response = await fetch(url);
282
- if (!response.ok) {
283
- throw new Error(`Failed to fetch audio from URL: ${response.status}`);
284
- }
285
- return response.blob();
393
+ const response = await fetch(url);
394
+ if (!response.ok) {
395
+ throw new Error(`Failed to fetch audio from URL: ${response.status}`);
396
+ }
397
+ return response.blob();
286
398
  }
287
399
  async function handleTranscription(runtime, input) {
288
- let modelName = getTranscriptionModel(runtime);
289
- let blob;
290
- let extraParams = {};
291
- if (typeof input === "string") {
292
- import_core4.logger.debug(`[OpenAI] Fetching audio from URL: ${input}`);
293
- blob = await fetchAudioFromUrl(input);
294
- } else if (isBlobOrFile(input)) {
295
- blob = input;
296
- } else if (isBuffer(input)) {
297
- const mimeType2 = detectAudioMimeType(input);
298
- import_core4.logger.debug(`[OpenAI] Auto-detected audio MIME type: ${mimeType2}`);
299
- blob = new Blob([new Uint8Array(input)], { type: mimeType2 });
300
- } else if (isLocalTranscriptionParams(input)) {
301
- extraParams = input;
302
- if (input.model) {
303
- modelName = input.model;
304
- }
305
- if (isBuffer(input.audio)) {
306
- const mimeType2 = input.mimeType ?? detectAudioMimeType(input.audio);
307
- import_core4.logger.debug(`[OpenAI] Using MIME type: ${mimeType2}`);
308
- blob = new Blob([new Uint8Array(input.audio)], { type: mimeType2 });
309
- } else {
310
- blob = input.audio;
311
- }
312
- } else if (isCoreTranscriptionParams(input)) {
313
- import_core4.logger.debug(`[OpenAI] Fetching audio from URL: ${input.audioUrl}`);
314
- blob = await fetchAudioFromUrl(input.audioUrl);
315
- extraParams = { prompt: input.prompt };
316
- } else {
317
- throw new Error("TRANSCRIPTION expects Blob, File, Buffer, URL string, or TranscriptionParams object");
318
- }
319
- import_core4.logger.debug(`[OpenAI] Using TRANSCRIPTION model: ${modelName}`);
320
- const mimeType = blob.type || "audio/webm";
321
- const filename = blob.name || getFilenameForMimeType(mimeType.startsWith("audio/") ? mimeType : "audio/webm");
322
- const formData = new FormData;
323
- formData.append("file", blob, filename);
324
- formData.append("model", modelName);
325
- if (extraParams.language) {
326
- formData.append("language", extraParams.language);
327
- }
328
- if (extraParams.responseFormat) {
329
- formData.append("response_format", extraParams.responseFormat);
330
- }
331
- if (extraParams.prompt) {
332
- formData.append("prompt", extraParams.prompt);
333
- }
334
- if (extraParams.temperature !== undefined) {
335
- formData.append("temperature", String(extraParams.temperature));
336
- }
337
- if (extraParams.timestampGranularities) {
338
- for (const granularity of extraParams.timestampGranularities) {
339
- formData.append("timestamp_granularities[]", granularity);
340
- }
341
- }
342
- const baseURL = getBaseURL(runtime);
343
- const response = await fetch(`${baseURL}/audio/transcriptions`, {
344
- method: "POST",
345
- headers: getAuthHeader(runtime),
346
- body: formData
347
- });
348
- if (!response.ok) {
349
- const errorText = await response.text().catch(() => "Unknown error");
350
- throw new Error(`OpenAI transcription failed: ${response.status} ${response.statusText} - ${errorText}`);
351
- }
352
- const data = await response.json();
353
- return data.text;
400
+ let modelName = getTranscriptionModel(runtime);
401
+ let blob;
402
+ let extraParams = {};
403
+ if (typeof input === "string") {
404
+ import_core4.logger.debug(`[OpenAI] Fetching audio from URL: ${input}`);
405
+ blob = await fetchAudioFromUrl(input);
406
+ } else if (isBlobOrFile(input)) {
407
+ blob = input;
408
+ } else if (isBuffer(input)) {
409
+ const mimeType2 = detectAudioMimeType(input);
410
+ import_core4.logger.debug(
411
+ `[OpenAI] Auto-detected audio MIME type: ${mimeType2}`,
412
+ );
413
+ blob = new Blob([new Uint8Array(input)], { type: mimeType2 });
414
+ } else if (isLocalTranscriptionParams(input)) {
415
+ extraParams = input;
416
+ if (input.model) {
417
+ modelName = input.model;
418
+ }
419
+ if (isBuffer(input.audio)) {
420
+ const mimeType2 = input.mimeType ?? detectAudioMimeType(input.audio);
421
+ import_core4.logger.debug(`[OpenAI] Using MIME type: ${mimeType2}`);
422
+ blob = new Blob([new Uint8Array(input.audio)], { type: mimeType2 });
423
+ } else {
424
+ blob = input.audio;
425
+ }
426
+ } else if (isCoreTranscriptionParams(input)) {
427
+ import_core4.logger.debug(
428
+ `[OpenAI] Fetching audio from URL: ${input.audioUrl}`,
429
+ );
430
+ blob = await fetchAudioFromUrl(input.audioUrl);
431
+ extraParams = { prompt: input.prompt };
432
+ } else {
433
+ throw new Error(
434
+ "TRANSCRIPTION expects Blob, File, Buffer, URL string, or TranscriptionParams object",
435
+ );
436
+ }
437
+ import_core4.logger.debug(`[OpenAI] Using TRANSCRIPTION model: ${modelName}`);
438
+ const mimeType = blob.type || "audio/webm";
439
+ const filename =
440
+ blob.name ||
441
+ getFilenameForMimeType(
442
+ mimeType.startsWith("audio/") ? mimeType : "audio/webm",
443
+ );
444
+ const formData = new FormData();
445
+ formData.append("file", blob, filename);
446
+ formData.append("model", modelName);
447
+ if (extraParams.language) {
448
+ formData.append("language", extraParams.language);
449
+ }
450
+ if (extraParams.responseFormat) {
451
+ formData.append("response_format", extraParams.responseFormat);
452
+ }
453
+ if (extraParams.prompt) {
454
+ formData.append("prompt", extraParams.prompt);
455
+ }
456
+ if (extraParams.temperature !== undefined) {
457
+ formData.append("temperature", String(extraParams.temperature));
458
+ }
459
+ if (extraParams.timestampGranularities) {
460
+ for (const granularity of extraParams.timestampGranularities) {
461
+ formData.append("timestamp_granularities[]", granularity);
462
+ }
463
+ }
464
+ const baseURL = getBaseURL(runtime);
465
+ const response = await fetch(`${baseURL}/audio/transcriptions`, {
466
+ method: "POST",
467
+ headers: getAuthHeader(runtime),
468
+ body: formData,
469
+ });
470
+ if (!response.ok) {
471
+ const errorText = await response.text().catch(() => "Unknown error");
472
+ throw new Error(
473
+ `OpenAI transcription failed: ${response.status} ${response.statusText} - ${errorText}`,
474
+ );
475
+ }
476
+ const data = await response.json();
477
+ return data.text;
354
478
  }
355
479
  async function handleTextToSpeech(runtime, input) {
356
- let text;
357
- let voice;
358
- let format = "mp3";
359
- let model;
360
- let instructions;
361
- if (typeof input === "string") {
362
- text = input;
363
- voice = undefined;
364
- } else {
365
- text = input.text;
366
- voice = input.voice;
367
- if ("format" in input && input.format) {
368
- format = input.format;
369
- }
370
- if ("model" in input && input.model) {
371
- model = input.model;
372
- }
373
- if ("instructions" in input && input.instructions) {
374
- instructions = input.instructions;
375
- }
376
- }
377
- model = model ?? getTTSModel(runtime);
378
- voice = voice ?? getTTSVoice(runtime);
379
- instructions = instructions ?? getTTSInstructions(runtime);
380
- import_core4.logger.debug(`[OpenAI] Using TEXT_TO_SPEECH model: ${model}`);
381
- if (!text || text.trim().length === 0) {
382
- throw new Error("TEXT_TO_SPEECH requires non-empty text");
383
- }
384
- if (text.length > 4096) {
385
- throw new Error("TEXT_TO_SPEECH text exceeds 4096 character limit");
386
- }
387
- const validVoices = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"];
388
- if (voice && !validVoices.includes(voice)) {
389
- throw new Error(`Invalid voice: ${voice}. Must be one of: ${validVoices.join(", ")}`);
390
- }
391
- const baseURL = getBaseURL(runtime);
392
- const requestBody = {
393
- model,
394
- voice,
395
- input: text,
396
- response_format: format
397
- };
398
- if (instructions && instructions.length > 0) {
399
- requestBody.instructions = instructions;
400
- }
401
- const response = await fetch(`${baseURL}/audio/speech`, {
402
- method: "POST",
403
- headers: {
404
- ...getAuthHeader(runtime),
405
- "Content-Type": "application/json",
406
- ...format === "mp3" ? { Accept: "audio/mpeg" } : {}
407
- },
408
- body: JSON.stringify(requestBody)
409
- });
410
- if (!response.ok) {
411
- const errorText = await response.text().catch(() => "Unknown error");
412
- throw new Error(`OpenAI TTS failed: ${response.status} ${response.statusText} - ${errorText}`);
413
- }
414
- return response.arrayBuffer();
480
+ let text;
481
+ let voice;
482
+ let format = "mp3";
483
+ let model;
484
+ let instructions;
485
+ if (typeof input === "string") {
486
+ text = input;
487
+ voice = undefined;
488
+ } else {
489
+ text = input.text;
490
+ voice = input.voice;
491
+ if ("format" in input && input.format) {
492
+ format = input.format;
493
+ }
494
+ if ("model" in input && input.model) {
495
+ model = input.model;
496
+ }
497
+ if ("instructions" in input && input.instructions) {
498
+ instructions = input.instructions;
499
+ }
500
+ }
501
+ model = model ?? getTTSModel(runtime);
502
+ voice = voice ?? getTTSVoice(runtime);
503
+ instructions = instructions ?? getTTSInstructions(runtime);
504
+ import_core4.logger.debug(`[OpenAI] Using TEXT_TO_SPEECH model: ${model}`);
505
+ if (!text || text.trim().length === 0) {
506
+ throw new Error("TEXT_TO_SPEECH requires non-empty text");
507
+ }
508
+ if (text.length > 4096) {
509
+ throw new Error("TEXT_TO_SPEECH text exceeds 4096 character limit");
510
+ }
511
+ const validVoices = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"];
512
+ if (voice && !validVoices.includes(voice)) {
513
+ throw new Error(
514
+ `Invalid voice: ${voice}. Must be one of: ${validVoices.join(", ")}`,
515
+ );
516
+ }
517
+ const baseURL = getBaseURL(runtime);
518
+ const requestBody = {
519
+ model,
520
+ voice,
521
+ input: text,
522
+ response_format: format,
523
+ };
524
+ if (instructions && instructions.length > 0) {
525
+ requestBody.instructions = instructions;
526
+ }
527
+ const response = await fetch(`${baseURL}/audio/speech`, {
528
+ method: "POST",
529
+ headers: {
530
+ ...getAuthHeader(runtime),
531
+ "Content-Type": "application/json",
532
+ ...(format === "mp3" ? { Accept: "audio/mpeg" } : {}),
533
+ },
534
+ body: JSON.stringify(requestBody),
535
+ });
536
+ if (!response.ok) {
537
+ const errorText = await response.text().catch(() => "Unknown error");
538
+ throw new Error(
539
+ `OpenAI TTS failed: ${response.status} ${response.statusText} - ${errorText}`,
540
+ );
541
+ }
542
+ return response.arrayBuffer();
415
543
  }
416
544
  // models/embedding.ts
417
545
  var import_core6 = require("@elizaos/core");
@@ -420,242 +548,287 @@ var import_core6 = require("@elizaos/core");
420
548
  var import_core5 = require("@elizaos/core");
421
549
  var MAX_PROMPT_LENGTH = 200;
422
550
  function truncatePrompt(prompt) {
423
- if (prompt.length <= MAX_PROMPT_LENGTH) {
424
- return prompt;
425
- }
426
- return `${prompt.slice(0, MAX_PROMPT_LENGTH)}…`;
551
+ if (prompt.length <= MAX_PROMPT_LENGTH) {
552
+ return prompt;
553
+ }
554
+ return `${prompt.slice(0, MAX_PROMPT_LENGTH)}…`;
427
555
  }
428
556
  function normalizeUsage(usage) {
429
- if ("promptTokens" in usage) {
430
- return {
431
- promptTokens: usage.promptTokens ?? 0,
432
- completionTokens: usage.completionTokens ?? 0,
433
- totalTokens: usage.totalTokens ?? (usage.promptTokens ?? 0) + (usage.completionTokens ?? 0)
434
- };
435
- }
436
- if ("inputTokens" in usage || "outputTokens" in usage) {
437
- const input = usage.inputTokens ?? 0;
438
- const output = usage.outputTokens ?? 0;
439
- return {
440
- promptTokens: input,
441
- completionTokens: output,
442
- totalTokens: input + output
443
- };
444
- }
445
- return {
446
- promptTokens: 0,
447
- completionTokens: 0,
448
- totalTokens: 0
449
- };
557
+ if ("promptTokens" in usage) {
558
+ const promptTokensDetails =
559
+ "promptTokensDetails" in usage ? usage.promptTokensDetails : undefined;
560
+ const cachedPromptTokens =
561
+ usage.cachedPromptTokens ?? promptTokensDetails?.cachedTokens;
562
+ return {
563
+ promptTokens: usage.promptTokens ?? 0,
564
+ completionTokens: usage.completionTokens ?? 0,
565
+ totalTokens:
566
+ usage.totalTokens ??
567
+ (usage.promptTokens ?? 0) + (usage.completionTokens ?? 0),
568
+ cachedPromptTokens,
569
+ };
570
+ }
571
+ if ("inputTokens" in usage || "outputTokens" in usage) {
572
+ const input = usage.inputTokens ?? 0;
573
+ const output = usage.outputTokens ?? 0;
574
+ const total = usage.totalTokens ?? input + output;
575
+ return {
576
+ promptTokens: input,
577
+ completionTokens: output,
578
+ totalTokens: total,
579
+ cachedPromptTokens: usage.cachedInputTokens,
580
+ };
581
+ }
582
+ return {
583
+ promptTokens: 0,
584
+ completionTokens: 0,
585
+ totalTokens: 0,
586
+ };
450
587
  }
451
588
  function emitModelUsageEvent(runtime, type, prompt, usage) {
452
- const normalized = normalizeUsage(usage);
453
- const payload = {
454
- runtime,
455
- source: "openai",
456
- provider: "openai",
457
- type,
458
- prompt: truncatePrompt(prompt),
459
- tokens: {
460
- prompt: normalized.promptTokens,
461
- completion: normalized.completionTokens,
462
- total: normalized.totalTokens
463
- }
464
- };
465
- runtime.emitEvent(import_core5.EventType.MODEL_USED, payload);
589
+ const normalized = normalizeUsage(usage);
590
+ const payload = {
591
+ runtime,
592
+ source: "openai",
593
+ provider: "openai",
594
+ type,
595
+ prompt: truncatePrompt(prompt),
596
+ tokens: {
597
+ prompt: normalized.promptTokens,
598
+ completion: normalized.completionTokens,
599
+ total: normalized.totalTokens,
600
+ ...(normalized.cachedPromptTokens !== undefined
601
+ ? { cached: normalized.cachedPromptTokens }
602
+ : {}),
603
+ },
604
+ };
605
+ runtime.emitEvent(import_core5.EventType.MODEL_USED, payload);
466
606
  }
467
607
 
468
608
  // models/embedding.ts
469
609
  function validateDimension(dimension) {
470
- const validDimensions = Object.values(import_core6.VECTOR_DIMS);
471
- if (!validDimensions.includes(dimension)) {
472
- throw new Error(`Invalid embedding dimension: ${dimension}. Must be one of: ${validDimensions.join(", ")}`);
473
- }
474
- return dimension;
610
+ const validDimensions = Object.values(import_core6.VECTOR_DIMS);
611
+ if (!validDimensions.includes(dimension)) {
612
+ throw new Error(
613
+ `Invalid embedding dimension: ${dimension}. Must be one of: ${validDimensions.join(", ")}`,
614
+ );
615
+ }
616
+ return dimension;
475
617
  }
476
618
  function extractText(params) {
477
- if (params === null) {
478
- return null;
479
- }
480
- if (typeof params === "string") {
481
- return params;
482
- }
483
- if (typeof params === "object" && typeof params.text === "string") {
484
- return params.text;
485
- }
486
- throw new Error("Invalid embedding params: expected string, { text: string }, or null");
619
+ if (params === null) {
620
+ return null;
621
+ }
622
+ if (typeof params === "string") {
623
+ return params;
624
+ }
625
+ if (typeof params === "object" && typeof params.text === "string") {
626
+ return params.text;
627
+ }
628
+ throw new Error(
629
+ "Invalid embedding params: expected string, { text: string }, or null",
630
+ );
487
631
  }
488
632
  async function handleTextEmbedding(runtime, params) {
489
- const embeddingModel = getEmbeddingModel(runtime);
490
- const embeddingDimension = validateDimension(getEmbeddingDimensions(runtime));
491
- const text = extractText(params);
492
- if (text === null) {
493
- import_core6.logger.debug("[OpenAI] Creating test embedding for initialization");
494
- const testVector = new Array(embeddingDimension).fill(0);
495
- testVector[0] = 0.1;
496
- return testVector;
497
- }
498
- let trimmedText = text.trim();
499
- if (trimmedText.length === 0) {
500
- throw new Error("Cannot generate embedding for empty text");
501
- }
502
- const maxChars = import_core6.MAX_EMBEDDING_TOKENS * 4;
503
- if (trimmedText.length > maxChars) {
504
- import_core6.logger.warn(`[OpenAI] Embedding input too long (~${Math.ceil(trimmedText.length / 4)} tokens), truncating to ~${import_core6.MAX_EMBEDDING_TOKENS} tokens`);
505
- trimmedText = trimmedText.slice(0, maxChars);
506
- }
507
- const baseURL = getEmbeddingBaseURL(runtime);
508
- const url = `${baseURL}/embeddings`;
509
- import_core6.logger.debug(`[OpenAI] Generating embedding with model: ${embeddingModel}`);
510
- const response = await fetch(url, {
511
- method: "POST",
512
- headers: {
513
- ...getAuthHeader(runtime, true),
514
- "Content-Type": "application/json"
515
- },
516
- body: JSON.stringify({
517
- model: embeddingModel,
518
- input: trimmedText
519
- })
520
- });
521
- if (!response.ok) {
522
- const errorText = await response.text().catch(() => "Unknown error");
523
- throw new Error(`OpenAI embedding API error: ${response.status} ${response.statusText} - ${errorText}`);
524
- }
525
- const data = await response.json();
526
- const firstResult = data?.data?.[0];
527
- if (!firstResult || !firstResult.embedding) {
528
- throw new Error("OpenAI API returned invalid embedding response structure");
529
- }
530
- const embedding = firstResult.embedding;
531
- if (embedding.length !== embeddingDimension) {
532
- throw new Error(`Embedding dimension mismatch: got ${embedding.length}, expected ${embeddingDimension}. ` + `Check OPENAI_EMBEDDING_DIMENSIONS setting.`);
533
- }
534
- if (data.usage) {
535
- emitModelUsageEvent(runtime, import_core6.ModelType.TEXT_EMBEDDING, trimmedText, {
536
- promptTokens: data.usage.prompt_tokens,
537
- completionTokens: 0,
538
- totalTokens: data.usage.total_tokens
539
- });
540
- }
541
- import_core6.logger.debug(`[OpenAI] Generated embedding with ${embedding.length} dimensions`);
542
- return embedding;
633
+ const embeddingModel = getEmbeddingModel(runtime);
634
+ const embeddingDimension = validateDimension(getEmbeddingDimensions(runtime));
635
+ const text = extractText(params);
636
+ if (text === null) {
637
+ import_core6.logger.debug(
638
+ "[OpenAI] Creating test embedding for initialization",
639
+ );
640
+ const testVector = new Array(embeddingDimension).fill(0);
641
+ testVector[0] = 0.1;
642
+ return testVector;
643
+ }
644
+ let trimmedText = text.trim();
645
+ if (trimmedText.length === 0) {
646
+ throw new Error("Cannot generate embedding for empty text");
647
+ }
648
+ const maxChars = 8000 * 4;
649
+ if (trimmedText.length > maxChars) {
650
+ import_core6.logger.warn(
651
+ `[OpenAI] Embedding input too long (~${Math.ceil(trimmedText.length / 4)} tokens), truncating to ~8000 tokens`,
652
+ );
653
+ trimmedText = trimmedText.slice(0, maxChars);
654
+ }
655
+ const baseURL = getEmbeddingBaseURL(runtime);
656
+ const url = `${baseURL}/embeddings`;
657
+ import_core6.logger.debug(
658
+ `[OpenAI] Generating embedding with model: ${embeddingModel}`,
659
+ );
660
+ const response = await fetch(url, {
661
+ method: "POST",
662
+ headers: {
663
+ ...getAuthHeader(runtime, true),
664
+ "Content-Type": "application/json",
665
+ },
666
+ body: JSON.stringify({
667
+ model: embeddingModel,
668
+ input: trimmedText,
669
+ }),
670
+ });
671
+ if (!response.ok) {
672
+ const errorText = await response.text().catch(() => "Unknown error");
673
+ throw new Error(
674
+ `OpenAI embedding API error: ${response.status} ${response.statusText} - ${errorText}`,
675
+ );
676
+ }
677
+ const data = await response.json();
678
+ const firstResult = data?.data?.[0];
679
+ if (!firstResult?.embedding) {
680
+ throw new Error("OpenAI API returned invalid embedding response structure");
681
+ }
682
+ const embedding = firstResult.embedding;
683
+ if (embedding.length !== embeddingDimension) {
684
+ throw new Error(
685
+ `Embedding dimension mismatch: got ${embedding.length}, expected ${embeddingDimension}. Check OPENAI_EMBEDDING_DIMENSIONS setting.`,
686
+ );
687
+ }
688
+ if (data.usage) {
689
+ emitModelUsageEvent(
690
+ runtime,
691
+ import_core6.ModelType.TEXT_EMBEDDING,
692
+ trimmedText,
693
+ {
694
+ promptTokens: data.usage.prompt_tokens,
695
+ completionTokens: 0,
696
+ totalTokens: data.usage.total_tokens,
697
+ },
698
+ );
699
+ }
700
+ import_core6.logger.debug(
701
+ `[OpenAI] Generated embedding with ${embedding.length} dimensions`,
702
+ );
703
+ return embedding;
543
704
  }
544
705
  // models/image.ts
545
706
  var import_core7 = require("@elizaos/core");
546
- var DEFAULT_IMAGE_DESCRIPTION_PROMPT = "Please analyze this image and provide a title and detailed description.";
707
+ var DEFAULT_IMAGE_DESCRIPTION_PROMPT =
708
+ "Please analyze this image and provide a title and detailed description.";
547
709
  async function handleImageGeneration(runtime, params) {
548
- const modelName = getImageModel(runtime);
549
- const count = params.count ?? 1;
550
- const size = params.size ?? "1024x1024";
551
- const extendedParams = params;
552
- import_core7.logger.debug(`[OpenAI] Using IMAGE model: ${modelName}`);
553
- if (!params.prompt || params.prompt.trim().length === 0) {
554
- throw new Error("IMAGE generation requires a non-empty prompt");
555
- }
556
- if (count < 1 || count > 10) {
557
- throw new Error("IMAGE count must be between 1 and 10");
558
- }
559
- const baseURL = getBaseURL(runtime);
560
- const requestBody = {
561
- model: modelName,
562
- prompt: params.prompt,
563
- n: count,
564
- size
565
- };
566
- if (extendedParams.quality) {
567
- requestBody.quality = extendedParams.quality;
568
- }
569
- if (extendedParams.style) {
570
- requestBody.style = extendedParams.style;
571
- }
572
- const response = await fetch(`${baseURL}/images/generations`, {
573
- method: "POST",
574
- headers: {
575
- ...getAuthHeader(runtime),
576
- "Content-Type": "application/json"
577
- },
578
- body: JSON.stringify(requestBody)
579
- });
580
- if (!response.ok) {
581
- const errorText = await response.text().catch(() => "Unknown error");
582
- throw new Error(`OpenAI image generation failed: ${response.status} ${response.statusText} - ${errorText}`);
583
- }
584
- const data = await response.json();
585
- if (!data.data || data.data.length === 0) {
586
- throw new Error("OpenAI API returned no images");
587
- }
588
- return data.data.map((item) => ({
589
- url: item.url,
590
- revisedPrompt: item.revised_prompt
591
- }));
710
+ const modelName = getImageModel(runtime);
711
+ const count = params.count ?? 1;
712
+ const size = params.size ?? "1024x1024";
713
+ const extendedParams = params;
714
+ import_core7.logger.debug(`[OpenAI] Using IMAGE model: ${modelName}`);
715
+ if (!params.prompt || params.prompt.trim().length === 0) {
716
+ throw new Error("IMAGE generation requires a non-empty prompt");
717
+ }
718
+ if (count < 1 || count > 10) {
719
+ throw new Error("IMAGE count must be between 1 and 10");
720
+ }
721
+ const baseURL = getBaseURL(runtime);
722
+ const requestBody = {
723
+ model: modelName,
724
+ prompt: params.prompt,
725
+ n: count,
726
+ size,
727
+ };
728
+ if (extendedParams.quality) {
729
+ requestBody.quality = extendedParams.quality;
730
+ }
731
+ if (extendedParams.style) {
732
+ requestBody.style = extendedParams.style;
733
+ }
734
+ const response = await fetch(`${baseURL}/images/generations`, {
735
+ method: "POST",
736
+ headers: {
737
+ ...getAuthHeader(runtime),
738
+ "Content-Type": "application/json",
739
+ },
740
+ body: JSON.stringify(requestBody),
741
+ });
742
+ if (!response.ok) {
743
+ const errorText = await response.text().catch(() => "Unknown error");
744
+ throw new Error(
745
+ `OpenAI image generation failed: ${response.status} ${response.statusText} - ${errorText}`,
746
+ );
747
+ }
748
+ const data = await response.json();
749
+ if (!data.data || data.data.length === 0) {
750
+ throw new Error("OpenAI API returned no images");
751
+ }
752
+ return data.data.map((item) => ({
753
+ url: item.url,
754
+ revisedPrompt: item.revised_prompt,
755
+ }));
592
756
  }
593
757
  function parseTitleFromResponse(content) {
594
- const titleMatch = content.match(/title[:\s]+(.+?)(?:\n|$)/i);
595
- return titleMatch?.[1]?.trim() ?? "Image Analysis";
758
+ const titleMatch = content.match(/title[:\s]+(.+?)(?:\n|$)/i);
759
+ return titleMatch?.[1]?.trim() ?? "Image Analysis";
596
760
  }
597
761
  function parseDescriptionFromResponse(content) {
598
- return content.replace(/title[:\s]+(.+?)(?:\n|$)/i, "").trim();
762
+ return content.replace(/title[:\s]+(.+?)(?:\n|$)/i, "").trim();
599
763
  }
600
764
  async function handleImageDescription(runtime, params) {
601
- const modelName = getImageDescriptionModel(runtime);
602
- const maxTokens = getImageDescriptionMaxTokens(runtime);
603
- import_core7.logger.debug(`[OpenAI] Using IMAGE_DESCRIPTION model: ${modelName}`);
604
- let imageUrl;
605
- let promptText;
606
- if (typeof params === "string") {
607
- imageUrl = params;
608
- promptText = DEFAULT_IMAGE_DESCRIPTION_PROMPT;
609
- } else {
610
- imageUrl = params.imageUrl;
611
- promptText = params.prompt ?? DEFAULT_IMAGE_DESCRIPTION_PROMPT;
612
- }
613
- if (!imageUrl || imageUrl.trim().length === 0) {
614
- throw new Error("IMAGE_DESCRIPTION requires a valid image URL");
615
- }
616
- const baseURL = getBaseURL(runtime);
617
- const requestBody = {
618
- model: modelName,
619
- messages: [
620
- {
621
- role: "user",
622
- content: [
623
- { type: "text", text: promptText },
624
- { type: "image_url", image_url: { url: imageUrl } }
625
- ]
626
- }
627
- ],
628
- max_tokens: maxTokens
629
- };
630
- const response = await fetch(`${baseURL}/chat/completions`, {
631
- method: "POST",
632
- headers: {
633
- ...getAuthHeader(runtime),
634
- "Content-Type": "application/json"
635
- },
636
- body: JSON.stringify(requestBody)
637
- });
638
- if (!response.ok) {
639
- const errorText = await response.text().catch(() => "Unknown error");
640
- throw new Error(`OpenAI image description failed: ${response.status} ${response.statusText} - ${errorText}`);
641
- }
642
- const data = await response.json();
643
- if (data.usage) {
644
- emitModelUsageEvent(runtime, import_core7.ModelType.IMAGE_DESCRIPTION, typeof params === "string" ? params : params.prompt ?? "", {
645
- promptTokens: data.usage.prompt_tokens,
646
- completionTokens: data.usage.completion_tokens,
647
- totalTokens: data.usage.total_tokens
648
- });
649
- }
650
- const firstChoice = data.choices?.[0];
651
- const content = firstChoice?.message?.content;
652
- if (!content) {
653
- throw new Error("OpenAI API returned empty image description");
654
- }
655
- return {
656
- title: parseTitleFromResponse(content),
657
- description: parseDescriptionFromResponse(content)
658
- };
765
+ const modelName = getImageDescriptionModel(runtime);
766
+ const maxTokens = getImageDescriptionMaxTokens(runtime);
767
+ import_core7.logger.debug(
768
+ `[OpenAI] Using IMAGE_DESCRIPTION model: ${modelName}`,
769
+ );
770
+ let imageUrl;
771
+ let promptText;
772
+ if (typeof params === "string") {
773
+ imageUrl = params;
774
+ promptText = DEFAULT_IMAGE_DESCRIPTION_PROMPT;
775
+ } else {
776
+ imageUrl = params.imageUrl;
777
+ promptText = params.prompt ?? DEFAULT_IMAGE_DESCRIPTION_PROMPT;
778
+ }
779
+ if (!imageUrl || imageUrl.trim().length === 0) {
780
+ throw new Error("IMAGE_DESCRIPTION requires a valid image URL");
781
+ }
782
+ const baseURL = getBaseURL(runtime);
783
+ const requestBody = {
784
+ model: modelName,
785
+ messages: [
786
+ {
787
+ role: "user",
788
+ content: [
789
+ { type: "text", text: promptText },
790
+ { type: "image_url", image_url: { url: imageUrl } },
791
+ ],
792
+ },
793
+ ],
794
+ max_tokens: maxTokens,
795
+ };
796
+ const response = await fetch(`${baseURL}/chat/completions`, {
797
+ method: "POST",
798
+ headers: {
799
+ ...getAuthHeader(runtime),
800
+ "Content-Type": "application/json",
801
+ },
802
+ body: JSON.stringify(requestBody),
803
+ });
804
+ if (!response.ok) {
805
+ const errorText = await response.text().catch(() => "Unknown error");
806
+ throw new Error(
807
+ `OpenAI image description failed: ${response.status} ${response.statusText} - ${errorText}`,
808
+ );
809
+ }
810
+ const data = await response.json();
811
+ if (data.usage) {
812
+ emitModelUsageEvent(
813
+ runtime,
814
+ import_core7.ModelType.IMAGE_DESCRIPTION,
815
+ typeof params === "string" ? params : (params.prompt ?? ""),
816
+ {
817
+ promptTokens: data.usage.prompt_tokens,
818
+ completionTokens: data.usage.completion_tokens,
819
+ totalTokens: data.usage.total_tokens,
820
+ },
821
+ );
822
+ }
823
+ const firstChoice = data.choices?.[0];
824
+ const content = firstChoice?.message?.content;
825
+ if (!content) {
826
+ throw new Error("OpenAI API returned empty image description");
827
+ }
828
+ return {
829
+ title: parseTitleFromResponse(content),
830
+ description: parseDescriptionFromResponse(content),
831
+ };
659
832
  }
660
833
  // models/object.ts
661
834
  var import_core9 = require("@elizaos/core");
@@ -665,335 +838,492 @@ var import_ai2 = require("ai");
665
838
  var import_openai = require("@ai-sdk/openai");
666
839
  var PROXY_API_KEY = "sk-proxy";
667
840
  function createOpenAIClient(runtime) {
668
- const baseURL = getBaseURL(runtime);
669
- const apiKey = getApiKey(runtime);
670
- if (!apiKey && isProxyMode(runtime)) {
671
- return import_openai.createOpenAI({
672
- apiKey: PROXY_API_KEY,
673
- baseURL
674
- });
675
- }
676
- if (!apiKey) {
677
- throw new Error("OPENAI_API_KEY is required. Set it in your environment variables or runtime settings.");
678
- }
679
- return import_openai.createOpenAI({
680
- apiKey,
681
- baseURL
682
- });
841
+ const baseURL = getBaseURL(runtime);
842
+ const apiKey = getApiKey(runtime);
843
+ if (!apiKey && isProxyMode(runtime)) {
844
+ return import_openai.createOpenAI({
845
+ apiKey: PROXY_API_KEY,
846
+ baseURL,
847
+ });
848
+ }
849
+ if (!apiKey) {
850
+ throw new Error(
851
+ "OPENAI_API_KEY is required. Set it in your environment variables or runtime settings.",
852
+ );
853
+ }
854
+ return import_openai.createOpenAI({
855
+ apiKey,
856
+ baseURL,
857
+ });
683
858
  }
684
859
  // utils/json.ts
685
860
  var import_core8 = require("@elizaos/core");
686
861
  var import_ai = require("ai");
687
862
  var JSON_CLEANUP_PATTERNS = {
688
- MARKDOWN_JSON: /```json\n|\n```|```/g,
689
- WHITESPACE: /^\s+|\s+$/g
863
+ MARKDOWN_JSON: /```json\n|\n```|```/g,
864
+ WHITESPACE: /^\s+|\s+$/g,
690
865
  };
691
866
  function getJsonRepairFunction() {
692
- return async ({ text, error }) => {
693
- if (!(error instanceof import_ai.JSONParseError)) {
694
- return null;
695
- }
696
- try {
697
- const cleanedText = text.replace(JSON_CLEANUP_PATTERNS.MARKDOWN_JSON, "");
698
- JSON.parse(cleanedText);
699
- import_core8.logger.debug("[JSON Repair] Successfully repaired JSON by removing markdown wrappers");
700
- return cleanedText;
701
- } catch {
702
- import_core8.logger.warn("[JSON Repair] Unable to repair JSON text");
703
- return null;
704
- }
705
- };
867
+ return async ({ text, error }) => {
868
+ if (!(error instanceof import_ai.JSONParseError)) {
869
+ return null;
870
+ }
871
+ try {
872
+ const cleanedText = text.replace(JSON_CLEANUP_PATTERNS.MARKDOWN_JSON, "");
873
+ JSON.parse(cleanedText);
874
+ import_core8.logger.debug(
875
+ "[JSON Repair] Successfully repaired JSON by removing markdown wrappers",
876
+ );
877
+ return cleanedText;
878
+ } catch {
879
+ import_core8.logger.warn("[JSON Repair] Unable to repair JSON text");
880
+ return null;
881
+ }
882
+ };
706
883
  }
707
884
 
708
885
  // models/object.ts
709
- async function generateObjectByModelType(runtime, params, modelType, getModelFn) {
710
- const openai = createOpenAIClient(runtime);
711
- const modelName = getModelFn(runtime);
712
- import_core9.logger.debug(`[OpenAI] Using ${modelType} model: ${modelName}`);
713
- if (!params.prompt || params.prompt.trim().length === 0) {
714
- throw new Error("Object generation requires a non-empty prompt");
715
- }
716
- if (params.schema) {
717
- import_core9.logger.debug("[OpenAI] Schema provided but using no-schema mode. " + "Structure is determined by prompt instructions.");
718
- }
719
- const model = openai.chat(modelName);
720
- const { object, usage } = await import_ai2.generateObject({
721
- model,
722
- output: "no-schema",
723
- prompt: params.prompt,
724
- experimental_repairText: getJsonRepairFunction()
725
- });
726
- if (usage) {
727
- emitModelUsageEvent(runtime, modelType, params.prompt, usage);
728
- }
729
- if (typeof object !== "object" || object === null) {
730
- throw new Error(`Object generation returned ${typeof object}, expected object`);
731
- }
732
- return object;
886
+ async function generateObjectByModelType(
887
+ runtime,
888
+ params,
889
+ modelType,
890
+ getModelFn,
891
+ ) {
892
+ const openai = createOpenAIClient(runtime);
893
+ const modelName = getModelFn(runtime);
894
+ import_core9.logger.debug(`[OpenAI] Using ${modelType} model: ${modelName}`);
895
+ if (!params.prompt || params.prompt.trim().length === 0) {
896
+ throw new Error("Object generation requires a non-empty prompt");
897
+ }
898
+ if (params.schema) {
899
+ import_core9.logger.debug(
900
+ "[OpenAI] Schema provided but using no-schema mode. " +
901
+ "Structure is determined by prompt instructions.",
902
+ );
903
+ }
904
+ const model = openai.chat(modelName);
905
+ const { object, usage } = await import_ai2.generateObject({
906
+ model,
907
+ output: "no-schema",
908
+ prompt: params.prompt,
909
+ experimental_repairText: getJsonRepairFunction(),
910
+ });
911
+ if (usage) {
912
+ emitModelUsageEvent(runtime, modelType, params.prompt, usage);
913
+ }
914
+ if (typeof object !== "object" || object === null) {
915
+ throw new Error(
916
+ `Object generation returned ${typeof object}, expected object`,
917
+ );
918
+ }
919
+ return object;
733
920
  }
734
921
  async function handleObjectSmall(runtime, params) {
735
- return generateObjectByModelType(runtime, params, import_core9.ModelType.OBJECT_SMALL, getSmallModel);
922
+ return generateObjectByModelType(
923
+ runtime,
924
+ params,
925
+ import_core9.ModelType.OBJECT_SMALL,
926
+ getSmallModel,
927
+ );
736
928
  }
737
929
  async function handleObjectLarge(runtime, params) {
738
- return generateObjectByModelType(runtime, params, import_core9.ModelType.OBJECT_LARGE, getLargeModel);
930
+ return generateObjectByModelType(
931
+ runtime,
932
+ params,
933
+ import_core9.ModelType.OBJECT_LARGE,
934
+ getLargeModel,
935
+ );
739
936
  }
740
937
  // models/research.ts
741
938
  var import_core10 = require("@elizaos/core");
742
939
  function convertToolToApi(tool) {
743
- switch (tool.type) {
744
- case "web_search_preview":
745
- return { type: "web_search_preview" };
746
- case "file_search":
747
- return {
748
- type: "file_search",
749
- vector_store_ids: tool.vectorStoreIds
750
- };
751
- case "code_interpreter":
752
- return {
753
- type: "code_interpreter",
754
- container: tool.container ?? { type: "auto" }
755
- };
756
- case "mcp":
757
- return {
758
- type: "mcp",
759
- server_label: tool.serverLabel,
760
- server_url: tool.serverUrl,
761
- require_approval: tool.requireApproval ?? "never"
762
- };
763
- default:
764
- throw new Error(`Unknown research tool type: ${tool.type}`);
765
- }
940
+ switch (tool.type) {
941
+ case "web_search_preview":
942
+ return { type: "web_search_preview" };
943
+ case "file_search":
944
+ return {
945
+ type: "file_search",
946
+ vector_store_ids: tool.vectorStoreIds,
947
+ };
948
+ case "code_interpreter":
949
+ return {
950
+ type: "code_interpreter",
951
+ container: tool.container ?? { type: "auto" },
952
+ };
953
+ case "mcp":
954
+ return {
955
+ type: "mcp",
956
+ server_label: tool.serverLabel,
957
+ server_url: tool.serverUrl,
958
+ require_approval: tool.requireApproval ?? "never",
959
+ };
960
+ default:
961
+ throw new Error(`Unknown research tool type: ${tool.type}`);
962
+ }
766
963
  }
767
964
  function convertOutputItem(item) {
768
- switch (item.type) {
769
- case "web_search_call":
770
- return {
771
- id: item.id ?? "",
772
- type: "web_search_call",
773
- status: item.status ?? "completed",
774
- action: {
775
- type: item.action?.type ?? "search",
776
- query: item.action?.query,
777
- url: item.action?.url
778
- }
779
- };
780
- case "file_search_call":
781
- return {
782
- id: item.id ?? "",
783
- type: "file_search_call",
784
- status: item.status ?? "completed",
785
- query: item.query ?? "",
786
- results: item.results?.map((r) => ({
787
- fileId: r.file_id,
788
- fileName: r.file_name,
789
- score: r.score
790
- }))
791
- };
792
- case "code_interpreter_call":
793
- return {
794
- id: item.id ?? "",
795
- type: "code_interpreter_call",
796
- status: item.status ?? "completed",
797
- code: item.code ?? "",
798
- output: item.output
799
- };
800
- case "mcp_tool_call":
801
- return {
802
- id: item.id ?? "",
803
- type: "mcp_tool_call",
804
- status: item.status ?? "completed",
805
- serverLabel: item.server_label ?? "",
806
- toolName: item.tool_name ?? "",
807
- arguments: item.arguments ?? {},
808
- result: item.result
809
- };
810
- case "message":
811
- return {
812
- type: "message",
813
- content: item.content?.map((c) => ({
814
- type: "output_text",
815
- text: c.text,
816
- annotations: c.annotations?.map((a) => ({
817
- url: a.url,
818
- title: a.title,
819
- startIndex: a.start_index,
820
- endIndex: a.end_index
821
- })) ?? []
822
- })) ?? []
823
- };
824
- default:
825
- return null;
826
- }
965
+ switch (item.type) {
966
+ case "web_search_call":
967
+ return {
968
+ id: item.id ?? "",
969
+ type: "web_search_call",
970
+ status: item.status ?? "completed",
971
+ action: {
972
+ type: item.action?.type ?? "search",
973
+ query: item.action?.query,
974
+ url: item.action?.url,
975
+ },
976
+ };
977
+ case "file_search_call":
978
+ return {
979
+ id: item.id ?? "",
980
+ type: "file_search_call",
981
+ status: item.status ?? "completed",
982
+ query: item.query ?? "",
983
+ results: item.results?.map((r) => ({
984
+ fileId: r.file_id,
985
+ fileName: r.file_name,
986
+ score: r.score,
987
+ })),
988
+ };
989
+ case "code_interpreter_call":
990
+ return {
991
+ id: item.id ?? "",
992
+ type: "code_interpreter_call",
993
+ status: item.status ?? "completed",
994
+ code: item.code ?? "",
995
+ output: item.output,
996
+ };
997
+ case "mcp_tool_call":
998
+ return {
999
+ id: item.id ?? "",
1000
+ type: "mcp_tool_call",
1001
+ status: item.status ?? "completed",
1002
+ serverLabel: item.server_label ?? "",
1003
+ toolName: item.tool_name ?? "",
1004
+ arguments: item.arguments ?? {},
1005
+ result: item.result,
1006
+ };
1007
+ case "message":
1008
+ return {
1009
+ type: "message",
1010
+ content:
1011
+ item.content?.map((c) => ({
1012
+ type: "output_text",
1013
+ text: c.text,
1014
+ annotations:
1015
+ c.annotations?.map((a) => ({
1016
+ url: a.url,
1017
+ title: a.title,
1018
+ startIndex: a.start_index,
1019
+ endIndex: a.end_index,
1020
+ })) ?? [],
1021
+ })) ?? [],
1022
+ };
1023
+ default:
1024
+ return null;
1025
+ }
827
1026
  }
828
1027
  function extractTextAndAnnotations(response) {
829
- if (response.output_text) {
830
- const annotations2 = [];
831
- if (response.output) {
832
- for (const item of response.output) {
833
- if (item.type === "message" && item.content) {
834
- for (const content of item.content) {
835
- if (content.annotations) {
836
- for (const ann of content.annotations) {
837
- annotations2.push({
838
- url: ann.url,
839
- title: ann.title,
840
- startIndex: ann.start_index,
841
- endIndex: ann.end_index
842
- });
843
- }
844
- }
845
- }
846
- }
847
- }
848
- }
849
- return { text: response.output_text, annotations: annotations2 };
850
- }
851
- let text = "";
852
- const annotations = [];
853
- if (response.output) {
854
- for (const item of response.output) {
855
- if (item.type === "message" && item.content) {
856
- for (const content of item.content) {
857
- text += content.text;
858
- if (content.annotations) {
859
- for (const ann of content.annotations) {
860
- annotations.push({
861
- url: ann.url,
862
- title: ann.title,
863
- startIndex: ann.start_index,
864
- endIndex: ann.end_index
865
- });
866
- }
867
- }
868
- }
869
- }
870
- }
871
- }
872
- return { text, annotations };
1028
+ if (response.output_text) {
1029
+ const annotations2 = [];
1030
+ if (response.output) {
1031
+ for (const item of response.output) {
1032
+ if (item.type === "message" && item.content) {
1033
+ for (const content of item.content) {
1034
+ if (content.annotations) {
1035
+ for (const ann of content.annotations) {
1036
+ annotations2.push({
1037
+ url: ann.url,
1038
+ title: ann.title,
1039
+ startIndex: ann.start_index,
1040
+ endIndex: ann.end_index,
1041
+ });
1042
+ }
1043
+ }
1044
+ }
1045
+ }
1046
+ }
1047
+ }
1048
+ return { text: response.output_text, annotations: annotations2 };
1049
+ }
1050
+ let text = "";
1051
+ const annotations = [];
1052
+ if (response.output) {
1053
+ for (const item of response.output) {
1054
+ if (item.type === "message" && item.content) {
1055
+ for (const content of item.content) {
1056
+ text += content.text;
1057
+ if (content.annotations) {
1058
+ for (const ann of content.annotations) {
1059
+ annotations.push({
1060
+ url: ann.url,
1061
+ title: ann.title,
1062
+ startIndex: ann.start_index,
1063
+ endIndex: ann.end_index,
1064
+ });
1065
+ }
1066
+ }
1067
+ }
1068
+ }
1069
+ }
1070
+ }
1071
+ return { text, annotations };
873
1072
  }
874
1073
  async function handleResearch(runtime, params) {
875
- const apiKey = getApiKey(runtime);
876
- if (!apiKey) {
877
- throw new Error("OPENAI_API_KEY is required for deep research. Set it in your environment variables or runtime settings.");
878
- }
879
- const baseURL = getBaseURL(runtime);
880
- const modelName = params.model ?? getResearchModel(runtime);
881
- const timeout = getResearchTimeout(runtime);
882
- import_core10.logger.debug(`[OpenAI] Starting deep research with model: ${modelName}`);
883
- import_core10.logger.debug(`[OpenAI] Research input: ${params.input.substring(0, 100)}...`);
884
- const dataSourceTools = params.tools?.filter((t) => t.type === "web_search_preview" || t.type === "file_search" || t.type === "mcp");
885
- if (!dataSourceTools || dataSourceTools.length === 0) {
886
- import_core10.logger.debug("[OpenAI] No data source tools specified, defaulting to web_search_preview");
887
- params.tools = [{ type: "web_search_preview" }, ...params.tools ?? []];
888
- }
889
- const requestBody = {
890
- model: modelName,
891
- input: params.input
892
- };
893
- if (params.instructions) {
894
- requestBody.instructions = params.instructions;
895
- }
896
- if (params.background !== undefined) {
897
- requestBody.background = params.background;
898
- }
899
- if (params.tools && params.tools.length > 0) {
900
- requestBody.tools = params.tools.map(convertToolToApi);
901
- }
902
- if (params.maxToolCalls !== undefined) {
903
- requestBody.max_tool_calls = params.maxToolCalls;
904
- }
905
- if (params.reasoningSummary) {
906
- requestBody.reasoning = { summary: params.reasoningSummary };
907
- }
908
- import_core10.logger.debug(`[OpenAI] Research request body: ${JSON.stringify(requestBody, null, 2)}`);
909
- const response = await fetch(`${baseURL}/responses`, {
910
- method: "POST",
911
- headers: {
912
- Authorization: `Bearer ${apiKey}`,
913
- "Content-Type": "application/json"
914
- },
915
- body: JSON.stringify(requestBody),
916
- signal: AbortSignal.timeout(timeout)
917
- });
918
- if (!response.ok) {
919
- const errorText = await response.text();
920
- import_core10.logger.error(`[OpenAI] Research request failed: ${response.status} ${errorText}`);
921
- throw new Error(`Deep research request failed: ${response.status} ${response.statusText}`);
922
- }
923
- const data = await response.json();
924
- if (data.error) {
925
- import_core10.logger.error(`[OpenAI] Research API error: ${data.error.message}`);
926
- throw new Error(`Deep research error: ${data.error.message}`);
927
- }
928
- import_core10.logger.debug(`[OpenAI] Research response received. Status: ${data.status ?? "completed"}`);
929
- const { text, annotations } = extractTextAndAnnotations(data);
930
- const outputItems = [];
931
- if (data.output) {
932
- for (const item of data.output) {
933
- const converted = convertOutputItem(item);
934
- if (converted) {
935
- outputItems.push(converted);
936
- }
937
- }
938
- }
939
- const result = {
940
- id: data.id,
941
- text,
942
- annotations,
943
- outputItems,
944
- status: data.status
945
- };
946
- import_core10.logger.info(`[OpenAI] Research completed. Text length: ${text.length}, Annotations: ${annotations.length}, Output items: ${outputItems.length}`);
947
- return result;
1074
+ const apiKey = getApiKey(runtime);
1075
+ if (!apiKey) {
1076
+ throw new Error(
1077
+ "OPENAI_API_KEY is required for deep research. Set it in your environment variables or runtime settings.",
1078
+ );
1079
+ }
1080
+ const baseURL = getBaseURL(runtime);
1081
+ const modelName = params.model ?? getResearchModel(runtime);
1082
+ const timeout = getResearchTimeout(runtime);
1083
+ import_core10.logger.debug(
1084
+ `[OpenAI] Starting deep research with model: ${modelName}`,
1085
+ );
1086
+ import_core10.logger.debug(
1087
+ `[OpenAI] Research input: ${params.input.substring(0, 100)}...`,
1088
+ );
1089
+ const dataSourceTools = params.tools?.filter(
1090
+ (t) =>
1091
+ t.type === "web_search_preview" ||
1092
+ t.type === "file_search" ||
1093
+ t.type === "mcp",
1094
+ );
1095
+ if (!dataSourceTools || dataSourceTools.length === 0) {
1096
+ import_core10.logger.debug(
1097
+ "[OpenAI] No data source tools specified, defaulting to web_search_preview",
1098
+ );
1099
+ params.tools = [{ type: "web_search_preview" }, ...(params.tools ?? [])];
1100
+ }
1101
+ const requestBody = {
1102
+ model: modelName,
1103
+ input: params.input,
1104
+ };
1105
+ if (params.instructions) {
1106
+ requestBody.instructions = params.instructions;
1107
+ }
1108
+ if (params.background !== undefined) {
1109
+ requestBody.background = params.background;
1110
+ }
1111
+ if (params.tools && params.tools.length > 0) {
1112
+ requestBody.tools = params.tools.map(convertToolToApi);
1113
+ }
1114
+ if (params.maxToolCalls !== undefined) {
1115
+ requestBody.max_tool_calls = params.maxToolCalls;
1116
+ }
1117
+ if (params.reasoningSummary) {
1118
+ requestBody.reasoning = { summary: params.reasoningSummary };
1119
+ }
1120
+ import_core10.logger.debug(
1121
+ `[OpenAI] Research request body: ${JSON.stringify(requestBody, null, 2)}`,
1122
+ );
1123
+ const response = await fetch(`${baseURL}/responses`, {
1124
+ method: "POST",
1125
+ headers: {
1126
+ Authorization: `Bearer ${apiKey}`,
1127
+ "Content-Type": "application/json",
1128
+ },
1129
+ body: JSON.stringify(requestBody),
1130
+ signal: AbortSignal.timeout(timeout),
1131
+ });
1132
+ if (!response.ok) {
1133
+ const errorText = await response.text();
1134
+ import_core10.logger.error(
1135
+ `[OpenAI] Research request failed: ${response.status} ${errorText}`,
1136
+ );
1137
+ throw new Error(
1138
+ `Deep research request failed: ${response.status} ${response.statusText}`,
1139
+ );
1140
+ }
1141
+ const data = await response.json();
1142
+ if (data.error) {
1143
+ import_core10.logger.error(
1144
+ `[OpenAI] Research API error: ${data.error.message}`,
1145
+ );
1146
+ throw new Error(`Deep research error: ${data.error.message}`);
1147
+ }
1148
+ import_core10.logger.debug(
1149
+ `[OpenAI] Research response received. Status: ${data.status ?? "completed"}`,
1150
+ );
1151
+ const { text, annotations } = extractTextAndAnnotations(data);
1152
+ const outputItems = [];
1153
+ if (data.output) {
1154
+ for (const item of data.output) {
1155
+ const converted = convertOutputItem(item);
1156
+ if (converted) {
1157
+ outputItems.push(converted);
1158
+ }
1159
+ }
1160
+ }
1161
+ const result = {
1162
+ id: data.id,
1163
+ text,
1164
+ annotations,
1165
+ outputItems,
1166
+ status: data.status,
1167
+ };
1168
+ import_core10.logger.info(
1169
+ `[OpenAI] Research completed. Text length: ${text.length}, Annotations: ${annotations.length}, Output items: ${outputItems.length}`,
1170
+ );
1171
+ return result;
948
1172
  }
949
1173
  // models/text.ts
950
1174
  var import_core11 = require("@elizaos/core");
951
1175
  var import_ai3 = require("ai");
1176
+ var TEXT_NANO_MODEL_TYPE = import_core11.ModelType.TEXT_NANO ?? "TEXT_NANO";
1177
+ var TEXT_MEDIUM_MODEL_TYPE =
1178
+ import_core11.ModelType.TEXT_MEDIUM ?? "TEXT_MEDIUM";
1179
+ var TEXT_MEGA_MODEL_TYPE = import_core11.ModelType.TEXT_MEGA ?? "TEXT_MEGA";
1180
+ var RESPONSE_HANDLER_MODEL_TYPE =
1181
+ import_core11.ModelType.RESPONSE_HANDLER ?? "RESPONSE_HANDLER";
1182
+ var ACTION_PLANNER_MODEL_TYPE =
1183
+ import_core11.ModelType.ACTION_PLANNER ?? "ACTION_PLANNER";
1184
+ function buildUserContent(params) {
1185
+ const content = [{ type: "text", text: params.prompt }];
1186
+ for (const attachment of params.attachments ?? []) {
1187
+ content.push({
1188
+ type: "file",
1189
+ data: attachment.data,
1190
+ mediaType: attachment.mediaType,
1191
+ ...(attachment.filename ? { filename: attachment.filename } : {}),
1192
+ });
1193
+ }
1194
+ return content;
1195
+ }
952
1196
  function convertUsage(usage) {
953
- if (!usage) {
954
- return;
955
- }
956
- const promptTokens = usage.inputTokens ?? 0;
957
- const completionTokens = usage.outputTokens ?? 0;
958
- return {
959
- promptTokens,
960
- completionTokens,
961
- totalTokens: promptTokens + completionTokens
962
- };
1197
+ if (!usage) {
1198
+ return;
1199
+ }
1200
+ const promptTokens = usage.inputTokens ?? 0;
1201
+ const completionTokens = usage.outputTokens ?? 0;
1202
+ const usageWithCache = usage;
1203
+ return {
1204
+ promptTokens,
1205
+ completionTokens,
1206
+ totalTokens: promptTokens + completionTokens,
1207
+ cachedPromptTokens: usageWithCache.cachedInputTokens,
1208
+ };
1209
+ }
1210
+ function resolvePromptCacheOptions(params) {
1211
+ const withOpenAIOptions = params;
1212
+ return {
1213
+ promptCacheKey: withOpenAIOptions.providerOptions?.openai?.promptCacheKey,
1214
+ promptCacheRetention:
1215
+ withOpenAIOptions.providerOptions?.openai?.promptCacheRetention,
1216
+ };
963
1217
  }
964
1218
  async function generateTextByModelType(runtime, params, modelType, getModelFn) {
965
- const openai = createOpenAIClient(runtime);
966
- const modelName = getModelFn(runtime);
967
- import_core11.logger.debug(`[OpenAI] Using ${modelType} model: ${modelName}`);
968
- const systemPrompt = runtime.character.system ?? undefined;
969
- const model = openai.chat(modelName);
970
- const generateParams = {
971
- model,
972
- prompt: params.prompt,
973
- system: systemPrompt,
974
- maxOutputTokens: params.maxTokens ?? 8192,
975
- experimental_telemetry: { isEnabled: getExperimentalTelemetry(runtime) }
976
- };
977
- if (params.stream) {
978
- const result = import_ai3.streamText(generateParams);
979
- return {
980
- textStream: result.textStream,
981
- text: Promise.resolve(result.text),
982
- usage: Promise.resolve(result.usage).then(convertUsage),
983
- finishReason: Promise.resolve(result.finishReason).then((r) => r)
984
- };
985
- }
986
- const { text, usage } = await import_ai3.generateText(generateParams);
987
- if (usage) {
988
- emitModelUsageEvent(runtime, modelType, params.prompt, usage);
989
- }
990
- return text;
1219
+ const paramsWithAttachments = params;
1220
+ const openai = createOpenAIClient(runtime);
1221
+ const modelName = getModelFn(runtime);
1222
+ import_core11.logger.debug(`[OpenAI] Using ${modelType} model: ${modelName}`);
1223
+ const promptCacheOptions = resolvePromptCacheOptions(params);
1224
+ const hasAttachments = (paramsWithAttachments.attachments?.length ?? 0) > 0;
1225
+ const userContent = hasAttachments
1226
+ ? buildUserContent(paramsWithAttachments)
1227
+ : undefined;
1228
+ const systemPrompt = runtime.character.system ?? undefined;
1229
+ const model = openai.chat(modelName);
1230
+ const generateParams = {
1231
+ model,
1232
+ ...(userContent
1233
+ ? { messages: [{ role: "user", content: userContent }] }
1234
+ : { prompt: params.prompt }),
1235
+ system: systemPrompt,
1236
+ maxOutputTokens: params.maxTokens ?? 8192,
1237
+ experimental_telemetry: { isEnabled: getExperimentalTelemetry(runtime) },
1238
+ ...(promptCacheOptions.promptCacheKey ||
1239
+ promptCacheOptions.promptCacheRetention
1240
+ ? {
1241
+ providerOptions: {
1242
+ openai: {
1243
+ ...(promptCacheOptions.promptCacheKey
1244
+ ? { promptCacheKey: promptCacheOptions.promptCacheKey }
1245
+ : {}),
1246
+ ...(promptCacheOptions.promptCacheRetention
1247
+ ? {
1248
+ promptCacheRetention:
1249
+ promptCacheOptions.promptCacheRetention,
1250
+ }
1251
+ : {}),
1252
+ },
1253
+ },
1254
+ }
1255
+ : {}),
1256
+ };
1257
+ if (params.stream) {
1258
+ const result = import_ai3.streamText(generateParams);
1259
+ return {
1260
+ textStream: result.textStream,
1261
+ text: Promise.resolve(result.text),
1262
+ usage: Promise.resolve(result.usage).then(convertUsage),
1263
+ finishReason: Promise.resolve(result.finishReason).then((r) => r),
1264
+ };
1265
+ }
1266
+ const { text, usage } = await import_ai3.generateText(generateParams);
1267
+ if (usage) {
1268
+ emitModelUsageEvent(runtime, modelType, params.prompt, usage);
1269
+ }
1270
+ return text;
991
1271
  }
992
1272
  async function handleTextSmall(runtime, params) {
993
- return generateTextByModelType(runtime, params, import_core11.ModelType.TEXT_SMALL, getSmallModel);
1273
+ return generateTextByModelType(
1274
+ runtime,
1275
+ params,
1276
+ import_core11.ModelType.TEXT_SMALL,
1277
+ getSmallModel,
1278
+ );
1279
+ }
1280
+ async function handleTextNano(runtime, params) {
1281
+ return generateTextByModelType(
1282
+ runtime,
1283
+ params,
1284
+ TEXT_NANO_MODEL_TYPE,
1285
+ getNanoModel,
1286
+ );
1287
+ }
1288
+ async function handleTextMedium(runtime, params) {
1289
+ return generateTextByModelType(
1290
+ runtime,
1291
+ params,
1292
+ TEXT_MEDIUM_MODEL_TYPE,
1293
+ getMediumModel,
1294
+ );
994
1295
  }
995
1296
  async function handleTextLarge(runtime, params) {
996
- return generateTextByModelType(runtime, params, import_core11.ModelType.TEXT_LARGE, getLargeModel);
1297
+ return generateTextByModelType(
1298
+ runtime,
1299
+ params,
1300
+ import_core11.ModelType.TEXT_LARGE,
1301
+ getLargeModel,
1302
+ );
1303
+ }
1304
+ async function handleTextMega(runtime, params) {
1305
+ return generateTextByModelType(
1306
+ runtime,
1307
+ params,
1308
+ TEXT_MEGA_MODEL_TYPE,
1309
+ getMegaModel,
1310
+ );
1311
+ }
1312
+ async function handleResponseHandler(runtime, params) {
1313
+ return generateTextByModelType(
1314
+ runtime,
1315
+ params,
1316
+ RESPONSE_HANDLER_MODEL_TYPE,
1317
+ getResponseHandlerModel,
1318
+ );
1319
+ }
1320
+ async function handleActionPlanner(runtime, params) {
1321
+ return generateTextByModelType(
1322
+ runtime,
1323
+ params,
1324
+ ACTION_PLANNER_MODEL_TYPE,
1325
+ getActionPlannerModel,
1326
+ );
997
1327
  }
998
1328
  // models/tokenizer.ts
999
1329
  var import_core13 = require("@elizaos/core");
@@ -1002,289 +1332,417 @@ var import_core13 = require("@elizaos/core");
1002
1332
  var import_core12 = require("@elizaos/core");
1003
1333
  var import_js_tiktoken = require("js-tiktoken");
1004
1334
  function resolveTokenizerEncoding(modelName) {
1005
- const normalized = modelName.toLowerCase();
1006
- const fallbackEncoding = normalized.includes("4o") ? "o200k_base" : "cl100k_base";
1007
- try {
1008
- return import_js_tiktoken.encodingForModel(modelName);
1009
- } catch {
1010
- return import_js_tiktoken.getEncoding(fallbackEncoding);
1011
- }
1335
+ const normalized = modelName.toLowerCase();
1336
+ const fallbackEncoding = normalized.includes("4o")
1337
+ ? "o200k_base"
1338
+ : "cl100k_base";
1339
+ try {
1340
+ return import_js_tiktoken.encodingForModel(modelName);
1341
+ } catch {
1342
+ return import_js_tiktoken.getEncoding(fallbackEncoding);
1343
+ }
1012
1344
  }
1013
1345
  function getModelName(runtime, modelType) {
1014
- if (modelType === import_core12.ModelType.TEXT_SMALL) {
1015
- return getSmallModel(runtime);
1016
- }
1017
- return getLargeModel(runtime);
1346
+ if (modelType === import_core12.ModelType.TEXT_SMALL) {
1347
+ return getSmallModel(runtime);
1348
+ }
1349
+ return getLargeModel(runtime);
1018
1350
  }
1019
1351
  function tokenizeText(runtime, modelType, text) {
1020
- const modelName = getModelName(runtime, modelType);
1021
- const encoder = resolveTokenizerEncoding(modelName);
1022
- return encoder.encode(text);
1352
+ const modelName = getModelName(runtime, modelType);
1353
+ const encoder = resolveTokenizerEncoding(modelName);
1354
+ return encoder.encode(text);
1023
1355
  }
1024
1356
  function detokenizeText(runtime, modelType, tokens) {
1025
- const modelName = getModelName(runtime, modelType);
1026
- const encoder = resolveTokenizerEncoding(modelName);
1027
- return encoder.decode(tokens);
1357
+ const modelName = getModelName(runtime, modelType);
1358
+ const encoder = resolveTokenizerEncoding(modelName);
1359
+ return encoder.decode(tokens);
1028
1360
  }
1029
1361
 
1030
1362
  // models/tokenizer.ts
1031
1363
  async function handleTokenizerEncode(runtime, params) {
1032
- if (!params.prompt) {
1033
- throw new Error("Tokenization requires a non-empty prompt");
1034
- }
1035
- const modelType = params.modelType ?? import_core13.ModelType.TEXT_LARGE;
1036
- return tokenizeText(runtime, modelType, params.prompt);
1364
+ if (!params.prompt) {
1365
+ throw new Error("Tokenization requires a non-empty prompt");
1366
+ }
1367
+ const modelType = params.modelType ?? import_core13.ModelType.TEXT_LARGE;
1368
+ return tokenizeText(runtime, modelType, params.prompt);
1037
1369
  }
1038
1370
  async function handleTokenizerDecode(runtime, params) {
1039
- if (!params.tokens || !Array.isArray(params.tokens)) {
1040
- throw new Error("Detokenization requires a valid tokens array");
1041
- }
1042
- if (params.tokens.length === 0) {
1043
- return "";
1044
- }
1045
- for (let i = 0;i < params.tokens.length; i++) {
1046
- const token = params.tokens[i];
1047
- if (typeof token !== "number" || !Number.isFinite(token)) {
1048
- throw new Error(`Invalid token at index ${i}: expected number`);
1049
- }
1050
- }
1051
- const modelType = params.modelType ?? import_core13.ModelType.TEXT_LARGE;
1052
- return detokenizeText(runtime, modelType, params.tokens);
1371
+ if (!params.tokens || !Array.isArray(params.tokens)) {
1372
+ throw new Error("Detokenization requires a valid tokens array");
1373
+ }
1374
+ if (params.tokens.length === 0) {
1375
+ return "";
1376
+ }
1377
+ for (let i = 0; i < params.tokens.length; i++) {
1378
+ const token = params.tokens[i];
1379
+ if (typeof token !== "number" || !Number.isFinite(token)) {
1380
+ throw new Error(`Invalid token at index ${i}: expected number`);
1381
+ }
1382
+ }
1383
+ const modelType = params.modelType ?? import_core13.ModelType.TEXT_LARGE;
1384
+ return detokenizeText(runtime, modelType, params.tokens);
1053
1385
  }
1054
1386
  // index.ts
1055
1387
  function getProcessEnv() {
1056
- if (typeof process === "undefined") {
1057
- return {};
1058
- }
1059
- return process.env;
1388
+ if (typeof process === "undefined") {
1389
+ return {};
1390
+ }
1391
+ return process.env;
1060
1392
  }
1061
1393
  var env = getProcessEnv();
1394
+ var TEXT_NANO_MODEL_TYPE2 = import_core14.ModelType.TEXT_NANO ?? "TEXT_NANO";
1395
+ var TEXT_MEDIUM_MODEL_TYPE2 =
1396
+ import_core14.ModelType.TEXT_MEDIUM ?? "TEXT_MEDIUM";
1397
+ var TEXT_MEGA_MODEL_TYPE2 = import_core14.ModelType.TEXT_MEGA ?? "TEXT_MEGA";
1398
+ var RESPONSE_HANDLER_MODEL_TYPE2 =
1399
+ import_core14.ModelType.RESPONSE_HANDLER ?? "RESPONSE_HANDLER";
1400
+ var ACTION_PLANNER_MODEL_TYPE2 =
1401
+ import_core14.ModelType.ACTION_PLANNER ?? "ACTION_PLANNER";
1062
1402
  var openaiPlugin = {
1063
- name: "openai",
1064
- description: "OpenAI API integration for text, image, audio, and embedding models",
1065
- config: {
1066
- OPENAI_API_KEY: env.OPENAI_API_KEY ?? null,
1067
- OPENAI_BASE_URL: env.OPENAI_BASE_URL ?? null,
1068
- OPENAI_SMALL_MODEL: env.OPENAI_SMALL_MODEL ?? null,
1069
- OPENAI_LARGE_MODEL: env.OPENAI_LARGE_MODEL ?? null,
1070
- SMALL_MODEL: env.SMALL_MODEL ?? null,
1071
- LARGE_MODEL: env.LARGE_MODEL ?? null,
1072
- OPENAI_EMBEDDING_MODEL: env.OPENAI_EMBEDDING_MODEL ?? null,
1073
- OPENAI_EMBEDDING_API_KEY: env.OPENAI_EMBEDDING_API_KEY ?? null,
1074
- OPENAI_EMBEDDING_URL: env.OPENAI_EMBEDDING_URL ?? null,
1075
- OPENAI_EMBEDDING_DIMENSIONS: env.OPENAI_EMBEDDING_DIMENSIONS ?? null,
1076
- OPENAI_IMAGE_DESCRIPTION_MODEL: env.OPENAI_IMAGE_DESCRIPTION_MODEL ?? null,
1077
- OPENAI_IMAGE_DESCRIPTION_MAX_TOKENS: env.OPENAI_IMAGE_DESCRIPTION_MAX_TOKENS ?? null,
1078
- OPENAI_EXPERIMENTAL_TELEMETRY: env.OPENAI_EXPERIMENTAL_TELEMETRY ?? null,
1079
- OPENAI_RESEARCH_MODEL: env.OPENAI_RESEARCH_MODEL ?? null,
1080
- OPENAI_RESEARCH_TIMEOUT: env.OPENAI_RESEARCH_TIMEOUT ?? null
1081
- },
1082
- async init(config, runtime) {
1083
- initializeOpenAI(config, runtime);
1084
- },
1085
- models: {
1086
- [import_core14.ModelType.TEXT_EMBEDDING]: async (runtime, params) => {
1087
- return handleTextEmbedding(runtime, params);
1088
- },
1089
- [import_core14.ModelType.TEXT_TOKENIZER_ENCODE]: async (runtime, params) => {
1090
- return handleTokenizerEncode(runtime, params);
1091
- },
1092
- [import_core14.ModelType.TEXT_TOKENIZER_DECODE]: async (runtime, params) => {
1093
- return handleTokenizerDecode(runtime, params);
1094
- },
1095
- [import_core14.ModelType.TEXT_SMALL]: async (runtime, params) => {
1096
- return handleTextSmall(runtime, params);
1097
- },
1098
- [import_core14.ModelType.TEXT_LARGE]: async (runtime, params) => {
1099
- return handleTextLarge(runtime, params);
1100
- },
1101
- [import_core14.ModelType.IMAGE]: async (runtime, params) => {
1102
- return handleImageGeneration(runtime, params);
1103
- },
1104
- [import_core14.ModelType.IMAGE_DESCRIPTION]: async (runtime, params) => {
1105
- return handleImageDescription(runtime, params);
1106
- },
1107
- [import_core14.ModelType.TRANSCRIPTION]: async (runtime, input) => {
1108
- return handleTranscription(runtime, input);
1109
- },
1110
- [import_core14.ModelType.TEXT_TO_SPEECH]: async (runtime, input) => {
1111
- return handleTextToSpeech(runtime, input);
1112
- },
1113
- [import_core14.ModelType.OBJECT_SMALL]: async (runtime, params) => {
1114
- return handleObjectSmall(runtime, params);
1115
- },
1116
- [import_core14.ModelType.OBJECT_LARGE]: async (runtime, params) => {
1117
- return handleObjectLarge(runtime, params);
1118
- },
1119
- [import_core14.ModelType.RESEARCH]: async (runtime, params) => {
1120
- return handleResearch(runtime, params);
1121
- }
1122
- },
1123
- tests: [
1124
- {
1125
- name: "openai_plugin_tests",
1126
- tests: [
1127
- {
1128
- name: "openai_test_api_connectivity",
1129
- fn: async (runtime) => {
1130
- const baseURL = getBaseURL(runtime);
1131
- const response = await fetch(`${baseURL}/models`, {
1132
- headers: getAuthHeader(runtime)
1133
- });
1134
- if (!response.ok) {
1135
- throw new Error(`API connectivity test failed: ${response.status} ${response.statusText}`);
1136
- }
1137
- const data = await response.json();
1138
- import_core14.logger.info(`[OpenAI Test] API connected. ${data.data?.length ?? 0} models available.`);
1139
- }
1140
- },
1141
- {
1142
- name: "openai_test_text_embedding",
1143
- fn: async (runtime) => {
1144
- const embedding = await runtime.useModel(import_core14.ModelType.TEXT_EMBEDDING, {
1145
- text: "Hello, world!"
1146
- });
1147
- if (!Array.isArray(embedding) || embedding.length === 0) {
1148
- throw new Error("Embedding should return a non-empty array");
1149
- }
1150
- import_core14.logger.info(`[OpenAI Test] Generated embedding with ${embedding.length} dimensions`);
1151
- }
1152
- },
1153
- {
1154
- name: "openai_test_text_small",
1155
- fn: async (runtime) => {
1156
- const text = await runtime.useModel(import_core14.ModelType.TEXT_SMALL, {
1157
- prompt: "Say hello in exactly 5 words."
1158
- });
1159
- if (typeof text !== "string" || text.length === 0) {
1160
- throw new Error("TEXT_SMALL should return non-empty string");
1161
- }
1162
- import_core14.logger.info(`[OpenAI Test] TEXT_SMALL generated: "${text.substring(0, 50)}..."`);
1163
- }
1164
- },
1165
- {
1166
- name: "openai_test_text_large",
1167
- fn: async (runtime) => {
1168
- const text = await runtime.useModel(import_core14.ModelType.TEXT_LARGE, {
1169
- prompt: "Explain quantum computing in 2 sentences."
1170
- });
1171
- if (typeof text !== "string" || text.length === 0) {
1172
- throw new Error("TEXT_LARGE should return non-empty string");
1173
- }
1174
- import_core14.logger.info(`[OpenAI Test] TEXT_LARGE generated: "${text.substring(0, 50)}..."`);
1175
- }
1176
- },
1177
- {
1178
- name: "openai_test_tokenizer_roundtrip",
1179
- fn: async (runtime) => {
1180
- const originalText = "Hello, tokenizer test!";
1181
- const tokens = await runtime.useModel(import_core14.ModelType.TEXT_TOKENIZER_ENCODE, {
1182
- prompt: originalText,
1183
- modelType: import_core14.ModelType.TEXT_SMALL
1184
- });
1185
- if (!Array.isArray(tokens) || tokens.length === 0) {
1186
- throw new Error("Tokenization should return non-empty token array");
1187
- }
1188
- const decodedText = await runtime.useModel(import_core14.ModelType.TEXT_TOKENIZER_DECODE, {
1189
- tokens,
1190
- modelType: import_core14.ModelType.TEXT_SMALL
1191
- });
1192
- if (decodedText !== originalText) {
1193
- throw new Error(`Tokenizer roundtrip failed: expected "${originalText}", got "${decodedText}"`);
1194
- }
1195
- import_core14.logger.info(`[OpenAI Test] Tokenizer roundtrip successful (${tokens.length} tokens)`);
1196
- }
1197
- },
1198
- {
1199
- name: "openai_test_streaming",
1200
- fn: async (runtime) => {
1201
- const chunks = [];
1202
- const result = await runtime.useModel(import_core14.ModelType.TEXT_LARGE, {
1203
- prompt: "Count from 1 to 5, one number per line.",
1204
- stream: true,
1205
- onStreamChunk: (chunk) => {
1206
- chunks.push(chunk);
1207
- }
1208
- });
1209
- if (typeof result !== "string" || result.length === 0) {
1210
- throw new Error("Streaming should return non-empty result");
1211
- }
1212
- if (chunks.length === 0) {
1213
- throw new Error("No streaming chunks received");
1214
- }
1215
- import_core14.logger.info(`[OpenAI Test] Streaming test: ${chunks.length} chunks received`);
1216
- }
1217
- },
1218
- {
1219
- name: "openai_test_image_description",
1220
- fn: async (runtime) => {
1221
- const testImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Camponotus_flavomarginatus_ant.jpg/440px-Camponotus_flavomarginatus_ant.jpg";
1222
- const result = await runtime.useModel(import_core14.ModelType.IMAGE_DESCRIPTION, testImageUrl);
1223
- if (!result || typeof result !== "object" || !("title" in result) || !("description" in result)) {
1224
- throw new Error("Image description should return { title, description }");
1225
- }
1226
- import_core14.logger.info(`[OpenAI Test] Image described: "${result.title}"`);
1227
- }
1228
- },
1229
- {
1230
- name: "openai_test_transcription",
1231
- fn: async (runtime) => {
1232
- const audioUrl = "https://upload.wikimedia.org/wikipedia/commons/2/25/En-Open_Source.ogg";
1233
- const response = await fetch(audioUrl);
1234
- const arrayBuffer = await response.arrayBuffer();
1235
- const audioBuffer = Buffer.from(new Uint8Array(arrayBuffer));
1236
- const transcription = await runtime.useModel(import_core14.ModelType.TRANSCRIPTION, audioBuffer);
1237
- if (typeof transcription !== "string") {
1238
- throw new Error("Transcription should return a string");
1239
- }
1240
- import_core14.logger.info(`[OpenAI Test] Transcription: "${transcription.substring(0, 50)}..."`);
1241
- }
1242
- },
1243
- {
1244
- name: "openai_test_text_to_speech",
1245
- fn: async (runtime) => {
1246
- const audioData = await runtime.useModel(import_core14.ModelType.TEXT_TO_SPEECH, {
1247
- text: "Hello, this is a text-to-speech test."
1248
- });
1249
- if (!(audioData instanceof ArrayBuffer) || audioData.byteLength === 0) {
1250
- throw new Error("TTS should return non-empty ArrayBuffer");
1251
- }
1252
- import_core14.logger.info(`[OpenAI Test] TTS generated ${audioData.byteLength} bytes of audio`);
1253
- }
1254
- },
1255
- {
1256
- name: "openai_test_object_generation",
1257
- fn: async (runtime) => {
1258
- const result = await runtime.useModel(import_core14.ModelType.OBJECT_SMALL, {
1259
- prompt: "Return a JSON object with exactly these fields: name (string), age (number), active (boolean)"
1260
- });
1261
- if (!result || typeof result !== "object") {
1262
- throw new Error("Object generation should return an object");
1263
- }
1264
- import_core14.logger.info(`[OpenAI Test] Object generated: ${JSON.stringify(result).substring(0, 100)}`);
1265
- }
1266
- },
1267
- {
1268
- name: "openai_test_research",
1269
- fn: async (runtime) => {
1270
- const result = await runtime.useModel(import_core14.ModelType.RESEARCH, {
1271
- input: "What is the current date and time?",
1272
- tools: [{ type: "web_search_preview" }],
1273
- maxToolCalls: 3
1274
- });
1275
- if (!result || typeof result !== "object" || !("text" in result)) {
1276
- throw new Error("Research should return an object with text property");
1277
- }
1278
- if (typeof result.text !== "string" || result.text.length === 0) {
1279
- throw new Error("Research result text should be a non-empty string");
1280
- }
1281
- import_core14.logger.info(`[OpenAI Test] Research completed. Text length: ${result.text.length}, Annotations: ${result.annotations?.length ?? 0}`);
1282
- }
1283
- }
1284
- ]
1285
- }
1286
- ]
1403
+ name: "openai",
1404
+ description:
1405
+ "OpenAI API integration for text, image, audio, and embedding models",
1406
+ config: {
1407
+ OPENAI_API_KEY: env.OPENAI_API_KEY ?? null,
1408
+ OPENAI_BASE_URL: env.OPENAI_BASE_URL ?? null,
1409
+ OPENAI_NANO_MODEL: env.OPENAI_NANO_MODEL ?? null,
1410
+ OPENAI_MEDIUM_MODEL: env.OPENAI_MEDIUM_MODEL ?? null,
1411
+ OPENAI_SMALL_MODEL: env.OPENAI_SMALL_MODEL ?? null,
1412
+ OPENAI_LARGE_MODEL: env.OPENAI_LARGE_MODEL ?? null,
1413
+ OPENAI_MEGA_MODEL: env.OPENAI_MEGA_MODEL ?? null,
1414
+ OPENAI_RESPONSE_HANDLER_MODEL: env.OPENAI_RESPONSE_HANDLER_MODEL ?? null,
1415
+ OPENAI_SHOULD_RESPOND_MODEL: env.OPENAI_SHOULD_RESPOND_MODEL ?? null,
1416
+ OPENAI_ACTION_PLANNER_MODEL: env.OPENAI_ACTION_PLANNER_MODEL ?? null,
1417
+ OPENAI_PLANNER_MODEL: env.OPENAI_PLANNER_MODEL ?? null,
1418
+ NANO_MODEL: env.NANO_MODEL ?? null,
1419
+ MEDIUM_MODEL: env.MEDIUM_MODEL ?? null,
1420
+ SMALL_MODEL: env.SMALL_MODEL ?? null,
1421
+ LARGE_MODEL: env.LARGE_MODEL ?? null,
1422
+ MEGA_MODEL: env.MEGA_MODEL ?? null,
1423
+ RESPONSE_HANDLER_MODEL: env.RESPONSE_HANDLER_MODEL ?? null,
1424
+ SHOULD_RESPOND_MODEL: env.SHOULD_RESPOND_MODEL ?? null,
1425
+ ACTION_PLANNER_MODEL: env.ACTION_PLANNER_MODEL ?? null,
1426
+ PLANNER_MODEL: env.PLANNER_MODEL ?? null,
1427
+ OPENAI_EMBEDDING_MODEL: env.OPENAI_EMBEDDING_MODEL ?? null,
1428
+ OPENAI_EMBEDDING_API_KEY: env.OPENAI_EMBEDDING_API_KEY ?? null,
1429
+ OPENAI_EMBEDDING_URL: env.OPENAI_EMBEDDING_URL ?? null,
1430
+ OPENAI_EMBEDDING_DIMENSIONS: env.OPENAI_EMBEDDING_DIMENSIONS ?? null,
1431
+ OPENAI_IMAGE_DESCRIPTION_MODEL: env.OPENAI_IMAGE_DESCRIPTION_MODEL ?? null,
1432
+ OPENAI_IMAGE_DESCRIPTION_MAX_TOKENS:
1433
+ env.OPENAI_IMAGE_DESCRIPTION_MAX_TOKENS ?? null,
1434
+ OPENAI_EXPERIMENTAL_TELEMETRY: env.OPENAI_EXPERIMENTAL_TELEMETRY ?? null,
1435
+ OPENAI_RESEARCH_MODEL: env.OPENAI_RESEARCH_MODEL ?? null,
1436
+ OPENAI_RESEARCH_TIMEOUT: env.OPENAI_RESEARCH_TIMEOUT ?? null,
1437
+ },
1438
+ async init(config, runtime) {
1439
+ initializeOpenAI(config, runtime);
1440
+ },
1441
+ models: {
1442
+ [import_core14.ModelType.TEXT_EMBEDDING]: async (runtime, params) => {
1443
+ return handleTextEmbedding(runtime, params);
1444
+ },
1445
+ [import_core14.ModelType.TEXT_TOKENIZER_ENCODE]: async (
1446
+ runtime,
1447
+ params,
1448
+ ) => {
1449
+ return handleTokenizerEncode(runtime, params);
1450
+ },
1451
+ [import_core14.ModelType.TEXT_TOKENIZER_DECODE]: async (
1452
+ runtime,
1453
+ params,
1454
+ ) => {
1455
+ return handleTokenizerDecode(runtime, params);
1456
+ },
1457
+ [import_core14.ModelType.TEXT_SMALL]: async (runtime, params) => {
1458
+ return handleTextSmall(runtime, params);
1459
+ },
1460
+ [TEXT_NANO_MODEL_TYPE2]: async (runtime, params) => {
1461
+ return handleTextNano(runtime, params);
1462
+ },
1463
+ [TEXT_MEDIUM_MODEL_TYPE2]: async (runtime, params) => {
1464
+ return handleTextMedium(runtime, params);
1465
+ },
1466
+ [import_core14.ModelType.TEXT_LARGE]: async (runtime, params) => {
1467
+ return handleTextLarge(runtime, params);
1468
+ },
1469
+ [TEXT_MEGA_MODEL_TYPE2]: async (runtime, params) => {
1470
+ return handleTextMega(runtime, params);
1471
+ },
1472
+ [RESPONSE_HANDLER_MODEL_TYPE2]: async (runtime, params) => {
1473
+ return handleResponseHandler(runtime, params);
1474
+ },
1475
+ [ACTION_PLANNER_MODEL_TYPE2]: async (runtime, params) => {
1476
+ return handleActionPlanner(runtime, params);
1477
+ },
1478
+ [import_core14.ModelType.IMAGE]: async (runtime, params) => {
1479
+ return handleImageGeneration(runtime, params);
1480
+ },
1481
+ [import_core14.ModelType.IMAGE_DESCRIPTION]: async (runtime, params) => {
1482
+ return handleImageDescription(runtime, params);
1483
+ },
1484
+ [import_core14.ModelType.TRANSCRIPTION]: async (runtime, input) => {
1485
+ return handleTranscription(runtime, input);
1486
+ },
1487
+ [import_core14.ModelType.TEXT_TO_SPEECH]: async (runtime, input) => {
1488
+ return handleTextToSpeech(runtime, input);
1489
+ },
1490
+ [import_core14.ModelType.OBJECT_SMALL]: async (runtime, params) => {
1491
+ return handleObjectSmall(runtime, params);
1492
+ },
1493
+ [import_core14.ModelType.OBJECT_LARGE]: async (runtime, params) => {
1494
+ return handleObjectLarge(runtime, params);
1495
+ },
1496
+ [import_core14.ModelType.RESEARCH]: async (runtime, params) => {
1497
+ return handleResearch(runtime, params);
1498
+ },
1499
+ },
1500
+ tests: [
1501
+ {
1502
+ name: "openai_plugin_tests",
1503
+ tests: [
1504
+ {
1505
+ name: "openai_test_api_connectivity",
1506
+ fn: async (runtime) => {
1507
+ const baseURL = getBaseURL(runtime);
1508
+ const response = await fetch(`${baseURL}/models`, {
1509
+ headers: getAuthHeader(runtime),
1510
+ });
1511
+ if (!response.ok) {
1512
+ throw new Error(
1513
+ `API connectivity test failed: ${response.status} ${response.statusText}`,
1514
+ );
1515
+ }
1516
+ const data = await response.json();
1517
+ import_core14.logger.info(
1518
+ `[OpenAI Test] API connected. ${data.data?.length ?? 0} models available.`,
1519
+ );
1520
+ },
1521
+ },
1522
+ {
1523
+ name: "openai_test_text_embedding",
1524
+ fn: async (runtime) => {
1525
+ const embedding = await runtime.useModel(
1526
+ import_core14.ModelType.TEXT_EMBEDDING,
1527
+ {
1528
+ text: "Hello, world!",
1529
+ },
1530
+ );
1531
+ if (!Array.isArray(embedding) || embedding.length === 0) {
1532
+ throw new Error("Embedding should return a non-empty array");
1533
+ }
1534
+ import_core14.logger.info(
1535
+ `[OpenAI Test] Generated embedding with ${embedding.length} dimensions`,
1536
+ );
1537
+ },
1538
+ },
1539
+ {
1540
+ name: "openai_test_text_small",
1541
+ fn: async (runtime) => {
1542
+ const text = await runtime.useModel(
1543
+ import_core14.ModelType.TEXT_SMALL,
1544
+ {
1545
+ prompt: "Say hello in exactly 5 words.",
1546
+ },
1547
+ );
1548
+ if (typeof text !== "string" || text.length === 0) {
1549
+ throw new Error("TEXT_SMALL should return non-empty string");
1550
+ }
1551
+ import_core14.logger.info(
1552
+ `[OpenAI Test] TEXT_SMALL generated: "${text.substring(0, 50)}..."`,
1553
+ );
1554
+ },
1555
+ },
1556
+ {
1557
+ name: "openai_test_text_large",
1558
+ fn: async (runtime) => {
1559
+ const text = await runtime.useModel(
1560
+ import_core14.ModelType.TEXT_LARGE,
1561
+ {
1562
+ prompt: "Explain quantum computing in 2 sentences.",
1563
+ },
1564
+ );
1565
+ if (typeof text !== "string" || text.length === 0) {
1566
+ throw new Error("TEXT_LARGE should return non-empty string");
1567
+ }
1568
+ import_core14.logger.info(
1569
+ `[OpenAI Test] TEXT_LARGE generated: "${text.substring(0, 50)}..."`,
1570
+ );
1571
+ },
1572
+ },
1573
+ {
1574
+ name: "openai_test_tokenizer_roundtrip",
1575
+ fn: async (runtime) => {
1576
+ const originalText = "Hello, tokenizer test!";
1577
+ const tokens = await runtime.useModel(
1578
+ import_core14.ModelType.TEXT_TOKENIZER_ENCODE,
1579
+ {
1580
+ prompt: originalText,
1581
+ modelType: import_core14.ModelType.TEXT_SMALL,
1582
+ },
1583
+ );
1584
+ if (!Array.isArray(tokens) || tokens.length === 0) {
1585
+ throw new Error(
1586
+ "Tokenization should return non-empty token array",
1587
+ );
1588
+ }
1589
+ const decodedText = await runtime.useModel(
1590
+ import_core14.ModelType.TEXT_TOKENIZER_DECODE,
1591
+ {
1592
+ tokens,
1593
+ modelType: import_core14.ModelType.TEXT_SMALL,
1594
+ },
1595
+ );
1596
+ if (decodedText !== originalText) {
1597
+ throw new Error(
1598
+ `Tokenizer roundtrip failed: expected "${originalText}", got "${decodedText}"`,
1599
+ );
1600
+ }
1601
+ import_core14.logger.info(
1602
+ `[OpenAI Test] Tokenizer roundtrip successful (${tokens.length} tokens)`,
1603
+ );
1604
+ },
1605
+ },
1606
+ {
1607
+ name: "openai_test_streaming",
1608
+ fn: async (runtime) => {
1609
+ const chunks = [];
1610
+ const result = await runtime.useModel(
1611
+ import_core14.ModelType.TEXT_LARGE,
1612
+ {
1613
+ prompt: "Count from 1 to 5, one number per line.",
1614
+ stream: true,
1615
+ onStreamChunk: (chunk) => {
1616
+ chunks.push(chunk);
1617
+ },
1618
+ },
1619
+ );
1620
+ if (typeof result !== "string" || result.length === 0) {
1621
+ throw new Error("Streaming should return non-empty result");
1622
+ }
1623
+ if (chunks.length === 0) {
1624
+ throw new Error("No streaming chunks received");
1625
+ }
1626
+ import_core14.logger.info(
1627
+ `[OpenAI Test] Streaming test: ${chunks.length} chunks received`,
1628
+ );
1629
+ },
1630
+ },
1631
+ {
1632
+ name: "openai_test_image_description",
1633
+ fn: async (runtime) => {
1634
+ const testImageUrl =
1635
+ "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Camponotus_flavomarginatus_ant.jpg/440px-Camponotus_flavomarginatus_ant.jpg";
1636
+ const result = await runtime.useModel(
1637
+ import_core14.ModelType.IMAGE_DESCRIPTION,
1638
+ testImageUrl,
1639
+ );
1640
+ if (
1641
+ !result ||
1642
+ typeof result !== "object" ||
1643
+ !("title" in result) ||
1644
+ !("description" in result)
1645
+ ) {
1646
+ throw new Error(
1647
+ "Image description should return { title, description }",
1648
+ );
1649
+ }
1650
+ import_core14.logger.info(
1651
+ `[OpenAI Test] Image described: "${result.title}"`,
1652
+ );
1653
+ },
1654
+ },
1655
+ {
1656
+ name: "openai_test_transcription",
1657
+ fn: async (runtime) => {
1658
+ const audioUrl =
1659
+ "https://upload.wikimedia.org/wikipedia/commons/2/25/En-Open_Source.ogg";
1660
+ const response = await fetch(audioUrl);
1661
+ const arrayBuffer = await response.arrayBuffer();
1662
+ const audioBuffer = Buffer.from(new Uint8Array(arrayBuffer));
1663
+ const transcription = await runtime.useModel(
1664
+ import_core14.ModelType.TRANSCRIPTION,
1665
+ audioBuffer,
1666
+ );
1667
+ if (typeof transcription !== "string") {
1668
+ throw new Error("Transcription should return a string");
1669
+ }
1670
+ import_core14.logger.info(
1671
+ `[OpenAI Test] Transcription: "${transcription.substring(0, 50)}..."`,
1672
+ );
1673
+ },
1674
+ },
1675
+ {
1676
+ name: "openai_test_text_to_speech",
1677
+ fn: async (runtime) => {
1678
+ const audioData = await runtime.useModel(
1679
+ import_core14.ModelType.TEXT_TO_SPEECH,
1680
+ {
1681
+ text: "Hello, this is a text-to-speech test.",
1682
+ },
1683
+ );
1684
+ if (
1685
+ !(audioData instanceof ArrayBuffer) ||
1686
+ audioData.byteLength === 0
1687
+ ) {
1688
+ throw new Error("TTS should return non-empty ArrayBuffer");
1689
+ }
1690
+ import_core14.logger.info(
1691
+ `[OpenAI Test] TTS generated ${audioData.byteLength} bytes of audio`,
1692
+ );
1693
+ },
1694
+ },
1695
+ {
1696
+ name: "openai_test_object_generation",
1697
+ fn: async (runtime) => {
1698
+ const result = await runtime.useModel(
1699
+ import_core14.ModelType.OBJECT_SMALL,
1700
+ {
1701
+ prompt:
1702
+ "Return a JSON object with exactly these fields: name (string), age (number), active (boolean)",
1703
+ },
1704
+ );
1705
+ if (!result || typeof result !== "object") {
1706
+ throw new Error("Object generation should return an object");
1707
+ }
1708
+ import_core14.logger.info(
1709
+ `[OpenAI Test] Object generated: ${JSON.stringify(result).substring(0, 100)}`,
1710
+ );
1711
+ },
1712
+ },
1713
+ {
1714
+ name: "openai_test_research",
1715
+ fn: async (runtime) => {
1716
+ const result = await runtime.useModel(
1717
+ import_core14.ModelType.RESEARCH,
1718
+ {
1719
+ input: "What is the current date and time?",
1720
+ tools: [{ type: "web_search_preview" }],
1721
+ maxToolCalls: 3,
1722
+ },
1723
+ );
1724
+ if (!result || typeof result !== "object" || !("text" in result)) {
1725
+ throw new Error(
1726
+ "Research should return an object with text property",
1727
+ );
1728
+ }
1729
+ if (typeof result.text !== "string" || result.text.length === 0) {
1730
+ throw new Error(
1731
+ "Research result text should be a non-empty string",
1732
+ );
1733
+ }
1734
+ import_core14.logger.info(
1735
+ `[OpenAI Test] Research completed. Text length: ${result.text.length}, Annotations: ${result.annotations?.length ?? 0}`,
1736
+ );
1737
+ },
1738
+ },
1739
+ ],
1740
+ },
1741
+ ],
1287
1742
  };
1288
1743
  var typescript_default = openaiPlugin;
1289
1744
 
1290
- //# debugId=C2F3BD7F1D67EDF864756E2164756E21
1745
+ // index.node.ts
1746
+ var index_node_default = typescript_default;
1747
+
1748
+ //# debugId=236821D6733C981C64756E2164756E21