@agent-smith/core 0.0.1

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.
Files changed (119) hide show
  1. package/README.md +378 -0
  2. package/dist/actions/cmd.d.ts +5 -0
  3. package/dist/actions/cmd.js +135 -0
  4. package/dist/actions/read.d.ts +3 -0
  5. package/dist/actions/read.js +17 -0
  6. package/dist/adaptaters/cmd.d.ts +2 -0
  7. package/dist/adaptaters/cmd.js +39 -0
  8. package/dist/agents/cmd.d.ts +3 -0
  9. package/dist/agents/cmd.js +10 -0
  10. package/dist/agents/conf.d.ts +3 -0
  11. package/dist/agents/conf.js +9 -0
  12. package/dist/agents/files.d.ts +3 -0
  13. package/dist/agents/files.js +40 -0
  14. package/dist/agents/read.d.ts +11 -0
  15. package/dist/agents/read.js +241 -0
  16. package/dist/agents/useagent.d.ts +13 -0
  17. package/dist/agents/useagent.js +395 -0
  18. package/dist/conf.d.ts +14 -0
  19. package/dist/conf.js +160 -0
  20. package/dist/const.d.ts +3 -0
  21. package/dist/const.js +10 -0
  22. package/dist/db/db.d.ts +3 -0
  23. package/dist/db/db.js +24 -0
  24. package/dist/db/read.d.ts +45 -0
  25. package/dist/db/read.js +280 -0
  26. package/dist/db/schemas.d.ts +2 -0
  27. package/dist/db/schemas.js +141 -0
  28. package/dist/db/write.d.ts +23 -0
  29. package/dist/db/write.js +570 -0
  30. package/dist/features/actions/load-skill.d.ts +2 -0
  31. package/dist/features/actions/load-skill.js +37 -0
  32. package/dist/features/actions/notify-user.d.ts +2 -0
  33. package/dist/features/actions/notify-user.js +25 -0
  34. package/dist/features/actions/read-feature.d.ts +2 -0
  35. package/dist/features/actions/read-feature.js +25 -0
  36. package/dist/features/actions/run-agent.d.ts +2 -0
  37. package/dist/features/actions/run-agent.js +34 -0
  38. package/dist/features/actions/run-collaborator.d.ts +1 -0
  39. package/dist/features/actions/run-collaborator.js +42 -0
  40. package/dist/features/actions/run-worker.d.ts +2 -0
  41. package/dist/features/actions/run-worker.js +34 -0
  42. package/dist/features/adaptaters/agent-smith-db-getschema.d.ts +4 -0
  43. package/dist/features/adaptaters/agent-smith-db-getschema.js +62 -0
  44. package/dist/features/adaptaters/imgs2base64.d.ts +1 -0
  45. package/dist/features/adaptaters/imgs2base64.js +13 -0
  46. package/dist/features/adaptaters/prequery.d.ts +4 -0
  47. package/dist/features/adaptaters/prequery.js +5 -0
  48. package/dist/features/agents/agent-smith-colab.yml +37 -0
  49. package/dist/features/agents/agent-smith-doc.yml +50 -0
  50. package/dist/features/agents/agent-smith-help.yml +35 -0
  51. package/dist/features/agents/agent-smith-search.yml +41 -0
  52. package/dist/features/agents/agent-smith-sql.yml +31 -0
  53. package/dist/features/agents/agent-smith.yml +37 -0
  54. package/dist/features/agents/collaborator.yml +15 -0
  55. package/dist/features/agents/infer.yml +12 -0
  56. package/dist/features/fragments/ctx-helper-files.md +4 -0
  57. package/dist/features/fragments/workspace.txt +6 -0
  58. package/dist/features/skills/create-package-readme/SKILL.md +66 -0
  59. package/dist/features/skills/document-package/SKILL.md +83 -0
  60. package/dist/features/skills/t/SKILL.md +26 -0
  61. package/dist/features/skills/update-codebase-summary/SKILL.md +45 -0
  62. package/dist/features/skills/update-doc-map/SKILL.md +8 -0
  63. package/dist/features/skills/update-doc-map/scripts/generate-doc-map.mjs +196 -0
  64. package/dist/features/workflows/agent-smith-db.yml +9 -0
  65. package/dist/features/workflows/q.yml +3 -0
  66. package/dist/features/workflows/vision.yml +3 -0
  67. package/dist/main.d.ts +127 -0
  68. package/dist/main.js +80 -0
  69. package/dist/mcp.d.ts +16 -0
  70. package/dist/mcp.js +110 -0
  71. package/dist/state/backends.d.ts +7 -0
  72. package/dist/state/backends.js +96 -0
  73. package/dist/state/features.d.ts +10 -0
  74. package/dist/state/features.js +42 -0
  75. package/dist/state/plugins.d.ts +6 -0
  76. package/dist/state/plugins.js +27 -0
  77. package/dist/state/state.d.ts +18 -0
  78. package/dist/state/state.js +79 -0
  79. package/dist/state/tasks.d.ts +6 -0
  80. package/dist/state/tasks.js +33 -0
  81. package/dist/tools.d.ts +15 -0
  82. package/dist/tools.js +149 -0
  83. package/dist/updateconf.d.ts +5 -0
  84. package/dist/updateconf.js +124 -0
  85. package/dist/utils/io.d.ts +10 -0
  86. package/dist/utils/io.js +98 -0
  87. package/dist/utils/perf.d.ts +9 -0
  88. package/dist/utils/perf.js +63 -0
  89. package/dist/utils/sys/clipboard.d.ts +3 -0
  90. package/dist/utils/sys/clipboard.js +33 -0
  91. package/dist/utils/sys/delete_file.d.ts +2 -0
  92. package/dist/utils/sys/delete_file.js +10 -0
  93. package/dist/utils/sys/dirs.d.ts +2 -0
  94. package/dist/utils/sys/dirs.js +9 -0
  95. package/dist/utils/sys/execute.d.ts +13 -0
  96. package/dist/utils/sys/execute.js +48 -0
  97. package/dist/utils/sys/read.d.ts +3 -0
  98. package/dist/utils/sys/read.js +21 -0
  99. package/dist/utils/sys/read_agent.d.ts +6 -0
  100. package/dist/utils/sys/read_agent.js +23 -0
  101. package/dist/utils/sys/read_cmds.d.ts +7 -0
  102. package/dist/utils/sys/read_cmds.js +37 -0
  103. package/dist/utils/sys/read_conf.d.ts +6 -0
  104. package/dist/utils/sys/read_conf.js +12 -0
  105. package/dist/utils/sys/read_features.d.ts +3 -0
  106. package/dist/utils/sys/read_features.js +134 -0
  107. package/dist/utils/sys/read_yml_file.d.ts +5 -0
  108. package/dist/utils/sys/read_yml_file.js +11 -0
  109. package/dist/utils/sys/run_python.d.ts +6 -0
  110. package/dist/utils/sys/run_python.js +39 -0
  111. package/dist/utils/text.d.ts +2 -0
  112. package/dist/utils/text.js +28 -0
  113. package/dist/utils/user_msgs.d.ts +5 -0
  114. package/dist/utils/user_msgs.js +19 -0
  115. package/dist/workflows/cmd.d.ts +3 -0
  116. package/dist/workflows/cmd.js +189 -0
  117. package/dist/workflows/read.d.ts +6 -0
  118. package/dist/workflows/read.js +61 -0
  119. package/package.json +45 -0
