@omnicross/core 0.1.9 → 0.2.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/LICENSE +21 -21
- package/NOTICE +57 -57
- package/README.md +22 -15
- package/dist/{CompletionService-kNA17cfI.d.cts → CompletionService-BiJqftc-.d.cts} +1 -1
- package/dist/{CompletionService-C3BX0Ovy.d.ts → CompletionService-NXN_Av5W.d.ts} +1 -1
- package/dist/{ProviderProxy-QdwNV6l1.d.cts → ProviderProxy-CgIcCVwk.d.cts} +1 -1
- package/dist/{ProviderProxy--ROec6VI.d.ts → ProviderProxy-DYoY5XoT.d.ts} +1 -1
- package/dist/auth/GeminiCodeAssistProjectResolver.cjs +6 -6
- package/dist/auth/GeminiCodeAssistProjectResolver.js +4 -4
- package/dist/chunk-32SRZFFP.cjs +153 -0
- package/dist/chunk-4JOU54M5.js +200 -0
- package/dist/{chunk-2UTGX6FM.js → chunk-4VVCXVB7.js} +1 -1
- package/dist/{chunk-EPN67EGP.cjs → chunk-5UK3KLE2.cjs} +9 -1
- package/dist/chunk-6DDYNS2B.cjs +384 -0
- package/dist/{chunk-HKB656B4.js → chunk-6OC3ORW6.js} +55 -287
- package/dist/chunk-6OKXH2TW.js +320 -0
- package/dist/{chunk-XELFM7KL.cjs → chunk-7B56AJBB.cjs} +6275 -2421
- package/dist/{chunk-YWL7YZWS.cjs → chunk-7LRTPNFL.cjs} +5 -0
- package/dist/{chunk-CCVRRJOX.js → chunk-7PIUVCKK.js} +9 -2
- package/dist/{chunk-IIRPUVPH.cjs → chunk-7ZHBN75N.cjs} +3 -1
- package/dist/{chunk-UAPBLNN2.js → chunk-AQDON6MB.js} +8 -0
- package/dist/{chunk-RJ5ZGNI3.js → chunk-CFSGYFOZ.js} +1 -1
- package/dist/chunk-CTKM773J.cjs +3919 -0
- package/dist/chunk-DEIUR2TH.js +3919 -0
- package/dist/{chunk-WNKWAEUR.js → chunk-EYGBM2VC.js} +3 -1
- package/dist/{chunk-NGYOO5TO.js → chunk-FJNKXOWE.js} +9 -9
- package/dist/chunk-FX325I5Q.cjs +320 -0
- package/dist/chunk-G4CZIUIB.cjs +200 -0
- package/dist/chunk-HCOQQAZE.cjs +123 -0
- package/dist/{chunk-55FMGZ3L.js → chunk-HN74IGEO.js} +1 -1
- package/dist/{chunk-ZY6KU6P3.js → chunk-HOMUGKHR.js} +8 -5
- package/dist/{chunk-AOFWNWLT.cjs → chunk-IUL4VWNU.cjs} +22 -1
- package/dist/{chunk-5LNYGCIW.cjs → chunk-KIQ2HA2A.cjs} +5 -5
- package/dist/{chunk-MUHWHQVM.cjs → chunk-KQBLZMP6.cjs} +4 -3
- package/dist/chunk-LFEH7CEO.js +384 -0
- package/dist/chunk-N76KU6F5.js +123 -0
- package/dist/{chunk-CMVXX7ON.cjs → chunk-NOITAVHA.cjs} +114 -58
- package/dist/{chunk-Q27JY5RG.cjs → chunk-OIKREZVQ.cjs} +7 -4
- package/dist/{chunk-RJUONLUQ.cjs → chunk-OST5RJOJ.cjs} +1 -1
- package/dist/chunk-QIIAMBTM.js +153 -0
- package/dist/{chunk-56QD3YW6.cjs → chunk-QWNUOR7F.cjs} +11 -4
- package/dist/{chunk-S3DYFEBE.js → chunk-R4VCO2CB.js} +10 -1
- package/dist/{chunk-ZQQUD66M.cjs → chunk-SQMAEDNE.cjs} +63 -295
- package/dist/{chunk-4VZX5F4T.cjs → chunk-T6NWPIYZ.cjs} +5 -2
- package/dist/{chunk-P7EM6BND.cjs → chunk-TVYA5JSQ.cjs} +10 -10
- package/dist/{chunk-EBLFCNPY.js → chunk-U47LQXPB.js} +5 -2
- package/dist/{chunk-N7OOJI5V.js → chunk-V2UKUTJ3.js} +5 -0
- package/dist/{chunk-45FHEOKW.js → chunk-V53ZEFXC.js} +92 -36
- package/dist/{chunk-OC6DOOFX.cjs → chunk-VXRTQZF2.cjs} +5 -5
- package/dist/{chunk-CQIJCPMF.js → chunk-WSJE42SG.js} +5149 -1295
- package/dist/{chunk-RICPANBY.cjs → chunk-WX2E2G7H.cjs} +13 -4
- package/dist/{chunk-DE2SRBFZ.js → chunk-X335TJNS.js} +4 -3
- package/dist/{chunk-N2EIM2XJ.js → chunk-ZLNHPUFA.js} +22 -1
- package/dist/completion/CompletionService.cjs +30 -27
- package/dist/completion/CompletionService.d.cts +3 -2
- package/dist/completion/CompletionService.d.ts +3 -2
- package/dist/completion/CompletionService.js +29 -26
- package/dist/completion.cjs +38 -35
- package/dist/completion.d.cts +3 -2
- package/dist/completion.d.ts +3 -2
- package/dist/completion.js +37 -34
- package/dist/image-generation.cjs +52 -0
- package/dist/image-generation.d.cts +93 -0
- package/dist/image-generation.d.ts +93 -0
- package/dist/image-generation.js +52 -0
- package/dist/index.cjs +126 -31
- package/dist/index.d.cts +11 -7
- package/dist/index.d.ts +11 -7
- package/dist/index.js +143 -48
- package/dist/openai-operation.cjs +18 -0
- package/dist/openai-operation.d.cts +19 -0
- package/dist/openai-operation.d.ts +19 -0
- package/dist/openai-operation.js +18 -0
- package/dist/outbound-api/auditCapture.cjs +3 -3
- package/dist/outbound-api/auditCapture.d.cts +19 -2
- package/dist/outbound-api/auditCapture.d.ts +19 -2
- package/dist/outbound-api/auditCapture.js +2 -2
- package/dist/outbound-api/auditRedact.cjs +2 -2
- package/dist/outbound-api/auditRedact.js +1 -1
- package/dist/outbound-api/routeResolver.cjs +2 -2
- package/dist/outbound-api/routeResolver.d.cts +7 -4
- package/dist/outbound-api/routeResolver.d.ts +7 -4
- package/dist/outbound-api/routeResolver.js +1 -1
- package/dist/outbound-api/subscriptionRegistryPort.d.cts +2 -1
- package/dist/outbound-api/subscriptionRegistryPort.d.ts +2 -1
- package/dist/outbound-api/types.d.cts +6 -3
- package/dist/outbound-api/types.d.ts +6 -3
- package/dist/outbound-api.cjs +49 -32
- package/dist/outbound-api.d.cts +97 -10
- package/dist/outbound-api.d.ts +97 -10
- package/dist/outbound-api.js +48 -31
- package/dist/pipeline/AccountAllowanceScheduling.d.cts +18 -15
- package/dist/pipeline/AccountAllowanceScheduling.d.ts +18 -15
- package/dist/pipeline/LlmConfigProviderAuth.cjs +3 -3
- package/dist/pipeline/LlmConfigProviderAuth.js +2 -2
- package/dist/pipeline/SubscriptionAccountHealth.cjs +2 -2
- package/dist/pipeline/SubscriptionAccountHealth.d.cts +7 -0
- package/dist/pipeline/SubscriptionAccountHealth.d.ts +7 -0
- package/dist/pipeline/SubscriptionAccountHealth.js +1 -1
- package/dist/pipeline/executeProviderCall.cjs +2 -2
- package/dist/pipeline/executeProviderCall.d.cts +11 -6
- package/dist/pipeline/executeProviderCall.d.ts +11 -6
- package/dist/pipeline/executeProviderCall.js +1 -1
- package/dist/pipeline/upstreamFetch.cjs +2 -2
- package/dist/pipeline/upstreamFetch.d.cts +5 -0
- package/dist/pipeline/upstreamFetch.d.ts +5 -0
- package/dist/pipeline/upstreamFetch.js +1 -1
- package/dist/ports.d.cts +6 -3
- package/dist/ports.d.ts +6 -3
- package/dist/provider-proxy/ProviderProxy.cjs +30 -27
- package/dist/provider-proxy/ProviderProxy.d.cts +3 -2
- package/dist/provider-proxy/ProviderProxy.d.ts +3 -2
- package/dist/provider-proxy/ProviderProxy.js +29 -26
- package/dist/provider-proxy/identity/codexCliHeaders.cjs +4 -2
- package/dist/provider-proxy/identity/codexCliHeaders.d.cts +26 -6
- package/dist/provider-proxy/identity/codexCliHeaders.d.ts +26 -6
- package/dist/provider-proxy/identity/codexCliHeaders.js +5 -3
- package/dist/provider-proxy/ingress/providerProxyShared.cjs +32 -27
- package/dist/provider-proxy/ingress/providerProxyShared.d.cts +21 -8
- package/dist/provider-proxy/ingress/providerProxyShared.d.ts +21 -8
- package/dist/provider-proxy/ingress/providerProxyShared.js +31 -26
- package/dist/provider-proxy/types.d.cts +14 -483
- package/dist/provider-proxy/types.d.ts +14 -483
- package/dist/provider-proxy.cjs +44 -27
- package/dist/provider-proxy.d.cts +30 -8
- package/dist/provider-proxy.d.ts +30 -8
- package/dist/provider-proxy.js +43 -26
- package/dist/{routeResolver-C0mkkZaM.d.ts → routeResolver-CTCcSHWu.d.ts} +4 -2
- package/dist/{routeResolver-CVFE8KKe.d.cts → routeResolver-rpKQLyDO.d.cts} +4 -2
- package/dist/transformer/TransformerChainExecutor.cjs +2 -2
- package/dist/transformer/TransformerChainExecutor.d.cts +2 -2
- package/dist/transformer/TransformerChainExecutor.d.ts +2 -2
- package/dist/transformer/TransformerChainExecutor.js +1 -1
- package/dist/transformer/transformers/AnthropicOpenAIToAnthropicStream.cjs +16 -0
- package/dist/transformer/transformers/AnthropicOpenAIToAnthropicStream.d.cts +53 -0
- package/dist/transformer/transformers/AnthropicOpenAIToAnthropicStream.d.ts +53 -0
- package/dist/transformer/transformers/AnthropicOpenAIToAnthropicStream.js +16 -0
- package/dist/transformer/transformers/AnthropicTransformer.cjs +4 -3
- package/dist/transformer/transformers/AnthropicTransformer.js +3 -2
- package/dist/transformer/transformers/GeminiCodeAssistTransformer.cjs +4 -4
- package/dist/transformer/transformers/GeminiCodeAssistTransformer.js +3 -3
- package/dist/transformer/transformers/GeminiTransformer.cjs +4 -4
- package/dist/transformer/transformers/GeminiTransformer.js +3 -3
- package/dist/transformer/transformers/OpenAIResponseTransformer.cjs +3 -3
- package/dist/transformer/transformers/OpenAIResponseTransformer.js +2 -2
- package/dist/transformer/transformers/OpenAITransformer.cjs +3 -3
- package/dist/transformer/transformers/OpenAITransformer.js +2 -2
- package/dist/transformer/transformers.cjs +10 -9
- package/dist/transformer/transformers.js +13 -12
- package/dist/transformer/types.d.cts +39 -4
- package/dist/transformer/types.d.ts +39 -4
- package/dist/transformer.cjs +14 -13
- package/dist/transformer.js +14 -13
- package/dist/{types-BB8pxk_3.d.ts → types-5T223WYQ.d.ts} +183 -23
- package/dist/types-Bd72H4SB.d.ts +196 -0
- package/dist/types-CVRge76p.d.ts +1115 -0
- package/dist/types-CbglB73l.d.cts +196 -0
- package/dist/types-DsHBBk3h.d.cts +1115 -0
- package/dist/{types-C2MwkLah.d.cts → types-jjU24eYG.d.cts} +183 -23
- package/dist/usage/usage-recorder.cjs +2 -2
- package/dist/usage/usage-recorder.d.cts +12 -2
- package/dist/usage/usage-recorder.d.ts +12 -2
- package/dist/usage/usage-recorder.js +1 -1
- package/dist/usage.cjs +20 -3
- package/dist/usage.d.cts +154 -0
- package/dist/usage.d.ts +154 -0
- package/dist/usage.js +20 -3
- package/package.json +69 -64
- package/dist/chunk-7C7DET6S.js +0 -154
- package/dist/chunk-7VU7V2E4.js +0 -0
- package/dist/chunk-EYZYXJTJ.cjs +0 -1
- package/dist/chunk-FCR77GYM.cjs +0 -33
- package/dist/chunk-K5NM7VAH.js +0 -33
- package/dist/chunk-XPIOJQS2.cjs +0 -154
- /package/dist/{chunk-XXFYLL7R.js → chunk-ECZ3ZE2C.js} +0 -0
- /package/dist/{chunk-3SCOLIPT.cjs → chunk-MXI2PWIC.cjs} +0 -0
|
@@ -30,6 +30,7 @@ var TransformerChainExecutor = class {
|
|
|
30
30
|
providerName: provider.name
|
|
31
31
|
};
|
|
32
32
|
let requestBody = request;
|
|
33
|
+
let endpointRequest;
|
|
33
34
|
let config = {};
|
|
34
35
|
let bypass = false;
|
|
35
36
|
bypass = this.shouldBypassTransformers(
|
|
@@ -60,6 +61,7 @@ var TransformerChainExecutor = class {
|
|
|
60
61
|
this.logger.error(`transformRequestOut error: ${this.getErrorMessage(error)}`);
|
|
61
62
|
throw error;
|
|
62
63
|
}
|
|
64
|
+
endpointRequest = requestBody;
|
|
63
65
|
}
|
|
64
66
|
if (!bypass && chain.providerTransformers.length > 0) {
|
|
65
67
|
this.logger.debug(`Executing ${chain.providerTransformers.length} provider transformers`);
|
|
@@ -118,7 +120,7 @@ var TransformerChainExecutor = class {
|
|
|
118
120
|
true
|
|
119
121
|
);
|
|
120
122
|
}
|
|
121
|
-
return { requestBody, config, bypass };
|
|
123
|
+
return { requestBody, endpointRequest, config, bypass };
|
|
122
124
|
}
|
|
123
125
|
/**
|
|
124
126
|
* Execute the response transformation chain
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AnthropicTransformer
|
|
3
|
-
} from "./chunk-HKB656B4.js";
|
|
4
|
-
import {
|
|
5
|
-
GeminiTransformer
|
|
6
|
-
} from "./chunk-55FMGZ3L.js";
|
|
7
1
|
import {
|
|
8
2
|
OpenAIResponseTransformer
|
|
9
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-V53ZEFXC.js";
|
|
10
4
|
import {
|
|
11
5
|
OpenAITransformer
|
|
12
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-7PIUVCKK.js";
|
|
7
|
+
import {
|
|
8
|
+
AnthropicTransformer
|
|
9
|
+
} from "./chunk-6OC3ORW6.js";
|
|
10
|
+
import {
|
|
11
|
+
GeminiTransformer
|
|
12
|
+
} from "./chunk-HN74IGEO.js";
|
|
13
13
|
import {
|
|
14
14
|
GeminiCodeAssistTransformer
|
|
15
|
-
} from "./chunk-
|
|
15
|
+
} from "./chunk-4VVCXVB7.js";
|
|
16
16
|
|
|
17
17
|
// src/transformer/transformers/index.ts
|
|
18
18
|
var BuiltinTransformers = {
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkAEOZIDEBcjs = require('./chunk-AEOZIDEB.cjs');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _chunkHCOQQAZEcjs = require('./chunk-HCOQQAZE.cjs');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
var _chunkXSVNC2UDcjs = require('./chunk-XSVNC2UD.cjs');
|
|
11
|
+
|
|
12
|
+
// src/outbound-api/auditCapture.ts
|
|
13
|
+
var _crypto = require('crypto');
|
|
14
|
+
|
|
15
|
+
// src/outbound-api/auditSseCompact.ts
|
|
16
|
+
var MERGED_FRAMES_FIELD = "_omnicrossMergedFrames";
|
|
17
|
+
function asObject(value) {
|
|
18
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
19
|
+
}
|
|
20
|
+
function scalar(value) {
|
|
21
|
+
return typeof value === "string" || typeof value === "number" ? String(value) : "";
|
|
22
|
+
}
|
|
23
|
+
function classify(payload) {
|
|
24
|
+
const type = scalar(payload["type"]);
|
|
25
|
+
if (type === "content_block_delta") {
|
|
26
|
+
const delta = asObject(payload["delta"]);
|
|
27
|
+
const deltaType = scalar(_optionalChain([delta, 'optionalAccess', _ => _["type"]]));
|
|
28
|
+
const field = deltaType === "text_delta" ? "text" : deltaType === "thinking_delta" ? "thinking" : deltaType === "input_json_delta" ? "partial_json" : null;
|
|
29
|
+
if (!delta || !field || typeof delta[field] !== "string") return null;
|
|
30
|
+
return {
|
|
31
|
+
channel: "anthropic:" + scalar(payload["index"]) + ":" + deltaType,
|
|
32
|
+
text: delta[field],
|
|
33
|
+
payload,
|
|
34
|
+
textPath: ["delta", field]
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
if (type.endsWith(".delta") && typeof payload["delta"] === "string") {
|
|
38
|
+
const channel = [
|
|
39
|
+
"responses",
|
|
40
|
+
type,
|
|
41
|
+
scalar(payload["item_id"]),
|
|
42
|
+
scalar(payload["output_index"]),
|
|
43
|
+
scalar(payload["content_index"])
|
|
44
|
+
].join(":");
|
|
45
|
+
return { channel, text: payload["delta"], payload, textPath: ["delta"] };
|
|
46
|
+
}
|
|
47
|
+
const choices = payload["choices"];
|
|
48
|
+
if (Array.isArray(choices) && choices.length === 1) {
|
|
49
|
+
const choice = asObject(choices[0]);
|
|
50
|
+
const delta = asObject(_optionalChain([choice, 'optionalAccess', _2 => _2["delta"]]));
|
|
51
|
+
if (choice && delta && typeof delta["content"] === "string" && choice["finish_reason"] == null && delta["tool_calls"] === void 0 && delta["function_call"] === void 0) {
|
|
52
|
+
return {
|
|
53
|
+
channel: "chat:" + scalar(choice["index"]),
|
|
54
|
+
text: delta["content"],
|
|
55
|
+
payload,
|
|
56
|
+
textPath: ["choices", "0", "delta", "content"]
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
function withMergedText(frame, text, mergedCount) {
|
|
63
|
+
const clone = JSON.parse(JSON.stringify(frame.payload));
|
|
64
|
+
let cursor = clone;
|
|
65
|
+
for (let i = 0; i < frame.textPath.length - 1; i += 1) {
|
|
66
|
+
const step = frame.textPath[i];
|
|
67
|
+
const next = Array.isArray(cursor) ? cursor[Number(step)] : cursor[step];
|
|
68
|
+
if (next === null || typeof next !== "object") return clone;
|
|
69
|
+
cursor = next;
|
|
70
|
+
}
|
|
71
|
+
const leaf = frame.textPath[frame.textPath.length - 1];
|
|
72
|
+
if (Array.isArray(cursor)) cursor[Number(leaf)] = text;
|
|
73
|
+
else cursor[leaf] = text;
|
|
74
|
+
clone[MERGED_FRAMES_FIELD] = mergedCount;
|
|
75
|
+
return clone;
|
|
76
|
+
}
|
|
77
|
+
function splitBlocks(text, nl) {
|
|
78
|
+
const blocks = [];
|
|
79
|
+
let head = [];
|
|
80
|
+
let data = null;
|
|
81
|
+
let raw = [];
|
|
82
|
+
const flush = () => {
|
|
83
|
+
if (raw.length === 0) return;
|
|
84
|
+
blocks.push({ head, data, raw: raw.join(nl) });
|
|
85
|
+
head = [];
|
|
86
|
+
data = null;
|
|
87
|
+
raw = [];
|
|
88
|
+
};
|
|
89
|
+
for (const line of text.split(nl)) {
|
|
90
|
+
if (line.trim() === "") {
|
|
91
|
+
flush();
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
raw.push(line);
|
|
95
|
+
if (line.startsWith("data:")) {
|
|
96
|
+
const payload = line.slice("data:".length).trimStart();
|
|
97
|
+
data = data === null ? payload : data + nl + payload;
|
|
98
|
+
} else {
|
|
99
|
+
head.push(line);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
flush();
|
|
103
|
+
return blocks;
|
|
104
|
+
}
|
|
105
|
+
function compactSseBody(text) {
|
|
106
|
+
if (!text || !text.includes("data:")) return text;
|
|
107
|
+
const nl = "\n";
|
|
108
|
+
const blocks = splitBlocks(text, nl);
|
|
109
|
+
if (blocks.length === 0) return text;
|
|
110
|
+
const out = [];
|
|
111
|
+
let runFrame = null;
|
|
112
|
+
let runBlock = null;
|
|
113
|
+
let runText = "";
|
|
114
|
+
let runCount = 0;
|
|
115
|
+
let merged = 0;
|
|
116
|
+
const flushRun = () => {
|
|
117
|
+
if (runFrame === null || runBlock === null) return;
|
|
118
|
+
if (runCount === 1) {
|
|
119
|
+
out.push(runBlock.raw);
|
|
120
|
+
} else {
|
|
121
|
+
const payload = withMergedText(runFrame, runText, runCount);
|
|
122
|
+
out.push([...runBlock.head, "data: " + JSON.stringify(payload)].join(nl));
|
|
123
|
+
merged += runCount;
|
|
124
|
+
}
|
|
125
|
+
runFrame = null;
|
|
126
|
+
runBlock = null;
|
|
127
|
+
runText = "";
|
|
128
|
+
runCount = 0;
|
|
129
|
+
};
|
|
130
|
+
for (const block of blocks) {
|
|
131
|
+
let frame = null;
|
|
132
|
+
if (block.data !== null && block.data !== "[DONE]") {
|
|
133
|
+
try {
|
|
134
|
+
const parsed = asObject(JSON.parse(block.data));
|
|
135
|
+
if (parsed) frame = classify(parsed);
|
|
136
|
+
} catch (e) {
|
|
137
|
+
frame = null;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (frame === null) {
|
|
141
|
+
flushRun();
|
|
142
|
+
out.push(block.raw);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const open = runFrame;
|
|
146
|
+
if (open !== null && open.channel === frame.channel) {
|
|
147
|
+
runText += frame.text;
|
|
148
|
+
runCount += 1;
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
flushRun();
|
|
152
|
+
runFrame = frame;
|
|
153
|
+
runBlock = block;
|
|
154
|
+
runText = frame.text;
|
|
155
|
+
runCount = 1;
|
|
156
|
+
}
|
|
157
|
+
flushRun();
|
|
158
|
+
if (merged === 0) return text;
|
|
159
|
+
const trailer = /\s$/.test(text) ? nl + nl : "";
|
|
160
|
+
return out.join(nl + nl) + trailer;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// src/outbound-api/auditCapture.ts
|
|
164
|
+
function completeUtf8PrefixLength(buf) {
|
|
165
|
+
if (buf.length === 0) return 0;
|
|
166
|
+
let leadIndex = buf.length - 1;
|
|
167
|
+
while (leadIndex >= 0 && (buf[leadIndex] & 192) === 128) leadIndex -= 1;
|
|
168
|
+
if (leadIndex < 0) return 0;
|
|
169
|
+
const lead = buf[leadIndex];
|
|
170
|
+
let expectedLength = 1;
|
|
171
|
+
if ((lead & 224) === 192) expectedLength = 2;
|
|
172
|
+
else if ((lead & 240) === 224) expectedLength = 3;
|
|
173
|
+
else if ((lead & 248) === 240) expectedLength = 4;
|
|
174
|
+
return buf.length - leadIndex < expectedLength ? leadIndex : buf.length;
|
|
175
|
+
}
|
|
176
|
+
function truncateToBytes(text, maxBytes) {
|
|
177
|
+
if (maxBytes < 0) return text;
|
|
178
|
+
const buf = Buffer.from(text, "utf8");
|
|
179
|
+
if (buf.length <= maxBytes) return text;
|
|
180
|
+
const head = buf.subarray(0, maxBytes);
|
|
181
|
+
return head.subarray(0, completeUtf8PrefixLength(head)).toString("utf8");
|
|
182
|
+
}
|
|
183
|
+
function decodeCapturedBody(body, truncated) {
|
|
184
|
+
if (!truncated) return body.toString("utf8");
|
|
185
|
+
return body.subarray(0, completeUtf8PrefixLength(body)).toString("utf8");
|
|
186
|
+
}
|
|
187
|
+
function resolveClientIp(req, trustForwardedFor) {
|
|
188
|
+
if (trustForwardedFor) {
|
|
189
|
+
const xff = req.headers["x-forwarded-for"];
|
|
190
|
+
const raw = Array.isArray(xff) ? xff[0] : xff;
|
|
191
|
+
if (typeof raw === "string" && raw.trim()) {
|
|
192
|
+
const first = _optionalChain([raw, 'access', _3 => _3.split, 'call', _4 => _4(","), 'access', _5 => _5[0], 'optionalAccess', _6 => _6.trim, 'call', _7 => _7()]);
|
|
193
|
+
if (first) return first;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return _nullishCoalesce(_optionalChain([req, 'access', _8 => _8.socket, 'optionalAccess', _9 => _9.remoteAddress]), () => ( void 0));
|
|
197
|
+
}
|
|
198
|
+
function beginAuditCapture(req, res, now, options) {
|
|
199
|
+
const config = _chunkAEOZIDEBcjs.getAuditCaptureConfig.call(void 0, );
|
|
200
|
+
if (!config) return null;
|
|
201
|
+
const captureBodies = config.captureBodies;
|
|
202
|
+
let requestBody;
|
|
203
|
+
const responseChunks = [];
|
|
204
|
+
let responseBytes = 0;
|
|
205
|
+
let responseTruncated = false;
|
|
206
|
+
let bodiesSuppressed = _optionalChain([options, 'optionalAccess', _10 => _10.suppressBodies]) === true;
|
|
207
|
+
let finished = false;
|
|
208
|
+
const ctx = {
|
|
209
|
+
setRequestBody(raw) {
|
|
210
|
+
if (captureBodies && !bodiesSuppressed) {
|
|
211
|
+
requestBody = truncateToBytes(_chunkHCOQQAZEcjs.redactAuditText.call(void 0, raw), config.maxBodyBytes);
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
suppressBodies() {
|
|
215
|
+
if (bodiesSuppressed) return;
|
|
216
|
+
bodiesSuppressed = true;
|
|
217
|
+
requestBody = void 0;
|
|
218
|
+
responseChunks.length = 0;
|
|
219
|
+
responseBytes = 0;
|
|
220
|
+
responseTruncated = false;
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
if (captureBodies && !bodiesSuppressed) {
|
|
224
|
+
installResponseCapture(res, {
|
|
225
|
+
push(chunk) {
|
|
226
|
+
if (bodiesSuppressed) return;
|
|
227
|
+
if (config.maxBodyBytes < 0) {
|
|
228
|
+
responseChunks.push(chunk);
|
|
229
|
+
responseBytes += chunk.length;
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
const remaining = config.maxBodyBytes - responseBytes;
|
|
233
|
+
if (remaining <= 0) {
|
|
234
|
+
responseTruncated = true;
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (chunk.length > remaining) {
|
|
238
|
+
responseChunks.push(chunk.subarray(0, remaining));
|
|
239
|
+
responseBytes += remaining;
|
|
240
|
+
responseTruncated = true;
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
responseChunks.push(chunk);
|
|
244
|
+
responseBytes += chunk.length;
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
const finalize = () => {
|
|
249
|
+
if (finished) return;
|
|
250
|
+
finished = true;
|
|
251
|
+
try {
|
|
252
|
+
const record = {
|
|
253
|
+
id: _crypto.randomUUID.call(void 0, ),
|
|
254
|
+
ts: now,
|
|
255
|
+
method: _nullishCoalesce(req.method, () => ( "")),
|
|
256
|
+
path: _nullishCoalesce((_nullishCoalesce(req.url, () => ( ""))).split("?")[0], () => ( "")),
|
|
257
|
+
status: res.statusCode,
|
|
258
|
+
latencyMs: Math.max(0, Date.now() - now)
|
|
259
|
+
};
|
|
260
|
+
if (ctx.keyId != null) record.keyId = ctx.keyId;
|
|
261
|
+
const ip = resolveClientIp(req, config.trustForwardedFor);
|
|
262
|
+
if (ip) record.ip = ip;
|
|
263
|
+
const ua = req.headers["user-agent"];
|
|
264
|
+
if (typeof ua === "string" && ua) record.ua = ua;
|
|
265
|
+
if (ctx.model) record.model = ctx.model;
|
|
266
|
+
if (ctx.provider) record.provider = ctx.provider;
|
|
267
|
+
const usage = _chunkXSVNC2UDcjs.readAuditUsage.call(void 0, res);
|
|
268
|
+
if (usage) {
|
|
269
|
+
if (typeof usage.inputTokens === "number") record.inputTokens = usage.inputTokens;
|
|
270
|
+
if (typeof usage.outputTokens === "number") record.outputTokens = usage.outputTokens;
|
|
271
|
+
if (typeof usage.costUsd === "number") record.costUsd = usage.costUsd;
|
|
272
|
+
if (!record.model && usage.model) record.model = usage.model;
|
|
273
|
+
if (!record.provider && usage.provider) record.provider = usage.provider;
|
|
274
|
+
}
|
|
275
|
+
if (ctx.error) record.error = _chunkHCOQQAZEcjs.redactAuditText.call(void 0, ctx.error);
|
|
276
|
+
if (ctx.sessionKey) record.sessionKey = ctx.sessionKey;
|
|
277
|
+
if (captureBodies && !bodiesSuppressed) {
|
|
278
|
+
if (requestBody != null && requestBody.length > 0) {
|
|
279
|
+
record.requestBody = requestBody;
|
|
280
|
+
}
|
|
281
|
+
if (responseChunks.length > 0) {
|
|
282
|
+
const captured = Buffer.concat(responseChunks, responseBytes);
|
|
283
|
+
const decoded = decodeCapturedBody(captured, responseTruncated);
|
|
284
|
+
const body = config.compactStreamingBodies ? compactSseBody(decoded) : decoded;
|
|
285
|
+
record.responseBody = truncateToBytes(_chunkHCOQQAZEcjs.redactAuditText.call(void 0, body), config.maxBodyBytes);
|
|
286
|
+
}
|
|
287
|
+
if (record.requestBody !== void 0 || record.responseBody !== void 0) {
|
|
288
|
+
record.hasBody = true;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
_chunkAEOZIDEBcjs.recordAudit.call(void 0, record);
|
|
292
|
+
} catch (e2) {
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
res.once("close", finalize);
|
|
296
|
+
return ctx;
|
|
297
|
+
}
|
|
298
|
+
function installResponseCapture(res, sink) {
|
|
299
|
+
const capture = (chunk) => {
|
|
300
|
+
let buf = null;
|
|
301
|
+
if (Buffer.isBuffer(chunk)) buf = chunk;
|
|
302
|
+
else if (typeof chunk === "string") buf = Buffer.from(chunk, "utf8");
|
|
303
|
+
if (!buf || buf.length === 0) return;
|
|
304
|
+
sink.push(buf);
|
|
305
|
+
};
|
|
306
|
+
const originalWrite = res.write.bind(res);
|
|
307
|
+
const originalEnd = res.end.bind(res);
|
|
308
|
+
res.write = ((chunk, ...args) => {
|
|
309
|
+
if (chunk != null) capture(chunk);
|
|
310
|
+
return originalWrite(chunk, ...args);
|
|
311
|
+
});
|
|
312
|
+
res.end = ((chunk, ...args) => {
|
|
313
|
+
if (chunk != null && typeof chunk !== "function") capture(chunk);
|
|
314
|
+
return originalEnd(chunk, ...args);
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
exports.beginAuditCapture = beginAuditCapture;
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } var _class; var _class2; var _class3;// src/openai-operation/openAIOperation.ts
|
|
2
|
+
var OPERATIONS = {
|
|
3
|
+
"chat.completions.create": {
|
|
4
|
+
id: "chat.completions.create",
|
|
5
|
+
policyFamily: "chat",
|
|
6
|
+
routeFamily: "chat",
|
|
7
|
+
owner: "builtin",
|
|
8
|
+
bodyKind: "json",
|
|
9
|
+
requestedModelSource: "request"
|
|
10
|
+
},
|
|
11
|
+
"responses.create": {
|
|
12
|
+
id: "responses.create",
|
|
13
|
+
policyFamily: "responses",
|
|
14
|
+
routeFamily: "responses",
|
|
15
|
+
owner: "builtin",
|
|
16
|
+
bodyKind: "json",
|
|
17
|
+
requestedModelSource: "request"
|
|
18
|
+
},
|
|
19
|
+
"responses.compact": {
|
|
20
|
+
id: "responses.compact",
|
|
21
|
+
policyFamily: "responses",
|
|
22
|
+
routeFamily: "responses",
|
|
23
|
+
owner: "extension",
|
|
24
|
+
bodyKind: "json",
|
|
25
|
+
requestedModelSource: "request"
|
|
26
|
+
},
|
|
27
|
+
"images.generate": {
|
|
28
|
+
id: "images.generate",
|
|
29
|
+
policyFamily: "images",
|
|
30
|
+
routeFamily: "images",
|
|
31
|
+
owner: "extension",
|
|
32
|
+
bodyKind: "json",
|
|
33
|
+
requestedModelSource: "configured"
|
|
34
|
+
},
|
|
35
|
+
"images.edit": {
|
|
36
|
+
id: "images.edit",
|
|
37
|
+
policyFamily: "images",
|
|
38
|
+
routeFamily: "images",
|
|
39
|
+
owner: "extension",
|
|
40
|
+
bodyKind: "multipart",
|
|
41
|
+
requestedModelSource: "configured"
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var MATCH_ORDER = [
|
|
45
|
+
{ id: "responses.compact", suffix: ["responses", "compact"] },
|
|
46
|
+
{ id: "images.generate", suffix: ["images", "generations"] },
|
|
47
|
+
{ id: "images.edit", suffix: ["images", "edits"] },
|
|
48
|
+
{ id: "chat.completions.create", suffix: ["chat", "completions"] },
|
|
49
|
+
{ id: "responses.create", suffix: ["responses"] }
|
|
50
|
+
];
|
|
51
|
+
function getOpenAIOperation(id) {
|
|
52
|
+
return OPERATIONS[id];
|
|
53
|
+
}
|
|
54
|
+
function pathSegments(url) {
|
|
55
|
+
const queryIndex = url.indexOf("?");
|
|
56
|
+
const path = queryIndex >= 0 ? url.slice(0, queryIndex) : url;
|
|
57
|
+
return path.split("/").filter(Boolean);
|
|
58
|
+
}
|
|
59
|
+
function hasExactSuffix(segments, suffix) {
|
|
60
|
+
if (segments.length < suffix.length) return false;
|
|
61
|
+
const offset = segments.length - suffix.length;
|
|
62
|
+
return suffix.every((segment, index) => segments[offset + index] === segment);
|
|
63
|
+
}
|
|
64
|
+
function classifyOpenAIOperation(method, url) {
|
|
65
|
+
if (method !== "POST" || !url) return null;
|
|
66
|
+
const segments = pathSegments(url);
|
|
67
|
+
for (const candidate of MATCH_ORDER) {
|
|
68
|
+
if (hasExactSuffix(segments, candidate.suffix)) return OPERATIONS[candidate.id];
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/openai-operation/openAIOperationRegistry.ts
|
|
74
|
+
var OpenAIOperationRegistrationError = (_class = class extends Error {
|
|
75
|
+
constructor(code, message) {
|
|
76
|
+
super(message);_class.prototype.__init.call(this);;
|
|
77
|
+
this.code = code;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
__init() {this.name = "OpenAIOperationRegistrationError"}
|
|
81
|
+
}, _class);
|
|
82
|
+
var OpenAIOperationError = (_class2 = class extends Error {
|
|
83
|
+
__init2() {this.name = "OpenAIOperationError"}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
constructor(init) {
|
|
89
|
+
super(init.message);_class2.prototype.__init2.call(this);;
|
|
90
|
+
this.status = init.status;
|
|
91
|
+
this.code = init.code;
|
|
92
|
+
this.retryable = _nullishCoalesce(init.retryable, () => ( false));
|
|
93
|
+
this.headers = _nullishCoalesce(init.headers, () => ( {}));
|
|
94
|
+
}
|
|
95
|
+
}, _class2);
|
|
96
|
+
var SAFE_LOCAL_ERROR_HEADERS = /* @__PURE__ */ new Set(["retry-after", "x-request-id"]);
|
|
97
|
+
function safeErrorHeaders(headers) {
|
|
98
|
+
const safe = {};
|
|
99
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
100
|
+
if (SAFE_LOCAL_ERROR_HEADERS.has(name.toLowerCase())) safe[name] = value;
|
|
101
|
+
}
|
|
102
|
+
return safe;
|
|
103
|
+
}
|
|
104
|
+
function writeOpenAIOperationError(res, error) {
|
|
105
|
+
if (res.headersSent || res.writableEnded) return false;
|
|
106
|
+
res.writeHead(error.status, {
|
|
107
|
+
"Content-Type": "application/json",
|
|
108
|
+
...safeErrorHeaders(error.headers)
|
|
109
|
+
});
|
|
110
|
+
res.end(JSON.stringify({
|
|
111
|
+
error: {
|
|
112
|
+
type: "openai_operation_error",
|
|
113
|
+
code: error.code,
|
|
114
|
+
message: error.message,
|
|
115
|
+
retryable: error.retryable
|
|
116
|
+
}
|
|
117
|
+
}));
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
function unsupportedOpenAIOperation(operation) {
|
|
121
|
+
return new OpenAIOperationError({
|
|
122
|
+
status: 501,
|
|
123
|
+
code: "unsupported_capability",
|
|
124
|
+
message: `Operation '${operation.id}' is not available`
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
var OpenAIOperationRegistry = (_class3 = class {constructor() { _class3.prototype.__init3.call(this); }
|
|
128
|
+
__init3() {this.handlers = /* @__PURE__ */ new Map()}
|
|
129
|
+
register(operationId, handler) {
|
|
130
|
+
const operation = getOpenAIOperation(operationId);
|
|
131
|
+
if (operation.owner !== "extension") {
|
|
132
|
+
throw new OpenAIOperationRegistrationError(
|
|
133
|
+
"builtin_operation",
|
|
134
|
+
`Operation '${operationId}' is owned by a built-in ingress`
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
if (this.handlers.has(operationId)) {
|
|
138
|
+
throw new OpenAIOperationRegistrationError(
|
|
139
|
+
"duplicate_operation_handler",
|
|
140
|
+
`Operation '${operationId}' already has a handler`
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
this.handlers.set(operationId, handler);
|
|
144
|
+
let disposed = false;
|
|
145
|
+
return () => {
|
|
146
|
+
if (disposed) return;
|
|
147
|
+
disposed = true;
|
|
148
|
+
if (this.handlers.get(operationId) === handler) this.handlers.delete(operationId);
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
has(operationId) {
|
|
152
|
+
return this.handlers.has(operationId);
|
|
153
|
+
}
|
|
154
|
+
async dispatch(context) {
|
|
155
|
+
const handler = this.handlers.get(context.operation.id);
|
|
156
|
+
if (!handler) return false;
|
|
157
|
+
const controller = new AbortController();
|
|
158
|
+
const abort = () => {
|
|
159
|
+
if (!controller.signal.aborted) controller.abort();
|
|
160
|
+
};
|
|
161
|
+
const onRequestClose = () => {
|
|
162
|
+
if (context.request.aborted || !context.request.complete) abort();
|
|
163
|
+
};
|
|
164
|
+
const onResponseClose = () => {
|
|
165
|
+
if (!context.response.writableEnded) abort();
|
|
166
|
+
};
|
|
167
|
+
context.request.once("aborted", abort);
|
|
168
|
+
context.request.once("close", onRequestClose);
|
|
169
|
+
context.response.once("close", onResponseClose);
|
|
170
|
+
if (context.request.aborted || context.response.destroyed) abort();
|
|
171
|
+
try {
|
|
172
|
+
await handler({ ...context, signal: controller.signal });
|
|
173
|
+
return true;
|
|
174
|
+
} catch (error) {
|
|
175
|
+
const publicError = error instanceof OpenAIOperationError ? error : new OpenAIOperationError({
|
|
176
|
+
status: 500,
|
|
177
|
+
code: "operation_handler_failed",
|
|
178
|
+
message: `Operation '${context.operation.id}' handler failed`
|
|
179
|
+
});
|
|
180
|
+
if (!writeOpenAIOperationError(context.response, publicError)) {
|
|
181
|
+
throw new Error(publicError.message);
|
|
182
|
+
}
|
|
183
|
+
return true;
|
|
184
|
+
} finally {
|
|
185
|
+
context.request.removeListener("aborted", abort);
|
|
186
|
+
context.request.removeListener("close", onRequestClose);
|
|
187
|
+
context.response.removeListener("close", onResponseClose);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}, _class3);
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
exports.getOpenAIOperation = getOpenAIOperation; exports.classifyOpenAIOperation = classifyOpenAIOperation; exports.OpenAIOperationRegistrationError = OpenAIOperationRegistrationError; exports.OpenAIOperationError = OpenAIOperationError; exports.writeOpenAIOperationError = writeOpenAIOperationError; exports.unsupportedOpenAIOperation = unsupportedOpenAIOperation; exports.OpenAIOperationRegistry = OpenAIOperationRegistry;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/outbound-api/auditRedact.ts
|
|
2
|
+
var AUDIT_REDACTED = "***REDACTED***";
|
|
3
|
+
var RULES = [
|
|
4
|
+
// `sk-…` family (Anthropic sk-ant-, omnicross sk-omnicross-, OpenAI sk-proj-/sk-).
|
|
5
|
+
{ re: /\bsk-[A-Za-z0-9_-]{6,}/g, replace: AUDIT_REDACTED },
|
|
6
|
+
// Google API keys.
|
|
7
|
+
{ re: /\bAIza[A-Za-z0-9_-]{10,}/g, replace: AUDIT_REDACTED },
|
|
8
|
+
// `Bearer <token>` (auth headers echoed into a body / SSE error).
|
|
9
|
+
{ re: /\bBearer\s+[A-Za-z0-9._~+/=-]{6,}/gi, replace: `Bearer ${AUDIT_REDACTED}` },
|
|
10
|
+
// Inline `x-api-key` / `api-key` / `authorization` label followed by a value.
|
|
11
|
+
{
|
|
12
|
+
re: /\b(x-api-key|api[_-]?key|authorization)(["']?\s*[:=]\s*["']?)[A-Za-z0-9._~+/=-]{6,}/gi,
|
|
13
|
+
replace: `$1$2${AUDIT_REDACTED}`
|
|
14
|
+
},
|
|
15
|
+
// `x-goog-api-key` header echoed inline.
|
|
16
|
+
{
|
|
17
|
+
re: /\b(x-goog-api-key)(["']?\s*[:=]\s*["']?)[A-Za-z0-9._~+/=-]{6,}/gi,
|
|
18
|
+
replace: `$1$2${AUDIT_REDACTED}`
|
|
19
|
+
}
|
|
20
|
+
];
|
|
21
|
+
function redactEncryptedContentValue(value) {
|
|
22
|
+
if (!value || typeof value !== "object") return false;
|
|
23
|
+
if (Array.isArray(value)) {
|
|
24
|
+
let changed2 = false;
|
|
25
|
+
for (const item of value) changed2 = redactEncryptedContentValue(item) || changed2;
|
|
26
|
+
return changed2;
|
|
27
|
+
}
|
|
28
|
+
let changed = false;
|
|
29
|
+
const record = value;
|
|
30
|
+
for (const [key, nested] of Object.entries(record)) {
|
|
31
|
+
if (key === "encrypted_content") {
|
|
32
|
+
record[key] = AUDIT_REDACTED;
|
|
33
|
+
changed = true;
|
|
34
|
+
} else {
|
|
35
|
+
changed = redactEncryptedContentValue(nested) || changed;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return changed;
|
|
39
|
+
}
|
|
40
|
+
function redactJsonValue(text) {
|
|
41
|
+
try {
|
|
42
|
+
const parsed = JSON.parse(text);
|
|
43
|
+
return redactEncryptedContentValue(parsed) ? JSON.stringify(parsed) : null;
|
|
44
|
+
} catch (e) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function redactEncryptedContentSse(text) {
|
|
49
|
+
return text.replace(/^(\s*data:\s*)([^\r\n]*)(\r?)$/gm, (line, prefix, payload, cr) => {
|
|
50
|
+
if (!payload || payload.trim() === "[DONE]") return line;
|
|
51
|
+
const redacted = redactJsonValue(payload.trim());
|
|
52
|
+
return redacted === null ? line : `${prefix}${redacted}${cr}`;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function findJsonLikeValueEnd(text, start) {
|
|
56
|
+
const first = text[start];
|
|
57
|
+
if (first === '"' || first === "'") {
|
|
58
|
+
let escaped = false;
|
|
59
|
+
for (let index = start + 1; index < text.length; index += 1) {
|
|
60
|
+
const char = text[index];
|
|
61
|
+
if (escaped) escaped = false;
|
|
62
|
+
else if (char === "\\") escaped = true;
|
|
63
|
+
else if (char === first) return index + 1;
|
|
64
|
+
}
|
|
65
|
+
return text.length;
|
|
66
|
+
}
|
|
67
|
+
if (first === "{" || first === "[") {
|
|
68
|
+
const opening = first;
|
|
69
|
+
const closing = first === "{" ? "}" : "]";
|
|
70
|
+
let depth = 0;
|
|
71
|
+
let quote = "";
|
|
72
|
+
let escaped = false;
|
|
73
|
+
for (let index = start; index < text.length; index += 1) {
|
|
74
|
+
const char = text[index];
|
|
75
|
+
if (quote) {
|
|
76
|
+
if (escaped) escaped = false;
|
|
77
|
+
else if (char === "\\") escaped = true;
|
|
78
|
+
else if (char === quote) quote = "";
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (char === '"' || char === "'") quote = char;
|
|
82
|
+
else if (char === opening) depth += 1;
|
|
83
|
+
else if (char === closing && --depth === 0) return index + 1;
|
|
84
|
+
}
|
|
85
|
+
return text.length;
|
|
86
|
+
}
|
|
87
|
+
let end = start;
|
|
88
|
+
while (end < text.length && !/[\r\n,}\]]/.test(text[end])) end += 1;
|
|
89
|
+
return end;
|
|
90
|
+
}
|
|
91
|
+
function redactEncryptedContentText(text) {
|
|
92
|
+
const key = /["']encrypted_content["']\s*:\s*/g;
|
|
93
|
+
let output = "";
|
|
94
|
+
let cursor = 0;
|
|
95
|
+
for (; ; ) {
|
|
96
|
+
const match = key.exec(text);
|
|
97
|
+
if (!match) break;
|
|
98
|
+
const valueStart = match.index + match[0].length;
|
|
99
|
+
const valueEnd = findJsonLikeValueEnd(text, valueStart);
|
|
100
|
+
output += text.slice(cursor, valueStart) + JSON.stringify(AUDIT_REDACTED);
|
|
101
|
+
cursor = valueEnd;
|
|
102
|
+
key.lastIndex = valueEnd;
|
|
103
|
+
}
|
|
104
|
+
return cursor === 0 ? text : output + text.slice(cursor);
|
|
105
|
+
}
|
|
106
|
+
function redactEncryptedContent(text) {
|
|
107
|
+
const completeJson = redactJsonValue(text);
|
|
108
|
+
if (completeJson !== null) return completeJson;
|
|
109
|
+
return redactEncryptedContentText(redactEncryptedContentSse(text));
|
|
110
|
+
}
|
|
111
|
+
function redactAuditText(text) {
|
|
112
|
+
if (typeof text !== "string" || text.length === 0) return _nullishCoalesce(text, () => ( ""));
|
|
113
|
+
let out = redactEncryptedContent(text);
|
|
114
|
+
for (const { re, replace } of RULES) {
|
|
115
|
+
out = out.replace(re, replace);
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
exports.AUDIT_REDACTED = AUDIT_REDACTED; exports.redactAuditText = redactAuditText;
|