@sixb/server 0.1.12 → 0.1.13

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 (85) hide show
  1. package/README.md +5 -4
  2. package/dist/auth/access-token-boundary.d.ts +13 -9
  3. package/dist/auth/access-token-boundary.d.ts.map +1 -1
  4. package/dist/auth/client-address.d.ts +12 -0
  5. package/dist/auth/client-address.d.ts.map +1 -0
  6. package/dist/auth/guard.d.ts.map +1 -1
  7. package/dist/auth/public-routes.d.ts +5 -0
  8. package/dist/auth/public-routes.d.ts.map +1 -1
  9. package/dist/auth/rate-limit.d.ts +15 -0
  10. package/dist/auth/rate-limit.d.ts.map +1 -0
  11. package/dist/auth/scope.d.ts +8 -3
  12. package/dist/auth/scope.d.ts.map +1 -1
  13. package/dist/auth/shared-access.d.ts.map +1 -1
  14. package/dist/index.js +578 -455
  15. package/dist/index.js.map +40 -38
  16. package/dist/openapi/security.d.ts +34 -10
  17. package/dist/openapi/security.d.ts.map +1 -1
  18. package/dist/routes/auth.d.ts +64 -17
  19. package/dist/routes/auth.d.ts.map +1 -1
  20. package/dist/routes/connectors.d.ts.map +1 -1
  21. package/dist/routes/events.d.ts +0 -4
  22. package/dist/routes/events.d.ts.map +1 -1
  23. package/dist/routes/files.d.ts +15 -2
  24. package/dist/routes/files.d.ts.map +1 -1
  25. package/dist/routes/models.d.ts +7 -7
  26. package/dist/routes/ontology.d.ts +13 -1
  27. package/dist/routes/ontology.d.ts.map +1 -1
  28. package/dist/routes/shared-access.d.ts.map +1 -1
  29. package/dist/routes/workflows.d.ts +30 -30
  30. package/dist/routes/workflows.d.ts.map +1 -1
  31. package/dist/routes/ws/agents.d.ts +2 -3
  32. package/dist/routes/ws/agents.d.ts.map +1 -1
  33. package/dist/schemas/auth.d.ts +241 -85
  34. package/dist/schemas/auth.d.ts.map +1 -1
  35. package/dist/schemas/common.d.ts +11 -0
  36. package/dist/schemas/common.d.ts.map +1 -1
  37. package/dist/schemas/events.d.ts +74 -44
  38. package/dist/schemas/events.d.ts.map +1 -1
  39. package/dist/schemas/models.d.ts +38 -38
  40. package/dist/schemas/ontology.d.ts +160 -0
  41. package/dist/schemas/ontology.d.ts.map +1 -1
  42. package/dist/schemas/workflows.d.ts +180 -190
  43. package/dist/schemas/workflows.d.ts.map +1 -1
  44. package/dist/server.d.ts +6 -0
  45. package/dist/server.d.ts.map +1 -1
  46. package/dist/utils/ws.d.ts +1 -2
  47. package/dist/utils/ws.d.ts.map +1 -1
  48. package/package.json +4 -4
  49. package/src/auth/access-token-boundary.ts +31 -30
  50. package/src/auth/client-address.ts +67 -0
  51. package/src/auth/guard.ts +13 -4
  52. package/src/auth/public-routes.ts +9 -2
  53. package/src/auth/rate-limit.ts +43 -0
  54. package/src/auth/scope.ts +13 -3
  55. package/src/auth/shared-access.ts +0 -3
  56. package/src/openapi/security.ts +37 -17
  57. package/src/routes/action-runs.ts +5 -5
  58. package/src/routes/actions.ts +4 -4
  59. package/src/routes/agents.ts +9 -9
  60. package/src/routes/ai-accounting.ts +10 -10
  61. package/src/routes/auth.ts +331 -432
  62. package/src/routes/connector-connection-runs.ts +5 -5
  63. package/src/routes/connector-connections.ts +3 -3
  64. package/src/routes/connectors.ts +2 -2
  65. package/src/routes/events.ts +2 -2
  66. package/src/routes/files.ts +30 -15
  67. package/src/routes/links.ts +3 -3
  68. package/src/routes/logs.ts +2 -2
  69. package/src/routes/objects.ts +12 -12
  70. package/src/routes/ontology.ts +27 -9
  71. package/src/routes/pipelines.ts +2 -2
  72. package/src/routes/project.ts +2 -2
  73. package/src/routes/share-grants.ts +4 -4
  74. package/src/routes/shared-access.ts +3 -2
  75. package/src/routes/syncs.ts +2 -2
  76. package/src/routes/telemetry.ts +5 -5
  77. package/src/routes/workflows.ts +18 -19
  78. package/src/routes/ws/agents.ts +4 -5
  79. package/src/schemas/auth.ts +27 -8
  80. package/src/schemas/common.ts +6 -0
  81. package/src/schemas/events.ts +35 -7
  82. package/src/schemas/ontology.ts +16 -1
  83. package/src/schemas/workflows.ts +4 -9
  84. package/src/server.ts +35 -5
  85. package/src/utils/ws.ts +2 -3
