feat: Complete zCode CLI X with Telegram bot integration

- Add full Telegram bot functionality with Z.AI API integration
- Implement 4 tools: Bash, FileEdit, WebSearch, Git
- Add 3 agents: Code Reviewer, Architect, DevOps Engineer
- Add 6 skills for common coding tasks
- Add systemd service file for 24/7 operation
- Add nginx configuration for HTTPS webhook
- Add comprehensive documentation
- Implement WebSocket server for real-time updates
- Add logging system with Winston
- Add environment validation

🤖 zCode CLI X - Agentic coder with Z.AI + Telegram integration
This commit is contained in:
admin
2026-05-05 09:01:26 +00:00
Unverified
parent 4a7035dd92
commit 875c7f9b91
24688 changed files with 3224957 additions and 221 deletions

View File

@@ -0,0 +1,3 @@
<svg width="248" height="248" viewBox="0 0 248 248" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M52.4285 162.873L98.7844 136.879L99.5485 134.602L98.7844 133.334H96.4921L88.7237 132.862L62.2346 132.153L39.3113 131.207L17.0249 130.026L11.4214 128.844L6.2 121.873L6.7094 118.447L11.4214 115.257L18.171 115.847L33.0711 116.911L55.485 118.447L71.6586 119.392L95.728 121.873H99.5485L100.058 120.337L98.7844 119.392L97.7656 118.447L74.5877 102.732L49.4995 86.1905L36.3823 76.62L29.3779 71.7757L25.8121 67.2858L24.2839 57.3608L30.6515 50.2716L39.3113 50.8623L41.4763 51.4531L50.2636 58.1879L68.9842 72.7209L93.4357 90.6804L97.0015 93.6343L98.4374 92.6652L98.6571 91.9801L97.0015 89.2625L83.757 65.2772L69.621 40.8192L63.2534 30.6579L61.5978 24.632C60.9565 22.1032 60.579 20.0111 60.579 17.4246L67.8381 7.49965L71.9133 6.19995L81.7193 7.49965L85.7946 11.0443L91.9074 24.9865L101.714 46.8451L116.996 76.62L121.453 85.4816L123.873 93.6343L124.764 96.1155H126.292V94.6976L127.566 77.9197L129.858 57.3608L132.15 30.8942L132.915 23.4505L136.608 14.4708L143.994 9.62643L149.725 12.344L154.437 19.0788L153.8 23.4505L150.998 41.6463L145.522 70.1215L141.957 89.2625H143.994L146.414 86.7813L156.093 74.0206L172.266 53.698L179.398 45.6635L187.803 36.802L193.152 32.5484H203.34L210.726 43.6549L207.415 55.1159L196.972 68.3492L188.312 79.5739L175.896 96.2095L168.191 109.585L168.882 110.689L170.738 110.53L198.755 104.504L213.91 101.787L231.994 98.7149L240.144 102.496L241.036 106.395L237.852 114.311L218.495 119.037L195.826 123.645L162.07 131.592L161.696 131.893L162.137 132.547L177.36 133.925L183.855 134.279H199.774L229.447 136.524L237.215 141.605L241.8 147.867L241.036 152.711L229.065 158.737L213.019 154.956L175.45 145.977L162.587 142.787H160.805V143.85L171.502 154.366L191.242 172.089L215.82 195.011L217.094 200.682L213.91 205.172L210.599 204.699L188.949 188.394L180.544 181.069L161.696 165.118H160.422V166.772L164.752 173.152L187.803 207.771L188.949 218.405L187.294 221.832L181.308 223.959L174.813 222.777L161.187 203.754L147.305 182.486L136.098 163.345L134.745 164.2L128.075 235.42L125.019 239.082L117.887 241.8L111.902 237.31L108.718 229.984L111.902 215.452L115.722 196.547L118.779 181.541L121.58 162.873L123.291 156.636L123.14 156.219L121.773 156.449L107.699 175.752L86.304 204.699L69.3663 222.777L65.291 224.431L58.2867 220.768L58.9235 214.27L62.8713 208.48L86.304 178.705L100.44 160.155L109.551 149.507L109.462 147.967L108.959 147.924L46.6977 188.512L35.6182 189.93L30.7788 185.44L31.4156 178.115L33.7079 175.752L52.4285 162.873Z" fill="#D97757"/>
</svg>

After

