@treeseed/sdk 0.1.2 → 0.3.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 (237) hide show
  1. package/README.md +97 -506
  2. package/dist/{src/cli-tools.d.ts → cli-tools.d.ts} +1 -1
  3. package/dist/cli-tools.js +5 -3
  4. package/dist/{src/content-store.d.ts → content-store.d.ts} +3 -2
  5. package/dist/content-store.js +52 -20
  6. package/dist/{src/d1-store.d.ts → d1-store.d.ts} +62 -1
  7. package/dist/d1-store.js +625 -65
  8. package/dist/field-aliases.d.ts +11 -0
  9. package/dist/field-aliases.js +41 -0
  10. package/dist/graph/build.d.ts +19 -0
  11. package/dist/graph/build.js +949 -0
  12. package/dist/graph/dsl.d.ts +2 -0
  13. package/dist/graph/dsl.js +243 -0
  14. package/dist/graph/query.d.ts +47 -0
  15. package/dist/graph/query.js +447 -0
  16. package/dist/graph/ranking.d.ts +3 -0
  17. package/dist/graph/ranking.js +483 -0
  18. package/dist/graph/schema.d.ts +142 -0
  19. package/dist/graph/schema.js +133 -0
  20. package/dist/graph.d.ts +52 -0
  21. package/dist/graph.js +133 -0
  22. package/dist/index.d.ts +28 -0
  23. package/dist/index.js +91 -2
  24. package/dist/model-registry.d.ts +8 -0
  25. package/dist/model-registry.js +351 -25
  26. package/dist/operations/providers/default.d.ts +10 -0
  27. package/dist/operations/providers/default.js +514 -0
  28. package/dist/operations/runtime.d.ts +7 -0
  29. package/dist/operations/runtime.js +60 -0
  30. package/dist/operations/services/config-runtime.d.ts +269 -0
  31. package/dist/operations/services/config-runtime.js +1397 -0
  32. package/dist/operations/services/d1-migration.d.ts +6 -0
  33. package/dist/operations/services/d1-migration.js +89 -0
  34. package/dist/operations/services/deploy.d.ts +371 -0
  35. package/dist/operations/services/deploy.js +981 -0
  36. package/dist/operations/services/git-workflow.d.ts +49 -0
  37. package/dist/operations/services/git-workflow.js +218 -0
  38. package/dist/operations/services/github-automation.d.ts +156 -0
  39. package/dist/operations/services/github-automation.js +256 -0
  40. package/dist/operations/services/local-dev.d.ts +9 -0
  41. package/dist/operations/services/local-dev.js +106 -0
  42. package/dist/operations/services/mailpit-runtime.d.ts +4 -0
  43. package/dist/operations/services/mailpit-runtime.js +59 -0
  44. package/dist/operations/services/railway-deploy.d.ts +53 -0
  45. package/dist/operations/services/railway-deploy.js +123 -0
  46. package/dist/operations/services/runtime-paths.d.ts +19 -0
  47. package/dist/operations/services/runtime-paths.js +54 -0
  48. package/dist/operations/services/runtime-tools.d.ts +117 -0
  49. package/dist/operations/services/runtime-tools.js +358 -0
  50. package/dist/operations/services/save-deploy-preflight.d.ts +34 -0
  51. package/dist/operations/services/save-deploy-preflight.js +76 -0
  52. package/dist/operations/services/template-registry.d.ts +88 -0
  53. package/dist/operations/services/template-registry.js +407 -0
  54. package/dist/operations/services/watch-dev.d.ts +21 -0
  55. package/dist/operations/services/watch-dev.js +284 -0
  56. package/dist/operations/services/workspace-preflight.d.ts +40 -0
  57. package/dist/operations/services/workspace-preflight.js +165 -0
  58. package/dist/operations/services/workspace-save.d.ts +42 -0
  59. package/dist/operations/services/workspace-save.js +235 -0
  60. package/dist/operations/services/workspace-tools.d.ts +16 -0
  61. package/dist/operations/services/workspace-tools.js +270 -0
  62. package/dist/operations-registry.d.ts +5 -0
  63. package/dist/operations-registry.js +68 -0
  64. package/dist/operations-types.d.ts +71 -0
  65. package/dist/operations-types.js +17 -0
  66. package/dist/operations.d.ts +6 -0
  67. package/dist/operations.js +16 -0
  68. package/dist/platform/books-data.d.ts +1 -0
  69. package/dist/platform/books-data.js +1 -0
  70. package/dist/platform/contracts.d.ts +158 -0
  71. package/dist/platform/contracts.js +0 -0
  72. package/dist/platform/deploy/config.d.ts +4 -0
  73. package/dist/platform/deploy/config.js +222 -0
  74. package/dist/platform/deploy-config.d.ts +1 -0
  75. package/dist/platform/deploy-config.js +1 -0
  76. package/dist/platform/deploy-runtime.d.ts +18 -0
  77. package/dist/platform/deploy-runtime.js +78 -0
  78. package/dist/platform/env.yaml +394 -0
  79. package/dist/platform/environment.d.ts +130 -0
  80. package/dist/platform/environment.js +331 -0
  81. package/dist/platform/plugin.d.ts +2 -0
  82. package/dist/platform/plugin.js +4 -0
  83. package/dist/platform/plugins/constants.d.ts +22 -0
  84. package/dist/platform/plugins/constants.js +29 -0
  85. package/dist/platform/plugins/plugin.d.ts +51 -0
  86. package/dist/platform/plugins/plugin.js +6 -0
  87. package/dist/platform/plugins/runtime.d.ts +35 -0
  88. package/dist/platform/plugins/runtime.js +161 -0
  89. package/dist/platform/plugins.d.ts +6 -0
  90. package/dist/platform/plugins.js +38 -0
  91. package/dist/platform/site-config-schema.js +1 -0
  92. package/dist/platform/tenant/config.d.ts +9 -0
  93. package/dist/platform/tenant/config.js +154 -0
  94. package/dist/platform/tenant/runtime-config.d.ts +4 -0
  95. package/dist/platform/tenant/runtime-config.js +20 -0
  96. package/dist/platform/tenant-config.d.ts +1 -0
  97. package/dist/platform/tenant-config.js +1 -0
  98. package/dist/platform/utils/books-data.d.ts +29 -0
  99. package/dist/platform/utils/books-data.js +82 -0
  100. package/dist/platform/utils/site-config-schema.js +321 -0
  101. package/dist/remote.d.ts +175 -0
  102. package/dist/remote.js +202 -0
  103. package/dist/runtime.js +35 -22
  104. package/dist/scripts/aggregate-book.js +121 -0
  105. package/dist/scripts/build-dist.js +54 -13
  106. package/dist/scripts/build-tenant-worker.js +36 -0
  107. package/dist/scripts/cleanup-markdown.js +373 -0
  108. package/dist/scripts/cli-test-fixtures.js +48 -0
  109. package/dist/scripts/config-treeseed.js +95 -0
  110. package/dist/scripts/ensure-mailpit.js +29 -0
  111. package/dist/scripts/local-dev.js +129 -0
  112. package/dist/scripts/logs-mailpit.js +2 -0
  113. package/dist/scripts/patch-starlight-content-path.js +172 -0
  114. package/dist/scripts/release-verify.js +34 -6
  115. package/dist/scripts/run-fixture-astro-command.js +18 -0
  116. package/dist/scripts/scaffold-site.js +65 -0
  117. package/dist/scripts/stop-mailpit.js +5 -0
  118. package/dist/scripts/sync-dev-vars.js +6 -0
  119. package/dist/scripts/sync-template.js +20 -0
  120. package/dist/scripts/template-catalog.test.js +100 -0
  121. package/dist/scripts/template-command.js +31 -0
  122. package/dist/scripts/tenant-astro-command.js +3 -0
  123. package/dist/scripts/tenant-build.js +16 -0
  124. package/dist/scripts/tenant-check.js +7 -0
  125. package/dist/scripts/tenant-d1-migrate-local.js +11 -0
  126. package/dist/scripts/tenant-deploy.js +180 -0
  127. package/dist/scripts/tenant-destroy.js +104 -0
  128. package/dist/scripts/tenant-dev.js +171 -0
  129. package/dist/scripts/tenant-lint.js +4 -0
  130. package/dist/scripts/tenant-test.js +4 -0
  131. package/dist/scripts/test-cloudflare-local.js +212 -0
  132. package/dist/scripts/test-scaffold.js +314 -0
  133. package/dist/scripts/test-smoke.js +71 -13
  134. package/dist/scripts/treeseed-assert-release-tag-version.js +21 -0
  135. package/dist/scripts/treeseed-build-dist.js +134 -0
  136. package/dist/scripts/treeseed-publish-package.js +19 -0
  137. package/dist/scripts/treeseed-release-verify.js +131 -0
  138. package/dist/scripts/treeseed-run-ts.js +45 -0
  139. package/dist/scripts/validate-templates.js +6 -0
  140. package/dist/scripts/verify-driver.js +29 -0
  141. package/dist/scripts/workflow-commands.test.js +39 -0
  142. package/dist/scripts/workspace-close.js +24 -0
  143. package/dist/scripts/workspace-command-e2e.js +718 -0
  144. package/dist/scripts/workspace-lint.js +9 -0
  145. package/dist/scripts/workspace-preflight.js +22 -0
  146. package/dist/scripts/workspace-publish-changed-packages.js +16 -0
  147. package/dist/scripts/workspace-release-verify.js +81 -0
  148. package/dist/scripts/workspace-release.js +42 -0
  149. package/dist/scripts/workspace-save.js +124 -0
  150. package/dist/scripts/workspace-start-warning.js +3 -0
  151. package/dist/scripts/workspace-start.js +71 -0
  152. package/dist/scripts/workspace-test-unit.js +4 -0
  153. package/dist/scripts/workspace-test.js +11 -0
  154. package/dist/sdk-fields.d.ts +11 -0
  155. package/dist/sdk-fields.js +169 -0
  156. package/dist/sdk-filters.d.ts +4 -0
  157. package/dist/sdk-filters.js +12 -15
  158. package/dist/sdk-types.d.ts +796 -0
  159. package/dist/sdk-types.js +7 -1
  160. package/dist/sdk-version.d.ts +2 -0
  161. package/dist/sdk-version.js +42 -0
  162. package/dist/sdk.d.ts +215 -0
  163. package/dist/sdk.js +235 -11
  164. package/dist/stores/cursor-store.js +9 -3
  165. package/dist/stores/lease-store.js +8 -2
  166. package/dist/{src/stores → stores}/message-store.d.ts +1 -1
  167. package/dist/stores/message-store.js +27 -3
  168. package/dist/stores/operational-store.d.ts +24 -0
  169. package/dist/stores/operational-store.js +279 -0
  170. package/dist/stores/run-store.js +8 -1
  171. package/dist/stores/subscription-store.js +7 -5
  172. package/dist/template-catalog.d.ts +13 -0
  173. package/dist/template-catalog.js +141 -0
  174. package/dist/treeseed/services/compose.yml +7 -0
  175. package/dist/treeseed/template-catalog/catalog.fixture.json +55 -0
  176. package/dist/treeseed/template-catalog/templates/starter-basic/template/astro.config.d.ts +2 -0
  177. package/dist/treeseed/template-catalog/templates/starter-basic/template/astro.config.ts +3 -0
  178. package/dist/treeseed/template-catalog/templates/starter-basic/template/package.json +32 -0
  179. package/dist/treeseed/template-catalog/templates/starter-basic/template/src/config.yaml +40 -0
  180. package/dist/treeseed/template-catalog/templates/starter-basic/template/src/content/empty/.gitkeep +1 -0
  181. package/dist/treeseed/template-catalog/templates/starter-basic/template/src/content/knowledge/handbook/index.mdx +11 -0
  182. package/dist/treeseed/template-catalog/templates/starter-basic/template/src/content/pages/welcome.mdx +11 -0
  183. package/dist/treeseed/template-catalog/templates/starter-basic/template/src/content.config.d.ts +1 -0
  184. package/dist/treeseed/template-catalog/templates/starter-basic/template/src/content.config.ts +3 -0
  185. package/dist/treeseed/template-catalog/templates/starter-basic/template/src/env.yaml +1 -0
  186. package/dist/treeseed/template-catalog/templates/starter-basic/template/src/manifest.yaml +19 -0
  187. package/dist/treeseed/template-catalog/templates/starter-basic/template/treeseed.site.yaml +26 -0
  188. package/dist/treeseed/template-catalog/templates/starter-basic/template/tsconfig.json +9 -0
  189. package/dist/treeseed/template-catalog/templates/starter-basic/template.config.json +90 -0
  190. package/dist/utils/agents/contracts/messages.d.ts +88 -0
  191. package/dist/utils/agents/contracts/messages.js +138 -0
  192. package/dist/utils/agents/contracts/run.d.ts +20 -0
  193. package/dist/utils/agents/contracts/run.js +0 -0
  194. package/dist/utils/agents/runtime-types.d.ts +117 -0
  195. package/dist/utils/agents/runtime-types.js +4 -0
  196. package/dist/verification.d.ts +20 -0
  197. package/dist/verification.js +98 -0
  198. package/dist/workflow/operations.d.ts +396 -0
  199. package/dist/workflow/operations.js +841 -0
  200. package/dist/workflow-state.d.ts +56 -0
  201. package/dist/workflow-state.js +195 -0
  202. package/dist/workflow-support.d.ts +9 -0
  203. package/dist/workflow-support.js +176 -0
  204. package/dist/workflow.d.ts +111 -0
  205. package/dist/workflow.js +97 -0
  206. package/package.json +111 -5
  207. package/scripts/verify-driver.mjs +29 -0
  208. package/dist/scripts/.ts-run-1775630384291-crtqr3izsa.js +0 -22
  209. package/dist/scripts/.ts-run-1775630388025-vnjle0z75a.js +0 -129
  210. package/dist/scripts/assert-release-tag-version.d.ts +0 -1
  211. package/dist/scripts/build-dist.d.ts +0 -1
  212. package/dist/scripts/fixture-tools.d.ts +0 -5
  213. package/dist/scripts/package-tools.d.ts +0 -15
  214. package/dist/scripts/publish-package.d.ts +0 -1
  215. package/dist/scripts/release-verify.d.ts +0 -1
  216. package/dist/scripts/test-smoke.d.ts +0 -1
  217. package/dist/src/index.d.ts +0 -6
  218. package/dist/src/model-registry.d.ts +0 -4
  219. package/dist/src/sdk-filters.d.ts +0 -4
  220. package/dist/src/sdk-types.d.ts +0 -285
  221. package/dist/src/sdk.d.ts +0 -109
  222. package/dist/test/test-fixture.d.ts +0 -1
  223. package/dist/test/utils/envelopes.test.d.ts +0 -1
  224. package/dist/test/utils/sdk.test.d.ts +0 -1
  225. package/dist/vitest.config.d.ts +0 -2
  226. /package/dist/{src/frontmatter.d.ts → frontmatter.d.ts} +0 -0
  227. /package/dist/{src/git-runtime.d.ts → git-runtime.d.ts} +0 -0
  228. /package/dist/{src/runtime.d.ts → runtime.d.ts} +0 -0
  229. /package/dist/{src/stores → stores}/cursor-store.d.ts +0 -0
  230. /package/dist/{src/stores → stores}/envelopes.d.ts +0 -0
  231. /package/dist/{src/stores → stores}/helpers.d.ts +0 -0
  232. /package/dist/{src/stores → stores}/lease-store.d.ts +0 -0
  233. /package/dist/{src/stores → stores}/run-store.d.ts +0 -0
  234. /package/dist/{src/stores → stores}/subscription-store.d.ts +0 -0
  235. /package/dist/{src/types → types}/agents.d.ts +0 -0
  236. /package/dist/{src/types → types}/cloudflare.d.ts +0 -0
  237. /package/dist/{src/wrangler-d1.d.ts → wrangler-d1.d.ts} +0 -0