@@ -1,7 +1,7 @@
1
1
  import type { SixbHostView } from "@sixb/core"
2
2
  import type { TimeseriesPoint } from "@sixb/core/storage"
3
3
  import type { Elysia } from "elysia"
4
- import { bearerSecurityRequirement } from "../auth/access-token-boundary"
4
+ import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
5
5
  import { requireRequestSixb } from "../auth/scope"
6
6
  import { OPENAPI_TAGS } from "../openapi/tags"
7
7
  import { ErrorResponseSchema, SuccessResponseSchema } from "../schemas/common"
@@ -65,7 +65,7 @@ export function registerTelemetryRoutes(app: Elysia, _host: SixbHostView) {
65
65
  summary: "Append telemetry point",
66
66
  tags: [OPENAPI_TAGS.telemetry.name],
67
67
  operationId: "appendTelemetry",
68
- security: bearerSecurityRequirement("appendTelemetry"),
68
+ security: accessTokenSecurityRequirement("appendTelemetry"),
69
69
  },
70
70
  }
71
71
  )
@@ -106,7 +106,7 @@ export function registerTelemetryRoutes(app: Elysia, _host: SixbHostView) {
106
106
  summary: "Get bulk telemetry history",
107
107
  tags: [OPENAPI_TAGS.telemetry.name],
108
108
  operationId: "getBulkTelemetryHistory",
109
- security: bearerSecurityRequirement("getBulkTelemetryHistory"),
109
+ security: accessTokenSecurityRequirement("getBulkTelemetryHistory"),
110
110
  },
111
111
  }
112
112
  )
@@ -148,7 +148,7 @@ export function registerTelemetryRoutes(app: Elysia, _host: SixbHostView) {
148
148
  summary: "Get telemetry history",
149
149
  tags: [OPENAPI_TAGS.telemetry.name],
150
150
  operationId: "getTelemetryHistory",
151
- security: bearerSecurityRequirement("getTelemetryHistory"),
151
+ security: accessTokenSecurityRequirement("getTelemetryHistory"),
152
152
  },
153
153
  }
154
154
  )
@@ -189,7 +189,7 @@ export function registerTelemetryRoutes(app: Elysia, _host: SixbHostView) {
189
189
  summary: "Get latest telemetry point",
190
190
  tags: [OPENAPI_TAGS.telemetry.name],
191
191
  operationId: "getLatestTelemetry",
192
- security: bearerSecurityRequirement("getLatestTelemetry"),
192
+ security: accessTokenSecurityRequirement("getLatestTelemetry"),
193
193
  },
194
194
  }
195
195
  )
@@ -1,5 +1,4 @@
1
1
  import {
2
- type OntologySource,
3
2
  type Principal,
4
3
  type SixbHostView,
5
4
  SYSTEM_PRINCIPAL,
@@ -22,7 +21,7 @@ import type {
22
21
  import { AGENT_RUN_FAILURE_CODES, WORKFLOW_RUN_FAILURE_CODES } from "@sixb/core/storage"
23
22
  import type { Elysia } from "elysia"
24
23
  import { z } from "zod"
25
- import { bearerSecurityRequirement } from "../auth/access-token-boundary"
24
+ import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
26
25
  import { requestAuthState, requireRequestSixb } from "../auth/scope"
27
26
  import {
28
27
  createContextualFileContentResponse,
@@ -30,7 +29,7 @@ import {
30
29
  fileContentHeadResponses,
31
30
  handleFileContentQueryValidationError,
32
31
  } from "../files/content"
33
- import { SIXB_CSRF_SECURITY_REQUIREMENT } from "../openapi/security"
32
+ import { SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT } from "../openapi/security"
34
33
  import { OPENAPI_TAGS } from "../openapi/tags"
35
34
  import { ErrorResponseSchema } from "../schemas/common"
36
35
  import { FileContentQuerySchema } from "../schemas/files"
@@ -133,8 +132,8 @@ function serializeWorkflowNodeRun(node: WorkflowNodeRunView) {
133
132
  }
134
133
  }
135
134
 
136
- function serializePrincipal(principal: Principal) {
137
- return { principalType: principal.type, principalId: principal.id }
135
+ function serializePrincipal(principal: Principal): Principal {
136
+ return { type: principal.type, id: principal.id }
138
137
  }
139
138
 
140
139
  function serializeWorkflowAgentExecutionSummary(execution: WorkflowAgentNodeRunView) {
@@ -177,7 +176,7 @@ function isRecord(value: unknown): value is Readonly<Record<string, unknown>> {
177
176
  return value !== null && typeof value === "object" && !Array.isArray(value)
178
177
  }
179
178
 
180
- function principalForExecution(sixb: Sixb<readonly OntologySource[]>): Principal {
179
+ function principalForExecution(sixb: Sixb): Principal {
181
180
  return sixb.execution.requestedBy ?? SYSTEM_PRINCIPAL
182
181
  }
183
182
 
@@ -581,7 +580,7 @@ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
581
580
  summary: "List registered workflows",
582
581
  tags: [OPENAPI_TAGS.workflows.name],
583
582
  operationId: "listWorkflows",
584
- security: bearerSecurityRequirement("listWorkflows"),
583
+ security: accessTokenSecurityRequirement("listWorkflows"),
585
584
  },
586
585
  }
587
586
  )
@@ -607,7 +606,7 @@ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
607
606
  summary: "Get workflow metadata",
608
607
  tags: [OPENAPI_TAGS.workflows.name],
609
608
  operationId: "getWorkflow",
610
- security: bearerSecurityRequirement("getWorkflow"),
609
+ security: accessTokenSecurityRequirement("getWorkflow"),
611
610
  },
612
611
  }
