CreateWriteTextFileDiff generates a diff for write_text_file or edit_text_file tool calls. Returns the original content, modified content, and any error. For Anthropic, this returns an unimplemented error.
(ctx context.Context, chatId string, toolCallId string)
| 772 | // Returns the original content, modified content, and any error. |
| 773 | // For Anthropic, this returns an unimplemented error. |
| 774 | func CreateWriteTextFileDiff(ctx context.Context, chatId string, toolCallId string) ([]byte, []byte, error) { |
| 775 | aiChat := chatstore.DefaultChatStore.Get(chatId) |
| 776 | if aiChat == nil { |
| 777 | return nil, nil, fmt.Errorf("chat not found: %s", chatId) |
| 778 | } |
| 779 | |
| 780 | backend, err := GetBackendByAPIType(aiChat.APIType) |
| 781 | if err != nil { |
| 782 | return nil, nil, err |
| 783 | } |
| 784 | |
| 785 | funcCallInput := backend.GetFunctionCallInputByToolCallId(*aiChat, toolCallId) |
| 786 | if funcCallInput == nil { |
| 787 | return nil, nil, fmt.Errorf("tool call not found: %s", toolCallId) |
| 788 | } |
| 789 | |
| 790 | toolName := funcCallInput.Name |
| 791 | if toolName != "write_text_file" && toolName != "edit_text_file" { |
| 792 | return nil, nil, fmt.Errorf("tool call %s is not a write_text_file or edit_text_file (got: %s)", toolCallId, toolName) |
| 793 | } |
| 794 | |
| 795 | var backupFileName string |
| 796 | if funcCallInput.ToolUseData != nil { |
| 797 | backupFileName = funcCallInput.ToolUseData.WriteBackupFileName |
| 798 | } |
| 799 | |
| 800 | var parsedArguments any |
| 801 | if err := json.Unmarshal([]byte(funcCallInput.Arguments), &parsedArguments); err != nil { |
| 802 | return nil, nil, fmt.Errorf("failed to unmarshal arguments: %w", err) |
| 803 | } |
| 804 | |
| 805 | if toolName == "edit_text_file" { |
| 806 | originalContent, modifiedContent, err := EditTextFileDryRun(parsedArguments, backupFileName) |
| 807 | if err != nil { |
| 808 | return nil, nil, fmt.Errorf("failed to generate diff: %w", err) |
| 809 | } |
| 810 | return originalContent, modifiedContent, nil |
| 811 | } |
| 812 | |
| 813 | params, err := parseWriteTextFileInput(parsedArguments) |
| 814 | if err != nil { |
| 815 | return nil, nil, fmt.Errorf("failed to parse write_text_file input: %w", err) |
| 816 | } |
| 817 | |
| 818 | var originalContent []byte |
| 819 | if backupFileName != "" { |
| 820 | originalContent, err = os.ReadFile(backupFileName) |
| 821 | if err != nil { |
| 822 | return nil, nil, fmt.Errorf("failed to read backup file: %w", err) |
| 823 | } |
| 824 | } else { |
| 825 | expandedPath, err := wavebase.ExpandHomeDir(params.Filename) |
| 826 | if err != nil { |
| 827 | return nil, nil, fmt.Errorf("failed to expand path: %w", err) |
| 828 | } |
| 829 | originalContent, err = os.ReadFile(expandedPath) |
| 830 | if err != nil && !os.IsNotExist(err) { |
| 831 | return nil, nil, fmt.Errorf("failed to read original file: %w", err) |
no test coverage detected