package/README.md CHANGED
@@ -1,577 +1,168 @@
1
1
  # `@treeseed/sdk`
2
2
 
3
- `@treeseed/sdk` is the standalone TreeSeed SDK for content-backed and D1-backed object models.
3
+ `@treeseed/sdk` is the main programmatic interface for Treeseed content, control-plane state, and graph-first AI context retrieval.
4
4
 
5
- It exposes the public model and storage surface used by TreeSeed agents and supporting tooling:
5
+ For most consumers, the right entrypoint is `AgentSdk`.
6
6
 
7
- - content-backed access for pages, notes, questions, objectives, people, agents, books, and knowledge
8
- - D1-backed access for subscriptions, messages, agent runs, cursors, and content leases
9
- - stable query and mutation APIs for `get`, `read`, `search`, `follow`, `pick`, `create`, and `update`
7
+ ## Which Surface Should I Use?
10
8
 
11
- ## Consumer Contract
9
+ Treeseed exposes three public SDK surfaces, but they are not peers:
12
10
 
13
- - Node `>=20`
14
- - ESM package
15
- - install from npm as a normal package dependency
16
- - import from the package root or documented subpath exports
11
+ | Surface | Role | Use It For |
12
+ | --- | --- | --- |
13
+ | `AgentSdk` | Primary | application code, workers, scripts, API handlers, graph-first context retrieval |
14
+ | `ScopedAgentSdk` | Operational | agent-runtime code that must enforce model permissions and inject actor identity |
15
+ | `ContentGraphRuntime` | Advanced | low-level graph-only integrations that want the graph engine without the rest of the SDK |
17
16
 
