prompt.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import path from "path"
  2. import { describe, expect, test } from "bun:test"
  3. import { fileURLToPath } from "url"
  4. import { Instance } from "../../src/project/instance"
  5. import { Session } from "../../src/session"
  6. import { MessageV2 } from "../../src/session/message-v2"
  7. import { SessionPrompt } from "../../src/session/prompt"
  8. import { Log } from "../../src/util/log"
  9. import { tmpdir } from "../fixture/fixture"
  10. Log.init({ print: false })
  11. describe("session.prompt missing file", () => {
  12. test("does not fail the prompt when a file part is missing", async () => {
  13. await using tmp = await tmpdir({
  14. git: true,
  15. config: {
  16. agent: {
  17. build: {
  18. model: "openai/gpt-5.2",
  19. },
  20. },
  21. },
  22. })
  23. await Instance.provide({
  24. directory: tmp.path,
  25. fn: async () => {
  26. const session = await Session.create({})
  27. const missing = path.join(tmp.path, "does-not-exist.ts")
  28. const msg = await SessionPrompt.prompt({
  29. sessionID: session.id,
  30. agent: "build",
  31. noReply: true,
  32. parts: [
  33. { type: "text", text: "please review @does-not-exist.ts" },
  34. {
  35. type: "file",
  36. mime: "text/plain",
  37. url: `file://${missing}`,
  38. filename: "does-not-exist.ts",
  39. },
  40. ],
  41. })
  42. if (msg.info.role !== "user") throw new Error("expected user message")
  43. const hasFailure = msg.parts.some(
  44. (part) => part.type === "text" && part.synthetic && part.text.includes("Read tool failed to read"),
  45. )
  46. expect(hasFailure).toBe(true)
  47. await Session.remove(session.id)
  48. },
  49. })
  50. })
  51. test("keeps stored part order stable when file resolution is async", async () => {
  52. await using tmp = await tmpdir({
  53. git: true,
  54. config: {
  55. agent: {
  56. build: {
  57. model: "openai/gpt-5.2",
  58. },
  59. },
  60. },
  61. })
  62. await Instance.provide({
  63. directory: tmp.path,
  64. fn: async () => {
  65. const session = await Session.create({})
  66. const missing = path.join(tmp.path, "still-missing.ts")
  67. const msg = await SessionPrompt.prompt({
  68. sessionID: session.id,
  69. agent: "build",
  70. noReply: true,
  71. parts: [
  72. {
  73. type: "file",
  74. mime: "text/plain",
  75. url: `file://${missing}`,
  76. filename: "still-missing.ts",
  77. },
  78. { type: "text", text: "after-file" },
  79. ],
  80. })
  81. if (msg.info.role !== "user") throw new Error("expected user message")
  82. const stored = await MessageV2.get({
  83. sessionID: session.id,
  84. messageID: msg.info.id,
  85. })
  86. const text = stored.parts.filter((part) => part.type === "text").map((part) => part.text)
  87. expect(text[0]?.startsWith("Called the Read tool with the following input:")).toBe(true)
  88. expect(text[1]?.includes("Read tool failed to read")).toBe(true)
  89. expect(text[2]).toBe("after-file")
  90. await Session.remove(session.id)
  91. },
  92. })
  93. })
  94. })
  95. describe("session.prompt special characters", () => {
  96. test("handles filenames with # character", async () => {
  97. await using tmp = await tmpdir({
  98. git: true,
  99. init: async (dir) => {
  100. await Bun.write(path.join(dir, "file#name.txt"), "special content\n")
  101. },
  102. })
  103. await Instance.provide({
  104. directory: tmp.path,
  105. fn: async () => {
  106. const session = await Session.create({})
  107. const template = "Read @file#name.txt"
  108. const parts = await SessionPrompt.resolvePromptParts(template)
  109. const fileParts = parts.filter((part) => part.type === "file")
  110. expect(fileParts.length).toBe(1)
  111. expect(fileParts[0].filename).toBe("file#name.txt")
  112. expect(fileParts[0].url).toContain("%23")
  113. const decodedPath = fileURLToPath(fileParts[0].url)
  114. expect(decodedPath).toBe(path.join(tmp.path, "file#name.txt"))
  115. const message = await SessionPrompt.prompt({
  116. sessionID: session.id,
  117. parts,
  118. noReply: true,
  119. })
  120. const stored = await MessageV2.get({ sessionID: session.id, messageID: message.info.id })
  121. const textParts = stored.parts.filter((part) => part.type === "text")
  122. const hasContent = textParts.some((part) => part.text.includes("special content"))
  123. expect(hasContent).toBe(true)
  124. await Session.remove(session.id)
  125. },
  126. })
  127. })
  128. })
  129. describe("session.prompt agent variant", () => {
  130. test("applies agent variant only when using agent model", async () => {
  131. const prev = process.env.OPENAI_API_KEY
  132. process.env.OPENAI_API_KEY = "test-openai-key"
  133. try {
  134. await using tmp = await tmpdir({
  135. git: true,
  136. config: {
  137. agent: {
  138. build: {
  139. model: "openai/gpt-5.2",
  140. variant: "xhigh",
  141. },
  142. },
  143. },
  144. })
  145. await Instance.provide({
  146. directory: tmp.path,
  147. fn: async () => {
  148. const session = await Session.create({})
  149. const other = await SessionPrompt.prompt({
  150. sessionID: session.id,
  151. agent: "build",
  152. model: { providerID: "opencode", modelID: "kimi-k2.5-free" },
  153. noReply: true,
  154. parts: [{ type: "text", text: "hello" }],
  155. })
  156. if (other.info.role !== "user") throw new Error("expected user message")
  157. expect(other.info.variant).toBeUndefined()
  158. const match = await SessionPrompt.prompt({
  159. sessionID: session.id,
  160. agent: "build",
  161. noReply: true,
  162. parts: [{ type: "text", text: "hello again" }],
  163. })
  164. if (match.info.role !== "user") throw new Error("expected user message")
  165. expect(match.info.model).toEqual({ providerID: "openai", modelID: "gpt-5.2" })
  166. expect(match.info.variant).toBe("xhigh")
  167. const override = await SessionPrompt.prompt({
  168. sessionID: session.id,
  169. agent: "build",
  170. noReply: true,
  171. variant: "high",
  172. parts: [{ type: "text", text: "hello third" }],
  173. })
  174. if (override.info.role !== "user") throw new Error("expected user message")
  175. expect(override.info.variant).toBe("high")
  176. await Session.remove(session.id)
  177. },
  178. })
  179. } finally {
  180. if (prev === undefined) delete process.env.OPENAI_API_KEY
  181. else process.env.OPENAI_API_KEY = prev
  182. }
  183. })
  184. })