@_tc/template-core 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (297) hide show
  1. package/.skills/tc-component-usage-skills/reference/component-api.md +27 -0
  2. package/.skills/tc-generator/SKILL.md +1 -1
  3. package/.skills/tc-generator/reference/project-template/ssr/apps/hello/hello.entry.tsx +6 -1
  4. package/.skills/tc-generator/reference/runtime-extensions.md +10 -0
  5. package/AGENT_README.md +8 -2
  6. package/CHANGE.md +27 -0
  7. package/README.md +157 -8
  8. package/cjs/app/ai/agent/agent-scanner.service.js +1 -1
  9. package/cjs/app/ai/agent/agent.module.js +1 -1
  10. package/cjs/app/ai/ai-config.module.js +1 -1
  11. package/cjs/app/ai/ai-config.service.js +1 -1
  12. package/cjs/app/ai/ai.controller.js +1 -1
  13. package/cjs/app/ai/ai.module.js +1 -1
  14. package/cjs/app/ai/guardrail/guardrail.module.js +1 -1
  15. package/cjs/app/ai/guardrail/guardrail.service.js +1 -1
  16. package/cjs/app/ai/index.js +1 -0
  17. package/cjs/app/ai/mcp/internal/read-sse-response.js +2 -0
  18. package/cjs/app/ai/mcp/internal/stdio-process.js +1 -0
  19. package/cjs/app/ai/mcp/mcp-registry.service.js +2 -2
  20. package/cjs/app/ai/mcp/mcp.client.js +2 -3
  21. package/cjs/app/ai/mcp/mcp.loader.js +1 -1
  22. package/cjs/app/ai/mcp/mcp.module.js +1 -1
  23. package/cjs/app/ai/memory/checkpoint.service.js +1 -1
  24. package/cjs/app/ai/memory/memory.module.js +1 -1
  25. package/cjs/app/ai/memory/thread.service.js +1 -1
  26. package/cjs/app/ai/models/chat-sse.js +4 -0
  27. package/cjs/app/ai/models/chat.js +1 -0
  28. package/cjs/app/ai/models/http.js +1 -0
  29. package/cjs/app/ai/models/image.js +1 -0
  30. package/cjs/app/ai/models/video.js +1 -0
  31. package/cjs/app/ai/nest-service-provider.js +1 -0
  32. package/cjs/app/ai/rag/rag.module.js +1 -1
  33. package/cjs/app/ai/rag/retriever.service.js +1 -1
  34. package/cjs/app/ai/retention/conversation-retention.service.js +1 -1
  35. package/cjs/app/ai/runtime/ai-runtime-startup.validator.js +1 -1
  36. package/cjs/app/ai/runtime/ai-runtime.js +1 -0
  37. package/cjs/app/ai/runtime/internal/context-compaction.service.js +3 -3
  38. package/cjs/app/ai/runtime/internal/declaration-snapshot.js +1 -0
  39. package/cjs/app/ai/runtime/internal/owned-runtime-resources.js +1 -0
  40. package/cjs/app/ai/runtime/langchain/langchain-planner.service.js +1 -1
  41. package/cjs/app/ai/runtime/langgraph/langgraph-orchestrator.service.js +1 -1
  42. package/cjs/app/ai/runtime/runtime.module.js +1 -1
  43. package/cjs/app/ai/skill/skill-scanner.service.js +1 -1
  44. package/cjs/app/ai/skill/skill.module.js +1 -1
  45. package/cjs/app/ai/snapshot-data.js +1 -0
  46. package/cjs/app/ai/store/internal/create-vector-store.js +1 -0
  47. package/cjs/app/ai/store/store.module.js +1 -1
  48. package/cjs/app/ai/tool/builtin/index.js +1 -1
  49. package/cjs/app/ai/tool/builtin/invoke-agent.tool.js +1 -1
  50. package/cjs/app/ai/tool/builtin/retrieve.tool.js +1 -1
  51. package/cjs/app/ai/tool/executor.service.js +1 -1
  52. package/cjs/app/ai/tool/tool-registry.service.js +1 -1
  53. package/cjs/app/ai/tool/tool.module.js +1 -1
  54. package/cjs/app/ai/trace/trace.service.js +1 -1
  55. package/cjs/app/csr/controller.js +1 -1
  56. package/cjs/app/csr/types.js +0 -0
  57. package/cjs/app/html/browser-options.js +1 -1
  58. package/cjs/app/html/shell.js +31 -16
  59. package/cjs/app/http/api-signing.js +1 -0
  60. package/cjs/app/http/body-parser.middleware.js +1 -1
  61. package/cjs/app/http/csp-nonce.js +1 -0
  62. package/cjs/app/http/error-response.js +1 -1
  63. package/cjs/app/http/exception-filter.js +1 -1
  64. package/cjs/app/http/request-body-parser-policy.js +1 -0
  65. package/cjs/app/http/security.guard.js +1 -1
  66. package/cjs/app/project/controller.js +1 -1
  67. package/cjs/app/project/service.js +1 -1
  68. package/cjs/app/providers/foundation.js +1 -1
  69. package/cjs/app/ssr/artifacts.js +1 -1
  70. package/cjs/app/ssr/controller.js +1 -1
  71. package/cjs/app/ssr/html.js +17 -17
  72. package/cjs/app/ssr/stream.js +1 -1
  73. package/cjs/app/static-assets.middleware.js +1 -1
  74. package/cjs/app/template-core.module.js +1 -1
  75. package/cjs/app/view/entry.tpl +2 -2
  76. package/cjs/bundler/buildSSR.js +1 -1
  77. package/cjs/bundler/entryCollect.js +1 -1
  78. package/cjs/bundler/feSharedConfig.js +1 -1
  79. package/cjs/bundler/utils.js +3 -3
  80. package/cjs/config/config.default.js +1 -1
  81. package/cjs/packages/common/index.js +1 -1
  82. package/cjs/packages/core/bootstrap.js +1 -1
  83. package/cjs/packages/core/startup.js +1 -1
  84. package/esm/app/ai/agent/agent-scanner.service.js +33 -36
  85. package/esm/app/ai/agent/agent.module.js +9 -7
  86. package/esm/app/ai/ai-config.module.js +14 -7
  87. package/esm/app/ai/ai-config.service.js +24 -20
  88. package/esm/app/ai/ai.controller.js +41 -41
  89. package/esm/app/ai/ai.module.js +15 -15
  90. package/esm/app/ai/guardrail/guardrail.module.js +11 -9
  91. package/esm/app/ai/guardrail/guardrail.service.js +5 -10
  92. package/esm/app/ai/index.js +2 -0
  93. package/esm/app/ai/mcp/internal/read-sse-response.js +35 -0
  94. package/esm/app/ai/mcp/internal/stdio-process.js +59 -0
  95. package/esm/app/ai/mcp/mcp-registry.service.js +10 -15
  96. package/esm/app/ai/mcp/mcp.client.js +77 -78
  97. package/esm/app/ai/mcp/mcp.loader.js +64 -42
  98. package/esm/app/ai/mcp/mcp.module.js +62 -22
  99. package/esm/app/ai/memory/checkpoint.service.js +18 -28
  100. package/esm/app/ai/memory/memory.module.js +15 -12
  101. package/esm/app/ai/memory/thread.service.js +21 -26
  102. package/esm/app/ai/models/chat-sse.js +44 -0
  103. package/esm/app/ai/models/chat.js +60 -0
  104. package/esm/app/ai/models/http.js +52 -0
  105. package/esm/app/ai/models/image.js +37 -0
  106. package/esm/app/ai/models/video.js +32 -0
  107. package/esm/app/ai/nest-service-provider.js +10 -0
  108. package/esm/app/ai/rag/rag.module.js +24 -15
  109. package/esm/app/ai/rag/retriever.service.js +37 -54
  110. package/esm/app/ai/retention/conversation-retention.service.js +16 -34
  111. package/esm/app/ai/runtime/ai-runtime-startup.validator.js +2 -19
  112. package/esm/app/ai/runtime/ai-runtime.js +104 -0
  113. package/esm/app/ai/runtime/internal/context-compaction.service.js +139 -145
  114. package/esm/app/ai/runtime/internal/declaration-snapshot.js +32 -0
  115. package/esm/app/ai/runtime/internal/owned-runtime-resources.js +21 -0
  116. package/esm/app/ai/runtime/langchain/langchain-planner.service.js +43 -49
  117. package/esm/app/ai/runtime/langgraph/langgraph-orchestrator.service.js +273 -225
  118. package/esm/app/ai/runtime/runtime.module.js +56 -35
  119. package/esm/app/ai/skill/skill-scanner.service.js +11 -16
  120. package/esm/app/ai/skill/skill.module.js +11 -9
  121. package/esm/app/ai/snapshot-data.js +24 -0
  122. package/esm/app/ai/store/internal/create-vector-store.js +10 -0
  123. package/esm/app/ai/store/store.module.js +9 -13
  124. package/esm/app/ai/tool/builtin/index.js +16 -4
  125. package/esm/app/ai/tool/builtin/invoke-agent.tool.js +30 -34
  126. package/esm/app/ai/tool/builtin/retrieve.tool.js +9 -13
  127. package/esm/app/ai/tool/executor.service.js +22 -29
  128. package/esm/app/ai/tool/tool-registry.service.js +6 -16
  129. package/esm/app/ai/tool/tool.module.js +26 -12
  130. package/esm/app/ai/trace/trace.service.js +15 -28
  131. package/esm/app/csr/controller.js +114 -63
  132. package/esm/app/csr/types.js +0 -0
  133. package/esm/app/html/browser-options.js +2 -2
  134. package/esm/app/html/shell.js +173 -32
  135. package/esm/app/http/api-signing.js +13 -0
  136. package/esm/app/http/body-parser.middleware.js +47 -27
  137. package/esm/app/http/csp-nonce.js +13 -0
  138. package/esm/app/http/error-response.js +8 -5
  139. package/esm/app/http/exception-filter.js +4 -4
  140. package/esm/app/http/request-body-parser-policy.js +46 -0
  141. package/esm/app/http/security.guard.js +23 -21
  142. package/esm/app/project/controller.js +15 -15
  143. package/esm/app/project/service.js +3 -3
  144. package/esm/app/providers/foundation.js +5 -5
  145. package/esm/app/ssr/artifacts.js +25 -21
  146. package/esm/app/ssr/controller.js +148 -75
  147. package/esm/app/ssr/html.js +49 -44
  148. package/esm/app/ssr/stream.js +25 -24
  149. package/esm/app/static-assets.middleware.js +4 -4
  150. package/esm/app/template-core.module.js +57 -52
  151. package/esm/app/view/entry.tpl +2 -2
  152. package/esm/bundler/buildSSR.js +1 -1
  153. package/esm/bundler/entryCollect.js +9 -9
  154. package/esm/bundler/feSharedConfig.js +42 -42
  155. package/esm/bundler/utils.js +78 -90
  156. package/esm/config/config.default.js +4 -3
  157. package/esm/packages/common/index.js +6 -6
  158. package/esm/packages/core/bootstrap.js +5 -5
  159. package/esm/packages/core/startup.js +69 -46
  160. package/fe/frontend/src/common/request.js +5 -2
  161. package/fe/frontend/src/hooks/useRouterParams.d.ts +4 -2
  162. package/fe/packages/common/ssr/hydration.d.ts +9 -0
  163. package/fe/packages/react/core/Label/Label.d.ts +1 -0
  164. package/fe/packages/react/core/Label/Label.js +3 -1
  165. package/fe/packages/react/core/internal/i18n/default.d.ts +13 -0
  166. package/fe/packages/react/core/internal/i18n/default.js +16 -1
  167. package/fe/packages/react/core/internal/i18n/en-US.d.ts +13 -0
  168. package/fe/packages/react/core/internal/i18n/en-US.js +16 -1
  169. package/fe/packages/react/forms/DataTable/index.d.ts +2 -0
  170. package/fe/packages/react/forms/DataTable/index.js +43 -29
  171. package/fe/packages/react/forms/Form/FormItem.js +39 -12
  172. package/fe/packages/react/forms/MultiSelect/MultiSelect.d.ts +26 -0
  173. package/fe/packages/react/forms/MultiSelect/MultiSelect.js +218 -0
  174. package/fe/packages/react/forms/MultiSelect/index.d.ts +1 -0
  175. package/fe/packages/react/forms/MultiSelect/index.js +2 -0
  176. package/fe/packages/react/forms/Pagination/Pagination.d.ts +6 -1
  177. package/fe/packages/react/forms/Pagination/Pagination.js +13 -4
  178. package/fe/packages/react/forms/Select/Select.d.ts +11 -0
  179. package/fe/packages/react/forms/Select/Select.js +20 -7
  180. package/fe/packages/react/forms/index.d.ts +1 -0
  181. package/fe/packages/react/forms/index.js +3 -1
  182. package/fe/packages/react/i18n/getUiReactLocale.d.ts +26 -0
  183. package/fe/packages/react/index.js +2 -1
  184. package/fe/packages/react/overlays/Drawer/Drawer.d.ts +3 -2
  185. package/fe/packages/react/overlays/Drawer/Drawer.js +16 -2
  186. package/fe/packages/react/overlays/Dropdown/Dropdown.d.ts +12 -2
  187. package/fe/packages/react/overlays/Dropdown/Dropdown.js +27 -14
  188. package/fe/packages/react/overlays/Modal/Modal.d.ts +11 -3
  189. package/fe/packages/react/overlays/Modal/Modal.js +26 -2
  190. package/fe/packages/react/overlays/Modal/ModalManager.d.ts +13 -12
  191. package/fe/packages/react/overlays/Modal/ModalManager.js +15 -5
  192. package/fe/packages/react/overlays/Modal/index.d.ts +1 -0
  193. package/fe/packages/react/overlays/Overlay/Overlay.d.ts +6 -2
  194. package/fe/packages/react/overlays/Overlay/Overlay.js +31 -14
  195. package/fe/packages/react/overlays/Overlay/focusTypes.d.ts +8 -0
  196. package/fe/packages/react/overlays/Overlay/focusTypes.js +0 -0
  197. package/fe/packages/react/overlays/Overlay/index.d.ts +1 -0
  198. package/fe/packages/react/overlays/Overlay/overlayStack.d.ts +16 -0
  199. package/fe/packages/react/overlays/Overlay/overlayStack.js +290 -0
  200. package/fe/packages/react/overlays/Popup/Popup.d.ts +5 -4
  201. package/fe/packages/react/overlays/Popup/Popup.js +28 -11
  202. package/fe/packages/react/ui/components/index.js +2 -1
  203. package/fe/packages/react/ui/index.js +2 -1
  204. package/fe/ssr/components/StreamingRender/StreamingCache.d.ts +1 -0
  205. package/fe/ssr/components/StreamingRender/StreamingCache.js +5 -1
  206. package/fe/ssr/components/StreamingRender/StreamingRender.js +1 -1
  207. package/fe/ssr/components/StreamingRender/StreamingScript.d.ts +1 -0
  208. package/fe/ssr/components/StreamingRender/StreamingScript.js +9 -1
  209. package/fe/ssr/createSSREntry.d.ts +9 -3
  210. package/fe/ssr/createSSREntry.js +43 -19
  211. package/fe/ssr/index.d.ts +4 -1
  212. package/fe/ssr/index.js +2 -1
  213. package/fe/ssr/router.d.ts +76 -0
  214. package/fe/ssr/router.js +349 -0
  215. package/fe/ssr/types.d.ts +1 -1
  216. package/models/packages/common/ssr/hydration.d.ts +9 -0
  217. package/models/packages/react/core/Label/Label.d.ts +1 -0
  218. package/models/packages/react/core/internal/i18n/default.d.ts +13 -0
  219. package/models/packages/react/core/internal/i18n/en-US.d.ts +13 -0
  220. package/models/packages/react/forms/DataTable/index.d.ts +2 -0
  221. package/models/packages/react/forms/MultiSelect/MultiSelect.d.ts +26 -0
  222. package/models/packages/react/forms/MultiSelect/index.d.ts +1 -0
  223. package/models/packages/react/forms/Pagination/Pagination.d.ts +6 -1
  224. package/models/packages/react/forms/Select/Select.d.ts +11 -0
  225. package/models/packages/react/forms/index.d.ts +1 -0
  226. package/models/packages/react/i18n/getUiReactLocale.d.ts +26 -0
  227. package/models/packages/react/overlays/Drawer/Drawer.d.ts +3 -2
  228. package/models/packages/react/overlays/Dropdown/Dropdown.d.ts +12 -2
  229. package/models/packages/react/overlays/Modal/Modal.d.ts +11 -3
  230. package/models/packages/react/overlays/Modal/ModalManager.d.ts +13 -12
  231. package/models/packages/react/overlays/Modal/index.d.ts +1 -0
  232. package/models/packages/react/overlays/Overlay/Overlay.d.ts +6 -2
  233. package/models/packages/react/overlays/Overlay/focusTypes.d.ts +8 -0
  234. package/models/packages/react/overlays/Overlay/index.d.ts +1 -0
  235. package/models/packages/react/overlays/Overlay/overlayStack.d.ts +16 -0
  236. package/models/packages/react/overlays/Popup/Popup.d.ts +5 -4
  237. package/package.json +26 -1
  238. package/types/app/ai/agent/agent-scanner.service.d.ts +4 -4
  239. package/types/app/ai/ai-config.service.d.ts +7 -3
  240. package/types/app/ai/guardrail/guardrail.service.d.ts +2 -2
  241. package/types/app/ai/index.d.ts +9 -0
  242. package/types/app/ai/mcp/internal/load-mcp-config.d.ts +2 -2
  243. package/types/app/ai/mcp/internal/read-sse-response.d.ts +2 -0
  244. package/types/app/ai/mcp/internal/stdio-process.d.ts +5 -0
  245. package/types/app/ai/mcp/mcp-registry.service.d.ts +5 -3
  246. package/types/app/ai/mcp/mcp.client.d.ts +7 -4
  247. package/types/app/ai/mcp/mcp.loader.d.ts +11 -11
  248. package/types/app/ai/mcp/mcp.module.d.ts +5 -5
  249. package/types/app/ai/memory/thread.service.d.ts +3 -3
  250. package/types/app/ai/models/chat-sse.d.ts +5 -0
  251. package/types/app/ai/models/chat.d.ts +22 -0
  252. package/types/app/ai/models/http.d.ts +20 -0
  253. package/types/app/ai/models/image.d.ts +69 -0
  254. package/types/app/ai/models/video.d.ts +99 -0
  255. package/types/app/ai/nest-service-provider.d.ts +6 -0
  256. package/types/app/ai/retention/conversation-retention.service.d.ts +3 -4
  257. package/types/app/ai/runtime/ai-runtime-startup.validator.d.ts +2 -3
  258. package/types/app/ai/runtime/ai-runtime.d.ts +46 -0
  259. package/types/app/ai/runtime/internal/agent-run-context.d.ts +4 -0
  260. package/types/app/ai/runtime/internal/context-compaction.service.d.ts +2 -2
  261. package/types/app/ai/runtime/internal/declaration-snapshot.d.ts +8 -0
  262. package/types/app/ai/runtime/internal/owned-runtime-resources.d.ts +5 -0
  263. package/types/app/ai/runtime/langchain/langchain-planner.service.d.ts +1 -1
  264. package/types/app/ai/runtime/langgraph/langgraph-orchestrator.service.d.ts +33 -16
  265. package/types/app/ai/skill/skill-scanner.service.d.ts +3 -4
  266. package/types/app/ai/snapshot-data.d.ts +1 -0
  267. package/types/app/ai/store/internal/create-vector-store.d.ts +4 -0
  268. package/types/app/ai/tool/builtin/index.d.ts +1 -1
  269. package/types/app/ai/tool/builtin/invoke-agent.tool.d.ts +10 -3
  270. package/types/app/ai/tool/builtin/retrieve.tool.d.ts +4 -3
  271. package/types/app/ai/tool/executor.service.d.ts +3 -3
  272. package/types/app/ai/tool/tool-registry.service.d.ts +2 -7
  273. package/types/app/ai/tool/tool.interface.d.ts +1 -1
  274. package/types/app/ai/tool/tool.module.d.ts +8 -1
  275. package/types/app/ai/trace/trace.service.d.ts +4 -5
  276. package/types/app/csr/types.d.ts +10 -0
  277. package/types/app/html/browser-options.d.ts +1 -1
  278. package/types/app/html/shell.d.ts +37 -10
  279. package/types/app/http/api-signing.d.ts +16 -0
  280. package/types/app/http/body-parser.middleware.d.ts +6 -2
  281. package/types/app/http/csp-nonce.d.ts +3 -0
  282. package/types/app/http/error-response.d.ts +1 -0
  283. package/types/app/http/request-body-parser-policy.d.ts +8 -0
  284. package/types/app/http/security.guard.d.ts +1 -0
  285. package/types/app/ssr/artifacts.d.ts +6 -3
  286. package/types/app/ssr/html.d.ts +7 -0
  287. package/types/app/ssr/stream.d.ts +1 -1
  288. package/types/app/ssr/types.d.ts +56 -0
  289. package/types/bundler/buildSSR.d.ts +2 -1
  290. package/types/bundler/index.d.ts +1 -1
  291. package/types/bundler/state.d.ts +13 -0
  292. package/types/bundler/utils.d.ts +3 -14
  293. package/types/config/config.default.d.ts +9 -3
  294. package/types/index.d.ts +4 -1
  295. package/types/packages/common/ssr/hydration.d.ts +13 -0
  296. package/types/packages/core/index.d.ts +1 -1
  297. package/types/packages/core/startup.d.ts +41 -0