Width:  |  Height:  |  Size: 2.5 KiB

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,8 @@
Copyright 2023 Anthropic, PBC.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,45 @@
# <img src=".github/logo.svg" alt="" width="32"> Claude SDK for TypeScript
[![NPM version](https://img.shields.io/npm/v/@anthropic-ai/sdk.svg)](https://npmjs.org/package/@anthropic-ai/sdk)
The Claude SDK for TypeScript provides access to the [Claude API](https://docs.anthropic.com/en/api/) from server-side TypeScript or JavaScript applications.
## Documentation
Full documentation is available at **[platform.claude.com/docs/en/api/sdks/typescript](https://platform.claude.com/docs/en/api/sdks/typescript)**.
## Installation
```sh
npm install @anthropic-ai/sdk
```
## Getting started
```js
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env['ANTHROPIC_API_KEY'], // This is the default and can be omitted
});
const message = await client.messages.create({
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello, Claude' }],
model: 'claude-opus-4-6',
});
console.log(message.content);
```
## Requirements
Node.js 18+
## Contributing
See [CONTRIBUTING.md](./CONTRIBUTING.md).
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

View File

@@ -0,0 +1,3 @@
declare const partialParse: (input: string) => unknown;
export { partialParse };
//# sourceMappingURL=parser.d.mts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"parser.d.mts","sourceRoot":"","sources":["../../src/_vendor/partial-json-parser/parser.ts"],"names":[],"mappings":"AAKA,QAAA,MAgQE,YAAY,GAAI,OAAO,MAAM,KAAG,OAAgE,CAAC;AAEnG,OAAO,EAAE,YAAY,EAAE,CAAC"}

View File

@@ -0,0 +1,3 @@
declare const partialParse: (input: string) => unknown;
export { partialParse };
//# sourceMappingURL=parser.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/_vendor/partial-json-parser/parser.ts"],"names":[],"mappings":"AAKA,QAAA,MAgQE,YAAY,GAAI,OAAO,MAAM,KAAG,OAAgE,CAAC;AAEnG,OAAO,EAAE,YAAY,EAAE,CAAC"}

View File

@@ -0,0 +1,226 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.partialParse = void 0;
const tokenize = (input) => {
let current = 0;
let tokens = [];
while (current < input.length) {
let char = input[current];
if (char === '\\') {
current++;
continue;
}
if (char === '{') {
tokens.push({
type: 'brace',
value: '{',
});
current++;
continue;
}
if (char === '}') {
tokens.push({
type: 'brace',
value: '}',
});
current++;
continue;
}
if (char === '[') {
tokens.push({
type: 'paren',
value: '[',
});
current++;
continue;
}
if (char === ']') {
tokens.push({
type: 'paren',
value: ']',
});
current++;
continue;
}
if (char === ':') {
tokens.push({
type: 'separator',
value: ':',
});
current++;
continue;
}
if (char === ',') {
tokens.push({
type: 'delimiter',
value: ',',
});
current++;
continue;
}
if (char === '"') {
let value = '';
let danglingQuote = false;
char = input[++current];
while (char !== '"') {
if (current === input.length) {
danglingQuote = true;
break;
}
if (char === '\\') {
current++;
if (current === input.length) {
danglingQuote = true;
break;
}
value += char + input[current];
char = input[++current];
}
else {
value += char;
char = input[++current];
}
}
char = input[++current];
if (!danglingQuote) {
tokens.push({
type: 'string',
value,
});
}
continue;
}
let WHITESPACE = /\s/;
if (char && WHITESPACE.test(char)) {
current++;
continue;
}
let NUMBERS = /[0-9]/;
if ((char && NUMBERS.test(char)) || char === '-' || char === '.') {
let value = '';
if (char === '-') {
value += char;
char = input[++current];
}
while ((char && NUMBERS.test(char)) || char === '.') {
value += char;
char = input[++current];
}
tokens.push({
type: 'number',
value,
});
continue;
}
let LETTERS = /[a-z]/i;
if (char && LETTERS.test(char)) {
let value = '';
while (char && LETTERS.test(char)) {
if (current === input.length) {
break;
}
value += char;
char = input[++current];
}
if (value == 'true' || value == 'false' || value === 'null') {
tokens.push({
type: 'name',
value,
});
}
else {
// unknown token, e.g. `nul` which isn't quite `null`
current++;
continue;
}
continue;
}
current++;
}
return tokens;
}, strip = (tokens) => {
if (tokens.length === 0) {
return tokens;
}
let lastToken = tokens[tokens.length - 1];
switch (lastToken.type) {
case 'separator':
tokens = tokens.slice(0, tokens.length - 1);
return strip(tokens);
break;
case 'number':
let lastCharacterOfLastToken = lastToken.value[lastToken.value.length - 1];
if (lastCharacterOfLastToken === '.' || lastCharacterOfLastToken === '-') {
tokens = tokens.slice(0, tokens.length - 1);
return strip(tokens);
}
case 'string':
let tokenBeforeTheLastToken = tokens[tokens.length - 2];
if (tokenBeforeTheLastToken?.type === 'delimiter') {
tokens = tokens.slice(0, tokens.length - 1);
return strip(tokens);
}
else if (tokenBeforeTheLastToken?.type === 'brace' && tokenBeforeTheLastToken.value === '{') {
tokens = tokens.slice(0, tokens.length - 1);
return strip(tokens);
}
break;
case 'delimiter':
tokens = tokens.slice(0, tokens.length - 1);
return strip(tokens);
break;
}
return tokens;
}, unstrip = (tokens) => {
let tail = [];
tokens.map((token) => {
if (token.type === 'brace') {
if (token.value === '{') {
tail.push('}');
}
else {
tail.splice(tail.lastIndexOf('}'), 1);
}
}
if (token.type === 'paren') {
if (token.value === '[') {
tail.push(']');
}
else {
tail.splice(tail.lastIndexOf(']'), 1);
}
}
});
if (tail.length > 0) {
tail.reverse().map((item) => {
if (item === '}') {
tokens.push({
type: 'brace',
value: '}',
});
}
else if (item === ']') {
tokens.push({
type: 'paren',
value: ']',
});
}
});
}
return tokens;
}, generate = (tokens) => {
let output = '';
tokens.map((token) => {
switch (token.type) {
case 'string':
output += '"' + token.value + '"';
break;
default:
output += token.value;
break;
}
});
return output;
}, partialParse = (input) => JSON.parse(generate(unstrip(strip(tokenize(input)))));
exports.partialParse = partialParse;
//# sourceMappingURL=parser.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,223 @@
const tokenize = (input) => {
let current = 0;
let tokens = [];
while (current < input.length) {
let char = input[current];
if (char === '\\') {
current++;
continue;
}
if (char === '{') {
tokens.push({
type: 'brace',
value: '{',
});
current++;
continue;
}
if (char === '}') {
tokens.push({
type: 'brace',
value: '}',
});
current++;
continue;
}
if (char === '[') {
tokens.push({
type: 'paren',
value: '[',
});
current++;
continue;
}
if (char === ']') {
tokens.push({
type: 'paren',
value: ']',
});
current++;
continue;
}
if (char === ':') {
tokens.push({
type: 'separator',
value: ':',
});
current++;
continue;
}
if (char === ',') {
tokens.push({
type: 'delimiter',
value: ',',
});
current++;
continue;
}
if (char === '"') {
let value = '';
let danglingQuote = false;
char = input[++current];
while (char !== '"') {
if (current === input.length) {
danglingQuote = true;
break;
}
if (char === '\\') {
current++;
if (current === input.length) {
danglingQuote = true;
break;
}
value += char + input[current];
char = input[++current];
}
else {
value += char;
char = input[++current];
}
}
char = input[++current];
if (!danglingQuote) {
tokens.push({
type: 'string',
value,
});
}
continue;
}
let WHITESPACE = /\s/;
if (char && WHITESPACE.test(char)) {
current++;
continue;
}
let NUMBERS = /[0-9]/;
if ((char && NUMBERS.test(char)) || char === '-' || char === '.') {
let value = '';
if (char === '-') {
value += char;
char = input[++current];
}
while ((char && NUMBERS.test(char)) || char === '.') {
value += char;
char = input[++current];
}
tokens.push({
type: 'number',
value,
});
continue;
}
let LETTERS = /[a-z]/i;
if (char && LETTERS.test(char)) {
let value = '';
while (char && LETTERS.test(char)) {
if (current === input.length) {
break;
}
value += char;
char = input[++current];
}
if (value == 'true' || value == 'false' || value === 'null') {
tokens.push({
type: 'name',
value,
});
}
else {
// unknown token, e.g. `nul` which isn't quite `null`
current++;
continue;
}
continue;
}
current++;
}
return tokens;
}, strip = (tokens) => {
if (tokens.length === 0) {
return tokens;
}
let lastToken = tokens[tokens.length - 1];
switch (lastToken.type) {
case 'separator':
tokens = tokens.slice(0, tokens.length - 1);
return strip(tokens);
break;
case 'number':
let lastCharacterOfLastToken = lastToken.value[lastToken.value.length - 1];
if (lastCharacterOfLastToken === '.' || lastCharacterOfLastToken === '-') {
tokens = tokens.slice(0, tokens.length - 1);
return strip(tokens);
}
case 'string':
let tokenBeforeTheLastToken = tokens[tokens.length - 2];
if (tokenBeforeTheLastToken?.type === 'delimiter') {
tokens = tokens.slice(0, tokens.length - 1);
return strip(tokens);
}
else if (tokenBeforeTheLastToken?.type === 'brace' && tokenBeforeTheLastToken.value === '{') {
tokens = tokens.slice(0, tokens.length - 1);
return strip(tokens);
}
break;
case 'delimiter':
tokens = tokens.slice(0, tokens.length - 1);
return strip(tokens);
break;
}
return tokens;
}, unstrip = (tokens) => {
let tail = [];
tokens.map((token) => {
if (token.type === 'brace') {
if (token.value === '{') {
tail.push('}');
}
else {
tail.splice(tail.lastIndexOf('}'), 1);
}
}
if (token.type === 'paren') {
if (token.value === '[') {
tail.push(']');
}
else {
tail.splice(tail.lastIndexOf(']'), 1);
}
}
});
if (tail.length > 0) {
tail.reverse().map((item) => {
if (item === '}') {
tokens.push({
type: 'brace',
value: '}',
});
}
else if (item === ']') {
tokens.push({
type: 'paren',
value: ']',
});
}
});
}
return tokens;
}, generate = (tokens) => {
let output = '';
tokens.map((token) => {
switch (token.type) {
case 'string':
output += '"' + token.value + '"';
break;
default:
output += token.value;
break;
}
});
return output;
}, partialParse = (input) => JSON.parse(generate(unstrip(strip(tokenize(input)))));
export { partialParse };
//# sourceMappingURL=parser.mjs.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,2 @@
export * from "./core/api-promise.mjs";
//# sourceMappingURL=api-promise.d.mts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"api-promise.d.mts","sourceRoot":"","sources":["src/api-promise.ts"],"names":[],"mappings":""}

View File

@@ -0,0 +1,2 @@
export * from "./core/api-promise.js";
//# sourceMappingURL=api-promise.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"api-promise.d.ts","sourceRoot":"","sources":["src/api-promise.ts"],"names":[],"mappings":""}

View File

@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("./internal/tslib.js");
/** @deprecated Import from ./core/api-promise instead */
tslib_1.__exportStar(require("./core/api-promise.js"), exports);
//# sourceMappingURL=api-promise.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"api-promise.js","sourceRoot":"","sources":["src/api-promise.ts"],"names":[],"mappings":";;;AAAA,yDAAyD;AACzD,gEAAmC"}

View File

@@ -0,0 +1,2 @@
export * from "./core/api-promise.mjs";
//# sourceMappingURL=api-promise.mjs.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"api-promise.mjs","sourceRoot":"","sources":["src/api-promise.ts"],"names":[],"mappings":""}

View File

@@ -0,0 +1,53 @@
#!/usr/bin/env node
const { spawnSync } = require('child_process');
const commands = {
migrate: {
description:
'Run migrations to update your code using @anthropic-ai/sdk@0.41 to be compatible with @anthropic-ai/sdk@0.50',
fn: () => {
const result = spawnSync(
'npx',
[
'-y',
'https://github.com/stainless-api/migrate-ts/releases/download/0.0.2/stainless-api-migrate-0.0.2-6.tgz',
'--migrationConfig',
require.resolve('./migration-config.json'),
...process.argv.slice(3),
],
{ stdio: 'inherit' },
);
if (result.status !== 0) {
process.exit(result.status);
}
},
},
};
function exitWithHelp() {
console.log(`Usage: anthropic-ai-sdk <subcommand>`);
console.log();
console.log('Subcommands:');
for (const [name, info] of Object.entries(commands)) {
console.log(` ${name} ${info.description}`);
}
console.log();
process.exit(1);
}
if (process.argv.length < 3) {
exitWithHelp();
}
const commandName = process.argv[2];
const command = commands[commandName];
if (!command) {
console.log(`Unknown subcommand ${commandName}.`);
exitWithHelp();
}
command.fn();

View File

@@ -0,0 +1,82 @@
{
"pkg": "@anthropic-ai/sdk",
"githubRepo": "https://github.com/anthropics/anthropic-sdk-typescript",
"clientClass": "Anthropic",
"baseClientClass": "BaseAnthropic",
"methods": [
{
"base": "beta.skills.versions",
"name": "retrieve",
"params": [
{
"type": "param",
"key": "version",
"location": "path"
},
{
"type": "params",
"maybeOverload": false
},
{
"type": "options"
}
],
"oldParams": [
{
"type": "param",
"key": "skill_id",
"location": "path"
},
{
"type": "param",
"key": "version",
"location": "path"
},
{
"type": "params",
"maybeOverload": true
},
{
"type": "options"
}
]
},
{
"base": "beta.skills.versions",
"name": "delete",
"params": [
{
"type": "param",
"key": "version",
"location": "path"
},
{
"type": "params",
"maybeOverload": false
},
{
"type": "options"
}
],
"oldParams": [
{
"type": "param",
"key": "skill_id",
"location": "path"
},
{
"type": "param",
"key": "version",
"location": "path"
},
{
"type": "params",
"maybeOverload": true
},
{
"type": "options"
}
]
}
]
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,562 @@
"use strict";
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
var _BaseAnthropic_instances, _a, _BaseAnthropic_encoder, _BaseAnthropic_baseURLOverridden;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Anthropic = exports.BaseAnthropic = exports.AI_PROMPT = exports.HUMAN_PROMPT = void 0;
const tslib_1 = require("./internal/tslib.js");
const uuid_1 = require("./internal/utils/uuid.js");
const values_1 = require("./internal/utils/values.js");
const sleep_1 = require("./internal/utils/sleep.js");
const errors_1 = require("./internal/errors.js");
const detect_platform_1 = require("./internal/detect-platform.js");
const Shims = tslib_1.__importStar(require("./internal/shims.js"));
const Opts = tslib_1.__importStar(require("./internal/request-options.js"));
const query_1 = require("./internal/utils/query.js");
const version_1 = require("./version.js");
const Errors = tslib_1.__importStar(require("./core/error.js"));
const Pagination = tslib_1.__importStar(require("./core/pagination.js"));
const Uploads = tslib_1.__importStar(require("./core/uploads.js"));
const API = tslib_1.__importStar(require("./resources/index.js"));
const api_promise_1 = require("./core/api-promise.js");
const completions_1 = require("./resources/completions.js");
const models_1 = require("./resources/models.js");
const beta_1 = require("./resources/beta/beta.js");
const messages_1 = require("./resources/messages/messages.js");
const detect_platform_2 = require("./internal/detect-platform.js");
const headers_1 = require("./internal/headers.js");
const env_1 = require("./internal/utils/env.js");
const log_1 = require("./internal/utils/log.js");
const values_2 = require("./internal/utils/values.js");
exports.HUMAN_PROMPT = '\\n\\nHuman:';
exports.AI_PROMPT = '\\n\\nAssistant:';
/**
* Base class for Anthropic API clients.
*/
class BaseAnthropic {
/**
* API Client for interfacing with the Anthropic API.
*
* @param {string | null | undefined} [opts.apiKey=process.env['ANTHROPIC_API_KEY'] ?? null]
* @param {string | null | undefined} [opts.authToken=process.env['ANTHROPIC_AUTH_TOKEN'] ?? null]
* @param {string} [opts.baseURL=process.env['ANTHROPIC_BASE_URL'] ?? https://api.anthropic.com] - Override the default base URL for the API.
* @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
* @param {MergedRequestInit} [opts.fetchOptions] - Additional `RequestInit` options to be passed to `fetch` calls.
* @param {Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.
* @param {number} [opts.maxRetries=2] - The maximum number of times the client will retry a request.
* @param {HeadersLike} opts.defaultHeaders - Default headers to include with every request to the API.
* @param {Record<string, string | undefined>} opts.defaultQuery - Default query parameters to include with every request to the API.
* @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.
*/
constructor({ baseURL = (0, env_1.readEnv)('ANTHROPIC_BASE_URL'), apiKey = (0, env_1.readEnv)('ANTHROPIC_API_KEY') ?? null, authToken = (0, env_1.readEnv)('ANTHROPIC_AUTH_TOKEN') ?? null, ...opts } = {}) {
_BaseAnthropic_instances.add(this);
_BaseAnthropic_encoder.set(this, void 0);
const options = {
apiKey,
authToken,
...opts,
baseURL: baseURL || `https://api.anthropic.com`,
};
if (!options.dangerouslyAllowBrowser && (0, detect_platform_2.isRunningInBrowser)()) {
throw new Errors.AnthropicError("It looks like you're running in a browser-like environment.\n\nThis is disabled by default, as it risks exposing your secret API credentials to attackers.\nIf you understand the risks and have appropriate mitigations in place,\nyou can set the `dangerouslyAllowBrowser` option to `true`, e.g.,\n\nnew Anthropic({ apiKey, dangerouslyAllowBrowser: true });\n");
}
this.baseURL = options.baseURL;
this.timeout = options.timeout ?? _a.DEFAULT_TIMEOUT /* 10 minutes */;
this.logger = options.logger ?? console;
const defaultLogLevel = 'warn';
// Set default logLevel early so that we can log a warning in parseLogLevel.
this.logLevel = defaultLogLevel;
this.logLevel =
(0, log_1.parseLogLevel)(options.logLevel, 'ClientOptions.logLevel', this) ??
(0, log_1.parseLogLevel)((0, env_1.readEnv)('ANTHROPIC_LOG'), "process.env['ANTHROPIC_LOG']", this) ??
defaultLogLevel;
this.fetchOptions = options.fetchOptions;
this.maxRetries = options.maxRetries ?? 2;
this.fetch = options.fetch ?? Shims.getDefaultFetch();
tslib_1.__classPrivateFieldSet(this, _BaseAnthropic_encoder, Opts.FallbackEncoder, "f");
this._options = options;
this.apiKey = typeof apiKey === 'string' ? apiKey : null;
this.authToken = authToken;
}
/**
* Create a new client instance re-using the same options given to the current client with optional overriding.
*/
withOptions(options) {
const client = new this.constructor({
...this._options,
baseURL: this.baseURL,
maxRetries: this.maxRetries,
timeout: this.timeout,
logger: this.logger,
logLevel: this.logLevel,
fetch: this.fetch,
fetchOptions: this.fetchOptions,
apiKey: this.apiKey,
authToken: this.authToken,
...options,
});
return client;
}
defaultQuery() {
return this._options.defaultQuery;
}
validateHeaders({ values, nulls }) {
if (values.get('x-api-key') || values.get('authorization')) {
return;
}
if (this.apiKey && values.get('x-api-key')) {
return;
}
if (nulls.has('x-api-key')) {
return;
}
if (this.authToken && values.get('authorization')) {
return;
}
if (nulls.has('authorization')) {
return;
}
throw new Error('Could not resolve authentication method. Expected either apiKey or authToken to be set. Or for one of the "X-Api-Key" or "Authorization" headers to be explicitly omitted');
}
async authHeaders(opts) {
return (0, headers_1.buildHeaders)([await this.apiKeyAuth(opts), await this.bearerAuth(opts)]);
}
async apiKeyAuth(opts) {
if (this.apiKey == null) {
return undefined;
}
return (0, headers_1.buildHeaders)([{ 'X-Api-Key': this.apiKey }]);
}
async bearerAuth(opts) {
if (this.authToken == null) {
return undefined;
}
return (0, headers_1.buildHeaders)([{ Authorization: `Bearer ${this.authToken}` }]);
}
/**
* Basic re-implementation of `qs.stringify` for primitive types.
*/
stringifyQuery(query) {
return (0, query_1.stringifyQuery)(query);
}
getUserAgent() {
return `${this.constructor.name}/JS ${version_1.VERSION}`;
}
defaultIdempotencyKey() {
return `stainless-node-retry-${(0, uuid_1.uuid4)()}`;
}
makeStatusError(status, error, message, headers) {
return Errors.APIError.generate(status, error, message, headers);
}
buildURL(path, query, defaultBaseURL) {
const baseURL = (!tslib_1.__classPrivateFieldGet(this, _BaseAnthropic_instances, "m", _BaseAnthropic_baseURLOverridden).call(this) && defaultBaseURL) || this.baseURL;
const url = (0, values_1.isAbsoluteURL)(path) ?
new URL(path)
: new URL(baseURL + (baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
const defaultQuery = this.defaultQuery();
const pathQuery = Object.fromEntries(url.searchParams);
if (!(0, values_2.isEmptyObj)(defaultQuery) || !(0, values_2.isEmptyObj)(pathQuery)) {
query = { ...pathQuery, ...defaultQuery, ...query };
}
if (typeof query === 'object' && query && !Array.isArray(query)) {
url.search = this.stringifyQuery(query);
}
return url.toString();
}
_calculateNonstreamingTimeout(maxTokens) {
const defaultTimeout = 10 * 60;
const expectedTimeout = (60 * 60 * maxTokens) / 128000;
if (expectedTimeout > defaultTimeout) {
throw new Errors.AnthropicError('Streaming is required for operations that may take longer than 10 minutes. ' +
'See https://github.com/anthropics/anthropic-sdk-typescript#streaming-responses for more details');
}
return defaultTimeout * 1000;
}
/**
* Used as a callback for mutating the given `FinalRequestOptions` object.
*/
async prepareOptions(options) { }
/**
* Used as a callback for mutating the given `RequestInit` object.
*
* This is useful for cases where you want to add certain headers based off of
* the request properties, e.g. `method` or `url`.
*/
async prepareRequest(request, { url, options }) { }
get(path, opts) {
return this.methodRequest('get', path, opts);
}
post(path, opts) {
return this.methodRequest('post', path, opts);
}
patch(path, opts) {
return this.methodRequest('patch', path, opts);
}
put(path, opts) {
return this.methodRequest('put', path, opts);
}
delete(path, opts) {
return this.methodRequest('delete', path, opts);
}
methodRequest(method, path, opts) {
return this.request(Promise.resolve(opts).then((opts) => {
return { method, path, ...opts };
}));
}
request(options, remainingRetries = null) {
return new api_promise_1.APIPromise(this, this.makeRequest(options, remainingRetries, undefined));
}
async makeRequest(optionsInput, retriesRemaining, retryOfRequestLogID) {
const options = await optionsInput;
const maxRetries = options.maxRetries ?? this.maxRetries;
if (retriesRemaining == null) {
retriesRemaining = maxRetries;
}
await this.prepareOptions(options);
const { req, url, timeout } = await this.buildRequest(options, {
retryCount: maxRetries - retriesRemaining,
});
await this.prepareRequest(req, { url, options });
/** Not an API request ID, just for correlating local log entries. */
const requestLogID = 'log_' + ((Math.random() * (1 << 24)) | 0).toString(16).padStart(6, '0');
const retryLogStr = retryOfRequestLogID === undefined ? '' : `, retryOf: ${retryOfRequestLogID}`;
const startTime = Date.now();
(0, log_1.loggerFor)(this).debug(`[${requestLogID}] sending request`, (0, log_1.formatRequestDetails)({
retryOfRequestLogID,
method: options.method,
url,
options,
headers: req.headers,
}));
if (options.signal?.aborted) {
throw new Errors.APIUserAbortError();
}
const controller = new AbortController();
const response = await this.fetchWithTimeout(url, req, timeout, controller).catch(errors_1.castToError);
const headersTime = Date.now();
if (response instanceof globalThis.Error) {
const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;
if (options.signal?.aborted) {
throw new Errors.APIUserAbortError();
}
// detect native connection timeout errors
// deno throws "TypeError: error sending request for url (https://example/): client error (Connect): tcp connect error: Operation timed out (os error 60): Operation timed out (os error 60)"
// undici throws "TypeError: fetch failed" with cause "ConnectTimeoutError: Connect Timeout Error (attempted address: example:443, timeout: 1ms)"
// others do not provide enough information to distinguish timeouts from other connection errors
const isTimeout = (0, errors_1.isAbortError)(response) ||
/timed? ?out/i.test(String(response) + ('cause' in response ? String(response.cause) : ''));
if (retriesRemaining) {
(0, log_1.loggerFor)(this).info(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} - ${retryMessage}`);
(0, log_1.loggerFor)(this).debug(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} (${retryMessage})`, (0, log_1.formatRequestDetails)({
retryOfRequestLogID,
url,
durationMs: headersTime - startTime,
message: response.message,
}));
return this.retryRequest(options, retriesRemaining, retryOfRequestLogID ?? requestLogID);
}
(0, log_1.loggerFor)(this).info(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} - error; no more retries left`);
(0, log_1.loggerFor)(this).debug(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} (error; no more retries left)`, (0, log_1.formatRequestDetails)({
retryOfRequestLogID,
url,
durationMs: headersTime - startTime,
message: response.message,
}));
if (isTimeout) {
throw new Errors.APIConnectionTimeoutError();
}
throw new Errors.APIConnectionError({ cause: response });
}
const specialHeaders = [...response.headers.entries()]
.filter(([name]) => name === 'request-id')
.map(([name, value]) => ', ' + name + ': ' + JSON.stringify(value))
.join('');
const responseInfo = `[${requestLogID}${retryLogStr}${specialHeaders}] ${req.method} ${url} ${response.ok ? 'succeeded' : 'failed'} with status ${response.status} in ${headersTime - startTime}ms`;
if (!response.ok) {
const shouldRetry = await this.shouldRetry(response);
if (retriesRemaining && shouldRetry) {
const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;
// We don't need the body of this response.
await Shims.CancelReadableStream(response.body);
(0, log_1.loggerFor)(this).info(`${responseInfo} - ${retryMessage}`);
(0, log_1.loggerFor)(this).debug(`[${requestLogID}] response error (${retryMessage})`, (0, log_1.formatRequestDetails)({
retryOfRequestLogID,
url: response.url,
status: response.status,
headers: response.headers,
durationMs: headersTime - startTime,
}));
return this.retryRequest(options, retriesRemaining, retryOfRequestLogID ?? requestLogID, response.headers);
}
const retryMessage = shouldRetry ? `error; no more retries left` : `error; not retryable`;
(0, log_1.loggerFor)(this).info(`${responseInfo} - ${retryMessage}`);
const errText = await response.text().catch((err) => (0, errors_1.castToError)(err).message);
const errJSON = (0, values_1.safeJSON)(errText);
const errMessage = errJSON ? undefined : errText;
(0, log_1.loggerFor)(this).debug(`[${requestLogID}] response error (${retryMessage})`, (0, log_1.formatRequestDetails)({
retryOfRequestLogID,
url: response.url,
status: response.status,
headers: response.headers,
message: errMessage,
durationMs: Date.now() - startTime,
}));
const err = this.makeStatusError(response.status, errJSON, errMessage, response.headers);
throw err;
}
(0, log_1.loggerFor)(this).info(responseInfo);
(0, log_1.loggerFor)(this).debug(`[${requestLogID}] response start`, (0, log_1.formatRequestDetails)({
retryOfRequestLogID,
url: response.url,
status: response.status,
headers: response.headers,
durationMs: headersTime - startTime,
}));
return { response, options, controller, requestLogID, retryOfRequestLogID, startTime };
}
getAPIList(path, Page, opts) {
return this.requestAPIList(Page, opts && 'then' in opts ?
opts.then((opts) => ({ method: 'get', path, ...opts }))
: { method: 'get', path, ...opts });
}
requestAPIList(Page, options) {
const request = this.makeRequest(options, null, undefined);
return new Pagination.PagePromise(this, request, Page);
}
async fetchWithTimeout(url, init, ms, controller) {
const { signal, method, ...options } = init || {};
// Avoid creating a closure over `this`, `init`, or `options` to prevent memory leaks.
// An arrow function like `() => controller.abort()` captures the surrounding scope,
// which includes the request body and other large objects. When the user passes a
// long-lived AbortSignal, the listener prevents those objects from being GC'd for
// the lifetime of the signal. Using `.bind()` only retains a reference to the
// controller itself.
const abort = this._makeAbort(controller);
if (signal)
signal.addEventListener('abort', abort, { once: true });
const timeout = setTimeout(abort, ms);
const isReadableBody = (globalThis.ReadableStream && options.body instanceof globalThis.ReadableStream) ||
(typeof options.body === 'object' && options.body !== null && Symbol.asyncIterator in options.body);
const fetchOptions = {
signal: controller.signal,
...(isReadableBody ? { duplex: 'half' } : {}),
method: 'GET',
...options,
};
if (method) {
// Custom methods like 'patch' need to be uppercased
// See https://github.com/nodejs/undici/issues/2294
fetchOptions.method = method.toUpperCase();
}
try {
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
return await this.fetch.call(undefined, url, fetchOptions);
}
finally {
clearTimeout(timeout);
}
}
async shouldRetry(response) {
// Note this is not a standard header.
const shouldRetryHeader = response.headers.get('x-should-retry');
// If the server explicitly says whether or not to retry, obey.
if (shouldRetryHeader === 'true')
return true;
if (shouldRetryHeader === 'false')
return false;
// Retry on request timeouts.
if (response.status === 408)
return true;
// Retry on lock timeouts.
if (response.status === 409)
return true;
// Retry on rate limits.
if (response.status === 429)
return true;
// Retry internal errors.
if (response.status >= 500)
return true;
return false;
}
async retryRequest(options, retriesRemaining, requestLogID, responseHeaders) {
let timeoutMillis;
// Note the `retry-after-ms` header may not be standard, but is a good idea and we'd like proactive support for it.
const retryAfterMillisHeader = responseHeaders?.get('retry-after-ms');
if (retryAfterMillisHeader) {
const timeoutMs = parseFloat(retryAfterMillisHeader);
if (!Number.isNaN(timeoutMs)) {
timeoutMillis = timeoutMs;
}
}
// About the Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
const retryAfterHeader = responseHeaders?.get('retry-after');
if (retryAfterHeader && !timeoutMillis) {
const timeoutSeconds = parseFloat(retryAfterHeader);
if (!Number.isNaN(timeoutSeconds)) {
timeoutMillis = timeoutSeconds * 1000;
}
else {
timeoutMillis = Date.parse(retryAfterHeader) - Date.now();
}
}
// If the API asks us to wait a certain amount of time, just do what it
// says, but otherwise calculate a default
if (timeoutMillis === undefined) {
const maxRetries = options.maxRetries ?? this.maxRetries;
timeoutMillis = this.calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries);
}
await (0, sleep_1.sleep)(timeoutMillis);
return this.makeRequest(options, retriesRemaining - 1, requestLogID);
}
calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries) {
const initialRetryDelay = 0.5;
const maxRetryDelay = 8.0;
const numRetries = maxRetries - retriesRemaining;
// Apply exponential backoff, but not more than the max.
const sleepSeconds = Math.min(initialRetryDelay * Math.pow(2, numRetries), maxRetryDelay);
// Apply some jitter, take up to at most 25 percent of the retry time.
const jitter = 1 - Math.random() * 0.25;
return sleepSeconds * jitter * 1000;
}
calculateNonstreamingTimeout(maxTokens, maxNonstreamingTokens) {
const maxTime = 60 * 60 * 1000; // 60 minutes
const defaultTime = 60 * 10 * 1000; // 10 minutes
const expectedTime = (maxTime * maxTokens) / 128000;
if (expectedTime > defaultTime || (maxNonstreamingTokens != null && maxTokens > maxNonstreamingTokens)) {
throw new Errors.AnthropicError('Streaming is required for operations that may take longer than 10 minutes. See https://github.com/anthropics/anthropic-sdk-typescript#long-requests for more details');
}
return defaultTime;
}
async buildRequest(inputOptions, { retryCount = 0 } = {}) {
const options = { ...inputOptions };
const { method, path, query, defaultBaseURL } = options;
const url = this.buildURL(path, query, defaultBaseURL);
if ('timeout' in options)
(0, values_1.validatePositiveInteger)('timeout', options.timeout);
options.timeout = options.timeout ?? this.timeout;
const { bodyHeaders, body } = this.buildBody({ options });
const reqHeaders = await this.buildHeaders({ options: inputOptions, method, bodyHeaders, retryCount });
const req = {
method,
headers: reqHeaders,
...(options.signal && { signal: options.signal }),
...(globalThis.ReadableStream &&
body instanceof globalThis.ReadableStream && { duplex: 'half' }),
...(body && { body }),
...(this.fetchOptions ?? {}),
...(options.fetchOptions ?? {}),
};
return { req, url, timeout: options.timeout };
}
async buildHeaders({ options, method, bodyHeaders, retryCount, }) {
let idempotencyHeaders = {};
if (this.idempotencyHeader && method !== 'get') {
if (!options.idempotencyKey)
options.idempotencyKey = this.defaultIdempotencyKey();
idempotencyHeaders[this.idempotencyHeader] = options.idempotencyKey;
}
const headers = (0, headers_1.buildHeaders)([
idempotencyHeaders,
{
Accept: 'application/json',
'User-Agent': this.getUserAgent(),
'X-Stainless-Retry-Count': String(retryCount),
...(options.timeout ? { 'X-Stainless-Timeout': String(Math.trunc(options.timeout / 1000)) } : {}),
...(0, detect_platform_1.getPlatformHeaders)(),
...(this._options.dangerouslyAllowBrowser ?
{ 'anthropic-dangerous-direct-browser-access': 'true' }
: undefined),
'anthropic-version': '2023-06-01',
},
await this.authHeaders(options),
this._options.defaultHeaders,
bodyHeaders,
options.headers,
]);
this.validateHeaders(headers);
return headers.values;
}
_makeAbort(controller) {
// note: we can't just inline this method inside `fetchWithTimeout()` because then the closure
// would capture all request options, and cause a memory leak.
return () => controller.abort();
}
buildBody({ options: { body, headers: rawHeaders } }) {
if (!body) {
return { bodyHeaders: undefined, body: undefined };
}
const headers = (0, headers_1.buildHeaders)([rawHeaders]);
if (
// Pass raw type verbatim
ArrayBuffer.isView(body) ||
body instanceof ArrayBuffer ||
body instanceof DataView ||
(typeof body === 'string' &&
// Preserve legacy string encoding behavior for now
headers.values.has('content-type')) ||
// `Blob` is superset of `File`
(globalThis.Blob && body instanceof globalThis.Blob) ||
// `FormData` -> `multipart/form-data`
body instanceof FormData ||
// `URLSearchParams` -> `application/x-www-form-urlencoded`
body instanceof URLSearchParams ||
// Send chunked stream (each chunk has own `length`)
(globalThis.ReadableStream && body instanceof globalThis.ReadableStream)) {
return { bodyHeaders: undefined, body: body };
}
else if (typeof body === 'object' &&
(Symbol.asyncIterator in body ||
(Symbol.iterator in body && 'next' in body && typeof body.next === 'function'))) {
return { bodyHeaders: undefined, body: Shims.ReadableStreamFrom(body) };
}
else if (typeof body === 'object' &&
headers.values.get('content-type') === 'application/x-www-form-urlencoded') {
return {
bodyHeaders: { 'content-type': 'application/x-www-form-urlencoded' },
body: this.stringifyQuery(body),
};
}
else {
return tslib_1.__classPrivateFieldGet(this, _BaseAnthropic_encoder, "f").call(this, { body, headers });
}
}
}
exports.BaseAnthropic = BaseAnthropic;
_a = BaseAnthropic, _BaseAnthropic_encoder = new WeakMap(), _BaseAnthropic_instances = new WeakSet(), _BaseAnthropic_baseURLOverridden = function _BaseAnthropic_baseURLOverridden() {
return this.baseURL !== 'https://api.anthropic.com';
};
BaseAnthropic.Anthropic = _a;
BaseAnthropic.HUMAN_PROMPT = exports.HUMAN_PROMPT;
BaseAnthropic.AI_PROMPT = exports.AI_PROMPT;
BaseAnthropic.DEFAULT_TIMEOUT = 600000; // 10 minutes
BaseAnthropic.AnthropicError = Errors.AnthropicError;
BaseAnthropic.APIError = Errors.APIError;
BaseAnthropic.APIConnectionError = Errors.APIConnectionError;
BaseAnthropic.APIConnectionTimeoutError = Errors.APIConnectionTimeoutError;
BaseAnthropic.APIUserAbortError = Errors.APIUserAbortError;
BaseAnthropic.NotFoundError = Errors.NotFoundError;
BaseAnthropic.ConflictError = Errors.ConflictError;
BaseAnthropic.RateLimitError = Errors.RateLimitError;
BaseAnthropic.BadRequestError = Errors.BadRequestError;
BaseAnthropic.AuthenticationError = Errors.AuthenticationError;
BaseAnthropic.InternalServerError = Errors.InternalServerError;
BaseAnthropic.PermissionDeniedError = Errors.PermissionDeniedError;
BaseAnthropic.UnprocessableEntityError = Errors.UnprocessableEntityError;
BaseAnthropic.toFile = Uploads.toFile;
/**
* API Client for interfacing with the Anthropic API.
*/
class Anthropic extends BaseAnthropic {
constructor() {
super(...arguments);
this.completions = new API.Completions(this);
this.messages = new API.Messages(this);
this.models = new API.Models(this);
this.beta = new API.Beta(this);
}
}
exports.Anthropic = Anthropic;
Anthropic.Completions = completions_1.Completions;
Anthropic.Messages = messages_1.Messages;
Anthropic.Models = models_1.Models;
Anthropic.Beta = beta_1.Beta;
//# sourceMappingURL=client.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,557 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
var _BaseAnthropic_instances, _a, _BaseAnthropic_encoder, _BaseAnthropic_baseURLOverridden;
import { __classPrivateFieldGet, __classPrivateFieldSet } from "./internal/tslib.mjs";
import { uuid4 } from "./internal/utils/uuid.mjs";
import { validatePositiveInteger, isAbsoluteURL, safeJSON } from "./internal/utils/values.mjs";
import { sleep } from "./internal/utils/sleep.mjs";
import { castToError, isAbortError } from "./internal/errors.mjs";
import { getPlatformHeaders } from "./internal/detect-platform.mjs";
import * as Shims from "./internal/shims.mjs";
import * as Opts from "./internal/request-options.mjs";
import { stringifyQuery } from "./internal/utils/query.mjs";
import { VERSION } from "./version.mjs";
import * as Errors from "./core/error.mjs";
import * as Pagination from "./core/pagination.mjs";
import * as Uploads from "./core/uploads.mjs";
import * as API from "./resources/index.mjs";
import { APIPromise } from "./core/api-promise.mjs";
import { Completions, } from "./resources/completions.mjs";
import { Models, } from "./resources/models.mjs";
import { Beta, } from "./resources/beta/beta.mjs";
import { Messages, } from "./resources/messages/messages.mjs";
import { isRunningInBrowser } from "./internal/detect-platform.mjs";
import { buildHeaders } from "./internal/headers.mjs";
import { readEnv } from "./internal/utils/env.mjs";
import { formatRequestDetails, loggerFor, parseLogLevel, } from "./internal/utils/log.mjs";
import { isEmptyObj } from "./internal/utils/values.mjs";
export const HUMAN_PROMPT = '\\n\\nHuman:';
export const AI_PROMPT = '\\n\\nAssistant:';
/**
* Base class for Anthropic API clients.
*/
export class BaseAnthropic {
/**
* API Client for interfacing with the Anthropic API.
*
* @param {string | null | undefined} [opts.apiKey=process.env['ANTHROPIC_API_KEY'] ?? null]
* @param {string | null | undefined} [opts.authToken=process.env['ANTHROPIC_AUTH_TOKEN'] ?? null]
* @param {string} [opts.baseURL=process.env['ANTHROPIC_BASE_URL'] ?? https://api.anthropic.com] - Override the default base URL for the API.
* @param {number} [opts.timeout=10 minutes] - The maximum amount of time (in milliseconds) the client will wait for a response before timing out.
* @param {MergedRequestInit} [opts.fetchOptions] - Additional `RequestInit` options to be passed to `fetch` calls.
* @param {Fetch} [opts.fetch] - Specify a custom `fetch` function implementation.
* @param {number} [opts.maxRetries=2] - The maximum number of times the client will retry a request.
* @param {HeadersLike} opts.defaultHeaders - Default headers to include with every request to the API.
* @param {Record<string, string | undefined>} opts.defaultQuery - Default query parameters to include with every request to the API.
* @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.
*/
constructor({ baseURL = readEnv('ANTHROPIC_BASE_URL'), apiKey = readEnv('ANTHROPIC_API_KEY') ?? null, authToken = readEnv('ANTHROPIC_AUTH_TOKEN') ?? null, ...opts } = {}) {
_BaseAnthropic_instances.add(this);
_BaseAnthropic_encoder.set(this, void 0);
const options = {
apiKey,
authToken,
...opts,
baseURL: baseURL || `https://api.anthropic.com`,
};
if (!options.dangerouslyAllowBrowser && isRunningInBrowser()) {
throw new Errors.AnthropicError("It looks like you're running in a browser-like environment.\n\nThis is disabled by default, as it risks exposing your secret API credentials to attackers.\nIf you understand the risks and have appropriate mitigations in place,\nyou can set the `dangerouslyAllowBrowser` option to `true`, e.g.,\n\nnew Anthropic({ apiKey, dangerouslyAllowBrowser: true });\n");
}
this.baseURL = options.baseURL;
this.timeout = options.timeout ?? _a.DEFAULT_TIMEOUT /* 10 minutes */;
this.logger = options.logger ?? console;
const defaultLogLevel = 'warn';
// Set default logLevel early so that we can log a warning in parseLogLevel.
this.logLevel = defaultLogLevel;
this.logLevel =
parseLogLevel(options.logLevel, 'ClientOptions.logLevel', this) ??
parseLogLevel(readEnv('ANTHROPIC_LOG'), "process.env['ANTHROPIC_LOG']", this) ??
defaultLogLevel;
this.fetchOptions = options.fetchOptions;
this.maxRetries = options.maxRetries ?? 2;
this.fetch = options.fetch ?? Shims.getDefaultFetch();
__classPrivateFieldSet(this, _BaseAnthropic_encoder, Opts.FallbackEncoder, "f");
this._options = options;
this.apiKey = typeof apiKey === 'string' ? apiKey : null;
this.authToken = authToken;
}
/**
* Create a new client instance re-using the same options given to the current client with optional overriding.
*/
withOptions(options) {
const client = new this.constructor({
...this._options,
baseURL: this.baseURL,
maxRetries: this.maxRetries,
timeout: this.timeout,
logger: this.logger,
logLevel: this.logLevel,
fetch: this.fetch,
fetchOptions: this.fetchOptions,
apiKey: this.apiKey,
authToken: this.authToken,
...options,
});
return client;
}
defaultQuery() {
return this._options.defaultQuery;
}
validateHeaders({ values, nulls }) {
if (values.get('x-api-key') || values.get('authorization')) {
return;
}
if (this.apiKey && values.get('x-api-key')) {
return;
}
if (nulls.has('x-api-key')) {
return;
}
if (this.authToken && values.get('authorization')) {
return;
}
if (nulls.has('authorization')) {
return;
}
throw new Error('Could not resolve authentication method. Expected either apiKey or authToken to be set. Or for one of the "X-Api-Key" or "Authorization" headers to be explicitly omitted');
}
async authHeaders(opts) {
return buildHeaders([await this.apiKeyAuth(opts), await this.bearerAuth(opts)]);
}
async apiKeyAuth(opts) {
if (this.apiKey == null) {
return undefined;
}
return buildHeaders([{ 'X-Api-Key': this.apiKey }]);
}
async bearerAuth(opts) {
if (this.authToken == null) {
return undefined;
}
return buildHeaders([{ Authorization: `Bearer ${this.authToken}` }]);
}
/**
* Basic re-implementation of `qs.stringify` for primitive types.
*/
stringifyQuery(query) {
return stringifyQuery(query);
}
getUserAgent() {
return `${this.constructor.name}/JS ${VERSION}`;
}
defaultIdempotencyKey() {
return `stainless-node-retry-${uuid4()}`;
}
makeStatusError(status, error, message, headers) {
return Errors.APIError.generate(status, error, message, headers);
}
buildURL(path, query, defaultBaseURL) {
const baseURL = (!__classPrivateFieldGet(this, _BaseAnthropic_instances, "m", _BaseAnthropic_baseURLOverridden).call(this) && defaultBaseURL) || this.baseURL;
const url = isAbsoluteURL(path) ?
new URL(path)
: new URL(baseURL + (baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path));
const defaultQuery = this.defaultQuery();
const pathQuery = Object.fromEntries(url.searchParams);
if (!isEmptyObj(defaultQuery) || !isEmptyObj(pathQuery)) {
query = { ...pathQuery, ...defaultQuery, ...query };
}
if (typeof query === 'object' && query && !Array.isArray(query)) {
url.search = this.stringifyQuery(query);
}
return url.toString();
}
_calculateNonstreamingTimeout(maxTokens) {
const defaultTimeout = 10 * 60;
const expectedTimeout = (60 * 60 * maxTokens) / 128000;
if (expectedTimeout > defaultTimeout) {
throw new Errors.AnthropicError('Streaming is required for operations that may take longer than 10 minutes. ' +
'See https://github.com/anthropics/anthropic-sdk-typescript#streaming-responses for more details');
}
return defaultTimeout * 1000;
}
/**
* Used as a callback for mutating the given `FinalRequestOptions` object.
*/
async prepareOptions(options) { }
/**
* Used as a callback for mutating the given `RequestInit` object.
*
* This is useful for cases where you want to add certain headers based off of
* the request properties, e.g. `method` or `url`.
*/
async prepareRequest(request, { url, options }) { }
get(path, opts) {
return this.methodRequest('get', path, opts);
}
post(path, opts) {
return this.methodRequest('post', path, opts);
}
patch(path, opts) {
return this.methodRequest('patch', path, opts);
}
put(path, opts) {
return this.methodRequest('put', path, opts);
}
delete(path, opts) {
return this.methodRequest('delete', path, opts);
}
methodRequest(method, path, opts) {
return this.request(Promise.resolve(opts).then((opts) => {
return { method, path, ...opts };
}));
}
request(options, remainingRetries = null) {
return new APIPromise(this, this.makeRequest(options, remainingRetries, undefined));
}
async makeRequest(optionsInput, retriesRemaining, retryOfRequestLogID) {
const options = await optionsInput;
const maxRetries = options.maxRetries ?? this.maxRetries;
if (retriesRemaining == null) {
retriesRemaining = maxRetries;
}
await this.prepareOptions(options);
const { req, url, timeout } = await this.buildRequest(options, {
retryCount: maxRetries - retriesRemaining,
});
await this.prepareRequest(req, { url, options });
/** Not an API request ID, just for correlating local log entries. */
const requestLogID = 'log_' + ((Math.random() * (1 << 24)) | 0).toString(16).padStart(6, '0');
const retryLogStr = retryOfRequestLogID === undefined ? '' : `, retryOf: ${retryOfRequestLogID}`;
const startTime = Date.now();
loggerFor(this).debug(`[${requestLogID}] sending request`, formatRequestDetails({
retryOfRequestLogID,
method: options.method,
url,
options,
headers: req.headers,
}));
if (options.signal?.aborted) {
throw new Errors.APIUserAbortError();
}
const controller = new AbortController();
const response = await this.fetchWithTimeout(url, req, timeout, controller).catch(castToError);
const headersTime = Date.now();
if (response instanceof globalThis.Error) {
const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;
if (options.signal?.aborted) {
throw new Errors.APIUserAbortError();
}
// detect native connection timeout errors
// deno throws "TypeError: error sending request for url (https://example/): client error (Connect): tcp connect error: Operation timed out (os error 60): Operation timed out (os error 60)"
// undici throws "TypeError: fetch failed" with cause "ConnectTimeoutError: Connect Timeout Error (attempted address: example:443, timeout: 1ms)"
// others do not provide enough information to distinguish timeouts from other connection errors
const isTimeout = isAbortError(response) ||
/timed? ?out/i.test(String(response) + ('cause' in response ? String(response.cause) : ''));
if (retriesRemaining) {
loggerFor(this).info(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} - ${retryMessage}`);
loggerFor(this).debug(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} (${retryMessage})`, formatRequestDetails({
retryOfRequestLogID,
url,
durationMs: headersTime - startTime,
message: response.message,
}));
return this.retryRequest(options, retriesRemaining, retryOfRequestLogID ?? requestLogID);
}
loggerFor(this).info(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} - error; no more retries left`);
loggerFor(this).debug(`[${requestLogID}] connection ${isTimeout ? 'timed out' : 'failed'} (error; no more retries left)`, formatRequestDetails({
retryOfRequestLogID,
url,
durationMs: headersTime - startTime,
message: response.message,
}));
if (isTimeout) {
throw new Errors.APIConnectionTimeoutError();
}
throw new Errors.APIConnectionError({ cause: response });
}
const specialHeaders = [...response.headers.entries()]
.filter(([name]) => name === 'request-id')
.map(([name, value]) => ', ' + name + ': ' + JSON.stringify(value))
.join('');
const responseInfo = `[${requestLogID}${retryLogStr}${specialHeaders}] ${req.method} ${url} ${response.ok ? 'succeeded' : 'failed'} with status ${response.status} in ${headersTime - startTime}ms`;
if (!response.ok) {
const shouldRetry = await this.shouldRetry(response);
if (retriesRemaining && shouldRetry) {
const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;
// We don't need the body of this response.
await Shims.CancelReadableStream(response.body);
loggerFor(this).info(`${responseInfo} - ${retryMessage}`);
loggerFor(this).debug(`[${requestLogID}] response error (${retryMessage})`, formatRequestDetails({
retryOfRequestLogID,
url: response.url,
status: response.status,
headers: response.headers,
durationMs: headersTime - startTime,
}));
return this.retryRequest(options, retriesRemaining, retryOfRequestLogID ?? requestLogID, response.headers);
}
const retryMessage = shouldRetry ? `error; no more retries left` : `error; not retryable`;
loggerFor(this).info(`${responseInfo} - ${retryMessage}`);
const errText = await response.text().catch((err) => castToError(err).message);
const errJSON = safeJSON(errText);
const errMessage = errJSON ? undefined : errText;
loggerFor(this).debug(`[${requestLogID}] response error (${retryMessage})`, formatRequestDetails({
retryOfRequestLogID,
url: response.url,
status: response.status,
headers: response.headers,
message: errMessage,
durationMs: Date.now() - startTime,
}));
const err = this.makeStatusError(response.status, errJSON, errMessage, response.headers);
throw err;
}
loggerFor(this).info(responseInfo);
loggerFor(this).debug(`[${requestLogID}] response start`, formatRequestDetails({
retryOfRequestLogID,
url: response.url,
status: response.status,
headers: response.headers,
durationMs: headersTime - startTime,
}));
return { response, options, controller, requestLogID, retryOfRequestLogID, startTime };
}
getAPIList(path, Page, opts) {
return this.requestAPIList(Page, opts && 'then' in opts ?
opts.then((opts) => ({ method: 'get', path, ...opts }))
: { method: 'get', path, ...opts });
}
requestAPIList(Page, options) {
const request = this.makeRequest(options, null, undefined);
return new Pagination.PagePromise(this, request, Page);
}
async fetchWithTimeout(url, init, ms, controller) {
const { signal, method, ...options } = init || {};
// Avoid creating a closure over `this`, `init`, or `options` to prevent memory leaks.
// An arrow function like `() => controller.abort()` captures the surrounding scope,
// which includes the request body and other large objects. When the user passes a
// long-lived AbortSignal, the listener prevents those objects from being GC'd for
// the lifetime of the signal. Using `.bind()` only retains a reference to the
// controller itself.
const abort = this._makeAbort(controller);
if (signal)
signal.addEventListener('abort', abort, { once: true });
const timeout = setTimeout(abort, ms);
const isReadableBody = (globalThis.ReadableStream && options.body instanceof globalThis.ReadableStream) ||
(typeof options.body === 'object' && options.body !== null && Symbol.asyncIterator in options.body);
const fetchOptions = {
signal: controller.signal,
...(isReadableBody ? { duplex: 'half' } : {}),
method: 'GET',
...options,
};
if (method) {
// Custom methods like 'patch' need to be uppercased
// See https://github.com/nodejs/undici/issues/2294
fetchOptions.method = method.toUpperCase();
}
try {
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
return await this.fetch.call(undefined, url, fetchOptions);
}
finally {
clearTimeout(timeout);
}
}
async shouldRetry(response) {
// Note this is not a standard header.
const shouldRetryHeader = response.headers.get('x-should-retry');
// If the server explicitly says whether or not to retry, obey.
if (shouldRetryHeader === 'true')
return true;
if (shouldRetryHeader === 'false')
return false;
// Retry on request timeouts.
if (response.status === 408)
return true;
// Retry on lock timeouts.
if (response.status === 409)
return true;
// Retry on rate limits.
if (response.status === 429)
return true;
// Retry internal errors.
if (response.status >= 500)
return true;
return false;
}
async retryRequest(options, retriesRemaining, requestLogID, responseHeaders) {
let timeoutMillis;
// Note the `retry-after-ms` header may not be standard, but is a good idea and we'd like proactive support for it.
const retryAfterMillisHeader = responseHeaders?.get('retry-after-ms');
if (retryAfterMillisHeader) {
const timeoutMs = parseFloat(retryAfterMillisHeader);
if (!Number.isNaN(timeoutMs)) {
timeoutMillis = timeoutMs;
}
}
// About the Retry-After header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
const retryAfterHeader = responseHeaders?.get('retry-after');
if (retryAfterHeader && !timeoutMillis) {
const timeoutSeconds = parseFloat(retryAfterHeader);
if (!Number.isNaN(timeoutSeconds)) {
timeoutMillis = timeoutSeconds * 1000;
}
else {
timeoutMillis = Date.parse(retryAfterHeader) - Date.now();
}
}
// If the API asks us to wait a certain amount of time, just do what it
// says, but otherwise calculate a default
if (timeoutMillis === undefined) {
const maxRetries = options.maxRetries ?? this.maxRetries;
timeoutMillis = this.calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries);
}
await sleep(timeoutMillis);
return this.makeRequest(options, retriesRemaining - 1, requestLogID);
}
calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries) {
const initialRetryDelay = 0.5;
const maxRetryDelay = 8.0;
const numRetries = maxRetries - retriesRemaining;
// Apply exponential backoff, but not more than the max.
const sleepSeconds = Math.min(initialRetryDelay * Math.pow(2, numRetries), maxRetryDelay);
// Apply some jitter, take up to at most 25 percent of the retry time.
const jitter = 1 - Math.random() * 0.25;
return sleepSeconds * jitter * 1000;
}
calculateNonstreamingTimeout(maxTokens, maxNonstreamingTokens) {
const maxTime = 60 * 60 * 1000; // 60 minutes
const defaultTime = 60 * 10 * 1000; // 10 minutes
const expectedTime = (maxTime * maxTokens) / 128000;
if (expectedTime > defaultTime || (maxNonstreamingTokens != null && maxTokens > maxNonstreamingTokens)) {
throw new Errors.AnthropicError('Streaming is required for operations that may take longer than 10 minutes. See https://github.com/anthropics/anthropic-sdk-typescript#long-requests for more details');
}
return defaultTime;
}
async buildRequest(inputOptions, { retryCount = 0 } = {}) {
const options = { ...inputOptions };
const { method, path, query, defaultBaseURL } = options;
const url = this.buildURL(path, query, defaultBaseURL);
if ('timeout' in options)
validatePositiveInteger('timeout', options.timeout);
options.timeout = options.timeout ?? this.timeout;
const { bodyHeaders, body } = this.buildBody({ options });
const reqHeaders = await this.buildHeaders({ options: inputOptions, method, bodyHeaders, retryCount });
const req = {
method,
headers: reqHeaders,
...(options.signal && { signal: options.signal }),
...(globalThis.ReadableStream &&
body instanceof globalThis.ReadableStream && { duplex: 'half' }),
...(body && { body }),
...(this.fetchOptions ?? {}),
...(options.fetchOptions ?? {}),
};
return { req, url, timeout: options.timeout };
}
async buildHeaders({ options, method, bodyHeaders, retryCount, }) {
let idempotencyHeaders = {};
if (this.idempotencyHeader && method !== 'get') {
if (!options.idempotencyKey)
options.idempotencyKey = this.defaultIdempotencyKey();
idempotencyHeaders[this.idempotencyHeader] = options.idempotencyKey;
}
const headers = buildHeaders([
idempotencyHeaders,
{
Accept: 'application/json',
'User-Agent': this.getUserAgent(),
'X-Stainless-Retry-Count': String(retryCount),
...(options.timeout ? { 'X-Stainless-Timeout': String(Math.trunc(options.timeout / 1000)) } : {}),
...getPlatformHeaders(),
...(this._options.dangerouslyAllowBrowser ?
{ 'anthropic-dangerous-direct-browser-access': 'true' }
: undefined),
'anthropic-version': '2023-06-01',
},
await this.authHeaders(options),
this._options.defaultHeaders,
bodyHeaders,
options.headers,
]);
this.validateHeaders(headers);
return headers.values;
}
_makeAbort(controller) {
// note: we can't just inline this method inside `fetchWithTimeout()` because then the closure
// would capture all request options, and cause a memory leak.
return () => controller.abort();
}
buildBody({ options: { body, headers: rawHeaders } }) {
if (!body) {
return { bodyHeaders: undefined, body: undefined };
}
const headers = buildHeaders([rawHeaders]);
if (
// Pass raw type verbatim
ArrayBuffer.isView(body) ||
body instanceof ArrayBuffer ||
body instanceof DataView ||
(typeof body === 'string' &&
// Preserve legacy string encoding behavior for now
headers.values.has('content-type')) ||
// `Blob` is superset of `File`
(globalThis.Blob && body instanceof globalThis.Blob) ||
// `FormData` -> `multipart/form-data`
body instanceof FormData ||
// `URLSearchParams` -> `application/x-www-form-urlencoded`
body instanceof URLSearchParams ||
// Send chunked stream (each chunk has own `length`)
(globalThis.ReadableStream && body instanceof globalThis.ReadableStream)) {
return { bodyHeaders: undefined, body: body };
}
else if (typeof body === 'object' &&
(Symbol.asyncIterator in body ||
(Symbol.iterator in body && 'next' in body && typeof body.next === 'function'))) {
return { bodyHeaders: undefined, body: Shims.ReadableStreamFrom(body) };
}
else if (typeof body === 'object' &&
headers.values.get('content-type') === 'application/x-www-form-urlencoded') {
return {
bodyHeaders: { 'content-type': 'application/x-www-form-urlencoded' },
body: this.stringifyQuery(body),
};
}
else {
return __classPrivateFieldGet(this, _BaseAnthropic_encoder, "f").call(this, { body, headers });
}
}
}
_a = BaseAnthropic, _BaseAnthropic_encoder = new WeakMap(), _BaseAnthropic_instances = new WeakSet(), _BaseAnthropic_baseURLOverridden = function _BaseAnthropic_baseURLOverridden() {
return this.baseURL !== 'https://api.anthropic.com';
};
BaseAnthropic.Anthropic = _a;
BaseAnthropic.HUMAN_PROMPT = HUMAN_PROMPT;
BaseAnthropic.AI_PROMPT = AI_PROMPT;
BaseAnthropic.DEFAULT_TIMEOUT = 600000; // 10 minutes
BaseAnthropic.AnthropicError = Errors.AnthropicError;
BaseAnthropic.APIError = Errors.APIError;
BaseAnthropic.APIConnectionError = Errors.APIConnectionError;
BaseAnthropic.APIConnectionTimeoutError = Errors.APIConnectionTimeoutError;
BaseAnthropic.APIUserAbortError = Errors.APIUserAbortError;
BaseAnthropic.NotFoundError = Errors.NotFoundError;
BaseAnthropic.ConflictError = Errors.ConflictError;
BaseAnthropic.RateLimitError = Errors.RateLimitError;
BaseAnthropic.BadRequestError = Errors.BadRequestError;
BaseAnthropic.AuthenticationError = Errors.AuthenticationError;
BaseAnthropic.InternalServerError = Errors.InternalServerError;
BaseAnthropic.PermissionDeniedError = Errors.PermissionDeniedError;
BaseAnthropic.UnprocessableEntityError = Errors.UnprocessableEntityError;
BaseAnthropic.toFile = Uploads.toFile;
/**
* API Client for interfacing with the Anthropic API.
*/
export class Anthropic extends BaseAnthropic {
constructor() {
super(...arguments);
this.completions = new API.Completions(this);
this.messages = new API.Messages(this);
this.models = new API.Models(this);
this.beta = new API.Beta(this);
}
}
Anthropic.Completions = Completions;
Anthropic.Messages = Messages;
Anthropic.Models = Models;
Anthropic.Beta = Beta;
//# sourceMappingURL=client.mjs.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,49 @@
import { type BaseAnthropic } from "../client.mjs";
import { type PromiseOrValue } from "../internal/types.mjs";
import { type APIResponseProps, type WithRequestID } from "../internal/parse.mjs";
/**
* A subclass of `Promise` providing additional helper methods
* for interacting with the SDK.
*/
export declare class APIPromise<T> extends Promise<WithRequestID<T>> {
#private;
private responsePromise;
private parseResponse;
private parsedPromise;
constructor(client: BaseAnthropic, responsePromise: Promise<APIResponseProps>, parseResponse?: (client: BaseAnthropic, props: APIResponseProps) => PromiseOrValue<WithRequestID<T>>);
_thenUnwrap<U>(transform: (data: T, props: APIResponseProps) => U): APIPromise<U>;
/**
* Gets the raw `Response` instance instead of parsing the response
* data.
*
* If you want to parse the response body but still get the `Response`
* instance, you can use {@link withResponse()}.
*
* 👋 Getting the wrong TypeScript type for `Response`?
* Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]`
* to your `tsconfig.json`.
*/
asResponse(): Promise<Response>;
/**
* Gets the parsed response data, the raw `Response` instance and the ID of the request,
* returned via the `request-id` header which is useful for debugging requests and resporting
* issues to Anthropic.
*
* If you just want to get the raw `Response` instance without parsing it,
* you can use {@link asResponse()}.
*
* 👋 Getting the wrong TypeScript type for `Response`?
* Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]`
* to your `tsconfig.json`.
*/
withResponse(): Promise<{
data: T;
response: Response;
request_id: string | null | undefined;
}>;
private parse;
then<TResult1 = WithRequestID<T>, TResult2 = never>(onfulfilled?: ((value: WithRequestID<T>) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<WithRequestID<T> | TResult>;
finally(onfinally?: (() => void) | undefined | null): Promise<WithRequestID<T>>;
}
//# sourceMappingURL=api-promise.d.mts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"api-promise.d.mts","sourceRoot":"","sources":["../src/core/api-promise.ts"],"names":[],"mappings":"OAEO,EAAE,KAAK,aAAa,EAAE;OAEtB,EAAE,KAAK,cAAc,EAAE;OACvB,EACL,KAAK,gBAAgB,EACrB,KAAK,aAAa,EAGnB;AAED;;;GAGG;AACH,qBAAa,UAAU,CAAC,CAAC,CAAE,SAAQ,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;;IAMxD,OAAO,CAAC,eAAe;IACvB,OAAO,CAAC,aAAa;IANvB,OAAO,CAAC,aAAa,CAAwC;gBAI3D,MAAM,EAAE,aAAa,EACb,eAAe,EAAE,OAAO,CAAC,gBAAgB,CAAC,EAC1C,aAAa,GAAE,CACrB,MAAM,EAAE,aAAa,EACrB,KAAK,EAAE,gBAAgB,KACpB,cAAc,CAAC,aAAa,CAAC,CAAC,CAAC,CAAwB;IAW9D,WAAW,CAAC,CA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{ type APIResponseProps, type WithRequestID } from "../internal/parse.js";
/**
* A subclass of `Promise` providing additional helper methods
* for interacting with the SDK.
*/
export declare class APIPromise<T> extends Promise<WithRequestID<T>> {
#private;
private responsePromise;
private parseResponse;
private parsedPromise;
constructor(client: BaseAnthropic, responsePromise: Promise<APIResponseProps>, parseResponse?: (client: BaseAnthropic, props: APIResponseProps) => PromiseOrValue<WithRequestID<T>>);
_thenUnwrap<U>(transform: (data: T, props: APIResponseProps) => U): APIPromise<U>;
/**
* Gets the raw `Response` instance instead of parsing the response
* data.
*
* If you want to parse the response body but still get the `Response`
* instance, you can use {@link withResponse()}.
*
* 👋 Getting the wrong TypeScript type for `Response`?
* Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]`
* to your `tsconfig.json`.
*/
asResponse(): Promise<Response>;
/**
* Gets the parsed response data, the raw `Response` instance and the ID of the request,
* returned via the `request-id` header which is useful for debugging requests and resporting
* issues to Anthropic.
*
* If you just want to get the raw `Response` instance without parsing it,
* you can use {@link asResponse()}.
*
* 👋 Getting the wrong TypeScript type for `Response`?
* Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]`
* to your `tsconfig.json`.
*/
withResponse(): Promise<{
data: T;
response: Response;
request_id: string | null | undefined;
}>;
private parse;
then<TResult1 = WithRequestID<T>, TResult2 = never>(onfulfilled?: ((value: WithRequestID<T>) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<WithRequestID<T> | TResult>;
finally(onfinally?: (() => void) | undefined | null): Promise<WithRequestID<T>>;
}
//# sourceMappingURL=api-promise.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"api-promise.d.ts","sourceRoot":"","sources":["../src/core/api-promise.ts"],"names":[],"mappings":"OAEO,EAAE,KAAK,aAAa,EAAE;OAEtB,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Q,GAAG,QAAQ,CAAC;IAItB,KAAK,CAAC,OAAO,GAAG,KAAK,EAC5B,UAAU,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC,GAAG,SAAS,GAAG,IAAI,GAChF,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;IAI7B,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,GAAG,IAAI,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;CAGzF"}

View File

@@ -0,0 +1,76 @@
"use strict";
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
var _APIPromise_client;
Object.defineProperty(exports, "__esModule", { value: true });
exports.APIPromise = void 0;
const tslib_1 = require("../internal/tslib.js");
const parse_1 = require("../internal/parse.js");
/**
* A subclass of `Promise` providing additional helper methods
* for interacting with the SDK.
*/
class APIPromise extends Promise {
constructor(client, responsePromise, parseResponse = parse_1.defaultParseResponse) {
super((resolve) => {
// this is maybe a bit weird but this has to be a no-op to not implicitly
// parse the response body; instead .then, .catch, .finally are overridden
// to parse the response
resolve(null);
});
this.responsePromise = responsePromise;
this.parseResponse = parseResponse;
_APIPromise_client.set(this, void 0);
tslib_1.__classPrivateFieldSet(this, _APIPromise_client, client, "f");
}
_thenUnwrap(transform) {
return new APIPromise(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), this.responsePromise, async (client, props) => (0, parse_1.addRequestID)(transform(await this.parseResponse(client, props), props), props.response));
}
/**
* Gets the raw `Response` instance instead of parsing the response
* data.
*
* If you want to parse the response body but still get the `Response`
* instance, you can use {@link withResponse()}.
*
* 👋 Getting the wrong TypeScript type for `Response`?
* Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]`
* to your `tsconfig.json`.
*/
asResponse() {
return this.responsePromise.then((p) => p.response);
}
/**
* Gets the parsed response data, the raw `Response` instance and the ID of the request,
* returned via the `request-id` header which is useful for debugging requests and resporting
* issues to Anthropic.
*
* If you just want to get the raw `Response` instance without parsing it,
* you can use {@link asResponse()}.
*
* 👋 Getting the wrong TypeScript type for `Response`?
* Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]`
* to your `tsconfig.json`.
*/
async withResponse() {
const [data, response] = await Promise.all([this.parse(), this.asResponse()]);
return { data, response, request_id: response.headers.get('request-id') };
}
parse() {
if (!this.parsedPromise) {
this.parsedPromise = this.responsePromise.then((data) => this.parseResponse(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), data));
}
return this.parsedPromise;
}
then(onfulfilled, onrejected) {
return this.parse().then(onfulfilled, onrejected);
}
catch(onrejected) {
return this.parse().catch(onrejected);
}
finally(onfinally) {
return this.parse().finally(onfinally);
}
}
exports.APIPromise = APIPromise;
_APIPromise_client = new WeakMap();
//# sourceMappingURL=api-promise.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"api-promise.js","sourceRoot":"","sources":["../src/core/api-promise.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;;;AAKtF,gDAK2B;AAE3B;;;GAGG;AACH,MAAa,UAAc,SAAQ,OAAyB;IAI1D,YACE,MAAqB,EACb,eAA0C,EAC1C,gBAGgC,4BAAoB;QAE5D,KAAK,CAAC,CAAC,OAAO,EAAE,EAAE;YAChB,yEAAyE;YACzE,0EAA0E;YAC1E,wBAAwB;YACxB,OAAO,CAAC,IAAW,CAAC,CAAC;QACvB,CAAC,CAAC,CAAC;QAXK,oBAAe,GAAf,eAAe,CAA2B;QAC1C,kBAAa,GAAb,aAAa,CAGuC;QAR9D,qCAAuB;QAgBrB,+BAAA,IAAI,sBAAW,MAAM,MAAA,CAAC;IACxB,CAAC;IAED,WAAW,CAAI,SAAkD;QAC/D,OAAO,IAAI,UAAU,CAAC,+BAAA,IAAI,0BAAQ,EAAE,IAAI,CAAC,eAAe,EAAE,KAAK,EAAE,MAAM,EAAE,KAA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C;QAC1D,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACzC,CAAC;CACF;AApFD,gCAoFC"}

View File

@@ -0,0 +1,72 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
var _APIPromise_client;
import { __classPrivateFieldGet, __classPrivateFieldSet } from "../internal/tslib.mjs";
import { defaultParseResponse, addRequestID, } from "../internal/parse.mjs";
/**
* A subclass of `Promise` providing additional helper methods
* for interacting with the SDK.
*/
export class APIPromise extends Promise {
constructor(client, responsePromise, parseResponse = defaultParseResponse) {
super((resolve) => {
// this is maybe a bit weird but this has to be a no-op to not implicitly
// parse the response body; instead .then, .catch, .finally are overridden
// to parse the response
resolve(null);
});
this.responsePromise = responsePromise;
this.parseResponse = parseResponse;
_APIPromise_client.set(this, void 0);
__classPrivateFieldSet(this, _APIPromise_client, client, "f");
}
_thenUnwrap(transform) {
return new APIPromise(__classPrivateFieldGet(this, _APIPromise_client, "f"), this.responsePromise, async (client, props) => addRequestID(transform(await this.parseResponse(client, props), props), props.response));
}
/**
* Gets the raw `Response` instance instead of parsing the response
* data.
*
* If you want to parse the response body but still get the `Response`
* instance, you can use {@link withResponse()}.
*
* 👋 Getting the wrong TypeScript type for `Response`?
* Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]`
* to your `tsconfig.json`.
*/
asResponse() {
return this.responsePromise.then((p) => p.response);
}
/**
* Gets the parsed response data, the raw `Response` instance and the ID of the request,
* returned via the `request-id` header which is useful for debugging requests and resporting
* issues to Anthropic.
*
* If you just want to get the raw `Response` instance without parsing it,
* you can use {@link asResponse()}.
*
* 👋 Getting the wrong TypeScript type for `Response`?
* Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]`
* to your `tsconfig.json`.
*/
async withResponse() {
const [data, response] = await Promise.all([this.parse(), this.asResponse()]);
return { data, response, request_id: response.headers.get('request-id') };
}
parse() {
if (!this.parsedPromise) {
this.parsedPromise = this.responsePromise.then((data) => this.parseResponse(__classPrivateFieldGet(this, _APIPromise_client, "f"), data));
}
return this.parsedPromise;
}
then(onfulfilled, onrejected) {
return this.parse().then(onfulfilled, onrejected);
}
catch(onrejected) {
return this.parse().catch(onrejected);
}
finally(onfinally) {
return this.parse().finally(onfinally);
}
}
_APIPromise_client = new WeakMap();
//# sourceMappingURL=api-promise.mjs.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"api-promise.mjs","sourceRoot":"","sources":["../src/core/api-promise.ts"],"names":[],"mappings":"AAAA,sFAAsF;;;OAK/E,EAGL,oBAAoB,EACpB,YAAY,GACb;AAED;;;GAGG;AACH,MAAM,OAAO,UAAc,SAAQ,OAAyB;IAI1D,YACE,MAAqB,EACb,eAA0C,EAC1C,gBAGgC,oBAAoB;QAE5D,KAAK,CAAC,CAAC,OAAO,EAAE,EAAE;YAChB,yEAAyE;YACzE,0EAA0E;YAC1E,wBAAwB;YACxB,OAAO,CAAC,IAAW,CAAC,CAAC;QACvB,CAAC,CAAC,CAAC;QAXK,oBAAe,GAAf,eAAe,CAA2B;QAC1C,kBAAa,GAAb,aAAa,CAGuC;QAR9D,qCAAuB;QAgBrB,uBAAA,IAAI,sBAAW,MAAM,MAAA,CAAC;IACxB,CAAC;IAED,WAAW,CAAI,SAAkD;QAC/D,OAAO,IAAI,UAAU,CAAC,uBAAA,IAAI,0BAAQ,EAAE,IAAI,CAAC,eAAe,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,CAChF,YAAY,CAAC,SAAS,CAAC,MAAM,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE,KAAK,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,CACxF,CAAC;IACJ,CAAC;IAED;;;;;;;;;;OAUG;IACH,UAAU;QACR,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;IACtD,CAAC;IAED;;;;;;;;;;;OAWG;IACH,KAAK,CAAC,YAAY;QAChB,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC;QAC9E,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,EAAE,CAAC;IAC5E,CAAC;IAEO,KAAK;QACX,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC;YACxB,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC5C,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,uBAAA,IAAI,0BAAQ,EAAE,IAAI,CAAqC,CACrF,CAAC;QACJ,CAAC;QACD,OAAO,IAAI,CAAC,aAAa,CAAC;IAC5B,CAAC;IAEQ,IAAI,CACX,WAAgG,EAChG,UAAmF;QAEnF,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;IACpD,CAAC;IAEQ,KAAK,CACZ,UAAiF;QAEjF,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IACxC,CAAC;IAEQ,OAAO,CAAC,SAA2C;QAC1D,OAAO,IAAI,CAAC,KAAK,EAAE,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACzC,CAAC;CACF"}

View File

@@ -0,0 +1,50 @@
import type { ErrorType } from "../resources/shared.mjs";
export declare class AnthropicError extends Error {
}
export declare class APIError<TStatus extends number | undefined = number | undefined, THeaders extends Headers | undefined = Headers | undefined, TError extends Object | undefined = Object | undefined> extends AnthropicError {
/** HTTP status for the response that caused the error */
readonly status: TStatus;
/** HTTP headers for the response that caused the error */
readonly headers: THeaders;
/** JSON body of the response that caused the error */
readonly error: TError;
readonly requestID: string | null | undefined;
/** The `error.type` from the API response body, e.g. `"rate_limit_error"` */
readonly type: ErrorType | null;
constructor(status: TStatus, error: TError, message: string | undefined, headers: THeaders, type?: ErrorType | null);
private static makeMessage;
static generate(status: number | undefined, errorResponse: Object | undefined, message: string | undefined, headers: Headers | undefined): APIError;
}
export declare class APIUserAbortError extends APIError<undefined, undefined, undefined> {
constructor({ message }?: {
message?: string;
});
}
export declare class APIConnectionError extends APIError<undefined, undefined, undefined> {
constructor({ message, cause }: {
message?: string | undefined;
cause?: Error | undefined;
});
}
export declare class APIConnectionTimeoutError extends APIConnectionError {
constructor({ message }?: {
message?: string;
});
}
export declare class BadRequestError extends APIError<400, Headers> {
}
export declare class AuthenticationError extends APIError<401, Headers> {
}
export declare class PermissionDeniedError extends APIError<403, Headers> {
}
export declare class NotFoundError extends APIError<404, Headers> {
}
export declare class ConflictError extends APIError<409, Headers> {
}
export declare class UnprocessableEntityError extends APIError<422, Headers> {
}
export declare class RateLimitError extends APIError<429, Headers> {
}
export declare class InternalServerError extends APIError<number, Headers> {
}
//# sourceMappingURL=error.d.mts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"error.d.mts","sourceRoot":"","sources":["../src/core/error.ts"],"names":[],"mappings":"OAGO,KAAK,EAAE,SAAS,EAAE;AAEzB,qBAAa,cAAe,SAAQ,KAAK;CAAG;AAE5C,qBAAa,QAAQ,CACnB,OAAO,SAAS,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,EACvD,QAAQ,SAAS,OAAO,GAAG,SAAS,GAAG,OAAO,GAAG,SAAS,EAC1D,MAAM,SAAS,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CACtD,SAAQ,cAAc;IACtB,yDAAyD;IACzD,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,0DAA0D;IAC1D,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC;IAC3B,sDAAsD;IACtD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B,SAAQ,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CAAG;AAEpE,qBAAa,aAAc,SAAQ,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CAAG;AAE5D,qBAAa,aAAc,SAAQ,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CAAG;AAE5D,qBAAa,wBAAyB,SAAQ,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CAAG;AAEvE,qBAAa,cAAe,SAAQ,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CAAG;AAE7D,qBAAa,mBAAoB,SAAQ,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;CAAG"}

View File

@@ -0,0 +1,50 @@
import type { ErrorType } from "../resources/shared.js";
export declare class AnthropicError extends Error {
}
export declare class APIError<TStatus extends number | undefined = number | undefined, THeaders extends Headers | undefined = Headers | undefined, TError extends Object | undefined = Object | undefined> extends AnthropicError {
/** HTTP status for the response that caused the error */
readonly status: TStatus;
/** HTTP headers for the response that caused the error */
readonly headers: THeaders;
/** JSON body of the response that caused the error */
readonly error: TError;
readonly requestID: string | null | undefined;
/** The `error.type` from the API response body, e.g. `"rate_limit_error"` */
readonly type: ErrorType | null;
constructor(status: TStatus, error: TError, message: string | undefined, headers: THeaders, type?: ErrorType | null);
private static makeMessage;
static generate(status: number | undefined, errorResponse: Object | undefined, message: string | undefined, headers: Headers | undefined): APIError;
}
export declare class APIUserAbortError extends APIError<undefined, undefined, undefined> {
constructor({ message }?: {
message?: string;
});
}
export declare class APIConnectionError extends APIError<undefined, undefined, undefined> {
constructor({ message, cause }: {
message?: string | undefined;
cause?: Error | undefined;
});
}
export declare class APIConnectionTimeoutError extends APIConnectionError {
constructor({ message }?: {
message?: string;
});
}
export declare class BadRequestError extends APIError<400, Headers> {
}
export declare class AuthenticationError extends APIError<401, Headers> {
}
export declare class PermissionDeniedError extends APIError<403, Headers> {
}
export declare class NotFoundError extends APIError<404, Headers> {
}
export declare class ConflictError extends APIError<409, Headers> {
}
export declare class UnprocessableEntityError extends APIError<422, Headers> {
}
export declare class RateLimitError extends APIError<429, Headers> {
}
export declare class InternalServerError extends APIError<number, Headers> {
}
//# sourceMappingURL=error.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/core/error.ts"],"names":[],"mappings":"OAGO,KAAK,EAAE,SAAS,EAAE;AAEzB,qBAAa,cAAe,SAAQ,KAAK;CAAG;AAE5C,qBAAa,QAAQ,CACnB,OAAO,SAAS,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,EACvD,QAAQ,SAAS,OAAO,GAAG,SAAS,GAAG,OAAO,GAAG,SAAS,EAC1D,MAAM,SAAS,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CACtD,SAAQ,cAAc;IACtB,yDAAyD;IACzD,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,0DAA0D;IAC1D,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC;IAC3B,sDAAsD;IACtD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;IAE9C,6EAA6E;IAC7E,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,IAAI,CAAC;gBAG9B,MAAM,EAAE,OAAO,EACf,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,OAAO,EAAE,QAAQ,EACjB,IAAI,CAAC,EAAE,SAAS,GAAG,IAAI;IAUzB,OAAO,CAAC,MAAM,CAAC,WAAW;IAqB1B,MAAM,CAAC,QAAQ,CACb,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,aAAa,EAAE,MAAM,GAAG,SAAS,EACjC,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,OAAO,EAAE,OAAO,GAAG,SAAS,GAC3B,QAAQ;CA0CZ;AAED,qBAAa,iBAAkB,SAAQ,QAAQ,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,CAAC;gBAClE,EAAE,OAAO,EAAE,GAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAA;KAAO;CAGnD;AAED,qBAAa,kBAAmB,SAAQ,QAAQ,CAAC,SAAS,EAAE,SAAS,EAAE,SAAS,CAAC;gBACnE,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAAC,KAAK,CAAC,EAAE,KAAK,GAAG,SAAS,CAAA;KAAE;CAM5F;AAED,qBAAa,yBAA0B,SAAQ,kBAAkB;gBACnD,EAAE,OAAO,EAAE,GAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAA;KAAO;CAGnD;AAED,qBAAa,eAAgB,SAAQ,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CAAG;AAE9D,qBAAa,mBAAoB,SAAQ,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CAAG;AAElE,qBAAa,qBAAsB,SAAQ,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CAAG;AAEpE,qBAAa,aAAc,SAAQ,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CAAG;AAE5D,qBAAa,aAAc,SAAQ,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CAAG;AAE5D,qBAAa,wBAAyB,SAAQ,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CAAG;AAEvE,qBAAa,cAAe,SAAQ,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CAAG;AAE7D,qBAAa,mBAAoB,SAAQ,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;CAAG"}

View File

@@ -0,0 +1,116 @@
"use strict";
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.InternalServerError = exports.RateLimitError = exports.UnprocessableEntityError = exports.ConflictError = exports.NotFoundError = exports.PermissionDeniedError = exports.AuthenticationError = exports.BadRequestError = exports.APIConnectionTimeoutError = exports.APIConnectionError = exports.APIUserAbortError = exports.APIError = exports.AnthropicError = void 0;
const errors_1 = require("../internal/errors.js");
class AnthropicError extends Error {
}
exports.AnthropicError = AnthropicError;
class APIError extends AnthropicError {
constructor(status, error, message, headers, type) {
super(`${APIError.makeMessage(status, error, message)}`);
this.status = status;
this.headers = headers;
this.requestID = headers?.get('request-id');
this.error = error;
this.type = type ?? null;
}
static makeMessage(status, error, message) {
const msg = error?.message ?
typeof error.message === 'string' ?
error.message
: JSON.stringify(error.message)
: error ? JSON.stringify(error)
: message;
if (status && msg) {
return `${status} ${msg}`;
}
if (status) {
return `${status} status code (no body)`;
}
if (msg) {
return msg;
}
return '(no status code or body)';
}
static generate(status, errorResponse, message, headers) {
if (!status || !headers) {
return new APIConnectionError({ message, cause: (0, errors_1.castToError)(errorResponse) });
}
const error = errorResponse;
const type = error?.['error']?.['type'];
if (status === 400) {
return new BadRequestError(status, error, message, headers, type);
}
if (status === 401) {
return new AuthenticationError(status, error, message, headers, type);
}
if (status === 403) {
return new PermissionDeniedError(status, error, message, headers, type);
}
if (status === 404) {
return new NotFoundError(status, error, message, headers, type);
}
if (status === 409) {
return new ConflictError(status, error, message, headers, type);
}
if (status === 422) {
return new UnprocessableEntityError(status, error, message, headers, type);
}
if (status === 429) {
return new RateLimitError(status, error, message, headers, type);
}
if (status >= 500) {
return new InternalServerError(status, error, message, headers, type);
}
return new APIError(status, error, message, headers, type);
}
}
exports.APIError = APIError;
class APIUserAbortError extends APIError {
constructor({ message } = {}) {
super(undefined, undefined, message || 'Request was aborted.', undefined);
}
}
exports.APIUserAbortError = APIUserAbortError;
class APIConnectionError extends APIError {
constructor({ message, cause }) {
super(undefined, undefined, message || 'Connection error.', undefined);
// in some environments the 'cause' property is already declared
// @ts-ignore
if (cause)
this.cause = cause;
}
}
exports.APIConnectionError = APIConnectionError;
class APIConnectionTimeoutError extends APIConnectionError {
constructor({ message } = {}) {
super({ message: message ?? 'Request timed out.' });
}
}
exports.APIConnectionTimeoutError = APIConnectionTimeoutError;
class BadRequestError extends APIError {
}
exports.BadRequestError = BadRequestError;
class AuthenticationError extends APIError {
}
exports.AuthenticationError = AuthenticationError;
class PermissionDeniedError extends APIError {
}
exports.PermissionDeniedError = PermissionDeniedError;
class NotFoundError extends APIError {
}
exports.NotFoundError = NotFoundError;
class ConflictError extends APIError {
}
exports.ConflictError = ConflictError;
class UnprocessableEntityError extends APIError {
}
exports.UnprocessableEntityError = UnprocessableEntityError;
class RateLimitError extends APIError {
}
exports.RateLimitError = RateLimitError;
class InternalServerError extends APIError {
}
exports.InternalServerError = InternalServerError;
//# sourceMappingURL=error.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"error.js","sourceRoot":"","sources":["../src/core/error.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,kDAAiD;AAGjD,MAAa,cAAe,SAAQ,KAAK;CAAG;AAA5C,wCAA4C;AAE5C,MAAa,QAIX,SAAQ,cAAc;IAatB,YACE,MAAe,EACf,KAAa,EACb,OAA2B,EAC3B,OAAiB,EACjB,IAAuB;QAEvB,KAAK,CAAC,GAAG,QAAQ,CAAC,WAAW,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC;QACzD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,SAAS,GAAG,OAAO,EAAE,GAAG,CAAC,YAAY,CAAC,CAAC;QAC5C,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,IAAI,GAAG,IAAI,IAAI,IAAI,CAAC;IAC3B,CAAC;IAEO,MAAM,CAAC,WAAW,CAAC,MAA0B,EAAE,KAAU,EAAE,OAA2B;QAC5F,MAAM,GAAG,GACP,KAAK,EAAE,OAAO,CAAC,CAAC;YACd,OAAO,KAAK,CAAC,OAAO,KA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AE,OAAO,EAAE,KAAK,EAA+D;QACzF,KAAK,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,IAAI,mBAAmB,EAAE,SAAS,CAAC,CAAC;QACvE,gEAAgE;QAChE,aAAa;QACb,IAAI,KAAK;YAAE,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IAChC,CAAC;CACF;AAPD,gDAOC;AAED,MAAa,yBAA0B,SAAQ,kBAAkB;IAC/D,YAAY,EAAE,OAAO,KAA2B,EAAE;QAChD,KAAK,CAAC,EAAE,OAAO,EAAE,OAAO,IAAI,oBAAoB,EAAE,CAAC,CAAC;IACtD,CAAC;CACF;AAJD,8DAIC;AAED,MAAa,eAAgB,SAAQ,QAAsB;CAAG;AAA9D,0CAA8D;AAE9D,MAAa,mBAAoB,SAAQ,QAAsB;CAAG;AAAlE,kDAAkE;AAElE,MAAa,qBAAsB,SAAQ,QAAsB;CAAG;AAApE,sDAAoE;AAEpE,MAAa,aAAc,SAAQ,QAAsB;CAAG;AAA5D,sCAA4D;AAE5D,MAAa,aAAc,SAAQ,QAAsB;CAAG;AAA5D,sCAA4D;AAE5D,MAAa,wBAAyB,SAAQ,QAAsB;CAAG;AAAvE,4DAAuE;AAEvE,MAAa,cAAe,SAAQ,QAAsB;CAAG;AAA7D,wCAA6D;AAE7D,MAAa,mBAAoB,SAAQ,QAAyB;CAAG;AAArE,kDAAqE"}

View File

@@ -0,0 +1,100 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { castToError } from "../internal/errors.mjs";
export class AnthropicError extends Error {
}
export class APIError extends AnthropicError {
constructor(status, error, message, headers, type) {
super(`${APIError.makeMessage(status, error, message)}`);
this.status = status;
this.headers = headers;
this.requestID = headers?.get('request-id');
this.error = error;
this.type = type ?? null;
}
static makeMessage(status, error, message) {
const msg = error?.message ?
typeof error.message === 'string' ?
error.message
: JSON.stringify(error.message)
: error ? JSON.stringify(error)
: message;
if (status && msg) {
return `${status} ${msg}`;
}
if (status) {
return `${status} status code (no body)`;
}
if (msg) {
return msg;
}
return '(no status code or body)';
}
static generate(status, errorResponse, message, headers) {
if (!status || !headers) {
return new APIConnectionError({ message, cause: castToError(errorResponse) });
}
const error = errorResponse;
const type = error?.['error']?.['type'];
if (status === 400) {
return new BadRequestError(status, error, message, headers, type);
}
if (status === 401) {
return new AuthenticationError(status, error, message, headers, type);
}
if (status === 403) {
return new PermissionDeniedError(status, error, message, headers, type);
}
if (status === 404) {
return new NotFoundError(status, error, message, headers, type);
}
if (status === 409) {
return new ConflictError(status, error, message, headers, type);
}
if (status === 422) {
return new UnprocessableEntityError(status, error, message, headers, type);
}
if (status === 429) {
return new RateLimitError(status, error, message, headers, type);
}
if (status >= 500) {
return new InternalServerError(status, error, message, headers, type);
}
return new APIError(status, error, message, headers, type);
}
}
export class APIUserAbortError extends APIError {
constructor({ message } = {}) {
super(undefined, undefined, message || 'Request was aborted.', undefined);
}
}
export class APIConnectionError extends APIError {
constructor({ message, cause }) {
super(undefined, undefined, message || 'Connection error.', undefined);
// in some environments the 'cause' property is already declared
// @ts-ignore
if (cause)
this.cause = cause;
}
}
export class APIConnectionTimeoutError extends APIConnectionError {
constructor({ message } = {}) {
super({ message: message ?? 'Request timed out.' });
}
}
export class BadRequestError extends APIError {
}
export class AuthenticationError extends APIError {
}
export class PermissionDeniedError extends APIError {
}
export class NotFoundError extends APIError {
}
export class ConflictError extends APIError {
}
export class UnprocessableEntityError extends APIError {
}
export class RateLimitError extends APIError {
}
export class InternalServerError extends APIError {
}
//# sourceMappingURL=error.mjs.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"error.mjs","sourceRoot":"","sources":["../src/core/error.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,WAAW,EAAE;AAGtB,MAAM,OAAO,cAAe,SAAQ,KAAK;CAAG;AAE5C,MAAM,OAAO,QAIX,SAAQ,cAAc;IAatB,YACE,MAAe,EACf,KAAa,EACb,OAA2B,EAC3B,OAAiB,EACjB,IAAuB;QA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oB,SAAQ,QAAyB;CAAG"}

View File

@@ -0,0 +1,105 @@
import { FinalRequestOptions } from "../internal/request-options.mjs";
import { type BaseAnthropic } from "../client.mjs";
import { APIPromise } from "./api-promise.mjs";
import { type APIResponseProps } from "../internal/parse.mjs";
export type PageRequestOptions = Pick<FinalRequestOptions, 'query' | 'headers' | 'body' | 'path' | 'method'>;
export declare abstract class AbstractPage<Item> implements AsyncIterable<Item> {
#private;
protected options: FinalRequestOptions;
protected response: Response;
protected body: unknown;
constructor(client: BaseAnthropic, response: Response, body: unknown, options: FinalRequestOptions);
abstract nextPageRequestOptions(): PageRequestOptions | null;
abstract getPaginatedItems(): Item[];
hasNextPage(): boolean;
getNextPage(): Promise<this>;
iterPages(): AsyncGenerator<this>;
[Symbol.asyncIterator](): AsyncGenerator<Item>;
}
/**
* This subclass of Promise will resolve to an instantiated Page once the request completes.
*
* It also implements AsyncIterable to allow auto-paginating iteration on an unawaited list call, eg:
*
* for await (const item of client.items.list()) {
* console.log(item)
* }
*/
export declare class PagePromise<PageClass extends AbstractPage<Item>, Item = ReturnType<PageClass['getPaginatedItems']>[number]> extends APIPromise<PageClass> implements AsyncIterable<Item> {
constructor(client: BaseAnthropic, request: Promise<APIResponseProps>, Page: new (...args: ConstructorParameters<typeof AbstractPage>) => PageClass);
/**
* Allow auto-paginating iteration on an unawaited list call, eg:
*
* for await (const item of client.items.list()) {
* console.log(item)
* }
*/
[Symbol.asyncIterator](): AsyncGenerator<Item>;
}
export interface PageResponse<Item> {
data: Array<Item>;
has_more: boolean;
first_id: string | null;
last_id: string | null;
}
export interface PageParams {
/**
* Number of items per page.
*/
limit?: number;
before_id?: string;
after_id?: string;
}
export declare class Page<Item> extends AbstractPage<Item> implements PageResponse<Item> {
data: Array<Item>;
has_more: boolean;
first_id: string | null;
last_id: string | null;
constructor(client: BaseAnthropic, response: Response, body: PageResponse<Item>, options: FinalRequestOptions);
getPaginatedItems(): Item[];
hasNextPage(): boolean;
nextPageRequestOptions(): PageRequestOptions | null;
}
export interface TokenPageResponse<Item> {
data: Array<Item>;
has_more: boolean;
next_page: string | null;
}
export interface TokenPageParams {
/**
* Number of items per page.
*/
limit?: number;
page_token?: string;
}
export declare class TokenPage<Item> extends AbstractPage<Item> implements TokenPageResponse<Item> {
data: Array<Item>;
has_more: boolean;
next_page: string | null;
constructor(client: BaseAnthropic, response: Response, body: TokenPageResponse<Item>, options: FinalRequestOptions);
getPaginatedItems(): Item[];
hasNextPage(): boolean;
nextPageRequestOptions(): PageRequestOptions | null;
}
export interface PageCursorResponse<Item> {
data: Array<Item>;
has_more: boolean;
next_page: string | null;
}
export interface PageCursorParams {
/**
* Number of items per page.
*/
limit?: number;
page?: string | null;
}
export declare class PageCursor<Item> extends AbstractPage<Item> implements PageCursorResponse<Item> {
data: Array<Item>;
has_more: boolean;
next_page: string | null;
constructor(client: BaseAnthropic, response: Response, body: PageCursorResponse<Item>, options: FinalRequestOptions);
getPaginatedItems(): Item[];
hasNextPage(): boolean;
nextPageRequestOptions(): PageRequestOptions | null;
}
//# sourceMappingURL=pagination.d.mts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"pagination.d.mts","sourceRoot":"","sources":["../src/core/pagination.ts"],"names":[],"mappings":"OAGO,EAAE,mBAAmB,EAAE;OAEvB,EAAE,KAAK,aAAa,EAAE;OACtB,EAAE,UAAU,EAAE;OACd,EAAE,KAAK,gBAAgB,EAAE;AAGhC,MAAM,MAAM,kBAAkB,GAAG,IAAI,CAAC,mBAAmB,EAAE,OAAO,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,CAAC,CAAC;AAE7G,8BAAsB,YAAY,CAAC,IAAI,CAAE,YAAW,aAAa,CAAC,IAAI,CAAC;;IAErE,SAAS,CAAC,OAAO,EAAE,mBAAmB,CAAC;IAEvC,SAAS,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC7B,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC;gBAEZ,MAAM,EAAE,aAAa,EAAE,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IACtC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;IAElB,QAAQ,EAAE,OAAO,CAAC;IAElB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAED,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED,qBAAa,UAAU,CAAC,IAAI,CAAE,SAAQ,YAAY,CAAC,IAAI,CAAE,YAAW,kBAAkB,CAAC,IAAI,CAAC;IAC1F,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;IAElB,QAAQ,EAAE,OAAO,CAAC;IAElB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;gBAGvB,MAAM,EAAE,aAAa,EACrB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,kBAAkB,CAAC,IAAI,CAAC,EAC9B,OAAO,EAAE,mBAAmB;IAS9B,iBAAiB,IAAI,IAAI,EAAE;IAIlB,WAAW,IAAI,OAAO;IAQ/B,sBAAsB,IAAI,kBAAkB,GAAG,IAAI;CAcpD"}

View File

@@ -0,0 +1,105 @@
import { FinalRequestOptions } from "../internal/request-options.js";
import { type BaseAnthropic } from "../client.js";
import { APIPromise } from "./api-promise.js";
import { type APIResponseProps } from "../internal/parse.js";
export type PageRequestOptions = Pick<FinalRequestOptions, 'query' | 'headers' | 'body' | 'path' | 'method'>;
export declare abstract class AbstractPage<Item> implements AsyncIterable<Item> {
#private;
protected options: FinalRequestOptions;
protected response: Response;
protected body: unknown;
constructor(client: BaseAnthropic, response: Response, body: unknown, options: FinalRequestOptions);
abstract nextPageRequestOptions(): PageRequestOptions | null;
abstract getPaginatedItems(): Item[];
hasNextPage(): boolean;
getNextPage(): Promise<this>;
iterPages(): AsyncGenerator<this>;
[Symbol.asyncIterator](): AsyncGenerator<Item>;
}
/**
* This subclass of Promise will resolve to an instantiated Page once the request completes.
*
* It also implements AsyncIterable to allow auto-paginating iteration on an unawaited list call, eg:
*
* for await (const item of client.items.list()) {
* console.log(item)
* }
*/
export declare class PagePromise<PageClass extends AbstractPage<Item>, Item = ReturnType<PageClass['getPaginatedItems']>[number]> extends APIPromise<PageClass> implements AsyncIterable<Item> {
constructor(client: BaseAnthropic, request: Promise<APIResponseProps>, Page: new (...args: ConstructorParameters<typeof AbstractPage>) => PageClass);
/**
* Allow auto-paginating iteration on an unawaited list call, eg:
*
* for await (const item of client.items.list()) {
* console.log(item)
* }
*/
[Symbol.asyncIterator](): AsyncGenerator<Item>;
}
export interface PageResponse<Item> {
data: Array<Item>;
has_more: boolean;
first_id: string | null;
last_id: string | null;
}
export interface PageParams {
/**
* Number of items per page.
*/
limit?: number;
before_id?: string;
after_id?: string;
}
export declare class Page<Item> extends AbstractPage<Item> implements PageResponse<Item> {
data: Array<Item>;
has_more: boolean;
first_id: string | null;
last_id: string | null;
constructor(client: BaseAnthropic, response: Response, body: PageResponse<Item>, options: FinalRequestOptions);
getPaginatedItems(): Item[];
hasNextPage(): boolean;
nextPageRequestOptions(): PageRequestOptions | null;
}
export interface TokenPageResponse<Item> {
data: Array<Item>;
has_more: boolean;
next_page: string | null;
}
export interface TokenPageParams {
/**
* Number of items per page.
*/
limit?: number;
page_token?: string;
}
export declare class TokenPage<Item> extends AbstractPage<Item> implements TokenPageResponse<Item> {
data: Array<Item>;
has_more: boolean;
next_page: string | null;
constructor(client: BaseAnthropic, response: Response, body: TokenPageResponse<Item>, options: FinalRequestOptions);
getPaginatedItems(): Item[];
hasNextPage(): boolean;
nextPageRequestOptions(): PageRequestOptions | null;
}
export interface PageCursorResponse<Item> {
data: Array<Item>;
has_more: boolean;
next_page: string | null;
}
export interface PageCursorParams {
/**
* Number of items per page.
*/
limit?: number;
page?: string | null;
}
export declare class PageCursor<Item> extends AbstractPage<Item> implements PageCursorResponse<Item> {
data: Array<Item>;
has_more: boolean;
next_page: string | null;
constructor(client: BaseAnthropic, response: Response, body: PageCursorResponse<Item>, options: FinalRequestOptions);
getPaginatedItems(): Item[];
hasNextPage(): boolean;
nextPageRequestOptions(): PageRequestOptions | null;
}
//# sourceMappingURL=pagination.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"pagination.d.ts","sourceRoot":"","sources":["../src/core/pagination.ts"],"names":[],"mappings":"OAGO,EAAE,mBAAmB,EAAE;OAEvB,EAAE,KAAK,aAAa,EAAE;OACtB,EAAE,UAAU,EAAE;OACd,EAAE,KAAK,gBAAgB,EAAE;AAGhC,MAAM,MAAM,kBAAkB,GAAG,IAAI,CAAC,mBAAmB,EAAE,OAAO,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,CAAC,CAAC;AAE7G,8BAAsB,YAAY,CAAC,IAAI,CAAE,YAAW,aAAa,CAAC,IAAI,CAAC;;IAErE,SAAS,CAAC,OAAO,EAAE,mBAAmB,CAAC;IAEvC,SAAS,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC7B,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC;gBAEZ,MAAM,EAAE,aAAa,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,mBAAmB;IAOlG,QAAQ,CAAC,sBAAsB,IAAI,kBAAkB,GAAG,IAAI;IAE5D,QAAQ,CAAC,iBAAiB,IAAI,IAAI,EAAE;IAEpC,WAAW,IAAI,OAAO;IAMhB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAW3B,SAAS,IAAI,cAAc,CAAC,IAAI,CAAC;IASjC,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,cAAc,CAAC,IAAI,CAAC;CAOtD;AAED;;;;;;;;GAQG;AACH,qBAAa,WAAW,CACpB,SAAS,SAAS,YAAY,CAAC,IAAI,CAAC,EACpC,IAAI,GAAG,UAAU,CAAC,SAAS,CAAC,mBAAmB,CAAC,CAAC,CAAC,MAAM,CAAC,CAE3D,SAAQ,UAAU,CAAC,SAAS,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kBAAkB,CAAC,IAAI,CAAC,EAC9B,OAAO,EAAE,mBAAmB;IAS9B,iBAAiB,IAAI,IAAI,EAAE;IAIlB,WAAW,IAAI,OAAO;IAQ/B,sBAAsB,IAAI,kBAAkB,GAAG,IAAI;CAcpD"}

View File

@@ -0,0 +1,185 @@
"use strict";
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
var _AbstractPage_client;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PageCursor = exports.TokenPage = exports.Page = exports.PagePromise = exports.AbstractPage = void 0;
const tslib_1 = require("../internal/tslib.js");
const error_1 = require("./error.js");
const parse_1 = require("../internal/parse.js");
const api_promise_1 = require("./api-promise.js");
const values_1 = require("../internal/utils/values.js");
class AbstractPage {
constructor(client, response, body, options) {
_AbstractPage_client.set(this, void 0);
tslib_1.__classPrivateFieldSet(this, _AbstractPage_client, client, "f");
this.options = options;
this.response = response;
this.body = body;
}
hasNextPage() {
const items = this.getPaginatedItems();
if (!items.length)
return false;
return this.nextPageRequestOptions() != null;
}
async getNextPage() {
const nextOptions = this.nextPageRequestOptions();
if (!nextOptions) {
throw new error_1.AnthropicError('No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.');
}
return await tslib_1.__classPrivateFieldGet(this, _AbstractPage_client, "f").requestAPIList(this.constructor, nextOptions);
}
async *iterPages() {
let page = this;
yield page;
while (page.hasNextPage()) {
page = await page.getNextPage();
yield page;
}
}
async *[(_AbstractPage_client = new WeakMap(), Symbol.asyncIterator)]() {
for await (const page of this.iterPages()) {
for (const item of page.getPaginatedItems()) {
yield item;
}
}
}
}
exports.AbstractPage = AbstractPage;
/**
* This subclass of Promise will resolve to an instantiated Page once the request completes.
*
* It also implements AsyncIterable to allow auto-paginating iteration on an unawaited list call, eg:
*
* for await (const item of client.items.list()) {
* console.log(item)
* }
*/
class PagePromise extends api_promise_1.APIPromise {
constructor(client, request, Page) {
super(client, request, async (client, props) => new Page(client, props.response, await (0, parse_1.defaultParseResponse)(client, props), props.options));
}
/**
* Allow auto-paginating iteration on an unawaited list call, eg:
*
* for await (const item of client.items.list()) {
* console.log(item)
* }
*/
async *[Symbol.asyncIterator]() {
const page = await this;
for await (const item of page) {
yield item;
}
}
}
exports.PagePromise = PagePromise;
class Page extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.data = body.data || [];
this.has_more = body.has_more || false;
this.first_id = body.first_id || null;
this.last_id = body.last_id || null;
}
getPaginatedItems() {
return this.data ?? [];
}
hasNextPage() {
if (this.has_more === false) {
return false;
}
return super.hasNextPage();
}
nextPageRequestOptions() {
if (this.options.query?.['before_id']) {
// in reverse
const first_id = this.first_id;
if (!first_id) {
return null;
}
return {
...this.options,
query: {
...(0, values_1.maybeObj)(this.options.query),
before_id: first_id,
},
};
}
const cursor = this.last_id;
if (!cursor) {
return null;
}
return {
...this.options,
query: {
...(0, values_1.maybeObj)(this.options.query),
after_id: cursor,
},
};
}
}
exports.Page = Page;
class TokenPage extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.data = body.data || [];
this.has_more = body.has_more || false;
this.next_page = body.next_page || null;
}
getPaginatedItems() {
return this.data ?? [];
}
hasNextPage() {
if (this.has_more === false) {
return false;
}
return super.hasNextPage();
}
nextPageRequestOptions() {
const cursor = this.next_page;
if (!cursor) {
return null;
}
return {
...this.options,
query: {
...(0, values_1.maybeObj)(this.options.query),
page_token: cursor,
},
};
}
}
exports.TokenPage = TokenPage;
class PageCursor extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.data = body.data || [];
this.has_more = body.has_more || false;
this.next_page = body.next_page || null;
}
getPaginatedItems() {
return this.data ?? [];
}
hasNextPage() {
if (this.has_more === false) {
return false;
}
return super.hasNextPage();
}
nextPageRequestOptions() {
const cursor = this.next_page;
if (!cursor) {
return null;
}
return {
...this.options,
query: {
...(0, values_1.maybeObj)(this.options.query),
page: cursor,
},
};
}
}
exports.PageCursor = PageCursor;
//# sourceMappingURL=pagination.js.map

