khotan-data 0.0.1 → 0.1.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.
Files changed (55) hide show
  1. package/AGENTS.md +54 -0
  2. package/README.md +117 -1
  3. package/dist/cli.js +2869 -0
  4. package/dist/factory.cjs +3303 -0
  5. package/dist/factory.cjs.map +1 -0
  6. package/dist/factory.d.cts +662 -0
  7. package/dist/factory.d.ts +662 -0
  8. package/dist/factory.js +3292 -0
  9. package/dist/factory.js.map +1 -0
  10. package/dist/plug-client.cjs +99 -0
  11. package/dist/plug-client.cjs.map +1 -0
  12. package/dist/plug-client.d.cts +71 -0
  13. package/dist/plug-client.d.ts +71 -0
  14. package/dist/plug-client.js +96 -0
  15. package/dist/plug-client.js.map +1 -0
  16. package/dist/templates/agent-skill.md +73 -0
  17. package/dist/templates/agents.md +41 -0
  18. package/dist/templates/cache.example.ts +11 -0
  19. package/dist/templates/cache.ts +58 -0
  20. package/dist/templates/catch.example.ts +36 -0
  21. package/dist/templates/catch.ts +119 -0
  22. package/dist/templates/config-page.tsx +20 -0
  23. package/dist/templates/debug-index-page.tsx +101 -0
  24. package/dist/templates/debug-page.tsx +48 -0
  25. package/dist/templates/graph-page.tsx +11 -0
  26. package/dist/templates/hub.tsx +450 -0
  27. package/dist/templates/inflow.example.ts +61 -0
  28. package/dist/templates/inflow.ts +98 -0
  29. package/dist/templates/khotan-config.ts +49 -0
  30. package/dist/templates/khotan-route.ts +13 -0
  31. package/dist/templates/logs-page.tsx +9 -0
  32. package/dist/templates/logs.tsx +20 -0
  33. package/dist/templates/mapping-browser.tsx +761 -0
  34. package/dist/templates/mappings-page.tsx +9 -0
  35. package/dist/templates/outflow.example.ts +52 -0
  36. package/dist/templates/outflow.ts +90 -0
  37. package/dist/templates/pass.example.ts +51 -0
  38. package/dist/templates/pass.ts +134 -0
  39. package/dist/templates/plug-debugger.tsx +1185 -0
  40. package/dist/templates/plug.example.ts +93 -0
  41. package/dist/templates/plug.ts +806 -0
  42. package/dist/templates/relay.example.ts +71 -0
  43. package/dist/templates/relay.ts +104 -0
  44. package/dist/templates/runs-table.tsx +592 -0
  45. package/dist/templates/schema.ts +505 -0
  46. package/dist/templates/skill-dashboard.md +144 -0
  47. package/dist/templates/skill-plug.md +216 -0
  48. package/dist/templates/skill-setup.md +161 -0
  49. package/dist/templates/skill-webhook.md +196 -0
  50. package/dist/templates/topology-canvas.tsx +1406 -0
  51. package/dist/templates/var-panel.tsx +276 -0
  52. package/dist/templates/webhook-events-table.tsx +241 -0
  53. package/dist/templates/wire-panel.tsx +216 -0
  54. package/dist/templates/wire.ts +155 -0
  55. package/package.json +46 -5
package/AGENTS.md ADDED
@@ -0,0 +1,54 @@
1
+ # khotan-data
2
+
3
+ Production-grade data sync, ETL, and webhook components for Next.js + Drizzle + Postgres.
4
+
5
+ ## Agent Skills
6
+
7
+ This package ships agent skills that teach coding agents how to use khotan-data.
8
+ Skills are installed into consumer projects via the CLI:
9
+
10
+ ```bash
11
+ npx khotan init --yes # Installs all skills during setup
12
+ npx khotan add skill-setup # Or install individually
13
+ ```
14
+
15
+ The installer auto-detects which coding agents are present (Cursor, Claude Code, Codex, Copilot, Kiro, Roo) and installs to all detected agent directories. It also places an `AGENTS.md` router at the project root.
16
+
17
+ ## Skill Templates
18
+
19
+ Source templates live in `src/cli/templates/`:
20
+
21
+ | Template | Skill name | Teaches |
22
+ |----------|-----------|---------|
23
+ | `skill-setup.md` | khotan-setup | Project initialization, factory config, database setup |
24
+ | `skill-plug.md` | khotan-plug | Plug authoring, auth strategies, typed endpoints |
25
+ | `skill-dashboard.md` | khotan-dashboard | Hub dashboard, plug debugger UI |
26
+ | `skill-webhook.md` | khotan-webhook | Wires, Catch, Pass, webhook flow |
27
+ | `agent-skill.md` | khotan-probe | Probe CLI for debugging plugs |
28
+
29
+ ## Agent Detection
30
+
31
+ `src/cli/agent-detect.ts` handles multi-agent installation:
32
+
33
+ | Agent | Marker directory | Skill path |
34
+ |-------|-----------------|------------|
35
+ | Cursor | `.cursor/` | `.cursor/skills/{name}/SKILL.md` |
36
+ | Claude Code | `.claude/` | `.claude/skills/{name}/SKILL.md` |
37
+ | Codex | `.agents/` | `.agents/skills/{name}/SKILL.md` |
38
+ | Copilot | `.github/` | `.github/skills/{name}/SKILL.md` |
39
+ | Kiro | `.kiro/` | `.kiro/skills/{name}/SKILL.md` |
40
+ | Roo | `.roo/` | `.roo/rules/{name}/SKILL.md` |
41
+
42
+ If no agent directories are detected, defaults to Cursor + Claude Code.
43
+
44
+ ## Package Architecture
45
+
46
+ See `openspec/` for full specifications. Key source files:
47
+
48
+ | File | Purpose |
49
+ |------|---------|
50
+ | `src/factory.ts` | Runtime core — plug registration, API routing, debug endpoints |
51
+ | `src/cli/` | CLI commands (init, add, generate, migrate, probe) |
52
+ | `src/cli/registry.ts` | Component/block registry |
53
+ | `src/cli/agent-detect.ts` | Agent detection + multi-path skill installer |
54
+ | `src/cli/compare.ts` | Schema inference + diff engine for probe --compare |
package/README.md CHANGED
@@ -7,11 +7,127 @@ Built for **Next.js + Drizzle + Postgres** projects. Think better-auth for data
7
7
  ## Install
