跳转到内容
EN

给文档站加问答助手

给公开文档站加一个按需打开的问答窗口:点击按钮或按 ⌘K / Ctrl+K 才下载 PraxisKit,关闭后焦点回到原控件。访客不用登录,使用可公开 key 获取各自的匿名身份。下面交付一个完整页面和本地启动器。

准备公开资料与身份

需要 Bun、一个服务智能体,以及按匿名访客指南创建的可公开 key。模型使用可用的 deepseek-flash。先按网页接入指南准备测试策略;本例在 http://localhost:4242 运行,仅启用 localhost。正式站点则配置实际 HTTPS 来源和匿名限额。

Kit 的安装方式见嵌入到网页;公共选项见组件参考。下面只使用它们完成按需加载和窗口交互,不需要服务端 API Key。

此配方面向任何人都能阅读的帮助内容。若资料需要登录,请改用登录用户配方的可信签发,不能把私有文档放进匿名智能体。

配置答案范围

在「服务智能体 → 选择智能体 → 接入与配置 → 配置 → 指令」保存以下示例资料;使用你已核对的产品内容替换,再选择新版本。该示例把少量公开 FAQ 放进指令,没有文档抓取、索引或检索步骤,不能作为完整知识库检索方案。

你是产品帮助中心助手。只根据以下公开资料回答;资料没有覆盖的问题,说明不知道并引导用户阅读帮助中心。不要编造政策,不读取账户或订单。
资料:导出报告:进入报表页面,点击“导出”,选择 CSV。下载内容采用当前报表筛选条件。
资料:修改通知:进入设置中的“通知”,选择要接收的通知类型并保存。

在「配置 → 工具与授权」只保留本任务需要的能力;此例不需要业务工具。在「试用调试」问“如何导出报告”和一个资料之外的问题。预期: 前者使用给定步骤,后者明确无法从资料回答。资料与指令可能被复述,不能存放秘密。

实现按需问答窗口

