Skip to content

Commit ce72503

Browse files
amondnetgemini-code-assist[bot]
andcommitted
refactor(js/plugins/anthropic): extract _prepareConfigAndTools helper
Extract duplicated config and tools preparation logic into a private helper method to improve maintainability (DRY principle). Co-authored-by: gemini-code-assist[bot] <176aborting-your-request@users.noreply.github.com>
1 parent e365811 commit ce72503

File tree

1 file changed

+41
-42
lines changed
  • js/plugins/anthropic/src/runner

1 file changed

+41
-42
lines changed

js/plugins/anthropic/src/runner/beta.ts

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ interface BetaRunnerTypes extends RunnerTypes {
138138
| BetaRedactedThinkingBlockParam;
139139
}
140140

141+
/**
142+
* Return type for _prepareConfigAndTools helper.
143+
*/
144+
interface PreparedConfigAndTools {
145+
topP: number | undefined;
146+
topK: number | undefined;
147+
mcp_servers: unknown[] | undefined;
148+
tools: unknown[] | undefined;
149+
restConfig: Record<string, unknown>;
150+
}
151+
141152
/**
142153
* Runner for the Anthropic Beta API.
143154
*/
@@ -146,6 +157,32 @@ export class BetaRunner extends BaseRunner<BetaRunnerTypes> {
146157
super(params);
147158
}
148159

160+
/**
161+
* Extract and prepare config fields and tools array from request.
162+
* Handles MCP toolset merging with regular tools.
163+
*/
164+
private _prepareConfigAndTools(
165+
request: GenerateRequest<typeof AnthropicConfigSchema>
166+
): PreparedConfigAndTools {
167+
const {
168+
topP,
169+
topK,
170+
apiVersion: _1,
171+
thinking: _2,
172+
mcp_servers,
173+
mcp_toolsets,
174+
...restConfig
175+
} = request.config ?? {};
176+
177+
const genkitTools = request.tools?.map((tool) => this.toAnthropicTool(tool));
178+
const tools =
179+
genkitTools || mcp_toolsets
180+
? [...(genkitTools ?? []), ...(mcp_toolsets ?? [])]
181+
: undefined;
182+
183+
return { topP, topK, mcp_servers, tools, restConfig };
184+
}
185+
149186
/**
150187
* Map a Genkit Part -> Anthropic beta content block param.
151188
* Supports: text, images (base64 data URLs), PDFs (document source),
@@ -323,27 +360,8 @@ export class BetaRunner extends BaseRunner<BetaRunnerTypes> {
323360
request.config?.thinking
324361
) as BetaMessageCreateParams['thinking'] | undefined;
325362

326-
// Need to extract topP and topK from request.config to avoid duplicate properties being added to the body
327-
// This happens because topP and topK have different property names (top_p and top_k) in the Anthropic API.
328-
// Thinking is extracted separately to avoid type issues.
329-
// ApiVersion is extracted separately as it's not a valid property for the Anthropic API.
330-
// MCP config (mcp_servers, mcp_toolsets) is extracted separately to handle toolset merging.
331-
const {
332-
topP,
333-
topK,
334-
apiVersion: _1,
335-
thinking: _2,
336-
mcp_servers,
337-
mcp_toolsets,
338-
...restConfig
339-
} = request.config ?? {};
340-
341-
// Build tools array, merging regular tools with MCP toolsets
342-
const genkitTools = request.tools?.map((tool) => this.toAnthropicTool(tool));
343-
const tools =
344-
genkitTools || mcp_toolsets
345-
? [...(genkitTools ?? []), ...(mcp_toolsets ?? [])]
346-
: undefined;
363+
const { topP, topK, mcp_servers, tools, restConfig } =
364+
this._prepareConfigAndTools(request);
347365

348366
const body = {
349367
model: mappedModelName,
@@ -405,27 +423,8 @@ export class BetaRunner extends BaseRunner<BetaRunnerTypes> {
405423
request.config?.thinking
406424
) as BetaMessageCreateParams['thinking'] | undefined;
407425

408-
// Need to extract topP and topK from request.config to avoid duplicate properties being added to the body
409-
// This happens because topP and topK have different property names (top_p and top_k) in the Anthropic API.
410-
// Thinking is extracted separately to avoid type issues.
411-
// ApiVersion is extracted separately as it's not a valid property for the Anthropic API.
412-
// MCP config (mcp_servers, mcp_toolsets) is extracted separately to handle toolset merging.
413-
const {
414-
topP,
415-
topK,
416-
apiVersion: _1,
417-
thinking: _2,
418-
mcp_servers,
419-
mcp_toolsets,
420-
...restConfig
421-
} = request.config ?? {};
422-
423-
// Build tools array, merging regular tools with MCP toolsets
424-
const genkitTools = request.tools?.map((tool) => this.toAnthropicTool(tool));
425-
const tools =
426-
genkitTools || mcp_toolsets
427-
? [...(genkitTools ?? []), ...(mcp_toolsets ?? [])]
428-
: undefined;
426+
const { topP, topK, mcp_servers, tools, restConfig } =
427+
this._prepareConfigAndTools(request);
429428

430429
const body = {
431430
model: mappedModelName,

0 commit comments

Comments
 (0)