@anthropic-ai/sdk
Version:
The official TypeScript library for the Anthropic API
76 lines • 3.45 kB
JavaScript
import { Stream } from "../../../core/streaming.mjs";
import { guardTracing } from "../../../internal/tracing.mjs";
import { loggerFor } from "../../../internal/utils/log.mjs";
import { isObj } from "../../../internal/utils/values.mjs";
import { ATTR_GEN_AI_AGENT_ID, ATTR_GEN_AI_OUTPUT_TYPE, GEN_AI_OUTPUT_TYPE_VALUE_JSON } from "./attributes.mjs";
import { setMessageContentAttributes } from "./content.mjs";
import { setUsageSumAttributes } from "./response.mjs";
/**
* 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.
*/
export function messagesCreateSpanSettings(client, request) {
// This runs in the method, outside the guard of `startApiCallSpan`.
const attributes = guardTracing(client, () => {
// The beta method has already moved a deprecated `output_format` here.
const outputConfig = isObj(request.body) ? request.body['output_config'] : undefined;
const format = isObj(outputConfig) ? outputConfig['format'] : undefined;
return isObj(format) && format['type'] === 'json_schema' ?
{ [ATTR_GEN_AI_OUTPUT_TYPE]: 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.
*/
export function sessionsCreateSpanAttributes(body) {
// This runs in the method, outside the guard of `startApiCallSpan`.
return guardTracing(undefined, () => {
const agent = isObj(body) ? body['agent'] : undefined;
const id = isObj(agent) ? agent['id'] : agent;
return typeof id === 'string' ? { [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`.
*/
export function recordMessagesCreateRequest(client, call, body) {
if (!call?.span.isRecording()) {
return call;
}
try {
const { contentMode, maxContentBytes } = client.openTelemetry.traces;
if (contentMode === 'content') {
setMessageContentAttributes(call, body, undefined, maxContentBytes);
}
}
catch (problem) {
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`).
*/
export function recordMessagesCreateResponse(client, call, body, data) {
if (!call?.span.isRecording() || data instanceof Stream) {
return;
}
try {
setUsageSumAttributes(call.span, data.usage, true);
const { contentMode, maxContentBytes } = client.openTelemetry.traces;
if (contentMode === 'content') {
setMessageContentAttributes(call, body, data.content, maxContentBytes);
}
}
catch (problem) {
loggerFor(client).debug('could not record the response on its span', problem);
}
}
//# sourceMappingURL=api-call.mjs.map