@agent-smith/server 0.0.8 → 0.0.9

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.
@@ -1,6 +1,5 @@
1
- import { getModelsCmd } from "./models.js";
2
- import { getTaskSettingsCmd, updateTaskSettingsCmd } from "./task_settings.js";
3
- import { getTaskRoute, getTasksRoute } from "./tasks.js";
1
+ import { getModelsRoute, getModelsPresetsRoute, upsertModelPresetRoute, delModelPresetRoute } from "./models.js";
2
+ import { getAgentSettingsCmd, updateAgentSettingsCmd } from "./agent_settings.js";
4
3
  import { getAgentRoute, getAgentsRoute } from "./agents.js";
5
4
  import { createConfRoute, getConfRoute } from "./conf.js";
6
5
  import { getToolsRoute } from "./tools.js";
@@ -10,5 +9,8 @@ import { addFolderRoute } from "./folders.js";
10
9
  import { getWorkflowRoute, getWorkflowsRoute } from "./workflows.js";
11
10
  import { getBackendsRoute, setBackendRoute } from "./backends.js";
12
11
  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);
12
+ import { getWorkspaceRoute, updateDefaultWorkspaceRoute, upsertWorkspaceRoute } from "./workspace.js";
13
+ import { getSettingsRoute } from "./settings.js";
14
+ import { applyTemplateRoute } from "./templates.js";
15
+ const baseRoutes = new Array(getConfRoute, getAgentRoute, getAgentsRoute, getModelsRoute, getModelsPresetsRoute, getToolsRoute, getAgentSettingsCmd, getStateRoute, createConfRoute, updateAgentSettingsCmd, installPluginRoute, addFolderRoute, getWorkflowRoute, getWorkflowsRoute, getBackendsRoute, setBackendRoute, getOrCreateAppConfigFileRoute, updateAppConfigFileRoute, getWorkspaceRoute, upsertWorkspaceRoute, updateDefaultWorkspaceRoute, getSettingsRoute, upsertModelPresetRoute, delModelPresetRoute, applyTemplateRoute);
14
16
  export { baseRoutes };
@@ -1,3 +1,6 @@
1
1
  import type Router from '@koa/router';
