@m6d/cortex-server 1.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 (82) hide show
  1. package/README.md +64 -0
  2. package/dist/index.d.ts +1 -0
  3. package/dist/src/adapters/database.d.ts +27 -0
  4. package/dist/src/adapters/minio.d.ts +10 -0
  5. package/dist/src/adapters/mssql.d.ts +3 -0
  6. package/dist/src/adapters/storage.d.ts +6 -0
  7. package/dist/src/ai/fetch.d.ts +2 -0
  8. package/dist/src/ai/helpers.d.ts +5 -0
  9. package/dist/src/ai/index.d.ts +4 -0
  10. package/dist/src/ai/interceptors/resolve-captured-files.d.ts +11 -0
  11. package/dist/src/ai/prompt.d.ts +4 -0
  12. package/dist/src/ai/tools/call-endpoint.tool.d.ts +7 -0
  13. package/dist/src/ai/tools/capture-files.tool.d.ts +6 -0
  14. package/dist/src/ai/tools/execute-code.tool.d.ts +4 -0
  15. package/dist/src/ai/tools/query-graph.tool.d.ts +5 -0
  16. package/dist/src/auth/middleware.d.ts +4 -0
  17. package/dist/src/cli/extract-endpoints.d.ts +6 -0
  18. package/dist/src/config.d.ts +145 -0
  19. package/dist/src/db/migrate.d.ts +1 -0
  20. package/dist/src/db/schema.d.ts +345 -0
  21. package/dist/src/factory.d.ts +17 -0
  22. package/dist/src/graph/generate-cypher.d.ts +22 -0
  23. package/dist/src/graph/helpers.d.ts +60 -0
  24. package/dist/src/graph/index.d.ts +11 -0
  25. package/dist/src/graph/neo4j.d.ts +18 -0
  26. package/dist/src/graph/resolver.d.ts +51 -0
  27. package/dist/src/graph/seed.d.ts +19 -0
  28. package/dist/src/graph/types.d.ts +104 -0
  29. package/dist/src/graph/validate.d.ts +2 -0
  30. package/dist/src/index.d.ts +10 -0
  31. package/dist/src/routes/chat.d.ts +3 -0
  32. package/dist/src/routes/files.d.ts +3 -0
  33. package/dist/src/routes/index.d.ts +4 -0
  34. package/dist/src/routes/threads.d.ts +3 -0
  35. package/dist/src/routes/ws.d.ts +3 -0
  36. package/dist/src/types.d.ts +56 -0
  37. package/dist/src/ws/connections.d.ts +4 -0
  38. package/dist/src/ws/events.d.ts +8 -0
  39. package/dist/src/ws/index.d.ts +3 -0
  40. package/dist/src/ws/notify.d.ts +2 -0
  41. package/index.ts +1 -0
  42. package/package.json +57 -0
  43. package/src/adapters/database.ts +33 -0
  44. package/src/adapters/minio.ts +89 -0
  45. package/src/adapters/mssql.ts +203 -0
  46. package/src/adapters/storage.ts +6 -0
  47. package/src/ai/fetch.ts +39 -0
  48. package/src/ai/helpers.ts +36 -0
  49. package/src/ai/index.ts +145 -0
  50. package/src/ai/interceptors/resolve-captured-files.ts +64 -0
  51. package/src/ai/prompt.ts +120 -0
  52. package/src/ai/tools/call-endpoint.tool.ts +96 -0
  53. package/src/ai/tools/capture-files.tool.ts +22 -0
  54. package/src/ai/tools/execute-code.tool.ts +108 -0
  55. package/src/ai/tools/query-graph.tool.ts +35 -0
  56. package/src/auth/middleware.ts +63 -0
  57. package/src/cli/extract-endpoints.ts +588 -0
  58. package/src/config.ts +155 -0
  59. package/src/db/migrate.ts +21 -0
  60. package/src/db/migrations/20260309012148_cloudy_maria_hill/migration.sql +36 -0
  61. package/src/db/migrations/20260309012148_cloudy_maria_hill/snapshot.json +305 -0
  62. package/src/db/schema.ts +77 -0
  63. package/src/factory.ts +159 -0
  64. package/src/graph/generate-cypher.ts +179 -0
  65. package/src/graph/helpers.ts +68 -0
  66. package/src/graph/index.ts +47 -0
  67. package/src/graph/neo4j.ts +117 -0
  68. package/src/graph/resolver.ts +357 -0
  69. package/src/graph/seed.ts +172 -0
  70. package/src/graph/types.ts +152 -0
  71. package/src/graph/validate.ts +80 -0
  72. package/src/index.ts +27 -0
  73. package/src/routes/chat.ts +38 -0
  74. package/src/routes/files.ts +105 -0
  75. package/src/routes/index.ts +4 -0
  76. package/src/routes/threads.ts +69 -0
  77. package/src/routes/ws.ts +33 -0
  78. package/src/types.ts +50 -0
  79. package/src/ws/connections.ts +23 -0
  80. package/src/ws/events.ts +6 -0
  81. package/src/ws/index.ts +7 -0
  82. package/src/ws/notify.ts +9 -0
