@hue-run/sdk 0.1.4 → 0.2.0

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 (58) hide show
  1. package/ENVIRONMENTS.md +182 -0
  2. package/EVALUATIONS.md +12 -0
  3. package/README.md +204 -21
  4. package/dist/ai-sdk.d.ts +9 -1
  5. package/dist/ai-sdk.js +37 -2
  6. package/dist/client.d.ts +130 -5
  7. package/dist/client.js +518 -110
  8. package/dist/config.d.ts +11 -2
  9. package/dist/config.js +50 -4
  10. package/dist/environment/client.d.ts +73 -0
  11. package/dist/environment/client.js +209 -0
  12. package/dist/environment/tools.d.ts +30 -0
  13. package/dist/environment/tools.js +24 -0
  14. package/dist/environment/types.d.ts +429 -0
  15. package/dist/environment/types.js +1 -0
  16. package/dist/environment.d.ts +5 -0
  17. package/dist/environment.js +2 -0
  18. package/dist/evals/attempt.d.ts +454 -0
  19. package/dist/evals/attempt.js +687 -0
  20. package/dist/evals/client.d.ts +99 -5
  21. package/dist/evals/client.js +136 -7
  22. package/dist/evals/environment-evidence.d.ts +6 -0
  23. package/dist/evals/environment-evidence.js +123 -0
  24. package/dist/evals/environment-json.d.ts +3 -0
  25. package/dist/evals/environment-json.js +76 -0
  26. package/dist/evals/json.d.ts +9 -1
  27. package/dist/evals/json.js +14 -6
  28. package/dist/evals/runner.d.ts +61 -2
  29. package/dist/evals/runner.js +71 -9
  30. package/dist/evals/scorer-publication.d.ts +2 -0
  31. package/dist/evals/scorer-publication.js +84 -0
  32. package/dist/evals/scorers.d.ts +11 -0
  33. package/dist/evals/scorers.js +56 -5
  34. package/dist/evals/simulation.d.ts +184 -0
  35. package/dist/evals/simulation.js +603 -0
  36. package/dist/evals/types.d.ts +304 -0
  37. package/dist/evals.d.ts +5 -1
  38. package/dist/evals.js +3 -1
  39. package/dist/experimental-telemetry.d.ts +8 -0
  40. package/dist/experimental-telemetry.js +13 -0
  41. package/dist/index.d.ts +4 -1
  42. package/dist/index.js +3 -1
  43. package/dist/managed.d.ts +51 -1
  44. package/dist/managed.js +11 -1
  45. package/dist/privacy.d.ts +2 -0
  46. package/dist/privacy.js +54 -21
  47. package/dist/receipt.d.ts +12 -1
  48. package/dist/receipt.js +10 -1
  49. package/dist/safety.d.ts +7 -0
  50. package/dist/safety.js +179 -0
  51. package/dist/snapshot.d.ts +12 -0
  52. package/dist/snapshot.js +200 -0
  53. package/dist/transport.d.ts +46 -8
  54. package/dist/transport.js +266 -48
  55. package/dist/types.d.ts +167 -8
  56. package/dist/version.d.ts +2 -0
  57. package/dist/version.js +3 -0
  58. package/package.json +51 -15
