@open-pencil/cloud 0.17.0-beta.2 → 0.17.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/README.md +1 -1
  2. package/dist/bin/{ai-relay.mjs → services.mjs} +8 -8
  3. package/dist/bin/services.mjs.map +1 -0
  4. package/dist/client.d.ts +236 -46
  5. package/dist/client.d.ts.map +1 -1
  6. package/dist/client.js +649 -400
  7. package/dist/client.js.map +1 -1
  8. package/dist/contract.d.ts +2 -2
  9. package/dist/contract.js +2 -2
  10. package/dist/contract2.js +680 -443
  11. package/dist/contract2.js.map +1 -1
  12. package/dist/email.d.ts +9 -1
  13. package/dist/email.d.ts.map +1 -1
  14. package/dist/email.js +99 -58
  15. package/dist/email.js.map +1 -1
  16. package/dist/index.d.ts +469 -65
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/listen.mjs +505 -787
  19. package/dist/listen.mjs.map +1 -1
  20. package/dist/runtime-node.d.mts +3920 -2524
  21. package/dist/runtime-node.d.mts.map +1 -1
  22. package/dist/runtime-node.mjs +6 -5
  23. package/dist/runtime-node.mjs.map +1 -1
  24. package/dist/server.d.ts +3586 -1897
  25. package/dist/server.d.ts.map +1 -1
  26. package/dist/server.js +5216 -4272
  27. package/dist/server.js.map +1 -1
  28. package/dist/server.mjs +5179 -3623
  29. package/dist/server.mjs.map +1 -1
  30. package/dist/server2.mjs +101 -79
  31. package/dist/server2.mjs.map +1 -1
  32. package/package.json +7 -6
  33. package/src/admin/api/routes.ts +59 -0
  34. package/src/admin/audit/service.ts +25 -6
  35. package/src/admin/documents/service.ts +361 -0
  36. package/src/admin/errors.ts +9 -1
  37. package/src/admin/index.ts +6 -0
  38. package/src/admin/operations/service.ts +7 -0
  39. package/src/admin/reports/service.ts +147 -0
  40. package/src/admin/schema.ts +6 -0
  41. package/src/client/api.ts +36 -21
  42. package/src/client/auth/navigation.ts +6 -5
  43. package/src/client/index.ts +7 -7
  44. package/src/client/portal/account.ts +25 -0
  45. package/src/client/portal/administration.ts +56 -0
  46. package/src/client/{ai.ts → services.ts} +44 -27
  47. package/src/contract/account.ts +58 -2
  48. package/src/contract/admin/index.ts +67 -3
  49. package/src/contract/collaboration.ts +6 -1
  50. package/src/contract/discovery.ts +22 -4
  51. package/src/contract/documents.ts +27 -0
  52. package/src/contract/entitlements.ts +2 -2
  53. package/src/contract/index.ts +63 -37
  54. package/src/contract/relay.ts +6 -0
  55. package/src/contract/reports.ts +42 -0
  56. package/src/contract/services.ts +233 -0
  57. package/src/contract/sharing.ts +3 -9
  58. package/src/email/render.ts +26 -0
  59. package/src/email/schema.ts +11 -1
  60. package/src/email/templates/brand.ts +1 -1
  61. package/src/runtime/cloudflare/environment.ts +22 -11
  62. package/src/runtime/cloudflare/index.ts +1 -1
  63. package/src/runtime/cloudflare/logging.ts +15 -0
  64. package/src/runtime/cloudflare/maintenance.ts +31 -0
  65. package/src/runtime/cloudflare/relay/index.ts +62 -14
  66. package/src/runtime/cloudflare/worker.ts +39 -13
  67. package/src/runtime/node/application.ts +5 -3
  68. package/src/runtime/node/bootstrap.ts +15 -4
  69. package/src/runtime/node/cleanup.ts +16 -3
  70. package/src/runtime/node/entitlements/main.ts +14 -11
  71. package/src/runtime/node/index.ts +1 -1
  72. package/src/runtime/node/migrate.ts +5 -1
  73. package/src/runtime/node/relay.ts +8 -2
  74. package/src/runtime/node/request-id.ts +13 -0
  75. package/src/runtime/node/server.ts +39 -15
  76. package/src/runtime/node/{ai-relay → services}/main.ts +6 -6
  77. package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
  78. package/src/runtime/postgres/database.ts +20 -1
  79. package/src/runtime/posthog/errors.ts +15 -31
  80. package/src/runtime/s3/objects.ts +24 -1
  81. package/src/runtime/site/server.ts +32 -5
  82. package/src/server/account/actions.ts +5 -0
  83. package/src/server/account/deletion.ts +302 -0
  84. package/src/server/account/routes.ts +47 -3
  85. package/src/server/account/terms.ts +42 -0
  86. package/src/server/api/router.ts +9 -5
  87. package/src/server/api/types.ts +2 -0
  88. package/src/server/app.ts +90 -16
  89. package/src/server/auth/access-control.ts +63 -0
  90. package/src/server/auth/adapter.ts +11 -0
  91. package/src/server/auth/endpoints.ts +5 -1
  92. package/src/server/auth/factory.ts +30 -3
  93. package/src/server/auth/index.ts +10 -1
  94. package/src/server/cleanup/configured.ts +12 -5
  95. package/src/server/cleanup/documents.ts +165 -71
  96. package/src/server/cleanup/revisions.ts +16 -5
  97. package/src/server/cleanup/uploads.ts +8 -2
  98. package/src/server/cleanup/worker.ts +5 -2
  99. package/src/server/collaboration/relay/connection.ts +4 -3
  100. package/src/server/collaboration/relay/documents.ts +13 -10
  101. package/src/server/collaboration/relay/hub.ts +97 -25
  102. package/src/server/collaboration/relay/log.ts +5 -2
  103. package/src/server/collaboration/service.ts +27 -31
  104. package/src/server/config/deployment.ts +24 -4
  105. package/src/server/config/public.ts +5 -4
  106. package/src/server/config/schema.ts +82 -16
  107. package/src/server/db/expressions.ts +15 -1
  108. package/src/server/db/migrate.ts +34 -116
  109. package/src/server/db/migrations/001_accounts.ts +79 -0
  110. package/src/server/db/migrations/002_workspaces.ts +48 -0
  111. package/src/server/db/migrations/003_storage.ts +44 -0
  112. package/src/server/db/migrations/004_documents.ts +193 -0
  113. package/src/server/db/migrations/005_sharing.ts +95 -0
  114. package/src/server/db/migrations/006_operations.ts +110 -0
  115. package/src/server/db/migrations/007_trash.ts +33 -0
  116. package/src/server/db/migrations/008_terms.ts +18 -0
  117. package/src/server/db/migrations/schema.ts +71 -0
  118. package/src/server/db/tables.ts +52 -3
  119. package/src/server/documents/access.ts +66 -55
  120. package/src/server/documents/errors.ts +7 -0
  121. package/src/server/documents/images.ts +2 -1
  122. package/src/server/documents/limits.ts +9 -9
  123. package/src/server/documents/purge.ts +75 -0
  124. package/src/server/documents/repository.ts +2 -0
  125. package/src/server/documents/routes.ts +26 -0
  126. package/src/server/documents/service.ts +18 -4
  127. package/src/server/documents/summary.ts +1 -0
  128. package/src/server/documents/trash.ts +203 -0
  129. package/src/server/documents/versions/checkpoint.ts +2 -1
  130. package/src/server/documents/versions/history.ts +11 -7
  131. package/src/server/documents/versions/service.ts +13 -4
  132. package/src/server/errors/index.ts +59 -16
  133. package/src/server/errors/posthog.ts +80 -0
  134. package/src/server/index.ts +46 -17
  135. package/src/server/internal/index.ts +3 -3
  136. package/src/server/internal/routes.ts +7 -10
  137. package/src/server/internal/schema.ts +1 -1
  138. package/src/server/internal/service.ts +17 -23
  139. package/src/server/invitations/budget.ts +66 -0
  140. package/src/server/invitations/index.ts +1 -0
  141. package/src/server/logging.ts +76 -0
  142. package/src/server/pictures/service.ts +2 -1
  143. package/src/server/policy/database.ts +39 -25
  144. package/src/server/policy/index.ts +2 -1
  145. package/src/server/policy/keys.ts +5 -2
  146. package/src/server/policy/policy.ts +1 -0
  147. package/src/server/policy/schema.ts +2 -2
  148. package/src/server/policy/service.ts +12 -9
  149. package/src/server/policy/static.ts +5 -5
  150. package/src/server/policy/workspace.ts +41 -0
  151. package/src/server/quota/reconcile.ts +2 -0
  152. package/src/server/quota/storage.ts +69 -31
  153. package/src/server/rate-limit/cleanup.ts +17 -5
  154. package/src/server/rate-limit/middleware.ts +2 -2
  155. package/src/server/reports/index.ts +8 -0
  156. package/src/server/reports/routes.ts +37 -0
  157. package/src/server/reports/service.ts +123 -0
  158. package/src/server/services/errors.ts +7 -0
  159. package/src/server/services/index.ts +8 -0
  160. package/src/server/{ai → services}/keys.ts +11 -11
  161. package/src/server/services/routes.ts +47 -0
  162. package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
  163. package/src/server/sharing/capabilities/service.ts +68 -22
  164. package/src/server/sharing/grants/service.ts +11 -18
  165. package/src/server/sharing/http.ts +4 -0
  166. package/src/server/sharing/invitations/service.ts +21 -11
  167. package/src/server/sharing/policy.ts +9 -14
  168. package/src/server/sharing/routes.ts +0 -16
  169. package/src/server/sharing/service.ts +1 -17
  170. package/src/server/validation.ts +1 -0
  171. package/src/server/workspaces/index.ts +1 -0
  172. package/src/server/workspaces/members.ts +89 -38
  173. package/src/server/workspaces/repository.ts +2 -1
  174. package/src/server/workspaces/routes.ts +4 -0
  175. package/src/{ai-relay → services}/app.ts +48 -44
  176. package/src/{ai-relay → services}/config.ts +25 -19
  177. package/src/services/db/index.ts +10 -0
  178. package/src/services/db/migrate.ts +37 -0
  179. package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
  180. package/src/services/db/schema.ts +22 -0
  181. package/src/services/index.ts +18 -0
  182. package/src/{ai-relay → services}/metering.ts +2 -2
  183. package/src/{ai-relay → services}/usage.ts +43 -19
  184. package/dist/bin/ai-relay.mjs.map +0 -1
  185. package/src/ai-relay/db/index.ts +0 -10
  186. package/src/ai-relay/db/migrate.ts +0 -32
  187. package/src/ai-relay/db/schema.ts +0 -20
  188. package/src/ai-relay/index.ts +0 -18
  189. package/src/contract/ai.ts +0 -150
  190. package/src/server/ai/errors.ts +0 -7
  191. package/src/server/ai/index.ts +0 -4
  192. package/src/server/ai/routes.ts +0 -43
  193. package/src/server/auth/workspace-access.ts +0 -23
  194. package/src/server/db/migrations/001_foundation.ts +0 -164
  195. package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
  196. package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
  197. package/src/server/db/migrations/004_document_sharing.ts +0 -82
  198. package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
  199. package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
  200. package/src/server/db/migrations/007_collaboration_state.ts +0 -25
  201. package/src/server/db/migrations/008_storage_reservations.ts +0 -37
  202. package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
  203. package/src/server/db/migrations/010_upload_finalization.ts +0 -22
  204. package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
  205. package/src/server/db/migrations/012_transactional_email.ts +0 -48
  206. package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
  207. package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
  208. package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
  209. package/src/server/db/migrations/016_auth_schema.ts +0 -16
  210. package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
  211. package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
  212. package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
  213. package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
  214. package/src/server/db/migrations/021_document_versions.ts +0 -66
  215. package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
  216. package/src/server/db/migrations/023_document_images.ts +0 -61
  217. package/src/server/db/migrations/024_account_activity.ts +0 -66
  218. package/src/server/db/migrations/025_table_names.ts +0 -58
  219. package/src/server/db/migrations/026_user_picture.ts +0 -30
  220. package/src/server/db/migrations/027_document_kind.ts +0 -19
  221. package/src/server/db/migrations/028_collaboration_updates.ts +0 -84
  222. package/src/server/db/migrations/helpers.ts +0 -78
  223. /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
