@open-pencil/cloud 0.17.0-beta.2 → 0.17.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +9 -9
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +525 -66
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1257 -654
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +1028 -481
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +1780 -31
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +3556 -325
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +929 -72
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +35 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +802 -927
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +89 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +15667 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +7166 -3101
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +6525 -2205
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +12500 -7613
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +8625 -2878
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +27 -9
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +27 -8
- package/src/admin/documents/service.ts +431 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +153 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +28 -461
- package/src/client/auth/client.ts +22 -3
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/billing.ts +80 -0
- package/src/client/community.ts +40 -0
- package/src/client/discovery.ts +35 -9
- package/src/client/documents.ts +160 -0
- package/src/client/index.ts +14 -7
- package/src/client/portal/account.ts +79 -2
- package/src/client/portal/administration.ts +56 -0
- package/src/client/portal/request.ts +2 -0
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +118 -0
- package/src/client/sharing.ts +262 -0
- package/src/client/versions.ts +104 -0
- package/src/contract/account.ts +108 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/billing.ts +166 -0
- package/src/contract/collaboration.ts +11 -1
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +83 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +12 -2
- package/src/contract/index.ts +114 -39
- package/src/contract/links.ts +57 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +235 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +141 -0
- package/src/email/locales/de.json +141 -0
- package/src/email/locales/es.json +141 -0
- package/src/email/locales/fr.json +141 -0
- package/src/email/locales/it.json +141 -0
- package/src/email/locales/ja.json +141 -0
- package/src/email/locales/pl.json +141 -0
- package/src/email/locales/pt-br.json +141 -0
- package/src/email/locales/ru.json +141 -0
- package/src/email/locales/zh-cn.json +141 -0
- package/src/email/messages.ts +200 -0
- package/src/email/render.ts +138 -246
- package/src/email/samples.ts +115 -0
- package/src/email/schema.ts +79 -5
- package/src/email/templates/BrandEmail.vue +54 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +32 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +9 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/errors/posthog.ts +80 -0
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/environment.ts +22 -11
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +69 -15
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +46 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +20 -3
- package/src/runtime/node/email/transport.ts +13 -10
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +50 -16
- package/src/runtime/node/{ai-relay → services}/main.ts +7 -7
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +15 -30
- package/src/runtime/s3/objects.ts +41 -1
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +49 -6
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +304 -0
- package/src/server/account/routes.ts +281 -206
- package/src/server/account/terms.ts +42 -0
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +32 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app/checks.ts +84 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +114 -0
- package/src/server/app/services.ts +185 -0
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/adapter.ts +32 -1
- package/src/server/auth/email.ts +4 -1
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +116 -132
- package/src/server/auth/index.ts +10 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/mfa-assurance.ts +51 -0
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/session.ts +2 -0
- package/src/server/billing/index.ts +6 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +27 -7
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +15 -3
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +77 -122
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/service.ts +48 -33
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +41 -9
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +11 -4
- package/src/server/config/schema.ts +91 -277
- package/src/server/config/toml/schema.ts +307 -0
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/index.ts +1 -0
- package/src/server/db/migrate.ts +46 -116
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/schema.ts +96 -0
- package/src/server/db/tables.ts +82 -4
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +14 -8
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +30 -406
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +94 -28
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +31 -4
- package/src/server/email/types.ts +12 -0
- package/src/server/errors/index.ts +51 -16
- package/src/server/index.ts +52 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/logging.ts +76 -0
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +52 -27
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +40 -12
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +78 -56
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +74 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +6 -2
- package/src/server/reports/index.ts +9 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +39 -0
- package/src/server/reports/service.ts +145 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -23
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +35 -15
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +4 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +91 -52
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +46 -44
- package/src/{ai-relay → services}/config.ts +26 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +35 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/client/ai.ts +0 -96
- package/src/contract/ai.ts +0 -150
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/app.ts +0 -379
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
package/src/contract/ai.ts
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import * as v from 'valibot'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The contract between editors, Cloud, and an AI service. Cloud only advertises the service and
|
|
5
|
-
* signs short-lived tokens for it; the service, official or self-hosted, proxies models and keeps
|
|
6
|
-
* the credit wallet. It is versioned apart from the Cloud protocol so a service can change without
|
|
7
|
-
* a Cloud release.
|
|
8
|
-
*/
|
|
9
|
-
export const CLOUD_AI_CONTRACT_VERSION = '1' as const
|
|
10
|
-
/** The `aud` of every token Cloud signs for an AI service. */
|
|
11
|
-
export const CLOUD_AI_TOKEN_AUDIENCE = 'openpencil-ai' as const
|
|
12
|
-
/** Where Cloud publishes the public keys an AI service verifies its tokens with. */
|
|
13
|
-
export const CLOUD_AI_JWKS_PATH = '/.well-known/jwks.json' as const
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Where a service answers, below the discovered `endpoint`. Model requests follow the OpenAI chat
|
|
17
|
-
* completions API, so an OpenAI-compatible client takes `${endpoint}${base}` as its base URL.
|
|
18
|
-
*/
|
|
19
|
-
export const CLOUD_AI_API_PATHS = {
|
|
20
|
-
base: '/v1',
|
|
21
|
-
chatCompletions: '/v1/chat/completions',
|
|
22
|
-
catalog: '/v1/catalog',
|
|
23
|
-
wallet: '/v1/wallet',
|
|
24
|
-
usage: '/v1/usage'
|
|
25
|
-
} as const
|
|
26
|
-
|
|
27
|
-
const urlSchema = v.pipe(v.string(), v.url())
|
|
28
|
-
const creditsSchema = v.pipe(v.number(), v.integer(), v.minValue(0))
|
|
29
|
-
|
|
30
|
-
export const cloudAIDiscoverySchema = v.object({
|
|
31
|
-
endpoint: urlSchema,
|
|
32
|
-
contractVersion: v.literal(CLOUD_AI_CONTRACT_VERSION)
|
|
33
|
-
})
|
|
34
|
-
export type CloudAIDiscovery = v.InferOutput<typeof cloudAIDiscoverySchema>
|
|
35
|
-
|
|
36
|
-
export const cloudBillingDiscoverySchema = v.object({ portalURL: urlSchema })
|
|
37
|
-
export type CloudBillingDiscovery = v.InferOutput<typeof cloudBillingDiscoverySchema>
|
|
38
|
-
|
|
39
|
-
export const createAITokenSchema = v.object({ workspaceId: v.pipe(v.string(), v.uuid()) })
|
|
40
|
-
export type CreateAITokenInput = v.InferOutput<typeof createAITokenSchema>
|
|
41
|
-
|
|
42
|
-
export function parseCreateAIToken(input: unknown): CreateAITokenInput {
|
|
43
|
-
return v.parse(createAITokenSchema, input)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export const aiTokenSchema = v.object({
|
|
47
|
-
token: v.pipe(v.string(), v.minLength(1)),
|
|
48
|
-
expiresAt: v.pipe(v.string(), v.isoTimestamp())
|
|
49
|
-
})
|
|
50
|
-
export type AIToken = v.InferOutput<typeof aiTokenSchema>
|
|
51
|
-
|
|
52
|
-
/** What an AI service may rely on once a token's signature, issuer, audience, and expiry check. */
|
|
53
|
-
export const aiTokenClaimsSchema = v.object({
|
|
54
|
-
iss: urlSchema,
|
|
55
|
-
aud: v.literal(CLOUD_AI_TOKEN_AUDIENCE),
|
|
56
|
-
sub: v.pipe(v.string(), v.minLength(1)),
|
|
57
|
-
workspaceId: v.pipe(v.string(), v.uuid()),
|
|
58
|
-
/** Credits the workspace's plan grants each month; `null` when Cloud sets no allowance. */
|
|
59
|
-
monthlyCreditAllowance: v.nullable(creditsSchema),
|
|
60
|
-
iat: v.pipe(v.number(), v.integer()),
|
|
61
|
-
exp: v.pipe(v.number(), v.integer())
|
|
62
|
-
})
|
|
63
|
-
export type AITokenClaims = v.InferOutput<typeof aiTokenClaimsSchema>
|
|
64
|
-
|
|
65
|
-
export const aiModelSchema = v.object({
|
|
66
|
-
id: v.pipe(v.string(), v.minLength(1)),
|
|
67
|
-
label: v.pipe(v.string(), v.minLength(1)),
|
|
68
|
-
/** Credits per million input and output tokens. */
|
|
69
|
-
creditsPerMillionInputTokens: creditsSchema,
|
|
70
|
-
creditsPerMillionOutputTokens: creditsSchema,
|
|
71
|
-
contextTokens: v.pipe(v.number(), v.integer(), v.minValue(1)),
|
|
72
|
-
capabilities: v.object({ reasoning: v.boolean(), vision: v.boolean(), tools: v.boolean() })
|
|
73
|
-
})
|
|
74
|
-
export type AIModel = v.InferOutput<typeof aiModelSchema>
|
|
75
|
-
|
|
76
|
-
export const aiCatalogSchema = v.object({
|
|
77
|
-
models: v.array(aiModelSchema),
|
|
78
|
-
defaultModelID: v.pipe(v.string(), v.minLength(1))
|
|
79
|
-
})
|
|
80
|
-
export type AICatalog = v.InferOutput<typeof aiCatalogSchema>
|
|
81
|
-
|
|
82
|
-
export const aiCreditBucketSchema = v.object({
|
|
83
|
-
kind: v.picklist(['allowance', 'purchased', 'grant']),
|
|
84
|
-
remaining: creditsSchema,
|
|
85
|
-
expiresAt: v.nullable(v.pipe(v.string(), v.isoTimestamp()))
|
|
86
|
-
})
|
|
87
|
-
export type AICreditBucket = v.InferOutput<typeof aiCreditBucketSchema>
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* A service records a request's charge before the request's answer ends, so a wallet read after
|
|
91
|
-
* an answer includes what it cost.
|
|
92
|
-
*/
|
|
93
|
-
export const aiWalletSchema = v.object({
|
|
94
|
-
/** Credits left to spend; `null` when the service sets no limit. */
|
|
95
|
-
balance: v.nullable(creditsSchema),
|
|
96
|
-
/** Spent first to last. */
|
|
97
|
-
buckets: v.array(aiCreditBucketSchema)
|
|
98
|
-
})
|
|
99
|
-
export type AIWallet = v.InferOutput<typeof aiWalletSchema>
|
|
100
|
-
|
|
101
|
-
export const aiUsageEntrySchema = v.object({
|
|
102
|
-
id: v.pipe(v.string(), v.minLength(1)),
|
|
103
|
-
createdAt: v.pipe(v.string(), v.isoTimestamp()),
|
|
104
|
-
kind: v.picklist(['model', 'image', 'search']),
|
|
105
|
-
modelID: v.nullable(v.string()),
|
|
106
|
-
userId: v.pipe(v.string(), v.minLength(1)),
|
|
107
|
-
credits: creditsSchema
|
|
108
|
-
})
|
|
109
|
-
export type AIUsageEntry = v.InferOutput<typeof aiUsageEntrySchema>
|
|
110
|
-
|
|
111
|
-
export const aiUsageQuerySchema = v.object({
|
|
112
|
-
cursor: v.optional(v.pipe(v.string(), v.minLength(1)))
|
|
113
|
-
})
|
|
114
|
-
export type AIUsageQuery = v.InferOutput<typeof aiUsageQuerySchema>
|
|
115
|
-
|
|
116
|
-
export function parseAIUsageQuery(input: unknown): AIUsageQuery {
|
|
117
|
-
return v.parse(aiUsageQuerySchema, input)
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export const aiUsagePageSchema = v.object({
|
|
121
|
-
entries: v.array(aiUsageEntrySchema),
|
|
122
|
-
next: v.nullable(v.string())
|
|
123
|
-
})
|
|
124
|
-
export type AIUsagePage = v.InferOutput<typeof aiUsagePageSchema>
|
|
125
|
-
|
|
126
|
-
/** Errors an AI service answers with, beside the upstream model's own. */
|
|
127
|
-
export const aiErrorCodeSchema = v.picklist([
|
|
128
|
-
'unauthorized',
|
|
129
|
-
'insufficient_credits',
|
|
130
|
-
'model_not_allowed',
|
|
131
|
-
'rate_limited'
|
|
132
|
-
])
|
|
133
|
-
export type AIErrorCode = v.InferOutput<typeof aiErrorCodeSchema>
|
|
134
|
-
|
|
135
|
-
export const aiErrorSchema = v.object({
|
|
136
|
-
error: v.object({
|
|
137
|
-
code: aiErrorCodeSchema,
|
|
138
|
-
message: v.optional(v.string()),
|
|
139
|
-
/** With `insufficient_credits`: when the workspace's allowance renews, if the service knows. */
|
|
140
|
-
resetsAt: v.optional(v.pipe(v.string(), v.isoTimestamp()))
|
|
141
|
-
})
|
|
142
|
-
})
|
|
143
|
-
export type AIError = v.InferOutput<typeof aiErrorSchema>
|
|
144
|
-
|
|
145
|
-
export const AI_ERROR_STATUS = {
|
|
146
|
-
unauthorized: 401,
|
|
147
|
-
insufficient_credits: 402,
|
|
148
|
-
model_not_allowed: 403,
|
|
149
|
-
rate_limited: 429
|
|
150
|
-
} as const satisfies Record<AIErrorCode, number>
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { defineComponent, type PropType } from 'vue'
|
|
2
|
-
|
|
3
|
-
import { transactionalEmailLayout } from './layout'
|
|
4
|
-
|
|
5
|
-
export type DocumentInvitationEmailProps = {
|
|
6
|
-
inviterName: string
|
|
7
|
-
documentName: string
|
|
8
|
-
permissionLabel: string
|
|
9
|
-
expiresAt: string
|
|
10
|
-
acceptanceURL: string
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const DocumentInvitationEmail = defineComponent({
|
|
14
|
-
props: {
|
|
15
|
-
inviterName: { type: String, required: true },
|
|
16
|
-
documentName: { type: String, required: true },
|
|
17
|
-
permissionLabel: { type: String, required: true },
|
|
18
|
-
expiresAt: { type: String, required: true },
|
|
19
|
-
acceptanceURL: { type: String, required: true }
|
|
20
|
-
} satisfies Record<
|
|
21
|
-
keyof DocumentInvitationEmailProps,
|
|
22
|
-
{ type: PropType<string>; required: true }
|
|
23
|
-
>,
|
|
24
|
-
setup(props) {
|
|
25
|
-
return () =>
|
|
26
|
-
transactionalEmailLayout({
|
|
27
|
-
preview: `${props.inviterName} invited you to ${props.permissionLabel} a document`,
|
|
28
|
-
heading: 'OpenPencil invitation',
|
|
29
|
-
message: `${props.inviterName} invited you to ${props.permissionLabel} “${props.documentName}”.`,
|
|
30
|
-
actionLabel: 'Open document',
|
|
31
|
-
actionURL: props.acceptanceURL,
|
|
32
|
-
footer: `This invitation expires ${props.expiresAt}.`
|
|
33
|
-
})
|
|
34
|
-
}
|
|
35
|
-
})
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { defineComponent, type PropType } from 'vue'
|
|
2
|
-
|
|
3
|
-
import { transactionalEmailLayout } from './layout'
|
|
4
|
-
|
|
5
|
-
export type EnrollmentEmailProps = {
|
|
6
|
-
heading: string
|
|
7
|
-
preview: string
|
|
8
|
-
message: string
|
|
9
|
-
actionLabel: string
|
|
10
|
-
actionURL: string
|
|
11
|
-
/** Small print under the button. */
|
|
12
|
-
footer?: string
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const EnrollmentEmail = defineComponent({
|
|
16
|
-
props: {
|
|
17
|
-
heading: { type: String, required: true },
|
|
18
|
-
preview: { type: String, required: true },
|
|
19
|
-
message: { type: String, required: true },
|
|
20
|
-
actionLabel: { type: String, required: true },
|
|
21
|
-
actionURL: { type: String, required: true },
|
|
22
|
-
footer: { type: String, required: false }
|
|
23
|
-
} satisfies Record<keyof EnrollmentEmailProps, { type: PropType<string>; required: boolean }>,
|
|
24
|
-
setup(props) {
|
|
25
|
-
return () => transactionalEmailLayout(props)
|
|
26
|
-
}
|
|
27
|
-
})
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { VNode } from 'vue'
|
|
2
|
-
|
|
3
|
-
import { emailLayout } from './brand'
|
|
4
|
-
|
|
5
|
-
export type TransactionalEmailLayout = {
|
|
6
|
-
preview: string
|
|
7
|
-
heading: string
|
|
8
|
-
message: string
|
|
9
|
-
actionLabel: string
|
|
10
|
-
actionURL: string
|
|
11
|
-
footer?: string
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/** Cloud's mail in the same branded layout as the waitlist's, so people know who wrote. */
|
|
15
|
-
export function transactionalEmailLayout(props: TransactionalEmailLayout): VNode {
|
|
16
|
-
const { footer, ...content } = props
|
|
17
|
-
return emailLayout({ ...content, note: footer ? { text: footer } : undefined })
|
|
18
|
-
}
|
package/src/server/ai/errors.ts
DELETED
package/src/server/ai/index.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export { AINotEntitledError } from './errors'
|
|
2
|
-
export { importAISigningKey, loadAISigningKey, type AISigningKey } from './keys'
|
|
3
|
-
export { createAIRoutes, type AIRouteEnvironment } from './routes'
|
|
4
|
-
export { createAITokenService, type AITokenService, type AITokenServiceOptions } from './service'
|
package/src/server/ai/routes.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { parseCreateAIToken } from '#cloud/contract'
|
|
2
|
-
import type { CloudActor } from '#cloud/server/auth'
|
|
3
|
-
import type { CloudDatabase } from '#cloud/server/db'
|
|
4
|
-
import { DocumentNotFoundError } from '#cloud/server/documents'
|
|
5
|
-
import { CLOUD_RATE_LIMITS, createRouteRateLimiter } from '#cloud/server/rate-limit'
|
|
6
|
-
import { validatedJSON } from '#cloud/server/validation'
|
|
7
|
-
import { Hono } from 'hono'
|
|
8
|
-
import type { Kysely } from 'kysely'
|
|
9
|
-
|
|
10
|
-
import { AINotEntitledError } from './errors'
|
|
11
|
-
import type { AITokenService } from './service'
|
|
12
|
-
|
|
13
|
-
export type AIRouteEnvironment = { Variables: { actor: CloudActor } }
|
|
14
|
-
|
|
15
|
-
/** Without a service, the deployment has no AI service and the token route is not found. */
|
|
16
|
-
export function createAIRoutes(
|
|
17
|
-
service: AITokenService | undefined,
|
|
18
|
-
rateLimit?: { database: Kysely<CloudDatabase>; secret: string }
|
|
19
|
-
) {
|
|
20
|
-
const router = new Hono<AIRouteEnvironment>()
|
|
21
|
-
if (service && rateLimit) {
|
|
22
|
-
router.use(
|
|
23
|
-
'/ai/token',
|
|
24
|
-
createRouteRateLimiter(rateLimit.database, rateLimit.secret, CLOUD_RATE_LIMITS.aiToken)
|
|
25
|
-
)
|
|
26
|
-
}
|
|
27
|
-
return router.post('/ai/token', validatedJSON(parseCreateAIToken), async (context) => {
|
|
28
|
-
if (!service) return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
29
|
-
try {
|
|
30
|
-
return context.json(
|
|
31
|
-
await service.issue(context.get('actor'), context.req.valid('json').workspaceId)
|
|
32
|
-
)
|
|
33
|
-
} catch (error) {
|
|
34
|
-
if (error instanceof DocumentNotFoundError) {
|
|
35
|
-
return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
36
|
-
}
|
|
37
|
-
if (error instanceof AINotEntitledError) {
|
|
38
|
-
return context.json({ error: { code: 'forbidden' as const } }, 403)
|
|
39
|
-
}
|
|
40
|
-
throw error
|
|
41
|
-
}
|
|
42
|
-
})
|
|
43
|
-
}
|
package/src/server/app.ts
DELETED
|
@@ -1,379 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createAdminAuditService,
|
|
3
|
-
createAdminEmailService,
|
|
4
|
-
createAdminOperationsService,
|
|
5
|
-
createAdminUserService,
|
|
6
|
-
createCloudAdminRoutes,
|
|
7
|
-
createEnrollmentService,
|
|
8
|
-
type EnrollmentService
|
|
9
|
-
} from '#cloud/admin'
|
|
10
|
-
import {
|
|
11
|
-
CLOUD_AI_JWKS_PATH,
|
|
12
|
-
CLOUD_DISCOVERY_PATH,
|
|
13
|
-
CLOUD_PROTOCOL_VERSION,
|
|
14
|
-
type CloudDiscovery
|
|
15
|
-
} from '#cloud/contract'
|
|
16
|
-
import { createAccountRoutes } from '#cloud/server/account/routes'
|
|
17
|
-
import { createAITokenService, type AISigningKey } from '#cloud/server/ai'
|
|
18
|
-
import {
|
|
19
|
-
createCloudAPIRouter,
|
|
20
|
-
createPublicCloudAPIRouter,
|
|
21
|
-
type CloudAPIEnvironment
|
|
22
|
-
} from '#cloud/server/api'
|
|
23
|
-
import {
|
|
24
|
-
AUTH_BASE_PATH,
|
|
25
|
-
createAuthenticationEmailService,
|
|
26
|
-
createCloudIdentityResolver,
|
|
27
|
-
createCloudSessionResolver,
|
|
28
|
-
type CloudAuthAdapter,
|
|
29
|
-
type CloudIdentityResolver,
|
|
30
|
-
type CloudSessionResolver
|
|
31
|
-
} from '#cloud/server/auth'
|
|
32
|
-
import { createCollaborationTicketService } from '#cloud/server/collaboration'
|
|
33
|
-
import {
|
|
34
|
-
CloudConfigError,
|
|
35
|
-
cloudDiscoveryFromConfig,
|
|
36
|
-
type CloudServerConfig
|
|
37
|
-
} from '#cloud/server/config'
|
|
38
|
-
import type { CloudDatabase } from '#cloud/server/db'
|
|
39
|
-
import { createDocumentService } from '#cloud/server/documents'
|
|
40
|
-
import { createDocumentVersionService } from '#cloud/server/documents/versions'
|
|
41
|
-
import type { TransactionalEmailService } from '#cloud/server/email'
|
|
42
|
-
import { cloudErrorHandler, type CloudErrorReporter } from '#cloud/server/errors'
|
|
43
|
-
import { createInternalRoutes, createInternalService } from '#cloud/server/internal'
|
|
44
|
-
import type { InvitationDelivery, InvitationOutbox } from '#cloud/server/invitations'
|
|
45
|
-
import type { ObjectStore } from '#cloud/server/objects'
|
|
46
|
-
import { createPictureRoutes, createUserPictures } from '#cloud/server/pictures'
|
|
47
|
-
import {
|
|
48
|
-
createConfiguredCloudPolicy,
|
|
49
|
-
createDefaultCloudPolicy,
|
|
50
|
-
createEntitlementService,
|
|
51
|
-
EntitlementOpenFeatureProvider,
|
|
52
|
-
type CloudPolicy,
|
|
53
|
-
type EntitlementSource
|
|
54
|
-
} from '#cloud/server/policy'
|
|
55
|
-
import {
|
|
56
|
-
CLOUD_RATE_LIMITS,
|
|
57
|
-
createActorRateLimiter,
|
|
58
|
-
createTrustedIPRateLimiter,
|
|
59
|
-
routeHasOwnBudget
|
|
60
|
-
} from '#cloud/server/rate-limit'
|
|
61
|
-
import { createDocumentSharingService } from '#cloud/server/sharing'
|
|
62
|
-
import { createWorkspaceMemberService, createWorkspaceService } from '#cloud/server/workspaces'
|
|
63
|
-
import { Hono, type Context } from 'hono'
|
|
64
|
-
import { cors } from 'hono/cors'
|
|
65
|
-
import type { Kysely } from 'kysely'
|
|
66
|
-
|
|
67
|
-
export type CloudServices = {
|
|
68
|
-
config: CloudServerConfig
|
|
69
|
-
database: Kysely<CloudDatabase>
|
|
70
|
-
auth: CloudAuthAdapter
|
|
71
|
-
objects: ObjectStore
|
|
72
|
-
resolveIdentity?: CloudIdentityResolver
|
|
73
|
-
resolveSession?: CloudSessionResolver
|
|
74
|
-
invitationDelivery?: InvitationDelivery
|
|
75
|
-
invitationOutbox?: InvitationOutbox
|
|
76
|
-
transactionalEmail?: TransactionalEmailService
|
|
77
|
-
enrollment?: EnrollmentService
|
|
78
|
-
entitlementSource?: EntitlementSource
|
|
79
|
-
policy?: CloudPolicy
|
|
80
|
-
/** The imported `aiSigningKey`, required when the config names one. See `loadAISigningKey`. */
|
|
81
|
-
aiSigningKey?: AISigningKey
|
|
82
|
-
/**
|
|
83
|
-
* Where clients reach this runtime's collaboration relay. Without one, Cloud rooms fall back to
|
|
84
|
-
* peer-to-peer and writes are not enforced by the server.
|
|
85
|
-
*/
|
|
86
|
-
relayURL?: string
|
|
87
|
-
/** Ends a restored document's live rooms, on the relay this runtime runs, if any. */
|
|
88
|
-
onDocumentRestored?: (documentId: string) => void
|
|
89
|
-
/** Tells a document's live rooms its version history changed, on the relay this runtime runs. */
|
|
90
|
-
onDocumentVersionsChanged?: (documentId: string) => void
|
|
91
|
-
/** Reports errors no route handled, when the deployment configures error tracking. */
|
|
92
|
-
reportError?: CloudErrorReporter
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
type CloudEnvironment = CloudAPIEnvironment
|
|
96
|
-
|
|
97
|
-
function discoveryFromServices(services: CloudServices): CloudDiscovery {
|
|
98
|
-
return cloudDiscoveryFromConfig(services.config)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export function shouldPreventIndexing(path: string, policy: 'allow' | 'deny'): boolean {
|
|
102
|
-
return (
|
|
103
|
-
policy === 'deny' ||
|
|
104
|
-
path.startsWith('/api/') ||
|
|
105
|
-
path.startsWith('/admin') ||
|
|
106
|
-
path.startsWith('/auth') ||
|
|
107
|
-
path.startsWith('/account') ||
|
|
108
|
-
path === '/app' ||
|
|
109
|
-
path === '/sign-in' ||
|
|
110
|
-
path === '/sign-up' ||
|
|
111
|
-
path === '/join' ||
|
|
112
|
-
path === CLOUD_DISCOVERY_PATH
|
|
113
|
-
)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function withIndexingPolicy(
|
|
117
|
-
response: Response,
|
|
118
|
-
path: string,
|
|
119
|
-
policy: 'allow' | 'deny'
|
|
120
|
-
): Response {
|
|
121
|
-
if (!shouldPreventIndexing(path, policy)) return response
|
|
122
|
-
const headers = new Headers(response.headers)
|
|
123
|
-
headers.set('X-Robots-Tag', 'noindex, nofollow, noarchive')
|
|
124
|
-
return new Response(response.body, {
|
|
125
|
-
status: response.status,
|
|
126
|
-
statusText: response.statusText,
|
|
127
|
-
headers
|
|
128
|
-
})
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export function createCloudApp(services: CloudServices) {
|
|
132
|
-
const discovery = discoveryFromServices(services)
|
|
133
|
-
const resolveIdentity = services.resolveIdentity ?? createCloudIdentityResolver(services.auth)
|
|
134
|
-
const resolveSession = services.resolveSession ?? createCloudSessionResolver(services.auth)
|
|
135
|
-
const allowedOrigins = new Set(
|
|
136
|
-
[services.config.publicURL, ...services.config.trustedOrigins].map(
|
|
137
|
-
(origin) => new URL(origin).origin
|
|
138
|
-
)
|
|
139
|
-
)
|
|
140
|
-
const cloudCORS = cors({
|
|
141
|
-
origin: (origin) => (allowedOrigins.has(origin) ? origin : null),
|
|
142
|
-
allowMethods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
|
|
143
|
-
allowHeaders: ['Content-Type', 'Authorization'],
|
|
144
|
-
credentials: true,
|
|
145
|
-
maxAge: 600
|
|
146
|
-
})
|
|
147
|
-
const workspaces = createWorkspaceService(services.database)
|
|
148
|
-
const pictures = createUserPictures(services.database, { publicURL: services.config.publicURL })
|
|
149
|
-
const workspaceMembers = createWorkspaceMemberService(services.database, {
|
|
150
|
-
email: services.transactionalEmail,
|
|
151
|
-
publicURL: services.config.publicURL
|
|
152
|
-
})
|
|
153
|
-
const enrollment =
|
|
154
|
-
services.enrollment ??
|
|
155
|
-
createEnrollmentService(services.database, {
|
|
156
|
-
publicURL: services.config.publicURL,
|
|
157
|
-
adminRecipients: services.config.enrollmentAdminNotificationEmails,
|
|
158
|
-
email: services.transactionalEmail
|
|
159
|
-
})
|
|
160
|
-
const admin = createCloudAdminRoutes(
|
|
161
|
-
{
|
|
162
|
-
email: createAdminEmailService(services.database, services.transactionalEmail, {
|
|
163
|
-
publicURL: services.config.publicURL
|
|
164
|
-
}),
|
|
165
|
-
enrollment,
|
|
166
|
-
users: createAdminUserService(services.database, services.auth, {
|
|
167
|
-
requireMFA: services.config.deploymentAdminMFARequired,
|
|
168
|
-
pictures,
|
|
169
|
-
notify: services.transactionalEmail
|
|
170
|
-
? createAuthenticationEmailService(services.transactionalEmail, services.config.publicURL)
|
|
171
|
-
: undefined
|
|
172
|
-
}),
|
|
173
|
-
audit: createAdminAuditService(services.database),
|
|
174
|
-
operations: createAdminOperationsService(services.database, services.config)
|
|
175
|
-
},
|
|
176
|
-
allowedOrigins,
|
|
177
|
-
{
|
|
178
|
-
database: services.database,
|
|
179
|
-
secret: services.config.authSecret,
|
|
180
|
-
auth: services.auth,
|
|
181
|
-
requireMFA: services.config.deploymentAdminMFARequired
|
|
182
|
-
}
|
|
183
|
-
)
|
|
184
|
-
const policy =
|
|
185
|
-
services.policy ??
|
|
186
|
-
(services.entitlementSource
|
|
187
|
-
? createDefaultCloudPolicy(new EntitlementOpenFeatureProvider(services.entitlementSource))
|
|
188
|
-
: createConfiguredCloudPolicy(services.config, services.database))
|
|
189
|
-
const documents = createDocumentService(services.database, services.objects, {
|
|
190
|
-
policy,
|
|
191
|
-
technicalMaximumUploadBytes: services.config.technicalLimits.maximumUploadBytes,
|
|
192
|
-
onVersionsChanged: services.onDocumentVersionsChanged
|
|
193
|
-
})
|
|
194
|
-
const versions = createDocumentVersionService(services.database, services.objects, {
|
|
195
|
-
policy,
|
|
196
|
-
deploymentMode: services.config.deployment,
|
|
197
|
-
onRestored: services.onDocumentRestored,
|
|
198
|
-
onVersionsChanged: services.onDocumentVersionsChanged
|
|
199
|
-
})
|
|
200
|
-
const entitlements = createEntitlementService(
|
|
201
|
-
services.database,
|
|
202
|
-
policy,
|
|
203
|
-
services.config.technicalLimits.maximumUploadBytes,
|
|
204
|
-
services.config.technicalLimits.maximumConnectionsPerRoom
|
|
205
|
-
)
|
|
206
|
-
const sharing = createDocumentSharingService(services.database, {
|
|
207
|
-
delivery: services.invitationDelivery,
|
|
208
|
-
outbox: services.invitationOutbox,
|
|
209
|
-
continuationSecret: services.config.authSecret,
|
|
210
|
-
publicURL: services.config.publicURL,
|
|
211
|
-
policy,
|
|
212
|
-
deploymentMode: services.config.deployment,
|
|
213
|
-
workspaceInvitations: workspaceMembers
|
|
214
|
-
})
|
|
215
|
-
const collaboration = createCollaborationTicketService({
|
|
216
|
-
database: services.database,
|
|
217
|
-
sharing,
|
|
218
|
-
authSecret: services.config.authSecret,
|
|
219
|
-
relayURL: services.relayURL,
|
|
220
|
-
policy,
|
|
221
|
-
deploymentMode: services.config.deployment
|
|
222
|
-
})
|
|
223
|
-
if (services.config.aiSigningKey && !services.aiSigningKey) {
|
|
224
|
-
throw new CloudConfigError('The AI service needs the signing key imported by loadAISigningKey')
|
|
225
|
-
}
|
|
226
|
-
const aiTokens = services.aiSigningKey
|
|
227
|
-
? createAITokenService({
|
|
228
|
-
database: services.database,
|
|
229
|
-
policy,
|
|
230
|
-
deploymentMode: services.config.deployment,
|
|
231
|
-
issuer: new URL(services.config.publicURL).origin,
|
|
232
|
-
signingKey: services.aiSigningKey,
|
|
233
|
-
lifetimeSeconds: services.config.aiTokenLifetimeSeconds
|
|
234
|
-
})
|
|
235
|
-
: undefined
|
|
236
|
-
|
|
237
|
-
const requireActiveSession = async (context: Context, next: () => Promise<void>) => {
|
|
238
|
-
const actor = await resolveSession(context.req.raw)
|
|
239
|
-
if (!actor) return context.json({ error: { code: 'unauthorized' as const } }, 401)
|
|
240
|
-
context.set('actor', actor)
|
|
241
|
-
return next()
|
|
242
|
-
}
|
|
243
|
-
const publicRateLimiter = createTrustedIPRateLimiter(
|
|
244
|
-
services.database,
|
|
245
|
-
services.config.authSecret,
|
|
246
|
-
CLOUD_RATE_LIMITS.publicCapability,
|
|
247
|
-
{ trustedHeaders: services.config.authTrustedIPHeaders },
|
|
248
|
-
(context) => context.json({ error: { code: 'not_found' as const } }, 404),
|
|
249
|
-
{
|
|
250
|
-
resource: (context) => context.req.path,
|
|
251
|
-
// Guests refresh collaboration tickets on their own budget, `publicCollaborationLimiter`.
|
|
252
|
-
skip: routeHasOwnBudget
|
|
253
|
-
}
|
|
254
|
-
)
|
|
255
|
-
const publicCollaborationLimiter = createTrustedIPRateLimiter(
|
|
256
|
-
services.database,
|
|
257
|
-
services.config.authSecret,
|
|
258
|
-
CLOUD_RATE_LIMITS.collaborationTicket,
|
|
259
|
-
{ trustedHeaders: services.config.authTrustedIPHeaders },
|
|
260
|
-
(context) => context.json({ error: { code: 'not_found' as const } }, 404),
|
|
261
|
-
{ resource: (context) => context.req.path, route: true }
|
|
262
|
-
)
|
|
263
|
-
const authenticatedMutationLimiter = createActorRateLimiter(
|
|
264
|
-
services.database,
|
|
265
|
-
services.config.authSecret,
|
|
266
|
-
CLOUD_RATE_LIMITS.authenticatedMutation,
|
|
267
|
-
// Reads are free, and a route with a budget of its own, such as creating documents, counts
|
|
268
|
-
// against that instead.
|
|
269
|
-
(context) => context.req.method === 'GET' || routeHasOwnBudget(context)
|
|
270
|
-
)
|
|
271
|
-
|
|
272
|
-
const accountRoutes = createAccountRoutes({
|
|
273
|
-
auth: services.auth,
|
|
274
|
-
pictures,
|
|
275
|
-
config: services.config,
|
|
276
|
-
enrollment,
|
|
277
|
-
workspaces,
|
|
278
|
-
resolveIdentity,
|
|
279
|
-
resolveSession,
|
|
280
|
-
requireActiveSession,
|
|
281
|
-
authenticatedMutationLimiter
|
|
282
|
-
})
|
|
283
|
-
const cloudAPI = createCloudAPIRouter({
|
|
284
|
-
collaboration,
|
|
285
|
-
database: services.database,
|
|
286
|
-
rateLimitSecret: services.config.authSecret,
|
|
287
|
-
documents,
|
|
288
|
-
versions,
|
|
289
|
-
entitlements,
|
|
290
|
-
aiTokens,
|
|
291
|
-
sharing,
|
|
292
|
-
workspaces,
|
|
293
|
-
workspaceMembers,
|
|
294
|
-
resolveSession
|
|
295
|
-
})
|
|
296
|
-
const publicCloudAPI = createPublicCloudAPIRouter({
|
|
297
|
-
collaboration,
|
|
298
|
-
documents,
|
|
299
|
-
versions,
|
|
300
|
-
entitlements,
|
|
301
|
-
sharing,
|
|
302
|
-
workspaces,
|
|
303
|
-
workspaceMembers,
|
|
304
|
-
resolveSession
|
|
305
|
-
})
|
|
306
|
-
|
|
307
|
-
// Services such as billing authenticate with their own token, not a session.
|
|
308
|
-
const internalAPI = services.config.internalServiceToken
|
|
309
|
-
? createInternalRoutes(
|
|
310
|
-
createInternalService(services.database),
|
|
311
|
-
services.config.internalServiceToken
|
|
312
|
-
)
|
|
313
|
-
: new Hono()
|
|
314
|
-
|
|
315
|
-
return new Hono<CloudEnvironment>()
|
|
316
|
-
.onError(cloudErrorHandler(services.reportError))
|
|
317
|
-
.use(CLOUD_DISCOVERY_PATH, cloudCORS)
|
|
318
|
-
.use('/api/*', cloudCORS)
|
|
319
|
-
.use('*', async (context, next) => {
|
|
320
|
-
await next()
|
|
321
|
-
if (shouldPreventIndexing(context.req.path, services.config.indexingPolicy)) {
|
|
322
|
-
context.header('X-Robots-Tag', 'noindex, nofollow, noarchive')
|
|
323
|
-
}
|
|
324
|
-
})
|
|
325
|
-
.get('/robots.txt', (context) => {
|
|
326
|
-
context.header('Content-Type', 'text/plain; charset=utf-8')
|
|
327
|
-
return context.body(
|
|
328
|
-
services.config.indexingPolicy === 'deny'
|
|
329
|
-
? 'User-agent: *\nDisallow: /\n'
|
|
330
|
-
: 'User-agent: *\nDisallow: /admin\nDisallow: /account\nDisallow: /app\nDisallow: /api\nDisallow: /auth\nDisallow: /sign-in\nDisallow: /sign-up\nDisallow: /join\nDisallow: /.well-known\n'
|
|
331
|
-
)
|
|
332
|
-
})
|
|
333
|
-
.get('/health', (context) =>
|
|
334
|
-
context.json({
|
|
335
|
-
status: 'ok' as const,
|
|
336
|
-
protocolVersion: CLOUD_PROTOCOL_VERSION
|
|
337
|
-
})
|
|
338
|
-
)
|
|
339
|
-
.get('/ready', async (context) => {
|
|
340
|
-
try {
|
|
341
|
-
await services.database.selectFrom('workspace').select('id').limit(1).execute()
|
|
342
|
-
const objects = await services.objects.checkReadiness()
|
|
343
|
-
if (!objects.ok) {
|
|
344
|
-
return context.json(
|
|
345
|
-
{ status: 'unavailable' as const, dependency: 'object_storage' as const },
|
|
346
|
-
503
|
|
347
|
-
)
|
|
348
|
-
}
|
|
349
|
-
return context.json({
|
|
350
|
-
status: 'ready' as const,
|
|
351
|
-
objectStorage: {
|
|
352
|
-
checksumVerification: objects.checksumVerification,
|
|
353
|
-
multipartUpload: services.objects.capabilities.multipartUpload
|
|
354
|
-
}
|
|
355
|
-
})
|
|
356
|
-
} catch {
|
|
357
|
-
return context.json({ status: 'unavailable' as const }, 503)
|
|
358
|
-
}
|
|
359
|
-
})
|
|
360
|
-
.get(CLOUD_DISCOVERY_PATH, (context) => context.json(discovery))
|
|
361
|
-
.get(CLOUD_AI_JWKS_PATH, async (context) =>
|
|
362
|
-
aiTokens ? context.json(aiTokens.jwks()) : context.notFound()
|
|
363
|
-
)
|
|
364
|
-
.on(['GET', 'POST'], `${AUTH_BASE_PATH}/*`, (context) => services.auth.handler(context.req.raw))
|
|
365
|
-
.route('/', accountRoutes)
|
|
366
|
-
.route('/', createPictureRoutes(pictures))
|
|
367
|
-
.use('/api/shares/:shareId/collaboration-ticket', publicCollaborationLimiter)
|
|
368
|
-
.use('/api/public/*', publicRateLimiter)
|
|
369
|
-
.use('/api/shares/*', publicRateLimiter)
|
|
370
|
-
.use('/api/invitations/*', publicRateLimiter)
|
|
371
|
-
.route('/api', publicCloudAPI)
|
|
372
|
-
.route('/api/internal', internalAPI)
|
|
373
|
-
.use('/api/*', requireActiveSession)
|
|
374
|
-
.route('/api/admin', admin)
|
|
375
|
-
.use('/api/*', authenticatedMutationLimiter)
|
|
376
|
-
.route('/api', cloudAPI)
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
export type CloudApp = ReturnType<typeof createCloudApp>
|