View File

@@ -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,KAAK,CAAC;QACf,CAAC;QAED,OAAO,KAAK,CAAC,WAAW,EAAE,CAAC;IAC7B,CAAC;IAED,sBAAsB;QACpB,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC;QAC9B,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO,IAAI,CAAC;QACd,CAAC;QAED,OAAO;YACL,GAAG,IAAI,CAAC,OAAO;YACf,KAAK,EAAE;gBACL,GAAG,IAAA,iBAAQ,EAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;gBAC/B,IAAI,EAAE,MAAM;aACb;SACF,CAAC;IACJ,CAAC;CACF;AA9CD,gCA8CC"}

View File

@@ -0,0 +1,177 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
var _AbstractPage_client;
import { __classPrivateFieldGet, __classPrivateFieldSet } from "../internal/tslib.mjs";
import { AnthropicError } from "./error.mjs";
import { defaultParseResponse } from "../internal/parse.mjs";
import { APIPromise } from "./api-promise.mjs";
import { maybeObj } from "../internal/utils/values.mjs";
export class AbstractPage {
constructor(client, response, body, options) {
_AbstractPage_client.set(this, void 0);
__classPrivateFieldSet(this, _AbstractPage_client, client, "f");
this.options = options;
this.response = response;
this.body = body;
}
hasNextPage() {
const items = this.getPaginatedItems();
if (!items.length)
return false;
return this.nextPageRequestOptions() != null;
}
async getNextPage() {
const nextOptions = this.nextPageRequestOptions();
if (!nextOptions) {
throw new AnthropicError('No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.');
}
return await __classPrivateFieldGet(this, _AbstractPage_client, "f").requestAPIList(this.constructor, nextOptions);
}
async *iterPages() {
let page = this;
yield page;
while (page.hasNextPage()) {
page = await page.getNextPage();
yield page;
}
}
async *[(_AbstractPage_client = new WeakMap(), Symbol.asyncIterator)]() {
for await (const page of this.iterPages()) {
for (const item of page.getPaginatedItems()) {
yield item;
}
}
}
}
/**
* This subclass of Promise will resolve to an instantiated Page once the request completes.
*
* It also implements AsyncIterable to allow auto-paginating iteration on an unawaited list call, eg:
*
* for await (const item of client.items.list()) {
* console.log(item)
* }
*/
export class PagePromise extends APIPromise {
constructor(client, request, Page) {
super(client, request, async (client, props) => new Page(client, props.response, await defaultParseResponse(client, props), props.options));
}
/**
* Allow auto-paginating iteration on an unawaited list call, eg:
*
* for await (const item of client.items.list()) {
* console.log(item)
* }
*/
async *[Symbol.asyncIterator]() {
const page = await this;
for await (const item of page) {
yield item;
}
}
}
export class Page extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.data = body.data || [];
this.has_more = body.has_more || false;
this.first_id = body.first_id || null;
this.last_id = body.last_id || null;
}
getPaginatedItems() {
return this.data ?? [];
}
hasNextPage() {
if (this.has_more === false) {
return false;
}
return super.hasNextPage();
}
nextPageRequestOptions() {
if (this.options.query?.['before_id']) {
// in reverse
const first_id = this.first_id;
if (!first_id) {
return null;
}
return {
...this.options,
query: {
...maybeObj(this.options.query),
before_id: first_id,
},
};
}
const cursor = this.last_id;
if (!cursor) {
return null;
}
return {
...this.options,
query: {
...maybeObj(this.options.query),
after_id: cursor,
},
};
}
}
export class TokenPage extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.data = body.data || [];
this.has_more = body.has_more || false;
this.next_page = body.next_page || null;
}
getPaginatedItems() {
return this.data ?? [];
}
hasNextPage() {
if (this.has_more === false) {
return false;
}
return super.hasNextPage();
}
nextPageRequestOptions() {
const cursor = this.next_page;
if (!cursor) {
return null;
}
return {
...this.options,
query: {
...maybeObj(this.options.query),
page_token: cursor,
},
};
}
}
export class PageCursor extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.data = body.data || [];
this.has_more = body.has_more || false;
this.next_page = body.next_page || null;
}
getPaginatedItems() {
return this.data ?? [];
}
hasNextPage() {
if (this.has_more === false) {
return false;
}
return super.hasNextPage();
}
nextPageRequestOptions() {
const cursor = this.next_page;
if (!cursor) {
return null;
}
return {
...this.options,
query: {
...maybeObj(this.options.query),
page: cursor,
},
};
}
}
//# sourceMappingURL=pagination.mjs.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"pagination.mjs","sourceRoot":"","sources":["../src/core/pagination.ts"],"names":[],"mappings":"AAAA,sFAAsF;;;OAE/E,EAAE,cAAc,EAAE;OAElB,EAAE,oBAAoB,EAAiB;OAEvC,EAAE,UAAU,EAAE;OAEd,EAAE,QAAQ,EAAE;AAInB,MAAM,OAAgB,YAAY;IAOhC,YAAY,MAAqB,EAAE,QAAkB,EAAE,IAAa,EAAE,OAA4B;QANlG,uCAAuB;QAOrB,uBAAA,IAAI,wBAAW,MAAM,MAAA,CAAC;QACtB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAMD,WAAW;QACT,MAAM,KAAK,GAAG,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACvC,IAAI,CAAC,KAAK,CAAC,MAAM;YAAE,OAAO,KAAK,CAAC;QAChC,OAAO,IAAI,CAAC,sBAAsB,EAAE,IAAI,IAAI,CAAC;IAC/C,CAAC;IAED,KAAK,CAAC,WAAW;QACf,MAAM,WAAW,GAAG,IAAI,CAAC,sBAAsB,EAAE,CAAC;QAClD,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,MAAM,IAAI,cAAc,CACtB,uFAAuF,CACxF,CAAC;QACJ,CAAC;QAED,OAAO,MAAM,uBAAA,IAAI,4BAAQ,CAAC,cAAc,CAAC,IAAI,CAAC,WAAkB,EAAE,WAAW,CAAC,CAAC;IACjF,CAAC;IAED,KAAK,CAAC,CAAC,SAAS;QACd,IAAI,IAAI,GAAS,IAAI,CAAC;QACtB,MAAM,IAAI,CAAC;QACX,OAAO,IAAI,CAAC,WAAW,EAAE,EAAE,CAAC;YAC1B,IAAI,GAAG,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC;YAChC,MAAM,IAAI,CAAC;QACb,CAAC;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AAC;YAC5B,OAAO,KAAK,CAAC;QACf,CAAC;QAED,OAAO,KAAK,CAAC,WAAW,EAAE,CAAC;IAC7B,CAAC;IAED,sBAAsB;QACpB,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC;QAC9B,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO,IAAI,CAAC;QACd,CAAC;QAED,OAAO;YACL,GAAG,IAAI,CAAC,OAAO;YACf,KAAK,EAAE;gBACL,GAAG,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;gBAC/B,IAAI,EAAE,MAAM;aACb;SACF,CAAC;IACJ,CAAC;CACF"}

