create-windy 0.2.33 → 0.3.0

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 (284) hide show
  1. package/dist/cli.js +173 -17
  2. package/package.json +1 -1
  3. package/template/.agents/skills/windy-business-module/SKILL.md +10 -1
  4. package/template/.windy-template.json +2 -2
  5. package/template/AGENTS.md +9 -0
  6. package/template/README.md +3 -1
  7. package/template/apps/agent-server/Dockerfile +1 -0
  8. package/template/apps/agent-server/README.md +28 -5
  9. package/template/apps/agent-server/adk_web_agents/__init__.py +1 -0
  10. package/template/apps/agent-server/adk_web_agents/safe_debug/__init__.py +3 -0
  11. package/template/apps/agent-server/adk_web_agents/safe_debug/agent.py +40 -0
  12. package/template/apps/agent-server/pyproject.toml +1 -1
  13. package/template/apps/agent-server/src/southwind_agent_server/adk_adapter.py +191 -16
  14. package/template/apps/agent-server/src/southwind_agent_server/adk_failure.py +13 -0
  15. package/template/apps/agent-server/src/southwind_agent_server/adk_model.py +225 -0
  16. package/template/apps/agent-server/src/southwind_agent_server/adk_tools.py +76 -0
  17. package/template/apps/agent-server/src/southwind_agent_server/app.py +63 -3
  18. package/template/apps/agent-server/src/southwind_agent_server/contracts.py +35 -0
  19. package/template/apps/agent-server/src/southwind_agent_server/engine.py +2 -0
  20. package/template/apps/agent-server/src/southwind_agent_server/openai_engine.py +31 -3
  21. package/template/apps/agent-server/src/southwind_agent_server/openai_provider.py +22 -9
  22. package/template/apps/agent-server/src/southwind_agent_server/provider_audit.py +144 -0
  23. package/template/apps/agent-server/src/southwind_agent_server/provider_governance.py +227 -0
  24. package/template/apps/agent-server/src/southwind_agent_server/service.py +1 -0
  25. package/template/apps/agent-server/tests/test_adk_adapter.py +305 -0
  26. package/template/apps/agent-server/tests/test_adk_web_agent.py +12 -0
  27. package/template/apps/agent-server/tests/test_api.py +16 -1
  28. package/template/apps/agent-server/tests/test_openai_provider.py +79 -0
  29. package/template/apps/agent-server/tests/test_provider_governance.py +215 -0
  30. package/template/apps/server/package.json +4 -1
  31. package/template/apps/server/src/agent/provider-input-governance.test.ts +82 -0
  32. package/template/apps/server/src/agent/provider-input-governance.ts +140 -0
  33. package/template/apps/server/src/agent/remote-runtime.test.ts +22 -0
  34. package/template/apps/server/src/agent/remote-runtime.ts +1 -0
  35. package/template/apps/server/src/agent/run-contracts.ts +2 -0
  36. package/template/apps/server/src/agent/run-facade.test.ts +52 -2
  37. package/template/apps/server/src/agent/run-facade.ts +38 -9
  38. package/template/apps/server/src/agent/tool-registry.ts +2 -6
  39. package/template/apps/server/src/application-services.ts +50 -0
  40. package/template/apps/server/src/audit/drizzle-log-store.ts +25 -0
  41. package/template/apps/server/src/audit/search-summary.ts +2 -6
  42. package/template/apps/server/src/auth/credential-restriction.ts +1 -0
  43. package/template/apps/server/src/auth/password.ts +30 -21
  44. package/template/apps/server/src/auth/routes.test.ts +44 -3
  45. package/template/apps/server/src/auth/routes.ts +6 -1
  46. package/template/apps/server/src/auth/runtime.ts +4 -1
  47. package/template/apps/server/src/auth/service.ts +25 -7
  48. package/template/apps/server/src/configuration/bootstrap.ts +20 -1
  49. package/template/apps/server/src/configuration/definition.test.ts +71 -0
  50. package/template/apps/server/src/configuration/definition.ts +242 -12
  51. package/template/apps/server/src/configuration/drizzle-repository.ts +36 -2
  52. package/template/apps/server/src/configuration/repository.ts +27 -2
  53. package/template/apps/server/src/configuration/routes.ts +4 -1
  54. package/template/apps/server/src/configuration/service.test.ts +100 -0
  55. package/template/apps/server/src/configuration/service.ts +50 -10
  56. package/template/apps/server/src/data-access/scoped-repository.integration.test.ts +3 -2
  57. package/template/apps/server/src/data-governance/export/service.ts +2 -6
  58. package/template/apps/server/src/example-modules.ts +4 -1
  59. package/template/apps/server/src/guards.ts +18 -5
  60. package/template/apps/server/src/http/request-source.test.ts +85 -0
  61. package/template/apps/server/src/http/request-source.ts +90 -0
  62. package/template/apps/server/src/index.ts +45 -45
  63. package/template/apps/server/src/license/routes.ts +2 -0
  64. package/template/apps/server/src/license/runtime-service.ts +10 -11
  65. package/template/apps/server/src/module-bootstrap.ts +2 -2
  66. package/template/apps/server/src/module-host/initialize-contracts.ts +6 -0
  67. package/template/apps/server/src/module-host/initialize.ts +30 -44
  68. package/template/apps/server/src/module-host/request-context.ts +29 -23
  69. package/template/apps/server/src/module-host/resource-file-policies.test.ts +197 -0
  70. package/template/apps/server/src/module-host/resource-file-policies.ts +108 -0
  71. package/template/apps/server/src/module-host/route-declaration.ts +45 -0
  72. package/template/apps/server/src/module-host/route-installer.ts +27 -3
  73. package/template/apps/server/src/module-host/storage-port.test.ts +170 -1
  74. package/template/apps/server/src/module-host/storage-port.ts +134 -1
  75. package/template/apps/server/src/module-host/storage-scope.ts +89 -0
  76. package/template/apps/server/src/module-host/tool-handlers.test.ts +305 -0
  77. package/template/apps/server/src/module-host/tool-handlers.ts +38 -4
  78. package/template/apps/server/src/module-host/user-directory-composition.test.ts +126 -0
  79. package/template/apps/server/src/module-host/user-directory-port.test.ts +238 -0
  80. package/template/apps/server/src/module-host/user-directory-port.ts +139 -0
  81. package/template/apps/server/src/module-host.ts +4 -0
  82. package/template/apps/server/src/notification/channel-adapter.ts +46 -0
  83. package/template/apps/server/src/notification/delivery-policy.test.ts +59 -0
  84. package/template/apps/server/src/notification/delivery-policy.ts +37 -0
  85. package/template/apps/server/src/notification/delivery-repository.ts +198 -0
  86. package/template/apps/server/src/notification/delivery-worker.test.ts +87 -0
  87. package/template/apps/server/src/notification/delivery-worker.ts +88 -0
  88. package/template/apps/server/src/notification/drizzle-management.ts +180 -0
  89. package/template/apps/server/src/notification/drizzle-query.ts +160 -0
  90. package/template/apps/server/src/notification/drizzle-repository.integration.test.ts +301 -0
  91. package/template/apps/server/src/notification/drizzle-repository.ts +156 -90
  92. package/template/apps/server/src/notification/extension-routes.test.ts +119 -0
  93. package/template/apps/server/src/notification/extension-routes.ts +111 -0
  94. package/template/apps/server/src/notification/memory-delivery-repository.ts +137 -0
  95. package/template/apps/server/src/notification/memory-support.ts +72 -0
  96. package/template/apps/server/src/notification/preference-repository.ts +95 -0
  97. package/template/apps/server/src/notification/recipient-directory.ts +104 -0
  98. package/template/apps/server/src/notification/repository.test.ts +171 -0
  99. package/template/apps/server/src/notification/repository.ts +179 -32
  100. package/template/apps/server/src/notification/revision-routes.test.ts +186 -0
  101. package/template/apps/server/src/notification/route-support.ts +225 -0
  102. package/template/apps/server/src/notification/routes.test.ts +182 -0
  103. package/template/apps/server/src/notification/routes.ts +225 -132
  104. package/template/apps/server/src/notification/service.test.ts +104 -0
  105. package/template/apps/server/src/notification/service.ts +140 -0
  106. package/template/apps/server/src/notification/smtp-adapter.test.ts +65 -0
  107. package/template/apps/server/src/notification/smtp-adapter.ts +68 -0
  108. package/template/apps/server/src/persistence.integration.test.ts +41 -2
  109. package/template/apps/server/src/route-guards.ts +1 -0
  110. package/template/apps/server/src/runtime-audit-list.test.ts +39 -0
  111. package/template/apps/server/src/runtime-development.ts +2 -0
  112. package/template/apps/server/src/runtime.test.ts +23 -9
  113. package/template/apps/server/src/runtime.ts +39 -2
  114. package/template/apps/server/src/scheduler/audit.ts +2 -11
  115. package/template/apps/server/src/scheduler/definitions.test.ts +6 -0
  116. package/template/apps/server/src/scheduler/definitions.ts +7 -0
  117. package/template/apps/server/src/scheduler/recovery-service.ts +2 -11
  118. package/template/apps/server/src/scheduler/routes.ts +2 -7
  119. package/template/apps/server/src/search/opensearch-client.test.ts +45 -0
  120. package/template/apps/server/src/search/opensearch-client.ts +145 -0
  121. package/template/apps/server/src/search/service.ts +2 -6
  122. package/template/apps/server/src/settings/routes.ts +15 -3
  123. package/template/apps/server/src/settings/runtime.ts +10 -1
  124. package/template/apps/server/src/storage/runtime.ts +13 -0
  125. package/template/apps/server/src/system/audit-query.ts +33 -4
  126. package/template/apps/server/src/system/audit-routes.test.ts +58 -4
  127. package/template/apps/server/src/system/audit-routes.ts +6 -5
  128. package/template/apps/server/src/system/built-in-roles.ts +1 -0
  129. package/template/apps/server/src/system/department-organization.ts +320 -0
  130. package/template/apps/server/src/system/department-routes.test.ts +207 -48
  131. package/template/apps/server/src/system/department-routes.ts +114 -33
  132. package/template/apps/server/src/system/drizzle-repository.ts +15 -7
  133. package/template/apps/server/src/system/drizzle-scoped-repository.ts +5 -3
  134. package/template/apps/server/src/system/drizzle-system.ts +37 -9
  135. package/template/apps/server/src/system/drizzle-transaction-context.ts +14 -0
  136. package/template/apps/server/src/system/identity-route-config.ts +7 -2
  137. package/template/apps/server/src/system/mutation-runner.ts +23 -0
  138. package/template/apps/server/src/system/organization-postgres.integration.test.ts +176 -0
  139. package/template/apps/server/src/system/resource-access-response.ts +12 -6
  140. package/template/apps/server/src/system/role-department-scope-policy.ts +55 -0
  141. package/template/apps/server/src/system/route-helpers.ts +2 -5
  142. package/template/apps/server/src/system/route-types.ts +12 -3
  143. package/template/apps/server/src/system/routes-validation.test.ts +26 -0
  144. package/template/apps/server/src/system/routes.test.ts +10 -2
  145. package/template/apps/server/src/system/routes.ts +70 -36
  146. package/template/apps/server/src/system/seed.ts +7 -0
  147. package/template/apps/server/src/work-order/data-scope.integration.test.ts +9 -2
  148. package/template/apps/server/src/work-order/opensearch-index.test.ts +97 -0
  149. package/template/apps/server/src/work-order/opensearch-index.ts +313 -0
  150. package/template/apps/server/src/work-order/search-provider.ts +60 -1
  151. package/template/apps/server/src/work-order/service.ts +42 -10
  152. package/template/apps/web/src/app/error-pages.ts +9 -0
  153. package/template/apps/web/src/components/auth/PasswordChangeForm.vue +16 -11
  154. package/template/apps/web/src/components/auth/PasswordChangeForm.webtest.ts +14 -0
  155. package/template/apps/web/src/components/common/FormDialog.vue +16 -2
  156. package/template/apps/web/src/components/common/ModalLayout.webtest.ts +20 -1
  157. package/template/apps/web/src/composables/useGlobalSearch.ts +68 -0
  158. package/template/apps/web/src/composables/usePlatformSettings.ts +8 -7
  159. package/template/apps/web/src/layout/GlobalSearchResults.vue +58 -0
  160. package/template/apps/web/src/layout/NavigationSearchDialog.vue +42 -6
  161. package/template/apps/web/src/layout/NavigationSearchDialog.webtest.ts +71 -0
  162. package/template/apps/web/src/layout/NotificationMenu.vue +73 -31
  163. package/template/apps/web/src/layout/NotificationMenu.webtest.ts +44 -1
  164. package/template/apps/web/src/layout/NotificationMenuItem.vue +61 -0
  165. package/template/apps/web/src/layout/NotificationPreferencesDialog.vue +181 -0
  166. package/template/apps/web/src/layout/NotificationPreferencesDialog.webtest.ts +80 -0
  167. package/template/apps/web/src/lib/date-time.ts +22 -10
  168. package/template/apps/web/src/lib/date-time.webtest.ts +9 -1
  169. package/template/apps/web/src/main.ts +14 -1
  170. package/template/apps/web/src/pages/auth/ChangePasswordPage.vue +16 -1
  171. package/template/apps/web/src/pages/configuration/components/ModuleConfigVersionList.vue +1 -0
  172. package/template/apps/web/src/pages/configuration/components/ModuleConfigurationEditor.vue +10 -0
  173. package/template/apps/web/src/pages/configuration/components/ModuleConfigurationWorkbench.vue +37 -6
  174. package/template/apps/web/src/pages/errors/PlatformErrorPage.vue +115 -0
  175. package/template/apps/web/src/pages/errors/PlatformErrorPage.webtest.ts +44 -0
  176. package/template/apps/web/src/pages/errors/error-page-extension.ts +26 -0
  177. package/template/apps/web/src/pages/system/SystemNotificationsPage.vue +136 -68
  178. package/template/apps/web/src/pages/system/SystemNotificationsPage.webtest.ts +224 -12
  179. package/template/apps/web/src/pages/system/audit-log-presenter.ts +86 -0
  180. package/template/apps/web/src/pages/system/audit-time-range.ts +28 -0
  181. package/template/apps/web/src/pages/system/components/AuditActionCell.vue +10 -69
  182. package/template/apps/web/src/pages/system/components/AuditActionCell.webtest.ts +20 -27
  183. package/template/apps/web/src/pages/system/components/AuditLogDetailDialog.vue +134 -0
  184. package/template/apps/web/src/pages/system/components/AuditLogDetailDialog.webtest.ts +76 -0
  185. package/template/apps/web/src/pages/system/components/AuditLogFilters.vue +116 -52
  186. package/template/apps/web/src/pages/system/components/AuditLogFilters.webtest.ts +52 -15
  187. package/template/apps/web/src/pages/system/components/NotificationHistoryDialog.vue +129 -0
  188. package/template/apps/web/src/pages/system/components/NotificationManagementCard.vue +91 -0
  189. package/template/apps/web/src/pages/system/components/NotificationPublishDialog.vue +121 -0
  190. package/template/apps/web/src/pages/system/components/NotificationPublishForm.vue +162 -1
  191. package/template/apps/web/src/pages/system/components/SystemResourceActions.vue +68 -0
  192. package/template/apps/web/src/pages/system/components/SystemResourceEditor.vue +74 -0
  193. package/template/apps/web/src/pages/system/components/SystemResourceFilters.vue +3 -4
  194. package/template/apps/web/src/pages/system/components/SystemResourcePage.vue +91 -98
  195. package/template/apps/web/src/pages/system/components/SystemResourcePage.webtest.ts +68 -0
  196. package/template/apps/web/src/pages/system/components/SystemResourceTable.vue +20 -1
  197. package/template/apps/web/src/pages/system/components/SystemResourceTable.webtest.ts +28 -7
  198. package/template/apps/web/src/pages/system/components/department/DepartmentOrganizationForm.vue +210 -0
  199. package/template/apps/web/src/pages/system/components/department/DepartmentTransitionDialog.vue +112 -0
  200. package/template/apps/web/src/pages/system/components/user-account/UserAccountForm.vue +17 -4
  201. package/template/apps/web/src/pages/system/composables/useDepartmentTransitions.ts +79 -0
  202. package/template/apps/web/src/pages/system/composables/useNotificationPermissions.ts +14 -0
  203. package/template/apps/web/src/pages/system/composables/useSystemResource.ts +2 -0
  204. package/template/apps/web/src/pages/system/composables/useSystemResource.webtest.ts +15 -2
  205. package/template/apps/web/src/pages/system/resource-config.ts +12 -4
  206. package/template/apps/web/src/router/error-navigation.ts +50 -0
  207. package/template/apps/web/src/router/error-navigation.webtest.ts +106 -0
  208. package/template/apps/web/src/router/index.ts +71 -0
  209. package/template/apps/web/src/router/index.webtest.ts +29 -0
  210. package/template/apps/web/src/router/manifest-routes.webtest.ts +4 -0
  211. package/template/apps/web/src/router/shared-scaffold-neutrality.webtest.ts +12 -0
  212. package/template/apps/web/src/services/auth-api.ts +5 -0
  213. package/template/apps/web/src/services/http.ts +43 -6
  214. package/template/apps/web/src/services/http.webtest.ts +70 -0
  215. package/template/apps/web/src/services/notification-api.ts +85 -2
  216. package/template/apps/web/src/services/notification-api.webtest.ts +110 -0
  217. package/template/apps/web/src/services/search-api.ts +39 -0
  218. package/template/apps/web/src/services/system-api.ts +18 -0
  219. package/template/apps/web/vitest.config.ts +1 -0
  220. package/template/docker-compose.yml +68 -0
  221. package/template/docs/architecture/ai-egress.md +49 -7
  222. package/template/docs/architecture/ai-runtime.md +34 -6
  223. package/template/docs/architecture/data-scope-query-enforcement.md +4 -0
  224. package/template/docs/architecture/object-storage.md +22 -2
  225. package/template/docs/architecture/search-provider-protocol.md +38 -2
  226. package/template/docs/development/custom-error-pages.md +78 -0
  227. package/template/docs/development/custom-login-page.md +3 -3
  228. package/template/docs/platform/agent-runtime.md +76 -7
  229. package/template/docs/platform/audit-reliability.md +46 -2
  230. package/template/docs/platform/business-user-directory.md +82 -0
  231. package/template/docs/platform/module-configuration.md +13 -4
  232. package/template/docs/platform/notifications.md +67 -11
  233. package/template/docs/platform/organization-membership.md +17 -3
  234. package/template/docs/platform/system-crud-api.md +16 -0
  235. package/template/docs/platform/web-system-crud.md +6 -2
  236. package/template/package.json +4 -0
  237. package/template/packages/config/index.test.ts +45 -0
  238. package/template/packages/config/package.json +1 -1
  239. package/template/packages/config/src/platform.ts +169 -48
  240. package/template/packages/database/drizzle/0035_material_nightshade.sql +10 -0
  241. package/template/packages/database/drizzle/0036_hesitant_speed.sql +35 -0
  242. package/template/packages/database/drizzle/0037_absent_nick_fury.sql +2 -0
  243. package/template/packages/database/drizzle/0038_uneven_wasp.sql +38 -0
  244. package/template/packages/database/drizzle/meta/0035_snapshot.json +7331 -0
  245. package/template/packages/database/drizzle/meta/0036_snapshot.json +7436 -0
  246. package/template/packages/database/drizzle/meta/0037_snapshot.json +7457 -0
  247. package/template/packages/database/drizzle/meta/0038_snapshot.json +7749 -0
  248. package/template/packages/database/drizzle/meta/_journal.json +28 -0
  249. package/template/packages/database/package.json +1 -1
  250. package/template/packages/database/src/schema/identity.ts +7 -0
  251. package/template/packages/database/src/schema/notifications.ts +135 -0
  252. package/template/packages/database/src/schema-notification-delivery.test.ts +54 -0
  253. package/template/packages/database/src/schema-workflow-notification.test.ts +40 -1
  254. package/template/packages/modules/package.json +1 -1
  255. package/template/packages/modules/src/ai-operation-composition.test.ts +68 -0
  256. package/template/packages/modules/src/ai-operation-composition.ts +115 -0
  257. package/template/packages/modules/src/manifest.ts +26 -0
  258. package/template/packages/modules/src/provider-data-policy-composition.test.ts +74 -0
  259. package/template/packages/modules/src/system-api-permissions.ts +54 -0
  260. package/template/packages/modules/src/system-audit-actions.ts +15 -0
  261. package/template/packages/modules/src/system-features.ts +6 -1
  262. package/template/packages/modules/src/system-module-catalog.test.ts +16 -0
  263. package/template/packages/modules/src/system-module-catalog.ts +28 -4
  264. package/template/packages/modules/src/system-modules.test.ts +13 -1
  265. package/template/packages/modules/src/system-modules.ts +12 -0
  266. package/template/packages/modules/src/system-permissions.ts +11 -5
  267. package/template/packages/server-sdk/index.ts +11 -0
  268. package/template/packages/server-sdk/package.json +3 -2
  269. package/template/packages/server-sdk/src/ai-operation-registration.ts +2 -1
  270. package/template/packages/server-sdk/src/module-host.ts +8 -0
  271. package/template/packages/server-sdk/src/request-context.ts +2 -0
  272. package/template/packages/server-sdk/src/storage-port.ts +66 -0
  273. package/template/packages/server-sdk/src/tool-registration.ts +6 -0
  274. package/template/packages/server-sdk/src/user-directory-port.ts +42 -0
  275. package/template/packages/shared/package.json +1 -1
  276. package/template/packages/shared/src/audit.ts +25 -0
  277. package/template/packages/shared/src/module-configuration.ts +5 -0
  278. package/template/packages/shared/src/notification.ts +92 -1
  279. package/template/packages/shared/src/password.ts +7 -2
  280. package/template/packages/shared/src/platform-settings.ts +2 -0
  281. package/template/packages/storage/package.json +1 -1
  282. package/template/packages/storage/src/upload-service.test.ts +16 -0
  283. package/template/packages/storage/src/upload-service.ts +5 -1
  284. package/template/scripts/notification-postgres-regression.ts +27 -0
