generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin/chat): add a way to modify the text
- Loading branch information
1 parent
e28ca76
commit 520f8fa
Showing
5 changed files
with
130 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/plugin/views/assistant/ai-chat/tool-handlers/modify-text-handler.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import React, { useRef, useState } from "react"; | ||
import { App, TFile } from "obsidian"; | ||
import { logger } from "../../../../services/logger"; | ||
|
||
interface ModifyTextHandlerProps { | ||
toolInvocation: any; | ||
handleAddResult: (result: string) => void; | ||
app: App; | ||
} | ||
|
||
export function ModifyTextHandler({ | ||
toolInvocation, | ||
handleAddResult, | ||
app, | ||
}: ModifyTextHandlerProps) { | ||
const hasFetchedRef = useRef(false); | ||
const [modifySuccess, setModifySuccess] = useState<boolean | null>(null); | ||
|
||
React.useEffect(() => { | ||
const handleModifyText = async () => { | ||
if (!hasFetchedRef.current && !("result" in toolInvocation)) { | ||
hasFetchedRef.current = true; | ||
const { content, path } = toolInvocation.args; | ||
try { | ||
let targetFile: TFile; | ||
|
||
if (path) { | ||
targetFile = app.vault.getAbstractFileByPath(path) as TFile; | ||
if (!targetFile) { | ||
throw new Error(`File not found at path: ${path}`); | ||
} | ||
} else { | ||
// Get the active file | ||
targetFile = app.workspace.getActiveFile(); | ||
if (!targetFile) { | ||
throw new Error("No active file found"); | ||
} | ||
} | ||
|
||
// Get editor if it exists | ||
const editor = app.workspace.activeEditor?.editor; | ||
|
||
if (editor) { | ||
// If we have an editor, replace the selected text or entire content | ||
const selection = editor.getSelection(); | ||
if (selection) { | ||
// If there's selected text, replace just that | ||
editor.replaceSelection(content); | ||
} else { | ||
// If no selection, replace entire content | ||
editor.setValue(content); | ||
} | ||
} else { | ||
// If no editor is open, modify the entire file | ||
await app.vault.modify(targetFile, content); | ||
} | ||
|
||
logger.info(`Successfully modified text in document: ${targetFile.path}`); | ||
handleAddResult(`Successfully modified text in document${path ? `: ${path}` : ""}`); | ||
setModifySuccess(true); | ||
} catch (error) { | ||
logger.error("Error modifying text in document:", error); | ||
handleAddResult(`Error: ${error.message}`); | ||
setModifySuccess(false); | ||
} | ||
} | ||
}; | ||
|
||
handleModifyText(); | ||
}, [toolInvocation, handleAddResult, app]); | ||
|
||
if (modifySuccess === null) { | ||
return <div className="text-sm text-[--text-muted]">Modifying text in document...</div>; | ||
} | ||
|
||
if (modifySuccess) { | ||
return <div className="text-sm text-[--text-muted]">Text successfully modified in document</div>; | ||
} | ||
|
||
return <div className="text-sm text-[--text-error]">Failed to modify text in document</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters