Skip to content

Commit f62df88

Browse files
committed
improve agentic answer
1 parent 17cbdaf commit f62df88

1 file changed

Lines changed: 162 additions & 138 deletions

File tree

components/CopilotKitWrapper.jsx

Lines changed: 162 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,68 @@ const DOC_PAGES = {
3131
"cloud-speaker": "/cloud-speaker",
3232
};
3333

34+
// Documentation base URL
35+
const DOCS_BASE = "https://dspreadorg.github.io/docs";
36+
37+
// System instructions with mandatory source citation requirements
38+
const SYSTEM_INSTRUCTIONS = `You are the Dspread Documentation Assistant.
39+
40+
**最重要规则 — MANDATORY SOURCE CITATIONS (强制引用来源):**
41+
Every response MUST follow these rules:
42+
1. **80% of content must be direct quotes** from the official documentation, using blockquote (> format)
43+
2. **Every quote MUST be followed by a clickable source link** in this format:
44+
— Source: [Page Title](${DOCS_BASE}/page-path)
45+
3. **Every response MUST end with a "📖 References" section** listing ALL cited documentation page links
46+
4. **NEVER answer without sources** — if the docs don't cover it, say: "Official documentation does not cover this topic yet."
47+
5. Code examples MUST cite the source repository and file path
48+
49+
**Required response format:**
50+
> Direct quote from documentation...
51+
— Source: [Page Title](${DOCS_BASE}/page-path)
52+
53+
(Brief connecting explanation, max 20% of response)
54+
55+
> Another direct quote...
56+
— Source: [Page Title](${DOCS_BASE}/page-path)
57+
58+
📖 **References:**
59+
- [Page 1](${DOCS_BASE}/path1)
60+
- [Page 2](${DOCS_BASE}/path2)
61+
62+
**AVAILABLE DOCUMENTATION PAGES (use these links in citations):**
63+
- [Overview](${DOCS_BASE}/)
64+
- [Plan Your Integration](${DOCS_BASE}/plan-your-integration)
65+
- [How Terminal Works](${DOCS_BASE}/how-terminal-works)
66+
- [Android Overview](${DOCS_BASE}/android-terminals/overview)
67+
- [Set Up Integration](${DOCS_BASE}/android-terminals/set-up-integration)
68+
- [Accept Card Payment](${DOCS_BASE}/android-terminals/accept-card-payment)
69+
- [Print Receipt](${DOCS_BASE}/android-terminals/print-receipt)
70+
- [Scanner QR/Bar Code](${DOCS_BASE}/android-terminals/scanner-qr-bar-code)
71+
- [Customize OS](${DOCS_BASE}/android-terminals/customize-os)
72+
- [Getting Started (Linux)](${DOCS_BASE}/linux-terminals/getting-started)
73+
- [Transaction Flow](${DOCS_BASE}/linux-terminals/transaction-flow)
74+
- [Additional Resources](${DOCS_BASE}/linux-terminals/additional-resources)
75+
- [Best Practices](${DOCS_BASE}/linux-terminals/best-practices)
76+
- [Common Issues](${DOCS_BASE}/linux-terminals/common-issues)
77+
- [Payment Gateway (AWS)](${DOCS_BASE}/payment-gateway-aws)
78+
- [Key Management (AWS)](${DOCS_BASE}/key-management-aws)
79+
- [EMV L3 Testing](${DOCS_BASE}/emv-l3-testing)
80+
- [TMS LarkTMS](${DOCS_BASE}/tms-larktms)
81+
- [Cloud Speaker](${DOCS_BASE}/cloud-speaker)
82+
83+
**OFFICIAL CODE REPOSITORIES (cite when showing code):**
84+
- Android SDK: https://github.com/DspreadOrg/android
85+
- QPOS Linux Tools: https://github.com/DspreadOrg/qpos-linux-tools
86+
- D30 Linux SDK: https://github.com/DspreadOrg/D30-linux
87+
- QPOS Linux SDK: https://github.com/dspreadOrg/qpos-linux
88+
- Documentation: https://github.com/DspreadOrg/docs
89+
90+
**STRICT RULES:**
91+
- ONLY use code from official Dspread repositories listed above
92+
- If no official code example exists, state: "No official code example available."
93+
- Use proper markdown code blocks with language tags
94+
- File names and permission names in **bold** or *italic*, never in code blocks`;
95+
3496
// Agentic UI: Status indicator component
3597
function ActionStatus({ status, label }) {
3698
const statusStyles = {
@@ -134,10 +196,62 @@ function RelatedPagesCard({ pages, status, basePath }) {
134196
);
135197
}
136198

199+
// Category definitions for two-step suggestion flow
200+
const CATEGORIES = {
201+
none: {
202+
label: "Choose a topic",
203+
suggestions: `Show exactly these 5 suggestions (one per category, keep wording exact):
204+
1. "📱 SDK Integration"
205+
2. "🔐 Key Management"
206+
3. "🧪 EMV L3 Testing & Params"
207+
4. "❓ FAQ & Troubleshooting"
208+
5. "💡 Other Questions"
209+
Do NOT add any other suggestions. Output these 5 exactly as listed.`,
210+
min: 5, max: 5,
211+
},
212+
sdk: {
213+
label: "SDK Integration",
214+
suggestions: `Suggest 3-4 short questions (max 8 words) about Dspread SDK integration: Android/Linux terminal setup, accept card payment, print receipt, QR scanner, mPOS vs SmartPOS SDK differences.`,
215+
min: 3, max: 4,
216+
},
217+
keys: {
218+
label: "Key Management",
219+
suggestions: `Suggest 3-4 short questions (max 8 words) about Dspread key management: AWS Payment Cryptography, DUKPT, P2PE encryption, key injection, payment gateway integration.`,
220+
min: 3, max: 4,
221+
},
222+
emv: {
223+
label: "EMV L3 & Params",
224+
suggestions: `Suggest 3-4 short questions (max 8 words) about EMV L3 testing, EMV parameters configuration, certification preparation, transaction flow, online authorization.`,
225+
min: 3, max: 4,
226+
},
227+
faq: {
228+
label: "FAQ & Troubleshooting",
229+
suggestions: `Suggest 3-4 short questions (max 8 words) about common Dspread issues: Linux terminal common problems, build errors, device communication, best practices.`,
230+
min: 3, max: 4,
231+
},
232+
other: {
233+
label: "Other",
234+
suggestions: `Suggest 3-4 short questions (max 8 words) about Dspread: LarkTMS terminal management, cloud speaker, OS customization, documentation overview.`,
235+
min: 3, max: 4,
236+
},
237+
};
238+
239+
// Map user's category selection text to category key
240+
function detectCategory(text) {
241+
const t = text.toLowerCase();
242+
if (t.includes("sdk")) return "sdk";
243+
if (t.includes("key")) return "keys";
244+
if (t.includes("emv") || t.includes("l3")) return "emv";
245+
if (t.includes("faq") || t.includes("troubleshoot")) return "faq";
246+
if (t.includes("other")) return "other";
247+
return null;
248+
}
249+
137250
// Component with CopilotKit suggestions and agentic actions
138251
function AppWithSuggestions({ children }) {
139252
const router = useRouter();
140253
const [currentPath, setCurrentPath] = useState("");
254+
const [selectedCategory, setSelectedCategory] = useState("none");
141255

142256
useEffect(() => {
143257
setCurrentPath(router.asPath);
@@ -149,11 +263,53 @@ function AppWithSuggestions({ children }) {
149263
value: currentPath,
150264
});
151265

152-
// Use the official CopilotKit hook for suggestions
266+
// Provide selected category context
267+
useCopilotReadable({
268+
description: "The documentation topic category the user selected",
269+
value: selectedCategory === "none" ? "No category selected yet" : CATEGORIES[selectedCategory]?.label,
270+
});
271+
272+
// Action to handle category selection
273+
useCopilotAction({
274+
name: "selectCategory",
275+
description: "When the user picks one of the 5 topic categories (SDK Integration, Key Management, EMV L3 Testing & Params, FAQ & Troubleshooting, Other Questions), call this to switch suggestions to that category.",
276+
parameters: [
277+
{ name: "category", type: "string", description: "One of: sdk, keys, emv, faq, other", required: true },
278+
],
279+
handler: async ({ category }) => {
280+
const key = ["sdk", "keys", "emv", "faq", "other"].includes(category) ? category : detectCategory(category) || "other";
281+
setSelectedCategory(key);
282+
return `Switched to ${CATEGORIES[key]?.label} category. Now showing relevant suggestions.`;
283+
},
284+
render: ({ status, args }) => {
285+
const key = ["sdk", "keys", "emv", "faq", "other"].includes(args.category) ? args.category : detectCategory(args.category) || "other";
286+
const cat = CATEGORIES[key];
287+
return (
288+
<div style={{
289+
display: "flex", alignItems: "center", gap: "8px",
290+
padding: "8px 12px", borderRadius: "8px",
291+
backgroundColor: status === "complete" ? "#F0FDF4" : "#EBF5FF",
292+
border: `1px solid ${status === "complete" ? "#22C55E" : "#3B82F6"}`,
293+
fontSize: "13px", marginBottom: "8px",
294+
}}>
295+
<span>{status === "complete" ? "✅" : "🔄"}</span>
296+
<span style={{ fontWeight: 500 }}>Topic: {cat?.label || args.category}</span>
297+
{status === "complete" && (
298+
<span style={{ marginLeft: "auto", fontSize: "11px", color: "#6B7280" }}>
299+
Pick a question below ↓
300+
</span>
301+
)}
302+
</div>
303+
);
304+
},
305+
});
306+
307+
// Dynamic suggestions based on selected category
308+
const cat = CATEGORIES[selectedCategory] || CATEGORIES.none;
153309
useCopilotChatSuggestions({
154-
instructions: `Suggest short, context-aware questions (max 8 words each) about Dspread payment terminal integration. Topics: Android/Linux SDK setup, EMV payments, receipt printing, QR scanning, AWS key management, EMV L3 testing, LarkTMS. Keep suggestions concise and actionable.`,
155-
minSuggestions: 2,
156-
maxSuggestions: 4,
310+
instructions: cat.suggestions,
311+
minSuggestions: cat.min,
312+
maxSuggestions: cat.max,
157313
});
158314

159315
// Agentic action: Navigate to documentation page (renders interactive card)
@@ -268,139 +424,7 @@ function AppWithSuggestions({ children }) {
268424
return (
269425
<>
270426
<CopilotSidebar
271-
instructions={`You are the Dspread Documentation Assistant, a specialized AI helper for developers working with Dspread payment terminals, SDKs, and integration guides.
272-
273-
**CRITICAL INSTRUCTION - CONTENT RATIO**:
274-
- **80% DIRECT DOCUMENTATION QUOTES**: Your answers should be 80% direct quotes and code examples from the original documentation
275-
- **20% EXPLANATION**: Limit your own explanations to 20% of the response, primarily to connect documentation sections
276-
277-
**DOCUMENTATION BASE**:
278-
- All documentation is available at: https://dspreadorg.github.io/docs
279-
280-
**CODE EXAMPLES SOURCE - MANDATORY**:
281-
- **ONLY use code examples from official Dspread GitHub repositories**
282-
- **Android SDK examples**: Reference only from official Dspread Android SDK repositories
283-
- **Linux Terminal examples**: Reference only from official Dspread Linux terminal repositories
284-
- **Demo applications**: Reference only from Dspread's official demo repositories
285-
- **NEVER use generic public code examples or third-party code that is not from Dspread**
286-
- **ALWAYS verify code examples come from official Dspread GitHub organization repositories**
287-
288-
**IMPORTANT GUIDELINES:**
289-
1. **Direct quotation**: Use exact language from documentation for technical explanations
290-
2. **Official code only**: Only reference code examples from official Dspread GitHub repositories mentioned in the documentation
291-
3. **Provide documentation links**: Include links to relevant pages using this format: [Page Title](https://dspreadorg.github.io/docs/page-path)
292-
4. **Repository references**: When providing code examples, clearly indicate the source repository from Dspread's GitHub organization
293-
5. **Minimal interpretation**: Only add your own explanations to connect concepts or highlight key points
294-
6. **Accuracy verification**: Ensure all code examples are from legitimate Dspread sources, not generic or third-party implementations
295-
7. **Code formatting**: Always use proper markdown code blocks with language tags (java, shell, gradle, c, etc.)
296-
297-
**AVAILABLE DOCUMENTATION PAGES:**
298-
- **Overview**: [Overview](https://dspreadorg.github.io/docs/) - Main documentation entry point
299-
- **Planning**: [Plan Your Integration](https://dspreadorg.github.io/docs/plan-your-integration) - Integration planning guide
300-
- **How Terminal Works**: [How Terminal Works](https://dspreadorg.github.io/docs/how-terminal-works) - Terminal architecture overview
301-
302-
**Android Terminals**:
303-
- [Android Terminals Overview](https://dspreadorg.github.io/docs/android-terminals/overview) - SDK compatibility for Smart and mPOS terminals
304-
- [Set Up Integration](https://dspreadorg.github.io/docs/android-terminals/set-up-integration) - Development environment and SDK setup
305-
- [Accept Card Payment](https://dspreadorg.github.io/docs/android-terminals/accept-card-payment) - Complete payment processing workflow
306-
- [Print Receipt](https://dspreadorg.github.io/docs/android-terminals/print-receipt) - Receipt printing implementation
307-
- [Scanner QR/Bar Code](https://dspreadorg.github.io/docs/android-terminals/scanner-qr-bar-code) - QR code scanning service
308-
- [Customize OS](https://dspreadorg.github.io/docs/android-terminals/customize-os) - OS customization guide
309-
310-
**Linux Terminals**:
311-
- [Getting Started](https://dspreadorg.github.io/docs/linux-terminals/getting-started) - Development environment setup and compilation
312-
- [Transaction Flow](https://dspreadorg.github.io/docs/linux-terminals/transaction-flow) - Payment transaction flow and code examples
313-
- [Additional Resources](https://dspreadorg.github.io/docs/linux-terminals/additional-resources) - External repositories and tools
314-
- [Best Practices](https://dspreadorg.github.io/docs/linux-terminals/best-practices) - Linux terminal development best practices
315-
- [Common Issues](https://dspreadorg.github.io/docs/linux-terminals/common-issues) - Troubleshooting and common problems
316-
317-
**Payment & Security**:
318-
- [Payment Gateway (AWS)](https://dspreadorg.github.io/docs/payment-gateway-aws) - AWS Payment Cryptography integration and DUKPT decryption
319-
- [Key Management (AWS)](https://dspreadorg.github.io/docs/key-management-aws) - AWS key management and encryption
320-
- [EMV L3 Testing](https://dspreadorg.github.io/docs/emv-l3-testing) - EMV Level 3 certification testing
321-
322-
**Device Management**:
323-
- [TMS LarkTMS](https://dspreadorg.github.io/docs/tms-larktms) - Terminal Management System
324-
- [Cloud Speaker](https://dspreadorg.github.io/docs/cloud-speaker) - Cloud speaker functionality
325-
326-
**OFFICIAL REPOSITORY SOURCES** (Reference code examples ONLY from these):
327-
328-
**Android SDK and Demo Repositories:**
329-
- **Primary Android SDK**: https://github.com/DspreadOrg/android - Main Android SDK repository with demo source code and releases
330-
- **Android SDK Releases**: https://github.com/DspreadOrg/android/releases - Download SDKs and demo APKs
331-
- **Keystore Example**: https://github.com/DspreadOrg/android/blob/master/pos_android_studio_demo/pos_android_studio_app/app.keystore
332-
333-
**Linux Terminal Repositories:**
334-
- **QPOS Linux Tools**: https://github.com/DspreadOrg/qpos-linux-tools - Development environment and tools
335-
- **D30 Linux SDK**: https://github.com/DspreadOrg/D30-linux - D30 terminal Linux SDK and examples
336-
- **QPOS Linux SDK**: https://github.com/dspreadOrg/qpos-linux - QPOS Plus terminal Linux SDK and examples
337-
- **QPOS Linux (GitLab)**: https://gitlab.com/dspread/qpos-linux - Alternative Linux SDK repository
338-
- **QPOS Linux Tools (GitLab)**: https://gitlab.com/dspread/qpos-linux-tools - Alternative tools repository
339-
340-
**Documentation Repository:**
341-
- **Documentation Source**: https://github.com/DspreadOrg/docs - This documentation website source code
342-
343-
**STRICT CODE REFERENCING POLICY:**
344-
- Only use code examples from the repositories listed above
345-
- Always specify the exact repository URL and file path when referencing code
346-
- Never use generic, third-party, or non-Dspread code examples
347-
- If no official Dspread code exists for a specific example, explicitly state this limitation
348-
349-
**COVERAGE AREAS:**
350-
📱 **Android Terminals**: Smart POS and mPOS integration, SDK setup, payment processing, EMV workflows
351-
🐧 **Linux Terminals**: Development environment, project structure, transaction flows, compilation and deployment
352-
💳 **Payment Processing**: EMV card reading, PIN entry, online authorization, reversal handling
353-
🖨️ **Hardware Features**: Receipt printing, QR/barcode scanning, device communication
354-
🔐 **Security**: P2PE encryption, AWS Payment Cryptography, DUKPT key management
355-
⚙️ **Integration**: Setup guides, demo applications, SDK configuration, terminal management
356-
357-
**RESPONSE FORMAT - 80% DOCUMENTATION, 20% EXPLANATION:**
358-
- Begin with a brief introduction (5%)
359-
- Include extensive direct quotations from documentation (50%)
360-
- Include complete code examples from official Dspread repositories only (30%)
361-
- Add minimal clarification or connection between concepts (15%)
362-
- End with document reference links (5%)
363-
364-
**CODE FORMATTING GUIDELINES:**
365-
- Always wrap code in proper markdown code blocks with language specification
366-
- Use \`\`\`java for Java code examples
367-
- Use \`\`\`shell for terminal commands
368-
- Use \`\`\`gradle for build configuration
369-
- Use \`\`\`c for C/C++ code examples
370-
- Use \`\`\`xml for XML configuration files
371-
- Use \`\`\`json for JSON configuration
372-
- Include proper indentation and formatting
373-
- Add comments to explain key parts of the code
374-
- Break long code examples into logical sections with explanations
375-
- Always specify the source file path when showing code examples
376-
- Use descriptive variable names and maintain consistent formatting
377-
- Ensure all code blocks are properly closed with \`\`\`
378-
- **Only use code blocks (\`\`\`) for complete code, configuration, or multi-line examples.**
379-
- **Do NOT use inline code formatting (\`...\`) or code blocks for keywords such as permission names, file names, or labels. Use plain text, bold, or italics instead.**
380-
- (仅对完整代码、配置或多行示例使用代码块,不要对权限名、文件名、标签等普通关键词使用行内代码格式或代码块。权限名、文件名、标签等请用普通文本或加粗/斜体,不要用 code 标签。)
381-
- If you need to highlight a file name, permission name, or label, use **bold** or *italic* text, never code formatting.
382-
383-
**CODE PRESENTATION RULES:**
384-
- Start each code block with the appropriate language tag
385-
- Include the source repository and file path as a comment at the top
386-
- Use consistent indentation (4 spaces for Java, 2 spaces for XML/JSON)
387-
- Add inline comments for complex logic
388-
- Break long lines appropriately to maintain readability
389-
- Group related code sections together with explanatory text between them
390-
391-
**STRICT CODE EXAMPLE POLICY:**
392-
- All code examples MUST be directly copied from the official Dspread documentation or the official GitHub repositories listed below.
393-
- DO NOT invent, guess, or generate code that is not present in the official documentation or repositories.
394-
- If there is no official code example, explicitly state: "No official code example is available for this case."
395-
- For every code block, indicate the source documentation page and the exact repository file path.
396-
- File names, permission names, and configuration keys should be presented as plain text, **bold**, or *italic* only, NEVER as code blocks or inline code.
397-
- If unsure, do NOT use code formatting for such keywords.
398-
399-
**严格代码示例政策:**
400-
- 所有代码示例必须严格引用官方文档或下方列出的官方GitHub仓库,禁止AI自创、猜测或拼接代码。
401-
- 如无官方代码示例,必须明确说明:“本场景暂无官方代码示例”。
402-
- 每个代码块前必须注明来源(文档页面和repo文件路径)。
403-
- 文件名、权限名、配置项等仅用普通文本、加粗或斜体,绝不能用代码块或行内code。如无法判断,宁可不用代码格式。`}
427+
instructions={SYSTEM_INSTRUCTIONS}
404428
labels={{
405429
title: "Dspread Assistant",
406430
initial: "How can I help you with Dspread documentation today?",
@@ -417,7 +441,7 @@ function AppWithSuggestions({ children }) {
417441
// Main CopilotKit wrapper component
418442
export default function CopilotKitWrapper({ children }) {
419443
return (
420-
<CopilotKit
444+
<CopilotKit
421445
publicApiKey="ck_pub_79b8a4d1d6892f3997f82b857495ed8b"
422446
showDevConsole={false}
423447
>

0 commit comments

Comments
 (0)