Skip to content

Commit

Permalink
fixup imports, finish tea
Browse files Browse the repository at this point in the history
  • Loading branch information
dlants committed Dec 14, 2024
1 parent 37bfc3a commit 11e639e
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 207 deletions.
4 changes: 2 additions & 2 deletions rplugin/node/magenta/src/anthropic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Anthropic from "@anthropic-ai/sdk";
import { Logger } from "./logger";
import { TOOLS, ToolRequest } from "./tools/index";
import { Logger } from "./logger.js";
import { TOOLS, ToolRequest } from "./tools/index.js";

export class AnthropicClient {
private client: Anthropic;
Expand Down
12 changes: 6 additions & 6 deletions rplugin/node/magenta/src/chat.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import Anthropic from "@anthropic-ai/sdk";
import { Buffer } from "neovim";
import { Context } from "./types";
import { Context } from "./types.js";
import {
createMarkedSpaces,
getExtMark,
Mark,
replaceBetweenMarks,
} from "./utils/extmarks";
import { ToolResultBlockParam } from "@anthropic-ai/sdk/resources";
} from "./utils/extmarks.js";
import { ToolResultBlockParam } from "@anthropic-ai/sdk/resources/index.mjs";
import {
Line,
Part,
partToMessageParam,
TextPart,
ToolResultPart,
ToolUsePart,
} from "./part";
import { ToolRequest } from "./tools";
import { ToolProcess } from "./tools/types";
} from "./part.js";
import { ToolProcess } from "./tools/types.js";
import { ToolRequest } from "./tools/index.js";

type Role = "user" | "assistant";

Expand Down
56 changes: 34 additions & 22 deletions rplugin/node/magenta/src/debug/call-anthropic.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
import { AnthropicClient } from '../anthropic'
import { Logger } from '../logger'
import { AnthropicClient } from "../anthropic.js";
import { Logger } from "../logger.js";

const logger = new Logger({
outWriteLine: () => Promise.resolve(undefined),
errWriteLine: () => Promise.resolve(undefined)
}, {
level: 'trace'
})
const logger = new Logger(
{
outWriteLine: () => Promise.resolve(undefined),
errWrite: () => Promise.resolve(undefined),
errWriteLine: () => Promise.resolve(undefined),
},
{
level: "trace",
},
);

async function run() {
const client = new AnthropicClient(logger)
const client = new AnthropicClient(logger);

await client.sendMessage([{
role: 'user',
content: 'try reading the contents of the file ./src/index.js'
}], (text) => {
return Promise.resolve(console.log('text: ' + text))
})
await client.sendMessage(
[
{
role: "user",
content: "try reading the contents of the file ./src/index.js",
},
],
(text) => {
return Promise.resolve(console.log("text: " + text));
},
);
}

run().then(() => {
console.log('success');
process.exit(0)
}, (err) => {
console.error(err);
process.exit(1)
})
run().then(
() => {
console.log("success");
process.exit(0);
},
(err) => {
console.error(err);
process.exit(1);
},
);
18 changes: 9 additions & 9 deletions rplugin/node/magenta/src/magenta.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { AnthropicClient } from "./anthropic";
import { AnthropicClient } from "./anthropic.js";
import { NvimPlugin } from "neovim";
import { Sidebar } from "./sidebar";
import { Chat } from "./chat";
import { Logger } from "./logger";
import { Context } from "./types";
import { TOOLS } from "./tools/index";
import { assertUnreachable } from "./utils/assertUnreachable";
import { ToolProcess } from "./tools/types";
import { Moderator } from "./moderator";
import { Sidebar } from "./sidebar.js";
import { Chat } from "./chat.js";
import { Logger } from "./logger.js";
import { Context } from "./types.js";
import { TOOLS } from "./tools/index.js";
import { assertUnreachable } from "./utils/assertUnreachable.js";
import { ToolProcess } from "./tools/types.js";
import { Moderator } from "./moderator.js";

