@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
@@ -0,0 +1,143 @@
1
+ /**
2
+ * The wire contract — every shape that crosses the HTTP/WebSocket boundary
3
+ * between `@m6d/cortex-server` and the client SDKs (`@m6d/cortex-angular`,
4
+ * `@m6d/cortex-react`).
5
+ *
6
+ * Nothing here has a runtime dependency, and nothing here may grow one: this
7
+ * file is compiled into an Angular library, a React library and a Bun server
8
+ * alike.
9
+ */
10
+
11
+ export const ATTACHMENT_MIME_TYPES = [
12
+ "image/png",
13
+ "image/jpeg",
14
+ "image/webp",
15
+ "application/pdf",
16
+ ] as const;
17
+
18
+ export const ATTACHMENT_MAX_BYTES = 10 * 1024 * 1024;
19
+
20
+ export const MAX_ATTACHMENTS_PER_MESSAGE = 5;
21
+
22
+ export const ATTACHMENT_STATUSES = ["pending", "described", "failed", "skipped"] as const;
23
+
24
+ export function checkAttachmentPolicy(contentType: string, sizeBytes: number) {
25
+ if (!ATTACHMENT_MIME_TYPES.some((type) => type === contentType)) return "type";
26
+ if (sizeBytes > ATTACHMENT_MAX_BYTES) return "size";
27
+ return "ok";
28
+ }
29
+
30
+ export type AttachmentStatus = (typeof ATTACHMENT_STATUSES)[number];
31
+
32
+ export type AttachmentSummary = {
33
+ id: string;
34
+ filename: string;
35
+ contentType: string;
36
+ sizeBytes: number;
37
+ status: AttachmentStatus;
38
+ description?: string;
39
+ };
40
+
41
+ export type ThreadSummary = {
42
+ id: string;
43
+ title?: string;
44
+ createdAt: string;
45
+ updatedAt: string;
46
+ isRunning: boolean;
47
+ };
48
+
49
+ /**
50
+ * Response of `POST /chat/:chatId/abort`. `aborted: false` means there was no
51
+ * live run to abort — the turn had already committed — so the caller must not
52
+ * present the turn as aborted and should read the settled state back instead.
53
+ */
54
+ export type AbortRunResult = {
55
+ aborted: boolean;
56
+ };
57
+
58
+ export type TokenUsage = {
59
+ input: {
60
+ noCache: number;
61
+ cacheRead: number;
62
+ cacheWrite: number;
63
+ total: number;
64
+ };
65
+ output: {
66
+ reasoning: number;
67
+ text: number;
68
+ total: number;
69
+ };
70
+ total: number;
71
+ };
72
+
73
+ export type MessageMetadata = {
74
+ modelId?: string;
75
+ providerMetadata?: unknown;
76
+ isAborted?: boolean;
77
+ tokenUsage?: TokenUsage;
78
+ attachments?: AttachmentSummary[];
79
+ };
80
+
81
+ /**
82
+ * A stored message as it is persisted and served. TanStack AI's `UIMessage`
83
+ * has no metadata field, so cortex owns the envelope: the SDK's parts plus the
84
+ * server-derived metadata the UI renders on settled messages.
85
+ *
86
+ * `TPart` stays generic so this file keeps its zero dependencies — both
87
+ * sides substitute the SDK's `MessagePart`.
88
+ */
89
+ export type CortexMessage<TPart = unknown> = {
90
+ id: string;
91
+ role: "system" | "user" | "assistant";
92
+ parts: TPart[];
93
+ metadata?: MessageMetadata;
94
+ };
95
+
96
+ export type ThreadCreatedEvent = {
97
+ type: "thread:created";
98
+ payload: { thread: ThreadSummary };
99
+ };
100
+
101
+ export type ThreadDeletedEvent = {
102
+ type: "thread:deleted";
103
+ payload: { threadId: string };
104
+ };
105
+
106
+ export type ThreadTitleUpdatedEvent = {
107
+ type: "thread:title-updated";
108
+ payload: { thread: ThreadSummary };
109
+ };
110
+
111
+ export type ThreadRunStartedEvent = {
112
+ type: "thread:run-started";
113
+ payload: { thread: ThreadSummary };
114
+ };
115
+
116
+ export type ThreadRunFinishedEvent = {
117
+ type: "thread:run-finished";
118
+ payload: { thread: ThreadSummary };
119
+ };
120
+
121
+ export type ThreadMessagesUpdatedEvent = {
122
+ type: "thread:messages-updated";
123
+ payload: { threadId: string; thread: ThreadSummary };
124
+ };
125
+
126
+ export type ToolProgressEvent = {
127
+ type: "thread:tool-progress";
128
+ payload: {
129
+ threadId: string;
130
+ toolCallId: string;
131
+ toolName: string;
132
+ status: "started" | "finished";
133
+ };
134
+ };
135
+
136
+ export type WsEvent =
137
+ | ThreadCreatedEvent
138
+ | ThreadDeletedEvent
139
+ | ThreadTitleUpdatedEvent
140
+ | ThreadRunStartedEvent
141
+ | ThreadRunFinishedEvent
142
+ | ThreadMessagesUpdatedEvent
143
+ | ToolProgressEvent;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Embeddings, which both halves of the graph need: `@m6d/cortex-cli` embeds every
3
+ * concept description as it seeds, `@m6d/cortex-server` embeds every prompt as it
4
+ * resolves. Two embedders would mean two vector spaces, so there is one.
5
+ *
6
+ * TanStack AI removed embeddings, and the graph only ever needed the plain
7
+ * OpenAI-compatible `/embeddings` call cortex is already configured for — so this
8
+ * is a bare `fetch`, and this package keeps its zero dependencies.
9
+ *
10
+ * Everything under `./graph` is written to the oldest target any consumer builds
11
+ * against — an Angular library compiles these sources directly — and imports
12
+ * siblings by their `.js` specifier so one emit serves both `dist` and source.
13
+ */
14
+ /** Everything cortex needs to reach one OpenAI-compatible embeddings endpoint. */
15
+ export type EmbeddingProviderConfig = {
16
+ baseURL: string;
17
+ apiKey: string;
18
+ modelName: string;
19
+ };
20
+ /** Turns a batch of strings into their vectors, in the order given. */
21
+ export type EmbedFn = (values: string[]) => Promise<number[][]>;
22
+ export declare function createEmbedder(config: EmbeddingProviderConfig): (values: string[]) => Promise<number[][]>;
@@ -1,34 +1,36 @@
1
1
  /**
2
- * Helper functions for building knowledge graph seed data.
2
+ * Helper functions for building knowledge graph seed data. Projects author
3
+ * domains with these; `@m6d/cortex-server` re-exports them so authoring needs
4
+ * exactly one import path.
3
5
  */
