@carrieros/cli-staging 0.0.0-stage → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -2
- package/dist/commands/attach.js +45 -0
- package/dist/commands/auth.js +60 -0
- package/dist/commands/btw.js +78 -0
- package/dist/commands/changes.js +167 -0
- package/dist/commands/chat.js +182 -0
- package/dist/commands/context.js +200 -0
- package/dist/commands/doctor.js +119 -0
- package/dist/commands/files.js +94 -0
- package/dist/commands/help.js +37 -0
- package/dist/commands/history.js +112 -0
- package/dist/commands/login.js +62 -0
- package/dist/commands/logout.js +26 -0
- package/dist/commands/memory.js +162 -0
- package/dist/commands/mode-model.js +64 -0
- package/dist/commands/models.js +28 -0
- package/dist/commands/modes.js +30 -0
- package/dist/commands/permissions.js +78 -0
- package/dist/commands/project.js +106 -0
- package/dist/commands/repo.js +146 -0
- package/dist/commands/repos.js +61 -0
- package/dist/commands/review.js +92 -0
- package/dist/commands/scope.js +155 -0
- package/dist/commands/search.js +92 -0
- package/dist/commands/sessions.js +282 -0
- package/dist/commands/settings.js +160 -0
- package/dist/commands/update.js +285 -0
- package/dist/commands/upload.js +223 -0
- package/dist/commands/usage.js +229 -0
- package/dist/commands/version.js +26 -0
- package/dist/commands/whoami.js +44 -0
- package/dist/ide/editor-context.js +275 -0
- package/dist/ide/ide-session.js +364 -0
- package/dist/ide/tool-host.js +109 -0
- package/dist/main.js +132 -0
- package/dist/services/api.js +441 -0
- package/dist/services/auth-origin.js +20 -0
- package/dist/services/auto-update.js +221 -0
- package/dist/services/chat-stream.js +168 -0
- package/dist/services/checkpoints.js +278 -0
- package/dist/services/client-tools.js +356 -0
- package/dist/services/code-intel.js +243 -0
- package/dist/services/code-symbols.js +94 -0
- package/dist/services/coding-workflow.js +554 -0
- package/dist/services/command-coordinator.js +273 -0
- package/dist/services/command-policy.js +434 -0
- package/dist/services/config.js +384 -0
- package/dist/services/container-runner.js +254 -0
- package/dist/services/container-supervisor.js +38 -0
- package/dist/services/context.js +416 -0
- package/dist/services/device-auth.js +288 -0
- package/dist/services/diff.js +196 -0
- package/dist/services/env-policy.js +108 -0
- package/dist/services/execution-platform.js +30 -0
- package/dist/services/file-detect.js +104 -0
- package/dist/services/file-resolver.js +199 -0
- package/dist/services/file-upload.js +126 -0
- package/dist/services/folder-key.js +22 -0
- package/dist/services/git-tools.js +429 -0
- package/dist/services/glob-match.js +164 -0
- package/dist/services/helper-bin.js +85 -0
- package/dist/services/ignore-rules.js +189 -0
- package/dist/services/instruction-resolver.js +54 -0
- package/dist/services/job-helper.js +328 -0
- package/dist/services/local-files.js +639 -0
- package/dist/services/local-tools.js +937 -0
- package/dist/services/login-flow.js +156 -0
- package/dist/services/meta.js +42 -0
- package/dist/services/mutation-coordinator.js +327 -0
- package/dist/services/mutation-groups.js +309 -0
- package/dist/services/mutation-recovery.js +159 -0
- package/dist/services/mutations.js +679 -0
- package/dist/services/open-url.js +68 -0
- package/dist/services/output.js +62 -0
- package/dist/services/packaged-update.js +403 -0
- package/dist/services/packaged.js +39 -0
- package/dist/services/permission-mode.js +78 -0
- package/dist/services/process-runner.js +412 -0
- package/dist/services/project-context.js +298 -0
- package/dist/services/redaction.js +201 -0
- package/dist/services/result-reconciliation.js +49 -0
- package/dist/services/secure-store.js +217 -0
- package/dist/services/session-resolver.js +115 -0
- package/dist/services/sia.js +51 -0
- package/dist/services/spinner.js +40 -0
- package/dist/services/supported-runtime.js +59 -0
- package/dist/services/task-context.js +212 -0
- package/dist/services/task-snapshot.js +38 -0
- package/dist/services/tool-result-outbox.js +195 -0
- package/dist/services/update.js +730 -0
- package/dist/services/workspace-registry.js +310 -0
- package/dist/services/workspace.js +580 -0
- package/dist/shell/shell-chat.js +1622 -0
- package/dist/shell/shell-commands.js +411 -0
- package/dist/shell/shell-prompt.js +110 -0
- package/dist/shell/shell-renderer.js +51 -0
- package/dist/shell/shell-router.js +37 -0
- package/dist/shell/shell.js +414 -0
- package/dist/shell/task-context-view.js +40 -0
- package/dist/staging.js +5 -0
- package/dist/types/index.js +6 -0
- package/dist/ui/agent-cards.js +111 -0
- package/dist/ui/agent-presentation.js +131 -0
- package/dist/ui/approval.js +111 -0
- package/dist/ui/banner.js +56 -0
- package/dist/ui/btw-overlay.js +230 -0
- package/dist/ui/change-review.js +334 -0
- package/dist/ui/chat-renderer.js +463 -0
- package/dist/ui/code-block.js +52 -0
- package/dist/ui/color-picker.js +100 -0
- package/dist/ui/colors.js +55 -0
- package/dist/ui/command-details.js +11 -0
- package/dist/ui/command-filter.js +92 -0
- package/dist/ui/command-item.js +6 -0
- package/dist/ui/command-palette.js +383 -0
- package/dist/ui/command-registry.js +128 -0
- package/dist/ui/command-renderer.js +55 -0
- package/dist/ui/command-review.js +187 -0
- package/dist/ui/composer-layout.js +74 -0
- package/dist/ui/conversation.js +450 -0
- package/dist/ui/interrupt.js +35 -0
- package/dist/ui/live.js +141 -0
- package/dist/ui/logo.js +58 -0
- package/dist/ui/markdown.js +205 -0
- package/dist/ui/message.js +21 -0
- package/dist/ui/screen-prompt.js +181 -0
- package/dist/ui/screen.js +297 -0
- package/dist/ui/select.js +284 -0
- package/dist/ui/sia-qa.js +213 -0
- package/dist/ui/spinner.js +23 -0
- package/dist/ui/sticky-stream.js +64 -0
- package/dist/ui/task-context-card.js +99 -0
- package/dist/ui/text-prompt.js +74 -0
- package/dist/ui/theme.js +80 -0
- package/dist/ui/trust.js +88 -0
- package/dist/ui/ui.js +101 -0
- package/package.json +33 -4
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { line } from "../services/output.js";
|
|
2
|
+
import { withSpinner } from "../services/spinner.js";
|
|
3
|
+
import { isPackaged, isSignedRelease, PACKAGED_DOWNLOAD_URL } from "../services/packaged.js";
|
|
4
|
+
import { checkInstallerSignature, checkPackagedUpdate, downloadVerified, launchInstaller, updatesDir, UpdateError } from "../services/packaged-update.js";
|
|
5
|
+
import { acquireUpdateLock, checkChannel, defaultUpdateChannel, detectInstallation, installExactVersion, manualUpdateCommand, PACKAGE_NAME, packageName, releaseUpdateLock, updateInProgress } from "../services/update.js";
|
|
6
|
+
import { updateSettings } from "../services/auto-update.js";
|
|
7
|
+
import { c } from "../ui/colors.js";
|
|
8
|
+
import { pickFromList } from "../ui/select.js";
|
|
9
|
+
/**
|
|
10
|
+
* `/update` and `cos update` — check for, and install, a newer CLI.
|
|
11
|
+
*
|
|
12
|
+
* Bare `/update` NEVER mutates the installation; `/update install` is the only
|
|
13
|
+
* path that changes anything, and only after an explicit confirmation. The
|
|
14
|
+
* process is never restarted: npm swaps files under a running Node, and
|
|
15
|
+
* re-execing through the shim while the old process still holds handles is how
|
|
16
|
+
* partial installs happen. The user is told to restart instead.
|
|
17
|
+
*
|
|
18
|
+
* This file owns every message. services/update.ts owns the logic, so the shell
|
|
19
|
+
* command and the standalone command cannot drift apart.
|
|
20
|
+
*/
|
|
21
|
+
const MANUAL = "npm install -g " + PACKAGE_NAME + "@latest";
|
|
22
|
+
function heading() {
|
|
23
|
+
line();
|
|
24
|
+
line(" " + c.title("Carrier OS CLI"));
|
|
25
|
+
line();
|
|
26
|
+
}
|
|
27
|
+
function versions(current, latest) {
|
|
28
|
+
line(" " + c.dim("Current version: ") + c.value(current));
|
|
29
|
+
line(" " + c.dim("Latest version: ") + c.value(latest));
|
|
30
|
+
line();
|
|
31
|
+
}
|
|
32
|
+
function renderTerminalState(state, manual = MANUAL) {
|
|
33
|
+
if (state.kind === "current") {
|
|
34
|
+
line();
|
|
35
|
+
line(" " + c.ok("✓") + " " + c.value("Carrier OS CLI is already up to date (v" + state.current + ")"));
|
|
36
|
+
line();
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (state.kind === "ahead") {
|
|
40
|
+
heading();
|
|
41
|
+
versions(state.current, state.latest);
|
|
42
|
+
line(" " + c.ok("✓") + " " + c.value("You are running a newer version than npm's latest."));
|
|
43
|
+
line(" " + c.dim("Nothing to do — this will not downgrade."));
|
|
44
|
+
line();
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (state.kind === "no-npm") {
|
|
48
|
+
heading();
|
|
49
|
+
line(" " + c.err("✗") + " " + c.value("Unable to check for updates."));
|
|
50
|
+
line();
|
|
51
|
+
line(" " + c.dim("npm was not found on this system."));
|
|
52
|
+
line(" " + c.dim("Update manually with:"));
|
|
53
|
+
line(" " + c.cmd(manual));
|
|
54
|
+
line();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
// unreachable
|
|
58
|
+
heading();
|
|
59
|
+
line(" " + c.err("✗") + " " + c.value("Could not check for the latest version."));
|
|
60
|
+
line();
|
|
61
|
+
line(" " + c.dim("Your current version: ") + c.value(state.current));
|
|
62
|
+
line(" " + c.dim(state.detail));
|
|
63
|
+
line();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* `/update` and `cos update` — check the configured channel and, if a newer
|
|
67
|
+
* release exists, offer to install it.
|
|
68
|
+
*
|
|
69
|
+
* Nothing is installed without an explicit Yes (or --yes), never during a
|
|
70
|
+
* running agent turn, and never over a local, development, npx or unwritable
|
|
71
|
+
* install — those get the right manual command instead. The process is never
|
|
72
|
+
* restarted: the user is told to restart once the new version is verified on
|
|
73
|
+
* disk. Settings (channel) come from the CLI's own config.
|
|
74
|
+
*/
|
|
75
|
+
export async function runUpdate(opts = {}) {
|
|
76
|
+
// The packaged executable has no npm to update through.
|
|
77
|
+
if (isPackaged()) {
|
|
78
|
+
await runPackagedUpdate(opts.assumeYes === true);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
// Never alongside a running agent task: it may be using files npm would replace.
|
|
82
|
+
if (opts.busy) {
|
|
83
|
+
line();
|
|
84
|
+
line(" " + c.warn("⚠") + " " + c.value("An agent task is running. Run /update again when it has finished."));
|
|
85
|
+
line();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const channel = opts.config ? updateSettings(opts.config).channel : defaultUpdateChannel();
|
|
89
|
+
const manual = manualUpdateCommand(channel);
|
|
90
|
+
const state = await withSpinner(c.dim("Checking for updates…"), () => checkChannel(channel));
|
|
91
|
+
if (state.kind === "current") {
|
|
92
|
+
line();
|
|
93
|
+
line(" " + c.ok("✓") + " " + c.value("You are using the latest version of CarrierOS CLI.") + c.dim(" (v" + state.current + (channel === "latest" ? "" : ", " + channel + " channel") + ")"));
|
|
94
|
+
line();
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (state.kind === "no-channel") {
|
|
98
|
+
heading();
|
|
99
|
+
line(" " + c.dim("Current version: ") + c.value(state.current));
|
|
100
|
+
line(" " + c.warn("⚠") + " " + c.value(state.detail));
|
|
101
|
+
line(" " + c.dim("Switch back with: ") + c.cmd("/settings channel latest"));
|
|
102
|
+
line();
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (state.kind !== "available") {
|
|
106
|
+
renderTerminalState(state, manual);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
heading();
|
|
110
|
+
versions(state.current, state.latest);
|
|
111
|
+
const install = opts.installation ?? (await detectInstallation());
|
|
112
|
+
if (install.kind === "development") {
|
|
113
|
+
line(" " + c.warn("⚠") + " " + c.value("Running from a development checkout (or npm link)."));
|
|
114
|
+
line(" " + c.dim("Automatic update is disabled here — it would replace the link to your checkout."));
|
|
115
|
+
line(" " + c.dim("Install the published CLI with: ") + c.cmd(manual));
|
|
116
|
+
line();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (install.kind === "local") {
|
|
120
|
+
line(" " + c.value("This copy is installed as a project dependency."));
|
|
121
|
+
line(" " + c.dim("Update it with your package manager, e.g. ") + c.cmd("npm install " + packageName() + "@" + state.latest));
|
|
122
|
+
line();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (install.kind === "npx") {
|
|
126
|
+
line(" " + c.dim("Running through npx. Use the new version with: ") + c.cmd("npx " + packageName() + "@" + channel));
|
|
127
|
+
line();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (install.kind !== "global") {
|
|
131
|
+
line(" " + c.dim("This installation cannot be updated automatically. Update with:"));
|
|
132
|
+
line(" " + c.cmd(manual));
|
|
133
|
+
line();
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (!install.writable) {
|
|
137
|
+
line(" " + c.warn("⚠") + " " + c.value("Your global npm folder is not writable by this user."));
|
|
138
|
+
line(" " + c.dim("Carrier OS will not request administrator rights. Update with:"));
|
|
139
|
+
line(" " + c.cmd(manual));
|
|
140
|
+
line();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const dir = opts.config && "dir" in opts.config ? opts.config.dir : null;
|
|
144
|
+
if (dir && updateInProgress(dir)) {
|
|
145
|
+
line(" " + c.value("An update is already being installed in the background."));
|
|
146
|
+
line(" " + c.dim("Restart Carrier OS CLI in a moment to use it."));
|
|
147
|
+
line();
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
line(" " + c.value("CarrierOS CLI " + state.latest + " is available."));
|
|
151
|
+
line();
|
|
152
|
+
const answer = opts.assumeYes ? "yes" : await pickFromList({
|
|
153
|
+
title: "Update now?",
|
|
154
|
+
description: "Runs npm install -g " + packageName() + "@" + state.latest,
|
|
155
|
+
choices: [
|
|
156
|
+
{ value: "yes", label: "Yes", description: "Install v" + state.latest + " now" },
|
|
157
|
+
{ value: "no", label: "No", description: "Stay on v" + state.current }
|
|
158
|
+
],
|
|
159
|
+
initial: 0,
|
|
160
|
+
confirmOnNumber: true
|
|
161
|
+
});
|
|
162
|
+
// Esc resolves null — a dismissed prompt must never install anything.
|
|
163
|
+
if (answer !== "yes") {
|
|
164
|
+
line(" " + c.dim("Update cancelled."));
|
|
165
|
+
line();
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (dir && !acquireUpdateLock(dir)) {
|
|
169
|
+
line(" " + c.value("An update is already being installed. Try again in a moment."));
|
|
170
|
+
line();
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
let result;
|
|
174
|
+
try {
|
|
175
|
+
result = await withSpinner(c.dim("Installing CarrierOS CLI " + state.latest + "…"), () => installExactVersion(state.latest, install.globalRoot ?? null));
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
if (dir)
|
|
179
|
+
releaseUpdateLock(dir);
|
|
180
|
+
}
|
|
181
|
+
if (!result.ok) {
|
|
182
|
+
line();
|
|
183
|
+
line(" " + c.err("✗") + " " + c.value("CarrierOS CLI could not be updated."));
|
|
184
|
+
line(" " + c.dim("You are still on v") + c.value(state.current) + c.dim(". Update manually with: ") + c.cmd(manual));
|
|
185
|
+
if (result.permission)
|
|
186
|
+
line(" " + c.dim("npm reported a permissions problem with the global npm folder."));
|
|
187
|
+
if (result.detail) {
|
|
188
|
+
line();
|
|
189
|
+
for (const l of result.detail.split("\n").slice(0, 6))
|
|
190
|
+
line(" " + c.dim(l));
|
|
191
|
+
}
|
|
192
|
+
line();
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
// Verified on disk (installExactVersion reads the installed package.json).
|
|
196
|
+
if (opts.config) {
|
|
197
|
+
opts.config.set("pendingUpdate", undefined);
|
|
198
|
+
opts.config.set("updateFailedVersion", undefined);
|
|
199
|
+
}
|
|
200
|
+
line();
|
|
201
|
+
line(" " + c.ok("✓") + " " + c.value("CarrierOS CLI " + result.installed + " has been installed successfully."));
|
|
202
|
+
line(" " + c.dim(state.current) + c.dim(" → ") + c.value(String(result.installed)));
|
|
203
|
+
line(" " + c.dim("Restart the CLI to use the new version."));
|
|
204
|
+
line();
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* The packaged (installer) update: check the release manifest, then — only
|
|
208
|
+
* after an explicit Yes — download, verify the checksum (and the signature on
|
|
209
|
+
* a signed build), hand the installer this PID and exit. The installer waits
|
|
210
|
+
* for this process to end before replacing cos.exe, so nothing is swapped
|
|
211
|
+
* underneath a running CLI. Every failure leaves the current version intact.
|
|
212
|
+
*/
|
|
213
|
+
async function runPackagedUpdate(assumeYes) {
|
|
214
|
+
const state = await withSpinner(c.dim("Checking for updates…"), () => checkPackagedUpdate());
|
|
215
|
+
if (state.kind === "current") {
|
|
216
|
+
renderTerminalState({ kind: "current", current: state.current, latest: state.latest });
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
if (state.kind === "ahead") {
|
|
220
|
+
heading();
|
|
221
|
+
versions(state.current, state.latest);
|
|
222
|
+
line(" " + c.ok("✓") + " " + c.value("You are running a newer version than the latest release."));
|
|
223
|
+
line(" " + c.dim("Nothing to do — this will not downgrade."));
|
|
224
|
+
line();
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (state.kind === "unreachable") {
|
|
228
|
+
heading();
|
|
229
|
+
line(" " + c.err("✗") + " " + c.value("Could not check for the latest version."));
|
|
230
|
+
line();
|
|
231
|
+
line(" " + c.dim("Your current version: ") + c.value(state.current));
|
|
232
|
+
line(" " + c.dim(state.detail));
|
|
233
|
+
line(" " + c.dim("You can also download the installer from ") + c.value(PACKAGED_DOWNLOAD_URL) + c.dim("."));
|
|
234
|
+
line();
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const artifact = state.manifest.artifacts["win-x64-setup"];
|
|
238
|
+
heading();
|
|
239
|
+
versions(state.current, state.latest);
|
|
240
|
+
line(" " + c.value("A new version is available."));
|
|
241
|
+
line();
|
|
242
|
+
const answer = assumeYes ? "yes" : await pickFromList({
|
|
243
|
+
title: "Update now?",
|
|
244
|
+
description: `Downloads ${artifact.file} (${(artifact.size / 1024 / 1024).toFixed(1)} MB), verifies it, then installs it`,
|
|
245
|
+
choices: [
|
|
246
|
+
{ value: "yes", label: "Yes", description: "Install v" + state.latest + " — Carrier OS closes while it updates" },
|
|
247
|
+
{ value: "no", label: "No", description: "Stay on v" + state.current }
|
|
248
|
+
],
|
|
249
|
+
initial: 0,
|
|
250
|
+
confirmOnNumber: true
|
|
251
|
+
});
|
|
252
|
+
if (answer !== "yes") {
|
|
253
|
+
line(" " + c.dim("Update cancelled."));
|
|
254
|
+
line();
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
try {
|
|
258
|
+
const installer = await withSpinner(c.dim("Downloading and verifying…"), () => downloadVerified(artifact, updatesDir()));
|
|
259
|
+
const sig = checkInstallerSignature(installer, artifact, isSignedRelease());
|
|
260
|
+
const log = await launchInstaller(installer);
|
|
261
|
+
line();
|
|
262
|
+
line(" " + c.ok("✓") + " " + c.value(`Verified v${state.latest}`) + c.dim(sig.verified ? " (checksum + signature)" : " (checksum)"));
|
|
263
|
+
line(" " + c.dim("Installing now. Carrier OS will close; open a new terminal when it finishes."));
|
|
264
|
+
line(" " + c.dim("Installer log: ") + c.value(log));
|
|
265
|
+
line();
|
|
266
|
+
// The installer is waiting for this PID. Exit now so it can replace cos.exe.
|
|
267
|
+
process.exit(0);
|
|
268
|
+
}
|
|
269
|
+
catch (err) {
|
|
270
|
+
line();
|
|
271
|
+
line(" " + c.err("✗") + " " + c.value("Update failed."));
|
|
272
|
+
line(" " + c.dim(err instanceof UpdateError ? err.message : String(err?.message ?? err)));
|
|
273
|
+
line(" " + c.dim("Carrier OS CLI remains at v") + c.value(state.current) + c.dim("."));
|
|
274
|
+
line();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
export function registerUpdate(program, ctx) {
|
|
278
|
+
program
|
|
279
|
+
.command("update")
|
|
280
|
+
.description("Check for and install the latest Carrier OS CLI version")
|
|
281
|
+
.option("-y, --yes", "install an available update without asking (for scripts)")
|
|
282
|
+
.action(async (opts) => {
|
|
283
|
+
await runUpdate({ assumeYes: opts.yes === true, config: ctx.config });
|
|
284
|
+
});
|
|
285
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { attachedFilenames, ensureUploaded, uploadAndAttach, UploadError } from "../services/file-upload.js";
|
|
4
|
+
import { errorLine, info, line, success } from "../services/output.js";
|
|
5
|
+
import { withSpinner } from "../services/spinner.js";
|
|
6
|
+
import { c } from "../ui/colors.js";
|
|
7
|
+
import { pickFromList } from "../ui/select.js";
|
|
8
|
+
import { ensureWorkspaceTrust } from "../ui/trust.js";
|
|
9
|
+
/** Expand file args — literal paths pass through; `*`/`?` are globbed locally. */
|
|
10
|
+
function expandFiles(patterns) {
|
|
11
|
+
const out = [];
|
|
12
|
+
for (const p of patterns) {
|
|
13
|
+
const base = path.basename(p);
|
|
14
|
+
if (/[*?]/.test(base)) {
|
|
15
|
+
const dir = path.resolve(path.dirname(p));
|
|
16
|
+
const rx = new RegExp("^" +
|
|
17
|
+
base
|
|
18
|
+
.replace(/[.+^${}()|[\]\\]/g, "\\$&")
|
|
19
|
+
.replace(/\*/g, ".*")
|
|
20
|
+
.replace(/\?/g, ".") +
|
|
21
|
+
"$", "i");
|
|
22
|
+
try {
|
|
23
|
+
for (const name of fs.readdirSync(dir)) {
|
|
24
|
+
const full = path.join(dir, name);
|
|
25
|
+
if (rx.test(name) && fs.statSync(full).isFile())
|
|
26
|
+
out.push(full);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
/* directory missing */
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
out.push(path.resolve(p));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return [...new Set(out)];
|
|
38
|
+
}
|
|
39
|
+
function reportError(err) {
|
|
40
|
+
errorLine(err instanceof UploadError ? err.message : err.message);
|
|
41
|
+
}
|
|
42
|
+
/** "Attached files (N)" list. */
|
|
43
|
+
function printAttachedList(ctx) {
|
|
44
|
+
const names = attachedFilenames(ctx);
|
|
45
|
+
if (names.length === 0)
|
|
46
|
+
return;
|
|
47
|
+
line();
|
|
48
|
+
line(" " + c.dim(`Attached files (${names.length})`));
|
|
49
|
+
for (const n of names)
|
|
50
|
+
line(" " + c.dim("• ") + c.value(n));
|
|
51
|
+
}
|
|
52
|
+
/** Next-command suggestions after a standalone upload. */
|
|
53
|
+
function printTry(multiple) {
|
|
54
|
+
line();
|
|
55
|
+
line(" " + c.dim("Try:"));
|
|
56
|
+
line(' ' + c.cmd('cos chat "Summarize this file"'));
|
|
57
|
+
if (multiple)
|
|
58
|
+
line(' ' + c.cmd('cos chat "Compare the attached files"'));
|
|
59
|
+
line(" " + c.cmd("cos") + c.dim(" (open interactive mode)"));
|
|
60
|
+
line();
|
|
61
|
+
}
|
|
62
|
+
/** Yes/No confirm (default Yes, 1→Yes / 2→No, Esc cancels → false). */
|
|
63
|
+
async function confirmAttach(question) {
|
|
64
|
+
const chosen = await pickFromList({
|
|
65
|
+
title: question,
|
|
66
|
+
choices: [
|
|
67
|
+
{ value: "yes", label: "Yes" },
|
|
68
|
+
{ value: "no", label: "No" }
|
|
69
|
+
],
|
|
70
|
+
initial: 0,
|
|
71
|
+
confirmOnNumber: true
|
|
72
|
+
});
|
|
73
|
+
return chosen === "yes";
|
|
74
|
+
}
|
|
75
|
+
/** Standalone single-file flow: upload/find, then confirm attach (Case A/B/C). */
|
|
76
|
+
async function standaloneSingle(ctx, client, filePath, opts) {
|
|
77
|
+
const r = await ensureUploaded(ctx, client, filePath, {
|
|
78
|
+
onUploading: () => {
|
|
79
|
+
line();
|
|
80
|
+
line(" " + c.dim("Uploading…"));
|
|
81
|
+
},
|
|
82
|
+
onUploaded: (n) => success(`Uploaded ${n}`),
|
|
83
|
+
onProcessing: () => {
|
|
84
|
+
line();
|
|
85
|
+
line(" " + c.dim("Processing in background…"));
|
|
86
|
+
},
|
|
87
|
+
process: opts.process
|
|
88
|
+
});
|
|
89
|
+
// Case A — already uploaded AND attached.
|
|
90
|
+
if (r.status === "existing" && r.wasAttached) {
|
|
91
|
+
line();
|
|
92
|
+
success(`${r.filename} is already attached to this conversation.`);
|
|
93
|
+
line();
|
|
94
|
+
line(" " + c.dim("Ready to use."));
|
|
95
|
+
printAttachedList(ctx);
|
|
96
|
+
printTry(attachedFilenames(ctx).length > 1);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
// Case B — previously uploaded, not attached.
|
|
100
|
+
if (r.status === "existing" && !r.wasAttached) {
|
|
101
|
+
line();
|
|
102
|
+
success(`Found existing upload: ${r.filename}`);
|
|
103
|
+
line();
|
|
104
|
+
const yes = await confirmAttach("Attach it to this conversation?");
|
|
105
|
+
line();
|
|
106
|
+
if (yes) {
|
|
107
|
+
ctx.attachFile(r.fileId);
|
|
108
|
+
success(`Attached ${r.filename}`);
|
|
109
|
+
line();
|
|
110
|
+
line(" " + c.dim("Ready to chat about it."));
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
info("Nothing changed.");
|
|
114
|
+
}
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
// Case C — a brand-new upload (Uploading/Processing already printed).
|
|
118
|
+
line();
|
|
119
|
+
const yes = await confirmAttach("Attach this file to the current conversation?");
|
|
120
|
+
line();
|
|
121
|
+
if (yes) {
|
|
122
|
+
ctx.attachFile(r.fileId);
|
|
123
|
+
success(`Attached ${r.filename}`);
|
|
124
|
+
line();
|
|
125
|
+
line(" " + c.dim("Ready to chat about it."));
|
|
126
|
+
printTry(false);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
success("Upload complete.");
|
|
130
|
+
line();
|
|
131
|
+
line(" " + c.dim("The file is saved to your uploads. Attach it anytime with:"));
|
|
132
|
+
line(" " + c.cmd(`cos attach "${r.filename}"`));
|
|
133
|
+
line();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* `cos upload <files...>` / `/upload` — upload one or more files and index each.
|
|
138
|
+
* Thin client: `ensureUploaded` does the /v1/files + /process calls; this owns the
|
|
139
|
+
* UX. It NEVER starts a chat or summarizes.
|
|
140
|
+
*
|
|
141
|
+
* • Inside the shell → upload + auto-attach + "Ready." (you're already chatting).
|
|
142
|
+
* • Standalone (single file) → upload, then confirm whether to attach.
|
|
143
|
+
* • Standalone (multiple) → upload + attach all, then show next steps.
|
|
144
|
+
*/
|
|
145
|
+
export function registerUpload(program, ctx) {
|
|
146
|
+
program
|
|
147
|
+
.command("upload")
|
|
148
|
+
.description("Upload one or more files and start processing them")
|
|
149
|
+
.argument("<files...>", "path(s) to upload — multiple files and * globs are supported")
|
|
150
|
+
.option("--no-process", "upload only; skip the processing/indexing step")
|
|
151
|
+
.action(async (files, opts) => {
|
|
152
|
+
// Standalone runs reach the disk without having passed the shell's
|
|
153
|
+
// startup check, so gate them here too. In-shell is already trusted.
|
|
154
|
+
if (!ctx.inShell && !(await ensureWorkspaceTrust(ctx.config))) {
|
|
155
|
+
process.exitCode = 1;
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const client = ctx.requireAuth();
|
|
159
|
+
const paths = expandFiles(files);
|
|
160
|
+
if (paths.length === 0) {
|
|
161
|
+
errorLine("Couldn't find that file. Check the path and try again.");
|
|
162
|
+
process.exitCode = 1;
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
// ── Interactive shell: upload + auto-attach, no prompts ──────────────────
|
|
166
|
+
if (ctx.inShell) {
|
|
167
|
+
let ok = 0;
|
|
168
|
+
for (const fp of paths) {
|
|
169
|
+
try {
|
|
170
|
+
const r = await withSpinner(`Uploading ${path.basename(fp)}…`, () => uploadAndAttach(ctx, client, fp, { process: opts.process }));
|
|
171
|
+
line();
|
|
172
|
+
if (r.status === "uploaded")
|
|
173
|
+
success(`Uploaded ${r.filename}`);
|
|
174
|
+
success(r.wasAttached ? `${r.filename} already attached` : `Attached ${r.filename}`);
|
|
175
|
+
ok += 1;
|
|
176
|
+
}
|
|
177
|
+
catch (err) {
|
|
178
|
+
reportError(err);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (ok > 0) {
|
|
182
|
+
line();
|
|
183
|
+
info("Ready.");
|
|
184
|
+
line();
|
|
185
|
+
}
|
|
186
|
+
if (ok < paths.length)
|
|
187
|
+
process.exitCode = 1;
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
// ── Standalone, single file: confirm attach (Case A/B/C) ─────────────────
|
|
191
|
+
if (paths.length === 1) {
|
|
192
|
+
try {
|
|
193
|
+
await standaloneSingle(ctx, client, paths[0], opts);
|
|
194
|
+
}
|
|
195
|
+
catch (err) {
|
|
196
|
+
reportError(err);
|
|
197
|
+
process.exitCode = 1;
|
|
198
|
+
}
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
// ── Standalone, multiple files: upload + attach all, then next steps ─────
|
|
202
|
+
let ok = 0;
|
|
203
|
+
for (const fp of paths) {
|
|
204
|
+
try {
|
|
205
|
+
const r = await withSpinner(`Uploading ${path.basename(fp)}…`, () => uploadAndAttach(ctx, client, fp, { process: opts.process }));
|
|
206
|
+
line();
|
|
207
|
+
success(r.status === "uploaded" ? `Uploaded ${r.filename}` : `${r.filename} (already uploaded)`);
|
|
208
|
+
ok += 1;
|
|
209
|
+
}
|
|
210
|
+
catch (err) {
|
|
211
|
+
reportError(err);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (ok > 0) {
|
|
215
|
+
line();
|
|
216
|
+
info(`Attached ${ok} file${ok > 1 ? "s" : ""} to this conversation.`);
|
|
217
|
+
printAttachedList(ctx);
|
|
218
|
+
printTry(ok > 1);
|
|
219
|
+
}
|
|
220
|
+
if (ok < paths.length)
|
|
221
|
+
process.exitCode = 1;
|
|
222
|
+
});
|
|
223
|
+
}
|