From 854fba4f766491e9251734105a663430f22a6145 Mon Sep 17 00:00:00 2001 From: solomon2773 Date: Thu, 27 Aug 2026 15:37:03 -0500 Subject: [PATCH] feat: add read-only customer AI assistant (#9) --- docs/guides/ai-support.md | 20 ++++- src/app/admin/settings/page.tsx | 12 +++ src/app/dashboard/assistant/page.tsx | 29 ++++++ src/app/dashboard/layout.tsx | 1 + src/components/customer-assistant.tsx | 123 ++++++++++++++++++++++++++ src/lib/actions/assistant.ts | 87 ++++++++++++++++++ src/lib/i18n.ts | 5 ++ src/lib/services/ai.ts | 94 ++++++++++++++++++++ src/lib/settings.ts | 3 + 9 files changed, 373 insertions(+), 1 deletion(-) create mode 100644 src/app/dashboard/assistant/page.tsx create mode 100644 src/components/customer-assistant.tsx create mode 100644 src/lib/actions/assistant.ts diff --git a/docs/guides/ai-support.md b/docs/guides/ai-support.md index 6d3a065..a483829 100644 --- a/docs/guides/ai-support.md +++ b/docs/guides/ai-support.md @@ -28,6 +28,8 @@ priority, and confidence gates. | Minimum triage confidence | 0.7 | Below this the customer's own choices are kept | | Auto-resolve grounded tier-1 tickets | off | Posts and closes only a non-urgent answer grounded in published articles | | Minimum auto-resolve confidence | 0.92 | Below this the ticket remains open for staff | +| Read-only customer assistant | off | Adds a client-area chat over published articles and that customer's account summary | +| Customer questions per hour | 20 | Per-account cost and abuse limit; `0` stops requests | ## Reply drafts @@ -85,11 +87,27 @@ the same database transaction, and `ticket.auto_resolved` records its confidence and source article IDs in the audit log. If a staff/customer update races the model response, the optimistic write fails and no automated message is posted. +## Customer assistant + +When enabled, **Dashboard → AI assistant** provides a read-only chat grounded in: + +- published knowledgebase articles; +- up to 50 of the signed-in customer's own services; and +- up to 30 of that customer's recent invoices. + +The assistant receives no service configuration, credentials, payment methods, +or other customers' data. It cannot mutate the account or claim that it took an +action. Requests are rate-limited per account through audited successful calls. +The customer can send the visible transcript to the normal support queue with +**Escalate to ticket** whenever a person or account change is needed. + ## Costs and privacy - You are billed by your provider for the tokens each feature uses. A reply draft and auto-resolution send published knowledgebase content plus relevant - ticket text; classification sends the subject and first message. + ticket text; classification sends the subject and first message. Customer + assistant requests send the published knowledgebase and the requesting + customer's limited account summary. - Ticket content and knowledgebase articles are sent to the provider you configured, when a feature runs. Customer passwords, payment details and API keys are never part of a prompt. diff --git a/src/app/admin/settings/page.tsx b/src/app/admin/settings/page.tsx index 3de1c3c..0aac45a 100644 --- a/src/app/admin/settings/page.tsx +++ b/src/app/admin/settings/page.tsx @@ -117,6 +117,18 @@ const SECTIONS: Array<{ type: "text", help: "Defaults to 0.92. Lower thresholds increase the risk of an incorrect automatic answer.", }, + { + key: "ai_customer_assistant", + label: "Read-only customer assistant", + type: "checkbox", + help: "Lets signed-in customers ask about published documentation and their own services or invoices.", + }, + { + key: "ai_customer_assistant_hourly_limit", + label: "Customer assistant questions per hour", + type: "number", + help: "Per account. Set to 0 to disable customer requests even when the assistant is enabled.", + }, ], }, { diff --git a/src/app/dashboard/assistant/page.tsx b/src/app/dashboard/assistant/page.tsx new file mode 100644 index 0000000..09f72e6 --- /dev/null +++ b/src/app/dashboard/assistant/page.tsx @@ -0,0 +1,29 @@ +import { CustomerAssistant } from "@/components/customer-assistant"; +import { requireUser } from "@/lib/auth"; +import { customerAssistantEnabled } from "@/lib/services/ai"; + +export const metadata = { title: "AI assistant" }; + +export default async function CustomerAssistantPage() { + await requireUser(); + const enabled = await customerAssistantEnabled(); + + return ( +
+

AI assistant

+

+ Read-only answers grounded in published help articles and your account. +

+
+ {enabled ? ( + + ) : ( +
+ The customer assistant is not enabled for this installation. You can + still contact the support team from the Tickets page. +
+ )} +
+
+ ); +} diff --git a/src/app/dashboard/layout.tsx b/src/app/dashboard/layout.tsx index bc2bb7b..4d7f39b 100644 --- a/src/app/dashboard/layout.tsx +++ b/src/app/dashboard/layout.tsx @@ -13,6 +13,7 @@ const NAV: Array<{ href: string; label: MessageKey }> = [ { href: "/dashboard/invoices", label: "dash.nav.invoices" }, { href: "/dashboard/quotes", label: "dash.nav.quotes" }, { href: "/dashboard/tickets", label: "dash.nav.support" }, + { href: "/dashboard/assistant", label: "dash.nav.assistant" }, { href: "/dashboard/account/billing", label: "dash.nav.billing" }, { href: "/dashboard/affiliate", label: "dash.nav.affiliate" }, { href: "/dashboard/account", label: "dash.nav.account" }, diff --git a/src/components/customer-assistant.tsx b/src/components/customer-assistant.tsx new file mode 100644 index 0000000..7415e90 --- /dev/null +++ b/src/components/customer-assistant.tsx @@ -0,0 +1,123 @@ +"use client"; + +import { useActionState, useEffect, useRef, useState } from "react"; +import { useFormStatus } from "react-dom"; +import { askCustomerAssistant } from "@/lib/actions/assistant"; +import { createTicket } from "@/lib/actions/client"; +import { ActionForm } from "@/components/forms"; + +type Message = { role: "user" | "assistant"; content: string }; + +function AskButton() { + const { pending } = useFormStatus(); + return ( + + ); +} + +export function CustomerAssistant() { + const [state, action] = useActionState(askCustomerAssistant, null); + const [question, setQuestion] = useState(""); + const [messages, setMessages] = useState([]); + const handledResponse = useRef(null); + + useEffect(() => { + if (!state?.id || handledResponse.current === state.id) return; + handledResponse.current = state.id; + setMessages((current) => [ + ...current, + { role: "user", content: state.question }, + { role: "assistant", content: state.answer }, + ]); + setQuestion(""); + }, [state]); + + const transcript = messages.length + ? messages + .map( + (message) => + `${message.role === "user" ? "Customer" : "Assistant"}: ${message.content}`, + ) + .join("\n\n") + : "Customer requested help from the read-only assistant and asked to escalate to support."; + const lastQuestion = [...messages] + .reverse() + .find((message) => message.role === "user")?.content; + + return ( +
+
+ {messages.length === 0 && ( +
+

How can I help?

+

+ I can explain published documentation and summarize your services + or invoices. I cannot change your account or operate a service. +

+
+ )} + {messages.map((message, index) => ( +
+ {message.content} +
+ ))} +
+ + {state?.error && ( +

+ {state.error} +

+ )} + +
+ +