View File

@@ -0,0 +1,6 @@
import { BaseAnthropic } from "../client.mjs";
export declare abstract class APIResource {
protected _client: BaseAnthropic;
constructor(client: BaseAnthropic);
}
//# sourceMappingURL=resource.d.mts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"resource.d.mts","sourceRoot":"","sources":["../src/core/resource.ts"],"names":[],"mappings":"OAEO,EAAE,aAAa,EAAE;AAExB,8BAAsB,WAAW;IAC/B,SAAS,CAAC,OAAO,EAAE,aAAa,CAAC;gBAErB,MAAM,EAAE,aAAa;CAGlC"}

View File

@@ -0,0 +1,6 @@
import { BaseAnthropic } from "../client.js";
export declare abstract class APIResource {
protected _client: BaseAnthropic;
constructor(client: BaseAnthropic);
}
//# sourceMappingURL=resource.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"resource.d.ts","sourceRoot":"","sources":["../src/core/resource.ts"],"names":[],"mappings":"OAEO,EAAE,aAAa,EAAE;AAExB,8BAAsB,WAAW;IAC/B,SAAS,CAAC,OAAO,EAAE,aAAa,CAAC;gBAErB,MAAM,EAAE,aAAa;CAGlC"}

View File

@@ -0,0 +1,11 @@
"use strict";
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.APIResource = void 0;
class APIResource {
constructor(client) {
this._client = client;
}
}
exports.APIResource = APIResource;
//# sourceMappingURL=resource.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"resource.js","sourceRoot":"","sources":["../src/core/resource.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAItF,MAAsB,WAAW;IAG/B,YAAY,MAAqB;QAC/B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;CACF;AAND,kCAMC"}