8
8
 
9
9
  ```bash
10
- npm install khotan-data
10
+ npm i khotan-data
11
11
  ```
12
12
 
13
13
  Requires `drizzle-orm` as a peer dependency (you almost certainly already have it).
14
14
 
15
+ ## CLI
16
+
17
+ Scaffold components into your Next.js + Drizzle project:
18
+
19
+ ```bash
20
+ # Initialize khotan config
21
+ npx khotan init
22
+
23
+ # Full setup (drizzle + shadcn + config in one go)
24
+ npx khotan init --full
25
+
26
+ # Add components (reusable building blocks — never create pages)
27
+ npx khotan add schema # Drizzle table definitions (plugs, flows, runs, resources, mappings)
28
+ npx khotan add cache # Durable key/value caches for workflows and relays
29
+ npx khotan add plug # Fetch wrapper with auth, retry, pagination
30
+ npx khotan add inflow # Workflow-backed flow for pulling data in
31
+ npx khotan add outflow # Workflow-backed flow for pushing data out
32
+ npx khotan add relay # Workflow-backed flow for moving data between plugs
33
+ npx khotan add hub # Dashboard UI + API route + config (requires shadcn)
34
+
35
+ # Add blocks (sample pages composed from components)
36
+ npx khotan add config-page-1 # /config page that renders the KhotanHub dashboard
37
+
38
+ # Options
39
+ npx khotan add schema --force # Overwrite existing files
40
+ npx khotan add hub --yes # Auto-accept dependency install prompts
41
+ ```
42
+
43
+ ## Factory (Runtime Engine)
44
+
45
+ Register plugs, caches, flows, and resources — the factory upserts them on boot and serves a REST API:
46
+
47
+ ```typescript
48
+ import { khotan, drizzleAdapter, toNextJsHandler } from "khotan-data/factory";
49
+ import { db } from "@/db";
50
+ import { shopifyPlug } from "@/lib/khotan/plugs/shopify";
51
+ import { shopifyProductsInflow } from "@/lib/khotan/flows/shopify-products";
52
+ import { shopifyProductsSnapshotCache } from "@/lib/khotan/caches/shopify-products-snapshot";
53
+
54
+ const khotanData = khotan({
55
+ adapter: drizzleAdapter(db),
56
+ resources: [
57
+ { name: "products", mapping: { connectField: "sku" } },
58
+ ],
59
+ caches: [
60
+ shopifyProductsSnapshotCache,
61
+ ],
62
+ plugs: [
63
+ {
64
+ name: "shopify",
65
+ plug: shopifyPlug,
66
+ flows: [
67
+ shopifyProductsInflow,
68
+ ],
69
+ },
70
+ ],
71
+ });
72
+
73
+ // Next.js App Router: app/api/khotan/[...all]/route.ts
74
+ export const { GET, POST, PUT, DELETE } = toNextJsHandler(khotanData.handler);
75
+
76
+ // Start a flow through Khotan so run tracking + Workflow IDs are recorded
77
+ await khotanData.flow("products-inflow", { plugName: "shopify" }).start({
78
+ runType: "delta",
79
+ });
80
+ ```
81
+
82
+ ## Caches
83
+
84
+ Use first-class caches when a flow, relay, catch, or pass needs durable state between runs.
85
+
86
+ ```typescript
87
+ import { cache } from "@/lib/khotan/caches/cache";
88
+
89
+ export const shopifyProductsSnapshotCache = cache({
90
+ name: "shopify-products-snapshot",
91
+ scope: {
92
+ plug: "shopify",
93
+ resource: "products",
94
+ flow: "shopify-products-inflow",
95
+ },
96
+ ttl: "6h",
97
+ });
98
+ ```
99
+
100
+ Inside workflows, use `khotanCache(ctx, "name")` for snapshots, cursors, and dedupe markers:
101
+
102
+ ```typescript
103
+ import { khotanCache } from "khotan-data/factory";
104
+
105
+ async function shopifyProductsWorkflow(ctx: InflowContext) {
106
+ "use workflow";
107
+
108
+ async function syncProducts() {
109
+ "use step";
110
+ const snapshotCache = khotanCache(ctx, "shopify-products-snapshot");
111
+ const previous =
112
+ (await snapshotCache.get<Array<Record<string, unknown>>>("latest")) ?? [];
113
+
114
+ const response = await shopifyPlug.get<{ data?: Array<Record<string, unknown>> }>("/products");
115
+ const records = Array.isArray(response.data) ? response.data : [];
116
+
117
+ await snapshotCache.set("latest", records);
118
+
119
+ return {
120
+ extracted: records.length,
121
+ transformed: records.length,
122
+ created: records.length,
123
+ metadata: { previousCount: previous.length },
124
+ };
125
+ }
126
+
127
+ return syncProducts();
128
+ }
129
+ ```
130
+
15
131
  ## Quick Start
16
132
 
17
133
  ```typescript