@@ -0,0 +1,200 @@
1
+ import { createTraceState } from "@opentelemetry/api";
2
+ import { types as utilTypes } from "node:util";
3
+ import { resourceFromAttributes } from "@opentelemetry/resources";
4
+ // Intrinsic accessors are captured once and invoked with an explicit receiver so a
5
+ // hostile object cannot override them; the unbound reference is the point.
6
+ // eslint-disable-next-line @typescript-eslint/unbound-method
7
+ const typedArrayByteLength = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), "byteLength").get;
8
+ // eslint-disable-next-line @typescript-eslint/unbound-method
9
+ const typedArraySet = Uint8Array.prototype.set;
10
+ /** Copies only exported data, with the same finite budget used for admission. */
11
+ class Snapshot {
12
+ limit;
13
+ bytes = 512;
14
+ unresolvedResource = false;
15
+ nodes = 0;
16
+ ancestors = new Set();
17
+ copied = new Map();
18
+ constructor(limit) {
19
+ this.limit = limit;
20
+ }
21
+ charge(bytes) {
22
+ this.bytes += bytes;
23
+ if (this.bytes > this.limit)
24
+ throw new RangeError("Telemetry byte budget exceeded");
25
+ }
26
+ copy(value, depth = 0) {
27
+ if (++this.nodes > 16384 || depth > 32)
28
+ throw new RangeError("Telemetry complexity limit exceeded");
29
+ this.charge(16);
30
+ if (typeof value === "string") {
31
+ this.charge(value.length * 2);
32
+ return value;
33
+ }
34
+ if (value === null ||
35
+ value === undefined ||
36
+ typeof value === "boolean" ||
37
+ typeof value === "number")
38
+ return value;
39
+ if (typeof value !== "object")
40
+ throw new TypeError("Unsupported telemetry value");
41
+ if (utilTypes.isProxy(value))
42
+ throw new TypeError("Telemetry proxies are unsupported");
43
+ if (this.ancestors.has(value))
44
+ throw new TypeError("Cyclic telemetry value");
45
+ if (this.copied.has(value))
46
+ return this.copied.get(value);
47
+ if (utilTypes.isUint8Array(value)) {
48
+ // Own accessors/subclasses cannot disguise the retained byte count.
49
+ // A length-tracking SharedArrayBuffer view can grow on another thread
50
+ // after charging: keep the destination fixed and reject growth during
51
+ // the intrinsic copy instead of retaining an uncharged larger array.
52
+ const length = typedArrayByteLength.call(value);
53
+ this.charge(length);
54
+ const copy = new Uint8Array(length);
55
+ typedArraySet.call(copy, value);
56
+ this.copied.set(value, copy);
57
+ return copy;
58
+ }
59
+ const array = Array.isArray(value);
60
+ if (!array && ![Object.prototype, null].includes(Object.getPrototypeOf(value)))
61
+ throw new TypeError("Telemetry must contain data objects");
62
+ const copy = array ? [] : Object.create(null);
63
+ this.copied.set(value, copy);
64
+ this.ancestors.add(value);
65
+ if (array) {
66
+ if (value.length > 16384)
67
+ throw new RangeError("Telemetry complexity limit exceeded");
68
+ for (let index = 0; index < value.length; index++) {
69
+ const descriptor = Object.getOwnPropertyDescriptor(value, index);
70
+ if (descriptor && !("value" in descriptor))
71
+ throw new TypeError("Telemetry accessors are unsupported");
72
+ copy.push(this.copy(descriptor?.value, depth + 1));
73
+ }
74
+ }
75
+ else {
76
+ for (const key in value) {
77
+ if (!Object.hasOwn(value, key))
78
+ continue;
79
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
80
+ if (!descriptor || !("value" in descriptor))
81
+ throw new TypeError("Telemetry accessors are unsupported");
82
+ this.charge(key.length * 2 + 16);
83
+ copy[key] = this.copy(descriptor.value, depth + 1);
84
+ }
85
+ }
86
+ this.ancestors.delete(value);
87
+ return copy;
88
+ }
89
+ context(source) {
90
+ if (!source)
91
+ return undefined;
92
+ const { traceState, ...context } = this.copy({
93
+ traceId: source.traceId,
94
+ spanId: source.spanId,
95
+ traceFlags: source.traceFlags,
96
+ isRemote: source.isRemote,
97
+ traceState: source.traceState?.serialize(),
98
+ });
99
+ return {
100
+ ...context,
101
+ ...(traceState !== undefined ? { traceState: createTraceState(traceState) } : {}),
102
+ };
103
+ }
104
+ resource(source) {
105
+ // Do not retain a detector promise or its mutable resource graph. Later
106
+ // records can include metadata once detection completes. The caller is
107
+ // informed if this record omits unresolved resource attributes.
108
+ this.unresolvedResource ||= source.asyncAttributesPending === true;
109
+ const attributes = Object.create(null);
110
+ const raw = source.getRawAttributes();
111
+ if (raw.length > 16384)
112
+ throw new RangeError("Resource complexity limit exceeded");
113
+ for (const [key, value] of raw) {
114
+ if (value && typeof value.then === "function") {
115
+ this.unresolvedResource = true;
116
+ continue;
117
+ }
118
+ if (value == null || Object.hasOwn(attributes, key))
119
+ continue;
120
+ this.charge(key.length * 2 + 16);
121
+ attributes[key] = this.copy(value);
122
+ }
123
+ return resourceFromAttributes(attributes, { schemaUrl: this.copy(source.schemaUrl) });
124
+ }
125
+ }
126
+ function contextReader(context) {
127
+ return () => context;
128
+ }
129
+ export function snapshotSpan(source, limit) {
130
+ const snapshot = new Snapshot(limit);
131
+ const context = snapshot.context(source.spanContext());
132
+ if (source.links.length > 16384)
133
+ throw new RangeError("Link complexity limit exceeded");
134
+ const links = source.links.map((link) => ({
135
+ context: snapshot.context(link.context),
136
+ attributes: snapshot.copy(link.attributes),
137
+ droppedAttributesCount: snapshot.copy(link.droppedAttributesCount),
138
+ }));
139
+ const record = {
140
+ ...snapshot.copy({
141
+ name: source.name,
142
+ kind: source.kind,
143
+ startTime: source.startTime,
144
+ endTime: source.endTime,
145
+ duration: source.duration,
146
+ ended: source.ended,
147
+ status: source.status,
148
+ attributes: source.attributes,
149
+ events: source.events,
150
+ instrumentationScope: source.instrumentationScope,
151
+ droppedAttributesCount: source.droppedAttributesCount,
152
+ droppedEventsCount: source.droppedEventsCount,
153
+ droppedLinksCount: source.droppedLinksCount,
154
+ }),
155
+ spanContext: contextReader(context),
156
+ parentSpanContext: snapshot.context(source.parentSpanContext),
157
+ links,
158
+ resource: snapshot.resource(source.resource),
159
+ };
160
+ return { record, bytes: snapshot.bytes, unresolvedResource: snapshot.unresolvedResource };
161
+ }
162
+ export function snapshotLog(source, limit) {
163
+ const snapshot = new Snapshot(limit);
164
+ // Only our batching processor sees this copy. Its writer methods deliberately
165
+ // cannot mutate the admitted snapshot or invalidate its charged byte count.
166
+ const record = {
167
+ ...snapshot.copy({
168
+ hrTime: source.hrTime,
169
+ hrTimeObserved: source.hrTimeObserved,
170
+ severityText: source.severityText,
171
+ severityNumber: source.severityNumber,
172
+ eventName: source.eventName,
173
+ body: source.body,
174
+ attributes: source.attributes,
175
+ instrumentationScope: source.instrumentationScope,
176
+ droppedAttributesCount: source.droppedAttributesCount,
177
+ }),
178
+ spanContext: snapshot.context(source.spanContext),
179
+ resource: snapshot.resource(source.resource),
180
+ setAttribute() {
181
+ return this;
182
+ },
183
+ setAttributes() {
184
+ return this;
185
+ },
186
+ setBody() {
187
+ return this;
188
+ },
189
+ setEventName() {
190
+ return this;
191
+ },
192
+ setSeverityNumber() {
193
+ return this;
194
+ },
195
+ setSeverityText() {
196
+ return this;
197
+ },
198
+ };
199
+ return { record, bytes: snapshot.bytes, unresolvedResource: snapshot.unresolvedResource };
200
+ }
@@ -1,17 +1,35 @@
1
1
  import { type ReadableSpan, type SpanProcessor } from "@opentelemetry/sdk-trace";
