Skip to content

Commit

Permalink
First extension test
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSouther committed May 16, 2024
1 parent c489ce4 commit a6af672
Show file tree
Hide file tree
Showing 15 changed files with 694 additions and 132 deletions.
40 changes: 38 additions & 2 deletions .github/workflows/extension.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
name: Package Extension
name: Extension

on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:

jobs:
npm-test:
extension-test:
name: Test Extension
strategy:
matrix:
os:
# - macos-latest,
- ubuntu-latest
# - windows-latest

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: "20.x"

- name: Install dependencies
run: npm install && npx tsc -p core && npm link ./core -w extension

- name: Build all workspaces
run: npm run build

- name: Run CI script in extension workspace (Linux)
run: xvfb-run -a npm run --workspace extension ci
if: runner.os == 'Linux'

- name: Run CI script in extension workspace (Windows or macOS)
run: npm run --workspace extension ci
if: runner.os != 'Linux'

package-extension:
name: Package Extension
needs: [extension-test]
runs-on: ubuntu-latest

steps:
Expand Down
2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"build": "tsc",
"prebuild": "npm link ../core",
"preci": "npm run build",
"test": "vitest",
"test": "vitest src",
"ci": "vitest --run --reporter=verbose src"
},
"dependencies": {
Expand Down
12 changes: 2 additions & 10 deletions core/src/actions/generate_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
PromptThread,
PromptThreadSummary,
PromptThreadsSummary,
drain,
} from "./prompt_thread.js";

