@ohos-cpf/3rdloop 0.0.7 → 0.0.9
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 +44 -25
- package/lib/cli.js +54 -29
- package/lib/config-cmd.js +1 -1
- package/lib/config.js +32 -0
- package/lib/doctor.js +266 -48
- package/lib/mcp.js +498 -0
- package/lib/opencode.js +75 -29
- package/lib/orch.js +32 -10
- package/lib/runner.js +8 -5
- package/lib/serve.js +162 -149
- package/lib/step.js +15 -4
- package/lib/web.js +107 -265
- package/lib/workflow.js +18 -8
- package/package.json +6 -2
- package/vendor/Archive/data/.index/meta.json +7 -0
- package/vendor/Archive/data/.index/search.db +0 -0
- package/vendor/Archive/data/docs/experiences/arkts-check-tool-pitfalls-baseline-comparison.md +13 -0
- package/vendor/Archive/data/docs/experiences/arkts-deep-path-import-recognition.md +31 -0
- package/vendor/Archive/data/docs/experiences/arkts-demo-coverage-entry-file-case-mismatch.md +25 -0
- package/vendor/Archive/data/docs/experiences/arkts-demo-coverage-import-trailing-slash.md +20 -0
- package/vendor/Archive/data/docs/experiences/arkts-library-source-files-may-be-ts.md +18 -0
- package/vendor/Archive/data/docs/experiences/arkts-typedarray-buffer-interface-cast-compile-fixes.md +36 -0
- package/vendor/Archive/data/docs/experiences/arkts-xts-compile-error-clustering-fix.md +24 -0
- package/vendor/Archive/data/docs/experiences/arkts-xts-doc-expected-value-runtime-probe.md +35 -0
- package/vendor/Archive/data/docs/experiences/arkts-xts-fixture-rawfile-testability-injection.md +33 -0
- package/vendor/Archive/data/docs/experiences/code-linter-chinese-comment-false-positive.md +16 -0
- package/vendor/Archive/data/docs/experiences/esobject-new-expression-variable-tracking.md +33 -0
- package/vendor/Archive/data/docs/experiences/filesystem-case-sensitivity-module-resolution.md +31 -0
- package/vendor/Archive/data/docs/experiences/gitcode-clone-redirect-warning-normal.md +13 -0
- package/vendor/Archive/data/docs/experiences/kn_msgvq1w585ec8319.md +77 -0
- package/vendor/Archive/data/docs/experiences/kn_msovnm6rd46de72c.md +98 -0
- package/vendor/Archive/data/docs/experiences/kn_mspt8vntfe708576.md +87 -0
- package/vendor/Archive/data/docs/experiences/kn_mspu1ei77adbbb48.md +133 -0
- package/vendor/Archive/data/docs/experiences/kn_mspubgak8d30bfce.md +128 -0
- package/vendor/Archive/data/docs/experiences/kn_mspug2de3cb1f7c3.md +115 -0
- package/vendor/Archive/data/docs/experiences/kn_skill_opt_001.md +38 -0
- package/vendor/Archive/data/docs/experiences/loop-artifacts-verification-evidence-triangle.md +19 -0
- package/vendor/Archive/data/docs/experiences/macos-bash-replace-build-ps1.md +19 -0
- package/vendor/Archive/data/docs/experiences/macos-bsd-diff-q-illegal-option.md +15 -0
- package/vendor/Archive/data/docs/experiences/migrate-cjs-compatible-api-version-map-limit.md +17 -0
- package/vendor/Archive/data/docs/experiences/mp3agic-arkts-library-behavior-quirks.md +21 -0
- package/vendor/Archive/data/docs/experiences/ohos-face-detection-scan-api-mapping.md +15 -0
- package/vendor/Archive/data/docs/experiences/pr-metadata-and-diff-integrity-verification.md +12 -0
- package/vendor/Archive/data/docs/experiences/rn-coverage-index-signature-pseudo-member-false-negative.md +32 -0
- package/vendor/Archive/data/docs/experiences/rn-memory-check-severity-calibration.md +21 -0
- package/vendor/Archive/data/docs/experiences/rn-platform-specific-api-exclusion-evidence.md +24 -0
- package/vendor/Archive/data/docs/experiences/rn-upstream-version-diff-pitfalls.md +17 -0
- package/vendor/Archive/data/docs/experiences/rn-version-gap-risk-rating-rules.md +13 -0
- package/vendor/Archive/data/docs/experiences/rn-view-lib-native-contract-diff-fabric-spec.md +16 -0
- package/vendor/Archive/data/docs/experiences/rnoh-adapter-package-version-field-semantics.md +18 -0
- package/vendor/Archive/data/docs/experiences/rnoh-arkts-sqlite-rows-array-format.md +18 -0
- package/vendor/Archive/data/docs/experiences/rnoh-dependency-residue-scan-strategy.md +18 -0
- package/vendor/Archive/data/docs/experiences/rnoh-example-local-lib-dep-use-tgz.md +53 -0
- package/vendor/Archive/data/docs/experiences/rnoh-example-prereserve-rntesterlist.md +17 -0
- package/vendor/Archive/data/docs/experiences/rnoh-example-template-compatiblesdkversion-upgrade.md +29 -0
- package/vendor/Archive/data/docs/experiences/rnoh-example-turbomodule-shell-adaptation-checklist.md +41 -0
- package/vendor/Archive/data/docs/experiences/rnoh-mcp-build-project-tool-risk.md +18 -0
- package/vendor/Archive/data/docs/experiences/rnoh-npm-devdependencies-version-precheck.md +29 -0
- package/vendor/Archive/data/docs/experiences/rnoh-repo-baseline-version-verification.md +13 -0
- package/vendor/Archive/data/docs/experiences/rnoh-repo-upstream-directory-mapping.md +18 -0
- package/vendor/Archive/data/docs/experiences/rnoh-stateless-library-zero-leak-verification.md +31 -0
- package/vendor/Archive/data/docs/experiences/rnoh-template-npmrc-credential-removal.md +15 -0
- package/vendor/Archive/data/docs/experiences/rnoh-turbomodule-codegen-wiring-verification.md +19 -0
- package/vendor/Archive/data/docs/experiences/rnoh-turbomodule-cpp-methodmap-compatibility-check.md +20 -0
- package/vendor/Archive/data/docs/experiences/scenario-doc-vs-source-signature-diff-four-patterns.md +24 -0
- package/vendor/Archive/data/docs/experiences/static-analysis-grep-cross-validation.md +29 -0
- package/vendor/Archive/demo.js +489 -0
- package/vendor/Archive/index.js +43 -0
- package/vendor/Archive/package.json +16 -0
- package/vendor/Archive/src/constants.js +83 -0
- package/vendor/Archive/src/data/stopwords.txt +110 -0
- package/vendor/Archive/src/data/synonyms.json +25 -0
- package/vendor/Archive/src/data/terms.txt +59 -0
- package/vendor/Archive/src/db/sqlite-backend.js +312 -0
- package/vendor/Archive/src/db/sqlite-schema.js +64 -0
- package/vendor/Archive/src/engine.js +501 -0
- package/vendor/Archive/src/index/index-manager.js +203 -0
- package/vendor/Archive/src/index/index-state.js +94 -0
- package/vendor/Archive/src/index/search-text-builder.js +106 -0
- package/vendor/Archive/src/index.js +22 -0
- package/vendor/Archive/src/parser/api-identifiers.js +121 -0
- package/vendor/Archive/src/parser/api-suffixes.js +48 -0
- package/vendor/Archive/src/parser/heading-normalize.js +40 -0
- package/vendor/Archive/src/parser/markdown-splitter.js +215 -0
- package/vendor/Archive/src/search/catalog-routing.js +87 -0
- package/vendor/Archive/src/search/fts-search.js +289 -0
- package/vendor/Archive/src/search/query-normalizer.js +159 -0
- package/vendor/Archive/src/search/query-rewriter.js +72 -0
- package/vendor/Archive/src/search/search-engine.js +72 -0
- package/vendor/Archive/src/search/snippet.js +64 -0
- package/vendor/Archive/src/store/dir-store.js +103 -0
- package/vendor/Archive/src/store/doc-store.js +36 -0
- package/vendor/Archive/src/tokenizer/lexicon.js +151 -0
- package/vendor/Archive/src/tokenizer/tokenizer.js +167 -0
- package/vendor/Archive/src/types.js +86 -0
- package/vendor/Archive/src/utils/hash.js +16 -0
- package/vendor/Archive/src/utils/lock.js +73 -0
- package/vendor/Archive/src/utils/path-safety.js +96 -0
- package/vendor/Archive/src/utils/paths.js +39 -0
- package/vendor/BadCase/rn/INDEX.md +127 -0
- package/vendor/BadCase/rn/cases/KI-001-webview/350/247/206/351/242/221/346/227/240/346/263/225/346/250/252/345/261/217.md +52 -0
- package/vendor/BadCase/rn/cases/KI-002-fastImage/347/203/255/346/233/264/346/226/260/345/233/276/347/211/207/345/244/261/350/264/245.md +37 -0
- package/vendor/BadCase/rn/cases/KI-003-wechat/347/274/251/347/225/245/345/233/276/346/234/254/345/234/260/345/233/276/347/211/207.md +62 -0
- package/vendor/BadCase/rn/cases/KI-004-wechat/345/233/236/350/260/203/346/227/266/345/272/217.md +47 -0
- package/vendor/BadCase/rn/cases/KI-005-geolocation/345/257/274/345/205/245/350/267/257/345/276/204/345/220/216/347/274/200.md +31 -0
- package/vendor/BadCase/rn/cases/KI-006-SmartRefreshLayout/346/231/272/350/203/275/346/214/207/351/222/210/345/264/251/346/272/203.md +55 -0
- package/vendor/BadCase/rn/cases/KI-007-orientation-locker/344/274/240/346/204/237/345/231/250/345/210/235/345/247/213/345/214/226.md +58 -0
- package/vendor/BadCase/rn/cases/KI-008-orientation-locker/344/274/240/346/204/237/345/231/250/345/274/225/347/224/250/350/256/241/346/225/260.md +47 -0
- package/vendor/BadCase/rn/cases/KI-009-pager-view/345/265/214/345/245/227/346/273/221/345/212/250.md +38 -0
- package/vendor/BadCase/rn/cases/KI-010-screens/346/211/213/345/212/277/345/206/262/347/252/201.md +48 -0
- package/vendor/BadCase/rn/cases/KI-011-spring-scrollview/346/211/213/345/212/277/351/207/215/345/244/215/347/273/221/345/256/232.md +49 -0
- package/vendor/BadCase/rn/cases/KI-012-qr-camera/346/250/252/345/261/217/351/200/202/351/205/215.md +59 -0
- package/vendor/BadCase/rn/cases/KI-013-webview/346/267/261/350/211/262/346/250/241/345/274/217/350/203/214/346/231/257/350/211/262.md +37 -0
- package/vendor/BadCase/rn/cases/KI-014-amap-geolocation/345/233/236/350/260/203/347/274/272/345/244/261.md +42 -0
- package/vendor/BadCase/rn/cases/KI-015-reanimated/350/277/220/350/241/214/346/227/266/346/236/220/346/236/204/345/264/251/346/272/203.md +54 -0
- package/vendor/BadCase/rn/cases/KI-016-fs/346/226/207/344/273/266/350/257/273/345/217/226/347/274/226/347/240/201.md +58 -0
- package/vendor/BadCase/rn/cases/KI-017-blob-util/345/217/202/346/225/260/347/274/226/347/240/201/351/224/231/350/257/257.md +39 -0
- package/vendor/BadCase/rn/cases/KI-018-modal/345/274/271/347/252/227/351/227/252/347/203/201.md +53 -0
- package/vendor/BadCase/rn/cases/KI-019-code-push/351/207/215/345/220/257/351/200/273/350/276/221/345/244/261/346/225/210.md +79 -0
- package/vendor/BadCase/rn/cases/KI-020-netinfo/346/225/217/346/204/237/344/277/241/346/201/257/346/263/204/351/234/262.md +60 -0
- package/vendor/BadCase/rn/cases/KI-021-video/346/234/254/345/234/260/350/247/206/351/242/221/346/222/255/346/224/276/345/274/202/345/270/270.md +79 -0
- package/vendor/BadCase/rn/cases/KI-022-audio-recorder-player/346/236/232/344/270/276/347/274/272/345/244/261.md +38 -0
- package/vendor/BadCase/rn/cases/KI-023-svg/346/270/220/345/217/230/347/273/247/346/211/277/345/244/261/346/225/210.md +62 -0
- package/vendor/BadCase/rn/generalization-log.md +55 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-SmartRefreshLayout-32.json +328 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-SmartRefreshLayout-32.md +382 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-amap-geolocation-27.json +1351 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-amap-geolocation-27.md +1176 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-audio-recorder-player-22.json +538 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-audio-recorder-player-22.md +1488 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-blob-util-23.json +211 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-blob-util-23.md +35 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-code-push-17.json +2048 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-code-push-17.md +3866 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-fast-image-102.json +198 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-fast-image-102.md +40 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-fs-38.json +751 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-fs-38.md +883 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-geolocation-24.json +1544 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-geolocation-24.md +2598 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-modal-20.json +246 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-modal-20.md +554 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-netinfo-31.json +211 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-netinfo-31.md +54 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-orientation-locker-45.json +1626 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-orientation-locker-45.md +2213 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-orientation-locker-51.json +208 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-orientation-locker-51.md +77 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-pager-view-32.json +377 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-pager-view-32.md +174 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-qr-decode-image-camera-37.json +232 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-qr-decode-image-camera-37.md +194 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-reanimated-82.json +198 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-reanimated-82.md +67 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-screens-87.json +198 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-screens-87.md +38 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-screens-93.json +208 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-screens-93.md +34 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-spring-scrollview-38.json +222 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-spring-scrollview-38.md +56 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-svg-117.json +463 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-svg-117.md +383 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-video-66.json +464 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-video-66.md +565 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-webview-112.json +198 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-webview-112.md +47 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-webview-121.json +232 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-webview-121.md +422 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-wechat-lib-15.json +295 -0
- package/vendor/BadCase/rn/pr-info/rntpc_react-native-wechat-lib-15.md +1038 -0
- package/vendor/BadCase/rn/rules/R-01-/345/205/250/345/261/217/347/252/227/345/217/243/347/212/266/346/200/201/347/256/241/347/220/206/345/256/214/346/225/264/346/200/247.md +20 -0
- package/vendor/BadCase/rn/rules/R-02-/350/265/204/346/272/220URI/345/275/242/346/200/201/345/210/206/346/224/257/350/246/206/347/233/226.md +21 -0
- package/vendor/BadCase/rn/rules/R-03-/351/231/204/345/261/236/346/223/215/344/275/234/345/244/261/350/264/245/351/230/273/346/226/255/344/270/273/346/265/201/347/250/213.md +20 -0
- package/vendor/BadCase/rn/rules/R-04-/345/274/202/346/255/245/345/233/236/350/260/203/346/263/250/345/206/214/346/227/266/345/272/217.md +20 -0
- package/vendor/BadCase/rn/rules/R-05-/346/250/241/345/235/227/345/257/274/345/205/245/350/267/257/345/276/204/350/247/204/350/214/203.md +19 -0
- package/vendor/BadCase/rn/rules/R-06-/344/274/240/346/204/237/345/231/250/347/241/254/344/273/266/346/235/203/351/231/220/346/214/211/351/234/200/350/216/267/345/217/226.md +21 -0
- package/vendor/BadCase/rn/rules/R-07-/346/231/272/350/203/275/346/214/207/351/222/210/347/224/237/345/221/275/345/221/250/346/234/237/347/256/241/347/220/206.md +20 -0
- package/vendor/BadCase/rn/rules/R-08-/346/211/213/345/212/277/344/272/213/344/273/266/346/263/250/345/206/214/344/270/216/345/206/262/347/252/201/345/244/204/347/220/206.md +22 -0
- package/vendor/BadCase/rn/rules/R-09-/346/250/252/347/253/226/345/261/217/345/244/232/345/275/242/346/200/201/345/270/203/345/261/200/351/200/202/351/205/215.md +20 -0
- package/vendor/BadCase/rn/rules/R-10-/346/267/261/350/211/262/346/250/241/345/274/217/344/270/273/351/242/230/351/242/234/350/211/262/351/200/202/351/205/215.md +19 -0
- package/vendor/BadCase/rn/rules/R-11-/346/216/245/345/217/243/345/233/236/350/260/203/345/256/236/347/216/260/345/256/214/346/225/264/346/200/247.md +20 -0
- package/vendor/BadCase/rn/rules/R-12-/345/216/237/347/224/237/350/277/220/350/241/214/346/227/266/350/265/204/346/272/220/346/236/220/346/236/204/346/227/266/345/272/217.md +20 -0
- package/vendor/BadCase/rn/rules/R-13-/346/226/207/344/273/266/346/225/260/346/215/256/347/274/226/347/240/201/344/270/216/346/240/274/345/274/217/345/205/274/345/256/271.md +21 -0
- package/vendor/BadCase/rn/rules/R-14-/345/212/250/347/224/273/345/210/235/345/247/213/345/214/226/346/227/266/345/272/217/344/270/216/347/212/266/346/200/201/345/220/214/346/255/245.md +20 -0
- package/vendor/BadCase/rn/rules/R-15-/347/212/266/346/200/201/346/214/201/344/271/205/345/214/226/344/270/216/351/207/215/345/220/257/351/200/273/350/276/221/344/270/200/350/207/264/346/200/247.md +20 -0
- package/vendor/BadCase/rn/rules/R-16-/346/225/217/346/204/237/344/277/241/346/201/257/346/227/245/345/277/227/346/263/204/351/234/262.md +19 -0
- package/vendor/BadCase/rn/rules/R-17-/345/252/222/344/275/223/350/265/204/346/272/220/347/224/237/345/221/275/345/221/250/346/234/237/344/270/216/351/224/231/350/257/257/345/244/204/347/220/206.md +21 -0
- package/vendor/BadCase/rn/rules/R-18-/350/203/275/345/212/233/346/236/232/344/270/276/351/205/215/347/275/256/345/256/214/346/225/264/346/200/247.md +19 -0
- package/vendor/BadCase/rn/rules/R-19-/345/233/276/345/275/242/346/270/262/346/237/223/345/261/236/346/200/247/347/273/247/346/211/277/344/270/216/351/207/215/347/275/256.md +19 -0
- package/vendor/MCP/README.md +490 -0
- package/vendor/MCP/config.json +29 -0
- package/vendor/MCP/package.json +23 -0
- package/vendor/MCP/scripts/analyze-demo-coverage.cjs +1203 -0
- package/vendor/MCP/scripts/analyze-xts-coverage.cjs +1245 -0
- package/vendor/MCP/scripts/deveco_docs.js +247 -0
- package/vendor/MCP/scripts/extract-interfaces.cjs +1487 -0
- package/vendor/MCP/scripts/flutter-analyze-demo-coverage.cjs +666 -0
- package/vendor/MCP/scripts/flutter-analyze-xts-coverage.cjs +684 -0
- package/vendor/MCP/scripts/flutter-extract-interfaces.cjs +1251 -0
- package/vendor/MCP/scripts/flutter-render-spec-doc.cjs +351 -0
- package/vendor/MCP/scripts/render-spec-doc.cjs +347 -0
- package/vendor/MCP/scripts/rn-analyze-demo-coverage.cjs +645 -0
- package/vendor/MCP/scripts/rn-analyze-test-coverage.cjs +614 -0
- package/vendor/MCP/scripts/rn-extract-interfaces.cjs +1462 -0
- package/vendor/MCP/scripts/rn-render-spec-doc.cjs +327 -0
- package/vendor/MCP/scripts/rn-scan-exports.cjs +1336 -0
- package/vendor/MCP/src/client/MCPClientPool.js +220 -0
- package/vendor/MCP/src/config/ConfigLoader.js +189 -0
- package/vendor/MCP/src/executor/ScriptExecutor.js +110 -0
- package/vendor/MCP/src/gateway/Gateway.js +451 -0
- package/vendor/MCP/src/index.js +87 -0
- package/vendor/MCP/src/library/KBToolProvider.js +229 -0
- package/vendor/MCP/src/library/logger.js +60 -0
- package/vendor/MCP/src/registry/ScriptRegistry.js +238 -0
- package/vendor/MCP/src/registry/ToolRegistry.js +111 -0
- package/vendor/MCP/src/types.js +115 -0
- package/vendor/Server/CLI/cli.js +3 -3
- package/vendor/Server/Routes/server.js +3 -3
- package/vendor/Server/Skills/arkts-library-test-coverage-check/references/MCP_TOOL_USAGE.md +4 -2
- package/vendor/Server/Skills/arkts-library-xts-coverage/SKILL.md +3 -3
- package/vendor/Server/Skills/knowledge-import/SKILL.md +1 -1
- package/vendor/Server/Skills/knowledge-import/references/MCP_TOOL_USAGE.md +12 -4
- package/vendor/Server/Skills/ohos-lib-pr-push/SKILL.md +5 -4
- package/vendor/Server/Skills/rn-library-issue-generalizer/SKILL.md +2 -2
- package/vendor/Server/Skills/rn-library-known-issue-check/SKILL.md +2 -2
- package/vendor/Server/Skills/rnoh-lib-demo-coverage/SKILL.md +1 -2
- package/vendor/Server/Skills/rnoh-lib-test-align/references/port-test-demo-guide.md +1 -1
- package/vendor/Server/Skills/rnoh-lib-xts-coverage/SKILL.md +1 -2
- package/vendor/Server/package.json +5 -1
- package/vendor/VERSION +3 -3
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCPClientPool —— 下游 MCP Server 连接池
|
|
3
|
+
*
|
|
4
|
+
* 职责:
|
|
5
|
+
* 1. 管理与下游 MCP Server 的 Client 连接(stdio + remote 两种传输)。
|
|
6
|
+
* 2. 拉取下游 Server 的工具列表。
|
|
7
|
+
* 3. 转发工具调用到下游 Server。
|
|
8
|
+
* 4. 维护连接状态,支持断开与重连。
|
|
9
|
+
*
|
|
10
|
+
* 设计原则:
|
|
11
|
+
* - 高内聚:所有下游 MCP Client 的连接/调用/断开逻辑集中在此模块。
|
|
12
|
+
* - 低耦合:不感知工具注册表和脚本执行器,只提供 connect/listTools/callTool 接口。
|
|
13
|
+
* - 错误隔离:单个下游 Server 连接失败不影响其他 Server。
|
|
14
|
+
*
|
|
15
|
+
* 支持的传输方式:
|
|
16
|
+
* - stdio:spawn 子进程,通过 stdin/stdout 通信 JSON-RPC
|
|
17
|
+
* - remote:Streamable HTTP,连接远程 MCP Server URL
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
21
|
+
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
22
|
+
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
23
|
+
import { createLogger } from '../library/logger.js';
|
|
24
|
+
|
|
25
|
+
const log = createLogger('ClientPool');
|
|
26
|
+
|
|
27
|
+
class MCPClientPool {
|
|
28
|
+
constructor() {
|
|
29
|
+
/** @type {Map<string, Client>} */
|
|
30
|
+
this._clients = new Map();
|
|
31
|
+
/** @type {Map<string, StdioClientTransport | StreamableHTTPClientTransport>} */
|
|
32
|
+
this._transports = new Map();
|
|
33
|
+
/** @type {Map<string, import('../types.js').ServerConnectionStatus>} */
|
|
34
|
+
this._statuses = new Map();
|
|
35
|
+
/** @type {Map<string, string[]>} 下游原始工具名列表 */
|
|
36
|
+
this._toolNames = new Map();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 连接一个下游 MCP Server。
|
|
41
|
+
* - 根据 type 选择传输方式(stdio / remote)
|
|
42
|
+
* - 建立 Client session
|
|
43
|
+
* - 自动完成 MCP 握手(connect 内部会发送 initialize)
|
|
44
|
+
*
|
|
45
|
+
* @param {string} name - Server 名称(用作工具前缀)
|
|
46
|
+
* @param {import('../types.js').DownstreamServerConfig} config
|
|
47
|
+
* @returns {Promise<void>}
|
|
48
|
+
*/
|
|
49
|
+
async connect(name, config) {
|
|
50
|
+
const transport = _createTransport(name, config);
|
|
51
|
+
if (!transport) return; // 创建失败已记录错误
|
|
52
|
+
|
|
53
|
+
const client = new Client(
|
|
54
|
+
{ name: 'mcp-gateway', version: '1.0.0' },
|
|
55
|
+
{ capabilities: {} }
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
// connect() 内部会启动 transport 并完成 initialize 握手
|
|
60
|
+
await client.connect(transport);
|
|
61
|
+
} catch (err) {
|
|
62
|
+
_setStatus(this._statuses, name, false, 0, err.message);
|
|
63
|
+
log.error(`连接下游 Server "${name}" 失败`, { error: err.message });
|
|
64
|
+
// 清理已创建但连接失败的资源
|
|
65
|
+
try { await client.close(); } catch { /* 忽略 */ }
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
this._clients.set(name, client);
|
|
70
|
+
this._transports.set(name, transport);
|
|
71
|
+
_setStatus(this._statuses, name, true, 0);
|
|
72
|
+
log.info(`下游 Server "${name}" 连接成功`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 拉取下游 Server 的工具列表。
|
|
77
|
+
* @param {string} name - Server 名称
|
|
78
|
+
* @returns {Promise<Array<{ name: string, description: string, inputSchema: Object }>>}
|
|
79
|
+
* MCP SDK 的 Tool 对象数组
|
|
80
|
+
* @throws {Error} Server 未连接或拉取失败
|
|
81
|
+
*/
|
|
82
|
+
async listTools(name) {
|
|
83
|
+
const client = this._clients.get(name);
|
|
84
|
+
if (!client) {
|
|
85
|
+
throw new Error(`下游 Server "${name}" 未连接`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const result = await client.listTools();
|
|
89
|
+
const tools = result.tools || [];
|
|
90
|
+
this._toolNames.set(name, tools.map(t => t.name));
|
|
91
|
+
_updateStatus(this._statuses, name, { toolCount: tools.length });
|
|
92
|
+
return tools;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 转发工具调用到下游 Server。
|
|
97
|
+
* @param {string} serverName - Server 名称
|
|
98
|
+
* @param {string} toolName - 原始工具名(不含前缀)
|
|
99
|
+
* @param {Object} args - 工具参数
|
|
100
|
+
* @returns {Promise<Object>} MCP CallToolResult
|
|
101
|
+
* @throws {Error} Server 未连接或调用失败
|
|
102
|
+
*/
|
|
103
|
+
async callTool(serverName, toolName, args) {
|
|
104
|
+
const client = this._clients.get(serverName);
|
|
105
|
+
if (!client) {
|
|
106
|
+
throw new Error(`下游 Server "${serverName}" 未连接`);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const result = await client.callTool({
|
|
110
|
+
name: toolName,
|
|
111
|
+
arguments: args
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// 返回 CallToolResult:{ content: [{ type, text }], isError? }
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 断开指定 Server 的连接。
|
|
120
|
+
* @param {string} name
|
|
121
|
+
* @returns {Promise<void>}
|
|
122
|
+
*/
|
|
123
|
+
async disconnect(name) {
|
|
124
|
+
const client = this._clients.get(name);
|
|
125
|
+
if (client) {
|
|
126
|
+
try { await client.close(); } catch { /* 忽略关闭错误 */ }
|
|
127
|
+
}
|
|
128
|
+
this._clients.delete(name);
|
|
129
|
+
this._transports.delete(name);
|
|
130
|
+
this._toolNames.delete(name);
|
|
131
|
+
_setStatus(this._statuses, name, false, 0, '已断开');
|
|
132
|
+
log.info(`下游 Server "${name}" 已断开`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 断开所有下游连接。
|
|
137
|
+
* @returns {Promise<void>}
|
|
138
|
+
*/
|
|
139
|
+
async disconnectAll() {
|
|
140
|
+
const names = Array.from(this._clients.keys());
|
|
141
|
+
await Promise.allSettled(names.map(n => this.disconnect(n)));
|
|
142
|
+
log.info('所有下游 Server 连接已断开');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 获取所有 Server 连接状态。
|
|
147
|
+
* @returns {import('../types.js').ServerConnectionStatus[]}
|
|
148
|
+
*/
|
|
149
|
+
getStatuses() {
|
|
150
|
+
return Array.from(this._statuses.values());
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 获取已连接的 Server 名称列表。
|
|
155
|
+
* @returns {string[]}
|
|
156
|
+
*/
|
|
157
|
+
getConnectedNames() {
|
|
158
|
+
return Array.from(this._clients.keys());
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// ─── 内部工具函数 ──────────────────────────────────────────────────────────────
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 根据 config.type 创建对应的 Transport。
|
|
166
|
+
* @param {string} name - Server 名称(日志用)
|
|
167
|
+
* @param {import('../types.js').DownstreamServerConfig} config
|
|
168
|
+
* @returns {StdioClientTransport | StreamableHTTPClientTransport | null}
|
|
169
|
+
* @private
|
|
170
|
+
*/
|
|
171
|
+
function _createTransport(name, config) {
|
|
172
|
+
const type = config.type || 'stdio';
|
|
173
|
+
|
|
174
|
+
if (type === 'stdio') {
|
|
175
|
+
log.info(`连接下游 Server: ${name}`, { type, command: config.command });
|
|
176
|
+
return new StdioClientTransport({
|
|
177
|
+
command: config.command,
|
|
178
|
+
args: config.args,
|
|
179
|
+
env: { ...process.env, ...config.env }
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (type === 'remote') {
|
|
184
|
+
log.info(`连接下游 Server: ${name}`, { type, url: config.url });
|
|
185
|
+
return new StreamableHTTPClientTransport(
|
|
186
|
+
new URL(config.url),
|
|
187
|
+
{ requestInit: { headers: config.headers || {} } }
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
log.error(`下游 Server "${name}" 传输类型未知: ${type}`);
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* 设置/更新 Server 连接状态。
|
|
197
|
+
* @param {Map} statuses
|
|
198
|
+
* @param {string} name
|
|
199
|
+
* @param {boolean} connected
|
|
200
|
+
* @param {number} [toolCount]
|
|
201
|
+
* @param {string} [error]
|
|
202
|
+
* @private
|
|
203
|
+
*/
|
|
204
|
+
function _setStatus(statuses, name, connected, toolCount, error) {
|
|
205
|
+
statuses.set(name, { name, connected, toolCount: toolCount ?? 0, error });
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 部分更新 Server 连接状态。
|
|
210
|
+
* @param {Map} statuses
|
|
211
|
+
* @param {string} name
|
|
212
|
+
* @param {Object} patch
|
|
213
|
+
* @private
|
|
214
|
+
*/
|
|
215
|
+
function _updateStatus(statuses, name, patch) {
|
|
216
|
+
const current = statuses.get(name) || { name, connected: false, toolCount: 0 };
|
|
217
|
+
statuses.set(name, { ...current, ...patch });
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export default MCPClientPool;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ConfigLoader —— 配置加载与校验
|
|
3
|
+
*
|
|
4
|
+
* 职责:
|
|
5
|
+
* 1. 从 JSON 文件加载 Gateway 配置。
|
|
6
|
+
* 2. 校验配置字段完整性,缺失字段使用默认值。
|
|
7
|
+
* 3. 解析相对路径(scriptsDir 相对于配置文件目录)。
|
|
8
|
+
*
|
|
9
|
+
* 设计原则:
|
|
10
|
+
* - 高内聚:所有配置解析逻辑集中在此模块。
|
|
11
|
+
* - 低耦合:只输出纯数据对象,不依赖任何业务模块。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { readFileSync } from 'node:fs';
|
|
15
|
+
import { resolve, dirname, isAbsolute } from 'node:path';
|
|
16
|
+
import { createLogger } from '../library/logger.js';
|
|
17
|
+
|
|
18
|
+
const log = createLogger('ConfigLoader');
|
|
19
|
+
|
|
20
|
+
/** 默认脚本配置 */
|
|
21
|
+
const DEFAULT_SCRIPTS_CONFIG = {
|
|
22
|
+
enabled: true,
|
|
23
|
+
hotReload: true,
|
|
24
|
+
timeout: 30000,
|
|
25
|
+
allowedExtensions: ['.sh', '.py', '.js']
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** 默认 Gateway 元信息 */
|
|
29
|
+
const DEFAULT_GATEWAY_META = {
|
|
30
|
+
name: 'mcp-gateway',
|
|
31
|
+
version: '1.0.0',
|
|
32
|
+
transport: 'stdio'
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 加载并校验 Gateway 配置。
|
|
37
|
+
*
|
|
38
|
+
* @param {string} configPath - 配置文件路径(绝对或相对)
|
|
39
|
+
* @returns {import('../types.js').GatewayConfig}
|
|
40
|
+
* @throws {Error} 配置文件不存在或格式非法时抛出
|
|
41
|
+
*/
|
|
42
|
+
export function loadConfig(configPath) {
|
|
43
|
+
const absPath = resolve(configPath);
|
|
44
|
+
|
|
45
|
+
let raw;
|
|
46
|
+
try {
|
|
47
|
+
raw = readFileSync(absPath, 'utf-8');
|
|
48
|
+
} catch (err) {
|
|
49
|
+
throw new Error(`无法读取配置文件: ${absPath} (${err.message})`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let parsed;
|
|
53
|
+
try {
|
|
54
|
+
parsed = JSON.parse(raw);
|
|
55
|
+
} catch (err) {
|
|
56
|
+
throw new Error(`配置文件 JSON 解析失败: ${absPath} (${err.message})`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 校验必需的顶层结构
|
|
60
|
+
if (typeof parsed !== 'object' || parsed === null) {
|
|
61
|
+
throw new Error('配置根节点必须是 JSON 对象');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 合并默认值
|
|
65
|
+
const config = {
|
|
66
|
+
downstreamServers: _normalizeDownstreamServers(parsed.downstreamServers),
|
|
67
|
+
scriptsDir: _normalizeScriptsDir(parsed.scriptsDir, absPath),
|
|
68
|
+
scripts: { ...DEFAULT_SCRIPTS_CONFIG, ...(parsed.scripts || {}) },
|
|
69
|
+
knowledgeBase: _normalizeKnowledgeBase(parsed.knowledgeBase, absPath),
|
|
70
|
+
gateway: { ...DEFAULT_GATEWAY_META, ...(parsed.gateway || {}) }
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
log.info('配置加载完成', {
|
|
74
|
+
downstreamCount: Object.keys(config.downstreamServers).length,
|
|
75
|
+
scriptsDir: config.scriptsDir,
|
|
76
|
+
scriptsEnabled: config.scripts.enabled,
|
|
77
|
+
kbEnabled: config.knowledgeBase ? config.knowledgeBase.enabled : false
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return config;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 规范化下游 Server 配置:过滤无效配置、补全默认字段。
|
|
85
|
+
* 支持两种传输类型:
|
|
86
|
+
* - stdio(默认):本地子进程,需要 command + args
|
|
87
|
+
* - remote:远程 HTTP,需要 url,可选 headers
|
|
88
|
+
* @param {Object} [raw]
|
|
89
|
+
* @returns {Object<string, import('../types.js').DownstreamServerConfig>}
|
|
90
|
+
* @private
|
|
91
|
+
*/
|
|
92
|
+
function _normalizeDownstreamServers(raw) {
|
|
93
|
+
if (!raw || typeof raw !== 'object') return {};
|
|
94
|
+
|
|
95
|
+
const result = {};
|
|
96
|
+
for (const [name, cfg] of Object.entries(raw)) {
|
|
97
|
+
if (!cfg || typeof cfg !== 'object') {
|
|
98
|
+
log.warn(`下游 Server "${name}" 配置无效,跳过`);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const type = cfg.type || 'stdio';
|
|
103
|
+
|
|
104
|
+
if (type === 'stdio') {
|
|
105
|
+
if (!cfg.command || !Array.isArray(cfg.args)) {
|
|
106
|
+
log.warn(`下游 Server "${name}" (stdio) 缺少 command 或 args,跳过`);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
result[name] = {
|
|
110
|
+
type: 'stdio',
|
|
111
|
+
command: cfg.command,
|
|
112
|
+
args: cfg.args,
|
|
113
|
+
env: cfg.env || {},
|
|
114
|
+
enabled: cfg.enabled !== false
|
|
115
|
+
};
|
|
116
|
+
} else if (type === 'remote') {
|
|
117
|
+
if (!cfg.url || typeof cfg.url !== 'string') {
|
|
118
|
+
log.warn(`下游 Server "${name}" (remote) 缺少 url,跳过`);
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
result[name] = {
|
|
122
|
+
type: 'remote',
|
|
123
|
+
url: cfg.url,
|
|
124
|
+
headers: cfg.headers || {},
|
|
125
|
+
enabled: cfg.enabled !== false
|
|
126
|
+
};
|
|
127
|
+
} else {
|
|
128
|
+
log.warn(`下游 Server "${name}" 未知 type: ${type},跳过`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 规范化脚本目录路径:相对于配置文件目录解析。
|
|
136
|
+
* @param {string} [raw]
|
|
137
|
+
* @param {string} configPath
|
|
138
|
+
* @returns {string}
|
|
139
|
+
* @private
|
|
140
|
+
*/
|
|
141
|
+
function _normalizeScriptsDir(raw, configPath) {
|
|
142
|
+
const fallback = resolve(dirname(configPath), 'scripts');
|
|
143
|
+
if (!raw) return fallback;
|
|
144
|
+
return isAbsolute(raw) ? raw : resolve(dirname(configPath), raw);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 规范化知识库配置。
|
|
149
|
+
* 未配置时返回 null(不启用)。配置后解析相对路径并补全默认值。
|
|
150
|
+
* @param {Object} [raw]
|
|
151
|
+
* @param {string} configPath - 配置文件绝对路径(用于解析相对路径)
|
|
152
|
+
* @returns {import('../types.js').KnowledgeBaseConfig | null}
|
|
153
|
+
* @private
|
|
154
|
+
*/
|
|
155
|
+
function _normalizeKnowledgeBase(raw, configPath) {
|
|
156
|
+
if (!raw || typeof raw !== 'object') return null;
|
|
157
|
+
|
|
158
|
+
const enabled = raw.enabled !== false;
|
|
159
|
+
|
|
160
|
+
// enginePath: Archive 引擎入口路径,相对路径基于配置文件目录解析
|
|
161
|
+
let enginePath = raw.enginePath;
|
|
162
|
+
if (!enginePath) {
|
|
163
|
+
log.warn('knowledgeBase.enginePath 未指定,知识库工具将无法加载');
|
|
164
|
+
enginePath = '';
|
|
165
|
+
} else if (!isAbsolute(enginePath)) {
|
|
166
|
+
enginePath = resolve(dirname(configPath), enginePath);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// dataDir: 知识库数据目录,相对路径基于配置文件目录解析
|
|
170
|
+
let dataDir = raw.dataDir;
|
|
171
|
+
if (!dataDir) {
|
|
172
|
+
log.warn('knowledgeBase.dataDir 未指定,使用引擎内置默认路径');
|
|
173
|
+
dataDir = '';
|
|
174
|
+
} else if (!isAbsolute(dataDir)) {
|
|
175
|
+
dataDir = resolve(dirname(configPath), dataDir);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// catalogs: 目录配置(透传,不校验细节)
|
|
179
|
+
const catalogs = Array.isArray(raw.catalogs) ? raw.catalogs : undefined;
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
enabled,
|
|
183
|
+
enginePath,
|
|
184
|
+
dataDir,
|
|
185
|
+
catalogs,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export default loadConfig;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ScriptExecutor —— 脚本执行器
|
|
3
|
+
*
|
|
4
|
+
* 职责:
|
|
5
|
+
* 1. 根据脚本扩展名选择解释器(.sh→bash, .py→python3, .js→node)。
|
|
6
|
+
* 2. 通过 child_process.spawn 执行脚本,参数以 JSON 字符串形式传入。
|
|
7
|
+
* 3. 捕获 stdout/stderr,超时自动终止。
|
|
8
|
+
*
|
|
9
|
+
* 设计原则:
|
|
10
|
+
* - 高内聚:所有脚本执行逻辑集中在此模块。
|
|
11
|
+
* - 低耦合:不感知工具注册表和 MCP 协议,只接收路径+参数,返回输出。
|
|
12
|
+
* - 安全:执行超时自动 SIGTERM,避免僵尸进程。
|
|
13
|
+
*
|
|
14
|
+
* 参数传递方式:
|
|
15
|
+
* Gateway 将 MCP 工具参数序列化为 JSON 字符串,作为脚本的第一个参数传入。
|
|
16
|
+
* 脚本内通过 $1(bash)或 sys.argv[1](python)获取后自行解析。
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { spawn } from 'node:child_process';
|
|
20
|
+
import { createLogger } from '../library/logger.js';
|
|
21
|
+
|
|
22
|
+
const log = createLogger('ScriptExecutor');
|
|
23
|
+
|
|
24
|
+
/** 扩展名 → 解释器映射 */
|
|
25
|
+
const INTERPRETERS = {
|
|
26
|
+
'.sh': 'bash',
|
|
27
|
+
'.py': 'python3',
|
|
28
|
+
'.js': 'node',
|
|
29
|
+
'.cjs': 'node',
|
|
30
|
+
'.ts': 'npx',
|
|
31
|
+
'.mjs': 'node'
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
class ScriptExecutor {
|
|
35
|
+
/**
|
|
36
|
+
* @param {number} timeout - 执行超时(ms)
|
|
37
|
+
*/
|
|
38
|
+
constructor(timeout) {
|
|
39
|
+
this._timeout = timeout;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 执行脚本,返回 stdout + stderr。
|
|
44
|
+
*
|
|
45
|
+
* - 退出码为 0 时,返回 stdout(trim)
|
|
46
|
+
* - 退出码非 0 时,返回 stdout + stderr + 退出码(便于 AI 排查)
|
|
47
|
+
* - 超时时自动终止子进程并 reject
|
|
48
|
+
*
|
|
49
|
+
* @param {string} filePath - 脚本文件绝对路径
|
|
50
|
+
* @param {string} extension - 文件扩展名
|
|
51
|
+
* @param {Object} args - 工具参数(序列化为 JSON 传入脚本)
|
|
52
|
+
* @returns {Promise<string>} 脚本输出
|
|
53
|
+
*/
|
|
54
|
+
async execute(filePath, extension, args) {
|
|
55
|
+
const interpreter = _getInterpreter(extension);
|
|
56
|
+
const argsJson = JSON.stringify(args);
|
|
57
|
+
|
|
58
|
+
log.debug(`执行脚本: ${filePath}`, { interpreter, args: argsJson });
|
|
59
|
+
|
|
60
|
+
// .ts 需要通过 npx tsx 执行
|
|
61
|
+
const cmd = extension === '.ts'
|
|
62
|
+
? spawn(interpreter, ['tsx', filePath, argsJson], { stdio: ['pipe', 'pipe', 'pipe'] })
|
|
63
|
+
: spawn(interpreter, [filePath, argsJson], { stdio: ['pipe', 'pipe', 'pipe'] });
|
|
64
|
+
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
let stdout = '';
|
|
67
|
+
let stderr = '';
|
|
68
|
+
|
|
69
|
+
cmd.stdout.on('data', (data) => { stdout += data.toString(); });
|
|
70
|
+
cmd.stderr.on('data', (data) => { stderr += data.toString(); });
|
|
71
|
+
|
|
72
|
+
const timer = setTimeout(() => {
|
|
73
|
+
cmd.kill('SIGTERM');
|
|
74
|
+
reject(new Error(`脚本执行超时 (${this._timeout}ms): ${filePath}`));
|
|
75
|
+
}, this._timeout);
|
|
76
|
+
|
|
77
|
+
cmd.on('close', (code) => {
|
|
78
|
+
clearTimeout(timer);
|
|
79
|
+
if (code === 0) {
|
|
80
|
+
resolve(stdout.trim());
|
|
81
|
+
} else {
|
|
82
|
+
// 非零退出码:拼接 stdout + stderr + 退出码,便于 AI 排查
|
|
83
|
+
const output = [
|
|
84
|
+
stdout.trim(),
|
|
85
|
+
stderr.trim() ? `[STDERR]\n${stderr.trim()}` : '',
|
|
86
|
+
`[Exit Code: ${code}]`
|
|
87
|
+
].filter(Boolean).join('\n');
|
|
88
|
+
resolve(output);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
cmd.on('error', (err) => {
|
|
93
|
+
clearTimeout(timer);
|
|
94
|
+
reject(new Error(`脚本启动失败: ${err.message}`));
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 根据扩展名获取解释器命令。
|
|
102
|
+
* @param {string} extension
|
|
103
|
+
* @returns {string}
|
|
104
|
+
* @private
|
|
105
|
+
*/
|
|
106
|
+
function _getInterpreter(extension) {
|
|
107
|
+
return INTERPRETERS[extension] || 'bash';
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export default ScriptExecutor;
|