// dashboard/src/lib/dream-kb/scope.ts import type { DreamScope } from "./types"; export const DEFAULT_SEGMENT = "__default__"; /** 空/undefined → "__default__";其余(含 "*")原样返回。 */ export function normalizeSegment(v: string | undefined): string { return v && v.length > 0 ? v : DEFAULT_SEGMENT; } /** 拼四级 concrete scopeKey,空段填 __default__。 */ export function buildScopeKey(scope: DreamScope): string { return [ normalizeSegment(scope.appId), normalizeSegment(scope.tenantId), normalizeSegment(scope.agentId), normalizeSegment(scope.runId), ].join("/"); } /** * 逐级精确匹配:filter 某级为 "*" 时该级任意,否则必须字符串全等。 * 禁止前缀匹配。arity 不同直接不匹配。 */ export function matchesScope( docScopeKey: string, filterScopeKey: string, ): boolean { const doc = docScopeKey.split("/"); const filter = filterScopeKey.split("/"); if (doc.length !== filter.length) return false; for (let i = 0; i < doc.length; i++) { if (filter[i] === "*") continue; if (filter[i] !== doc[i]) return false; } return true; }