API 参考
Widget API 和 REST 端点的完整参考文档。
Widget API
全局 window.LaunchChatWidget 对象提供了以编程方式控制组件的方法。
方法
open()以编程方式打开聊天窗口。
window.LaunchChatWidget.open();close()关闭聊天窗口。
window.LaunchChatWidget.close();destroy()从页面中完全移除组件。适用于 SPA 页面切换时使用。
window.LaunchChatWidget.destroy();init(config)使用新配置重新初始化组件。
window.LaunchChatWidget.init({ widgetId: "new-widget-id" });on(event, callback)订阅组件事件。
window.LaunchChatWidget.on('message', (data) => {
console.log('New message:', data);
});off(event, callback)取消订阅组件事件。
事件
| 事件 | 载荷 | 描述 |
|---|---|---|
open | { timestamp } | 聊天窗口已打开 |
close | { timestamp } | 聊天窗口已关闭 |
message | { content, role } | 消息已发送或接收 |
escalate | { email, message } | 用户请求人工客服 |
feedback | { messageId, type } | 用户提交了反馈(好评/差评) |
REST API
POST /api/widget/chat
发送消息并接收AI生成的回复。
请求体
{
"widgetId": "string", // Required
"message": "string", // Required - user's question
"conversationId": "string", // Optional - for context
"visitorId": "string", // Optional - track visitor
"pageUrl": "string" // Optional - current page
}响应
{
"conversationId": "uuid",
"messageId": "uuid",
"answer": "string",
"citations": [
{
"pageId": "string",
"pageTitle": "string",
"pageUrl": "string",
"excerpt": "string"
}
],
"relatedArticles": [...],
"confidenceScore": 0.85,
"wasRefused": false
}错误码
| 状态码 | 状态 | 描述 |
|---|---|---|
400 | 请求错误 | 缺少必填字段 |
403 | 禁止访问 | 域名不在允许列表中 |
404 | 未找到 | 组件未找到或已停用 |
429 | 请求频率受限 | 消息配额已用尽 |
500 | 服务器错误 | 内部处理错误 |
TypeScript 类型
将这些类型复制到您的项目中以获得完整的类型安全:
interface LaunchChatConfig {
widgetId: string;
primaryColor?: string;
greeting?: string;
placeholder?: string;
position?: 'bottom-right' | 'bottom-left';
theme?: 'light' | 'dark' | 'auto';
}
interface WidgetAPI {
open(): void;
close(): void;
destroy(): void;
init(config: { widgetId: string }): Promise<void>;
on<E extends keyof WidgetEvents>(
event: E,
callback: (data: WidgetEvents[E]) => void
): void;
off<E extends keyof WidgetEvents>(
event: E,
callback: (data: WidgetEvents[E]) => void
): void;
}
interface WidgetEvents {
open: { timestamp: number };
close: { timestamp: number };
message: { content: string; role: 'user' | 'assistant' };
escalate: { email: string; message: string };
feedback: { messageId: string; type: 'positive' | 'negative' };
}
declare global {
interface Window {
LaunchChatConfig?: LaunchChatConfig;
LaunchChatWidget?: WidgetAPI;
}
}