@anthropic-ai/sdk
Advanced tools
Comparing version 0.5.1 to 0.5.2
{ | ||
"name": "@anthropic-ai/sdk", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "Client library for the Anthropic API", | ||
@@ -5,0 +5,0 @@ "author": "Anthropic <support@anthropic.com>", |
import type { Response } from '@anthropic-ai/sdk/_shims/fetch'; | ||
import { APIResponse, Headers, createResponseHeaders } from './core'; | ||
import { safeJSON } from '@anthropic-ai/sdk/core'; | ||
import { APIError } from '@anthropic-ai/sdk/error'; | ||
type Bytes = string | ArrayBuffer | Uint8Array | Buffer | null | undefined; | ||
type ServerSentEvent = { | ||
@@ -88,15 +92,7 @@ event: string | null; | ||
} | ||
const lineDecoder = new LineDecoder(); | ||
// @ts-ignore | ||
for await (const chunk of this.response.body) { | ||
let text; | ||
if (chunk instanceof Buffer) { | ||
text = chunk.toString(); | ||
} else if ((chunk as any) instanceof Uint8Array) { | ||
text = Buffer.from(chunk).toString(); | ||
} else { | ||
text = chunk; | ||
} | ||
const iter = readableStreamAsyncIterable<Bytes>(this.response.body); | ||
for await (const chunk of iter) { | ||
const text = decodeText(chunk); | ||
@@ -222,1 +218,65 @@ for (const line of lineDecoder.decode(text)) { | ||
} | ||
let _textDecoder; | ||
function decodeText(bytes: Bytes): string { | ||
if (bytes == null) return ''; | ||
if (typeof bytes === 'string') return bytes; | ||
// Node: | ||
if (typeof Buffer !== 'undefined') { | ||
if (bytes instanceof Buffer) { | ||
return bytes.toString(); | ||
} | ||
if (bytes instanceof Uint8Array) { | ||
return Buffer.from(bytes).toString(); | ||
} | ||
throw new Error(`Unexpected: received non-Uint8Array (${bytes.constructor.name}) in Node.`); | ||
} | ||
// Browser | ||
if (typeof TextDecoder !== 'undefined') { | ||
if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) { | ||
_textDecoder ??= new TextDecoder('utf8'); | ||
return _textDecoder.decode(bytes); | ||
} | ||
throw new Error( | ||
`Unexpected: received non-Uint8Array/ArrayBuffer (${ | ||
(bytes as any).constructor.name | ||
}) in a web platform.`, | ||
); | ||
} | ||
throw new Error(`Unexpected: neither Buffer nor TextDecoder are available as globals.`); | ||
} | ||
/** | ||
* Most browsers don't yet have async iterable support for ReadableStream, | ||
* and Node has a very different way of reading bytes from its "ReadableStream". | ||
* | ||
* This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1624185965 | ||
* | ||
* We make extensive use of "any" here to avoid pulling in either "node" or "dom" types | ||
* to library users' type scopes. | ||
*/ | ||
function readableStreamAsyncIterable<T>(stream: any): AsyncIterableIterator<T> { | ||
if (stream[Symbol.asyncIterator]) { | ||
return stream[Symbol.asyncIterator]; | ||
} | ||
const reader = stream.getReader(); | ||
return { | ||
next() { | ||
return reader.read(); | ||
}, | ||
async return() { | ||
reader.releaseLock(); | ||
return { done: true, value: undefined }; | ||
}, | ||
[Symbol.asyncIterator]() { | ||
return this; | ||
}, | ||
}; | ||
} |
@@ -1,1 +0,1 @@ | ||
export const VERSION = '0.5.1'; | ||
export const VERSION = '0.5.2'; |
@@ -59,12 +59,5 @@ 'use strict'; | ||
const lineDecoder = new LineDecoder(); | ||
// @ts-ignore | ||
for await (const chunk of this.response.body) { | ||
let text; | ||
if (chunk instanceof Buffer) { | ||
text = chunk.toString(); | ||
} else if (chunk instanceof Uint8Array) { | ||
text = Buffer.from(chunk).toString(); | ||
} else { | ||
text = chunk; | ||
} | ||
const iter = readableStreamAsyncIterable(this.response.body); | ||
for await (const chunk of iter) { | ||
const text = decodeText(chunk); | ||
for (const line of lineDecoder.decode(text)) { | ||
@@ -169,2 +162,57 @@ const sse = this.decoder.decode(line); | ||
} | ||
let _textDecoder; | ||
function decodeText(bytes) { | ||
if (bytes == null) return ''; | ||
if (typeof bytes === 'string') return bytes; | ||
// Node: | ||
if (typeof Buffer !== 'undefined') { | ||
if (bytes instanceof Buffer) { | ||
return bytes.toString(); | ||
} | ||
if (bytes instanceof Uint8Array) { | ||
return Buffer.from(bytes).toString(); | ||
} | ||
throw new Error(`Unexpected: received non-Uint8Array (${bytes.constructor.name}) in Node.`); | ||
} | ||
// Browser | ||
if (typeof TextDecoder !== 'undefined') { | ||
if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) { | ||
_textDecoder !== null && _textDecoder !== void 0 ? | ||
_textDecoder | ||
: (_textDecoder = new TextDecoder('utf8')); | ||
return _textDecoder.decode(bytes); | ||
} | ||
throw new Error( | ||
`Unexpected: received non-Uint8Array/ArrayBuffer (${bytes.constructor.name}) in a web platform.`, | ||
); | ||
} | ||
throw new Error(`Unexpected: neither Buffer nor TextDecoder are available as globals.`); | ||
} | ||
/** | ||
* Most browsers don't yet have async iterable support for ReadableStream, | ||
* and Node has a very different way of reading bytes from its "ReadableStream". | ||
* | ||
* This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1624185965 | ||
* | ||
* We make extensive use of "any" here to avoid pulling in either "node" or "dom" types | ||
* to library users' type scopes. | ||
*/ | ||
function readableStreamAsyncIterable(stream) { | ||
if (stream[Symbol.asyncIterator]) { | ||
return stream[Symbol.asyncIterator]; | ||
} | ||
const reader = stream.getReader(); | ||
return { | ||
next() { | ||
return reader.read(); | ||
}, | ||
async return() { | ||
reader.releaseLock(); | ||
return { done: true, value: undefined }; | ||
}, | ||
[Symbol.asyncIterator]() { | ||
return this; | ||
}, | ||
}; | ||
} | ||
//# sourceMappingURL=streaming.js.map |
@@ -1,2 +0,2 @@ | ||
export declare const VERSION = '0.5.1'; | ||
export declare const VERSION = '0.5.2'; | ||
//# sourceMappingURL=version.d.ts.map |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
exports.VERSION = void 0; | ||
exports.VERSION = '0.5.1'; | ||
exports.VERSION = '0.5.2'; | ||
//# sourceMappingURL=version.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
342001
154
6150
0
306
5