@namzu/anthropic 3.0.0 → 3.1.0

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.
Files changed (32) hide show
  1. package/CHANGELOG.md +99 -0
  2. package/dist/__tests__/background-delegation.live.test.d.ts +2 -0
  3. package/dist/__tests__/background-delegation.live.test.d.ts.map +1 -0
  4. package/dist/__tests__/background-delegation.live.test.js +212 -0
  5. package/dist/__tests__/background-delegation.live.test.js.map +1 -0
  6. package/dist/__tests__/effort-levels-are-askable.test.d.ts +2 -0
  7. package/dist/__tests__/effort-levels-are-askable.test.d.ts.map +1 -0
  8. package/dist/__tests__/effort-levels-are-askable.test.js +74 -0
  9. package/dist/__tests__/effort-levels-are-askable.test.js.map +1 -0
  10. package/dist/__tests__/thinking-capability.test.js +6 -1
  11. package/dist/__tests__/thinking-capability.test.js.map +1 -1
  12. package/dist/__tests__/wire-contract.live.test.js +102 -16
  13. package/dist/__tests__/wire-contract.live.test.js.map +1 -1
  14. package/dist/client.d.ts +15 -1
  15. package/dist/client.d.ts.map +1 -1
  16. package/dist/client.js +25 -1
  17. package/dist/client.js.map +1 -1
  18. package/dist/index.d.ts +2 -0
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +8 -0
  21. package/dist/index.js.map +1 -1
  22. package/dist/thinking-capability.d.ts.map +1 -1
  23. package/dist/thinking-capability.js +23 -2
  24. package/dist/thinking-capability.js.map +1 -1
  25. package/package.json +2 -2
  26. package/src/__tests__/background-delegation.live.test.ts +243 -0
  27. package/src/__tests__/effort-levels-are-askable.test.ts +86 -0
  28. package/src/__tests__/thinking-capability.test.ts +6 -1
  29. package/src/__tests__/wire-contract.live.test.ts +111 -17
  30. package/src/client.ts +28 -1
  31. package/src/index.ts +18 -0
  32. package/src/thinking-capability.ts +23 -2
