feat: SodiumPet 小钠桌宠 v0.1.0(Live2D 桌宠 / 经济任务 / 音乐 / 成就 / 编辑器 / MOD)
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
const fs = require('fs')
|
||||
const base = 'C:/Users/ASUS/Documents/deepseek-harness/default-workspace/desktop-pet/src/'
|
||||
|
||||
function patch(file, pairs) {
|
||||
const p = base + file
|
||||
let c = fs.readFileSync(p, 'utf8')
|
||||
for (const [from, to] of pairs) {
|
||||
if (!c.includes(from)) {
|
||||
console.log('MISS in', file, '::', from.slice(0, 70).replace(/\n/g, '\\n'))
|
||||
continue
|
||||
}
|
||||
c = c.replace(from, to)
|
||||
}
|
||||
fs.writeFileSync(p, c, 'utf8')
|
||||
}
|
||||
|
||||
// ---- stateStore:事故处罚
|
||||
patch('main/stateStore.ts', [
|
||||
[
|
||||
" applyChatEffect(e: {",
|
||||
` applyJobPenalty(p: { money?: number; mood?: number; oxidation?: number }): void {
|
||||
if (p.money) this.save.money = Math.max(0, this.save.money + p.money)
|
||||
if (p.mood) this.save.stats.mood = Math.max(0, Math.min(100, this.save.stats.mood + p.mood))
|
||||
if (p.oxidation) {
|
||||
this.save.stats.oxidation = Math.max(0, Math.min(100, this.save.stats.oxidation + p.oxidation))
|
||||
}
|
||||
this.emit()
|
||||
this.saveNow()
|
||||
}
|
||||
|
||||
applyChatEffect(e: {`
|
||||
]
|
||||
])
|
||||
|
||||
// ---- llm:askStream 支持知识库上下文
|
||||
patch('main/llm.ts', [
|
||||
[
|
||||
' async askStream(text: string, onChunk: (chunk: string) => void): Promise<string> {',
|
||||
' async askStream(\n text: string,\n onChunk: (chunk: string) => void,\n extraContext = ""\n ): Promise<string> {'
|
||||
],
|
||||
[
|
||||
" const messages: ChatMessage[] = [\n { role: 'system', content: systemPrompt(this.getSave(), cfg.personaExtra) },",
|
||||
" const sys = systemPrompt(this.getSave(), cfg.personaExtra)\n const messages: ChatMessage[] = [\n {\n role: 'system',\n content: extraContext ? `${sys}\\n\\n【知识库参考,可用可不用】\\n${extraContext}` : sys\n },"
|
||||
]
|
||||
])
|
||||
|
||||
// ---- main:知识库 + 联网搜索 + 事故
|
||||
patch('main/index.ts', [
|
||||
[
|
||||
"import { MediaStore } from './mediaStore'",
|
||||
"import { MediaStore } from './mediaStore'\nimport { KnowledgeBase } from './knowledge'\nimport { rollAccident } from '../shared/accidents'"
|
||||
],
|
||||
[
|
||||
"const media = new MediaStore(path.join(HOME, 'data'))",
|
||||
"const media = new MediaStore(path.join(HOME, 'data'))\nconst kb = new KnowledgeBase(path.join(HOME, 'data'))"
|
||||
],
|
||||
[
|
||||
` ipcMain.handle('pet:kb:query', () => ({ ok: false, message: '知识库将在后续版本接入' }))
|
||||
ipcMain.handle('pet:web:search', () => ({ ok: false, message: '联网搜索将在后续版本接入' }))`,
|
||||
` ipcMain.handle('pet:kb:list', () => kb.list())
|
||||
ipcMain.handle('pet:kb:add', (_e, name: string) => {
|
||||
const src = path.join(media.fileDir, String(name))
|
||||
if (src.startsWith(media.fileDir) && fs.existsSync(src)) kb.addFromFile(src)
|
||||
return kb.list()
|
||||
})
|
||||
ipcMain.handle('pet:kb:del', (_e, name: string) => {
|
||||
kb.remove(String(name))
|
||||
return kb.list()
|
||||
})`
|
||||
],
|
||||
// 聊天:搜索前缀 + 知识库注入
|
||||
[
|
||||
` const eff = chatEffect(t)
|
||||
if (eff) {
|
||||
store.applyChatEffect(eff)
|
||||
mainWindow?.webContents.send('pet:toast', eff.toast)
|
||||
}
|
||||
bubbleStreamStart()
|
||||
try {
|
||||
const reply = await chatService.askStream(t, (chunk) => bubbleStreamChunk(chunk))`,
|
||||
` const eff = chatEffect(t)
|
||||
if (eff) {
|
||||
store.applyChatEffect(eff)
|
||||
mainWindow?.webContents.send('pet:toast', eff.toast)
|
||||
}
|
||||
bubbleStreamStart()
|
||||
try {
|
||||
// 搜索指令:帮我查 / 搜索 xxx
|
||||
const m = t.match(/^(?:搜索|帮我查|查一下|search)[::\\s]+(.+)$/i)
|
||||
let reply: string
|
||||
if (m) {
|
||||
const q = m[1].trim()
|
||||
const results = await webSearch(q)
|
||||
if (!results) {
|
||||
reply = '(挠头)没查到诶,网络好像不太配合 (・_・;)'
|
||||
bubbleStreamChunk(reply)
|
||||
} else {
|
||||
reply = await chatService.askStream(
|
||||
\`搜索结果:\\n\${results}\\n\\n请用小钠的语气、三句话以内总结回答这个问题:「\${q}」\`,
|
||||
(chunk) => bubbleStreamChunk(chunk)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
const kbCtx = kb.search(t)
|
||||
reply = await chatService.askStream(t, (chunk) => bubbleStreamChunk(chunk), kbCtx)
|
||||
}`
|
||||
],
|
||||
// 事故:任务完成时掷骰子
|
||||
[
|
||||
`let nextSuggestAt = Date.now() + 180_000`,
|
||||
`let lastJobsDone = -1
|
||||
let nextSuggestAt = Date.now() + 180_000`
|
||||
],
|
||||
[
|
||||
`function scheduleEvents(): void {
|
||||
setInterval(() => {
|
||||
if (Date.now() < nextEventAt) return
|
||||
const snap = store.snapshot()`,
|
||||
`function scheduleEvents(): void {
|
||||
lastJobsDone = store.snapshot().jobsDone
|
||||
setInterval(() => {
|
||||
const snapNow = store.snapshot()
|
||||
if (snapNow.jobsDone > lastJobsDone) {
|
||||
lastJobsDone = snapNow.jobsDone
|
||||
const acc = rollAccident()
|
||||
if (acc) {
|
||||
store.applyJobPenalty({
|
||||
money: acc.money,
|
||||
mood: acc.mood,
|
||||
oxidation: acc.oxidation
|
||||
})
|
||||
mainWindow?.webContents.send('pet:motion', 'shake')
|
||||
mainWindow?.webContents.send('pet:toast', \`\${acc.name}:\${acc.money ? ' 经费 ' + acc.money : ''}\${acc.mood ? ' 心情 ' + acc.mood : ''}\`)
|
||||
say(acc.bark, 7000)
|
||||
}
|
||||
}
|
||||
if (Date.now() < nextEventAt) return
|
||||
const snap = store.snapshot()`
|
||||
],
|
||||
// webSearch 函数
|
||||
[
|
||||
`async function capturePhoto(): Promise<void> {`,
|
||||
`async function webSearch(q: string): Promise<string> {
|
||||
try {
|
||||
const url = \`https://api.duckduckgo.com/?q=\${encodeURIComponent(q)}&format=json&no_html=1&skip_disambig=1\`
|
||||
const res = await fetch(url, { signal: AbortSignal.timeout(8000) })
|
||||
if (!res.ok) return ''
|
||||
const json = (await res.json()) as {
|
||||
AbstractText?: string
|
||||
RelatedTopics?: Array<string | { Text?: string }>
|
||||
}
|
||||
const parts: string[] = []
|
||||
if (json.AbstractText) parts.push(json.AbstractText)
|
||||
for (const r of (json.RelatedTopics ?? []).slice(0, 4)) {
|
||||
if (typeof r === 'string') parts.push(r)
|
||||
else if (r?.Text) parts.push(r.Text)
|
||||
}
|
||||
return parts.join('\\n').slice(0, 1500)
|
||||
} catch (err) {
|
||||
console.warn('[web] 搜索失败', err)
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
async function capturePhoto(): Promise<void> {`
|
||||
]
|
||||
])
|
||||
|
||||
console.log('kb/web/accidents patched')
|
||||
Reference in New Issue
Block a user