@jingyi0605/codingns 0.9.0 → 0.9.5

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 (224) hide show
  1. package/dist/public/assets/{AdaptiveButlerPage-B17QiMyT.js → AdaptiveButlerPage-kkJDsnCO.js} +2 -2
  2. package/dist/public/assets/{App-CFBwDUNA.js → App-DrNI9lWA.js} +6 -6
  3. package/dist/public/assets/{BootstrapPage-W5wU3BPh.js → BootstrapPage-QgVH5Mps.js} +1 -1
  4. package/dist/public/assets/{ConversationPage-DQLX1bUh.js → ConversationPage-DVk8VfIj.js} +1 -1
  5. package/dist/public/assets/{DesktopDetachPreviewPage-DTPeuAW-.js → DesktopDetachPreviewPage-BhfP0TpH.js} +1 -1
  6. package/dist/public/assets/{DesktopModal-6ii53_Y9.js → DesktopModal-DRmDrv0S.js} +1 -1
  7. package/dist/public/assets/DesktopWindowPage-DNbJXnSs.js +2 -0
  8. package/dist/public/assets/FileContextPanel---fLO4ve.js +1 -0
  9. package/dist/public/assets/GitSidebar-sXUE0TqT.js +6 -0
  10. package/dist/public/assets/MobileCreateSessionSheet-BftZ5pvb.js +1 -0
  11. package/dist/public/assets/{MobileSheet-opTWyRe1.js → MobileSheet-nw5SCa3N.js} +1 -1
  12. package/dist/public/assets/{MobileTopHeaderFrame-BbNON3Y4.js → MobileTopHeaderFrame-DH_D02Wy.js} +1 -1
  13. package/dist/public/assets/{MobileWorkspaceSwitcherHeader-BZEzPeMj.js → MobileWorkspaceSwitcherHeader-2K406G5p.js} +1 -1
  14. package/dist/public/assets/{PluginAccessOverview-mQDmAljp.js → PluginAccessOverview-BVJihw3D.js} +1 -1
  15. package/dist/public/assets/{PluginContainerPage-CcxUJpM4.js → PluginContainerPage-CR4vStvr.js} +1 -1
  16. package/dist/public/assets/{PluginDetailPage-D5--ACIt.js → PluginDetailPage-CrMX0Mnm.js} +1 -1
  17. package/dist/public/assets/{PluginsListPage-D_oJxYXT.js → PluginsListPage-FtIL71Yg.js} +1 -1
  18. package/dist/public/assets/{RelayConnectEntryPage-DROxpnkv.js → RelayConnectEntryPage-Bt1apX53.js} +1 -1
  19. package/dist/public/assets/{ServerSettingsModal-CUUOPqSe.js → ServerSettingsModal-D-guzPrI.js} +1 -1
  20. package/dist/public/assets/{SessionIndexPage-C2Jxh6Gp.js → SessionIndexPage-CX2FppcJ.js} +1 -1
  21. package/dist/public/assets/SettingsPage-BI2Olcvr.js +2 -0
  22. package/dist/public/assets/TerminalManagerPanel-B5MKGPy-.js +1 -0
  23. package/dist/public/assets/{TerminalPage-CwWyFDj8.js → TerminalPage-C2dTNGHK.js} +1 -1
  24. package/dist/public/assets/{TerminalRuntimeFallbackModal-CSVVbO8r.js → TerminalRuntimeFallbackModal-DAqOxFD8.js} +1 -1
  25. package/dist/public/assets/{ToolFilesPage-QBEY8oCf.js → ToolFilesPage-IsNwyE6T.js} +1 -1
  26. package/dist/public/assets/{ToolGitPage-BKoZ2l9v.js → ToolGitPage-BK1JBERN.js} +1 -1
  27. package/dist/public/assets/{ToolProcessesPage-BOH0ib4G.js → ToolProcessesPage-DwTYUQCK.js} +1 -1
  28. package/dist/public/assets/{ToolsHomePage-BcMZ3BCQ.js → ToolsHomePage-BLOy7lPg.js} +1 -1
  29. package/dist/public/assets/{WorkbenchLandingPage-B5zoppEl.js → WorkbenchLandingPage-CqZKR6EA.js} +1 -1
  30. package/dist/public/assets/WorkbenchLayout-CJHQtwuL.js +1022 -0
  31. package/dist/public/assets/{WorkbenchModal-NGmPgqaE.js → WorkbenchModal-BM-OeW-b.js} +1 -1
  32. package/dist/public/assets/WorkbenchShellRoute-2bKI6Q9k.js +1 -0
  33. package/dist/public/assets/WorkbenchShellRoute-BjuZD101.css +1 -0
  34. package/dist/public/assets/WorkspaceDebugDetailPage-BMsEN5iG.js +1 -0
  35. package/dist/public/assets/WorkspaceDetailPage-5H9Gosx2.js +1 -0
  36. package/dist/public/assets/WorkspaceHomePage-DQiXKgiP.js +1 -0
  37. package/dist/public/assets/{client-runtime-manager-DXbI9K1K.js → client-runtime-manager-CgPJq21V.js} +1 -1
  38. package/dist/public/assets/index-BARqMVSw.css +1 -0
  39. package/dist/public/assets/index-BUoNjVrY.js +50 -0
  40. package/dist/public/assets/{login-direct-candidate-resolver-DkKyFtQJ.js → login-direct-candidate-resolver-CGaxAXV8.js} +1 -1
  41. package/dist/public/assets/{plugin-permission-copy-CzN269Bk.js → plugin-permission-copy-BR9gWy8b.js} +1 -1
  42. package/dist/public/assets/{plugins-api-Bv9DHpLF.js → plugins-api-CdCsrG2e.js} +1 -1
  43. package/dist/public/assets/{preferences-service-D2ISL2Zz.js → preferences-service-lOhnlxzP.js} +1 -1
  44. package/dist/public/assets/{relay-entry-Bg0OisQy.js → relay-entry-CQpxTS8y.js} +1 -1
  45. package/dist/public/assets/{terminal-runtime-meta-C8t-CIDF.js → terminal-runtime-meta-oteTx66X.js} +1 -1
  46. package/dist/public/assets/useRegisteredDebugTemplates-Bu2ykZ6s.js +1 -0
  47. package/dist/public/assets/workbench-navigation-DlgXuFW2.js +1 -0
  48. package/dist/public/index.html +2 -2
  49. package/dist/server/config/env.d.ts +1 -0
  50. package/dist/server/config/env.js +3 -0
  51. package/dist/server/config/env.js.map +1 -1
  52. package/dist/server/modules/affairs-indexer/core/src/parser/parser-skip-repository.js.map +1 -1
  53. package/dist/server/modules/affairs-indexer/core/src/repositories/catalog-write-repository.js.map +1 -1
  54. package/dist/server/modules/affairs-indexer/core/src/sqlite/detect-catalog-schema.js +2 -2
  55. package/dist/server/modules/affairs-indexer/core/src/sqlite/detect-catalog-schema.js.map +1 -1
  56. package/dist/server/modules/affairs-indexer/core/src/sqlite/open-database.d.ts +20 -3
  57. package/dist/server/modules/affairs-indexer/core/src/sqlite/open-database.js +3 -3
  58. package/dist/server/modules/affairs-indexer/core/src/sqlite/open-database.js.map +1 -1
  59. package/dist/server/modules/butler/assistant-sandbox-cleanup-scheduler.d.ts +32 -0
  60. package/dist/server/modules/butler/assistant-sandbox-cleanup-scheduler.js +93 -0
  61. package/dist/server/modules/butler/assistant-sandbox-cleanup-scheduler.js.map +1 -0
  62. package/dist/server/modules/butler/assistant-sandbox-service.d.ts +69 -0
  63. package/dist/server/modules/butler/assistant-sandbox-service.js +399 -0
  64. package/dist/server/modules/butler/assistant-sandbox-service.js.map +1 -0
  65. package/dist/server/modules/butler/butler-follow-up-service.d.ts +3 -0
  66. package/dist/server/modules/butler/butler-follow-up-service.js +11 -1
  67. package/dist/server/modules/butler/butler-follow-up-service.js.map +1 -1
  68. package/dist/server/modules/butler/butler-inbox-service.d.ts +3 -0
  69. package/dist/server/modules/butler/butler-inbox-service.js +14 -2
  70. package/dist/server/modules/butler/butler-inbox-service.js.map +1 -1
  71. package/dist/server/modules/butler/butler-session-service.d.ts +3 -0
  72. package/dist/server/modules/butler/butler-session-service.js +18 -0
  73. package/dist/server/modules/butler/butler-session-service.js.map +1 -1
  74. package/dist/server/modules/channels/wechat-claw-client.d.ts +51 -0
  75. package/dist/server/modules/channels/wechat-claw-client.js +245 -0
  76. package/dist/server/modules/channels/wechat-claw-client.js.map +1 -0
  77. package/dist/server/modules/file/file-controller.d.ts +11 -2
  78. package/dist/server/modules/file/file-controller.js +404 -17
  79. package/dist/server/modules/file/file-controller.js.map +1 -1
  80. package/dist/server/modules/file/file-search-service.js +200 -12
  81. package/dist/server/modules/file/file-search-service.js.map +1 -1
  82. package/dist/server/modules/file/runtime/codingns-workspace-bridge.js +18 -5
  83. package/dist/server/modules/file/workspace-file-bridge-service.d.ts +9 -0
  84. package/dist/server/modules/file/workspace-file-bridge-service.js +3 -0
  85. package/dist/server/modules/file/workspace-file-bridge-service.js.map +1 -1
  86. package/dist/server/modules/file/workspace-file-bridge-watch-service.d.ts +9 -0
  87. package/dist/server/modules/file/workspace-file-bridge-watch-service.js +28 -0
  88. package/dist/server/modules/file/workspace-file-bridge-watch-service.js.map +1 -1
  89. package/dist/server/modules/tasks/task-types.d.ts +1 -0
  90. package/dist/server/modules/tasks/task-types.js +1 -0
  91. package/dist/server/modules/tasks/task-types.js.map +1 -1
  92. package/dist/server/modules/workbench/affairs-assistant-session-snapshot-service.d.ts +1 -1
  93. package/dist/server/modules/workbench/affairs-assistant-session-snapshot-service.js +22 -5
  94. package/dist/server/modules/workbench/affairs-assistant-session-snapshot-service.js.map +1 -1
  95. package/dist/server/modules/workbench/workbench-controller.d.ts +5 -0
  96. package/dist/server/modules/workbench/workbench-controller.js +20 -0
  97. package/dist/server/modules/workbench/workbench-controller.js.map +1 -1
  98. package/dist/server/modules/workbench/workbench-service.d.ts +6 -6
  99. package/dist/server/modules/workbench/workbench-service.js +33 -36
  100. package/dist/server/modules/workbench/workbench-service.js.map +1 -1
  101. package/dist/server/modules/workspace/affairs-library-controller.d.ts +8 -0
  102. package/dist/server/modules/workspace/affairs-library-controller.js +11 -0
  103. package/dist/server/modules/workspace/affairs-library-controller.js.map +1 -1
  104. package/dist/server/modules/workspace/affairs-library-preview-link-service.d.ts +6 -0
  105. package/dist/server/modules/workspace/affairs-library-preview-link-service.js +12 -2
  106. package/dist/server/modules/workspace/affairs-library-preview-link-service.js.map +1 -1
  107. package/dist/server/modules/workspace/affairs-library-service.d.ts +9 -0
  108. package/dist/server/modules/workspace/affairs-library-service.js +182 -42
  109. package/dist/server/modules/workspace/affairs-library-service.js.map +1 -1
  110. package/dist/server/modules/workspace/affairs-lightweight-session-controller.d.ts +8 -0
  111. package/dist/server/modules/workspace/affairs-lightweight-session-controller.js +55 -8
  112. package/dist/server/modules/workspace/affairs-lightweight-session-controller.js.map +1 -1
  113. package/dist/server/modules/workspace/affairs-lightweight-session-service.d.ts +13 -0
  114. package/dist/server/modules/workspace/affairs-lightweight-session-service.js +167 -21
  115. package/dist/server/modules/workspace/affairs-lightweight-session-service.js.map +1 -1
  116. package/dist/server/modules/workspace/affairs-tag-controller.d.ts +3 -0
  117. package/dist/server/modules/workspace/affairs-tag-controller.js +5 -0
  118. package/dist/server/modules/workspace/affairs-tag-controller.js.map +1 -1
  119. package/dist/server/modules/workspace/affairs-tag-service.d.ts +22 -1
  120. package/dist/server/modules/workspace/affairs-tag-service.js +41 -2
  121. package/dist/server/modules/workspace/affairs-tag-service.js.map +1 -1
  122. package/dist/server/modules/workspace/teable-api-client.d.ts +118 -0
  123. package/dist/server/modules/workspace/teable-api-client.js +142 -0
  124. package/dist/server/modules/workspace/teable-api-client.js.map +1 -0
  125. package/dist/server/modules/workspace/teable-catalog-controller.d.ts +18 -0
  126. package/dist/server/modules/workspace/teable-catalog-controller.js +17 -0
  127. package/dist/server/modules/workspace/teable-catalog-controller.js.map +1 -0
  128. package/dist/server/modules/workspace/teable-catalog-service.d.ts +36 -0
  129. package/dist/server/modules/workspace/teable-catalog-service.js +124 -0
  130. package/dist/server/modules/workspace/teable-catalog-service.js.map +1 -0
  131. package/dist/server/modules/workspace/teable-credential-service.d.ts +8 -0
  132. package/dist/server/modules/workspace/teable-credential-service.js +37 -0
  133. package/dist/server/modules/workspace/teable-credential-service.js.map +1 -0
  134. package/dist/server/modules/workspace/teable-field-mapping-controller.d.ts +25 -0
  135. package/dist/server/modules/workspace/teable-field-mapping-controller.js +31 -0
  136. package/dist/server/modules/workspace/teable-field-mapping-controller.js.map +1 -0
  137. package/dist/server/modules/workspace/teable-field-mapping-service.d.ts +38 -0
  138. package/dist/server/modules/workspace/teable-field-mapping-service.js +215 -0
  139. package/dist/server/modules/workspace/teable-field-mapping-service.js.map +1 -0
  140. package/dist/server/modules/workspace/teable-global-binding-controller.d.ts +22 -0
  141. package/dist/server/modules/workspace/teable-global-binding-controller.js +25 -0
  142. package/dist/server/modules/workspace/teable-global-binding-controller.js.map +1 -0
  143. package/dist/server/modules/workspace/teable-global-binding-service.d.ts +35 -0
  144. package/dist/server/modules/workspace/teable-global-binding-service.js +151 -0
  145. package/dist/server/modules/workspace/teable-global-binding-service.js.map +1 -0
  146. package/dist/server/modules/workspace/teable-mirror-sync-controller.d.ts +29 -0
  147. package/dist/server/modules/workspace/teable-mirror-sync-controller.js +50 -0
  148. package/dist/server/modules/workspace/teable-mirror-sync-controller.js.map +1 -0
  149. package/dist/server/modules/workspace/teable-mirror-sync-service.d.ts +157 -0
  150. package/dist/server/modules/workspace/teable-mirror-sync-service.js +917 -0
  151. package/dist/server/modules/workspace/teable-mirror-sync-service.js.map +1 -0
  152. package/dist/server/modules/workspace/teable-runtime-controller.d.ts +58 -0
  153. package/dist/server/modules/workspace/teable-runtime-controller.js +60 -0
  154. package/dist/server/modules/workspace/teable-runtime-controller.js.map +1 -0
  155. package/dist/server/modules/workspace/teable-runtime-service.d.ts +96 -0
  156. package/dist/server/modules/workspace/teable-runtime-service.js +362 -0
  157. package/dist/server/modules/workspace/teable-runtime-service.js.map +1 -0
  158. package/dist/server/modules/workspace/teable-workbench-sync-config-controller.d.ts +22 -0
  159. package/dist/server/modules/workspace/teable-workbench-sync-config-controller.js +20 -0
  160. package/dist/server/modules/workspace/teable-workbench-sync-config-controller.js.map +1 -0
  161. package/dist/server/modules/workspace/teable-workbench-sync-config-service.d.ts +22 -0
  162. package/dist/server/modules/workspace/teable-workbench-sync-config-service.js +159 -0
  163. package/dist/server/modules/workspace/teable-workbench-sync-config-service.js.map +1 -0
  164. package/dist/server/routes/affairs.d.ts +9 -1
  165. package/dist/server/routes/affairs.js +120 -1
  166. package/dist/server/routes/affairs.js.map +1 -1
  167. package/dist/server/routes/workbench.js +15 -0
  168. package/dist/server/routes/workbench.js.map +1 -1
  169. package/dist/server/routes/workspaces.js +51 -41
  170. package/dist/server/routes/workspaces.js.map +1 -1
  171. package/dist/server/server/create-server.d.ts +14 -0
  172. package/dist/server/server/create-server.js +73 -7
  173. package/dist/server/server/create-server.js.map +1 -1
  174. package/dist/server/storage/repositories/assistant-sandbox-workspace-repository.d.ts +18 -0
  175. package/dist/server/storage/repositories/assistant-sandbox-workspace-repository.js +191 -0
  176. package/dist/server/storage/repositories/assistant-sandbox-workspace-repository.js.map +1 -0
  177. package/dist/server/storage/repositories/user-affairs-library-setting-repository.js +8 -5
  178. package/dist/server/storage/repositories/user-affairs-library-setting-repository.js.map +1 -1
  179. package/dist/server/storage/repositories/user-teable-credential-repository.d.ts +9 -0
  180. package/dist/server/storage/repositories/user-teable-credential-repository.js +45 -0
  181. package/dist/server/storage/repositories/user-teable-credential-repository.js.map +1 -0
  182. package/dist/server/storage/repositories/user-teable-field-mapping-repository.d.ts +10 -0
  183. package/dist/server/storage/repositories/user-teable-field-mapping-repository.js +69 -0
  184. package/dist/server/storage/repositories/user-teable-field-mapping-repository.js.map +1 -0
  185. package/dist/server/storage/repositories/user-teable-global-setting-repository.d.ts +8 -0
  186. package/dist/server/storage/repositories/user-teable-global-setting-repository.js +52 -0
  187. package/dist/server/storage/repositories/user-teable-global-setting-repository.js.map +1 -0
  188. package/dist/server/storage/repositories/user-teable-mirror-record-mapping-repository.d.ts +9 -0
  189. package/dist/server/storage/repositories/user-teable-mirror-record-mapping-repository.js +66 -0
  190. package/dist/server/storage/repositories/user-teable-mirror-record-mapping-repository.js.map +1 -0
  191. package/dist/server/storage/repositories/user-teable-mirror-table-binding-repository.d.ts +9 -0
  192. package/dist/server/storage/repositories/user-teable-mirror-table-binding-repository.js +67 -0
  193. package/dist/server/storage/repositories/user-teable-mirror-table-binding-repository.js.map +1 -0
  194. package/dist/server/storage/repositories/user-teable-sync-log-repository.d.ts +14 -0
  195. package/dist/server/storage/repositories/user-teable-sync-log-repository.js +97 -0
  196. package/dist/server/storage/repositories/user-teable-sync-log-repository.js.map +1 -0
  197. package/dist/server/storage/repositories/user-teable-workbench-sync-config-repository.d.ts +8 -0
  198. package/dist/server/storage/repositories/user-teable-workbench-sync-config-repository.js +55 -0
  199. package/dist/server/storage/repositories/user-teable-workbench-sync-config-repository.js.map +1 -0
  200. package/dist/server/storage/sqlite/client.js +404 -1
  201. package/dist/server/storage/sqlite/client.js.map +1 -1
  202. package/dist/server/storage/sqlite/schema.sql +167 -1
  203. package/dist/server/types/domain.d.ts +106 -0
  204. package/node_modules/@codingns/session-sync-core/dist/sqlite/node-sqlite.d.ts +22 -3
  205. package/node_modules/@codingns/session-sync-core/dist/sqlite/node-sqlite.js +29 -2
  206. package/node_modules/@codingns/session-sync-core/dist/sqlite/node-sqlite.js.map +1 -1
  207. package/node_modules/@codingns/session-sync-core/package.json +3 -1
  208. package/package.json +1 -1
  209. package/dist/public/assets/DesktopWindowPage-D0blSuKd.js +0 -2
  210. package/dist/public/assets/FileContextPanel-BrKO8Xt6.js +0 -1
  211. package/dist/public/assets/GitSidebar-BdwiDtOr.js +0 -6
  212. package/dist/public/assets/MobileCreateSessionSheet-Cx_dBiBb.js +0 -1
  213. package/dist/public/assets/SettingsPage-BlAZCHsy.js +0 -2
  214. package/dist/public/assets/TerminalManagerPanel-CjzbiWjl.js +0 -1
  215. package/dist/public/assets/WorkbenchLayout-CikJBS62.js +0 -1019
  216. package/dist/public/assets/WorkbenchShellRoute-BbbSOiZw.js +0 -1
  217. package/dist/public/assets/WorkbenchShellRoute-DT3VMjWD.css +0 -1
  218. package/dist/public/assets/WorkspaceDebugDetailPage-CVivdPx5.js +0 -1
  219. package/dist/public/assets/WorkspaceDetailPage-DgOSjscR.js +0 -1
  220. package/dist/public/assets/WorkspaceHomePage-HPa7M_Vh.js +0 -1
  221. package/dist/public/assets/index-BxJPQpFM.css +0 -1
  222. package/dist/public/assets/index-CeXGOT_T.js +0 -50
  223. package/dist/public/assets/useRegisteredDebugTemplates-Bol3NVfN.js +0 -1
  224. package/dist/public/assets/workbench-navigation-B7IjRQd8.js +0 -1
