Files
SodiumPet/tools/patch-kb-ui.cjs
T

94 lines
3.8 KiB
JavaScript

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')
}
patch('preload/index.ts', [
[
" dropFile: (file: unknown) => void",
" dropFile: (file: unknown) => void\n getKb: () => Promise<Array<{ name: string; chars: number }>>\n addKb: (name: string) => Promise<Array<{ name: string; chars: number }>>\n delKb: (name: string) => Promise<Array<{ name: string; chars: number }>>"
],
[
" dropFile: (file: unknown) => {",
" getKb: () => ipcRenderer.invoke('pet:kb:list'),\n addKb: (name: string) => ipcRenderer.invoke('pet:kb:add', name),\n delKb: (name: string) => ipcRenderer.invoke('pet:kb:del', name),\n dropFile: (file: unknown) => {"
]
])
patch('renderer/src/api.ts', [
[
" dropFile: (file: unknown) => void",
" dropFile: (file: unknown) => void\n getKb: () => Promise<Array<{ name: string; chars: number }>>\n addKb: (name: string) => Promise<Array<{ name: string; chars: number }>>\n delKb: (name: string) => Promise<Array<{ name: string; chars: number }>>"
]
])
// Panel:知识库状态 + 文件行按钮 + 设置区列表
patch('renderer/src/panel/Panel.tsx', [
[
" const [photoCat, setPhotoCat] = useState('全部')",
" const [photoCat, setPhotoCat] = useState('全部')\n const [kbList, setKbList] = useState<Array<{ name: string; chars: number }>>([])"
],
[
" void a?.getPhotos().then(setPhotos)",
" void a?.getPhotos().then(setPhotos)\n void a?.getKb().then(setKbList)"
],
[
` <button className="mini" onClick={() => void api()?.delFile(f.name)}>
删除
</button>`,
` {/\.(md|txt)$/i.test(f.name) && (
<button
className="mini"
title="存进知识库"
onClick={() => void api()?.addKb(f.name).then(setKbList)}
>
存知识库
</button>
)}
<button className="mini" onClick={() => void api()?.delFile(f.name)}>
删除
</button>`
],
[
` <p className="tips">
密钥只存在本机 \`data/llm.json\`。没配置时她会用本地台词回你。
</p>`,
` <p className="tips">
密钥只存在本机 \`data/llm.json\`。没配置时她会用本地台词回你。
</p>
<div className="section-title">知识库</div>
{kbList.length === 0 && (
<div className="empty">
还没有资料。把 .md/.txt 文件拖给小钠,再到「图库 → 文件袋」点「存知识库」。
聊天提到相关内容时她会自动查阅。
</div>
)}
<div className="list">
{kbList.map((k) => (
<div className="row" key={k.name}>
<div className="row-main">
<div className="row-title">📚 {k.name}</div>
<div className="row-sub">{k.chars} 字</div>
</div>
<button className="mini" onClick={() => void api()?.delKb(k.name).then(setKbList)}>
删除
</button>
</div>
))}
</div>`
]
])
console.log('panel kb patched')