@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.
- package/LICENSE +21 -0
- package/README.md +163 -0
- package/dist/browser/index.browser.js +2 -2
- package/dist/browser/index.browser.js.map +12 -11
- package/dist/build.d.ts +1 -1
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/index.node.cjs +1533 -1075
- package/dist/cjs/index.node.js.map +10 -9
- package/dist/generated/specs/specs.d.ts +27 -27
- package/dist/index.browser.d.ts +2 -1
- package/dist/index.browser.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.node.d.ts +2 -1
- package/dist/index.node.d.ts.map +1 -1
- package/dist/models/embedding.d.ts.map +1 -1
- package/dist/models/index.d.ts +1 -1
- package/dist/models/index.d.ts.map +1 -1
- package/dist/models/text.d.ts +5 -0
- package/dist/models/text.d.ts.map +1 -1
- package/dist/node/index.node.js +129 -16
- package/dist/node/index.node.js.map +12 -11
- package/dist/types/index.d.ts +16 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/utils/config.d.ts +5 -0
- package/dist/utils/config.d.ts.map +1 -1
- package/dist/utils/events.d.ts +6 -0
- package/dist/utils/events.d.ts.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/package.json +14 -11
package/dist/cjs/index.node.cjs
CHANGED
|
@@ -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
|
-
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
6
8
|
var __toCommonJS = (from) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
|
|
33
|
-
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
|
|
89
|
+
return (
|
|
90
|
+
typeof globalThis !== "undefined" &&
|
|
91
|
+
typeof globalThis.document !== "undefined"
|
|
92
|
+
);
|
|
80
93
|
}
|
|
81
94
|
function isProxyMode(runtime) {
|
|
82
|
-
|
|
95
|
+
return isBrowser() && !!getSetting(runtime, "OPENAI_BROWSER_BASE_URL");
|
|
83
96
|
}
|
|
84
97
|
function getApiKey(runtime) {
|
|
85
|
-
|
|
98
|
+
return getSetting(runtime, "OPENAI_API_KEY");
|
|
86
99
|
}
|
|
87
100
|
function getEmbeddingApiKey(runtime) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
200
|
+
return (
|
|
201
|
+
getSetting(runtime, "OPENAI_EMBEDDING_MODEL") ?? "text-embedding-3-small"
|
|
202
|
+
);
|
|
126
203
|
}
|
|
127
204
|
function getImageDescriptionModel(runtime) {
|
|
128
|
-
|
|
205
|
+
return getSetting(runtime, "OPENAI_IMAGE_DESCRIPTION_MODEL") ?? "gpt-5-mini";
|
|
129
206
|
}
|
|
130
207
|
function getTranscriptionModel(runtime) {
|
|
131
|
-
|
|
208
|
+
return (
|
|
209
|
+
getSetting(runtime, "OPENAI_TRANSCRIPTION_MODEL") ?? "gpt-5-mini-transcribe"
|
|
210
|
+
);
|
|
132
211
|
}
|
|
133
212
|
function getTTSModel(runtime) {
|
|
134
|
-
|
|
213
|
+
return getSetting(runtime, "OPENAI_TTS_MODEL") ?? "tts-1";
|
|
135
214
|
}
|
|
136
215
|
function getTTSVoice(runtime) {
|
|
137
|
-
|
|
216
|
+
return getSetting(runtime, "OPENAI_TTS_VOICE") ?? "nova";
|
|
138
217
|
}
|
|
139
218
|
function getTTSInstructions(runtime) {
|
|
140
|
-
|
|
219
|
+
return getSetting(runtime, "OPENAI_TTS_INSTRUCTIONS") ?? "";
|
|
141
220
|
}
|
|
142
221
|
function getImageModel(runtime) {
|
|
143
|
-
|
|
222
|
+
return getSetting(runtime, "OPENAI_IMAGE_MODEL") ?? "dall-e-3";
|
|
144
223
|
}
|
|
145
224
|
function getExperimentalTelemetry(runtime) {
|
|
146
|
-
|
|
225
|
+
return getBooleanSetting(runtime, "OPENAI_EXPERIMENTAL_TELEMETRY", false);
|
|
147
226
|
}
|
|
148
227
|
function getEmbeddingDimensions(runtime) {
|
|
149
|
-
|
|
228
|
+
return getNumericSetting(runtime, "OPENAI_EMBEDDING_DIMENSIONS", 1536);
|
|
150
229
|
}
|
|
151
230
|
function getImageDescriptionMaxTokens(runtime) {
|
|
152
|
-
|
|
231
|
+
return getNumericSetting(
|
|
232
|
+
runtime,
|
|
233
|
+
"OPENAI_IMAGE_DESCRIPTION_MAX_TOKENS",
|
|
234
|
+
8192,
|
|
235
|
+
);
|
|
153
236
|
}
|
|
154
237
|
function getResearchModel(runtime) {
|
|
155
|
-
|
|
238
|
+
return getSetting(runtime, "OPENAI_RESEARCH_MODEL") ?? "o3-deep-research";
|
|
156
239
|
}
|
|
157
240
|
function getResearchTimeout(runtime) {
|
|
158
|
-
|
|
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
|
-
|
|
247
|
+
validateOpenAIConfiguration(runtime);
|
|
165
248
|
}
|
|
166
249
|
async function validateOpenAIConfiguration(runtime) {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
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
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
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
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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
|
-
|
|
264
|
-
|
|
365
|
+
const ext = getExtensionForMimeType(mimeType);
|
|
366
|
+
return `recording.${ext}`;
|
|
265
367
|
}
|
|
266
368
|
|
|
267
369
|
// models/audio.ts
|
|
268
370
|
function isBlobOrFile(value) {
|
|
269
|
-
|
|
371
|
+
return value instanceof Blob || value instanceof File;
|
|
270
372
|
}
|
|
271
373
|
function isBuffer(value) {
|
|
272
|
-
|
|
374
|
+
return Buffer.isBuffer(value);
|
|
273
375
|
}
|
|
274
376
|
function isLocalTranscriptionParams(value) {
|
|
275
|
-
|
|
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
|
-
|
|
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
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
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
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
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
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
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
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
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
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
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
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
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
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
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
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
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
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
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 =
|
|
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
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
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
|
-
|
|
595
|
-
|
|
758
|
+
const titleMatch = content.match(/title[:\s]+(.+?)(?:\n|$)/i);
|
|
759
|
+
return titleMatch?.[1]?.trim() ?? "Image Analysis";
|
|
596
760
|
}
|
|
597
761
|
function parseDescriptionFromResponse(content) {
|
|
598
|
-
|
|
762
|
+
return content.replace(/title[:\s]+(.+?)(?:\n|$)/i, "").trim();
|
|
599
763
|
}
|
|
600
764
|
async function handleImageDescription(runtime, params) {
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
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
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
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
|
-
|
|
689
|
-
|
|
863
|
+
MARKDOWN_JSON: /```json\n|\n```|```/g,
|
|
864
|
+
WHITESPACE: /^\s+|\s+$/g,
|
|
690
865
|
};
|
|
691
866
|
function getJsonRepairFunction() {
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
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(
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
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
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
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
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
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
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
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
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
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
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
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
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
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
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
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
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
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
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
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
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
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
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
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
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
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
|
-
|
|
1745
|
+
// index.node.ts
|
|
1746
|
+
var index_node_default = typescript_default;
|
|
1747
|
+
|
|
1748
|
+
//# debugId=236821D6733C981C64756E2164756E21
|