drizzle-cube 0.4.18 → 0.4.20
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/dist/adapters/express/index.cjs +6 -6
- package/dist/adapters/express/index.js +75 -74
- package/dist/adapters/fastify/index.cjs +5 -5
- package/dist/adapters/fastify/index.js +91 -90
- package/dist/adapters/{handler-DefTXJpi.js → handler-CbDMdSY5.js} +95 -82
- package/dist/adapters/{handler-hwoGzGex.cjs → handler-DtdjM1Vx.cjs} +18 -14
- package/dist/adapters/hono/index.cjs +6 -6
- package/dist/adapters/hono/index.js +66 -65
- package/dist/adapters/nextjs/index.cjs +5 -5
- package/dist/adapters/nextjs/index.js +116 -115
- package/dist/server/index.cjs +6 -2
- package/dist/server/index.d.ts +12 -0
- package/dist/server/index.js +14 -1
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { NextResponse as t } from "next/server";
|
|
2
2
|
import { e as T, s as A, v as D, b as P, a as _, n as Q, p as V, w as I, d as J, i as M, M as k, c as $, S as K } from "../mcp-transport-m1X1GtwG.js";
|
|
3
3
|
import { formatErrorResponse as i, formatCubeResponse as z, formatMetaResponse as G, formatSqlResponse as U, handleDryRun as q, handleBatchRequest as B, handleDiscover as F, handleSuggest as X, handleValidate as W, handleLoad as Y } from "../utils.js";
|
|
4
|
-
function
|
|
4
|
+
function x(n) {
|
|
5
5
|
const { cubes: a, drizzle: s, schema: c, engineType: d, cache: o } = n;
|
|
6
6
|
if (!a || a.length === 0)
|
|
7
7
|
throw new Error("At least one cube must be provided in the cubes array");
|
|
@@ -15,13 +15,13 @@ function E(n) {
|
|
|
15
15
|
r.registerCube(e);
|
|
16
16
|
}), r;
|
|
17
17
|
}
|
|
18
|
-
function
|
|
18
|
+
function j(n, a) {
|
|
19
19
|
const s = n.headers.get("origin"), c = {};
|
|
20
20
|
return a.origin && (typeof a.origin == "string" ? c["Access-Control-Allow-Origin"] = a.origin : Array.isArray(a.origin) ? s && a.origin.includes(s) && (c["Access-Control-Allow-Origin"] = s) : typeof a.origin == "function" && s && a.origin(s) && (c["Access-Control-Allow-Origin"] = s)), a.methods && (c["Access-Control-Allow-Methods"] = a.methods.join(", ")), a.allowedHeaders && (c["Access-Control-Allow-Headers"] = a.allowedHeaders.join(", ")), a.credentials && (c["Access-Control-Allow-Credentials"] = "true"), c;
|
|
21
21
|
}
|
|
22
22
|
function ue(n) {
|
|
23
23
|
return async function(s) {
|
|
24
|
-
const c =
|
|
24
|
+
const c = j(s, n);
|
|
25
25
|
return new Response(null, {
|
|
26
26
|
status: 200,
|
|
27
27
|
headers: c
|
|
@@ -29,7 +29,7 @@ function ue(n) {
|
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
31
|
function Z(n) {
|
|
32
|
-
const { extractSecurityContext: a, cors: s } = n, c =
|
|
32
|
+
const { extractSecurityContext: a, cors: s } = n, c = x(n);
|
|
33
33
|
return async function(o, r) {
|
|
34
34
|
try {
|
|
35
35
|
let e;
|
|
@@ -56,15 +56,15 @@ function Z(n) {
|
|
|
56
56
|
i("Method not allowed", 405),
|
|
57
57
|
{ status: 405 }
|
|
58
58
|
);
|
|
59
|
-
const u = await a(o, r),
|
|
60
|
-
if (!
|
|
59
|
+
const u = await a(o, r), h = c.validateQuery(e);
|
|
60
|
+
if (!h.isValid)
|
|
61
61
|
return t.json(
|
|
62
|
-
i(`Query validation failed: ${
|
|
62
|
+
i(`Query validation failed: ${h.errors.join(", ")}`, 400),
|
|
63
63
|
{ status: 400 }
|
|
64
64
|
);
|
|
65
|
-
const l = o.headers.get("x-cache-control") === "no-cache",
|
|
66
|
-
return t.json(
|
|
67
|
-
headers: s ?
|
|
65
|
+
const l = o.headers.get("x-cache-control") === "no-cache", y = await c.executeMultiCubeQuery(e, u, { skipCache: l }), S = z(e, y, c);
|
|
66
|
+
return t.json(S, {
|
|
67
|
+
headers: s ? j(o, s) : {}
|
|
68
68
|
});
|
|
69
69
|
} catch (e) {
|
|
70
70
|
return process.env.NODE_ENV !== "test" && console.error("Next.js load handler error:", e), t.json(
|
|
@@ -78,12 +78,12 @@ function Z(n) {
|
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
80
|
function ee(n) {
|
|
81
|
-
const { cors: a } = n, s =
|
|
81
|
+
const { cors: a } = n, s = x(n);
|
|
82
82
|
return async function(d, o) {
|
|
83
83
|
try {
|
|
84
84
|
const r = s.getMetadata(), e = G(r);
|
|
85
85
|
return t.json(e, {
|
|
86
|
-
headers: a ?
|
|
86
|
+
headers: a ? j(d, a) : {}
|
|
87
87
|
});
|
|
88
88
|
} catch (r) {
|
|
89
89
|
return process.env.NODE_ENV !== "test" && console.error("Next.js meta handler error:", r), t.json(
|
|
@@ -97,22 +97,22 @@ function ee(n) {
|
|
|
97
97
|
};
|
|
98
98
|
}
|
|
99
99
|
function te(n) {
|
|
100
|
-
const { extractSecurityContext: a, cors: s } = n, c =
|
|
100
|
+
const { extractSecurityContext: a, cors: s } = n, c = x(n);
|
|
101
101
|
return async function(o, r) {
|
|
102
102
|
try {
|
|
103
103
|
let e;
|
|
104
104
|
if (o.method === "POST") {
|
|
105
|
-
const
|
|
106
|
-
e =
|
|
105
|
+
const E = await o.json();
|
|
106
|
+
e = E.query || E;
|
|
107
107
|
} else if (o.method === "GET") {
|
|
108
|
-
const
|
|
109
|
-
if (!
|
|
108
|
+
const E = o.nextUrl.searchParams.get("query");
|
|
109
|
+
if (!E)
|
|
110
110
|
return t.json(
|
|
111
111
|
i("Query parameter is required", 400),
|
|
112
112
|
{ status: 400 }
|
|
113
113
|
);
|
|
114
114
|
try {
|
|
115
|
-
e = JSON.parse(
|
|
115
|
+
e = JSON.parse(E);
|
|
116
116
|
} catch {
|
|
117
117
|
return t.json(
|
|
118
118
|
i("Invalid JSON in query parameter", 400),
|
|
@@ -124,10 +124,10 @@ function te(n) {
|
|
|
124
124
|
i("Method not allowed", 405),
|
|
125
125
|
{ status: 405 }
|
|
126
126
|
);
|
|
127
|
-
const u = await a(o, r),
|
|
128
|
-
if (!
|
|
127
|
+
const u = await a(o, r), h = c.validateQuery(e);
|
|
128
|
+
if (!h.isValid)
|
|
129
129
|
return t.json(
|
|
130
|
-
i(`Query validation failed: ${
|
|
130
|
+
i(`Query validation failed: ${h.errors.join(", ")}`, 400),
|
|
131
131
|
{ status: 400 }
|
|
132
132
|
);
|
|
133
133
|
const l = e.measures?.[0] || e.dimensions?.[0];
|
|
@@ -136,9 +136,9 @@ function te(n) {
|
|
|
136
136
|
i("No measures or dimensions specified", 400),
|
|
137
137
|
{ status: 400 }
|
|
138
138
|
);
|
|
139
|
-
const
|
|
139
|
+
const y = l.split(".")[0], S = await c.generateSQL(y, e, u), p = U(e, S);
|
|
140
140
|
return t.json(p, {
|
|
141
|
-
headers: s ?
|
|
141
|
+
headers: s ? j(o, s) : {}
|
|
142
142
|
});
|
|
143
143
|
} catch (e) {
|
|
144
144
|
return process.env.NODE_ENV !== "test" && console.error("Next.js SQL handler error:", e), t.json(
|
|
@@ -152,7 +152,7 @@ function te(n) {
|
|
|
152
152
|
};
|
|
153
153
|
}
|
|
154
154
|
function re(n) {
|
|
155
|
-
const { extractSecurityContext: a, cors: s } = n, c =
|
|
155
|
+
const { extractSecurityContext: a, cors: s } = n, c = x(n);
|
|
156
156
|
return async function(o, r) {
|
|
157
157
|
try {
|
|
158
158
|
let e;
|
|
@@ -179,9 +179,9 @@ function re(n) {
|
|
|
179
179
|
{ error: "Method not allowed", valid: !1 },
|
|
180
180
|
{ status: 405 }
|
|
181
181
|
);
|
|
182
|
-
const u = await a(o, r),
|
|
183
|
-
return t.json(
|
|
184
|
-
headers: s ?
|
|
182
|
+
const u = await a(o, r), h = await q(e, u, c);
|
|
183
|
+
return t.json(h, {
|
|
184
|
+
headers: s ? j(o, s) : {}
|
|
185
185
|
});
|
|
186
186
|
} catch (e) {
|
|
187
187
|
return process.env.NODE_ENV !== "test" && console.error("Next.js dry-run handler error:", e), t.json(
|
|
@@ -195,7 +195,7 @@ function re(n) {
|
|
|
195
195
|
};
|
|
196
196
|
}
|
|
197
197
|
function ne(n) {
|
|
198
|
-
const { extractSecurityContext: a, cors: s } = n, c =
|
|
198
|
+
const { extractSecurityContext: a, cors: s } = n, c = x(n);
|
|
199
199
|
return async function(o, r) {
|
|
200
200
|
try {
|
|
201
201
|
if (o.method !== "POST")
|
|
@@ -214,9 +214,9 @@ function ne(n) {
|
|
|
214
214
|
i("Queries array cannot be empty", 400),
|
|
215
215
|
{ status: 400 }
|
|
216
216
|
);
|
|
217
|
-
const
|
|
218
|
-
return t.json(
|
|
219
|
-
headers: s ?
|
|
217
|
+
const h = await a(o, r), l = o.headers.get("x-cache-control") === "no-cache", y = await B(u, h, c, { skipCache: l });
|
|
218
|
+
return t.json(y, {
|
|
219
|
+
headers: s ? j(o, s) : {}
|
|
220
220
|
});
|
|
221
221
|
} catch (e) {
|
|
222
222
|
return process.env.NODE_ENV !== "test" && console.error("Next.js batch handler error:", e), t.json(
|
|
@@ -230,7 +230,7 @@ function ne(n) {
|
|
|
230
230
|
};
|
|
231
231
|
}
|
|
232
232
|
function se(n) {
|
|
233
|
-
const { extractSecurityContext: a, cors: s } = n, c =
|
|
233
|
+
const { extractSecurityContext: a, cors: s } = n, c = x(n);
|
|
234
234
|
return async function(o, r) {
|
|
235
235
|
try {
|
|
236
236
|
if (o.method !== "POST")
|
|
@@ -238,19 +238,19 @@ function se(n) {
|
|
|
238
238
|
{ error: "Method not allowed" },
|
|
239
239
|
{ status: 405 }
|
|
240
240
|
);
|
|
241
|
-
const e = await o.json(), u = e.query || e,
|
|
242
|
-
if (!
|
|
241
|
+
const e = await o.json(), u = e.query || e, h = e.options || {}, l = await a(o, r), y = c.validateQuery(u);
|
|
242
|
+
if (!y.isValid)
|
|
243
243
|
return t.json(
|
|
244
|
-
{ error: `Query validation failed: ${
|
|
244
|
+
{ error: `Query validation failed: ${y.errors.join(", ")}` },
|
|
245
245
|
{ status: 400 }
|
|
246
246
|
);
|
|
247
|
-
const
|
|
247
|
+
const S = await c.explainQuery(
|
|
248
248
|
u,
|
|
249
249
|
l,
|
|
250
|
-
|
|
250
|
+
h
|
|
251
251
|
);
|
|
252
|
-
return t.json(
|
|
253
|
-
headers: s ?
|
|
252
|
+
return t.json(S, {
|
|
253
|
+
headers: s ? j(o, s) : {}
|
|
254
254
|
});
|
|
255
255
|
} catch (e) {
|
|
256
256
|
return process.env.NODE_ENV !== "test" && console.error("Next.js explain handler error:", e), t.json(
|
|
@@ -261,7 +261,7 @@ function se(n) {
|
|
|
261
261
|
};
|
|
262
262
|
}
|
|
263
263
|
function le(n) {
|
|
264
|
-
const { cors: a } = n, s =
|
|
264
|
+
const { cors: a } = n, s = x(n);
|
|
265
265
|
return async function(d, o) {
|
|
266
266
|
try {
|
|
267
267
|
if (d.method !== "POST")
|
|
@@ -271,7 +271,7 @@ function le(n) {
|
|
|
271
271
|
);
|
|
272
272
|
const r = await d.json(), e = await F(s, r);
|
|
273
273
|
return t.json(e, {
|
|
274
|
-
headers: a ?
|
|
274
|
+
headers: a ? j(d, a) : {}
|
|
275
275
|
});
|
|
276
276
|
} catch (r) {
|
|
277
277
|
return process.env.NODE_ENV !== "test" && console.error("Next.js discover handler error:", r), t.json(
|
|
@@ -285,7 +285,7 @@ function le(n) {
|
|
|
285
285
|
};
|
|
286
286
|
}
|
|
287
287
|
function fe(n) {
|
|
288
|
-
const { cors: a } = n, s =
|
|
288
|
+
const { cors: a } = n, s = x(n);
|
|
289
289
|
return async function(d, o) {
|
|
290
290
|
try {
|
|
291
291
|
if (d.method !== "POST")
|
|
@@ -301,7 +301,7 @@ function fe(n) {
|
|
|
301
301
|
);
|
|
302
302
|
const e = await X(s, r);
|
|
303
303
|
return t.json(e, {
|
|
304
|
-
headers: a ?
|
|
304
|
+
headers: a ? j(d, a) : {}
|
|
305
305
|
});
|
|
306
306
|
} catch (r) {
|
|
307
307
|
return process.env.NODE_ENV !== "test" && console.error("Next.js suggest handler error:", r), t.json(
|
|
@@ -315,7 +315,7 @@ function fe(n) {
|
|
|
315
315
|
};
|
|
316
316
|
}
|
|
317
317
|
function ye(n) {
|
|
318
|
-
const { cors: a } = n, s =
|
|
318
|
+
const { cors: a } = n, s = x(n);
|
|
319
319
|
return async function(d, o) {
|
|
320
320
|
try {
|
|
321
321
|
if (d.method !== "POST")
|
|
@@ -331,7 +331,7 @@ function ye(n) {
|
|
|
331
331
|
);
|
|
332
332
|
const e = await W(s, r);
|
|
333
333
|
return t.json(e, {
|
|
334
|
-
headers: a ?
|
|
334
|
+
headers: a ? j(d, a) : {}
|
|
335
335
|
});
|
|
336
336
|
} catch (r) {
|
|
337
337
|
return process.env.NODE_ENV !== "test" && console.error("Next.js validate handler error:", r), t.json(
|
|
@@ -345,7 +345,7 @@ function ye(n) {
|
|
|
345
345
|
};
|
|
346
346
|
}
|
|
347
347
|
function he(n) {
|
|
348
|
-
const { extractSecurityContext: a, cors: s } = n, c =
|
|
348
|
+
const { extractSecurityContext: a, cors: s } = n, c = x(n);
|
|
349
349
|
return async function(o, r) {
|
|
350
350
|
try {
|
|
351
351
|
if (o.method !== "POST")
|
|
@@ -359,9 +359,9 @@ function he(n) {
|
|
|
359
359
|
i("query field is required", 400),
|
|
360
360
|
{ status: 400 }
|
|
361
361
|
);
|
|
362
|
-
const u = await a(o, r),
|
|
363
|
-
return t.json(
|
|
364
|
-
headers: s ?
|
|
362
|
+
const u = await a(o, r), h = await Y(c, u, e);
|
|
363
|
+
return t.json(h, {
|
|
364
|
+
headers: s ? j(o, s) : {}
|
|
365
365
|
});
|
|
366
366
|
} catch (e) {
|
|
367
367
|
return process.env.NODE_ENV !== "test" && console.error("Next.js MCP load handler error:", e), t.json(
|
|
@@ -375,7 +375,7 @@ function he(n) {
|
|
|
375
375
|
};
|
|
376
376
|
}
|
|
377
377
|
function ae(n) {
|
|
378
|
-
const { extractSecurityContext: a, cors: s, mcp: c = { enabled: !0 } } = n, d =
|
|
378
|
+
const { extractSecurityContext: a, cors: s, mcp: c = { enabled: !0 } } = n, d = x(n);
|
|
379
379
|
return async function(r) {
|
|
380
380
|
if (r.method === "DELETE")
|
|
381
381
|
return t.json(
|
|
@@ -383,24 +383,24 @@ function ae(n) {
|
|
|
383
383
|
{ status: 405 }
|
|
384
384
|
);
|
|
385
385
|
if (r.method === "GET") {
|
|
386
|
-
const
|
|
387
|
-
start(
|
|
388
|
-
|
|
386
|
+
const m = new TextEncoder(), v = T(), C = new ReadableStream({
|
|
387
|
+
start(f) {
|
|
388
|
+
f.enqueue(m.encode(A({
|
|
389
389
|
jsonrpc: "2.0",
|
|
390
390
|
method: "mcp/ready",
|
|
391
391
|
params: { protocol: "streamable-http" }
|
|
392
|
-
},
|
|
392
|
+
}, v, 15e3)));
|
|
393
393
|
}
|
|
394
|
-
}),
|
|
394
|
+
}), N = new Headers({
|
|
395
395
|
"Content-Type": "text/event-stream",
|
|
396
396
|
"Cache-Control": "no-cache",
|
|
397
397
|
Connection: "keep-alive"
|
|
398
398
|
});
|
|
399
399
|
if (s) {
|
|
400
|
-
const
|
|
401
|
-
Object.entries(
|
|
400
|
+
const f = j(r, s);
|
|
401
|
+
Object.entries(f).forEach(([g, w]) => N.set(g, w));
|
|
402
402
|
}
|
|
403
|
-
return new t(
|
|
403
|
+
return new t(C, { status: 200, headers: N });
|
|
404
404
|
}
|
|
405
405
|
if (r.method !== "POST")
|
|
406
406
|
return t.json(
|
|
@@ -422,11 +422,11 @@ function ae(n) {
|
|
|
422
422
|
P(null, -32600, "Accept header must include both application/json and text/event-stream"),
|
|
423
423
|
{ status: 400 }
|
|
424
424
|
);
|
|
425
|
-
const
|
|
426
|
-
if (!
|
|
425
|
+
const h = Q(Object.fromEntries(r.headers.entries()));
|
|
426
|
+
if (!h.ok)
|
|
427
427
|
return t.json({
|
|
428
428
|
error: "Unsupported MCP protocol version",
|
|
429
|
-
supported:
|
|
429
|
+
supported: h.supported
|
|
430
430
|
}, { status: 426 });
|
|
431
431
|
let l;
|
|
432
432
|
try {
|
|
@@ -434,63 +434,63 @@ function ae(n) {
|
|
|
434
434
|
} catch {
|
|
435
435
|
l = null;
|
|
436
436
|
}
|
|
437
|
-
const
|
|
438
|
-
if (!
|
|
437
|
+
const y = V(l);
|
|
438
|
+
if (!y)
|
|
439
439
|
return t.json(
|
|
440
440
|
P(null, -32600, "Invalid JSON-RPC 2.0 request"),
|
|
441
441
|
{ status: 400 }
|
|
442
442
|
);
|
|
443
|
-
const
|
|
444
|
-
status:
|
|
445
|
-
headers: { ...s ?
|
|
443
|
+
const S = I(u), p = y.method === "initialize", E = (m, v = 200, C = {}) => t.json(m, {
|
|
444
|
+
status: v,
|
|
445
|
+
headers: { ...s ? j(r, s) : {}, ...C }
|
|
446
446
|
});
|
|
447
447
|
try {
|
|
448
|
-
const
|
|
449
|
-
|
|
450
|
-
|
|
448
|
+
const m = await J(
|
|
449
|
+
y.method,
|
|
450
|
+
y.params,
|
|
451
451
|
{
|
|
452
452
|
semanticLayer: d,
|
|
453
|
-
extractSecurityContext: (
|
|
453
|
+
extractSecurityContext: (f) => a(f),
|
|
454
454
|
rawRequest: r,
|
|
455
455
|
rawResponse: null
|
|
456
456
|
}
|
|
457
457
|
);
|
|
458
|
-
if (M(
|
|
458
|
+
if (M(y))
|
|
459
459
|
return new t(null, { status: 202 });
|
|
460
|
-
const
|
|
461
|
-
|
|
462
|
-
const
|
|
463
|
-
if (
|
|
464
|
-
const
|
|
460
|
+
const v = p && m && typeof m == "object" && "sessionId" in m ? m.sessionId : void 0, C = {};
|
|
461
|
+
v && (C[k] = v);
|
|
462
|
+
const N = $(y.id ?? null, m);
|
|
463
|
+
if (S) {
|
|
464
|
+
const f = new TextEncoder(), g = T(), w = new ReadableStream({
|
|
465
465
|
start(b) {
|
|
466
|
-
b.enqueue(
|
|
466
|
+
b.enqueue(f.encode(`id: ${g}
|
|
467
467
|
|
|
468
|
-
`)), b.enqueue(
|
|
468
|
+
`)), b.enqueue(f.encode(A(N, g))), b.close();
|
|
469
469
|
}
|
|
470
470
|
}), O = new Headers({
|
|
471
471
|
"Content-Type": "text/event-stream",
|
|
472
472
|
"Cache-Control": "no-cache",
|
|
473
473
|
Connection: "keep-alive",
|
|
474
|
-
...
|
|
474
|
+
...C
|
|
475
475
|
});
|
|
476
476
|
if (s) {
|
|
477
|
-
const b =
|
|
477
|
+
const b = j(r, s);
|
|
478
478
|
Object.entries(b).forEach(([H, R]) => O.set(H, R));
|
|
479
479
|
}
|
|
480
|
-
return new t(
|
|
480
|
+
return new t(w, { status: 200, headers: O });
|
|
481
481
|
}
|
|
482
|
-
return
|
|
483
|
-
} catch (
|
|
484
|
-
if (M(
|
|
485
|
-
return process.env.NODE_ENV !== "test" && console.error("Next.js MCP notification processing error:",
|
|
486
|
-
process.env.NODE_ENV !== "test" && console.error("Next.js MCP RPC handler error:",
|
|
487
|
-
const
|
|
488
|
-
if (
|
|
489
|
-
const
|
|
482
|
+
return E(N, 200, C);
|
|
483
|
+
} catch (m) {
|
|
484
|
+
if (M(y))
|
|
485
|
+
return process.env.NODE_ENV !== "test" && console.error("Next.js MCP notification processing error:", m), new t(null, { status: 202 });
|
|
486
|
+
process.env.NODE_ENV !== "test" && console.error("Next.js MCP RPC handler error:", m);
|
|
487
|
+
const v = m?.code ?? -32603, C = m?.data, N = m.message || "MCP request failed", f = P(y.id ?? null, v, N, C);
|
|
488
|
+
if (S) {
|
|
489
|
+
const g = new TextEncoder(), w = T(), O = new ReadableStream({
|
|
490
490
|
start(H) {
|
|
491
|
-
H.enqueue(
|
|
491
|
+
H.enqueue(g.encode(`id: ${w}
|
|
492
492
|
|
|
493
|
-
`)), H.enqueue(
|
|
493
|
+
`)), H.enqueue(g.encode(A(f, w))), H.close();
|
|
494
494
|
}
|
|
495
495
|
}), b = new Headers({
|
|
496
496
|
"Content-Type": "text/event-stream",
|
|
@@ -498,12 +498,12 @@ function ae(n) {
|
|
|
498
498
|
Connection: "keep-alive"
|
|
499
499
|
});
|
|
500
500
|
if (s) {
|
|
501
|
-
const H =
|
|
501
|
+
const H = j(r, s);
|
|
502
502
|
Object.entries(H).forEach(([R, L]) => b.set(R, L));
|
|
503
503
|
}
|
|
504
504
|
return new t(O, { status: 200, headers: b });
|
|
505
505
|
}
|
|
506
|
-
return
|
|
506
|
+
return E(f, 200);
|
|
507
507
|
}
|
|
508
508
|
};
|
|
509
509
|
}
|
|
@@ -511,7 +511,7 @@ function oe(n) {
|
|
|
511
511
|
const { extractSecurityContext: a, cors: s, agent: c } = n;
|
|
512
512
|
if (!c)
|
|
513
513
|
throw new Error("agent config is required for createAgentChatHandler");
|
|
514
|
-
const d =
|
|
514
|
+
const d = x(n);
|
|
515
515
|
return async function(r, e) {
|
|
516
516
|
try {
|
|
517
517
|
if (r.method !== "POST")
|
|
@@ -519,7 +519,7 @@ function oe(n) {
|
|
|
519
519
|
{ error: "Method not allowed - use POST" },
|
|
520
520
|
{ status: 405 }
|
|
521
521
|
);
|
|
522
|
-
const { handleAgentChat: u } = await import("../handler-
|
|
522
|
+
const { handleAgentChat: u } = await import("../handler-CbDMdSY5.js"), h = await r.json(), { message: l, sessionId: y, history: S } = h;
|
|
523
523
|
if (!l || typeof l != "string")
|
|
524
524
|
return t.json(
|
|
525
525
|
{ error: "message is required and must be a string" },
|
|
@@ -527,54 +527,55 @@ function oe(n) {
|
|
|
527
527
|
);
|
|
528
528
|
let p = (c.apiKey || "").trim();
|
|
529
529
|
if (c.allowClientApiKey) {
|
|
530
|
-
const
|
|
531
|
-
|
|
530
|
+
const f = r.headers.get("x-agent-api-key");
|
|
531
|
+
f && (p = f.trim());
|
|
532
532
|
}
|
|
533
533
|
if (!p)
|
|
534
534
|
return t.json(
|
|
535
535
|
{ error: "No API key configured. Set agent.apiKey in server config or send X-Agent-Api-Key header." },
|
|
536
536
|
{ status: 401 }
|
|
537
537
|
);
|
|
538
|
-
const
|
|
539
|
-
async start(
|
|
538
|
+
const E = await a(r, e), m = c.buildSystemContext?.(E), v = new TextEncoder(), C = new ReadableStream({
|
|
539
|
+
async start(f) {
|
|
540
540
|
try {
|
|
541
|
-
const
|
|
541
|
+
const g = u({
|
|
542
542
|
message: l,
|
|
543
|
-
sessionId:
|
|
544
|
-
history:
|
|
543
|
+
sessionId: y,
|
|
544
|
+
history: S,
|
|
545
545
|
semanticLayer: d,
|
|
546
|
-
securityContext:
|
|
546
|
+
securityContext: E,
|
|
547
547
|
agentConfig: c,
|
|
548
|
-
apiKey: p
|
|
548
|
+
apiKey: p,
|
|
549
|
+
systemContext: m
|
|
549
550
|
});
|
|
550
|
-
for await (const w of
|
|
551
|
-
const
|
|
551
|
+
for await (const w of g) {
|
|
552
|
+
const O = `data: ${JSON.stringify(w)}
|
|
552
553
|
|
|
553
554
|
`;
|
|
554
|
-
|
|
555
|
+
f.enqueue(v.encode(O));
|
|
555
556
|
}
|
|
556
|
-
} catch (
|
|
557
|
+
} catch (g) {
|
|
557
558
|
const w = {
|
|
558
559
|
type: "error",
|
|
559
|
-
data: { message:
|
|
560
|
+
data: { message: g instanceof Error ? g.message : "Stream failed" }
|
|
560
561
|
};
|
|
561
|
-
|
|
562
|
+
f.enqueue(v.encode(`data: ${JSON.stringify(w)}
|
|
562
563
|
|
|
563
564
|
`));
|
|
564
565
|
} finally {
|
|
565
|
-
|
|
566
|
+
f.close();
|
|
566
567
|
}
|
|
567
568
|
}
|
|
568
|
-
}),
|
|
569
|
+
}), N = new Headers({
|
|
569
570
|
"Content-Type": "text/event-stream",
|
|
570
571
|
"Cache-Control": "no-cache",
|
|
571
572
|
Connection: "keep-alive"
|
|
572
573
|
});
|
|
573
574
|
if (s) {
|
|
574
|
-
const
|
|
575
|
-
Object.entries(
|
|
575
|
+
const f = j(r, s);
|
|
576
|
+
Object.entries(f).forEach(([g, w]) => N.set(g, w));
|
|
576
577
|
}
|
|
577
|
-
return new Response(
|
|
578
|
+
return new Response(C, { status: 200, headers: N });
|
|
578
579
|
} catch (u) {
|
|
579
580
|
return process.env.NODE_ENV !== "test" && console.error("Next.js agent chat handler error:", u), t.json(
|
|
580
581
|
{ error: u instanceof Error ? u.message : "Agent chat failed" },
|