@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.
- package/README.md +8 -3
- package/contracts/README.md +23 -0
- package/contracts/graph/embed.ts +50 -0
- package/{src → contracts}/graph/helpers.ts +4 -10
- package/{src → contracts}/graph/neo4j.ts +14 -27
- package/contracts/graph/schema.ts +65 -0
- package/{src → contracts}/graph/types.ts +2 -29
- package/contracts/graph.ts +36 -0
- package/contracts/runtime.ts +208 -0
- package/contracts/wire.ts +143 -0
- package/dist/contracts/graph/embed.d.ts +22 -0
- package/dist/{src → contracts}/graph/helpers.d.ts +14 -12
- package/dist/{src → contracts}/graph/neo4j.d.ts +5 -5
- package/dist/contracts/graph/schema.d.ts +62 -0
- package/dist/{src → contracts}/graph/types.d.ts +1 -1
- package/dist/contracts/graph.d.ts +14 -0
- package/dist/contracts/runtime.d.ts +278 -0
- package/dist/contracts/wire.d.ts +120 -0
- package/dist/index.d.ts +1 -1
- package/dist/src/lib/adapters/database/index.d.ts +61 -0
- package/dist/src/lib/adapters/database/message-content.d.ts +30 -0
- package/dist/src/lib/adapters/database/mssql/attachments.d.ts +100 -0
- package/dist/src/lib/adapters/database/mssql/client.d.ts +8 -0
- package/dist/src/lib/adapters/database/mssql/index.d.ts +194 -0
- package/dist/src/lib/adapters/database/mssql/llm-requests.d.ts +17 -0
- package/dist/src/lib/adapters/database/mssql/messages.d.ts +37 -0
- package/dist/src/lib/adapters/database/mssql/threads.d.ts +50 -0
- package/dist/src/lib/adapters/database/postgres/attachments.d.ts +113 -0
- package/dist/src/lib/adapters/database/postgres/client.d.ts +8 -0
- package/dist/src/lib/adapters/database/postgres/index.d.ts +207 -0
- package/dist/src/lib/adapters/database/postgres/llm-requests.d.ts +17 -0
- package/dist/src/lib/adapters/database/postgres/messages.d.ts +37 -0
- package/dist/src/lib/adapters/database/postgres/threads.d.ts +50 -0
- package/{src/adapters/storage.ts → dist/src/lib/adapters/storage/index.d.ts} +1 -1
- package/dist/src/{adapters → lib/adapters/storage}/minio.d.ts +1 -1
- package/dist/src/lib/ai/active-runs.d.ts +63 -0
- package/dist/src/lib/ai/attachments.d.ts +30 -0
- package/dist/src/lib/ai/backend-url.d.ts +16 -0
- package/dist/src/lib/ai/cc-runtime.d.ts +61 -0
- package/dist/src/lib/ai/commit-gate.d.ts +21 -0
- package/dist/src/{ai → lib/ai}/context/builder.d.ts +6 -7
- package/dist/src/lib/ai/context/compression-middleware.d.ts +15 -0
- package/dist/src/lib/ai/context/compressor.d.ts +7 -0
- package/dist/src/lib/ai/context/intra-turn-compressor.d.ts +24 -0
- package/dist/src/lib/ai/context/optimize-thread-context.d.ts +9 -0
- package/dist/src/lib/ai/context/summarizer.d.ts +5 -0
- package/dist/src/{ai → lib/ai}/context/token-estimator.d.ts +5 -8
- package/dist/src/{ai → lib/ai}/context/types.d.ts +2 -0
- package/dist/src/{ai → lib/ai}/fetch.d.ts +1 -1
- package/dist/src/lib/ai/finish-turn.d.ts +23 -0
- package/dist/src/lib/ai/helpers.d.ts +38 -0
- package/dist/src/lib/ai/index.d.ts +7 -0
- package/dist/src/lib/ai/inspector.d.ts +41 -0
- package/dist/src/lib/ai/interceptors/request-interceptor.d.ts +13 -0
- package/dist/src/{ai → lib/ai}/prompt.d.ts +5 -4
- package/dist/src/lib/ai/redis-runs.d.ts +41 -0
- package/dist/src/lib/ai/redis-stream-log.d.ts +39 -0
- package/dist/src/lib/ai/tools/execute-code.tool.d.ts +18 -0
- package/dist/src/lib/ai/tools/query-graph.tool.d.ts +13 -0
- package/dist/src/lib/ai/tools/read-attachment.tool.d.ts +21 -0
- package/dist/src/lib/ai/tools/search-common.d.ts +17 -0
- package/dist/src/lib/ai/tools/search-knowledge.tool.d.ts +15 -0
- package/dist/src/lib/ai/tools/search-services.tool.d.ts +12 -0
- package/dist/src/lib/ai/tools/search-tools.tool.d.ts +12 -0
- package/dist/src/lib/ai/turn-tools.d.ts +39 -0
- package/dist/src/lib/ai/vision/rasterize-pdf.d.ts +26 -0
- package/dist/src/lib/ai/vision/rasterize-pdf.worker.d.ts +1 -0
- package/dist/src/lib/ai/vision/vision-reader.d.ts +45 -0
- package/dist/src/lib/cc/client.d.ts +183 -0
- package/dist/src/lib/cc/config-cache.d.ts +20 -0
- package/dist/src/lib/cc/format.d.ts +23 -0
- package/dist/src/lib/cc/registry.d.ts +34 -0
- package/dist/src/lib/cc/types.d.ts +27 -0
- package/dist/src/lib/config.d.ts +170 -0
- package/dist/src/lib/db/drizzle.config.mssql.d.ts +2 -0
- package/dist/src/lib/db/drizzle.config.pg.d.ts +2 -0
- package/dist/src/lib/db/migrate.d.ts +7 -0
- package/dist/src/{db/schema.d.ts → lib/db/schema.mssql.d.ts} +236 -24
- package/dist/src/lib/db/schema.pg.d.ts +546 -0
- package/dist/src/{factory.d.ts → lib/factory.d.ts} +2 -12
- package/dist/src/lib/graph/index.d.ts +10 -0
- package/dist/src/{graph → lib/graph}/resolver.d.ts +2 -3
- package/dist/src/{index.d.ts → lib/index.d.ts} +3 -4
- package/dist/src/lib/redis.d.ts +19 -0
- package/dist/src/{routes → lib/routes}/chat.d.ts +1 -1
- package/dist/src/{routes → lib/routes}/files.d.ts +1 -1
- package/dist/src/lib/routes/owned-thread.d.ts +17 -0
- package/dist/src/{routes → lib/routes}/threads.d.ts +1 -1
- package/dist/src/{routes → lib/routes}/ws.d.ts +1 -1
- package/dist/src/lib/types.d.ts +58 -0
- package/dist/src/{ws → lib/ws}/connections.d.ts +2 -1
- package/index.ts +1 -1
- package/package.json +35 -15
- package/src/lib/adapters/database/index.ts +78 -0
- package/src/lib/adapters/database/message-content.ts +39 -0
- package/src/lib/adapters/database/mssql/attachments.ts +90 -0
- package/src/lib/adapters/database/mssql/client.ts +11 -0
- package/src/lib/adapters/database/mssql/index.ts +23 -0
- package/src/lib/adapters/database/mssql/llm-requests.ts +43 -0
- package/src/lib/adapters/database/mssql/messages.ts +92 -0
- package/src/lib/adapters/database/mssql/threads.ts +83 -0
- package/src/lib/adapters/database/postgres/attachments.ts +88 -0
- package/src/lib/adapters/database/postgres/client.ts +11 -0
- package/src/lib/adapters/database/postgres/index.ts +23 -0
- package/src/lib/adapters/database/postgres/llm-requests.ts +45 -0
- package/src/lib/adapters/database/postgres/messages.ts +92 -0
- package/src/lib/adapters/database/postgres/threads.ts +84 -0
- package/{dist/src/adapters/storage.d.ts → src/lib/adapters/storage/index.ts} +1 -1
- package/src/{adapters → lib/adapters/storage}/minio.ts +7 -6
- package/src/lib/ai/active-runs.ts +157 -0
- package/src/lib/ai/attachments.ts +144 -0
- package/src/lib/ai/backend-url.ts +27 -0
- package/src/lib/ai/cc-runtime.ts +63 -0
- package/src/lib/ai/commit-gate.ts +116 -0
- package/src/{ai → lib/ai}/context/builder.ts +12 -14
- package/src/lib/ai/context/compression-middleware.ts +44 -0
- package/src/lib/ai/context/compressor.ts +267 -0
- package/src/lib/ai/context/intra-turn-compressor.ts +181 -0
- package/src/lib/ai/context/optimize-thread-context.ts +50 -0
- package/src/lib/ai/context/summarizer.ts +48 -0
- package/src/lib/ai/context/token-estimator.ts +65 -0
- package/src/{ai → lib/ai}/context/types.ts +4 -1
- package/src/{ai → lib/ai}/fetch.ts +10 -5
- package/src/lib/ai/finish-turn.ts +75 -0
- package/src/lib/ai/helpers.ts +100 -0
- package/src/lib/ai/index.ts +408 -0
- package/src/lib/ai/inspector.ts +112 -0
- package/src/lib/ai/interceptors/request-interceptor.ts +89 -0
- package/src/lib/ai/prompt.ts +355 -0
- package/src/lib/ai/redis-runs.ts +167 -0
- package/src/lib/ai/redis-stream-log.ts +219 -0
- package/src/lib/ai/tools/execute-code.tool.ts +267 -0
- package/src/{ai → lib/ai}/tools/query-graph.tool.ts +7 -8
- package/src/lib/ai/tools/read-attachment.tool.ts +59 -0
- package/src/lib/ai/tools/search-common.ts +15 -0
- package/src/lib/ai/tools/search-knowledge.tool.ts +33 -0
- package/src/lib/ai/tools/search-services.tool.ts +31 -0
- package/src/lib/ai/tools/search-tools.tool.ts +29 -0
- package/src/lib/ai/turn-tools.ts +154 -0
- package/src/lib/ai/vision/rasterize-pdf.ts +69 -0
- package/src/lib/ai/vision/rasterize-pdf.worker.ts +81 -0
- package/src/lib/ai/vision/vision-reader.ts +130 -0
- package/src/{auth → lib/auth}/middleware.ts +12 -3
- package/src/lib/cc/client.ts +273 -0
- package/src/lib/cc/config-cache.ts +32 -0
- package/src/lib/cc/format.ts +96 -0
- package/src/lib/cc/registry.ts +62 -0
- package/src/lib/cc/types.ts +44 -0
- package/src/lib/config.ts +212 -0
- package/src/lib/db/drizzle.config.mssql.ts +9 -0
- package/src/lib/db/drizzle.config.pg.ts +9 -0
- package/src/lib/db/migrate.ts +56 -0
- package/src/{db/migrations/20260309012148_cloudy_maria_hill → lib/db/migrations/mssql/20260326231647_nice_speedball}/migration.sql +12 -0
- package/src/{db/migrations/20260309012148_cloudy_maria_hill → lib/db/migrations/mssql/20260326231647_nice_speedball}/snapshot.json +106 -1
- package/src/lib/db/migrations/mssql/20260729092726_talented_wilson_fisk/migration.sql +20 -0
- package/src/lib/db/migrations/mssql/20260729092726_talented_wilson_fisk/snapshot.json +497 -0
- package/src/lib/db/migrations/mssql/20260801003652_perpetual_blue_shield/migration.sql +1 -0
- package/src/lib/db/migrations/mssql/20260801003652_perpetual_blue_shield/snapshot.json +516 -0
- package/src/lib/db/migrations/mssql/20260812174642_user_id_text/migration.sql +2 -0
- package/src/lib/db/migrations/mssql/20260812174642_user_id_text/snapshot.json +516 -0
- package/src/lib/db/migrations/pg/20260801084117_thick_ben_grimm/migration.sql +53 -0
- package/src/lib/db/migrations/pg/20260801084117_thick_ben_grimm/snapshot.json +584 -0
- package/src/lib/db/migrations/pg/20260812174622_user_id_text/migration.sql +2 -0
- package/src/lib/db/migrations/pg/20260812174622_user_id_text/snapshot.json +584 -0
- package/src/lib/db/schema.mssql.ts +94 -0
- package/src/lib/db/schema.pg.ts +95 -0
- package/src/lib/factory.ts +127 -0
- package/src/lib/graph/index.ts +19 -0
- package/src/lib/graph/resolver.ts +387 -0
- package/src/{index.ts → lib/index.ts} +13 -5
- package/src/lib/redis.ts +44 -0
- package/src/lib/routes/chat.ts +115 -0
- package/src/lib/routes/files.ts +129 -0
- package/src/lib/routes/owned-thread.ts +17 -0
- package/src/{routes → lib/routes}/threads.ts +22 -31
- package/src/{routes → lib/routes}/ws.ts +9 -3
- package/src/lib/types.ts +76 -0
- package/src/{ws → lib/ws}/connections.ts +9 -1
- package/tsconfig.json +48 -0
- package/dist/src/adapters/database.d.ts +0 -30
- package/dist/src/adapters/mssql.d.ts +0 -3
- package/dist/src/ai/active-streams.d.ts +0 -14
- package/dist/src/ai/context/compressor.d.ts +0 -7
- package/dist/src/ai/context/index.d.ts +0 -15
- package/dist/src/ai/context/summarizer.d.ts +0 -5
- package/dist/src/ai/helpers.d.ts +0 -5
- package/dist/src/ai/index.d.ts +0 -4
- package/dist/src/ai/interceptors/request-interceptor.d.ts +0 -12
- package/dist/src/ai/tools/call-endpoint.tool.d.ts +0 -7
- package/dist/src/ai/tools/capture-files.tool.d.ts +0 -6
- package/dist/src/ai/tools/execute-code.tool.d.ts +0 -4
- package/dist/src/ai/tools/query-graph.tool.d.ts +0 -5
- package/dist/src/cli/extract-endpoints.d.ts +0 -6
- package/dist/src/config.d.ts +0 -165
- package/dist/src/db/migrate.d.ts +0 -1
- package/dist/src/graph/expand-domains.d.ts +0 -2
- package/dist/src/graph/generate-cypher.d.ts +0 -22
- package/dist/src/graph/index.d.ts +0 -11
- package/dist/src/graph/seed.d.ts +0 -19
- package/dist/src/graph/validate.d.ts +0 -2
- package/dist/src/routes/index.d.ts +0 -4
- package/dist/src/types.d.ts +0 -74
- package/dist/src/ws/events.d.ts +0 -39
- package/dist/src/ws/index.d.ts +0 -3
- package/dist/src/ws/notify.d.ts +0 -2
- package/src/adapters/database.ts +0 -29
- package/src/adapters/mssql.ts +0 -195
- package/src/ai/active-streams.ts +0 -123
- package/src/ai/context/compressor.ts +0 -47
- package/src/ai/context/index.ts +0 -75
- package/src/ai/context/summarizer.ts +0 -50
- package/src/ai/context/token-estimator.ts +0 -60
- package/src/ai/helpers.ts +0 -32
- package/src/ai/index.ts +0 -277
- package/src/ai/interceptors/request-interceptor.ts +0 -61
- package/src/ai/prompt.ts +0 -126
- package/src/ai/tools/call-endpoint.tool.ts +0 -89
- package/src/ai/tools/capture-files.tool.ts +0 -20
- package/src/ai/tools/execute-code.tool.ts +0 -153
- package/src/cli/extract-endpoints.ts +0 -550
- package/src/config.ts +0 -182
- package/src/db/migrate.ts +0 -21
- package/src/db/migrations/20260315000000_add_context_meta/migration.sql +0 -1
- package/src/db/schema.ts +0 -70
- package/src/factory.ts +0 -171
- package/src/graph/expand-domains.ts +0 -276
- package/src/graph/generate-cypher.ts +0 -192
- package/src/graph/index.ts +0 -47
- package/src/graph/resolver.ts +0 -357
- package/src/graph/seed.ts +0 -162
- package/src/graph/validate.ts +0 -78
- package/src/routes/chat.ts +0 -86
- package/src/routes/files.ts +0 -100
- package/src/routes/index.ts +0 -4
- package/src/types.ts +0 -74
- package/src/ws/events.ts +0 -39
- package/src/ws/index.ts +0 -11
- package/src/ws/notify.ts +0 -9
- /package/dist/src/{auth → lib/auth}/middleware.d.ts +0 -0
package/src/config.ts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import type { Tool, ToolSet, 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
|
-
import type { RequestInterceptorOptions } from "./ai/interceptors/request-interceptor.ts";
|
|
6
|
-
import type { ContextConfig } from "./ai/context/types.ts";
|
|
7
|
-
|
|
8
|
-
export type KnowledgeConfig = {
|
|
9
|
-
swagger?: { url: string };
|
|
10
|
-
domains?: Record<string, DomainDef>;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type PromptContext<
|
|
14
|
-
TSession extends Record<string, unknown> = Record<string, unknown>,
|
|
15
|
-
TRequestContext extends Record<string, unknown> = Record<string, unknown>,
|
|
16
|
-
> = {
|
|
17
|
-
session: TSession | null;
|
|
18
|
-
requestContext: TRequestContext;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export type DatabaseConfig = {
|
|
22
|
-
type: "mssql";
|
|
23
|
-
connectionString: string;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export type StorageConfig = {
|
|
27
|
-
endPoint: string;
|
|
28
|
-
port: number;
|
|
29
|
-
useSSL: boolean;
|
|
30
|
-
accessKey: string;
|
|
31
|
-
secretKey: string;
|
|
32
|
-
bucketName?: string;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export type CortexAgentDefinition<
|
|
36
|
-
TSession extends Record<string, unknown> = Record<string, unknown>,
|
|
37
|
-
TRequestContext extends Record<string, unknown> = Record<string, unknown>,
|
|
38
|
-
> = {
|
|
39
|
-
systemPrompt:
|
|
40
|
-
| string
|
|
41
|
-
| ((context: PromptContext<TSession, TRequestContext>) => string | Promise<string>);
|
|
42
|
-
tools?: ToolSet;
|
|
43
|
-
backendFetch?: {
|
|
44
|
-
baseUrl: string;
|
|
45
|
-
apiKey: string;
|
|
46
|
-
headers?: Record<string, string>;
|
|
47
|
-
transformRequestBody?: (
|
|
48
|
-
body: Record<string, unknown>,
|
|
49
|
-
context: { token: string },
|
|
50
|
-
) => Promise<Record<string, unknown>>;
|
|
51
|
-
interceptor?: RequestInterceptorOptions;
|
|
52
|
-
};
|
|
53
|
-
loadSessionData?: (token: string) => Promise<TSession>;
|
|
54
|
-
resolveRequestContext?: (request: Request) => TRequestContext | Promise<TRequestContext>;
|
|
55
|
-
onToolCall?: (toolCall: {
|
|
56
|
-
toolName: string;
|
|
57
|
-
toolCallId: string;
|
|
58
|
-
args: Record<string, unknown>;
|
|
59
|
-
}) => void;
|
|
60
|
-
onStreamFinish?: (result: { messages: UIMessage[]; isAborted: boolean }) => void;
|
|
61
|
-
model?: {
|
|
62
|
-
baseURL: string;
|
|
63
|
-
apiKey: string;
|
|
64
|
-
modelName: string;
|
|
65
|
-
providerName?: string;
|
|
66
|
-
};
|
|
67
|
-
embedding?: {
|
|
68
|
-
baseURL: string;
|
|
69
|
-
apiKey: string;
|
|
70
|
-
modelName: string;
|
|
71
|
-
dimension: number;
|
|
72
|
-
};
|
|
73
|
-
neo4j?: {
|
|
74
|
-
url: string;
|
|
75
|
-
user: string;
|
|
76
|
-
password: string;
|
|
77
|
-
};
|
|
78
|
-
reranker?: {
|
|
79
|
-
url: string;
|
|
80
|
-
apiKey: string;
|
|
81
|
-
};
|
|
82
|
-
context?: Partial<ContextConfig>;
|
|
83
|
-
knowledge?: KnowledgeConfig | null;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
export type ResolvedCortexAgentConfig = {
|
|
87
|
-
db: DatabaseAdapter;
|
|
88
|
-
storage: StorageAdapter;
|
|
89
|
-
model: {
|
|
90
|
-
baseURL: string;
|
|
91
|
-
apiKey: string;
|
|
92
|
-
modelName: string;
|
|
93
|
-
providerName?: string;
|
|
94
|
-
};
|
|
95
|
-
embedding: {
|
|
96
|
-
baseURL: string;
|
|
97
|
-
apiKey: string;
|
|
98
|
-
modelName: string;
|
|
99
|
-
dimension: number;
|
|
100
|
-
};
|
|
101
|
-
neo4j: {
|
|
102
|
-
url: string;
|
|
103
|
-
user: string;
|
|
104
|
-
password: string;
|
|
105
|
-
};
|
|
106
|
-
reranker?: {
|
|
107
|
-
url: string;
|
|
108
|
-
apiKey: string;
|
|
109
|
-
};
|
|
110
|
-
systemPrompt: string | ((context: PromptContext) => string | Promise<string>);
|
|
111
|
-
tools?: ToolSet;
|
|
112
|
-
backendFetch?: {
|
|
113
|
-
baseUrl: string;
|
|
114
|
-
apiKey: string;
|
|
115
|
-
headers?: Record<string, string>;
|
|
116
|
-
transformRequestBody?: (
|
|
117
|
-
body: Record<string, unknown>,
|
|
118
|
-
context: { token: string },
|
|
119
|
-
) => Promise<Record<string, unknown>>;
|
|
120
|
-
interceptor?: RequestInterceptorOptions;
|
|
121
|
-
};
|
|
122
|
-
loadSessionData?: (token: string) => Promise<Record<string, unknown>>;
|
|
123
|
-
resolveRequestContext?: (
|
|
124
|
-
request: Request,
|
|
125
|
-
) => Record<string, unknown> | Promise<Record<string, unknown>>;
|
|
126
|
-
onToolCall?: (toolCall: {
|
|
127
|
-
toolName: string;
|
|
128
|
-
toolCallId: string;
|
|
129
|
-
args: Record<string, unknown>;
|
|
130
|
-
}) => void;
|
|
131
|
-
onStreamFinish?: (result: { messages: UIMessage[]; isAborted: boolean }) => void;
|
|
132
|
-
context: ContextConfig;
|
|
133
|
-
knowledge?: KnowledgeConfig;
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
export type CortexConfig = {
|
|
137
|
-
port?: number;
|
|
138
|
-
database: DatabaseConfig;
|
|
139
|
-
storage: StorageConfig;
|
|
140
|
-
auth: {
|
|
141
|
-
jwksUri: string;
|
|
142
|
-
issuer: string;
|
|
143
|
-
tokenExtractor?: (req: Request) => string | null;
|
|
144
|
-
cookieName?: string;
|
|
145
|
-
};
|
|
146
|
-
model: {
|
|
147
|
-
baseURL: string;
|
|
148
|
-
apiKey: string;
|
|
149
|
-
modelName: string;
|
|
150
|
-
providerName?: string;
|
|
151
|
-
};
|
|
152
|
-
embedding: {
|
|
153
|
-
baseURL: string;
|
|
154
|
-
apiKey: string;
|
|
155
|
-
modelName: string;
|
|
156
|
-
dimension: number;
|
|
157
|
-
};
|
|
158
|
-
neo4j: {
|
|
159
|
-
url: string;
|
|
160
|
-
user: string;
|
|
161
|
-
password: string;
|
|
162
|
-
};
|
|
163
|
-
reranker?: {
|
|
164
|
-
url: string;
|
|
165
|
-
apiKey: string;
|
|
166
|
-
};
|
|
167
|
-
context?: Partial<ContextConfig>;
|
|
168
|
-
knowledge?: KnowledgeConfig;
|
|
169
|
-
agents: Record<string, CortexAgentDefinition>;
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Helper to define an agent with full type inference for `systemPrompt` context.
|
|
174
|
-
* The `context.session` type is inferred from `loadSessionData`'s return type,
|
|
175
|
-
* and `context.requestContext` is inferred from `resolveRequestContext`'s return type.
|
|
176
|
-
*/
|
|
177
|
-
export function defineAgent<
|
|
178
|
-
TSession extends Record<string, unknown> = Record<string, unknown>,
|
|
179
|
-
TRequestContext extends Record<string, unknown> = Record<string, unknown>,
|
|
180
|
-
>(config: CortexAgentDefinition<TSession, TRequestContext>): CortexAgentDefinition {
|
|
181
|
-
return config as CortexAgentDefinition;
|
|
182
|
-
}
|
package/src/db/migrate.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import mssql from "mssql";
|
|
2
|
-
import { drizzle } from "drizzle-orm/node-mssql";
|
|
3
|
-
import { migrate } from "drizzle-orm/node-mssql/migrator";
|
|
4
|
-
import { resolve } from "path";
|
|
5
|
-
|
|
6
|
-
export async function runMigrations(connectionString: string) {
|
|
7
|
-
const pool = new mssql.ConnectionPool(connectionString);
|
|
8
|
-
await pool.connect();
|
|
9
|
-
|
|
10
|
-
try {
|
|
11
|
-
const db = drizzle({ client: pool, casing: "snake_case" });
|
|
12
|
-
const migrationsFolder = resolve(import.meta.dir, "migrations");
|
|
13
|
-
console.log("[cortex-server] Running migrations from:", migrationsFolder);
|
|
14
|
-
await migrate(db, { migrationsFolder });
|
|
15
|
-
console.log("[cortex-server] Migrations complete");
|
|
16
|
-
} catch (e) {
|
|
17
|
-
console.log(e);
|
|
18
|
-
} finally {
|
|
19
|
-
await pool.close();
|
|
20
|
-
}
|
|
21
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
ALTER TABLE [ai].[threads] ADD [context_meta] nvarchar(max);
|
package/src/db/schema.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import type { ToolUIPart, UIMessage } from "ai";
|
|
2
|
-
import { sql } from "drizzle-orm";
|
|
3
|
-
import { customType, datetime2, index, mssqlSchema, nvarchar } from "drizzle-orm/mssql-core";
|
|
4
|
-
import type { MessageMetadata } from "src/types";
|
|
5
|
-
import type { ThreadContextMeta } from "../ai/context/types.ts";
|
|
6
|
-
|
|
7
|
-
const uniqueIdentifier = customType<{ data: string }>({
|
|
8
|
-
dataType() {
|
|
9
|
-
return "UNIQUEIDENTIFIER";
|
|
10
|
-
},
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
export const aiSchema = mssqlSchema("ai");
|
|
14
|
-
|
|
15
|
-
const auditColumns = {
|
|
16
|
-
createdAt: datetime2({ mode: "date" })
|
|
17
|
-
.$default(() => new Date())
|
|
18
|
-
.notNull(),
|
|
19
|
-
updatedAt: datetime2({ mode: "date" })
|
|
20
|
-
.$default(() => new Date())
|
|
21
|
-
.$onUpdate(() => new Date())
|
|
22
|
-
.notNull(),
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export const threads = aiSchema.table("threads", {
|
|
26
|
-
id: uniqueIdentifier()
|
|
27
|
-
.default(sql`NEWID()`)
|
|
28
|
-
.primaryKey(),
|
|
29
|
-
userId: uniqueIdentifier().notNull(),
|
|
30
|
-
agentId: nvarchar({ length: 128 }).notNull().default("default"),
|
|
31
|
-
title: nvarchar({ length: 256 }),
|
|
32
|
-
session: nvarchar({ mode: "json", length: "max" }).$type<Record<string, unknown>>(),
|
|
33
|
-
contextMeta: nvarchar({ mode: "json", length: "max" }).$type<ThreadContextMeta>(),
|
|
34
|
-
...auditColumns,
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
export const messages = aiSchema.table("messages", {
|
|
38
|
-
id: nvarchar({ length: 128 }).primaryKey(),
|
|
39
|
-
threadId: uniqueIdentifier()
|
|
40
|
-
.references(() => threads.id, { onDelete: "cascade" })
|
|
41
|
-
.notNull(),
|
|
42
|
-
text: nvarchar({ length: "max" }),
|
|
43
|
-
content: nvarchar({ mode: "json", length: "max" })
|
|
44
|
-
.notNull()
|
|
45
|
-
.$type<UIMessage<MessageMetadata>>(),
|
|
46
|
-
role: nvarchar({
|
|
47
|
-
enum: ["system", "user", "assistant", "tool"],
|
|
48
|
-
length: 16,
|
|
49
|
-
}).notNull(),
|
|
50
|
-
...auditColumns,
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
export const capturedFiles = aiSchema.table(
|
|
54
|
-
"captured_files",
|
|
55
|
-
{
|
|
56
|
-
id: uniqueIdentifier()
|
|
57
|
-
.default(sql`NEWID()`)
|
|
58
|
-
.primaryKey(),
|
|
59
|
-
name: nvarchar({ length: 2048 }).notNull(),
|
|
60
|
-
userId: uniqueIdentifier().notNull(),
|
|
61
|
-
agentGeneratedId: nvarchar({ length: 1024 }).notNull(),
|
|
62
|
-
messageId: nvarchar({ length: 128 })
|
|
63
|
-
.references(() => messages.id, {
|
|
64
|
-
onDelete: "cascade",
|
|
65
|
-
})
|
|
66
|
-
.notNull(),
|
|
67
|
-
toolPart: nvarchar({ length: "max", mode: "json" }).notNull().$type<ToolUIPart>(),
|
|
68
|
-
},
|
|
69
|
-
(table) => [index(table.agentGeneratedId)],
|
|
70
|
-
);
|
package/src/factory.ts
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import { Hono } from "hono";
|
|
2
|
-
import { websocket } from "hono/bun";
|
|
3
|
-
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
4
|
-
import type { CortexConfig, ResolvedCortexAgentConfig } from "./config.ts";
|
|
5
|
-
import { DEFAULT_CONTEXT_CONFIG } from "./ai/context/types.ts";
|
|
6
|
-
import type { ContextConfig } from "./ai/context/types.ts";
|
|
7
|
-
import type { AppEnv, CortexAppEnv } from "./types.ts";
|
|
8
|
-
import type { DomainDef } from "./graph/types.ts";
|
|
9
|
-
import { createUserLoaderMiddleware } from "./auth/middleware.ts";
|
|
10
|
-
import { createThreadRoutes } from "./routes/threads.ts";
|
|
11
|
-
import { createChatRoutes } from "./routes/chat.ts";
|
|
12
|
-
import { createFileRoutes } from "./routes/files.ts";
|
|
13
|
-
import { createWsRoute } from "./routes/ws.ts";
|
|
14
|
-
import { runMigrations } from "./db/migrate.ts";
|
|
15
|
-
import { createMssqlAdapter } from "./adapters/mssql.ts";
|
|
16
|
-
import { createMinioAdapter } from "./adapters/minio.ts";
|
|
17
|
-
import { seedGraph as seedGraphFn } from "./graph/seed.ts";
|
|
18
|
-
import { extractEndpoints as extractEndpointsFn } from "./cli/extract-endpoints.ts";
|
|
19
|
-
|
|
20
|
-
export type CortexInstance = {
|
|
21
|
-
serve(): Promise<{
|
|
22
|
-
app: Hono<AppEnv>;
|
|
23
|
-
port: number;
|
|
24
|
-
fetch: Hono<AppEnv>["fetch"];
|
|
25
|
-
websocket: typeof websocket;
|
|
26
|
-
}>;
|
|
27
|
-
seedGraph(): Promise<void>;
|
|
28
|
-
extractEndpoints(options: { domainsDir: string; write?: boolean }): Promise<void>;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export function createCortex(config: CortexConfig) {
|
|
32
|
-
function collectAllDomains() {
|
|
33
|
-
const all: Record<string, DomainDef> = {};
|
|
34
|
-
if (config.knowledge?.domains) Object.assign(all, config.knowledge.domains);
|
|
35
|
-
for (const agent of Object.values(config.agents)) {
|
|
36
|
-
if (agent.knowledge && agent.knowledge.domains) {
|
|
37
|
-
Object.assign(all, agent.knowledge.domains);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return all;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function collectAllSwaggerUrls() {
|
|
44
|
-
const urls = new Set<string>();
|
|
45
|
-
if (config.knowledge?.swagger?.url) urls.add(config.knowledge.swagger.url);
|
|
46
|
-
for (const agent of Object.values(config.agents)) {
|
|
47
|
-
if (agent.knowledge && agent.knowledge.swagger?.url) {
|
|
48
|
-
urls.add(agent.knowledge.swagger.url);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return [...urls];
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return {
|
|
55
|
-
async serve() {
|
|
56
|
-
const storage = createMinioAdapter(config.storage);
|
|
57
|
-
const db = createMssqlAdapter(config.database.connectionString, storage);
|
|
58
|
-
|
|
59
|
-
await runMigrations(config.database.connectionString);
|
|
60
|
-
|
|
61
|
-
const app = new Hono<AppEnv>();
|
|
62
|
-
|
|
63
|
-
const userLoader = createUserLoaderMiddleware(config.auth);
|
|
64
|
-
app.use("*", userLoader);
|
|
65
|
-
|
|
66
|
-
// Compatibility WebSocket route
|
|
67
|
-
app.route("/", createWsRoute());
|
|
68
|
-
|
|
69
|
-
// Agent-scoped routes
|
|
70
|
-
const agentApp = new Hono<CortexAppEnv>();
|
|
71
|
-
|
|
72
|
-
agentApp.use("*", async function (c, next) {
|
|
73
|
-
const agentId = c.req.param("agentId") as string;
|
|
74
|
-
const agentDef = config.agents[agentId];
|
|
75
|
-
if (!agentDef) return c.json({ error: "Agent not found" }, 404);
|
|
76
|
-
|
|
77
|
-
const resolvedContext: ContextConfig = {
|
|
78
|
-
...DEFAULT_CONTEXT_CONFIG,
|
|
79
|
-
...config.context,
|
|
80
|
-
...agentDef.context,
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const resolvedConfig: ResolvedCortexAgentConfig = {
|
|
84
|
-
db,
|
|
85
|
-
storage,
|
|
86
|
-
model: agentDef.model ?? config.model,
|
|
87
|
-
embedding: agentDef.embedding ?? config.embedding,
|
|
88
|
-
neo4j: agentDef.neo4j ?? config.neo4j,
|
|
89
|
-
reranker: agentDef.reranker ?? config.reranker,
|
|
90
|
-
knowledge:
|
|
91
|
-
agentDef.knowledge === null
|
|
92
|
-
? undefined
|
|
93
|
-
: (agentDef.knowledge ?? config.knowledge),
|
|
94
|
-
systemPrompt: agentDef.systemPrompt,
|
|
95
|
-
tools: agentDef.tools,
|
|
96
|
-
backendFetch: agentDef.backendFetch,
|
|
97
|
-
loadSessionData: agentDef.loadSessionData,
|
|
98
|
-
resolveRequestContext: agentDef.resolveRequestContext,
|
|
99
|
-
onToolCall: agentDef.onToolCall,
|
|
100
|
-
onStreamFinish: agentDef.onStreamFinish,
|
|
101
|
-
context: resolvedContext,
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
c.set("agentConfig", resolvedConfig);
|
|
105
|
-
c.set("agentId", agentId);
|
|
106
|
-
await next();
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
agentApp.route("/", createThreadRoutes());
|
|
110
|
-
agentApp.route("/", createChatRoutes());
|
|
111
|
-
agentApp.route("/", createFileRoutes());
|
|
112
|
-
agentApp.route("/", createWsRoute({ useAgentParam: true }));
|
|
113
|
-
|
|
114
|
-
app.route("/agents/:agentId", agentApp);
|
|
115
|
-
|
|
116
|
-
const port = config.port ?? 3331;
|
|
117
|
-
|
|
118
|
-
return {
|
|
119
|
-
app,
|
|
120
|
-
port,
|
|
121
|
-
fetch: app.fetch,
|
|
122
|
-
websocket,
|
|
123
|
-
};
|
|
124
|
-
},
|
|
125
|
-
|
|
126
|
-
async seedGraph() {
|
|
127
|
-
const domains = collectAllDomains();
|
|
128
|
-
if (!Object.keys(domains).length) {
|
|
129
|
-
throw new Error("No domains found in knowledge config");
|
|
130
|
-
}
|
|
131
|
-
if (!config.neo4j) {
|
|
132
|
-
throw new Error("neo4j config is required for seedGraph");
|
|
133
|
-
}
|
|
134
|
-
if (!config.embedding) {
|
|
135
|
-
throw new Error("embedding config is required for seedGraph");
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const provider = createOpenAICompatible({
|
|
139
|
-
name: "embedding-provider",
|
|
140
|
-
baseURL: config.embedding.baseURL,
|
|
141
|
-
apiKey: config.embedding.apiKey,
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
await seedGraphFn(
|
|
145
|
-
{
|
|
146
|
-
neo4j: config.neo4j,
|
|
147
|
-
embedding: {
|
|
148
|
-
model: provider.textEmbeddingModel(config.embedding.modelName),
|
|
149
|
-
dimension: config.embedding.dimension,
|
|
150
|
-
},
|
|
151
|
-
},
|
|
152
|
-
domains,
|
|
153
|
-
);
|
|
154
|
-
},
|
|
155
|
-
|
|
156
|
-
async extractEndpoints(options) {
|
|
157
|
-
const swaggerUrls = collectAllSwaggerUrls();
|
|
158
|
-
if (!swaggerUrls.length) {
|
|
159
|
-
throw new Error("No swagger URLs found in knowledge config");
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
for (const swaggerUrl of swaggerUrls) {
|
|
163
|
-
await extractEndpointsFn({
|
|
164
|
-
swaggerUrl,
|
|
165
|
-
domainsDir: options.domainsDir,
|
|
166
|
-
write: options.write ?? true,
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
},
|
|
170
|
-
} satisfies CortexInstance;
|
|
171
|
-
}
|