View File

@@ -0,0 +1,7 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
export class APIResource {
constructor(client) {
this._client = client;
}
}
//# sourceMappingURL=resource.mjs.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"resource.mjs","sourceRoot":"","sources":["../src/core/resource.ts"],"names":[],"mappings":"AAAA,sFAAsF;AAItF,MAAM,OAAgB,WAAW;IAG/B,YAAY,MAAqB;QAC/B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;CACF"}

View File

@@ -0,0 +1,33 @@
import { type ReadableStream } from "../internal/shim-types.mjs";
import type { BaseAnthropic } from "../client.mjs";
export type ServerSentEvent = {
event: string | null;
data: string;
raw: string[];
};
export declare class Stream<Item> implements AsyncIterable<Item> {
#private;
private iterator;
controller: AbortController;
constructor(iterator: () => AsyncIterator<Item>, controller: AbortController, client?: BaseAnthropic);
static fromSSEResponse<Item>(response: Response, controller: AbortController, client?: BaseAnthropic): Stream<Item>;
/**
* Generates a Stream from a newline-separated ReadableStream
* where each item is a JSON value.
*/
static fromReadableStream<Item>(readableStream: ReadableStream, controller: AbortController, client?: BaseAnthropic): Stream<Item>;
[Symbol.asyncIterator](): AsyncIterator<Item>;
/**
* Splits the stream into two streams which can be
* independently read from at different speeds.
*/
tee(): [Stream<Item>, Stream<Item>];
/**
* Converts this stream to a newline-separated ReadableStream of
* JSON stringified values in the stream
* which can be turned back into a Stream with `Stream.fromReadableStream()`.
*/
toReadableStream(): ReadableStream;
}
export declare function _iterSSEMessages(response: Response, controller: AbortController): AsyncGenerator<ServerSentEvent, void, unknown>;
//# sourceMappingURL=streaming.d.mts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"streaming.d.mts","sourceRoot":"","sources":["../src/core/streaming.ts"],"names":[],"mappings":"OACO,EAAE,KAAK,cAAc,EAAE;OAQvB,KAAK,EAAE,aAAa,EAAE;AAO7B,MAAM,MAAM,eAAe,GAAG;IAC5B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,EAAE,CAAC;CACf,CAAC;AAEF,qBAAa,MAAM,CAAC,IAAI,CAAE,YAAW,aAAa,CAAC,IAAI,CAAC;;IAKpD,OAAO,CAAC,QAAQ;IAJlB,UAAU,EAAE,eAAe,CAAC;gBAIlB,QAAQ,EAAE,MAAM,aAAa,CAAC,IAAI,CAAC,EAC3C,UAAU,EAAE,eAAe,EAC3B,MAAM,CAAC,EAAE,aAAa;IAMxB,MAAM,CAAC,eAAe,CAAC,IAAI,EACzB,QAAQ,EAAE,QAAQ,EAClB,UAAU,EAAE,eAAe,EAC3B,MAAM,CAAC,EAAE,aAAa,GACrB,MAAM,CAAC,IAAI,CAAC;IA+Df;;;OAGG;IACH,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAC5B,cAAc,EAAE,cAAc,EAC9B,UAAU,EAAE,eAAe,EAC3B,MAAM,CAAC,EAAE,aAAa,GACrB,MAAM,CAAC,IAAI,CAAC;IA2Cf,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,aAAa,CAAC,IAAI,CAAC;IAI7C;;;OAGG;IACH,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IAwBnC;;;;OAIG;IACH,gBAAgB,IAAI,cAAc;CAyBnC;AAED,wBAAuB,gBAAgB,CACrC,QAAQ,EAAE,QAAQ,EAClB,UAAU,EAAE,eAAe,GAC1B,cAAc,CAAC,eAAe,EAAE,IAAI,EAAE,OAAO,CAAC,CA6BhD"}

View File

