-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
384 lines (368 loc) · 12.1 KB
/
Copy pathmod.ts
File metadata and controls
384 lines (368 loc) · 12.1 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// deno-lint-ignore-file
import type { PluginContext, Tool, ToolCallResult } from 'cortex/plugins';
let config: Record<string, string> = {};
export async function onLoad(ctx: PluginContext): Promise<void> {
ctx.logger.info(`[cortex-plugin-docker] Loaded`);
config = {
dockerHost: (await ctx.config.get('dockerHost')) ?? 'unix:///var/run/docker.sock',
dockerRegistry: (await ctx.config.get('dockerRegistry')) ?? '',
kubeconfigPath: (await ctx.config.get('kubeconfigPath')) ?? '~/.kube/config',
defaultNamespace: (await ctx.config.get('defaultNamespace')) ?? 'default',
};
}
export async function onUnload(_ctx: PluginContext): Promise<void> {}
const docker_list: Tool = {
definition: {
name: 'docker_list',
description: 'List containers',
params: [
{
name: 'all',
type: 'boolean',
description: 'Show all containers (including stopped)',
required: false,
},
{
name: 'filter',
type: 'string',
description: 'Filter by status: running, stopped, all',
required: false,
},
],
capabilities: ['shell:run'],
},
execute: async (args: Record<string, unknown>, _ctx: PluginContext): Promise<ToolCallResult> => {
const start = Date.now();
try {
const all = args.all ?? false;
const filter = (args.filter as string) ?? 'all';
const cmdArgs = ['ps'];
if (all) cmdArgs.push('--all');
if (filter === 'running') cmdArgs.push('--filter', 'status=running');
else if (filter === 'stopped') cmdArgs.push('--filter', 'status=exited');
const p = new Deno.Command('docker', { args: cmdArgs });
const { stdout, stderr } = await p.output();
const output = new TextDecoder().decode(stdout);
if (!output.trim()) {
return {
toolName: 'docker_list',
success: true,
output: 'No containers found.',
durationMs: Date.now() - start,
};
}
return { toolName: 'docker_list', success: true, output, durationMs: Date.now() - start };
} catch (error) {
return {
toolName: 'docker_list',
success: false,
output: '',
error: `Failed to list containers: ${
error instanceof Error ? error.message : String(error)
}`,
durationMs: Date.now() - start,
};
}
},
};
const docker_run: Tool = {
definition: {
name: 'docker_run',
description: 'Run a container',
params: [
{ name: 'image', type: 'string', description: 'Docker image to run', required: true },
{ name: 'name', type: 'string', description: 'Container name', required: false },
{
name: 'ports',
type: 'string',
description: 'Port mapping (e.g. 8080:80)',
required: false,
},
{
name: 'env_vars',
type: 'string',
description: 'Environment variables as JSON',
required: false,
},
{ name: 'command', type: 'string', description: 'Override default command', required: false },
{
name: 'detach',
type: 'boolean',
description: 'Run container in background',
required: false,
},
],
capabilities: ['shell:run'],
},
execute: async (args: Record<string, unknown>, _ctx: PluginContext): Promise<ToolCallResult> => {
const start = Date.now();
try {
const image = args.image as string;
if (!image || typeof image !== 'string') {
return {
toolName: 'docker_run',
success: false,
output: '',
error: 'Image name is required',
durationMs: Date.now() - start,
};
}
const cmdArgs = ['run'];
if (args.detach) cmdArgs.push('--detach');
if (args.name) cmdArgs.push('--name', args.name as string);
if (args.ports) cmdArgs.push('-p', args.ports as string);
if (args.env_vars) {
try {
const envObj = JSON.parse(args.env_vars as string);
for (const [k, v] of Object.entries(envObj)) {
cmdArgs.push('-e', `${k}=${v}`);
}
} catch {
return {
toolName: 'docker_run',
success: false,
output: '',
error: 'Invalid env_vars JSON',
durationMs: Date.now() - start,
};
}
}
cmdArgs.push(image);
if (args.command) cmdArgs.push(args.command as string);
const p = new Deno.Command('docker', { args: cmdArgs });
const { stdout, stderr } = await p.output();
const output = new TextDecoder().decode(stdout);
const err = new TextDecoder().decode(stderr);
if (err) {
return {
toolName: 'docker_run',
success: false,
output: '',
error: err,
durationMs: Date.now() - start,
};
}
return {
toolName: 'docker_run',
success: true,
output: output.trim() || `Container started: ${image}`,
durationMs: Date.now() - start,
};
} catch (error) {
return {
toolName: 'docker_run',
success: false,
output: '',
error: `Failed to run container: ${error instanceof Error ? error.message : String(error)}`,
durationMs: Date.now() - start,
};
}
},
};
const docker_logs: Tool = {
definition: {
name: 'docker_logs',
description: 'Get container logs',
params: [
{ name: 'container_id', type: 'string', description: 'Container ID or name', required: true },
{ name: 'tail', type: 'number', description: 'Number of lines to show', required: false },
{ name: 'follow', type: 'boolean', description: 'Follow log output', required: false },
],
capabilities: ['shell:run'],
},
execute: async (args: Record<string, unknown>, _ctx: PluginContext): Promise<ToolCallResult> => {
const start = Date.now();
try {
const containerId = args.container_id as string;
if (!containerId) {
return {
toolName: 'docker_logs',
success: false,
output: '',
error: 'Container ID is required',
durationMs: Date.now() - start,
};
}
const tail = (args.tail as number) ?? 100;
const cmdArgs = ['logs', '--tail', String(tail)];
if (args.follow) cmdArgs.push('--follow');
cmdArgs.push(containerId);
const p = new Deno.Command('docker', { args: cmdArgs });
const { stdout, stderr } = await p.output();
const output = new TextDecoder().decode(stdout);
if (!output.trim()) {
return {
toolName: 'docker_logs',
success: true,
output: '(no log output)',
durationMs: Date.now() - start,
};
}
return { toolName: 'docker_logs', success: true, output, durationMs: Date.now() - start };
} catch (error) {
return {
toolName: 'docker_logs',
success: false,
output: '',
error: `Failed to get logs: ${error instanceof Error ? error.message : String(error)}`,
durationMs: Date.now() - start,
};
}
},
};
const docker_stop: Tool = {
definition: {
name: 'docker_stop',
description: 'Stop container',
params: [
{ name: 'container_id', type: 'string', description: 'Container ID or name', required: true },
{ name: 'force', type: 'boolean', description: 'Force stop the container', required: false },
],
capabilities: ['shell:run'],
},
execute: async (args: Record<string, unknown>, _ctx: PluginContext): Promise<ToolCallResult> => {
const start = Date.now();
try {
const containerId = args.container_id as string;
if (!containerId) {
return {
toolName: 'docker_stop',
success: false,
output: '',
error: 'Container ID is required',
durationMs: Date.now() - start,
};
}
const cmdArgs = args.force ? ['rm', '-f', containerId] : ['stop', containerId];
const p = new Deno.Command('docker', { args: cmdArgs });
const { stdout, stderr } = await p.output();
const err = new TextDecoder().decode(stderr);
if (err && !err.includes(containerId)) {
return {
toolName: 'docker_stop',
success: false,
output: '',
error: err,
durationMs: Date.now() - start,
};
}
return {
toolName: 'docker_stop',
success: true,
output: `Container ${containerId} stopped`,
durationMs: Date.now() - start,
};
} catch (error) {
return {
toolName: 'docker_stop',
success: false,
output: '',
error: `Failed to stop container: ${
error instanceof Error ? error.message : String(error)
}`,
durationMs: Date.now() - start,
};
}
},
};
const k8s_get_pods: Tool = {
definition: {
name: 'k8s_get_pods',
description: 'Get Kubernetes pods',
params: [
{ name: 'namespace', type: 'string', description: 'Kubernetes namespace', required: false },
{
name: 'label_selector',
type: 'string',
description: 'Label selector filter',
required: false,
},
],
capabilities: ['shell:run'],
},
execute: async (args: Record<string, unknown>, _ctx: PluginContext): Promise<ToolCallResult> => {
const start = Date.now();
try {
const namespace = (args.namespace as string) ?? config.defaultNamespace ?? 'default';
const cmdArgs = ['get', 'pods', '-n', namespace];
if (args.label_selector) cmdArgs.push('-l', args.label_selector as string);
const p = new Deno.Command('kubectl', { args: cmdArgs });
const { stdout } = await p.output();
const output = new TextDecoder().decode(stdout);
return {
toolName: 'k8s_get_pods',
success: true,
output: output.trim() || 'No pods found',
durationMs: Date.now() - start,
};
} catch (error) {
return {
toolName: 'k8s_get_pods',
success: false,
output: '',
error: `Failed to get pods: ${error instanceof Error ? error.message : String(error)}`,
durationMs: Date.now() - start,
};
}
},
};
const k8s_describe: Tool = {
definition: {
name: 'k8s_describe',
description: 'Describe a K8s resource',
params: [
{ name: 'resource_type', type: 'string', description: 'Type of resource', required: true },
{ name: 'name', type: 'string', description: 'Resource name', required: true },
{ name: 'namespace', type: 'string', description: 'Kubernetes namespace', required: false },
],
capabilities: ['shell:run'],
},
execute: async (args: Record<string, unknown>, _ctx: PluginContext): Promise<ToolCallResult> => {
const start = Date.now();
try {
const resourceType = args.resource_type as string;
const name = args.name as string;
const namespace = (args.namespace as string) ?? config.defaultNamespace ?? 'default';
if (!resourceType || !name) {
return {
toolName: 'k8s_describe',
success: false,
output: '',
error: 'resource_type and name are required',
durationMs: Date.now() - start,
};
}
const validResources = ['pod', 'deployment', 'service', 'node'];
if (!validResources.includes(resourceType)) {
return {
toolName: 'k8s_describe',
success: false,
output: '',
error: `Invalid resource_type: ${resourceType}. Valid: ${validResources.join(', ')}`,
durationMs: Date.now() - start,
};
}
const cmdArgs = ['describe', resourceType, name, '-n', namespace];
const p = new Deno.Command('kubectl', { args: cmdArgs });
const { stdout } = await p.output();
const output = new TextDecoder().decode(stdout);
return { toolName: 'k8s_describe', success: true, output, durationMs: Date.now() - start };
} catch (error) {
return {
toolName: 'k8s_describe',
success: false,
output: '',
error: `Failed to describe: ${error instanceof Error ? error.message : String(error)}`,
durationMs: Date.now() - start,
};
}
},
};
export const tools: Tool[] = [
docker_list,
docker_run,
docker_logs,
docker_stop,
k8s_get_pods,
k8s_describe,
];