2
2
  import { type LogRecordProcessor, type ReadableLogRecord } from "@opentelemetry/sdk-logs";
3
3
  import { validateOptions } from "./config.js";
4
- import type { ExportIssue, ExportReport, HueOptions, Signal } from "./types.js";
5
- type RecordValue = ReadableSpan | ReadableLogRecord;
4
+ import type { ExportIssue, ExportReport, HueOptions } from "./types.js";
5
+ /** A finished span or emitted log record as the OpenTelemetry SDK hands it to a processor. */
6
+ export type RecordValue = ReadableSpan | ReadableLogRecord;
7
+ /**
8
+ * Rejection of {@link HueClient.flush} and {@link HueClient.shutdown}: telemetry was not fully
9
+ * accepted. Carries sanitized counts only, never server response text or content.
10
+ */
6
11
  export declare class HueExportError extends Error {
12
+ /** Non-warning issues observed since the failing drain began. */
7
13
  readonly issues: ExportIssue[];
14
+ /** Cumulative counters and current gauges at the time of the failure. */
8
15
  readonly report: ExportReport;
9
- constructor(issues: ExportIssue[], report: ExportReport);
16
+ constructor(
17
+ /** Non-warning issues observed since the failing drain began. */
18
+ issues: ExportIssue[],
19
+ /** Cumulative counters and current gauges at the time of the failure. */
20
+ report: ExportReport);
10
21
  }
