Add git autosync functionality

This commit is contained in:
Alex Gleason committed 2025-12-29 16:00:23 -06:00
1 parent 0ee88f5968
commit 758e3ab261
7 files changed
+201 -51

No files matched your search

+10 -2
View File
@@ -15,6 +15,7 @@ import { useAIChat } from '@/hooks/useAIChat';
import { useProviderModels } from '@/hooks/useProviderModels';
import { useSessionManager } from '@/hooks/useSessionManager';
import { useMCPTools } from '@/hooks/useMCPTools';
import { useQueryClient } from '@tanstack/react-query';
import { AIMessageItem } from '@/components/AIMessageItem';
import { ToolCallDisplay } from '@/components/ToolCallDisplay';
import { TextEditorViewTool } from '@/lib/tools/TextEditorViewTool';
@@ -87,6 +88,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
const { models } = useProviderModels();
const { config } = useAppContext();
const { projectsPath, tmpPath, pluginsPath } = useFSPaths();
const queryClient = useQueryClient();
const [showScrollToBottom, setShowScrollToBottom] = useState(false);
const [isDragOver, setIsDragOver] = useState(false);
const [scrolledProjects] = useState(() => new Set<string>());
@@ -159,13 +161,19 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
sessionManager.emit('fileChanged', projectId, filePath);
}, [sessionManager, projectId]);
// Callback to trigger sync after git commits
const handleCommit = useCallback(() => {
// Invalidate the git-sync query to trigger an immediate sync
queryClient.invalidateQueries({ queryKey: ['git-sync', cwd] });
}, [queryClient, cwd]);
// Fetch MCP tools
const { tools: mcpOpenAITools, clients: mcpClients } = useMCPTools();
// Separate built-in tools from MCP tools for clarity
const builtInTools = useMemo(() => {
const tools: Record<string, Tool<unknown>> = {
git_commit: new GitCommitTool(fs, cwd, git),
git_commit: new GitCommitTool(fs, cwd, git, { onCommit: handleCommit }),
text_editor_view: new TextEditorViewTool(fs, cwd, { projectsPath }),
text_editor_write: new TextEditorWriteTool(fs, cwd, { projectsPath, tmpPath, onFileChanged: handleFileChanged }),
text_editor_str_replace: new TextEditorStrReplaceTool(fs, cwd, { projectsPath, tmpPath, onFileChanged: handleFileChanged }),
@@ -234,7 +242,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
}
return tools;
}, [fs, git, cwd, user, projectsPath, tmpPath, pluginsPath, config.corsProxy, settings, aiSettings, models, handleFileChanged]);
}, [fs, git, cwd, user, projectsPath, tmpPath, pluginsPath, config.corsProxy, settings, aiSettings, models, handleFileChanged, handleCommit]);
// MCP tools wrapped for execution
const mcpToolWrappers = useMemo(() => createMCPTools(mcpClients), [mcpClients]);
@@ -193,6 +193,13 @@ export function ConfigureRepoStep({
});
}
// Enable autosync by default after successfully setting up the remote
await git.setConfig({
dir,
path: 'shakespeare.autosync',
value: 'true',
});
// Refresh git status before showing success
await refetchGitStatus();
// Show success state
+57 -1
View File
@@ -1,8 +1,10 @@
import { useState, useCallback } from 'react';
import { useState, useCallback, useEffect } from 'react';
import { Zap, Copy, Check, RefreshCw, EllipsisVertical, CloudOff, ExternalLink, ChevronDown, ArrowDown, ArrowUp, LoaderCircle, History } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Switch } from '@/components/ui/switch';
import { useQueryClient } from '@tanstack/react-query';
import {
DropdownMenu,
DropdownMenuContent,
@@ -41,6 +43,8 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
const [showForcePullDialog, setShowForcePullDialog] = useState(false);
const [showForcePushDialog, setShowForcePushDialog] = useState(false);
const [gitHistoryOpen, setGitHistoryOpen] = useState(false);
const [autosync, setAutosync] = useState<boolean>(false);
const [isLoadingAutosync, setIsLoadingAutosync] = useState(true);
const { data: gitStatus } = useGitStatus(projectId);
const { settings } = useGitSettings();
@@ -55,6 +59,45 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
const commitsAhead = gitStatus?.ahead ?? 0;
const commitsBehind = gitStatus?.behind ?? 0;
// Load autosync config on mount
useEffect(() => {
const loadAutosync = async () => {
try {
const value = await git.getConfig({
dir,
path: 'shakespeare.autosync',
});
setAutosync(value === 'true');
} catch {
// Config not set, default to false
setAutosync(false);
} finally {
setIsLoadingAutosync(false);
}
};
loadAutosync();
}, [git, dir]);
// Handler to toggle autosync
const handleAutosyncToggle = async (checked: boolean) => {
try {
await git.setConfig({
dir,
path: 'shakespeare.autosync',
value: String(checked),
});
setAutosync(checked);
// If enabling autosync, trigger an immediate sync
if (checked) {
queryClient.invalidateQueries({ queryKey: ['git-sync', dir] });
}
} catch (error) {
console.error('Failed to set autosync config:', error);
}
};
// Generic git operation handler
const executeGitOperation = useCallback(async (
operation: GitOperation,
@@ -306,6 +349,19 @@ export function SyncStep({ projectId, remoteUrl }: SyncStepProps) {
</div>
</div>
<div className="flex items-center gap-1.5">
<Switch
id="autosync"
checked={autosync}
onCheckedChange={handleAutosyncToggle}
disabled={isLoadingAutosync}
className="scale-75"
/>
<Label htmlFor="autosync" className="text-xs cursor-pointer font-normal text-muted-foreground">
Automatic sync
</Label>
</div>
{error && (
<SyncStepError
error={error}