@open-pencil/cloud 0.17.0-beta.2 → 0.17.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +8 -8
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +236 -46
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +649 -400
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +680 -443
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +9 -1
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +99 -58
- package/dist/email.js.map +1 -1
- package/dist/index.d.ts +469 -65
- package/dist/index.d.ts.map +1 -1
- package/dist/listen.mjs +505 -787
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +3920 -2524
- 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 +3586 -1897
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +5216 -4272
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +5179 -3623
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +7 -6
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +25 -6
- package/src/admin/documents/service.ts +361 -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 +147 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +36 -21
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/index.ts +7 -7
- package/src/client/portal/account.ts +25 -0
- package/src/client/portal/administration.ts +56 -0
- package/src/client/{ai.ts → services.ts} +44 -27
- package/src/contract/account.ts +58 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/collaboration.ts +6 -1
- package/src/contract/discovery.ts +22 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +2 -2
- package/src/contract/index.ts +63 -37
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/services.ts +233 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/render.ts +26 -0
- package/src/email/schema.ts +11 -1
- package/src/email/templates/brand.ts +1 -1
- 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 +62 -14
- package/src/runtime/cloudflare/worker.ts +39 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +16 -3
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.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 +39 -15
- package/src/runtime/node/{ai-relay → services}/main.ts +6 -6
- 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 -31
- package/src/runtime/s3/objects.ts +24 -1
- package/src/runtime/site/server.ts +32 -5
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +302 -0
- package/src/server/account/routes.ts +47 -3
- package/src/server/account/terms.ts +42 -0
- package/src/server/api/router.ts +9 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app.ts +90 -16
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/adapter.ts +11 -0
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/factory.ts +30 -3
- package/src/server/auth/index.ts +10 -1
- package/src/server/cleanup/configured.ts +12 -5
- 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 +5 -2
- 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 +97 -25
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/service.ts +27 -31
- package/src/server/config/deployment.ts +24 -4
- package/src/server/config/public.ts +5 -4
- package/src/server/config/schema.ts +82 -16
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/migrate.ts +34 -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/schema.ts +71 -0
- package/src/server/db/tables.ts +52 -3
- 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/limits.ts +9 -9
- 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 +18 -4
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -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 +13 -4
- package/src/server/errors/index.ts +59 -16
- package/src/server/errors/posthog.ts +80 -0
- package/src/server/index.ts +46 -17
- 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/logging.ts +76 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/database.ts +39 -25
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +1 -0
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +12 -9
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +69 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +2 -2
- package/src/server/reports/index.ts +8 -0
- package/src/server/reports/routes.ts +37 -0
- package/src/server/reports/service.ts +123 -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 -22
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +21 -11
- 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 +1 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +89 -38
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +48 -44
- package/src/{ai-relay → services}/config.ts +25 -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 +43 -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/contract/ai.ts +0 -150
- 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/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,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
type
|
|
8
|
-
type
|
|
9
|
-
type
|
|
2
|
+
SERVICE_ERROR_STATUS,
|
|
3
|
+
servicesTokenClaimsSchema,
|
|
4
|
+
CLOUD_SERVICES_API_PATHS,
|
|
5
|
+
CLOUD_SERVICES_TOKEN_AUDIENCE,
|
|
6
|
+
parseUsageQuery,
|
|
7
|
+
type ServiceError,
|
|
8
|
+
type ServiceErrorCode,
|
|
9
|
+
type ServicesTokenClaims
|
|
10
10
|
} from '#cloud/contract'
|
|
11
11
|
import { validatedQuery } from '#cloud/server/validation'
|
|
12
12
|
import { pick } from 'es-toolkit'
|
|
@@ -17,14 +17,14 @@ import { jwtVerify, type JWTVerifyGetKey } from 'jose'
|
|
|
17
17
|
import type { Kysely } from 'kysely'
|
|
18
18
|
import * as v from 'valibot'
|
|
19
19
|
|
|
20
|
-
import type {
|
|
21
|
-
import type {
|
|
20
|
+
import type { ServicesConfig } from './config'
|
|
21
|
+
import type { ServicesDatabase } from './db'
|
|
22
22
|
import { creditsFor, meteredUsage } from './metering'
|
|
23
|
-
import {
|
|
23
|
+
import { ServicesUsageCursorError, createServicesUsageStore, spender } from './usage'
|
|
24
24
|
|
|
25
|
-
export type
|
|
26
|
-
config:
|
|
27
|
-
database: Kysely<
|
|
25
|
+
export type ServicesAppDependencies = {
|
|
26
|
+
config: ServicesConfig
|
|
27
|
+
database: Kysely<ServicesDatabase>
|
|
28
28
|
/** Resolves Cloud's signing keys, normally `createRemoteJWKSet` on Cloud's JWKS. */
|
|
29
29
|
keys: JWTVerifyGetKey
|
|
30
30
|
/** Reaches the upstream model API. */
|
|
@@ -39,7 +39,7 @@ export type AIRelayServices = {
|
|
|
39
39
|
upstreamTimeoutMs?: number
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
type
|
|
42
|
+
type ServicesEnvironment = { Variables: { claims: ServicesTokenClaims } }
|
|
43
43
|
|
|
44
44
|
const chatRequestSchema = v.looseObject({
|
|
45
45
|
model: v.pipe(v.string(), v.minLength(1)),
|
|
@@ -77,27 +77,30 @@ const DEFAULT_UPSTREAM_TIMEOUT_MS = 10 * 60_000
|
|
|
77
77
|
|
|
78
78
|
function aiError(
|
|
79
79
|
context: Context,
|
|
80
|
-
code:
|
|
81
|
-
details: Omit<
|
|
80
|
+
code: ServiceErrorCode,
|
|
81
|
+
details: Omit<ServiceError['error'], 'code'> = {}
|
|
82
82
|
) {
|
|
83
|
-
return context.json({ error: { code, ...details } },
|
|
83
|
+
return context.json({ error: { code, ...details } }, SERVICE_ERROR_STATUS[code])
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
/**
|
|
86
|
+
/**
|
|
87
|
+
* Counts each account's model requests in flight, within one process: its workspaces spend the
|
|
88
|
+
* same credits, so they share one limit.
|
|
89
|
+
*/
|
|
87
90
|
function concurrencyLimiter(maximum: number) {
|
|
88
91
|
const inFlight = new Map<string, number>()
|
|
89
92
|
return {
|
|
90
|
-
acquire(
|
|
91
|
-
const count = inFlight.get(
|
|
93
|
+
acquire(accountId: string): (() => void) | null {
|
|
94
|
+
const count = inFlight.get(accountId) ?? 0
|
|
92
95
|
if (count >= maximum) return null
|
|
93
|
-
inFlight.set(
|
|
96
|
+
inFlight.set(accountId, count + 1)
|
|
94
97
|
let released = false
|
|
95
98
|
return () => {
|
|
96
99
|
if (released) return
|
|
97
100
|
released = true
|
|
98
|
-
const remaining = (inFlight.get(
|
|
99
|
-
if (remaining > 0) inFlight.set(
|
|
100
|
-
else inFlight.delete(
|
|
101
|
+
const remaining = (inFlight.get(accountId) ?? 1) - 1
|
|
102
|
+
if (remaining > 0) inFlight.set(accountId, remaining)
|
|
103
|
+
else inFlight.delete(accountId)
|
|
101
104
|
}
|
|
102
105
|
}
|
|
103
106
|
}
|
|
@@ -109,36 +112,36 @@ function concurrencyLimiter(maximum: number) {
|
|
|
109
112
|
* A request is admitted while the workspace has credits left and charged once its usage is known,
|
|
110
113
|
* so concurrent requests can overdraw the allowance by what they cost.
|
|
111
114
|
*/
|
|
112
|
-
export function
|
|
115
|
+
export function createServicesApp(services: ServicesAppDependencies) {
|
|
113
116
|
const { config } = services
|
|
114
117
|
const upstreamFetch = services.fetch ?? ((input, init) => fetch(input, init))
|
|
115
118
|
const now = services.now ?? (() => new Date())
|
|
116
|
-
const usage =
|
|
119
|
+
const usage = createServicesUsageStore(services.database)
|
|
117
120
|
const models = new Map(config.catalog.models.map((model) => [model.id, model]))
|
|
118
121
|
const limiter = concurrencyLimiter(config.maximumConcurrentRequests)
|
|
119
122
|
const allowedOrigins = new Set(config.allowedOrigins)
|
|
120
123
|
const reportBackgroundError =
|
|
121
124
|
services.onBackgroundError ??
|
|
122
|
-
((error: unknown) => console.error('[
|
|
125
|
+
((error: unknown) => console.error('[Services] Metering failed:', error))
|
|
123
126
|
|
|
124
|
-
const requireToken = async (context: Context<
|
|
127
|
+
const requireToken = async (context: Context<ServicesEnvironment>, next: () => Promise<void>) => {
|
|
125
128
|
const header = context.req.header('Authorization')
|
|
126
129
|
const token = header?.startsWith('Bearer ') ? header.slice('Bearer '.length) : undefined
|
|
127
130
|
if (!token) return aiError(context, 'unauthorized')
|
|
128
131
|
try {
|
|
129
132
|
const { payload } = await jwtVerify(token, services.keys, {
|
|
130
133
|
issuer: config.cloudURL,
|
|
131
|
-
audience:
|
|
134
|
+
audience: CLOUD_SERVICES_TOKEN_AUDIENCE,
|
|
132
135
|
algorithms: ['ES256']
|
|
133
136
|
})
|
|
134
|
-
context.set('claims', v.parse(
|
|
137
|
+
context.set('claims', v.parse(servicesTokenClaimsSchema, payload))
|
|
135
138
|
} catch {
|
|
136
139
|
return aiError(context, 'unauthorized')
|
|
137
140
|
}
|
|
138
141
|
return next()
|
|
139
142
|
}
|
|
140
143
|
|
|
141
|
-
return new Hono<
|
|
144
|
+
return new Hono<ServicesEnvironment>()
|
|
142
145
|
.use(
|
|
143
146
|
'*',
|
|
144
147
|
cors({
|
|
@@ -148,28 +151,26 @@ export function createAIRelayApp(services: AIRelayServices) {
|
|
|
148
151
|
maxAge: 600
|
|
149
152
|
})
|
|
150
153
|
)
|
|
151
|
-
.use(`${
|
|
152
|
-
.get(
|
|
153
|
-
.get(
|
|
154
|
+
.use(`${CLOUD_SERVICES_API_PATHS.base}/*`, requireToken)
|
|
155
|
+
.get(CLOUD_SERVICES_API_PATHS.catalog, (context) => context.json(config.catalog))
|
|
156
|
+
.get(CLOUD_SERVICES_API_PATHS.wallet, async (context) => {
|
|
154
157
|
const claims = context.get('claims')
|
|
155
|
-
return context.json(
|
|
156
|
-
await usage.wallet(claims.workspaceId, claims.monthlyCreditAllowance, now())
|
|
157
|
-
)
|
|
158
|
+
return context.json(await usage.wallet(spender(claims), claims.monthlyCreditAllowance, now()))
|
|
158
159
|
})
|
|
159
|
-
.get(
|
|
160
|
+
.get(CLOUD_SERVICES_API_PATHS.usage, validatedQuery(parseUsageQuery), async (context) => {
|
|
160
161
|
try {
|
|
161
162
|
return context.json(
|
|
162
163
|
await usage.page(context.get('claims').workspaceId, context.req.valid('query').cursor)
|
|
163
164
|
)
|
|
164
165
|
} catch (error) {
|
|
165
|
-
if (error instanceof
|
|
166
|
+
if (error instanceof ServicesUsageCursorError) {
|
|
166
167
|
return context.json({ error: { code: 'invalid_request' as const } }, 400)
|
|
167
168
|
}
|
|
168
169
|
throw error
|
|
169
170
|
}
|
|
170
171
|
})
|
|
171
172
|
.post(
|
|
172
|
-
|
|
173
|
+
CLOUD_SERVICES_API_PATHS.chatCompletions,
|
|
173
174
|
// Counted as it streams in, so a body without Content-Length is bounded too.
|
|
174
175
|
bodyLimit({
|
|
175
176
|
maxSize: config.maximumRequestBytes,
|
|
@@ -183,13 +184,13 @@ export function createAIRelayApp(services: AIRelayServices) {
|
|
|
183
184
|
const request = pick(parsed.output, FORWARDED_FIELDS)
|
|
184
185
|
const model = models.get(request.model)
|
|
185
186
|
if (!model) return aiError(context, 'model_not_allowed')
|
|
186
|
-
const wallet = await usage.wallet(claims
|
|
187
|
+
const wallet = await usage.wallet(spender(claims), claims.monthlyCreditAllowance, now())
|
|
187
188
|
if (wallet.balance === 0) {
|
|
188
189
|
return aiError(context, 'insufficient_credits', {
|
|
189
190
|
resetsAt: wallet.buckets[0]?.expiresAt ?? undefined
|
|
190
191
|
})
|
|
191
192
|
}
|
|
192
|
-
const release = limiter.acquire(claims.
|
|
193
|
+
const release = limiter.acquire(spender(claims).accountId)
|
|
193
194
|
if (!release) return aiError(context, 'rate_limited')
|
|
194
195
|
|
|
195
196
|
const streamed = request.stream === true
|
|
@@ -231,6 +232,9 @@ export function createAIRelayApp(services: AIRelayServices) {
|
|
|
231
232
|
await usage.record({
|
|
232
233
|
id: crypto.randomUUID(),
|
|
233
234
|
workspaceId: claims.workspaceId,
|
|
235
|
+
// A token from a Cloud without accounts names none; the row then counts for its
|
|
236
|
+
// workspace, and once Cloud upgrades, for that workspace's owner as well.
|
|
237
|
+
accountId: claims.accountId ?? null,
|
|
234
238
|
userId: claims.sub,
|
|
235
239
|
modelId: model.id,
|
|
236
240
|
inputTokens: tokens.inputTokens,
|
|
@@ -248,4 +252,4 @@ export function createAIRelayApp(services: AIRelayServices) {
|
|
|
248
252
|
)
|
|
249
253
|
}
|
|
250
254
|
|
|
251
|
-
export type
|
|
255
|
+
export type ServicesApp = ReturnType<typeof createServicesApp>
|
|
@@ -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,8 +101,11 @@ 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),
|
|
@@ -115,6 +118,9 @@ export function parseAIRelayConfig(input: unknown, environment: AIRelayEnvironme
|
|
|
115
118
|
}
|
|
116
119
|
}
|
|
117
120
|
|
|
118
|
-
export function
|
|
119
|
-
|
|
121
|
+
export function parseServicesTOML(
|
|
122
|
+
source: string,
|
|
123
|
+
environment: ServicesEnvironment
|
|
124
|
+
): ServicesConfig {
|
|
125
|
+
return parseServicesConfig(parseTOML(source), environment)
|
|
120
126
|
}
|
|
@@ -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,36 @@ 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
|
+
/**
|
|
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
|
+
*/
|
|
45
|
+
export type CreditSpender = { accountId: string; workspaceId: string }
|
|
46
|
+
|
|
47
|
+
/** The spender a token names; a token without an account spends its workspace's allowance. */
|
|
48
|
+
export function spender(claims: { accountId?: string; workspaceId: string }): CreditSpender {
|
|
49
|
+
return { accountId: claims.accountId ?? claims.workspaceId, workspaceId: claims.workspaceId }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function createServicesUsageStore(database: Kysely<ServicesDatabase>) {
|
|
53
|
+
async function spent({ accountId, workspaceId }: CreditSpender, now: Date): Promise<number> {
|
|
43
54
|
const { start, end } = usagePeriod(now)
|
|
44
55
|
const row = await database
|
|
45
|
-
.selectFrom('
|
|
56
|
+
.selectFrom('servicesUsage')
|
|
46
57
|
.select((query) => query.fn.sum<string | number | null>('credits').as('credits'))
|
|
47
|
-
.where(
|
|
58
|
+
.where((usage) =>
|
|
59
|
+
usage.or([
|
|
60
|
+
usage('accountId', '=', accountId),
|
|
61
|
+
usage.and([usage('accountId', 'is', null), usage('workspaceId', '=', workspaceId)])
|
|
62
|
+
])
|
|
63
|
+
)
|
|
48
64
|
.where('createdAt', '>=', start)
|
|
49
65
|
.where('createdAt', '<', end)
|
|
50
66
|
.executeTakeFirst()
|
|
@@ -52,33 +68,41 @@ export function createAIUsageStore(database: Kysely<AIRelayDatabase>) {
|
|
|
52
68
|
}
|
|
53
69
|
|
|
54
70
|
return {
|
|
55
|
-
async record(usage:
|
|
56
|
-
await database.insertInto('
|
|
71
|
+
async record(usage: NewServicesUsage): Promise<void> {
|
|
72
|
+
await database.insertInto('servicesUsage').values(usage).execute()
|
|
57
73
|
},
|
|
58
74
|
|
|
59
75
|
/**
|
|
60
|
-
* The
|
|
61
|
-
* Cloud set none, and the relay does not limit spend.
|
|
76
|
+
* The account's single allowance bucket for the current month, which every workspace it owns
|
|
77
|
+
* spends from. A `null` allowance means Cloud set none, and the relay does not limit spend.
|
|
62
78
|
*/
|
|
63
|
-
async wallet(
|
|
79
|
+
async wallet(
|
|
80
|
+
spending: CreditSpender,
|
|
81
|
+
allowance: number | null,
|
|
82
|
+
now: Date
|
|
83
|
+
): Promise<CreditWallet> {
|
|
64
84
|
if (allowance === null) return { balance: null, buckets: [] }
|
|
65
|
-
const remaining = Math.max(0, allowance - (await spent(
|
|
85
|
+
const remaining = Math.max(0, allowance - (await spent(spending, now)))
|
|
66
86
|
return {
|
|
67
87
|
balance: remaining,
|
|
68
88
|
buckets: [{ kind: 'allowance', remaining, expiresAt: usagePeriod(now).end.toISOString() }]
|
|
69
89
|
}
|
|
70
90
|
},
|
|
71
91
|
|
|
72
|
-
/**
|
|
73
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Newest first, across every member of the workspace. Only this workspace's requests are
|
|
94
|
+
* listed, though its owner's other workspaces spend the same credits: their members and
|
|
95
|
+
* models are theirs to see.
|
|
96
|
+
*/
|
|
97
|
+
async page(workspaceId: string, cursor: string | undefined): Promise<UsagePage> {
|
|
74
98
|
let after: { createdAt: Date; id: string } | undefined
|
|
75
99
|
if (cursor !== undefined) {
|
|
76
100
|
const parsed = v.safeParse(cursorSchema, cursor)
|
|
77
|
-
if (!parsed.success) throw new
|
|
101
|
+
if (!parsed.success) throw new ServicesUsageCursorError()
|
|
78
102
|
after = parsed.output
|
|
79
103
|
}
|
|
80
104
|
let query = database
|
|
81
|
-
.selectFrom('
|
|
105
|
+
.selectFrom('servicesUsage')
|
|
82
106
|
.select(['id', 'createdAt', 'modelId', 'userId', 'credits'])
|
|
83
107
|
.where('workspaceId', '=', workspaceId)
|
|
84
108
|
if (after) {
|
|
@@ -112,4 +136,4 @@ export function createAIUsageStore(database: Kysely<AIRelayDatabase>) {
|
|
|
112
136
|
}
|
|
113
137
|
}
|
|
114
138
|
|
|
115
|
-
export type
|
|
139
|
+
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>
|