@open-pencil/cloud 0.17.0-beta.4 → 0.17.0-beta.6
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 +2 -1
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/services.mjs +1 -1
- package/dist/bin/services.mjs.map +1 -1
- package/dist/client.d.ts +783 -47
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1651 -382
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +701 -45
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +2138 -34
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +4343 -306
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +61 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +1338 -44
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +45 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +958 -340
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +93 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +18032 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +12615 -5249
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/server.d.ts +17367 -8567
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +13587 -7333
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +11601 -5401
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +84 -62
- package/dist/server2.mjs.map +1 -1
- package/package.json +31 -5
- package/src/admin/api/routes.ts +263 -190
- package/src/admin/audit/service.ts +11 -3
- package/src/admin/bootstrap.ts +86 -0
- package/src/admin/documents/service.ts +195 -10
- package/src/admin/email/service.ts +2 -1
- package/src/admin/enrollment/email.ts +17 -3
- package/src/admin/enrollment/service.ts +71 -10
- package/src/admin/errors.ts +5 -0
- package/src/admin/index.ts +1 -0
- package/src/admin/reports/service.ts +15 -3
- package/src/admin/schema.ts +6 -5
- package/src/admin/users/service.ts +173 -16
- package/src/client/api.ts +52 -473
- package/src/client/auth/client.ts +22 -3
- package/src/client/comments.ts +205 -0
- package/src/client/community.ts +40 -0
- package/src/client/device-authorization.ts +4 -20
- package/src/client/discovery.ts +39 -11
- package/src/client/documents.ts +160 -0
- package/src/client/files/archive.ts +60 -0
- package/src/client/files/checksum.ts +7 -0
- package/src/client/files/download.ts +122 -0
- package/src/client/files/images.ts +89 -0
- package/src/client/files/index.ts +15 -0
- package/src/client/files/objects.ts +173 -0
- package/src/client/files/save.ts +79 -0
- package/src/client/index.ts +4 -0
- package/src/client/portal/account.ts +73 -3
- package/src/client/portal/administration.ts +52 -5
- package/src/client/portal/request.ts +6 -2
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +23 -18
- package/src/client/sharing.ts +262 -0
- package/src/client/tour.ts +44 -0
- package/src/client/versions.ts +110 -0
- package/src/contract/account.ts +111 -11
- package/src/contract/admin/index.ts +48 -3
- package/src/contract/billing.ts +235 -0
- package/src/contract/collaboration.ts +5 -0
- package/src/contract/comments.ts +223 -0
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +94 -5
- package/src/contract/entitlements.ts +10 -0
- package/src/contract/index.ts +64 -5
- package/src/contract/links.ts +62 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +3 -1
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +62 -20
- package/src/contract/workspaces.ts +5 -0
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +169 -0
- package/src/email/locales/de.json +169 -0
- package/src/email/locales/es.json +169 -0
- package/src/email/locales/fr.json +169 -0
- package/src/email/locales/it.json +169 -0
- package/src/email/locales/ja.json +169 -0
- package/src/email/locales/pl.json +169 -0
- package/src/email/locales/pt-br.json +169 -0
- package/src/email/locales/ru.json +169 -0
- package/src/email/locales/zh-cn.json +169 -0
- package/src/email/messages.ts +235 -0
- package/src/email/render.ts +160 -276
- package/src/email/samples.ts +142 -0
- package/src/email/schema.ts +98 -8
- package/src/email/templates/BrandEmail.vue +56 -0
- package/src/email/templates/CommentNotification.vue +39 -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/EnrollmentInvitation.vue +53 -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 +47 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +15 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/{server/errors → errors}/posthog.ts +28 -4
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/email.ts +2 -0
- package/src/runtime/cloudflare/relay/index.ts +28 -7
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +14 -1
- package/src/runtime/cloudflare/xml.ts +12 -0
- package/src/runtime/node/admin.ts +51 -47
- package/src/runtime/node/cleanup.ts +5 -1
- package/src/runtime/node/email/runtime.ts +1 -0
- package/src/runtime/node/email/transport.ts +14 -10
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/server.ts +20 -4
- package/src/runtime/node/services/main.ts +1 -1
- package/src/runtime/posthog/errors.ts +5 -2
- package/src/runtime/s3/objects.ts +17 -0
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +17 -1
- package/src/server/account/actions.ts +3 -1
- package/src/server/account/deletion.ts +40 -4
- package/src/server/account/routes.ts +305 -253
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +44 -6
- package/src/server/app/checks.ts +89 -0
- package/src/server/app/cors.ts +44 -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 +123 -0
- package/src/server/app/services.ts +219 -0
- package/src/server/auth/access-control.ts +13 -8
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/account.ts +13 -12
- package/src/server/auth/activity.ts +7 -2
- package/src/server/auth/adapter.ts +36 -3
- package/src/server/auth/email.ts +33 -18
- package/src/server/auth/endpoints.ts +4 -0
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +165 -189
- package/src/server/auth/index.ts +1 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/methods.ts +162 -0
- package/src/server/auth/mfa-assurance.ts +190 -2
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/rate-limit.ts +31 -0
- package/src/server/auth/runtime.ts +19 -5
- package/src/server/auth/session.ts +2 -0
- package/src/server/auth/social-providers.ts +70 -24
- package/src/server/billing/index.ts +8 -0
- package/src/server/billing/notify.ts +55 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/seats.ts +17 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +16 -3
- package/src/server/cleanup/documents.ts +5 -2
- package/src/server/cleanup/revisions.ts +6 -3
- package/src/server/cleanup/uploads.ts +2 -1
- package/src/server/cleanup/worker.ts +10 -1
- package/src/server/collaboration/relay/connection.ts +7 -2
- package/src/server/collaboration/relay/hub.ts +80 -166
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/routes.ts +8 -5
- package/src/server/collaboration/service.ts +21 -2
- package/src/server/comments/index.ts +9 -0
- package/src/server/comments/notify.ts +277 -0
- package/src/server/comments/routes.ts +352 -0
- package/src/server/comments/rows.ts +249 -0
- package/src/server/comments/service.ts +570 -0
- 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 +35 -10
- package/src/server/config/index.ts +2 -2
- package/src/server/config/public.ts +25 -6
- package/src/server/config/schema.ts +53 -293
- package/src/server/config/toml/schema.ts +330 -0
- package/src/server/db/index.ts +3 -1
- package/src/server/db/migrate.ts +33 -1
- 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/015_comments.ts +57 -0
- package/src/server/db/migrations/016_comment_notifications.ts +76 -0
- package/src/server/db/migrations/017_invitation_emails.ts +19 -0
- package/src/server/db/migrations/018_enrollment_source.ts +17 -0
- package/src/server/db/migrations/schema.ts +26 -1
- package/src/server/db/tables.ts +122 -3
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +17 -0
- package/src/server/documents/images.ts +6 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +7 -1
- package/src/server/documents/routes.ts +12 -12
- package/src/server/documents/service.ts +31 -407
- package/src/server/documents/summary.ts +15 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/routes.ts +7 -8
- package/src/server/documents/versions/service.ts +86 -26
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +39 -6
- package/src/server/email/types.ts +13 -0
- package/src/server/errors/index.ts +49 -12
- package/src/server/index.ts +27 -6
- package/src/server/internal/routes.ts +8 -2
- package/src/server/internal/service.ts +10 -0
- package/src/server/invitations/budget.ts +3 -1
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/links.ts +6 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +3 -1
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +13 -2
- package/src/server/policy/policy.ts +39 -12
- package/src/server/policy/service.ts +71 -52
- package/src/server/policy/storage.ts +33 -0
- package/src/server/policy/workspace.ts +5 -2
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/storage.ts +5 -0
- package/src/server/rate-limit/middleware.ts +4 -0
- package/src/server/reports/index.ts +1 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +5 -3
- package/src/server/reports/service.ts +27 -3
- package/src/server/services/routes.ts +2 -2
- package/src/server/sharing/capabilities/service.ts +3 -4
- package/src/server/sharing/document-routes.ts +3 -3
- package/src/server/sharing/invitations/service.ts +17 -8
- package/src/server/sharing/routes.ts +15 -15
- package/src/server/validation.ts +35 -24
- package/src/server/workspaces/members.ts +57 -31
- package/src/server/workspaces/repository.ts +23 -12
- package/src/server/workspaces/routes.ts +8 -8
- package/src/services/app.ts +31 -22
- package/src/services/config.ts +48 -8
- package/src/services/metering.ts +15 -27
- package/src/services/usage.ts +6 -14
- 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/app.ts +0 -453
package/src/services/app.ts
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
servicesTokenClaimsSchema,
|
|
4
4
|
CLOUD_SERVICES_API_PATHS,
|
|
5
5
|
CLOUD_SERVICES_TOKEN_AUDIENCE,
|
|
6
|
-
|
|
6
|
+
usageQuerySchema,
|
|
7
7
|
type ServiceError,
|
|
8
8
|
type ServiceErrorCode,
|
|
9
9
|
type ServicesTokenClaims
|
|
@@ -11,12 +11,15 @@ import {
|
|
|
11
11
|
import { validatedQuery } from '#cloud/server/validation'
|
|
12
12
|
import { pick } from 'es-toolkit'
|
|
13
13
|
import { Hono, type Context } from 'hono'
|
|
14
|
+
import { bearerAuth } from 'hono/bearer-auth'
|
|
14
15
|
import { bodyLimit } from 'hono/body-limit'
|
|
15
16
|
import { cors } from 'hono/cors'
|
|
16
17
|
import { jwtVerify, type JWTVerifyGetKey } from 'jose'
|
|
17
18
|
import type { Kysely } from 'kysely'
|
|
18
19
|
import * as v from 'valibot'
|
|
19
20
|
|
|
21
|
+
import { MINUTE_MS } from '@open-pencil/std/timing'
|
|
22
|
+
|
|
20
23
|
import type { ServicesConfig } from './config'
|
|
21
24
|
import type { ServicesDatabase } from './db'
|
|
22
25
|
import { creditsFor, meteredUsage } from './metering'
|
|
@@ -73,7 +76,7 @@ const FORWARDED_FIELDS = [
|
|
|
73
76
|
] as const
|
|
74
77
|
|
|
75
78
|
/** How long an upstream answer may take in all, streamed or not, so a stalled one cannot hold a request slot forever. */
|
|
76
|
-
const DEFAULT_UPSTREAM_TIMEOUT_MS = 10 *
|
|
79
|
+
const DEFAULT_UPSTREAM_TIMEOUT_MS = 10 * MINUTE_MS
|
|
77
80
|
|
|
78
81
|
function aiError(
|
|
79
82
|
context: Context,
|
|
@@ -111,6 +114,7 @@ function concurrencyLimiter(maximum: number) {
|
|
|
111
114
|
* OpenAI-compatible upstream with one key, and holds each workspace to its monthly allowance.
|
|
112
115
|
* A request is admitted while the workspace has credits left and charged once its usage is known,
|
|
113
116
|
* so concurrent requests can overdraw the allowance by what they cost.
|
|
117
|
+
* `/health` answers without a token, for uptime checks; it says nothing about the upstream.
|
|
114
118
|
*/
|
|
115
119
|
export function createServicesApp(services: ServicesAppDependencies) {
|
|
116
120
|
const { config } = services
|
|
@@ -124,22 +128,28 @@ export function createServicesApp(services: ServicesAppDependencies) {
|
|
|
124
128
|
services.onBackgroundError ??
|
|
125
129
|
((error: unknown) => console.error('[Services] Metering failed:', error))
|
|
126
130
|
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
131
|
+
const unauthorized = { error: { code: 'unauthorized' as const } }
|
|
132
|
+
const requireToken = bearerAuth<ServicesEnvironment>({
|
|
133
|
+
async verifyToken(token, context) {
|
|
134
|
+
try {
|
|
135
|
+
const { payload } = await jwtVerify(token, services.keys, {
|
|
136
|
+
issuer: config.cloudURL,
|
|
137
|
+
audience: CLOUD_SERVICES_TOKEN_AUDIENCE,
|
|
138
|
+
algorithms: ['ES256']
|
|
139
|
+
})
|
|
140
|
+
const claims = v.safeParse(servicesTokenClaimsSchema, payload)
|
|
141
|
+
if (!claims.success) return false
|
|
142
|
+
context.set('claims', claims.output)
|
|
143
|
+
return true
|
|
144
|
+
} catch {
|
|
145
|
+
return false
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
noAuthenticationHeader: { message: unauthorized },
|
|
149
|
+
// A header that is not a bearer token is malformed, not a wrong token.
|
|
150
|
+
invalidAuthenticationHeader: { message: { error: { code: 'invalid_request' as const } } },
|
|
151
|
+
invalidToken: { message: unauthorized }
|
|
152
|
+
})
|
|
143
153
|
|
|
144
154
|
return new Hono<ServicesEnvironment>()
|
|
145
155
|
.use(
|
|
@@ -151,13 +161,14 @@ export function createServicesApp(services: ServicesAppDependencies) {
|
|
|
151
161
|
maxAge: 600
|
|
152
162
|
})
|
|
153
163
|
)
|
|
164
|
+
.get('/health', (context) => context.json({ status: 'ok' as const }))
|
|
154
165
|
.use(`${CLOUD_SERVICES_API_PATHS.base}/*`, requireToken)
|
|
155
166
|
.get(CLOUD_SERVICES_API_PATHS.catalog, (context) => context.json(config.catalog))
|
|
156
167
|
.get(CLOUD_SERVICES_API_PATHS.wallet, async (context) => {
|
|
157
168
|
const claims = context.get('claims')
|
|
158
169
|
return context.json(await usage.wallet(spender(claims), claims.monthlyCreditAllowance, now()))
|
|
159
170
|
})
|
|
160
|
-
.get(CLOUD_SERVICES_API_PATHS.usage, validatedQuery(
|
|
171
|
+
.get(CLOUD_SERVICES_API_PATHS.usage, validatedQuery(usageQuerySchema), async (context) => {
|
|
161
172
|
try {
|
|
162
173
|
return context.json(
|
|
163
174
|
await usage.page(context.get('claims').workspaceId, context.req.valid('query').cursor)
|
|
@@ -232,9 +243,7 @@ export function createServicesApp(services: ServicesAppDependencies) {
|
|
|
232
243
|
await usage.record({
|
|
233
244
|
id: crypto.randomUUID(),
|
|
234
245
|
workspaceId: claims.workspaceId,
|
|
235
|
-
|
|
236
|
-
// workspace, and once Cloud upgrades, for that workspace's owner as well.
|
|
237
|
-
accountId: claims.accountId ?? null,
|
|
246
|
+
accountId: claims.accountId,
|
|
238
247
|
userId: claims.sub,
|
|
239
248
|
modelId: model.id,
|
|
240
249
|
inputTokens: tokens.inputTokens,
|
package/src/services/config.ts
CHANGED
|
@@ -67,6 +67,20 @@ const servicesConfigSchema = v.object({
|
|
|
67
67
|
}),
|
|
68
68
|
catalog: v.object({
|
|
69
69
|
default_model: textSchema,
|
|
70
|
+
/**
|
|
71
|
+
* The model for each of the editor's roles. Design left out is the default model; Review and
|
|
72
|
+
* Fast left out follow Design, and Vision left out does too when Design reads images.
|
|
73
|
+
*/
|
|
74
|
+
// Strict, so a misspelled role fails at start instead of quietly following Design.
|
|
75
|
+
roles: v.optional(
|
|
76
|
+
v.strictObject({
|
|
77
|
+
design: v.optional(textSchema),
|
|
78
|
+
review: v.optional(textSchema),
|
|
79
|
+
fast: v.optional(textSchema),
|
|
80
|
+
vision: v.optional(textSchema)
|
|
81
|
+
}),
|
|
82
|
+
{}
|
|
83
|
+
),
|
|
70
84
|
models: v.pipe(v.array(modelSchema), v.minLength(1))
|
|
71
85
|
})
|
|
72
86
|
})
|
|
@@ -88,16 +102,41 @@ function catalogFrom(input: v.InferOutput<typeof servicesConfigSchema>['catalog'
|
|
|
88
102
|
if (!ids.has(input.default_model)) {
|
|
89
103
|
throw new ServicesConfigError(`Default model ${input.default_model} is not in the catalog`)
|
|
90
104
|
}
|
|
105
|
+
const models = input.models.map((model) => ({
|
|
106
|
+
id: model.id,
|
|
107
|
+
label: model.label,
|
|
108
|
+
creditsPerMillionInputTokens: model.credits_per_million_input_tokens,
|
|
109
|
+
creditsPerMillionOutputTokens: model.credits_per_million_output_tokens,
|
|
110
|
+
contextTokens: model.context_tokens,
|
|
111
|
+
capabilities: { reasoning: model.reasoning, vision: model.vision, tools: model.tools }
|
|
112
|
+
}))
|
|
113
|
+
const byID = new Map(input.models.map((model) => [model.id, model]))
|
|
114
|
+
const role = (name: string, id: string, needs: 'tools' | 'vision' | null) => {
|
|
115
|
+
const model = byID.get(id)
|
|
116
|
+
if (!model) throw new ServicesConfigError(`The ${name} model ${id} is not in the catalog`)
|
|
117
|
+
if (needs && !model[needs]) {
|
|
118
|
+
throw new ServicesConfigError(`The ${name} model ${id} has no ${needs}`)
|
|
119
|
+
}
|
|
120
|
+
return id
|
|
121
|
+
}
|
|
122
|
+
// A catalog from before roles, whose default model can't run the chat, recommends nothing
|
|
123
|
+
// rather than refusing to start; roles named explicitly are always checked.
|
|
124
|
+
const namesRoles = Object.keys(input.roles).length > 0
|
|
125
|
+
if (!namesRoles && !byID.get(input.default_model)?.tools) {
|
|
126
|
+
return v.parse(serviceCatalogSchema, { defaultModelID: input.default_model, models })
|
|
127
|
+
}
|
|
128
|
+
const design = role('design', input.roles.design ?? input.default_model, 'tools')
|
|
129
|
+
// Vision falls back to the design model when it reads images, and is left out when none can.
|
|
130
|
+
const visionID = input.roles.vision ?? (byID.get(design)?.vision ? design : undefined) ?? null
|
|
91
131
|
return v.parse(serviceCatalogSchema, {
|
|
92
132
|
defaultModelID: input.default_model,
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}))
|
|
133
|
+
roleModels: {
|
|
134
|
+
design,
|
|
135
|
+
review: role('review', input.roles.review ?? design, null),
|
|
136
|
+
fast: role('fast', input.roles.fast ?? design, 'tools'),
|
|
137
|
+
vision: visionID === null ? null : role('vision', visionID, 'vision')
|
|
138
|
+
},
|
|
139
|
+
models
|
|
101
140
|
})
|
|
102
141
|
}
|
|
103
142
|
|
|
@@ -112,6 +151,7 @@ export function parseServicesConfig(
|
|
|
112
151
|
maximumConcurrentRequests: config.relay.max_concurrent_requests,
|
|
113
152
|
maximumRequestBytes: config.relay.max_request_bytes,
|
|
114
153
|
databaseURL: requiredSecret(environment, config.database.url.from_env),
|
|
154
|
+
// Every trailing slash: ufo's withoutTrailingSlash removes only one.
|
|
115
155
|
upstreamURL: config.upstream.base_url.replace(/\/+$/, ''),
|
|
116
156
|
upstreamKey: requiredSecret(environment, config.upstream.api_key.from_env),
|
|
117
157
|
catalog: catalogFrom(config.catalog)
|
package/src/services/metering.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ChatModel } from '#cloud/contract'
|
|
2
|
+
import { EventSourceParserStream } from 'eventsource-parser/stream'
|
|
2
3
|
import * as v from 'valibot'
|
|
3
4
|
|
|
4
5
|
export type TokenUsage = { inputTokens: number; outputTokens: number }
|
|
@@ -10,6 +11,12 @@ const usageSchema = v.object({
|
|
|
10
11
|
|
|
11
12
|
const usageBodySchema = v.pipe(v.string(), v.parseJson(), usageSchema)
|
|
12
13
|
|
|
14
|
+
/**
|
|
15
|
+
* The most an event may buffer before metering gives up, in characters. Stream events carry a
|
|
16
|
+
* small delta each, so only an upstream that never ends an event reaches it.
|
|
17
|
+
*/
|
|
18
|
+
const MAX_EVENT_CHARACTERS = 1024 * 1024
|
|
19
|
+
|
|
13
20
|
function usageOf(text: string): TokenUsage | null {
|
|
14
21
|
const parsed = v.safeParse(usageBodySchema, text)
|
|
15
22
|
if (!parsed.success) return null
|
|
@@ -33,36 +40,17 @@ export function creditsFor(model: ChatModel, usage: TokenUsage): number {
|
|
|
33
40
|
* event that carries one, which upstreams send when asked with `stream_options.include_usage`.
|
|
34
41
|
*/
|
|
35
42
|
export async function meteredUsage(
|
|
36
|
-
body: ReadableStream<Uint8Array
|
|
43
|
+
body: ReadableStream<Uint8Array<ArrayBuffer>>,
|
|
37
44
|
streamed: boolean
|
|
38
45
|
): Promise<TokenUsage | null> {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const chunks = async function* () {
|
|
42
|
-
for (let next = await reader.read(); !next.done; next = await reader.read()) {
|
|
43
|
-
yield decoder.decode(next.value, { stream: true })
|
|
44
|
-
}
|
|
45
|
-
yield decoder.decode()
|
|
46
|
-
}
|
|
47
|
-
if (!streamed) {
|
|
48
|
-
let text = ''
|
|
49
|
-
for await (const chunk of chunks()) text += chunk
|
|
50
|
-
return usageOf(text)
|
|
51
|
-
}
|
|
46
|
+
if (!streamed) return usageOf(await new Response(body).text())
|
|
47
|
+
// Server-sent events, as OpenAI-compatible APIs stream them; `[DONE]` ends the stream.
|
|
52
48
|
let usage: TokenUsage | null = null
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (data
|
|
58
|
-
usage = usageOf(data) ?? usage
|
|
59
|
-
}
|
|
60
|
-
for await (const chunk of chunks()) {
|
|
61
|
-
pending += chunk
|
|
62
|
-
const parts = pending.split(/\r?\n/)
|
|
63
|
-
pending = parts.pop() ?? ''
|
|
64
|
-
for (const line of parts) read(line)
|
|
49
|
+
const events = body
|
|
50
|
+
.pipeThrough(new TextDecoderStream())
|
|
51
|
+
.pipeThrough(new EventSourceParserStream({ maxBufferSize: MAX_EVENT_CHARACTERS }))
|
|
52
|
+
for await (const event of events) {
|
|
53
|
+
if (event.data !== '[DONE]') usage = usageOf(event.data) ?? usage
|
|
65
54
|
}
|
|
66
|
-
read(pending)
|
|
67
55
|
return usage
|
|
68
56
|
}
|
package/src/services/usage.ts
CHANGED
|
@@ -38,29 +38,21 @@ export class ServicesUsageCursorError extends Error {
|
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
/**
|
|
42
|
-
* Who a request spends for: the account whose allowance it draws on, and the workspace it came
|
|
43
|
-
* from, whose requests recorded before accounts carry no account and still count this month.
|
|
44
|
-
*/
|
|
41
|
+
/** Who a request spends for: the account whose allowance it draws on, and its workspace. */
|
|
45
42
|
export type CreditSpender = { accountId: string; workspaceId: string }
|
|
46
43
|
|
|
47
|
-
/** The spender a token names
|
|
48
|
-
export function spender(claims:
|
|
49
|
-
return { accountId: claims.accountId
|
|
44
|
+
/** The spender a token names. */
|
|
45
|
+
export function spender(claims: CreditSpender): CreditSpender {
|
|
46
|
+
return { accountId: claims.accountId, workspaceId: claims.workspaceId }
|
|
50
47
|
}
|
|
51
48
|
|
|
52
49
|
export function createServicesUsageStore(database: Kysely<ServicesDatabase>) {
|
|
53
|
-
async function spent({ accountId
|
|
50
|
+
async function spent({ accountId }: CreditSpender, now: Date): Promise<number> {
|
|
54
51
|
const { start, end } = usagePeriod(now)
|
|
55
52
|
const row = await database
|
|
56
53
|
.selectFrom('servicesUsage')
|
|
57
54
|
.select((query) => query.fn.sum<string | number | null>('credits').as('credits'))
|
|
58
|
-
.where(
|
|
59
|
-
usage.or([
|
|
60
|
-
usage('accountId', '=', accountId),
|
|
61
|
-
usage.and([usage('accountId', 'is', null), usage('workspaceId', '=', workspaceId)])
|
|
62
|
-
])
|
|
63
|
-
)
|
|
55
|
+
.where('accountId', '=', accountId)
|
|
64
56
|
.where('createdAt', '>=', start)
|
|
65
57
|
.where('createdAt', '<', end)
|
|
66
58
|
.executeTakeFirst()
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { defineComponent, type PropType } from 'vue'
|
|
2
|
-
|
|
3
|
-
import { transactionalEmailLayout } from './layout'
|
|
4
|
-
|
|
5
|
-
export type DocumentInvitationEmailProps = {
|
|
6
|
-
inviterName: string
|
|
7
|
-
documentName: string
|
|
8
|
-
permissionLabel: string
|
|
9
|
-
expiresAt: string
|
|
10
|
-
acceptanceURL: string
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const DocumentInvitationEmail = defineComponent({
|
|
14
|
-
props: {
|
|
15
|
-
inviterName: { type: String, required: true },
|
|
16
|
-
documentName: { type: String, required: true },
|
|
17
|
-
permissionLabel: { type: String, required: true },
|
|
18
|
-
expiresAt: { type: String, required: true },
|
|
19
|
-
acceptanceURL: { type: String, required: true }
|
|
20
|
-
} satisfies Record<
|
|
21
|
-
keyof DocumentInvitationEmailProps,
|
|
22
|
-
{ type: PropType<string>; required: true }
|
|
23
|
-
>,
|
|
24
|
-
setup(props) {
|
|
25
|
-
return () =>
|
|
26
|
-
transactionalEmailLayout({
|
|
27
|
-
preview: `${props.inviterName} invited you to ${props.permissionLabel} a document`,
|
|
28
|
-
heading: 'OpenPencil invitation',
|
|
29
|
-
message: `${props.inviterName} invited you to ${props.permissionLabel} “${props.documentName}”.`,
|
|
30
|
-
actionLabel: 'Open document',
|
|
31
|
-
actionURL: props.acceptanceURL,
|
|
32
|
-
footer: `This invitation expires ${props.expiresAt}.`
|
|
33
|
-
})
|
|
34
|
-
}
|
|
35
|
-
})
|
|
@@ -1,27 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,18 +0,0 @@
|
|
|
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
|
-
}
|