refactor!: rename chat providers to channels
This commit is contained in:
13
src/utils/message-channel.test.ts
Normal file
13
src/utils/message-channel.test.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { resolveGatewayMessageChannel } from "./message-channel.js";
|
||||
|
||||
describe("message-channel", () => {
|
||||
it("normalizes gateway message channels and rejects unknown values", () => {
|
||||
expect(resolveGatewayMessageChannel("discord")).toBe("discord");
|
||||
expect(resolveGatewayMessageChannel(" imsg ")).toBe("imessage");
|
||||
expect(resolveGatewayMessageChannel("teams")).toBe("msteams");
|
||||
expect(resolveGatewayMessageChannel("web")).toBeUndefined();
|
||||
expect(resolveGatewayMessageChannel("nope")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
109
src/utils/message-channel.ts
Normal file
109
src/utils/message-channel.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import {
|
||||
CHANNEL_IDS,
|
||||
listChatChannelAliases,
|
||||
normalizeChatChannelId,
|
||||
} from "../channels/registry.js";
|
||||
import {
|
||||
GATEWAY_CLIENT_MODES,
|
||||
GATEWAY_CLIENT_NAMES,
|
||||
type GatewayClientMode,
|
||||
type GatewayClientName,
|
||||
normalizeGatewayClientMode,
|
||||
normalizeGatewayClientName,
|
||||
} from "../gateway/protocol/client-info.js";
|
||||
|
||||
export const INTERNAL_MESSAGE_CHANNEL = "webchat" as const;
|
||||
export type InternalMessageChannel = typeof INTERNAL_MESSAGE_CHANNEL;
|
||||
|
||||
export { GATEWAY_CLIENT_NAMES, GATEWAY_CLIENT_MODES };
|
||||
export type { GatewayClientName, GatewayClientMode };
|
||||
export { normalizeGatewayClientName, normalizeGatewayClientMode };
|
||||
|
||||
type GatewayClientInfoLike = {
|
||||
mode?: string | null;
|
||||
id?: string | null;
|
||||
};
|
||||
|
||||
export function isGatewayCliClient(
|
||||
client?: GatewayClientInfoLike | null,
|
||||
): boolean {
|
||||
return normalizeGatewayClientMode(client?.mode) === GATEWAY_CLIENT_MODES.CLI;
|
||||
}
|
||||
|
||||
export function isInternalMessageChannel(
|
||||
raw?: string | null,
|
||||
): raw is InternalMessageChannel {
|
||||
return normalizeMessageChannel(raw) === INTERNAL_MESSAGE_CHANNEL;
|
||||
}
|
||||
|
||||
export function isWebchatClient(
|
||||
client?: GatewayClientInfoLike | null,
|
||||
): boolean {
|
||||
const mode = normalizeGatewayClientMode(client?.mode);
|
||||
if (mode === GATEWAY_CLIENT_MODES.WEBCHAT) return true;
|
||||
return (
|
||||
normalizeGatewayClientName(client?.id) === GATEWAY_CLIENT_NAMES.WEBCHAT_UI
|
||||
);
|
||||
}
|
||||
|
||||
export function normalizeMessageChannel(
|
||||
raw?: string | null,
|
||||
): string | undefined {
|
||||
const normalized = raw?.trim().toLowerCase();
|
||||
if (!normalized) return undefined;
|
||||
if (normalized === INTERNAL_MESSAGE_CHANNEL) return INTERNAL_MESSAGE_CHANNEL;
|
||||
return normalizeChatChannelId(normalized) ?? normalized;
|
||||
}
|
||||
|
||||
export const DELIVERABLE_MESSAGE_CHANNELS = CHANNEL_IDS;
|
||||
|
||||
export type DeliverableMessageChannel =
|
||||
(typeof DELIVERABLE_MESSAGE_CHANNELS)[number];
|
||||
|
||||
export type GatewayMessageChannel =
|
||||
| DeliverableMessageChannel
|
||||
| InternalMessageChannel;
|
||||
|
||||
export const GATEWAY_MESSAGE_CHANNELS = [
|
||||
...DELIVERABLE_MESSAGE_CHANNELS,
|
||||
INTERNAL_MESSAGE_CHANNEL,
|
||||
] as const;
|
||||
|
||||
export const GATEWAY_AGENT_CHANNEL_ALIASES = listChatChannelAliases();
|
||||
|
||||
export type GatewayAgentChannelHint = GatewayMessageChannel | "last";
|
||||
|
||||
export const GATEWAY_AGENT_CHANNEL_VALUES = Array.from(
|
||||
new Set([
|
||||
...GATEWAY_MESSAGE_CHANNELS,
|
||||
"last",
|
||||
...GATEWAY_AGENT_CHANNEL_ALIASES,
|
||||
]),
|
||||
);
|
||||
|
||||
export function isGatewayMessageChannel(
|
||||
value: string,
|
||||
): value is GatewayMessageChannel {
|
||||
return (GATEWAY_MESSAGE_CHANNELS as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
export function isDeliverableMessageChannel(
|
||||
value: string,
|
||||
): value is DeliverableMessageChannel {
|
||||
return (DELIVERABLE_MESSAGE_CHANNELS as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
export function resolveGatewayMessageChannel(
|
||||
raw?: string | null,
|
||||
): GatewayMessageChannel | undefined {
|
||||
const normalized = normalizeMessageChannel(raw);
|
||||
if (!normalized) return undefined;
|
||||
return isGatewayMessageChannel(normalized) ? normalized : undefined;
|
||||
}
|
||||
|
||||
export function resolveMessageChannel(
|
||||
primary?: string | null,
|
||||
fallback?: string | null,
|
||||
): string | undefined {
|
||||
return normalizeMessageChannel(primary) ?? normalizeMessageChannel(fallback);
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { resolveGatewayMessageProvider } from "./message-provider.js";
|
||||
|
||||
describe("message-provider", () => {
|
||||
it("normalizes gateway message providers and rejects unknown values", () => {
|
||||
expect(resolveGatewayMessageProvider("discord")).toBe("discord");
|
||||
expect(resolveGatewayMessageProvider(" imsg ")).toBe("imessage");
|
||||
expect(resolveGatewayMessageProvider("teams")).toBe("msteams");
|
||||
expect(resolveGatewayMessageProvider("web")).toBeUndefined();
|
||||
expect(resolveGatewayMessageProvider("nope")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -1,112 +0,0 @@
|
||||
import {
|
||||
GATEWAY_CLIENT_MODES,
|
||||
GATEWAY_CLIENT_NAMES,
|
||||
type GatewayClientMode,
|
||||
type GatewayClientName,
|
||||
normalizeGatewayClientMode,
|
||||
normalizeGatewayClientName,
|
||||
} from "../gateway/protocol/client-info.js";
|
||||
import {
|
||||
listChatProviderAliases,
|
||||
normalizeChatProviderId,
|
||||
PROVIDER_IDS,
|
||||
} from "../providers/registry.js";
|
||||
|
||||
export const INTERNAL_MESSAGE_PROVIDER = "webchat" as const;
|
||||
export type InternalMessageProvider = typeof INTERNAL_MESSAGE_PROVIDER;
|
||||
|
||||
export { GATEWAY_CLIENT_NAMES, GATEWAY_CLIENT_MODES };
|
||||
export type { GatewayClientName, GatewayClientMode };
|
||||
export { normalizeGatewayClientName, normalizeGatewayClientMode };
|
||||
|
||||
type GatewayClientInfoLike = {
|
||||
mode?: string | null;
|
||||
id?: string | null;
|
||||
};
|
||||
|
||||
export function isGatewayCliClient(
|
||||
client?: GatewayClientInfoLike | null,
|
||||
): boolean {
|
||||
return normalizeGatewayClientMode(client?.mode) === GATEWAY_CLIENT_MODES.CLI;
|
||||
}
|
||||
|
||||
export function isInternalMessageProvider(
|
||||
raw?: string | null,
|
||||
): raw is InternalMessageProvider {
|
||||
return normalizeMessageProvider(raw) === INTERNAL_MESSAGE_PROVIDER;
|
||||
}
|
||||
|
||||
export function isWebchatClient(
|
||||
client?: GatewayClientInfoLike | null,
|
||||
): boolean {
|
||||
const mode = normalizeGatewayClientMode(client?.mode);
|
||||
if (mode === GATEWAY_CLIENT_MODES.WEBCHAT) return true;
|
||||
return (
|
||||
normalizeGatewayClientName(client?.id) === GATEWAY_CLIENT_NAMES.WEBCHAT_UI
|
||||
);
|
||||
}
|
||||
|
||||
export function normalizeMessageProvider(
|
||||
raw?: string | null,
|
||||
): string | undefined {
|
||||
const normalized = raw?.trim().toLowerCase();
|
||||
if (!normalized) return undefined;
|
||||
if (normalized === INTERNAL_MESSAGE_PROVIDER)
|
||||
return INTERNAL_MESSAGE_PROVIDER;
|
||||
return normalizeChatProviderId(normalized) ?? normalized;
|
||||
}
|
||||
|
||||
export const DELIVERABLE_MESSAGE_PROVIDERS = PROVIDER_IDS;
|
||||
|
||||
export type DeliverableMessageProvider =
|
||||
(typeof DELIVERABLE_MESSAGE_PROVIDERS)[number];
|
||||
|
||||
export type GatewayMessageProvider =
|
||||
| DeliverableMessageProvider
|
||||
| InternalMessageProvider;
|
||||
|
||||
export const GATEWAY_MESSAGE_PROVIDERS = [
|
||||
...DELIVERABLE_MESSAGE_PROVIDERS,
|
||||
INTERNAL_MESSAGE_PROVIDER,
|
||||
] as const;
|
||||
|
||||
export const GATEWAY_AGENT_PROVIDER_ALIASES = listChatProviderAliases();
|
||||
|
||||
export type GatewayAgentProviderHint = GatewayMessageProvider | "last";
|
||||
|
||||
export const GATEWAY_AGENT_PROVIDER_VALUES = Array.from(
|
||||
new Set([
|
||||
...GATEWAY_MESSAGE_PROVIDERS,
|
||||
"last",
|
||||
...GATEWAY_AGENT_PROVIDER_ALIASES,
|
||||
]),
|
||||
);
|
||||
|
||||
export function isGatewayMessageProvider(
|
||||
value: string,
|
||||
): value is GatewayMessageProvider {
|
||||
return (GATEWAY_MESSAGE_PROVIDERS as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
export function isDeliverableMessageProvider(
|
||||
value: string,
|
||||
): value is DeliverableMessageProvider {
|
||||
return (DELIVERABLE_MESSAGE_PROVIDERS as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
export function resolveGatewayMessageProvider(
|
||||
raw?: string | null,
|
||||
): GatewayMessageProvider | undefined {
|
||||
const normalized = normalizeMessageProvider(raw);
|
||||
if (!normalized) return undefined;
|
||||
return isGatewayMessageProvider(normalized) ? normalized : undefined;
|
||||
}
|
||||
|
||||
export function resolveMessageProvider(
|
||||
primary?: string | null,
|
||||
fallback?: string | null,
|
||||
): string | undefined {
|
||||
return (
|
||||
normalizeMessageProvider(primary) ?? normalizeMessageProvider(fallback)
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user