@agent-smith/server 0.0.5 → 0.0.7

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, };
@@ -0,0 +1,4 @@
1
+ import type Router from "@koa/router";
2
+ declare function getOrCreateAppConfigFileRoute(r: Router): void;
3
+ declare function updateAppConfigFileRoute(r: Router): void;
4
+ export { getOrCreateAppConfigFileRoute, updateAppConfigFileRoute, };
@@ -0,0 +1,40 @@
1
+ import { getConfigPath } from "@agent-smith/cli";
2
+ import fs, { existsSync } from "node:fs";
3
+ import path from "node:path";
4
+ import yaml from "yaml";
5
+ function getOrCreateAppConfigFile(appName) {
6
+ const { confDir } = getConfigPath("agent-smith/" + appName, "config.db");
7
+ if (!fs.existsSync(confDir)) {
8
+ fs.mkdirSync(confDir);
9
+ }
10
+ const fp = path.join(confDir, "config.yml");
11
+ let fc = "";
12
+ if (!existsSync(fp)) {
13
+ fs.writeFileSync(fp, "");
14
+ return {};
15
+ }
16
+ else {
17
+ fc = fs.readFileSync(fp, { encoding: "utf-8" });
18
+ return yaml.parse(fc);
19
+ }
20
+ }
21
+ function updateAppConfigFile(appName, content) {
22
+ const { confDir } = getConfigPath("agent-smith/" + appName, "config.db");
23
+ const fp = path.join(confDir, "config.yml");
24
+ const txt = yaml.stringify(content);
25
+ fs.writeFileSync(fp, txt, { encoding: "utf-8" });
26
+ }
27
+ function getOrCreateAppConfigFileRoute(r) {
28
+ r.get('/app/:name/conf', async (ctx, next) => {
29
+ ctx.body = getOrCreateAppConfigFile(ctx.params.name);
30
+ ctx.status = 200;
31
+ });
32
+ }
33
+ function updateAppConfigFileRoute(r) {
34
+ r.post('/app/:name/update', async (ctx, next) => {
35
+ const payload = ctx.request.body;
36
+ updateAppConfigFile(ctx.params.name, payload);
37
+ ctx.status = 200;
38
+ });
39
+ }
40
+ export { getOrCreateAppConfigFileRoute, updateAppConfigFileRoute, };
@@ -0,0 +1,4 @@
1
+ import type Router from '@koa/router';
2
+ declare function getBackendsRoute(r: Router): void;
3
+ declare function setBackendRoute(r: Router): void;
4
+ export { getBackendsRoute, setBackendRoute, };
@@ -0,0 +1,24 @@
1
+ import { db, setBackend } from '@agent-smith/cli';
2
+ function getBackendsRoute(r) {
3
+ r.get('/backends', async (ctx, next) => {
4
+ const backends = db.readBackends();
5
+ ctx.body = backends;
6
+ ctx.status = 200;
7
+ });
8
+ }
9
+ function setBackendRoute(r) {
10
+ r.get('/backend/:name', async (ctx, next) => {
11
+ const name = ctx.params.name;
12
+ console.log("Loading backend", name);
13
+ const ok = await setBackend(name, true);
14
+ if (!ok) {
15
+ ctx.status = 400;
16
+ ctx.body = "backend not found";
17
+ }
18
+ else {
19
+ ctx.body = ok;
20
+ ctx.status = 200;
21
+ }
22
+ });
23
+ }
24
+ export { getBackendsRoute, setBackendRoute, };
@@ -11,14 +11,13 @@ function getConfRoute(r) {
11
11
  ctx.body = conf;
12
12
  ctx.status = 200;
13
13
  }
14
- await next();
15
14
  });
16
15
  }
17
16
  function createConfRoute(r) {
18
17
  r.get('/conf/create', async (ctx, next) => {
19
18
  let cfp = null;
20
19
  try {
21
- cfp = createConfigFile();
20
+ cfp = createConfigFile(undefined, ["llamacpp"]);
22
21
  }
23
22
  catch (e) {
24
23
  console.error("500", e);
@@ -30,7 +29,6 @@ function createConfRoute(r) {
30
29
  ctx.status = 201;
31
30
  await updateConfCmd([cfp]);
32
31
  }
33
- await next();
34
32
  });
35
33
  }
36
34
  export { createConfRoute, getConfRoute };
@@ -22,7 +22,6 @@ function addFolderRoute(r) {
22
22
  console.log("Updating db features from config file");
23
23
  await updateConfCmd([path]);
24
24
  ctx.status = 202;
25
- await next();
26
25
  });
27
26
  }
28
27
  export { addFolderRoute, };
@@ -7,5 +7,8 @@ import { getToolsRoute } from "./tools.js";
7
7
  import { getStateRoute } from "./state.js";
