@agent-smith/server 0.0.4 → 0.0.6

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.
@@ -4,7 +4,8 @@ function getAgentsRoute(r) {
4
4
  const agents = db.readFeaturesType("agent");
5
5
  ctx.body = agents;
6
6
  ctx.status = 200;
7
- await next();
7
+ //console.log("AGENTS", ctx.status);
8
+ //await next()
8
9
  });
9
10
  }
10
11
  function getAgentRoute(r) {
@@ -13,7 +14,7 @@ function getAgentRoute(r) {
13
14
  const taskSpec = fs.openTaskSpec(ctx.params.id, true);
14
15
  ctx.body = taskSpec.taskFileSpec;
15
16
  ctx.status = 200;
16
- await next();
17
+ //await next()
17
18
  });
18
19
  }
19
20
  export { getAgentRoute, getAgentsRoute, };
@@ -1,31 +1,16 @@
1
- import { createConfigFile, db, readConf, updateConfCmd } from '@agent-smith/cli';
1
+ import { createConfigFile, updateConfCmd } from '@agent-smith/cli';
2
+ import { getConfig } from '../utils.js';
2
3
  function getConfRoute(r) {
3
4
  r.get('/conf', async (ctx, next) => {
4
- const fp = db.readFilePaths();
5
- let confFilePath = "";
6
- //let promptFilePath = "";
7
- for (const p of fp) {
8
- if (p.name == "conf") {
9
- confFilePath = p.path;
10
- break;
11
- }
12
- }
13
- if (confFilePath == "") {
5
+ const { found, conf } = getConfig();
6
+ if (!found) {
14
7
  ctx.body = "can not find config path in db";
15
8
  ctx.status = 400;
16
9
  }
17
10
  else {
18
- const { found, data } = readConf(confFilePath);
19
- if (!found) {
20
- ctx.status = 400;
21
- ctx.body = "config file not found at " + confFilePath;
22
- }
23
- else {
24
- ctx.body = data;
25
- ctx.status = 200;
26
- }
11
+ ctx.body = conf;
12
+ ctx.status = 200;
27
13
  }
28
- await next();
29
14
  });
30
15
  }
31
16
  function createConfRoute(r) {
@@ -44,7 +29,6 @@ function createConfRoute(r) {
44
29
  ctx.status = 201;
45
30
  await updateConfCmd([cfp]);
46
31
  }
47
- await next();
48
32
  });
49
33
  }
50
34
  export { createConfRoute, getConfRoute };
@@ -0,0 +1,3 @@
1
+ import type Router from '@koa/router';
2
+ declare function addFolderRoute(r: Router): void;
3
+ export { addFolderRoute, };
@@ -0,0 +1,27 @@
1
+ import { updateConfCmd, updateConfigFile, init } from '@agent-smith/cli';
2
+ import { getConfig } from '../utils.js';
3
+ function addFolderRoute(r) {
4
+ r.post('/folders/add', async (ctx, next) => {
5
+ const payload = ctx.request.body;
6
+ await init();
7
+ const { found, conf, path } = getConfig();
8
+ if (!found) {
9
+ throw new Error("no config file found");
10
+ }
11
+ ;
12
+ if (!conf?.features) {
13
+ conf.features = [];
14
+ }
15
+ for (const p of payload) {
16
+ conf.features.push(p);
17
+ }
18
+ ;
19
+ console.log("Updating config file at", path);
20
+ console.dir(conf, { depth: 3 });
21
+ updateConfigFile(conf, path);
22
+ console.log("Updating db features from config file");
23
+ await updateConfCmd([path]);
24
+ ctx.status = 202;
25
+ });
26
+ }
27
+ export { addFolderRoute, };
@@ -5,5 +5,7 @@ import { getAgentRoute, getAgentsRoute } from "./agents.js";
5
5
  import { createConfRoute, getConfRoute } from "./conf.js";
6
6
  import { getToolsRoute } from "./tools.js";
7
7
  import { getStateRoute } from "./state.js";
8
- const baseRoutes = new Array(getConfRoute, getTasksRoute, getTaskRoute, getAgentRoute, getAgentsRoute, getModelsCmd, getToolsRoute, getTaskSettingsCmd, getStateRoute, createConfRoute, updateTaskSettingsCmd);
8
+ import { installPluginRoute } from "./plugins.js";
9
+ import { addFolderRoute } from "./folders.js";
10
+ const baseRoutes = new Array(getConfRoute, getTasksRoute, getTaskRoute, getAgentRoute, getAgentsRoute, getModelsCmd, getToolsRoute, getTaskSettingsCmd, getStateRoute, createConfRoute, updateTaskSettingsCmd, installPluginRoute, addFolderRoute);
9
11
  export { baseRoutes };
