@microi.net/cli 5.9.2 → 5.9.3

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.
Files changed (199) hide show
  1. package/.codebuddy-plugin/marketplace.json +2 -2
  2. package/.codebuddy-plugin/plugin.json +1 -1
  3. package/.codex-plugin/plugin.json +1 -1
  4. package/.workbuddy-plugin/marketplace.json +2 -2
  5. package/.workbuddy-plugin/plugin.json +1 -1
  6. package/LICENSE +21 -21
  7. package/README.md +71 -71
  8. package/assets/build-meta.json +5 -5
  9. package/assets/feature-matrix.json +138 -138
  10. package/assets/logo.svg +4 -4
  11. package/cordis.patch.yml +1 -1
  12. package/package.json +1 -1
  13. package/scripts/microi-codex-broker.js +466 -450
  14. package/scripts/microi-codex-router.js +634 -618
  15. package/scripts/microi-skills.meta.json +384 -378
  16. package/skills/.microi-skills-version.json +2 -2
  17. package/skills/.progressive-disclosure-manifest.json +122 -115
  18. package/skills/README.md +287 -287
  19. package/skills/ai-engine/SKILL.md +269 -269
  20. package/skills/ai-engine/agents/openai.yaml +4 -4
  21. package/skills/ai-engine/references/ai-employees.md +48 -48
  22. package/skills/ai-engine/references/self-hosted-digital-human.md +59 -59
  23. package/skills/ai-platform-governance/SKILL.md +177 -177
  24. package/skills/ai-platform-governance/references/progressive-01-/345/212/237/350/203/275/345/274/200/345/205/263.md +190 -190
  25. package/skills/app-store/SKILL.md +525 -525
  26. package/skills/app-store/agents/openai.yaml +4 -4
  27. package/skills/business-blueprint/SKILL.md +193 -193
  28. package/skills/datasource-engine/SKILL.md +93 -93
  29. package/skills/datasource-engine/agents/openai.yaml +4 -4
  30. package/skills/dos-orm/SKILL.md +101 -95
  31. package/skills/dos-orm/references/api-reference.md +229 -229
  32. package/skills/email-engine/SKILL.md +81 -81
  33. package/skills/email-engine/references/v8-email.md +34 -34
  34. package/skills/job-engine/SKILL.md +176 -176
  35. package/skills/job-engine/agents/openai.yaml +4 -4
  36. package/skills/message-notification/SKILL.md +156 -156
  37. package/skills/message-notification/agents/openai.yaml +5 -5
  38. package/skills/message-notification/references/contracts.md +102 -102
  39. package/skills/microi/SKILL.md +14 -14
  40. package/skills/microi-ai-app-auth.js +652 -652
  41. package/skills/microi-ai-application/SKILL.md +115 -115
  42. package/skills/microi-ai-application/agents/openai.yaml +4 -4
  43. package/skills/microi-ai-application/references/frontend-baseline.md +164 -164
  44. package/skills/microi-client-frontend/SKILL.md +242 -242
  45. package/skills/microi-client-frontend/references/progressive-01-3-/345/212/250/346/200/201/346/214/211/351/222/256/347/263/273/347/273/237.md +144 -144
  46. package/skills/microi-client-frontend/references/progressive-02-8-/350/277/220/350/241/214/346/227/266/351/253/230/351/242/221/345/235/221/345/244/215/347/233/230.md +196 -196
  47. package/skills/microi-client-frontend/references/progressive-03-vue3-/345/211/215/347/253/257/345/276/256/346/234/215/345/212/241/345/256/277/344/270/273/350/247/204/345/210/231.md +167 -167
  48. package/skills/microi-codex-installer/SKILL.md +231 -231
  49. package/skills/microi-codex-installer/agents/openai.yaml +7 -7
  50. package/skills/microi-datasource-mapping/SKILL.md +122 -122
  51. package/skills/microi-db-schema/SKILL.md +175 -175
  52. package/skills/microi-db-schema/agents/openai.yaml +4 -4
  53. package/skills/microi-db-schema/references/core-tables.md +695 -695
  54. package/skills/microi-db-schema/references/form-component-options.md +256 -256
  55. package/skills/microi-db-schema/references/schema-overview.md +202 -202
  56. package/skills/microi-db-schema/references/schema.md +646 -646
  57. package/skills/microi-db-schema/references/table-catalog.md +1599 -1599
  58. package/skills/microi-deployment/SKILL.md +239 -220
  59. package/skills/microi-deployment/references/deployment-matrix.md +109 -109
  60. package/skills/microi-docs-coverage/SKILL.md +133 -133
  61. package/skills/microi-docs-coverage/references/capability-map.md +90 -90
  62. package/skills/microi-docs-coverage/scripts/audit-doc-skill-coverage.mjs +894 -894
  63. package/skills/microi-form-engine/SKILL.md +338 -333
  64. package/skills/microi-form-engine/references/component-catalog.md +218 -218
  65. package/skills/microi-form-engine/references/data-source-events.md +124 -124
  66. package/skills/microi-frontend-sdk/SKILL.md +194 -194
  67. package/skills/microi-frontend-sdk/references/progressive-01-token-/345/275/223/345/211/215/347/231/273/345/275/225/347/224/250/346/210/267/344/270/216/345/275/223/345/211/215/347/273/210/347/253/257/347/231/273/345/275/225/345/215/217/350/256/256.md +176 -176
  68. package/skills/microi-left-right-layout/SKILL.md +141 -141
  69. package/skills/microi-microservice/SKILL.md +330 -328
  70. package/skills/microi-microservice/references/runtime-delivery.md +278 -278
  71. package/skills/microi-mobile-app-quality/SKILL.md +203 -203
  72. package/skills/microi-mobile-app-quality/references/progressive-01-4-/351/207/215/350/246/201/346/214/211/351/222/256/345/277/205/351/241/273/345/270/246/345/233/276/346/240/207.md +227 -227
  73. package/skills/microi-mobile-app-quality/references/progressive-02-9-/344/270/273/351/242/230/345/210/207/346/215/242/345/277/205/351/241/273/347/234/237/345/256/236/344/270/224/345/205/250/345/261/200/347/224/237/346/225/210.md +129 -129
  74. package/skills/microi-solution-quotation/SKILL.md +78 -78
  75. package/skills/microi-solution-quotation/agents/openai.yaml +4 -4
  76. package/skills/microi-solution-quotation/scripts/build_solution_quote.py +296 -296
  77. package/skills/microi-sso/SKILL.md +95 -91
  78. package/skills/microi-sso/references/acceptance.md +51 -49
  79. package/skills/microi-sso/references/configuration-and-security.md +53 -53
  80. package/skills/microi-sso/references/inbound.md +53 -53
  81. package/skills/microi-sso/references/outbound.md +43 -39
  82. package/skills/microi-ui/SKILL.md +192 -192
  83. package/skills/microi-ui/references/progressive-01-/347/247/273/345/212/250/347/253/257/345/234/272/346/231/257/350/223/235/345/233/276.md +183 -183
  84. package/skills/microi-uniapp-frontend/SKILL.md +206 -204
  85. package/skills/microi-uniapp-frontend/references/progressive-01-/347/247/273/345/212/250/347/253/257/345/210/206/347/261/273-/345/217/214/346/240/217/345/210/227/350/241/250/347/213/254/347/253/213/346/273/232/345/212/250.md +261 -261
  86. package/skills/microi-uniapp-frontend/references/progressive-02-/345/205/263/351/224/256/344/270/232/345/212/241/350/265/204/344/272/247/344/270/215/345/276/227/351/273/230/350/256/244/351/200/211/344/270/255.md +154 -154
  87. package/skills/microi.v8.js +1921 -1921
  88. package/skills/module-engine/SKILL.md +255 -255
  89. package/skills/module-engine/references/module-config.md +204 -204
  90. package/skills/ocr-engine/SKILL.md +113 -113
  91. package/skills/ocr-engine/agents/openai.yaml +4 -4
  92. package/skills/page-engine/SKILL.md +206 -206
  93. package/skills/page-engine/examples/compact-dashboard.json +1444 -1444
  94. package/skills/page-engine/references/progressive-01-/346/211/200/346/234/211/347/273/204/344/273/266/347/261/273/345/236/213.md +234 -234
  95. package/skills/page-engine/references/progressive-02-/347/211/210/346/234/254/345/216/206/345/217/262-/345/271/266/345/217/221/344/277/235/345/255/230/344/270/216/345/233/236/346/273/232.md +60 -60
  96. package/skills/performance-testing/SKILL.md +227 -221
  97. package/skills/playwright-e2e/SKILL.md +197 -196
  98. package/skills/playwright-e2e/references/progressive-01-/345/205/250/350/207/252/345/212/250/347/231/273/345/275/225-/345/205/215/351/252/214/350/257/201/347/240/201-/344/275/206/344/270/215/345/205/215/345/257/206/347/240/201-/345/277/205/350/257/273.md +173 -173
  99. package/skills/playwright-e2e/references/progressive-02-/346/226/207/345/255/227/345/257/271/346/257/224/345/272/246/344/270/216/345/217/257/350/257/273/346/200/247/350/207/252/345/212/250/345/214/226/346/243/200/346/237/245-/345/277/205/345/201/232.md +183 -183
  100. package/skills/playwright-e2e/references/progressive-03-microi-helper-/346/250/241/346/235/277.md +221 -221
  101. package/skills/playwright-e2e/references/progressive-04-ci-/345/273/272/350/256/256.md +115 -115
  102. package/skills/print-engine/SKILL.md +259 -259
  103. package/skills/production-readonly-audit/SKILL.md +41 -41
  104. package/skills/report-engine/SKILL.md +71 -71
  105. package/skills/report-engine/agents/openai.yaml +4 -4
  106. package/skills/scripts/optimize-progressive-disclosure.mjs +204 -204
  107. package/skills/scripts/refresh-progressive-disclosure.mjs +64 -64
  108. package/skills/scripts/sync-embedded-skills.mjs +46 -46
  109. package/skills/scripts/validate-progressive-disclosure.mjs +57 -57
  110. package/skills/search-engine/SKILL.md +75 -75
  111. package/skills/search-engine/agents/openai.yaml +4 -4
  112. package/skills/spider-engine/SKILL.md +190 -190
  113. package/skills/system-observability/SKILL.md +259 -246
  114. package/skills/system-observability/references/memory-incident-triage.md +77 -77
  115. package/skills/translate-engine/SKILL.md +140 -140
  116. package/skills/translate-engine/agents/openai.yaml +4 -4
  117. package/skills/ui-design/SKILL.md +223 -223
  118. package/skills/ui-design/assets/pattern-showcase/app.js +54 -54
  119. package/skills/ui-design/assets/pattern-showcase/index.html +163 -163
  120. package/skills/ui-design/assets/pattern-showcase/styles.css +311 -311
  121. package/skills/ui-design/assets/templates/MCI-DESIGN.md +206 -206
  122. package/skills/ui-design/references/design-pattern-library.md +184 -184
  123. package/skills/ui-design/references/mci-design-contract.md +163 -163
  124. package/skills/ui-design/references/motion-and-media.md +78 -78
  125. package/skills/ui-design/references/product-flow-recipes.md +94 -94
  126. package/skills/ui-design/references/progressive-01-/351/242/234/350/211/262/344/275/223/347/263/273-css-variables-/346/224/257/346/214/201/344/270/273/351/242/230/345/210/207/346/215/242.md +218 -218
  127. package/skills/ui-design/references/progressive-02-/345/255/227/344/275/223.md +164 -164
  128. package/skills/ui-design/references/progressive-03-/345/212/250/346/225/210/350/247/204/350/214/203-/344/270/260/345/257/214/344/275/206/344/270/215/345/215/241.md +235 -235
  129. package/skills/ui-design/references/progressive-04-/347/273/204/344/273/266/351/243/216/346/240/274/351/200/237/346/237/245.md +152 -152
  130. package/skills/ui-design/references/progressive-05-/347/247/273/345/212/250/347/253/257/344/270/223/347/224/250/350/247/204/350/214/203.md +238 -238
  131. package/skills/ui-design/references/progressive-06-/344/270/273/351/242/230/345/210/207/346/215/242/345/256/236/347/216/260.md +194 -194
  132. package/skills/ui-design/references/progressive-07-/351/200/237/346/237/245-/344/273/216/345/244/264/346/220/255/345/273/272/344/270/200/344/270/252/347/247/273/345/212/250/347/253/257/351/241/265/351/235/242.md +207 -207
  133. package/skills/ui-design/references/progressive-08-/350/241/250/345/215/225/345/210/206/347/273/204/350/247/204/350/214/203-tabs-vs-collapsegroup-/345/274/272/345/210/266.md +142 -142
  134. package/skills/uniapp-mall-assets/SKILL.md +176 -176
  135. package/skills/unity-integration/SKILL.md +178 -171
  136. package/skills/unity-integration/agents/openai.yaml +4 -4
  137. package/skills/unity-integration/references/ai-app-delivery.md +119 -119
  138. package/skills/unity-integration/references/sdk-api.md +82 -82
  139. package/skills/unity-integration/references/toolbox-migration.md +66 -66
  140. package/skills/unity-integration/references/webgl-hosting.md +57 -57
  141. package/skills/v8-api-config/SKILL.md +388 -388
  142. package/skills/v8-cache-pattern/SKILL.md +312 -312
  143. package/skills/v8-crud-api/SKILL.md +179 -178
  144. package/skills/v8-crud-api/references/progressive-01-/346/237/245/350/257/242/345/210/227/350/241/250-/345/210/206/351/241/265.md +226 -226
  145. package/skills/v8-crud-api/references/progressive-02-where-/346/235/241/344/273/266/350/257/255/346/263/225/351/200/237/346/237/245.md +49 -49
  146. package/skills/v8-debugging/SKILL.md +284 -284
  147. package/skills/v8-explorer-tree/SKILL.md +228 -228
  148. package/skills/v8-export-import/SKILL.md +219 -219
  149. package/skills/v8-export-import/references/progressive-01-excellayout-/351/253/230/347/272/247/350/207/252/347/224/261/345/270/203/345/261/200.md +211 -211
  150. package/skills/v8-export-import/references/progressive-02-powerpoint-/345/257/274/345/207/272.md +202 -202
  151. package/skills/v8-export-import/references/progressive-03-/345/256/211/345/205/250-/346/200/247/350/203/275/346/263/250/346/204/217.md +42 -42
  152. package/skills/v8-file-upload/SKILL.md +255 -284
  153. package/skills/v8-file-upload/references/cdn-alias-repair.md +13 -0
  154. package/skills/v8-file-upload/references/progressive-01-/345/205/254/346/234/211/346/241/266-vs-/347/247/201/346/234/211/346/241/266.md +263 -263
  155. package/skills/v8-file-upload/references/progressive-02-office-/346/226/207/344/273/266/345/234/250/347/272/277/347/274/226/350/276/221/347/211/210/346/234/254/345/217/267/350/247/204/345/210/231.md +161 -161
  156. package/skills/v8-file-upload/references/progressive-03-/347/251/272/347/233/256/345/275/225/346/240/207/350/256/260/344/270/216/350/277/234/347/250/213/344/270/213/350/275/275.md +36 -0
  157. package/skills/v8-formengine-http/SKILL.md +238 -238
  158. package/skills/v8-frontend-events/SKILL.md +180 -180
  159. package/skills/v8-frontend-events/references/bluetooth-print-api.md +135 -135
  160. package/skills/v8-frontend-events/references/bluetooth-print.md +258 -258
  161. package/skills/v8-frontend-events/references/progressive-01-/345/210/227/350/241/250/344/272/213/344/273/266.md +219 -219
  162. package/skills/v8-http-integration/SKILL.md +182 -182
  163. package/skills/v8-http-integration/references/progressive-01-get-/350/257/267/346/261/202.md +220 -220
  164. package/skills/v8-http-integration/references/progressive-02-/351/224/231/350/257/257/345/244/204/347/220/206/346/250/241/345/274/217.md +44 -44
  165. package/skills/v8-image-processing/SKILL.md +190 -190
  166. package/skills/v8-image-processing/agents/openai.yaml +4 -4
  167. package/skills/v8-image-processing/references/api-reference.md +623 -623
  168. package/skills/v8-menu-buttons/SKILL.md +186 -186
  169. package/skills/v8-menu-buttons/references/progressive-01-2-/346/214/211/351/222/256/345/257/271/350/261/241-schema.md +222 -222
  170. package/skills/v8-menu-buttons/references/progressive-02-8-/346/250/241/345/274/217-f-/345/220/216/345/217/260/344/273/273/345/212/241/346/214/211/351/222/256-/351/225/277/344/273/273/345/212/241.md +228 -228
  171. package/skills/v8-menu-buttons/references/progressive-03-10-/345/217/215/346/250/241/345/274/217-/351/201/277/345/205/215.md +104 -104
  172. package/skills/v8-mongodb/SKILL.md +200 -200
  173. package/skills/v8-mq-mqtt/SKILL.md +178 -176
  174. package/skills/v8-mq-mqtt/references/mqtt-production.md +352 -342
  175. package/skills/v8-mq-mqtt/references/progressive-01-v8-mqtt-iot-/347/211/251/350/201/224/347/275/221.md +181 -181
  176. package/skills/v8-mq-mqtt/scripts/check-mqtt-skill-coverage.mjs +203 -203
  177. package/skills/v8-saas-multi-tenant/SKILL.md +305 -305
  178. package/skills/v8-security/SKILL.md +227 -210
  179. package/skills/v8-security/references/progressive-01-2-/346/235/203/351/231/220/346/240/241/351/252/214.md +200 -200
  180. package/skills/v8-security/references/progressive-02-7-/346/227/245/345/277/227/350/256/260/345/275/225.md +160 -160
  181. package/skills/v8-sql-query/SKILL.md +302 -302
  182. package/skills/v8-table-event/SKILL.md +176 -176
  183. package/skills/v8-table-event/references/progressive-01-informv8-js-/350/241/250/345/215/225/346/211/223/345/274/200/344/272/213/344/273/266.md +216 -216
  184. package/skills/v8-table-event/references/progressive-02-/345/211/215/347/253/257/344/272/213/344/273/266/345/220/215-v8-eventname-/345/217/257/350/203/275/347/232/204/345/200/274.md +46 -46
  185. package/skills/v8-tcp-integration/SKILL.md +158 -147
  186. package/skills/v8-tcp-integration/agents/openai.yaml +4 -4
  187. package/skills/v8-template-engine/SKILL.md +167 -167
  188. package/skills/v8-utilities/SKILL.md +104 -104
  189. package/skills/v8-utilities/references/client-api-index.md +143 -143
  190. package/skills/v8-utilities/references/platform-http-routes.md +83 -83
  191. package/skills/v8-utilities/references/server-api-index.md +188 -188
  192. package/skills/v8-workflow/SKILL.md +252 -252
  193. package/skills/v8-workflow/references/progressive-01-/350/212/202/347/202/271/345/274/200/345/247/213-v8-/344/272/213/344/273/266.md +180 -180
  194. package/skills/v8-workflow/references/workflow-configuration.md +49 -49
  195. package/skills/vision-engine/SKILL.md +160 -160
  196. package/skills/vision-engine/agents/openai.yaml +4 -4
  197. package/skills/vision-engine/references/architecture-and-acceptance.md +194 -194
  198. package/skills/workspace-conventions/SKILL.md +13 -13
  199. package/skills/workspace-conventions/references/progressive-03-cli-/344/270/216-ide-/346/217/222/344/273/266/351/224/231/347/211/210/345/205/261/345/255/230/347/272/246/345/256/232.md +2 -2
