Skip to content

Commit

Permalink
refactor: improve bots/todo-* (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Jun 10, 2024
1 parent 8f3385c commit 7225ee3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 21 deletions.
9 changes: 2 additions & 7 deletions bots/todo-js/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports.del_todo = function delTodo(args) {
fs.writeFileSync(todosFile, JSON.stringify(newData));
console.log(`Successfully deleted todo id=${args.id}`);
} else {
_die('Empty todo list');
console.log('Empty todo list');
}
}

Expand All @@ -46,7 +46,7 @@ exports.list_todos = function listTodos() {
if (fs.existsSync(todosFile)) {
console.log(fs.readFileSync(todosFile, "utf8"));
} else {
_die('Empty todo list');
console.log("[]");
}
}

Expand All @@ -66,8 +66,3 @@ function _getTodosFile() {
}
return path.join(cacheDir, 'todos.json');
}

function _die(message) {
console.error(message);
process.exit(1);
}
9 changes: 3 additions & 6 deletions bots/todo-py/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def del_todo(id: int):
with open(todos_file, "r") as f:
data = json.load(f)
except (FileNotFoundError, JSONDecodeError):
_die("Empty todo list")
print("Empty todo list")
return
data = [item for item in data if item["id"] != id]
with open(todos_file, "w") as f:
json.dump(data, f)
Expand All @@ -46,7 +47,7 @@ def list_todos():
with open(todos_file, "r") as f:
print(f.read())
except FileNotFoundError:
_die("Empty todo list")
print("[]")


def clear_todos():
Expand All @@ -59,7 +60,3 @@ def _get_todos_file() -> str:
if not os.path.exists(cache_dir):
os.makedirs(cache_dir, exist_ok=True)
return os.path.join(cache_dir, "todos.json")

def _die(msg: str):
print(msg, file=sys.stderr)
exit(1)
2 changes: 1 addition & 1 deletion bots/todo-sh/index.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Todo List
name: TodoBot
description: Your name is TodoBot and you are a helpful chatbot that manages a todo list.
instructions: |
You will be provided with a list of todos.
Expand Down
9 changes: 2 additions & 7 deletions bots/todo-sh/tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ del_todo() {
> "$todos_file"
echo "Successfully deleted todo id=$argc_id"
else
_die "Empty todo list"
echo "Empty todo list"
fi
}

Expand All @@ -41,7 +41,7 @@ list_todos() {
if [[ -f "$todos_file" ]]; then
cat "$todos_file"
else
_die "Empty todo list"
echo '[]'
fi
}

Expand All @@ -63,10 +63,5 @@ _get_todos_file() {
echo "${LLM_BOT_CACHE_DIR:-/tmp}/todos.json"
}

_die() {
echo "$*" >&2
exit 1
}

# See more details at https://github.com/sigoden/argc
eval "$(argc --argc-eval "$0" "$@")"

0 comments on commit 7225ee3

Please sign in to comment.