create-signet-app 0.5.0 → 0.7.0
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 +31 -8
- package/base/api/api/app.ts +57 -8
- package/base/api/api/db/schema.ts +16 -4
- package/base/api/api/env.ts +8 -0
- package/base/api/api/server.ts +6 -0
- package/base/api/api/services.ts +43 -3
- package/base/api/drizzle/0002_ai_paywalls_workflows.sql +77 -0
- package/base/api/drizzle/meta/0002_snapshot.json +1759 -0
- package/base/api/drizzle/meta/_journal.json +7 -0
- package/base/shared/README.md +87 -0
- package/base/web/index.html +5 -0
- package/base/web/src/main.tsx +2 -0
- package/base/web/src/routeTree.gen.ts +25 -0
- package/base/web/src/router.test.ts +1 -0
- package/base/web/src/routes/_app/index.tsx +7 -0
- package/base/web/src/routes/_app.tsx +96 -49
- package/package.json +5 -2
- package/parts/ai/api/api/routes/ai.ts +28 -0
- package/parts/ai/api/test/ai.test.ts +52 -0
- package/parts/ai/part.ts +16 -0
- package/parts/dashboard/part.ts +6 -0
- package/parts/dashboard/web/src/components/overview.test.tsx +16 -0
- package/parts/dashboard/web/src/components/overview.tsx +175 -0
- package/parts/paywalls/api/api/routes/paywall.ts +126 -0
- package/parts/paywalls/api/test/paywalls.test.ts +71 -0
- package/parts/paywalls/part.ts +8 -0
- package/parts/pwa/part.ts +6 -0
- package/parts/pwa/web/public/icon.svg +6 -0
- package/parts/pwa/web/public/manifest.webmanifest +10 -0
- package/parts/pwa/web/public/sw.js +69 -0
- package/parts/pwa/web/src/components/install-button.tsx +14 -0
- package/parts/pwa/web/src/lib/pwa.ts +39 -0
- package/parts/workflows/api/api/workflows.ts +33 -0
- package/parts/workflows/api/test/workflows.test.ts +44 -0
- package/parts/workflows/part.ts +7 -0
- package/parts/workflows/web/src/routes/_app/approvals.tsx +34 -0
- package/src/compose.ts +107 -30
- package/src/desktop.ts +1 -1
- package/src/index.ts +49 -8
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ a plain `node_modules` (`.yarnrc.yml`), and lists signet-ui's required peers,
|
|
|
30
30
|
which Yarn does not install on its own. Deno 2 installs and runs the
|
|
31
31
|
`package.json` scripts; `--platform cli` is refused with deno, as its build is
|
|
32
32
|
`bun build --compile`. Tests stay on `bun test`, so CI sets up Bun too.
|
|
33
|
-
`bun run test
|
|
33
|
+
`bun run test:pm` scaffolds the full starter and the web platform with each
|
|
34
34
|
manager installed on the machine, installs, typechecks and builds.
|
|
35
35
|
|
|
36
36
|
## Which platform
|
|
@@ -58,6 +58,7 @@ bunx create-signet-app my-app --without admin,privacy # the full starter w
|
|
|
58
58
|
bunx create-signet-app my-api --api --with orgs # the API half only
|
|
59
59
|
bunx create-signet-app my-ui --ui --with account # the web half only
|
|
60
60
|
bunx create-signet-app my-app --starter blank # no parts: shell, API, database
|
|
61
|
+
bunx create-signet-app my-app --starter saas --with ai # a starter, plus a part
|
|
61
62
|
```
|
|
62
63
|
|
|
63
64
|
- **Surfaces.** Both halves by default. `--ui` writes the web half only: its
|
|
@@ -65,9 +66,23 @@ bunx create-signet-app my-app --starter blank # no parts: shell, A
|
|
|
65
66
|
until you point `src/lib/api.ts` at that API's `AppType`. `--api` writes the
|
|
66
67
|
API half only: no web files, no Vite. `--ui --api` together is an error (it
|
|
67
68
|
is the default).
|
|
68
|
-
- **Starters** are named mixes of parts
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
- **Starters** are named mixes of parts, by the shape of app. `--with` adds to
|
|
70
|
+
the starter, `--without` takes away; `--without` alone starts from `full`.
|
|
71
|
+
Run with no flags on a terminal to pick a platform or starter from a list.
|
|
72
|
+
|
|
73
|
+
| starter | parts (plus what they need) |
|
|
74
|
+
| --- | --- |
|
|
75
|
+
| `full` | every part but `dashboard` and `pwa` (`--template full` is the same) |
|
|
76
|
+
| `blank` | none: the shell, the API, the database |
|
|
77
|
+
| `saas` | account, orgs, admin, notifications, privacy, payments |
|
|
78
|
+
| `launch` | account, privacy, growth (waitlist, invites, referrals, feedback) |
|
|
79
|
+
| `internal-tool` | account, orgs, admin, workflows |
|
|
80
|
+
| `api` | account, payments; the API half only |
|
|
81
|
+
| `ai-app` | account, privacy, payments, ai |
|
|
82
|
+
| `paywall` | account, payments, paywalls |
|
|
83
|
+
| `automation` | account, notifications, workflows |
|
|
84
|
+
| `dashboard` | account, dashboard: a sidebar shell, home is an overview |
|
|
85
|
+
| `pwa` | account, pwa: installable, the app shell offline |
|
|
71
86
|
- **Needs** come along on their own and the output says so (`--with payments`
|
|
72
87
|
prints `added auth for payments`). Leaving out a part another one needs is
|
|
73
88
|
an error.
|
|
@@ -75,7 +90,7 @@ bunx create-signet-app my-app --starter blank # no parts: shell, A
|
|
|
75
90
|
`bun`: Bun.serve and bun:sqlite, as before). `deno` (Deno 2) and `node`
|
|
76
91
|
(22.18+) get node:sqlite under Drizzle and a node:http server instead, and
|
|
77
92
|
their `dev`/`start` scripts run on that runtime with `.env` loaded. Tests stay
|
|
78
|
-
on `bun test` (Bun runs node:sqlite too). `bun run test
|
|
93
|
+
on `bun test` (Bun runs node:sqlite too). `bun run test:runtime` serves the
|
|
79
94
|
full starter on each runtime installed and checks `/api/health`, an MCP call
|
|
80
95
|
and the built page.
|
|
81
96
|
- **MCP** is the `mcp` part. As before it is asked for on a terminal (default
|
|
@@ -90,7 +105,12 @@ bunx create-signet-app my-app --starter blank # no parts: shell, A
|
|
|
90
105
|
| `notifications` | the in-app inbox and preferences, `/notifications` | auth |
|
|
91
106
|
| `privacy` | DPDP consent and data export / erasure requests, `/privacy` | auth |
|
|
92
107
|
| `payments` | plans (`api/plans.ts`), entitlements, `/billing`, gateway webhooks | auth |
|
|
108
|
+
| `paywalls` | remote paywalls: versioned offerings and placements with audience overrides (signet-db's `createOfferings`), `GET /api/paywall/:placement`; with `admin`, publishing under `/api/admin/paywalls` (API only) | payments |
|
|
93
109
|
| `growth` | waitlist, invites, referral codes, referrals, feedback and marks tables, with signet-db's `createGrowth` (API only) | |
|
|
110
|
+
| `ai` | AI spend caps per user and for the app (signet-db's `createAiSpend`, `requireAiSpend` on `/api/ai/*`), a sample `/api/ai/summary` on signet-ai's fake provider (no key needed); with `admin`, `/api/admin/ai-spend` (API only) | auth |
|
|
111
|
+
| `workflows` | durable workflows (signet-db's `createWorkflows`) at `/api/workflows`, a generic approval-request example, the runner in `api/server.ts`, `/approvals` | auth |
|
|
112
|
+
| `dashboard` | the shell's nav in signet-ui's `Sidebar` (a drawer on phones), and home as an overview: stat tiles, a bar chart and a recent-activity table on sample data, with loading, empty and error states (`src/components/overview.tsx`; web only) | |
|
|
113
|
+
| `pwa` | `public/manifest.webmanifest`, a placeholder `public/icon.svg`, `public/sw.js` (network-first pages over a cached app shell, cached `/assets/`, never `/api`), registered in built apps by `src/lib/pwa.ts`, and an "Install app" button in the shell (web only) | |
|
|
94
114
|
| `mcp` | the API's validated routes as tools at `/mcp` (API only) | |
|
|
95
115
|
|
|
96
116
|
### How parts are put together
|
|
@@ -105,8 +125,10 @@ tree, the tests' helpers) lives in `base/` once, with each part's lines marked
|
|
|
105
125
|
`!<part>` means without it; `web` and `api` are the surfaces). The composer
|
|
106
126
|
keeps the chosen lines and drops every marker, so an app has no trace of the
|
|
107
127
|
parts it left out. The migrations in `base/api/drizzle/` cover every table; the
|
|
108
|
-
composer keeps the statements and snapshot entries for the chosen parts' tables
|
|
109
|
-
|
|
128
|
+
composer keeps the statements and snapshot entries for the chosen parts' tables
|
|
129
|
+
(a table two parts list, like `usage_counters` for `payments` and `ai`, stays
|
|
130
|
+
with either), drops a migration left empty and moves the later ones up a
|
|
131
|
+
number, so `db:check` passes and `db:generate` finds nothing to do.
|
|
110
132
|
|
|
111
133
|
## AI-assisted planning: `--describe`
|
|
112
134
|
|
|
@@ -331,7 +353,8 @@ config into anything new produces a crash.
|
|
|
331
353
|
|
|
332
354
|
## Test
|
|
333
355
|
|
|
334
|
-
`bun run test/scaffold-parts.ts
|
|
356
|
+
`bun run test:parts` (`test/scaffold-parts.ts`; CI runs it on PRs that touch
|
|
357
|
+
this package and nightly) scaffolds each part alone (plus what it needs), no
|
|
335
358
|
parts and every part, on each surface (`--ui`, `--api`, both), and checks each
|
|
336
359
|
app: no markers or placeholders left, only its surfaces' files, every
|
|
337
360
|
dependency imported (or a peer of one that is) and every import declared, lint,
|
package/base/api/api/app.ts
CHANGED
|
@@ -5,13 +5,16 @@ import { sessionRoutes } from "@dapplelight/signet-server/auth/hono"
|
|
|
5
5
|
import { maskPhone } from "@dapplelight/signet-server/auth/otp"
|
|
6
6
|
// orgs:end
|
|
7
7
|
import { errorHandler } from "@dapplelight/signet-server/http"
|
|
8
|
-
// admin
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
// admin
|
|
8
|
+
import { adminRoutes } from "@dapplelight/signet-server/http/admin" // @admin
|
|
9
|
+
// ai:begin
|
|
10
|
+
import { aiSpendAdminRoutes, requireAiSpend } from "@dapplelight/signet-server/http/ai-spend" // @admin
|
|
11
|
+
import { requireAiSpend } from "@dapplelight/signet-server/http/ai-spend" // @!admin
|
|
12
|
+
// ai:end
|
|
13
|
+
import { ApiException } from "@dapplelight/signet-server/http/errors" // @admin
|
|
12
14
|
// orgs:begin
|
|
13
15
|
import { orgRoutes, tenant } from "@dapplelight/signet-server/http/orgs"
|
|
14
16
|
// orgs:end
|
|
17
|
+
import { workflowRoutes } from "@dapplelight/signet-server/http/workflows" // @workflows
|
|
15
18
|
// mcp:begin
|
|
16
19
|
import { appTools } from "@dapplelight/signet-server/mcp"
|
|
17
20
|
import { mountMcp } from "@dapplelight/signet-server/mcp/hono"
|
|
@@ -32,20 +35,24 @@ import { type User, users } from "./db/schema.ts" // @orgs
|
|
|
32
35
|
import type { User } from "./db/schema.ts" // @!orgs
|
|
33
36
|
// auth:end
|
|
34
37
|
import { env } from "./env.ts"
|
|
35
|
-
// auth
|
|
36
|
-
import {
|
|
37
|
-
import { authRoutes, requireUser } from "./routes/auth.ts"
|
|
38
|
-
// auth:end
|
|
38
|
+
import { accountRoutes } from "./routes/account.ts" // @auth
|
|
39
|
+
import { aiRoutes } from "./routes/ai.ts" // @ai
|
|
40
|
+
import { authRoutes, requireUser } from "./routes/auth.ts" // @auth
|
|
39
41
|
// payments:begin
|
|
40
42
|
import { billingRoutes, billingWebhooks } from "./routes/billing.ts"
|
|
41
43
|
// payments:end
|
|
42
44
|
// notifications:begin
|
|
43
45
|
import { notificationRoutes } from "./routes/notifications.ts"
|
|
44
46
|
// notifications:end
|
|
47
|
+
// paywalls:begin
|
|
48
|
+
import { paywallAdminRoutes, paywallRoutes } from "./routes/paywall.ts" // @admin
|
|
49
|
+
import { paywallRoutes } from "./routes/paywall.ts" // @!admin
|
|
50
|
+
// paywalls:end
|
|
45
51
|
// privacy:begin
|
|
46
52
|
import { privacyRoutes } from "./routes/privacy.ts"
|
|
47
53
|
// privacy:end
|
|
48
54
|
import { createServices, type Deps, TRUST } from "./services.ts" // @auth
|
|
55
|
+
import { approvalRequest } from "./workflows.ts" // @workflows
|
|
49
56
|
import { type Deps, TRUST } from "./services.ts" // @!auth
|
|
50
57
|
|
|
51
58
|
export type AppEnv = { Variables: { reqId: string; user: User } } // @auth
|
|
@@ -59,6 +66,9 @@ const SIGNED_IN = [
|
|
|
59
66
|
"/api/privacy", // consent and data requests // @privacy
|
|
60
67
|
"/api/billing", // plan and checkout // @payments
|
|
61
68
|
"/api/admin", // the operator's admin API // @admin
|
|
69
|
+
"/api/ai", // AI calls, within the spend caps // @ai
|
|
70
|
+
"/api/paywall", // the paywall for a placement // @paywalls
|
|
71
|
+
"/api/workflows", // workflow runs and approvals // @workflows
|
|
62
72
|
]
|
|
63
73
|
// auth:end
|
|
64
74
|
|
|
@@ -98,6 +108,17 @@ export function createApp(deps: Deps) {
|
|
|
98
108
|
// auth:begin
|
|
99
109
|
for (const path of SIGNED_IN) base.use(`${path}/*`, requireUser(s))
|
|
100
110
|
// auth:end
|
|
111
|
+
// ai:begin
|
|
112
|
+
// Over the caller's or the app's AI spend cap: 429 with Retry-After, before
|
|
113
|
+
// the route runs (caps in api/env.ts and the ai_spend_caps table).
|
|
114
|
+
base.use(
|
|
115
|
+
"/api/ai/*",
|
|
116
|
+
requireAiSpend({
|
|
117
|
+
spend: s.aiSpend,
|
|
118
|
+
subject: (c) => ({ userId: (c.get("user" as never) as User).id }),
|
|
119
|
+
}),
|
|
120
|
+
)
|
|
121
|
+
// ai:end
|
|
101
122
|
// orgs:begin
|
|
102
123
|
const gate = requireUser(s)
|
|
103
124
|
base.use("/api/orgs/*", (c, next) =>
|
|
@@ -126,6 +147,21 @@ export function createApp(deps: Deps) {
|
|
|
126
147
|
.route("/api/billing", billingRoutes(s))
|
|
127
148
|
.route("/api/webhooks", billingWebhooks(s))
|
|
128
149
|
// payments:end
|
|
150
|
+
.route("/api/ai", aiRoutes(s)) // @ai
|
|
151
|
+
.route("/api/paywall", paywallRoutes(s)) // @paywalls
|
|
152
|
+
// workflows:begin
|
|
153
|
+
// Durable workflows (api/workflows.ts): anyone signed in may start an
|
|
154
|
+
// approval request and decide the ones waiting on them; admins see every run.
|
|
155
|
+
.route(
|
|
156
|
+
"/api/workflows",
|
|
157
|
+
workflowRoutes({
|
|
158
|
+
workflows: s.workflows,
|
|
159
|
+
subject: (c) => (c.get("user" as never) as User).id,
|
|
160
|
+
isAdmin: (c) => (c.get("user" as never) as User).role === "admin",
|
|
161
|
+
canStart: (_c, workflow) => workflow === approvalRequest.name,
|
|
162
|
+
}),
|
|
163
|
+
)
|
|
164
|
+
// workflows:end
|
|
129
165
|
// orgs:begin
|
|
130
166
|
// Orgs: switch, settings, members, invites (signet-server orgRoutes).
|
|
131
167
|
// Guard your own org routes the same way: orgGuards({ orgs: s.orgs, userId })
|
|
@@ -150,6 +186,19 @@ export function createApp(deps: Deps) {
|
|
|
150
186
|
)
|
|
151
187
|
// orgs:end
|
|
152
188
|
// admin:begin
|
|
189
|
+
// ai:begin
|
|
190
|
+
// AI spend for admins: usage, top spenders, and caps per user or for the app.
|
|
191
|
+
.route(
|
|
192
|
+
"/api/admin/ai-spend",
|
|
193
|
+
aiSpendAdminRoutes({
|
|
194
|
+
spend: s.aiSpend,
|
|
195
|
+
authorize: (c) => (c.get("user" as never) as User).role === "admin",
|
|
196
|
+
actor: (c) => ({ type: "user", id: (c.get("user" as never) as User).id }),
|
|
197
|
+
}),
|
|
198
|
+
)
|
|
199
|
+
// ai:end
|
|
200
|
+
// Publishing offerings and pointing placements at them. // @paywalls
|
|
201
|
+
.route("/api/admin/paywalls", paywallAdminRoutes(s)) // @paywalls
|
|
153
202
|
// The operator's admin API over every org (signet-db defineAdmin, config
|
|
154
203
|
// in services.ts): platform roles readonly, staff and admin (ADMIN_PHONES)
|
|
155
204
|
// only; everyone else gets 403. Org owners manage their org under /org.
|
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
// auth:begin
|
|
2
|
-
// payments|growth|orgs|privacy:begin
|
|
2
|
+
// payments|growth|orgs|privacy|ai|paywalls|workflows:begin
|
|
3
3
|
import {
|
|
4
|
+
aiSpendCaps, // @ai
|
|
4
5
|
auditLog,
|
|
5
|
-
entitlementTables, // @payments
|
|
6
|
+
entitlementTables, // @payments|ai
|
|
6
7
|
growthTables, // @growth
|
|
7
8
|
id,
|
|
8
9
|
idempotencyKeys,
|
|
10
|
+
monetisationTables, // @paywalls
|
|
9
11
|
orgTables, // @orgs
|
|
10
12
|
privacyTables, // @privacy
|
|
11
13
|
timestamps,
|
|
14
|
+
workflowTables, // @workflows
|
|
12
15
|
} from "@dapplelight/signet-db/columns/sqlite"
|
|
13
|
-
// payments|growth|orgs|privacy:end
|
|
14
|
-
import { auditLog, id, idempotencyKeys, timestamps } from "@dapplelight/signet-db/columns/sqlite" // @!payments|growth|orgs|privacy
|
|
16
|
+
// payments|growth|orgs|privacy|ai|paywalls|workflows:end
|
|
17
|
+
import { auditLog, id, idempotencyKeys, timestamps } from "@dapplelight/signet-db/columns/sqlite" // @!payments|growth|orgs|privacy|ai|paywalls|workflows
|
|
15
18
|
// auth:end
|
|
16
19
|
// !auth:begin
|
|
17
20
|
import { auditLog, growthTables, idempotencyKeys } from "@dapplelight/signet-db/columns/sqlite" // @growth
|
|
@@ -55,6 +58,15 @@ export const audit = auditLog()
|
|
|
55
58
|
export const idempotency = idempotencyKeys()
|
|
56
59
|
|
|
57
60
|
export const { subjectPlans, entitlementOverrides, usageCounters } = entitlementTables() // @payments
|
|
61
|
+
// ai:begin
|
|
62
|
+
export const { usageCounters } = entitlementTables() // @!payments
|
|
63
|
+
/** AI spend caps set at runtime; usage_counters holds the spend. */
|
|
64
|
+
export const spendCaps = aiSpendCaps()
|
|
65
|
+
// ai:end
|
|
66
|
+
/** Versioned offerings and the placements that serve them: the remote paywalls. */ // @paywalls
|
|
67
|
+
export const { offerings, placements } = monetisationTables() // @paywalls
|
|
68
|
+
/** Durable workflow runs, their steps, and the events they wait on. */ // @workflows
|
|
69
|
+
export const { workflowRuns, workflowSteps, workflowEvents } = workflowTables() // @workflows
|
|
58
70
|
export const { waitlist, invites, referralCodes, referrals, marks, feedback } = growthTables() // @growth
|
|
59
71
|
export const { consents, dataRequests } = privacyTables() // @privacy
|
|
60
72
|
// orgs:begin
|
package/base/api/api/env.ts
CHANGED
|
@@ -45,6 +45,14 @@ export const envSchema = z.object({
|
|
|
45
45
|
RAZORPAY_KEY_SECRET: z.string().optional(),
|
|
46
46
|
RAZORPAY_WEBHOOK_SECRET: z.string().optional(),
|
|
47
47
|
// payments:end
|
|
48
|
+
// ai:begin
|
|
49
|
+
/**
|
|
50
|
+
* AI spend caps in cost units (1,000,000 = one US dollar): per user per UTC
|
|
51
|
+
* day, and for the whole app per month. A cap stored in ai_spend_caps wins.
|
|
52
|
+
*/
|
|
53
|
+
AI_CAP_USER_DAY: z.coerce.number().int().min(0).default(200_000),
|
|
54
|
+
AI_CAP_GLOBAL_MONTH: z.coerce.number().int().min(0).default(50_000_000),
|
|
55
|
+
// ai:end
|
|
48
56
|
// mcp:begin
|
|
49
57
|
/** The MCP endpoint at /mcp (api/app.ts). "off" answers it with a 404. */
|
|
50
58
|
MCP: z.enum(["on", "off"]).default("on"),
|
package/base/api/api/server.ts
CHANGED
|
@@ -16,6 +16,7 @@ import { createApp } from "./app.ts"
|
|
|
16
16
|
import { pruneKv, sqlKv } from "./db/kv.ts"
|
|
17
17
|
import { openDb } from "./db/open.ts"
|
|
18
18
|
import { env } from "./env.ts"
|
|
19
|
+
import { createAppWorkflows } from "./workflows.ts" // @workflows
|
|
19
20
|
|
|
20
21
|
const { db, query } = openDb(env.DATABASE_PATH)
|
|
21
22
|
// OBSERVABILITY=on adds request logs, spans and /health/live and /health/ready.
|
|
@@ -23,6 +24,11 @@ const serviceName = "__APP_NAME__"
|
|
|
23
24
|
const observability = env.OBSERVABILITY === "on" ? { serviceName } : undefined
|
|
24
25
|
const app = createApp({ db, query, kv: sqlKv(db.orm), observability })
|
|
25
26
|
setInterval(() => pruneKv(db.orm), 60 * 60 * 1000)
|
|
27
|
+
// workflows:begin
|
|
28
|
+
// The workflow runner (api/workflows.ts): every second it takes the runs that
|
|
29
|
+
// are due, including any a restart cut short. The same on Bun, Deno and Node.
|
|
30
|
+
void createAppWorkflows(db).poll({ intervalMs: 1000 })
|
|
31
|
+
// workflows:end
|
|
26
32
|
|
|
27
33
|
const DIST = join(import.meta.dirname, "../dist") // @web
|
|
28
34
|
|
package/base/api/api/services.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
+
import { createExtractor, fakeProvider } from "@dapplelight/signet-ai" // @ai
|
|
1
2
|
// admin:begin
|
|
2
3
|
import { type AdminContext, type Authorize, defineAdmin, roles } from "@dapplelight/signet-db/admin"
|
|
3
4
|
// admin:end
|
|
5
|
+
import { createAiSpend } from "@dapplelight/signet-db/ai-spend" // @ai
|
|
4
6
|
// payments:begin
|
|
5
7
|
import { createEntitlements } from "@dapplelight/signet-db/entitlements"
|
|
6
8
|
// payments:end
|
|
7
9
|
import { createGrowth } from "@dapplelight/signet-db/growth" // @growth
|
|
10
|
+
import { createOfferings } from "@dapplelight/signet-db/offerings" // @paywalls
|
|
8
11
|
// orgs:begin
|
|
9
12
|
import { createOrgs } from "@dapplelight/signet-db/orgs"
|
|
10
13
|
// orgs:end
|
|
@@ -39,10 +42,11 @@ import { eq } from "drizzle-orm"
|
|
|
39
42
|
import { z } from "zod"
|
|
40
43
|
// notifications:end
|
|
41
44
|
import type { Db } from "./db/index.ts"
|
|
42
|
-
import * as schema from "./db/schema.ts" // @notifications|payments|privacy|growth|orgs|admin
|
|
45
|
+
import * as schema from "./db/schema.ts" // @notifications|payments|privacy|growth|orgs|admin|ai
|
|
43
46
|
import { env } from "./env.ts" // @auth
|
|
47
|
+
import { plans } from "./plans.ts" // @payments
|
|
48
|
+
import { createAppWorkflows } from "./workflows.ts" // @workflows
|
|
44
49
|
// payments:begin
|
|
45
|
-
import { plans } from "./plans.ts"
|
|
46
50
|
|
|
47
51
|
export { PLAN_DISPLAY, type PlanId, plans } from "./plans.ts"
|
|
48
52
|
// payments:end
|
|
@@ -209,6 +213,26 @@ export function createServices({ db, kv }: Deps) { // @!auth
|
|
|
209
213
|
},
|
|
210
214
|
})
|
|
211
215
|
// payments:end
|
|
216
|
+
// ai:begin
|
|
217
|
+
// Spend per user and for the app, per UTC day and month, in cost units
|
|
218
|
+
// (1,000,000 = one US dollar). A cap an admin sets wins over these.
|
|
219
|
+
const aiSpend = createAiSpend(db, {
|
|
220
|
+
tables: { usageCounters: schema.usageCounters, aiSpendCaps: schema.spendCaps },
|
|
221
|
+
caps: { user: { day: env.AI_CAP_USER_DAY }, global: { month: env.AI_CAP_GLOBAL_MONTH } },
|
|
222
|
+
})
|
|
223
|
+
const ai = createExtractor({
|
|
224
|
+
// A canned answer, so /api/ai runs with no key and no network. Put a real
|
|
225
|
+
// provider here (see README, "AI"); its pricing is what the caps count.
|
|
226
|
+
providers: [
|
|
227
|
+
{
|
|
228
|
+
...fakeProvider((input: string) => ({ summary: input.slice(0, 80) })),
|
|
229
|
+
pricing: { perCallUsd: 0.001 },
|
|
230
|
+
},
|
|
231
|
+
],
|
|
232
|
+
// Checks the caps before each call and charges its cost after.
|
|
233
|
+
ledger: aiSpend.ledger,
|
|
234
|
+
})
|
|
235
|
+
// ai:end
|
|
212
236
|
return {
|
|
213
237
|
db,
|
|
214
238
|
orm,
|
|
@@ -234,6 +258,13 @@ export function createServices({ db, kv }: Deps) { // @!auth
|
|
|
234
258
|
}),
|
|
235
259
|
// privacy:end
|
|
236
260
|
growth: createGrowth(db, { tables: schema }), // @growth
|
|
261
|
+
// ai:begin
|
|
262
|
+
aiSpend,
|
|
263
|
+
ai,
|
|
264
|
+
// ai:end
|
|
265
|
+
// Offerings and placements: what each paywall sells (api/routes/paywall.ts). // @paywalls
|
|
266
|
+
offerings: createOfferings(db, { tables: schema, plans }), // @paywalls
|
|
267
|
+
workflows: createAppWorkflows(db), // @workflows
|
|
237
268
|
// orgs:begin
|
|
238
269
|
// Orgs, roles (owner, admin, member) and invites. Add roles with
|
|
239
270
|
// `roles: defineRoles({ ...DEFAULT_ORG_ROLES, ... })`.
|
|
@@ -245,7 +276,16 @@ export function createServices({ db, kv }: Deps) { // @!auth
|
|
|
245
276
|
admin: defineAdmin(schema, {
|
|
246
277
|
authorize: adminAuthorize,
|
|
247
278
|
// Per table by SQL name: search, readonly, hidden columns, row actions.
|
|
248
|
-
tables: {
|
|
279
|
+
tables: {
|
|
280
|
+
kv: false,
|
|
281
|
+
idempotency_keys: false,
|
|
282
|
+
// These change through their own APIs, which keep them consistent: read only here. // @ai|paywalls|workflows
|
|
283
|
+
ai_spend_caps: { ops: ["list", "get"] }, // @ai
|
|
284
|
+
offerings: { ops: ["list", "get"] }, // @paywalls
|
|
285
|
+
placements: { ops: ["list", "get"] }, // @paywalls
|
|
286
|
+
workflow_runs: { ops: ["list", "get"] }, // @workflows
|
|
287
|
+
workflow_steps: { ops: ["list", "get"] }, // @workflows
|
|
288
|
+
},
|
|
249
289
|
}),
|
|
250
290
|
// admin:end
|
|
251
291
|
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
CREATE TABLE `offerings` (
|
|
2
|
+
`id` text PRIMARY KEY NOT NULL,
|
|
3
|
+
`key` text NOT NULL,
|
|
4
|
+
`version` integer NOT NULL,
|
|
5
|
+
`products` text NOT NULL,
|
|
6
|
+
`paywall` text,
|
|
7
|
+
`note` text,
|
|
8
|
+
`created_by` text,
|
|
9
|
+
`created_at` integer DEFAULT (cast((julianday('now') - 2440587.5) * 86400000 as integer)) NOT NULL
|
|
10
|
+
);
|
|
11
|
+
--> statement-breakpoint
|
|
12
|
+
CREATE TABLE `placements` (
|
|
13
|
+
`placement` text PRIMARY KEY NOT NULL,
|
|
14
|
+
`offering` text NOT NULL,
|
|
15
|
+
`version` integer,
|
|
16
|
+
`overrides` text NOT NULL,
|
|
17
|
+
`experiment` text,
|
|
18
|
+
`updated_by` text,
|
|
19
|
+
`updated_at` integer DEFAULT (cast((julianday('now') - 2440587.5) * 86400000 as integer)) NOT NULL
|
|
20
|
+
);
|
|
21
|
+
--> statement-breakpoint
|
|
22
|
+
CREATE TABLE `ai_spend_caps` (
|
|
23
|
+
`id` text PRIMARY KEY NOT NULL,
|
|
24
|
+
`scope` text NOT NULL,
|
|
25
|
+
`subject` text NOT NULL,
|
|
26
|
+
`period` text NOT NULL,
|
|
27
|
+
`units` integer,
|
|
28
|
+
`updated_by` text,
|
|
29
|
+
`updated_at` integer DEFAULT (cast((julianday('now') - 2440587.5) * 86400000 as integer)) NOT NULL
|
|
30
|
+
);
|
|
31
|
+
--> statement-breakpoint
|
|
32
|
+
CREATE TABLE `workflow_events` (
|
|
33
|
+
`id` text PRIMARY KEY NOT NULL,
|
|
34
|
+
`name` text NOT NULL,
|
|
35
|
+
`run_id` text,
|
|
36
|
+
`payload` text,
|
|
37
|
+
`created_at` integer NOT NULL
|
|
38
|
+
);
|
|
39
|
+
--> statement-breakpoint
|
|
40
|
+
CREATE INDEX `workflow_events_name` ON `workflow_events` (`name`,`id`);--> statement-breakpoint
|
|
41
|
+
CREATE TABLE `workflow_runs` (
|
|
42
|
+
`id` text PRIMARY KEY NOT NULL,
|
|
43
|
+
`workflow` text NOT NULL,
|
|
44
|
+
`input` text,
|
|
45
|
+
`status` text NOT NULL,
|
|
46
|
+
`output` text,
|
|
47
|
+
`error` text,
|
|
48
|
+
`created_by` text,
|
|
49
|
+
`wake_at` integer,
|
|
50
|
+
`lease_owner` text,
|
|
51
|
+
`lease_until` integer,
|
|
52
|
+
`waiting_on` text,
|
|
53
|
+
`created_at` integer NOT NULL,
|
|
54
|
+
`updated_at` integer NOT NULL,
|
|
55
|
+
`finished_at` integer
|
|
56
|
+
);
|
|
57
|
+
--> statement-breakpoint
|
|
58
|
+
CREATE INDEX `workflow_runs_due` ON `workflow_runs` (`status`,`wake_at`);--> statement-breakpoint
|
|
59
|
+
CREATE TABLE `workflow_steps` (
|
|
60
|
+
`run_id` text NOT NULL,
|
|
61
|
+
`step_id` text NOT NULL,
|
|
62
|
+
`seq` integer NOT NULL,
|
|
63
|
+
`kind` text NOT NULL,
|
|
64
|
+
`status` text NOT NULL,
|
|
65
|
+
`output` text,
|
|
66
|
+
`error` text,
|
|
67
|
+
`attempts` integer DEFAULT 0 NOT NULL,
|
|
68
|
+
`wake_at` integer,
|
|
69
|
+
`event` text,
|
|
70
|
+
`event_id` text,
|
|
71
|
+
`approvers` text,
|
|
72
|
+
`summary` text,
|
|
73
|
+
`decided_by` text,
|
|
74
|
+
`started_at` integer NOT NULL,
|
|
75
|
+
`finished_at` integer,
|
|
76
|
+
PRIMARY KEY(`run_id`, `step_id`)
|
|
77
|
+
);
|