@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 121 lines 3.6 KB
Raw
1 /**
2 * The `AnthropicTelemetry` module defines Anthropic-specific telemetry
3 * attributes and a helper for adding them to a tracing span. It keeps the
4 * standard GenAI telemetry attributes and adds request and response metadata
5 * under the `gen_ai.anthropic.*` OpenTelemetry namespaces.
6 *
7 * @stability unstable
8 * @since 4.0.0
9 */
10 import * as Telemetry from "effect/ai/Telemetry"
11 import { dual } from "effect/Function"
12 import * as String from "effect/String"
13 import type { Span } from "effect/Tracer"
14 import type { Simplify } from "effect/Types"
15
16 /**
17 * The attributes used to describe telemetry in the context of Generative
18 * Artificial Intelligence (GenAI) Models requests and responses.
19 *
20 * **Details**
21 *
22 * These attributes follow the OpenTelemetry generative AI semantic
23 * conventions:
24 * https://opentelemetry.io/docs/specs/semconv/attributes-registry/gen-ai/
25 *
26 * @stability unstable
27 * @category models
28 * @since 4.0.0
29 */
30 export type AnthropicTelemetryAttributes = Simplify<
31 & Telemetry.GenAITelemetryAttributes
32 & Telemetry.AttributesWithPrefix<RequestAttributes, "gen_ai.anthropic.request">
33 & Telemetry.AttributesWithPrefix<ResponseAttributes, "gen_ai.anthropic.response">
34 >
35
36 /**
37 * Telemetry attributes which are part of the GenAI specification and are
38 * namespaced by `gen_ai.anthropic.request`.
39 *
40 * @stability unstable
41 * @category models
42 * @since 4.0.0
43 */
44 export interface RequestAttributes {
45 /**
46 * Whether extended thinking is enabled.
47 */
48 readonly extendedThinking?: boolean | null | undefined
49 /**
50 * The budget tokens for extended thinking.
51 */
52 readonly thinkingBudgetTokens?: number | null | undefined
53 }
54
55 /**
56 * Telemetry attributes which are part of the GenAI specification and are
57 * namespaced by `gen_ai.anthropic.response`.
58 *
59 * @stability unstable
60 * @category models
61 * @since 4.0.0
62 */
63 export interface ResponseAttributes {
64 /**
65 * The stop reason from the response.
66 */
67 readonly stopReason?: string | null | undefined
68 }
69
70 /**
71 * Options accepted by `addGenAIAnnotations`, combining standard GenAI telemetry attributes with optional Anthropic request and response attributes.
72 *
73 * @stability unstable
74 * @category options
75 * @since 4.0.0
76 */
77 export type AnthropicTelemetryAttributeOptions = Telemetry.GenAITelemetryAttributeOptions & {
78 anthropic?: {
79 request?: RequestAttributes | undefined
80 response?: ResponseAttributes | undefined
81 } | undefined
82 }
83
84 const addAnthropicRequestAttributes = Telemetry.addSpanAttributes("gen_ai.anthropic.request", String.camelToSnake)<
85 RequestAttributes
86 >
87 const addAnthropicResponseAttributes = Telemetry.addSpanAttributes("gen_ai.anthropic.response", String.camelToSnake)<
88 ResponseAttributes
89 >
90
91 /**
92 * Applies the specified Anthropic GenAI telemetry attributes to the provided
93 * `Span`.
94 *
95 * **When to use**
96 *
97 * Use to annotate an Anthropic model span with standard GenAI telemetry
98 * attributes and Anthropic-specific request or response metadata.
99 *
100 * **Gotchas**
101 *
102 * This method mutates the `Span` in place.
103 *
104 * @stability unstable
105 * @category tracing
106 * @since 4.0.0
107 */
108 export const addGenAIAnnotations: {
109 (options: AnthropicTelemetryAttributeOptions): (span: Span) => void
110 (span: Span, options: AnthropicTelemetryAttributeOptions): void
111 } = dual(2, (span: Span, options: AnthropicTelemetryAttributeOptions) => {
112 Telemetry.addGenAIAnnotations(span, options)
113 if (options.anthropic != null) {
114 if (options.anthropic.request != null) {
115 addAnthropicRequestAttributes(span, options.anthropic.request)
116 }
117 if (options.anthropic.response != null) {
118 addAnthropicResponseAttributes(span, options.anthropic.response)
119 }
120 }
121 })