agent-space-manager 1.0.1 → 1.0.3

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/page/index.html CHANGED
@@ -17,7 +17,7 @@
17
17
  <div>
18
18
  <div class="title-row">
19
19
  <h1>Agent Space Manager</h1>
20
- <span class="plugin-version-badge" id="pluginVersionBadge">V1.0.1</span>
20
+ <span class="plugin-version-badge" id="pluginVersionBadge">V1.0.3</span>
21
21
  </div>
22
22
  <p class="subtitle" id="main-subtitle">Universal Multi-IDE & Project Skill Orchestrator</p>
23
23
  </div>
@@ -132,7 +132,7 @@
132
132
  <div class="prompt-desc">
133
133
  Ask your AI agent to enforce standard layouts, record upstream links, and install rules for component addendum/edits.
134
134
  </div>
135
- <button type="button" class="btn-copy-prompt" id="btnCopyStoragePrompt" title="Copy prompt for your AI agent to standardize Components Storage file structure, enforce upstream links, clean up plugins, enforce UTF-8 No BOM encoding, respect one-time rearrangement stability, preserve shared cross-agent communication folders, and install rules">Copy Prompt (V1.4)</button>
135
+ <button type="button" class="btn-copy-prompt" id="btnCopyStoragePrompt" title="Copy prompt for your AI agent to standardize Components Storage file structure, enforce upstream links, clean up plugins, enforce UTF-8 No BOM encoding, respect one-time rearrangement stability, preserve shared cross-agent communication folders, maintain dynamic Smart Middleware intent rules, and install rules">Copy Prompt (V1.6)</button>
136
136
  </div>
137
137
 
138
138
  <!-- Tile 4: Gateway Tool Optimization (Adjustable Cap with "Default" button) -->
