generated from ellisonleao/nvim-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getFile.ts
242 lines (219 loc) · 6.2 KB
/
getFile.ts
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
import * as Anthropic from "@anthropic-ai/sdk";
import { getBufferIfOpen } from "../utils/buffers.ts";
import fs from "fs";
import path from "path";
import { assertUnreachable } from "../utils/assertUnreachable.ts";
import { ToolResultBlockParam } from "@anthropic-ai/sdk/resources/index.mjs";
import { Thunk, Update } from "../tea/tea.ts";
import { d, VDOMNode } from "../tea/view.ts";
import { context } from "../context.ts";
import { ToolRequestId } from "./toolManager.ts";
import { Result } from "../utils/result.ts";
export type Model = {
type: "get_file";
autoRespond: boolean;
request: GetFileToolUseRequest;
state:
| {
state: "processing";
}
| {
state: "pending-user-action";
}
| {
state: "done";
result: ToolResultBlockParam;
};
};
export type Msg = {
type: "finish";
result: ToolResultBlockParam;
};
export const update: Update<Msg, Model> = (msg, model) => {
switch (msg.type) {
case "finish":
return [
{
...model,
state: {
state: "done",
result: msg.result,
},
},
];
default:
assertUnreachable(msg.type);
}
};
export function initModel(request: GetFileToolUseRequest): [Model, Thunk<Msg>] {
const model: Model = {
type: "get_file",
autoRespond: true,
request,
state: {
state: "processing",
},
};
return [
model,
async (dispatch) => {
const filePath = model.request.input.filePath;
context.logger.trace(`request: ${JSON.stringify(model.request)}`);
const bufferContents = await getBufferIfOpen({
relativePath: filePath,
});
if (bufferContents.status === "ok") {
dispatch({
type: "finish",
result: {
type: "tool_result",
tool_use_id: model.request.id,
content: bufferContents.result,
is_error: false,
},
});
return;
}
if (bufferContents.status === "error") {
dispatch({
type: "finish",
result: {
type: "tool_result",
tool_use_id: request.id,
content: bufferContents.error,
is_error: true,
},
});
return;
}
try {
const cwd = (await context.nvim.call("getcwd")) as string;
const absolutePath = path.resolve(cwd, filePath);
if (!absolutePath.startsWith(cwd)) {
dispatch({
type: "finish",
result: {
type: "tool_result",
tool_use_id: model.request.id,
content: "The path must be inside of neovim cwd",
is_error: true,
},
});
return;
}
const fileContent = await fs.promises.readFile(absolutePath, "utf-8");
dispatch({
type: "finish",
result: {
type: "tool_result",
tool_use_id: model.request.id,
content: fileContent,
is_error: false,
},
});
return;
} catch (error) {
dispatch({
type: "finish",
result: {
type: "tool_result",
tool_use_id: model.request.id,
content: `Failed to read file: ${(error as Error).message}`,
is_error: true,
},
});
}
},
];
}
export function view({ model }: { model: Model }): VDOMNode {
switch (model.state.state) {
case "processing":
return d`⚙️ Reading file ${model.request.input.filePath}`;
case "pending-user-action":
return d`⏳ Pending approval to read file ${model.request.input.filePath}`;
case "done":
return d`✅ Finished reading file ${model.request.input.filePath}`;
default:
assertUnreachable(model.state);
}
}
export function getToolResult(model: Model): ToolResultBlockParam {
switch (model.state.state) {
case "processing":
return {
type: "tool_result",
tool_use_id: model.request.id,
content: `This tool use is being processed. Please proceed with your answer or address other parts of the question.`,
};
case "pending-user-action":
return {
type: "tool_result",
tool_use_id: model.request.id,
content: `Waiting for a user action to finish processing this tool use. Please proceed with your answer or address other parts of the question.`,
};
case "done":
return model.state.result;
default:
assertUnreachable(model.state);
}
}
export const spec: Anthropic.Anthropic.Tool = {
name: "get_file",
description: `Get the full contents of a file in the project directory.`,
input_schema: {
type: "object",
properties: {
filePath: {
type: "string",
description:
"the path, relative to the project root, of the file. e.g. ./src/index.ts",
},
},
required: ["filePath"],
},
};
export type GetFileToolUseRequest = {
type: "tool_use";
id: ToolRequestId; //"toolu_01UJtsBsBED9bwkonjqdxji4"
name: "get_file";
input: {
filePath: string; //"./src/index.ts"
};
};
export function displayRequest(request: GetFileToolUseRequest) {
return `get_file: {
filePath: ${request.input.filePath}
}`;
}
export function validateToolRequest(
req: unknown,
): Result<GetFileToolUseRequest> {
if (typeof req != "object" || req == null) {
return { status: "error", error: "received a non-object" };
}
const req2 = req as { [key: string]: unknown };
if (req2.type != "tool_use") {
return { status: "error", error: "expected req.type to be tool_use" };
}
if (typeof req2.id != "string") {
return { status: "error", error: "expected req.id to be a string" };
}
if (req2.name != "get_file") {
return { status: "error", error: "expected req.name to be insert" };
}
if (typeof req2.input != "object" || req2.input == null) {
return { status: "error", error: "expected req.input to be an object" };
}
const input = req2.input as { [key: string]: unknown };
if (typeof input.filePath != "string") {
return {
status: "error",
error: "expected req.input.filePath to be a string",
};
}
return {
status: "ok",
value: req as GetFileToolUseRequest,
};
}