把下面保存为 index.html。将 PRAXIS_PUBLISHABLE_KEY 替换为实际可公开 key。它可以出现在网页源码中;不要换成组织 API Key。页面通过 pending 合并同时发生的打开操作,加载失败会清除状态以便重试。

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Product documentation</title>
<h1>Product documentation</h1>
<button id="ask" type="button">Ask the docs (⌘K / Ctrl+K)</button>
<p id="status" role="status" aria-live="polite"></p>
<script type="module">
const button = document.querySelector("#ask");
const status = document.querySelector("#status");
const kitUrl = "https://cdn.deepraxis.com/v1/praxis-kit.js";
let pending;
let returnFocus = button;
function loadKit() {
if (customElements.get("praxis-agent")) return Promise.resolve();
return new Promise((resolve, reject) => {
const script = document.createElement("script");
script.type = "module";
script.src = kitUrl;
script.crossOrigin = "anonymous";
const timer = setTimeout(() => {
script.remove(); reject(new Error("Kit load timeout"));
}, 15000);
script.onload = () => {
clearTimeout(timer);
if (customElements.get("praxis-agent")) resolve();
else reject(new Error("Kit element unavailable"));
};
script.onerror = () => {
clearTimeout(timer); script.remove(); reject(new Error("Kit load failed"));
};
document.head.appendChild(script);
});
}
function assistant() {
pending ??= (async () => {
await loadKit();
const { configure } = await import("https://cdn.deepraxis.com/v1/praxis-session.js");
// 匿名访客用可公开 key;不要换成组织 API Key。
configure({ api: { url: "https://api.deepraxis.com", publishableKey: "PRAXIS_PUBLISHABLE_KEY" } });
const agent = document.createElement("praxis-agent");
agent.setOptions({
agent: { name: "Documentation assistant" },
layout: "modal",
launcher: { type: "none" },
header: { title: "Ask the docs" },
startScreen: {
greeting: "Ask about the public product guide.",
prompts: [{ label: "Export a report", prompt: "How do I export a report?" }],
},
composer: { placeholder: "Ask a product question" },
});
agent.addEventListener("praxis.close", () => {
button.setAttribute("aria-expanded", "false");
returnFocus?.focus();
});
agent.addEventListener("praxis.error", () => {
status.textContent = "Chat unavailable. Try again later or read the guide.";
});
document.body.appendChild(agent);
return agent;
})().catch(error => { pending = undefined; throw error; });
return pending;
}
async function openAssistant() {
returnFocus = document.activeElement instanceof HTMLElement ? document.activeElement : button;
button.disabled = true;
status.textContent = "Loading assistant…";
try {
const agent = await assistant();
agent.open();
button.setAttribute("aria-expanded", "true");
status.textContent = "";
} catch { status.textContent = "Could not load the assistant. Press the button to retry."; }
finally { button.disabled = false; }
}
button.setAttribute("aria-haspopup", "dialog");
button.setAttribute("aria-expanded", "false");
button.addEventListener("click", openAssistant);
document.addEventListener("keydown", event => {
if (!event.repeat && !event.isComposing && (event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "k") {
event.preventDefault();
if (!button.disabled) void openAssistant();
}
});
</script>
</html>

保存启动器为 server.ts,执行 bun server.ts,打开它打印的地址。

const server = Bun.serve({
hostname: "127.0.0.1", port: 4242,
fetch(request) {
const url = new URL(request.url);
if (request.method === "GET" && url.pathname === "/") {
return new Response(Bun.file("index.html"), { headers: { "Cache-Control": "no-store" } });
}
return new Response("Not found", { status: 404 });
},
});
console.log(`Open http://localhost:${server.port}`);

预期: 初次访问没有 Kit 下载请求;点击按钮后加载模块并打开居中窗口。再次打开复用同一个元素和匿名身份。Esc 关闭后,焦点回到触发前的控件。快捷键由页面监听,窗口布局、遮罩与关闭由 Kit 提供。

自动验证与答案验收

安装浏览器测试依赖,将下面保存为 docs-qa.spec.ts,在本地站点运行时执行:

终端窗口
bun add --dev @playwright/test
bunx playwright install chromium
bunx playwright test docs-qa.spec.ts --workers=1
import { test, expect } from "@playwright/test";
test("loads once, opens a modal, and restores focus", async ({ page }) => {
const kitRequests = [];
page.on("request", request => {
if (request.url().endsWith("/praxis-kit.js")) kitRequests.push(request.url());
});
await page.goto("http://localhost:4242");
expect(kitRequests).toHaveLength(0);
const button = page.getByRole("button", { name: /Ask the docs/ });
await button.click();
const dialog = page.locator("praxis-agent dialog");
await expect(dialog).toBeVisible();
await expect(button).toHaveAttribute("aria-expanded", "true");
await page.keyboard.press("Escape");
await expect(dialog).not.toBeVisible();
await expect(button).toBeFocused();
await page.keyboard.press("Control+k");
await expect(dialog).toBeVisible();
await expect(page.locator("praxis-agent")).toHaveCount(1);
expect(kitRequests).toHaveLength(1);
});

该测试验证延迟加载、单实例、快捷键和焦点,不把组件出现当作真实对话成功。接着在窗口问“如何导出报告”,确认得到上述步骤,再问资料外的问题,确认没有编造答案。在「运营与管理 → 会话」核对消息与匿名用户。

用独立浏览器配置测试另一位访客,确认看不到前一位的会话;刷新原浏览器会开始一位新的匿名访客。匿名令牌只保存在页面内存中,可公开 key 本身不能把所有人变成同一个登录账户。

失败分支与清理

  • 下载失败时页面显示重试提示:检查网络、CSP 与 Kit 地址,再点击按钮。没有成功下载时不应残留一个半初始化组件。
  • 窗口出现但无法回复:检查可公开 key 是否启用、来源策略和预算;按嵌入限制与错误处理实际响应。
  • 错误来源或额度问题不能靠重复打开解决;在控制台修正后再验证真实回复。
  • 答案错误时先更新资料与指令、确认所用版本,再新建对话验证;不能用增加超时掩盖知识缺口。

完成测试后停止本地服务器。停用专用测试可公开 key,并按终端用户管理撤销不再需要的测试访问;关闭窗口本身不会吊销令牌。上线前把测试来源换为真实站点并执行上线清单。

源码与延伸

本页的 index.html、server.ts 和 docs-qa.spec.ts 构成完整示例。需要把当前文章标题带入提问时,继续传入上下文;需要查询私有订单时,使用SaaS 登录用户配方。 这份配方是产品帮助中心方案里的问答入口,整体取舍与成本见该方案。