followup-during-streaming.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { runStreamCase, StreamEvent } from "../lib/stream-harness"
  2. const START_PROMPT = 'Answer this question and finish: What is 1+1? Reply with only "2", then complete the task.'
  3. const FOLLOWUP_PROMPT = 'Different question now: what is 3+3? Reply with only "6".'
  4. function looksLikeAttemptCompletionToolUse(event: StreamEvent): boolean {
  5. if (event.type !== "tool_use") {
  6. return false
  7. }
  8. if (event.tool_use?.name === "attempt_completion") {
  9. return true
  10. }
  11. const content = event.content ?? ""
  12. return content.includes('"tool":"attempt_completion"') || content.includes('"name":"attempt_completion"')
  13. }
  14. function validateFollowupResult(text: string): void {
  15. if (text.trim().length === 0) {
  16. throw new Error("follow-up produced an empty result")
  17. }
  18. }
  19. async function main() {
  20. const startRequestId = `start-${Date.now()}`
  21. const followupRequestId = `message-${Date.now()}`
  22. const shutdownRequestId = `shutdown-${Date.now()}`
  23. let initSeen = false
  24. let sentFollowup = false
  25. let sentShutdown = false
  26. let sawAttemptCompletion = false
  27. let sawFollowupUserTurn = false
  28. let sawMisroutedToolResult = false
  29. let followupResult = ""
  30. let sawFirstAssistantChunkForStart = false
  31. await runStreamCase({
  32. onEvent(event: StreamEvent, context) {
  33. if (event.type === "system" && event.subtype === "init" && !initSeen) {
  34. initSeen = true
  35. context.sendCommand({
  36. command: "start",
  37. requestId: startRequestId,
  38. prompt: START_PROMPT,
  39. })
  40. return
  41. }
  42. if (event.type === "control" && event.subtype === "error") {
  43. throw new Error(
  44. `received control error for requestId=${event.requestId ?? "unknown"} command=${event.command ?? "unknown"} code=${event.code ?? "unknown"} content=${event.content ?? ""}`,
  45. )
  46. }
  47. if (!sawAttemptCompletion && looksLikeAttemptCompletionToolUse(event)) {
  48. sawAttemptCompletion = true
  49. if (!sentFollowup) {
  50. context.sendCommand({
  51. command: "message",
  52. requestId: followupRequestId,
  53. prompt: FOLLOWUP_PROMPT,
  54. })
  55. sentFollowup = true
  56. }
  57. return
  58. }
  59. if (
  60. event.type === "assistant" &&
  61. event.requestId === startRequestId &&
  62. event.done !== true &&
  63. !sawFirstAssistantChunkForStart
  64. ) {
  65. sawFirstAssistantChunkForStart = true
  66. if (!sentFollowup) {
  67. context.sendCommand({
  68. command: "message",
  69. requestId: followupRequestId,
  70. prompt: FOLLOWUP_PROMPT,
  71. })
  72. sentFollowup = true
  73. }
  74. return
  75. }
  76. if (
  77. event.type === "tool_result" &&
  78. event.requestId === followupRequestId &&
  79. typeof event.content === "string" &&
  80. event.content.includes("<user_message>")
  81. ) {
  82. sawMisroutedToolResult = true
  83. return
  84. }
  85. if (event.type === "user" && event.requestId === followupRequestId) {
  86. sawFollowupUserTurn = typeof event.content === "string" && event.content.includes("3+3")
  87. return
  88. }
  89. if (event.type === "result" && event.done === true && event.requestId === startRequestId && !sentFollowup) {
  90. context.sendCommand({
  91. command: "message",
  92. requestId: followupRequestId,
  93. prompt: FOLLOWUP_PROMPT,
  94. })
  95. sentFollowup = true
  96. return
  97. }
  98. if (event.type !== "result" || event.done !== true || event.requestId !== followupRequestId) {
  99. return
  100. }
  101. followupResult = event.content ?? ""
  102. validateFollowupResult(followupResult)
  103. if (sawMisroutedToolResult) {
  104. throw new Error("follow-up message was misrouted into tool_result (<user_message>), old bug reproduced")
  105. }
  106. if (!sawFollowupUserTurn) {
  107. throw new Error("follow-up did not appear as a normal user turn in stream output")
  108. }
  109. console.log(`[PASS] saw attempt_completion tool use: ${sawAttemptCompletion}`)
  110. console.log(`[PASS] saw start assistant chunk before follow-up: ${sawFirstAssistantChunkForStart}`)
  111. console.log(`[PASS] follow-up user turn observed: ${sawFollowupUserTurn}`)
  112. console.log(`[PASS] follow-up result: "${followupResult}"`)
  113. if (!sentShutdown) {
  114. context.sendCommand({
  115. command: "shutdown",
  116. requestId: shutdownRequestId,
  117. })
  118. sentShutdown = true
  119. }
  120. },
  121. onTimeoutMessage() {
  122. return [
  123. "timed out waiting for follow-up validation",
  124. `initSeen=${initSeen}`,
  125. `sentFollowup=${sentFollowup}`,
  126. `sawAttemptCompletion=${sawAttemptCompletion}`,
  127. `sawFirstAssistantChunkForStart=${sawFirstAssistantChunkForStart}`,
  128. `sawFollowupUserTurn=${sawFollowupUserTurn}`,
  129. `sawMisroutedToolResult=${sawMisroutedToolResult}`,
  130. `haveFollowupResult=${Boolean(followupResult)}`,
  131. ].join(" ")
  132. },
  133. })
  134. }
  135. main().catch((error) => {
  136. console.error(`[FAIL] ${error instanceof Error ? error.message : String(error)}`)
  137. process.exit(1)
  138. })