File size: 6,805 Bytes
702c725
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# ALAS Backend API

FastAPI backend for the ALAS workplace soft-skills coaching app (`main.py`). It drives an LLM-played character through a scripted scenario, extracts live coaching notes as the conversation progresses, and produces a rubric-based assessment on demand.

- **Base URL (local):** `http://localhost:8000`
- **Run:** `uv run uvicorn main:app --reload --port 8000`
- **Interactive docs:** `/docs` (Swagger UI) and `/redoc`, auto-generated by FastAPI
- **Content type:** `application/json` for all requests/responses except `/chats/{chat_id}/stream`, which returns `text/event-stream`

## Session model

State is kept **in memory** (`store: dict[chat_id -> session]`) β€” there is no database, and all sessions are lost on server restart. A session holds:

| Field | Description |
|---|---|
| `agent` | `CharacterAgent` instance (LangChain-backed) that plays the AI role and holds conversation history |
| `scenario_id` | The scenario this chat was started with |
| `messages` | Ordered list of `{role, text, turn}` β€” the full transcript |
| `notes` | Accumulated coaching notes, appended asynchronously |
| `pending_turns` | Trainee turn numbers whose note extraction is still running in the background |

A session must be created via `POST /chats` before any of the `/chats/{chat_id}/...` endpoints can be used, and every endpoint that takes a `chat_id` returns **404** if it doesn't exist (see [Errors](#errors)).

## Endpoints

### `GET /scenarios`

All available scenarios, for a scenario picker UI.

**Response 200** β€” `ScenariosResponse`
```json
{
  "scenarios": [
    {
      "id": "onboarding_report",
      "title": "string",
      "skill_area": "string",
      "ai_name": "string",
      "ai_role": "string",
      "learner_role": "string",
      "learning_goal": "string",
      "has_autoplay": true
    }
  ]
}
```
Known scenario ids in this repo: `onboarding_report` (default), `coaching_employee`, `guest_price_complaint`.

---

### `POST /chats`

Start a new chat session for a scenario. Returns a fresh `chat_id` and any scripted opening message from the AI character.

**Request body** β€” `NewChatRequest` (optional; defaults to `onboarding_report`)
```json
{ "scenario_id": "onboarding_report" }
```

**Response 201** β€” `NewChatResponse`
```json
{
  "chat_id": "uuid",
  "scenario_id": "onboarding_report",
  "messages": [
    { "role": "manager", "text": "string", "turn": 0 }
  ]
}
```
`messages` is empty if the scenario doesn't open with an AI line (`opens == "learner"`).

**Errors**
- `400` β€” unknown `scenario_id`

---

### `GET /chats/{chat_id}`

Retrieve the full message history for a session.

**Response 200** β€” `ChatHistoryResponse`
```json
{
  "chat_id": "uuid",
  "messages": [
    { "role": "trainee", "text": "string", "turn": 1 },
    { "role": "manager", "text": "string", "turn": 1 }
  ]
}
```

**Errors**
- `404` β€” unknown `chat_id`

---

### `POST /chats/{chat_id}/message`

Send a trainee message and get the full AI reply back in one blocking response. Note extraction for this turn is kicked off as a FastAPI background task (non-blocking).

**Request body** β€” `ChatRequest`
```json
{ "message": "string" }
```

**Response 200** β€” `ChatResponse`
```json
{
  "chat_id": "uuid",
  "reply": "string",
  "turn": 1,
  "messages": [ { "role": "trainee|manager", "text": "string", "turn": 1 } ]
}
```

**Errors**
- `404` β€” unknown `chat_id`

---

### `POST /chats/{chat_id}/stream`

Same as `/message`, but streams the AI reply token-by-token over **Server-Sent Events**.

**Request body** β€” `ChatRequest` (same as above)

**Response 200** β€” `text/event-stream`
```
data: Hel
data: lo
data: !
data: [DONE]

```
Each `data:` line is a raw token chunk (not JSON β€” concatenate to reassemble the reply). The stream always ends with a `data: [DONE]` sentinel. Note extraction is scheduled on a background thread once streaming completes and is *not* reflected in this response β€” poll `GET /chats/{chat_id}/notes` and watch `pending_turns`.

**Errors**
- `404` β€” unknown `chat_id`

---

### `GET /skills`

The rubric's skill list, in display order. Same set for every scenario β€” lets a UI render empty sections before any notes exist.

**Response 200** β€” `SkillsResponse`
```json
{ "skills": [ { "slug": "string", "name": "string", "order": 0 } ] }
```

---

### `GET /chats/{chat_id}/notes`

Flat, insertion-ordered list of coaching notes extracted so far. Grouping by skill is left to the caller.

**Response 200** β€” `NotesResponse`
```json
{
  "chat_id": "uuid",
  "notes": [
    { "skill": "string", "note": "string", "evidence": "string", "turn": 1 }
  ],
  "pending_turns": [2]
}
```
`pending_turns` lists trainee turns whose extraction is still running in the background β€” poll this endpoint until a turn drops out of the list to know its notes (possibly none) are final.

**Errors**
- `404` β€” unknown `chat_id`

---

### `POST /chats/{chat_id}/assessment`

Score the conversation so far against the rubric's skills. The result is **cached on the session** β€” repeat calls for the same `chat_id` return the same assessment without re-invoking the LLM.

**Response 200** β€” `Assessment`
```json
{
  "overall_summary": "string",
  "grades": [
    {
      "skill": "string",
      "level": "Low | Medium | High",
      "reasoning": "string",
      "evidence_turns": [1, 3]
    }
  ],
  "key_moment": "string",
  "coaching_suggestion": "string",
  "readiness_score": 7
}
```

**Errors**
- `400` β€” no messages yet to assess, or no rubric skills configured for the scenario
- `404` β€” unknown `chat_id`

---

### `DELETE /chats/{chat_id}`

Drop a session and free its in-memory state.

**Response** β€” `204 No Content`

**Errors**
- `404` β€” unknown `chat_id`

---

### `GET /health`

Liveness/readiness probe.

**Response 200**
```json
{ "status": "ok", "active_sessions": 3 }
```

## Errors

All error responses use FastAPI's default shape:
```json
{ "detail": "chat_id '...' not found" }
```

| Status | Meaning |
|---|---|
| 400 | Bad request β€” unknown `scenario_id`, empty conversation on assessment, or no rubric configured |
| 404 | `chat_id` not found (session expired, deleted, or server restarted) |
| 422 | Request body failed Pydantic validation (e.g. missing `message` field) |

## Typical flow

1. `GET /scenarios` β†’ let the user pick a scenario
2. `POST /chats` β†’ get a `chat_id`
3. Repeat: `POST /chats/{chat_id}/message` (or `/stream`) for each trainee turn
4. `GET /chats/{chat_id}/notes` β†’ poll for live coaching notes, checking `pending_turns`
5. `POST /chats/{chat_id}/assessment` β†’ final rubric-based score once the conversation is done
6. `DELETE /chats/{chat_id}` β†’ clean up when finished

A minimal reference client for this exact flow lives in [api_client.py](api_client.py).