18
- Install:
17
+ If you are unsure, use `AgentSdk`.
19
18
 
20
- ```bash
21
- npm install @treeseed/sdk
22
- ```
23
-
24
- Example:
25
-
26
- ```ts
27
- import { AgentSdk } from '@treeseed/sdk';
28
-
29
- const sdk = new AgentSdk();
30
- ```
19
+ ## Major Capability Groups
31
20
 
32
- ## Using The SDK In Applications
21
+ `AgentSdk` covers five main areas:
33
22
 
34
- `AgentSdk` is the main application entrypoint. It routes each request to either the content-backed store or the D1-backed store based on the model you ask for, and it always returns a JSON envelope with:
23
+ - generic model reads and mutations across content-backed and D1-backed models
24
+ - operational runtime state such as messages, runs, cursors, and leases
25
+ - control-plane orchestration for work days, tasks, task events, graph runs, and reports
26
+ - graph-first context retrieval through `parseGraphDsl()`, `resolveSeeds()`, `queryGraph()`, and `buildContextPack()`
27
+ - agent scoping through `scopeForAgent()`
35
28
 
36
- - `ok`
37
- - `model`
38
- - `operation`
39
- - `payload`
40
- - optional `meta`
29
+ ## Install
41
30
 
42
- For most application code, the working pattern is:
31
+ ```bash
32
+ npm install @treeseed/sdk
33
+ ```
43
34
 