@@ -8,7 +8,6 @@ function getModelsCmd(r) {
8
8
  catch (e) {
9
9
  ctx.body = "error reading the models";
10
10
  ctx.status = 502;
11
- await next();
12
11
  return;
13
12
  }
14
13
  const ms = {};
@@ -32,7 +31,6 @@ function getModelsCmd(r) {
32
31
  });
33
32
  ctx.body = ms;
34
33
  ctx.status = 200;
35
- await next();
36
34
  });
37
35
  }
38
36
  export { getModelsCmd, };
@@ -0,0 +1,3 @@
1
+ import type Router from '@koa/router';
2
+ declare function installPluginRoute(r: Router): void;
3
+ export { installPluginRoute, };
@@ -0,0 +1,33 @@
1
+ import { execute, updateConfCmd, updateConfigFile, init } from '@agent-smith/cli';
2
+ import { getConfig } from '../utils.js';
3
+ function installPluginRoute(r) {
4
+ r.post('/plugins/install', async (ctx, next) => {
5
+ const payload = ctx.request.body;
6
+ console.log("P", payload);
7
+ for (const p of payload) {
8
+ console.log("Installing", p, "plugin");
9
+ const res = await execute("npm", ["i", "-g", p]);
10
+ console.log(p, res);
11
+ }
12
+ await init();
13
+ const { found, conf, path } = getConfig();
14
+ if (!found) {
15
+ throw new Error("no config file found");
16
+ }
17
+ ;
18
+ if (!conf?.plugins) {
19
+ conf.plugins = [];
20
+ }
21
+ for (const p of payload) {
22
+ conf.plugins?.push(p);
23
+ }
24
+ ;
25
+ console.log("Updating config file at", path);
26
+ console.dir(conf, { depth: 3 });
27
+ updateConfigFile(conf, path);
28
+ console.log("Updating db features from config file");
29
+ await updateConfCmd([path]);
30
+ ctx.status = 202;
31
+ });
32
+ }
33
+ export { installPluginRoute, };
@@ -2,9 +2,10 @@ import { getConfigPath, init } from '@agent-smith/cli';
2
2
  import fs from "node:fs";
3
3
  function getStateRoute(r) {
4
4
  r.get('/state', async (ctx, next) => {
5
- console.log("STATE ROUTE");
5
+ //console.log('STATE URL --> ' + ctx.request.url);
6
+ //console.log("STATE ROUTE");
6
7
  const { confDir, dbPath } = getConfigPath("agent-smith", "config.db");
7
- console.log("conf paths", confDir, dbPath);
8
+ //console.log("conf paths", confDir, dbPath);
8
9
  if (!fs.existsSync(dbPath)) {
9
10
  ctx.body = "no db found at " + dbPath;
10
11
  ctx.status = 202;
@@ -13,7 +14,8 @@ function getStateRoute(r) {
13
14
  ctx.status = 200;
14
15
  await init();
15
16
  }
16
- await next();
17
+ //console.log("STATE", ctx.status)
18
+ //return next()
17
19
  });
18
20
  }
19
21
  export { getStateRoute, };
@@ -4,7 +4,6 @@ function getTaskSettingsCmd(r) {
4
4
  const ts = db.getTaskSettings();
5
5
  ctx.body = ts;
6
6
  ctx.status = 200;
7
- await next();
8
7
  });
9
8
  }
10
9
  function updateTaskSettingsCmd(r) {
@@ -17,7 +16,6 @@ function updateTaskSettingsCmd(r) {
17
16
  db.getTaskSettings(true);
18
17
  ctx.body = ts;
19
18
  ctx.status = 200;
20
- await next();
21
19
  });
22
20
  }
23
21
  export { getTaskSettingsCmd, updateTaskSettingsCmd, };
@@ -4,7 +4,6 @@ function getTasksRoute(r) {
4
4
  const tasks = db.readFeaturesType("task");
5
5
  ctx.body = tasks;
6
6
  ctx.status = 200;
7
- await next();
8
7
  });
9
8
  }
10
9
  function getTaskRoute(r) {
@@ -13,7 +12,6 @@ function getTaskRoute(r) {
13
12
  const taskSpec = fs.openTaskSpec(ctx.params.id);
14
13
  ctx.body = taskSpec.taskFileSpec;
15
14
  ctx.status = 200;
16
- await next();
17
15
  });
