@m6d/cortex-server 1.7.0 → 2.0.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 (239) hide show
  1. package/README.md +8 -3
  2. package/contracts/README.md +23 -0
  3. package/contracts/graph/embed.ts +50 -0
  4. package/{src → contracts}/graph/helpers.ts +4 -10
  5. package/{src → contracts}/graph/neo4j.ts +14 -27
  6. package/contracts/graph/schema.ts +65 -0
  7. package/{src → contracts}/graph/types.ts +2 -29
  8. package/contracts/graph.ts +36 -0
  9. package/contracts/runtime.ts +208 -0
  10. package/contracts/wire.ts +143 -0
  11. package/dist/contracts/graph/embed.d.ts +22 -0
  12. package/dist/{src → contracts}/graph/helpers.d.ts +14 -12
  13. package/dist/{src → contracts}/graph/neo4j.d.ts +5 -5
  14. package/dist/contracts/graph/schema.d.ts +62 -0
  15. package/dist/{src → contracts}/graph/types.d.ts +1 -1
  16. package/dist/contracts/graph.d.ts +14 -0
  17. package/dist/contracts/runtime.d.ts +278 -0
  18. package/dist/contracts/wire.d.ts +120 -0
  19. package/dist/index.d.ts +1 -1
  20. package/dist/src/lib/adapters/database/index.d.ts +61 -0
  21. package/dist/src/lib/adapters/database/message-content.d.ts +30 -0
  22. package/dist/src/lib/adapters/database/mssql/attachments.d.ts +100 -0
  23. package/dist/src/lib/adapters/database/mssql/client.d.ts +8 -0
  24. package/dist/src/lib/adapters/database/mssql/index.d.ts +194 -0
  25. package/dist/src/lib/adapters/database/mssql/llm-requests.d.ts +17 -0
  26. package/dist/src/lib/adapters/database/mssql/messages.d.ts +37 -0
  27. package/dist/src/lib/adapters/database/mssql/threads.d.ts +50 -0
  28. package/dist/src/lib/adapters/database/postgres/attachments.d.ts +113 -0
  29. package/dist/src/lib/adapters/database/postgres/client.d.ts +8 -0
  30. package/dist/src/lib/adapters/database/postgres/index.d.ts +207 -0
  31. package/dist/src/lib/adapters/database/postgres/llm-requests.d.ts +17 -0
  32. package/dist/src/lib/adapters/database/postgres/messages.d.ts +37 -0
  33. package/dist/src/lib/adapters/database/postgres/threads.d.ts +50 -0
  34. package/{src/adapters/storage.ts → dist/src/lib/adapters/storage/index.d.ts} +1 -1
  35. package/dist/src/{adapters → lib/adapters/storage}/minio.d.ts +1 -1
  36. package/dist/src/lib/ai/active-runs.d.ts +63 -0
  37. package/dist/src/lib/ai/attachments.d.ts +30 -0
  38. package/dist/src/lib/ai/backend-url.d.ts +16 -0
  39. package/dist/src/lib/ai/cc-runtime.d.ts +61 -0
  40. package/dist/src/lib/ai/commit-gate.d.ts +21 -0
  41. package/dist/src/{ai → lib/ai}/context/builder.d.ts +6 -7
  42. package/dist/src/lib/ai/context/compression-middleware.d.ts +15 -0
  43. package/dist/src/lib/ai/context/compressor.d.ts +7 -0
  44. package/dist/src/lib/ai/context/intra-turn-compressor.d.ts +24 -0
  45. package/dist/src/lib/ai/context/optimize-thread-context.d.ts +9 -0
  46. package/dist/src/lib/ai/context/summarizer.d.ts +5 -0
  47. package/dist/src/{ai → lib/ai}/context/token-estimator.d.ts +5 -8
  48. package/dist/src/{ai → lib/ai}/context/types.d.ts +2 -0
  49. package/dist/src/{ai → lib/ai}/fetch.d.ts +1 -1
  50. package/dist/src/lib/ai/finish-turn.d.ts +23 -0
  51. package/dist/src/lib/ai/helpers.d.ts +38 -0
  52. package/dist/src/lib/ai/index.d.ts +7 -0
  53. package/dist/src/lib/ai/inspector.d.ts +41 -0
  54. package/dist/src/lib/ai/interceptors/request-interceptor.d.ts +13 -0
  55. package/dist/src/{ai → lib/ai}/prompt.d.ts +5 -4
  56. package/dist/src/lib/ai/redis-runs.d.ts +41 -0
  57. package/dist/src/lib/ai/redis-stream-log.d.ts +39 -0
  58. package/dist/src/lib/ai/tools/execute-code.tool.d.ts +18 -0
  59. package/dist/src/lib/ai/tools/query-graph.tool.d.ts +13 -0
  60. package/dist/src/lib/ai/tools/read-attachment.tool.d.ts +21 -0
  61. package/dist/src/lib/ai/tools/search-common.d.ts +17 -0
  62. package/dist/src/lib/ai/tools/search-knowledge.tool.d.ts +15 -0
  63. package/dist/src/lib/ai/tools/search-services.tool.d.ts +12 -0
  64. package/dist/src/lib/ai/tools/search-tools.tool.d.ts +12 -0
  65. package/dist/src/lib/ai/turn-tools.d.ts +39 -0
  66. package/dist/src/lib/ai/vision/rasterize-pdf.d.ts +26 -0
  67. package/dist/src/lib/ai/vision/rasterize-pdf.worker.d.ts +1 -0
  68. package/dist/src/lib/ai/vision/vision-reader.d.ts +45 -0
  69. package/dist/src/lib/cc/client.d.ts +183 -0
  70. package/dist/src/lib/cc/config-cache.d.ts +20 -0
  71. package/dist/src/lib/cc/format.d.ts +23 -0
  72. package/dist/src/lib/cc/registry.d.ts +34 -0
  73. package/dist/src/lib/cc/types.d.ts +27 -0
  74. package/dist/src/lib/config.d.ts +170 -0
  75. package/dist/src/lib/db/drizzle.config.mssql.d.ts +2 -0
  76. package/dist/src/lib/db/drizzle.config.pg.d.ts +2 -0
  77. package/dist/src/lib/db/migrate.d.ts +7 -0
  78. package/dist/src/{db/schema.d.ts → lib/db/schema.mssql.d.ts} +236 -24
  79. package/dist/src/lib/db/schema.pg.d.ts +546 -0
  80. package/dist/src/{factory.d.ts → lib/factory.d.ts} +2 -12
  81. package/dist/src/lib/graph/index.d.ts +10 -0
  82. package/dist/src/{graph → lib/graph}/resolver.d.ts +2 -3
  83. package/dist/src/{index.d.ts → lib/index.d.ts} +3 -4
  84. package/dist/src/lib/redis.d.ts +19 -0
  85. package/dist/src/{routes → lib/routes}/chat.d.ts +1 -1
  86. package/dist/src/{routes → lib/routes}/files.d.ts +1 -1
  87. package/dist/src/lib/routes/owned-thread.d.ts +17 -0
  88. package/dist/src/{routes → lib/routes}/threads.d.ts +1 -1
  89. package/dist/src/{routes → lib/routes}/ws.d.ts +1 -1
  90. package/dist/src/lib/types.d.ts +58 -0
  91. package/dist/src/{ws → lib/ws}/connections.d.ts +2 -1
  92. package/index.ts +1 -1
  93. package/package.json +35 -15
  94. package/src/lib/adapters/database/index.ts +78 -0
  95. package/src/lib/adapters/database/message-content.ts +39 -0
  96. package/src/lib/adapters/database/mssql/attachments.ts +90 -0
  97. package/src/lib/adapters/database/mssql/client.ts +11 -0
  98. package/src/lib/adapters/database/mssql/index.ts +23 -0
  99. package/src/lib/adapters/database/mssql/llm-requests.ts +43 -0
  100. package/src/lib/adapters/database/mssql/messages.ts +92 -0
  101. package/src/lib/adapters/database/mssql/threads.ts +83 -0
  102. package/src/lib/adapters/database/postgres/attachments.ts +88 -0
  103. package/src/lib/adapters/database/postgres/client.ts +11 -0
  104. package/src/lib/adapters/database/postgres/index.ts +23 -0
  105. package/src/lib/adapters/database/postgres/llm-requests.ts +45 -0
  106. package/src/lib/adapters/database/postgres/messages.ts +92 -0
  107. package/src/lib/adapters/database/postgres/threads.ts +84 -0
  108. package/{dist/src/adapters/storage.d.ts → src/lib/adapters/storage/index.ts} +1 -1
  109. package/src/{adapters → lib/adapters/storage}/minio.ts +7 -6
  110. package/src/lib/ai/active-runs.ts +157 -0
  111. package/src/lib/ai/attachments.ts +144 -0
  112. package/src/lib/ai/backend-url.ts +27 -0
  113. package/src/lib/ai/cc-runtime.ts +63 -0
  114. package/src/lib/ai/commit-gate.ts +116 -0
  115. package/src/{ai → lib/ai}/context/builder.ts +12 -14
  116. package/src/lib/ai/context/compression-middleware.ts +44 -0
  117. package/src/lib/ai/context/compressor.ts +267 -0
  118. package/src/lib/ai/context/intra-turn-compressor.ts +181 -0
  119. package/src/lib/ai/context/optimize-thread-context.ts +50 -0
  120. package/src/lib/ai/context/summarizer.ts +48 -0
  121. package/src/lib/ai/context/token-estimator.ts +65 -0
  122. package/src/{ai → lib/ai}/context/types.ts +4 -1
  123. package/src/{ai → lib/ai}/fetch.ts +10 -5
  124. package/src/lib/ai/finish-turn.ts +75 -0
  125. package/src/lib/ai/helpers.ts +100 -0
  126. package/src/lib/ai/index.ts +408 -0
  127. package/src/lib/ai/inspector.ts +112 -0
  128. package/src/lib/ai/interceptors/request-interceptor.ts +89 -0
  129. package/src/lib/ai/prompt.ts +355 -0
  130. package/src/lib/ai/redis-runs.ts +167 -0
  131. package/src/lib/ai/redis-stream-log.ts +219 -0
  132. package/src/lib/ai/tools/execute-code.tool.ts +267 -0
  133. package/src/{ai → lib/ai}/tools/query-graph.tool.ts +7 -8
  134. package/src/lib/ai/tools/read-attachment.tool.ts +59 -0
  135. package/src/lib/ai/tools/search-common.ts +15 -0
  136. package/src/lib/ai/tools/search-knowledge.tool.ts +33 -0
  137. package/src/lib/ai/tools/search-services.tool.ts +31 -0
  138. package/src/lib/ai/tools/search-tools.tool.ts +29 -0
  139. package/src/lib/ai/turn-tools.ts +154 -0
  140. package/src/lib/ai/vision/rasterize-pdf.ts +69 -0
  141. package/src/lib/ai/vision/rasterize-pdf.worker.ts +81 -0
  142. package/src/lib/ai/vision/vision-reader.ts +130 -0
  143. package/src/{auth → lib/auth}/middleware.ts +12 -3
  144. package/src/lib/cc/client.ts +273 -0
  145. package/src/lib/cc/config-cache.ts +32 -0
  146. package/src/lib/cc/format.ts +96 -0
  147. package/src/lib/cc/registry.ts +62 -0
  148. package/src/lib/cc/types.ts +44 -0
  149. package/src/lib/config.ts +212 -0
  150. package/src/lib/db/drizzle.config.mssql.ts +9 -0
  151. package/src/lib/db/drizzle.config.pg.ts +9 -0
  152. package/src/lib/db/migrate.ts +56 -0
  153. package/src/{db/migrations/20260309012148_cloudy_maria_hill → lib/db/migrations/mssql/20260326231647_nice_speedball}/migration.sql +12 -0
  154. package/src/{db/migrations/20260309012148_cloudy_maria_hill → lib/db/migrations/mssql/20260326231647_nice_speedball}/snapshot.json +106 -1
  155. package/src/lib/db/migrations/mssql/20260729092726_talented_wilson_fisk/migration.sql +20 -0
  156. package/src/lib/db/migrations/mssql/20260729092726_talented_wilson_fisk/snapshot.json +497 -0
  157. package/src/lib/db/migrations/mssql/20260801003652_perpetual_blue_shield/migration.sql +1 -0
  158. package/src/lib/db/migrations/mssql/20260801003652_perpetual_blue_shield/snapshot.json +516 -0
  159. package/src/lib/db/migrations/mssql/20260812174642_user_id_text/migration.sql +2 -0
  160. package/src/lib/db/migrations/mssql/20260812174642_user_id_text/snapshot.json +516 -0
  161. package/src/lib/db/migrations/pg/20260801084117_thick_ben_grimm/migration.sql +53 -0
  162. package/src/lib/db/migrations/pg/20260801084117_thick_ben_grimm/snapshot.json +584 -0
  163. package/src/lib/db/migrations/pg/20260812174622_user_id_text/migration.sql +2 -0
  164. package/src/lib/db/migrations/pg/20260812174622_user_id_text/snapshot.json +584 -0
  165. package/src/lib/db/schema.mssql.ts +94 -0
  166. package/src/lib/db/schema.pg.ts +95 -0
  167. package/src/lib/factory.ts +127 -0
  168. package/src/lib/graph/index.ts +19 -0
  169. package/src/lib/graph/resolver.ts +387 -0
  170. package/src/{index.ts → lib/index.ts} +13 -5
  171. package/src/lib/redis.ts +44 -0
  172. package/src/lib/routes/chat.ts +115 -0
  173. package/src/lib/routes/files.ts +129 -0
  174. package/src/lib/routes/owned-thread.ts +17 -0
  175. package/src/{routes → lib/routes}/threads.ts +22 -31
  176. package/src/{routes → lib/routes}/ws.ts +9 -3
  177. package/src/lib/types.ts +76 -0
  178. package/src/{ws → lib/ws}/connections.ts +9 -1
  179. package/tsconfig.json +48 -0
  180. package/dist/src/adapters/database.d.ts +0 -30
  181. package/dist/src/adapters/mssql.d.ts +0 -3
  182. package/dist/src/ai/active-streams.d.ts +0 -14
  183. package/dist/src/ai/context/compressor.d.ts +0 -7
  184. package/dist/src/ai/context/index.d.ts +0 -15
  185. package/dist/src/ai/context/summarizer.d.ts +0 -5
  186. package/dist/src/ai/helpers.d.ts +0 -5
  187. package/dist/src/ai/index.d.ts +0 -4
  188. package/dist/src/ai/interceptors/request-interceptor.d.ts +0 -12
  189. package/dist/src/ai/tools/call-endpoint.tool.d.ts +0 -7
  190. package/dist/src/ai/tools/capture-files.tool.d.ts +0 -6
  191. package/dist/src/ai/tools/execute-code.tool.d.ts +0 -4
  192. package/dist/src/ai/tools/query-graph.tool.d.ts +0 -5
  193. package/dist/src/cli/extract-endpoints.d.ts +0 -6
  194. package/dist/src/config.d.ts +0 -165
  195. package/dist/src/db/migrate.d.ts +0 -1
  196. package/dist/src/graph/expand-domains.d.ts +0 -2
  197. package/dist/src/graph/generate-cypher.d.ts +0 -22
  198. package/dist/src/graph/index.d.ts +0 -11
  199. package/dist/src/graph/seed.d.ts +0 -19
  200. package/dist/src/graph/validate.d.ts +0 -2
  201. package/dist/src/routes/index.d.ts +0 -4
  202. package/dist/src/types.d.ts +0 -74
  203. package/dist/src/ws/events.d.ts +0 -39
  204. package/dist/src/ws/index.d.ts +0 -3
  205. package/dist/src/ws/notify.d.ts +0 -2
  206. package/src/adapters/database.ts +0 -29
  207. package/src/adapters/mssql.ts +0 -195
  208. package/src/ai/active-streams.ts +0 -123
  209. package/src/ai/context/compressor.ts +0 -47
  210. package/src/ai/context/index.ts +0 -75
  211. package/src/ai/context/summarizer.ts +0 -50
  212. package/src/ai/context/token-estimator.ts +0 -60
  213. package/src/ai/helpers.ts +0 -32
  214. package/src/ai/index.ts +0 -277
  215. package/src/ai/interceptors/request-interceptor.ts +0 -61
  216. package/src/ai/prompt.ts +0 -126
  217. package/src/ai/tools/call-endpoint.tool.ts +0 -89
  218. package/src/ai/tools/capture-files.tool.ts +0 -20
  219. package/src/ai/tools/execute-code.tool.ts +0 -153
  220. package/src/cli/extract-endpoints.ts +0 -550
  221. package/src/config.ts +0 -182
  222. package/src/db/migrate.ts +0 -21
  223. package/src/db/migrations/20260315000000_add_context_meta/migration.sql +0 -1
  224. package/src/db/schema.ts +0 -70
  225. package/src/factory.ts +0 -171
  226. package/src/graph/expand-domains.ts +0 -276
  227. package/src/graph/generate-cypher.ts +0 -192
  228. package/src/graph/index.ts +0 -47
  229. package/src/graph/resolver.ts +0 -357
  230. package/src/graph/seed.ts +0 -162
  231. package/src/graph/validate.ts +0 -78
  232. package/src/routes/chat.ts +0 -86
  233. package/src/routes/files.ts +0 -100
  234. package/src/routes/index.ts +0 -4
  235. package/src/types.ts +0 -74
  236. package/src/ws/events.ts +0 -39
  237. package/src/ws/index.ts +0 -11
  238. package/src/ws/notify.ts +0 -9
  239. /package/dist/src/{auth → lib/auth}/middleware.d.ts +0 -0