44
- 1. create one SDK instance for your process, request handler, worker, or job
45
- 2. call `get`, `read`, `search`, `follow`, `pick`, `create`, or `update`
46
- 3. read the typed `payload` from the returned envelope
35
+ Consumer contract:
47
36
 
48
- ### Initialize An SDK Instance
37
+ - Node `>=22`
38
+ - ESM package
39
+ - import from the package root or documented subpath exports
49
40
 
50
- Use the default constructor when you want in-memory D1 behavior and a content root resolved from your environment or working directory:
41
+ ## Quickstart
51
42
 
52
43
  ```ts
53
44
  import { AgentSdk } from '@treeseed/sdk';
54
45
 
55
46
  const sdk = new AgentSdk({
56
- repoRoot: '/absolute/path/to/your-site',
47
+ repoRoot: '/absolute/path/to/your-site',
57
48
  });
58
49
  ```
59
50
 
60
- Use `createLocal()` when you want a local Wrangler-backed D1 database:
51
+ Use `AgentSdk.createLocal()` when you want a local Wrangler-backed D1 database:
61
52
 
62
53
  ```ts
63
54
  import { AgentSdk } from '@treeseed/sdk';
64
55
 
65
56
  const sdk = AgentSdk.createLocal({
66
- repoRoot: '/absolute/path/to/your-site',
67
- databaseName: 'treeseed-local',
68
- persistTo: '.wrangler/state/v3/d1',
69
- });
70
- ```
71
-
72
- Use `MemoryAgentDatabase` explicitly in tests or scripts when you want a fully in-memory setup:
73
-
74
- ```ts
75
- import { AgentSdk } from '@treeseed/sdk';
76
- import { MemoryAgentDatabase } from '@treeseed/sdk/d1-store';
77
-
78
- const sdk = new AgentSdk({
79
- repoRoot: '/absolute/path/to/your-site',
80
- database: new MemoryAgentDatabase(),
57
+ repoRoot: '/absolute/path/to/your-site',
58
+ databaseName: 'treeseed-local',
59
+ persistTo: '.wrangler/state/v3/d1',
81
60
  });
82
61
  ```
83
62
 
84
- ### Read A Single Record
85
-
86
- Use `get()` when you want one record by `id`, `slug`, or `key`.
87
-
88
- ```ts
89
- const response = await sdk.get({
90
- model: 'knowledge',
91
- slug: 'guides/getting-started/1-introduction',
92
- });
93
-
94
- if (response.payload) {
95
- console.log(response.payload.title);
96
- console.log(response.payload.body);
97
- }
98
- ```
99
-
100
- Use `read()` when you want the same lookup behavior but want the returned envelope to say `operation: 'read'`.
101
-
102
- ```ts
103
- const response = await sdk.read({
104
- model: 'page',
105
- slug: 'getting-started',
106
- });
107
- ```
63
+ ## Preferred Graph Workflow
108
64
 
