@anthropic-ai/sdk
Version:
The official TypeScript library for the Anthropic API
82 lines • 3.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.messagesCreateSpanSettings = messagesCreateSpanSettings;
exports.sessionsCreateSpanAttributes = sessionsCreateSpanAttributes;
exports.recordMessagesCreateRequest = recordMessagesCreateRequest;
exports.recordMessagesCreateResponse = recordMessagesCreateResponse;
const streaming_1 = require("../../../core/streaming.js");
const tracing_1 = require("../../../internal/tracing.js");
const log_1 = require("../../../internal/utils/log.js");
const values_1 = require("../../../internal/utils/values.js");
const attributes_1 = require("./attributes.js");
const content_1 = require("./content.js");
const response_1 = require("./response.js");
/**
* What a `messages.create` span starts with from `request`, the options of its request.
* `startApiCallSpan` reads the attributes that one field of the body or one header gives. A sampler
* sees only the attributes that a span starts with.
*/
function messagesCreateSpanSettings(client, request) {
// This runs in the method, outside the guard of `startApiCallSpan`.
const attributes = (0, tracing_1.guardTracing)(client, () => {
// The beta method has already moved a deprecated `output_format` here.
const outputConfig = (0, values_1.isObj)(request.body) ? request.body['output_config'] : undefined;
const format = (0, values_1.isObj)(outputConfig) ? outputConfig['format'] : undefined;
return (0, values_1.isObj)(format) && format['type'] === 'json_schema' ?
{ [attributes_1.ATTR_GEN_AI_OUTPUT_TYPE]: attributes_1.GEN_AI_OUTPUT_TYPE_VALUE_JSON }
: {};
});
return { route: '/v1/messages', request, attributes };
}
/**
* The ID of the Agent that `body`, the body of a `sessions.create` request, names: the request
* gives it as a string or in an object. An `agent` that is neither is for the API to refuse.
*/
function sessionsCreateSpanAttributes(body) {
// This runs in the method, outside the guard of `startApiCallSpan`.
return (0, tracing_1.guardTracing)(undefined, () => {
const agent = (0, values_1.isObj)(body) ? body['agent'] : undefined;
const id = (0, values_1.isObj)(agent) ? agent['id'] : agent;
return typeof id === 'string' ? { [attributes_1.ATTR_GEN_AI_AGENT_ID]: id } : {};
});
}
/**
* In content mode, sets the request's content on `call`. The span does not start with it, because
* the size limit counts it together with the response's content. Returns `call`.
*/
function recordMessagesCreateRequest(client, call, body) {
if (!call?.span.isRecording()) {
return call;
}
try {
const { contentMode, maxContentBytes } = client.openTelemetry.traces;
if (contentMode === 'content') {
(0, content_1.setMessageContentAttributes)(call, body, undefined, maxContentBytes);
}
}
catch (problem) {
(0, log_1.loggerFor)(client).debug('could not record the request on its span', problem);
}
return call;
}
/**
* Sets the sums over the response's usage and, in content mode, the response's content on `call`.
* A streamed response is recorded as the application reads it
* (`endAnthropicApiCallSpanWhenStreamEnds`).
*/
function recordMessagesCreateResponse(client, call, body, data) {
if (!call?.span.isRecording() || data instanceof streaming_1.Stream) {
return;
}
try {
(0, response_1.setUsageSumAttributes)(call.span, data.usage, true);
const { contentMode, maxContentBytes } = client.openTelemetry.traces;
if (contentMode === 'content') {
(0, content_1.setMessageContentAttributes)(call, body, data.content, maxContentBytes);
}
}
catch (problem) {
(0, log_1.loggerFor)(client).debug('could not record the response on its span', problem);
}
}
//# sourceMappingURL=api-call.js.map