@@ -62,6 +62,279 @@ function recordFailure(error, contextInfo = {}) {
62
62
 
63
63
  const CORE_TOOL_REGEX = /^(read_file|write_file|edit_file|run_command|execute_command|bash|terminal|search_web|web_search|get_context|fetch)/i;
64
64
 
65
+ function escapeRegex(str) {
66
+ return String(str).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
67
+ }
68
+
69
+ export const BUILTIN_INTENT_RULES = [
70
+ {
71
+ id: "reverse_engineering",
72
+ toolPattern: /(^|[_-])(re|reverse|binary|disasm|decompile|gdb|frida|rizin)([_-]|$)/i,
73
+ intentPattern: /(reverse|binary|disassembl|decompile|pe|elf|macho|dll|exe|instruction|register|gdb|frida|rizin|assembly|opcode|demangle|symbols)/i,
74
+ },
75
+ {
76
+ id: "web_scraping",
77
+ toolPattern: /(crawl|scrape|stagehand|crw)/i,
78
+ intentPattern: /(scrape|crawl|website|browser|webpage|url|http|extract|html|dom|click|navigate|page)/i,
79
+ },
80
+ {
81
+ id: "gemini_docs",
82
+ toolPattern: /(gemini|docs|knowledge)/i,
83
+ intentPattern: /(doc|documentation|api|sdk|reference|search|lookup|guide|gemini)/i,
84
+ },
85
+ {
86
+ id: "dart_flutter",
87
+ toolPattern: /(dart|flutter)/i,
88
+ intentPattern: /(dart|flutter|pubspec|widget|stateful|stateless)/i,
89
+ },
90
+ {
91
+ id: "bio_chem",
92
+ toolPattern: /(bio|protein|chem|pdb|structure)/i,
93
+ intentPattern: /(protein|amino|structure|alphafold|pdb|dna|rna|gene|sequence|molecular|chembl|biology)/i,
94
+ },
95
+ ];
96
+
97
+ export function compileCustomIntentRules(raw) {
98
+ if (!raw) return [];
99
+ const rules = [];
100
+
101
+ const entries = Array.isArray(raw)
102
+ ? raw
103
+ : typeof raw === "object"
104
+ ? Object.entries(raw).map(([key, val]) => ({ id: key, ...(val || {}) }))
105
+ : [];
106
+
107
+ for (const item of entries) {
108
+ if (!item || typeof item !== "object") continue;
109
+
110
+ let toolRegex = null;
111
+ const rawTools = item.toolPattern || item.toolPatterns || item.tools || item.tool;
112
+ if (rawTools instanceof RegExp) {
113
+ toolRegex = rawTools;
114
+ } else if (Array.isArray(rawTools) && rawTools.length > 0) {
115
+ const parts = rawTools.map((t) => (typeof t === "string" ? escapeRegex(t.trim()) : "")).filter(Boolean);
116
+ if (parts.length > 0) {
117
+ toolRegex = new RegExp(`(${parts.join("|")})`, "i");
118
+ }
119
+ } else if (typeof rawTools === "string" && rawTools.trim()) {
120
+ const trimmed = rawTools.trim();
121
+ toolRegex = /[\^$|*+?]/.test(trimmed)
122
+ ? new RegExp(trimmed, "i")
123
+ : new RegExp(escapeRegex(trimmed), "i");
124
+ } else if (item.id && typeof item.id === "string") {
125
+ toolRegex = new RegExp(escapeRegex(item.id.trim()), "i");
126
+ }
127
+
128
+ let intentRegex = null;
129
+ const rawIntents = item.intentPattern || item.intentKeywords || item.keywords || item.intents;
130
+ if (rawIntents instanceof RegExp) {
131
+ intentRegex = rawIntents;
132
+ } else if (Array.isArray(rawIntents) && rawIntents.length > 0) {
133
+ const parts = rawIntents.map((k) => (typeof k === "string" ? escapeRegex(k.trim()) : "")).filter(Boolean);
134
+ if (parts.length > 0) {
135
+ toolRegex = toolRegex || new RegExp(`(${parts.join("|")})`, "i");
136
+ intentRegex = new RegExp(`(${parts.join("|")})`, "i");
137
+ }
138
+ } else if (typeof rawIntents === "string" && rawIntents.trim()) {
139
+ const trimmed = rawIntents.trim();
140
+ intentRegex = /[\^$|*+?]/.test(trimmed)
141
+ ? new RegExp(trimmed, "i")
142
+ : new RegExp(escapeRegex(trimmed), "i");
143
+ }
144
+
145
+ if (toolRegex && intentRegex) {
146
+ rules.push({
147
+ id: item.id || "custom",
148
+ toolPattern: toolRegex,
149
+ intentPattern: intentRegex,
150
+ });
151
+ }
152
+ }
153
+
154
+ return rules;
155
+ }
156
+
157
+ export function autoSeedMiddlewareIntents(hubRoot) {
158
+ if (!hubRoot || !fs.existsSync(hubRoot)) return {};
159
+ const sharedDir = path.join(hubRoot, "shared");
160
+ const intentsPath = path.join(sharedDir, "middleware-intents.json");
161
+ if (fs.existsSync(intentsPath)) {
162
+ try {
163
+ const content = fs.readFileSync(intentsPath, "utf8").trim();
164
+ if (content) return JSON.parse(content);
165
+ } catch {}
166
+ }
167
+
168
+ const pluginsDir = path.join(hubRoot, "plugins");
169
+ if (!fs.existsSync(pluginsDir)) return {};
170
+
171
+ const intents = {};
172
+ try {
173
+ const entries = fs.readdirSync(pluginsDir);
174
+ for (const ent of entries) {
175
+ const pluginDir = path.join(pluginsDir, ent);
176
+ try {
177
+ const st = fs.statSync(pluginDir);
178
+ if (!st.isDirectory()) continue;
179
+ } catch {
180
+ continue;
181
+ }
182
+
183
+ const pluginJsonPath = path.join(pluginDir, "plugin.json");
184
+ const mcpJsonPath = path.join(pluginDir, "mcp_config.json");
185
+ const toolPatterns = [ent.toLowerCase()];
186
+ const intentKeywords = new Set([ent.toLowerCase()]);
187
+
188
+ if (fs.existsSync(pluginJsonPath)) {
189
+ try {
190
+ const pj = JSON.parse(fs.readFileSync(pluginJsonPath, "utf8"));
191
+ if (pj.name) {
192
+ pj.name.split(/[^a-z0-9]+/i).filter((t) => t.length > 2).forEach((t) => intentKeywords.add(t.toLowerCase()));
193
+ }
194
+ if (pj.description) {
195
+ pj.description.split(/[^a-z0-9]+/i).filter((t) => t.length > 3).slice(0, 10).forEach((t) => intentKeywords.add(t.toLowerCase()));
196
+ }
197
+ if (pj.middleware?.intents) {
198
+ pj.middleware.intents.forEach((i) => intentKeywords.add(String(i).toLowerCase()));
199
+ }
200
+ } catch {}
201
+ }
202
+
203
+ if (fs.existsSync(mcpJsonPath)) {
204
+ try {
205
+ const mcp = JSON.parse(fs.readFileSync(mcpJsonPath, "utf8"));
206
+ const servers = mcp.mcpServers || mcp.servers || {};
207
+ for (const sName of Object.keys(servers)) {
208
+ toolPatterns.push(sName.toLowerCase());
209
+ sName.split(/[^a-z0-9]+/i).filter((t) => t.length > 2).forEach((t) => intentKeywords.add(t.toLowerCase()));
210
+ }
211
+ } catch {}
212
+ }
213
+
214
+ const skillsDir = path.join(pluginDir, "skills");
215
+ if (fs.existsSync(skillsDir)) {
216
+ try {
217
+ for (const sEnt of fs.readdirSync(skillsDir)) {
218
+ toolPatterns.push(sEnt.toLowerCase());
219
+ sEnt.split(/[^a-z0-9]+/i).filter((t) => t.length > 2).forEach((t) => intentKeywords.add(t.toLowerCase()));
220
+ }
221
+ } catch {}
222
+ }
223
+
224
+ intents[ent.toLowerCase()] = {
225
+ toolPatterns: Array.from(new Set(toolPatterns)),
226
+ intentKeywords: Array.from(intentKeywords),
227
+ };
228
+ }
229
+
230
+ if (Object.keys(intents).length > 0) {
231
+ if (!fs.existsSync(sharedDir)) fs.mkdirSync(sharedDir, { recursive: true });
232
+ fs.writeFileSync(intentsPath, JSON.stringify(intents, null, 2), "utf8");
233
+ }
234
+ } catch (err) {
235
+ console.warn("Failed to auto-seed middleware intents:", err);
236
+ }
237
+
238
+ return intents;
239
+ }
240
+
241
+ export function trimMiddlewareIntent(hubRoot, pluginId) {
242
+ if (!hubRoot || !pluginId) return false;
243
+ const intentsPath = path.join(hubRoot, "shared", "middleware-intents.json");
244
+ if (!fs.existsSync(intentsPath)) return false;
245
+
246
+ try {
247
+ const content = fs.readFileSync(intentsPath, "utf8").trim();
248
+ if (!content) return false;
249
+ const data = JSON.parse(content);
250
+ const targetKey = String(pluginId).toLowerCase();
251
+ let changed = false;
252
+
253
+ if (data[targetKey]) {
254
+ delete data[targetKey];
255
+ changed = true;
256
+ }
257
+
258
+ for (const [k, v] of Object.entries(data)) {
259
+ if (k.toLowerCase() === targetKey) {
260
+ delete data[k];
261
+ changed = true;
262
+ } else if (v && Array.isArray(v.toolPatterns)) {
263
+ const filtered = v.toolPatterns.filter((p) => String(p).toLowerCase() !== targetKey);
264
+ if (filtered.length !== v.toolPatterns.length) {
265
+ if (filtered.length === 0) {
266
+ delete data[k];
267
+ } else {
268
+ v.toolPatterns = filtered;
269
+ }
270
+ changed = true;
271
+ }
272
+ }
273
+ }
274
+
275
+ if (changed) {
276
+ fs.writeFileSync(intentsPath, JSON.stringify(data, null, 2), "utf8");
277
+ return true;
278
+ }
279
+ } catch {}
280
+ return false;
281
+ }
282
+
283
+ let cachedUserIntents = [];
284
+ let cachedUserIntentsAt = 0;
285
+ let lastIntentsFile = null;
286
+ let lastIntentsMtime = 0;
287
+
288
+ export function getCachedUserIntents(hubConfig) {
289
+ const now = Date.now();
290
+ const hubRoot = hubConfig?.hubRoot;
291
+ const intentsPath = hubRoot ? path.join(hubRoot, "shared", "middleware-intents.json") : null;
292
+
293
+ if (now - cachedUserIntentsAt < 2000 && lastIntentsFile === intentsPath) {
294
+ return cachedUserIntents;
295
+ }
296
+
297
+ cachedUserIntentsAt = now;
298
+ lastIntentsFile = intentsPath;
299
+
300
+ if (!intentsPath || !fs.existsSync(intentsPath)) {
301
+ if (hubRoot && fs.existsSync(path.join(hubRoot, "plugins"))) {
302
+ const seeded = autoSeedMiddlewareIntents(hubRoot);
303
+ if (Object.keys(seeded).length > 0) {
304
+ cachedUserIntents = compileCustomIntentRules(seeded);
305
+ return cachedUserIntents;
306
+ }
307
+ }
308
+ if (hubConfig?.smartMiddleware?.customIntents) {
309
+ cachedUserIntents = compileCustomIntentRules(hubConfig.smartMiddleware.customIntents);
310
+ return cachedUserIntents;
311
+ }
312
+ cachedUserIntents = [];
313
+ return cachedUserIntents;
314
+ }
315
+
316
+ try {
317
+ const stat = fs.statSync(intentsPath);
318
+ if (stat.mtimeMs === lastIntentsMtime && cachedUserIntents.length > 0) {
319
+ return cachedUserIntents;
320
+ }
321
+ lastIntentsMtime = stat.mtimeMs;
322
+
323
+ const content = fs.readFileSync(intentsPath, "utf8").trim();
324
+ if (!content) {
325
+ cachedUserIntents = [];
326
+ return cachedUserIntents;
327
+ }
328
+
329
+ const parsed = JSON.parse(content);
330
+ cachedUserIntents = compileCustomIntentRules(parsed);
331
+ } catch {
332
+ if (!cachedUserIntents) cachedUserIntents = [];
333
+ }
334
+
335
+ return cachedUserIntents;
336
+ }
337
+
65
338
  export function onRequest(body, ctx) {
66
339
  if (!body) return;
67
340
 
@@ -127,6 +400,12 @@ export function onRequest(body, ctx) {
127
400
  return { ...body, tools: chatTools };
128
401
  }
129
402
 
403
+ const optionIntents = ctx?.options?.customIntents
404
+ ? compileCustomIntentRules(ctx.options.customIntents)
405
+ : [];
406
+ const userIntents = [...getCachedUserIntents(hubConfig), ...optionIntents];
407
+ const allIntentRules = [...BUILTIN_INTENT_RULES, ...userIntents];
408
+
130
409
  const matchedTools = [...coreTools];
131
410
  const fallbackTools = [];
132
411
 
@@ -145,25 +424,12 @@ export function onRequest(body, ctx) {
145
424
  }
146
425
 
147
426
  if (!isMatch) {
148
- if (/(^|[_-])(re|reverse|binary|disasm|decompile|gdb|frida|rizin)([_-]|$)/i.test(name)) {
149
- if (/(reverse|binary|disassembl|decompile|pe|elf|macho|dll|exe|instruction|register|gdb|frida|rizin|assembly|opcode|demangle|symbols)/i.test(userText)) {
150
- isMatch = true;
151
- }
152
- } else if (name.includes("crawl") || name.includes("scrape") || name.includes("stagehand") || name.includes("crw")) {
153
- if (/(scrape|crawl|website|browser|webpage|url|http|extract|html|dom|click|navigate|page)/i.test(userText)) {
154
- isMatch = true;
155
- }
156
- } else if (name.includes("gemini") || name.includes("docs") || name.includes("knowledge")) {
157
- if (/(doc|documentation|api|sdk|reference|search|lookup|guide|gemini)/i.test(userText)) {
158
- isMatch = true;
159
- }
160
- } else if (name.includes("dart") || name.includes("flutter")) {
161
- if (/(dart|flutter|pubspec|widget|stateful|stateless)/i.test(userText)) {
162
- isMatch = true;
163
- }
164
- } else if (name.includes("bio") || name.includes("protein") || name.includes("chem") || name.includes("pdb") || name.includes("structure")) {
165
- if (/(protein|amino|structure|alphafold|pdb|dna|rna|gene|sequence|molecular|chembl|biology)/i.test(userText)) {
166
- isMatch = true;
427
+ for (const rule of allIntentRules) {
428
+ if (rule.toolPattern && rule.intentPattern) {
429
+ if (rule.toolPattern.test(name) && rule.intentPattern.test(userText)) {
430
+ isMatch = true;
431
+ break;
432
+ }
167
433
  }
168
434
  }
169
435
  }