Skip to content

Commit d5dcd59

Browse files
committed
WIP
1 parent ce755e9 commit d5dcd59

File tree

2 files changed

+17
-34
lines changed

2 files changed

+17
-34
lines changed

src/agent/context/contextWindow.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ModelConfig } from "@/config.js";
44
import logger from "@/logger.js";
55

66
function getTokenCount(text: string): number {
7+
if (text.length >= 12000) return Math.ceil(text.length * 0.4);
78
return encode(text).length;
89
}
910

@@ -31,22 +32,22 @@ function getMessageTokenCount(message: ModelMessage): number {
3132
tokens += getTokenCount(part.text);
3233
} else if ("output" in part && part.output) {
3334
if (typeof part.output === "object" && "value" in part.output) {
34-
tokens += getTokenCount(serializeContent(part.output.value));
35+
tokens += getTokenCount(serializeContent((part.output as any).value));
3536
} else {
36-
tokens += getTokenCount(serializeContent(part.output));
37+
tokens += getTokenCount(serializeContent((part as any).output));
3738
}
38-
} else if ("toolName" in part && typeof part.toolName === "string") {
39-
tokens += getTokenCount(part.toolName);
39+
} else if ("toolName" in part && typeof (part as any).toolName === "string") {
40+
tokens += getTokenCount((part as any).toolName as string);
4041
tokens += 10;
41-
if ("args" in part && part.args) {
42-
tokens += getTokenCount(serializeContent(part.args));
42+
if ("args" in part && (part as any).args) {
43+
tokens += getTokenCount(serializeContent((part as any).args));
4344
}
4445
} else {
45-
tokens += getTokenCount(serializeContent(part));
46+
tokens += getTokenCount(serializeContent(part as any));
4647
}
4748
}
4849
} else {
49-
tokens += getTokenCount(serializeContent(message.content));
50+
tokens += getTokenCount(serializeContent(message.content as any));
5051
}
5152

5253
tokens += 4;

src/agent/tools/definitions/edit.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,17 @@ export const editTool = tool({
8484
switch (edit.type) {
8585
case "replace": {
8686
if (!originalContent.includes(edit.search)) {
87-
// Check if autofix is available and enabled
88-
const shouldAttemptAutofix =
89-
process.env.OPENAI_API_KEY &&
90-
process.env.ENABLE_EDIT_AUTOFIX !== "false";
91-
92-
if (shouldAttemptAutofix) {
87+
try {
9388
logger.warn(`Search string not found in file. Attempting autofix...`);
94-
try {
95-
const correctedSearch = await autofixEdit(
96-
originalContent,
97-
edit.search,
98-
);
99-
if (correctedSearch) {
100-
logger.info(
101-
"Autofix successful, using corrected search string",
102-
);
103-
newContent = originalContent.replace(
104-
correctedSearch,
105-
edit.replaceWith,
106-
);
107-
break;
108-
}
109-
} catch (autofixError) {
110-
logger.error("Autofix threw an error:", autofixError);
111-
// Fall through to the error below
89+
const correctedSearch = await autofixEdit(originalContent, edit.search);
90+
if (correctedSearch) {
91+
logger.info("Autofix successful, using corrected search string");
92+
newContent = originalContent.replace(correctedSearch, edit.replaceWith);
93+
break;
11294
}
95+
} catch (autofixError) {
96+
logger.error("Autofix threw an error:", autofixError);
11397
}
114-
115-
// Autofix failed or disabled
11698
throw new ToolError(
11799
`The search string was not found in the file. ` +
118100
`Expected to find:\n"${edit.search.substring(0, 100)}${edit.search.length > 100 ? "..." : ""}"\n\n` +

0 commit comments

Comments
 (0)