@agent-smith/server 0.0.2 → 0.0.3

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,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env node
2
+ import { fileURLToPath } from 'url';
3
+ import path from 'path';
4
+ import { runserver } from './server/server.js';
5
+ import { baseRoutes } from "./routes/index.js";
6
+ const __filename = fileURLToPath(import.meta.url);
7
+ const dirpath = path.resolve(path.dirname(__filename), "../");
8
+ async function main() {
9
+ //console.log(dirpath, process.env.NODE_ENV);
10
+ let staticPath = undefined;
11
+ if (process.env.NODE_ENV != "development") {
12
+ staticPath = dirpath;
13
+ }
14
+ //console.log("B", baseRoutes);
15
+ runserver(baseRoutes, staticPath);
16
+ }
17
+ (async () => {
18
+ await main();
19
+ })();
@@ -0,0 +1,4 @@
1
+ import type Router from '@koa/router';
2
+ declare function getAgentsRoute(r: Router): void;
3
+ declare function getAgentRoute(r: Router): void;
4
+ export { getAgentRoute, getAgentsRoute, };
@@ -0,0 +1,19 @@
1
+ import { db, fs } from '@agent-smith/cli';
2
+ function getAgentsRoute(r) {
3
+ r.get('/agents', async (ctx, next) => {
4
+ const agents = db.readFeaturesType("agent");
5
+ ctx.body = agents;
6
+ ctx.status = 200;
7
+ await next();
8
+ });
9
+ }
10
+ function getAgentRoute(r) {
11
+ r.get('/agent/:id', async (ctx, next) => {
12
+ //console.log(ctx.params.id)
13
+ const taskSpec = fs.openTaskSpec(ctx.params.id, true);
14
+ ctx.body = taskSpec.taskFileSpec;
15
+ ctx.status = 200;
16
+ await next();
17
+ });
18
+ }
19
+ export { getAgentRoute, getAgentsRoute, };
@@ -0,0 +1,4 @@
1
+ import type Router from '@koa/router';
2
+ declare function getConfRoute(r: Router): void;
3
+ declare function createConfRoute(r: Router): void;
4
+ export { createConfRoute, getConfRoute };
@@ -0,0 +1,50 @@
1
+ import { createConfigFile, db, readConf, updateConfCmd } from '@agent-smith/cli';
2
+ function getConfRoute(r) {
3
+ 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 == "") {
14
+ ctx.body = "can not find config path in db";
15
+ ctx.status = 400;
16
+ }
17
+ 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
+ }
27
+ }
28
+ await next();
29
+ });
30
+ }
31
+ function createConfRoute(r) {
32
+ r.get('/conf/create', async (ctx, next) => {
33
+ let cfp = null;
34
+ try {
35
+ cfp = createConfigFile();
36
+ }
37
+ catch (e) {
38
+ console.error("500", e);
39
+ ctx.body = e;
40
+ ctx.status = 500;
41
+ }
42
+ if (cfp) {
43
+ ctx.body = cfp;
44
+ ctx.status = 201;
45
+ await updateConfCmd([cfp]);
46
+ }
47
+ await next();
48
+ });
49
+ }
50
+ export { createConfRoute, getConfRoute };
@@ -1,4 +1,9 @@
1
1
  import { getModelsCmd } from "./models.js";
2
2
  import { getTaskSettingsCmd, updateTaskSettingsCmd } from "./task_settings.js";
3
- const baseRoutes = new Array(getModelsCmd, getTaskSettingsCmd, updateTaskSettingsCmd);
3
+ import { getTaskRoute, getTasksRoute } from "./tasks.js";
4
+ import { getAgentRoute, getAgentsRoute } from "./agents.js";
5
+ import { createConfRoute, getConfRoute } from "./conf.js";
6
+ import { getToolsRoute } from "./tools.js";
7
+ import { getStateRoute } from "./state.js";
8
+ const baseRoutes = new Array(getConfRoute, getTasksRoute, getTaskRoute, getAgentRoute, getAgentsRoute, getModelsCmd, getToolsRoute, getTaskSettingsCmd, getStateRoute, createConfRoute, updateTaskSettingsCmd);
4
9
  export { baseRoutes };