613
612
  )
@@ -777,7 +776,7 @@ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
777
776
  summary: "Submit a workflow intervention response",
778
777
  tags: [OPENAPI_TAGS.workflowInterventions.name],
779
778
  operationId: "submitWorkflowIntervention",
780
- security: SIXB_CSRF_SECURITY_REQUIREMENT,
779
+ security: SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
781
780
  },
782
781
  }
783
782
  )
@@ -905,7 +904,7 @@ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
905
904
  summary: "Cancel a workflow intervention",
906
905
  tags: [OPENAPI_TAGS.workflowInterventions.name],
907
906
  operationId: "cancelWorkflowIntervention",
908
- security: SIXB_CSRF_SECURITY_REQUIREMENT,
907
+ security: SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
909
908
  },
910
909
  }
911
910
  )
@@ -953,7 +952,7 @@ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
953
952
  summary: "List workflow run history",
954
953
  tags: [OPENAPI_TAGS.workflowRuns.name],
955
954
  operationId: "listWorkflowRuns",
956
- security: bearerSecurityRequirement("listWorkflowRuns"),
955
+ security: accessTokenSecurityRequirement("listWorkflowRuns"),
957
956
  },
958
957
  }
959
958
  )
@@ -1008,7 +1007,7 @@ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
1008
1007
  summary: "Get workflow run detail",
1009
1008
  tags: [OPENAPI_TAGS.workflowRuns.name],
1010
1009
  operationId: "getWorkflowRun",
1011
- security: bearerSecurityRequirement("getWorkflowRun"),
1010
+ security: accessTokenSecurityRequirement("getWorkflowRun"),
1012
1011
  },
1013
1012
  }
1014
1013
  )
@@ -1055,7 +1054,7 @@ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
1055
1054
  summary: "Get workflow agent node execution detail",
1056
1055
  tags: [OPENAPI_TAGS.workflowRuns.name],
1057
1056
  operationId: "getWorkflowAgentNodeExecution",
1058
- security: bearerSecurityRequirement("getWorkflowAgentNodeExecution"),
1057
+ security: accessTokenSecurityRequirement("getWorkflowAgentNodeExecution"),
1059
1058
  },
1060
1059
  }
1061
1060
  )
@@ -1230,7 +1229,7 @@ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
1230
1229
  summary: "Cancel a workflow run",
1231
1230
  tags: [OPENAPI_TAGS.workflowRuns.name],
1232
1231
  operationId: "cancelWorkflowRun",
1233
- security: bearerSecurityRequirement("cancelWorkflowRun"),
1232
+ security: accessTokenSecurityRequirement("cancelWorkflowRun"),
1234
1233
  },
1235
1234
  }
1236
1235
  )
@@ -1245,7 +1244,7 @@ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
1245
1244
  summary: "Get workflow run file content",
1246
1245
  tags: ["Workflows"],
1247
1246
  operationId: "getWorkflowRunFileContent",
1248
- security: bearerSecurityRequirement("getWorkflowRunFileContent"),
1247
+ security: accessTokenSecurityRequirement("getWorkflowRunFileContent"),
1249
1248
  responses: fileContentGetResponses({ optionalStorage: true }),
1250
1249
  },
1251
1250
  }
