Merge pull request #3613 from continuedev/dallin/clipboard-context-provider

Clipboard Context Provider
This commit is contained in:
Nate Sesti 2025-01-07 15:26:19 -08:00 • committed by GitHub
commit eba6479a2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 150 additions and 3 deletions

View file

@ -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: "Recent copies",
type: "submenu",
};
async getContextItems(
query: string,
extras: ContextProviderExtras,
): Promise<ContextItem[]> {
// 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<ContextSubmenuItem[]> {
const recentClipboardItems = clipboardCache.getNItems(MAX_CLIPBOARD_ITEMS);
console.log(recentClipboardItems);
return recentClipboardItems.map((item, index) => {
return {
id: item.id,
title: item.content.slice(0, 20),
description: `#${index + 1}`,
};
});
}
}
export default ClipboardContextProvider;

View file

@ -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(

View file

@ -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<ToCoreProtocol, FromCoreProtocol>
@ -298,6 +299,7 @@ export class Core {
});
on("context/loadSubmenuItems", async (msg) => {
console.log("Load SUMBENU ITEMS", msg);
const config = await this.config();
if (!config) {
return [];
@ -381,6 +383,12 @@ 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(
configHandler: ConfigHandler,
abortedMessageIds: Set<string>,

1
core/index.d.ts vendored
View file

@ -751,6 +751,7 @@ export type ContextProviderName =
| "issue"
| "repo-map"
| "url"
| "clipboard"
| string;
export type TemplateType =

View file

@ -211,4 +211,5 @@ export type ToCoreFromIdeOrWebviewProtocol = {
{ toolCall: ToolCall; selectedModelTitle: string },
{ contextItems: ContextItem[] },
];
"clipboardCache/add": [{ content: string }, void];
};

View file

@ -0,0 +1,54 @@
class ClipboardCache {
private cache: Map<string, string>;
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();

View file

@ -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.

View file

@ -1905,7 +1905,8 @@
"os",
"repo-map",
"greptile",
"web"
"web",
"clipboard"
],
"markdownEnumDescriptions": [
"Reference the contents of the current changes as given by `git diff`",

View file

@ -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(