@@ -1,618 +1,634 @@
1
- #!/usr/bin/env node
2
- /*
3
- * Microi Agentx Plugin 多连接路由器。
4
- *
5
- * 它只负责发现 VS Code/CLI 共用的工作区配置、选择连接并把请求转发给
6
- * 同一份 Microi MCP bundle。所有业务 schema、确认口令、审计和写后回读
7
- * 都由原 MCP 工具执行,本文件不实现第二套远端 API。
8
- */
9
-
10
- const childProcess = require('node:child_process');
11
- const crypto = require('node:crypto');
12
- const fs = require('node:fs');
13
- const net = require('node:net');
14
- const os = require('node:os');
15
- const path = require('node:path');
16
-
17
- const CONFIG_DIR = 'Microi-V8-Engine';
18
- const CONFIG_FILE = '.microi-config.json';
19
- const TOKEN_FILE = '.microi-mcp-tokens.json';
20
- const PROTOCOL_VERSION = '2024-11-05';
21
- const pluginRoot = path.resolve(__dirname, '..');
22
- const mcpServerPath = path.resolve(process.env.MICROI_MCP_SERVER_PATH || path.join(__dirname, 'mcp-server.js'));
23
- const brokerScriptPath = path.resolve(process.env.MICROI_CODEX_BROKER_SCRIPT || path.join(__dirname, 'microi-codex-broker.js'));
24
- const packagePath = path.join(pluginRoot, 'package.json');
25
- const packageVersion = String(readJson(packagePath, {}).version || '').trim();
26
- const environmentVersion = String(process.env.MICROI_TOOL_VERSION || '').trim();
27
- // An installed plugin is self-describing. A stale parent MCP environment must
28
- // never force a newer package to reconnect to an older shared broker. The
29
- // version environment remains authoritative for the package-less stable CLI
30
- // runtime, whose current.json launcher selects one immutable version folder.
31
- const pluginVersion = /^\d+(?:\.\d+){2}$/.test(packageVersion)
32
- ? packageVersion
33
- : (/^\d+(?:\.\d+){2}$/.test(environmentVersion) ? environmentVersion : '0.0.0');
34
- let inputBuffer = '';
35
- let requestSequence = 0;
36
- let shuttingDown = false;
37
- let brokerClient = null;
38
-
39
- function readJson(filePath, fallback) {
40
- try {
41
- const parsed = JSON.parse(fs.readFileSync(filePath, 'utf8'));
42
- return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : fallback;
43
- } catch {
44
- return fallback;
45
- }
46
- }
47
-
48
- function normalizeUrl(value) {
49
- return String(value || '').trim().replace(/\/+$/, '');
50
- }
51
-
52
- function safeName(value) {
53
- return String(value || '').normalize('NFKD').replace(/[^\x00-\x7F]/g, '')
54
- .replace(/[^A-Za-z0-9_]+/g, '_').replace(/_+/g, '_')
55
- .replace(/^_+|_+$/g, '').toLowerCase().slice(0, 48);
56
- }
57
-
58
- function hostPart(profile) {
59
- try { return safeName(new URL(profile.apiBaseUrl).host); } catch { return safeName(profile.apiBaseUrl); }
60
- }
61
-
62
- function profileIdentity(profile) {
63
- return [
64
- normalizeUrl(profile.apiBaseUrl),
65
- String(profile.osClient || '').trim(),
66
- String(profile.osClientType || '').trim(),
67
- String(profile.osClientNetwork || '').trim(),
68
- ].join('|');
69
- }
70
-
71
- function candidateRoots() {
72
- const values = [
73
- process.env.MICROI_WORKSPACE_ROOT,
74
- process.env.CODEX_WORKSPACE_ROOT,
75
- process.env.INIT_CWD,
76
- process.cwd(),
77
- ].filter(Boolean).map(value => path.resolve(value));
78
- const roots = [];
79
- for (const start of values) {
80
- let current = start;
81
- while (current && !roots.includes(current)) {
82
- roots.push(current);
83
- const parent = path.dirname(current);
84
- if (parent === current) break;
85
- current = parent;
86
- }
87
- }
88
- return roots;
89
- }
90
-
91
- function resolveWorkspace() {
92
- const explicitConfig = process.env.MICROI_CONFIG_PATH
93
- ? path.resolve(process.env.MICROI_CONFIG_PATH)
94
- : '';
95
- const candidates = explicitConfig
96
- ? [explicitConfig]
97
- : candidateRoots().flatMap(root => [
98
- path.join(root, CONFIG_DIR, CONFIG_FILE),
99
- path.join(root, CONFIG_DIR.toLowerCase(), CONFIG_FILE),
100
- ]);
101
- const configPath = candidates.find(filePath => fs.existsSync(filePath));
102
- const fallbackRoot = path.resolve(process.env.MICROI_WORKSPACE_ROOT || process.cwd());
103
- if (!configPath) {
104
- return {
105
- workspaceRoot: fallbackRoot,
106
- syncRoot: path.join(fallbackRoot, CONFIG_DIR),
107
- configPath: path.join(fallbackRoot, CONFIG_DIR, CONFIG_FILE),
108
- tokenPath: path.join(fallbackRoot, CONFIG_DIR, TOKEN_FILE),
109
- config: {},
110
- };
111
- }
112
- const syncRoot = path.dirname(configPath);
113
- return {
114
- workspaceRoot: path.dirname(syncRoot),
115
- syncRoot,
116
- configPath,
117
- tokenPath: path.join(syncRoot, TOKEN_FILE),
118
- config: readJson(configPath, {}),
119
- };
120
- }
121
-
122
- function scheduleAutomaticUpdate() {
123
- if (process.env.MICROI_DISABLE_AUTO_UPDATE === '1') return;
124
- const cliPath = path.join(__dirname, 'microi-cli.js');
125
- if (!fs.existsSync(cliPath)) return;
126
- const workspace = resolveWorkspace();
127
- try {
128
- const child = childProcess.spawn(process.execPath, [
129
- cliPath,
130
- 'update',
131
- '--background',
132
- '--workspace',
133
- workspace.workspaceRoot,
134
- '--json',
135
- ], {
136
- cwd: pluginRoot,
137
- detached: true,
138
- windowsHide: true,
139
- stdio: 'ignore',
140
- env: { ...process.env },
141
- });
142
- // 不等待更新、不重启当前 Router。新版本仅供后续新进程使用。
143
- child.on('error', () => { /* 下一次启动自动重试 */ });
144
- child.unref();
145
- } catch {
146
- // 自动更新永远不能成为 MCP 启动门禁。
147
- }
148
- }
149
-
150
- function loadProfiles() {
151
- const workspace = resolveWorkspace();
152
- const configured = Array.isArray(workspace.config.profiles)
153
- ? workspace.config.profiles
154
- : [];
155
- const rawProfiles = configured.length > 0
156
- ? configured
157
- : (workspace.config.apiBaseUrl ? [{
158
- label: workspace.config.osClient || workspace.config.apiBaseUrl,
159
- apiBaseUrl: workspace.config.apiBaseUrl,
160
- osClient: workspace.config.osClient || '',
161
- }] : []);
162
- const tokens = readJson(workspace.tokenPath, {});
163
- const identityNames = rawProfiles.map(profile => safeName(profile.osClient || profile.label) || hostPart(profile) || 'default');
164
- const counts = identityNames.reduce((map, name) => map.set(name, (map.get(name) || 0) + 1), new Map());
165
- const used = new Set();
166
- const profiles = rawProfiles.filter(profile => /^https?:\/\//i.test(String(profile.apiBaseUrl || ''))).map((raw, index) => {
167
- const profile = {
168
- label: String(raw.label || raw.sysShortTitle || raw.sysTitle || raw.osClient || raw.apiBaseUrl || ''),
169
- apiBaseUrl: normalizeUrl(raw.apiBaseUrl),
170
- osClient: String(raw.osClient || '').trim(),
171
- osClientType: String(raw.osClientType || '').trim(),
172
- osClientNetwork: String(raw.osClientNetwork || '').trim(),
173
- };
174
- const identityName = identityNames[index];
175
- const host = hostPart(profile);
176
- const base = `microi_${identityName}`;
177
- let name = counts.get(identityName) > 1 && host && host !== identityName ? `${base}_${host}` : base;
178
- if (used.has(name) && host) name = `${base}_${host}`;
179
- let suffix = 2;
180
- while (used.has(name)) name = `${base}_${suffix++}`;
181
- used.add(name);
182
- const exactKey = profileIdentity(profile);
183
- const legacyKey = profile.osClient ? `${profile.apiBaseUrl}|${profile.osClient}` : profile.apiBaseUrl;
184
- return {
185
- ...profile,
186
- index: index + 1,
187
- name,
188
- identity: exactKey,
189
- loggedIn: typeof tokens[exactKey] === 'string' || typeof tokens[legacyKey] === 'string',
190
- workspaceRoot: workspace.workspaceRoot,
191
- syncRoot: workspace.syncRoot,
192
- tokenPath: workspace.tokenPath,
193
- };
194
- });
195
- return { workspace, profiles };
196
- }
197
-
198
- function publicProfile(profile) {
199
- return {
200
- index: profile.index,
201
- name: profile.name,
202
- label: profile.label,
203
- apiBaseUrl: profile.apiBaseUrl,
204
- osClient: profile.osClient,
205
- osClientType: profile.osClientType,
206
- osClientNetwork: profile.osClientNetwork,
207
- loggedIn: profile.loggedIn,
208
- };
209
- }
210
-
211
- function selectProfile(selector) {
212
- const state = loadProfiles();
213
- if (state.profiles.length === 0) {
214
- throw new Error(`当前工作区尚未初始化 Microi。请在 ${state.workspace.workspaceRoot} 运行 bundled CLI 的 init 命令。`);
215
- }
216
- const raw = String(selector || '').trim();
217
- if (raw) {
218
- const lower = raw.toLowerCase();
219
- const matches = state.profiles.filter(profile =>
220
- String(profile.index) === raw
221
- || profile.name.toLowerCase() === lower
222
- || profile.osClient.toLowerCase() === lower
223
- || profile.label.toLowerCase() === lower
224
- || profile.identity.toLowerCase() === lower
225
- );
226
- if (matches.length === 1) return matches[0];
227
- if (matches.length > 1) throw new Error(`连接选择“${raw}”存在歧义,请改用 profiles 返回的 name。`);
228
- throw new Error(`未找到连接“${raw}”。可用连接:${state.profiles.map(item => item.name).join(', ')}`);
229
- }
230
- const loggedIn = state.profiles.filter(profile => profile.loggedIn);
231
- if (loggedIn.length === 1) return loggedIn[0];
232
- if (state.profiles.length === 1) return state.profiles[0];
233
- throw new Error(`当前工作区有 ${state.profiles.length} 个 Microi 连接,请先调用 action="profiles",再传 profile=name。`);
234
- }
235
-
236
- function buildApplicationsDir(profile) {
237
- const title = profile.label || profile.apiBaseUrl || 'Microi';
238
- const serverDir = `${title} (${hostPart(profile) || 'server'})`.replace(/[<>:"/\\|?*]/g, '_');
239
- const osClientDir = [profile.osClient || 'default', profile.osClientType || 'default', profile.osClientNetwork || 'default']
240
- .join('.').replace(/[<>:"/\\|?*]/g, '_');
241
- return path.join(profile.syncRoot, serverDir, osClientDir, 'AI应用');
242
- }
243
-
244
- function brokerCoordinates(workspace) {
245
- const normalizedRoot = process.platform === 'win32'
246
- ? workspace.workspaceRoot.toLowerCase()
247
- : workspace.workspaceRoot;
248
- const key = crypto.createHash('sha256').update(`${normalizedRoot}\0${pluginVersion}`).digest('hex').slice(0, 24);
249
- const runtimeRoot = path.resolve(process.env.MICROI_RUNTIME_HOME || path.join(os.homedir(), '.microi', 'runtime'));
250
- const endpoint = process.env.MICROI_CODEX_BROKER_ENDPOINT || (process.platform === 'win32'
251
- ? `\\\\.\\pipe\\microi-codex-${key}`
252
- : path.join(runtimeRoot, 'brokers', `${key}.sock`));
253
- return {
254
- endpoint,
255
- leasePath: path.resolve(process.env.MICROI_CODEX_BROKER_LEASE || path.join(runtimeRoot, 'leases', `microi-codex-${key}.json`)),
256
- workspaceRoot: workspace.workspaceRoot,
257
- };
258
- }
259
-
260
- function wait(milliseconds) {
261
- return new Promise(resolve => setTimeout(resolve, milliseconds));
262
- }
263
-
264
- function readBrokerToken(leasePath) {
265
- const lease = readJson(leasePath, {});
266
- return typeof lease.token === 'string' ? lease.token : '';
267
- }
268
-
269
- class SharedBrokerClient {
270
- constructor(workspace) {
271
- this.coordinates = brokerCoordinates(workspace);
272
- this.pending = new Map();
273
- this.buffer = '';
274
- this.closed = false;
275
- this.spawnAttempted = false;
276
- this.ready = this.connect();
277
- }
278
-
279
- startBroker() {
280
- if (!fs.existsSync(brokerScriptPath)) {
281
- throw new Error(`Codex Plugin 缺少共享 Broker:${brokerScriptPath}`);
282
- }
283
- const child = childProcess.spawn(process.execPath, [brokerScriptPath], {
284
- cwd: pluginRoot,
285
- detached: true,
286
- windowsHide: true,
287
- stdio: 'ignore',
288
- env: {
289
- ...process.env,
290
- MICROI_CODEX_BROKER_ENDPOINT: this.coordinates.endpoint,
291
- MICROI_CODEX_BROKER_LEASE: this.coordinates.leasePath,
292
- MICROI_WORKSPACE_ROOT: this.coordinates.workspaceRoot,
293
- MICROI_MCP_SERVER_PATH: mcpServerPath,
294
- MICROI_TOOL_VERSION: pluginVersion,
295
- },
296
- });
297
- child.on('error', () => { /* connect retry reports the actionable failure */ });
298
- child.unref();
299
- }
300
-
301
- openSocket() {
302
- return new Promise((resolve, reject) => {
303
- const socket = net.createConnection(this.coordinates.endpoint);
304
- const fail = error => {
305
- socket.destroy();
306
- reject(error);
307
- };
308
- socket.once('error', fail);
309
- socket.once('connect', () => {
310
- socket.off('error', fail);
311
- this.socket = socket;
312
- socket.setEncoding('utf8');
313
- socket.on('data', chunk => this.onData(chunk));
314
- socket.on('error', error => this.rejectAll(error));
315
- socket.on('close', () => {
316
- this.closed = true;
317
- this.rejectAll(new Error('Microi shared broker disconnected'));
318
- });
319
- resolve();
320
- });
321
- });
322
- }
323
-
324
- async connect() {
325
- let lastError;
326
- for (let attempt = 0; attempt < 50; attempt += 1) {
327
- try {
328
- await this.openSocket();
329
- return;
330
- } catch (error) {
331
- lastError = error;
332
- if (!this.spawnAttempted || attempt === 15) {
333
- this.spawnAttempted = true;
334
- this.startBroker();
335
- }
336
- await wait(Math.min(40 + attempt * 12, 250));
337
- }
338
- }
339
- throw new Error(`无法连接 Microi 共享 Broker:${lastError instanceof Error ? lastError.message : String(lastError)}`);
340
- }
341
-
342
- onData(chunk) {
343
- this.buffer += chunk.toString('utf8');
344
- let newline;
345
- while ((newline = this.buffer.indexOf('\n')) >= 0) {
346
- const line = this.buffer.slice(0, newline).trim();
347
- this.buffer = this.buffer.slice(newline + 1);
348
- if (!line) continue;
349
- let message;
350
- try { message = JSON.parse(line); } catch { continue; }
351
- if (message.id == null) continue;
352
- const pending = this.pending.get(String(message.id));
353
- if (!pending) continue;
354
- this.pending.delete(String(message.id));
355
- if (message.error) pending.reject(new Error(typeof message.error === 'string' ? message.error : (message.error.message || JSON.stringify(message.error))));
356
- else pending.resolve(message.result);
357
- }
358
- }
359
-
360
- rejectAll(error) {
361
- for (const pending of this.pending.values()) pending.reject(error);
362
- this.pending.clear();
363
- }
364
-
365
- request(profile, method, params) {
366
- return (async () => {
367
- await this.ready;
368
- if (this.closed || !this.socket || this.socket.destroyed) throw new Error('Microi shared broker is unavailable');
369
- let auth = '';
370
- for (let attempt = 0; attempt < 10 && !auth; attempt += 1) {
371
- auth = readBrokerToken(this.coordinates.leasePath);
372
- if (!auth) await wait(25);
373
- }
374
- if (!auth) throw new Error('Microi shared broker lease is unavailable');
375
- const id = `router-${process.pid}-${++requestSequence}`;
376
- const response = new Promise((resolve, reject) => this.pending.set(id, { resolve, reject }));
377
- this.socket.write(`${JSON.stringify({
378
- id,
379
- auth,
380
- type: 'request',
381
- profile: { ...profile, applicationsDir: buildApplicationsDir(profile) },
382
- method,
383
- params,
384
- })}\n`);
385
- return response;
386
- })();
387
- }
388
-
389
- close() {
390
- try { this.socket?.end(); } catch { /* best effort */ }
391
- }
392
- }
393
-
394
- function sharedBroker(profile) {
395
- if (!brokerClient || brokerClient.closed) brokerClient = new SharedBrokerClient({ workspaceRoot: profile.workspaceRoot });
396
- return brokerClient;
397
- }
398
-
399
- function requestThroughBroker(profile, method, params) {
400
- return sharedBroker(profile).request(profile, method, params);
401
- }
402
-
403
- function resultText(value, isError = false) {
404
- return {
405
- content: [{ type: 'text', text: typeof value === 'string' ? value : JSON.stringify(value, null, 2) }],
406
- ...(isError ? { isError: true } : {}),
407
- };
408
- }
409
-
410
- function toolchainStatus(workspaceRoot) {
411
- // 状态文件可能由另一工作区的后台更新器写入,只回读有限的诊断字段,
412
- // 不透传日志/错误正文或未知字段,也不把安装版本冒充当前已加载版本。
413
- const statusPath = path.join(path.resolve(process.env.MICROI_UPDATE_HOME || path.join(os.homedir(), '.microi', 'updater')), 'status.json');
414
- let status = {};
415
- try {
416
- if (fs.statSync(statusPath).size <= 64 * 1024) status = readJson(statusPath, {});
417
- } catch { /* 缺失/损坏状态不阻断连接发现 */ }
418
- const version = value => /^\d+(?:\.\d+){2}$/.test(String(value || '')) ? String(value) : 'unknown';
419
- const states = ['scheduled', 'running', 'completed', 'completed-with-warnings', 'deferred'];
420
- const skills = readJson(path.join(workspaceRoot, 'microi.skills', '.microi-skills-manifest.json'), {});
421
- return {
422
- package: '@microi.net/cli',
423
- loadedVersion: pluginVersion,
424
- workspaceSkillsVersion: version(skills.bundleVersion),
425
- automaticUpdate: {
426
- enabled: process.env.MICROI_DISABLE_AUTO_UPDATE !== '1',
427
- statusPath,
428
- state: states.includes(status.state) ? status.state : 'unknown',
429
- latestObservedVersion: version(status.latestVersion),
430
- globalVersion: version(status.globalVersion),
431
- checkedAt: typeof status.attemptedAt === 'string' && /^\d{4}-\d{2}-\d{2}T[\d:.]+Z$/.test(status.attemptedAt) ? status.attemptedAt : null,
432
- // 共享状态只证明记录中的工作区;其它目录须单独 doctor 验证。
433
- appliesToWorkspace: typeof status.workspace === 'string' && path.resolve(status.workspace) === path.resolve(workspaceRoot),
434
- },
435
- };
436
- }
437
-
438
- function baseInstructions() {
439
- return `先调用 microi_codex action="profiles";多连接时把返回的 name 作为 profile。所有业务动作由同源 Microi MCP 工具处理。
440
- 每次新建或接续吾码任务必须先完整读取工作区 microi.skills/workspace-conventions/SKILL.md;尚未初始化时读取插件 skills/workspace-conventions/SKILL.md。同样强制执行其中的进度播报与非阻塞自动更新规范,适用于任何 AI 模型。
441
- 首次说明必须以“Microi吾码开发工具版本”报告实际版本、官方最新版本及自动更新/Skills/MCP 同步状态;profiles.toolchain.loadedVersion=${pluginVersion} 只代表本进程,历史 latestObservedVersion 必须附 checkedAt,未知项写检测中,禁止猜测全局 CLI 或 npm 最新版。
442
- 已有 CLI 时必须执行 microi update --background --workspace "<工作区绝对路径>" --json,自动升级 @microi.net/cli 到 npm 官方 latest 并同步 Skills/AI/MCP;不得等更新结束才继续工作,不终止或强制重载当前会话。用户明确关闭或宿主禁止执行时报告限制。`;
443
- }
444
-
445
- function toolCatalog() {
446
- return [{
447
- name: 'microi_codex',
448
- description: 'Microi吾码统一入口。每次吾码任务先用 action="profiles" 查看连接与 toolchain 版本/自动更新状态,并读取 workspace-conventions/SKILL.md 执行强制版本播报与后台升级;多连接时传 profile=name。其余 action 复用原工具验证、确认、审计和回读。',
449
- inputSchema: {
450
- type: 'object',
451
- additionalProperties: false,
452
- required: ['action'],
453
- properties: {
454
- profile: { type: 'string', description: '可选:profiles 返回的 name、序号、OsClient 或连接名称。多连接时必填。' },
455
- action: { type: 'string', description: 'profiles | list_tools | describe_tool | 任意原始 microi_* 工具名' },
456
- params: { type: 'object', additionalProperties: true, description: '所选 action 的参数。' },
457
- },
458
- },
459
- }];
460
- }
461
-
462
- async function callRouterTool(args) {
463
- const action = String(args?.action || '').trim();
464
- if (!action) return resultText('action 不能为空', true);
465
- if (action === 'profiles' || action === 'profile_list') {
466
- const state = loadProfiles();
467
- return resultText({
468
- workspace: state.workspace.workspaceRoot,
469
- configPath: state.workspace.configPath,
470
- count: state.profiles.length,
471
- profiles: state.profiles.map(publicProfile),
472
- toolchain: toolchainStatus(state.workspace.workspaceRoot),
473
- next: state.profiles.length > 1 ? '后续调用请传 profile=name。' : '后续调用可省略 profile。',
474
- });
475
- }
476
- try {
477
- const profile = selectProfile(args?.profile);
478
- return await requestThroughBroker(profile, 'tools/call', {
479
- name: 'microi_codex',
480
- arguments: { action, params: args?.params || {} },
481
- });
482
- } catch (error) {
483
- return resultText(`Microi Agentx 路由失败:${error instanceof Error ? error.message : String(error)}`, true);
484
- }
485
- }
486
-
487
- function resourceList() {
488
- const state = loadProfiles();
489
- return [{
490
- uri: 'microi://codex/profiles',
491
- name: 'microi_profiles',
492
- title: 'Microi connection profiles',
493
- description: '当前工作区 Microi 连接与登录状态。',
494
- mimeType: 'application/json',
495
- }, ...state.profiles.flatMap(profile => ['status', 'tools'].map(kind => ({
496
- uri: `microi://codex/${encodeURIComponent(profile.name)}/${kind}`,
497
- name: `microi_${profile.name}_${kind}`,
498
- title: `Microi ${profile.label || profile.name} ${kind}`,
499
- description: kind === 'status' ? '只读连接状态。' : '该连接的原始工具目录。',
500
- mimeType: 'application/json',
501
- })))];
502
- }
503
-
504
- async function readResource(uri) {
505
- if (uri === 'microi://codex/profiles') {
506
- const state = loadProfiles();
507
- return { contents: [{ uri, mimeType: 'application/json', text: JSON.stringify({ profiles: state.profiles.map(publicProfile), toolchain: toolchainStatus(state.workspace.workspaceRoot) }, null, 2) }] };
508
- }
509
- const match = /^microi:\/\/codex\/([^/]+)\/(status|tools|action\/([^/]+)\/(.*))$/.exec(uri);
510
- if (!match) throw new Error(`不支持的 Microi 资源 URI:${uri}`);
511
- const profile = selectProfile(decodeURIComponent(match[1]));
512
- let action;
513
- let params = {};
514
- if (match[2] === 'status') action = 'microi_get_status';
515
- else if (match[2] === 'tools') action = 'list_tools';
516
- else {
517
- action = decodeURIComponent(match[3]);
518
- const raw = decodeURIComponent(match[4] || '');
519
- params = raw ? JSON.parse(raw) : {};
520
- }
521
- const result = await requestThroughBroker(profile, 'tools/call', {
522
- name: 'microi_codex',
523
- arguments: { action, params },
524
- });
525
- return { contents: [{ uri, mimeType: 'application/json', text: JSON.stringify(result, null, 2) }] };
526
- }
527
-
528
- function send(message) {
529
- process.stdout.write(`${JSON.stringify(message)}\n`);
530
- }
531
-
532
- function sendResult(id, result) {
533
- send({ jsonrpc: '2.0', id, result });
534
- }
535
-
536
- function sendError(id, code, message) {
537
- send({ jsonrpc: '2.0', id, error: { code, message } });
538
- }
539
-
540
- async function handle(message) {
541
- if (!message || message.jsonrpc !== '2.0') return;
542
- const { id, method, params } = message;
543
- if (method === 'initialize') {
544
- sendResult(id, {
545
- protocolVersion: params?.protocolVersion || PROTOCOL_VERSION,
546
- capabilities: { tools: {}, resources: {} },
547
- serverInfo: { name: 'Microi-Codex-Plugin', version: pluginVersion },
548
- instructions: baseInstructions(),
549
- });
550
- return;
551
- }
552
- if (method === 'ping') { sendResult(id, {}); return; }
553
- if (method === 'tools/list') { sendResult(id, { tools: toolCatalog() }); return; }
554
- if (method === 'tools/call') {
555
- if (params?.name !== 'microi_codex') { sendResult(id, resultText(`Unknown tool: ${params?.name || '(empty)'}`, true)); return; }
556
- sendResult(id, await callRouterTool(params.arguments || {})); return;
557
- }
558
- if (method === 'resources/list') { sendResult(id, { resources: resourceList() }); return; }
559
- if (method === 'resources/templates/list') {
560
- sendResult(id, { resourceTemplates: [{
561
- uriTemplate: 'microi://codex/{profile}/action/{action}/{params}',
562
- name: 'microi_codex_action',
563
- title: 'Microi action fallback',
564
- description: '工具未注入时,按连接路由到同一原始 Microi 工具。params 是 URI 编码 JSON 对象。',
565
- mimeType: 'application/json',
566
- }] });
567
- return;
568
- }
569
- if (method === 'resources/read') {
570
- try { sendResult(id, await readResource(String(params?.uri || ''))); }
571
- catch (error) { sendError(id, -32002, error instanceof Error ? error.message : String(error)); }
572
- return;
573
- }
574
- if (method === 'shutdown') { sendResult(id, {}); shutdown(); return; }
575
- if (method?.startsWith('notifications/')) return;
576
- if (id != null) sendError(id, -32601, `Method not found: ${method}`);
577
- }
578
-
579
- function shutdown() {
580
- if (shuttingDown) return;
581
- shuttingDown = true;
582
- brokerClient?.close();
583
- brokerClient = null;
584
- }
585
-
586
- process.stdin.setEncoding('utf8');
587
- const automaticUpdateTimer = setTimeout(scheduleAutomaticUpdate, 1500);
588
- automaticUpdateTimer.unref();
589
- process.stdin.on('data', chunk => {
590
- inputBuffer += chunk;
591
- let newline;
592
- while ((newline = inputBuffer.indexOf('\n')) >= 0) {
593
- const line = inputBuffer.slice(0, newline).trim();
594
- inputBuffer = inputBuffer.slice(newline + 1);
595
- if (!line) continue;
596
- let message;
597
- try { message = JSON.parse(line); }
598
- catch { continue; }
599
- Promise.resolve(handle(message)).catch(error => {
600
- if (message.id != null) sendError(message.id, -32603, error instanceof Error ? error.message : String(error));
601
- });
602
- }
603
- });
604
- process.stdin.on('end', () => { shutdown(); process.exit(0); });
605
- process.stdin.on('error', () => { shutdown(); process.exit(1); });
606
- process.stdout.on('error', error => { shutdown(); process.exit(error?.code === 'EPIPE' ? 0 : 1); });
607
- process.on('disconnect', () => { shutdown(); process.exit(0); });
608
- for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP']) process.on(signal, () => { shutdown(); process.exit(0); });
609
- process.on('exit', shutdown);
610
-
611
- const parentPid = process.ppid;
612
- if (parentPid > 1) {
613
- const parentWatchTimer = setInterval(() => {
614
- try { process.kill(parentPid, 0); }
615
- catch (error) { if (error?.code === 'ESRCH') { shutdown(); process.exit(0); } }
616
- }, 5000);
617
- parentWatchTimer.unref();
618
- }
1
+ #!/usr/bin/env node
2
+ /*
3
+ * Microi Agentx Plugin 多连接路由器。
4
+ *
5
+ * 它只负责发现 VS Code/CLI 共用的工作区配置、选择连接并把请求转发给
6
+ * 同一份 Microi MCP bundle。所有业务 schema、确认口令、审计和写后回读
7
+ * 都由原 MCP 工具执行,本文件不实现第二套远端 API。
8
+ */
9
+
10
+ const childProcess = require('node:child_process');
11
+ const crypto = require('node:crypto');
12
+ const fs = require('node:fs');
13
+ const net = require('node:net');
14
+ const os = require('node:os');
15
+ const path = require('node:path');
16
+
17
+ const CONFIG_DIR = 'Microi-V8-Engine';
18
+ const CONFIG_FILE = '.microi-config.json';
19
+ const TOKEN_FILE = '.microi-mcp-tokens.json';
20
+ const PROTOCOL_VERSION = '2024-11-05';
21
+ const pluginRoot = path.resolve(__dirname, '..');
22
+ const mcpServerPath = path.resolve(process.env.MICROI_MCP_SERVER_PATH || path.join(__dirname, 'mcp-server.js'));
23
+ const brokerScriptPath = path.resolve(process.env.MICROI_CODEX_BROKER_SCRIPT || path.join(__dirname, 'microi-codex-broker.js'));
24
+ const packagePath = path.join(pluginRoot, 'package.json');
25
+ const packageVersion = String(readJson(packagePath, {}).version || '').trim();
26
+ const environmentVersion = String(process.env.MICROI_TOOL_VERSION || '').trim();
27
+ // An installed plugin is self-describing. A stale parent MCP environment must
28
+ // never force a newer package to reconnect to an older shared broker. The
29
+ // version environment remains authoritative for the package-less stable CLI
30
+ // runtime, whose current.json launcher selects one immutable version folder.
31
+ const pluginVersion = /^\d+(?:\.\d+){2}$/.test(packageVersion)
32
+ ? packageVersion
33
+ : (/^\d+(?:\.\d+){2}$/.test(environmentVersion) ? environmentVersion : '0.0.0');
34
+ let inputBuffer = '';
35
+ let requestSequence = 0;
36
+ let shuttingDown = false;
37
+ let brokerClient = null;
38
+
39
+ function readJson(filePath, fallback) {
40
+ try {
41
+ const parsed = JSON.parse(fs.readFileSync(filePath, 'utf8'));
42
+ return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : fallback;
43
+ } catch {
44
+ return fallback;
45
+ }
46
+ }
47
+
48
+ function normalizeUrl(value) {
49
+ return String(value || '').trim().replace(/\/+$/, '');
50
+ }
51
+
52
+ function safeName(value) {
53
+ return String(value || '').normalize('NFKD').replace(/[^\x00-\x7F]/g, '')
54
+ .replace(/[^A-Za-z0-9_]+/g, '_').replace(/_+/g, '_')
55
+ .replace(/^_+|_+$/g, '').toLowerCase().slice(0, 48);
56
+ }
57
+
58
+ function hostPart(profile) {
59
+ try { return safeName(new URL(profile.apiBaseUrl).host); } catch { return safeName(profile.apiBaseUrl); }
60
+ }
61
+
62
+ function profileIdentity(profile) {
63
+ return [
64
+ normalizeUrl(profile.apiBaseUrl),
65
+ String(profile.osClient || '').trim(),
66
+ String(profile.osClientType || '').trim(),
67
+ String(profile.osClientNetwork || '').trim(),
68
+ ].join('|');
69
+ }
70
+
71
+ function candidateRoots() {
72
+ const values = [
73
+ process.env.MICROI_WORKSPACE_ROOT,
74
+ process.env.CODEX_WORKSPACE_ROOT,
75
+ process.env.INIT_CWD,
76
+ process.cwd(),
77
+ ].filter(Boolean).map(value => path.resolve(value));
78
+ const roots = [];
79
+ for (const start of values) {
80
+ let current = start;
81
+ while (current && !roots.includes(current)) {
82
+ roots.push(current);
83
+ const parent = path.dirname(current);
84
+ if (parent === current) break;
85
+ current = parent;
86
+ }
87
+ }
88
+ return roots;
89
+ }
90
+
91
+ function resolveWorkspace() {
92
+ const explicitConfig = process.env.MICROI_CONFIG_PATH
93
+ ? path.resolve(process.env.MICROI_CONFIG_PATH)
94
+ : '';
95
+ const candidates = explicitConfig
96
+ ? [explicitConfig]
97
+ : candidateRoots().flatMap(root => [
98
+ path.join(root, CONFIG_DIR, CONFIG_FILE),
99
+ path.join(root, CONFIG_DIR.toLowerCase(), CONFIG_FILE),
100
+ ]);
101
+ const configPath = candidates.find(filePath => fs.existsSync(filePath));
102
+ const fallbackRoot = path.resolve(process.env.MICROI_WORKSPACE_ROOT || process.cwd());
103
+ if (!configPath) {
104
+ return {
105
+ workspaceRoot: fallbackRoot,
106
+ syncRoot: path.join(fallbackRoot, CONFIG_DIR),
107
+ configPath: path.join(fallbackRoot, CONFIG_DIR, CONFIG_FILE),
108
+ tokenPath: path.join(fallbackRoot, CONFIG_DIR, TOKEN_FILE),
109
+ config: {},
110
+ };
111
+ }
112
+ const syncRoot = path.dirname(configPath);
113
+ return {
114
+ workspaceRoot: path.dirname(syncRoot),
115
+ syncRoot,
116
+ configPath,
117
+ tokenPath: path.join(syncRoot, TOKEN_FILE),
118
+ config: readJson(configPath, {}),
119
+ };
120
+ }
121
+
122
+ function scheduleAutomaticUpdate() {
123
+ if (process.env.MICROI_DISABLE_AUTO_UPDATE === '1') return;
124
+ const cliPath = path.join(__dirname, 'microi-cli.js');
125
+ if (!fs.existsSync(cliPath)) return;
126
+ const workspace = resolveWorkspace();
127
+ try {
128
+ const child = childProcess.spawn(process.execPath, [
129
+ cliPath,
130
+ 'update',
131
+ '--background',
132
+ '--workspace',
133
+ workspace.workspaceRoot,
134
+ '--json',
135
+ ], {
136
+ cwd: pluginRoot,
137
+ detached: true,
138
+ windowsHide: true,
139
+ stdio: 'ignore',
140
+ env: { ...process.env },
141
+ });
142
+ // 不等待更新、不重启当前 Router。新版本仅供后续新进程使用。
143
+ child.on('error', () => { /* 下一次启动自动重试 */ });
144
+ child.unref();
145
+ } catch {
146
+ // 自动更新永远不能成为 MCP 启动门禁。
147
+ }
148
+ }
149
+
150
+ function loadProfiles() {
151
+ const workspace = resolveWorkspace();
152
+ const configured = Array.isArray(workspace.config.profiles)
153
+ ? workspace.config.profiles
154
+ : [];
155
+ const rawProfiles = configured.length > 0
156
+ ? configured
157
+ : (workspace.config.apiBaseUrl ? [{
158
+ label: workspace.config.osClient || workspace.config.apiBaseUrl,
159
+ apiBaseUrl: workspace.config.apiBaseUrl,
160
+ osClient: workspace.config.osClient || '',
161
+ }] : []);
162
+ const tokens = readJson(workspace.tokenPath, {});
163
+ const identityNames = rawProfiles.map(profile => safeName(profile.osClient || profile.label) || hostPart(profile) || 'default');
164
+ const counts = identityNames.reduce((map, name) => map.set(name, (map.get(name) || 0) + 1), new Map());
165
+ const used = new Set();
166
+ const profiles = rawProfiles.filter(profile => /^https?:\/\//i.test(String(profile.apiBaseUrl || ''))).map((raw, index) => {
167
+ const profile = {
168
+ label: String(raw.label || raw.sysShortTitle || raw.sysTitle || raw.osClient || raw.apiBaseUrl || ''),
169
+ apiBaseUrl: normalizeUrl(raw.apiBaseUrl),
170
+ osClient: String(raw.osClient || '').trim(),
171
+ osClientType: String(raw.osClientType || '').trim(),
172
+ osClientNetwork: String(raw.osClientNetwork || '').trim(),
173
+ };
174
+ const identityName = identityNames[index];
175
+ const host = hostPart(profile);
176
+ const base = `microi_${identityName}`;
177
+ let name = counts.get(identityName) > 1 && host && host !== identityName ? `${base}_${host}` : base;
178
+ if (used.has(name) && host) name = `${base}_${host}`;
179
+ let suffix = 2;
180
+ while (used.has(name)) name = `${base}_${suffix++}`;
181
+ used.add(name);
182
+ const exactKey = profileIdentity(profile);
183
+ const legacyKey = profile.osClient ? `${profile.apiBaseUrl}|${profile.osClient}` : profile.apiBaseUrl;
184
+ return {
185
+ ...profile,
186
+ index: index + 1,
187
+ name,
188
+ identity: exactKey,
189
+ loggedIn: typeof tokens[exactKey] === 'string' || typeof tokens[legacyKey] === 'string',
190
+ workspaceRoot: workspace.workspaceRoot,
191
+ syncRoot: workspace.syncRoot,
192
+ tokenPath: workspace.tokenPath,
193
+ };
194
+ });
195
+ return { workspace, profiles };
196
+ }
197
+
198
+ function publicProfile(profile) {
199
+ return {
200
+ index: profile.index,
201
+ name: profile.name,
202
+ label: profile.label,
203
+ apiBaseUrl: profile.apiBaseUrl,
204
+ osClient: profile.osClient,
205
+ osClientType: profile.osClientType,
206
+ osClientNetwork: profile.osClientNetwork,
207
+ loggedIn: profile.loggedIn,
208
+ };
209
+ }
210
+
211
+ function selectProfile(selector) {
212
+ const state = loadProfiles();
213
+ if (state.profiles.length === 0) {
214
+ throw new Error(`当前工作区尚未初始化 Microi。请在 ${state.workspace.workspaceRoot} 运行 bundled CLI 的 init 命令。`);
215
+ }
216
+ const raw = String(selector || '').trim();
217
+ if (raw) {
218
+ const lower = raw.toLowerCase();
219
+ const matches = state.profiles.filter(profile =>
220
+ String(profile.index) === raw
221
+ || profile.name.toLowerCase() === lower
222
+ || profile.osClient.toLowerCase() === lower
223
+ || profile.label.toLowerCase() === lower
224
+ || profile.identity.toLowerCase() === lower
225
+ );
226
+ if (matches.length === 1) return matches[0];
227
+ if (matches.length > 1) throw new Error(`连接选择“${raw}”存在歧义,请改用 profiles 返回的 name。`);
228
+ throw new Error(`未找到连接“${raw}”。可用连接:${state.profiles.map(item => item.name).join(', ')}`);
229
+ }
230
+ const loggedIn = state.profiles.filter(profile => profile.loggedIn);
231
+ if (loggedIn.length === 1) return loggedIn[0];
232
+ if (state.profiles.length === 1) return state.profiles[0];
233
+ throw new Error(`当前工作区有 ${state.profiles.length} 个 Microi 连接,请先调用 action="profiles",再传 profile=name。`);
234
+ }
235
+
236
+ function buildApplicationsDir(profile) {
237
+ const title = profile.label || profile.apiBaseUrl || 'Microi';
238
+ const serverDir = `${title} (${hostPart(profile) || 'server'})`.replace(/[<>:"/\\|?*]/g, '_');
239
+ const osClientDir = [profile.osClient || 'default', profile.osClientType || 'default', profile.osClientNetwork || 'default']
240
+ .join('.').replace(/[<>:"/\\|?*]/g, '_');
241
+ return path.join(profile.syncRoot, serverDir, osClientDir, 'AI应用');
242
+ }
243
+
244
+ function brokerCoordinates(workspace) {
245
+ const normalizedRoot = process.platform === 'win32'
246
+ ? workspace.workspaceRoot.toLowerCase()
247
+ : workspace.workspaceRoot;
248
+ const key = crypto.createHash('sha256').update(`${normalizedRoot}\0${pluginVersion}`).digest('hex').slice(0, 24);
249
+ const runtimeRoot = path.resolve(process.env.MICROI_RUNTIME_HOME || path.join(os.homedir(), '.microi', 'runtime'));
250
+ const endpoint = process.env.MICROI_CODEX_BROKER_ENDPOINT || (process.platform === 'win32'
251
+ ? `\\\\.\\pipe\\microi-codex-${key}`
252
+ : path.join(runtimeRoot, 'brokers', `${key}.sock`));
253
+ return {
254
+ endpoint,
255
+ leasePath: path.resolve(process.env.MICROI_CODEX_BROKER_LEASE || path.join(runtimeRoot, 'leases', `microi-codex-${key}.json`)),
256
+ workspaceRoot: workspace.workspaceRoot,
257
+ };
258
+ }
259
+
260
+ function wait(milliseconds) {
261
+ return new Promise(resolve => setTimeout(resolve, milliseconds));
262
+ }
263
+
264
+ function readBrokerToken(leasePath) {
265
+ const lease = readJson(leasePath, {});
266
+ return typeof lease.token === 'string' ? lease.token : '';
267
+ }
268
+
269
+ class SharedBrokerClient {
270
+ constructor(workspace) {
271
+ this.coordinates = brokerCoordinates(workspace);
272
+ this.pending = new Map();
273
+ this.buffer = '';
274
+ this.closed = false;
275
+ this.spawnAttempted = false;
276
+ this.ready = this.connect();
277
+ }
278
+
279
+ startBroker() {
280
+ if (!fs.existsSync(brokerScriptPath)) {
281
+ throw new Error(`Codex Plugin 缺少共享 Broker:${brokerScriptPath}`);
282
+ }
283
+ const testDiagnostics = process.env.MICROI_CODEX_TEST_DIAGNOSTICS === '1';
284
+ const child = childProcess.spawn(process.execPath, [brokerScriptPath], {
285
+ cwd: pluginRoot,
286
+ detached: true,
287
+ windowsHide: true,
288
+ stdio: testDiagnostics ? ['ignore', 'ignore', 'pipe'] : 'ignore',
289
+ env: {
290
+ ...process.env,
291
+ MICROI_CODEX_BROKER_ENDPOINT: this.coordinates.endpoint,
292
+ MICROI_CODEX_BROKER_LEASE: this.coordinates.leasePath,
293
+ MICROI_WORKSPACE_ROOT: this.coordinates.workspaceRoot,
294
+ MICROI_MCP_SERVER_PATH: mcpServerPath,
295
+ MICROI_TOOL_VERSION: pluginVersion,
296
+ },
297
+ });
298
+ // Keep production brokers detached. Smoke tests may opt into bounded stderr
299
+ // so a child boot failure cannot be reduced to an unexplained write EPIPE.
300
+ if (testDiagnostics && child.stderr) {
301
+ let remaining = 65536;
302
+ child.stderr.on('data', chunk => {
303
+ if (remaining <= 0) return;
304
+ const bytes = chunk.subarray(0, remaining); remaining -= bytes.length;
305
+ process.stderr.write(bytes);
306
+ });
307
+ child.stderr.unref?.();
308
+ }
309
+ child.on('error', () => { /* connect retry reports the actionable failure */ });
310
+ child.unref();
311
+ }
312
+
313
+ openSocket() {
314
+ return new Promise((resolve, reject) => {
315
+ const diagnostic = message => { if (process.env.MICROI_CODEX_TEST_DIAGNOSTICS === '1' && process.env.MICROI_CODEX_TEST_LOG) { try { fs.appendFileSync(process.env.MICROI_CODEX_TEST_LOG, `${new Date().toISOString()} router ${message}\n`); } catch {} } };
316
+ diagnostic(`connect ${JSON.stringify(this.coordinates.endpoint)}`);
317
+ const socket = net.createConnection(this.coordinates.endpoint);
318
+ const fail = error => {
319
+ diagnostic(`connect failed ${error.code}`);
320
+ socket.destroy();
321
+ reject(error);
322
+ };
323
+ socket.once('error', fail);
324
+ socket.once('connect', () => {
325
+ diagnostic('connected');
326
+ socket.off('error', fail);
327
+ this.socket = socket;
328
+ socket.setEncoding('utf8');
329
+ socket.on('data', chunk => this.onData(chunk));
330
+ socket.on('error', error => this.rejectAll(error));
331
+ socket.on('close', () => {
332
+ this.closed = true;
333
+ this.rejectAll(new Error('Microi shared broker disconnected'));
334
+ });
335
+ resolve();
336
+ });
337
+ });
338
+ }
339
+
340
+ async connect() {
341
+ let lastError;
342
+ for (let attempt = 0; attempt < 50; attempt += 1) {
343
+ try {
344
+ await this.openSocket();
345
+ return;
346
+ } catch (error) {
347
+ lastError = error;
348
+ if (!this.spawnAttempted || attempt === 15) {
349
+ this.spawnAttempted = true;
350
+ this.startBroker();
351
+ }
352
+ await wait(Math.min(40 + attempt * 12, 250));
353
+ }
354
+ }
355
+ throw new Error(`无法连接 Microi 共享 Broker:${lastError instanceof Error ? lastError.message : String(lastError)}`);
356
+ }
357
+
358
+ onData(chunk) {
359
+ this.buffer += chunk.toString('utf8');
360
+ let newline;
361
+ while ((newline = this.buffer.indexOf('\n')) >= 0) {
362
+ const line = this.buffer.slice(0, newline).trim();
363
+ this.buffer = this.buffer.slice(newline + 1);
364
+ if (!line) continue;
365
+ let message;
366
+ try { message = JSON.parse(line); } catch { continue; }
367
+ if (message.id == null) continue;
368
+ const pending = this.pending.get(String(message.id));
369
+ if (!pending) continue;
370
+ this.pending.delete(String(message.id));
371
+ if (message.error) pending.reject(new Error(typeof message.error === 'string' ? message.error : (message.error.message || JSON.stringify(message.error))));
372
+ else pending.resolve(message.result);
373
+ }
374
+ }
375
+
376
+ rejectAll(error) {
377
+ for (const pending of this.pending.values()) pending.reject(error);
378
+ this.pending.clear();
379
+ }
380
+
381
+ request(profile, method, params) {
382
+ return (async () => {
383
+ await this.ready;
384
+ if (this.closed || !this.socket || this.socket.destroyed) throw new Error('Microi shared broker is unavailable');
385
+ let auth = '';
386
+ for (let attempt = 0; attempt < 10 && !auth; attempt += 1) {
387
+ auth = readBrokerToken(this.coordinates.leasePath);
388
+ if (!auth) await wait(25);
389
+ }
390
+ if (!auth) throw new Error('Microi shared broker lease is unavailable');
391
+ const id = `router-${process.pid}-${++requestSequence}`;
392
+ const response = new Promise((resolve, reject) => this.pending.set(id, { resolve, reject }));
393
+ this.socket.write(`${JSON.stringify({
394
+ id,
395
+ auth,
396
+ type: 'request',
397
+ profile: { ...profile, applicationsDir: buildApplicationsDir(profile) },
398
+ method,
399
+ params,
400
+ })}\n`);
401
+ return response;
402
+ })();
403
+ }
404
+
405
+ close() {
406
+ try { this.socket?.end(); } catch { /* best effort */ }
407
+ }
408
+ }
409
+
410
+ function sharedBroker(profile) {
411
+ if (!brokerClient || brokerClient.closed) brokerClient = new SharedBrokerClient({ workspaceRoot: profile.workspaceRoot });
412
+ return brokerClient;
413
+ }
414
+
415
+ function requestThroughBroker(profile, method, params) {
416
+ return sharedBroker(profile).request(profile, method, params);
417
+ }
418
+
419
+ function resultText(value, isError = false) {
420
+ return {
421
+ content: [{ type: 'text', text: typeof value === 'string' ? value : JSON.stringify(value, null, 2) }],
422
+ ...(isError ? { isError: true } : {}),
423
+ };
424
+ }
425
+
426
+ function toolchainStatus(workspaceRoot) {
427
+ // 状态文件可能由另一工作区的后台更新器写入,只回读有限的诊断字段,
428
+ // 不透传日志/错误正文或未知字段,也不把安装版本冒充当前已加载版本。
429
+ const statusPath = path.join(path.resolve(process.env.MICROI_UPDATE_HOME || path.join(os.homedir(), '.microi', 'updater')), 'status.json');
430
+ let status = {};
431
+ try {
432
+ if (fs.statSync(statusPath).size <= 64 * 1024) status = readJson(statusPath, {});
433
+ } catch { /* 缺失/损坏状态不阻断连接发现 */ }
434
+ const version = value => /^\d+(?:\.\d+){2}$/.test(String(value || '')) ? String(value) : 'unknown';
435
+ const states = ['scheduled', 'running', 'completed', 'completed-with-warnings', 'deferred'];
436
+ const skills = readJson(path.join(workspaceRoot, 'microi.skills', '.microi-skills-manifest.json'), {});
437
+ return {
438
+ package: '@microi.net/cli',
439
+ loadedVersion: pluginVersion,
440
+ workspaceSkillsVersion: version(skills.bundleVersion),
441
+ automaticUpdate: {
442
+ enabled: process.env.MICROI_DISABLE_AUTO_UPDATE !== '1',
443
+ statusPath,
444
+ state: states.includes(status.state) ? status.state : 'unknown',
445
+ latestObservedVersion: version(status.latestVersion),
446
+ globalVersion: version(status.globalVersion),
447
+ checkedAt: typeof status.attemptedAt === 'string' && /^\d{4}-\d{2}-\d{2}T[\d:.]+Z$/.test(status.attemptedAt) ? status.attemptedAt : null,
448
+ // 共享状态只证明记录中的工作区;其它目录须单独 doctor 验证。
449
+ appliesToWorkspace: typeof status.workspace === 'string' && path.resolve(status.workspace) === path.resolve(workspaceRoot),
450
+ },
451
+ };
452
+ }
453
+
454
+ function baseInstructions() {
455
+ return `先调用 microi_codex action="profiles";多连接时把返回的 name 作为 profile。所有业务动作由同源 Microi MCP 工具处理。
456
+ 每次新建或接续吾码任务必须先完整读取工作区 microi.skills/workspace-conventions/SKILL.md;尚未初始化时读取插件 skills/workspace-conventions/SKILL.md。同样强制执行其中的进度播报与非阻塞自动更新规范,适用于任何 AI 模型。
457
+ 首次说明必须以“Microi吾码开发工具版本”报告实际版本、官方最新版本及自动更新/Skills/MCP 同步状态;profiles.toolchain.loadedVersion=${pluginVersion} 只代表本进程,历史 latestObservedVersion 必须附 checkedAt,未知项写检测中,禁止猜测全局 CLI 或 npm 最新版。
458
+ 已有 CLI 时必须执行 microi update --background --workspace "<工作区绝对路径>" --json,自动升级 @microi.net/cli 到 npm 官方 latest 并同步 Skills/AI/MCP;不得等更新结束才继续工作,不终止或强制重载当前会话。用户明确关闭或宿主禁止执行时报告限制。`;
459
+ }
460
+
461
+ function toolCatalog() {
462
+ return [{
463
+ name: 'microi_codex',
464
+ description: 'Microi吾码统一入口。每次吾码任务先用 action="profiles" 查看连接与 toolchain 版本/自动更新状态,并读取 workspace-conventions/SKILL.md 执行强制版本播报与后台升级;多连接时传 profile=name。其余 action 复用原工具验证、确认、审计和回读。',
465
+ inputSchema: {
466
+ type: 'object',
467
+ additionalProperties: false,
468
+ required: ['action'],
469
+ properties: {
470
+ profile: { type: 'string', description: '可选:profiles 返回的 name、序号、OsClient 或连接名称。多连接时必填。' },
471
+ action: { type: 'string', description: 'profiles | list_tools | describe_tool | 任意原始 microi_* 工具名' },
472
+ params: { type: 'object', additionalProperties: true, description: '所选 action 的参数。' },
473
+ },
474
+ },
475
+ }];
476
+ }
477
+
478
+ async function callRouterTool(args) {
479
+ const action = String(args?.action || '').trim();
480
+ if (!action) return resultText('action 不能为空', true);
481
+ if (action === 'profiles' || action === 'profile_list') {
482
+ const state = loadProfiles();
483
+ return resultText({
484
+ workspace: state.workspace.workspaceRoot,
485
+ configPath: state.workspace.configPath,
486
+ count: state.profiles.length,
487
+ profiles: state.profiles.map(publicProfile),
488
+ toolchain: toolchainStatus(state.workspace.workspaceRoot),
489
+ next: state.profiles.length > 1 ? '后续调用请传 profile=name。' : '后续调用可省略 profile。',
490
+ });
491
+ }
492
+ try {
493
+ const profile = selectProfile(args?.profile);
494
+ return await requestThroughBroker(profile, 'tools/call', {
495
+ name: 'microi_codex',
496
+ arguments: { action, params: args?.params || {} },
497
+ });
498
+ } catch (error) {
499
+ return resultText(`Microi Agentx 路由失败:${error instanceof Error ? error.message : String(error)}`, true);
500
+ }
501
+ }
502
+
503
+ function resourceList() {
504
+ const state = loadProfiles();
505
+ return [{
506
+ uri: 'microi://codex/profiles',
507
+ name: 'microi_profiles',
508
+ title: 'Microi connection profiles',
509
+ description: '当前工作区 Microi 连接与登录状态。',
510
+ mimeType: 'application/json',
511
+ }, ...state.profiles.flatMap(profile => ['status', 'tools'].map(kind => ({
512
+ uri: `microi://codex/${encodeURIComponent(profile.name)}/${kind}`,
513
+ name: `microi_${profile.name}_${kind}`,
514
+ title: `Microi ${profile.label || profile.name} ${kind}`,
515
+ description: kind === 'status' ? '只读连接状态。' : '该连接的原始工具目录。',
516
+ mimeType: 'application/json',
517
+ })))];
518
+ }
519
+
520
+ async function readResource(uri) {
521
+ if (uri === 'microi://codex/profiles') {
522
+ const state = loadProfiles();
523
+ return { contents: [{ uri, mimeType: 'application/json', text: JSON.stringify({ profiles: state.profiles.map(publicProfile), toolchain: toolchainStatus(state.workspace.workspaceRoot) }, null, 2) }] };
524
+ }
525
+ const match = /^microi:\/\/codex\/([^/]+)\/(status|tools|action\/([^/]+)\/(.*))$/.exec(uri);
526
+ if (!match) throw new Error(`不支持的 Microi 资源 URI:${uri}`);
527
+ const profile = selectProfile(decodeURIComponent(match[1]));
528
+ let action;
529
+ let params = {};
530
+ if (match[2] === 'status') action = 'microi_get_status';
531
+ else if (match[2] === 'tools') action = 'list_tools';
532
+ else {
533
+ action = decodeURIComponent(match[3]);
534
+ const raw = decodeURIComponent(match[4] || '');
535
+ params = raw ? JSON.parse(raw) : {};
536
+ }
537
+ const result = await requestThroughBroker(profile, 'tools/call', {
538
+ name: 'microi_codex',
539
+ arguments: { action, params },
540
+ });
541
+ return { contents: [{ uri, mimeType: 'application/json', text: JSON.stringify(result, null, 2) }] };
542
+ }
543
+
544
+ function send(message) {
545
+ process.stdout.write(`${JSON.stringify(message)}\n`);
546
+ }
547
+
548
+ function sendResult(id, result) {
549
+ send({ jsonrpc: '2.0', id, result });
550
+ }
551
+
552
+ function sendError(id, code, message) {
553
+ send({ jsonrpc: '2.0', id, error: { code, message } });
554
+ }
555
+
556
+ async function handle(message) {
557
+ if (!message || message.jsonrpc !== '2.0') return;
558
+ const { id, method, params } = message;
559
+ if (method === 'initialize') {
560
+ sendResult(id, {
561
+ protocolVersion: params?.protocolVersion || PROTOCOL_VERSION,
562
+ capabilities: { tools: {}, resources: {} },
563
+ serverInfo: { name: 'Microi-Codex-Plugin', version: pluginVersion },
564
+ instructions: baseInstructions(),
565
+ });
566
+ return;
567
+ }
568
+ if (method === 'ping') { sendResult(id, {}); return; }
569
+ if (method === 'tools/list') { sendResult(id, { tools: toolCatalog() }); return; }
570
+ if (method === 'tools/call') {
571
+ if (params?.name !== 'microi_codex') { sendResult(id, resultText(`Unknown tool: ${params?.name || '(empty)'}`, true)); return; }
572
+ sendResult(id, await callRouterTool(params.arguments || {})); return;
573
+ }
574
+ if (method === 'resources/list') { sendResult(id, { resources: resourceList() }); return; }
575
+ if (method === 'resources/templates/list') {
576
+ sendResult(id, { resourceTemplates: [{
577
+ uriTemplate: 'microi://codex/{profile}/action/{action}/{params}',
578
+ name: 'microi_codex_action',
579
+ title: 'Microi action fallback',
580
+ description: '工具未注入时,按连接路由到同一原始 Microi 工具。params 是 URI 编码 JSON 对象。',
581
+ mimeType: 'application/json',
582
+ }] });
583
+ return;
584
+ }
585
+ if (method === 'resources/read') {
586
+ try { sendResult(id, await readResource(String(params?.uri || ''))); }
587
+ catch (error) { sendError(id, -32002, error instanceof Error ? error.message : String(error)); }
588
+ return;
589
+ }
590
+ if (method === 'shutdown') { sendResult(id, {}); shutdown(); return; }
591
+ if (method?.startsWith('notifications/')) return;
592
+ if (id != null) sendError(id, -32601, `Method not found: ${method}`);
593
+ }
594
+
595
+ function shutdown() {
596
+ if (shuttingDown) return;
597
+ shuttingDown = true;
598
+ brokerClient?.close();
599
+ brokerClient = null;
600
+ }
601
+
602
+ process.stdin.setEncoding('utf8');
603
+ const automaticUpdateTimer = setTimeout(scheduleAutomaticUpdate, 1500);
604
+ automaticUpdateTimer.unref();
605
+ process.stdin.on('data', chunk => {
606
+ inputBuffer += chunk;
607
+ let newline;
608
+ while ((newline = inputBuffer.indexOf('\n')) >= 0) {
609
+ const line = inputBuffer.slice(0, newline).trim();
610
+ inputBuffer = inputBuffer.slice(newline + 1);
611
+ if (!line) continue;
612
+ let message;
613
+ try { message = JSON.parse(line); }
614
+ catch { continue; }
615
+ Promise.resolve(handle(message)).catch(error => {
616
+ if (message.id != null) sendError(message.id, -32603, error instanceof Error ? error.message : String(error));
617
+ });
618
+ }
619
+ });
620
+ process.stdin.on('end', () => { shutdown(); process.exit(0); });
621
+ process.stdin.on('error', () => { shutdown(); process.exit(1); });
622
+ process.stdout.on('error', error => { shutdown(); process.exit(error?.code === 'EPIPE' ? 0 : 1); });
623
+ process.on('disconnect', () => { shutdown(); process.exit(0); });
624
+ for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP']) process.on(signal, () => { shutdown(); process.exit(0); });
625
+ process.on('exit', shutdown);
626
+
627
+ const parentPid = process.ppid;
628
+ if (parentPid > 1) {
629
+ const parentWatchTimer = setInterval(() => {
630
+ try { process.kill(parentPid, 0); }
631
+ catch (error) { if (error?.code === 'ESRCH') { shutdown(); process.exit(0); } }
632
+ }, 5000);
633
+ parentWatchTimer.unref();
634
+ }