@sanqianx/project-knowledge 5.0.18 → 5.0.20-rc.1

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 (209) hide show
  1. package/.agents/plugins/marketplace.json +3 -3
  2. package/.claude-plugin/marketplace.json +1 -1
  3. package/CHANGELOG.md +14 -0
  4. package/LICENSE +201 -201
  5. package/NOTICE +5 -5
  6. package/README.md +349 -349
  7. package/_modules/knowledge-engine/README.md +4 -0
  8. package/_modules/knowledge-engine/src/analysis-contract.js +4 -3
  9. package/_modules/knowledge-engine/src/analysis-failures.js +39 -0
  10. package/_modules/knowledge-engine/src/analysis-worker.js +6 -2
  11. package/_modules/knowledge-engine/src/coordinator.js +8 -0
  12. package/_modules/knowledge-engine/src/headless-analyzer.js +2 -1
  13. package/_modules/knowledge-engine/src/memories.js +10 -5
  14. package/_modules/knowledge-engine/src/memory-eligibility.js +74 -0
  15. package/_modules/knowledge-engine/src/retrieval-ranking.js +19 -1
  16. package/_modules/knowledge-engine/src/runtime/commit-conversation-binder.js +1 -0
  17. package/_modules/knowledge-engine/src/runtime/commit-prompt.js +12 -9
  18. package/_modules/knowledge-engine/src/runtime/conversation-store.js +2 -0
  19. package/_modules/knowledge-engine/src/server.js +1 -1
  20. package/_site/_test/agent-capture-mcp-ui-test.js +67 -67
  21. package/_site/_test/ai-profile-test.js +75 -75
  22. package/_site/_test/ai-vendor-presets-test.js +91 -91
  23. package/_site/_test/automatic-context-test.js +70 -70
  24. package/_site/_test/bin-cli-test.js +230 -230
  25. package/_site/_test/bridge-adapter-test.js +95 -95
  26. package/_site/_test/bridge-consumer-service-test.js +310 -310
  27. package/_site/_test/candidate-bin-content-test.js +35 -0
  28. package/_site/_test/candidate-product-assets-test.js +13 -0
  29. package/_site/_test/candidate-release-contract-test.js +14 -14
  30. package/_site/_test/capture-hook-dedup-test.js +158 -158
  31. package/_site/_test/claude-executable-discovery-test.js +28 -28
  32. package/_site/_test/claude-session-lifecycle-sweep-test.js +252 -252
  33. package/_site/_test/cli-instance-management-test.js +62 -62
  34. package/_site/_test/client-capture-restart-e2e-test.js +189 -189
  35. package/_site/_test/codex-conversation-projection-test.js +60 -60
  36. package/_site/_test/codex-marketplace-scoped-path-test.js +186 -186
  37. package/_site/_test/codex-notify-capture-e2e-test.js +101 -101
  38. package/_site/_test/codex-notify-fanout-test.js +134 -134
  39. package/_site/_test/codex-notify-upgrade-test.js +253 -253
  40. package/_site/_test/commit-boundary-freeze-test.js +147 -147
  41. package/_site/_test/commit-conversation-binding-test.js +4 -4
  42. package/_site/_test/configuration-generation-test.js +22 -22
  43. package/_site/_test/context-pack-test.js +200 -200
  44. package/_site/_test/conversation-api-test.js +10 -10
  45. package/_site/_test/conversation-origin-test.js +15 -0
  46. package/_site/_test/cross-repo-isolation-e2e-test.js +187 -187
  47. package/_site/_test/embedding-config-test.js +32 -32
  48. package/_site/_test/fixtures/fake-browser-exit.js +10 -10
  49. package/_site/_test/fixtures/fake-browser-hang.js +10 -10
  50. package/_site/_test/fixtures/integration-busy-worker.cjs +6 -6
  51. package/_site/_test/fixtures/make-git-repos.js +92 -92
  52. package/_site/_test/fixtures/retrieval-gold-queries.json +32 -0
  53. package/_site/_test/fresh-hook-reimport-test.js +53 -53
  54. package/_site/_test/fresh-product-data-test.js +56 -56
  55. package/_site/_test/git-path-compatibility-test.js +80 -80
  56. package/_site/_test/git-validation-test.js +81 -81
  57. package/_site/_test/hook-worktree-test.js +42 -42
  58. package/_site/_test/index-builder-test.js +41 -41
  59. package/_site/_test/index-error-display-contract-test.js +10 -10
  60. package/_site/_test/integration-adapters-test.js +67 -67
  61. package/_site/_test/integration-capture-setup-test.js +118 -118
  62. package/_site/_test/integration-runtime-binding-a2-test.js +566 -566
  63. package/_site/_test/integration-runtime-binding-a3-test.js +227 -227
  64. package/_site/_test/integration-runtime-binding-test.js +473 -473
  65. package/_site/_test/integration-status-scan-test.js +29 -29
  66. package/_site/_test/kb-framework-test.js +57 -57
  67. package/_site/_test/kbpath-follow-test.js +63 -63
  68. package/_site/_test/knowledge-db-test.js +144 -144
  69. package/_site/_test/knowledge-engine-client-test.js +109 -109
  70. package/_site/_test/knowledge-maintenance-test.js +40 -40
  71. package/_site/_test/knowledge-panel-ui-contract-test.js +112 -0
  72. package/_site/_test/knowledge-scopes-test.js +37 -37
  73. package/_site/_test/knowledge-storage-location-test.js +28 -28
  74. package/_site/_test/launcher-readiness-test.js +80 -80
  75. package/_site/_test/legacy-claude-session-alias-test.js +106 -106
  76. package/_site/_test/legacy-routes-removed-test.js +47 -47
  77. package/_site/_test/legacy-workbench-exclusion-test.js +92 -92
  78. package/_site/_test/live-gateway-test.js +48 -48
  79. package/_site/_test/local-marketplace-test.js +16 -16
  80. package/_site/_test/log-redaction-test.js +58 -58
  81. package/_site/_test/markdown-maintenance-test.js +11 -11
  82. package/_site/_test/model-context-windows-test.js +89 -89
  83. package/_site/_test/module-agent-start-proxy-test.js +65 -65
  84. package/_site/_test/module-artifact-integrity-test.js +27 -27
  85. package/_site/_test/module-boundary-test.js +30 -30
  86. package/_site/_test/module-bridge-eventbridge-test.js +202 -202
  87. package/_site/_test/module-dynamic-port-test.js +118 -118
  88. package/_site/_test/module-index-proxy-test.js +33 -0
  89. package/_site/_test/module-model-configuration-test.js +44 -44
  90. package/_site/_test/module-process-lifecycle-test.js +35 -35
  91. package/_site/_test/module-stream-proxy-test.js +34 -34
  92. package/_site/_test/module-terminal-restart-test.js +63 -0
  93. package/_site/_test/module-wiring-unbind-race-test.js +59 -59
  94. package/_site/_test/native-context-integrations-test.js +86 -86
  95. package/_site/_test/orphaned-claim-recovery-test.js +80 -80
  96. package/_site/_test/package-boundary-test.js +43 -42
  97. package/_site/_test/package-startup-test.js +56 -56
  98. package/_site/_test/packaged-ui-smoke-test.js +88 -88
  99. package/_site/_test/pr-consumer-contract-test.js +55 -55
  100. package/_site/_test/product-brand-contract-test.js +25 -25
  101. package/_site/_test/product-diagnostics-ui-test.js +93 -93
  102. package/_site/_test/product-import-ui-test.js +104 -104
  103. package/_site/_test/product-progress-status-test.js +12 -1
  104. package/_site/_test/project-remove-running-guard-test.js +79 -79
  105. package/_site/_test/project-store-test.js +90 -90
  106. package/_site/_test/registry-events-test.js +38 -0
  107. package/_site/_test/release-version-sync-test.js +115 -115
  108. package/_site/_test/requirement-binding-test.js +113 -113
  109. package/_site/_test/runtime-endpoint-test.js +64 -64
  110. package/_site/_test/scanner-test.js +83 -83
  111. package/_site/_test/server-security-test.js +96 -96
  112. package/_site/_test/sessions-stream-test.js +93 -93
  113. package/_site/_test/shared-scope-root-binding-test.js +67 -67
  114. package/_site/_test/simple-import-test.js +98 -98
  115. package/_site/_test/startup-analysis-disabled-test.js +6 -6
  116. package/_site/_test/team-knowledge-store-sparse-checkout-test.js +196 -196
  117. package/_site/_test/team-knowledge-store-test.js +132 -132
  118. package/_site/_test/test-server-cleanup-test.js +35 -35
  119. package/_site/_test/test-server-module-isolation-test.js +37 -37
  120. package/_site/_test/tracking-start-test.js +35 -35
  121. package/_site/_test/workbench-context-receipts-test.js +22 -0
  122. package/_site/_test/workspace-ui-contract-test.js +31 -31
  123. package/_site/_test/zcode-capture-integration-test.js +108 -108
  124. package/_site/lib/ai-vendor-presets.js +83 -83
  125. package/_site/lib/ai-workspace.js +3 -3
  126. package/_site/lib/atomic-file.js +204 -204
  127. package/_site/lib/bridge-adapter.js +260 -260
  128. package/_site/lib/capture-hook-config.js +108 -108
  129. package/_site/lib/claude-cli-runner.js +89 -89
  130. package/_site/lib/cli-instances.js +43 -43
  131. package/_site/lib/codex-context-status.js +17 -17
  132. package/_site/lib/codex-notify-config.js +193 -193
  133. package/_site/lib/commit-conversation-binder.js +13 -12
  134. package/_site/lib/commit-reconciler.js +38 -38
  135. package/_site/lib/context-pack-builder.js +299 -299
  136. package/_site/lib/conversation-exclusions.js +204 -204
  137. package/_site/lib/conversation-query-service.js +17 -17
  138. package/_site/lib/conversation-store.js +2 -0
  139. package/_site/lib/data-dir.js +13 -13
  140. package/_site/lib/embedding-service.js +85 -85
  141. package/_site/lib/engine-connection.js +93 -90
  142. package/_site/lib/engine-knowledge-client.js +72 -72
  143. package/_site/lib/folder-picker-output.js +3 -3
  144. package/_site/lib/git-path.js +14 -14
  145. package/_site/lib/github-team-store.js +3 -3
  146. package/_site/lib/index-builder.js +157 -157
  147. package/_site/lib/integration-status-scan.js +99 -99
  148. package/_site/lib/integration-status-worker.js +14 -14
  149. package/_site/lib/kb-framework.js +15 -15
  150. package/_site/lib/knowledge-db.js +363 -363
  151. package/_site/lib/knowledge-schema.js +121 -121
  152. package/_site/lib/live-gateway.js +78 -76
  153. package/_site/lib/llm-client.js +12 -12
  154. package/_site/lib/model-context-windows.js +82 -82
  155. package/_site/lib/module-bridge.js +874 -865
  156. package/_site/lib/module-port-resolver.js +57 -57
  157. package/_site/lib/post-commit-automation.js +54 -54
  158. package/_site/lib/product-data.js +35 -35
  159. package/_site/lib/project-lifecycle-service.js +14 -14
  160. package/_site/lib/project-registry-store.js +129 -129
  161. package/_site/lib/prompt-registry.js +148 -148
  162. package/_site/lib/registry-events.js +16 -0
  163. package/_site/lib/requirement-adapters.js +10 -10
  164. package/_site/lib/requirement-binder.js +112 -112
  165. package/_site/lib/runtime-control.js +89 -89
  166. package/_site/lib/runtime-endpoint.js +5 -5
  167. package/_site/lib/server-app.js +10 -1
  168. package/_site/lib/zcode-context-integration.js +60 -60
  169. package/_site/scripts/codex-notify-fanout.cjs +95 -95
  170. package/_site/scripts/install-module-candidates.js +69 -69
  171. package/_site/scripts/pack-module-candidates.js +89 -72
  172. package/_site/scripts/sync-product-content.js +28 -28
  173. package/_site/scripts/sync-release-version.js +164 -164
  174. package/_site/scripts/vendor-modules.js +49 -49
  175. package/_site/scripts/verify-module-runtime.js +50 -50
  176. package/_site/scripts/verify-product-runtime.js +36 -36
  177. package/_site/scripts/verify-retrieval-gold.js +83 -0
  178. package/_site/server.js +15 -15
  179. package/_site/start.bat +26 -26
  180. package/_site/stop.bat +11 -11
  181. package/bin/project-knowledge-integrations.js +254 -254
  182. package/bin/project-knowledge-kb.js +67 -67
  183. package/bin/project-knowledge-mcp.js +8 -8
  184. package/bin/project-knowledge.js +170 -170
  185. package/claude-prompts.json +21 -21
  186. package/docs/README.zh-CN.md +205 -205
  187. package/docs/assets/architecture.svg +181 -181
  188. package/docs/assets/logo.svg +49 -49
  189. package/docs/fresh-v5-start.md +42 -42
  190. package/docs/leader-glm-workbench-playbook.md +122 -119
  191. package/docs/project-registry-schema.md +11 -11
  192. package/docs/release-5.0.16-acceptance.md +176 -176
  193. package/docs/release-5.0.17-acceptance.md +117 -117
  194. package/docs/release-5.0.19-acceptance.md +39 -0
  195. package/docs/team-knowledge-mode-a-plan.md +73 -73
  196. package/module-runtime-manifest.json +126 -104
  197. package/package.json +110 -108
  198. package/plugins/project-knowledge/.claude-plugin/plugin.json +1 -1
  199. package/plugins/project-knowledge/.codex-plugin/plugin.json +1 -1
  200. package/plugins/project-knowledge/.mcp.json +1 -1
  201. package/plugins/project-knowledge/opencode/project-knowledge.md +3 -3
  202. package/plugins/project-knowledge/skills/project-knowledge/SKILL.md +28 -28
  203. package/templates/change.md +30 -30
  204. package/templates/project-readme.md +18 -18
  205. package/ui/index.html +1 -1
  206. package/ui/knowledge-panel.html +118 -0
  207. package/ui/product.css +8 -0
  208. package/ui/product.js +175 -8
  209. package/vendor-manifest.json +55 -53
