@terreno/ai 57.3.0 → 57.6.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 +2 -2
- package/dist/aiAdminApp.js +9 -10
- package/dist/aiApp.js +31 -31
- package/dist/langfuseApp.js +33 -76
- package/dist/langfuseCache.js +20 -79
- package/dist/langfuseClient.js +13 -58
- package/dist/langfusePrompts.js +70 -173
- package/dist/langfuseRoutesEvaluations.js +26 -73
- package/dist/langfuseRoutesMiddleware.js +4 -4
- package/dist/langfuseRoutesPlayground.js +29 -99
- package/dist/langfuseRoutesPrompts.js +48 -121
- package/dist/langfuseRoutesTraces.js +24 -76
- package/dist/langfuseTracing.js +15 -60
- package/dist/langfuseVercelAi.js +38 -87
- package/dist/models/aiRequest.js +19 -72
- package/dist/models/fileAttachment.js +3 -3
- package/dist/models/gptHistory.js +5 -5
- package/dist/models/project.js +4 -4
- package/dist/routes/aiRequestsExplorer.js +89 -150
- package/dist/routes/files.js +50 -115
- package/dist/routes/gpt.js +457 -694
- package/dist/routes/gptHistories.js +17 -19
- package/dist/routes/mcp.js +30 -102
- package/dist/routes/projects.js +53 -116
- package/dist/service/aiService.js +393 -706
- package/dist/service/fileStorage.js +42 -134
- package/dist/service/gemini.js +55 -130
- package/dist/service/getMCPTools.js +17 -86
- package/dist/service/mcpService.js +87 -258
- package/dist/service/parseAiJson.js +32 -32
- package/dist/service/vertex.js +129 -283
- package/package.json +30 -24
- package/src/langfuseApp.ts +6 -1
- package/src/tests/helpers.ts +3 -3
- package/tsconfig.json +6 -21
|
@@ -1,90 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
24
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
50
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
51
|
-
var m = o[Symbol.asyncIterator], i;
|
|
52
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
53
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
54
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
55
|
-
};
|
|
56
|
-
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
|
57
|
-
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
|
58
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
59
|
-
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
60
|
-
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
|
|
61
|
-
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
|
|
62
|
-
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
|
|
63
|
-
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
|
64
|
-
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
|
65
|
-
function fulfill(value) { resume("next", value); }
|
|
66
|
-
function reject(value) { resume("throw", value); }
|
|
67
|
-
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
68
|
-
};
|
|
69
|
-
var __values = (this && this.__values) || function(o) {
|
|
70
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
71
|
-
if (m) return m.call(o);
|
|
72
|
-
if (o && typeof o.length === "number") return {
|
|
73
|
-
next: function () {
|
|
74
|
-
if (o && i >= o.length) o = void 0;
|
|
75
|
-
return { value: o && o[i++], done: !o };
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
79
|
-
};
|
|
80
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
81
3
|
exports.AIService = exports.TemperaturePresets = void 0;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
4
|
+
const api_1 = require("@terreno/api");
|
|
5
|
+
const ai_1 = require("ai");
|
|
6
|
+
const luxon_1 = require("luxon");
|
|
7
|
+
const aiRequest_1 = require("../models/aiRequest");
|
|
8
|
+
const parseAiJson_1 = require("./parseAiJson");
|
|
9
|
+
const prompts_1 = require("./prompts");
|
|
88
10
|
exports.TemperaturePresets = {
|
|
89
11
|
BALANCED: 0.7,
|
|
90
12
|
DEFAULT: 1.0,
|
|
@@ -98,694 +20,459 @@ exports.TemperaturePresets = {
|
|
|
98
20
|
* {@link normalizeLlmJsonTextForStructuredOutput} (fences, preamble, balanced slice, light repairs)
|
|
99
21
|
* before Vercel `Output.*` parsing.
|
|
100
22
|
*/
|
|
101
|
-
|
|
23
|
+
const withStrippedJsonFencesModel = (model) => {
|
|
102
24
|
if (typeof model === "string") {
|
|
103
25
|
return model;
|
|
104
26
|
}
|
|
105
27
|
return new Proxy(model, {
|
|
106
|
-
get
|
|
107
|
-
var _this = this;
|
|
28
|
+
get(target, prop, receiver) {
|
|
108
29
|
if (prop === "doGenerate") {
|
|
109
|
-
|
|
30
|
+
const original = Reflect.get(target, prop, receiver);
|
|
110
31
|
if (typeof original !== "function") {
|
|
111
32
|
return original;
|
|
112
33
|
}
|
|
113
|
-
|
|
114
|
-
return
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}); };
|
|
34
|
+
const boundGenerate = original;
|
|
35
|
+
return async (options) => {
|
|
36
|
+
const result = await Promise.resolve(boundGenerate.call(target, options));
|
|
37
|
+
if (!result?.content || !Array.isArray(result.content)) {
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
...result,
|
|
42
|
+
content: result.content.map((part) => {
|
|
43
|
+
if (part.type !== "text" || typeof part.text !== "string") {
|
|
44
|
+
return part;
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
...part,
|
|
48
|
+
text: (0, parseAiJson_1.normalizeLlmJsonTextForStructuredOutput)(part.text),
|
|
49
|
+
};
|
|
50
|
+
}),
|
|
51
|
+
};
|
|
52
|
+
};
|
|
133
53
|
}
|
|
134
54
|
return Reflect.get(target, prop, receiver);
|
|
135
55
|
},
|
|
136
56
|
});
|
|
137
57
|
};
|
|
138
|
-
|
|
139
|
-
var _a;
|
|
58
|
+
const getModelId = (model) => {
|
|
140
59
|
if (typeof model === "string") {
|
|
141
60
|
return model;
|
|
142
61
|
}
|
|
143
|
-
return
|
|
62
|
+
return model.modelId ?? "unknown";
|
|
144
63
|
};
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
64
|
+
class AIService {
|
|
65
|
+
model;
|
|
66
|
+
defaultTemperature;
|
|
67
|
+
structuredJsonModel;
|
|
68
|
+
constructor({ model, defaultTemperature = exports.TemperaturePresets.DEFAULT }) {
|
|
148
69
|
this.model = model;
|
|
149
70
|
this.defaultTemperature = defaultTemperature;
|
|
150
71
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
enumerable: false,
|
|
156
|
-
configurable: true
|
|
157
|
-
});
|
|
158
|
-
AIService.prototype.getModelForStructuredJson = function () {
|
|
72
|
+
get modelId() {
|
|
73
|
+
return getModelId(this.model);
|
|
74
|
+
}
|
|
75
|
+
getModelForStructuredJson() {
|
|
159
76
|
if (!this.structuredJsonModel) {
|
|
160
77
|
this.structuredJsonModel = withStrippedJsonFencesModel(this.model);
|
|
161
78
|
}
|
|
162
79
|
return this.structuredJsonModel;
|
|
163
|
-
}
|
|
164
|
-
|
|
80
|
+
}
|
|
81
|
+
describeStructuredGenerationError(error) {
|
|
165
82
|
if (error instanceof Error) {
|
|
166
|
-
|
|
83
|
+
const base = `${error.name}: ${error.message}`;
|
|
167
84
|
if (error.cause instanceof Error) {
|
|
168
|
-
return
|
|
85
|
+
return `${base} | cause: ${error.cause.name}: ${error.cause.message}`;
|
|
169
86
|
}
|
|
170
87
|
return base;
|
|
171
88
|
}
|
|
172
89
|
return String(error);
|
|
173
|
-
}
|
|
174
|
-
|
|
90
|
+
}
|
|
91
|
+
extractRawModelTextFromStructuredError(error) {
|
|
175
92
|
if (ai_1.NoObjectGeneratedError.isInstance(error) && typeof error.text === "string") {
|
|
176
93
|
return error.text;
|
|
177
94
|
}
|
|
178
95
|
if (error instanceof Error && "text" in error) {
|
|
179
|
-
|
|
96
|
+
const t = error.text;
|
|
180
97
|
if (typeof t === "string") {
|
|
181
98
|
return t;
|
|
182
99
|
}
|
|
183
100
|
}
|
|
184
101
|
return undefined;
|
|
185
|
-
}
|
|
186
|
-
|
|
102
|
+
}
|
|
103
|
+
extractFinishReasonFromStructuredError(error) {
|
|
187
104
|
if (ai_1.NoObjectGeneratedError.isInstance(error) && error.finishReason) {
|
|
188
105
|
return error.finishReason;
|
|
189
106
|
}
|
|
190
107
|
return undefined;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
finishReason: finishReason,
|
|
213
|
-
prompt: params.prompt,
|
|
214
|
-
requestType: params.requestType,
|
|
215
|
-
response: rawText !== null && rawText !== void 0 ? rawText : "",
|
|
216
|
-
system: params.system,
|
|
217
|
-
});
|
|
218
|
-
return [4 /*yield*/, this.logRequest({
|
|
219
|
-
aiModel: getModelId(this.model),
|
|
220
|
-
error: errorDescription,
|
|
221
|
-
metadata: {
|
|
222
|
-
errorStack: errorStack,
|
|
223
|
-
finishReason: finishReason,
|
|
224
|
-
rawModelTextCaptured: Boolean(rawText && rawText.length > 0),
|
|
225
|
-
system: params.system,
|
|
226
|
-
},
|
|
227
|
-
prompt: params.prompt,
|
|
228
|
-
requestType: params.requestType,
|
|
229
|
-
response: responseForLog,
|
|
230
|
-
responseTime: params.responseTime,
|
|
231
|
-
userId: params.userId,
|
|
232
|
-
})];
|
|
233
|
-
case 1:
|
|
234
|
-
_a.sent();
|
|
235
|
-
return [2 /*return*/];
|
|
236
|
-
}
|
|
237
|
-
});
|
|
108
|
+
}
|
|
109
|
+
async logStructuredJsonFailure(params) {
|
|
110
|
+
const rawText = this.extractRawModelTextFromStructuredError(params.error);
|
|
111
|
+
const responseForLog = rawText !== undefined && rawText.length > 0
|
|
112
|
+
? rawText
|
|
113
|
+
: "(no raw model text captured on this error)";
|
|
114
|
+
const errorDescription = this.describeStructuredGenerationError(params.error);
|
|
115
|
+
const finishReason = this.extractFinishReasonFromStructuredError(params.error);
|
|
116
|
+
const errorStack = params.error instanceof Error && typeof params.error.stack === "string"
|
|
117
|
+
? params.error.stack.length > 8000
|
|
118
|
+
? `${params.error.stack.slice(0, 8000)}…`
|
|
119
|
+
: params.error.stack
|
|
120
|
+
: undefined;
|
|
121
|
+
api_1.logger.error("AIService structured JSON generation failed", {
|
|
122
|
+
aiModel: getModelId(this.model),
|
|
123
|
+
error: errorDescription,
|
|
124
|
+
finishReason,
|
|
125
|
+
prompt: params.prompt,
|
|
126
|
+
requestType: params.requestType,
|
|
127
|
+
response: rawText ?? "",
|
|
128
|
+
system: params.system,
|
|
238
129
|
});
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
return [3 /*break*/, 3];
|
|
254
|
-
case 3: return [2 /*return*/];
|
|
255
|
-
}
|
|
256
|
-
});
|
|
130
|
+
await this.logRequest({
|
|
131
|
+
aiModel: getModelId(this.model),
|
|
132
|
+
error: errorDescription,
|
|
133
|
+
metadata: {
|
|
134
|
+
errorStack,
|
|
135
|
+
finishReason,
|
|
136
|
+
rawModelTextCaptured: Boolean(rawText && rawText.length > 0),
|
|
137
|
+
system: params.system,
|
|
138
|
+
},
|
|
139
|
+
prompt: params.prompt,
|
|
140
|
+
requestType: params.requestType,
|
|
141
|
+
response: responseForLog,
|
|
142
|
+
responseTime: params.responseTime,
|
|
143
|
+
userId: params.userId,
|
|
257
144
|
});
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
})];
|
|
279
|
-
case 2:
|
|
280
|
-
result = _b.sent();
|
|
281
|
-
responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
282
|
-
return [4 /*yield*/, this.logRequest({
|
|
283
|
-
aiModel: getModelId(this.model),
|
|
284
|
-
prompt: prompt,
|
|
285
|
-
requestType: "general",
|
|
286
|
-
response: result.text,
|
|
287
|
-
responseTime: responseTime,
|
|
288
|
-
tokensUsed: (_a = result.usage) === null || _a === void 0 ? void 0 : _a.totalTokens,
|
|
289
|
-
userId: userId,
|
|
290
|
-
})];
|
|
291
|
-
case 3:
|
|
292
|
-
_b.sent();
|
|
293
|
-
return [2 /*return*/, result.text];
|
|
294
|
-
case 4:
|
|
295
|
-
error_1 = _b.sent();
|
|
296
|
-
responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
297
|
-
return [4 /*yield*/, this.logRequest({
|
|
298
|
-
aiModel: getModelId(this.model),
|
|
299
|
-
error: error_1 instanceof Error ? error_1.message : String(error_1),
|
|
300
|
-
prompt: prompt,
|
|
301
|
-
requestType: "general",
|
|
302
|
-
responseTime: responseTime,
|
|
303
|
-
userId: userId,
|
|
304
|
-
})];
|
|
305
|
-
case 5:
|
|
306
|
-
_b.sent();
|
|
307
|
-
throw error_1;
|
|
308
|
-
case 6: return [2 /*return*/];
|
|
309
|
-
}
|
|
145
|
+
}
|
|
146
|
+
async logRequest(params) {
|
|
147
|
+
try {
|
|
148
|
+
await aiRequest_1.AIRequest.logRequest(params);
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
// Logging failures should not break the main flow
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
async generateText(options) {
|
|
155
|
+
const { prompt, systemPrompt, temperature, maxOutputTokens, userId } = options;
|
|
156
|
+
const startTime = luxon_1.DateTime.now().toMillis();
|
|
157
|
+
try {
|
|
158
|
+
const result = await (0, ai_1.generateText)({
|
|
159
|
+
experimental_telemetry: { functionId: "generate-text", isEnabled: true },
|
|
160
|
+
maxOutputTokens,
|
|
161
|
+
model: this.model,
|
|
162
|
+
prompt,
|
|
163
|
+
system: systemPrompt,
|
|
164
|
+
temperature: temperature ?? this.defaultTemperature,
|
|
310
165
|
});
|
|
311
|
-
|
|
312
|
-
|
|
166
|
+
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
167
|
+
await this.logRequest({
|
|
168
|
+
aiModel: getModelId(this.model),
|
|
169
|
+
prompt,
|
|
170
|
+
requestType: "general",
|
|
171
|
+
response: result.text,
|
|
172
|
+
responseTime,
|
|
173
|
+
tokensUsed: result.usage?.totalTokens,
|
|
174
|
+
userId,
|
|
175
|
+
});
|
|
176
|
+
return result.text;
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
180
|
+
await this.logRequest({
|
|
181
|
+
aiModel: getModelId(this.model),
|
|
182
|
+
error: error instanceof Error ? error.message : String(error),
|
|
183
|
+
prompt,
|
|
184
|
+
requestType: "general",
|
|
185
|
+
responseTime,
|
|
186
|
+
userId,
|
|
187
|
+
});
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
313
191
|
/** Any JSON value (object, array, primitive, or null) via the AI SDK `Output.json()` parser. */
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
return [4 /*yield*/, (0, ai_1.generateText)({
|
|
328
|
-
experimental_telemetry: { functionId: "generate-json-value", isEnabled: true },
|
|
329
|
-
maxOutputTokens: maxOutputTokens,
|
|
330
|
-
model: this.getModelForStructuredJson(),
|
|
331
|
-
output: ai_1.Output.json({ description: outputDescription, name: outputName }),
|
|
332
|
-
prompt: prompt,
|
|
333
|
-
system: system,
|
|
334
|
-
temperature: temperature !== null && temperature !== void 0 ? temperature : exports.TemperaturePresets.DETERMINISTIC,
|
|
335
|
-
})];
|
|
336
|
-
case 2:
|
|
337
|
-
result = _b.sent();
|
|
338
|
-
responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
339
|
-
return [4 /*yield*/, this.logRequest({
|
|
340
|
-
aiModel: getModelId(this.model),
|
|
341
|
-
prompt: prompt,
|
|
342
|
-
requestType: "json_value",
|
|
343
|
-
response: JSON.stringify(result.output),
|
|
344
|
-
responseTime: responseTime,
|
|
345
|
-
tokensUsed: (_a = result.usage) === null || _a === void 0 ? void 0 : _a.totalTokens,
|
|
346
|
-
userId: userId,
|
|
347
|
-
})];
|
|
348
|
-
case 3:
|
|
349
|
-
_b.sent();
|
|
350
|
-
return [2 /*return*/, result.output];
|
|
351
|
-
case 4:
|
|
352
|
-
error_2 = _b.sent();
|
|
353
|
-
responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
354
|
-
return [4 /*yield*/, this.logStructuredJsonFailure({
|
|
355
|
-
error: error_2,
|
|
356
|
-
prompt: prompt,
|
|
357
|
-
requestType: "json_value",
|
|
358
|
-
responseTime: responseTime,
|
|
359
|
-
system: system,
|
|
360
|
-
userId: userId,
|
|
361
|
-
})];
|
|
362
|
-
case 5:
|
|
363
|
-
_b.sent();
|
|
364
|
-
throw error_2;
|
|
365
|
-
case 6: return [2 /*return*/];
|
|
366
|
-
}
|
|
192
|
+
async generateJsonValue(options) {
|
|
193
|
+
const { maxOutputTokens, outputDescription, outputName, prompt, systemPrompt, temperature, userId, } = options;
|
|
194
|
+
const startTime = luxon_1.DateTime.now().toMillis();
|
|
195
|
+
const system = systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
|
|
196
|
+
try {
|
|
197
|
+
const result = await (0, ai_1.generateText)({
|
|
198
|
+
experimental_telemetry: { functionId: "generate-json-value", isEnabled: true },
|
|
199
|
+
maxOutputTokens,
|
|
200
|
+
model: this.getModelForStructuredJson(),
|
|
201
|
+
output: ai_1.Output.json({ description: outputDescription, name: outputName }),
|
|
202
|
+
prompt,
|
|
203
|
+
system,
|
|
204
|
+
temperature: temperature ?? exports.TemperaturePresets.DETERMINISTIC,
|
|
367
205
|
});
|
|
368
|
-
|
|
369
|
-
|
|
206
|
+
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
207
|
+
await this.logRequest({
|
|
208
|
+
aiModel: getModelId(this.model),
|
|
209
|
+
prompt,
|
|
210
|
+
requestType: "json_value",
|
|
211
|
+
response: JSON.stringify(result.output),
|
|
212
|
+
responseTime,
|
|
213
|
+
tokensUsed: result.usage?.totalTokens,
|
|
214
|
+
userId,
|
|
215
|
+
});
|
|
216
|
+
return result.output;
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
220
|
+
await this.logStructuredJsonFailure({
|
|
221
|
+
error,
|
|
222
|
+
prompt,
|
|
223
|
+
requestType: "json_value",
|
|
224
|
+
responseTime,
|
|
225
|
+
system,
|
|
226
|
+
userId,
|
|
227
|
+
});
|
|
228
|
+
throw error;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
370
231
|
/** Typed object from a Zod schema, `jsonSchema(...)`, or other `FlexibleSchema` (`Output.object()`). */
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
output: ai_1.Output.object({
|
|
389
|
-
description: schemaDescription,
|
|
390
|
-
name: schemaName,
|
|
391
|
-
schema: schema,
|
|
392
|
-
}),
|
|
393
|
-
prompt: prompt,
|
|
394
|
-
system: system,
|
|
395
|
-
temperature: temperature !== null && temperature !== void 0 ? temperature : exports.TemperaturePresets.DETERMINISTIC,
|
|
396
|
-
})];
|
|
397
|
-
case 2:
|
|
398
|
-
result = _b.sent();
|
|
399
|
-
responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
400
|
-
return [4 /*yield*/, this.logRequest({
|
|
401
|
-
aiModel: getModelId(this.model),
|
|
402
|
-
prompt: prompt,
|
|
403
|
-
requestType: "json_object",
|
|
404
|
-
response: JSON.stringify(result.output),
|
|
405
|
-
responseTime: responseTime,
|
|
406
|
-
tokensUsed: (_a = result.usage) === null || _a === void 0 ? void 0 : _a.totalTokens,
|
|
407
|
-
userId: userId,
|
|
408
|
-
})];
|
|
409
|
-
case 3:
|
|
410
|
-
_b.sent();
|
|
411
|
-
return [2 /*return*/, result.output];
|
|
412
|
-
case 4:
|
|
413
|
-
error_3 = _b.sent();
|
|
414
|
-
responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
415
|
-
return [4 /*yield*/, this.logStructuredJsonFailure({
|
|
416
|
-
error: error_3,
|
|
417
|
-
prompt: prompt,
|
|
418
|
-
requestType: "json_object",
|
|
419
|
-
responseTime: responseTime,
|
|
420
|
-
system: system,
|
|
421
|
-
userId: userId,
|
|
422
|
-
})];
|
|
423
|
-
case 5:
|
|
424
|
-
_b.sent();
|
|
425
|
-
throw error_3;
|
|
426
|
-
case 6: return [2 /*return*/];
|
|
427
|
-
}
|
|
232
|
+
async generateJsonObject(options) {
|
|
233
|
+
const { maxOutputTokens, prompt, schema, schemaDescription, schemaName, systemPrompt, temperature, userId, } = options;
|
|
234
|
+
const startTime = luxon_1.DateTime.now().toMillis();
|
|
235
|
+
const system = systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
|
|
236
|
+
try {
|
|
237
|
+
const result = await (0, ai_1.generateText)({
|
|
238
|
+
experimental_telemetry: { functionId: "generate-json-object", isEnabled: true },
|
|
239
|
+
maxOutputTokens,
|
|
240
|
+
model: this.getModelForStructuredJson(),
|
|
241
|
+
output: ai_1.Output.object({
|
|
242
|
+
description: schemaDescription,
|
|
243
|
+
name: schemaName,
|
|
244
|
+
schema,
|
|
245
|
+
}),
|
|
246
|
+
prompt,
|
|
247
|
+
system,
|
|
248
|
+
temperature: temperature ?? exports.TemperaturePresets.DETERMINISTIC,
|
|
428
249
|
});
|
|
429
|
-
|
|
430
|
-
|
|
250
|
+
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
251
|
+
await this.logRequest({
|
|
252
|
+
aiModel: getModelId(this.model),
|
|
253
|
+
prompt,
|
|
254
|
+
requestType: "json_object",
|
|
255
|
+
response: JSON.stringify(result.output),
|
|
256
|
+
responseTime,
|
|
257
|
+
tokensUsed: result.usage?.totalTokens,
|
|
258
|
+
userId,
|
|
259
|
+
});
|
|
260
|
+
return result.output;
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
264
|
+
await this.logStructuredJsonFailure({
|
|
265
|
+
error,
|
|
266
|
+
prompt,
|
|
267
|
+
requestType: "json_object",
|
|
268
|
+
responseTime,
|
|
269
|
+
system,
|
|
270
|
+
userId,
|
|
271
|
+
});
|
|
272
|
+
throw error;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
431
275
|
/**
|
|
432
276
|
* Typed array: the model is steered to emit `{"elements":[...]}`; the SDK validates each entry
|
|
433
277
|
* and this method returns the plain array (`Output.array()`).
|
|
434
278
|
*/
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
output: ai_1.Output.array({
|
|
453
|
-
description: outputDescription,
|
|
454
|
-
element: element,
|
|
455
|
-
name: outputName,
|
|
456
|
-
}),
|
|
457
|
-
prompt: prompt,
|
|
458
|
-
system: system,
|
|
459
|
-
temperature: temperature !== null && temperature !== void 0 ? temperature : exports.TemperaturePresets.DETERMINISTIC,
|
|
460
|
-
})];
|
|
461
|
-
case 2:
|
|
462
|
-
result = _b.sent();
|
|
463
|
-
responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
464
|
-
return [4 /*yield*/, this.logRequest({
|
|
465
|
-
aiModel: getModelId(this.model),
|
|
466
|
-
prompt: prompt,
|
|
467
|
-
requestType: "json_array",
|
|
468
|
-
response: JSON.stringify(result.output),
|
|
469
|
-
responseTime: responseTime,
|
|
470
|
-
tokensUsed: (_a = result.usage) === null || _a === void 0 ? void 0 : _a.totalTokens,
|
|
471
|
-
userId: userId,
|
|
472
|
-
})];
|
|
473
|
-
case 3:
|
|
474
|
-
_b.sent();
|
|
475
|
-
return [2 /*return*/, result.output];
|
|
476
|
-
case 4:
|
|
477
|
-
error_4 = _b.sent();
|
|
478
|
-
responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
479
|
-
return [4 /*yield*/, this.logStructuredJsonFailure({
|
|
480
|
-
error: error_4,
|
|
481
|
-
prompt: prompt,
|
|
482
|
-
requestType: "json_array",
|
|
483
|
-
responseTime: responseTime,
|
|
484
|
-
system: system,
|
|
485
|
-
userId: userId,
|
|
486
|
-
})];
|
|
487
|
-
case 5:
|
|
488
|
-
_b.sent();
|
|
489
|
-
throw error_4;
|
|
490
|
-
case 6: return [2 /*return*/];
|
|
491
|
-
}
|
|
279
|
+
async generateJsonArray(options) {
|
|
280
|
+
const { element, maxOutputTokens, outputDescription, outputName, prompt, systemPrompt, temperature, userId, } = options;
|
|
281
|
+
const startTime = luxon_1.DateTime.now().toMillis();
|
|
282
|
+
const system = systemPrompt ?? prompts_1.JSON_VALUE_SYSTEM_PROMPT;
|
|
283
|
+
try {
|
|
284
|
+
const result = await (0, ai_1.generateText)({
|
|
285
|
+
experimental_telemetry: { functionId: "generate-json-array", isEnabled: true },
|
|
286
|
+
maxOutputTokens,
|
|
287
|
+
model: this.getModelForStructuredJson(),
|
|
288
|
+
output: ai_1.Output.array({
|
|
289
|
+
description: outputDescription,
|
|
290
|
+
element,
|
|
291
|
+
name: outputName,
|
|
292
|
+
}),
|
|
293
|
+
prompt,
|
|
294
|
+
system,
|
|
295
|
+
temperature: temperature ?? exports.TemperaturePresets.DETERMINISTIC,
|
|
492
296
|
});
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
prompt = options.prompt, systemPrompt = options.systemPrompt, temperature = options.temperature, maxOutputTokens = options.maxOutputTokens, userId = options.userId;
|
|
503
|
-
startTime = luxon_1.DateTime.now().toMillis();
|
|
504
|
-
fullResponse = "";
|
|
505
|
-
_g.label = 1;
|
|
506
|
-
case 1:
|
|
507
|
-
_g.trys.push([1, 18, , 20]);
|
|
508
|
-
result = (0, ai_1.streamText)({
|
|
509
|
-
experimental_telemetry: { functionId: "generate-text-stream", isEnabled: true },
|
|
510
|
-
maxOutputTokens: maxOutputTokens,
|
|
511
|
-
model: this.model,
|
|
512
|
-
prompt: prompt,
|
|
513
|
-
system: systemPrompt,
|
|
514
|
-
temperature: temperature !== null && temperature !== void 0 ? temperature : this.defaultTemperature,
|
|
515
|
-
});
|
|
516
|
-
_g.label = 2;
|
|
517
|
-
case 2:
|
|
518
|
-
_g.trys.push([2, 9, 10, 15]);
|
|
519
|
-
_a = true, _b = __asyncValues(result.textStream);
|
|
520
|
-
_g.label = 3;
|
|
521
|
-
case 3: return [4 /*yield*/, __await(_b.next())];
|
|
522
|
-
case 4:
|
|
523
|
-
if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 8];
|
|
524
|
-
_f = _c.value;
|
|
525
|
-
_a = false;
|
|
526
|
-
chunk = _f;
|
|
527
|
-
fullResponse += chunk;
|
|
528
|
-
return [4 /*yield*/, __await(chunk)];
|
|
529
|
-
case 5: return [4 /*yield*/, _g.sent()];
|
|
530
|
-
case 6:
|
|
531
|
-
_g.sent();
|
|
532
|
-
_g.label = 7;
|
|
533
|
-
case 7:
|
|
534
|
-
_a = true;
|
|
535
|
-
return [3 /*break*/, 3];
|
|
536
|
-
case 8: return [3 /*break*/, 15];
|
|
537
|
-
case 9:
|
|
538
|
-
e_1_1 = _g.sent();
|
|
539
|
-
e_1 = { error: e_1_1 };
|
|
540
|
-
return [3 /*break*/, 15];
|
|
541
|
-
case 10:
|
|
542
|
-
_g.trys.push([10, , 13, 14]);
|
|
543
|
-
if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 12];
|
|
544
|
-
return [4 /*yield*/, __await(_e.call(_b))];
|
|
545
|
-
case 11:
|
|
546
|
-
_g.sent();
|
|
547
|
-
_g.label = 12;
|
|
548
|
-
case 12: return [3 /*break*/, 14];
|
|
549
|
-
case 13:
|
|
550
|
-
if (e_1) throw e_1.error;
|
|
551
|
-
return [7 /*endfinally*/];
|
|
552
|
-
case 14: return [7 /*endfinally*/];
|
|
553
|
-
case 15:
|
|
554
|
-
responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
555
|
-
return [4 /*yield*/, __await(result.usage)];
|
|
556
|
-
case 16:
|
|
557
|
-
usage = _g.sent();
|
|
558
|
-
return [4 /*yield*/, __await(this.logRequest({
|
|
559
|
-
aiModel: getModelId(this.model),
|
|
560
|
-
prompt: prompt,
|
|
561
|
-
requestType: "general",
|
|
562
|
-
response: fullResponse,
|
|
563
|
-
responseTime: responseTime,
|
|
564
|
-
tokensUsed: usage === null || usage === void 0 ? void 0 : usage.totalTokens,
|
|
565
|
-
userId: userId,
|
|
566
|
-
}))];
|
|
567
|
-
case 17:
|
|
568
|
-
_g.sent();
|
|
569
|
-
return [3 /*break*/, 20];
|
|
570
|
-
case 18:
|
|
571
|
-
error_5 = _g.sent();
|
|
572
|
-
responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
573
|
-
return [4 /*yield*/, __await(this.logRequest({
|
|
574
|
-
aiModel: getModelId(this.model),
|
|
575
|
-
error: error_5 instanceof Error ? error_5.message : String(error_5),
|
|
576
|
-
prompt: prompt,
|
|
577
|
-
requestType: "general",
|
|
578
|
-
responseTime: responseTime,
|
|
579
|
-
userId: userId,
|
|
580
|
-
}))];
|
|
581
|
-
case 19:
|
|
582
|
-
_g.sent();
|
|
583
|
-
throw error_5;
|
|
584
|
-
case 20: return [2 /*return*/];
|
|
585
|
-
}
|
|
297
|
+
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
298
|
+
await this.logRequest({
|
|
299
|
+
aiModel: getModelId(this.model),
|
|
300
|
+
prompt,
|
|
301
|
+
requestType: "json_array",
|
|
302
|
+
response: JSON.stringify(result.output),
|
|
303
|
+
responseTime,
|
|
304
|
+
tokensUsed: result.usage?.totalTokens,
|
|
305
|
+
userId,
|
|
586
306
|
});
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
307
|
+
return result.output;
|
|
308
|
+
}
|
|
309
|
+
catch (error) {
|
|
310
|
+
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
311
|
+
await this.logStructuredJsonFailure({
|
|
312
|
+
error,
|
|
313
|
+
prompt,
|
|
314
|
+
requestType: "json_array",
|
|
315
|
+
responseTime,
|
|
316
|
+
system,
|
|
317
|
+
userId,
|
|
598
318
|
});
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
319
|
+
throw error;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
async *generateTextStream(options) {
|
|
323
|
+
const { prompt, systemPrompt, temperature, maxOutputTokens, userId } = options;
|
|
324
|
+
const startTime = luxon_1.DateTime.now().toMillis();
|
|
325
|
+
let fullResponse = "";
|
|
326
|
+
try {
|
|
327
|
+
const result = (0, ai_1.streamText)({
|
|
328
|
+
experimental_telemetry: { functionId: "generate-text-stream", isEnabled: true },
|
|
329
|
+
maxOutputTokens,
|
|
330
|
+
model: this.model,
|
|
331
|
+
prompt,
|
|
332
|
+
system: systemPrompt,
|
|
333
|
+
temperature: temperature ?? this.defaultTemperature,
|
|
610
334
|
});
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
335
|
+
for await (const chunk of result.textStream) {
|
|
336
|
+
fullResponse += chunk;
|
|
337
|
+
yield chunk;
|
|
338
|
+
}
|
|
339
|
+
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
340
|
+
const usage = await result.usage;
|
|
341
|
+
await this.logRequest({
|
|
342
|
+
aiModel: getModelId(this.model),
|
|
343
|
+
prompt,
|
|
344
|
+
requestType: "general",
|
|
345
|
+
response: fullResponse,
|
|
346
|
+
responseTime,
|
|
347
|
+
tokensUsed: usage?.totalTokens,
|
|
348
|
+
userId,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
catch (error) {
|
|
352
|
+
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
353
|
+
await this.logRequest({
|
|
354
|
+
aiModel: getModelId(this.model),
|
|
355
|
+
error: error instanceof Error ? error.message : String(error),
|
|
356
|
+
prompt,
|
|
357
|
+
requestType: "general",
|
|
358
|
+
responseTime,
|
|
359
|
+
userId,
|
|
625
360
|
});
|
|
361
|
+
throw error;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
async generateRemix(options) {
|
|
365
|
+
return this.generateText({
|
|
366
|
+
prompt: options.text,
|
|
367
|
+
systemPrompt: prompts_1.REMIX_PROMPT,
|
|
368
|
+
temperature: exports.TemperaturePresets.BALANCED,
|
|
369
|
+
userId: options.userId,
|
|
626
370
|
});
|
|
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
|
-
urlPrefix: (_d = part.url) === null || _d === void 0 ? void 0 : _d.substring(0, 50),
|
|
659
|
-
});
|
|
660
|
-
parts.push({
|
|
661
|
-
data: new URL(part.url),
|
|
662
|
-
filename: part.filename,
|
|
663
|
-
mediaType: part.mimeType,
|
|
664
|
-
type: "file",
|
|
665
|
-
});
|
|
666
|
-
}
|
|
667
|
-
}
|
|
371
|
+
}
|
|
372
|
+
async generateSummary(options) {
|
|
373
|
+
return this.generateText({
|
|
374
|
+
prompt: options.text,
|
|
375
|
+
systemPrompt: prompts_1.CONTENT_SUMMARY_PROMPT,
|
|
376
|
+
temperature: exports.TemperaturePresets.LOW,
|
|
377
|
+
userId: options.userId,
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
async translateText(options) {
|
|
381
|
+
const { text, targetLanguage, sourceLanguage = "auto-detect", userId } = options;
|
|
382
|
+
const systemPrompt = prompts_1.TRANSLATION_PROMPT.replace("{sourceLanguage}", sourceLanguage).replace("{targetLanguage}", targetLanguage);
|
|
383
|
+
return this.generateText({
|
|
384
|
+
prompt: text,
|
|
385
|
+
systemPrompt,
|
|
386
|
+
temperature: exports.TemperaturePresets.LOW,
|
|
387
|
+
userId,
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
buildMessages(prompts) {
|
|
391
|
+
const messages = [];
|
|
392
|
+
for (const prompt of prompts) {
|
|
393
|
+
if (prompt.type === "tool-call" || prompt.type === "tool-result") {
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
const role = prompt.type;
|
|
397
|
+
if (prompt.content && prompt.content.length > 0 && role === "user") {
|
|
398
|
+
const parts = [];
|
|
399
|
+
for (const part of prompt.content) {
|
|
400
|
+
if (part.type === "text") {
|
|
401
|
+
parts.push({ text: part.text, type: "text" });
|
|
668
402
|
}
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
}
|
|
674
|
-
|
|
403
|
+
else if (part.type === "image") {
|
|
404
|
+
api_1.logger.debug("Building image message part", {
|
|
405
|
+
mediaType: part.mimeType,
|
|
406
|
+
urlPrefix: part.url?.substring(0, 50),
|
|
407
|
+
});
|
|
408
|
+
parts.push({ image: new URL(part.url), mediaType: part.mimeType, type: "image" });
|
|
409
|
+
}
|
|
410
|
+
else if (part.type === "file") {
|
|
411
|
+
api_1.logger.debug("Building file message part", {
|
|
412
|
+
filename: part.filename,
|
|
413
|
+
mediaType: part.mimeType,
|
|
414
|
+
urlPrefix: part.url?.substring(0, 50),
|
|
415
|
+
});
|
|
416
|
+
parts.push({
|
|
417
|
+
data: new URL(part.url),
|
|
418
|
+
filename: part.filename,
|
|
419
|
+
mediaType: part.mimeType,
|
|
420
|
+
type: "file",
|
|
421
|
+
});
|
|
675
422
|
}
|
|
676
|
-
messages.push({ content: parts, role: "user" });
|
|
677
|
-
}
|
|
678
|
-
else {
|
|
679
|
-
messages.push({ content: prompt_1.text, role: role });
|
|
680
423
|
}
|
|
424
|
+
messages.push({ content: parts, role: "user" });
|
|
681
425
|
}
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
finally {
|
|
685
|
-
try {
|
|
686
|
-
if (prompts_2_1 && !prompts_2_1.done && (_a = prompts_2.return)) _a.call(prompts_2);
|
|
426
|
+
else {
|
|
427
|
+
messages.push({ content: prompt.text, role });
|
|
687
428
|
}
|
|
688
|
-
finally { if (e_2) throw e_2.error; }
|
|
689
429
|
}
|
|
690
430
|
return messages;
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
experimental_telemetry: { functionId: "generate-chat-stream", isEnabled: true },
|
|
708
|
-
messages: messages.map(function (m) { return ({ content: m.content, role: m.role }); }),
|
|
709
|
-
model: this.model,
|
|
710
|
-
stopWhen: stopWhen !== null && stopWhen !== void 0 ? stopWhen : (0, ai_1.stepCountIs)(1),
|
|
711
|
-
system: systemPrompt !== null && systemPrompt !== void 0 ? systemPrompt : prompts_1.DEFAULT_GPT_MEMORY,
|
|
712
|
-
temperature: this.defaultTemperature,
|
|
713
|
-
toolChoice: toolChoice,
|
|
714
|
-
tools: tools,
|
|
715
|
-
});
|
|
716
|
-
_g.label = 2;
|
|
717
|
-
case 2:
|
|
718
|
-
_g.trys.push([2, 9, 10, 15]);
|
|
719
|
-
_a = true, _b = __asyncValues(result.textStream);
|
|
720
|
-
_g.label = 3;
|
|
721
|
-
case 3: return [4 /*yield*/, __await(_b.next())];
|
|
722
|
-
case 4:
|
|
723
|
-
if (!(_c = _g.sent(), _d = _c.done, !_d)) return [3 /*break*/, 8];
|
|
724
|
-
_f = _c.value;
|
|
725
|
-
_a = false;
|
|
726
|
-
chunk = _f;
|
|
727
|
-
fullResponse += chunk;
|
|
728
|
-
return [4 /*yield*/, __await(chunk)];
|
|
729
|
-
case 5: return [4 /*yield*/, _g.sent()];
|
|
730
|
-
case 6:
|
|
731
|
-
_g.sent();
|
|
732
|
-
_g.label = 7;
|
|
733
|
-
case 7:
|
|
734
|
-
_a = true;
|
|
735
|
-
return [3 /*break*/, 3];
|
|
736
|
-
case 8: return [3 /*break*/, 15];
|
|
737
|
-
case 9:
|
|
738
|
-
e_4_1 = _g.sent();
|
|
739
|
-
e_4 = { error: e_4_1 };
|
|
740
|
-
return [3 /*break*/, 15];
|
|
741
|
-
case 10:
|
|
742
|
-
_g.trys.push([10, , 13, 14]);
|
|
743
|
-
if (!(!_a && !_d && (_e = _b.return))) return [3 /*break*/, 12];
|
|
744
|
-
return [4 /*yield*/, __await(_e.call(_b))];
|
|
745
|
-
case 11:
|
|
746
|
-
_g.sent();
|
|
747
|
-
_g.label = 12;
|
|
748
|
-
case 12: return [3 /*break*/, 14];
|
|
749
|
-
case 13:
|
|
750
|
-
if (e_4) throw e_4.error;
|
|
751
|
-
return [7 /*endfinally*/];
|
|
752
|
-
case 14: return [7 /*endfinally*/];
|
|
753
|
-
case 15:
|
|
754
|
-
responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
755
|
-
return [4 /*yield*/, __await(result.usage)];
|
|
756
|
-
case 16:
|
|
757
|
-
usage = _g.sent();
|
|
758
|
-
return [4 /*yield*/, __await(this.logRequest({
|
|
759
|
-
aiModel: getModelId(this.model),
|
|
760
|
-
prompt: promptText,
|
|
761
|
-
requestType: "general",
|
|
762
|
-
response: fullResponse,
|
|
763
|
-
responseTime: responseTime,
|
|
764
|
-
tokensUsed: usage === null || usage === void 0 ? void 0 : usage.totalTokens,
|
|
765
|
-
userId: userId,
|
|
766
|
-
}))];
|
|
767
|
-
case 17:
|
|
768
|
-
_g.sent();
|
|
769
|
-
return [3 /*break*/, 20];
|
|
770
|
-
case 18:
|
|
771
|
-
error_6 = _g.sent();
|
|
772
|
-
responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
773
|
-
return [4 /*yield*/, __await(this.logRequest({
|
|
774
|
-
aiModel: getModelId(this.model),
|
|
775
|
-
error: error_6 instanceof Error ? error_6.message : String(error_6),
|
|
776
|
-
prompt: promptText,
|
|
777
|
-
requestType: "general",
|
|
778
|
-
responseTime: responseTime,
|
|
779
|
-
userId: userId,
|
|
780
|
-
}))];
|
|
781
|
-
case 19:
|
|
782
|
-
_g.sent();
|
|
783
|
-
throw error_6;
|
|
784
|
-
case 20: return [2 /*return*/];
|
|
785
|
-
}
|
|
431
|
+
}
|
|
432
|
+
async *generateChatStream(options) {
|
|
433
|
+
const { messages, systemPrompt, tools, toolChoice, stopWhen, userId } = options;
|
|
434
|
+
const startTime = luxon_1.DateTime.now().toMillis();
|
|
435
|
+
let fullResponse = "";
|
|
436
|
+
const promptText = messages.map((m) => `${m.role}: ${m.content}`).join("\n");
|
|
437
|
+
try {
|
|
438
|
+
const result = (0, ai_1.streamText)({
|
|
439
|
+
experimental_telemetry: { functionId: "generate-chat-stream", isEnabled: true },
|
|
440
|
+
messages: messages.map((m) => ({ content: m.content, role: m.role })),
|
|
441
|
+
model: this.model,
|
|
442
|
+
stopWhen: stopWhen ?? (0, ai_1.stepCountIs)(1),
|
|
443
|
+
system: systemPrompt ?? prompts_1.DEFAULT_GPT_MEMORY,
|
|
444
|
+
temperature: this.defaultTemperature,
|
|
445
|
+
toolChoice,
|
|
446
|
+
tools,
|
|
786
447
|
});
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
}
|
|
448
|
+
for await (const chunk of result.textStream) {
|
|
449
|
+
fullResponse += chunk;
|
|
450
|
+
yield chunk;
|
|
451
|
+
}
|
|
452
|
+
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
453
|
+
const usage = await result.usage;
|
|
454
|
+
await this.logRequest({
|
|
455
|
+
aiModel: getModelId(this.model),
|
|
456
|
+
prompt: promptText,
|
|
457
|
+
requestType: "general",
|
|
458
|
+
response: fullResponse,
|
|
459
|
+
responseTime,
|
|
460
|
+
tokensUsed: usage?.totalTokens,
|
|
461
|
+
userId,
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
catch (error) {
|
|
465
|
+
const responseTime = luxon_1.DateTime.now().toMillis() - startTime;
|
|
466
|
+
await this.logRequest({
|
|
467
|
+
aiModel: getModelId(this.model),
|
|
468
|
+
error: error instanceof Error ? error.message : String(error),
|
|
469
|
+
prompt: promptText,
|
|
470
|
+
requestType: "general",
|
|
471
|
+
responseTime,
|
|
472
|
+
userId,
|
|
473
|
+
});
|
|
474
|
+
throw error;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
791
478
|
exports.AIService = AIService;
|