Skip to content

Refactor: Provider handler registry for extra-params.ts #25311

@Elarwei001

Description

@Elarwei001

Problem

extra-params.ts currently has multiple hardcoded provider checks:

if (provider === "anthropic") { ... }
if (provider === "amazon-bedrock" && ...) { ... }
if (provider === "openrouter") { ... }
if (provider === "zai" || provider === "z-ai") { ... }
if (provider === "moonshot" && sessionKey) { ... }  // PR #25104

As more providers add special handling, this pattern becomes harder to maintain.

Proposal

Replace hardcoded checks with a provider handler registry:

type ProviderHandler = {
  createWrapper?: (baseStreamFn: StreamFn, config: unknown, ...args: unknown[]) => StreamFn;
  enabledWhen?: (provider: string, config: Record<string, unknown>) => boolean;
};

const providerHandlers: Record<string, ProviderHandler> = {
  moonshot: {
    createWrapper: createMoonshotCacheWrapper,
    enabledWhen: (_, config) => config.contextCache?.enabled === true,
  },
  openrouter: {
    createWrapper: createOpenRouterWrapper,
    enabledWhen: () => true,
  },
  // ...
};

// In applyExtraParamsToAgent:
for (const [name, handler] of Object.entries(providerHandlers)) {
  if (provider === name && handler.enabledWhen?.(provider, merged)) {
    agent.streamFn = handler.createWrapper?.(agent.streamFn, ...);
  }
}

Benefits

  • Easier to add new providers
  • Clearer separation of concerns
  • Each provider's logic is self-contained
  • Potentially config-driven in the future

Context

Discovered during PR #25104 (Moonshot context caching). Keeping current hardcoded approach for that PR to minimize scope.

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleMarked as stale due to inactivity

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions