@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,61 @@
|
|
|
1
|
+
import { line } from "../services/output.js";
|
|
2
|
+
import { activeRepository, inWorkspaceMode, listRepositories, repositoryDetails, getRegistry } from "../services/workspace-registry.js";
|
|
3
|
+
import { c } from "../ui/colors.js";
|
|
4
|
+
import { sectionHeader } from "../ui/ui.js";
|
|
5
|
+
/**
|
|
6
|
+
* `cos repos` — the repositories this workspace knows about.
|
|
7
|
+
*
|
|
8
|
+
* Listing is not access: a repository appears here because it exists next to
|
|
9
|
+
* the others, and each one keeps its own trust state and its own boundary. The
|
|
10
|
+
* marker says which one commands and tools act on right now.
|
|
11
|
+
*/
|
|
12
|
+
export function registerRepos(program, _ctx) {
|
|
13
|
+
program
|
|
14
|
+
.command("repos")
|
|
15
|
+
.description("List the repositories in this workspace")
|
|
16
|
+
.action(() => {
|
|
17
|
+
showRepositories();
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function statusText(handle) {
|
|
21
|
+
const d = repositoryDetails(handle);
|
|
22
|
+
const parts = [];
|
|
23
|
+
if (d.status) {
|
|
24
|
+
parts.push(d.status.dirty
|
|
25
|
+
? [d.status.modified ? `${d.status.modified} modified` : "", d.status.untracked ? `${d.status.untracked} untracked` : ""]
|
|
26
|
+
.filter(Boolean)
|
|
27
|
+
.join(" · ")
|
|
28
|
+
: "clean");
|
|
29
|
+
}
|
|
30
|
+
else if (handle.git) {
|
|
31
|
+
parts.push("status unavailable");
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
parts.push("not a Git repository");
|
|
35
|
+
}
|
|
36
|
+
if (d.trust !== "trusted")
|
|
37
|
+
parts.push("not trusted");
|
|
38
|
+
return parts.join(" · ");
|
|
39
|
+
}
|
|
40
|
+
export function showRepositories() {
|
|
41
|
+
const reg = getRegistry();
|
|
42
|
+
const active = activeRepository();
|
|
43
|
+
line(sectionHeader(inWorkspaceMode() ? "Workspace" : "Repository"));
|
|
44
|
+
if (reg.workspaceRoot)
|
|
45
|
+
line(" " + c.dim(reg.workspaceRoot));
|
|
46
|
+
line();
|
|
47
|
+
for (const handle of listRepositories()) {
|
|
48
|
+
const isActive = handle.id === active.id;
|
|
49
|
+
const marker = isActive ? c.brand("*") : " ";
|
|
50
|
+
const name = isActive ? c.title(handle.name) : c.value(handle.name);
|
|
51
|
+
line(` ${marker} ${name}`);
|
|
52
|
+
const d = repositoryDetails(handle);
|
|
53
|
+
line(" " + c.dim(`branch ${d.branch ?? "unknown"} · ${statusText(handle)}`));
|
|
54
|
+
line(" " + c.dim(handle.root));
|
|
55
|
+
line();
|
|
56
|
+
}
|
|
57
|
+
if (inWorkspaceMode()) {
|
|
58
|
+
line(" " + c.dim("Switch with ") + c.cmd("/repo use <name>") + c.dim(" — tools and commands follow the active repository."));
|
|
59
|
+
line();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { errorLine } from "../services/output.js";
|
|
2
|
+
import { activeRepository, isWildcardSelector, repositoryTrust, resolveRepository } from "../services/workspace-registry.js";
|
|
3
|
+
import { chatTurn } from "../shell/shell-chat.js";
|
|
4
|
+
import { buildFooter } from "../shell/shell-prompt.js";
|
|
5
|
+
/**
|
|
6
|
+
* `/review` — a read-only code review of one repository's uncommitted changes.
|
|
7
|
+
*
|
|
8
|
+
* This is a normal model turn, deliberately constrained: it is sent with the
|
|
9
|
+
* read tools only (read_file, search_files, git_status, git_diff) and NO
|
|
10
|
+
* mutation or command tools, so a review cannot change files or run commands
|
|
11
|
+
* however the model is prompted. It reuses the existing backend orchestration —
|
|
12
|
+
* there is no second agent loop — and the existing conversation renderer, so
|
|
13
|
+
* git activity and file reads appear as the same compact steps as any turn.
|
|
14
|
+
*
|
|
15
|
+
* "Review and fix" is not one step: this produces findings and stops. Fixing is
|
|
16
|
+
* a separate, explicit request that goes through the ordinary mutation flow
|
|
17
|
+
* (with its diffs and approvals). The two are never combined here.
|
|
18
|
+
*/
|
|
19
|
+
export function registerReview(program, ctx) {
|
|
20
|
+
program
|
|
21
|
+
.command("review")
|
|
22
|
+
.argument("[repository]", "repository to review (defaults to the active one)")
|
|
23
|
+
.description("Review the uncommitted changes in a repository (read-only)")
|
|
24
|
+
.action(async (repository) => {
|
|
25
|
+
const client = ctx.requireAuth();
|
|
26
|
+
await runReview(ctx, client, repository);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Resolve the repository, refuse anything that is not exactly one trusted
|
|
31
|
+
* registered repository, and run the review turn. The active repository is
|
|
32
|
+
* never changed — a review of a sibling reads the sibling and leaves you where
|
|
33
|
+
* you were.
|
|
34
|
+
*/
|
|
35
|
+
export async function runReview(ctx, client, repository, footer) {
|
|
36
|
+
if (isWildcardSelector(repository)) {
|
|
37
|
+
errorLine('Review is one repository at a time — "*" is not accepted. Name a repository, or omit it for the active one.');
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
let handle;
|
|
41
|
+
try {
|
|
42
|
+
handle = repository ? resolveRepository(repository) : activeRepository();
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
errorLine(err.message);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (repositoryTrust(handle) !== "trusted") {
|
|
49
|
+
errorLine(`${handle.name} is not a trusted repository, so it cannot be reviewed. Run /repo use ${handle.name} and confirm the trust prompt first.`);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const display = repository
|
|
53
|
+
? `Review the changes in ${handle.name}.`
|
|
54
|
+
: "Review the current changes.";
|
|
55
|
+
await chatTurn(ctx, client, display, {
|
|
56
|
+
footer: footer ?? (process.stdin.isTTY ? buildFooter(ctx) : undefined),
|
|
57
|
+
readOnly: true,
|
|
58
|
+
webSearch: false,
|
|
59
|
+
sendAs: reviewPrompt(handle.name)
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The task framing sent to the model. It names the repository, drives the
|
|
64
|
+
* git_status → git_diff → read_file flow, fixes the output shape, and is
|
|
65
|
+
* explicit that this is read-only and one repository only. The read-only
|
|
66
|
+
* guarantee does not rest on this text — the turn declares no write tools — but
|
|
67
|
+
* saying so keeps the model from proposing changes it cannot make.
|
|
68
|
+
*/
|
|
69
|
+
function reviewPrompt(repository) {
|
|
70
|
+
return [
|
|
71
|
+
`Review the uncommitted changes in the repository "${repository}". This is a READ-ONLY code review.`,
|
|
72
|
+
"",
|
|
73
|
+
"Do this:",
|
|
74
|
+
`1. Call git_status with repository: "${repository}". If the repository is clean (no staged, unstaged or untracked changes), say so in one line and stop — there is nothing to review.`,
|
|
75
|
+
`2. Otherwise call git_diff with repository: "${repository}" (scope "both" to see staged and unstaged together).`,
|
|
76
|
+
`3. Read only the changed files that need a closer look, and just enough surrounding context to judge a finding — use read_file with repository: "${repository}". Do not read the whole project.`,
|
|
77
|
+
"4. Analyse the changes for concrete problems: correctness bugs, regressions, broken edge cases, security issues, unsafe assumptions, missing error handling or validation, race conditions, resource leaks, incorrect API use, and material test-coverage gaps.",
|
|
78
|
+
"",
|
|
79
|
+
"Rules:",
|
|
80
|
+
"- Report only real issues you can point to in the diff or the files you read. A clean review is a valid result — do not invent findings.",
|
|
81
|
+
"- Do not invent line numbers. Cite a file and, where you can map it precisely, a line or range; if you cannot map it exactly, say which file and describe the location without a fabricated number.",
|
|
82
|
+
"- This is read-only. Do not propose or make any file change, and do not run any command. If asked to fix afterwards, the user will make that request separately.",
|
|
83
|
+
"- Review only this one repository.",
|
|
84
|
+
"",
|
|
85
|
+
"Output format:",
|
|
86
|
+
`Start with a one-line header naming the repository. Then, under a "### Findings" heading, one entry per finding as:`,
|
|
87
|
+
"**<Severity> — `<file>:<line or range>`** then a one-line title, then a short \"Why:\" explanation, then a \"Suggested fix:\" line.",
|
|
88
|
+
"Severity is one of critical, high, medium, low, info. Order findings most severe first.",
|
|
89
|
+
'End with a "### Summary" section: the count by severity, and the line "No files were modified."',
|
|
90
|
+
'If there are no findings, replace both sections with "### Review complete" and one line saying no concrete issues were found in the current changes.'
|
|
91
|
+
].join("\n");
|
|
92
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { select } from "@inquirer/prompts";
|
|
2
|
+
import { DEFAULT_API_URL } from "../services/config.js";
|
|
3
|
+
import { listCliProjects, listCliWorkspaces, setCliScope } from "../services/device-auth.js";
|
|
4
|
+
import { errorLine, heading, info, kv, line, success } from "../services/output.js";
|
|
5
|
+
import { withSpinner } from "../services/spinner.js";
|
|
6
|
+
import { screenActive } from "../ui/screen.js";
|
|
7
|
+
/**
|
|
8
|
+
* `cos workspace use [target]` / `cos project use [target]` — switch the active
|
|
9
|
+
* developer context. The chosen workspace + project are re-bound server-side
|
|
10
|
+
* (which rotates the session), so every subsequent request carries the new
|
|
11
|
+
* scope. `target` may be an id or a name; omit it for an interactive picker.
|
|
12
|
+
*/
|
|
13
|
+
export function registerScope(program, ctx) {
|
|
14
|
+
const workspace = program.command("workspace").description("Advanced: manage API workspace scope (not local repositories)");
|
|
15
|
+
workspace
|
|
16
|
+
.command("list")
|
|
17
|
+
.description("List your workspaces")
|
|
18
|
+
.action(async () => {
|
|
19
|
+
const { apiUrl, provider } = auth(ctx);
|
|
20
|
+
const { workspaces } = await withSpinner("Loading workspaces…", () => listCliWorkspaces(apiUrl, provider));
|
|
21
|
+
printList("Workspaces", workspaces, activeWorkspaceId(ctx), (w) => `${w.name} ${dim(w.slug)}`);
|
|
22
|
+
});
|
|
23
|
+
workspace
|
|
24
|
+
.command("use [target]")
|
|
25
|
+
.description("Switch API workspace and automatically select its default project")
|
|
26
|
+
.action(async (target) => {
|
|
27
|
+
await useWorkspace(ctx, target);
|
|
28
|
+
});
|
|
29
|
+
const project = program.command("project").description("Advanced: manage API project scope (not local repositories)");
|
|
30
|
+
project
|
|
31
|
+
.command("list")
|
|
32
|
+
.description("List projects in the active workspace")
|
|
33
|
+
.action(async () => {
|
|
34
|
+
const { apiUrl, provider } = auth(ctx);
|
|
35
|
+
const wsId = requireActiveWorkspace(ctx);
|
|
36
|
+
const { projects } = await withSpinner("Loading projects…", () => listCliProjects(apiUrl, provider, wsId));
|
|
37
|
+
printList("Projects", projects, activeProjectId(ctx), (p) => `${p.name} ${dim(p.environment)}`);
|
|
38
|
+
});
|
|
39
|
+
project
|
|
40
|
+
.command("use [target]")
|
|
41
|
+
.description("Switch the active project within the current workspace")
|
|
42
|
+
.action(async (target) => {
|
|
43
|
+
await useProject(ctx, target);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
// ── flows ─────────────────────────────────────────────────────────────────────
|
|
47
|
+
async function useWorkspace(ctx, target) {
|
|
48
|
+
const { apiUrl, provider } = auth(ctx);
|
|
49
|
+
const { workspaces } = await withSpinner("Loading workspaces…", () => listCliWorkspaces(apiUrl, provider));
|
|
50
|
+
const ws = await resolve(workspaces, target, "Select a workspace:", (w) => `${w.name} (${w.slug})`);
|
|
51
|
+
if (!ws)
|
|
52
|
+
return unresolved("workspace", workspaces, activeWorkspaceId(ctx), (w) => `${w.name} ${dim(w.slug)}`);
|
|
53
|
+
// A workspace switch needs a project in that workspace — pick its default, or
|
|
54
|
+
// use the server-provided default ordering without a second prompt.
|
|
55
|
+
const { projects } = await withSpinner("Loading projects…", () => listCliProjects(apiUrl, provider, ws.id));
|
|
56
|
+
if (projects.length === 0) {
|
|
57
|
+
errorLine(`Workspace "${ws.name}" has no projects. Create one in the dashboard first.`);
|
|
58
|
+
process.exitCode = 1;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
// The API lists the default project first. Keep a valid current selection
|
|
62
|
+
// when reselecting the same workspace; otherwise use that server ordering.
|
|
63
|
+
const proj = projects.find((p) => p.id === activeProjectId(ctx)) ?? projects[0];
|
|
64
|
+
await applyScope(ctx, apiUrl, provider, ws.id, proj.id);
|
|
65
|
+
}
|
|
66
|
+
async function useProject(ctx, target) {
|
|
67
|
+
const { apiUrl, provider } = auth(ctx);
|
|
68
|
+
const wsId = requireActiveWorkspace(ctx);
|
|
69
|
+
const { projects } = await withSpinner("Loading projects…", () => listCliProjects(apiUrl, provider, wsId));
|
|
70
|
+
const proj = await resolve(projects, target, "Select a project:", (p) => `${p.name} (${p.environment})`);
|
|
71
|
+
if (!proj)
|
|
72
|
+
return unresolved("project", projects, activeProjectId(ctx), (p) => `${p.name} ${dim(p.environment)}`);
|
|
73
|
+
await applyScope(ctx, apiUrl, provider, wsId, proj.id);
|
|
74
|
+
}
|
|
75
|
+
async function applyScope(ctx, apiUrl, provider, workspaceId, projectId) {
|
|
76
|
+
const tokens = await withSpinner("Switching context…", () => setCliScope(apiUrl, provider, workspaceId, projectId));
|
|
77
|
+
persist(ctx, tokens);
|
|
78
|
+
success("Context updated.");
|
|
79
|
+
line();
|
|
80
|
+
kv("Workspace", tokens.workspace?.name ?? "—");
|
|
81
|
+
kv("Project", tokens.project?.name ?? "—");
|
|
82
|
+
line();
|
|
83
|
+
}
|
|
84
|
+
// ── helpers ───────────────────────────────────────────────────────────────────
|
|
85
|
+
function auth(ctx) {
|
|
86
|
+
ctx.requireAuth();
|
|
87
|
+
return { apiUrl: ctx.config.get("apiUrl") ?? DEFAULT_API_URL, provider: ctx.authProvider() };
|
|
88
|
+
}
|
|
89
|
+
/** Resolve a target (id or name) or prompt interactively; returns undefined when
|
|
90
|
+
* no match and no picker is available (caller prints the list). */
|
|
91
|
+
async function resolve(items, target, message, labelFn) {
|
|
92
|
+
if (target) {
|
|
93
|
+
const q = target.trim().toLowerCase();
|
|
94
|
+
return items.find((i) => i.id.toLowerCase() === q || i.name.toLowerCase() === q);
|
|
95
|
+
}
|
|
96
|
+
// Interactive select only when we own a real TTY (not inside the shell's TUI).
|
|
97
|
+
if (!process.stdout.isTTY || screenActive())
|
|
98
|
+
return undefined;
|
|
99
|
+
if (items.length === 0)
|
|
100
|
+
return undefined;
|
|
101
|
+
if (items.length === 1)
|
|
102
|
+
return items[0];
|
|
103
|
+
const id = await select({
|
|
104
|
+
message,
|
|
105
|
+
choices: items.map((i) => ({ name: labelFn(i), value: i.id }))
|
|
106
|
+
});
|
|
107
|
+
return items.find((i) => i.id === id);
|
|
108
|
+
}
|
|
109
|
+
function unresolved(kind, items, activeId, labelFn) {
|
|
110
|
+
errorLine(`No matching ${kind}. Available ${kind}s:`);
|
|
111
|
+
printList(cap(kind) + "s", items, activeId, labelFn);
|
|
112
|
+
info(`Run \`cos ${kind} use <name-or-id>\` to switch.`);
|
|
113
|
+
process.exitCode = 1;
|
|
114
|
+
}
|
|
115
|
+
function persist(ctx, tokens) {
|
|
116
|
+
const current = ctx.config.get("auth");
|
|
117
|
+
if (current) {
|
|
118
|
+
ctx.config.update({
|
|
119
|
+
auth: {
|
|
120
|
+
...current,
|
|
121
|
+
accessToken: tokens.access_token,
|
|
122
|
+
refreshToken: tokens.refresh_token,
|
|
123
|
+
workspace: tokens.workspace ?? current.workspace,
|
|
124
|
+
project: tokens.project ?? current.project
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const user = ctx.config.get("scope")?.user ?? null;
|
|
129
|
+
ctx.config.set("scope", {
|
|
130
|
+
apiKey: null,
|
|
131
|
+
workspace: tokens.workspace ?? { id: "", name: "—", slug: "" },
|
|
132
|
+
project: tokens.project ?? { id: "", name: "—", projectId: "", environment: "" },
|
|
133
|
+
user
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
function printList(title, items, activeId, labelFn) {
|
|
137
|
+
heading(title);
|
|
138
|
+
for (const it of items) {
|
|
139
|
+
const marker = it.id === activeId ? "● " : " ";
|
|
140
|
+
line(` ${marker}${labelFn(it)}`);
|
|
141
|
+
}
|
|
142
|
+
line();
|
|
143
|
+
}
|
|
144
|
+
function requireActiveWorkspace(ctx) {
|
|
145
|
+
const wsId = activeWorkspaceId(ctx);
|
|
146
|
+
if (!wsId) {
|
|
147
|
+
errorLine("No active workspace. Run `cos workspace use` first.");
|
|
148
|
+
throw new Error("no active workspace");
|
|
149
|
+
}
|
|
150
|
+
return wsId;
|
|
151
|
+
}
|
|
152
|
+
const activeWorkspaceId = (ctx) => ctx.config.get("auth")?.workspace?.id;
|
|
153
|
+
const activeProjectId = (ctx) => ctx.config.get("auth")?.project?.id;
|
|
154
|
+
const dim = (s) => `· ${s}`;
|
|
155
|
+
const cap = (s) => (s ? s[0].toUpperCase() + s.slice(1) : s);
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { errorLine, line } from "../services/output.js";
|
|
2
|
+
import { searchFiles } from "../services/local-files.js";
|
|
3
|
+
import { discoverRootWithoutGit, getWorkspace } from "../services/workspace.js";
|
|
4
|
+
import { c } from "../ui/colors.js";
|
|
5
|
+
import { watchForInterrupt } from "../ui/interrupt.js";
|
|
6
|
+
import { ensureWorkspaceTrust } from "../ui/trust.js";
|
|
7
|
+
import { sectionHeader } from "../ui/ui.js";
|
|
8
|
+
/**
|
|
9
|
+
* `cos search <query>` — plain text search across the local project.
|
|
10
|
+
*
|
|
11
|
+
* Local and literal. This is the Grep half of the CLI, not the semantic half:
|
|
12
|
+
* it looks for the characters you typed in the files on your disk, nothing is
|
|
13
|
+
* uploaded, and no embedding is involved. Semantic search over a project stays
|
|
14
|
+
* a backend capability reached through chat.
|
|
15
|
+
*/
|
|
16
|
+
export function registerSearch(program, ctx) {
|
|
17
|
+
program
|
|
18
|
+
.command("search")
|
|
19
|
+
.argument("<query...>", "text to look for")
|
|
20
|
+
.option("-g, --glob <pattern>", "only search files matching a glob")
|
|
21
|
+
.option("-c, --case-sensitive", "match case exactly")
|
|
22
|
+
.option("-n, --limit <n>", "maximum matches to show", "50")
|
|
23
|
+
.description("Search the local project for text")
|
|
24
|
+
.action(async (queryParts, options) => {
|
|
25
|
+
await runSearch(ctx, queryParts.join(" "), {
|
|
26
|
+
glob: options.glob,
|
|
27
|
+
caseSensitive: Boolean(options.caseSensitive),
|
|
28
|
+
limit: Number.parseInt(options.limit, 10) || 50
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export async function runSearch(ctx, query, opts = {}) {
|
|
33
|
+
if (!query.trim()) {
|
|
34
|
+
errorLine("Usage: /search <text>");
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
// Reading the project is exactly what the folder-trust gate covers, and the
|
|
38
|
+
// scope that matters is the workspace ROOT — that is what the search walks,
|
|
39
|
+
// not just the directory you happen to be standing in.
|
|
40
|
+
if (!(await ensureWorkspaceTrust(ctx.config, discoverRootWithoutGit())))
|
|
41
|
+
return;
|
|
42
|
+
const ws = getWorkspace();
|
|
43
|
+
const label = ws.project.name;
|
|
44
|
+
// Ctrl+C during a long walk. watchForInterrupt is the existing mechanism and
|
|
45
|
+
// attaches only for the duration, so the composer keeps sole ownership of
|
|
46
|
+
// stdin either side of this.
|
|
47
|
+
const controller = new AbortController();
|
|
48
|
+
const stopWatching = watchForInterrupt(() => controller.abort());
|
|
49
|
+
line();
|
|
50
|
+
line(" " + c.dim(`Searching ${label}…`));
|
|
51
|
+
let result;
|
|
52
|
+
try {
|
|
53
|
+
result = searchFiles({
|
|
54
|
+
query,
|
|
55
|
+
glob: opts.glob,
|
|
56
|
+
caseSensitive: opts.caseSensitive,
|
|
57
|
+
limit: opts.limit ?? 50,
|
|
58
|
+
signal: controller.signal
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
stopWatching();
|
|
63
|
+
}
|
|
64
|
+
if (result.matches.length === 0) {
|
|
65
|
+
line();
|
|
66
|
+
line(" " + c.dim(result.aborted ? "Search cancelled." : "No matches found."));
|
|
67
|
+
line();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const count = result.matches.length;
|
|
71
|
+
line();
|
|
72
|
+
line(sectionHeader(`${count} ${count === 1 ? "match" : "matches"} for "${query}"`));
|
|
73
|
+
const width = Math.max(40, (process.stdout.columns ?? 80) - 8);
|
|
74
|
+
for (const m of result.matches) {
|
|
75
|
+
line(" " + c.value(m.path) + c.dim(":") + c.accent(String(m.line)));
|
|
76
|
+
if (m.text)
|
|
77
|
+
line(" " + c.dim(clip(m.text, width)));
|
|
78
|
+
}
|
|
79
|
+
if (result.aborted) {
|
|
80
|
+
line();
|
|
81
|
+
line(" " + c.dim("Cancelled — showing what was found so far."));
|
|
82
|
+
}
|
|
83
|
+
else if (result.truncated) {
|
|
84
|
+
line();
|
|
85
|
+
line(" " + c.dim(`Stopped at ${count}. Narrow it with `) + c.cmd("--glob") + c.dim("."));
|
|
86
|
+
}
|
|
87
|
+
line();
|
|
88
|
+
}
|
|
89
|
+
/** Keep a matching line to one row. */
|
|
90
|
+
function clip(s, max) {
|
|
91
|
+
return [...s].length <= max ? s : [...s].slice(0, max - 1).join("") + "…";
|
|
92
|
+
}
|