@sixb/client 0.0.1
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.
- package/LICENSE +21 -0
- package/README.md +193 -0
- package/dist/actions.d.ts +61 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/agent-streams.d.ts +72 -0
- package/dist/agent-streams.d.ts.map +1 -0
- package/dist/api.d.ts +54 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/browser.d.ts +41 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +174 -0
- package/dist/browser.js.map +10 -0
- package/dist/chunk-05yz32g0.js +240 -0
- package/dist/chunk-05yz32g0.js.map +10 -0
- package/dist/chunk-2d7239yx.js +1181 -0
- package/dist/chunk-2d7239yx.js.map +17 -0
- package/dist/chunk-3g6ad4jc.js +147 -0
- package/dist/chunk-3g6ad4jc.js.map +10 -0
- package/dist/chunk-aw8rcsq3.js +145 -0
- package/dist/chunk-aw8rcsq3.js.map +10 -0
- package/dist/chunk-ctsdm59b.js +1477 -0
- package/dist/chunk-ctsdm59b.js.map +19 -0
- package/dist/chunk-fxrh83wy.js +180 -0
- package/dist/chunk-fxrh83wy.js.map +12 -0
- package/dist/chunk-pey7nh0d.js +123 -0
- package/dist/chunk-pey7nh0d.js.map +10 -0
- package/dist/chunk-trpg18dp.js +3 -0
- package/dist/chunk-trpg18dp.js.map +9 -0
- package/dist/events-builder.d.ts +218 -0
- package/dist/events-builder.d.ts.map +1 -0
- package/dist/events-hooks.d.ts +50 -0
- package/dist/events-hooks.d.ts.map +1 -0
- package/dist/events-model.d.ts +12 -0
- package/dist/events-model.d.ts.map +1 -0
- package/dist/events-provider.d.ts +56 -0
- package/dist/events-provider.d.ts.map +1 -0
- package/dist/events-transport.d.ts +62 -0
- package/dist/events-transport.d.ts.map +1 -0
- package/dist/events.d.ts +5 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/file.d.ts +27 -0
- package/dist/file.d.ts.map +1 -0
- package/dist/generated/@tanstack/react-query.gen.d.ts +17064 -0
- package/dist/generated/@tanstack/react-query.gen.d.ts.map +1 -0
- package/dist/generated/client/client.gen.d.ts +3 -0
- package/dist/generated/client/client.gen.d.ts.map +1 -0
- package/dist/generated/client/index.d.ts +9 -0
- package/dist/generated/client/index.d.ts.map +1 -0
- package/dist/generated/client/types.gen.d.ts +118 -0
- package/dist/generated/client/types.gen.d.ts.map +1 -0
- package/dist/generated/client/utils.gen.d.ts +34 -0
- package/dist/generated/client/utils.gen.d.ts.map +1 -0
- package/dist/generated/client.gen.d.ts +13 -0
- package/dist/generated/client.gen.d.ts.map +1 -0
- package/dist/generated/core/auth.gen.d.ts +19 -0
- package/dist/generated/core/auth.gen.d.ts.map +1 -0
- package/dist/generated/core/bodySerializer.gen.d.ts +26 -0
- package/dist/generated/core/bodySerializer.gen.d.ts.map +1 -0
- package/dist/generated/core/params.gen.d.ts +44 -0
- package/dist/generated/core/params.gen.d.ts.map +1 -0
- package/dist/generated/core/pathSerializer.gen.d.ts +34 -0
- package/dist/generated/core/pathSerializer.gen.d.ts.map +1 -0
- package/dist/generated/core/queryKeySerializer.gen.d.ts +19 -0
- package/dist/generated/core/queryKeySerializer.gen.d.ts.map +1 -0
- package/dist/generated/core/serverSentEvents.gen.d.ts +72 -0
- package/dist/generated/core/serverSentEvents.gen.d.ts.map +1 -0
- package/dist/generated/core/types.gen.d.ts +79 -0
- package/dist/generated/core/types.gen.d.ts.map +1 -0
- package/dist/generated/core/utils.gen.d.ts +20 -0
- package/dist/generated/core/utils.gen.d.ts.map +1 -0
- package/dist/generated/index.d.ts +3 -0
- package/dist/generated/index.d.ts.map +1 -0
- package/dist/generated/sdk.gen.d.ts +464 -0
- package/dist/generated/sdk.gen.d.ts.map +1 -0
- package/dist/generated/types.gen.d.ts +7672 -0
- package/dist/generated/types.gen.d.ts.map +1 -0
- package/dist/hooks.d.ts +123 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +3179 -0
- package/dist/hooks.js.map +16 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +329 -0
- package/dist/index.js.map +9 -0
- package/dist/logs-builder.d.ts +59 -0
- package/dist/logs-builder.d.ts.map +1 -0
- package/dist/logs-hooks.d.ts +30 -0
- package/dist/logs-hooks.d.ts.map +1 -0
- package/dist/logs-transport.d.ts +38 -0
- package/dist/logs-transport.d.ts.map +1 -0
- package/dist/logs.d.ts +7 -0
- package/dist/logs.d.ts.map +1 -0
- package/dist/logs.js +22 -0
- package/dist/logs.js.map +9 -0
- package/dist/models.d.ts +126 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +252 -0
- package/dist/models.js.map +9 -0
- package/dist/query-hooks.d.ts +365 -0
- package/dist/query-hooks.d.ts.map +1 -0
- package/dist/query.d.ts +27 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +15 -0
- package/dist/query.js.map +9 -0
- package/dist/sha256.d.ts +15 -0
- package/dist/sha256.d.ts.map +1 -0
- package/dist/telemetry-events.d.ts +25 -0
- package/dist/telemetry-events.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/useAgentRunStream.d.ts +21 -0
- package/dist/useAgentRunStream.d.ts.map +1 -0
- package/dist/ws-socket.d.ts +45 -0
- package/dist/ws-socket.d.ts.map +1 -0
- package/package.json +105 -0
- package/src/actions.ts +310 -0
- package/src/agent-streams.ts +275 -0
- package/src/api.ts +229 -0
- package/src/browser.ts +267 -0
- package/src/events-builder.ts +735 -0
- package/src/events-hooks.ts +233 -0
- package/src/events-model.ts +40 -0
- package/src/events-provider.ts +161 -0
- package/src/events-transport.ts +193 -0
- package/src/events.ts +6 -0
- package/src/file.ts +367 -0
- package/src/generated/@tanstack/react-query.gen.ts +3876 -0
- package/src/generated/client/client.gen.ts +288 -0
- package/src/generated/client/index.ts +25 -0
- package/src/generated/client/types.gen.ts +209 -0
- package/src/generated/client/utils.gen.ts +316 -0
- package/src/generated/client.gen.ts +18 -0
- package/src/generated/core/auth.gen.ts +41 -0
- package/src/generated/core/bodySerializer.gen.ts +84 -0
- package/src/generated/core/params.gen.ts +169 -0
- package/src/generated/core/pathSerializer.gen.ts +171 -0
- package/src/generated/core/queryKeySerializer.gen.ts +117 -0
- package/src/generated/core/serverSentEvents.gen.ts +243 -0
- package/src/generated/core/types.gen.ts +104 -0
- package/src/generated/core/utils.gen.ts +140 -0
- package/src/generated/index.ts +672 -0
- package/src/generated/sdk.gen.ts +1984 -0
- package/src/generated/types.gen.ts +9049 -0
- package/src/hooks.ts +404 -0
- package/src/index.ts +18 -0
- package/src/logs-builder.ts +171 -0
- package/src/logs-hooks.ts +112 -0
- package/src/logs-transport.ts +112 -0
- package/src/logs.ts +15 -0
- package/src/models.ts +420 -0
- package/src/query-hooks.ts +1021 -0
- package/src/query.ts +175 -0
- package/src/sha256.ts +159 -0
- package/src/telemetry-events.ts +71 -0
- package/src/useAgentRunStream.ts +62 -0
- package/src/ws-socket.ts +202 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isStoredLogLine,
|
|
3
|
+
type LogLevel,
|
|
4
|
+
type LogRunRef,
|
|
5
|
+
type SixbRunKind,
|
|
6
|
+
type StoredLogLine,
|
|
7
|
+
} from "@sixb/core/logging"
|
|
8
|
+
import {
|
|
9
|
+
createReconnectingSocket,
|
|
10
|
+
createSixbWebSocketUrl,
|
|
11
|
+
type ReconnectingSocketState,
|
|
12
|
+
} from "./ws-socket"
|
|
13
|
+
|
|
14
|
+
export type LogSocketState = ReconnectingSocketState
|
|
15
|
+
|
|
16
|
+
export interface LogSocketOptions {
|
|
17
|
+
readonly kinds?: readonly SixbRunKind[]
|
|
18
|
+
readonly levels?: readonly LogLevel[]
|
|
19
|
+
readonly run?: LogRunRef
|
|
20
|
+
readonly afterCursor?: string
|
|
21
|
+
readonly reconnect?: boolean
|
|
22
|
+
readonly reconnectDelayMs?: number
|
|
23
|
+
/** API origin override. Authentication uses the browser session cookie. */
|
|
24
|
+
readonly baseUrl?: string
|
|
25
|
+
readonly onLog: (line: StoredLogLine) => void
|
|
26
|
+
readonly onError?: (error: string) => void
|
|
27
|
+
readonly onReset?: (cursor?: string) => void
|
|
28
|
+
readonly onStateChange?: (state: LogSocketState) => void
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface LogSocket {
|
|
32
|
+
close(): void
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type LogStreamServerMessage =
|
|
36
|
+
| { readonly type: "connected" | "subscribed" | "unsubscribed" }
|
|
37
|
+
| { readonly type: "error"; readonly message: string }
|
|
38
|
+
| { readonly type: "logs"; readonly logs: readonly StoredLogLine[] }
|
|
39
|
+
| { readonly type: "reset"; readonly reason: "cursor_expired"; readonly cursor?: string }
|
|
40
|
+
|
|
41
|
+
export function createLogSocket(options: LogSocketOptions): LogSocket {
|
|
42
|
+
let latestCursor = options.afterCursor
|
|
43
|
+
|
|
44
|
+
return createReconnectingSocket({
|
|
45
|
+
url: createSixbLogsWebSocketUrl(options.baseUrl),
|
|
46
|
+
reconnect: options.reconnect,
|
|
47
|
+
reconnectDelayMs: options.reconnectDelayMs,
|
|
48
|
+
connectionErrorMessage: "Log websocket connection failed.",
|
|
49
|
+
onError: options.onError,
|
|
50
|
+
onStateChange: options.onStateChange,
|
|
51
|
+
subscribeMessage: () => ({
|
|
52
|
+
type: "subscribe",
|
|
53
|
+
...(options.kinds?.length ? { kinds: options.kinds } : {}),
|
|
54
|
+
...(options.levels?.length ? { levels: options.levels } : {}),
|
|
55
|
+
...(options.run ? { run: options.run } : {}),
|
|
56
|
+
...(latestCursor ? { afterCursor: latestCursor } : {}),
|
|
57
|
+
}),
|
|
58
|
+
onMessage: (data, sink) => {
|
|
59
|
+
const message = parseLogStreamMessage(data)
|
|
60
|
+
if (!message) return
|
|
61
|
+
|
|
62
|
+
if (message.type === "logs") {
|
|
63
|
+
for (const line of message.logs) {
|
|
64
|
+
latestCursor = line.cursor
|
|
65
|
+
options.onLog(line)
|
|
66
|
+
}
|
|
67
|
+
} else if (message.type === "reset") {
|
|
68
|
+
latestCursor = message.cursor
|
|
69
|
+
options.onReset?.(message.cursor)
|
|
70
|
+
} else if (message.type === "error") {
|
|
71
|
+
sink.reportError(message.message)
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function createSixbLogsWebSocketUrl(baseUrl?: string): string {
|
|
78
|
+
return createSixbWebSocketUrl("/ws/logs", baseUrl)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function parseLogStreamMessage(value: unknown): LogStreamServerMessage | null {
|
|
82
|
+
if (typeof value !== "string") return null
|
|
83
|
+
let parsed: unknown
|
|
84
|
+
try {
|
|
85
|
+
parsed = JSON.parse(value)
|
|
86
|
+
} catch {
|
|
87
|
+
return null
|
|
88
|
+
}
|
|
89
|
+
if (!isRecord(parsed) || typeof parsed.type !== "string") return null
|
|
90
|
+
|
|
91
|
+
if (parsed.type === "logs" && Array.isArray(parsed.logs) && parsed.logs.every(isStoredLogLine)) {
|
|
92
|
+
return { type: "logs", logs: parsed.logs }
|
|
93
|
+
}
|
|
94
|
+
if (parsed.type === "reset" && parsed.reason === "cursor_expired") {
|
|
95
|
+
return {
|
|
96
|
+
type: "reset",
|
|
97
|
+
reason: "cursor_expired",
|
|
98
|
+
cursor: typeof parsed.cursor === "string" ? parsed.cursor : undefined,
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (parsed.type === "error") {
|
|
102
|
+
return { type: "error", message: String(parsed.message ?? "Log stream error.") }
|
|
103
|
+
}
|
|
104
|
+
if (["connected", "subscribed", "unsubscribed"].includes(parsed.type)) {
|
|
105
|
+
return { type: parsed.type as "connected" | "subscribed" | "unsubscribed" }
|
|
106
|
+
}
|
|
107
|
+
return null
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
111
|
+
return typeof value === "object" && value !== null
|
|
112
|
+
}
|
package/src/logs.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// The client-side log model, fluent read/subscribe builder, and React-free transport.
|
|
2
|
+
|
|
3
|
+
export type {
|
|
4
|
+
LogFields,
|
|
5
|
+
LogLevel,
|
|
6
|
+
LogRecord,
|
|
7
|
+
LogRunRef,
|
|
8
|
+
SixbRunKind,
|
|
9
|
+
StoredLogLine,
|
|
10
|
+
} from "@sixb/core/logging"
|
|
11
|
+
export { isStoredLogLine, isStoredLogLine as isSixbLogLine } from "@sixb/core/logging"
|
|
12
|
+
/** One stored log line delivered by the `logs` builder (`.read()` or live `.subscribe()`). */
|
|
13
|
+
export type SixbLogLine = import("@sixb/core/logging").StoredLogLine
|
|
14
|
+
export * from "./logs-builder"
|
|
15
|
+
export * from "./logs-transport"
|
package/src/models.ts
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
import type { DomainEvent } from "@sixb/core"
|
|
2
|
+
import { requestAction } from "./generated/sdk.gen"
|
|
3
|
+
import type {
|
|
4
|
+
GetProjectInfoResponse,
|
|
5
|
+
GetTelemetryHistoryResponse,
|
|
6
|
+
ListObjectsResponse,
|
|
7
|
+
ListObjectTypesResponse,
|
|
8
|
+
} from "./generated/types.gen"
|
|
9
|
+
|
|
10
|
+
export * from "./generated"
|
|
11
|
+
export { client } from "./generated/client.gen"
|
|
12
|
+
|
|
13
|
+
type ObjectTypeDefinition = ListObjectTypesResponse extends Array<infer T> ? T : never
|
|
14
|
+
type ObjectListItem = ListObjectsResponse extends {
|
|
15
|
+
objects: Array<infer T>
|
|
16
|
+
}
|
|
17
|
+
? T
|
|
18
|
+
: never
|
|
19
|
+
|
|
20
|
+
export interface RelationshipEdge {
|
|
21
|
+
source: string
|
|
22
|
+
target: string
|
|
23
|
+
type: string
|
|
24
|
+
properties?: Record<string, unknown>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ActionParam {
|
|
28
|
+
type: "string" | "number" | "boolean" | "fileRef"
|
|
29
|
+
required?: boolean
|
|
30
|
+
nullable?: boolean
|
|
31
|
+
description?: string
|
|
32
|
+
enum?: Array<string | number | boolean>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ObjectAction {
|
|
36
|
+
id: string
|
|
37
|
+
description?: string
|
|
38
|
+
params?: Record<string, ActionParam>
|
|
39
|
+
timeout?: number
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface TelemetryProperty {
|
|
43
|
+
source: string
|
|
44
|
+
class?: string
|
|
45
|
+
dataType?: "number" | "string" | "boolean"
|
|
46
|
+
unit?: string
|
|
47
|
+
writable?: boolean
|
|
48
|
+
currentValue?: number | string | boolean
|
|
49
|
+
timestamp?: string
|
|
50
|
+
quality?: "good" | "uncertain" | "bad"
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ProjectInfo {
|
|
54
|
+
name: string
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type ObjectSummary = {
|
|
58
|
+
id: string
|
|
59
|
+
primaryId: string
|
|
60
|
+
objectTypeId: string
|
|
61
|
+
name: string
|
|
62
|
+
class: string
|
|
63
|
+
properties: Record<string, unknown>
|
|
64
|
+
telemetry: Record<string, TelemetryProperty>
|
|
65
|
+
actions: Record<string, ObjectAction>
|
|
66
|
+
location?: Record<string, string> | string
|
|
67
|
+
telemetryCount: number
|
|
68
|
+
createdAt: string
|
|
69
|
+
updatedAt: string
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type ObjectDetail = ObjectSummary
|
|
73
|
+
|
|
74
|
+
export type TelemetryHistory = {
|
|
75
|
+
objectId: string
|
|
76
|
+
propertyId: string
|
|
77
|
+
range: {
|
|
78
|
+
start: string
|
|
79
|
+
end: string
|
|
80
|
+
}
|
|
81
|
+
data: Array<{
|
|
82
|
+
value: number | string | boolean
|
|
83
|
+
timestamp: string
|
|
84
|
+
quality?: "good" | "uncertain" | "bad"
|
|
85
|
+
}>
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface EventStreamServerMessage {
|
|
89
|
+
type: "connected" | "subscribed" | "unsubscribed" | "error" | "event"
|
|
90
|
+
channel?: string
|
|
91
|
+
message?: string
|
|
92
|
+
topic?: string | null
|
|
93
|
+
afterCursor?: string | null
|
|
94
|
+
types?: string[] | null
|
|
95
|
+
event?: DomainEvent & { readonly cursor: string }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function encodeObjectId(objectTypeId: string, primaryId: string): string {
|
|
99
|
+
return `${encodeURIComponent(objectTypeId)}~${encodeURIComponent(primaryId)}`
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function decodeObjectId(objectId: string): {
|
|
103
|
+
objectTypeId: string
|
|
104
|
+
primaryId: string
|
|
105
|
+
} | null {
|
|
106
|
+
const separatorIndex = objectId.indexOf("~")
|
|
107
|
+
if (separatorIndex <= 0 || separatorIndex >= objectId.length - 1) {
|
|
108
|
+
return null
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const decodeSegment = (value: string) => {
|
|
112
|
+
try {
|
|
113
|
+
return decodeURIComponent(value)
|
|
114
|
+
} catch {
|
|
115
|
+
return value
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
objectTypeId: decodeSegment(objectId.slice(0, separatorIndex)),
|
|
121
|
+
primaryId: decodeSegment(objectId.slice(separatorIndex + 1)),
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function normalizePrimitive(value: unknown): number | string | boolean | undefined {
|
|
126
|
+
if (typeof value === "number" || typeof value === "string" || typeof value === "boolean") {
|
|
127
|
+
return value
|
|
128
|
+
}
|
|
129
|
+
return undefined
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function parseLocation(
|
|
133
|
+
properties: Record<string, unknown>
|
|
134
|
+
): Record<string, string> | string | undefined {
|
|
135
|
+
const location = properties.location
|
|
136
|
+
if (typeof location === "string") return location
|
|
137
|
+
|
|
138
|
+
if (location && typeof location === "object") {
|
|
139
|
+
const entries = Object.entries(location as Record<string, unknown>).filter(
|
|
140
|
+
([, value]) => typeof value === "string"
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
if (entries.length > 0) {
|
|
144
|
+
return Object.fromEntries(entries) as Record<string, string>
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (typeof properties.zone === "string") {
|
|
149
|
+
return properties.zone
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return undefined
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function inferPrimitiveType(schema: unknown): "string" | "number" | "boolean" | "fileRef" {
|
|
156
|
+
if (typeof schema === "string") {
|
|
157
|
+
if (schema === "integer" || schema === "double") return "number"
|
|
158
|
+
if (schema === "decimal") return "string"
|
|
159
|
+
if (schema === "boolean") return "boolean"
|
|
160
|
+
if (schema === "fileRef") return "fileRef"
|
|
161
|
+
return "string"
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (!schema || typeof schema !== "object") return "string"
|
|
165
|
+
const type = (schema as { type?: unknown }).type
|
|
166
|
+
if (type === "number" || type === "integer") return "number"
|
|
167
|
+
if (type === "boolean") return "boolean"
|
|
168
|
+
if (type === "fileRef") return "fileRef"
|
|
169
|
+
return "string"
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function isPrimitiveEnumValue(value: unknown): value is string | number | boolean {
|
|
173
|
+
return typeof value === "string" || typeof value === "number" || typeof value === "boolean"
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function getEnumValues(schema: unknown): Array<string | number | boolean> | undefined {
|
|
177
|
+
if (!schema || typeof schema !== "object") return undefined
|
|
178
|
+
|
|
179
|
+
const enumValues = (schema as { enum?: unknown }).enum
|
|
180
|
+
if (!Array.isArray(enumValues)) return undefined
|
|
181
|
+
|
|
182
|
+
const primitiveEnumValues = enumValues.filter(isPrimitiveEnumValue)
|
|
183
|
+
return primitiveEnumValues.length === enumValues.length ? primitiveEnumValues : undefined
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function mapActionParams(
|
|
187
|
+
action: ObjectTypeDefinition["actions"][number]
|
|
188
|
+
): Record<string, ActionParam> {
|
|
189
|
+
const result: Record<string, ActionParam> = {}
|
|
190
|
+
|
|
191
|
+
for (const param of action.params ?? []) {
|
|
192
|
+
const enumValues = getEnumValues(param.schema)
|
|
193
|
+
|
|
194
|
+
result[param.id] = {
|
|
195
|
+
type: inferPrimitiveType(param.schema),
|
|
196
|
+
required: param.required,
|
|
197
|
+
nullable: param.nullable,
|
|
198
|
+
description: param.description,
|
|
199
|
+
enum: enumValues,
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return result
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function mapActions(objectType: ObjectTypeDefinition | undefined): Record<string, ObjectAction> {
|
|
207
|
+
if (!objectType) return {}
|
|
208
|
+
|
|
209
|
+
const result: Record<string, ObjectAction> = {}
|
|
210
|
+
for (const action of objectType.actions ?? []) {
|
|
211
|
+
result[action.id] = {
|
|
212
|
+
id: action.id,
|
|
213
|
+
description: action.description,
|
|
214
|
+
params: mapActionParams(action),
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return result
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function mapTelemetryProperties(
|
|
222
|
+
objectType: ObjectTypeDefinition | undefined,
|
|
223
|
+
properties: Record<string, unknown>
|
|
224
|
+
): Record<string, TelemetryProperty> {
|
|
225
|
+
if (!objectType) return {}
|
|
226
|
+
|
|
227
|
+
const streams = objectType.properties.filter((property) => property.mode === "telemetry")
|
|
228
|
+
const result: Record<string, TelemetryProperty> = {}
|
|
229
|
+
|
|
230
|
+
for (const stream of streams) {
|
|
231
|
+
const schemaType =
|
|
232
|
+
stream.schema && typeof stream.schema === "object"
|
|
233
|
+
? ((stream.schema as { type?: unknown }).type ?? undefined)
|
|
234
|
+
: undefined
|
|
235
|
+
const dataType =
|
|
236
|
+
schemaType === "number" || schemaType === "integer"
|
|
237
|
+
? "number"
|
|
238
|
+
: schemaType === "boolean"
|
|
239
|
+
? "boolean"
|
|
240
|
+
: "string"
|
|
241
|
+
|
|
242
|
+
result[stream.id] = {
|
|
243
|
+
source: `${objectType.id}.${stream.id}`,
|
|
244
|
+
class: stream.semanticType ?? stream.name,
|
|
245
|
+
dataType,
|
|
246
|
+
currentValue: normalizePrimitive(properties[stream.id]),
|
|
247
|
+
writable: false,
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return result
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export function toProjectInfo(project: GetProjectInfoResponse): ProjectInfo {
|
|
255
|
+
return {
|
|
256
|
+
name: project.id,
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export function toObjectSummary(
|
|
261
|
+
object: ObjectListItem,
|
|
262
|
+
objectType: ObjectTypeDefinition | undefined
|
|
263
|
+
): ObjectSummary {
|
|
264
|
+
const properties = (object.properties ?? {}) as Record<string, unknown>
|
|
265
|
+
const name =
|
|
266
|
+
typeof properties.name === "string" && properties.name.length > 0
|
|
267
|
+
? properties.name
|
|
268
|
+
: object.primaryId
|
|
269
|
+
const telemetry = mapTelemetryProperties(objectType, properties)
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
id: encodeObjectId(object.objectTypeId, object.primaryId),
|
|
273
|
+
primaryId: object.primaryId,
|
|
274
|
+
objectTypeId: object.objectTypeId,
|
|
275
|
+
name,
|
|
276
|
+
class: objectType?.id ?? object.objectTypeId,
|
|
277
|
+
properties,
|
|
278
|
+
telemetry,
|
|
279
|
+
actions: mapActions(objectType),
|
|
280
|
+
location: parseLocation(properties),
|
|
281
|
+
telemetryCount: Object.keys(telemetry).length,
|
|
282
|
+
createdAt: object.createdAt,
|
|
283
|
+
updatedAt: object.updatedAt,
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function toObjectDetail(
|
|
288
|
+
object: ObjectListItem,
|
|
289
|
+
objectType: ObjectTypeDefinition | undefined
|
|
290
|
+
): ObjectDetail {
|
|
291
|
+
return toObjectSummary(object, objectType)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export function toTelemetryHistoryWithRange(input: {
|
|
295
|
+
objectId: string
|
|
296
|
+
propertyId: string
|
|
297
|
+
history: GetTelemetryHistoryResponse
|
|
298
|
+
rangeStart: string
|
|
299
|
+
rangeEnd: string
|
|
300
|
+
}): TelemetryHistory {
|
|
301
|
+
return {
|
|
302
|
+
objectId: input.objectId,
|
|
303
|
+
propertyId: input.propertyId,
|
|
304
|
+
range: {
|
|
305
|
+
start: input.rangeStart,
|
|
306
|
+
end: input.rangeEnd,
|
|
307
|
+
},
|
|
308
|
+
data: input.history
|
|
309
|
+
.map((sample) => ({
|
|
310
|
+
value: normalizePrimitive(sample.value),
|
|
311
|
+
timestamp: sample.at,
|
|
312
|
+
}))
|
|
313
|
+
.filter(
|
|
314
|
+
(sample): sample is { value: number | string | boolean; timestamp: string } =>
|
|
315
|
+
sample.value !== undefined
|
|
316
|
+
),
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export async function executeAction(options: {
|
|
321
|
+
path: {
|
|
322
|
+
objectId: string
|
|
323
|
+
actionId: string
|
|
324
|
+
}
|
|
325
|
+
body: {
|
|
326
|
+
params?: Record<string, unknown>
|
|
327
|
+
runId?: string
|
|
328
|
+
}
|
|
329
|
+
}): Promise<{
|
|
330
|
+
data: {
|
|
331
|
+
success: boolean
|
|
332
|
+
runId?: string
|
|
333
|
+
error?: string
|
|
334
|
+
}
|
|
335
|
+
}> {
|
|
336
|
+
const parsed = decodeObjectId(options.path.objectId)
|
|
337
|
+
if (!parsed) {
|
|
338
|
+
return {
|
|
339
|
+
data: {
|
|
340
|
+
success: false,
|
|
341
|
+
error: `Invalid object id: ${options.path.objectId}`,
|
|
342
|
+
},
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
try {
|
|
347
|
+
const response = await requestAction({
|
|
348
|
+
path: {
|
|
349
|
+
actionId: options.path.actionId,
|
|
350
|
+
},
|
|
351
|
+
body: {
|
|
352
|
+
subject: {
|
|
353
|
+
kind: "object",
|
|
354
|
+
objectTypeId: parsed.objectTypeId,
|
|
355
|
+
primaryId: parsed.primaryId,
|
|
356
|
+
},
|
|
357
|
+
params: options.body.params,
|
|
358
|
+
runId: options.body.runId,
|
|
359
|
+
},
|
|
360
|
+
throwOnError: true,
|
|
361
|
+
})
|
|
362
|
+
|
|
363
|
+
return {
|
|
364
|
+
data: {
|
|
365
|
+
success: true,
|
|
366
|
+
runId: response.data.runId,
|
|
367
|
+
},
|
|
368
|
+
}
|
|
369
|
+
} catch (error) {
|
|
370
|
+
return {
|
|
371
|
+
data: {
|
|
372
|
+
success: false,
|
|
373
|
+
error: error instanceof Error ? error.message : String(error),
|
|
374
|
+
},
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export async function executeGlobalAction(options: {
|
|
380
|
+
path: {
|
|
381
|
+
actionId: string
|
|
382
|
+
}
|
|
383
|
+
body: {
|
|
384
|
+
params?: Record<string, unknown>
|
|
385
|
+
runId?: string
|
|
386
|
+
}
|
|
387
|
+
}): Promise<{
|
|
388
|
+
data: {
|
|
389
|
+
success: boolean
|
|
390
|
+
runId?: string
|
|
391
|
+
error?: string
|
|
392
|
+
}
|
|
393
|
+
}> {
|
|
394
|
+
try {
|
|
395
|
+
const response = await requestAction({
|
|
396
|
+
path: {
|
|
397
|
+
actionId: options.path.actionId,
|
|
398
|
+
},
|
|
399
|
+
body: {
|
|
400
|
+
params: options.body.params,
|
|
401
|
+
runId: options.body.runId,
|
|
402
|
+
},
|
|
403
|
+
throwOnError: true,
|
|
404
|
+
})
|
|
405
|
+
|
|
406
|
+
return {
|
|
407
|
+
data: {
|
|
408
|
+
success: true,
|
|
409
|
+
runId: response.data.runId,
|
|
410
|
+
},
|
|
411
|
+
}
|
|
412
|
+
} catch (error) {
|
|
413
|
+
return {
|
|
414
|
+
data: {
|
|
415
|
+
success: false,
|
|
416
|
+
error: error instanceof Error ? error.message : String(error),
|
|
417
|
+
},
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|