Refactor auto-suggestion (#40)

This commit is contained in:
Will Chen
2025-04-28 22:43:37 -07:00
committed by GitHub
parent 322fcb002d
commit 7ab1aab9b0
4 changed files with 87 additions and 13 deletions

View File

@@ -224,6 +224,23 @@ const getProposalHandler = async (
);
}
}
const actions: ActionProposal["actions"] = [];
if (latestAssistantMessage?.content) {
const writeTags = getDyadWriteTags(latestAssistantMessage.content);
const refactorTarget = writeTags.reduce((largest, tag) => {
const lineCount = tag.content.split("\n").length;
return lineCount > 500 && (!largest || lineCount > largest.lineCount)
? { path: tag.path, lineCount }
: largest;
}, null as { path: string; lineCount: number } | null);
if (refactorTarget) {
actions.push({
id: "refactor-file",
path: refactorTarget.path,
});
}
}
// Get all chat messages to calculate token usage
const chat = await db.query.chats.findFirst({
where: eq(chats.id, chatId),
@@ -260,16 +277,21 @@ const getProposalHandler = async (
logger.log(
`Token usage high (${totalTokens}/${contextWindow}), suggesting summarize action`
);
return {
proposal: {
type: "action-proposal",
actions: [{ id: "summarize-in-new-chat" }],
},
chatId,
messageId: latestAssistantMessage.id,
};
actions.push({
id: "summarize-in-new-chat",
});
}
}
if (actions.length > 0 && latestAssistantMessage) {
return {
proposal: {
type: "action-proposal",
actions: actions,
},
chatId,
messageId: latestAssistantMessage.id,
};
}
return null;
} catch (error) {
logger.error(`Error processing proposal for chatId ${chatId}:`, error);