refactor(v2/providers): self-registration barrel + host container-config registry

Providers now mirror the channels pattern: each module calls
registerProvider() at top level, and providers/index.ts is a barrel of
side-effect imports. createProvider() becomes a thin registry lookup;
the closed ProviderName union is gone (now a string alias, since the
env var is a runtime string anyway).

Also adds a host-side provider-container-registry so providers can
declare their own mounts and env passthrough in src/providers/<name>.ts
instead of the container-runner having to know about each one. The
resolver runs once per spawn and threads provider + contribution
through buildMounts and buildContainerArgs so side effects (mkdir,
etc.) fire exactly once.

Both barrels are append-only — adding a new provider is a new file
+ one import line per barrel, no edits to existing files. The built-in
providers (claude, mock) don't need host-side config, so src/providers/
ships with an empty barrel; the container-side barrel imports both.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
gavrielc
2026-04-17 11:34:29 +03:00
parent a38c6a962f
commit 1f3b023a5a
9 changed files with 186 additions and 22 deletions

6
src/providers/index.ts Normal file
View File

@@ -0,0 +1,6 @@
// Host-side provider container-config barrel.
// Providers that need host-side container setup (extra mounts, env passthrough,
// per-session directories) self-register on import. Providers with no host
// needs (claude, mock) don't appear here.
//
// Skills add a new provider by appending one import line below.

View File

@@ -0,0 +1,63 @@
/**
* Host-side provider container-config registry.
*
* Providers that need per-spawn host-side setup (extra volume mounts, env var
* passthrough, per-session directories) register a function here. The
* container-runner resolves the session's effective provider name, looks up
* the registered config fn, and merges the returned mounts/env into the spawn
* args.
*
* Providers without host-side needs (e.g. `claude`, `mock`) don't appear in
* this registry at all — the lookup returns `undefined` and the spawn path
* proceeds with only the default mounts and env.
*
* Skills add a new provider's host config by creating `src/providers/<name>.ts`
* with a top-level `registerProviderContainerConfig(...)` call, then appending
* `import './<name>.js';` to `src/providers/index.ts` (the barrel).
*/
export interface VolumeMount {
hostPath: string;
containerPath: string;
readonly: boolean;
}
export interface ProviderContainerContext {
/** Per-session host directory: `<DATA_DIR>/v2-sessions/<session_id>`. */
sessionDir: string;
/** Agent group ID, for any per-group logic. */
agentGroupId: string;
/** `process.env` at spawn time — pull passthrough values from here. */
hostEnv: NodeJS.ProcessEnv;
}
export interface ProviderContainerContribution {
/** Extra volume mounts (merged with the default session/group/agent-runner mounts). */
mounts?: VolumeMount[];
/** Extra env vars to pass to the container (`-e KEY=VALUE`). */
env?: Record<string, string>;
}
export type ProviderContainerConfigFn = (
ctx: ProviderContainerContext,
) => ProviderContainerContribution;
const registry = new Map<string, ProviderContainerConfigFn>();
export function registerProviderContainerConfig(
name: string,
fn: ProviderContainerConfigFn,
): void {
if (registry.has(name)) {
throw new Error(`Provider container config already registered: ${name}`);
}
registry.set(name, fn);
}
export function getProviderContainerConfig(name: string): ProviderContainerConfigFn | undefined {
return registry.get(name);
}
export function listProviderContainerConfigNames(): string[] {
return [...registry.keys()];
}