@@ -7,6 +7,7 @@ import {
7
7
  cloudPersonSchema,
8
8
  cloudSignInSchema
9
9
  } from '../account'
10
+ import { documentReportReasonSchema } from '../reports'
10
11
  import { workspaceRoleSchema } from '../workspaces'
11
12
 
12
13
  const timestampSchema = v.pipe(v.string(), v.isoTimestamp())
@@ -39,7 +40,9 @@ export const adminErrorCodeSchema = v.picklist([
39
40
  'last_admin_required',
40
41
  'self_admin_action_forbidden',
41
42
  'email_regeneration_unavailable',
42
- 'mfa_required'
43
+ 'mfa_required',
44
+ 'document_not_restorable',
45
+ 'workspace_gone'
43
46
  ])
44
47
 
45
48
  export const adminErrorResponseSchema = v.object({
@@ -147,23 +150,79 @@ export const adminAuditEventSchema = v.object({
147
150
  id: v.string(),
148
151
  actor: auditPartySchema,
149
152
  action: v.string(),
150
- /** What the event is about: an account, an access request, or an email. */
153
+ /** What the event is about: an account, an access request, an email, or a document. */
151
154
  subject: v.object({ ...auditPartySchema.entries, type: v.string() }),
152
155
  metadata: v.unknown(),
153
156
  createdAt: timestampSchema
154
157
  })
155
158
  /** Which events a list of activity shows, by what they are about. */
156
- export const cloudAdminAuditFilterSchema = v.picklist(['all', 'accounts', 'requests', 'email'])
159
+ export const cloudAdminAuditFilterSchema = v.picklist([
160
+ 'all',
161
+ 'accounts',
162
+ 'requests',
163
+ 'email',
164
+ 'documents'
165
+ ])
157
166
  export const adminAuditResponseSchema = v.object({
158
167
  events: v.array(adminAuditEventSchema),
159
168
  ...listTotals(cloudAdminAuditFilterSchema.options)
160
169
  })
161
170
 
171
+ /** A document as the console shows it: where it lives, who made it, and whether it is taken down. */
172
+ export const cloudAdminDocumentSchema = v.object({
173
+ id: v.string(),
174
+ name: v.string(),
175
+ workspace: v.object({ id: v.string(), name: v.string() }),
176
+ /** Who made it, while the server knows them. */
177
+ owner: v.nullable(v.object({ id: v.string(), name: v.string(), email: v.string() })),
178
+ createdAt: timestampSchema,
179
+ updatedAt: timestampSchema,
180
+ disabledAt: nullableTimestampSchema,
181
+ disabledReason: v.nullable(v.string()),
182
+ /** Reports about it still waiting for a decision. */
183
+ openReports: countSchema,
184
+ /**
185
+ * When someone deleted it forever, and when cleanup destroys it; until then an administrator
186
+ * can still restore it. Servers from before Trash send neither.
187
+ */
188
+ purgedAt: v.optional(nullableTimestampSchema, null),
189
+ destroyedAt: v.optional(nullableTimestampSchema, null)
190
+ })
191
+ /** Which documents a list shows: live ones, or those deleted forever an administrator can restore. */
192
+ export const cloudAdminDocumentFilterSchema = v.picklist(['all', 'reported', 'disabled', 'purged'])
193
+ export const cloudAdminDocumentsResponseSchema = v.object({
194
+ documents: v.array(cloudAdminDocumentSchema),
195
+ ...listTotals(cloudAdminDocumentFilterSchema.options)
196
+ })
197
+
198
+ export const cloudAdminReportStatusSchema = v.picklist(['open', 'dismissed', 'actioned'])
199
+ export const cloudAdminReportSchema = v.object({
200
+ id: v.string(),
201
+ reason: documentReportReasonSchema,
202
+ description: v.string(),
203
+ status: cloudAdminReportStatusSchema,
204
+ /** The account that filed it, or null for a guest. */
205
+ reporter: v.nullable(v.object({ id: v.string(), name: v.nullable(v.string()) })),
206
+ replyEmail: v.nullable(v.string()),
207
+ createdAt: timestampSchema,
208
+ resolvedAt: nullableTimestampSchema,
209
+ document: cloudAdminDocumentSchema,
210
+ /** The link it was opened from, and whether that link still works. */
211
+ share: v.nullable(v.object({ id: v.string(), revoked: v.boolean() }))
212
+ })
213
+ /** Which reports a list shows, by where they stand. */
214
+ export const cloudAdminReportFilterSchema = v.picklist(['open', 'actioned', 'dismissed', 'all'])
215
+ export const cloudAdminReportsResponseSchema = v.object({
216
+ reports: v.array(cloudAdminReportSchema),
217
+ ...listTotals(cloudAdminReportFilterSchema.options)
218
+ })
219
+
162
220
  export const adminOperationsResponseSchema = v.object({
163
221
  deployment: v.picklist(['official', 'self-hosted']),
164
222
  enrollmentMode: v.picklist(['open', 'approval', 'closed']),
165
223
  emailTransport: v.picklist(['none', 'smtp', 'cloudflare']),
166
224
  pendingEnrollment: v.pipe(v.number(), v.integer(), v.minValue(0)),
225
+ openReports: countSchema,
167
226
  pendingEmail: v.pipe(v.number(), v.integer(), v.minValue(0)),
168
227
  failedEmail: v.pipe(v.number(), v.integer(), v.minValue(0)),
169
228
  accounts: countSchema,
@@ -185,3 +244,8 @@ export type CloudAdminUserDetail = v.InferOutput<typeof cloudAdminUserDetailSche
185
244
  export type TransactionalEmailAdmin = v.InferOutput<typeof transactionalEmailAdminSchema>
186
245
  export type AdminAuditEvent = v.InferOutput<typeof adminAuditEventSchema>
187
246
  export type AdminOperations = v.InferOutput<typeof adminOperationsResponseSchema>
247
+ export type CloudAdminDocument = v.InferOutput<typeof cloudAdminDocumentSchema>
248
+ export type CloudAdminDocumentFilter = v.InferOutput<typeof cloudAdminDocumentFilterSchema>
249
+ export type CloudAdminReport = v.InferOutput<typeof cloudAdminReportSchema>
250
+ export type CloudAdminReportFilter = v.InferOutput<typeof cloudAdminReportFilterSchema>
251
+ export type CloudAdminReportStatus = v.InferOutput<typeof cloudAdminReportStatusSchema>
@@ -40,6 +40,11 @@ export const collaborationTicketSchema = v.object({
40
40
  permission: documentPermissionSchema,
41
41
  roomEpoch: v.pipe(v.number(), v.integer(), v.minValue(0)),
42
42
  expiresAt: v.string(),
43
- serverEnforcedWrites: v.boolean()
43
+ serverEnforcedWrites: v.boolean(),
44
+ /**
45
+ * Whether the relay answers `RELAY_HEARTBEAT`. A relay from before it takes a ping for a
46
+ * malformed frame and turns the peer away, so editors ask only a relay that says it answers.
47
+ */
48
+ relayHeartbeat: v.optional(v.boolean(), false)
44
49
  })
45
50
  export type CollaborationTicket = v.InferOutput<typeof collaborationTicketSchema>
@@ -1,6 +1,6 @@
1
1
  import * as v from 'valibot'
2
2
 
3
- import { cloudAIDiscoverySchema, cloudBillingDiscoverySchema } from './ai'
3
+ import { cloudServicesDiscoverySchema, cloudBillingDiscoverySchema } from './services'
4
4
 
5
5
  export const CLOUD_PROTOCOL_VERSION = '1' as const
6
6
  export const CLOUD_DISCOVERY_PATH = '/.well-known/openpencil' as const
@@ -54,6 +54,23 @@ export const cloudEditorVersionSchema = v.pipe(v.string(), v.regex(/^\d+\.\d+\.\
54
54
  export const cloudListingSchema = v.object({ minimumEditorVersion: cloudEditorVersionSchema })
55
55
  export type CloudListing = v.InferOutput<typeof cloudListingSchema>
56
56
 
57
+ const legalURLSchema = v.pipe(v.string(), v.url())
58
+
59
+ /**
60
+ * The operator's legal links, which clients show at sign-up and in account pages. A server that
61
+ * names none shows none; `requireAcceptance` means an account accepts `termsVersion` before it uses
62
+ * the server.
63
+ */
64
+ export const cloudLegalDiscoverySchema = v.object({
65
+ termsURL: v.optional(legalURLSchema),
66
+ privacyURL: v.optional(legalURLSchema),
67
+ termsVersion: v.optional(v.string()),
68
+ requireAcceptance: v.boolean(),
69
+ abuseEmail: v.optional(v.pipe(v.string(), v.email())),
70
+ minimumAge: v.optional(v.pipe(v.number(), v.integer()))
71
+ })
72
+ export type CloudLegalDiscovery = v.InferOutput<typeof cloudLegalDiscoverySchema>
73
+
57
74
  export const cloudDiscoverySchema = v.object({
58
75
  protocolVersion: v.literal(CLOUD_PROTOCOL_VERSION),
59
76
  deployment: cloudDeploymentSchema,
@@ -64,10 +81,11 @@ export const cloudDiscoverySchema = v.object({
64
81
  authentication: cloudAuthenticationSchema,
65
82
  capabilities: cloudCapabilitiesSchema,
66
83
  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),
84
+ /** The services editors reach with Cloud-signed tokens; absent when the deployment has none. */
85
+ services: v.optional(cloudServicesDiscoverySchema),
69
86
  /** Where people manage their plan and buy credits; absent when the deployment does not bill. */
70
- billing: v.optional(cloudBillingDiscoverySchema)
87
+ billing: v.optional(cloudBillingDiscoverySchema),
88
+ legal: v.optional(cloudLegalDiscoverySchema)
71
89
  })
72
90
  export type CloudDiscovery = v.InferOutput<typeof cloudDiscoverySchema>
73
91
 
@@ -12,6 +12,14 @@ export function isCloudChecksum(value: string): boolean {
12
12
  /** The most images one revision, or one request for image uploads, may name. */
13
13
  export const CLOUD_MAX_DOCUMENT_IMAGES = 10_000
14
14
 
15
+ /**
16
+ * The most a Cloud API request body may hold; a server refuses anything past it before reading
17
+ * it. Files go to the object store, never through the API, so only lists grow large: a save's
18
+ * images fit with room to spare, and a client commits image uploads, whose part lists can run
19
+ * long, in batches under half of it.
20
+ */
21
+ export const CLOUD_API_MAX_BODY_BYTES = 8 * 1024 * 1024
22
+
15
23
  /**
16
24
  * An image's name inside a `.fig` file that Cloud stores apart from it: the SHA-1 Figma names it
17
25
  * by, or an older plain name. A client leaves images under other names inside the file.
@@ -89,6 +97,25 @@ export const sharedDocumentSchema = v.object({
89
97
  })
90
98
  export type SharedDocument = v.InferOutput<typeof sharedDocumentSchema>
91
99
 
100
+ /**
101
+ * A document in Trash the person may restore: who deleted it and when, and when it goes for good.
102
+ * `canPurge` says whether they may also delete it for good before then.
103
+ */
104
+ export const trashedDocumentSchema = v.object({
105
+ id: v.pipe(v.string(), v.uuid()),
106
+ workspaceId: v.pipe(v.string(), v.uuid()),
107
+ workspaceName: v.string(),
108
+ name: v.string(),
109
+ kind: v.optional(documentKindSchema, 'design'),
110
+ deletedAt: v.string(),
111
+ deletedBy: v.nullable(v.object({ name: v.string() })),
112
+ purgeAt: v.string(),
113
+ canPurge: v.boolean()
114
+ })
115
+ export type TrashedDocument = v.InferOutput<typeof trashedDocumentSchema>
116
+
117
+ export const trashSchema = v.object({ documents: v.array(trashedDocumentSchema) })
118
+
92
119
  export const documentDownloadSchema = v.object({
93
120
  document: documentSummarySchema,
94
121
  revisionId: v.pipe(v.string(), v.uuid()),
@@ -8,8 +8,8 @@ export const workspaceEntitlementsSchema = v.object({
8
8
  guestPresence: v.boolean(),
9
9
  collaboration: v.boolean(),
10
10
  revisionHistory: v.boolean(),
11
- // Optional so editors keep reading servers released before the AI service.
12
- cloudAI: v.optional(v.boolean(), false)
11
+ // Optional so editors keep reading servers released before services.
12
+ cloudServices: v.optional(v.boolean(), false)
13
13
  }),
14
14
  limits: v.object({
15
15
  maximumFileBytes: v.pipe(v.number(), v.integer(), v.minValue(1)),
@@ -1,41 +1,55 @@
1
1
  export * from './account'
2
2
  export {
3
- AI_ERROR_STATUS,
4
- aiCatalogSchema,
5
- aiCreditBucketSchema,
6
- aiErrorCodeSchema,
7
- aiErrorSchema,
8
- aiModelSchema,
9
- aiTokenClaimsSchema,
10
- aiTokenSchema,
11
- aiUsageEntrySchema,
12
- aiUsagePageSchema,
13
- aiUsageQuerySchema,
14
- aiWalletSchema,
15
- CLOUD_AI_API_PATHS,
16
- CLOUD_AI_CONTRACT_VERSION,
17
- CLOUD_AI_JWKS_PATH,
18
- CLOUD_AI_TOKEN_AUDIENCE,
19
- cloudAIDiscoverySchema,
3
+ SERVICE_ERROR_STATUS,
4
+ serviceCatalogSchema,
5
+ creditBucketSchema,
6
+ serviceErrorCodeSchema,
7
+ serviceErrorSchema,
8
+ chatModelSchema,
9
+ servicesTokenClaimsSchema,
10
+ servicesTokenSchema,
11
+ usageEntrySchema,
12
+ usagePageSchema,
13
+ usageQuerySchema,
14
+ creditWalletSchema,
15
+ searchPricingSchema,
16
+ searchRequestSchema,
17
+ searchResultSchema,
18
+ searchResponseSchema,
19
+ MAX_SEARCH_RESULTS,
20
+ MAX_SEARCH_QUERY_LENGTH,
21
+ CLOUD_SERVICES_API_PATHS,
22
+ CLOUD_SERVICES_CONTRACT_VERSION,
23
+ CLOUD_SERVICES_JWKS_PATH,
24
+ CLOUD_SERVICES_TOKEN_AUDIENCE,
25
+ cloudServicesDiscoverySchema,
20
26
  cloudBillingDiscoverySchema,
21
- createAITokenSchema,
22
- parseAIUsageQuery,
23
- parseCreateAIToken,
24
- type AICatalog,
25
- type AICreditBucket,
26
- type AIError,
27
- type AIErrorCode,
28
- type AIModel,
29
- type AIToken,
30
- type AITokenClaims,
31
- type AIUsageEntry,
32
- type AIUsagePage,
33
- type AIUsageQuery,
34
- type AIWallet,
35
- type CloudAIDiscovery,
27
+ createServicesTokenSchema,
28
+ parseUsageQuery,
29
+ parseSearchRequest,
30
+ parseCreateServicesToken,
31
+ type ServiceCatalog,
32
+ type CreditBucket,
33
+ type ServiceError,
34
+ type ServiceErrorCode,
35
+ type ChatModel,
36
+ type ServicesToken,
37
+ type ServicesTokenClaims,
38
+ type UsageEntry,
39
+ type UsagePage,
40
+ type UsageQuery,
41
+ type CreditWallet,
42
+ type SearchPricing,
43
+ type SearchRequest,
44
+ type SearchRequestInput,
45
+ type SearchResult,
46
+ type SearchResponse,
47
+ type CloudServicesDiscovery,
36
48
  type CloudBillingDiscovery,
37
- type CreateAITokenInput
38
- } from './ai'
49
+ type CreateServicesTokenInput,
50
+ type CloudServicesAPI,
51
+ type CloudServicesSchema
52
+ } from './services'
39
53
  export * from './admin'
40
54
  export {
41
55
  cloudPresenceSchema,
@@ -55,6 +69,7 @@ export {
55
69
  cloudDeploymentSchema,
56
70
  cloudDiscoverySchema,
57
71
  cloudEditorVersionSchema,
72
+ cloudLegalDiscoverySchema,
58
73
  cloudListingSchema,
59
74
  cloudServerOrigin,
60
75
  parseCloudDiscovery,
@@ -62,11 +77,13 @@ export {
62
77
  type CloudCapabilities,
63
78
  type CloudDeployment,
64
79
  type CloudDiscovery,
80
+ type CloudLegalDiscovery,
65
81
  type CloudListing
66
82
  } from './discovery'
67
83
  export {
68
84
  CLOUD_DOCUMENT_NAME_MAX_LENGTH,
69
85
  CLOUD_IMAGE_NAME_PATTERN,
86
+ CLOUD_API_MAX_BODY_BYTES,
70
87
  CLOUD_MAX_DOCUMENT_IMAGES,
71
88
  CLOUD_PROTOCOL_MAX_UPLOAD_BYTES,
72
89
  cloudActorSchema,
@@ -86,6 +103,9 @@ export {
86
103
  documentSummarySchema,
87
104
  sharedDocumentSchema,
88
105
  type SharedDocument,
106
+ trashSchema,
107
+ trashedDocumentSchema,
108
+ type TrashedDocument,
89
109
  workspaceUsageSchema,
90
110
  isCloudChecksum,
91
111
  parseCommitImageUploads,
@@ -129,12 +149,10 @@ export {
129
149
  documentShareSchema,
130
150
  invitationContinuationSchema,
131
151
  invitationPreviewSchema,
132
- lookupCloudUserSchema,
133
152
  parseAcceptDocumentInvitation,
134
153
  parseCreateInvitationContinuation,
135
154
  parseCreateDocumentInvitation,
136
155
  parseCreateDocumentShare,
137
- parseLookupCloudUser,
138
156
  parsePutDocumentGrant,
139
157
  parseRequestSharedImageLinks,
140
158
  parseResolveDocumentShare,
@@ -155,7 +173,6 @@ export {
155
173
  type DocumentShare,
156
174
  type InvitationContinuation,
157
175
  type InvitationPreview,
158
- type LookupCloudUserInput,
159
176
  type PutDocumentGrantInput,
160
177
  type RequestSharedImageLinksInput,
161
178
  type ResolveDocumentShareInput,
@@ -202,3 +219,12 @@ export {
202
219
  } from './links'
203
220
  export { CLOUD_PORTAL_PAGE_ROOTS, isCloudPortalPage } from './portal'
204
221
  export * from './versions'
222
+ export {
223
+ DOCUMENT_REPORT_DESCRIPTION_MAX,
224
+ documentReportReasonSchema,
225
+ fileDocumentReportResponseSchema,
226
+ fileDocumentReportSchema,
227
+ parseFileDocumentReport,
228
+ type DocumentReportReason,
229
+ type FileDocumentReportInput
230
+ } from './reports'
@@ -109,6 +109,12 @@ export const RELAY_CLOSE = {
109
109
  restored: 4410
110
110
  } as const
111
111
 
112
+ /**
113
+ * The text a peer sends to learn whether the relay still hears it, and the relay's answer. Relay
114
+ * frames are binary, so neither is taken for one, and a Durable Object answers without waking.
115
+ */
116
+ export const RELAY_HEARTBEAT = { ping: 'ping', pong: 'pong' } as const
117
+
112
118
  const CLIENT_AUTH = 0
113
119
  const CLIENT_MESSAGE = 1
114
120
  const CLIENT_FRAGMENT = 2
@@ -0,0 +1,42 @@
1
+ import * as v from 'valibot'
2
+
3
+ import { resolveDocumentShareSchema } from './sharing'
4
+
5
+ /** Why someone reports a document shared with them. */
6
+ export const documentReportReasonSchema = v.picklist([
7
+ 'spam-phishing',
8
+ 'illegal',
9
+ 'copyright',
10
+ 'harassment',
11
+ 'other'
12
+ ])
13
+ export type DocumentReportReason = v.InferOutput<typeof documentReportReasonSchema>
14
+
15
+ /** How long a report's description may be. */
16
+ export const DOCUMENT_REPORT_DESCRIPTION_MAX = 2000
17
+
18
+ /**
19
+ * A report about a document someone opened from a share link: the link's secret proves they can
20
+ * see it, as resolving the link does, and a guest names themselves as they do there.
21
+ */
22
+ export const fileDocumentReportSchema = v.object({
23
+ secret: resolveDocumentShareSchema.entries.secret,
24
+ guestId: resolveDocumentShareSchema.entries.guestId,
25
+ reason: documentReportReasonSchema,
26
+ description: v.pipe(
27
+ v.string(),
28
+ v.trim(),
29
+ v.minLength(1),
30
+ v.maxLength(DOCUMENT_REPORT_DESCRIPTION_MAX)
31
+ ),
32
+ /** Where an administrator may answer; optional, and never shown to the document's people. */
33
+ replyEmail: v.optional(v.pipe(v.string(), v.trim(), v.toLowerCase(), v.email(), v.maxLength(254)))
34
+ })
35
+ export type FileDocumentReportInput = v.InferOutput<typeof fileDocumentReportSchema>
36
+
37
+ export function parseFileDocumentReport(input: unknown): FileDocumentReportInput {
38
+ return v.parse(fileDocumentReportSchema, input)
39
+ }
40
+
41
+ /** What filing a report answers: that it arrived, and nothing about the document. */
42
+ export const fileDocumentReportResponseSchema = v.object({ received: v.literal(true) })
@@ -0,0 +1,233 @@
1
+ import type { Hono, TypedResponse } from 'hono'
2
+ import type { BlankEnv, BlankInput, ToSchema } from 'hono/types'
3
+ import * as v from 'valibot'
4
+
5
+ /**
6
+ * The contract between editors, Cloud, and the services Cloud advertises: AI models today, and
7
+ * other paid capabilities such as image generation and search as they arrive. Cloud only
8
+ * advertises the service and signs short-lived tokens for it; the service, official or
9
+ * self-hosted, calls the providers and keeps the credit wallet. It is versioned apart from the
10
+ * Cloud protocol so a service can change without a Cloud release.
11
+ */
12
+ export const CLOUD_SERVICES_CONTRACT_VERSION = '1' as const
13
+ /** The `aud` of every token Cloud signs for its services. */
14
+ export const CLOUD_SERVICES_TOKEN_AUDIENCE = 'openpencil-services' as const
15
+ /** Where Cloud publishes the public keys a service verifies its tokens with. */
16
+ export const CLOUD_SERVICES_JWKS_PATH = '/.well-known/jwks.json' as const
17
+
18
+ /**
19
+ * Where a service answers, below the discovered `endpoint`. Model requests follow the OpenAI chat
20
+ * completions API, so an OpenAI-compatible client takes `${endpoint}${base}` as its base URL.
21
+ */
22
+ export const CLOUD_SERVICES_API_PATHS = {
23
+ base: '/v1',
24
+ chatCompletions: '/v1/chat/completions',
25
+ catalog: '/v1/catalog',
26
+ wallet: '/v1/wallet',
27
+ usage: '/v1/usage',
28
+ search: '/v1/search'
29
+ } as const
30
+
31
+ const urlSchema = v.pipe(v.string(), v.url())
32
+ const creditsSchema = v.pipe(v.number(), v.integer(), v.minValue(0))
33
+
34
+ export const cloudServicesDiscoverySchema = v.object({
35
+ endpoint: urlSchema,
36
+ contractVersion: v.literal(CLOUD_SERVICES_CONTRACT_VERSION)
37
+ })
38
+ export type CloudServicesDiscovery = v.InferOutput<typeof cloudServicesDiscoverySchema>
39
+
40
+ export const cloudBillingDiscoverySchema = v.object({ portalURL: urlSchema })
41
+ export type CloudBillingDiscovery = v.InferOutput<typeof cloudBillingDiscoverySchema>
42
+
43
+ export const createServicesTokenSchema = v.object({ workspaceId: v.pipe(v.string(), v.uuid()) })
44
+ export type CreateServicesTokenInput = v.InferOutput<typeof createServicesTokenSchema>
45
+
46
+ export function parseCreateServicesToken(input: unknown): CreateServicesTokenInput {
47
+ return v.parse(createServicesTokenSchema, input)
48
+ }
49
+
50
+ export const servicesTokenSchema = v.object({
51
+ token: v.pipe(v.string(), v.minLength(1)),
52
+ expiresAt: v.pipe(v.string(), v.isoTimestamp())
53
+ })
54
+ export type ServicesToken = v.InferOutput<typeof servicesTokenSchema>
55
+
56
+ /** What a service may rely on once a token's signature, issuer, audience, and expiry check. */
57
+ export const servicesTokenClaimsSchema = v.object({
58
+ iss: urlSchema,
59
+ aud: v.literal(CLOUD_SERVICES_TOKEN_AUDIENCE),
60
+ sub: v.pipe(v.string(), v.minLength(1)),
61
+ workspaceId: v.pipe(v.string(), v.uuid()),
62
+ /**
63
+ * The account whose credits the request spends: the workspace's owner, whose allowance every
64
+ * workspace they own shares. Absent from tokens a Cloud from before account plans issued, which
65
+ * a relay takes as the workspace's own allowance, as such a Cloud meant.
66
+ */
67
+ accountId: v.optional(v.pipe(v.string(), v.minLength(1))),
68
+ /** Credits the owner's plan grants each month; `null` when Cloud sets no allowance. */
69
+ monthlyCreditAllowance: v.nullable(creditsSchema),
70
+ iat: v.pipe(v.number(), v.integer()),
71
+ exp: v.pipe(v.number(), v.integer())
72
+ })
73
+ export type ServicesTokenClaims = v.InferOutput<typeof servicesTokenClaimsSchema>
74
+
75
+ export const chatModelSchema = v.object({
76
+ id: v.pipe(v.string(), v.minLength(1)),
77
+ label: v.pipe(v.string(), v.minLength(1)),
78
+ /** Credits per million input and output tokens. */
79
+ creditsPerMillionInputTokens: creditsSchema,
80
+ creditsPerMillionOutputTokens: creditsSchema,
81
+ contextTokens: v.pipe(v.number(), v.integer(), v.minValue(1)),
82
+ capabilities: v.object({ reasoning: v.boolean(), vision: v.boolean(), tools: v.boolean() })
83
+ })
84
+ export type ChatModel = v.InferOutput<typeof chatModelSchema>
85
+
86
+ /** What one web search costs; a service without search leaves it out of its catalog. */
87
+ export const searchPricingSchema = v.object({ creditsPerRequest: creditsSchema })
88
+ export type SearchPricing = v.InferOutput<typeof searchPricingSchema>
89
+
90
+ export const serviceCatalogSchema = v.object({
91
+ models: v.array(chatModelSchema),
92
+ defaultModelID: v.pipe(v.string(), v.minLength(1)),
93
+ search: v.optional(searchPricingSchema)
94
+ })
95
+ export type ServiceCatalog = v.InferOutput<typeof serviceCatalogSchema>
96
+
97
+ export const creditBucketSchema = v.object({
98
+ kind: v.picklist(['allowance', 'purchased', 'grant']),
99
+ remaining: creditsSchema,
100
+ expiresAt: v.nullable(v.pipe(v.string(), v.isoTimestamp()))
101
+ })
102
+ export type CreditBucket = v.InferOutput<typeof creditBucketSchema>
103
+
104
+ /**
105
+ * A service records a request's charge before the request's answer ends, so a wallet read after
106
+ * an answer includes what it cost.
107
+ */
108
+ export const creditWalletSchema = v.object({
109
+ /** Credits left to spend; `null` when the service sets no limit. */
110
+ balance: v.nullable(creditsSchema),
111
+ /** Spent first to last. */
112
+ buckets: v.array(creditBucketSchema)
113
+ })
114
+ export type CreditWallet = v.InferOutput<typeof creditWalletSchema>
115
+
116
+ export const usageEntrySchema = v.object({
117
+ id: v.pipe(v.string(), v.minLength(1)),
118
+ createdAt: v.pipe(v.string(), v.isoTimestamp()),
119
+ kind: v.picklist(['model', 'image', 'search']),
120
+ modelID: v.nullable(v.string()),
121
+ userId: v.pipe(v.string(), v.minLength(1)),
122
+ credits: creditsSchema
123
+ })
124
+ export type UsageEntry = v.InferOutput<typeof usageEntrySchema>
125
+
126
+ /** The most results one search returns. */
127
+ export const MAX_SEARCH_RESULTS = 10
128
+ /** The longest query a search takes, in characters. */
129
+ export const MAX_SEARCH_QUERY_LENGTH = 1_000
130
+
131
+ export const searchRequestSchema = v.object({
132
+ query: v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(MAX_SEARCH_QUERY_LENGTH)),
133
+ numResults: v.optional(
134
+ v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(MAX_SEARCH_RESULTS)),
135
+ 5
136
+ )
137
+ })
138
+ export type SearchRequestInput = v.InferInput<typeof searchRequestSchema>
139
+ export type SearchRequest = v.InferOutput<typeof searchRequestSchema>
140
+
141
+ export function parseSearchRequest(input: unknown): SearchRequest {
142
+ return v.parse(searchRequestSchema, input)
143
+ }
144
+
145
+ /** One page a search found, with the part of its text that answers the query. */
146
+ export const searchResultSchema = v.object({
147
+ // Only web pages: editors open it when clicked, so a `javascript:` URL must never pass.
148
+ url: v.pipe(urlSchema, v.regex(/^https?:\/\//i, 'A search result links to an HTTP(S) page')),
149
+ title: v.string(),
150
+ publishedAt: v.nullable(v.pipe(v.string(), v.isoTimestamp())),
151
+ author: v.nullable(v.string()),
152
+ text: v.string()
153
+ })
154
+ export type SearchResult = v.InferOutput<typeof searchResultSchema>
155
+
156
+ export const searchResponseSchema = v.object({ results: v.array(searchResultSchema) })
157
+ export type SearchResponse = v.InferOutput<typeof searchResponseSchema>
158
+
159
+ export const usageQuerySchema = v.object({
160
+ cursor: v.optional(v.pipe(v.string(), v.minLength(1)))
161
+ })
162
+ export type UsageQuery = v.InferOutput<typeof usageQuerySchema>
163
+
164
+ export function parseUsageQuery(input: unknown): UsageQuery {
165
+ return v.parse(usageQuerySchema, input)
166
+ }
167
+
168
+ export const usagePageSchema = v.object({
169
+ entries: v.array(usageEntrySchema),
170
+ next: v.nullable(v.string())
171
+ })
172
+ export type UsagePage = v.InferOutput<typeof usagePageSchema>
173
+
174
+ /** Errors a service answers with, beside the upstream provider's own. */
175
+ export const serviceErrorCodeSchema = v.picklist([
176
+ 'invalid_request',
177
+ 'unauthorized',
178
+ 'insufficient_credits',
179
+ 'model_not_allowed',
180
+ 'capability_unavailable',
181
+ 'rate_limited'
182
+ ])
183
+ export type ServiceErrorCode = v.InferOutput<typeof serviceErrorCodeSchema>
184
+
185
+ export const serviceErrorSchema = v.object({
186
+ error: v.object({
187
+ code: serviceErrorCodeSchema,
188
+ message: v.optional(v.string()),
189
+ /** With `insufficient_credits`: when the workspace's allowance renews, if the service knows. */
190
+ resetsAt: v.optional(v.pipe(v.string(), v.isoTimestamp()))
191
+ })
192
+ })
193
+ export type ServiceError = v.InferOutput<typeof serviceErrorSchema>
194
+
195
+ export const SERVICE_ERROR_STATUS = {
196
+ invalid_request: 400,
197
+ unauthorized: 401,
198
+ insufficient_credits: 402,
199
+ model_not_allowed: 403,
200
+ /** A capability, such as search, the service does not offer. */
201
+ capability_unavailable: 404,
202
+ rate_limited: 429
203
+ } as const satisfies Record<ServiceErrorCode, number>
204
+
205
+ /** A route's input, and the JSON it answers with a 200. */
206
+ type JSONRoute<
207
+ Method extends string,
208
+ Path extends string,
209
+ Input extends BlankInput,
210
+ Output
211
+ > = ToSchema<Method, Path, Input, TypedResponse<Output, 200, 'json'>>
212
+
213
+ /**
214
+ * Every route a service answers besides chat completions, which OpenAI-compatible clients call
215
+ * themselves. Editors build a typed client from it with Hono's `hc`; a service's own app must
216
+ * match it, so the two cannot drift apart.
217
+ */
218
+ export type CloudServicesSchema = JSONRoute<
219
+ 'get',
220
+ typeof CLOUD_SERVICES_API_PATHS.catalog,
221
+ BlankInput,
222
+ ServiceCatalog
223
+ > &
224
+ JSONRoute<'get', typeof CLOUD_SERVICES_API_PATHS.wallet, BlankInput, CreditWallet> &
225
+ JSONRoute<'get', typeof CLOUD_SERVICES_API_PATHS.usage, { query: UsageQuery }, UsagePage> &
226
+ JSONRoute<
227
+ 'post',
228
+ typeof CLOUD_SERVICES_API_PATHS.search,
229
+ { json: SearchRequestInput },
230
+ SearchResponse
231
+ >
232
+
233
+ export type CloudServicesAPI = Hono<BlankEnv, CloudServicesSchema>