@ekairos/domain 1.22.15-beta.feature-thread-unify.0 → 1.22.16-beta.development.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 (62) hide show
  1. package/README.md +123 -128
  2. package/dist/cli/bin.d.ts +9 -0
  3. package/dist/cli/bin.d.ts.map +1 -0
  4. package/dist/cli/bin.js +488 -0
  5. package/dist/cli/bin.js.map +1 -0
  6. package/dist/cli/client-runtime.d.ts +25 -0
  7. package/dist/cli/client-runtime.d.ts.map +1 -0
  8. package/dist/cli/client-runtime.js +60 -0
  9. package/dist/cli/client-runtime.js.map +1 -0
  10. package/dist/cli/config.d.ts +5 -0
  11. package/dist/cli/config.d.ts.map +1 -0
  12. package/dist/cli/config.js +44 -0
  13. package/dist/cli/config.js.map +1 -0
  14. package/dist/cli/create-app.d.ts +33 -0
  15. package/dist/cli/create-app.d.ts.map +1 -0
  16. package/dist/cli/create-app.js +1317 -0
  17. package/dist/cli/create-app.js.map +1 -0
  18. package/dist/cli/http.d.ts +28 -0
  19. package/dist/cli/http.d.ts.map +1 -0
  20. package/dist/cli/http.js +117 -0
  21. package/dist/cli/http.js.map +1 -0
  22. package/dist/cli/index.d.ts +8 -0
  23. package/dist/cli/index.d.ts.map +1 -0
  24. package/dist/cli/index.js +7 -0
  25. package/dist/cli/index.js.map +1 -0
  26. package/dist/cli/server.d.ts +3 -0
  27. package/dist/cli/server.d.ts.map +1 -0
  28. package/dist/cli/server.js +437 -0
  29. package/dist/cli/server.js.map +1 -0
  30. package/dist/cli/types.d.ts +60 -0
  31. package/dist/cli/types.d.ts.map +1 -0
  32. package/dist/cli/types.js +2 -0
  33. package/dist/cli/types.js.map +1 -0
  34. package/dist/cli/ui.d.ts +3 -0
  35. package/dist/cli/ui.d.ts.map +1 -0
  36. package/dist/cli/ui.js +136 -0
  37. package/dist/cli/ui.js.map +1 -0
  38. package/dist/index.d.ts +72 -12
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +128 -19
  41. package/dist/index.js.map +1 -1
  42. package/dist/next.d.ts +21 -21
  43. package/dist/next.d.ts.map +1 -1
  44. package/dist/next.js +212 -345
  45. package/dist/next.js.map +1 -1
  46. package/dist/polyfills/dom-events.d.ts +2 -0
  47. package/dist/polyfills/dom-events.d.ts.map +1 -0
  48. package/dist/polyfills/dom-events.js +92 -0
  49. package/dist/polyfills/dom-events.js.map +1 -0
  50. package/dist/runtime-handle.d.ts +34 -0
  51. package/dist/runtime-handle.d.ts.map +1 -0
  52. package/dist/runtime-handle.js +82 -0
  53. package/dist/runtime-handle.js.map +1 -0
  54. package/dist/runtime-step.d.ts +52 -0
  55. package/dist/runtime-step.d.ts.map +1 -0
  56. package/dist/runtime-step.js +116 -0
  57. package/dist/runtime-step.js.map +1 -0
  58. package/dist/runtime.d.ts +3 -2
  59. package/dist/runtime.d.ts.map +1 -1
  60. package/dist/runtime.js +72 -19
  61. package/dist/runtime.js.map +1 -1
  62. package/package.json +42 -4