@@ -0,0 +1,238 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { ModuleUserDirectoryError } from "@southwind-ai/server-sdk";
3
+ import { buildScopeContext } from "../data-access/context.js";
4
+ import { createFoundationSnapshot } from "../foundation.js";
5
+ import type { RequestActor, RequestGuardContext } from "../guards.js";
6
+ import { createServerRuntime } from "../runtime.js";
7
+ import type { ManagedDepartment, ManagedUser } from "../system/entities.js";
8
+ import { createSystemRepositories } from "../system/seed.js";
9
+ import { createModuleUserDirectoryPort } from "./user-directory-port.js";
10
+
11
+ describe("模块用户目录 Port", () => {
12
+ test("只返回启用用户最小投影,并在 Repository 查询中执行部门范围", async () => {
13
+ const fixture = await createFixture();
14
+ const page = await fixture.port.list({
15
+ page: 1,
16
+ pageSize: 10,
17
+ q: "案",
18
+ ...({ actorId: "forged", departmentId: "dept_other" } as object),
19
+ });
20
+
21
+ expect(page).toEqual({
22
+ items: [
23
+ {
24
+ id: "user_child",
25
+ displayName: "子部门办案员",
26
+ },
27
+ ],
28
+ total: 1,
29
+ page: 1,
30
+ pageSize: 10,
31
+ });
32
+ expect(Object.keys(page.items[0] ?? {}).sort()).toEqual([
33
+ "displayName",
34
+ "id",
35
+ ]);
36
+ expect(fixture.runtime.auditSink.list()).toContainEqual(
37
+ expect.objectContaining({
38
+ action: "system.user-directory.query",
39
+ actor: expect.objectContaining({ id: "actor_manager" }),
40
+ metadata: expect.objectContaining({ resultCount: 1, total: 1 }),
41
+ }),
42
+ );
43
+ });
44
+
45
+ test("缺少权限或 Feature 停用时 fail closed 并记录拒绝", async () => {
46
+ const missingPermission = await createFixture({ permission: false });
47
+ await expect(missingPermission.port.list()).rejects.toMatchObject({
48
+ code: "USER_DIRECTORY_FORBIDDEN",
49
+ });
50
+ expect(missingPermission.runtime.auditSink.list()).toContainEqual(
51
+ expect.objectContaining({
52
+ action: "security.denied",
53
+ outcome: "denied",
54
+ }),
55
+ );
56
+
57
+ const disabledFeature = await createFixture({ featureEnabled: false });
58
+ await expect(disabledFeature.port.list()).rejects.toMatchObject({
59
+ code: "USER_DIRECTORY_FORBIDDEN",
60
+ });
61
+ });
62
+
63
+ test("拒绝越界分页与搜索 Schema", async () => {
64
+ const fixture = await createFixture();
65
+ for (const query of [
66
+ { page: 0 },
67
+ { pageSize: 101 },
68
+ { q: "x".repeat(101) },
69
+ { q: 23 } as never,
70
+ ]) {
71
+ await expect(fixture.port.list(query)).rejects.toBeInstanceOf(
72
+ ModuleUserDirectoryError,
73
+ );
74
+ await expect(fixture.port.list(query)).rejects.toMatchObject({
75
+ code: "USER_DIRECTORY_INVALID_QUERY",
76
+ });
77
+ }
78
+ });
79
+
80
+ test("V1 tenant、custom 与 self 均只读取宿主 nominal ScopeContext", async () => {
81
+ const tenant = await createFixture({ dataScope: "tenant" });
82
+ expect((await tenant.port.list({ q: "办案员" })).items).toHaveLength(2);
83
+
84
+ const custom = await createFixture({ dataScope: "custom" });
85
+ expect((await custom.port.list({ q: "办案员" })).items).toEqual([
86
+ { id: "user_child", displayName: "子部门办案员" },
87
+ ]);
88
+
89
+ const self = await createFixture({ dataScope: "self" });
90
+ expect((await self.port.list({ q: "办案员" })).items).toEqual([
91
+ { id: "user_child", displayName: "子部门办案员" },
92
+ ]);
93
+ });
94
+
95
+ test("缺少宿主 nominal ScopeContext 时拒绝,不回退全量目录", async () => {
96
+ const fixture = await createFixture({ dataScope: "missing" });
97
+ await expect(fixture.port.list()).rejects.toThrow("data-scope-denied");
98
+ });
99
+
100
+ test("生产关键审计不可持久化时不返回目录结果", async () => {
101
+ const fixture = await createFixture({ productionAudit: true });
102
+ await expect(fixture.port.list()).rejects.toThrow(
103
+ "关键审计事件无法持久化或进入恢复队列",
104
+ );
105
+ });
106
+ });
107
+
108
+ interface FixtureOptions {
109
+ permission?: boolean;
110
+ featureEnabled?: boolean;
111
+ dataScope?: DirectoryScope;
112
+ productionAudit?: boolean;
113
+ }
114
+
115
+ type DirectoryScope =
116
+ | "all"
117
+ | "tenant"
118
+ | "department-and-children"
119
+ | "custom"
120
+ | "self"
121
+ | "missing";
122
+
123
+ async function createFixture(options: FixtureOptions = {}) {
124
+ const snapshot = createFoundationSnapshot();
125
+ const repositories = createSystemRepositories(snapshot);
126
+ await seedDirectory(repositories);
127
+ const actor = directoryActor(
128
+ options.permission ?? true,
129
+ options.dataScope ?? "department-and-children",
130
+ );
131
+ const runtime = createServerRuntime(
132
+ options.productionAudit ? { NODE_ENV: "production" } : {},
133
+ {
134
+ featureCatalog: snapshot.features,
135
+ featureResolver: {
136
+ resolve: (key) => ({
137
+ key,
138
+ enabled: options.featureEnabled ?? true,
139
+ visible: "visible",
140
+ }),
141
+ },
142
+ },
143
+ );
144
+ const context: RequestGuardContext = {
145
+ requestId: "request-directory",
146
+ actor,
147
+ startedAt: "2026-07-25T00:00:00.000Z",
148
+ };
149
+ return {
150
+ runtime,
151
+ port: createModuleUserDirectoryPort(
152
+ { repositories, runtime, features: snapshot.features },
153
+ context,
154
+ ),
155
+ };
156
+ }
157
+
158
+ async function seedDirectory(
159
+ repositories: ReturnType<typeof createSystemRepositories>,
160
+ ) {
161
+ const system = directoryActor(true, "all");
162
+ for (const departmentInput of [
163
+ department("dept_root", "业务部门"),
164
+ department("dept_child", "子部门", "dept_root"),
165
+ department("dept_other", "其它部门"),
166
+ ]) {
167
+ await repositories.departments.create(departmentInput, system);
168
+ }
169
+ for (const userInput of [
170
+ user("user_root", "根部门用户", "dept_root"),
171
+ user("user_child", "子部门办案员", "dept_child"),
172
+ user("user_other", "其它办案员", "dept_other"),
173
+ user("user_disabled", "停用办案员", "dept_child", "disabled"),
174
+ ]) {
175
+ await repositories.users.create(userInput, system);
176
+ }
177
+ }
178
+
179
+ function directoryActor(
180
+ permission: boolean,
181
+ scope: DirectoryScope = "department-and-children",
182
+ ): RequestActor {
183
+ const actorId = scope === "self" ? "user_child" : "actor_manager";
184
+ const role =
185
+ scope === "custom"
186
+ ? {
187
+ id: "role",
188
+ code: "role",
189
+ dataScope: "custom" as const,
190
+ departmentIds: ["dept_child"],
191
+ }
192
+ : {
193
+ id: "role",
194
+ code: "role",
195
+ dataScope: scope === "missing" ? ("self" as const) : scope,
196
+ departmentIds: [],
197
+ };
198
+ return {
199
+ id: scope === "all" ? "system_seed" : actorId,
200
+ type: "user",
201
+ name: "部门负责人",
202
+ roleCodes: ["department-manager"],
203
+ permissionKeys: permission ? ["system.user-directory.read"] : [],
204
+ ...(scope === "missing"
205
+ ? {}
206
+ : {
207
+ scopeContext: buildScopeContext(
208
+ scope === "all" ? "system_seed" : actorId,
209
+ scope === "all" || scope === "tenant" ? undefined : "dept_root",
210
+ [role],
211
+ ),
212
+ }),
213
+ };
214
+ }
215
+
216
+ function department(
217
+ id: string,
218
+ name: string,
219
+ parentId?: string,
220
+ ): Partial<ManagedDepartment> {
221
+ return { id, name, code: id, order: 10, status: "active", parentId };
222
+ }
223
+
224
+ function user(
225
+ id: string,
226
+ displayName: string,
227
+ departmentId: string,
228
+ status: ManagedUser["status"] = "active",
229
+ ): Partial<ManagedUser> {
230
+ return {
231
+ id,
232
+ username: id,
233
+ displayName,
234
+ departmentId,
235
+ roleIds: [],
236
+ status,
237
+ };
238
+ }
@@ -0,0 +1,139 @@
1
+ import { randomUUIDv7 } from "bun";
2
+ import {
3
+ ModuleUserDirectoryError,
4
+ type ModuleUserDirectoryPage,
5
+ type ModuleUserDirectoryPort,
6
+ type ModuleUserDirectoryQuery,
7
+ } from "@southwind-ai/server-sdk";
8
+ import type { AuditEvent, FeatureFlagDefinition } from "@southwind-ai/shared";
9
+ import { DataScopedResourceService } from "../data-access/scoped-resource.js";
10
+ import { departmentDescendantResolver } from "../data-access/department-tree.js";
11
+ import { auditActor, type RequestGuardContext } from "../guards.js";
12
+ import { evaluateRouteGuards, findFeature } from "../route-guards.js";
13
+ import type { createServerRuntime } from "../runtime.js";
14
+ import type { SystemRepositories } from "../system/seed.js";
15
+
16
+ const featureKey = "system.user-directory";
17
+ const permissionKey = "system.user-directory.read";
18
+ const auditAction = "system.user-directory.query";
19
+
20
+ type ServerRuntime = ReturnType<typeof createServerRuntime>;
21
+
22
+ export interface ModuleUserDirectoryServices {
23
+ readonly repositories: Pick<SystemRepositories, "users" | "departments">;
24
+ readonly runtime: ServerRuntime;
25
+ readonly features: readonly FeatureFlagDefinition[];
26
+ }
27
+
28
+ export function createModuleUserDirectoryPort(
29
+ services: ModuleUserDirectoryServices,
30
+ context: RequestGuardContext,
31
+ ): ModuleUserDirectoryPort {
32
+ const feature = findFeature([...services.features], featureKey);
33
+ const access = new DataScopedResourceService(
34
+ services.repositories.users,
35
+ departmentDescendantResolver(services.repositories.departments),
36
+ );
37
+ return {
38
+ async list(rawQuery = {}) {
39
+ if (!feature) {
40
+ throw new ModuleUserDirectoryError(
41
+ "USER_DIRECTORY_UNAVAILABLE",
42
+ `宿主未注册 Feature:${featureKey}`,
43
+ );
44
+ }
45
+ const decision = await evaluateRouteGuards(services.runtime, context, {
46
+ permission: {
47
+ method: "GET",
48
+ path: "/internal/module-user-directory",
49
+ permissionKey,
50
+ },
51
+ feature,
52
+ });
53
+ if (!decision.allowed) {
54
+ throw new ModuleUserDirectoryError(
55
+ "USER_DIRECTORY_FORBIDDEN",
56
+ `用户目录访问被拒绝:${decision.reason}`,
57
+ );
58
+ }
59
+ const query = normalizeQuery(rawQuery);
60
+ const page = await access.list(
61
+ {
62
+ page: query.page,
63
+ pageSize: query.pageSize,
64
+ q: query.q,
65
+ status: "active",
66
+ sort: [{ field: "displayName", order: "asc" }],
67
+ },
68
+ ["displayName", "username"],
69
+ context.actor,
70
+ );
71
+ const result: ModuleUserDirectoryPage = {
72
+ items: page.items.map((user) => ({
73
+ id: user.id,
74
+ displayName:
75
+ user.displayName?.trim() || user.username || "未命名用户",
76
+ })),
77
+ total: page.total,
78
+ page: page.page,
79
+ pageSize: page.pageSize,
80
+ };
81
+ await services.runtime.auditSink.recordCriticalEvent(
82
+ queryAuditEvent(context, result),
83
+ );
84
+ return result;
85
+ },
86
+ };
87
+ }
88
+
89
+ function normalizeQuery(
90
+ query: ModuleUserDirectoryQuery,
91
+ ): Required<Pick<ModuleUserDirectoryQuery, "page" | "pageSize">> &
92
+ Pick<ModuleUserDirectoryQuery, "q"> {
93
+ const page = positiveInteger(query.page ?? 1, "page", 10_000);
94
+ const pageSize = positiveInteger(query.pageSize ?? 20, "pageSize", 100);
95
+ if (query.q !== undefined && typeof query.q !== "string") {
96
+ invalidQuery("q 必须是字符串");
97
+ }
98
+ const q = query.q?.trim();
99
+ if (q && q.length > 100) {
100
+ invalidQuery("q 最多 100 个字符");
101
+ }
102
+ return { page, pageSize, ...(q ? { q } : {}) };
103
+ }
104
+
105
+ function positiveInteger(
106
+ value: number,
107
+ field: string,
108
+ maximum: number,
109
+ ): number {
110
+ if (!Number.isInteger(value) || value < 1 || value > maximum) {
111
+ invalidQuery(`${field} 必须是 1-${maximum} 的整数`);
112
+ }
113
+ return value;
114
+ }
115
+
116
+ function invalidQuery(message: string): never {
117
+ throw new ModuleUserDirectoryError("USER_DIRECTORY_INVALID_QUERY", message);
118
+ }
119
+
120
+ function queryAuditEvent(
121
+ context: RequestGuardContext,
122
+ result: ModuleUserDirectoryPage,
123
+ ): AuditEvent {
124
+ return {
125
+ id: randomUUIDv7(),
126
+ action: auditAction,
127
+ outcome: "success",
128
+ actor: auditActor(context),
129
+ target: { type: "system.user-directory", id: "enabled-users" },
130
+ requestId: context.requestId,
131
+ metadata: {
132
+ page: result.page,
133
+ pageSize: result.pageSize,
134
+ resultCount: result.items.length,
135
+ total: result.total,
136
+ },
137
+ occurredAt: new Date().toISOString(),
138
+ };
139
+ }
@@ -33,6 +33,10 @@ export {
33
33
  type ModuleStorageScope,
34
34
  type ModuleStorageServices,
35
35
  } from "./module-host/storage-port.js";