4
- import type { ConceptDef, DomainDef, EndpointDef, EndpointInput, RuleDef, ServiceDef } from "./types.ts";
6
+ import type { ConceptDef, DomainDef, EndpointDef, EndpointInput, RuleDef, ServiceDef } from "./types";
5
7
  export declare function defineConcept(def: Omit<ConceptDef, "__brand">): {
6
- name: string;
7
8
  description: string;
9
+ name: string;
8
10
  aliases?: string[] | undefined;
11
+ metadata?: Record<string, unknown> | undefined;
9
12
  parentConcept?: ConceptDef | undefined;
10
13
  governedBy?: RuleDef[] | undefined;
11
- metadata?: Record<string, unknown> | undefined;
12
14
  __brand: "concept";
13
15
  };
14
16
  export declare function defineRule(def: Omit<RuleDef, "__brand">): {
15
- name: string;
16
17
  description: string;
18
+ name: string;
17
19
  metadata?: Record<string, unknown> | undefined;
18
20
  __brand: "rule";
19
21
  };
20
22
  export declare function defineService(def: Omit<ServiceDef, "__brand">): {
21
- name: string;
22
23
  description: string;
23
- governedBy?: RuleDef[] | undefined;
24
+ name: string;
24
25
  metadata?: Record<string, unknown> | undefined;
25
26
  builtInId: string;
27
+ governedBy?: RuleDef[] | undefined;
26
28
  belongsTo: ConceptDef;
27
29
  __brand: "service";
28
30
  };
