@open-pencil/cloud 0.17.0-beta.2 → 0.17.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +9 -9
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +525 -66
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1257 -654
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +1028 -481
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +1780 -31
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +3556 -325
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +929 -72
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +35 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +802 -927
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +89 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +15667 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +7166 -3101
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +6525 -2205
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +12500 -7613
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +8625 -2878
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +27 -9
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +27 -8
- package/src/admin/documents/service.ts +431 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +153 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +28 -461
- package/src/client/auth/client.ts +22 -3
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/billing.ts +80 -0
- package/src/client/community.ts +40 -0
- package/src/client/discovery.ts +35 -9
- package/src/client/documents.ts +160 -0
- package/src/client/index.ts +14 -7
- package/src/client/portal/account.ts +79 -2
- package/src/client/portal/administration.ts +56 -0
- package/src/client/portal/request.ts +2 -0
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +118 -0
- package/src/client/sharing.ts +262 -0
- package/src/client/versions.ts +104 -0
- package/src/contract/account.ts +108 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/billing.ts +166 -0
- package/src/contract/collaboration.ts +11 -1
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +83 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +12 -2
- package/src/contract/index.ts +114 -39
- package/src/contract/links.ts +57 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +235 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +141 -0
- package/src/email/locales/de.json +141 -0
- package/src/email/locales/es.json +141 -0
- package/src/email/locales/fr.json +141 -0
- package/src/email/locales/it.json +141 -0
- package/src/email/locales/ja.json +141 -0
- package/src/email/locales/pl.json +141 -0
- package/src/email/locales/pt-br.json +141 -0
- package/src/email/locales/ru.json +141 -0
- package/src/email/locales/zh-cn.json +141 -0
- package/src/email/messages.ts +200 -0
- package/src/email/render.ts +138 -246
- package/src/email/samples.ts +115 -0
- package/src/email/schema.ts +79 -5
- package/src/email/templates/BrandEmail.vue +54 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +32 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +9 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/errors/posthog.ts +80 -0
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/environment.ts +22 -11
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +69 -15
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +46 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +20 -3
- package/src/runtime/node/email/transport.ts +13 -10
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +50 -16
- package/src/runtime/node/{ai-relay → services}/main.ts +7 -7
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +15 -30
- package/src/runtime/s3/objects.ts +41 -1
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +49 -6
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +304 -0
- package/src/server/account/routes.ts +281 -206
- package/src/server/account/terms.ts +42 -0
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +32 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app/checks.ts +84 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +114 -0
- package/src/server/app/services.ts +185 -0
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/adapter.ts +32 -1
- package/src/server/auth/email.ts +4 -1
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +116 -132
- package/src/server/auth/index.ts +10 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/mfa-assurance.ts +51 -0
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/session.ts +2 -0
- package/src/server/billing/index.ts +6 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +27 -7
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +15 -3
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +77 -122
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/service.ts +48 -33
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +41 -9
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +11 -4
- package/src/server/config/schema.ts +91 -277
- package/src/server/config/toml/schema.ts +307 -0
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/index.ts +1 -0
- package/src/server/db/migrate.ts +46 -116
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/schema.ts +96 -0
- package/src/server/db/tables.ts +82 -4
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +14 -8
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +30 -406
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +94 -28
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +31 -4
- package/src/server/email/types.ts +12 -0
- package/src/server/errors/index.ts +51 -16
- package/src/server/index.ts +52 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/logging.ts +76 -0
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +52 -27
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +40 -12
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +78 -56
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +74 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +6 -2
- package/src/server/reports/index.ts +9 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +39 -0
- package/src/server/reports/service.ts +145 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -23
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +35 -15
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +4 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +91 -52
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +46 -44
- package/src/{ai-relay → services}/config.ts +26 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +35 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/client/ai.ts +0 -96
- package/src/contract/ai.ts +0 -150
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/app.ts +0 -379
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { serviceCatalogSchema, type ServiceCatalog } from '#cloud/contract'
|
|
2
2
|
import { httpURLSchema } from '#cloud/server/config/schema'
|
|
3
3
|
import { parse as parseTOML } from 'smol-toml'
|
|
4
4
|
import * as v from 'valibot'
|
|
5
5
|
|
|
6
|
-
export type
|
|
6
|
+
export type ServicesEnvironment = Readonly<Record<string, string | undefined>>
|
|
7
7
|
|
|
8
|
-
export type
|
|
8
|
+
export type ServicesConfig = {
|
|
9
9
|
/** The Cloud whose tokens the relay accepts: their issuer, and where its keys are published. */
|
|
10
10
|
cloudURL: string
|
|
11
11
|
/** Browser origins allowed to call the relay; the desktop editor is not subject to CORS. */
|
|
@@ -18,19 +18,19 @@ export type AIRelayConfig = {
|
|
|
18
18
|
/** An OpenAI-compatible API base URL, such as `https://openrouter.ai/api/v1`. */
|
|
19
19
|
upstreamURL: string
|
|
20
20
|
upstreamKey: string
|
|
21
|
-
catalog:
|
|
21
|
+
catalog: ServiceCatalog
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export class
|
|
24
|
+
export class ServicesConfigError extends Error {
|
|
25
25
|
constructor(message: string) {
|
|
26
26
|
super(message)
|
|
27
|
-
this.name = '
|
|
27
|
+
this.name = 'ServicesConfigError'
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
const DEFAULT_SECRET_NAMES = {
|
|
32
32
|
databaseURL: 'DATABASE_URL',
|
|
33
|
-
upstreamKey: '
|
|
33
|
+
upstreamKey: 'OPENPENCIL_SERVICES_UPSTREAM_KEY'
|
|
34
34
|
} as const
|
|
35
35
|
|
|
36
36
|
const textSchema = v.pipe(v.string(), v.trim(), v.minLength(1))
|
|
@@ -49,7 +49,7 @@ const modelSchema = v.object({
|
|
|
49
49
|
tools: v.optional(v.boolean(), true)
|
|
50
50
|
})
|
|
51
51
|
|
|
52
|
-
const
|
|
52
|
+
const servicesConfigSchema = v.object({
|
|
53
53
|
schema_version: v.literal(1),
|
|
54
54
|
relay: v.object({
|
|
55
55
|
cloud_url: httpURLSchema,
|
|
@@ -71,24 +71,24 @@ const aiRelayConfigSchema = v.object({
|
|
|
71
71
|
})
|
|
72
72
|
})
|
|
73
73
|
|
|
74
|
-
export type
|
|
74
|
+
export type ServicesConfigInput = v.InferInput<typeof servicesConfigSchema>
|
|
75
75
|
|
|
76
|
-
function requiredSecret(environment:
|
|
76
|
+
function requiredSecret(environment: ServicesEnvironment, name: string): string {
|
|
77
77
|
const value = environment[name]
|
|
78
|
-
if (!value) throw new
|
|
78
|
+
if (!value) throw new ServicesConfigError(`Required secret binding is unavailable: ${name}`)
|
|
79
79
|
return value
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
function catalogFrom(input: v.InferOutput<typeof
|
|
82
|
+
function catalogFrom(input: v.InferOutput<typeof servicesConfigSchema>['catalog']): ServiceCatalog {
|
|
83
83
|
const ids = new Set<string>()
|
|
84
84
|
for (const model of input.models) {
|
|
85
|
-
if (ids.has(model.id)) throw new
|
|
85
|
+
if (ids.has(model.id)) throw new ServicesConfigError(`Model ${model.id} is listed twice`)
|
|
86
86
|
ids.add(model.id)
|
|
87
87
|
}
|
|
88
88
|
if (!ids.has(input.default_model)) {
|
|
89
|
-
throw new
|
|
89
|
+
throw new ServicesConfigError(`Default model ${input.default_model} is not in the catalog`)
|
|
90
90
|
}
|
|
91
|
-
return v.parse(
|
|
91
|
+
return v.parse(serviceCatalogSchema, {
|
|
92
92
|
defaultModelID: input.default_model,
|
|
93
93
|
models: input.models.map((model) => ({
|
|
94
94
|
id: model.id,
|
|
@@ -101,20 +101,27 @@ function catalogFrom(input: v.InferOutput<typeof aiRelayConfigSchema>['catalog']
|
|
|
101
101
|
})
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
export function
|
|
105
|
-
|
|
104
|
+
export function parseServicesConfig(
|
|
105
|
+
input: unknown,
|
|
106
|
+
environment: ServicesEnvironment
|
|
107
|
+
): ServicesConfig {
|
|
108
|
+
const config = v.parse(servicesConfigSchema, input)
|
|
106
109
|
return {
|
|
107
110
|
cloudURL: new URL(config.relay.cloud_url).origin,
|
|
108
111
|
allowedOrigins: config.relay.allowed_origins.map((origin) => new URL(origin).origin),
|
|
109
112
|
maximumConcurrentRequests: config.relay.max_concurrent_requests,
|
|
110
113
|
maximumRequestBytes: config.relay.max_request_bytes,
|
|
111
114
|
databaseURL: requiredSecret(environment, config.database.url.from_env),
|
|
115
|
+
// Every trailing slash: ufo's withoutTrailingSlash removes only one.
|
|
112
116
|
upstreamURL: config.upstream.base_url.replace(/\/+$/, ''),
|
|
113
117
|
upstreamKey: requiredSecret(environment, config.upstream.api_key.from_env),
|
|
114
118
|
catalog: catalogFrom(config.catalog)
|
|
115
119
|
}
|
|
116
120
|
}
|
|
117
121
|
|
|
118
|
-
export function
|
|
119
|
-
|
|
122
|
+
export function parseServicesTOML(
|
|
123
|
+
source: string,
|
|
124
|
+
environment: ServicesEnvironment
|
|
125
|
+
): ServicesConfig {
|
|
126
|
+
return parseServicesConfig(parseTOML(source), environment)
|
|
120
127
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CamelCasePlugin, Kysely, type Dialect } from 'kysely'
|
|
2
|
+
|
|
3
|
+
import type { ServicesDatabase } from './schema'
|
|
4
|
+
|
|
5
|
+
export { migrateServicesDatabase } from './migrate'
|
|
6
|
+
export type { ServicesDatabase, ServicesUsage, NewServicesUsage } from './schema'
|
|
7
|
+
|
|
8
|
+
export function createServicesDatabase(dialect: Dialect): Kysely<ServicesDatabase> {
|
|
9
|
+
return new Kysely<ServicesDatabase>({ dialect, plugins: [new CamelCasePlugin()] })
|
|
10
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { migrateToLatest, type CloudMigration } from '#cloud/server/db/migrate'
|
|
2
|
+
import type { Kysely } from 'kysely'
|
|
3
|
+
import { Migrator, type MigrationProvider } from 'kysely/migration'
|
|
4
|
+
|
|
5
|
+
import * as baseline from './migrations/001_baseline'
|
|
6
|
+
import type { ServicesDatabase } from './schema'
|
|
7
|
+
|
|
8
|
+
/** Expand-only, as Cloud's are (see `CloudMigration`). */
|
|
9
|
+
const migrations: Record<string, CloudMigration> = {
|
|
10
|
+
'001_baseline': baseline
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const SERVICES_MIGRATION_TABLES = {
|
|
14
|
+
migrationTableName: 'services_migration',
|
|
15
|
+
migrationLockTableName: 'services_migration_lock'
|
|
16
|
+
} as const
|
|
17
|
+
|
|
18
|
+
export class ServicesMigrationProvider implements MigrationProvider {
|
|
19
|
+
async getMigrations(): Promise<Record<string, CloudMigration>> {
|
|
20
|
+
return migrations
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The services server keeps its own migration history, so it can share a PostgreSQL database
|
|
26
|
+
* with Cloud without either one seeing the other's migrations as unknown.
|
|
27
|
+
*/
|
|
28
|
+
export async function migrateServicesDatabase(database: Kysely<ServicesDatabase>): Promise<void> {
|
|
29
|
+
await migrateToLatest(
|
|
30
|
+
new Migrator({
|
|
31
|
+
db: database,
|
|
32
|
+
provider: new ServicesMigrationProvider(),
|
|
33
|
+
...SERVICES_MIGRATION_TABLES
|
|
34
|
+
}),
|
|
35
|
+
'OpenPencil services database migration failed'
|
|
36
|
+
)
|
|
37
|
+
}
|
|
@@ -1,26 +1,37 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CURRENT_TIMESTAMP } from '#cloud/server/db/expressions'
|
|
2
2
|
import type { Kysely } from 'kysely'
|
|
3
3
|
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* One row per settled model request. An account's monthly spend is the sum of its rows across the
|
|
6
|
+
* workspaces it owns; a row from a token that named no account counts for its workspace.
|
|
7
|
+
*/
|
|
5
8
|
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
6
9
|
await database.schema
|
|
7
|
-
.createTable('
|
|
10
|
+
.createTable('services_usage')
|
|
8
11
|
.addColumn('id', 'uuid', (column) => column.primaryKey())
|
|
9
12
|
.addColumn('workspace_id', 'uuid', (column) => column.notNull())
|
|
13
|
+
.addColumn('account_id', 'text')
|
|
10
14
|
.addColumn('user_id', 'text', (column) => column.notNull())
|
|
11
15
|
.addColumn('model_id', 'text', (column) => column.notNull())
|
|
12
16
|
.addColumn('input_tokens', 'integer', (column) => column.notNull())
|
|
13
17
|
.addColumn('output_tokens', 'integer', (column) => column.notNull())
|
|
14
18
|
.addColumn('credits', 'integer', (column) => column.notNull())
|
|
19
|
+
.addColumn('created_at', 'timestamptz', (column) =>
|
|
20
|
+
column.notNull().defaultTo(CURRENT_TIMESTAMP)
|
|
21
|
+
)
|
|
15
22
|
.execute()
|
|
16
|
-
await addCurrentTimestampColumns(database, 'ai_relay_usage', ['created_at'])
|
|
17
23
|
await database.schema
|
|
18
|
-
.createIndex('
|
|
19
|
-
.on('
|
|
24
|
+
.createIndex('services_usage_workspace_id_created_at_id_idx')
|
|
25
|
+
.on('services_usage')
|
|
20
26
|
.columns(['workspace_id', 'created_at desc', 'id desc'])
|
|
21
27
|
.execute()
|
|
28
|
+
await database.schema
|
|
29
|
+
.createIndex('services_usage_account_id_created_at_idx')
|
|
30
|
+
.on('services_usage')
|
|
31
|
+
.columns(['account_id', 'created_at'])
|
|
32
|
+
.execute()
|
|
22
33
|
}
|
|
23
34
|
|
|
24
35
|
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
25
|
-
await database.schema.dropTable('
|
|
36
|
+
await database.schema.dropTable('services_usage').ifExists().execute()
|
|
26
37
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { TimestampColumn } from '#cloud/server/db/tables'
|
|
2
|
+
import type { Insertable, Selectable } from 'kysely'
|
|
3
|
+
|
|
4
|
+
export interface ServicesUsageTable {
|
|
5
|
+
id: string
|
|
6
|
+
workspaceId: string
|
|
7
|
+
/** The account whose credits the request spent; null on rows from before accounts did. */
|
|
8
|
+
accountId: string | null
|
|
9
|
+
userId: string
|
|
10
|
+
modelId: string
|
|
11
|
+
inputTokens: number
|
|
12
|
+
outputTokens: number
|
|
13
|
+
credits: number
|
|
14
|
+
createdAt: TimestampColumn
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ServicesDatabase {
|
|
18
|
+
servicesUsage: ServicesUsageTable
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type ServicesUsage = Selectable<ServicesUsageTable>
|
|
22
|
+
export type NewServicesUsage = Insertable<ServicesUsageTable>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { createServicesApp, type ServicesApp, type ServicesAppDependencies } from './app'
|
|
2
|
+
export {
|
|
3
|
+
ServicesConfigError,
|
|
4
|
+
parseServicesConfig,
|
|
5
|
+
parseServicesTOML,
|
|
6
|
+
type ServicesConfig,
|
|
7
|
+
type ServicesConfigInput,
|
|
8
|
+
type ServicesEnvironment
|
|
9
|
+
} from './config'
|
|
10
|
+
export {
|
|
11
|
+
createServicesDatabase,
|
|
12
|
+
migrateServicesDatabase,
|
|
13
|
+
type ServicesDatabase,
|
|
14
|
+
type ServicesUsage,
|
|
15
|
+
type NewServicesUsage
|
|
16
|
+
} from './db'
|
|
17
|
+
export { creditsFor, meteredUsage, type TokenUsage } from './metering'
|
|
18
|
+
export { createServicesUsageStore, usagePeriod, type ServicesUsageStore } from './usage'
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ChatModel } from '#cloud/contract'
|
|
2
2
|
import * as v from 'valibot'
|
|
3
3
|
|
|
4
4
|
export type TokenUsage = { inputTokens: number; outputTokens: number }
|
|
@@ -20,7 +20,7 @@ function usageOf(text: string): TokenUsage | null {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
/** Credits a request costs, rounded up so a request is never free. */
|
|
23
|
-
export function creditsFor(model:
|
|
23
|
+
export function creditsFor(model: ChatModel, usage: TokenUsage): number {
|
|
24
24
|
return Math.ceil(
|
|
25
25
|
(usage.inputTokens * model.creditsPerMillionInputTokens +
|
|
26
26
|
usage.outputTokens * model.creditsPerMillionOutputTokens) /
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { UsagePage, CreditWallet } from '#cloud/contract'
|
|
2
2
|
import { fromDate, startOfMonth, toCalendarDate } from '@internationalized/date'
|
|
3
3
|
import type { Kysely } from 'kysely'
|
|
4
4
|
import * as v from 'valibot'
|
|
5
5
|
|
|
6
|
-
import type {
|
|
6
|
+
import type { ServicesDatabase, NewServicesUsage } from './db'
|
|
7
7
|
|
|
8
8
|
const USAGE_PAGE_SIZE = 50
|
|
9
9
|
|
|
@@ -31,20 +31,28 @@ function encodeCursor(createdAt: Date, id: string): string {
|
|
|
31
31
|
return `${createdAt.getTime()}.${id}`
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
export class
|
|
34
|
+
export class ServicesUsageCursorError extends Error {
|
|
35
35
|
constructor() {
|
|
36
36
|
super('Invalid usage cursor')
|
|
37
|
-
this.name = '
|
|
37
|
+
this.name = 'ServicesUsageCursorError'
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
/** Who a request spends for: the account whose allowance it draws on, and its workspace. */
|
|
42
|
+
export type CreditSpender = { accountId: string; workspaceId: string }
|
|
43
|
+
|
|
44
|
+
/** The spender a token names. */
|
|
45
|
+
export function spender(claims: CreditSpender): CreditSpender {
|
|
46
|
+
return { accountId: claims.accountId, workspaceId: claims.workspaceId }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function createServicesUsageStore(database: Kysely<ServicesDatabase>) {
|
|
50
|
+
async function spent({ accountId }: CreditSpender, now: Date): Promise<number> {
|
|
43
51
|
const { start, end } = usagePeriod(now)
|
|
44
52
|
const row = await database
|
|
45
|
-
.selectFrom('
|
|
53
|
+
.selectFrom('servicesUsage')
|
|
46
54
|
.select((query) => query.fn.sum<string | number | null>('credits').as('credits'))
|
|
47
|
-
.where('
|
|
55
|
+
.where('accountId', '=', accountId)
|
|
48
56
|
.where('createdAt', '>=', start)
|
|
49
57
|
.where('createdAt', '<', end)
|
|
50
58
|
.executeTakeFirst()
|
|
@@ -52,33 +60,41 @@ export function createAIUsageStore(database: Kysely<AIRelayDatabase>) {
|
|
|
52
60
|
}
|
|
53
61
|
|
|
54
62
|
return {
|
|
55
|
-
async record(usage:
|
|
56
|
-
await database.insertInto('
|
|
63
|
+
async record(usage: NewServicesUsage): Promise<void> {
|
|
64
|
+
await database.insertInto('servicesUsage').values(usage).execute()
|
|
57
65
|
},
|
|
58
66
|
|
|
59
67
|
/**
|
|
60
|
-
* The
|
|
61
|
-
* Cloud set none, and the relay does not limit spend.
|
|
68
|
+
* The account's single allowance bucket for the current month, which every workspace it owns
|
|
69
|
+
* spends from. A `null` allowance means Cloud set none, and the relay does not limit spend.
|
|
62
70
|
*/
|
|
63
|
-
async wallet(
|
|
71
|
+
async wallet(
|
|
72
|
+
spending: CreditSpender,
|
|
73
|
+
allowance: number | null,
|
|
74
|
+
now: Date
|
|
75
|
+
): Promise<CreditWallet> {
|
|
64
76
|
if (allowance === null) return { balance: null, buckets: [] }
|
|
65
|
-
const remaining = Math.max(0, allowance - (await spent(
|
|
77
|
+
const remaining = Math.max(0, allowance - (await spent(spending, now)))
|
|
66
78
|
return {
|
|
67
79
|
balance: remaining,
|
|
68
80
|
buckets: [{ kind: 'allowance', remaining, expiresAt: usagePeriod(now).end.toISOString() }]
|
|
69
81
|
}
|
|
70
82
|
},
|
|
71
83
|
|
|
72
|
-
/**
|
|
73
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Newest first, across every member of the workspace. Only this workspace's requests are
|
|
86
|
+
* listed, though its owner's other workspaces spend the same credits: their members and
|
|
87
|
+
* models are theirs to see.
|
|
88
|
+
*/
|
|
89
|
+
async page(workspaceId: string, cursor: string | undefined): Promise<UsagePage> {
|
|
74
90
|
let after: { createdAt: Date; id: string } | undefined
|
|
75
91
|
if (cursor !== undefined) {
|
|
76
92
|
const parsed = v.safeParse(cursorSchema, cursor)
|
|
77
|
-
if (!parsed.success) throw new
|
|
93
|
+
if (!parsed.success) throw new ServicesUsageCursorError()
|
|
78
94
|
after = parsed.output
|
|
79
95
|
}
|
|
80
96
|
let query = database
|
|
81
|
-
.selectFrom('
|
|
97
|
+
.selectFrom('servicesUsage')
|
|
82
98
|
.select(['id', 'createdAt', 'modelId', 'userId', 'credits'])
|
|
83
99
|
.where('workspaceId', '=', workspaceId)
|
|
84
100
|
if (after) {
|
|
@@ -112,4 +128,4 @@ export function createAIUsageStore(database: Kysely<AIRelayDatabase>) {
|
|
|
112
128
|
}
|
|
113
129
|
}
|
|
114
130
|
|
|
115
|
-
export type
|
|
131
|
+
export type ServicesUsageStore = ReturnType<typeof createServicesUsageStore>
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ai-relay.mjs","names":[],"sources":["../../src/runtime/node/ai-relay/main.ts"],"sourcesContent":["#!/usr/bin/env node\nimport dedent from 'dedent'\n\nimport { startNodeAIRelay } from './server'\n\nif (process.argv.includes('--help') || process.argv.includes('-h')) {\n console.log(dedent`\n Usage: openpencil-ai-relay\n\n Runs the reference AI service for a self-hosted OpenPencil Cloud. Settings come from the\n environment:\n OPENPENCIL_AI_RELAY_CONFIG the relay TOML file (required)\n PORT, HOST where to listen (default 8788 on 0.0.0.0)\n plus the secrets the TOML names. See https://openpencil.dev/reference/self-hosting-cloud\n `)\n process.exit(0)\n}\n\nconst relay = await startNodeAIRelay()\nconsole.log(`OpenPencil AI relay listening on ${relay.url}`)\n\nlet stopping: Promise<void> | undefined\n\nfunction shutdown() {\n stopping ??= relay.stop().then(\n () => process.exit(0),\n (error: unknown) => {\n console.error('[AI relay] Shutdown failed:', error)\n process.exit(1)\n }\n )\n}\n\nprocess.on('SIGINT', shutdown)\nprocess.on('SIGTERM', shutdown)\n"],"mappings":";;;;AAKA,IAAI,QAAQ,KAAK,SAAS,QAAQ,KAAK,QAAQ,KAAK,SAAS,IAAI,GAAG;CAClE,QAAQ,IAAI,MAAM;;;;;;;;GAQjB;CACD,QAAQ,KAAK,CAAC;AAChB;AAEA,MAAM,QAAQ,MAAM,iBAAiB;AACrC,QAAQ,IAAI,oCAAoC,MAAM,KAAK;AAE3D,IAAI;AAEJ,SAAS,WAAW;CAClB,aAAa,MAAM,KAAK,CAAC,CAAC,WAClB,QAAQ,KAAK,CAAC,IACnB,UAAmB;EAClB,QAAQ,MAAM,+BAA+B,KAAK;EAClD,QAAQ,KAAK,CAAC;CAChB,CACF;AACF;AAEA,QAAQ,GAAG,UAAU,QAAQ;AAC7B,QAAQ,GAAG,WAAW,QAAQ"}
|
package/src/ai-relay/db/index.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { CamelCasePlugin, Kysely, type Dialect } from 'kysely'
|
|
2
|
-
|
|
3
|
-
import type { AIRelayDatabase } from './schema'
|
|
4
|
-
|
|
5
|
-
export { migrateAIRelayDatabase } from './migrate'
|
|
6
|
-
export type { AIRelayDatabase, AIRelayUsage, NewAIRelayUsage } from './schema'
|
|
7
|
-
|
|
8
|
-
export function createAIRelayDatabase(dialect: Dialect): Kysely<AIRelayDatabase> {
|
|
9
|
-
return new Kysely<AIRelayDatabase>({ dialect, plugins: [new CamelCasePlugin()] })
|
|
10
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { migrateToLatest } from '#cloud/server/db/migrate'
|
|
2
|
-
import type { Kysely } from 'kysely'
|
|
3
|
-
import { Migrator, type Migration, type MigrationProvider } from 'kysely/migration'
|
|
4
|
-
|
|
5
|
-
import * as usage from './migrations/001_usage'
|
|
6
|
-
import type { AIRelayDatabase } from './schema'
|
|
7
|
-
|
|
8
|
-
const migrations: Record<string, Migration> = {
|
|
9
|
-
'001_usage': usage
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
class AIRelayMigrationProvider implements MigrationProvider {
|
|
13
|
-
async getMigrations(): Promise<Record<string, Migration>> {
|
|
14
|
-
return migrations
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* The relay keeps its own migration history, so it can share a PostgreSQL database with Cloud
|
|
20
|
-
* without either one seeing the other's migrations as unknown.
|
|
21
|
-
*/
|
|
22
|
-
export async function migrateAIRelayDatabase(database: Kysely<AIRelayDatabase>): Promise<void> {
|
|
23
|
-
await migrateToLatest(
|
|
24
|
-
new Migrator({
|
|
25
|
-
db: database,
|
|
26
|
-
provider: new AIRelayMigrationProvider(),
|
|
27
|
-
migrationTableName: 'ai_relay_migration',
|
|
28
|
-
migrationLockTableName: 'ai_relay_migration_lock'
|
|
29
|
-
}),
|
|
30
|
-
'OpenPencil AI relay database migration failed'
|
|
31
|
-
)
|
|
32
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { TimestampColumn } from '#cloud/server/db/tables'
|
|
2
|
-
import type { Insertable, Selectable } from 'kysely'
|
|
3
|
-
|
|
4
|
-
export interface AIRelayUsageTable {
|
|
5
|
-
id: string
|
|
6
|
-
workspaceId: string
|
|
7
|
-
userId: string
|
|
8
|
-
modelId: string
|
|
9
|
-
inputTokens: number
|
|
10
|
-
outputTokens: number
|
|
11
|
-
credits: number
|
|
12
|
-
createdAt: TimestampColumn
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface AIRelayDatabase {
|
|
16
|
-
aiRelayUsage: AIRelayUsageTable
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export type AIRelayUsage = Selectable<AIRelayUsageTable>
|
|
20
|
-
export type NewAIRelayUsage = Insertable<AIRelayUsageTable>
|
package/src/ai-relay/index.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export { createAIRelayApp, type AIRelayApp, type AIRelayServices } from './app'
|
|
2
|
-
export {
|
|
3
|
-
AIRelayConfigError,
|
|
4
|
-
parseAIRelayConfig,
|
|
5
|
-
parseAIRelayTOML,
|
|
6
|
-
type AIRelayConfig,
|
|
7
|
-
type AIRelayConfigInput,
|
|
8
|
-
type AIRelayEnvironment
|
|
9
|
-
} from './config'
|
|
10
|
-
export {
|
|
11
|
-
createAIRelayDatabase,
|
|
12
|
-
migrateAIRelayDatabase,
|
|
13
|
-
type AIRelayDatabase,
|
|
14
|
-
type AIRelayUsage,
|
|
15
|
-
type NewAIRelayUsage
|
|
16
|
-
} from './db'
|
|
17
|
-
export { creditsFor, meteredUsage, type TokenUsage } from './metering'
|
|
18
|
-
export { createAIUsageStore, usagePeriod, type AIUsageStore } from './usage'
|
package/src/client/ai.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import type { CloudFetch } from '#cloud/client/discovery'
|
|
2
|
-
import {
|
|
3
|
-
aiCatalogSchema,
|
|
4
|
-
aiErrorSchema,
|
|
5
|
-
aiWalletSchema,
|
|
6
|
-
CLOUD_AI_API_PATHS,
|
|
7
|
-
type AICatalog,
|
|
8
|
-
type AIErrorCode,
|
|
9
|
-
type AIWallet
|
|
10
|
-
} from '#cloud/contract'
|
|
11
|
-
import * as v from 'valibot'
|
|
12
|
-
|
|
13
|
-
/** Where a request gets the token it sends, and how it drops one the service refused. */
|
|
14
|
-
export type CloudAITokenSource = {
|
|
15
|
-
token(): Promise<string>
|
|
16
|
-
/** Forgets `token`, so the next call signs a new one. */
|
|
17
|
-
invalidate(token: string): void
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export class CloudAIError extends Error {
|
|
21
|
-
override readonly name = 'CloudAIError'
|
|
22
|
-
|
|
23
|
-
constructor(
|
|
24
|
-
readonly status: number,
|
|
25
|
-
readonly code?: AIErrorCode
|
|
26
|
-
) {
|
|
27
|
-
super(`OpenPencil AI request failed with HTTP ${status}`)
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** Whether a request can be sent twice: a stream, in `init` or a `Request`, is read by the first. */
|
|
32
|
-
function replayable(input: RequestInfo | URL, body: RequestInit['body']): boolean {
|
|
33
|
-
if (input instanceof Request && input.body) return false
|
|
34
|
-
return !(body instanceof ReadableStream)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Sends each request with a current token. A token the service refuses, for instance one signed
|
|
39
|
-
* by a key Cloud has since replaced, is dropped and a request that can be replayed is sent once
|
|
40
|
-
* more with a new one.
|
|
41
|
-
*/
|
|
42
|
-
export function cloudAIFetch(tokens: CloudAITokenSource, fetch: CloudFetch): CloudFetch {
|
|
43
|
-
const send = async (input: RequestInfo | URL, init: RequestInit | undefined, token: string) => {
|
|
44
|
-
const headers = new Headers(init?.headers)
|
|
45
|
-
headers.set('Authorization', `Bearer ${token}`)
|
|
46
|
-
return fetch(input, { ...init, headers })
|
|
47
|
-
}
|
|
48
|
-
return async (input, init) => {
|
|
49
|
-
const token = await tokens.token()
|
|
50
|
-
const response = await send(input, init, token)
|
|
51
|
-
if (response.status !== 401) return response
|
|
52
|
-
tokens.invalidate(token)
|
|
53
|
-
if (!replayable(input, init?.body)) return response
|
|
54
|
-
return send(input, init, await tokens.token())
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const errorBodySchema = v.pipe(v.string(), v.parseJson(), aiErrorSchema)
|
|
59
|
-
|
|
60
|
-
async function parsed<TSchema extends v.GenericSchema>(
|
|
61
|
-
response: Response,
|
|
62
|
-
schema: TSchema
|
|
63
|
-
): Promise<v.InferOutput<TSchema>> {
|
|
64
|
-
const text = await response.text()
|
|
65
|
-
if (!response.ok) {
|
|
66
|
-
const error = v.safeParse(errorBodySchema, text)
|
|
67
|
-
throw new CloudAIError(response.status, error.success ? error.output.error.code : undefined)
|
|
68
|
-
}
|
|
69
|
-
return v.parse(v.pipe(v.string(), v.parseJson(), schema), text)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export type CloudAIClientOptions = {
|
|
73
|
-
/** The service's endpoint, as Cloud's discovery names it. */
|
|
74
|
-
endpoint: string
|
|
75
|
-
tokens: CloudAITokenSource
|
|
76
|
-
fetch: CloudFetch
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** The AI service a Cloud advertises: its model API base URL, catalog, and the wallet. */
|
|
80
|
-
export function createCloudAIClient(options: CloudAIClientOptions) {
|
|
81
|
-
const endpoint = options.endpoint.replace(/\/+$/, '')
|
|
82
|
-
const fetch = cloudAIFetch(options.tokens, options.fetch)
|
|
83
|
-
return {
|
|
84
|
-
/** The base URL an OpenAI-compatible client sends chat completions below. */
|
|
85
|
-
modelBaseURL: `${endpoint}${CLOUD_AI_API_PATHS.base}`,
|
|
86
|
-
fetch,
|
|
87
|
-
async catalog(): Promise<AICatalog> {
|
|
88
|
-
return parsed(await fetch(`${endpoint}${CLOUD_AI_API_PATHS.catalog}`), aiCatalogSchema)
|
|
89
|
-
},
|
|
90
|
-
async wallet(): Promise<AIWallet> {
|
|
91
|
-
return parsed(await fetch(`${endpoint}${CLOUD_AI_API_PATHS.wallet}`), aiWalletSchema)
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export type CloudAIClient = ReturnType<typeof createCloudAIClient>
|