@downcity/plugins 1.0.174 → 1.0.194

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 (301) hide show
  1. package/bin/BuiltinPlugins.d.ts +4 -9
  2. package/bin/BuiltinPlugins.d.ts.map +1 -1
  3. package/bin/BuiltinPlugins.js +4 -8
  4. package/bin/BuiltinPlugins.js.map +1 -1
  5. package/bin/chat/Action.d.ts +8 -2
  6. package/bin/chat/Action.d.ts.map +1 -1
  7. package/bin/chat/Action.js +9 -5
  8. package/bin/chat/Action.js.map +1 -1
  9. package/bin/chat/ChatPlugin.d.ts +2 -22
  10. package/bin/chat/ChatPlugin.d.ts.map +1 -1
  11. package/bin/chat/ChatPlugin.js +2 -21
  12. package/bin/chat/ChatPlugin.js.map +1 -1
  13. package/bin/chat/Index.d.ts +1 -1
  14. package/bin/chat/Index.d.ts.map +1 -1
  15. package/bin/chat/channels/BaseChatChannel.js +1 -1
  16. package/bin/chat/channels/BaseChatChannel.js.map +1 -1
  17. package/bin/chat/channels/Configuration.d.ts +1 -1
  18. package/bin/chat/channels/Configuration.js +1 -1
  19. package/bin/chat/channels/RuntimeChannel.d.ts +2 -3
  20. package/bin/chat/channels/RuntimeChannel.d.ts.map +1 -1
  21. package/bin/chat/channels/RuntimeChannel.js +0 -8
  22. package/bin/chat/channels/RuntimeChannel.js.map +1 -1
  23. package/bin/chat/channels/feishu/Feishu.d.ts.map +1 -1
  24. package/bin/chat/channels/feishu/Feishu.js +2 -1
  25. package/bin/chat/channels/feishu/Feishu.js.map +1 -1
  26. package/bin/chat/runtime/ChannelContextStore.d.ts.map +1 -1
  27. package/bin/chat/runtime/ChannelContextStore.js +13 -12
  28. package/bin/chat/runtime/ChannelContextStore.js.map +1 -1
  29. package/bin/chat/runtime/ChatActionExecution.d.ts +26 -1
  30. package/bin/chat/runtime/ChatActionExecution.d.ts.map +1 -1
  31. package/bin/chat/runtime/ChatActionExecution.js +31 -3
  32. package/bin/chat/runtime/ChatActionExecution.js.map +1 -1
  33. package/bin/chat/runtime/ChatActionInput.d.ts +1 -2
  34. package/bin/chat/runtime/ChatActionInput.d.ts.map +1 -1
  35. package/bin/chat/runtime/ChatActionInput.js +1 -27
  36. package/bin/chat/runtime/ChatActionInput.js.map +1 -1
  37. package/bin/chat/runtime/ChatChannelActions.d.ts +2 -66
  38. package/bin/chat/runtime/ChatChannelActions.d.ts.map +1 -1
  39. package/bin/chat/runtime/ChatChannelActions.js +3 -195
  40. package/bin/chat/runtime/ChatChannelActions.js.map +1 -1
  41. package/bin/chat/runtime/ChatChannelConfig.d.ts +2 -23
  42. package/bin/chat/runtime/ChatChannelConfig.d.ts.map +1 -1
  43. package/bin/chat/runtime/ChatChannelConfig.js +3 -116
  44. package/bin/chat/runtime/ChatChannelConfig.js.map +1 -1
  45. package/bin/chat/runtime/ChatChannelCore.d.ts +0 -14
  46. package/bin/chat/runtime/ChatChannelCore.d.ts.map +1 -1
  47. package/bin/chat/runtime/ChatChannelCore.js +1 -1
  48. package/bin/chat/runtime/ChatChannelCore.js.map +1 -1
  49. package/bin/chat/runtime/ChatChannelFacade.d.ts +1 -1
  50. package/bin/chat/runtime/ChatChannelFacade.d.ts.map +1 -1
  51. package/bin/chat/runtime/ChatChannelFacade.js +1 -1
  52. package/bin/chat/runtime/ChatChannelFacade.js.map +1 -1
  53. package/bin/chat/runtime/ChatHistoryStore.d.ts.map +1 -1
  54. package/bin/chat/runtime/ChatHistoryStore.js +4 -3
  55. package/bin/chat/runtime/ChatHistoryStore.js.map +1 -1
  56. package/bin/chat/runtime/ChatPluginActionRegistry.d.ts.map +1 -1
  57. package/bin/chat/runtime/ChatPluginActionRegistry.js +27 -89
  58. package/bin/chat/runtime/ChatPluginActionRegistry.js.map +1 -1
  59. package/bin/chat/runtime/ChatPluginSystem.d.ts +3 -2
  60. package/bin/chat/runtime/ChatPluginSystem.d.ts.map +1 -1
  61. package/bin/chat/runtime/ChatPluginSystem.js +5 -5
  62. package/bin/chat/runtime/ChatPluginSystem.js.map +1 -1
  63. package/bin/chat/runtime/ChatQueueStore.d.ts +1 -1
  64. package/bin/chat/runtime/ChatQueueStore.js +2 -2
  65. package/bin/chat/runtime/ChatQueueStore.js.map +1 -1
  66. package/bin/chat/runtime/ChatQueueWorker.js +1 -1
  67. package/bin/chat/runtime/ChatQueueWorker.js.map +1 -1
  68. package/bin/chat/runtime/ChatSessionDelete.d.ts.map +1 -1
  69. package/bin/chat/runtime/ChatSessionDelete.js +11 -20
  70. package/bin/chat/runtime/ChatSessionDelete.js.map +1 -1
  71. package/bin/chat/runtime/ChatStorage.d.ts +32 -0
  72. package/bin/chat/runtime/ChatStorage.d.ts.map +1 -0
  73. package/bin/chat/runtime/ChatStorage.js +158 -0
  74. package/bin/chat/runtime/ChatStorage.js.map +1 -0
  75. package/bin/chat/runtime/SystemPrompt.d.ts +3 -2
  76. package/bin/chat/runtime/SystemPrompt.d.ts.map +1 -1
  77. package/bin/chat/runtime/SystemPrompt.js +4 -5
  78. package/bin/chat/runtime/SystemPrompt.js.map +1 -1
  79. package/bin/chat/types/ChannelConfiguration.d.ts +2 -2
  80. package/bin/chat/types/ChatPluginActionPayload.d.ts +7 -45
  81. package/bin/chat/types/ChatPluginActionPayload.d.ts.map +1 -1
  82. package/bin/chat/types/ChatPluginOptions.d.ts +0 -21
  83. package/bin/chat/types/ChatPluginOptions.d.ts.map +1 -1
  84. package/bin/chat/types/ChatStorage.d.ts +36 -0
  85. package/bin/chat/types/ChatStorage.d.ts.map +1 -0
  86. package/bin/chat/types/ChatStorage.js +9 -0
  87. package/bin/chat/types/ChatStorage.js.map +1 -0
  88. package/bin/contact/Action.d.ts +3 -2
  89. package/bin/contact/Action.d.ts.map +1 -1
  90. package/bin/contact/Action.js +2 -2
  91. package/bin/contact/Action.js.map +1 -1
  92. package/bin/contact/ContactPlugin.d.ts.map +1 -1
  93. package/bin/contact/ContactPlugin.js +17 -14
  94. package/bin/contact/ContactPlugin.js.map +1 -1
  95. package/bin/contact/runtime/ChatRuntime.js +1 -1
  96. package/bin/contact/runtime/ChatRuntime.js.map +1 -1
  97. package/bin/contact/types/ContactPluginOptions.d.ts +4 -0
  98. package/bin/contact/types/ContactPluginOptions.d.ts.map +1 -1
  99. package/bin/index.d.ts +5 -5
  100. package/bin/index.d.ts.map +1 -1
  101. package/bin/index.js +2 -2
  102. package/bin/index.js.map +1 -1
  103. package/bin/memory/Action.d.ts.map +1 -1
  104. package/bin/memory/Action.js +16 -4
  105. package/bin/memory/Action.js.map +1 -1
  106. package/bin/memory/runtime/Store.d.ts.map +1 -1
  107. package/bin/memory/runtime/Store.js +0 -22
  108. package/bin/memory/runtime/Store.js.map +1 -1
  109. package/bin/memory/runtime/Writer.d.ts +1 -1
  110. package/bin/memory/runtime/Writer.d.ts.map +1 -1
  111. package/bin/memory/runtime/Writer.js +2 -4
  112. package/bin/memory/runtime/Writer.js.map +1 -1
  113. package/bin/skill/Command.d.ts +3 -11
  114. package/bin/skill/Command.d.ts.map +1 -1
  115. package/bin/skill/Command.js +3 -31
  116. package/bin/skill/Command.js.map +1 -1
  117. package/bin/skill/PROMPT.d.ts +1 -1
  118. package/bin/skill/PROMPT.d.ts.map +1 -1
  119. package/bin/skill/PROMPT.js +1 -1
  120. package/bin/skill/PROMPT.js.map +1 -1
  121. package/bin/skill/Plugin.d.ts +3 -2
  122. package/bin/skill/Plugin.d.ts.map +1 -1
  123. package/bin/skill/Plugin.js +37 -129
  124. package/bin/skill/Plugin.js.map +1 -1
  125. package/bin/skill/runtime/Prompt.d.ts +11 -2
  126. package/bin/skill/runtime/Prompt.d.ts.map +1 -1
  127. package/bin/skill/runtime/Prompt.js +93 -8
  128. package/bin/skill/runtime/Prompt.js.map +1 -1
  129. package/bin/skill/runtime/SystemProvider.d.ts +2 -1
  130. package/bin/skill/runtime/SystemProvider.d.ts.map +1 -1
  131. package/bin/skill/runtime/SystemProvider.js +4 -9
  132. package/bin/skill/runtime/SystemProvider.js.map +1 -1
  133. package/bin/skill/types/SkillPlugin.d.ts +6 -18
  134. package/bin/skill/types/SkillPlugin.d.ts.map +1 -1
  135. package/bin/skill/types/SkillPlugin.js +2 -2
  136. package/bin/sound/Plugin.d.ts +69 -0
  137. package/bin/sound/Plugin.d.ts.map +1 -0
  138. package/bin/sound/Plugin.js +623 -0
  139. package/bin/sound/Plugin.js.map +1 -0
  140. package/bin/sound/types/SoundPlugin.d.ts +150 -0
  141. package/bin/sound/types/SoundPlugin.d.ts.map +1 -0
  142. package/bin/sound/types/SoundPlugin.js +10 -0
  143. package/bin/sound/types/SoundPlugin.js.map +1 -0
  144. package/bin/task/Action.d.ts +2 -0
  145. package/bin/task/Action.d.ts.map +1 -1
  146. package/bin/task/Action.js +6 -0
  147. package/bin/task/Action.js.map +1 -1
  148. package/bin/task/runtime/Runner.d.ts +4 -0
  149. package/bin/task/runtime/Runner.d.ts.map +1 -1
  150. package/bin/task/runtime/Runner.js +15 -11
  151. package/bin/task/runtime/Runner.js.map +1 -1
  152. package/bin/task/runtime/TaskActionExecution.d.ts +2 -0
  153. package/bin/task/runtime/TaskActionExecution.d.ts.map +1 -1
  154. package/bin/task/runtime/TaskActionExecution.js +1 -0
  155. package/bin/task/runtime/TaskActionExecution.js.map +1 -1
  156. package/bin/task/runtime/TaskPluginActionRegistry.d.ts.map +1 -1
  157. package/bin/task/runtime/TaskPluginActionRegistry.js +1 -0
  158. package/bin/task/runtime/TaskPluginActionRegistry.js.map +1 -1
  159. package/bin/task/runtime/TaskRunChatDispatch.js +1 -1
  160. package/bin/task/runtime/TaskRunChatDispatch.js.map +1 -1
  161. package/bin/task/runtime/TaskRunnerRound.d.ts.map +1 -1
  162. package/bin/task/runtime/TaskRunnerRound.js +19 -26
  163. package/bin/task/runtime/TaskRunnerRound.js.map +1 -1
  164. package/bin/task/runtime/TaskRunnerSession.d.ts +6 -2
  165. package/bin/task/runtime/TaskRunnerSession.d.ts.map +1 -1
  166. package/bin/task/runtime/TaskRunnerSession.js +93 -72
  167. package/bin/task/runtime/TaskRunnerSession.js.map +1 -1
  168. package/bin/task/runtime/TaskRunnerTypes.d.ts +4 -4
  169. package/bin/task/runtime/TaskRunnerTypes.d.ts.map +1 -1
  170. package/bin/task/types/TaskPluginOptions.d.ts.map +1 -1
  171. package/bin/web/PROMPT.d.ts +1 -1
  172. package/bin/web/PROMPT.d.ts.map +1 -1
  173. package/bin/web/PROMPT.js +2 -2
  174. package/bin/web/PROMPT.js.map +1 -1
  175. package/bin/web/Plugin.d.ts +1 -21
  176. package/bin/web/Plugin.d.ts.map +1 -1
  177. package/bin/web/Plugin.js +14 -79
  178. package/bin/web/Plugin.js.map +1 -1
  179. package/bin/web/runtime/Prompt.d.ts +14 -0
  180. package/bin/web/runtime/Prompt.d.ts.map +1 -0
  181. package/bin/web/runtime/Prompt.js +77 -0
  182. package/bin/web/runtime/Prompt.js.map +1 -0
  183. package/bin/web/types/WebPlugin.d.ts +40 -29
  184. package/bin/web/types/WebPlugin.d.ts.map +1 -1
  185. package/bin/web/types/WebPlugin.js +11 -3
  186. package/bin/web/types/WebPlugin.js.map +1 -1
  187. package/bin/workboard/runtime/Collector.js +1 -1
  188. package/bin/workboard/runtime/Collector.js.map +1 -1
  189. package/bin/workboard/runtime/Normalizer.js +1 -1
  190. package/bin/workboard/runtime/Normalizer.js.map +1 -1
  191. package/bin/workboard/runtime/SessionSummary.d.ts.map +1 -1
  192. package/bin/workboard/runtime/SessionSummary.js +11 -38
  193. package/bin/workboard/runtime/SessionSummary.js.map +1 -1
  194. package/package.json +8 -4
  195. package/scripts/chat-access.test.mjs +1 -1
  196. package/scripts/chat-channel-config.test.mjs +15 -64
  197. package/scripts/chat-storage.test.mjs +92 -0
  198. package/scripts/skill-plugin.test.mjs +93 -0
  199. package/scripts/sound-plugin.test.mjs +250 -0
  200. package/scripts/web-plugin.test.mjs +85 -0
  201. package/src/BuiltinPlugins.ts +7 -17
  202. package/src/chat/Action.ts +16 -5
  203. package/src/chat/ChatPlugin.ts +6 -35
  204. package/src/chat/Index.ts +0 -1
  205. package/src/chat/channels/BaseChatChannel.ts +1 -1
  206. package/src/chat/channels/Configuration.ts +1 -1
  207. package/src/chat/channels/RuntimeChannel.ts +2 -14
  208. package/src/chat/channels/feishu/Feishu.ts +2 -1
  209. package/src/chat/runtime/ChannelContextStore.ts +16 -12
  210. package/src/chat/runtime/ChatActionExecution.ts +48 -3
  211. package/src/chat/runtime/ChatActionInput.ts +0 -31
  212. package/src/chat/runtime/ChatChannelActions.ts +3 -254
  213. package/src/chat/runtime/ChatChannelConfig.ts +4 -133
  214. package/src/chat/runtime/ChatChannelCore.ts +1 -15
  215. package/src/chat/runtime/ChatChannelFacade.ts +0 -4
  216. package/src/chat/runtime/ChatHistoryStore.ts +4 -3
  217. package/src/chat/runtime/ChatPluginActionRegistry.ts +26 -98
  218. package/src/chat/runtime/ChatPluginSystem.ts +9 -3
  219. package/src/chat/runtime/ChatQueueStore.ts +2 -2
  220. package/src/chat/runtime/ChatQueueWorker.ts +1 -1
  221. package/src/chat/runtime/ChatSessionDelete.ts +11 -21
  222. package/src/chat/runtime/ChatStorage.ts +203 -0
  223. package/src/chat/runtime/SystemPrompt.ts +8 -3
  224. package/src/chat/types/ChannelConfiguration.ts +2 -2
  225. package/src/chat/types/ChatPluginActionPayload.ts +8 -49
  226. package/src/chat/types/ChatPluginOptions.ts +0 -22
  227. package/src/chat/types/ChatStorage.ts +37 -0
  228. package/src/contact/Action.ts +9 -1
  229. package/src/contact/ContactPlugin.ts +38 -14
  230. package/src/contact/runtime/ChatRuntime.ts +1 -1
  231. package/src/contact/types/ContactPluginOptions.ts +6 -0
  232. package/src/index.ts +16 -14
  233. package/src/memory/Action.ts +19 -4
  234. package/src/memory/runtime/Store.ts +0 -27
  235. package/src/memory/runtime/Writer.ts +2 -5
  236. package/src/skill/Command.ts +3 -33
  237. package/src/skill/PROMPT.ts +1 -1
  238. package/src/skill/PROMPT.ts.txt +5 -1
  239. package/src/skill/Plugin.ts +49 -156
  240. package/src/skill/runtime/Prompt.ts +109 -9
  241. package/src/skill/runtime/SystemProvider.ts +5 -9
  242. package/src/skill/types/SkillPlugin.ts +6 -18
  243. package/src/sound/Plugin.ts +699 -0
  244. package/src/sound/types/SoundPlugin.ts +163 -0
  245. package/src/task/Action.ts +8 -0
  246. package/src/task/runtime/Runner.ts +19 -13
  247. package/src/task/runtime/TaskActionExecution.ts +3 -0
  248. package/src/task/runtime/TaskPluginActionRegistry.ts +1 -0
  249. package/src/task/runtime/TaskRunChatDispatch.ts +1 -1
  250. package/src/task/runtime/TaskRunnerRound.ts +20 -38
  251. package/src/task/runtime/TaskRunnerSession.ts +108 -72
  252. package/src/task/runtime/TaskRunnerTypes.ts +4 -4
  253. package/src/task/types/TaskPluginOptions.ts +1 -0
  254. package/src/web/PROMPT.ts +2 -2
  255. package/src/web/PROMPT.ts.txt +4 -2
  256. package/src/web/Plugin.ts +20 -90
  257. package/src/web/runtime/Prompt.ts +99 -0
  258. package/src/web/types/WebPlugin.ts +42 -29
  259. package/src/workboard/runtime/Collector.ts +1 -1
  260. package/src/workboard/runtime/Normalizer.ts +1 -1
  261. package/src/workboard/runtime/SessionSummary.ts +11 -42
  262. package/bin/asr/Plugin.d.ts +0 -63
  263. package/bin/asr/Plugin.d.ts.map +0 -1
  264. package/bin/asr/Plugin.js +0 -280
  265. package/bin/asr/Plugin.js.map +0 -1
  266. package/bin/asr/types/AsrPlugin.d.ts +0 -114
  267. package/bin/asr/types/AsrPlugin.d.ts.map +0 -1
  268. package/bin/asr/types/AsrPlugin.js +0 -10
  269. package/bin/asr/types/AsrPlugin.js.map +0 -1
  270. package/bin/chat/runtime/ChatSession.d.ts +0 -97
  271. package/bin/chat/runtime/ChatSession.d.ts.map +0 -1
  272. package/bin/chat/runtime/ChatSession.js +0 -91
  273. package/bin/chat/runtime/ChatSession.js.map +0 -1
  274. package/bin/chat/runtime/ChatSessionContextComposer.d.ts +0 -45
  275. package/bin/chat/runtime/ChatSessionContextComposer.d.ts.map +0 -1
  276. package/bin/chat/runtime/ChatSessionContextComposer.js +0 -60
  277. package/bin/chat/runtime/ChatSessionContextComposer.js.map +0 -1
  278. package/bin/chat/runtime/ChatSessionTypes.d.ts +0 -30
  279. package/bin/chat/runtime/ChatSessionTypes.d.ts.map +0 -1
  280. package/bin/chat/runtime/ChatSessionTypes.js +0 -9
  281. package/bin/chat/runtime/ChatSessionTypes.js.map +0 -1
  282. package/bin/tts/Plugin.d.ts +0 -49
  283. package/bin/tts/Plugin.d.ts.map +0 -1
  284. package/bin/tts/Plugin.js +0 -247
  285. package/bin/tts/Plugin.js.map +0 -1
  286. package/bin/tts/types/TtsPlugin.d.ts +0 -122
  287. package/bin/tts/types/TtsPlugin.d.ts.map +0 -1
  288. package/bin/tts/types/TtsPlugin.js +0 -10
  289. package/bin/tts/types/TtsPlugin.js.map +0 -1
  290. package/bin/web/runtime/Install.d.ts +0 -19
  291. package/bin/web/runtime/Install.d.ts.map +0 -1
  292. package/bin/web/runtime/Install.js +0 -178
  293. package/bin/web/runtime/Install.js.map +0 -1
  294. package/src/asr/Plugin.ts +0 -322
  295. package/src/asr/types/AsrPlugin.ts +0 -118
  296. package/src/chat/runtime/ChatSession.ts +0 -168
  297. package/src/chat/runtime/ChatSessionContextComposer.ts +0 -106
  298. package/src/chat/runtime/ChatSessionTypes.ts +0 -32
  299. package/src/tts/Plugin.ts +0 -284
  300. package/src/tts/types/TtsPlugin.ts +0 -129
  301. package/src/web/runtime/Install.ts +0 -241
