Skip to content

Commit a0f3a6d

Browse files
committed
refactor(anthropic): body request simplified and allowed additional params
1 parent a221d21 commit a0f3a6d

File tree

3 files changed

+98
-134
lines changed

3 files changed

+98
-134
lines changed

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

Lines changed: 56 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -299,47 +299,41 @@ export class BetaRunner extends BaseRunner<BetaRunnerTypes> {
299299
: system;
300300
}
301301

302+
const thinkingConfig = this.toAnthropicThinkingConfig(
303+
request.config?.thinking
304+
) as BetaMessageCreateParams['thinking'] | undefined;
305+
306+
const { thinking: defaultThinkingConfig, ...restConfig } =
307+
request.config || {};
308+
302309
const body: BetaMessageCreateParamsNonStreaming = {
303310
model: mappedModelName,
304311
max_tokens:
305312
request.config?.maxOutputTokens ?? this.DEFAULT_MAX_OUTPUT_TOKENS,
306313
messages,
307-
betas: BETA_APIS,
314+
system: betaSystem,
315+
stop_sequences: request.config?.stopSequences,
316+
temperature: request.config?.temperature,
317+
top_k: request.config?.topK,
318+
top_p: request.config?.topP,
319+
tool_choice: request.config?.tool_choice,
320+
metadata: request.config?.metadata,
321+
tools: request.tools?.map((tool) => this.toAnthropicTool(tool)),
322+
thinking:
323+
(defaultThinkingConfig as BetaMessageCreateParams['thinking']) ??
324+
thinkingConfig,
325+
output_format: this.isStructuredOutputEnabled(request)
326+
? {
327+
type: 'json_schema',
328+
schema: toAnthropicSchema(request.output!.schema!),
329+
}
330+
: undefined,
331+
betas: Array.isArray(request.config?.betas)
332+
? [...BETA_APIS, ...(request.config?.betas ?? [])]
333+
: [...BETA_APIS],
334+
...restConfig,
308335
};
309336

310-
if (betaSystem !== undefined) body.system = betaSystem;
311-
if (request.config?.stopSequences !== undefined)
312-
body.stop_sequences = request.config.stopSequences;
313-
if (request.config?.temperature !== undefined)
314-
body.temperature = request.config.temperature;
315-
if (request.config?.topK !== undefined) body.top_k = request.config.topK;
316-
if (request.config?.topP !== undefined) body.top_p = request.config.topP;
317-
if (request.config?.tool_choice !== undefined) {
318-
body.tool_choice = request.config
319-
.tool_choice as BetaMessageCreateParams['tool_choice'];
320-
}
321-
if (request.config?.metadata !== undefined) {
322-
body.metadata = request.config
323-
.metadata as BetaMessageCreateParams['metadata'];
324-
}
325-
if (request.tools) {
326-
body.tools = request.tools.map((tool) => this.toAnthropicTool(tool));
327-
}
328-
const thinkingConfig = this.toAnthropicThinkingConfig(
329-
request.config?.thinking
330-
);
331-
if (thinkingConfig) {
332-
body.thinking = thinkingConfig as BetaMessageCreateParams['thinking'];
333-
}
334-
335-
// Apply structured output when model supports it and constrained output is requested
336-
if (this.isStructuredOutputEnabled(request)) {
337-
body.output_format = {
338-
type: 'json_schema',
339-
schema: toAnthropicSchema(request.output!.schema!),
340-
};
341-
}
342-
343337
return body;
344338
}
345339

@@ -369,47 +363,42 @@ export class BetaRunner extends BaseRunner<BetaRunnerTypes> {
369363
]
370364
: system;
371365

