Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cli-space-refs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@neta-art/cohub-cli": patch
---

Space arguments on `spaces get/setup/update/rename/config/pin/unpin/archive/unarchive`, `search --space-id`, `references aggregate`, `apps authorize --space`, and `run -s` (before or after `run`) accept an ID, `home`, your own slug, or `username/slug`. `cohub -s <space> search` scopes the search. `spaces sessions tail --json` now writes only JSONL to stdout.
13 changes: 8 additions & 5 deletions packages/cli/src/commands/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getCohubContext, HttpError, isUserAppFilter, type CohubHttpClient, type
import type { Command } from "commander";
import { createClient, createClientWithAccessToken } from "../client.js";
import { error, handleHttp, json as outJson, jsonRequested, ok, table } from "../output.js";
import { resolveSpace } from "../space.js";
import { explicitSpaceFlag, failSpaceTarget, resolveSpace, resolveSpaceRef } from "../space.js";
import { downloadApp } from "../app-download.js";
import { getAppByRef, parseAppRef } from "../app-ref.js";
import { checkAppTarget } from "../app-target.js";
Expand Down Expand Up @@ -799,22 +799,25 @@ export function registerApps(program: Command): void {
.command("authorize <app>")
.description("Grant an app scopes as the current user")
.requiredOption("--scope <scope>", "Scope to grant (repeatable)", collectOption, [])
.option("--space <spaceId>", "Target space; defaults to the app's own space")
.option("--space <space>", "Target space (ID, home, slug, or username/slug); defaults to the app's own space")
.option("--extend", "Add scopes without replacing active grants")
.option("--json", "Output as JSON")
.action(async (appRef: string, opts: { scope: string[]; space?: string; extend?: boolean; json?: boolean }) => {
.action(async (appRef: string, opts: { scope: string[]; space?: string; extend?: boolean; json?: boolean }, command: Command) => {
const client = createClient();
try {
// The root `-s, --space` option claims `--space` after the subcommand too; COHUB_SPACE_ID must not apply here.
const spaceRef = explicitSpaceFlag(command);
const spaceId = spaceRef ? await resolveSpaceRef(spaceRef) : undefined;
const detail = await getAppByRef(client, appRef);
const result = await client.apps.authorize(detail.app.id, {
scopes: opts.scope as Permission[],
...(opts.space ? { spaceId: opts.space } : {}),
...(spaceId ? { spaceId } : {}),
...(opts.extend ? { scopeMode: "extend" as const } : {}),
});
if (jsonRequested(opts)) return outJson(result);
ok(`Granted ${result.grant.scopes.join(", ")} on space ${result.grant.spaceId}`);
} catch (e: unknown) {
handleHttp(e);
failSpaceTarget(e);
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export function registerModels(program: Command): void {

Examples:
cohub models ls
cohub models ls --space <spaceId>
cohub models ls --space <space>
cohub models ls --model-type multimodal
cohub models ls --model-type multimodal --category video
cohub models ls --model-type multimodal --json
Expand Down
28 changes: 14 additions & 14 deletions packages/cli/src/commands/references.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
isUuid,
type ReferenceAggregateGroupBy,
type ReferenceDirection,
type ReferenceKind,
type ReferenceQueryableType,
import type {
ReferenceAggregateGroupBy,
ReferenceDirection,
ReferenceKind,
ReferenceQueryableType,
} from "@neta-art/cohub";
import type { Command } from "commander";
import { createClient } from "../client.js";
import { formatLocalDateTime, table, json as outJson, jsonRequested, error, handleHttp, type Row } from "../output.js";
import { failSpaceTarget, resolveSpaceRef } from "../space.js";

const RESOURCE_TYPES = new Set<ReferenceQueryableType>([
"turn",
Expand Down Expand Up @@ -152,7 +152,7 @@ Examples:
references
.command("aggregate")
.description("Grouped reference counts for a space")
.argument("<spaceId>", "Space id")
.argument("<space>", "Space: ID, home, slug, or username/slug")
.option("--group-by <field>", "kind | targetType | target | sourceType | day", "kind")
.option("--kinds <kinds>", "Comma-separated kinds (see list below)")
.option("--days <n>", "Only references seen within the last N days")
Expand All @@ -166,21 +166,21 @@ Notes:
--group-by target: file targets look like {spaceId}:{path}

Examples:
cohub references aggregate <spaceId> --json
cohub references aggregate <spaceId> --group-by targetType
cohub references aggregate <spaceId> --group-by target --kinds agent_tool_file_read --limit 20
cohub references aggregate <spaceId> --group-by day --days 30
cohub references aggregate <space> --json
cohub references aggregate <space> --group-by targetType
cohub references aggregate <space> --group-by target --kinds agent_tool_file_read --limit 20
cohub references aggregate <space> --group-by day --days 30
`)
.action(async (spaceId: string, opts: AggregateOpts) => {
.action(async (space: string, opts: AggregateOpts) => {
const client = createClient();
try {
if (!isUuid(spaceId.trim())) return error("Invalid space id");
const groupBy = (opts.groupBy ?? "kind") as ReferenceAggregateGroupBy;
if (!GROUP_BYS.has(groupBy)) return error(`Invalid group-by: ${opts.groupBy}`);
const kinds = parseKinds(opts.kinds);
const days = opts.days ? clampNumber(opts.days, 30, 1, 365) : undefined;
const limit = clampNumber(opts.limit, 200, 1, 500);
const result = await client.references.aggregate({ spaceId: spaceId.trim(), groupBy, kinds, days, limit });
const spaceId = await resolveSpaceRef(space).catch(failSpaceTarget);
const result = await client.references.aggregate({ spaceId, groupBy, kinds, days, limit });
if (jsonRequested(opts)) return outJson(result);
const rows: Row[] = result.groups.map((g) => ({
group: g.group,
Expand Down
51 changes: 35 additions & 16 deletions packages/cli/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,19 @@ function topLevelRunIndex(argv: string[]): number {
return -1;
}

function parseSpaceId(tokens: string[]): string | undefined {
for (let index = 0; index < tokens.length; index += 1) {
const token = tokens[index] ?? "";
if (token === "-s" || token === "--space") {
const value = tokens[index + 1];
if (!value) return error("Missing space", `${token} requires a value`);
return value.trim() || undefined;
}
if (token.startsWith("--space=")) return token.slice("--space=".length).trim() || undefined;
function parseSpaceOption(tokens: string[], index: number): { value: string; nextIndex: number } | null {
const token = tokens[index] ?? "";
if (token === "-s" || token === "--space") {
const value = tokens[index + 1]?.trim();
if (!value) return error("Missing space", `${token} requires a value`);
return { value, nextIndex: index + 1 };
}
if (token.startsWith("--space=")) {
const value = token.slice("--space=".length).trim();
if (!value) return error("Missing space", "--space requires a value");
return { value, nextIndex: index };
}
return undefined;
return null;
}

export async function parseRunCliOptions(
Expand All @@ -76,12 +78,20 @@ export async function parseRunCliOptions(
const beforeRun = argv.slice(0, runIndex);
const afterRun = argv.slice(runIndex + 1);

let explicitSpaceId: string | undefined;
for (let index = 0; index < beforeRun.length; index += 1) {
index = consumeBaseUrlOption(beforeRun, index) ?? index;
if (beforeRun[index] === "-s" || beforeRun[index] === "--space") index += 1;
const endpointIndex = consumeBaseUrlOption(beforeRun, index);
if (endpointIndex !== null) {
index = endpointIndex;
continue;
}
const spaceOption = parseSpaceOption(beforeRun, index);
if (spaceOption) {
explicitSpaceId = spaceOption.value;
index = spaceOption.nextIndex;
}
}

const explicitSpaceId = parseSpaceId(beforeRun) ?? process.env.COHUB_SPACE_ID?.trim() ?? "";
let json = beforeRun.includes("--json");
let async = false;
let commandOption: string | null = null;
Expand All @@ -96,6 +106,13 @@ export async function parseRunCliOptions(
continue;
}

const spaceOption = parseSpaceOption(afterRun, index);
if (spaceOption) {
explicitSpaceId = spaceOption.value;
index = spaceOption.nextIndex;
continue;
}

if (token === "-h" || token === "--help") {
printRunHelp();
process.exit(0);
Expand Down Expand Up @@ -159,19 +176,21 @@ export function printRunHelp(): void {
process.stdout.write(`
Usage:
cohub [--base-url <url>] [-s <space>] run [options] -- <shell command>
cohub run [-s <space>] [options] -- <shell command>
cohub [-s <space>] run --command <shell command>

Options:
-s, --space <space> Target Space: ID, slug, or username/slug
--base-url <url> API base URL (overrides COHUB_BASE_URL)
-c, --command <command> Shell command to execute in the space workspace
--async Queue the command and return immediately
--json Output as JSON
-h, --help Show this help

Examples:
cohub -s <spaceId> run --command "git status"
cohub -s <spaceId> run --async --command "pnpm test"
cohub -s <spaceId> run -- git status -sb
cohub -s <space> run --command "git status"
cohub -s <space> run --async --command "pnpm test"
cohub -s <space> run -- git status -sb

Notes:
- Without -s or COHUB_SPACE_ID, the command uses the current directory Runtime binding; it never falls back to Home.
Expand Down
36 changes: 17 additions & 19 deletions packages/cli/src/commands/search.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { isUuid, type GlobalSearchResult, type GlobalSearchType } from "@neta-art/cohub";
import type { GlobalSearchResult, GlobalSearchType } from "@neta-art/cohub";
import type { Command } from "commander";
import { createClient } from "../client.js";
import { formatLocalDateTime, table, json as outJson, jsonRequested, error, handleHttp, truncateText, type Row } from "../output.js";
import { formatLocalDateTime, table, json as outJson, jsonRequested, error, truncateText, type Row } from "../output.js";
import { explicitSpaceFlag, failSpaceTarget, resolveSpaceRef } from "../space.js";

const DEFAULT_LIMIT = 20;
const MAX_TITLE_LENGTH = 72;
Expand Down Expand Up @@ -48,13 +49,6 @@ function parseTypes(value: string | undefined): GlobalSearchType[] | undefined {
return [...new Set(types)] as GlobalSearchType[];
}

function parseSearchInput(opts: SearchCliOptions) {
const types = parseTypes(opts.types);
const spaceId = opts.spaceId?.trim();
if (spaceId && !isUuid(spaceId)) throw new Error("Invalid space id");
return { types, spaceId: spaceId || undefined, labelRef: opts.labelRef?.trim() || undefined };
}

function rowsFor(items: GlobalSearchResult[]): Row[] {
return items.map((item) => ({
type: item.type,
Expand All @@ -73,31 +67,35 @@ export function registerSearch(program: Command): void {
.argument("[query]", "Search query")
.option("--limit <n>", "Maximum results, 1-50", String(DEFAULT_LIMIT))
.option("--types <types>", "Comma-separated result types: chat,space,label")
.option("--space-id <id>", "Limit search to a space")
.option("--space-id <space>", "Limit search to a space (ID, home, slug, or username/slug)")
.option("--label-ref <ref>", "Search items under an exact label ref")
.option("--json", "Output as JSON")
.addHelpText("after", `

Examples:
cohub search "release notes"
cohub search "failing tests" --limit 10
cohub search "bug" --types chat --space-id <spaceId>
cohub search "bug" --types chat --space-id <space>
cohub -s <space> search "bug" --types chat
cohub search --types label --label-ref bug
cohub search "login" --types label --label-ref bug
cohub search "design review" --json
`)
.action(async (query: string | undefined, opts: SearchCliOptions) => {
.action(async (query: string | undefined, opts: SearchCliOptions, command: Command) => {
const client = createClient();
try {
const input = parseSearchInput(opts);
const types = parseTypes(opts.types);
const labelRef = opts.labelRef?.trim() || undefined;
const q = query ?? "";
if (!q.trim() && !input.labelRef) return error("Search query is required unless --label-ref is provided.");
if (!q.trim() && !labelRef) return error("Search query is required unless --label-ref is provided.");
const spaceRef = opts.spaceId?.trim() || explicitSpaceFlag(command);
const spaceId = spaceRef ? await resolveSpaceRef(spaceRef) : undefined;
const result = await client.search.query({
q,
limit: clampLimit(opts.limit),
types: input.types,
spaceId: input.spaceId,
labelRef: input.labelRef,
types,
spaceId,
labelRef,
});
if (jsonRequested(opts)) return outJson(result);

Expand All @@ -114,10 +112,10 @@ Examples:
{ key: "href", label: "Href" },
]);
} catch (e: unknown) {
if (e instanceof Error && (e.message === "Invalid space id" || e.message.startsWith("Invalid search type:"))) {
if (e instanceof Error && e.message.startsWith("Invalid search type:")) {
return error(e.message);
}
handleHttp(e);
failSpaceTarget(e);
}
});
}
Loading
Loading