@open-pencil/cloud 0.0.0-stage → 0.17.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/LICENSE-MIT +21 -0
- package/NOTICE +7 -0
- package/README.md +37 -2
- package/dist/bin/ai-relay.d.mts +1 -0
- package/dist/bin/ai-relay.mjs +31 -0
- package/dist/bin/ai-relay.mjs.map +1 -0
- package/dist/bin/server.d.mts +1 -0
- package/dist/bin/server.mjs +29 -0
- package/dist/bin/server.mjs.map +1 -0
- package/dist/client.d.ts +1520 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +1917 -0
- package/dist/client.js.map +1 -0
- package/dist/contract.d.ts +2 -0
- package/dist/contract.js +2 -0
- package/dist/contract2.js +1453 -0
- package/dist/contract2.js.map +1 -0
- package/dist/email.d.ts +169 -0
- package/dist/email.d.ts.map +1 -0
- package/dist/email.js +670 -0
- package/dist/email.js.map +1 -0
- package/dist/index.d.ts +1755 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/listen.mjs +1881 -0
- package/dist/listen.mjs.map +1 -0
- package/dist/runtime-node.d.mts +10040 -0
- package/dist/runtime-node.d.mts.map +1 -0
- package/dist/runtime-node.mjs +32 -0
- package/dist/runtime-node.mjs.map +1 -0
- package/dist/server.d.ts +12792 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +8475 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +8324 -0
- package/dist/server.mjs.map +1 -0
- package/dist/server2.mjs +505 -0
- package/dist/server2.mjs.map +1 -0
- package/package.json +124 -4
- package/src/admin/api/routes.ts +223 -0
- package/src/admin/api/security.ts +12 -0
- package/src/admin/audit/service.ts +138 -0
- package/src/admin/email/service.ts +140 -0
- package/src/admin/enrollment/email.ts +77 -0
- package/src/admin/enrollment/service.ts +308 -0
- package/src/admin/errors.ts +20 -0
- package/src/admin/index.ts +19 -0
- package/src/admin/list.ts +61 -0
- package/src/admin/operations/service.ts +70 -0
- package/src/admin/schema.ts +28 -0
- package/src/admin/users/service.ts +369 -0
- package/src/ai-relay/app.ts +251 -0
- package/src/ai-relay/config.ts +120 -0
- package/src/ai-relay/db/index.ts +10 -0
- package/src/ai-relay/db/migrate.ts +32 -0
- package/src/ai-relay/db/migrations/001_usage.ts +26 -0
- package/src/ai-relay/db/schema.ts +20 -0
- package/src/ai-relay/index.ts +18 -0
- package/src/ai-relay/metering.ts +68 -0
- package/src/ai-relay/usage.ts +115 -0
- package/src/client/ai.ts +96 -0
- package/src/client/api.ts +528 -0
- package/src/client/auth/client.ts +188 -0
- package/src/client/auth/failure.ts +54 -0
- package/src/client/auth/navigation.ts +45 -0
- package/src/client/device-authorization.ts +134 -0
- package/src/client/discovery.ts +105 -0
- package/src/client/images.ts +81 -0
- package/src/client/index.ts +53 -0
- package/src/client/portal/account.ts +183 -0
- package/src/client/portal/administration.ts +130 -0
- package/src/client/portal/client.ts +18 -0
- package/src/client/portal/request.ts +115 -0
- package/src/client/response.ts +51 -0
- package/src/client/workspaces.ts +142 -0
- package/src/contract/account.ts +204 -0
- package/src/contract/admin/index.ts +187 -0
- package/src/contract/ai.ts +150 -0
- package/src/contract/collaboration.ts +45 -0
- package/src/contract/discovery.ts +81 -0
- package/src/contract/documents.ts +265 -0
- package/src/contract/entitlements.ts +28 -0
- package/src/contract/index.ts +204 -0
- package/src/contract/links.ts +87 -0
- package/src/contract/portal.ts +11 -0
- package/src/contract/relay.ts +368 -0
- package/src/contract/sharing.ts +191 -0
- package/src/contract/versions.ts +87 -0
- package/src/contract/workspaces.ts +100 -0
- package/src/email/index.ts +14 -0
- package/src/email/render.ts +276 -0
- package/src/email/schema.ts +144 -0
- package/src/email/templates/brand.ts +105 -0
- package/src/email/templates/document-invitation.ts +35 -0
- package/src/email/templates/enrollment.ts +27 -0
- package/src/email/templates/layout.ts +18 -0
- package/src/runtime/cloudflare/email.ts +70 -0
- package/src/runtime/cloudflare/environment.ts +57 -0
- package/src/runtime/cloudflare/index.ts +3 -0
- package/src/runtime/cloudflare/relay.ts +136 -0
- package/src/runtime/cloudflare/site.ts +13 -0
- package/src/runtime/cloudflare/tsconfig.json +21 -0
- package/src/runtime/cloudflare/worker.ts +160 -0
- package/src/runtime/node/admin.ts +59 -0
- package/src/runtime/node/ai-relay/main.ts +35 -0
- package/src/runtime/node/ai-relay/server.ts +63 -0
- package/src/runtime/node/application.ts +50 -0
- package/src/runtime/node/bootstrap.ts +22 -0
- package/src/runtime/node/cleanup.ts +24 -0
- package/src/runtime/node/command.ts +17 -0
- package/src/runtime/node/config.ts +16 -0
- package/src/runtime/node/email/runtime.ts +43 -0
- package/src/runtime/node/email/transport.ts +112 -0
- package/src/runtime/node/entitlements/main.ts +41 -0
- package/src/runtime/node/entitlements/refusal.ts +15 -0
- package/src/runtime/node/index.ts +25 -0
- package/src/runtime/node/listen.ts +35 -0
- package/src/runtime/node/main.ts +29 -0
- package/src/runtime/node/migrate.ts +13 -0
- package/src/runtime/node/quota-reconcile.ts +9 -0
- package/src/runtime/node/relay.ts +54 -0
- package/src/runtime/node/server.ts +121 -0
- package/src/runtime/node/site.ts +64 -0
- package/src/runtime/postgres/database.ts +19 -0
- package/src/runtime/s3/objects.ts +244 -0
- package/src/runtime/site/server.ts +82 -0
- package/src/server/account/actions.ts +66 -0
- package/src/server/account/routes.ts +300 -0
- package/src/server/ai/errors.ts +7 -0
- package/src/server/ai/index.ts +4 -0
- package/src/server/ai/keys.ts +43 -0
- package/src/server/ai/routes.ts +43 -0
- package/src/server/ai/service.ts +70 -0
- package/src/server/api/index.ts +8 -0
- package/src/server/api/router.ts +117 -0
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +375 -0
- package/src/server/auth/account.ts +73 -0
- package/src/server/auth/activity.ts +213 -0
- package/src/server/auth/adapter.ts +79 -0
- package/src/server/auth/email.ts +125 -0
- package/src/server/auth/endpoints.ts +57 -0
- package/src/server/auth/factory.ts +583 -0
- package/src/server/auth/index.ts +17 -0
- package/src/server/auth/mfa-assurance.ts +55 -0
- package/src/server/auth/runtime.ts +26 -0
- package/src/server/auth/session.ts +19 -0
- package/src/server/auth/social-providers.ts +55 -0
- package/src/server/auth/user-agent.ts +16 -0
- package/src/server/auth/workspace-access.ts +23 -0
- package/src/server/cleanup/configured.ts +38 -0
- package/src/server/cleanup/documents.ts +231 -0
- package/src/server/cleanup/index.ts +29 -0
- package/src/server/cleanup/objects.ts +27 -0
- package/src/server/cleanup/revisions.ts +232 -0
- package/src/server/cleanup/uploads.ts +129 -0
- package/src/server/cleanup/worker.ts +79 -0
- package/src/server/collaboration/index.ts +28 -0
- package/src/server/collaboration/relay/authorize.ts +53 -0
- package/src/server/collaboration/relay/awareness.ts +104 -0
- package/src/server/collaboration/relay/documents.ts +39 -0
- package/src/server/collaboration/relay/hub.ts +524 -0
- package/src/server/collaboration/relay/index.ts +15 -0
- package/src/server/collaboration/relay/persistence.ts +48 -0
- package/src/server/collaboration/routes.ts +77 -0
- package/src/server/collaboration/service.ts +188 -0
- package/src/server/config/deployment.ts +178 -0
- package/src/server/config/index.ts +20 -0
- package/src/server/config/limits.ts +8 -0
- package/src/server/config/public.ts +66 -0
- package/src/server/config/schema.ts +512 -0
- package/src/server/db/database.ts +15 -0
- package/src/server/db/expressions.ts +13 -0
- package/src/server/db/index.ts +21 -0
- package/src/server/db/migrate.ts +177 -0
- package/src/server/db/migrations/001_foundation.ts +164 -0
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +34 -0
- package/src/server/db/migrations/003_document_cleanup_claims.ts +23 -0
- package/src/server/db/migrations/004_document_sharing.ts +82 -0
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +13 -0
- package/src/server/db/migrations/006_invitation_continuation.ts +27 -0
- package/src/server/db/migrations/007_collaboration_state.ts +25 -0
- package/src/server/db/migrations/008_storage_reservations.ts +37 -0
- package/src/server/db/migrations/009_workspace_entitlements.ts +22 -0
- package/src/server/db/migrations/010_upload_finalization.ts +22 -0
- package/src/server/db/migrations/011_upload_finalization_lease.ts +12 -0
- package/src/server/db/migrations/012_transactional_email.ts +48 -0
- package/src/server/db/migrations/013_admin_enrollment.ts +54 -0
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +30 -0
- package/src/server/db/migrations/015_cloud_rate_limit.ts +22 -0
- package/src/server/db/migrations/016_auth_schema.ts +16 -0
- package/src/server/db/migrations/017_authentication_email_kinds.ts +32 -0
- package/src/server/db/migrations/018_authentication_mfa.ts +78 -0
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +42 -0
- package/src/server/db/migrations/020_workspace_invitations.ts +57 -0
- package/src/server/db/migrations/021_document_versions.ts +66 -0
- package/src/server/db/migrations/022_entitlement_external_version.ts +17 -0
- package/src/server/db/migrations/023_document_images.ts +61 -0
- package/src/server/db/migrations/024_account_activity.ts +66 -0
- package/src/server/db/migrations/025_table_names.ts +58 -0
- package/src/server/db/migrations/026_user_picture.ts +30 -0
- package/src/server/db/migrations/027_document_kind.ts +19 -0
- package/src/server/db/migrations/helpers.ts +78 -0
- package/src/server/db/tables.ts +394 -0
- package/src/server/documents/access.ts +103 -0
- package/src/server/documents/errors.ts +16 -0
- package/src/server/documents/images.ts +407 -0
- package/src/server/documents/index.ts +14 -0
- package/src/server/documents/limits.ts +57 -0
- package/src/server/documents/repository.ts +141 -0
- package/src/server/documents/routes.ts +239 -0
- package/src/server/documents/service.ts +597 -0
- package/src/server/documents/summary.ts +37 -0
- package/src/server/documents/types.ts +14 -0
- package/src/server/documents/versions/checkpoint.ts +98 -0
- package/src/server/documents/versions/history.ts +25 -0
- package/src/server/documents/versions/index.ts +4 -0
- package/src/server/documents/versions/routes.ts +112 -0
- package/src/server/documents/versions/service.ts +397 -0
- package/src/server/email/crypto.ts +57 -0
- package/src/server/email/index.ts +18 -0
- package/src/server/email/invitations.ts +30 -0
- package/src/server/email/service.ts +245 -0
- package/src/server/email/types.ts +56 -0
- package/src/server/email/worker.ts +33 -0
- package/src/server/index.ts +279 -0
- package/src/server/internal/index.ts +8 -0
- package/src/server/internal/routes.ts +41 -0
- package/src/server/internal/schema.ts +10 -0
- package/src/server/internal/service.ts +44 -0
- package/src/server/invitations/index.ts +6 -0
- package/src/server/invitations/types.ts +30 -0
- package/src/server/objects/index.ts +16 -0
- package/src/server/objects/types.ts +81 -0
- package/src/server/pictures/index.ts +9 -0
- package/src/server/pictures/provider.ts +61 -0
- package/src/server/pictures/routes.ts +21 -0
- package/src/server/pictures/service.ts +235 -0
- package/src/server/policy/configured.ts +23 -0
- package/src/server/policy/database.ts +108 -0
- package/src/server/policy/entitlements.ts +29 -0
- package/src/server/policy/index.ts +17 -0
- package/src/server/policy/keys.ts +23 -0
- package/src/server/policy/policy.ts +117 -0
- package/src/server/policy/provider.ts +67 -0
- package/src/server/policy/schema.ts +69 -0
- package/src/server/policy/service.ts +85 -0
- package/src/server/policy/static.ts +62 -0
- package/src/server/quota/index.ts +7 -0
- package/src/server/quota/reconcile.ts +61 -0
- package/src/server/quota/storage.ts +165 -0
- package/src/server/rate-limit/cleanup.ts +16 -0
- package/src/server/rate-limit/index.ts +12 -0
- package/src/server/rate-limit/keys.ts +38 -0
- package/src/server/rate-limit/middleware.ts +137 -0
- package/src/server/rate-limit/postgres.ts +79 -0
- package/src/server/sharing/access.ts +29 -0
- package/src/server/sharing/actor.ts +12 -0
- package/src/server/sharing/capabilities/service.ts +262 -0
- package/src/server/sharing/continuation.ts +85 -0
- package/src/server/sharing/contracts.ts +80 -0
- package/src/server/sharing/crypto.ts +48 -0
- package/src/server/sharing/document-routes.ts +41 -0
- package/src/server/sharing/errors.ts +12 -0
- package/src/server/sharing/grants/service.ts +79 -0
- package/src/server/sharing/http.ts +41 -0
- package/src/server/sharing/index.ts +39 -0
- package/src/server/sharing/invitations/service.ts +315 -0
- package/src/server/sharing/policy.ts +44 -0
- package/src/server/sharing/routes.ts +251 -0
- package/src/server/sharing/service.ts +117 -0
- package/src/server/validation.ts +24 -0
- package/src/server/worker/interval.ts +23 -0
- package/src/server/worker/periodic.ts +42 -0
- package/src/server/workspaces/index.ts +16 -0
- package/src/server/workspaces/members.ts +502 -0
- package/src/server/workspaces/repository.ts +91 -0
- package/src/server/workspaces/routes.ts +198 -0
- package/src/server/workspaces/service.ts +84 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { aiCatalogSchema, type AICatalog } from '#cloud/contract'
|
|
2
|
+
import { httpURLSchema } from '#cloud/server/config/schema'
|
|
3
|
+
import { parse as parseTOML } from 'smol-toml'
|
|
4
|
+
import * as v from 'valibot'
|
|
5
|
+
|
|
6
|
+
export type AIRelayEnvironment = Readonly<Record<string, string | undefined>>
|
|
7
|
+
|
|
8
|
+
export type AIRelayConfig = {
|
|
9
|
+
/** The Cloud whose tokens the relay accepts: their issuer, and where its keys are published. */
|
|
10
|
+
cloudURL: string
|
|
11
|
+
/** Browser origins allowed to call the relay; the desktop editor is not subject to CORS. */
|
|
12
|
+
allowedOrigins: string[]
|
|
13
|
+
/** Model requests a workspace may have in flight at once. */
|
|
14
|
+
maximumConcurrentRequests: number
|
|
15
|
+
/** The largest model request body accepted, in bytes. */
|
|
16
|
+
maximumRequestBytes: number
|
|
17
|
+
databaseURL: string
|
|
18
|
+
/** An OpenAI-compatible API base URL, such as `https://openrouter.ai/api/v1`. */
|
|
19
|
+
upstreamURL: string
|
|
20
|
+
upstreamKey: string
|
|
21
|
+
catalog: AICatalog
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class AIRelayConfigError extends Error {
|
|
25
|
+
constructor(message: string) {
|
|
26
|
+
super(message)
|
|
27
|
+
this.name = 'AIRelayConfigError'
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const DEFAULT_SECRET_NAMES = {
|
|
32
|
+
databaseURL: 'DATABASE_URL',
|
|
33
|
+
upstreamKey: 'OPENPENCIL_AI_RELAY_UPSTREAM_KEY'
|
|
34
|
+
} as const
|
|
35
|
+
|
|
36
|
+
const textSchema = v.pipe(v.string(), v.trim(), v.minLength(1))
|
|
37
|
+
const creditsSchema = v.pipe(v.number(), v.integer(), v.minValue(0))
|
|
38
|
+
const secretReferenceSchema = v.object({ from_env: textSchema })
|
|
39
|
+
const defaultReference = (name: string) => v.optional(secretReferenceSchema, { from_env: name })
|
|
40
|
+
|
|
41
|
+
const modelSchema = v.object({
|
|
42
|
+
id: textSchema,
|
|
43
|
+
label: textSchema,
|
|
44
|
+
credits_per_million_input_tokens: creditsSchema,
|
|
45
|
+
credits_per_million_output_tokens: creditsSchema,
|
|
46
|
+
context_tokens: v.pipe(v.number(), v.integer(), v.minValue(1)),
|
|
47
|
+
reasoning: v.optional(v.boolean(), false),
|
|
48
|
+
vision: v.optional(v.boolean(), false),
|
|
49
|
+
tools: v.optional(v.boolean(), true)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const aiRelayConfigSchema = v.object({
|
|
53
|
+
schema_version: v.literal(1),
|
|
54
|
+
relay: v.object({
|
|
55
|
+
cloud_url: httpURLSchema,
|
|
56
|
+
allowed_origins: v.optional(v.array(httpURLSchema), []),
|
|
57
|
+
max_concurrent_requests: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1)), 4),
|
|
58
|
+
// Room for the images a vision request carries inline.
|
|
59
|
+
max_request_bytes: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1)), 16 * 1024 * 1024)
|
|
60
|
+
}),
|
|
61
|
+
database: v.optional(v.object({ url: defaultReference(DEFAULT_SECRET_NAMES.databaseURL) }), {
|
|
62
|
+
url: { from_env: DEFAULT_SECRET_NAMES.databaseURL }
|
|
63
|
+
}),
|
|
64
|
+
upstream: v.object({
|
|
65
|
+
base_url: httpURLSchema,
|
|
66
|
+
api_key: defaultReference(DEFAULT_SECRET_NAMES.upstreamKey)
|
|
67
|
+
}),
|
|
68
|
+
catalog: v.object({
|
|
69
|
+
default_model: textSchema,
|
|
70
|
+
models: v.pipe(v.array(modelSchema), v.minLength(1))
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
export type AIRelayConfigInput = v.InferInput<typeof aiRelayConfigSchema>
|
|
75
|
+
|
|
76
|
+
function requiredSecret(environment: AIRelayEnvironment, name: string): string {
|
|
77
|
+
const value = environment[name]
|
|
78
|
+
if (!value) throw new AIRelayConfigError(`Required secret binding is unavailable: ${name}`)
|
|
79
|
+
return value
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function catalogFrom(input: v.InferOutput<typeof aiRelayConfigSchema>['catalog']): AICatalog {
|
|
83
|
+
const ids = new Set<string>()
|
|
84
|
+
for (const model of input.models) {
|
|
85
|
+
if (ids.has(model.id)) throw new AIRelayConfigError(`Model ${model.id} is listed twice`)
|
|
86
|
+
ids.add(model.id)
|
|
87
|
+
}
|
|
88
|
+
if (!ids.has(input.default_model)) {
|
|
89
|
+
throw new AIRelayConfigError(`Default model ${input.default_model} is not in the catalog`)
|
|
90
|
+
}
|
|
91
|
+
return v.parse(aiCatalogSchema, {
|
|
92
|
+
defaultModelID: input.default_model,
|
|
93
|
+
models: input.models.map((model) => ({
|
|
94
|
+
id: model.id,
|
|
95
|
+
label: model.label,
|
|
96
|
+
creditsPerMillionInputTokens: model.credits_per_million_input_tokens,
|
|
97
|
+
creditsPerMillionOutputTokens: model.credits_per_million_output_tokens,
|
|
98
|
+
contextTokens: model.context_tokens,
|
|
99
|
+
capabilities: { reasoning: model.reasoning, vision: model.vision, tools: model.tools }
|
|
100
|
+
}))
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function parseAIRelayConfig(input: unknown, environment: AIRelayEnvironment): AIRelayConfig {
|
|
105
|
+
const config = v.parse(aiRelayConfigSchema, input)
|
|
106
|
+
return {
|
|
107
|
+
cloudURL: new URL(config.relay.cloud_url).origin,
|
|
108
|
+
allowedOrigins: config.relay.allowed_origins.map((origin) => new URL(origin).origin),
|
|
109
|
+
maximumConcurrentRequests: config.relay.max_concurrent_requests,
|
|
110
|
+
maximumRequestBytes: config.relay.max_request_bytes,
|
|
111
|
+
databaseURL: requiredSecret(environment, config.database.url.from_env),
|
|
112
|
+
upstreamURL: config.upstream.base_url.replace(/\/+$/, ''),
|
|
113
|
+
upstreamKey: requiredSecret(environment, config.upstream.api_key.from_env),
|
|
114
|
+
catalog: catalogFrom(config.catalog)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function parseAIRelayTOML(source: string, environment: AIRelayEnvironment): AIRelayConfig {
|
|
119
|
+
return parseAIRelayConfig(parseTOML(source), environment)
|
|
120
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { addCurrentTimestampColumns } from '#cloud/server/db/migrations/helpers'
|
|
2
|
+
import type { Kysely } from 'kysely'
|
|
3
|
+
|
|
4
|
+
/** One row per settled model request; a workspace's monthly spend is the sum of its rows. */
|
|
5
|
+
export async function up(database: Kysely<unknown>): Promise<void> {
|
|
6
|
+
await database.schema
|
|
7
|
+
.createTable('ai_relay_usage')
|
|
8
|
+
.addColumn('id', 'uuid', (column) => column.primaryKey())
|
|
9
|
+
.addColumn('workspace_id', 'uuid', (column) => column.notNull())
|
|
10
|
+
.addColumn('user_id', 'text', (column) => column.notNull())
|
|
11
|
+
.addColumn('model_id', 'text', (column) => column.notNull())
|
|
12
|
+
.addColumn('input_tokens', 'integer', (column) => column.notNull())
|
|
13
|
+
.addColumn('output_tokens', 'integer', (column) => column.notNull())
|
|
14
|
+
.addColumn('credits', 'integer', (column) => column.notNull())
|
|
15
|
+
.execute()
|
|
16
|
+
await addCurrentTimestampColumns(database, 'ai_relay_usage', ['created_at'])
|
|
17
|
+
await database.schema
|
|
18
|
+
.createIndex('ai_relay_usage_workspace_created_idx')
|
|
19
|
+
.on('ai_relay_usage')
|
|
20
|
+
.columns(['workspace_id', 'created_at desc', 'id desc'])
|
|
21
|
+
.execute()
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function down(database: Kysely<unknown>): Promise<void> {
|
|
25
|
+
await database.schema.dropTable('ai_relay_usage').ifExists().execute()
|
|
26
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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>
|
|
@@ -0,0 +1,18 @@
|
|
|
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'
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { AIModel } from '#cloud/contract'
|
|
2
|
+
import * as v from 'valibot'
|
|
3
|
+
|
|
4
|
+
export type TokenUsage = { inputTokens: number; outputTokens: number }
|
|
5
|
+
|
|
6
|
+
const tokensSchema = v.pipe(v.number(), v.integer(), v.minValue(0))
|
|
7
|
+
const usageSchema = v.object({
|
|
8
|
+
usage: v.object({ prompt_tokens: tokensSchema, completion_tokens: tokensSchema })
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
const usageBodySchema = v.pipe(v.string(), v.parseJson(), usageSchema)
|
|
12
|
+
|
|
13
|
+
function usageOf(text: string): TokenUsage | null {
|
|
14
|
+
const parsed = v.safeParse(usageBodySchema, text)
|
|
15
|
+
if (!parsed.success) return null
|
|
16
|
+
return {
|
|
17
|
+
inputTokens: parsed.output.usage.prompt_tokens,
|
|
18
|
+
outputTokens: parsed.output.usage.completion_tokens
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Credits a request costs, rounded up so a request is never free. */
|
|
23
|
+
export function creditsFor(model: AIModel, usage: TokenUsage): number {
|
|
24
|
+
return Math.ceil(
|
|
25
|
+
(usage.inputTokens * model.creditsPerMillionInputTokens +
|
|
26
|
+
usage.outputTokens * model.creditsPerMillionOutputTokens) /
|
|
27
|
+
1_000_000
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Reads the token usage of a chat completion: the `usage` of a JSON answer, or of the last stream
|
|
33
|
+
* event that carries one, which upstreams send when asked with `stream_options.include_usage`.
|
|
34
|
+
*/
|
|
35
|
+
export async function meteredUsage(
|
|
36
|
+
body: ReadableStream<Uint8Array>,
|
|
37
|
+
streamed: boolean
|
|
38
|
+
): Promise<TokenUsage | null> {
|
|
39
|
+
const reader = body.getReader()
|
|
40
|
+
const decoder = new TextDecoder()
|
|
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
|
+
}
|
|
52
|
+
let usage: TokenUsage | null = null
|
|
53
|
+
let pending = ''
|
|
54
|
+
const read = (line: string) => {
|
|
55
|
+
if (!line.startsWith('data:')) return
|
|
56
|
+
const data = line.slice('data:'.length).trim()
|
|
57
|
+
if (data === '[DONE]') return
|
|
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)
|
|
65
|
+
}
|
|
66
|
+
read(pending)
|
|
67
|
+
return usage
|
|
68
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { AIUsagePage, AIWallet } from '#cloud/contract'
|
|
2
|
+
import { fromDate, startOfMonth, toCalendarDate } from '@internationalized/date'
|
|
3
|
+
import type { Kysely } from 'kysely'
|
|
4
|
+
import * as v from 'valibot'
|
|
5
|
+
|
|
6
|
+
import type { AIRelayDatabase, NewAIRelayUsage } from './db'
|
|
7
|
+
|
|
8
|
+
const USAGE_PAGE_SIZE = 50
|
|
9
|
+
|
|
10
|
+
/** The UTC calendar month a moment falls in, as its first instant and the next month's. */
|
|
11
|
+
export function usagePeriod(now: Date): { start: Date; end: Date } {
|
|
12
|
+
const month = startOfMonth(toCalendarDate(fromDate(now, 'UTC')))
|
|
13
|
+
return { start: month.toDate('UTC'), end: month.add({ months: 1 }).toDate('UTC') }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** A page boundary: the last entry's creation time in milliseconds and its ID. */
|
|
17
|
+
const cursorSchema = v.pipe(
|
|
18
|
+
v.string(),
|
|
19
|
+
v.regex(/^\d+\.[0-9a-f-]{36}$/),
|
|
20
|
+
v.transform((value) => {
|
|
21
|
+
const [milliseconds = '', id = ''] = value.split('.')
|
|
22
|
+
return { createdAt: new Date(Number(milliseconds)), id }
|
|
23
|
+
}),
|
|
24
|
+
v.check(
|
|
25
|
+
({ createdAt, id }) =>
|
|
26
|
+
!Number.isNaN(createdAt.getTime()) && v.is(v.pipe(v.string(), v.uuid()), id)
|
|
27
|
+
)
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
function encodeCursor(createdAt: Date, id: string): string {
|
|
31
|
+
return `${createdAt.getTime()}.${id}`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class AIUsageCursorError extends Error {
|
|
35
|
+
constructor() {
|
|
36
|
+
super('Invalid usage cursor')
|
|
37
|
+
this.name = 'AIUsageCursorError'
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createAIUsageStore(database: Kysely<AIRelayDatabase>) {
|
|
42
|
+
async function spent(workspaceId: string, now: Date): Promise<number> {
|
|
43
|
+
const { start, end } = usagePeriod(now)
|
|
44
|
+
const row = await database
|
|
45
|
+
.selectFrom('aiRelayUsage')
|
|
46
|
+
.select((query) => query.fn.sum<string | number | null>('credits').as('credits'))
|
|
47
|
+
.where('workspaceId', '=', workspaceId)
|
|
48
|
+
.where('createdAt', '>=', start)
|
|
49
|
+
.where('createdAt', '<', end)
|
|
50
|
+
.executeTakeFirst()
|
|
51
|
+
return Number(row?.credits ?? 0)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
async record(usage: NewAIRelayUsage): Promise<void> {
|
|
56
|
+
await database.insertInto('aiRelayUsage').values(usage).execute()
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The workspace's single allowance bucket for the current month. A `null` allowance means
|
|
61
|
+
* Cloud set none, and the relay does not limit spend.
|
|
62
|
+
*/
|
|
63
|
+
async wallet(workspaceId: string, allowance: number | null, now: Date): Promise<AIWallet> {
|
|
64
|
+
if (allowance === null) return { balance: null, buckets: [] }
|
|
65
|
+
const remaining = Math.max(0, allowance - (await spent(workspaceId, now)))
|
|
66
|
+
return {
|
|
67
|
+
balance: remaining,
|
|
68
|
+
buckets: [{ kind: 'allowance', remaining, expiresAt: usagePeriod(now).end.toISOString() }]
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
/** Newest first, across every member of the workspace. */
|
|
73
|
+
async page(workspaceId: string, cursor: string | undefined): Promise<AIUsagePage> {
|
|
74
|
+
let after: { createdAt: Date; id: string } | undefined
|
|
75
|
+
if (cursor !== undefined) {
|
|
76
|
+
const parsed = v.safeParse(cursorSchema, cursor)
|
|
77
|
+
if (!parsed.success) throw new AIUsageCursorError()
|
|
78
|
+
after = parsed.output
|
|
79
|
+
}
|
|
80
|
+
let query = database
|
|
81
|
+
.selectFrom('aiRelayUsage')
|
|
82
|
+
.select(['id', 'createdAt', 'modelId', 'userId', 'credits'])
|
|
83
|
+
.where('workspaceId', '=', workspaceId)
|
|
84
|
+
if (after) {
|
|
85
|
+
const { createdAt, id } = after
|
|
86
|
+
query = query.where((row) =>
|
|
87
|
+
row.or([
|
|
88
|
+
row('createdAt', '<', createdAt),
|
|
89
|
+
row.and([row('createdAt', '=', createdAt), row('id', '<', id)])
|
|
90
|
+
])
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
const rows = await query
|
|
94
|
+
.orderBy('createdAt', 'desc')
|
|
95
|
+
.orderBy('id', 'desc')
|
|
96
|
+
.limit(USAGE_PAGE_SIZE + 1)
|
|
97
|
+
.execute()
|
|
98
|
+
const entries = rows.slice(0, USAGE_PAGE_SIZE)
|
|
99
|
+
const last = entries.at(-1)
|
|
100
|
+
return {
|
|
101
|
+
entries: entries.map((row) => ({
|
|
102
|
+
id: row.id,
|
|
103
|
+
createdAt: row.createdAt.toISOString(),
|
|
104
|
+
kind: 'model',
|
|
105
|
+
modelID: row.modelId,
|
|
106
|
+
userId: row.userId,
|
|
107
|
+
credits: row.credits
|
|
108
|
+
})),
|
|
109
|
+
next: rows.length > USAGE_PAGE_SIZE && last ? encodeCursor(last.createdAt, last.id) : null
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export type AIUsageStore = ReturnType<typeof createAIUsageStore>
|
package/src/client/ai.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { CloudFetch } from '#cloud/client/discovery'
|
|
2
|
+
import {
|
|
3
|
+
aiCatalogSchema,
|
|
4
|
+
aiErrorSchema,
|
|
5
|
+
aiWalletSchema,
|
|
6
|
+
CLOUD_AI_API_PATHS,
|
|
7
|
+
type AICatalog,
|
|
8
|
+
type AIErrorCode,
|
|
9
|
+
type AIWallet
|
|
10
|
+
} from '#cloud/contract'
|
|
11
|
+
import * as v from 'valibot'
|
|
12
|
+
|
|
13
|
+
/** Where a request gets the token it sends, and how it drops one the service refused. */
|
|
14
|
+
export type CloudAITokenSource = {
|
|
15
|
+
token(): Promise<string>
|
|
16
|
+
/** Forgets `token`, so the next call signs a new one. */
|
|
17
|
+
invalidate(token: string): void
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class CloudAIError extends Error {
|
|
21
|
+
override readonly name = 'CloudAIError'
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
readonly status: number,
|
|
25
|
+
readonly code?: AIErrorCode
|
|
26
|
+
) {
|
|
27
|
+
super(`OpenPencil AI request failed with HTTP ${status}`)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Whether a request can be sent twice: a stream, in `init` or a `Request`, is read by the first. */
|
|
32
|
+
function replayable(input: RequestInfo | URL, body: RequestInit['body']): boolean {
|
|
33
|
+
if (input instanceof Request && input.body) return false
|
|
34
|
+
return !(body instanceof ReadableStream)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Sends each request with a current token. A token the service refuses, for instance one signed
|
|
39
|
+
* by a key Cloud has since replaced, is dropped and a request that can be replayed is sent once
|
|
40
|
+
* more with a new one.
|
|
41
|
+
*/
|
|
42
|
+
export function cloudAIFetch(tokens: CloudAITokenSource, fetch: CloudFetch): CloudFetch {
|
|
43
|
+
const send = async (input: RequestInfo | URL, init: RequestInit | undefined, token: string) => {
|
|
44
|
+
const headers = new Headers(init?.headers)
|
|
45
|
+
headers.set('Authorization', `Bearer ${token}`)
|
|
46
|
+
return fetch(input, { ...init, headers })
|
|
47
|
+
}
|
|
48
|
+
return async (input, init) => {
|
|
49
|
+
const token = await tokens.token()
|
|
50
|
+
const response = await send(input, init, token)
|
|
51
|
+
if (response.status !== 401) return response
|
|
52
|
+
tokens.invalidate(token)
|
|
53
|
+
if (!replayable(input, init?.body)) return response
|
|
54
|
+
return send(input, init, await tokens.token())
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const errorBodySchema = v.pipe(v.string(), v.parseJson(), aiErrorSchema)
|
|
59
|
+
|
|
60
|
+
async function parsed<TSchema extends v.GenericSchema>(
|
|
61
|
+
response: Response,
|
|
62
|
+
schema: TSchema
|
|
63
|
+
): Promise<v.InferOutput<TSchema>> {
|
|
64
|
+
const text = await response.text()
|
|
65
|
+
if (!response.ok) {
|
|
66
|
+
const error = v.safeParse(errorBodySchema, text)
|
|
67
|
+
throw new CloudAIError(response.status, error.success ? error.output.error.code : undefined)
|
|
68
|
+
}
|
|
69
|
+
return v.parse(v.pipe(v.string(), v.parseJson(), schema), text)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type CloudAIClientOptions = {
|
|
73
|
+
/** The service's endpoint, as Cloud's discovery names it. */
|
|
74
|
+
endpoint: string
|
|
75
|
+
tokens: CloudAITokenSource
|
|
76
|
+
fetch: CloudFetch
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The AI service a Cloud advertises: its model API base URL, catalog, and the wallet. */
|
|
80
|
+
export function createCloudAIClient(options: CloudAIClientOptions) {
|
|
81
|
+
const endpoint = options.endpoint.replace(/\/+$/, '')
|
|
82
|
+
const fetch = cloudAIFetch(options.tokens, options.fetch)
|
|
83
|
+
return {
|
|
84
|
+
/** The base URL an OpenAI-compatible client sends chat completions below. */
|
|
85
|
+
modelBaseURL: `${endpoint}${CLOUD_AI_API_PATHS.base}`,
|
|
86
|
+
fetch,
|
|
87
|
+
async catalog(): Promise<AICatalog> {
|
|
88
|
+
return parsed(await fetch(`${endpoint}${CLOUD_AI_API_PATHS.catalog}`), aiCatalogSchema)
|
|
89
|
+
},
|
|
90
|
+
async wallet(): Promise<AIWallet> {
|
|
91
|
+
return parsed(await fetch(`${endpoint}${CLOUD_AI_API_PATHS.wallet}`), aiWalletSchema)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export type CloudAIClient = ReturnType<typeof createCloudAIClient>
|