@khaentertainment/grok-swarm 1.0.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.
@@ -0,0 +1,198 @@
1
+ /**
2
+ * grok-swarm plugin — registers `grok_swarm` as a native OpenClaw agent tool.
3
+ *
4
+ * Bridges to xAI Grok 4.20 Multi-Agent Beta via OpenRouter.
5
+ * Single API call — no tool loop — controlled timeout — no cascade risk.
6
+ *
7
+ * Modes: refactor, analyze, code, reason, orchestrate
8
+ */
9
+
10
+ import { spawn } from "child_process";
11
+ import { existsSync } from "fs";
12
+ import { join, dirname } from "path";
13
+ import { Type } from "@sinclair/typebox";
14
+
15
+ const SKILL_DIR = join(
16
+ dirname(new URL(import.meta.url).pathname),
17
+ "..",
18
+ "..",
19
+ "..",
20
+ "skills",
21
+ "grok-refactor",
22
+ );
23
+
24
+ const DEFAULT_PYTHON = join(SKILL_DIR, ".venv", "bin", "python3");
25
+ const DEFAULT_BRIDGE = join(SKILL_DIR, "grok_bridge.py");
26
+
27
+ const GrokSwarmSchema = Type.Object({
28
+ prompt: Type.String({ description: "Task instruction or question" }),
29
+ mode: Type.Optional(
30
+ Type.Union(
31
+ [
32
+ Type.Literal("refactor"),
33
+ Type.Literal("analyze"),
34
+ Type.Literal("code"),
35
+ Type.Literal("reason"),
36
+ Type.Literal("orchestrate"),
37
+ ],
38
+ { description: "Task mode (default: reason)" },
39
+ ),
40
+ ),
41
+ files: Type.Optional(
42
+ Type.Array(Type.String(), {
43
+ description: "Local file paths to include as context",
44
+ }),
45
+ ),
46
+ system: Type.Optional(
47
+ Type.String({
48
+ description: "Override system prompt (required for orchestrate mode)",
49
+ }),
50
+ ),
51
+ timeout: Type.Optional(
52
+ Type.Number({ description: "Timeout in seconds (default: 120)" }),
53
+ ),
54
+ write_files: Type.Optional(
55
+ Type.Boolean({
56
+ description: "Write generated files directly to disk; orchestrator receives a brief summary only",
57
+ }),
58
+ ),
59
+ output_dir: Type.Optional(
60
+ Type.String({
61
+ description: "Directory to write files into (default: ./grok-output/)",
62
+ }),
63
+ ),
64
+ });
65
+
66
+ export default function (api: any) {
67
+ api.registerTool(
68
+ {
69
+ name: "grok_swarm",
70
+ label: "Grok Swarm",
71
+ description:
72
+ "Delegate tasks to xAI Grok 4.20 Multi-Agent Beta (4-agent swarm with 2M context). " +
73
+ "Use for codebase analysis, refactoring, code generation, or complex reasoning. " +
74
+ "Modes: refactor, analyze, code, reason, orchestrate. " +
75
+ "With write_files=true, annotated code blocks are written directly to disk and a " +
76
+ "compact summary is returned instead of the full response.",
77
+ parameters: GrokSwarmSchema,
78
+ async execute(_toolCallId: string, params: any) {
79
+ const json = (payload: unknown) => ({
80
+ content: [
81
+ { type: "text" as const, text: typeof payload === "string" ? payload : JSON.stringify(payload, null, 2) },
82
+ ],
83
+ details: payload,
84
+ });
85
+
86
+ try {
87
+ const pythonPath = api.config?.pythonPath || DEFAULT_PYTHON;
88
+ const bridgeScript = api.config?.bridgeScript || DEFAULT_BRIDGE;
89
+ const defaultTimeout = api.config?.defaultTimeout || 120;
90
+
91
+ // Validate bridge script exists
92
+ if (!existsSync(bridgeScript)) {
93
+ return json({
94
+ error: `Bridge script not found: ${bridgeScript}. Ensure grok-refactor skill is installed.`,
95
+ });
96
+ }
97
+
98
+ const mode = params.mode || "reason";
99
+ const timeout = params.timeout || defaultTimeout;
100
+
101
+ // Validate orchestrate mode
102
+ if (mode === "orchestrate" && !params.system) {
103
+ return json({ error: "orchestrate mode requires 'system' parameter" });
104
+ }
105
+
106
+ // Build args
107
+ const args = [
108
+ bridgeScript,
109
+ "--prompt", params.prompt,
110
+ "--mode", mode,
111
+ "--timeout", String(timeout),
112
+ ];
113
+
114
+ if (params.files && params.files.length > 0) {
115
+ args.push("--files", ...params.files);
116
+ }
117
+
118
+ if (params.system) {
119
+ args.push("--system", params.system);
120
+ }
121
+
122
+ if (params.write_files) {
123
+ args.push("--write-files");
124
+ }
125
+
126
+ if (params.output_dir) {
127
+ args.push("--output-dir", params.output_dir);
128
+ } else if (api.config?.defaultOutputDir) {
129
+ args.push("--output-dir", api.config.defaultOutputDir);
130
+ }
131
+
132
+ // Spawn Python bridge with timeout enforcement
133
+ return new Promise((resolve) => {
134
+ const child = spawn(pythonPath, args, {
135
+ stdio: ["ignore", "pipe", "pipe"],
136
+ env: { ...process.env },
137
+ });
138
+
139
+ let stdout = "";
140
+ let stderr = "";
141
+ let timedOut = false;
142
+
143
+ const timer = setTimeout(() => {
144
+ timedOut = true;
145
+ child.kill("SIGTERM");
146
+ setTimeout(() => child.kill("SIGKILL"), 5000);
147
+ }, timeout * 1000);
148
+
149
+ child.stdout.on("data", (data: Buffer) => {
150
+ stdout += data.toString();
151
+ });
152
+
153
+ child.stderr.on("data", (data: Buffer) => {
154
+ stderr += data.toString();
155
+ });
156
+
157
+ child.on("close", (code: number | null) => {
158
+ clearTimeout(timer);
159
+
160
+ if (timedOut) {
161
+ resolve(
162
+ json({
163
+ error: `Grok call timed out after ${timeout}s`,
164
+ stderr: stderr.slice(-500),
165
+ }),
166
+ );
167
+ return;
168
+ }
169
+
170
+ if (code !== 0) {
171
+ resolve(
172
+ json({
173
+ error: `Bridge exited with code ${code}`,
174
+ stderr: stderr.slice(-500),
175
+ stdout: stdout.slice(-200),
176
+ }),
177
+ );
178
+ return;
179
+ }
180
+
181
+ resolve(json({ result: stdout.trim(), usage: stderr.trim() }));
182
+ });
183
+
184
+ child.on("error", (err: Error) => {
185
+ clearTimeout(timer);
186
+ resolve(json({ error: `Failed to spawn bridge: ${err.message}` }));
187
+ });
188
+ });
189
+ } catch (err) {
190
+ return json({
191
+ error: err instanceof Error ? err.message : String(err),
192
+ });
193
+ }
194
+ },
195
+ },
196
+ { optional: true },
197
+ );
198
+ }
@@ -0,0 +1,28 @@
1
+ {
2
+ "id": "grok-swarm",
3
+ "name": "Grok Multi-Agent Swarm",
4
+ "description": "Bridge to xAI Grok 4.20 Multi-Agent Beta (4-agent swarm) for codebase analysis, refactoring, reasoning, and code generation.",
5
+ "version": "1.0.0",
6
+ "configSchema": {
7
+ "type": "object",
8
+ "additionalProperties": false,
9
+ "properties": {
10
+ "defaultTimeout": {
11
+ "type": "number",
12
+ "description": "Default timeout in seconds (default: 120)"
13
+ },
14
+ "pythonPath": {
15
+ "type": "string",
16
+ "description": "Path to Python 3 binary"
17
+ },
18
+ "bridgeScript": {
19
+ "type": "string",
20
+ "description": "Path to grok_bridge.py script"
21
+ },
22
+ "defaultOutputDir": {
23
+ "type": "string",
24
+ "description": "Default output directory for file writes (default: ./grok-output/)"
25
+ }
26
+ }
27
+ }
28
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "@openclaw/grok-swarm",
3
+ "version": "1.0.0",
4
+ "description": "Grok 4.20 Multi-Agent Beta bridge plugin for OpenClaw",
5
+ "main": "index.ts",
6
+ "private": true
7
+ }