UNPKG

@anthropic-ai/sdk

Version:
127 lines • 6.5 kB
"use strict"; 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"); const tracing_1 = require("../internal/tracing.js"); const uploads_1 = require("../internal/uploads.js"); const tracing_2 = require("../lib/internal/tracing.js"); var APIPromise = /* @__PURE__ */ (() => { /** * 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.addResponseIDs)(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((props) => { // A traced API call's span ends with the body. A response read this way, before any parse // of its body has begun, may never be parsed, so its span ends now, without the body. const span = props.options.__span?.span; if (span && !this.parsedPromise) { const result = { status: 'success', ...props, data: undefined }; (0, tracing_2.setAnthropicSpanAttributes)(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), span, result); (0, tracing_1.guardTracing)(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), () => (0, tracing_1.endApiCallSpan)(span, result)); } return props.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'), workspace_id: response.headers.get('anthropic-workspace-id'), }; } parse() { if (!this.parsedPromise) { this.parsedPromise = this.responsePromise.then((props) => this.parseResponseTraced(props)); } return this.parsedPromise; } /** * Parses the body of an API call. When the call has a span, the span ends with the parse, or, * when the parse returns a streamed body, when the application has read that stream. */ async parseResponseTraced(props) { const call = props.options.__span; if (!call) { return this.parseResponse(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), props); } const { span } = call; let data; try { data = await this.parseResponse(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), props); } catch (error) { const result = { status: 'error', ...props, error }; (0, tracing_2.setAnthropicSpanAttributes)(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), span, result); (0, tracing_1.guardTracing)(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), () => (0, tracing_1.endApiCallSpan)(span, result)); throw error; } const result = { status: 'success', ...props, data }; (0, tracing_1.guardTracing)(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), () => (0, tracing_1.setCreatedIDAttribute)(call, data)); (0, tracing_2.setAnthropicSpanAttributes)(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), span, result); if (props.options.stream && (0, uploads_1.isAsyncIterable)(data)) { (0, tracing_1.guardTracing)(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), () => (0, tracing_2.endAnthropicApiCallSpanWhenStreamEnds)(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), span, data, props)); } else { (0, tracing_1.guardTracing)(tslib_1.__classPrivateFieldGet(this, _APIPromise_client, "f"), () => (0, tracing_1.endApiCallSpan)(span, result)); } return data; } 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(); return APIPromise; })(); exports.APIPromise = APIPromise; //# sourceMappingURL=api-promise.js.map