diff --git a/.github/workflows/build-docker-image.yml b/.github/workflows/build-docker-image.yml index 7fbe1dcb..88297261 100644 --- a/.github/workflows/build-docker-image.yml +++ b/.github/workflows/build-docker-image.yml @@ -473,9 +473,12 @@ jobs: cache-to: type=registry,ref=whyour/qinglong:cache-debian-python3.10,mode=max publish: - if: ${{ github.ref_name == 'master' }} + if: ${{ github.repository == 'whyour/qinglong' && github.ref == 'refs/heads/master' }} needs: [build-alpine, build-debian] runs-on: ubuntu-latest + permissions: + contents: read + id-token: write steps: - uses: actions/checkout@v4 - uses: pnpm/action-setup@v3 @@ -493,7 +496,12 @@ jobs: pnpm build:front pnpm build:back - - name: publish npm package - run: | - echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" >> ~/.npmrc - npm publish + - name: Set up Node for trusted publishing + uses: actions/setup-node@v6 + with: + node-version: '24' + registry-url: https://registry.npmjs.org + package-manager-cache: false + + - name: Publish npm package with OIDC + run: npm publish --access public --registry=https://registry.npmjs.org diff --git a/.github/workflows/cli-package.yml b/.github/workflows/cli-package.yml new file mode 100644 index 00000000..17fe9bda --- /dev/null +++ b/.github/workflows/cli-package.yml @@ -0,0 +1,115 @@ +name: CLI package + +on: + pull_request: + paths: + - 'cli/**' + - 'shell/**' + - 'back/api/**' + - 'back/loaders/deps.ts' + - 'package.json' + - '.github/workflows/cli-package.yml' + push: + branches: [develop, master] + paths: + - 'cli/**' + - 'shell/**' + - 'back/api/**' + - 'back/loaders/deps.ts' + - 'package.json' + - '.github/workflows/cli-package.yml' + workflow_dispatch: + inputs: + publish: + description: Publish the verified CLI package (master only) + type: boolean + default: false + +permissions: + contents: read + +concurrency: + group: cli-package-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.ref != 'refs/heads/master' }} + +jobs: + package: + runs-on: ubuntu-latest + timeout-minutes: 15 + strategy: + fail-fast: false + matrix: + node: ['22.12.0', '24'] + steps: + - uses: actions/checkout@v6 + - uses: actions/setup-node@v6 + with: + node-version: ${{ matrix.node }} + cache: npm + cache-dependency-path: cli/package-lock.json + - name: Install test interpreters and tools + run: | + sudo apt-get update + sudo apt-get install -y --no-install-recommends bash ca-certificates curl git jq perl procps python3 unzip zip + npm install --global --ignore-scripts --no-audit --no-fund ts-node@10.9.2 typescript@5.2.2 + - run: npm ci --prefix cli --no-audit --no-fund + - run: npm run check:cli + - run: npm run test:cli + - name: Verify offline package installation + env: + QL_CLI_PACKAGE_OUTPUT: ${{ runner.temp }}/cli-package + run: node cli/scripts/verify-package.cjs + - name: Upload verified npm archive + if: matrix.node == '24' + uses: actions/upload-artifact@v6 + with: + name: qinglong-cli-${{ github.sha }} + path: ${{ runner.temp }}/cli-package/*.tgz + if-no-files-found: error + retention-days: 14 + + publish: + needs: package + if: >- + github.repository == 'whyour/qinglong' && + github.ref == 'refs/heads/master' && + (github.event_name == 'push' || + (github.event_name == 'workflow_dispatch' && inputs.publish)) + runs-on: ubuntu-latest + timeout-minutes: 5 + permissions: + contents: read + id-token: write + steps: + - uses: actions/setup-node@v6 + with: + node-version: '24' + package-manager-cache: false + registry-url: https://registry.npmjs.org + - uses: actions/download-artifact@v7 + with: + name: qinglong-cli-${{ github.sha }} + path: cli-package + - name: Publish verified npm archive + shell: bash + run: | + set -euo pipefail + shopt -s nullglob + archives=(cli-package/*.tgz) + if [ "${#archives[@]}" -ne 1 ]; then + echo '::error::Expected exactly one verified CLI archive.' + exit 1 + fi + archive="${archives[0]}" + metadata=$(tar -xOf "$archive" package/package.json) + name=$(jq -er '.name | select(. == "@whyour/qinglong-cli")' <<< "$metadata") + version=$(jq -er '.version | select(type == "string" and test("^[0-9]+\\.[0-9]+\\.[0-9]+$"))' <<< "$metadata") + if npm view "$name@$version" version --json --registry=https://registry.npmjs.org > version.json 2> version-error.log; then + jq -e --arg version "$version" '. == $version' version.json > /dev/null + echo "::notice::$name@$version is already published; bump cli/package.json and its lockfile to release changes." + exit 0 + elif ! jq -e '.error.code == "E404"' version.json > /dev/null; then + echo '::error::Could not check the published CLI version; refusing to publish.' + exit 1 + fi + npm publish "./$archive" --access public --tag latest --ignore-scripts --registry=https://registry.npmjs.org diff --git a/README-en.md b/README-en.md index 4570d6d5..581661c9 100644 --- a/README-en.md +++ b/README-en.md @@ -34,6 +34,7 @@ Timed task management platform supporting Python3, JavaScript, Shell, Typescript - Support system level notification - Support dark mode - Support cell phone operation +- Manage panels through the [remote CLI and Agent Skill](#remote-cli-and-agent-skill) ## Version @@ -79,6 +80,29 @@ npm i @whyour/qinglong [View Documentation](https://qinglong.online/guide/user-guide/basic-explanation) +## Remote CLI and Agent Skill + +Use the standalone npm package [@whyour/qinglong-cli](https://www.npmjs.com/package/@whyour/qinglong-cli) to manage a remote QingLong panel from your terminal or automation workflows. Create, update and run tasks and subscriptions, inspect logs, and manage applications, environment variables, scripts, configuration, dependencies and other OpenAPI resources. JSON output supports scripts and AI agents. + +Requires Node.js >=22.12; Node.js 24 is recommended. Install the package to use `ql`: + +```bash +npm install -g @whyour/qinglong-cli +ql login --url https://ql.example.com +ql auth status --json +ql task list --json +ql subscription list --json +ql --help +``` + +Create an application in the panel's System Settings → Application Settings and grant the required permissions. Enter its Client ID/Secret when prompted during login. Existing tokens are also supported through `QL_URL` and `QL_ACCESS_TOKEN`. Each command supports `--help`; set `QL_LANG=en` for English help. + +The npm CLI manages remote panels. The panel's built-in executable is also named `ql`; use a separate installation directory on panel hosts to avoid replacing it. For local script execution and maintenance, see [panel-internal tools](cli/LOCAL.en.md). + +[CLI guide](cli/README.en.md) · [简体中文](cli/README.md) · [Full command reference](cli/skills/qinglong-cli/references/openapi.md) + +The package includes a [remote management skill](cli/skills/qinglong-cli/SKILL.md) covering authentication, command selection and result verification for AI agents. Local execution and maintenance have a separate [internal skill](cli/skills/qinglong-local/SKILL.md). + ## Development ```bash diff --git a/README.md b/README.md index 9b7cdd55..b6d34f94 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ Timed task management platform supporting Python3, JavaScript, Shell, Typescript - 支持系统级通知 - 支持暗黑模式 - 支持手机端操作 +- 支持通过 [远程 CLI 与 Agent Skill](#远程-cli-与-agent-skill) 管理面板 ## 版本 @@ -81,6 +82,29 @@ npm i @whyour/qinglong [查看文档](https://qinglong.online/guide/user-guide/basic-explanation) +## 远程 CLI 与 Agent Skill + +使用独立 npm 包 [@whyour/qinglong-cli](https://www.npmjs.com/package/@whyour/qinglong-cli),可以从本机终端或自动化流程管理远程青龙面板。支持任务与订阅的创建、修改、运行和日志查看,以及应用、环境变量、脚本、配置、依赖等 OpenAPI 资源;JSON 输出便于脚本和 AI Agent 使用。 + +需要 Node.js >=22.12,推荐 Node.js 24。安装后使用 `ql` 命令: + +```bash +npm install -g @whyour/qinglong-cli +ql login --url https://ql.example.com +ql auth status --json +ql task list --json +ql subscription list --json +ql --help +``` + +在面板「系统设置 → 应用设置」创建应用并授予所需权限,登录时按提示输入 Client ID/Secret;也支持通过 `QL_URL` 和 `QL_ACCESS_TOKEN` 使用已有令牌。各命令支持 `--help`,`QL_LANG=en` 可切换英文帮助。 + +npm CLI 用于远程管理。面板内置 `ql` 同样使用这个命令名,已有面板的机器请使用独立安装目录,避免覆盖内置命令。本机脚本执行和运维见 [面板内部工具](cli/LOCAL.md)。 + +[CLI 中文说明](cli/README.md) · [English guide](cli/README.en.md) · [完整命令参考](cli/skills/qinglong-cli/references/openapi.md) + +包内附带 [远程管理 Skill](cli/skills/qinglong-cli/SKILL.md),涵盖认证、命令选择和操作结果检查,可供 AI Agent 使用。本机执行与运维另见 [内部 Skill](cli/skills/qinglong-local/SKILL.md)。 + ## 开发 ```bash diff --git a/back/loaders/deps.ts b/back/loaders/deps.ts index d9413f4b..59fd5b21 100644 --- a/back/loaders/deps.ts +++ b/back/loaders/deps.ts @@ -17,6 +17,50 @@ async function linkCommand() { } async function linkCommandToDir(commandDir: string) { + const cliRoot = process.env.QL_CLI_ROOT; + if (cliRoot) { + if (!path.isAbsolute(cliRoot)) { + throw new Error('QL_CLI_ROOT must be an absolute CLI installation path'); + } + const { installCliEntrypoints } = require(path.join( + cliRoot, + 'dist/local/entrypoints.js', + )); + await installCliEntrypoints(commandDir, cliRoot); + const { installCronEntrypoint } = require(path.join( + cliRoot, + 'dist/local/cronEntrypoint.js', + )); + await installCronEntrypoint(commandDir, cliRoot, config.crontabFile, { + ...process.env, + QL_DIR: config.rootPath, + QL_DATA_DIR: config.dataPath, + }); + // Container-wide legacy links may precede ~/bin. Ensure panel children use + // the explicitly selected entries without changing system-wide links. + process.env.PATH = [ + commandDir, + ...(process.env.PATH?.split(path.delimiter) ?? []).filter( + (entry) => entry !== commandDir, + ), + ].join(path.delimiter); + return; + } + + const cronBridge = path.join(commandDir, 'crontab'); + try { + if ( + (await fs.lstat(cronBridge)).isFile() && + (await fs.readFile(cronBridge, 'utf8')).includes( + '// QingLong CLI crontab bridge', + ) + ) { + await fs.unlink(cronBridge); + } + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; + } + const linkShell = [ { src: 'update.sh', @@ -39,7 +83,7 @@ async function linkCommandToDir(commandDir: string) { if (stats) { await fs.unlink(tmpTarget); } - } catch (error) { } + } catch (error) {} await fs.symlink(source, tmpTarget); await fs.rename(tmpTarget, target); diff --git a/cli/.gitignore b/cli/.gitignore new file mode 100644 index 00000000..16fd4ff8 --- /dev/null +++ b/cli/.gitignore @@ -0,0 +1,2 @@ +/dist/ +node_modules diff --git a/cli/LICENSE b/cli/LICENSE new file mode 100644 index 00000000..dde8d7b4 --- /dev/null +++ b/cli/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright 2021 WHYOUR . + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. \ No newline at end of file diff --git a/cli/LOCAL.en.md b/cli/LOCAL.en.md new file mode 100644 index 00000000..0b7c822d --- /dev/null +++ b/cli/LOCAL.en.md @@ -0,0 +1,45 @@ +# Panel-internal TypeScript tools + +[简体中文](LOCAL.md) | **English** + + +The npm and panel-internal entries both use the name `ql`, but have separate Commander command trees. The npm entry only calls remote APIs; the internal entry only runs local tools. Verify the absolute executable path and `--help` before use. Installing the npm package does not migrate the built-in Shell commands. + +These tools ship with panel builds and are excluded from the @whyour/qinglong-cli npm package. Node >=22.12 is required; user configuration/hooks remain Bash and scripts need their interpreters. Development publishing was removed from the CLI; shell/pub.sh remains available to release workflows. + +```sh +npm ci --prefix cli +npm run build:cli +node cli/dist/ql.js --help +node cli/dist/ql.js task exec --root /ql demo.js now +node cli/dist/ql.js reload --root /ql +``` + +Run on the actual panel host or inside its container. For Docker use `docker exec ...`. A workstation npm CLI cannot reset a remote account; a mounted data directory alone is not the complete operational environment. + +| Capability | Internal commands | +| --- | --- | +| Execution/inventory | `ql task exec [options] [script]`, with `ql task