@@ -0,0 +1,33 @@
import { type ReadableStream } from "../internal/shim-types.js";
import type { BaseAnthropic } from "../client.js";
export type ServerSentEvent = {
event: string | null;
data: string;
raw: string[];
};
export declare class Stream<Item> implements AsyncIterable<Item> {
#private;
private iterator;
controller: AbortController;
constructor(iterator: () => AsyncIterator<Item>, controller: AbortController, client?: BaseAnthropic);
static fromSSEResponse<Item>(response: Response, controller: AbortController, client?: BaseAnthropic): Stream<Item>;
/**
* Generates a Stream from a newline-separated ReadableStream
* where each item is a JSON value.
*/
static fromReadableStream<Item>(readableStream: ReadableStream, controller: AbortController, client?: BaseAnthropic): Stream<Item>;
[Symbol.asyncIterator](): AsyncIterator<Item>;
/**
* Splits the stream into two streams which can be
* independently read from at different speeds.
*/
tee(): [Stream<Item>, Stream<Item>];
/**
* Converts this stream to a newline-separated ReadableStream of
* JSON stringified values in the stream
* which can be turned back into a Stream with `Stream.fromReadableStream()`.
*/
toReadableStream(): ReadableStream;
}
export declare function _iterSSEMessages(response: Response, controller: AbortController): AsyncGenerator<ServerSentEvent, void, unknown>;
//# sourceMappingURL=streaming.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["../src/core/streaming.ts"],"names":[],"mappings":"OACO,EAAE,KAAK,cAAc,EAAE;OAQvB,KAAK,EAAE,aAAa,EAAE;AAO7B,MAAM,MAAM,eAAe,GAAG;IAC5B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,EAAE,CAAC;CACf,CAAC;AAEF,qBAAa,MAAM,CAAC,IAAI,CAAE,YAAW,aAAa,CAAC,IAAI,CAAC;;IAKpD,OAAO,CAAC,QAAQ;IAJlB,UAAU,EAAE,eAAe,CAAC;gBAIlB,QAAQ,EAAE,MAAM,aAAa,CAAC,IAAI,CAAC,EAC3C,UAAU,EAAE,eAAe,EAC3B,MAAM,CAAC,EAAE,aAAa;IAMxB,MAAM,CAAC,eAAe,CAAC,IAAI,EACzB,QAAQ,EAAE,QAAQ,EAClB,UAAU,EAAE,eAAe,EAC3B,MAAM,CAAC,EAAE,aAAa,GACrB,MAAM,CAAC,IAAI,CAAC;IA+Df;;;OAGG;IACH,MAAM,CAAC,kBAAkB,CAAC,IAAI,EAC5B,cAAc,EAAE,cAAc,EAC9B,UAAU,EAAE,eAAe,EAC3B,MAAM,CAAC,EAAE,aAAa,GACrB,MAAM,CAAC,IAAI,CAAC;IA2Cf,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,aAAa,CAAC,IAAI,CAAC;IAI7C;;;OAGG;IACH,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IAwBnC;;;;OAIG;IACH,gBAAgB,IAAI,cAAc;CAyBnC;AAED,wBAAuB,gBAAgB,CACrC,QAAQ,EAAE,QAAQ,EAClB,UAAU,EAAE,eAAe,GAC1B,cAAc,CAAC,eAAe,EAAE,IAAI,EAAE,OAAO,CAAC,CA6BhD"}

View File

@@ -0,0 +1,290 @@
"use strict";
var _Stream_client;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Stream = void 0;
exports._iterSSEMessages = _iterSSEMessages;
const tslib_1 = require("../internal/tslib.js");
const error_1 = require("./error.js");
const shims_1 = require("../internal/shims.js");
const line_1 = require("../internal/decoders/line.js");
const shims_2 = require("../internal/shims.js");
const errors_1 = require("../internal/errors.js");
const values_1 = require("../internal/utils/values.js");
const bytes_1 = require("../internal/utils/bytes.js");
const log_1 = require("../internal/utils/log.js");
const error_2 = require("./error.js");
class Stream {
constructor(iterator, controller, client) {
this.iterator = iterator;
_Stream_client.set(this, void 0);
this.controller = controller;
tslib_1.__classPrivateFieldSet(this, _Stream_client, client, "f");
}
static fromSSEResponse(response, controller, client) {
let consumed = false;
const logger = client ? (0, log_1.loggerFor)(client) : console;
async function* iterator() {
if (consumed) {
throw new error_1.AnthropicError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
}
consumed = true;
let done = false;
try {
for await (const sse of _iterSSEMessages(response, controller)) {
if (sse.event === 'completion') {
try {
yield JSON.parse(sse.data);
}
catch (e) {
logger.error(`Could not parse message into JSON:`, sse.data);
logger.error(`From chunk:`, sse.raw);
throw e;
}
}
if (sse.event === 'message_start' ||
sse.event === 'message_delta' ||
sse.event === 'message_stop' ||
sse.event === 'content_block_start' ||
sse.event === 'content_block_delta' ||
sse.event === 'content_block_stop') {
try {
yield JSON.parse(sse.data);
}
catch (e) {
logger.error(`Could not parse message into JSON:`, sse.data);
logger.error(`From chunk:`, sse.raw);
throw e;
}
}
if (sse.event === 'ping') {
continue;
}
if (sse.event === 'error') {
const body = (0, values_1.safeJSON)(sse.data) ?? sse.data;
const type = body?.error?.type;
throw new error_2.APIError(undefined, body, undefined, response.headers, type);
}
}
done = true;
}
catch (e) {
// If the user calls `stream.controller.abort()`, we should exit without throwing.
if ((0, errors_1.isAbortError)(e))
return;
throw e;
}
finally {
// If the user `break`s, abort the ongoing request.
if (!done)
controller.abort();
}
}
return new Stream(iterator, controller, client);
}
/**
* Generates a Stream from a newline-separated ReadableStream
* where each item is a JSON value.
*/
static fromReadableStream(readableStream, controller, client) {
let consumed = false;
async function* iterLines() {
const lineDecoder = new line_1.LineDecoder();
const iter = (0, shims_2.ReadableStreamToAsyncIterable)(readableStream);
for await (const chunk of iter) {
for (const line of lineDecoder.decode(chunk)) {
yield line;
}
}
for (const line of lineDecoder.flush()) {
yield line;
}
}
async function* iterator() {
if (consumed) {
throw new error_1.AnthropicError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
}
consumed = true;
let done = false;
try {
for await (const line of iterLines()) {
if (done)
continue;
if (line)
yield JSON.parse(line);
}
done = true;
}
catch (e) {
// If the user calls `stream.controller.abort()`, we should exit without throwing.
if ((0, errors_1.isAbortError)(e))
return;
throw e;
}
finally {
// If the user `break`s, abort the ongoing request.
if (!done)
controller.abort();
}
}
return new Stream(iterator, controller, client);
}
[(_Stream_client = new WeakMap(), Symbol.asyncIterator)]() {
return this.iterator();
}
/**
* Splits the stream into two streams which can be
* independently read from at different speeds.
*/
tee() {
const left = [];
const right = [];
const iterator = this.iterator();
const teeIterator = (queue) => {
return {
next: () => {
if (queue.length === 0) {
const result = iterator.next();
left.push(result);
right.push(result);
}
return queue.shift();
},
};
};
return [
new Stream(() => teeIterator(left), this.controller, tslib_1.__classPrivateFieldGet(this, _Stream_client, "f")),
new Stream(() => teeIterator(right), this.controller, tslib_1.__classPrivateFieldGet(this, _Stream_client, "f")),
];
}
/**
* Converts this stream to a newline-separated ReadableStream of
* JSON stringified values in the stream
* which can be turned back into a Stream with `Stream.fromReadableStream()`.
*/
toReadableStream() {
const self = this;
let iter;
return (0, shims_1.makeReadableStream)({
async start() {
iter = self[Symbol.asyncIterator]();
},
async pull(ctrl) {
try {
const { value, done } = await iter.next();
if (done)
return ctrl.close();
const bytes = (0, bytes_1.encodeUTF8)(JSON.stringify(value) + '\n');
ctrl.enqueue(bytes);
}
catch (err) {
ctrl.error(err);
}
},
async cancel() {
await iter.return?.();
},
});
}
}
exports.Stream = Stream;
async function* _iterSSEMessages(response, controller) {
if (!response.body) {
controller.abort();
if (typeof globalThis.navigator !== 'undefined' &&
globalThis.navigator.product === 'ReactNative') {
throw new error_1.AnthropicError(`The default react-native fetch implementation does not support streaming. Please use expo/fetch: https://docs.expo.dev/versions/latest/sdk/expo/#expofetch-api`);
}
throw new error_1.AnthropicError(`Attempted to iterate over a response with no body`);
}
const sseDecoder = new SSEDecoder();
const lineDecoder = new line_1.LineDecoder();
const iter = (0, shims_2.ReadableStreamToAsyncIterable)(response.body);
for await (const sseChunk of iterSSEChunks(iter)) {
for (const line of lineDecoder.decode(sseChunk)) {
const sse = sseDecoder.decode(line);
if (sse)
yield sse;
}
}
for (const line of lineDecoder.flush()) {
const sse = sseDecoder.decode(line);
if (sse)
yield sse;
}
}
/**
* Given an async iterable iterator, iterates over it and yields full
* SSE chunks, i.e. yields when a double new-line is encountered.
*/
async function* iterSSEChunks(iterator) {
let data = new Uint8Array();
for await (const chunk of iterator) {
if (chunk == null) {
continue;
}
const binaryChunk = chunk instanceof ArrayBuffer ? new Uint8Array(chunk)
: typeof chunk === 'string' ? (0, bytes_1.encodeUTF8)(chunk)
: chunk;
let newData = new Uint8Array(data.length + binaryChunk.length);
newData.set(data);
newData.set(binaryChunk, data.length);
data = newData;
let patternIndex;
while ((patternIndex = (0, line_1.findDoubleNewlineIndex)(data)) !== -1) {
yield data.slice(0, patternIndex);
data = data.slice(patternIndex);
}
}
if (data.length > 0) {
yield data;
}
}
class SSEDecoder {
constructor() {
this.event = null;
this.data = [];
this.chunks = [];
}
decode(line) {
if (line.endsWith('\r')) {
line = line.substring(0, line.length - 1);
}
if (!line) {
// empty line and we didn't previously encounter any messages
if (!this.event && !this.data.length)
return null;
const sse = {
event: this.event,
data: this.data.join('\n'),
raw: this.chunks,
};
this.event = null;
this.data = [];
this.chunks = [];
return sse;
}
this.chunks.push(line);
if (line.startsWith(':')) {
return null;
}
let [fieldname, _, value] = partition(line, ':');
if (value.startsWith(' ')) {
value = value.substring(1);
}
if (fieldname === 'event') {
this.event = value;
}
else if (fieldname === 'data') {
this.data.push(value);
}
return null;
}
}
function partition(str, delimiter) {
const index = str.indexOf(delimiter);
if (index !== -1) {
return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];
}
return [str, '', ''];
}
//# sourceMappingURL=streaming.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,285 @@
var _Stream_client;
import { __classPrivateFieldGet, __classPrivateFieldSet } from "../internal/tslib.mjs";
import { AnthropicError } from "./error.mjs";
import { makeReadableStream } from "../internal/shims.mjs";
import { findDoubleNewlineIndex, LineDecoder } from "../internal/decoders/line.mjs";
import { ReadableStreamToAsyncIterable } from "../internal/shims.mjs";
import { isAbortError } from "../internal/errors.mjs";
import { safeJSON } from "../internal/utils/values.mjs";
import { encodeUTF8 } from "../internal/utils/bytes.mjs";
import { loggerFor } from "../internal/utils/log.mjs";
import { APIError } from "./error.mjs";
export class Stream {
constructor(iterator, controller, client) {
this.iterator = iterator;
_Stream_client.set(this, void 0);
this.controller = controller;
__classPrivateFieldSet(this, _Stream_client, client, "f");
}
static fromSSEResponse(response, controller, client) {
let consumed = false;
const logger = client ? loggerFor(client) : console;
async function* iterator() {
if (consumed) {
throw new AnthropicError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
}
consumed = true;
let done = false;
try {
for await (const sse of _iterSSEMessages(response, controller)) {
if (sse.event === 'completion') {
try {
yield JSON.parse(sse.data);
}
catch (e) {
logger.error(`Could not parse message into JSON:`, sse.data);
logger.error(`From chunk:`, sse.raw);
throw e;
}
}
if (sse.event === 'message_start' ||
sse.event === 'message_delta' ||
sse.event === 'message_stop' ||
sse.event === 'content_block_start' ||
sse.event === 'content_block_delta' ||
sse.event === 'content_block_stop') {
try {
yield JSON.parse(sse.data);
}
catch (e) {
logger.error(`Could not parse message into JSON:`, sse.data);
logger.error(`From chunk:`, sse.raw);
throw e;
}
}
if (sse.event === 'ping') {
continue;
}
if (sse.event === 'error') {
const body = safeJSON(sse.data) ?? sse.data;
const type = body?.error?.type;
throw new APIError(undefined, body, undefined, response.headers, type);
}
}
done = true;
}
catch (e) {
// If the user calls `stream.controller.abort()`, we should exit without throwing.
if (isAbortError(e))
return;
throw e;
}
finally {
// If the user `break`s, abort the ongoing request.
if (!done)
controller.abort();
}
}
return new Stream(iterator, controller, client);
}
/**
* Generates a Stream from a newline-separated ReadableStream
* where each item is a JSON value.
*/
static fromReadableStream(readableStream, controller, client) {
let consumed = false;
async function* iterLines() {
const lineDecoder = new LineDecoder();
const iter = ReadableStreamToAsyncIterable(readableStream);
for await (const chunk of iter) {
for (const line of lineDecoder.decode(chunk)) {
yield line;
}
}
for (const line of lineDecoder.flush()) {
yield line;
}
}
async function* iterator() {
if (consumed) {
throw new AnthropicError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
}
consumed = true;
let done = false;
try {
for await (const line of iterLines()) {
if (done)
continue;
if (line)
yield JSON.parse(line);
}
done = true;
}
catch (e) {
// If the user calls `stream.controller.abort()`, we should exit without throwing.
if (isAbortError(e))
return;
throw e;
}
finally {
// If the user `break`s, abort the ongoing request.
if (!done)
controller.abort();
}
}
return new Stream(iterator, controller, client);
}
[(_Stream_client = new WeakMap(), Symbol.asyncIterator)]() {
return this.iterator();
}
/**
* Splits the stream into two streams which can be
* independently read from at different speeds.
*/
tee() {
const left = [];
const right = [];
const iterator = this.iterator();
const teeIterator = (queue) => {
return {
next: () => {
if (queue.length === 0) {
const result = iterator.next();
left.push(result);
right.push(result);
}
return queue.shift();
},
};
};
return [
new Stream(() => teeIterator(left), this.controller, __classPrivateFieldGet(this, _Stream_client, "f")),
new Stream(() => teeIterator(right), this.controller, __classPrivateFieldGet(this, _Stream_client, "f")),
];
}
/**
* Converts this stream to a newline-separated ReadableStream of
* JSON stringified values in the stream
* which can be turned back into a Stream with `Stream.fromReadableStream()`.
*/
toReadableStream() {
const self = this;
let iter;
return makeReadableStream({
async start() {
iter = self[Symbol.asyncIterator]();
},
async pull(ctrl) {
try {
const { value, done } = await iter.next();
if (done)
return ctrl.close();
const bytes = encodeUTF8(JSON.stringify(value) + '\n');
ctrl.enqueue(bytes);
}
catch (err) {
ctrl.error(err);
}
},
async cancel() {
await iter.return?.();
},
});
}
}
export async function* _iterSSEMessages(response, controller) {
if (!response.body) {
controller.abort();
if (typeof globalThis.navigator !== 'undefined' &&
globalThis.navigator.product === 'ReactNative') {
throw new AnthropicError(`The default react-native fetch implementation does not support streaming. Please use expo/fetch: https://docs.expo.dev/versions/latest/sdk/expo/#expofetch-api`);
}
throw new AnthropicError(`Attempted to iterate over a response with no body`);
}
const sseDecoder = new SSEDecoder();
const lineDecoder = new LineDecoder();
const iter = ReadableStreamToAsyncIterable(response.body);
for await (const sseChunk of iterSSEChunks(iter)) {
for (const line of lineDecoder.decode(sseChunk)) {
const sse = sseDecoder.decode(line);
if (sse)
yield sse;
}
}
for (const line of lineDecoder.flush()) {
const sse = sseDecoder.decode(line);
if (sse)
yield sse;
}
}
/**
* Given an async iterable iterator, iterates over it and yields full
* SSE chunks, i.e. yields when a double new-line is encountered.
*/
async function* iterSSEChunks(iterator) {
let data = new Uint8Array();
for await (const chunk of iterator) {
if (chunk == null) {
continue;
}
const binaryChunk = chunk instanceof ArrayBuffer ? new Uint8Array(chunk)
: typeof chunk === 'string' ? encodeUTF8(chunk)
: chunk;
let newData = new Uint8Array(data.length + binaryChunk.length);
newData.set(data);
newData.set(binaryChunk, data.length);
data = newData;
let patternIndex;
while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
yield data.slice(0, patternIndex);
data = data.slice(patternIndex);
}
}
if (data.length > 0) {
yield data;
}
}
class SSEDecoder {
constructor() {
this.event = null;
this.data = [];
this.chunks = [];
}
decode(line) {
if (line.endsWith('\r')) {
line = line.substring(0, line.length - 1);
}
if (!line) {
// empty line and we didn't previously encounter any messages
if (!this.event && !this.data.length)
return null;
const sse = {
event: this.event,
data: this.data.join('\n'),
raw: this.chunks,
};
this.event = null;
this.data = [];
this.chunks = [];
return sse;
}
this.chunks.push(line);
if (line.startsWith(':')) {
return null;
}
let [fieldname, _, value] = partition(line, ':');
if (value.startsWith(' ')) {
value = value.substring(1);
}
if (fieldname === 'event') {
this.event = value;
}
else if (fieldname === 'data') {
this.data.push(value);
}
return null;
}
}
function partition(str, delimiter) {
const index = str.indexOf(delimiter);
if (index !== -1) {
return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];
}
return [str, '', ''];
}
//# sourceMappingURL=streaming.mjs.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,3 @@
export { type Uploadable } from "../internal/uploads.mjs";
export { toFile, type ToFileInput } from "../internal/to-file.mjs";
//# sourceMappingURL=uploads.d.mts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"uploads.d.mts","sourceRoot":"","sources":["../src/core/uploads.ts"],"names":[],"mappings":"OAAO,EAAE,KAAK,UAAU,EAAE;OACnB,EAAE,MAAM,EAAE,KAAK,WAAW,EAAE"}

View File

@@ -0,0 +1,3 @@
export { type Uploadable } from "../internal/uploads.js";
export { toFile, type ToFileInput } from "../internal/to-file.js";
//# sourceMappingURL=uploads.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"uploads.d.ts","sourceRoot":"","sources":["../src/core/uploads.ts"],"names":[],"mappings":"OAAO,EAAE,KAAK,UAAU,EAAE;OACnB,EAAE,MAAM,EAAE,KAAK,WAAW,EAAE"}

View File

@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toFile = void 0;
var to_file_1 = require("../internal/to-file.js");
Object.defineProperty(exports, "toFile", { enumerable: true, get: function () { return to_file_1.toFile; } });
//# sourceMappingURL=uploads.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"uploads.js","sourceRoot":"","sources":["../src/core/uploads.ts"],"names":[],"mappings":";;;AACA,kDAA+D;AAAtD,iGAAA,MAAM,OAAA"}

View File

@@ -0,0 +1,2 @@
export { toFile } from "../internal/to-file.mjs";
//# sourceMappingURL=uploads.mjs.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"uploads.mjs","sourceRoot":"","sources":["../src/core/uploads.ts"],"names":[],"mappings":"OACO,EAAE,MAAM,EAAoB"}

View File

@@ -0,0 +1,2 @@
export * from "./core/error.mjs";
//# sourceMappingURL=error.d.mts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"error.d.mts","sourceRoot":"","sources":["src/error.ts"],"names":[],"mappings":""}

View File

@@ -0,0 +1,2 @@
export * from "./core/error.js";
//# sourceMappingURL=error.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["src/error.ts"],"names":[],"mappings":""}

View File

@@ -0,0 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("./internal/tslib.js");
/** @deprecated Import from ./core/error instead */
tslib_1.__exportStar(require("./core/error.js"), exports);
//# sourceMappingURL=error.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"error.js","sourceRoot":"","sources":["src/error.ts"],"names":[],"mappings":";;;AAAA,mDAAmD;AACnD,0DAA6B"}

View File

@@ -0,0 +1,2 @@
export * from "./core/error.mjs";
//# sourceMappingURL=error.mjs.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"error.mjs","sourceRoot":"","sources":["src/error.ts"],"names":[],"mappings":""}

View File

@@ -0,0 +1,31 @@
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { Promisable, BetaRunnableTool } from "../../lib/tools/BetaRunnableTool.mjs";
import { BetaToolResultContentBlockParam } from "../../resources/beta.mjs";
import { AutoParseableBetaOutputFormat } from "../../lib/beta-parser.mjs";
type NoInfer<T> = T extends infer R ? R : never;
/**
* Creates a Tool with a provided JSON schema that can be passed
* to the `.toolRunner()` method. The schema is used to automatically validate
* the input arguments for the tool.
*/
export declare function betaTool<const Schema extends Exclude<JSONSchema, boolean> & {
type: 'object';
}>(options: {
name: string;
inputSchema: Schema;
description: string;
run: (args: NoInfer<FromSchema<Schema>>) => Promisable<string | Array<BetaToolResultContentBlockParam>>;
}): BetaRunnableTool<NoInfer<FromSchema<Schema>>>;
/**
* Creates a JSON schema output format object from the given JSON schema.
* If this is passed to the `.parse()` method then the response message will contain a
* `.parsed_output` property that is the result of parsing the content with the given JSON schema.
*
*/
export declare function betaJSONSchemaOutputFormat<const Schema extends Exclude<JSONSchema, boolean> & {
type: 'object';
}>(jsonSchema: Schema, options?: {
transform?: boolean;
}): AutoParseableBetaOutputFormat<NoInfer<FromSchema<Schema>>>;
export {};
//# sourceMappingURL=json-schema.d.mts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"json-schema.d.mts","sourceRoot":"","sources":["../../src/helpers/beta/json-schema.ts"],"names":[],"mappings":"OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,mBAAmB;OACnD,EAAE,UAAU,EAAE,gBAAgB,EAAE;OAChC,EAAE,+BAA+B,EAAE;OACnC,EAAE,6BAA6B,EAAE;AAIxC,KAAK,OAAO,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAEhD;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,KAAK,CAAC,MAAM,SAAS,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,GAAG;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,EAAE,OAAO,EAAE;IACxG,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,GAAG,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,KAAK,UAAU,CAAC,MAAM,GAAG,KAAK,CAAC,+BAA+B,CAAC,CAAC,CAAC;CACzG,GAAG,gBAAgB,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAehD;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CACxC,KAAK,CAAC,MAAM,SAAS,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,GAAG;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,EAEtE,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE;IACR,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,GACA,6BAA6B,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAyB5D"}

View File

@@ -0,0 +1,31 @@
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { Promisable, BetaRunnableTool } from "../../lib/tools/BetaRunnableTool.js";
import { BetaToolResultContentBlockParam } from "../../resources/beta.js";
import { AutoParseableBetaOutputFormat } from "../../lib/beta-parser.js";
type NoInfer<T> = T extends infer R ? R : never;
/**
* Creates a Tool with a provided JSON schema that can be passed
* to the `.toolRunner()` method. The schema is used to automatically validate
* the input arguments for the tool.
*/
export declare function betaTool<const Schema extends Exclude<JSONSchema, boolean> & {
type: 'object';
}>(options: {
name: string;
inputSchema: Schema;
description: string;
run: (args: NoInfer<FromSchema<Schema>>) => Promisable<string | Array<BetaToolResultContentBlockParam>>;
}): BetaRunnableTool<NoInfer<FromSchema<Schema>>>;
/**
* Creates a JSON schema output format object from the given JSON schema.
* If this is passed to the `.parse()` method then the response message will contain a
* `.parsed_output` property that is the result of parsing the content with the given JSON schema.
*
*/
export declare function betaJSONSchemaOutputFormat<const Schema extends Exclude<JSONSchema, boolean> & {
type: 'object';
}>(jsonSchema: Schema, options?: {
transform?: boolean;
}): AutoParseableBetaOutputFormat<NoInfer<FromSchema<Schema>>>;
export {};
//# sourceMappingURL=json-schema.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"json-schema.d.ts","sourceRoot":"","sources":["../../src/helpers/beta/json-schema.ts"],"names":[],"mappings":"OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,mBAAmB;OACnD,EAAE,UAAU,EAAE,gBAAgB,EAAE;OAChC,EAAE,+BAA+B,EAAE;OACnC,EAAE,6BAA6B,EAAE;AAIxC,KAAK,OAAO,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAEhD;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,KAAK,CAAC,MAAM,SAAS,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,GAAG;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,EAAE,OAAO,EAAE;IACxG,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,GAAG,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,KAAK,UAAU,CAAC,MAAM,GAAG,KAAK,CAAC,+BAA+B,CAAC,CAAC,CAAC;CACzG,GAAG,gBAAgB,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAehD;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CACxC,KAAK,CAAC,MAAM,SAAS,OAAO,CAAC,UAAU,EAAE,OAAO,CAAC,GAAG;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,EAEtE,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE;IACR,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,GACA,6BAA6B,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAyB5D"}

View File

