feat: P4.6 agent demo + escalate button + support seed/smoke (P4 complete)

apps/agent-demo (Vite): agent sets AVAILABLE, sees assigned tickets, opens the
thread, replies. message-demo customer pane gains 'escalate to support' (via
SupportProvider/useEscalate). seed-support + smoke-support prove the loop:
escalate -> auto-assign -> agent reply -> customer receives. 45 tests; realtime/
inbox/support smokes all pass; both demos build.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-01 12:09:29 +05:30
parent c53258c8eb
commit 7441d0ec36
11 changed files with 309 additions and 3 deletions
@@ -0,0 +1,25 @@
// Seed a support queue + one available agent. Run against a running service.
import 'dotenv/config';
import jwt from 'jsonwebtoken';
const SERVICE = process.env.SMOKE_URL ?? 'http://localhost:3200';
const APP_ID = 'portal-demo';
const SECRET = JSON.parse(process.env.APP_SECRETS ?? '{"portal-demo":"dev-secret"}')[APP_ID];
const sign = (u) => jwt.sign({ sub: u, name: u, appId: APP_ID, orgId: `org_${APP_ID}` }, SECRET, { algorithm: 'HS256', expiresIn: '2h' });
async function api(path, method, token, body) {
const r = await fetch(`${SERVICE}${path}`, {
method,
headers: { 'content-type': 'application/json', authorization: `Bearer ${token}` },
body: body ? JSON.stringify(body) : undefined,
});
if (!r.ok) throw new Error(`${method} ${path}${r.status}`);
return r.json();
}
const agentToken = sign(process.env.AGENT_ID ?? 'agent1');
const queue = await api('/v1/support/queues', 'POST', agentToken, { name: 'Support' });
await api(`/v1/support/queues/${queue.id}/members`, 'POST', agentToken);
await api('/v1/support/members/me/availability', 'PATCH', agentToken, { state: 'AVAILABLE' });
console.log(`seeded queue ${queue.id}; agent AVAILABLE`);
process.exit(0);
@@ -0,0 +1,66 @@
// P4 support smoke: customer escalates a chat → ticket created + auto-assigned to
// an available agent → agent (now a thread participant) replies → customer sees it
// live. Requires: service running (relay timer on) + `node scripts/seed-support.mjs`.
import 'dotenv/config';
import { io } from 'socket.io-client';
import jwt from 'jsonwebtoken';
const SERVICE = process.env.SMOKE_URL ?? 'http://localhost:3200';
const APP_ID = 'portal-demo';
const SECRET = JSON.parse(process.env.APP_SECRETS ?? '{"portal-demo":"dev-secret"}')[APP_ID];
const sign = (u) => jwt.sign({ sub: u, name: u, appId: APP_ID, orgId: `org_${APP_ID}` }, SECRET, { algorithm: 'HS256', expiresIn: '1h' });
const connect = (u) => io(`${SERVICE}/message`, { auth: { token: sign(u) }, transports: ['websocket'], forceNew: true });
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const assert = (c, m) => { if (!c) { console.error('✗', m); process.exit(1); } console.log('✓', m); };
async function api(path, method, token, body) {
const r = await fetch(`${SERVICE}${path}`, {
method,
headers: { 'content-type': 'application/json', authorization: `Bearer ${token}` },
body: body ? JSON.stringify(body) : undefined,
});
if (!r.ok) throw new Error(`${method} ${path}${r.status}`);
return r.json();
}
async function pollUntil(fn, ms = 8000) {
const end = Date.now() + ms;
while (Date.now() < end) { const v = await fn(); if (v) return v; await sleep(300); }
return null;
}
function waitForMessage(socket, pred, ms = 6000) {
return new Promise((res) => {
const to = setTimeout(() => res(null), ms);
const h = (m) => { if (pred(m)) { socket.off('message', h); clearTimeout(to); res(m); } };
socket.on('message', h);
});
}
const custToken = sign('cust');
const cust = connect('cust');
const agent = connect(process.env.AGENT_ID ?? 'agent1');
try {
const { threadId } = await cust.emitWithAck('open_thread', {});
await cust.emitWithAck('send_message', { threadId, content: 'my payment failed' });
const ticket = await api('/v1/support/escalate', 'POST', custToken, { threadId });
assert(!!ticket.id, `customer escalated → ticket ${ticket.id} (${ticket.state})`);
const assigned = await pollUntil(async () => {
const ts = await api('/v1/support/tickets?scope=mine', 'GET', custToken);
const t = ts.find((x) => x.id === ticket.id);
return t && t.state === 'OPEN' && t.assignedActorId ? t : null;
});
assert(assigned, 'ticket auto-assigned to an agent (state OPEN)');
await agent.emitWithAck('open_thread', { threadId });
const gotReply = waitForMessage(cust, (m) => m.content === 'Hi, I can help with that.');
await agent.emitWithAck('send_message', { threadId, content: 'Hi, I can help with that.' });
assert(await gotReply, 'customer received the agent reply live');
console.log('\nP4 support smoke: PASS');
} finally {
cust.close();
agent.close();
}
process.exit(0);