From 739a832d87c00e3b5977a24bba5654fa5ea7a702 Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 7 Jun 2024 15:16:31 +0800 Subject: feat: js/py generate declarations from comments (#30) --- tools/demo_tool.js | 16 ++++++++++++++++ tools/demo_tool.py | 32 ++++++++++++++++++++++++++++++++ tools/demo_tool.sh | 15 +++++++++++++++ tools/may_execute_js_code.js | 29 ++++++++--------------------- tools/may_execute_py_code.py | 27 ++++++--------------------- 5 files changed, 77 insertions(+), 42 deletions(-) create mode 100644 tools/demo_tool.js create mode 100644 tools/demo_tool.py create mode 100755 tools/demo_tool.sh (limited to 'tools') diff --git a/tools/demo_tool.js b/tools/demo_tool.js new file mode 100644 index 0000000..f8fd283 --- /dev/null +++ b/tools/demo_tool.js @@ -0,0 +1,16 @@ +/** + * Demonstrate how to create a tool using Javascript and how to use comments. + * @typedef {Object} Args + * @property {string} string - Define a required string property + * @property {'foo'|'bar'} string_enum - Define a required string property with enum + * @property {string} [string_optional] - Define a optional string property + * @property {boolean} boolean - Define a required boolean property + * @property {Integer} integer - Define a required integer property + * @property {number} number - Define a required number property + * @property {string[]} array - Define a required string array property + * @property {string[]} [array_optional] - Define a optional string array property + * @param {Args} args + */ +exports.main = function main(args) { + console.log(args); +} diff --git a/tools/demo_tool.py b/tools/demo_tool.py new file mode 100644 index 0000000..bd353ad --- /dev/null +++ b/tools/demo_tool.py @@ -0,0 +1,32 @@ +from typing import List, Literal, Optional + + +def main( + boolean: bool, + string: str, + string_enum: Literal["foo", "bar"], + integer: int, + number: float, + array: List[str], + string_optional: Optional[str] = None, + array_optional: Optional[List[str]] = None, +) -> None: + """Demonstrate how to create a tool using Python and how to use comments. + Args: + boolean: Define a required boolean property + string: Define a required string property + string_enum: Define a required string property with enum + integer: Define a required integer property + number: Define a required number property + array: Define a required string array property + string_optional: Define a optional string property + array_optional: Define a optional string array property + """ + print(f"boolean: {boolean}") + print(f"string: {string}") + print(f"string_enum: {string_enum}") + print(f"integer: {integer}") + print(f"number: {number}") + print(f"array: {array}") + print(f"string_optional: {string_optional}") + print(f"array_optional: {array_optional}") diff --git a/tools/demo_tool.sh b/tools/demo_tool.sh new file mode 100755 index 0000000..0d24c75 --- /dev/null +++ b/tools/demo_tool.sh @@ -0,0 +1,15 @@ +# @describe Demonstrate how to create a tool using Bash and how to use comment tags. +# @option --string! Define a required string property +# @option --string-enum![foo|bar] Define a required string property with enum +# @option --string-optional Define a optional string property +# @flag --boolean Define a boolean property +# @option --integer! Define a required integer property +# @option --number! Define a required number property +# @option --array+ Define a required string array property +# @option --array-optional* Define a optional string array property + +main() { + ( set -o posix ; set ) | grep ^argc_ # inspect all argc variables +} + +eval "$(argc --argc-eval "$0" "$@")" \ No newline at end of file diff --git a/tools/may_execute_js_code.js b/tools/may_execute_js_code.js index 9575582..2dec177 100644 --- a/tools/may_execute_js_code.js +++ b/tools/may_execute_js_code.js @@ -1,22 +1,9 @@ -exports.declarate = function declarate() { - return { - "name": "may_execute_js_code", - "description": "Runs the javascript code in node.js.", - "parameters": { - "type": "object", - "properties": { - "code": { - "type": "string", - "description": "Javascript code to execute, such as `console.log(\"hello world\")`" - } - }, - "required": [ - "code" - ] - } - } -} - -exports.execute = function execute(data) { - eval(data.code) +/** + * Runs the javascript code in node.js. + * @typedef {Object} Args + * @property {string} code - Javascript code to execute, such as `console.log("hello world")` + * @param {Args} args + */ +exports.main = function main({ code }) { + eval(code); } diff --git a/tools/may_execute_py_code.py b/tools/may_execute_py_code.py index 01a59b9..8e55a8d 100644 --- a/tools/may_execute_py_code.py +++ b/tools/may_execute_py_code.py @@ -1,21 +1,6 @@ -def declarate(): - return { - "name": "may_execute_py_code", - "description": "Runs the python code.", - "parameters": { - "type": "object", - "properties": { - "code": { - "type": "string", - "description": "Python code to execute, such as `print(\"hello world\")`" - } - }, - "required": [ - "code" - ] - } - } - - -def execute(data): - exec(data["code"]) +def main(code: str): + """Runs the python code. + Args: + code: Python code to execute, such as `print("hello world")` + """ + exec(code) -- cgit v1.2.3