@@ -0,0 +1,86 @@
1
+ import { describe, expect, it } from 'vitest'
2
+
3
+ import { AnthropicProvider } from '../client.js'
4
+
5
+ /**
6
+ * A caller building an effort control has to offer the levels a model actually
7
+ * accepts, and only those, because effort is REFUSED rather than clamped: a
8
+ * level a model does not have makes the vendor reject the request, so a picker
9
+ * offering the wrong one produces a run that fails at the start.
10
+ *
11
+ * The driver already knew the answer and kept it to itself. Every option open
12
+ * to a caller without it was bad: offer all five and break some models, offer
13
+ * the intersection and hide the levels that are most of the reason to build the
14
+ * control, or copy the table — which looks fine and is worst, because the
15
+ * ceiling has moved twice and a copy goes stale SILENTLY, surfacing as a vendor
16
+ * rejection rather than a failing build.
17
+ */
18
+
19
+ const provider = (): AnthropicProvider =>
20
+ new AnthropicProvider({ apiKey: 'sk-ant-test', model: 'claude-sonnet-5' })
21
+
22
+ describe('a caller can ask which levels a model takes', () => {
23
+ it('answers per model rather than per provider', () => {
24
+ // Three eras, three different answers. If this ever collapses to one
25
+ // set, the method has been reduced to a constant and the question it
26
+ // exists to answer has stopped being asked.
27
+ const p = provider()
28
+ const modern = p.effortLevelsFor('claude-opus-5')
29
+ const midRange = p.effortLevelsFor('claude-sonnet-4-6')
30
+ const legacy = p.effortLevelsFor('claude-opus-4-5')
31
+
32
+ expect(modern).toContain('xhigh')
33
+ expect(midRange).not.toContain('xhigh')
34
+ expect(midRange).toContain('max')
35
+ expect(legacy).not.toContain('max')
36
+
37
+ const distinct = new Set([modern, midRange, legacy].map((s) => [...s].sort().join(',')))
38
+ expect(distinct.size, 'the three eras must not answer alike').toBe(3)
39
+ })
40
+
41
+ it('reports an empty set rather than nothing for a model with no levels', () => {
42
+ // A real answer, and different from the method being absent. Absent
43
+ // means the driver has no effort concept at all; empty means it does
44
+ // and this model has none.
45
+ expect(provider().effortLevelsFor('claude-haiku-4-5')).toEqual([])
46
+ })
47
+ })
48
+
49
+ describe('the answer depends on the thinking configuration you will send', () => {
50
+ it('narrows for the family that caps effort while thinking is off', () => {
51
+ // The trap this signature exists to remove. A caller reading a single
52
+ // `effort` array, rendering a picker, and then also sending
53
+ // `thinking: disabled` produces a combination the vendor rejects — on
54
+ // exactly one family, which is why it survives casual testing.
55
+ const p = provider()
56
+ const withThinking = p.effortLevelsFor('claude-opus-5', { type: 'adaptive' })
57
+ const withoutThinking = p.effortLevelsFor('claude-opus-5', { type: 'disabled' })
58
+
59
+ expect(withThinking).toContain('max')
60
+ expect(withoutThinking).not.toContain('max')
61
+ expect(withoutThinking).not.toContain('xhigh')
62
+ expect(withoutThinking).toContain('high')
63
+ })
64
+
65
+ it('does not narrow for a sibling that accepts the full set either way', () => {
66
+ // Measured, not inferred: the cap belongs to one family, and a version
67
+ // comparison of "5 and later" would have caught this model too and
68
+ // hidden levels the wire honours.
69
+ const p = provider()
70
+ expect(p.effortLevelsFor('claude-sonnet-5', { type: 'disabled' })).toContain('max')
71
+ })
72
+ })
73
+
74
+ describe('the resolver is reachable for the fuller picture', () => {
75
+ it('is exported from the package entry, not only its own module', async () => {
76
+ // The whole report was that the answer existed and could not be
77
+ // reached. A caller that also needs to know whether thinking can be
78
+ // switched off at all reads the capability directly.
79
+ const entry = await import('../index.js')
80
+
81
+ expect(typeof entry.resolveThinkingCapability).toBe('function')
82
+ const capability = entry.resolveThinkingCapability('claude-opus-5')
83
+ expect(capability.effort.length).toBeGreaterThan(0)
84
+ expect(typeof capability.canDisable).toBe('boolean')
85
+ })
86
+ })
@@ -38,7 +38,12 @@ const TABLE: readonly [string, boolean, boolean, boolean, string[], string[]][]
38
38
  // Always-on families: thinking cannot be switched off at any version.
39
39
  ['claude-fable-5', true, false, false, ALL, ALL],
40
40
  ['claude-mythos-5', true, false, false, ALL, ALL],
41
- ['claude-mythos-preview', true, true, false, ALL, ALL],
41
+ // The preview takes `max` and NOT `xhigh`, which is the pairing it is easy
42
+ // to assume away — the reference says outright that some models supporting
43
+ // `max` do not support `xhigh`, and this is one. Reading the levels as a
44
+ // ladder is what put ALL on this row originally, and a ladder reading sends
45
+ // a level the wire rejects.
46
+ ['claude-mythos-preview', true, true, false, NO_XHIGH, NO_XHIGH],
42
47
  // 4.7 and later: adaptive only, and it can still be disabled.
43
48
  ['claude-opus-5', true, false, true, ALL, CAPPED],
44
49
  ['claude-sonnet-5', true, false, true, ALL, ALL],
@@ -42,25 +42,54 @@ interface WireResult {
42
42
  readonly message: string
43
43
  }
44
44
 
