@jeffusion/bungee-core 6.16.1 → 6.17.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/main.js +5700 -2198
- package/dist/observability-worker.js +3245 -1495
- 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 +37 -15
- package/dist/plugins/chatgpt-oauth/index.js.map +5 -5
- package/dist/plugins/codex-router/index.js +21 -3
- package/dist/plugins/codex-router/index.js.map +5 -5
- package/dist/plugins/deepseek-reasoning-fix/index.js.map +2 -2
- package/dist/plugins/key-access/index.js +1 -1
- package/dist/plugins/key-access/index.js.map +4 -4
- package/dist/plugins/key-access/ingress.js +1 -1
- package/dist/plugins/key-access/ingress.js.map +3 -3
- package/dist/plugins/key-rate-limit/index.js.map +2 -2
- package/dist/plugins/llm-protocol-adapter/control.js +1 -1
- package/dist/plugins/llm-protocol-adapter/control.js.map +3 -3
- package/dist/plugins/llm-protocol-adapter/index.js +1 -1
- package/dist/plugins/llm-protocol-adapter/index.js.map +5 -5
- package/dist/plugins/llm-tps/index.js.map +2 -2
- package/dist/plugins/model-mapping/index.js +1 -1
- package/dist/plugins/model-mapping/index.js.map +4 -4
- package/dist/plugins/models-dev/index.js.map +2 -2
- package/dist/plugins/request-logs/index.js +505 -118
- package/dist/plugins/request-logs/index.js.map +11 -10
- package/dist/plugins/request-logs/ingress.js +3 -23
- package/dist/plugins/request-logs/ingress.js.map +3 -3
- package/dist/plugins/request-logs/ui/RequestLogsPage.svelte +215 -130
- 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/control.js +257 -61
- package/dist/plugins/token-stats/control.js.map +7 -6
- package/dist/plugins/token-stats/index.js +271 -127
- package/dist/plugins/token-stats/index.js.map +8 -8
- package/dist/plugins/token-stats/manifest.json +2 -2
- package/dist/plugins/user-auth/index.js.map +2 -2
- package/package.json +3 -2
|
@@ -776,6 +776,213 @@ function object(value) {
|
|
|
776
776
|
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : undefined;
|
|
777
777
|
}
|
|
778
778
|
|
|
779
|
+
// plugins/request-logs/server/protocols/content.ts
|
|
780
|
+
function index(value) {
|
|
781
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
782
|
+
}
|
|
783
|
+
function responsesIncremental(value) {
|
|
784
|
+
const body = object(value);
|
|
785
|
+
if (!body)
|
|
786
|
+
return false;
|
|
787
|
+
return typeof body.type === "string" && body.type.endsWith(".delta") || Object.entries(body).some(([key, value]) => key === "delta" || (Array.isArray(value) ? value.some(responsesIncremental) : responsesIncremental(value)));
|
|
788
|
+
}
|
|
789
|
+
function geminiFragment(value) {
|
|
790
|
+
const part = object(value);
|
|
791
|
+
return !part || !Object.keys(part).every((key) => ["inlineData", "fileData"].includes(key));
|
|
792
|
+
}
|
|
793
|
+
function geminiIncremental(value) {
|
|
794
|
+
const body = object(value);
|
|
795
|
+
if (!body)
|
|
796
|
+
return false;
|
|
797
|
+
return Object.entries(body).some(([key, value]) => {
|
|
798
|
+
if (key === "toolResponse")
|
|
799
|
+
return false;
|
|
800
|
+
if (key === "clientContent") {
|
|
801
|
+
const content = object(value);
|
|
802
|
+
return !content || Object.keys(content).some((key) => !["turns", "turnComplete"].includes(key)) || content.turns !== undefined && !Array.isArray(content.turns);
|
|
803
|
+
}
|
|
804
|
+
return key === "text" && typeof value === "string" || key === "parts" && (!Array.isArray(value) || value.some(geminiFragment)) || key === "toolCall" || key === "realtimeInput" || (Array.isArray(value) ? value.some(geminiIncremental) : geminiIncremental(value));
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
function emit(sink, direction, value, group, mode, text) {
|
|
808
|
+
if (value === undefined)
|
|
809
|
+
return;
|
|
810
|
+
sink.project(direction, [{ mode, part: text === undefined ? { type: "json", group, value } : { type: "text", group, text } }], text === undefined ? "partial" : "complete");
|
|
811
|
+
}
|
|
812
|
+
function parts(sink, direction, values, prefix, mode, gemini = false, incremental = false) {
|
|
813
|
+
if (!Array.isArray(values)) {
|
|
814
|
+
if (incremental)
|
|
815
|
+
sink.project(direction, [], "partial");
|
|
816
|
+
else
|
|
817
|
+
emit(sink, direction, values, prefix, mode);
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
values.forEach((value, index) => {
|
|
821
|
+
const part = object(value);
|
|
822
|
+
const text = part && typeof part.text === "string" && (gemini ? part.thought !== true : part.type === "output_text" || part.type === "input_text" || part.type === "text") ? part.text : undefined;
|
|
823
|
+
if (incremental && text === undefined && geminiFragment(value)) {
|
|
824
|
+
sink.project(direction, [], "partial");
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
emit(sink, direction, value, `${prefix}/part/${index}${gemini && part?.thought === true ? "/thought" : ""}`, mode, text);
|
|
828
|
+
});
|
|
829
|
+
}
|
|
830
|
+
function projectResponses(sink, direction, value) {
|
|
831
|
+
sink.project(direction, [], "complete");
|
|
832
|
+
const body = object(value);
|
|
833
|
+
if (!body) {
|
|
834
|
+
emit(sink, direction, value, "response", "replace");
|
|
835
|
+
return;
|
|
836
|
+
}
|
|
837
|
+
if (direction === "input") {
|
|
838
|
+
if (typeof body.instructions === "string")
|
|
839
|
+
emit(sink, direction, body.instructions, "instructions", "replace", body.instructions);
|
|
840
|
+
if (typeof body.input === "string")
|
|
841
|
+
emit(sink, direction, body.input, "input", "replace", body.input);
|
|
842
|
+
else if (Array.isArray(body.input))
|
|
843
|
+
body.input.forEach((value, index) => {
|
|
844
|
+
const item = object(value);
|
|
845
|
+
if (item && (item.type === undefined || item.type === "message") && typeof item.content === "string")
|
|
846
|
+
emit(sink, direction, item.content, `input/${index}`, "replace", item.content);
|
|
847
|
+
else if (item && (item.type === undefined || item.type === "message") && Array.isArray(item.content))
|
|
848
|
+
parts(sink, direction, item.content, `input/${index}`, "replace");
|
|
849
|
+
else
|
|
850
|
+
emit(sink, direction, value, `input/${index}`, "replace");
|
|
851
|
+
});
|
|
852
|
+
else
|
|
853
|
+
emit(sink, direction, body, "input", "replace");
|
|
854
|
+
return;
|
|
855
|
+
}
|
|
856
|
+
const type = body.type;
|
|
857
|
+
const incremental = responsesIncremental(body);
|
|
858
|
+
if (incremental && type !== "response.output_text.delta") {
|
|
859
|
+
sink.project(direction, [], "partial");
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
if ((type === "response.content_part.done" || type === "response.output_item.done") && (!index(body.output_index) || type === "response.content_part.done" && !index(body.content_index))) {
|
|
863
|
+
sink.project(direction, [], "partial");
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
if (type === "response.content_part.done") {
|
|
867
|
+
const part = object(body.part);
|
|
868
|
+
emit(sink, direction, body.part, `output/${body.output_index}/part/${body.content_index}`, "replace", part?.type === "output_text" && typeof part.text === "string" ? part.text : undefined);
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
if (type === "response.output_text.delta" || type === "response.output_text.done") {
|
|
872
|
+
if (!index(body.output_index) || !index(body.content_index)) {
|
|
873
|
+
sink.project(direction, [], "partial");
|
|
874
|
+
return;
|
|
875
|
+
}
|
|
876
|
+
const text = type.endsWith(".delta") ? body.delta : body.text;
|
|
877
|
+
if (typeof text === "string")
|
|
878
|
+
emit(sink, direction, text, `output/${body.output_index}/part/${body.content_index}`, type.endsWith(".delta") ? "append" : "replace", text);
|
|
879
|
+
else if (incremental)
|
|
880
|
+
sink.project(direction, [], "partial");
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
const response = object(body.response) ?? body;
|
|
884
|
+
if (Array.isArray(response.output))
|
|
885
|
+
response.output.forEach((value, index) => {
|
|
886
|
+
const item = object(value);
|
|
887
|
+
if (item?.type === "message")
|
|
888
|
+
parts(sink, direction, item.content, `output/${index}`, "replace");
|
|
889
|
+
else
|
|
890
|
+
emit(sink, direction, value, `output/${index}`, "replace");
|
|
891
|
+
});
|
|
892
|
+
else if (type === "response.output_item.done") {
|
|
893
|
+
const item = object(body.item);
|
|
894
|
+
if (item?.type === "message")
|
|
895
|
+
parts(sink, direction, item.content, `output/${body.output_index}`, "replace");
|
|
896
|
+
else
|
|
897
|
+
emit(sink, direction, body.item, `output/${body.output_index}`, "replace");
|
|
898
|
+
} else if (typeof type !== "string" || !["response.created", "response.in_progress", "response.output_item.added", "response.content_part.added", "response.content_part.done"].includes(type))
|
|
899
|
+
emit(sink, direction, body, "native-event", "append");
|
|
900
|
+
}
|
|
901
|
+
function projectGemini(sink, direction, value, streaming = false, prefix = "") {
|
|
902
|
+
sink.project(direction, [], "complete");
|
|
903
|
+
const body = object(value);
|
|
904
|
+
if (!body) {
|
|
905
|
+
if (streaming)
|
|
906
|
+
sink.project(direction, [], "partial");
|
|
907
|
+
else
|
|
908
|
+
emit(sink, direction, value, `${prefix}gemini`, "append");
|
|
909
|
+
return;
|
|
910
|
+
}
|
|
911
|
+
if (direction === "input") {
|
|
912
|
+
const instruction = object(body.systemInstruction);
|
|
913
|
+
if (instruction)
|
|
914
|
+
parts(sink, direction, instruction.parts, `${prefix}system-instruction`, "replace", true);
|
|
915
|
+
const realtime = object(body.realtimeInput);
|
|
916
|
+
if (realtime) {
|
|
917
|
+
if (typeof realtime.text === "string")
|
|
918
|
+
emit(sink, direction, realtime.text, "realtime/text", "append", realtime.text);
|
|
919
|
+
else
|
|
920
|
+
sink.project(direction, [], "partial");
|
|
921
|
+
const { text: _text, ...metadata } = realtime;
|
|
922
|
+
if (Object.keys(metadata).length) {
|
|
923
|
+
if (Object.keys(metadata).some((key) => key !== "audioStreamEnd") || typeof metadata.audioStreamEnd !== "boolean")
|
|
924
|
+
sink.project(direction, [], "partial");
|
|
925
|
+
else
|
|
926
|
+
emit(sink, direction, metadata, `${prefix}realtime`, "append");
|
|
927
|
+
}
|
|
928
|
+
return;
|
|
929
|
+
}
|
|
930
|
+
const content = object(body.clientContent) ?? body;
|
|
931
|
+
const turns = content.turns ?? content.contents;
|
|
932
|
+
if (Array.isArray(turns))
|
|
933
|
+
turns.forEach((value, index) => {
|
|
934
|
+
const turn = object(value);
|
|
935
|
+
if (turn)
|
|
936
|
+
parts(sink, direction, turn.parts, `${prefix}turn/${index}`, "replace", true);
|
|
937
|
+
else
|
|
938
|
+
emit(sink, direction, value, `${prefix}turn/${index}`, "replace");
|
|
939
|
+
});
|
|
940
|
+
else if (object(body.toolResponse))
|
|
941
|
+
emit(sink, direction, body.toolResponse, `${prefix}tool-response`, "append");
|
|
942
|
+
else if (body.clientContent !== undefined || streaming || geminiIncremental(body))
|
|
943
|
+
sink.project(direction, [], "partial");
|
|
944
|
+
else
|
|
945
|
+
emit(sink, direction, body, `${prefix}input`, "append");
|
|
946
|
+
return;
|
|
947
|
+
}
|
|
948
|
+
const server = object(body.serverContent);
|
|
949
|
+
const modelTurn = object(server?.modelTurn);
|
|
950
|
+
if (modelTurn) {
|
|
951
|
+
parts(sink, direction, modelTurn.parts, "model", "append", true, true);
|
|
952
|
+
const { modelTurn: _modelTurn, ...metadata } = server;
|
|
953
|
+
if (Object.keys(metadata).some((key) => !["turnComplete", "generationComplete", "interrupted"].includes(key))) {
|
|
954
|
+
if (geminiIncremental(metadata))
|
|
955
|
+
sink.project(direction, [], "partial");
|
|
956
|
+
else
|
|
957
|
+
emit(sink, direction, metadata, "server-metadata", "append");
|
|
958
|
+
}
|
|
959
|
+
} else if (Array.isArray(body.candidates))
|
|
960
|
+
body.candidates.forEach((value, position) => {
|
|
961
|
+
const candidate = object(value);
|
|
962
|
+
const index = candidate?.index ?? (streaming ? undefined : position);
|
|
963
|
+
if (typeof index !== "number" || !Number.isSafeInteger(index) || index < 0 || index >= 64) {
|
|
964
|
+
if (streaming && geminiIncremental(value))
|
|
965
|
+
sink.project(direction, [], "partial");
|
|
966
|
+
else
|
|
967
|
+
emit(sink, direction, value, "unattributed", "append");
|
|
968
|
+
return;
|
|
969
|
+
}
|
|
970
|
+
const content = object(candidate?.content);
|
|
971
|
+
if (content)
|
|
972
|
+
parts(sink, direction, content.parts, `candidate/${index}`, streaming ? "append" : "replace", true, streaming);
|
|
973
|
+
else if (streaming && geminiIncremental(value))
|
|
974
|
+
sink.project(direction, [], "partial");
|
|
975
|
+
else
|
|
976
|
+
emit(sink, direction, value, `candidate/${index}`, "append");
|
|
977
|
+
});
|
|
978
|
+
else if (!server || Object.keys(server).some((key) => !["turnComplete", "generationComplete", "interrupted"].includes(key))) {
|
|
979
|
+
if (streaming && geminiIncremental(body))
|
|
980
|
+
sink.project(direction, [], "partial");
|
|
981
|
+
else
|
|
982
|
+
emit(sink, direction, body, "native-event", "append");
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
|
|
779
986
|
// plugins/request-logs/server/protocols/gemini.ts
|
|
780
987
|
function candidates(body) {
|
|
781
988
|
if (!Array.isArray(body.candidates))
|
|
@@ -786,11 +993,11 @@ function finishReason(value) {
|
|
|
786
993
|
if (typeof value !== "string" || !/^[A-Z][A-Z_]{1,63}$/.test(value) || value === "FINISH_REASON_UNSPECIFIED")
|
|
787
994
|
return;
|
|
788
995
|
if (value === "STOP")
|
|
789
|
-
return {
|
|
790
|
-
return {
|
|
996
|
+
return { state: "succeeded" };
|
|
997
|
+
return { state: value === "MAX_TOKENS" ? "incomplete" : "failed", code: value };
|
|
791
998
|
}
|
|
792
999
|
function live() {
|
|
793
|
-
const operation = { protocol: "gemini", operation: "bidiGenerateContent" };
|
|
1000
|
+
const operation = { protocol: "gemini", operation: "bidiGenerateContent", requireTerminal: true };
|
|
794
1001
|
let setup = false;
|
|
795
1002
|
let confirmed = false;
|
|
796
1003
|
let current;
|
|
@@ -799,17 +1006,28 @@ function live() {
|
|
|
799
1006
|
return;
|
|
800
1007
|
turn.started = true;
|
|
801
1008
|
return async (records) => {
|
|
802
|
-
turn.id = await records.start({ ...operation, startedAt: turn.startedAt });
|
|
1009
|
+
turn.id = await records.start({ ...operation, startedAt: turn.startedAt, body: turn.capture, requestToken: turn.requestToken });
|
|
1010
|
+
if (!turn.id)
|
|
1011
|
+
turn.capture?.discard();
|
|
803
1012
|
};
|
|
804
1013
|
};
|
|
805
1014
|
return {
|
|
806
1015
|
observe() {},
|
|
807
|
-
prepareWebSocket(event) {
|
|
1016
|
+
prepareWebSocket(event, bodies) {
|
|
808
1017
|
const message = event.message;
|
|
809
|
-
if (event.settled
|
|
1018
|
+
if (event.settled)
|
|
1019
|
+
return;
|
|
1020
|
+
if (event.oversized) {
|
|
1021
|
+
if (event.direction === "client")
|
|
1022
|
+
current?.capture?.discardRequest();
|
|
1023
|
+
else
|
|
1024
|
+
current?.capture?.discardResponse("size_limit");
|
|
1025
|
+
return;
|
|
1026
|
+
}
|
|
1027
|
+
if (!setup && event.direction !== "client")
|
|
810
1028
|
return;
|
|
811
1029
|
if (event.direction === "client") {
|
|
812
|
-
if ((setup ? message.mayContainJsonString?.("clientContent", "realtimeInput") : message.mayContainJsonString?.("setup")) === false)
|
|
1030
|
+
if ((setup ? message.mayContainJsonString?.("clientContent", "realtimeInput", "toolResponse") : message.mayContainJsonString?.("setup")) === false)
|
|
813
1031
|
return;
|
|
814
1032
|
const body = object(message.json());
|
|
815
1033
|
if (!setup) {
|
|
@@ -817,15 +1035,24 @@ function live() {
|
|
|
817
1035
|
setup = typeof model === "string" && model.startsWith("models/") && model.length > 7;
|
|
818
1036
|
} else if (confirmed) {
|
|
819
1037
|
const content = object(body?.clientContent);
|
|
820
|
-
if (content || object(body?.realtimeInput)) {
|
|
821
|
-
|
|
1038
|
+
if (content || object(body?.realtimeInput) || current && object(body?.toolResponse)) {
|
|
1039
|
+
const incremental = geminiIncremental(body);
|
|
1040
|
+
if (!incremental)
|
|
1041
|
+
current?.capture?.append("input", message);
|
|
1042
|
+
current ??= { startedAt: event.timestamp, requestToken: event.requestToken, started: false, interrupted: false, capture: bodies?.start(message), inputs: 0 };
|
|
1043
|
+
if (incremental)
|
|
1044
|
+
current.capture?.discardNative("input", message);
|
|
1045
|
+
if (current.capture)
|
|
1046
|
+
projectGemini(current.capture, "input", body, false, `message/${current.inputs}/`);
|
|
1047
|
+
if (current.inputs < Number.MAX_SAFE_INTEGER)
|
|
1048
|
+
current.inputs++;
|
|
822
1049
|
if (content?.turnComplete === true)
|
|
823
1050
|
return beginTurn(current);
|
|
824
1051
|
}
|
|
825
1052
|
}
|
|
826
1053
|
return;
|
|
827
1054
|
}
|
|
828
|
-
if (message.mayContainJsonString?.(
|
|
1055
|
+
if ((confirmed ? message.mayContainJsonString?.("serverContent", "toolCall", "toolCallCancellation", "error") : message.mayContainJsonString?.("setupComplete")) === false)
|
|
829
1056
|
return;
|
|
830
1057
|
const body = object(message.json({ binary: true }));
|
|
831
1058
|
if (!confirmed) {
|
|
@@ -834,7 +1061,15 @@ function live() {
|
|
|
834
1061
|
}
|
|
835
1062
|
const content = object(body?.serverContent);
|
|
836
1063
|
const turn = current;
|
|
837
|
-
if (!
|
|
1064
|
+
if (!turn || !body)
|
|
1065
|
+
return;
|
|
1066
|
+
if (geminiIncremental(body))
|
|
1067
|
+
turn.capture?.discardNative("output", message);
|
|
1068
|
+
else
|
|
1069
|
+
turn.capture?.append("output", message);
|
|
1070
|
+
if (turn.capture)
|
|
1071
|
+
projectGemini(turn.capture, "output", body, true);
|
|
1072
|
+
if (!content)
|
|
838
1073
|
return;
|
|
839
1074
|
turn.interrupted ||= content.interrupted === true;
|
|
840
1075
|
const complete = content.turnComplete === true;
|
|
@@ -846,7 +1081,7 @@ function live() {
|
|
|
846
1081
|
return async (records) => {
|
|
847
1082
|
await begin?.(records);
|
|
848
1083
|
if (turn.id)
|
|
849
|
-
records.finish(turn.id, {
|
|
1084
|
+
records.finish(turn.id, { state: turn.interrupted ? "cancelled" : "succeeded" });
|
|
850
1085
|
};
|
|
851
1086
|
}
|
|
852
1087
|
};
|
|
@@ -856,32 +1091,39 @@ var gemini = {
|
|
|
856
1091
|
http(context, streaming) {
|
|
857
1092
|
const endpoint = /:(?:streamGenerateContent|generateContent)\/?$/.test(context.path);
|
|
858
1093
|
const outcomes = new Map;
|
|
1094
|
+
let unattributed = false;
|
|
1095
|
+
let unattributedResult;
|
|
859
1096
|
return {
|
|
860
1097
|
operation: streaming ? "streamGenerateContent" : "generateContent",
|
|
861
1098
|
matches(body, _event, confirmed) {
|
|
862
1099
|
const metadata = typeof body.modelVersion === "string" || typeof body.responseId === "string";
|
|
863
1100
|
const feedback = object(body.promptFeedback);
|
|
864
1101
|
const error = object(body.error);
|
|
865
|
-
|
|
1102
|
+
const matched = (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";
|
|
1103
|
+
return matched ? "accepted" : "invalid";
|
|
866
1104
|
},
|
|
867
1105
|
observe(body) {
|
|
868
1106
|
if (object(body.error))
|
|
869
|
-
return {
|
|
1107
|
+
return { state: "failed", code: "upstream_protocol_error" };
|
|
870
1108
|
const block = object(body.promptFeedback)?.blockReason;
|
|
871
1109
|
if (typeof block === "string" && block !== "BLOCK_REASON_UNSPECIFIED")
|
|
872
|
-
return {
|
|
1110
|
+
return { state: "failed", code: block };
|
|
873
1111
|
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
1112
|
const result = finishReason(candidate.finishReason);
|
|
1113
|
+
const index = candidate.index ?? (streaming ? undefined : position);
|
|
1114
|
+
if (typeof index !== "number" || !Number.isInteger(index) || index < 0 || index >= 64) {
|
|
1115
|
+
unattributed = true;
|
|
1116
|
+
if (result?.state === "failed" || result?.state === "incomplete" && unattributedResult?.state !== "failed")
|
|
1117
|
+
unattributedResult = result;
|
|
1118
|
+
continue;
|
|
1119
|
+
}
|
|
878
1120
|
if (result || !outcomes.has(index))
|
|
879
1121
|
outcomes.set(index, result);
|
|
880
1122
|
}
|
|
881
1123
|
},
|
|
882
1124
|
end() {
|
|
883
|
-
const results = [...outcomes.values()];
|
|
884
|
-
return results.find((result) => result?.
|
|
1125
|
+
const results = [...outcomes.values(), unattributedResult];
|
|
1126
|
+
return results.find((result) => result?.state === "failed") ?? results.find((result) => result?.state === "incomplete") ?? (unattributed || [...outcomes.values()].some((result) => !result) ? undefined : outcomes.values().next().value);
|
|
885
1127
|
}
|
|
886
1128
|
};
|
|
887
1129
|
},
|
|
@@ -892,7 +1134,7 @@ var gemini = {
|
|
|
892
1134
|
};
|
|
893
1135
|
|
|
894
1136
|
// plugins/request-logs/server/protocols/responses.ts
|
|
895
|
-
var operation = { protocol: "responses", operation: "response.create" };
|
|
1137
|
+
var operation = { protocol: "responses", operation: "response.create", requireTerminal: true };
|
|
896
1138
|
function identifier(value) {
|
|
897
1139
|
return typeof value === "string" && value.length > 0 && value.length <= 256 ? value : undefined;
|
|
898
1140
|
}
|
|
@@ -907,13 +1149,13 @@ function streamId(body, response) {
|
|
|
907
1149
|
function responseResult(status) {
|
|
908
1150
|
switch (status) {
|
|
909
1151
|
case "completed":
|
|
910
|
-
return {
|
|
1152
|
+
return { state: "succeeded" };
|
|
911
1153
|
case "failed":
|
|
912
|
-
return {
|
|
1154
|
+
return { state: "failed", code: "upstream_protocol_error" };
|
|
913
1155
|
case "incomplete":
|
|
914
|
-
return {
|
|
1156
|
+
return { state: "incomplete", code: "incomplete" };
|
|
915
1157
|
case "cancelled":
|
|
916
|
-
return {
|
|
1158
|
+
return { state: "cancelled" };
|
|
917
1159
|
}
|
|
918
1160
|
}
|
|
919
1161
|
function eventResult(type, response) {
|
|
@@ -922,7 +1164,7 @@ function eventResult(type, response) {
|
|
|
922
1164
|
if (typeof type !== "string" || !type.startsWith("response.") || !response)
|
|
923
1165
|
return;
|
|
924
1166
|
const result = responseResult(type.slice("response.".length));
|
|
925
|
-
if (response.status === undefined || response.status ===
|
|
1167
|
+
if (response.status === undefined || response.status === type.slice("response.".length)) {
|
|
926
1168
|
return result;
|
|
927
1169
|
}
|
|
928
1170
|
}
|
|
@@ -950,31 +1192,60 @@ function observeWebSocket(hosted) {
|
|
|
950
1192
|
const start = (key, current) => {
|
|
951
1193
|
operations.set(key, current);
|
|
952
1194
|
return async (records) => {
|
|
953
|
-
|
|
1195
|
+
if (current.abandoned)
|
|
1196
|
+
return;
|
|
1197
|
+
const id = await records.start({ ...operation, body: current.capture, startedAt: current.startedAt, requestToken: current.requestToken });
|
|
1198
|
+
if (current.abandoned)
|
|
1199
|
+
return;
|
|
1200
|
+
current.id = id;
|
|
954
1201
|
if (current.id)
|
|
955
1202
|
return;
|
|
956
|
-
current.capture
|
|
1203
|
+
const capture = current.capture;
|
|
1204
|
+
current.capture = undefined;
|
|
1205
|
+
capture?.discard();
|
|
957
1206
|
if (operations.get(key) === current)
|
|
958
1207
|
operations.delete(key);
|
|
959
1208
|
};
|
|
960
1209
|
};
|
|
961
1210
|
const finish = (key, current, result) => {
|
|
962
|
-
|
|
1211
|
+
if (hosted)
|
|
1212
|
+
current.settled = true;
|
|
1213
|
+
else
|
|
1214
|
+
operations.delete(key);
|
|
963
1215
|
return (records) => {
|
|
964
|
-
|
|
965
|
-
|
|
1216
|
+
try {
|
|
1217
|
+
if (!current.abandoned && current.id)
|
|
1218
|
+
records.finish(current.id, result);
|
|
1219
|
+
} finally {
|
|
1220
|
+
if (hosted && operations.get(key) === current)
|
|
1221
|
+
operations.delete(key);
|
|
1222
|
+
}
|
|
966
1223
|
};
|
|
967
1224
|
};
|
|
968
1225
|
return {
|
|
1226
|
+
...hosted ? {
|
|
1227
|
+
abandonWebSocketOperation(operationId) {
|
|
1228
|
+
const current = operations.get(operationId);
|
|
1229
|
+
if (!current)
|
|
1230
|
+
return;
|
|
1231
|
+
current.abandoned = true;
|
|
1232
|
+
operations.delete(operationId);
|
|
1233
|
+
const capture = current.capture;
|
|
1234
|
+
current.capture = undefined;
|
|
1235
|
+
capture?.discard();
|
|
1236
|
+
}
|
|
1237
|
+
} : {},
|
|
969
1238
|
observe() {},
|
|
970
1239
|
prepareWebSocket(event, bodies) {
|
|
971
1240
|
const { relatedOperationId } = event;
|
|
972
1241
|
const message = event.message;
|
|
973
1242
|
const hostedOperation = hosted && relatedOperationId ? operations.get(relatedOperationId) : undefined;
|
|
1243
|
+
if (hostedOperation?.settled)
|
|
1244
|
+
return;
|
|
974
1245
|
if (event.settled) {
|
|
975
1246
|
if (!hostedOperation || !relatedOperationId)
|
|
976
1247
|
return;
|
|
977
|
-
return finish(relatedOperationId, hostedOperation
|
|
1248
|
+
return finish(relatedOperationId, hostedOperation);
|
|
978
1249
|
}
|
|
979
1250
|
if (message.kind !== "text")
|
|
980
1251
|
return;
|
|
@@ -1015,17 +1286,28 @@ function observeWebSocket(hosted) {
|
|
|
1015
1286
|
if (event.direction === "client") {
|
|
1016
1287
|
if (body.type !== "response.create")
|
|
1017
1288
|
return;
|
|
1289
|
+
const capture = bodies?.start(message);
|
|
1290
|
+
if (capture)
|
|
1291
|
+
projectResponses(capture, "input", body);
|
|
1018
1292
|
if (hosted) {
|
|
1293
|
+
if (operations.size >= MAX_OPERATIONS || relatedOperationId && operations.has(relatedOperationId)) {
|
|
1294
|
+
capture?.discard();
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1019
1297
|
if (relatedOperationId)
|
|
1020
|
-
return start(relatedOperationId, { stream: undefined, startedAt: event.timestamp, capture
|
|
1298
|
+
return start(relatedOperationId, { stream: undefined, startedAt: event.timestamp, capture });
|
|
1299
|
+
capture?.discard();
|
|
1021
1300
|
return;
|
|
1022
1301
|
}
|
|
1023
1302
|
const stream = streamId(body);
|
|
1024
|
-
if (overflow || stream === null || uncertain.has(stream))
|
|
1303
|
+
if (overflow || stream === null || uncertain.has(stream)) {
|
|
1304
|
+
capture?.discard();
|
|
1025
1305
|
return;
|
|
1306
|
+
}
|
|
1026
1307
|
const existing = pending.findIndex((candidate) => candidate.stream === stream);
|
|
1027
1308
|
if (existing !== -1) {
|
|
1028
1309
|
pending.splice(existing, 1)[0].capture?.discard();
|
|
1310
|
+
capture?.discard();
|
|
1029
1311
|
uncertain.add(stream);
|
|
1030
1312
|
return;
|
|
1031
1313
|
}
|
|
@@ -1034,9 +1316,10 @@ function observeWebSocket(hosted) {
|
|
|
1034
1316
|
for (const candidate of pending)
|
|
1035
1317
|
candidate.capture?.discard();
|
|
1036
1318
|
pending.length = 0;
|
|
1319
|
+
capture?.discard();
|
|
1037
1320
|
return;
|
|
1038
1321
|
}
|
|
1039
|
-
pending.push({ stream, startedAt: event.timestamp, capture:
|
|
1322
|
+
pending.push({ stream, startedAt: event.timestamp, capture, requestToken: event.requestToken });
|
|
1040
1323
|
return;
|
|
1041
1324
|
}
|
|
1042
1325
|
const response = object(body.response);
|
|
@@ -1044,14 +1327,17 @@ function observeWebSocket(hosted) {
|
|
|
1044
1327
|
if (hosted) {
|
|
1045
1328
|
if (!hostedOperation)
|
|
1046
1329
|
return;
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1330
|
+
if (responsesIncremental(body))
|
|
1331
|
+
hostedOperation.capture?.discardNative("output", message);
|
|
1332
|
+
else
|
|
1333
|
+
hostedOperation.capture?.append("output", message);
|
|
1334
|
+
if (hostedOperation.capture)
|
|
1335
|
+
projectResponses(hostedOperation.capture, "output", body);
|
|
1336
|
+
if (!event.hostedErrorOutcome && result && body.type !== "error") {
|
|
1337
|
+
return (records) => {
|
|
1338
|
+
if (!hostedOperation.abandoned && hostedOperation.id)
|
|
1339
|
+
records.finish(hostedOperation.id, result);
|
|
1052
1340
|
};
|
|
1053
|
-
} else if (result && body.type !== "error") {
|
|
1054
|
-
hostedOperation.result = result;
|
|
1055
1341
|
}
|
|
1056
1342
|
return;
|
|
1057
1343
|
}
|
|
@@ -1091,8 +1377,14 @@ function observeWebSocket(hosted) {
|
|
|
1091
1377
|
for (const current of possible) {
|
|
1092
1378
|
if (ambiguous || result || isOpeningType(body.type))
|
|
1093
1379
|
current.capture?.discardResponse();
|
|
1094
|
-
else
|
|
1095
|
-
|
|
1380
|
+
else {
|
|
1381
|
+
if (responsesIncremental(body))
|
|
1382
|
+
current.capture?.discardNative("output", message);
|
|
1383
|
+
else
|
|
1384
|
+
current.capture?.append("output", message);
|
|
1385
|
+
if (current.capture)
|
|
1386
|
+
projectResponses(current.capture, "output", body);
|
|
1387
|
+
}
|
|
1096
1388
|
}
|
|
1097
1389
|
return;
|
|
1098
1390
|
}
|
|
@@ -1100,7 +1392,12 @@ function observeWebSocket(hosted) {
|
|
|
1100
1392
|
if (established) {
|
|
1101
1393
|
if (stream === undefined || stream === established.stream) {
|
|
1102
1394
|
if (typeof body.type === "string" && (body.type.startsWith("response.") || body.type === "error")) {
|
|
1103
|
-
|
|
1395
|
+
if (responsesIncremental(body))
|
|
1396
|
+
established.capture?.discardNative("output", message);
|
|
1397
|
+
else
|
|
1398
|
+
established.capture?.append("output", message);
|
|
1399
|
+
if (established.capture)
|
|
1400
|
+
projectResponses(established.capture, "output", body);
|
|
1104
1401
|
}
|
|
1105
1402
|
if (result)
|
|
1106
1403
|
return finish(responseId, established, result);
|
|
@@ -1128,7 +1425,12 @@ function observeWebSocket(hosted) {
|
|
|
1128
1425
|
return;
|
|
1129
1426
|
}
|
|
1130
1427
|
const current = pending.splice(index, 1)[0];
|
|
1131
|
-
|
|
1428
|
+
if (responsesIncremental(body))
|
|
1429
|
+
current.capture?.discardNative("output", message);
|
|
1430
|
+
else
|
|
1431
|
+
current.capture?.append("output", message);
|
|
1432
|
+
if (current.capture)
|
|
1433
|
+
projectResponses(current.capture, "output", body);
|
|
1132
1434
|
return start(responseId, current);
|
|
1133
1435
|
}
|
|
1134
1436
|
};
|
|
@@ -1144,36 +1446,40 @@ var responses = {
|
|
|
1144
1446
|
const error = object(body.error);
|
|
1145
1447
|
const errorEnvelope = typeof error?.message === "string" && (typeof error.type === "string" || typeof error.code === "string");
|
|
1146
1448
|
if (!streaming)
|
|
1147
|
-
return body.object === "response" && identifier(body.id) !== undefined || endpoint && errorEnvelope;
|
|
1449
|
+
return body.object === "response" && identifier(body.id) !== undefined || endpoint && errorEnvelope ? "accepted" : "invalid";
|
|
1148
1450
|
const type = body.type ?? event;
|
|
1451
|
+
if (body.type !== undefined && event && event !== "message" && body.type !== event)
|
|
1452
|
+
return "invalid";
|
|
1453
|
+
const response = object(body.response);
|
|
1454
|
+
if (body.response_id !== undefined && (!identifier(body.response_id) || response?.id !== undefined && body.response_id !== response.id))
|
|
1455
|
+
return "invalid";
|
|
1149
1456
|
if (type === "error") {
|
|
1150
1457
|
const errorResponseId = identifier(body.response_id);
|
|
1151
1458
|
if (responseId && errorResponseId && errorResponseId !== responseId)
|
|
1152
|
-
return
|
|
1459
|
+
return "invalid";
|
|
1153
1460
|
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;
|
|
1461
|
+
return openAIError || confirmed && errorEnvelope ? "accepted" : "invalid";
|
|
1155
1462
|
}
|
|
1156
1463
|
if (typeof type !== "string" || !type.startsWith("response."))
|
|
1157
|
-
return
|
|
1158
|
-
const response = object(body.response);
|
|
1464
|
+
return "invalid";
|
|
1159
1465
|
const id = identifier(response?.id ?? body.response_id);
|
|
1160
|
-
if (responseId && id && id !== responseId)
|
|
1161
|
-
return false;
|
|
1162
1466
|
const opening = isOpeningType(type);
|
|
1163
1467
|
const terminal = responseResult(type.slice("response.".length)) !== undefined;
|
|
1468
|
+
if (responseId && id && id !== responseId) {
|
|
1469
|
+
return confirmed && terminal && response?.object === "response" && identifier(response.id) === id && response.status === type.slice("response.".length) && eventResult(type, response) && !responsesIncremental(body) ? "foreign-terminal" : "invalid";
|
|
1470
|
+
}
|
|
1164
1471
|
if (opening || terminal) {
|
|
1165
1472
|
if (!id || response?.object !== "response" || terminal && !eventResult(type, response))
|
|
1166
|
-
return
|
|
1473
|
+
return "invalid";
|
|
1167
1474
|
responseId ??= id;
|
|
1168
|
-
return
|
|
1475
|
+
return "accepted";
|
|
1169
1476
|
}
|
|
1170
|
-
return confirmed;
|
|
1477
|
+
return confirmed ? "accepted" : "invalid";
|
|
1171
1478
|
},
|
|
1172
1479
|
observe(body, event) {
|
|
1173
1480
|
if (!streaming)
|
|
1174
1481
|
return responseResult(body.object === "response" ? body.status : "failed");
|
|
1175
|
-
|
|
1176
|
-
return result?.outcome === "completed" ? { ...result, acceptClientDisconnect: true } : result;
|
|
1482
|
+
return eventResult(body.type ?? event, object(body.response));
|
|
1177
1483
|
}
|
|
1178
1484
|
};
|
|
1179
1485
|
},
|
|
@@ -1200,20 +1506,52 @@ function streamBodyParser(context) {
|
|
|
1200
1506
|
async* stream(body) {
|
|
1201
1507
|
const candidates = httpCandidates(context, true);
|
|
1202
1508
|
let decoder;
|
|
1509
|
+
let protocol;
|
|
1203
1510
|
let first = true;
|
|
1204
1511
|
for await (const frame of body.events()) {
|
|
1205
|
-
if (frame.
|
|
1512
|
+
if (frame.hasData === false)
|
|
1206
1513
|
continue;
|
|
1514
|
+
if (frame.truncated) {
|
|
1515
|
+
body.discardNative();
|
|
1516
|
+
body.project("output", [], "partial");
|
|
1517
|
+
continue;
|
|
1518
|
+
}
|
|
1207
1519
|
const value = object(frame.json);
|
|
1208
|
-
if (!value)
|
|
1520
|
+
if (!value) {
|
|
1521
|
+
if (frame.data.trim() !== "[DONE]") {
|
|
1522
|
+
body.discardNative();
|
|
1523
|
+
body.project("output", [], "partial");
|
|
1524
|
+
}
|
|
1209
1525
|
continue;
|
|
1526
|
+
}
|
|
1210
1527
|
if (decoder) {
|
|
1211
|
-
|
|
1528
|
+
const classification = decoder.matches(value, frame.event, true);
|
|
1529
|
+
if (classification === "foreign-terminal")
|
|
1212
1530
|
continue;
|
|
1531
|
+
if (classification === "invalid") {
|
|
1532
|
+
body.discardNative();
|
|
1533
|
+
body.project("output", [], "partial");
|
|
1534
|
+
continue;
|
|
1535
|
+
}
|
|
1213
1536
|
} else {
|
|
1214
|
-
|
|
1215
|
-
|
|
1537
|
+
const candidate = candidates.find((candidate) => candidate.decoder.matches(value, frame.event, false) === "accepted");
|
|
1538
|
+
decoder = candidate?.decoder;
|
|
1539
|
+
protocol = candidate?.name;
|
|
1540
|
+
if (!decoder) {
|
|
1541
|
+
body.discardNative();
|
|
1542
|
+
body.project("output", [], "partial");
|
|
1216
1543
|
continue;
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
if (protocol === "responses") {
|
|
1547
|
+
const event = { ...value, type: value.type ?? frame.event };
|
|
1548
|
+
if (responsesIncremental(event))
|
|
1549
|
+
body.discardNative();
|
|
1550
|
+
projectResponses(body, "output", event);
|
|
1551
|
+
} else if (protocol === "gemini") {
|
|
1552
|
+
if (geminiIncremental(value))
|
|
1553
|
+
body.discardNative();
|
|
1554
|
+
projectGemini(body, "output", value, true);
|
|
1217
1555
|
}
|
|
1218
1556
|
yield (first ? "[" : ",") + JSON.stringify({ event: frame.event || "message", data: value });
|
|
1219
1557
|
first = false;
|
|
@@ -1224,6 +1562,8 @@ function streamBodyParser(context) {
|
|
|
1224
1562
|
};
|
|
1225
1563
|
}
|
|
1226
1564
|
async function observeHttp(context, event, records) {
|
|
1565
|
+
if (event.responseOrigin !== "upstream")
|
|
1566
|
+
return;
|
|
1227
1567
|
const type = mediaType(event.contentType);
|
|
1228
1568
|
const streaming = type === "text/event-stream";
|
|
1229
1569
|
if (!streaming && !isJson(type))
|
|
@@ -1232,15 +1572,19 @@ async function observeHttp(context, event, records) {
|
|
|
1232
1572
|
const consumer = { id: "request-log-protocol", mandatory: false, signal: event.signal };
|
|
1233
1573
|
if (!streaming) {
|
|
1234
1574
|
const bodyHandle = event.body;
|
|
1235
|
-
const body = object(await bodyHandle.json(consumer))
|
|
1575
|
+
const body = object(await bodyHandle.json(consumer).catch((error) => {
|
|
1576
|
+
if (error instanceof Error && "code" in error && error.code === "invalid_json_body")
|
|
1577
|
+
return;
|
|
1578
|
+
throw error;
|
|
1579
|
+
}));
|
|
1236
1580
|
if (!body)
|
|
1237
1581
|
return;
|
|
1238
|
-
const candidate = candidates.find(({ decoder }) => decoder.matches(body, undefined, false));
|
|
1582
|
+
const candidate = candidates.find(({ decoder }) => decoder.matches(body, undefined, false) === "accepted");
|
|
1239
1583
|
if (!candidate)
|
|
1240
1584
|
return;
|
|
1241
|
-
const id = await records.start({ protocol: candidate.name, operation: candidate.decoder.operation });
|
|
1585
|
+
const id = await records.start({ protocol: candidate.name, operation: candidate.decoder.operation, requireTerminal: true });
|
|
1242
1586
|
const result = candidate.decoder.observe(body, undefined) ?? candidate.decoder.end?.();
|
|
1243
|
-
if (id
|
|
1587
|
+
if (id)
|
|
1244
1588
|
records.finish(id, result);
|
|
1245
1589
|
return;
|
|
1246
1590
|
}
|
|
@@ -1253,23 +1597,23 @@ async function observeHttp(context, event, records) {
|
|
|
1253
1597
|
if (!body)
|
|
1254
1598
|
continue;
|
|
1255
1599
|
if (!decoder) {
|
|
1256
|
-
const candidate = candidates.find((candidate) => candidate.decoder.matches(body, frame.event, false));
|
|
1600
|
+
const candidate = candidates.find((candidate) => candidate.decoder.matches(body, frame.event, false) === "accepted");
|
|
1257
1601
|
if (!candidate)
|
|
1258
1602
|
continue;
|
|
1259
1603
|
decoder = candidate.decoder;
|
|
1260
|
-
id = await records.start({ protocol: candidate.name, operation: decoder.operation });
|
|
1604
|
+
id = await records.start({ protocol: candidate.name, operation: decoder.operation, requireTerminal: true });
|
|
1261
1605
|
if (!id)
|
|
1262
1606
|
return;
|
|
1263
|
-
} else if (
|
|
1607
|
+
} else if (decoder.matches(body, frame.event, true) !== "accepted")
|
|
1264
1608
|
continue;
|
|
1265
1609
|
const result = decoder.observe(body, frame.event);
|
|
1266
1610
|
if (result && id) {
|
|
1267
|
-
records.finish(id, result);
|
|
1611
|
+
records.finish(id, result, frame);
|
|
1268
1612
|
return;
|
|
1269
1613
|
}
|
|
1270
1614
|
}
|
|
1271
1615
|
const result = decoder?.end?.();
|
|
1272
|
-
if (id
|
|
1616
|
+
if (id)
|
|
1273
1617
|
records.finish(id, result);
|
|
1274
1618
|
}
|
|
1275
1619
|
function applicationObserver(context) {
|
|
@@ -1299,6 +1643,17 @@ function applicationObserver(context) {
|
|
|
1299
1643
|
}
|
|
1300
1644
|
};
|
|
1301
1645
|
return {
|
|
1646
|
+
...context.webSocketMode && observers.length > 0 && observers.every((observer) => typeof observer.abandonWebSocketOperation === "function") ? {
|
|
1647
|
+
abandonWebSocketOperation(operationId) {
|
|
1648
|
+
for (const observer of observers) {
|
|
1649
|
+
const result = observer.abandonWebSocketOperation(operationId);
|
|
1650
|
+
if (result !== undefined) {
|
|
1651
|
+
Promise.resolve(result).catch(() => {});
|
|
1652
|
+
throw new Error("request_log_abandon_must_be_synchronous");
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
} : {},
|
|
1302
1657
|
observe() {},
|
|
1303
1658
|
prepareWebSocket(event, bodies) {
|
|
1304
1659
|
const deliveries = (selected ? [selected] : observers).flatMap((observer) => {
|
|
@@ -1314,53 +1669,85 @@ function applicationObserver(context) {
|
|
|
1314
1669
|
// plugins/request-logs/server/body-parsers.ts
|
|
1315
1670
|
var import_stream_json = __toESM(require_stream_json());
|
|
1316
1671
|
import { finished } from "stream/promises";
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
if (
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1672
|
+
function json(context) {
|
|
1673
|
+
return {
|
|
1674
|
+
async* stream(body) {
|
|
1675
|
+
const direction = context.stage === "client-response" ? "output" : "input";
|
|
1676
|
+
const project = /\/responses\/?$/.test(context.path) ? projectResponses : /:(?:streamGenerateContent|generateContent)\/?$/.test(context.path) ? projectGemini : undefined;
|
|
1677
|
+
let fragments = project && body.projectionByteLimit > 0 ? [] : undefined;
|
|
1678
|
+
let projectionBytes = 0;
|
|
1679
|
+
if (project && !fragments)
|
|
1680
|
+
body.project(direction, [], "partial");
|
|
1681
|
+
const parser = new import_stream_json.Parser({ packValues: false, streamValues: true });
|
|
1682
|
+
const done = finished(parser);
|
|
1683
|
+
done.catch(() => {});
|
|
1684
|
+
let depth = 0;
|
|
1685
|
+
parser.on("data", ({ name }) => {
|
|
1686
|
+
if (name === "startArray" || name === "startObject") {
|
|
1687
|
+
if (++depth > 1024)
|
|
1688
|
+
parser.destroy(new Error("JSON nesting limit exceeded"));
|
|
1689
|
+
} else if (name === "endArray" || name === "endObject")
|
|
1690
|
+
depth--;
|
|
1691
|
+
});
|
|
1692
|
+
let inString = false;
|
|
1693
|
+
let escaped = false;
|
|
1694
|
+
try {
|
|
1695
|
+
for await (const part of body.textChunks()) {
|
|
1696
|
+
let escapedIndex = escaped ? 0 : -1;
|
|
1697
|
+
for (const match of part.matchAll(/["\\\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\uffff]/g)) {
|
|
1698
|
+
if (match.index === escapedIndex)
|
|
1699
|
+
continue;
|
|
1700
|
+
if (inString && match[0] === "\\")
|
|
1701
|
+
escapedIndex = match.index + 1;
|
|
1702
|
+
else if (match[0] === '"')
|
|
1703
|
+
inString = !inString;
|
|
1704
|
+
else if (!inString)
|
|
1705
|
+
throw new Error("Invalid character outside JSON string");
|
|
1706
|
+
}
|
|
1707
|
+
escaped = escapedIndex === part.length;
|
|
1708
|
+
if (!part)
|
|
1337
1709
|
continue;
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1710
|
+
await new Promise((resolve, reject) => parser.write(part, (error) => error ? reject(error) : resolve()));
|
|
1711
|
+
if (fragments) {
|
|
1712
|
+
projectionBytes += Buffer.byteLength(part, "utf8");
|
|
1713
|
+
if (projectionBytes > body.projectionByteLimit) {
|
|
1714
|
+
fragments = undefined;
|
|
1715
|
+
body.project(direction, [], "partial");
|
|
1716
|
+
} else
|
|
1717
|
+
fragments.push(part);
|
|
1718
|
+
}
|
|
1719
|
+
yield part;
|
|
1344
1720
|
}
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1721
|
+
parser.end();
|
|
1722
|
+
await done;
|
|
1723
|
+
if (project && fragments) {
|
|
1724
|
+
try {
|
|
1725
|
+
const value = JSON.parse(fragments.join(""));
|
|
1726
|
+
fragments = undefined;
|
|
1727
|
+
project(body, direction, value);
|
|
1728
|
+
} catch {
|
|
1729
|
+
body.project(direction, [], "partial");
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
} finally {
|
|
1733
|
+
fragments = undefined;
|
|
1734
|
+
parser.destroy();
|
|
1350
1735
|
}
|
|
1351
|
-
parser.end();
|
|
1352
|
-
await done;
|
|
1353
|
-
} finally {
|
|
1354
|
-
parser.destroy();
|
|
1355
1736
|
}
|
|
1356
|
-
}
|
|
1357
|
-
}
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1737
|
+
};
|
|
1738
|
+
}
|
|
1739
|
+
function text(context) {
|
|
1740
|
+
return {
|
|
1741
|
+
format: "text",
|
|
1742
|
+
async* stream(body) {
|
|
1743
|
+
body.project(context.stage === "client-response" ? "output" : "input", [], "complete");
|
|
1744
|
+
for await (const text of body.textChunks()) {
|
|
1745
|
+
body.project(context.stage === "client-response" ? "output" : "input", [{ mode: "append", part: { type: "text", text } }], "complete");
|
|
1746
|
+
yield text;
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
};
|
|
1750
|
+
}
|
|
1364
1751
|
|
|
1365
1752
|
// plugins/request-logs/server/index.ts
|
|
1366
1753
|
var server_default = definePlugin(class {
|
|
@@ -1377,9 +1764,9 @@ var server_default = definePlugin(class {
|
|
|
1377
1764
|
return;
|
|
1378
1765
|
const type = mediaType(context.contentType);
|
|
1379
1766
|
if (type === "application/json" || type.endsWith("+json"))
|
|
1380
|
-
return json;
|
|
1767
|
+
return json(context);
|
|
1381
1768
|
if (type === "text/plain")
|
|
1382
|
-
return text;
|
|
1769
|
+
return text(context);
|
|
1383
1770
|
return streamBodyParser(context);
|
|
1384
1771
|
});
|
|
1385
1772
|
hooks.onRequestLogApplication.tap(fallback, applicationObserver);
|
|
@@ -1389,4 +1776,4 @@ export {
|
|
|
1389
1776
|
server_default as default
|
|
1390
1777
|
};
|
|
1391
1778
|
|
|
1392
|
-
//# debugId=
|
|
1779
|
+
//# debugId=1530CC57F102D7EA64756E2164756E21
|