@@ -0,0 +1,56 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.betaTool = betaTool;
exports.betaJSONSchemaOutputFormat = betaJSONSchemaOutputFormat;
const __1 = require("../../index.js");
const transform_json_schema_1 = require("../../lib/transform-json-schema.js");
/**
* Creates a Tool with a provided JSON schema that can be passed
* to the `.toolRunner()` method. The schema is used to automatically validate
* the input arguments for the tool.
*/
function betaTool(options) {
if (options.inputSchema.type !== 'object') {
throw new Error(`JSON schema for tool "${options.name}" must be an object, but got ${options.inputSchema.type}`);
}
return {
type: 'custom',
name: options.name,
input_schema: options.inputSchema,
description: options.description,
run: options.run,
parse: (content) => content,
};
}
/**
* Creates a JSON schema output format object from the given JSON schema.
* If this is passed to the `.parse()` method then the response message will contain a
* `.parsed_output` property that is the result of parsing the content with the given JSON schema.
*
*/
function betaJSONSchemaOutputFormat(jsonSchema, options) {
if (jsonSchema.type !== 'object') {
throw new Error(`JSON schema for tool must be an object, but got ${jsonSchema.type}`);
}
const transform = options?.transform ?? true;
if (transform) {
// todo: doing this is arguably necessary, but it does change the schema the user passed in
// so I'm not sure how we should handle that
jsonSchema = (0, transform_json_schema_1.transformJSONSchema)(jsonSchema);
}
return {
type: 'json_schema',
schema: {
...jsonSchema,
},
parse: (content) => {
try {
return JSON.parse(content);
}
catch (error) {
throw new __1.AnthropicError(`Failed to parse structured output: ${error}`);
}
},
};
}
//# sourceMappingURL=json-schema.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"json-schema.js","sourceRoot":"","sources":["../../src/helpers/beta/json-schema.ts"],"names":[],"mappings":";;AAcA,4BAoBC;AAQD,gEAgCC;AAtED,sCAAuC;AACvC,8EAAsE;AAItE;;;;GAIG;AACH,SAAgB,QAAQ,CAAyE,OAKhG;IACC,IAAI,OAAO,CAAC,WAAW,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC1C,MAAM,IAAI,KAAK,CACb,yBAAyB,OAAO,CAAC,IAAI,gCAAgC,OAAO,CAAC,WAAW,CAAC,IAAI,EAAE,CAChG,CAAC;IACJ,CAAC;IAED,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,YAAY,EAAE,OAAO,CAAC,WAAW;QACjC,WAAW,EAAE,OAAO,CAAC,WAAW;QAChC,GAAG,EAAE,OAAO,CAAC,GAAG;QAChB,KAAK,EAAE,CAAC,OAAgB,EAAE,EAAE,CAAC,OAA6B;KACpD,CAAC;AACX,CAAC;AAED;;;;;GAKG;AACH,SAAgB,0BAA0B,CAGxC,UAAkB,EAClB,OAEC;IAED,IAAI,UAAU,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CAAC,mDAAmD,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC;IACxF,CAAC;IAED,MAAM,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,IAAI,CAAC;IAC7C,IAAI,SAAS,EAAE,CAAC;QACd,2FAA2F;QAC3F,4CAA4C;QAC5C,UAAU,GAAG,IAAA,2CAAmB,EAAC,UAAU,CAAW,CAAC;IACzD,CAAC;IAED,OAAO;QACL,IAAI,EAAE,aAAa;QACnB,MAAM,EAAE;YACN,GAAG,UAAU;SACd;QACD,KAAK,EAAE,CAAC,OAAO,EAAE,EAAE;YACjB,IAAI,CAAC;gBACH,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAC7B,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,IAAI,kBAAc,CAAC,sCAAsC,KAAK,EAAE,CAAC,CAAC;YAC1E,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}

View File

@@ -0,0 +1,52 @@
import { AnthropicError } from "../../index.mjs";
import { transformJSONSchema } from "../../lib/transform-json-schema.mjs";
/**
* Creates a Tool with a provided JSON schema that can be passed
* to the `.toolRunner()` method. The schema is used to automatically validate
* the input arguments for the tool.
*/
export function betaTool(options) {
if (options.inputSchema.type !== 'object') {
throw new Error(`JSON schema for tool "${options.name}" must be an object, but got ${options.inputSchema.type}`);
}
return {
type: 'custom',
name: options.name,
input_schema: options.inputSchema,
description: options.description,
run: options.run,
parse: (content) => content,
};
}
/**
* Creates a JSON schema output format object from the given JSON schema.
* If this is passed to the `.parse()` method then the response message will contain a
* `.parsed_output` property that is the result of parsing the content with the given JSON schema.
*
*/
export function betaJSONSchemaOutputFormat(jsonSchema, options) {
if (jsonSchema.type !== 'object') {
throw new Error(`JSON schema for tool must be an object, but got ${jsonSchema.type}`);
}
const transform = options?.transform ?? true;
if (transform) {
// todo: doing this is arguably necessary, but it does change the schema the user passed in
// so I'm not sure how we should handle that
jsonSchema = transformJSONSchema(jsonSchema);
}
return {
type: 'json_schema',
schema: {
...jsonSchema,
},
parse: (content) => {
try {
return JSON.parse(content);
}
catch (error) {
throw new AnthropicError(`Failed to parse structured output: ${error}`);
}
},
};
}
//# sourceMappingURL=json-schema.mjs.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"json-schema.mjs","sourceRoot":"","sources":["../../src/helpers/beta/json-schema.ts"],"names":[],"mappings":"OAIO,EAAE,cAAc,EAAE;OAClB,EAAE,mBAAmB,EAAE;AAI9B;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAyE,OAKhG;IACC,IAAI,OAAO,CAAC,WAAW,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC1C,MAAM,IAAI,KAAK,CACb,yBAAyB,OAAO,CAAC,IAAI,gCAAgC,OAAO,CAAC,WAAW,CAAC,IAAI,EAAE,CAChG,CAAC;IACJ,CAAC;IAED,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,YAAY,EAAE,OAAO,CAAC,WAAW;QACjC,WAAW,EAAE,OAAO,CAAC,WAAW;QAChC,GAAG,EAAE,OAAO,CAAC,GAAG;QAChB,KAAK,EAAE,CAAC,OAAgB,EAAE,EAAE,CAAC,OAA6B;KACpD,CAAC;AACX,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,0BAA0B,CAGxC,UAAkB,EAClB,OAEC;IAED,IAAI,UAAU,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CAAC,mDAAmD,UAAU,CAAC,IAAI,EAAE,CAAC,CAAC;IACxF,CAAC;IAED,MAAM,SAAS,GAAG,OAAO,EAAE,SAAS,IAAI,IAAI,CAAC;IAC7C,IAAI,SAAS,EAAE,CAAC;QACd,2FAA2F;QAC3F,4CAA4C;QAC5C,UAAU,GAAG,mBAAmB,CAAC,UAAU,CAAW,CAAC;IACzD,CAAC;IAED,OAAO;QACL,IAAI,EAAE,aAAa;QACnB,MAAM,EAAE;YACN,GAAG,UAAU;SACd;QACD,KAAK,EAAE,CAAC,OAAO,EAAE,EAAE;YACjB,IAAI,CAAC;gBACH,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAC7B,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,IAAI,cAAc,CAAC,sCAAsC,KAAK,EAAE,CAAC,CAAC;YAC1E,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}

View File

@@ -0,0 +1,303 @@
/**
* Helper functions for integrating MCP (Model Context Protocol) SDK types
* with the Anthropic SDK.
*
* These helpers reduce boilerplate when converting between MCP types and
* Anthropic API types. The interfaces defined here use TypeScript's structural
* typing to match MCP SDK types without requiring a direct dependency.
*/
import { BetaRunnableTool } from "../../lib/tools/BetaRunnableTool.mjs";
import { BetaTool, BetaMessageParam, BetaTextBlockParam, BetaImageBlockParam, BetaRequestDocumentBlock } from "../../resources/beta.mjs";
import { SDK_HELPER_SYMBOL, collectStainlessHelpers, stainlessHelperHeader } from "../../lib/stainless-helper-header.mjs";
export { SDK_HELPER_SYMBOL, collectStainlessHelpers, stainlessHelperHeader };
/**
* Represents an MCP tool definition.
* Matches the shape returned by `mcpClient.listTools()`.
*/
export interface MCPToolLike {
name: string;
description?: string | undefined;
inputSchema: {
type: 'object';
properties?: Record<string, unknown> | null | undefined;
required?: string[] | readonly string[] | null | undefined;
[key: string]: unknown;
};
}
/**
* Represents the result of calling an MCP tool.
* Matches the shape returned by `mcpClient.callTool()`.
*/
export interface MCPCallToolResultLike {
content: MCPToolResultContentLike[];
structuredContent?: object | undefined;
isError?: boolean | undefined;
}
export type MCPToolResultContentLike = MCPTextContentLike | MCPImageContentLike | MCPAudioContentLike | MCPEmbeddedResourceLike | MCPResourceLinkLike;
export interface MCPTextContentLike {
type: 'text';
text: string;
}
export interface MCPImageContentLike {
type: 'image';
data: string;
mimeType: string;
}
export interface MCPAudioContentLike {
type: 'audio';
data: string;
mimeType: string;
}
export interface MCPEmbeddedResourceLike {
type: 'resource';
resource: MCPResourceContentsLike;
}
export interface MCPResourceLinkLike {
type: 'resource_link';
uri: string;
name: string;
mimeType?: string | undefined;
}
/**
* Text resource contents from MCP.
*/
export interface MCPTextResourceContentsLike {
uri: string;
mimeType?: string | undefined;
text: string;
}
/**
* Blob (binary) resource contents from MCP.
*/
export interface MCPBlobResourceContentsLike {
uri: string;
mimeType?: string | undefined;
blob: string;
}
/**
* Resource contents - either text or blob.
* Matches `TextResourceContents | BlobResourceContents` from MCP SDK.
*/
export type MCPResourceContentsLike = MCPTextResourceContentsLike | MCPBlobResourceContentsLike;
/**
* Interface for an MCP client that can call tools.
* Matches the relevant methods of `Client` from `@modelcontextprotocol/sdk`.
*/
export interface MCPClientLike {
callTool(params: {
name: string;
arguments?: Record<string, unknown>;
}): Promise<MCPCallToolResultLike>;
}
/**
* Represents a message from an MCP prompt.
* Matches the shape returned by `mcpClient.getPrompt()`.
*/
export interface MCPPromptMessageLike {
role: 'user' | 'assistant';
content: MCPPromptContentLike;
}
export type MCPPromptContentLike = MCPTextContentLike | MCPImageContentLike | MCPAudioContentLike | MCPEmbeddedResourceLike | MCPResourceLinkLike;
/**
* Represents the contents of an MCP resource.
* Matches the shape returned by `mcpClient.readResource()`.
*/
export interface MCPReadResourceResultLike {
contents: MCPResourceContentsLike[];
}
/**
* Error thrown when an MCP value cannot be converted to a format supported by the Claude API.
*/
export declare class UnsupportedMCPValueError extends Error {
constructor(message: string);
}
/**
* Converts an MCP tool to a BetaRunnableTool for use with the Anthropic SDK's
* `toolRunner()` method.
*
* @param tool The MCP tool definition from `mcpClient.listTools()`
* @param mcpClient The MCP client instance used to call the tool
* @param extraProps Additional Claude API properties to include in the tool definition
* @returns A runnable tool for use with `anthropic.beta.messages.toolRunner()`
* @throws {UnsupportedMCPValueError} When the tool returns unsupported content types
* @throws {UnsupportedMCPValueError} When the tool returns unsupported resource links
* @throws {UnsupportedMCPValueError} When the tool returns resources with unsupported MIME types
*
* @example
* ```ts
* import { Client } from "@modelcontextprotocol/sdk/client/index.js";
* import Anthropic from "@anthropic-ai/sdk";
* import { mcpTool } from "@anthropic-ai/sdk/helpers/beta/mcp";
*
* const mcpClient = new Client({ name: "example", version: "1.0.0" });
* const anthropic = new Anthropic();
*
* const tools = await mcpClient.listTools();
* const runner = await anthropic.beta.messages.toolRunner({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* tools: tools.tools.map(tool => mcpTool(tool, mcpClient)),
* messages: [{ role: "user", content: "Use the available tools" }],
* });
* ```
*/
export declare function mcpTool(tool: MCPToolLike, mcpClient: MCPClientLike, extraProps?: Partial<Omit<BetaTool, 'name' | 'description' | 'input_schema'>>): BetaRunnableTool<Record<string, unknown>>;
/**
* Converts an array of MCP tools to BetaRunnableTools.
*
* @param tools Array of MCP tool definitions from `mcpClient.listTools()`
* @param mcpClient The MCP client instance used to call the tools
* @param extraProps Additional Claude API properties to include in each tool definition
* @returns An array of runnable tools for use with `anthropic.beta.messages.toolRunner()`
*
* @example
* ```ts
* const { tools } = await mcpClient.listTools();
* const runner = await anthropic.beta.messages.toolRunner({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* tools: mcpTools(tools, mcpClient),
* messages: [{ role: "user", content: "Use the available tools" }],
* });
* ```
*/
export declare function mcpTools(tools: MCPToolLike[], mcpClient: MCPClientLike, extraProps?: Partial<Omit<BetaTool, 'name' | 'description' | 'input_schema'>>): BetaRunnableTool<Record<string, unknown>>[];
/**
* Converts an MCP prompt message to an Anthropic BetaMessageParam.
*
* @param mcpMessage The MCP prompt message from `mcpClient.getPrompt()`
* @param extraProps Additional Claude API properties to include in content blocks (e.g., `cache_control`)
* @returns A message parameter for use with `anthropic.beta.messages.create()`
* @throws {UnsupportedMCPValueError} When the message contains unsupported content types
* @throws {UnsupportedMCPValueError} When the message contains unsupported resource links
* @throws {UnsupportedMCPValueError} When the message contains resources with unsupported MIME types
*
* @example
* ```ts
* import { Client } from "@modelcontextprotocol/sdk/client/index.js";
* import Anthropic from "@anthropic-ai/sdk";
* import { mcpMessage } from "@anthropic-ai/sdk/helpers/beta/mcp";
*
* const mcpClient = new Client({ name: "example", version: "1.0.0" });
* const anthropic = new Anthropic();
*
* const prompt = await mcpClient.getPrompt({
* name: "example-prompt",
* arguments: { arg1: "value" },
* });
*
* await anthropic.beta.messages.create({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* messages: prompt.messages.map(msg => mcpMessage(msg)),
* });
* ```
*/
export declare function mcpMessage(mcpMessage: MCPPromptMessageLike, extraProps?: Partial<Omit<BetaTextBlockParam, 'type' | 'text' | 'source'> & Omit<BetaImageBlockParam, 'type' | 'source'> & Omit<BetaRequestDocumentBlock, 'type' | 'source'>>): BetaMessageParam;
/**
* Converts an array of MCP prompt messages to Anthropic BetaMessageParams.
*
* @param messages Array of MCP prompt messages from `mcpClient.getPrompt()`
* @param extraProps Additional Claude API properties to include in content blocks (e.g., `cache_control`)
* @returns An array of message parameters for use with `anthropic.beta.messages.create()`
* @throws {UnsupportedMCPValueError} When any message contains unsupported content types
* @throws {UnsupportedMCPValueError} When any message contains unsupported resource links
* @throws {UnsupportedMCPValueError} When any message contains resources with unsupported MIME types
*
* @example
* ```ts
* const { messages } = await mcpClient.getPrompt({ name: "example-prompt" });
* await anthropic.beta.messages.create({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* messages: mcpMessages(messages),
* });
* ```
*/
export declare function mcpMessages(messages: MCPPromptMessageLike[], extraProps?: Partial<Omit<BetaTextBlockParam, 'type' | 'text' | 'source'> & Omit<BetaImageBlockParam, 'type' | 'source'> & Omit<BetaRequestDocumentBlock, 'type' | 'source'>>): BetaMessageParam[];
/**
* Converts a single MCP prompt content item to an Anthropic content block.
*
* @param content The MCP content item (text, image, or embedded resource)
* @param extraProps Additional Claude API properties to include in the content block (e.g., `cache_control`)
* @returns A Claude content block for use in a message's content array
* @throws {UnsupportedMCPValueError} When the content type is not supported (e.g., 'audio')
* @throws {UnsupportedMCPValueError} When resource links use non-http/https protocols
* @throws {UnsupportedMCPValueError} When resources have unsupported MIME types
*
* @example
* ```ts
* const { messages } = await mcpClient.getPrompt({ name: "my-prompt" });
* // If you need to mix MCP content with other content:
* await anthropic.beta.messages.create({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* messages: [{
* role: "user",
* content: [
* mcpContent(messages[0].content),
* { type: "text", text: "Additional context" },
* ],
* }],
* });
* ```
*/
export declare function mcpContent(content: MCPPromptContentLike, extraProps?: Partial<Omit<BetaTextBlockParam, 'type' | 'text' | 'source'> & Omit<BetaImageBlockParam, 'type' | 'source'> & Omit<BetaRequestDocumentBlock, 'type' | 'source'>>): BetaTextBlockParam | BetaImageBlockParam | BetaRequestDocumentBlock;
/**
* Converts MCP resource contents to an Anthropic content block.
*
* This helper is useful when you have resource contents from `mcpClient.readResource()`
* and want to include them in a message or as a document source. It automatically
* finds the first resource with a supported MIME type.
*
* @param result The result from `mcpClient.readResource()`
* @param extraProps Additional Claude API properties to include in the content block (e.g., `cache_control`)
* @returns A Claude content block
* @throws {UnsupportedMCPValueError} When contents array is empty or none have a supported MIME type
*
* @example
* ```ts
* import { Client } from "@modelcontextprotocol/sdk/client/index.js";
* import Anthropic from "@anthropic-ai/sdk";
* import { mcpResourceToContent } from "@anthropic-ai/sdk/helpers/beta/mcp";
*
* const mcpClient = new Client({ name: "example", version: "1.0.0" });
* const anthropic = new Anthropic();
*
* const resource = await mcpClient.readResource({ uri: "file:///example.txt" });
* await anthropic.beta.messages.create({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* messages: [{
* role: "user",
* content: [mcpResourceToContent(resource)],
* }],
* });
* ```
*/
export declare function mcpResourceToContent(result: MCPReadResourceResultLike, extraProps?: Partial<Omit<BetaRequestDocumentBlock, 'type' | 'source'>>): BetaTextBlockParam | BetaImageBlockParam | BetaRequestDocumentBlock;
/**
* Converts an MCP resource to a File object suitable for uploading via `anthropic.beta.files.upload()`.
*
* @param result The result from `mcpClient.readResource()`
* @returns A File object for use with `anthropic.beta.files.upload()`
* @throws {UnsupportedMCPValueError} When contents array is empty
*
* @example
* ```ts
* import { Client } from "@modelcontextprotocol/sdk/client/index.js";
* import Anthropic from "@anthropic-ai/sdk";
* import { mcpResourceToFile } from "@anthropic-ai/sdk/helpers/beta/mcp";
*
* const mcpClient = new Client({ name: "example", version: "1.0.0" });
* const anthropic = new Anthropic();
*
* const resource = await mcpClient.readResource({ uri: "file:///document.pdf" });
*
* const uploaded = await anthropic.beta.files.upload({
* file: mcpResourceToFile(resource),
* });
* ```
*/
export declare function mcpResourceToFile(result: MCPReadResourceResultLike): File;
//# sourceMappingURL=mcp.d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AE,MAAM,GAAG,MAAM,GAAG,QAAQ,CAAC,GAClD,IAAI,CAAC,mBAAmB,EAAE,MAAM,GAAG,QAAQ,CAAC,GAC5C,IAAI,CAAC,wBAAwB,EAAE,MAAM,GAAG,QAAQ,CAAC,CACpD,GACA,gBAAgB,EAAE,CAEpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,UAAU,CACxB,OAAO,EAAE,oBAAoB,EAC7B,UAAU,CAAC,EAAE,OAAO,CAClB,IAAI,CAAC,kBAAkB,EAAE,MAAM,GAAG,MAAM,GAAG,QAAQ,CAAC,GAClD,IAAI,CAAC,mBAAmB,EAAE,MAAM,GAAG,QAAQ,CAAC,GAC5C,IAAI,CAAC,wBAAwB,EAAE,MAAM,GAAG,QAAQ,CAAC,CACpD,GACA,kBAAkB,GAAG,mBAAmB,GAAG,wBAAwB,CA2CrE;AAoED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,yBAAyB,EACjC,UAAU,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,wBAAwB,EAAE,MAAM,GAAG,QAAQ,CAAC,CAAC,GACtE,kBAAkB,GAAG,mBAAmB,GAAG,wBAAwB,CAYrE;AAoBD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,yBAAyB,GAAG,IAAI,CAWzE"}

View File

