|
| 1 | +import type { AgentCrumbsConfig } from "./types.js"; |
| 2 | + |
| 3 | +const DEFAULT_PORT = 8374; |
| 4 | + |
| 5 | +type ParsedConfig = |
| 6 | + | { enabled: false } |
| 7 | + | { |
| 8 | + enabled: true; |
| 9 | + app?: string; |
| 10 | + includes: RegExp[]; |
| 11 | + excludes: RegExp[]; |
| 12 | + port: number; |
| 13 | + format: "pretty" | "json"; |
| 14 | + }; |
| 15 | + |
| 16 | +let cachedConfig: ParsedConfig | undefined; |
| 17 | +let cachedApp: string | undefined; |
| 18 | + |
| 19 | +declare const globalThis: { |
| 20 | + __AGENTCRUMBS__?: string | AgentCrumbsConfig; |
| 21 | + __AGENTCRUMBS_APP__?: string; |
| 22 | +}; |
| 23 | + |
| 24 | +function namespaceToRegex(pattern: string): RegExp { |
| 25 | + const escaped = pattern |
| 26 | + .replace(/[.+?^${}()|[\]\\]/g, "\\$&") |
| 27 | + .replace(/\*/g, ".*?"); |
| 28 | + return new RegExp(`^${escaped}$`); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Configure agentcrumbs in the browser. |
| 33 | + * |
| 34 | + * @example |
| 35 | + * configure("*") // enable all namespaces |
| 36 | + * configure("myapp:*") // enable namespaces matching pattern |
| 37 | + * configure({ ns: "*", app: "my-app", format: "pretty" }) |
| 38 | + */ |
| 39 | +export function configure(config: AgentCrumbsConfig | string): void { |
| 40 | + cachedConfig = undefined; |
| 41 | + cachedApp = undefined; |
| 42 | + |
| 43 | + if (typeof config === "string") { |
| 44 | + (globalThis as Record<string, unknown>).__AGENTCRUMBS__ = config; |
| 45 | + } else { |
| 46 | + (globalThis as Record<string, unknown>).__AGENTCRUMBS__ = config; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +export function parseConfig(): ParsedConfig { |
| 51 | + if (cachedConfig !== undefined) return cachedConfig; |
| 52 | + |
| 53 | + const raw = globalThis.__AGENTCRUMBS__; |
| 54 | + if (!raw) { |
| 55 | + cachedConfig = { enabled: false }; |
| 56 | + return cachedConfig; |
| 57 | + } |
| 58 | + |
| 59 | + let config: AgentCrumbsConfig; |
| 60 | + |
| 61 | + if (typeof raw === "object") { |
| 62 | + config = raw; |
| 63 | + } else { |
| 64 | + // Shorthand: "1", "*", "true" → enable all |
| 65 | + if (raw === "1" || raw === "*" || raw === "true") { |
| 66 | + cachedConfig = { |
| 67 | + enabled: true, |
| 68 | + includes: [/^.*$/], |
| 69 | + excludes: [], |
| 70 | + port: DEFAULT_PORT, |
| 71 | + format: "pretty", |
| 72 | + }; |
| 73 | + return cachedConfig; |
| 74 | + } |
| 75 | + |
| 76 | + // Try parsing as JSON config object |
| 77 | + try { |
| 78 | + config = JSON.parse(raw) as AgentCrumbsConfig; |
| 79 | + } catch { |
| 80 | + config = { ns: raw }; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + const parts = config.ns.split(/[\s,]+/).filter(Boolean); |
| 85 | + const includes: RegExp[] = []; |
| 86 | + const excludes: RegExp[] = []; |
| 87 | + |
| 88 | + for (const part of parts) { |
| 89 | + if (part.startsWith("-")) { |
| 90 | + excludes.push(namespaceToRegex(part.slice(1))); |
| 91 | + } else { |
| 92 | + includes.push(namespaceToRegex(part)); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (includes.length === 0) { |
| 97 | + cachedConfig = { enabled: false }; |
| 98 | + return cachedConfig; |
| 99 | + } |
| 100 | + |
| 101 | + cachedConfig = { |
| 102 | + enabled: true, |
| 103 | + app: config.app, |
| 104 | + includes, |
| 105 | + excludes, |
| 106 | + port: config.port ?? DEFAULT_PORT, |
| 107 | + format: config.format ?? "pretty", |
| 108 | + }; |
| 109 | + return cachedConfig; |
| 110 | +} |
| 111 | + |
| 112 | +export function isNamespaceEnabled(namespace: string): boolean { |
| 113 | + const config = parseConfig(); |
| 114 | + if (!config.enabled) return false; |
| 115 | + |
| 116 | + const included = config.includes.some((re) => re.test(namespace)); |
| 117 | + if (!included) return false; |
| 118 | + |
| 119 | + const excluded = config.excludes.some((re) => re.test(namespace)); |
| 120 | + return !excluded; |
| 121 | +} |
| 122 | + |
| 123 | +export function getCollectorUrl(): string { |
| 124 | + const config = parseConfig(); |
| 125 | + const port = config.enabled ? config.port : DEFAULT_PORT; |
| 126 | + return `http://localhost:${port}/crumb`; |
| 127 | +} |
| 128 | + |
| 129 | +export function getFormat(): "pretty" | "json" { |
| 130 | + const config = parseConfig(); |
| 131 | + if (!config.enabled) return "pretty"; |
| 132 | + return config.format; |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Resolve the app name. Priority: |
| 137 | + * 1. `app` field from configure() config |
| 138 | + * 2. `globalThis.__AGENTCRUMBS_APP__` |
| 139 | + * 3. Fallback: "browser" |
| 140 | + */ |
| 141 | +export function getApp(): string { |
| 142 | + if (cachedApp !== undefined) return cachedApp; |
| 143 | + |
| 144 | + const config = parseConfig(); |
| 145 | + if (config.enabled && config.app) { |
| 146 | + cachedApp = config.app; |
| 147 | + return cachedApp; |
| 148 | + } |
| 149 | + |
| 150 | + const globalApp = globalThis.__AGENTCRUMBS_APP__; |
| 151 | + if (globalApp) { |
| 152 | + cachedApp = globalApp; |
| 153 | + return cachedApp; |
| 154 | + } |
| 155 | + |
| 156 | + cachedApp = "browser"; |
| 157 | + return cachedApp; |
| 158 | +} |
| 159 | + |
| 160 | +/** Reset cached config — useful for tests */ |
| 161 | +export function resetConfig(): void { |
| 162 | + cachedConfig = undefined; |
| 163 | +} |
| 164 | + |
| 165 | +/** Reset cached app — useful for tests */ |
| 166 | +export function resetApp(): void { |
| 167 | + cachedApp = undefined; |
| 168 | +} |
0 commit comments