import { mutation, t, table } from "@bijectionhq/bijection";
import { send } from "@bijectionhq/app-agents";
import { clerk, desk } from "./access";
import { support } from "./agents";
export const tickets = table({ subject: t.string, reply: t.maybe(t.string) }, { label: desk });
export const triage = mutation(
{ under: desk, allow: [clerk], args: { id: t.key(tickets) }, refuses: { NoTicket: null } },
async (ctx, { id }) => {
const ticket = await ctx.get(tickets(id));
if (ticket === null) return ctx.refuse("NoTicket", "No such ticket");
await send(ctx, support, { agent: "support", participant: ctx.frame.as.id, text: ticket.subject }, {
once: ["ticket", id],
within: { minutes: 10 },
then: replied.with({ id }),
});
return null;
},
);
export const replied = mutation(
{ under: desk, args: { id: t.key(tickets), result: t.result(support.sent) } },
async (ctx, { id, result }) => {
const reply = result.ok && result.value.status === "completed" ? result.value.text : null;
await ctx.patch(tickets(id), { reply });
return null;
},
);