conversation-quotes.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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("carries native references through queue persistence and only exposes refIds to the model", () => {
  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. expect(model).toContain("ReplyToChannel");
  71. expect(model).toContain('"refId":"m1"');
  72. expect(model).not.toContain("ou_target");
  73. for (const state of ["queued", "drained"] as const)
  74. expect(
  75. buildPersistedConversationInputIntent(
  76. model,
  77. { kind: "sendText", text: "帮我 @Ryan Bot", sourceCommandId: "input", botGroupSource },
  78. state,
  79. ),
  80. ).toMatchObject({ text: "帮我 @Ryan Bot", botGroupSource });
  81. });
  82. it("pins the current bot identity and distinguishes per-message assignment from shared context", () => {
  83. const model = withConversationQuotes("continue", [], {
  84. botId: "local-bot",
  85. provider: "feishu",
  86. botIdentity: { name: "Travel Bot", openId: "ou_self" },
  87. mentionedBot: false,
  88. messages: [
  89. {
  90. messageId: "m1",
  91. text: "continue",
  92. mentionedBot: false,
  93. senderId: "user",
  94. senderName: "Member",
  95. attachmentIndexes: [],
  96. },
  97. ],
  98. });
  99. expect(model).toContain('"name":"Travel Bot"');
  100. expect(model).toContain('"openId":"ou_self"');
  101. expect(model).toContain('"mentionedBot":false');
  102. expect(model).toContain("Shared context is not shared task ownership");
  103. expect(model).toContain("no visible reply");
  104. });
  105. it("distinguishes same-name recipients by their trusted self association", () => {
  106. const project = (ids: string[]) =>
  107. withConversationQuotes("@Assistant outline; @Assistant implement", [], {
  108. botId: "bot",
  109. provider: "feishu",
  110. botIdentity: { name: "Configured Bot", openId: "ou_self" },
  111. contentParts: ids.map((targetId, index) => ({
  112. type: "channelMention",
  113. refId: `m${index}`,
  114. name: "Assistant",
  115. targetId,
  116. channel: "feishu",
  117. idType: "open_id",
  118. entityType: "unknown",
  119. })),
  120. });
  121. const first = project(["ou_self", "ou_other"]);
  122. const swapped = project(["ou_other", "ou_self"]);
  123. expect(first).not.toBe(swapped);
  124. expect(first).toContain(
  125. '"refId":"m0","name":"Assistant","channel":"feishu","isCurrentBot":true',
  126. );
  127. expect(swapped).toContain(
  128. '"refId":"m1","name":"Assistant","channel":"feishu","isCurrentBot":true',
  129. );
  130. expect(first).not.toContain("ou_other");
  131. expect(swapped).not.toContain("ou_other");
  132. });
  133. it("locates native self mentions among identical typed names and repeated batch mentions", () => {
  134. const mention = {
  135. type: "channelMention" as const,
  136. refId: "self",
  137. name: "Assistant",
  138. targetId: "ou_self",
  139. channel: "feishu" as const,
  140. idType: "open_id" as const,
  141. entityType: "unknown" as const,
  142. };
  143. const source = {
  144. botId: "bot",
  145. provider: "feishu" as const,
  146. botIdentity: { name: "Configured Bot", openId: "ou_self" },
  147. };
  148. const text = "@Assistant summarize; @Assistant implement";
  149. const first = withConversationQuotes(text, [], {
  150. ...source,
  151. contentParts: [mention, { type: "text", text: " summarize; @Assistant implement" }],
  152. });
  153. const last = withConversationQuotes(text, [], {
  154. ...source,
  155. contentParts: [
  156. { type: "text", text: "@Assistant summarize; " },
  157. mention,
  158. { type: "text", text: " implement" },
  159. ],
  160. });
  161. expect(first).not.toBe(last);
  162. expect(first).toContain(
  163. '"isCurrentBot":true},{"type":"text","text":" summarize; @Assistant implement"}',
  164. );
  165. expect(last).toContain('"text":"@Assistant summarize; "},{"type":"channelMention"');
  166. const batch = withConversationQuotes(text, [], {
  167. ...source,
  168. messages: [
  169. {
  170. messageId: "first",
  171. text,
  172. senderId: "user",
  173. senderName: "Member",
  174. attachmentIndexes: [],
  175. contentParts: [
  176. mention,
  177. { type: "text", text: " summarize; " },
  178. { ...mention, refId: "second" },
  179. ],
  180. },
  181. {
  182. messageId: "last",
  183. text,
  184. senderId: "user",
  185. senderName: "Member",
  186. attachmentIndexes: [],
  187. contentParts: [
  188. { type: "text", text: " implement " },
  189. { ...mention, refId: "third" },
  190. ],
  191. },
  192. ],
  193. });
  194. expect(batch).toContain('"messageId":"first","contentParts"');
  195. expect(batch).toContain('"messageId":"last","contentParts"');
  196. for (const ref of ["self", "second", "third"]) expect(batch).toContain(`"refId":"${ref}"`);
  197. expect(batch).not.toContain('"targetId"');
  198. });
  199. it("leaves plain input unchanged", () => {
  200. expect(withConversationQuotes("hello", [])).toBe("hello");
  201. });
  202. });