366+
const thinkingConfig = this.toAnthropicThinkingConfig(
367+
request.config?.thinking
368+
) as BetaMessageCreateParams['thinking'] | undefined;
369+
370+
const { thinking: defaultThinkingConfig, ...restConfig } =
371+
request.config || {};
372+
372373
const body: BetaMessageCreateParamsStreaming = {
373374
model: mappedModelName,
374375
max_tokens:
375376
request.config?.maxOutputTokens ?? this.DEFAULT_MAX_OUTPUT_TOKENS,
376377
messages,
377378
stream: true,
378-
betas: BETA_APIS,
379+
system: betaSystem,
380+
stop_sequences: request.config?.stopSequences,
381+
temperature: request.config?.temperature,
382+
top_k: request.config?.topK,
383+
top_p: request.config?.topP,
384+
tool_choice: request.config?.tool_choice,
385+
metadata: request.config?.metadata,
386+
tools: request.tools?.map((tool) => this.toAnthropicTool(tool)),
387+
thinking:
388+
(defaultThinkingConfig as BetaMessageCreateParams['thinking']) ??
389+
thinkingConfig,
390+
output_format: this.isStructuredOutputEnabled(request)
391+
? {
392+
type: 'json_schema',
393+
schema: toAnthropicSchema(request.output!.schema!),
394+
}
395+
: undefined,
396+
betas: Array.isArray(request.config?.betas)
397+
? [...BETA_APIS, ...(request.config?.betas ?? [])]
398+
: [...BETA_APIS],
399+
...restConfig,
379400
};
380401

381-
if (betaSystem !== undefined) body.system = betaSystem;
382-
if (request.config?.stopSequences !== undefined)
383-
body.stop_sequences = request.config.stopSequences;
384-
if (request.config?.temperature !== undefined)
385-
body.temperature = request.config.temperature;
386-
if (request.config?.topK !== undefined) body.top_k = request.config.topK;
387-
if (request.config?.topP !== undefined) body.top_p = request.config.topP;
388-
if (request.config?.tool_choice !== undefined) {
389-
body.tool_choice = request.config
390-
.tool_choice as BetaMessageCreateParams['tool_choice'];
391-
}
392-
if (request.config?.metadata !== undefined) {
393-
body.metadata = request.config
394-
.metadata as BetaMessageCreateParams['metadata'];
395-
}
396-
if (request.tools) {
397-
body.tools = request.tools.map((tool) => this.toAnthropicTool(tool));
398-
}
399-
const thinkingConfig = this.toAnthropicThinkingConfig(
400-
request.config?.thinking
401-
);
402-
if (thinkingConfig) {
403-
body.thinking = thinkingConfig as BetaMessageCreateParams['thinking'];
404-
}
405-
406-
// Apply structured output when model supports it and constrained output is requested
407-
if (this.isStructuredOutputEnabled(request)) {
408-
body.output_format = {
409-
type: 'json_schema',
410-
schema: toAnthropicSchema(request.output!.schema!),
411-
};
412-
}
413402
return body;
414403
}
415404

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

Lines changed: 41 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
import { MessageStream } from '@anthropic-ai/sdk/lib/MessageStream.js';
1818
import type {
19+
MessageCreateParams as BetaMessageCreateParams,
1920
ContentBlock,
2021
DocumentBlockParam,
2122
ImageBlockParam,
2223
Message,
23-
MessageCreateParams,
2424
MessageCreateParamsNonStreaming,
2525
MessageCreateParamsStreaming,
2626
MessageParam,
@@ -197,50 +197,38 @@ export class Runner extends BaseRunner<RunnerTypes> {
197197
]
198198
: system;
199199

200+
const thinkingConfig = this.toAnthropicThinkingConfig(
201+
request.config?.thinking
202+
) as BetaMessageCreateParams['thinking'] | undefined;
203+
204+
const { thinking: defaultThinkingConfig, ...restConfig } =
205+
request.config || {};
206+
200207
const body: MessageCreateParamsNonStreaming = {
201208
model: mappedModelName,
202209
max_tokens:
203210
request.config?.maxOutputTokens ?? this.DEFAULT_MAX_OUTPUT_TOKENS,
204211
messages,
212+
system: systemValue,
213+
stop_sequences: request.config?.stopSequences,
214+
temperature: request.config?.temperature,
215+
top_k: request.config?.topK,
216+
top_p: request.config?.topP,
217+
tool_choice: request.config?.tool_choice,
218+
metadata: request.config?.metadata,
219+
tools: request.tools?.map((tool) => this.toAnthropicTool(tool)),
220+
thinking:
221+
(defaultThinkingConfig as BetaMessageCreateParams['thinking']) ??
222+
thinkingConfig,
223+
...restConfig,
205224
};
206225

207-
if (systemValue !== undefined) {
208-
body.system = systemValue;
209-
}
210-
211-
if (request.tools) {
212-
body.tools = request.tools.map((tool) => this.toAnthropicTool(tool));
213-
}
214-
if (request.config?.topK !== undefined) {
215-
body.top_k = request.config.topK;
216-
}
217-
if (request.config?.topP !== undefined) {
218-
body.top_p = request.config.topP;
219-
}
220-
if (request.config?.temperature !== undefined) {
221-
body.temperature = request.config.temperature;
222-
}
223-
if (request.config?.stopSequences !== undefined) {
224-
body.stop_sequences = request.config.stopSequences;
225-
}
226-
if (request.config?.metadata !== undefined) {
227-
body.metadata = request.config.metadata;
228-
}
229-
if (request.config?.tool_choice !== undefined) {
230-
body.tool_choice = request.config.tool_choice;
231-
}
232-
const thinkingConfig = this.toAnthropicThinkingConfig(
233-
request.config?.thinking
234-
);
235-
if (thinkingConfig) {
236-
body.thinking = thinkingConfig as MessageCreateParams['thinking'];
237-
}
238-
239226
if (request.output?.format && request.output.format !== 'text') {
240227
throw new Error(
241228
`Only text output format is supported for Claude models currently`
242229
);
243230
}
231+
244232
return body;
245233
}
246234