11
- /** Owned transport components; attach processors during provider construction. */
22
+ /**
23
+ * Hue's export pipeline: OTLP/HTTP exporters behind bounded batch processors, with cumulative
24
+ * counters and a sanitized issue history. A client owns one; in attach mode the application attaches
25
+ * `spanProcessor` and `logRecordProcessor` to its own providers while constructing them.
26
+ */
12
27
  export declare class HueTransport {
28
+ /** Validated options with defaults applied; `baseUrl` is the origin. Not enumerable, so it does not leak the key when logged. */
13
29
  readonly options: ReturnType<typeof validateOptions>;
30
+ /** Span processor to attach to a tracer provider; a no-op when disabled. */
14
31
  readonly spanProcessor: SpanProcessor;
32
+ /** Log record processor to attach to a logger provider; a no-op when disabled. */
15
33
  readonly logRecordProcessor: LogRecordProcessor;
16
34
  private sequence;
17
35
  private observedSequence;
@@ -22,6 +40,11 @@ export declare class HueTransport {
22
40
  private failed;
23
41
  private spans;
24
42
  private logs;
43
+ private pendingBytes;
44
+ private dropped;
45
+ private instrumentationFailures;
46
+ private diagnosticPending;
47
+ private lastDiagnosticAt;
25
48
  private traceExporter;
26
49
  private logExporter;
27
50
  private closed;
@@ -29,16 +52,31 @@ export declare class HueTransport {
29
52
  private flushPromise?;
30
53
  constructor(options: HueOptions);
31
54
  private enqueue;
32
- finish(signal: Signal, records: RecordValue[]): void;
33
- acceptedRecords(signal: Signal, count: number): void;
34
- issue(signal: Signal, kind: ExportIssue["kind"], count: number, message: string, status?: number): void;
55
+ /** Cumulative counters and current queue gauges. */
35
56
  getReport(): ExportReport;
57
+ /** Copies of the latest 128 sanitized issues, oldest first. */
36
58
  getIssues(): ExportIssue[];
37
59
  /** Monotonic failure marker, retained even when the bounded issue history rolls over. */
38
60
  getFailureSequence(): number;
61
+ /**
62
+ * Waits for the processors' and exporters' in-flight work; drain the providers first.
63
+ *
64
+ * @throws HueExportError when a new non-warning issue was recorded since the previous observation.
65
+ */
39
66
  flush(): Promise<ExportReport>;
40
67
  private flushOnce;
68
+ /**
69
+ * Flushes and stops the processors; records emitted afterwards are dropped and counted. In attach
70
+ * mode call it after shutting down the application's providers.
71
+ *
72
+ * @throws HueExportError when the final flush observed new failures.
73
+ */
41
74
  shutdown(): Promise<ExportReport>;
42
75
  }
76
+ /**
77
+ * Creates the export pipeline for attach mode; pass it with the application's providers to
78
+ * {@link createHue}. Validates options like an owned client.
79
+ *
80
+ * @throws TypeError for invalid options; see {@link createHue}.
81
+ */
43
82
  export declare function createHueTransport(options: HueOptions): HueTransport;
44
- export {};