package/README.md CHANGED
@@ -1,173 +1,168 @@
1
1
  # @ekairos/domain
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/@ekairos/domain)](https://www.npmjs.com/package/@ekairos/domain)
4
- [![npm downloads](https://img.shields.io/npm/dm/@ekairos/domain)](https://www.npmjs.com/package/@ekairos/domain)
3
+ Build one app graph from many bounded contexts.
4
+ Read it through InstantDB.
5
+ Write through step-safe domain actions.
6
+ Operate it through a CLI.
5
7
 
6
- Domain-first TypeScript primitives for InstantDB applications.
8
+ ## What you get
7
9
 
8
- `@ekairos/domain` gives you one source of truth for:
10
+ - Composed domain graphs with `domain(...).schema(...)`
11
+ - Explicit runtimes with `EkairosRuntime`
12
+ - Step-safe write boundaries with `defineDomainAction(...)`
13
+ - Workflow-ready action execution with `executeRuntimeAction(...)`
14
+ - A built-in CLI for `create-app`, `inspect`, `action`, and `query`
9
15
 
10
- - Schema composition.
11
- - Runtime resolution.
12
- - Typed domain actions.
13
- - AI-ready domain context.
16
+ ## Start Fast
14
17
 
15
- If `@ekairos/thread` is execution, `@ekairos/domain` is system truth.
16
-
17
- ## Install
18
+ Scaffold a Next app that already exposes the Ekairos domain endpoint:
18
19
 
19
20
  ```bash
20
- pnpm add @ekairos/domain @instantdb/core
21
- pnpm add -D @instantdb/admin
21
+ npx @ekairos/domain create-app my-app --next
22
22
  ```
23
23
 
24
- Exports:
25
-
26
- - `@ekairos/domain`
27
- - `@ekairos/domain/runtime`
28
- - `@ekairos/domain/next`
29
-
30
- ## Quick start
31
-
32
- ### 1. Define a domain schema
33
-
34
- ```ts
35
- import { domain } from "@ekairos/domain";
36
- import { i } from "@instantdb/core";
24
+ If you already have an Instant platform token, provision the app and write `.env.local` in one pass:
37
25
 
38
- export const managementDomain = domain("management").schema({
39
- entities: {
40
- management_tasks: i.entity({
41
- title: i.string(),
42
- status: i.string().indexed(),
43
- createdAt: i.date().indexed(),
44
- }),
45
- },
46
- links: {},
47
- rooms: {},
48
- });
26
+ ```bash
27
+ npx @ekairos/domain create-app my-app --next --instantToken=$INSTANT_PERSONAL_ACCESS_TOKEN
49
28
  ```
50
29
 
51
- ### 2. Add typed actions
52
-
53
- ```ts
54
- import { defineDomainAction } from "@ekairos/domain";
55
-
56
- const createTask = defineDomainAction<
57
- { orgId: string; actorId: string },
58
- { title: string },
59
- { ok: true; taskId: string },
60
- { db: any }
61
- >({
62
- name: "management.task.create",
63
- description: "Create a management task",
64
- async execute({ env, input, runtime }) {
65
- const title = String(input.title ?? "").trim();
66
- if (!title) throw new Error("title_required");
67
-
68
- // use runtime.db here
69
- return { ok: true, taskId: `task_${env.orgId}` };
70
- },
71
- });
30
+ Then run it and inspect it:
72
31
 
73
- export const appDomain = managementDomain.actions([createTask]);
32
+ ```bash
33
+ npx @ekairos/domain inspect --baseUrl=http://localhost:3000 --admin --pretty
34
+ npx @ekairos/domain seedDemo --baseUrl=http://localhost:3000 --admin --pretty
35
+ npx @ekairos/domain query "{ app_tasks: { comments: {} } }" --baseUrl=http://localhost:3000 --admin --pretty
74
36
  ```
75
37
 
76
- ### 3. Configure runtime once
77
-
78
- ```ts
79
- import { configureRuntime } from "@ekairos/domain/runtime";
80
- import { appDomain } from "./domain";
81
-
82
- configureRuntime({
83
- domain: { domain: appDomain },
84
- runtime: async (env) => {
85
- // Resolve DB by env (org, actor, tenant, etc.)
86
- return { db: { orgId: env.orgId } };
87
- },
88
- });
89
- ```
38
+ ## Next.js Route
90
39
 
91
- ### 4. Execute actions
40
+ New Next.js apps expose the domain adapter explicitly:
92
41
 
93
42
  ```ts
94
- import { executeRuntimeAction } from "@ekairos/domain/runtime";
43
+ // src/app/api/ekairos/domain/route.ts
44
+ import { createRuntimeRouteHandler } from "@ekairos/domain/next";
45
+ import { createRuntime } from "@/runtime";
95
46
 
96
- const output = await executeRuntimeAction({
97
- action: "management.task.create",
98
- env: { orgId: "org_123", actorId: "user_1" },
99
- input: { title: "Ship domain action runtime" },
47
+ export const { GET, POST } = createRuntimeRouteHandler({
48
+ createRuntime,
100
49
  });
101
50
  ```
102
51
 
103
- ## Action model
104
-
105
- Actions are explicit and portable:
106
-
107
- - Domain package owns contracts.
108
- - Adapters own environment (`env`).
109
- - Runtime is resolved per action domain.
110
- - Nested action calls are supported with cycle protection.
52
+ This replaces the old `withRuntime(...)` pattern.
53
+ There is no Next config patching and no generated `.well-known` domain route in new apps.
111
54
 
112
- This keeps Web/API/MCP integrations aligned without duplicating mutation logic.
55
+ Your `next.config.ts` should stay focused on Workflow:
113
56
 
114
- ## Domain context for AI
57
+ ```ts
58
+ import type { NextConfig } from "next";
59
+ import { withWorkflow } from "workflow/next";
115
60
 
116
- Every domain can produce structured context:
61
+ const nextConfig: NextConfig = {
62
+ transpilePackages: ["@ekairos/domain"],
63
+ };
117
64
 
118
- - `domain.context()` returns a machine-friendly context object.
119
- - `domain.contextString()` returns a prompt-friendly string.
65
+ export default withWorkflow(nextConfig) as NextConfig;
66
+ ```
120
67
 
121
- Use this to ground coding agents and domain assistants with current entities, links, docs, and subdomains.
68
+ The CLI uses `/api/ekairos/domain` by default and falls back to the legacy
69
+ `/.well-known/ekairos/v1/domain` endpoint for older apps.
122
70
 
123
- ## Adapter pattern (recommended)
71
+ ## Core Pattern
124
72
 
125
- ### Web/API
73
+ ```ts
74
+ import { defineDomainAction, domain } from "@ekairos/domain";
75
+ import { EkairosRuntime } from "@ekairos/domain/runtime-handle";
76
+ import { executeRuntimeAction } from "@ekairos/domain/runtime";
77
+ import { init } from "@instantdb/admin";
78
+ import { i } from "@instantdb/core";
126
79
 
127
- - UI calls API route.
128
- - API route resolves auth and builds `env`.
129
- - API route executes typed domain action.
80
+ const baseDomain = domain("tasks").schema({
81
+ entities: {
82
+ tasks: i.entity({
83
+ title: i.string().indexed(),
84
+ status: i.string().indexed(),
85
+ }),
86
+ },
87
+ links: {},
88
+ rooms: {},
89
+ });
130
90
 
131
- ### MCP
91
+ export const createTaskAction = defineDomainAction({
92
+ name: "tasks.create",
93
+ async execute({ runtime, input }) {
94
+ "use step";
95
+ const scoped = await runtime.use(appDomain);
96
+ // transact...
97
+ return { ok: true };
98
+ },
99
+ });
132
100
 
133
- - Expose one MCP tool per action (or a thin action dispatcher).
134
- - Resolve transport auth to `env`.
135
- - Execute the same domain actions used by Web/API.
101
+ export const appDomain = baseDomain.actions({
102
+ createTask: createTaskAction,
103
+ });
136
104
 
137
- ## Design rules
105
+ export class AppRuntime extends EkairosRuntime<{
106
+ appId?: string;
107
+ adminToken?: string;
108
+ }, typeof appDomain, any> {
109
+ protected getDomain() {
110
+ return appDomain;
111
+ }
112
+
113
+ protected async resolveDb(env: { appId?: string; adminToken?: string }) {
114
+ return init({
115
+ appId: env.appId!,
116
+ adminToken: env.adminToken!,
117
+ schema: appDomain.toInstantSchema(),
118
+ useDateObjects: true,
119
+ } as any);
120
+ }
121
+ }
122
+
123
+ export function createRuntime(env = {}) {
124
+ return new AppRuntime(env);
125
+ }
126
+
127
+ export async function runWorkflow() {
128
+ "use workflow";
129
+ const runtime = createRuntime({
130
+ appId: process.env.NEXT_PUBLIC_INSTANT_APP_ID,
131
+ adminToken: process.env.INSTANT_ADMIN_TOKEN,
132
+ });
133
+
134
+ return await executeRuntimeAction({
135
+ runtime,
136
+ action: createTaskAction,
137
+ input: { title: "Ship it" },
138
+ });
139
+ }
140
+ ```
138
141
 
139
- - Domain defines truth.
140
- - Runtime is explicit.
141
- - Actions are explicit.
142
- - `env` is adapter-defined and opaque to the package.
143
- - Included subdomains do not auto-export actions.
142
+ ## Rules Of Thumb
144
143
 
145
- ## Purity and immutability guarantees
144
+ - Read directly from the composed schema when no invariant is involved.
145
+ - Put every meaningful write behind an action.
146
+ - Keep action bodies `"use step"`.
147
+ - Keep workflow orchestration above actions.
148
+ - Use `DOMAIN.md` plus `domain.contextString()` when an AI agent needs the model explained.
146
149
 
147
- - `toInstantSchema()` is pure and idempotent for the same domain result.
148
- - Domain schema results are immutable at runtime.
149
- - `.actions(...)` is persistent: it returns a new domain result and never mutates the original one.
150
- - Duplicate link attributes (`entity->label`) are rejected before schema materialization.
150
+ ## CLI Input Quality Of Life
151
151
 
152
- ## Testing
152
+ The CLI accepts JSON5, `@file`, and stdin:
153
153
 
154
154
  ```bash
155
- pnpm run test:unit # runtime and action behavior
156
- pnpm run test:e2e # InstantDB temp app flow (requires INSTANT_CLI_AUTH_TOKEN)
155
+ npx @ekairos/domain query "{ tasks: { $: { limit: 5 } } }" --admin
156
+ npx @ekairos/domain query @query.json5 --admin
157
+ cat query.json5 | npx @ekairos/domain query - --admin
157
158
  ```
158
159
 
159
- In the monorepo release flow, required checks run before publishing:
160
+ Add `--meta` when you need to know whether a query used the local client runtime path or the server route.
161
+
162
+ ## Tests
160
163
 
161
164
  ```bash
162
- pnpm run release:required-checks
165
+ pnpm --filter @ekairos/domain test
166
+ pnpm --filter @ekairos/domain test:cli
167
+ pnpm --filter @ekairos/domain test:workflow
163
168
  ```
164
-
165
- ## Links
166
-
167
- - npm: https://www.npmjs.com/package/@ekairos/domain
168
- - source: https://github.com/e-kairos/ekairos/tree/main/packages/domain
169
- - issues: https://github.com/e-kairos/ekairos/issues
170
-
171
- ## License
172
-
173
- MIT
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env node
2
+ import { stdout as output } from "node:process";
3
+ type CliContext = {
4
+ stdout: Pick<typeof output, "write">;
5
+ stderr: Pick<typeof output, "write">;
6
+ };
7
+ export declare function runCli(argv: string[], ctx?: CliContext): Promise<1 | 0>;
8
+ export {};
9
+ //# sourceMappingURL=bin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bin.d.ts","sourceRoot":"","sources":["../../src/cli/bin.ts"],"names":[],"mappings":";AAEA,OAAO,EAAkB,MAAM,IAAI,MAAM,EAAE,MAAM,cAAc,CAAA;AAgB/D,KAAK,UAAU,GAAG;IAChB,MAAM,EAAE,IAAI,CAAC,OAAO,MAAM,EAAE,OAAO,CAAC,CAAA;IACpC,MAAM,EAAE,IAAI,CAAC,OAAO,MAAM,EAAE,OAAO,CAAC,CAAA;CACrC,CAAA;AAkiBD,wBAAsB,MAAM,CAC1B,IAAI,EAAE,MAAM,EAAE,EACd,GAAG,GAAE,UAA+C,kBAkDrD"}