Files
zn-ai/runtime-shared/lib/models.ts
duanshuwen ee72cf7261 feat: refactor HomePage to integrate agents store and update related components
feat: add runtime event handling for providers in ProvidersSection

feat: update routing to include Channels and Agents pages

feat: extend route types and navigation items for Channels and Agents

feat: implement agents store for managing agent data and interactions

fix: update chat store to utilize agents store for agent-related functionality

chore: export agents store from index

fix: enhance runtime types for better event handling

fix: update Vite config to handle dev server URL correctly
2026-04-18 14:56:32 +08:00

114 lines
3.0 KiB
TypeScript

import {
DEFAULT_AGENT_ID,
DEFAULT_CHANNEL_ACCOUNT_ID,
DEFAULT_MAIN_SESSION_SUFFIX,
buildChannelAccountOwnerKey,
normalizeChannelAccountId,
normalizeChannelType,
parseChannelAccountOwnerKey,
resolveChannelAccountOwner,
type AgentSummary,
type AgentChannelBinding,
type AgentChannelBindingInput,
type AgentChannelUnbindingInput,
type AgentsSnapshot,
} from './agents';
export type ModelSummary = AgentSummary;
export const DEFAULT_MODEL_ID = DEFAULT_AGENT_ID;
export type ModelsSnapshot = AgentsSnapshot & {
models: ModelSummary[];
agents?: ModelSummary[];
};
export interface ParsedSessionKey {
sessionKey: string;
agentId: string;
sessionId: string;
isAgentSession: boolean;
}
export function normalizeAgentId(value: string | null | undefined): string {
const normalized = String(value ?? '').trim().toLowerCase();
return normalized || DEFAULT_AGENT_ID;
}
export function normalizeSessionSuffix(value: string | null | undefined): string {
const normalized = String(value ?? '').trim().toLowerCase();
return normalized || DEFAULT_MAIN_SESSION_SUFFIX;
}
export function buildAgentSessionKey(agentId: string, sessionId: string): string {
return `agent:${normalizeAgentId(agentId)}:${normalizeSessionSuffix(sessionId)}`;
}
export function buildMainSessionKey(
agentId: string,
sessionId = DEFAULT_MAIN_SESSION_SUFFIX,
): string {
return buildAgentSessionKey(agentId, sessionId);
}
export function parseSessionKey(sessionKey: string): ParsedSessionKey {
const trimmed = String(sessionKey ?? '').trim();
if (trimmed.startsWith('agent:')) {
const parts = trimmed.split(':');
const agentId = normalizeAgentId(parts[1]);
const sessionId = normalizeSessionSuffix(parts.slice(2).join(':'));
return {
sessionKey: buildAgentSessionKey(agentId, sessionId),
agentId,
sessionId,
isAgentSession: true,
};
}
if (trimmed.startsWith('local:')) {
const parts = trimmed.split(':');
const agentId = normalizeAgentId(parts[1]);
const sessionId = normalizeSessionSuffix(parts.slice(2).join(':'));
return {
sessionKey: buildAgentSessionKey(agentId, sessionId),
agentId,
sessionId,
isAgentSession: true,
};
}
return {
sessionKey: trimmed,
agentId: DEFAULT_AGENT_ID,
sessionId: normalizeSessionSuffix(trimmed),
isAgentSession: false,
};
}
export function normalizeAgentSessionKey(sessionKey: string): string {
return parseSessionKey(sessionKey).sessionKey;
}
export const normalizeModelId = normalizeAgentId;
export const buildModelSessionKey = buildAgentSessionKey;
export const normalizeModelSessionKey = normalizeAgentSessionKey;
export {
DEFAULT_AGENT_ID,
DEFAULT_CHANNEL_ACCOUNT_ID,
DEFAULT_MAIN_SESSION_SUFFIX,
buildChannelAccountOwnerKey,
normalizeChannelAccountId,
normalizeChannelType,
parseChannelAccountOwnerKey,
resolveChannelAccountOwner,
};
export type {
AgentSummary,
AgentChannelBinding,
AgentChannelBindingInput,
AgentChannelUnbindingInput,
AgentsSnapshot,
};