120 lines
3.8 KiB
TypeScript
120 lines
3.8 KiB
TypeScript
import type { KnowledgeItem } from "@/lib/types"
|
|
import { CATEGORIES, DIFFICULTIES, MASTERY_LEVELS, TAGS } from "@/lib/types"
|
|
|
|
const ITEM_FIELDS = [
|
|
"title",
|
|
"summary",
|
|
"content",
|
|
"code_snippet",
|
|
"tags",
|
|
"category",
|
|
"difficulty",
|
|
"mastery",
|
|
"source_url",
|
|
"notes",
|
|
] as const
|
|
|
|
export interface BatchImportPromptOptions {
|
|
/** tags 可选枚举,默认取 lib/types 中的 TAGS */
|
|
tags?: readonly string[]
|
|
/** category 可选枚举,默认取 lib/types 中的 CATEGORIES */
|
|
categories?: readonly string[]
|
|
/** difficulty 可选枚举,默认取 lib/types 中的 DIFFICULTIES */
|
|
difficulties?: readonly string[]
|
|
/** mastery 默认值,默认取 MASTERY_LEVELS 的第一项 */
|
|
defaultMastery?: string
|
|
}
|
|
|
|
/**
|
|
* 根据类型定义生成批量导入提示词。
|
|
* 枚举字段全部从 lib/types 的常量派生,修改类型后提示词会自动同步。
|
|
*/
|
|
export function buildBatchImportPrompt(options: BatchImportPromptOptions = {}): string {
|
|
const {
|
|
tags = TAGS,
|
|
categories = CATEGORIES,
|
|
difficulties = DIFFICULTIES,
|
|
defaultMastery = MASTERY_LEVELS[0],
|
|
} = options
|
|
|
|
const fieldsList = ITEM_FIELDS.map((f) => ` - ${f}`).join("\n")
|
|
|
|
return `请把用户输入的多个前端题目整理成知识库 JSON。
|
|
要求:
|
|
1. 只返回严格 JSON。
|
|
2. JSON 顶层格式必须是:{ "items": [] }
|
|
3. 每个 item 必须包含:
|
|
${fieldsList}
|
|
4. tags 只能从这些枚举中选择:
|
|
${tags.join("、")}
|
|
5. category 只能从这些枚举中选择:
|
|
${categories.join("、")}
|
|
6. difficulty 只能是:
|
|
${difficulties.join("、")}
|
|
7. mastery 默认是 ${defaultMastery}
|
|
8. 如果题目适合写代码示例,请放入 code_snippet
|
|
9. content 要适合后续复习,结构清晰
|
|
10. 不要返回 Markdown,不要返回解释`
|
|
}
|
|
|
|
/** 默认批量导入提示词(使用 lib/types 中的枚举常量) */
|
|
export const BATCH_IMPORT_PROMPT = buildBatchImportPrompt()
|
|
|
|
export function buildItemContext(item: KnowledgeItem): string {
|
|
return `当前知识点:
|
|
标题:${item.title}
|
|
摘要:${item.summary ?? ""}
|
|
正文:${item.content ?? ""}
|
|
代码:${item.code_snippet ?? ""}
|
|
标签:${item.tags.join("、")}
|
|
难度:${item.difficulty}`
|
|
}
|
|
|
|
export function buildChatSystemPrompt(item: KnowledgeItem): string {
|
|
return `你正在帮助用户复习一个前端知识点。
|
|
|
|
${buildItemContext(item)}
|
|
|
|
请基于这个知识点回答用户问题。
|
|
回答要求:
|
|
- 解释清晰
|
|
- 尽量结合面试场景
|
|
- 必要时给代码示例
|
|
- 如果用户回答错误,要指出问题并给出正确理解
|
|
- 不要编造不存在的上下文`
|
|
}
|
|
|
|
export function buildOptimizePrompt(item: { title: string; summary: string; content: string }): string {
|
|
return `请优化下面这个前端知识点,使其更适合复习记忆。
|
|
只返回严格 JSON,格式为:{ "summary": "...", "content": "..." }
|
|
不要返回 Markdown,不要返回解释。
|
|
|
|
标题:${item.title}
|
|
当前摘要:${item.summary}
|
|
当前正文:${item.content}
|
|
|
|
要求:
|
|
- summary 为一句话精炼摘要
|
|
- content 结构清晰、分点、突出重点与易错点,适合反复复习`
|
|
}
|
|
|
|
export function buildCodeGenPrompt(item: { title: string; summary: string; content: string }): string {
|
|
return `请为下面这个前端知识点生成一段简洁、可运行、有代表性的示例代码。
|
|
只返回代码本身,不要返回 Markdown 代码块标记,不要返回解释。
|
|
|
|
标题:${item.title}
|
|
摘要:${item.summary}
|
|
正文:${item.content}`
|
|
}
|
|
|
|
export function buildInterviewPrompt(item: KnowledgeItem): string {
|
|
return `请把下面这个前端知识点整理成"面试回答版"。
|
|
要求:
|
|
- 模拟面试场景下口语化但专业的回答
|
|
- 先给结论,再展开原理,最后补充延伸/注意点
|
|
- 必要时给简短代码示例
|
|
- 不要返回 Markdown 代码块以外的多余解释
|
|
|
|
${buildItemContext(item)}`
|
|
}
|