@open-pencil/cloud 0.0.0-stage → 0.17.0-beta.1
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,27 @@
|
|
|
1
|
+
import { defineComponent, type PropType } from 'vue'
|
|
2
|
+
|
|
3
|
+
import { transactionalEmailLayout } from './layout'
|
|
4
|
+
|
|
5
|
+
export type EnrollmentEmailProps = {
|
|
6
|
+
heading: string
|
|
7
|
+
preview: string
|
|
8
|
+
message: string
|
|
9
|
+
actionLabel: string
|
|
10
|
+
actionURL: string
|
|
11
|
+
/** Small print under the button. */
|
|
12
|
+
footer?: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const EnrollmentEmail = defineComponent({
|
|
16
|
+
props: {
|
|
17
|
+
heading: { type: String, required: true },
|
|
18
|
+
preview: { type: String, required: true },
|
|
19
|
+
message: { type: String, required: true },
|
|
20
|
+
actionLabel: { type: String, required: true },
|
|
21
|
+
actionURL: { type: String, required: true },
|
|
22
|
+
footer: { type: String, required: false }
|
|
23
|
+
} satisfies Record<keyof EnrollmentEmailProps, { type: PropType<string>; required: boolean }>,
|
|
24
|
+
setup(props) {
|
|
25
|
+
return () => transactionalEmailLayout(props)
|
|
26
|
+
}
|
|
27
|
+
})
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { VNode } from 'vue'
|
|
2
|
+
|
|
3
|
+
import { emailLayout } from './brand'
|
|
4
|
+
|
|
5
|
+
export type TransactionalEmailLayout = {
|
|
6
|
+
preview: string
|
|
7
|
+
heading: string
|
|
8
|
+
message: string
|
|
9
|
+
actionLabel: string
|
|
10
|
+
actionURL: string
|
|
11
|
+
footer?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Cloud's mail in the same branded layout as the waitlist's, so people know who wrote. */
|
|
15
|
+
export function transactionalEmailLayout(props: TransactionalEmailLayout): VNode {
|
|
16
|
+
const { footer, ...content } = props
|
|
17
|
+
return emailLayout({ ...content, note: footer ? { text: footer } : undefined })
|
|
18
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TransactionalEmailTransportError,
|
|
3
|
+
type TransactionalEmailEnvelope,
|
|
4
|
+
type TransactionalEmailTransport,
|
|
5
|
+
type TransactionalEmailTransportErrorKind
|
|
6
|
+
} from '#cloud/server/email'
|
|
7
|
+
|
|
8
|
+
/** The part of a Worker's `send_email` binding the transport uses. */
|
|
9
|
+
export type CloudflareEmailBinding = {
|
|
10
|
+
send(message: {
|
|
11
|
+
to: string
|
|
12
|
+
from: string
|
|
13
|
+
subject: string
|
|
14
|
+
html: string
|
|
15
|
+
text: string
|
|
16
|
+
headers?: Record<string, string>
|
|
17
|
+
}): Promise<{ messageId: string }>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Cloudflare Email Service error codes, by what retrying them would do. */
|
|
21
|
+
const ERROR_KINDS: Record<string, TransactionalEmailTransportErrorKind> = {
|
|
22
|
+
E_SENDER_NOT_VERIFIED: 'configuration',
|
|
23
|
+
E_RECIPIENT_NOT_ALLOWED: 'configuration',
|
|
24
|
+
E_SENDER_DOMAIN_NOT_AVAILABLE: 'configuration',
|
|
25
|
+
E_RECIPIENT_SUPPRESSED: 'suppressed',
|
|
26
|
+
E_VALIDATION_ERROR: 'permanent',
|
|
27
|
+
E_FIELD_MISSING: 'permanent',
|
|
28
|
+
E_TOO_MANY_RECIPIENTS: 'permanent',
|
|
29
|
+
E_TOO_MANY_ATTACHMENTS: 'permanent',
|
|
30
|
+
E_CONTENT_TOO_LARGE: 'permanent',
|
|
31
|
+
E_DELIVERY_FAILED: 'permanent',
|
|
32
|
+
E_RATE_LIMIT_EXCEEDED: 'rate-limited',
|
|
33
|
+
E_DAILY_LIMIT_EXCEEDED: 'rate-limited'
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function errorCode(error: unknown): string {
|
|
37
|
+
return error instanceof Error && 'code' in error && typeof error.code === 'string'
|
|
38
|
+
? error.code
|
|
39
|
+
: 'E_UNKNOWN'
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Sends transactional email through a Worker's `send_email` binding. */
|
|
43
|
+
export function createCloudflareEmailTransport(
|
|
44
|
+
binding: CloudflareEmailBinding
|
|
45
|
+
): TransactionalEmailTransport {
|
|
46
|
+
return {
|
|
47
|
+
id: 'cloudflare-email-service',
|
|
48
|
+
async send(message: TransactionalEmailEnvelope) {
|
|
49
|
+
try {
|
|
50
|
+
const result = await binding.send({
|
|
51
|
+
to: message.to,
|
|
52
|
+
from: message.from,
|
|
53
|
+
subject: message.subject,
|
|
54
|
+
html: message.html,
|
|
55
|
+
text: message.text,
|
|
56
|
+
headers: message.headers
|
|
57
|
+
})
|
|
58
|
+
return { transportMessageId: result.messageId, acceptedAt: new Date().toISOString() }
|
|
59
|
+
} catch (error) {
|
|
60
|
+
const code = errorCode(error)
|
|
61
|
+
throw new TransactionalEmailTransportError(
|
|
62
|
+
ERROR_KINDS[code] ?? 'transient',
|
|
63
|
+
code,
|
|
64
|
+
'Cloudflare Email Service rejected a transactional email',
|
|
65
|
+
{ cause: error }
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
loadAISigningKey,
|
|
3
|
+
parseCloudDeploymentTOML,
|
|
4
|
+
type AISigningKey,
|
|
5
|
+
type CloudEnvironment,
|
|
6
|
+
type CloudServerConfig
|
|
7
|
+
} from '#cloud/server'
|
|
8
|
+
|
|
9
|
+
import type { CollaborationRelayRPC } from './relay'
|
|
10
|
+
|
|
11
|
+
/** The bindings `deploy/cloudflare/wrangler.jsonc` gives the Worker and its relay objects. */
|
|
12
|
+
export type CloudflareCloudBindings = {
|
|
13
|
+
/** PostgreSQL, through Hyperdrive. */
|
|
14
|
+
HYPERDRIVE: Hyperdrive
|
|
15
|
+
/** The collaboration relay, one object per document. */
|
|
16
|
+
RELAY: DurableObjectNamespace<CollaborationRelayRPC>
|
|
17
|
+
/** The built site, `dist-cloud`: the editor and the portal. */
|
|
18
|
+
ASSETS: Fetcher
|
|
19
|
+
/** Required by `[email] transport = "cloudflare"`. */
|
|
20
|
+
EMAIL?: SendEmail
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The bindings, plus the variables and secrets the deployment TOML names. */
|
|
24
|
+
export type CloudflareCloudEnvironment = CloudflareCloudBindings & Record<string, unknown>
|
|
25
|
+
|
|
26
|
+
export type PreparedCloudflareCloud = {
|
|
27
|
+
config: CloudServerConfig
|
|
28
|
+
aiSigningKey: AISigningKey | undefined
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The Worker's string variables and secrets, with `DATABASE_URL` from Hyperdrive. */
|
|
32
|
+
function configEnvironment(env: CloudflareCloudEnvironment): CloudEnvironment {
|
|
33
|
+
return {
|
|
34
|
+
...Object.fromEntries(
|
|
35
|
+
Object.entries(env).filter((entry): entry is [string, string] => typeof entry[1] === 'string')
|
|
36
|
+
),
|
|
37
|
+
DATABASE_URL: env.HYPERDRIVE.connectionString
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Reads the deployment TOML once per isolate: its config and AI signing key stay the same for the
|
|
43
|
+
* Worker's lifetime. A failure is not kept, so the next request tries again.
|
|
44
|
+
*/
|
|
45
|
+
export function cloudflareCloudSetup(source: string) {
|
|
46
|
+
let prepared: Promise<PreparedCloudflareCloud> | null = null
|
|
47
|
+
return (env: CloudflareCloudEnvironment): Promise<PreparedCloudflareCloud> => {
|
|
48
|
+
prepared ??= (async () => {
|
|
49
|
+
const config = parseCloudDeploymentTOML(source, configEnvironment(env))
|
|
50
|
+
return { config, aiSigningKey: await loadAISigningKey(config) }
|
|
51
|
+
})().catch((error: unknown) => {
|
|
52
|
+
prepared = null
|
|
53
|
+
throw error
|
|
54
|
+
})
|
|
55
|
+
return prepared
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { RELAY_DOCUMENT_PARAM } from '#cloud/contract'
|
|
2
|
+
import { createPostgresCloudDatabase } from '#cloud/runtime/postgres/database'
|
|
3
|
+
import {
|
|
4
|
+
createCollaborationRelay,
|
|
5
|
+
createCollaborationStateStore,
|
|
6
|
+
createConfiguredCloudPolicy,
|
|
7
|
+
relayDocumentLookups,
|
|
8
|
+
type CollaborationRelay,
|
|
9
|
+
type RelayConnection
|
|
10
|
+
} from '#cloud/server'
|
|
11
|
+
import { DurableObject } from 'cloudflare:workers'
|
|
12
|
+
import type { Context } from 'hono'
|
|
13
|
+
import * as v from 'valibot'
|
|
14
|
+
|
|
15
|
+
import type { CloudflareCloudEnvironment, PreparedCloudflareCloud } from './environment'
|
|
16
|
+
|
|
17
|
+
/** What the Worker calls on a document's relay object. */
|
|
18
|
+
export interface CollaborationRelayRPC extends DurableObject<CloudflareCloudEnvironment> {
|
|
19
|
+
/** Ends the document's live rooms after a restore, without saving them. */
|
|
20
|
+
startOver(documentId: string): void
|
|
21
|
+
/** Tells the document's live rooms its version history changed. */
|
|
22
|
+
announceVersions(documentId: string): void
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const documentIdSchema = v.pipe(v.string(), v.uuid())
|
|
26
|
+
|
|
27
|
+
/** The document's relay object. */
|
|
28
|
+
export function relayFor(env: CloudflareCloudEnvironment, documentId: string) {
|
|
29
|
+
return env.RELAY.get(env.RELAY.idFromName(documentId))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Sends a relay socket to its document's relay object. The ticket's URL names the document; the
|
|
34
|
+
* object refuses tickets for any other.
|
|
35
|
+
*/
|
|
36
|
+
export async function routeToRelay(
|
|
37
|
+
c: Context<{ Bindings: CloudflareCloudEnvironment }>
|
|
38
|
+
): Promise<Response> {
|
|
39
|
+
if (c.req.header('Upgrade')?.toLowerCase() !== 'websocket') {
|
|
40
|
+
return c.text('Expected a WebSocket upgrade', 426)
|
|
41
|
+
}
|
|
42
|
+
const documentId = c.req.query(RELAY_DOCUMENT_PARAM)
|
|
43
|
+
if (!v.is(documentIdSchema, documentId)) return c.text('Missing document', 400)
|
|
44
|
+
return relayFor(c.env, documentId).fetch(c.req.raw)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A socket message's bytes; text is passed on as bytes for the relay to refuse. */
|
|
48
|
+
function bytes(data: unknown): Uint8Array {
|
|
49
|
+
if (data instanceof ArrayBuffer) return new Uint8Array(data)
|
|
50
|
+
return new TextEncoder().encode(typeof data === 'string' ? data : '')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Hands a WebSocket the object accepted to the relay, as the Node runtime does with `ws`. */
|
|
54
|
+
function accept(relay: CollaborationRelay): Response {
|
|
55
|
+
const { 0: client, 1: server } = new WebSocketPair()
|
|
56
|
+
// Standard sockets deliver binary messages as Blobs unless asked otherwise.
|
|
57
|
+
server.binaryType = 'arraybuffer'
|
|
58
|
+
server.accept()
|
|
59
|
+
let connection: RelayConnection | null = null
|
|
60
|
+
const closed = () => {
|
|
61
|
+
connection?.closed()
|
|
62
|
+
connection = null
|
|
63
|
+
}
|
|
64
|
+
// A closing socket throws on send and close, which would stop a broadcast to the others.
|
|
65
|
+
const open = () => server.readyState === WebSocket.OPEN
|
|
66
|
+
connection = relay.connect({
|
|
67
|
+
send(data) {
|
|
68
|
+
if (open()) server.send(data)
|
|
69
|
+
},
|
|
70
|
+
close(code, reason) {
|
|
71
|
+
if (open()) server.close(code, reason)
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
server.addEventListener('message', ({ data }: { data: unknown }) =>
|
|
75
|
+
connection?.receive(bytes(data))
|
|
76
|
+
)
|
|
77
|
+
server.addEventListener('close', closed)
|
|
78
|
+
server.addEventListener('error', closed)
|
|
79
|
+
return new Response(null, { status: 101, webSocket: client })
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The Durable Object class holding one document's live rooms: every epoch of the document meets
|
|
84
|
+
* on one relay, so a restore or a version change reaches all of them in one call. Sockets stay
|
|
85
|
+
* open without hibernation, so the rooms' documents and timers live in memory as on Node.
|
|
86
|
+
*/
|
|
87
|
+
export function createCloudflareRelayObject(
|
|
88
|
+
prepare: (env: CloudflareCloudEnvironment) => Promise<PreparedCloudflareCloud>
|
|
89
|
+
) {
|
|
90
|
+
return class CollaborationRelayObject
|
|
91
|
+
extends DurableObject<CloudflareCloudEnvironment>
|
|
92
|
+
implements CollaborationRelayRPC
|
|
93
|
+
{
|
|
94
|
+
#relay: Promise<CollaborationRelay> | null = null
|
|
95
|
+
/** Set by the first socket; restores and version changes before it have no rooms to reach. */
|
|
96
|
+
#opened: CollaborationRelay | null = null
|
|
97
|
+
|
|
98
|
+
#open(documentId: string) {
|
|
99
|
+
this.#relay ??= prepare(this.env).then(({ config }) => {
|
|
100
|
+
const database = createPostgresCloudDatabase({ connectionString: config.databaseURL })
|
|
101
|
+
this.#opened = createCollaborationRelay({
|
|
102
|
+
authSecret: config.authSecret,
|
|
103
|
+
documentId,
|
|
104
|
+
store: createCollaborationStateStore(database),
|
|
105
|
+
maximumMessageBytes: config.technicalLimits.maximumCollaborationMessageBytes,
|
|
106
|
+
maximumConnectionsPerRoom: config.technicalLimits.maximumConnectionsPerRoom,
|
|
107
|
+
...relayDocumentLookups(
|
|
108
|
+
database,
|
|
109
|
+
createConfiguredCloudPolicy(config, database),
|
|
110
|
+
config.deployment
|
|
111
|
+
)
|
|
112
|
+
})
|
|
113
|
+
return this.#opened
|
|
114
|
+
})
|
|
115
|
+
this.#relay.catch(() => {
|
|
116
|
+
this.#relay = null
|
|
117
|
+
})
|
|
118
|
+
return this.#relay
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Accepts a socket that `routeToRelay` sent here. */
|
|
122
|
+
override async fetch(request: Request): Promise<Response> {
|
|
123
|
+
const documentId = new URL(request.url).searchParams.get(RELAY_DOCUMENT_PARAM)
|
|
124
|
+
if (!v.is(documentIdSchema, documentId)) return new Response(null, { status: 400 })
|
|
125
|
+
return accept(await this.#open(documentId))
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
startOver(documentId: string) {
|
|
129
|
+
this.#opened?.startOver(documentId)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
announceVersions(documentId: string) {
|
|
133
|
+
this.#opened?.announceVersions(documentId)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SiteFiles } from '#cloud/runtime/site/server'
|
|
2
|
+
|
|
3
|
+
/** The built site's files from the Worker's static assets. */
|
|
4
|
+
export function assetSiteFiles(assets: Fetcher): SiteFiles {
|
|
5
|
+
return async (path, request) => {
|
|
6
|
+
const file = await assets.fetch(new URL(path, request.url))
|
|
7
|
+
if (!file.ok || !file.body) return null
|
|
8
|
+
return {
|
|
9
|
+
body: file.body,
|
|
10
|
+
type: file.headers.get('Content-Type') ?? 'application/octet-stream'
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"noUncheckedIndexedAccess": true,
|
|
5
|
+
"noImplicitOverride": true,
|
|
6
|
+
"useUnknownInCatchVariables": true,
|
|
7
|
+
"noFallthroughCasesInSwitch": true,
|
|
8
|
+
"lib": ["ES2023"],
|
|
9
|
+
"module": "ESNext",
|
|
10
|
+
"moduleResolution": "bundler",
|
|
11
|
+
"strict": true,
|
|
12
|
+
"noEmit": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"types": ["@cloudflare/workers-types"],
|
|
15
|
+
"paths": {
|
|
16
|
+
"#cloud/*": ["../../*"]
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"include": ["./**/*.ts", "../../../deploy/**/*.ts"],
|
|
20
|
+
"exclude": ["../../../deploy/generated"]
|
|
21
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { defaultRelayURL, RELAY_PATH } from '#cloud/contract'
|
|
2
|
+
import { createPostgresCloudDatabase } from '#cloud/runtime/postgres/database'
|
|
3
|
+
import { createS3ObjectStore } from '#cloud/runtime/s3/objects'
|
|
4
|
+
import { createCloudServer } from '#cloud/runtime/site/server'
|
|
5
|
+
import {
|
|
6
|
+
cleanupPassOptions,
|
|
7
|
+
createCleanupServices,
|
|
8
|
+
createCloudApp,
|
|
9
|
+
createCloudAuthenticationRuntime,
|
|
10
|
+
createConfiguredCloudPolicy,
|
|
11
|
+
createInvitationOutbox,
|
|
12
|
+
createTransactionalEmailService,
|
|
13
|
+
runCleanup,
|
|
14
|
+
type TransactionalEmailService
|
|
15
|
+
} from '#cloud/server'
|
|
16
|
+
import { Hono } from 'hono'
|
|
17
|
+
|
|
18
|
+
import { createCloudflareEmailTransport } from './email'
|
|
19
|
+
import {
|
|
20
|
+
cloudflareCloudSetup,
|
|
21
|
+
type CloudflareCloudEnvironment,
|
|
22
|
+
type PreparedCloudflareCloud
|
|
23
|
+
} from './environment'
|
|
24
|
+
import { createCloudflareRelayObject, relayFor, routeToRelay } from './relay'
|
|
25
|
+
import { assetSiteFiles } from './site'
|
|
26
|
+
|
|
27
|
+
function emailTransport({ config }: PreparedCloudflareCloud, env: CloudflareCloudEnvironment) {
|
|
28
|
+
if (config.emailTransport !== 'cloudflare') return undefined
|
|
29
|
+
if (!env.EMAIL) throw new Error('[email] transport = "cloudflare" needs the EMAIL binding')
|
|
30
|
+
return createCloudflareEmailTransport(env.EMAIL)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* What one request or cron run needs. Workers can't share connections between requests, so each
|
|
35
|
+
* gets its own database client, closed once its background work settles.
|
|
36
|
+
*/
|
|
37
|
+
function cloudServices(
|
|
38
|
+
prepared: PreparedCloudflareCloud,
|
|
39
|
+
env: CloudflareCloudEnvironment,
|
|
40
|
+
context: Pick<ExecutionContext, 'waitUntil'>
|
|
41
|
+
) {
|
|
42
|
+
const { config } = prepared
|
|
43
|
+
const database = createPostgresCloudDatabase({ connectionString: config.databaseURL })
|
|
44
|
+
const objects = createS3ObjectStore(config)
|
|
45
|
+
const transport = emailTransport(prepared, env)
|
|
46
|
+
const queued = createTransactionalEmailService(database, {
|
|
47
|
+
encryptionSecret: config.authSecret,
|
|
48
|
+
from: config.emailFrom ?? '',
|
|
49
|
+
transport
|
|
50
|
+
})
|
|
51
|
+
// Mail is sent once the request is done rather than by a polling worker, so sign-in links
|
|
52
|
+
// don't wait for the next cron run.
|
|
53
|
+
let enqueued = false
|
|
54
|
+
const email: TransactionalEmailService = {
|
|
55
|
+
...queued,
|
|
56
|
+
enqueue(input, transaction) {
|
|
57
|
+
enqueued = true
|
|
58
|
+
return queued.enqueue(input, transaction)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const background: Promise<unknown>[] = []
|
|
62
|
+
const runInBackground = (task: Promise<unknown>) => {
|
|
63
|
+
background.push(
|
|
64
|
+
task.catch((error: unknown) => console.error('[Cloud] Background work failed:', error))
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
const deliver = () =>
|
|
68
|
+
email.deliverPending({
|
|
69
|
+
batchSize: config.emailBatchSize,
|
|
70
|
+
leaseDurationMs: config.emailLeaseDurationMs,
|
|
71
|
+
maximumAttempts: config.emailMaximumAttempts
|
|
72
|
+
})
|
|
73
|
+
return {
|
|
74
|
+
config,
|
|
75
|
+
database,
|
|
76
|
+
objects,
|
|
77
|
+
email,
|
|
78
|
+
transport,
|
|
79
|
+
runInBackground,
|
|
80
|
+
deliver,
|
|
81
|
+
/** Sends what the request queued and closes the database after everything it started. */
|
|
82
|
+
finish() {
|
|
83
|
+
context.waitUntil(
|
|
84
|
+
Promise.allSettled(background)
|
|
85
|
+
.then(() => (enqueued && transport ? deliver() : undefined))
|
|
86
|
+
.catch((error: unknown) => console.error('[Cloud] Sending email failed:', error))
|
|
87
|
+
.then(() => database.destroy())
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* OpenPencil Cloud on Cloudflare Workers, configured by a deployment TOML. The Worker serves the
|
|
95
|
+
* API, the editor, and the portal per request, a Durable Object per document runs its collaboration rooms, and a
|
|
96
|
+
* Cron Trigger runs cleanup and retries email.
|
|
97
|
+
*/
|
|
98
|
+
export function createCloudflareCloud(configSource: string) {
|
|
99
|
+
const prepare = cloudflareCloudSetup(configSource)
|
|
100
|
+
|
|
101
|
+
const router = new Hono<{ Bindings: CloudflareCloudEnvironment }>()
|
|
102
|
+
.get(RELAY_PATH, routeToRelay)
|
|
103
|
+
.all('*', async (c) => {
|
|
104
|
+
const prepared = await prepare(c.env)
|
|
105
|
+
const services = cloudServices(prepared, c.env, c.executionCtx)
|
|
106
|
+
const { config, database, email, transport, runInBackground } = services
|
|
107
|
+
try {
|
|
108
|
+
const { auth, enrollment } = createCloudAuthenticationRuntime(config, database, email, {
|
|
109
|
+
runInBackground,
|
|
110
|
+
validateSchema: false
|
|
111
|
+
})
|
|
112
|
+
const app = createCloudApp({
|
|
113
|
+
config,
|
|
114
|
+
database,
|
|
115
|
+
auth,
|
|
116
|
+
objects: services.objects,
|
|
117
|
+
invitationOutbox: transport ? createInvitationOutbox(email) : undefined,
|
|
118
|
+
transactionalEmail: email,
|
|
119
|
+
enrollment,
|
|
120
|
+
aiSigningKey: prepared.aiSigningKey,
|
|
121
|
+
relayURL: config.relayURL ?? defaultRelayURL(config.publicURL),
|
|
122
|
+
onDocumentRestored: (documentId) =>
|
|
123
|
+
runInBackground(relayFor(c.env, documentId).startOver(documentId)),
|
|
124
|
+
onDocumentVersionsChanged: (documentId) =>
|
|
125
|
+
runInBackground(relayFor(c.env, documentId).announceVersions(documentId))
|
|
126
|
+
})
|
|
127
|
+
return await createCloudServer(app, {
|
|
128
|
+
site: assetSiteFiles(c.env.ASSETS),
|
|
129
|
+
indexingPolicy: config.indexingPolicy
|
|
130
|
+
}).fetch(c.req.raw)
|
|
131
|
+
} finally {
|
|
132
|
+
services.finish()
|
|
133
|
+
}
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
const worker: ExportedHandler<CloudflareCloudEnvironment> = {
|
|
137
|
+
fetch: (request, env, context) => router.fetch(request, env, context),
|
|
138
|
+
|
|
139
|
+
async scheduled(_controller, env, context) {
|
|
140
|
+
const prepared = await prepare(env)
|
|
141
|
+
const services = cloudServices(prepared, env, context)
|
|
142
|
+
const { config, database, objects } = services
|
|
143
|
+
try {
|
|
144
|
+
if (config.cleanupEnabled) {
|
|
145
|
+
const policy = createConfiguredCloudPolicy(config, database)
|
|
146
|
+
await runCleanup(
|
|
147
|
+
createCleanupServices(config, database, objects, policy),
|
|
148
|
+
cleanupPassOptions(config)
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
// Retries what failed to send after its request, and what a full batch left behind.
|
|
152
|
+
if (services.transport) await services.deliver()
|
|
153
|
+
} finally {
|
|
154
|
+
services.finish()
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return { worker, CollaborationRelayObject: createCloudflareRelayObject(prepare) }
|
|
160
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { createMigratedNodeCloudDatabase } from './bootstrap'
|
|
4
|
+
|
|
5
|
+
const [operation, email] = process.argv.slice(2)
|
|
6
|
+
if ((operation !== 'approve' && operation !== 'grant') || !email) {
|
|
7
|
+
throw new Error('Usage: admin <approve|grant> <email>')
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const { database } = await createMigratedNodeCloudDatabase(process.env)
|
|
11
|
+
try {
|
|
12
|
+
const normalized = email.trim().toLowerCase()
|
|
13
|
+
if (operation === 'approve') {
|
|
14
|
+
await database
|
|
15
|
+
.insertInto('enrollment')
|
|
16
|
+
.values({
|
|
17
|
+
id: crypto.randomUUID(),
|
|
18
|
+
emailNormalized: normalized,
|
|
19
|
+
name: null,
|
|
20
|
+
reason: 'Administrative bootstrap',
|
|
21
|
+
status: 'approved',
|
|
22
|
+
reviewedAt: new Date(),
|
|
23
|
+
reviewedBy: 'cli-bootstrap',
|
|
24
|
+
reviewNote: 'Approved through CLI bootstrap',
|
|
25
|
+
approvedUserId: null
|
|
26
|
+
})
|
|
27
|
+
.onConflict((conflict) =>
|
|
28
|
+
conflict.column('emailNormalized').doUpdateSet({
|
|
29
|
+
status: 'approved',
|
|
30
|
+
reviewedAt: new Date(),
|
|
31
|
+
reviewedBy: 'cli-bootstrap',
|
|
32
|
+
reviewNote: 'Approved through CLI bootstrap'
|
|
33
|
+
})
|
|
34
|
+
)
|
|
35
|
+
.execute()
|
|
36
|
+
console.log(JSON.stringify({ approved: normalized }))
|
|
37
|
+
} else {
|
|
38
|
+
const user = await database
|
|
39
|
+
.selectFrom('user')
|
|
40
|
+
.select(['id', 'email'])
|
|
41
|
+
.where('email', '=', normalized)
|
|
42
|
+
.executeTakeFirstOrThrow()
|
|
43
|
+
await database.updateTable('user').set({ role: 'admin' }).where('id', '=', user.id).execute()
|
|
44
|
+
await database
|
|
45
|
+
.insertInto('adminAuditEvent')
|
|
46
|
+
.values({
|
|
47
|
+
id: crypto.randomUUID(),
|
|
48
|
+
actorUserId: 'cli-bootstrap',
|
|
49
|
+
action: 'user.admin-granted',
|
|
50
|
+
subjectType: 'user',
|
|
51
|
+
subjectId: user.id,
|
|
52
|
+
metadata: { email: normalized }
|
|
53
|
+
})
|
|
54
|
+
.execute()
|
|
55
|
+
console.log(JSON.stringify({ admin: normalized, userId: user.id }))
|
|
56
|
+
}
|
|
57
|
+
} finally {
|
|
58
|
+
await database.destroy()
|
|
59
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import dedent from 'dedent'
|
|
3
|
+
|
|
4
|
+
import { startNodeAIRelay } from './server'
|
|
5
|
+
|
|
6
|
+
if (process.argv.includes('--help') || process.argv.includes('-h')) {
|
|
7
|
+
console.log(dedent`
|
|
8
|
+
Usage: openpencil-ai-relay
|
|
9
|
+
|
|
10
|
+
Runs the reference AI service for a self-hosted OpenPencil Cloud. Settings come from the
|
|
11
|
+
environment:
|
|
12
|
+
OPENPENCIL_AI_RELAY_CONFIG the relay TOML file (required)
|
|
13
|
+
PORT, HOST where to listen (default 8788 on 0.0.0.0)
|
|
14
|
+
plus the secrets the TOML names. See https://openpencil.dev/reference/self-hosting-cloud
|
|
15
|
+
`)
|
|
16
|
+
process.exit(0)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const relay = await startNodeAIRelay()
|
|
20
|
+
console.log(`OpenPencil AI relay listening on ${relay.url}`)
|
|
21
|
+
|
|
22
|
+
let stopping: Promise<void> | undefined
|
|
23
|
+
|
|
24
|
+
function shutdown() {
|
|
25
|
+
stopping ??= relay.stop().then(
|
|
26
|
+
() => process.exit(0),
|
|
27
|
+
(error: unknown) => {
|
|
28
|
+
console.error('[AI relay] Shutdown failed:', error)
|
|
29
|
+
process.exit(1)
|
|
30
|
+
}
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
process.on('SIGINT', shutdown)
|
|
35
|
+
process.on('SIGTERM', shutdown)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
AIRelayConfigError,
|
|
5
|
+
createAIRelayApp,
|
|
6
|
+
createAIRelayDatabase,
|
|
7
|
+
migrateAIRelayDatabase,
|
|
8
|
+
parseAIRelayTOML,
|
|
9
|
+
type AIRelayEnvironment
|
|
10
|
+
} from '#cloud/ai-relay'
|
|
11
|
+
import { CLOUD_AI_JWKS_PATH } from '#cloud/contract'
|
|
12
|
+
import { createRemoteJWKSet } from 'jose'
|
|
13
|
+
import { PostgresDialect } from 'kysely'
|
|
14
|
+
import { Pool } from 'pg'
|
|
15
|
+
|
|
16
|
+
import { listen } from '../listen'
|
|
17
|
+
|
|
18
|
+
export type NodeAIRelayOptions = {
|
|
19
|
+
environment?: AIRelayEnvironment
|
|
20
|
+
port?: number
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function startNodeAIRelay(options: NodeAIRelayOptions = {}) {
|
|
24
|
+
const environment = options.environment ?? process.env
|
|
25
|
+
const path = environment.OPENPENCIL_AI_RELAY_CONFIG
|
|
26
|
+
if (!path) {
|
|
27
|
+
throw new AIRelayConfigError('OPENPENCIL_AI_RELAY_CONFIG must name a relay TOML file')
|
|
28
|
+
}
|
|
29
|
+
const config = parseAIRelayTOML(await readFile(path, 'utf8'), environment)
|
|
30
|
+
const database = createAIRelayDatabase(
|
|
31
|
+
new PostgresDialect({ pool: new Pool({ connectionString: config.databaseURL }) })
|
|
32
|
+
)
|
|
33
|
+
try {
|
|
34
|
+
await migrateAIRelayDatabase(database)
|
|
35
|
+
} catch (error) {
|
|
36
|
+
await database.destroy()
|
|
37
|
+
throw error
|
|
38
|
+
}
|
|
39
|
+
// Metering outlives its answer; shutdown waits for it so no request goes uncharged.
|
|
40
|
+
const metering = new Set<Promise<unknown>>()
|
|
41
|
+
const app = createAIRelayApp({
|
|
42
|
+
config,
|
|
43
|
+
database,
|
|
44
|
+
keys: createRemoteJWKSet(new URL(CLOUD_AI_JWKS_PATH, config.cloudURL)),
|
|
45
|
+
waitUntil: (promise) => {
|
|
46
|
+
metering.add(promise)
|
|
47
|
+
void promise.finally(() => metering.delete(promise))
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
const server = await listen(
|
|
51
|
+
async (request) => app.fetch(request),
|
|
52
|
+
options.port ?? Number(environment.PORT ?? 8788),
|
|
53
|
+
environment.HOST ?? '0.0.0.0'
|
|
54
|
+
)
|
|
55
|
+
return {
|
|
56
|
+
url: server.url,
|
|
57
|
+
async stop() {
|
|
58
|
+
await server.close()
|
|
59
|
+
await Promise.allSettled(metering)
|
|
60
|
+
await database.destroy()
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|