2
- declare function getModelsCmd(r: Router): void;
3
- export { getModelsCmd, };
2
+ declare function getModelsRoute(r: Router): void;
3
+ declare function getModelsPresetsRoute(r: Router): void;
4
+ declare function upsertModelPresetRoute(r: Router): void;
5
+ declare function delModelPresetRoute(r: Router): void;
6
+ export { getModelsRoute, getModelsPresetsRoute, upsertModelPresetRoute, delModelPresetRoute, };
@@ -1,36 +1,73 @@
1
- import { backend } from '@agent-smith/cli';
2
- function getModelsCmd(r) {
3
- r.get('/models', async (ctx, next) => {
4
- let mi;
1
+ import { state } from '@agent-smith/core';
2
+ import { db } from '@agent-smith/core';
3
+ function getModelsRoute(r) {
4
+ r.get('/models/:backend', async (ctx, next) => {
5
+ const backend = ctx.params?.backend;
6
+ if (!Object.keys(state.backends).includes(backend)) {
7
+ ctx.body = `backend ${backend} not found in config`;
8
+ ctx.status = 400;
9
+ }
10
+ const b = state.backends[backend];
11
+ //console.log("MB", backend, "/", b);
12
+ //console.log("SB", state.backends);
13
+ let mi = new Array();
14
+ try {
15
+ mi = await b.modelsInfo() ?? [];
16
+ //console.log("M", mi);
17
+ }
18
+ catch (e) {
19
+ ctx.body = `error reading the models from backend ${backend}\n${e}`;
20
+ ctx.status = 502;
21
+ return;
22
+ }
23
+ ctx.body = mi;
24
+ ctx.status = 200;
25
+ });
26
+ }
27
+ function getModelsPresetsRoute(r) {
28
+ r.get('/models/presets/read', async (ctx, next) => {
29
+ let mp = new Array();
5
30
  try {
6
- mi = await backend.value?.modelsInfo();
31
+ //console.log("READ SP");
32
+ mp = db.readSamplingPresets();
33
+ //console.log("M", mp);
7
34
  }
8
35
  catch (e) {
9
- ctx.body = "error reading the models";
36
+ ctx.body = "error reading the models presets";
10
37
  ctx.status = 502;
11
38
  return;
12
39
  }
13
- const ms = {};
14
- mi?.forEach(m => {
15
- let prevArg = "";
16
- let ctx = 0;
17
- if (backend.value?.providerType == "openai") {
18
- ms[m.name] = { status: "unknown", ctx: m?.ctx ?? ctx };
19
- }
20
- else {
21
- //@ts-ignore
22
- m.extra.status.args.forEach(a => {
23
- if (prevArg == "--ctx-size") {
24
- ctx = parseInt(a);
25
- }
26
- //@ts-ignore
27
- ms[m.name] = { status: m.extra.status.value, ctx: ctx };
28
- prevArg = a;
29
- });
30
- }
31
- });
32
- ctx.body = ms;
40
+ ctx.body = mp;
33
41
  ctx.status = 200;
34
42
  });
35
43
  }
36
- export { getModelsCmd, };
44
+ function upsertModelPresetRoute(r) {
45
+ r.post('/models/preset/update', async (ctx, next) => {
46
+ const payload = ctx.request.body;
47
+ try {
48
+ db.upsertSamplingPreset(payload);
49
+ ctx.status = 204;
50
+ }
51
+ catch (e) {
52
+ const err = `error updating model preset:\n ${e}`;
53
+ console.error(err);
54
+ ctx.body = err;
55
+ ctx.status = 500;
56
+ }
57
+ });
58
+ }
59
+ function delModelPresetRoute(r) {
60
+ r.del('/models/preset/delete/:name', async (ctx, next) => {
61
+ const name = ctx.params?.name;
62
+ if (!name) {
63
+ ctx.body = "provide a name to delete model preset";
64
+ ctx.status = 400;
65
+ }
66
+ else {
67
+ const w = db.deleteSamplingPreset(name);
68
+ ctx.body = w;
69
+ ctx.status = 200;
70
+ }
71
+ });
72
+ }
73
+ export { getModelsRoute, getModelsPresetsRoute, upsertModelPresetRoute, delModelPresetRoute, };
@@ -1,15 +1,15 @@
1
- import { execute, updateConfCmd, updateConfigFile, init } from '@agent-smith/cli';
1
+ import { utils, conf as c, state } from '@agent-smith/core';
2
2
  import { getConfig } from '../utils.js';
3
3
  function installPluginRoute(r) {
4
4
  r.post('/plugins/install', async (ctx, next) => {
5
5
  const payload = ctx.request.body;
6
- console.log("P", payload);
6
+ //console.log("P", payload);
7
7
  for (const p of payload) {
8
8
  console.log("Installing", p, "plugin");
9
- const res = await execute("npm", ["i", "-g", p]);
9
+ const res = await utils.execute("npm", ["i", "-g", p]);
10
10
  console.log(p, res);
11
11
  }
12
- await init();
12
+ await state.init();
13
13
  const { found, conf, path } = getConfig();
14
14
  if (!found) {
15
15
  throw new Error("no config file found");
@@ -24,9 +24,9 @@ function installPluginRoute(r) {
24
24
  ;
25
25
  console.log("Updating config file at", path);
26
26
  console.dir(conf, { depth: 3 });
27
- updateConfigFile(conf, path);
27
+ c.updateConfigFile(conf, path);
28
28
  console.log("Updating db features from config file");
29
- await updateConfCmd([path]);
29
+ await c.updateConfCmd([path]);
30
30
  ctx.status = 202;
31
31
  });
32
32
  }
@@ -0,0 +1,3 @@
1
+ import type Router from '@koa/router';
2
+ declare function getSettingsRoute(r: Router): void;
3
+ export { getSettingsRoute, };
@@ -0,0 +1,20 @@
1
+ import { db } from '@agent-smith/core';
2
+ function getSettingsRoute(r) {
3
+ r.get('/settings', async (ctx, next) => {
4
+ let res = {};
5
+ try {
6
+ res = db.readSettings();
7
+ //console.log("M", mi);
8
+ }
9
+ catch (e) {
10
+ const err = `error reading settings ${e}`;
11
+ console.error(err);
12
+ ctx.body = err;
13
+ ctx.status = 500;
14
+ return;
15
+ }
16
+ ctx.body = res;
17
+ ctx.status = 200;
18
+ });
19
+ }
20
+ export { getSettingsRoute, };
@@ -1,18 +1,18 @@
1
- import { getConfigPath, init } from '@agent-smith/cli';
1
+ import { conf, state } from '@agent-smith/core';
2
2
  import fs from "node:fs";
3
3
  import { getConfig } from '../utils.js';
4
4
  function getStateRoute(r) {
5
5
  r.get('/state', async (ctx, next) => {
6
6
  //console.log('STATE URL --> ' + ctx.request.url);
7
7
  //console.log("STATE ROUTE");
8
- const { confDir, dbPath } = getConfigPath("agent-smith", "config.db");
8
+ const { dbPath } = conf.getConfigPath("agent-smith", "config.db");
9
9
  //console.log("conf paths", confDir, dbPath);
10
10
  if (!fs.existsSync(dbPath)) {
11
11
  ctx.body = "no db found at " + dbPath;
12
12
  ctx.status = 202;
13
13
  }
14
14
  else {
15
- await init();
15
+ await state.init();
16
16
  const { found, conf } = getConfig();
17
17
  if (!found) {
18
18
  ctx.body = "can not find config path in db";
@@ -0,0 +1,3 @@
1
+ import type Router from '@koa/router';
2
+ declare function applyTemplateRoute(r: Router): void;
3
+ export { applyTemplateRoute, };
@@ -0,0 +1,18 @@
1
+ import { useAgentExecutor } from '@agent-smith/core';
2
+ function applyTemplateRoute(r) {
3
+ r.post('/templates/apply', async (ctx, next) => {
4
+ const payload = ctx.request.body;
5
+ const ex = await useAgentExecutor("infer", { prompt: "" }, {});
6
+ try {
7
+ const res = await ex.agent.lm.applyTemplate(payload.messages, payload.modelName);
8
+ ctx.status = 200;
9
+ ctx.body = res.prompt;
10
+ }
11
+ catch (e) {
12
+ console.error(e);
13
+ ctx.status = 500;
14
+ ctx.body = e.toString();
15
+ }
16
+ });
17
+ }
18
+ export { applyTemplateRoute, };
@@ -1,11 +1,11 @@
1
- import { db } from '@agent-smith/cli';
1
+ import { db } from '@agent-smith/core';
2
2
  function getToolsRoute(r) {
3
3
  r.post('/tools', async (ctx, next) => {
4
4
  const payload = ctx.request.body;
5
5
  const tools = new Array();
6
6
  let nf = false;
7
7
  for (const tn of payload) {
8
- const { found, tool, type } = db.readTool(tn);
8
+ const { found, tool } = db.readTool(tn);
9
9
  if (!found) {
10
10
  ctx.status = 400;
11
11
  ctx.body = `Tool ${tn} not found`;
@@ -18,7 +18,7 @@ function getToolsRoute(r) {
18
18
  description: tool.description,
19
19
  arguments: tool.arguments,
20
20
  };
21
- tools.push({ def: ts, type: type });
21
+ tools.push({ def: ts, type: tool.type });
22
22
  }
23
23
  if (!nf) {
24
24
  ctx.body = tools;
@@ -1,4 +1,4 @@
1
- import { db, fs } from '@agent-smith/cli';
1
+ import { db, fs } from '@agent-smith/core';
2
2
  function getWorkflowsRoute(r) {
3
3
  r.get('/workflows', async (ctx, next) => {
4
4
  const w = db.readFeaturesType("workflow");
@@ -0,0 +1,5 @@
1
+ import type Router from '@koa/router';
2
+ declare function getWorkspaceRoute(r: Router): void;
3
+ declare function upsertWorkspaceRoute(r: Router): void;
4
+ declare function updateDefaultWorkspaceRoute(r: Router): void;
5
+ export { getWorkspaceRoute, upsertWorkspaceRoute, updateDefaultWorkspaceRoute, };
@@ -0,0 +1,50 @@
1
+ import { db } from '@agent-smith/core';
2
+ function getWorkspaceRoute(r) {
3
+ r.get('/workspace', async (ctx, next) => {
4
+ let ws;
5
+ try {
6
+ ws = db.readWorkspaces();
7
+ //console.log("M", mi);
8
+ }
9
+ catch (e) {
10
+ const err = `error reading the workspaces ${e}`;
11
+ console.error(err);
12
+ ctx.body = err;
13
+ ctx.status = 500;
14
+ return;
15
+ }
16
+ ctx.body = ws;
17
+ ctx.status = 200;
18
+ });
19
+ }
20
+ function upsertWorkspaceRoute(r) {
21
+ r.post('/workspace', async (ctx, next) => {
22
+ const payload = ctx.request.body;
23
+ try {
24
+ db.upsertWorkspace(payload);
25
+ ctx.status = 204;
26
+ }
27
+ catch (e) {
28
+ const err = `error writing workspace file path into db:\n ${e}`;
29
+ console.error(err);
30
+ ctx.body = err;
31
+ ctx.status = 500;
32
+ }
33
+ });
34
+ }
35
+ function updateDefaultWorkspaceRoute(r) {
36
+ r.post('/workspace/update', async (ctx, next) => {
37
+ const payload = ctx.request.body;
38
+ try {
39
+ db.upsertSetting("workspace", payload.name);
40
+ ctx.status = 204;
41
+ }
42
+ catch (e) {
43
+ const err = `error updating default workspace:\n ${e}`;
44
+ console.error(err);
45
+ ctx.body = err;
46
+ ctx.status = 500;
47
+ }
48
+ });
49
+ }
50
+ export { getWorkspaceRoute, upsertWorkspaceRoute, updateDefaultWorkspaceRoute, };
@@ -1,3 +1,6 @@
1
1
  import Router from '@koa/router';
2
- declare const useRouter: (routes?: Array<(r: Router) => void>) => Router<import("koa").DefaultState, import("koa").DefaultContext>;
2
+ declare const useRouter: (apiRoutes?: Array<(r: Router) => void>) => {
3
+ baseRouter: Router<import("koa").DefaultState, import("koa").DefaultContext>;
4
+ apiRouter: import("@koa/router").RouterWithMethods<string, import("koa").DefaultState, import("koa").DefaultContext>;
5
+ };
3
6
  export { useRouter };
@@ -1,13 +1,15 @@
1
1
  import Router from '@koa/router';
2
- const useRouter = (routes = []) => {
2
+ const useRouter = (apiRoutes = []) => {
3
+ const apiRouter = new Router({ prefix: '/api' });
3
4
  const router = new Router();
4
- routes.forEach((f) => f(router));
5
- //console.log("R", router.routes())
5
+ apiRoutes.forEach((f) => f(apiRouter));
6
6
  router.get('/ping', async (ctx, next) => {
7
7
  ctx.body = { ok: true };
8
8
  ctx.status = 200;
9
9
  await next();
10
10
  });
11
- return router;
11
+ //console.log("Routes", router.stack.map(i => i.path));
12
+ //console.log("Apiroutes", apiRouter.stack.map(i => i.path));
13
+ return { baseRouter: router, apiRouter };
12
14
  };
13
15
  export { useRouter };