@things-factory/ai-assistant 10.1.278 → 10.1.280

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 (531) hide show
  1. package/dist-client/bootstrap.d.ts +4 -4
  2. package/dist-client/bootstrap.js +4 -8
  3. package/dist-client/components/ai-model-price-table.d.ts +32 -0
  4. package/dist-client/components/ai-model-price-table.js +311 -0
  5. package/dist-client/components/ai-usage-table.d.ts +17 -0
  6. package/dist-client/components/ai-usage-table.js +151 -0
  7. package/dist-client/index.d.ts +1 -0
  8. package/dist-client/index.js +2 -0
  9. package/dist-client/pages/ai-model-price-page.d.ts +37 -0
  10. package/dist-client/pages/ai-model-price-page.js +312 -0
  11. package/dist-client/route.d.ts +1 -1
  12. package/dist-client/route.js +4 -0
  13. package/dist-client/settings/ai-agent-setting-let.js +11 -0
  14. package/dist-client/utils/model-price-model.d.ts +31 -0
  15. package/dist-client/utils/model-price-model.js +30 -0
  16. package/dist-client/utils/usage-charts.d.ts +21 -0
  17. package/dist-client/utils/usage-charts.js +45 -0
  18. package/dist-client/utils/usage-model.d.ts +44 -0
  19. package/dist-client/utils/usage-model.js +44 -0
  20. package/dist-server/service/ai-agent-resolver.d.ts +46 -0
  21. package/dist-server/service/ai-agent-resolver.js +252 -0
  22. package/package.json +25 -10
  23. package/things-factory.config.js +4 -1
  24. package/translations/en.json +46 -4
  25. package/translations/es.json +43 -1
  26. package/translations/ja.json +43 -1
  27. package/translations/ko.json +46 -4
  28. package/translations/ms.json +43 -1
  29. package/translations/zh.json +43 -1
  30. package/client/bootstrap.ts +0 -27
  31. package/client/components/assistant-chat.test.ts +0 -149
  32. package/client/components/assistant-chat.ts +0 -5599
  33. package/client/components/assistant-dock.ts +0 -1036
  34. package/client/components/assistant-invite.ts +0 -138
  35. package/client/components/assistant-mode.test.ts +0 -119
  36. package/client/components/assistant-mode.ts +0 -174
  37. package/client/components/assistant-selection-bar.test.ts +0 -167
  38. package/client/components/assistant-selection-bar.ts +0 -178
  39. package/client/components/assistant-session-manager.ts +0 -915
  40. package/client/components/assistant-session-toolbar.ts +0 -80
  41. package/client/components/chat-defaults.ts +0 -64
  42. package/client/components/chat-echo-dedup.test.ts +0 -253
  43. package/client/components/chat-echo-dedup.ts +0 -190
  44. package/client/components/chat-input-builder.test.ts +0 -86
  45. package/client/components/chat-input-builder.ts +0 -133
  46. package/client/components/chat-proposal-survival.test.ts +0 -73
  47. package/client/components/entry-review.ts +0 -346
  48. package/client/components/markdown.test.ts +0 -518
  49. package/client/components/markdown.ts +0 -508
  50. package/client/components/mention-popup-helpers.ts +0 -433
  51. package/client/components/mention-popup.test.ts +0 -588
  52. package/client/components/mention-popup.ts +0 -261
  53. package/client/components/panel-aria.test.ts +0 -59
  54. package/client/components/proposal-outcome-line.ts +0 -74
  55. package/client/components/proposal-request.test.ts +0 -73
  56. package/client/components/reply-message.test.ts +0 -15
  57. package/client/components/reply-message.ts +0 -11
  58. package/client/components/roster-identity.test.ts +0 -8
  59. package/client/components/roster-identity.ts +0 -4
  60. package/client/components/workspace-view.ts +0 -486
  61. package/client/index.ts +0 -36
  62. package/client/pages/assistant-workspace-page.ts +0 -317
  63. package/client/route.ts +0 -9
  64. package/client/settings/agent-settings-model.ts +0 -164
  65. package/client/settings/ai-agent-setting-let.ts +0 -667
  66. package/client/settings/ai-usage-setting-let.ts +0 -240
  67. package/client/settings/usage-model.ts +0 -46
  68. package/client/setup-assistant-dock.ts +0 -177
  69. package/client/tsconfig.json +0 -13
  70. package/client/utils/addressed-draft.test.ts +0 -31
  71. package/client/utils/agent-choice.ts +0 -56
  72. package/client/utils/applied-screens.ts +0 -36
  73. package/client/utils/assistant-activity.ts +0 -122
  74. package/client/utils/assistant-ask.test.ts +0 -38
  75. package/client/utils/assistant-ask.ts +0 -93
  76. package/client/utils/assistant-capability-read.ts +0 -24
  77. package/client/utils/assistant-capability.test.ts +0 -17
  78. package/client/utils/assistant-capability.ts +0 -19
  79. package/client/utils/assistant-navigation.test.ts +0 -66
  80. package/client/utils/assistant-navigation.ts +0 -11
  81. package/client/utils/assistant-proposals.ts +0 -85
  82. package/client/utils/assistant-request-context.ts +0 -63
  83. package/client/utils/assistant-selection.test.ts +0 -54
  84. package/client/utils/assistant-selection.ts +0 -75
  85. package/client/utils/assistant-session-actions.ts +0 -32
  86. package/client/utils/assistant-session-admin.ts +0 -239
  87. package/client/utils/assistant-session-controller.test.ts +0 -75
  88. package/client/utils/assistant-session-controller.ts +0 -208
  89. package/client/utils/assistant-session-transport.ts +0 -46
  90. package/client/utils/assistant-surface.ts +0 -178
  91. package/client/utils/assistant-typing.test.ts +0 -33
  92. package/client/utils/assistant-typing.ts +0 -53
  93. package/client/utils/attachment-policy.ts +0 -106
  94. package/client/utils/composer-frame.ts +0 -77
  95. package/client/utils/document-text.ts +0 -332
  96. package/client/utils/entry-hash.ts +0 -35
  97. package/client/utils/entry-proposal.ts +0 -215
  98. package/client/utils/entry-workspace.ts +0 -70
  99. package/client/utils/invite-proposal-register.ts +0 -31
  100. package/client/utils/invite-proposal.test.ts +0 -40
  101. package/client/utils/invite-proposal.ts +0 -54
  102. package/client/utils/menu-room.ts +0 -33
  103. package/client/utils/open-page.ts +0 -21
  104. package/client/utils/pasted-table.ts +0 -145
  105. package/client/utils/pdf-pages.ts +0 -25
  106. package/client/utils/spreadsheet-text.ts +0 -125
  107. package/client/utils/workspace.ts +0 -89
  108. package/dist-client/bootstrap.js.map +0 -1
  109. package/dist-client/components/assistant-chat.js.map +0 -1
  110. package/dist-client/components/assistant-chat.test.d.ts +0 -1
  111. package/dist-client/components/assistant-chat.test.js +0 -138
  112. package/dist-client/components/assistant-chat.test.js.map +0 -1
  113. package/dist-client/components/assistant-dock.js.map +0 -1
  114. package/dist-client/components/assistant-invite.js.map +0 -1
  115. package/dist-client/components/assistant-mode.js.map +0 -1
  116. package/dist-client/components/assistant-mode.test.d.ts +0 -1
  117. package/dist-client/components/assistant-mode.test.js +0 -100
  118. package/dist-client/components/assistant-mode.test.js.map +0 -1
  119. package/dist-client/components/assistant-selection-bar.js.map +0 -1
  120. package/dist-client/components/assistant-selection-bar.test.d.ts +0 -1
  121. package/dist-client/components/assistant-selection-bar.test.js +0 -148
  122. package/dist-client/components/assistant-selection-bar.test.js.map +0 -1
  123. package/dist-client/components/assistant-session-manager.js.map +0 -1
  124. package/dist-client/components/assistant-session-toolbar.js.map +0 -1
  125. package/dist-client/components/chat-defaults.js.map +0 -1
  126. package/dist-client/components/chat-echo-dedup.js.map +0 -1
  127. package/dist-client/components/chat-echo-dedup.test.d.ts +0 -1
  128. package/dist-client/components/chat-echo-dedup.test.js +0 -223
  129. package/dist-client/components/chat-echo-dedup.test.js.map +0 -1
  130. package/dist-client/components/chat-input-builder.js.map +0 -1
  131. package/dist-client/components/chat-input-builder.test.d.ts +0 -1
  132. package/dist-client/components/chat-input-builder.test.js +0 -70
  133. package/dist-client/components/chat-input-builder.test.js.map +0 -1
  134. package/dist-client/components/chat-proposal-survival.test.d.ts +0 -1
  135. package/dist-client/components/chat-proposal-survival.test.js +0 -61
  136. package/dist-client/components/chat-proposal-survival.test.js.map +0 -1
  137. package/dist-client/components/entry-review.js.map +0 -1
  138. package/dist-client/components/markdown.js.map +0 -1
  139. package/dist-client/components/markdown.test.d.ts +0 -1
  140. package/dist-client/components/markdown.test.js +0 -418
  141. package/dist-client/components/markdown.test.js.map +0 -1
  142. package/dist-client/components/mention-popup-helpers.js.map +0 -1
  143. package/dist-client/components/mention-popup.js.map +0 -1
  144. package/dist-client/components/mention-popup.test.d.ts +0 -1
  145. package/dist-client/components/mention-popup.test.js +0 -474
  146. package/dist-client/components/mention-popup.test.js.map +0 -1
  147. package/dist-client/components/panel-aria.test.d.ts +0 -1
  148. package/dist-client/components/panel-aria.test.js +0 -53
  149. package/dist-client/components/panel-aria.test.js.map +0 -1
  150. package/dist-client/components/proposal-outcome-line.js.map +0 -1
  151. package/dist-client/components/proposal-request.test.d.ts +0 -1
  152. package/dist-client/components/proposal-request.test.js +0 -63
  153. package/dist-client/components/proposal-request.test.js.map +0 -1
  154. package/dist-client/components/reply-message.js.map +0 -1
  155. package/dist-client/components/reply-message.test.d.ts +0 -1
  156. package/dist-client/components/reply-message.test.js +0 -16
  157. package/dist-client/components/reply-message.test.js.map +0 -1
  158. package/dist-client/components/roster-identity.js.map +0 -1
  159. package/dist-client/components/roster-identity.test.d.ts +0 -1
  160. package/dist-client/components/roster-identity.test.js +0 -9
  161. package/dist-client/components/roster-identity.test.js.map +0 -1
  162. package/dist-client/components/workspace-view.js.map +0 -1
  163. package/dist-client/index.js.map +0 -1
  164. package/dist-client/pages/assistant-workspace-page.js.map +0 -1
  165. package/dist-client/route.js.map +0 -1
  166. package/dist-client/settings/agent-settings-model.js.map +0 -1
  167. package/dist-client/settings/ai-agent-setting-let.js.map +0 -1
  168. package/dist-client/settings/ai-usage-setting-let.d.ts +0 -25
  169. package/dist-client/settings/ai-usage-setting-let.js +0 -258
  170. package/dist-client/settings/ai-usage-setting-let.js.map +0 -1
  171. package/dist-client/settings/usage-model.d.ts +0 -32
  172. package/dist-client/settings/usage-model.js +0 -25
  173. package/dist-client/settings/usage-model.js.map +0 -1
  174. package/dist-client/setup-assistant-dock.js.map +0 -1
  175. package/dist-client/tsconfig.tsbuildinfo +0 -1
  176. package/dist-client/utils/addressed-draft.test.d.ts +0 -1
  177. package/dist-client/utils/addressed-draft.test.js +0 -27
  178. package/dist-client/utils/addressed-draft.test.js.map +0 -1
  179. package/dist-client/utils/agent-choice.js.map +0 -1
  180. package/dist-client/utils/applied-screens.js.map +0 -1
  181. package/dist-client/utils/assistant-activity.js.map +0 -1
  182. package/dist-client/utils/assistant-ask.js.map +0 -1
  183. package/dist-client/utils/assistant-ask.test.d.ts +0 -1
  184. package/dist-client/utils/assistant-ask.test.js +0 -40
  185. package/dist-client/utils/assistant-ask.test.js.map +0 -1
  186. package/dist-client/utils/assistant-capability-read.js.map +0 -1
  187. package/dist-client/utils/assistant-capability.js.map +0 -1
  188. package/dist-client/utils/assistant-capability.test.d.ts +0 -1
  189. package/dist-client/utils/assistant-capability.test.js +0 -16
  190. package/dist-client/utils/assistant-capability.test.js.map +0 -1
  191. package/dist-client/utils/assistant-navigation.js.map +0 -1
  192. package/dist-client/utils/assistant-navigation.test.d.ts +0 -1
  193. package/dist-client/utils/assistant-navigation.test.js +0 -63
  194. package/dist-client/utils/assistant-navigation.test.js.map +0 -1
  195. package/dist-client/utils/assistant-proposals.js.map +0 -1
  196. package/dist-client/utils/assistant-request-context.js.map +0 -1
  197. package/dist-client/utils/assistant-selection.js.map +0 -1
  198. package/dist-client/utils/assistant-selection.test.d.ts +0 -1
  199. package/dist-client/utils/assistant-selection.test.js +0 -46
  200. package/dist-client/utils/assistant-selection.test.js.map +0 -1
  201. package/dist-client/utils/assistant-session-actions.js.map +0 -1
  202. package/dist-client/utils/assistant-session-admin.js.map +0 -1
  203. package/dist-client/utils/assistant-session-controller.js.map +0 -1
  204. package/dist-client/utils/assistant-session-controller.test.d.ts +0 -1
  205. package/dist-client/utils/assistant-session-controller.test.js +0 -56
  206. package/dist-client/utils/assistant-session-controller.test.js.map +0 -1
  207. package/dist-client/utils/assistant-session-transport.js.map +0 -1
  208. package/dist-client/utils/assistant-surface.js.map +0 -1
  209. package/dist-client/utils/assistant-typing.js.map +0 -1
  210. package/dist-client/utils/assistant-typing.test.d.ts +0 -1
  211. package/dist-client/utils/assistant-typing.test.js +0 -30
  212. package/dist-client/utils/assistant-typing.test.js.map +0 -1
  213. package/dist-client/utils/attachment-policy.js.map +0 -1
  214. package/dist-client/utils/composer-frame.js.map +0 -1
  215. package/dist-client/utils/document-text.js.map +0 -1
  216. package/dist-client/utils/entry-hash.js.map +0 -1
  217. package/dist-client/utils/entry-proposal.js.map +0 -1
  218. package/dist-client/utils/entry-workspace.js.map +0 -1
  219. package/dist-client/utils/invite-proposal-register.js.map +0 -1
  220. package/dist-client/utils/invite-proposal.js.map +0 -1
  221. package/dist-client/utils/invite-proposal.test.d.ts +0 -1
  222. package/dist-client/utils/invite-proposal.test.js +0 -35
  223. package/dist-client/utils/invite-proposal.test.js.map +0 -1
  224. package/dist-client/utils/menu-room.js.map +0 -1
  225. package/dist-client/utils/open-page.js.map +0 -1
  226. package/dist-client/utils/pasted-table.js.map +0 -1
  227. package/dist-client/utils/pdf-pages.js.map +0 -1
  228. package/dist-client/utils/spreadsheet-text.js.map +0 -1
  229. package/dist-client/utils/workspace.js.map +0 -1
  230. package/dist-server/door-declaration-report.js.map +0 -1
  231. package/dist-server/index.js.map +0 -1
  232. package/dist-server/role-tasks.js.map +0 -1
  233. package/dist-server/service/ai-agent-resolver.js.map +0 -1
  234. package/dist-server/service/assistant-capability-resolver.js.map +0 -1
  235. package/dist-server/service/assistant-chat-resolver.js.map +0 -1
  236. package/dist-server/service/assistant-mode/assistant-mode-resolver.js.map +0 -1
  237. package/dist-server/service/assistant-mode/fallback-subjects.js.map +0 -1
  238. package/dist-server/service/assistant-mode/index.js.map +0 -1
  239. package/dist-server/service/assistant-mode/mode-categories.js.map +0 -1
  240. package/dist-server/service/assistant-mode/record-subject.js.map +0 -1
  241. package/dist-server/service/assistant-mode/registry.js.map +0 -1
  242. package/dist-server/service/assistant-mode/resolve.js.map +0 -1
  243. package/dist-server/service/assistant-mode/screen-rules.js.map +0 -1
  244. package/dist-server/service/assistant-mode/turn-lines.js.map +0 -1
  245. package/dist-server/service/assistant-mode/types.js.map +0 -1
  246. package/dist-server/service/chat-message/carried-turns.js.map +0 -1
  247. package/dist-server/service/chat-message/chat-message-broadcast.js.map +0 -1
  248. package/dist-server/service/chat-message/chat-message-publish.js.map +0 -1
  249. package/dist-server/service/chat-message/chat-message-room.js.map +0 -1
  250. package/dist-server/service/chat-message/chat-message-subscription.js.map +0 -1
  251. package/dist-server/service/chat-message/chat-message.js.map +0 -1
  252. package/dist-server/service/chat-message/chat-typing-subscription.js.map +0 -1
  253. package/dist-server/service/chat-message/chat-typing.js.map +0 -1
  254. package/dist-server/service/chat-message/door-access.js.map +0 -1
  255. package/dist-server/service/chat-message/fold-history.js.map +0 -1
  256. package/dist-server/service/chat-message/fold-session-history.js.map +0 -1
  257. package/dist-server/service/chat-message/history-summary.js.map +0 -1
  258. package/dist-server/service/chat-message/index.js.map +0 -1
  259. package/dist-server/service/chat-message/llm-history.js.map +0 -1
  260. package/dist-server/service/chat-message/message-doors.js.map +0 -1
  261. package/dist-server/service/chat-message/proposal-outcome.js.map +0 -1
  262. package/dist-server/service/chat-message/reader-gate.js.map +0 -1
  263. package/dist-server/service/chat-message/room-message.js.map +0 -1
  264. package/dist-server/service/chat-message/room-turn.js.map +0 -1
  265. package/dist-server/service/chat-message/used-tools.js.map +0 -1
  266. package/dist-server/service/chat-session/attachment-policy.js.map +0 -1
  267. package/dist-server/service/chat-session/attachment-route.js.map +0 -1
  268. package/dist-server/service/chat-session/attachment-tool-result.js.map +0 -1
  269. package/dist-server/service/chat-session/attachment-tools.js.map +0 -1
  270. package/dist-server/service/chat-session/chat-session.js.map +0 -1
  271. package/dist-server/service/chat-session/index.js.map +0 -1
  272. package/dist-server/service/chat-session/session-activity-broadcast.js.map +0 -1
  273. package/dist-server/service/chat-session/session-activity-publish.js.map +0 -1
  274. package/dist-server/service/chat-session/session-activity-subscription.js.map +0 -1
  275. package/dist-server/service/chat-session/session-anchor.js.map +0 -1
  276. package/dist-server/service/chat-session/session-attachments.js.map +0 -1
  277. package/dist-server/service/chat-session/session-cleanup.js.map +0 -1
  278. package/dist-server/service/chat-session/session-inbox.js.map +0 -1
  279. package/dist-server/service/chat-session/session-kind.js.map +0 -1
  280. package/dist-server/service/chat-session/session-name.js.map +0 -1
  281. package/dist-server/service/chat-session/session-purge.js.map +0 -1
  282. package/dist-server/service/chat-session/session-retention.js.map +0 -1
  283. package/dist-server/service/chat-session/session-visibility.js.map +0 -1
  284. package/dist-server/service/chat-session/session-write-gate.js.map +0 -1
  285. package/dist-server/service/chat-session/station-session-guard.js.map +0 -1
  286. package/dist-server/service/chat-session/turn-attachments.js.map +0 -1
  287. package/dist-server/service/chat-session-participant/chat-session-participant.js.map +0 -1
  288. package/dist-server/service/chat-session-participant/index.js.map +0 -1
  289. package/dist-server/service/chat-session-participant/invite-notification.js.map +0 -1
  290. package/dist-server/service/chat-session-participant/mention-invite.js.map +0 -1
  291. package/dist-server/service/chat-session-participant/participant-lookup-roles.js.map +0 -1
  292. package/dist-server/service/chat-session-participant/participant-lookup.js.map +0 -1
  293. package/dist-server/service/chat-session-participant/presence.js.map +0 -1
  294. package/dist-server/service/chat-session-participant/read-roster.js.map +0 -1
  295. package/dist-server/service/chat-session-resolver.js.map +0 -1
  296. package/dist-server/service/data-entry/entry-apply.js.map +0 -1
  297. package/dist-server/service/data-entry/entry-hash.js.map +0 -1
  298. package/dist-server/service/data-entry/entry-refusal.js.map +0 -1
  299. package/dist-server/service/data-entry/entry-resolver.js.map +0 -1
  300. package/dist-server/service/data-entry/entry-screen.js.map +0 -1
  301. package/dist-server/service/data-entry/entry-sources.js.map +0 -1
  302. package/dist-server/service/data-entry/entry-target.js.map +0 -1
  303. package/dist-server/service/data-entry/entry-tools.js.map +0 -1
  304. package/dist-server/service/data-entry/entry-update.js.map +0 -1
  305. package/dist-server/service/data-read/list-read.js.map +0 -1
  306. package/dist-server/service/data-read/read-target.js.map +0 -1
  307. package/dist-server/service/data-read/read-tools.js.map +0 -1
  308. package/dist-server/service/grounding-sources.js.map +0 -1
  309. package/dist-server/service/help-documents/help-keys.js.map +0 -1
  310. package/dist-server/service/help-documents/help-tools.js.map +0 -1
  311. package/dist-server/service/index.js.map +0 -1
  312. package/dist-server/service/people/index.js.map +0 -1
  313. package/dist-server/service/people/invite-tools.js.map +0 -1
  314. package/dist-server/service/people/people-keys.js.map +0 -1
  315. package/dist-server/service/people/people-read.js.map +0 -1
  316. package/dist-server/service/people/person-subject.js.map +0 -1
  317. package/dist-server/service/request-translator.js.map +0 -1
  318. package/dist-server/service/testing/conversation-script.js.map +0 -1
  319. package/dist-server/service/testing/mode-conformance.js.map +0 -1
  320. package/dist-server/service/tool-access.js.map +0 -1
  321. package/dist-server/service/types.js.map +0 -1
  322. package/dist-server/service/unattended-run/assistant-run.js.map +0 -1
  323. package/dist-server/service/unattended-run/index.js.map +0 -1
  324. package/dist-server/service/unattended-run/limits.js.map +0 -1
  325. package/dist-server/service/unattended-run/run-as.js.map +0 -1
  326. package/dist-server/service/unattended-run/run-budget.js.map +0 -1
  327. package/dist-server/service/unattended-run/run-ledger.js.map +0 -1
  328. package/dist-server/service/unattended-run/run-outcome.js.map +0 -1
  329. package/dist-server/service/unattended-run/run-store.js.map +0 -1
  330. package/dist-server/service/unattended-run/run-unattended.js.map +0 -1
  331. package/dist-server/service/unattended-run/unattended-prompt.js.map +0 -1
  332. package/dist-server/service/workspace/workspace-blocks.js.map +0 -1
  333. package/dist-server/service/workspace/workspace-tools.js.map +0 -1
  334. package/dist-server/tool-registry-boot-report.js.map +0 -1
  335. package/dist-server/tsconfig.tsbuildinfo +0 -1
  336. package/docs/session-controller.md +0 -52
  337. package/server/door-declaration-report.test.ts +0 -53
  338. package/server/door-declaration-report.ts +0 -61
  339. package/server/explicit-graphql-types.test.ts +0 -56
  340. package/server/index.ts +0 -36
  341. package/server/role-tasks.ts +0 -14
  342. package/server/service/ai-agent-resolver.ts +0 -330
  343. package/server/service/assistant-capability-resolver.ts +0 -66
  344. package/server/service/assistant-chat-resolver.ts +0 -640
  345. package/server/service/assistant-mode/assistant-mode-resolver.ts +0 -101
  346. package/server/service/assistant-mode/conversation-category.test.ts +0 -48
  347. package/server/service/assistant-mode/fallback-subjects.ts +0 -36
  348. package/server/service/assistant-mode/index.ts +0 -22
  349. package/server/service/assistant-mode/mode-categories.ts +0 -178
  350. package/server/service/assistant-mode/mode-front.test.ts +0 -183
  351. package/server/service/assistant-mode/one-conversation-per-subject.test.ts +0 -37
  352. package/server/service/assistant-mode/open-subject-door.test.ts +0 -52
  353. package/server/service/assistant-mode/record-subject.test.ts +0 -41
  354. package/server/service/assistant-mode/record-subject.ts +0 -44
  355. package/server/service/assistant-mode/registry.test.ts +0 -171
  356. package/server/service/assistant-mode/registry.ts +0 -139
  357. package/server/service/assistant-mode/resolve.test.ts +0 -129
  358. package/server/service/assistant-mode/resolve.ts +0 -113
  359. package/server/service/assistant-mode/screen-rules.test.ts +0 -99
  360. package/server/service/assistant-mode/screen-rules.ts +0 -103
  361. package/server/service/assistant-mode/turn-lines.ts +0 -43
  362. package/server/service/assistant-mode/types.ts +0 -138
  363. package/server/service/chat-message/carried-turns.test.ts +0 -80
  364. package/server/service/chat-message/carried-turns.ts +0 -112
  365. package/server/service/chat-message/chat-message-broadcast.integration.test.ts +0 -73
  366. package/server/service/chat-message/chat-message-broadcast.test.ts +0 -40
  367. package/server/service/chat-message/chat-message-broadcast.ts +0 -70
  368. package/server/service/chat-message/chat-message-publish.ts +0 -22
  369. package/server/service/chat-message/chat-message-room.test.ts +0 -145
  370. package/server/service/chat-message/chat-message-room.ts +0 -50
  371. package/server/service/chat-message/chat-message-subscription.ts +0 -77
  372. package/server/service/chat-message/chat-message.ts +0 -289
  373. package/server/service/chat-message/chat-typing-subscription.ts +0 -52
  374. package/server/service/chat-message/chat-typing.test.ts +0 -45
  375. package/server/service/chat-message/chat-typing.ts +0 -48
  376. package/server/service/chat-message/door-access.test.ts +0 -72
  377. package/server/service/chat-message/door-access.ts +0 -65
  378. package/server/service/chat-message/fold-history-window.test.ts +0 -184
  379. package/server/service/chat-message/fold-history.test.ts +0 -98
  380. package/server/service/chat-message/fold-history.ts +0 -77
  381. package/server/service/chat-message/fold-session-history.test.ts +0 -116
  382. package/server/service/chat-message/fold-session-history.ts +0 -115
  383. package/server/service/chat-message/history-summary.test.ts +0 -127
  384. package/server/service/chat-message/history-summary.ts +0 -100
  385. package/server/service/chat-message/index.ts +0 -13
  386. package/server/service/chat-message/llm-history.test.ts +0 -245
  387. package/server/service/chat-message/llm-history.ts +0 -182
  388. package/server/service/chat-message/message-doors.test.ts +0 -124
  389. package/server/service/chat-message/message-doors.ts +0 -132
  390. package/server/service/chat-message/proposal-outcome.test.ts +0 -111
  391. package/server/service/chat-message/proposal-outcome.ts +0 -99
  392. package/server/service/chat-message/reader-gate.ts +0 -44
  393. package/server/service/chat-message/room-message.test.ts +0 -21
  394. package/server/service/chat-message/room-message.ts +0 -22
  395. package/server/service/chat-message/room-turn.ts +0 -42
  396. package/server/service/chat-message/used-tools.test.ts +0 -19
  397. package/server/service/chat-message/used-tools.ts +0 -25
  398. package/server/service/chat-session/attachment-policy.test.ts +0 -42
  399. package/server/service/chat-session/attachment-policy.ts +0 -91
  400. package/server/service/chat-session/attachment-route.ts +0 -45
  401. package/server/service/chat-session/attachment-tool-result.test.ts +0 -63
  402. package/server/service/chat-session/attachment-tool-result.ts +0 -37
  403. package/server/service/chat-session/attachment-tools.ts +0 -66
  404. package/server/service/chat-session/chat-session.ts +0 -244
  405. package/server/service/chat-session/group-room.test.ts +0 -233
  406. package/server/service/chat-session/index.ts +0 -5
  407. package/server/service/chat-session/session-activity-broadcast.ts +0 -77
  408. package/server/service/chat-session/session-activity-publish.ts +0 -49
  409. package/server/service/chat-session/session-activity-receivers.test.ts +0 -26
  410. package/server/service/chat-session/session-activity-subscription.ts +0 -66
  411. package/server/service/chat-session/session-anchor.ts +0 -142
  412. package/server/service/chat-session/session-attachments.ts +0 -90
  413. package/server/service/chat-session/session-cleanup.test.ts +0 -83
  414. package/server/service/chat-session/session-cleanup.ts +0 -69
  415. package/server/service/chat-session/session-inbox.test.ts +0 -273
  416. package/server/service/chat-session/session-inbox.ts +0 -240
  417. package/server/service/chat-session/session-kind.test.ts +0 -24
  418. package/server/service/chat-session/session-kind.ts +0 -32
  419. package/server/service/chat-session/session-name.ts +0 -32
  420. package/server/service/chat-session/session-purge.test.ts +0 -24
  421. package/server/service/chat-session/session-purge.ts +0 -117
  422. package/server/service/chat-session/session-retention.ts +0 -17
  423. package/server/service/chat-session/session-visibility.test.ts +0 -130
  424. package/server/service/chat-session/session-visibility.ts +0 -68
  425. package/server/service/chat-session/session-write-gate.test.ts +0 -59
  426. package/server/service/chat-session/session-write-gate.ts +0 -31
  427. package/server/service/chat-session/station-session-guard.ts +0 -25
  428. package/server/service/chat-session/turn-attachments.test.ts +0 -106
  429. package/server/service/chat-session/turn-attachments.ts +0 -45
  430. package/server/service/chat-session-participant/chat-session-participant.ts +0 -135
  431. package/server/service/chat-session-participant/index.ts +0 -8
  432. package/server/service/chat-session-participant/invite-notification.test.ts +0 -80
  433. package/server/service/chat-session-participant/invite-notification.ts +0 -118
  434. package/server/service/chat-session-participant/mention-invite.test.ts +0 -173
  435. package/server/service/chat-session-participant/mention-invite.ts +0 -180
  436. package/server/service/chat-session-participant/participant-lookup-roles.ts +0 -6
  437. package/server/service/chat-session-participant/participant-lookup.ts +0 -22
  438. package/server/service/chat-session-participant/presence.test.ts +0 -39
  439. package/server/service/chat-session-participant/presence.ts +0 -21
  440. package/server/service/chat-session-participant/read-roster.test.ts +0 -27
  441. package/server/service/chat-session-participant/read-roster.ts +0 -17
  442. package/server/service/chat-session-resolver.ts +0 -1455
  443. package/server/service/data-entry/data-entry-update.test.ts +0 -378
  444. package/server/service/data-entry/data-entry.test.ts +0 -494
  445. package/server/service/data-entry/entry-apply.ts +0 -247
  446. package/server/service/data-entry/entry-hash.ts +0 -35
  447. package/server/service/data-entry/entry-refusal.test.ts +0 -73
  448. package/server/service/data-entry/entry-refusal.ts +0 -20
  449. package/server/service/data-entry/entry-resolver.ts +0 -74
  450. package/server/service/data-entry/entry-screen.ts +0 -14
  451. package/server/service/data-entry/entry-sources.ts +0 -39
  452. package/server/service/data-entry/entry-target.ts +0 -774
  453. package/server/service/data-entry/entry-tools.ts +0 -282
  454. package/server/service/data-entry/entry-update.ts +0 -238
  455. package/server/service/data-read/data-read.test.ts +0 -395
  456. package/server/service/data-read/detail-lists.test.ts +0 -61
  457. package/server/service/data-read/list-missing.test.ts +0 -49
  458. package/server/service/data-read/list-read.ts +0 -146
  459. package/server/service/data-read/list-status.test.ts +0 -46
  460. package/server/service/data-read/read-target.ts +0 -509
  461. package/server/service/data-read/read-tools.ts +0 -334
  462. package/server/service/data-read/read-words.test.ts +0 -99
  463. package/server/service/data-read/record-keys.test.ts +0 -42
  464. package/server/service/grounding-sources.test.ts +0 -55
  465. package/server/service/grounding-sources.ts +0 -46
  466. package/server/service/help-documents/help-keys.ts +0 -2
  467. package/server/service/help-documents/help-tools.test.ts +0 -40
  468. package/server/service/help-documents/help-tools.ts +0 -88
  469. package/server/service/index.ts +0 -99
  470. package/server/service/people/index.ts +0 -15
  471. package/server/service/people/invite-tools.ts +0 -159
  472. package/server/service/people/invite.test.ts +0 -159
  473. package/server/service/people/people-keys.ts +0 -16
  474. package/server/service/people/people-read.test.ts +0 -82
  475. package/server/service/people/people-read.ts +0 -56
  476. package/server/service/people/person-subject.test.ts +0 -161
  477. package/server/service/people/person-subject.ts +0 -83
  478. package/server/service/request-translator.test.ts +0 -45
  479. package/server/service/request-translator.ts +0 -33
  480. package/server/service/session-anchor.test.ts +0 -198
  481. package/server/service/station-session-guard.test.ts +0 -31
  482. package/server/service/testing/conversation-scenario.test.ts +0 -164
  483. package/server/service/testing/conversation-script.test.ts +0 -133
  484. package/server/service/testing/conversation-script.ts +0 -296
  485. package/server/service/testing/mode-conformance.test.ts +0 -61
  486. package/server/service/testing/mode-conformance.ts +0 -165
  487. package/server/service/tool-access.test.ts +0 -96
  488. package/server/service/tool-access.ts +0 -36
  489. package/server/service/types.ts +0 -15
  490. package/server/service/unattended-run/assistant-run.ts +0 -199
  491. package/server/service/unattended-run/index.ts +0 -10
  492. package/server/service/unattended-run/limits.ts +0 -24
  493. package/server/service/unattended-run/run-as.ts +0 -38
  494. package/server/service/unattended-run/run-budget.ts +0 -58
  495. package/server/service/unattended-run/run-ledger.ts +0 -31
  496. package/server/service/unattended-run/run-outcome.ts +0 -43
  497. package/server/service/unattended-run/run-store-race.pg.test.ts +0 -83
  498. package/server/service/unattended-run/run-store.ts +0 -162
  499. package/server/service/unattended-run/run-unattended.test.ts +0 -478
  500. package/server/service/unattended-run/run-unattended.ts +0 -248
  501. package/server/service/unattended-run/unattended-prompt.ts +0 -24
  502. package/server/service/workspace/workspace-blocks.ts +0 -134
  503. package/server/service/workspace/workspace-tools.ts +0 -108
  504. package/server/service/workspace/workspace.test.ts +0 -74
  505. package/server/tool-registry-boot-report.test.ts +0 -41
  506. package/server/tool-registry-boot-report.ts +0 -63
  507. package/test/ai-agent-settings.test.ts +0 -123
  508. package/test/applied-screens.test.ts +0 -65
  509. package/test/assistant-chat-call-sites.test.ts +0 -92
  510. package/test/assistant-dock.test.ts +0 -294
  511. package/test/assistant-request-context.test.ts +0 -72
  512. package/test/assistant-session-controller.test.ts +0 -87
  513. package/test/assistant-session-manager.test.ts +0 -186
  514. package/test/assistant-session-toolbar.test.ts +0 -43
  515. package/test/attachment-one-rule.test.ts +0 -46
  516. package/test/attachment-policy-agree.test.ts +0 -38
  517. package/test/composer-frame.test.ts +0 -173
  518. package/test/document-text.test.ts +0 -175
  519. package/test/entry-card-styles.test.ts +0 -18
  520. package/test/entry-proposal.test.ts +0 -186
  521. package/test/entry-workspace.test.ts +0 -89
  522. package/test/menu-room.test.ts +0 -33
  523. package/test/open-doors.test.ts +0 -164
  524. package/test/open-page.test.ts +0 -31
  525. package/test/pasted-table.test.ts +0 -147
  526. package/test/refusals-in-korean.test.ts +0 -80
  527. package/test/session-discovery.test.ts +0 -43
  528. package/test/spreadsheet-text.test.ts +0 -92
  529. package/test/translations.test.ts +0 -80
  530. package/test/workspace.test.ts +0 -54
  531. package/tsconfig.json +0 -9
