forked from jorrit-stack/Raycast-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot.js
More file actions
executable file
·168 lines (150 loc) · 6.42 KB
/
copilot.js
File metadata and controls
executable file
·168 lines (150 loc) · 6.42 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env node
// @raycast.schemaVersion 1
// @raycast.title Ask Copilot
// @raycast.mode silent
// @raycast.packageName Copilot
// @raycast.icon 🤖
// @raycast.argument1 { "type": "text", "placeholder": "Selected Text", "optional": true }
// @raycast.argument2 { "type": "text", "placeholder": "Prompt" }
// @raycast.description Open Microsoft Copilot in Chrome and submit a prompt with optional selected text as context
const { execSync } = require("child_process");
const fs = require("fs");
const os = require("os");
const path = require("path");
function sh(cmd, opts = {}) {
return execSync(cmd, { stdio: "pipe", encoding: "utf8", ...opts });
}
const prompt = process.argv[3] || "";
const selectedText = process.argv[2] || "";
const recipientName = process.argv[4] || "";
const useCopilotApp = process.env.COPILOT_APP === "1";
const useBoltSupport = process.env.BOLT_SUPPORT === "1";
// Common rules
const commonRules = [];
if (recipientName && recipientName.trim() !== "") {
commonRules.push(
`If the email context doesn't include a recipient name, address them by name: ${recipientName}. Start with a friendly greeting using their name (for example: "Hi ${recipientName},").`
);
} else {
commonRules.push(
'If you do not know the customer name, start with a generic friendly greeting such as "Hi there,".'
);
}
commonRules.push(
"You may end the email with a short generic closing (for example: Best, Best regards, or similar) but do not include my name in the closing, as my email client will add my signature automatically."
);
// Optional Bolt support profile (match Gemini guidance exactly)
const boltGuidelines = [
"### Core Identity & Tone",
"Role: You are a bolt.new support agent.",
"Goal: Draft clear, professional email replies focused on resolution, care, and goodwill.",
"Empathy: Acknowledge the user's situation authentically. Reassure them we are here to help.",
"No Emojis: Do not use emojis in any part of the email.",
"Punctuation: Do not use em dashes (—); use a regular hyphen (-) instead.",
"### Resolution & Goodwill Policy",
"No Compensation: We do not compensate users (refunds/credits). Focus on making things right through care and resolution.",
"Empathy Phrasing: Use terms like 'As a courtesy', 'To make things right', 'As a gesture of goodwill', or 'To help resolve this'.",
"Inconvenience Phrasing: Use terms like 'For the inconvenience', 'To acknowledge the experience', 'To help offset the inconvenience', or 'In appreciation of your patience'.",
"### Technical Guidance",
"Documentation: Prioritize answers from the official docs: https://support.bolt.new. Cite specific pages where possible.",
"Simplicity: Assume the user is non-technical. Avoid jargon and explain steps simply.",
"UI First: Prefer solutions using Bolt's interface, prompts, or built-in features over code changes.",
"Code: If code is unavoidable, provide minimal, step-by-step guidance or a direct doc link.",
"Structure: Provide short, actionable steps using bullet points for clarity. Keep it concise.",
"### Security & Internal Info",
"Internal Links: You may see internal links (Linear, Slack, Drive, etc.). These are for your context only.",
"Privacy: Never reference or link to internal tools or internal URLs in the customer-facing email.",
"External Links: The only external URL you are permitted to include is https://support.bolt.new."
].join("\n");
const baseBlock = commonRules.join("\n");
const mergedInstructions = (() => {
if (!useBoltSupport) {
if (prompt && prompt.trim() !== "") return [baseBlock, prompt].filter(Boolean).join("\n\n");
return baseBlock;
}
const withBolt = [baseBlock, boltGuidelines].filter(Boolean).join("\n");
if (prompt && prompt.trim() !== "") return `${withBolt}\n\nAdditional instructions from user:\n${prompt}`;
return withBolt;
})();
const finalPrompt =
selectedText && selectedText.trim() !== ""
? `Context (from email):\n\n${selectedText}\n\nInstructions:\n${mergedInstructions || ""}`
: `Instructions:\n${mergedInstructions || ""}`;
// Copy prompt to clipboard
try {
sh(`printf %s "${finalPrompt.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}" | pbcopy`);
} catch (e) {
console.error("Failed to copy to clipboard:", e.message);
}
let appleScript = "";
if (useCopilotApp) {
// Use the macOS Copilot app; just activate and paste into current chat
try {
// Prefer explicit path if available
const explicitPath = "/System/Volumes/Data/Applications/Copilot.app";
try {
sh(`open -a "${explicitPath}"`);
} catch (_) {
// Fall back to common app names
try { sh('open -a "Copilot"'); } catch (__) {
try { sh('open -a "Microsoft Copilot"'); } catch (___) {
// Try by bundle id as last resort
try { sh('open -b com.microsoft.copilot'); } catch (____) {}
}
}
}
} catch (e) {
console.error("Failed to open Microsoft Copilot app:", e.message);
}
appleScript = `
try
tell application "Copilot" to activate
on error
try
tell application "Microsoft Copilot" to activate
on error
try
tell application id "com.microsoft.copilot" to activate
on error
-- If all fails, do nothing; System Events will still paste to frontmost app if any
end try
end try
end try
delay 1.0
tell application "System Events" to keystroke "v" using {command down}
delay 0.2
tell application "System Events" to key code 36
`;
} else {
// Web flow (Chrome). This may open a new tab; acceptable for web usage.
try {
sh('open -a "Google Chrome"');
} catch (e) {
console.error("Failed to open Google Chrome:", e.message);
}
const COPILOT_URL = "https://copilot.microsoft.com/";
try {
sh(`chrome-cli open "${COPILOT_URL}"`);
} catch (e) {
sh(`open -a "Google Chrome" "${COPILOT_URL}"`);
}
appleScript = `
tell application "Google Chrome" to activate
delay 1.0
tell application "System Events" to keystroke "v" using {command down}
delay 0.3
tell application "System Events" to key code 36
`;
}
const tmpScriptPath = path.join(os.tmpdir(), `copilot_autopaste_${Date.now()}.applescript`);
try {
fs.writeFileSync(tmpScriptPath, appleScript, { encoding: "utf8" });
execSync(`osascript "${tmpScriptPath}"`, { stdio: "inherit" });
console.log("Prompt pasted into Copilot.");
} catch (e) {
console.warn("Auto-paste failed; prompt is in clipboard. Paste manually (Cmd+V).");
console.warn(e.message);
} finally {
try { fs.unlinkSync(tmpScriptPath); } catch (_) {}
}
process.exit(0);