@m6d/cortex-server 1.3.0 → 1.5.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/dist/src/adapters/database.d.ts +3 -0
- package/dist/src/ai/active-streams.d.ts +14 -0
- package/dist/src/ai/context/builder.d.ts +24 -0
- package/dist/src/ai/context/compressor.d.ts +7 -0
- package/dist/src/ai/context/index.d.ts +15 -0
- package/dist/src/ai/context/summarizer.d.ts +5 -0
- package/dist/src/ai/context/token-estimator.d.ts +20 -0
- package/dist/src/ai/context/types.d.ts +20 -0
- package/dist/src/ai/index.d.ts +1 -1
- package/dist/src/ai/prompt.d.ts +6 -1
- package/dist/src/config.d.ts +4 -0
- package/dist/src/db/schema.d.ts +19 -1
- package/dist/src/graph/expand-domains.d.ts +2 -0
- package/dist/src/graph/helpers.d.ts +5 -0
- package/dist/src/graph/resolver.d.ts +2 -0
- package/dist/src/graph/types.d.ts +6 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/routes/ws.d.ts +5 -1
- package/dist/src/types.d.ts +32 -14
- package/dist/src/ws/connections.d.ts +3 -3
- package/dist/src/ws/events.d.ts +28 -3
- package/dist/src/ws/index.d.ts +1 -1
- package/dist/src/ws/notify.d.ts +1 -1
- package/package.json +1 -1
- package/src/adapters/database.ts +3 -0
- package/src/adapters/mssql.ts +26 -6
- package/src/ai/active-streams.ts +123 -0
- package/src/ai/context/builder.ts +94 -0
- package/src/ai/context/compressor.ts +47 -0
- package/src/ai/context/index.ts +75 -0
- package/src/ai/context/summarizer.ts +50 -0
- package/src/ai/context/token-estimator.ts +60 -0
- package/src/ai/context/types.ts +28 -0
- package/src/ai/index.ts +124 -29
- package/src/ai/prompt.ts +27 -18
- package/src/ai/tools/query-graph.tool.ts +1 -1
- package/src/cli/extract-endpoints.ts +18 -18
- package/src/config.ts +4 -0
- package/src/db/migrations/20260315000000_add_context_meta/migration.sql +1 -0
- package/src/db/schema.ts +6 -1
- package/src/factory.ts +11 -1
- package/src/graph/expand-domains.ts +276 -0
- package/src/graph/generate-cypher.ts +18 -5
- package/src/graph/helpers.ts +1 -0
- package/src/graph/resolver.ts +10 -0
- package/src/graph/seed.ts +5 -2
- package/src/graph/types.ts +6 -0
- package/src/index.ts +2 -0
- package/src/routes/chat.ts +47 -2
- package/src/routes/threads.ts +46 -9
- package/src/routes/ws.ts +37 -23
- package/src/types.ts +37 -13
- package/src/ws/connections.ts +15 -9
- package/src/ws/events.ts +31 -3
- package/src/ws/index.ts +9 -1
- package/src/ws/notify.ts +2 -2
package/src/routes/ws.ts
CHANGED
|
@@ -3,31 +3,45 @@ import { upgradeWebSocket } from "hono/bun";
|
|
|
3
3
|
import type { AppEnv } from "../types.ts";
|
|
4
4
|
import { addConnection, removeConnection } from "../ws/index.ts";
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
type WsRouteOptions = {
|
|
7
|
+
useAgentParam?: boolean;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function createWsRoute(options?: WsRouteOptions) {
|
|
7
11
|
const app = new Hono<AppEnv>();
|
|
12
|
+
const handleUpgrade = upgradeWebSocket(function (c) {
|
|
13
|
+
const userId = c.get("user")?.id;
|
|
14
|
+
const agentId =
|
|
15
|
+
options?.useAgentParam === true ? c.req.param("agentId") : c.req.query("agentId");
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
onOpen(_, ws) {
|
|
19
|
+
if (userId && agentId) {
|
|
20
|
+
addConnection(userId, agentId, ws);
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
onClose(_, ws) {
|
|
25
|
+
if (userId && agentId) {
|
|
26
|
+
removeConnection(userId, agentId, ws);
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
app.get("/ws", function (c, next) {
|
|
33
|
+
if (!c.get("user")) {
|
|
34
|
+
return c.json({ error: "Unauthorized" }, 401);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const agentId =
|
|
38
|
+
options?.useAgentParam === true ? c.req.param("agentId") : c.req.query("agentId");
|
|
39
|
+
if (!agentId) {
|
|
40
|
+
return c.json({ error: "agentId is required" }, 400);
|
|
41
|
+
}
|
|
8
42
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
upgradeWebSocket(function (c) {
|
|
12
|
-
const user = c.get("user");
|
|
13
|
-
const userId = user?.id;
|
|
14
|
-
|
|
15
|
-
return {
|
|
16
|
-
onOpen(_, ws) {
|
|
17
|
-
if (userId) {
|
|
18
|
-
addConnection(userId, ws);
|
|
19
|
-
}
|
|
20
|
-
ws.send("Connected!");
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
onClose(_, ws) {
|
|
24
|
-
if (userId) {
|
|
25
|
-
removeConnection(userId, ws);
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
};
|
|
29
|
-
}),
|
|
30
|
-
);
|
|
43
|
+
return handleUpgrade(c, next) as unknown as Response;
|
|
44
|
+
});
|
|
31
45
|
|
|
32
46
|
return app;
|
|
33
47
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,24 +1,38 @@
|
|
|
1
1
|
import type { ToolUIPart, UIMessage } from "ai";
|
|
2
2
|
import type { ResolvedCortexAgentConfig } from "./config";
|
|
3
|
+
import type { InferSelectModel } from "drizzle-orm";
|
|
4
|
+
import type { messages, threads } from "./db/schema";
|
|
3
5
|
|
|
4
|
-
export type Thread =
|
|
6
|
+
export type Thread = InferSelectModel<typeof threads>;
|
|
7
|
+
|
|
8
|
+
export type StoredMessage = InferSelectModel<typeof messages>;
|
|
9
|
+
|
|
10
|
+
export type ThreadSummary = {
|
|
5
11
|
id: string;
|
|
6
|
-
|
|
7
|
-
agentId: string;
|
|
8
|
-
title: string | null;
|
|
9
|
-
session: Record<string, unknown> | null;
|
|
12
|
+
title?: string;
|
|
10
13
|
createdAt: Date;
|
|
11
14
|
updatedAt: Date;
|
|
15
|
+
isRunning: boolean;
|
|
12
16
|
};
|
|
13
17
|
|
|
14
|
-
export type
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
export type MessageMetadata = {
|
|
19
|
+
modelId: string;
|
|
20
|
+
providerMetadata: unknown;
|
|
21
|
+
isAborted?: boolean;
|
|
22
|
+
tokenUsage?: {
|
|
23
|
+
input: {
|
|
24
|
+
noCache: number;
|
|
25
|
+
cacheRead: number;
|
|
26
|
+
cacheWrite: number;
|
|
27
|
+
total: number;
|
|
28
|
+
};
|
|
29
|
+
output: {
|
|
30
|
+
reasoning: number;
|
|
31
|
+
text: number;
|
|
32
|
+
total: number;
|
|
33
|
+
};
|
|
34
|
+
total: number;
|
|
35
|
+
};
|
|
22
36
|
};
|
|
23
37
|
|
|
24
38
|
export type CapturedFileInput = {
|
|
@@ -48,3 +62,13 @@ export type CortexAppEnv = {
|
|
|
48
62
|
agentId: string;
|
|
49
63
|
};
|
|
50
64
|
};
|
|
65
|
+
|
|
66
|
+
export function toThreadSummary(thread: Thread, isRunning: boolean) {
|
|
67
|
+
return {
|
|
68
|
+
id: thread.id,
|
|
69
|
+
title: thread.title ?? undefined,
|
|
70
|
+
createdAt: thread.createdAt,
|
|
71
|
+
updatedAt: thread.updatedAt,
|
|
72
|
+
isRunning,
|
|
73
|
+
} satisfies ThreadSummary;
|
|
74
|
+
}
|
package/src/ws/connections.ts
CHANGED
|
@@ -2,22 +2,28 @@ import type { WSContext } from "hono/ws";
|
|
|
2
2
|
|
|
3
3
|
const connections = new Map<string, WSContext[]>();
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
function getConnectionKey(userId: string, agentId: string) {
|
|
6
|
+
return `${userId}:${agentId}`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function addConnection(userId: string, agentId: string, ws: WSContext) {
|
|
10
|
+
const key = getConnectionKey(userId, agentId);
|
|
11
|
+
const sockets = connections.get(key) ?? [];
|
|
7
12
|
sockets.push(ws);
|
|
8
|
-
connections.set(
|
|
13
|
+
connections.set(key, sockets);
|
|
9
14
|
}
|
|
10
15
|
|
|
11
|
-
export function removeConnection(userId: string, ws: WSContext) {
|
|
12
|
-
|
|
16
|
+
export function removeConnection(userId: string, agentId: string, ws: WSContext) {
|
|
17
|
+
const key = getConnectionKey(userId, agentId);
|
|
18
|
+
let sockets = connections.get(key) ?? [];
|
|
13
19
|
sockets = sockets.filter((x) => x !== ws);
|
|
14
20
|
if (sockets.length) {
|
|
15
|
-
connections.set(
|
|
21
|
+
connections.set(key, sockets);
|
|
16
22
|
} else {
|
|
17
|
-
connections.delete(
|
|
23
|
+
connections.delete(key);
|
|
18
24
|
}
|
|
19
25
|
}
|
|
20
26
|
|
|
21
|
-
export function getConnections(userId: string) {
|
|
22
|
-
return connections.get(userId) ?? [];
|
|
27
|
+
export function getConnections(userId: string, agentId: string) {
|
|
28
|
+
return connections.get(getConnectionKey(userId, agentId)) ?? [];
|
|
23
29
|
}
|
package/src/ws/events.ts
CHANGED
|
@@ -1,11 +1,39 @@
|
|
|
1
|
+
import type { ThreadSummary } from "../types.ts";
|
|
2
|
+
|
|
3
|
+
export type ThreadCreatedEvent = {
|
|
4
|
+
type: "thread:created";
|
|
5
|
+
payload: { thread: ThreadSummary };
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export type ThreadDeletedEvent = {
|
|
9
|
+
type: "thread:deleted";
|
|
10
|
+
payload: { threadId: string };
|
|
11
|
+
};
|
|
12
|
+
|
|
1
13
|
export type ThreadTitleUpdatedEvent = {
|
|
2
14
|
type: "thread:title-updated";
|
|
3
|
-
payload: {
|
|
15
|
+
payload: { thread: ThreadSummary };
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type ThreadRunStartedEvent = {
|
|
19
|
+
type: "thread:run-started";
|
|
20
|
+
payload: { thread: ThreadSummary };
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type ThreadRunFinishedEvent = {
|
|
24
|
+
type: "thread:run-finished";
|
|
25
|
+
payload: { thread: ThreadSummary };
|
|
4
26
|
};
|
|
5
27
|
|
|
6
28
|
export type ThreadMessagesUpdatedEvent = {
|
|
7
29
|
type: "thread:messages-updated";
|
|
8
|
-
payload: { threadId: string };
|
|
30
|
+
payload: { threadId: string; thread: ThreadSummary };
|
|
9
31
|
};
|
|
10
32
|
|
|
11
|
-
export type WsEvent =
|
|
33
|
+
export type WsEvent =
|
|
34
|
+
| ThreadCreatedEvent
|
|
35
|
+
| ThreadDeletedEvent
|
|
36
|
+
| ThreadTitleUpdatedEvent
|
|
37
|
+
| ThreadRunStartedEvent
|
|
38
|
+
| ThreadRunFinishedEvent
|
|
39
|
+
| ThreadMessagesUpdatedEvent;
|
package/src/ws/index.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
1
|
export { addConnection, removeConnection, getConnections } from "./connections.ts";
|
|
2
2
|
export { notify } from "./notify.ts";
|
|
3
|
-
export type {
|
|
3
|
+
export type {
|
|
4
|
+
WsEvent,
|
|
5
|
+
ThreadCreatedEvent,
|
|
6
|
+
ThreadDeletedEvent,
|
|
7
|
+
ThreadTitleUpdatedEvent,
|
|
8
|
+
ThreadRunStartedEvent,
|
|
9
|
+
ThreadRunFinishedEvent,
|
|
10
|
+
ThreadMessagesUpdatedEvent,
|
|
11
|
+
} from "./events.ts";
|
package/src/ws/notify.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { getConnections } from "./connections.ts";
|
|
2
2
|
import type { WsEvent } from "./events.ts";
|
|
3
3
|
|
|
4
|
-
export function notify(userId: string, event: WsEvent) {
|
|
4
|
+
export function notify(userId: string, agentId: string, event: WsEvent) {
|
|
5
5
|
const message = JSON.stringify(event);
|
|
6
|
-
for (const ws of getConnections(userId)) {
|
|
6
|
+
for (const ws of getConnections(userId, agentId)) {
|
|
7
7
|
ws.send(message);
|
|
8
8
|
}
|
|
9
9
|
}
|