anhedral 0.15.0 → 0.17.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.
Files changed (246) hide show
  1. package/README.md +82 -34
  2. package/assets/stack/ai-schema.ts.txt +5 -0
  3. package/assets/stack/ai.ts.txt +30 -0
  4. package/assets/stack/api-index.ts.txt +15 -0
  5. package/assets/stack/auth-features.ts.txt +4 -0
  6. package/assets/stack/auth-journal.json.txt +13 -0
  7. package/assets/stack/auth-migration.sql.txt +53 -0
  8. package/assets/stack/auth-routes.ts.txt +11 -0
  9. package/assets/stack/auth-schema.ts.txt +64 -0
  10. package/assets/stack/auth-snapshot.json.txt +346 -0
  11. package/assets/stack/auth.ts.txt +32 -0
  12. package/assets/stack/billing-mode.ts.txt +8 -0
  13. package/assets/stack/billing-schema.ts.txt +11 -0
  14. package/assets/stack/desktop.rs.txt +21 -0
  15. package/assets/stack/entitlement-guard.ts.txt +32 -0
  16. package/assets/stack/entitlements.ts.txt +8 -0
  17. package/assets/stack/events-schema.ts.txt +12 -0
  18. package/assets/stack/events.ts.txt +59 -0
  19. package/assets/stack/mobile-auth.ts.txt +25 -0
  20. package/assets/stack/mobile-billing.tsx.txt +63 -0
  21. package/assets/stack/mobile-session.tsx.txt +83 -0
  22. package/assets/stack/native-button.tsx.txt +108 -0
  23. package/assets/stack/native-global.css.txt +4 -0
  24. package/assets/stack/native-lib-utils.ts.txt +5 -0
  25. package/assets/stack/native-metro.config.js.txt +6 -0
  26. package/assets/stack/native-nativewind-env.d.ts.txt +1 -0
  27. package/assets/stack/native-tailwind.config.js.txt +73 -0
  28. package/assets/stack/native-text.tsx.txt +89 -0
  29. package/assets/stack/purchases.ts.txt +65 -0
  30. package/assets/stack/revenuecat.ts.txt +88 -0
  31. package/assets/stack/rtc-schema.ts.txt +10 -0
  32. package/assets/stack/rtc.ts.txt +122 -0
  33. package/assets/stack/security.ts.txt +31 -0
  34. package/assets/stack/storage.ts.txt +36 -0
  35. package/assets/stack/stripe.ts.txt +126 -0
  36. package/assets/stack/web-sign-in.tsx.txt +51 -0
  37. package/assets/ui/LICENSE.react-native-reusables +21 -0
  38. package/assets/ui/UPSTREAM.json +0 -11
  39. package/assets/ui/catalog.json +891 -0
  40. package/assets/ui/extensions/registry.json +6 -0
  41. package/dist/bin.js +7 -12
  42. package/dist/capabilities.d.ts +4 -33
  43. package/dist/capabilities.js +2 -66
  44. package/dist/capability-catalog.d.ts +83 -0
  45. package/dist/capability-catalog.js +121 -0
  46. package/dist/capability-command.d.ts +3 -0
  47. package/dist/capability-command.js +188 -0
  48. package/dist/capability-editor.d.ts +17 -0
  49. package/dist/capability-editor.js +97 -0
  50. package/dist/capability-recipes.d.ts +4 -0
  51. package/dist/capability-recipes.js +520 -0
  52. package/dist/capability-references.d.ts +4 -0
  53. package/dist/capability-references.js +7 -0
  54. package/dist/cli-commands.d.ts +1 -2
  55. package/dist/cli-commands.js +10 -104
  56. package/dist/cli-output.d.ts +1 -2
  57. package/dist/cli-output.js +6 -251
  58. package/dist/command-schema.js +39 -322
  59. package/dist/dependencies.d.ts +29 -9
  60. package/dist/dependencies.js +57 -64
  61. package/dist/index.d.ts +1 -42
  62. package/dist/index.js +1 -27
  63. package/dist/platforms/web.d.ts +1 -1
  64. package/dist/platforms/web.js +27 -75
  65. package/dist/project-config.d.ts +2 -13
  66. package/dist/project-config.js +10 -82
  67. package/dist/registry/options.js +1 -5
  68. package/dist/registry/resolve.d.ts +1 -1
  69. package/dist/registry/resolve.js +4 -10
  70. package/dist/registry/usage.js +3 -9
  71. package/dist/scaffold/backend.js +0 -3
  72. package/dist/scaffold/database.js +2 -1
  73. package/dist/scaffold/documentation.js +56 -25
  74. package/dist/scaffold/stack-documentation.js +1 -1
  75. package/dist/scaffold/storage.d.ts +2 -0
  76. package/dist/scaffold/storage.js +21 -0
  77. package/dist/scaffold/workspace.js +21 -22
  78. package/dist/stack-registry.d.ts +2 -10
  79. package/dist/stack-registry.js +12 -98
  80. package/dist/standard.js +1 -11
  81. package/dist/ui-catalog.d.ts +26 -0
  82. package/dist/ui-catalog.js +31 -0
  83. package/dist/ui-command.d.ts +3 -0
  84. package/dist/ui-command.js +219 -0
  85. package/dist/ui-project.d.ts +22 -0
  86. package/dist/ui-project.js +110 -0
  87. package/dist/version.d.ts +1 -1
  88. package/dist/version.js +1 -1
  89. package/docs/anhedral-stack.md +12 -23
  90. package/docs/assets/anhedral-stack-light.png +0 -0
  91. package/docs/assets/anhedral-stack.png +0 -0
  92. package/docs/capabilities.md +110 -0
  93. package/docs/native-ui.md +47 -0
  94. package/docs/software-factory.md +35 -50
  95. package/docs/stack-standard.md +19 -70
  96. package/docs/ui.md +33 -0
  97. package/docs/verification.md +6 -876
  98. package/package.json +21 -16
  99. package/assets/ui/native/button.tsx +0 -85
  100. package/assets/ui/native/card.tsx +0 -33
  101. package/assets/ui/native/dialog.tsx +0 -75
  102. package/assets/ui/native/input.tsx +0 -27
  103. package/assets/ui/native/label.tsx +0 -4
  104. package/assets/ui/native/layout.tsx +0 -26
  105. package/assets/ui/native/text.tsx +0 -20
  106. package/assets/ui/native/theme-switcher.tsx +0 -22
  107. package/assets/ui/theme-storage.ts +0 -25
  108. package/assets/ui/theme.tsx +0 -66
  109. package/dist/access.d.ts +0 -10
  110. package/dist/access.js +0 -93
  111. package/dist/auth-schema-snapshot.d.ts +0 -346
  112. package/dist/auth-schema-snapshot.js +0 -347
  113. package/dist/auth-schema.d.ts +0 -1
  114. package/dist/auth-schema.js +0 -57
  115. package/dist/capability-contract.d.ts +0 -47
  116. package/dist/capability-contract.js +0 -129
  117. package/dist/catalog-model.d.ts +0 -24
  118. package/dist/catalog-model.js +0 -1
  119. package/dist/catalog-registration.d.ts +0 -16
  120. package/dist/catalog-registration.js +0 -22
  121. package/dist/catalog-snapshot.d.ts +0 -5
  122. package/dist/catalog-snapshot.js +0 -97
  123. package/dist/cli-delivery.d.ts +0 -3
  124. package/dist/cli-delivery.js +0 -112
  125. package/dist/cloudflare.d.ts +0 -25
  126. package/dist/cloudflare.js +0 -226
  127. package/dist/delivery-evidence.d.ts +0 -62
  128. package/dist/delivery-evidence.js +0 -157
  129. package/dist/delivery.d.ts +0 -82
  130. package/dist/delivery.js +0 -489
  131. package/dist/inspector-model.d.ts +0 -123
  132. package/dist/inspector-model.js +0 -1
  133. package/dist/integration-service-readers.d.ts +0 -12
  134. package/dist/integration-service-readers.js +0 -92
  135. package/dist/lifecycle-model.d.ts +0 -15
  136. package/dist/lifecycle-model.js +0 -20
  137. package/dist/lifecycle.d.ts +0 -11
  138. package/dist/lifecycle.js +0 -91
  139. package/dist/management.d.ts +0 -3
  140. package/dist/management.js +0 -47
  141. package/dist/mobile-delivery.d.ts +0 -77
  142. package/dist/mobile-delivery.js +0 -370
  143. package/dist/platforms/mobile.d.ts +0 -2
  144. package/dist/platforms/mobile.js +0 -156
  145. package/dist/project-catalog.d.ts +0 -41
  146. package/dist/project-catalog.js +0 -288
  147. package/dist/project-inspector.d.ts +0 -4
  148. package/dist/project-inspector.js +0 -671
  149. package/dist/project-lifecycle.d.ts +0 -25
  150. package/dist/project-lifecycle.js +0 -57
  151. package/dist/project-state.d.ts +0 -47
  152. package/dist/project-state.js +0 -480
  153. package/dist/provider-auth.d.ts +0 -41
  154. package/dist/provider-auth.js +0 -180
  155. package/dist/provider-readiness.d.ts +0 -30
  156. package/dist/provider-readiness.js +0 -85
  157. package/dist/providers/adapters.d.ts +0 -17
  158. package/dist/providers/adapters.js +0 -103
  159. package/dist/providers/api.d.ts +0 -29
  160. package/dist/providers/api.js +0 -253
  161. package/dist/providers/config.d.ts +0 -15
  162. package/dist/providers/config.js +0 -136
  163. package/dist/providers/definitions.d.ts +0 -115
  164. package/dist/providers/definitions.js +0 -42
  165. package/dist/providers/provision.d.ts +0 -24
  166. package/dist/providers/provision.js +0 -76
  167. package/dist/providers/receipts.d.ts +0 -20
  168. package/dist/providers/receipts.js +0 -56
  169. package/dist/providers/recovery-verification.d.ts +0 -7
  170. package/dist/providers/recovery-verification.js +0 -52
  171. package/dist/providers/recovery.d.ts +0 -30
  172. package/dist/providers/recovery.js +0 -34
  173. package/dist/providers/runtime.d.ts +0 -10
  174. package/dist/providers/runtime.js +0 -234
  175. package/dist/providers/schema.d.ts +0 -28
  176. package/dist/providers/schema.js +0 -49
  177. package/dist/providers/state.d.ts +0 -55
  178. package/dist/providers/state.js +0 -283
  179. package/dist/provision.d.ts +0 -47
  180. package/dist/provision.js +0 -208
  181. package/dist/r2-service-data.d.ts +0 -2
  182. package/dist/r2-service-data.js +0 -136
  183. package/dist/readiness.d.ts +0 -23
  184. package/dist/readiness.js +0 -117
  185. package/dist/registry/add.d.ts +0 -24
  186. package/dist/registry/add.js +0 -210
  187. package/dist/registry/bootstrap.d.ts +0 -3
  188. package/dist/registry/bootstrap.js +0 -5
  189. package/dist/registry/commands.d.ts +0 -3
  190. package/dist/registry/commands.js +0 -83
  191. package/dist/registry/preview.d.ts +0 -11
  192. package/dist/registry/preview.js +0 -91
  193. package/dist/registry/receipt.d.ts +0 -3
  194. package/dist/registry/receipt.js +0 -68
  195. package/dist/repository.d.ts +0 -24
  196. package/dist/repository.js +0 -96
  197. package/dist/scaffold/auth.d.ts +0 -2
  198. package/dist/scaffold/auth.js +0 -76
  199. package/dist/scaffold/billing-config.d.ts +0 -1
  200. package/dist/scaffold/billing-config.js +0 -73
  201. package/dist/scaffold/billing-contracts.d.ts +0 -2
  202. package/dist/scaffold/billing-contracts.js +0 -48
  203. package/dist/scaffold/billing-providers.d.ts +0 -1
  204. package/dist/scaffold/billing-providers.js +0 -249
  205. package/dist/scaffold/billing-schema.d.ts +0 -1
  206. package/dist/scaffold/billing-schema.js +0 -34
  207. package/dist/scaffold/billing-security.d.ts +0 -1
  208. package/dist/scaffold/billing-security.js +0 -46
  209. package/dist/scaffold/billing-server.d.ts +0 -1
  210. package/dist/scaffold/billing-server.js +0 -139
  211. package/dist/scaffold/billing-store.d.ts +0 -1
  212. package/dist/scaffold/billing-store.js +0 -72
  213. package/dist/scaffold/billing.d.ts +0 -2
  214. package/dist/scaffold/billing.js +0 -208
  215. package/dist/scaffold/expo-auth.d.ts +0 -3
  216. package/dist/scaffold/expo-auth.js +0 -203
  217. package/dist/scaffold/expo-billing.d.ts +0 -3
  218. package/dist/scaffold/expo-billing.js +0 -257
  219. package/dist/scaffold/expo-runtime.d.ts +0 -3
  220. package/dist/scaffold/expo-runtime.js +0 -43
  221. package/dist/scaffold/integrations.d.ts +0 -2
  222. package/dist/scaffold/integrations.js +0 -11
  223. package/dist/scaffold/managed-email.d.ts +0 -3
  224. package/dist/scaffold/managed-email.js +0 -47
  225. package/dist/scaffold/native-ui.d.ts +0 -2
  226. package/dist/scaffold/native-ui.js +0 -69
  227. package/dist/scaffold/password-reset.d.ts +0 -1
  228. package/dist/scaffold/password-reset.js +0 -99
  229. package/dist/scaffold/private-files.d.ts +0 -2
  230. package/dist/scaffold/private-files.js +0 -78
  231. package/dist/scaffold/web-billing.d.ts +0 -2
  232. package/dist/scaffold/web-billing.js +0 -48
  233. package/dist/service-deletion.d.ts +0 -29
  234. package/dist/service-deletion.js +0 -195
  235. package/dist/service-descriptor.d.ts +0 -10
  236. package/dist/service-descriptor.js +0 -71
  237. package/dist/service-model.d.ts +0 -49
  238. package/dist/service-model.js +0 -8
  239. package/dist/service-operations.d.ts +0 -29
  240. package/dist/service-operations.js +0 -48
  241. package/dist/service-readers.d.ts +0 -8
  242. package/dist/service-readers.js +0 -216
  243. package/dist/services-command.d.ts +0 -39
  244. package/dist/services-command.js +0 -96
  245. package/dist/ship.d.ts +0 -35
  246. package/dist/ship.js +0 -107
