@sixb/server 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.
Files changed (216) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +123 -0
  3. package/dist/auth/access-token-boundary.d.ts +23 -0
  4. package/dist/auth/access-token-boundary.d.ts.map +1 -0
  5. package/dist/auth/application-access.d.ts +4 -0
  6. package/dist/auth/application-access.d.ts.map +1 -0
  7. package/dist/auth/browser-origin.d.ts +62 -0
  8. package/dist/auth/browser-origin.d.ts.map +1 -0
  9. package/dist/auth/csrf.d.ts +2 -0
  10. package/dist/auth/csrf.d.ts.map +1 -0
  11. package/dist/auth/guard.d.ts +35 -0
  12. package/dist/auth/guard.d.ts.map +1 -0
  13. package/dist/auth/public-routes.d.ts +8 -0
  14. package/dist/auth/public-routes.d.ts.map +1 -0
  15. package/dist/auth/responses.d.ts +11 -0
  16. package/dist/auth/responses.d.ts.map +1 -0
  17. package/dist/auth/return-to.d.ts +3 -0
  18. package/dist/auth/return-to.d.ts.map +1 -0
  19. package/dist/auth/scope.d.ts +43 -0
  20. package/dist/auth/scope.d.ts.map +1 -0
  21. package/dist/auth/session-activity.d.ts +4 -0
  22. package/dist/auth/session-activity.d.ts.map +1 -0
  23. package/dist/auth/session-cookies.d.ts +12 -0
  24. package/dist/auth/session-cookies.d.ts.map +1 -0
  25. package/dist/files/content.d.ts +141 -0
  26. package/dist/files/content.d.ts.map +1 -0
  27. package/dist/index.d.ts +3 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +11522 -0
  30. package/dist/index.js.map +78 -0
  31. package/dist/openapi/security.d.ts +26 -0
  32. package/dist/openapi/security.d.ts.map +1 -0
  33. package/dist/openapi/tags.d.ts +151 -0
  34. package/dist/openapi/tags.d.ts.map +1 -0
  35. package/dist/registerRoutes.d.ts +31 -0
  36. package/dist/registerRoutes.d.ts.map +1 -0
  37. package/dist/routes/action-runs.d.ts +244 -0
  38. package/dist/routes/action-runs.d.ts.map +1 -0
  39. package/dist/routes/actions.d.ts +183 -0
  40. package/dist/routes/actions.d.ts.map +1 -0
  41. package/dist/routes/agent-api-gateway.d.ts +4 -0
  42. package/dist/routes/agent-api-gateway.d.ts.map +1 -0
  43. package/dist/routes/agents.d.ts +1061 -0
  44. package/dist/routes/agents.d.ts.map +1 -0
  45. package/dist/routes/auth.d.ts +1366 -0
  46. package/dist/routes/auth.d.ts.map +1 -0
  47. package/dist/routes/connectors.d.ts +108 -0
  48. package/dist/routes/connectors.d.ts.map +1 -0
  49. package/dist/routes/datasets.d.ts +339 -0
  50. package/dist/routes/datasets.d.ts.map +1 -0
  51. package/dist/routes/events.d.ts +116 -0
  52. package/dist/routes/events.d.ts.map +1 -0
  53. package/dist/routes/files.d.ts +340 -0
  54. package/dist/routes/files.d.ts.map +1 -0
  55. package/dist/routes/links.d.ts +175 -0
  56. package/dist/routes/links.d.ts.map +1 -0
  57. package/dist/routes/logs.d.ts +88 -0
  58. package/dist/routes/logs.d.ts.map +1 -0
  59. package/dist/routes/objects.d.ts +450 -0
  60. package/dist/routes/objects.d.ts.map +1 -0
  61. package/dist/routes/ontology.d.ts +240 -0
  62. package/dist/routes/ontology.d.ts.map +1 -0
  63. package/dist/routes/pipelines.d.ts +399 -0
  64. package/dist/routes/pipelines.d.ts.map +1 -0
  65. package/dist/routes/project.d.ts +56 -0
  66. package/dist/routes/project.d.ts.map +1 -0
  67. package/dist/routes/projections.d.ts +916 -0
  68. package/dist/routes/projections.d.ts.map +1 -0
  69. package/dist/routes/rules.d.ts +165 -0
  70. package/dist/routes/rules.d.ts.map +1 -0
  71. package/dist/routes/status.d.ts +81 -0
  72. package/dist/routes/status.d.ts.map +1 -0
  73. package/dist/routes/syncs.d.ts +294 -0
  74. package/dist/routes/syncs.d.ts.map +1 -0
  75. package/dist/routes/telemetry.d.ts +241 -0
  76. package/dist/routes/telemetry.d.ts.map +1 -0
  77. package/dist/routes/webhook-runs.d.ts +89 -0
  78. package/dist/routes/webhook-runs.d.ts.map +1 -0
  79. package/dist/routes/webhooks.d.ts +60 -0
  80. package/dist/routes/webhooks.d.ts.map +1 -0
  81. package/dist/routes/workflows.d.ts +973 -0
  82. package/dist/routes/workflows.d.ts.map +1 -0
  83. package/dist/routes/ws/agents.d.ts +99 -0
  84. package/dist/routes/ws/agents.d.ts.map +1 -0
  85. package/dist/routes/ws/events.d.ts +89 -0
  86. package/dist/routes/ws/events.d.ts.map +1 -0
  87. package/dist/routes/ws/index.d.ts +31 -0
  88. package/dist/routes/ws/index.d.ts.map +1 -0
  89. package/dist/routes/ws/log-subscription-hub.d.ts +47 -0
  90. package/dist/routes/ws/log-subscription-hub.d.ts.map +1 -0
  91. package/dist/routes/ws/logs.d.ts +110 -0
  92. package/dist/routes/ws/logs.d.ts.map +1 -0
  93. package/dist/schemas/actions.d.ts +644 -0
  94. package/dist/schemas/actions.d.ts.map +1 -0
  95. package/dist/schemas/agents.d.ts +3157 -0
  96. package/dist/schemas/agents.d.ts.map +1 -0
  97. package/dist/schemas/auth.d.ts +2023 -0
  98. package/dist/schemas/auth.d.ts.map +1 -0
  99. package/dist/schemas/common.d.ts +30 -0
  100. package/dist/schemas/common.d.ts.map +1 -0
  101. package/dist/schemas/connectors.d.ts +82 -0
  102. package/dist/schemas/connectors.d.ts.map +1 -0
  103. package/dist/schemas/datasets.d.ts +744 -0
  104. package/dist/schemas/datasets.d.ts.map +1 -0
  105. package/dist/schemas/event-payloads.d.ts +477 -0
  106. package/dist/schemas/event-payloads.d.ts.map +1 -0
  107. package/dist/schemas/events.d.ts +665 -0
  108. package/dist/schemas/events.d.ts.map +1 -0
  109. package/dist/schemas/files.d.ts +241 -0
  110. package/dist/schemas/files.d.ts.map +1 -0
  111. package/dist/schemas/links.d.ts +90 -0
  112. package/dist/schemas/links.d.ts.map +1 -0
  113. package/dist/schemas/logs.d.ts +231 -0
  114. package/dist/schemas/logs.d.ts.map +1 -0
  115. package/dist/schemas/objects.d.ts +288 -0
  116. package/dist/schemas/objects.d.ts.map +1 -0
  117. package/dist/schemas/ontology.d.ts +778 -0
  118. package/dist/schemas/ontology.d.ts.map +1 -0
  119. package/dist/schemas/pipelines.d.ts +1373 -0
  120. package/dist/schemas/pipelines.d.ts.map +1 -0
  121. package/dist/schemas/project.d.ts +9 -0
  122. package/dist/schemas/project.d.ts.map +1 -0
  123. package/dist/schemas/projections.d.ts +6992 -0
  124. package/dist/schemas/projections.d.ts.map +1 -0
  125. package/dist/schemas/rules.d.ts +204 -0
  126. package/dist/schemas/rules.d.ts.map +1 -0
  127. package/dist/schemas/status.d.ts +189 -0
  128. package/dist/schemas/status.d.ts.map +1 -0
  129. package/dist/schemas/syncs.d.ts +538 -0
  130. package/dist/schemas/syncs.d.ts.map +1 -0
  131. package/dist/schemas/telemetry.d.ts +265 -0
  132. package/dist/schemas/telemetry.d.ts.map +1 -0
  133. package/dist/schemas/webhook-runs.d.ts +169 -0
  134. package/dist/schemas/webhook-runs.d.ts.map +1 -0
  135. package/dist/schemas/workflows.d.ts +1720 -0
  136. package/dist/schemas/workflows.d.ts.map +1 -0
  137. package/dist/server.d.ts +64 -0
  138. package/dist/server.d.ts.map +1 -0
  139. package/dist/tsconfig.json +5 -0
  140. package/dist/utils/http.d.ts +23 -0
  141. package/dist/utils/http.d.ts.map +1 -0
  142. package/dist/utils/request-body.d.ts +15 -0
  143. package/dist/utils/request-body.d.ts.map +1 -0
  144. package/dist/utils/ws.d.ts +10 -0
  145. package/dist/utils/ws.d.ts.map +1 -0
  146. package/package.json +68 -0
  147. package/src/auth/access-token-boundary.ts +68 -0
  148. package/src/auth/application-access.ts +16 -0
  149. package/src/auth/browser-origin.ts +367 -0
  150. package/src/auth/csrf.ts +1 -0
  151. package/src/auth/guard.ts +169 -0
  152. package/src/auth/public-routes.ts +87 -0
  153. package/src/auth/responses.ts +54 -0
  154. package/src/auth/return-to.ts +21 -0
  155. package/src/auth/scope.ts +64 -0
  156. package/src/auth/session-activity.ts +19 -0
  157. package/src/auth/session-cookies.ts +43 -0
  158. package/src/files/content.ts +419 -0
  159. package/src/index.ts +2 -0
  160. package/src/openapi/security.ts +37 -0
  161. package/src/openapi/tags.ts +151 -0
  162. package/src/registerRoutes.ts +49 -0
  163. package/src/routes/action-runs.ts +238 -0
  164. package/src/routes/actions.ts +126 -0
  165. package/src/routes/agent-api-gateway.ts +260 -0
  166. package/src/routes/agents.ts +943 -0
  167. package/src/routes/auth.ts +2233 -0
  168. package/src/routes/connectors.ts +69 -0
  169. package/src/routes/datasets.ts +402 -0
  170. package/src/routes/events.ts +55 -0
  171. package/src/routes/files.ts +675 -0
  172. package/src/routes/links.ts +143 -0
  173. package/src/routes/logs.ts +65 -0
  174. package/src/routes/objects.ts +838 -0
  175. package/src/routes/ontology.ts +138 -0
  176. package/src/routes/pipelines.ts +332 -0
  177. package/src/routes/project.ts +16 -0
  178. package/src/routes/projections.ts +251 -0
  179. package/src/routes/rules.ts +124 -0
  180. package/src/routes/status.ts +53 -0
  181. package/src/routes/syncs.ts +243 -0
  182. package/src/routes/telemetry.ts +207 -0
  183. package/src/routes/webhook-runs.ts +81 -0
  184. package/src/routes/webhooks.ts +508 -0
  185. package/src/routes/workflows.ts +1333 -0
  186. package/src/routes/ws/agents.ts +301 -0
  187. package/src/routes/ws/events.ts +278 -0
  188. package/src/routes/ws/index.ts +12 -0
  189. package/src/routes/ws/log-subscription-hub.ts +375 -0
  190. package/src/routes/ws/logs.ts +117 -0
  191. package/src/schemas/actions.ts +123 -0
  192. package/src/schemas/agents.ts +293 -0
  193. package/src/schemas/auth.ts +317 -0
  194. package/src/schemas/common.ts +60 -0
  195. package/src/schemas/connectors.ts +21 -0
  196. package/src/schemas/datasets.ts +107 -0
  197. package/src/schemas/event-payloads.ts +79 -0
  198. package/src/schemas/events.ts +117 -0
  199. package/src/schemas/files.ts +82 -0
  200. package/src/schemas/links.ts +38 -0
  201. package/src/schemas/logs.ts +76 -0
  202. package/src/schemas/objects.ts +759 -0
  203. package/src/schemas/ontology.ts +77 -0
  204. package/src/schemas/pipelines.ts +150 -0
  205. package/src/schemas/project.ts +5 -0
  206. package/src/schemas/projections.ts +144 -0
  207. package/src/schemas/rules.ts +98 -0
  208. package/src/schemas/status.ts +51 -0
  209. package/src/schemas/syncs.ts +83 -0
  210. package/src/schemas/telemetry.ts +59 -0
  211. package/src/schemas/webhook-runs.ts +40 -0
  212. package/src/schemas/workflows.ts +229 -0
  213. package/src/server.ts +383 -0
  214. package/src/utils/http.ts +108 -0
  215. package/src/utils/request-body.ts +62 -0
  216. package/src/utils/ws.ts +59 -0
