返回 oh-my-ppt
backend.ts
根目录 / src / main / agent-runtime / agent / backend.ts
1 import type { BaseLanguageModel } from '@langchain/core/language_models/base'
2 import {
3 CompositeBackend,
4 FilesystemBackend,
5 GENERAL_PURPOSE_SUBAGENT,
6 type EditResult,
7 type WriteResult
8 } from 'deepagents'
9 import { createProductSkillsMiddlewareSet } from '../skills/backend'
10 import type { RequiredProductSkillName } from '../../product-skills'
11
12 export class GuardedFilesystemBackend extends FilesystemBackend {
13 constructor(
14 options: { rootDir?: string; virtualMode?: boolean; maxFileSizeMb?: number } & {
15 disableEditFile?: boolean
16 disableWriteFile?: boolean
17 editBlockedReason?: string
18 writeBlockedReason?: string
19 }
20 ) {
21 super(options)
22 this.disableEditFile = Boolean(options.disableEditFile)
23 this.disableWriteFile = Boolean(options.disableWriteFile)
24 this.editBlockedReason =
25 options.editBlockedReason ||
26 '当前任务禁止调用 edit_file。请使用 update_single_page_file(pageId, content) 或 update_page_file(pageId, content)。'
27 this.writeBlockedReason =
28 options.writeBlockedReason || '当前任务禁止调用 write_file。请使用受控的页面写入工具。'
29 }
30
31 private readonly disableEditFile: boolean
32 private readonly disableWriteFile: boolean
33 private readonly editBlockedReason: string
34 private readonly writeBlockedReason: string
35
36 async write(filePath: string, content: string): Promise<WriteResult> {
37 if (this.disableWriteFile) return { error: this.writeBlockedReason }
38 return super.write(filePath, content)
39 }
40
41 async edit(
42 filePath: string,
43 oldString: string,
44 newString: string,
45 replaceAll?: boolean
46 ): Promise<EditResult> {
47 if (this.disableEditFile) return { error: this.editBlockedReason }
48 return super.edit(filePath, oldString, newString, replaceAll)
49 }
50 }
51
52 export function createProductGeneralPurposeSubagent(args: {
53 model: BaseLanguageModel
54 tools: unknown[]
55 backend: FilesystemBackend | CompositeBackend
56 skillSource: string
57 requiredSkillNames: readonly RequiredProductSkillName[]
58 }): any[] {
59 if (!(args.backend instanceof CompositeBackend)) return []
60 return [
61 {
62 ...GENERAL_PURPOSE_SUBAGENT,
63 model: args.model as any,
64 tools: args.tools as any,
65 middleware: createProductSkillsMiddlewareSet(
66 args.backend,
67 args.skillSource,
68 'general-purpose',
69 args.requiredSkillNames
70 )
71 }
72 ]
73 }
74
74 lines TYPESCRIPT