api.ts 1.3 KB

1234567891011121314151617181920212223242526272829
  1. /**
  2. * `createAgentRuntime`: Entry factory for agent-runtime. The host passes `{ base, additions,
  3. * overlays, profile }` and receives an `AgentRuntime` exposing `assembleTurn / setEnabled /
  4. * listExtensions / disposeSession`.
  5. *
  6. * Implementation: `Registry` holds assembly state and implements `AgentRuntime`. Each extension's
  7. * `init` receives an independent owner-scoped `ExtensionAPI` facade, which can register
  8. * contributions only during that owner's init window. The factory narrows the return type to
  9. * `AgentRuntime`, so the host does not receive SPI methods such as `registerTool`.
  10. */
  11. import { Registry } from './registry.js';
  12. import { resolveExtensions } from './resolve.js';
  13. import type { AgentRuntime, RuntimeOptions } from './types.js';
  14. export async function createAgentRuntime(opts: RuntimeOptions): Promise<AgentRuntime> {
  15. const extensions = resolveExtensions(
  16. opts.base,
  17. opts.additions ?? [],
  18. opts.overlays ?? {},
  19. opts.profile,
  20. );
  21. const overlay = opts.profile ? opts.overlays?.[opts.profile] : undefined;
  22. const params: Readonly<Record<string, unknown>> = overlay?.param ?? {};
  23. const registry = new Registry(params);
  24. await registry.initExtensions(extensions);
  25. // Narrow the return type to AgentRuntime; owner-scoped ExtensionAPI exists only during init.
  26. return registry;
  27. }