45
+ /**
46
+ * Statuses that mean "ask again", not "this request is wrong".
47
+ *
48
+ * These tests assert a CONTRACT: that a given schema is or is not expressible
49
+ * on this wire. A 529 says the service is busy and answers nothing about the
50
+ * schema — so reporting it as a contract failure claims something the run did
51
+ * not establish. That happened twice in one day, and both times cost a manual
52
+ * re-run to discover the wire had no opinion.
53
+ *
54
+ * Retried rather than reported inconclusive because a test that sometimes
55
+ * declines to check is a test nobody trusts, and the retry is cheap: these
56
+ * requests set `max_tokens: 1` and are rejected or accepted at validation.
57
+ */
58
+ const TRANSIENT = new Set([429, 500, 502, 503, 529])
59
+
60
+ async function post(body: unknown): Promise<{ ok: boolean; status: number; message: string }> {
61
+ let last = { ok: false, status: 0, message: 'no attempt made' }
62
+ for (let attempt = 1; attempt <= 4; attempt += 1) {
63
+ const res = await fetch('https://api.anthropic.com/v1/messages', {
64
+ method: 'POST',
65
+ headers: {
66
+ 'x-api-key': KEY as string,
67
+ 'anthropic-version': '2023-06-01',
68
+ 'content-type': 'application/json',
69
+ },
70
+ body: JSON.stringify(body),
71
+ })
72
+ const parsed: unknown = await res.json().catch(() => ({}))
73
+ const message =
74
+ (parsed as { error?: { message?: string } } | null)?.error?.message ??
75
+ (res.ok ? '' : 'unknown')
76
+ last = { ok: res.ok, status: res.status, message }
77
+ if (!TRANSIENT.has(res.status)) return last
78
+ await new Promise((resolve) => setTimeout(resolve, 1_500 * attempt))
79
+ }
80
+ // Exhausted. Returned rather than thrown so the assertion names the status
81
+ // — a bare failure here reads as a rejected schema, which is the confusion
82
+ // this whole helper exists to prevent.
83
+ return last
84
+ }
85
+
45
86
  async function offerTools(tools: unknown[]): Promise<WireResult> {
46
- const res = await fetch('https://api.anthropic.com/v1/messages', {
47
- method: 'POST',
48
- headers: {
49
- 'x-api-key': KEY as string,
50
- 'anthropic-version': '2023-06-01',
51
- 'content-type': 'application/json',
52
- },
53
- body: JSON.stringify({
54
- model: MODEL,
55
- max_tokens: 1,
56
- messages: [{ role: 'user', content: 'hi' }],
57
- tools,
58
- }),
87
+ return post({
88
+ model: MODEL,
89
+ max_tokens: 1,
90
+ messages: [{ role: 'user', content: 'hi' }],
91
+ tools,
59
92
  })
60
- const body: unknown = await res.json().catch(() => ({}))
61
- const message =
62
- (body as { error?: { message?: string } } | null)?.error?.message ?? (res.ok ? '' : 'unknown')
63
- return { ok: res.ok, status: res.status, message }
64
93
  }
65
94
 
66
95
  /** A tool as this driver would put it on the wire. */
@@ -213,6 +242,71 @@ describe.skipIf(!KEY)('the strict subset is what the deny-list says it is', () =
213
242
  }, 180_000)
214
243
  })
215
244
 
