@anthropic-ai/sdk 0.70.1 → 0.71.1
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.
- package/CHANGELOG.md +31 -0
- package/README.md +1 -1
- package/client.d.mts +1 -1
- package/client.d.mts.map +1 -1
- package/client.d.ts +1 -1
- package/client.d.ts.map +1 -1
- package/helpers/beta/json-schema.d.mts +1 -1
- package/helpers/beta/json-schema.d.ts +1 -1
- package/helpers/beta/json-schema.js +1 -1
- package/helpers/beta/json-schema.mjs +1 -1
- package/helpers/beta/zod.d.mts +1 -1
- package/helpers/beta/zod.d.ts +1 -1
- package/helpers/beta/zod.js +1 -1
- package/helpers/beta/zod.mjs +1 -1
- package/lib/BetaMessageStream.d.mts +7 -2
- package/lib/BetaMessageStream.d.mts.map +1 -1
- package/lib/BetaMessageStream.d.ts +7 -2
- package/lib/BetaMessageStream.d.ts.map +1 -1
- package/lib/BetaMessageStream.js +9 -7
- package/lib/BetaMessageStream.js.map +1 -1
- package/lib/BetaMessageStream.mjs +9 -7
- package/lib/BetaMessageStream.mjs.map +1 -1
- package/lib/beta-parser.d.mts +8 -3
- package/lib/beta-parser.d.mts.map +1 -1
- package/lib/beta-parser.d.ts +8 -3
- package/lib/beta-parser.d.ts.map +1 -1
- package/lib/beta-parser.js +30 -16
- package/lib/beta-parser.js.map +1 -1
- package/lib/beta-parser.mjs +30 -16
- package/lib/beta-parser.mjs.map +1 -1
- package/lib/tools/BetaToolRunner.d.mts +2 -0
- package/lib/tools/BetaToolRunner.d.mts.map +1 -1
- package/lib/tools/BetaToolRunner.d.ts +2 -0
- package/lib/tools/BetaToolRunner.d.ts.map +1 -1
- package/lib/tools/BetaToolRunner.js +88 -15
- package/lib/tools/BetaToolRunner.js.map +1 -1
- package/lib/tools/BetaToolRunner.mjs +88 -15
- package/lib/tools/BetaToolRunner.mjs.map +1 -1
- package/lib/tools/CompactionControl.d.mts +25 -0
- package/lib/tools/CompactionControl.d.mts.map +1 -0
- package/lib/tools/CompactionControl.d.ts +25 -0
- package/lib/tools/CompactionControl.d.ts.map +1 -0
- package/lib/tools/CompactionControl.js +28 -0
- package/lib/tools/CompactionControl.js.map +1 -0
- package/lib/tools/CompactionControl.mjs +25 -0
- package/lib/tools/CompactionControl.mjs.map +1 -0
- package/lib/tools/ToolRunner.js +1 -1
- package/lib/tools/ToolRunner.js.map +1 -1
- package/lib/tools/ToolRunner.mjs +1 -1
- package/lib/tools/ToolRunner.mjs.map +1 -1
- package/package.json +1 -1
- package/resources/beta/beta.d.mts +3 -3
- package/resources/beta/beta.d.mts.map +1 -1
- package/resources/beta/beta.d.ts +3 -3
- package/resources/beta/beta.d.ts.map +1 -1
- package/resources/beta/beta.js.map +1 -1
- package/resources/beta/beta.mjs.map +1 -1
- package/resources/beta/index.d.mts +1 -1
- package/resources/beta/index.d.mts.map +1 -1
- package/resources/beta/index.d.ts +1 -1
- package/resources/beta/index.d.ts.map +1 -1
- package/resources/beta/index.js.map +1 -1
- package/resources/beta/index.mjs.map +1 -1
- package/resources/beta/messages/batches.d.mts +288 -3
- package/resources/beta/messages/batches.d.mts.map +1 -1
- package/resources/beta/messages/batches.d.ts +288 -3
- package/resources/beta/messages/batches.d.ts.map +1 -1
- package/resources/beta/messages/batches.js.map +1 -1
- package/resources/beta/messages/batches.mjs.map +1 -1
- package/resources/beta/messages/index.d.mts +1 -1
- package/resources/beta/messages/index.d.mts.map +1 -1
- package/resources/beta/messages/index.d.ts +1 -1
- package/resources/beta/messages/index.d.ts.map +1 -1
- package/resources/beta/messages/index.js.map +1 -1
- package/resources/beta/messages/index.mjs.map +1 -1
- package/resources/beta/messages/messages.d.mts +346 -13
- package/resources/beta/messages/messages.d.mts.map +1 -1
- package/resources/beta/messages/messages.d.ts +346 -13
- package/resources/beta/messages/messages.d.ts.map +1 -1
- package/resources/beta/messages/messages.js +4 -4
- package/resources/beta/messages/messages.js.map +1 -1
- package/resources/beta/messages/messages.mjs +4 -4
- package/resources/beta/messages/messages.mjs.map +1 -1
- package/resources/completions.d.mts +1 -1
- package/resources/completions.d.ts +1 -1
- package/resources/messages/batches.d.mts +2 -2
- package/resources/messages/batches.d.ts +2 -2
- package/resources/messages/messages.d.mts +2 -2
- package/resources/messages/messages.d.mts.map +1 -1
- package/resources/messages/messages.d.ts +2 -2
- package/resources/messages/messages.d.ts.map +1 -1
- package/resources/messages/messages.js +1 -1
- package/resources/messages/messages.js.map +1 -1
- package/resources/messages/messages.mjs +1 -1
- package/resources/messages/messages.mjs.map +1 -1
- package/src/client.ts +1 -1
- package/src/helpers/beta/json-schema.ts +1 -1
- package/src/helpers/beta/zod.ts +1 -1
- package/src/lib/BetaMessageStream.ts +11 -4
- package/src/lib/beta-parser.ts +37 -15
- package/src/lib/tools/BetaToolRunner.ts +101 -15
- package/src/lib/tools/CompactionControl.ts +52 -0
- package/src/lib/tools/ToolRunner.ts +1 -1
- package/src/resources/beta/beta.ts +19 -2
- package/src/resources/beta/index.ts +18 -1
- package/src/resources/beta/messages/batches.ts +307 -3
- package/src/resources/beta/messages/index.ts +18 -1
- package/src/resources/beta/messages/messages.ts +467 -11
- package/src/resources/completions.ts +1 -1
- package/src/resources/messages/batches.ts +2 -2
- package/src/resources/messages/messages.ts +3 -1
- package/src/version.ts +1 -1
- package/version.d.mts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/version.mjs +1 -1
package/src/client.ts
CHANGED
|
@@ -37,7 +37,7 @@ export function betaTool<const Schema extends Exclude<JSONSchema, boolean> & { t
|
|
|
37
37
|
/**
|
|
38
38
|
* Creates a JSON schema output format object from the given JSON schema.
|
|
39
39
|
* If this is passed to the `.parse()` method then the response message will contain a
|
|
40
|
-
* `.
|
|
40
|
+
* `.parsed_output` property that is the result of parsing the content with the given JSON schema.
|
|
41
41
|
*
|
|
42
42
|
*/
|
|
43
43
|
export function betaJSONSchemaOutputFormat<
|
package/src/helpers/beta/zod.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { BetaToolResultContentBlockParam } from '../../resources/beta';
|
|
|
9
9
|
* Creates a JSON schema output format object from the given Zod schema.
|
|
10
10
|
*
|
|
11
11
|
* If this is passed to the `.parse()` method then the response message will contain a
|
|
12
|
-
* `.
|
|
12
|
+
* `.parsed_output` property that is the result of parsing the content with the given Zod object.
|
|
13
13
|
*
|
|
14
14
|
* This can be passed directly to the `.create()` method but will not
|
|
15
15
|
* result in any automatic parsing, you'll have to parse the response yourself.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { partialParse } from '../_vendor/partial-json-parser/parser';
|
|
2
|
+
import type { Logger } from '../client';
|
|
2
3
|
import { AnthropicError, APIUserAbortError } from '../error';
|
|
3
4
|
import { isAbortError } from '../internal/errors';
|
|
4
5
|
import { type RequestOptions } from '../internal/request-options';
|
|
@@ -73,8 +74,9 @@ export class BetaMessageStream<ParsedT = null> implements AsyncIterable<BetaMess
|
|
|
73
74
|
#catchingPromiseCreated = false;
|
|
74
75
|
#response: Response | null | undefined;
|
|
75
76
|
#request_id: string | null | undefined;
|
|
77
|
+
#logger: Logger;
|
|
76
78
|
|
|
77
|
-
constructor(params: MessageCreateParamsBase | null) {
|
|
79
|
+
constructor(params: MessageCreateParamsBase | null, opts?: { logger?: Logger | undefined }) {
|
|
78
80
|
this.#connectedPromise = new Promise<Response | null>((resolve, reject) => {
|
|
79
81
|
this.#resolveConnectedPromise = resolve;
|
|
80
82
|
this.#rejectConnectedPromise = reject;
|
|
@@ -93,6 +95,7 @@ export class BetaMessageStream<ParsedT = null> implements AsyncIterable<BetaMess
|
|
|
93
95
|
this.#endPromise.catch(() => {});
|
|
94
96
|
|
|
95
97
|
this.#params = params;
|
|
98
|
+
this.#logger = opts?.logger ?? console;
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
get response(): Response | null | undefined {
|
|
@@ -147,8 +150,9 @@ export class BetaMessageStream<ParsedT = null> implements AsyncIterable<BetaMess
|
|
|
147
150
|
messages: BetaMessages,
|
|
148
151
|
params: MessageCreateParamsBase,
|
|
149
152
|
options?: RequestOptions,
|
|
153
|
+
{ logger }: { logger?: Logger | undefined } = {},
|
|
150
154
|
): BetaMessageStream<ParsedT> {
|
|
151
|
-
const runner = new BetaMessageStream<ParsedT>(params as MessageCreateParamsStreaming);
|
|
155
|
+
const runner = new BetaMessageStream<ParsedT>(params as MessageCreateParamsStreaming, { logger });
|
|
152
156
|
for (const message of params.messages) {
|
|
153
157
|
runner._addMessageParam(message);
|
|
154
158
|
}
|
|
@@ -479,7 +483,10 @@ export class BetaMessageStream<ParsedT = null> implements AsyncIterable<BetaMess
|
|
|
479
483
|
}
|
|
480
484
|
case 'message_stop': {
|
|
481
485
|
this._addMessageParam(messageSnapshot);
|
|
482
|
-
this._addMessage(
|
|
486
|
+
this._addMessage(
|
|
487
|
+
maybeParseBetaMessage(messageSnapshot, this.#params, { logger: this.#logger }),
|
|
488
|
+
true,
|
|
489
|
+
);
|
|
483
490
|
break;
|
|
484
491
|
}
|
|
485
492
|
case 'content_block_stop': {
|
|
@@ -504,7 +511,7 @@ export class BetaMessageStream<ParsedT = null> implements AsyncIterable<BetaMess
|
|
|
504
511
|
throw new AnthropicError(`request ended without sending any chunks`);
|
|
505
512
|
}
|
|
506
513
|
this.#currentMessageSnapshot = undefined;
|
|
507
|
-
return maybeParseBetaMessage(snapshot, this.#params);
|
|
514
|
+
return maybeParseBetaMessage(snapshot, this.#params, { logger: this.#logger });
|
|
508
515
|
}
|
|
509
516
|
|
|
510
517
|
protected async _fromReadableStream(
|
package/src/lib/beta-parser.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Logger } from '../client';
|
|
1
2
|
import { AnthropicError } from '../core/error';
|
|
2
3
|
import {
|
|
3
4
|
BetaContentBlock,
|
|
@@ -29,22 +30,33 @@ export type ParsedBetaMessage<ParsedT> = BetaMessage & {
|
|
|
29
30
|
};
|
|
30
31
|
|
|
31
32
|
export type ParsedBetaContentBlock<ParsedT> =
|
|
32
|
-
| (BetaTextBlock & {
|
|
33
|
+
| (BetaTextBlock & { parsed_output: ParsedT | null })
|
|
33
34
|
| Exclude<BetaContentBlock, BetaTextBlock>;
|
|
34
35
|
|
|
35
36
|
export function maybeParseBetaMessage<Params extends BetaParseableMessageCreateParams | null>(
|
|
36
37
|
message: BetaMessage,
|
|
37
38
|
params: Params,
|
|
39
|
+
opts: { logger: Logger },
|
|
38
40
|
): ParsedBetaMessage<ExtractParsedContentFromBetaParams<NonNullable<Params>>> {
|
|
39
41
|
if (!params || !('parse' in (params.output_format ?? {}))) {
|
|
40
42
|
return {
|
|
41
43
|
...message,
|
|
42
44
|
content: message.content.map((block) => {
|
|
43
45
|
if (block.type === 'text') {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
46
|
+
const parsedBlock = Object.defineProperty({ ...block }, 'parsed_output', {
|
|
47
|
+
value: null,
|
|
48
|
+
enumerable: false,
|
|
49
|
+
}) as ParsedBetaContentBlock<ExtractParsedContentFromBetaParams<NonNullable<Params>>>;
|
|
50
|
+
|
|
51
|
+
return Object.defineProperty(parsedBlock, 'parsed', {
|
|
52
|
+
get() {
|
|
53
|
+
opts.logger.warn(
|
|
54
|
+
'The `parsed` property on `text` blocks is deprecated, please use `parsed_output` instead.',
|
|
55
|
+
);
|
|
56
|
+
return null;
|
|
57
|
+
},
|
|
58
|
+
enumerable: false,
|
|
59
|
+
});
|
|
48
60
|
}
|
|
49
61
|
return block;
|
|
50
62
|
}),
|
|
@@ -52,28 +64,38 @@ export function maybeParseBetaMessage<Params extends BetaParseableMessageCreateP
|
|
|
52
64
|
} as ParsedBetaMessage<ExtractParsedContentFromBetaParams<NonNullable<Params>>>;
|
|
53
65
|
}
|
|
54
66
|
|
|
55
|
-
return parseBetaMessage(message, params);
|
|
67
|
+
return parseBetaMessage(message, params, opts);
|
|
56
68
|
}
|
|
57
69
|
|
|
58
70
|
export function parseBetaMessage<Params extends BetaParseableMessageCreateParams>(
|
|
59
71
|
message: BetaMessage,
|
|
60
72
|
params: Params,
|
|
73
|
+
opts: { logger: Logger },
|
|
61
74
|
): ParsedBetaMessage<ExtractParsedContentFromBetaParams<Params>> {
|
|
62
|
-
let
|
|
75
|
+
let firstParsedOutput: ReturnType<typeof parseBetaOutputFormat<Params>> | null = null;
|
|
63
76
|
|
|
64
77
|
const content: Array<ParsedBetaContentBlock<ExtractParsedContentFromBetaParams<Params>>> =
|
|
65
78
|
message.content.map((block) => {
|
|
66
79
|
if (block.type === 'text') {
|
|
67
|
-
const
|
|
80
|
+
const parsedOutput = parseBetaOutputFormat(params, block.text);
|
|
68
81
|
|
|
69
|
-
if (
|
|
70
|
-
|
|
82
|
+
if (firstParsedOutput === null) {
|
|
83
|
+
firstParsedOutput = parsedOutput;
|
|
71
84
|
}
|
|
72
85
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
86
|
+
const parsedBlock = Object.defineProperty({ ...block }, 'parsed_output', {
|
|
87
|
+
value: parsedOutput,
|
|
88
|
+
enumerable: false,
|
|
89
|
+
}) as ParsedBetaContentBlock<ExtractParsedContentFromBetaParams<Params>>;
|
|
90
|
+
return Object.defineProperty(parsedBlock, 'parsed', {
|
|
91
|
+
get() {
|
|
92
|
+
opts.logger.warn(
|
|
93
|
+
'The `parsed` property on `text` blocks is deprecated, please use `parsed_output` instead.',
|
|
94
|
+
);
|
|
95
|
+
return parsedOutput;
|
|
96
|
+
},
|
|
97
|
+
enumerable: false,
|
|
98
|
+
});
|
|
77
99
|
}
|
|
78
100
|
return block;
|
|
79
101
|
});
|
|
@@ -81,7 +103,7 @@ export function parseBetaMessage<Params extends BetaParseableMessageCreateParams
|
|
|
81
103
|
return {
|
|
82
104
|
...message,
|
|
83
105
|
content,
|
|
84
|
-
parsed_output:
|
|
106
|
+
parsed_output: firstParsedOutput,
|
|
85
107
|
} as ParsedBetaMessage<ExtractParsedContentFromBetaParams<Params>>;
|
|
86
108
|
}
|
|
87
109
|
|
|
@@ -5,6 +5,7 @@ import { BetaMessage, BetaMessageParam, BetaToolUnion, MessageCreateParams } fro
|
|
|
5
5
|
import { BetaMessageStream } from '../BetaMessageStream';
|
|
6
6
|
import { RequestOptions } from '../../internal/request-options';
|
|
7
7
|
import { buildHeaders } from '../../internal/headers';
|
|
8
|
+
import { CompactionControl, DEFAULT_SUMMARY_PROMPT, DEFAULT_TOKEN_THRESHOLD } from './CompactionControl';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Just Promise.withResolvers(), which is not available in all environments.
|
|
@@ -72,6 +73,88 @@ export class BetaToolRunner<Stream extends boolean> {
|
|
|
72
73
|
this.#completion = promiseWithResolvers();
|
|
73
74
|
}
|
|
74
75
|
|
|
76
|
+
async #checkAndCompact(): Promise<boolean> {
|
|
77
|
+
const compactionControl = this.#state.params.compactionControl;
|
|
78
|
+
if (!compactionControl || !compactionControl.enabled) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let tokensUsed = 0;
|
|
83
|
+
if (this.#message !== undefined) {
|
|
84
|
+
try {
|
|
85
|
+
const message = await this.#message;
|
|
86
|
+
const totalInputTokens =
|
|
87
|
+
message.usage.input_tokens +
|
|
88
|
+
(message.usage.cache_creation_input_tokens ?? 0) +
|
|
89
|
+
(message.usage.cache_read_input_tokens ?? 0);
|
|
90
|
+
tokensUsed = totalInputTokens + message.usage.output_tokens;
|
|
91
|
+
} catch {
|
|
92
|
+
// If we can't get the message, skip compaction
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const threshold = compactionControl.contextTokenThreshold ?? DEFAULT_TOKEN_THRESHOLD;
|
|
98
|
+
|
|
99
|
+
if (tokensUsed < threshold) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const model = compactionControl.model ?? this.#state.params.model;
|
|
104
|
+
const summaryPrompt = compactionControl.summaryPrompt ?? DEFAULT_SUMMARY_PROMPT;
|
|
105
|
+
|
|
106
|
+
const messages = this.#state.params.messages;
|
|
107
|
+
|
|
108
|
+
if (messages[messages.length - 1]!.role === 'assistant') {
|
|
109
|
+
// Remove tool_use blocks from the last message to avoid 400 error
|
|
110
|
+
// (tool_use requires tool_result, which we don't have yet)
|
|
111
|
+
const lastMessage = messages[messages.length - 1]!;
|
|
112
|
+
if (Array.isArray(lastMessage.content)) {
|
|
113
|
+
const nonToolBlocks = lastMessage.content.filter((block) => block.type !== 'tool_use');
|
|
114
|
+
|
|
115
|
+
if (nonToolBlocks.length === 0) {
|
|
116
|
+
// If all blocks were tool_use, just remove the message entirely
|
|
117
|
+
messages.pop();
|
|
118
|
+
} else {
|
|
119
|
+
lastMessage.content = nonToolBlocks;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const response = await this.client.beta.messages.create(
|
|
125
|
+
{
|
|
126
|
+
model,
|
|
127
|
+
messages: [
|
|
128
|
+
...messages,
|
|
129
|
+
{
|
|
130
|
+
role: 'user',
|
|
131
|
+
content: [
|
|
132
|
+
{
|
|
133
|
+
type: 'text',
|
|
134
|
+
text: summaryPrompt,
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
max_tokens: this.#state.params.max_tokens,
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
headers: { 'x-stainless-helper': 'compaction' },
|
|
143
|
+
},
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
if (response.content[0]?.type !== 'text') {
|
|
147
|
+
throw new AnthropicError('Expected text response for compaction');
|
|
148
|
+
}
|
|
149
|
+
this.#state.params.messages = [
|
|
150
|
+
{
|
|
151
|
+
role: 'user',
|
|
152
|
+
content: response.content,
|
|
153
|
+
},
|
|
154
|
+
];
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
|
|
75
158
|
async *[Symbol.asyncIterator](): AsyncIterator<
|
|
76
159
|
Stream extends true ? BetaMessageStream
|
|
77
160
|
: Stream extends false ? BetaMessage
|
|
@@ -97,11 +180,12 @@ export class BetaToolRunner<Stream extends boolean> {
|
|
|
97
180
|
}
|
|
98
181
|
|
|
99
182
|
this.#mutated = false;
|
|
100
|
-
this.#message = undefined;
|
|
101
183
|
this.#toolResponse = undefined;
|
|
102
184
|
this.#iterationCount++;
|
|
185
|
+
this.#message = undefined;
|
|
186
|
+
|
|
187
|
+
const { max_iterations, compactionControl, ...params } = this.#state.params;
|
|
103
188
|
|
|
104
|
-
const { max_iterations, ...params } = this.#state.params;
|
|
105
189
|
if (params.stream) {
|
|
106
190
|
stream = this.client.beta.messages.stream({ ...params }, this.#options);
|
|
107
191
|
this.#message = stream.finalMessage();
|
|
@@ -114,18 +198,19 @@ export class BetaToolRunner<Stream extends boolean> {
|
|
|
114
198
|
yield this.#message as any;
|
|
115
199
|
}
|
|
116
200
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
this.#
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
this.#state.params.messages.
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
201
|
+
const isCompacted = await this.#checkAndCompact();
|
|
202
|
+
if (!isCompacted) {
|
|
203
|
+
if (!this.#mutated) {
|
|
204
|
+
const { role, content } = await this.#message;
|
|
205
|
+
this.#state.params.messages.push({ role, content });
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const toolMessage = await this.#generateToolResponse(this.#state.params.messages.at(-1)!);
|
|
209
|
+
if (toolMessage) {
|
|
210
|
+
this.#state.params.messages.push(toolMessage);
|
|
211
|
+
} else if (!this.#mutated) {
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
129
214
|
}
|
|
130
215
|
} finally {
|
|
131
216
|
if (stream) {
|
|
@@ -327,7 +412,7 @@ async function generateToolResponse(
|
|
|
327
412
|
|
|
328
413
|
const toolResults = await Promise.all(
|
|
329
414
|
toolUseBlocks.map(async (toolUse) => {
|
|
330
|
-
const tool = params.tools.find((t) => t.name === toolUse.name);
|
|
415
|
+
const tool = params.tools.find((t) => ('name' in t ? t.name : t.mcp_server_name) === toolUse.name);
|
|
331
416
|
if (!tool || !('run' in tool)) {
|
|
332
417
|
return {
|
|
333
418
|
type: 'tool_result' as const,
|
|
@@ -381,6 +466,7 @@ export type BetaToolRunnerParams = Simplify<
|
|
|
381
466
|
* When exceeded, the loop will terminate even if tools are still being requested.
|
|
382
467
|
*/
|
|
383
468
|
max_iterations?: number;
|
|
469
|
+
compactionControl?: CompactionControl;
|
|
384
470
|
}
|
|
385
471
|
>;
|
|
386
472
|
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Model } from '../../resources';
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_TOKEN_THRESHOLD = 100_000;
|
|
4
|
+
|
|
5
|
+
export const DEFAULT_SUMMARY_PROMPT = `You have been working on the task described above but have not yet completed it. Write a continuation summary that will allow you (or another instance of yourself) to resume work efficiently in a future context window where the conversation history will be replaced with this summary. Your summary should be structured, concise, and actionable. Include:
|
|
6
|
+
1. Task Overview
|
|
7
|
+
The user's core request and success criteria
|
|
8
|
+
Any clarifications or constraints they specified
|
|
9
|
+
2. Current State
|
|
10
|
+
What has been completed so far
|
|
11
|
+
Files created, modified, or analyzed (with paths if relevant)
|
|
12
|
+
Key outputs or artifacts produced
|
|
13
|
+
3. Important Discoveries
|
|
14
|
+
Technical constraints or requirements uncovered
|
|
15
|
+
Decisions made and their rationale
|
|
16
|
+
Errors encountered and how they were resolved
|
|
17
|
+
What approaches were tried that didn't work (and why)
|
|
18
|
+
4. Next Steps
|
|
19
|
+
Specific actions needed to complete the task
|
|
20
|
+
Any blockers or open questions to resolve
|
|
21
|
+
Priority order if multiple steps remain
|
|
22
|
+
5. Context to Preserve
|
|
23
|
+
User preferences or style requirements
|
|
24
|
+
Domain-specific details that aren't obvious
|
|
25
|
+
Any promises made to the user
|
|
26
|
+
Be concise but complete—err on the side of including information that would prevent duplicate work or repeated mistakes. Write in a way that enables immediate resumption of the task.
|
|
27
|
+
Wrap your summary in <summary></summary> tags.`;
|
|
28
|
+
|
|
29
|
+
export interface CompactionControl {
|
|
30
|
+
/**
|
|
31
|
+
* The context token threshold at which to trigger compaction.
|
|
32
|
+
*
|
|
33
|
+
* When the cumulative token count (input + output) across all messages exceeds this threshold,
|
|
34
|
+
* the message history will be automatically summarized and compressed.
|
|
35
|
+
*
|
|
36
|
+
* @default 100000
|
|
37
|
+
*/
|
|
38
|
+
contextTokenThreshold?: number;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The model to use for generating the compaction summary.
|
|
42
|
+
* If not specified, defaults to the same model used for the tool runner.
|
|
43
|
+
*/
|
|
44
|
+
model?: Model;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The prompt used to instruct the model on how to generate the summary.
|
|
48
|
+
*/
|
|
49
|
+
summaryPrompt?: string;
|
|
50
|
+
|
|
51
|
+
enabled: boolean;
|
|
52
|
+
}
|
|
@@ -319,7 +319,7 @@ async function generateToolResponse(
|
|
|
319
319
|
|
|
320
320
|
const toolResults = await Promise.all(
|
|
321
321
|
toolUseBlocks.map(async (toolUse) => {
|
|
322
|
-
const tool = params.tools.find((t) => t.name === toolUse.name);
|
|
322
|
+
const tool = params.tools.find((t) => ('name' in t ? t.name : t.mcp_server_name) === toolUse.name);
|
|
323
323
|
if (!tool || !('run' in tool)) {
|
|
324
324
|
return {
|
|
325
325
|
type: 'tool_result' as const,
|
|
@@ -72,6 +72,7 @@ import {
|
|
|
72
72
|
BetaContextManagementConfig,
|
|
73
73
|
BetaContextManagementResponse,
|
|
74
74
|
BetaCountTokensContextManagementResponse,
|
|
75
|
+
BetaDirectCaller,
|
|
75
76
|
BetaDocumentBlock,
|
|
76
77
|
BetaFileDocumentSource,
|
|
77
78
|
BetaFileImageSource,
|
|
@@ -83,6 +84,7 @@ import {
|
|
|
83
84
|
BetaMCPToolResultBlock,
|
|
84
85
|
BetaMCPToolUseBlock,
|
|
85
86
|
BetaMCPToolUseBlockParam,
|
|
87
|
+
BetaMCPToolset,
|
|
86
88
|
BetaMemoryTool20250818,
|
|
87
89
|
BetaMemoryTool20250818Command,
|
|
88
90
|
BetaMemoryTool20250818CreateCommand,
|
|
@@ -96,6 +98,7 @@ import {
|
|
|
96
98
|
BetaMessageParam,
|
|
97
99
|
BetaMessageTokensCount,
|
|
98
100
|
BetaMetadata,
|
|
101
|
+
BetaOutputConfig,
|
|
99
102
|
BetaPlainTextSource,
|
|
100
103
|
BetaRawContentBlockDelta,
|
|
101
104
|
BetaRawContentBlockDeltaEvent,
|
|
@@ -112,6 +115,7 @@ import {
|
|
|
112
115
|
BetaRequestMCPServerURLDefinition,
|
|
113
116
|
BetaRequestMCPToolResultBlockParam,
|
|
114
117
|
BetaSearchResultBlockParam,
|
|
118
|
+
BetaServerToolCaller,
|
|
115
119
|
BetaServerToolUsage,
|
|
116
120
|
BetaServerToolUseBlock,
|
|
117
121
|
BetaServerToolUseBlockParam,
|
|
@@ -151,8 +155,10 @@ import {
|
|
|
151
155
|
BetaToolChoiceTool,
|
|
152
156
|
BetaToolComputerUse20241022,
|
|
153
157
|
BetaToolComputerUse20250124,
|
|
158
|
+
BetaToolComputerUse20251124,
|
|
159
|
+
BetaToolReferenceBlock,
|
|
160
|
+
BetaToolReferenceBlockParam,
|
|
154
161
|
BetaToolResultBlockParam,
|
|
155
|
-
BetaToolResultContentBlockParam,
|
|
156
162
|
BetaToolTextEditor20241022,
|
|
157
163
|
BetaToolTextEditor20250124,
|
|
158
164
|
BetaToolTextEditor20250429,
|
|
@@ -188,6 +194,8 @@ import {
|
|
|
188
194
|
MessageCreateParamsNonStreaming,
|
|
189
195
|
MessageCreateParamsStreaming,
|
|
190
196
|
Messages,
|
|
197
|
+
BetaMCPToolConfig,
|
|
198
|
+
BetaMCPToolDefaultConfig,
|
|
191
199
|
} from './messages/messages';
|
|
192
200
|
import * as SkillsAPI from './skills/skills';
|
|
193
201
|
import {
|
|
@@ -222,6 +230,7 @@ export type AnthropicBeta =
|
|
|
222
230
|
| 'output-128k-2025-02-19'
|
|
223
231
|
| 'files-api-2025-04-14'
|
|
224
232
|
| 'mcp-client-2025-04-04'
|
|
233
|
+
| 'mcp-client-2025-11-20'
|
|
225
234
|
| 'dev-full-thinking-2025-05-14'
|
|
226
235
|
| 'interleaved-thinking-2025-05-14'
|
|
227
236
|
| 'code-execution-2025-05-22'
|
|
@@ -389,6 +398,7 @@ export declare namespace Beta {
|
|
|
389
398
|
type BetaContextManagementConfig as BetaContextManagementConfig,
|
|
390
399
|
type BetaContextManagementResponse as BetaContextManagementResponse,
|
|
391
400
|
type BetaCountTokensContextManagementResponse as BetaCountTokensContextManagementResponse,
|
|
401
|
+
type BetaDirectCaller as BetaDirectCaller,
|
|
392
402
|
type BetaDocumentBlock as BetaDocumentBlock,
|
|
393
403
|
type BetaFileDocumentSource as BetaFileDocumentSource,
|
|
394
404
|
type BetaFileImageSource as BetaFileImageSource,
|
|
@@ -397,9 +407,12 @@ export declare namespace Beta {
|
|
|
397
407
|
type BetaJSONOutputFormat as BetaJSONOutputFormat,
|
|
398
408
|
type BetaInputTokensClearAtLeast as BetaInputTokensClearAtLeast,
|
|
399
409
|
type BetaInputTokensTrigger as BetaInputTokensTrigger,
|
|
410
|
+
type BetaMCPToolConfig as BetaMCPToolConfig,
|
|
411
|
+
type BetaMCPToolDefaultConfig as BetaMCPToolDefaultConfig,
|
|
400
412
|
type BetaMCPToolResultBlock as BetaMCPToolResultBlock,
|
|
401
413
|
type BetaMCPToolUseBlock as BetaMCPToolUseBlock,
|
|
402
414
|
type BetaMCPToolUseBlockParam as BetaMCPToolUseBlockParam,
|
|
415
|
+
type BetaMCPToolset as BetaMCPToolset,
|
|
403
416
|
type BetaMemoryTool20250818 as BetaMemoryTool20250818,
|
|
404
417
|
type BetaMemoryTool20250818Command as BetaMemoryTool20250818Command,
|
|
405
418
|
type BetaMemoryTool20250818CreateCommand as BetaMemoryTool20250818CreateCommand,
|
|
@@ -413,6 +426,7 @@ export declare namespace Beta {
|
|
|
413
426
|
type BetaMessageParam as BetaMessageParam,
|
|
414
427
|
type BetaMessageTokensCount as BetaMessageTokensCount,
|
|
415
428
|
type BetaMetadata as BetaMetadata,
|
|
429
|
+
type BetaOutputConfig as BetaOutputConfig,
|
|
416
430
|
type BetaPlainTextSource as BetaPlainTextSource,
|
|
417
431
|
type BetaRawContentBlockDelta as BetaRawContentBlockDelta,
|
|
418
432
|
type BetaRawContentBlockDeltaEvent as BetaRawContentBlockDeltaEvent,
|
|
@@ -429,6 +443,7 @@ export declare namespace Beta {
|
|
|
429
443
|
type BetaRequestMCPServerURLDefinition as BetaRequestMCPServerURLDefinition,
|
|
430
444
|
type BetaRequestMCPToolResultBlockParam as BetaRequestMCPToolResultBlockParam,
|
|
431
445
|
type BetaSearchResultBlockParam as BetaSearchResultBlockParam,
|
|
446
|
+
type BetaServerToolCaller as BetaServerToolCaller,
|
|
432
447
|
type BetaServerToolUsage as BetaServerToolUsage,
|
|
433
448
|
type BetaServerToolUseBlock as BetaServerToolUseBlock,
|
|
434
449
|
type BetaServerToolUseBlockParam as BetaServerToolUseBlockParam,
|
|
@@ -468,8 +483,10 @@ export declare namespace Beta {
|
|
|
468
483
|
type BetaToolChoiceTool as BetaToolChoiceTool,
|
|
469
484
|
type BetaToolComputerUse20241022 as BetaToolComputerUse20241022,
|
|
470
485
|
type BetaToolComputerUse20250124 as BetaToolComputerUse20250124,
|
|
486
|
+
type BetaToolComputerUse20251124 as BetaToolComputerUse20251124,
|
|
487
|
+
type BetaToolReferenceBlock as BetaToolReferenceBlock,
|
|
488
|
+
type BetaToolReferenceBlockParam as BetaToolReferenceBlockParam,
|
|
471
489
|
type BetaToolResultBlockParam as BetaToolResultBlockParam,
|
|
472
|
-
type BetaToolResultContentBlockParam as BetaToolResultContentBlockParam,
|
|
473
490
|
type BetaToolTextEditor20241022 as BetaToolTextEditor20241022,
|
|
474
491
|
type BetaToolTextEditor20250124 as BetaToolTextEditor20250124,
|
|
475
492
|
type BetaToolTextEditor20250429 as BetaToolTextEditor20250429,
|
|
@@ -82,6 +82,7 @@ export {
|
|
|
82
82
|
type BetaContextManagementConfig,
|
|
83
83
|
type BetaContextManagementResponse,
|
|
84
84
|
type BetaCountTokensContextManagementResponse,
|
|
85
|
+
type BetaDirectCaller,
|
|
85
86
|
type BetaDocumentBlock,
|
|
86
87
|
type BetaFileDocumentSource,
|
|
87
88
|
type BetaFileImageSource,
|
|
@@ -90,9 +91,12 @@ export {
|
|
|
90
91
|
type BetaJSONOutputFormat,
|
|
91
92
|
type BetaInputTokensClearAtLeast,
|
|
92
93
|
type BetaInputTokensTrigger,
|
|
94
|
+
type BetaMCPToolConfig,
|
|
95
|
+
type BetaMCPToolDefaultConfig,
|
|
93
96
|
type BetaMCPToolResultBlock,
|
|
94
97
|
type BetaMCPToolUseBlock,
|
|
95
98
|
type BetaMCPToolUseBlockParam,
|
|
99
|
+
type BetaMCPToolset,
|
|
96
100
|
type BetaMemoryTool20250818,
|
|
97
101
|
type BetaMemoryTool20250818Command,
|
|
98
102
|
type BetaMemoryTool20250818CreateCommand,
|
|
@@ -106,6 +110,7 @@ export {
|
|
|
106
110
|
type BetaMessageParam,
|
|
107
111
|
type BetaMessageTokensCount,
|
|
108
112
|
type BetaMetadata,
|
|
113
|
+
type BetaOutputConfig,
|
|
109
114
|
type BetaPlainTextSource,
|
|
110
115
|
type BetaRawContentBlockDelta,
|
|
111
116
|
type BetaRawContentBlockDeltaEvent,
|
|
@@ -122,6 +127,7 @@ export {
|
|
|
122
127
|
type BetaRequestMCPServerURLDefinition,
|
|
123
128
|
type BetaRequestMCPToolResultBlockParam,
|
|
124
129
|
type BetaSearchResultBlockParam,
|
|
130
|
+
type BetaServerToolCaller,
|
|
125
131
|
type BetaServerToolUsage,
|
|
126
132
|
type BetaServerToolUseBlock,
|
|
127
133
|
type BetaServerToolUseBlockParam,
|
|
@@ -161,8 +167,18 @@ export {
|
|
|
161
167
|
type BetaToolChoiceTool,
|
|
162
168
|
type BetaToolComputerUse20241022,
|
|
163
169
|
type BetaToolComputerUse20250124,
|
|
170
|
+
type BetaToolComputerUse20251124,
|
|
171
|
+
type BetaToolReferenceBlock,
|
|
172
|
+
type BetaToolReferenceBlockParam,
|
|
164
173
|
type BetaToolResultBlockParam,
|
|
165
|
-
type
|
|
174
|
+
type BetaToolSearchToolBm25_20251119,
|
|
175
|
+
type BetaToolSearchToolRegex20251119,
|
|
176
|
+
type BetaToolSearchToolResultBlock,
|
|
177
|
+
type BetaToolSearchToolResultBlockParam,
|
|
178
|
+
type BetaToolSearchToolResultError,
|
|
179
|
+
type BetaToolSearchToolResultErrorParam,
|
|
180
|
+
type BetaToolSearchToolSearchResultBlock,
|
|
181
|
+
type BetaToolSearchToolSearchResultBlockParam,
|
|
166
182
|
type BetaToolTextEditor20241022,
|
|
167
183
|
type BetaToolTextEditor20250124,
|
|
168
184
|
type BetaToolTextEditor20250429,
|
|
@@ -198,6 +214,7 @@ export {
|
|
|
198
214
|
type MessageCreateParamsNonStreaming,
|
|
199
215
|
type MessageCreateParamsStreaming,
|
|
200
216
|
type MessageCountTokensParams,
|
|
217
|
+
type BetaToolResultContentBlockParam,
|
|
201
218
|
} from './messages/index';
|
|
202
219
|
export {
|
|
203
220
|
Models,
|