refactor: dedupe channel and gateway surfaces

This commit is contained in:
Peter Steinberger
2026-03-02 19:48:12 +00:00
parent 9617ac9dd5
commit 9d30159fcd
44 changed files with 1072 additions and 1479 deletions

View File

@@ -8,6 +8,22 @@ type RoutePeerLike = {
id: string | number;
};
type InboundEnvelopeFormatParams<TEnvelope> = {
channel: string;
from: string;
timestamp?: number;
previousTimestamp?: number;
envelope: TEnvelope;
body: string;
};
type InboundRouteResolveParams<TConfig, TPeer extends RoutePeerLike> = {
cfg: TConfig;
channel: string;
accountId: string;
peer: TPeer;
};
export function createInboundEnvelopeBuilder<TConfig, TEnvelope>(params: {
cfg: TConfig;
route: RouteLike;
@@ -15,14 +31,7 @@ export function createInboundEnvelopeBuilder<TConfig, TEnvelope>(params: {
resolveStorePath: (store: string | undefined, opts: { agentId: string }) => string;
readSessionUpdatedAt: (params: { storePath: string; sessionKey: string }) => number | undefined;
resolveEnvelopeFormatOptions: (cfg: TConfig) => TEnvelope;
formatAgentEnvelope: (params: {
channel: string;
from: string;
timestamp?: number;
previousTimestamp?: number;
envelope: TEnvelope;
body: string;
}) => string;
formatAgentEnvelope: (params: InboundEnvelopeFormatParams<TEnvelope>) => string;
}) {
const storePath = params.resolveStorePath(params.sessionStore, {
agentId: params.route.agentId,
@@ -55,24 +64,12 @@ export function resolveInboundRouteEnvelopeBuilder<
channel: string;
accountId: string;
peer: TPeer;
resolveAgentRoute: (params: {
cfg: TConfig;
channel: string;
accountId: string;
peer: TPeer;
}) => TRoute;
resolveAgentRoute: (params: InboundRouteResolveParams<TConfig, TPeer>) => TRoute;
sessionStore?: string;
resolveStorePath: (store: string | undefined, opts: { agentId: string }) => string;
readSessionUpdatedAt: (params: { storePath: string; sessionKey: string }) => number | undefined;
resolveEnvelopeFormatOptions: (cfg: TConfig) => TEnvelope;
formatAgentEnvelope: (params: {
channel: string;
from: string;
timestamp?: number;
previousTimestamp?: number;
envelope: TEnvelope;
body: string;
}) => string;
formatAgentEnvelope: (params: InboundEnvelopeFormatParams<TEnvelope>) => string;
}): {
route: TRoute;
buildEnvelope: ReturnType<typeof createInboundEnvelopeBuilder<TConfig, TEnvelope>>;
@@ -102,12 +99,7 @@ type InboundRouteEnvelopeRuntime<
TPeer extends RoutePeerLike,
> = {
routing: {
resolveAgentRoute: (params: {
cfg: TConfig;
channel: string;
accountId: string;
peer: TPeer;
}) => TRoute;
resolveAgentRoute: (params: InboundRouteResolveParams<TConfig, TPeer>) => TRoute;
};
session: {
resolveStorePath: (store: string | undefined, opts: { agentId: string }) => string;
@@ -115,14 +107,7 @@ type InboundRouteEnvelopeRuntime<
};
reply: {
resolveEnvelopeFormatOptions: (cfg: TConfig) => TEnvelope;
formatAgentEnvelope: (params: {
channel: string;
from: string;
timestamp?: number;
previousTimestamp?: number;
envelope: TEnvelope;
body: string;
}) => string;
formatAgentEnvelope: (params: InboundEnvelopeFormatParams<TEnvelope>) => string;
};
};

View File

@@ -112,6 +112,23 @@ export type WebhookTargetMatchResult<T> =
| { kind: "single"; target: T }
| { kind: "ambiguous" };
function updateMatchedWebhookTarget<T>(
matched: T | undefined,
target: T,
): { ok: true; matched: T } | { ok: false; result: WebhookTargetMatchResult<T> } {
if (matched) {
return { ok: false, result: { kind: "ambiguous" } };
}
return { ok: true, matched: target };
}
function finalizeMatchedWebhookTarget<T>(matched: T | undefined): WebhookTargetMatchResult<T> {
if (!matched) {
return { kind: "none" };
}
return { kind: "single", target: matched };
}
export function resolveSingleWebhookTarget<T>(
targets: readonly T[],
isMatch: (target: T) => boolean,
@@ -121,15 +138,13 @@ export function resolveSingleWebhookTarget<T>(
if (!isMatch(target)) {
continue;
}
if (matched) {
return { kind: "ambiguous" };
const updated = updateMatchedWebhookTarget(matched, target);
if (!updated.ok) {
return updated.result;
}
matched = target;
matched = updated.matched;
}
if (!matched) {
return { kind: "none" };
}
return { kind: "single", target: matched };
return finalizeMatchedWebhookTarget(matched);
}
export async function resolveSingleWebhookTargetAsync<T>(
@@ -141,15 +156,13 @@ export async function resolveSingleWebhookTargetAsync<T>(
if (!(await isMatch(target))) {
continue;
}
if (matched) {
return { kind: "ambiguous" };
const updated = updateMatchedWebhookTarget(matched, target);
if (!updated.ok) {
return updated.result;
}
matched = target;
matched = updated.matched;
}
if (!matched) {
return { kind: "none" };
}
return { kind: "single", target: matched };
return finalizeMatchedWebhookTarget(matched);
}
export async function resolveWebhookTargetWithAuthOrReject<T>(params: {