@@ -1,276 +0,0 @@
1
- import type { ConceptDef, DomainDef, EndpointDef, RuleDef, ServiceDef } from "./types.ts";
2
-
3
- type DomainCollections = {
4
- concepts: ConceptDef[];
5
- endpoints: EndpointDef[];
6
- services: ServiceDef[];
7
- rules: RuleDef[];
8
- };
9
-
10
- type OwnershipMaps = {
11
- concepts: WeakMap<ConceptDef, string>;
12
- endpoints: WeakMap<EndpointDef, string>;
13
- services: WeakMap<ServiceDef, string>;
14
- rules: WeakMap<RuleDef, string>;
15
- };
16
-
17
- export function expandDomains(domains: Record<string, DomainDef>) {
18
- const ownership = createOwnershipMaps(domains);
19
- const expanded: Record<string, DomainDef> = {};
20
-
21
- for (const [domainKey, domain] of Object.entries(domains)) {
22
- const collections = cloneCollections(domain);
23
- const pendingConcepts = [...collections.concepts];
24
- const pendingEndpoints = [...collections.endpoints];
25
- const pendingServices = [...collections.services];
26
-
27
- while (
28
- pendingConcepts.length > 0 ||
29
- pendingEndpoints.length > 0 ||
30
- pendingServices.length > 0
31
- ) {
32
- const concept = pendingConcepts.pop();
33
- if (concept) {
34
- includeConcept({
35
- domainKey,
36
- concept,
37
- ownership,
38
- collections,
39
- pendingConcepts,
40
- pendingEndpoints,
41
- });
42
- }
43
-
44
- const endpoint = pendingEndpoints.pop();
45
- if (endpoint) {
46
- includeEndpoint({
47
- domainKey,
48
- endpoint,
49
- ownership,
50
- collections,
51
- pendingConcepts,
52
- pendingEndpoints,
53
- });
54
- }
55
-
56
- const service = pendingServices.pop();
57
- if (service) {
58
- includeService({
59
- domainKey,
60
- service,
61
- ownership,
62
- collections,
63
- pendingConcepts,
64
- });
65
- }
66
- }
67
-
68
- expanded[domainKey] = {
69
- ...domain,
70
- concepts: collections.concepts,
71
- endpoints: collections.endpoints,
72
- services: collections.services,
73
- rules: collections.rules,
74
- };
75
- }
76
-
77
- return expanded;
78
- }
79
-
80
- function createOwnershipMaps(domains: Record<string, DomainDef>) {
81
- const ownership: OwnershipMaps = {
82
- concepts: new WeakMap<ConceptDef, string>(),
83
- endpoints: new WeakMap<EndpointDef, string>(),
84
- services: new WeakMap<ServiceDef, string>(),
85
- rules: new WeakMap<RuleDef, string>(),
86
- };
87
-
88
- for (const [domainKey, domain] of Object.entries(domains)) {
89
- for (const concept of domain.concepts ?? []) {
90
- assignOwner(ownership.concepts, concept, domainKey);
91
- }
92
-
93
- for (const endpoint of domain.endpoints ?? []) {
94
- assignOwner(ownership.endpoints, endpoint, domainKey);
95
- }
96
-
97
- for (const service of domain.services ?? []) {
98
- assignOwner(ownership.services, service, domainKey);
99
- }
100
-
101
- for (const rule of domain.rules ?? []) {
102
- assignOwner(ownership.rules, rule, domainKey);
103
- }
104
- }
105
-
106
- return ownership;
107
- }
108
-
109
- function assignOwner<T extends object>(owners: WeakMap<T, string>, value: T, domainKey: string) {
110
- if (!owners.has(value)) {
111
- owners.set(value, domainKey);
112
- }
113
- }
114
-
115
- function cloneCollections(domain: DomainDef) {
116
- return {
117
- concepts: [...(domain.concepts ?? [])],
118
- endpoints: [...(domain.endpoints ?? [])],
119
- services: [...(domain.services ?? [])],
120
- rules: [...(domain.rules ?? [])],
121
- } satisfies DomainCollections;
122
- }
123
-
124
- function includeConcept(options: {
125
- domainKey: string;
126
- concept: ConceptDef;
127
- ownership: OwnershipMaps;
128
- collections: DomainCollections;
129
- pendingConcepts: ConceptDef[];
130
- pendingEndpoints: EndpointDef[];
131
- }) {
132
- const { domainKey, concept, ownership, collections, pendingConcepts, pendingEndpoints } =
133
- options;
134
-
135
- includeRule(domainKey, concept.governedBy ?? [], ownership, collections);
136
-
137
- if (shouldAttach(ownership.concepts, concept, domainKey)) {
138
- pushUnique(collections.concepts, concept);
139
- }
140
-
141
- if (
142
- concept.parentConcept &&
143
- shouldAttach(ownership.concepts, concept.parentConcept, domainKey)
144
- ) {
145
- if (pushUnique(collections.concepts, concept.parentConcept)) {
146
- pendingConcepts.push(concept.parentConcept);
147
- }
148
- }
149
-
150
- for (const endpoint of collections.endpoints) {
151
- if (referencesConcept(endpoint, concept)) {
152
- includeEndpoint({
153
- domainKey,
154
- endpoint,
155
- ownership,
156
- collections,
157
- pendingConcepts,
158
- pendingEndpoints,
159
- });
160
- }
161
- }
162
- }
163
-
164
- function includeEndpoint(options: {
165
- domainKey: string;
166
- endpoint: EndpointDef;
167
- ownership: OwnershipMaps;
168
- collections: DomainCollections;
169
- pendingConcepts: ConceptDef[];
170
- pendingEndpoints: EndpointDef[];
171
- }) {
172
- const { domainKey, endpoint, ownership, collections, pendingConcepts, pendingEndpoints } =
173
- options;
174
-
175
- if (shouldAttach(ownership.endpoints, endpoint, domainKey)) {
176
- pushUnique(collections.endpoints, endpoint);
177
- }
178
-
179
- includeRule(domainKey, endpoint.governedBy ?? [], ownership, collections);
180
-
181
- for (const concept of endpoint.queries ?? []) {
182
- if (
183
- shouldAttach(ownership.concepts, concept, domainKey) &&
184
- pushUnique(collections.concepts, concept)
185
- ) {
186
- pendingConcepts.push(concept);
187
- }
188
- }
189
-
190
- for (const concept of endpoint.mutates ?? []) {
191
- if (
192
- shouldAttach(ownership.concepts, concept, domainKey) &&
193
- pushUnique(collections.concepts, concept)
194
- ) {
195
- pendingConcepts.push(concept);
196
- }
197
- }
198
-
199
- for (const returned of endpoint.returns ?? []) {
200
- if (
201
- shouldAttach(ownership.concepts, returned.concept, domainKey) &&
202
- pushUnique(collections.concepts, returned.concept)
203
- ) {
204
- pendingConcepts.push(returned.concept);
205
- }
206
- }
207
-
208
- for (const dependency of endpoint.dependsOn ?? []) {
209
- if (
210
- shouldAttach(ownership.endpoints, dependency.endpoint, domainKey) &&
211
- pushUnique(collections.endpoints, dependency.endpoint)
212
- ) {
213
- pendingEndpoints.push(dependency.endpoint);
214
- }
215
- }
216
- }
217
-
218
- function includeService(options: {
219
- domainKey: string;
220
- service: ServiceDef;
221
- ownership: OwnershipMaps;
222
- collections: DomainCollections;
223
- pendingConcepts: ConceptDef[];
224
- }) {
225
- const { domainKey, service, ownership, collections, pendingConcepts } = options;
226
-
227
- if (shouldAttach(ownership.services, service, domainKey)) {
228
- pushUnique(collections.services, service);
229
- }
230
-
231
- includeRule(domainKey, service.governedBy ?? [], ownership, collections);
232
-
233
- if (
234
- shouldAttach(ownership.concepts, service.belongsTo, domainKey) &&
235
- pushUnique(collections.concepts, service.belongsTo)
236
- ) {
237
- pendingConcepts.push(service.belongsTo);
238
- }
239
- }
240
-
241
- function includeRule(
242
- domainKey: string,
243
- rules: readonly RuleDef[],
244
- ownership: OwnershipMaps,
245
- collections: DomainCollections,
246
- ) {
247
- for (const rule of rules) {
248
- if (shouldAttach(ownership.rules, rule, domainKey)) {
249
- pushUnique(collections.rules, rule);
250
- }
251
- }
252
- }
253
-
254
- function shouldAttach<T extends object>(owners: WeakMap<T, string>, value: T, domainKey: string) {
255
- const owner = owners.get(value);
256
- return owner == null || owner === domainKey;
257
- }
258
-
259
- function pushUnique<T extends object>(values: T[], value: T) {
260
- if (values.includes(value)) {
261
- return false;
262
- }
263
-
264
- values.push(value);
265
- return true;
266
- }
267
-
268
- function referencesConcept(endpoint: EndpointDef, concept: ConceptDef) {
269
- return (
270
- endpoint.queries?.includes(concept) ||
271
- endpoint.mutates?.includes(concept) ||
272
- endpoint.returns?.some(function (returned) {
273
- return returned.concept === concept;
274
- })
275
- );
276
- }
@@ -1,192 +0,0 @@
1
- /**
2
- * Transforms declarative DomainDef into idempotent MERGE-based Cypher.
3
- *
4
- * Returns two phases:
5
- * - `nodes` -- MERGE statements that create/update Domain, Concept, Endpoint,
6
- * Service, and Rule nodes.
7
- * - `edges` -- MATCH+MERGE statements that wire up relationships between nodes.
8
- *
9
- * The caller MUST run all `nodes` from every module before running any `edges`,
10
- * because edges may reference nodes defined in a different module (e.g. a Rule
11
- * in the leaves module that GOVERNS a Concept created by the servicing module).
12
- */
13
-
14
- import type { DomainDef } from "./types.ts";
15
-
16
- export type GeneratedCypher = {
17
- nodes: string[];
18
- edges: string[];
19
- };
20
-
21
- function esc(s: string) {
22
- return s.replace(/\\/g, "\\\\").replace(/'/g, "\\'").replace(/\n/g, "\\n");
23
- }
24
-
25
- function jsonStr(value: unknown) {
26
- return esc(JSON.stringify(value));
27
- }
28
-
29
- export function generateCypher(data: DomainDef) {
30
- const nodes: string[] = [];
31
- const edges: string[] = [];
32
-
33
- // -- Domain node --
34
- const domainMeta = data.metadata
35
- ? `,\n d.metadata = '${jsonStr(data.metadata)}'`
36
- : "";
37
- nodes.push(
38
- `MERGE (d:Domain {name: '${esc(data.name)}'})
39
- SET d.description = '${esc(data.description)}'${domainMeta}`,
40
- );
41
-
42
- // -- Concept nodes --
43
- for (const c of data.concepts ?? []) {
44
- const aliasClause =
45
- c.aliases && c.aliases.length > 0
46
- ? `, c.aliases = [${c.aliases.map((a) => `'${esc(a)}'`).join(", ")}]`
47
- : "";
48
- const conceptMeta = c.metadata ? `, c.metadata = '${jsonStr(c.metadata)}'` : "";
49
-
50
- nodes.push(
51
- `MERGE (c:Concept {name: '${esc(c.name)}'})
52
- SET c.description = '${esc(c.description)}'${aliasClause}${conceptMeta}`,
53
- );
54
-
55
- edges.push(
56
- `MATCH (c:Concept {name: '${esc(c.name)}'})
57
- MATCH (d:Domain {name: '${esc(data.name)}'})
58
- MERGE (c)-[:BELONGS_TO]->(d)`,
59
- );
60
-
61
- if (c.parentConcept) {
62
- edges.push(
63
- `MATCH (child:Concept {name: '${esc(c.name)}'})
64
- MATCH (parent:Concept {name: '${esc(c.parentConcept.name)}'})
65
- MERGE (child)-[:SPECIALIZES]->(parent)`,
66
- );
67
- }
68
-
69
- c.governedBy?.forEach((rule) => {
70
- edges.push(
71
- `MATCH (r:Rule {name: '${esc(rule.name)}'})
72
- MATCH (t:Concept {name: '${esc(c.name)}'})
73
- MERGE (r)-[:GOVERNS]->(t)`,
74
- );
75
- });
76
- }
77
-
78
- // -- Endpoint nodes --
79
- for (const ep of data.endpoints ?? []) {
80
- const endpointMeta = ep.metadata
81
- ? `,\n e.metadata = '${jsonStr(ep.metadata)}'`
82
- : "";
83
- nodes.push(
84
- `MERGE (e:Endpoint {path: '${esc(ep.path)}', method: '${ep.method}'})
85
- SET e.name = '${esc(ep.name)}',
86
- e.description = '${esc(ep.description)}',
87
- e.params = '${jsonStr(ep.params)}',
88
- e.body = '${jsonStr(ep.body)}',
89
- e.response = '${jsonStr(ep.response)}',
90
- e.propertiesDescriptions = '${jsonStr(ep.propertiesDescriptions)}',
91
- e.successStatus = ${ep.successStatus},
92
- e.errorStatuses = [${ep.errorStatuses.join(", ")}]${endpointMeta}`,
93
- );
94
-
95
- for (const concept of ep.queries ?? []) {
96
- edges.push(
97
- `MATCH (c:Concept {name: '${esc(concept.name)}'})
98
- MATCH (e:Endpoint {path: '${esc(ep.path)}', method: '${ep.method}'})
99
- MERGE (c)-[:QUERIED_VIA]->(e)`,
100
- );
101
- }
102
-
103
- for (const concept of ep.mutates ?? []) {
104
- edges.push(
105
- `MATCH (c:Concept {name: '${esc(concept.name)}'})
106
- MATCH (e:Endpoint {path: '${esc(ep.path)}', method: '${ep.method}'})
107
- MERGE (c)-[:MUTATED_VIA]->(e)`,
108
- );
109
- }
110
-
111
- for (const ret of ep.returns ?? []) {
112
- const fieldClause = ret.field ? ` SET r.field = '${esc(ret.field)}'` : "";
113
-
114
- edges.push(
115
- `MATCH (e:Endpoint {path: '${esc(ep.path)}', method: '${ep.method}'})
116
- MATCH (c:Concept {name: '${esc(ret.concept.name)}'})
117
- MERGE (e)-[r:RETURNS]->(c)${fieldClause}`,
118
- );
119
- }
120
-
121
- for (const rule of ep.governedBy ?? []) {
122
- edges.push(
123
- `MATCH (r:Rule {name: '${esc(rule.name)}'})
124
- MATCH (t:Endpoint {path: '${esc(ep.path)}', method: '${ep.method}'})
125
- MERGE (r)-[:GOVERNS]->(t)`,
126
- );
127
- }
128
-
129
- for (const dep of ep.dependsOn ?? []) {
130
- edges.push(
131
- `MATCH (e:Endpoint {path: '${esc(ep.path)}', method: '${ep.method}'})
132
- MATCH (dep:Endpoint {path: '${esc(dep.endpoint.path)}', method: '${dep.endpoint.method}'})
133
- MERGE (e)-[d:DEPENDS_ON]->(dep)
134
- SET d.paramName = '${esc(dep.paramName)}', d.fromField = '${esc(dep.fromField)}'`,
135
- );
136
- }
137
- }
138
-
139
- // -- Service nodes --
140
- for (const svc of data.services ?? []) {
141
- const serviceMeta = svc.metadata
142
- ? `,\n s.metadata = '${jsonStr(svc.metadata)}'`
143
- : "";
144
- nodes.push(
145
- `MERGE (s:Service {builtInId: '${esc(svc.builtInId)}'})
146
- SET s.name = '${esc(svc.name)}',
147
- s.description = '${esc(svc.description)}'${serviceMeta}`,
148
- );
149
-
150
- edges.push(
151
- `MATCH (c:Concept {name: '${esc(svc.belongsTo.name)}'})
152
- MATCH (s:Service {builtInId: '${esc(svc.builtInId)}'})
153
- MERGE (c)-[:REQUESTED_VIA]->(s)`,
154
- );
155
-
156
- svc.governedBy?.forEach((rule) => {
157
- edges.push(
158
- `MATCH (r:Rule {name: '${esc(rule.name)}'})
159
- MATCH (t:Service {builtInId: '${esc(svc.builtInId)}'})
160
- MERGE (r)-[:GOVERNS]->(t)`,
161
- );
162
- });
163
- }
164
-
165
- // -- Rule nodes --
166
- for (const rule of data.rules ?? []) {
167
- const ruleMeta = rule.metadata
168
- ? `,\n r.metadata = '${jsonStr(rule.metadata)}'`
169
- : "";
170
- nodes.push(
171
- `MERGE (r:Rule {name: '${esc(rule.name)}'})
172
- SET r.description = '${esc(rule.description)}'${ruleMeta}`,
173
- );
174
- }
175
-
176
- return { nodes, edges } satisfies GeneratedCypher;
177
- }
178
-
179
- export function toCypherScript(data: DomainDef) {
180
- const { nodes, edges } = generateCypher(data);
181
- return (
182
- `// Auto-generated seed script for module: ${data.name}\n` +
183
- `// Generated at: ${new Date().toISOString()}\n` +
184
- `// Safe to re-run (uses MERGE)\n\n` +
185
- `// --- Nodes ---\n` +
186
- nodes.join(";\n\n") +
187
- ";\n\n" +
188
- `// --- Edges ---\n` +
189
- edges.join(";\n\n") +
190
- ";\n"
191
- );
192
- }
@@ -1,47 +0,0 @@
1
- // Types
2
- export type {
3
- EndpointScalarType,
4
- EndpointProperty,
5
- ResponseKind,
6
- AutoGenerated,
7
- ConceptDef,
8
- EndpointDef,
9
- EndpointInput,
10
- ServiceDef,
11
- RuleDef,
12
- DomainDef,
13
- } from "./types.ts";
14
-
15
- // Helpers
16
- export {
17
- defineConcept,
18
- defineRule,
19
- defineService,
20
- defineDomain,
21
- defineEndpoint,
22
- } from "./helpers.ts";
23
-
24
- // Cypher generation
25
- export type { GeneratedCypher } from "./generate-cypher.ts";
26
- export { generateCypher, toCypherScript } from "./generate-cypher.ts";
27
-
28
- // Validation
29
- export { validateDomain } from "./validate.ts";
30
-
31
- // Neo4j client
32
- export type { Neo4jConfig, Neo4jClient } from "./neo4j.ts";
33
- export { createNeo4jClient } from "./neo4j.ts";
34
-
35
- // Seed orchestrator
36
- export type { EmbeddingConfig, SeedGraphConfig } from "./seed.ts";
37
- export { seedGraph } from "./seed.ts";
38
-
39
- // Graph resolver
40
- export type {
41
- RerankerConfig,
42
- ResolverConfig,
43
- ResolvedEndpoint,
44
- ResolvedService,
45
- ResolvedContext,
46
- } from "./resolver.ts";
47
- export { resolveFromGraph } from "./resolver.ts";