@@ -0,0 +1,23 @@
1
+ import { Features, InferenceBackend, AgentSettings, type Workspace, type SamplingPreset } from "@agent-smith/types";
2
+ declare function updatePromptfilePath(pf: string): void;
3
+ declare function updateDataDirPath(dd: string): void;
4
+ declare function updateWorkspacePath(dd: string): void;
5
+ declare function setDefaultBackend(name: string): void;
6
+ declare function upsertBackends(bdata: Array<InferenceBackend>): boolean;
7
+ declare function insertFeaturesPathIfNotExists(path: string): boolean;
8
+ declare function insertPluginIfNotExists(n: string, p: string): boolean;
9
+ declare function cleanupFeaturePaths(paths: Array<string>): Array<string>;
10
+ declare function updateAliases(feats: Features): void;
11
+ declare function updateFeatures(feats: Features, isVerbose?: boolean): void;
12
+ declare function upsertSetting(name: string, value: any): boolean;
13
+ declare function upsertWorkspace(workspace: Workspace, isVerbose?: boolean): boolean;
14
+ declare function upsertAndCleanWorkspaces(workspaces: Array<Workspace>): Array<Workspace>;
15
+ declare function upsertFilePath(name: string, newPath: string): boolean;
16
+ declare function upsertAgentSettings(taskName: string, settings: AgentSettings): boolean;
17
+ declare function upsertSamplingPreset(preset: SamplingPreset): boolean;
18
+ declare function upsertSamplingPresets(modelPresets: Array<SamplingPreset>): boolean;
19
+ declare function deleteWorkspace(name: string): void;
20
+ declare function deleteAgentSettings(settings: Array<string>): void;
21
+ declare function deleteAgentSetting(name: string): void;
22
+ declare function deleteSamplingPreset(name: string): void;
23
+ export { updatePromptfilePath, updateWorkspacePath, updateDataDirPath, upsertBackends, upsertSetting, upsertWorkspace, upsertAndCleanWorkspaces, setDefaultBackend, insertFeaturesPathIfNotExists, insertPluginIfNotExists, updateFeatures, updateAliases, cleanupFeaturePaths, upsertFilePath, upsertAgentSettings, deleteAgentSettings, deleteAgentSetting, deleteWorkspace, upsertSamplingPresets, upsertSamplingPreset, deleteSamplingPreset, };
@@ -0,0 +1,570 @@
1
+ import { extractAgentToolDocAndVariables, extractToolDoc } from "../tools.js";
2
+ import { db } from "./db.js";
3
+ function updatePromptfilePath(pf) {
4
+ const deleteStmt = db.prepare("DELETE FROM filepath WHERE name = ?");
5
+ deleteStmt.run("promptfile");
6
+ const stmt = db.prepare("INSERT INTO filepath (name, path) VALUES (?, ?)");
7
+ stmt.run("promptfile", pf);
8
+ }
9
+ function updateDataDirPath(dd) {
10
+ const deleteStmt = db.prepare("DELETE FROM filepath WHERE name = ?");
11
+ deleteStmt.run("datadir");
12
+ const stmt = db.prepare("INSERT INTO filepath (name, path) VALUES (?, ?)");
13
+ stmt.run("datadir", dd);
14
+ }
15
+ function updateWorkspacePath(dd) {
16
+ const deleteStmt = db.prepare("DELETE FROM filepath WHERE name = ?");
17
+ deleteStmt.run("workspace");
18
+ const stmt = db.prepare("INSERT INTO filepath (name, path) VALUES (?, ?)");
19
+ stmt.run("workspace", dd);
20
+ }
21
+ function setDefaultBackend(name) {
22
+ const updateStmt = db.prepare("UPDATE backend SET isdefault = 0 WHERE isdefault = 1");
23
+ updateStmt.run();
24
+ const nupdStmt = db.prepare("UPDATE backend SET isdefault = 1 WHERE name = ?");
25
+ nupdStmt.run(name);
26
+ }
27
+ function upsertBackends(bdata) {
28
+ let hasUpdates = false;
29
+ // Get all existing backend names
30
+ const existingStmt = db.prepare("SELECT name FROM backend");
31
+ const existingBackends = existingStmt.all();
32
+ const existingNames = new Set(existingBackends.map(b => b.name));
33
+ // Create a set of new backend names for comparison
34
+ const newNames = new Set(bdata.map(b => b.name));
35
+ // Delete backends that are not in the new list
36
+ const toDelete = Array.from(existingNames).filter(name => !newNames.has(name));
37
+ if (toDelete.length > 0) {
38
+ const deleteStmt = db.prepare("DELETE FROM backend WHERE name = ?");
39
+ for (const name of toDelete) {
40
+ deleteStmt.run(name);
41
+ }
42
+ hasUpdates = true;
43
+ }
44
+ // Upsert the new backends
45
+ for (const backend of bdata) {
46
+ const stmt1 = db.prepare("SELECT * FROM backend WHERE name = ?");
47
+ const result = stmt1.get(backend.name);
48
+ if (result?.id) {
49
+ const updateStmt = db.prepare("UPDATE backend SET type = ?, url = ?, apiKey = ?, isdefault = ? WHERE name = ?");
50
+ updateStmt.run(backend.type, backend.url, backend?.apiKey ?? "NULL", backend.isDefault ? 1 : 0, backend.name);
51
+ hasUpdates = true;
52
+ }
53
+ else {
54
+ const stmt = db.prepare("INSERT INTO backend (name,type,url,apiKey,isdefault) VALUES (?,?,?,?,?)");
55
+ stmt.run(backend.name, backend.type, backend.url, backend?.apiKey ?? "NULL", backend.isDefault ? 1 : 0);
56
+ hasUpdates = true;
57
+ }
58
+ }
59
+ return hasUpdates;
60
+ }
61
+ function insertFeaturesPathIfNotExists(path) {
62
+ const stmt1 = db.prepare("SELECT * FROM featurespath WHERE path = ?");
63
+ const result = stmt1.get(path);
64
+ if (result?.id) {
65
+ return true;
66
+ }
67
+ const stmt = db.prepare("INSERT INTO featurespath (path) VALUES (?)");
68
+ stmt.run(path);
69
+ return false;
70
+ }
71
+ function insertPluginIfNotExists(n, p) {
72
+ const stmt1 = db.prepare("SELECT * FROM plugin WHERE name = ?");
73
+ const result = stmt1.get(n);
74
+ if (result?.id) {
75
+ return true;
76
+ }
77
+ const stmt = db.prepare("INSERT INTO plugin (name, path) VALUES (?,?)");
78
+ stmt.run(n, p);
79
+ return false;
80
+ }
81
+ function cleanupFeaturePaths(paths) {
82
+ const stmt = db.prepare("SELECT path FROM featurespath");
83
+ const rows = stmt.all();
84
+ const deleted = new Array();
85
+ for (const entry of rows) {
86
+ if (!paths.includes(entry.path)) {
87
+ const deleteStmt = db.prepare("DELETE FROM featurespath WHERE path = ?");
88
+ deleteStmt.run(entry.path);
89
+ deleted.push(entry.path);
90
+ }
91
+ }
92
+ return deleted;
93
+ }
94
+ function _updateAlias(existingAliases, name, type) {
95
+ if (!existingAliases.includes(name)) {
96
+ const insertStmt = db.prepare("INSERT INTO aliases (name, type) VALUES (?, ?)");
97
+ insertStmt.run(name, type);
98
+ }
99
+ else {
100
+ console.log("Can not create command alias", name, ": duplicate name");
101
+ }
102
+ existingAliases.push(name);
103
+ return existingAliases;
104
+ }
105
+ function updateAliases(feats) {
106
+ const deleteStmt = db.prepare("DELETE FROM aliases");
107
+ deleteStmt.run();
108
+ let existingAliases = new Array();
109
+ feats.agent.forEach((feat) => {
110
+ existingAliases = _updateAlias(existingAliases, feat.name, "agent");
111
+ });
112
+ feats.action.forEach((feat) => {
113
+ existingAliases = _updateAlias(existingAliases, feat.name, "action");
114
+ });
115
+ feats.workflow.forEach((feat) => {
116
+ existingAliases = _updateAlias(existingAliases, feat.name, "workflow");
117
+ });
118
+ }
119
+ function upsertAndCleanFeatures(feats, type) {
120
+ const stmt = db.prepare(`SELECT name FROM ${type}`);
121
+ const rows = stmt.all();
122
+ const names = rows.map(row => row.name);
123
+ // cleanup removed features
124
+ const availableFeatsNames = feats.map((f) => f.name);
125
+ //console.log("NAMES", names);
126
+ //console.log("AVAILABLE", availableFeatsNames);
127
+ const newFeatures = new Array();
128
+ names.forEach((name) => {
129
+ //console.log(name, !availableFeatsNames.includes(name));
130
+ if (!availableFeatsNames.includes(name)) {
131
+ //console.log("DELETE", name);
132
+ const deleteStmt = db.prepare(`DELETE FROM ${type} WHERE name = ?`);
133
+ deleteStmt.run(name);
134
+ console.log("-", "[" + type + "]", name);
135
+ // check if the feature has a tool and delete if if so
136
+ const stmt1 = db.prepare("SELECT * FROM tool WHERE name = ?");
137
+ const result = stmt1.get(name);
138
+ if (result?.id) {
139
+ const deleteStmt = db.prepare("DELETE FROM featurespath WHERE id = ?");
140
+ deleteStmt.run(result.id);
141
+ console.log("-", "[tool] from", type, ":", name);
142
+ }
143
+ }
144
+ });
145
+ feats.forEach((feat) => {
146
+ if (!names.includes(feat.name)) {
147
+ //console.log("ADD", type, feat);
148
+ if (feat?.variables) {
149
+ const insertStmt = db.prepare(`INSERT INTO ${type} (name, path, ext, variables) VALUES (?, ?, ?, ?)`);
150
+ insertStmt.run(feat.name, feat.path, feat.ext, JSON.stringify(feat.variables, null, 2));
151
+ }
152
+ else {
153
+ const insertStmt2 = db.prepare(`INSERT INTO ${type} (name, path, ext) VALUES (?, ?, ?)`);
154
+ insertStmt2.run(feat.name, feat.path, feat.ext);
155
+ }
156
+ console.log("+", "[" + type + "]", feat.name, feat.path);
157
+ newFeatures.push(feat);
158
+ }
159
+ else {
160
+ //console.log("FFF", feat);
161
+ if (type == "skill") {
162
+ const updateStmt = db.prepare(`UPDATE ${type} SET variables = ? WHERE name = ?`);
163
+ //console.log("Update skill", feat.name, feat.variables)
164
+ updateStmt.run(JSON.stringify(feat.variables), feat.name);
165
+ }
166
+ }
167
+ });
168
+ return newFeatures;
169
+ }
170
+ function updateVariablesAndInfo(name, type, variableDoc, itemType, itemCat) {
171
+ const stmt1 = db.prepare(`SELECT id FROM ${type} WHERE name = ?`);
172
+ const result = stmt1.get(name);
173
+ //console.log("UV res", result);
174
+ if (!result?.id) {
175
+ return;
176
+ }
177
+ const updateStmt = db.prepare(`UPDATE ${type} SET variables = ?, type = ?, category = ? WHERE id = ?`);
178
+ //console.log("~", "task variables and info updated for", name, "/", itemType, itemCat);
179
+ updateStmt.run(variableDoc, itemType, itemCat, result.id);
180
+ }
181
+ function updateUserCmd(feat) {
182
+ const stmt1 = db.prepare("SELECT id FROM cmd WHERE name = ?");
183
+ const result = stmt1.get(feat.name);
184
+ //console.log("UV res", result);
185
+ if (!result?.id) {
186
+ return;
187
+ }
188
+ const updateStmt = db.prepare("UPDATE cmd SET variables = ? WHERE id = ?");
189
+ updateStmt.run(JSON.stringify(feat.variables, null, 2), result.id);
190
+ }
191
+ function upsertTool(name, type, toolDoc) {
192
+ const stmt1 = db.prepare("SELECT * FROM tool WHERE name = ?");
193
+ const result = stmt1.get(name);
194
+ if (result?.id) {
195
+ // Update the existing tool
196
+ const updateStmt = db.prepare("UPDATE tool SET spec = ?, type = ? WHERE id = ?");
197
+ updateStmt.run(toolDoc, type, result.id);
198
+ //console.log("~", "[tool] updated from", type, ":", name);
199
+ }
200
+ else {
201
+ // Insert a new tool
202
+ const stmt = db.prepare("INSERT INTO tool (name, spec, type) VALUES (?,?,?)");
203
+ stmt.run(name, toolDoc, type);
204
+ //console.log("+", "[tool] added from", type, ":", name);
205
+ }
206
+ }
207
+ function updateFeatures(feats, isVerbose = false) {
208
+ //console.log("FEATS", feats);
209
+ upsertAndCleanFeatures(feats.agent, "agent");
210
+ let items = {};
211
+ feats.agent.forEach((feat) => {
212
+ const { toolDoc, variables, type, category } = extractAgentToolDocAndVariables(feat.name, feat.ext, feat.path);
213
+ //const { found, toolDoc } = extractToolDoc(feat.name, feat.ext, feat.path);
214
+ //console.log(`TASK ${feat.name} TOOL DOC`, toolDoc);
215
+ if (toolDoc.length > 0) {
216
+ upsertTool(feat.name, "agent", toolDoc);
217
+ }
218
+ if (isVerbose) {
219
+ const dirp = feat.path.split("/");
220
+ dirp.pop();
221
+ const p = dirp.join("/");
222
+ if (!(p in items)) {
223
+ items[p] = { agents: [], actions: [], workflows: [], tools: [] };
224
+ }
225
+ items[p].agents.push(feat.name);
226
+ if (toolDoc.length > 0) {
227
+ items[p].tools.push(feat.name);
228
+ }
229
+ }
230
+ if (Object.keys(variables.required).length > 0 || Object.keys(variables.optional).length > 0 || type !== null || category !== null) {
231
+ //console.log("UPDATE VARS", feat.name, ":", variables)
232
+ updateVariablesAndInfo(feat.name, "agent", JSON.stringify(variables, null, " "), type, category);
233
+ }
234
+ });
235
+ upsertAndCleanFeatures(feats.action, "action");
236
+ feats.action.forEach((feat) => {
237
+ const { found, toolDoc } = extractToolDoc(feat.name, feat.ext, feat.path);
238
+ //console.log(`ACTION ${feat.name} TOOL DOC`, found);
239
+ if (found) {
240
+ upsertTool(feat.name, "action", toolDoc);
241
+ }
242
+ if (isVerbose) {
243
+ const dirp = feat.path.split("/");
244
+ dirp.pop();
245
+ const p = dirp.join("/");
246
+ if (!(p in items)) {
247
+ items[p] = { agents: [], actions: [], workflows: [], tools: [] };
248
+ }
249
+ items[p].actions.push(feat.name);
250
+ if (toolDoc.length > 0) {
251
+ items[p].tools.push(feat.name);
252
+ }
253
+ }
254
+ });
255
+ upsertAndCleanFeatures(feats.workflow, "workflow");
256
+ feats.workflow.forEach((feat) => {
257
+ const { found, toolDoc } = extractToolDoc(feat.name, feat.ext, feat.path);
258
+ //console.log(`WORKFLOW ${feat.name} TOOL DOC`, toolDoc);
259
+ if (found) {
260
+ upsertTool(feat.name, "workflow", toolDoc);
261
+ }
262
+ if (isVerbose) {
263
+ const dirp = feat.path.split("/");
264
+ dirp.pop();
265
+ const p = dirp.join("/");
266
+ if (!(p in items)) {
267
+ items[p] = { agents: [], actions: [], workflows: [], tools: [] };
268
+ }
269
+ items[p].actions.push(feat.name);
270
+ if (toolDoc.length > 0) {
271
+ items[p].tools.push(feat.name);
272
+ }
273
+ }
274
+ });
275
+ if (isVerbose) {
276
+ for (const [k, v] of Object.entries(items)) {
277
+ console.log("📂", k);
278
+ if (v.actions.length > 0) {
279
+ console.log("- actions:", v.actions.join(", "));
280
+ }
281
+ if (v.workflows.length > 0) {
282
+ console.log("- workflows:", v.workflows.join(", "));
283
+ }
284
+ if (v.agents.length > 0) {
285
+ console.log("- agents:", v.agents.join(", "));
286
+ }
287
+ if (v.tools.length > 0) {
288
+ console.log("- tools:", v.tools.join(", "));
289
+ }
290
+ }
291
+ }
292
+ upsertAndCleanFeatures(feats.adaptater, "adaptater");
293
+ upsertAndCleanFeatures(feats.cmd, "cmd");
294
+ upsertAndCleanFeatures(feats.skill, "skill");
295
+ feats.cmd.forEach(c => updateUserCmd(c));
296
+ }
297
+ function upsertSetting(name, value) {
298
+ const selectStmt = db.prepare("SELECT * FROM setting WHERE name = ?");
299
+ const result = selectStmt.get(name);
300
+ if (result?.id) {
301
+ // If the exists, update
302
+ const q = `UPDATE setting SET value = ? WHERE name = ?`;
303
+ const stmt = db.prepare(q);
304
+ const updateResult = stmt.run(value, name);
305
+ return updateResult.changes > 0;
306
+ }
307
+ else {
308
+ // If not exists, insert it
309
+ const insertStmt = db.prepare("INSERT INTO setting (name, value) VALUES (?, ?)");
310
+ insertStmt.run(name, value);
311
+ return true;
312
+ }
313
+ }
314
+ function upsertWorkspace(workspace, isVerbose = false) {
315
+ const selectStmt = db.prepare("SELECT * FROM workspace WHERE name = ?");
316
+ const result = selectStmt.get(workspace.name);
317
+ if (result?.id) {
318
+ // If the filepath exists, update it
319
+ const q = `UPDATE workspace SET path = ?, props = ? WHERE name = ?`;
320
+ const stmt = db.prepare(q);
321
+ const updateResult = stmt.run(workspace.path, JSON.stringify(workspace.props), workspace.name);
322
+ return updateResult.changes > 0;
323
+ }
324
+ else {
325
+ // If the filepath does not exist, insert it
326
+ const insertStmt = db.prepare("INSERT INTO workspace (name, path, props) VALUES (?, ?, ?)");
327
+ insertStmt.run(workspace.name, workspace.path, JSON.stringify(workspace.props));
328
+ if (isVerbose) {
329
+ console.log("-", "[workspace]", workspace.name, workspace.path);
330
+ }
331
+ return true;
332
+ }
333
+ }
334
+ function upsertAndCleanWorkspaces(workspaces) {
335
+ // Get all existing workspace names
336
+ const existingStmt = db.prepare("SELECT name FROM workspace");
337
+ const existingWorkspaces = existingStmt.all();
338
+ const existingNames = new Set(existingWorkspaces.map(w => w.name));
339
+ // Create a set of new workspace names for comparison
340
+ const newNames = new Set(workspaces.map(w => w.name));
341
+ // Delete workspaces that are not in the new list
342
+ const toDelete = Array.from(existingNames).filter(name => !newNames.has(name));
343
+ for (const name of toDelete) {
344
+ deleteWorkspace(name);
345
+ console.log("-", "[workspace]", name);
346
+ }
347
+ // Upsert the new workspaces and track new/updated ones
348
+ const updatedWorkspaces = new Array();
349
+ for (const workspace of workspaces) {
350
+ if (upsertWorkspace(workspace, true)) {
351
+ updatedWorkspaces.push(workspace);
352
+ }
353
+ }
354
+ return updatedWorkspaces;
355
+ }
356
+ function upsertFilePath(name, newPath) {
357
+ const selectStmt = db.prepare("SELECT * FROM filepath WHERE name = ?");
358
+ const result = selectStmt.get(name);
359
+ if (result?.id) {
360
+ // If the filepath exists, update it
361
+ const q = `UPDATE filepath SET path = ? WHERE name = ?`;
362
+ const stmt = db.prepare(q);
363
+ const updateResult = stmt.run(newPath, name);
364
+ return updateResult.changes > 0;
365
+ }
366
+ else {
367
+ // If the filepath does not exist, insert it
368
+ const insertStmt = db.prepare("INSERT INTO filepath (name, path) VALUES (?, ?)");
369
+ insertStmt.run(name, newPath);
370
+ return true;
371
+ }
372
+ }
373
+ function upsertAgentSettings(taskName, settings) {
374
+ //console.log("Upsert agent settings", taskName, settings);
375
+ const selectStmt = db.prepare("SELECT * FROM agentsettings WHERE name = ?");
376
+ const result = selectStmt.get(taskName);
377
+ if (result?.id) {
378
+ const qparams = new Array();
379
+ const qvalues = new Array();
380
+ qparams.push("model = ?");
381
+ qvalues.push(settings?.model ?? null);
382
+ if (settings?.max_tokens !== undefined) {
383
+ qparams.push("max_tokens = ?");
384
+ qvalues.push(settings.max_tokens);
385
+ }
386
+ if (settings?.top_k !== undefined) {
387
+ qparams.push("top_k = ?");
388
+ qvalues.push(settings.top_k);
389
+ }
390
+ if (settings?.top_p !== undefined) {
391
+ qparams.push("top_p = ?");
392
+ qvalues.push(settings.top_p);
393
+ }
394
+ if (settings?.min_p !== undefined) {
395
+ qparams.push("min_p = ?");
396
+ qvalues.push(settings.min_p);
397
+ }
398
+ if (settings?.temperature !== undefined) {
399
+ qparams.push("temperature = ?");
400
+ qvalues.push(settings.temperature);
401
+ }
402
+ if (settings?.repeat_penalty !== undefined) {
403
+ qparams.push("repeat_penalty = ?");
404
+ qvalues.push(settings.repeat_penalty);
405
+ }
406
+ if (settings?.presence_penalty !== undefined) {
407
+ qparams.push("presence_penalty = ?");
408
+ qvalues.push(settings.presence_penalty);
409
+ }
410
+ if (settings?.frequency_penalty !== undefined) {
411
+ qparams.push("frequency_penalty = ?");
412
+ qvalues.push(settings.frequency_penalty);
413
+ }
414
+ if (settings?.backend !== undefined) {
415
+ qparams.push("backend = ?");
416
+ qvalues.push(settings.backend);
417
+ }
418
+ if (settings?.chat_template_kwargs !== undefined) {
419
+ qparams.push("chat_template_kwargs = ?");
420
+ qvalues.push(JSON.stringify(settings.chat_template_kwargs));
421
+ }
422
+ if (settings?.props !== undefined) {
423
+ qparams.push("props = ?");
424
+ qvalues.push(JSON.stringify(settings.props));
425
+ }
426
+ const q = `UPDATE agentsettings SET ${qparams.join(", ")} WHERE name = ?`;
427
+ //console.log("Q", q, qparams);
428
+ const stmt = db.prepare(q);
429
+ const updateResult = stmt.run(...qvalues, taskName);
430
+ return updateResult.changes > 0;
431
+ }
432
+ else {
433
+ const qnames = new Array();
434
+ const qvalues = new Array();
435
+ qnames.push("model");
436
+ qvalues.push(settings?.model ?? null);
437
+ if (settings?.max_tokens !== undefined) {
438
+ qnames.push("max_tokens");
439
+ qvalues.push(settings.max_tokens);
440
+ }
441
+ if (settings?.top_k !== undefined) {
442
+ qnames.push("top_k");
443
+ qvalues.push(settings.top_k);
444
+ }
445
+ if (settings?.top_p !== undefined) {
446
+ qnames.push("top_p");
447
+ qvalues.push(settings.top_p);
448
+ }
449
+ if (settings?.min_p !== undefined) {
450
+ qnames.push("min_p");
451
+ qvalues.push(settings.min_p);
452
+ }
453
+ if (settings?.temperature !== undefined) {
454
+ qnames.push("temperature");
455
+ qvalues.push(settings.temperature);
456
+ }
457
+ if (settings?.repeat_penalty !== undefined) {
458
+ qnames.push("repeat_penalty");
459
+ qvalues.push(settings.repeat_penalty);
460
+ }
461
+ if (settings?.presence_penalty !== undefined) {
462
+ qnames.push("presence_penalty");
463
+ qvalues.push(settings.presence_penalty);
464
+ }
465
+ if (settings?.frequency_penalty !== undefined) {
466
+ qnames.push("frequency_penalty");
467
+ qvalues.push(settings.frequency_penalty);
468
+ }
469
+ if (settings?.backend !== undefined) {
470
+ qnames.push("backend");
471
+ qvalues.push(settings.backend);
472
+ }
473
+ if (settings?.chat_template_kwargs !== undefined) {
474
+ qnames.push("chat_template_kwargs");
475
+ qvalues.push(JSON.stringify(settings.chat_template_kwargs));
476
+ }
477
+ if (settings?.props !== undefined) {
478
+ qnames.push("props");
479
+ qvalues.push(JSON.stringify(settings.props));
480
+ }
481
+ const nq = new Array("?");
482
+ qnames.forEach(n => nq.push("?"));
483
+ const q = `INSERT INTO agentsettings (name, ${qnames.join(", ")}) VALUES (${nq.join(", ")})`;
484
+ //console.log("Q2", q);
485
+ //console.log("VALs", qvalues);
486
+ const insertStmt = db.prepare(q);
487
+ insertStmt.run(taskName, ...qvalues);
488
+ return true;
489
+ }
490
+ }
491
+ function upsertSamplingPreset(preset) {
492
+ const stmt1 = db.prepare("SELECT * FROM modelpreset WHERE name = ?");
493
+ const result = stmt1.get(preset.name);
494
+ let hasUpdates = false;
495
+ if (result?.id) {
496
+ // Update existing model preset
497
+ const updateStmt = db.prepare(`
498
+ UPDATE modelpreset SET
499
+ model = ?,
500
+ max_tokens = ?,
501
+ top_k = ?,
502
+ top_p = ?,
503
+ min_p = ?,
504
+ temperature = ?,
505
+ repeat_penalty = ?,
506
+ presence_penalty = ?,
507
+ frequency_penalty = ?,
508
+ backend = ?,
509
+ chat_template_kwargs = ?,
510
+ props = ?
511
+ WHERE name = ?`);
512
+ updateStmt.run(preset.model, preset.max_tokens ?? null, preset.top_k ?? null, preset.top_p ?? null, preset.min_p ?? null, preset.temperature ?? null, preset.repeat_penalty ?? null, preset.presence_penalty ?? null, preset.frequency_penalty ?? null, preset.backend ?? null, preset?.chat_template_kwargs ? JSON.stringify(preset.chat_template_kwargs) : null, preset?.props ? JSON.stringify(preset.props) : null, preset.name);
513
+ hasUpdates = true;
514
+ }
515
+ else {
516
+ // Insert new model preset
517
+ const stmt = db.prepare(`
518
+ INSERT INTO modelpreset
519
+ (name, model, max_tokens, top_k, top_p, min_p, temperature, repeat_penalty, presence_penalty, frequency_penalty, backend, chat_template_kwargs, props)
520
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
521
+ stmt.run(preset.name, preset.model, preset.max_tokens ?? null, preset.top_k ?? null, preset.top_p ?? null, preset.min_p ?? null, preset.temperature ?? null, preset.repeat_penalty ?? null, preset.presence_penalty ?? null, preset.frequency_penalty ?? null, preset.backend ?? null, preset?.chat_template_kwargs ? JSON.stringify(preset.chat_template_kwargs) : null, preset?.props ? JSON.stringify(preset.props) : null);
522
+ hasUpdates = true;
523
+ }
524
+ return hasUpdates;
525
+ }
526
+ function upsertSamplingPresets(modelPresets) {
527
+ let hasUpdates = false;
528
+ // Get all existing model preset names
529
+ const existingStmt = db.prepare("SELECT name FROM modelpreset");
530
+ const existingSamplingPresets = existingStmt.all();
531
+ const existingNames = new Set(existingSamplingPresets.map(p => p.name));
532
+ // Create a set of new model preset names for comparison
533
+ const newNames = new Set(modelPresets.map(p => p.name));
534
+ // Delete model presets that are not in the new list
535
+ const toDelete = Array.from(existingNames).filter(name => !newNames.has(name));
536
+ if (toDelete.length > 0) {
537
+ const deleteStmt = db.prepare("DELETE FROM modelpreset WHERE name = ?");
538
+ for (const name of toDelete) {
539
+ deleteStmt.run(name);
540
+ }
541
+ hasUpdates = true;
542
+ }
543
+ // Upsert the new model presets
544
+ for (const preset of modelPresets) {
545
+ const up = upsertSamplingPreset(preset);
546
+ if (!hasUpdates && up) {
547
+ hasUpdates = up;
548
+ }
549
+ }
550
+ return hasUpdates;
551
+ }
552
+ function deleteWorkspace(name) {
553
+ const deleteStmt = db.prepare("DELETE FROM workspace WHERE name = ?");
554
+ deleteStmt.run(name);
555
+ }
556
+ function deleteAgentSettings(settings) {
557
+ settings.forEach(s => {
558
+ const deleteStmt = db.prepare("DELETE FROM agentsettings WHERE name = ?");
559
+ deleteStmt.run(s);
560
+ });
561
+ }
562
+ function deleteAgentSetting(name) {
563
+ const deleteStmt = db.prepare("DELETE FROM agentsettings WHERE name = ?");
564
+ deleteStmt.run(name);
565
+ }
566
+ function deleteSamplingPreset(name) {
567
+ const deleteStmt = db.prepare("DELETE FROM modelpreset WHERE name = ?");
568
+ deleteStmt.run(name);
569
+ }
570
+ export { updatePromptfilePath, updateWorkspacePath, updateDataDirPath, upsertBackends, upsertSetting, upsertWorkspace, upsertAndCleanWorkspaces, setDefaultBackend, insertFeaturesPathIfNotExists, insertPluginIfNotExists, updateFeatures, updateAliases, cleanupFeaturePaths, upsertFilePath, upsertAgentSettings, deleteAgentSettings, deleteAgentSetting, deleteWorkspace, upsertSamplingPresets, upsertSamplingPreset, deleteSamplingPreset, };
@@ -0,0 +1,2 @@
1
+ declare function action(args: Record<string, any>, options: Record<string, any>): string;
2
+ export { action, };
@@ -0,0 +1,37 @@
1
+ /*
2
+ # tool
3
+ name: load-skill
4
+ description: read a given skill
5
+ arguments:
6
+ name:
7
+ description: the name of the skill to load
8
+ required: true
9
+ */
10
+ import { default as fm } from "front-matter";
11
+ import { readFeature } from "../../db/read.js";
12
+ import { readFile } from "../../utils/sys/read.js";
13
+ import { runtimeDataError } from "../../utils/user_msgs.js";
14
+ function action(args, options) {
15
+ let sb = "";
16
+ let errMsg = "";
17
+ if (!args?.name) {
18
+ errMsg = `loading skill: provide a skill name`;
19
+ }
20
+ const { found, feature } = readFeature(args.name, "skill");
21
+ if (!found) {
22
+ errMsg = `skill ${args.name} not found`;
23
+ }
24
+ if (errMsg) {
25
+ if (options?.onError) {
26
+ options.onError(errMsg, "load-skill");
27
+ }
28
+ else {
29
+ runtimeDataError(errMsg);
30
+ }
31
+ }
32
+ const fc = readFile(feature.path);
33
+ const data = fm(fc);
34
+ sb = data.body;
35
+ return sb;
36
+ }
37
+ export { action, };
@@ -0,0 +1,2 @@
1
+ declare function action(args: Record<string, any>, options: Record<string, any>): Promise<string>;
2
+ export { action, };
@@ -0,0 +1,25 @@
1
+ /*
2
+ # tool
3
+ name: notify-user
4
+ description: use to send a notification to the user
5
+ arguments:
6
+ title:
7
+ description: 'the title of the notification. Format: "From [your-name]: notification title"'
8
+ required: true
9
+ message:
10
+ description: the short message to send to the user in a notification
11
+ required: true
12
+ */
13
+ // @ts-ignore
14
+ import pkg from 'node-notifier';
15
+ const { notify } = pkg;
16
+ async function action(args, options) {
17
+ //console.log("NA", args);
18
+ //console.log("NO", options);
19
+ notify({
20
+ message: args.message,
21
+ title: args.title,
22
+ });
23
+ return "Done";
24
+ }
25
+ export { action, };
@@ -0,0 +1,2 @@
1
+ declare function action(args: Record<string, any>, options: Record<string, any>): Promise<string>;
2
+ export { action, };