@@ -0,0 +1,3 @@
1
+ import type Router from '@koa/router';
2
+ declare function getStateRoute(r: Router): void;
3
+ export { getStateRoute, };
@@ -0,0 +1,19 @@
1
+ import { getConfigPath, init } from '@agent-smith/cli';
2
+ import fs from "node:fs";
3
+ function getStateRoute(r) {
4
+ r.get('/state', async (ctx, next) => {
5
+ console.log("STATE ROUTE");
6
+ const { confDir, dbPath } = getConfigPath("agent-smith", "config.db");
7
+ console.log("conf paths", confDir, dbPath);
8
+ if (!fs.existsSync(dbPath)) {
9
+ ctx.body = "no db found at " + dbPath;
10
+ ctx.status = 202;
11
+ }
12
+ else {
13
+ ctx.status = 200;
14
+ await init();
15
+ }
16
+ await next();
17
+ });
18
+ }
19
+ export { getStateRoute, };
@@ -0,0 +1,4 @@
1
+ import type Router from '@koa/router';
2
+ declare function getTasksRoute(r: Router): void;
3
+ declare function getTaskRoute(r: Router): void;
4
+ export { getTasksRoute, getTaskRoute, };
@@ -0,0 +1,27 @@
1
+ import { db, fs } from '@agent-smith/cli';
2
+ function getTasksRoute(r) {
3
+ r.get('/tasks', async (ctx, next) => {
4
+ const tasks = db.readFeaturesType("task");
5
+ ctx.body = tasks;
6
+ ctx.status = 200;
7
+ await next();
8
+ });
9
+ }
10
+ function getTaskRoute(r) {
11
+ r.get('/task/:id', async (ctx, next) => {
12
+ //console.log(ctx.params.id)
13
+ const taskSpec = fs.openTaskSpec(ctx.params.id);
14
+ ctx.body = taskSpec.taskFileSpec;
15
+ ctx.status = 200;
16
+ await next();
17
+ });
18
+ }
19
+ function saveTasksRoute(r) {
20
+ r.post('/save', async (ctx, next) => {
21
+ //const payload = ctx.request.body;
22
+ ctx.body = "ok";
23
+ ctx.status = 200;
24
+ await next();
25
+ });
26
+ }
27
+ export { getTasksRoute, getTaskRoute, };
@@ -0,0 +1,3 @@
1
+ import type Router from '@koa/router';
2
+ declare function getToolsRoute(r: Router): void;
3
+ export { getToolsRoute, };
@@ -0,0 +1,30 @@
1
+ import { db } from '@agent-smith/cli';
2
+ function getToolsRoute(r) {
3
+ r.post('/tools', async (ctx, next) => {
4
+ const payload = ctx.request.body;
5
+ const tools = new Array();
6
+ let nf = false;
7
+ for (const tn of payload) {
8
+ const { found, tool, type } = db.readTool(tn);
9
+ if (!found) {
10
+ ctx.status = 400;
11
+ ctx.body = `Tool ${tn} not found`;
12
+ nf = true;
13
+ break;
14
+ }
15
+ ;
16
+ const ts = {
17
+ name: tool.name,
18
+ description: tool.description,
19
+ arguments: tool.arguments,
20
+ };
21
+ tools.push({ def: ts, type: type });
22
+ }
23
+ if (!nf) {
24
+ ctx.body = tools;
25
+ ctx.status = 200;
26
+ }
27
+ await next();
28
+ });
29
+ }
30
+ export { getToolsRoute, };
@@ -2,6 +2,7 @@ import Router from '@koa/router';
2
2
  const useRouter = (routes = []) => {
3
3
  const router = new Router();
4
4
  routes.forEach((f) => f(router));
5
+ //console.log("R", router.routes())
5
6
  router.get('/ping', async (ctx, next) => {
6
7
  ctx.body = { ok: true };
7
8
  ctx.status = 200;
@@ -34,7 +34,7 @@ app.use(cors({
34
34
  app.ws.use(function (ctx, next) {
35
35
  return next();
36
36
  });
37
- function createManualPromise() {
37
+ function createAwaiter() {
38
38
  let resolveFn;
39
39
  let rejectFn;
40
40
  const promise = new Promise((resolve, reject) => {
@@ -109,19 +109,43 @@ function runserver(routes, staticDir) {
109
109
  buf += t;
110
110
  process.stdout.write(t);
111
111
  };
112
+ msg.options.onTurnEnd = (ht) => {
113
+ const rsm = {
114
+ type: "turnend",
115
+ msg: JSON.stringify(ht),
116
+ };
117
+ ctx.websocket.send(JSON.stringify(rsm));
118
+ };
119
+ msg.options.onAssistant = (txt) => {
120
+ const rsm = {
121
+ type: "assistant",
122
+ msg: txt,
123
+ };
124
+ ctx.websocket.send(JSON.stringify(rsm));
125
+ };
126
+ msg.options.onThink = (txt) => {
127
+ const rsm = {
128
+ type: "think",
129
+ msg: txt,
130
+ };
131
+ ctx.websocket.send(JSON.stringify(rsm));
132
+ };
112
133
  try {
113
134
  const it = setInterval(() => {
114
135
  const rsm = {
115
136
  type: "token",
116
137
  msg: buf,
117
138
  };
118
- ctx.websocket.send(JSON.stringify(rsm));
139
+ if (buf !== "") {
140
+ // model is loading if no buffer
141
+ ctx.websocket.send(JSON.stringify(rsm));
142
+ }
119
143
  buf = "";
120
144
  }, sendTokensInterval);
121
145
  const res = await executeTask(msg.command, msg.payload, msg.options);
122
- setTimeout(() => {
123
- clearInterval(it);
124
- }, sendTokensInterval);
146
+ //setTimeout(() => {
147
+ clearInterval(it);
148
+ //}, sendTokensInterval);
125
149
  //console.dir(res, { depth: 3 });
126
150
  let r;
127
151
  if (res.template.id == "none") {
@@ -171,6 +195,15 @@ function runserver(routes, staticDir) {
171
195
  type: "assistant",
172
196
  msg: txt,
173
197
  };
198
+ //console.log("SRV ON ASSISTANT", txt);
199
+ ctx.websocket.send(JSON.stringify(rsm));
200
+ };
201
+ msg.options.onThink = (txt) => {
202
+ const rsm = {
203
+ type: "think",
204
+ msg: txt,
205
+ };
206
+ //console.log("SRV ON THINK", txt);
174
207
  ctx.websocket.send(JSON.stringify(rsm));
175
208
  };
176
209
  msg.options.onToolCall = (tc) => {
@@ -212,7 +245,7 @@ function runserver(routes, staticDir) {
212
245
  type: "toolcallconfirm",
213
246
  msg: JSON.stringify(tc),
214
247
  };
215
- const { promise, resolve } = createManualPromise();
248
+ const { promise, resolve } = createAwaiter();
216
249
  confirmToolCalls[tc.id] = resolve;
217
250
  ctx.websocket.send(JSON.stringify(rsm));
218
251
  const res = await promise;
@@ -238,9 +271,9 @@ function runserver(routes, staticDir) {
238
271
  buf = "";
239
272
  }, sendTokensInterval);
240
273
  const res = await executeTask(msg.command, msg.payload, msg.options);
241
- setTimeout(() => {
242
- clearInterval(it);
243
- }, sendTokensInterval);
274
+ //setTimeout(() => {
275
+ clearInterval(it);
276
+ //}, sendTokensInterval);
244
277
  const ht = JSON.stringify(res.template.history.pop());
245
278
  //console.log("FINAL MSG", ht)
246
279
  const rsm = {
@@ -294,8 +327,18 @@ function runserver(routes, staticDir) {
294
327
  });
295
328
  }));
296
329
  app.use(router.routes()).use(router.allowedMethods());
330
+ // 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
+ });*/
297
340
  app.listen(5184, () => {
298
- console.log('Please open localhost:5184 in a browser');
341
+ console.log('Please open url localhost:5184 in a browser');
299
342
  });
300
343
  }
301
344
  export { runserver };
package/package.json CHANGED
@@ -1,29 +1,28 @@
1
1
  {
2
2
  "name": "@agent-smith/server",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Agent Smith Nodejs server",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1",
7
7
  "build": "rm -rf dist && tsc",
8
- "start": "node dist/index.js",
9
- "server": "node --loader ts-node/esm src/index.ts",
8
+ "server": "node dist/index.js",
10
9
  "devserver": "node --loader ts-node/esm --watch src/index.ts"
11
10
  },
12
11
  "type": "module",
13
12
  "dependencies": {
14
- "@agent-smith/cli": "^0.0.111",
13
+ "@agent-smith/cli": "^0.0.112",
15
14
  "@koa/cors": "^5.0.0",
16
15
  "@koa/router": "^15.3.0",
17
16
  "ansi-colors": "^4.1.3",
18
- "koa": "^3.1.1",
17
+ "koa": "^3.1.2",
19
18
  "koa-bodyparser": "^4.4.1",
20
19
  "koa-route": "^4.0.1",
21
20
  "koa-static": "^5.0.0",
22
21
  "koa-websocket": "^7.0.0"
23
22
  },
24
23
  "devDependencies": {
25
- "@agent-smith/types": "^0.0.2",
26
- "@locallm/types": "^0.7.0",
24
+ "@agent-smith/types": "^0.0.3",
25
+ "@locallm/types": "^0.7.1",
27
26
  "@types/better-sqlite3": "^7.6.13",
28
27
  "@types/koa": "^3.0.1",
29
28
  "@types/koa__cors": "^5.0.1",
@@ -31,7 +30,7 @@
31
30
  "@types/koa-route": "^3.2.9",
32
31
  "@types/koa-static": "^4.0.4",
33
32
  "@types/koa-websocket": "^5.0.11",
34
- "@types/node": "^25.3.0",
33
+ "@types/node": "^25.3.1",
35
34
  "ts-node": "^10.9.2",
36
35
  "tslib": "2.8.1",
37
36
  "typescript": "^5.9.3"