@@ -0,0 +1,303 @@
/**
* Helper functions for integrating MCP (Model Context Protocol) SDK types
* with the Anthropic SDK.
*
* These helpers reduce boilerplate when converting between MCP types and
* Anthropic API types. The interfaces defined here use TypeScript's structural
* typing to match MCP SDK types without requiring a direct dependency.
*/
import { BetaRunnableTool } from "../../lib/tools/BetaRunnableTool.js";
import { BetaTool, BetaMessageParam, BetaTextBlockParam, BetaImageBlockParam, BetaRequestDocumentBlock } from "../../resources/beta.js";
import { SDK_HELPER_SYMBOL, collectStainlessHelpers, stainlessHelperHeader } from "../../lib/stainless-helper-header.js";
export { SDK_HELPER_SYMBOL, collectStainlessHelpers, stainlessHelperHeader };
/**
* Represents an MCP tool definition.
* Matches the shape returned by `mcpClient.listTools()`.
*/
export interface MCPToolLike {
name: string;
description?: string | undefined;
inputSchema: {
type: 'object';
properties?: Record<string, unknown> | null | undefined;
required?: string[] | readonly string[] | null | undefined;
[key: string]: unknown;
};
}
/**
* Represents the result of calling an MCP tool.
* Matches the shape returned by `mcpClient.callTool()`.
*/
export interface MCPCallToolResultLike {
content: MCPToolResultContentLike[];
structuredContent?: object | undefined;
isError?: boolean | undefined;
}
export type MCPToolResultContentLike = MCPTextContentLike | MCPImageContentLike | MCPAudioContentLike | MCPEmbeddedResourceLike | MCPResourceLinkLike;
export interface MCPTextContentLike {
type: 'text';
text: string;
}
export interface MCPImageContentLike {
type: 'image';
data: string;
mimeType: string;
}
export interface MCPAudioContentLike {
type: 'audio';
data: string;
mimeType: string;
}
export interface MCPEmbeddedResourceLike {
type: 'resource';
resource: MCPResourceContentsLike;
}
export interface MCPResourceLinkLike {
type: 'resource_link';
uri: string;
name: string;
mimeType?: string | undefined;
}
/**
* Text resource contents from MCP.
*/
export interface MCPTextResourceContentsLike {
uri: string;
mimeType?: string | undefined;
text: string;
}
/**
* Blob (binary) resource contents from MCP.
*/
export interface MCPBlobResourceContentsLike {
uri: string;
mimeType?: string | undefined;
blob: string;
}
/**
* Resource contents - either text or blob.
* Matches `TextResourceContents | BlobResourceContents` from MCP SDK.
*/
export type MCPResourceContentsLike = MCPTextResourceContentsLike | MCPBlobResourceContentsLike;
/**
* Interface for an MCP client that can call tools.
* Matches the relevant methods of `Client` from `@modelcontextprotocol/sdk`.
*/
export interface MCPClientLike {
callTool(params: {
name: string;
arguments?: Record<string, unknown>;
}): Promise<MCPCallToolResultLike>;
}
/**
* Represents a message from an MCP prompt.
* Matches the shape returned by `mcpClient.getPrompt()`.
*/
export interface MCPPromptMessageLike {
role: 'user' | 'assistant';
content: MCPPromptContentLike;
}
export type MCPPromptContentLike = MCPTextContentLike | MCPImageContentLike | MCPAudioContentLike | MCPEmbeddedResourceLike | MCPResourceLinkLike;
/**
* Represents the contents of an MCP resource.
* Matches the shape returned by `mcpClient.readResource()`.
*/
export interface MCPReadResourceResultLike {
contents: MCPResourceContentsLike[];
}
/**
* Error thrown when an MCP value cannot be converted to a format supported by the Claude API.
*/
export declare class UnsupportedMCPValueError extends Error {
constructor(message: string);
}
/**
* Converts an MCP tool to a BetaRunnableTool for use with the Anthropic SDK's
* `toolRunner()` method.
*
* @param tool The MCP tool definition from `mcpClient.listTools()`
* @param mcpClient The MCP client instance used to call the tool
* @param extraProps Additional Claude API properties to include in the tool definition
* @returns A runnable tool for use with `anthropic.beta.messages.toolRunner()`
* @throws {UnsupportedMCPValueError} When the tool returns unsupported content types
* @throws {UnsupportedMCPValueError} When the tool returns unsupported resource links
* @throws {UnsupportedMCPValueError} When the tool returns resources with unsupported MIME types
*
* @example
* ```ts
* import { Client } from "@modelcontextprotocol/sdk/client/index.js";
* import Anthropic from "@anthropic-ai/sdk";
* import { mcpTool } from "@anthropic-ai/sdk/helpers/beta/mcp";
*
* const mcpClient = new Client({ name: "example", version: "1.0.0" });
* const anthropic = new Anthropic();
*
* const tools = await mcpClient.listTools();
* const runner = await anthropic.beta.messages.toolRunner({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* tools: tools.tools.map(tool => mcpTool(tool, mcpClient)),
* messages: [{ role: "user", content: "Use the available tools" }],
* });
* ```
*/
export declare function mcpTool(tool: MCPToolLike, mcpClient: MCPClientLike, extraProps?: Partial<Omit<BetaTool, 'name' | 'description' | 'input_schema'>>): BetaRunnableTool<Record<string, unknown>>;
/**
* Converts an array of MCP tools to BetaRunnableTools.
*
* @param tools Array of MCP tool definitions from `mcpClient.listTools()`
* @param mcpClient The MCP client instance used to call the tools
* @param extraProps Additional Claude API properties to include in each tool definition
* @returns An array of runnable tools for use with `anthropic.beta.messages.toolRunner()`
*
* @example
* ```ts
* const { tools } = await mcpClient.listTools();
* const runner = await anthropic.beta.messages.toolRunner({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* tools: mcpTools(tools, mcpClient),
* messages: [{ role: "user", content: "Use the available tools" }],
* });
* ```
*/
export declare function mcpTools(tools: MCPToolLike[], mcpClient: MCPClientLike, extraProps?: Partial<Omit<BetaTool, 'name' | 'description' | 'input_schema'>>): BetaRunnableTool<Record<string, unknown>>[];
/**
* Converts an MCP prompt message to an Anthropic BetaMessageParam.
*
* @param mcpMessage The MCP prompt message from `mcpClient.getPrompt()`
* @param extraProps Additional Claude API properties to include in content blocks (e.g., `cache_control`)
* @returns A message parameter for use with `anthropic.beta.messages.create()`
* @throws {UnsupportedMCPValueError} When the message contains unsupported content types
* @throws {UnsupportedMCPValueError} When the message contains unsupported resource links
* @throws {UnsupportedMCPValueError} When the message contains resources with unsupported MIME types
*
* @example
* ```ts
* import { Client } from "@modelcontextprotocol/sdk/client/index.js";
* import Anthropic from "@anthropic-ai/sdk";
* import { mcpMessage } from "@anthropic-ai/sdk/helpers/beta/mcp";
*
* const mcpClient = new Client({ name: "example", version: "1.0.0" });
* const anthropic = new Anthropic();
*
* const prompt = await mcpClient.getPrompt({
* name: "example-prompt",
* arguments: { arg1: "value" },
* });
*
* await anthropic.beta.messages.create({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* messages: prompt.messages.map(msg => mcpMessage(msg)),
* });
* ```
*/
export declare function mcpMessage(mcpMessage: MCPPromptMessageLike, extraProps?: Partial<Omit<BetaTextBlockParam, 'type' | 'text' | 'source'> & Omit<BetaImageBlockParam, 'type' | 'source'> & Omit<BetaRequestDocumentBlock, 'type' | 'source'>>): BetaMessageParam;
/**
* Converts an array of MCP prompt messages to Anthropic BetaMessageParams.
*
* @param messages Array of MCP prompt messages from `mcpClient.getPrompt()`
* @param extraProps Additional Claude API properties to include in content blocks (e.g., `cache_control`)
* @returns An array of message parameters for use with `anthropic.beta.messages.create()`
* @throws {UnsupportedMCPValueError} When any message contains unsupported content types
* @throws {UnsupportedMCPValueError} When any message contains unsupported resource links
* @throws {UnsupportedMCPValueError} When any message contains resources with unsupported MIME types
*
* @example
* ```ts
* const { messages } = await mcpClient.getPrompt({ name: "example-prompt" });
* await anthropic.beta.messages.create({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* messages: mcpMessages(messages),
* });
* ```
*/
export declare function mcpMessages(messages: MCPPromptMessageLike[], extraProps?: Partial<Omit<BetaTextBlockParam, 'type' | 'text' | 'source'> & Omit<BetaImageBlockParam, 'type' | 'source'> & Omit<BetaRequestDocumentBlock, 'type' | 'source'>>): BetaMessageParam[];
/**
* Converts a single MCP prompt content item to an Anthropic content block.
*
* @param content The MCP content item (text, image, or embedded resource)
* @param extraProps Additional Claude API properties to include in the content block (e.g., `cache_control`)
* @returns A Claude content block for use in a message's content array
* @throws {UnsupportedMCPValueError} When the content type is not supported (e.g., 'audio')
* @throws {UnsupportedMCPValueError} When resource links use non-http/https protocols
* @throws {UnsupportedMCPValueError} When resources have unsupported MIME types
*
* @example
* ```ts
* const { messages } = await mcpClient.getPrompt({ name: "my-prompt" });
* // If you need to mix MCP content with other content:
* await anthropic.beta.messages.create({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* messages: [{
* role: "user",
* content: [
* mcpContent(messages[0].content),
* { type: "text", text: "Additional context" },
* ],
* }],
* });
* ```
*/
export declare function mcpContent(content: MCPPromptContentLike, extraProps?: Partial<Omit<BetaTextBlockParam, 'type' | 'text' | 'source'> & Omit<BetaImageBlockParam, 'type' | 'source'> & Omit<BetaRequestDocumentBlock, 'type' | 'source'>>): BetaTextBlockParam | BetaImageBlockParam | BetaRequestDocumentBlock;
/**
* Converts MCP resource contents to an Anthropic content block.
*
* This helper is useful when you have resource contents from `mcpClient.readResource()`
* and want to include them in a message or as a document source. It automatically
* finds the first resource with a supported MIME type.
*
* @param result The result from `mcpClient.readResource()`
* @param extraProps Additional Claude API properties to include in the content block (e.g., `cache_control`)
* @returns A Claude content block
* @throws {UnsupportedMCPValueError} When contents array is empty or none have a supported MIME type
*
* @example
* ```ts
* import { Client } from "@modelcontextprotocol/sdk/client/index.js";
* import Anthropic from "@anthropic-ai/sdk";
* import { mcpResourceToContent } from "@anthropic-ai/sdk/helpers/beta/mcp";
*
* const mcpClient = new Client({ name: "example", version: "1.0.0" });
* const anthropic = new Anthropic();
*
* const resource = await mcpClient.readResource({ uri: "file:///example.txt" });
* await anthropic.beta.messages.create({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* messages: [{
* role: "user",
* content: [mcpResourceToContent(resource)],
* }],
* });
* ```
*/
export declare function mcpResourceToContent(result: MCPReadResourceResultLike, extraProps?: Partial<Omit<BetaRequestDocumentBlock, 'type' | 'source'>>): BetaTextBlockParam | BetaImageBlockParam | BetaRequestDocumentBlock;
/**
* Converts an MCP resource to a File object suitable for uploading via `anthropic.beta.files.upload()`.
*
* @param result The result from `mcpClient.readResource()`
* @returns A File object for use with `anthropic.beta.files.upload()`
* @throws {UnsupportedMCPValueError} When contents array is empty
*
* @example
* ```ts
* import { Client } from "@modelcontextprotocol/sdk/client/index.js";
* import Anthropic from "@anthropic-ai/sdk";
* import { mcpResourceToFile } from "@anthropic-ai/sdk/helpers/beta/mcp";
*
* const mcpClient = new Client({ name: "example", version: "1.0.0" });
* const anthropic = new Anthropic();
*
* const resource = await mcpClient.readResource({ uri: "file:///document.pdf" });
*
* const uploaded = await anthropic.beta.files.upload({
* file: mcpResourceToFile(resource),
* });
* ```
*/
export declare function mcpResourceToFile(result: MCPReadResourceResultLike): File;
//# sourceMappingURL=mcp.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"mcp.d.ts","sourceRoot":"","sources":["../../src/helpers/beta/mcp.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;OAEI,EAAE,gBAAgB,EAAE;OAEpB,EACL,QAAQ,EAER,gBAAgB,EAChB,kBAAkB,EAClB,mBAAmB,EACnB,wBAAwB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;;GA0BG;AACH,wBAAgB,UAAU,CACxB,OAAO,EAAE,oBAAoB,EAC7B,UAAU,CAAC,EAAE,OAAO,CAClB,IAAI,CAAC,kBAAkB,EAAE,MAAM,GAAG,MAAM,GAAG,QAAQ,CAAC,GAClD,IAAI,CAAC,mBAAmB,EAAE,MAAM,GAAG,QAAQ,CAAC,GAC5C,IAAI,CAAC,wBAAwB,EAAE,MAAM,GAAG,QAAQ,CAAC,CACpD,GACA,kBAAkB,GAAG,mBAAmB,GAAG,wBAAwB,CA2CrE;AAoED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,yBAAyB,EACjC,UAAU,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,wBAAwB,EAAE,MAAM,GAAG,QAAQ,CAAC,CAAC,GACtE,kBAAkB,GAAG,mBAAmB,GAAG,wBAAwB,CAYrE;AAoBD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,yBAAyB,GAAG,IAAI,CAWzE"}

View File

@@ -0,0 +1,419 @@
"use strict";
/**
* Helper functions for integrating MCP (Model Context Protocol) SDK types
* with the Anthropic SDK.
*
* These helpers reduce boilerplate when converting between MCP types and
* Anthropic API types. The interfaces defined here use TypeScript's structural
* typing to match MCP SDK types without requiring a direct dependency.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnsupportedMCPValueError = exports.stainlessHelperHeader = exports.collectStainlessHelpers = exports.SDK_HELPER_SYMBOL = void 0;
exports.mcpTool = mcpTool;
exports.mcpTools = mcpTools;
exports.mcpMessage = mcpMessage;
exports.mcpMessages = mcpMessages;
exports.mcpContent = mcpContent;
exports.mcpResourceToContent = mcpResourceToContent;
exports.mcpResourceToFile = mcpResourceToFile;
const ToolError_1 = require("../../lib/tools/ToolError.js");
const stainless_helper_header_1 = require("../../lib/stainless-helper-header.js");
Object.defineProperty(exports, "SDK_HELPER_SYMBOL", { enumerable: true, get: function () { return stainless_helper_header_1.SDK_HELPER_SYMBOL; } });
Object.defineProperty(exports, "collectStainlessHelpers", { enumerable: true, get: function () { return stainless_helper_header_1.collectStainlessHelpers; } });
Object.defineProperty(exports, "stainlessHelperHeader", { enumerable: true, get: function () { return stainless_helper_header_1.stainlessHelperHeader; } });
const base64_1 = require("../../internal/utils/base64.js");
// -----------------------------------------------------------------------------
// Supported MIME types
// -----------------------------------------------------------------------------
const SUPPORTED_IMAGE_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'];
function isSupportedImageType(mimeType) {
return SUPPORTED_IMAGE_TYPES.includes(mimeType);
}
function isSupportedResourceMimeType(mimeType) {
return (!mimeType ||
mimeType.startsWith('text/') ||
mimeType === 'application/pdf' ||
isSupportedImageType(mimeType));
}
// -----------------------------------------------------------------------------
// Error classes
// -----------------------------------------------------------------------------
/**
* Error thrown when an MCP value cannot be converted to a format supported by the Claude API.
*/
class UnsupportedMCPValueError extends Error {
constructor(message) {
super(message);
this.name = 'UnsupportedMCPValueError';
}
}
exports.UnsupportedMCPValueError = UnsupportedMCPValueError;
// -----------------------------------------------------------------------------
// Helper functions
// -----------------------------------------------------------------------------
/**
* Converts an MCP tool to a BetaRunnableTool for use with the Anthropic SDK's
* `toolRunner()` method.
*
* @param tool The MCP tool definition from `mcpClient.listTools()`
* @param mcpClient The MCP client instance used to call the tool
* @param extraProps Additional Claude API properties to include in the tool definition
* @returns A runnable tool for use with `anthropic.beta.messages.toolRunner()`
* @throws {UnsupportedMCPValueError} When the tool returns unsupported content types
* @throws {UnsupportedMCPValueError} When the tool returns unsupported resource links
* @throws {UnsupportedMCPValueError} When the tool returns resources with unsupported MIME types
*
* @example
* ```ts
* import { Client } from "@modelcontextprotocol/sdk/client/index.js";
* import Anthropic from "@anthropic-ai/sdk";
* import { mcpTool } from "@anthropic-ai/sdk/helpers/beta/mcp";
*
* const mcpClient = new Client({ name: "example", version: "1.0.0" });
* const anthropic = new Anthropic();
*
* const tools = await mcpClient.listTools();
* const runner = await anthropic.beta.messages.toolRunner({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* tools: tools.tools.map(tool => mcpTool(tool, mcpClient)),
* messages: [{ role: "user", content: "Use the available tools" }],
* });
* ```
*/
function mcpTool(tool, mcpClient, extraProps) {
// Transform inputSchema to match BetaTool.InputSchema (convert undefined to null)
const inputSchema = {
...tool.inputSchema,
type: 'object',
properties: tool.inputSchema.properties ?? null,
required: tool.inputSchema.required ?? null,
};
const betaTool = {
name: tool.name,
input_schema: inputSchema,
...(tool.description !== undefined ? { description: tool.description } : {}),
...extraProps,
};
const runnableTool = {
...betaTool,
run: async (input) => {
const result = await mcpClient.callTool({
name: tool.name,
arguments: input,
});
if (result.isError) {
const content = result.content.map((item) => mcpContent(item));
throw new ToolError_1.ToolError(content);
}
// If content is empty but structuredContent is present, JSON encode it
// Spec: "For backwards compatibility, a tool that returns structured content SHOULD also return the serialized JSON in a TextContent block."
// meaning it's not required and cannot be assumed.
if (result.content.length === 0 &&
// Spec: "Structured content is returned as a JSON object in the structuredContent field of a result."
typeof result.structuredContent === 'object' &&
result.structuredContent !== null) {
return JSON.stringify(result.structuredContent);
}
return result.content.map((item) => mcpContent(item));
},
parse: (content) => content,
[stainless_helper_header_1.SDK_HELPER_SYMBOL]: 'mcpTool',
};
return runnableTool;
}
/**
* Converts an array of MCP tools to BetaRunnableTools.
*
* @param tools Array of MCP tool definitions from `mcpClient.listTools()`
* @param mcpClient The MCP client instance used to call the tools
* @param extraProps Additional Claude API properties to include in each tool definition
* @returns An array of runnable tools for use with `anthropic.beta.messages.toolRunner()`
*
* @example
* ```ts
* const { tools } = await mcpClient.listTools();
* const runner = await anthropic.beta.messages.toolRunner({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* tools: mcpTools(tools, mcpClient),
* messages: [{ role: "user", content: "Use the available tools" }],
* });
* ```
*/
function mcpTools(tools, mcpClient, extraProps) {
return tools.map((tool) => mcpTool(tool, mcpClient, extraProps));
}
/**
* Converts an MCP prompt message to an Anthropic BetaMessageParam.
*
* @param mcpMessage The MCP prompt message from `mcpClient.getPrompt()`
* @param extraProps Additional Claude API properties to include in content blocks (e.g., `cache_control`)
* @returns A message parameter for use with `anthropic.beta.messages.create()`
* @throws {UnsupportedMCPValueError} When the message contains unsupported content types
* @throws {UnsupportedMCPValueError} When the message contains unsupported resource links
* @throws {UnsupportedMCPValueError} When the message contains resources with unsupported MIME types
*
* @example
* ```ts
* import { Client } from "@modelcontextprotocol/sdk/client/index.js";
* import Anthropic from "@anthropic-ai/sdk";
* import { mcpMessage } from "@anthropic-ai/sdk/helpers/beta/mcp";
*
* const mcpClient = new Client({ name: "example", version: "1.0.0" });
* const anthropic = new Anthropic();
*
* const prompt = await mcpClient.getPrompt({
* name: "example-prompt",
* arguments: { arg1: "value" },
* });
*
* await anthropic.beta.messages.create({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* messages: prompt.messages.map(msg => mcpMessage(msg)),
* });
* ```
*/
function mcpMessage(mcpMessage, extraProps) {
const message = {
role: mcpMessage.role,
content: [mcpContent(mcpMessage.content, extraProps)],
[stainless_helper_header_1.SDK_HELPER_SYMBOL]: 'mcpMessage',
};
return message;
}
/**
* Converts an array of MCP prompt messages to Anthropic BetaMessageParams.
*
* @param messages Array of MCP prompt messages from `mcpClient.getPrompt()`
* @param extraProps Additional Claude API properties to include in content blocks (e.g., `cache_control`)
* @returns An array of message parameters for use with `anthropic.beta.messages.create()`
* @throws {UnsupportedMCPValueError} When any message contains unsupported content types
* @throws {UnsupportedMCPValueError} When any message contains unsupported resource links
* @throws {UnsupportedMCPValueError} When any message contains resources with unsupported MIME types
*
* @example
* ```ts
* const { messages } = await mcpClient.getPrompt({ name: "example-prompt" });
* await anthropic.beta.messages.create({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* messages: mcpMessages(messages),
* });
* ```
*/
function mcpMessages(messages, extraProps) {
return messages.map((message) => mcpMessage(message, extraProps));
}
/**
* Converts a single MCP prompt content item to an Anthropic content block.
*
* @param content The MCP content item (text, image, or embedded resource)
* @param extraProps Additional Claude API properties to include in the content block (e.g., `cache_control`)
* @returns A Claude content block for use in a message's content array
* @throws {UnsupportedMCPValueError} When the content type is not supported (e.g., 'audio')
* @throws {UnsupportedMCPValueError} When resource links use non-http/https protocols
* @throws {UnsupportedMCPValueError} When resources have unsupported MIME types
*
* @example
* ```ts
* const { messages } = await mcpClient.getPrompt({ name: "my-prompt" });
* // If you need to mix MCP content with other content:
* await anthropic.beta.messages.create({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* messages: [{
* role: "user",
* content: [
* mcpContent(messages[0].content),
* { type: "text", text: "Additional context" },
* ],
* }],
* });
* ```
*/
function mcpContent(content, extraProps) {
switch (content.type) {
case 'text': {
const textBlock = {
type: 'text',
text: content.text,
...extraProps,
[stainless_helper_header_1.SDK_HELPER_SYMBOL]: 'mcpContent',
};
return textBlock;
}
case 'image': {
if (!isSupportedImageType(content.mimeType)) {
throw new UnsupportedMCPValueError(`Unsupported image MIME type: ${content.mimeType}`);
}
const imageBlock = {
type: 'image',
source: {
type: 'base64',
data: content.data,
media_type: content.mimeType,
},
...extraProps,
[stainless_helper_header_1.SDK_HELPER_SYMBOL]: 'mcpContent',
};
return imageBlock;
}
case 'resource':
return mcpResourceContentToContentBlock(content.resource, extraProps, 'mcpContent');
case 'resource_link':
case 'audio':
throw new UnsupportedMCPValueError(`Unsupported MCP content type: ${content.type}`);
default:
// This should never happen as we handle all MCPPromptContentLike types
content;
throw new UnsupportedMCPValueError(`Unsupported MCP content type: ${content.type}`);
}
}
/**
* Converts a single MCP resource contents item to an Anthropic content block.
*/
function mcpResourceContentToContentBlock(resourceContent, extraProps, helperName = 'mcpResourceToContent') {
const mimeType = resourceContent.mimeType;
// Handle images (requires blob - base64-encoded binary data)
if (mimeType && isSupportedImageType(mimeType)) {
if (!('blob' in resourceContent)) {
throw new UnsupportedMCPValueError(`Image resource must have blob data, not text. URI: ${resourceContent.uri}`);
}
const imageBlock = {
type: 'image',
source: {
type: 'base64',
data: resourceContent.blob,
media_type: mimeType,
},
...extraProps,
[stainless_helper_header_1.SDK_HELPER_SYMBOL]: helperName,
};
return imageBlock;
}
// Handle PDFs (requires blob - base64-encoded binary data)
if (mimeType === 'application/pdf') {
if (!('blob' in resourceContent)) {
throw new UnsupportedMCPValueError(`PDF resource must have blob data, not text. URI: ${resourceContent.uri}`);
}
const pdfBlock = {
type: 'document',
source: {
type: 'base64',
data: resourceContent.blob,
media_type: 'application/pdf',
},
...extraProps,
[stainless_helper_header_1.SDK_HELPER_SYMBOL]: helperName,
};
return pdfBlock;
}
// Handle text types (text/*, or no MIME type defaults to text)
if (!mimeType || mimeType.startsWith('text/')) {
const textDocBlock = {
type: 'document',
source: textSourceFromResource(resourceContent),
...extraProps,
[stainless_helper_header_1.SDK_HELPER_SYMBOL]: helperName,
};
return textDocBlock;
}
throw new UnsupportedMCPValueError(`Unsupported MIME type "${mimeType}" for resource: ${resourceContent.uri}`);
}
/**
* Converts MCP resource contents to an Anthropic content block.
*
* This helper is useful when you have resource contents from `mcpClient.readResource()`
* and want to include them in a message or as a document source. It automatically
* finds the first resource with a supported MIME type.
*
* @param result The result from `mcpClient.readResource()`
* @param extraProps Additional Claude API properties to include in the content block (e.g., `cache_control`)
* @returns A Claude content block
* @throws {UnsupportedMCPValueError} When contents array is empty or none have a supported MIME type
*
* @example
* ```ts
* import { Client } from "@modelcontextprotocol/sdk/client/index.js";
* import Anthropic from "@anthropic-ai/sdk";
* import { mcpResourceToContent } from "@anthropic-ai/sdk/helpers/beta/mcp";
*
* const mcpClient = new Client({ name: "example", version: "1.0.0" });
* const anthropic = new Anthropic();
*
* const resource = await mcpClient.readResource({ uri: "file:///example.txt" });
* await anthropic.beta.messages.create({
* model: "claude-sonnet-4-20250514",
* max_tokens: 1024,
* messages: [{
* role: "user",
* content: [mcpResourceToContent(resource)],
* }],
* });
* ```
*/
function mcpResourceToContent(result, extraProps) {
if (result.contents.length === 0) {
throw new UnsupportedMCPValueError('Resource contents array must contain at least one item');
}
const supported = result.contents.find((c) => isSupportedResourceMimeType(c.mimeType));
if (!supported) {
const mimeTypes = result.contents.map((c) => c.mimeType).filter((m) => m !== undefined);
throw new UnsupportedMCPValueError(`No supported MIME type found in resource contents. Available: ${mimeTypes.join(', ')}`);
}
return mcpResourceContentToContentBlock(supported, extraProps);
}
/**
* Gets the raw bytes from an MCP resource.
*/
function bytesFromResource(resource) {
if ('blob' in resource) {
return (0, base64_1.fromBase64)(resource.blob);
}
return new TextEncoder().encode(resource.text);
}
/**
* Creates a text document source from an MCP resource, decoding base64 blob to UTF-8 if needed.
*/
function textSourceFromResource(resource) {
const data = 'text' in resource ? resource.text : new TextDecoder().decode((0, base64_1.fromBase64)(resource.blob));
return { type: 'text', data, media_type: 'text/plain' };
}
/**
* Converts an MCP resource to a File object suitable for uploading via `anthropic.beta.files.upload()`.
*
* @param result The result from `mcpClient.readResource()`
* @returns A File object for use with `anthropic.beta.files.upload()`
* @throws {UnsupportedMCPValueError} When contents array is empty
*
* @example
* ```ts
* import { Client } from "@modelcontextprotocol/sdk/client/index.js";
* import Anthropic from "@anthropic-ai/sdk";
* import { mcpResourceToFile } from "@anthropic-ai/sdk/helpers/beta/mcp";
*
* const mcpClient = new Client({ name: "example", version: "1.0.0" });
* const anthropic = new Anthropic();
*
* const resource = await mcpClient.readResource({ uri: "file:///document.pdf" });
*
* const uploaded = await anthropic.beta.files.upload({
* file: mcpResourceToFile(resource),
* });
* ```
*/
function mcpResourceToFile(result) {
if (result.contents.length === 0) {
throw new UnsupportedMCPValueError('Resource contents array must contain at least one item');
}
const resourceContents = result.contents[0];
const name = new URL(resourceContents.uri).pathname.split('/').at(-1) || 'file';
const type = resourceContents.mimeType;
const data = bytesFromResource(resourceContents);
const file = new File([data], name, type ? { type } : undefined);
file[stainless_helper_header_1.SDK_HELPER_SYMBOL] = 'mcpResourceToFile';
return file;
}
//# sourceMappingURL=mcp.js.map

File diff suppressed because one or more lines are too long

Some files were not shown because too many files have changed in this diff Show More