36
+ export {
37
+ createModuleUserDirectoryPort,
38
+ type ModuleUserDirectoryServices,
39
+ } from "./module-host/user-directory-port.js";
36
40
  // @windy-module system.scheduler begin
37
41
  export { createModuleTaskDefinitions } from "./module-host/task-definitions.js";
38
42
  // @windy-module system.scheduler end
@@ -0,0 +1,46 @@
1
+ import type { ExternalNotificationChannel } from "@southwind-ai/shared";
2
+
3
+ export interface NotificationChannelMessage {
4
+ deliveryId: string;
5
+ userId: string;
6
+ address: string;
7
+ subject: string;
8
+ content: string;
9
+ }
10
+
11
+ export interface NotificationChannelResult {
12
+ providerMessageId?: string;
13
+ }
14
+
15
+ export interface NotificationChannelAdapter {
16
+ readonly channel: ExternalNotificationChannel;
17
+ deliver(
18
+ message: NotificationChannelMessage,
19
+ ): Promise<NotificationChannelResult>;
20
+ }
21
+
22
+ export class NotificationChannelError extends Error {
23
+ constructor(
24
+ readonly code: string,
25
+ readonly retryable: boolean,
26
+ message = code,
27
+ ) {
28
+ super(message);
29
+ this.name = "NotificationChannelError";
30
+ }
31
+ }
32
+
33
+ export class NotificationChannelRegistry {
34
+ private readonly adapters = new Map<
35
+ ExternalNotificationChannel,
36
+ NotificationChannelAdapter
37
+ >();
38
+
39
+ constructor(adapters: NotificationChannelAdapter[] = []) {
40
+ for (const adapter of adapters) this.adapters.set(adapter.channel, adapter);
41
+ }
42
+
43
+ get(channel: ExternalNotificationChannel) {
44
+ return this.adapters.get(channel);
45
+ }
46
+ }
@@ -0,0 +1,59 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { mergeExternalDeliveryPolicy } from "./delivery-policy.js";
3
+
4
+ describe("mergeExternalDeliveryPolicy", () => {
5
+ test("未设置偏好时,普通类别只保留站内通知", () => {
6
+ expect(
7
+ mergeExternalDeliveryPolicy({
8
+ category: "运营",
9
+ requestedChannels: ["email", "sms"],
10
+ mandatoryCategories: new Set(),
11
+ }),
12
+ ).toEqual([]);
13
+ });
14
+
15
+ test("普通类别先应用类别退订,再取请求渠道与启用渠道交集", () => {
16
+ const preference = {
17
+ userId: "u1",
18
+ enabledChannels: ["email"] as const,
19
+ disabledCategories: ["营销"],
20
+ };
21
+ expect(
22
+ mergeExternalDeliveryPolicy({
23
+ category: "营销",
24
+ requestedChannels: ["email"],
25
+ preference: {
26
+ ...preference,
27
+ enabledChannels: [...preference.enabledChannels],
28
+ },
29
+ mandatoryCategories: new Set(),
30
+ }),
31
+ ).toEqual([]);
32
+ expect(
33
+ mergeExternalDeliveryPolicy({
34
+ category: "运营",
35
+ requestedChannels: ["email", "sms"],
36
+ preference: {
37
+ ...preference,
38
+ enabledChannels: [...preference.enabledChannels],
39
+ },
40
+ mandatoryCategories: new Set(),
41
+ }),
42
+ ).toEqual(["email"]);
43
+ });
44
+
45
+ test("强制类别绕过用户外部偏好,但不增加未请求渠道", () => {
46
+ expect(
47
+ mergeExternalDeliveryPolicy({
48
+ category: "安全",
49
+ requestedChannels: ["sms", "sms"],
50
+ preference: {
51
+ userId: "u1",
52
+ enabledChannels: [],
53
+ disabledCategories: ["安全"],
54
+ },
55
+ mandatoryCategories: new Set(["安全"]),
56
+ }),
57
+ ).toEqual(["sms"]);
58
+ });
59
+ });
@@ -0,0 +1,37 @@
1
+ import type {
2
+ ExternalNotificationChannel,
3
+ NotificationPreference,
4
+ } from "@southwind-ai/shared";
5
+
6
+ export interface NotificationDeliveryPolicyInput {
7
+ category: string;
8
+ requestedChannels: ExternalNotificationChannel[];
9
+ preference?: NotificationPreference;
10
+ mandatoryCategories: ReadonlySet<string>;
11
+ }
12
+
13
+ /**
14
+ * 站内通知始终可见,此函数只决定外部渠道:
15
+ * 强制类别 > 类别退订 > 渠道启用;未保存偏好时外部渠道默认关闭。
16
+ */
17
+ export function mergeExternalDeliveryPolicy(
18
+ input: NotificationDeliveryPolicyInput,
19
+ ): ExternalNotificationChannel[] {
20
+ if (input.mandatoryCategories.has(input.category)) {
21
+ return unique(input.requestedChannels);
22
+ }
23
+ if (
24
+ !input.preference ||
25
+ input.preference.disabledCategories.includes(input.category)
26
+ ) {
27
+ return [];
28
+ }
29
+ const enabled = new Set(input.preference.enabledChannels);
30
+ return unique(input.requestedChannels).filter((channel) =>
31
+ enabled.has(channel),
32
+ );
33
+ }
34
+
35
+ function unique<T>(values: T[]) {
36
+ return [...new Set(values)];
37
+ }