@@ -267,52 +255,39 @@ export class Runner extends BaseRunner<RunnerTypes> {
267255
]
268256
: system;
269257

258+
const thinkingConfig = this.toAnthropicThinkingConfig(
259+
request.config?.thinking
260+
) as BetaMessageCreateParams['thinking'] | undefined;
261+
262+
const { thinking: defaultThinkingConfig, ...restConfig } =
263+
request.config || {};
264+
270265
const body: MessageCreateParamsStreaming = {
271266
model: mappedModelName,
272267
max_tokens:
273268
request.config?.maxOutputTokens ?? this.DEFAULT_MAX_OUTPUT_TOKENS,
274269
messages,
275270
stream: true,
271+
system: systemValue,
272+
stop_sequences: request.config?.stopSequences,
273+
temperature: request.config?.temperature,
274+
top_k: request.config?.topK,
275+
top_p: request.config?.topP,
276+
tool_choice: request.config?.tool_choice,
277+
metadata: request.config?.metadata,
278+
tools: request.tools?.map((tool) => this.toAnthropicTool(tool)),
279+
thinking:
280+
(defaultThinkingConfig as BetaMessageCreateParams['thinking']) ??
281+
thinkingConfig,
282+
...restConfig,
276283
};
277284

278-
if (systemValue !== undefined) {
279-
body.system = systemValue;
280-
}
281-
282-
if (request.tools) {
283-
body.tools = request.tools.map((tool) => this.toAnthropicTool(tool));
284-
}
285-
if (request.config?.topK !== undefined) {
286-
body.top_k = request.config.topK;
287-
}
288-
if (request.config?.topP !== undefined) {
289-
body.top_p = request.config.topP;
290-
}
291-
if (request.config?.temperature !== undefined) {
292-
body.temperature = request.config.temperature;
293-
}
294-
if (request.config?.stopSequences !== undefined) {
295-
body.stop_sequences = request.config.stopSequences;
296-
}
297-
if (request.config?.metadata !== undefined) {
298-
body.metadata = request.config.metadata;
299-
}
300-
if (request.config?.tool_choice !== undefined) {
301-
body.tool_choice = request.config.tool_choice;
302-
}
303-
const thinkingConfig = this.toAnthropicThinkingConfig(
304-
request.config?.thinking
305-
);
306-
if (thinkingConfig) {
307-
body.thinking =
308-
thinkingConfig as MessageCreateParamsStreaming['thinking'];
309-
}
310-
311285
if (request.output?.format && request.output.format !== 'text') {
312286
throw new Error(
313287
`Only text output format is supported for Claude models currently`
314288
);
315289
}
290+
316291
return body;
317292
}
318293

js/plugins/anthropic/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const AnthropicBaseConfigSchema = GenerationCommonConfigSchema.extend({
8686
.optional(),
8787
/** Optional shorthand to pick API surface for this request. */
8888
apiVersion: z.enum(['stable', 'beta']).optional(),
89-
});
89+
}).passthrough();
9090

9191
export type AnthropicBaseConfigSchemaType = typeof AnthropicBaseConfigSchema;
9292

0 commit comments

Comments
 (0)