@sixb/server 0.1.20 → 0.1.21
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/dist/artifacts/frame.d.ts +13 -0
- package/dist/artifacts/frame.d.ts.map +1 -0
- package/dist/auth/browser-origin.d.ts +5 -0
- package/dist/auth/browser-origin.d.ts.map +1 -1
- package/dist/auth/public-routes.d.ts.map +1 -1
- package/dist/files/content.d.ts +2 -3
- package/dist/files/content.d.ts.map +1 -1
- package/dist/index.js +3427 -2402
- package/dist/index.js.map +23 -17
- package/dist/openapi/tags.d.ts +10 -2
- package/dist/openapi/tags.d.ts.map +1 -1
- package/dist/registerRoutes.d.ts +3 -0
- package/dist/registerRoutes.d.ts.map +1 -1
- package/dist/routes/activity.d.ts +35 -0
- package/dist/routes/activity.d.ts.map +1 -0
- package/dist/routes/agents.d.ts +18 -27
- package/dist/routes/agents.d.ts.map +1 -1
- package/dist/routes/artifact-frames.d.ts +98 -0
- package/dist/routes/artifact-frames.d.ts.map +1 -0
- package/dist/routes/artifacts.d.ts +852 -0
- package/dist/routes/artifacts.d.ts.map +1 -0
- package/dist/routes/auth.d.ts +1 -1
- package/dist/routes/connector-connection-runs.d.ts +59 -59
- package/dist/routes/connector-connections.d.ts +2 -2
- package/dist/routes/datasets.d.ts +7 -7
- package/dist/routes/events.d.ts +1 -1
- package/dist/routes/pipelines.d.ts +31 -31
- package/dist/routes/projections.d.ts +10 -10
- package/dist/routes/share-grants.d.ts +5 -5
- package/dist/routes/syncs.d.ts +38 -38
- package/dist/routes/workflows.d.ts +30 -30
- package/dist/routes/ws/events.d.ts.map +1 -1
- package/dist/routes/ws/subscription-hub.d.ts +8 -3
- package/dist/routes/ws/subscription-hub.d.ts.map +1 -1
- package/dist/schemas/activity.d.ts +1278 -0
- package/dist/schemas/activity.d.ts.map +1 -0
- package/dist/schemas/agents.d.ts +96 -144
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/ai-accounting.d.ts +24 -24
- package/dist/schemas/ai-limits.d.ts +26 -26
- package/dist/schemas/artifacts.d.ts +1305 -0
- package/dist/schemas/artifacts.d.ts.map +1 -0
- package/dist/schemas/auth.d.ts +6 -6
- package/dist/schemas/connectors.d.ts +30 -30
- package/dist/schemas/datasets.d.ts +10 -10
- package/dist/schemas/events.d.ts +6 -6
- package/dist/schemas/pipelines.d.ts +35 -35
- package/dist/schemas/projections.d.ts +40 -40
- package/dist/schemas/share-grants.d.ts +16 -16
- package/dist/schemas/syncs.d.ts +36 -36
- package/dist/schemas/workflows.d.ts +84 -84
- package/dist/server.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/artifacts/frame.ts +46 -0
- package/src/auth/browser-origin.ts +12 -2
- package/src/auth/public-routes.ts +10 -0
- package/src/files/content.ts +9 -13
- package/src/openapi/tags.ts +8 -0
- package/src/registerRoutes.ts +9 -0
- package/src/routes/activity.ts +327 -0
- package/src/routes/agents.ts +1 -1
- package/src/routes/artifact-frames.ts +138 -0
- package/src/routes/artifacts.ts +442 -0
- package/src/routes/file-downloads.ts +1 -3
- package/src/routes/ws/events.ts +3 -2
- package/src/routes/ws/subscription-hub.ts +53 -20
- package/src/schemas/activity.ts +285 -0
- package/src/schemas/agents.ts +1 -2
- package/src/schemas/artifacts.ts +158 -0
- package/src/server.ts +25 -9
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ARTIFACT_FRAME_ROUTE_PREFIX } from "@sixb/core/internal/artifacts/frames"
|
|
2
|
+
import {
|
|
3
|
+
type ResolvedSixbApiBrowserPolicy,
|
|
4
|
+
resolveApiBrowserPublicOrigin,
|
|
5
|
+
} from "../auth/browser-origin"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Hosts a frame may load from besides its own files: CSS and script libraries and web fonts. Each
|
|
9
|
+
* is a public CDN; a frame that uses one contacts it when it opens.
|
|
10
|
+
*/
|
|
11
|
+
const FRAME_CDN_ALLOWLIST = [
|
|
12
|
+
"https://cdn.jsdelivr.net",
|
|
13
|
+
"https://cdnjs.cloudflare.com",
|
|
14
|
+
"https://unpkg.com",
|
|
15
|
+
"https://fonts.googleapis.com",
|
|
16
|
+
"https://fonts.gstatic.com",
|
|
17
|
+
] as const
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The policy every frame response carries, 404s included. The frame is sandboxed without
|
|
21
|
+
* `allow-same-origin`, so its page cannot read the app's cookies or call the API as the reader;
|
|
22
|
+
* it loads only its own version's files, under its token, and the CDNs above; only the app's
|
|
23
|
+
* origins may show it.
|
|
24
|
+
*/
|
|
25
|
+
export function frameContentSecurityPolicy(input: {
|
|
26
|
+
readonly policy: ResolvedSixbApiBrowserPolicy
|
|
27
|
+
readonly request: Request
|
|
28
|
+
readonly token: string
|
|
29
|
+
}): string {
|
|
30
|
+
const origin = resolveApiBrowserPublicOrigin(input.policy, input.request)
|
|
31
|
+
const base = `${origin}${ARTIFACT_FRAME_ROUTE_PREFIX}${input.token}/`
|
|
32
|
+
const cdn = FRAME_CDN_ALLOWLIST.join(" ")
|
|
33
|
+
return [
|
|
34
|
+
"default-src 'none'",
|
|
35
|
+
`script-src ${base} 'unsafe-inline' ${cdn}`,
|
|
36
|
+
`style-src ${base} 'unsafe-inline' ${cdn}`,
|
|
37
|
+
`img-src ${base} data: blob: ${cdn}`,
|
|
38
|
+
`font-src ${base} data: ${cdn}`,
|
|
39
|
+
`media-src ${base} data: blob:`,
|
|
40
|
+
`connect-src ${base}`,
|
|
41
|
+
"form-action 'none'",
|
|
42
|
+
"base-uri 'none'",
|
|
43
|
+
`frame-ancestors 'self' ${input.policy.allowedOrigins.join(" ")}`.trim(),
|
|
44
|
+
"sandbox allow-scripts allow-popups allow-popups-to-escape-sandbox allow-modals",
|
|
45
|
+
].join("; ")
|
|
46
|
+
}
|
|
@@ -87,12 +87,22 @@ export function resolveApiBrowserAuthContext(
|
|
|
87
87
|
throw new BrowserOriginError(`[SixbServer] Browser origin '${origin}' is not allowed.`)
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Whether the request's Origin may call the API with a session. An Origin that is not an http(s)
|
|
92
|
+
* origin, such as a sandboxed frame's `null`, is not allowed, never an error: the CORS plugin asks
|
|
93
|
+
* this of every request, and a throw there replaces the route's response with the error's.
|
|
94
|
+
*/
|
|
90
95
|
export function isAllowedApiBrowserOrigin(
|
|
91
96
|
policy: ResolvedSixbApiBrowserPolicy,
|
|
92
97
|
request: Request
|
|
93
98
|
): boolean {
|
|
94
|
-
|
|
95
|
-
|
|
99
|
+
try {
|
|
100
|
+
const origin = normalizeRequestOrigin(request)
|
|
101
|
+
return origin !== null && isAllowedOrigin(policy, request, origin)
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (error instanceof BrowserOriginError) return false
|
|
104
|
+
throw error
|
|
105
|
+
}
|
|
96
106
|
}
|
|
97
107
|
|
|
98
108
|
export function resolveApiBrowserAuthRedirectContext(
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AGENT_API_GATEWAY_PREFIX } from "@sixb/core/internal/agents"
|
|
2
|
+
import { ARTIFACT_FRAME_ROUTE_PREFIX } from "@sixb/core/internal/artifacts/frames"
|
|
2
3
|
import { isCsrfExemptMethod } from "@sixb/core/internal/auth"
|
|
3
4
|
import { FILE_DOWNLOAD_ROUTE_PREFIX } from "@sixb/core/internal/blob-storage"
|
|
4
5
|
import { isSharedAccessPublicPath } from "./shared-access"
|
|
@@ -71,6 +72,15 @@ export function isPublicRoute(pathname: string, method: string): boolean {
|
|
|
71
72
|
return true
|
|
72
73
|
}
|
|
73
74
|
|
|
75
|
+
// A frame's page is sandboxed with an opaque origin, so it cannot hold a session either. The
|
|
76
|
+
// token in a frame link's path is the credential.
|
|
77
|
+
if (
|
|
78
|
+
pathname.startsWith(ARTIFACT_FRAME_ROUTE_PREFIX) &&
|
|
79
|
+
(normalizedMethod === "GET" || normalizedMethod === "HEAD")
|
|
80
|
+
) {
|
|
81
|
+
return true
|
|
82
|
+
}
|
|
83
|
+
|
|
74
84
|
// Public because a webhook cannot carry a session: the signature its connector
|
|
75
85
|
// verifies is the credential. POST only — `WebhookDefinition.method` is the literal
|
|
76
86
|
// `"POST"`, so nothing else is ever registered here, and the allow-list should not be
|
package/src/files/content.ts
CHANGED
|
@@ -15,9 +15,8 @@ export interface FileContentResponseInput {
|
|
|
15
15
|
readonly fileRef: FileRef
|
|
16
16
|
readonly disposition?: FileContentDisposition
|
|
17
17
|
readonly head?: boolean
|
|
18
|
-
|
|
19
|
-
readonly
|
|
20
|
-
readonly ifRangeHeader?: string | null
|
|
18
|
+
/** The request being answered: its Range, If-Range, and If-None-Match headers apply. */
|
|
19
|
+
readonly request?: Request
|
|
21
20
|
}
|
|
22
21
|
|
|
23
22
|
export interface FileContentQuery {
|
|
@@ -161,18 +160,17 @@ export async function createFileContentResponse(
|
|
|
161
160
|
const canReadRange = supportsRangeRead(input.blobStorage)
|
|
162
161
|
const headers = fileContentHeaders(input.fileRef, stat, input.disposition, canReadRange)
|
|
163
162
|
const etag = headers.get("etag")!
|
|
164
|
-
|
|
163
|
+
const requestHeaders = input.request?.headers
|
|
164
|
+
if (matchesIfNoneMatch(requestHeaders?.get("if-none-match"), etag)) {
|
|
165
165
|
headers.delete("content-length")
|
|
166
166
|
return new Response(null, { status: 304, headers })
|
|
167
167
|
}
|
|
168
168
|
|
|
169
169
|
// Date validators are unsupported: only a strong ETag can validate a range.
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
) {
|
|
175
|
-
const range = parseSingleByteRange(input.rangeHeader, stat.sizeBytes)
|
|
170
|
+
const rangeHeader = requestHeaders?.get("range")
|
|
171
|
+
const ifRangeHeader = requestHeaders?.get("if-range")
|
|
172
|
+
if (canReadRange && rangeHeader && (ifRangeHeader == null || ifRangeHeader.trim() === etag)) {
|
|
173
|
+
const range = parseSingleByteRange(rangeHeader, stat.sizeBytes)
|
|
176
174
|
if (!range) {
|
|
177
175
|
return rangeNotSatisfiableResponse(stat.sizeBytes)
|
|
178
176
|
}
|
|
@@ -237,9 +235,7 @@ export async function createContextualFileContentResponse<TQuery extends FileCon
|
|
|
237
235
|
fileRef,
|
|
238
236
|
disposition: parsed.disposition,
|
|
239
237
|
head: input.head,
|
|
240
|
-
|
|
241
|
-
ifNoneMatchHeader: input.request.headers.get("if-none-match"),
|
|
242
|
-
ifRangeHeader: input.request.headers.get("if-range"),
|
|
238
|
+
request: input.request,
|
|
243
239
|
})
|
|
244
240
|
if (!response) {
|
|
245
241
|
return fileContentNotFound(input.set, missingMessage)
|
package/src/openapi/tags.ts
CHANGED
|
@@ -123,6 +123,10 @@ export const OPENAPI_TAGS = {
|
|
|
123
123
|
name: "Files",
|
|
124
124
|
description: "File uploads and file references",
|
|
125
125
|
},
|
|
126
|
+
artifacts: {
|
|
127
|
+
name: "Artifacts",
|
|
128
|
+
description: "Versioned documents, sheets, slides, pages, and files",
|
|
129
|
+
},
|
|
126
130
|
links: {
|
|
127
131
|
name: "Links",
|
|
128
132
|
description: "Object relationship links",
|
|
@@ -139,6 +143,10 @@ export const OPENAPI_TAGS = {
|
|
|
139
143
|
name: "Logs",
|
|
140
144
|
description: "Run log stream",
|
|
141
145
|
},
|
|
146
|
+
activity: {
|
|
147
|
+
name: "Activity",
|
|
148
|
+
description: "Visits and page views in the project's app and its shared links",
|
|
149
|
+
},
|
|
142
150
|
authSessions: {
|
|
143
151
|
name: "Auth Sessions",
|
|
144
152
|
description: "Authentication sessions and sign-out",
|
package/src/registerRoutes.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import type { SixbHostView } from "@sixb/core"
|
|
2
2
|
import type { Elysia } from "elysia"
|
|
3
|
+
import type { ResolvedSixbApiBrowserPolicy } from "./auth/browser-origin"
|
|
3
4
|
import type { SharedAccessBoundary } from "./auth/shared-access"
|
|
4
5
|
import { registerActionRunRoutes } from "./routes/action-runs"
|
|
5
6
|
import { registerActionRoutes } from "./routes/actions"
|
|
7
|
+
import { registerActivityRoutes } from "./routes/activity"
|
|
6
8
|
import { registerAgentApiGatewayRoutes } from "./routes/agent-api-gateway"
|
|
7
9
|
import { registerAgentRoutes } from "./routes/agents"
|
|
8
10
|
import { registerAiAccountingRoutes } from "./routes/ai-accounting"
|
|
11
|
+
import { registerArtifactFrameRoutes } from "./routes/artifact-frames"
|
|
12
|
+
import { registerArtifactRoutes } from "./routes/artifacts"
|
|
9
13
|
import {
|
|
10
14
|
type ConnectorConnectionRouteOptions,
|
|
11
15
|
registerConnectorConnectionRunRoutes,
|
|
@@ -36,6 +40,8 @@ import { registerWorkflowRoutes } from "./routes/workflows"
|
|
|
36
40
|
export interface HttpRouteOptions {
|
|
37
41
|
readonly connectorConnections: ConnectorConnectionRouteOptions
|
|
38
42
|
readonly sharedAccess: SharedAccessBoundary
|
|
43
|
+
/** Where frames are served from and which app origins may show them. */
|
|
44
|
+
readonly browserPolicy: ResolvedSixbApiBrowserPolicy
|
|
39
45
|
}
|
|
40
46
|
|
|
41
47
|
export function registerHttpRoutes(app: Elysia, host: SixbHostView, options: HttpRouteOptions) {
|
|
@@ -60,11 +66,14 @@ export function registerHttpRoutes(app: Elysia, host: SixbHostView, options: Htt
|
|
|
60
66
|
registerFileDownloadRoutes(app, host)
|
|
61
67
|
registerActionRunRoutes(app, host)
|
|
62
68
|
registerAgentRoutes(app, host)
|
|
69
|
+
registerArtifactRoutes(app, host)
|
|
70
|
+
registerArtifactFrameRoutes(app, host, options.browserPolicy)
|
|
63
71
|
registerModelRoutes(app, host)
|
|
64
72
|
registerLinkRoutes(app, host)
|
|
65
73
|
registerTelemetryRoutes(app, host)
|
|
66
74
|
registerEventRoutes(app, host)
|
|
67
75
|
registerLogRoutes(app, host)
|
|
76
|
+
registerActivityRoutes(app, host)
|
|
68
77
|
registerProjectionRoutes(app, host)
|
|
69
78
|
registerWebhookRunRoutes(app, host)
|
|
70
79
|
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ActivityConnection,
|
|
3
|
+
type ActivityTimelineEntry,
|
|
4
|
+
type ActivityVisitTimeline,
|
|
5
|
+
type ActivityVisitView,
|
|
6
|
+
AuthorizationError,
|
|
7
|
+
type SixbHostView,
|
|
8
|
+
} from "@sixb/core"
|
|
9
|
+
import { getActivityCaptureRuntime } from "@sixb/core/internal/runtime"
|
|
10
|
+
import type { Elysia } from "elysia"
|
|
11
|
+
import { accessTokenSecurityRequirement, credentialTransport } from "../auth/access-token-boundary"
|
|
12
|
+
import { requestCaller, requireRequestSixb } from "../auth/scope"
|
|
13
|
+
import {
|
|
14
|
+
SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
|
|
15
|
+
SIXB_SESSION_SECURITY_REQUIREMENT,
|
|
16
|
+
SIXB_SHARED_MUTATION_SECURITY_REQUIREMENT,
|
|
17
|
+
SIXB_SHARED_READ_SECURITY_REQUIREMENT,
|
|
18
|
+
} from "../openapi/security"
|
|
19
|
+
import { OPENAPI_TAGS } from "../openapi/tags"
|
|
20
|
+
import {
|
|
21
|
+
ActivityCalendarQuerySchema,
|
|
22
|
+
ActivityCalendarSchema,
|
|
23
|
+
ActivityCaptureOptionsSchema,
|
|
24
|
+
ActivityCaptureResultSchema,
|
|
25
|
+
ActivityConnectionsPageSchema,
|
|
26
|
+
ActivityConnectionsQuerySchema,
|
|
27
|
+
ActivityPageQuerySchema,
|
|
28
|
+
ActivityPeriodQuerySchema,
|
|
29
|
+
ActivitySummarySchema,
|
|
30
|
+
ActivityUsersPageSchema,
|
|
31
|
+
ActivityVisitParamsSchema,
|
|
32
|
+
ActivityVisitsPageSchema,
|
|
33
|
+
ActivityVisitTimelineSchema,
|
|
34
|
+
CaptureActivityBodySchema,
|
|
35
|
+
} from "../schemas/activity"
|
|
36
|
+
import { ErrorResponseSchema } from "../schemas/common"
|
|
37
|
+
import { handleRouteError } from "../utils/http"
|
|
38
|
+
|
|
39
|
+
const errors = { 400: ErrorResponseSchema, 403: ErrorResponseSchema }
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Recording and reading take different authority: a browser session records only its own visits,
|
|
43
|
+
* and reading needs `can.observe(project.activity)`.
|
|
44
|
+
*/
|
|
45
|
+
export function registerActivityRoutes(app: Elysia, _host: SixbHostView) {
|
|
46
|
+
app.get(
|
|
47
|
+
"/api/activity/capture-config",
|
|
48
|
+
(context) => {
|
|
49
|
+
try {
|
|
50
|
+
assertBrowserCapture(context)
|
|
51
|
+
return getActivityCaptureRuntime(requireRequestSixb(context)).captureOptions()
|
|
52
|
+
} catch (error) {
|
|
53
|
+
return handleRouteError(error, context.set)
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
response: { 200: ActivityCaptureOptionsSchema, ...errors },
|
|
58
|
+
detail: {
|
|
59
|
+
summary: "Read whether this browser session records activity",
|
|
60
|
+
description: "A browser or shared-link session only; a native client's session gets 403.",
|
|
61
|
+
tags: [OPENAPI_TAGS.activity.name],
|
|
62
|
+
operationId: "getActivityCaptureConfig",
|
|
63
|
+
security: [...SIXB_SESSION_SECURITY_REQUIREMENT, SIXB_SHARED_READ_SECURITY_REQUIREMENT],
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
app.post(
|
|
69
|
+
"/api/activity/events",
|
|
70
|
+
async (context) => {
|
|
71
|
+
try {
|
|
72
|
+
assertBrowserCapture(context)
|
|
73
|
+
const capture = getActivityCaptureRuntime(requireRequestSixb(context))
|
|
74
|
+
const result = await capture.capture(context.body.events)
|
|
75
|
+
|
|
76
|
+
if (result.status === "throttled") {
|
|
77
|
+
context.set.status = 429
|
|
78
|
+
context.set.headers["retry-after"] = "60"
|
|
79
|
+
}
|
|
80
|
+
return result
|
|
81
|
+
} catch (error) {
|
|
82
|
+
return handleRouteError(error, context.set)
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
body: CaptureActivityBodySchema,
|
|
87
|
+
response: { 200: ActivityCaptureResultSchema, 429: ActivityCaptureResultSchema, ...errors },
|
|
88
|
+
detail: {
|
|
89
|
+
summary: "Record page views, activity and interactions for this browser session",
|
|
90
|
+
description:
|
|
91
|
+
"A browser or shared-link session only; a native client's session gets 403. The visit belongs to the calling session.",
|
|
92
|
+
tags: [OPENAPI_TAGS.activity.name],
|
|
93
|
+
operationId: "captureActivity",
|
|
94
|
+
security: [
|
|
95
|
+
...SIXB_SESSION_MUTATION_SECURITY_REQUIREMENT,
|
|
96
|
+
SIXB_SHARED_MUTATION_SECURITY_REQUIREMENT,
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
}
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
app.get(
|
|
103
|
+
"/api/activity/visits",
|
|
104
|
+
async (context) => {
|
|
105
|
+
try {
|
|
106
|
+
const page = await requireRequestSixb(context).activity.list(pageQuery(context.query))
|
|
107
|
+
return { visits: page.visits.map(serializeVisit), hasMore: page.hasMore }
|
|
108
|
+
} catch (error) {
|
|
109
|
+
return handleRouteError(error, context.set)
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
query: ActivityPageQuerySchema,
|
|
114
|
+
response: { 200: ActivityVisitsPageSchema, ...errors },
|
|
115
|
+
detail: {
|
|
116
|
+
summary: "List the visits active in a period",
|
|
117
|
+
tags: [OPENAPI_TAGS.activity.name],
|
|
118
|
+
operationId: "listActivityVisits",
|
|
119
|
+
security: accessTokenSecurityRequirement("listActivityVisits"),
|
|
120
|
+
},
|
|
121
|
+
}
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
app.get(
|
|
125
|
+
"/api/activity/users",
|
|
126
|
+
async (context) => {
|
|
127
|
+
try {
|
|
128
|
+
const page = await requireRequestSixb(context).activity.users(pageQuery(context.query))
|
|
129
|
+
return {
|
|
130
|
+
users: page.users.map((user) => ({ ...user, lastSeenAt: user.lastSeenAt.toISOString() })),
|
|
131
|
+
hasMore: page.hasMore,
|
|
132
|
+
}
|
|
133
|
+
} catch (error) {
|
|
134
|
+
return handleRouteError(error, context.set)
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
query: ActivityPageQuerySchema,
|
|
139
|
+
response: { 200: ActivityUsersPageSchema, ...errors },
|
|
140
|
+
detail: {
|
|
141
|
+
summary: "List the signed-in users active in a period",
|
|
142
|
+
tags: [OPENAPI_TAGS.activity.name],
|
|
143
|
+
operationId: "listActivityUsers",
|
|
144
|
+
security: accessTokenSecurityRequirement("listActivityUsers"),
|
|
145
|
+
},
|
|
146
|
+
}
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
app.get(
|
|
150
|
+
"/api/activity/summary",
|
|
151
|
+
async (context) => {
|
|
152
|
+
try {
|
|
153
|
+
return await requireRequestSixb(context).activity.summarize({
|
|
154
|
+
from: new Date(context.query.from),
|
|
155
|
+
to: new Date(context.query.to),
|
|
156
|
+
userId: context.query.userId,
|
|
157
|
+
})
|
|
158
|
+
} catch (error) {
|
|
159
|
+
return handleRouteError(error, context.set)
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
query: ActivityPeriodQuerySchema,
|
|
164
|
+
response: { 200: ActivitySummarySchema, ...errors },
|
|
165
|
+
detail: {
|
|
166
|
+
summary: "Count the users, visits and page views of a period",
|
|
167
|
+
tags: [OPENAPI_TAGS.activity.name],
|
|
168
|
+
operationId: "getActivitySummary",
|
|
169
|
+
security: accessTokenSecurityRequirement("getActivitySummary"),
|
|
170
|
+
},
|
|
171
|
+
}
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
app.get(
|
|
175
|
+
"/api/activity/calendar",
|
|
176
|
+
async (context) => {
|
|
177
|
+
try {
|
|
178
|
+
const calendar = await requireRequestSixb(context).activity.calendar(context.query)
|
|
179
|
+
return { ...calendar, days: [...calendar.days] }
|
|
180
|
+
} catch (error) {
|
|
181
|
+
return handleRouteError(error, context.set)
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
query: ActivityCalendarQuerySchema,
|
|
186
|
+
response: { 200: ActivityCalendarSchema, ...errors },
|
|
187
|
+
detail: {
|
|
188
|
+
summary: "Read a user's active days in a UTC year",
|
|
189
|
+
tags: [OPENAPI_TAGS.activity.name],
|
|
190
|
+
operationId: "getActivityCalendar",
|
|
191
|
+
security: accessTokenSecurityRequirement("getActivityCalendar"),
|
|
192
|
+
},
|
|
193
|
+
}
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
app.get(
|
|
197
|
+
"/api/activity/visits/:visitId/timeline",
|
|
198
|
+
async (context) => {
|
|
199
|
+
try {
|
|
200
|
+
const timeline = await requireRequestSixb(context).activity.timeline(context.params.visitId)
|
|
201
|
+
if (!timeline) {
|
|
202
|
+
context.set.status = 404
|
|
203
|
+
return { error: "Activity visit not found" }
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return serializeTimeline(timeline)
|
|
207
|
+
} catch (error) {
|
|
208
|
+
return handleRouteError(error, context.set)
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
params: ActivityVisitParamsSchema,
|
|
213
|
+
response: { 200: ActivityVisitTimelineSchema, 404: ErrorResponseSchema, ...errors },
|
|
214
|
+
detail: {
|
|
215
|
+
summary: "Read what one visit did",
|
|
216
|
+
description:
|
|
217
|
+
"Its page views and interactions, with the actions, workflows and Agent runs its session started, and the connection it came through. At most 200 page views and interactions, and 200 recorded executions: see truncated.",
|
|
218
|
+
tags: [OPENAPI_TAGS.activity.name],
|
|
219
|
+
operationId: "getActivityVisitTimeline",
|
|
220
|
+
security: accessTokenSecurityRequirement("getActivityVisitTimeline"),
|
|
221
|
+
},
|
|
222
|
+
}
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
app.get(
|
|
226
|
+
"/api/activity/connections",
|
|
227
|
+
async (context) => {
|
|
228
|
+
try {
|
|
229
|
+
const page = await requireRequestSixb(context).activity.connections(context.query)
|
|
230
|
+
return {
|
|
231
|
+
connections: page.connections.map((connection) => ({
|
|
232
|
+
...serializeConnection(connection),
|
|
233
|
+
latestVisits: connection.latestVisits.map(serializeVisit),
|
|
234
|
+
})),
|
|
235
|
+
...(page.nextCursor === undefined ? {} : { nextCursor: page.nextCursor }),
|
|
236
|
+
}
|
|
237
|
+
} catch (error) {
|
|
238
|
+
return handleRouteError(error, context.set)
|
|
239
|
+
}
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
query: ActivityConnectionsQuerySchema,
|
|
243
|
+
response: { 200: ActivityConnectionsPageSchema, ...errors },
|
|
244
|
+
detail: {
|
|
245
|
+
summary: "List a user's sign-ins with the visits each opened",
|
|
246
|
+
description: "Newest first, revoked and expired sessions included.",
|
|
247
|
+
tags: [OPENAPI_TAGS.activity.name],
|
|
248
|
+
operationId: "listActivityConnections",
|
|
249
|
+
security: accessTokenSecurityRequirement("listActivityConnections"),
|
|
250
|
+
},
|
|
251
|
+
}
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
return app
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function pageQuery(query: {
|
|
258
|
+
readonly from: string
|
|
259
|
+
readonly to: string
|
|
260
|
+
readonly userId?: string
|
|
261
|
+
readonly limit?: number
|
|
262
|
+
readonly beforeAt?: string
|
|
263
|
+
readonly beforeId?: string
|
|
264
|
+
}) {
|
|
265
|
+
return {
|
|
266
|
+
from: new Date(query.from),
|
|
267
|
+
to: new Date(query.to),
|
|
268
|
+
userId: query.userId,
|
|
269
|
+
limit: query.limit,
|
|
270
|
+
before:
|
|
271
|
+
query.beforeAt !== undefined && query.beforeId !== undefined
|
|
272
|
+
? { at: new Date(query.beforeAt), id: query.beforeId }
|
|
273
|
+
: undefined,
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function serializeVisit(visit: ActivityVisitView) {
|
|
278
|
+
return {
|
|
279
|
+
...visit,
|
|
280
|
+
startedAt: visit.startedAt.toISOString(),
|
|
281
|
+
lastSeenAt: visit.lastSeenAt.toISOString(),
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function serializeTimeline(timeline: ActivityVisitTimeline) {
|
|
286
|
+
return {
|
|
287
|
+
visit: serializeVisit(timeline.visit),
|
|
288
|
+
...(timeline.connection ? { connection: serializeConnection(timeline.connection) } : {}),
|
|
289
|
+
entries: timeline.entries.map(serializeEntry),
|
|
290
|
+
truncated: timeline.truncated,
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function serializeEntry(entry: ActivityTimelineEntry) {
|
|
295
|
+
const at = entry.at.toISOString()
|
|
296
|
+
if (entry.kind !== "agent") {
|
|
297
|
+
return { ...entry, at }
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const { cost, ...run } = entry
|
|
301
|
+
return { ...run, at, ...(cost ? { cost: { ...cost, amounts: [...cost.amounts] } } : {}) }
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function serializeConnection<T extends ActivityConnection>(connection: T) {
|
|
305
|
+
return {
|
|
306
|
+
...connection,
|
|
307
|
+
createdAt: connection.createdAt.toISOString(),
|
|
308
|
+
...(connection.type === "session" && connection.lastSeenAt
|
|
309
|
+
? { lastSeenAt: connection.lastSeenAt.toISOString() }
|
|
310
|
+
: {}),
|
|
311
|
+
...(connection.revokedAt ? { revokedAt: connection.revokedAt.toISOString() } : {}),
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* A signed-in browser records through its cookie. A native client's bearer session is not a
|
|
317
|
+
* visit; a shared-link session has no caller here and is checked by the runtime.
|
|
318
|
+
*/
|
|
319
|
+
function assertBrowserCapture(context: unknown): void {
|
|
320
|
+
const caller = requestCaller(context)
|
|
321
|
+
if (caller && credentialTransport(caller) !== "cookie") {
|
|
322
|
+
throw new AuthorizationError(
|
|
323
|
+
"activity.capture",
|
|
324
|
+
"[SixbServer] Activity is recorded only through a browser or shared-link session."
|
|
325
|
+
)
|
|
326
|
+
}
|
|
327
|
+
}
|
package/src/routes/agents.ts
CHANGED
|
@@ -285,7 +285,7 @@ async function agentMessageFileContentResponse(
|
|
|
285
285
|
fileRef,
|
|
286
286
|
disposition: parsed.disposition,
|
|
287
287
|
head: options.head,
|
|
288
|
-
|
|
288
|
+
request: context.request,
|
|
289
289
|
})
|
|
290
290
|
if (!response) {
|
|
291
291
|
context.set.status = 404
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { SixbHostView } from "@sixb/core"
|
|
2
|
+
import { parseArtifactAddress } from "@sixb/core/artifacts"
|
|
3
|
+
import {
|
|
4
|
+
ARTIFACT_FRAME_ROUTE_PREFIX,
|
|
5
|
+
FRAME_ASSETS,
|
|
6
|
+
FRAME_REFERENCE_PREFIX,
|
|
7
|
+
injectFrameAssets,
|
|
8
|
+
resolveArtifactFrame,
|
|
9
|
+
rewriteFrameReferences,
|
|
10
|
+
} from "@sixb/core/internal/artifacts/frames"
|
|
11
|
+
import { openFileRef } from "@sixb/core/internal/blob-storage"
|
|
12
|
+
import type { Elysia } from "elysia"
|
|
13
|
+
import { frameContentSecurityPolicy } from "../artifacts/frame"
|
|
14
|
+
import type { ResolvedSixbApiBrowserPolicy } from "../auth/browser-origin"
|
|
15
|
+
import { createFileContentResponse } from "../files/content"
|
|
16
|
+
|
|
17
|
+
interface FrameContext {
|
|
18
|
+
readonly params: { readonly token: string; readonly "*": string }
|
|
19
|
+
readonly request: Request
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Serves a deck's files to its sandboxed frame, under the token a frame link carries: the token
|
|
24
|
+
* is the credential, so the route is public. The entry gets the kit and the runtime, and the kit
|
|
25
|
+
* and runtime are served beside the files. Hidden from OpenAPI: clients receive frame links, they
|
|
26
|
+
* never build them.
|
|
27
|
+
*/
|
|
28
|
+
export function registerArtifactFrameRoutes(
|
|
29
|
+
app: Elysia,
|
|
30
|
+
host: SixbHostView,
|
|
31
|
+
policy: ResolvedSixbApiBrowserPolicy
|
|
32
|
+
) {
|
|
33
|
+
const path = `${ARTIFACT_FRAME_ROUTE_PREFIX}:token/*`
|
|
34
|
+
return app
|
|
35
|
+
.get(path, (context) => frameResponse(host, policy, context), { detail: { hide: true } })
|
|
36
|
+
.head(path, (context) => frameResponse(host, policy, context, { head: true }), {
|
|
37
|
+
detail: { hide: true },
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function frameResponse(
|
|
42
|
+
host: SixbHostView,
|
|
43
|
+
policy: ResolvedSixbApiBrowserPolicy,
|
|
44
|
+
{ params, request }: FrameContext,
|
|
45
|
+
options: { readonly head?: boolean } = {}
|
|
46
|
+
): Promise<Response> {
|
|
47
|
+
const headers = new Headers({
|
|
48
|
+
"content-security-policy": frameContentSecurityPolicy({ policy, request, token: params.token }),
|
|
49
|
+
// The frame's origin is opaque, so its module scripts and fonts arrive with `Origin: null`.
|
|
50
|
+
// Nothing here depends on cookies: the token is the credential.
|
|
51
|
+
"access-control-allow-origin": "*",
|
|
52
|
+
"x-content-type-options": "nosniff",
|
|
53
|
+
"referrer-policy": "no-referrer",
|
|
54
|
+
})
|
|
55
|
+
const notFound = () => {
|
|
56
|
+
headers.set("content-type", "text/plain; charset=utf-8")
|
|
57
|
+
headers.set("cache-control", "no-store")
|
|
58
|
+
return new Response(options.head ? null : "Not found", { status: 404, headers })
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// A frame link opens only in a frame. Opened as a page on the API's origin, its sandbox would
|
|
62
|
+
// still let it show dialogs and open windows that escape the sandbox, under the API's address.
|
|
63
|
+
if (request.headers.get("sec-fetch-dest") === "document") return notFound()
|
|
64
|
+
|
|
65
|
+
const storage = host.storage.artifacts
|
|
66
|
+
if (!storage) return notFound()
|
|
67
|
+
// An unknown or expired token, a deleted version, and one that is no deck all answer one 404.
|
|
68
|
+
const frame = await resolveArtifactFrame({
|
|
69
|
+
projectId: host.id,
|
|
70
|
+
storage,
|
|
71
|
+
token: params.token,
|
|
72
|
+
})
|
|
73
|
+
if (!frame) return notFound()
|
|
74
|
+
let version = frame.version
|
|
75
|
+
|
|
76
|
+
let path = decodePath(params["*"])
|
|
77
|
+
const asset = path === null ? undefined : FRAME_ASSETS[path]
|
|
78
|
+
if (asset) {
|
|
79
|
+
headers.set("content-type", asset.type)
|
|
80
|
+
headers.set("cache-control", "private, max-age=3600")
|
|
81
|
+
return new Response(options.head ? null : asset.body, { headers })
|
|
82
|
+
}
|
|
83
|
+
let lent = false
|
|
84
|
+
if (path?.startsWith(FRAME_REFERENCE_PREFIX)) {
|
|
85
|
+
// A file another artifact lends the deck: only one its frame was issued with, at that version.
|
|
86
|
+
const match = /^([^/@]+)@(\d+)\/(.+)$/.exec(path.slice(FRAME_REFERENCE_PREFIX.length))
|
|
87
|
+
const target = frame.targets.find(
|
|
88
|
+
(candidate) =>
|
|
89
|
+
candidate.artifactId === match?.[1] &&
|
|
90
|
+
candidate.version === Number(match?.[2]) &&
|
|
91
|
+
parseArtifactAddress(candidate.address)?.path === match?.[3]
|
|
92
|
+
)
|
|
93
|
+
const lender = target
|
|
94
|
+
? await storage.getVersion({
|
|
95
|
+
projectId: host.id,
|
|
96
|
+
artifactId: target.artifactId,
|
|
97
|
+
version: target.version,
|
|
98
|
+
})
|
|
99
|
+
: null
|
|
100
|
+
if (!lender || !match) return notFound()
|
|
101
|
+
version = lender
|
|
102
|
+
path = match[3] ?? null
|
|
103
|
+
lent = true
|
|
104
|
+
}
|
|
105
|
+
const file = version.files.find((candidate) => candidate.path === path)
|
|
106
|
+
if (!file) return notFound()
|
|
107
|
+
|
|
108
|
+
if (!lent && file.path === version.entry) {
|
|
109
|
+
const opened = await openFileRef(host.blobStorage, file.file)
|
|
110
|
+
if (!opened) return notFound()
|
|
111
|
+
headers.set("content-type", "text/html; charset=utf-8")
|
|
112
|
+
headers.set("cache-control", "private, no-cache")
|
|
113
|
+
if (options.head) return new Response(null, { headers })
|
|
114
|
+
const text = await new Response(opened.body).text()
|
|
115
|
+
return new Response(injectFrameAssets(rewriteFrameReferences(text, frame.targets)), { headers })
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const response = await createFileContentResponse({
|
|
119
|
+
blobStorage: host.blobStorage,
|
|
120
|
+
fileRef: file.file,
|
|
121
|
+
disposition: "inline",
|
|
122
|
+
head: options.head,
|
|
123
|
+
request,
|
|
124
|
+
})
|
|
125
|
+
if (!response) return notFound()
|
|
126
|
+
// A frame loads its files as the page's parts: inline, under the frame's own policy.
|
|
127
|
+
for (const [name, value] of headers) response.headers.set(name, value)
|
|
128
|
+
response.headers.set("content-disposition", "inline")
|
|
129
|
+
return response
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function decodePath(raw: string | undefined): string | null {
|
|
133
|
+
try {
|
|
134
|
+
return decodeURIComponent(raw ?? "")
|
|
135
|
+
} catch {
|
|
136
|
+
return null
|
|
137
|
+
}
|
|
138
|
+
}
|