@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
package/src/query.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed object queries over HTTP (`@sixb/client/query`).
|
|
3
|
+
*
|
|
4
|
+
* `objects(Type)` returns the same fluent query builder the server runtime
|
|
5
|
+
* uses, wired to the object query routes through the generated SDK. Queries
|
|
6
|
+
* are validated server-side; failures surface as `SixbQueryError`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { SixbObjectTypeMap } from "@sixb/core/ontology"
|
|
10
|
+
import type {
|
|
11
|
+
ObjectQuery,
|
|
12
|
+
ObjectQueryBuilder,
|
|
13
|
+
ObjectQueryExecutor,
|
|
14
|
+
ObjectQueryExecutorExpandedRow,
|
|
15
|
+
ObjectQueryExecutorFacetRequest,
|
|
16
|
+
ObjectQueryExecutorLinkValue,
|
|
17
|
+
ObjectQueryExecutorRow,
|
|
18
|
+
ObjectTypeWithPropertyTokens,
|
|
19
|
+
} from "@sixb/core/query"
|
|
20
|
+
import { createObjectQueryBuilder } from "@sixb/core/query"
|
|
21
|
+
import { isSixbApiError } from "./api"
|
|
22
|
+
import type { Client } from "./generated/client"
|
|
23
|
+
import { countObjects, existsObjects, facetObjects, queryObjects } from "./generated/sdk.gen"
|
|
24
|
+
import type {
|
|
25
|
+
ObjectQueryIssue,
|
|
26
|
+
ObjectQueryObject,
|
|
27
|
+
ObjectQuery as WireObjectQuery,
|
|
28
|
+
} from "./generated/types.gen"
|
|
29
|
+
|
|
30
|
+
export type { ObjectQueryIssue }
|
|
31
|
+
|
|
32
|
+
export class SixbQueryError extends Error {
|
|
33
|
+
readonly issues: readonly ObjectQueryIssue[]
|
|
34
|
+
|
|
35
|
+
constructor(message: string, issues: readonly ObjectQueryIssue[] = []) {
|
|
36
|
+
super(message)
|
|
37
|
+
this.name = "SixbQueryError"
|
|
38
|
+
this.issues = issues
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface SixbQueryClientOptions {
|
|
43
|
+
/** hey-api client override (base url, auth, fetch). Defaults to the global client. */
|
|
44
|
+
client?: Client
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Value types are typed as "none registered": custom value-type refs resolve
|
|
48
|
+
// like a runtime without them, and the empty tuple keeps property-value
|
|
49
|
+
// inference shallow enough for TypeScript's recursion limits.
|
|
50
|
+
type ClientRegisteredObjectTypes<TObjectType extends ObjectTypeWithPropertyTokens> =
|
|
51
|
+
| TObjectType
|
|
52
|
+
| Extract<SixbObjectTypeMap[keyof SixbObjectTypeMap], ObjectTypeWithPropertyTokens>
|
|
53
|
+
|
|
54
|
+
export type ClientObjectQueryBuilder<TObjectType extends ObjectTypeWithPropertyTokens> =
|
|
55
|
+
ObjectQueryBuilder<TObjectType, ClientRegisteredObjectTypes<TObjectType>, readonly []>
|
|
56
|
+
|
|
57
|
+
export function objects<TObjectType extends ObjectTypeWithPropertyTokens>(
|
|
58
|
+
objectType: TObjectType,
|
|
59
|
+
options?: SixbQueryClientOptions
|
|
60
|
+
): { query: () => ClientObjectQueryBuilder<TObjectType> } {
|
|
61
|
+
return {
|
|
62
|
+
query: () =>
|
|
63
|
+
createObjectQueryBuilder<TObjectType, ClientRegisteredObjectTypes<TObjectType>, readonly []>({
|
|
64
|
+
query: { kind: "start", objectTypeId: objectType.id },
|
|
65
|
+
executor: createHttpQueryExecutor(options?.client),
|
|
66
|
+
}),
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function createHttpQueryExecutor(client?: Client): ObjectQueryExecutor {
|
|
71
|
+
// Pin the SDK call shape regardless of client-level config: with
|
|
72
|
+
// `responseStyle: "data"` the call would return the bare body (breaking the
|
|
73
|
+
// `{ data, error }` destructuring below), and with `throwOnError: true`
|
|
74
|
+
// failures would throw raw SDK errors instead of mapping to SixbQueryError.
|
|
75
|
+
const callOptions = { client, responseStyle: "fields", throwOnError: false } as const
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
async list(query: ObjectQuery, options?: { includeTotal?: boolean }) {
|
|
79
|
+
const { data, error } = await queryObjects({
|
|
80
|
+
...callOptions,
|
|
81
|
+
body: { query: toWireQuery(query), includeTotal: options?.includeTotal },
|
|
82
|
+
})
|
|
83
|
+
if (error || !data) throw toSixbQueryError(error)
|
|
84
|
+
return {
|
|
85
|
+
objects: data.objects.map(reviveQueryRow),
|
|
86
|
+
hasMore: data.hasMore,
|
|
87
|
+
total: data.total,
|
|
88
|
+
nextPageToken: data.nextPageToken,
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
async count(query: ObjectQuery) {
|
|
93
|
+
const { data, error } = await countObjects({
|
|
94
|
+
...callOptions,
|
|
95
|
+
body: { query: toWireQuery(query) },
|
|
96
|
+
})
|
|
97
|
+
if (error || !data) throw toSixbQueryError(error)
|
|
98
|
+
return data.count
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
async exists(query: ObjectQuery) {
|
|
102
|
+
const { data, error } = await existsObjects({
|
|
103
|
+
...callOptions,
|
|
104
|
+
body: { query: toWireQuery(query) },
|
|
105
|
+
})
|
|
106
|
+
if (error || !data) throw toSixbQueryError(error)
|
|
107
|
+
return data.exists
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
async facets(query: ObjectQuery, facets: readonly ObjectQueryExecutorFacetRequest[]) {
|
|
111
|
+
const { data, error } = await facetObjects({
|
|
112
|
+
...callOptions,
|
|
113
|
+
body: { query: toWireQuery(query), facets: [...facets] },
|
|
114
|
+
})
|
|
115
|
+
if (error || !data) throw toSixbQueryError(error)
|
|
116
|
+
return data.facets.map((facet) => ({
|
|
117
|
+
propertyId: facet.propertyId,
|
|
118
|
+
buckets: [...facet.buckets],
|
|
119
|
+
}))
|
|
120
|
+
},
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Revive a query row tree: parse ISO timestamps to Date at every hop and carry
|
|
125
|
+
// the `links` attached by `expand` nodes (with edge `linkProperties`). The
|
|
126
|
+
// builder types `links` precisely; here it flows through as runtime data.
|
|
127
|
+
function reviveQueryRow(row: ObjectQueryObject): ObjectQueryExecutorRow {
|
|
128
|
+
const revived: ObjectQueryExecutorRow = {
|
|
129
|
+
primaryId: row.primaryId,
|
|
130
|
+
objectTypeId: row.objectTypeId,
|
|
131
|
+
properties: row.properties,
|
|
132
|
+
createdAt: new Date(row.createdAt),
|
|
133
|
+
updatedAt: new Date(row.updatedAt),
|
|
134
|
+
}
|
|
135
|
+
if (row.links) {
|
|
136
|
+
const links: Record<string, ObjectQueryExecutorLinkValue> = {}
|
|
137
|
+
for (const [linkId, value] of Object.entries(row.links)) {
|
|
138
|
+
links[linkId] = reviveLinkValue(value)
|
|
139
|
+
}
|
|
140
|
+
revived.links = links
|
|
141
|
+
}
|
|
142
|
+
return revived
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function reviveLinkValue(
|
|
146
|
+
value: ObjectQueryObject | ObjectQueryObject[] | null
|
|
147
|
+
): ObjectQueryExecutorLinkValue {
|
|
148
|
+
if (value === null) return null
|
|
149
|
+
if (Array.isArray(value)) return value.map(reviveExpandedRow)
|
|
150
|
+
return reviveExpandedRow(value)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function reviveExpandedRow(row: ObjectQueryObject): ObjectQueryExecutorExpandedRow {
|
|
154
|
+
const revived: ObjectQueryExecutorExpandedRow = reviveQueryRow(row)
|
|
155
|
+
if (row.linkProperties !== undefined) revived.linkProperties = row.linkProperties
|
|
156
|
+
return revived
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// The generated wire type mirrors the core IR JSON schema; they differ only in
|
|
160
|
+
// readonly-ness, which a structural cast cannot bridge.
|
|
161
|
+
function toWireQuery(query: ObjectQuery): WireObjectQuery {
|
|
162
|
+
return query as unknown as WireObjectQuery
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function toSixbQueryError(error: unknown): SixbQueryError {
|
|
166
|
+
// The browser client wraps failures in `SixbApiError`; tests and bare clients
|
|
167
|
+
// surface the raw response body. Unwrap to the body either way so server-sent
|
|
168
|
+
// query issues survive.
|
|
169
|
+
const body = isSixbApiError(error) ? error.body : error
|
|
170
|
+
if (body && typeof body === "object" && "error" in body) {
|
|
171
|
+
const payload = body as { error: unknown; issues?: ObjectQueryIssue[] }
|
|
172
|
+
return new SixbQueryError(`[SixbClient] ${String(payload.error)}`, payload.issues ?? [])
|
|
173
|
+
}
|
|
174
|
+
return new SixbQueryError("[SixbClient] Object query request failed")
|
|
175
|
+
}
|
package/src/sha256.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-dependency streaming SHA-256 (FIPS 180-4).
|
|
3
|
+
*
|
|
4
|
+
* `SubtleCrypto.digest` has no incremental API, so hashing a file through it
|
|
5
|
+
* means buffering the whole file in memory before hashing. This implementation
|
|
6
|
+
* absorbs data one chunk at a time, keeping peak memory bounded to a single
|
|
7
|
+
* slice — letting the client hash multi-gigabyte uploads without OOM. Output is
|
|
8
|
+
* byte-identical to `crypto.subtle.digest("SHA-256", ...)`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// FIPS 180-4 §4.2.2 round constants (first 32 bits of the fractional parts of
|
|
12
|
+
// the cube roots of the first 64 primes).
|
|
13
|
+
const ROUND_CONSTANTS = new Uint32Array([
|
|
14
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
15
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
16
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
17
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
18
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
19
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
20
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
21
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
22
|
+
])
|
|
23
|
+
|
|
24
|
+
function rotr(value: number, bits: number): number {
|
|
25
|
+
return (value >>> bits) | (value << (32 - bits))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
class Sha256 {
|
|
29
|
+
private readonly state = new Uint32Array([
|
|
30
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
31
|
+
])
|
|
32
|
+
private readonly pending = new Uint8Array(64)
|
|
33
|
+
private pendingLength = 0
|
|
34
|
+
private totalBytes = 0
|
|
35
|
+
private readonly schedule = new Uint32Array(64)
|
|
36
|
+
|
|
37
|
+
update(data: Uint8Array): void {
|
|
38
|
+
this.totalBytes += data.byteLength
|
|
39
|
+
let offset = 0
|
|
40
|
+
|
|
41
|
+
if (this.pendingLength > 0) {
|
|
42
|
+
const take = Math.min(64 - this.pendingLength, data.byteLength)
|
|
43
|
+
this.pending.set(data.subarray(0, take), this.pendingLength)
|
|
44
|
+
this.pendingLength += take
|
|
45
|
+
offset = take
|
|
46
|
+
if (this.pendingLength === 64) {
|
|
47
|
+
this.compress(this.pending, 0)
|
|
48
|
+
this.pendingLength = 0
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
while (offset + 64 <= data.byteLength) {
|
|
53
|
+
this.compress(data, offset)
|
|
54
|
+
offset += 64
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (offset < data.byteLength) {
|
|
58
|
+
const rest = data.subarray(offset)
|
|
59
|
+
this.pending.set(rest, 0)
|
|
60
|
+
this.pendingLength = rest.byteLength
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
digest(): Uint8Array {
|
|
65
|
+
// Message length in bits as a 64-bit big-endian value. `totalBytes` is exact
|
|
66
|
+
// in a double well past any real upload size (2^53 bits ≈ 1 PB).
|
|
67
|
+
const bitsHigh = Math.floor(this.totalBytes / 0x20000000) >>> 0
|
|
68
|
+
const bitsLow = (this.totalBytes * 8) >>> 0
|
|
69
|
+
|
|
70
|
+
const paddingLength =
|
|
71
|
+
this.pendingLength < 56 ? 56 - this.pendingLength : 120 - this.pendingLength
|
|
72
|
+
const tail = new Uint8Array(paddingLength + 8)
|
|
73
|
+
tail[0] = 0x80
|
|
74
|
+
const tailView = new DataView(tail.buffer)
|
|
75
|
+
tailView.setUint32(tail.byteLength - 8, bitsHigh)
|
|
76
|
+
tailView.setUint32(tail.byteLength - 4, bitsLow)
|
|
77
|
+
this.update(tail)
|
|
78
|
+
|
|
79
|
+
const digest = new Uint8Array(32)
|
|
80
|
+
const digestView = new DataView(digest.buffer)
|
|
81
|
+
for (let i = 0; i < 8; i++) {
|
|
82
|
+
digestView.setUint32(i * 4, this.state[i])
|
|
83
|
+
}
|
|
84
|
+
return digest
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private compress(block: Uint8Array, offset: number): void {
|
|
88
|
+
const w = this.schedule
|
|
89
|
+
for (let i = 0; i < 16; i++) {
|
|
90
|
+
const j = offset + i * 4
|
|
91
|
+
w[i] = (block[j] << 24) | (block[j + 1] << 16) | (block[j + 2] << 8) | block[j + 3]
|
|
92
|
+
}
|
|
93
|
+
for (let i = 16; i < 64; i++) {
|
|
94
|
+
const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >>> 3)
|
|
95
|
+
const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >>> 10)
|
|
96
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
let a = this.state[0]
|
|
100
|
+
let b = this.state[1]
|
|
101
|
+
let c = this.state[2]
|
|
102
|
+
let d = this.state[3]
|
|
103
|
+
let e = this.state[4]
|
|
104
|
+
let f = this.state[5]
|
|
105
|
+
let g = this.state[6]
|
|
106
|
+
let h = this.state[7]
|
|
107
|
+
|
|
108
|
+
for (let i = 0; i < 64; i++) {
|
|
109
|
+
const bigSigma1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25)
|
|
110
|
+
const ch = (e & f) ^ (~e & g)
|
|
111
|
+
const temp1 = (h + bigSigma1 + ch + ROUND_CONSTANTS[i] + w[i]) >>> 0
|
|
112
|
+
const bigSigma0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)
|
|
113
|
+
const maj = (a & b) ^ (a & c) ^ (b & c)
|
|
114
|
+
const temp2 = (bigSigma0 + maj) >>> 0
|
|
115
|
+
|
|
116
|
+
h = g
|
|
117
|
+
g = f
|
|
118
|
+
f = e
|
|
119
|
+
e = (d + temp1) >>> 0
|
|
120
|
+
d = c
|
|
121
|
+
c = b
|
|
122
|
+
b = a
|
|
123
|
+
a = (temp1 + temp2) >>> 0
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
this.state[0] = (this.state[0] + a) >>> 0
|
|
127
|
+
this.state[1] = (this.state[1] + b) >>> 0
|
|
128
|
+
this.state[2] = (this.state[2] + c) >>> 0
|
|
129
|
+
this.state[3] = (this.state[3] + d) >>> 0
|
|
130
|
+
this.state[4] = (this.state[4] + e) >>> 0
|
|
131
|
+
this.state[5] = (this.state[5] + f) >>> 0
|
|
132
|
+
this.state[6] = (this.state[6] + g) >>> 0
|
|
133
|
+
this.state[7] = (this.state[7] + h) >>> 0
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function toHex(bytes: Uint8Array): string {
|
|
138
|
+
let hex = ""
|
|
139
|
+
for (const byte of bytes) {
|
|
140
|
+
hex += byte.toString(16).padStart(2, "0")
|
|
141
|
+
}
|
|
142
|
+
return hex
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Streams `file` through SHA-256 in `chunkBytes`-sized slices and returns the
|
|
147
|
+
* `sha256:<hex>` digest used for content-addressed blob identity.
|
|
148
|
+
*/
|
|
149
|
+
export async function computeStreamingBlobDigest(
|
|
150
|
+
file: Blob,
|
|
151
|
+
chunkBytes = 8 * 1024 * 1024
|
|
152
|
+
): Promise<`sha256:${string}`> {
|
|
153
|
+
const hasher = new Sha256()
|
|
154
|
+
for (let offset = 0; offset < file.size; offset += chunkBytes) {
|
|
155
|
+
const slice = file.slice(offset, Math.min(file.size, offset + chunkBytes))
|
|
156
|
+
hasher.update(new Uint8Array(await slice.arrayBuffer()))
|
|
157
|
+
}
|
|
158
|
+
return `sha256:${toHex(hasher.digest())}`
|
|
159
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telemetry overlay model (exported via `@sixb/client` and `/hooks`).
|
|
3
|
+
*
|
|
4
|
+
* A normalized "latest value" shape derived from a `telemetry.appended` event,
|
|
5
|
+
* shared by `useLatest`/`useLatestByObject` and the apps that render live
|
|
6
|
+
* telemetry. Previously copy-pasted byte-for-byte across every app's
|
|
7
|
+
* `lib/telemetryEvents.ts`; it lives here once.
|
|
8
|
+
*/
|
|
9
|
+
import type { SixbEventOfType } from "./events-model"
|
|
10
|
+
import { decodeObjectId, encodeObjectId } from "./models"
|
|
11
|
+
|
|
12
|
+
export interface TelemetryUpdate {
|
|
13
|
+
readonly type: "telemetryUpdate"
|
|
14
|
+
readonly projectId: string
|
|
15
|
+
readonly projectName: string
|
|
16
|
+
readonly objectTypeId: string
|
|
17
|
+
readonly objectId: string
|
|
18
|
+
readonly propertyId: string
|
|
19
|
+
readonly value: number | string | boolean
|
|
20
|
+
readonly timestamp: string
|
|
21
|
+
readonly quality: "good" | "uncertain" | "bad"
|
|
22
|
+
readonly unit?: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function telemetryUpdateFromEvent(
|
|
26
|
+
event: SixbEventOfType<"telemetry.appended">
|
|
27
|
+
): TelemetryUpdate {
|
|
28
|
+
return {
|
|
29
|
+
type: "telemetryUpdate",
|
|
30
|
+
projectId: event.projectId,
|
|
31
|
+
projectName: event.projectId,
|
|
32
|
+
objectTypeId: event.payload.objectTypeId,
|
|
33
|
+
objectId: event.payload.objectId,
|
|
34
|
+
propertyId: event.payload.propertyId,
|
|
35
|
+
value: normalizeTelemetryValue(event.payload.value),
|
|
36
|
+
timestamp: event.payload.at,
|
|
37
|
+
quality: "good",
|
|
38
|
+
unit: event.payload.unit,
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function objectIdAliases(objectId: string): readonly string[] {
|
|
43
|
+
const parsed = decodeObjectId(objectId)
|
|
44
|
+
if (!parsed) return [objectId]
|
|
45
|
+
|
|
46
|
+
return [objectId, parsed.primaryId, encodeObjectId(parsed.objectTypeId, parsed.primaryId)]
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function telemetryUpdateKey(
|
|
50
|
+
projectId: string,
|
|
51
|
+
objectId: string,
|
|
52
|
+
propertyId: string
|
|
53
|
+
): string {
|
|
54
|
+
return `${projectId}:${objectId}:${propertyId}`
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function normalizeTelemetryValue(value: unknown): number | string | boolean {
|
|
58
|
+
if (typeof value === "number" || typeof value === "string" || typeof value === "boolean") {
|
|
59
|
+
return value
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (value === null) {
|
|
63
|
+
return "null"
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
return JSON.stringify(value)
|
|
68
|
+
} catch {
|
|
69
|
+
return String(value)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from "react"
|
|
2
|
+
import {
|
|
3
|
+
type AgentRunSnapshot,
|
|
4
|
+
type AgentRunStreamEvent,
|
|
5
|
+
createAgentRunSocket,
|
|
6
|
+
} from "./agent-streams"
|
|
7
|
+
import type { ReconnectingSocketState } from "./ws-socket"
|
|
8
|
+
|
|
9
|
+
export interface UseAgentRunStreamOptions {
|
|
10
|
+
readonly runId?: string | null
|
|
11
|
+
readonly afterCursor?: string
|
|
12
|
+
readonly enabled?: boolean
|
|
13
|
+
readonly reconnect?: boolean
|
|
14
|
+
readonly reconnectDelayMs?: number
|
|
15
|
+
readonly onEvent?: (event: AgentRunStreamEvent, cursor: string) => void
|
|
16
|
+
readonly onRunSnapshot?: (run: AgentRunSnapshot) => void
|
|
17
|
+
readonly onError?: (message: string) => void
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type UseAgentRunStreamResult = ReconnectingSocketState
|
|
21
|
+
|
|
22
|
+
const DISCONNECTED: ReconnectingSocketState = { connected: false, reconnecting: false, error: null }
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* React binding over {@link createAgentRunSocket}: opens the run's stream while enabled and mirrors
|
|
26
|
+
* the transport's connection state into React state. Callbacks are read through refs so re-renders
|
|
27
|
+
* with new callback identities never tear the socket down; the socket is re-created only when a
|
|
28
|
+
* subscription-defining option (runId/afterCursor/enabled/reconnect) changes.
|
|
29
|
+
*/
|
|
30
|
+
export function useAgentRunStream(options: UseAgentRunStreamOptions): UseAgentRunStreamResult {
|
|
31
|
+
const { runId, afterCursor, enabled = true, reconnect, reconnectDelayMs } = options
|
|
32
|
+
const onEventRef = useRef(options.onEvent)
|
|
33
|
+
onEventRef.current = options.onEvent
|
|
34
|
+
const onSnapshotRef = useRef(options.onRunSnapshot)
|
|
35
|
+
onSnapshotRef.current = options.onRunSnapshot
|
|
36
|
+
const onErrorRef = useRef(options.onError)
|
|
37
|
+
onErrorRef.current = options.onError
|
|
38
|
+
|
|
39
|
+
const [state, setState] = useState<ReconnectingSocketState>(DISCONNECTED)
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (!enabled || !runId) {
|
|
43
|
+
setState(DISCONNECTED)
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const socket = createAgentRunSocket({
|
|
48
|
+
runId,
|
|
49
|
+
...(afterCursor ? { afterCursor } : {}),
|
|
50
|
+
reconnect,
|
|
51
|
+
reconnectDelayMs,
|
|
52
|
+
onEvent: (event, cursor) => onEventRef.current?.(event, cursor),
|
|
53
|
+
onRunSnapshot: (run) => onSnapshotRef.current?.(run),
|
|
54
|
+
onError: (message) => onErrorRef.current?.(message),
|
|
55
|
+
onStateChange: setState,
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
return () => socket.close()
|
|
59
|
+
}, [enabled, runId, afterCursor, reconnect, reconnectDelayMs])
|
|
60
|
+
|
|
61
|
+
return state
|
|
62
|
+
}
|
package/src/ws-socket.ts
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React-free reconnecting WebSocket transport shared by the event and agent-run streams.
|
|
3
|
+
*
|
|
4
|
+
* It owns the whole connection lifecycle — connect, send the subscribe frame, reconnect with
|
|
5
|
+
* backoff, tear down — plus the connected/reconnecting/error state machine. Callers inject the URL,
|
|
6
|
+
* a cursor-aware subscribe payload, and message handling, so each stream keeps its own wire
|
|
7
|
+
* protocol while sharing one reconnection loop. React is an optional peer of `@sixb/client`;
|
|
8
|
+
* nothing here may import it.
|
|
9
|
+
*/
|
|
10
|
+
import { client } from "./generated/client.gen"
|
|
11
|
+
|
|
12
|
+
const DEFAULT_SIXB_API_BASE_URL = "http://localhost:3002"
|
|
13
|
+
const DEFAULT_RECONNECT_DELAY_MS = 1000
|
|
14
|
+
const DEFAULT_READY_TIMEOUT_MS = 10_000
|
|
15
|
+
|
|
16
|
+
export interface ReconnectingSocketState {
|
|
17
|
+
readonly connected: boolean
|
|
18
|
+
readonly reconnecting: boolean
|
|
19
|
+
readonly error: string | null
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Handed to `onMessage` so a stream-level error frame updates socket state + notifies the caller. */
|
|
23
|
+
export interface ReconnectingSocketErrorSink {
|
|
24
|
+
reportError(message: string): void
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ReconnectingSocketOptions {
|
|
28
|
+
readonly url: string
|
|
29
|
+
/** Built before every connection; useful for one-shot WebSocket tickets. */
|
|
30
|
+
readonly protocols?: () => readonly string[] | Promise<readonly string[]>
|
|
31
|
+
/** Decide whether a failed async connection setup should be retried. */
|
|
32
|
+
readonly shouldReconnectAfterSetupError?: (error: unknown) => boolean
|
|
33
|
+
readonly reconnect?: boolean
|
|
34
|
+
readonly reconnectDelayMs?: number
|
|
35
|
+
/** Built fresh for each subscription so it can carry the latest resume cursor. */
|
|
36
|
+
readonly subscribeMessage: () => unknown
|
|
37
|
+
/**
|
|
38
|
+
* Optional protocol-level readiness check. When provided, defer the subscribe
|
|
39
|
+
* frame until an inbound message passes this check. Without it, subscribe as
|
|
40
|
+
* soon as the websocket opens.
|
|
41
|
+
*/
|
|
42
|
+
readonly subscribeWhen?: (data: unknown) => boolean
|
|
43
|
+
/** Inbound message that marks the protocol handshake as complete. */
|
|
44
|
+
readonly readyWhen?: (data: unknown) => boolean
|
|
45
|
+
/** Close and reconnect when `readyWhen` does not pass within this duration. */
|
|
46
|
+
readonly readyTimeoutMs?: number
|
|
47
|
+
readonly readyTimeoutMessage?: string
|
|
48
|
+
/** Handle one inbound message; call `sink.reportError` for stream-level error frames. */
|
|
49
|
+
readonly onMessage: (data: unknown, sink: ReconnectingSocketErrorSink) => void
|
|
50
|
+
/** Message surfaced when the socket itself errors (connection failure). */
|
|
51
|
+
readonly connectionErrorMessage: string
|
|
52
|
+
readonly onError?: (message: string) => void
|
|
53
|
+
readonly onStateChange?: (state: ReconnectingSocketState) => void
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface ReconnectingSocket {
|
|
57
|
+
/** Stop reconnecting and close the underlying WebSocket. */
|
|
58
|
+
close(): void
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const INITIAL_STATE: ReconnectingSocketState = {
|
|
62
|
+
connected: false,
|
|
63
|
+
reconnecting: false,
|
|
64
|
+
error: null,
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function createReconnectingSocket(options: ReconnectingSocketOptions): ReconnectingSocket {
|
|
68
|
+
const { reconnect = true, reconnectDelayMs = DEFAULT_RECONNECT_DELAY_MS } = options
|
|
69
|
+
|
|
70
|
+
let state = INITIAL_STATE
|
|
71
|
+
let stopped = false
|
|
72
|
+
let openedOnce = false
|
|
73
|
+
let socket: WebSocket | null = null
|
|
74
|
+
let reconnectTimer: ReturnType<typeof setTimeout> | null = null
|
|
75
|
+
let connectionGeneration = 0
|
|
76
|
+
|
|
77
|
+
const setState = (next: ReconnectingSocketState) => {
|
|
78
|
+
state = next
|
|
79
|
+
options.onStateChange?.(next)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const sink: ReconnectingSocketErrorSink = {
|
|
83
|
+
reportError(message: string) {
|
|
84
|
+
options.onError?.(message)
|
|
85
|
+
setState({ ...state, error: message })
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const scheduleReconnect = () => {
|
|
90
|
+
if (!reconnect || stopped || reconnectTimer) return
|
|
91
|
+
reconnectTimer = setTimeout(() => {
|
|
92
|
+
reconnectTimer = null
|
|
93
|
+
void connect()
|
|
94
|
+
}, reconnectDelayMs)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const connect = async () => {
|
|
98
|
+
if (stopped) return
|
|
99
|
+
const generation = ++connectionGeneration
|
|
100
|
+
setState({ connected: false, reconnecting: openedOnce || state.reconnecting, error: null })
|
|
101
|
+
|
|
102
|
+
let protocols: readonly string[] | undefined
|
|
103
|
+
try {
|
|
104
|
+
if (options.protocols) protocols = await options.protocols()
|
|
105
|
+
} catch (error) {
|
|
106
|
+
if (stopped || generation !== connectionGeneration) return
|
|
107
|
+
sink.reportError(error instanceof Error ? error.message : String(error))
|
|
108
|
+
const retry = reconnect && (options.shouldReconnectAfterSetupError?.(error) ?? true)
|
|
109
|
+
setState({ connected: false, reconnecting: retry, error: state.error })
|
|
110
|
+
if (retry) scheduleReconnect()
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
if (stopped || generation !== connectionGeneration) return
|
|
114
|
+
|
|
115
|
+
const ws = protocols ? new WebSocket(options.url, [...protocols]) : new WebSocket(options.url)
|
|
116
|
+
socket = ws
|
|
117
|
+
let subscribed = false
|
|
118
|
+
let readyTimer: ReturnType<typeof setTimeout> | null = null
|
|
119
|
+
|
|
120
|
+
const clearReadyTimer = () => {
|
|
121
|
+
if (readyTimer) {
|
|
122
|
+
clearTimeout(readyTimer)
|
|
123
|
+
readyTimer = null
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const subscribe = () => {
|
|
128
|
+
if (stopped || subscribed || socket !== ws) return
|
|
129
|
+
subscribed = true
|
|
130
|
+
ws.send(JSON.stringify(options.subscribeMessage()))
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
ws.onopen = () => {
|
|
134
|
+
if (stopped) return
|
|
135
|
+
openedOnce = true
|
|
136
|
+
setState({ connected: true, reconnecting: false, error: null })
|
|
137
|
+
if (!options.subscribeWhen) subscribe()
|
|
138
|
+
if (options.readyWhen) {
|
|
139
|
+
readyTimer = setTimeout(
|
|
140
|
+
() => {
|
|
141
|
+
if (stopped || socket !== ws) return
|
|
142
|
+
try {
|
|
143
|
+
sink.reportError(
|
|
144
|
+
options.readyTimeoutMessage ?? "Websocket protocol handshake timed out."
|
|
145
|
+
)
|
|
146
|
+
} finally {
|
|
147
|
+
ws.close()
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
Math.max(0, options.readyTimeoutMs ?? DEFAULT_READY_TIMEOUT_MS)
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
ws.onmessage = (messageEvent) => {
|
|
156
|
+
if (stopped) return
|
|
157
|
+
if (options.subscribeWhen?.(messageEvent.data)) subscribe()
|
|
158
|
+
if (options.readyWhen?.(messageEvent.data)) clearReadyTimer()
|
|
159
|
+
options.onMessage(messageEvent.data, sink)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
ws.onerror = () => {
|
|
163
|
+
if (stopped) return
|
|
164
|
+
sink.reportError(options.connectionErrorMessage)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
ws.onclose = () => {
|
|
168
|
+
clearReadyTimer()
|
|
169
|
+
if (socket === ws) {
|
|
170
|
+
socket = null
|
|
171
|
+
}
|
|
172
|
+
if (stopped) return
|
|
173
|
+
setState({ connected: false, reconnecting: reconnect, error: state.error })
|
|
174
|
+
scheduleReconnect()
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
void connect()
|
|
179
|
+
|
|
180
|
+
return {
|
|
181
|
+
close() {
|
|
182
|
+
stopped = true
|
|
183
|
+
connectionGeneration += 1
|
|
184
|
+
if (reconnectTimer) {
|
|
185
|
+
clearTimeout(reconnectTimer)
|
|
186
|
+
reconnectTimer = null
|
|
187
|
+
}
|
|
188
|
+
socket?.close()
|
|
189
|
+
socket = null
|
|
190
|
+
},
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Build the `ws(s)://.../<path>` URL for a Sixb WebSocket stream from the client's API base URL. */
|
|
195
|
+
export function createSixbWebSocketUrl(path: string, baseUrl?: string): string {
|
|
196
|
+
const url = new URL(baseUrl ?? client.getConfig().baseUrl ?? DEFAULT_SIXB_API_BASE_URL)
|
|
197
|
+
url.protocol = url.protocol === "https:" ? "wss:" : "ws:"
|
|
198
|
+
url.pathname = path
|
|
199
|
+
url.search = ""
|
|
200
|
+
url.hash = ""
|
|
201
|
+
return url.toString()
|
|
202
|
+
}
|