@aid-framework/runtime 0.0.0-stage → 0.1.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +52 -2
  3. package/dist/.tsbuildinfo +1 -0
  4. package/dist/agents/deep_agents.d.ts +78 -0
  5. package/dist/agents/deep_agents.d.ts.map +1 -0
  6. package/dist/agents/deep_agents.js +477 -0
  7. package/dist/agents/deep_agents.js.map +1 -0
  8. package/dist/agents/engine.d.ts +85 -0
  9. package/dist/agents/engine.d.ts.map +1 -0
  10. package/dist/agents/engine.js +129 -0
  11. package/dist/agents/engine.js.map +1 -0
  12. package/dist/agents/gateway_chat_model.d.ts +151 -0
  13. package/dist/agents/gateway_chat_model.d.ts.map +1 -0
  14. package/dist/agents/gateway_chat_model.js +350 -0
  15. package/dist/agents/gateway_chat_model.js.map +1 -0
  16. package/dist/agents/guardrail_middleware.d.ts +57 -0
  17. package/dist/agents/guardrail_middleware.d.ts.map +1 -0
  18. package/dist/agents/guardrail_middleware.js +85 -0
  19. package/dist/agents/guardrail_middleware.js.map +1 -0
  20. package/dist/agents/guardrails.d.ts +124 -0
  21. package/dist/agents/guardrails.d.ts.map +1 -0
  22. package/dist/agents/guardrails.js +336 -0
  23. package/dist/agents/guardrails.js.map +1 -0
  24. package/dist/agents/index.d.ts +15 -0
  25. package/dist/agents/index.d.ts.map +1 -0
  26. package/dist/agents/index.js +15 -0
  27. package/dist/agents/index.js.map +1 -0
  28. package/dist/agents/ir.d.ts +144 -0
  29. package/dist/agents/ir.d.ts.map +1 -0
  30. package/dist/agents/ir.js +513 -0
  31. package/dist/agents/ir.js.map +1 -0
  32. package/dist/agents/loop.d.ts +145 -0
  33. package/dist/agents/loop.d.ts.map +1 -0
  34. package/dist/agents/loop.js +1025 -0
  35. package/dist/agents/loop.js.map +1 -0
  36. package/dist/agents/memory.d.ts +115 -0
  37. package/dist/agents/memory.d.ts.map +1 -0
  38. package/dist/agents/memory.js +258 -0
  39. package/dist/agents/memory.js.map +1 -0
  40. package/dist/agents/state.d.ts +137 -0
  41. package/dist/agents/state.d.ts.map +1 -0
  42. package/dist/agents/state.js +255 -0
  43. package/dist/agents/state.js.map +1 -0
  44. package/dist/concurrency.d.ts +36 -0
  45. package/dist/concurrency.d.ts.map +1 -0
  46. package/dist/concurrency.js +86 -0
  47. package/dist/concurrency.js.map +1 -0
  48. package/dist/control.d.ts +116 -0
  49. package/dist/control.d.ts.map +1 -0
  50. package/dist/control.js +281 -0
  51. package/dist/control.js.map +1 -0
  52. package/dist/cost.d.ts +95 -0
  53. package/dist/cost.d.ts.map +1 -0
  54. package/dist/cost.js +161 -0
  55. package/dist/cost.js.map +1 -0
  56. package/dist/errors.d.ts +213 -0
  57. package/dist/errors.d.ts.map +1 -0
  58. package/dist/errors.js +316 -0
  59. package/dist/errors.js.map +1 -0
  60. package/dist/evals/dataset.d.ts +45 -0
  61. package/dist/evals/dataset.d.ts.map +1 -0
  62. package/dist/evals/dataset.js +147 -0
  63. package/dist/evals/dataset.js.map +1 -0
  64. package/dist/evals/gate.d.ts +236 -0
  65. package/dist/evals/gate.d.ts.map +1 -0
  66. package/dist/evals/gate.js +572 -0
  67. package/dist/evals/gate.js.map +1 -0
  68. package/dist/evals/metrics.d.ts +141 -0
  69. package/dist/evals/metrics.d.ts.map +1 -0
  70. package/dist/evals/metrics.js +364 -0
  71. package/dist/evals/metrics.js.map +1 -0
  72. package/dist/evals/predicate.d.ts +79 -0
  73. package/dist/evals/predicate.d.ts.map +1 -0
  74. package/dist/evals/predicate.js +178 -0
  75. package/dist/evals/predicate.js.map +1 -0
  76. package/dist/evals/runner.d.ts +94 -0
  77. package/dist/evals/runner.d.ts.map +1 -0
  78. package/dist/evals/runner.js +202 -0
  79. package/dist/evals/runner.js.map +1 -0
  80. package/dist/gateway.d.ts +118 -0
  81. package/dist/gateway.d.ts.map +1 -0
  82. package/dist/gateway.js +250 -0
  83. package/dist/gateway.js.map +1 -0
  84. package/dist/index.d.ts +83 -0
  85. package/dist/index.d.ts.map +1 -0
  86. package/dist/index.js +59 -0
  87. package/dist/index.js.map +1 -0
  88. package/dist/internal/json.d.ts +44 -0
  89. package/dist/internal/json.d.ts.map +1 -0
  90. package/dist/internal/json.js +90 -0
  91. package/dist/internal/json.js.map +1 -0
  92. package/dist/internal/text.d.ts +17 -0
  93. package/dist/internal/text.d.ts.map +1 -0
  94. package/dist/internal/text.js +22 -0
  95. package/dist/internal/text.js.map +1 -0
  96. package/dist/pipeline.d.ts +80 -0
  97. package/dist/pipeline.d.ts.map +1 -0
  98. package/dist/pipeline.js +159 -0
  99. package/dist/pipeline.js.map +1 -0
  100. package/dist/policy.d.ts +52 -0
  101. package/dist/policy.d.ts.map +1 -0
  102. package/dist/policy.js +199 -0
  103. package/dist/policy.js.map +1 -0
  104. package/dist/prompts.d.ts +86 -0
  105. package/dist/prompts.d.ts.map +1 -0
  106. package/dist/prompts.js +337 -0
  107. package/dist/prompts.js.map +1 -0
  108. package/dist/providers/base.d.ts +19 -0
  109. package/dist/providers/base.d.ts.map +1 -0
  110. package/dist/providers/base.js +13 -0
  111. package/dist/providers/base.js.map +1 -0
  112. package/dist/providers/fake.d.ts +98 -0
  113. package/dist/providers/fake.d.ts.map +1 -0
  114. package/dist/providers/fake.js +181 -0
  115. package/dist/providers/fake.js.map +1 -0
  116. package/dist/providers/http.d.ts +62 -0
  117. package/dist/providers/http.d.ts.map +1 -0
  118. package/dist/providers/http.js +245 -0
  119. package/dist/providers/http.js.map +1 -0
  120. package/dist/retrieval/chunking.d.ts +88 -0
  121. package/dist/retrieval/chunking.d.ts.map +1 -0
  122. package/dist/retrieval/chunking.js +188 -0
  123. package/dist/retrieval/chunking.js.map +1 -0
  124. package/dist/retrieval/citations.d.ts +98 -0
  125. package/dist/retrieval/citations.d.ts.map +1 -0
  126. package/dist/retrieval/citations.js +134 -0
  127. package/dist/retrieval/citations.js.map +1 -0
  128. package/dist/retrieval/connectors.d.ts +74 -0
  129. package/dist/retrieval/connectors.d.ts.map +1 -0
  130. package/dist/retrieval/connectors.js +218 -0
  131. package/dist/retrieval/connectors.js.map +1 -0
  132. package/dist/retrieval/embedding.d.ts +116 -0
  133. package/dist/retrieval/embedding.d.ts.map +1 -0
  134. package/dist/retrieval/embedding.js +282 -0
  135. package/dist/retrieval/embedding.js.map +1 -0
  136. package/dist/retrieval/gate.d.ts +52 -0
  137. package/dist/retrieval/gate.d.ts.map +1 -0
  138. package/dist/retrieval/gate.js +91 -0
  139. package/dist/retrieval/gate.js.map +1 -0
  140. package/dist/retrieval/hybrid.d.ts +76 -0
  141. package/dist/retrieval/hybrid.d.ts.map +1 -0
  142. package/dist/retrieval/hybrid.js +150 -0
  143. package/dist/retrieval/hybrid.js.map +1 -0
  144. package/dist/retrieval/metrics.d.ts +126 -0
  145. package/dist/retrieval/metrics.d.ts.map +1 -0
  146. package/dist/retrieval/metrics.js +200 -0
  147. package/dist/retrieval/metrics.js.map +1 -0
  148. package/dist/retrieval/pgvector.d.ts +62 -0
  149. package/dist/retrieval/pgvector.d.ts.map +1 -0
  150. package/dist/retrieval/pgvector.js +262 -0
  151. package/dist/retrieval/pgvector.js.map +1 -0
  152. package/dist/retrieval/rerank.d.ts +61 -0
  153. package/dist/retrieval/rerank.d.ts.map +1 -0
  154. package/dist/retrieval/rerank.js +105 -0
  155. package/dist/retrieval/rerank.js.map +1 -0
  156. package/dist/retrieval/retriever.d.ts +125 -0
  157. package/dist/retrieval/retriever.d.ts.map +1 -0
  158. package/dist/retrieval/retriever.js +281 -0
  159. package/dist/retrieval/retriever.js.map +1 -0
  160. package/dist/retrieval/stores.d.ts +106 -0
  161. package/dist/retrieval/stores.d.ts.map +1 -0
  162. package/dist/retrieval/stores.js +142 -0
  163. package/dist/retrieval/stores.js.map +1 -0
  164. package/dist/retrieval/types.d.ts +314 -0
  165. package/dist/retrieval/types.d.ts.map +1 -0
  166. package/dist/retrieval/types.js +388 -0
  167. package/dist/retrieval/types.js.map +1 -0
  168. package/dist/steps.d.ts +156 -0
  169. package/dist/steps.d.ts.map +1 -0
  170. package/dist/steps.js +199 -0
  171. package/dist/steps.js.map +1 -0
  172. package/dist/structured.d.ts +91 -0
  173. package/dist/structured.d.ts.map +1 -0
  174. package/dist/structured.js +155 -0
  175. package/dist/structured.js.map +1 -0
  176. package/dist/tools.d.ts +100 -0
  177. package/dist/tools.d.ts.map +1 -0
  178. package/dist/tools.js +201 -0
  179. package/dist/tools.js.map +1 -0
  180. package/dist/tracing.d.ts +104 -0
  181. package/dist/tracing.d.ts.map +1 -0
  182. package/dist/tracing.js +155 -0
  183. package/dist/tracing.js.map +1 -0
  184. package/dist/types.d.ts +240 -0
  185. package/dist/types.d.ts.map +1 -0
  186. package/dist/types.js +357 -0
  187. package/dist/types.js.map +1 -0
  188. package/package.json +77 -3
