trace-utils.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { describe, expect, it } from 'vitest'
  2. import { getThreadPartsFromThread } from './trace-utils'
  3. // Sanitized mock of the thread shape returned by trace.getThread().
  4. const MOCK_THREAD = [
  5. {
  6. role: 'system',
  7. content: 'System instructions omitted for fixture.',
  8. },
  9. {
  10. role: 'assistant',
  11. content: "The user's current project is Acme Analytics.",
  12. },
  13. {
  14. role: 'user',
  15. content: 'What did we decide earlier?',
  16. },
  17. {
  18. role: 'assistant',
  19. content: [
  20. {
  21. type: 'text',
  22. text: 'We decided to add an orders table with RLS policies before generating sample data.',
  23. },
  24. ],
  25. },
  26. {
  27. role: 'user',
  28. content: 'Can you create that orders table now?',
  29. },
  30. {
  31. role: 'assistant',
  32. id: null,
  33. content: [
  34. {
  35. type: 'tool_call',
  36. tool_name: 'rename_chat',
  37. tool_call_id: 'call_dummy_rename',
  38. arguments: {
  39. type: 'valid',
  40. value: {
  41. newName: 'Create Orders Table',
  42. },
  43. },
  44. },
  45. ],
  46. },
  47. {
  48. role: 'tool',
  49. content: [
  50. {
  51. type: 'tool_result',
  52. tool_name: 'rename_chat',
  53. tool_call_id: 'call_dummy_rename',
  54. output: {
  55. status: 'Chat request sent to client',
  56. },
  57. },
  58. ],
  59. },
  60. {
  61. role: 'assistant',
  62. id: null,
  63. content: [
  64. {
  65. type: 'tool_call',
  66. tool_name: 'load_knowledge',
  67. tool_call_id: 'call_dummy_knowledge',
  68. arguments: {
  69. type: 'valid',
  70. value: {
  71. name: 'database',
  72. },
  73. },
  74. },
  75. {
  76. type: 'tool_call',
  77. tool_name: 'execute_sql',
  78. tool_call_id: 'call_dummy_sql',
  79. arguments: {
  80. type: 'valid',
  81. value: {
  82. sql: 'create table public.orders (id bigint generated by default as identity primary key);',
  83. },
  84. },
  85. },
  86. ],
  87. },
  88. {
  89. role: 'tool',
  90. content: [
  91. {
  92. type: 'tool_result',
  93. tool_name: 'load_knowledge',
  94. tool_call_id: 'call_dummy_knowledge',
  95. output: 'Knowledge fixture omitted.',
  96. },
  97. {
  98. type: 'tool_result',
  99. tool_name: 'execute_sql',
  100. tool_call_id: 'call_dummy_sql',
  101. output: {
  102. type: 'text',
  103. text: 'SQL executed successfully.',
  104. },
  105. },
  106. ],
  107. },
  108. {
  109. role: 'assistant',
  110. id: null,
  111. content:
  112. 'I created the public.orders table. You should add RLS policies before exposing it to users.',
  113. },
  114. ]
  115. describe('getThreadPartsFromThread', () => {
  116. it('parses a sanitized Braintrust trace.getThread payload', () => {
  117. expect(getThreadPartsFromThread(MOCK_THREAD)).toEqual({
  118. projectContext: "The user's current project is Acme Analytics.",
  119. priorConversation:
  120. '[user]\nWhat did we decide earlier?\n\n[assistant]\nWe decided to add an orders table with RLS policies before generating sample data.',
  121. currentUserInput: 'Can you create that orders table now?',
  122. lastAssistantTurn:
  123. '[assistant]\n[called rename_chat]\n\n[assistant]\n[called load_knowledge]\n[called execute_sql]\n\n[assistant]\nI created the public.orders table. You should add RLS policies before exposing it to users.',
  124. })
  125. })
  126. it('uses the most recent project context message', () => {
  127. expect(
  128. getThreadPartsFromThread([
  129. {
  130. role: 'assistant',
  131. content: "The user's current project is Old Project.",
  132. },
  133. ...MOCK_THREAD,
  134. ])
  135. ).toMatchObject({
  136. projectContext: "The user's current project is Acme Analytics.",
  137. })
  138. })
  139. it('returns prior conversation without current turn parts when there is no user message', () => {
  140. expect(
  141. getThreadPartsFromThread([
  142. {
  143. role: 'assistant',
  144. content: 'I can help with your Briven project.',
  145. },
  146. ])
  147. ).toEqual({
  148. projectContext: null,
  149. priorConversation: '[assistant]\nI can help with your Briven project.',
  150. currentUserInput: null,
  151. lastAssistantTurn: null,
  152. })
  153. })
  154. })