| 418 | // --------------------------------------------------------------------------- |
| 419 | |
| 420 | export class PackAgent implements IPackAgent { |
| 421 | private options: PackAgentOptions; |
| 422 | private channels = new Map<string, ChannelSession>(); |
| 423 | private pendingSessionCreations = new Map<string, Promise<ChannelSession>>(); |
| 424 | private pendingAbortChannels = new Set<string>(); |
| 425 | private schedulerRef: { current: SchedulerAdapter | null } = { |
| 426 | current: null, |
| 427 | }; |
| 428 | private authStorage: AuthStorage; |
| 429 | private readonly delegatedCustomToolClient = new DelegatedCustomToolClient(); |
| 430 | private readonly hostIpcClient = new HostIpcClient(); |
| 431 | |
| 432 | constructor(options: PackAgentOptions) { |
| 433 | this.options = options; |
| 434 | |
| 435 | // Use ConfigFileAuthBackend to persist OAuth credentials in config.json._auth |
| 436 | const configPath = path.resolve(options.rootDir, "data", "config.json"); |
| 437 | const backend = new ConfigFileAuthBackend(configPath); |
| 438 | this.authStorage = AuthStorage.fromStorage(backend); |
| 439 | |
| 440 | // For API Key providers, set runtime key (not persisted to _auth) |
| 441 | const providerMeta = SUPPORTED_PROVIDERS[options.provider]; |
| 442 | if (providerMeta?.authType === "api_key" && options.apiKey) { |
| 443 | this.authStorage.setRuntimeApiKey(options.provider, options.apiKey); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /** Get the shared AuthStorage instance (used by OAuth API endpoints) */ |
| 448 | getAuthStorage(): AuthStorage { |
| 449 | return this.authStorage; |
| 450 | } |
| 451 | |
| 452 | /** Update runtime auth when provider/apiKey changes */ |
| 453 | updateAuth(provider: string, apiKey?: string): void { |
| 454 | // Remove old runtime key |
| 455 | this.authStorage.removeRuntimeApiKey(this.options.provider); |
| 456 | this.options.provider = provider; |
| 457 | this.options.apiKey = apiKey ?? ""; |
| 458 | if (apiKey) { |
| 459 | this.authStorage.setRuntimeApiKey(provider, apiKey); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Inject scheduler reference (called by server.ts after adapter init). |
| 465 | */ |
| 466 | setScheduler(scheduler: SchedulerAdapter): void { |
| 467 | this.schedulerRef.current = scheduler; |
| 468 | } |
| 469 | |
| 470 | private async createCustomTools( |
| 471 | adapter: RuntimePlatform, |
| 472 | channelId: string, |
| 473 | fileOutputCallbackRef: { current: FileOutputCallback | null }, |
| 474 | delegatedToolRunContextRef: DelegatedToolRunContextRef, |
| 475 | ): Promise<any[]> { |
| 476 | const delegatedDefinitions = |
| 477 | await this.delegatedCustomToolClient.listDefinitions(); |
nothing calls this directly
no outgoing calls
no test coverage detected