@sixb/server 0.1.16 → 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 +406 -327
- package/dist/index.js.map +17 -16
- 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 +4 -4
- 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 +18 -18
- 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/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/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
package/src/routes/ws/events.ts
CHANGED
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
import type { DomainEvent } from "@sixb/core"
|
|
2
2
|
import { scopeKeysForEvent } from "@sixb/core/internal/event-scope"
|
|
3
|
+
import type { StoredDomainEvent } from "@sixb/core/internal/events"
|
|
3
4
|
import type { Elysia } from "elysia"
|
|
4
5
|
import { z } from "zod"
|
|
5
6
|
import { EVENT_TOPICS, EVENT_TYPES } from "../../schemas/events"
|
|
6
7
|
import type { SixbServer } from "../../server"
|
|
7
8
|
import { decodeWsMessage, safeSend, wsRequestSixb, wsStateKey } from "../../utils/ws"
|
|
9
|
+
import { SubscriptionHub } from "./subscription-hub"
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
actionId?: string
|
|
15
|
-
runId?: string
|
|
16
|
-
afterCursor?: string
|
|
17
|
-
limit: number
|
|
18
|
-
polling: boolean
|
|
19
|
-
timer: ReturnType<typeof setInterval> | null
|
|
11
|
+
const DEFAULT_READ_LIMIT = 200
|
|
12
|
+
|
|
13
|
+
interface EventSocketState {
|
|
14
|
+
/** The first subscription replays from where the stream ended when the socket opened. */
|
|
15
|
+
replayFrom: { readonly afterCursor?: string } | null
|
|
20
16
|
initialized: Promise<void>
|
|
21
17
|
initializationFailureHandled: boolean
|
|
22
18
|
}
|
|
@@ -63,33 +59,21 @@ export function parseSubscriptionMessage(payload: unknown):
|
|
|
63
59
|
return { ok: true, data: parsed.data }
|
|
64
60
|
}
|
|
65
61
|
|
|
66
|
-
async function resolveLatestCursor(server: SixbServer): Promise<string | undefined> {
|
|
67
|
-
return server.getHost().events.latestCursor()
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function createDefaultState(): EventSubscriptionState {
|
|
71
|
-
return {
|
|
72
|
-
topics: undefined,
|
|
73
|
-
types: undefined,
|
|
74
|
-
objectTypeId: undefined,
|
|
75
|
-
primaryId: undefined,
|
|
76
|
-
actionId: undefined,
|
|
77
|
-
runId: undefined,
|
|
78
|
-
afterCursor: undefined,
|
|
79
|
-
limit: 200,
|
|
80
|
-
polling: false,
|
|
81
|
-
timer: null,
|
|
82
|
-
initialized: Promise.resolve(),
|
|
83
|
-
initializationFailureHandled: false,
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
62
|
export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
|
|
88
|
-
const
|
|
63
|
+
const events = server.getHost().events
|
|
64
|
+
const states = new WeakMap<object, EventSocketState>()
|
|
65
|
+
const hub = new SubscriptionHub<StoredDomainEvent>({
|
|
66
|
+
subscribe: (deliver) => events.subscribe({ from: "latest" }, deliver),
|
|
67
|
+
frames: (batch) => batch.map((event) => ({ type: "event", event })),
|
|
68
|
+
expired: async () => ({
|
|
69
|
+
type: "error",
|
|
70
|
+
message: "[SixbServer] Event cursor expired; refetch current state. Live events continue.",
|
|
71
|
+
}),
|
|
72
|
+
})
|
|
89
73
|
|
|
90
74
|
const awaitInitialization = async (
|
|
91
75
|
ws: { close: () => void; send: (message: string) => void },
|
|
92
|
-
state:
|
|
76
|
+
state: EventSocketState
|
|
93
77
|
): Promise<boolean> => {
|
|
94
78
|
try {
|
|
95
79
|
await state.initialized
|
|
@@ -107,83 +91,19 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
|
|
|
107
91
|
}
|
|
108
92
|
}
|
|
109
93
|
|
|
110
|
-
|
|
111
|
-
const state = states.get(wsStateKey(ws))
|
|
112
|
-
if (!state) {
|
|
113
|
-
return
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (state.timer) {
|
|
117
|
-
clearInterval(state.timer)
|
|
118
|
-
state.timer = null
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const startPolling = (ws: { send: (message: string) => void }) => {
|
|
123
|
-
const state = states.get(wsStateKey(ws))
|
|
124
|
-
if (!state || state.timer) {
|
|
125
|
-
return
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const sixb = wsRequestSixb(ws)
|
|
129
|
-
if (!sixb) {
|
|
130
|
-
safeSend(ws, { type: "error", message: "Execution scope is not available." })
|
|
131
|
-
return
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const tick = async () => {
|
|
135
|
-
if (state.polling) {
|
|
136
|
-
return
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
state.polling = true
|
|
140
|
-
try {
|
|
141
|
-
const p = server.getHost()
|
|
142
|
-
const events = await p.events.read({
|
|
143
|
-
afterCursor: state.afterCursor,
|
|
144
|
-
limit: state.limit,
|
|
145
|
-
topics: state.topics,
|
|
146
|
-
types: state.types,
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
// The execution facade owns per-event visibility. The cursor still advances over every
|
|
150
|
-
// record read, not only the visible ones, so an invisible tail cannot stall polling.
|
|
151
|
-
for (const event of events) {
|
|
152
|
-
if (!eventMatchesScope(event, state)) {
|
|
153
|
-
continue
|
|
154
|
-
}
|
|
155
|
-
if (sixb.events.canRead(event)) {
|
|
156
|
-
safeSend(ws, { type: "event", event })
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const last = events[events.length - 1]
|
|
161
|
-
if (last) {
|
|
162
|
-
state.afterCursor = last.cursor
|
|
163
|
-
}
|
|
164
|
-
} catch (error) {
|
|
165
|
-
const message = error instanceof Error ? error.message : String(error)
|
|
166
|
-
safeSend(ws, { type: "error", message })
|
|
167
|
-
} finally {
|
|
168
|
-
state.polling = false
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
state.timer = setInterval(() => {
|
|
173
|
-
void tick()
|
|
174
|
-
}, 500)
|
|
175
|
-
|
|
176
|
-
void tick()
|
|
177
|
-
}
|
|
178
|
-
|
|
94
|
+
app.onStop(() => hub.close())
|
|
179
95
|
return app.ws("/ws/events", {
|
|
180
96
|
async open(ws) {
|
|
181
97
|
// Any authenticated principal may connect; events are filtered per-event
|
|
182
|
-
// by grants as they stream (see
|
|
183
|
-
const state =
|
|
98
|
+
// by grants as they stream (see `matches` below).
|
|
99
|
+
const state: EventSocketState = {
|
|
100
|
+
replayFrom: null,
|
|
101
|
+
initialized: Promise.resolve(),
|
|
102
|
+
initializationFailureHandled: false,
|
|
103
|
+
}
|
|
184
104
|
states.set(wsStateKey(ws), state)
|
|
185
|
-
state.initialized =
|
|
186
|
-
state.
|
|
105
|
+
state.initialized = events.latestCursor().then((afterCursor) => {
|
|
106
|
+
state.replayFrom = { afterCursor }
|
|
187
107
|
})
|
|
188
108
|
if (!(await awaitInitialization(ws, state))) return
|
|
189
109
|
safeSend(ws, { type: "connected", channel: "events" })
|
|
@@ -197,7 +117,8 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
|
|
|
197
117
|
return
|
|
198
118
|
}
|
|
199
119
|
|
|
200
|
-
const
|
|
120
|
+
const key = wsStateKey(ws)
|
|
121
|
+
const state = states.get(key)
|
|
201
122
|
if (!state) {
|
|
202
123
|
safeSend(ws, { type: "error", message: "Subscription state not found." })
|
|
203
124
|
return
|
|
@@ -208,38 +129,72 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
|
|
|
208
129
|
if (!(await awaitInitialization(ws, state))) return
|
|
209
130
|
|
|
210
131
|
if (parsed.data.type === "unsubscribe") {
|
|
211
|
-
|
|
212
|
-
state.topics = undefined
|
|
213
|
-
state.types = undefined
|
|
214
|
-
state.objectTypeId = undefined
|
|
215
|
-
state.primaryId = undefined
|
|
216
|
-
state.actionId = undefined
|
|
217
|
-
state.runId = undefined
|
|
218
|
-
state.afterCursor = await resolveLatestCursor(server)
|
|
132
|
+
hub.unsubscribe(key)
|
|
219
133
|
safeSend(ws, { type: "unsubscribed" })
|
|
220
134
|
return
|
|
221
135
|
}
|
|
222
136
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
state.runId = parsed.data.runId
|
|
229
|
-
state.limit = parsed.data.limit ?? state.limit
|
|
230
|
-
state.afterCursor = parsed.data.afterCursor ?? state.afterCursor
|
|
137
|
+
const sixb = wsRequestSixb(ws)
|
|
138
|
+
if (!sixb) {
|
|
139
|
+
safeSend(ws, { type: "error", message: "Execution scope is not available." })
|
|
140
|
+
return
|
|
141
|
+
}
|
|
231
142
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
afterCursor
|
|
238
|
-
|
|
143
|
+
const filter = parsed.data
|
|
144
|
+
const topics = filter.topic ? [filter.topic] : undefined
|
|
145
|
+
const limit = filter.limit ?? DEFAULT_READ_LIMIT
|
|
146
|
+
// Without a cursor, a later subscription on the same socket starts with live events.
|
|
147
|
+
const replayFrom =
|
|
148
|
+
filter.afterCursor === undefined ? state.replayFrom : { afterCursor: filter.afterCursor }
|
|
149
|
+
state.replayFrom = null
|
|
150
|
+
try {
|
|
151
|
+
await hub.subscribe(
|
|
152
|
+
key,
|
|
153
|
+
ws,
|
|
154
|
+
{
|
|
155
|
+
matches: (event) =>
|
|
156
|
+
(!filter.topic || event.topic === filter.topic) &&
|
|
157
|
+
(!filter.types?.length || filter.types.includes(event.type)) &&
|
|
158
|
+
eventMatchesScope(event, filter) &&
|
|
159
|
+
sixb.events.canRead(event),
|
|
160
|
+
replay: replayFrom
|
|
161
|
+
? {
|
|
162
|
+
afterCursor: replayFrom.afterCursor,
|
|
163
|
+
read: async (afterCursor) => {
|
|
164
|
+
const records = await events.read({
|
|
165
|
+
afterCursor,
|
|
166
|
+
limit,
|
|
167
|
+
topics,
|
|
168
|
+
types: filter.types,
|
|
169
|
+
})
|
|
170
|
+
return {
|
|
171
|
+
records,
|
|
172
|
+
cursor: records.at(-1)?.cursor,
|
|
173
|
+
hasMore: records.length === limit,
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
}
|
|
177
|
+
: undefined,
|
|
178
|
+
},
|
|
179
|
+
() =>
|
|
180
|
+
safeSend(ws, {
|
|
181
|
+
type: "subscribed",
|
|
182
|
+
topic: filter.topic ?? null,
|
|
183
|
+
types: filter.types ?? null,
|
|
184
|
+
afterCursor: replayFrom?.afterCursor ?? null,
|
|
185
|
+
})
|
|
186
|
+
)
|
|
187
|
+
} catch (error) {
|
|
188
|
+
safeSend(ws, {
|
|
189
|
+
type: "error",
|
|
190
|
+
message: error instanceof Error ? error.message : String(error),
|
|
191
|
+
})
|
|
192
|
+
ws.close(1011, "Event stream setup failed")
|
|
193
|
+
}
|
|
239
194
|
},
|
|
240
195
|
|
|
241
196
|
close(ws) {
|
|
242
|
-
|
|
197
|
+
hub.unsubscribe(wsStateKey(ws))
|
|
243
198
|
states.delete(wsStateKey(ws))
|
|
244
199
|
},
|
|
245
200
|
})
|
|
@@ -250,7 +205,15 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
|
|
|
250
205
|
* `scopeKeysForEvent` (the same extraction the client predicate uses), so events
|
|
251
206
|
* without those keys (e.g. workflows) never match a scoped subscription.
|
|
252
207
|
*/
|
|
253
|
-
function eventMatchesScope(
|
|
208
|
+
function eventMatchesScope(
|
|
209
|
+
event: DomainEvent,
|
|
210
|
+
filter: {
|
|
211
|
+
readonly objectTypeId?: string
|
|
212
|
+
readonly primaryId?: string
|
|
213
|
+
readonly actionId?: string
|
|
214
|
+
readonly runId?: string
|
|
215
|
+
}
|
|
216
|
+
): boolean {
|
|
254
217
|
const { objectTypeId, primaryId, actionId, runId } = filter
|
|
255
218
|
if (
|
|
256
219
|
objectTypeId === undefined &&
|
package/src/routes/ws/logs.ts
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
|
-
import type { LogLevel } from "@sixb/core"
|
|
1
|
+
import type { LogLevel, LogRunRef, SixbRunKind, StoredLogLine } from "@sixb/core"
|
|
2
|
+
import type { LoggingService } from "@sixb/core/internal/logging"
|
|
2
3
|
import type { Elysia } from "elysia"
|
|
3
4
|
import { z } from "zod"
|
|
4
5
|
import { LOG_LEVELS, LogRunIdSchema, SIXB_RUN_KINDS } from "../../schemas/logs"
|
|
5
6
|
import type { SixbServer } from "../../server"
|
|
6
7
|
import { decodeWsMessage, safeSend, wsRequestSixb, wsStateKey } from "../../utils/ws"
|
|
7
|
-
import {
|
|
8
|
+
import { type HubSubscription, SubscriptionHub } from "./subscription-hub"
|
|
9
|
+
|
|
10
|
+
const READ_PAGE_SIZE = 500
|
|
11
|
+
|
|
12
|
+
export interface LogSubscriptionFilter {
|
|
13
|
+
readonly kinds?: readonly SixbRunKind[]
|
|
14
|
+
readonly levels?: readonly LogLevel[]
|
|
15
|
+
readonly run?: LogRunRef
|
|
16
|
+
readonly afterCursor?: string
|
|
17
|
+
}
|
|
8
18
|
|
|
9
19
|
const SubscribeSchema = z
|
|
10
20
|
.object({
|
|
@@ -48,8 +58,49 @@ export function parseLogSubscriptionMessage(
|
|
|
48
58
|
return { ok: true, data: parsed.data }
|
|
49
59
|
}
|
|
50
60
|
|
|
61
|
+
export function createLogSubscriptionHub(logging: LoggingService): SubscriptionHub<StoredLogLine> {
|
|
62
|
+
return new SubscriptionHub({
|
|
63
|
+
subscribe: (deliver) => logging.subscribe({ from: "latest" }, deliver),
|
|
64
|
+
frames: (logs) => [{ type: "logs", logs }],
|
|
65
|
+
expired: async () => {
|
|
66
|
+
const latest = await logging.tail({ limit: 1 })
|
|
67
|
+
return {
|
|
68
|
+
type: "reset",
|
|
69
|
+
reason: "cursor_expired",
|
|
70
|
+
cursor: latest.lines.at(-1)?.cursor ?? latest.cursor,
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function logSubscription(
|
|
77
|
+
logging: LoggingService,
|
|
78
|
+
filter: LogSubscriptionFilter
|
|
79
|
+
): HubSubscription<StoredLogLine> {
|
|
80
|
+
return {
|
|
81
|
+
matches: (line) => matchesLogFilter(line, filter),
|
|
82
|
+
replay:
|
|
83
|
+
filter.afterCursor === undefined
|
|
84
|
+
? undefined
|
|
85
|
+
: {
|
|
86
|
+
afterCursor: filter.afterCursor,
|
|
87
|
+
read: async (afterCursor) => {
|
|
88
|
+
const page = await logging.read({
|
|
89
|
+
afterCursor,
|
|
90
|
+
limit: READ_PAGE_SIZE,
|
|
91
|
+
kinds: filter.kinds,
|
|
92
|
+
levels: filter.levels,
|
|
93
|
+
run: filter.run,
|
|
94
|
+
})
|
|
95
|
+
return { records: page.lines, cursor: page.cursor, hasMore: page.hasMore }
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
51
101
|
export function registerLogStreamRoutes(app: Elysia, server: SixbServer) {
|
|
52
|
-
const
|
|
102
|
+
const logging = server.getHost().logging
|
|
103
|
+
const hub = createLogSubscriptionHub(logging)
|
|
53
104
|
|
|
54
105
|
app.onStop(() => hub.close())
|
|
55
106
|
return app.ws("/ws/logs", {
|
|
@@ -94,12 +145,12 @@ export function registerLogStreamRoutes(app: Elysia, server: SixbServer) {
|
|
|
94
145
|
await hub.subscribe(
|
|
95
146
|
key,
|
|
96
147
|
ws,
|
|
97
|
-
{
|
|
148
|
+
logSubscription(logging, {
|
|
98
149
|
kinds: subscription.kinds,
|
|
99
150
|
levels,
|
|
100
151
|
run: subscription.run,
|
|
101
152
|
afterCursor: subscription.afterCursor,
|
|
102
|
-
},
|
|
153
|
+
}),
|
|
103
154
|
() =>
|
|
104
155
|
safeSend(ws, {
|
|
105
156
|
type: "subscribed",
|
|
@@ -123,3 +174,14 @@ export function registerLogStreamRoutes(app: Elysia, server: SixbServer) {
|
|
|
123
174
|
},
|
|
124
175
|
})
|
|
125
176
|
}
|
|
177
|
+
|
|
178
|
+
function matchesLogFilter(line: StoredLogLine, filter: LogSubscriptionFilter): boolean {
|
|
179
|
+
if (filter.run) {
|
|
180
|
+
if (line.context.run.kind !== filter.run.kind || line.context.run.id !== filter.run.id) {
|
|
181
|
+
return false
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (filter.kinds && !filter.kinds.includes(line.context.run.kind)) return false
|
|
185
|
+
if (filter.levels && !filter.levels.includes(line.level)) return false
|
|
186
|
+
return true
|
|
187
|
+
}
|