gm-oc 2.0.39 → 2.0.41
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/cli.js +8 -0
- package/gm.mjs +48 -30
- package/package.json +1 -1
package/cli.js
CHANGED
|
@@ -44,6 +44,14 @@ try {
|
|
|
44
44
|
|
|
45
45
|
filesToCopy.forEach(([src, dst]) => copyRecursive(path.join(srcDir, src), path.join(destDir, dst)));
|
|
46
46
|
|
|
47
|
+
// Also write to plugins/gm-oc.mjs - the actual file OpenCode loads
|
|
48
|
+
const pluginsDir = path.join(destDir, 'plugins');
|
|
49
|
+
fs.mkdirSync(pluginsDir, { recursive: true });
|
|
50
|
+
const gmMjsSrc = path.join(srcDir, 'gm.mjs');
|
|
51
|
+
if (fs.existsSync(gmMjsSrc)) {
|
|
52
|
+
fs.copyFileSync(gmMjsSrc, path.join(pluginsDir, 'gm-oc.mjs'));
|
|
53
|
+
}
|
|
54
|
+
|
|
47
55
|
const destPath = process.platform === 'win32'
|
|
48
56
|
? destDir.replace(/\\/g, '/')
|
|
49
57
|
: destDir;
|
package/gm.mjs
CHANGED
|
@@ -1,52 +1,70 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { readFileSync, existsSync } from 'fs';
|
|
2
|
+
import { join, dirname } from 'path';
|
|
3
3
|
import { exec } from 'child_process';
|
|
4
4
|
import { fileURLToPath } from 'url';
|
|
5
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
let thornsOutput = '';
|
|
6
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
|
|
8
|
+
export async function GmPlugin({ directory }) {
|
|
9
|
+
const agentsDir = join(__dirname, '..', 'agents');
|
|
12
10
|
|
|
13
11
|
const loadAgentRules = () => {
|
|
14
|
-
|
|
15
|
-
const agentMd = path.join(pluginDir, 'agents', 'gm.md');
|
|
16
|
-
try { agentRules = fs.readFileSync(agentMd, 'utf-8'); } catch (e) {}
|
|
17
|
-
return agentRules;
|
|
12
|
+
try { return readFileSync(join(agentsDir, 'gm.md'), 'utf-8'); } catch (e) { return ''; }
|
|
18
13
|
};
|
|
19
14
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
15
|
+
const runThorns = () => new Promise((resolve) => {
|
|
16
|
+
exec('bun x mcp-thorns@latest', {
|
|
17
|
+
encoding: 'utf-8', cwd: directory, timeout: 180000
|
|
18
|
+
}, (err, stdout) => resolve(err ? '' : (stdout || '').trim()));
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const runCodeSearch = (query) => new Promise((resolve) => {
|
|
22
|
+
const q = query.replace(/"/g, '\\"').substring(0, 200);
|
|
23
|
+
exec('bun x codebasesearch@latest "' + q + '"', {
|
|
24
|
+
encoding: 'utf-8', cwd: directory, timeout: 55000
|
|
25
|
+
}, (err, stdout) => {
|
|
26
|
+
if (err) return resolve('');
|
|
27
|
+
const lines = (stdout || '').split('\n');
|
|
28
|
+
const start = lines.findIndex(l => l.includes('Searching for:'));
|
|
29
|
+
resolve(start >= 0 ? lines.slice(start).join('\n').trim() : (stdout || '').trim());
|
|
30
30
|
});
|
|
31
|
-
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const getLastUserMessage = (msgs) => {
|
|
34
|
+
for (let i = msgs.length - 1; i >= 0; i--) {
|
|
35
|
+
const m = msgs[i];
|
|
36
|
+
if (m.role === 'user') {
|
|
37
|
+
const parts = Array.isArray(m.content) ? m.content : [m.content];
|
|
38
|
+
const text = parts.map(p => typeof p === 'string' ? p : (p?.text || '')).join(' ').trim();
|
|
39
|
+
if (text) return text;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return '';
|
|
32
43
|
};
|
|
33
44
|
|
|
34
|
-
const prdFile =
|
|
45
|
+
const prdFile = join(directory, '.prd');
|
|
35
46
|
|
|
36
47
|
return {
|
|
37
48
|
'experimental.chat.system.transform': async (input, output) => {
|
|
38
49
|
const rules = loadAgentRules();
|
|
39
|
-
const prd =
|
|
50
|
+
const prd = existsSync(prdFile) ? readFileSync(prdFile, 'utf-8').trim() : '';
|
|
40
51
|
let content = rules || '';
|
|
41
52
|
if (prd) content += '\n\nPENDING WORK (.prd):\n' + prd;
|
|
42
|
-
//
|
|
53
|
+
// On every user prompt: run thorns + codebasesearch fresh in parallel
|
|
54
|
+
// Skip if last message is from assistant (tool result inject, no new user input)
|
|
43
55
|
const msgs = input?.messages || [];
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
56
|
+
const lastMsg = msgs.length > 0 ? msgs[msgs.length - 1] : null;
|
|
57
|
+
const hasNewUserPrompt = !lastMsg || lastMsg.role === 'user';
|
|
58
|
+
if (hasNewUserPrompt) {
|
|
59
|
+
const userQuery = getLastUserMessage(msgs);
|
|
60
|
+
const [thorns, search] = await Promise.all([
|
|
61
|
+
runThorns(),
|
|
62
|
+
userQuery ? runCodeSearch(userQuery) : Promise.resolve('')
|
|
63
|
+
]);
|
|
47
64
|
if (thorns) content += '\n\n=== Repository Analysis (mcp-thorns) ===\n' + thorns;
|
|
65
|
+
if (search) content += '\n\n=== Semantic code search results ===\n' + search;
|
|
48
66
|
}
|
|
49
67
|
if (content) output.system.push(content);
|
|
50
68
|
}
|
|
51
69
|
};
|
|
52
|
-
}
|
|
70
|
+
}
|