@@ -1261,7 +1260,7 @@ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
1261
1260
  summary: "Head workflow run file content",
1262
1261
  tags: ["Workflows"],
1263
1262
  operationId: "headWorkflowRunFileContent",
1264
- security: bearerSecurityRequirement("headWorkflowRunFileContent"),
1263
+ security: accessTokenSecurityRequirement("headWorkflowRunFileContent"),
1265
1264
  responses: fileContentHeadResponses({ optionalStorage: true }),
1266
1265
  },
1267
1266
  }
@@ -1277,7 +1276,7 @@ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
1277
1276
  summary: "Get workflow node run file content",
1278
1277
  tags: ["Workflows"],
1279
1278
  operationId: "getWorkflowNodeRunFileContent",
1280
- security: bearerSecurityRequirement("getWorkflowNodeRunFileContent"),
1279
+ security: accessTokenSecurityRequirement("getWorkflowNodeRunFileContent"),
1281
1280
  responses: fileContentGetResponses({ optionalStorage: true }),
1282
1281
  },
1283
1282
  }
@@ -1293,7 +1292,7 @@ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
1293
1292
  summary: "Head workflow node run file content",
1294
1293
  tags: ["Workflows"],
1295
1294
  operationId: "headWorkflowNodeRunFileContent",
1296
- security: bearerSecurityRequirement("headWorkflowNodeRunFileContent"),
1295
+ security: accessTokenSecurityRequirement("headWorkflowNodeRunFileContent"),
1297
1296
  responses: fileContentHeadResponses({ optionalStorage: true }),
1298
1297
  },
1299
1298
  }
@@ -1341,7 +1340,7 @@ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
1341
1340
  summary: "Request a workflow run",
1342
1341
  tags: [OPENAPI_TAGS.workflowRuns.name],
1343
1342
  operationId: "requestWorkflowRun",
1344
- security: bearerSecurityRequirement("requestWorkflowRun"),
1343
+ security: accessTokenSecurityRequirement("requestWorkflowRun"),
1345
1344
  },
1346
1345
  }
1347
1346
  )
@@ -1,4 +1,3 @@
1
- import type { OntologySource } from "@sixb/core"
2
1
  import {
3
2
  AGENT_ACTIVITY_STREAM_ID,
4
3
  type AgentRunActivityEvent,
@@ -81,7 +80,7 @@ export function parseAgentStreamMessage(payload: unknown):
81
80
  }
82
81
 
