File size: 714 Bytes
94e1b2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import type {
  StudioAssistantMessage,
  StudioPartStore
} from '../domain/types'

const DOOM_LOOP_THRESHOLD = 3

export async function isDoomLoop(input: {
  assistantMessage: StudioAssistantMessage
  partStore: StudioPartStore
  toolName: string
  toolInput: Record<string, unknown>
}): Promise<boolean> {
  const latestParts = await input.partStore.listByMessageId(input.assistantMessage.id)
  const lastThree = latestParts.slice(-DOOM_LOOP_THRESHOLD)

  return (
    lastThree.length === DOOM_LOOP_THRESHOLD &&
    lastThree.every(
      (part) =>
        part.type === 'tool' &&
        part.tool === input.toolName &&
        JSON.stringify(part.state.input) === JSON.stringify(input.toolInput)
    )
  )
}