109
- ### Search Across A Model
65
+ The preferred graph API for new integrations is:
110
66
 
111
- Use `search()` to list and filter records from a model.
112
-
113
- ```ts
114
- const response = await sdk.search({
115
- model: 'knowledge',
116
- filters: [
117
- { field: 'title', op: 'contains', value: 'TreeSeed' },
118
- { field: 'tags', op: 'contains', value: 'onboarding' },
119
- ],
120
- sort: [{ field: 'updated', direction: 'desc' }],
121
- limit: 10,
122
- });
123
-
124
- console.log(response.meta?.count);
125
- console.log(response.payload.map((item) => item.slug));
126
- ```
127
-
128
- `search()` is the main method for application reads such as:
129
-
130
- - listing recent notes
131
- - finding objectives by status
132
- - finding people by role or affiliation
133
- - finding queued D1 messages by type or status
134
-
135
- ### Follow Changes Since A Timestamp
136
-
137
- Use `follow()` when your application wants records changed since a known point in time.
138
-
139
- ```ts
140
- const response = await sdk.follow({
141
- model: 'knowledge',
142
- since: '2026-04-07T00:00:00.000Z',
143
- filters: [{ field: 'tags', op: 'contains', value: 'treeseed' }],
144
- });
67
+ 1. `parseGraphDsl()`
68
+ 2. `queryGraph()`
69
+ 3. `buildContextPack()`
145
70
 
146
- for (const item of response.payload.items) {
147
- console.log(item.slug);
148
- }
149
- ```
150
-
151
- The payload shape is:
71
+ Example:
152
72
 
153
73
  ```ts
154
- {
155
- items: [...],
156
- since: '...'
157
- }
158
- ```
159
-
160
- ### Pick Work Items
161
-
162
- Use `pick()` when you want the SDK to choose one item from a model for a worker.
163
-
164
- For content-backed models, `pick()` tries to claim a content lease and returns both the selected item and a `leaseToken` when a claim succeeds.
74
+ import { AgentSdk } from '@treeseed/sdk';
165
75
 
166
- ```ts
167
- const response = await sdk.pick({
168
- model: 'objective',
169
- workerId: 'planner-1',
170
- leaseSeconds: 300,
171
- filters: [{ field: 'status', op: 'eq', value: 'in progress' }],
172
- });
76
+ const sdk = new AgentSdk();
77
+ const parsed = await sdk.parseGraphDsl(
78
+ 'ctx "queue api" for implement in /knowledge via implements,references depth 1 budget 4000 as full',
79
+ );
173
80
 
174
- if (response.payload.item) {
175
- console.log(response.payload.item.slug);
176
- console.log(response.payload.leaseToken);
81
+ if (!parsed.ok || !parsed.query) {
82
+ throw new Error(parsed.errors.join('; '));
177
83
  }
178
- ```
179
-
180
- For `message`, `pick()` routes to queue claiming behavior in the D1 layer.
181
-
182
- ### Create Content Or D1 Records
183
-
184
- Use `create()` for models that support creation.
185
-
186
- For content-backed models, pass frontmatter-like fields in `data`. The SDK writes the document and returns the created item plus git metadata.
187
-
188
- ```ts
189
- const response = await sdk.create({
190
- model: 'note',
191
- actor: 'app-server',
192
- data: {
193
- slug: 'operating-a-small-treeseed',
194
- title: 'Operating a Small TreeSeed',
195
- status: 'live',
196
- author: 'TreeSeed Team',
197
- tags: ['operations', 'treeseed'],
198
- body: 'Keep the content model simple and the workflows visible.',
199
- },
200
- });
201
84
 
202
- console.log(response.payload.item.slug);
203
- console.log(response.payload.git);
85
+ const graph = await sdk.queryGraph(parsed.query);
86
+ const pack = await sdk.buildContextPack(parsed.query);
204
87
  ```
205
88
 
206
- For D1-backed models, `create()` delegates to the relevant store and returns the created entity.
207
-
208
- ### Update Existing Records
89
+ The public `ctx` syntax is:
209
90
 