@@ -1,168 +0,0 @@
1
- /**
2
- * ChatSession:chat 专用 Executor 实现。
3
- *
4
- * 关键点(中文)
5
- * - ChatSession 在实例化时持有自己的 context composer 实例。
6
- * - chat 当前 turn 的 step 合并/step 回发状态都绑定在 Session 自身,不再新增 `runChat` 一层入口。
7
- * - 外层仍只调用标准 `run(...)`;chat 语义由 ChatSession 内部收敛。
8
- */
9
-
10
- import type { LanguageModel, Tool } from "ai";
11
- import { Executor } from "@downcity/agent";
12
- import type { SessionHistoryComposer } from "@downcity/agent";
13
- import type { SessionHistoryStore } from "@downcity/agent";
14
- import type { SessionCompactionComposer } from "@downcity/agent";
15
- import type { SessionSystemComposer } from "@downcity/agent";
16
- import type { ChatSessionContextComposer } from "@/chat/runtime/ChatSessionContextComposer.js";
17
- import type { Logger } from "@downcity/agent";
18
- import type { SessionAssistantStepCallback } from "@downcity/agent";
19
- import type { SessionRunResult } from "@downcity/agent";
20
- import type { SessionRunContext } from "@downcity/agent";
21
- import type { ChatSessionTurnState } from "@/chat/runtime/ChatSessionTypes.js";
22
-
23
- type ChatSessionOptions = {
24
- /**
25
- * 当前会话 ID。
26
- */
27
- sessionId: string;
28
-
29
- /**
30
- * 当前 session 的 history store。
31
- */
32
- historyStore: SessionHistoryStore;
33
-
34
- /**
35
- * 当前 session 的 history composer。
36
- */
37
- historyComposer: SessionHistoryComposer;
38
-
39
- /**
40
- * 读取当前 session 使用的模型实例。
41
- */
42
- getModel: () => LanguageModel | undefined;
43
-
44
- /**
45
- * 统一日志器。
46
- */
47
- logger: Logger;
48
-
49
- /**
50
- * 当前 session 对应的 compaction Composer。
51
- */
52
- compactionComposer: SessionCompactionComposer;
53
-
54
- /**
55
- * 当前 session 对应的 system Composer。
56
- */
57
- systemComposer: SessionSystemComposer;
58
-
59
- /**
60
- * 获取当前可用工具集合。
61
- */
62
- getTools: () => Record<string, Tool>;
63
-
64
- /**
65
- * 读取当前 chat session effective Agent env。
66
- */
67
- getEnv: () => Record<string, string>;
68
-
69
- /**
70
- * 当前 session 绑定的 chat context composer。
71
- */
72
- contextComposer: ChatSessionContextComposer;
73
-
74
- /**
75
- * session 更新后的异步回调。
76
- */
77
- };
78
-
79
- /**
80
- * Chat 专用 Session。
81
- */
82
- export class ChatSession extends Executor {
83
- /**
84
- * ChatSession 持有的 composer 实例。
85
- */
86
- readonly contextComposer: ChatSessionContextComposer;
87
-
88
- private activeTurnState: ChatSessionTurnState | null = null;
89
-
90
- constructor(options: ChatSessionOptions) {
91
- const contextComposer = options.contextComposer;
92
- super({
93
- sessionId: options.sessionId,
94
- historyStore: options.historyStore,
95
- historyComposer: options.historyComposer,
96
- getModel: options.getModel,
97
- logger: options.logger,
98
- compactionComposer: options.compactionComposer,
99
- systemComposer: options.systemComposer,
100
- getTools: options.getTools,
101
- getEnv: options.getEnv,
102
- contextComposer,
103
- });
104
- this.contextComposer = contextComposer;
105
- }
106
-
107
- /**
108
- * 读取当前绑定的 chat turn 状态。
109
- */
110
- getTurnState(): ChatSessionTurnState | null {
111
- return this.activeTurnState;
112
- }
113
-
114
- /**
115
- * 处理一次 assistant step 回发。
116
- *
117
- * 关键点(中文)
118
- * - step 持久化由基类 `Session.run` 先完成。
119
- * - 这里仅负责把当前 turn 绑定的外部回发钩子继续调出去。
120
- */
121
- private readonly forwardAssistantStep: SessionAssistantStepCallback = async (
122
- step,
123
- ): Promise<void> => {
124
- const callback = this.activeTurnState?.onAssistantStepCallback;
125
- if (typeof callback !== "function") return;
126
- await callback(step);
127
- };
128
-
129
- /**
130
- * 运行当前 chat session 的一次请求。
131
- *
132
- * 关键点(中文)
133
- * - 外层继续走统一的内部执行入口。
134
- * - ChatSession 只是在 run 生命周期里临时绑定 turn 状态给自己的 composer。
135
- */
136
- override async run(params: {
137
- query: string;
138
- onStepCallback?: ChatSessionTurnState["onStepCallback"];
139
- onAssistantStepCallback?: ChatSessionTurnState["onAssistantStepCallback"];
140
- }): Promise<SessionRunResult> {
141
- this.activeTurnState = {
142
- ...(typeof params.onStepCallback === "function"
143
- ? { onStepCallback: params.onStepCallback }
144
- : {}),
145
- ...(typeof params.onAssistantStepCallback === "function"
146
- ? { onAssistantStepCallback: params.onAssistantStepCallback }
147
- : {}),
148
- };
149
- try {
150
- const run_context: SessionRunContext = {
151
- sessionId: this.sessionId,
152
- ...(typeof params.onStepCallback === "function"
153
- ? { onStepCallback: params.onStepCallback }
154
- : {}),
155
- onAssistantStepCallback: this.forwardAssistantStep,
156
- injectedUserMessages: [],
157
- deferredPersistedUserMessages: [],
158
- pendingAssistantFileParts: [],
159
- };
160
- return await super.run({
161
- query: params.query,
162
- runContext: run_context,
163
- });
164
- } finally {
165
- this.activeTurnState = null;
166
- }
167
- }
168
- }
@@ -1,106 +0,0 @@
1
- /**
2
- * ChatSessionContextComposer:chat 专用 context composer。
3
- *
4
- * 关键点(中文)
5
- * - 由 chat 层在 Session 实例化时注入,不再依赖 run 时层层传 callback。
6
- * - step 合并消息读取自 ChatSession 当前绑定的 turn context。
7
- * - 其余能力(tools / fallback assistant)继续复用默认本地实现。
8
- */
9
-
10
- import type { ModelMessage, Tool } from "ai";
11
- import { drainInjectedUserMessages } from "@downcity/agent";
12
- import { LocalSessionContextComposer } from "@downcity/agent";
13
- import type { SessionRecordV1 } from "@downcity/agent";
14
- import type { SessionSystemMessage } from "@downcity/agent";
15
- import type { ChatSessionTurnState } from "@/chat/runtime/ChatSessionTypes.js";
16
-
17
- type ChatSessionContextComposerOptions = {
18
- /**
19
- * 当前 session 标识。
20
- */
21
- sessionId: string;
22
-
23
- /**
24
- * 读取当前可用工具集合。
25
- */
26
- getTools: () => Record<string, Tool>;
27
-
28
- /**
29
- * 读取当前已绑定的 chat turn 状态。
30
- */
31
- getTurnState: () => ChatSessionTurnState | null;
32
- };
33
-
34
- /**
35
- * ChatSession 专用 context composer。
36
- */
37
- export class ChatSessionContextComposer extends LocalSessionContextComposer {
38
- private readonly getTurnState: ChatSessionContextComposerOptions["getTurnState"];
39
-
40
- constructor(options: ChatSessionContextComposerOptions) {
41
- super({
42
- sessionId: options.sessionId,
43
- getTools: options.getTools,
44
- });
45
- this.getTurnState = options.getTurnState;
46
- }
47
-
48
- createPrepareStepHandler(
49
- input: {
50
- system: SessionSystemMessage[];
51
- appendMergedUserMessages: (
52
- messages: SessionRecordV1[],
53
- ) => Promise<ModelMessage[]>;
54
- },
55
- ): (input: { messages?: ModelMessage[] }) => Promise<{
56
- system: SessionSystemMessage[];
57
- messages?: ModelMessage[];
58
- }> {
59
- return async ({
60
- messages,
61
- }: {
62
- messages?: ModelMessage[];
63
- }): Promise<{
64
- system: SessionSystemMessage[];
65
- messages?: ModelMessage[];
66
- }> => {
67
- const injectedMessages = drainInjectedUserMessages();
68
- const turnState = this.getTurnState();
69
- const onStepCallback = turnState?.onStepCallback;
70
- if (
71
- typeof onStepCallback !== "function" &&
72
- injectedMessages.length === 0
73
- ) {
74
- return { system: input.system };
75
- }
76
-
77
- const incomingMessages: ModelMessage[] = Array.isArray(messages)
78
- ? messages
79
- : [];
80
- let outMessages: ModelMessage[] | undefined;
81
- try {
82
- const mergedMessages =
83
- typeof onStepCallback === "function"
84
- ? await onStepCallback()
85
- : [];
86
- const mergedWithInjected = [
87
- ...injectedMessages,
88
- ...(Array.isArray(mergedMessages) ? mergedMessages : []),
89
- ];
90
- const mergedModelMessages = await input.appendMergedUserMessages(
91
- mergedWithInjected,
92
- );
93
- if (mergedModelMessages.length > 0) {
94
- // 关键点(中文):保持当前 step 顺序,仅把新增 chat user 消息追加到末尾。
95
- outMessages = [...incomingMessages, ...mergedModelMessages];
96
- }
97
- } catch {
98
- // ignore merge hook failures
99
- }
100
- return {
101
- system: input.system,
102
- ...(Array.isArray(outMessages) ? { messages: outMessages } : {}),
103
- };
104
- };
105
- }
106
- }
@@ -1,32 +0,0 @@
1
- /**
2
- * ChatSession 相关类型定义。
3
- *
4
- * 关键点(中文)
5
- * - Chat 专用 session 会把“队列消息合并 / step 回发”收敛到 chat 层自身。
6
- * - 普通 Session 仍可复用基础 `run / append*` 能力,不强耦合 chat 语义。
7
- */
8
-
9
- import type { SessionUserMessageV1 } from "@downcity/agent";
10
- import type { SessionAssistantStepCallback } from "@downcity/agent";
11
-
12
- /**
13
- * ChatSession 单次 turn 绑定的运行态。
14
- */
15
- export interface ChatSessionTurnState {
16
- /**
17
- * 提取本轮 step 边界新增的 user 消息。
18
- *
19
- * 关键点(中文)
20
- * - 该回调用于把同一 chat lane 后续到达的消息并入当前推理。
21
- * - 若未提供,则表示当前 turn 不启用 step 合并。
22
- */
23
- onStepCallback?: () => Promise<SessionUserMessageV1[]>;
24
-
25
- /**
26
- * assistant step 回发钩子。
27
- *
28
- * 关键点(中文)
29
- * - ChatSession 内部会先完成 step 持久化,再调用该钩子做真实渠道发送。
30
- */
31
- onAssistantStepCallback?: SessionAssistantStepCallback;
32
- }
package/src/tts/Plugin.ts DELETED
@@ -1,284 +0,0 @@
1
- /**
2
- * TtsPlugin:文本转语音插件。
3
- *
4
- * 关键点(中文)
5
- * - TTS 能力通过 constructor 注入,推荐传入 `city.ai.tts`。
6
- * - plugin 不负责本地模型、Python 依赖、provider 或项目配置。
7
- * - action 返回 AI SDK UIMessage,音频 file part 会由 plugin tool bridge 统一落盘到资源目录。
8
- */
9
-
10
- import { BasePlugin } from "@downcity/agent";
11
- import { createAction } from "@downcity/agent";
12
- import { z } from "zod";
13
- import type { AgentContext } from "@downcity/agent";
14
- import type {
15
- JsonObject,
16
- JsonValue,
17
- } from "@downcity/agent";
18
- import type {
19
- TtsPluginInput,
20
- TtsPluginOptions,
21
- TtsPluginResult,
22
- TtsPluginSimpleAudioResult,
23
- TtsPluginUiMessageResult,
24
- } from "@/tts/types/TtsPlugin.js";
25
-
26
- const DEFAULT_TTS_PLUGIN_NAME = "tts";
27
- const DEFAULT_TTS_PLUGIN_TITLE = "TTS";
28
- const DEFAULT_TTS_PLUGIN_DESCRIPTION =
29
- "Synthesize speech through an injected TTS function and return assistant audio file parts.";
30
- const DEFAULT_MEDIA_TYPE = "audio/wav";
31
- const DEFAULT_FILENAME = "speech.wav";
32
-
33
- /**
34
- * 判断值是否为普通对象。
35
- */
36
- function to_record(value: unknown): Record<string, unknown> | null {
37
- if (!value || typeof value !== "object" || Array.isArray(value)) return null;
38
- return value as Record<string, unknown>;
39
- }
40
-
41
- /**
42
- * 归一化 action payload。
43
- */
44
- function normalize_tts_payload(payload: JsonValue | undefined): TtsPluginInput {
45
- const record = to_record(payload ?? {});
46
- if (!record) {
47
- throw new TypeError("TtsPlugin.synthesize payload must be an object");
48
- }
49
- const text = typeof record.text === "string" ? record.text.trim() : "";
50
- if (!text) {
51
- throw new Error("TtsPlugin.synthesize requires text");
52
- }
53
- return {
54
- ...(record as TtsPluginInput),
55
- text,
56
- };
57
- }
58
-
59
- /**
60
- * 判断返回值是否已经是 AI SDK UIMessage。
61
- */
62
- function is_ui_message_result(value: TtsPluginResult): value is TtsPluginUiMessageResult {
63
- const record = to_record(value);
64
- return Boolean(record && Array.isArray(record.parts));
65
- }
66
-
67
- /**
68
- * 读取简单音频结果中的 URL。
69
- */
70
- function read_audio_url(record: Record<string, unknown>): string {
71
- const url =
72
- typeof record.url === "string"
73
- ? record.url.trim()
74
- : typeof record.data_url === "string"
75
- ? record.data_url.trim()
76
- : typeof record.audio_path === "string"
77
- ? record.audio_path.trim()
78
- : "";
79
- if (!url) {
80
- throw new TypeError(
81
- "TtsPlugin tts function must return a UIMessage or { url | data_url | audio_path }",
82
- );
83
- }
84
- return url;
85
- }
86
-
87
- /**
88
- * 把简单音频结果归一为 UIMessage。
89
- */
90
- function simple_audio_to_ui_message(
91
- result: TtsPluginSimpleAudioResult,
92
- ): TtsPluginUiMessageResult {
93
- const record = to_record(result);
94
- if (!record) {
95
- throw new TypeError("TtsPlugin tts function returned an invalid result");
96
- }
97
- const url = read_audio_url(record);
98
- const media_type =
99
- typeof record.media_type === "string" && record.media_type.trim()
100
- ? record.media_type.trim()
101
- : DEFAULT_MEDIA_TYPE;
102
- const filename =
103
- typeof record.filename === "string" && record.filename.trim()
104
- ? record.filename.trim()
105
- : DEFAULT_FILENAME;
106
- const text = typeof record.text === "string" ? record.text.trim() : "";
107
-
108
- return {
109
- id: `tts:${Date.now()}`,
110
- role: "assistant",
111
- parts: [
112
- ...(text ? [{ type: "text" as const, text }] : []),
113
- {
114
- type: "file" as const,
115
- mediaType: media_type,
116
- filename,
117
- url,
118
- },
119
- ],
120
- };
121
- }
122
-
123
- /**
124
- * 校验并归一化 TTS 返回结果。
125
- */
126
- function normalize_tts_result(result: TtsPluginResult): TtsPluginUiMessageResult {
127
- if (is_ui_message_result(result)) {
128
- return result;
129
- }
130
- return simple_audio_to_ui_message(result);
131
- }
132
-
133
- /**
134
- * Agent TTS 插件。
135
- */
136
- export class TtsPlugin extends BasePlugin {
137
- /**
138
- * 当前 plugin 稳定名称。
139
- */
140
- readonly name: string;
141
-
142
- /**
143
- * 插件标题。
144
- */
145
- readonly title: string;
146
-
147
- /**
148
- * 插件说明。
149
- */
150
- readonly description: string;
151
-
152
- private readonly tts: TtsPluginOptions["tts"];
153
- private readonly language?: string;
154
- private readonly voice?: string;
155
- private readonly format?: string;
156
-
157
- constructor(options: TtsPluginOptions) {
158
- super();
159
- const name = String(options.name || DEFAULT_TTS_PLUGIN_NAME).trim();
160
- if (!name) {
161
- throw new Error("TtsPlugin requires a non-empty name");
162
- }
163
- if (typeof options.tts !== "function") {
164
- throw new Error("TtsPlugin requires a tts function");
165
- }
166
- this.name = name;
167
- this.title = String(options.title || DEFAULT_TTS_PLUGIN_TITLE).trim();
168
- this.description = String(
169
- options.description || DEFAULT_TTS_PLUGIN_DESCRIPTION,
170
- ).trim();
171
- this.tts = options.tts;
172
- this.language =
173
- typeof options.language === "string" && options.language.trim()
174
- ? options.language.trim()
175
- : undefined;
176
- this.voice =
177
- typeof options.voice === "string" && options.voice.trim()
178
- ? options.voice.trim()
179
- : undefined;
180
- this.format =
181
- typeof options.format === "string" && options.format.trim()
182
- ? options.format.trim()
183
- : undefined;
184
- }
185
-
186
- /**
187
- * TTS 插件给模型的使用说明。
188
- */
189
- system(_context: AgentContext): string {
190
- return [
191
- "# TTS Plugin",
192
- "",
193
- "Use this plugin when the user asks to create spoken audio, voice output, narration, or a reusable audio file.",
194
- "Do not call it for ordinary text replies unless the user explicitly wants audio.",
195
- "",
196
- "Call through `plugin_call`:",
197
- "",
198
- "```ts",
199
- "plugin_call({",
200
- ` plugin: "${this.name}",`,
201
- ' action: "synthesize",',
202
- " payload: {",
203
- ' text: "...",',
204
- " },",
205
- "});",
206
- "```",
207
- "",
208
- "Payload rules:",
209
- "- `text` is required.",
210
- "- Optional fields: `language`, `voice`, `format`, `speed`, `provider_options`.",
211
- "- The returned audio file part is saved under project `.downcity/resources` and attached to the final assistant message automatically.",
212
- ].join("\n");
213
- }
214
-
215
- /**
216
- * 执行一次 TTS 合成。
217
- */
218
- private async synthesize(input: TtsPluginInput): Promise<TtsPluginUiMessageResult> {
219
- const result = await this.tts({
220
- ...(this.language ? { language: this.language } : {}),
221
- ...(this.voice ? { voice: this.voice } : {}),
222
- ...(this.format ? { format: this.format } : {}),
223
- ...input,
224
- });
225
- return normalize_tts_result(result);
226
- }
227
-
228
- /**
229
- * 显式 action 集合。
230
- */
231
- readonly actions = {
232
- synthesize: createAction({
233
- description:
234
- "Synthesize speech from text. Returns a UIMessage whose audio file part is auto-saved under the project resources directory.",
235
- input_schema: {
236
- zod: z
237
- .object({
238
- text: z.string(),
239
- language: z.string().optional(),
240
- voice: z.string().optional(),
241
- format: z.string().optional(),
242
- speed: z.number().optional(),
243
- provider_options: z.record(z.string(), z.unknown()).optional(),
244
- })
245
- .passthrough(),
246
- json_schema: {
247
- type: "object",
248
- required: ["text"],
249
- properties: {
250
- text: { type: "string", description: "Text to synthesize." },
251
- language: { type: "string", description: "Language code, optional." },
252
- voice: { type: "string", description: "Voice, optional." },
253
- format: { type: "string", description: "Audio format, optional." },
254
- speed: { type: "number", description: "Speech speed, optional." },
255
- },
256
- },
257
- },
258
- examples: [
259
- { title: "Default voice", payload: { text: "Hello, world" } },
260
- {
261
- title: "Specific voice",
262
- payload: { text: "Welcome back", voice: "alloy", format: "mp3" },
263
- },
264
- ],
265
- execute: async ({ input }: { input: JsonValue }) => {
266
- try {
267
- const synth_input = normalize_tts_payload(input);
268
- const message = await this.synthesize(synth_input);
269
- return {
270
- success: true,
271
- data: message as unknown as JsonObject,
272
- message: "speech synthesized",
273
- };
274
- } catch (error) {
275
- return {
276
- success: false,
277
- error: String(error),
278
- message: String(error),
279
- };
280
- }
281
- },
282
- }),
283
- };
284
- }