package/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # @m6d/cortex-server
2
+
3
+ Multi-agent AI chat server built on Hono + Bun. Supports MSSQL persistence, MinIO file storage, Neo4j knowledge graphs, and AI streaming with tool execution.
4
+
5
+ ## Usage
6
+
7
+ ```ts
8
+ import { createCortex } from "@m6d/cortex-server";
9
+
10
+ const cortex = createCortex({
11
+ database: {
12
+ type: "mssql",
13
+ connectionString: process.env.DB_CONNECTION_STRING!,
14
+ },
15
+ storage: {
16
+ endPoint: "localhost",
17
+ port: 9000,
18
+ useSSL: false,
19
+ accessKey: "minioadmin",
20
+ secretKey: "minioadmin",
21
+ },
22
+ auth: {
23
+ jwksUri: "https://your-auth-provider/.well-known/jwks.json",
24
+ issuer: "https://your-auth-provider/",
25
+ },
26
+ model: {
27
+ baseURL: "https://api.openai.com/v1",
28
+ apiKey: process.env.MODEL_KEY!,
29
+ modelName: "gpt-4o",
30
+ },
31
+ embedding: {
32
+ baseURL: "https://api.openai.com/v1",
33
+ apiKey: process.env.EMBEDDING_KEY!,
34
+ modelName: "text-embedding-3-small",
35
+ dimension: 1536,
36
+ },
37
+ neo4j: {
38
+ url: "bolt://localhost:7687",
39
+ user: "neo4j",
40
+ password: "password",
41
+ },
42
+ agents: {
43
+ assistant: {
44
+ systemPrompt: "You are a helpful assistant.",
45
+ tools: {},
46
+ },
47
+ },
48
+ });
49
+
50
+ const server = await cortex.serve();
51
+
52
+ export default {
53
+ fetch: server.fetch,
54
+ websocket: server.websocket,
55
+ };
56
+ ```
57
+
58
+ `cortex.serve()` is async — it runs database migrations on startup.
59
+
60
+ Each key in `agents` becomes a route prefix (e.g. `assistant` → `/agents/assistant/...`). Agents can define per-agent `systemPrompt`, `tools`, `backendFetch`, `loadSessionData`, and lifecycle hooks (`onToolCall`, `onStreamFinish`).
61
+
62
+ ## Requirements
63
+
64
+ - Bun >= 1.0.0
@@ -0,0 +1 @@
1
+ export * from "./src/index";
@@ -0,0 +1,27 @@
1
+ import type { ToolUIPart, UIMessage } from "ai";
2
+ import type { Thread, StoredMessage, CapturedFileInput } from "../types";
3
+ export type DatabaseAdapter = {
4
+ threads: {
5
+ list(userId: string, agentId: string): Promise<Thread[]>;
6
+ getById(userId: string, threadId: string): Promise<Thread | null>;
7
+ create(userId: string, agentId: string): Promise<Thread>;
8
+ delete(userId: string, threadId: string): Promise<void>;
9
+ updateTitle(threadId: string, title: string): Promise<void>;
10
+ updateSession(threadId: string, session: Record<string, unknown>): Promise<void>;
11
+ };
12
+ messages: {
13
+ list(userId: string, threadId: string, opts?: {
14
+ limit?: number;
15
+ }): Promise<StoredMessage[]>;
16
+ upsert(threadId: string, messages: UIMessage[]): Promise<void>;
17
+ };
18
+ capturedFiles: {
19
+ create(userId: string, messageId: string, toolPart: ToolUIPart, files: CapturedFileInput[]): Promise<{
20
+ id: string;
21
+ uploadId: string;
22
+ }[]>;
23
+ getById(id: string, userId: string): Promise<{
24
+ name: string;
25
+ } | null>;
26
+ };
27
+ };
@@ -0,0 +1,10 @@
1
+ import type { StorageAdapter } from "./storage";
2
+ export type MinioConfig = {
3
+ endPoint: string;
4
+ port: number;
5
+ useSSL: boolean;
6
+ accessKey: string;
7
+ secretKey: string;
8
+ bucketName?: string;
9
+ };
10
+ export declare function createMinioAdapter(config: MinioConfig): StorageAdapter;
@@ -0,0 +1,3 @@
1
+ import type { DatabaseAdapter } from "./database";
2
+ import type { StorageAdapter } from "./storage";
3
+ export declare function createMssqlAdapter(connectionString: string, storage: StorageAdapter): DatabaseAdapter;
@@ -0,0 +1,6 @@
1
+ export type StorageAdapter = {
2
+ put(path: string, data: string): Promise<boolean>;
3
+ get(path: string): Promise<string | false>;
4
+ delete(paths: string[]): Promise<boolean>;
5
+ stream(path: string): Promise<ReadableStream>;
6
+ };
@@ -0,0 +1,2 @@
1
+ import type { ResolvedCortexAgentConfig } from "../config.ts";
2
+ export declare function fetchBackend(path: string, backendFetch: NonNullable<ResolvedCortexAgentConfig["backendFetch"]>, token: string, options?: RequestInit): Promise<Response>;
@@ -0,0 +1,5 @@
1
+ import type { ResolvedCortexAgentConfig } from "../config.ts";
2
+ export declare function createModel(config: ResolvedCortexAgentConfig["model"]): import("@ai-sdk/provider").LanguageModelV3;
3
+ export declare function createEmbeddingModel(config: ResolvedCortexAgentConfig["embedding"]): import("@ai-sdk/provider").EmbeddingModelV3;
4
+ export declare function streamToBase64(stream: ReadableStream<Uint8Array>): Promise<string>;
5
+ export declare function tokenToUserId(token: string): string;
@@ -0,0 +1,4 @@
1
+ import type { ResolvedCortexAgentConfig } from "../config.ts";
2
+ import type { Thread } from "../types.ts";
3
+ export declare function stream(messages: unknown[], thread: Thread, userId: string, token: string, config: ResolvedCortexAgentConfig): Promise<Response>;
4
+ export declare function generateTitle(threadId: string, prompt: string, userId: string, config: ResolvedCortexAgentConfig): Promise<void>;
@@ -0,0 +1,11 @@
1
+ import type { DatabaseAdapter } from "../../adapters/database.ts";
2
+ import type { StorageAdapter } from "../../adapters/storage.ts";
3
+ export type ResolvedFile = {
4
+ name: string;
5
+ bytes: string;
6
+ };
7
+ export declare function createCapturedFileInterceptor(db: DatabaseAdapter, storage: StorageAdapter, options?: {
8
+ transformFile?: (file: ResolvedFile) => unknown;
9
+ }): (body: Record<string, unknown>, context: {
10
+ token: string;
11
+ }) => Promise<Record<string, unknown>>;
@@ -0,0 +1,4 @@
1
+ import type { ResolvedContext } from "../graph/resolver.ts";
2
+ import type { ResolvedCortexAgentConfig } from "../config.ts";
3
+ import type { Thread } from "../types.ts";
4
+ export declare function buildSystemPrompt(config: ResolvedCortexAgentConfig, prompt: string, thread: Thread, token: string, resolved: ResolvedContext | null): Promise<string>;
@@ -0,0 +1,7 @@
1
+ import type { ResolvedCortexAgentConfig } from "../../config.ts";
2
+ export declare function createCallEndpointTool(backendFetch: NonNullable<ResolvedCortexAgentConfig["backendFetch"]>, token: string): import("ai").Tool<{
3
+ method: "GET" | "POST" | "PUT" | "DELETE";
4
+ path: string;
5
+ body?: string | undefined;
6
+ queryParams?: string | undefined;
7
+ }, string>;
@@ -0,0 +1,6 @@
1
+ export declare const captureFilesTool: import("ai").Tool<{
2
+ files: {
3
+ id: string;
4
+ label: string;
5
+ }[];
6
+ }, never>;
@@ -0,0 +1,4 @@
1
+ import type { ResolvedCortexAgentConfig } from "../../config.ts";
2
+ export declare function createExecuteCodeTool(backendFetch: NonNullable<ResolvedCortexAgentConfig["backendFetch"]>, token: string): import("ai").Tool<{
3
+ code: string;
4
+ }, string>;
@@ -0,0 +1,5 @@
1
+ import type { Neo4jClient } from "../../graph/neo4j.ts";
2
+ export declare function createQueryGraphTool(neo4j: Neo4jClient): import("ai").Tool<{
3
+ query: string;
4
+ parameters?: string | undefined;
5
+ }, string>;
@@ -0,0 +1,4 @@
1
+ import type { AppEnv, AuthedAppEnv } from "../types";
2
+ import type { CortexConfig } from "../config";
3
+ export declare function createUserLoaderMiddleware(authConfig: CortexConfig["auth"]): import("hono").MiddlewareHandler<AppEnv, string, {}, Response>;
4
+ export declare const requireAuth: import("hono").MiddlewareHandler<AuthedAppEnv, string, {}, Response>;
@@ -0,0 +1,6 @@
1
+ export type ExtractEndpointsOptions = {
2
+ swaggerUrl: string;
3
+ domainsDir: string;
4
+ write?: boolean;
5
+ };
6
+ export declare function extractEndpoints(options: ExtractEndpointsOptions): Promise<void>;
@@ -0,0 +1,145 @@
1
+ import type { UIMessage } from "ai";
2
+ import type { DatabaseAdapter } from "./adapters/database";
3
+ import type { StorageAdapter } from "./adapters/storage";
4
+ import type { DomainDef } from "./graph/types.ts";
5
+ export type KnowledgeConfig = {
6
+ swagger?: {
7
+ url: string;
8
+ };
9
+ domains?: Record<string, DomainDef>;
10
+ };
11
+ export type DatabaseConfig = {
12
+ type: "mssql";
13
+ connectionString: string;
14
+ };
15
+ export type StorageConfig = {
16
+ endPoint: string;
17
+ port: number;
18
+ useSSL: boolean;
19
+ accessKey: string;
20
+ secretKey: string;
21
+ bucketName?: string;
22
+ };
23
+ export type CortexAgentDefinition = {
24
+ systemPrompt: string | ((session: Record<string, unknown> | null) => string | Promise<string>);
25
+ tools?: Record<string, unknown>;
26
+ backendFetch?: {
27
+ baseUrl: string;
28
+ apiKey: string;
29
+ headers?: Record<string, string>;
30
+ transformRequestBody?: (body: Record<string, unknown>, context: {
31
+ token: string;
32
+ }) => Promise<Record<string, unknown>>;
33
+ };
34
+ loadSessionData?: (token: string) => Promise<Record<string, unknown>>;
35
+ onToolCall?: (toolCall: {
36
+ toolName: string;
37
+ toolCallId: string;
38
+ args: Record<string, unknown>;
39
+ }) => void;
40
+ onStreamFinish?: (result: {
41
+ messages: UIMessage[];
42
+ isAborted: boolean;
43
+ }) => void;
44
+ model?: {
45
+ baseURL: string;
46
+ apiKey: string;
47
+ modelName: string;
48
+ providerName?: string;
49
+ };
50
+ embedding?: {
51
+ baseURL: string;
52
+ apiKey: string;
53
+ modelName: string;
54
+ dimension: number;
55
+ };
56
+ neo4j?: {
57
+ url: string;
58
+ user: string;
59
+ password: string;
60
+ };
61
+ reranker?: {
62
+ url: string;
63
+ apiKey: string;
64
+ };
65
+ knowledge?: KnowledgeConfig | null;
66
+ };
67
+ export type ResolvedCortexAgentConfig = {
68
+ db: DatabaseAdapter;
69
+ storage: StorageAdapter;
70
+ model: {
71
+ baseURL: string;
72
+ apiKey: string;
73
+ modelName: string;
74
+ providerName?: string;
75
+ };
76
+ embedding: {
77
+ baseURL: string;
78
+ apiKey: string;
79
+ modelName: string;
80
+ dimension: number;
81
+ };
82
+ neo4j: {
83
+ url: string;
84
+ user: string;
85
+ password: string;
86
+ };
87
+ reranker?: {
88
+ url: string;
89
+ apiKey: string;
90
+ };
91
+ systemPrompt: string | ((session: Record<string, unknown> | null) => string | Promise<string>);
92
+ tools?: Record<string, unknown>;
93
+ backendFetch?: {
94
+ baseUrl: string;
95
+ apiKey: string;
96
+ headers?: Record<string, string>;
97
+ transformRequestBody?: (body: Record<string, unknown>, context: {
98
+ token: string;
99
+ }) => Promise<Record<string, unknown>>;
100
+ };
101
+ loadSessionData?: (token: string) => Promise<Record<string, unknown>>;
102
+ onToolCall?: (toolCall: {
103
+ toolName: string;
104
+ toolCallId: string;
105
+ args: Record<string, unknown>;
106
+ }) => void;
107
+ onStreamFinish?: (result: {
108
+ messages: UIMessage[];
109
+ isAborted: boolean;
110
+ }) => void;
111
+ knowledge?: KnowledgeConfig;
112
+ };
113
+ export type CortexConfig = {
114
+ database: DatabaseConfig;
115
+ storage: StorageConfig;
116
+ auth: {
117
+ jwksUri: string;
118
+ issuer: string;
119
+ tokenExtractor?: (req: Request) => string | null;
120
+ cookieName?: string;
121
+ };
122
+ model: {
123
+ baseURL: string;
124
+ apiKey: string;
125
+ modelName: string;
126
+ providerName?: string;
127
+ };
128
+ embedding: {
129
+ baseURL: string;
130
+ apiKey: string;
131
+ modelName: string;
132
+ dimension: number;
133
+ };
134
+ neo4j: {
135
+ url: string;
136
+ user: string;
137
+ password: string;
138
+ };
139
+ reranker?: {
140
+ url: string;
141
+ apiKey: string;
142
+ };
143
+ knowledge?: KnowledgeConfig;
144
+ agents: Record<string, CortexAgentDefinition>;
145
+ };
@@ -0,0 +1 @@
1
+ export declare function runMigrations(connectionString: string): Promise<void>;