@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, 04:45 UTC

main
ts 56 lines 1.74 KB
Raw
1 import type * as Otel from "@opentelemetry/api"
2 import * as Inspectable from "effect/Inspectable"
3 import * as Rec from "effect/Record"
4
5 const bigint1e9 = BigInt(1_000_000_000)
6
7 /** @internal */
8 export const nanosToHrTime = (timestamp: bigint): Otel.HrTime => {
9 return [Number(timestamp / bigint1e9), Number(timestamp % bigint1e9)]
10 }
11
12 /** @internal */
13 export const recordToAttributes = (record: Record<string, unknown>): Otel.Attributes => {
14 const attributes: Otel.Attributes = {}
15 for (const [key, value] of Object.entries(record)) {
16 Rec.assignProperty(attributes, key, unknownToAttributeValue(value))
17 }
18 return attributes
19 }
20
21 /**
22 * The attribute values produced by `unknownToAttributeValue`.
23 *
24 * `@opentelemetry/api` 1.10 widened `AttributeValue` to `unknown`, so this
25 * keeps the concrete primitive shape that both `AttributeValue` and the logs
26 * `AnyValue` accept.
27 *
28 * @internal
29 */
30 export type PrimitiveAttributeValue =
31 | string
32 | number
33 | boolean
34 | Array<string>
35 | Array<number>
36 | Array<boolean>
37
38 /** @internal */
39 export const unknownToAttributeValue = (value: unknown): PrimitiveAttributeValue => {
40 if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
41 return value
42 } else if (typeof value === "bigint") {
43 return value.toString()
44 } else if (Array.isArray(value) && isHomogeneousPrimitiveArray(value)) {
45 return value.slice()
46 }
47 return Inspectable.toStringUnknown(value)
48 }
49
50 const isHomogeneousPrimitiveArray = (
51 array: ReadonlyArray<unknown>
52 ): array is Array<string> | Array<number> | Array<boolean> => {
53 const type = typeof array[0]
54 return (array.length === 0 || type === "string" || type === "number" || type === "boolean") &&
55 array.every((item) => typeof item === type)
56 }