From caf31ed11f55d7be40cb74225dfd1068594deaec Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Sun, 29 Dec 2024 15:14:56 +0100 Subject: [PATCH 1/2] clipboard context provider p1 --- .../providers/ClipboardContextProvider.ts | 57 +++++++++++++++++++ core/core.ts | 5 ++ core/protocol/core.ts | 1 + core/util/clipboardCache.ts | 54 ++++++++++++++++++ extensions/vscode/package-lock.json | 4 +- extensions/vscode/src/commands.ts | 13 ++++- 6 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 core/context/providers/ClipboardContextProvider.ts create mode 100644 core/util/clipboardCache.ts diff --git a/core/context/providers/ClipboardContextProvider.ts b/core/context/providers/ClipboardContextProvider.ts new file mode 100644 index 000000000..eb128ea7f --- /dev/null +++ b/core/context/providers/ClipboardContextProvider.ts @@ -0,0 +1,57 @@ +import { BaseContextProvider } from ".."; +import { + ContextItem, + ContextProviderDescription, + ContextProviderExtras, + ContextSubmenuItem, + LoadSubmenuItemsArgs, +} from "../.."; +import { clipboardCache } from "../../util/clipboardCache"; + +const MAX_CLIPBOARD_ITEMS = 10; + +class ClipboardContextProvider extends BaseContextProvider { + static description: ContextProviderDescription = { + title: "clipboard", + displayTitle: "Clipboard", + description: "Use recent copies", + type: "submenu", + }; + + async getContextItems( + query: string, + extras: ContextProviderExtras, + ): Promise { + // Assume the query is a cache id + const id = query.trim(); + const content = clipboardCache.get(id); + + if (content) { + clipboardCache.select(id); + return [ + { + name: "Clipboard item", + description: content.slice(0, 20), + content, + }, + ]; + } + return []; + } + + async loadSubmenuItems( + args: LoadSubmenuItemsArgs, + ): Promise { + const recentClipboardItems = clipboardCache.getNItems(MAX_CLIPBOARD_ITEMS); + + return recentClipboardItems.map((item, index) => { + return { + id: item.id, + title: item.content.slice(0, 20), + description: `#${index + 1}`, + }; + }); + } +} + +export default ClipboardContextProvider; diff --git a/core/core.ts b/core/core.ts index 14389910c..90a6246c3 100644 --- a/core/core.ts +++ b/core/core.ts @@ -46,6 +46,7 @@ import * as URI from "uri-js"; import { SYSTEM_PROMPT_DOT_FILE } from "./config/getSystemPromptDotFile"; import type { IMessenger, Message } from "./protocol/messenger"; import { localPathToUri } from "./util/pathToUri"; +import { clipboardCache } from "./util/clipboardCache"; export class Core { // implements IMessenger @@ -377,6 +378,10 @@ export class Core { }; }); + on("clipboardCache/add", (msg) => { + clipboardCache.add(uuidv4(), msg.data.content); + }); + async function* llmStreamChat( configHandler: ConfigHandler, abortedMessageIds: Set, diff --git a/core/protocol/core.ts b/core/protocol/core.ts index e05ce1bc4..d0c84121f 100644 --- a/core/protocol/core.ts +++ b/core/protocol/core.ts @@ -193,4 +193,5 @@ export type ToCoreFromIdeOrWebviewProtocol = { { toolCall: ToolCall; selectedModelTitle: string }, { contextItems: ContextItem[] }, ]; + "clipboardCache/add": [{ content: string }, void]; }; diff --git a/core/util/clipboardCache.ts b/core/util/clipboardCache.ts new file mode 100644 index 000000000..4ecd16afc --- /dev/null +++ b/core/util/clipboardCache.ts @@ -0,0 +1,54 @@ +class ClipboardCache { + private cache: Map; + private order: string[]; + private readonly maxSize = 30; + + constructor() { + this.cache = new Map(); + this.order = []; + } + + add(id: string, content: string): void { + if (!content) { + return; + } + if (this.order.length >= this.maxSize) { + const oldest = this.order.pop(); + if (oldest) { + this.cache.delete(oldest); + } + } + + this.cache.set(id, content); + this.order.unshift(id); + } + + getNItems(count: number): { + id: string; + content: string; + }[] { + return this.order.slice(0, count).map((id) => ({ + id, + content: this.cache.get(id) || "", + })); + } + + get(id: string): string | undefined { + return this.cache.get(id); + } + + select(id: string): void { + const index = this.order.indexOf(id); + if (index > -1) { + this.order.splice(index, 1); + this.order.unshift(id); + } + } + + clear(): void { + this.cache.clear(); + this.order = []; + } +} + +export const clipboardCache = new ClipboardCache(); diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 8f5cad8a0..0a2dd8330 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -1,12 +1,12 @@ { "name": "continue", - "version": "0.9.247", + "version": "0.9.248", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "continue", - "version": "0.9.247", + "version": "0.9.248", "license": "Apache-2.0", "dependencies": { "@continuedev/fetch": "^1.0.3", diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index b83d3d380..147a1f118 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -1001,7 +1001,10 @@ const getCommandsMap: ( }; }; -const registerCopyBufferSpy = (context: vscode.ExtensionContext) => { +const registerCopyBufferSpy = ( + context: vscode.ExtensionContext, + core: Core, +) => { const typeDisposable = vscode.commands.registerCommand( "editor.action.clipboardCopyAction", async (arg) => doCopy(typeDisposable), @@ -1014,6 +1017,12 @@ const registerCopyBufferSpy = (context: vscode.ExtensionContext) => { const clipboardText = await vscode.env.clipboard.readText(); + if (clipboardText) { + core.invoke("clipboardCache/add", { + content: clipboardText, + }); + } + await context.workspaceState.update("continue.copyBuffer", { text: clipboardText, copiedAt: new Date().toISOString(), @@ -1043,7 +1052,7 @@ export function registerAllCommands( core: Core, editDecorationManager: EditDecorationManager, ) { - registerCopyBufferSpy(context); + registerCopyBufferSpy(context, core); for (const [command, callback] of Object.entries( getCommandsMap( From 465362e407ac7ee0da1951f3ee78d6f9c60a19a3 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Sun, 29 Dec 2024 15:34:13 +0100 Subject: [PATCH 2/2] clipboard context provider p2 --- core/context/providers/ClipboardContextProvider.ts | 4 ++-- core/context/providers/index.ts | 2 ++ core/core.ts | 3 +++ core/index.d.ts | 1 + docs/docs/customize/context-providers.md | 14 ++++++++++++++ extensions/vscode/config_schema.json | 8 +++----- 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/core/context/providers/ClipboardContextProvider.ts b/core/context/providers/ClipboardContextProvider.ts index eb128ea7f..86e64d619 100644 --- a/core/context/providers/ClipboardContextProvider.ts +++ b/core/context/providers/ClipboardContextProvider.ts @@ -14,7 +14,7 @@ class ClipboardContextProvider extends BaseContextProvider { static description: ContextProviderDescription = { title: "clipboard", displayTitle: "Clipboard", - description: "Use recent copies", + description: "Recent copies", type: "submenu", }; @@ -43,7 +43,7 @@ class ClipboardContextProvider extends BaseContextProvider { args: LoadSubmenuItemsArgs, ): Promise { const recentClipboardItems = clipboardCache.getNItems(MAX_CLIPBOARD_ITEMS); - + console.log(recentClipboardItems); return recentClipboardItems.map((item, index) => { return { id: item.id, diff --git a/core/context/providers/index.ts b/core/context/providers/index.ts index f5656832c..3d1d7cc55 100644 --- a/core/context/providers/index.ts +++ b/core/context/providers/index.ts @@ -1,5 +1,6 @@ import { BaseContextProvider } from "../"; import { ContextProviderName } from "../../"; +import ClipboardContextProvider from "./ClipboardContextProvider"; import CodeContextProvider from "./CodeContextProvider"; import ContinueProxyContextProvider from "./ContinueProxyContextProvider"; @@ -62,6 +63,7 @@ export const Providers: (typeof BaseContextProvider)[] = [ GreptileContextProvider, WebContextProvider, MCPContextProvider, + ClipboardContextProvider, ]; export function contextProviderClassFromName( diff --git a/core/core.ts b/core/core.ts index 90a6246c3..871856038 100644 --- a/core/core.ts +++ b/core/core.ts @@ -295,6 +295,7 @@ export class Core { }); on("context/loadSubmenuItems", async (msg) => { + console.log("Load SUMBENU ITEMS", msg); const config = await this.config(); if (!config) { return []; @@ -379,7 +380,9 @@ export class Core { }); on("clipboardCache/add", (msg) => { + console.log("ADD TO CLIPBOARD", msg); clipboardCache.add(uuidv4(), msg.data.content); + this.messenger.send("refreshSubmenuItems", undefined); }); async function* llmStreamChat( diff --git a/core/index.d.ts b/core/index.d.ts index d916a19f1..c21040e3c 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -751,6 +751,7 @@ export type ContextProviderName = | "issue" | "repo-map" | "url" + | "clipboard" | string; export type TemplateType = diff --git a/docs/docs/customize/context-providers.md b/docs/docs/customize/context-providers.md index 256f4eab5..0b48c386b 100644 --- a/docs/docs/customize/context-providers.md +++ b/docs/docs/customize/context-providers.md @@ -576,6 +576,20 @@ The response 200 OK should be a JSON object with the following structure: } ``` +### `@Clipboard` + +Reference recent clipboard items + +```json title="config.json" +{ + "contextProviders": [ + { + "name": "clipboard" + } + ] +} +``` + ### Requesting Context Providers Not seeing what you want? Create an issue [here](https://github.com/continuedev/continue/issues/new?assignees=TyDunn&labels=enhancement&projects=&template=feature-request-%F0%9F%92%AA.md&title=) to request a new Context Provider. diff --git a/extensions/vscode/config_schema.json b/extensions/vscode/config_schema.json index f2c98642f..d60daa499 100644 --- a/extensions/vscode/config_schema.json +++ b/extensions/vscode/config_schema.json @@ -1864,7 +1864,8 @@ "os", "repo-map", "greptile", - "web" + "web", + "clipboard" ], "markdownEnumDescriptions": [ "Reference the contents of the current changes as given by `git diff`", @@ -2805,10 +2806,7 @@ "type": "object", "properties": { "model": { - "enum": [ - "cohere.rerank-v3-5:0", - "amazon.rerank-v1:0" - ] + "enum": ["cohere.rerank-v3-5:0", "amazon.rerank-v1:0"] } }, "required": ["model"]