package/README.md CHANGED
@@ -1,55 +1,103 @@
1
1
  # Anhedral CLI
2
2
 
3
- Anhedral CLI is the starter foundation for applications built with Anhedral. It brings web, iOS, Android, backend services, authentication, data storage, and shared design foundations into one maintained application stack. Generated projects contain editable source, established architectural boundaries, and the configuration and development conventions needed to build on that foundation.
3
+ > [!WARNING]
4
+ > **Under construction.** Anhedral CLI is under active development. Commands, generated source and integrations are evolving, and breaking changes are expected. Commands and source-generation details are documented below.
4
5
 
5
- The CLI gives each application a consistent starting point that Anhedral Agents and developers can extend together, carrying a shared architecture from initial setup into ongoing development and cloud delivery.
6
-
7
- Anhedral Platform is the shared workspace for collaboration, planning, and software delivery. Anhedral Agents is the orchestration layer for programming agents, isolated development environments, and source management. Anhedral Cloud is the managed hosting and application services layer. See the [Anhedral product family](https://www.anhedral.com/work) for how they fit together.
8
-
9
- Anhedral is open source under the [Apache License 2.0](LICENSE).
10
- Generated applications are editable source that developers can customize and license for their products.
11
-
12
- One command creates one stack:
13
-
14
- - **Next.js App Router** for the web, with DOM shadcn UI and Cloudflare OpenNext.
15
- - **Expo** for iOS and Android, with the Anhedral-maintained native shadcn port using StyleSheet and shared tokens.
16
- - **Hono** on Cloudflare Workers for application APIs and authorization.
17
- - **Drizzle / D1** for SQL persistence and explicit migrations.
18
- - **Private R2** for authenticated, owner-scoped files.
19
- - Supporting Better Auth sessions, shared public contracts and design tokens.
20
-
21
- The only optional additions are **Stripe** for web billing and **RevenueCat** for native purchases. They can be selected independently or together. Provider accounts, activation, product configuration and acceptance remain explicit work.
6
+ Anhedral CLI scaffolds applications progressively: Next.js on Cloudflare Workers, a Hono API, Drizzle/D1 and Better Auth, then mobile, browser extension and desktop clients, storage, payments, realtime and AI. Each addition writes editable source into your project.
22
7
 
23
8
  <picture>
24
- <source media="(prefers-color-scheme: dark)" srcset="docs/assets/anhedral-stack.png">
25
9
  <source media="(prefers-color-scheme: light)" srcset="docs/assets/anhedral-stack-light.png">
26
- <img alt="Next.js web and Expo iOS/Android connect to Hono, Drizzle/D1 and private R2. Stripe and RevenueCat are optional payment additions." src="docs/assets/anhedral-stack-light.png" width="1200">
10
+ <img alt="Progressive scaffold: Next.js, then Hono, then Drizzle/D1 and Better Auth, with optional storage, services, clients and payments." src="docs/assets/anhedral-stack.png" width="1600">
27
11
  </picture>
28
12
 
29
- CLI 0.15 is a source preview and is not yet published to npm. Build this repository
30
- with `pnpm install --frozen-lockfile && pnpm build`, then use its local CLI.
31
- For the examples below, replace `anhedral` with `node /path/to/anhedral-cli/dist/bin.js`
32
- until the matching version is published.
13
+ ## Start a project
14
+
15
+ Start with a Next.js application configured for OpenNext on Cloudflare Workers, with Anhedral UI included as editable source:
33
16
 
34
17
  ```sh
35
- node /path/to/anhedral-cli/dist/bin.js init my-app
18
+ anhedral init my-app
36
19
  cd my-app
37
20
  pnpm dev
38
21
  ```
39
22
 
40
- Initialization writes source locally, installs dependencies and initializes Git unless requested otherwise. Generation does not create cloud resources, apply remote migrations or deploy.
23
+ Preview initialization with `anhedral init my-app --dry-run --json`, or use `--skip-install --no-git` to handle dependency installation and Git setup yourself.
24
+
25
+ Init creates `apps/web` plus shared tooling and tokens. It includes three reviewed web starter components (Button, Input and Label), official shadcn configuration and Anhedral UI metadata.
26
+
27
+ ## Build the stack progressively
28
+
29
+ Scaffold the API, database, authentication and Expo app as your product grows:
30
+
31
+ ```sh
32
+ anhedral add hono
33
+ anhedral add database-auth
34
+ anhedral add expo native-ui
35
+ ```
36
+
37
+ Or add that complete foundation to a newly initialized project in one step:
38
+
39
+ ```sh
40
+ anhedral add expo native-ui --with-dependencies
41
+ ```
42
+
43
+ Explore additional capabilities and preview their generated source:
44
+
45
+ ```sh
46
+ anhedral stack list
47
+ anhedral add side-panel --with-dependencies --dry-run --json
48
+ ```
49
+
50
+ The core progression is **Next.js → Hono → Drizzle/D1 + Better Auth**. After that, choose the capabilities your application needs:
51
+
52
+ | Add | Scaffolded source and configuration | Requires |
53
+ | --- | --- | --- |
54
+ | `hono` | Hono API Worker, health route and web/API connection | Next.js foundation |
55
+ | `database-auth` | Drizzle schema and migrations, D1 binding, Better Auth and sign-in starter | `hono` |
56
+ | `r2` | Private R2 binding and authenticated, owner-scoped file routes | `database-auth` |
57
+ | `stripe` | Stripe subscription Checkout, signed webhooks and server-side access checks | `database-auth` |
58
+ | `realtime` | Durable events and resumable server-sent event streams | `database-auth` |
59
+ | `rtc` | Cloudflare Realtime SFU session and track API adapter | `database-auth` |
60
+ | `ai` | Workers AI text-generation route, authentication and daily quota | `database-auth` |
61
+ | `expo` | Expo iOS/Android app with Better Auth and SecureStore session integration | `database-auth` |
62
+ | `native-ui` | NativeWind 4, shared theme styling and RNR Button/Text starter for Expo | `expo` |
63
+ | `revenuecat` | Mobile subscription SDK/UI, webhooks and server-side entitlement reconciliation | `expo`, `stripe` |
64
+ | `wxt`, with optional `side-panel` | WXT browser extension; `side-panel` adds Chrome Side Panel API support to render the extension in the browser side panel | `wxt` requires `database-auth`; `side-panel` requires `wxt` |
65
+ | `gpui` | Rust/GPUI native desktop application and build scripts | `database-auth` |
66
+
67
+ Include prerequisites explicitly or use `--with-dependencies` to add them in order. These dependencies define the Anhedral scaffold's composition rules. Additions preserve edited application source.
41
68
 
42
69
  ```sh
43
- anhedral init my-app --dry-run --json
44
- anhedral init my-app --stripe --revenuecat
45
- anhedral add stripe --cwd my-app --dry-run --json
46
- anhedral add stripe --cwd my-app
70
+ anhedral add r2 stripe realtime rtc ai
71
+ anhedral add revenuecat --with-dependencies
72
+ anhedral add wxt side-panel --with-dependencies
73
+ anhedral add gpui --with-dependencies
74
+ anhedral stack status
75
+ anhedral stack doctor
47
76
  ```
48
77
 
49
- Use `--skip-install --no-git` for source-only generation. This mode requires no network. Keep the project CLI version pinned before applying additions; edited-source conflicts are preserved rather than overwritten.
78
+ The CLI generates application source, bindings, configuration examples and migration tooling. Complete service activation with resource provisioning, credentials, reviewed migrations and runtime validation, then deploy the server applications and build/distribute the selected clients. Better Auth methods become usable after you explicitly enable and configure them. The [capability guide](docs/capabilities.md) covers setup and acceptance for each addition. `stack status` and `stack doctor` report the installed source and its completeness.
79
+
80
+ ## Anhedral UI
81
+
82
+ ```sh
83
+ anhedral ui list
84
+ anhedral ui add card --web
85
+ anhedral ui diff card --web
86
+ anhedral ui doctor --web
87
+ ```
88
+
89
+ Anhedral UI is a small catalog and CLI over official shadcn on web and React Native Reusables on native. Components are copied into your application. Native resolution prefers a reviewed Anhedral extension when one exists, then RNR; unsupported components are reported. You own and customize the installed source.
90
+
91
+ For an existing Expo app, complete the upstream React Native Reusables NativeWind 4 setup first, then use `anhedral ui init --native`. Ambiguous workspaces require `--web`, `--native`, `--all` or an application-specific `--cwd`.
92
+
93
+ Install additional components as needed with `anhedral ui add`. Use `ui list` to explore availability, `ui diff` to review changes and `ui doctor` to check setup. Shared theme tokens connect the web and native design language while each platform receives its own component source.
94
+
95
+ ## Documentation
96
+
97
+ See [stack standard](docs/stack-standard.md), [workflow](docs/software-factory.md), [UI commands](docs/ui.md) and [stack additions](docs/capabilities.md).
50
98
 
51
- Deployable applications live in `apps/web`, `apps/mobile` and `apps/api`; shared contracts, database, auth and styling live in `packages`. The API owns data access and authorization. Clients never receive provider management credentials.
99
+ ## Develop the CLI
52
100
 
53
- Read the [stack standard](docs/stack-standard.md), [development and delivery workflow](docs/software-factory.md), [architecture diagram](docs/anhedral-stack.md), and [verification record](docs/verification.md).
101
+ For source development, run `pnpm install --frozen-lockfile && pnpm build`, then `node dist/bin.js init my-app`.
54
102
 
55
- Version 0.15 separates Next.js web from Expo native. Web uses the full App Router with SSR and React Server Components through OpenNext; Expo has no browser build. The focused catalog retains only Stripe and RevenueCat additions. Existing applications are not automatically migrated or reinitialized; retain their pinned older CLI until a reviewed migration is available. Historical versions remain in Git history and release tags.
103
+ Apache-2.0. Generated applications are editable source that consumers may customize and license for their own products. Retain the applicable MIT notices for copied upstream UI source.
@@ -0,0 +1,5 @@
1
+ import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';
2
+ export const aiUsage = sqliteTable('ai_usage', {
3
+ id: text('id').primaryKey(),
4
+ count: integer('count').notNull(),
5
+ });
@@ -0,0 +1,30 @@
1
+ import { Hono } from 'hono';
2
+ import { z } from 'zod';
3
+ import { requireSession } from '../security';
4
+ import type { AppEnv } from '../environment';
5
+ export const aiRoutes = new Hono<AppEnv>();
6
+ const configuration = z.object({
7
+ AI_MODEL: z.string().min(1),
8
+ AI_DAILY_LIMIT: z.coerce.number().int().min(1).max(1000),
9
+ });
10
+ const input = z.object({ prompt: z.string().trim().min(1).max(4000) });
11
+ aiRoutes.post('/ai/generate', requireSession, async (c) => {
12
+ const config = configuration.safeParse(c.env);
13
+ if (!config.success)
14
+ return c.json({ error: 'Choose a model and a daily limit from 1 to 1000' }, 503);
15
+ const body = input.safeParse(await c.req.json().catch(() => null));
16
+ if (!body.success) return c.json({ error: 'Prompt must contain 1–4000 characters' }, 400);
17
+ const key = c.get('userId') + ':' + new Date().toISOString().slice(0, 10);
18
+ const quota = await c.env.DB.prepare(
19
+ 'INSERT INTO ai_usage(id,count) VALUES (?,1) ON CONFLICT(id) DO UPDATE SET count = count + 1 WHERE count < ? RETURNING count',
20
+ )
21
+ .bind(key, config.data.AI_DAILY_LIMIT)
22
+ .first();
23
+ if (!quota) return c.json({ error: 'Daily AI limit reached' }, 429);
24
+ // Failed provider calls consume quota as well; retrying cannot bypass the limit.
25
+ const result = await c.env.AI.run(config.data.AI_MODEL as Parameters<Ai['run']>[0], {
26
+ messages: [{ role: 'user', content: body.data.prompt }],
27
+ max_tokens: 512,
28
+ });
29
+ return c.json({ result });
30
+ });
@@ -0,0 +1,15 @@
1
+ import { Hono } from 'hono';
2
+ import { bodyLimit } from 'hono/body-limit';
3
+ import { capabilities } from './capabilities';
4
+ import type { AppEnv } from './environment';
5
+
6
+ const app = new Hono<AppEnv>();
7
+ app.use('/api/*', bodyLimit({ maxSize: 1024 * 1024 }));
8
+ app.use('/api/*', async (c, next) => {
9
+ await next();
10
+ c.header('Cache-Control', 'private, no-store');
11
+ });
12
+ app.get('/api/health', (c) => c.json({ ok: true }));
13
+ app.route('/api', capabilities);
14
+ app.onError((_error, c) => c.json({ error: 'Application request failed' }, 500));
15
+ export default app;
@@ -0,0 +1,4 @@
1
+ // Enable methods deliberately after configuring credentials, email and policies.
2
+ // OAuth, passkeys, MFA and organizations each need their matching provider/plugin,
3
+ // schema generation, reviewed migrations and client configuration before use.
4
+ export const authFeatures = { emailPassword: false };
@@ -0,0 +1,13 @@
1
+ {
2
+ "version": "7",
3
+ "dialect": "sqlite",
4
+ "entries": [
5
+ {
6
+ "idx": 0,
7
+ "version": "6",
8
+ "when": 1791504000000,
9
+ "tag": "0000_better_auth",
10
+ "breakpoints": true
11
+ }
12
+ ]
13
+ }
@@ -0,0 +1,53 @@
1
+ CREATE TABLE `account` (
2
+ `id` text PRIMARY KEY NOT NULL,
3
+ `account_id` text NOT NULL,
4
+ `provider_id` text NOT NULL,
5
+ `user_id` text NOT NULL,
6
+ `access_token` text,
7
+ `refresh_token` text,
8
+ `id_token` text,
9
+ `access_token_expires_at` integer,
10
+ `refresh_token_expires_at` integer,
11
+ `scope` text,
12
+ `password` text,
13
+ `created_at` integer NOT NULL,
14
+ `updated_at` integer NOT NULL,
15
+ FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
16
+ );
17
+ --> statement-breakpoint
18
+ CREATE INDEX `account_user_id_idx` ON `account` (`user_id`);--> statement-breakpoint
19
+ CREATE TABLE `session` (
20
+ `id` text PRIMARY KEY NOT NULL,
21
+ `expires_at` integer NOT NULL,
22
+ `token` text NOT NULL,
23
+ `created_at` integer NOT NULL,
24
+ `updated_at` integer NOT NULL,
25
+ `ip_address` text,
26
+ `user_agent` text,
27
+ `user_id` text NOT NULL,
28
+ FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
29
+ );
30
+ --> statement-breakpoint
31
+ CREATE UNIQUE INDEX `session_token_unique` ON `session` (`token`);--> statement-breakpoint
32
+ CREATE INDEX `session_user_id_idx` ON `session` (`user_id`);--> statement-breakpoint
33
+ CREATE TABLE `user` (
34
+ `id` text PRIMARY KEY NOT NULL,
35
+ `name` text NOT NULL,
36
+ `email` text NOT NULL,
37
+ `email_verified` integer DEFAULT false NOT NULL,
38
+ `image` text,
39
+ `created_at` integer NOT NULL,
40
+ `updated_at` integer NOT NULL
41
+ );
42
+ --> statement-breakpoint
43
+ CREATE UNIQUE INDEX `user_email_unique` ON `user` (`email`);--> statement-breakpoint
44
+ CREATE TABLE `verification` (
45
+ `id` text PRIMARY KEY NOT NULL,
46
+ `identifier` text NOT NULL,
47
+ `value` text NOT NULL,
48
+ `expires_at` integer NOT NULL,
49
+ `created_at` integer NOT NULL,
50
+ `updated_at` integer NOT NULL
51
+ );
52
+ --> statement-breakpoint
53
+ CREATE INDEX `verification_identifier_idx` ON `verification` (`identifier`);
@@ -0,0 +1,11 @@
1
+ import { Hono } from 'hono';
2
+ import { createAuth, authConfiguration } from '../auth';
3
+ import { requireSession } from '../security';
4
+ import type { AppEnv } from '../environment';
5
+ export const authRoutes = new Hono<AppEnv>();
6
+ authRoutes.on(['GET', 'POST'], '/auth/*', (c) => {
7
+ if (!authConfiguration.safeParse(c.env).success)
8
+ return c.json({ error: 'Authentication needs configuration' }, 503);
9
+ return createAuth(c.env).handler(c.req.raw);
10
+ });
11
+ authRoutes.get('/session', requireSession, (c) => c.json({ userId: c.get('userId') }));
@@ -0,0 +1,64 @@
1
+ import { index, integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';
2
+ const timestamps = () => ({
3
+ createdAt: integer('created_at', { mode: 'timestamp_ms' })
4
+ .notNull()
5
+ .$defaultFn(() => new Date()),
6
+ updatedAt: integer('updated_at', { mode: 'timestamp_ms' })
7
+ .notNull()
8
+ .$defaultFn(() => new Date())
9
+ .$onUpdate(() => new Date()),
10
+ });
11
+ export const user = sqliteTable('user', {
12
+ id: text('id').primaryKey(),
13
+ name: text('name').notNull(),
14
+ email: text('email').notNull().unique(),
15
+ emailVerified: integer('email_verified', { mode: 'boolean' }).notNull().default(false),
16
+ image: text('image'),
17
+ ...timestamps(),
18
+ });
19
+ export const session = sqliteTable(
20
+ 'session',
21
+ {
22
+ id: text('id').primaryKey(),
23
+ expiresAt: integer('expires_at', { mode: 'timestamp_ms' }).notNull(),
24
+ token: text('token').notNull().unique(),
25
+ ...timestamps(),
26
+ ipAddress: text('ip_address'),
27
+ userAgent: text('user_agent'),
28
+ userId: text('user_id')
29
+ .notNull()
30
+ .references(() => user.id, { onDelete: 'cascade' }),
31
+ },
32
+ (t) => [index('session_user_id_idx').on(t.userId)],
33
+ );
34
+ export const account = sqliteTable(
35
+ 'account',
36
+ {
37
+ id: text('id').primaryKey(),
38
+ accountId: text('account_id').notNull(),
39
+ providerId: text('provider_id').notNull(),
40
+ userId: text('user_id')
41
+ .notNull()
42
+ .references(() => user.id, { onDelete: 'cascade' }),
43
+ accessToken: text('access_token'),
44
+ refreshToken: text('refresh_token'),
45
+ idToken: text('id_token'),
46
+ accessTokenExpiresAt: integer('access_token_expires_at', { mode: 'timestamp_ms' }),
47
+ refreshTokenExpiresAt: integer('refresh_token_expires_at', { mode: 'timestamp_ms' }),
48
+ scope: text('scope'),
49
+ password: text('password'),
50
+ ...timestamps(),
51
+ },
52
+ (t) => [index('account_user_id_idx').on(t.userId)],
53
+ );
54
+ export const verification = sqliteTable(
55
+ 'verification',
56
+ {
57
+ id: text('id').primaryKey(),
58
+ identifier: text('identifier').notNull(),
59
+ value: text('value').notNull(),
60
+ expiresAt: integer('expires_at', { mode: 'timestamp_ms' }).notNull(),
61
+ ...timestamps(),
62
+ },
63
+ (t) => [index('verification_identifier_idx').on(t.identifier)],
64
+ );