8
8
  import { installPluginRoute } from "./plugins.js";
9
9
  import { addFolderRoute } from "./folders.js";
10
- const baseRoutes = new Array(getConfRoute, getTasksRoute, getTaskRoute, getAgentRoute, getAgentsRoute, getModelsCmd, getToolsRoute, getTaskSettingsCmd, getStateRoute, createConfRoute, updateTaskSettingsCmd, installPluginRoute, addFolderRoute);
10
+ import { getWorkflowRoute, getWorkflowsRoute } from "./workflows.js";
11
+ import { getBackendsRoute, setBackendRoute } from "./backends.js";
12
+ import { getOrCreateAppConfigFileRoute, updateAppConfigFileRoute } from "./apps.js";
13
+ const baseRoutes = new Array(getConfRoute, getTasksRoute, getTaskRoute, getAgentRoute, getAgentsRoute, getModelsCmd, getToolsRoute, getTaskSettingsCmd, getStateRoute, createConfRoute, updateTaskSettingsCmd, installPluginRoute, addFolderRoute, getWorkflowRoute, getWorkflowsRoute, getBackendsRoute, setBackendRoute, getOrCreateAppConfigFileRoute, updateAppConfigFileRoute);
11
14
  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, };
@@ -28,7 +28,6 @@ function installPluginRoute(r) {
28
28
  console.log("Updating db features from config file");
29
29
  await updateConfCmd([path]);
30
30
  ctx.status = 202;
31
- await next();
32
31
  });
33
32
  }
34
33
  export { installPluginRoute, };
@@ -1,19 +1,30 @@
1
1
  import { getConfigPath, init } from '@agent-smith/cli';
2
2
  import fs from "node:fs";
3
+ import { getConfig } from '../utils.js';
3
4
  function getStateRoute(r) {
4
5
  r.get('/state', async (ctx, next) => {
5
- console.log("STATE ROUTE");
6
+ //console.log('STATE URL --> ' + ctx.request.url);
7
+ //console.log("STATE ROUTE");
6
8
  const { confDir, dbPath } = getConfigPath("agent-smith", "config.db");
7
- console.log("conf paths", confDir, dbPath);
9
+ //console.log("conf paths", confDir, dbPath);
8
10
  if (!fs.existsSync(dbPath)) {
9
11
  ctx.body = "no db found at " + dbPath;
10
12
  ctx.status = 202;
11
13
  }
12
14
  else {
13
- ctx.status = 200;
14
15
  await init();
16
+ const { found, conf } = getConfig();
17
+ if (!found) {
18
+ ctx.body = "can not find config path in db";
19
+ ctx.status = 400;
20
+ }
21
+ else {
22
+ ctx.body = conf;
23
+ ctx.status = 200;
24
+ }
15
25
  }
16
- await next();
26
+ //console.log("STATE", ctx.status)
27
+ //return next()
17
28
  });
18
29
  }
19
30
  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, };