29
31
  export declare function defineDomain(def: Omit<DomainDef, "__brand">): {
30
- name: string;
31
32
  description: string;
33
+ name: string;
32
34
  metadata?: Record<string, unknown> | undefined;
33
35
  concepts?: ConceptDef[] | undefined;
34
36
  endpoints?: EndpointDef[] | undefined;
@@ -43,12 +45,12 @@ export declare function defineEndpoint(input: EndpointInput): {
43
45
  path: string;
44
46
  method: "GET" | "POST" | "PUT" | "DELETE";
45
47
  propertiesDescriptions: Record<string, string>;
46
- params: import("./types.ts").EndpointProperty[];
47
- body: import("./types.ts").EndpointProperty[];
48
- response: import("./types.ts").EndpointProperty[];
48
+ params: import("./types").EndpointProperty[];
49
+ body: import("./types").EndpointProperty[];
50
+ response: import("./types").EndpointProperty[];
49
51
  successStatus: number;
50
52
  errorStatuses: number[];
51
- responseKind: import("./types.ts").ResponseKind;
53
+ responseKind: import("./types").ResponseKind;
52
54
  queries: ConceptDef[] | undefined;
53
55
  mutates: ConceptDef[] | undefined;
54
56
  returns: {
@@ -1,10 +1,10 @@
1
1
  /**
2
- * Configurable Neo4j HTTP client.
2
+ * Configurable Neo4j HTTP client, shared by the graph's writer and its reader.
3
3
  *
4
- * Replaces the hardcoded process.env-based client from packages/sample-backend.
5
- * All configuration is passed explicitly no environment variables are read.
4
+ * All configuration is passed explicitly no environment variables are read —
5
+ * and the transport is `fetch`, so this package stays dependency-free.
6
6
  */
7
- import type { EmbeddingModel } from "ai";
7
+ import type { EmbedFn } from "./embed";
8
8
  export type Neo4jConfig = {
9
9
  url: string;
10
10
  user: string;
@@ -13,6 +13,6 @@ export type Neo4jConfig = {
13
13
  export type Neo4jClient = {
14
14
  query(cypher: string, parameters?: Record<string, unknown>): Promise<string>;
15
15
  };
16
- export declare function createNeo4jClient(config: Neo4jConfig, embeddingModel?: EmbeddingModel): {
16
+ export declare function createNeo4jClient(config: Neo4jConfig, embed?: EmbedFn): {
17
17
  query: (cypher: string, parameters?: Record<string, unknown>) => Promise<string>;
18
18
  };
@@ -0,0 +1,62 @@
1
+ /**
2
+ * The contract between the graph's writer and its reader.
3
+ *
4
+ * Seeding lives in `@m6d/cortex-cli` and resolution lives in `@m6d/cortex-server`,
5
+ * two packages a project installs and upgrades separately. The labels, relationship
6
+ * types, property names and index name they agree on are this module, here, so
7
+ * neither side owns the vocabulary the other has to match.
8
+ *
9
+ * A mismatch between the two is invisible at runtime: the resolver's Cypher
10
+ * matches nothing and retrieval comes back empty rather than failing. That is what
11
+ * `GRAPH_SCHEMA_VERSION` is for — bump it on any change to the shape below. The
12
+ * CLI stamps the version it wrote onto the graph and the server checks that stamp
13
+ * on its first resolve, so skew surfaces as an error instead of as silence.
14
+ */
15
+ export declare const GRAPH_SCHEMA_VERSION = 1;
16
+ export declare const GRAPH_SCHEMA: {
17
+ readonly label: {
18
+ readonly domain: "Domain";
19
+ readonly concept: "Concept";
20
+ readonly endpoint: "Endpoint";
21
+ readonly service: "Service";
22
+ readonly rule: "Rule";
23
+ /** Stamped with the version above, so a stale graph is detectable later. */
24
+ readonly marker: "CortexGraph";
25
+ };
26
+ readonly rel: {
27
+ readonly belongsTo: "BELONGS_TO";
28
+ readonly specializes: "SPECIALIZES";
29
+ readonly queriedVia: "QUERIED_VIA";
30
+ readonly mutatedVia: "MUTATED_VIA";
31
+ readonly returns: "RETURNS";
32
+ readonly dependsOn: "DEPENDS_ON";
33
+ readonly governs: "GOVERNS";
34
+ readonly requestedVia: "REQUESTED_VIA";
35
+ };
36
+ readonly prop: {
37
+ readonly name: "name";
38
+ readonly description: "description";
39
+ readonly aliases: "aliases";
40
+ readonly metadata: "metadata";
41
+ readonly path: "path";
42
+ readonly method: "method";
43
+ readonly params: "params";
44
+ readonly body: "body";
45
+ readonly response: "response";
46
+ readonly propertiesDescriptions: "propertiesDescriptions";
47
+ readonly successStatus: "successStatus";
48
+ readonly errorStatuses: "errorStatuses";
49
+ readonly builtInId: "builtInId";
50
+ readonly embedding: "embedding";
51
+ /** On a RETURNS edge: which part of the response carries the concept. */
52
+ readonly field: "field";
53
+ /** On a DEPENDS_ON edge. */
54
+ readonly paramName: "paramName";
55
+ readonly fromField: "fromField";
56
+ /** On the marker node. */
57
+ readonly version: "version";
58
+ };
59
+ /** Created by the CLI's seeder, queried by name in the resolver's first hop. */
60
+ readonly conceptEmbeddingsIndex: "concept_embeddings";
61
+ };
62
+ export type GraphSchema = typeof GRAPH_SCHEMA;
@@ -10,7 +10,7 @@ export type EndpointScalarType = "uuid" | "number" | "date" | "datetime" | "stri
10
10
  export type EndpointProperty = {
11
11
  readonly name: string;
12
12
  readonly required: boolean;
13
- readonly type: EndpointScalarType | string;
13
+ readonly type: EndpointScalarType | (string & NonNullable<unknown>);
14
14
  readonly isArray?: boolean;
15
15
  readonly properties?: readonly EndpointProperty[];
16
16
  readonly description?: string;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The graph contract — the knowledge-graph vocabulary, authoring types and
3
+ * clients that `@m6d/cortex-cli` writes with and `@m6d/cortex-server` reads
4
+ * with. `@m6d/cortex-server` re-exports all of it, so a project authoring
5
+ * domains still has exactly one import path.
6
+ */
7
+ export type { GraphSchema } from "./graph/schema";
8
+ export { GRAPH_SCHEMA, GRAPH_SCHEMA_VERSION } from "./graph/schema";
9
+ export type { EndpointScalarType, EndpointProperty, ResponseKind, AutoGenerated, ConceptDef, EndpointDef, EndpointInput, ServiceDef, RuleDef, DomainDef, } from "./graph/types";
10
+ export { defineConcept, defineRule, defineService, defineDomain, defineEndpoint, } from "./graph/helpers";
11
+ export type { Neo4jConfig, Neo4jClient } from "./graph/neo4j";
12
+ export { createNeo4jClient } from "./graph/neo4j";
13
+ export type { EmbedFn, EmbeddingProviderConfig } from "./graph/embed";
14
+ export { createEmbedder } from "./graph/embed";
@@ -0,0 +1,278 @@
1
+ import { z } from "zod";
2
+ /** Runtime contract §1.1: the `:id` path param, validated on both sides of the wire. */
3
+ export declare const AGENT_SLUG_PATTERN: RegExp;
4
+ /** Runtime contract §5: tool names become `tools.<name>()` sandbox bindings. */
5
+ export declare const TOOL_NAME_PATTERN: RegExp;
6
+ export declare const PROMPT_VARIABLES: readonly ["userName", "channel", "locale"];
7
+ export declare const RUNTIME_ERROR_KINDS: readonly ["timeout", "upstream_4xx", "upstream_5xx", "schema_mismatch", "rate_limited", "not_found", "unauthorized", "invalid_request", "egress_blocked", "internal"];
8
+ /** The ways an execute output may disagree with the tool's declared schema. */
9
+ export declare const JSON_SCHEMA_ISSUE_KINDS: readonly ["missing", "unexpected", "type_mismatch"];
10
+ export declare const agentSlugSchema: z.ZodString;
11
+ export declare const localeSchema: z.ZodEnum<{
12
+ en: "en";
13
+ ar: "ar";
14
+ }>;
15
+ export declare const catalogVersionSchema: z.ZodString;
16
+ export declare const sharedShapeSchema: z.ZodObject<{
17
+ ref: z.ZodString;
18
+ shape: z.ZodString;
19
+ }, z.core.$strip>;
20
+ export declare const knowledgeChunkSchema: z.ZodObject<{
21
+ chunkId: z.ZodUUID;
22
+ text: z.ZodString;
23
+ score: z.ZodNumber;
24
+ citation: z.ZodObject<{
25
+ documentId: z.ZodUUID;
26
+ documentTitle: z.ZodString;
27
+ page: z.ZodNullable<z.ZodNumber>;
28
+ section: z.ZodNullable<z.ZodString>;
29
+ uri: z.ZodNullable<z.ZodURL>;
30
+ service: z.ZodNullable<z.ZodString>;
31
+ }, z.core.$strip>;
32
+ }, z.core.$strip>;
33
+ export declare const toolSignatureSchema: z.ZodObject<{
34
+ toolId: z.ZodUUID;
35
+ name: z.ZodString;
36
+ version: z.ZodNumber;
37
+ readOnly: z.ZodBoolean;
38
+ tags: z.ZodArray<z.ZodString>;
39
+ signature: z.ZodString;
40
+ score: z.ZodNullable<z.ZodNumber>;
41
+ pinned: z.ZodBoolean;
42
+ }, z.core.$strip>;
43
+ export declare const serviceCardSchema: z.ZodObject<{
44
+ serviceId: z.ZodUUID;
45
+ key: z.ZodString;
46
+ name: z.ZodString;
47
+ card: z.ZodString;
48
+ score: z.ZodNumber;
49
+ }, z.core.$strip>;
50
+ export declare const runtimeAgentConfigSchema: z.ZodObject<{
51
+ agentId: z.ZodString;
52
+ systemPrompt: z.ZodString;
53
+ promptVariables: z.ZodArray<z.ZodEnum<{
54
+ userName: "userName";
55
+ channel: "channel";
56
+ locale: "locale";
57
+ }>>;
58
+ catalogBlurb: z.ZodString;
59
+ defaultLocale: z.ZodEnum<{
60
+ en: "en";
61
+ ar: "ar";
62
+ }>;
63
+ metaTools: z.ZodObject<{
64
+ searchKnowledge: z.ZodBoolean;
65
+ searchTools: z.ZodBoolean;
66
+ searchServices: z.ZodBoolean;
67
+ }, z.core.$strip>;
68
+ limits: z.ZodObject<{
69
+ resolveTokenBudget: z.ZodNumber;
70
+ executeTimeoutMs: z.ZodNumber;
71
+ maxResponseBytes: z.ZodNumber;
72
+ }, z.core.$strip>;
73
+ catalogVersion: z.ZodString;
74
+ publishedAt: z.ZodISODateTime;
75
+ }, z.core.$strip>;
76
+ export declare const resolveRequestSchema: z.ZodObject<{
77
+ query: z.ZodString;
78
+ locale: z.ZodOptional<z.ZodEnum<{
79
+ en: "en";
80
+ ar: "ar";
81
+ }>>;
82
+ hints: z.ZodOptional<z.ZodObject<{
83
+ threadId: z.ZodOptional<z.ZodString>;
84
+ recentToolNames: z.ZodDefault<z.ZodArray<z.ZodString>>;
85
+ turnIndex: z.ZodOptional<z.ZodNumber>;
86
+ }, z.core.$strip>>;
87
+ tokenBudget: z.ZodOptional<z.ZodNumber>;
88
+ limits: z.ZodOptional<z.ZodObject<{
89
+ knowledge: z.ZodDefault<z.ZodNumber>;
90
+ tools: z.ZodDefault<z.ZodNumber>;
91
+ services: z.ZodDefault<z.ZodNumber>;
92
+ }, z.core.$strip>>;
93
+ }, z.core.$strip>;
94
+ export declare const resolveResponseSchema: z.ZodObject<{
95
+ resolveId: z.ZodUUID;
96
+ catalogVersion: z.ZodString;
97
+ locale: z.ZodEnum<{
98
+ en: "en";
99
+ ar: "ar";
100
+ }>;
101
+ knowledge: z.ZodArray<z.ZodObject<{
102
+ chunkId: z.ZodUUID;
103
+ text: z.ZodString;
104
+ score: z.ZodNumber;
105
+ citation: z.ZodObject<{
106
+ documentId: z.ZodUUID;
107
+ documentTitle: z.ZodString;
108
+ page: z.ZodNullable<z.ZodNumber>;
109
+ section: z.ZodNullable<z.ZodString>;
110
+ uri: z.ZodNullable<z.ZodURL>;
111
+ service: z.ZodNullable<z.ZodString>;
112
+ }, z.core.$strip>;
113
+ }, z.core.$strip>>;
114
+ tools: z.ZodArray<z.ZodObject<{
115
+ toolId: z.ZodUUID;
116
+ name: z.ZodString;
117
+ version: z.ZodNumber;
118
+ readOnly: z.ZodBoolean;
119
+ tags: z.ZodArray<z.ZodString>;
120
+ signature: z.ZodString;
121
+ score: z.ZodNullable<z.ZodNumber>;
122
+ pinned: z.ZodBoolean;
123
+ }, z.core.$strip>>;
124
+ services: z.ZodArray<z.ZodObject<{
125
+ serviceId: z.ZodUUID;
126
+ key: z.ZodString;
127
+ name: z.ZodString;
128
+ card: z.ZodString;
129
+ score: z.ZodNumber;
130
+ }, z.core.$strip>>;
131
+ sharedShapes: z.ZodArray<z.ZodObject<{
132
+ ref: z.ZodString;
133
+ shape: z.ZodString;
134
+ }, z.core.$strip>>;
135
+ budget: z.ZodObject<{
136
+ requested: z.ZodNumber;
137
+ used: z.ZodNumber;
138
+ truncated: z.ZodBoolean;
139
+ }, z.core.$strip>;
140
+ }, z.core.$strip>;
141
+ export declare const searchRequestSchema: z.ZodObject<{
142
+ query: z.ZodString;
143
+ limit: z.ZodDefault<z.ZodNumber>;
144
+ locale: z.ZodOptional<z.ZodEnum<{
145
+ en: "en";
146
+ ar: "ar";
147
+ }>>;
148
+ threadId: z.ZodOptional<z.ZodString>;
149
+ service: z.ZodOptional<z.ZodString>;
150
+ }, z.core.$strip>;
151
+ export declare const searchToolsResponseSchema: z.ZodObject<{
152
+ catalogVersion: z.ZodString;
153
+ tools: z.ZodArray<z.ZodObject<{
154
+ toolId: z.ZodUUID;
155
+ name: z.ZodString;
156
+ version: z.ZodNumber;
157
+ readOnly: z.ZodBoolean;
158
+ tags: z.ZodArray<z.ZodString>;
159
+ signature: z.ZodString;
160
+ score: z.ZodNullable<z.ZodNumber>;
161
+ pinned: z.ZodBoolean;
162
+ }, z.core.$strip>>;
163
+ sharedShapes: z.ZodArray<z.ZodObject<{
164
+ ref: z.ZodString;
165
+ shape: z.ZodString;
166
+ }, z.core.$strip>>;
167
+ }, z.core.$strip>;
168
+ export declare const searchServicesResponseSchema: z.ZodObject<{
169
+ catalogVersion: z.ZodString;
170
+ services: z.ZodArray<z.ZodObject<{
171
+ serviceId: z.ZodUUID;
172
+ key: z.ZodString;
173
+ name: z.ZodString;
174
+ card: z.ZodString;
175
+ score: z.ZodNumber;
176
+ }, z.core.$strip>>;
177
+ tools: z.ZodArray<z.ZodObject<{
178
+ toolId: z.ZodUUID;
179
+ name: z.ZodString;
180
+ version: z.ZodNumber;
181
+ readOnly: z.ZodBoolean;
182
+ tags: z.ZodArray<z.ZodString>;
183
+ signature: z.ZodString;
184
+ score: z.ZodNullable<z.ZodNumber>;
185
+ pinned: z.ZodBoolean;
186
+ }, z.core.$strip>>;
187
+ sharedShapes: z.ZodArray<z.ZodObject<{
188
+ ref: z.ZodString;
189
+ shape: z.ZodString;
190
+ }, z.core.$strip>>;
191
+ }, z.core.$strip>;
192
+ export declare const searchKnowledgeResponseSchema: z.ZodObject<{
193
+ catalogVersion: z.ZodString;
194
+ knowledge: z.ZodArray<z.ZodObject<{
195
+ chunkId: z.ZodUUID;
196
+ text: z.ZodString;
197
+ score: z.ZodNumber;
198
+ citation: z.ZodObject<{
199
+ documentId: z.ZodUUID;
200
+ documentTitle: z.ZodString;
201
+ page: z.ZodNullable<z.ZodNumber>;
202
+ section: z.ZodNullable<z.ZodString>;
203
+ uri: z.ZodNullable<z.ZodURL>;
204
+ service: z.ZodNullable<z.ZodString>;
205
+ }, z.core.$strip>;
206
+ }, z.core.$strip>>;
207
+ }, z.core.$strip>;
208
+ export declare const executeRequestSchema: z.ZodObject<{
209
+ input: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
210
+ context: z.ZodOptional<z.ZodObject<{
211
+ threadId: z.ZodOptional<z.ZodString>;
212
+ userId: z.ZodOptional<z.ZodString>;
213
+ locale: z.ZodOptional<z.ZodEnum<{
214
+ en: "en";
215
+ ar: "ar";
216
+ }>>;
217
+ }, z.core.$strip>>;
218
+ }, z.core.$strip>;
219
+ export declare const executeResponseSchema: z.ZodObject<{
220
+ toolId: z.ZodUUID;
221
+ name: z.ZodString;
222
+ version: z.ZodNumber;
223
+ output: z.ZodUnknown;
224
+ meta: z.ZodObject<{
225
+ upstreamStatus: z.ZodNumber;
226
+ durationMs: z.ZodNumber;
227
+ replayed: z.ZodBoolean;
228
+ truncated: z.ZodBoolean;
229
+ truncatedPaths: z.ZodArray<z.ZodString>;
230
+ drift: z.ZodArray<z.ZodObject<{
231
+ path: z.ZodString;
232
+ kind: z.ZodEnum<{
233
+ missing: "missing";
234
+ unexpected: "unexpected";
235
+ type_mismatch: "type_mismatch";
236
+ }>;
237
+ }, z.core.$strip>>;
238
+ }, z.core.$strip>;
239
+ }, z.core.$strip>;
240
+ declare const runtimeErrorKindSchema: z.ZodEnum<{
241
+ timeout: "timeout";
242
+ upstream_4xx: "upstream_4xx";
243
+ upstream_5xx: "upstream_5xx";
244
+ schema_mismatch: "schema_mismatch";
245
+ rate_limited: "rate_limited";
246
+ not_found: "not_found";
247
+ unauthorized: "unauthorized";
248
+ invalid_request: "invalid_request";
249
+ egress_blocked: "egress_blocked";
250
+ internal: "internal";
251
+ }>;
252
+ export type RuntimeErrorKind = z.infer<typeof runtimeErrorKindSchema>;
253
+ export declare const runtimeErrorSchema: z.ZodObject<{
254
+ error: z.ZodObject<{
255
+ kind: z.ZodEnum<{
256
+ timeout: "timeout";
257
+ upstream_4xx: "upstream_4xx";
258
+ upstream_5xx: "upstream_5xx";
259
+ schema_mismatch: "schema_mismatch";
260
+ rate_limited: "rate_limited";
261
+ not_found: "not_found";
262
+ unauthorized: "unauthorized";
263
+ invalid_request: "invalid_request";
264
+ egress_blocked: "egress_blocked";
265
+ internal: "internal";
266
+ }>;
267
+ detail: z.ZodString;
268
+ retryable: z.ZodBoolean;
269
+ retryAfterSeconds: z.ZodOptional<z.ZodNumber>;
270
+ upstreamStatus: z.ZodOptional<z.ZodNumber>;
271
+ }, z.core.$strip>;
272
+ }, z.core.$strip>;
273
+ export type RuntimeAgentConfig = z.infer<typeof runtimeAgentConfigSchema>;
274
+ export type ResolveRequest = z.input<typeof resolveRequestSchema>;
275
+ export type ResolveResponse = z.infer<typeof resolveResponseSchema>;
276
+ export type SearchRequest = z.input<typeof searchRequestSchema>;
277
+ export type ExecuteRequest = z.input<typeof executeRequestSchema>;
278
+ export {};