-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGitToolsModule.ts
More file actions
243 lines (231 loc) · 7.1 KB
/
GitToolsModule.ts
File metadata and controls
243 lines (231 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// src/controls/modules/GitToolsModule.ts
// FULL FILE
import { type ControlModule } from "@/types/litechat/control";
import {
type LiteChatModApi,
type ReadonlyChatContextSnapshot,
} from "@/types/litechat/modding";
import { useSettingsStore } from "@/store/settings.store";
import * as VfsOps from "@/lib/litechat/vfs-operations";
import { z } from "zod";
import { Tool } from "ai";
import type { fs as FsType } from "@zenfs/core"; // Corrected import
const gitInitSchema = z.object({
path: z
.string()
.describe(
"The directory path within the VFS to initialize as a Git repository."
),
});
const gitCommitSchema = z.object({
path: z
.string()
.describe(
"The directory path of the Git repository within the VFS to commit."
),
message: z.string().describe("The commit message."),
});
const gitPullSchema = z.object({
path: z
.string()
.describe(
"The directory path of the Git repository within the VFS to pull from."
),
branch: z.string().optional().describe("The branch name to pull."),
});
const gitPushSchema = z.object({
path: z
.string()
.describe(
"The directory path of the Git repository within the VFS to push."
),
branch: z.string().optional().describe("The branch name to push."),
});
const gitStatusSchema = z.object({
path: z
.string()
.describe(
"The directory path of the Git repository within the VFS to check status."
),
});
// Corrected ToolContext to use FsType from @zenfs/core
type ToolContext = ReadonlyChatContextSnapshot & {
fsInstance?: typeof FsType;
};
export class GitToolsModule implements ControlModule {
readonly id = "core-git-tools";
private unregisterCallbacks: (() => void)[] = [];
async initialize(_modApi: LiteChatModApi): Promise<void> {
// console.log(`[${this.id}] Initialized.`);
}
register(modApi: LiteChatModApi): void {
if (this.unregisterCallbacks.length > 0) {
console.warn(`[${this.id}] Already registered. Skipping.`);
return;
}
// console.log(`[${this.id}] Registering Core Git Tools...`);
const gitInitTool: Tool<any> = {
description: "Initialize an empty Git repository in a VFS directory.",
inputSchema: gitInitSchema,
};
this.unregisterCallbacks.push(
modApi.registerTool(
"gitInit",
gitInitTool,
async (
{ path }: z.infer<typeof gitInitSchema>,
context: ToolContext
) => {
const fsInstance = context?.fsInstance;
if (!fsInstance) {
return {
success: false,
error: "Filesystem instance not available in context.",
};
}
try {
await VfsOps.gitInitOp(path, { fsInstance });
return {
success: true,
message: `Repository initialized at ${path}`,
};
} catch (e: any) {
return { success: false, error: e.message };
}
}
)
);
const gitCommitTool: Tool<any> = {
description: "Stage all changes and commit them in a VFS Git repository.",
inputSchema: gitCommitSchema,
};
this.unregisterCallbacks.push(
modApi.registerTool(
"gitCommit",
gitCommitTool,
async (
{ path, message }: z.infer<typeof gitCommitSchema>,
context: ToolContext
) => {
const fsInstance = context?.fsInstance;
if (!fsInstance) {
return {
success: false,
error: "Filesystem instance not available in context.",
};
}
const currentSettings = useSettingsStore.getState();
if (!currentSettings.gitUserName || !currentSettings.gitUserEmail) {
return {
success: false,
error:
"Git user name and email not configured in settings. Cannot commit.",
};
}
try {
await VfsOps.gitCommitOp(path, message, { fsInstance });
return { success: true, message: `Changes committed in ${path}` };
} catch (e: any) {
return { success: false, error: e.message };
}
}
)
);
const gitPullTool: Tool<any> = {
description:
"Pull changes from the remote repository for the specified branch.",
inputSchema: gitPullSchema,
};
this.unregisterCallbacks.push(
modApi.registerTool(
"gitPull",
gitPullTool,
async (
{ path, branch }: z.infer<typeof gitPullSchema>,
context: ToolContext
) => {
const fsInstance = context?.fsInstance;
if (!fsInstance) {
return {
success: false,
error: "Filesystem instance not available in context.",
};
}
try {
await VfsOps.gitPullOp(path, branch || "main", undefined, {
fsInstance,
});
return { success: true, message: `Pulled changes for ${path}` };
} catch (e: any) {
return { success: false, error: e.message };
}
}
)
);
const gitPushTool: Tool<any> = {
description:
"Push committed changes from the local branch to the remote repository.",
inputSchema: gitPushSchema,
};
this.unregisterCallbacks.push(
modApi.registerTool(
"gitPush",
gitPushTool,
async (
{ path, branch }: z.infer<typeof gitPushSchema>,
context: ToolContext
) => {
const fsInstance = context?.fsInstance;
if (!fsInstance) {
return {
success: false,
error: "Filesystem instance not available in context.",
};
}
try {
await VfsOps.gitPushOp(path, branch || "main", undefined, {
fsInstance,
});
return { success: true, message: `Pushed changes for ${path}` };
} catch (e: any) {
return { success: false, error: e.message };
}
}
)
);
const gitStatusTool: Tool<any> = {
description: "Get the Git status for a VFS repository.",
inputSchema: gitStatusSchema,
};
this.unregisterCallbacks.push(
modApi.registerTool(
"gitStatus",
gitStatusTool,
async (
{ path }: z.infer<typeof gitStatusSchema>,
context: ToolContext
) => {
const fsInstance = context?.fsInstance;
if (!fsInstance) {
return {
success: false,
error: "Filesystem instance not available in context.",
};
}
try {
await VfsOps.gitStatusOp(path, { fsInstance });
return { success: true, message: `Status checked for ${path}` };
} catch (e: any) {
return { success: false, error: e.message };
}
}
)
);
// console.log(`[${this.id}] Core Git Tools Registered.`);
}
destroy(): void {
this.unregisterCallbacks.forEach((unsub) => unsub());
this.unregisterCallbacks = [];
console.log(`[${this.id}] Destroyed.`);
}
}