( config: RuntimeRouterConfig )
| 326 | } |
| 327 | |
| 328 | export function createRuntimeRouter( |
| 329 | config: RuntimeRouterConfig |
| 330 | ): RuntimeRouter { |
| 331 | const normalized = normalizeConfig(config) |
| 332 | const { |
| 333 | baseUrl, |
| 334 | serveEndpoint, |
| 335 | webhookPath, |
| 336 | registry, |
| 337 | idleTimeout, |
| 338 | heartbeatInterval, |
| 339 | createElectricTools, |
| 340 | registrationConcurrency, |
| 341 | defaultDispatchPolicyForType, |
| 342 | serverHeaders, |
| 343 | webhookSignature, |
| 344 | } = normalized |
| 345 | |
| 346 | const getRegisteredType = (name: string) => |
| 347 | registry ? registry.get(name) : getEntityType(name) |
| 348 | const getRegisteredTypes = () => |
| 349 | registry ? registry.list() : listEntityTypes() |
| 350 | |
| 351 | // Index the runtime's profiles by name. Duplicate names are a |
| 352 | // configuration bug — fail fast rather than silently dropping one. |
| 353 | const sandboxProfiles = new Map<string, SandboxProfile>() |
| 354 | for (const profile of config.sandboxProfiles ?? []) { |
| 355 | if (sandboxProfiles.has(profile.name)) { |
| 356 | throw new Error( |
| 357 | `[agent-runtime] duplicate sandbox profile name "${profile.name}" registered on createRuntimeRouter` |
| 358 | ) |
| 359 | } |
| 360 | sandboxProfiles.set(profile.name, profile) |
| 361 | } |
| 362 | |
| 363 | const wakeConfig: ProcessWakeConfig = { |
| 364 | baseUrl, |
| 365 | registry, |
| 366 | createElectricTools, |
| 367 | idleTimeout, |
| 368 | heartbeatInterval, |
| 369 | sandboxProfiles, |
| 370 | } |
| 371 | const debugRegistrationTiming = |
| 372 | process.env.ELECTRIC_AGENTS_DEBUG_REGISTRATION_TIMING === `1` |
| 373 | const pendingWakes = new Set<Promise<void>>() |
| 374 | const pendingWakeLabels = new Map<Promise<void>, string>() |
| 375 | const pendingWakeControllers = new Map<Promise<void>, AbortController>() |
| 376 | const wakeErrors: Array<Error> = [] |
| 377 | const debugCleanup = process.env.ELECTRIC_AGENTS_DEBUG_CLEANUP === `1` |
| 378 | |
| 379 | const registrationHeaders = async (): Promise<Headers> => { |
| 380 | const init = |
| 381 | typeof serverHeaders === `function` |
| 382 | ? await serverHeaders() |
| 383 | : serverHeaders |
| 384 | const headers = new Headers(init) |
| 385 | headers.set(`content-type`, `application/json`) |
no test coverage detected