@@ -1,121 +1,121 @@
1
- const crypto = require('crypto');
2
- const arrow = require('apache-arrow');
3
-
4
- const KNOWLEDGE_DB_SCHEMA_VERSION = 1;
5
- const KNOWLEDGE_TABLE = 'knowledge_chunks';
6
- const EMBEDDING_DIMENSIONS = 512;
7
-
8
- function field(name, type, nullable = false) {
9
- return new arrow.Field(name, type, nullable);
10
- }
11
-
12
- function knowledgeChunkSchema(dimensions = EMBEDDING_DIMENSIONS) {
13
- return new arrow.Schema([
14
- field('schema_version', new arrow.Int32()),
15
- field('record_id', new arrow.Utf8()),
16
- field('space_id', new arrow.Utf8()),
17
- field('entry_id', new arrow.Utf8()),
18
- field('entry_type', new arrow.Utf8()),
19
- field('entry_version', new arrow.Int32()),
20
- field('chunk_id', new arrow.Utf8()),
21
- field('chunk_order', new arrow.Int32()),
22
- field('title', new arrow.Utf8()),
23
- field('heading_path', new arrow.Utf8()),
24
- field('chunk_text', new arrow.Utf8()),
25
- field('search_text', new arrow.Utf8()),
26
- field('vector', new arrow.FixedSizeList(dimensions, field('item', new arrow.Float32()))),
27
- field('tags_json', new arrow.Utf8()),
28
- field('source_paths_json', new arrow.Utf8()),
29
- field('routes_json', new arrow.Utf8()),
30
- field('symbols_json', new arrow.Utf8()),
31
- field('source_project_id', new arrow.Utf8()),
32
- field('source_commit', new arrow.Utf8()),
33
- field('document_hash', new arrow.Utf8()),
34
- field('content_hash', new arrow.Utf8()),
35
- field('created_at', new arrow.Utf8()),
36
- field('updated_at', new arrow.Utf8()),
37
- ]);
38
- }
39
-
40
- function sha256(value) {
41
- return crypto.createHash('sha256').update(String(value || ''), 'utf8').digest('hex');
42
- }
43
-
44
- function requiredText(value, name) {
45
- const normalized = String(value || '').trim();
46
- if (!normalized) throw new Error(`${name} is required`);
47
- return normalized;
48
- }
49
-
50
- function jsonArray(value) {
51
- return JSON.stringify(Array.isArray(value) ? value.map(item => String(item)) : []);
52
- }
53
-
54
- function normalizeVector(value, dimensions = EMBEDDING_DIMENSIONS) {
55
- const vector = Array.from(value || [], Number);
56
- if (vector.length !== dimensions || vector.some(item => !Number.isFinite(item))) {
57
- throw new Error(`vector must contain exactly ${dimensions} finite numbers`);
58
- }
59
- return vector;
60
- }
61
-
62
- function normalizeKnowledgeChunk(input, options = {}) {
63
- const dimensions = options.dimensions || EMBEDDING_DIMENSIONS;
64
- const now = options.now || new Date().toISOString();
65
- const spaceId = requiredText(input.spaceId || input.space_id, 'spaceId');
66
- const entryId = requiredText(input.entryId || input.entry_id, 'entryId');
67
- const chunkOrder = Number.isInteger(input.chunkOrder) ? input.chunkOrder : Number(input.chunk_order || 0);
68
- if (!Number.isInteger(chunkOrder) || chunkOrder < 0) throw new Error('chunkOrder must be a non-negative integer');
69
- const chunkText = requiredText(input.chunkText || input.chunk_text, 'chunkText');
70
- const contentHash = String(input.contentHash || input.content_hash || sha256(chunkText));
71
- const chunkId = String(input.chunkId || input.chunk_id || `${entryId}:${chunkOrder}`);
72
-
73
- return {
74
- schema_version: KNOWLEDGE_DB_SCHEMA_VERSION,
75
- record_id: `${spaceId}:${chunkId}`,
76
- space_id: spaceId,
77
- entry_id: entryId,
78
- entry_type: String(input.entryType || input.entry_type || 'document'),
79
- entry_version: Number.isInteger(input.entryVersion) ? input.entryVersion : Number(input.entry_version || 1),
80
- chunk_id: chunkId,
81
- chunk_order: chunkOrder,
82
- title: String(input.title || ''),
83
- heading_path: Array.isArray(input.headingPath) ? input.headingPath.join(' > ') : String(input.heading_path || ''),
84
- chunk_text: chunkText,
85
- search_text: String(input.searchText || input.search_text || `${input.title || ''}\n${chunkText}`).trim(),
86
- vector: normalizeVector(input.vector, dimensions),
87
- tags_json: jsonArray(input.tags),
88
- source_paths_json: jsonArray(input.sourcePaths || input.source_paths),
89
- routes_json: jsonArray(input.routes),
90
- symbols_json: jsonArray(input.symbols),
91
- source_project_id: String(input.sourceProjectId || input.source_project_id || ''),
92
- source_commit: String(input.sourceCommit || input.source_commit || ''),
93
- document_hash: String(input.documentHash || input.document_hash || contentHash),
94
- content_hash: contentHash,
95
- created_at: String(input.createdAt || input.created_at || now),
96
- updated_at: String(input.updatedAt || input.updated_at || now),
97
- };
98
- }
99
-
100
- function decodeKnowledgeChunk(row) {
101
- const parse = (value) => {
102
- try { return JSON.parse(value || '[]'); } catch { return []; }
103
- };
104
- return {
105
- ...row,
106
- tags: parse(row.tags_json),
107
- sourcePaths: parse(row.source_paths_json),
108
- routes: parse(row.routes_json),
109
- symbols: parse(row.symbols_json),
110
- };
111
- }
112
-
113
- module.exports = {
114
- KNOWLEDGE_DB_SCHEMA_VERSION,
115
- KNOWLEDGE_TABLE,
116
- EMBEDDING_DIMENSIONS,
117
- knowledgeChunkSchema,
118
- normalizeKnowledgeChunk,
119
- decodeKnowledgeChunk,
120
- sha256,
121
- };
1
+ const crypto = require('crypto');
2
+ const arrow = require('apache-arrow');
3
+
4
+ const KNOWLEDGE_DB_SCHEMA_VERSION = 1;
5
+ const KNOWLEDGE_TABLE = 'knowledge_chunks';
6
+ const EMBEDDING_DIMENSIONS = 512;
7
+
8
+ function field(name, type, nullable = false) {
9
+ return new arrow.Field(name, type, nullable);
10
+ }
11
+
12
+ function knowledgeChunkSchema(dimensions = EMBEDDING_DIMENSIONS) {
13
+ return new arrow.Schema([
14
+ field('schema_version', new arrow.Int32()),
15
+ field('record_id', new arrow.Utf8()),
16
+ field('space_id', new arrow.Utf8()),
17
+ field('entry_id', new arrow.Utf8()),
18
+ field('entry_type', new arrow.Utf8()),
19
+ field('entry_version', new arrow.Int32()),
20
+ field('chunk_id', new arrow.Utf8()),
21
+ field('chunk_order', new arrow.Int32()),
22
+ field('title', new arrow.Utf8()),
23
+ field('heading_path', new arrow.Utf8()),
24
+ field('chunk_text', new arrow.Utf8()),
25
+ field('search_text', new arrow.Utf8()),
26
+ field('vector', new arrow.FixedSizeList(dimensions, field('item', new arrow.Float32()))),
27
+ field('tags_json', new arrow.Utf8()),
28
+ field('source_paths_json', new arrow.Utf8()),
29
+ field('routes_json', new arrow.Utf8()),
30
+ field('symbols_json', new arrow.Utf8()),
31
+ field('source_project_id', new arrow.Utf8()),
32
+ field('source_commit', new arrow.Utf8()),
33
+ field('document_hash', new arrow.Utf8()),
34
+ field('content_hash', new arrow.Utf8()),
35
+ field('created_at', new arrow.Utf8()),
36
+ field('updated_at', new arrow.Utf8()),
37
+ ]);
38
+ }
39
+
40
+ function sha256(value) {
41
+ return crypto.createHash('sha256').update(String(value || ''), 'utf8').digest('hex');
42
+ }
43
+
44
+ function requiredText(value, name) {
45
+ const normalized = String(value || '').trim();
46
+ if (!normalized) throw new Error(`${name} is required`);
47
+ return normalized;
48
+ }
49
+
50
+ function jsonArray(value) {
51
+ return JSON.stringify(Array.isArray(value) ? value.map(item => String(item)) : []);
52
+ }
53
+
54
+ function normalizeVector(value, dimensions = EMBEDDING_DIMENSIONS) {
55
+ const vector = Array.from(value || [], Number);
56
+ if (vector.length !== dimensions || vector.some(item => !Number.isFinite(item))) {
57
+ throw new Error(`vector must contain exactly ${dimensions} finite numbers`);
58
+ }
59
+ return vector;
60
+ }
61
+
62
+ function normalizeKnowledgeChunk(input, options = {}) {
63
+ const dimensions = options.dimensions || EMBEDDING_DIMENSIONS;
64
+ const now = options.now || new Date().toISOString();
65
+ const spaceId = requiredText(input.spaceId || input.space_id, 'spaceId');
66
+ const entryId = requiredText(input.entryId || input.entry_id, 'entryId');
67
+ const chunkOrder = Number.isInteger(input.chunkOrder) ? input.chunkOrder : Number(input.chunk_order || 0);
68
+ if (!Number.isInteger(chunkOrder) || chunkOrder < 0) throw new Error('chunkOrder must be a non-negative integer');
69
+ const chunkText = requiredText(input.chunkText || input.chunk_text, 'chunkText');
70
+ const contentHash = String(input.contentHash || input.content_hash || sha256(chunkText));
71
+ const chunkId = String(input.chunkId || input.chunk_id || `${entryId}:${chunkOrder}`);
72
+
73
+ return {
74
+ schema_version: KNOWLEDGE_DB_SCHEMA_VERSION,
75
+ record_id: `${spaceId}:${chunkId}`,
76
+ space_id: spaceId,
77
+ entry_id: entryId,
78
+ entry_type: String(input.entryType || input.entry_type || 'document'),
79
+ entry_version: Number.isInteger(input.entryVersion) ? input.entryVersion : Number(input.entry_version || 1),
80
+ chunk_id: chunkId,
81
+ chunk_order: chunkOrder,
82
+ title: String(input.title || ''),
83
+ heading_path: Array.isArray(input.headingPath) ? input.headingPath.join(' > ') : String(input.heading_path || ''),
84
+ chunk_text: chunkText,
85
+ search_text: String(input.searchText || input.search_text || `${input.title || ''}\n${chunkText}`).trim(),
86
+ vector: normalizeVector(input.vector, dimensions),
87
+ tags_json: jsonArray(input.tags),
88
+ source_paths_json: jsonArray(input.sourcePaths || input.source_paths),
89
+ routes_json: jsonArray(input.routes),
90
+ symbols_json: jsonArray(input.symbols),
91
+ source_project_id: String(input.sourceProjectId || input.source_project_id || ''),
92
+ source_commit: String(input.sourceCommit || input.source_commit || ''),
93
+ document_hash: String(input.documentHash || input.document_hash || contentHash),
94
+ content_hash: contentHash,
95
+ created_at: String(input.createdAt || input.created_at || now),
96
+ updated_at: String(input.updatedAt || input.updated_at || now),
97
+ };
98
+ }
99
+
100
+ function decodeKnowledgeChunk(row) {
101
+ const parse = (value) => {
102
+ try { return JSON.parse(value || '[]'); } catch { return []; }
103
+ };
104
+ return {
105
+ ...row,
106
+ tags: parse(row.tags_json),
107
+ sourcePaths: parse(row.source_paths_json),
108
+ routes: parse(row.routes_json),
109
+ symbols: parse(row.symbols_json),
110
+ };
111
+ }
112
+
113
+ module.exports = {
114
+ KNOWLEDGE_DB_SCHEMA_VERSION,
115
+ KNOWLEDGE_TABLE,
116
+ EMBEDDING_DIMENSIONS,
117
+ knowledgeChunkSchema,
118
+ normalizeKnowledgeChunk,
119
+ decodeKnowledgeChunk,
120
+ sha256,
121
+ };
@@ -1,76 +1,78 @@
1
- 'use strict';
2
- const { WebSocketServer } = require('ws');
3
- const { TextDecoder } = require('node:util');
4
-
5
- function attachLiveGateway(server, runtime) {
6
- const wss = new WebSocketServer({ noServer: true, maxPayload: 8192 });
7
- function target(route) {
8
- const url = new URL(route, 'http://local');
9
- if (url.origin !== 'http://local' || !route.startsWith('/') || route.length > 2048) throw new Error('INVALID_ROUTE');
10
- if ([...url.searchParams.keys()].some(k => !['after', 'projectId', 'level', 'component', 'event', 'q', 'operationId', 'runId', 'commitSha', 'limit', 'summary', 'jobId', 'since', 'streamCursor'].includes(k))) throw new Error('INVALID_QUERY');
11
- const bridge = runtime.moduleBridge;
12
- if (url.pathname === '/api/engine/progress') {
13
- if (bridge.engineCompatible === false) throw new Error('ENGINE_CONNECTION_PENDING');
14
- return `${bridge.engineUrl}/api/progress${url.search}`;
15
- }
16
- if (/^\/api\/terminal\/sessions\/[a-zA-Z0-9_-]+\/events$/.test(url.pathname)) return bridge._terminalApi(url.pathname) + url.search;
17
- if (url.pathname === '/api/logs/stream') return `http://${runtime.liveAddress}${route}`;
18
- throw new Error('INVALID_ROUTE');
19
- }
20
- server.on('upgrade', (req, socket, head) => {
21
- const local = ['127.0.0.1', '::1', '::ffff:127.0.0.1'].includes(req.socket.remoteAddress);
22
- let hostname; try { hostname = new URL(`http://${req.headers.host}`).hostname; } catch { socket.destroy(); return; }
23
- if (req.url !== '/api/live' || !local || !['127.0.0.1', 'localhost', '[::1]'].includes(hostname) || req.headers.origin !== `http://${req.headers.host}`) { socket.destroy(); return; }
24
- wss.handleUpgrade(req, socket, head, ws => wss.emit('connection', ws));
25
- });
26
- wss.on('connection', ws => {
27
- const subscriptions = new Map();
28
- const send = payload => {
29
- if (ws.readyState !== 1) return;
30
- if (ws.bufferedAmount > 1024 * 1024) { ws.close(1013, 'slow consumer'); return; }
31
- ws.send(JSON.stringify(payload));
32
- };
33
- const stop = id => { subscriptions.get(id)?.abort(); subscriptions.delete(id); };
34
- ws.on('message', async bytes => {
35
- let message, controller;
36
- try {
37
- message = JSON.parse(String(bytes));
38
- if (typeof message?.id !== 'string' || !/^[a-zA-Z0-9_-]{1,80}$/.test(message.id)) throw new Error('INVALID_SUBSCRIPTION');
39
- if (message.type === 'unsubscribe') { stop(message.id); return; }
40
- if (message.type !== 'subscribe' || subscriptions.size >= 8) throw new Error('INVALID_SUBSCRIPTION');
41
- const url = target(message.route); stop(message.id);
42
- controller = new AbortController(); subscriptions.set(message.id, controller);
43
- const deadline = setTimeout(() => controller.abort(), 3000);
44
- let upstream;
45
- try { upstream = await fetch(url, { signal: controller.signal, headers: message.after ? { 'Last-Event-ID': String(message.after).slice(0, 128) } : {} }); }
46
- finally { clearTimeout(deadline); }
47
- if (!upstream.ok || !upstream.headers.get('content-type')?.includes('text/event-stream')) throw new Error('UPSTREAM_UNAVAILABLE');
48
- send({ id: message.id, type: 'open' });
49
- const decoder = new TextDecoder(); let buffer = '';
50
- for await (const chunk of upstream.body) {
51
- buffer += decoder.decode(chunk, { stream: true });
52
- if (buffer.length > 2 * 1024 * 1024) throw new Error('EVENT_TOO_LARGE');
53
- let boundary;
54
- while ((boundary = /\r?\n\r?\n/.exec(buffer))) {
55
- const block = buffer.slice(0, boundary.index); buffer = buffer.slice(boundary.index + boundary[0].length);
56
- let event = 'message', data = [], cursor;
57
- for (const line of block.split(/\r?\n/)) {
58
- const colon = line.indexOf(':'), key = colon < 0 ? line : line.slice(0, colon), value = colon < 0 ? '' : line.slice(colon + 1).replace(/^ /, '');
59
- if (key === 'event') event = value; else if (key === 'data') data.push(value); else if (key === 'id') cursor = value;
60
- }
61
- if (data.length && subscriptions.get(message.id) === controller) send({ type: 'event', id: message.id, event, data: data.join('\n'), cursor });
62
- }
63
- }
64
- if (!controller.signal.aborted) throw new Error('UPSTREAM_CLOSED');
65
- } catch {
66
- if (controller) { if (subscriptions.get(message.id) === controller) { stop(message.id); send({ type: 'error', id: message.id, code: 'STREAM_UNAVAILABLE' }); } }
67
- else send({ type: 'error', id: message?.id, code: 'INVALID_SUBSCRIPTION' });
68
- }
69
- });
70
- const heartbeat = setInterval(() => { if (ws.isAlive === false) return ws.terminate(); ws.isAlive = false; ws.ping(); }, 30000);
71
- ws.on('pong', () => { ws.isAlive = true; });
72
- ws.on('close', () => { clearInterval(heartbeat); for (const id of subscriptions.keys()) stop(id); });
73
- });
74
- return { close() { for (const ws of wss.clients) ws.terminate(); wss.close(); } };
75
- }
76
- module.exports = { attachLiveGateway };
1
+ 'use strict';
2
+ const { WebSocketServer } = require('ws');
3
+ const { TextDecoder } = require('node:util');
4
+
5
+ function attachLiveGateway(server, runtime) {
6
+ const wss = new WebSocketServer({ noServer: true, maxPayload: 8192 });
7
+ function target(route) {
8
+ const url = new URL(route, 'http://local');
9
+ if (url.origin !== 'http://local' || !route.startsWith('/') || route.length > 2048) throw new Error('INVALID_ROUTE');
10
+ if ([...url.searchParams.keys()].some(k => !['after', 'projectId', 'contextId', 'level', 'component', 'event', 'q', 'operationId', 'runId', 'commitSha', 'limit', 'summary', 'jobId', 'since', 'streamCursor'].includes(k))) throw new Error('INVALID_QUERY');
11
+ const bridge = runtime.moduleBridge;
12
+ if (url.pathname === '/api/engine/progress') {
13
+ if (bridge.engineCompatible === false) throw new Error('ENGINE_CONNECTION_PENDING');
14
+ return `${bridge.engineUrl}/api/progress${url.search}`;
15
+ }
16
+ if (/^\/api\/terminal\/sessions\/[a-zA-Z0-9_-]+\/events$/.test(url.pathname)) return bridge._terminalApi(url.pathname) + url.search;
17
+ if (url.pathname === '/api/terminal/sessions/changes') return bridge._terminalApi(url.pathname) + url.search;
18
+ if (url.pathname === '/api/registry/events') return `http://${runtime.liveAddress}${route}`;
19
+ if (url.pathname === '/api/logs/stream') return `http://${runtime.liveAddress}${route}`;
20
+ throw new Error('INVALID_ROUTE');
21
+ }
22
+ server.on('upgrade', (req, socket, head) => {
23
+ const local = ['127.0.0.1', '::1', '::ffff:127.0.0.1'].includes(req.socket.remoteAddress);
24
+ let hostname; try { hostname = new URL(`http://${req.headers.host}`).hostname; } catch { socket.destroy(); return; }
25
+ if (req.url !== '/api/live' || !local || !['127.0.0.1', 'localhost', '[::1]'].includes(hostname) || req.headers.origin !== `http://${req.headers.host}`) { socket.destroy(); return; }
26
+ wss.handleUpgrade(req, socket, head, ws => wss.emit('connection', ws));
27
+ });
28
+ wss.on('connection', ws => {
29
+ const subscriptions = new Map();
30
+ const send = payload => {
31
+ if (ws.readyState !== 1) return;
32
+ if (ws.bufferedAmount > 1024 * 1024) { ws.close(1013, 'slow consumer'); return; }
33
+ ws.send(JSON.stringify(payload));
34
+ };
35
+ const stop = id => { subscriptions.get(id)?.abort(); subscriptions.delete(id); };
36
+ ws.on('message', async bytes => {
37
+ let message, controller;
38
+ try {
39
+ message = JSON.parse(String(bytes));
40
+ if (typeof message?.id !== 'string' || !/^[a-zA-Z0-9_-]{1,80}$/.test(message.id)) throw new Error('INVALID_SUBSCRIPTION');
41
+ if (message.type === 'unsubscribe') { stop(message.id); return; }
42
+ if (message.type !== 'subscribe' || subscriptions.size >= 8) throw new Error('INVALID_SUBSCRIPTION');
43
+ const url = target(message.route); stop(message.id);
44
+ controller = new AbortController(); subscriptions.set(message.id, controller);
45
+ const deadline = setTimeout(() => controller.abort(), 3000);
46
+ let upstream;
47
+ try { upstream = await fetch(url, { signal: controller.signal, headers: message.after ? { 'Last-Event-ID': String(message.after).slice(0, 128) } : {} }); }
48
+ finally { clearTimeout(deadline); }
49
+ if (!upstream.ok || !upstream.headers.get('content-type')?.includes('text/event-stream')) throw new Error('UPSTREAM_UNAVAILABLE');
50
+ send({ id: message.id, type: 'open' });
51
+ const decoder = new TextDecoder(); let buffer = '';
52
+ for await (const chunk of upstream.body) {
53
+ buffer += decoder.decode(chunk, { stream: true });
54
+ if (buffer.length > 2 * 1024 * 1024) throw new Error('EVENT_TOO_LARGE');
55
+ let boundary;
56
+ while ((boundary = /\r?\n\r?\n/.exec(buffer))) {
57
+ const block = buffer.slice(0, boundary.index); buffer = buffer.slice(boundary.index + boundary[0].length);
58
+ let event = 'message', data = [], cursor;
59
+ for (const line of block.split(/\r?\n/)) {
60
+ const colon = line.indexOf(':'), key = colon < 0 ? line : line.slice(0, colon), value = colon < 0 ? '' : line.slice(colon + 1).replace(/^ /, '');
61
+ if (key === 'event') event = value; else if (key === 'data') data.push(value); else if (key === 'id') cursor = value;
62
+ }
63
+ if (data.length && subscriptions.get(message.id) === controller) send({ type: 'event', id: message.id, event, data: data.join('\n'), cursor });
64
+ }
65
+ }
66
+ if (!controller.signal.aborted) throw new Error('UPSTREAM_CLOSED');
67
+ } catch {
68
+ if (controller) { if (subscriptions.get(message.id) === controller) { stop(message.id); send({ type: 'error', id: message.id, code: 'STREAM_UNAVAILABLE' }); } }
69
+ else send({ type: 'error', id: message?.id, code: 'INVALID_SUBSCRIPTION' });
70
+ }
71
+ });
72
+ const heartbeat = setInterval(() => { if (ws.isAlive === false) return ws.terminate(); ws.isAlive = false; ws.ping(); }, 30000);
73
+ ws.on('pong', () => { ws.isAlive = true; });
74
+ ws.on('close', () => { clearInterval(heartbeat); for (const id of subscriptions.keys()) stop(id); });
75
+ });
76
+ return { close() { for (const ws of wss.clients) ws.terminate(); wss.close(); } };
77
+ }
78
+ module.exports = { attachLiveGateway };
@@ -39,12 +39,12 @@ function findProfile(profileId) {
39
39
  return readProfilesFile().find(profile => profile && profile.id === profileId) || null;
40
40
  }
