Skip to content

Commit

Permalink
refactor aborting requests, add test for aborting inline requests
Browse files Browse the repository at this point in the history
  • Loading branch information
dlants committed Jan 20, 2025
1 parent 27adbee commit a0099b2
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 17 deletions.
13 changes: 0 additions & 13 deletions node/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ export type Msg =
| {
type: "clear";
}
| {
type: "abort";
}
| {
type: "tool-manager-msg";
msg: ToolManager.Msg;
Expand Down Expand Up @@ -420,16 +417,6 @@ ${msg.error.stack}`,
return [initModel()];
}

case "abort": {
return [
model,
// eslint-disable-next-line @typescript-eslint/require-await
async () => {
getProvider(nvim, model.activeProvider, model.options).abort();
},
];
}

case "set-opts": {
return [{ ...model, options: msg.options }];
}
Expand Down
2 changes: 1 addition & 1 deletion node/chat/part.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function init({ nvim, lsp }: { nvim: Nvim; lsp: Lsp }) {

case "malformed-tool-request":
return d`Malformed tool request: ${model.error}
${JSON.stringify(model.rawRequest, null, 2)}`;
${JSON.stringify(model.rawRequest, null, 2) || "undefined"}`;

case "tool-request": {
const toolModel = toolManager.toolWrappers[model.requestId];
Expand Down
31 changes: 31 additions & 0 deletions node/inline-edit/inline-edit-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,35 @@ Silver shadows dance with ease.
expect(request.messages).toMatchSnapshot();
});
});

it("abort command should work", async () => {
await withDriver(async (driver) => {
await driver.editFile("node/test/fixtures/poem.txt");
const targetBuffer = await getCurrentBuffer(driver.nvim);

// Select a range of text
await driver.selectRange(
{ row: 1, col: 0 } as Position0Indexed,
{ row: 1, col: 32 } as Position0Indexed,
);

await driver.startInlineEditWithSelection();
await driver.assertWindowCount(2);

const inputBuffer = await getCurrentBuffer(driver.nvim);
await inputBuffer.setLines({
start: 0,
end: -1,
lines: ["Please change 'Silver' to 'Golden'"] as Line[],
});

// eslint-disable-next-line @typescript-eslint/no-floating-promises
driver.submitInlineEdit(targetBuffer.id);
const request = await driver.mockAnthropic.awaitPendingReplaceRequest();
expect(request.defer.resolved).toBe(false);

await driver.abort();
expect(request.defer.resolved).toBe(true);
});
});
});
9 changes: 9 additions & 0 deletions node/inline-edit/inline-edit-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ export class InlineEditManager {
);
}

destroy() {
Object.entries(this.inlineEdits).map(([bufnr, edit]) => {
delete this.inlineEdits[bufnr as unknown as BufNr];
edit.app.destroy();
});
}

async initInlineEdit(selection?: {
startPos: Position1Indexed;
endPos: Position1Indexed;
Expand Down Expand Up @@ -349,4 +356,6 @@ ${input.find}
});
}
}

abort() {}
}
2 changes: 1 addition & 1 deletion node/inline-edit/inline-edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const view: View<{ model: Model; dispatch: Dispatch<Msg> }> = ({
case "tool-use":
switch (model.edit.status) {
case "error":
return d`Error: ${model.edit.error}, rawRequest: ${JSON.stringify(model.edit.rawRequest, null, 2)}`;
return d`Error: ${model.edit.error}, rawRequest: ${JSON.stringify(model.edit.rawRequest, null, 2) || "undefined"}`;
case "ok":
return d`Got tool use: ${JSON.stringify(model.edit.value, null, 2)}`;
default:
Expand Down
20 changes: 18 additions & 2 deletions node/magenta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,23 @@ export class Magenta {
this.chatApp.dispatch({ type: "clear" });
break;

case "abort":
this.chatApp.dispatch({ type: "abort" });
case "abort": {
const chat = this.chatApp.getState();
if (chat.status !== "running") {
this.nvim.logger?.error(`Chat is not running.`);
return;
}

const provider = getProvider(
this.nvim,
chat.model.activeProvider,
chat.model.options,
);

provider.abort();

break;
}

case "paste-selection": {
const [startPos, endPos, cwd, currentBuffer] = await Promise.all([
Expand Down Expand Up @@ -231,6 +245,7 @@ ${lines.join("\n")}
chat.model.activeProvider,
chat.model.options,
);

const messages = await this.chatModel.getMessages(chat.model);
await this.inlineEditManager.submitInlineEdit(
bufnr,
Expand Down Expand Up @@ -272,6 +287,7 @@ ${lines.join("\n")}
this.mountedChatApp.unmount();
this.mountedChatApp = undefined;
}
this.inlineEditManager.destroy();
}

static async start(nvim: Nvim) {
Expand Down
36 changes: 36 additions & 0 deletions node/providers/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,42 @@ export class MockProvider implements Provider {
});
}
}

if (this.inlineRequests.length) {
const lastRequest = this.inlineRequests[this.inlineRequests.length - 1];
if (!lastRequest.defer.resolved) {
lastRequest.defer.resolve({
inlineEdit: {
status: "error",
error: "aborted",
rawRequest: undefined,
},
stopReason: "end_turn",
usage: {
inputTokens: 0,
outputTokens: 0,
},
});
}
}

if (this.replaceRequests.length) {
const lastRequest = this.replaceRequests[this.replaceRequests.length - 1];
if (!lastRequest.defer.resolved) {
lastRequest.defer.resolve({
replaceSelection: {
status: "error",
error: "aborted",
rawRequest: undefined,
},
stopReason: "end_turn",
usage: {
inputTokens: 0,
outputTokens: 0,
},
});
}
}
}

createStreamParameters(messages: Array<ProviderMessage>): unknown {
Expand Down

0 comments on commit a0099b2

Please sign in to comment.