@terreno/ai 57.4.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 +27 -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 +7 -7
- package/tsconfig.json +6 -21
|
@@ -1,271 +1,100 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
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);
|
|
13
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
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;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __values = (this && this.__values) || function(o) {
|
|
39
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
40
|
-
if (m) return m.call(o);
|
|
41
|
-
if (o && typeof o.length === "number") return {
|
|
42
|
-
next: function () {
|
|
43
|
-
if (o && i >= o.length) o = void 0;
|
|
44
|
-
return { value: o && o[i++], done: !o };
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
48
|
-
};
|
|
49
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
50
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
51
|
-
if (!m) return o;
|
|
52
|
-
var i = m.call(o), r, ar = [], e;
|
|
53
|
-
try {
|
|
54
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
55
|
-
}
|
|
56
|
-
catch (error) { e = { error: error }; }
|
|
57
|
-
finally {
|
|
58
|
-
try {
|
|
59
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
60
|
-
}
|
|
61
|
-
finally { if (e) throw e.error; }
|
|
62
|
-
}
|
|
63
|
-
return ar;
|
|
64
|
-
};
|
|
65
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
66
3
|
exports.MCPService = void 0;
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
for (var servers_1 = __values(servers), servers_1_1 = servers_1.next(); !servers_1_1.done; servers_1_1 = servers_1.next()) {
|
|
74
|
-
var config = servers_1_1.value;
|
|
75
|
-
this.connections.set(config.name, { client: null, config: config, connected: false });
|
|
76
|
-
}
|
|
4
|
+
const mcp_1 = require("@ai-sdk/mcp");
|
|
5
|
+
class MCPService {
|
|
6
|
+
connections = new Map();
|
|
7
|
+
constructor(servers) {
|
|
8
|
+
for (const config of servers) {
|
|
9
|
+
this.connections.set(config.name, { client: null, config, connected: false });
|
|
77
10
|
}
|
|
78
|
-
|
|
79
|
-
|
|
11
|
+
}
|
|
12
|
+
async connect() {
|
|
13
|
+
const connectPromises = Array.from(this.connections.entries()).map(async ([_name, connection]) => {
|
|
80
14
|
try {
|
|
81
|
-
|
|
15
|
+
const client = await (0, mcp_1.createMCPClient)({
|
|
16
|
+
transport: {
|
|
17
|
+
headers: connection.config.transport.headers,
|
|
18
|
+
type: "sse",
|
|
19
|
+
url: connection.config.transport.url,
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
connection.client = client;
|
|
23
|
+
connection.connected = true;
|
|
82
24
|
}
|
|
83
|
-
|
|
84
|
-
|
|
25
|
+
catch {
|
|
26
|
+
connection.connected = false;
|
|
27
|
+
connection.client = null;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
await Promise.allSettled(connectPromises);
|
|
85
31
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
switch (_a.label) {
|
|
92
|
-
case 0:
|
|
93
|
-
connectPromises = Array.from(this.connections.entries()).map(function (_a) { return __awaiter(_this, [_a], void 0, function (_b) {
|
|
94
|
-
var client, _c;
|
|
95
|
-
var _d = __read(_b, 2), _name = _d[0], connection = _d[1];
|
|
96
|
-
return __generator(this, function (_e) {
|
|
97
|
-
switch (_e.label) {
|
|
98
|
-
case 0:
|
|
99
|
-
_e.trys.push([0, 2, , 3]);
|
|
100
|
-
return [4 /*yield*/, (0, mcp_1.createMCPClient)({
|
|
101
|
-
transport: {
|
|
102
|
-
headers: connection.config.transport.headers,
|
|
103
|
-
type: "sse",
|
|
104
|
-
url: connection.config.transport.url,
|
|
105
|
-
},
|
|
106
|
-
})];
|
|
107
|
-
case 1:
|
|
108
|
-
client = _e.sent();
|
|
109
|
-
connection.client = client;
|
|
110
|
-
connection.connected = true;
|
|
111
|
-
return [3 /*break*/, 3];
|
|
112
|
-
case 2:
|
|
113
|
-
_c = _e.sent();
|
|
114
|
-
connection.connected = false;
|
|
115
|
-
connection.client = null;
|
|
116
|
-
return [3 /*break*/, 3];
|
|
117
|
-
case 3: return [2 /*return*/];
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
}); });
|
|
121
|
-
return [4 /*yield*/, Promise.allSettled(connectPromises)];
|
|
122
|
-
case 1:
|
|
123
|
-
_a.sent();
|
|
124
|
-
return [2 /*return*/];
|
|
32
|
+
async disconnect() {
|
|
33
|
+
const disconnectPromises = Array.from(this.connections.values()).map(async (connection) => {
|
|
34
|
+
if (connection.client) {
|
|
35
|
+
try {
|
|
36
|
+
await connection.client.close();
|
|
125
37
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
};
|
|
129
|
-
MCPService.prototype.disconnect = function () {
|
|
130
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
131
|
-
var disconnectPromises;
|
|
132
|
-
var _this = this;
|
|
133
|
-
return __generator(this, function (_a) {
|
|
134
|
-
switch (_a.label) {
|
|
135
|
-
case 0:
|
|
136
|
-
disconnectPromises = Array.from(this.connections.values()).map(function (connection) { return __awaiter(_this, void 0, void 0, function () {
|
|
137
|
-
var _a;
|
|
138
|
-
return __generator(this, function (_b) {
|
|
139
|
-
switch (_b.label) {
|
|
140
|
-
case 0:
|
|
141
|
-
if (!connection.client) return [3 /*break*/, 5];
|
|
142
|
-
_b.label = 1;
|
|
143
|
-
case 1:
|
|
144
|
-
_b.trys.push([1, 3, , 4]);
|
|
145
|
-
return [4 /*yield*/, connection.client.close()];
|
|
146
|
-
case 2:
|
|
147
|
-
_b.sent();
|
|
148
|
-
return [3 /*break*/, 4];
|
|
149
|
-
case 3:
|
|
150
|
-
_a = _b.sent();
|
|
151
|
-
return [3 /*break*/, 4];
|
|
152
|
-
case 4:
|
|
153
|
-
connection.client = null;
|
|
154
|
-
connection.connected = false;
|
|
155
|
-
_b.label = 5;
|
|
156
|
-
case 5: return [2 /*return*/];
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
}); });
|
|
160
|
-
return [4 /*yield*/, Promise.allSettled(disconnectPromises)];
|
|
161
|
-
case 1:
|
|
162
|
-
_a.sent();
|
|
163
|
-
return [2 /*return*/];
|
|
38
|
+
catch {
|
|
39
|
+
// Ignore disconnect errors
|
|
164
40
|
}
|
|
165
|
-
|
|
41
|
+
connection.client = null;
|
|
42
|
+
connection.connected = false;
|
|
43
|
+
}
|
|
166
44
|
});
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
case 4:
|
|
190
|
-
_c.trys.push([4, 6, , 7]);
|
|
191
|
-
return [4 /*yield*/, (0, mcp_1.createMCPClient)({
|
|
192
|
-
transport: {
|
|
193
|
-
headers: connection.config.transport.headers,
|
|
194
|
-
type: "sse",
|
|
195
|
-
url: connection.config.transport.url,
|
|
196
|
-
},
|
|
197
|
-
})];
|
|
198
|
-
case 5:
|
|
199
|
-
client = _c.sent();
|
|
200
|
-
connection.client = client;
|
|
201
|
-
connection.connected = true;
|
|
202
|
-
return [2 /*return*/, true];
|
|
203
|
-
case 6:
|
|
204
|
-
_b = _c.sent();
|
|
205
|
-
connection.connected = false;
|
|
206
|
-
connection.client = null;
|
|
207
|
-
return [2 /*return*/, false];
|
|
208
|
-
case 7: return [2 /*return*/];
|
|
209
|
-
}
|
|
45
|
+
await Promise.allSettled(disconnectPromises);
|
|
46
|
+
}
|
|
47
|
+
async reconnectServer(name) {
|
|
48
|
+
const connection = this.connections.get(name);
|
|
49
|
+
if (!connection) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
if (connection.client) {
|
|
53
|
+
try {
|
|
54
|
+
await connection.client.close();
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// Ignore
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
const client = await (0, mcp_1.createMCPClient)({
|
|
62
|
+
transport: {
|
|
63
|
+
headers: connection.config.transport.headers,
|
|
64
|
+
type: "sse",
|
|
65
|
+
url: connection.config.transport.url,
|
|
66
|
+
},
|
|
210
67
|
});
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
_c = __read(_b.value, 2), connection = _c[1];
|
|
229
|
-
if (!(connection.connected && connection.client)) return [3 /*break*/, 6];
|
|
230
|
-
_f.label = 3;
|
|
231
|
-
case 3:
|
|
232
|
-
_f.trys.push([3, 5, , 6]);
|
|
233
|
-
return [4 /*yield*/, connection.client.tools()];
|
|
234
|
-
case 4:
|
|
235
|
-
tools = _f.sent();
|
|
236
|
-
Object.assign(allTools, tools);
|
|
237
|
-
return [3 /*break*/, 6];
|
|
238
|
-
case 5:
|
|
239
|
-
_d = _f.sent();
|
|
240
|
-
return [3 /*break*/, 6];
|
|
241
|
-
case 6:
|
|
242
|
-
_b = _a.next();
|
|
243
|
-
return [3 /*break*/, 2];
|
|
244
|
-
case 7: return [3 /*break*/, 10];
|
|
245
|
-
case 8:
|
|
246
|
-
e_2_1 = _f.sent();
|
|
247
|
-
e_2 = { error: e_2_1 };
|
|
248
|
-
return [3 /*break*/, 10];
|
|
249
|
-
case 9:
|
|
250
|
-
try {
|
|
251
|
-
if (_b && !_b.done && (_e = _a.return)) _e.call(_a);
|
|
252
|
-
}
|
|
253
|
-
finally { if (e_2) throw e_2.error; }
|
|
254
|
-
return [7 /*endfinally*/];
|
|
255
|
-
case 10: return [2 /*return*/, allTools];
|
|
68
|
+
connection.client = client;
|
|
69
|
+
connection.connected = true;
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
connection.connected = false;
|
|
74
|
+
connection.client = null;
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async getTools() {
|
|
79
|
+
const allTools = {};
|
|
80
|
+
for (const [, connection] of this.connections) {
|
|
81
|
+
if (connection.connected && connection.client) {
|
|
82
|
+
try {
|
|
83
|
+
const tools = await connection.client.tools();
|
|
84
|
+
Object.assign(allTools, tools);
|
|
256
85
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
}
|
|
86
|
+
catch {
|
|
87
|
+
// Skip tools from failed servers
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return allTools;
|
|
92
|
+
}
|
|
93
|
+
getServerStatus() {
|
|
94
|
+
return Array.from(this.connections.entries()).map(([name, connection]) => ({
|
|
95
|
+
connected: connection.connected,
|
|
96
|
+
name,
|
|
97
|
+
}));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
271
100
|
exports.MCPService = MCPService;
|
|
@@ -17,13 +17,13 @@ exports.normalizeLlmJsonTextForStructuredOutput = exports.parseAiJson = void 0;
|
|
|
17
17
|
* Strip leading/trailing markdown code fences when the whole payload is fenced,
|
|
18
18
|
* otherwise strip a common opening ```lang line and trailing ``` block.
|
|
19
19
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
const stripFences = (input) => {
|
|
21
|
+
const trimmed = input.trim();
|
|
22
|
+
const fullyFenced = /^```(?:json|javascript|js|typescript|ts)?\s*\n?([\s\S]*?)\n?```$/i.exec(trimmed);
|
|
23
23
|
if (fullyFenced) {
|
|
24
24
|
return fullyFenced[1].trim();
|
|
25
25
|
}
|
|
26
|
-
|
|
26
|
+
let t = trimmed;
|
|
27
27
|
t = t.replace(/^```[a-zA-Z0-9_-]*\s*:?\s*\n?/, "");
|
|
28
28
|
t = t.replace(/\n?```\s*$/m, "").trim();
|
|
29
29
|
return t;
|
|
@@ -32,16 +32,16 @@ var stripFences = function (input) {
|
|
|
32
32
|
* Returns the first balanced JSON object or array substring, respecting string literals
|
|
33
33
|
* and escapes. Uses a bracket stack so nested `{}` / `[]` mix correctly.
|
|
34
34
|
*/
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
const extractBalancedJson = (input) => {
|
|
36
|
+
const start = input.search(/[{[]/);
|
|
37
37
|
if (start === -1) {
|
|
38
38
|
return null;
|
|
39
39
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
for (
|
|
44
|
-
|
|
40
|
+
const stack = [];
|
|
41
|
+
let inString = false;
|
|
42
|
+
let escaped = false;
|
|
43
|
+
for (let i = start; i < input.length; i++) {
|
|
44
|
+
const char = input[i];
|
|
45
45
|
if (escaped) {
|
|
46
46
|
escaped = false;
|
|
47
47
|
continue;
|
|
@@ -66,7 +66,7 @@ var extractBalancedJson = function (input) {
|
|
|
66
66
|
continue;
|
|
67
67
|
}
|
|
68
68
|
if (char === "}" || char === "]") {
|
|
69
|
-
|
|
69
|
+
const expected = stack.pop();
|
|
70
70
|
if (expected !== char) {
|
|
71
71
|
return null;
|
|
72
72
|
}
|
|
@@ -81,12 +81,12 @@ var extractBalancedJson = function (input) {
|
|
|
81
81
|
* Light-touch repairs for common malformations. Only changes characters outside
|
|
82
82
|
* string literals so legitimate string content is preserved.
|
|
83
83
|
*/
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
for (
|
|
89
|
-
|
|
84
|
+
const repairJsonishOutsideStrings = (input) => {
|
|
85
|
+
let result = "";
|
|
86
|
+
let inString = false;
|
|
87
|
+
let escaped = false;
|
|
88
|
+
for (let i = 0; i < input.length; i++) {
|
|
89
|
+
const char = input[i];
|
|
90
90
|
if (escaped) {
|
|
91
91
|
result += char;
|
|
92
92
|
escaped = false;
|
|
@@ -108,8 +108,8 @@ var repairJsonishOutsideStrings = function (input) {
|
|
|
108
108
|
continue;
|
|
109
109
|
}
|
|
110
110
|
if (char === ",") {
|
|
111
|
-
|
|
112
|
-
|
|
111
|
+
const rest = input.slice(i + 1);
|
|
112
|
+
const next = rest.match(/^\s*([}\]])/);
|
|
113
113
|
if (next) {
|
|
114
114
|
continue;
|
|
115
115
|
}
|
|
@@ -119,42 +119,42 @@ var repairJsonishOutsideStrings = function (input) {
|
|
|
119
119
|
}
|
|
120
120
|
return result;
|
|
121
121
|
};
|
|
122
|
-
|
|
122
|
+
const tryParse = (candidate) => {
|
|
123
123
|
try {
|
|
124
124
|
return JSON.parse(candidate);
|
|
125
125
|
}
|
|
126
|
-
catch
|
|
126
|
+
catch {
|
|
127
127
|
return undefined;
|
|
128
128
|
}
|
|
129
129
|
};
|
|
130
130
|
/**
|
|
131
131
|
* Extract and parse JSON from arbitrary LLM output.
|
|
132
132
|
*/
|
|
133
|
-
|
|
133
|
+
const parseAiJson = (input) => {
|
|
134
134
|
if (typeof input !== "string" || input.trim().length === 0) {
|
|
135
135
|
return { error: "Empty or non-string input", raw: input, success: false };
|
|
136
136
|
}
|
|
137
|
-
|
|
138
|
-
|
|
137
|
+
const cleaned = stripFences(input);
|
|
138
|
+
const direct = tryParse(cleaned);
|
|
139
139
|
if (direct !== undefined) {
|
|
140
140
|
return { data: direct, repaired: false, success: true };
|
|
141
141
|
}
|
|
142
|
-
|
|
142
|
+
const balanced = extractBalancedJson(cleaned);
|
|
143
143
|
if (balanced !== null) {
|
|
144
|
-
|
|
144
|
+
const extracted = tryParse(balanced);
|
|
145
145
|
if (extracted !== undefined) {
|
|
146
146
|
return { data: extracted, repaired: true, success: true };
|
|
147
147
|
}
|
|
148
|
-
|
|
148
|
+
const repairedSlice = tryParse(repairJsonishOutsideStrings(balanced));
|
|
149
149
|
if (repairedSlice !== undefined) {
|
|
150
150
|
return { data: repairedSlice, repaired: true, success: true };
|
|
151
151
|
}
|
|
152
152
|
}
|
|
153
|
-
|
|
153
|
+
const repairedWhole = tryParse(repairJsonishOutsideStrings(cleaned));
|
|
154
154
|
if (repairedWhole !== undefined) {
|
|
155
155
|
return { data: repairedWhole, repaired: true, success: true };
|
|
156
156
|
}
|
|
157
|
-
|
|
157
|
+
const aggressiveQuotes = tryParse(cleaned.replace(/[\u201C\u201D]/g, '"'));
|
|
158
158
|
if (aggressiveQuotes !== undefined) {
|
|
159
159
|
return { data: aggressiveQuotes, repaired: true, success: true };
|
|
160
160
|
}
|
|
@@ -170,8 +170,8 @@ exports.parseAiJson = parseAiJson;
|
|
|
170
170
|
* On success returns `JSON.stringify` of the parsed value; on failure returns `cleaned`
|
|
171
171
|
* (fence-stripped) text so the SDK can still attempt its own error path.
|
|
172
172
|
*/
|
|
173
|
-
|
|
174
|
-
|
|
173
|
+
const normalizeLlmJsonTextForStructuredOutput = (raw) => {
|
|
174
|
+
const parsed = (0, exports.parseAiJson)(raw);
|
|
175
175
|
if (parsed.success) {
|
|
176
176
|
return JSON.stringify(parsed.data);
|
|
177
177
|
}
|