@shyftai/ranq 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +74 -2
- package/dist/bin/ranq-client.mjs +8 -0
- package/dist/bin/ranq-mcp.mjs +40 -0
- package/dist/src/cli/api-client.js +45 -0
- package/dist/src/cli/api-tool.js +123 -0
- package/dist/src/cli/argv.js +39 -0
- package/dist/src/cli/client-main.js +51 -0
- package/dist/src/cli/control.js +30 -0
- package/dist/src/cli/table.js +56 -0
- package/dist/src/client/client.js +227 -0
- package/dist/src/client/rest.js +16 -0
- package/dist/src/mcp/server.js +154 -0
- package/dist/src/mcp/tools.js +832 -0
- package/dist/src/version.js +2 -0
- package/package.json +24 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shyft AI LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,75 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @shyftai/ranq
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The command line and MCP client for the ranq API. Your agent uses it to read corpus, presence rate and citation history for your projects.
|
|
4
|
+
|
|
5
|
+
Read more at https://ranq.sh
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
Node 22.18 or later is necessary.
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm i -g @shyftai/ranq
|
|
13
|
+
ranq api list
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
You can also run it without an install.
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npx @shyftai/ranq api list
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Set up
|
|
23
|
+
|
|
24
|
+
Set these variables in your environment.
|
|
25
|
+
|
|
26
|
+
| Variable | Value |
|
|
27
|
+
| --- | --- |
|
|
28
|
+
| `RANQ_API_URL` | `https://api.ranq.sh` |
|
|
29
|
+
| `RANQ_API_KEY` | Your workspace key. |
|
|
30
|
+
| `RANQ_AGENT` | Your agent slug, for example `my-agent`. Write tools need it. |
|
|
31
|
+
|
|
32
|
+
The client reads the key from `RANQ_API_KEY` only. It refuses `--api-key`, because a key on the command line goes into your shell history and the process list.
|
|
33
|
+
|
|
34
|
+
## Use the command line
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
ranq api list
|
|
38
|
+
ranq api get_visibility my-project
|
|
39
|
+
ranq --version
|
|
40
|
+
ranq --help
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`ranq api list` shows each tool and its HTTP method. It does not call the API.
|
|
44
|
+
|
|
45
|
+
Give tool arguments as flags. The output is JSON. Most write tools do a dry run. Add `--execute` to make the change.
|
|
46
|
+
|
|
47
|
+
The public client has only these commands. Other `ranq` commands are operator commands. The client stops with exit code 2 if you use one.
|
|
48
|
+
|
|
49
|
+
## Use the MCP bridge
|
|
50
|
+
|
|
51
|
+
`ranq-mcp` is a stdio MCP server. It sends each tool call to the ranq API with your key.
|
|
52
|
+
|
|
53
|
+
Add it to your MCP client configuration.
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{
|
|
57
|
+
"mcpServers": {
|
|
58
|
+
"ranq": {
|
|
59
|
+
"command": "npx",
|
|
60
|
+
"args": ["-y", "-p", "@shyftai/ranq", "ranq-mcp"],
|
|
61
|
+
"env": {
|
|
62
|
+
"RANQ_API_URL": "https://api.ranq.sh",
|
|
63
|
+
"RANQ_API_KEY": "your-workspace-key",
|
|
64
|
+
"RANQ_AGENT": "my-agent"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
`ranq-mcp` sends the key only over https or to a loopback address.
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
MIT
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The public `ranq` bin (packages/ranq-client): API tools only, no database or operator verbs.
|
|
3
|
+
import { clientMain } from "../src/cli/client-main.js";
|
|
4
|
+
process.exitCode = await clientMain(process.argv.slice(2), {
|
|
5
|
+
out: (line) => process.stdout.write(line + '\n'),
|
|
6
|
+
err: (line) => process.stderr.write(line + '\n'),
|
|
7
|
+
env: process.env,
|
|
8
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The MCP server (RQ2-1, shyftai/ranq#14): a thin stdio client of the REST API started by
|
|
3
|
+
// bin/ranq-api.mjs. Env: RANQ_API_URL (default http://127.0.0.1:8787), RANQ_API_KEY.
|
|
4
|
+
import { agentFromEnv, createMcpServer, runMcpStdio } from "../src/mcp/server.js";
|
|
5
|
+
import { CLIENT_VERSION } from "../src/version.js";
|
|
6
|
+
if (process.argv.includes('--help') || process.argv.includes('--version')) {
|
|
7
|
+
process.stdout.write(process.argv.includes('--version') ? `${CLIENT_VERSION}\n` : `ranq-mcp ${CLIENT_VERSION} is the stdio MCP bridge to the ranq API.
|
|
8
|
+
Set RANQ_API_URL (for example https://api.ranq.sh), RANQ_API_KEY and RANQ_AGENT, then start it from your MCP client.
|
|
9
|
+
It reads JSON-RPC on stdin and writes it on stdout. Read more at https://ranq.sh
|
|
10
|
+
`);
|
|
11
|
+
process.exit(0);
|
|
12
|
+
}
|
|
13
|
+
const apiUrl = process.env['RANQ_API_URL'] ?? 'http://127.0.0.1:8787';
|
|
14
|
+
let parsed;
|
|
15
|
+
try {
|
|
16
|
+
parsed = new URL(apiUrl);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
process.stderr.write(`ranq-mcp refused to start: RANQ_API_URL is not a valid URL\n`);
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
const host = parsed.hostname.replace(/^\[|\]$/g, '');
|
|
23
|
+
const loopback = host === '127.0.0.1' || host === '::1' || host === 'localhost' || host.endsWith('.localhost');
|
|
24
|
+
if (parsed.protocol !== 'https:' && !loopback) {
|
|
25
|
+
process.stderr.write(`ranq-mcp refused to start: RANQ_API_URL must be https or a loopback address, refusing to send the API key unencrypted\n`);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
let agent;
|
|
29
|
+
try {
|
|
30
|
+
agent = agentFromEnv(process.env);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
process.stderr.write(`ranq-mcp refused to start: ${error.message}\n`);
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
runMcpStdio(createMcpServer({
|
|
37
|
+
baseUrl: apiUrl,
|
|
38
|
+
apiKey: process.env['RANQ_API_KEY'] || undefined,
|
|
39
|
+
agent,
|
|
40
|
+
}));
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { stripControl } from "./control.js";
|
|
2
|
+
async function apiClient(flags, io) {
|
|
3
|
+
if (io.openApiClient)
|
|
4
|
+
return io.openApiClient();
|
|
5
|
+
const { RanqClient } = await import("../client/client.js");
|
|
6
|
+
return RanqClient.fromEnv(io.env, {
|
|
7
|
+
baseUrl: typeof flags.get('api-url') === 'string' ? String(flags.get('api-url')) : undefined,
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
export async function withApiClient(flags, io, fn) {
|
|
11
|
+
const { MissingApiConfigError, ApiError, ApiPayloadError, ApiRedirectRefusedError, ApiUnreachableError, InsecureApiUrlError, isHttpRedirectStatus } = await import("../client/client.js");
|
|
12
|
+
try {
|
|
13
|
+
return await fn(await apiClient(flags, io));
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
if (error instanceof MissingApiConfigError) {
|
|
17
|
+
io.err(stripControl(error.message));
|
|
18
|
+
return 2;
|
|
19
|
+
}
|
|
20
|
+
if (error instanceof InsecureApiUrlError) {
|
|
21
|
+
io.err(stripControl(error.message));
|
|
22
|
+
return 2;
|
|
23
|
+
}
|
|
24
|
+
if (error instanceof ApiRedirectRefusedError) {
|
|
25
|
+
if (isHttpRedirectStatus(error.status))
|
|
26
|
+
io.err(stripControl(`the API answered with an HTTP ${error.status} redirect, which ranq refuses to follow: check RANQ_API_URL / --api-url`));
|
|
27
|
+
else
|
|
28
|
+
io.err(stripControl(`the API answered with an unexpected 3xx status (HTTP ${error.status}); nothing was rendered`));
|
|
29
|
+
return 1;
|
|
30
|
+
}
|
|
31
|
+
if (error instanceof ApiPayloadError) {
|
|
32
|
+
io.err('the API returned a malformed or unexpected payload; nothing was rendered');
|
|
33
|
+
return 1;
|
|
34
|
+
}
|
|
35
|
+
if (error instanceof ApiUnreachableError) {
|
|
36
|
+
io.err('could not reach the API: check RANQ_API_URL / --api-url and that the API is running');
|
|
37
|
+
return 1;
|
|
38
|
+
}
|
|
39
|
+
if (error instanceof ApiError) {
|
|
40
|
+
io.err(stripControl(error.message));
|
|
41
|
+
return 1;
|
|
42
|
+
}
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { AGENT_ENV, isValidAgentSlug } from "../mcp/server.js";
|
|
2
|
+
import { TOOLS } from "../mcp/tools.js";
|
|
3
|
+
import { stripControl } from "./control.js";
|
|
4
|
+
import { json } from "./table.js";
|
|
5
|
+
/** Tools the API exposes that stay operator acts on the CLI: refused here with the operator verb. */
|
|
6
|
+
export const OPERATOR_ONLY_TOOLS = {
|
|
7
|
+
send_approved: 'outreach send is an operator act: run "ranq placements <project> outreach send --approval=ID" against the database',
|
|
8
|
+
};
|
|
9
|
+
export function apiToolUsage() {
|
|
10
|
+
return TOOLS.filter((t) => !Object.hasOwn(OPERATOR_ONLY_TOOLS, t.name))
|
|
11
|
+
.map((t) => `${t.method ?? 'GET'}\t${t.name}`)
|
|
12
|
+
.join('\n');
|
|
13
|
+
}
|
|
14
|
+
/** Flags to tool arguments, typed by the tool's inputSchema. Returns an error string on a bad flag. */
|
|
15
|
+
export function toolArgs(tool, positional, flags) {
|
|
16
|
+
const props = (tool.inputSchema.properties ?? {});
|
|
17
|
+
const args = {};
|
|
18
|
+
if (positional.length > 1)
|
|
19
|
+
return `ranq api ${tool.name} takes at most one positional argument (the project)`;
|
|
20
|
+
if (positional[0] !== undefined) {
|
|
21
|
+
if (props['project']?.type !== 'string')
|
|
22
|
+
return `ranq api ${tool.name} takes no project argument: pass its arguments as flags`;
|
|
23
|
+
args['project'] = positional[0];
|
|
24
|
+
}
|
|
25
|
+
for (const [flag, raw] of flags) {
|
|
26
|
+
if (flag === 'json')
|
|
27
|
+
continue; // output is always JSON
|
|
28
|
+
const name = Object.hasOwn(props, flag) ? flag : flag.replaceAll('-', '_');
|
|
29
|
+
const type = Object.hasOwn(props, name) ? props[name].type : undefined;
|
|
30
|
+
if (type === undefined)
|
|
31
|
+
return `unknown flag --${flag} for "ranq api ${tool.name}"`;
|
|
32
|
+
if (Object.hasOwn(args, name))
|
|
33
|
+
return `--${flag} duplicates an argument already given`;
|
|
34
|
+
if (type === 'boolean') {
|
|
35
|
+
if (raw !== true && raw !== 'true' && raw !== 'false')
|
|
36
|
+
return `--${flag} is a boolean`;
|
|
37
|
+
args[name] = raw === true || raw === 'true';
|
|
38
|
+
}
|
|
39
|
+
else if (raw === true) {
|
|
40
|
+
return `--${flag} needs a value`;
|
|
41
|
+
}
|
|
42
|
+
else if (type === 'number') {
|
|
43
|
+
const n = Number(raw);
|
|
44
|
+
if (raw.trim() === '' || !Number.isFinite(n))
|
|
45
|
+
return `--${flag} must be a number`;
|
|
46
|
+
args[name] = n;
|
|
47
|
+
}
|
|
48
|
+
else if (type === 'object' || type === 'array') {
|
|
49
|
+
let parsed;
|
|
50
|
+
try {
|
|
51
|
+
parsed = JSON.parse(raw);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return `--${flag} must be JSON (${type})`;
|
|
55
|
+
}
|
|
56
|
+
if (type === 'array' ? !Array.isArray(parsed) : typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
|
|
57
|
+
return `--${flag} must be a JSON ${type}`;
|
|
58
|
+
}
|
|
59
|
+
args[name] = parsed;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
args[name] = raw;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return args;
|
|
66
|
+
}
|
|
67
|
+
export async function apiTool(positional, flags, io, withClient) {
|
|
68
|
+
const [name, ...rest] = positional;
|
|
69
|
+
if (name === undefined || name === 'list') {
|
|
70
|
+
if (rest.length > 0) {
|
|
71
|
+
io.err('ranq api list takes no arguments');
|
|
72
|
+
return 2;
|
|
73
|
+
}
|
|
74
|
+
const bad = [...flags.keys()].find((f) => f !== 'json');
|
|
75
|
+
if (bad !== undefined) {
|
|
76
|
+
io.err(stripControl(`unknown flag --${bad} for "ranq api list"`));
|
|
77
|
+
return 2;
|
|
78
|
+
}
|
|
79
|
+
io.out(apiToolUsage());
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
if (Object.hasOwn(OPERATOR_ONLY_TOOLS, name)) {
|
|
83
|
+
io.err(`ranq api ${name} is refused: ${OPERATOR_ONLY_TOOLS[name]}`);
|
|
84
|
+
return 2;
|
|
85
|
+
}
|
|
86
|
+
const tool = TOOLS.find((t) => t.name === name);
|
|
87
|
+
if (!tool) {
|
|
88
|
+
io.err(stripControl(`unknown API tool: ${name}`) + '\n\nranq api list shows every tool');
|
|
89
|
+
return 2;
|
|
90
|
+
}
|
|
91
|
+
const args = toolArgs(tool, rest, flags);
|
|
92
|
+
if (typeof args === 'string') {
|
|
93
|
+
io.err(stripControl(args));
|
|
94
|
+
return 2;
|
|
95
|
+
}
|
|
96
|
+
for (const required of tool.inputSchema.required ?? []) {
|
|
97
|
+
if (args[required] === undefined || args[required] === '') {
|
|
98
|
+
io.err(`--${required} is required for "ranq api ${tool.name}"`);
|
|
99
|
+
return 2;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const write = tool.method === 'POST';
|
|
103
|
+
const agent = io.env[AGENT_ENV];
|
|
104
|
+
if (write && !isValidAgentSlug(agent)) {
|
|
105
|
+
io.err(`${AGENT_ENV} must be set to an agent slug ([a-z0-9-]{1,40}) for a write: the API records actor agent:<slug>`);
|
|
106
|
+
return 2;
|
|
107
|
+
}
|
|
108
|
+
let path;
|
|
109
|
+
let body;
|
|
110
|
+
try {
|
|
111
|
+
path = tool.path(args);
|
|
112
|
+
body = tool.body ? tool.body(args) : undefined;
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
io.err(stripControl(error instanceof Error ? error.message : String(error)));
|
|
116
|
+
return 2;
|
|
117
|
+
}
|
|
118
|
+
return withClient(async (client) => {
|
|
119
|
+
const data = await client.call(write ? 'POST' : 'GET', path, { body, headers: write ? { 'x-ranq-agent': agent } : {} });
|
|
120
|
+
io.out(json(data));
|
|
121
|
+
return 0;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const BOOLEAN_FLAGS = new Set(['json', 'active', 'execute', 'read', 'supersede', 'check', 'arm', 'markdown', 'help', 'draft', 'dry-run', 'api', 'enabled', 'version']);
|
|
2
|
+
export function parse(argv) {
|
|
3
|
+
const positional = [];
|
|
4
|
+
const flags = new Map();
|
|
5
|
+
for (let i = 0; i < argv.length; i++) {
|
|
6
|
+
const arg = argv[i];
|
|
7
|
+
if (arg.startsWith('--')) {
|
|
8
|
+
const repeatable = (name, value) => {
|
|
9
|
+
const prev = flags.get(name);
|
|
10
|
+
flags.set(name, typeof prev === 'string' ? `${prev},${value}` : value);
|
|
11
|
+
};
|
|
12
|
+
if (arg.includes('=')) {
|
|
13
|
+
const [name, ...rest] = arg.slice(2).split('=');
|
|
14
|
+
const value = rest.join('=');
|
|
15
|
+
if (name === 'market' || name === 'region')
|
|
16
|
+
repeatable(name, value);
|
|
17
|
+
else
|
|
18
|
+
flags.set(name ?? '', value);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
const name = arg.slice(2);
|
|
22
|
+
if (!BOOLEAN_FLAGS.has(name) && i + 1 < argv.length && !argv[i + 1].startsWith('--')) {
|
|
23
|
+
const value = argv[++i];
|
|
24
|
+
if (name === 'market' || name === 'region')
|
|
25
|
+
repeatable(name, value);
|
|
26
|
+
else
|
|
27
|
+
flags.set(name, value);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
flags.set(name, true);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
positional.push(arg);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return { command: positional[0], positional: positional.slice(1), flags };
|
|
39
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The public `ranq` client (packages/ranq-client, shyftai/ranq#313): `ranq api <tool>`, `ranq api list`,
|
|
3
|
+
* `--version` and `--help` only. Every other verb is an operator command that needs the database and
|
|
4
|
+
* stays in the private checkout (main.ts). This module must not import main.ts or anything server-side:
|
|
5
|
+
* scripts/ci/check-client-pack.mjs fails the tarball if it does.
|
|
6
|
+
*/
|
|
7
|
+
import { apiTool } from "./api-tool.js";
|
|
8
|
+
import { withApiClient } from "./api-client.js";
|
|
9
|
+
import { parse } from "./argv.js";
|
|
10
|
+
import { stripControl } from "./control.js";
|
|
11
|
+
import { CLIENT_VERSION } from "../version.js";
|
|
12
|
+
export const CLIENT_USAGE = `usage
|
|
13
|
+
ranq api list List the API tools.
|
|
14
|
+
ranq api <tool> [project] [--flag value ...]
|
|
15
|
+
Call one API tool. The output is JSON.
|
|
16
|
+
ranq --version Show the client version.
|
|
17
|
+
ranq --help Show this help.
|
|
18
|
+
|
|
19
|
+
Environment
|
|
20
|
+
RANQ_API_URL The API address, for example https://api.ranq.sh
|
|
21
|
+
RANQ_API_KEY Your workspace key. The client does not accept a key as a flag.
|
|
22
|
+
RANQ_AGENT Your agent slug. Write tools need it.
|
|
23
|
+
|
|
24
|
+
Most write tools do a dry run. Add --execute to make the change.
|
|
25
|
+
For agents, ranq-mcp is the stdio MCP bridge. Read more at https://ranq.sh`;
|
|
26
|
+
export async function clientMain(argv, io) {
|
|
27
|
+
const { command, positional, flags } = parse(argv);
|
|
28
|
+
// Same refusal as the operator CLI (#367): a key on argv lands in shell history and `ps`. First, so no other exit skips it.
|
|
29
|
+
if (flags.has('api-key')) {
|
|
30
|
+
io.err('--api-key is not accepted: a key on the command line leaks into shell history and the process list. Set RANQ_API_KEY in the environment instead');
|
|
31
|
+
return 2;
|
|
32
|
+
}
|
|
33
|
+
if (flags.has('help') || command === 'help') {
|
|
34
|
+
io.out(CLIENT_USAGE);
|
|
35
|
+
return 0;
|
|
36
|
+
}
|
|
37
|
+
// Only a bare `ranq --version`; with a verb, a value or another flag it falls through and exits 2.
|
|
38
|
+
if (flags.get('version') === true && flags.size === 1 && command === undefined) {
|
|
39
|
+
io.out(CLIENT_VERSION);
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
if (command === undefined) {
|
|
43
|
+
io.err(CLIENT_USAGE);
|
|
44
|
+
return 2;
|
|
45
|
+
}
|
|
46
|
+
if (command !== 'api') {
|
|
47
|
+
io.err(stripControl(`ranq ${command} is an operator command and is not in the public client.`) + ' Run "ranq api list" to see the tools this client can call.');
|
|
48
|
+
return 2;
|
|
49
|
+
}
|
|
50
|
+
return apiTool(positional, flags, io, (fn) => withApiClient(new Map(), io, fn));
|
|
51
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Caller-supplied text is stripped of its controls and invisibles before it reaches a table
|
|
3
|
+
* cell or a message: the output gets piped and an escape sequence can rewrite the terminal or
|
|
4
|
+
* hide what the report says, while a bidi override or isolate can reorder what the reader sees
|
|
5
|
+
* and a line separator breaks the row. This is the one strip, shared by `table()` (every header
|
|
6
|
+
* and every cell of every table the CLI prints: `ranq corpus-drift`, `ranq mention-diff`,
|
|
7
|
+
* `ranq placements-seed`, `ranq competitor-gap` and the rest) and by every read, parse or
|
|
8
|
+
* refusal message that quotes a path, a file's content or a parser's message for a file the
|
|
9
|
+
* user passed in (`ranq corpus-drift`, `ranq mention-diff`, `ranq placements-seed`,
|
|
10
|
+
* `ranq competitor-gap`). Dropped from that text in full are the C0 range, DEL and the C1 range
|
|
11
|
+
* (ESC among them), the soft hyphen U+00AD, the combining grapheme joiner U+034F, the Arabic
|
|
12
|
+
* letter mark U+061C, the Mongolian vowel separator U+180E, the zero-width spaces and joiners
|
|
13
|
+
* and the marks U+200B to U+200F, the line and paragraph separators and the bidi embeddings and
|
|
14
|
+
* overrides U+2028 to U+202E, the word joiner and invisible operators U+2060 to U+2064, the bidi
|
|
15
|
+
* isolates U+2066 to U+2069, the BOM U+FEFF, the interlinear annotation controls U+FFF9 to U+FFFB,
|
|
16
|
+
* and the tag range U+E0000 to U+E007F. The JSON form is data and keeps every code unit. The
|
|
17
|
+
* replacement repeats until the text stops changing so lone surrogates that join into a tag
|
|
18
|
+
* character when a stripped character between them is removed are dropped too. The strip is
|
|
19
|
+
* idempotent, so a caller that already stripped its own text is not hurt by a second call.
|
|
20
|
+
*/
|
|
21
|
+
export const CONTROL_CHARS = /[\u0000-\u001f\u007f-\u009f\u00ad\u034f\u061c\u180e\u200b-\u200f\u2028-\u202e\u2060-\u2064\u2066-\u2069\ufeff\ufff9-\ufffb\u{e0000}-\u{e007f}]/gu;
|
|
22
|
+
export function stripControl(text) {
|
|
23
|
+
let current = text;
|
|
24
|
+
for (;;) {
|
|
25
|
+
const next = current.replace(CONTROL_CHARS, '');
|
|
26
|
+
if (next === current)
|
|
27
|
+
return current;
|
|
28
|
+
current = next;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { stripControl } from "./control.js";
|
|
2
|
+
/**
|
|
3
|
+
* Fixed-width table output. No dependency, no colour, no box drawing: this gets piped. Every
|
|
4
|
+
* header and cell is caller-derived text, so it is stripped of its controls and invisibles
|
|
5
|
+
* before the clip and the width run — padding is computed on what is printed.
|
|
6
|
+
*/
|
|
7
|
+
export function table(headers, rows, maxCell = 78) {
|
|
8
|
+
const clip = (s) => {
|
|
9
|
+
if (s.length <= maxCell)
|
|
10
|
+
return s;
|
|
11
|
+
// Length is UTF-16 code units. A cut between the two halves of an astral pair, a high
|
|
12
|
+
// surrogate at U+D800..U+DBFF followed by a low one at U+DC00..U+DFFF, would leave a lone
|
|
13
|
+
// high surrogate before the ellipsis, so that half pair goes too. The clipped cell is then
|
|
14
|
+
// at most maxCell code units including the ellipsis.
|
|
15
|
+
const cut = maxCell - 1;
|
|
16
|
+
const high = s.charCodeAt(cut - 1);
|
|
17
|
+
const low = s.charCodeAt(cut);
|
|
18
|
+
// No `cut > 0` guard: with cut = 0, charCodeAt(-1) is NaN and every comparison below is
|
|
19
|
+
// false, so the half pair is never dropped at a cut that has nothing before it. That term
|
|
20
|
+
// was an equivalent mutant and the tests could not tell it from this.
|
|
21
|
+
const halfPair = high >= 0xd800 && high <= 0xdbff && low >= 0xdc00 && low <= 0xdfff;
|
|
22
|
+
return s.slice(0, halfPair ? cut - 1 : cut) + '…';
|
|
23
|
+
};
|
|
24
|
+
const body = rows.map((r) => r.map((c) => clip(stripControl(c))));
|
|
25
|
+
const head = headers.map(stripControl);
|
|
26
|
+
// A loop over the rows, never a spread: `Math.max(h.length, ...body.map(...), 0)` passes one
|
|
27
|
+
// argument per row and throws RangeError near 124,000 rows.
|
|
28
|
+
const widths = head.map((h, i) => {
|
|
29
|
+
let width = h.length;
|
|
30
|
+
for (const r of body)
|
|
31
|
+
width = Math.max(width, (r[i] ?? '').length);
|
|
32
|
+
return width;
|
|
33
|
+
});
|
|
34
|
+
const line = (cells) => cells
|
|
35
|
+
.map((c, i) => (i === cells.length - 1 ? c : c.padEnd(widths[i] ?? 0)))
|
|
36
|
+
.join(' ')
|
|
37
|
+
.trimEnd();
|
|
38
|
+
const lines = body.map(line);
|
|
39
|
+
lines.unshift(line(head));
|
|
40
|
+
return lines.join('\n');
|
|
41
|
+
}
|
|
42
|
+
export function json(value) {
|
|
43
|
+
return JSON.stringify(value, null, 2);
|
|
44
|
+
}
|
|
45
|
+
/** `2026-08-21T09:14:22.000Z` -> `2026-08-21`. Nulls read as a dash, never as "null". */
|
|
46
|
+
export function day(value) {
|
|
47
|
+
if (value === null || value === undefined)
|
|
48
|
+
return '-';
|
|
49
|
+
const d = value instanceof Date ? value : new Date(String(value));
|
|
50
|
+
return Number.isNaN(d.getTime()) ? String(value) : d.toISOString().slice(0, 10);
|
|
51
|
+
}
|
|
52
|
+
export function percent(fraction) {
|
|
53
|
+
if (fraction === null || fraction === undefined)
|
|
54
|
+
return '-';
|
|
55
|
+
return `${Math.round(fraction * 100)}%`;
|
|
56
|
+
}
|