aboutsummaryrefslogtreecommitdiffstats
path: root/bots/todo-js
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-06-08 20:46:27 +0800
committerGitHub <noreply@github.com>2024-06-08 20:46:27 +0800
commit213e949fc8c2362046d197554fda98c87a7906df (patch)
tree595a883c6b1fd1154a916569c3d0255634c6e193 /bots/todo-js
parent82d7a7de8a76e56cff306b0da7f4f14fdb57cbf1 (diff)
downloadllm-functions-docker-213e949fc8c2362046d197554fda98c87a7906df.tar.gz
feat: support bots (#39)
Diffstat (limited to 'bots/todo-js')
l---------bots/todo-js/index.yaml1
-rw-r--r--bots/todo-js/tools.js73
2 files changed, 74 insertions, 0 deletions
diff --git a/bots/todo-js/index.yaml b/bots/todo-js/index.yaml
new file mode 120000
index 0000000..0d19c11
--- /dev/null
+++ b/bots/todo-js/index.yaml
@@ -0,0 +1 @@
+../todo-sh/index.yaml \ No newline at end of file
diff --git a/bots/todo-js/tools.js b/bots/todo-js/tools.js
new file mode 100644
index 0000000..ed3ed39
--- /dev/null
+++ b/bots/todo-js/tools.js
@@ -0,0 +1,73 @@
+const fs = require('fs');
+const path = require('path');
+
+/**
+ * Add a new todo item
+ * @typedef {Object} AddTodoArgs
+ * @property {string} desc - The task description
+ * @param {AddTodoArgs} args
+ */
+exports.add_todo = function addTodo(args) {
+ const todosFile = _getTodosFile();
+ if (fs.existsSync(todosFile)) {
+ const num = JSON.parse(fs.readFileSync(todosFile)).reduce((max, item) => Math.max(max, item.id), 0) + 1;
+ const data = fs.readFileSync(todosFile);
+ fs.writeFileSync(todosFile, JSON.stringify([...JSON.parse(data), { id: num, desc: args.desc }]));
+ console.log(`Successfully added todo id=${num}`);
+ } else {
+ fs.writeFileSync(todosFile, JSON.stringify([{ id: 1, desc: args.desc }]));
+ console.log('Successfully added todo id=1');
+ }
+}
+
+/**
+ * Delete an existing todo item
+ * @typedef {Object} DelTodoArgs
+ * @property {number} id - The task id
+ * @param {DelTodoArgs} args
+ */
+exports.del_todo = function delTodo(args) {
+ const todosFile = _getTodosFile();
+ if (fs.existsSync(todosFile)) {
+ const data = fs.readFileSync(todosFile);
+ const newData = JSON.parse(data).filter(item => item.id !== args.id);
+ fs.writeFileSync(todosFile, JSON.stringify(newData));
+ console.log(`Successfully deleted todo id=${args.id}`);
+ } else {
+ _die('Empty todo list');
+ }
+}
+
+/**
+ * Display the current todo list in json format.
+ */
+exports.list_todos = function listTodos() {
+ const todosFile = _getTodosFile();
+ if (fs.existsSync(todosFile)) {
+ console.log(fs.readFileSync(todosFile, "utf8"));
+ } else {
+ _die('Empty todo list');
+ }
+}
+
+/**
+ * Delete the entire todo list.
+ */
+exports.clear_todos = function clearTodos() {
+ const todosFile = _getTodosFile();
+ fs.unlinkSync(todosFile)
+ console.log("Successfully deleted entry todo list");
+}
+
+function _getTodosFile() {
+ const cacheDir = process.env.LLM_BOT_CACHE_DIR || '/tmp';
+ if (!fs.existsSync(cacheDir)) {
+ fs.mkdirSync(cacheDir, { recursive: true });
+ }
+ return path.join(cacheDir, 'todos.json');
+}
+
+function _die(message) {
+ console.error(message);
+ process.exit(1);
+} \ No newline at end of file