@@ -0,0 +1,251 @@
1
+ import type {
2
+ LinkProjectionDefinition,
3
+ ObjectProjectionDefinition,
4
+ OntologySource,
5
+ ProjectionDefinition,
6
+ Sixb,
7
+ TelemetryProjectionDefinition,
8
+ } from "@sixb/core"
9
+ import { canViewProjection, canViewProjectionRun } from "@sixb/core/internal/authorization"
10
+ import type { ProjectionRunRecord } from "@sixb/core/storage"
11
+ import type { Elysia } from "elysia"
12
+ import { requestAuthState } from "../auth/scope"
13
+ import { OPENAPI_TAGS } from "../openapi/tags"
14
+ import { ErrorResponseSchema } from "../schemas/common"
15
+ import {
16
+ ProjectionListResponseSchema,
17
+ ProjectionParamsSchema,
18
+ ProjectionResponseSchema,
19
+ ProjectionRunListResponseSchema,
20
+ ProjectionRunParamsSchema,
21
+ ProjectionRunSchema,
22
+ ProjectionRunsQuerySchema,
23
+ } from "../schemas/projections"
24
+ import {
25
+ handleRouteError,
26
+ parseDate,
27
+ parseOptionalInt,
28
+ toIsoString,
29
+ unconfiguredStorageResponse,
30
+ } from "../utils/http"
31
+
32
+ function serializeProjectionRun(run: ProjectionRunRecord) {
33
+ return ProjectionRunSchema.parse({
34
+ id: run.id,
35
+ projectId: run.projectId,
36
+ identity: run.identity,
37
+ target: run.target,
38
+ status: run.status,
39
+ attempt: run.attempt,
40
+ progress: run.progress,
41
+ startedAt: toIsoString(run.startedAt),
42
+ finishedAt: run.finishedAt ? toIsoString(run.finishedAt) : undefined,
43
+ errorMessage: run.errorMessage,
44
+ })
45
+ }
46
+
47
+ type SerializedProjectionRun = ReturnType<typeof serializeProjectionRun>
48
+ type RequestAuthorization = ReturnType<typeof requestAuthState>["authz"]
49
+
50
+ interface ViewableProjectionCatalog {
51
+ readonly objectProjections: readonly ObjectProjectionDefinition[]
52
+ readonly linkProjections: readonly LinkProjectionDefinition[]
53
+ readonly telemetryProjections: readonly TelemetryProjectionDefinition[]
54
+ }
55
+
56
+ async function getLatestProjectionRuns(
57
+ sixb: Sixb<readonly OntologySource[]>,
58
+ projectionIds: readonly string[]
59
+ ): Promise<Map<string, SerializedProjectionRun>> {
60
+ const storage = sixb.storage.projectionRuns
61
+ if (!storage || projectionIds.length === 0) {
62
+ return new Map()
63
+ }
64
+
65
+ const result = await storage.listLatestByProjectionIds({ projectId: sixb.id, projectionIds })
66
+ return new Map(result.runs.map((run) => [run.identity.projectionId, serializeProjectionRun(run)]))
67
+ }
68
+
69
+ function listViewableProjections(
70
+ sixb: Sixb<readonly OntologySource[]>,
71
+ authz: RequestAuthorization
72
+ ): ViewableProjectionCatalog {
73
+ return {
74
+ objectProjections: sixb
75
+ .listObjectProjections()
76
+ .filter((projection) => canViewProjection(authz, projection)),
77
+ linkProjections: sixb
78
+ .listLinkProjections()
79
+ .filter((projection) => canViewProjection(authz, projection)),
80
+ telemetryProjections: sixb
81
+ .listTelemetryProjections()
82
+ .filter((projection) => canViewProjection(authz, projection)),
83
+ }
84
+ }
85
+
86
+ function projectionCatalogIds(catalog: ViewableProjectionCatalog): string[] {
87
+ return [
88
+ ...catalog.objectProjections,
89
+ ...catalog.linkProjections,
90
+ ...catalog.telemetryProjections,
91
+ ].map((projection) => projection.id)
92
+ }
93
+
94
+ function serializeProjection<TProjection extends ProjectionDefinition>(
95
+ projection: TProjection,
96
+ latestRuns: ReadonlyMap<string, SerializedProjectionRun>
97
+ ): TProjection & { latestRun: SerializedProjectionRun | null } {
98
+ return {
99
+ ...projection,
100
+ latestRun: latestRuns.get(projection.id) ?? null,
101
+ }
102
+ }
103
+
104
+ function serializeProjectionCatalog(
105
+ catalog: ViewableProjectionCatalog,
106
+ latestRuns: ReadonlyMap<string, SerializedProjectionRun>
107
+ ) {
108
+ return {
109
+ objectProjections: catalog.objectProjections.map((projection) =>
110
+ serializeProjection(projection, latestRuns)
111
+ ),
112
+ linkProjections: catalog.linkProjections.map((projection) =>
113
+ serializeProjection(projection, latestRuns)
114
+ ),
115
+ telemetryProjections: catalog.telemetryProjections.map((projection) =>
116
+ serializeProjection(projection, latestRuns)
117
+ ),
118
+ }
119
+ }
120
+
121
+ export function registerProjectionRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
122
+ return app
123
+ .get(
124
+ "/api/projections",
125
+ async (context) => {
126
+ const { authz } = requestAuthState(context)
127
+ const catalog = listViewableProjections(sixb, authz)
128
+ const latestRuns = await getLatestProjectionRuns(sixb, projectionCatalogIds(catalog))
129
+ return serializeProjectionCatalog(catalog, latestRuns)
130
+ },
131
+ {
132
+ response: { 200: ProjectionListResponseSchema },
133
+ detail: {
134
+ summary: "List all projection definitions",
135
+ tags: [OPENAPI_TAGS.projections.name],
136
+ operationId: "listProjections",
137
+ },
138
+ }
139
+ )
140
+ .get(
141
+ "/api/projections/:projectionId",
142
+ async (context) => {
143
+ const { params, set } = context
144
+ const { authz } = requestAuthState(context)
145
+ const found = sixb.getProjectionById(params.projectionId)
146
+ if (!found || !canViewProjection(authz, found)) {
147
+ set.status = 404
148
+ return { error: `Projection '${params.projectionId}' not found` }
149
+ }
150
+
151
+ const latestRuns = await getLatestProjectionRuns(sixb, [found.id])
152
+ return serializeProjection(found, latestRuns)
153
+ },
154
+ {
155
+ params: ProjectionParamsSchema,
156
+ response: { 200: ProjectionResponseSchema, 404: ErrorResponseSchema },
157
+ detail: {
158
+ summary: "Get a projection definition by id",
159
+ tags: [OPENAPI_TAGS.projections.name],
160
+ operationId: "getProjection",
161
+ },
162
+ }
163
+ )
164
+ .get(
165
+ "/api/projection-runs",
166
+ async (context) => {
167
+ const { query, set } = context
168
+ const { authz } = requestAuthState(context)
169
+ try {
170
+ const storage = sixb.storage.projectionRuns
171
+ if (!storage) {
172
+ return unconfiguredStorageResponse(set, "Projection run storage")
173
+ }
174
+
175
+ const parsed = ProjectionRunsQuerySchema.parse(query)
176
+ const result = await storage.list({
177
+ projectId: sixb.id,
178
+ projectionId: parsed.projectionId,
179
+ projectionKind: parsed.projectionKind,
180
+ datasetId: parsed.datasetId,
181
+ datasetVersionId: parsed.datasetVersionId,
182
+ objectTypeIds: authz ? [...authz.grants["view:object"]] : undefined,
183
+ statuses: parsed.status ? [parsed.status] : undefined,
184
+ startedAfter: parseDate(parsed.startedAfter),
185
+ startedBefore: parseDate(parsed.startedBefore),
186
+ limit: parseOptionalInt(parsed.limit),
187
+ offset: parseOptionalInt(parsed.offset),
188
+ order: parsed.order,
189
+ })
190
+
191
+ return {
192
+ runs: result.runs.map(serializeProjectionRun),
193
+ hasMore: result.hasMore,
194
+ total: result.total,
195
+ }
196
+ } catch (error) {
197
+ return handleRouteError(error, set)
198
+ }
199
+ },
200
+ {
201
+ query: ProjectionRunsQuerySchema,
202
+ response: {
203
+ 200: ProjectionRunListResponseSchema,
204
+ 400: ErrorResponseSchema,
205
+ 501: ErrorResponseSchema,
206
+ },
207
+ detail: {
208
+ summary: "List projection run history",
209
+ tags: [OPENAPI_TAGS.projectionRuns.name],
210
+ operationId: "listProjectionRuns",
211
+ },
212
+ }
213
+ )
214
+ .get(
215
+ "/api/projection-runs/:runId",
216
+ async (context) => {
217
+ const { params, set } = context
218
+ const { authz } = requestAuthState(context)
219
+ try {
220
+ const storage = sixb.storage.projectionRuns
221
+ if (!storage) {
222
+ return unconfiguredStorageResponse(set, "Projection run storage")
223
+ }
224
+
225
+ const run = await storage.getById({ projectId: sixb.id, id: params.runId })
226
+ if (!run || !canViewProjectionRun(authz, run.target)) {
227
+ set.status = 404
228
+ return { error: "Projection run not found" }
229
+ }
230
+
231
+ return serializeProjectionRun(run)
232
+ } catch (error) {
233
+ return handleRouteError(error, set)
234
+ }
235
+ },
236
+ {
237
+ params: ProjectionRunParamsSchema,
238
+ response: {
239
+ 200: ProjectionRunSchema,
240
+ 400: ErrorResponseSchema,
241
+ 404: ErrorResponseSchema,
242
+ 501: ErrorResponseSchema,
243
+ },
244
+ detail: {
245
+ summary: "Get a projection run by id",
246
+ tags: [OPENAPI_TAGS.projectionRuns.name],
247
+ operationId: "getProjectionRun",
248
+ },
249
+ }
250
+ )
251
+ }
@@ -0,0 +1,124 @@
1
+ import { isAllowed, type OntologySource, type RuleDefinition, type Sixb } from "@sixb/core"
2
+ import { deriveRuleEventDependencies } from "@sixb/core/internal/rules"
3
+ import type { RuleStateRecord } from "@sixb/core/storage"
4
+ import type { Elysia } from "elysia"
5
+ import { requestAuthState } from "../auth/scope"
6
+ import { OPENAPI_TAGS } from "../openapi/tags"
7
+ import { ErrorResponseSchema } from "../schemas/common"
8
+ import {
9
+ RuleParamsSchema,
10
+ RuleSchema,
11
+ RuleStateListResponseSchema,
12
+ RuleStateSchema,
13
+ RuleStatesQuerySchema,
14
+ } from "../schemas/rules"
15
+ import { handleRouteError, parseOptionalInt, unconfiguredStorageResponse } from "../utils/http"
16
+
17
+ function serializeRule(rule: RuleDefinition): ReturnType<typeof RuleSchema.parse> {
18
+ return RuleSchema.parse({
19
+ ...rule,
20
+ dependencies: deriveRuleEventDependencies(rule),
21
+ })
22
+ }
23
+
24
+ function serializeRuleState(state: RuleStateRecord): ReturnType<typeof RuleStateSchema.parse> {
25
+ return RuleStateSchema.parse(state)
26
+ }
27
+
28
+ export function registerRuleRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
29
+ return app
30
+ .get(
31
+ "/api/rules",
32
+ () => {
33
+ return sixb.listRules().map(serializeRule)
34
+ },
35
+ {
36
+ response: { 200: RuleSchema.array() },
37
+ detail: {
38
+ summary: "List registered rules",
39
+ tags: [OPENAPI_TAGS.rules.name],
40
+ operationId: "listRules",
41
+ },
42
+ }
43
+ )
44
+ .get(
45
+ "/api/rules/:ruleId",
46
+ ({ params, set }) => {
47
+ const rule = sixb.getRuleById(params.ruleId)
48
+ if (!rule) {
49
+ set.status = 404
50
+ return { error: "Rule not found" }
51
+ }
52
+
53
+ return serializeRule(rule)
54
+ },
55
+ {
56
+ params: RuleParamsSchema,
57
+ response: { 200: RuleSchema, 404: ErrorResponseSchema },
58
+ detail: {
59
+ summary: "Get rule metadata",
60
+ tags: [OPENAPI_TAGS.rules.name],
61
+ operationId: "getRule",
62
+ },
63
+ }
64
+ )
65
+ .get(
66
+ "/api/rule-states",
67
+ async (context) => {
68
+ const { query, set } = context
69
+ const { authz } = requestAuthState(context)
70
+ try {
71
+ const parsed = RuleStatesQuerySchema.parse(query)
72
+ const storage = sixb.storage.rules
73
+ if (!storage) {
74
+ return unconfiguredStorageResponse(set, "Rule state storage")
75
+ }
76
+
77
+ const objectTypeIds = authz ? [...authz.grants["view:object"]] : undefined
78
+ if (
79
+ authz &&
80
+ parsed.objectTypeId &&
81
+ !isAllowed(authz, { kind: "object.view", objectTypeId: parsed.objectTypeId })
82
+ ) {
83
+ return {
84
+ states: [],
85
+ hasMore: false,
86
+ total: 0,
87
+ }
88
+ }
89
+
90
+ const result = await storage.listActive({
91
+ projectId: sixb.id,
92
+ ruleId: parsed.ruleId,
93
+ objectTypeId: parsed.objectTypeId,
94
+ objectTypeIds,
95
+ primaryId: parsed.primaryId,
96
+ limit: parseOptionalInt(parsed.limit),
97
+ offset: parseOptionalInt(parsed.offset),
98
+ order: parsed.order,
99
+ })
100
+
101
+ return {
102
+ states: result.states.map(serializeRuleState),
103
+ hasMore: result.hasMore,
104
+ total: result.total,
105
+ }
106
+ } catch (error) {
107
+ return handleRouteError(error, set)
108
+ }
109
+ },
110
+ {
111
+ query: RuleStatesQuerySchema,
112
+ response: {
113
+ 200: RuleStateListResponseSchema,
114
+ 400: ErrorResponseSchema,
115
+ 501: ErrorResponseSchema,
116
+ },
117
+ detail: {
118
+ summary: "List active rule states",
119
+ tags: [OPENAPI_TAGS.ruleStates.name],
120
+ operationId: "listRuleStates",
121
+ },
122
+ }
123
+ )
124
+ }
@@ -0,0 +1,53 @@
1
+ import type { OntologySource, Sixb } from "@sixb/core"
2
+ import type { Elysia } from "elysia"
3
+ import { OPENAPI_TAGS } from "../openapi/tags"
4
+ import {
5
+ HealthResponseSchema,
6
+ ReadinessResponseSchema,
7
+ StatusResponseSchema,
8
+ } from "../schemas/status"
9
+ export function registerStatusRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
10
+ app.get("/health", () => ({ status: "ok" as const }), {
11
+ response: { 200: HealthResponseSchema },
12
+ detail: {
13
+ hide: true,
14
+ summary: "Check process liveness",
15
+ tags: [OPENAPI_TAGS.status.name],
16
+ operationId: "getHealth",
17
+ },
18
+ })
19
+
20
+ app.get(
21
+ "/ready",
22
+ async ({ set }) => {
23
+ const readiness = await sixb.checkReadiness()
24
+ if (readiness.status === "unready") set.status = 503
25
+ return readiness
26
+ },
27
+ {
28
+ response: { 200: ReadinessResponseSchema, 503: ReadinessResponseSchema },
29
+ detail: {
30
+ hide: true,
31
+ summary: "Check runtime readiness",
32
+ tags: [OPENAPI_TAGS.status.name],
33
+ operationId: "getReadiness",
34
+ },
35
+ }
36
+ )
37
+
38
+ return app.get(
39
+ "/api/status",
40
+ async () => ({
41
+ ...sixb.getOntologyOperationalStatus(),
42
+ objectTypes: sixb.listObjectTypes().length,
43
+ }),
44
+ {
45
+ response: { 200: StatusResponseSchema },
46
+ detail: {
47
+ summary: "Get runtime status",
48
+ tags: [OPENAPI_TAGS.status.name],
49
+ operationId: "getStatus",
50
+ },
51
+ }
52
+ )
53
+ }
@@ -0,0 +1,243 @@
1
+ import type { OntologySource, Sixb, SyncDefinition } from "@sixb/core"
2
+ import type { SyncRunRecord } from "@sixb/core/storage"
3
+ import type { Elysia } from "elysia"
4
+ import { requestAuthState } from "../auth/scope"
5
+ import { SIXB_CSRF_SECURITY_REQUIREMENT } from "../openapi/security"
6
+ import { OPENAPI_TAGS } from "../openapi/tags"
7
+ import { ErrorResponseSchema } from "../schemas/common"
8
+ import {
9
+ RequestSyncRunBodySchema,
10
+ RequestSyncRunResponseSchema,
11
+ SyncParamsSchema,
12
+ SyncRunListResponseSchema,
13
+ SyncRunsQuerySchema,
14
+ SyncSchema,
15
+ } from "../schemas/syncs"
16
+ import {
17
+ handleRouteError,
18
+ parseDate,
19
+ parseOptionalInt,
20
+ toIsoString,
21
+ unconfiguredStorageResponse,
22
+ } from "../utils/http"
23
+
24
+ function serializeSyncRun(run: SyncRunRecord) {
25
+ return {
26
+ id: run.id,
27
+ projectId: run.projectId,
28
+ syncId: run.syncId,
29
+ datasetId: run.datasetId,
30
+ mode: run.mode,
31
+ status: run.status,
32
+ startedAt: toIsoString(run.startedAt),
33
+ finishedAt: run.finishedAt ? toIsoString(run.finishedAt) : undefined,
34
+ rowsRead: run.rowsRead,
35
+ output: run.output,
36
+ expectedLatestVersionId: run.expectedLatestVersionId,
37
+ commitMessage: run.commitMessage,
38
+ error: run.error,
39
+ }
40
+ }
41
+
42
+ type SerializedSyncRun = ReturnType<typeof serializeSyncRun>
43
+
44
+ async function getLatestSyncRun(
45
+ sixb: Sixb<readonly OntologySource[]>,
46
+ syncId: string
47
+ ): Promise<SerializedSyncRun | null> {
48
+ if (!sixb.storage.syncRuns) {
49
+ return null
50
+ }
51
+
52
+ const storage = sixb.storage.syncRuns
53
+ const result = await storage.listLatestBySyncIds({
54
+ projectId: sixb.id,
55
+ syncIds: [syncId],
56
+ })
57
+
58
+ const [latest] = result.runs
59
+ return latest ? serializeSyncRun(latest) : null
60
+ }
61
+
62
+ async function getLatestSyncRuns(
63
+ sixb: Sixb<readonly OntologySource[]>,
64
+ syncIds: readonly string[]
65
+ ): Promise<Map<string, SerializedSyncRun>> {
66
+ const storage = sixb.storage.syncRuns
67
+ if (!storage || syncIds.length === 0) {
68
+ return new Map()
69
+ }
70
+
71
+ const result = await storage.listLatestBySyncIds({
72
+ projectId: sixb.id,
73
+ syncIds,
74
+ })
75
+
76
+ return new Map(result.runs.map((run) => [run.syncId, serializeSyncRun(run)]))
77
+ }
78
+
79
+ function serializeSync(
80
+ sync: SyncDefinition,
81
+ latestRun: SerializedSyncRun | null
82
+ ): ReturnType<typeof SyncSchema.parse> {
83
+ return SyncSchema.parse({
84
+ id: sync.id,
85
+ mode: sync.config.mode,
86
+ connector: {
87
+ id: sync.connector.id,
88
+ type: sync.connector.adapter.type,
89
+ },
90
+ target: {
91
+ kind: "dataset",
92
+ dataset: sync.target.dataset,
93
+ },
94
+ triggers: sync.triggers,
95
+ latestRun,
96
+ })
97
+ }
98
+
99
+ export function registerSyncRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
100
+ return app
101
+ .get(
102
+ "/api/syncs",
103
+ async (context) => {
104
+ const { scoped } = requestAuthState(context)
105
+ const syncs = scoped ? scoped.listSyncs() : sixb.listSyncs()
106
+ const latestRuns = await getLatestSyncRuns(
107
+ sixb,
108
+ syncs.map((sync) => sync.id)
109
+ )
110
+
111
+ return syncs.map((sync) => serializeSync(sync, latestRuns.get(sync.id) ?? null))
112
+ },
113
+ {
114
+ response: { 200: SyncSchema.array() },
115
+ detail: {
116
+ summary: "List registered syncs",
117
+ tags: [OPENAPI_TAGS.syncs.name],
118
+ operationId: "listSyncs",
119
+ },
120
+ }
121
+ )
122
+ .get(
123
+ "/api/syncs/:syncId",
124
+ async (context) => {
125
+ const { params, set } = context
126
+ const { scoped } = requestAuthState(context)
127
+ const sync = scoped ? scoped.getSyncById(params.syncId) : sixb.getSyncById(params.syncId)
128
+ if (!sync) {
129
+ set.status = 404
130
+ return { error: "Sync not found" }
131
+ }
132
+
133
+ return serializeSync(sync, await getLatestSyncRun(sixb, sync.id))
134
+ },
135
+ {
136
+ params: SyncParamsSchema,
137
+ response: { 200: SyncSchema, 404: ErrorResponseSchema },
138
+ detail: {
139
+ summary: "Get sync metadata",
140
+ tags: [OPENAPI_TAGS.syncs.name],
141
+ operationId: "getSync",
142
+ },
143
+ }
144
+ )
145
+ .get(
146
+ "/api/sync-runs",
147
+ async (context) => {
148
+ const { query, set } = context
149
+ const { authz } = requestAuthState(context)
150
+ try {
151
+ const parsed = SyncRunsQuerySchema.parse(query)
152
+ const storage = sixb.storage.syncRuns
153
+ if (!storage) {
154
+ return unconfiguredStorageResponse(set, "Sync run storage")
155
+ }
156
+
157
+ // Scope to runnable syncs the same way workflow run history does: pass
158
+ // the grant allowlist alongside any explicit syncId and let storage AND
159
+ // them. An ungranted syncId yields an empty intersection (no rows), and
160
+ // an empty allowlist short-circuits to no rows.
161
+ const syncIds = authz ? [...authz.grants["run:sync"]] : undefined
162
+ const result = await storage.list({
163
+ projectId: sixb.id,
164
+ syncId: parsed.syncId,
165
+ syncIds,
166
+ datasetId: parsed.datasetId,
167
+ statuses: parsed.status ? [parsed.status] : undefined,
168
+ startedAfter: parseDate(parsed.startedAfter),
169
+ startedBefore: parseDate(parsed.startedBefore),
170
+ limit: parseOptionalInt(parsed.limit),
171
+ offset: parseOptionalInt(parsed.offset),
172
+ order: parsed.order,
173
+ })
174
+
175
+ return {
176
+ runs: result.runs.map(serializeSyncRun),
177
+ hasMore: result.hasMore,
178
+ total: result.total,
179
+ }
180
+ } catch (error) {
181
+ return handleRouteError(error, set)
182
+ }
183
+ },
184
+ {
185
+ query: SyncRunsQuerySchema,
186
+ response: {
187
+ 200: SyncRunListResponseSchema,
188
+ 400: ErrorResponseSchema,
189
+ 501: ErrorResponseSchema,
190
+ },
191
+ detail: {
192
+ summary: "List sync run history",
193
+ tags: [OPENAPI_TAGS.syncRuns.name],
194
+ operationId: "listSyncRuns",
195
+ },
196
+ }
197
+ )
198
+ .post(
199
+ "/api/syncs/:syncId/runs",
200
+ async (context) => {
201
+ const { params, body, set } = context
202
+ const { scoped } = requestAuthState(context)
203
+ try {
204
+ const sync = sixb.getSyncById(params.syncId)
205
+ if (!sync) {
206
+ set.status = 404
207
+ return { error: "Sync not found" }
208
+ }
209
+
210
+ const parsedBody = RequestSyncRunBodySchema.parse(body)
211
+ const result = scoped
212
+ ? await scoped.requestSyncRun({ syncId: sync.id, ...parsedBody })
213
+ : await sixb.requestSyncRun({ syncId: sync.id, ...parsedBody })
214
+
215
+ set.status = 202
216
+ return {
217
+ runId: result.runId,
218
+ jobId: result.jobId ?? "",
219
+ syncId: result.syncId,
220
+ queuedAt: result.queuedAt,
221
+ }
222
+ } catch (error) {
223
+ return handleRouteError(error, set)
224
+ }
225
+ },
226
+ {
227
+ params: SyncParamsSchema,
228
+ body: RequestSyncRunBodySchema,
229
+ response: {
230
+ 202: RequestSyncRunResponseSchema,
231
+ 400: ErrorResponseSchema,
232
+ 403: ErrorResponseSchema,
233
+ 404: ErrorResponseSchema,
234
+ },
235
+ detail: {
236
+ summary: "Request a sync run",
237
+ tags: [OPENAPI_TAGS.syncRuns.name],
238
+ operationId: "requestSyncRun",
239
+ security: SIXB_CSRF_SECURITY_REQUIREMENT,
240
+ },
241
+ }
242
+ )
243
+ }