@ohos-cpf/3rdloop 0.0.8 → 0.0.10
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 +43 -0
- package/lib/cli.js +38 -5
- package/lib/doctor.js +111 -10
- package/lib/knowledge.js +440 -0
- package/lib/mcp.js +498 -0
- 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/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,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KBToolProvider —— 知识库工具提供者
|
|
3
|
+
*
|
|
4
|
+
* 封装 Archive 的 DocSearchEngine,将其搜索/文档管理/索引管理能力
|
|
5
|
+
* 以 MCP 内置工具形式(前缀 `kb_`)注册到 ToolRegistry。
|
|
6
|
+
*
|
|
7
|
+
* 设计原则:
|
|
8
|
+
* - 高内聚:所有知识库相关的工具定义、引擎生命周期、调用分发集中于此。
|
|
9
|
+
* - 低耦合:不直接依赖 MCP SDK,只产出纯数据(工具定义 + handler),
|
|
10
|
+
* 由 Gateway 负责注册到 MCP Server。
|
|
11
|
+
* - 延迟初始化:引擎在 register() 时仅注册工具定义(不启动引擎),
|
|
12
|
+
* 首次调用搜索类工具时才惰性 init(),避免 Gateway 启动阻塞。
|
|
13
|
+
*
|
|
14
|
+
* 工具清单(前缀 kb_):
|
|
15
|
+
* - kb_search 全文搜索(FTS5 + BM25 Rerank + jieba 中文分词)
|
|
16
|
+
* - kb_add_document 添加/更新文档
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { createLogger } from '../library/logger.js';
|
|
21
|
+
|
|
22
|
+
const log = createLogger('KBToolProvider');
|
|
23
|
+
|
|
24
|
+
/** 知识库工具前缀 */
|
|
25
|
+
const KB_PREFIX = 'kb_';
|
|
26
|
+
|
|
27
|
+
class KBToolProvider {
|
|
28
|
+
/**
|
|
29
|
+
* @param {import('../types.js').KnowledgeBaseConfig} config
|
|
30
|
+
*/
|
|
31
|
+
constructor(config) {
|
|
32
|
+
this._config = config;
|
|
33
|
+
/** @type {import('../../Archive/src/engine.js').default | null} */
|
|
34
|
+
this._engine = null;
|
|
35
|
+
this._initialized = false;
|
|
36
|
+
this._initPromise = null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ─── 生命周期 ──────────────────────────────────────────────────────────────
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 惰性初始化引擎(并发安全:多次调用复用同一 Promise)。
|
|
43
|
+
* @returns {Promise<void>}
|
|
44
|
+
*/
|
|
45
|
+
async _ensureInit() {
|
|
46
|
+
if (this._initialized) return;
|
|
47
|
+
if (!this._initPromise) {
|
|
48
|
+
this._initPromise = this._doInit();
|
|
49
|
+
}
|
|
50
|
+
await this._initPromise;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 实际执行引擎初始化。
|
|
55
|
+
* @private
|
|
56
|
+
*/
|
|
57
|
+
async _doInit() {
|
|
58
|
+
// 动态导入 Archive 引擎,避免未启用知识库时加载失败
|
|
59
|
+
const { DocSearchEngine } = await import(this._config.enginePath);
|
|
60
|
+
|
|
61
|
+
this._engine = new DocSearchEngine();
|
|
62
|
+
|
|
63
|
+
const initOptions = {
|
|
64
|
+
dataDir: this._config.dataDir,
|
|
65
|
+
catalogs: this._config.catalogs,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
await this._engine.init(initOptions);
|
|
69
|
+
this._initialized = true;
|
|
70
|
+
|
|
71
|
+
const status = await this._engine.getStatus();
|
|
72
|
+
log.info('知识库引擎已初始化', {
|
|
73
|
+
dataDir: this._config.dataDir,
|
|
74
|
+
documentCount: status.documentCount,
|
|
75
|
+
segmentCount: status.segmentCount,
|
|
76
|
+
dbSizeBytes: status.dbSizeBytes,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 关闭引擎,释放资源。
|
|
82
|
+
* @returns {Promise<void>}
|
|
83
|
+
*/
|
|
84
|
+
async shutdown() {
|
|
85
|
+
if (this._engine) {
|
|
86
|
+
try {
|
|
87
|
+
await this._engine.close();
|
|
88
|
+
} catch (err) {
|
|
89
|
+
log.warn('关闭知识库引擎时出错', { error: err.message });
|
|
90
|
+
}
|
|
91
|
+
this._engine = null;
|
|
92
|
+
this._initialized = false;
|
|
93
|
+
this._initPromise = null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ─── 工具注册 ──────────────────────────────────────────────────────────────
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 返回所有知识库工具定义(含 handler)。
|
|
101
|
+
* 每个工具包含 name / source / description / inputSchema / _handler。
|
|
102
|
+
*
|
|
103
|
+
* @returns {Array<{name:string, source:'builtin', description:string, inputSchema:Object, _handler:(args:Object)=>Promise<Object>}>}
|
|
104
|
+
*/
|
|
105
|
+
getTools() {
|
|
106
|
+
return [
|
|
107
|
+
this._searchTool(),
|
|
108
|
+
this._addDocumentTool(),
|
|
109
|
+
];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ─── 工具定义 ──────────────────────────────────────────────────────────────
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* kb_search — 全文搜索。
|
|
116
|
+
* @private
|
|
117
|
+
*/
|
|
118
|
+
_searchTool() {
|
|
119
|
+
return {
|
|
120
|
+
name: `${KB_PREFIX}search`,
|
|
121
|
+
source: 'builtin',
|
|
122
|
+
description: '知识库全文搜索(FTS5 + BM25 Rerank + jieba 中文分词)。支持按目录过滤、同义词扩展。返回相关度排序的文档片段摘要。',
|
|
123
|
+
inputSchema: {
|
|
124
|
+
type: 'object',
|
|
125
|
+
properties: {
|
|
126
|
+
query: {
|
|
127
|
+
type: 'string',
|
|
128
|
+
description: '搜索关键词(空格分隔多词)',
|
|
129
|
+
},
|
|
130
|
+
catalog: {
|
|
131
|
+
type: 'string',
|
|
132
|
+
description: '限定搜索的目录 ID(如 guides/references/best-practices/faqs/releases/roadmap)。不传则全局搜索。',
|
|
133
|
+
},
|
|
134
|
+
limit: {
|
|
135
|
+
type: 'number',
|
|
136
|
+
description: '返回结果数量上限,默认 10',
|
|
137
|
+
},
|
|
138
|
+
includeSnippet: {
|
|
139
|
+
type: 'boolean',
|
|
140
|
+
description: '是否包含摘要片段,默认 true',
|
|
141
|
+
},
|
|
142
|
+
snippetMaxChars: {
|
|
143
|
+
type: 'number',
|
|
144
|
+
description: '摘要最大字符数,默认 200',
|
|
145
|
+
},
|
|
146
|
+
preferAnd: {
|
|
147
|
+
type: 'boolean',
|
|
148
|
+
description: '是否优先 AND(全部匹配)策略,默认 false(OR 任一匹配)',
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
required: ['query'],
|
|
152
|
+
},
|
|
153
|
+
_handler: async (args) => {
|
|
154
|
+
await this._ensureInit();
|
|
155
|
+
const results = await this._engine.search(
|
|
156
|
+
{ query: args.query },
|
|
157
|
+
{
|
|
158
|
+
catalog: args.catalog,
|
|
159
|
+
limit: args.limit,
|
|
160
|
+
includeSnippet: args.includeSnippet,
|
|
161
|
+
snippetMaxChars: args.snippetMaxChars,
|
|
162
|
+
preferAnd: args.preferAnd,
|
|
163
|
+
}
|
|
164
|
+
);
|
|
165
|
+
return _textResult(JSON.stringify(results, null, 2));
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* kb_add_document — 添加或更新文档。
|
|
172
|
+
* @private
|
|
173
|
+
*/
|
|
174
|
+
_addDocumentTool() {
|
|
175
|
+
return {
|
|
176
|
+
name: `${KB_PREFIX}add_document`,
|
|
177
|
+
source: 'builtin',
|
|
178
|
+
description: '向知识库添加或更新文档(自动分词建索引)。若文档已存在则覆盖更新。',
|
|
179
|
+
inputSchema: {
|
|
180
|
+
type: 'object',
|
|
181
|
+
properties: {
|
|
182
|
+
documentId: {
|
|
183
|
+
type: 'string',
|
|
184
|
+
description: '文档唯一标识(如 guides/intro)。禁止包含 .. 或绝对路径。',
|
|
185
|
+
},
|
|
186
|
+
catalog: {
|
|
187
|
+
type: 'string',
|
|
188
|
+
description: '所属目录 ID(如 guides/references/best-practices/faqs/releases/roadmap)',
|
|
189
|
+
},
|
|
190
|
+
title: {
|
|
191
|
+
type: 'string',
|
|
192
|
+
description: '文档标题(可选,不传则从 Markdown 首个标题提取)',
|
|
193
|
+
},
|
|
194
|
+
content: {
|
|
195
|
+
type: 'string',
|
|
196
|
+
description: '文档 Markdown 原文',
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
required: ['documentId', 'catalog', 'content'],
|
|
200
|
+
},
|
|
201
|
+
_handler: async (args) => {
|
|
202
|
+
await this._ensureInit();
|
|
203
|
+
await this._engine.addDocument({
|
|
204
|
+
documentId: args.documentId,
|
|
205
|
+
catalog: args.catalog,
|
|
206
|
+
title: args.title,
|
|
207
|
+
content: args.content,
|
|
208
|
+
});
|
|
209
|
+
return _textResult(`文档已添加: ${args.documentId} (catalog: ${args.catalog})`);
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// ─── 内部工具函数 ──────────────────────────────────────────────────────────────
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 构造文本类型的 CallToolResult。
|
|
219
|
+
* @param {string} text
|
|
220
|
+
* @returns {Object}
|
|
221
|
+
* @private
|
|
222
|
+
*/
|
|
223
|
+
function _textResult(text) {
|
|
224
|
+
return {
|
|
225
|
+
content: [{ type: 'text', text }]
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export default KBToolProvider;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 轻量日志工具
|
|
3
|
+
*
|
|
4
|
+
* 为 MCP Gateway 各模块提供统一日志接口。
|
|
5
|
+
* 所有日志输出到 stderr(MCP stdio 协议要求 stdout 仅用于 JSON-RPC)。
|
|
6
|
+
*
|
|
7
|
+
* 日志级别通过环境变量 LOG_LEVEL 控制(debug/info/warn/error)。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const LEVELS = { debug: 10, info: 20, warn: 30, error: 40 };
|
|
11
|
+
const MIN_LEVEL = LEVELS[process.env.LOG_LEVEL ?? 'info'] ?? LEVELS.info;
|
|
12
|
+
|
|
13
|
+
const COLORS = {
|
|
14
|
+
debug: '\x1b[90m',
|
|
15
|
+
info: '\x1b[36m',
|
|
16
|
+
warn: '\x1b[33m',
|
|
17
|
+
error: '\x1b[31m',
|
|
18
|
+
reset: '\x1b[0m'
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 格式化日志行。
|
|
23
|
+
* @param {string} level
|
|
24
|
+
* @param {string} tag
|
|
25
|
+
* @param {string} message
|
|
26
|
+
* @param {object} [meta]
|
|
27
|
+
* @returns {string}
|
|
28
|
+
*/
|
|
29
|
+
function format(level, tag, message, meta) {
|
|
30
|
+
const time = new Date().toISOString();
|
|
31
|
+
const prefix = `${COLORS[level]}${time} [${tag}] ${level.toUpperCase()}${COLORS.reset} ${message}`;
|
|
32
|
+
if (meta && Object.keys(meta).length > 0) {
|
|
33
|
+
return `${prefix} ${JSON.stringify(meta)}`;
|
|
34
|
+
}
|
|
35
|
+
return prefix;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 创建带标签的日志实例。
|
|
40
|
+
* @param {string} tag - 模块标签(如 'Gateway'、'ClientPool')
|
|
41
|
+
* @returns {{ debug: Function, info: Function, warn: Function, error: Function }}
|
|
42
|
+
*/
|
|
43
|
+
export function createLogger(tag) {
|
|
44
|
+
return {
|
|
45
|
+
debug: (msg, meta) => {
|
|
46
|
+
if (LEVELS.debug >= MIN_LEVEL) console.error(format('debug', tag, msg, meta));
|
|
47
|
+
},
|
|
48
|
+
info: (msg, meta) => {
|
|
49
|
+
if (LEVELS.info >= MIN_LEVEL) console.error(format('info', tag, msg, meta));
|
|
50
|
+
},
|
|
51
|
+
warn: (msg, meta) => {
|
|
52
|
+
if (LEVELS.warn >= MIN_LEVEL) console.error(format('warn', tag, msg, meta));
|
|
53
|
+
},
|
|
54
|
+
error: (msg, meta) => {
|
|
55
|
+
if (LEVELS.error >= MIN_LEVEL) console.error(format('error', tag, msg, meta));
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export default createLogger;
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ScriptRegistry —— 脚本注册表 + 自动发现
|
|
3
|
+
*
|
|
4
|
+
* 职责:
|
|
5
|
+
* 1. 扫描脚本目录,解析脚本头部 @tool 注释,提取元数据。
|
|
6
|
+
* 2. 维护脚本工具注册表(toolName → ScriptMeta)。
|
|
7
|
+
* 3. 支持文件监听(chokidar),脚本增删时自动更新注册表并回调通知。
|
|
8
|
+
*
|
|
9
|
+
* 设计原则:
|
|
10
|
+
* - 高内聚:所有脚本发现/解析/监听逻辑集中在此模块。
|
|
11
|
+
* - 低耦合:不执行脚本,不注册 MCP 工具,只输出 ScriptMeta 数据。
|
|
12
|
+
*
|
|
13
|
+
* 脚本头部注释约定:
|
|
14
|
+
* # @tool <名称> — 必填,工具名(中文可用)
|
|
15
|
+
* # @description <描述> — 工具描述,给 AI 理解用途
|
|
16
|
+
* # @param <名>: <类型> - <说明> — 参数定义(类型: string/number/boolean)
|
|
17
|
+
* # @example <调用示例> — 调用示例
|
|
18
|
+
*
|
|
19
|
+
* 示例:
|
|
20
|
+
* #!/bin/bash
|
|
21
|
+
* # @tool git操作
|
|
22
|
+
* # @description 执行 Git 操作
|
|
23
|
+
* # @param action: string - 操作类型 (clone|pull|push)
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import fs from 'node:fs';
|
|
27
|
+
import path from 'node:path';
|
|
28
|
+
import chokidar from 'chokidar';
|
|
29
|
+
import { createLogger } from '../library/logger.js';
|
|
30
|
+
|
|
31
|
+
const log = createLogger('ScriptRegistry');
|
|
32
|
+
|
|
33
|
+
/** 只解析脚本前 N 行的注释 */
|
|
34
|
+
const MAX_SCAN_LINES = 20;
|
|
35
|
+
|
|
36
|
+
class ScriptRegistry {
|
|
37
|
+
/**
|
|
38
|
+
* @param {string} scriptsDir - 脚本目录绝对路径
|
|
39
|
+
* @param {string[]} allowedExtensions - 允许的扩展名(如 ['.sh', '.py'])
|
|
40
|
+
*/
|
|
41
|
+
constructor(scriptsDir, allowedExtensions) {
|
|
42
|
+
this._scriptsDir = scriptsDir;
|
|
43
|
+
this._allowedExtensions = allowedExtensions;
|
|
44
|
+
/** @type {Map<string, import('../types.js').ScriptMeta>} */
|
|
45
|
+
this._scripts = new Map();
|
|
46
|
+
/** @type {chokidar.FSWatcher | null} */
|
|
47
|
+
this._watcher = null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 扫描脚本目录,解析所有脚本的元数据并注册。
|
|
52
|
+
* @returns {Promise<import('../types.js').ScriptMeta[]>}
|
|
53
|
+
*/
|
|
54
|
+
async scanAll() {
|
|
55
|
+
this._scripts.clear();
|
|
56
|
+
|
|
57
|
+
let files;
|
|
58
|
+
try {
|
|
59
|
+
files = fs.readdirSync(this._scriptsDir);
|
|
60
|
+
} catch (err) {
|
|
61
|
+
if (err.code === 'ENOENT') {
|
|
62
|
+
log.warn(`脚本目录不存在: ${this._scriptsDir}`);
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
throw err;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const metas = [];
|
|
69
|
+
for (const file of files) {
|
|
70
|
+
const filePath = path.join(this._scriptsDir, file);
|
|
71
|
+
const meta = this._parseAndRegister(filePath);
|
|
72
|
+
if (meta) metas.push(meta);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
log.info(`脚本扫描完成`, { count: metas.length, dir: this._scriptsDir });
|
|
76
|
+
return metas;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 解析单个脚本的元数据并注册到注册表。
|
|
81
|
+
* @param {string} filePath - 脚本文件绝对路径
|
|
82
|
+
* @returns {import('../types.js').ScriptMeta | null}
|
|
83
|
+
* @private
|
|
84
|
+
*/
|
|
85
|
+
_parseAndRegister(filePath) {
|
|
86
|
+
let stat;
|
|
87
|
+
try {
|
|
88
|
+
stat = fs.statSync(filePath);
|
|
89
|
+
} catch {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
if (!stat.isFile()) return null;
|
|
93
|
+
|
|
94
|
+
const ext = path.extname(filePath);
|
|
95
|
+
if (!this._allowedExtensions.includes(ext)) return null;
|
|
96
|
+
|
|
97
|
+
const meta = this._parseScriptMeta(filePath);
|
|
98
|
+
if (!meta) return null;
|
|
99
|
+
|
|
100
|
+
this._scripts.set(meta.toolName, meta);
|
|
101
|
+
return meta;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 解析脚本头部注释,提取 @tool / @description / @param。
|
|
106
|
+
* @param {string} filePath
|
|
107
|
+
* @returns {import('../types.js').ScriptMeta | null} 无 @tool 注释返回 null
|
|
108
|
+
* @private
|
|
109
|
+
*/
|
|
110
|
+
_parseScriptMeta(filePath) {
|
|
111
|
+
let content;
|
|
112
|
+
try {
|
|
113
|
+
content = fs.readFileSync(filePath, 'utf-8');
|
|
114
|
+
} catch (err) {
|
|
115
|
+
log.warn(`读取脚本失败: ${filePath}`, { error: err.message });
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const lines = content.split('\n').slice(0, MAX_SCAN_LINES);
|
|
120
|
+
|
|
121
|
+
let rawName = '';
|
|
122
|
+
let description = '';
|
|
123
|
+
const params = [];
|
|
124
|
+
|
|
125
|
+
for (const line of lines) {
|
|
126
|
+
// @tool <名称>
|
|
127
|
+
const toolMatch = line.match(/@tool\s+(.+)/);
|
|
128
|
+
if (toolMatch) {
|
|
129
|
+
rawName = toolMatch[1].trim();
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// @description <描述>
|
|
134
|
+
const descMatch = line.match(/@description\s+(.+)/);
|
|
135
|
+
if (descMatch) {
|
|
136
|
+
description = descMatch[1].trim();
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// @param <名>: <类型> - <说明>
|
|
141
|
+
const paramMatch = line.match(/@param\s+(\w+):\s*(\w+)\s*-\s*(.+)/);
|
|
142
|
+
if (paramMatch) {
|
|
143
|
+
params.push({
|
|
144
|
+
name: paramMatch[1],
|
|
145
|
+
type: paramMatch[2],
|
|
146
|
+
description: paramMatch[3].trim(),
|
|
147
|
+
required: true
|
|
148
|
+
});
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (!rawName) return null; // 无 @tool 注释的脚本跳过
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
toolName: `script_${rawName}`,
|
|
157
|
+
rawName,
|
|
158
|
+
description: description || `Script: ${rawName}`,
|
|
159
|
+
filePath,
|
|
160
|
+
extension: path.extname(filePath),
|
|
161
|
+
params
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 启动文件监听。脚本增删时自动更新注册表并回调。
|
|
167
|
+
* @param {(meta: import('../types.js').ScriptMeta, action: 'add'|'remove') => void} onChange
|
|
168
|
+
*/
|
|
169
|
+
startWatch(onChange) {
|
|
170
|
+
this._watcher = chokidar.watch(this._scriptsDir, {
|
|
171
|
+
ignoreInitial: true,
|
|
172
|
+
persistent: true
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
this._watcher.on('add', (filePath) => {
|
|
176
|
+
const meta = this._parseAndRegister(filePath);
|
|
177
|
+
if (meta) {
|
|
178
|
+
log.info(`脚本新增: ${meta.toolName}`);
|
|
179
|
+
onChange(meta, 'add');
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
this._watcher.on('unlink', (filePath) => {
|
|
184
|
+
for (const [name, meta] of this._scripts) {
|
|
185
|
+
if (meta.filePath === filePath) {
|
|
186
|
+
this._scripts.delete(name);
|
|
187
|
+
log.info(`脚本移除: ${name}`);
|
|
188
|
+
onChange(meta, 'remove');
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
this._watcher.on('error', (err) => {
|
|
195
|
+
log.error('脚本监听错误', { error: err.message });
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
log.info('脚本热加载监听已启动');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 停止文件监听。
|
|
203
|
+
*/
|
|
204
|
+
async stopWatch() {
|
|
205
|
+
if (this._watcher) {
|
|
206
|
+
await this._watcher.close();
|
|
207
|
+
this._watcher = null;
|
|
208
|
+
log.info('脚本热加载监听已停止');
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* 根据 toolName 获取脚本元数据。
|
|
214
|
+
* @param {string} toolName
|
|
215
|
+
* @returns {import('../types.js').ScriptMeta | undefined}
|
|
216
|
+
*/
|
|
217
|
+
get(toolName) {
|
|
218
|
+
return this._scripts.get(toolName);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* 获取所有已注册脚本。
|
|
223
|
+
* @returns {import('../types.js').ScriptMeta[]}
|
|
224
|
+
*/
|
|
225
|
+
getAll() {
|
|
226
|
+
return Array.from(this._scripts.values());
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 获取脚本数量。
|
|
231
|
+
* @returns {number}
|
|
232
|
+
*/
|
|
233
|
+
size() {
|
|
234
|
+
return this._scripts.size;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export default ScriptRegistry;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ToolRegistry —— 统一工具注册表
|
|
3
|
+
*
|
|
4
|
+
* 职责:
|
|
5
|
+
* 1. 维护所有聚合工具的注册表(下游工具 + 脚本工具 + 内置工具)。
|
|
6
|
+
* 2. 提供工具的增删查接口,供 Gateway 核心调用。
|
|
7
|
+
* 3. 检测工具名冲突,后注册同名工具时跳过并记录警告。
|
|
8
|
+
*
|
|
9
|
+
* 设计原则:
|
|
10
|
+
* - 高内聚:工具注册/查找/删除逻辑集中在此模块。
|
|
11
|
+
* - 低耦合:只存储数据,不执行工具调用,不感知 MCP SDK。
|
|
12
|
+
*
|
|
13
|
+
* 工具命名规范:
|
|
14
|
+
* - 下游工具:{serverName}_{originalToolName}(如 fetch_url)
|
|
15
|
+
* - 脚本工具:script_{rawName}(如 script_git操作)
|
|
16
|
+
* - 内置工具:gateway_{action}(如 gateway_health)
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { createLogger } from '../library/logger.js';
|
|
20
|
+
|
|
21
|
+
const log = createLogger('ToolRegistry');
|
|
22
|
+
|
|
23
|
+
class ToolRegistry {
|
|
24
|
+
constructor() {
|
|
25
|
+
/** @type {Map<string, import('../types.js').AggregatedTool>} */
|
|
26
|
+
this._tools = new Map();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 注册一个工具。若名称已存在则跳过并记录警告。
|
|
31
|
+
* @param {import('../types.js').AggregatedTool} tool
|
|
32
|
+
* @returns {boolean} 是否注册成功(false 表示名称冲突被跳过)
|
|
33
|
+
*/
|
|
34
|
+
register(tool) {
|
|
35
|
+
if (this._tools.has(tool.name)) {
|
|
36
|
+
log.warn(`工具名冲突,跳过注册: "${tool.name}"`);
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
this._tools.set(tool.name, tool);
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 移除一个工具。
|
|
45
|
+
* @param {string} name
|
|
46
|
+
* @returns {boolean} 是否移除成功
|
|
47
|
+
*/
|
|
48
|
+
unregister(name) {
|
|
49
|
+
return this._tools.delete(name);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 按名称查找工具。
|
|
54
|
+
* @param {string} name
|
|
55
|
+
* @returns {import('../types.js').AggregatedTool | undefined}
|
|
56
|
+
*/
|
|
57
|
+
get(name) {
|
|
58
|
+
return this._tools.get(name);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 判断工具是否已注册。
|
|
63
|
+
* @param {string} name
|
|
64
|
+
* @returns {boolean}
|
|
65
|
+
*/
|
|
66
|
+
has(name) {
|
|
67
|
+
return this._tools.has(name);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 获取所有已注册工具。
|
|
72
|
+
* @returns {import('../types.js').AggregatedTool[]}
|
|
73
|
+
*/
|
|
74
|
+
getAll() {
|
|
75
|
+
return Array.from(this._tools.values());
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 获取所有工具名。
|
|
80
|
+
* @returns {string[]}
|
|
81
|
+
*/
|
|
82
|
+
names() {
|
|
83
|
+
return Array.from(this._tools.keys());
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 获取工具总数。
|
|
88
|
+
* @returns {number}
|
|
89
|
+
*/
|
|
90
|
+
size() {
|
|
91
|
+
return this._tools.size;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 按来源过滤工具。
|
|
96
|
+
* @param {import('../types.js').ToolSource} source
|
|
97
|
+
* @returns {import('../types.js').AggregatedTool[]}
|
|
98
|
+
*/
|
|
99
|
+
filterBySource(source) {
|
|
100
|
+
return this.getAll().filter(t => t.source === source);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 清空注册表。
|
|
105
|
+
*/
|
|
106
|
+
clear() {
|
|
107
|
+
this._tools.clear();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export default ToolRegistry;
|