> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nextmed.med.br/llms.txt
> Use this file to discover all available pages before exploring further.

# Chat e IA

> Contratos de chat medico, sugestoes, transcricao e anamnese com OpenAI.

## Chat e IA

## Modelo de mensagem

```ts theme={"system"}
type Message = {
  id: string;
  content: string;
  role: "user" | "assistant";
  timestamp: Date;
  type: "text" | "audio-transcribed";
  audioUrl?: string;
  suggestions?: string[];
  suggestionsLoading?: boolean;
};
```

## Chat por encaminhamento

```http theme={"system"}
POST /api/chat/message
```

Body:

```ts theme={"system"}
type ChatMessageRequest = {
  referralId: string;
  message: string;
  history: Message[];
};
```

Resposta:

* `200 text/event-stream`.
* cada chunk e emitido no formato:

```text theme={"system"}
0:{"type":"text-delta","textDelta":"conteudo"}
```

## Acoes rapidas

```http theme={"system"}
POST /api/chat/quick-action
```

Body:

```ts theme={"system"}
type QuickActionRequest = {
  referralId: string;
  action: "suggestion" | "summary" | "anamnesis" | "diagnosis";
  history: Message[];
};
```

Resposta:

* `200 text/event-stream` com chunks `text-delta`.

## Sugestoes de proximo passo

```http theme={"system"}
POST /api/chat/suggestions
```

Body:

```ts theme={"system"}
type ChatSuggestionsRequest = {
  referralId: string;
  doctorMessage: string;
  assistantMessage: string;
  history: Message[];
};
```

Resposta:

```json theme={"system"}
{
  "suggestions": [
    "Sugestao curta.",
    "Outra sugestao curta."
  ]
}
```

## Transcricao de audio

```http theme={"system"}
POST /api/chat/transcribe
```

Content-Type:

```http theme={"system"}
multipart/form-data
```

Campo:

```text theme={"system"}
audio: File
```

Formatos aceitos: `mp3`, `wav`, `m4a`, `webm`, `ogg`.

Limite: `10 MB`.

Resposta:

```json theme={"system"}
{
  "transcription": "texto transcrito"
}
```

## Chat de anamnese

```http theme={"system"}
POST /api/anamnesis-chat
```

Body:

```ts theme={"system"}
type AnamnesisChatRequest = {
  messages: Array<{
    role: "user" | "assistant";
    content: string;
  }>;
  patient: {
    name: string;
    birthdate: string;
    gender: "male" | "female";
    is_neonate: boolean;
    is_foreigner: boolean;
  };
  complaint: string;
  initialAnamnesis?: string;
  conversationHistory?: string;
};
```

Resposta:

* `200 text/event-stream` com chunks `text-delta`.
* quando a IA considera a anamnese completa, o conteudo pode conter `ANAMNESE_COMPLETA:`.

## Gerar anamnese final

```http theme={"system"}
POST /api/anamnesis-chat/generate
```

Body:

```ts theme={"system"}
type GenerateAnamnesisRequest = {
  patient: {
    name: string;
    birthdate: string;
    gender: "male" | "female";
    is_neonate: boolean;
    is_foreigner: boolean;
  };
  complaint: string;
  initialAnamnesis?: string;
  conversationHistory: string;
};
```

Resposta:

```json theme={"system"}
{
  "anamnesis": "texto final"
}
```

## Avaliar anamnese e possivel AVC

```http theme={"system"}
POST /api/anamnesis-chat/evaluate
```

Body:

```ts theme={"system"}
type EvaluateAnamnesisRequest = {
  anamnesis: string;
  complaint: string;
  patient: {
    name: string;
    birthdate: string;
    gender: "male" | "female";
    is_neonate: boolean;
    is_foreigner: boolean;
  };
};
```

Resposta:

```ts theme={"system"}
type EvaluateAnamnesisResponse = {
  sufficient: boolean;
  reason: string;
  message: string;
  needsImprovement: boolean;
  suggestions?: string[];
  possibleStroke: boolean;
  strokeIndicators: string[];
};
```

Se nao houver chave OpenAI, o endpoint aplica uma validacao basica com heuristica local para possivel AVC.

## Validar queixa principal

```http theme={"system"}
POST /api/anamnesis-chat/validate
```

Body:

```ts theme={"system"}
type ValidateComplaintRequest = {
  complaint: string;
  patient: {
    name: string;
    age: number;
    gender: "male" | "female";
  };
};
```

Resposta:

```ts theme={"system"}
type ValidateComplaintResponse = {
  valid: boolean;
  reason: string;
  message: string;
  suggestion?: string;
};
```