export class GenerateManager {
Expand Down Expand Up @@ -79,16 +80,7 @@ export class GenerateManager {
}

drainAll() {
this.threads.forEach((thread) =>
thread.forEach(async (c) => {
const stream = await c.responseStream.promise;
if (!stream.locked) {
for await (const _ of stream) {
// Drain c.responseStream
}
}
})
);
this.threads.forEach((thread) => thread.forEach(drain));
}

async allSettled(): Promise<PromiseSettledResult<Content>[]> {
Expand Down
3 changes: 3 additions & 0 deletions extension/.vscode-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { defineConfig } = require('@vscode/test-cli');

module.exports = defineConfig({ files: 'lib/**/*.test.js' });
4 changes: 3 additions & 1 deletion extension/.vscodeignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.vscode/**
.vscode-test/**
src/**
testing/**
.gitignore
.vscode-test.js
out/**/*.test.js
Expand All @@ -9,4 +10,5 @@ tsconfig.tsbuildinfo
**/.eslintrc.json
**/*.map
**/*.ts
../**
../**
*.vsix
13 changes: 8 additions & 5 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"displayName": "Ailly",
"description": "AI Writer Ally",
"author": "David Souther <davidsouther@gmail.com>",
"publisher": "davidsouther",
"contributors": [],
"repository": "http://github.com/davidsouther/ailly",
"version": "0.1.1",
Expand All @@ -16,7 +17,7 @@
"Other"
],
"activationEvents": [],
"main": "./out/main.js",
"main": "./out/extension.js",
"contributes": {
"commands": [
{
Expand Down Expand Up @@ -82,16 +83,17 @@
},
"scripts": {
"vscode:prepublish": "npm run esbuild-base -- --minify",
"esbuild-base": "esbuild ./src/extension.ts --bundle --outfile=out/main.js --external:vscode --format=cjs --platform=node",
"esbuild-base": "esbuild ./lib/extension.js --bundle --outfile=out/extension.js --external:vscode --format=cjs --platform=node",
"esbuild": "npm run esbuild-base -- --sourcemap",
"preesbuild": "npm run compile",
"esbuild-watch": "npm run esbuild-base -- --sourcemap --watch",
"test-compile": "tsc -p ./",
"compile": "tsc -p ./",
"build": "npm run esbuild",
"watch": "tsc -watch -p ./",
"pretest": "npm run compile && npm run lint",
"lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js",
"test": "vscode-test",
"ci": "npm run test",
"prepackage": "npm run vscode:prepublish",
"package": "vsce package"
},
Expand All @@ -101,7 +103,8 @@
"@types/vscode": "^1.83.0",
"@typescript-eslint/eslint-plugin": "^6.7.3",
"@typescript-eslint/parser": "^6.7.3",
"@vscode/test-electron": "^2.3.4",
"@vscode/test-cli": "^0.0.9",
"@vscode/test-electron": "^2.3.10",
"@vscode/vsce": "^2.21.1",
"esbuild": "^0.19.4",
"eslint": "^8.50.0",
Expand Down
10 changes: 7 additions & 3 deletions extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as vscode from "vscode";
import { basename } from "path";
import { generate } from "./generate.js";
import { LOGGER, resetLogger } from "./settings";
import { LOGGER, resetLogger } from "./settings.js";

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
Expand Down Expand Up @@ -57,7 +57,9 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand(
"ailly.edit",
async (uri?: vscode.Uri, ..._args) => {
if (!vscode.window.activeTextEditor) return;
if (!vscode.window.activeTextEditor) {
return;
}
try {
let path: string;
if (uri) {
Expand All @@ -74,7 +76,9 @@ export function activate(context: vscode.ExtensionContext) {
prompt: "What edits should Ailly make?",
});

if (!prompt) return;
if (!prompt) {
return;
}

const start = vscode.window.activeTextEditor.selection.start.line;
const end = vscode.window.activeTextEditor.selection.end.line;
Expand Down
40 changes: 40 additions & 0 deletions extension/src/generate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as assert from "assert";
import { afterEach } from "mocha";
import { mock } from "node:test";
import { resolve } from "path";
import * as vscode from "vscode";
import { generate } from "./generate.js";
import { SETTINGS } from "./settings.js";
const { assertExists } = require("@davidsouther/jiffies/lib/cjs/assert.js");

async function activate(docUri: vscode.Uri) {
const ext = vscode.extensions.getExtension("davidsouther.ailly");
await ext?.activate();
const doc = await vscode.workspace.openTextDocument(docUri);
const editor = await vscode.window.showTextDocument(doc);
return { doc, editor };
}

process.env["AILLY_ENGINE"] = "noop";
process.env["AILLY_NOOP_RESPONSE"] = "Edited";
process.env["AILLY_NOOP_TIMEOUT"] = "0";
process.env["AILLY_NOOP_STREAM"] = "";

suite("Ailly Extension Generate", () => {
afterEach(() =>
vscode.commands.executeCommand("workbench.action.closeActiveEditor")
);

test("generate edit", async () => {
const path = resolve(__dirname, "..", "testing", "edit.txt");
const docUri = vscode.Uri.file(path);
await activate(docUri);
mock.method(SETTINGS, "getAillyEngine", () => "noop");

await generate(path, { prompt: "Replace with Edited", start: 1, end: 3 });
// await new Promise((r) => setTimeout(r, 2000));

const activeWindow = assertExists(vscode.window.activeTextEditor);
assert.equal(activeWindow.document.getText(), "Line 1\nEdited\nLine 4");
});
});
35 changes: 18 additions & 17 deletions extension/src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import vscode from "vscode";

import { FileSystem } from "@davidsouther/jiffies/lib/cjs/fs.js";
import { VSCodeFileSystemAdapter } from "./fs.js";
import { LOGGER, getAillyEngine, getAillyModel, resetLogger } from "./settings";
import { dirname } from "node:path";
import { prepareBedrock } from "./settings.js";
import { GenerateManager } from "@ailly/core/lib/actions/generate_manager.js";
import {
AillyEdit,
loadContent,
writeContent,
} from "@ailly/core/src/content/content";
import { makePipelineSettings } from "@ailly/core/src/index";
import { GenerateManager } from "@ailly/core/src/actions/generate_manager";
import { withResolvers } from "@ailly/core/lib/util";
} from "@ailly/core/lib/content/content.js";
import { makePipelineSettings } from "@ailly/core/lib/index.js";
import { withResolvers } from "@ailly/core/lib/util.js";
import { FileSystem } from "@davidsouther/jiffies/lib/cjs/fs.js";
import { dirname } from "node:path";
import { VSCodeFileSystemAdapter } from "./fs.js";
import { LOGGER, SETTINGS, resetLogger } from "./settings.js";

export async function generate(
path: string,
Expand All @@ -25,10 +24,10 @@ export async function generate(
const root = dirname(path);
fs.cd(root);

const engine = await getAillyEngine();
const model = await getAillyModel(engine);
if (engine == "bedrock") {
await prepareBedrock();
const engine = await SETTINGS.getAillyEngine();
const model = await SETTINGS.getAillyModel(engine);
if (engine === "bedrock") {
await SETTINGS.prepareBedrock();
}

const settings = await makePipelineSettings({
Expand All @@ -42,7 +41,9 @@ export async function generate(
// Load content
const context = await loadContent(fs, [], {}, 1);
const content = Object.values(context).filter((c) => c.path.startsWith(path));
if (content.length == 0) return;
if (content.length === 0) {
return;
}
if (edit) {
const editContext: AillyEdit =
edit.start === edit.end
Expand Down Expand Up @@ -78,17 +79,17 @@ export async function generate(
generator.start();
await generator.allSettled();

if (content[0].meta?.debug?.finish! == "failed") {
if (content[0].meta?.debug?.finish! === "failed") {
throw new Error(content[0].meta?.debug?.error?.message ?? "unknown");
}

// Write
if (edit && content[0].context.edit) {
vscode.window.activeTextEditor?.edit((builder) => {
await vscode.window.activeTextEditor?.edit((builder) => {
builder.replace(
new vscode.Range(
new vscode.Position(edit.start, 0),
new vscode.Position(edit.end + 1, 0)
new vscode.Position(edit.end, 0)
),
(content[0].response ?? "") + "\n"
);
Expand Down
Loading

0 comments on commit a6af672

Please sign in to comment.