@sixb/server 0.1.15 → 0.1.17
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/README.md +6 -0
- package/dist/auth/public-routes.d.ts.map +1 -1
- package/dist/auth/shared-access.d.ts.map +1 -1
- package/dist/index.js +408 -327
- package/dist/index.js.map +19 -18
- package/dist/registerRoutes.d.ts.map +1 -1
- package/dist/routes/agents.d.ts +8 -0
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/ai-accounting.d.ts.map +1 -1
- package/dist/routes/auth.d.ts +1 -1
- package/dist/routes/file-downloads.d.ts +98 -0
- package/dist/routes/file-downloads.d.ts.map +1 -0
- package/dist/routes/objects.d.ts +3 -0
- package/dist/routes/objects.d.ts.map +1 -1
- package/dist/routes/ontology.d.ts +6 -4
- package/dist/routes/ontology.d.ts.map +1 -1
- package/dist/routes/status.d.ts +0 -4
- package/dist/routes/status.d.ts.map +1 -1
- package/dist/routes/workflows.d.ts +3 -0
- package/dist/routes/workflows.d.ts.map +1 -1
- package/dist/routes/ws/events.d.ts.map +1 -1
- package/dist/routes/ws/logs.d.ts +11 -0
- package/dist/routes/ws/logs.d.ts.map +1 -1
- package/dist/routes/ws/subscription-hub.d.ts +66 -0
- package/dist/routes/ws/subscription-hub.d.ts.map +1 -0
- package/dist/schemas/agents.d.ts +33 -0
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-accounting.d.ts +224 -100
- package/dist/schemas/ai-accounting.d.ts.map +1 -1
- package/dist/schemas/ai-usage.d.ts +3 -0
- package/dist/schemas/ai-usage.d.ts.map +1 -1
- package/dist/schemas/auth.d.ts +2 -2
- package/dist/schemas/objects.d.ts.map +1 -1
- package/dist/schemas/ontology.d.ts +27 -18
- package/dist/schemas/ontology.d.ts.map +1 -1
- package/dist/schemas/status.d.ts +0 -26
- package/dist/schemas/status.d.ts.map +1 -1
- package/dist/schemas/workflows.d.ts +35 -0
- package/dist/schemas/workflows.d.ts.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/auth/public-routes.ts +10 -0
- package/src/auth/shared-access.ts +48 -6
- package/src/registerRoutes.ts +2 -0
- package/src/routes/ai-accounting.ts +1 -0
- package/src/routes/file-downloads.ts +48 -0
- package/src/routes/objects.ts +13 -3
- package/src/routes/ontology.ts +1 -0
- package/src/routes/ws/events.ts +96 -133
- package/src/routes/ws/logs.ts +67 -5
- package/src/routes/ws/{log-subscription-hub.ts → subscription-hub.ts} +137 -127
- package/src/schemas/ai-accounting.ts +5 -0
- package/src/schemas/ai-usage.ts +3 -0
- package/src/schemas/objects.ts +28 -2
- package/src/schemas/ontology.ts +1 -0
- package/src/schemas/status.ts +0 -6
- package/src/server.ts +5 -0
- package/dist/routes/ws/log-subscription-hub.d.ts +0 -50
- package/dist/routes/ws/log-subscription-hub.d.ts.map +0 -1
|
@@ -1,33 +1,51 @@
|
|
|
1
|
-
import type { LogLevel, LogRunRef, SixbRunKind, StoredLogLine } from "@sixb/core"
|
|
2
1
|
import { BrokerCursorExpiredError } from "@sixb/core/broker"
|
|
3
|
-
import type { LoggingService } from "@sixb/core/internal/logging"
|
|
4
2
|
|
|
5
3
|
const MAX_CLIENT_RECORDS = 1_000
|
|
6
4
|
const MAX_CLIENT_BYTES = 1_048_576
|
|
7
5
|
const MAX_SOCKET_BUFFERED_BYTES = 1_048_576
|
|
8
6
|
const MAX_BATCH_RECORDS = 100
|
|
9
7
|
const FLUSH_DELAY_MS = 10
|
|
10
|
-
const READ_PAGE_SIZE = 500
|
|
11
8
|
|
|
12
|
-
interface
|
|
9
|
+
interface HubSocket {
|
|
13
10
|
send(message: string): unknown
|
|
14
11
|
close(code?: number, reason?: string): unknown
|
|
15
12
|
readonly raw?: unknown
|
|
16
13
|
}
|
|
17
14
|
|
|
18
|
-
|
|
19
|
-
readonly
|
|
20
|
-
readonly levels?: readonly LogLevel[]
|
|
21
|
-
readonly run?: LogRunRef
|
|
22
|
-
readonly afterCursor?: string
|
|
15
|
+
interface HubRecord {
|
|
16
|
+
readonly cursor: string
|
|
23
17
|
}
|
|
24
18
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
19
|
+
/** One retained broker stream, as the hub consumes and frames it. */
|
|
20
|
+
export interface HubSource<T extends HubRecord> {
|
|
21
|
+
/** Delivers records appended after the call. */
|
|
22
|
+
subscribe(deliver: (records: readonly T[]) => void): Promise<() => void>
|
|
23
|
+
/** Socket frames for one flushed batch, in order. */
|
|
24
|
+
frames(records: readonly T[]): readonly unknown[]
|
|
25
|
+
/** Frame for a client whose replay cursor left retention. Live delivery continues after it. */
|
|
26
|
+
expired(): Promise<unknown>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** One socket's view of the stream. */
|
|
30
|
+
export interface HubSubscription<T extends HubRecord> {
|
|
31
|
+
matches(record: T): boolean
|
|
32
|
+
/** Replays retained records after `afterCursor` (or from the oldest) before live ones. */
|
|
33
|
+
readonly replay?: {
|
|
34
|
+
readonly afterCursor: string | undefined
|
|
35
|
+
read(afterCursor: string | undefined): Promise<{
|
|
36
|
+
readonly records: readonly T[]
|
|
37
|
+
readonly cursor?: string
|
|
38
|
+
readonly hasMore: boolean
|
|
39
|
+
}>
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface ClientState<T extends HubRecord> {
|
|
44
|
+
readonly ws: HubSocket
|
|
45
|
+
readonly subscription: HubSubscription<T>
|
|
46
|
+
readonly queue: T[]
|
|
29
47
|
readonly queuedCursors: Set<string>
|
|
30
|
-
readonly pendingLive:
|
|
48
|
+
readonly pendingLive: T[]
|
|
31
49
|
readonly pendingCursors: Set<string>
|
|
32
50
|
readonly replayedPendingCursors: Set<string>
|
|
33
51
|
queuedBytes: number
|
|
@@ -39,40 +57,44 @@ interface ClientState {
|
|
|
39
57
|
}
|
|
40
58
|
|
|
41
59
|
/**
|
|
42
|
-
* One broker subscription per server/project, multiplexed to all connected
|
|
43
|
-
*
|
|
44
|
-
* unbounded portion of the process heap.
|
|
60
|
+
* One broker subscription per server/project and stream, multiplexed to all connected clients
|
|
61
|
+
* and released when the last one leaves. Each client has a bounded queue so a slow socket cannot
|
|
62
|
+
* retain an unbounded portion of the process heap.
|
|
45
63
|
*/
|
|
46
|
-
export class
|
|
47
|
-
private readonly clients = new Map<object, ClientState
|
|
64
|
+
export class SubscriptionHub<T extends HubRecord> {
|
|
65
|
+
private readonly clients = new Map<object, ClientState<T>>()
|
|
48
66
|
private readonly subscriptionGenerations = new WeakMap<object, number>()
|
|
49
|
-
private
|
|
50
|
-
private unsubscribeBroker: (() => void) | null = null
|
|
67
|
+
private started: Promise<() => void> | null = null
|
|
51
68
|
private closed = false
|
|
52
69
|
|
|
53
|
-
constructor(private readonly
|
|
70
|
+
constructor(private readonly source: HubSource<T>) {}
|
|
54
71
|
|
|
55
72
|
async subscribe(
|
|
56
73
|
key: object,
|
|
57
|
-
ws:
|
|
58
|
-
|
|
74
|
+
ws: HubSocket,
|
|
75
|
+
subscription: HubSubscription<T>,
|
|
59
76
|
onSubscribed: () => void
|
|
60
77
|
): Promise<void> {
|
|
61
|
-
if (this.closed) throw new Error("
|
|
78
|
+
if (this.closed) throw new Error("Subscription hub is closed")
|
|
62
79
|
const generation = this.nextSubscriptionGeneration(key)
|
|
63
80
|
this.removeClient(key)
|
|
64
81
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
82
|
+
// The last client leaving while this waits releases the subscription it waited for.
|
|
83
|
+
let started: Promise<() => void>
|
|
84
|
+
do {
|
|
85
|
+
started = this.start()
|
|
86
|
+
try {
|
|
87
|
+
await started
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if (this.closed || !this.isCurrentSubscription(key, generation)) return
|
|
90
|
+
throw error
|
|
91
|
+
}
|
|
68
92
|
if (this.closed || !this.isCurrentSubscription(key, generation)) return
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
if (this.closed || !this.isCurrentSubscription(key, generation)) return
|
|
93
|
+
} while (started !== this.started)
|
|
72
94
|
|
|
73
|
-
const state: ClientState = {
|
|
95
|
+
const state: ClientState<T> = {
|
|
74
96
|
ws,
|
|
75
|
-
|
|
97
|
+
subscription,
|
|
76
98
|
queue: [],
|
|
77
99
|
queuedCursors: new Set(),
|
|
78
100
|
pendingLive: [],
|
|
@@ -82,14 +104,14 @@ export class LogSubscriptionHub {
|
|
|
82
104
|
pendingBytes: 0,
|
|
83
105
|
flushTimer: null,
|
|
84
106
|
queueProgressWaiters: new Set(),
|
|
85
|
-
catchingUp:
|
|
107
|
+
catchingUp: subscription.replay !== undefined,
|
|
86
108
|
closed: false,
|
|
87
109
|
}
|
|
88
110
|
this.clients.set(key, state)
|
|
89
111
|
onSubscribed()
|
|
90
112
|
|
|
91
|
-
if (
|
|
92
|
-
void this.catchUp(key, state)
|
|
113
|
+
if (subscription.replay) {
|
|
114
|
+
void this.catchUp(key, state, subscription.replay)
|
|
93
115
|
}
|
|
94
116
|
}
|
|
95
117
|
|
|
@@ -104,7 +126,12 @@ export class LogSubscriptionHub {
|
|
|
104
126
|
state.closed = true
|
|
105
127
|
if (state.flushTimer) clearTimeout(state.flushTimer)
|
|
106
128
|
this.notifyQueueProgress(state)
|
|
129
|
+
this.deleteClient(key)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private deleteClient(key: object): void {
|
|
107
133
|
this.clients.delete(key)
|
|
134
|
+
if (this.clients.size === 0) this.release()
|
|
108
135
|
}
|
|
109
136
|
|
|
110
137
|
private nextSubscriptionGeneration(key: object): number {
|
|
@@ -119,63 +146,65 @@ export class LogSubscriptionHub {
|
|
|
119
146
|
|
|
120
147
|
async close(): Promise<void> {
|
|
121
148
|
this.closed = true
|
|
122
|
-
this.unsubscribeBroker?.()
|
|
123
|
-
this.unsubscribeBroker = null
|
|
124
149
|
for (const key of this.clients.keys()) this.unsubscribe(key)
|
|
150
|
+
this.release()
|
|
125
151
|
}
|
|
126
152
|
|
|
127
|
-
private
|
|
128
|
-
if (this.closed)
|
|
129
|
-
if (!this.
|
|
130
|
-
this.
|
|
131
|
-
|
|
132
|
-
.
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
})
|
|
136
|
-
.catch((error) => {
|
|
137
|
-
this.startPromise = null
|
|
138
|
-
throw error
|
|
139
|
-
})
|
|
153
|
+
private start(): Promise<() => void> {
|
|
154
|
+
if (this.closed) return Promise.reject(new Error("Subscription hub is closed"))
|
|
155
|
+
if (!this.started) {
|
|
156
|
+
const started = this.source.subscribe((records) => this.deliverLive(records))
|
|
157
|
+
started.catch(() => {
|
|
158
|
+
if (this.started === started) this.started = null
|
|
159
|
+
})
|
|
160
|
+
this.started = started
|
|
140
161
|
}
|
|
141
|
-
|
|
162
|
+
return this.started
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
private release(): void {
|
|
166
|
+
const started = this.started
|
|
167
|
+
this.started = null
|
|
168
|
+
started?.then(
|
|
169
|
+
(unsubscribe) => unsubscribe(),
|
|
170
|
+
() => undefined
|
|
171
|
+
)
|
|
142
172
|
}
|
|
143
173
|
|
|
144
|
-
private deliverLive(
|
|
174
|
+
private deliverLive(records: readonly T[]): void {
|
|
145
175
|
for (const state of this.clients.values()) {
|
|
146
|
-
for (const
|
|
147
|
-
if (!matches(
|
|
148
|
-
if (state.catchingUp) this.enqueuePendingLive(state,
|
|
149
|
-
else this.enqueue(state,
|
|
176
|
+
for (const record of records) {
|
|
177
|
+
if (!state.subscription.matches(record)) continue
|
|
178
|
+
if (state.catchingUp) this.enqueuePendingLive(state, record)
|
|
179
|
+
else this.enqueue(state, record)
|
|
150
180
|
}
|
|
151
181
|
}
|
|
152
182
|
}
|
|
153
183
|
|
|
154
|
-
private async catchUp(
|
|
155
|
-
|
|
184
|
+
private async catchUp(
|
|
185
|
+
key: object,
|
|
186
|
+
state: ClientState<T>,
|
|
187
|
+
replay: NonNullable<HubSubscription<T>["replay"]>
|
|
188
|
+
): Promise<void> {
|
|
189
|
+
let afterCursor = replay.afterCursor
|
|
156
190
|
try {
|
|
157
191
|
let hasMore = true
|
|
158
192
|
while (hasMore) {
|
|
159
|
-
const page = await
|
|
160
|
-
afterCursor,
|
|
161
|
-
limit: READ_PAGE_SIZE,
|
|
162
|
-
kinds: state.filter.kinds,
|
|
163
|
-
levels: state.filter.levels,
|
|
164
|
-
run: state.filter.run,
|
|
165
|
-
})
|
|
193
|
+
const page = await replay.read(afterCursor)
|
|
166
194
|
if (state.closed || this.clients.get(key) !== state) return
|
|
167
195
|
|
|
168
|
-
for (const
|
|
169
|
-
if (state.pendingCursors.has(
|
|
170
|
-
state.replayedPendingCursors.add(
|
|
196
|
+
for (const record of page.records) {
|
|
197
|
+
if (state.pendingCursors.has(record.cursor)) {
|
|
198
|
+
state.replayedPendingCursors.add(record.cursor)
|
|
171
199
|
}
|
|
172
|
-
if (!
|
|
200
|
+
if (!state.subscription.matches(record)) continue
|
|
201
|
+
if (!(await this.enqueueReplay(state, record))) return
|
|
173
202
|
}
|
|
174
203
|
if (state.closed) return
|
|
175
204
|
afterCursor = page.cursor ?? afterCursor
|
|
176
205
|
hasMore = page.hasMore
|
|
177
206
|
if (hasMore && !page.cursor) {
|
|
178
|
-
throw new Error("
|
|
207
|
+
throw new Error("Broker returned hasMore without a cursor")
|
|
179
208
|
}
|
|
180
209
|
}
|
|
181
210
|
|
|
@@ -186,8 +215,8 @@ export class LogSubscriptionHub {
|
|
|
186
215
|
state.pendingCursors.clear()
|
|
187
216
|
state.replayedPendingCursors.clear()
|
|
188
217
|
state.pendingBytes = 0
|
|
189
|
-
for (const
|
|
190
|
-
if (!replayedPendingCursors.has(
|
|
218
|
+
for (const record of pendingLive) {
|
|
219
|
+
if (!replayedPendingCursors.has(record.cursor)) this.enqueue(state, record)
|
|
191
220
|
}
|
|
192
221
|
} catch (error) {
|
|
193
222
|
if (state.closed || this.clients.get(key) !== state) return
|
|
@@ -199,12 +228,7 @@ export class LogSubscriptionHub {
|
|
|
199
228
|
|
|
200
229
|
if (error instanceof BrokerCursorExpiredError) {
|
|
201
230
|
try {
|
|
202
|
-
|
|
203
|
-
this.send(state, {
|
|
204
|
-
type: "reset",
|
|
205
|
-
reason: "cursor_expired",
|
|
206
|
-
cursor: latest.lines.at(-1)?.cursor ?? latest.cursor,
|
|
207
|
-
})
|
|
231
|
+
this.send(state, await this.source.expired())
|
|
208
232
|
} catch (resetError) {
|
|
209
233
|
this.fail(
|
|
210
234
|
state,
|
|
@@ -219,28 +243,28 @@ export class LogSubscriptionHub {
|
|
|
219
243
|
}
|
|
220
244
|
}
|
|
221
245
|
|
|
222
|
-
private enqueue(state: ClientState
|
|
223
|
-
if (state.closed || state.queuedCursors.has(
|
|
224
|
-
const bytes = encodedBytes(
|
|
246
|
+
private enqueue(state: ClientState<T>, record: T): void {
|
|
247
|
+
if (state.closed || state.queuedCursors.has(record.cursor)) return
|
|
248
|
+
const bytes = encodedBytes(record)
|
|
225
249
|
if (!hasQueueCapacity(state, bytes)) {
|
|
226
|
-
this.fail(state, "
|
|
250
|
+
this.fail(state, "Stream client is too slow; reconnect from the last cursor.", 1013)
|
|
227
251
|
return
|
|
228
252
|
}
|
|
229
253
|
|
|
230
|
-
this.
|
|
254
|
+
this.pushQueuedRecord(state, record, bytes)
|
|
231
255
|
}
|
|
232
256
|
|
|
233
|
-
private async enqueueReplay(state: ClientState
|
|
257
|
+
private async enqueueReplay(state: ClientState<T>, record: T): Promise<boolean> {
|
|
234
258
|
if (state.closed) return false
|
|
235
|
-
if (state.queuedCursors.has(
|
|
236
|
-
const bytes = encodedBytes(
|
|
259
|
+
if (state.queuedCursors.has(record.cursor)) return true
|
|
260
|
+
const bytes = encodedBytes(record)
|
|
237
261
|
|
|
238
262
|
while (!hasQueueCapacity(state, bytes)) {
|
|
239
263
|
// Replay is an internal producer and can pause while already-queued replay
|
|
240
264
|
// batches drain. Pending live records cannot drain until replay finishes,
|
|
241
265
|
// so a queue containing only pending live records has no forward progress.
|
|
242
266
|
if (state.queue.length === 0 || socketBufferedAmount(state.ws) > MAX_SOCKET_BUFFERED_BYTES) {
|
|
243
|
-
this.fail(state, "
|
|
267
|
+
this.fail(state, "Stream client is too slow; reconnect from the last cursor.", 1013)
|
|
244
268
|
return false
|
|
245
269
|
}
|
|
246
270
|
this.scheduleFlush(state)
|
|
@@ -248,30 +272,30 @@ export class LogSubscriptionHub {
|
|
|
248
272
|
if (state.closed) return false
|
|
249
273
|
}
|
|
250
274
|
|
|
251
|
-
this.
|
|
275
|
+
this.pushQueuedRecord(state, record, bytes)
|
|
252
276
|
return !state.closed
|
|
253
277
|
}
|
|
254
278
|
|
|
255
|
-
private
|
|
256
|
-
state.queue.push(
|
|
257
|
-
state.queuedCursors.add(
|
|
279
|
+
private pushQueuedRecord(state: ClientState<T>, record: T, bytes: number): void {
|
|
280
|
+
state.queue.push(record)
|
|
281
|
+
state.queuedCursors.add(record.cursor)
|
|
258
282
|
state.queuedBytes += bytes
|
|
259
283
|
this.scheduleFlush(state)
|
|
260
284
|
}
|
|
261
285
|
|
|
262
|
-
private enqueuePendingLive(state: ClientState
|
|
263
|
-
if (state.closed || state.pendingCursors.has(
|
|
264
|
-
const bytes = encodedBytes(
|
|
286
|
+
private enqueuePendingLive(state: ClientState<T>, record: T): void {
|
|
287
|
+
if (state.closed || state.pendingCursors.has(record.cursor)) return
|
|
288
|
+
const bytes = encodedBytes(record)
|
|
265
289
|
if (!hasQueueCapacity(state, bytes)) {
|
|
266
|
-
this.fail(state, "
|
|
290
|
+
this.fail(state, "Stream client is too slow; reconnect from the last cursor.", 1013)
|
|
267
291
|
return
|
|
268
292
|
}
|
|
269
|
-
state.pendingLive.push(
|
|
270
|
-
state.pendingCursors.add(
|
|
293
|
+
state.pendingLive.push(record)
|
|
294
|
+
state.pendingCursors.add(record.cursor)
|
|
271
295
|
state.pendingBytes += bytes
|
|
272
296
|
}
|
|
273
297
|
|
|
274
|
-
private scheduleFlush(state: ClientState): void {
|
|
298
|
+
private scheduleFlush(state: ClientState<T>): void {
|
|
275
299
|
if (state.closed || state.flushTimer) return
|
|
276
300
|
state.flushTimer = setTimeout(() => {
|
|
277
301
|
state.flushTimer = null
|
|
@@ -279,34 +303,34 @@ export class LogSubscriptionHub {
|
|
|
279
303
|
}, FLUSH_DELAY_MS)
|
|
280
304
|
}
|
|
281
305
|
|
|
282
|
-
private flush(state: ClientState): void {
|
|
306
|
+
private flush(state: ClientState<T>): void {
|
|
283
307
|
if (state.closed || state.queue.length === 0) return
|
|
284
308
|
if (socketBufferedAmount(state.ws) > MAX_SOCKET_BUFFERED_BYTES) {
|
|
285
309
|
this.scheduleFlush(state)
|
|
286
310
|
return
|
|
287
311
|
}
|
|
288
312
|
|
|
289
|
-
const
|
|
290
|
-
for (const
|
|
291
|
-
state.queuedCursors.delete(
|
|
292
|
-
state.queuedBytes -= encodedBytes(
|
|
313
|
+
const records = state.queue.splice(0, MAX_BATCH_RECORDS)
|
|
314
|
+
for (const record of records) {
|
|
315
|
+
state.queuedCursors.delete(record.cursor)
|
|
316
|
+
state.queuedBytes -= encodedBytes(record)
|
|
293
317
|
}
|
|
294
318
|
this.notifyQueueProgress(state)
|
|
295
|
-
this.send(state,
|
|
319
|
+
for (const frame of this.source.frames(records)) this.send(state, frame)
|
|
296
320
|
if (state.queue.length > 0) this.scheduleFlush(state)
|
|
297
321
|
}
|
|
298
322
|
|
|
299
|
-
private waitForQueueProgress(state: ClientState): Promise<void> {
|
|
323
|
+
private waitForQueueProgress(state: ClientState<T>): Promise<void> {
|
|
300
324
|
return new Promise((resolve) => state.queueProgressWaiters.add(resolve))
|
|
301
325
|
}
|
|
302
326
|
|
|
303
|
-
private notifyQueueProgress(state: ClientState): void {
|
|
327
|
+
private notifyQueueProgress(state: ClientState<T>): void {
|
|
304
328
|
const waiters = [...state.queueProgressWaiters]
|
|
305
329
|
state.queueProgressWaiters.clear()
|
|
306
330
|
for (const resolve of waiters) resolve()
|
|
307
331
|
}
|
|
308
332
|
|
|
309
|
-
private send(state: ClientState
|
|
333
|
+
private send(state: ClientState<T>, payload: unknown): void {
|
|
310
334
|
if (state.closed) return
|
|
311
335
|
try {
|
|
312
336
|
state.ws.send(JSON.stringify(payload))
|
|
@@ -315,7 +339,7 @@ export class LogSubscriptionHub {
|
|
|
315
339
|
}
|
|
316
340
|
}
|
|
317
341
|
|
|
318
|
-
private fail(state: ClientState
|
|
342
|
+
private fail(state: ClientState<T>, message: string, closeCode: number): void {
|
|
319
343
|
if (state.closed) return
|
|
320
344
|
try {
|
|
321
345
|
state.ws.send(JSON.stringify({ type: "error", message }))
|
|
@@ -326,42 +350,28 @@ export class LogSubscriptionHub {
|
|
|
326
350
|
if (state.flushTimer) clearTimeout(state.flushTimer)
|
|
327
351
|
this.notifyQueueProgress(state)
|
|
328
352
|
for (const [key, candidate] of this.clients) {
|
|
329
|
-
if (candidate === state) this.
|
|
353
|
+
if (candidate === state) this.deleteClient(key)
|
|
330
354
|
}
|
|
331
355
|
try {
|
|
332
|
-
state.ws.close(
|
|
333
|
-
closeCode,
|
|
334
|
-
closeCode === 1013 ? "Log stream backpressure" : "Log stream failure"
|
|
335
|
-
)
|
|
356
|
+
state.ws.close(closeCode, closeCode === 1013 ? "Stream backpressure" : "Stream failure")
|
|
336
357
|
} catch {
|
|
337
358
|
// Socket is already gone.
|
|
338
359
|
}
|
|
339
360
|
}
|
|
340
361
|
}
|
|
341
362
|
|
|
342
|
-
function hasQueueCapacity(state: ClientState
|
|
363
|
+
function hasQueueCapacity(state: ClientState<HubRecord>, bytes: number): boolean {
|
|
343
364
|
return (
|
|
344
365
|
state.queue.length + state.pendingLive.length < MAX_CLIENT_RECORDS &&
|
|
345
366
|
state.queuedBytes + state.pendingBytes + bytes <= MAX_CLIENT_BYTES
|
|
346
367
|
)
|
|
347
368
|
}
|
|
348
369
|
|
|
349
|
-
function matches(line: StoredLogLine, filter: LogSubscriptionFilter): boolean {
|
|
350
|
-
if (filter.run) {
|
|
351
|
-
if (line.context.run.kind !== filter.run.kind || line.context.run.id !== filter.run.id) {
|
|
352
|
-
return false
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
if (filter.kinds && !filter.kinds.includes(line.context.run.kind)) return false
|
|
356
|
-
if (filter.levels && !filter.levels.includes(line.level)) return false
|
|
357
|
-
return true
|
|
358
|
-
}
|
|
359
|
-
|
|
360
370
|
function encodedBytes(value: unknown): number {
|
|
361
371
|
return new TextEncoder().encode(JSON.stringify(value)).byteLength
|
|
362
372
|
}
|
|
363
373
|
|
|
364
|
-
function socketBufferedAmount(ws:
|
|
374
|
+
function socketBufferedAmount(ws: HubSocket): number {
|
|
365
375
|
const raw = ws.raw
|
|
366
376
|
if (!raw || typeof raw !== "object" || !("bufferedAmount" in raw)) return 0
|
|
367
377
|
const bufferedAmount = (raw as { bufferedAmount?: unknown }).bufferedAmount
|
|
@@ -58,6 +58,7 @@ const AiAccountingAggregateSchema = z.object({
|
|
|
58
58
|
cacheWriteInputTokens: z.number().int().nonnegative(),
|
|
59
59
|
textOutputTokens: z.number().int().nonnegative(),
|
|
60
60
|
reasoningOutputTokens: z.number().int().nonnegative(),
|
|
61
|
+
audioDurationMs: z.number().int().nonnegative(),
|
|
61
62
|
}),
|
|
62
63
|
}),
|
|
63
64
|
costs: AiCostSummarySchema,
|
|
@@ -120,6 +121,7 @@ const AiCostComponentSchema = z.object({
|
|
|
120
121
|
"tokens.output.total",
|
|
121
122
|
"tokens.output.text",
|
|
122
123
|
"tokens.output.reasoning",
|
|
124
|
+
"audio.input.milliseconds",
|
|
123
125
|
]),
|
|
124
126
|
quantity: IntegerStringSchema,
|
|
125
127
|
rateAmountNanosPerMillion: IntegerStringSchema,
|
|
@@ -192,6 +194,9 @@ const AiModelCallUsageRecordSchema = z.object({
|
|
|
192
194
|
callId: z.string(),
|
|
193
195
|
providerId: z.string(),
|
|
194
196
|
requestedModelId: z.string(),
|
|
197
|
+
modelKind: z
|
|
198
|
+
.enum(["language", "image", "video", "embedding", "decision", "transcription", "reranking"])
|
|
199
|
+
.optional(),
|
|
195
200
|
requestedReasoning: ModelReasoningSchema.optional(),
|
|
196
201
|
responseModelId: z.string().optional(),
|
|
197
202
|
responseId: z.string(),
|
package/src/schemas/ai-usage.ts
CHANGED
|
@@ -12,5 +12,8 @@ export const AiUsageSummarySchema = z.object({
|
|
|
12
12
|
cacheWriteInputTokens: TokenCountSchema.optional(),
|
|
13
13
|
textOutputTokens: TokenCountSchema.optional(),
|
|
14
14
|
reasoningOutputTokens: TokenCountSchema.optional(),
|
|
15
|
+
audioDurationMs: TokenCountSchema.optional().describe(
|
|
16
|
+
"Audio content duration in milliseconds; not billed duration."
|
|
17
|
+
),
|
|
15
18
|
reportingStatus: z.enum(["complete", "partial", "unavailable"]),
|
|
16
19
|
})
|
package/src/schemas/objects.ts
CHANGED
|
@@ -239,6 +239,13 @@ export const ObjectQuerySchema: z.ZodType<unknown> = z.lazy(() =>
|
|
|
239
239
|
k: z.number().int().positive(),
|
|
240
240
|
})
|
|
241
241
|
.strict(),
|
|
242
|
+
z
|
|
243
|
+
.object({
|
|
244
|
+
kind: z.literal("rerank"),
|
|
245
|
+
input: ObjectQuerySchema,
|
|
246
|
+
model: z.object({ provider: z.string().min(1), modelId: z.string().min(1) }).strict(),
|
|
247
|
+
})
|
|
248
|
+
.strict(),
|
|
242
249
|
z
|
|
243
250
|
.object({
|
|
244
251
|
kind: z.literal("traverse"),
|
|
@@ -424,7 +431,7 @@ export const ObjectQueryOpenApiSchemas: OpenApiSchemas = {
|
|
|
424
431
|
required: ["mode", "providerIssues", "fallbackIssues", "issues"],
|
|
425
432
|
additionalProperties: false,
|
|
426
433
|
properties: {
|
|
427
|
-
mode: { type: "string", enum: ["pushdown", "fallback", "rejected"] },
|
|
434
|
+
mode: { type: "string", enum: ["pushdown", "fallback", "pipeline", "rejected"] },
|
|
428
435
|
providerIssues: {
|
|
429
436
|
type: "array",
|
|
430
437
|
items: { $ref: "#/components/schemas/ObjectQueryIssue" },
|
|
@@ -504,9 +511,10 @@ export const ObjectQueryOpenApiSchemas: OpenApiSchemas = {
|
|
|
504
511
|
},
|
|
505
512
|
ObjectQueryFacetsResponse: {
|
|
506
513
|
type: "object",
|
|
507
|
-
required: ["facets", "plan"],
|
|
514
|
+
required: ["total", "facets", "plan"],
|
|
508
515
|
additionalProperties: false,
|
|
509
516
|
properties: {
|
|
517
|
+
total: { type: "number" },
|
|
510
518
|
facets: {
|
|
511
519
|
type: "array",
|
|
512
520
|
items: { $ref: "#/components/schemas/ObjectQueryFacetResult" },
|
|
@@ -702,6 +710,24 @@ export const ObjectQueryOpenApiSchemas: OpenApiSchemas = {
|
|
|
702
710
|
k: { type: "integer", minimum: 1 },
|
|
703
711
|
},
|
|
704
712
|
},
|
|
713
|
+
{
|
|
714
|
+
type: "object",
|
|
715
|
+
required: ["kind", "input", "model"],
|
|
716
|
+
additionalProperties: false,
|
|
717
|
+
properties: {
|
|
718
|
+
kind: { type: "string", enum: ["rerank"] },
|
|
719
|
+
input: objectQueryRef,
|
|
720
|
+
model: {
|
|
721
|
+
type: "object",
|
|
722
|
+
required: ["provider", "modelId"],
|
|
723
|
+
additionalProperties: false,
|
|
724
|
+
properties: {
|
|
725
|
+
provider: { type: "string", minLength: 1 },
|
|
726
|
+
modelId: { type: "string", minLength: 1 },
|
|
727
|
+
},
|
|
728
|
+
},
|
|
729
|
+
},
|
|
730
|
+
},
|
|
705
731
|
{
|
|
706
732
|
type: "object",
|
|
707
733
|
required: ["kind", "input", "linkId", "direction"],
|
package/src/schemas/ontology.ts
CHANGED
|
@@ -87,6 +87,7 @@ export const ObjectTypeSchema = z.object({
|
|
|
87
87
|
providerId: z.string(),
|
|
88
88
|
modelId: z.string(),
|
|
89
89
|
dimensions: z.number().int().positive(),
|
|
90
|
+
inputMode: z.enum(["symmetric", "asymmetric"]).optional(),
|
|
90
91
|
representation: z
|
|
91
92
|
.object({ name: z.string(), version: z.string().optional() })
|
|
92
93
|
.optional(),
|
package/src/schemas/status.ts
CHANGED
|
@@ -29,12 +29,6 @@ const MaintenanceSummarySchema = z.object({
|
|
|
29
29
|
maxAttempts: z.number().int().nonnegative(),
|
|
30
30
|
})
|
|
31
31
|
.nullable(),
|
|
32
|
-
terminalSources: z
|
|
33
|
-
.object({
|
|
34
|
-
count: z.number().int().nonnegative(),
|
|
35
|
-
oldestTerminalAt: z.string().nullable(),
|
|
36
|
-
})
|
|
37
|
-
.nullable(),
|
|
38
32
|
cleanup: z
|
|
39
33
|
.object({
|
|
40
34
|
publishedOutboxRowsDeleted: z.number().int().nonnegative(),
|
package/src/server.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { openapi } from "@elysiajs/openapi"
|
|
|
3
3
|
import type { OntologyMaintenanceHandle, SixbHostView } from "@sixb/core"
|
|
4
4
|
import { CSRF_HEADER_NAME, setRequestClientAddress } from "@sixb/core/internal/auth"
|
|
5
5
|
import { bindRequestExecution } from "@sixb/core/internal/request-execution"
|
|
6
|
+
import { setApiPublicOrigin } from "@sixb/core/internal/runtime"
|
|
6
7
|
import type { Server } from "bun"
|
|
7
8
|
import { Elysia } from "elysia"
|
|
8
9
|
import { websocket as elysiaWebSocket } from "elysia/ws"
|
|
@@ -102,6 +103,10 @@ export class SixbServer {
|
|
|
102
103
|
this.hostname = options.hostname ?? "0.0.0.0"
|
|
103
104
|
this.quiet = options.quiet ?? false
|
|
104
105
|
this.apiBrowserPolicy = resolveApiBrowserPolicy(options.browser)
|
|
106
|
+
// URLs the API hands to third parties, such as file download URLs, name this origin.
|
|
107
|
+
if (this.apiBrowserPolicy.publicOrigin) {
|
|
108
|
+
setApiPublicOrigin(this.hostRuntime, this.apiBrowserPolicy.publicOrigin)
|
|
109
|
+
}
|
|
105
110
|
this.authContextResolver = createApiBrowserAuthContextResolver(this.apiBrowserPolicy)
|
|
106
111
|
this.authRedirectContextResolver = createApiBrowserAuthRedirectContextResolver(
|
|
107
112
|
this.apiBrowserPolicy
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import type { LogLevel, LogRunRef, SixbRunKind } from "@sixb/core";
|
|
2
|
-
import type { LoggingService } from "@sixb/core/internal/logging";
|
|
3
|
-
interface LogSocket {
|
|
4
|
-
send(message: string): unknown;
|
|
5
|
-
close(code?: number, reason?: string): unknown;
|
|
6
|
-
readonly raw?: unknown;
|
|
7
|
-
}
|
|
8
|
-
export interface LogSubscriptionFilter {
|
|
9
|
-
readonly kinds?: readonly SixbRunKind[];
|
|
10
|
-
readonly levels?: readonly LogLevel[];
|
|
11
|
-
readonly run?: LogRunRef;
|
|
12
|
-
readonly afterCursor?: string;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* One broker subscription per server/project, multiplexed to all connected
|
|
16
|
-
* clients. Each client has a bounded queue so a slow socket cannot retain an
|
|
17
|
-
* unbounded portion of the process heap.
|
|
18
|
-
*/
|
|
19
|
-
export declare class LogSubscriptionHub {
|
|
20
|
-
private readonly host;
|
|
21
|
-
private readonly clients;
|
|
22
|
-
private readonly subscriptionGenerations;
|
|
23
|
-
private startPromise;
|
|
24
|
-
private unsubscribeBroker;
|
|
25
|
-
private closed;
|
|
26
|
-
constructor(host: {
|
|
27
|
-
readonly logging: LoggingService;
|
|
28
|
-
});
|
|
29
|
-
subscribe(key: object, ws: LogSocket, filter: LogSubscriptionFilter, onSubscribed: () => void): Promise<void>;
|
|
30
|
-
unsubscribe(key: object): void;
|
|
31
|
-
private removeClient;
|
|
32
|
-
private nextSubscriptionGeneration;
|
|
33
|
-
private isCurrentSubscription;
|
|
34
|
-
close(): Promise<void>;
|
|
35
|
-
private ensureStarted;
|
|
36
|
-
private deliverLive;
|
|
37
|
-
private catchUp;
|
|
38
|
-
private enqueue;
|
|
39
|
-
private enqueueReplay;
|
|
40
|
-
private pushQueuedLine;
|
|
41
|
-
private enqueuePendingLive;
|
|
42
|
-
private scheduleFlush;
|
|
43
|
-
private flush;
|
|
44
|
-
private waitForQueueProgress;
|
|
45
|
-
private notifyQueueProgress;
|
|
46
|
-
private send;
|
|
47
|
-
private fail;
|
|
48
|
-
}
|
|
49
|
-
export {};
|
|
50
|
-
//# sourceMappingURL=log-subscription-hub.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"log-subscription-hub.d.ts","sourceRoot":"","sources":["../../../src/routes/ws/log-subscription-hub.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,WAAW,EAAiB,MAAM,YAAY,CAAA;AAEjF,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AASjE,UAAU,SAAS;IACjB,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAA;IAC9B,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;IAC9C,QAAQ,CAAC,GAAG,CAAC,EAAE,OAAO,CAAA;CACvB;AAED,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,WAAW,EAAE,CAAA;IACvC,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,QAAQ,EAAE,CAAA;IACrC,QAAQ,CAAC,GAAG,CAAC,EAAE,SAAS,CAAA;IACxB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAC9B;AAkBD;;;;GAIG;AACH,qBAAa,kBAAkB;IAOjB,OAAO,CAAC,QAAQ,CAAC,IAAI;IANjC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiC;IACzD,OAAO,CAAC,QAAQ,CAAC,uBAAuB,CAAgC;IACxE,OAAO,CAAC,YAAY,CAA6B;IACjD,OAAO,CAAC,iBAAiB,CAA4B;IACrD,OAAO,CAAC,MAAM,CAAQ;gBAEO,IAAI,EAAE;QAAE,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAA;KAAE;IAEjE,SAAS,CACb,GAAG,EAAE,MAAM,EACX,EAAE,EAAE,SAAS,EACb,MAAM,EAAE,qBAAqB,EAC7B,YAAY,EAAE,MAAM,IAAI,GACvB,OAAO,CAAC,IAAI,CAAC;IAoChB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAK9B,OAAO,CAAC,YAAY;IASpB,OAAO,CAAC,0BAA0B;IAMlC,OAAO,CAAC,qBAAqB;IAIvB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;YAOd,aAAa;IAiB3B,OAAO,CAAC,WAAW;YAUL,OAAO;IAoErB,OAAO,CAAC,OAAO;YAWD,aAAa;IAsB3B,OAAO,CAAC,cAAc;IAOtB,OAAO,CAAC,kBAAkB;IAY1B,OAAO,CAAC,aAAa;IAQrB,OAAO,CAAC,KAAK;IAiBb,OAAO,CAAC,oBAAoB;IAI5B,OAAO,CAAC,mBAAmB;IAM3B,OAAO,CAAC,IAAI;IASZ,OAAO,CAAC,IAAI;CAsBb"}
|