All files / web/src/mocks/handlers agents.ts

0% Statements 0/156
0% Branches 0/1
0% Functions 0/1
0% Lines 0/156

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176                                                                                                                                                                                                                                                                                                                                                               
import { http, HttpResponse, delay } from 'msw'
import type { StoredAgent } from '@/types/generated/StoredAgent'
import demoAgents from '../data/agents.json'
import { chatHistories } from '../data/agent-chat-history'
 
// BigInt values must be converted to numbers/strings for JSON.stringify()
const toJsonAgent = (agent: StoredAgent): any => ({
  ...agent,
  created_at: Number(agent.created_at),
  updated_at: Number(agent.updated_at)
})
 
const convertToStoredAgent = (agent: any): StoredAgent => ({
  ...agent,
  created_at: BigInt(agent.created_at),
  updated_at: BigInt(agent.updated_at)
})
 
let agents: StoredAgent[] = demoAgents.map(convertToStoredAgent)
 
export const agentHandlers = [
  http.get('/api/agents', () => {
    return HttpResponse.json({
      success: true,
      data: agents.map(toJsonAgent)
    })
  }),
 
  http.get('/api/agents/:id', ({ params }) => {
    const agent = agents.find(a => a.id === params.id)
    if (!agent) {
      return HttpResponse.json(
        {
          success: false,
          message: 'Agent not found'
        },
        { status: 404 }
      )
    }
    return HttpResponse.json({
      success: true,
      data: toJsonAgent(agent)
    })
  }),
 
  http.post('/api/agents', async ({ request }) => {
    const body = await request.json() as Partial<StoredAgent>
 
    if (body.id && agents.find(a => a.id === body.id)) {
      return HttpResponse.json(
        {
          success: false,
          message: `Agent with ID ${body.id} already exists`
        },
        { status: 409 }
      )
    }
 
    const newAgent: StoredAgent = {
      id: body.id || 'demo-agent-' + Date.now(),
      name: body.name || 'Untitled Agent',
      agent: body.agent || {
        model: 'claude-sonnet-4.5',
        prompt: null,
        temperature: null,
        api_key_config: null,
        tools: null
      },
      created_at: BigInt(Date.now()),
      updated_at: BigInt(Date.now())
    }
    agents.push(newAgent)
    return HttpResponse.json(
      {
        success: true,
        data: toJsonAgent(newAgent)
      },
      { status: 201 }
    )
  }),
 
  http.put('/api/agents/:id', async ({ params, request }) => {
    const index = agents.findIndex(a => a.id === params.id)
    if (index === -1) {
      return HttpResponse.json(
        {
          success: false,
          message: 'Agent not found'
        },
        { status: 404 }
      )
    }
    const body = await request.json() as Partial<StoredAgent>
    const currentAgent = agents[index]
    if (!currentAgent) {
      return HttpResponse.json(
        {
          success: false,
          message: 'Agent not found'
        },
        { status: 404 }
      )
    }
    agents[index] = {
      ...currentAgent,
      ...body,
      id: currentAgent.id,  // Ensure id is preserved
      updated_at: BigInt(Date.now())
    } as StoredAgent
    return HttpResponse.json({
      success: true,
      data: toJsonAgent(agents[index]!)
    })
  }),
 
  http.delete('/api/agents/:id', ({ params }) => {
    const index = agents.findIndex(a => a.id === params.id)
    if (index === -1) {
      return HttpResponse.json(
        {
          success: false,
          message: 'Agent not found'
        },
        { status: 404 }
      )
    }
    agents.splice(index, 1)
    return HttpResponse.json({
      success: true
    })
  }),
 
  http.post('/api/agents/:id/execute', async ({ params }) => {
    const agent = agents.find(a => a.id === params.id)
    if (!agent) {
      return HttpResponse.json(
        {
          success: false,
          message: 'Agent not found'
        },
        { status: 404 }
      )
    }
 
    await delay(1200)
 
    return HttpResponse.json({
      success: true,
      data: {
        response: `[Demo] This is a sample execution result for ${agent.name}. In a real environment, this would be the actual response from the AI model.`
      }
    })
  }),
 
  http.post('/api/agents/execute-inline', async () => {
    await delay(1000)
 
    return HttpResponse.json({
      success: true,
      data: {
        response: '[Demo] This is a sample execution result for inline agent.'
      }
    })
  }),
 
  http.get('/api/agents/:id/chat-history', ({ params }) => {
    const agentId = params.id as string
    const history = chatHistories[agentId] || []
 
    return HttpResponse.json({
      success: true,
      data: history
    })
  })
]