Skip to content

Commit

Permalink
port to bun!
Browse files Browse the repository at this point in the history
  • Loading branch information
dlants committed Dec 27, 2024
1 parent 0ea2a54 commit 8c28399
Show file tree
Hide file tree
Showing 55 changed files with 1,072 additions and 2,033 deletions.
9 changes: 9 additions & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
"Lua.diagnostics.globals": ["vim"],
"Lua.workspace.library": ["lua", "${3rd}/luv/library"],
"Lua.workspace.checkThirdParty": false,
"Lua.runtime.version": "LuaJIT",
"Lua.workspace.ignoreDir": ["lua_modules", ".git"],
"Lua.diagnostics.disable": ["different-requires"]
}
175 changes: 175 additions & 0 deletions bun/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
15 changes: 15 additions & 0 deletions bun/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# magenta.nvim

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run index.ts
```

This project was created using `bun init` in bun v1.1.38. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
Binary file added bun/bun.lockb
Binary file not shown.
File renamed without changes.
17 changes: 17 additions & 0 deletions bun/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "magenta.nvim",
"module": "magenta.ts",
"type": "module",
"devDependencies": {
"@types/bun": "^1.1.14",
"typescript": "5.6.3"
},
"dependencies": {
"@anthropic-ai/sdk": "^0.33.1",
"bunvim": "^1.1.13",
"ignore": "^7.0.0"
},
"scripts": {
"start": "bun run src/index.ts"
}
}
15 changes: 7 additions & 8 deletions rplugin/node/magenta/src/anthropic.ts → bun/src/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import Anthropic from "@anthropic-ai/sdk";
import { context } from "./context.ts";
import {
TOOL_SPECS,
ToolRequest,
type ToolRequest,
validateToolRequest,
} from "./tools/toolManager.ts";
import { Result } from "./utils/result.ts";
import { Message } from "@anthropic-ai/sdk/src/resources/messages.js";
import { type Result } from "./utils/result.ts";

export type StopReason = Message["stop_reason"];
export type StopReason = Anthropic.Message["stop_reason"];

class AnthropicClient {
private client: Anthropic;
Expand Down Expand Up @@ -71,12 +70,12 @@ Be concise. You can use multiple tools at once, so try to minimize round trips.`
})
.on("error", onError)
.on("inputJson", (_delta, snapshot) => {
context.logger.debug(
context.nvim.logger?.debug(
`anthropic stream inputJson: ${JSON.stringify(snapshot)}`,
);
});

const response = await stream.finalMessage();
const response: Anthropic.Message = await stream.finalMessage();

if (response.stop_reason === "max_tokens") {
onError(new Error("Response exceeded max_tokens limit"));
Expand All @@ -85,8 +84,8 @@ Be concise. You can use multiple tools at once, so try to minimize round trips.`
const toolRequests = response.content
.filter((c): c is ToolRequest => c.type == "tool_use")
.map((c) => validateToolRequest(c));
context.logger.trace("toolRequests: " + JSON.stringify(toolRequests));
context.logger.trace("stopReason: " + response.stop_reason);
context.nvim.logger?.debug("toolRequests: " + JSON.stringify(toolRequests));
context.nvim.logger?.debug("stopReason: " + response.stop_reason);
return { toolRequests, stopReason: response.stop_reason };
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import type { NeovimClient, Buffer } from "neovim";
import { extractMountTree, NeovimTestHelper } from "../../test/preamble.ts";
import * as Chat from "./chat.ts";
import * as assert from "assert";
import { ToolRequestId } from "../tools/toolManager.ts";
import { type ToolRequestId } from "../tools/toolManager.ts";
import { createApp } from "../tea/tea.ts";
import { test, describe, it } from "node:test";
import { pos } from "../tea/view.ts";
import { NvimBuffer } from "../nvim/buffer.ts";

describe("tea/chat.spec.ts", () => {
let helper: NeovimTestHelper;
let nvim: NeovimClient;
let buffer: Buffer;
let buffer: NvimBuffer;

test.before(() => {
helper = new NeovimTestHelper();
});

test.beforeEach(async () => {
nvim = await helper.startNvim();
buffer = (await nvim.createBuffer(false, true)) as Buffer;
await helper.startNvim();
buffer = await NvimBuffer.create(false, true);
await buffer.setOption("modifiable", false);
});

Expand All @@ -46,9 +45,7 @@ describe("tea/chat.spec.ts", () => {
await mountedApp.waitForRender();

assert.equal(
(
await buffer.getLines({ start: 0, end: -1, strictIndexing: false })
).join("\n"),
(await buffer.getLines({ start: 0, end: -1 })).join("\n"),
``,
"initial render of chat works",
);
Expand Down Expand Up @@ -80,7 +77,7 @@ describe("tea/chat.spec.ts", () => {
await mountedApp.waitForRender();

assert.deepStrictEqual(
await buffer.getLines({ start: 0, end: -1, strictIndexing: false }),
await buffer.getLines({ start: 0, end: -1 }),
[
"### user:",
"Can you look at my list of buffers?",
Expand Down Expand Up @@ -483,7 +480,7 @@ describe("tea/chat.spec.ts", () => {
await mountedApp.waitForRender();

assert.deepStrictEqual(
await buffer.getLines({ start: 0, end: -1, strictIndexing: false }),
await buffer.getLines({ start: 0, end: -1 }),
[
"### user:",
"Can you look at my list of buffers?",
Expand Down
18 changes: 9 additions & 9 deletions rplugin/node/magenta/src/chat/chat.ts → bun/src/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import Anthropic from "@anthropic-ai/sdk";
import { toMessageParam } from "./part.ts";
import * as Message from "./message.ts";
import {
Dispatch,
type Dispatch,
parallelThunks,
Thunk,
Update,
type Thunk,
type Update,
wrapThunk,
} from "../tea/tea.ts";
import { d, View } from "../tea/view.ts";
import { d, type View } from "../tea/view.ts";
import { context } from "../context.ts";
import * as ToolManager from "../tools/toolManager.ts";
import { getClient, StopReason } from "../anthropic.ts";
import { Result } from "../utils/result.ts";
import { getClient, type StopReason } from "../anthropic.ts";
import { type Result } from "../utils/result.ts";

export type Role = "user" | "assistant";

Expand Down Expand Up @@ -129,7 +129,7 @@ export const update: Update<Msg, Model> = (msg, model) => {
if (lastMessage && lastMessage.role == "user") {
return [model, sendMessage(model)];
} else {
context.logger.error(
context.nvim.logger?.error(
`Cannot send when the last message has role ${lastMessage && lastMessage.role}`,
);
return [model];
Expand Down Expand Up @@ -367,14 +367,14 @@ function sendMessage(model: Model): Thunk<Msg> {
res = await getClient().sendMessage(
messages,
(text) => {
context.logger.trace(`stream received text ${text}`);
context.nvim.logger?.debug(`stream received text ${text}`);
dispatch({
type: "stream-response",
text,
});
},
(error) => {
context.logger.trace(`stream received error ${error}`);
context.nvim.logger?.debug(`stream received error ${error}`);
dispatch({
type: "stream-error",
error,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as Part from "./part.ts";
import * as ToolManager from "../tools/toolManager.ts";
import { Role } from "./chat.ts";
import { Dispatch, Thunk } from "../tea/tea.ts";
import { type Role } from "./chat.ts";
import { type Dispatch, type Thunk } from "../tea/tea.ts";
import { assertUnreachable } from "../utils/assertUnreachable.ts";
import { d, View, withBindings } from "../tea/view.ts";
import { d, type View, withBindings } from "../tea/view.ts";
import { displayDiffs } from "../tools/diff.ts";
import { context } from "../context.ts";

Expand Down Expand Up @@ -179,7 +179,7 @@ export const update = (
},
);
} catch (error) {
context.logger.error(error as Error);
context.nvim.logger?.error(error as Error);
dispatch({
type: "diff-error",
filePath: msg.filePath,
Expand Down
Loading

0 comments on commit 8c28399

Please sign in to comment.