@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.
- package/README.md +378 -0
- package/dist/actions/cmd.d.ts +5 -0
- package/dist/actions/cmd.js +135 -0
- package/dist/actions/read.d.ts +3 -0
- package/dist/actions/read.js +17 -0
- package/dist/adaptaters/cmd.d.ts +2 -0
- package/dist/adaptaters/cmd.js +39 -0
- package/dist/agents/cmd.d.ts +3 -0
- package/dist/agents/cmd.js +10 -0
- package/dist/agents/conf.d.ts +3 -0
- package/dist/agents/conf.js +9 -0
- package/dist/agents/files.d.ts +3 -0
- package/dist/agents/files.js +40 -0
- package/dist/agents/read.d.ts +11 -0
- package/dist/agents/read.js +241 -0
- package/dist/agents/useagent.d.ts +13 -0
- package/dist/agents/useagent.js +395 -0
- package/dist/conf.d.ts +14 -0
- package/dist/conf.js +160 -0
- package/dist/const.d.ts +3 -0
- package/dist/const.js +10 -0
- package/dist/db/db.d.ts +3 -0
- package/dist/db/db.js +24 -0
- package/dist/db/read.d.ts +45 -0
- package/dist/db/read.js +280 -0
- package/dist/db/schemas.d.ts +2 -0
- package/dist/db/schemas.js +141 -0
- package/dist/db/write.d.ts +23 -0
- package/dist/db/write.js +570 -0
- package/dist/features/actions/load-skill.d.ts +2 -0
- package/dist/features/actions/load-skill.js +37 -0
- package/dist/features/actions/notify-user.d.ts +2 -0
- package/dist/features/actions/notify-user.js +25 -0
- package/dist/features/actions/read-feature.d.ts +2 -0
- package/dist/features/actions/read-feature.js +25 -0
- package/dist/features/actions/run-agent.d.ts +2 -0
- package/dist/features/actions/run-agent.js +34 -0
- package/dist/features/actions/run-collaborator.d.ts +1 -0
- package/dist/features/actions/run-collaborator.js +42 -0
- package/dist/features/actions/run-worker.d.ts +2 -0
- package/dist/features/actions/run-worker.js +34 -0
- package/dist/features/adaptaters/agent-smith-db-getschema.d.ts +4 -0
- package/dist/features/adaptaters/agent-smith-db-getschema.js +62 -0
- package/dist/features/adaptaters/imgs2base64.d.ts +1 -0
- package/dist/features/adaptaters/imgs2base64.js +13 -0
- package/dist/features/adaptaters/prequery.d.ts +4 -0
- package/dist/features/adaptaters/prequery.js +5 -0
- package/dist/features/agents/agent-smith-colab.yml +37 -0
- package/dist/features/agents/agent-smith-doc.yml +50 -0
- package/dist/features/agents/agent-smith-help.yml +35 -0
- package/dist/features/agents/agent-smith-search.yml +41 -0
- package/dist/features/agents/agent-smith-sql.yml +31 -0
- package/dist/features/agents/agent-smith.yml +37 -0
- package/dist/features/agents/collaborator.yml +15 -0
- package/dist/features/agents/infer.yml +12 -0
- package/dist/features/fragments/ctx-helper-files.md +4 -0
- package/dist/features/fragments/workspace.txt +6 -0
- package/dist/features/skills/create-package-readme/SKILL.md +66 -0
- package/dist/features/skills/document-package/SKILL.md +83 -0
- package/dist/features/skills/t/SKILL.md +26 -0
- package/dist/features/skills/update-codebase-summary/SKILL.md +45 -0
- package/dist/features/skills/update-doc-map/SKILL.md +8 -0
- package/dist/features/skills/update-doc-map/scripts/generate-doc-map.mjs +196 -0
- package/dist/features/workflows/agent-smith-db.yml +9 -0
- package/dist/features/workflows/q.yml +3 -0
- package/dist/features/workflows/vision.yml +3 -0
- package/dist/main.d.ts +127 -0
- package/dist/main.js +80 -0
- package/dist/mcp.d.ts +16 -0
- package/dist/mcp.js +110 -0
- package/dist/state/backends.d.ts +7 -0
- package/dist/state/backends.js +96 -0
- package/dist/state/features.d.ts +10 -0
- package/dist/state/features.js +42 -0
- package/dist/state/plugins.d.ts +6 -0
- package/dist/state/plugins.js +27 -0
- package/dist/state/state.d.ts +18 -0
- package/dist/state/state.js +79 -0
- package/dist/state/tasks.d.ts +6 -0
- package/dist/state/tasks.js +33 -0
- package/dist/tools.d.ts +15 -0
- package/dist/tools.js +149 -0
- package/dist/updateconf.d.ts +5 -0
- package/dist/updateconf.js +124 -0
- package/dist/utils/io.d.ts +10 -0
- package/dist/utils/io.js +98 -0
- package/dist/utils/perf.d.ts +9 -0
- package/dist/utils/perf.js +63 -0
- package/dist/utils/sys/clipboard.d.ts +3 -0
- package/dist/utils/sys/clipboard.js +33 -0
- package/dist/utils/sys/delete_file.d.ts +2 -0
- package/dist/utils/sys/delete_file.js +10 -0
- package/dist/utils/sys/dirs.d.ts +2 -0
- package/dist/utils/sys/dirs.js +9 -0
- package/dist/utils/sys/execute.d.ts +13 -0
- package/dist/utils/sys/execute.js +48 -0
- package/dist/utils/sys/read.d.ts +3 -0
- package/dist/utils/sys/read.js +21 -0
- package/dist/utils/sys/read_agent.d.ts +6 -0
- package/dist/utils/sys/read_agent.js +23 -0
- package/dist/utils/sys/read_cmds.d.ts +7 -0
- package/dist/utils/sys/read_cmds.js +37 -0
- package/dist/utils/sys/read_conf.d.ts +6 -0
- package/dist/utils/sys/read_conf.js +12 -0
- package/dist/utils/sys/read_features.d.ts +3 -0
- package/dist/utils/sys/read_features.js +134 -0
- package/dist/utils/sys/read_yml_file.d.ts +5 -0
- package/dist/utils/sys/read_yml_file.js +11 -0
- package/dist/utils/sys/run_python.d.ts +6 -0
- package/dist/utils/sys/run_python.js +39 -0
- package/dist/utils/text.d.ts +2 -0
- package/dist/utils/text.js +28 -0
- package/dist/utils/user_msgs.d.ts +5 -0
- package/dist/utils/user_msgs.js +19 -0
- package/dist/workflows/cmd.d.ts +3 -0
- package/dist/workflows/cmd.js +189 -0
- package/dist/workflows/read.d.ts +6 -0
- package/dist/workflows/read.js +61 -0
- package/package.json +45 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { confDir, createConfigFileIfNotExists, dbPath, processConfPath } from "./conf.js";
|
|
3
|
+
import { initDb } from "./db/db.js";
|
|
4
|
+
import { readFeaturePaths, readFilePath } from "./db/read.js";
|
|
5
|
+
import { cleanupFeaturePaths, updateAliases, updateDataDirPath, updateFeatures, updatePromptfilePath, upsertFilePath } from "./db/write.js";
|
|
6
|
+
import { getBuiltinFeaturesDirPath, readFeaturesDirs } from "./state/features.js";
|
|
7
|
+
import { readPluginsPaths } from "./state/plugins.js";
|
|
8
|
+
import { dataDirPath, promptfilePath } from "./state/state.js";
|
|
9
|
+
//import { runtimeDataError, runtimeInfo } from './user_msgs.js';
|
|
10
|
+
import { readUserCmd } from "./utils/sys/read_cmds.js";
|
|
11
|
+
import { deleteFileIfExists } from "./utils/sys/delete_file.js";
|
|
12
|
+
async function getUserCmdsData(feats) {
|
|
13
|
+
for (const feat of feats.cmd) {
|
|
14
|
+
const cmdPath = path.join(feat.path, feat.name + "." + feat.ext);
|
|
15
|
+
const { found, userCmd } = await readUserCmd(feat.name, cmdPath);
|
|
16
|
+
//console.log("READ CMD", userCmd);
|
|
17
|
+
if (!userCmd?.name) {
|
|
18
|
+
throw new Error(`provide a name for the ${feat.path} command`);
|
|
19
|
+
}
|
|
20
|
+
if (!userCmd?.description) {
|
|
21
|
+
throw new Error(`provide a description for the ${feat.path} command`);
|
|
22
|
+
}
|
|
23
|
+
if (found) {
|
|
24
|
+
feat.variables = {
|
|
25
|
+
description: userCmd.description,
|
|
26
|
+
name: userCmd.name,
|
|
27
|
+
};
|
|
28
|
+
if (userCmd?.options) {
|
|
29
|
+
feat.variables.options = userCmd.options;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return feats;
|
|
34
|
+
}
|
|
35
|
+
async function updateAllFeatures(paths, userFeats) {
|
|
36
|
+
//console.log("updateAllFeatures", paths);
|
|
37
|
+
const p = [getBuiltinFeaturesDirPath(), ...paths];
|
|
38
|
+
let feats = readFeaturesDirs(p, true);
|
|
39
|
+
feats = await getUserCmdsData(feats);
|
|
40
|
+
if (userFeats?.action) {
|
|
41
|
+
feats.action.push(...userFeats.action);
|
|
42
|
+
}
|
|
43
|
+
if (userFeats?.adaptater) {
|
|
44
|
+
feats.adaptater.push(...userFeats.adaptater);
|
|
45
|
+
}
|
|
46
|
+
if (userFeats?.agent) {
|
|
47
|
+
feats.agent.push(...userFeats.agent);
|
|
48
|
+
}
|
|
49
|
+
if (userFeats?.cmd) {
|
|
50
|
+
feats.cmd.push(...userFeats.cmd);
|
|
51
|
+
}
|
|
52
|
+
if (userFeats?.workflow) {
|
|
53
|
+
feats.workflow.push(...userFeats.workflow);
|
|
54
|
+
}
|
|
55
|
+
if (userFeats?.skill) {
|
|
56
|
+
feats.skill.push(...userFeats.skill);
|
|
57
|
+
}
|
|
58
|
+
updateFeatures(feats);
|
|
59
|
+
updateAliases(feats);
|
|
60
|
+
const deleted = cleanupFeaturePaths(paths);
|
|
61
|
+
for (const el of deleted) {
|
|
62
|
+
console.log("- [feature path]", el);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async function updateFeaturesCmd(options, userFeats) {
|
|
66
|
+
const fp = readFeaturePaths();
|
|
67
|
+
const pp = await readPluginsPaths();
|
|
68
|
+
const paths = [...fp, ...pp];
|
|
69
|
+
updateAllFeatures(paths, userFeats);
|
|
70
|
+
}
|
|
71
|
+
async function recreateDbFromConf() {
|
|
72
|
+
// try to find a conf path in db
|
|
73
|
+
let confPath;
|
|
74
|
+
const cf = readFilePath("conf");
|
|
75
|
+
if (cf.found) {
|
|
76
|
+
confPath = cf.path;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
// use default conf path
|
|
80
|
+
confPath = path.join(confDir, "config.yml");
|
|
81
|
+
}
|
|
82
|
+
console.log("Deleting db");
|
|
83
|
+
deleteFileIfExists(dbPath);
|
|
84
|
+
console.log("Using", confPath, "to recreate the db");
|
|
85
|
+
await updateConfCmd([confPath]);
|
|
86
|
+
console.log(`Config recreated db ${dbPath} from ${confPath} ok`);
|
|
87
|
+
}
|
|
88
|
+
async function updateConfCmd(args) {
|
|
89
|
+
initDb(false, true);
|
|
90
|
+
let confPath;
|
|
91
|
+
const userProvidedConfPath = (args[0] != "conf") ? args[0] : null;
|
|
92
|
+
if (userProvidedConfPath) {
|
|
93
|
+
confPath = userProvidedConfPath;
|
|
94
|
+
const isu = upsertFilePath("conf", confPath);
|
|
95
|
+
if (isu) {
|
|
96
|
+
console.log("Config path", confPath, "updated");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
// try to find a conf path in db
|
|
101
|
+
const cf = readFilePath("conf");
|
|
102
|
+
if (cf.found) {
|
|
103
|
+
confPath = cf.path;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
// use default conf path
|
|
107
|
+
confPath = path.join(confDir, "config.yml");
|
|
108
|
+
upsertFilePath("conf", confPath);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
createConfigFileIfNotExists(confPath);
|
|
112
|
+
const { paths, pf, dd } = await processConfPath(confPath);
|
|
113
|
+
console.log("Using", confPath, "to update features");
|
|
114
|
+
if (pf.length > 0) {
|
|
115
|
+
updatePromptfilePath(pf);
|
|
116
|
+
promptfilePath.value = pf;
|
|
117
|
+
}
|
|
118
|
+
if (dd.length > 0) {
|
|
119
|
+
updateDataDirPath(dd);
|
|
120
|
+
dataDirPath.value = dd;
|
|
121
|
+
}
|
|
122
|
+
updateAllFeatures(paths);
|
|
123
|
+
}
|
|
124
|
+
export { updateConfCmd, updateFeaturesCmd, recreateDbFromConf, };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { InferenceResult, AgentSpec } from "@agent-smith/types";
|
|
2
|
+
declare function readPromptFile(): string;
|
|
3
|
+
declare function processOutput(res: InferenceResult): Promise<void>;
|
|
4
|
+
declare function openAgentSpec(name: string): {
|
|
5
|
+
agentSpec: AgentSpec;
|
|
6
|
+
agentPath: string;
|
|
7
|
+
};
|
|
8
|
+
declare function getInputFromOptions(options: Record<string, any>): Promise<string | null>;
|
|
9
|
+
declare function getAgentPrompt(name: string, args: Array<any> | Record<string, any>, options: Record<string, any>): Promise<string>;
|
|
10
|
+
export { getAgentPrompt, getInputFromOptions, openAgentSpec, readPromptFile, processOutput, };
|
package/dist/utils/io.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import YAML from 'yaml';
|
|
2
|
+
import { readClipboard } from '../utils/sys/clipboard.js';
|
|
3
|
+
import { readAgent } from "../utils/sys/read_agent.js";
|
|
4
|
+
import { getFeatureSpec } from "../state/features.js";
|
|
5
|
+
import { runtimeWarning } from '../utils/user_msgs.js';
|
|
6
|
+
import { initFilepaths, promptfilePath, outputMode, formatMode } from "../state/state.js";
|
|
7
|
+
import { readFile } from "../utils/sys/read.js";
|
|
8
|
+
import { runtimeError } from '../utils/user_msgs.js';
|
|
9
|
+
import { writeToClipboard } from '../utils/sys/clipboard.js';
|
|
10
|
+
import { marked } from 'marked';
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
import { markedTerminal } from "marked-terminal";
|
|
13
|
+
marked.use(markedTerminal());
|
|
14
|
+
function readPromptFile() {
|
|
15
|
+
initFilepaths();
|
|
16
|
+
return readFile(promptfilePath.value);
|
|
17
|
+
}
|
|
18
|
+
async function processOutput(res) {
|
|
19
|
+
//if (!(outputMode.value == "clipboard")) { return }
|
|
20
|
+
let data = "";
|
|
21
|
+
//console.log("Process OUTPUT", typeof res);
|
|
22
|
+
let hasTextData = false;
|
|
23
|
+
if (typeof res == "object") {
|
|
24
|
+
if (res?.text) {
|
|
25
|
+
data = res.text;
|
|
26
|
+
hasTextData = true;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
try {
|
|
30
|
+
data = JSON.stringify(res);
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
runtimeError("Unable to parse json result");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
data = res;
|
|
39
|
+
}
|
|
40
|
+
//onsole.log("OUTPUT", typeof res, data);
|
|
41
|
+
if (outputMode.value == "clipboard") {
|
|
42
|
+
//console.log("Writing to kb", data)
|
|
43
|
+
await writeToClipboard(data);
|
|
44
|
+
}
|
|
45
|
+
if (hasTextData) {
|
|
46
|
+
if (formatMode.value == "markdown") {
|
|
47
|
+
console.log("\n------------------\n");
|
|
48
|
+
console.log(marked.parse(data).trim());
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function openAgentSpec(name) {
|
|
53
|
+
const { found, path } = getFeatureSpec(name, "agent");
|
|
54
|
+
if (!found) {
|
|
55
|
+
throw new Error(`agent ${name} not found`);
|
|
56
|
+
}
|
|
57
|
+
const res = readAgent(path);
|
|
58
|
+
if (!res.found) {
|
|
59
|
+
throw new Error(`agent ${name}, ${path} not found`);
|
|
60
|
+
}
|
|
61
|
+
const agentSpec = YAML.parse(res.ymlAgent);
|
|
62
|
+
agentSpec.name = name;
|
|
63
|
+
return { agentSpec: agentSpec, agentPath: path };
|
|
64
|
+
}
|
|
65
|
+
async function getInputFromOptions(options) {
|
|
66
|
+
let out = null;
|
|
67
|
+
if (options?.clipboardInput === true) {
|
|
68
|
+
out = await readClipboard();
|
|
69
|
+
options.clipboardInput = false;
|
|
70
|
+
}
|
|
71
|
+
else if (options?.inputFile === true) {
|
|
72
|
+
out = readPromptFile();
|
|
73
|
+
options.inputFile = false;
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
async function getAgentPrompt(name, args, options) {
|
|
78
|
+
const ic = await getInputFromOptions(options);
|
|
79
|
+
if (ic) {
|
|
80
|
+
return ic;
|
|
81
|
+
}
|
|
82
|
+
let pr = "";
|
|
83
|
+
if (Array.isArray(args)) {
|
|
84
|
+
if (args[0] !== undefined) {
|
|
85
|
+
pr = args[0];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
if (args?.prompt) {
|
|
90
|
+
pr = args.prompt;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (pr.length == 0) {
|
|
94
|
+
runtimeWarning("empty prompt provided to agent", name);
|
|
95
|
+
}
|
|
96
|
+
return pr;
|
|
97
|
+
}
|
|
98
|
+
export { getAgentPrompt, getInputFromOptions, openAgentSpec, readPromptFile, processOutput, };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const usePerfTimer = (startTimer = true) => {
|
|
2
|
+
let startTime;
|
|
3
|
+
const measurements = [];
|
|
4
|
+
let lastTime;
|
|
5
|
+
if (startTimer) {
|
|
6
|
+
startTime = performance.now();
|
|
7
|
+
lastTime = startTime;
|
|
8
|
+
}
|
|
9
|
+
const measure = (name) => {
|
|
10
|
+
const currentTime = performance.now();
|
|
11
|
+
const elapsedNs = Number(currentTime - lastTime);
|
|
12
|
+
const elapsedSec = elapsedNs / 1000;
|
|
13
|
+
measurements.push({ name, time: elapsedSec, percentage: 0 });
|
|
14
|
+
lastTime = currentTime;
|
|
15
|
+
console.log(name, elapsedSec);
|
|
16
|
+
};
|
|
17
|
+
const final = (name = "") => {
|
|
18
|
+
const totalTime = Number(performance.now() - startTime) / 1000;
|
|
19
|
+
console.log("\n*** Performance", name.length > 0 ? `for ${name}` : "", " ***");
|
|
20
|
+
measurements.forEach(m => {
|
|
21
|
+
m.percentage = (m.time / totalTime) * 100;
|
|
22
|
+
console.log(` - ${m.name}: ${m.time.toFixed(6)}s (${m.percentage.toFixed(2)}%)`);
|
|
23
|
+
});
|
|
24
|
+
console.log(`Total time: ${totalTime.toFixed(6)}s\n`);
|
|
25
|
+
};
|
|
26
|
+
const start = () => {
|
|
27
|
+
startTime = performance.now();
|
|
28
|
+
lastTime = startTime;
|
|
29
|
+
};
|
|
30
|
+
const _end = (raw) => {
|
|
31
|
+
if (!startTime) {
|
|
32
|
+
throw new Error("the timer has not started, can not end it");
|
|
33
|
+
}
|
|
34
|
+
const endTime = performance.now();
|
|
35
|
+
const duration = endTime - startTime;
|
|
36
|
+
if (raw) {
|
|
37
|
+
return duration;
|
|
38
|
+
}
|
|
39
|
+
const humanizedTime = _formatDuration(duration);
|
|
40
|
+
return humanizedTime;
|
|
41
|
+
};
|
|
42
|
+
const time = () => _end(false).toString();
|
|
43
|
+
const printTime = () => console.log(_end(false));
|
|
44
|
+
const timeRaw = () => _end(true);
|
|
45
|
+
const _formatDuration = (ms) => {
|
|
46
|
+
const seconds = ms / 1000;
|
|
47
|
+
const minutes = seconds / 60;
|
|
48
|
+
if (ms < 1000)
|
|
49
|
+
return `${ms.toFixed(2)} milliseconds`;
|
|
50
|
+
if (seconds < 60)
|
|
51
|
+
return `${seconds.toFixed(1)} seconds`;
|
|
52
|
+
return `${minutes.toFixed()} minutes ${(seconds % 60).toFixed()} seconds`;
|
|
53
|
+
};
|
|
54
|
+
return {
|
|
55
|
+
start,
|
|
56
|
+
time,
|
|
57
|
+
timeRaw,
|
|
58
|
+
printTime,
|
|
59
|
+
measure,
|
|
60
|
+
final,
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
export { usePerfTimer };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import clipboard from 'clipboardy';
|
|
2
|
+
//import { execute } from "./execute.js";
|
|
3
|
+
//const { platform } = process;
|
|
4
|
+
async function readClipboard() {
|
|
5
|
+
/*let res = "";
|
|
6
|
+
if (platform == "linux") {
|
|
7
|
+
res = await execute("xclip", ["-o", "-selection", "primary"]);
|
|
8
|
+
} else {
|
|
9
|
+
res = await clipboard.read();
|
|
10
|
+
}
|
|
11
|
+
return res*/
|
|
12
|
+
return await clipboard.read();
|
|
13
|
+
}
|
|
14
|
+
async function writeToClipboard(data) {
|
|
15
|
+
await clipboard.write(data);
|
|
16
|
+
}
|
|
17
|
+
/*import { platform } from "os";
|
|
18
|
+
|
|
19
|
+
async function getClipboard(): Promise<string> {
|
|
20
|
+
const currentPlatform = platform();
|
|
21
|
+
|
|
22
|
+
switch (currentPlatform) {
|
|
23
|
+
case 'darwin': // macOS
|
|
24
|
+
return await execute("pbpaste", []);
|
|
25
|
+
case 'win32': // Windows
|
|
26
|
+
return await execute("powershell", ["-command", "Get-Clipboard"]);
|
|
27
|
+
case 'linux': // Linux
|
|
28
|
+
return await execute("xclip", ["-o", "-selection", "clipboard"]);
|
|
29
|
+
default:
|
|
30
|
+
throw new Error(`Unsupported platform: ${currentPlatform}`);
|
|
31
|
+
}
|
|
32
|
+
}*/
|
|
33
|
+
export { readClipboard, writeToClipboard, };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
function createDirectoryIfNotExists(dirPath, recursive = false) {
|
|
4
|
+
const resolvedDirPath = path.resolve(dirPath);
|
|
5
|
+
if (!fs.existsSync(resolvedDirPath)) {
|
|
6
|
+
fs.mkdirSync(resolvedDirPath, { recursive: recursive });
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export { createDirectoryIfNotExists };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare function execute(command: string, args?: Array<string>, { onStdout, onStderr, onError, stream, }?: {
|
|
2
|
+
onStderr?: (data: any) => void;
|
|
3
|
+
onError?: (err: any) => void;
|
|
4
|
+
onStdout?: ((data: any) => void) | undefined;
|
|
5
|
+
stream?: boolean | undefined;
|
|
6
|
+
}): Promise<string>;
|
|
7
|
+
declare function runShellCmd(command: string, args?: Array<string>, { onStdout, onStderr, onError, onFinished, }?: {
|
|
8
|
+
onStdout?: (data: any) => void;
|
|
9
|
+
onStderr?: (data: any) => void;
|
|
10
|
+
onError?: (err: any) => void;
|
|
11
|
+
onFinished?: () => void;
|
|
12
|
+
}): () => boolean;
|
|
13
|
+
export { execute, runShellCmd };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { spawn } from "child_process";
|
|
2
|
+
import { platform } from "os";
|
|
3
|
+
async function execute(command, args = [], { onStdout = (data) => { }, onStderr = (data) => { }, onError = (data) => { }, stream = false, } = {
|
|
4
|
+
onStderr: (data) => console.log("stderr:", data),
|
|
5
|
+
onError: (err) => { if (err)
|
|
6
|
+
throw err; },
|
|
7
|
+
}) {
|
|
8
|
+
let buffer = new Array();
|
|
9
|
+
//console.log("Cmd args:", args)
|
|
10
|
+
const useShell = platform() === 'win32';
|
|
11
|
+
const child = spawn(command, args, { shell: useShell });
|
|
12
|
+
child.stdout.setEncoding('utf8');
|
|
13
|
+
child.stdout.on('data', (data) => {
|
|
14
|
+
buffer.push(data);
|
|
15
|
+
onStdout(data);
|
|
16
|
+
});
|
|
17
|
+
child.stderr.setEncoding('utf8');
|
|
18
|
+
child.stderr.on('data', (data) => onStderr(data));
|
|
19
|
+
child.on("error", (data) => onError(data));
|
|
20
|
+
let finish;
|
|
21
|
+
let end = new Promise((r) => finish = r);
|
|
22
|
+
child.on('close', () => finish(true));
|
|
23
|
+
await end;
|
|
24
|
+
if (!stream) {
|
|
25
|
+
return buffer.join("\n");
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
return buffer.join("");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function runShellCmd(command, args = [], { onStdout = (data) => { }, onStderr = (data) => { }, onError = (data) => { }, onFinished = () => { }, } = {
|
|
32
|
+
onStdout: (data) => console.log("stdout:", data),
|
|
33
|
+
onStderr: (data) => console.log("stderr:", data),
|
|
34
|
+
onError: (err) => { if (err)
|
|
35
|
+
throw err; },
|
|
36
|
+
onFinished: () => { },
|
|
37
|
+
}) {
|
|
38
|
+
const useShell = platform() === 'win32';
|
|
39
|
+
var child = spawn(command, args, { shell: useShell });
|
|
40
|
+
child.stdout.setEncoding('utf8');
|
|
41
|
+
child.stdout.on('data', (data) => onStdout(data));
|
|
42
|
+
child.stderr.setEncoding('utf8');
|
|
43
|
+
child.stderr.on('data', (data) => onStderr(data));
|
|
44
|
+
child.on("error", (data) => onError(data));
|
|
45
|
+
child.on('close', () => onFinished());
|
|
46
|
+
return () => child.kill();
|
|
47
|
+
}
|
|
48
|
+
export { execute, runShellCmd };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { default as fs } from "fs";
|
|
2
|
+
import { access, constants } from 'fs/promises';
|
|
3
|
+
import { resolve } from 'path';
|
|
4
|
+
function readFile(fp) {
|
|
5
|
+
try {
|
|
6
|
+
return fs.readFileSync(fp, 'utf8');
|
|
7
|
+
}
|
|
8
|
+
catch (e) {
|
|
9
|
+
throw new Error(`reading file ${e}}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
async function checkIfFileExists(filePath) {
|
|
13
|
+
try {
|
|
14
|
+
await access(resolve(filePath), constants.F_OK);
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export { readFile, checkIfFileExists, };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { default as fs } from "fs";
|
|
2
|
+
import { default as path } from "path";
|
|
3
|
+
function readAgent(taskpath) {
|
|
4
|
+
if (!fs.existsSync(taskpath)) {
|
|
5
|
+
return { ymlAgent: "", found: false };
|
|
6
|
+
}
|
|
7
|
+
const data = fs.readFileSync(taskpath, 'utf8');
|
|
8
|
+
return { ymlAgent: data, found: true };
|
|
9
|
+
}
|
|
10
|
+
function readAgentsDir(dir) {
|
|
11
|
+
const tasks = new Array();
|
|
12
|
+
fs.readdirSync(dir).forEach((filename) => {
|
|
13
|
+
const filepath = path.join(dir, filename);
|
|
14
|
+
const isDir = fs.statSync(filepath).isDirectory();
|
|
15
|
+
if (!isDir) {
|
|
16
|
+
if (filename.endsWith(".yml")) {
|
|
17
|
+
tasks.push(filename);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
return tasks;
|
|
22
|
+
}
|
|
23
|
+
export { readAgent, readAgentsDir, };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { UserCmdDef } from "@agent-smith/types";
|
|
2
|
+
declare function readCmd(name: string, cmdPath: string): Promise<UserCmdDef | null>;
|
|
3
|
+
declare function readUserCmd(name: string, cmdPath: string): Promise<{
|
|
4
|
+
found: boolean;
|
|
5
|
+
userCmd: UserCmdDef;
|
|
6
|
+
}>;
|
|
7
|
+
export { readCmd, readUserCmd, };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { pathToFileURL } from 'url';
|
|
2
|
+
import { runtimeWarning } from "../user_msgs.js";
|
|
3
|
+
async function readCmd(name, cmdPath) {
|
|
4
|
+
const url = pathToFileURL(cmdPath).href;
|
|
5
|
+
let _cmd;
|
|
6
|
+
try {
|
|
7
|
+
const mod = await import(/* @vite-ignore */ url);
|
|
8
|
+
_cmd = mod.cmd;
|
|
9
|
+
}
|
|
10
|
+
catch (e) {
|
|
11
|
+
runtimeWarning(`command ${name} not found at ${cmdPath}, ${e}`);
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
if (!_cmd) {
|
|
15
|
+
throw new Error("no cmd");
|
|
16
|
+
}
|
|
17
|
+
return _cmd;
|
|
18
|
+
}
|
|
19
|
+
async function readUserCmd(name, cmdPath) {
|
|
20
|
+
const url = pathToFileURL(cmdPath).href;
|
|
21
|
+
try {
|
|
22
|
+
const mod = await import(/* @vite-ignore */ url);
|
|
23
|
+
const cmdMod = mod.cmd;
|
|
24
|
+
const uc = {
|
|
25
|
+
name: cmdMod.name,
|
|
26
|
+
description: cmdMod.description,
|
|
27
|
+
run: cmdMod.run,
|
|
28
|
+
options: cmdMod?.options ? cmdMod.options : undefined,
|
|
29
|
+
};
|
|
30
|
+
return { found: true, userCmd: uc };
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
runtimeWarning(`command ${name} not found at ${cmdPath}, ${e}`);
|
|
34
|
+
return { found: false, userCmd: { name: "", description: "", run: async (a, b) => null } };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export { readCmd, readUserCmd, };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { default as fs } from "fs";
|
|
2
|
+
import YAML from 'yaml';
|
|
3
|
+
function readConf(confPath) {
|
|
4
|
+
if (!fs.existsSync(confPath)) {
|
|
5
|
+
return { data: {}, found: false };
|
|
6
|
+
}
|
|
7
|
+
const file = fs.readFileSync(confPath, 'utf8');
|
|
8
|
+
const data = YAML.parse(file);
|
|
9
|
+
//console.log("READ CONF", data);
|
|
10
|
+
return { data: data, found: true };
|
|
11
|
+
}
|
|
12
|
+
export { readConf };
|