@walkhi/code-relax 0.1.0-beta.30 → 0.1.0-beta.32

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 (187) hide show
  1. package/README.md +95 -89
  2. package/dist/bin/cleanup-accounts.mjs +63 -63
  3. package/dist/bin/codex-remote.mjs +374 -367
  4. package/dist/bin/manage-relay-admin.mjs +20 -20
  5. package/dist/bin/migrate-relay-db.mjs +109 -109
  6. package/dist/bin/publish-android-release.mjs +63 -58
  7. package/dist/bin/publish-harmony-release.mjs +46 -0
  8. package/dist/bin/release-upload-server.mjs +174 -0
  9. package/dist/bin/self-relay-admin.mjs +8 -8
  10. package/dist/bin/self-relay-demo.mjs +33 -33
  11. package/dist/bin/self-relay-server.mjs +66 -66
  12. package/dist/build-platform.json +1 -1
  13. package/dist/get-lan-networks.ps1 +40 -40
  14. package/dist/get-shared-service-url.ps1 +6 -6
  15. package/dist/shared/app-server-events.cjs +422 -412
  16. package/dist/shared/code-browser.cjs +159 -159
  17. package/dist/shared/file-changes.cjs +31 -31
  18. package/dist/shared/file-citations.cjs +117 -117
  19. package/dist/shared/file-mentions.cjs +92 -92
  20. package/dist/shared/message-delivery.cjs +54 -54
  21. package/dist/shared/p2p-data-channel.cjs +72 -72
  22. package/dist/shared/p2p-diagnostics.cjs +28 -28
  23. package/dist/shared/p2p-file-channel.cjs +78 -74
  24. package/dist/shared/plan-pricing.cjs +32 -32
  25. package/dist/shared/question-attention.cjs +69 -0
  26. package/dist/shared/server-requests.cjs +219 -219
  27. package/dist/shared/timeline-sync.cjs +173 -173
  28. package/dist/skills/code-relax/SKILL.md +22 -22
  29. package/dist/src/archived-threads.mjs +22 -0
  30. package/dist/src/background-process.mjs +18 -18
  31. package/dist/src/bounded-process-log.mjs +63 -63
  32. package/dist/src/celsius-client.mjs +107 -107
  33. package/dist/src/code-browser.mjs +100 -100
  34. package/dist/src/desktop-launcher.mjs +46 -46
  35. package/dist/src/dots-approval-timeline.mjs +99 -99
  36. package/dist/src/dots-avatar.mjs +42 -42
  37. package/dist/src/dots-chat-projection.mjs +50 -50
  38. package/dist/src/dots-chat-server.mjs +461 -461
  39. package/dist/src/dots-message-images.mjs +319 -319
  40. package/dist/src/dots-profile.mjs +77 -77
  41. package/dist/src/dots-room-activity.mjs +94 -94
  42. package/dist/src/dots-room-client.mjs +135 -135
  43. package/dist/src/dots-room-projection.mjs +94 -94
  44. package/dist/src/dots-schedules.mjs +66 -66
  45. package/dist/src/dots-summary-tasks.mjs +32 -32
  46. package/dist/src/dots-task-catalog.mjs +430 -430
  47. package/dist/src/dots-task-files.mjs +52 -52
  48. package/dist/src/execution-health.mjs +26 -26
  49. package/dist/src/file-transfer-range.mjs +22 -22
  50. package/dist/src/file-upload-session.mjs +130 -130
  51. package/dist/src/hosted-history-app-server.mjs +103 -103
  52. package/dist/src/hosted-history-cache.mjs +113 -113
  53. package/dist/src/lan-socket-server.mjs +265 -263
  54. package/dist/src/managed-app-server.mjs +244 -244
  55. package/dist/src/message-images.mjs +9 -9
  56. package/dist/src/message-upload-files.mjs +74 -74
  57. package/dist/src/onboarding.mjs +12 -12
  58. package/dist/src/plan-pricing.mjs +47 -47
  59. package/dist/src/platform/README.md +12 -12
  60. package/dist/src/platform/codex-protocol.mjs +46 -46
  61. package/dist/src/platform/codex-runtime.mjs +3 -3
  62. package/dist/src/platform/credential-store.mjs +3 -3
  63. package/dist/src/platform/desktop-host.mjs +3 -3
  64. package/dist/src/platform/desktop-project-registration.mjs +3 -3
  65. package/dist/src/platform/desktop-project-store.mjs +208 -149
  66. package/dist/src/platform/file-opener.mjs +9 -0
  67. package/dist/src/platform/linux/README.md +3 -3
  68. package/dist/src/platform/linux/codex-runtime.mjs +56 -0
  69. package/dist/src/platform/linux/credential-store.mjs +28 -0
  70. package/dist/src/platform/linux/desktop-host.mjs +83 -0
  71. package/dist/src/platform/linux/desktop-installation.mjs +36 -0
  72. package/dist/src/platform/linux/desktop-project-registration.mjs +11 -0
  73. package/dist/src/platform/linux/network-info.mjs +11 -0
  74. package/dist/src/platform/linux/process.mjs +42 -0
  75. package/dist/src/platform/linux/project-locations.mjs +17 -0
  76. package/dist/src/platform/linux/resident-startup.mjs +52 -0
  77. package/dist/src/platform/linux/retained-runtime.mjs +76 -0
  78. package/dist/src/platform/linux/service-host.mjs +18 -0
  79. package/dist/src/platform/linux/system.mjs +49 -0
  80. package/dist/src/platform/linux/user-environment.mjs +7 -0
  81. package/dist/src/platform/macos/README.md +3 -3
  82. package/dist/src/platform/macos/codex-runtime.mjs +125 -125
  83. package/dist/src/platform/macos/credential-store.mjs +10 -10
  84. package/dist/src/platform/macos/desktop-host.mjs +82 -82
  85. package/dist/src/platform/macos/desktop-project-registration.mjs +11 -10
  86. package/dist/src/platform/macos/network-info.mjs +8 -28
  87. package/dist/src/platform/macos/project-locations.mjs +8 -8
  88. package/dist/src/platform/macos/resident-startup.mjs +84 -84
  89. package/dist/src/platform/macos/retained-runtime.mjs +127 -127
  90. package/dist/src/platform/macos/service-host.mjs +18 -18
  91. package/dist/src/platform/macos/system.mjs +29 -29
  92. package/dist/src/platform/macos/user-environment.mjs +7 -7
  93. package/dist/src/platform/native-package.mjs +58 -46
  94. package/dist/src/platform/network-info.mjs +3 -3
  95. package/dist/src/platform/path-identity.mjs +5 -5
  96. package/dist/src/platform/posix/network-candidates.mjs +23 -0
  97. package/dist/src/platform/posix/runtime-paths.mjs +25 -25
  98. package/dist/src/platform/project-locations.mjs +3 -3
  99. package/dist/src/platform/runtime-paths.mjs +8 -8
  100. package/dist/src/platform/select.mjs +5 -5
  101. package/dist/src/platform/service-host.mjs +3 -3
  102. package/dist/src/platform/user-environment.mjs +3 -3
  103. package/dist/src/platform/windows/IsolatedProcess.cs +79 -79
  104. package/dist/src/platform/windows/activate-desktop.ps1 +48 -48
  105. package/dist/src/platform/windows/background-process.mjs +30 -30
  106. package/dist/src/platform/windows/codex-runtime.mjs +215 -215
  107. package/dist/src/platform/windows/credential-store.mjs +27 -27
  108. package/dist/src/platform/windows/credential-store.ps1 +49 -49
  109. package/dist/src/platform/windows/desktop-helper.mjs +179 -179
  110. package/dist/src/platform/windows/desktop-host.mjs +58 -58
  111. package/dist/src/platform/windows/desktop-info.ps1 +14 -14
  112. package/dist/src/platform/windows/desktop-project-registration.mjs +4 -4
  113. package/dist/src/platform/windows/desktop-runtime.ps1 +10 -10
  114. package/dist/src/platform/windows/desktop-shortcuts.ps1 +21 -21
  115. package/dist/src/platform/windows/dots-profile.mjs +49 -49
  116. package/dist/src/platform/windows/launch-worker.mjs +41 -41
  117. package/dist/src/platform/windows/network-info.mjs +34 -34
  118. package/dist/src/platform/windows/project-locations.mjs +16 -16
  119. package/dist/src/platform/windows/resident-start.js +20 -20
  120. package/dist/src/platform/windows/resident-start.ps1 +7 -7
  121. package/dist/src/platform/windows/resident-startup.ps1 +83 -83
  122. package/dist/src/platform/windows/resolve-state-root.ps1 +14 -14
  123. package/dist/src/platform/windows/runtime-paths.mjs +20 -20
  124. package/dist/src/platform/windows/rustdesk-video-probe.mjs +167 -167
  125. package/dist/src/platform/windows/service-host.mjs +31 -31
  126. package/dist/src/platform/windows/start-hidden-console.ps1 +49 -49
  127. package/dist/src/platform/windows/stop-desktop.ps1 +22 -22
  128. package/dist/src/platform/windows/user-environment.mjs +50 -50
  129. package/dist/src/platform/windows/user-environment.ps1 +41 -41
  130. package/dist/src/project-archive.mjs +30 -0
  131. package/dist/src/project-create.mjs +121 -119
  132. package/dist/src/project-management.mjs +59 -69
  133. package/dist/src/project-remove.mjs +55 -0
  134. package/dist/src/project-update.mjs +56 -0
  135. package/dist/src/runtime-discovery.mjs +47 -47
  136. package/dist/src/self-relay/account-cleanup.mjs +30 -30
  137. package/dist/src/self-relay/account-connections.mjs +52 -52
  138. package/dist/src/self-relay/admin-cloud.mjs +101 -101
  139. package/dist/src/self-relay/admin-page.mjs +80 -80
  140. package/dist/src/self-relay/admin-server.mjs +74 -74
  141. package/dist/src/self-relay/admin-state.mjs +127 -127
  142. package/dist/src/self-relay/admin-style.mjs +14 -14
  143. package/dist/src/self-relay/android-releases.mjs +61 -61
  144. package/dist/src/self-relay/client.mjs +61 -61
  145. package/dist/src/self-relay/connector.mjs +439 -438
  146. package/dist/src/self-relay/debug-allowlist.mjs +33 -33
  147. package/dist/src/self-relay/demo.mjs +37 -37
  148. package/dist/src/self-relay/device-info.mjs +27 -0
  149. package/dist/src/self-relay/device-store.mjs +92 -84
  150. package/dist/src/self-relay/diagnostics.mjs +54 -54
  151. package/dist/src/self-relay/email-accounts.mjs +183 -183
  152. package/dist/src/self-relay/file-wire.mjs +13 -13
  153. package/dist/src/self-relay/lan-authorizations.mjs +71 -42
  154. package/dist/src/self-relay/lifecycle.mjs +264 -250
  155. package/dist/src/self-relay/long-pairing.mjs +40 -40
  156. package/dist/src/self-relay/mysql-state.mjs +118 -105
  157. package/dist/src/self-relay/p2p-probe.mjs +159 -159
  158. package/dist/src/self-relay/privacy-page.mjs +26 -26
  159. package/dist/src/self-relay/public-releases.mjs +52 -0
  160. package/dist/src/self-relay/rtc-file-bridge.mjs +41 -41
  161. package/dist/src/self-relay/server.mjs +1006 -932
  162. package/dist/src/self-relay/usage-daily.mjs +55 -55
  163. package/dist/src/self-relay/usage-meter.mjs +141 -141
  164. package/dist/src/self-relay/windows-route-helper.mjs +75 -75
  165. package/dist/src/self-relay/wire.mjs +36 -36
  166. package/dist/src/server.mjs +1861 -1778
  167. package/dist/src/service-control.mjs +82 -82
  168. package/dist/src/service-doctor.mjs +110 -110
  169. package/dist/src/service-health.mjs +33 -33
  170. package/dist/src/service-install.mjs +91 -91
  171. package/dist/src/service-lifecycle.mjs +138 -138
  172. package/dist/src/service-restart.mjs +81 -81
  173. package/dist/src/shared-app-server.mjs +615 -611
  174. package/dist/src/shared-catalog.mjs +132 -132
  175. package/dist/src/shared-recovery.mjs +146 -146
  176. package/dist/src/shared-thread-stream.mjs +164 -164
  177. package/dist/src/skill-install.mjs +51 -51
  178. package/dist/src/thread-title-generation.mjs +150 -150
  179. package/dist/src/turn-model-history.mjs +47 -47
  180. package/dist/src/usage-reset.mjs +17 -17
  181. package/dist/src/workstation-downloads.mjs +120 -120
  182. package/dist/tools/rtc-route-helper.ps1 +99 -99
  183. package/dist/tools/start-rtc-route-helper.ps1 +15 -15
  184. package/dist/web/vendor/markdown-it.LICENSE.txt +22 -22
  185. package/dist/web/vendor/markdown-it.umd.min.js +13 -13
  186. package/package.json +7 -6
  187. package/tools/postinstall.mjs +127 -127
