@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.
- package/README.md +64 -0
- package/dist/index.d.ts +1 -0
- package/dist/src/adapters/database.d.ts +27 -0
- package/dist/src/adapters/minio.d.ts +10 -0
- package/dist/src/adapters/mssql.d.ts +3 -0
- package/dist/src/adapters/storage.d.ts +6 -0
- package/dist/src/ai/fetch.d.ts +2 -0
- package/dist/src/ai/helpers.d.ts +5 -0
- package/dist/src/ai/index.d.ts +4 -0
- package/dist/src/ai/interceptors/resolve-captured-files.d.ts +11 -0
- package/dist/src/ai/prompt.d.ts +4 -0
- package/dist/src/ai/tools/call-endpoint.tool.d.ts +7 -0
- package/dist/src/ai/tools/capture-files.tool.d.ts +6 -0
- package/dist/src/ai/tools/execute-code.tool.d.ts +4 -0
- package/dist/src/ai/tools/query-graph.tool.d.ts +5 -0
- package/dist/src/auth/middleware.d.ts +4 -0
- package/dist/src/cli/extract-endpoints.d.ts +6 -0
- package/dist/src/config.d.ts +145 -0
- package/dist/src/db/migrate.d.ts +1 -0
- package/dist/src/db/schema.d.ts +345 -0
- package/dist/src/factory.d.ts +17 -0
- package/dist/src/graph/generate-cypher.d.ts +22 -0
- package/dist/src/graph/helpers.d.ts +60 -0
- package/dist/src/graph/index.d.ts +11 -0
- package/dist/src/graph/neo4j.d.ts +18 -0
- package/dist/src/graph/resolver.d.ts +51 -0
- package/dist/src/graph/seed.d.ts +19 -0
- package/dist/src/graph/types.d.ts +104 -0
- package/dist/src/graph/validate.d.ts +2 -0
- package/dist/src/index.d.ts +10 -0
- package/dist/src/routes/chat.d.ts +3 -0
- package/dist/src/routes/files.d.ts +3 -0
- package/dist/src/routes/index.d.ts +4 -0
- package/dist/src/routes/threads.d.ts +3 -0
- package/dist/src/routes/ws.d.ts +3 -0
- package/dist/src/types.d.ts +56 -0
- package/dist/src/ws/connections.d.ts +4 -0
- package/dist/src/ws/events.d.ts +8 -0
- package/dist/src/ws/index.d.ts +3 -0
- package/dist/src/ws/notify.d.ts +2 -0
- package/index.ts +1 -0
- package/package.json +57 -0
- package/src/adapters/database.ts +33 -0
- package/src/adapters/minio.ts +89 -0
- package/src/adapters/mssql.ts +203 -0
- package/src/adapters/storage.ts +6 -0
- package/src/ai/fetch.ts +39 -0
- package/src/ai/helpers.ts +36 -0
- package/src/ai/index.ts +145 -0
- package/src/ai/interceptors/resolve-captured-files.ts +64 -0
- package/src/ai/prompt.ts +120 -0
- package/src/ai/tools/call-endpoint.tool.ts +96 -0
- package/src/ai/tools/capture-files.tool.ts +22 -0
- package/src/ai/tools/execute-code.tool.ts +108 -0
- package/src/ai/tools/query-graph.tool.ts +35 -0
- package/src/auth/middleware.ts +63 -0
- package/src/cli/extract-endpoints.ts +588 -0
- package/src/config.ts +155 -0
- package/src/db/migrate.ts +21 -0
- package/src/db/migrations/20260309012148_cloudy_maria_hill/migration.sql +36 -0
- package/src/db/migrations/20260309012148_cloudy_maria_hill/snapshot.json +305 -0
- package/src/db/schema.ts +77 -0
- package/src/factory.ts +159 -0
- package/src/graph/generate-cypher.ts +179 -0
- package/src/graph/helpers.ts +68 -0
- package/src/graph/index.ts +47 -0
- package/src/graph/neo4j.ts +117 -0
- package/src/graph/resolver.ts +357 -0
- package/src/graph/seed.ts +172 -0
- package/src/graph/types.ts +152 -0
- package/src/graph/validate.ts +80 -0
- package/src/index.ts +27 -0
- package/src/routes/chat.ts +38 -0
- package/src/routes/files.ts +105 -0
- package/src/routes/index.ts +4 -0
- package/src/routes/threads.ts +69 -0
- package/src/routes/ws.ts +33 -0
- package/src/types.ts +50 -0
- package/src/ws/connections.ts +23 -0
- package/src/ws/events.ts +6 -0
- package/src/ws/index.ts +7 -0
- 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
|
package/dist/index.d.ts
ADDED
|
@@ -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,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,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,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>;
|