@@ -1,774 +0,0 @@
1
- /*
2
- * Data entry from material - what an application declares so the assistant can enter its records
3
- * (operato-application/design/ai/data-entry.md).
4
- *
5
- * A person hands the conversation what the records are made of - a bank export, a photo of a receipt,
6
- * lines pasted from a mail - and the assistant turns it into rows of one of the application's records.
7
- * Nothing is written by the model. It proposes the rows; the card shows them with what was wrong in
8
- * each and where each came from; a correction is said in the conversation and the model proposes again;
9
- * the person presses the button; the server enters each row **through the application's own GraphQL
10
- * mutation**, run with the person's request state - the same door, directives and checks as entering by hand.
11
- * There is no second way into the record.
12
- *
13
- * This file is pure - no framework, no database - so the rules a row passes are tested without a server.
14
- */
15
- import { parse } from 'graphql'
16
-
17
- export type EntryFieldType = 'string' | 'text' | 'number' | 'integer' | 'date' | 'datetime' | 'boolean' | 'enum' | 'reference'
18
-
19
- export interface EntryField {
20
- name: string
21
- type: EntryFieldType
22
- required?: boolean
23
- /** The allowed values of an `enum` field. */
24
- values?: string[]
25
- /** What the model reads about the field - English, like every tool description. */
26
- description?: string
27
- /** i18n key of the column heading on the card. Absent: the field name. */
28
- labelKey?: string
29
- /** A number field the card sums - and compares with the total the material prints (§ totals). */
30
- total?: boolean
31
- /** An enum's values on the card: `<prefix><value in lowercase>` - `text.expense-payment-` shows PERSONAL_CARD as 개인카드. */
32
- valueKeyPrefix?: string
33
- /**
34
- * A `reference` field points at another record - a loan, an item, a location. The model knows it by a name or
35
- * a code; the server finds it through the application's query, as the person, and keeps its id. One match is
36
- * taken; none or several keep the row out and say so - the server never picks among several.
37
- */
38
- reference?: EntryReference
39
- /**
40
- * The axis of the record the field names (`tax-invoice`, `contract`) - several when it may name a record of each - for a
41
- * record the model is shown by its key: an open record's anchor and an application's link evidence write
42
- * `<axis>:<id>`. The key is read as the id it names, so what the model was shown is what it may write (2026-10-23: a
43
- * card took `tax-invoice:<uuid>` from the evidence, looked it up as an approval number and found nothing). A reference
44
- * reads it before its query runs; a string field holding an id reads it as the id.
45
- */
46
- axis?: string | readonly string[]
47
- }
48
-
49
- export interface EntryReference {
50
- query: string
51
- /** The query's variables for what the model wrote - "기업은행", "A-100". */
52
- variables(text: string): Record<string, unknown>
53
- /** The records the answer holds that the text names - the application filters, the server does not guess. */
54
- recordsOf(data: any, text: string): { id: string; label?: string }[]
55
- }
56
-
57
- const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
58
-
59
- /**
60
- * The id a record's key names - `<axis>:<uuid>` as an anchor writes it, or the uuid alone. Nothing for a word, or for
61
- * the key of a record of another axis: that one is not this record, whatever its uuid.
62
- */
63
- export function recordIdOfKey(axis: string | readonly string[], text: string): string | undefined {
64
- const value = String(text ?? '').trim()
65
- const prefix = (typeof axis === 'string' ? [axis] : axis).map(one => `${one}:`).find(one => value.startsWith(one))
66
- const id = prefix ? value.slice(prefix.length) : value
67
- return UUID.test(id) ? id : undefined
68
- }
69
-
70
- /** What the model wrote for a field that names a record - the id, when it wrote the record's key. */
71
- const shownId = (field: EntryField, text: string): string => (field.axis && recordIdOfKey(field.axis, text)) || text
72
-
73
- /** A resolved reference, as a row keeps it. */
74
- export interface ReferenceValue {
75
- id: string
76
- label?: string
77
- }
78
-
79
- /** Where a row came from - which file, and where in it. Shown on the card beside the row. */
80
- export interface EntrySource {
81
- attachmentId?: string
82
- /** "row 3", "the receipt photo" - the model's words, in the person's language. */
83
- where?: string
84
- /** The value came from what the person said in the conversation - shown in the screen's own words, never the model's. */
85
- person?: boolean
86
- }
87
-
88
- export interface EntryIssue {
89
- /** The field it is about. Absent: the row as a whole. */
90
- field?: string
91
- /** A code the screen translates: `ai-assistant.entry.issue.<code>`, or the target's own `messageKey`. */
92
- code: string
93
- messageKey?: string
94
- params?: Record<string, unknown>
95
- /** A param that is a code the screen words - { status: 'text.state-' } shows `down` as 고장. */
96
- paramKeys?: Record<string, string>
97
- /** A blocking issue keeps the row out when the person presses the button. */
98
- blocking: boolean
99
- }
100
-
101
- export interface EntryRowInput {
102
- values?: Record<string, unknown>
103
- sources?: EntrySource[]
104
- }
105
-
106
- export interface EntryRow {
107
- index: number
108
- values: Record<string, unknown>
109
- sources: EntrySource[]
110
- issues: EntryIssue[]
111
- /** The line of a text source where this row's numbers and dates were all found - the server's check, not the model's word. */
112
- evidence?: { attachmentId: string; line: number }
113
- }
114
-
115
- export interface EntryContext {
116
- /** The request state - domain, user, and when writing, `tx` and `afterCommit`. */
117
- state: any
118
- }
119
-
120
- export interface EntryApplied {
121
- /** The created record's id, when there is one. */
122
- id?: string
123
- /** A short name for the conversation line - "2026-09-12 · 택시 · ₩12,400". */
124
- label?: string
125
- }
126
-
127
- /** What happened, for an application that writes down every assistant call (office's AI audit). */
128
- export interface EntryEvent {
129
- phase: 'propose' | 'apply'
130
- tool: string
131
- rows: number
132
- ok: boolean
133
- applied?: number
134
- missed?: number
135
- error?: string
136
- /** propose: the rows as the model gave them; apply: the proposal key and each row's result. */
137
- detail?: unknown
138
- }
139
-
140
- export interface EntryTarget {
141
- /** `<app>.<record>` - `office.expense-line`. */
142
- key: string
143
- /**
144
- * The records the card is about besides its own (the axes of `subject` anchors) - a link card is about the deposit,
145
- * the invoice and the round it joins. On those records' screens its propose tool is put in front (ADR-0061 decision
146
- * 9); asked to link a deposit from its 360, the model found the invoice and never opened the card (2026-10-24).
147
- */
148
- axes?: string[]
149
- /** i18n key of the record's name on the card. */
150
- labelKey: string
151
- /** What one row is, for the model. */
152
- description: string
153
- fields: EntryField[]
154
- /**
155
- * The application's own mutation that enters one record, as a GraphQL document with one root field -
156
- * `mutation ($line: NewExpenseLine!) { addExpenseLine(line: $line) { id } }`. Its root field is the door:
157
- * the propose tool is offered only to someone who passes it, and each row is entered by running it with
158
- * the person's request state, so `@privilege`, `@transaction` and the door's own checks apply as they do
159
- * to a person typing it in.
160
- */
161
- mutation: string
162
- /**
163
- * The mutation's variables for one row. Absent when the target enters rows by group (below). `card.sessionId` is the
164
- * conversation the card was pressed in - for a record about the conversation itself, never a value the model wrote.
165
- */
166
- variables?(row: EntryRow, card?: { sessionId?: string }): Record<string, unknown>
167
- /** What the conversation says of an entered row - its id and a short name. */
168
- appliedOf?(data: any, row: EntryRow): EntryApplied | undefined
169
- /**
170
- * A record with lines - one order, several item lines. Rows that share the value of `by` (the order number) are one
171
- * record, entered by one call of the mutation with `variables(rows)`. If any row of a group is kept out, the whole
172
- * group is: an order missing a line must not go in unseen.
173
- */
174
- group?: {
175
- by: string
176
- variables(rows: EntryRow[]): Record<string, unknown>
177
- appliedOf?(data: any, rows: EntryRow[]): EntryApplied | undefined
178
- }
179
- /**
180
- * The application's route where the entered or changed records are seen - the card offers it once they went in.
181
- * Left out, the list screen of the read target with the same key; declared where that is none (a parent's screen,
182
- * a workspace).
183
- */
184
- screen?: string
185
- /** At most this many rows in one proposal. Default 200. */
186
- maxRows?: number
187
- /**
188
- * Read-only checks the application owns - a duplicate of what is already entered, a code that does not
189
- * exist. Asked when the rows are proposed and again when they are entered: the records may have moved.
190
- * Returns one list per row, in order.
191
- */
192
- check?(rows: EntryRow[], context: EntryContext): EntryIssue[][] | Promise<EntryIssue[][]>
193
- /** Told of every proposal and every entering - an application that audits its assistant writes it down. A throw is ignored. */
194
- record?(event: EntryEvent, context: EntryContext): void | Promise<void>
195
- /** Changing records that exist (design/ai/data-entry.md §5-1). Absent: this target only enters new ones. */
196
- update?: EntryUpdate
197
- }
198
-
199
- /** One record as the application's read query answers it - an `id` and the fields. */
200
- export type EntryRecord = { id: string } & Record<string, unknown>
201
-
202
- /**
203
- * How records of a target are changed - every step through the application's API, as the person.
204
- *
205
- * Which record is meant is the server's to settle, never the model's memory: whichever way it was picked
206
- * (on screen, selected, described), the server reads it with `read` before a card is made and again before
207
- * it is written.
208
- */
209
- export interface EntryUpdate {
210
- /** Reads the records by id, as the person - one they may not see is not found. */
211
- read: { query: string; variables(ids: string[]): Record<string, unknown>; recordsOf(data: any, ids: string[]): EntryRecord[] }
212
- /** Finds candidates from what the person said - "last week's 12,400 taxi". */
213
- find?: { query: string; variables(criteria: Record<string, unknown>): Record<string, unknown>; recordsOf(data: any, criteria: Record<string, unknown>): EntryRecord[]; criteria: EntryField[] }
214
- /** The application's own mutation that changes one record. */
215
- mutation: string
216
- variables(change: { id: string; record: EntryRecord; values: Record<string, unknown> }): Record<string, unknown>
217
- /** How a record is named on the card - "2026-09-12 · 카카오T". */
218
- labelOf?(record: EntryRecord): string
219
- /** The fields that may change. Default: every declared field. */
220
- fields?: string[]
221
- /**
222
- * Read-only checks of the changes - a new name that another record already has. Asked when the card is made and
223
- * again when it is pressed, like an entry's check; one issue list per row. Without it a clash is caught only by
224
- * the database's own refusal.
225
- */
226
- check?(rows: { id: string; record?: EntryRecord; values: Record<string, unknown> }[], context: EntryContext): EntryIssue[][] | Promise<EntryIssue[][]>
227
- }
228
-
229
- /**
230
- * `Mutation.<root field>` of the target's mutation - the one declaration both the tool's gate and the
231
- * entering read. A document that is not one mutation with one root field is refused at boot.
232
- */
233
- export function entryDoorOf(mutation: string): string {
234
- return rootFieldOf(mutation, 'mutation')
235
- }
236
-
237
- /** `Query.<field>` or `Mutation.<field>` of a document with one operation and one root field. */
238
- export function rootFieldOf(document: string, operation: 'query' | 'mutation'): string {
239
- const doc = parse(document)
240
- const operations = doc.definitions.filter((one: any) => one.kind === 'OperationDefinition') as any[]
241
- if (operations.length !== 1 || operations[0].operation !== operation) throw new Error(`is not one ${operation}`)
242
- const fields = operations[0].selectionSet.selections.filter((one: any) => one.kind === 'Field')
243
- if (fields.length !== 1) throw new Error('must have exactly one root field')
244
- return `${operation === 'query' ? 'Query' : 'Mutation'}.${fields[0].name.value}`
245
- }
246
-
247
- /** Tells the target, and never lets its record keeping stop the work. */
248
- export async function recordEntryEvent(target: EntryTarget, event: EntryEvent, context: EntryContext): Promise<void> {
249
- if (!target.record) return
250
- try {
251
- await target.record(event, context)
252
- } catch {
253
- /* an audit that failed to write is the application's to report */
254
- }
255
- }
256
-
257
- export const DEFAULT_MAX_ROWS = 200
258
-
259
- /* The model's way of saying a value came from the person - kept only to read older rows and loose words. */
260
- const PERSON = /person|message|user|사용자|말/i
261
-
262
- const targets = new Map<string, EntryTarget>()
263
-
264
- const KEY = /^[a-z][a-z0-9-]*\.[a-z][a-z0-9-]*$/
265
-
266
- /** Validates and keeps a target - `registerEntryTarget` (entry-tools.ts) also opens its tool. A malformed declaration is refused at boot, not in a conversation. */
267
- export function declareEntryTarget(target: EntryTarget): void {
268
- if (!KEY.test(target?.key ?? '')) throw new Error(`[ai-assistant] entry target key '${target?.key}' is not '<app>.<record>'.`)
269
- try {
270
- entryDoorOf(target.mutation ?? '')
271
- } catch (error: any) {
272
- throw new Error(`[ai-assistant] entry target '${target.key}': its mutation ${error?.message ?? 'does not parse'}.`)
273
- }
274
- if (typeof target.variables !== 'function' && typeof target.group?.variables !== 'function')
275
- throw new Error(`[ai-assistant] entry target '${target.key}' has no variables (per row, or per group).`)
276
- if (target.group && !target.fields.some(field => field.name === target.group!.by))
277
- throw new Error(`[ai-assistant] entry target '${target.key}': group by '${target.group.by}' is not a declared field.`)
278
- if (target.update) {
279
- for (const [what, document, operation] of [
280
- ['update.read', target.update.read?.query, 'query'],
281
- ['update.mutation', target.update.mutation, 'mutation'],
282
- ...(target.update.find ? [['update.find', target.update.find.query, 'query']] : [])
283
- ] as [string, string, 'query' | 'mutation'][]) {
284
- try {
285
- rootFieldOf(document ?? '', operation)
286
- } catch (error: any) {
287
- throw new Error(`[ai-assistant] entry target '${target.key}': ${what} ${error?.message ?? 'does not parse'}.`)
288
- }
289
- }
290
- const known = new Set(target.fields.map(field => field.name))
291
- for (const name of target.update.fields ?? []) if (!known.has(name)) throw new Error(`[ai-assistant] entry target '${target.key}': update field '${name}' is not declared.`)
292
- }
293
- if (!target.fields?.length) throw new Error(`[ai-assistant] entry target '${target.key}' declares no fields.`)
294
- const names = new Set<string>()
295
- for (const field of target.fields) {
296
- if (field.type === 'reference') {
297
- try {
298
- rootFieldOf(field.reference?.query ?? '', 'query')
299
- } catch (error: any) {
300
- throw new Error(`[ai-assistant] entry target '${target.key}': reference field '${field.name}' ${error?.message ?? 'has no query'}.`)
301
- }
302
- }
303
- if (names.has(field.name)) throw new Error(`[ai-assistant] entry target '${target.key}' declares '${field.name}' twice.`)
304
- names.add(field.name)
305
- if (field.type === 'enum' && !field.values?.length)
306
- throw new Error(`[ai-assistant] entry target '${target.key}': enum field '${field.name}' lists no values.`)
307
- }
308
- targets.set(target.key, target)
309
- }
310
-
311
- export function entryTargetOf(key: string | undefined): EntryTarget | undefined {
312
- return key ? targets.get(key) : undefined
313
- }
314
-
315
- export function entryTargets(): EntryTarget[] {
316
- return [...targets.values()]
317
- }
318
-
319
- /** For tests. */
320
- export function clearEntryTargets(): void {
321
- targets.clear()
322
- }
323
-
324
- const pascalOf = (key: string) =>
325
- key
326
- .split(/[.-]/)
327
- .filter(Boolean)
328
- .map(part => part[0].toUpperCase() + part.slice(1))
329
- .join('')
330
-
331
- /** `office.expense-line` → `proposeOfficeExpenseLineChanges` / `findOfficeExpenseLine` - the tools of changing. */
332
- export const changeToolName = (key: string) => `propose${pascalOf(key)}Changes`
333
- export const findToolName = (key: string) => `find${pascalOf(key)}`
334
-
335
- /** `office.expense-line` → `proposeOfficeExpenseLineEntries` - one tool per target, so each has its own door. */
336
- export function entryToolName(key: string): string {
337
- const pascal = key
338
- .split(/[.-]/)
339
- .filter(Boolean)
340
- .map(part => part[0].toUpperCase() + part.slice(1))
341
- .join('')
342
- return `propose${pascal}Entries`
343
- }
344
-
345
- export function entryCategoryName(key: string): string {
346
- return `data-entry:${key}`
347
- }
348
-
349
- const empty = (value: unknown) => value === undefined || value === null || (typeof value === 'string' && !value.trim())
350
-
351
- /** "12,400" · "₩12,400" · "12 400" → 12400. Only the separators people write; anything else is not a number. */
352
- function toNumber(value: unknown): number | undefined {
353
- if (typeof value === 'number') return Number.isFinite(value) ? value : undefined
354
- if (typeof value !== 'string') return undefined
355
- const cleaned = value.trim().replace(/^[₩$€¥£]\s*/, '').replace(/[,\s]/g, '').replace(/원$/, '')
356
- if (!/^-?\d+(\.\d+)?$/.test(cleaned)) return undefined
357
- return Number(cleaned)
358
- }
359
-
360
- /** A calendar date as `YYYY-MM-DD` - "2026.9.12", "2026/09/12" too. A day that does not exist is not a date. */
361
- function toDate(value: unknown): string | undefined {
362
- if (typeof value !== 'string') return undefined
363
- const match = value.trim().match(/^(\d{4})[-./](\d{1,2})[-./](\d{1,2})\.?$/)
364
- if (!match) return undefined
365
- const [year, month, day] = [Number(match[1]), Number(match[2]), Number(match[3])]
366
- const date = new Date(Date.UTC(year, month - 1, day))
367
- if (date.getUTCFullYear() !== year || date.getUTCMonth() !== month - 1 || date.getUTCDate() !== day) return undefined
368
- return `${match[1]}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`
369
- }
370
-
371
- /**
372
- * A moment as `YYYY-MM-DDTHH:mm:ss`, with the offset when it was written - "2026-10-01 08:30", "2026.10.1 8:30",
373
- * an ISO string. Without an offset it stays without one: the application reads it in its own time zone, as it
374
- * reads a time typed into its own screen.
375
- */
376
- function toDateTime(value: unknown): string | undefined {
377
- if (typeof value !== 'string') return undefined
378
- const match = value.trim().match(/^(\d{4})[-./](\d{1,2})[-./](\d{1,2})[ T](\d{1,2}):(\d{2})(?::(\d{2})(?:\.\d+)?)?\s*(Z|[+-]\d{2}:?\d{2})?$/)
379
- if (!match) return undefined
380
- const day = toDate(`${match[1]}-${match[2]}-${match[3]}`)
381
- const [hour, minute, second] = [Number(match[4]), Number(match[5]), Number(match[6] ?? 0)]
382
- if (!day || hour > 23 || minute > 59 || second > 59) return undefined
383
- const offset = match[7] ? (match[7] === 'Z' ? 'Z' : match[7].replace(/^([+-]\d{2})(\d{2})$/, '$1:$2')) : ''
384
- return `${day}T${String(hour).padStart(2, '0')}:${match[5]}:${String(second).padStart(2, '0')}${offset}`
385
- }
386
-
387
- function toBoolean(value: unknown): boolean | undefined {
388
- if (typeof value === 'boolean') return value
389
- if (typeof value !== 'string') return undefined
390
- const text = value.trim().toLowerCase()
391
- if (['true', 'yes', 'y', '1'].includes(text)) return true
392
- if (['false', 'no', 'n', '0'].includes(text)) return false
393
- return undefined
394
- }
395
-
396
- /** One value in its field's type - or the code of why it is not. An empty value is the caller's to judge. */
397
- export function readFieldValue(field: EntryField, value: unknown): { read?: unknown; code?: string } {
398
- switch (field.type) {
399
- case 'number': {
400
- const read = toNumber(value)
401
- return read === undefined ? { code: 'not-a-number' } : { read }
402
- }
403
- case 'integer': {
404
- const read = toNumber(value)
405
- return read === undefined || !Number.isInteger(read) ? { code: 'not-an-integer' } : { read }
406
- }
407
- case 'date': {
408
- const read = toDate(value)
409
- return read === undefined ? { code: 'not-a-date' } : { read }
410
- }
411
- case 'datetime': {
412
- const read = toDateTime(value)
413
- return read === undefined ? { code: 'not-a-datetime' } : { read }
414
- }
415
- case 'boolean': {
416
- const read = toBoolean(value)
417
- return read === undefined ? { code: 'not-a-boolean' } : { read }
418
- }
419
- case 'enum':
420
- return field.values!.includes(String(value).trim()) ? { read: String(value).trim() } : { code: 'not-in-list' }
421
- case 'reference':
422
- /* Resolved already ({ id }), or a name to look up ({ text }) - resolveReferences settles the second. */
423
- if (value && typeof value === 'object' && typeof (value as any).id === 'string') return { read: { id: shownId(field, (value as any).id), ...((value as any).label ? { label: String((value as any).label) } : {}) } }
424
- if (value && typeof value === 'object' && typeof (value as any).text === 'string') return { read: { text: (value as any).text } }
425
- return typeof value === 'string' || typeof value === 'number' ? { read: { text: String(value).trim() } } : { code: 'not-a-reference' }
426
- default:
427
- return { read: shownId(field, String(value).trim()) }
428
- }
429
- }
430
-
431
- /** Two values of one field the same - "12400.00" read from the database is 12400. */
432
- export function sameFieldValue(field: EntryField, a: unknown, b: unknown): boolean {
433
- if (empty(a) && empty(b)) return true
434
- if (empty(a) || empty(b)) return false
435
- if (field.type === 'reference') {
436
- const idOf = (v: any) => (v && typeof v === 'object' ? v.id ?? v.text : String(v))
437
- return idOf(a) === idOf(b)
438
- }
439
- const x = readFieldValue(field, a)
440
- const y = readFieldValue(field, b)
441
- return x.read !== undefined && y.read !== undefined ? x.read === y.read : String(a) === String(b)
442
- }
443
-
444
- /**
445
- * The rows as the target reads them - each value in its field's type, and what was wrong said as an issue.
446
- *
447
- * A value that does not fit is **kept as it was written and marked**, never dropped or guessed: the card
448
- * shows what the material said, and the person corrects it in the conversation.
449
- */
450
- export function normalizeEntryRows(target: EntryTarget, input: unknown): { rows: EntryRow[]; cut: number } {
451
- const list = Array.isArray(input) ? input : []
452
- const max = target.maxRows ?? DEFAULT_MAX_ROWS
453
- const fields = new Map(target.fields.map(field => [field.name, field]))
454
-
455
- const rows = list.slice(0, max).map((raw: any, index): EntryRow => {
456
- const given = raw && typeof raw.values === 'object' && raw.values ? raw.values : {}
457
- const values: Record<string, unknown> = {}
458
- const issues: EntryIssue[] = []
459
-
460
- for (const name of Object.keys(given)) {
461
- if (!fields.has(name)) issues.push({ field: name, code: 'unknown-field', blocking: false })
462
- }
463
-
464
- for (const field of target.fields) {
465
- const value = given[field.name]
466
- if (empty(value)) {
467
- if (field.required) issues.push({ field: field.name, code: 'required', blocking: true })
468
- continue
469
- }
470
- const { read, code } = readFieldValue(field, value)
471
- if (read === undefined) {
472
- values[field.name] = value
473
- issues.push({ field: field.name, code: code!, params: code === 'not-in-list' ? { values: field.values } : undefined, blocking: true })
474
- } else {
475
- values[field.name] = read
476
- }
477
- }
478
-
479
- const sources: EntrySource[] = Array.isArray(raw?.sources)
480
- ? raw.sources
481
- .filter((one: any) => one && (one.attachmentId || one.where || one.person === true))
482
- .map((one: any): EntrySource => {
483
- /* "the person's message" written by the model is the person flag - the screen says it in its own words. */
484
- if (one.person === true || (!one.attachmentId && typeof one.where === 'string' && PERSON.test(one.where))) return { person: true }
485
- return {
486
- ...(typeof one.attachmentId === 'string' ? { attachmentId: one.attachmentId } : {}),
487
- ...(typeof one.where === 'string' ? { where: one.where.slice(0, 120) } : {})
488
- }
489
- })
490
- : []
491
- if (!sources.length) issues.push({ code: 'no-source', blocking: false })
492
-
493
- return { index, values, sources, issues }
494
- })
495
-
496
- return { rows, cut: Math.max(0, list.length - max) }
497
- }
498
-
499
- /** Adds the target's own findings to each row. A check that throws marks nothing and is reported by the caller. */
500
- export function withCheckedIssues(rows: EntryRow[], found: EntryIssue[][] | undefined): EntryRow[] {
501
- if (!found) return rows
502
- return rows.map((row, i) => (found[i]?.length ? { ...row, issues: [...row.issues, ...found[i]] } : row))
503
- }
504
-
505
- export function isBlocked(row: EntryRow): boolean {
506
- return row.issues.some(issue => issue.blocking)
507
- }
508
-
509
- /* ── Checking the rows against the material (design/ai/data-entry.md §4-1) ─────────────────────────────
510
- *
511
- * A row cites where it came from; the model's word is not proof. For a text source the server looks: the
512
- * row's numbers and dates must stand together on one line of the cited file. Found - the row carries that
513
- * line as evidence. A value the file does not have is marked, unless the row also cites the person's
514
- * message (a correction said in the conversation). A photo cannot be read here - the row says so.
515
- * Names and descriptions are not looked for: the model tidies them, and a tidied name is not a wrong one.
516
- */
517
-
518
-
519
- /** The ways a number or a date is written in a file. */
520
- export function writtenForms(type: EntryFieldType, value: unknown): string[] {
521
- if (type === 'number' || type === 'integer') {
522
- if (typeof value !== 'number' || !Number.isFinite(value)) return []
523
- const negative = value < 0
524
- const abs = Math.abs(value)
525
- const fixed = Number.isInteger(abs) ? String(abs) : abs.toFixed(2).replace(/0+$/, '').replace(/\.$/, '')
526
- const [int, frac] = fixed.split('.')
527
- const grouped = int.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
528
- const forms = new Set<string>()
529
- for (const whole of [int, grouped]) {
530
- forms.add(frac ? `${whole}.${frac}` : whole)
531
- if (!frac) forms.add(`${whole}.00`)
532
- }
533
- return [...forms].map(form => (negative ? `-${form}` : form))
534
- }
535
- if (type === 'datetime') {
536
- /* A moment is found by its day and its hh:mm - the file writes seconds and zones its own way. */
537
- const match = typeof value === 'string' ? value.match(/^(\d{4}-\d{2}-\d{2})T(\d{2}):(\d{2})/) : null
538
- if (!match) return []
539
- return writtenForms('date', match[1])
540
- }
541
- if (type === 'date') {
542
- const match = typeof value === 'string' ? value.match(/^(\d{4})-(\d{2})-(\d{2})$/) : null
543
- if (!match) return []
544
- const [, y, m, d] = match
545
- const mm = String(Number(m))
546
- const dd = String(Number(d))
547
- return [`${y}-${m}-${d}`, `${y}.${m}.${d}`, `${y}/${m}/${d}`, `${y}${m}${d}`, `${y}.${mm}.${dd}`, `${y}/${mm}/${dd}`, `${y}-${mm}-${dd}`, `${y.slice(2)}.${m}.${d}`, `${y.slice(2)}/${m}/${d}`]
548
- }
549
- return []
550
- }
551
-
552
- /**
553
- * A form found as a whole number - "12,400" is not found in "112,400", "12,4001", "12,400.50" or "12,400,000";
554
- * "2026-09-12" not in "2026-09-120". A CSV's own commas are not part of the number.
555
- */
556
- export function lineHas(line: string, forms: string[]): boolean {
557
- const digit = (c: string | undefined) => !!c && c >= '0' && c <= '9'
558
- return forms.some(form => {
559
- for (let at = line.indexOf(form); at >= 0; at = line.indexOf(form, at + 1)) {
560
- const before = line[at - 1]
561
- const after = line[at + form.length]
562
- /* A comma is also the column separator of a CSV: only a comma and three digits more continue the number. */
563
- if (digit(before) || (before === '.' && digit(line[at - 2]))) continue
564
- if (digit(after) || (after === '.' && digit(line[at + form.length + 1]))) continue
565
- if (after === ',' && /^,\d{3}(?!\d)/.test(line.slice(at + form.length))) continue
566
- return true
567
- }
568
- return false
569
- })
570
- }
571
-
572
- /**
573
- * Checks each row against the text of the files it cites. `texts` holds what could be read, by attachment id;
574
- * an id that is not there was an image, or could not be opened.
575
- */
576
- export function verifyRowsAgainstSources(rows: EntryRow[], fields: EntryField[], texts: Map<string, string>): EntryRow[] {
577
- const checked = fields.filter(field => ['number', 'integer', 'date'].includes(field.type))
578
- return rows.map(row => {
579
- const files = row.sources.map(source => source.attachmentId).filter((id): id is string => !!id)
580
- if (!files.length || !checked.length) return row
581
- const readable = files.filter(id => texts.has(id))
582
- if (!readable.length) return { ...row, issues: [...row.issues, { code: 'source-not-checked', blocking: false }] }
583
-
584
- const forms = checked
585
- .map(field => ({ field, forms: writtenForms(field.type, row.values[field.name]) }))
586
- .filter(one => one.forms.length)
587
- if (!forms.length) return row
588
-
589
- for (const id of readable) {
590
- const lines = texts.get(id)!.split(/\r?\n/)
591
- const at = lines.findIndex(line => forms.every(one => lineHas(line, one.forms)))
592
- if (at >= 0) return { ...row, evidence: { attachmentId: id, line: at + 1 } }
593
- }
594
-
595
- const saidByPerson = row.sources.some(source => source.person)
596
- const issues = [...row.issues]
597
- for (const one of forms) {
598
- const anywhere = readable.some(id => texts.get(id)!.split(/\r?\n/).some(line => lineHas(line, one.forms)))
599
- if (!anywhere && !saidByPerson) issues.push({ field: one.field.name, code: 'not-in-source', blocking: false })
600
- }
601
- if (issues.length === row.issues.length) issues.push({ code: 'source-scattered', blocking: false })
602
- return { ...row, issues }
603
- })
604
- }
605
-
606
- /**
607
- * The total a file prints - a line named 합계 · 총계 · 총액 · total · sum, and the last amount on it. The server
608
- * reads it itself, so the card can compare even when the model did not say it (2026-10-01: once it did, once not).
609
- */
610
- export function printedTotalOf(text: string): { value: number; line: number } | undefined {
611
- const lines = text.split(/\r?\n/)
612
- for (let i = lines.length - 1; i >= 0; i--) {
613
- if (!/(합\s*계|총\s*계|총\s*액|total|sum)/i.test(lines[i])) continue
614
- const amounts = [...lines[i].matchAll(/-?\d{1,3}(?:,\d{3})+(?:\.\d+)?|-?\d+(?:\.\d+)?/g)].map(match => Number(match[0].replace(/,/g, '')))
615
- if (amounts.length) return { value: amounts[amounts.length - 1], line: i + 1 }
616
- }
617
- return undefined
618
- }
619
-
620
- export interface EntryTotal {
621
- field: string
622
- /** The sum over every row, and over the rows that will be entered. */
623
- all: number
624
- ready: number
625
- /** The total the material prints, as the model read it - and whether the server found it in the file. */
626
- declared?: number
627
- declaredFound?: boolean
628
- /** declared - all. */
629
- difference?: number
630
- /** Where the declared total came from - the model's reading, or the server's own from a file's total line. */
631
- declaredBy?: 'model' | 'file'
632
- }
633
-
634
- /** The sums of the fields declared `total`, and how they stand against the total the material prints. */
635
- export function entryTotals(
636
- rows: EntryRow[],
637
- fields: EntryField[],
638
- declared: { field?: string; value?: unknown; attachmentId?: string } | undefined,
639
- texts: Map<string, string>
640
- ): EntryTotal[] {
641
- const totalled = fields.filter(field => field.total && (field.type === 'number' || field.type === 'integer'))
642
- /* With one totalled field and no total from the model, the server looks for the file's own total line. */
643
- const printed = !declared && totalled.length === 1 ? [...texts.values()].map(printedTotalOf).find(Boolean) : undefined
644
- return totalled
645
- .map(field => {
646
- const sum = (list: EntryRow[]) => Math.round(list.reduce((acc, row) => acc + (typeof row.values[field.name] === 'number' ? (row.values[field.name] as number) : 0), 0) * 100) / 100
647
- const total: EntryTotal = { field: field.name, all: sum(rows), ready: sum(rows.filter(row => !isBlocked(row))) }
648
- if (printed) {
649
- total.declared = printed.value
650
- total.declaredFound = true
651
- total.declaredBy = 'file'
652
- total.difference = Math.round((printed.value - total.all) * 100) / 100
653
- return total
654
- }
655
- /*
656
- * With one totalled field the model's total is that field's, whatever it named it - on office's second card it
657
- * wrote the file's column name (이용금액) for amount, and the total was dropped (2026-10-01).
658
- */
659
- const mine = declared && (totalled.length === 1 || (declared.field ?? field.name) === field.name)
660
- const value = mine ? toNumber(declared!.value) : undefined
661
- if (value !== undefined) {
662
- total.declaredBy = 'model'
663
- total.declared = value
664
- total.difference = Math.round((value - total.all) * 100) / 100
665
- const text = declared!.attachmentId ? texts.get(declared!.attachmentId) : undefined
666
- if (text !== undefined) total.declaredFound = text.split(/\r?\n/).some(line => lineHas(line, writtenForms(field.type, value)))
667
- }
668
- return total
669
- })
670
- }
671
-
672
- /** The doors of the queries a target's reference fields reach - the person must pass them too. */
673
- export function referenceDoorsOf(fields: EntryField[]): string[] {
674
- return [...new Set(fields.filter(field => field.type === 'reference' && field.reference).map(field => rootFieldOf(field.reference!.query, 'query')))]
675
- }
676
-
677
- /**
678
- * Settles each reference a row names by text, through the application's query as the person. One match keeps its
679
- * id and name; none or several keep the row out - several with their names, so the person can say which.
680
- */
681
- export async function resolveReferences<T extends { values: Record<string, unknown>; issues: EntryIssue[] }>(
682
- fields: EntryField[],
683
- rows: T[],
684
- run: (document: string, variables: Record<string, unknown>) => Promise<{ data?: any; errors?: readonly { message: string }[] }>
685
- ): Promise<T[]> {
686
- const references = fields.filter(field => field.type === 'reference' && field.reference)
687
- if (!references.length) return rows
688
- const asked = new Map<string, { id: string; label?: string }[] | Error>()
689
- for (const row of rows) {
690
- for (const field of references) {
691
- const value: any = row.values[field.name]
692
- if (!value || typeof value !== 'object' || typeof value.text !== 'string') continue
693
- const text = shownId(field, value.text)
694
- const key = `${field.name}\u0000${text}`
695
- if (!asked.has(key)) {
696
- try {
697
- const answer = await run(field.reference!.query, field.reference!.variables(text))
698
- asked.set(key, answer.errors?.length ? new Error(answer.errors[0].message) : field.reference!.recordsOf(answer.data, text) ?? [])
699
- } catch (error: any) {
700
- asked.set(key, error instanceof Error ? error : new Error(String(error)))
701
- }
702
- }
703
- const found = asked.get(key)!
704
- if (found instanceof Error) {
705
- row.issues.push({ field: field.name, code: 'reference-unreadable', params: { text: value.text }, blocking: true })
706
- } else if (found.length === 1) {
707
- row.values[field.name] = { id: found[0].id, ...(found[0].label ? { label: found[0].label } : {}) }
708
- } else {
709
- row.issues.push({
710
- field: field.name,
711
- code: found.length ? 'reference-ambiguous' : 'reference-not-found',
712
- params: { text: value.text, ...(found.length ? { candidates: found.slice(0, 5).map(one => one.label || one.id).join(', ') } : {}) },
713
- blocking: true
714
- })
715
- }
716
- }
717
- }
718
- return rows
719
- }
720
-
721
- /** The rows by group, in the order groups first appear - `[key, rows]`. A row without the key is a group of its own. */
722
- export function entryGroups<T extends { index: number; values: Record<string, unknown> }>(rows: T[], by: string): [string, T[]][] {
723
- const groups = new Map<string, T[]>()
724
- for (const row of rows) {
725
- const value = row.values[by]
726
- const key = value === undefined || value === null || value === '' ? `\u0000row-${row.index}` : String(typeof value === 'object' ? (value as any).id ?? (value as any).text : value)
727
- groups.set(key, [...(groups.get(key) ?? []), row])
728
- }
729
- return [...groups.entries()]
730
- }
731
-
732
- /** The UTC offset a time zone has at a wall time - "+09:00". DST is taken at that day, not today. */
733
- export function offsetAt(timeZone: string | undefined, wall: string): string | undefined {
734
- const match = wall.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})$/)
735
- if (!match) return undefined
736
- const [y, mo, d, h, mi, se] = match.slice(1).map(Number)
737
- let format: Intl.DateTimeFormat
738
- try {
739
- format = new Intl.DateTimeFormat('en-US', { timeZone: timeZone || undefined, timeZoneName: 'longOffset', year: 'numeric' })
740
- } catch {
741
- return undefined
742
- }
743
- const minutesOf = (at: number) => {
744
- const name = format.formatToParts(new Date(at)).find(part => part.type === 'timeZoneName')?.value ?? 'GMT'
745
- const m = name.match(/GMT([+-])(\d{2}):?(\d{2})?/)
746
- return m ? (m[1] === '-' ? -1 : 1) * (Number(m[2]) * 60 + Number(m[3] ?? 0)) : 0
747
- }
748
- /* The offset at the wall time read as UTC, then once more at the instant that gives - right across a DST change. */
749
- const asUtc = Date.UTC(y, mo - 1, d, h, mi, se)
750
- const minutes = minutesOf(asUtc - minutesOf(asUtc) * 60_000)
751
- const sign = minutes < 0 ? '-' : '+'
752
- const abs = Math.abs(minutes)
753
- return `${sign}${String(Math.floor(abs / 60)).padStart(2, '0')}:${String(abs % 60).padStart(2, '0')}`
754
- }
755
-
756
- /**
757
- * A moment written without an offset is the domain's wall time - "08:30" in a factory is 08:30 there. Each such
758
- * value gets the domain time zone's offset at that day, so an API taking DateTimeISO accepts it and reads the moment
759
- * meant (plant refused every downtime log without it, 2026-10-01).
760
- */
761
- export function zonedDateTimes<T extends { values: Record<string, unknown> }>(fields: EntryField[], rows: T[], timeZone: string | undefined): T[] {
762
- const moments = fields.filter(field => field.type === 'datetime')
763
- if (!moments.length) return rows
764
- for (const row of rows) {
765
- for (const field of moments) {
766
- const value = row.values[field.name]
767
- if (typeof value !== 'string' || !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/.test(value)) continue
768
- const offset = offsetAt(timeZone, value)
769
- if (offset) row.values[field.name] = `${value}${offset}`
770
- }
771
- }
772
- return rows
773
- }
774
-