@elizaos/plugin-openai 1.0.0-beta.8 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -3
- package/dist/index.d.ts +9 -0
- package/dist/index.js +1252 -226
- package/dist/index.js.map +1 -1
- package/package.json +12 -10
package/dist/index.js
CHANGED
|
@@ -1,11 +1,595 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { createOpenAI } from "@ai-sdk/openai";
|
|
3
3
|
import {
|
|
4
|
+
EventType,
|
|
5
|
+
logger,
|
|
4
6
|
ModelType,
|
|
5
|
-
|
|
7
|
+
safeReplacer,
|
|
8
|
+
ServiceType,
|
|
9
|
+
VECTOR_DIMS
|
|
6
10
|
} from "@elizaos/core";
|
|
7
|
-
|
|
11
|
+
|
|
12
|
+
// node_modules/@opentelemetry/api/build/esm/platform/node/globalThis.js
|
|
13
|
+
var _globalThis = typeof globalThis === "object" ? globalThis : global;
|
|
14
|
+
|
|
15
|
+
// node_modules/@opentelemetry/api/build/esm/version.js
|
|
16
|
+
var VERSION = "1.9.0";
|
|
17
|
+
|
|
18
|
+
// node_modules/@opentelemetry/api/build/esm/internal/semver.js
|
|
19
|
+
var re = /^(\d+)\.(\d+)\.(\d+)(-(.+))?$/;
|
|
20
|
+
function _makeCompatibilityCheck(ownVersion) {
|
|
21
|
+
var acceptedVersions = /* @__PURE__ */ new Set([ownVersion]);
|
|
22
|
+
var rejectedVersions = /* @__PURE__ */ new Set();
|
|
23
|
+
var myVersionMatch = ownVersion.match(re);
|
|
24
|
+
if (!myVersionMatch) {
|
|
25
|
+
return function() {
|
|
26
|
+
return false;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
var ownVersionParsed = {
|
|
30
|
+
major: +myVersionMatch[1],
|
|
31
|
+
minor: +myVersionMatch[2],
|
|
32
|
+
patch: +myVersionMatch[3],
|
|
33
|
+
prerelease: myVersionMatch[4]
|
|
34
|
+
};
|
|
35
|
+
if (ownVersionParsed.prerelease != null) {
|
|
36
|
+
return function isExactmatch(globalVersion) {
|
|
37
|
+
return globalVersion === ownVersion;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function _reject(v) {
|
|
41
|
+
rejectedVersions.add(v);
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
function _accept(v) {
|
|
45
|
+
acceptedVersions.add(v);
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
return function isCompatible2(globalVersion) {
|
|
49
|
+
if (acceptedVersions.has(globalVersion)) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
if (rejectedVersions.has(globalVersion)) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
var globalVersionMatch = globalVersion.match(re);
|
|
56
|
+
if (!globalVersionMatch) {
|
|
57
|
+
return _reject(globalVersion);
|
|
58
|
+
}
|
|
59
|
+
var globalVersionParsed = {
|
|
60
|
+
major: +globalVersionMatch[1],
|
|
61
|
+
minor: +globalVersionMatch[2],
|
|
62
|
+
patch: +globalVersionMatch[3],
|
|
63
|
+
prerelease: globalVersionMatch[4]
|
|
64
|
+
};
|
|
65
|
+
if (globalVersionParsed.prerelease != null) {
|
|
66
|
+
return _reject(globalVersion);
|
|
67
|
+
}
|
|
68
|
+
if (ownVersionParsed.major !== globalVersionParsed.major) {
|
|
69
|
+
return _reject(globalVersion);
|
|
70
|
+
}
|
|
71
|
+
if (ownVersionParsed.major === 0) {
|
|
72
|
+
if (ownVersionParsed.minor === globalVersionParsed.minor && ownVersionParsed.patch <= globalVersionParsed.patch) {
|
|
73
|
+
return _accept(globalVersion);
|
|
74
|
+
}
|
|
75
|
+
return _reject(globalVersion);
|
|
76
|
+
}
|
|
77
|
+
if (ownVersionParsed.minor <= globalVersionParsed.minor) {
|
|
78
|
+
return _accept(globalVersion);
|
|
79
|
+
}
|
|
80
|
+
return _reject(globalVersion);
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
var isCompatible = _makeCompatibilityCheck(VERSION);
|
|
84
|
+
|
|
85
|
+
// node_modules/@opentelemetry/api/build/esm/internal/global-utils.js
|
|
86
|
+
var major = VERSION.split(".")[0];
|
|
87
|
+
var GLOBAL_OPENTELEMETRY_API_KEY = Symbol.for("opentelemetry.js.api." + major);
|
|
88
|
+
var _global = _globalThis;
|
|
89
|
+
function registerGlobal(type, instance, diag, allowOverride) {
|
|
90
|
+
var _a;
|
|
91
|
+
if (allowOverride === void 0) {
|
|
92
|
+
allowOverride = false;
|
|
93
|
+
}
|
|
94
|
+
var api = _global[GLOBAL_OPENTELEMETRY_API_KEY] = (_a = _global[GLOBAL_OPENTELEMETRY_API_KEY]) !== null && _a !== void 0 ? _a : {
|
|
95
|
+
version: VERSION
|
|
96
|
+
};
|
|
97
|
+
if (!allowOverride && api[type]) {
|
|
98
|
+
var err = new Error("@opentelemetry/api: Attempted duplicate registration of API: " + type);
|
|
99
|
+
diag.error(err.stack || err.message);
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
if (api.version !== VERSION) {
|
|
103
|
+
var err = new Error("@opentelemetry/api: Registration of version v" + api.version + " for " + type + " does not match previously registered API v" + VERSION);
|
|
104
|
+
diag.error(err.stack || err.message);
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
api[type] = instance;
|
|
108
|
+
diag.debug("@opentelemetry/api: Registered a global for " + type + " v" + VERSION + ".");
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
function getGlobal(type) {
|
|
112
|
+
var _a, _b;
|
|
113
|
+
var globalVersion = (_a = _global[GLOBAL_OPENTELEMETRY_API_KEY]) === null || _a === void 0 ? void 0 : _a.version;
|
|
114
|
+
if (!globalVersion || !isCompatible(globalVersion)) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
return (_b = _global[GLOBAL_OPENTELEMETRY_API_KEY]) === null || _b === void 0 ? void 0 : _b[type];
|
|
118
|
+
}
|
|
119
|
+
function unregisterGlobal(type, diag) {
|
|
120
|
+
diag.debug("@opentelemetry/api: Unregistering a global for " + type + " v" + VERSION + ".");
|
|
121
|
+
var api = _global[GLOBAL_OPENTELEMETRY_API_KEY];
|
|
122
|
+
if (api) {
|
|
123
|
+
delete api[type];
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// node_modules/@opentelemetry/api/build/esm/diag/ComponentLogger.js
|
|
128
|
+
var __read = function(o, n) {
|
|
129
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
130
|
+
if (!m) return o;
|
|
131
|
+
var i = m.call(o), r, ar = [], e;
|
|
132
|
+
try {
|
|
133
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
e = { error };
|
|
136
|
+
} finally {
|
|
137
|
+
try {
|
|
138
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
139
|
+
} finally {
|
|
140
|
+
if (e) throw e.error;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return ar;
|
|
144
|
+
};
|
|
145
|
+
var __spreadArray = function(to, from, pack) {
|
|
146
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
147
|
+
if (ar || !(i in from)) {
|
|
148
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
149
|
+
ar[i] = from[i];
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
153
|
+
};
|
|
154
|
+
var DiagComponentLogger = (
|
|
155
|
+
/** @class */
|
|
156
|
+
function() {
|
|
157
|
+
function DiagComponentLogger2(props) {
|
|
158
|
+
this._namespace = props.namespace || "DiagComponentLogger";
|
|
159
|
+
}
|
|
160
|
+
DiagComponentLogger2.prototype.debug = function() {
|
|
161
|
+
var args = [];
|
|
162
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
163
|
+
args[_i] = arguments[_i];
|
|
164
|
+
}
|
|
165
|
+
return logProxy("debug", this._namespace, args);
|
|
166
|
+
};
|
|
167
|
+
DiagComponentLogger2.prototype.error = function() {
|
|
168
|
+
var args = [];
|
|
169
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
170
|
+
args[_i] = arguments[_i];
|
|
171
|
+
}
|
|
172
|
+
return logProxy("error", this._namespace, args);
|
|
173
|
+
};
|
|
174
|
+
DiagComponentLogger2.prototype.info = function() {
|
|
175
|
+
var args = [];
|
|
176
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
177
|
+
args[_i] = arguments[_i];
|
|
178
|
+
}
|
|
179
|
+
return logProxy("info", this._namespace, args);
|
|
180
|
+
};
|
|
181
|
+
DiagComponentLogger2.prototype.warn = function() {
|
|
182
|
+
var args = [];
|
|
183
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
184
|
+
args[_i] = arguments[_i];
|
|
185
|
+
}
|
|
186
|
+
return logProxy("warn", this._namespace, args);
|
|
187
|
+
};
|
|
188
|
+
DiagComponentLogger2.prototype.verbose = function() {
|
|
189
|
+
var args = [];
|
|
190
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
191
|
+
args[_i] = arguments[_i];
|
|
192
|
+
}
|
|
193
|
+
return logProxy("verbose", this._namespace, args);
|
|
194
|
+
};
|
|
195
|
+
return DiagComponentLogger2;
|
|
196
|
+
}()
|
|
197
|
+
);
|
|
198
|
+
function logProxy(funcName, namespace, args) {
|
|
199
|
+
var logger2 = getGlobal("diag");
|
|
200
|
+
if (!logger2) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
args.unshift(namespace);
|
|
204
|
+
return logger2[funcName].apply(logger2, __spreadArray([], __read(args), false));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// node_modules/@opentelemetry/api/build/esm/diag/types.js
|
|
208
|
+
var DiagLogLevel;
|
|
209
|
+
(function(DiagLogLevel2) {
|
|
210
|
+
DiagLogLevel2[DiagLogLevel2["NONE"] = 0] = "NONE";
|
|
211
|
+
DiagLogLevel2[DiagLogLevel2["ERROR"] = 30] = "ERROR";
|
|
212
|
+
DiagLogLevel2[DiagLogLevel2["WARN"] = 50] = "WARN";
|
|
213
|
+
DiagLogLevel2[DiagLogLevel2["INFO"] = 60] = "INFO";
|
|
214
|
+
DiagLogLevel2[DiagLogLevel2["DEBUG"] = 70] = "DEBUG";
|
|
215
|
+
DiagLogLevel2[DiagLogLevel2["VERBOSE"] = 80] = "VERBOSE";
|
|
216
|
+
DiagLogLevel2[DiagLogLevel2["ALL"] = 9999] = "ALL";
|
|
217
|
+
})(DiagLogLevel || (DiagLogLevel = {}));
|
|
218
|
+
|
|
219
|
+
// node_modules/@opentelemetry/api/build/esm/diag/internal/logLevelLogger.js
|
|
220
|
+
function createLogLevelDiagLogger(maxLevel, logger2) {
|
|
221
|
+
if (maxLevel < DiagLogLevel.NONE) {
|
|
222
|
+
maxLevel = DiagLogLevel.NONE;
|
|
223
|
+
} else if (maxLevel > DiagLogLevel.ALL) {
|
|
224
|
+
maxLevel = DiagLogLevel.ALL;
|
|
225
|
+
}
|
|
226
|
+
logger2 = logger2 || {};
|
|
227
|
+
function _filterFunc(funcName, theLevel) {
|
|
228
|
+
var theFunc = logger2[funcName];
|
|
229
|
+
if (typeof theFunc === "function" && maxLevel >= theLevel) {
|
|
230
|
+
return theFunc.bind(logger2);
|
|
231
|
+
}
|
|
232
|
+
return function() {
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
error: _filterFunc("error", DiagLogLevel.ERROR),
|
|
237
|
+
warn: _filterFunc("warn", DiagLogLevel.WARN),
|
|
238
|
+
info: _filterFunc("info", DiagLogLevel.INFO),
|
|
239
|
+
debug: _filterFunc("debug", DiagLogLevel.DEBUG),
|
|
240
|
+
verbose: _filterFunc("verbose", DiagLogLevel.VERBOSE)
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// node_modules/@opentelemetry/api/build/esm/api/diag.js
|
|
245
|
+
var __read2 = function(o, n) {
|
|
246
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
247
|
+
if (!m) return o;
|
|
248
|
+
var i = m.call(o), r, ar = [], e;
|
|
249
|
+
try {
|
|
250
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
251
|
+
} catch (error) {
|
|
252
|
+
e = { error };
|
|
253
|
+
} finally {
|
|
254
|
+
try {
|
|
255
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
256
|
+
} finally {
|
|
257
|
+
if (e) throw e.error;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return ar;
|
|
261
|
+
};
|
|
262
|
+
var __spreadArray2 = function(to, from, pack) {
|
|
263
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
264
|
+
if (ar || !(i in from)) {
|
|
265
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
266
|
+
ar[i] = from[i];
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
270
|
+
};
|
|
271
|
+
var API_NAME = "diag";
|
|
272
|
+
var DiagAPI = (
|
|
273
|
+
/** @class */
|
|
274
|
+
function() {
|
|
275
|
+
function DiagAPI2() {
|
|
276
|
+
function _logProxy(funcName) {
|
|
277
|
+
return function() {
|
|
278
|
+
var args = [];
|
|
279
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
280
|
+
args[_i] = arguments[_i];
|
|
281
|
+
}
|
|
282
|
+
var logger2 = getGlobal("diag");
|
|
283
|
+
if (!logger2)
|
|
284
|
+
return;
|
|
285
|
+
return logger2[funcName].apply(logger2, __spreadArray2([], __read2(args), false));
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
var self = this;
|
|
289
|
+
var setLogger = function(logger2, optionsOrLogLevel) {
|
|
290
|
+
var _a, _b, _c;
|
|
291
|
+
if (optionsOrLogLevel === void 0) {
|
|
292
|
+
optionsOrLogLevel = { logLevel: DiagLogLevel.INFO };
|
|
293
|
+
}
|
|
294
|
+
if (logger2 === self) {
|
|
295
|
+
var err = new Error("Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation");
|
|
296
|
+
self.error((_a = err.stack) !== null && _a !== void 0 ? _a : err.message);
|
|
297
|
+
return false;
|
|
298
|
+
}
|
|
299
|
+
if (typeof optionsOrLogLevel === "number") {
|
|
300
|
+
optionsOrLogLevel = {
|
|
301
|
+
logLevel: optionsOrLogLevel
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
var oldLogger = getGlobal("diag");
|
|
305
|
+
var newLogger = createLogLevelDiagLogger((_b = optionsOrLogLevel.logLevel) !== null && _b !== void 0 ? _b : DiagLogLevel.INFO, logger2);
|
|
306
|
+
if (oldLogger && !optionsOrLogLevel.suppressOverrideMessage) {
|
|
307
|
+
var stack = (_c = new Error().stack) !== null && _c !== void 0 ? _c : "<failed to generate stacktrace>";
|
|
308
|
+
oldLogger.warn("Current logger will be overwritten from " + stack);
|
|
309
|
+
newLogger.warn("Current logger will overwrite one already registered from " + stack);
|
|
310
|
+
}
|
|
311
|
+
return registerGlobal("diag", newLogger, self, true);
|
|
312
|
+
};
|
|
313
|
+
self.setLogger = setLogger;
|
|
314
|
+
self.disable = function() {
|
|
315
|
+
unregisterGlobal(API_NAME, self);
|
|
316
|
+
};
|
|
317
|
+
self.createComponentLogger = function(options) {
|
|
318
|
+
return new DiagComponentLogger(options);
|
|
319
|
+
};
|
|
320
|
+
self.verbose = _logProxy("verbose");
|
|
321
|
+
self.debug = _logProxy("debug");
|
|
322
|
+
self.info = _logProxy("info");
|
|
323
|
+
self.warn = _logProxy("warn");
|
|
324
|
+
self.error = _logProxy("error");
|
|
325
|
+
}
|
|
326
|
+
DiagAPI2.instance = function() {
|
|
327
|
+
if (!this._instance) {
|
|
328
|
+
this._instance = new DiagAPI2();
|
|
329
|
+
}
|
|
330
|
+
return this._instance;
|
|
331
|
+
};
|
|
332
|
+
return DiagAPI2;
|
|
333
|
+
}()
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
// node_modules/@opentelemetry/api/build/esm/context/context.js
|
|
337
|
+
var BaseContext = (
|
|
338
|
+
/** @class */
|
|
339
|
+
/* @__PURE__ */ function() {
|
|
340
|
+
function BaseContext2(parentContext) {
|
|
341
|
+
var self = this;
|
|
342
|
+
self._currentContext = parentContext ? new Map(parentContext) : /* @__PURE__ */ new Map();
|
|
343
|
+
self.getValue = function(key) {
|
|
344
|
+
return self._currentContext.get(key);
|
|
345
|
+
};
|
|
346
|
+
self.setValue = function(key, value) {
|
|
347
|
+
var context2 = new BaseContext2(self._currentContext);
|
|
348
|
+
context2._currentContext.set(key, value);
|
|
349
|
+
return context2;
|
|
350
|
+
};
|
|
351
|
+
self.deleteValue = function(key) {
|
|
352
|
+
var context2 = new BaseContext2(self._currentContext);
|
|
353
|
+
context2._currentContext.delete(key);
|
|
354
|
+
return context2;
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
return BaseContext2;
|
|
358
|
+
}()
|
|
359
|
+
);
|
|
360
|
+
var ROOT_CONTEXT = new BaseContext();
|
|
361
|
+
|
|
362
|
+
// node_modules/@opentelemetry/api/build/esm/context/NoopContextManager.js
|
|
363
|
+
var __read3 = function(o, n) {
|
|
364
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
365
|
+
if (!m) return o;
|
|
366
|
+
var i = m.call(o), r, ar = [], e;
|
|
367
|
+
try {
|
|
368
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
369
|
+
} catch (error) {
|
|
370
|
+
e = { error };
|
|
371
|
+
} finally {
|
|
372
|
+
try {
|
|
373
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
374
|
+
} finally {
|
|
375
|
+
if (e) throw e.error;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
return ar;
|
|
379
|
+
};
|
|
380
|
+
var __spreadArray3 = function(to, from, pack) {
|
|
381
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
382
|
+
if (ar || !(i in from)) {
|
|
383
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
384
|
+
ar[i] = from[i];
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
388
|
+
};
|
|
389
|
+
var NoopContextManager = (
|
|
390
|
+
/** @class */
|
|
391
|
+
function() {
|
|
392
|
+
function NoopContextManager2() {
|
|
393
|
+
}
|
|
394
|
+
NoopContextManager2.prototype.active = function() {
|
|
395
|
+
return ROOT_CONTEXT;
|
|
396
|
+
};
|
|
397
|
+
NoopContextManager2.prototype.with = function(_context, fn, thisArg) {
|
|
398
|
+
var args = [];
|
|
399
|
+
for (var _i = 3; _i < arguments.length; _i++) {
|
|
400
|
+
args[_i - 3] = arguments[_i];
|
|
401
|
+
}
|
|
402
|
+
return fn.call.apply(fn, __spreadArray3([thisArg], __read3(args), false));
|
|
403
|
+
};
|
|
404
|
+
NoopContextManager2.prototype.bind = function(_context, target) {
|
|
405
|
+
return target;
|
|
406
|
+
};
|
|
407
|
+
NoopContextManager2.prototype.enable = function() {
|
|
408
|
+
return this;
|
|
409
|
+
};
|
|
410
|
+
NoopContextManager2.prototype.disable = function() {
|
|
411
|
+
return this;
|
|
412
|
+
};
|
|
413
|
+
return NoopContextManager2;
|
|
414
|
+
}()
|
|
415
|
+
);
|
|
416
|
+
|
|
417
|
+
// node_modules/@opentelemetry/api/build/esm/api/context.js
|
|
418
|
+
var __read4 = function(o, n) {
|
|
419
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
420
|
+
if (!m) return o;
|
|
421
|
+
var i = m.call(o), r, ar = [], e;
|
|
422
|
+
try {
|
|
423
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
424
|
+
} catch (error) {
|
|
425
|
+
e = { error };
|
|
426
|
+
} finally {
|
|
427
|
+
try {
|
|
428
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
429
|
+
} finally {
|
|
430
|
+
if (e) throw e.error;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
return ar;
|
|
434
|
+
};
|
|
435
|
+
var __spreadArray4 = function(to, from, pack) {
|
|
436
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
437
|
+
if (ar || !(i in from)) {
|
|
438
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
439
|
+
ar[i] = from[i];
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
443
|
+
};
|
|
444
|
+
var API_NAME2 = "context";
|
|
445
|
+
var NOOP_CONTEXT_MANAGER = new NoopContextManager();
|
|
446
|
+
var ContextAPI = (
|
|
447
|
+
/** @class */
|
|
448
|
+
function() {
|
|
449
|
+
function ContextAPI2() {
|
|
450
|
+
}
|
|
451
|
+
ContextAPI2.getInstance = function() {
|
|
452
|
+
if (!this._instance) {
|
|
453
|
+
this._instance = new ContextAPI2();
|
|
454
|
+
}
|
|
455
|
+
return this._instance;
|
|
456
|
+
};
|
|
457
|
+
ContextAPI2.prototype.setGlobalContextManager = function(contextManager) {
|
|
458
|
+
return registerGlobal(API_NAME2, contextManager, DiagAPI.instance());
|
|
459
|
+
};
|
|
460
|
+
ContextAPI2.prototype.active = function() {
|
|
461
|
+
return this._getContextManager().active();
|
|
462
|
+
};
|
|
463
|
+
ContextAPI2.prototype.with = function(context2, fn, thisArg) {
|
|
464
|
+
var _a;
|
|
465
|
+
var args = [];
|
|
466
|
+
for (var _i = 3; _i < arguments.length; _i++) {
|
|
467
|
+
args[_i - 3] = arguments[_i];
|
|
468
|
+
}
|
|
469
|
+
return (_a = this._getContextManager()).with.apply(_a, __spreadArray4([context2, fn, thisArg], __read4(args), false));
|
|
470
|
+
};
|
|
471
|
+
ContextAPI2.prototype.bind = function(context2, target) {
|
|
472
|
+
return this._getContextManager().bind(context2, target);
|
|
473
|
+
};
|
|
474
|
+
ContextAPI2.prototype._getContextManager = function() {
|
|
475
|
+
return getGlobal(API_NAME2) || NOOP_CONTEXT_MANAGER;
|
|
476
|
+
};
|
|
477
|
+
ContextAPI2.prototype.disable = function() {
|
|
478
|
+
this._getContextManager().disable();
|
|
479
|
+
unregisterGlobal(API_NAME2, DiagAPI.instance());
|
|
480
|
+
};
|
|
481
|
+
return ContextAPI2;
|
|
482
|
+
}()
|
|
483
|
+
);
|
|
484
|
+
|
|
485
|
+
// node_modules/@opentelemetry/api/build/esm/trace/status.js
|
|
486
|
+
var SpanStatusCode;
|
|
487
|
+
(function(SpanStatusCode2) {
|
|
488
|
+
SpanStatusCode2[SpanStatusCode2["UNSET"] = 0] = "UNSET";
|
|
489
|
+
SpanStatusCode2[SpanStatusCode2["OK"] = 1] = "OK";
|
|
490
|
+
SpanStatusCode2[SpanStatusCode2["ERROR"] = 2] = "ERROR";
|
|
491
|
+
})(SpanStatusCode || (SpanStatusCode = {}));
|
|
492
|
+
|
|
493
|
+
// node_modules/@opentelemetry/api/build/esm/context-api.js
|
|
494
|
+
var context = ContextAPI.getInstance();
|
|
495
|
+
|
|
496
|
+
// src/index.ts
|
|
497
|
+
import {
|
|
498
|
+
generateObject,
|
|
499
|
+
generateText,
|
|
500
|
+
JSONParseError
|
|
501
|
+
} from "ai";
|
|
8
502
|
import { encodingForModel } from "js-tiktoken";
|
|
503
|
+
import { fetch, FormData } from "undici";
|
|
504
|
+
function getTracer(runtime) {
|
|
505
|
+
const availableServices = Array.from(runtime.getAllServices().keys());
|
|
506
|
+
logger.debug(`[getTracer] Available services: ${JSON.stringify(availableServices)}`);
|
|
507
|
+
logger.debug(`[getTracer] Attempting to get service with key: ${ServiceType.INSTRUMENTATION}`);
|
|
508
|
+
const instrumentationService = runtime.getService(
|
|
509
|
+
ServiceType.INSTRUMENTATION
|
|
510
|
+
);
|
|
511
|
+
if (!instrumentationService) {
|
|
512
|
+
logger.warn(`[getTracer] Service ${ServiceType.INSTRUMENTATION} not found in runtime.`);
|
|
513
|
+
return null;
|
|
514
|
+
}
|
|
515
|
+
if (!instrumentationService.isEnabled()) {
|
|
516
|
+
logger.debug("[getTracer] Instrumentation service found but is disabled.");
|
|
517
|
+
return null;
|
|
518
|
+
}
|
|
519
|
+
logger.debug("[getTracer] Successfully retrieved enabled instrumentation service.");
|
|
520
|
+
return instrumentationService.getTracer("eliza.llm.openai");
|
|
521
|
+
}
|
|
522
|
+
async function startLlmSpan(runtime, spanName, attributes, fn) {
|
|
523
|
+
const tracer = getTracer(runtime);
|
|
524
|
+
if (!tracer) {
|
|
525
|
+
const dummySpan = {
|
|
526
|
+
setAttribute: () => {
|
|
527
|
+
},
|
|
528
|
+
setAttributes: () => {
|
|
529
|
+
},
|
|
530
|
+
addEvent: () => {
|
|
531
|
+
},
|
|
532
|
+
recordException: () => {
|
|
533
|
+
},
|
|
534
|
+
setStatus: () => {
|
|
535
|
+
},
|
|
536
|
+
end: () => {
|
|
537
|
+
},
|
|
538
|
+
spanContext: () => ({ traceId: "", spanId: "", traceFlags: 0 })
|
|
539
|
+
};
|
|
540
|
+
return fn(dummySpan);
|
|
541
|
+
}
|
|
542
|
+
const activeContext = context.active();
|
|
543
|
+
return tracer.startActiveSpan(spanName, { attributes }, activeContext, async (span) => {
|
|
544
|
+
try {
|
|
545
|
+
const result = await fn(span);
|
|
546
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
547
|
+
span.end();
|
|
548
|
+
return result;
|
|
549
|
+
} catch (error) {
|
|
550
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
551
|
+
span.recordException(error);
|
|
552
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message });
|
|
553
|
+
span.end();
|
|
554
|
+
throw error;
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
function getSetting(runtime, key, defaultValue) {
|
|
559
|
+
return runtime.getSetting(key) ?? process.env[key] ?? defaultValue;
|
|
560
|
+
}
|
|
561
|
+
function getBaseURL(runtime) {
|
|
562
|
+
const baseURL = getSetting(runtime, "OPENAI_BASE_URL", "https://api.openai.com/v1");
|
|
563
|
+
logger.debug(`[OpenAI] Default base URL: ${baseURL}`);
|
|
564
|
+
return baseURL;
|
|
565
|
+
}
|
|
566
|
+
function getEmbeddingBaseURL(runtime) {
|
|
567
|
+
const embeddingURL = getSetting(runtime, "OPENAI_EMBEDDING_URL");
|
|
568
|
+
if (embeddingURL) {
|
|
569
|
+
logger.debug(`[OpenAI] Using specific embedding base URL: ${embeddingURL}`);
|
|
570
|
+
return embeddingURL;
|
|
571
|
+
}
|
|
572
|
+
logger.debug("[OpenAI] Falling back to general base URL for embeddings.");
|
|
573
|
+
return getBaseURL(runtime);
|
|
574
|
+
}
|
|
575
|
+
function getApiKey(runtime) {
|
|
576
|
+
return getSetting(runtime, "OPENAI_API_KEY");
|
|
577
|
+
}
|
|
578
|
+
function getSmallModel(runtime) {
|
|
579
|
+
return getSetting(runtime, "OPENAI_SMALL_MODEL") ?? getSetting(runtime, "SMALL_MODEL", "gpt-4o-mini");
|
|
580
|
+
}
|
|
581
|
+
function getLargeModel(runtime) {
|
|
582
|
+
return getSetting(runtime, "OPENAI_LARGE_MODEL") ?? getSetting(runtime, "LARGE_MODEL", "gpt-4o");
|
|
583
|
+
}
|
|
584
|
+
function getImageDescriptionModel(runtime) {
|
|
585
|
+
return getSetting(runtime, "OPENAI_IMAGE_DESCRIPTION_MODEL", "gpt-4o-mini") ?? "gpt-4o-mini";
|
|
586
|
+
}
|
|
587
|
+
function createOpenAIClient(runtime) {
|
|
588
|
+
return createOpenAI({
|
|
589
|
+
apiKey: getApiKey(runtime),
|
|
590
|
+
baseURL: getBaseURL(runtime)
|
|
591
|
+
});
|
|
592
|
+
}
|
|
9
593
|
async function tokenizeText(model, prompt) {
|
|
10
594
|
const modelName = model === ModelType.TEXT_SMALL ? process.env.OPENAI_SMALL_MODEL ?? process.env.SMALL_MODEL ?? "gpt-4o-mini" : process.env.LARGE_MODEL ?? "gpt-4o";
|
|
11
595
|
const encoding = encodingForModel(modelName);
|
|
@@ -17,6 +601,171 @@ async function detokenizeText(model, tokens) {
|
|
|
17
601
|
const encoding = encodingForModel(modelName);
|
|
18
602
|
return encoding.decode(tokens);
|
|
19
603
|
}
|
|
604
|
+
async function generateObjectByModelType(runtime, params, modelType, getModelFn) {
|
|
605
|
+
const openai = createOpenAIClient(runtime);
|
|
606
|
+
const modelName = getModelFn(runtime);
|
|
607
|
+
logger.log(`[OpenAI] Using ${modelType} model: ${modelName}`);
|
|
608
|
+
const temperature = params.temperature ?? 0;
|
|
609
|
+
const schemaPresent = !!params.schema;
|
|
610
|
+
const attributes = {
|
|
611
|
+
"llm.vendor": "OpenAI",
|
|
612
|
+
"llm.request.type": "object_generation",
|
|
613
|
+
"llm.request.model": modelName,
|
|
614
|
+
"llm.request.temperature": temperature,
|
|
615
|
+
"llm.request.schema_present": schemaPresent
|
|
616
|
+
};
|
|
617
|
+
return startLlmSpan(runtime, "LLM.generateObject", attributes, async (span) => {
|
|
618
|
+
span.addEvent("llm.prompt", { "prompt.content": params.prompt });
|
|
619
|
+
if (schemaPresent) {
|
|
620
|
+
span.addEvent("llm.request.schema", {
|
|
621
|
+
schema: JSON.stringify(params.schema, safeReplacer())
|
|
622
|
+
});
|
|
623
|
+
logger.info(
|
|
624
|
+
`Using ${modelType} without schema validation (schema provided but output=no-schema)`
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
try {
|
|
628
|
+
const { object, usage } = await generateObject({
|
|
629
|
+
model: openai.languageModel(modelName),
|
|
630
|
+
output: "no-schema",
|
|
631
|
+
prompt: params.prompt,
|
|
632
|
+
temperature,
|
|
633
|
+
experimental_repairText: getJsonRepairFunction()
|
|
634
|
+
});
|
|
635
|
+
span.addEvent("llm.response.processed", {
|
|
636
|
+
"response.object": JSON.stringify(object, safeReplacer())
|
|
637
|
+
});
|
|
638
|
+
if (usage) {
|
|
639
|
+
span.setAttributes({
|
|
640
|
+
"llm.usage.prompt_tokens": usage.promptTokens,
|
|
641
|
+
"llm.usage.completion_tokens": usage.completionTokens,
|
|
642
|
+
"llm.usage.total_tokens": usage.totalTokens
|
|
643
|
+
});
|
|
644
|
+
emitModelUsageEvent(runtime, modelType, params.prompt, usage);
|
|
645
|
+
}
|
|
646
|
+
return object;
|
|
647
|
+
} catch (error) {
|
|
648
|
+
if (error instanceof JSONParseError) {
|
|
649
|
+
logger.error(`[generateObject] Failed to parse JSON: ${error.message}`);
|
|
650
|
+
span.recordException(error);
|
|
651
|
+
span.addEvent("llm.error.json_parse", {
|
|
652
|
+
"error.message": error.message,
|
|
653
|
+
"error.text": error.text
|
|
654
|
+
});
|
|
655
|
+
span.addEvent("llm.repair.attempt");
|
|
656
|
+
const repairFunction = getJsonRepairFunction();
|
|
657
|
+
const repairedJsonString = await repairFunction({
|
|
658
|
+
text: error.text,
|
|
659
|
+
error
|
|
660
|
+
});
|
|
661
|
+
if (repairedJsonString) {
|
|
662
|
+
try {
|
|
663
|
+
const repairedObject = JSON.parse(repairedJsonString);
|
|
664
|
+
span.addEvent("llm.repair.success", {
|
|
665
|
+
repaired_object: JSON.stringify(repairedObject, safeReplacer())
|
|
666
|
+
});
|
|
667
|
+
logger.info("[generateObject] Successfully repaired JSON.");
|
|
668
|
+
span.setStatus({
|
|
669
|
+
code: SpanStatusCode.ERROR,
|
|
670
|
+
message: "JSON parsing failed but was repaired"
|
|
671
|
+
});
|
|
672
|
+
return repairedObject;
|
|
673
|
+
} catch (repairParseError) {
|
|
674
|
+
const message = repairParseError instanceof Error ? repairParseError.message : String(repairParseError);
|
|
675
|
+
logger.error(`[generateObject] Failed to parse repaired JSON: ${message}`);
|
|
676
|
+
const exception = repairParseError instanceof Error ? repairParseError : new Error(message);
|
|
677
|
+
span.recordException(exception);
|
|
678
|
+
span.addEvent("llm.repair.parse_error", {
|
|
679
|
+
"error.message": message
|
|
680
|
+
});
|
|
681
|
+
span.setStatus({
|
|
682
|
+
code: SpanStatusCode.ERROR,
|
|
683
|
+
message: `JSON repair failed: ${message}`
|
|
684
|
+
});
|
|
685
|
+
throw repairParseError;
|
|
686
|
+
}
|
|
687
|
+
} else {
|
|
688
|
+
const errMsg = error instanceof Error ? error.message : String(error);
|
|
689
|
+
logger.error("[generateObject] JSON repair failed.");
|
|
690
|
+
span.addEvent("llm.repair.failed");
|
|
691
|
+
span.setStatus({
|
|
692
|
+
code: SpanStatusCode.ERROR,
|
|
693
|
+
message: `JSON repair failed: ${errMsg}`
|
|
694
|
+
});
|
|
695
|
+
throw error;
|
|
696
|
+
}
|
|
697
|
+
} else {
|
|
698
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
699
|
+
logger.error(`[generateObject] Unknown error: ${message}`);
|
|
700
|
+
const exception = error instanceof Error ? error : new Error(message);
|
|
701
|
+
span.recordException(exception);
|
|
702
|
+
span.setStatus({
|
|
703
|
+
code: SpanStatusCode.ERROR,
|
|
704
|
+
message
|
|
705
|
+
});
|
|
706
|
+
throw error;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
function getJsonRepairFunction() {
|
|
712
|
+
return async ({ text, error }) => {
|
|
713
|
+
try {
|
|
714
|
+
if (error instanceof JSONParseError) {
|
|
715
|
+
const cleanedText = text.replace(/```json\n|\n```|```/g, "");
|
|
716
|
+
JSON.parse(cleanedText);
|
|
717
|
+
return cleanedText;
|
|
718
|
+
}
|
|
719
|
+
return null;
|
|
720
|
+
} catch (jsonError) {
|
|
721
|
+
const message = jsonError instanceof Error ? jsonError.message : String(jsonError);
|
|
722
|
+
logger.warn(`Failed to repair JSON text: ${message}`);
|
|
723
|
+
return null;
|
|
724
|
+
}
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
function emitModelUsageEvent(runtime, type, prompt, usage) {
|
|
728
|
+
runtime.emitEvent(EventType.MODEL_USED, {
|
|
729
|
+
provider: "openai",
|
|
730
|
+
type,
|
|
731
|
+
prompt,
|
|
732
|
+
tokens: {
|
|
733
|
+
prompt: usage.promptTokens,
|
|
734
|
+
completion: usage.completionTokens,
|
|
735
|
+
total: usage.totalTokens
|
|
736
|
+
}
|
|
737
|
+
});
|
|
738
|
+
}
|
|
739
|
+
async function fetchTextToSpeech(runtime, text) {
|
|
740
|
+
const apiKey = getApiKey(runtime);
|
|
741
|
+
const model = getSetting(runtime, "OPENAI_TTS_MODEL", "gpt-4o-mini-tts");
|
|
742
|
+
const voice = getSetting(runtime, "OPENAI_TTS_VOICE", "nova");
|
|
743
|
+
const instructions = getSetting(runtime, "OPENAI_TTS_INSTRUCTIONS", "");
|
|
744
|
+
const baseURL = getBaseURL(runtime);
|
|
745
|
+
try {
|
|
746
|
+
const res = await fetch(`${baseURL}/audio/speech`, {
|
|
747
|
+
method: "POST",
|
|
748
|
+
headers: {
|
|
749
|
+
Authorization: `Bearer ${apiKey}`,
|
|
750
|
+
"Content-Type": "application/json"
|
|
751
|
+
},
|
|
752
|
+
body: JSON.stringify({
|
|
753
|
+
model,
|
|
754
|
+
voice,
|
|
755
|
+
input: text,
|
|
756
|
+
...instructions && { instructions }
|
|
757
|
+
})
|
|
758
|
+
});
|
|
759
|
+
if (!res.ok) {
|
|
760
|
+
const err = await res.text();
|
|
761
|
+
throw new Error(`OpenAI TTS error ${res.status}: ${err}`);
|
|
762
|
+
}
|
|
763
|
+
return res.body;
|
|
764
|
+
} catch (err) {
|
|
765
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
766
|
+
throw new Error(`Failed to fetch speech from OpenAI TTS: ${message}`);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
20
769
|
var openaiPlugin = {
|
|
21
770
|
name: "openai",
|
|
22
771
|
description: "OpenAI plugin",
|
|
@@ -26,41 +775,66 @@ var openaiPlugin = {
|
|
|
26
775
|
OPENAI_SMALL_MODEL: process.env.OPENAI_SMALL_MODEL,
|
|
27
776
|
OPENAI_LARGE_MODEL: process.env.OPENAI_LARGE_MODEL,
|
|
28
777
|
SMALL_MODEL: process.env.SMALL_MODEL,
|
|
29
|
-
LARGE_MODEL: process.env.LARGE_MODEL
|
|
778
|
+
LARGE_MODEL: process.env.LARGE_MODEL,
|
|
779
|
+
OPENAI_EMBEDDING_MODEL: process.env.OPENAI_EMBEDDING_MODEL,
|
|
780
|
+
OPENAI_EMBEDDING_URL: process.env.OPENAI_EMBEDDING_URL,
|
|
781
|
+
OPENAI_EMBEDDING_DIMENSIONS: process.env.OPENAI_EMBEDDING_DIMENSIONS,
|
|
782
|
+
OPENAI_IMAGE_DESCRIPTION_MODEL: process.env.OPENAI_IMAGE_DESCRIPTION_MODEL,
|
|
783
|
+
OPENAI_IMAGE_DESCRIPTION_MAX_TOKENS: process.env.OPENAI_IMAGE_DESCRIPTION_MAX_TOKENS
|
|
30
784
|
},
|
|
31
|
-
async init(
|
|
785
|
+
async init(_config, runtime) {
|
|
32
786
|
try {
|
|
33
|
-
if (!
|
|
787
|
+
if (!getApiKey(runtime)) {
|
|
34
788
|
logger.warn(
|
|
35
789
|
"OPENAI_API_KEY is not set in environment - OpenAI functionality will be limited"
|
|
36
790
|
);
|
|
37
791
|
return;
|
|
38
792
|
}
|
|
39
793
|
try {
|
|
40
|
-
const baseURL =
|
|
794
|
+
const baseURL = getBaseURL(runtime);
|
|
41
795
|
const response = await fetch(`${baseURL}/models`, {
|
|
42
|
-
headers: { Authorization: `Bearer ${
|
|
796
|
+
headers: { Authorization: `Bearer ${getApiKey(runtime)}` }
|
|
43
797
|
});
|
|
44
798
|
if (!response.ok) {
|
|
45
799
|
logger.warn(`OpenAI API key validation failed: ${response.statusText}`);
|
|
46
800
|
logger.warn("OpenAI functionality will be limited until a valid API key is provided");
|
|
47
801
|
} else {
|
|
802
|
+
logger.log("OpenAI API key validated successfully");
|
|
48
803
|
}
|
|
49
804
|
} catch (fetchError) {
|
|
50
|
-
|
|
805
|
+
const message = fetchError instanceof Error ? fetchError.message : String(fetchError);
|
|
806
|
+
logger.warn(`Error validating OpenAI API key: ${message}`);
|
|
51
807
|
logger.warn("OpenAI functionality will be limited until a valid API key is provided");
|
|
52
808
|
}
|
|
53
809
|
} catch (error) {
|
|
810
|
+
const message = error?.errors?.map((e) => e.message).join(", ") || (error instanceof Error ? error.message : String(error));
|
|
54
811
|
logger.warn(
|
|
55
|
-
`OpenAI plugin configuration issue: ${
|
|
812
|
+
`OpenAI plugin configuration issue: ${message} - You need to configure the OPENAI_API_KEY in your environment variables`
|
|
56
813
|
);
|
|
57
814
|
}
|
|
58
815
|
},
|
|
59
816
|
models: {
|
|
60
|
-
[ModelType.TEXT_EMBEDDING]: async (
|
|
817
|
+
[ModelType.TEXT_EMBEDDING]: async (runtime, params) => {
|
|
818
|
+
const embeddingModelName = getSetting(
|
|
819
|
+
runtime,
|
|
820
|
+
"OPENAI_EMBEDDING_MODEL",
|
|
821
|
+
"text-embedding-3-small"
|
|
822
|
+
);
|
|
823
|
+
const embeddingDimension = Number.parseInt(
|
|
824
|
+
getSetting(runtime, "OPENAI_EMBEDDING_DIMENSIONS", "1536") || "1536",
|
|
825
|
+
10
|
|
826
|
+
);
|
|
827
|
+
logger.debug(
|
|
828
|
+
`[OpenAI] Using embedding model: ${embeddingModelName} with dimension: ${embeddingDimension}`
|
|
829
|
+
);
|
|
830
|
+
if (!Object.values(VECTOR_DIMS).includes(embeddingDimension)) {
|
|
831
|
+
const errorMsg = `Invalid embedding dimension: ${embeddingDimension}. Must be one of: ${Object.values(VECTOR_DIMS).join(", ")}`;
|
|
832
|
+
logger.error(errorMsg);
|
|
833
|
+
throw new Error(errorMsg);
|
|
834
|
+
}
|
|
61
835
|
if (params === null) {
|
|
62
836
|
logger.debug("Creating test embedding for initialization");
|
|
63
|
-
const testVector = Array(
|
|
837
|
+
const testVector = Array(embeddingDimension).fill(0);
|
|
64
838
|
testVector[0] = 0.1;
|
|
65
839
|
return testVector;
|
|
66
840
|
}
|
|
@@ -71,51 +845,100 @@ var openaiPlugin = {
|
|
|
71
845
|
text = params.text;
|
|
72
846
|
} else {
|
|
73
847
|
logger.warn("Invalid input format for embedding");
|
|
74
|
-
const fallbackVector = Array(
|
|
848
|
+
const fallbackVector = Array(embeddingDimension).fill(0);
|
|
75
849
|
fallbackVector[0] = 0.2;
|
|
76
850
|
return fallbackVector;
|
|
77
851
|
}
|
|
78
852
|
if (!text.trim()) {
|
|
79
853
|
logger.warn("Empty text for embedding");
|
|
80
|
-
const emptyVector = Array(
|
|
854
|
+
const emptyVector = Array(embeddingDimension).fill(0);
|
|
81
855
|
emptyVector[0] = 0.3;
|
|
82
856
|
return emptyVector;
|
|
83
857
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
return errorVector;
|
|
858
|
+
const attributes = {
|
|
859
|
+
"llm.vendor": "OpenAI",
|
|
860
|
+
"llm.request.type": "embedding",
|
|
861
|
+
"llm.request.model": embeddingModelName,
|
|
862
|
+
"llm.request.embedding.dimensions": embeddingDimension,
|
|
863
|
+
"input.text.length": text.length
|
|
864
|
+
};
|
|
865
|
+
return startLlmSpan(runtime, "LLM.embedding", attributes, async (span) => {
|
|
866
|
+
span.addEvent("llm.prompt", { "prompt.content": text });
|
|
867
|
+
const embeddingBaseURL = getEmbeddingBaseURL(runtime);
|
|
868
|
+
const apiKey = getApiKey(runtime);
|
|
869
|
+
if (!apiKey) {
|
|
870
|
+
span.setStatus({
|
|
871
|
+
code: SpanStatusCode.ERROR,
|
|
872
|
+
message: "OpenAI API key not configured"
|
|
873
|
+
});
|
|
874
|
+
throw new Error("OpenAI API key not configured");
|
|
102
875
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
876
|
+
try {
|
|
877
|
+
const response = await fetch(`${embeddingBaseURL}/embeddings`, {
|
|
878
|
+
method: "POST",
|
|
879
|
+
headers: {
|
|
880
|
+
Authorization: `Bearer ${apiKey}`,
|
|
881
|
+
"Content-Type": "application/json"
|
|
882
|
+
},
|
|
883
|
+
body: JSON.stringify({
|
|
884
|
+
model: embeddingModelName,
|
|
885
|
+
input: text
|
|
886
|
+
})
|
|
887
|
+
});
|
|
888
|
+
const responseClone = response.clone();
|
|
889
|
+
const rawResponseBody = await responseClone.text();
|
|
890
|
+
span.addEvent("llm.response.raw", {
|
|
891
|
+
"response.body": rawResponseBody
|
|
892
|
+
});
|
|
893
|
+
if (!response.ok) {
|
|
894
|
+
logger.error(`OpenAI API error: ${response.status} - ${response.statusText}`);
|
|
895
|
+
span.setAttributes({ "error.api.status": response.status });
|
|
896
|
+
span.setStatus({
|
|
897
|
+
code: SpanStatusCode.ERROR,
|
|
898
|
+
message: `OpenAI API error: ${response.status} - ${response.statusText}. Response: ${rawResponseBody}`
|
|
899
|
+
});
|
|
900
|
+
const errorVector = Array(embeddingDimension).fill(0);
|
|
901
|
+
errorVector[0] = 0.4;
|
|
902
|
+
return errorVector;
|
|
903
|
+
}
|
|
904
|
+
const data = await response.json();
|
|
905
|
+
if (!data?.data?.[0]?.embedding) {
|
|
906
|
+
logger.error("API returned invalid structure");
|
|
907
|
+
span.setStatus({
|
|
908
|
+
code: SpanStatusCode.ERROR,
|
|
909
|
+
message: "API returned invalid structure"
|
|
910
|
+
});
|
|
911
|
+
const errorVector = Array(embeddingDimension).fill(0);
|
|
912
|
+
errorVector[0] = 0.5;
|
|
913
|
+
return errorVector;
|
|
914
|
+
}
|
|
915
|
+
const embedding = data.data[0].embedding;
|
|
916
|
+
span.setAttribute("llm.response.embedding.vector_length", embedding.length);
|
|
917
|
+
if (data.usage) {
|
|
918
|
+
span.setAttributes({
|
|
919
|
+
"llm.usage.prompt_tokens": data.usage.prompt_tokens,
|
|
920
|
+
"llm.usage.total_tokens": data.usage.total_tokens
|
|
921
|
+
});
|
|
922
|
+
const usage = {
|
|
923
|
+
promptTokens: data.usage.prompt_tokens,
|
|
924
|
+
completionTokens: 0,
|
|
925
|
+
totalTokens: data.usage.total_tokens
|
|
926
|
+
};
|
|
927
|
+
emitModelUsageEvent(runtime, ModelType.TEXT_EMBEDDING, text, usage);
|
|
928
|
+
}
|
|
929
|
+
logger.log(`Got valid embedding with length ${embedding.length}`);
|
|
930
|
+
return embedding;
|
|
931
|
+
} catch (error) {
|
|
932
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
933
|
+
logger.error(`Error generating embedding: ${message}`);
|
|
934
|
+
const exception = error instanceof Error ? error : new Error(message);
|
|
935
|
+
span.recordException(exception);
|
|
936
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message });
|
|
937
|
+
const errorVector = Array(embeddingDimension).fill(0);
|
|
938
|
+
errorVector[0] = 0.6;
|
|
108
939
|
return errorVector;
|
|
109
940
|
}
|
|
110
|
-
|
|
111
|
-
logger.log(`Got valid embedding with length ${embedding.length}`);
|
|
112
|
-
return embedding;
|
|
113
|
-
} catch (error) {
|
|
114
|
-
logger.error("Error generating embedding:", error);
|
|
115
|
-
const errorVector = Array(1536).fill(0);
|
|
116
|
-
errorVector[0] = 0.6;
|
|
117
|
-
return errorVector;
|
|
118
|
-
}
|
|
941
|
+
});
|
|
119
942
|
},
|
|
120
943
|
[ModelType.TEXT_TOKENIZER_ENCODE]: async (_runtime, { prompt, modelType = ModelType.TEXT_LARGE }) => {
|
|
121
944
|
return await tokenizeText(modelType ?? ModelType.TEXT_LARGE, prompt);
|
|
@@ -128,25 +951,46 @@ var openaiPlugin = {
|
|
|
128
951
|
const frequency_penalty = 0.7;
|
|
129
952
|
const presence_penalty = 0.7;
|
|
130
953
|
const max_response_length = 8192;
|
|
131
|
-
const
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
baseURL
|
|
135
|
-
});
|
|
136
|
-
const model = runtime.getSetting("OPENAI_SMALL_MODEL") ?? runtime.getSetting("SMALL_MODEL") ?? "gpt-4o-mini";
|
|
137
|
-
logger.log("generating text");
|
|
954
|
+
const openai = createOpenAIClient(runtime);
|
|
955
|
+
const modelName = getSmallModel(runtime);
|
|
956
|
+
logger.log(`[OpenAI] Using TEXT_SMALL model: ${modelName}`);
|
|
138
957
|
logger.log(prompt);
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
temperature,
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
stopSequences
|
|
958
|
+
const attributes = {
|
|
959
|
+
"llm.vendor": "OpenAI",
|
|
960
|
+
"llm.request.type": "completion",
|
|
961
|
+
"llm.request.model": modelName,
|
|
962
|
+
"llm.request.temperature": temperature,
|
|
963
|
+
"llm.request.max_tokens": max_response_length,
|
|
964
|
+
"llm.request.frequency_penalty": frequency_penalty,
|
|
965
|
+
"llm.request.presence_penalty": presence_penalty,
|
|
966
|
+
"llm.request.stop_sequences": JSON.stringify(stopSequences)
|
|
967
|
+
};
|
|
968
|
+
return startLlmSpan(runtime, "LLM.generateText", attributes, async (span) => {
|
|
969
|
+
span.addEvent("llm.prompt", { "prompt.content": prompt });
|
|
970
|
+
const { text: openaiResponse, usage } = await generateText({
|
|
971
|
+
model: openai.languageModel(modelName),
|
|
972
|
+
prompt,
|
|
973
|
+
system: runtime.character.system ?? void 0,
|
|
974
|
+
temperature,
|
|
975
|
+
maxTokens: max_response_length,
|
|
976
|
+
frequencyPenalty: frequency_penalty,
|
|
977
|
+
presencePenalty: presence_penalty,
|
|
978
|
+
stopSequences
|
|
979
|
+
});
|
|
980
|
+
span.setAttribute("llm.response.processed.length", openaiResponse.length);
|
|
981
|
+
span.addEvent("llm.response.processed", {
|
|
982
|
+
"response.content": openaiResponse.substring(0, 200) + (openaiResponse.length > 200 ? "..." : "")
|
|
983
|
+
});
|
|
984
|
+
if (usage) {
|
|
985
|
+
span.setAttributes({
|
|
986
|
+
"llm.usage.prompt_tokens": usage.promptTokens,
|
|
987
|
+
"llm.usage.completion_tokens": usage.completionTokens,
|
|
988
|
+
"llm.usage.total_tokens": usage.totalTokens
|
|
989
|
+
});
|
|
990
|
+
emitModelUsageEvent(runtime, ModelType.TEXT_SMALL, prompt, usage);
|
|
991
|
+
}
|
|
992
|
+
return openaiResponse;
|
|
148
993
|
});
|
|
149
|
-
return openaiResponse;
|
|
150
994
|
},
|
|
151
995
|
[ModelType.TEXT_LARGE]: async (runtime, {
|
|
152
996
|
prompt,
|
|
@@ -156,194 +1000,352 @@ var openaiPlugin = {
|
|
|
156
1000
|
frequencyPenalty = 0.7,
|
|
157
1001
|
presencePenalty = 0.7
|
|
158
1002
|
}) => {
|
|
159
|
-
const
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
model:
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
1003
|
+
const openai = createOpenAIClient(runtime);
|
|
1004
|
+
const modelName = getLargeModel(runtime);
|
|
1005
|
+
logger.log(`[OpenAI] Using TEXT_LARGE model: ${modelName}`);
|
|
1006
|
+
logger.log(prompt);
|
|
1007
|
+
const attributes = {
|
|
1008
|
+
"llm.vendor": "OpenAI",
|
|
1009
|
+
"llm.request.type": "completion",
|
|
1010
|
+
"llm.request.model": modelName,
|
|
1011
|
+
"llm.request.temperature": temperature,
|
|
1012
|
+
"llm.request.max_tokens": maxTokens,
|
|
1013
|
+
"llm.request.frequency_penalty": frequencyPenalty,
|
|
1014
|
+
"llm.request.presence_penalty": presencePenalty,
|
|
1015
|
+
"llm.request.stop_sequences": JSON.stringify(stopSequences)
|
|
1016
|
+
};
|
|
1017
|
+
return startLlmSpan(runtime, "LLM.generateText", attributes, async (span) => {
|
|
1018
|
+
span.addEvent("llm.prompt", { "prompt.content": prompt });
|
|
1019
|
+
const { text: openaiResponse, usage } = await generateText({
|
|
1020
|
+
model: openai.languageModel(modelName),
|
|
1021
|
+
prompt,
|
|
1022
|
+
system: runtime.character.system ?? void 0,
|
|
1023
|
+
temperature,
|
|
1024
|
+
maxTokens,
|
|
1025
|
+
frequencyPenalty,
|
|
1026
|
+
presencePenalty,
|
|
1027
|
+
stopSequences
|
|
1028
|
+
});
|
|
1029
|
+
span.setAttribute("llm.response.processed.length", openaiResponse.length);
|
|
1030
|
+
span.addEvent("llm.response.processed", {
|
|
1031
|
+
"response.content": openaiResponse.substring(0, 200) + (openaiResponse.length > 200 ? "..." : "")
|
|
1032
|
+
});
|
|
1033
|
+
if (usage) {
|
|
1034
|
+
span.setAttributes({
|
|
1035
|
+
"llm.usage.prompt_tokens": usage.promptTokens,
|
|
1036
|
+
"llm.usage.completion_tokens": usage.completionTokens,
|
|
1037
|
+
"llm.usage.total_tokens": usage.totalTokens
|
|
1038
|
+
});
|
|
1039
|
+
emitModelUsageEvent(runtime, ModelType.TEXT_LARGE, prompt, usage);
|
|
1040
|
+
}
|
|
1041
|
+
return openaiResponse;
|
|
174
1042
|
});
|
|
175
|
-
return openaiResponse;
|
|
176
1043
|
},
|
|
177
1044
|
[ModelType.IMAGE]: async (runtime, params) => {
|
|
178
|
-
const
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
1045
|
+
const n = params.n || 1;
|
|
1046
|
+
const size = params.size || "1024x1024";
|
|
1047
|
+
const prompt = params.prompt;
|
|
1048
|
+
const modelName = "dall-e-3";
|
|
1049
|
+
logger.log(`[OpenAI] Using IMAGE model: ${modelName}`);
|
|
1050
|
+
const attributes = {
|
|
1051
|
+
"llm.vendor": "OpenAI",
|
|
1052
|
+
"llm.request.type": "image_generation",
|
|
1053
|
+
"llm.request.image.size": size,
|
|
1054
|
+
"llm.request.image.count": n
|
|
1055
|
+
};
|
|
1056
|
+
return startLlmSpan(runtime, "LLM.imageGeneration", attributes, async (span) => {
|
|
1057
|
+
span.addEvent("llm.prompt", { "prompt.content": prompt });
|
|
1058
|
+
const baseURL = getBaseURL(runtime);
|
|
1059
|
+
const apiKey = getApiKey(runtime);
|
|
1060
|
+
if (!apiKey) {
|
|
1061
|
+
span.setStatus({
|
|
1062
|
+
code: SpanStatusCode.ERROR,
|
|
1063
|
+
message: "OpenAI API key not configured"
|
|
1064
|
+
});
|
|
1065
|
+
throw new Error("OpenAI API key not configured");
|
|
1066
|
+
}
|
|
1067
|
+
try {
|
|
1068
|
+
const response = await fetch(`${baseURL}/images/generations`, {
|
|
1069
|
+
method: "POST",
|
|
1070
|
+
headers: {
|
|
1071
|
+
Authorization: `Bearer ${apiKey}`,
|
|
1072
|
+
"Content-Type": "application/json"
|
|
1073
|
+
},
|
|
1074
|
+
body: JSON.stringify({
|
|
1075
|
+
prompt,
|
|
1076
|
+
n,
|
|
1077
|
+
size
|
|
1078
|
+
})
|
|
1079
|
+
});
|
|
1080
|
+
const responseClone = response.clone();
|
|
1081
|
+
const rawResponseBody = await responseClone.text();
|
|
1082
|
+
span.addEvent("llm.response.raw", {
|
|
1083
|
+
"response.body": rawResponseBody
|
|
1084
|
+
});
|
|
1085
|
+
if (!response.ok) {
|
|
1086
|
+
span.setAttributes({ "error.api.status": response.status });
|
|
1087
|
+
span.setStatus({
|
|
1088
|
+
code: SpanStatusCode.ERROR,
|
|
1089
|
+
message: `Failed to generate image: ${response.statusText}. Response: ${rawResponseBody}`
|
|
1090
|
+
});
|
|
1091
|
+
throw new Error(`Failed to generate image: ${response.statusText}`);
|
|
1092
|
+
}
|
|
1093
|
+
const data = await response.json();
|
|
1094
|
+
const typedData = data;
|
|
1095
|
+
span.addEvent("llm.response.processed", {
|
|
1096
|
+
"response.urls": JSON.stringify(typedData.data)
|
|
1097
|
+
});
|
|
1098
|
+
return typedData.data;
|
|
1099
|
+
} catch (error) {
|
|
1100
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1101
|
+
const exception = error instanceof Error ? error : new Error(message);
|
|
1102
|
+
span.recordException(exception);
|
|
1103
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message });
|
|
1104
|
+
throw error;
|
|
1105
|
+
}
|
|
190
1106
|
});
|
|
191
|
-
if (!response.ok) {
|
|
192
|
-
throw new Error(`Failed to generate image: ${response.statusText}`);
|
|
193
|
-
}
|
|
194
|
-
const data = await response.json();
|
|
195
|
-
const typedData = data;
|
|
196
|
-
return typedData.data;
|
|
197
1107
|
},
|
|
198
1108
|
[ModelType.IMAGE_DESCRIPTION]: async (runtime, params) => {
|
|
199
1109
|
let imageUrl;
|
|
200
|
-
let
|
|
1110
|
+
let promptText;
|
|
1111
|
+
const modelName = getImageDescriptionModel(runtime);
|
|
1112
|
+
logger.log(`[OpenAI] Using IMAGE_DESCRIPTION model: ${modelName}`);
|
|
1113
|
+
const maxTokens = Number.parseInt(
|
|
1114
|
+
getSetting(runtime, "OPENAI_IMAGE_DESCRIPTION_MAX_TOKENS", "8192") || "8192",
|
|
1115
|
+
10
|
|
1116
|
+
);
|
|
201
1117
|
if (typeof params === "string") {
|
|
202
1118
|
imageUrl = params;
|
|
203
|
-
|
|
1119
|
+
promptText = "Please analyze this image and provide a title and detailed description.";
|
|
204
1120
|
} else {
|
|
205
1121
|
imageUrl = params.imageUrl;
|
|
206
|
-
|
|
1122
|
+
promptText = params.prompt || "Please analyze this image and provide a title and detailed description.";
|
|
207
1123
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
1124
|
+
const attributes = {
|
|
1125
|
+
"llm.vendor": "OpenAI",
|
|
1126
|
+
"llm.request.type": "chat",
|
|
1127
|
+
"llm.request.model": modelName,
|
|
1128
|
+
"llm.request.max_tokens": maxTokens,
|
|
1129
|
+
"llm.request.image.url": imageUrl
|
|
1130
|
+
};
|
|
1131
|
+
const messages = [
|
|
1132
|
+
{
|
|
1133
|
+
role: "user",
|
|
1134
|
+
content: [
|
|
1135
|
+
{ type: "text", text: promptText },
|
|
1136
|
+
{ type: "image_url", image_url: { url: imageUrl } }
|
|
1137
|
+
]
|
|
1138
|
+
}
|
|
1139
|
+
];
|
|
1140
|
+
return startLlmSpan(runtime, "LLM.imageDescription", attributes, async (span) => {
|
|
1141
|
+
span.addEvent("llm.prompt", {
|
|
1142
|
+
"prompt.content": JSON.stringify(messages, safeReplacer())
|
|
1143
|
+
});
|
|
1144
|
+
const baseURL = getBaseURL(runtime);
|
|
1145
|
+
const apiKey = getApiKey(runtime);
|
|
211
1146
|
if (!apiKey) {
|
|
212
1147
|
logger.error("OpenAI API key not set");
|
|
1148
|
+
span.setStatus({
|
|
1149
|
+
code: SpanStatusCode.ERROR,
|
|
1150
|
+
message: "OpenAI API key not configured"
|
|
1151
|
+
});
|
|
213
1152
|
return {
|
|
214
1153
|
title: "Failed to analyze image",
|
|
215
1154
|
description: "API key not configured"
|
|
216
1155
|
};
|
|
217
1156
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
1157
|
+
try {
|
|
1158
|
+
const requestBody = {
|
|
1159
|
+
model: modelName,
|
|
1160
|
+
messages,
|
|
1161
|
+
max_tokens: maxTokens
|
|
1162
|
+
};
|
|
1163
|
+
const response = await fetch(`${baseURL}/chat/completions`, {
|
|
1164
|
+
method: "POST",
|
|
1165
|
+
headers: {
|
|
1166
|
+
"Content-Type": "application/json",
|
|
1167
|
+
Authorization: `Bearer ${apiKey}`
|
|
1168
|
+
},
|
|
1169
|
+
body: JSON.stringify(requestBody)
|
|
1170
|
+
});
|
|
1171
|
+
const responseClone = response.clone();
|
|
1172
|
+
const rawResponseBody = await responseClone.text();
|
|
1173
|
+
span.addEvent("llm.response.raw", {
|
|
1174
|
+
"response.body": rawResponseBody
|
|
1175
|
+
});
|
|
1176
|
+
if (!response.ok) {
|
|
1177
|
+
span.setAttributes({ "error.api.status": response.status });
|
|
1178
|
+
span.setStatus({
|
|
1179
|
+
code: SpanStatusCode.ERROR,
|
|
1180
|
+
message: `OpenAI API error: ${response.status}. Response: ${rawResponseBody}`
|
|
1181
|
+
});
|
|
1182
|
+
throw new Error(`OpenAI API error: ${response.status}`);
|
|
1183
|
+
}
|
|
1184
|
+
const result = await response.json();
|
|
1185
|
+
const typedResult = result;
|
|
1186
|
+
const content = typedResult.choices?.[0]?.message?.content;
|
|
1187
|
+
console.log("############## CONTENT", content);
|
|
1188
|
+
if (typedResult.usage) {
|
|
1189
|
+
span.setAttributes({
|
|
1190
|
+
"llm.usage.prompt_tokens": typedResult.usage.prompt_tokens,
|
|
1191
|
+
"llm.usage.completion_tokens": typedResult.usage.completion_tokens,
|
|
1192
|
+
"llm.usage.total_tokens": typedResult.usage.total_tokens
|
|
1193
|
+
});
|
|
1194
|
+
emitModelUsageEvent(
|
|
1195
|
+
runtime,
|
|
1196
|
+
ModelType.IMAGE_DESCRIPTION,
|
|
1197
|
+
typeof params === "string" ? params : params.prompt || "",
|
|
227
1198
|
{
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
type: "text",
|
|
232
|
-
text: prompt || "Please analyze this image and provide a title and detailed description."
|
|
233
|
-
},
|
|
234
|
-
{
|
|
235
|
-
type: "image_url",
|
|
236
|
-
image_url: { url: imageUrl }
|
|
237
|
-
}
|
|
238
|
-
]
|
|
1199
|
+
promptTokens: typedResult.usage.prompt_tokens,
|
|
1200
|
+
completionTokens: typedResult.usage.completion_tokens,
|
|
1201
|
+
totalTokens: typedResult.usage.total_tokens
|
|
239
1202
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
1203
|
+
);
|
|
1204
|
+
}
|
|
1205
|
+
if (typedResult.choices?.[0]?.finish_reason) {
|
|
1206
|
+
span.setAttribute("llm.response.finish_reason", typedResult.choices[0].finish_reason);
|
|
1207
|
+
}
|
|
1208
|
+
if (!content) {
|
|
1209
|
+
span.setStatus({
|
|
1210
|
+
code: SpanStatusCode.ERROR,
|
|
1211
|
+
message: "No content in API response"
|
|
1212
|
+
});
|
|
1213
|
+
return {
|
|
1214
|
+
title: "Failed to analyze image",
|
|
1215
|
+
description: "No response from API"
|
|
1216
|
+
};
|
|
1217
|
+
}
|
|
1218
|
+
console.log("######################## CONTENT", content);
|
|
1219
|
+
const isCustomPrompt = typeof params === "object" && params.prompt && params.prompt !== "Please analyze this image and provide a title and detailed description.";
|
|
1220
|
+
if (isCustomPrompt) {
|
|
1221
|
+
span.addEvent("llm.response.raw_content", {
|
|
1222
|
+
"response.content": content
|
|
1223
|
+
});
|
|
1224
|
+
return content;
|
|
1225
|
+
}
|
|
1226
|
+
const titleMatch = content.match(/title[:\s]+(.+?)(?:\n|$)/i);
|
|
1227
|
+
const title = titleMatch?.[1]?.trim() || "Image Analysis";
|
|
1228
|
+
const description = content.replace(/title[:\s]+(.+?)(?:\n|$)/i, "").trim();
|
|
1229
|
+
const processedResult = { title, description };
|
|
1230
|
+
span.addEvent("llm.response.processed", {
|
|
1231
|
+
"response.object": JSON.stringify(processedResult, safeReplacer())
|
|
1232
|
+
});
|
|
1233
|
+
return processedResult;
|
|
1234
|
+
} catch (error) {
|
|
1235
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1236
|
+
logger.error(`Error analyzing image: ${message}`);
|
|
1237
|
+
const exception = error instanceof Error ? error : new Error(message);
|
|
1238
|
+
span.recordException(exception);
|
|
1239
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message });
|
|
250
1240
|
return {
|
|
251
1241
|
title: "Failed to analyze image",
|
|
252
|
-
description:
|
|
1242
|
+
description: `Error: ${message}`
|
|
253
1243
|
};
|
|
254
1244
|
}
|
|
255
|
-
|
|
256
|
-
const title = titleMatch?.[1] || "Image Analysis";
|
|
257
|
-
const description = content.replace(/title[:\s]+(.+?)(?:\n|$)/i, "").trim();
|
|
258
|
-
return { title, description };
|
|
259
|
-
} catch (error) {
|
|
260
|
-
logger.error("Error analyzing image:", error);
|
|
261
|
-
return {
|
|
262
|
-
title: "Failed to analyze image",
|
|
263
|
-
description: `Error: ${error instanceof Error ? error.message : String(error)}`
|
|
264
|
-
};
|
|
265
|
-
}
|
|
1245
|
+
});
|
|
266
1246
|
},
|
|
267
1247
|
[ModelType.TRANSCRIPTION]: async (runtime, audioBuffer) => {
|
|
268
1248
|
logger.log("audioBuffer", audioBuffer);
|
|
269
|
-
const
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
1249
|
+
const modelName = "whisper-1";
|
|
1250
|
+
logger.log(`[OpenAI] Using TRANSCRIPTION model: ${modelName}`);
|
|
1251
|
+
const attributes = {
|
|
1252
|
+
"llm.vendor": "OpenAI",
|
|
1253
|
+
"llm.request.type": "transcription",
|
|
1254
|
+
"llm.request.model": modelName,
|
|
1255
|
+
"llm.request.audio.input_size_bytes": audioBuffer?.length || 0
|
|
1256
|
+
};
|
|
1257
|
+
return startLlmSpan(runtime, "LLM.transcription", attributes, async (span) => {
|
|
1258
|
+
span.addEvent("llm.prompt", {
|
|
1259
|
+
"prompt.info": "Audio buffer for transcription"
|
|
1260
|
+
});
|
|
1261
|
+
const baseURL = getBaseURL(runtime);
|
|
1262
|
+
const apiKey = getApiKey(runtime);
|
|
1263
|
+
if (!apiKey) {
|
|
1264
|
+
span.setStatus({
|
|
1265
|
+
code: SpanStatusCode.ERROR,
|
|
1266
|
+
message: "OpenAI API key not configured"
|
|
1267
|
+
});
|
|
1268
|
+
throw new Error("OpenAI API key not configured - Cannot make request");
|
|
1269
|
+
}
|
|
1270
|
+
if (!audioBuffer || audioBuffer.length === 0) {
|
|
1271
|
+
span.setStatus({
|
|
1272
|
+
code: SpanStatusCode.ERROR,
|
|
1273
|
+
message: "Audio buffer is empty or invalid"
|
|
1274
|
+
});
|
|
1275
|
+
throw new Error("Audio buffer is empty or invalid for transcription");
|
|
1276
|
+
}
|
|
1277
|
+
const formData = new FormData();
|
|
1278
|
+
formData.append("file", new Blob([audioBuffer]), "recording.mp3");
|
|
1279
|
+
formData.append("model", "whisper-1");
|
|
1280
|
+
try {
|
|
1281
|
+
const response = await fetch(`${baseURL}/audio/transcriptions`, {
|
|
1282
|
+
method: "POST",
|
|
1283
|
+
headers: {
|
|
1284
|
+
Authorization: `Bearer ${apiKey}`
|
|
1285
|
+
},
|
|
1286
|
+
body: formData
|
|
1287
|
+
});
|
|
1288
|
+
const responseClone = response.clone();
|
|
1289
|
+
const rawResponseBody = await responseClone.text();
|
|
1290
|
+
span.addEvent("llm.response.raw", {
|
|
1291
|
+
"response.body": rawResponseBody
|
|
1292
|
+
});
|
|
1293
|
+
logger.log("response", response);
|
|
1294
|
+
if (!response.ok) {
|
|
1295
|
+
span.setAttributes({ "error.api.status": response.status });
|
|
1296
|
+
span.setStatus({
|
|
1297
|
+
code: SpanStatusCode.ERROR,
|
|
1298
|
+
message: `Failed to transcribe audio: ${response.statusText}. Response: ${rawResponseBody}`
|
|
1299
|
+
});
|
|
1300
|
+
throw new Error(`Failed to transcribe audio: ${response.statusText}`);
|
|
1301
|
+
}
|
|
1302
|
+
const data = await response.json();
|
|
1303
|
+
const processedText = data.text;
|
|
1304
|
+
span.setAttribute("llm.response.processed.length", processedText.length);
|
|
1305
|
+
span.addEvent("llm.response.processed", {
|
|
1306
|
+
"response.text": processedText
|
|
1307
|
+
});
|
|
1308
|
+
return processedText;
|
|
1309
|
+
} catch (error) {
|
|
1310
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1311
|
+
const exception = error instanceof Error ? error : new Error(message);
|
|
1312
|
+
span.recordException(exception);
|
|
1313
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message });
|
|
1314
|
+
throw error;
|
|
1315
|
+
}
|
|
280
1316
|
});
|
|
281
|
-
logger.log("response", response);
|
|
282
|
-
if (!response.ok) {
|
|
283
|
-
throw new Error(`Failed to transcribe audio: ${response.statusText}`);
|
|
284
|
-
}
|
|
285
|
-
const data = await response.json();
|
|
286
|
-
return data.text;
|
|
287
1317
|
},
|
|
288
|
-
[ModelType.
|
|
289
|
-
const
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
1318
|
+
[ModelType.TEXT_TO_SPEECH]: async (runtime, text) => {
|
|
1319
|
+
const ttsModelName = getSetting(runtime, "OPENAI_TTS_MODEL", "gpt-4o-mini-tts");
|
|
1320
|
+
const attributes = {
|
|
1321
|
+
"llm.vendor": "OpenAI",
|
|
1322
|
+
"llm.request.type": "tts",
|
|
1323
|
+
"llm.request.model": ttsModelName,
|
|
1324
|
+
"input.text.length": text.length
|
|
1325
|
+
};
|
|
1326
|
+
return startLlmSpan(runtime, "LLM.tts", attributes, async (span) => {
|
|
1327
|
+
logger.log(`[OpenAI] Using TEXT_TO_SPEECH model: ${ttsModelName}`);
|
|
1328
|
+
span.addEvent("llm.prompt", { "prompt.content": text });
|
|
1329
|
+
try {
|
|
1330
|
+
const speechStream = await fetchTextToSpeech(runtime, text);
|
|
1331
|
+
span.addEvent("llm.response.success", {
|
|
1332
|
+
info: "Speech stream generated"
|
|
303
1333
|
});
|
|
304
|
-
return
|
|
1334
|
+
return speechStream;
|
|
1335
|
+
} catch (error) {
|
|
1336
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1337
|
+
const exception = error instanceof Error ? error : new Error(message);
|
|
1338
|
+
span.recordException(exception);
|
|
1339
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message });
|
|
1340
|
+
throw error;
|
|
305
1341
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
temperature: params.temperature
|
|
311
|
-
});
|
|
312
|
-
return object;
|
|
313
|
-
} catch (error) {
|
|
314
|
-
logger.error("Error generating object:", error);
|
|
315
|
-
throw error;
|
|
316
|
-
}
|
|
1342
|
+
});
|
|
1343
|
+
},
|
|
1344
|
+
[ModelType.OBJECT_SMALL]: async (runtime, params) => {
|
|
1345
|
+
return generateObjectByModelType(runtime, params, ModelType.OBJECT_SMALL, getSmallModel);
|
|
317
1346
|
},
|
|
318
1347
|
[ModelType.OBJECT_LARGE]: async (runtime, params) => {
|
|
319
|
-
|
|
320
|
-
const openai = createOpenAI({
|
|
321
|
-
apiKey: runtime.getSetting("OPENAI_API_KEY"),
|
|
322
|
-
baseURL
|
|
323
|
-
});
|
|
324
|
-
const model = runtime.getSetting("OPENAI_LARGE_MODEL") ?? runtime.getSetting("LARGE_MODEL") ?? "gpt-4o";
|
|
325
|
-
try {
|
|
326
|
-
if (params.schema) {
|
|
327
|
-
logger.info("Using OBJECT_LARGE without schema validation");
|
|
328
|
-
const { object: object2 } = await generateObject({
|
|
329
|
-
model: openai.languageModel(model),
|
|
330
|
-
output: "no-schema",
|
|
331
|
-
prompt: params.prompt,
|
|
332
|
-
temperature: params.temperature
|
|
333
|
-
});
|
|
334
|
-
return object2;
|
|
335
|
-
}
|
|
336
|
-
const { object } = await generateObject({
|
|
337
|
-
model: openai.languageModel(model),
|
|
338
|
-
output: "no-schema",
|
|
339
|
-
prompt: params.prompt,
|
|
340
|
-
temperature: params.temperature
|
|
341
|
-
});
|
|
342
|
-
return object;
|
|
343
|
-
} catch (error) {
|
|
344
|
-
logger.error("Error generating object:", error);
|
|
345
|
-
throw error;
|
|
346
|
-
}
|
|
1348
|
+
return generateObjectByModelType(runtime, params, ModelType.OBJECT_LARGE, getLargeModel);
|
|
347
1349
|
}
|
|
348
1350
|
},
|
|
349
1351
|
tests: [
|
|
@@ -353,14 +1355,14 @@ var openaiPlugin = {
|
|
|
353
1355
|
{
|
|
354
1356
|
name: "openai_test_url_and_api_key_validation",
|
|
355
1357
|
fn: async (runtime) => {
|
|
356
|
-
const baseURL = runtime
|
|
1358
|
+
const baseURL = getBaseURL(runtime);
|
|
357
1359
|
const response = await fetch(`${baseURL}/models`, {
|
|
358
1360
|
headers: {
|
|
359
|
-
Authorization: `Bearer ${runtime
|
|
1361
|
+
Authorization: `Bearer ${getApiKey(runtime)}`
|
|
360
1362
|
}
|
|
361
1363
|
});
|
|
362
1364
|
const data = await response.json();
|
|
363
|
-
logger.log("Models Available:", data?.data
|
|
1365
|
+
logger.log("Models Available:", data?.data?.length ?? "N/A");
|
|
364
1366
|
if (!response.ok) {
|
|
365
1367
|
throw new Error(`Failed to validate OpenAI API key: ${response.statusText}`);
|
|
366
1368
|
}
|
|
@@ -375,7 +1377,8 @@ var openaiPlugin = {
|
|
|
375
1377
|
});
|
|
376
1378
|
logger.log("embedding", embedding);
|
|
377
1379
|
} catch (error) {
|
|
378
|
-
|
|
1380
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1381
|
+
logger.error(`Error in test_text_embedding: ${message}`);
|
|
379
1382
|
throw error;
|
|
380
1383
|
}
|
|
381
1384
|
}
|
|
@@ -392,7 +1395,8 @@ var openaiPlugin = {
|
|
|
392
1395
|
}
|
|
393
1396
|
logger.log("generated with test_text_large:", text);
|
|
394
1397
|
} catch (error) {
|
|
395
|
-
|
|
1398
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1399
|
+
logger.error(`Error in test_text_large: ${message}`);
|
|
396
1400
|
throw error;
|
|
397
1401
|
}
|
|
398
1402
|
}
|
|
@@ -409,7 +1413,8 @@ var openaiPlugin = {
|
|
|
409
1413
|
}
|
|
410
1414
|
logger.log("generated with test_text_small:", text);
|
|
411
1415
|
} catch (error) {
|
|
412
|
-
|
|
1416
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1417
|
+
logger.error(`Error in test_text_small: ${message}`);
|
|
413
1418
|
throw error;
|
|
414
1419
|
}
|
|
415
1420
|
}
|
|
@@ -426,7 +1431,8 @@ var openaiPlugin = {
|
|
|
426
1431
|
});
|
|
427
1432
|
logger.log("generated with test_image_generation:", image);
|
|
428
1433
|
} catch (error) {
|
|
429
|
-
|
|
1434
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1435
|
+
logger.error(`Error in test_image_generation: ${message}`);
|
|
430
1436
|
throw error;
|
|
431
1437
|
}
|
|
432
1438
|
}
|
|
@@ -447,10 +1453,12 @@ var openaiPlugin = {
|
|
|
447
1453
|
logger.error("Invalid image description result format:", result);
|
|
448
1454
|
}
|
|
449
1455
|
} catch (e) {
|
|
450
|
-
|
|
1456
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
1457
|
+
logger.error(`Error in image description test: ${message}`);
|
|
451
1458
|
}
|
|
452
1459
|
} catch (e) {
|
|
453
|
-
|
|
1460
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
1461
|
+
logger.error(`Error in openai_test_image_description: ${message}`);
|
|
454
1462
|
}
|
|
455
1463
|
}
|
|
456
1464
|
},
|
|
@@ -469,7 +1477,8 @@ var openaiPlugin = {
|
|
|
469
1477
|
);
|
|
470
1478
|
logger.log("generated with test_transcription:", transcription);
|
|
471
1479
|
} catch (error) {
|
|
472
|
-
|
|
1480
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1481
|
+
logger.error(`Error in test_transcription: ${message}`);
|
|
473
1482
|
throw error;
|
|
474
1483
|
}
|
|
475
1484
|
}
|
|
@@ -498,6 +1507,23 @@ var openaiPlugin = {
|
|
|
498
1507
|
}
|
|
499
1508
|
logger.log("Decoded text:", decodedText);
|
|
500
1509
|
}
|
|
1510
|
+
},
|
|
1511
|
+
{
|
|
1512
|
+
name: "openai_test_text_to_speech",
|
|
1513
|
+
fn: async (runtime) => {
|
|
1514
|
+
try {
|
|
1515
|
+
const text = "Hello, this is a test for text-to-speech.";
|
|
1516
|
+
const response = await fetchTextToSpeech(runtime, text);
|
|
1517
|
+
if (!response) {
|
|
1518
|
+
throw new Error("Failed to generate speech");
|
|
1519
|
+
}
|
|
1520
|
+
logger.log("Generated speech successfully");
|
|
1521
|
+
} catch (error) {
|
|
1522
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1523
|
+
logger.error(`Error in openai_test_text_to_speech: ${message}`);
|
|
1524
|
+
throw error;
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
501
1527
|
}
|
|
502
1528
|
]
|
|
503
1529
|
}
|