From 7bbc14b73b4947901319a2ce448c3c2aebb6b043 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 5 Sep 2024 11:21:17 -0700 Subject: [PATCH 1/9] dedent utility --- core/util/dedent.test.ts | 128 +++++++++++++++++++++++++++++++++++++++ core/util/index.ts | 42 +++++++++++-- 2 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 core/util/dedent.test.ts diff --git a/core/util/dedent.test.ts b/core/util/dedent.test.ts new file mode 100644 index 000000000..130b6486b --- /dev/null +++ b/core/util/dedent.test.ts @@ -0,0 +1,128 @@ +import { dedent } from "./index"; + +describe("dedent function", () => { + it("should remove common leading whitespace from all lines", () => { + const result = dedent` + Hello + World + ! + `; + expect(result).toBe("Hello\n World\n !"); + }); + + it("should handle strings with no indentation", () => { + const result = dedent`Hello +World +!`; + expect(result).toBe("Hello\nWorld\n!"); + }); + + it("should handle strings with mixed indentation", () => { + const result = dedent` + Hello + World + ! + `; + expect(result).toBe(" Hello\nWorld\n !"); + }); + + it("should remove leading and trailing empty lines", () => { + const result = dedent` + + Hello + World + + `; + expect(result).toBe("Hello\nWorld"); + }); + + it("should handle empty strings", () => { + const result = dedent``; + expect(result).toBe(""); + }); + + it("should handle strings with only whitespace", () => { + const result = dedent` + + `; + expect(result).toBe(""); + }); + + it.skip("should handle strings with tabs", () => { + const result = dedent` + \tHello + \t\tWorld + \t\t\t! +`; + expect(result).toBe("\tHello\n\t\tWorld\n\t\t\t!"); + }); + + it("should handle interpolated values", () => { + const world = "World"; + const result = dedent` + Hello ${world} + How are you? + `; + expect(result).toBe("Hello World\n How are you?"); + }); + + it("should handle multiple interpolated values", () => { + const greeting = "Hello"; + const name = "Alice"; + const question = "How are you?"; + const result = dedent` + ${greeting} ${name} + ${question} + `; + expect(result).toBe("Hello Alice\n How are you?"); + }); + + it("should handle interpolated values with different indentation", () => { + const value1 = "foo"; + const value2 = "bar"; + const result = dedent` + ${value1} + ${value2} + `; + expect(result).toBe("foo\n bar"); + }); + + it("should handle a single line with indentation", () => { + const result = dedent` Hello World!`; + expect(result).toBe("Hello World!"); + }); + + it("should handle a string with only one non-empty line", () => { + const result = dedent` + + Hello World! + + `; + expect(result).toBe("Hello World!"); + }); + + it("should handle a string with Unicode characters", () => { + const result = dedent` + こんにちは + 世界 + `; + expect(result).toBe("こんにちは\n 世界"); + }); + + it("should handle a string with emoji", () => { + const result = dedent` + 🌍 + 🌎 + 🌏 + `; + expect(result).toBe("🌍\n 🌎\n 🌏"); + }); + + it.skip("should handle a string with CRLF line endings", () => { + const result = dedent` + Hello\r + World\r + `; + expect(result).toBe("Hello\r\n World"); + }); +}); diff --git a/core/util/index.ts b/core/util/index.ts index a628948c7..583eac5d2 100644 --- a/core/util/index.ts +++ b/core/util/index.ts @@ -2,19 +2,19 @@ export function removeQuotesAndEscapes(output: string): string { output = output.trim(); // Replace smart quotes - output = output.replace("“", "\""); - output = output.replace("”", "\""); + output = output.replace("“", '"'); + output = output.replace("”", '"'); output = output.replace("‘", "'"); output = output.replace("’", "'"); // Remove escapes - output = output.replace("\\\"", "\""); + output = output.replace('\\"', '"'); output = output.replace("\\'", "'"); output = output.replace("\\n", "\n"); output = output.replace("\\t", "\t"); output = output.replace("\\\\", "\\"); while ( - (output.startsWith("\"") && output.endsWith("\"")) || + (output.startsWith('"') && output.endsWith('"')) || (output.startsWith("'") && output.endsWith("'")) ) { output = output.slice(1, -1); @@ -120,7 +120,9 @@ export function getUniqueFilePath( } export function shortestRelativePaths(paths: string[]): string[] { - if (paths.length === 0) {return [];} + if (paths.length === 0) { + return []; + } const partsLengths = paths.map((x) => x.split(SEP_REGEX).length); const currentRelativePaths = paths.map(getBasename); @@ -135,7 +137,9 @@ export function shortestRelativePaths(paths: string[]): string[] { const firstDuplicatedPath = currentRelativePaths.find( (x, i) => isDuplicated[i], ); - if (!firstDuplicatedPath) {break;} + if (!firstDuplicatedPath) { + break; + } currentRelativePaths.forEach((x, i) => { if (x === firstDuplicatedPath) { @@ -270,3 +274,29 @@ export function deduplicateArray( } export type TODO = any; + +export function dedent(strings: TemplateStringsArray, ...values: any[]) { + let result = strings.reduce( + (acc, str, i) => acc + str + (values[i] || ""), + "", + ); + result = result.replace(/^\n/, "").replace(/\n\s*$/, ""); + let lines = result.split("\n"); + + // Remove leading white-space-only lines + while (lines.length > 0 && lines[0].trim() === "") { + lines.shift(); + } + + // Remove trailing white-space-only lines + while (lines.length > 0 && lines[lines.length - 1].trim() === "") { + lines.pop(); + } + + const minIndent = lines.reduce((min, line) => { + const match = line.match(/^\s*/); + return Math.min(min, match ? match[0].length : Infinity); + }, Infinity); + + return lines.map((line) => line.slice(minIndent)).join("\n"); +} From b887382c6336632d8b51f91732a6b528611f200e Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 5 Sep 2024 11:24:44 -0700 Subject: [PATCH 2/9] fix dedent to ignore empty lines --- core/util/dedent.test.ts | 10 ++++++++++ core/util/index.ts | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/core/util/dedent.test.ts b/core/util/dedent.test.ts index 130b6486b..3b0d526e1 100644 --- a/core/util/dedent.test.ts +++ b/core/util/dedent.test.ts @@ -125,4 +125,14 @@ World `; expect(result).toBe("Hello\r\n World"); }); + + it("should not count empty lines in the minimum indentation", () => { + const result = dedent` + Hello + + World + `; + + expect(result).toBe("Hello\n\nWorld"); + }); }); diff --git a/core/util/index.ts b/core/util/index.ts index 583eac5d2..3de43957e 100644 --- a/core/util/index.ts +++ b/core/util/index.ts @@ -294,6 +294,10 @@ export function dedent(strings: TemplateStringsArray, ...values: any[]) { } const minIndent = lines.reduce((min, line) => { + if (line.trim() === "") { + // Don't consider empty lines when calculating indentation + return min; + } const match = line.match(/^\s*/); return Math.min(min, match ? match[0].length : Infinity); }, Infinity); From f89486143f157c09797789f1d4027251f729af92 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 5 Sep 2024 12:20:53 -0700 Subject: [PATCH 3/9] add test with tabs --- core/diff/streamDiff.test.ts | 80 ++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/core/diff/streamDiff.test.ts b/core/diff/streamDiff.test.ts index 72be00746..f3cc189cd 100644 --- a/core/diff/streamDiff.test.ts +++ b/core/diff/streamDiff.test.ts @@ -2,6 +2,7 @@ import { streamDiff } from "../diff/streamDiff.js"; import { DiffLine, DiffLineType } from "../index.js"; // @ts-ignore no typings available import { changed, diff as myersDiff } from "myers-diff"; +import { dedent } from "../util/index.js"; // "modification" is an extra type used to represent an "old" + "new" diff line type MyersDiffTypes = Extract | "modification"; @@ -23,6 +24,10 @@ async function collectDiffs( } const myersDiffs = myersDiff(oldLines.join("\n"), newLines.join("\n")); + // const myersDiffs: DiffLine[] = myersDiffsRaw.map((diff) => ({ + // type: getMyersDiffType(diff), + // line: diff.rhs, + // })); return { streamDiffs, myersDiffs }; } @@ -43,6 +48,17 @@ function getMyersDiffType(diff: any): MyersDiffTypes | undefined { return undefined; } +function displayDiff(diff: DiffLine[]) { + const symbol = { + same: " ", + new: "+", + old: "-", + }; + console.log( + diff.map(({ type, line }) => `${symbol[type]} ${line}`).join("\n"), + ); +} + // We use a longer `)` string here to not get // caught by the fuzzy matcher describe("streamDiff(", () => { @@ -177,4 +193,68 @@ describe("streamDiff(", () => { expect(myersDiffs[0].lhs.del).toEqual(2); expect(getMyersDiffType(myersDiffs[0])).toBe("old"); }); + + test.only("tabs vs. spaces differences are ignored", async () => { + const oldLines = dedent` +\tfrom fastapi import FastAPI + +\tapp = FastAPI() + +\t@app.get("/") +\tdef read_root(): +\t\treturn {"Hello": "World"} + +\t@app.get("/landing") +\tdef read_landing_page(): +\t\treturn {"message": "Welcome to the landing page"} + `.split("\n"); + const newLines = dedent` + from fastapi import FastAPI, HTTPException + + app = FastAPI() + + @app.get("/") + def read_root(): + return {"Hello": "World"} + + @app.get("/landing") + def read_landing_page(): + raise HTTPException(status_code=404, detail="Page not found") + `.split("\n"); + + const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines); + displayDiff(streamDiffs); + }); + + test("FastAPI example", async () => { + const oldLines = dedent` + from fastapi import FastAPI + + app = FastAPI() + + @app.get("/") + def read_root(): + return {"Hello": "World"} + + @app.get("/landing") + def read_landing_page(): + return {"message": "Welcome to the landing page"} + `.split("\n"); + const newLines = dedent` + from fastapi import FastAPI, HTTPException + + app = FastAPI() + + @app.get("/") + def read_root(): + return {"Hello": "World"} + + @app.get("/landing") + def read_landing_page(): + raise HTTPException(status_code=404, detail="Page not found") + `.split("\n"); + + const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines); + displayDiff(streamDiffs); + }); }); From d1a15ff345587da970b66a86849310d46a59fb05 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 5 Sep 2024 16:53:50 -0700 Subject: [PATCH 4/9] tiny diff test framework --- archive/python.yaml | 44 --------- archive/rust.yaml | 65 ------------ core/diff/streamDiff.test.ts | 99 +++++++------------ core/diff/test-examples/README.md | 23 +++++ .../test-examples/fastapi-tabs-vs-spaces.diff | 46 +++++++++ core/diff/test-examples/fastapi.diff | 41 ++++++++ docs/static/schemas/config.json | 47 ++++++++- extensions/vscode/continue_rc_schema.json | 51 +++++++++- 8 files changed, 239 insertions(+), 177 deletions(-) delete mode 100644 archive/python.yaml delete mode 100644 archive/rust.yaml create mode 100644 core/diff/test-examples/README.md create mode 100644 core/diff/test-examples/fastapi-tabs-vs-spaces.diff create mode 100644 core/diff/test-examples/fastapi.diff diff --git a/archive/python.yaml b/archive/python.yaml deleted file mode 100644 index db4edf41f..000000000 --- a/archive/python.yaml +++ /dev/null @@ -1,44 +0,0 @@ -on: - -defaults: - run: - working-directory: ./server - -jobs: - lint: - runs-on: ubuntu-latest - name: ${{ matrix.name }} - strategy: - fail-fast: false - matrix: - include: - - name: ruff - command: ruff check --output-format=github . - deps: --only lint-ruff - - name: mypy - command: mypy - deps: --with lint-typing - steps: - - uses: actions/checkout@v4 - - name: Install Python - uses: actions/setup-python@v4 - with: - python-version: "3.11" - - uses: snok/install-poetry@v1 - - name: Install dependencies - run: poetry install ${{ matrix.deps }} - - run: poetry run ${{ matrix.command }} - - # test: - # runs-on: ubuntu-latest - # steps: - # - uses: actions/checkout@v4 - # - name: Install Python - # uses: actions/setup-python@v4 - # with: - # python-version: "3.12" - # - uses: snok/install-poetry@v1 - # - run: poetry run python -m pip install --upgrade pip setuptools - # - name: Install dependencies - # run: poetry install - # - run: poetry run pytest diff --git a/archive/rust.yaml b/archive/rust.yaml deleted file mode 100644 index 6bbb23423..000000000 --- a/archive/rust.yaml +++ /dev/null @@ -1,65 +0,0 @@ -on: - -name: Rust Continuous integration - -defaults: - run: - working-directory: ./server - -jobs: - # test: - # name: test - # runs-on: ${{ matrix.os }} - # strategy: - # matrix: - # os: [ubuntu-latest, macos-latest, windows-latest] - # toolchain: [stable] - # include: - # - os: ubuntu-latest - # toolchain: nightly - # steps: - # - uses: actions/checkout@v4 - # - uses: dtolnay/rust-toolchain@master - # with: - # toolchain: ${{ matrix.toolchain }} - # - run: cargo test --all-features - - fmt: - name: format - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@nightly - with: - components: rustfmt - - run: cargo fmt --all --check - - clippy: - name: lint - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@master - with: - toolchain: nightly-2023-12-10 - components: clippy - - uses: actions-rs-plus/clippy-check@v2 - with: - working-directory: server/ - args: --all-features --all-targets - - docs: - name: docs - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - - run: cargo doc --no-deps - - cargo-deny: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: EmbarkStudios/cargo-deny-action@v1 - with: - arguments: --manifest-path server/Cargo.toml diff --git a/core/diff/streamDiff.test.ts b/core/diff/streamDiff.test.ts index f3cc189cd..b86b4f8b5 100644 --- a/core/diff/streamDiff.test.ts +++ b/core/diff/streamDiff.test.ts @@ -1,8 +1,9 @@ +import fs from "node:fs"; import { streamDiff } from "../diff/streamDiff.js"; import { DiffLine, DiffLineType } from "../index.js"; // @ts-ignore no typings available import { changed, diff as myersDiff } from "myers-diff"; -import { dedent } from "../util/index.js"; +import path from "node:path"; // "modification" is an extra type used to represent an "old" + "new" diff line type MyersDiffTypes = Extract | "modification"; @@ -54,9 +55,39 @@ function displayDiff(diff: DiffLine[]) { new: "+", old: "-", }; - console.log( - diff.map(({ type, line }) => `${symbol[type]} ${line}`).join("\n"), + + return diff.map(({ type, line }) => `${symbol[type]} ${line}`).join("\n"); +} + +async function expectDiff(file: string) { + const testFilePath = path.join( + __dirname, + "diff", + "test-examples", + file + ".diff", ); + const testFileContents = fs.readFileSync(testFilePath, "utf-8"); + const [oldText, newText, expectedDiff] = testFileContents + .split("\n---\n") + .map((s) => s.trim()); + const oldLines = oldText.split("\n"); + const newLines = newText.split("\n"); + const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines); + const displayedDiff = displayDiff(streamDiffs); + + if (expectedDiff.trim() === "") { + console.log( + "Expected diff was empty. Writing computed diff to the test file", + ); + fs.writeFileSync( + testFilePath, + `${oldText}\n\n---\n\n${newText}\n\n---\n\n${displayedDiff}`, + ); + + throw new Error("Expected diff is empty"); + } + + expect(displayedDiff).toEqual(expectedDiff); } // We use a longer `)` string here to not get @@ -194,67 +225,11 @@ describe("streamDiff(", () => { expect(getMyersDiffType(myersDiffs[0])).toBe("old"); }); - test.only("tabs vs. spaces differences are ignored", async () => { - const oldLines = dedent` -\tfrom fastapi import FastAPI - -\tapp = FastAPI() - -\t@app.get("/") -\tdef read_root(): -\t\treturn {"Hello": "World"} - -\t@app.get("/landing") -\tdef read_landing_page(): -\t\treturn {"message": "Welcome to the landing page"} - `.split("\n"); - const newLines = dedent` - from fastapi import FastAPI, HTTPException - - app = FastAPI() - - @app.get("/") - def read_root(): - return {"Hello": "World"} - - @app.get("/landing") - def read_landing_page(): - raise HTTPException(status_code=404, detail="Page not found") - `.split("\n"); - - const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines); - displayDiff(streamDiffs); + test("tabs vs. spaces differences are ignored", async () => { + await expectDiff("fastapi-tabs-vs-spaces"); }); test("FastAPI example", async () => { - const oldLines = dedent` - from fastapi import FastAPI - - app = FastAPI() - - @app.get("/") - def read_root(): - return {"Hello": "World"} - - @app.get("/landing") - def read_landing_page(): - return {"message": "Welcome to the landing page"} - `.split("\n"); - const newLines = dedent` - from fastapi import FastAPI, HTTPException - - app = FastAPI() - - @app.get("/") - def read_root(): - return {"Hello": "World"} - - @app.get("/landing") - def read_landing_page(): - raise HTTPException(status_code=404, detail="Page not found") - `.split("\n"); - - const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines); - displayDiff(streamDiffs); + await expectDiff("fastapi"); }); }); diff --git a/core/diff/test-examples/README.md b/core/diff/test-examples/README.md new file mode 100644 index 000000000..b9b95421a --- /dev/null +++ b/core/diff/test-examples/README.md @@ -0,0 +1,23 @@ +# Diff algorithm tests + +Tests are specified as + +``` + + +--- + + + +--- + + +``` + +`---` is the delimeter, and surrounding whitespace will be trimmed. + +The expected diff can be generated with the `displayDiff` function. + +We make this explicit instead of comparing to the output of `myersDiff` in case the output from that is either unattainable or not exactly what we want. + +In order to generate the expected diff, you can first leave it empty and then run the test. The test will catch this and write the _computed_ diff to the test file. It is up to you to correct this to the expected diff. diff --git a/core/diff/test-examples/fastapi-tabs-vs-spaces.diff b/core/diff/test-examples/fastapi-tabs-vs-spaces.diff new file mode 100644 index 000000000..84bea74c7 --- /dev/null +++ b/core/diff/test-examples/fastapi-tabs-vs-spaces.diff @@ -0,0 +1,46 @@ +from fastapi import FastAPI + +app = FastAPI() + +@app.get("/") +def read_root(): +\treturn {"Hello": "World"} + +@app.get("/landing") +def read_landing_page(): +\treturn {"message": "Welcome to the landing page"} + +--- + +from fastapi import FastAPI, HTTPException + +app = FastAPI() + +@app.get("/") +def read_root(): + return {"Hello": "World"} + +@app.get("/landing") +def read_landing_page(): + raise HTTPException(status_code=404, detail="Page not found") + +--- + +- from fastapi import FastAPI ++ from fastapi import FastAPI, HTTPException + + app = FastAPI() + + @app.get("/") + def read_root(): +- \treturn {"Hello": "World"} ++ return {"Hello": "World"} + + @app.get("/landing") + def read_landing_page(): ++ raise HTTPException(status_code=404, detail="Page not found") +- \treturn {"message": "Welcome to the landing page"} + +--- + +Make sure we correctly ignore tabs-vs-spaces changes \ No newline at end of file diff --git a/core/diff/test-examples/fastapi.diff b/core/diff/test-examples/fastapi.diff new file mode 100644 index 000000000..9c3b6d1f7 --- /dev/null +++ b/core/diff/test-examples/fastapi.diff @@ -0,0 +1,41 @@ +from fastapi import FastAPI + +app = FastAPI() + +@app.get("/") +def read_root(): + return {"Hello": "World"} + +@app.get("/landing") +def read_landing_page(): + return {"message": "Welcome to the landing page"} + +--- + +from fastapi import FastAPI, HTTPException + +app = FastAPI() + +@app.get("/") +def read_root(): + return {"Hello": "World"} + +@app.get("/landing") +def read_landing_page(): + raise HTTPException(status_code=404, detail="Page not found") + +--- + +- from fastapi import FastAPI ++ from fastapi import FastAPI, HTTPException + + app = FastAPI() + + @app.get("/") + def read_root(): + return {"Hello": "World"} + + @app.get("/landing") + def read_landing_page(): ++ raise HTTPException(status_code=404, detail="Page not found") +- return {"message": "Welcome to the landing page"} \ No newline at end of file diff --git a/docs/static/schemas/config.json b/docs/static/schemas/config.json index 7166520e1..108714449 100644 --- a/docs/static/schemas/config.json +++ b/docs/static/schemas/config.json @@ -200,8 +200,8 @@ "### Free Trial\nNew users can try out Continue for free using a proxy server that securely makes calls to OpenAI using our API key. If you are ready to use your own API key or have used all 250 free uses, you can enter your API key in config.json where it says `apiKey=\"\"` or select another model provider.\n> [Reference](https://docs.continue.dev/reference/Model%20Providers/freetrial)", "### Anthropic\nTo get started with Anthropic models, you first need to sign up for the open beta [here](https://claude.ai/login) to obtain an API key.\n> [Reference](https://docs.continue.dev/reference/Model%20Providers/anthropicllm)", "### Cohere\nTo use Cohere, visit the [Cohere dashboard](https://dashboard.cohere.com/api-keys) to create an API key.\n\n> [Reference](https://docs.continue.dev/reference/Model%20Providers/cohere)", - "### Bedrock\nTo get started with Bedrock you need to sign up on AWS [here](https://aws.amazon.com/bedrock/claude/)", - "### Bedrock Imported Models\nTo get started with Bedrock you need to sign up on AWS [here](https://aws.amazon.com/bedrock/claude/)", + "### Bedrock\nTo get started with Bedrock you need to sign up on AWS [here](https://aws.amazon.com/bedrock)", + "### Bedrock Imported Models\nTo get started with Bedrock you need to sign up on AWS [here](https://aws.amazon.com/bedrock)", "### Sagemaker\nSagemaker is AWS' machine learning platform.", "### Together\nTogether is a hosted service that provides extremely fast streaming of open-source language models. To get started with Together:\n1. Obtain an API key from [here](https://together.ai)\n2. Paste below\n3. Select a model preset\n> [Reference](https://docs.continue.dev/reference/Model%20Providers/togetherllm)", "### Ollama\nTo get started with Ollama, follow these steps:\n1. Download from [ollama.ai](https://ollama.ai/) and open the application\n2. Open a terminal and run `ollama run `. Example model names are `codellama:7b-instruct` or `llama2:7b-text`. You can find the full list [here](https://ollama.ai/library).\n3. Make sure that the model name used in step 2 is the same as the one in config.json (e.g. `model=\"codellama:7b-instruct\"`)\n4. Once the model has finished downloading, you can start asking questions through Continue.\n> [Reference](https://docs.continue.dev/reference/Model%20Providers/ollama)", @@ -242,6 +242,21 @@ "description": "The base URL of the LLM API.", "type": "string" }, + "region": { + "title": "Region", + "description": "The region where the model is hosted", + "type": "string" + }, + "profile": { + "title": "Profile", + "description": "The AWS security profile to use", + "type": "string" + }, + "modelArn": { + "title": "Profile", + "description": "The AWS arn for the imported model", + "type": "string" + }, "contextLength": { "title": "Context Length", "description": "The maximum context length of the LLM in tokens, as counted by countTokens.", @@ -393,6 +408,21 @@ "required": ["apiKey"] } }, + { + "if": { + "properties": { + "provider": { + "enum": [ + "bedrockimport" + ] + } + }, + "required": ["provider"] + }, + "then": { + "required": ["modelArn"] + } + }, { "if": { "properties": { @@ -2030,7 +2060,8 @@ "cohere", "free-trial", "gemini", - "voyage" + "voyage", + "bedrock" ] }, "model": { @@ -2053,6 +2084,16 @@ "type": "integer", "minimum": 128, "exclusiveMaximum": 2147483647 + }, + "region": { + "title": "Region", + "description": "The region where the model is hosted", + "type": "string" + }, + "profile": { + "title": "Profile", + "description": "The AWS security profile to use", + "type": "string" } }, "required": ["provider"], diff --git a/extensions/vscode/continue_rc_schema.json b/extensions/vscode/continue_rc_schema.json index 70c584b9e..03d2b147b 100644 --- a/extensions/vscode/continue_rc_schema.json +++ b/extensions/vscode/continue_rc_schema.json @@ -203,8 +203,8 @@ "### Free Trial\nNew users can try out Continue for free using a proxy server that securely makes calls to OpenAI using our API key. If you are ready to use your own API key or have used all 250 free uses, you can enter your API key in config.json where it says `apiKey=\"\"` or select another model provider.\n> [Reference](https://docs.continue.dev/reference/Model%20Providers/freetrial)", "### Anthropic\nTo get started with Anthropic models, you first need to sign up for the open beta [here](https://claude.ai/login) to obtain an API key.\n> [Reference](https://docs.continue.dev/reference/Model%20Providers/anthropicllm)", "### Cohere\nTo use Cohere, visit the [Cohere dashboard](https://dashboard.cohere.com/api-keys) to create an API key.\n\n> [Reference](https://docs.continue.dev/reference/Model%20Providers/cohere)", - "### Bedrock\nTo get started with Bedrock you need to sign up on AWS [here](https://aws.amazon.com/bedrock/claude/)", - "### Bedrock Imported Models\nTo get started with Bedrock you need to sign up on AWS [here](https://aws.amazon.com/bedrock/claude/)", + "### Bedrock\nTo get started with Bedrock you need to sign up on AWS [here](https://aws.amazon.com/bedrock)", + "### Bedrock Imported Models\nTo get started with Bedrock you need to sign up on AWS [here](https://aws.amazon.com/bedrock)", "### Sagemaker\nSagemaker is AWS' machine learning platform.", "### Together\nTogether is a hosted service that provides extremely fast streaming of open-source language models. To get started with Together:\n1. Obtain an API key from [here](https://together.ai)\n2. Paste below\n3. Select a model preset\n> [Reference](https://docs.continue.dev/reference/Model%20Providers/togetherllm)", "### Ollama\nTo get started with Ollama, follow these steps:\n1. Download from [ollama.ai](https://ollama.ai/) and open the application\n2. Open a terminal and run `ollama run `. Example model names are `codellama:7b-instruct` or `llama2:7b-text`. You can find the full list [here](https://ollama.ai/library).\n3. Make sure that the model name used in step 2 is the same as the one in config.json (e.g. `model=\"codellama:7b-instruct\"`)\n4. Once the model has finished downloading, you can start asking questions through Continue.\n> [Reference](https://docs.continue.dev/reference/Model%20Providers/ollama)", @@ -245,6 +245,21 @@ "description": "The base URL of the LLM API.", "type": "string" }, + "region": { + "title": "Region", + "description": "The region where the model is hosted", + "type": "string" + }, + "profile": { + "title": "Profile", + "description": "The AWS security profile to use", + "type": "string" + }, + "modelArn": { + "title": "Profile", + "description": "The AWS arn for the imported model", + "type": "string" + }, "contextLength": { "title": "Context Length", "description": "The maximum context length of the LLM in tokens, as counted by countTokens.", @@ -409,6 +424,25 @@ ] } }, + { + "if": { + "properties": { + "provider": { + "enum": [ + "bedrockimport" + ] + } + }, + "required": [ + "provider" + ] + }, + "then": { + "required": [ + "modelArn" + ] + } + }, { "if": { "properties": { @@ -2257,7 +2291,8 @@ "cohere", "free-trial", "gemini", - "voyage" + "voyage", + "bedrock" ] }, "model": { @@ -2280,6 +2315,16 @@ "type": "integer", "minimum": 128, "exclusiveMaximum": 2147483647 + }, + "region": { + "title": "Region", + "description": "The region where the model is hosted", + "type": "string" + }, + "profile": { + "title": "Profile", + "description": "The AWS security profile to use", + "type": "string" } }, "required": [ From 22363da9e1fd3339c625fd2e3c81ecb57b7b627d Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 5 Sep 2024 17:23:29 -0700 Subject: [PATCH 5/9] another diff test --- core/diff/streamDiff.test.ts | 6 ++- core/diff/test-examples/add-comments.diff | 53 +++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 core/diff/test-examples/add-comments.diff diff --git a/core/diff/streamDiff.test.ts b/core/diff/streamDiff.test.ts index b86b4f8b5..de6bd06a4 100644 --- a/core/diff/streamDiff.test.ts +++ b/core/diff/streamDiff.test.ts @@ -75,7 +75,7 @@ async function expectDiff(file: string) { const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines); const displayedDiff = displayDiff(streamDiffs); - if (expectedDiff.trim() === "") { + if (!expectedDiff || expectedDiff.trim() === "") { console.log( "Expected diff was empty. Writing computed diff to the test file", ); @@ -232,4 +232,8 @@ describe("streamDiff(", () => { test("FastAPI example", async () => { await expectDiff("fastapi"); }); + + test("FastAPI comments", async () => { + await expectDiff("add-comments"); + }); }); diff --git a/core/diff/test-examples/add-comments.diff b/core/diff/test-examples/add-comments.diff new file mode 100644 index 000000000..c29c0c5cd --- /dev/null +++ b/core/diff/test-examples/add-comments.diff @@ -0,0 +1,53 @@ +from fastapi import FastAPI + +app = FastAPI() + +@app.get("/") +def read_root(): + return {"Hello": "World"} + +@app.get("/landing") +def read_landing_page(): + return {"message": "Welcome to the landing page"} + +--- + +# Import the FastAPI class from the fastapi module +from fastapi import FastAPI + +# Create an instance of the FastAPI application +app = FastAPI() + +# Define a route for the root endpoint ("/") +@app.get("/") +def read_root(): + # Return a simple JSON response + return {"Hello": "World"} // This is an example comment + +# Define a route for the landing page endpoint ("/landing") +@app.get("/landing") +def read_landing_page(): + # Return a JSON response with a welcome message + return {"message": "Welcome to the landing page"} + +--- + ++ # Import the FastAPI class from the fastapi module + from fastapi import FastAPI + ++ # Create an instance of the FastAPI application + app = FastAPI() + ++ # Define a route for the root endpoint ("/") + @app.get("/") + def read_root(): ++ # Return a simple JSON response ++ return {"Hello": "World"} // This is an example comment ++ ++ # Define a route for the landing page endpoint ("/landing") +- return {"Hello": "World"} +- + @app.get("/landing") + def read_landing_page(): ++ # Return a JSON response with a welcome message + return {"message": "Welcome to the landing page"} \ No newline at end of file From cc0259e6b36bf99bd67aad35feba8a6e902ca9cf Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 5 Sep 2024 17:52:12 -0700 Subject: [PATCH 6/9] fix CRLF bug in diff stream --- core/util/verticalEdit.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/util/verticalEdit.ts b/core/util/verticalEdit.ts index 0a32a6a5b..34a17ebd4 100644 --- a/core/util/verticalEdit.ts +++ b/core/util/verticalEdit.ts @@ -81,6 +81,9 @@ export async function* streamDiffLines( oldLines = []; } + // Trim end of oldLines, otherwise we have trailing \r on every line for CRLF files + oldLines = oldLines.map((line) => line.trimEnd()); + const prompt = constructPrompt( prefix, highlighted, From 6eb3f4eb6420ae9c1d7360f2bd3d2007b7f7b021 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 5 Sep 2024 18:15:16 -0700 Subject: [PATCH 7/9] update spacing to match unified diff format --- core/diff/streamDiff.test.ts | 8 +- core/diff/test-examples/add-comments.diff | 18 +-- .../test-examples/fastapi-tabs-vs-spaces.diff | 22 ++-- core/diff/test-examples/fastapi.diff | 18 +-- core/diff/test-examples/mock-llm.diff | 103 ++++++++++++++++++ 5 files changed, 136 insertions(+), 33 deletions(-) create mode 100644 core/diff/test-examples/mock-llm.diff diff --git a/core/diff/streamDiff.test.ts b/core/diff/streamDiff.test.ts index de6bd06a4..94933623c 100644 --- a/core/diff/streamDiff.test.ts +++ b/core/diff/streamDiff.test.ts @@ -51,7 +51,7 @@ function getMyersDiffType(diff: any): MyersDiffTypes | undefined { function displayDiff(diff: DiffLine[]) { const symbol = { - same: " ", + same: "", new: "+", old: "-", }; @@ -69,7 +69,7 @@ async function expectDiff(file: string) { const testFileContents = fs.readFileSync(testFilePath, "utf-8"); const [oldText, newText, expectedDiff] = testFileContents .split("\n---\n") - .map((s) => s.trim()); + .map((s) => s.replace(/^\n+/, "").trimEnd()); const oldLines = oldText.split("\n"); const newLines = newText.split("\n"); const { streamDiffs, myersDiffs } = await collectDiffs(oldLines, newLines); @@ -236,4 +236,8 @@ describe("streamDiff(", () => { test("FastAPI comments", async () => { await expectDiff("add-comments"); }); + + test.skip("Mock LLM example", async () => { + await expectDiff("mock-llm"); + }); }); diff --git a/core/diff/test-examples/add-comments.diff b/core/diff/test-examples/add-comments.diff index c29c0c5cd..8cc084dce 100644 --- a/core/diff/test-examples/add-comments.diff +++ b/core/diff/test-examples/add-comments.diff @@ -33,21 +33,21 @@ def read_landing_page(): --- + # Import the FastAPI class from the fastapi module - from fastapi import FastAPI - + from fastapi import FastAPI + + # Create an instance of the FastAPI application - app = FastAPI() - + app = FastAPI() + + # Define a route for the root endpoint ("/") - @app.get("/") - def read_root(): + @app.get("/") + def read_root(): + # Return a simple JSON response + return {"Hello": "World"} // This is an example comment + + # Define a route for the landing page endpoint ("/landing") - return {"Hello": "World"} - - @app.get("/landing") - def read_landing_page(): + @app.get("/landing") + def read_landing_page(): + # Return a JSON response with a welcome message - return {"message": "Welcome to the landing page"} \ No newline at end of file + return {"message": "Welcome to the landing page"} \ No newline at end of file diff --git a/core/diff/test-examples/fastapi-tabs-vs-spaces.diff b/core/diff/test-examples/fastapi-tabs-vs-spaces.diff index 84bea74c7..37df56673 100644 --- a/core/diff/test-examples/fastapi-tabs-vs-spaces.diff +++ b/core/diff/test-examples/fastapi-tabs-vs-spaces.diff @@ -28,19 +28,15 @@ def read_landing_page(): - from fastapi import FastAPI + from fastapi import FastAPI, HTTPException - - app = FastAPI() - - @app.get("/") - def read_root(): + + app = FastAPI() + + @app.get("/") + def read_root(): - \treturn {"Hello": "World"} + return {"Hello": "World"} - - @app.get("/landing") - def read_landing_page(): + + @app.get("/landing") + def read_landing_page(): + raise HTTPException(status_code=404, detail="Page not found") -- \treturn {"message": "Welcome to the landing page"} - ---- - -Make sure we correctly ignore tabs-vs-spaces changes \ No newline at end of file +- \treturn {"message": "Welcome to the landing page"} \ No newline at end of file diff --git a/core/diff/test-examples/fastapi.diff b/core/diff/test-examples/fastapi.diff index 9c3b6d1f7..0c8a0ba76 100644 --- a/core/diff/test-examples/fastapi.diff +++ b/core/diff/test-examples/fastapi.diff @@ -28,14 +28,14 @@ def read_landing_page(): - from fastapi import FastAPI + from fastapi import FastAPI, HTTPException - - app = FastAPI() - - @app.get("/") - def read_root(): - return {"Hello": "World"} - - @app.get("/landing") - def read_landing_page(): + + app = FastAPI() + + @app.get("/") + def read_root(): + return {"Hello": "World"} + + @app.get("/landing") + def read_landing_page(): + raise HTTPException(status_code=404, detail="Page not found") - return {"message": "Welcome to the landing page"} \ No newline at end of file diff --git a/core/diff/test-examples/mock-llm.diff b/core/diff/test-examples/mock-llm.diff new file mode 100644 index 000000000..2bf49c698 --- /dev/null +++ b/core/diff/test-examples/mock-llm.diff @@ -0,0 +1,103 @@ +import { ChatMessage, CompletionOptions, ModelProvider } from "../../index.js"; +import { BaseLLM } from "../index.js"; + +class Mock extends BaseLLM { + static Completion = "Test Completion"; + static providerName: ModelProvider = "mock"; + + protected async *_streamComplete( + prompt: string, + options: CompletionOptions, + ): AsyncGenerator { + yield Mock.Completion; + } + + protected async *_streamChat( + messages: ChatMessage[], + options: CompletionOptions, + ): AsyncGenerator { + yield { + role: "assistant", + content: Mock.Completion, + }; + } +} + +export default Mock; + +--- + +import { ChatMessage, CompletionOptions, ModelProvider } from "../../index.js"; +import { BaseLLM } from "../index.js"; + +class Mock extends BaseLLM { + private static _completion: string = "Test Completion"; + static providerName: ModelProvider = "mock"; + + static get completion(): string { + return Mock._completion; + } + + static set completion(value: string) { + Mock._completion = value; + } + + protected async *_streamComplete( + prompt: string, + options: CompletionOptions, + ): AsyncGenerator { + yield Mock.completion; + } + + protected async *_streamChat( + messages: ChatMessage[], + options: CompletionOptions, + ): AsyncGenerator { + yield { + role: "assistant", + content: Mock.completion, + }; + } +} + +export default Mock; + +--- + + import { ChatMessage, CompletionOptions, ModelProvider } from "../../index.js"; + import { BaseLLM } from "../index.js"; + + class Mock extends BaseLLM { +- static Completion = "Test Completion"; ++ private static _completion: string = "Test Completion"; + static providerName: ModelProvider = "mock"; + ++ static get completion(): string { ++ return Mock._completion; ++ } ++ ++ static set completion(value: string) { ++ Mock._completion = value; ++ } ++ + protected async *_streamComplete( + prompt: string, + options: CompletionOptions, + ): AsyncGenerator { +- yield Mock.Completion; ++ yield Mock.completion; + } + + protected async *_streamChat( + messages: ChatMessage[], + options: CompletionOptions, + ): AsyncGenerator { + yield { + role: "assistant", +- content: Mock.Completion, ++ content: Mock.completion, + }; + } + } + + export default Mock; \ No newline at end of file From dabc64e125a9db7108b2abdedfb4d702b31dc380 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 5 Sep 2024 19:32:23 -0700 Subject: [PATCH 8/9] tweak diff algo parameters for fuzzy matching --- core/diff/streamDiff.test.ts | 18 +++++++++-------- core/diff/test-examples/mock-llm.diff | 28 +++++++++++++-------------- core/diff/util.ts | 3 ++- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/core/diff/streamDiff.test.ts b/core/diff/streamDiff.test.ts index 94933623c..8978b92a5 100644 --- a/core/diff/streamDiff.test.ts +++ b/core/diff/streamDiff.test.ts @@ -8,6 +8,12 @@ import path from "node:path"; // "modification" is an extra type used to represent an "old" + "new" diff line type MyersDiffTypes = Extract | "modification"; +const UNIFIED_DIFF_SYMBOLS = { + same: "", + new: "+", + old: "-", +}; + async function* generateLines(lines: string[]): AsyncGenerator { for (const line of lines) { yield line; @@ -50,13 +56,9 @@ function getMyersDiffType(diff: any): MyersDiffTypes | undefined { } function displayDiff(diff: DiffLine[]) { - const symbol = { - same: "", - new: "+", - old: "-", - }; - - return diff.map(({ type, line }) => `${symbol[type]} ${line}`).join("\n"); + return diff + .map(({ type, line }) => `${UNIFIED_DIFF_SYMBOLS[type]} ${line}`) + .join("\n"); } async function expectDiff(file: string) { @@ -237,7 +239,7 @@ describe("streamDiff(", () => { await expectDiff("add-comments"); }); - test.skip("Mock LLM example", async () => { + test("Mock LLM example", async () => { await expectDiff("mock-llm"); }); }); diff --git a/core/diff/test-examples/mock-llm.diff b/core/diff/test-examples/mock-llm.diff index 2bf49c698..42321da5c 100644 --- a/core/diff/test-examples/mock-llm.diff +++ b/core/diff/test-examples/mock-llm.diff @@ -68,24 +68,24 @@ export default Mock; import { BaseLLM } from "../index.js"; class Mock extends BaseLLM { -- static Completion = "Test Completion"; -+ private static _completion: string = "Test Completion"; +- static Completion = "Test Completion"; ++ private static _completion: string = "Test Completion"; static providerName: ModelProvider = "mock"; -+ static get completion(): string { -+ return Mock._completion; -+ } -+ -+ static set completion(value: string) { -+ Mock._completion = value; -+ } -+ ++ static get completion(): string { ++ return Mock._completion; ++ } ++ ++ static set completion(value: string) { ++ Mock._completion = value; ++ } ++ protected async *_streamComplete( prompt: string, options: CompletionOptions, ): AsyncGenerator { -- yield Mock.Completion; -+ yield Mock.completion; +- yield Mock.Completion; ++ yield Mock.completion; } protected async *_streamChat( @@ -94,8 +94,8 @@ export default Mock; ): AsyncGenerator { yield { role: "assistant", -- content: Mock.Completion, -+ content: Mock.completion, +- content: Mock.Completion, ++ content: Mock.completion, }; } } diff --git a/core/diff/util.ts b/core/diff/util.ts index b3a895da9..0e24ee373 100644 --- a/core/diff/util.ts +++ b/core/diff/util.ts @@ -31,7 +31,8 @@ function linesMatch(lineA: string, lineB: string, linesBetween = 0): boolean { return ( // Should be more unlikely for lines to fuzzy match if they are further away - (d / Math.max(lineA.length, lineB.length) < 0.5 - linesBetween * 0.05 || + (d / Math.max(lineA.length, lineB.length) <= + Math.max(0, 0.48 - linesBetween * 0.06) || lineA.trim() === lineB.trim()) && lineA.trim() !== "" ); From d7ff5683f6eb8de487ea8f4322666275c195c0a3 Mon Sep 17 00:00:00 2001 From: Nate Date: Thu, 5 Sep 2024 19:37:49 -0700 Subject: [PATCH 9/9] clean up --- core/diff/streamDiff.test.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/diff/streamDiff.test.ts b/core/diff/streamDiff.test.ts index 8978b92a5..0ad669280 100644 --- a/core/diff/streamDiff.test.ts +++ b/core/diff/streamDiff.test.ts @@ -31,10 +31,6 @@ async function collectDiffs( } const myersDiffs = myersDiff(oldLines.join("\n"), newLines.join("\n")); - // const myersDiffs: DiffLine[] = myersDiffsRaw.map((diff) => ({ - // type: getMyersDiffType(diff), - // line: diff.rhs, - // })); return { streamDiffs, myersDiffs }; }