@anthropic-ai/sdk
Version:
The official TypeScript library for the Anthropic API
61 lines • 2.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.messagesCreateHeaders = messagesCreateHeaders;
exports.startMessagesStreamSpan = startMessagesStreamSpan;
exports.transformOutputFormat = transformOutputFormat;
const error_1 = require("../../error.js");
const headers_1 = require("../../internal/headers.js");
const stainless_helper_header_1 = require("../../internal/stainless-helper-header.js");
const tracing_1 = require("../../internal/tracing.js");
const tracing_2 = require("./tracing.js");
/**
* The headers of a `messages.create` request. `create()` and `stream()` build them before they
* start the span, so that what throws here throws while no span is open.
*/
function messagesCreateHeaders(params, options) {
const { betas, user_profile_id, workspace_id, tools, messages } = params;
return (0, headers_1.buildHeaders)([
{
...(betas?.toString() != null ? { 'anthropic-beta': betas?.toString() } : undefined),
...(user_profile_id != null ? { 'anthropic-user-profile-id': user_profile_id } : undefined),
...(workspace_id != null ? { 'anthropic-workspace-id': workspace_id } : undefined),
},
(0, stainless_helper_header_1.stainlessHelperHeader)(tools, messages),
options?.headers,
]);
}
/**
* Starts the call span of the request that a stream helper sends `body` with. The helper calls this
* as it sends the request, so that what throws here fails the stream. It records the final message
* on the span, which its `create()` call uses too.
*/
function startMessagesStreamSpan(client, body, options) {
if (!client._tracer)
return undefined;
// Built here too, where no span is open yet: the helper's `create()` call builds them again
// from the same input, so it cannot throw once the span is open.
const headers = messagesCreateHeaders(body, options);
return (0, tracing_1.startApiCallSpan)(client, 'anthropic.messages.create', options, (0, tracing_2.messagesCreateSpanSettings)(client, { ...transformOutputFormat(body), stream: true }, headers));
}
/**
* Transform deprecated output_format to output_config.format
* Returns a modified copy of the params without mutating the original
*/
function transformOutputFormat(params) {
if (!params.output_format) {
return params;
}
if (params.output_config?.format) {
throw new error_1.AnthropicError('Both output_format and output_config.format were provided. ' +
'Please use only output_config.format (output_format is deprecated).');
}
const { output_format, ...rest } = params;
return {
...rest,
output_config: {
...params.output_config,
format: output_format,
},
};
}
//# sourceMappingURL=beta-messages-request.js.map