@sixb/server 0.1.16 → 0.1.18
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/experience.d.ts +5 -1
- package/dist/auth/experience.d.ts.map +1 -1
- package/dist/auth/public-routes.d.ts.map +1 -1
- package/dist/auth/shared-access.d.ts.map +1 -1
- package/dist/files/content.d.ts +0 -1
- package/dist/files/content.d.ts.map +1 -1
- package/dist/index.js +603 -456
- package/dist/index.js.map +34 -33
- package/dist/registerRoutes.d.ts.map +1 -1
- package/dist/routes/agents.d.ts +56 -28
- 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 +4 -3
- package/dist/routes/auth.d.ts.map +1 -1
- package/dist/routes/datasets.d.ts +4 -1
- package/dist/routes/datasets.d.ts.map +1 -1
- package/dist/routes/file-downloads.d.ts +98 -0
- package/dist/routes/file-downloads.d.ts.map +1 -0
- package/dist/routes/files.d.ts.map +1 -1
- package/dist/routes/models.d.ts +1 -0
- package/dist/routes/models.d.ts.map +1 -1
- package/dist/routes/objects.d.ts +24 -4
- package/dist/routes/objects.d.ts.map +1 -1
- package/dist/routes/ontology.d.ts +4 -4
- package/dist/routes/project.d.ts +2 -0
- package/dist/routes/project.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 +6 -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 +71 -0
- package/dist/routes/ws/subscription-hub.d.ts.map +1 -0
- package/dist/schemas/agents.d.ts +255 -123
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-accounting.d.ts +234 -110
- 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 +9 -2
- package/dist/schemas/auth.d.ts.map +1 -1
- package/dist/schemas/common.d.ts +8 -0
- package/dist/schemas/common.d.ts.map +1 -1
- package/dist/schemas/datasets.d.ts +18 -3
- package/dist/schemas/datasets.d.ts.map +1 -1
- package/dist/schemas/event-payloads.d.ts +39 -0
- package/dist/schemas/event-payloads.d.ts.map +1 -1
- package/dist/schemas/files.d.ts +1 -1
- package/dist/schemas/files.d.ts.map +1 -1
- package/dist/schemas/models.d.ts +10 -0
- package/dist/schemas/models.d.ts.map +1 -1
- package/dist/schemas/objects.d.ts +32 -0
- package/dist/schemas/objects.d.ts.map +1 -1
- package/dist/schemas/ontology.d.ts +18 -18
- package/dist/schemas/project.d.ts +8 -0
- package/dist/schemas/project.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 +52 -0
- package/dist/schemas/workflows.d.ts.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/package.json +6 -5
- package/src/auth/experience.ts +20 -2
- package/src/auth/public-routes.ts +10 -0
- package/src/auth/shared-access.ts +48 -6
- package/src/files/content.ts +43 -88
- package/src/registerRoutes.ts +2 -0
- package/src/routes/agents.ts +29 -9
- package/src/routes/ai-accounting.ts +1 -0
- package/src/routes/auth.ts +84 -19
- package/src/routes/datasets.ts +12 -43
- package/src/routes/file-downloads.ts +48 -0
- package/src/routes/files.ts +3 -1
- package/src/routes/models.ts +3 -0
- package/src/routes/objects.ts +25 -5
- package/src/routes/project.ts +2 -1
- package/src/routes/workflows.ts +5 -0
- package/src/routes/ws/events.ts +98 -133
- package/src/routes/ws/logs.ts +67 -5
- package/src/routes/ws/subscription-hub.ts +394 -0
- package/src/schemas/agents.ts +28 -3
- package/src/schemas/ai-accounting.ts +5 -0
- package/src/schemas/ai-usage.ts +3 -0
- package/src/schemas/auth.ts +5 -0
- package/src/schemas/common.ts +3 -0
- package/src/schemas/datasets.ts +23 -1
- package/src/schemas/event-payloads.ts +3 -1
- package/src/schemas/files.ts +7 -1
- package/src/schemas/models.ts +2 -0
- package/src/schemas/objects.ts +41 -2
- package/src/schemas/project.ts +4 -0
- package/src/schemas/status.ts +0 -6
- package/src/server.ts +7 -1
- 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/log-subscription-hub.ts +0 -369
package/src/routes/files.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
SYSTEM_PRINCIPAL,
|
|
7
7
|
} from "@sixb/core"
|
|
8
8
|
import { computeBlobDigest, supportsDirectUpload } from "@sixb/core/blob-storage/server"
|
|
9
|
+
import { canonicalMediaType } from "@sixb/core/internal/blob-storage"
|
|
9
10
|
import { createFileUploadId, createUploadExpiresAt } from "@sixb/core/internal/storage"
|
|
10
11
|
import { type FileUploadSession, FileUploadSessionError } from "@sixb/core/storage"
|
|
11
12
|
import type { Elysia } from "elysia"
|
|
@@ -72,7 +73,8 @@ export function registerFileRoutes(app: Elysia, host: SixbHostView) {
|
|
|
72
73
|
const fileRef = await host.blobStorage.put({
|
|
73
74
|
body: file,
|
|
74
75
|
fileName: file.name || undefined,
|
|
75
|
-
|
|
76
|
+
// Bun derives the part's type from the file name; keep it only if it is well-formed.
|
|
77
|
+
mediaType: canonicalMediaType(file.type) ?? undefined,
|
|
76
78
|
logicalPath: logicalPath ?? undefined,
|
|
77
79
|
})
|
|
78
80
|
|
package/src/routes/models.ts
CHANGED
|
@@ -10,6 +10,9 @@ async function serializeLanguageModel(entry: LanguageModelEntry, defaultEntry: L
|
|
|
10
10
|
provider: entry.provider,
|
|
11
11
|
modelId: entry.modelId,
|
|
12
12
|
isDefault: entry.provider === defaultEntry.provider && entry.modelId === defaultEntry.modelId,
|
|
13
|
+
...(entry.model.defaultReasoning === undefined
|
|
14
|
+
? {}
|
|
15
|
+
: { defaultReasoning: entry.model.defaultReasoning }),
|
|
13
16
|
}
|
|
14
17
|
}
|
|
15
18
|
|
package/src/routes/objects.ts
CHANGED
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
ExpandedObjectRow,
|
|
13
13
|
ObjectRow,
|
|
14
14
|
ObjectRowLinks,
|
|
15
|
+
Redactions,
|
|
15
16
|
} from "@sixb/core/storage"
|
|
16
17
|
import type { Elysia } from "elysia"
|
|
17
18
|
import { ZodError, z } from "zod"
|
|
@@ -41,7 +42,16 @@ import {
|
|
|
41
42
|
} from "../schemas/objects"
|
|
42
43
|
import { handleRouteError, parseOptionalInt, toIsoString } from "../utils/http"
|
|
43
44
|
|
|
44
|
-
|
|
45
|
+
// Only object files are served to shared sessions. The boundary reads `shareGrant` before routing;
|
|
46
|
+
// declaring it here keeps it in the OpenAPI contract and through query validation.
|
|
47
|
+
const ObjectFileContentRouteQuerySchema = FileContentQuerySchema.extend({
|
|
48
|
+
shareGrant: z
|
|
49
|
+
.string()
|
|
50
|
+
.optional()
|
|
51
|
+
.describe("Share grant selector for elements that cannot send the x-sixb-share-grant header"),
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const ObjectFileContentQuerySchema = ObjectFileContentRouteQuerySchema.extend({
|
|
45
55
|
path: z
|
|
46
56
|
.string()
|
|
47
57
|
.min(1)
|
|
@@ -53,6 +63,7 @@ function serializeObject(row: {
|
|
|
53
63
|
primaryId: string
|
|
54
64
|
objectTypeId: string
|
|
55
65
|
properties: Record<string, unknown>
|
|
66
|
+
redactions?: Redactions
|
|
56
67
|
createdAt: Date
|
|
57
68
|
updatedAt: Date
|
|
58
69
|
}) {
|
|
@@ -61,6 +72,7 @@ function serializeObject(row: {
|
|
|
61
72
|
primaryId: row.primaryId,
|
|
62
73
|
objectTypeId: row.objectTypeId,
|
|
63
74
|
properties: row.properties,
|
|
75
|
+
...(row.redactions === undefined ? {} : { redactions: row.redactions }),
|
|
64
76
|
createdAt: toIsoString(row.createdAt),
|
|
65
77
|
updatedAt: toIsoString(row.updatedAt),
|
|
66
78
|
}
|
|
@@ -243,8 +255,15 @@ async function searchObjects(
|
|
|
243
255
|
input: { kind: "start", objectTypeId: objectType.id },
|
|
244
256
|
},
|
|
245
257
|
}
|
|
246
|
-
|
|
247
|
-
|
|
258
|
+
try {
|
|
259
|
+
const result = await sixb.objects.executeQuery({ query: objectQuery })
|
|
260
|
+
return result.objects
|
|
261
|
+
} catch (error) {
|
|
262
|
+
// Default text fields the caller cannot read are skipped; a type left with none denies
|
|
263
|
+
// its search. Global search covers what the caller can read, so it matches nothing there.
|
|
264
|
+
if (error instanceof AuthorizationError) return []
|
|
265
|
+
throw error
|
|
266
|
+
}
|
|
248
267
|
})
|
|
249
268
|
)
|
|
250
269
|
|
|
@@ -745,6 +764,7 @@ export function registerObjectRoutes(app: Elysia, host: SixbHostView) {
|
|
|
745
764
|
})
|
|
746
765
|
|
|
747
766
|
return {
|
|
767
|
+
total: result.total,
|
|
748
768
|
facets: result.facets,
|
|
749
769
|
plan: serializePlan(result.plan),
|
|
750
770
|
}
|
|
@@ -812,7 +832,7 @@ export function registerObjectRoutes(app: Elysia, host: SixbHostView) {
|
|
|
812
832
|
// the handler applies the stricter `^/properties` check and returns a
|
|
813
833
|
// consistent `{ error }` 400. Registering the strict schema here would
|
|
814
834
|
// make Elysia emit its own 422 validation body, breaking that contract.
|
|
815
|
-
query:
|
|
835
|
+
query: ObjectFileContentRouteQuerySchema,
|
|
816
836
|
// No top-level `response` map: Elysia's OpenAPI builder resets responses
|
|
817
837
|
// to JSON-only whenever one is present, which would erase the binary
|
|
818
838
|
// `application/octet-stream` bodies. Declaring every status in
|
|
@@ -833,7 +853,7 @@ export function registerObjectRoutes(app: Elysia, host: SixbHostView) {
|
|
|
833
853
|
params: ObjectParamsSchema,
|
|
834
854
|
// See the GET route: register the loose query so the handler owns the
|
|
835
855
|
// strict `^/properties` check and its consistent 400.
|
|
836
|
-
query:
|
|
856
|
+
query: ObjectFileContentRouteQuerySchema,
|
|
837
857
|
// See the GET route: no top-level `response` map, so the binary responses
|
|
838
858
|
// declared in `detail.responses` survive into the spec.
|
|
839
859
|
detail: {
|
package/src/routes/project.ts
CHANGED
|
@@ -4,7 +4,8 @@ import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
|
|
|
4
4
|
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
5
5
|
import { ProjectInfoResponseSchema } from "../schemas/project"
|
|
6
6
|
export function registerProjectRoutes(app: Elysia, host: SixbHostView) {
|
|
7
|
-
|
|
7
|
+
const project = { id: host.id, locale: host.locale, timeZone: host.timeZone }
|
|
8
|
+
return app.get("/api/project", async () => project, {
|
|
8
9
|
response: { 200: ProjectInfoResponseSchema },
|
|
9
10
|
detail: {
|
|
10
11
|
summary: "Get current project metadata",
|
package/src/routes/workflows.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
} from "@sixb/core"
|
|
9
9
|
import { publishAgentRunCancel } from "@sixb/core/internal/agents"
|
|
10
10
|
import { createSixbError, toSixbFailure } from "@sixb/core/internal/errors"
|
|
11
|
+
import { assertValidFileRefs } from "@sixb/core/internal/ontology"
|
|
11
12
|
import type { Sixb, WorkflowRunView } from "@sixb/core/internal/request-execution"
|
|
12
13
|
import {
|
|
13
14
|
snapshotWorkflowInterventionResponse,
|
|
@@ -731,6 +732,10 @@ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
|
|
|
731
732
|
value: parsedBody.response,
|
|
732
733
|
valueTypesById: host.definitions.ontology.getValueTypesById(),
|
|
733
734
|
})
|
|
735
|
+
assertValidFileRefs(
|
|
736
|
+
parsedBody.response,
|
|
737
|
+
`Workflow "${workflow.id}" intervention "${node.intervention.id}" response`
|
|
738
|
+
)
|
|
734
739
|
|
|
735
740
|
const submitted = await storage.submit({
|
|
736
741
|
projectId: host.id,
|
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 `view` 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,74 @@ 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
|
+
// The execution facade owns per-event visibility: it withholds events the caller
|
|
160
|
+
// cannot view and omits marked properties the caller is not cleared for.
|
|
161
|
+
view: (event) => sixb.events.readable(event),
|
|
162
|
+
replay: replayFrom
|
|
163
|
+
? {
|
|
164
|
+
afterCursor: replayFrom.afterCursor,
|
|
165
|
+
read: async (afterCursor) => {
|
|
166
|
+
const records = await events.read({
|
|
167
|
+
afterCursor,
|
|
168
|
+
limit,
|
|
169
|
+
topics,
|
|
170
|
+
types: filter.types,
|
|
171
|
+
})
|
|
172
|
+
return {
|
|
173
|
+
records,
|
|
174
|
+
cursor: records.at(-1)?.cursor,
|
|
175
|
+
hasMore: records.length === limit,
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
}
|
|
179
|
+
: undefined,
|
|
180
|
+
},
|
|
181
|
+
() =>
|
|
182
|
+
safeSend(ws, {
|
|
183
|
+
type: "subscribed",
|
|
184
|
+
topic: filter.topic ?? null,
|
|
185
|
+
types: filter.types ?? null,
|
|
186
|
+
afterCursor: replayFrom?.afterCursor ?? null,
|
|
187
|
+
})
|
|
188
|
+
)
|
|
189
|
+
} catch (error) {
|
|
190
|
+
safeSend(ws, {
|
|
191
|
+
type: "error",
|
|
192
|
+
message: error instanceof Error ? error.message : String(error),
|
|
193
|
+
})
|
|
194
|
+
ws.close(1011, "Event stream setup failed")
|
|
195
|
+
}
|
|
239
196
|
},
|
|
240
197
|
|
|
241
198
|
close(ws) {
|
|
242
|
-
|
|
199
|
+
hub.unsubscribe(wsStateKey(ws))
|
|
243
200
|
states.delete(wsStateKey(ws))
|
|
244
201
|
},
|
|
245
202
|
})
|
|
@@ -250,7 +207,15 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
|
|
|
250
207
|
* `scopeKeysForEvent` (the same extraction the client predicate uses), so events
|
|
251
208
|
* without those keys (e.g. workflows) never match a scoped subscription.
|
|
252
209
|
*/
|
|
253
|
-
function eventMatchesScope(
|
|
210
|
+
function eventMatchesScope(
|
|
211
|
+
event: DomainEvent,
|
|
212
|
+
filter: {
|
|
213
|
+
readonly objectTypeId?: string
|
|
214
|
+
readonly primaryId?: string
|
|
215
|
+
readonly actionId?: string
|
|
216
|
+
readonly runId?: string
|
|
217
|
+
}
|
|
218
|
+
): boolean {
|
|
254
219
|
const { objectTypeId, primaryId, actionId, runId } = filter
|
|
255
220
|
if (
|
|
256
221
|
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
|
+
}
|