@@ -1,92 +1,92 @@
1
- (function (root, factory) {
2
- const api = factory();
3
- if (typeof module === 'object' && module.exports) module.exports = api;
4
- else root.CodexFileMentions = api;
5
- })(globalThis, function () {
6
- const heading = '# Files mentioned by the user:';
7
- const pastedHeading = '# Files pasted by the user:';
8
- const request = '## My request:';
9
-
10
- function stripBrowserContext(value) {
11
- const context = /^\s*<in-app-browser-context\b[^>]*>[\s\S]*?<\/in-app-browser-context>\s*/;
12
- if (!context.test(value)) return value;
13
- return value.replace(context, '').replace(/^## My request:[ \t]*(?:\r?\n|$)/, '').trim();
14
- }
15
-
16
- function parse(value) {
17
- const source = stripBrowserContext(typeof value === 'string' ? value : '');
18
- const firstHeading = [heading, pastedHeading].find(value => source.startsWith(value));
19
- if (!firstHeading) return { text: source, files: [] };
20
- const marker = source.indexOf(`\n${request}`, firstHeading.length);
21
- if (marker < 0) return { text: source, files: [] };
22
- const files = [];
23
- let pasted = firstHeading === pastedHeading;
24
- for (const line of source.slice(firstHeading.length, marker).split(/\r?\n/)) {
25
- if (line === heading || line === pastedHeading) { pasted = line === pastedHeading; continue; }
26
- if (line.startsWith('Uploaded file: ')) {
27
- try {
28
- const value = JSON.parse(line.slice('Uploaded file: '.length));
29
- const fileId = /^sediment:\/\/([A-Za-z0-9_-]{1,256})$/.exec(value.pointer)?.[1];
30
- if (fileId && typeof value.fileName === 'string' && value.fileName) files.push({ type: 'file', name: value.fileName, fileId });
31
- } catch { /* Leave unrecognized text untouched when no attachment was parsed. */ }
32
- continue;
33
- }
34
- const media = /^<!-- code-relax-media:(.+) -->$/.exec(line);
35
- if (media && files.length) {
36
- try { Object.assign(files.at(-1), mediaMetadata(JSON.parse(media[1]))); } catch {}
37
- continue;
38
- }
39
- if (!pasted && line.trim() === 'Image attachment: true' && files.length) {
40
- files.at(-1).isImageAttachment = true;
41
- continue;
42
- }
43
- const match = /^## (.+): ((?:[A-Za-z]:[\\/]|\/)[^\r\n]+)$/.exec(line);
44
- if (!match) continue;
45
- let name = match[1];
46
- if (name.startsWith('"')) {
47
- try { name = JSON.parse(name); } catch { continue; }
48
- }
49
- if (typeof name !== 'string' || !name) continue;
50
- files.push({ type: 'file', name: pasted ? match[2].split(/[\\/]/).at(-1) : name, path: match[2] });
51
- }
52
- if (!files.length) return { text: source, files: [] };
53
- return { text: stripBrowserContext(source.slice(marker + request.length + 1).trim()), files };
54
- }
55
-
56
- function wrap(prompt, files) {
57
- if (!files?.length) return prompt;
58
- return `${heading}\n${files.map(file => {
59
- const media = mediaMetadata(file);
60
- return `\n## ${JSON.stringify(file.name)}: ${file.path}\n${media.mediaKind ? `<!-- code-relax-media:${JSON.stringify(media).replace(/</g, '\\u003c').replace(/>/g, '\\u003e')} -->\n` : ''}`;
61
- }).join('')}\n${request}\n${prompt || '请查看上述文件。'}`;
62
- }
63
-
64
- function mediaMetadata(value) {
65
- if (!value || !['image', 'video'].includes(value.mediaKind)) return {};
66
- const result = { mediaKind: value.mediaKind };
67
- for (const field of ['sha256', 'previewSha256']) if (/^[a-f0-9]{64}$/.test(value[field] || '')) result[field] = value[field];
68
- if (typeof value.previewPath === 'string' && /^(?:[A-Za-z]:[\\/]|\/(?!\/))[^\r\n]+$/.test(value.previewPath)) result.previewPath = value.previewPath;
69
- if (/^(?:image|video)\/[a-z0-9.+-]+$/i.test(value.mimeType || '')) result.mimeType = value.mimeType;
70
- for (const field of ['size', 'duration']) if (Number.isFinite(value[field]) && value[field] > 0) result[field] = value[field];
71
- return result;
72
- }
73
-
74
- // Formats accepted as native Codex image inputs; other files remain file references.
75
- function isNativeImage(file) {
76
- return file?.mediaKind === 'image' && /\.(png|jpe?g|webp|gif)$/i.test(file.name || file.path || '');
77
- }
78
- function attachment(file) {
79
- return isNativeImage(file) ? { type: 'image', name: file.name, remotePath: file.path,
80
- originalUpload: true, localImageId: file.sha256, localThumbnailId: file.previewSha256 }
81
- : { ...file, type: 'file' };
82
- }
83
- function originalInputs(body) {
84
- const paths = body.originalImages || [];
85
- if (!Array.isArray(paths) || paths.length + (body.images || []).length > 4
86
- || paths.some(value => typeof value !== 'string' || !/^(?:[A-Za-z]:[\\/]|\/(?!\/))[^\r\n]+\.(png|jpe?g|webp|gif)$/i.test(value))) {
87
- throw new Error('原图路径无效或每条消息图片超过 4 张');
88
- }
89
- return paths.map(path => ({ type: 'localImage', path }));
90
- }
91
- return Object.freeze({ parse, wrap, mediaMetadata, isNativeImage, attachment, originalInputs });
92
- });
1
+ (function (root, factory) {
2
+ const api = factory();
3
+ if (typeof module === 'object' && module.exports) module.exports = api;
4
+ else root.CodexFileMentions = api;
5
+ })(globalThis, function () {
6
+ const heading = '# Files mentioned by the user:';
7
+ const pastedHeading = '# Files pasted by the user:';
8
+ const request = '## My request:';
9
+
10
+ function stripBrowserContext(value) {
11
+ const context = /^\s*<in-app-browser-context\b[^>]*>[\s\S]*?<\/in-app-browser-context>\s*/;
12
+ if (!context.test(value)) return value;
13
+ return value.replace(context, '').replace(/^## My request:[ \t]*(?:\r?\n|$)/, '').trim();
14
+ }
15
+
16
+ function parse(value) {
17
+ const source = stripBrowserContext(typeof value === 'string' ? value : '');
18
+ const firstHeading = [heading, pastedHeading].find(value => source.startsWith(value));
19
+ if (!firstHeading) return { text: source, files: [] };
20
+ const marker = source.indexOf(`\n${request}`, firstHeading.length);
21
+ if (marker < 0) return { text: source, files: [] };
22
+ const files = [];
23
+ let pasted = firstHeading === pastedHeading;
24
+ for (const line of source.slice(firstHeading.length, marker).split(/\r?\n/)) {
25
+ if (line === heading || line === pastedHeading) { pasted = line === pastedHeading; continue; }
26
+ if (line.startsWith('Uploaded file: ')) {
27
+ try {
28
+ const value = JSON.parse(line.slice('Uploaded file: '.length));
29
+ const fileId = /^sediment:\/\/([A-Za-z0-9_-]{1,256})$/.exec(value.pointer)?.[1];
30
+ if (fileId && typeof value.fileName === 'string' && value.fileName) files.push({ type: 'file', name: value.fileName, fileId });
31
+ } catch { /* Leave unrecognized text untouched when no attachment was parsed. */ }
32
+ continue;
33
+ }
34
+ const media = /^<!-- code-relax-media:(.+) -->$/.exec(line);
35
+ if (media && files.length) {
36
+ try { Object.assign(files.at(-1), mediaMetadata(JSON.parse(media[1]))); } catch {}
37
+ continue;
38
+ }
39
+ if (!pasted && line.trim() === 'Image attachment: true' && files.length) {
40
+ files.at(-1).isImageAttachment = true;
41
+ continue;
42
+ }
43
+ const match = /^## (.+): ((?:[A-Za-z]:[\\/]|\/)[^\r\n]+)$/.exec(line);
44
+ if (!match) continue;
45
+ let name = match[1];
46
+ if (name.startsWith('"')) {
47
+ try { name = JSON.parse(name); } catch { continue; }
48
+ }
49
+ if (typeof name !== 'string' || !name) continue;
50
+ files.push({ type: 'file', name: pasted ? match[2].split(/[\\/]/).at(-1) : name, path: match[2] });
51
+ }
52
+ if (!files.length) return { text: source, files: [] };
53
+ return { text: stripBrowserContext(source.slice(marker + request.length + 1).trim()), files };
54
+ }
55
+
56
+ function wrap(prompt, files) {
57
+ if (!files?.length) return prompt;
58
+ return `${heading}\n${files.map(file => {
59
+ const media = mediaMetadata(file);
60
+ return `\n## ${JSON.stringify(file.name)}: ${file.path}\n${media.mediaKind ? `<!-- code-relax-media:${JSON.stringify(media).replace(/</g, '\\u003c').replace(/>/g, '\\u003e')} -->\n` : ''}`;
61
+ }).join('')}\n${request}\n${prompt || '请查看上述文件。'}`;
62
+ }
63
+
64
+ function mediaMetadata(value) {
65
+ if (!value || !['image', 'video'].includes(value.mediaKind)) return {};
66
+ const result = { mediaKind: value.mediaKind };
67
+ for (const field of ['sha256', 'previewSha256']) if (/^[a-f0-9]{64}$/.test(value[field] || '')) result[field] = value[field];
68
+ if (typeof value.previewPath === 'string' && /^(?:[A-Za-z]:[\\/]|\/(?!\/))[^\r\n]+$/.test(value.previewPath)) result.previewPath = value.previewPath;
69
+ if (/^(?:image|video)\/[a-z0-9.+-]+$/i.test(value.mimeType || '')) result.mimeType = value.mimeType;
70
+ for (const field of ['size', 'duration']) if (Number.isFinite(value[field]) && value[field] > 0) result[field] = value[field];
71
+ return result;
72
+ }
73
+
74
+ // Formats accepted as native Codex image inputs; other files remain file references.
75
+ function isNativeImage(file) {
76
+ return file?.mediaKind === 'image' && /\.(png|jpe?g|webp|gif)$/i.test(file.name || file.path || '');
77
+ }
78
+ function attachment(file) {
79
+ return isNativeImage(file) ? { type: 'image', name: file.name, remotePath: file.path,
80
+ originalUpload: true, localImageId: file.sha256, localThumbnailId: file.previewSha256 }
81
+ : { ...file, type: 'file' };
82
+ }
83
+ function originalInputs(body) {
84
+ const paths = body.originalImages || [];
85
+ if (!Array.isArray(paths) || paths.length + (body.images || []).length > 4
86
+ || paths.some(value => typeof value !== 'string' || !/^(?:[A-Za-z]:[\\/]|\/(?!\/))[^\r\n]+\.(png|jpe?g|webp|gif)$/i.test(value))) {
87
+ throw new Error('原图路径无效或每条消息图片超过 4 张');
88
+ }
89
+ return paths.map(path => ({ type: 'localImage', path }));
90
+ }
91
+ return Object.freeze({ parse, wrap, mediaMetadata, isNativeImage, attachment, originalInputs });
92
+ });
@@ -1,54 +1,54 @@
1
- (function (root, factory) {
2
- const value = factory();
3
- if (typeof module === 'object' && module.exports) module.exports = value;
4
- else root.CodexMessageDelivery = value;
5
- })(globalThis, function () {
6
- function rejectedBeforeSubmission(error) {
7
- return error?.rpcCode === -32602 || error?.rpcCode === -32600
8
- || error?.rpcCode != null && /^(no active turn to steer|expected active turn id `)/i.test(error.message || '');
9
- }
10
-
11
- // A UI/request deadline must not discard a later authoritative write receipt.
12
- function retainLateResult(pending, error) {
13
- const reject = pending.reject;
14
- error.deliveryResult = new Promise((resolve, reject) => {
15
- pending.resolve = resolve; pending.reject = reject;
16
- });
17
- error.deliveryResult.catch(() => {});
18
- reject(error);
19
- }
20
-
21
- class Store {
22
- constructor() { this.entries = new Map(); }
23
- key(scope, clientId) { return JSON.stringify([scope, clientId]); }
24
- status(scope, clientId) {
25
- const entry = this.entries.get(this.key(scope, clientId));
26
- return entry ? { state: entry.state, ...(entry.result ? { result: entry.result } : {}),
27
- ...(entry.error ? { error: entry.error.message } : {}) } : { state: 'unknown' };
28
- }
29
- submit(scope, clientId, operation) {
30
- if (!/^[a-f0-9]{32}$/i.test(clientId || '')) return Promise.reject(Object.assign(new Error('消息确认标识无效'), { deliveryUnknown: false }));
31
- const key = this.key(scope, clientId), previous = this.entries.get(key);
32
- if (previous?.state === 'accepted') return Promise.resolve(previous.result);
33
- if (previous?.state === 'sending') return previous.work;
34
- if (previous?.state === 'unknown') return Promise.reject(Object.assign(new Error(previous.error?.message || '原发送结果仍未确认'), { deliveryUnknown: true }));
35
- const entry = { state: 'sending' };
36
- this.entries.set(key, entry);
37
- entry.work = Promise.resolve().then(operation).then(result => {
38
- entry.state = 'accepted'; entry.result = result; return result;
39
- }, error => {
40
- entry.state = error.deliveryUnknown === false || error.details?.deliveryUnknown === false ? 'not_sent' : 'unknown';
41
- error.deliveryUnknown = entry.state !== 'not_sent';
42
- if (error.deliveryResult) error.deliveryResult.then(result => {
43
- entry.state = 'accepted'; entry.result = result; delete entry.error;
44
- }, lateError => {
45
- entry.state = rejectedBeforeSubmission(lateError) || lateError.deliveryUnknown === false ? 'not_sent' : 'unknown';
46
- entry.error = lateError;
47
- });
48
- entry.error = error; throw error;
49
- }).finally(() => { delete entry.work; });
50
- return entry.work;
51
- }
52
- }
53
- return Object.freeze({ Store, rejectedBeforeSubmission, retainLateResult });
54
- });
1
+ (function (root, factory) {
2
+ const value = factory();
3
+ if (typeof module === 'object' && module.exports) module.exports = value;
4
+ else root.CodexMessageDelivery = value;
5
+ })(globalThis, function () {
6
+ function rejectedBeforeSubmission(error) {
7
+ return error?.rpcCode === -32602 || error?.rpcCode === -32600
8
+ || error?.rpcCode != null && /^(no active turn to steer|expected active turn id `)/i.test(error.message || '');
9
+ }
10
+
11
+ // A UI/request deadline must not discard a later authoritative write receipt.
12
+ function retainLateResult(pending, error) {
13
+ const reject = pending.reject;
14
+ error.deliveryResult = new Promise((resolve, reject) => {
15
+ pending.resolve = resolve; pending.reject = reject;
16
+ });
17
+ error.deliveryResult.catch(() => {});
18
+ reject(error);
19
+ }
20
+
21
+ class Store {
22
+ constructor() { this.entries = new Map(); }
23
+ key(scope, clientId) { return JSON.stringify([scope, clientId]); }
24
+ status(scope, clientId) {
25
+ const entry = this.entries.get(this.key(scope, clientId));
26
+ return entry ? { state: entry.state, ...(entry.result ? { result: entry.result } : {}),
27
+ ...(entry.error ? { error: entry.error.message } : {}) } : { state: 'unknown' };
28
+ }
29
+ submit(scope, clientId, operation) {
30
+ if (!/^[a-f0-9]{32}$/i.test(clientId || '')) return Promise.reject(Object.assign(new Error('消息确认标识无效'), { deliveryUnknown: false }));
31
+ const key = this.key(scope, clientId), previous = this.entries.get(key);
32
+ if (previous?.state === 'accepted') return Promise.resolve(previous.result);
33
+ if (previous?.state === 'sending') return previous.work;
34
+ if (previous?.state === 'unknown') return Promise.reject(Object.assign(new Error(previous.error?.message || '原发送结果仍未确认'), { deliveryUnknown: true }));
35
+ const entry = { state: 'sending' };
36
+ this.entries.set(key, entry);
37
+ entry.work = Promise.resolve().then(operation).then(result => {
38
+ entry.state = 'accepted'; entry.result = result; return result;
39
+ }, error => {
40
+ entry.state = error.deliveryUnknown === false || error.details?.deliveryUnknown === false ? 'not_sent' : 'unknown';
41
+ error.deliveryUnknown = entry.state !== 'not_sent';
42
+ if (error.deliveryResult) error.deliveryResult.then(result => {
43
+ entry.state = 'accepted'; entry.result = result; delete entry.error;
44
+ }, lateError => {
45
+ entry.state = rejectedBeforeSubmission(lateError) || lateError.deliveryUnknown === false ? 'not_sent' : 'unknown';
46
+ entry.error = lateError;
47
+ });
48
+ entry.error = error; throw error;
49
+ }).finally(() => { delete entry.work; });
50
+ return entry.work;
51
+ }
52
+ }
53
+ return Object.freeze({ Store, rejectedBeforeSubmission, retainLateResult });
54
+ });
@@ -1,72 +1,72 @@
1
- (function (root, factory) {
2
- if (typeof module === 'object' && module.exports) module.exports = factory();
3
- else root.CodeRelaxP2pDataFramer = factory();
4
- })(globalThis, () => {
5
- const CHUNK_CHARS = 12000;
6
- const MAX_MESSAGE_BYTES = 8 * 1024 * 1024;
7
- const MAX_PARTS = 1024;
8
- const byteLength = value => new TextEncoder().encode(value).length;
9
-
10
- return class {
11
- static IdleWatch = class {
12
- constructor(send, failed) { this.send = send; this.failed = failed; this.serial = 0; this.closed = false; }
13
- received() {
14
- if (this.closed) return;
15
- clearTimeout(this.timer); this.pending = undefined;
16
- this.timer = setTimeout(() => {
17
- this.pending = String(++this.serial);
18
- this.timer = setTimeout(() => { this.close(); this.failed(); }, 15000);
19
- try { this.send({ type: 'idle-ping', nonce: this.pending }); } catch { this.close(); this.failed(); }
20
- }, 15000);
21
- this.timer.unref?.();
22
- }
23
- accept(message) {
24
- if (!['idle-ping', 'idle-pong'].includes(message.type)) return false;
25
- if (typeof message.nonce !== 'string' || !/^[0-9]{1,12}$/.test(message.nonce)) throw new Error('Invalid idle probe');
26
- if (message.type === 'idle-pong' && message.nonce !== this.pending) return true;
27
- this.received();
28
- if (message.type === 'idle-ping') this.send({ type: 'idle-pong', nonce: message.nonce });
29
- return true;
30
- }
31
- close() { this.closed = true; clearTimeout(this.timer); this.pending = undefined; }
32
- };
33
- constructor() { this.inbound = undefined; }
34
- create(payload, id) {
35
- if (typeof payload !== 'string' || !/^[a-f0-9]{32}-[0-9]{1,10}$/.test(id)) throw new Error('Invalid P2P payload');
36
- const bytes = byteLength(payload);
37
- if (bytes > MAX_MESSAGE_BYTES) throw new Error('Code Relax 直连消息超过 8 MiB');
38
- const total = Math.max(1, Math.ceil(payload.length / CHUNK_CHARS));
39
- if (total > MAX_PARTS) throw new Error('Code Relax 直连消息分片过多');
40
- return { id, payload, bytes, index: 0, total };
41
- }
42
- frame(item) {
43
- if (!item || item.index >= item.total) return undefined;
44
- const start = item.index * CHUNK_CHARS;
45
- return JSON.stringify({ type: 'data', id: item.id, index: item.index, total: item.total,
46
- payload: item.payload.slice(start, start + CHUNK_CHARS) });
47
- }
48
- commit(item) { item.index++; return item.index >= item.total; }
49
- accept(message) {
50
- if (!message || message.type !== 'data') return { handled: false };
51
- if (!/^[a-f0-9]{32}-[0-9]{1,10}$/.test(message.id || '') || !Number.isSafeInteger(message.index) ||
52
- !Number.isSafeInteger(message.total) || message.total < 1 || message.total > MAX_PARTS ||
53
- message.index < 0 || message.index >= message.total || typeof message.payload !== 'string' ||
54
- message.payload.length > CHUNK_CHARS) throw new Error('Invalid P2P data frame');
55
- if (!this.inbound) {
56
- if (message.index !== 0) throw new Error('Invalid P2P data order');
57
- this.inbound = { id: message.id, total: message.total, next: 0, bytes: 0, parts: [] };
58
- }
59
- const current = this.inbound;
60
- if (current.id !== message.id || current.total !== message.total || current.next !== message.index) {
61
- throw new Error('Invalid P2P data order');
62
- }
63
- current.bytes += byteLength(message.payload);
64
- if (current.bytes > MAX_MESSAGE_BYTES) throw new Error('P2P data message too large');
65
- current.parts.push(message.payload); current.next++;
66
- if (current.next !== current.total) return { handled: true, complete: false };
67
- this.inbound = undefined;
68
- return { handled: true, complete: true, payload: current.parts.join('') };
69
- }
70
- reset() { this.inbound = undefined; }
71
- };
72
- });
1
+ (function (root, factory) {
2
+ if (typeof module === 'object' && module.exports) module.exports = factory();
3
+ else root.CodeRelaxP2pDataFramer = factory();
4
+ })(globalThis, () => {
5
+ const CHUNK_CHARS = 12000;
6
+ const MAX_MESSAGE_BYTES = 8 * 1024 * 1024;
7
+ const MAX_PARTS = 1024;
8
+ const byteLength = value => new TextEncoder().encode(value).length;
9
+
10
+ return class {
11
+ static IdleWatch = class {
12
+ constructor(send, failed) { this.send = send; this.failed = failed; this.serial = 0; this.closed = false; }
13
+ received() {
14
+ if (this.closed) return;
15
+ clearTimeout(this.timer); this.pending = undefined;
16
+ this.timer = setTimeout(() => {
17
+ this.pending = String(++this.serial);
18
+ this.timer = setTimeout(() => { this.close(); this.failed(); }, 15000);
19
+ try { this.send({ type: 'idle-ping', nonce: this.pending }); } catch { this.close(); this.failed(); }
20
+ }, 15000);
21
+ this.timer.unref?.();
22
+ }
23
+ accept(message) {
24
+ if (!['idle-ping', 'idle-pong'].includes(message.type)) return false;
25
+ if (typeof message.nonce !== 'string' || !/^[0-9]{1,12}$/.test(message.nonce)) throw new Error('Invalid idle probe');
26
+ if (message.type === 'idle-pong' && message.nonce !== this.pending) return true;
27
+ this.received();
28
+ if (message.type === 'idle-ping') this.send({ type: 'idle-pong', nonce: message.nonce });
29
+ return true;
30
+ }
31
+ close() { this.closed = true; clearTimeout(this.timer); this.pending = undefined; }
32
+ };
33
+ constructor() { this.inbound = undefined; }
34
+ create(payload, id) {
35
+ if (typeof payload !== 'string' || !/^[a-f0-9]{32}-[0-9]{1,10}$/.test(id)) throw new Error('Invalid P2P payload');
36
+ const bytes = byteLength(payload);
37
+ if (bytes > MAX_MESSAGE_BYTES) throw new Error('Code Relax 直连消息超过 8 MiB');
38
+ const total = Math.max(1, Math.ceil(payload.length / CHUNK_CHARS));
39
+ if (total > MAX_PARTS) throw new Error('Code Relax 直连消息分片过多');
40
+ return { id, payload, bytes, index: 0, total };
41
+ }
42
+ frame(item) {
43
+ if (!item || item.index >= item.total) return undefined;
44
+ const start = item.index * CHUNK_CHARS;
45
+ return JSON.stringify({ type: 'data', id: item.id, index: item.index, total: item.total,
46
+ payload: item.payload.slice(start, start + CHUNK_CHARS) });
47
+ }
48
+ commit(item) { item.index++; return item.index >= item.total; }
49
+ accept(message) {
50
+ if (!message || message.type !== 'data') return { handled: false };
51
+ if (!/^[a-f0-9]{32}-[0-9]{1,10}$/.test(message.id || '') || !Number.isSafeInteger(message.index) ||
52
+ !Number.isSafeInteger(message.total) || message.total < 1 || message.total > MAX_PARTS ||
53
+ message.index < 0 || message.index >= message.total || typeof message.payload !== 'string' ||
54
+ message.payload.length > CHUNK_CHARS) throw new Error('Invalid P2P data frame');
55
+ if (!this.inbound) {
56
+ if (message.index !== 0) throw new Error('Invalid P2P data order');
57
+ this.inbound = { id: message.id, total: message.total, next: 0, bytes: 0, parts: [] };
58
+ }
59
+ const current = this.inbound;
60
+ if (current.id !== message.id || current.total !== message.total || current.next !== message.index) {
61
+ throw new Error('Invalid P2P data order');
62
+ }
63
+ current.bytes += byteLength(message.payload);
64
+ if (current.bytes > MAX_MESSAGE_BYTES) throw new Error('P2P data message too large');
65
+ current.parts.push(message.payload); current.next++;
66
+ if (current.next !== current.total) return { handled: true, complete: false };
67
+ this.inbound = undefined;
68
+ return { handled: true, complete: true, payload: current.parts.join('') };
69
+ }
70
+ reset() { this.inbound = undefined; }
71
+ };
72
+ });
@@ -1,28 +1,28 @@
1
- (function (root, factory) {
2
- if (typeof module === 'object' && module.exports) module.exports = factory();
3
- else root.CodeRelaxP2pDiagnostics = factory();
4
- })(globalThis, () => class {
5
- constructor() {
6
- this.started = Date.now(); this.local = [0, 0, 0, 0, 0]; this.remote = [0, 0, 0, 0, 0];
7
- this.offer = false; this.answer = false; this.open = false; this.ping = false; this.pong = false;
8
- this.ice = 'new'; this.gathering = 'new'; this.error = 'none';
9
- }
10
- candidate(side, candidate) {
11
- const type = /\btyp (host|srflx|prflx|relay)\b/.exec(candidate || '')?.[1];
12
- const index = ['host', 'srflx', 'prflx', 'relay'].indexOf(type);
13
- const counts = side === 'local' ? this.local : this.remote;
14
- counts[index < 0 ? 4 : index]++;
15
- }
16
- snapshot(stage) {
17
- const known = (value, choices) => choices.includes(value) ? value : 'unknown';
18
- const counts = values => values.map(value => Math.min(value, 999)).join(',');
19
- return `stage=${known(stage, ['verified', 'timeout', 'failed', 'closed', 'complete', 'replaced', 'unsupported'])}` +
20
- `;ms=${Math.min(Date.now() - this.started, 999999)}` +
21
- `;offer=${Number(this.offer)};answer=${Number(this.answer)}` +
22
- `;ice=${known(this.ice, ['new', 'checking', 'connected', 'completed', 'failed', 'disconnected', 'closed'])}` +
23
- `;gathering=${known(this.gathering, ['new', 'in-progress', 'gathering', 'complete'])}` +
24
- `;local=${counts(this.local)};remote=${counts(this.remote)}` +
25
- `;open=${Number(this.open)};ping=${Number(this.ping)};pong=${Number(this.pong)}` +
26
- `;error=${known(this.error, ['none', 'stun', 'description', 'candidate', 'channel', 'frame', 'peer', 'initialization', 'signal'])}`;
27
- }
28
- });
1
+ (function (root, factory) {
2
+ if (typeof module === 'object' && module.exports) module.exports = factory();
3
+ else root.CodeRelaxP2pDiagnostics = factory();
4
+ })(globalThis, () => class {
5
+ constructor() {
6
+ this.started = Date.now(); this.local = [0, 0, 0, 0, 0]; this.remote = [0, 0, 0, 0, 0];
7
+ this.offer = false; this.answer = false; this.open = false; this.ping = false; this.pong = false;
8
+ this.ice = 'new'; this.gathering = 'new'; this.error = 'none';
9
+ }
10
+ candidate(side, candidate) {
11
+ const type = /\btyp (host|srflx|prflx|relay)\b/.exec(candidate || '')?.[1];
12
+ const index = ['host', 'srflx', 'prflx', 'relay'].indexOf(type);
13
+ const counts = side === 'local' ? this.local : this.remote;
14
+ counts[index < 0 ? 4 : index]++;
15
+ }
16
+ snapshot(stage) {
17
+ const known = (value, choices) => choices.includes(value) ? value : 'unknown';
18
+ const counts = values => values.map(value => Math.min(value, 999)).join(',');
19
+ return `stage=${known(stage, ['verified', 'timeout', 'failed', 'closed', 'complete', 'replaced', 'unsupported'])}` +
20
+ `;ms=${Math.min(Date.now() - this.started, 999999)}` +
21
+ `;offer=${Number(this.offer)};answer=${Number(this.answer)}` +
22
+ `;ice=${known(this.ice, ['new', 'checking', 'connected', 'completed', 'failed', 'disconnected', 'closed'])}` +
23
+ `;gathering=${known(this.gathering, ['new', 'in-progress', 'gathering', 'complete'])}` +
24
+ `;local=${counts(this.local)};remote=${counts(this.remote)}` +
25
+ `;open=${Number(this.open)};ping=${Number(this.ping)};pong=${Number(this.pong)}` +
26
+ `;error=${known(this.error, ['none', 'stun', 'description', 'candidate', 'channel', 'frame', 'peer', 'initialization', 'signal'])}`;
27
+ }
28
+ });