Use injected resolveApiKey in AgentSession for settings.json support
This commit is contained in:
@@ -56,6 +56,8 @@ export interface AgentSessionConfig {
|
|||||||
/** Custom tools for session lifecycle events */
|
/** Custom tools for session lifecycle events */
|
||||||
customTools?: LoadedCustomTool[];
|
customTools?: LoadedCustomTool[];
|
||||||
skillsSettings?: Required<SkillsSettings>;
|
skillsSettings?: Required<SkillsSettings>;
|
||||||
|
/** Resolve API key for a model. Default: getApiKeyForModel */
|
||||||
|
resolveApiKey?: (model: Model<any>) => Promise<string | undefined>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Options for AgentSession.prompt() */
|
/** Options for AgentSession.prompt() */
|
||||||
@@ -151,6 +153,9 @@ export class AgentSession {
|
|||||||
|
|
||||||
private _skillsSettings: Required<SkillsSettings> | undefined;
|
private _skillsSettings: Required<SkillsSettings> | undefined;
|
||||||
|
|
||||||
|
// API key resolver
|
||||||
|
private _resolveApiKey: (model: Model<any>) => Promise<string | undefined>;
|
||||||
|
|
||||||
constructor(config: AgentSessionConfig) {
|
constructor(config: AgentSessionConfig) {
|
||||||
this.agent = config.agent;
|
this.agent = config.agent;
|
||||||
this.sessionManager = config.sessionManager;
|
this.sessionManager = config.sessionManager;
|
||||||
@@ -160,6 +165,7 @@ export class AgentSession {
|
|||||||
this._hookRunner = config.hookRunner ?? null;
|
this._hookRunner = config.hookRunner ?? null;
|
||||||
this._customTools = config.customTools ?? [];
|
this._customTools = config.customTools ?? [];
|
||||||
this._skillsSettings = config.skillsSettings;
|
this._skillsSettings = config.skillsSettings;
|
||||||
|
this._resolveApiKey = config.resolveApiKey ?? getApiKeyForModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
// =========================================================================
|
// =========================================================================
|
||||||
@@ -428,7 +434,7 @@ export class AgentSession {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate API key
|
// Validate API key
|
||||||
const apiKey = await getApiKeyForModel(this.model);
|
const apiKey = await this._resolveApiKey(this.model);
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`No API key found for ${this.model.provider}.\n\n` +
|
`No API key found for ${this.model.provider}.\n\n` +
|
||||||
@@ -555,7 +561,7 @@ export class AgentSession {
|
|||||||
* @throws Error if no API key available for the model
|
* @throws Error if no API key available for the model
|
||||||
*/
|
*/
|
||||||
async setModel(model: Model<any>): Promise<void> {
|
async setModel(model: Model<any>): Promise<void> {
|
||||||
const apiKey = await getApiKeyForModel(model);
|
const apiKey = await this._resolveApiKey(model);
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
throw new Error(`No API key for ${model.provider}/${model.id}`);
|
throw new Error(`No API key for ${model.provider}/${model.id}`);
|
||||||
}
|
}
|
||||||
@@ -593,7 +599,7 @@ export class AgentSession {
|
|||||||
const next = this._scopedModels[nextIndex];
|
const next = this._scopedModels[nextIndex];
|
||||||
|
|
||||||
// Validate API key
|
// Validate API key
|
||||||
const apiKey = await getApiKeyForModel(next.model);
|
const apiKey = await this._resolveApiKey(next.model);
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
throw new Error(`No API key for ${next.model.provider}/${next.model.id}`);
|
throw new Error(`No API key for ${next.model.provider}/${next.model.id}`);
|
||||||
}
|
}
|
||||||
@@ -623,7 +629,7 @@ export class AgentSession {
|
|||||||
const nextIndex = (currentIndex + 1) % availableModels.length;
|
const nextIndex = (currentIndex + 1) % availableModels.length;
|
||||||
const nextModel = availableModels[nextIndex];
|
const nextModel = availableModels[nextIndex];
|
||||||
|
|
||||||
const apiKey = await getApiKeyForModel(nextModel);
|
const apiKey = await this._resolveApiKey(nextModel);
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
throw new Error(`No API key for ${nextModel.provider}/${nextModel.id}`);
|
throw new Error(`No API key for ${nextModel.provider}/${nextModel.id}`);
|
||||||
}
|
}
|
||||||
@@ -740,7 +746,7 @@ export class AgentSession {
|
|||||||
throw new Error("No model selected");
|
throw new Error("No model selected");
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiKey = await getApiKeyForModel(this.model);
|
const apiKey = await this._resolveApiKey(this.model);
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
throw new Error(`No API key for ${this.model.provider}`);
|
throw new Error(`No API key for ${this.model.provider}`);
|
||||||
}
|
}
|
||||||
@@ -840,7 +846,7 @@ export class AgentSession {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiKey = await getApiKeyForModel(this.model);
|
const apiKey = await this._resolveApiKey(this.model);
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
this._emit({ type: "auto_compaction_end", result: null, aborted: false, willRetry: false });
|
this._emit({ type: "auto_compaction_end", result: null, aborted: false, willRetry: false });
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -681,6 +681,7 @@ export async function createAgentSession(options: CreateAgentSessionOptions = {}
|
|||||||
hookRunner,
|
hookRunner,
|
||||||
customTools: customToolsResult.tools,
|
customTools: customToolsResult.tools,
|
||||||
skillsSettings: settingsManager.getSkillsSettings(),
|
skillsSettings: settingsManager.getSkillsSettings(),
|
||||||
|
resolveApiKey: getApiKey,
|
||||||
});
|
});
|
||||||
time("createAgentSession");
|
time("createAgentSession");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user