conversation-quotes.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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("keeps bot delivery with the service and treats identity as attribution", () => {
  41. const result = withConversationQuotes("weather?", [], {
  42. botId: "bot",
  43. chatId: "chat",
  44. provider: "feishu",
  45. });
  46. expect(result).toContain("automatically delivers");
  47. expect(result).toContain("timezone");
  48. expect(result).toContain("explicitly requests an additional");
  49. expect(result).not.toContain("bot_topic_context");
  50. });
  51. it("persists native references without exposing channel reply instructions or send references", () => {
  52. const botGroupSource = {
  53. botId: "bot",
  54. chatId: "chat",
  55. provider: "feishu" as const,
  56. appId: "app",
  57. contentParts: [
  58. {
  59. type: "channelMention" as const,
  60. refId: "m1",
  61. name: "Ryan Bot",
  62. targetId: "ou_target",
  63. channel: "feishu" as const,
  64. idType: "open_id" as const,
  65. entityType: "unknown" as const,
  66. },
  67. ],
  68. };
  69. const model = withConversationQuotes("帮我 @Ryan Bot", [], botGroupSource);
  70. for (const removed of [
  71. "ReplyToChannel",
  72. "mentionName",
  73. "needs_clarification",
  74. "Trusted native mention references",
  75. '"refId"',
  76. ])
  77. expect(model).not.toContain(removed);
  78. expect(model).toContain('"name":"Ryan Bot"');
  79. expect(model).not.toContain("ou_target");
  80. for (const state of ["queued", "drained"] as const)
  81. expect(
  82. buildPersistedConversationInputIntent(
  83. model,
  84. { kind: "sendText", text: "帮我 @Ryan Bot", sourceCommandId: "input", botGroupSource },
  85. state,
  86. ),
  87. ).toMatchObject({ text: "帮我 @Ryan Bot", botGroupSource });
  88. });
  89. it("pins the current bot identity and distinguishes per-message assignment from shared context", () => {
  90. const model = withConversationQuotes("continue", [], {
  91. botId: "local-bot",
  92. provider: "feishu",
  93. botIdentity: { name: "Travel Bot", openId: "ou_self" },
  94. mentionedBot: false,
  95. messages: [
  96. {
  97. messageId: "m1",
  98. text: "continue",
  99. mentionedBot: false,
  100. senderId: "user",
  101. senderName: "Member",
  102. attachmentIndexes: [],
  103. },
  104. ],
  105. });
  106. expect(model).toContain('"name":"Travel Bot"');
  107. expect(model).toContain('"openId":"ou_self"');
  108. expect(model).toContain('"mentionedBot":false');
  109. expect(model).toContain("Shared context is not shared task ownership");
  110. expect(model).toContain("no visible reply");
  111. });
  112. it("distinguishes same-name recipients by their trusted self association", () => {
  113. const project = (ids: string[]) =>
  114. withConversationQuotes("@Assistant outline; @Assistant implement", [], {
  115. botId: "bot",
  116. provider: "feishu",
  117. botIdentity: { name: "Configured Bot", openId: "ou_self" },
  118. contentParts: ids.map((targetId, index) => ({
  119. type: "channelMention",
  120. refId: `m${index}`,
  121. name: "Assistant",
  122. targetId,
  123. channel: "feishu",
  124. idType: "open_id",
  125. entityType: "unknown",
  126. })),
  127. });
  128. const first = project(["ou_self", "ou_other"]);
  129. const swapped = project(["ou_other", "ou_self"]);
  130. expect(first).not.toBe(swapped);
  131. expect(first).toContain('"name":"Assistant","channel":"feishu","isCurrentBot":true');
  132. expect(swapped).toContain('"name":"Assistant","channel":"feishu","isCurrentBot":true');
  133. expect(first).not.toContain("ou_other");
  134. expect(swapped).not.toContain("ou_other");
  135. });
  136. it("locates native self mentions among identical typed names and repeated batch mentions", () => {
  137. const mention = {
  138. type: "channelMention" as const,
  139. refId: "self",
  140. name: "Assistant",
  141. targetId: "ou_self",
  142. channel: "feishu" as const,
  143. idType: "open_id" as const,
  144. entityType: "unknown" as const,
  145. };
  146. const source = {
  147. botId: "bot",
  148. provider: "feishu" as const,
  149. botIdentity: { name: "Configured Bot", openId: "ou_self" },
  150. };
  151. const text = "@Assistant summarize; @Assistant implement";
  152. const first = withConversationQuotes(text, [], {
  153. ...source,
  154. contentParts: [mention, { type: "text", text: " summarize; @Assistant implement" }],
  155. });
  156. const last = withConversationQuotes(text, [], {
  157. ...source,
  158. contentParts: [
  159. { type: "text", text: "@Assistant summarize; " },
  160. mention,
  161. { type: "text", text: " implement" },
  162. ],
  163. });
  164. expect(first).not.toBe(last);
  165. expect(first).toContain(
  166. '"isCurrentBot":true},{"type":"text","text":" summarize; @Assistant implement"}',
  167. );
  168. expect(last).toContain('"text":"@Assistant summarize; "},{"type":"channelMention"');
  169. const batch = withConversationQuotes(text, [], {
  170. ...source,
  171. messages: [
  172. {
  173. messageId: "first",
  174. text,
  175. senderId: "user",
  176. senderName: "Member",
  177. attachmentIndexes: [],
  178. contentParts: [
  179. mention,
  180. { type: "text", text: " summarize; " },
  181. { ...mention, refId: "second" },
  182. ],
  183. },
  184. {
  185. messageId: "last",
  186. text,
  187. senderId: "user",
  188. senderName: "Member",
  189. attachmentIndexes: [],
  190. contentParts: [
  191. { type: "text", text: " implement " },
  192. { ...mention, refId: "third" },
  193. ],
  194. },
  195. ],
  196. });
  197. expect(batch).toContain('"messageId":"first","contentParts"');
  198. expect(batch).toContain('"messageId":"last","contentParts"');
  199. expect(batch).not.toContain('"refId"');
  200. expect(batch).not.toContain('"targetId"');
  201. });
  202. it("leaves plain input unchanged", () => {
  203. expect(withConversationQuotes("hello", [])).toBe("hello");
  204. });
  205. });