245
+ describe.skipIf(!KEY)('the positional shape a bridged tool now emits', () => {
246
+ it('is accepted once converted', async () => {
247
+ // The MCP adapter emits a tuple ONLY where the server pinned the arity
248
+ // and closed the tail, because that renders as bounded `prefixItems`.
249
+ // The narrowness is the design rather than caution: a tool schema this
250
+ // wire refuses fails the WHOLE request instead of degrading one tool,
251
+ // so a faithful conversion it will not take is strictly worse than a
252
+ // lossy one it will.
253
+ //
254
+ // The refusal half of this pair is already asserted above, on the
255
+ // unconverted spelling — so this says something the sweep does not:
256
+ // that the shape we CHOSE to emit is one the wire takes.
257
+ const pinned = {
258
+ type: 'object',
259
+ properties: {
260
+ pair: {
261
+ type: 'array',
262
+ minItems: 2,
263
+ maxItems: 2,
264
+ items: [{ type: 'string' }, { type: 'number' }],
265
+ },
266
+ },
267
+ required: ['pair'],
268
+ additionalProperties: false,
269
+ }
270
+
271
+ const result = await offerTools([wireTool('pinned_pair', pinned, false)])
272
+ expect(result.ok, result.message).toBe(true)
273
+ }, 180_000)
274
+ })
275
+
276
+ describe.skipIf(!KEY)('effort is a per-model SET, and this wire says so itself', () => {
277
+ // Through the same retrying helper: a 529 here would read as "this model
278
+ // refuses that level", which is the exact claim these tests exist to make
279
+ // and the exact claim an overloaded service cannot support.
280
+ const withEffort = (model: string, effort: string): Promise<WireResult> =>
281
+ post({
282
+ model,
283
+ max_tokens: 16,
284
+ messages: [{ role: 'user', content: 'Say OK.' }],
285
+ output_config: { effort },
286
+ })
287
+
288
+ it('takes all five levels on a model the table says has all five', async () => {
289
+ for (const effort of ['low', 'medium', 'high', 'xhigh', 'max']) {
290
+ const result = await withEffort('claude-sonnet-5', effort)
291
+ expect(result.ok, `${effort}: ${result.message}`).toBe(true)
292
+ }
293
+ }, 180_000)
294
+
295
+ it('refuses a level a model lacks, and names the set it has', async () => {
296
+ // The pairing the capability table exists to express: `max` WITHOUT
297
+ // `xhigh`. A boolean could not have said this, and reading the levels
298
+ // as a ladder — where anything taking the top rung takes the one below
299
+ // — gets it backwards. That reading is how a row in that table came to
300
+ // claim a level this wire rejects.
301
+ const refused = await withEffort('claude-sonnet-4-6', 'xhigh')
302
+ expect(refused.ok).toBe(false)
303
+ expect(refused.message).toContain('xhigh')
304
+
305
+ const accepted = await withEffort('claude-sonnet-4-6', 'max')
306
+ expect(accepted.ok, accepted.message).toBe(true)
307
+ }, 180_000)
308
+ })
309
+
216
310
  describe('what can be checked without a key', () => {
217
311
  it('leaves no draft-07-only construct in any shipped tool', () => {
218
312
  // The offline half of the same guarantee, so a contributor without a
package/src/client.ts CHANGED
@@ -8,7 +8,9 @@ import type {
8
8
  ModelInfo,
9
9
  ProviderCapabilities,
10
10
  ReasoningBlock,
11
+ ReasoningEffort,
11
12
  StreamChunk,
13
+ ThinkingConfig,
12
14
  TokenUsage,
13
15
  ToolChoice,
14
16
  ToolResultContent,
@@ -740,7 +742,13 @@ export class AnthropicProvider implements LLMProvider {
740
742
  // A sibling of `thinking`, not a field inside it — and gated on the
741
743
  // model, since only some accept it at all.
742
744
  const effort = resolveEffort(params.effort, thinkingBody, capability)
743
- if (effort) body.output_config = { effort }
745
+ // Merged rather than assigned. `output_config` is a shared envelope on
746
+ // this wire — a structured-output format and a task budget live in it
747
+ // too, and `responseFormat` already exists on the params unhandled
748
+ // here. Assigning would mean whoever wires the next one silently
749
+ // deletes effort, or has effort silently delete theirs, depending only
750
+ // on which line ran last.
751
+ if (effort) body.output_config = { ...(body.output_config as object | undefined), effort }
744
752
  if (params.temperature !== undefined) body.temperature = params.temperature
745
753
  if (params.topP !== undefined) body.top_p = params.topP
746
754
  if (params.topK !== undefined) body.top_k = params.topK
@@ -1082,4 +1090,23 @@ export class AnthropicProvider implements LLMProvider {
1082
1090
  // want network-level verification should call `chat()` directly.
1083
1091
  return Boolean(this.client) && Boolean(this.config.apiKey)
1084
1092
  }
1093
+
1094
+ /**
1095
+ * The levels this model will accept, under the thinking configuration the
1096
+ * caller intends to send.
1097
+ *
1098
+ * Deliberately built from the SAME two functions the request path uses —
1099
+ * `resolveThinkingCapability` then `resolveThinkingBody` — rather than
1100
+ * reading `capability.effort` directly. Reading the field would answer the
1101
+ * question for adaptive thinking and silently give the wrong answer while
1102
+ * thinking is disabled, which is precisely the trap this method exists to
1103
+ * remove. Sharing the resolution means a caller's picker and the request
1104
+ * it produces cannot disagree: if they ever do, they do so together, and
1105
+ * the capability tests catch it.
1106
+ */
1107
+ effortLevelsFor(model: string, thinking?: ThinkingConfig): readonly ReasoningEffort[] {
1108
+ const capability = resolveThinkingCapability(model)
1109
+ const body = resolveThinkingBody(thinking, capability)
1110
+ return body?.type === 'disabled' ? capability.effortWhenDisabled : capability.effort
1111
+ }
1085
1112
  }
package/src/index.ts CHANGED
@@ -24,3 +24,21 @@ export function registerAnthropic(options?: RegisterOptions): void {
24
24
 
25
25
  export { ANTHROPIC_CAPABILITIES, AnthropicProvider } from './client.js'
26
26
  export type { AnthropicConfig, AnthropicProviderConfig } from './types.js'
27
+
28
+ // The driver's reading of which effort levels each model accepts, exported so
29
+ // a caller can build a control that offers only what will be honoured. The
30
+ // preferred path is `provider.effortLevelsFor(model, thinking)` — it is
31
+ // provider-agnostic and cannot return the wrong one of the two sets. This is
32
+ // the fuller picture for a caller that also needs to know whether thinking can
33
+ // be switched off at all, and it is exported rather than copied because the
34
+ // ceiling has moved twice and a copy would go stale silently.
35
+ export {
36
+ resolveThinkingCapability,
37
+ resolveThinkingBody,
38
+ resolveEffort,
39
+ } from './thinking-capability.js'
40
+ export type {
41
+ ThinkingCapability,
42
+ EffortLevels,
43
+ ResolvedThinkingBody,
44
+ } from './thinking-capability.js'
@@ -111,8 +111,29 @@ export function resolveThinkingCapability(model: string): ThinkingCapability {
111
111
  adaptive: true,
112
112
  manual: true,
113
113
  canDisable: false,
114
- effort: FULL_EFFORT,
115
- effortWhenDisabled: FULL_EFFORT,
114
+ // `max` but NOT `xhigh` — the combination it is easy to assume
115
+ // away, because reading the levels as a ladder makes anything
116
+ // accepting the top rung look like it accepts the one below. That
117
+ // reading is what put all five levels on this row, and it sends a
118
+ // level the wire refuses.
119
+ //
120
+ // SOURCED, NOT MEASURED, and the distinction is recorded because
121
+ // this file has been wrong from exactly this evidence class
122
+ // before. The reference states this model's availability twice: it
123
+ // is in the `max` list both times and in the `xhigh` list neither,
124
+ // and it says outright that some models supporting `max` do not
125
+ // support `xhigh`. The model is not reachable from the tenant used
126
+ // for the live contract suite (404), so no probe can confirm it
127
+ // here.
128
+ //
129
+ // What IS measured is that the pairing exists and this wire reports
130
+ // it exactly this way — `claude-sonnet-4-6` answers `xhigh` with
131
+ // "This model does not support effort level 'xhigh'. Supported
132
+ // levels: high, low, max, medium." and accepts `max`. So the row
133
+ // below is not a guess about a shape nobody has seen; it is a
134
+ // documented instance of a shape the wire demonstrably has.
135
+ effort: EFFORT_WITHOUT_XHIGH,
136
+ effortWhenDisabled: EFFORT_WITHOUT_XHIGH,
116
137
  }
117
138
  }
118
139