1515 * limitations under the License.
1616 */
1717
18+ import Anthropic from '@anthropic-ai/sdk' ;
1819import type {
1920 GenerateRequest ,
2021 GenerateResponseData ,
@@ -33,6 +34,7 @@ import {
3334 AnthropicBaseConfigSchemaType ,
3435 AnthropicConfigSchema ,
3536 AnthropicThinkingConfigSchema ,
37+ calculateApiKey ,
3638 resolveBetaEnabled ,
3739 type ClaudeModelParams ,
3840 type ClaudeRunnerParams ,
@@ -198,14 +200,7 @@ export function claudeRunner<TConfigSchema extends z.ZodTypeAny>(
198200 params : ClaudeRunnerParams ,
199201 configSchema : TConfigSchema
200202) {
201- const { defaultApiVersion, ...runnerParams } = params ;
202-
203- if ( ! runnerParams . client ) {
204- throw new Error ( 'Anthropic client is required to create a runner' ) ;
205- }
206-
207- let stableRunner : Runner | null = null ;
208- let betaRunner : BetaRunner | null = null ;
203+ const { defaultApiVersion, pluginApiKey, testClient, name } = params ;
209204
210205 return async (
211206 request : GenerateRequest < TConfigSchema > ,
@@ -223,13 +218,28 @@ export function claudeRunner<TConfigSchema extends z.ZodTypeAny>(
223218 const normalizedRequest = request as unknown as GenerateRequest <
224219 typeof AnthropicConfigSchema
225220 > ;
221+
222+ // Determine the client to use
223+ // Test client takes precedence, otherwise calculate API key at request time
224+ const client = testClient
225+ ? testClient
226+ : new Anthropic ( {
227+ apiKey : calculateApiKey (
228+ pluginApiKey ,
229+ normalizedRequest . config ?. apiKey
230+ ) ,
231+ } ) ;
232+
226233 const isBeta = resolveBetaEnabled (
227234 normalizedRequest . config ,
228235 defaultApiVersion
229236 ) ;
237+
238+ // Create runner with the client
230239 const runner = isBeta
231- ? ( betaRunner ??= new BetaRunner ( runnerParams ) )
232- : ( stableRunner ??= new Runner ( runnerParams ) ) ;
240+ ? new BetaRunner ( { name, client } )
241+ : new Runner ( { name, client } ) ;
242+
233243 return runner . run ( normalizedRequest , {
234244 streamingRequested,
235245 sendChunk,
@@ -265,17 +275,22 @@ export function claudeModelReference(
265275}
266276
267277/**
268- * Defines a Claude model with the given name and Anthropic client .
278+ * Defines a Claude model with the given name and API key configuration .
269279 * Accepts any model name and lets the API validate it. If the model is in KNOWN_CLAUDE_MODELS, uses that modelRef
270280 * for better defaults; otherwise creates a generic model reference.
271281 */
272282export function claudeModel (
273283 params : ClaudeModelParams
274284) : ModelAction < z . ZodTypeAny > {
275- const { name, client : runnerClient , defaultApiVersion : apiVersion } = params ;
285+ const {
286+ name,
287+ pluginApiKey,
288+ defaultApiVersion : apiVersion ,
289+ testClient,
290+ } = params ;
276291 // Use supported model ref if available, otherwise create generic model ref
277292 const knownModelRef = KNOWN_CLAUDE_MODELS [ name ] ;
278- let modelInfo = knownModelRef
293+ const modelInfo = knownModelRef
279294 ? knownModelRef . info
280295 : GENERIC_CLAUDE_MODEL_INFO ;
281296 const configSchema = knownModelRef ?. configSchema ?? AnthropicConfigSchema ;
@@ -291,8 +306,9 @@ export function claudeModel(
291306 claudeRunner (
292307 {
293308 name,
294- client : runnerClient ,
309+ pluginApiKey ,
295310 defaultApiVersion : apiVersion ,
311+ testClient,
296312 } ,
297313 configSchema
298314 )
0 commit comments