@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,104 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { isInside } from "./local-files.js";
|
|
4
|
+
import { repositoryForPath } from "./workspace-registry.js";
|
|
5
|
+
/** Split input into tokens, honoring double quotes so paths with spaces work. */
|
|
6
|
+
function tokenize(input) {
|
|
7
|
+
const tokens = [];
|
|
8
|
+
const re = /"([^"]*)"|(\S+)/g;
|
|
9
|
+
let m;
|
|
10
|
+
while ((m = re.exec(input)) !== null) {
|
|
11
|
+
if (m[1] !== undefined)
|
|
12
|
+
tokens.push({ value: m[1], quoted: true });
|
|
13
|
+
else
|
|
14
|
+
tokens.push({ value: m[2], quoted: false });
|
|
15
|
+
}
|
|
16
|
+
return tokens;
|
|
17
|
+
}
|
|
18
|
+
/** A token worth checking as a path: has a separator or a file extension. */
|
|
19
|
+
function looksLikePath(s) {
|
|
20
|
+
return /[\\/]/.test(s) || /\.[a-z0-9]{1,8}$/i.test(s);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Resolve a token to a file this workspace may attach.
|
|
24
|
+
*
|
|
25
|
+
* Attaching UPLOADS the file, so auto-detection has to obey the same boundary
|
|
26
|
+
* as every other local read. It used to accept any existing path at all — so
|
|
27
|
+
* `"C:\Users\me\.ssh\id_rsa" explain this`, typed from inside a project, would
|
|
28
|
+
* have sent that key to the server with nothing but the folder-trust prompt for
|
|
29
|
+
* the directory the shell started in standing in the way.
|
|
30
|
+
*
|
|
31
|
+
* Two checks, as in local-files.ts: the resolved path must be inside the
|
|
32
|
+
* workspace root, and so must its realpath — which is what stops a symlink or
|
|
33
|
+
* junction inside the project from pointing the upload somewhere else.
|
|
34
|
+
*/
|
|
35
|
+
function resolveToken(token) {
|
|
36
|
+
try {
|
|
37
|
+
const abs = path.resolve(token);
|
|
38
|
+
if (!(fs.existsSync(abs) && fs.statSync(abs).isFile()))
|
|
39
|
+
return { kind: "none" };
|
|
40
|
+
// A workspace holds several repositories, so "inside the workspace" means
|
|
41
|
+
// inside ONE OF THEM — never the folder that contains them. A file in no
|
|
42
|
+
// registered repository is still outside, exactly as before.
|
|
43
|
+
const owner = repositoryForPath(abs);
|
|
44
|
+
if (!owner)
|
|
45
|
+
return { kind: "outside", abs };
|
|
46
|
+
const real = fs.realpathSync.native(abs);
|
|
47
|
+
// The realpath must belong to the SAME repository: a junction inside repo A
|
|
48
|
+
// pointing at repo B is not a file of A, and following it would attach one
|
|
49
|
+
// repository's file under another's name.
|
|
50
|
+
const realOwner = repositoryForPath(real);
|
|
51
|
+
if (!realOwner || realOwner.id !== owner.id || !isInside(owner.root, real))
|
|
52
|
+
return { kind: "outside", abs };
|
|
53
|
+
return { kind: "file", abs, repository: owner.name };
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return { kind: "none" }; // invalid path — not a file
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Detect local file path(s) at the START of the message (a leading run) or in
|
|
61
|
+
* any QUOTED token, and return the message with those tokens stripped. Only the
|
|
62
|
+
* leading run + quoted tokens are considered, so a filename mentioned mid-
|
|
63
|
+
* sentence (e.g. "tell me about report.pdf") is NOT uploaded.
|
|
64
|
+
*
|
|
65
|
+
* "D:\a\report.pdf summarize" → { files: [D:\a\report.pdf], prompt: "summarize" }
|
|
66
|
+
* "report.pdf explain this" → { files: [report.pdf], prompt: "explain this" }
|
|
67
|
+
* "\"./my notes.txt\" summarize"→ { files: [./my notes.txt], prompt: "summarize" }
|
|
68
|
+
* "just a normal question" → { files: [], prompt: "just a normal question" }
|
|
69
|
+
*/
|
|
70
|
+
export function detectLocalFiles(input) {
|
|
71
|
+
const tokens = tokenize(input);
|
|
72
|
+
const files = [];
|
|
73
|
+
const rejected = [];
|
|
74
|
+
const consumed = new Set();
|
|
75
|
+
let inLeadingRun = true;
|
|
76
|
+
tokens.forEach((t, i) => {
|
|
77
|
+
const consider = t.quoted || inLeadingRun;
|
|
78
|
+
let matched = false;
|
|
79
|
+
if (consider && looksLikePath(t.value)) {
|
|
80
|
+
const r = resolveToken(t.value);
|
|
81
|
+
if (r.kind === "file") {
|
|
82
|
+
files.push(r.abs);
|
|
83
|
+
consumed.add(i);
|
|
84
|
+
matched = true;
|
|
85
|
+
}
|
|
86
|
+
else if (r.kind === "outside") {
|
|
87
|
+
// Not attached, and left in the prompt as the text the user typed — the
|
|
88
|
+
// file's CONTENT is what must not leave the machine, not their words.
|
|
89
|
+
rejected.push(r.abs);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// The leading run ends at the first non-quoted token that isn't a file path.
|
|
93
|
+
if (inLeadingRun && !matched && !t.quoted)
|
|
94
|
+
inLeadingRun = false;
|
|
95
|
+
});
|
|
96
|
+
if (files.length === 0)
|
|
97
|
+
return { files: [], prompt: input.trim(), rejected };
|
|
98
|
+
const prompt = tokens
|
|
99
|
+
.filter((_, i) => !consumed.has(i))
|
|
100
|
+
.map((t) => t.value)
|
|
101
|
+
.join(" ")
|
|
102
|
+
.trim();
|
|
103
|
+
return { files, prompt, rejected };
|
|
104
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { LocalFileError, isInside, isPathIgnored, resolveInside, walkRepository } from "./local-files.js";
|
|
4
|
+
import { isSensitiveName } from "./local-tools.js";
|
|
5
|
+
import { activeRepository, isWildcardSelector, listRepositories, repositoryForPath, repositoryTrust, resolveRepository } from "./workspace-registry.js";
|
|
6
|
+
/** Cap on how many repositories/entries a fuzzy search will examine. */
|
|
7
|
+
const MAX_CANDIDATES = 10;
|
|
8
|
+
const posix = (p) => p.split(path.sep).join("/");
|
|
9
|
+
function trustedRepositories() {
|
|
10
|
+
return listRepositories().filter((r) => repositoryTrust(r) === "trusted");
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A resolved entry IF `rel` exists inside `repo` and passes containment. Returns
|
|
14
|
+
* null when it does not exist or is excluded; throws nothing — a bad path is
|
|
15
|
+
* simply "no match here".
|
|
16
|
+
*/
|
|
17
|
+
function tryExact(repo, rel, kind) {
|
|
18
|
+
let abs;
|
|
19
|
+
try {
|
|
20
|
+
abs = resolveInside(rel, repo.root);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return null; // outside the repo, or a link that leaves it
|
|
24
|
+
}
|
|
25
|
+
let st;
|
|
26
|
+
try {
|
|
27
|
+
st = fs.statSync(abs);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const isDir = st.isDirectory();
|
|
33
|
+
if (kind === "file" && isDir)
|
|
34
|
+
return null;
|
|
35
|
+
if (kind === "directory" && !isDir)
|
|
36
|
+
return null;
|
|
37
|
+
const relOut = posix(path.relative(fs.realpathSync.native(repo.root), abs));
|
|
38
|
+
if (!relOut || relOut.startsWith(".."))
|
|
39
|
+
return null;
|
|
40
|
+
if (isPathIgnored(relOut, repo.root))
|
|
41
|
+
return null;
|
|
42
|
+
if (!isDir && isSensitiveName(path.basename(abs)))
|
|
43
|
+
return null;
|
|
44
|
+
return {
|
|
45
|
+
repositoryId: repo.id,
|
|
46
|
+
repositoryName: repo.name,
|
|
47
|
+
relativePath: relOut,
|
|
48
|
+
absolutePath: abs,
|
|
49
|
+
isDirectory: isDir,
|
|
50
|
+
matchType: "exact_repo_relative",
|
|
51
|
+
confidence: "high"
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** The registered repo a reference is qualified with, plus the remaining path. */
|
|
55
|
+
function splitRepoQualified(reference) {
|
|
56
|
+
const norm = reference.replace(/\\/g, "/").replace(/^\.\//, "");
|
|
57
|
+
const slash = norm.indexOf("/");
|
|
58
|
+
if (slash <= 0)
|
|
59
|
+
return null;
|
|
60
|
+
const head = norm.slice(0, slash);
|
|
61
|
+
const rest = norm.slice(slash + 1);
|
|
62
|
+
if (!rest)
|
|
63
|
+
return null;
|
|
64
|
+
const wanted = head.toLowerCase();
|
|
65
|
+
const repo = listRepositories().find((r) => r.id === wanted || r.name.toLowerCase() === wanted);
|
|
66
|
+
return repo ? { repo, rest } : null;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Fuzzy match by basename (a bare name like `user.js` or `hooks`) or by path
|
|
70
|
+
* suffix (`routes/user.js`) across the trusted repositories. Uses the contained
|
|
71
|
+
* walker, so ignore rules and symlink-skipping already apply. Bounded.
|
|
72
|
+
*/
|
|
73
|
+
function searchByName(reference, kind, repos) {
|
|
74
|
+
const norm = reference.replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/+$/, "");
|
|
75
|
+
const wantBasename = norm.split("/").pop() ?? norm;
|
|
76
|
+
const hasSlash = norm.includes("/");
|
|
77
|
+
const out = [];
|
|
78
|
+
for (const repo of repos) {
|
|
79
|
+
const walk = walkRepository({ root: repo.root, includeDirectories: true });
|
|
80
|
+
for (const e of walk.entries) {
|
|
81
|
+
if (out.length >= MAX_CANDIDATES)
|
|
82
|
+
break;
|
|
83
|
+
const isDir = e.type === "dir";
|
|
84
|
+
if (kind === "file" && isDir)
|
|
85
|
+
continue;
|
|
86
|
+
if (kind === "directory" && !isDir)
|
|
87
|
+
continue;
|
|
88
|
+
const base = e.path.split("/").pop() ?? e.path;
|
|
89
|
+
// A path-like reference matches by suffix; a bare name matches basename.
|
|
90
|
+
const matches = hasSlash
|
|
91
|
+
? e.path === norm || e.path.endsWith("/" + norm)
|
|
92
|
+
: base === wantBasename;
|
|
93
|
+
if (!matches)
|
|
94
|
+
continue;
|
|
95
|
+
if (!isDir && isSensitiveName(base))
|
|
96
|
+
continue;
|
|
97
|
+
out.push({
|
|
98
|
+
repositoryId: repo.id,
|
|
99
|
+
repositoryName: repo.name,
|
|
100
|
+
relativePath: e.path,
|
|
101
|
+
absolutePath: path.join(repo.root, e.path),
|
|
102
|
+
isDirectory: isDir,
|
|
103
|
+
matchType: isDir ? "directory" : hasSlash ? "exact_filename" : "basename",
|
|
104
|
+
confidence: "medium"
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (out.length >= MAX_CANDIDATES)
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Resolve one reference. See the module comment for the guarantees. The order
|
|
114
|
+
* is deterministic; a tie among repositories is returned as candidates.
|
|
115
|
+
*/
|
|
116
|
+
export function resolveWorkspaceFile(input) {
|
|
117
|
+
const reference = typeof input.reference === "string" ? input.reference.trim() : "";
|
|
118
|
+
const kind = input.kind ?? "either";
|
|
119
|
+
if (!reference)
|
|
120
|
+
return { status: "error", error: { code: "INVALID_REFERENCE", message: "A file reference is required." } };
|
|
121
|
+
if (isWildcardSelector(reference)) {
|
|
122
|
+
return { status: "error", error: { code: "INVALID_REFERENCE", message: 'A file reference cannot be "*".' } };
|
|
123
|
+
}
|
|
124
|
+
const trusted = trustedRepositories();
|
|
125
|
+
if (trusted.length === 0) {
|
|
126
|
+
return { status: "error", error: { code: "NO_TRUSTED_REPOSITORY", message: "No trusted repository to resolve against. Trust one first." } };
|
|
127
|
+
}
|
|
128
|
+
let active = null;
|
|
129
|
+
try {
|
|
130
|
+
active = input.activeRepository ? resolveRepository(input.activeRepository) : activeRepository();
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
active = activeRepository();
|
|
134
|
+
}
|
|
135
|
+
const isTrusted = (r) => repositoryTrust(r) === "trusted";
|
|
136
|
+
// 1. Repository-qualified: "api.carrieros.ai/src/routes/user.js".
|
|
137
|
+
const qualified = splitRepoQualified(reference);
|
|
138
|
+
if (qualified) {
|
|
139
|
+
if (!isTrusted(qualified.repo)) {
|
|
140
|
+
return { status: "error", error: { code: "REPOSITORY_NOT_TRUSTED", message: `${qualified.repo.name} is not a trusted repository.` } };
|
|
141
|
+
}
|
|
142
|
+
const hit = tryExact(qualified.repo, qualified.rest, kind);
|
|
143
|
+
if (hit)
|
|
144
|
+
return { status: "resolved", match: { ...hit, matchType: "repo_qualified", confidence: "high" } };
|
|
145
|
+
// A named repo whose path is absent is a miss, not a fall-through to fuzzy —
|
|
146
|
+
// the user was specific.
|
|
147
|
+
return { status: "not_found", error: { code: "NOT_FOUND", message: `${qualified.rest} is not in ${qualified.repo.name}.` } };
|
|
148
|
+
}
|
|
149
|
+
// 2. Absolute path that lands inside a registered, trusted repository.
|
|
150
|
+
if (path.isAbsolute(reference) || /^[a-zA-Z]:[\\/]/.test(reference) || reference.startsWith("\\\\")) {
|
|
151
|
+
const repo = repositoryForPath(reference);
|
|
152
|
+
if (!repo) {
|
|
153
|
+
return { status: "error", error: { code: "OUTSIDE_WORKSPACE", message: "That path is outside every registered repository." } };
|
|
154
|
+
}
|
|
155
|
+
if (!isTrusted(repo)) {
|
|
156
|
+
return { status: "error", error: { code: "REPOSITORY_NOT_TRUSTED", message: `${repo.name} is not a trusted repository.` } };
|
|
157
|
+
}
|
|
158
|
+
const rel = path.relative(repo.root, path.resolve(reference));
|
|
159
|
+
const hit = tryExact(repo, rel, kind);
|
|
160
|
+
if (hit)
|
|
161
|
+
return { status: "resolved", match: { ...hit, matchType: "absolute_workspace_path" } };
|
|
162
|
+
return { status: "not_found", error: { code: "NOT_FOUND", message: "No such file in that repository." } };
|
|
163
|
+
}
|
|
164
|
+
// A traversal reference is refused outright — never allowed to climb out.
|
|
165
|
+
if (reference.replace(/\\/g, "/").split("/").includes("..")) {
|
|
166
|
+
return { status: "error", error: { code: "OUTSIDE_WORKSPACE", message: "A reference may not contain '..'." } };
|
|
167
|
+
}
|
|
168
|
+
// 3. Exact path in the active repository.
|
|
169
|
+
if (active && isTrusted(active)) {
|
|
170
|
+
const hit = tryExact(active, reference, kind);
|
|
171
|
+
if (hit)
|
|
172
|
+
return { status: "resolved", match: hit };
|
|
173
|
+
}
|
|
174
|
+
// 4. Exact path in another trusted repository.
|
|
175
|
+
const others = trusted.filter((r) => r.id !== active?.id);
|
|
176
|
+
const exactElsewhere = [];
|
|
177
|
+
for (const repo of others) {
|
|
178
|
+
const hit = tryExact(repo, reference, kind);
|
|
179
|
+
if (hit)
|
|
180
|
+
exactElsewhere.push({ ...hit, matchType: "exact_path_other_repo" });
|
|
181
|
+
}
|
|
182
|
+
if (exactElsewhere.length === 1)
|
|
183
|
+
return { status: "resolved", match: exactElsewhere[0] };
|
|
184
|
+
if (exactElsewhere.length > 1)
|
|
185
|
+
return { status: "ambiguous", candidates: exactElsewhere };
|
|
186
|
+
// 5. Filename / basename / directory match across trusted repos (active first).
|
|
187
|
+
const ordered = active && isTrusted(active) ? [active, ...others] : trusted;
|
|
188
|
+
const found = searchByName(reference, kind, ordered);
|
|
189
|
+
if (found.length === 1)
|
|
190
|
+
return { status: "resolved", match: found[0] };
|
|
191
|
+
if (found.length > 1)
|
|
192
|
+
return { status: "ambiguous", candidates: found };
|
|
193
|
+
return {
|
|
194
|
+
status: "not_found",
|
|
195
|
+
error: { code: "NOT_FOUND", message: `Could not find "${reference}" in any trusted repository.` }
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/** Exported for the tool executor to keep error mapping in one place. */
|
|
199
|
+
export { LocalFileError, isInside };
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { ApiError } from "./api.js";
|
|
4
|
+
const SUPPORTED = "PDF, DOCX, TXT, CSV, JSON, Markdown, Images";
|
|
5
|
+
/** A friendly, already-formatted, user-facing upload error. */
|
|
6
|
+
export class UploadError extends Error {
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "UploadError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** A short human file-type label from the extension (e.g. "PDF", "CSV"). */
|
|
13
|
+
export function fileTypeLabel(name) {
|
|
14
|
+
const ext = path.extname(name).replace(".", "").toUpperCase();
|
|
15
|
+
return ext || "FILE";
|
|
16
|
+
}
|
|
17
|
+
/** Human size — "812 KB" / "2.3 MB". */
|
|
18
|
+
export function formatSize(bytes) {
|
|
19
|
+
if (bytes >= 1024 * 1024)
|
|
20
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
21
|
+
return `${Math.max(1, Math.round(bytes / 1024))} KB`;
|
|
22
|
+
}
|
|
23
|
+
/** Reuse an already-uploaded, unchanged local file (same path + size + mtime). */
|
|
24
|
+
function findDuplicate(ctx, absPath, size, mtime) {
|
|
25
|
+
const uploads = ctx.config.get("uploads") ?? [];
|
|
26
|
+
const hit = uploads.find((u) => u.local_path === absPath && u.size === size && u.mtime === mtime);
|
|
27
|
+
return hit?.file_id ?? null;
|
|
28
|
+
}
|
|
29
|
+
/** Filenames for the currently-attached file ids (for the footer / listings). */
|
|
30
|
+
export function attachedFilenames(ctx) {
|
|
31
|
+
const uploads = ctx.config.get("uploads") ?? [];
|
|
32
|
+
return ctx.attachedFileIds.map((id) => uploads.find((u) => u.file_id === id)?.filename ?? id);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Upload a local file (POST /v1/files) and, unless disabled, kick off indexing
|
|
36
|
+
* (POST /v1/files/:id/process). Reuses the existing file_id if the same unchanged
|
|
37
|
+
* file was already uploaded from this machine (no re-upload). Does NOT attach —
|
|
38
|
+
* the caller decides that (the shell auto-attaches; standalone asks). Throws
|
|
39
|
+
* UploadError with a friendly, ready-to-print message on any failure.
|
|
40
|
+
*
|
|
41
|
+
* Thin client only: no parsing, no indexing — the backend owns all of that.
|
|
42
|
+
*/
|
|
43
|
+
export async function ensureUploaded(ctx, client, filePath, hooks = {}) {
|
|
44
|
+
const absPath = path.resolve(filePath);
|
|
45
|
+
const name = path.basename(absPath);
|
|
46
|
+
let stat;
|
|
47
|
+
try {
|
|
48
|
+
stat = fs.statSync(absPath);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
throw new UploadError(`Couldn't find that file.\n Check the path and try again: ${filePath}`);
|
|
52
|
+
}
|
|
53
|
+
if (stat.isDirectory()) {
|
|
54
|
+
throw new UploadError(`That's a folder, not a file: ${filePath}`);
|
|
55
|
+
}
|
|
56
|
+
// Dedup — reuse the existing upload for the same unchanged file.
|
|
57
|
+
const dupId = findDuplicate(ctx, absPath, stat.size, Math.floor(stat.mtimeMs));
|
|
58
|
+
if (dupId) {
|
|
59
|
+
return {
|
|
60
|
+
fileId: dupId,
|
|
61
|
+
filename: name,
|
|
62
|
+
bytes: stat.size,
|
|
63
|
+
status: "existing",
|
|
64
|
+
wasAttached: ctx.attachedFileIds.includes(dupId)
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
hooks.onUploading?.();
|
|
68
|
+
let uploaded;
|
|
69
|
+
try {
|
|
70
|
+
uploaded = await client.uploadFile(absPath);
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
throw new UploadError(friendlyUploadError(err));
|
|
74
|
+
}
|
|
75
|
+
ctx.config.addUpload({
|
|
76
|
+
file_id: uploaded.file_id,
|
|
77
|
+
filename: uploaded.filename,
|
|
78
|
+
file_type: uploaded.file_type,
|
|
79
|
+
uploaded_at: new Date().toISOString(),
|
|
80
|
+
local_path: absPath,
|
|
81
|
+
size: stat.size,
|
|
82
|
+
mtime: Math.floor(stat.mtimeMs)
|
|
83
|
+
});
|
|
84
|
+
hooks.onUploaded?.(uploaded.filename);
|
|
85
|
+
if (hooks.process !== false) {
|
|
86
|
+
hooks.onProcessing?.();
|
|
87
|
+
try {
|
|
88
|
+
await client.processFile(uploaded.file_id);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
// Indexing is best-effort — the file is still usable once ready.
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
fileId: uploaded.file_id,
|
|
96
|
+
filename: uploaded.filename,
|
|
97
|
+
bytes: stat.size,
|
|
98
|
+
status: "uploaded",
|
|
99
|
+
wasAttached: false
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/** ensureUploaded + attach (the shell/auto flow — attaching is expected there). */
|
|
103
|
+
export async function uploadAndAttach(ctx, client, filePath, hooks = {}) {
|
|
104
|
+
const r = await ensureUploaded(ctx, client, filePath, hooks);
|
|
105
|
+
ctx.attachFile(r.fileId);
|
|
106
|
+
return r;
|
|
107
|
+
}
|
|
108
|
+
/** Map an API/local error to a friendly, actionable message. */
|
|
109
|
+
export function friendlyUploadError(err) {
|
|
110
|
+
if (err instanceof ApiError) {
|
|
111
|
+
switch (err.code) {
|
|
112
|
+
case "UNSUPPORTED_FILE_TYPE":
|
|
113
|
+
return `Carrier OS can't upload this file type.\n Supported: ${SUPPORTED}`;
|
|
114
|
+
case "FILE_TOO_LARGE":
|
|
115
|
+
return "This file exceeds the 25 MB upload limit.";
|
|
116
|
+
case "NO_ACCOUNT":
|
|
117
|
+
return "This account can't upload files — try `cos login` again.";
|
|
118
|
+
default:
|
|
119
|
+
return err.message || "Upload failed.";
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (err?.code === "ENOENT") {
|
|
123
|
+
return "Couldn't find that file. Check the path and try again.";
|
|
124
|
+
}
|
|
125
|
+
return err?.message || "Upload failed.";
|
|
126
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* A folder's canonical identity for trust decisions: its real path (symlinks
|
|
5
|
+
* resolved — macOS /var is /private/var), case-folded on Windows. Trust is about
|
|
6
|
+
* a folder on disk, so two spellings of the same folder must agree and two
|
|
7
|
+
* different folders can never share a key. A path not on disk (yet) keeps its
|
|
8
|
+
* resolved spelling. The ONE comparison every trust check uses.
|
|
9
|
+
*/
|
|
10
|
+
export function folderKey(dir) {
|
|
11
|
+
let resolved = path.resolve(dir);
|
|
12
|
+
try {
|
|
13
|
+
resolved = fs.realpathSync.native(resolved);
|
|
14
|
+
}
|
|
15
|
+
catch { /* not on disk: keep the spelling */ }
|
|
16
|
+
return process.platform === "win32" ? resolved.toLowerCase() : resolved;
|
|
17
|
+
}
|
|
18
|
+
/** Whether `dir` is one of `trusted` (entries saved under another spelling still count). */
|
|
19
|
+
export function isTrustedFolder(trusted, dir) {
|
|
20
|
+
const key = folderKey(dir);
|
|
21
|
+
return trusted.some((entry) => entry === key || folderKey(entry) === key);
|
|
22
|
+
}
|