83
82
  export async function canAccessAgentRunStream(
84
- sixb: Sixb<readonly OntologySource[]>,
83
+ sixb: Sixb,
85
84
  runId: string
86
85
  ): Promise<{ ok: true } | { ok: false; message: string }> {
87
86
  const run = await sixb.agent.runs.getById(runId)
@@ -92,7 +91,7 @@ export async function canAccessAgentRunStream(
92
91
  }
93
92
 
94
93
  export async function canAccessAgentThreadActivity(
95
- sixb: Sixb<readonly OntologySource[]>,
94
+ sixb: Sixb,
96
95
  event: AgentRunActivityEvent
97
96
  ): Promise<boolean> {
98
97
  const run = await sixb.agent.runs.getById(event.runId)
@@ -308,7 +307,7 @@ async function subscribeAgentActivity(
308
307
  }
309
308
 
310
309
  async function sendVisibleActivityRecords(
311
- sixb: Sixb<readonly OntologySource[]>,
310
+ sixb: Sixb,
312
311
  ws: { send: (message: string) => void },
313
312
  records: readonly BrokerRecord[]
314
313
  ): Promise<void> {
@@ -398,7 +397,7 @@ function stopSubscription(state: AgentStreamSubscriptionState | undefined): void
398
397
  }
399
398
 
400
399
  async function sendRunSnapshot(
401
- sixb: Sixb<readonly OntologySource[]>,
400
+ sixb: Sixb,
402
401
  ws: { send: (message: string) => void },
403
402
  runId: string
404
403
  ): Promise<boolean> {
@@ -33,18 +33,21 @@ export const AuthInvitationDeliverySchema = z.object({
33
33
  export const AuthInvitationGroupOptionSchema = AuthGroupOptionSchema
34
34
 
35
35
  export const AuthAccessTokenKindSchema = z.enum(["personal", "serviceAccount"])
36
- export const AuthAccessTokenSubjectTypeSchema = z.enum(["user", "serviceAccount"])
37
36
  export const AuthAccessTokenStatusSchema = z.enum(["active", "expired", "revoked"])
38
37
  export const AuthServiceAccountStatusSchema = z.enum(["active", "suspended"])
39
38
  export const AuthUserStatusSchema = z.enum(["active", "suspended"])
40
39
 
40
+ export const AuthAccessTokenSubjectSchema = z.object({
41
+ type: z.enum(["user", "serviceAccount"]),
42
+ id: z.string(),
43
+ })
44
+
41
45
  export const AuthAccessTokenSchema = z.object({
42
46
  id: z.string(),
43
47
  name: z.string(),
44
48
  kind: AuthAccessTokenKindSchema,
45
49
  status: AuthAccessTokenStatusSchema,
46
- subjectType: AuthAccessTokenSubjectTypeSchema,
47
- subjectId: z.string(),
50
+ subject: AuthAccessTokenSubjectSchema,
48
51
  subjectLabel: z.string().optional(),
49
52
  groupIds: z.array(z.string()).optional(),
50
53
  createdAt: z.string(),
@@ -272,7 +275,9 @@ export const AuthSessionResponseSchema = z.union([
272
275
  }),
273
276
  z.object({
274
277
  authenticated: z.literal(true),
275
- csrfToken: z.string(),
278
+ /** Browser sessions only: send it back in `x-sixb-csrf` on mutations. */
279
+ csrfToken: z.string().optional(),
280
+ /** Whether the user may open a web app: the browser session's own, or Atlas for a native client. */
276
281
  applicationAccess: z.object({
277
282
  allowed: z.boolean(),
278
283
  audience: z.enum(["atlas", "app"]),
@@ -296,9 +301,20 @@ export const AuthSignOutResponseSchema = z.object({
296
301
  })
297
302
 
298
303
  export const CreateDeviceAuthorizationBodySchema = z.object({
304
+ /** Shown to the approving user and on their sessions list, such as "sixb CLI on alex-mbp". */
299
305
  clientName: z.string().trim().min(1).max(100),
300
- tokenName: z.string().trim().min(1).max(100),
301
- expiresIn: z.literal("90d").optional(),
306
+ })
307
+
308
+ /** A native client's session tokens. Send `accessToken` as a Bearer token until `expiresIn` runs out. */
309
+ export const BearerSessionTokensSchema = z.object({
310
+ accessToken: z.string(),
311
+ refreshToken: z.string(),
312
+ /** Seconds until the access token expires. */
313
+ expiresIn: z.number(),
314
+ })
315
+
316
+ export const RefreshAuthSessionBodySchema = z.object({
317
+ refreshToken: z.string().min(1),
302
318
  })
303
319
 
304
320
  export const CreateDeviceAuthorizationResponseSchema = z.object({
@@ -318,7 +334,7 @@ export const ExchangeDeviceAuthorizationResponseSchema = z.union([
318
334
  z.object({ status: z.literal("pending") }),
319
335
  z.object({ status: z.literal("denied") }),
320
336
  z.object({ status: z.literal("expired") }),
321
- z.object({ status: z.literal("approved"), accessToken: z.string() }),
337
+ BearerSessionTokensSchema.extend({ status: z.literal("approved") }),
322
338
  ])
323
339
 
324
340
  export const DeviceAuthorizationDecisionBodySchema = z.object({
@@ -329,13 +345,16 @@ export const DeviceAuthorizationDecisionBodySchema = z.object({
329
345
 
330
346
  export const AuthSessionSummarySchema = z.object({
331
347
  id: z.string(),
332
- audience: z.enum(["atlas", "app"]),
348
+ /** The web app a browser session belongs to. Native clients' sessions have none. */
349
+ audience: z.enum(["atlas", "app"]).optional(),
333
350
  current: z.boolean(),
334
351
  createdAt: z.string(),
335
352
  expiresAt: z.string(),
336
353
  lastSeenAt: z.string().optional(),
337
354
  userAgent: z.string().optional(),
338
355
  ipAddress: z.string().optional(),
356
+ /** Set for native clients, such as the CLI. */
357
+ clientName: z.string().optional(),
339
358
  })
340
359
 
341
360
  export const ListAuthSessionsResponseSchema = z.object({
@@ -15,6 +15,12 @@ export function codedErrorResponseSchema<
15
15
 
16
16
  export const SuccessResponseSchema = z.object({ success: z.boolean() })
17
17
 
18
+ /** The canonical `Principal` shape from `@sixb/core`. */
19
+ export const PrincipalSchema = z.object({
20
+ type: z.enum(["user", "serviceAccount", "system"]),
21
+ id: z.string().min(1),
22
+ })
23
+
18
24
  export type JsonValue =
19
25
  | string
20
26
  | number
@@ -1,4 +1,4 @@
1
- import type { DomainEvent } from "@sixb/core"
1
+ import type { DomainEvent, TrustedPrimitiveKind } from "@sixb/core"
2
2
  import {
3
3
  EVENT_TOPICS as CORE_EVENT_TOPICS,
4
4
  EVENT_TYPES as CORE_EVENT_TYPES,
@@ -60,15 +60,38 @@ const StoredEventBaseSchema = z.object({
60
60
  schemaVersion: z.literal(1),
61
61
  projectId: z.string(),
62
62
  occurredAt: z.string(),
63
- actor: z
64
- .object({
65
- type: z.enum(["user", "serviceAccount", "system"]),
66
- id: z.string(),
67
- })
68
- .optional(),
69
63
  partitionKey: z.string(),
70
64
  })
71
65
 
66
+ const TRUSTED_PRIMITIVE_KINDS = [
67
+ "action",
68
+ "pipeline",
69
+ "projection",
70
+ "rule",
71
+ "sync",
72
+ "webhook",
73
+ "workflow",
74
+ ] as const satisfies readonly TrustedPrimitiveKind[]
75
+
76
+ /** Mirrors the core `EventExecutor`: the workload that made an ontology change. */
77
+ export const EventExecutorSchema = z.discriminatedUnion("type", [
78
+ z.object({ type: z.literal("request"), requestId: z.string() }),
79
+ z.object({
80
+ type: z.literal("primitive"),
81
+ kind: z.enum(TRUSTED_PRIMITIVE_KINDS),
82
+ id: z.string(),
83
+ runId: z.string(),
84
+ }),
85
+ z.object({ type: z.literal("agent"), runId: z.string() }),
86
+ z.object({
87
+ type: z.literal("kernel"),
88
+ operation: z.discriminatedUnion("type", [
89
+ z.object({ type: z.literal("ontology.recover"), recoveryId: z.string() }),
90
+ z.object({ type: z.literal("ontology.indexVectors"), indexingId: z.string() }),
91
+ ]),
92
+ }),
93
+ ])
94
+
72
95
  const StoredAuthorableEventBaseSchema = StoredEventBaseSchema.extend({
73
96
  correlationId: z.string().optional(),
74
97
  causationId: z.string().optional(),
@@ -79,6 +102,11 @@ const StoredAuthorableEventBaseSchema = StoredEventBaseSchema.extend({
79
102
 
80
103
  const StoredOntologyEventBaseSchema = StoredEventBaseSchema.extend({
81
104
  origin: EventOriginSchema,
105
+ requestedBy: z
106
+ .object({ type: z.enum(["user", "serviceAccount"]), id: z.string() })
107
+ .optional()
108
+ .describe("Principal on whose behalf the write ran; absent for automatic or anonymous work."),
109
+ executor: EventExecutorSchema,
82
110
  commitId: z.string(),
83
111
  commitOrdinal: z.number().int().nonnegative(),
84
112
  })
@@ -25,6 +25,21 @@ export const PropertyDefinitionSchema = z.object({
25
25
  .optional(),
26
26
  })
27
27
 
28
+ export const PropertyQueryCapabilitiesSchema = z
29
+ .object({
30
+ operators: z.array(z.enum(["eq", "neq", "lt", "lte", "gt", "gte", "in", "exists", "contains"])),
31
+ sortable: z.boolean(),
32
+ facet: z.boolean(),
33
+ text: z.boolean(),
34
+ })
35
+ .describe(
36
+ "What object queries accept for this property, resolved from its schema and query metadata."
37
+ )
38
+
39
+ export const ObjectPropertyDefinitionSchema = PropertyDefinitionSchema.extend({
40
+ capabilities: PropertyQueryCapabilitiesSchema,
41
+ })
42
+
28
43
  export const LinkDefinitionSchema = z.object({
29
44
  id: z.string(),
30
45
  name: z.string(),
@@ -57,7 +72,7 @@ export const ObjectTypeSchema = z.object({
57
72
  description: z.string().optional(),
58
73
  extends: z.string().optional(),
59
74
  implements: z.array(z.string()).optional(),
60
- properties: z.array(PropertyDefinitionSchema),
75
+ properties: z.array(ObjectPropertyDefinitionSchema),
61
76
  search: z
62
77
  .object({
63
78
  title: z.string().optional(),
@@ -7,7 +7,7 @@ import { z } from "zod"
7
7
  import { AgentMessagePartSchema, AgentRunFailureSchema } from "./agents"
8
8
  import { AiCostSummarySchema } from "./ai-accounting"
9
9
  import { AiUsageSummarySchema } from "./ai-usage"
10
- import { JsonValueSchema, sixbFailureSchema } from "./common"
10
+ import { JsonValueSchema, PrincipalSchema, sixbFailureSchema } from "./common"
11
11
 
12
12
  export const WorkflowIOSnapshotSchema = z.record(JsonValueSchema)
13
13
 
@@ -74,11 +74,6 @@ export const RequestWorkflowRunBodySchema = z
74
74
  })
75
75
  .default({})
76
76
 
77
- export const WorkflowInterventionActorSchema = z.object({
78
- principalType: z.enum(["user", "serviceAccount", "system"]),
79
- principalId: z.string().min(1),
80
- })
81
-
82
77
  export const CancelWorkflowRunBodySchema = z.object({}).default({})
83
78
 
84
79
  export const SubmitWorkflowInterventionBodySchema = z.object({
@@ -133,7 +128,7 @@ export const WorkflowRunSummarySchema = z.object({
133
128
  startedAt: z.string(),
134
129
  finishedAt: z.string().optional(),
135
130
  error: WorkflowRunFailureSchema.optional(),
136
- requestedBy: WorkflowInterventionActorSchema,
131
+ requestedBy: PrincipalSchema,
137
132
  })
138
133
 
139
134
  export const WorkflowRunDetailSchema = WorkflowRunSummarySchema.extend({
@@ -198,10 +193,10 @@ export const WorkflowInterventionSchema = z.object({
198
193
  requestedAt: z.string(),
199
194
  expiresAt: z.string().optional(),
200
195
  submittedAt: z.string().optional(),
201
- submittedBy: WorkflowInterventionActorSchema.optional(),
196
+ submittedBy: PrincipalSchema.optional(),
202
197
  response: WorkflowIOSnapshotSchema.optional(),
203
198
  cancelledAt: z.string().optional(),
204
- cancelledBy: WorkflowInterventionActorSchema.optional(),
199
+ cancelledBy: PrincipalSchema.optional(),
205
200
  expiredAt: z.string().optional(),
206
201
  })
207
202
 
package/src/server.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import { cors } from "@elysiajs/cors"
2
2
  import { openapi } from "@elysiajs/openapi"
3
3
  import type { OntologyMaintenanceHandle, SixbHostView } from "@sixb/core"
4
- import { CSRF_HEADER_NAME } from "@sixb/core/internal/auth"
4
+ import { CSRF_HEADER_NAME, setRequestClientAddress } from "@sixb/core/internal/auth"
5
5
  import { bindRequestExecution } from "@sixb/core/internal/request-execution"
6
+ import type { Server } from "bun"
6
7
  import { Elysia } from "elysia"
7
8
  import { websocket as elysiaWebSocket } from "elysia/ws"
8
9
  import { zodToJsonSchema } from "zod-to-json-schema"
@@ -24,6 +25,11 @@ import {
24
25
  resolveApiBrowserPublicOrigin,
25
26
  type SixbApiBrowserPolicy,
26
27
  } from "./auth/browser-origin"
28
+ import {
29
+ type ClientAddressResolver,
30
+ createClientAddressResolver,
31
+ PRIVATE_NETWORKS,
32
+ } from "./auth/client-address"
27
33
  import { CSRF_TOKEN_RESPONSE_HEADER_NAME } from "./auth/csrf"
28
34
  import type { SixbAuthExperienceOptions } from "./auth/experience"
29
35
  import { ServerAuthGuard } from "./auth/guard"
@@ -36,10 +42,13 @@ import {
36
42
  type SixbSharedAccessOptions,
37
43
  } from "./auth/shared-access"
38
44
  import {
39
- SIXB_BEARER_SECURITY_SCHEME,
40
- SIXB_BEARER_SECURITY_SCHEME_ID,
45
+ SIXB_ACCESS_TOKEN_SECURITY_SCHEME,
46
+ SIXB_ACCESS_TOKEN_SECURITY_SCHEME_ID,
41
47
  SIXB_CSRF_SECURITY_SCHEME,
42
48
  SIXB_CSRF_SECURITY_SCHEME_ID,
49
+ SIXB_SESSION_SECURITY_REQUIREMENT,
50
+ SIXB_SESSION_SECURITY_SCHEME,
51
+ SIXB_SESSION_SECURITY_SCHEME_ID,
43
52
  SIXB_SHARED_GRANT_SECURITY_SCHEME,
44
53
  SIXB_SHARED_GRANT_SECURITY_SCHEME_ID,
45
54
  } from "./openapi/security"
@@ -61,6 +70,11 @@ export interface SixbServerOptions {
61
70
  authExperience?: SixbAuthExperienceOptions
62
71
  /** Optional server limits for short-lived shared-access sessions. */
63
72
  sharedAccess?: SixbSharedAccessOptions
73
+ /**
74
+ * Proxies whose `x-forwarded-for` entries identify the client: IP addresses, CIDR ranges, or
75
+ * `"private"` for loopback, private, and link-local networks. Defaults to `["private"]`.
76
+ */
77
+ trustedProxies?: readonly string[]
64
78
  }
65
79
 
66
80
  export function createSixbServer(options: SixbServerOptions): SixbServer {
@@ -77,6 +91,7 @@ export class SixbServer {
77
91
  private readonly authRedirectContextResolver: ResolveAuthRedirectContext
78
92
  private readonly authExperience?: SixbAuthExperienceOptions
79
93
  private readonly sharedAccessOptions: SixbSharedAccessOptions
94
+ private readonly resolveClientAddress: ClientAddressResolver
80
95
  private app: SixbApp | null = null
81
96
  private bunServer: ReturnType<typeof Bun.serve> | null = null
82
97
  private maintenance: OntologyMaintenanceHandle | null = null
@@ -93,6 +108,9 @@ export class SixbServer {
93
108
  )
94
109
  this.authExperience = options.authExperience
95
110
  this.sharedAccessOptions = options.sharedAccess ?? {}
111
+ this.resolveClientAddress = createClientAddressResolver(
112
+ options.trustedProxies ?? [PRIVATE_NETWORKS]
113
+ )
96
114
  }
97
115
 
98
116
  getHost(): SixbHostView {
@@ -154,6 +172,7 @@ export class SixbServer {
154
172
  this.bunServer = startApiServer(this.app, {
155
173
  host: this.hostname,
156
174
  port: this.port,
175
+ resolveClientAddress: this.resolveClientAddress,
157
176
  })
158
177
  } catch (error) {
159
178
  this.app = null
@@ -377,11 +396,14 @@ export function createSixbApi(server: SixbServer) {
377
396
  components: {
378
397
  securitySchemes: {
379
398
  [SIXB_CSRF_SECURITY_SCHEME_ID]: SIXB_CSRF_SECURITY_SCHEME,
380
- [SIXB_BEARER_SECURITY_SCHEME_ID]: SIXB_BEARER_SECURITY_SCHEME,
399
+ [SIXB_SESSION_SECURITY_SCHEME_ID]: SIXB_SESSION_SECURITY_SCHEME,
400
+ [SIXB_ACCESS_TOKEN_SECURITY_SCHEME_ID]: SIXB_ACCESS_TOKEN_SECURITY_SCHEME,
381
401
  [SIXB_SHARED_GRANT_SECURITY_SCHEME_ID]: SIXB_SHARED_GRANT_SECURITY_SCHEME,
382
402
  },
383
403
  schemas: ObjectQueryOpenApiSchemas,
384
404
  },
405
+ // Every operation needs a session unless it says otherwise; public ones declare `[]`.
406
+ security: SIXB_SESSION_SECURITY_REQUIREMENT,
385
407
  tags: OPENAPI_TAG_METADATA,
386
408
  },
387
409
  swagger: {
@@ -498,12 +520,20 @@ function startApiServer(
498
520
  options: {
499
521
  readonly host: string
500
522
  readonly port: number
523
+ readonly resolveClientAddress: ClientAddressResolver
501
524
  }
502
525
  ) {
503
526
  const bunServer = Bun.serve({
504
527
  port: options.port,
505
528
  hostname: options.host,
506
- fetch: (request) => app.fetch(request),
529
+ fetch: (request: Request, server: Server<unknown>) => {
530
+ const socketAddress = server.requestIP(request)?.address
531
+ if (socketAddress) {
532
+ const forwardedFor = request.headers.get("x-forwarded-for")
533
+ setRequestClientAddress(request, options.resolveClientAddress(socketAddress, forwardedFor))
534
+ }
535
+ return app.fetch(request)
536
+ },
507
537
  websocket: getElysiaWsHandler(app),
508
538
  } as Parameters<typeof Bun.serve>[0])
509
539
 
package/src/utils/ws.ts CHANGED
@@ -1,4 +1,3 @@
1
- import type { OntologySource } from "@sixb/core"
2
1
  import type { Sixb } from "@sixb/core/internal/request-execution"
3
2
 
4
3
  export function safeSend(target: { send: (message: string) => void }, payload: unknown): void {
@@ -16,10 +15,10 @@ export function wsStateKey(ws: object): object {
16
15
  }
17
16
 
18
17
  /** The execution SDK attached once when the WebSocket request crosses the auth boundary. */
19
- export function wsRequestSixb(ws: object): Sixb<readonly OntologySource[]> | null {
18
+ export function wsRequestSixb(ws: object): Sixb | null {
20
19
  const data = (
21
20
  ws as {
22
- data?: { sixb?: Sixb<readonly OntologySource[]> | null }
21
+ data?: { sixb?: Sixb | null }
23
22
  }
24
23
  ).data
25
24
  return data?.sixb ?? null