Fix: use embed=threads API, fix thread ordering

This commit is contained in:
Hoid 2026-02-16 20:53:28 +00:00
parent edc030e932
commit 00348babe7

View file

@ -96,8 +96,8 @@ async function cmdView(args) {
console.log(`Status: ${c.status} | Customer: ${c.customer?.email || 'unknown'} | Created: ${c.createdAt}`);
console.log('---');
// Get threads (messages)
const tRes = await apiRequest('GET', `/conversations/${id}/threads`);
// Get threads (messages) — must use embed=threads on conversation endpoint
const tRes = await apiRequest('GET', `/conversations/${id}?embed=threads`);
if (tRes.status === 200) {
const threads = tRes.data._embedded?.threads || [];
for (const t of threads) {
@ -165,13 +165,12 @@ async function cmdNeedsReply(args) {
const assignee = c.assignee?.email || c.user?.email || '';
if (assignee.toLowerCase() !== AI_AGENT_EMAIL.toLowerCase()) continue;
const tRes = await apiRequest('GET', `/conversations/${c.id}/threads`);
const tRes = await apiRequest('GET', `/conversations/${c.id}?embed=threads`);
if (tRes.status !== 200) continue;
const threads = (tRes.data._embedded?.threads || [])
.filter(t => t.type !== 'note') // ignore internal notes
.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
.filter(t => t.type !== 'note' && t.type !== 'lineitem') // ignore internal notes and line items
if (threads.length === 0) continue;
const lastThread = threads[0];
const lastThread = threads[threads.length - 1]; // threads are in chronological order
// type 'customer' = from customer, 'message' = from agent
if (lastThread.type === 'customer') {
needsReply.push(c);