210
- Use `update()` when you want to modify an existing content-backed or D1-backed record.
211
-
212
- ```ts
213
- const response = await sdk.update({
214
- model: 'objective',
215
- slug: 'make-the-sample-site-easy-to-operate',
216
- actor: 'app-server',
217
- data: {
218
- status: 'live',
219
- body: 'The objective is now complete and documented.',
220
- },
221
- });
91
+ ```text
92
+ ctx <target>
93
+ [for <stage>]
94
+ [in <scope>]
95
+ [via <relation[,relation...]>]
96
+ [depth <0-3>]
97
+ [where <filter-expression>]
98
+ [limit <n>]
99
+ [budget <tokens>]
100
+ [as <list|brief|full|map>]
222
101
  ```
223
102
 
224
- For content-backed models, `update()` returns the updated item and git metadata. For D1-backed models, it returns the updated row or `null` when no matching record exists.
103
+ The old `key=value` graph DSL is no longer supported.
225
104
 
226
- ### Work With Messages
105
+ ## Advanced Graph Methods
227
106
 
228
- The SDK exposes dedicated queue helpers in addition to generic model access.
107
+ The SDK also exposes lower-level graph primitives such as:
229
108
 
230
- Create a message:
109
+ - `searchFiles()`
110
+ - `searchSections()`
111
+ - `searchEntities()`
112
+ - `getGraphNode()`
113
+ - `getNeighbors()`
114
+ - `followReferences()`
115
+ - `getBacklinks()`
116
+ - `getRelated()`
117
+ - `getSubgraph()`
118
+ - `refreshGraph()`
231
119
 
232
- ```ts
233
- const created = await sdk.createMessage({
234
- type: 'guidance_ready',
235
- actor: 'guide-agent',
236
- payload: {
237
- slug: 'guides/getting-started/1-introduction',
238
- },
239
- relatedModel: 'knowledge',
240
- relatedId: 'guides/getting-started/1-introduction',
241
- priority: 5,
242
- maxAttempts: 3,
243
- });
244
- ```
245
-
246
- Claim a message:
247
-
248
- ```ts
249
- const claimed = await sdk.claimMessage({
250
- workerId: 'worker-1',
251
- messageTypes: ['guidance_ready'],
252
- leaseSeconds: 300,
253
- });
254
- ```
255
-
256
- Acknowledge a message:
257
-
258
- ```ts
259
- await sdk.ackMessage({
260
- id: 1,
261
- status: 'completed',
262
- });
263
- ```
264
-
265
- ### Record Agent Runs, Cursors, And Leases
266
-
267
- Record a run:
120
+ These remain public, but they are considered advanced tools. Prefer the graph-first context workflow above unless you specifically need raw lexical search or raw traversal primitives.
268
121
 
269
- ```ts
270
- await sdk.recordRun({
271
- run: {
272
- runId: 'run-123',
273
- agentSlug: 'guide-agent',
274
- status: 'completed',
275
- triggerSource: 'message',
276
- startedAt: '2026-04-07T00:00:00.000Z',
277
- finishedAt: '2026-04-07T00:05:00.000Z',
278
- },
279
- });
280
- ```
122
+ ## `ScopedAgentSdk`
281
123
 
282
- Read and update a cursor:
283
-
284
- ```ts
285
- const cursor = await sdk.getCursor({
286
- agentSlug: 'guide-agent',
287
- cursorKey: 'knowledge-sync',
288
- });
289
-
290
- await sdk.upsertCursor({
291
- agentSlug: 'guide-agent',
292
- cursorKey: 'knowledge-sync',
293
- cursorValue: '2026-04-07T00:00:00.000Z',
294
- });
295
- ```
296
-
297
- Release one lease or all leases:
298
-
299
- ```ts
300
- await sdk.releaseLease({
301
- model: 'objective',
302
- itemKey: 'make-the-sample-site-easy-to-operate',
303
- leaseToken: 'lease-token',
304
- });
305
-
306
- await sdk.releaseAllLeases();
307
- ```
308
-
309
- ### How TreeSeed Uses Agent Runs, Cursors, And Leases
310
-
311
- These three concepts are the operational state layer for TreeSeed's agent runtime. They are not general content models like `page` or `knowledge`. Instead, they let TreeSeed coordinate ongoing agent work safely and make that work inspectable after the fact.
312
-
313
- #### Agent Runs
314
-
315
- An `agent_run` is the execution trace for one agent invocation.
316
-
317
- TreeSeed records a run when the agent kernel starts an agent, and records it again when the run finishes or fails. In practice, that means a run captures:
318
-
319
- - which agent ran
320
- - what triggered it
321
- - the current status such as `running`, `completed`, `failed`, or `waiting`
322
- - which message or item was selected
323
- - summary or error output
324
- - optional branch, commit, PR, and changed-path metadata
325
- - start and finish timestamps
326
-
327
- In the TreeSeed agent runtime, [`agent/src/agents/kernel/agent-kernel.ts`](/home/adrian/Projects/treeseed/agent/src/agents/kernel/agent-kernel.ts) calls `sdk.recordRun()` at the beginning of execution and again after the handler returns. That gives TreeSeed a durable per-run audit trail for:
328
-
329
- - debugging agent behavior
330
- - understanding why an agent did or did not run
331
- - inspecting outputs from planner, reviewer, notifier, and similar handlers
332
- - connecting downstream events back to the run that produced them
333
-
334
- Conceptually, `agent_run` is the answer to: "What happened during this agent invocation?"
335
-
336
- #### Agent Cursors
337
-
338
- An `agent_cursor` is a tiny per-agent checkpoint. It stores one named progress marker as:
339
-
340
- - `agentSlug`
341
- - `cursorKey`
342
- - `cursorValue`
343
-
344
- TreeSeed uses cursors to remember where an agent last left off, so the next cycle can resume from the correct point instead of starting over.
345
-
346
- In the runtime, cursors are used in a few concrete ways:
347
-
348
- - the agent kernel writes `last_run_at` after a successful run
349
- - follow triggers read a cursor like `follow:model-a,model-b` to know which timestamp to compare against
350
- - the sample planner agent writes `last_priority_run_at`
351
- - the sample notifier agent reads and updates `last_notified_at` so it only announces new activity
352
-
353
- You can see that usage in:
354
-
355
- - [`agent/src/agents/kernel/agent-kernel.ts`](/home/adrian/Projects/treeseed/agent/src/agents/kernel/agent-kernel.ts)
356
- - [`agent/src/agents/kernel/trigger-resolver.ts`](/home/adrian/Projects/treeseed/agent/src/agents/kernel/trigger-resolver.ts)
357
- - [`planner.ts`](/home/adrian/Projects/treeseed/core/.fixtures/treeseed-fixtures/sites/working-site/src/agents/planner.ts)
358
- - [`notifier.ts`](/home/adrian/Projects/treeseed/core/.fixtures/treeseed-fixtures/sites/working-site/src/agents/notifier.ts)
359
-
360
- Conceptually, `agent_cursor` is the answer to: "Where should this agent continue from next time?"
361
-
362
- #### Content Leases
363
-
364
- A `content_lease` is a short-lived claim on one content item. TreeSeed uses leases to avoid two workers picking and mutating the same item at the same time.
365
-
366
- When `pick()` runs against a content-backed model, the SDK does not just choose the "best" item. It also tries to claim a lease in the database. If another worker already holds a live lease for that item, the claim fails and the picker moves on.
367
-
368
- Each lease stores:
369
-
370
- - the model
371
- - the item key
372
- - who claimed it
373
- - when it was claimed
374
- - when the lease expires
375
- - a lease token
376
-
377
- This is how TreeSeed prevents duplicate work during autonomous or parallel agent execution, especially for content-backed tasks like selecting the next note, question, or objective to act on.
378
-
379
- In the SDK, the lease flow is wired through:
380
-
381
- - content selection in [`sdk/src/content-store.ts`](/home/adrian/Projects/treeseed/sdk/src/content-store.ts)
382
- - D1 lease persistence in [`sdk/src/stores/lease-store.ts`](/home/adrian/Projects/treeseed/sdk/src/stores/lease-store.ts)
383
- - runtime cleanup through `releaseLease()` and `releaseAllLeases()`
384
-
385
- The agent kernel exposes that cleanup path as `releaseLeases()` so TreeSeed operators can clear stale claims when needed.
386
-
387
- Conceptually, `content_lease` is the answer to: "Who currently owns this piece of work, and when does that claim expire?"
388
-
389
- #### How They Work Together
390
-
391
- In TreeSeed, these records solve different parts of the same runtime problem:
392
-
393
- - `agent_run` records what happened
394
- - `agent_cursor` records where to resume
395
- - `content_lease` records who currently owns a piece of work
396
-
397
- Together, they make the agent system:
398
-
399
- - inspectable, because each run leaves a trace
400
- - incremental, because agents can continue from saved cursors
401
- - concurrency-safe, because content picking is guarded by leases
402
-
403
- That combination is what lets TreeSeed move from one-off scripts toward a manageable long-running agent runtime.
404
-
405
- ### Discover Agent Specs
406
-
407
- When your application stores agent definitions in content, use `listRawAgentSpecs()` or `listAgentSpecs()`.
408
-
409
- ```ts
410
- const specs = await sdk.listAgentSpecs({ enabled: true });
411
-
412
- for (const spec of specs) {
413
- console.log(spec.slug, spec.handler);
414
- }
415
- ```
416
-
417
- Use `listRawAgentSpecs()` when you want the underlying content entries. Use `listAgentSpecs()` when you want normalized runtime specs.
418
-
419
- ### Restrict Access With `ScopedAgentSdk`
420
-
421
- Use `scopeForAgent()` when you want application code to enforce an agent’s declared permissions before executing SDK operations.
124
+ Use `scopeForAgent()` when code must enforce an agent’s declared permissions:
422
125
 
423
126
  ```ts
