conversation-quotes.test.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import { describe, expect, it } from "vitest";
  2. import { withConversationQuotes } from "../src/runtime/helpers/conversation-quotes.js";
  3. import { buildPersistedConversationInputIntent } from "../src/runtime/methods/input-intent-persistence.js";
  4. describe("structured conversation quotes", () => {
  5. it("keeps canonical body separate while the model receives original source metadata", () => {
  6. const conversationQuotes = [
  7. {
  8. text: "/stop\noriginal",
  9. senderName: "Ryan Bot",
  10. senderId: "ou_bot",
  11. messageId: "om_quote",
  12. sentAt: "2026-09-09T10:32:44+08:00",
  13. },
  14. ];
  15. const model = withConversationQuotes("", conversationQuotes);
  16. expect(model).toContain("<conversation_quote>");
  17. expect(model).not.toContain("userselect");
  18. expect(model).toContain(JSON.stringify(conversationQuotes));
  19. for (const state of ["queued", "drained"] as const) {
  20. expect(
  21. buildPersistedConversationInputIntent(
  22. model,
  23. {
  24. kind: "sendText",
  25. text: "",
  26. conversationQuotes,
  27. sourceCommandId: "c",
  28. queueItemId: "q",
  29. clientId: "desktop",
  30. admissionSeq: 1,
  31. admittedAt: 1,
  32. requestedDelivery: "queue",
  33. admittedDelivery: "queue",
  34. },
  35. state,
  36. ),
  37. ).toMatchObject({ text: "", conversationQuotes });
  38. }
  39. });
  40. it("tells the model that a quote comment is the user's annotation on that passage", () => {
  41. const conversationQuotes = [
  42. { text: "| 列 | 值 |\n| --- | --- |", comment: "这张表为什么缺少单位?", agent: "Explore" },
  43. { text: "plain quote" },
  44. ];
  45. const model = withConversationQuotes("请一起回答", conversationQuotes);
  46. expect(model).toContain(JSON.stringify(conversationQuotes));
  47. expect(model).toContain("user annotation on that exact quoted text");
  48. expect(model).toContain("`agent` field names the subagent");
  49. expect(
  50. buildPersistedConversationInputIntent(
  51. model,
  52. {
  53. kind: "sendText",
  54. text: "请一起回答",
  55. conversationQuotes,
  56. sourceCommandId: "c",
  57. queueItemId: "q",
  58. clientId: "desktop",
  59. admissionSeq: 1,
  60. admittedAt: 1,
  61. requestedDelivery: "queue",
  62. admittedDelivery: "queue",
  63. },
  64. "drained",
  65. ),
  66. ).toMatchObject({ conversationQuotes });
  67. // 没有批注时不追加说明,普通引用的模型输入保持原样。
  68. expect(withConversationQuotes("", [{ text: "plain quote", comment: " " }])).not.toContain(
  69. "user annotation",
  70. );
  71. });
  72. it("keeps bot delivery with the service and treats identity as attribution", () => {
  73. const result = withConversationQuotes("weather?", [], {
  74. botId: "bot",
  75. chatId: "chat",
  76. provider: "feishu",
  77. });
  78. expect(result).toContain("automatically delivers");
  79. expect(result).toContain("timezone");
  80. expect(result).toContain("explicitly requests an additional");
  81. expect(result).not.toContain("bot_topic_context");
  82. });
  83. it("persists native references without exposing channel reply instructions or send references", () => {
  84. const botGroupSource = {
  85. botId: "bot",
  86. chatId: "chat",
  87. provider: "feishu" as const,
  88. appId: "app",
  89. contentParts: [
  90. {
  91. type: "channelMention" as const,
  92. refId: "m1",
  93. name: "Ryan Bot",
  94. targetId: "ou_target",
  95. channel: "feishu" as const,
  96. idType: "open_id" as const,
  97. entityType: "unknown" as const,
  98. },
  99. ],
  100. };
  101. const model = withConversationQuotes("帮我 @Ryan Bot", [], botGroupSource);
  102. for (const removed of [
  103. "ReplyToChannel",
  104. "mentionName",
  105. "needs_clarification",
  106. "Trusted native mention references",
  107. '"refId"',
  108. ])
  109. expect(model).not.toContain(removed);
  110. expect(model).toContain('"name":"Ryan Bot"');
  111. expect(model).not.toContain("ou_target");
  112. for (const state of ["queued", "drained"] as const)
  113. expect(
  114. buildPersistedConversationInputIntent(
  115. model,
  116. { kind: "sendText", text: "帮我 @Ryan Bot", sourceCommandId: "input", botGroupSource },
  117. state,
  118. ),
  119. ).toMatchObject({ text: "帮我 @Ryan Bot", botGroupSource });
  120. });
  121. it("pins the current bot identity and distinguishes per-message assignment from shared context", () => {
  122. const model = withConversationQuotes("continue", [], {
  123. botId: "local-bot",
  124. provider: "feishu",
  125. botIdentity: { name: "Travel Bot", openId: "ou_self" },
  126. mentionedBot: false,
  127. messages: [
  128. {
  129. messageId: "m1",
  130. text: "continue",
  131. mentionedBot: false,
  132. senderId: "user",
  133. senderName: "Member",
  134. attachmentIndexes: [],
  135. },
  136. ],
  137. });
  138. expect(model).toContain('"name":"Travel Bot"');
  139. expect(model).toContain('"openId":"ou_self"');
  140. expect(model).toContain('"mentionedBot":false');
  141. expect(model).toContain("Shared context is not shared task ownership");
  142. expect(model).toContain("no visible reply");
  143. });
  144. it("distinguishes same-name recipients by their trusted self association", () => {
  145. const project = (ids: string[]) =>
  146. withConversationQuotes("@Assistant outline; @Assistant implement", [], {
  147. botId: "bot",
  148. provider: "feishu",
  149. botIdentity: { name: "Configured Bot", openId: "ou_self" },
  150. contentParts: ids.map((targetId, index) => ({
  151. type: "channelMention",
  152. refId: `m${index}`,
  153. name: "Assistant",
  154. targetId,
  155. channel: "feishu",
  156. idType: "open_id",
  157. entityType: "unknown",
  158. })),
  159. });
  160. const first = project(["ou_self", "ou_other"]);
  161. const swapped = project(["ou_other", "ou_self"]);
  162. expect(first).not.toBe(swapped);
  163. expect(first).toContain('"name":"Assistant","channel":"feishu","isCurrentBot":true');
  164. expect(swapped).toContain('"name":"Assistant","channel":"feishu","isCurrentBot":true');
  165. expect(first).not.toContain("ou_other");
  166. expect(swapped).not.toContain("ou_other");
  167. });
  168. it("locates native self mentions among identical typed names and repeated batch mentions", () => {
  169. const mention = {
  170. type: "channelMention" as const,
  171. refId: "self",
  172. name: "Assistant",
  173. targetId: "ou_self",
  174. channel: "feishu" as const,
  175. idType: "open_id" as const,
  176. entityType: "unknown" as const,
  177. };
  178. const source = {
  179. botId: "bot",
  180. provider: "feishu" as const,
  181. botIdentity: { name: "Configured Bot", openId: "ou_self" },
  182. };
  183. const text = "@Assistant summarize; @Assistant implement";
  184. const first = withConversationQuotes(text, [], {
  185. ...source,
  186. contentParts: [mention, { type: "text", text: " summarize; @Assistant implement" }],
  187. });
  188. const last = withConversationQuotes(text, [], {
  189. ...source,
  190. contentParts: [
  191. { type: "text", text: "@Assistant summarize; " },
  192. mention,
  193. { type: "text", text: " implement" },
  194. ],
  195. });
  196. expect(first).not.toBe(last);
  197. expect(first).toContain(
  198. '"isCurrentBot":true},{"type":"text","text":" summarize; @Assistant implement"}',
  199. );
  200. expect(last).toContain('"text":"@Assistant summarize; "},{"type":"channelMention"');
  201. const batch = withConversationQuotes(text, [], {
  202. ...source,
  203. messages: [
  204. {
  205. messageId: "first",
  206. text,
  207. senderId: "user",
  208. senderName: "Member",
  209. attachmentIndexes: [],
  210. contentParts: [
  211. mention,
  212. { type: "text", text: " summarize; " },
  213. { ...mention, refId: "second" },
  214. ],
  215. },
  216. {
  217. messageId: "last",
  218. text,
  219. senderId: "user",
  220. senderName: "Member",
  221. attachmentIndexes: [],
  222. contentParts: [
  223. { type: "text", text: " implement " },
  224. { ...mention, refId: "third" },
  225. ],
  226. },
  227. ],
  228. });
  229. expect(batch).toContain('"messageId":"first","contentParts"');
  230. expect(batch).toContain('"messageId":"last","contentParts"');
  231. expect(batch).not.toContain('"refId"');
  232. expect(batch).not.toContain('"targetId"');
  233. });
  234. it("leaves plain input unchanged", () => {
  235. expect(withConversationQuotes("hello", [])).toBe("hello");
  236. });
  237. });