18
16
  }
19
17
  function saveTasksRoute(r) {
@@ -21,7 +19,6 @@ function saveTasksRoute(r) {
21
19
  //const payload = ctx.request.body;
22
20
  ctx.body = "ok";
23
21
  ctx.status = 200;
24
- await next();
25
22
  });
26
23
  }
27
24
  export { getTasksRoute, getTaskRoute, };
@@ -24,7 +24,6 @@ function getToolsRoute(r) {
24
24
  ctx.body = tools;
25
25
  ctx.status = 200;
26
26
  }
27
- await next();
28
27
  });
29
28
  }
30
29
  export { getToolsRoute, };
@@ -21,17 +21,18 @@ if (argv.length > 2) {
21
21
  }*/
22
22
  const logger = async (ctx, next) => {
23
23
  const start = Date.now();
24
+ //console.log('LOGGER BEFORE URL --> ' + ctx.request.url);
24
25
  await next();
25
26
  const duration = Date.now() - start;
26
27
  console.log(`${ctx.method} ${ctx.url} - ${ctx.status} - ${duration}ms`);
28
+ //console.log('LOGGER AFTER URL --> ' + ctx.request.url);
27
29
  };
28
30
  const app = websockify(new Koa());
29
31
  app.use(bodyParser());
30
- app.use(logger);
31
32
  app.use(cors({
32
33
  credentials: true
33
34
  }));
34
- app.ws.use(function (ctx, next) {
35
+ app.ws.use((ctx, next) => {
35
36
  return next();
36
37
  });
37
38
  function createAwaiter() {
@@ -48,9 +49,6 @@ function createAwaiter() {
48
49
  };
49
50
  }
50
51
  function runserver(routes, staticDir) {
51
- if (staticDir) {
52
- app.use(serve(staticDir));
53
- }
54
52
  const router = useRouter(routes);
55
53
  const sendTokensInterval = 100;
56
54
  const confirmToolCalls = {};
@@ -326,17 +324,22 @@ function runserver(routes, staticDir) {
326
324
  abort = new AbortController();
327
325
  });
328
326
  }));
327
+ if (staticDir) {
328
+ app.use(serve(staticDir));
329
+ }
330
+ app.use(logger);
329
331
  app.use(router.routes()).use(router.allowedMethods());
330
332
  // 404 middleware - runs after router
331
- /*app.use((ctx) => {
332
- if (!ctx.matched || ctx.matched.length === 0) {
333
- ctx.status = 404;
334
- ctx.body = {
335
- error: 'Not Found',
336
- path: ctx.path
337
- };
338
- }
339
- });*/
333
+ app.use((ctx) => {
334
+ if (!ctx.matched || ctx.matched.length === 0) {
335
+ ctx.status = 404;
336
+ //console.log("404 ROUTE", ctx);
337
+ ctx.body = {
338
+ error: 'Not Found',
339
+ path: ctx.path
340
+ };
341
+ }
342
+ });
340
343
  app.listen(5184, () => {
341
344
  console.log('Please open url localhost:5184 in a browser');
342
345
  });
@@ -0,0 +1,7 @@
1
+ import type { ConfigFile } from "@agent-smith/types";
2
+ declare function getConfig(): {
3
+ found: boolean;
4
+ conf: ConfigFile;
5
+ path: string;
6
+ };
7
+ export { getConfig, };
package/dist/utils.js ADDED
@@ -0,0 +1,18 @@
1
+ import { db, readConf } from "@agent-smith/cli";
2
+ function getConfig() {
3
+ const fp = db.readFilePaths();
4
+ let confFilePath = "";
5
+ //let promptFilePath = "";
6
+ for (const p of fp) {
7
+ if (p.name == "conf") {
8
+ confFilePath = p.path;
9
+ break;
10
+ }
11
+ }
12
+ if (confFilePath == "") {
13
+ return { found: false, conf: {}, path: "" };
14
+ }
15
+ const c = readConf(confFilePath);
16
+ return { found: c.found, conf: c.data, path: confFilePath };
17
+ }
18
+ export { getConfig, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent-smith/server",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Agent Smith Nodejs server",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "type": "module",
12
12
  "dependencies": {
13
- "@agent-smith/cli": "^0.0.113",
13
+ "@agent-smith/cli": "^0.0.114",
14
14
  "@koa/cors": "^5.0.0",
15
15
  "@koa/router": "^15.3.0",
16
16
  "ansi-colors": "^4.1.3",