class Magenta {
private anthropicClient: AnthropicClient;
Expand Down
7 changes: 3 additions & 4 deletions rplugin/node/magenta/src/moderator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// as in a debate, moderator keeps track of tool state and manages turn taking in the conversation

import { ToolResultBlockParam } from "@anthropic-ai/sdk/resources";
import { ToolRequest } from "./tools";
import { ToolProcess } from "./tools/types";
import { Context } from "./types";
import { ToolResultBlockParam } from "@anthropic-ai/sdk/resources/index.mjs";
import { ToolProcess, ToolRequest } from "./tools/types.js";
import { Context } from "./types.js";

export class Moderator {
private toolProcesses: {
Expand Down
14 changes: 9 additions & 5 deletions rplugin/node/magenta/src/part.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import Anthropic from "@anthropic-ai/sdk";
import { Mark, insertBeforeMark, replaceBetweenMarks } from "./utils/extmarks";
import { ToolRequest } from "./tools/index";
import {
Mark,
insertBeforeMark,
replaceBetweenMarks,
} from "./utils/extmarks.js";
import { ToolRequest } from "./tools/index.js";
import { Buffer } from "neovim";
import { assertUnreachable } from "./utils/assertUnreachable";
import { ToolProcess } from "./tools/types";
import { Context } from "./types";
import { assertUnreachable } from "./utils/assertUnreachable.js";
import { ToolProcess } from "./tools/types.js";
import { Context } from "./types.js";

/** A line that's meant to be sent to neovim. Should not contain newlines
*/
Expand Down
126 changes: 70 additions & 56 deletions rplugin/node/magenta/src/sidebar.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import { Neovim, Buffer, Window } from "neovim";
import { Logger } from "./logger";
import { Logger } from "./logger.js";

/** This will mostly manage the window toggle
*/
export class Sidebar {
private state: {
state: 'not-loaded'
} | {
state: 'loaded';
visible: boolean;
displayBuffer: Buffer;
inputBuffer: Buffer;
displayWindow: Window;
inputWindow: Window;
}

constructor(private nvim: Neovim, private logger: Logger) {
this.state = { state: 'not-loaded' }
private state:
| {
state: "not-loaded";
}
| {
state: "loaded";
visible: boolean;
displayBuffer: Buffer;
inputBuffer: Buffer;
displayWindow: Window;
inputWindow: Window;
};

constructor(
private nvim: Neovim,
private logger: Logger,
) {
this.state = { state: "not-loaded" };
}

/** returns the input buffer when it was created
*/
async toggle(displayBuffer: Buffer): Promise<void> {
if (this.state.state == 'not-loaded') {
if (this.state.state == "not-loaded") {
await this.create(displayBuffer);
} else {
if (this.state.visible) {
Expand All @@ -35,31 +40,35 @@ export class Sidebar {

private async create(displayBuffer: Buffer): Promise<Buffer> {
const { nvim, logger } = this;
logger.trace(`sidebar.create`)
const totalHeight = await nvim.getOption('lines') as number;
const cmdHeight = await nvim.getOption('cmdheight') as number;
logger.trace(`sidebar.create`);
const totalHeight = (await nvim.getOption("lines")) as number;
const cmdHeight = (await nvim.getOption("cmdheight")) as number;
const width = 80;
const displayHeight = Math.floor((totalHeight - cmdHeight) * 0.8);
const inputHeight = totalHeight - displayHeight - 2;

await nvim.command('leftabove vsplit')
await nvim.command("leftabove vsplit");
const displayWindow = await nvim.window;
displayWindow.width = width;
await nvim.lua(`vim.api.nvim_win_set_buf(${displayWindow.id}, ${displayBuffer.id})`)
await nvim.lua(
`vim.api.nvim_win_set_buf(${displayWindow.id}, ${displayBuffer.id})`,
);

const inputBuffer = await this.nvim.createBuffer(false, true) as Buffer;
const inputBuffer = (await this.nvim.createBuffer(false, true)) as Buffer;

await nvim.command('below split')
await nvim.command("below split");
const inputWindow = await nvim.window;
inputWindow.height = inputHeight;
await nvim.lua(`vim.api.nvim_win_set_buf(${inputWindow.id}, ${inputBuffer.id})`)
await nvim.lua(
`vim.api.nvim_win_set_buf(${inputWindow.id}, ${inputBuffer.id})`,
);

await inputBuffer.setOption('buftype', 'nofile');
await inputBuffer.setOption('swapfile', false);
await inputBuffer.setLines(['> '], {
await inputBuffer.setOption("buftype", "nofile");
await inputBuffer.setOption("swapfile", false);
await inputBuffer.setLines(["> "], {
start: 0,
end: -1,
strictIndexing: false
strictIndexing: false,
});

const winOptions = {
Expand All @@ -74,30 +83,30 @@ export class Sidebar {
await inputWindow.setOption(key, value);
}

await inputBuffer.request('nvim_buf_set_keymap', [inputBuffer,
'n',
'<CR>',
':Magenta send<CR>',
{ silent: true, noremap: true }
await inputBuffer.request("nvim_buf_set_keymap", [
inputBuffer,
"n",
"<CR>",
":Magenta send<CR>",
{ silent: true, noremap: true },
]);

logger.trace(`sidebar.create setting state`)
logger.trace(`sidebar.create setting state`);
this.state = {
state: 'loaded',
state: "loaded",
visible: true,
displayBuffer,
inputBuffer,
displayWindow,
inputWindow
}

inputWindow,
};

return inputBuffer;
}

async hide() { }
async hide() {}

async show() { }
async show() {}

async scrollTop() {
// const { displayWindow } = await this.getWindowIfVisible();
Expand All @@ -112,42 +121,47 @@ export class Sidebar {
// }
}

async getWindowIfVisible(): Promise<{ displayWindow?: Window, inputWindow?: Window }> {
if (this.state.state != 'loaded') {
async getWindowIfVisible(): Promise<{
displayWindow?: Window;
inputWindow?: Window;
}> {
if (this.state.state != "loaded") {
return {};
}

const { displayWindow, inputWindow } = this.state;
const displayWindowValid = await displayWindow.valid
const inputWindowValid = await inputWindow.valid
const displayWindowValid = await displayWindow.valid;
const inputWindowValid = await inputWindow.valid;

return {
displayWindow: displayWindowValid ? displayWindow : undefined,
inputWindow: inputWindowValid ? inputWindow : undefined
}
inputWindow: inputWindowValid ? inputWindow : undefined,
};
}

async getMessage(): Promise<string> {
if (this.state.state != 'loaded') {
this.logger.trace(`sidebar state is ${this.state.state} in getMessage`)
return '';
if (this.state.state != "loaded") {
this.logger.trace(`sidebar state is ${this.state.state} in getMessage`);
return "";
}

const { inputBuffer } = this.state
const { inputBuffer } = this.state;

const lines = await inputBuffer.getLines({
start: 0,
end: -1,
strictIndexing: false
})
strictIndexing: false,
});

this.logger.trace(`sidebar got lines ${JSON.stringify(lines)} from inputBuffer`)
const message = lines.join('\n');
await inputBuffer.setLines([''], {
this.logger.trace(
`sidebar got lines ${JSON.stringify(lines)} from inputBuffer`,
);
const message = lines.join("\n");
await inputBuffer.setLines([""], {
start: 0,
end: -1,
strictIndexing: false
})
strictIndexing: false,
});

return message;
}
Expand Down
Loading

0 comments on commit 11e639e

Please sign in to comment.