@open-pencil/cloud 0.0.0-stage → 0.16.0
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/LICENSE +202 -0
- package/LICENSE-MIT +21 -0
- package/NOTICE +7 -0
- package/README.md +37 -2
- package/dist/bin/ai-relay.d.mts +1 -0
- package/dist/bin/ai-relay.mjs +31 -0
- package/dist/bin/ai-relay.mjs.map +1 -0
- package/dist/bin/server.d.mts +1 -0
- package/dist/bin/server.mjs +29 -0
- package/dist/bin/server.mjs.map +1 -0
- package/dist/client.d.ts +1520 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +1917 -0
- package/dist/client.js.map +1 -0
- package/dist/contract.d.ts +2 -0
- package/dist/contract.js +2 -0
- package/dist/contract2.js +1453 -0
- package/dist/contract2.js.map +1 -0
- package/dist/email.d.ts +169 -0
- package/dist/email.d.ts.map +1 -0
- package/dist/email.js +670 -0
- package/dist/email.js.map +1 -0
- package/dist/index.d.ts +1755 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/listen.mjs +1881 -0
- package/dist/listen.mjs.map +1 -0
- package/dist/runtime-node.d.mts +10040 -0
- package/dist/runtime-node.d.mts.map +1 -0
- package/dist/runtime-node.mjs +32 -0
- package/dist/runtime-node.mjs.map +1 -0
- package/dist/server.d.ts +12792 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +8475 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +8324 -0
- package/dist/server.mjs.map +1 -0
- package/dist/server2.mjs +505 -0
- package/dist/server2.mjs.map +1 -0
- package/package.json +124 -4
- package/src/admin/api/routes.ts +223 -0
- package/src/admin/api/security.ts +12 -0
- package/src/admin/audit/service.ts +138 -0
- package/src/admin/email/service.ts +140 -0
- package/src/admin/enrollment/email.ts +77 -0
- package/src/admin/enrollment/service.ts +308 -0
- package/src/admin/errors.ts +20 -0
- package/src/admin/index.ts +19 -0
- package/src/admin/list.ts +61 -0
- package/src/admin/operations/service.ts +70 -0
- package/src/admin/schema.ts +28 -0
- package/src/admin/users/service.ts +369 -0
- package/src/ai-relay/app.ts +251 -0
- package/src/ai-relay/config.ts +120 -0
- package/src/ai-relay/db/index.ts +10 -0
- package/src/ai-relay/db/migrate.ts +32 -0
- package/src/ai-relay/db/migrations/001_usage.ts +26 -0
- package/src/ai-relay/db/schema.ts +20 -0
- package/src/ai-relay/index.ts +18 -0
- package/src/ai-relay/metering.ts +68 -0
- package/src/ai-relay/usage.ts +115 -0
- package/src/client/ai.ts +96 -0
- package/src/client/api.ts +528 -0
- package/src/client/auth/client.ts +188 -0
- package/src/client/auth/failure.ts +54 -0
- package/src/client/auth/navigation.ts +45 -0
- package/src/client/device-authorization.ts +134 -0
- package/src/client/discovery.ts +105 -0
- package/src/client/images.ts +81 -0
- package/src/client/index.ts +53 -0
- package/src/client/portal/account.ts +183 -0
- package/src/client/portal/administration.ts +130 -0
- package/src/client/portal/client.ts +18 -0
- package/src/client/portal/request.ts +115 -0
- package/src/client/response.ts +51 -0
- package/src/client/workspaces.ts +142 -0
- package/src/contract/account.ts +204 -0
- package/src/contract/admin/index.ts +187 -0
- package/src/contract/ai.ts +150 -0
- package/src/contract/collaboration.ts +45 -0
- package/src/contract/discovery.ts +81 -0
- package/src/contract/documents.ts +265 -0
- package/src/contract/entitlements.ts +28 -0
- package/src/contract/index.ts +204 -0
- package/src/contract/links.ts +87 -0
- package/src/contract/portal.ts +11 -0
- package/src/contract/relay.ts +368 -0
- package/src/contract/sharing.ts +191 -0
- package/src/contract/versions.ts +87 -0
- package/src/contract/workspaces.ts +100 -0
- package/src/email/index.ts +14 -0
- package/src/email/render.ts +276 -0
- package/src/email/schema.ts +144 -0
- package/src/email/templates/brand.ts +105 -0
- package/src/email/templates/document-invitation.ts +35 -0
- package/src/email/templates/enrollment.ts +27 -0
- package/src/email/templates/layout.ts +18 -0
- package/src/runtime/cloudflare/email.ts +70 -0
- package/src/runtime/cloudflare/environment.ts +57 -0
- package/src/runtime/cloudflare/index.ts +3 -0
- package/src/runtime/cloudflare/relay.ts +136 -0
- package/src/runtime/cloudflare/site.ts +13 -0
- package/src/runtime/cloudflare/tsconfig.json +21 -0
- package/src/runtime/cloudflare/worker.ts +160 -0
- package/src/runtime/node/admin.ts +59 -0
- package/src/runtime/node/ai-relay/main.ts +35 -0
- package/src/runtime/node/ai-relay/server.ts +63 -0
- package/src/runtime/node/application.ts +50 -0
- package/src/runtime/node/bootstrap.ts +22 -0
- package/src/runtime/node/cleanup.ts +24 -0
- package/src/runtime/node/command.ts +17 -0
- package/src/runtime/node/config.ts +16 -0
- package/src/runtime/node/email/runtime.ts +43 -0
- package/src/runtime/node/email/transport.ts +112 -0
- package/src/runtime/node/entitlements/main.ts +41 -0
- package/src/runtime/node/entitlements/refusal.ts +15 -0
- package/src/runtime/node/index.ts +25 -0
- package/src/runtime/node/listen.ts +35 -0
- package/src/runtime/node/main.ts +29 -0
- package/src/runtime/node/migrate.ts +13 -0
- package/src/runtime/node/quota-reconcile.ts +9 -0
- package/src/runtime/node/relay.ts +54 -0
- package/src/runtime/node/server.ts +121 -0
- package/src/runtime/node/site.ts +64 -0
- package/src/runtime/postgres/database.ts +19 -0
- package/src/runtime/s3/objects.ts +244 -0
- package/src/runtime/site/server.ts +82 -0
- package/src/server/account/actions.ts +66 -0
- package/src/server/account/routes.ts +300 -0
- package/src/server/ai/errors.ts +7 -0
- package/src/server/ai/index.ts +4 -0
- package/src/server/ai/keys.ts +43 -0
- package/src/server/ai/routes.ts +43 -0
- package/src/server/ai/service.ts +70 -0
- package/src/server/api/index.ts +8 -0
- package/src/server/api/router.ts +117 -0
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +375 -0
- package/src/server/auth/account.ts +73 -0
- package/src/server/auth/activity.ts +213 -0
- package/src/server/auth/adapter.ts +79 -0
- package/src/server/auth/email.ts +125 -0
- package/src/server/auth/endpoints.ts +57 -0
- package/src/server/auth/factory.ts +583 -0
- package/src/server/auth/index.ts +17 -0
- package/src/server/auth/mfa-assurance.ts +55 -0
- package/src/server/auth/runtime.ts +26 -0
- package/src/server/auth/session.ts +19 -0
- package/src/server/auth/social-providers.ts +55 -0
- package/src/server/auth/user-agent.ts +16 -0
- package/src/server/auth/workspace-access.ts +23 -0
- package/src/server/cleanup/configured.ts +38 -0
- package/src/server/cleanup/documents.ts +231 -0
- package/src/server/cleanup/index.ts +29 -0
- package/src/server/cleanup/objects.ts +27 -0
- package/src/server/cleanup/revisions.ts +232 -0
- package/src/server/cleanup/uploads.ts +129 -0
- package/src/server/cleanup/worker.ts +79 -0
- package/src/server/collaboration/index.ts +28 -0
- package/src/server/collaboration/relay/authorize.ts +53 -0
- package/src/server/collaboration/relay/awareness.ts +104 -0
- package/src/server/collaboration/relay/documents.ts +39 -0
- package/src/server/collaboration/relay/hub.ts +524 -0
- package/src/server/collaboration/relay/index.ts +15 -0
- package/src/server/collaboration/relay/persistence.ts +48 -0
- package/src/server/collaboration/routes.ts +77 -0
- package/src/server/collaboration/service.ts +188 -0
- package/src/server/config/deployment.ts +178 -0
- package/src/server/config/index.ts +20 -0
- package/src/server/config/limits.ts +8 -0
- package/src/server/config/public.ts +66 -0
- package/src/server/config/schema.ts +512 -0
- package/src/server/db/database.ts +15 -0
- package/src/server/db/expressions.ts +13 -0
- package/src/server/db/index.ts +21 -0
- package/src/server/db/migrate.ts +177 -0
- package/src/server/db/migrations/001_foundation.ts +164 -0
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +34 -0
- package/src/server/db/migrations/003_document_cleanup_claims.ts +23 -0
- package/src/server/db/migrations/004_document_sharing.ts +82 -0
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +13 -0
- package/src/server/db/migrations/006_invitation_continuation.ts +27 -0
- package/src/server/db/migrations/007_collaboration_state.ts +25 -0
- package/src/server/db/migrations/008_storage_reservations.ts +37 -0
- package/src/server/db/migrations/009_workspace_entitlements.ts +22 -0
- package/src/server/db/migrations/010_upload_finalization.ts +22 -0
- package/src/server/db/migrations/011_upload_finalization_lease.ts +12 -0
- package/src/server/db/migrations/012_transactional_email.ts +48 -0
- package/src/server/db/migrations/013_admin_enrollment.ts +54 -0
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +30 -0
- package/src/server/db/migrations/015_cloud_rate_limit.ts +22 -0
- package/src/server/db/migrations/016_auth_schema.ts +16 -0
- package/src/server/db/migrations/017_authentication_email_kinds.ts +32 -0
- package/src/server/db/migrations/018_authentication_mfa.ts +78 -0
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +42 -0
- package/src/server/db/migrations/020_workspace_invitations.ts +57 -0
- package/src/server/db/migrations/021_document_versions.ts +66 -0
- package/src/server/db/migrations/022_entitlement_external_version.ts +17 -0
- package/src/server/db/migrations/023_document_images.ts +61 -0
- package/src/server/db/migrations/024_account_activity.ts +66 -0
- package/src/server/db/migrations/025_table_names.ts +58 -0
- package/src/server/db/migrations/026_user_picture.ts +30 -0
- package/src/server/db/migrations/027_document_kind.ts +19 -0
- package/src/server/db/migrations/helpers.ts +78 -0
- package/src/server/db/tables.ts +394 -0
- package/src/server/documents/access.ts +103 -0
- package/src/server/documents/errors.ts +16 -0
- package/src/server/documents/images.ts +407 -0
- package/src/server/documents/index.ts +14 -0
- package/src/server/documents/limits.ts +57 -0
- package/src/server/documents/repository.ts +141 -0
- package/src/server/documents/routes.ts +239 -0
- package/src/server/documents/service.ts +597 -0
- package/src/server/documents/summary.ts +37 -0
- package/src/server/documents/types.ts +14 -0
- package/src/server/documents/versions/checkpoint.ts +98 -0
- package/src/server/documents/versions/history.ts +25 -0
- package/src/server/documents/versions/index.ts +4 -0
- package/src/server/documents/versions/routes.ts +112 -0
- package/src/server/documents/versions/service.ts +397 -0
- package/src/server/email/crypto.ts +57 -0
- package/src/server/email/index.ts +18 -0
- package/src/server/email/invitations.ts +30 -0
- package/src/server/email/service.ts +245 -0
- package/src/server/email/types.ts +56 -0
- package/src/server/email/worker.ts +33 -0
- package/src/server/index.ts +279 -0
- package/src/server/internal/index.ts +8 -0
- package/src/server/internal/routes.ts +41 -0
- package/src/server/internal/schema.ts +10 -0
- package/src/server/internal/service.ts +44 -0
- package/src/server/invitations/index.ts +6 -0
- package/src/server/invitations/types.ts +30 -0
- package/src/server/objects/index.ts +16 -0
- package/src/server/objects/types.ts +81 -0
- package/src/server/pictures/index.ts +9 -0
- package/src/server/pictures/provider.ts +61 -0
- package/src/server/pictures/routes.ts +21 -0
- package/src/server/pictures/service.ts +235 -0
- package/src/server/policy/configured.ts +23 -0
- package/src/server/policy/database.ts +108 -0
- package/src/server/policy/entitlements.ts +29 -0
- package/src/server/policy/index.ts +17 -0
- package/src/server/policy/keys.ts +23 -0
- package/src/server/policy/policy.ts +117 -0
- package/src/server/policy/provider.ts +67 -0
- package/src/server/policy/schema.ts +69 -0
- package/src/server/policy/service.ts +85 -0
- package/src/server/policy/static.ts +62 -0
- package/src/server/quota/index.ts +7 -0
- package/src/server/quota/reconcile.ts +61 -0
- package/src/server/quota/storage.ts +165 -0
- package/src/server/rate-limit/cleanup.ts +16 -0
- package/src/server/rate-limit/index.ts +12 -0
- package/src/server/rate-limit/keys.ts +38 -0
- package/src/server/rate-limit/middleware.ts +137 -0
- package/src/server/rate-limit/postgres.ts +79 -0
- package/src/server/sharing/access.ts +29 -0
- package/src/server/sharing/actor.ts +12 -0
- package/src/server/sharing/capabilities/service.ts +262 -0
- package/src/server/sharing/continuation.ts +85 -0
- package/src/server/sharing/contracts.ts +80 -0
- package/src/server/sharing/crypto.ts +48 -0
- package/src/server/sharing/document-routes.ts +41 -0
- package/src/server/sharing/errors.ts +12 -0
- package/src/server/sharing/grants/service.ts +79 -0
- package/src/server/sharing/http.ts +41 -0
- package/src/server/sharing/index.ts +39 -0
- package/src/server/sharing/invitations/service.ts +315 -0
- package/src/server/sharing/policy.ts +44 -0
- package/src/server/sharing/routes.ts +251 -0
- package/src/server/sharing/service.ts +117 -0
- package/src/server/validation.ts +24 -0
- package/src/server/worker/interval.ts +23 -0
- package/src/server/worker/periodic.ts +42 -0
- package/src/server/workspaces/index.ts +16 -0
- package/src/server/workspaces/members.ts +502 -0
- package/src/server/workspaces/repository.ts +91 -0
- package/src/server/workspaces/routes.ts +198 -0
- package/src/server/workspaces/service.ts +84 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { isValid as isBase64 } from 'js-base64'
|
|
2
|
+
import * as v from 'valibot'
|
|
3
|
+
|
|
4
|
+
export const cloudAccountStateSchema = v.picklist(['active', 'pending', 'rejected', 'revoked'])
|
|
5
|
+
export const cloudIdentitySchema = v.object({
|
|
6
|
+
userId: v.string(),
|
|
7
|
+
email: v.pipe(v.string(), v.email()),
|
|
8
|
+
name: v.string(),
|
|
9
|
+
/** The account's picture, such as one a social provider gave it. */
|
|
10
|
+
image: v.optional(v.nullable(v.string())),
|
|
11
|
+
deploymentRole: v.optional(v.picklist(['user', 'admin']))
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
export const cloudAccountStatusResponseSchema = v.object({
|
|
15
|
+
user: cloudIdentitySchema,
|
|
16
|
+
state: cloudAccountStateSchema
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
export const cloudAuthenticationProviderSchema = v.picklist(['credential', 'google', 'apple'])
|
|
20
|
+
export const cloudAuthenticationMethodSchema = v.object({
|
|
21
|
+
id: v.string(),
|
|
22
|
+
provider: cloudAuthenticationProviderSchema,
|
|
23
|
+
createdAt: v.string(),
|
|
24
|
+
canUnlink: v.boolean()
|
|
25
|
+
})
|
|
26
|
+
export const cloudAuthenticationMethodsResponseSchema = v.object({
|
|
27
|
+
methods: v.array(cloudAuthenticationMethodSchema),
|
|
28
|
+
availableSocialProviders: v.array(v.picklist(['google', 'apple']))
|
|
29
|
+
})
|
|
30
|
+
export const cloudPasswordChangeSchema = v.object({
|
|
31
|
+
currentPassword: v.string(),
|
|
32
|
+
newPassword: v.string()
|
|
33
|
+
})
|
|
34
|
+
export const cloudSocialLinkSchema = v.object({
|
|
35
|
+
provider: v.picklist(['google', 'apple']),
|
|
36
|
+
callbackURL: v.pipe(v.string(), v.url())
|
|
37
|
+
})
|
|
38
|
+
export const cloudSocialLinkResponseSchema = v.object({ url: v.pipe(v.string(), v.url()) })
|
|
39
|
+
export const cloudUnlinkAuthenticationMethodSchema = v.object({ methodId: v.string() })
|
|
40
|
+
export const accountSecurityErrorCodeSchema = v.picklist([
|
|
41
|
+
'current_password_invalid',
|
|
42
|
+
'password_too_short',
|
|
43
|
+
'password_too_long',
|
|
44
|
+
'last_authentication_method',
|
|
45
|
+
'session_not_fresh',
|
|
46
|
+
'authentication_method_failed',
|
|
47
|
+
'email_unchanged',
|
|
48
|
+
'email_change_unavailable',
|
|
49
|
+
'current_session',
|
|
50
|
+
'picture_invalid'
|
|
51
|
+
])
|
|
52
|
+
export const accountSecurityErrorResponseSchema = v.object({
|
|
53
|
+
error: v.object({ code: accountSecurityErrorCodeSchema })
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
export const cloudMFAStatusSchema = v.object({
|
|
57
|
+
required: v.boolean(),
|
|
58
|
+
enabled: v.boolean(),
|
|
59
|
+
totpEnabled: v.boolean(),
|
|
60
|
+
assured: v.boolean(),
|
|
61
|
+
totpAvailable: v.boolean(),
|
|
62
|
+
passkeysAvailable: v.boolean(),
|
|
63
|
+
recoveryCodesAvailable: v.boolean()
|
|
64
|
+
})
|
|
65
|
+
export const cloudMFAStatusResponseSchema = v.object({ mfa: cloudMFAStatusSchema })
|
|
66
|
+
export const cloudMFAEnableSchema = v.object({ password: v.optional(v.string()) })
|
|
67
|
+
export const cloudMFAEnableResponseSchema = v.object({
|
|
68
|
+
totpURI: v.string(),
|
|
69
|
+
backupCodes: v.array(v.string())
|
|
70
|
+
})
|
|
71
|
+
export const cloudMFAVerifySchema = v.object({ code: v.pipe(v.string(), v.trim(), v.minLength(1)) })
|
|
72
|
+
export const cloudMFAVerifyResponseSchema = v.object({ ok: v.literal(true) })
|
|
73
|
+
export const cloudMFARecoveryCodesResponseSchema = v.object({ backupCodes: v.array(v.string()) })
|
|
74
|
+
export const cloudPasskeySchema = v.object({
|
|
75
|
+
id: v.string(),
|
|
76
|
+
name: v.nullish(v.string()),
|
|
77
|
+
createdAt: v.string()
|
|
78
|
+
})
|
|
79
|
+
export const cloudPasskeysResponseSchema = v.object({ passkeys: v.array(cloudPasskeySchema) })
|
|
80
|
+
export const cloudPasskeyDeleteSchema = v.object({ id: v.string() })
|
|
81
|
+
|
|
82
|
+
export function parseCloudMFAEnable(input: unknown) {
|
|
83
|
+
return v.parse(cloudMFAEnableSchema, input)
|
|
84
|
+
}
|
|
85
|
+
export function parseCloudMFAVerify(input: unknown) {
|
|
86
|
+
return v.parse(cloudMFAVerifySchema, input)
|
|
87
|
+
}
|
|
88
|
+
export function parseCloudPasskeyDelete(input: unknown) {
|
|
89
|
+
return v.parse(cloudPasskeyDeleteSchema, input)
|
|
90
|
+
}
|
|
91
|
+
export function parseCloudPasswordChange(input: unknown) {
|
|
92
|
+
return v.parse(cloudPasswordChangeSchema, input)
|
|
93
|
+
}
|
|
94
|
+
export function parseCloudSocialLink(input: unknown) {
|
|
95
|
+
return v.parse(cloudSocialLinkSchema, input)
|
|
96
|
+
}
|
|
97
|
+
export function parseCloudUnlinkAuthenticationMethod(input: unknown) {
|
|
98
|
+
return v.parse(cloudUnlinkAuthenticationMethodSchema, input)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export const cloudAccountMutationResponseSchema = v.object({ ok: v.literal(true) })
|
|
102
|
+
|
|
103
|
+
/** Who an account belongs to, as the portal shows it. */
|
|
104
|
+
export const cloudPersonSchema = v.object({
|
|
105
|
+
id: v.string(),
|
|
106
|
+
name: v.string(),
|
|
107
|
+
email: v.pipe(v.string(), v.email()),
|
|
108
|
+
/** The address of the account's picture, which the server serves; null shows initials. */
|
|
109
|
+
image: v.nullable(v.string())
|
|
110
|
+
})
|
|
111
|
+
/** What a person can see and change about themselves. */
|
|
112
|
+
export const cloudProfileSchema = v.object({
|
|
113
|
+
...cloudPersonSchema.entries,
|
|
114
|
+
createdAt: v.string(),
|
|
115
|
+
/** Whether the server can send the verification a new email address needs. */
|
|
116
|
+
emailChangeAvailable: v.boolean()
|
|
117
|
+
})
|
|
118
|
+
export const cloudProfileResponseSchema = v.object({ profile: cloudProfileSchema })
|
|
119
|
+
export const cloudProfileUpdateSchema = v.object({
|
|
120
|
+
name: v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(100))
|
|
121
|
+
})
|
|
122
|
+
/** The largest picture an account may upload, once the page has cropped and shrunk it. */
|
|
123
|
+
export const CLOUD_PICTURE_MAX_BYTES = 256 * 1024
|
|
124
|
+
/** The width and height a page shrinks a picture to before it uploads it. */
|
|
125
|
+
export const CLOUD_PICTURE_SIZE = 256
|
|
126
|
+
export const cloudPictureUploadSchema = v.object({
|
|
127
|
+
/** The picture's PNG or JPEG bytes, in Base64. */
|
|
128
|
+
data: v.pipe(
|
|
129
|
+
v.string(),
|
|
130
|
+
v.maxLength(Math.ceil(CLOUD_PICTURE_MAX_BYTES / 3) * 4),
|
|
131
|
+
v.check((value) => isBase64(value))
|
|
132
|
+
)
|
|
133
|
+
})
|
|
134
|
+
/** The address the account's picture is now served from. */
|
|
135
|
+
export const cloudPictureResponseSchema = v.object({ image: v.string() })
|
|
136
|
+
export const cloudEmailChangeSchema = v.object({
|
|
137
|
+
newEmail: v.pipe(v.string(), v.trim(), v.toLowerCase(), v.email()),
|
|
138
|
+
callbackURL: v.pipe(v.string(), v.url())
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
/** How a sign-in was made: the method it used, or the desktop app's approved code. */
|
|
142
|
+
export const cloudSignInMethodSchema = v.picklist([
|
|
143
|
+
'password',
|
|
144
|
+
'google',
|
|
145
|
+
'apple',
|
|
146
|
+
'passkey',
|
|
147
|
+
'two-step',
|
|
148
|
+
'email-link',
|
|
149
|
+
'desktop',
|
|
150
|
+
'other'
|
|
151
|
+
])
|
|
152
|
+
export const cloudAccountSessionSchema = v.object({
|
|
153
|
+
id: v.string(),
|
|
154
|
+
/** The session this request was made with. */
|
|
155
|
+
current: v.boolean(),
|
|
156
|
+
method: v.nullable(cloudSignInMethodSchema),
|
|
157
|
+
ipAddress: v.nullable(v.string()),
|
|
158
|
+
/** The browser and system the server read from the client's user agent, such as `Chrome 141`. */
|
|
159
|
+
browser: v.nullable(v.string()),
|
|
160
|
+
system: v.nullable(v.string()),
|
|
161
|
+
createdAt: v.string(),
|
|
162
|
+
lastActiveAt: v.string(),
|
|
163
|
+
expiresAt: v.string()
|
|
164
|
+
})
|
|
165
|
+
export const cloudAccountSessionsResponseSchema = v.object({
|
|
166
|
+
sessions: v.array(cloudAccountSessionSchema)
|
|
167
|
+
})
|
|
168
|
+
export const cloudSessionRevokeSchema = v.object({ sessionId: v.pipe(v.string(), v.minLength(1)) })
|
|
169
|
+
export const cloudSignInSchema = v.object({
|
|
170
|
+
id: v.string(),
|
|
171
|
+
method: cloudSignInMethodSchema,
|
|
172
|
+
ipAddress: v.nullable(v.string()),
|
|
173
|
+
/** The browser and system the server read from the client's user agent, such as `Chrome 141`. */
|
|
174
|
+
browser: v.nullable(v.string()),
|
|
175
|
+
system: v.nullable(v.string()),
|
|
176
|
+
createdAt: v.string()
|
|
177
|
+
})
|
|
178
|
+
export const cloudSignInsResponseSchema = v.object({ signIns: v.array(cloudSignInSchema) })
|
|
179
|
+
|
|
180
|
+
export function parseCloudProfileUpdate(input: unknown) {
|
|
181
|
+
return v.parse(cloudProfileUpdateSchema, input)
|
|
182
|
+
}
|
|
183
|
+
export function parseCloudPictureUpload(input: unknown) {
|
|
184
|
+
return v.parse(cloudPictureUploadSchema, input)
|
|
185
|
+
}
|
|
186
|
+
export function parseCloudEmailChange(input: unknown) {
|
|
187
|
+
return v.parse(cloudEmailChangeSchema, input)
|
|
188
|
+
}
|
|
189
|
+
export function parseCloudSessionRevoke(input: unknown) {
|
|
190
|
+
return v.parse(cloudSessionRevokeSchema, input)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export type AccountSecurityErrorCode = v.InferOutput<typeof accountSecurityErrorCodeSchema>
|
|
194
|
+
export type CloudAuthenticationProvider = v.InferOutput<typeof cloudAuthenticationProviderSchema>
|
|
195
|
+
export type CloudAuthenticationMethod = v.InferOutput<typeof cloudAuthenticationMethodSchema>
|
|
196
|
+
export type CloudAuthenticationMethods = v.InferOutput<
|
|
197
|
+
typeof cloudAuthenticationMethodsResponseSchema
|
|
198
|
+
>
|
|
199
|
+
|
|
200
|
+
export type CloudAccountStatus = v.InferOutput<typeof cloudAccountStatusResponseSchema>
|
|
201
|
+
export type CloudProfile = v.InferOutput<typeof cloudProfileSchema>
|
|
202
|
+
export type CloudSignInMethod = v.InferOutput<typeof cloudSignInMethodSchema>
|
|
203
|
+
export type CloudAccountSession = v.InferOutput<typeof cloudAccountSessionSchema>
|
|
204
|
+
export type CloudSignIn = v.InferOutput<typeof cloudSignInSchema>
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { zipObject } from 'es-toolkit'
|
|
2
|
+
import * as v from 'valibot'
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
cloudIdentitySchema,
|
|
6
|
+
cloudAccountSessionSchema,
|
|
7
|
+
cloudPersonSchema,
|
|
8
|
+
cloudSignInSchema
|
|
9
|
+
} from '../account'
|
|
10
|
+
import { workspaceRoleSchema } from '../workspaces'
|
|
11
|
+
|
|
12
|
+
const timestampSchema = v.pipe(v.string(), v.isoTimestamp())
|
|
13
|
+
const nullableTimestampSchema = v.nullable(timestampSchema)
|
|
14
|
+
const countSchema = v.pipe(v.number(), v.integer(), v.minValue(0))
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* What every admin list answers besides its rows: how many rows match the search and filter
|
|
18
|
+
* across every page, and how many match the search under each filter.
|
|
19
|
+
*/
|
|
20
|
+
function listTotals<const Filter extends string>(filters: readonly Filter[]) {
|
|
21
|
+
return {
|
|
22
|
+
total: countSchema,
|
|
23
|
+
counts: v.object(
|
|
24
|
+
zipObject(
|
|
25
|
+
filters,
|
|
26
|
+
filters.map(() => countSchema)
|
|
27
|
+
)
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const adminErrorCodeSchema = v.picklist([
|
|
33
|
+
'bad_request',
|
|
34
|
+
'unauthorized',
|
|
35
|
+
'forbidden',
|
|
36
|
+
'not_found',
|
|
37
|
+
'rate_limited',
|
|
38
|
+
'invalid_enrollment_transition',
|
|
39
|
+
'last_admin_required',
|
|
40
|
+
'self_admin_action_forbidden',
|
|
41
|
+
'email_regeneration_unavailable',
|
|
42
|
+
'mfa_required'
|
|
43
|
+
])
|
|
44
|
+
|
|
45
|
+
export const adminErrorResponseSchema = v.object({
|
|
46
|
+
error: v.object({ code: adminErrorCodeSchema })
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
export const enrollmentStatusSchema = v.picklist(['pending', 'approved', 'rejected', 'revoked'])
|
|
50
|
+
export const enrollmentSchema = v.object({
|
|
51
|
+
id: v.string(),
|
|
52
|
+
email: v.pipe(v.string(), v.email()),
|
|
53
|
+
name: v.nullable(v.string()),
|
|
54
|
+
reason: v.nullable(v.string()),
|
|
55
|
+
status: enrollmentStatusSchema,
|
|
56
|
+
requestedAt: timestampSchema,
|
|
57
|
+
reviewedAt: nullableTimestampSchema,
|
|
58
|
+
reviewedBy: v.nullable(v.string()),
|
|
59
|
+
/** The reviewer's name, in lists, while the server still knows them. */
|
|
60
|
+
reviewerName: v.optional(v.nullable(v.string())),
|
|
61
|
+
reviewNote: v.nullable(v.string()),
|
|
62
|
+
approvedUserId: v.nullable(v.string()),
|
|
63
|
+
requestRevision: v.pipe(v.number(), v.integer(), v.minValue(1))
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
export const enrollmentsResponseSchema = v.object({
|
|
67
|
+
enrollments: v.array(enrollmentSchema),
|
|
68
|
+
...listTotals(enrollmentStatusSchema.options)
|
|
69
|
+
})
|
|
70
|
+
export const enrollmentReviewResponseSchema = v.object({ enrollment: enrollmentSchema })
|
|
71
|
+
|
|
72
|
+
export const cloudAdminSessionResponseSchema = v.object({ user: cloudIdentitySchema })
|
|
73
|
+
|
|
74
|
+
export const cloudAdminUserSchema = v.object({
|
|
75
|
+
...cloudPersonSchema.entries,
|
|
76
|
+
role: v.optional(v.string()),
|
|
77
|
+
banned: v.nullable(v.boolean()),
|
|
78
|
+
banReason: v.optional(v.nullable(v.string())),
|
|
79
|
+
createdAt: timestampSchema,
|
|
80
|
+
/** An authenticator app or a passkey confirms this account's sign-ins. */
|
|
81
|
+
twoStep: v.boolean(),
|
|
82
|
+
/** When one of the account's sessions was last used, or null when none is left. */
|
|
83
|
+
lastActiveAt: nullableTimestampSchema,
|
|
84
|
+
workspaceCount: countSchema
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
/** Which accounts a list of people shows. */
|
|
88
|
+
export const cloudAdminUserFilterSchema = v.picklist([
|
|
89
|
+
'all',
|
|
90
|
+
'administrators',
|
|
91
|
+
'suspended',
|
|
92
|
+
'without-two-step'
|
|
93
|
+
])
|
|
94
|
+
export const cloudAdminUsersResponseSchema = v.object({
|
|
95
|
+
users: v.array(cloudAdminUserSchema),
|
|
96
|
+
...listTotals(cloudAdminUserFilterSchema.options)
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
export const cloudAdminUserDetailSchema = v.object({
|
|
100
|
+
user: v.object({
|
|
101
|
+
...cloudAdminUserSchema.entries,
|
|
102
|
+
emailVerified: v.boolean()
|
|
103
|
+
}),
|
|
104
|
+
methods: v.array(v.object({ provider: v.string(), createdAt: timestampSchema })),
|
|
105
|
+
authenticator: v.boolean(),
|
|
106
|
+
passkeyCount: countSchema,
|
|
107
|
+
sessions: v.array(cloudAccountSessionSchema),
|
|
108
|
+
signIns: v.array(cloudSignInSchema),
|
|
109
|
+
workspaces: v.array(
|
|
110
|
+
v.object({
|
|
111
|
+
id: v.string(),
|
|
112
|
+
name: v.string(),
|
|
113
|
+
role: workspaceRoleSchema,
|
|
114
|
+
memberCount: countSchema,
|
|
115
|
+
joinedAt: timestampSchema
|
|
116
|
+
})
|
|
117
|
+
)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
export const adminMutationResponseSchema = v.object({ ok: v.literal(true) })
|
|
121
|
+
|
|
122
|
+
export const transactionalEmailAdminSchema = v.object({
|
|
123
|
+
id: v.string(),
|
|
124
|
+
kind: v.string(),
|
|
125
|
+
recipientEmailNormalized: v.pipe(v.string(), v.email()),
|
|
126
|
+
status: v.string(),
|
|
127
|
+
attemptCount: v.pipe(v.number(), v.integer(), v.minValue(0)),
|
|
128
|
+
nextAttemptAt: timestampSchema,
|
|
129
|
+
transport: v.nullable(v.string()),
|
|
130
|
+
transportMessageId: v.nullable(v.string()),
|
|
131
|
+
lastErrorCode: v.nullable(v.string()),
|
|
132
|
+
createdAt: timestampSchema,
|
|
133
|
+
acceptedAt: nullableTimestampSchema,
|
|
134
|
+
regeneratable: v.boolean()
|
|
135
|
+
})
|
|
136
|
+
/** Which messages a list of email shows, by how their delivery went. */
|
|
137
|
+
export const cloudAdminEmailFilterSchema = v.picklist(['all', 'sent', 'waiting', 'failed'])
|
|
138
|
+
export const adminEmailResponseSchema = v.object({
|
|
139
|
+
messages: v.array(transactionalEmailAdminSchema),
|
|
140
|
+
...listTotals(cloudAdminEmailFilterSchema.options)
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
/** Who or what an event names; `name` is null once the server no longer knows it. */
|
|
144
|
+
const auditPartySchema = v.object({ id: v.string(), name: v.nullable(v.string()) })
|
|
145
|
+
|
|
146
|
+
export const adminAuditEventSchema = v.object({
|
|
147
|
+
id: v.string(),
|
|
148
|
+
actor: auditPartySchema,
|
|
149
|
+
action: v.string(),
|
|
150
|
+
/** What the event is about: an account, an access request, or an email. */
|
|
151
|
+
subject: v.object({ ...auditPartySchema.entries, type: v.string() }),
|
|
152
|
+
metadata: v.unknown(),
|
|
153
|
+
createdAt: timestampSchema
|
|
154
|
+
})
|
|
155
|
+
/** Which events a list of activity shows, by what they are about. */
|
|
156
|
+
export const cloudAdminAuditFilterSchema = v.picklist(['all', 'accounts', 'requests', 'email'])
|
|
157
|
+
export const adminAuditResponseSchema = v.object({
|
|
158
|
+
events: v.array(adminAuditEventSchema),
|
|
159
|
+
...listTotals(cloudAdminAuditFilterSchema.options)
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
export const adminOperationsResponseSchema = v.object({
|
|
163
|
+
deployment: v.picklist(['official', 'self-hosted']),
|
|
164
|
+
enrollmentMode: v.picklist(['open', 'approval', 'closed']),
|
|
165
|
+
emailTransport: v.picklist(['none', 'smtp', 'cloudflare']),
|
|
166
|
+
pendingEnrollment: v.pipe(v.number(), v.integer(), v.minValue(0)),
|
|
167
|
+
pendingEmail: v.pipe(v.number(), v.integer(), v.minValue(0)),
|
|
168
|
+
failedEmail: v.pipe(v.number(), v.integer(), v.minValue(0)),
|
|
169
|
+
accounts: countSchema,
|
|
170
|
+
workspaces: countSchema,
|
|
171
|
+
documents: countSchema,
|
|
172
|
+
storedBytes: countSchema
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
export type AdminErrorCode = v.InferOutput<typeof adminErrorCodeSchema>
|
|
176
|
+
export type Enrollment = v.InferOutput<typeof enrollmentSchema>
|
|
177
|
+
export type CloudAdminSessionResponse = v.InferOutput<typeof cloudAdminSessionResponseSchema>
|
|
178
|
+
export type CloudAdminUser = v.InferOutput<typeof cloudAdminUserSchema>
|
|
179
|
+
export type CloudAdminUserFilter = v.InferOutput<typeof cloudAdminUserFilterSchema>
|
|
180
|
+
export type CloudAdminEmailFilter = v.InferOutput<typeof cloudAdminEmailFilterSchema>
|
|
181
|
+
export type CloudAdminAuditFilter = v.InferOutput<typeof cloudAdminAuditFilterSchema>
|
|
182
|
+
export type EnrollmentStatus = v.InferOutput<typeof enrollmentStatusSchema>
|
|
183
|
+
export type CloudAdminUsers = v.InferOutput<typeof cloudAdminUsersResponseSchema>
|
|
184
|
+
export type CloudAdminUserDetail = v.InferOutput<typeof cloudAdminUserDetailSchema>
|
|
185
|
+
export type TransactionalEmailAdmin = v.InferOutput<typeof transactionalEmailAdminSchema>
|
|
186
|
+
export type AdminAuditEvent = v.InferOutput<typeof adminAuditEventSchema>
|
|
187
|
+
export type AdminOperations = v.InferOutput<typeof adminOperationsResponseSchema>
|
|
@@ -0,0 +1,150 @@
|
|
|
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>
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as v from 'valibot'
|
|
2
|
+
|
|
3
|
+
import { documentPermissionSchema } from './documents'
|
|
4
|
+
|
|
5
|
+
export const collaborationPrincipalSchema = v.variant('kind', [
|
|
6
|
+
v.object({
|
|
7
|
+
kind: v.literal('user'),
|
|
8
|
+
userId: v.string(),
|
|
9
|
+
name: v.string(),
|
|
10
|
+
email: v.pipe(v.string(), v.email())
|
|
11
|
+
}),
|
|
12
|
+
v.object({
|
|
13
|
+
kind: v.literal('guest'),
|
|
14
|
+
guestId: v.string(),
|
|
15
|
+
name: v.string()
|
|
16
|
+
})
|
|
17
|
+
])
|
|
18
|
+
export type CollaborationPrincipal = v.InferOutput<typeof collaborationPrincipalSchema>
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Who the relay verified a peer to be, as it writes into that peer's presence under `cloud`, so
|
|
22
|
+
* other peers can trust it over what the peer says of itself.
|
|
23
|
+
*/
|
|
24
|
+
export const cloudPresenceSchema = v.object({
|
|
25
|
+
kind: v.picklist(['user', 'guest']),
|
|
26
|
+
id: v.string(),
|
|
27
|
+
name: v.string(),
|
|
28
|
+
permission: documentPermissionSchema
|
|
29
|
+
})
|
|
30
|
+
export type CloudPresence = v.InferOutput<typeof cloudPresenceSchema>
|
|
31
|
+
|
|
32
|
+
export const collaborationTicketSchema = v.object({
|
|
33
|
+
token: v.string(),
|
|
34
|
+
provider: v.optional(v.picklist(['trystero', 'relay'])),
|
|
35
|
+
serverURL: v.optional(v.pipe(v.string(), v.url())),
|
|
36
|
+
documentId: v.pipe(v.string(), v.uuid()),
|
|
37
|
+
roomId: v.string(),
|
|
38
|
+
roomKey: v.pipe(v.string(), v.minLength(43), v.maxLength(128)),
|
|
39
|
+
principal: collaborationPrincipalSchema,
|
|
40
|
+
permission: documentPermissionSchema,
|
|
41
|
+
roomEpoch: v.pipe(v.number(), v.integer(), v.minValue(0)),
|
|
42
|
+
expiresAt: v.string(),
|
|
43
|
+
serverEnforcedWrites: v.boolean()
|
|
44
|
+
})
|
|
45
|
+
export type CollaborationTicket = v.InferOutput<typeof collaborationTicketSchema>
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import * as v from 'valibot'
|
|
2
|
+
|
|
3
|
+
import { cloudAIDiscoverySchema, cloudBillingDiscoverySchema } from './ai'
|
|
4
|
+
|
|
5
|
+
export const CLOUD_PROTOCOL_VERSION = '1' as const
|
|
6
|
+
export const CLOUD_DISCOVERY_PATH = '/.well-known/openpencil' as const
|
|
7
|
+
|
|
8
|
+
export const cloudDeploymentSchema = v.picklist(['official', 'self-hosted'])
|
|
9
|
+
export type CloudDeployment = v.InferOutput<typeof cloudDeploymentSchema>
|
|
10
|
+
|
|
11
|
+
export const cloudAuthenticationSchema = v.object({
|
|
12
|
+
emailPassword: v.optional(
|
|
13
|
+
v.object({
|
|
14
|
+
signIn: v.boolean(),
|
|
15
|
+
signUp: v.boolean(),
|
|
16
|
+
minimumPasswordLength: v.pipe(v.number(), v.integer(), v.minValue(8)),
|
|
17
|
+
captcha: v.optional(
|
|
18
|
+
v.object({
|
|
19
|
+
provider: v.literal('cloudflare-turnstile'),
|
|
20
|
+
siteKey: v.pipe(v.string(), v.trim(), v.minLength(1))
|
|
21
|
+
})
|
|
22
|
+
)
|
|
23
|
+
})
|
|
24
|
+
),
|
|
25
|
+
socialProviders: v.array(v.picklist(['apple', 'google'])),
|
|
26
|
+
enterpriseSSO: v.boolean(),
|
|
27
|
+
enrollmentMode: v.optional(v.picklist(['open', 'approval', 'closed']), 'open'),
|
|
28
|
+
mfa: v.optional(
|
|
29
|
+
v.object({
|
|
30
|
+
deploymentAdminRequired: v.boolean(),
|
|
31
|
+
totp: v.boolean(),
|
|
32
|
+
passkeys: v.boolean(),
|
|
33
|
+
recoveryCodes: v.boolean()
|
|
34
|
+
})
|
|
35
|
+
)
|
|
36
|
+
})
|
|
37
|
+
export type CloudAuthentication = v.InferOutput<typeof cloudAuthenticationSchema>
|
|
38
|
+
|
|
39
|
+
export const cloudCapabilitiesSchema = v.object({
|
|
40
|
+
documents: v.boolean(),
|
|
41
|
+
workspaces: v.boolean(),
|
|
42
|
+
collaboration: v.boolean()
|
|
43
|
+
})
|
|
44
|
+
export type CloudCapabilities = v.InferOutput<typeof cloudCapabilitiesSchema>
|
|
45
|
+
|
|
46
|
+
/** A released editor version, `major.minor.patch`. */
|
|
47
|
+
export const cloudEditorVersionSchema = v.pipe(v.string(), v.regex(/^\d+\.\d+\.\d+$/))
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Whether editors offer this server to everyone, and from which editor version on. Only the
|
|
51
|
+
* official server's listing is read: it turns the Cloud entry points on without an editor release,
|
|
52
|
+
* and an editor older than the minimum keeps them hidden.
|
|
53
|
+
*/
|
|
54
|
+
export const cloudListingSchema = v.object({ minimumEditorVersion: cloudEditorVersionSchema })
|
|
55
|
+
export type CloudListing = v.InferOutput<typeof cloudListingSchema>
|
|
56
|
+
|
|
57
|
+
export const cloudDiscoverySchema = v.object({
|
|
58
|
+
protocolVersion: v.literal(CLOUD_PROTOCOL_VERSION),
|
|
59
|
+
deployment: cloudDeploymentSchema,
|
|
60
|
+
apiURL: v.pipe(v.string(), v.url()),
|
|
61
|
+
authURL: v.pipe(v.string(), v.url()),
|
|
62
|
+
/** Whether the server's own editor works with files on this device without signing in. */
|
|
63
|
+
localEditor: v.optional(v.boolean(), true),
|
|
64
|
+
authentication: cloudAuthenticationSchema,
|
|
65
|
+
capabilities: cloudCapabilitiesSchema,
|
|
66
|
+
listing: v.optional(cloudListingSchema),
|
|
67
|
+
/** The AI service editors reach with Cloud-signed tokens; absent when the deployment has none. */
|
|
68
|
+
ai: v.optional(cloudAIDiscoverySchema),
|
|
69
|
+
/** Where people manage their plan and buy credits; absent when the deployment does not bill. */
|
|
70
|
+
billing: v.optional(cloudBillingDiscoverySchema)
|
|
71
|
+
})
|
|
72
|
+
export type CloudDiscovery = v.InferOutput<typeof cloudDiscoverySchema>
|
|
73
|
+
|
|
74
|
+
/** The server's own address, where its editor and pages are, from its auth API's. */
|
|
75
|
+
export function cloudServerOrigin(discovery: CloudDiscovery): string {
|
|
76
|
+
return new URL(discovery.authURL).origin
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function parseCloudDiscovery(input: unknown): CloudDiscovery {
|
|
80
|
+
return v.parse(cloudDiscoverySchema, input)
|
|
81
|
+
}
|