@jeffusion/bungee-core 6.12.0 → 6.13.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/dist/config-storage-worker.js +142 -7
- package/dist/gateway/plugin.js +3 -1
- package/dist/main.js +3290 -2992
- package/dist/observability-worker.js +948 -175
- package/dist/plugins/anthropic-request-sanitizer/index.js.map +2 -2
- package/dist/plugins/anthropic-tool-name-transformer/index.js.map +2 -2
- package/dist/plugins/chatgpt-oauth/index.js.map +2 -2
- package/dist/plugins/codex-router/index.js.map +2 -2
- package/dist/plugins/deepseek-reasoning-fix/index.js.map +2 -2
- package/dist/plugins/key-access/index.js.map +2 -2
- package/dist/plugins/key-rate-limit/index.js.map +2 -2
- package/dist/plugins/llm-protocol-adapter/index.js +4 -2
- package/dist/plugins/llm-protocol-adapter/index.js.map +4 -4
- package/dist/plugins/llm-tps/index.js.map +2 -2
- package/dist/plugins/model-mapping/index.js.map +2 -2
- package/dist/plugins/models-dev/index.js.map +2 -2
- package/dist/plugins/request-logs/index.js +1392 -0
- package/dist/plugins/request-logs/index.js.map +33 -0
- package/dist/plugins/request-logs/ingress.js +28 -0
- package/dist/plugins/request-logs/ingress.js.map +10 -0
- package/dist/plugins/request-logs/manifest.json +68 -0
- package/dist/plugins/request-logs/ui/RequestLogsPage.svelte +1130 -0
- package/dist/plugins/request-logs/ui/RequestLogsSettings.svelte +111 -0
- package/dist/plugins/signature-repair/index.js.map +2 -2
- package/dist/plugins/token-budget/index.js.map +2 -2
- package/dist/plugins/token-metering/index.js.map +2 -2
- package/dist/plugins/token-stats/index.js.map +2 -2
- package/dist/plugins/user-auth/index.js.map +2 -2
- package/package.json +2 -2
|
@@ -0,0 +1,1392 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
function __accessProp(key) {
|
|
8
|
+
return this[key];
|
|
9
|
+
}
|
|
10
|
+
var __toESMCache_node;
|
|
11
|
+
var __toESMCache_esm;
|
|
12
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
13
|
+
var canCache = mod != null && typeof mod === "object";
|
|
14
|
+
if (canCache) {
|
|
15
|
+
var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
|
|
16
|
+
var cached = cache.get(mod);
|
|
17
|
+
if (cached)
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
20
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
21
|
+
const to = isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
22
|
+
if (mod && typeof mod === "object" || typeof mod === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(mod))
|
|
24
|
+
if (!__hasOwnProp.call(to, key))
|
|
25
|
+
__defProp(to, key, {
|
|
26
|
+
get: __accessProp.bind(mod, key),
|
|
27
|
+
enumerable: true
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if (canCache)
|
|
31
|
+
cache.set(mod, to);
|
|
32
|
+
return to;
|
|
33
|
+
};
|
|
34
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
35
|
+
var __require = import.meta.require;
|
|
36
|
+
|
|
37
|
+
// node_modules/stream-json/utils/Utf8Stream.js
|
|
38
|
+
var require_Utf8Stream = __commonJS(function(exports, module) {
|
|
39
|
+
var { Transform } = __require("stream");
|
|
40
|
+
var { StringDecoder } = __require("string_decoder");
|
|
41
|
+
|
|
42
|
+
class Utf8Stream extends Transform {
|
|
43
|
+
constructor(options) {
|
|
44
|
+
super(Object.assign({}, options, { writableObjectMode: false }));
|
|
45
|
+
this._buffer = "";
|
|
46
|
+
}
|
|
47
|
+
_transform(chunk, encoding, callback) {
|
|
48
|
+
if (typeof chunk == "string") {
|
|
49
|
+
this._transform = this._transformString;
|
|
50
|
+
} else {
|
|
51
|
+
this._stringDecoder = new StringDecoder;
|
|
52
|
+
this._transform = this._transformBuffer;
|
|
53
|
+
}
|
|
54
|
+
this._transform(chunk, encoding, callback);
|
|
55
|
+
}
|
|
56
|
+
_transformBuffer(chunk, _, callback) {
|
|
57
|
+
this._buffer += this._stringDecoder.write(chunk);
|
|
58
|
+
this._processBuffer(callback);
|
|
59
|
+
}
|
|
60
|
+
_transformString(chunk, _, callback) {
|
|
61
|
+
this._buffer += chunk.toString();
|
|
62
|
+
this._processBuffer(callback);
|
|
63
|
+
}
|
|
64
|
+
_processBuffer(callback) {
|
|
65
|
+
if (this._buffer) {
|
|
66
|
+
this.push(this._buffer, "utf8");
|
|
67
|
+
this._buffer = "";
|
|
68
|
+
}
|
|
69
|
+
callback(null);
|
|
70
|
+
}
|
|
71
|
+
_flushInput() {
|
|
72
|
+
if (this._stringDecoder) {
|
|
73
|
+
this._buffer += this._stringDecoder.end();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
_flush(callback) {
|
|
77
|
+
this._flushInput();
|
|
78
|
+
this._processBuffer(callback);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
module.exports = Utf8Stream;
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// node_modules/stream-json/Parser.js
|
|
85
|
+
var require_Parser = __commonJS(function(exports, module) {
|
|
86
|
+
var Utf8Stream = require_Utf8Stream();
|
|
87
|
+
var patterns = {
|
|
88
|
+
value1: /^(?:[\"\{\[\]\-\d]|true\b|false\b|null\b|\s{1,256})/,
|
|
89
|
+
string: /^(?:[^\x00-\x1f\"\\]{1,256}|\\[bfnrt\"\\\/]|\\u[\da-fA-F]{4}|\")/,
|
|
90
|
+
key1: /^(?:[\"\}]|\s{1,256})/,
|
|
91
|
+
colon: /^(?:\:|\s{1,256})/,
|
|
92
|
+
comma: /^(?:[\,\]\}]|\s{1,256})/,
|
|
93
|
+
ws: /^\s{1,256}/,
|
|
94
|
+
numberStart: /^\d/,
|
|
95
|
+
numberDigit: /^\d{0,256}/,
|
|
96
|
+
numberFraction: /^[\.eE]/,
|
|
97
|
+
numberExponent: /^[eE]/,
|
|
98
|
+
numberExpSign: /^[-+]/
|
|
99
|
+
};
|
|
100
|
+
var MAX_PATTERN_SIZE = 16;
|
|
101
|
+
var noSticky = true;
|
|
102
|
+
try {
|
|
103
|
+
new RegExp(".", "y");
|
|
104
|
+
noSticky = false;
|
|
105
|
+
} catch (e) {}
|
|
106
|
+
!noSticky && Object.keys(patterns).forEach((key) => {
|
|
107
|
+
let src = patterns[key].source.slice(1);
|
|
108
|
+
if (src.slice(0, 3) === "(?:" && src.slice(-1) === ")") {
|
|
109
|
+
src = src.slice(3, -1);
|
|
110
|
+
}
|
|
111
|
+
patterns[key] = new RegExp(src, "y");
|
|
112
|
+
});
|
|
113
|
+
patterns.numberFracStart = patterns.numberExpStart = patterns.numberStart;
|
|
114
|
+
patterns.numberFracDigit = patterns.numberExpDigit = patterns.numberDigit;
|
|
115
|
+
var values = { true: true, false: false, null: null };
|
|
116
|
+
var expected = { object: "objectStop", array: "arrayStop", "": "done" };
|
|
117
|
+
var fromHex = (s) => String.fromCharCode(parseInt(s.slice(2), 16));
|
|
118
|
+
var codes = { b: "\b", f: "\f", n: `
|
|
119
|
+
`, r: "\r", t: "\t", '"': '"', "\\": "\\", "/": "/" };
|
|
120
|
+
|
|
121
|
+
class Parser extends Utf8Stream {
|
|
122
|
+
static make(options) {
|
|
123
|
+
return new Parser(options);
|
|
124
|
+
}
|
|
125
|
+
constructor(options) {
|
|
126
|
+
super(Object.assign({}, options, { readableObjectMode: true }));
|
|
127
|
+
this._packKeys = this._packStrings = this._packNumbers = this._streamKeys = this._streamStrings = this._streamNumbers = true;
|
|
128
|
+
if (options) {
|
|
129
|
+
"packValues" in options && (this._packKeys = this._packStrings = this._packNumbers = options.packValues);
|
|
130
|
+
"packKeys" in options && (this._packKeys = options.packKeys);
|
|
131
|
+
"packStrings" in options && (this._packStrings = options.packStrings);
|
|
132
|
+
"packNumbers" in options && (this._packNumbers = options.packNumbers);
|
|
133
|
+
"streamValues" in options && (this._streamKeys = this._streamStrings = this._streamNumbers = options.streamValues);
|
|
134
|
+
"streamKeys" in options && (this._streamKeys = options.streamKeys);
|
|
135
|
+
"streamStrings" in options && (this._streamStrings = options.streamStrings);
|
|
136
|
+
"streamNumbers" in options && (this._streamNumbers = options.streamNumbers);
|
|
137
|
+
this._jsonStreaming = options.jsonStreaming;
|
|
138
|
+
}
|
|
139
|
+
!this._packKeys && (this._streamKeys = true);
|
|
140
|
+
!this._packStrings && (this._streamStrings = true);
|
|
141
|
+
!this._packNumbers && (this._streamNumbers = true);
|
|
142
|
+
this._done = false;
|
|
143
|
+
this._expect = this._jsonStreaming ? "done" : "value";
|
|
144
|
+
this._stack = [];
|
|
145
|
+
this._parent = "";
|
|
146
|
+
this._open_number = false;
|
|
147
|
+
this._accumulator = "";
|
|
148
|
+
}
|
|
149
|
+
_flush(callback) {
|
|
150
|
+
this._done = true;
|
|
151
|
+
super._flush((error) => {
|
|
152
|
+
if (error)
|
|
153
|
+
return callback(error);
|
|
154
|
+
if (this._open_number) {
|
|
155
|
+
if (this._streamNumbers) {
|
|
156
|
+
this.push({ name: "endNumber" });
|
|
157
|
+
}
|
|
158
|
+
this._open_number = false;
|
|
159
|
+
if (this._packNumbers) {
|
|
160
|
+
this.push({ name: "numberValue", value: this._accumulator });
|
|
161
|
+
this._accumulator = "";
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
callback(null);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
_processBuffer(callback) {
|
|
168
|
+
let match, value, index = 0;
|
|
169
|
+
main:
|
|
170
|
+
for (;; ) {
|
|
171
|
+
switch (this._expect) {
|
|
172
|
+
case "value1":
|
|
173
|
+
case "value":
|
|
174
|
+
patterns.value1.lastIndex = index;
|
|
175
|
+
match = patterns.value1.exec(this._buffer);
|
|
176
|
+
if (!match) {
|
|
177
|
+
if (this._done || index + MAX_PATTERN_SIZE < this._buffer.length) {
|
|
178
|
+
if (index < this._buffer.length)
|
|
179
|
+
return callback(new Error("Parser cannot parse input: expected a value"));
|
|
180
|
+
return callback(new Error("Parser has expected a value"));
|
|
181
|
+
}
|
|
182
|
+
break main;
|
|
183
|
+
}
|
|
184
|
+
value = match[0];
|
|
185
|
+
switch (value) {
|
|
186
|
+
case '"':
|
|
187
|
+
this._streamStrings && this.push({ name: "startString" });
|
|
188
|
+
this._expect = "string";
|
|
189
|
+
break;
|
|
190
|
+
case "{":
|
|
191
|
+
this.push({ name: "startObject" });
|
|
192
|
+
this._stack.push(this._parent);
|
|
193
|
+
this._parent = "object";
|
|
194
|
+
this._expect = "key1";
|
|
195
|
+
break;
|
|
196
|
+
case "[":
|
|
197
|
+
this.push({ name: "startArray" });
|
|
198
|
+
this._stack.push(this._parent);
|
|
199
|
+
this._parent = "array";
|
|
200
|
+
this._expect = "value1";
|
|
201
|
+
break;
|
|
202
|
+
case "]":
|
|
203
|
+
if (this._expect !== "value1")
|
|
204
|
+
return callback(new Error("Parser cannot parse input: unexpected token ']'"));
|
|
205
|
+
if (this._open_number) {
|
|
206
|
+
this._streamNumbers && this.push({ name: "endNumber" });
|
|
207
|
+
this._open_number = false;
|
|
208
|
+
if (this._packNumbers) {
|
|
209
|
+
this.push({ name: "numberValue", value: this._accumulator });
|
|
210
|
+
this._accumulator = "";
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
this.push({ name: "endArray" });
|
|
214
|
+
this._parent = this._stack.pop();
|
|
215
|
+
this._expect = expected[this._parent];
|
|
216
|
+
break;
|
|
217
|
+
case "-":
|
|
218
|
+
this._open_number = true;
|
|
219
|
+
if (this._streamNumbers) {
|
|
220
|
+
this.push({ name: "startNumber" });
|
|
221
|
+
this.push({ name: "numberChunk", value: "-" });
|
|
222
|
+
}
|
|
223
|
+
this._packNumbers && (this._accumulator = "-");
|
|
224
|
+
this._expect = "numberStart";
|
|
225
|
+
break;
|
|
226
|
+
case "0":
|
|
227
|
+
this._open_number = true;
|
|
228
|
+
if (this._streamNumbers) {
|
|
229
|
+
this.push({ name: "startNumber" });
|
|
230
|
+
this.push({ name: "numberChunk", value: "0" });
|
|
231
|
+
}
|
|
232
|
+
this._packNumbers && (this._accumulator = "0");
|
|
233
|
+
this._expect = "numberFraction";
|
|
234
|
+
break;
|
|
235
|
+
case "1":
|
|
236
|
+
case "2":
|
|
237
|
+
case "3":
|
|
238
|
+
case "4":
|
|
239
|
+
case "5":
|
|
240
|
+
case "6":
|
|
241
|
+
case "7":
|
|
242
|
+
case "8":
|
|
243
|
+
case "9":
|
|
244
|
+
this._open_number = true;
|
|
245
|
+
if (this._streamNumbers) {
|
|
246
|
+
this.push({ name: "startNumber" });
|
|
247
|
+
this.push({ name: "numberChunk", value });
|
|
248
|
+
}
|
|
249
|
+
this._packNumbers && (this._accumulator = value);
|
|
250
|
+
this._expect = "numberDigit";
|
|
251
|
+
break;
|
|
252
|
+
case "true":
|
|
253
|
+
case "false":
|
|
254
|
+
case "null":
|
|
255
|
+
if (this._buffer.length - index === value.length && !this._done)
|
|
256
|
+
break main;
|
|
257
|
+
this.push({ name: value + "Value", value: values[value] });
|
|
258
|
+
this._expect = expected[this._parent];
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
if (noSticky) {
|
|
262
|
+
this._buffer = this._buffer.slice(value.length);
|
|
263
|
+
} else {
|
|
264
|
+
index += value.length;
|
|
265
|
+
}
|
|
266
|
+
break;
|
|
267
|
+
case "keyVal":
|
|
268
|
+
case "string":
|
|
269
|
+
patterns.string.lastIndex = index;
|
|
270
|
+
match = patterns.string.exec(this._buffer);
|
|
271
|
+
if (!match) {
|
|
272
|
+
if (index < this._buffer.length && (this._done || this._buffer.length - index >= 6))
|
|
273
|
+
return callback(new Error("Parser cannot parse input: escaped characters"));
|
|
274
|
+
if (this._done)
|
|
275
|
+
return callback(new Error("Parser has expected a string value"));
|
|
276
|
+
break main;
|
|
277
|
+
}
|
|
278
|
+
value = match[0];
|
|
279
|
+
if (value === '"') {
|
|
280
|
+
if (this._expect === "keyVal") {
|
|
281
|
+
this._streamKeys && this.push({ name: "endKey" });
|
|
282
|
+
if (this._packKeys) {
|
|
283
|
+
this.push({ name: "keyValue", value: this._accumulator });
|
|
284
|
+
this._accumulator = "";
|
|
285
|
+
}
|
|
286
|
+
this._expect = "colon";
|
|
287
|
+
} else {
|
|
288
|
+
this._streamStrings && this.push({ name: "endString" });
|
|
289
|
+
if (this._packStrings) {
|
|
290
|
+
this.push({ name: "stringValue", value: this._accumulator });
|
|
291
|
+
this._accumulator = "";
|
|
292
|
+
}
|
|
293
|
+
this._expect = expected[this._parent];
|
|
294
|
+
}
|
|
295
|
+
} else if (value.length > 1 && value.charAt(0) === "\\") {
|
|
296
|
+
const t = value.length == 2 ? codes[value.charAt(1)] : fromHex(value);
|
|
297
|
+
if (this._expect === "keyVal" ? this._streamKeys : this._streamStrings) {
|
|
298
|
+
this.push({ name: "stringChunk", value: t });
|
|
299
|
+
}
|
|
300
|
+
if (this._expect === "keyVal" ? this._packKeys : this._packStrings) {
|
|
301
|
+
this._accumulator += t;
|
|
302
|
+
}
|
|
303
|
+
} else {
|
|
304
|
+
if (this._expect === "keyVal" ? this._streamKeys : this._streamStrings) {
|
|
305
|
+
this.push({ name: "stringChunk", value });
|
|
306
|
+
}
|
|
307
|
+
if (this._expect === "keyVal" ? this._packKeys : this._packStrings) {
|
|
308
|
+
this._accumulator += value;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (noSticky) {
|
|
312
|
+
this._buffer = this._buffer.slice(value.length);
|
|
313
|
+
} else {
|
|
314
|
+
index += value.length;
|
|
315
|
+
}
|
|
316
|
+
break;
|
|
317
|
+
case "key1":
|
|
318
|
+
case "key":
|
|
319
|
+
patterns.key1.lastIndex = index;
|
|
320
|
+
match = patterns.key1.exec(this._buffer);
|
|
321
|
+
if (!match) {
|
|
322
|
+
if (index < this._buffer.length || this._done)
|
|
323
|
+
return callback(new Error("Parser cannot parse input: expected an object key"));
|
|
324
|
+
break main;
|
|
325
|
+
}
|
|
326
|
+
value = match[0];
|
|
327
|
+
if (value === '"') {
|
|
328
|
+
this._streamKeys && this.push({ name: "startKey" });
|
|
329
|
+
this._expect = "keyVal";
|
|
330
|
+
} else if (value === "}") {
|
|
331
|
+
if (this._expect !== "key1")
|
|
332
|
+
return callback(new Error("Parser cannot parse input: unexpected token '}'"));
|
|
333
|
+
this.push({ name: "endObject" });
|
|
334
|
+
this._parent = this._stack.pop();
|
|
335
|
+
this._expect = expected[this._parent];
|
|
336
|
+
}
|
|
337
|
+
if (noSticky) {
|
|
338
|
+
this._buffer = this._buffer.slice(value.length);
|
|
339
|
+
} else {
|
|
340
|
+
index += value.length;
|
|
341
|
+
}
|
|
342
|
+
break;
|
|
343
|
+
case "colon":
|
|
344
|
+
patterns.colon.lastIndex = index;
|
|
345
|
+
match = patterns.colon.exec(this._buffer);
|
|
346
|
+
if (!match) {
|
|
347
|
+
if (index < this._buffer.length || this._done)
|
|
348
|
+
return callback(new Error("Parser cannot parse input: expected ':'"));
|
|
349
|
+
break main;
|
|
350
|
+
}
|
|
351
|
+
value = match[0];
|
|
352
|
+
value === ":" && (this._expect = "value");
|
|
353
|
+
if (noSticky) {
|
|
354
|
+
this._buffer = this._buffer.slice(value.length);
|
|
355
|
+
} else {
|
|
356
|
+
index += value.length;
|
|
357
|
+
}
|
|
358
|
+
break;
|
|
359
|
+
case "arrayStop":
|
|
360
|
+
case "objectStop":
|
|
361
|
+
patterns.comma.lastIndex = index;
|
|
362
|
+
match = patterns.comma.exec(this._buffer);
|
|
363
|
+
if (!match) {
|
|
364
|
+
if (index < this._buffer.length || this._done)
|
|
365
|
+
return callback(new Error("Parser cannot parse input: expected ','"));
|
|
366
|
+
break main;
|
|
367
|
+
}
|
|
368
|
+
if (this._open_number) {
|
|
369
|
+
this._streamNumbers && this.push({ name: "endNumber" });
|
|
370
|
+
this._open_number = false;
|
|
371
|
+
if (this._packNumbers) {
|
|
372
|
+
this.push({ name: "numberValue", value: this._accumulator });
|
|
373
|
+
this._accumulator = "";
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
value = match[0];
|
|
377
|
+
if (value === ",") {
|
|
378
|
+
this._expect = this._expect === "arrayStop" ? "value" : "key";
|
|
379
|
+
} else if (value === "}" || value === "]") {
|
|
380
|
+
if (value === "}" ? this._expect === "arrayStop" : this._expect !== "arrayStop") {
|
|
381
|
+
return callback(new Error("Parser cannot parse input: expected '" + (this._expect === "arrayStop" ? "]" : "}") + "'"));
|
|
382
|
+
}
|
|
383
|
+
this.push({ name: value === "}" ? "endObject" : "endArray" });
|
|
384
|
+
this._parent = this._stack.pop();
|
|
385
|
+
this._expect = expected[this._parent];
|
|
386
|
+
}
|
|
387
|
+
if (noSticky) {
|
|
388
|
+
this._buffer = this._buffer.slice(value.length);
|
|
389
|
+
} else {
|
|
390
|
+
index += value.length;
|
|
391
|
+
}
|
|
392
|
+
break;
|
|
393
|
+
case "numberStart":
|
|
394
|
+
patterns.numberStart.lastIndex = index;
|
|
395
|
+
match = patterns.numberStart.exec(this._buffer);
|
|
396
|
+
if (!match) {
|
|
397
|
+
if (index < this._buffer.length || this._done)
|
|
398
|
+
return callback(new Error("Parser cannot parse input: expected a starting digit"));
|
|
399
|
+
break main;
|
|
400
|
+
}
|
|
401
|
+
value = match[0];
|
|
402
|
+
this._streamNumbers && this.push({ name: "numberChunk", value });
|
|
403
|
+
this._packNumbers && (this._accumulator += value);
|
|
404
|
+
this._expect = value === "0" ? "numberFraction" : "numberDigit";
|
|
405
|
+
if (noSticky) {
|
|
406
|
+
this._buffer = this._buffer.slice(value.length);
|
|
407
|
+
} else {
|
|
408
|
+
index += value.length;
|
|
409
|
+
}
|
|
410
|
+
break;
|
|
411
|
+
case "numberDigit":
|
|
412
|
+
patterns.numberDigit.lastIndex = index;
|
|
413
|
+
match = patterns.numberDigit.exec(this._buffer);
|
|
414
|
+
if (!match) {
|
|
415
|
+
if (index < this._buffer.length || this._done)
|
|
416
|
+
return callback(new Error("Parser cannot parse input: expected a digit"));
|
|
417
|
+
break main;
|
|
418
|
+
}
|
|
419
|
+
value = match[0];
|
|
420
|
+
if (value) {
|
|
421
|
+
this._streamNumbers && this.push({ name: "numberChunk", value });
|
|
422
|
+
this._packNumbers && (this._accumulator += value);
|
|
423
|
+
if (noSticky) {
|
|
424
|
+
this._buffer = this._buffer.slice(value.length);
|
|
425
|
+
} else {
|
|
426
|
+
index += value.length;
|
|
427
|
+
}
|
|
428
|
+
} else {
|
|
429
|
+
if (index < this._buffer.length) {
|
|
430
|
+
this._expect = "numberFraction";
|
|
431
|
+
break;
|
|
432
|
+
}
|
|
433
|
+
if (this._done) {
|
|
434
|
+
this._expect = expected[this._parent];
|
|
435
|
+
break;
|
|
436
|
+
}
|
|
437
|
+
break main;
|
|
438
|
+
}
|
|
439
|
+
break;
|
|
440
|
+
case "numberFraction":
|
|
441
|
+
patterns.numberFraction.lastIndex = index;
|
|
442
|
+
match = patterns.numberFraction.exec(this._buffer);
|
|
443
|
+
if (!match) {
|
|
444
|
+
if (index < this._buffer.length || this._done) {
|
|
445
|
+
this._expect = expected[this._parent];
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
break main;
|
|
449
|
+
}
|
|
450
|
+
value = match[0];
|
|
451
|
+
this._streamNumbers && this.push({ name: "numberChunk", value });
|
|
452
|
+
this._packNumbers && (this._accumulator += value);
|
|
453
|
+
this._expect = value === "." ? "numberFracStart" : "numberExpSign";
|
|
454
|
+
if (noSticky) {
|
|
455
|
+
this._buffer = this._buffer.slice(value.length);
|
|
456
|
+
} else {
|
|
457
|
+
index += value.length;
|
|
458
|
+
}
|
|
459
|
+
break;
|
|
460
|
+
case "numberFracStart":
|
|
461
|
+
patterns.numberFracStart.lastIndex = index;
|
|
462
|
+
match = patterns.numberFracStart.exec(this._buffer);
|
|
463
|
+
if (!match) {
|
|
464
|
+
if (index < this._buffer.length || this._done)
|
|
465
|
+
return callback(new Error("Parser cannot parse input: expected a fractional part of a number"));
|
|
466
|
+
break main;
|
|
467
|
+
}
|
|
468
|
+
value = match[0];
|
|
469
|
+
this._streamNumbers && this.push({ name: "numberChunk", value });
|
|
470
|
+
this._packNumbers && (this._accumulator += value);
|
|
471
|
+
this._expect = "numberFracDigit";
|
|
472
|
+
if (noSticky) {
|
|
473
|
+
this._buffer = this._buffer.slice(value.length);
|
|
474
|
+
} else {
|
|
475
|
+
index += value.length;
|
|
476
|
+
}
|
|
477
|
+
break;
|
|
478
|
+
case "numberFracDigit":
|
|
479
|
+
patterns.numberFracDigit.lastIndex = index;
|
|
480
|
+
match = patterns.numberFracDigit.exec(this._buffer);
|
|
481
|
+
value = match[0];
|
|
482
|
+
if (value) {
|
|
483
|
+
this._streamNumbers && this.push({ name: "numberChunk", value });
|
|
484
|
+
this._packNumbers && (this._accumulator += value);
|
|
485
|
+
if (noSticky) {
|
|
486
|
+
this._buffer = this._buffer.slice(value.length);
|
|
487
|
+
} else {
|
|
488
|
+
index += value.length;
|
|
489
|
+
}
|
|
490
|
+
} else {
|
|
491
|
+
if (index < this._buffer.length) {
|
|
492
|
+
this._expect = "numberExponent";
|
|
493
|
+
break;
|
|
494
|
+
}
|
|
495
|
+
if (this._done) {
|
|
496
|
+
this._expect = expected[this._parent];
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
break main;
|
|
500
|
+
}
|
|
501
|
+
break;
|
|
502
|
+
case "numberExponent":
|
|
503
|
+
patterns.numberExponent.lastIndex = index;
|
|
504
|
+
match = patterns.numberExponent.exec(this._buffer);
|
|
505
|
+
if (!match) {
|
|
506
|
+
if (index < this._buffer.length) {
|
|
507
|
+
this._expect = expected[this._parent];
|
|
508
|
+
break;
|
|
509
|
+
}
|
|
510
|
+
if (this._done) {
|
|
511
|
+
this._expect = "done";
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
break main;
|
|
515
|
+
}
|
|
516
|
+
value = match[0];
|
|
517
|
+
this._streamNumbers && this.push({ name: "numberChunk", value });
|
|
518
|
+
this._packNumbers && (this._accumulator += value);
|
|
519
|
+
this._expect = "numberExpSign";
|
|
520
|
+
if (noSticky) {
|
|
521
|
+
this._buffer = this._buffer.slice(value.length);
|
|
522
|
+
} else {
|
|
523
|
+
index += value.length;
|
|
524
|
+
}
|
|
525
|
+
break;
|
|
526
|
+
case "numberExpSign":
|
|
527
|
+
patterns.numberExpSign.lastIndex = index;
|
|
528
|
+
match = patterns.numberExpSign.exec(this._buffer);
|
|
529
|
+
if (!match) {
|
|
530
|
+
if (index < this._buffer.length) {
|
|
531
|
+
this._expect = "numberExpStart";
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
if (this._done)
|
|
535
|
+
return callback(new Error("Parser has expected an exponent value of a number"));
|
|
536
|
+
break main;
|
|
537
|
+
}
|
|
538
|
+
value = match[0];
|
|
539
|
+
this._streamNumbers && this.push({ name: "numberChunk", value });
|
|
540
|
+
this._packNumbers && (this._accumulator += value);
|
|
541
|
+
this._expect = "numberExpStart";
|
|
542
|
+
if (noSticky) {
|
|
543
|
+
this._buffer = this._buffer.slice(value.length);
|
|
544
|
+
} else {
|
|
545
|
+
index += value.length;
|
|
546
|
+
}
|
|
547
|
+
break;
|
|
548
|
+
case "numberExpStart":
|
|
549
|
+
patterns.numberExpStart.lastIndex = index;
|
|
550
|
+
match = patterns.numberExpStart.exec(this._buffer);
|
|
551
|
+
if (!match) {
|
|
552
|
+
if (index < this._buffer.length || this._done)
|
|
553
|
+
return callback(new Error("Parser cannot parse input: expected an exponent part of a number"));
|
|
554
|
+
break main;
|
|
555
|
+
}
|
|
556
|
+
value = match[0];
|
|
557
|
+
this._streamNumbers && this.push({ name: "numberChunk", value });
|
|
558
|
+
this._packNumbers && (this._accumulator += value);
|
|
559
|
+
this._expect = "numberExpDigit";
|
|
560
|
+
if (noSticky) {
|
|
561
|
+
this._buffer = this._buffer.slice(value.length);
|
|
562
|
+
} else {
|
|
563
|
+
index += value.length;
|
|
564
|
+
}
|
|
565
|
+
break;
|
|
566
|
+
case "numberExpDigit":
|
|
567
|
+
patterns.numberExpDigit.lastIndex = index;
|
|
568
|
+
match = patterns.numberExpDigit.exec(this._buffer);
|
|
569
|
+
value = match[0];
|
|
570
|
+
if (value) {
|
|
571
|
+
this._streamNumbers && this.push({ name: "numberChunk", value });
|
|
572
|
+
this._packNumbers && (this._accumulator += value);
|
|
573
|
+
if (noSticky) {
|
|
574
|
+
this._buffer = this._buffer.slice(value.length);
|
|
575
|
+
} else {
|
|
576
|
+
index += value.length;
|
|
577
|
+
}
|
|
578
|
+
} else {
|
|
579
|
+
if (index < this._buffer.length || this._done) {
|
|
580
|
+
this._expect = expected[this._parent];
|
|
581
|
+
break;
|
|
582
|
+
}
|
|
583
|
+
break main;
|
|
584
|
+
}
|
|
585
|
+
break;
|
|
586
|
+
case "done":
|
|
587
|
+
patterns.ws.lastIndex = index;
|
|
588
|
+
match = patterns.ws.exec(this._buffer);
|
|
589
|
+
if (!match) {
|
|
590
|
+
if (index < this._buffer.length) {
|
|
591
|
+
if (this._jsonStreaming) {
|
|
592
|
+
this._expect = "value";
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
return callback(new Error("Parser cannot parse input: unexpected characters"));
|
|
596
|
+
}
|
|
597
|
+
break main;
|
|
598
|
+
}
|
|
599
|
+
value = match[0];
|
|
600
|
+
if (this._open_number) {
|
|
601
|
+
this._streamNumbers && this.push({ name: "endNumber" });
|
|
602
|
+
this._open_number = false;
|
|
603
|
+
if (this._packNumbers) {
|
|
604
|
+
this.push({ name: "numberValue", value: this._accumulator });
|
|
605
|
+
this._accumulator = "";
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
if (noSticky) {
|
|
609
|
+
this._buffer = this._buffer.slice(value.length);
|
|
610
|
+
} else {
|
|
611
|
+
index += value.length;
|
|
612
|
+
}
|
|
613
|
+
break;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
!noSticky && (this._buffer = this._buffer.slice(index));
|
|
617
|
+
callback(null);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
Parser.parser = Parser.make;
|
|
621
|
+
Parser.make.Constructor = Parser;
|
|
622
|
+
module.exports = Parser;
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
// node_modules/stream-json/utils/emit.js
|
|
626
|
+
var require_emit = __commonJS(function(exports, module) {
|
|
627
|
+
var emit = (stream) => stream.on("data", (item) => stream.emit(item.name, item.value));
|
|
628
|
+
module.exports = emit;
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
// node_modules/stream-json/index.js
|
|
632
|
+
var require_stream_json = __commonJS(function(exports, module) {
|
|
633
|
+
var Parser = require_Parser();
|
|
634
|
+
var emit = require_emit();
|
|
635
|
+
var make = (options) => emit(new Parser(options));
|
|
636
|
+
make.Parser = Parser;
|
|
637
|
+
make.parser = Parser.parser;
|
|
638
|
+
module.exports = make;
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
// packages/core/src/plugin-logger.ts
|
|
642
|
+
var sinkKey = Symbol.for("@jeffusion/bungee/plugin-log-sink");
|
|
643
|
+
function createLogger(bindings = {}) {
|
|
644
|
+
const method = (level) => (value, message) => {
|
|
645
|
+
const sink = globalThis[sinkKey];
|
|
646
|
+
if (sink) {
|
|
647
|
+
if (typeof value === "string")
|
|
648
|
+
sink[level](bindings, value);
|
|
649
|
+
else
|
|
650
|
+
sink[level]({ ...bindings, ...value }, message);
|
|
651
|
+
} else if (level !== "debug" || process.env.LOG_LEVEL === "debug") {
|
|
652
|
+
const write = level === "fatal" ? console.error : console[level];
|
|
653
|
+
write(message ?? (typeof value === "string" ? value : ""), typeof value === "string" ? bindings : { ...bindings, ...value });
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
return {
|
|
657
|
+
info: method("info"),
|
|
658
|
+
warn: method("warn"),
|
|
659
|
+
error: method("error"),
|
|
660
|
+
debug: method("debug"),
|
|
661
|
+
fatal: method("fatal"),
|
|
662
|
+
child: (extra) => createLogger({ ...bindings, ...extra })
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
var logger = createLogger();
|
|
666
|
+
|
|
667
|
+
// packages/core/src/hooks/impl.ts
|
|
668
|
+
var WATERFALL_VIEW_TRANSFORM = Symbol("waterfall-view-transform");
|
|
669
|
+
// packages/core/src/plugin.types.ts
|
|
670
|
+
function definePlugin(plugin) {
|
|
671
|
+
return plugin;
|
|
672
|
+
}
|
|
673
|
+
// packages/core/src/plugin-extensions.ts
|
|
674
|
+
var ANONYMOUS_PRINCIPAL = Object.freeze({ domain: "anonymous", keyId: "", credentialVersion: 0 });
|
|
675
|
+
// packages/core/src/plugin-durable-state.ts
|
|
676
|
+
var MAX_BYTES = 1024 * 1024;
|
|
677
|
+
// packages/core/src/plugin-services/host-rpc.ts
|
|
678
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
679
|
+
|
|
680
|
+
// packages/core/src/plugin-services/wire-contract.ts
|
|
681
|
+
var RPC_JSON_MAX_BYTES = 64 * 1024;
|
|
682
|
+
var RPC_CONTRACT_MAX_BYTES = 256 * 1024;
|
|
683
|
+
var RPC_CALL_PURPOSES = new Set(["bootstrap", "background", "management", "request", "attempt"]);
|
|
684
|
+
var RPC_COMMAND_DEDUPLICATION = new Set(["local-transaction", "external-contract", "none"]);
|
|
685
|
+
var RPC_COMMAND_POLICY_KEYS = new Set(["deduplication", "resultRetentionMs", "quotaBytes", "maxResultBytes"]);
|
|
686
|
+
|
|
687
|
+
// packages/core/src/plugin-services/rpc-runtime.ts
|
|
688
|
+
var PROCESSES = new Set(["control", "worker", "ingress"]);
|
|
689
|
+
var SCOPES = new Set(["global", "binding"]);
|
|
690
|
+
var PURPOSES = new Set(["bootstrap", "background", "management", "request", "attempt"]);
|
|
691
|
+
var ENDPOINTS = new WeakMap;
|
|
692
|
+
var REVOKED = new WeakMap;
|
|
693
|
+
|
|
694
|
+
// packages/core/src/plugin-services/host-rpc.ts
|
|
695
|
+
var SERVICE_ERROR_MESSAGES = {
|
|
696
|
+
unauthorized: "RPC call is not authorized",
|
|
697
|
+
not_ready: "RPC endpoint is not ready",
|
|
698
|
+
retired: "RPC endpoint is retiring",
|
|
699
|
+
revoked: "RPC endpoint generation was revoked",
|
|
700
|
+
unsupported_method: "RPC method is not published",
|
|
701
|
+
wrong_purpose: "RPC method is not published for this purpose",
|
|
702
|
+
invalid_input: "RPC input is not valid",
|
|
703
|
+
invalid_output: "RPC output is not valid",
|
|
704
|
+
invalid_operation_id: "RPC command operation id is invalid",
|
|
705
|
+
overloaded: "RPC runtime is at capacity",
|
|
706
|
+
cancelled: "RPC call was cancelled",
|
|
707
|
+
timeout: "RPC call deadline exceeded",
|
|
708
|
+
unknown: "RPC command outcome is unknown",
|
|
709
|
+
capability_unavailable: "RPC command execution is unavailable",
|
|
710
|
+
closed: "RPC runtime is closed",
|
|
711
|
+
failed: "RPC call failed",
|
|
712
|
+
conflict: "RPC command identity conflicts with an existing record",
|
|
713
|
+
expired: "RPC command result has expired",
|
|
714
|
+
pending: "RPC command is still pending",
|
|
715
|
+
rejected: "RPC command was rejected",
|
|
716
|
+
missing: "RPC command has no stored result",
|
|
717
|
+
storage_failure: "RPC command storage failed",
|
|
718
|
+
deadlock: "RPC call chain would deadlock",
|
|
719
|
+
call_depth_exceeded: "RPC call depth exceeded",
|
|
720
|
+
undeclared: "RPC service is not declared by the manifest",
|
|
721
|
+
unavailable: "RPC service is unavailable",
|
|
722
|
+
ambiguous_placement: "RPC service placement is ambiguous",
|
|
723
|
+
invalid_registration: "RPC registration is invalid"
|
|
724
|
+
};
|
|
725
|
+
var SERVICE_ERROR_CODES = new Set(Object.keys(SERVICE_ERROR_MESSAGES));
|
|
726
|
+
var EMPTY_OPTIONS = Object.freeze({});
|
|
727
|
+
var FRAME_STORE = new AsyncLocalStorage;
|
|
728
|
+
var CALLEE_INFO = new WeakMap;
|
|
729
|
+
// packages/core/src/plugin-services/peer-channel-protocol.ts
|
|
730
|
+
var CHANNEL_REQUEST_MAX_BYTES = 4 * 1024;
|
|
731
|
+
var CHANNEL_RESULT_MAX_BYTES = 60 * 1024;
|
|
732
|
+
var CHANNEL_MAX_CHUNK_BYTES = 60 * 1024;
|
|
733
|
+
var PLUGIN_CHANNEL_LANES = Object.freeze(["event", "snapshot", "stream"]);
|
|
734
|
+
var ERROR_CODES = new Set([
|
|
735
|
+
"unavailable",
|
|
736
|
+
"ambiguous",
|
|
737
|
+
"unauthorized",
|
|
738
|
+
"invalid",
|
|
739
|
+
"overloaded",
|
|
740
|
+
"expired",
|
|
741
|
+
"conflict",
|
|
742
|
+
"gap",
|
|
743
|
+
"truncated",
|
|
744
|
+
"rejected",
|
|
745
|
+
"cancelled",
|
|
746
|
+
"closed",
|
|
747
|
+
"unknown",
|
|
748
|
+
"failed"
|
|
749
|
+
]);
|
|
750
|
+
var RPC_CALL_PURPOSES2 = new Set(["bootstrap", "background", "management", "request", "attempt"]);
|
|
751
|
+
|
|
752
|
+
// packages/core/src/plugin-services/peer-channel-hub.ts
|
|
753
|
+
var CHANNEL_MAX_TRANSFER_BUFFER_BYTES = 4 * CHANNEL_MAX_CHUNK_BYTES;
|
|
754
|
+
var CHANNEL_MAX_TOTAL_BUFFER_BYTES = 16 * CHANNEL_MAX_CHUNK_BYTES;
|
|
755
|
+
var CHANNEL_STREAM_MAX_TOTAL_BYTES = 64 * 1024 * 1024;
|
|
756
|
+
var CHANNEL_STREAM_MAX_CREDIT_BYTES = 8 * CHANNEL_MAX_CHUNK_BYTES;
|
|
757
|
+
var CHANNEL_STREAM_DEFAULT_CREDIT_BYTES = 4 * CHANNEL_MAX_CHUNK_BYTES;
|
|
758
|
+
var CHANNEL_SNAPSHOT_MAX_BYTES = 64 * 1024 * 1024;
|
|
759
|
+
var CHANNEL_EVENT_MAX_PAYLOAD_BYTES = 60 * 1024;
|
|
760
|
+
var EMPTY = new Uint8Array(0);
|
|
761
|
+
var HANDLE_REGISTRATIONS = new WeakMap;
|
|
762
|
+
|
|
763
|
+
// packages/core/src/plugin-services/snapshot-store.ts
|
|
764
|
+
var DEFAULT_CHUNK_BYTES = 60 * 1024;
|
|
765
|
+
var SNAPSHOT_FAMILY_REFS = new Map;
|
|
766
|
+
|
|
767
|
+
// packages/core/src/plugin-services.ts
|
|
768
|
+
var CHANNEL_KINDS = new Set(["events", "snapshot", "stream"]);
|
|
769
|
+
// packages/core/src/gateway/retry-action.ts
|
|
770
|
+
var retryActionBrand = Symbol.for("@jeffusion/bungee/request-retry-action");
|
|
771
|
+
// packages/core/src/token-stats-window.ts
|
|
772
|
+
var HOUR = 3600000;
|
|
773
|
+
var TOKEN_STATS_RETENTION_MS = 31 * 24 * HOUR;
|
|
774
|
+
// plugins/request-logs/server/protocols/contracts.ts
|
|
775
|
+
function object(value) {
|
|
776
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : undefined;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
// plugins/request-logs/server/protocols/gemini.ts
|
|
780
|
+
function candidates(body) {
|
|
781
|
+
if (!Array.isArray(body.candidates))
|
|
782
|
+
return [];
|
|
783
|
+
return body.candidates.map(object).filter((candidate) => candidate !== undefined);
|
|
784
|
+
}
|
|
785
|
+
function finishReason(value) {
|
|
786
|
+
if (typeof value !== "string" || !/^[A-Z][A-Z_]{1,63}$/.test(value) || value === "FINISH_REASON_UNSPECIFIED")
|
|
787
|
+
return;
|
|
788
|
+
if (value === "STOP")
|
|
789
|
+
return { outcome: "completed" };
|
|
790
|
+
return { outcome: value === "MAX_TOKENS" ? "incomplete" : "failed", code: value };
|
|
791
|
+
}
|
|
792
|
+
function live() {
|
|
793
|
+
const operation = { protocol: "gemini", operation: "bidiGenerateContent" };
|
|
794
|
+
let setup = false;
|
|
795
|
+
let confirmed = false;
|
|
796
|
+
let current;
|
|
797
|
+
const beginTurn = (turn) => {
|
|
798
|
+
if (turn.started)
|
|
799
|
+
return;
|
|
800
|
+
turn.started = true;
|
|
801
|
+
return async (records) => {
|
|
802
|
+
turn.id = await records.start({ ...operation, startedAt: turn.startedAt });
|
|
803
|
+
};
|
|
804
|
+
};
|
|
805
|
+
return {
|
|
806
|
+
observe() {},
|
|
807
|
+
prepareWebSocket(event) {
|
|
808
|
+
const message = event.message;
|
|
809
|
+
if (event.settled || event.oversized || !setup && event.direction !== "client")
|
|
810
|
+
return;
|
|
811
|
+
if (event.direction === "client") {
|
|
812
|
+
if ((setup ? message.mayContainJsonString?.("clientContent", "realtimeInput") : message.mayContainJsonString?.("setup")) === false)
|
|
813
|
+
return;
|
|
814
|
+
const body = object(message.json());
|
|
815
|
+
if (!setup) {
|
|
816
|
+
const model = object(body?.setup)?.model;
|
|
817
|
+
setup = typeof model === "string" && model.startsWith("models/") && model.length > 7;
|
|
818
|
+
} else if (confirmed) {
|
|
819
|
+
const content = object(body?.clientContent);
|
|
820
|
+
if (content || object(body?.realtimeInput)) {
|
|
821
|
+
current ??= { startedAt: event.timestamp, started: false, interrupted: false };
|
|
822
|
+
if (content?.turnComplete === true)
|
|
823
|
+
return beginTurn(current);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
828
|
+
if (message.mayContainJsonString?.(confirmed ? "serverContent" : "setupComplete") === false)
|
|
829
|
+
return;
|
|
830
|
+
const body = object(message.json({ binary: true }));
|
|
831
|
+
if (!confirmed) {
|
|
832
|
+
confirmed = object(body?.setupComplete) !== undefined;
|
|
833
|
+
return;
|
|
834
|
+
}
|
|
835
|
+
const content = object(body?.serverContent);
|
|
836
|
+
const turn = current;
|
|
837
|
+
if (!content || !turn)
|
|
838
|
+
return;
|
|
839
|
+
turn.interrupted ||= content.interrupted === true;
|
|
840
|
+
const complete = content.turnComplete === true;
|
|
841
|
+
const started = !!object(content.modelTurn) || content.generationComplete === true || turn.interrupted;
|
|
842
|
+
const begin = started || complete ? beginTurn(turn) : undefined;
|
|
843
|
+
if (!complete)
|
|
844
|
+
return begin;
|
|
845
|
+
current = undefined;
|
|
846
|
+
return async (records) => {
|
|
847
|
+
await begin?.(records);
|
|
848
|
+
if (turn.id)
|
|
849
|
+
records.finish(turn.id, { outcome: turn.interrupted ? "cancelled" : "completed" });
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
var gemini = {
|
|
855
|
+
name: "gemini",
|
|
856
|
+
http(context, streaming) {
|
|
857
|
+
const endpoint = /:(?:streamGenerateContent|generateContent)\/?$/.test(context.path);
|
|
858
|
+
const outcomes = new Map;
|
|
859
|
+
return {
|
|
860
|
+
operation: streaming ? "streamGenerateContent" : "generateContent",
|
|
861
|
+
matches(body, _event, confirmed) {
|
|
862
|
+
const metadata = typeof body.modelVersion === "string" || typeof body.responseId === "string";
|
|
863
|
+
const feedback = object(body.promptFeedback);
|
|
864
|
+
const error = object(body.error);
|
|
865
|
+
return (endpoint || metadata || confirmed) && (candidates(body).some((candidate) => object(candidate.content)?.role === "model" || finishReason(candidate.finishReason)) || typeof feedback?.blockReason === "string") || (endpoint || confirmed) && typeof error?.status === "string" && typeof error.code === "number";
|
|
866
|
+
},
|
|
867
|
+
observe(body) {
|
|
868
|
+
if (object(body.error))
|
|
869
|
+
return { outcome: "failed", code: "upstream_protocol_error" };
|
|
870
|
+
const block = object(body.promptFeedback)?.blockReason;
|
|
871
|
+
if (typeof block === "string" && block !== "BLOCK_REASON_UNSPECIFIED")
|
|
872
|
+
return { outcome: "failed", code: block };
|
|
873
|
+
for (const [position, candidate] of candidates(body).entries()) {
|
|
874
|
+
const index = candidate.index ?? position;
|
|
875
|
+
if (typeof index !== "number" || !Number.isInteger(index) || index < 0 || index >= 64)
|
|
876
|
+
continue;
|
|
877
|
+
const result = finishReason(candidate.finishReason);
|
|
878
|
+
if (result || !outcomes.has(index))
|
|
879
|
+
outcomes.set(index, result);
|
|
880
|
+
}
|
|
881
|
+
},
|
|
882
|
+
end() {
|
|
883
|
+
const results = [...outcomes.values()];
|
|
884
|
+
return results.find((result) => result?.outcome === "failed") ?? (results.some((result) => !result) ? undefined : results.find((result) => result?.outcome === "incomplete") ?? results[0]);
|
|
885
|
+
}
|
|
886
|
+
};
|
|
887
|
+
},
|
|
888
|
+
websocket(context) {
|
|
889
|
+
if (!context.webSocketMode)
|
|
890
|
+
return live();
|
|
891
|
+
}
|
|
892
|
+
};
|
|
893
|
+
|
|
894
|
+
// plugins/request-logs/server/protocols/responses.ts
|
|
895
|
+
var operation = { protocol: "responses", operation: "response.create" };
|
|
896
|
+
function identifier(value) {
|
|
897
|
+
return typeof value === "string" && value.length > 0 && value.length <= 256 ? value : undefined;
|
|
898
|
+
}
|
|
899
|
+
function streamId(body, response) {
|
|
900
|
+
const outer = body.stream_id;
|
|
901
|
+
const inner = response?.stream_id;
|
|
902
|
+
if (outer !== undefined && inner !== undefined && outer !== inner)
|
|
903
|
+
return null;
|
|
904
|
+
const value = outer === undefined ? inner : outer;
|
|
905
|
+
return value === undefined ? undefined : identifier(value) ?? null;
|
|
906
|
+
}
|
|
907
|
+
function responseResult(status) {
|
|
908
|
+
switch (status) {
|
|
909
|
+
case "completed":
|
|
910
|
+
return { outcome: "completed" };
|
|
911
|
+
case "failed":
|
|
912
|
+
return { outcome: "failed", code: "upstream_protocol_error" };
|
|
913
|
+
case "incomplete":
|
|
914
|
+
return { outcome: "incomplete", code: "incomplete" };
|
|
915
|
+
case "cancelled":
|
|
916
|
+
return { outcome: "cancelled" };
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
function eventResult(type, response) {
|
|
920
|
+
if (type === "error")
|
|
921
|
+
return responseResult("failed");
|
|
922
|
+
if (typeof type !== "string" || !type.startsWith("response.") || !response)
|
|
923
|
+
return;
|
|
924
|
+
const result = responseResult(type.slice("response.".length));
|
|
925
|
+
if (response.status === undefined || response.status === result?.outcome) {
|
|
926
|
+
return result;
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
function isOpeningType(type) {
|
|
930
|
+
return type === "response.created" || type === "response.in_progress";
|
|
931
|
+
}
|
|
932
|
+
var MAX_OPERATIONS = 64;
|
|
933
|
+
var MAX_RECENT_RESPONSE_IDS = 256;
|
|
934
|
+
var LIFECYCLE_TYPES = [
|
|
935
|
+
"response.created",
|
|
936
|
+
"response.in_progress",
|
|
937
|
+
"response.completed",
|
|
938
|
+
"response.incomplete",
|
|
939
|
+
"response.failed",
|
|
940
|
+
"response.cancelled",
|
|
941
|
+
"error"
|
|
942
|
+
];
|
|
943
|
+
function observeWebSocket(hosted) {
|
|
944
|
+
const operations = new Map;
|
|
945
|
+
const pending = [];
|
|
946
|
+
const seen = new Set;
|
|
947
|
+
const uncertain = new Set;
|
|
948
|
+
const unconfirmed = new Map;
|
|
949
|
+
let overflow = false;
|
|
950
|
+
const start = (key, current) => {
|
|
951
|
+
operations.set(key, current);
|
|
952
|
+
return async (records) => {
|
|
953
|
+
current.id = await records.start({ ...operation, body: current.capture, startedAt: current.startedAt });
|
|
954
|
+
if (current.id)
|
|
955
|
+
return;
|
|
956
|
+
current.capture?.discard();
|
|
957
|
+
if (operations.get(key) === current)
|
|
958
|
+
operations.delete(key);
|
|
959
|
+
};
|
|
960
|
+
};
|
|
961
|
+
const finish = (key, current, result) => {
|
|
962
|
+
operations.delete(key);
|
|
963
|
+
return (records) => {
|
|
964
|
+
if (current.id)
|
|
965
|
+
records.finish(current.id, result);
|
|
966
|
+
};
|
|
967
|
+
};
|
|
968
|
+
return {
|
|
969
|
+
observe() {},
|
|
970
|
+
prepareWebSocket(event, bodies) {
|
|
971
|
+
const { relatedOperationId } = event;
|
|
972
|
+
const message = event.message;
|
|
973
|
+
const hostedOperation = hosted && relatedOperationId ? operations.get(relatedOperationId) : undefined;
|
|
974
|
+
if (event.settled) {
|
|
975
|
+
if (!hostedOperation || !relatedOperationId)
|
|
976
|
+
return;
|
|
977
|
+
return finish(relatedOperationId, hostedOperation, hostedOperation.result);
|
|
978
|
+
}
|
|
979
|
+
if (message.kind !== "text")
|
|
980
|
+
return;
|
|
981
|
+
if (event.direction === "client" && message.mayContainJsonString?.("response.create") === false)
|
|
982
|
+
return;
|
|
983
|
+
if (event.direction === "upstream" && hosted && !hostedOperation)
|
|
984
|
+
return;
|
|
985
|
+
if (event.oversized) {
|
|
986
|
+
if (hosted) {
|
|
987
|
+
hostedOperation?.capture?.discardResponse("size_limit");
|
|
988
|
+
} else if (event.direction === "client") {
|
|
989
|
+
overflow = true;
|
|
990
|
+
for (const candidate of pending)
|
|
991
|
+
candidate.capture?.discard();
|
|
992
|
+
pending.length = 0;
|
|
993
|
+
} else {
|
|
994
|
+
for (const current of operations.values())
|
|
995
|
+
current.capture?.discardResponse("size_limit");
|
|
996
|
+
if (message.mayContainJsonString?.(...LIFECYCLE_TYPES) !== false) {
|
|
997
|
+
overflow = true;
|
|
998
|
+
for (const candidate of pending)
|
|
999
|
+
candidate.capture?.discard();
|
|
1000
|
+
pending.length = 0;
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
return;
|
|
1004
|
+
}
|
|
1005
|
+
if (event.direction === "upstream" && (!bodies || !operations.size) && message.mayContainJsonString?.(...LIFECYCLE_TYPES) === false)
|
|
1006
|
+
return;
|
|
1007
|
+
const body = object(message.json());
|
|
1008
|
+
if (!body) {
|
|
1009
|
+
if (event.direction === "upstream") {
|
|
1010
|
+
for (const current of operations.values())
|
|
1011
|
+
current.capture?.discardResponse();
|
|
1012
|
+
}
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
if (event.direction === "client") {
|
|
1016
|
+
if (body.type !== "response.create")
|
|
1017
|
+
return;
|
|
1018
|
+
if (hosted) {
|
|
1019
|
+
if (relatedOperationId)
|
|
1020
|
+
return start(relatedOperationId, { stream: undefined, startedAt: event.timestamp, capture: bodies?.start(message) });
|
|
1021
|
+
return;
|
|
1022
|
+
}
|
|
1023
|
+
const stream = streamId(body);
|
|
1024
|
+
if (overflow || stream === null || uncertain.has(stream))
|
|
1025
|
+
return;
|
|
1026
|
+
const existing = pending.findIndex((candidate) => candidate.stream === stream);
|
|
1027
|
+
if (existing !== -1) {
|
|
1028
|
+
pending.splice(existing, 1)[0].capture?.discard();
|
|
1029
|
+
uncertain.add(stream);
|
|
1030
|
+
return;
|
|
1031
|
+
}
|
|
1032
|
+
if (pending.length + uncertain.size >= MAX_OPERATIONS) {
|
|
1033
|
+
overflow = true;
|
|
1034
|
+
for (const candidate of pending)
|
|
1035
|
+
candidate.capture?.discard();
|
|
1036
|
+
pending.length = 0;
|
|
1037
|
+
return;
|
|
1038
|
+
}
|
|
1039
|
+
pending.push({ stream, startedAt: event.timestamp, capture: bodies?.start(message) });
|
|
1040
|
+
return;
|
|
1041
|
+
}
|
|
1042
|
+
const response = object(body.response);
|
|
1043
|
+
const result = eventResult(body.type, response);
|
|
1044
|
+
if (hosted) {
|
|
1045
|
+
if (!hostedOperation)
|
|
1046
|
+
return;
|
|
1047
|
+
hostedOperation.capture?.append(message);
|
|
1048
|
+
if (event.hostedErrorOutcome) {
|
|
1049
|
+
hostedOperation.result = {
|
|
1050
|
+
outcome: event.hostedErrorOutcome,
|
|
1051
|
+
code: event.hostedErrorOutcome === "failed" ? "upstream_protocol_error" : undefined
|
|
1052
|
+
};
|
|
1053
|
+
} else if (result && body.type !== "error") {
|
|
1054
|
+
hostedOperation.result = result;
|
|
1055
|
+
}
|
|
1056
|
+
return;
|
|
1057
|
+
}
|
|
1058
|
+
const opening = response?.object === "response" && isOpeningType(body.type) && identifier(response.id) !== undefined && (response.status === undefined || response.status === "queued" || response.status === "in_progress");
|
|
1059
|
+
const declaredResponseId = response?.id ?? body.response_id;
|
|
1060
|
+
const responseId = identifier(declaredResponseId);
|
|
1061
|
+
const stream = streamId(body, response);
|
|
1062
|
+
if (stream === null || declaredResponseId !== undefined && !responseId || response?.id !== undefined && body.response_id !== undefined && response.id !== body.response_id) {
|
|
1063
|
+
if (typeof body.type === "string" && (body.type.startsWith("response.") || body.type === "error")) {
|
|
1064
|
+
for (const current of operations.values())
|
|
1065
|
+
current.capture?.discardResponse();
|
|
1066
|
+
}
|
|
1067
|
+
return;
|
|
1068
|
+
}
|
|
1069
|
+
if (body.type === "error" && !responseId) {
|
|
1070
|
+
for (const current of operations.values()) {
|
|
1071
|
+
if (stream === undefined || current.stream === stream)
|
|
1072
|
+
current.capture?.discardResponse();
|
|
1073
|
+
}
|
|
1074
|
+
for (let i = pending.length - 1;i >= 0; i--) {
|
|
1075
|
+
const candidate = pending[i];
|
|
1076
|
+
if (stream !== undefined && candidate.stream !== stream)
|
|
1077
|
+
continue;
|
|
1078
|
+
uncertain.add(candidate.stream);
|
|
1079
|
+
candidate.capture?.discard();
|
|
1080
|
+
pending.splice(i, 1);
|
|
1081
|
+
}
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
if (!responseId) {
|
|
1085
|
+
if (typeof body.type !== "string" || !body.type.startsWith("response."))
|
|
1086
|
+
return;
|
|
1087
|
+
const possible = [...operations.values()].filter((current) => stream === undefined || current.stream === stream);
|
|
1088
|
+
const hasPending = pending.some((candidate) => stream === undefined || candidate.stream === stream);
|
|
1089
|
+
const hasUnconfirmed = stream === undefined ? unconfirmed.size > 0 : [...unconfirmed.values()].includes(stream);
|
|
1090
|
+
const ambiguous = overflow || hasUnconfirmed || hasPending || (stream === undefined ? uncertain.size > 0 : uncertain.has(stream)) || possible.length !== 1;
|
|
1091
|
+
for (const current of possible) {
|
|
1092
|
+
if (ambiguous || result || isOpeningType(body.type))
|
|
1093
|
+
current.capture?.discardResponse();
|
|
1094
|
+
else
|
|
1095
|
+
current.capture?.append(message);
|
|
1096
|
+
}
|
|
1097
|
+
return;
|
|
1098
|
+
}
|
|
1099
|
+
const established = operations.get(responseId);
|
|
1100
|
+
if (established) {
|
|
1101
|
+
if (stream === undefined || stream === established.stream) {
|
|
1102
|
+
if (typeof body.type === "string" && (body.type.startsWith("response.") || body.type === "error")) {
|
|
1103
|
+
established.capture?.append(message);
|
|
1104
|
+
}
|
|
1105
|
+
if (result)
|
|
1106
|
+
return finish(responseId, established, result);
|
|
1107
|
+
} else {
|
|
1108
|
+
established.capture?.discardResponse();
|
|
1109
|
+
}
|
|
1110
|
+
return;
|
|
1111
|
+
}
|
|
1112
|
+
if (result)
|
|
1113
|
+
unconfirmed.delete(responseId);
|
|
1114
|
+
const replay = seen.has(responseId);
|
|
1115
|
+
if (!opening && !result)
|
|
1116
|
+
return;
|
|
1117
|
+
seen.add(responseId);
|
|
1118
|
+
if (seen.size > MAX_RECENT_RESPONSE_IDS)
|
|
1119
|
+
seen.delete(seen.values().next().value);
|
|
1120
|
+
if (!opening)
|
|
1121
|
+
return;
|
|
1122
|
+
const index = pending.findIndex((candidate) => candidate.stream === stream);
|
|
1123
|
+
if (replay || index === -1 || overflow || uncertain.has(stream) || operations.size >= MAX_OPERATIONS) {
|
|
1124
|
+
if (unconfirmed.size < MAX_OPERATIONS)
|
|
1125
|
+
unconfirmed.set(responseId, stream);
|
|
1126
|
+
else
|
|
1127
|
+
overflow = true;
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
const current = pending.splice(index, 1)[0];
|
|
1131
|
+
current.capture?.append(message);
|
|
1132
|
+
return start(responseId, current);
|
|
1133
|
+
}
|
|
1134
|
+
};
|
|
1135
|
+
}
|
|
1136
|
+
var responses = {
|
|
1137
|
+
name: operation.protocol,
|
|
1138
|
+
http(context, streaming) {
|
|
1139
|
+
const endpoint = /\/responses\/?$/.test(context.path);
|
|
1140
|
+
let responseId;
|
|
1141
|
+
return {
|
|
1142
|
+
operation: operation.operation,
|
|
1143
|
+
matches(body, event, confirmed) {
|
|
1144
|
+
const error = object(body.error);
|
|
1145
|
+
const errorEnvelope = typeof error?.message === "string" && (typeof error.type === "string" || typeof error.code === "string");
|
|
1146
|
+
if (!streaming)
|
|
1147
|
+
return body.object === "response" && identifier(body.id) !== undefined || endpoint && errorEnvelope;
|
|
1148
|
+
const type = body.type ?? event;
|
|
1149
|
+
if (type === "error") {
|
|
1150
|
+
const errorResponseId = identifier(body.response_id);
|
|
1151
|
+
if (responseId && errorResponseId && errorResponseId !== responseId)
|
|
1152
|
+
return false;
|
|
1153
|
+
const openAIError = body.type === "error" && typeof body.message === "string" && Number.isInteger(body.sequence_number) && (body.code === null || typeof body.code === "string") && (body.param === null || typeof body.param === "string");
|
|
1154
|
+
return openAIError || confirmed && errorEnvelope;
|
|
1155
|
+
}
|
|
1156
|
+
if (typeof type !== "string" || !type.startsWith("response."))
|
|
1157
|
+
return false;
|
|
1158
|
+
const response = object(body.response);
|
|
1159
|
+
const id = identifier(response?.id ?? body.response_id);
|
|
1160
|
+
if (responseId && id && id !== responseId)
|
|
1161
|
+
return false;
|
|
1162
|
+
const opening = isOpeningType(type);
|
|
1163
|
+
const terminal = responseResult(type.slice("response.".length)) !== undefined;
|
|
1164
|
+
if (opening || terminal) {
|
|
1165
|
+
if (!id || response?.object !== "response" || terminal && !eventResult(type, response))
|
|
1166
|
+
return false;
|
|
1167
|
+
responseId ??= id;
|
|
1168
|
+
return true;
|
|
1169
|
+
}
|
|
1170
|
+
return confirmed;
|
|
1171
|
+
},
|
|
1172
|
+
observe(body, event) {
|
|
1173
|
+
if (!streaming)
|
|
1174
|
+
return responseResult(body.object === "response" ? body.status : "failed");
|
|
1175
|
+
const result = eventResult(body.type ?? event, object(body.response));
|
|
1176
|
+
return result?.outcome === "completed" ? { ...result, acceptClientDisconnect: true } : result;
|
|
1177
|
+
}
|
|
1178
|
+
};
|
|
1179
|
+
},
|
|
1180
|
+
websocket(context) {
|
|
1181
|
+
return observeWebSocket(context.webSocketMode === "responses");
|
|
1182
|
+
}
|
|
1183
|
+
};
|
|
1184
|
+
|
|
1185
|
+
// plugins/request-logs/server/protocols/index.ts
|
|
1186
|
+
var protocols = [responses, gemini];
|
|
1187
|
+
function mediaType(contentType) {
|
|
1188
|
+
return contentType.split(";", 1)[0].trim().toLowerCase();
|
|
1189
|
+
}
|
|
1190
|
+
function isJson(type) {
|
|
1191
|
+
return type === "application/json" || type.endsWith("+json");
|
|
1192
|
+
}
|
|
1193
|
+
function httpCandidates(context, streaming) {
|
|
1194
|
+
return protocols.map((protocol) => ({ name: protocol.name, decoder: protocol.http(context, streaming) }));
|
|
1195
|
+
}
|
|
1196
|
+
function streamBodyParser(context) {
|
|
1197
|
+
if (context.method !== "POST" || context.stage !== "client-response" || mediaType(context.contentType) !== "text/event-stream")
|
|
1198
|
+
return;
|
|
1199
|
+
return {
|
|
1200
|
+
async* stream(body) {
|
|
1201
|
+
const candidates = httpCandidates(context, true);
|
|
1202
|
+
let decoder;
|
|
1203
|
+
let first = true;
|
|
1204
|
+
for await (const frame of body.events()) {
|
|
1205
|
+
if (frame.truncated || frame.hasData === false)
|
|
1206
|
+
continue;
|
|
1207
|
+
const value = object(frame.json);
|
|
1208
|
+
if (!value)
|
|
1209
|
+
continue;
|
|
1210
|
+
if (decoder) {
|
|
1211
|
+
if (!decoder.matches(value, frame.event, true))
|
|
1212
|
+
continue;
|
|
1213
|
+
} else {
|
|
1214
|
+
decoder = candidates.find((candidate) => candidate.decoder.matches(value, frame.event, false))?.decoder;
|
|
1215
|
+
if (!decoder)
|
|
1216
|
+
continue;
|
|
1217
|
+
}
|
|
1218
|
+
yield (first ? "[" : ",") + JSON.stringify({ event: frame.event || "message", data: value });
|
|
1219
|
+
first = false;
|
|
1220
|
+
}
|
|
1221
|
+
if (!first)
|
|
1222
|
+
yield "]";
|
|
1223
|
+
}
|
|
1224
|
+
};
|
|
1225
|
+
}
|
|
1226
|
+
async function observeHttp(context, event, records) {
|
|
1227
|
+
const type = mediaType(event.contentType);
|
|
1228
|
+
const streaming = type === "text/event-stream";
|
|
1229
|
+
if (!streaming && !isJson(type))
|
|
1230
|
+
return;
|
|
1231
|
+
const candidates = httpCandidates(context, streaming);
|
|
1232
|
+
const consumer = { id: "request-log-protocol", mandatory: false, signal: event.signal };
|
|
1233
|
+
if (!streaming) {
|
|
1234
|
+
const bodyHandle = event.body;
|
|
1235
|
+
const body = object(await bodyHandle.json(consumer));
|
|
1236
|
+
if (!body)
|
|
1237
|
+
return;
|
|
1238
|
+
const candidate = candidates.find(({ decoder }) => decoder.matches(body, undefined, false));
|
|
1239
|
+
if (!candidate)
|
|
1240
|
+
return;
|
|
1241
|
+
const id = await records.start({ protocol: candidate.name, operation: candidate.decoder.operation });
|
|
1242
|
+
const result = candidate.decoder.observe(body, undefined) ?? candidate.decoder.end?.();
|
|
1243
|
+
if (id && result)
|
|
1244
|
+
records.finish(id, result);
|
|
1245
|
+
return;
|
|
1246
|
+
}
|
|
1247
|
+
let decoder;
|
|
1248
|
+
let id;
|
|
1249
|
+
for await (const frame of event.body.events(consumer)) {
|
|
1250
|
+
if (frame.truncated)
|
|
1251
|
+
continue;
|
|
1252
|
+
const body = object(frame.json);
|
|
1253
|
+
if (!body)
|
|
1254
|
+
continue;
|
|
1255
|
+
if (!decoder) {
|
|
1256
|
+
const candidate = candidates.find((candidate) => candidate.decoder.matches(body, frame.event, false));
|
|
1257
|
+
if (!candidate)
|
|
1258
|
+
continue;
|
|
1259
|
+
decoder = candidate.decoder;
|
|
1260
|
+
id = await records.start({ protocol: candidate.name, operation: decoder.operation });
|
|
1261
|
+
if (!id)
|
|
1262
|
+
return;
|
|
1263
|
+
} else if (!decoder.matches(body, frame.event, true))
|
|
1264
|
+
continue;
|
|
1265
|
+
const result = decoder.observe(body, frame.event);
|
|
1266
|
+
if (result && id) {
|
|
1267
|
+
records.finish(id, result);
|
|
1268
|
+
return;
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
const result = decoder?.end?.();
|
|
1272
|
+
if (id && result)
|
|
1273
|
+
records.finish(id, result);
|
|
1274
|
+
}
|
|
1275
|
+
function applicationObserver(context) {
|
|
1276
|
+
if (context.transportType === "http") {
|
|
1277
|
+
if (context.method === "POST")
|
|
1278
|
+
return { observe: (event, records) => {
|
|
1279
|
+
if (event.kind === "http")
|
|
1280
|
+
return observeHttp(context, event, records);
|
|
1281
|
+
} };
|
|
1282
|
+
return;
|
|
1283
|
+
}
|
|
1284
|
+
const observers = protocols.flatMap((protocol) => protocol.websocket(context) ?? []);
|
|
1285
|
+
let selected;
|
|
1286
|
+
const deliverLifecycle = (deliveries) => async (records) => {
|
|
1287
|
+
for (const { observer, deliver } of deliveries) {
|
|
1288
|
+
if (selected && selected !== observer)
|
|
1289
|
+
continue;
|
|
1290
|
+
await deliver({
|
|
1291
|
+
...records,
|
|
1292
|
+
async start(fields) {
|
|
1293
|
+
const id = await records.start(fields);
|
|
1294
|
+
if (id)
|
|
1295
|
+
selected = observer;
|
|
1296
|
+
return id;
|
|
1297
|
+
}
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1300
|
+
};
|
|
1301
|
+
return {
|
|
1302
|
+
observe() {},
|
|
1303
|
+
prepareWebSocket(event, bodies) {
|
|
1304
|
+
const deliveries = (selected ? [selected] : observers).flatMap((observer) => {
|
|
1305
|
+
const deliver = observer.prepareWebSocket?.(event, bodies);
|
|
1306
|
+
return deliver ? [{ observer, deliver }] : [];
|
|
1307
|
+
});
|
|
1308
|
+
if (deliveries.length)
|
|
1309
|
+
return deliverLifecycle(deliveries);
|
|
1310
|
+
}
|
|
1311
|
+
};
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
// plugins/request-logs/server/body-parsers.ts
|
|
1315
|
+
var import_stream_json = __toESM(require_stream_json());
|
|
1316
|
+
import { finished } from "stream/promises";
|
|
1317
|
+
var json = {
|
|
1318
|
+
async* stream(body) {
|
|
1319
|
+
const parser = new import_stream_json.Parser({ packValues: false, streamValues: true });
|
|
1320
|
+
const done = finished(parser);
|
|
1321
|
+
done.catch(() => {});
|
|
1322
|
+
let depth = 0;
|
|
1323
|
+
parser.on("data", ({ name }) => {
|
|
1324
|
+
if (name === "startArray" || name === "startObject") {
|
|
1325
|
+
if (++depth > 1024)
|
|
1326
|
+
parser.destroy(new Error("JSON nesting limit exceeded"));
|
|
1327
|
+
} else if (name === "endArray" || name === "endObject")
|
|
1328
|
+
depth--;
|
|
1329
|
+
});
|
|
1330
|
+
let inString = false;
|
|
1331
|
+
let escaped = false;
|
|
1332
|
+
try {
|
|
1333
|
+
for await (const part of body.textChunks()) {
|
|
1334
|
+
let escapedIndex = escaped ? 0 : -1;
|
|
1335
|
+
for (const match of part.matchAll(/["\\\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\uffff]/g)) {
|
|
1336
|
+
if (match.index === escapedIndex)
|
|
1337
|
+
continue;
|
|
1338
|
+
if (inString && match[0] === "\\")
|
|
1339
|
+
escapedIndex = match.index + 1;
|
|
1340
|
+
else if (match[0] === '"')
|
|
1341
|
+
inString = !inString;
|
|
1342
|
+
else if (!inString)
|
|
1343
|
+
throw new Error("Invalid character outside JSON string");
|
|
1344
|
+
}
|
|
1345
|
+
escaped = escapedIndex === part.length;
|
|
1346
|
+
if (!part)
|
|
1347
|
+
continue;
|
|
1348
|
+
await new Promise((resolve, reject) => parser.write(part, (error) => error ? reject(error) : resolve()));
|
|
1349
|
+
yield part;
|
|
1350
|
+
}
|
|
1351
|
+
parser.end();
|
|
1352
|
+
await done;
|
|
1353
|
+
} finally {
|
|
1354
|
+
parser.destroy();
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
};
|
|
1358
|
+
var text = {
|
|
1359
|
+
format: "text",
|
|
1360
|
+
async* stream(body) {
|
|
1361
|
+
yield* body.textChunks();
|
|
1362
|
+
}
|
|
1363
|
+
};
|
|
1364
|
+
|
|
1365
|
+
// plugins/request-logs/server/index.ts
|
|
1366
|
+
var server_default = definePlugin(class {
|
|
1367
|
+
static name = "request-logs";
|
|
1368
|
+
static version = "1.0.0";
|
|
1369
|
+
bodyRequirements() {
|
|
1370
|
+
return { request: "none" };
|
|
1371
|
+
}
|
|
1372
|
+
register(hooks) {
|
|
1373
|
+
hooks.onRequestLog.tap("request-logs", () => true);
|
|
1374
|
+
const fallback = { name: "request-logs", stage: Number.MAX_SAFE_INTEGER };
|
|
1375
|
+
hooks.onRequestLogBody.tap(fallback, (context) => {
|
|
1376
|
+
if (context.transportType !== "http")
|
|
1377
|
+
return;
|
|
1378
|
+
const type = mediaType(context.contentType);
|
|
1379
|
+
if (type === "application/json" || type.endsWith("+json"))
|
|
1380
|
+
return json;
|
|
1381
|
+
if (type === "text/plain")
|
|
1382
|
+
return text;
|
|
1383
|
+
return streamBodyParser(context);
|
|
1384
|
+
});
|
|
1385
|
+
hooks.onRequestLogApplication.tap(fallback, applicationObserver);
|
|
1386
|
+
}
|
|
1387
|
+
});
|
|
1388
|
+
export {
|
|
1389
|
+
server_default as default
|
|
1390
|
+
};
|
|
1391
|
+
|
|
1392
|
+
//# debugId=F9B5A5BFD2AC003364756E2164756E21
|