@open-pencil/cloud 0.17.0-beta.1 → 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 +468 -64
- package/dist/index.d.ts.map +1 -1
- package/dist/listen.mjs +575 -738
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +6564 -2934
- 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 +8931 -4198
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +5260 -3859
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +5686 -3736
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +9 -6
- package/src/admin/api/routes.ts +103 -33
- 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 +31 -6
- 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 +176 -0
- package/src/runtime/cloudflare/relay/sockets.ts +112 -0
- package/src/runtime/cloudflare/worker.ts +89 -26
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +20 -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 +71 -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 +32 -0
- package/src/runtime/s3/objects.ts +50 -1
- package/src/runtime/site/server.ts +33 -4
- 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 +12 -7
- package/src/server/api/types.ts +9 -0
- package/src/server/app.ts +93 -15
- 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/objects.ts +10 -2
- package/src/server/cleanup/revisions.ts +24 -7
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +5 -2
- package/src/server/collaboration/index.ts +3 -0
- package/src/server/collaboration/relay/connection.ts +196 -0
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +279 -195
- package/src/server/collaboration/relay/index.ts +3 -1
- package/src/server/collaboration/relay/log.ts +79 -0
- package/src/server/collaboration/relay/persistence.ts +147 -22
- package/src/server/collaboration/relay/resume.ts +28 -0
- package/src/server/collaboration/routes.ts +27 -16
- package/src/server/collaboration/service.ts +27 -31
- package/src/server/config/deployment.ts +35 -4
- package/src/server/config/public.ts +5 -4
- package/src/server/config/schema.ts +104 -16
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/migrate.ts +34 -114
- 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 +70 -7
- 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 +132 -80
- 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/routes.ts +36 -25
- package/src/server/documents/versions/service.ts +22 -4
- package/src/server/errors/index.ts +99 -0
- package/src/server/errors/posthog.ts +80 -0
- package/src/server/index.ts +56 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -15
- 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/objects/types.ts +12 -0
- package/src/server/pictures/routes.ts +4 -2
- 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/index.ts +4 -1
- package/src/server/rate-limit/middleware.ts +100 -12
- 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/document-routes.ts +30 -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 +108 -85
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +59 -2
- 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 +67 -45
- 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/runtime/cloudflare/relay.ts +0 -136
- 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/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,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>
|
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/contract/ai.ts
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import * as v from 'valibot'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The contract between editors, Cloud, and an AI service. Cloud only advertises the service and
|
|
5
|
-
* signs short-lived tokens for it; the service, official or self-hosted, proxies models and keeps
|
|
6
|
-
* the credit wallet. It is versioned apart from the Cloud protocol so a service can change without
|
|
7
|
-
* a Cloud release.
|
|
8
|
-
*/
|
|
9
|
-
export const CLOUD_AI_CONTRACT_VERSION = '1' as const
|
|
10
|
-
/** The `aud` of every token Cloud signs for an AI service. */
|
|
11
|
-
export const CLOUD_AI_TOKEN_AUDIENCE = 'openpencil-ai' as const
|
|
12
|
-
/** Where Cloud publishes the public keys an AI service verifies its tokens with. */
|
|
13
|
-
export const CLOUD_AI_JWKS_PATH = '/.well-known/jwks.json' as const
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Where a service answers, below the discovered `endpoint`. Model requests follow the OpenAI chat
|
|
17
|
-
* completions API, so an OpenAI-compatible client takes `${endpoint}${base}` as its base URL.
|
|
18
|
-
*/
|
|
19
|
-
export const CLOUD_AI_API_PATHS = {
|
|
20
|
-
base: '/v1',
|
|
21
|
-
chatCompletions: '/v1/chat/completions',
|
|
22
|
-
catalog: '/v1/catalog',
|
|
23
|
-
wallet: '/v1/wallet',
|
|
24
|
-
usage: '/v1/usage'
|
|
25
|
-
} as const
|
|
26
|
-
|
|
27
|
-
const urlSchema = v.pipe(v.string(), v.url())
|
|
28
|
-
const creditsSchema = v.pipe(v.number(), v.integer(), v.minValue(0))
|
|
29
|
-
|
|
30
|
-
export const cloudAIDiscoverySchema = v.object({
|
|
31
|
-
endpoint: urlSchema,
|
|
32
|
-
contractVersion: v.literal(CLOUD_AI_CONTRACT_VERSION)
|
|
33
|
-
})
|
|
34
|
-
export type CloudAIDiscovery = v.InferOutput<typeof cloudAIDiscoverySchema>
|
|
35
|
-
|
|
36
|
-
export const cloudBillingDiscoverySchema = v.object({ portalURL: urlSchema })
|
|
37
|
-
export type CloudBillingDiscovery = v.InferOutput<typeof cloudBillingDiscoverySchema>
|
|
38
|
-
|
|
39
|
-
export const createAITokenSchema = v.object({ workspaceId: v.pipe(v.string(), v.uuid()) })
|
|
40
|
-
export type CreateAITokenInput = v.InferOutput<typeof createAITokenSchema>
|
|
41
|
-
|
|
42
|
-
export function parseCreateAIToken(input: unknown): CreateAITokenInput {
|
|
43
|
-
return v.parse(createAITokenSchema, input)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export const aiTokenSchema = v.object({
|
|
47
|
-
token: v.pipe(v.string(), v.minLength(1)),
|
|
48
|
-
expiresAt: v.pipe(v.string(), v.isoTimestamp())
|
|
49
|
-
})
|
|
50
|
-
export type AIToken = v.InferOutput<typeof aiTokenSchema>
|
|
51
|
-
|
|
52
|
-
/** What an AI service may rely on once a token's signature, issuer, audience, and expiry check. */
|
|
53
|
-
export const aiTokenClaimsSchema = v.object({
|
|
54
|
-
iss: urlSchema,
|
|
55
|
-
aud: v.literal(CLOUD_AI_TOKEN_AUDIENCE),
|
|
56
|
-
sub: v.pipe(v.string(), v.minLength(1)),
|
|
57
|
-
workspaceId: v.pipe(v.string(), v.uuid()),
|
|
58
|
-
/** Credits the workspace's plan grants each month; `null` when Cloud sets no allowance. */
|
|
59
|
-
monthlyCreditAllowance: v.nullable(creditsSchema),
|
|
60
|
-
iat: v.pipe(v.number(), v.integer()),
|
|
61
|
-
exp: v.pipe(v.number(), v.integer())
|
|
62
|
-
})
|
|
63
|
-
export type AITokenClaims = v.InferOutput<typeof aiTokenClaimsSchema>
|
|
64
|
-
|
|
65
|
-
export const aiModelSchema = v.object({
|
|
66
|
-
id: v.pipe(v.string(), v.minLength(1)),
|
|
67
|
-
label: v.pipe(v.string(), v.minLength(1)),
|
|
68
|
-
/** Credits per million input and output tokens. */
|
|
69
|
-
creditsPerMillionInputTokens: creditsSchema,
|
|
70
|
-
creditsPerMillionOutputTokens: creditsSchema,
|
|
71
|
-
contextTokens: v.pipe(v.number(), v.integer(), v.minValue(1)),
|
|
72
|
-
capabilities: v.object({ reasoning: v.boolean(), vision: v.boolean(), tools: v.boolean() })
|
|
73
|
-
})
|
|
74
|
-
export type AIModel = v.InferOutput<typeof aiModelSchema>
|
|
75
|
-
|
|
76
|
-
export const aiCatalogSchema = v.object({
|
|
77
|
-
models: v.array(aiModelSchema),
|
|
78
|
-
defaultModelID: v.pipe(v.string(), v.minLength(1))
|
|
79
|
-
})
|
|
80
|
-
export type AICatalog = v.InferOutput<typeof aiCatalogSchema>
|
|
81
|
-
|
|
82
|
-
export const aiCreditBucketSchema = v.object({
|
|
83
|
-
kind: v.picklist(['allowance', 'purchased', 'grant']),
|
|
84
|
-
remaining: creditsSchema,
|
|
85
|
-
expiresAt: v.nullable(v.pipe(v.string(), v.isoTimestamp()))
|
|
86
|
-
})
|
|
87
|
-
export type AICreditBucket = v.InferOutput<typeof aiCreditBucketSchema>
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* A service records a request's charge before the request's answer ends, so a wallet read after
|
|
91
|
-
* an answer includes what it cost.
|
|
92
|
-
*/
|
|
93
|
-
export const aiWalletSchema = v.object({
|
|
94
|
-
/** Credits left to spend; `null` when the service sets no limit. */
|
|
95
|
-
balance: v.nullable(creditsSchema),
|
|
96
|
-
/** Spent first to last. */
|
|
97
|
-
buckets: v.array(aiCreditBucketSchema)
|
|
98
|
-
})
|
|
99
|
-
export type AIWallet = v.InferOutput<typeof aiWalletSchema>
|
|
100
|
-
|
|
101
|
-
export const aiUsageEntrySchema = v.object({
|
|
102
|
-
id: v.pipe(v.string(), v.minLength(1)),
|
|
103
|
-
createdAt: v.pipe(v.string(), v.isoTimestamp()),
|
|
104
|
-
kind: v.picklist(['model', 'image', 'search']),
|
|
105
|
-
modelID: v.nullable(v.string()),
|
|
106
|
-
userId: v.pipe(v.string(), v.minLength(1)),
|
|
107
|
-
credits: creditsSchema
|
|
108
|
-
})
|
|
109
|
-
export type AIUsageEntry = v.InferOutput<typeof aiUsageEntrySchema>
|
|
110
|
-
|
|
111
|
-
export const aiUsageQuerySchema = v.object({
|
|
112
|
-
cursor: v.optional(v.pipe(v.string(), v.minLength(1)))
|
|
113
|
-
})
|
|
114
|
-
export type AIUsageQuery = v.InferOutput<typeof aiUsageQuerySchema>
|
|
115
|
-
|
|
116
|
-
export function parseAIUsageQuery(input: unknown): AIUsageQuery {
|
|
117
|
-
return v.parse(aiUsageQuerySchema, input)
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export const aiUsagePageSchema = v.object({
|
|
121
|
-
entries: v.array(aiUsageEntrySchema),
|
|
122
|
-
next: v.nullable(v.string())
|
|
123
|
-
})
|
|
124
|
-
export type AIUsagePage = v.InferOutput<typeof aiUsagePageSchema>
|
|
125
|
-
|
|
126
|
-
/** Errors an AI service answers with, beside the upstream model's own. */
|
|
127
|
-
export const aiErrorCodeSchema = v.picklist([
|
|
128
|
-
'unauthorized',
|
|
129
|
-
'insufficient_credits',
|
|
130
|
-
'model_not_allowed',
|
|
131
|
-
'rate_limited'
|
|
132
|
-
])
|
|
133
|
-
export type AIErrorCode = v.InferOutput<typeof aiErrorCodeSchema>
|
|
134
|
-
|
|
135
|
-
export const aiErrorSchema = v.object({
|
|
136
|
-
error: v.object({
|
|
137
|
-
code: aiErrorCodeSchema,
|
|
138
|
-
message: v.optional(v.string()),
|
|
139
|
-
/** With `insufficient_credits`: when the workspace's allowance renews, if the service knows. */
|
|
140
|
-
resetsAt: v.optional(v.pipe(v.string(), v.isoTimestamp()))
|
|
141
|
-
})
|
|
142
|
-
})
|
|
143
|
-
export type AIError = v.InferOutput<typeof aiErrorSchema>
|
|
144
|
-
|
|
145
|
-
export const AI_ERROR_STATUS = {
|
|
146
|
-
unauthorized: 401,
|
|
147
|
-
insufficient_credits: 402,
|
|
148
|
-
model_not_allowed: 403,
|
|
149
|
-
rate_limited: 429
|
|
150
|
-
} as const satisfies Record<AIErrorCode, number>
|