@@ -1,34 +1,41 @@
1
- import { DEFAULT_MCP_TIMEOUT_MS as e, MCP_PROTOCOL_VERSION as t } from "./mcp.types.js";
2
- import { spawn as n } from "child_process";
1
+ import { readSseResponse as e } from "./internal/read-sse-response.js";
2
+ import { spawnStdioProcess as t } from "./internal/stdio-process.js";
3
+ import { DEFAULT_MCP_TIMEOUT_MS as n, MCP_PROTOCOL_VERSION as r } from "./mcp.types.js";
3
4
  //#region app/ai/mcp/mcp.client.ts
4
- function r(e) {
5
+ var i = 1e3;
6
+ function a(e) {
5
7
  return typeof e == "object" && !!e && !Array.isArray(e);
6
8
  }
7
- function i(e) {
8
- return r(e) && typeof e.name == "string" && e.name.trim() !== "" && (e.description === void 0 || typeof e.description == "string") && (e.inputSchema === void 0 || r(e.inputSchema));
9
+ function o(e) {
10
+ return a(e) && typeof e.name == "string" && e.name.trim() !== "" && (e.description === void 0 || typeof e.description == "string") && (e.inputSchema === void 0 || a(e.inputSchema));
9
11
  }
10
- function a(e) {
12
+ function s(e) {
11
13
  return e instanceof Error ? e.message : String(e);
12
14
  }
13
- var o = class {
15
+ var c = class {
14
16
  transport;
15
17
  timeoutMs;
16
18
  pending = /* @__PURE__ */ new Map();
19
+ httpLifetime = new AbortController();
17
20
  nextId = 1;
18
21
  stdoutBuffer = "";
19
- child;
22
+ stdio;
20
23
  sessionId;
21
- closed = !1;
24
+ handshake;
25
+ closing;
22
26
  deadReason;
23
- constructor(t) {
24
- this.config = t, this.transport = t.command ? "stdio" : "http", this.timeoutMs = t.timeoutMs !== void 0 && t.timeoutMs > 0 ? t.timeoutMs : e;
27
+ constructor(e) {
28
+ this.config = e, this.transport = e.command ? "stdio" : "http", this.timeoutMs = e.timeoutMs !== void 0 && e.timeoutMs > 0 ? e.timeoutMs : n;
25
29
  }
26
30
  get name() {
27
31
  return this.config.name;
28
32
  }
29
- async connect() {
33
+ connect() {
34
+ return this.closing ? Promise.reject(/* @__PURE__ */ Error(`MCP client 已关闭:${this.name}`)) : this.deadReason ? Promise.reject(/* @__PURE__ */ Error(`MCP ${this.name} ${this.deadReason}`)) : this.handshake ??= this.initialize();
35
+ }
36
+ async initialize() {
30
37
  this.transport === "stdio" && this.startStdio(), await this.request("initialize", {
31
- protocolVersion: t,
38
+ protocolVersion: r,
32
39
  capabilities: {},
33
40
  clientInfo: {
34
41
  name: "template-core",
@@ -37,8 +44,17 @@ var o = class {
37
44
  }), await this.notify("notifications/initialized");
38
45
  }
39
46
  async listTools() {
40
- let e = await this.request("tools/list", {}), t = r(e) ? e.tools : void 0;
41
- return Array.isArray(t) ? t.filter(i) : [];
47
+ let e = [], t = /* @__PURE__ */ new Set(), n;
48
+ do {
49
+ let r = await this.request("tools/list", n === void 0 ? {} : { cursor: n });
50
+ if (!a(r)) break;
51
+ if (Array.isArray(r.tools)) for (let t of r.tools) o(t) && e.push(t);
52
+ if (n = typeof r.nextCursor == "string" ? r.nextCursor : void 0, n !== void 0) {
53
+ if (t.has(n)) throw Error(`MCP tools/list repeated cursor: ${this.name}`);
54
+ t.add(n);
55
+ }
56
+ } while (n !== void 0);
57
+ return e;
42
58
  }
43
59
  callTool(e, t) {
44
60
  return this.request("tools/call", {
@@ -46,19 +62,11 @@ var o = class {
46
62
  arguments: t ?? {}
47
63
  });
48
64
  }
49
- async close() {
50
- if (!this.closed) {
51
- if (this.closed = !0, this.failAll("连接已关闭"), this.child) {
52
- try {
53
- this.child.stdin.end();
54
- } catch {}
55
- this.child.kill(), this.child = void 0;
56
- }
57
- this.sessionId = void 0, this.stdoutBuffer = "";
58
- }
65
+ close() {
66
+ return this.closing ? this.closing : (this.failAll("连接已关闭"), this.httpLifetime.abort(/* @__PURE__ */ Error(`MCP client 已关闭:${this.name}`)), this.stdoutBuffer = "", this.closing = this.stdio?.close() ?? this.closeHttpSession(), this.closing);
59
67
  }
60
68
  request(e, t) {
61
- if (this.closed) return Promise.reject(/* @__PURE__ */ Error(`MCP client 已关闭,无法调用 ${e}:${this.config.name}`));
69
+ if (this.closing) return Promise.reject(/* @__PURE__ */ Error(`MCP client 已关闭,无法调用 ${e}:${this.config.name}`));
62
70
  if (this.deadReason) return Promise.reject(/* @__PURE__ */ Error(`MCP ${e} 失败:${this.config.name} ${this.deadReason}`));
63
71
  let n = this.nextId++, r = {
64
72
  jsonrpc: "2.0",
@@ -94,28 +102,16 @@ var o = class {
94
102
  await this.postHttp(n);
95
103
  }
96
104
  startStdio() {
97
- let e = n(this.config.command, this.config.args ?? [], {
98
- cwd: this.config.cwd ?? process.cwd(),
99
- env: {
100
- ...process.env,
101
- ...this.config.env ?? {}
102
- },
103
- stdio: [
104
- "pipe",
105
- "pipe",
106
- "pipe"
107
- ]
108
- });
109
- e.stdout.setEncoding("utf8"), e.stdout.on("data", (e) => this.consumeStdout(e)), e.stdin.on("error", (e) => {
110
- console.log(`[ai] mcp ${this.config.name} stdin error: ${a(e)}`);
111
- }), e.stderr.setEncoding("utf8"), e.stderr.on("data", (e) => {
105
+ this.stdio = t(this.config);
106
+ let { child: e } = this.stdio;
107
+ e.stdout.setEncoding("utf8"), e.stdout.on("data", (e) => this.consumeStdout(e)), e.stdout.once("end", () => this.markDead("stdio stdout 已关闭")), e.stdout.on("error", (e) => this.markDead(`stdio stdout - ${e.message}`)), e.stdin.on("error", (e) => this.markDead(`stdio stdin - ${e.message}`)), e.stderr.setEncoding("utf8"), e.stderr.on("data", (e) => {
112
108
  let t = String(e).trim();
113
109
  t !== "" && console.log(`[ai] mcp ${this.config.name} stderr: ${t}`);
114
110
  }), e.on("error", (e) => {
115
- this.markDead(`启动失败 - ${a(e)}`);
111
+ this.markDead(`启动失败 - ${s(e)}`);
116
112
  }), e.on("exit", (e, t) => {
117
113
  this.markDead(`server 已退出(code=${e ?? "-"} signal=${t ?? "-"})`);
118
- }), this.child = e;
114
+ });
119
115
  }
120
116
  consumeStdout(e) {
121
117
  this.stdoutBuffer += e;
@@ -136,43 +132,46 @@ var o = class {
136
132
  this.handleMessage(t);
137
133
  }
138
134
  writeStdio(e) {
139
- if (!this.child || !this.child.stdin.writable) throw Error(`MCP stdio 通道不可写:${this.config.name}`);
140
- this.child.stdin.write(`${JSON.stringify(e)}\n`);
141
- }
142
- async postHttp(e, t) {
143
- let n = await fetch(this.config.url, {
135
+ if (!this.stdio?.child.stdin.writable) throw Error(`MCP stdio 通道不可写:${this.config.name}`);
136
+ this.stdio.child.stdin.write(`${JSON.stringify(e)}\n`);
137
+ }
138
+ httpHeaders() {
139
+ let e = new Headers(this.config.headers);
140
+ return e.has("Content-Type") || e.set("Content-Type", "application/json"), e.has("Accept") || e.set("Accept", "application/json, text/event-stream"), e.set("Connection", "close"), this.sessionId && e.set("Mcp-Session-Id", this.sessionId), e;
141
+ }
142
+ async closeHttpSession() {
143
+ if (!this.sessionId) return;
144
+ let e = this.httpHeaders();
145
+ this.sessionId = void 0;
146
+ let t = await fetch(this.config.url, {
147
+ method: "DELETE",
148
+ headers: e,
149
+ signal: AbortSignal.timeout(Math.min(i, this.timeoutMs))
150
+ });
151
+ if (await t.body?.cancel(), !t.ok && ![
152
+ 404,
153
+ 405,
154
+ 410,
155
+ 501
156
+ ].includes(t.status)) throw Error(`MCP HTTP DELETE 失败:${this.name} ${t.status} ${t.statusText}`);
157
+ }
158
+ async postHttp(t, n) {
159
+ let r = AbortSignal.any([this.httpLifetime.signal, AbortSignal.timeout(this.timeoutMs)]), i = await fetch(this.config.url, {
144
160
  method: "POST",
145
- headers: {
146
- "Content-Type": "application/json",
147
- Accept: "application/json, text/event-stream",
148
- ...this.config.headers ?? {},
149
- ...this.sessionId ? { "mcp-session-id": this.sessionId } : {}
150
- },
151
- body: JSON.stringify(e),
152
- signal: AbortSignal.timeout(this.timeoutMs)
153
- }), r = n.headers.get("mcp-session-id");
154
- if (r && (this.sessionId = r), !n.ok) throw Error(`MCP HTTP ${e.method} 失败:${n.status} ${n.statusText}`);
155
- if (t !== void 0) {
156
- if ((n.headers.get("content-type") ?? "").includes("text/event-stream")) {
157
- for (let e of this.parseSse(await n.text())) this.handleMessage(e);
158
- return;
159
- }
160
- this.handleMessage(await n.json());
161
+ headers: this.httpHeaders(),
162
+ body: JSON.stringify(t),
163
+ signal: r
164
+ }), a = i.headers.get("mcp-session-id");
165
+ if (a && !this.httpLifetime.signal.aborted && t.method === "initialize" && (this.sessionId = a), !i.ok) throw await i.body?.cancel(), Error(`MCP HTTP ${t.method} 失败:${i.status} ${i.statusText}`);
166
+ if (n === void 0) {
167
+ await i.body?.cancel();
168
+ return;
161
169
  }
162
- }
163
- parseSse(e) {
164
- let t = [];
165
- for (let n of e.split("\n")) {
166
- let e = n.trim();
167
- if (!e.startsWith("data:")) continue;
168
- let r = e.slice(5).trim();
169
- if (!(r === "" || r === "[DONE]")) try {
170
- t.push(JSON.parse(r));
171
- } catch {
172
- console.log(`[ai] mcp ${this.config.name} SSE data 非 JSON,已忽略`);
173
- }
170
+ if ((i.headers.get("content-type") ?? "").includes("text/event-stream")) {
171
+ this.settle(n, await e(i, n, r));
172
+ return;
174
173
  }
175
- return t;
174
+ this.handleMessage(await i.json());
176
175
  }
177
176
  handleMessage(e) {
178
177
  let t = e;
@@ -196,4 +195,4 @@ var o = class {
196
195
  }
197
196
  };
198
197
  //#endregion
199
- export { o as McpClient };
198
+ export { c as McpClient };
@@ -1,63 +1,85 @@
1
- import { AiConfigService as e } from "../ai-config.service.js";
2
- import { loadMcpConfig as t } from "./internal/load-mcp-config.js";
3
- import { McpClient as n } from "./mcp.client.js";
4
- import { Injectable as r } from "@nestjs/common";
5
- import i from "@oxc-project/runtime/helpers/decorateMetadata";
6
- import a from "@oxc-project/runtime/helpers/decorate";
1
+ import { loadMcpConfig as e } from "./internal/load-mcp-config.js";
2
+ import { McpClient as t } from "./mcp.client.js";
7
3
  //#region app/ai/mcp/mcp.loader.ts
8
- var o;
9
- function s(e) {
10
- return e instanceof Error ? e.message : String(e);
11
- }
12
- function c(e, t, n) {
4
+ function n(e, t) {
13
5
  return {
14
- name: n.name,
15
- description: n.description ?? `MCP tool from ${e}`,
16
- inputSchema: n.inputSchema ?? {},
6
+ name: t.name,
7
+ description: t.description ?? `MCP tool from ${e.name}`,
8
+ inputSchema: t.inputSchema ?? {},
17
9
  source: "mcp",
18
- execute(e) {
19
- return t.callTool(n.name, e);
20
- }
10
+ execute: (n) => e.callTool(t.name, n)
21
11
  };
22
12
  }
23
- var l = class {
13
+ var r = class {
14
+ clients = [];
24
15
  loaded = [];
16
+ loading;
17
+ closing;
25
18
  constructor(e) {
26
19
  this.configService = e;
27
20
  }
28
21
  loadMcpConfig() {
29
- let e = t(this.configService);
30
- return e.enabled ? e.servers : (e.servers.length > 0 && console.log("[ai] mcp 已配置 servers 但 ai.mcp.enabled 未打开,跳过"), []);
22
+ let t = e(this.configService);
23
+ return t.enabled ? t.servers : (t.servers.length && console.log("[ai] mcp 已配置 servers 但 ai.mcp.enabled 未打开,跳过"), []);
24
+ }
25
+ load({ skipUnavailable: e = !1 } = {}) {
26
+ return this.closing ? Promise.reject(/* @__PURE__ */ Error("[ai] MCP loader is closing")) : this.loading ??= this.openServers(this.clients, e);
31
27
  }
32
- async load() {
33
- let e = this.loadMcpConfig();
34
- for (let t of e) {
35
- let e = new n(t);
28
+ async discover(e, t) {
29
+ let r;
30
+ try {
31
+ await e.connect(), r = await e.listTools();
32
+ } catch (n) {
33
+ let r = n instanceof Error ? n.message : String(n), i = Error(`[ai] mcp server 连接失败:${e.name} - ${r}`, { cause: n });
34
+ if (!t) throw i;
36
35
  try {
37
- await e.connect();
38
- let n = await e.listTools();
39
- this.loaded.push({
40
- name: t.name,
41
- client: e,
42
- tools: n.map((n) => c(t.name, e, n))
43
- });
44
- } catch (n) {
45
- console.log(`[ai] mcp server 连接失败:${t.name} - ${s(n)}`), await e.close().catch(() => void 0);
36
+ await e.close();
37
+ } catch (e) {
38
+ throw AggregateError([i, e], i.message, { cause: i });
46
39
  }
40
+ console.warn(i.message);
41
+ return;
47
42
  }
48
- return this.loaded;
43
+ return {
44
+ name: e.name,
45
+ client: e,
46
+ tools: r.map((t) => n(e, t))
47
+ };
48
+ }
49
+ async openServers(e, n) {
50
+ let r = [];
51
+ try {
52
+ for (let i of this.loadMcpConfig()) {
53
+ let a = new t(i);
54
+ e.push(a);
55
+ let o = await this.discover(a, n);
56
+ if (e !== this.clients) throw Error("MCP loader closed during discovery");
57
+ o ? r.push(o) : e.pop();
58
+ }
59
+ } catch (t) {
60
+ try {
61
+ await (e === this.clients ? this.closeAll() : this.closing);
62
+ } catch (e) {
63
+ throw AggregateError([t, e], "[ai] MCP startup and rollback failed", { cause: t });
64
+ }
65
+ throw t;
66
+ }
67
+ return this.loaded = r, r;
49
68
  }
50
69
  get connections() {
51
70
  return this.loaded;
52
71
  }
53
- async closeAll() {
54
- let e = this.loaded;
55
- this.loaded = [];
56
- for (let t of e) await t.client.close().catch((e) => {
57
- console.log(`[ai] mcp client 关闭失败:${t.name} - ${s(e)}`);
58
- });
72
+ closeAll() {
73
+ if (this.closing) return this.closing;
74
+ let e = this.clients;
75
+ return this.clients = [], this.loaded = [], this.closing = Promise.allSettled(e.map((e) => e.close())).then((e) => {
76
+ let t = e.flatMap((e) => e.status === "rejected" ? [e.reason] : []);
77
+ if (t.length === 1) throw t[0];
78
+ if (t.length > 1) throw AggregateError(t, "[ai] MCP clients failed to close");
79
+ }).finally(() => {
80
+ this.loading = void 0, this.closing = void 0;
81
+ }), this.closing;
59
82
  }
60
83
  };
61
- l = a([r(), i("design:paramtypes", [typeof (o = e !== void 0 && e) == "function" ? o : Object])], l);
62
84
  //#endregion
63
- export { l as McpLoader };
85
+ export { r as McpLoader };
@@ -1,30 +1,70 @@
1
- import { ToolRegistryService as e } from "../tool/tool-registry.service.js";
2
- import { ToolModule as t } from "../tool/tool.module.js";
3
- import { McpLoader as n } from "./mcp.loader.js";
4
- import { McpRegistryService as r } from "./mcp-registry.service.js";
5
- import { Module as i } from "@nestjs/common";
6
- import a from "@oxc-project/runtime/helpers/decorateMetadata";
7
- import o from "@oxc-project/runtime/helpers/decorate";
1
+ import { AiConfigService as e } from "../ai-config.service.js";
2
+ import { LangGraphOrchestratorService as t } from "../runtime/langgraph/langgraph-orchestrator.service.js";
3
+ import { ToolRegistryService as n } from "../tool/tool-registry.service.js";
4
+ import { ToolModule as r } from "../tool/tool.module.js";
5
+ import { RuntimeModule as i } from "../runtime/runtime.module.js";
6
+ import { McpRegistryService as a } from "./mcp-registry.service.js";
7
+ import { McpLoader as o } from "./mcp.loader.js";
8
+ import { Inject as s, Module as c } from "@nestjs/common";
9
+ import l from "@oxc-project/runtime/helpers/decorate";
10
+ import u from "@oxc-project/runtime/helpers/decorateMetadata";
11
+ import d from "@oxc-project/runtime/helpers/decorateParam";
8
12
  //#region app/ai/mcp/mcp.module.ts
9
- var s, c, l, u = class {
10
- constructor(e, t, n) {
11
- this.loader = e, this.registry = t, this.toolRegistry = n;
13
+ var f, p, m, h, g = class {
14
+ constructor(e, t, n, r) {
15
+ this.loader = e, this.registry = t, this.toolRegistry = n, this.orchestrator = r;
12
16
  }
13
17
  async onModuleInit() {
14
- await this.loader.load(), this.registry.registerAll(this.toolRegistry);
18
+ await this.loader.load({ skipUnavailable: !0 });
19
+ try {
20
+ this.registry.registerAll(this.toolRegistry);
21
+ } catch (e) {
22
+ try {
23
+ await this.loader.closeAll();
24
+ } catch (t) {
25
+ throw AggregateError([e, t], "[ai] MCP registration and cleanup failed", { cause: e });
26
+ }
27
+ throw e;
28
+ }
15
29
  }
16
30
  async onModuleDestroy() {
17
- await this.loader.closeAll();
31
+ let e = [];
32
+ try {
33
+ await this.orchestrator.close();
34
+ } catch (t) {
35
+ e.push(t);
36
+ }
37
+ try {
38
+ await this.loader.closeAll();
39
+ } catch (t) {
40
+ e.push(t);
41
+ }
42
+ if (e.length === 1) throw e[0];
43
+ if (e.length > 1) throw AggregateError(e, "[ai] AI module resources failed to close");
18
44
  }
19
45
  };
20
- u = o([i({
21
- imports: [t],
22
- providers: [n, r],
23
- exports: [n, r]
24
- }), a("design:paramtypes", [
25
- typeof (s = n !== void 0 && n) == "function" ? s : Object,
26
- typeof (c = r !== void 0 && r) == "function" ? c : Object,
27
- typeof (l = e !== void 0 && e) == "function" ? l : Object
28
- ])], u);
46
+ g = l([
47
+ c({
48
+ imports: [r, i],
49
+ providers: [{
50
+ provide: o,
51
+ inject: [e],
52
+ useFactory: (e) => new o(e)
53
+ }, {
54
+ provide: a,
55
+ inject: [o],
56
+ useFactory: (e) => new a(e)
57
+ }],
58
+ exports: [o, a]
59
+ }),
60
+ d(2, s(n)),
61
+ d(3, s(t)),
62
+ u("design:paramtypes", [
63
+ typeof (f = o !== void 0 && o) == "function" ? f : Object,
64
+ typeof (p = a !== void 0 && a) == "function" ? p : Object,
65
+ typeof (m = typeof Pick < "u" && Pick) == "function" ? m : Object,
66
+ typeof (h = typeof Pick < "u" && Pick) == "function" ? h : Object
67
+ ])
68
+ ], g);
29
69
  //#endregion
30
- export { u as McpModule };
70
+ export { g as McpModule };
@@ -1,10 +1,5 @@
1
- import { THREAD_STATE_STORE as e } from "../store/store.interface.js";
2
- import { Inject as t, Injectable as n } from "@nestjs/common";
3
- import r from "@oxc-project/runtime/helpers/decorateMetadata";
4
- import i from "@oxc-project/runtime/helpers/decorate";
5
- import a from "@oxc-project/runtime/helpers/decorateParam";
6
1
  //#region app/ai/memory/checkpoint.service.ts
7
- var o = 1, s = class {
2
+ var e = 1, t = class {
8
3
  constructor(e) {
9
4
  this.store = e;
10
5
  }
@@ -30,18 +25,18 @@ var o = 1, s = class {
30
25
  return this.store.commitThreadMigration(e.threadId, t, n, r, e);
31
26
  }
32
27
  async save(e) {
33
- let t = c(e);
28
+ let t = n(e);
34
29
  if (!await this.compareAndSet(t, e.revision ?? 0)) throw Error(`[ai] checkpoint revision conflict for ${e.threadId}`);
35
30
  }
36
- createInitialCheckpoint(e, t, n) {
31
+ createInitialCheckpoint(t, n, r) {
37
32
  return {
38
- threadId: e,
39
- schemaVersion: o,
40
- agentId: t,
33
+ threadId: t,
34
+ schemaVersion: e,
35
+ agentId: n,
41
36
  pinnedContext: {
42
- initialGoal: n,
37
+ initialGoal: r,
43
38
  hardConstraints: [],
44
- latestUserDirection: n
39
+ latestUserDirection: r
45
40
  },
46
41
  rollingSummary: {
47
42
  goal: null,
@@ -57,22 +52,17 @@ var o = 1, s = class {
57
52
  };
58
53
  }
59
54
  };
60
- s = i([
61
- n(),
62
- a(0, t(e)),
63
- r("design:paramtypes", [Object])
64
- ], s);
65
- function c(e) {
55
+ function n(t) {
66
56
  return {
67
- ...e,
68
- schemaVersion: e.schemaVersion ?? o,
69
- agentId: e.agentId ?? "",
70
- pinnedContext: e.pinnedContext ?? {
57
+ ...t,
58
+ schemaVersion: t.schemaVersion ?? e,
59
+ agentId: t.agentId ?? "",
60
+ pinnedContext: t.pinnedContext ?? {
71
61
  initialGoal: "",
72
62
  hardConstraints: [],
73
63
  latestUserDirection: ""
74
64
  },
75
- rollingSummary: e.rollingSummary ?? {
65
+ rollingSummary: t.rollingSummary ?? {
76
66
  goal: null,
77
67
  constraints: [],
78
68
  facts: [],
@@ -80,10 +70,10 @@ function c(e) {
80
70
  progress: [],
81
71
  openWork: []
82
72
  },
83
- summarizedThroughSeq: e.summarizedThroughSeq ?? 0,
84
- revision: e.revision ?? 0,
85
- updatedAt: e.updatedAt ?? Date.now()
73
+ summarizedThroughSeq: t.summarizedThroughSeq ?? 0,
74
+ revision: t.revision ?? 0,
75
+ updatedAt: t.updatedAt ?? Date.now()
86
76
  };
87
77
  }
88
78
  //#endregion
89
- export { s as CheckpointService };
79
+ export { t as CheckpointService };
@@ -1,14 +1,17 @@
1
- import { CheckpointService as e } from "./checkpoint.service.js";
2
- import { ThreadService as t } from "./thread.service.js";
3
- import { StoreModule as n } from "../store/store.module.js";
4
- import { Module as r } from "@nestjs/common";
5
- import i from "@oxc-project/runtime/helpers/decorate";
1
+ import { AiConfigService as e } from "../ai-config.service.js";
2
+ import { provideAiService as t } from "../nest-service-provider.js";
3
+ import { CheckpointService as n } from "./checkpoint.service.js";
4
+ import { THREAD_STATE_STORE as r } from "../store/store.interface.js";
5
+ import { StoreModule as i } from "../store/store.module.js";
6
+ import { ThreadService as a } from "./thread.service.js";
7
+ import { Module as o } from "@nestjs/common";
8
+ import s from "@oxc-project/runtime/helpers/decorate";
6
9
  //#region app/ai/memory/memory.module.ts
7
- var a = class {};
8
- a = i([r({
9
- imports: [n],
10
- providers: [t, e],
11
- exports: [t, e]
12
- })], a);
10
+ var c = class {};
11
+ c = s([o({
12
+ imports: [i],
13
+ providers: [t(a, e), t(n, r)],
14
+ exports: [a, n]
15
+ })], c);
13
16
  //#endregion
14
- export { a as MemoryModule };
17
+ export { c as MemoryModule };
@@ -1,16 +1,12 @@
1
- import { AiConfigService as e } from "../ai-config.service.js";
2
- import { resolveMemoryMaxTurns as t } from "./internal/load-memory-config.js";
3
- import { Injectable as n } from "@nestjs/common";
4
- import r from "@oxc-project/runtime/helpers/decorateMetadata";
5
- import i from "@oxc-project/runtime/helpers/decorate";
6
- import { randomUUID as a } from "crypto";
1
+ import { resolveMemoryMaxTurns as e } from "./internal/load-memory-config.js";
2
+ import { randomUUID as t } from "crypto";
7
3
  //#region app/ai/memory/thread.service.ts
8
- var o, s = class {
4
+ var n = class {
9
5
  constructor(e) {
10
6
  this.configService = e;
11
7
  }
12
8
  createThread() {
13
- return a();
9
+ return t();
14
10
  }
15
11
  ensureThread(e) {
16
12
  let t = typeof e == "string" ? e.trim() : "";
@@ -23,7 +19,7 @@ var o, s = class {
23
19
  })) : [];
24
20
  }
25
21
  selectRecentCompleteTurns(e, t = {}) {
26
- return c(e, t);
22
+ return r(e, t);
27
23
  }
28
24
  truncate(e, t) {
29
25
  let n = this.resolveLimit(t);
@@ -46,35 +42,34 @@ var o, s = class {
46
42
  updatedAt: Date.now()
47
43
  };
48
44
  }
49
- resolveLimit(e) {
50
- return typeof e == "number" && Number.isFinite(e) && e > 0 ? Math.floor(e) : t(this.configService);
45
+ resolveLimit(t) {
46
+ return typeof t == "number" && Number.isFinite(t) && t > 0 ? Math.floor(t) : e(this.configService);
51
47
  }
52
48
  };
53
- s = i([n(), r("design:paramtypes", [typeof (o = e !== void 0 && e) == "function" ? o : Object])], s);
54
- async function c(e, t = {}) {
55
- let n = [...e].sort((e, t) => e.seq - t.seq), r = t.maxTokens, i = [];
49
+ async function r(e, t = {}) {
50
+ let n = [...e].sort((e, t) => e.seq - t.seq), r = t.maxTokens, a = [];
56
51
  for (let e = n.length - 1; e > 0;) {
57
- let a = n[e], o = n[e - 1];
58
- if (!a || !o) break;
59
- if (a.role !== "assistant" || o.role !== "user" || a.seq !== o.seq + 1) {
52
+ let o = n[e], s = n[e - 1];
53
+ if (!o || !s) break;
54
+ if (o.role !== "assistant" || s.role !== "user" || o.seq !== s.seq + 1) {
60
55
  --e;
61
56
  continue;
62
57
  }
63
- let s = [
58
+ let c = [
59
+ s,
64
60
  o,
65
- a,
66
- ...i
67
- ], c = t.countTokens ? await t.countTokens(s) : l(s);
68
- if (r !== void 0 && c > r) {
61
+ ...a
62
+ ], l = t.countTokens ? await t.countTokens(c) : i(c);
63
+ if (r !== void 0 && l > r) {
69
64
  e -= 2;
70
65
  continue;
71
66
  }
72
- i.unshift(o, a), e -= 2;
67
+ a.unshift(s, o), e -= 2;
73
68
  }
74
- return i;
69
+ return a;
75
70
  }
76
- function l(e) {
71
+ function i(e) {
77
72
  return e.reduce((e, t) => e + Math.max(1, Math.ceil(Buffer.byteLength(t.content, "utf8") / 4)), 0);
78
73
  }
79
74
  //#endregion
80
- export { s as ThreadService, c as selectRecentCompleteTurns };
75
+ export { n as ThreadService, r as selectRecentCompleteTurns };