package/dist/tools.js ADDED
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Tool dispatch with schema validation.
3
+ *
4
+ * The one place this module cannot mirror the reference runtime is the seam itself:
5
+ * Python infers a tool's JSON schema from `inspect.signature` and a docstring, and
6
+ * JavaScript has neither. So the schema is declared explicitly and validated against
7
+ * the tool set here instead -- the runtime's job is to refuse a *wrong* invocation,
8
+ * and that job is identical either way.
9
+ *
10
+ * The validator implements a deliberate subset of JSON Schema: `type`, `required`
11
+ * and `additionalProperties: false`. That subset is documented rather than silently
12
+ * partial, and an unknown `type` value is treated as unconstrained instead of
13
+ * failing, because a schema written for a newer draft must not reject every call.
14
+ */
15
+ import { ConfigurationError, ToolExecutionError, ToolNotFoundError } from './errors.js';
16
+ import { dumpsSorted, repr } from './internal/json.js';
17
+ import { ToolDefinition, ToolResult } from './types.js';
18
+ /** A registered tool: its public definition plus the code behind it. */
19
+ export class Tool {
20
+ definition_;
21
+ handler;
22
+ constructor(init) {
23
+ this.definition_ = new ToolDefinition({
24
+ name: init.name,
25
+ description: init.description,
26
+ parameters: (init.parameters ?? { type: 'object', properties: {} }),
27
+ });
28
+ this.handler = init.handler;
29
+ this.checkSchema();
30
+ }
31
+ /**
32
+ * Declare a tool.
33
+ *
34
+ * `parameters` is required where Python would infer it. A schema whose `required`
35
+ * list names a property it does not define is a wiring mistake, and it is reported
36
+ * here rather than at the first call: that is the same failure Python reports as
37
+ * "not accepted by the handler", just discovered from the other direction.
38
+ */
39
+ static of(init) {
40
+ return new Tool(init);
41
+ }
42
+ get name() {
43
+ return this.definition_.name;
44
+ }
45
+ get definition() {
46
+ return this.definition_;
47
+ }
48
+ checkSchema() {
49
+ const schema = this.definition_.parameters;
50
+ for (const property of schema.required ?? []) {
51
+ if (schema.properties === undefined || !(property in schema.properties)) {
52
+ throw new ConfigurationError(`tool ${repr(this.name)} declares required parameter ${repr(property)} but its schema does not define it, so it is not accepted by the handler`);
53
+ }
54
+ }
55
+ }
56
+ }
57
+ function isObject(value) {
58
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
59
+ }
60
+ function typeMatches(expected, value) {
61
+ const names = Array.isArray(expected) ? expected : [expected];
62
+ return names.some((name) => singleTypeMatches(name, value));
63
+ }
64
+ function singleTypeMatches(expected, value) {
65
+ if (typeof expected !== 'string')
66
+ return true;
67
+ switch (expected) {
68
+ case 'string':
69
+ return typeof value === 'string';
70
+ case 'integer':
71
+ return typeof value === 'number' && Number.isInteger(value);
72
+ case 'number':
73
+ // A boolean is not a number, even though `true + 0` is 1.
74
+ return typeof value === 'number';
75
+ case 'boolean':
76
+ return typeof value === 'boolean';
77
+ case 'object':
78
+ return isObject(value);
79
+ case 'array':
80
+ return Array.isArray(value);
81
+ case 'null':
82
+ return value === null;
83
+ default:
84
+ // An unknown type keyword is not a claim we can check, so it matches.
85
+ return true;
86
+ }
87
+ }
88
+ /**
89
+ * Validate `args` against `schema`, returning every problem rather than the first.
90
+ *
91
+ * Returning all problems is what makes a single repair turn enough: a model told
92
+ * about one bad field per round trips once per field.
93
+ */
94
+ export function validateArguments(schema, args) {
95
+ if (!isObject(args))
96
+ return ['arguments must be an object'];
97
+ const problems = [];
98
+ const properties = schema.properties ?? {};
99
+ for (const name of schema.required ?? []) {
100
+ if (!(name in args))
101
+ problems.push(`missing required property '${name}'`);
102
+ }
103
+ if (schema.additionalProperties === false) {
104
+ for (const name of Object.keys(args)) {
105
+ if (!(name in properties))
106
+ problems.push(`unexpected property '${name}'`);
107
+ }
108
+ }
109
+ for (const [name, rule] of Object.entries(properties)) {
110
+ if (!(name in args))
111
+ continue;
112
+ const value = args[name];
113
+ if (value === null || value === undefined)
114
+ continue;
115
+ if (rule.type !== undefined && !typeMatches(rule.type, value)) {
116
+ problems.push(`property '${name}' must be of type ${String(rule.type)}`);
117
+ }
118
+ }
119
+ return problems;
120
+ }
121
+ /**
122
+ * A set of tools, and the dispatcher for them.
123
+ *
124
+ * `strict` decides what a crashing handler means. The default is to hand the model
125
+ * an error result so it can react; strict mode rethrows, because a caller that has
126
+ * already decided a crash is a bug should not have to inspect a result to find out.
127
+ */
128
+ export class ToolExecutor {
129
+ _tools = new Map();
130
+ _strict;
131
+ _limiter;
132
+ constructor(tools = [], options = {}) {
133
+ this._strict = options.strict ?? false;
134
+ this._limiter = options.limiter;
135
+ for (const tool of tools)
136
+ this.register(tool);
137
+ }
138
+ /** The registered names, sorted. */
139
+ names() {
140
+ return [...this._tools.keys()].sort();
141
+ }
142
+ /** Add a tool. A duplicate name replaces, because a registry is a map. */
143
+ register(tool) {
144
+ this._tools.set(tool.name, tool);
145
+ }
146
+ /** One tool's public definition. */
147
+ definition(name) {
148
+ return this.require(name).definition;
149
+ }
150
+ /** Every registered definition, ordered by name. */
151
+ definitions() {
152
+ return this.names().map((name) => this.require(name).definition);
153
+ }
154
+ /** Whether a name is registered. */
155
+ has(name) {
156
+ return this._tools.has(name);
157
+ }
158
+ /**
159
+ * Run one call.
160
+ *
161
+ * An unknown tool throws: that is a wiring error in generated code, not something
162
+ * a model can fix by trying again with the same name.
163
+ */
164
+ async execute(call) {
165
+ // When a limiter is shared with the gateway, the call holds one slot for its whole
166
+ // duration, released on success, handler error, or cancellation.
167
+ if (this._limiter === undefined)
168
+ return this.dispatch(call);
169
+ return this._limiter.run(() => this.dispatch(call));
170
+ }
171
+ async dispatch(call) {
172
+ const tool = this.require(call.name);
173
+ const problems = validateArguments(tool.definition.parameters, call.args);
174
+ if (problems.length > 0) {
175
+ return ToolResult.fromError(call, `invalid arguments: ${problems.join('; ')} -- ${dumpsSorted(call.args)}`);
176
+ }
177
+ try {
178
+ return ToolResult.fromValue(call, await tool.handler(call.args));
179
+ }
180
+ catch (error) {
181
+ const detail = error instanceof Error ? `${error.name}: ${error.message}` : String(error);
182
+ if (this._strict)
183
+ throw new ToolExecutionError(detail);
184
+ return ToolResult.fromError(call, detail);
185
+ }
186
+ }
187
+ /** Run calls in order. Sequential on purpose: a tool set is a transcript, not a race. */
188
+ async executeAll(calls) {
189
+ const results = [];
190
+ for (const call of calls)
191
+ results.push(await this.execute(call));
192
+ return results;
193
+ }
194
+ require(name) {
195
+ const tool = this._tools.get(name);
196
+ if (tool === undefined)
197
+ throw new ToolNotFoundError(name, this.names());
198
+ return tool;
199
+ }
200
+ }
201
+ //# sourceMappingURL=tools.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tools.js","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACxF,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAiB,cAAc,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAavE,wEAAwE;AACxE,MAAM,OAAO,IAAI;IACN,WAAW,CAAiB;IAC5B,OAAO,CAAc;IAE9B,YAAY,IAKX;QACC,IAAI,CAAC,WAAW,GAAG,IAAI,cAAc,CAAC;YACpC,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,WAAW,EAAE,IAAI,CAAC,WAAW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AAO,IAAI,CAAC;IACd,CAAC;CACF"}
@@ -0,0 +1,104 @@
1
+ /**
2
+ * OpenTelemetry GenAI tracing.
3
+ *
4
+ * The attribute spellings are the GenAI semantic conventions, not house names, so a
5
+ * span this runtime emits lands in an existing dashboard without a translation
6
+ * layer. The three `aid.*` attributes are the additions the conventions have no
7
+ * opinion about: which alias was asked for, what it cost, and how many candidates
8
+ * were tried before one answered.
9
+ *
10
+ * The tracer is resolved from the global provider, so an application that never
11
+ * configures one gets a no-op tracer and no cost. That matters: a runtime that
12
+ * demanded a tracer would make tracing a precondition for calling a model.
13
+ */
14
+ import { type Attributes, type Span, trace } from '@opentelemetry/api';
15
+ /** The operation name the chat completion span carries. */
16
+ export declare const GEN_AI_OPERATION_CHAT = "chat";
17
+ /** Semantic-convention attribute names, keyed by the option that sets them. */
18
+ export declare const ATTRIBUTES: {
19
+ readonly system: 'gen_ai.system';
20
+ readonly operation: 'gen_ai.operation.name';
21
+ readonly requestModel: 'gen_ai.request.model';
22
+ readonly requestTemperature: 'gen_ai.request.temperature';
23
+ readonly requestMaxTokens: 'gen_ai.request.max_tokens';
24
+ readonly responseModel: 'gen_ai.response.model';
25
+ readonly responseFinishReasons: 'gen_ai.response.finish_reasons';
26
+ readonly inputTokens: 'gen_ai.usage.input_tokens';
27
+ readonly outputTokens: 'gen_ai.usage.output_tokens';
28
+ readonly alias: 'aid.model.alias';
29
+ readonly costUsd: 'aid.cost.usd';
30
+ readonly attempts: 'aid.gateway.attempts';
31
+ readonly toolNames: 'aid.tool.names';
32
+ };
33
+ /** Everything a chat span may record. Unset members are omitted entirely. */
34
+ export interface LlmSpanOptions {
35
+ readonly operation?: string;
36
+ readonly model: string;
37
+ readonly system?: string;
38
+ readonly requestTemperature?: number;
39
+ readonly requestMaxTokens?: number;
40
+ readonly responseModel?: string;
41
+ readonly responseFinishReasons?: readonly string[];
42
+ readonly inputTokens?: number;
43
+ readonly outputTokens?: number;
44
+ readonly alias?: string;
45
+ readonly costUsd?: number;
46
+ readonly attempts?: number;
47
+ readonly toolNames?: readonly string[];
48
+ readonly tracerName?: string;
49
+ }
50
+ /** The default tracer name, matching the reference runtime. */
51
+ export declare const DEFAULT_TRACER_NAME = "aid_runtime";
52
+ /** Resolve a tracer from the global provider. */
53
+ export declare function getTracer(name?: string): ReturnType<typeof trace.getTracer>;
54
+ /**
55
+ * Turn span options into OTel attributes.
56
+ *
57
+ * An unset option contributes no key at all rather than a defaulted zero: a span
58
+ * that reports `gen_ai.usage.input_tokens = 0` for a call whose usage was never
59
+ * measured is a lie a dashboard will happily average.
60
+ */
61
+ export declare function spanAttributes(options: LlmSpanOptions): Attributes;
62
+ /** The name a chat span carries: `"{operation} {model}"`. */
63
+ export declare function spanName(options: LlmSpanOptions): string;
64
+ /** A started chat span plus the operations that end it. */
65
+ export interface LlmSpanHandle {
66
+ readonly span: Span;
67
+ /** Record usage on an already-started span. */
68
+ recordUsage(inputTokens: number, outputTokens: number): void;
69
+ /** Record cost. Does not touch the alias attribute. */
70
+ recordCost(costUsd: number): void;
71
+ /** Record an exception once, and mark the span failed. */
72
+ recordException(error: unknown): void;
73
+ /** End the span. Idempotent. */
74
+ end(): void;
75
+ }
76
+ /** Start a chat span with the standard attributes applied. */
77
+ export declare function llmSpan(options: LlmSpanOptions): LlmSpanHandle;
78
+ /**
79
+ * Run `body` inside a chat span, recording an exception and marking it failed on
80
+ * throw.
81
+ *
82
+ * Automatic exception recording is off (`record_exception: false` /
83
+ * `set_status_on_exception: false` in the reference runtime) so the exception is
84
+ * recorded exactly once, by this function, rather than once by the SDK and once by
85
+ * the caller's wrapper.
86
+ */
87
+ export declare function withLlmSpan<T>(options: LlmSpanOptions, body: (handle: LlmSpanHandle) => Promise<T>): Promise<T>;
88
+ /** Record usage on an existing span. */
89
+ export declare function recordUsage(span: Span, inputTokens: number, outputTokens: number): void;
90
+ /** Record cost on an existing span. Deliberately leaves the alias attribute alone. */
91
+ export declare function recordCost(span: Span, costUsd: number): void;
92
+ /**
93
+ * Record an exception once and mark the span failed.
94
+ *
95
+ * `error` is `unknown` because a caught value in TypeScript is not guaranteed to be
96
+ * an `Error`, and a runtime that assumed otherwise would throw while reporting a
97
+ * failure.
98
+ */
99
+ export declare function recordException(span: Span, error: unknown): void;
100
+ /** Build an attribute bag from arbitrary named values, passing unknown keys through. */
101
+ export declare function aidAttributes(values: Readonly<Record<string, string | number | boolean>>): {
102
+ [key: string]: string | number | boolean;
103
+ };
104
+ //# sourceMappingURL=tracing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracing.d.ts","sourceRoot":"","sources":["../src/tracing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,IAAI,EAAkB,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAEvF,2DAA2D;AAC3D,eAAO,MAAM,qBAAqB,SAAS,CAAC;AAE5C,+EAA+E;AAC/E,eAAO,MAAM,UAAU;aACrB,MAAM,EAAE,eAAe;aACvB,SAAS,EAAE,uBAAuB;aAClC,YAAY,EAAE,sBAAsB;aACpC,kBAAkB,EAAE,4BAA4B;aAChD,gBAAgB,EAAE,2BAA2B;aAC7C,aAAa,EAAE,uBAAuB;aACtC,qBAAqB,EAAE,gCAAgC;aACvD,WAAW,EAAE,2BAA2B;aACxC,YAAY,EAAE,4BAA4B;aAC1C,KAAK,EAAE,iBAAiB;aACxB,OAAO,EAAE,cAAc;aACvB,QAAQ,EAAE,sBAAsB;aAChC,SAAS,EAAE,gBAAgB;CACnB,CAAC;AAEX,6EAA6E;AAC7E,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,kBAAkB,CAAC,EAAE,MAAM,CAAC;IACrC,QAAQ,CAAC,gBAAgB,CAAC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,OAAO,EAAE,cAAc,GAAG,aAAa,CAsB9D;AAED;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAAC,CAAC,EACjC,OAAO,EAAE,cAAc,EACvB,IAAI,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,OAAO,CAAC,CAAC,CAAC,GAC1C,OAAO,CAAC,CAAC,CAAC,CAUZ;AAED,wCAAwC;AACxC,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,IAAI,CAGvF;AAED,sFAAsF;AACtF,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAE5D;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI,CAKhE;AAED,wFAAwF;AACxF,wBAAgB,aAAa,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG;IAC1F,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;CAC1C,CAEA"}
@@ -0,0 +1,155 @@
1
+ /**
2
+ * OpenTelemetry GenAI tracing.
3
+ *
4
+ * The attribute spellings are the GenAI semantic conventions, not house names, so a
5
+ * span this runtime emits lands in an existing dashboard without a translation
6
+ * layer. The three `aid.*` attributes are the additions the conventions have no
7
+ * opinion about: which alias was asked for, what it cost, and how many candidates
8
+ * were tried before one answered.
9
+ *
10
+ * The tracer is resolved from the global provider, so an application that never
11
+ * configures one gets a no-op tracer and no cost. That matters: a runtime that
12
+ * demanded a tracer would make tracing a precondition for calling a model.
13
+ */
14
+ import { SpanStatusCode, trace } from '@opentelemetry/api';
15
+ /** The operation name the chat completion span carries. */
16
+ export const GEN_AI_OPERATION_CHAT = 'chat';
17
+ /** Semantic-convention attribute names, keyed by the option that sets them. */
18
+ export const ATTRIBUTES = {
19
+ system: 'gen_ai.system',
20
+ operation: 'gen_ai.operation.name',
21
+ requestModel: 'gen_ai.request.model',
22
+ requestTemperature: 'gen_ai.request.temperature',
23
+ requestMaxTokens: 'gen_ai.request.max_tokens',
24
+ responseModel: 'gen_ai.response.model',
25
+ responseFinishReasons: 'gen_ai.response.finish_reasons',
26
+ inputTokens: 'gen_ai.usage.input_tokens',
27
+ outputTokens: 'gen_ai.usage.output_tokens',
28
+ alias: 'aid.model.alias',
29
+ costUsd: 'aid.cost.usd',
30
+ attempts: 'aid.gateway.attempts',
31
+ toolNames: 'aid.tool.names',
32
+ };
33
+ /** The default tracer name, matching the reference runtime. */
34
+ export const DEFAULT_TRACER_NAME = 'aid_runtime';
35
+ /** Resolve a tracer from the global provider. */
36
+ export function getTracer(name = DEFAULT_TRACER_NAME) {
37
+ return trace.getTracer(name);
38
+ }
39
+ /**
40
+ * Turn span options into OTel attributes.
41
+ *
42
+ * An unset option contributes no key at all rather than a defaulted zero: a span
43
+ * that reports `gen_ai.usage.input_tokens = 0` for a call whose usage was never
44
+ * measured is a lie a dashboard will happily average.
45
+ */
46
+ export function spanAttributes(options) {
47
+ const attributes = {};
48
+ const operation = options.operation ?? GEN_AI_OPERATION_CHAT;
49
+ attributes[ATTRIBUTES.operation] = operation;
50
+ attributes[ATTRIBUTES.requestModel] = options.model;
51
+ if (options.system !== undefined)
52
+ attributes[ATTRIBUTES.system] = options.system;
53
+ if (options.requestTemperature !== undefined) {
54
+ attributes[ATTRIBUTES.requestTemperature] = options.requestTemperature;
55
+ }
56
+ if (options.requestMaxTokens !== undefined) {
57
+ attributes[ATTRIBUTES.requestMaxTokens] = options.requestMaxTokens;
58
+ }
59
+ if (options.responseModel !== undefined) {
60
+ attributes[ATTRIBUTES.responseModel] = options.responseModel;
61
+ }
62
+ if (options.responseFinishReasons !== undefined) {
63
+ attributes[ATTRIBUTES.responseFinishReasons] = [...options.responseFinishReasons];
64
+ }
65
+ if (options.inputTokens !== undefined)
66
+ attributes[ATTRIBUTES.inputTokens] = options.inputTokens;
67
+ if (options.outputTokens !== undefined)
68
+ attributes[ATTRIBUTES.outputTokens] = options.outputTokens;
69
+ if (options.alias !== undefined)
70
+ attributes[ATTRIBUTES.alias] = options.alias;
71
+ if (options.costUsd !== undefined)
72
+ attributes[ATTRIBUTES.costUsd] = options.costUsd;
73
+ if (options.attempts !== undefined)
74
+ attributes[ATTRIBUTES.attempts] = options.attempts;
75
+ if (options.toolNames !== undefined)
76
+ attributes[ATTRIBUTES.toolNames] = [...options.toolNames];
77
+ return attributes;
78
+ }
79
+ /** The name a chat span carries: `"{operation} {model}"`. */
80
+ export function spanName(options) {
81
+ return `${options.operation ?? GEN_AI_OPERATION_CHAT} ${options.model}`;
82
+ }
83
+ /** Start a chat span with the standard attributes applied. */
84
+ export function llmSpan(options) {
85
+ const tracer = getTracer(options.tracerName ?? DEFAULT_TRACER_NAME);
86
+ const span = tracer.startSpan(spanName(options), { attributes: spanAttributes(options) });
87
+ let ended = false;
88
+ return {
89
+ span,
90
+ recordUsage(inputTokens, outputTokens) {
91
+ span.setAttribute(ATTRIBUTES.inputTokens, inputTokens);
92
+ span.setAttribute(ATTRIBUTES.outputTokens, outputTokens);
93
+ },
94
+ recordCost(costUsd) {
95
+ span.setAttribute(ATTRIBUTES.costUsd, costUsd);
96
+ },
97
+ recordException(error) {
98
+ recordException(span, error);
99
+ },
100
+ end() {
101
+ if (ended)
102
+ return;
103
+ ended = true;
104
+ span.end();
105
+ },
106
+ };
107
+ }
108
+ /**
109
+ * Run `body` inside a chat span, recording an exception and marking it failed on
110
+ * throw.
111
+ *
112
+ * Automatic exception recording is off (`record_exception: false` /
113
+ * `set_status_on_exception: false` in the reference runtime) so the exception is
114
+ * recorded exactly once, by this function, rather than once by the SDK and once by
115
+ * the caller's wrapper.
116
+ */
117
+ export async function withLlmSpan(options, body) {
118
+ const handle = llmSpan(options);
119
+ try {
120
+ return await body(handle);
121
+ }
122
+ catch (error) {
123
+ handle.recordException(error);
124
+ throw error;
125
+ }
126
+ finally {
127
+ handle.end();
128
+ }
129
+ }
130
+ /** Record usage on an existing span. */
131
+ export function recordUsage(span, inputTokens, outputTokens) {
132
+ span.setAttribute(ATTRIBUTES.inputTokens, inputTokens);
133
+ span.setAttribute(ATTRIBUTES.outputTokens, outputTokens);
134
+ }
135
+ /** Record cost on an existing span. Deliberately leaves the alias attribute alone. */
136
+ export function recordCost(span, costUsd) {
137
+ span.setAttribute(ATTRIBUTES.costUsd, costUsd);
138
+ }
139
+ /**
140
+ * Record an exception once and mark the span failed.
141
+ *
142
+ * `error` is `unknown` because a caught value in TypeScript is not guaranteed to be
143
+ * an `Error`, and a runtime that assumed otherwise would throw while reporting a
144
+ * failure.
145
+ */
146
+ export function recordException(span, error) {
147
+ const asError = error instanceof Error ? error : new Error(typeof error === 'string' ? error : String(error));
148
+ span.recordException(asError);
149
+ span.setStatus({ code: SpanStatusCode.ERROR, message: asError.message });
150
+ }
151
+ /** Build an attribute bag from arbitrary named values, passing unknown keys through. */
152
+ export function aidAttributes(values) {
153
+ return { ...values };
154
+ }
155
+ //# sourceMappingURL=tracing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracing.js","sourceRoot":"","sources":["../src/tracing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAA8B,cAAc,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAEvF,2DAA2D;AAC3D,MAAM,CAAC,MAAM,qBAAqB,GAAG,MAAM,CAAC;AAE5C,+EAA+E;AAC/E,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,MAAM,EAAE,eAAe;IACvB,SAAS,EAAE,uBAAuB;IAClC,YAAY,EAAE,sBAAsB;IACpC,kBAAkB,EAAE,4BAA4B;IAChD,gBAAgB,EAAE,2BAA2B;IAC7C,aAAa,EAAE,uBAAuB;IACtC,qBAAqB,EAAE,gCAAgC;IACvD,WAA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