-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclip.ts
More file actions
76 lines (68 loc) · 2.37 KB
/
Copy pathclip.ts
File metadata and controls
76 lines (68 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Clipboard Extension (DEPRECATED)
*
* Registers a `/clip` command that copies the last assistant message
* (as markdown) to the macOS clipboard. No LLM round-trip needed.
*
* @deprecated Use the built-in `/copy` command instead.
*/
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import { execFile } from "node:child_process";
import { platform } from "node:os";
function getClipboardCommand(): { cmd: string; args: string[] } {
switch (platform()) {
case "darwin":
return { cmd: "pbcopy", args: [] };
case "win32":
return { cmd: "clip.exe", args: [] };
case "linux":
if (process.env.WAYLAND_DISPLAY) {
return { cmd: "wl-copy", args: [] };
}
return { cmd: "xclip", args: ["-selection", "clipboard"] };
default:
throw new Error(`Unsupported platform: ${platform()}`);
}
}
function copyToClipboard(text: string): Promise<void> {
const { cmd, args } = getClipboardCommand();
return new Promise((resolve, reject) => {
const proc = execFile(cmd, args, (err) => {
if (err) reject(err);
else resolve();
});
proc.stdin?.write(text);
proc.stdin?.end();
});
}
export default function (pi: ExtensionAPI) {
pi.registerCommand("clip", {
description: "[DEPRECATED: use /copy] Copy the last assistant message as markdown to clipboard",
handler: async (_args, ctx) => {
ctx.ui.notify("/clip is deprecated. Use the built-in /copy instead.", "warning");
const entries = ctx.sessionManager.getBranch();
// Walk backwards to find the last assistant message
for (let i = entries.length - 1; i >= 0; i--) {
const entry = entries[i];
if (
entry.type === "message" &&
entry.message.role === "assistant" &&
Array.isArray(entry.message.content)
) {
const textParts = entry.message.content
.filter((block: any) => block.type === "text")
.map((block: any) => block.text);
if (textParts.length === 0) {
ctx.ui.notify("Last assistant message had no text content.", "warn");
return;
}
const markdown = textParts.join("\n\n");
await copyToClipboard(markdown);
ctx.ui.notify("Copied to clipboard!", "success");
return;
}
}
ctx.ui.notify("No assistant message found.", "warn");
},
});
}