@@ -0,0 +1,917 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { AppError } from "../../shared/errors/app-error.js";
3
+ import { nowIso } from "../../shared/utils/time.js";
4
+ import { HOST_TASK_TYPES } from "../tasks/task-types.js";
5
+ import { TeableApiClient } from "./teable-api-client.js";
6
+ const SUPPORTED_MIRROR_TYPES = ["tags", "sessions", "todos"];
7
+ const SUPPORTED_READ_ONLY_MODES = ["role_based", "matrix_based", "unknown"];
8
+ export class TeableMirrorSyncService {
9
+ mirrorTableBindingRepository;
10
+ mirrorRecordMappingRepository;
11
+ taskManager;
12
+ teableGlobalBindingService;
13
+ teableCredentialService;
14
+ teableWorkbenchSyncConfigService;
15
+ affairsTagService;
16
+ affairsLightweightSessionService;
17
+ butlerInboxItemRepository;
18
+ butlerProjectRepository;
19
+ butlerFollowUpTaskRepository;
20
+ workspaceRepository;
21
+ teableFieldMappingService;
22
+ syncLogRepository;
23
+ constructor(mirrorTableBindingRepository, mirrorRecordMappingRepository, extra1, extra2, extra3, extra4, extra5, extra6, extra7, extra8, extra9, extra10, extra11, extra12) {
24
+ this.mirrorTableBindingRepository = mirrorTableBindingRepository;
25
+ this.mirrorRecordMappingRepository = mirrorRecordMappingRepository;
26
+ if (isTaskManagerLike(extra1)) {
27
+ this.taskManager = extra1;
28
+ this.teableGlobalBindingService = extra2;
29
+ this.teableCredentialService = extra3;
30
+ this.teableWorkbenchSyncConfigService = extra4;
31
+ this.affairsTagService = extra5;
32
+ this.affairsLightweightSessionService = extra6;
33
+ this.butlerInboxItemRepository = extra7;
34
+ this.butlerProjectRepository = extra8;
35
+ this.butlerFollowUpTaskRepository = extra9;
36
+ this.workspaceRepository = extra10;
37
+ this.teableFieldMappingService = extra11;
38
+ this.syncLogRepository = extra12;
39
+ }
40
+ else {
41
+ this.taskManager = undefined;
42
+ this.teableGlobalBindingService = extra1;
43
+ this.teableCredentialService = extra2;
44
+ this.teableWorkbenchSyncConfigService = extra3;
45
+ this.affairsTagService = extra4;
46
+ this.affairsLightweightSessionService = extra5;
47
+ this.butlerInboxItemRepository = extra6;
48
+ this.butlerProjectRepository = extra7;
49
+ this.butlerFollowUpTaskRepository = extra8;
50
+ this.workspaceRepository = extra9;
51
+ this.teableFieldMappingService = extra10;
52
+ this.syncLogRepository = extra11;
53
+ }
54
+ this.registerBackgroundTasks();
55
+ }
56
+ listMirrorTableBindings(userId) {
57
+ return this.mirrorTableBindingRepository.listByUserId(userId).map(mapBindingRecordToDto);
58
+ }
59
+ saveMirrorTableBinding(userId, input) {
60
+ const mirrorType = normalizeMirrorType(input.mirrorType);
61
+ const tableId = normalizeRequiredText(input.tableId, "tableId", "镜像表 tableId 不能为空");
62
+ const tableName = normalizeRequiredText(input.tableName, "tableName", "镜像表 tableName 不能为空");
63
+ const readOnlyMode = normalizeReadOnlyMode(input.readOnlyMode ?? "unknown");
64
+ const current = this.mirrorTableBindingRepository.findByUserIdAndMirrorType(userId, mirrorType);
65
+ const updatedAt = nowIso();
66
+ const record = {
67
+ bindingId: current?.bindingId ?? `teable-mirror-binding-${mirrorType}-${randomUUID()}`,
68
+ userId,
69
+ mirrorType,
70
+ tableId,
71
+ tableName,
72
+ readOnlyMode,
73
+ lastSyncedAt: normalizeOptionalText(input.lastSyncedAt),
74
+ createdAt: current?.createdAt ?? updatedAt,
75
+ updatedAt
76
+ };
77
+ return mapBindingRecordToDto(this.mirrorTableBindingRepository.upsert(record));
78
+ }
79
+ listMirrorRecordMappings(userId, mirrorType) {
80
+ return this.mirrorRecordMappingRepository.listByUserIdAndMirrorType(userId, normalizeMirrorType(mirrorType)).map(mapRecordMappingToDto);
81
+ }
82
+ saveMirrorRecordMapping(userId, input) {
83
+ const mirrorType = normalizeMirrorType(input.mirrorType);
84
+ const localId = normalizeRequiredText(input.localId, "localId", "localId 不能为空");
85
+ const teableRecordId = normalizeRequiredText(input.teableRecordId, "teableRecordId", "teableRecordId 不能为空");
86
+ const fingerprint = normalizeRequiredText(input.fingerprint, "fingerprint", "fingerprint 不能为空");
87
+ const current = this.mirrorRecordMappingRepository.findByUserIdAndMirrorTypeAndLocalId(userId, mirrorType, localId);
88
+ const updatedAt = nowIso();
89
+ const record = {
90
+ mappingId: current?.mappingId ?? `teable-mirror-record-${mirrorType}-${randomUUID()}`,
91
+ userId,
92
+ mirrorType,
93
+ localId,
94
+ teableRecordId,
95
+ fingerprint,
96
+ lastSyncedAt: normalizeRequiredText(input.lastSyncedAt ?? updatedAt, "lastSyncedAt", "lastSyncedAt 不能为空"),
97
+ deletedAt: normalizeOptionalText(input.deletedAt),
98
+ createdAt: current?.createdAt ?? updatedAt,
99
+ updatedAt
100
+ };
101
+ return mapRecordMappingToDto(this.mirrorRecordMappingRepository.upsert(record));
102
+ }
103
+ requestMirrorSync(userId, input) {
104
+ return this.enqueueMirrorSync(userId, {
105
+ mirrorTypes: input.mirrorTypes,
106
+ triggerType: "manual",
107
+ reason: "manual_request",
108
+ source: "teable.mirror_sync.request"
109
+ });
110
+ }
111
+ requestLocalChangeMirrorSync(userId, input) {
112
+ if (!this.teableGlobalBindingService || !this.teableWorkbenchSyncConfigService) {
113
+ return null;
114
+ }
115
+ const binding = this.teableGlobalBindingService.getGlobalBinding(userId);
116
+ if (!binding?.enabled || binding.mirrorMode !== "event_driven") {
117
+ return null;
118
+ }
119
+ const requestedMirrorTypes = normalizeRequestedMirrorTypes(input.mirrorTypes);
120
+ const enabledMirrorTypes = this.teableWorkbenchSyncConfigService.getConfigs(userId)
121
+ .filter((item) => item.enabled && requestedMirrorTypes.includes(item.sourceType))
122
+ .map((item) => item.sourceType);
123
+ if (enabledMirrorTypes.length === 0) {
124
+ return null;
125
+ }
126
+ return this.enqueueMirrorSync(userId, {
127
+ mirrorTypes: enabledMirrorTypes,
128
+ triggerType: "local_change",
129
+ reason: input.reason,
130
+ source: "teable.mirror_sync.local_change"
131
+ });
132
+ }
133
+ listSyncLogs(userId, input = {}) {
134
+ return this.syncLogRepository?.listByUserId(userId, input).map(mapSyncLogRecordToDto) ?? [];
135
+ }
136
+ enqueueMirrorSync(userId, input) {
137
+ this.ensureTaskManager();
138
+ const mirrorTypes = normalizeRequestedMirrorTypes(input.mirrorTypes);
139
+ const createdAt = nowIso();
140
+ const pendingLog = this.createSyncLog(userId, {
141
+ triggerType: input.triggerType,
142
+ sourceTypes: mirrorTypes,
143
+ state: "queued",
144
+ summary: input.triggerType === "local_change" ? "本地数据变化,已准备同步到 Teable" : "Teable 镜像同步任务已入队",
145
+ reason: input.reason ?? null,
146
+ createdAt
147
+ });
148
+ const handle = this.taskManager.enqueue(HOST_TASK_TYPES.teableMirrorSync, {
149
+ key: buildMirrorSyncTaskKey(userId),
150
+ source: input.source,
151
+ input: {
152
+ userId,
153
+ mirrorTypes,
154
+ triggerType: input.triggerType,
155
+ reason: input.reason ?? null,
156
+ logId: pendingLog?.logId ?? null
157
+ }
158
+ });
159
+ if (pendingLog) {
160
+ this.updateSyncLog(userId, pendingLog.logId, {
161
+ taskId: handle.taskId,
162
+ state: handle.deduped ? "running" : "queued",
163
+ summary: handle.deduped ? "已合并到正在运行的 Teable 同步任务" : pendingLog.summary,
164
+ startedAt: handle.deduped ? nowIso() : null
165
+ });
166
+ if (handle.deduped) {
167
+ void handle.promise
168
+ .then((result) => {
169
+ this.updateSyncLog(userId, pendingLog.logId, {
170
+ state: result.state,
171
+ summary: result.summary,
172
+ counts: result.counts,
173
+ errorDetail: result.failedMirrorTypes.length > 0
174
+ ? result.failedMirrorTypes.map((item) => `${resolveMirrorTypeLabel(item.mirrorType)}:${item.detail}`).join("\n")
175
+ : null,
176
+ finishedAt: nowIso()
177
+ });
178
+ })
179
+ .catch((error) => {
180
+ this.updateSyncLog(userId, pendingLog.logId, {
181
+ state: "failed",
182
+ summary: "Teable 镜像同步失败",
183
+ errorDetail: error instanceof Error ? error.message : String(error),
184
+ finishedAt: nowIso()
185
+ });
186
+ });
187
+ }
188
+ }
189
+ void handle.promise.catch(() => undefined);
190
+ return {
191
+ taskId: handle.taskId,
192
+ deduped: handle.deduped,
193
+ taskType: "mirror_sync",
194
+ state: "queued",
195
+ summary: handle.deduped ? "Teable 镜像同步任务已在队列中" : "Teable 镜像同步任务已入队",
196
+ updatedAt: nowIso()
197
+ };
198
+ }
199
+ getMirrorSyncTaskSnapshot(userId, _workspaceIdsOrWorkspaceId, _workspaceId) {
200
+ this.ensureTaskManager();
201
+ const snapshot = this.taskManager.peek(HOST_TASK_TYPES.teableMirrorSync, buildMirrorSyncTaskKey(userId));
202
+ return mapTaskSnapshotToDto(snapshot);
203
+ }
204
+ getOverview(userId, workspaceIdsOrWorkspaceId, workspaceId) {
205
+ if (!this.teableGlobalBindingService || !this.teableWorkbenchSyncConfigService) {
206
+ throw new AppError({
207
+ statusCode: 500,
208
+ errorCode: "TEABLE_OVERVIEW_DEPENDENCY_MISSING",
209
+ detail: "Teable 总览依赖没有完整注入"
210
+ });
211
+ }
212
+ return {
213
+ binding: this.teableGlobalBindingService.getOverview(userId),
214
+ syncConfigs: this.teableWorkbenchSyncConfigService.getConfigs(userId),
215
+ mirrorBindings: this.listMirrorTableBindings(userId),
216
+ latestMirrorSyncTask: this.getMirrorSyncTaskSnapshot(userId, workspaceIdsOrWorkspaceId, workspaceId)
217
+ };
218
+ }
219
+ async runMirrorSync(userId, input) {
220
+ this.ensureSyncDependencies();
221
+ const binding = this.teableGlobalBindingService.getGlobalBinding(userId);
222
+ if (!binding || !binding.enabled) {
223
+ throw new AppError({
224
+ statusCode: 400,
225
+ errorCode: "TEABLE_BINDING_REQUIRED",
226
+ detail: "当前事务工作台还没有可用的 Teable 绑定"
227
+ });
228
+ }
229
+ const token = this.teableCredentialService.loadToken(userId, binding.authRef);
230
+ if (!token) {
231
+ throw new AppError({
232
+ statusCode: 400,
233
+ errorCode: "TEABLE_AUTH_REQUIRED",
234
+ detail: "当前 Teable 认证引用没有可用 token"
235
+ });
236
+ }
237
+ const client = new TeableApiClient(binding.baseUrl, token);
238
+ const allConfigs = this.teableWorkbenchSyncConfigService.getConfigs(userId);
239
+ const requestedMirrorTypes = normalizeRequestedMirrorTypes(input.mirrorTypes);
240
+ const configs = allConfigs.filter((item) => item.enabled && requestedMirrorTypes.includes(item.sourceType));
241
+ if (configs.length === 0) {
242
+ return {
243
+ state: "succeeded",
244
+ summary: "当前没有启用的镜像配置需要同步",
245
+ syncedMirrorTypes: [],
246
+ failedMirrorTypes: [],
247
+ counts: buildEmptyCounts()
248
+ };
249
+ }
250
+ const result = {
251
+ state: "succeeded",
252
+ summary: "Teable 镜像同步完成",
253
+ syncedMirrorTypes: [],
254
+ failedMirrorTypes: [],
255
+ counts: buildEmptyCounts()
256
+ };
257
+ for (const config of configs) {
258
+ try {
259
+ const sourceRecords = await this.readMirrorSourceRecords(userId, config);
260
+ const tableBinding = await this.ensureMirrorTable(client, userId, binding.baseId, config);
261
+ const mapping = this.resolveFieldMapping(userId, config.configId, config.sourceType, config.targetTableId ?? tableBinding.tableId);
262
+ const counts = await this.syncMirrorRecords(client, userId, config.sourceType, tableBinding.tableId, sourceRecords, mapping);
263
+ result.counts[config.sourceType] = counts;
264
+ result.syncedMirrorTypes.push(config.sourceType);
265
+ }
266
+ catch (error) {
267
+ result.failedMirrorTypes.push({
268
+ mirrorType: config.sourceType,
269
+ detail: error instanceof Error ? error.message : String(error)
270
+ });
271
+ }
272
+ }
273
+ if (result.failedMirrorTypes.length > 0 && result.syncedMirrorTypes.length > 0) {
274
+ result.state = "partial_failed";
275
+ result.summary = "Teable 镜像同步部分成功,部分失败";
276
+ }
277
+ else if (result.failedMirrorTypes.length > 0) {
278
+ result.state = "failed";
279
+ result.summary = "Teable 镜像同步失败";
280
+ }
281
+ return result;
282
+ }
283
+ async ensureMirrorTable(client, userId, baseId, config) {
284
+ const mirrorType = config.sourceType;
285
+ const targetTableId = config.targetTableId?.trim() ?? "";
286
+ if (!targetTableId) {
287
+ throw new AppError({
288
+ statusCode: 400,
289
+ errorCode: "TEABLE_TARGET_TABLE_REQUIRED",
290
+ detail: `${resolveMirrorTypeLabel(mirrorType)} 镜像还没有指定目标表,请先到设置里绑定目标表`
291
+ });
292
+ }
293
+ const existingBinding = this.mirrorTableBindingRepository.findByUserIdAndMirrorType(userId, mirrorType);
294
+ if (existingBinding?.tableId === targetTableId) {
295
+ return mapBindingRecordToDto(existingBinding);
296
+ }
297
+ const table = await this.findExistingTable(client, baseId, targetTableId);
298
+ if (!table) {
299
+ throw new AppError({
300
+ statusCode: 400,
301
+ errorCode: "TEABLE_TARGET_TABLE_NOT_FOUND",
302
+ detail: `${resolveMirrorTypeLabel(mirrorType)} 镜像指定的目标表不存在,请先检查 Teable 连接和表 ID`
303
+ });
304
+ }
305
+ return this.saveMirrorTableBinding(userId, {
306
+ mirrorType,
307
+ tableId: table.id,
308
+ tableName: table.name,
309
+ readOnlyMode: existingBinding?.readOnlyMode ?? "unknown",
310
+ lastSyncedAt: existingBinding?.lastSyncedAt ?? null
311
+ });
312
+ }
313
+ async syncMirrorRecords(client, userId, mirrorType, tableId, sourceRecords, mapping) {
314
+ const currentMappings = this.mirrorRecordMappingRepository.listByUserIdAndMirrorType(userId, mirrorType);
315
+ const mappingByLocalId = new Map(currentMappings.map((item) => [item.localId, item]));
316
+ const sourceByLocalId = new Map(sourceRecords.map((item) => [item.localId, item]));
317
+ const createdPayload = [];
318
+ const createdLocalIds = [];
319
+ const updatedPayload = [];
320
+ const deletedRecordIds = [];
321
+ let skipped = 0;
322
+ for (const source of sourceRecords) {
323
+ const existing = mappingByLocalId.get(source.localId);
324
+ const fields = this.teableFieldMappingService.applyMapping(mapping, source.payload);
325
+ if (Object.keys(fields).length === 0) {
326
+ skipped += 1;
327
+ continue;
328
+ }
329
+ if (!existing) {
330
+ createdPayload.push({ fields });
331
+ createdLocalIds.push(source.localId);
332
+ continue;
333
+ }
334
+ if (existing.fingerprint === source.fingerprint && !existing.deletedAt) {
335
+ skipped += 1;
336
+ continue;
337
+ }
338
+ updatedPayload.push({ id: existing.teableRecordId, fields });
339
+ }
340
+ for (const existing of currentMappings) {
341
+ if (!sourceByLocalId.has(existing.localId) && !existing.deletedAt) {
342
+ deletedRecordIds.push(existing.teableRecordId);
343
+ }
344
+ }
345
+ let created = 0;
346
+ let updated = 0;
347
+ let deleted = 0;
348
+ const syncedAt = nowIso();
349
+ if (createdPayload.length > 0) {
350
+ const createdResponse = await client.createRecords(tableId, {
351
+ fieldKeyType: "id",
352
+ records: createdPayload
353
+ });
354
+ created = createdResponse.records.length;
355
+ createdResponse.records.forEach((record, index) => {
356
+ const localId = createdLocalIds[index];
357
+ const source = localId ? sourceByLocalId.get(localId) : null;
358
+ if (!localId || !source) {
359
+ return;
360
+ }
361
+ this.saveMirrorRecordMapping(userId, {
362
+ mirrorType,
363
+ localId,
364
+ teableRecordId: record.id,
365
+ fingerprint: source.fingerprint,
366
+ lastSyncedAt: syncedAt,
367
+ deletedAt: null
368
+ });
369
+ });
370
+ }
371
+ if (updatedPayload.length > 0) {
372
+ await client.updateRecords(tableId, {
373
+ fieldKeyType: "id",
374
+ records: updatedPayload
375
+ });
376
+ updated = updatedPayload.length;
377
+ for (const item of updatedPayload) {
378
+ const localEntry = currentMappings.find((entry) => entry.teableRecordId === item.id);
379
+ const source = localEntry ? sourceByLocalId.get(localEntry.localId) : null;
380
+ if (!localEntry || !source) {
381
+ continue;
382
+ }
383
+ this.saveMirrorRecordMapping(userId, {
384
+ mirrorType,
385
+ localId: localEntry.localId,
386
+ teableRecordId: localEntry.teableRecordId,
387
+ fingerprint: source.fingerprint,
388
+ lastSyncedAt: syncedAt,
389
+ deletedAt: null
390
+ });
391
+ }
392
+ }
393
+ if (deletedRecordIds.length > 0) {
394
+ await client.deleteRecords(tableId, deletedRecordIds);
395
+ deleted = deletedRecordIds.length;
396
+ for (const mappingRecord of currentMappings) {
397
+ if (!deletedRecordIds.includes(mappingRecord.teableRecordId)) {
398
+ continue;
399
+ }
400
+ this.saveMirrorRecordMapping(userId, {
401
+ mirrorType,
402
+ localId: mappingRecord.localId,
403
+ teableRecordId: mappingRecord.teableRecordId,
404
+ fingerprint: mappingRecord.fingerprint,
405
+ lastSyncedAt: syncedAt,
406
+ deletedAt: syncedAt
407
+ });
408
+ }
409
+ }
410
+ const currentBinding = this.mirrorTableBindingRepository.findByUserIdAndMirrorType(userId, mirrorType);
411
+ this.saveMirrorTableBinding(userId, {
412
+ mirrorType,
413
+ tableId,
414
+ tableName: currentBinding?.tableName ?? getDefaultMirrorTableName(mirrorType),
415
+ readOnlyMode: currentBinding?.readOnlyMode ?? "unknown",
416
+ lastSyncedAt: syncedAt
417
+ });
418
+ return { created, updated, deleted, skipped };
419
+ }
420
+ async readMirrorSourceRecords(userId, config) {
421
+ switch (config.sourceType) {
422
+ case "tags":
423
+ return this.readTagMirrorSourceRecords(userId, config);
424
+ case "sessions":
425
+ return this.readSessionMirrorSourceRecords(userId, config);
426
+ case "todos":
427
+ return this.readTodoMirrorSourceRecords(config);
428
+ default:
429
+ return [];
430
+ }
431
+ }
432
+ readTagMirrorSourceRecords(userId, config) {
433
+ const rootTagIds = Array.isArray(config.scope.rootTagIds)
434
+ ? config.scope.rootTagIds
435
+ : [];
436
+ if (rootTagIds.length === 0) {
437
+ return [];
438
+ }
439
+ const tagList = this.affairsTagService.listGlobalTags(userId, { includeDisabled: true });
440
+ const roots = tagList.items.filter((item) => rootTagIds.includes(item.id));
441
+ if (roots.length === 0) {
442
+ return [];
443
+ }
444
+ return tagList.items
445
+ .filter((item) => roots.some((root) => item.id === root.id || item.path === root.path || item.path.startsWith(`${root.path}/`)))
446
+ .map((item) => {
447
+ const payload = {
448
+ tag_id: item.id,
449
+ path: item.path,
450
+ name: item.name,
451
+ parent_id: item.parentId,
452
+ parent_path: item.parentPath,
453
+ root_type: item.rootType,
454
+ status: item.status,
455
+ description: item.description ?? "",
456
+ document_count: item.documentCount,
457
+ updated_at: item.updatedAt
458
+ };
459
+ return {
460
+ localId: item.id,
461
+ payload,
462
+ fingerprint: JSON.stringify(payload)
463
+ };
464
+ });
465
+ }
466
+ async readSessionMirrorSourceRecords(userId, config) {
467
+ const workspaces = resolveScopedWorkspaces(this.workspaceRepository, config.scope);
468
+ const groups = await Promise.all(workspaces.map(async (workspace) => ({
469
+ workspace,
470
+ sessions: await this.affairsLightweightSessionService.listSessions(workspace.id, userId)
471
+ })));
472
+ return groups.flatMap(({ workspace, sessions }) => sessions.map((item) => {
473
+ const payload = {
474
+ workspace_id: workspace.id,
475
+ workspace_name: workspace.name,
476
+ session_id: item.sessionId,
477
+ title: item.title,
478
+ provider: item.provider,
479
+ message_count: item.messageCount,
480
+ last_message_at: item.lastMessageAt ?? null,
481
+ running_state: item.runningState ?? null,
482
+ activity_state: item.activityState,
483
+ updated_at: item.updatedAt
484
+ };
485
+ return {
486
+ localId: buildScopedLocalId(workspace.id, item.sessionId),
487
+ payload,
488
+ fingerprint: JSON.stringify(payload)
489
+ };
490
+ }));
491
+ }
492
+ readTodoMirrorSourceRecords(config) {
493
+ const workspaces = resolveScopedWorkspaces(this.workspaceRepository, config.scope);
494
+ const workspaceById = new Map(workspaces.map((item) => [item.id, item]));
495
+ const selectedWorkspaceIds = new Set(workspaces.map((item) => item.id));
496
+ const projects = this.butlerProjectRepository.list().filter((item) => selectedWorkspaceIds.has(item.workspaceId));
497
+ const projectById = new Map(projects.map((item) => [item.id, item]));
498
+ const scope = config.scope;
499
+ const records = [];
500
+ if (scope.includeWorkspaceTodos !== false) {
501
+ const items = this.butlerInboxItemRepository.list({}).filter((item) => projectById.has(item.projectId));
502
+ for (const item of items) {
503
+ const project = projectById.get(item.projectId);
504
+ const workspace = workspaceById.get(project.workspaceId) ?? null;
505
+ const payload = {
506
+ workspace_id: workspace?.id ?? null,
507
+ workspace_name: workspace?.name ?? null,
508
+ todo_id: item.id,
509
+ title: item.title,
510
+ content: item.content,
511
+ source_type: "workspace",
512
+ item_type: item.itemType,
513
+ priority: item.priority,
514
+ status: item.status,
515
+ project_id: item.projectId,
516
+ updated_at: item.updatedAt
517
+ };
518
+ records.push({
519
+ localId: `workspace:${item.id}`,
520
+ payload,
521
+ fingerprint: JSON.stringify(payload)
522
+ });
523
+ }
524
+ }
525
+ if (scope.includeAffairsTodos !== false) {
526
+ const items = this.butlerFollowUpTaskRepository.list({}).filter((item) => projectById.has(item.projectId));
527
+ for (const item of items) {
528
+ const project = projectById.get(item.projectId);
529
+ const workspace = workspaceById.get(project.workspaceId) ?? null;
530
+ const payload = {
531
+ workspace_id: workspace?.id ?? null,
532
+ workspace_name: workspace?.name ?? null,
533
+ todo_id: item.id,
534
+ title: item.objective,
535
+ content: item.completionCriteria,
536
+ source_type: "affairs",
537
+ item_type: "follow_up",
538
+ priority: null,
539
+ status: item.status,
540
+ project_id: item.projectId,
541
+ updated_at: item.updatedAt
542
+ };
543
+ records.push({
544
+ localId: `affairs:${item.id}`,
545
+ payload,
546
+ fingerprint: JSON.stringify(payload)
547
+ });
548
+ }
549
+ }
550
+ return records;
551
+ }
552
+ resolveFieldMapping(userId, configId, sourceType, targetTableId) {
553
+ const mapping = this.teableFieldMappingService.resolveMapping(userId, configId);
554
+ if (!mapping) {
555
+ throw new AppError({
556
+ statusCode: 400,
557
+ errorCode: "TEABLE_FIELD_MAPPING_REQUIRED",
558
+ detail: `${resolveMirrorTypeLabel(sourceType)} 镜像还没有配置字段映射`
559
+ });
560
+ }
561
+ if (mapping.targetTableId !== targetTableId) {
562
+ throw new AppError({
563
+ statusCode: 400,
564
+ errorCode: "TEABLE_FIELD_MAPPING_TARGET_MISMATCH",
565
+ detail: `${resolveMirrorTypeLabel(sourceType)} 镜像的字段映射和目标表不一致,请重新保存映射`
566
+ });
567
+ }
568
+ return mapping;
569
+ }
570
+ ensureSyncDependencies() {
571
+ if (!this.teableGlobalBindingService || !this.teableCredentialService || !this.teableWorkbenchSyncConfigService || !this.affairsTagService || !this.affairsLightweightSessionService || !this.butlerInboxItemRepository || !this.butlerProjectRepository || !this.butlerFollowUpTaskRepository || !this.workspaceRepository || !this.teableFieldMappingService) {
572
+ throw new AppError({
573
+ statusCode: 500,
574
+ errorCode: "TEABLE_SYNC_DEPENDENCY_MISSING",
575
+ detail: "Teable 镜像同步依赖没有完整注入"
576
+ });
577
+ }
578
+ }
579
+ ensureTaskManager() {
580
+ if (!this.taskManager) {
581
+ throw new AppError({
582
+ statusCode: 500,
583
+ errorCode: "TEABLE_TASK_MANAGER_MISSING",
584
+ detail: "Teable 镜像同步任务管理器没有注入"
585
+ });
586
+ }
587
+ }
588
+ registerBackgroundTasks() {
589
+ if (!this.taskManager || this.taskManager.has(HOST_TASK_TYPES.teableMirrorSync)) {
590
+ return;
591
+ }
592
+ this.taskManager.register({
593
+ taskType: HOST_TASK_TYPES.teableMirrorSync,
594
+ executionLane: "host_background",
595
+ timeoutMs: 60_000,
596
+ concurrency: 1,
597
+ run: async (input, context) => {
598
+ const mirrorTypes = normalizeRequestedMirrorTypes(input.mirrorTypes);
599
+ if (input.logId) {
600
+ this.updateSyncLog(input.userId, input.logId, {
601
+ taskId: context.taskId,
602
+ state: "running",
603
+ summary: "Teable 镜像同步进行中",
604
+ startedAt: nowIso()
605
+ });
606
+ }
607
+ context.reportProgress({
608
+ phase: "queued",
609
+ label: "开始准备 Teable 镜像同步",
610
+ detail: `本次共 ${mirrorTypes.length} 类:${mirrorTypes.map(resolveMirrorTypeLabel).join("、")}`,
611
+ percent: 0
612
+ });
613
+ let result;
614
+ try {
615
+ result = await this.runMirrorSync(input.userId, { mirrorTypes });
616
+ }
617
+ catch (error) {
618
+ if (input.logId) {
619
+ this.updateSyncLog(input.userId, input.logId, {
620
+ state: "failed",
621
+ summary: "Teable 镜像同步失败",
622
+ errorDetail: error instanceof Error ? error.message : String(error),
623
+ finishedAt: nowIso()
624
+ });
625
+ }
626
+ throw error;
627
+ }
628
+ if (input.logId) {
629
+ this.updateSyncLog(input.userId, input.logId, {
630
+ state: result.state,
631
+ summary: result.summary,
632
+ counts: result.counts,
633
+ errorDetail: result.failedMirrorTypes.length > 0
634
+ ? result.failedMirrorTypes.map((item) => `${resolveMirrorTypeLabel(item.mirrorType)}:${item.detail}`).join("\n")
635
+ : null,
636
+ finishedAt: nowIso()
637
+ });
638
+ }
639
+ context.reportProgress({
640
+ phase: result.state,
641
+ label: result.summary,
642
+ detail: buildMirrorSyncProgressDetail(result),
643
+ current: mirrorTypes.length,
644
+ total: mirrorTypes.length,
645
+ percent: 100
646
+ });
647
+ return result;
648
+ }
649
+ });
650
+ }
651
+ async findExistingTable(client, baseId, targetTableId) {
652
+ const tables = await client.listTables(baseId);
653
+ return tables.find((item) => item.id === targetTableId) ?? null;
654
+ }
655
+ createSyncLog(userId, input) {
656
+ if (!this.syncLogRepository) {
657
+ return null;
658
+ }
659
+ const record = {
660
+ logId: `teable-sync-log-${randomUUID()}`,
661
+ userId,
662
+ triggerType: input.triggerType,
663
+ sourceTypesJson: JSON.stringify(input.sourceTypes),
664
+ taskId: null,
665
+ state: input.state,
666
+ summary: input.summary,
667
+ countsJson: "{}",
668
+ errorDetail: null,
669
+ reason: input.reason,
670
+ startedAt: null,
671
+ finishedAt: null,
672
+ createdAt: input.createdAt,
673
+ updatedAt: input.createdAt
674
+ };
675
+ return this.syncLogRepository.create(record);
676
+ }
677
+ updateSyncLog(userId, logId, patch) {
678
+ if (!this.syncLogRepository) {
679
+ return;
680
+ }
681
+ const current = this.syncLogRepository.findById(userId, logId);
682
+ if (!current) {
683
+ return;
684
+ }
685
+ this.syncLogRepository.update({
686
+ ...current,
687
+ taskId: patch.taskId !== undefined ? patch.taskId : current.taskId,
688
+ state: patch.state ?? current.state,
689
+ summary: patch.summary ?? current.summary,
690
+ countsJson: patch.counts ? JSON.stringify(patch.counts) : current.countsJson,
691
+ errorDetail: patch.errorDetail !== undefined ? patch.errorDetail : current.errorDetail,
692
+ reason: patch.reason !== undefined ? patch.reason : current.reason,
693
+ startedAt: patch.startedAt !== undefined ? patch.startedAt : current.startedAt,
694
+ finishedAt: patch.finishedAt !== undefined ? patch.finishedAt : current.finishedAt,
695
+ updatedAt: nowIso()
696
+ });
697
+ }
698
+ }
699
+ function buildEmptyCounts() {
700
+ return {
701
+ tags: { created: 0, updated: 0, deleted: 0, skipped: 0 },
702
+ sessions: { created: 0, updated: 0, deleted: 0, skipped: 0 },
703
+ todos: { created: 0, updated: 0, deleted: 0, skipped: 0 }
704
+ };
705
+ }
706
+ function resolveScopedWorkspaces(workspaceRepository, scope) {
707
+ const all = workspaceRepository.list();
708
+ const normalizedScope = scope && typeof scope === "object" && !Array.isArray(scope)
709
+ ? scope
710
+ : {};
711
+ const mode = normalizedScope.mode === "selected_workspaces" ? "selected_workspaces" : "all_workspaces";
712
+ if (mode !== "selected_workspaces") {
713
+ return all;
714
+ }
715
+ const ids = Array.isArray(normalizedScope.workspaceIds)
716
+ ? Array.from(new Set(normalizedScope.workspaceIds.map((item) => typeof item === "string" ? item.trim() : "").filter(Boolean)))
717
+ : [];
718
+ return ids.map((id) => workspaceRepository.findById(id)).filter((item) => Boolean(item));
719
+ }
720
+ function getDefaultMirrorTableName(mirrorType) {
721
+ switch (mirrorType) {
722
+ case "tags":
723
+ return "cn_tags";
724
+ case "sessions":
725
+ return "cn_sessions";
726
+ case "todos":
727
+ return "cn_todos";
728
+ default:
729
+ return `cn_${mirrorType}`;
730
+ }
731
+ }
732
+ function mapBindingRecordToDto(record) {
733
+ return {
734
+ mirrorType: record.mirrorType,
735
+ tableId: record.tableId,
736
+ tableName: record.tableName,
737
+ readOnlyMode: record.readOnlyMode,
738
+ lastSyncedAt: record.lastSyncedAt,
739
+ updatedAt: record.updatedAt
740
+ };
741
+ }
742
+ function mapRecordMappingToDto(record) {
743
+ return {
744
+ mirrorType: record.mirrorType,
745
+ localId: record.localId,
746
+ teableRecordId: record.teableRecordId,
747
+ fingerprint: record.fingerprint,
748
+ lastSyncedAt: record.lastSyncedAt,
749
+ deletedAt: record.deletedAt,
750
+ updatedAt: record.updatedAt
751
+ };
752
+ }
753
+ function mapSyncLogRecordToDto(record) {
754
+ return {
755
+ logId: record.logId,
756
+ triggerType: record.triggerType,
757
+ sourceTypes: parseMirrorTypesJson(record.sourceTypesJson),
758
+ taskId: record.taskId,
759
+ state: record.state,
760
+ summary: record.summary,
761
+ counts: parseCountsJson(record.countsJson),
762
+ errorDetail: record.errorDetail,
763
+ reason: record.reason,
764
+ startedAt: record.startedAt,
765
+ finishedAt: record.finishedAt,
766
+ createdAt: record.createdAt,
767
+ updatedAt: record.updatedAt
768
+ };
769
+ }
770
+ function parseMirrorTypesJson(raw) {
771
+ try {
772
+ const parsed = JSON.parse(raw);
773
+ return Array.isArray(parsed) ? normalizeRequestedMirrorTypes(parsed) : [];
774
+ }
775
+ catch {
776
+ return [];
777
+ }
778
+ }
779
+ function parseCountsJson(raw) {
780
+ try {
781
+ const parsed = JSON.parse(raw);
782
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed)
783
+ ? parsed
784
+ : {};
785
+ }
786
+ catch {
787
+ return {};
788
+ }
789
+ }
790
+ function normalizeMirrorType(value) {
791
+ if (SUPPORTED_MIRROR_TYPES.includes(value)) {
792
+ return value;
793
+ }
794
+ throw new AppError({
795
+ statusCode: 400,
796
+ errorCode: "INVALID_INPUT",
797
+ field: "mirrorType",
798
+ detail: "mirrorType 只允许 tags、sessions、todos"
799
+ });
800
+ }
801
+ function normalizeReadOnlyMode(value) {
802
+ if (SUPPORTED_READ_ONLY_MODES.includes(value)) {
803
+ return value;
804
+ }
805
+ throw new AppError({
806
+ statusCode: 400,
807
+ errorCode: "INVALID_INPUT",
808
+ field: "readOnlyMode",
809
+ detail: "readOnlyMode 只允许 role_based、matrix_based、unknown"
810
+ });
811
+ }
812
+ function normalizeRequiredText(value, field, detail) {
813
+ const normalized = value.trim();
814
+ if (!normalized) {
815
+ throw new AppError({ statusCode: 400, errorCode: "INVALID_INPUT", field, detail });
816
+ }
817
+ return normalized;
818
+ }
819
+ function normalizeOptionalText(value) {
820
+ const normalized = value?.trim() ?? "";
821
+ return normalized ? normalized : null;
822
+ }
823
+ function normalizeRequestedMirrorTypes(values) {
824
+ const target = values?.length ? values : SUPPORTED_MIRROR_TYPES;
825
+ return Array.from(new Set(target.map(normalizeMirrorType)));
826
+ }
827
+ function buildMirrorSyncTaskKey(userId) {
828
+ return `${userId}:global`;
829
+ }
830
+ function buildScopedLocalId(scopeId, localId) {
831
+ return `${scopeId}:${localId}`;
832
+ }
833
+ function mapTaskSnapshotToDto(snapshot) {
834
+ if (!snapshot) {
835
+ return null;
836
+ }
837
+ return {
838
+ taskId: snapshot.taskId,
839
+ taskType: "mirror_sync",
840
+ state: resolveMirrorSyncTaskState(snapshot),
841
+ summary: resolveMirrorSyncTaskSummary(snapshot),
842
+ lastError: snapshot.status === "failed" || snapshot.status === "queue_timeout" || snapshot.status === "timeout"
843
+ ? snapshot.errorMessage ?? snapshot.errorDetail ?? "Teable 镜像同步失败"
844
+ : null,
845
+ updatedAt: new Date(snapshot.finishedAt ?? snapshot.progress?.updatedAt ?? snapshot.startedAt ?? snapshot.enqueuedAt).toISOString(),
846
+ startedAt: snapshot.startedAt === null ? null : new Date(snapshot.startedAt).toISOString(),
847
+ finishedAt: snapshot.finishedAt === null ? null : new Date(snapshot.finishedAt).toISOString(),
848
+ progress: snapshot.progress ? {
849
+ phase: snapshot.progress.phase,
850
+ label: snapshot.progress.label ?? null,
851
+ detail: snapshot.progress.detail ?? null,
852
+ current: snapshot.progress.current ?? null,
853
+ total: snapshot.progress.total ?? null,
854
+ percent: snapshot.progress.percent ?? null,
855
+ updatedAt: new Date(snapshot.progress.updatedAt).toISOString()
856
+ } : null,
857
+ result: snapshot.result ?? null
858
+ };
859
+ }
860
+ function resolveMirrorSyncTaskState(snapshot) {
861
+ if (snapshot.status === "succeeded") {
862
+ return snapshot.result?.state ?? "succeeded";
863
+ }
864
+ if (snapshot.status === "failed" || snapshot.status === "timeout" || snapshot.status === "queue_timeout" || snapshot.status === "cancelled") {
865
+ return "failed";
866
+ }
867
+ if (snapshot.status === "running") {
868
+ return "running";
869
+ }
870
+ return "queued";
871
+ }
872
+ function resolveMirrorSyncTaskSummary(snapshot) {
873
+ if (snapshot.result?.summary?.trim()) {
874
+ return snapshot.result.summary.trim();
875
+ }
876
+ if (snapshot.progress?.label?.trim()) {
877
+ return snapshot.progress.label.trim();
878
+ }
879
+ if (snapshot.status === "queued") {
880
+ return "Teable 镜像同步任务排队中";
881
+ }
882
+ if (snapshot.status === "running") {
883
+ return "Teable 镜像同步进行中";
884
+ }
885
+ if (snapshot.status === "failed" || snapshot.status === "timeout" || snapshot.status === "queue_timeout" || snapshot.status === "cancelled") {
886
+ return snapshot.errorMessage ?? "Teable 镜像同步失败";
887
+ }
888
+ return null;
889
+ }
890
+ function resolveMirrorTypeLabel(mirrorType) {
891
+ switch (mirrorType) {
892
+ case "tags":
893
+ return "标签";
894
+ case "sessions":
895
+ return "会话";
896
+ case "todos":
897
+ return "代办";
898
+ default:
899
+ return mirrorType;
900
+ }
901
+ }
902
+ function buildMirrorSyncProgressDetail(result) {
903
+ const succeeded = result.syncedMirrorTypes.map(resolveMirrorTypeLabel);
904
+ const failed = result.failedMirrorTypes.map((item) => resolveMirrorTypeLabel(item.mirrorType));
905
+ const segments = [];
906
+ if (succeeded.length > 0) {
907
+ segments.push(`已完成:${succeeded.join("、")}`);
908
+ }
909
+ if (failed.length > 0) {
910
+ segments.push(`失败:${failed.join("、")}`);
911
+ }
912
+ return segments.join(";");
913
+ }
914
+ function isTaskManagerLike(value) {
915
+ return typeof value === "object" && value !== null && typeof value.has === "function" && typeof value.enqueue === "function";
916
+ }
917
+ //# sourceMappingURL=teable-mirror-sync-service.js.map