-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp.model.js
More file actions
205 lines (151 loc) · 4.75 KB
/
mcp.model.js
File metadata and controls
205 lines (151 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { v4 as uuidv4 } from "uuid";
export const MEMORY_SCOPE = {
PRIVATE: "private",
PROJECT: "project",
GLOBAL: "global"
};
export class BaseModel {
constructor(data = {}) {
this.id = data.id || uuidv4();
this.agent = data.agent || "unknown";
this.project = data.project || "default";
this.sessionId = data.sessionId || null;
this.scope = data.scope || MEMORY_SCOPE.PRIVATE;
this.createdAt = data.createdAt || new Date();
this.updatedAt = new Date();
this.tags = data.tags || [];
this.metadata = data.metadata || {};
this.version = data.version || 1;
}
}
export class ContextModel extends BaseModel {
constructor(data = {}) {
super(data);
this.type = data.type || "general";
this.content = data.content || "";
this.summary = data.summary || null;
this.embedding = data.embedding || null;
this.relatedContexts = data.relatedContexts || [];
this.relatedActions = data.relatedActions || [];
this.importance = data.importance ?? 3;
this.accessCount = data.accessCount || 0;
this.lastAccessedAt = data.lastAccessedAt || null;
}
}
export class ActionModel extends BaseModel {
constructor(data = {}) {
super(data);
this.actionType = data.actionType || "unknown";
this.target = data.target || null;
this.before = data.before || null;
this.after = data.after || null;
this.diff = data.diff || null;
this.summary = data.summary || null;
this.contextRefs = data.contextRefs || [];
this.outcome = data.outcome || {
success: true,
error: null
};
}
}
export class SessionModel extends BaseModel {
constructor(data = {}) {
super(data);
this.sessionId = data.sessionId || uuidv4();
this.status = data.status || "active";
this.startedAt = data.startedAt || new Date();
this.endedAt = data.endedAt || null;
this.contextIds = data.contextIds || [];
this.actionIds = data.actionIds || [];
}
}
export class AgentModel extends BaseModel {
constructor(data = {}) {
super(data);
this.agent_id = data.agent_id || this.id;
this.name = data.name || "Unnamed Agent";
this.role = data.role || "worker";
this.capabilities = data.capabilities || [];
this.status = data.status || "idle"; // active | idle | offline
this.current_task = data.current_task || null;
this.last_seen = new Date();
}
}
export class TaskModel extends BaseModel {
constructor(data = {}) {
super(data);
this.task_id = data.task_id || this.id;
this.title = data.title || "";
this.description = data.description || "";
this.assigned_to = data.assigned_to || null;
this.created_by = data.created_by || "system";
this.status = data.status || "pending";
// pending | in_progress | blocked | completed
this.priority = data.priority || 3;
this.dependencies = data.dependencies || [];
}
}
export class MessageModel extends BaseModel {
constructor(data = {}) {
super(data);
this.message_id = data.message_id || this.id;
this.from_agent = data.from_agent || "system";
this.to_agent = data.to_agent || null;
this.type = data.type || "info";
// info | warning | handoff | status
this.content = data.content || "";
this.related_task = data.related_task || null;
}
}
export class ProjectMapModel extends BaseModel {
constructor(data = {}) {
super(data);
this.file_path = data.file_path || "";
this.type = data.type || "unknown";
this.summary = data.summary || "";
this.dependencies = data.dependencies || [];
this.exports = data.exports || [];
this.relationships = data.relationships || {
parent: null,
children: []
};
}
}
export class MemoryQueryBuilder {
static build({ agent, project, query, scope = "project", includeGlobal = true } = {}) {
const conditions = [];
if (agent) {
conditions.push({ agent, scope: MEMORY_SCOPE.PRIVATE });
}
if (project && (scope === "project" || scope === "global")) {
conditions.push({ project, scope: MEMORY_SCOPE.PROJECT });
}
if (includeGlobal) {
conditions.push({ scope: MEMORY_SCOPE.GLOBAL });
}
const filters = [];
if (conditions.length) {
filters.push({ $or: conditions });
}
if (query?.trim()) {
filters.push({ $text: { $search: query.trim() } });
}
if (!filters.length) {
return {};
}
if (filters.length === 1) {
return filters[0];
}
return { $and: filters };
}
}
export function normalizeMemory(memory) {
return {
...JSON.parse(JSON.stringify(memory)),
importance: memory.importance ?? 3,
accessCount: memory.accessCount || 0,
lastAccessedAt: memory.lastAccessedAt || null,
createdAt: memory.createdAt || new Date(),
updatedAt: new Date()
};
}