41
41
 
42
- function readConfig(options = {}) {
43
- const profileId = typeof options === 'string' ? options : options.profileId;
44
- const inlineProfile = options && typeof options === 'object' && options.profile && typeof options.profile === 'object'
45
- ? options.profile
46
- : null;
47
- const profile = inlineProfile || findProfile(profileId) || {};
42
+ function readConfig(options = {}) {
43
+ const profileId = typeof options === 'string' ? options : options.profileId;
44
+ const inlineProfile = options && typeof options === 'object' && options.profile && typeof options.profile === 'object'
45
+ ? options.profile
46
+ : null;
47
+ const profile = inlineProfile || findProfile(profileId) || {};
48
48
  const timeoutMs = Number(profile.timeoutMs || process.env.API_TIMEOUT_MS || 60_000);
49
49
  return {
50
50
  profileId: profile.id || profileId || null,
@@ -102,8 +102,8 @@ function postJson({ baseUrl, path: p, body, apiKey, version, timeoutMs = 60_000
102
102
  });
103
103
  }
104
104
 
105
- async function completeText({ system, user, maxTokens = 512, model, profileId, profile }) {
106
- const cfg = readConfig({ profileId, profile });
105
+ async function completeText({ system, user, maxTokens = 512, model, profileId, profile }) {
106
+ const cfg = readConfig({ profileId, profile });
107
107
  if (!cfg.apiKey) {
108
108
  const id = profileId ? ` for profile ${profileId}` : '';
109
109
  throw new Error(`API key not set${id}`);
@@ -139,10 +139,10 @@ async function completeText({ system, user, maxTokens = 512, model, profileId, p
139
139
  };
140
140
  }
141
141
 
142
- async function completeJson({ system, user, schema, maxTokens = 2048, model, profileId, profile }) {
143
- const schemaHint = schema ? `\n\nThe output MUST be a JSON object matching this schema:\n${schema}\n` : '';
144
- const finalUser = `${user}${schemaHint}\n\nRespond with ONLY the JSON object, no prose, no markdown fences.`;
145
- const result = await completeText({ system, user: finalUser, maxTokens, model, profileId, profile });
142
+ async function completeJson({ system, user, schema, maxTokens = 2048, model, profileId, profile }) {
143
+ const schemaHint = schema ? `\n\nThe output MUST be a JSON object matching this schema:\n${schema}\n` : '';
144
+ const finalUser = `${user}${schemaHint}\n\nRespond with ONLY the JSON object, no prose, no markdown fences.`;
145
+ const result = await completeText({ system, user: finalUser, maxTokens, model, profileId, profile });
146
146
  const text = result.text;
147
147
 
148
148
  let parsed = null;
@@ -1,82 +1,82 @@
1
- // Per-family context window sizes (in tokens) for common models.
2
- // Match strategy: lowercase the model name, then prefix-match — first hit wins.
3
- // Entry order matters: more specific prefixes (e.g. 'claude-opus-4') precede
4
- // broader ones (e.g. 'claude'). Keep specific entries above generic ones.
5
- const MODEL_CONTEXT_WINDOWS = [
6
- // Anthropic Claude — full family 200K
7
- ['claude-opus-4', 200000],
8
- ['claude-sonnet-4', 200000],
9
- ['claude-haiku-4', 200000],
10
- ['claude-3-7', 200000],
11
- ['claude-3-5', 200000],
12
- ['claude-3', 200000],
13
- ['claude', 200000],
14
-
15
- // MiniMax
16
- ['minimax-m3', 1048576],
17
- ['minimax-m2', 1048576],
18
- ['minimax', 1048576],
19
-
20
- // Zhipu GLM
21
- ['glm-5', 1048576],
22
- ['glm-4.6', 1048576],
23
- ['glm-4.5', 128000],
24
- ['glm-4-plus', 128000],
25
- ['glm-4', 128000],
26
- ['glm', 128000],
27
-
28
- // DeepSeek
29
- ['deepseek-v3', 128000],
30
- ['deepseek-r1', 128000],
31
- ['deepseek', 64000],
32
-
33
- // OpenAI
34
- ['gpt-5', 200000],
35
- ['gpt-4.1', 1047576],
36
- ['gpt-4o', 128000],
37
- ['gpt-4-turbo', 128000],
38
- ['gpt-4', 8192],
39
- ['o3', 200000],
40
- ['o4-mini', 200000],
41
- ['o1', 200000],
42
-
43
- // Qwen
44
- ['qwen3', 131072],
45
- ['qwen-max', 32768],
46
- ['qwen', 32768],
47
-
48
- // Google Gemini
49
- ['gemini-2.5', 1048576],
50
- ['gemini-1.5', 1048576],
51
- ['gemini', 32768],
52
- ];
53
-
54
- function normalizeModelName(model) {
55
- return String(model || '').trim().toLowerCase();
56
- }
57
-
58
- function lookupContextWindow(modelName) {
59
- const name = normalizeModelName(modelName);
60
- if (!name) return null;
61
- for (const [prefix, size] of MODEL_CONTEXT_WINDOWS) {
62
- if (name.startsWith(prefix)) return size;
63
- }
64
- return null;
65
- }
66
-
67
- // Resolution order: explicit profile override → model table → env var → fallback.
68
- function resolveContextWindow({ profileContextWindow, model, env = process.env, fallback = 200000 } = {}) {
69
- const explicit = Number(profileContextWindow);
70
- if (Number.isFinite(explicit) && explicit > 0) return Math.floor(explicit);
71
- const tableHit = lookupContextWindow(model);
72
- if (tableHit) return tableHit;
73
- const envValue = Number(env && env.CLAUDE_CONTEXT_WINDOW);
74
- if (Number.isFinite(envValue) && envValue > 0) return Math.floor(envValue);
75
- return fallback;
76
- }
77
-
78
- module.exports = {
79
- MODEL_CONTEXT_WINDOWS,
80
- lookupContextWindow,
81
- resolveContextWindow,
82
- };
1
+ // Per-family context window sizes (in tokens) for common models.
2
+ // Match strategy: lowercase the model name, then prefix-match — first hit wins.
3
+ // Entry order matters: more specific prefixes (e.g. 'claude-opus-4') precede
4
+ // broader ones (e.g. 'claude'). Keep specific entries above generic ones.
5
+ const MODEL_CONTEXT_WINDOWS = [
6
+ // Anthropic Claude — full family 200K
7
+ ['claude-opus-4', 200000],
8
+ ['claude-sonnet-4', 200000],
9
+ ['claude-haiku-4', 200000],
10
+ ['claude-3-7', 200000],
11
+ ['claude-3-5', 200000],
12
+ ['claude-3', 200000],
13
+ ['claude', 200000],
14
+
15
+ // MiniMax
16
+ ['minimax-m3', 1048576],
17
+ ['minimax-m2', 1048576],
18
+ ['minimax', 1048576],
19
+
20
+ // Zhipu GLM
21
+ ['glm-5', 1048576],
22
+ ['glm-4.6', 1048576],
23
+ ['glm-4.5', 128000],
24
+ ['glm-4-plus', 128000],
25
+ ['glm-4', 128000],
26
+ ['glm', 128000],
27
+
28
+ // DeepSeek
29
+ ['deepseek-v3', 128000],
30
+ ['deepseek-r1', 128000],
31
+ ['deepseek', 64000],
32
+
33
+ // OpenAI
34
+ ['gpt-5', 200000],
35
+ ['gpt-4.1', 1047576],
36
+ ['gpt-4o', 128000],
37
+ ['gpt-4-turbo', 128000],
38
+ ['gpt-4', 8192],
39
+ ['o3', 200000],
40
+ ['o4-mini', 200000],
41
+ ['o1', 200000],
42
+
43
+ // Qwen
44
+ ['qwen3', 131072],
45
+ ['qwen-max', 32768],
46
+ ['qwen', 32768],
47
+
48
+ // Google Gemini
49
+ ['gemini-2.5', 1048576],
50
+ ['gemini-1.5', 1048576],
51
+ ['gemini', 32768],
52
+ ];
53
+
54
+ function normalizeModelName(model) {
55
+ return String(model || '').trim().toLowerCase();
56
+ }
57
+
58
+ function lookupContextWindow(modelName) {
59
+ const name = normalizeModelName(modelName);
60
+ if (!name) return null;
61
+ for (const [prefix, size] of MODEL_CONTEXT_WINDOWS) {
62
+ if (name.startsWith(prefix)) return size;
63
+ }
64
+ return null;
65
+ }
66
+
67
+ // Resolution order: explicit profile override → model table → env var → fallback.
68
+ function resolveContextWindow({ profileContextWindow, model, env = process.env, fallback = 200000 } = {}) {
69
+ const explicit = Number(profileContextWindow);
70
+ if (Number.isFinite(explicit) && explicit > 0) return Math.floor(explicit);
71
+ const tableHit = lookupContextWindow(model);
72
+ if (tableHit) return tableHit;
73
+ const envValue = Number(env && env.CLAUDE_CONTEXT_WINDOW);
74
+ if (Number.isFinite(envValue) && envValue > 0) return Math.floor(envValue);
75
+ return fallback;
76
+ }
77
+
78
+ module.exports = {
79
+ MODEL_CONTEXT_WINDOWS,
80
+ lookupContextWindow,
81
+ resolveContextWindow,
82
+ };