ai-interactions
What it does
ai-interactions (.framework/functions/ai-interactions.ts) writes the feedback and observability side of the AI loop as items rows. It is not the conversation engine:
- Interactive turns run through
POST /api/ai-agents?action=run—runAgentin.framework/functions/_shared/agent-runner.tsdrives athreadsrow, persistsmessages, and manages the episode lease. - Stateless pipeline inference runs through the
agent_inferencestage (.framework/functions/_shared/pipeline-runner.ts) — it POSTs{ context, model, temperature, max_tokens }toconfig.webhook_urlviaintegration-outbound(generic-http), and returns a marked mock response whenwebhook_urlis unset. ai-interactionsrecords what happened around those paths: customer thumbs feedback, per-interaction telemetry rows, and draft learning proposals.
Actions (dispatched on ?action=):
| Action | Method | Purpose |
|---|---|---|
submit_feedback | POST | Record helpful/not_helpful on an AI message; escalate or resolve the ticket. |
create_interaction | POST | Insert a telemetry item (default type ai_feedback_record, overridable via type_slug). The pipeline create_interaction stage lazy-imports this handler. |
update_interaction | PATCH or POST | Merge allow-listed fields into an existing interaction item. |
list | GET | List interaction items for the caller's account (default action). |
Route and methods
/api/ai-interactions?action=<action>—public/_redirectsmaps/api/*to/.netlify/functions/:splat; the exporthandleris acreateHandler-wrapped dispatcher readingctx.query.actionandctx.query.method(populated from the HTTP method).GETwith noaction(or an unrecognized one) falls through tolist; other invalid action/method pairs throwInvalid action or method.
Authentication
createHandler resolves a principal and rejects anonymous requests with 401. There is no role gate — any authenticated human or machine principal may call the actions. list reads through ctx.db scoped to ctx.accountId; create_interaction/update_interaction write through admin-data create/update, so the caller's permissions, audit, and triggers apply. submit_feedback writes messages/items via adminDb directly (service role) — it does not check that the caller owns the ticket.
Request contract
submit_feedback (POST):
| Field | Required | Notes |
|---|---|---|
message_id | yes | messages.id of the AI reply. |
ticket_id | yes | items.id of the ticket under discussion. |
feedback | yes | 'helpful' or 'not_helpful'. |
reason | no | Free text, truncated to 2000 chars. |
create_interaction (POST): thread_id or ai_message_id required. Optional: type_slug (default ai_feedback_record; skipped silently when the type isn't installed), retrieved_sources, per_source_feedback, ai_confidence, retrieval_confidence, kb_candidate, answer_quality, assistance_outcome, eval_case_id, the learning-proposal linkage id, review_notes, reviewed_by, reviewed_at. The record is titled AI Interaction — <date>, status: 'active'.
update_interaction (PATCH/POST): id required plus ≥1 allow-listed field: kb_article_id, human_message_id, human_edits_made, customer_feedback, kb_candidate, reuse_count, per_source_feedback, answer_quality, assistance_outcome, eval_case_id, the learning-proposal linkage id, review_notes, reviewed_by, reviewed_at.
optional thread_id, ai_message_id, source_message_ids, source_chunk_ids, proposed_tags. Returns { skipped: true } when the proposal type isn't installed.
list (GET): query params feedback, kb_candidate, limit (default 50), offset (default 0).
Response contract
submit_feedback→{ message_id, ticket_id, feedback, escalated }whereescalatedisfeedback === 'not_helpful'.create_interaction→ the createditemsrecord, or{ skipped: true, reason: '<type> type not found' }when the type slug can't be resolved — not an error.update_interaction→ the admin-dataupdateresult.list→ an array ofitemsrows (filtered to theai_feedback_recordtype when that type exists).
Side effects
submit_feedbackstampsmessages.data.feedback(+feedback_reason, and fornot_helpfulafeedback_diagnosis+needs_review: true). Diagnosis is heuristic over the message's own evidence trail:missing_knowledge(no retrieved sources),retrieval_miss(mean similarity < 0.35),reasoning_error(sources retrieved but none cited), elsewrong_answer.- Ticket lifecycle side effects on the
itemsrow:not_helpfulsetsstatus: 'human_assigned'from any state excepthuman_assigned/in_progress, reopeningresolved/closedtickets (reopened_at,reopen_reason: 'thumbs_down', resolution stamps cleared).helpfulon apending_resolutionticket resolves it withresolved_via: ai_confirmed|human_confirmed(chosen frompending_proposed_by) andaim_resolved_by_ai. - The first
itemsrow whosedata.ai_message_idmatches getscustomer_feedback(+reason/diagnosis) stamped. ai_interaction.feedback_submittedaudit event plus afunnel_signalfeedback_submittedjourney signal (fire-and-forget).
Errors
| Condition | Error |
|---|---|
| Missing inputs | message_id is required · ticket_id is required · id is required · thread_id or ai_message_id is required · proposed_summary is required |
| Bad enum | feedback must be "helpful" or "not_helpful" |
| Empty patch | No updatable fields provided |
| Unknown interaction id | Interaction record not found |
| Bad dispatch | Invalid action or method |
Agent notes
- Use
type_slugoncreate_interactionto record under an app-specific item type; the{ skipped: true }return makes the call safe to fire even when the type isn't installed — check forskippedinstead of treating it as failure. submit_feedbackis a lifecycle accelerator, not just a thumbs counter: thumbs-up is the explicit customer confirmation that closes apending_resolutionticket, thumbs-down escalates and can reopen. Calling it outside the support-ticket statuses is a no-op for the ticket fields.- The pipeline
create_interactionstage resolvesai_message_id/thread_idfrom trigger data and passesconfig.type_slugthrough. listqueriesctx.db— a machine principal sees account-scoped rows viaadminDb, a human sees RLS-filtered rows.- See AI agents, prompt configs, and embeddings for how the run path, inference, and these records fit together.