@jeffusion/bungee-core 5.19.0 → 5.20.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/gateway/contracts.d.ts +2 -0
- package/dist/hooks/plugin-hooks.d.ts +3 -0
- package/dist/main.js +38 -18
- package/dist/plugins/ai-transformer/index.js +11 -1
- package/dist/plugins/ai-transformer/index.js.map +5 -3
- package/dist/plugins/codex-router/control.js +673 -2
- package/dist/plugins/codex-router/control.js.map +19 -4
- package/dist/plugins/codex-router/index.js +3765 -8
- package/dist/plugins/codex-router/index.js.map +22 -6
- package/dist/plugins/codex-router/manifest.json +15 -1
- package/dist/plugins/openai-messages-to-chat/index.js +691 -543
- package/dist/plugins/openai-messages-to-chat/index.js.map +8 -4
- package/dist/plugins/token-metering/index.js +652 -71
- package/dist/plugins/token-metering/index.js.map +5 -3
- package/dist/scoped-plugin-registry.d.ts +4 -2
- package/package.json +2 -2
|
@@ -39,6 +39,7 @@ export interface DispatchRequestInput {
|
|
|
39
39
|
}
|
|
40
40
|
export interface DispatchRequestDecision {
|
|
41
41
|
readonly target: GatewayDispatchTarget;
|
|
42
|
+
readonly adapter?: import('../plugin.types').Plugin;
|
|
42
43
|
}
|
|
43
44
|
export interface GatewayDispatchInput extends Omit<DispatchRequestInput, 'targets'> {
|
|
44
45
|
readonly config: AppConfig;
|
|
@@ -50,6 +51,7 @@ export interface GatewayDispatchDecision {
|
|
|
50
51
|
readonly context: import('../hooks').MutableRequestContext;
|
|
51
52
|
readonly entryRouteId?: string;
|
|
52
53
|
readonly target?: GatewayDispatchTarget;
|
|
54
|
+
readonly adapter?: import('../plugin.types').Plugin;
|
|
53
55
|
}
|
|
54
56
|
export interface GatewayRouteInput {
|
|
55
57
|
readonly request: Request;
|
|
@@ -184,6 +184,9 @@ export interface PluginScopeInfo {
|
|
|
184
184
|
export interface PluginInitContext {
|
|
185
185
|
/** Host-controlled, dependency-restricted public service facade. */
|
|
186
186
|
readonly services?: PluginServices;
|
|
187
|
+
readonly dispatchTargets?: readonly (import('../gateway/contracts').GatewayDispatchTarget & {
|
|
188
|
+
readonly protocol?: import('@jeffusion/bungee-types').LLMProtocol;
|
|
189
|
+
})[];
|
|
187
190
|
/** 插件配置 */
|
|
188
191
|
config: Record<string, any>;
|
|
189
192
|
/** 插件存储 */
|
package/dist/main.js
CHANGED
|
@@ -30768,7 +30768,7 @@ var package_default;
|
|
|
30768
30768
|
var init_package = __esm(() => {
|
|
30769
30769
|
package_default = {
|
|
30770
30770
|
name: "@jeffusion/bungee-core",
|
|
30771
|
-
version: "5.
|
|
30771
|
+
version: "5.20.0",
|
|
30772
30772
|
description: "Bungee reverse proxy core engine",
|
|
30773
30773
|
main: "./dist/main.js",
|
|
30774
30774
|
types: "./dist/main.d.ts",
|
|
@@ -213025,6 +213025,7 @@ function assertBodyContract(handler) {
|
|
|
213025
213025
|
|
|
213026
213026
|
class ScopedPluginRegistry {
|
|
213027
213027
|
serviceHost;
|
|
213028
|
+
dispatchConfig;
|
|
213028
213029
|
rpcContexts = new Map;
|
|
213029
213030
|
requestLeases = new AsyncLocalStorage3;
|
|
213030
213031
|
runWithRequestLeases(leases, run) {
|
|
@@ -213353,6 +213354,7 @@ ${errorMessages}`);
|
|
|
213353
213354
|
}
|
|
213354
213355
|
async createInitContext(pluginName, config, scope) {
|
|
213355
213356
|
const scopeInfo = scope ? toPluginScopeInfo(scope) : undefined;
|
|
213357
|
+
const dispatchTargets = scope?.type === "route" ? declaredDispatchTargets(config, this.pluginClasses.get(pluginName)?.configSchema ?? []).map((target) => ({ ...target, protocol: (target.type === "route" ? this.dispatchConfig?.routes : this.dispatchConfig?.services)?.find((entity) => entity.id === target.id)?.llm_protocol })) : [];
|
|
213356
213358
|
const services = this.serviceHost.createContext(pluginName, scope ? getScopeKey(scope) : "global", this.serviceDependencies.get(pluginName) ?? {});
|
|
213357
213359
|
this.rpcContexts.set(`${pluginName}\x00${scope ? getScopeKey(scope) : "global"}`, services);
|
|
213358
213360
|
if (isPluginContextManagerInitialized()) {
|
|
@@ -213363,18 +213365,20 @@ ${errorMessages}`);
|
|
|
213363
213365
|
...existingContext,
|
|
213364
213366
|
config,
|
|
213365
213367
|
scope: scopeInfo,
|
|
213366
|
-
services
|
|
213368
|
+
services,
|
|
213369
|
+
dispatchTargets
|
|
213367
213370
|
};
|
|
213368
213371
|
}
|
|
213369
213372
|
const newContext = contextManager.getOrCreateContext(pluginName, "", config);
|
|
213370
|
-
return { ...newContext, scope: scopeInfo, services };
|
|
213373
|
+
return { ...newContext, scope: scopeInfo, services, dispatchTargets };
|
|
213371
213374
|
}
|
|
213372
213375
|
return {
|
|
213373
213376
|
config,
|
|
213374
213377
|
storage: this.createDummyStorage(),
|
|
213375
213378
|
logger: this.createPluginLogger(pluginName),
|
|
213376
213379
|
scope: scopeInfo,
|
|
213377
|
-
services
|
|
213380
|
+
services,
|
|
213381
|
+
dispatchTargets
|
|
213378
213382
|
};
|
|
213379
213383
|
}
|
|
213380
213384
|
createPluginLogger(pluginName) {
|
|
@@ -213424,21 +213428,28 @@ ${errorMessages}`);
|
|
|
213424
213428
|
}
|
|
213425
213429
|
};
|
|
213426
213430
|
}
|
|
213427
|
-
getPrecompiledHooks(routeId, upstreamId, serviceName) {
|
|
213431
|
+
getPrecompiledHooks(routeId, upstreamId, serviceName, adapter) {
|
|
213428
213432
|
if (!this.precompiled) {
|
|
213429
213433
|
this.precompileAllHooks();
|
|
213430
213434
|
}
|
|
213431
213435
|
const cacheKey = `phase-aware:${routeId}#${upstreamId || ""}#${serviceName || ""}`;
|
|
213432
|
-
const cached = this.phaseAwareCache.get(cacheKey);
|
|
213436
|
+
const cached = adapter ? undefined : this.phaseAwareCache.get(cacheKey);
|
|
213433
213437
|
if (cached) {
|
|
213434
213438
|
return cached;
|
|
213435
213439
|
}
|
|
213436
213440
|
const routePhase = this.buildRoutePhaseHooks(routeId);
|
|
213437
213441
|
const servicePhase = serviceName ? this.buildServicePhaseHooks(routeId, serviceName) : null;
|
|
213438
213442
|
const upstreamPhase = upstreamId ? this.buildUpstreamPhaseHooks(routeId, upstreamId) : this.buildEmptyPrecompiledHooks();
|
|
213439
|
-
const
|
|
213440
|
-
|
|
213441
|
-
|
|
213443
|
+
const dispatchAdapter = adapter ? this.buildPrecompiledHooks([{
|
|
213444
|
+
scope: { type: "route", routeId },
|
|
213445
|
+
config: { name: "gateway-dispatch-adapter" },
|
|
213446
|
+
priority: 0,
|
|
213447
|
+
handler: { pluginName: "gateway-dispatch-adapter", config: {}, bodyRequirements: adapter.bodyRequirements.bind(adapter), register: adapter.register.bind(adapter) }
|
|
213448
|
+
}], "dispatch-adapter") : undefined;
|
|
213449
|
+
const inbound = this.buildInboundChain(routeId, upstreamId || "", serviceName, dispatchAdapter);
|
|
213450
|
+
const result = { dispatchAdapter, routePhase, servicePhase, upstreamPhase, inbound, globalPrecompiled: this.globalPrecompiled, routePrecompiled: this.routePrecompiled.get(routeId) ?? null };
|
|
213451
|
+
if (!adapter)
|
|
213452
|
+
this.phaseAwareCache.set(cacheKey, result);
|
|
213442
213453
|
return result;
|
|
213443
213454
|
}
|
|
213444
213455
|
getWebSocketHandshakeOwners(routeId, upstreamId, serviceName) {
|
|
@@ -213651,7 +213662,7 @@ ${errorMessages}`);
|
|
|
213651
213662
|
}
|
|
213652
213663
|
return this.buildPrecompiledHooks(instances, `upstream-phase:${key}`, true);
|
|
213653
213664
|
}
|
|
213654
|
-
buildInboundChain(routeId, upstreamId, serviceName) {
|
|
213665
|
+
buildInboundChain(routeId, upstreamId, serviceName, adapter) {
|
|
213655
213666
|
const upstreamHooks = this.upstreamPrecompiled.get(`${routeId}#${upstreamId}`);
|
|
213656
213667
|
const serviceHooks = serviceName ? this.servicePrecompiled.get(`${routeId}#${serviceName}`) : null;
|
|
213657
213668
|
const routeHooks = this.routePrecompiled.get(routeId);
|
|
@@ -213667,6 +213678,8 @@ ${errorMessages}`);
|
|
|
213667
213678
|
response = await routeHooks.hooks.onResponse.promise(response, ctx);
|
|
213668
213679
|
if (globalHooks)
|
|
213669
213680
|
response = await globalHooks.hooks.onResponse.promise(response, ctx);
|
|
213681
|
+
if (adapter)
|
|
213682
|
+
response = await adapter.hooks.onResponse.promise(response, ctx);
|
|
213670
213683
|
return response;
|
|
213671
213684
|
},
|
|
213672
213685
|
onRawResponse: async (result, ctx) => {
|
|
@@ -213679,11 +213692,13 @@ ${errorMessages}`);
|
|
|
213679
213692
|
current = await routeHooks.hooks.onRawResponse.promise(current, ctx);
|
|
213680
213693
|
if (globalHooks)
|
|
213681
213694
|
current = await globalHooks.hooks.onRawResponse.promise(current, ctx);
|
|
213695
|
+
if (adapter)
|
|
213696
|
+
current = await adapter.hooks.onRawResponse.promise(current, ctx);
|
|
213682
213697
|
return current;
|
|
213683
213698
|
},
|
|
213684
213699
|
onStreamChunk: async (chunk, ctx) => {
|
|
213685
213700
|
let chunks = [chunk];
|
|
213686
|
-
const phaseHooks = [upstreamHooks, serviceHooks, routeHooks, globalHooks];
|
|
213701
|
+
const phaseHooks = [upstreamHooks, serviceHooks, routeHooks, globalHooks, adapter];
|
|
213687
213702
|
for (const precompiledHooks of phaseHooks) {
|
|
213688
213703
|
if (!precompiledHooks)
|
|
213689
213704
|
continue;
|
|
@@ -213706,6 +213721,8 @@ ${errorMessages}`);
|
|
|
213706
213721
|
result = await routeHooks.hooks.onFlushStream.promise(result, ctx);
|
|
213707
213722
|
if (globalHooks)
|
|
213708
213723
|
result = await globalHooks.hooks.onFlushStream.promise(result, ctx);
|
|
213724
|
+
if (adapter)
|
|
213725
|
+
result = await adapter.hooks.onFlushStream.promise(result, ctx);
|
|
213709
213726
|
return result;
|
|
213710
213727
|
},
|
|
213711
213728
|
onError: async (ctx) => {
|
|
@@ -213717,6 +213734,8 @@ ${errorMessages}`);
|
|
|
213717
213734
|
await routeHooks.hooks.onError.promise(ctx);
|
|
213718
213735
|
if (globalHooks)
|
|
213719
213736
|
await globalHooks.hooks.onError.promise(ctx);
|
|
213737
|
+
if (adapter)
|
|
213738
|
+
await adapter.hooks.onError.promise(ctx);
|
|
213720
213739
|
}
|
|
213721
213740
|
};
|
|
213722
213741
|
}
|
|
@@ -213789,6 +213808,7 @@ ${errorMessages}`);
|
|
|
213789
213808
|
return (this.routeInstances.get(routeId) ?? []).flatMap((instance) => declaredDispatchTargets(instance.config.options ?? {}, this.pluginClasses.get(instance.handler.pluginName)?.configSchema ?? []));
|
|
213790
213809
|
}
|
|
213791
213810
|
async initializeFromConfig(config, dependencies) {
|
|
213811
|
+
this.dispatchConfig = config;
|
|
213792
213812
|
this.dependencies = dependencies;
|
|
213793
213813
|
if (dependencies) {
|
|
213794
213814
|
this.setServiceDependencies(dependencies.declarations());
|
|
@@ -220418,7 +220438,7 @@ async function executeForward(requestSnapshot, route, upstream, requestLog, conf
|
|
|
220418
220438
|
throw new ManagedUpstreamAccessError("managed upstream access is unavailable", { cause: error });
|
|
220419
220439
|
}
|
|
220420
220440
|
}
|
|
220421
|
-
const responseDemand = collectPluginBodyRequirements([phaseAwareHooks?.upstreamPhase, phaseAwareHooks?.servicePhase, phaseAwareHooks?.routePhase], {
|
|
220441
|
+
const responseDemand = collectPluginBodyRequirements([phaseAwareHooks?.upstreamPhase, phaseAwareHooks?.servicePhase, phaseAwareHooks?.routePhase, phaseAwareHooks?.dispatchAdapter], {
|
|
220422
220442
|
requestId,
|
|
220423
220443
|
method: requestSnapshot.method,
|
|
220424
220444
|
url: finalTargetUrl,
|
|
@@ -220614,7 +220634,7 @@ async function executeForward(requestSnapshot, route, upstream, requestLog, conf
|
|
|
220614
220634
|
streamCompletionState = proxyRes.body ? { interrupted: false, cancelled: false, complete: (outcome) => {
|
|
220615
220635
|
transportCompletion.settle(outcome);
|
|
220616
220636
|
} } : undefined;
|
|
220617
|
-
const preparedResponse = await abortable(prepareResponse(proxyRes, finalResponseRules, createExpressionContext(attemptContext), requestLog, reqLogger, config, undefined, streamRequestContext, streamCompletionState, phaseAwareHooks?.inbound, Boolean(phaseAwareHooks && (selectedDemand.response?.includes("sse-json") || phase1and2Context?.bodyRequirements?.response?.includes("sse-json")) && (phaseAwareHooks.globalPrecompiled?.hasStreamCallbacks || phaseAwareHooks.upstreamPhase.hasStreamCallbacks || phaseAwareHooks.servicePhase?.hasStreamCallbacks || phaseAwareHooks.routePhase.hasStreamCallbacks)), hasRawResponseCallbacks, attemptSignal, responseBodyOwners, restoredPluginWire ? pluginBodyOwner : undefined, pluginRepresentationChanged), attemptSignal, (lateResponse) => {
|
|
220637
|
+
const preparedResponse = await abortable(prepareResponse(proxyRes, finalResponseRules, createExpressionContext(attemptContext), requestLog, reqLogger, config, undefined, streamRequestContext, streamCompletionState, phaseAwareHooks?.inbound, Boolean(phaseAwareHooks && (selectedDemand.response?.includes("sse-json") || phase1and2Context?.bodyRequirements?.response?.includes("sse-json")) && (phaseAwareHooks.globalPrecompiled?.hasStreamCallbacks || phaseAwareHooks.upstreamPhase.hasStreamCallbacks || phaseAwareHooks.servicePhase?.hasStreamCallbacks || phaseAwareHooks.routePhase.hasStreamCallbacks || phaseAwareHooks.dispatchAdapter?.hasStreamCallbacks)), hasRawResponseCallbacks, attemptSignal, responseBodyOwners, restoredPluginWire ? pluginBodyOwner : undefined, pluginRepresentationChanged), attemptSignal, (lateResponse) => {
|
|
220618
220638
|
if (lateResponse.body instanceof ReadableStream) {
|
|
220619
220639
|
lateResponse.body.cancel("request aborted").catch(() => {
|
|
220620
220640
|
return;
|
|
@@ -221880,12 +221900,12 @@ async function executeHttpRequestInternal(req, config, runtimeContextOrSelector,
|
|
|
221880
221900
|
serviceId: route.service_id ?? config.services?.find((service) => service.name === route.service)?.id ?? null
|
|
221881
221901
|
}, invoke: (plugin, method, payload, target) => requestRegistry.invokeAdmissionRpc(plugin, method, payload, target, ownerLeases.get(`${plugin}\x00global`)) }), "onGatewayAdmissionSession");
|
|
221882
221902
|
}
|
|
221883
|
-
const demandHooks = requestRegistry?.getPrecompiledHooks(currentRouteId, undefined, routeServiceName);
|
|
221903
|
+
const demandHooks = requestRegistry?.getPrecompiledHooks(currentRouteId, undefined, routeServiceName, dispatch.adapter);
|
|
221884
221904
|
const demandContext = createPhaseContext(requestSnapshot, requestId, currentRouteId, routeServiceName);
|
|
221885
221905
|
if (dispatch.target)
|
|
221886
221906
|
demandContext.url = new URL(dispatch.context.url);
|
|
221887
221907
|
applyRoutePathRewriteToContext(demandContext, route, requestLog);
|
|
221888
|
-
const routeDemand = collectPluginBodyRequirements([demandHooks?.routePhase, demandHooks?.servicePhase], {
|
|
221908
|
+
const routeDemand = collectPluginBodyRequirements([demandHooks?.routePhase, demandHooks?.servicePhase, demandHooks?.dispatchAdapter], {
|
|
221889
221909
|
requestId,
|
|
221890
221910
|
method: req.method,
|
|
221891
221911
|
url: demandContext.url,
|
|
@@ -221952,7 +221972,7 @@ async function executeHttpRequestInternal(req, config, runtimeContextOrSelector,
|
|
|
221952
221972
|
}));
|
|
221953
221973
|
}
|
|
221954
221974
|
const scopedRegistry = requestRegistry;
|
|
221955
|
-
const requestPhaseHooks = scopedRegistry?.getPrecompiledHooks(currentRouteId, undefined, routeServiceName) ?? null;
|
|
221975
|
+
const requestPhaseHooks = scopedRegistry?.getPrecompiledHooks(currentRouteId, undefined, routeServiceName, dispatch.adapter) ?? null;
|
|
221956
221976
|
const phaseContext = createPhaseContext(requestSnapshot, requestId, currentRouteId, routeServiceName);
|
|
221957
221977
|
phaseContext.bodyWrite = !!dispatch.target || routeDemand.request === "json-write";
|
|
221958
221978
|
if (dispatch.target)
|
|
@@ -222068,7 +222088,7 @@ async function executeHttpRequestInternal(req, config, runtimeContextOrSelector,
|
|
|
222068
222088
|
};
|
|
222069
222089
|
let repairClaimed = false;
|
|
222070
222090
|
const proxyWithRouteRetry = async (selectedUpstream, attemptLogger) => {
|
|
222071
|
-
const phaseAwareHooks = scopedRegistry?.getPrecompiledHooks(currentRouteId, selectedUpstream.upstream_id, routeServiceName) ?? null;
|
|
222091
|
+
const phaseAwareHooks = scopedRegistry?.getPrecompiledHooks(currentRouteId, selectedUpstream.upstream_id, routeServiceName, dispatch.adapter) ?? null;
|
|
222072
222092
|
const attemptLoggers = new Map;
|
|
222073
222093
|
const runAttempt = async (requestOverride) => {
|
|
222074
222094
|
const currentLogger = requestOverride ? await createRequestLogger(req, { isFailoverAttempt: true, parentRequestId: reqLogger.getRequestId(), attemptUpstream: selectedUpstream.target, requestType: "final" }) : attemptLogger;
|
|
@@ -223040,7 +223060,7 @@ class DispatchPlugin {
|
|
|
223040
223060
|
throw new DataAdmissionError(422, "dispatch_target_unavailable");
|
|
223041
223061
|
route = { ...entry, service: service.name, service_id: service.id, endpoints: undefined };
|
|
223042
223062
|
}
|
|
223043
|
-
return { route, effective: resolveEffectiveRoute(config, route), context, entryRouteId: entry.id ?? entry.path, target: dispatch.target };
|
|
223063
|
+
return { route, effective: resolveEffectiveRoute(config, route), context, entryRouteId: entry.id ?? entry.path, target: dispatch.target, adapter: dispatch.adapter };
|
|
223044
223064
|
});
|
|
223045
223065
|
}
|
|
223046
223066
|
}
|
|
@@ -7540,6 +7540,16 @@ var PROVIDER_ADAPTERS = {
|
|
|
7540
7540
|
anthropic: createAnthropicAdapter(),
|
|
7541
7541
|
gemini: createGeminiAdapter()
|
|
7542
7542
|
};
|
|
7543
|
+
// packages/llms/src/responses-codec/common.ts
|
|
7544
|
+
var DEFAULT_RESPONSES_CODEC_LIMITS = Object.freeze({
|
|
7545
|
+
maxRequestBytes: 8 * 1024 * 1024,
|
|
7546
|
+
maxOutputBytes: 8 * 1024 * 1024,
|
|
7547
|
+
maxArgumentBytes: 1024 * 1024,
|
|
7548
|
+
maxItems: 1024
|
|
7549
|
+
});
|
|
7550
|
+
|
|
7551
|
+
// packages/llms/src/responses-codec/request.ts
|
|
7552
|
+
var contentNormalizer = new OpenAIProtocolConversion({ trimWhitespace: false });
|
|
7543
7553
|
// plugins/ai-transformer/server/index.ts
|
|
7544
7554
|
class AITransformerPluginImpl {
|
|
7545
7555
|
static name = "ai-transformer";
|
|
@@ -7652,4 +7662,4 @@ export {
|
|
|
7652
7662
|
server_default as default
|
|
7653
7663
|
};
|
|
7654
7664
|
|
|
7655
|
-
//# debugId=
|
|
7665
|
+
//# debugId=D002ECBBC7145E6164756E2164756E21
|