Download packages/agent-core-v2/src/app/edit/fileEditService.ts from SaylorTwift/kimi-code: direct link, hf CLI and curl.
- Browser
- Download file 1.77 kB
-
https://huggingface.co/SaylorTwift/kimi-code/resolve/main/packages/agent-core-v2/src/app/edit/fileEditService.ts
- Command line
-
hf download hf://SaylorTwift/kimi-code/packages/agent-core-v2/src/app/edit/fileEditService.ts
-
curl -L -o fileEditService.ts https://huggingface.co/SaylorTwift/kimi-code/resolve/main/packages/agent-core-v2/src/app/edit/fileEditService.ts
1.77 kB
| import { LifecycleScope } from '#/app/scopes'; | |
| import { ScopeActivation, registerScopedService } from '#/_base/di/scope'; | |
| import { unwrapErrorCause } from '#/_base/errors/errors'; | |
| import { IHostFileSystem } from '#/os/interface/hostFileSystem'; | |
| import { EditService } from './editService'; | |
| import { type FileEditInput, type FileEditResult, IFileEditService } from './fileEdit'; | |
| import { TextModel } from './textModel'; | |
| export class FileEditService implements IFileEditService { | |
| declare readonly _serviceBrand: undefined; | |
| private readonly editor: EditService; | |
| constructor( private readonly fs: IHostFileSystem) { | |
| this.editor = new EditService(); | |
| } | |
| async edit(input: FileEditInput, fs: IHostFileSystem = this.fs): Promise<FileEditResult> { | |
| try { | |
| const raw = await fs.readText(input.path, { errors: 'strict' }); | |
| const model = new TextModel(raw); | |
| const result = this.editor.apply(model, { | |
| path: input.displayPath, | |
| old_string: input.old_string, | |
| new_string: input.new_string, | |
| replace_all: input.replace_all, | |
| }); | |
| if (!result.ok) { | |
| return { ok: false, error: result.error }; | |
| } | |
| await fs.writeText(input.path, result.rawContent); | |
| return { ok: true, count: result.count }; | |
| } catch (error) { | |
| const code = (unwrapErrorCause(error) as { code?: unknown } | null)?.code; | |
| if (code === 'EISDIR') { | |
| return { ok: false, error: `${input.displayPath} is not a file.` }; | |
| } | |
| return { | |
| ok: false, | |
| error: error instanceof Error ? error.message : String(error), | |
| }; | |
| } | |
| } | |
| } | |
| registerScopedService( | |
| LifecycleScope.App, | |
| IFileEditService, | |
| FileEditService, | |
| ScopeActivation.OnScopeCreated, | |
| 'edit', | |
| ); | |