@namzu/anthropic 6.1.0 → 6.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,110 @@
1
+ import { ProviderRequestError, parseVersionedModelId } from '@namzu/sdk'
2
+ import type { ThinkingConfig, ToolChoice } from '@namzu/sdk'
3
+
4
+ import {
5
+ MODEL_ID_GRAMMAR,
6
+ type ResolvedThinkingBody,
7
+ resolveThinkingBody,
8
+ resolveThinkingCapability,
9
+ } from './thinking-capability.js'
10
+ import { type VersionFloor, reachesFloor } from './version-floor.js'
11
+
12
+ /**
13
+ * Models that refuse a forced tool choice whatever the thinking settings.
14
+ *
15
+ * `tool_choice: {type: "any"}` and `{type: "tool", name}` come back 400 —
16
+ * `tool_choice: type "tool" and "any" are not supported for this model.` — on
17
+ * Claude Opus 5.5, Claude Fable 5.1 and Claude Mythos 5.1, and on the
18
+ * token-counting endpoint too. That list is the vendor's, stated identically
19
+ * in its error reference and in the "Forcing tool use" table of its tool
20
+ * definition page. Opus 5 and Fable 5 still take a forced choice, so these are
21
+ * version floors and not family rules — and not the always-on line in
22
+ * `thinking-capability.ts` either: Fable 5 cannot stop thinking and accepts a
23
+ * forced choice all the same.
24
+ *
25
+ * SOURCED, NOT MEASURED. The wire has not been asked from here; the live
26
+ * contract suite carries the probe for when a key is present. Mythos Preview
27
+ * is deliberately absent: neither vendor page names it, and this table holds
28
+ * what the vendor says, not what seems likely.
29
+ */
30
+ const REFUSES_FORCED_TOOL_CHOICE_FROM: readonly VersionFloor[] = [
31
+ { family: 'opus', major: 5, minor: 5 },
32
+ { family: 'fable', major: 5, minor: 1 },
33
+ { family: 'mythos', major: 5, minor: 1 },
34
+ ]
35
+
36
+ /** `'required'` or a named function: the two choices that make a call mandatory. */
37
+ function isForced(toolChoice: ToolChoice | undefined): boolean {
38
+ return (
39
+ toolChoice === 'required' || (typeof toolChoice === 'object' && toolChoice.type === 'function')
40
+ )
41
+ }
42
+
43
+ /**
44
+ * Why a forced choice would be refused for this model and thinking body, or
45
+ * `undefined` when it would be taken.
46
+ *
47
+ * Two causes, both from the vendor's table. The model may refuse it outright.
48
+ * Or the request may carry manual extended thinking — `thinking: {type:
49
+ * "enabled"}` — which rejects a forced choice on every model that has it.
50
+ * Adaptive thinking does not: Opus 5 takes a forced choice with thinking on.
51
+ *
52
+ * The second is decided on the RESOLVED body, not on the caller's intent,
53
+ * because that is what the vendor sees: an `adaptive` intent becomes `enabled`
54
+ * on a manual-only model, and an `enabled` intent becomes `adaptive` on an
55
+ * adaptive-only one.
56
+ */
57
+ function forcedToolChoiceRefusal(
58
+ model: string,
59
+ thinkingBody: ResolvedThinkingBody,
60
+ ): 'model' | 'manual-thinking' | undefined {
61
+ const version = parseVersionedModelId(model, MODEL_ID_GRAMMAR)
62
+ if (version && reachesFloor(version, REFUSES_FORCED_TOOL_CHOICE_FROM)) return 'model'
63
+ if (thinkingBody?.type === 'enabled') return 'manual-thinking'
64
+ return undefined
65
+ }
66
+
67
+ /**
68
+ * Whether this model takes a forced tool choice — `toolChoice: 'required'` or
69
+ * a named function — under the thinking configuration you intend to send.
70
+ *
71
+ * Built from the same resolution the request path uses, so a caller deciding
72
+ * whether to force a step and the request it then sends cannot disagree.
73
+ * `false` means the driver will refuse that request before sending it; use
74
+ * `toolChoice: 'auto'` and say in the prompt which tool to call.
75
+ */
76
+ export function acceptsForcedToolChoice(model: string, thinking?: ThinkingConfig): boolean {
77
+ const body = resolveThinkingBody(thinking, resolveThinkingCapability(model))
78
+ return forcedToolChoiceRefusal(model, body) === undefined
79
+ }
80
+
81
+ /**
82
+ * Refuse, before transport, a forced tool choice the vendor is certain to reject.
83
+ *
84
+ * The same trade `resolveEffort` makes for an effort level. Sending it costs a
85
+ * round trip to learn what is already known here. Quietly sending `auto`
86
+ * instead would be worse: a forced choice is a contract that the model calls a
87
+ * tool, and a caller relying on it — a step that must produce a structured
88
+ * answer — would get prose back and be told the request succeeded.
89
+ *
90
+ * The error is the `bad_request` `ProviderRequestError` the vendor's 400 would
91
+ * have become, with its own code, so a caller's handling does not change.
92
+ */
93
+ export function assertForcedToolChoiceAccepted(
94
+ toolChoice: ToolChoice | undefined,
95
+ thinkingBody: ResolvedThinkingBody,
96
+ model: string,
97
+ ): void {
98
+ if (!isForced(toolChoice)) return
99
+ const refusal = forcedToolChoiceRefusal(model, thinkingBody)
100
+ if (refusal === undefined) return
101
+ throw new ProviderRequestError({
102
+ kind: 'bad_request',
103
+ providerId: 'anthropic',
104
+ providerCode: 'forced_tool_choice_unsupported',
105
+ detail:
106
+ refusal === 'model'
107
+ ? `Model "${model}" does not accept a forced tool choice (toolChoice "required" or a named function). Use toolChoice "auto" and say in the prompt which tool to call, or responseFormat for a fixed JSON shape.`
108
+ : `A forced tool choice (toolChoice "required" or a named function) cannot be combined with manual extended thinking, which this request's thinking setting resolves to on model "${model}". Use toolChoice "auto", or leave thinking off for this step.`,
109
+ })
110
+ }
@@ -0,0 +1,29 @@
1
+ import type { ModelVersion } from '@namzu/sdk'
2
+
3
+ /**
4
+ * A rule that holds for one model family from one version onward.
5
+ *
6
+ * Inclusive and open-ended on purpose. The capability questions this package
7
+ * asks of a model id — can thinking be switched off, will a forced tool choice
8
+ * be taken — have each changed in one direction within a family, and at a
9
+ * MINOR version: `claude-opus-5` can switch thinking off and `claude-opus-5-5`
10
+ * cannot. A list of exact ids needs a row per release and fails open on the
11
+ * next one; a floor needs a row per family per change, and the next release
12
+ * in a family that has crossed inherits the answer instead of falling through
13
+ * to a default that sends a field the wire refuses.
14
+ */
15
+ export interface VersionFloor {
16
+ readonly family: string
17
+ readonly major: number
18
+ readonly minor: number
19
+ }
20
+
21
+ /** Whether `version` is at or above the floor listed for its own family. */
22
+ export function reachesFloor(version: ModelVersion, floors: readonly VersionFloor[]): boolean {
23
+ return floors.some(
24
+ (floor) =>
25
+ floor.family === version.family &&
26
+ (version.major > floor.major ||
27
+ (version.major === floor.major && version.minor >= floor.minor)),
28
+ )
29
+ }