@m6d/cortex-server 1.2.0 → 1.4.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 (48) hide show
  1. package/dist/src/adapters/database.d.ts +3 -0
  2. package/dist/src/ai/active-streams.d.ts +14 -0
  3. package/dist/src/ai/active-streams.test.d.ts +1 -0
  4. package/dist/src/ai/context/builder.d.ts +24 -0
  5. package/dist/src/ai/context/compressor.d.ts +7 -0
  6. package/dist/src/ai/context/index.d.ts +15 -0
  7. package/dist/src/ai/context/summarizer.d.ts +5 -0
  8. package/dist/src/ai/context/token-estimator.d.ts +20 -0
  9. package/dist/src/ai/context/types.d.ts +20 -0
  10. package/dist/src/ai/prompt.d.ts +6 -1
  11. package/dist/src/config.d.ts +4 -0
  12. package/dist/src/db/schema.d.ts +19 -1
  13. package/dist/src/index.d.ts +1 -0
  14. package/dist/src/routes/ws.d.ts +5 -1
  15. package/dist/src/types.d.ts +32 -14
  16. package/dist/src/ws/connections.d.ts +3 -3
  17. package/dist/src/ws/events.d.ts +33 -2
  18. package/dist/src/ws/index.d.ts +1 -1
  19. package/dist/src/ws/notify.d.ts +1 -1
  20. package/package.json +3 -2
  21. package/src/adapters/database.ts +3 -0
  22. package/src/adapters/mssql.ts +26 -6
  23. package/src/ai/active-streams.test.ts +21 -0
  24. package/src/ai/active-streams.ts +123 -0
  25. package/src/ai/context/builder.ts +94 -0
  26. package/src/ai/context/compressor.ts +47 -0
  27. package/src/ai/context/index.ts +75 -0
  28. package/src/ai/context/summarizer.ts +50 -0
  29. package/src/ai/context/token-estimator.ts +60 -0
  30. package/src/ai/context/types.ts +28 -0
  31. package/src/ai/index.ts +158 -22
  32. package/src/ai/prompt.ts +21 -15
  33. package/src/ai/tools/execute-code.tool.ts +79 -27
  34. package/src/ai/tools/query-graph.tool.ts +1 -1
  35. package/src/cli/extract-endpoints.ts +18 -18
  36. package/src/config.ts +4 -0
  37. package/src/db/migrations/20260315000000_add_context_meta/migration.sql +1 -0
  38. package/src/db/schema.ts +6 -1
  39. package/src/factory.ts +11 -1
  40. package/src/index.ts +2 -0
  41. package/src/routes/chat.ts +46 -1
  42. package/src/routes/threads.ts +46 -9
  43. package/src/routes/ws.ts +37 -23
  44. package/src/types.ts +37 -13
  45. package/src/ws/connections.ts +15 -9
  46. package/src/ws/events.ts +35 -2
  47. package/src/ws/index.ts +9 -1
  48. package/src/ws/notify.ts +2 -2
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
- userId: string;
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 StoredMessage = {
15
- id: string;
16
- threadId: string;
17
- text: string | null;
18
- content: UIMessage;
19
- role: "system" | "user" | "assistant" | "tool";
20
- createdAt: Date;
21
- updatedAt: Date;
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
+ }
@@ -2,22 +2,28 @@ import type { WSContext } from "hono/ws";
2
2
 
3
3
  const connections = new Map<string, WSContext[]>();
4
4
 
5
- export function addConnection(userId: string, ws: WSContext) {
6
- const sockets = connections.get(userId) ?? [];
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(userId, sockets);
13
+ connections.set(key, sockets);
9
14
  }
10
15
 
11
- export function removeConnection(userId: string, ws: WSContext) {
12
- let sockets = connections.get(userId) ?? [];
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(userId, sockets);
21
+ connections.set(key, sockets);
16
22
  } else {
17
- connections.delete(userId);
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,6 +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: { threadId: string; title: string };
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 };
26
+ };
27
+
28
+ export type ThreadMessagesUpdatedEvent = {
29
+ type: "thread:messages-updated";
30
+ payload: { threadId: string; thread: ThreadSummary };
4
31
  };
5
32
 
6
- export type WsEvent = ThreadTitleUpdatedEvent;
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 { WsEvent, ThreadTitleUpdatedEvent } from "./events.ts";
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
  }