@pairdraw/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 +1578 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +2008 -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 +1673 -0
- package/dist/contract2.js.map +1 -0
- package/dist/email.d.ts +155 -0
- package/dist/email.d.ts.map +1 -0
- package/dist/email.js +700 -0
- package/dist/email.js.map +1 -0
- package/dist/index.d.ts +1945 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/listen.mjs +1905 -0
- package/dist/listen.mjs.map +1 -0
- package/dist/runtime-node.d.mts +10715 -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 +13772 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +8856 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +8836 -0
- package/dist/server.mjs.map +1 -0
- package/dist/server2.mjs +505 -0
- package/dist/server2.mjs.map +1 -0
- package/package.json +120 -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 +26 -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 +530 -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/profiles.ts +79 -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/comments.ts +149 -0
- package/src/contract/discovery.ts +81 -0
- package/src/contract/documents.ts +263 -0
- package/src/contract/entitlements.ts +28 -0
- package/src/contract/index.ts +210 -0
- package/src/contract/links.ts +108 -0
- package/src/contract/portal.ts +11 -0
- package/src/contract/profiles.ts +88 -0
- package/src/contract/relay.ts +370 -0
- package/src/contract/sharing.ts +215 -0
- package/src/contract/versions.ts +87 -0
- package/src/contract/workspaces.ts +100 -0
- package/src/email/index.ts +13 -0
- package/src/email/render.ts +304 -0
- package/src/email/schema.ts +144 -0
- package/src/email/templates/brand.ts +125 -0
- package/src/email/templates/document-invitation.ts +39 -0
- package/src/email/templates/enrollment.ts +30 -0
- package/src/email/templates/layout.ts +20 -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 +161 -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 +44 -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 +119 -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 +129 -0
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +385 -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 +246 -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 +54 -0
- package/src/server/collaboration/relay/awareness.ts +104 -0
- package/src/server/collaboration/relay/comments.ts +86 -0
- package/src/server/collaboration/relay/documents.ts +39 -0
- package/src/server/collaboration/relay/hub.ts +567 -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 +190 -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_sharing_comments_snapshots_profiles.ts +74 -0
- package/src/server/db/migrations/helpers.ts +78 -0
- package/src/server/db/tables.ts +399 -0
- package/src/server/documents/access.ts +103 -0
- package/src/server/documents/errors.ts +16 -0
- package/src/server/documents/images.ts +405 -0
- package/src/server/documents/index.ts +14 -0
- package/src/server/documents/limits.ts +57 -0
- package/src/server/documents/repository.ts +131 -0
- package/src/server/documents/routes.ts +239 -0
- package/src/server/documents/service.ts +607 -0
- package/src/server/documents/summary.ts +36 -0
- package/src/server/documents/types.ts +13 -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 +396 -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 +247 -0
- package/src/server/email/types.ts +56 -0
- package/src/server/email/worker.ts +33 -0
- package/src/server/index.ts +288 -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/profiles/index.ts +12 -0
- package/src/server/profiles/routes.ts +103 -0
- package/src/server/profiles/service.ts +266 -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 +318 -0
- package/src/server/sharing/continuation.ts +83 -0
- package/src/server/sharing/contracts.ts +82 -0
- package/src/server/sharing/crypto.ts +48 -0
- package/src/server/sharing/document-routes.ts +41 -0
- package/src/server/sharing/errors.ts +20 -0
- package/src/server/sharing/grants/service.ts +79 -0
- package/src/server/sharing/http.ts +45 -0
- package/src/server/sharing/index.ts +40 -0
- package/src/server/sharing/invitations/service.ts +315 -0
- package/src/server/sharing/policy.ts +42 -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,142 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createdWorkspaceInvitationSchema,
|
|
3
|
+
workspaceEntitlementsSchema,
|
|
4
|
+
workspaceListSchema,
|
|
5
|
+
workspaceMembersSchema,
|
|
6
|
+
workspaceSummarySchema,
|
|
7
|
+
workspaceUsageSchema,
|
|
8
|
+
type CreatedWorkspaceInvitation,
|
|
9
|
+
type CreateWorkspaceInvitationInput,
|
|
10
|
+
type WorkspaceEntitlements,
|
|
11
|
+
type WorkspaceList,
|
|
12
|
+
type WorkspaceMembers,
|
|
13
|
+
type WorkspaceRole,
|
|
14
|
+
type WorkspaceSummary,
|
|
15
|
+
type WorkspaceUsage
|
|
16
|
+
} from '#cloud/contract'
|
|
17
|
+
import type { CloudAPI } from '#cloud/server/api'
|
|
18
|
+
import type { hc } from 'hono/client'
|
|
19
|
+
import * as v from 'valibot'
|
|
20
|
+
|
|
21
|
+
import { parseResponse, responseBody, type CloudClientDiagnostic } from './response'
|
|
22
|
+
|
|
23
|
+
const workspaceResponseSchema = v.object({ workspace: workspaceSummarySchema })
|
|
24
|
+
const usageResponseSchema = v.object({ usage: workspaceUsageSchema })
|
|
25
|
+
const entitlementResponseSchema = v.object({ entitlements: workspaceEntitlementsSchema })
|
|
26
|
+
|
|
27
|
+
/** Workspaces, their members, invitations, entitlements, and storage use. */
|
|
28
|
+
export function createWorkspaceRequests(
|
|
29
|
+
client: ReturnType<typeof hc<CloudAPI>>,
|
|
30
|
+
onDiagnostic?: CloudClientDiagnostic
|
|
31
|
+
) {
|
|
32
|
+
return {
|
|
33
|
+
async listWorkspaces(): Promise<WorkspaceList> {
|
|
34
|
+
return parseResponse(client.workspaces.$get(), workspaceListSchema, onDiagnostic)
|
|
35
|
+
},
|
|
36
|
+
async createWorkspace(name: string): Promise<WorkspaceSummary> {
|
|
37
|
+
const response = await parseResponse(
|
|
38
|
+
client.workspaces.$post({ json: { name } }),
|
|
39
|
+
workspaceResponseSchema,
|
|
40
|
+
onDiagnostic
|
|
41
|
+
)
|
|
42
|
+
return response.workspace
|
|
43
|
+
},
|
|
44
|
+
async renameWorkspace(workspaceId: string, name: string): Promise<WorkspaceSummary> {
|
|
45
|
+
const response = await parseResponse(
|
|
46
|
+
client.workspaces[':workspaceId'].$patch({ param: { workspaceId }, json: { name } }),
|
|
47
|
+
workspaceResponseSchema,
|
|
48
|
+
onDiagnostic
|
|
49
|
+
)
|
|
50
|
+
return response.workspace
|
|
51
|
+
},
|
|
52
|
+
/** Deletes an empty workspace; the server refuses one that still holds documents. */
|
|
53
|
+
async deleteWorkspace(workspaceId: string): Promise<void> {
|
|
54
|
+
await responseBody(
|
|
55
|
+
await client.workspaces[':workspaceId'].$delete({ param: { workspaceId } }),
|
|
56
|
+
onDiagnostic
|
|
57
|
+
)
|
|
58
|
+
},
|
|
59
|
+
/** The workspace's members, and for administrators its pending invitations. */
|
|
60
|
+
async listWorkspaceMembers(workspaceId: string): Promise<WorkspaceMembers> {
|
|
61
|
+
return parseResponse(
|
|
62
|
+
client.workspaces[':workspaceId'].members.$get({ param: { workspaceId } }),
|
|
63
|
+
workspaceMembersSchema,
|
|
64
|
+
onDiagnostic
|
|
65
|
+
)
|
|
66
|
+
},
|
|
67
|
+
async inviteToWorkspace(
|
|
68
|
+
workspaceId: string,
|
|
69
|
+
input: CreateWorkspaceInvitationInput
|
|
70
|
+
): Promise<CreatedWorkspaceInvitation> {
|
|
71
|
+
return parseResponse(
|
|
72
|
+
client.workspaces[':workspaceId'].invitations.$post({
|
|
73
|
+
param: { workspaceId },
|
|
74
|
+
json: input
|
|
75
|
+
}),
|
|
76
|
+
createdWorkspaceInvitationSchema,
|
|
77
|
+
onDiagnostic
|
|
78
|
+
)
|
|
79
|
+
},
|
|
80
|
+
/** Sends a waiting invitation again; its earlier link stops working. */
|
|
81
|
+
async resendWorkspaceInvitation(
|
|
82
|
+
workspaceId: string,
|
|
83
|
+
invitationId: string
|
|
84
|
+
): Promise<CreatedWorkspaceInvitation> {
|
|
85
|
+
return parseResponse(
|
|
86
|
+
client.workspaces[':workspaceId'].invitations[':invitationId'].resend.$post({
|
|
87
|
+
param: { workspaceId, invitationId }
|
|
88
|
+
}),
|
|
89
|
+
createdWorkspaceInvitationSchema,
|
|
90
|
+
onDiagnostic
|
|
91
|
+
)
|
|
92
|
+
},
|
|
93
|
+
async revokeWorkspaceInvitation(workspaceId: string, invitationId: string): Promise<void> {
|
|
94
|
+
await responseBody(
|
|
95
|
+
await client.workspaces[':workspaceId'].invitations[':invitationId'].$delete({
|
|
96
|
+
param: { workspaceId, invitationId }
|
|
97
|
+
}),
|
|
98
|
+
onDiagnostic
|
|
99
|
+
)
|
|
100
|
+
},
|
|
101
|
+
async updateWorkspaceMember(
|
|
102
|
+
workspaceId: string,
|
|
103
|
+
userId: string,
|
|
104
|
+
role: WorkspaceRole
|
|
105
|
+
): Promise<void> {
|
|
106
|
+
await responseBody(
|
|
107
|
+
await client.workspaces[':workspaceId'].members[':userId'].$patch({
|
|
108
|
+
param: { workspaceId, userId },
|
|
109
|
+
json: { role }
|
|
110
|
+
}),
|
|
111
|
+
onDiagnostic
|
|
112
|
+
)
|
|
113
|
+
},
|
|
114
|
+
/** Removes someone from the workspace; with the person's own id, leaves it. */
|
|
115
|
+
async removeWorkspaceMember(workspaceId: string, userId: string): Promise<void> {
|
|
116
|
+
await responseBody(
|
|
117
|
+
await client.workspaces[':workspaceId'].members[':userId'].$delete({
|
|
118
|
+
param: { workspaceId, userId }
|
|
119
|
+
}),
|
|
120
|
+
onDiagnostic
|
|
121
|
+
)
|
|
122
|
+
},
|
|
123
|
+
async getWorkspaceEntitlements(workspaceId: string): Promise<WorkspaceEntitlements> {
|
|
124
|
+
const response = await parseResponse(
|
|
125
|
+
client.workspaces[':workspaceId'].entitlements.$get({ param: { workspaceId } }),
|
|
126
|
+
entitlementResponseSchema,
|
|
127
|
+
onDiagnostic
|
|
128
|
+
)
|
|
129
|
+
return response.entitlements
|
|
130
|
+
},
|
|
131
|
+
async getUsage(workspaceId: string): Promise<WorkspaceUsage> {
|
|
132
|
+
const response = await parseResponse(
|
|
133
|
+
client.workspaces[':workspaceId'].usage.$get({
|
|
134
|
+
param: { workspaceId }
|
|
135
|
+
}),
|
|
136
|
+
usageResponseSchema,
|
|
137
|
+
onDiagnostic
|
|
138
|
+
)
|
|
139
|
+
return response.usage
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -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 = 'pairdraw-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>
|