@@ -0,0 +1,4 @@
1
+ import type Router from '@koa/router';
2
+ declare function getWorkflowsRoute(r: Router): void;
3
+ declare function getWorkflowRoute(r: Router): void;
4
+ export { getWorkflowRoute, getWorkflowsRoute, };
@@ -0,0 +1,23 @@
1
+ import { db, fs } from '@agent-smith/cli';
2
+ function getWorkflowsRoute(r) {
3
+ r.get('/workflows', async (ctx, next) => {
4
+ const w = db.readFeaturesType("workflow");
5
+ ctx.body = w;
6
+ ctx.status = 200;
7
+ });
8
+ }
9
+ function getWorkflowRoute(r) {
10
+ r.get('/workflow/:id', async (ctx, next) => {
11
+ //console.log(ctx.params.id)
12
+ const { found, workflow } = await fs.readWorkflow(ctx.params.id);
13
+ if (!found) {
14
+ ctx.body = "workflow not found";
15
+ ctx.status = 400;
16
+ }
17
+ else {
18
+ ctx.body = workflow;
19
+ ctx.status = 200;
20
+ }
21
+ });
22
+ }
23
+ export { getWorkflowRoute, getWorkflowsRoute, };
@@ -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 = {};
@@ -62,6 +60,7 @@ function runserver(routes, staticDir) {
62
60
  if (!msg?.options) {
63
61
  msg.options = {};
64
62
  }
63
+ msg.options.nocli = true;
65
64
  //console.log("ABO", abort);
66
65
  msg.options.abort = abort;
67
66
  //console.log(msg)
@@ -171,7 +170,7 @@ function runserver(routes, staticDir) {
171
170
  else if (msg.feature == "agent") {
172
171
  msg.options.onToolsTurnStart = (tcs) => {
173
172
  const rsm = {
174
- type: "finalresult",
173
+ type: "toolsturnstart",
175
174
  msg: JSON.stringify(tcs),
176
175
  };
177
176
  ctx.websocket.send(JSON.stringify(rsm));
@@ -235,6 +234,7 @@ function runserver(routes, staticDir) {
235
234
  type: "toolcallend",
236
235
  msg: `${id}<|xtool_call_id|>` + toolResData,
237
236
  };
237
+ //console.log("TOOL CALL END", toolResData);
238
238
  ctx.websocket.send(JSON.stringify(rsm));
239
239
  };
240
240
  msg.options.confirmToolUsage = async (tc) => {
@@ -253,26 +253,28 @@ function runserver(routes, staticDir) {
253
253
  };
254
254
  msg.options.isAgent = true;
255
255
  try {
256
- let buf = "";
256
+ //let buf = "";
257
257
  msg.options.onToken = (t) => {
258
- buf += t;
259
- process.stdout.write(t);
260
- };
261
- const it = setInterval(() => {
262
- if (buf == "") {
263
- return;
264
- }
265
- ;
266
- const rsm = {
258
+ const rsm2 = {
267
259
  type: "token",
268
- msg: buf,
260
+ msg: t,
269
261
  };
270
- ctx.websocket.send(JSON.stringify(rsm));
271
- buf = "";
272
- }, sendTokensInterval);
262
+ ctx.websocket.send(JSON.stringify(rsm2));
263
+ //buf += t;
264
+ process.stdout.write(t);
265
+ };
266
+ /*const it = setInterval(() => {
267
+ if (buf == "") { return };
268
+ const rsm: WsRawServerMsg = {
269
+ type: "token",
270
+ msg: buf,
271
+ }
272
+ ctx.websocket.send(JSON.stringify(rsm));
273
+ buf = "";
274
+ }, sendTokensInterval);*/
273
275
  const res = await executeTask(msg.command, msg.payload, msg.options);
274
276
  //setTimeout(() => {
275
- clearInterval(it);
277
+ //clearInterval(it);
276
278
  //}, sendTokensInterval);
277
279
  const ht = JSON.stringify(res.template.history.pop());
278
280
  //console.log("FINAL MSG", ht)
@@ -326,17 +328,22 @@ function runserver(routes, staticDir) {
326
328
  abort = new AbortController();
327
329
  });
328
330
  }));
331
+ if (staticDir) {
332
+ app.use(serve(staticDir));
333
+ }
334
+ app.use(logger);
329
335
  app.use(router.routes()).use(router.allowedMethods());
330
336
  // 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
- });*/
337
+ app.use((ctx) => {
338
+ if (!ctx.matched || ctx.matched.length === 0) {
339
+ ctx.status = 404;
340
+ //console.log("404 ROUTE", ctx);
341
+ ctx.body = {
342
+ error: 'Not Found',
343
+ path: ctx.path
344
+ };
345
+ }
346
+ });
340
347
  app.listen(5184, () => {
341
348
  console.log('Please open url localhost:5184 in a browser');
342
349
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent-smith/server",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Agent Smith Nodejs server",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
@@ -10,27 +10,28 @@
10
10
  },
11
11
  "type": "module",
12
12
  "dependencies": {
13
- "@agent-smith/cli": "^0.0.114",
13
+ "@agent-smith/cli": "^0.0.115",
14
14
  "@koa/cors": "^5.0.0",
15
- "@koa/router": "^15.3.0",
15
+ "@koa/router": "^15.4.0",
16
16
  "ansi-colors": "^4.1.3",
17
- "koa": "^3.1.2",
17
+ "koa": "^3.2.0",
18
18
  "koa-bodyparser": "^4.4.1",
19
19
  "koa-route": "^4.0.1",
20
20
  "koa-static": "^5.0.0",
21
- "koa-websocket": "^7.0.0"
21
+ "koa-websocket": "^7.0.0",
22
+ "yaml": "^2.8.3"
22
23
  },
23
24
  "devDependencies": {
24
25
  "@agent-smith/types": "^0.0.3",
25
26
  "@locallm/types": "^0.7.1",
26
27
  "@types/better-sqlite3": "^7.6.13",
27
- "@types/koa": "^3.0.1",
28
+ "@types/koa": "^3.0.2",
28
29
  "@types/koa__cors": "^5.0.1",
29
30
  "@types/koa-bodyparser": "^4.3.13",
30
31
  "@types/koa-route": "^3.2.9",
31
32
  "@types/koa-static": "^4.0.4",
32
33
  "@types/koa-websocket": "^5.0.11",
33
- "@types/node": "^25.3.2",
34
+ "@types/node": "^25.5.0",
34
35
  "ts-node": "^10.9.2",
35
36
  "tslib": "2.8.1",
36
37
  "typescript": "^5.9.3"