424
127
  const scoped = sdk.scopeForAgent({
425
- slug: 'guide-agent',
426
- permissions: [
427
- { model: 'knowledge', operations: ['get', 'read', 'search'] },
428
- { model: 'message', operations: ['create'] },
429
- ],
430
- });
431
-
432
- await scoped.search({
433
- model: 'knowledge',
434
- filters: [{ field: 'tags', op: 'contains', value: 'treeseed' }],
128
+ slug: 'guide-agent',
129
+ permissions: [
130
+ { model: 'knowledge', operations: ['get', 'read', 'search'] },
131
+ { model: 'message', operations: ['create'] },
132
+ ],
435
133
  });
436
134
  ```
437
135
 
438
- `ScopedAgentSdk` automatically injects the agent slug as `actor` for `create()`, `update()`, and `createMessage()`, and throws when the requested operation is not allowed.
439
-
440
- ### Model And Filter Notes
441
-
442
- The SDK resolves model aliases for you. For example, `docs` maps to `knowledge` and `people` maps to `person`.
443
-
444
- Common request fields:
445
-
446
- - `model`: the canonical model or an accepted alias
447
- - `filters`: array of `{ field, op, value }`
448
- - `sort`: array of `{ field, direction }`
449
- - `limit`: max number of returned items
136
+ `ScopedAgentSdk` is intended for manager/worker and agent-runtime code. It is not the default application entrypoint.
450
137
 
451
- Common filter operators include:
138
+ ## `ContentGraphRuntime`
452
139
 
453
- - `eq`
454
- - `in`
455
- - `contains`
456
- - `prefix`
457
- - `gt`
458
- - `gte`
459
- - `lt`
460
- - `lte`
461
- - `updated_since`
462
- - `related_to`
463
-
464
- ### Envelope Pattern
465
-
466
- Every top-level SDK call returns a consistent envelope:
467
-
468
- ```ts
469
- const response = await sdk.search({ model: 'person', limit: 1 });
470
-
471
- response.ok; // true
472
- response.model; // 'person'
473
- response.operation; // 'search'
474
- response.payload; // typed result
475
- response.meta; // optional metadata
476
- ```
140
+ `ContentGraphRuntime` is still exported, but it is an advanced graph runtime:
477
141
 
478
- That envelope shape makes it straightforward to use the SDK in API handlers, background jobs, CLIs, and agent runtimes without introducing a second application-specific response format.
142
+ - it powers the graph subsystem behind `AgentSdk`
143
+ - it is useful when you want only graph behavior
144
+ - it is not the recommended starting point for most applications
479
145
 
480
- ## Public Surface
146
+ ## Reference Docs
481
147
 
482
- The package root exports the main SDK class, model registry helpers, CLI option helpers, and shared SDK types.
148
+ - [SDK Interface Reference](/home/adrian/Projects/treeseed/market/src/content/knowledge/sdk/interface-reference.mdx)
149
+ - [Graph API Guide](/home/adrian/Projects/treeseed/market/src/content/knowledge/sdk/graph-api-guide.mdx)
150
+ - [ctx Query Language](/home/adrian/Projects/treeseed/market/src/content/knowledge/sdk/ctx-query-language.mdx)
151
+ - [How ctx Works](/home/adrian/Projects/treeseed/market/src/content/knowledge/sdk/ctx-query-engine.mdx)
483
152
 
484
- The package also exposes focused subpaths including:
153
+ ## Other Public Capabilities
485
154
 
486
- - `@treeseed/sdk/sdk`
487
- - `@treeseed/sdk/content-store`
488
- - `@treeseed/sdk/d1-store`
489
- - `@treeseed/sdk/frontmatter`
490
- - `@treeseed/sdk/git-runtime`
491
- - `@treeseed/sdk/models`
492
- - `@treeseed/sdk/sdk-filters`
493
- - `@treeseed/sdk/cli-tools`
494
- - `@treeseed/sdk/types`
495
- - `@treeseed/sdk/types/agents`
496
- - `@treeseed/sdk/types/cloudflare`
497
- - `@treeseed/sdk/wrangler-d1`
498
- - `@treeseed/sdk/stores/*`
155
+ The package also exports:
499
156
 
500
- ## Content Root Resolution
501
-
502
- Content-backed operations need a repository root that contains `src/content`.
503
-
504
- `AgentSdk` resolves that root in this order:
505
-
506
- 1. the explicit `repoRoot` option
507
- 2. `TREESEED_SDK_CONTENT_ROOT`
508
- 3. `TREESEED_SDK_REPO_ROOT`
509
- 4. auto-detection from the current working directory
510
-
511
- For fixture-driven development, the SDK also recognizes the shared submodule fixture at `.fixtures/treeseed-fixtures/sites/working-site`.
512
-
513
- Example with an explicit root:
514
-
515
- ```ts
516
- import { AgentSdk } from '@treeseed/sdk';
517
-
518
- const sdk = new AgentSdk({
519
- repoRoot: '/absolute/path/to/site-or-fixture-root',
520
- });
521
- ```
157
+ - workflow helpers such as `TreeseedWorkflowSdk`
158
+ - gateway and queue clients such as `TreeseedGatewayClient` and `CloudflareQueuePullClient`
159
+ - model registry, field, and filter utilities
160
+ - plugin/runtime types and helpers
522
161
 
523
- ## Local Development
524
-
525
- Initialize the shared fixtures submodule before running fixture-backed tests:
526
-
527
- ```bash
528
- git submodule update --init --recursive
529
- ```
162
+ For package work:
530
163
 
531
164
  ```bash
532
- npm ci
165
+ npm install
533
166
  npm run build
534
167
  npm test
535
- npm run test:smoke
536
- npm run verify
537
- ```
538
-
539
- What each command does:
540
-
541
- - `npm run build`: builds `dist/`
542
- - `npm test`: runs unit tests
543
- - `npm run test:smoke`: packs the SDK tarball and verifies a clean import from the packed install
544
- - `npm run verify`: runs the release verification path used by CI
545
-
546
- ## Sample Fixture Site
547
-
548
- The canonical shared fixture lives in the pinned `treeseed-fixtures` submodule at `.fixtures/treeseed-fixtures/sites/working-site`.
549
-
550
- It serves three purposes at once:
551
-
552
- - a small documentation surface about working with TreeSeed
553
- - the default local test ground for content-backed SDK behavior
554
- - a concrete example of a valid `repoRoot` for `AgentSdk`
555
-
556
- You can point the SDK at it directly:
557
-
558
- ```ts
559
- import path from 'node:path';
560
- import { AgentSdk } from '@treeseed/sdk';
561
-
562
- const sdk = new AgentSdk({
563
- repoRoot: path.resolve('.fixtures/treeseed-fixtures/sites/working-site'),
564
- });
565
168
  ```
566
-
567
- The fixture includes representative entries for pages, notes, questions, objectives, books, knowledge, people, and agents so local queries behave like a small real site instead of a synthetic stub.
568
-
569
- Shared fixture commands:
570
-
571
- ```bash
572
- npm run fixtures:resolve
573
- npm run fixtures:check
574
- ```
575
-
576
- - `fixtures:resolve`: prints the active shared fixture root
577
- - `fixtures:check`: verifies that the pinned shared fixture is initialized and usable
@@ -1,3 +1,3 @@
1
- import { type AgentCliOptions } from './types/agents.ts';
1
+ import type { AgentCliOptions } from './types/agents.ts';
2
2
  export declare function normalizeAgentCliOptions(input: unknown): AgentCliOptions;
3
3
  export declare function buildCopilotAllowToolArgs(allowTools?: AgentCliOptions['allowTools']): string[];