@kosuke-ai/kosuke 1.0.106 → 1.0.108
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/CHANGELOG.md +8 -0
- package/README.md +12 -0
- package/dist/commands/hand-written.js +1 -0
- package/dist/program.js +35 -2
- package/dist/runtime/arguments.js +4 -2
- package/dist/runtime/attach.js +4 -2
- package/dist/runtime/fields.js +279 -0
- package/dist/runtime/help.js +10 -4
- package/dist/runtime/runtime.js +31 -2
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.0.108
|
|
4
|
+
|
|
5
|
+
- Fable is now Claude Fable 5.2: model lists name it Fable 5.2, and sessions on any stored `anthropic:fable__*` id run `claude-fable-5-2` (Bedrock: `anthropic.claude-fable-5-2`). Stored Fable 5.1 ids keep resolving to Fable.
|
|
6
|
+
|
|
7
|
+
## 1.0.107
|
|
8
|
+
|
|
9
|
+
- Every command that prints the platform's JSON takes `--fields a,b.c`, only those fields per row or of the object, dotted into nested ones, and `--format compact`, a header line then one tab-separated line per row. An unknown field exits 1 and names the closest ones. Without them, output is unchanged.
|
|
10
|
+
|
|
3
11
|
## 1.0.106
|
|
4
12
|
|
|
5
13
|
- `kosuke sensei cards set` takes `attachments` on WhatsApp and Telegram reply cards as well as Gmail: up to 3 Personal Files, or workspace Files with `workspaceId`, each up to 20 MB. Approving the card sends each file as its own message, the draft as the first file's caption. A reply whose later file fails answers `partly_sent`, naming the files sent and the one that failed.
|
package/README.md
CHANGED
|
@@ -62,6 +62,18 @@ replies and card headlines. `--tools` prints the platform's response, tool block
|
|
|
62
62
|
At a terminal the JSON is indented. Piped, it is one line, and a top-level `data` list prints
|
|
63
63
|
one row per line, still one JSON document. `--pretty` indents it anywhere.
|
|
64
64
|
|
|
65
|
+
`--fields id,title,chatSession.id` prints only those fields, on every command that prints the
|
|
66
|
+
platform's JSON: per row of a list, or of a list the answer holds (`crons list`'s `crons`), else
|
|
67
|
+
of the object. A dot reaches into a nested object, and through a list to each item. A field no
|
|
68
|
+
row has exits 1 and names the closest ones. `--format compact` prints a header line, then one
|
|
69
|
+
tab-separated line per row: nulls empty, timestamps to the second, and anything beside the rows
|
|
70
|
+
(`meta`, a cursor, a total) on stderr. Both work with `--json`, except `--json --format compact`.
|
|
71
|
+
|
|
72
|
+
```sh
|
|
73
|
+
kosuke tasks list <workspaceId> --fields id,title,status,chatSession.id --format compact
|
|
74
|
+
kosuke sessions get <sessionId> --fields session.status,session.qaPassedSha
|
|
75
|
+
```
|
|
76
|
+
|
|
65
77
|
## Factory Skill markers
|
|
66
78
|
|
|
67
79
|
The main chat agent records Factory Skill boundaries with these commands:
|
|
@@ -103,6 +103,7 @@ export const HAND_WRITTEN = {
|
|
|
103
103
|
flags: ['--entries'],
|
|
104
104
|
agents: ['sandboxToken'],
|
|
105
105
|
workspace: true,
|
|
106
|
+
printsJson: true,
|
|
106
107
|
},
|
|
107
108
|
// lint-gate-exempt command-names: the capability probe ksb runs by fixed argv
|
|
108
109
|
// (skill-marker-capability.ts), so an older ksb finds markers on a newer CLI.
|
package/dist/program.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Command } from 'commander';
|
|
1
|
+
import { Command, InvalidArgumentError } from 'commander';
|
|
2
2
|
import { attachSkillMarkerCommands, runSkillMarkerCommand } from './commands/skill-runs.js';
|
|
3
3
|
import { attachAgentCommands } from './commands/agent.js';
|
|
4
4
|
import { attachAuthCommands } from './commands/auth.js';
|
|
@@ -6,13 +6,28 @@ import { attachCardSchemaCommand, attachHelpCommand } from './commands/help.js';
|
|
|
6
6
|
import { attachIntegrationsCommands } from './commands/integrations.js';
|
|
7
7
|
import { attachMergeTrainOrderCommand } from './commands/merge-train-order.js';
|
|
8
8
|
import { attachWatchCommand } from './commands/watch.js';
|
|
9
|
+
import { HAND_WRITTEN } from './commands/hand-written.js';
|
|
9
10
|
import { operations } from './generated/commands/index.js';
|
|
10
11
|
import { attachCommand } from './runtime/attach.js';
|
|
12
|
+
import { parseFields, parseFormat } from './runtime/fields.js';
|
|
11
13
|
import { helpConfiguration, suggestOnError, takeLongHelp } from './runtime/help.js';
|
|
12
14
|
import { formsOf } from './runtime/variants.js';
|
|
13
15
|
import { agentTokenOf, isReachable } from './runtime/agent-tokens.js';
|
|
14
16
|
import { createRuntime, ExitSignal } from './runtime/runtime.js';
|
|
15
17
|
import { version } from './runtime/version.js';
|
|
18
|
+
/** A commander value parser from one that throws, so its message prints as a usage error. */
|
|
19
|
+
const asOption = (parse) => (value) => {
|
|
20
|
+
try {
|
|
21
|
+
return parse(value);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
throw new InvalidArgumentError(error instanceof Error ? error.message : String(error));
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
/** The hand-written commands' words that print their own text rather than the platform's JSON. */
|
|
28
|
+
const OWN_OUTPUT = new Set(Object.values(HAND_WRITTEN)
|
|
29
|
+
.filter((entry) => !('printsJson' in entry && entry.printsJson))
|
|
30
|
+
.map((entry) => entry.command.join(' ')));
|
|
16
31
|
/**
|
|
17
32
|
* The command tree: the commands `commands/hand-written.ts` lists by hand, everything else
|
|
18
33
|
* from the generated descriptors, one leaf per command over its forms. Global flags are read
|
|
@@ -27,6 +42,8 @@ export function createProgram(options = {}) {
|
|
|
27
42
|
.option('--api-url <url>', 'Platform URL; overrides KOSUKE_API_URL. Default https://app.kosuke.ai.')
|
|
28
43
|
.option('--pretty', 'Indented JSON; the default at a terminal, one line when piped.')
|
|
29
44
|
.option('--json', "The platform's whole answer as JSON, for a script. Or KOSUKE_OUTPUT=json.")
|
|
45
|
+
.option('--fields <paths>', 'Only these comma-separated fields; dots reach nested ones. Per row on a list, else on the object: id,chatSession.id.', asOption(parseFields))
|
|
46
|
+
.option('--format <format>', 'compact: a header, then one tab-separated line per row. Nulls print empty, timestamps to the second; json is the default.', asOption(parseFormat))
|
|
30
47
|
// commander's default is kept: a program option is read wherever it
|
|
31
48
|
// appears, so `kosuke projects list --api-key X` works as the README shows.
|
|
32
49
|
.showHelpAfterError('(add --help for usage)')
|
|
@@ -35,10 +52,26 @@ export function createProgram(options = {}) {
|
|
|
35
52
|
// the credentials file.
|
|
36
53
|
let runtime = null;
|
|
37
54
|
const runtimeFor = () => (runtime ??= (options.runtime ?? ((flags) => createRuntime({ ...options.process, flags })))(program.opts()));
|
|
55
|
+
// `--fields` and `--format` shape the platform's JSON, so a command that prints its own text
|
|
56
|
+
// refuses them rather than ignoring them, and `--json`, the whole answer, conflicts with compact.
|
|
57
|
+
program.hook('preAction', (_root, action) => {
|
|
58
|
+
const { fields, format, json } = program.opts();
|
|
59
|
+
if (!fields && !format)
|
|
60
|
+
return;
|
|
61
|
+
const words = [];
|
|
62
|
+
for (let at = action; at?.parent; at = at.parent)
|
|
63
|
+
words.unshift(at.name());
|
|
64
|
+
if (OWN_OUTPUT.has(words.join(' '))) {
|
|
65
|
+
action.error(`error: \`kosuke ${words.join(' ')}\` prints its own text; --fields and --format shape the platform's JSON.`);
|
|
66
|
+
}
|
|
67
|
+
if (json && format === 'compact') {
|
|
68
|
+
action.error('error: --json prints JSON; leave it out with --format compact.');
|
|
69
|
+
}
|
|
70
|
+
});
|
|
38
71
|
const lazy = {
|
|
39
72
|
credentials: () => runtimeFor().credentials(),
|
|
40
73
|
client: (o) => runtimeFor().client(o),
|
|
41
|
-
output: (outcome) => runtimeFor().output(outcome),
|
|
74
|
+
output: (outcome, context) => runtimeFor().output(outcome, context),
|
|
42
75
|
notice: (line) => runtimeFor().notice?.(line),
|
|
43
76
|
};
|
|
44
77
|
attachAuthCommands(program, lazy, options.auth);
|
|
@@ -5,14 +5,16 @@
|
|
|
5
5
|
* reads the same rules under Bun without this package's dependencies.
|
|
6
6
|
*/
|
|
7
7
|
/**
|
|
8
|
-
* Global flags beyond commander's own, and whether each takes a value.
|
|
9
|
-
*
|
|
8
|
+
* Global flags beyond commander's own, and whether each takes a value. `takeLongHelp` and the
|
|
9
|
+
* prompt-commands lint gate read it.
|
|
10
10
|
*/
|
|
11
11
|
export const CLI_GLOBAL_FLAGS = new Map([
|
|
12
12
|
['--api-key', true],
|
|
13
13
|
['--api-url', true],
|
|
14
14
|
['--pretty', false],
|
|
15
15
|
['--json', false],
|
|
16
|
+
['--fields', true],
|
|
17
|
+
['--format', true],
|
|
16
18
|
['--long', false],
|
|
17
19
|
['--help', false],
|
|
18
20
|
['-h', false],
|
package/dist/runtime/attach.js
CHANGED
|
@@ -345,8 +345,10 @@ async function run(leaf, runtime, operation, input, options = {}) {
|
|
|
345
345
|
}
|
|
346
346
|
try {
|
|
347
347
|
const outcome = await execute(client, request, controller.signal);
|
|
348
|
-
|
|
349
|
-
|
|
348
|
+
// `--fields` and `--format` pick from the platform's own JSON, so a text view steps aside.
|
|
349
|
+
const { fields, format } = leaf.optsWithGlobals();
|
|
350
|
+
const view = fields || format ? undefined : VIEWS[operation.command.join(' ')]?.view;
|
|
351
|
+
runtime.output(view && outcome.ok ? { ...outcome, body: view(outcome.body, options) } : outcome, { list: operation.command[operation.command.length - 1] === 'list' });
|
|
350
352
|
}
|
|
351
353
|
finally {
|
|
352
354
|
process.removeListener('SIGINT', cancel);
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { editDistance } from './help.js';
|
|
2
|
+
/** The fields of `--fields`, split on commas, blanks dropped. Throws on an empty list. */
|
|
3
|
+
export function parseFields(value) {
|
|
4
|
+
const fields = value
|
|
5
|
+
.split(',')
|
|
6
|
+
.map((field) => field.trim())
|
|
7
|
+
.filter(Boolean);
|
|
8
|
+
if (!fields.length)
|
|
9
|
+
throw new Error('--fields needs at least one field, as `id,title`.');
|
|
10
|
+
const bad = fields.find((field) => field.split('.').some((part) => !part));
|
|
11
|
+
if (bad)
|
|
12
|
+
throw new Error(`--fields: "${bad}" is not a field path; write \`a.b\`.`);
|
|
13
|
+
return fields;
|
|
14
|
+
}
|
|
15
|
+
export function parseFormat(value) {
|
|
16
|
+
if (value === 'json' || value === 'compact')
|
|
17
|
+
return value;
|
|
18
|
+
throw new Error(`--format takes json or compact; got "${value}".`);
|
|
19
|
+
}
|
|
20
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
21
|
+
const MISSING = Symbol('missing');
|
|
22
|
+
/**
|
|
23
|
+
* The value at a path. A list on the way maps the rest of the path over its items; a null on
|
|
24
|
+
* the way is the value. MISSING when an object on the way lacks the key.
|
|
25
|
+
*/
|
|
26
|
+
function pick(value, path) {
|
|
27
|
+
if (!path.length)
|
|
28
|
+
return value;
|
|
29
|
+
if (value === null || value === undefined)
|
|
30
|
+
return null;
|
|
31
|
+
if (Array.isArray(value)) {
|
|
32
|
+
const items = value.map((item) => pick(item, path));
|
|
33
|
+
// Known when an item has it, or when no item can say (none, or only nulls).
|
|
34
|
+
if (items.length && items.every((item) => item === MISSING))
|
|
35
|
+
return MISSING;
|
|
36
|
+
return items.map((item) => (item === MISSING ? null : item));
|
|
37
|
+
}
|
|
38
|
+
if (!isRecord(value))
|
|
39
|
+
return MISSING;
|
|
40
|
+
const [head, ...rest] = path;
|
|
41
|
+
if (!Object.hasOwn(value, head))
|
|
42
|
+
return MISSING;
|
|
43
|
+
return pick(value[head], rest);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Whether a value has the path: `yes`, `no` (an object on the way lacks the key), or `maybe` (a
|
|
47
|
+
* null or an empty list on the way, which cannot say). A list says yes when an item does.
|
|
48
|
+
*/
|
|
49
|
+
function probe(value, path) {
|
|
50
|
+
if (!path.length)
|
|
51
|
+
return 'yes';
|
|
52
|
+
if (value === null || value === undefined)
|
|
53
|
+
return 'maybe';
|
|
54
|
+
if (Array.isArray(value)) {
|
|
55
|
+
const answers = value.map((item) => probe(item, path));
|
|
56
|
+
return answers.includes('yes') ? 'yes' : answers.includes('no') ? 'no' : 'maybe';
|
|
57
|
+
}
|
|
58
|
+
if (!isRecord(value))
|
|
59
|
+
return 'no';
|
|
60
|
+
const [head, ...rest] = path;
|
|
61
|
+
return Object.hasOwn(value, head) ? probe(value[head], rest) : 'no';
|
|
62
|
+
}
|
|
63
|
+
/** Whether the rows have the path: one row has it, or none says it lacks it. */
|
|
64
|
+
const known = (rows, path) => {
|
|
65
|
+
const answers = rows.map((row) => probe(row, path));
|
|
66
|
+
return answers.includes('yes') || !answers.includes('no');
|
|
67
|
+
};
|
|
68
|
+
/** Only the paths of a value, nested as in it; a list keeps its items, each picked alike. */
|
|
69
|
+
function project(value, paths) {
|
|
70
|
+
if (paths.some((path) => !path.length))
|
|
71
|
+
return value;
|
|
72
|
+
if (value === null || value === undefined)
|
|
73
|
+
return null;
|
|
74
|
+
if (Array.isArray(value))
|
|
75
|
+
return value.map((item) => project(item, paths));
|
|
76
|
+
if (!isRecord(value))
|
|
77
|
+
return null;
|
|
78
|
+
const byHead = new Map();
|
|
79
|
+
for (const [head, ...rest] of paths) {
|
|
80
|
+
byHead.set(head, [...(byHead.get(head) ?? []), rest]);
|
|
81
|
+
}
|
|
82
|
+
const out = {};
|
|
83
|
+
for (const [head, rests] of byHead) {
|
|
84
|
+
out[head] = Object.hasOwn(value, head) ? project(value[head], rests) : null;
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
/** Every path a value offers, objects and their leaves, lists looked into, for a hint. */
|
|
89
|
+
function pathsOf(value, prefix = '', out = new Set(), depth = 0) {
|
|
90
|
+
if (depth > 4)
|
|
91
|
+
return out;
|
|
92
|
+
if (Array.isArray(value)) {
|
|
93
|
+
for (const item of value.slice(0, 50))
|
|
94
|
+
pathsOf(item, prefix, out, depth + 1);
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
if (!isRecord(value))
|
|
98
|
+
return out;
|
|
99
|
+
for (const [key, child] of Object.entries(value)) {
|
|
100
|
+
const path = prefix ? `${prefix}.${key}` : key;
|
|
101
|
+
out.add(path);
|
|
102
|
+
pathsOf(child, path, out, depth + 1);
|
|
103
|
+
}
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Up to five available paths for an unknown one: those within two edits of it, of its last
|
|
108
|
+
* word, or holding it; when none is, the five nearest.
|
|
109
|
+
*/
|
|
110
|
+
function closest(field, available) {
|
|
111
|
+
const lower = (text) => text.toLowerCase();
|
|
112
|
+
const last = (path) => lower(path.slice(path.lastIndexOf('.') + 1));
|
|
113
|
+
const depth = (path) => path.split('.').length;
|
|
114
|
+
const wanted = lower(field);
|
|
115
|
+
const scored = available.map((path) => {
|
|
116
|
+
const whole = editDistance(wanted, lower(path));
|
|
117
|
+
const tail = depth(path) === depth(field)
|
|
118
|
+
? editDistance(last(field), last(path))
|
|
119
|
+
: Number.POSITIVE_INFINITY;
|
|
120
|
+
const holds = lower(path).includes(wanted) || (path.length > 2 && wanted.includes(lower(path)));
|
|
121
|
+
return { path, score: Math.min(whole, tail), close: whole <= 2 || tail <= 2 || holds };
|
|
122
|
+
});
|
|
123
|
+
const near = scored.some((entry) => entry.close) ? scored.filter((entry) => entry.close) : scored;
|
|
124
|
+
return near
|
|
125
|
+
.sort((a, b) => a.score - b.score || a.path.length - b.path.length || (a.path < b.path ? -1 : 1))
|
|
126
|
+
.slice(0, 5)
|
|
127
|
+
.map(({ path }) => path);
|
|
128
|
+
}
|
|
129
|
+
/** The members of an object that hold a list of objects (an empty list counts). */
|
|
130
|
+
const listsOf = (value) => Object.keys(value).filter((key) => {
|
|
131
|
+
const member = value[key];
|
|
132
|
+
return Array.isArray(member) && member.every(isRecord);
|
|
133
|
+
});
|
|
134
|
+
/** The rows the fields read, or why no reading has them all. */
|
|
135
|
+
function rowsOf(value, fields, listing) {
|
|
136
|
+
const split = (fields ?? []).map((field) => field.split('.'));
|
|
137
|
+
if (Array.isArray(value)) {
|
|
138
|
+
const unknown = (fields ?? []).filter((_, i) => !known(value, split[i]));
|
|
139
|
+
if (unknown.length)
|
|
140
|
+
return { unknown, available: [...pathsOf(value)] };
|
|
141
|
+
return { rows: value, list: true, paths: split };
|
|
142
|
+
}
|
|
143
|
+
if (!isRecord(value))
|
|
144
|
+
return { rows: [value], list: false, paths: split };
|
|
145
|
+
const lists = listing ? listsOf(value) : [];
|
|
146
|
+
const held = lists.length === 1 ? lists[0] : undefined;
|
|
147
|
+
const heldRows = held === undefined ? [] : value[held];
|
|
148
|
+
if (!fields?.length) {
|
|
149
|
+
return held === undefined
|
|
150
|
+
? { rows: [value], list: false, paths: [] }
|
|
151
|
+
: { rows: heldRows, list: true, key: held, paths: [] };
|
|
152
|
+
}
|
|
153
|
+
// `crons.id,crons.name`: the list named, its items the rows.
|
|
154
|
+
const [first] = split[0];
|
|
155
|
+
if (lists.includes(first) && split.every((path) => path.length > 1 && path[0] === first)) {
|
|
156
|
+
const rows = value[first];
|
|
157
|
+
const paths = split.map((path) => path.slice(1));
|
|
158
|
+
if (paths.every((path) => known(rows, path)))
|
|
159
|
+
return { rows, list: true, key: first, paths };
|
|
160
|
+
}
|
|
161
|
+
const onObject = fields.filter((_, i) => !known([value], split[i]));
|
|
162
|
+
if (!onObject.length)
|
|
163
|
+
return { rows: [value], list: false, paths: split };
|
|
164
|
+
// `id,name` on `{crons: [...], total}`: its one list's items.
|
|
165
|
+
const onRows = held === undefined ? fields : fields.filter((_, i) => !known(heldRows, split[i]));
|
|
166
|
+
if (held !== undefined && !onRows.length) {
|
|
167
|
+
return { rows: heldRows, list: true, key: held, paths: split };
|
|
168
|
+
}
|
|
169
|
+
// Each field read one way or the other, never all one way: `id,total`.
|
|
170
|
+
if (held !== undefined && onObject.every((field) => !onRows.includes(field))) {
|
|
171
|
+
return { mixed: [onObject, onRows] };
|
|
172
|
+
}
|
|
173
|
+
const available = new Set(pathsOf(value));
|
|
174
|
+
if (held !== undefined)
|
|
175
|
+
pathsOf(heldRows, '', available);
|
|
176
|
+
// Unknown both ways; never empty, as the mix above took the rest.
|
|
177
|
+
return { unknown: onObject.filter((field) => onRows.includes(field)), available: [...available] };
|
|
178
|
+
}
|
|
179
|
+
const TIMESTAMP = /^(\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d)\.\d+(Z|[+-]\d\d:?\d\d)?$/;
|
|
180
|
+
const toSeconds = (_key, value) => typeof value === 'string' ? value.replace(TIMESTAMP, '$1$2') : value;
|
|
181
|
+
/** A cell of the compact format: empty for null, seconds for a timestamp, one line always. */
|
|
182
|
+
function cell(value) {
|
|
183
|
+
if (value === null || value === undefined || value === MISSING)
|
|
184
|
+
return '';
|
|
185
|
+
if (typeof value === 'string') {
|
|
186
|
+
return value
|
|
187
|
+
.replace(TIMESTAMP, '$1$2')
|
|
188
|
+
.replace(/\\/g, '\\\\')
|
|
189
|
+
.replace(/\t/g, '\\t')
|
|
190
|
+
.replace(/\r/g, '\\r')
|
|
191
|
+
.replace(/\n/g, '\\n');
|
|
192
|
+
}
|
|
193
|
+
if (typeof value === 'object')
|
|
194
|
+
return JSON.stringify(value, toSeconds);
|
|
195
|
+
return String(value);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* What the answer says beside the rows, on one line for stderr: the envelope's `meta`, and the
|
|
199
|
+
* plain values beside a list held in `data` (`total=3`, a cursor).
|
|
200
|
+
*/
|
|
201
|
+
function besideLine(envelope, value, key) {
|
|
202
|
+
const parts = [];
|
|
203
|
+
const walk = (inner, prefix) => {
|
|
204
|
+
if (isRecord(inner)) {
|
|
205
|
+
for (const [name, child] of Object.entries(inner))
|
|
206
|
+
walk(child, prefix ? `${prefix}.${name}` : name);
|
|
207
|
+
}
|
|
208
|
+
else if (inner !== null && inner !== undefined && !Array.isArray(inner)) {
|
|
209
|
+
parts.push(`${prefix}=${cell(inner)}`);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
if (key !== undefined && isRecord(value)) {
|
|
213
|
+
for (const [name, member] of Object.entries(value)) {
|
|
214
|
+
if (name !== key && member !== null && typeof member !== 'object')
|
|
215
|
+
walk(member, name);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (envelope)
|
|
219
|
+
walk(envelope.meta, 'meta');
|
|
220
|
+
return parts.length ? parts.join(' ') : undefined;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* The answer shaped by `--fields` and `--format`. JSON keeps the envelope's `meta` and puts the
|
|
224
|
+
* picked fields in `data`, nested as in the answer. Compact prints a header, then a line per row,
|
|
225
|
+
* tab-separated, with what lies beside the rows for stderr. A field no row has is refused with
|
|
226
|
+
* the closest the answer offers.
|
|
227
|
+
*/
|
|
228
|
+
export function shape(body, { fields, format = 'json', list: listing = false }) {
|
|
229
|
+
const envelope = isRecord(body) && 'data' in body ? body : null;
|
|
230
|
+
const value = envelope ? envelope.data : body;
|
|
231
|
+
const found = rowsOf(value, fields?.length ? fields : undefined, listing);
|
|
232
|
+
if ('mixed' in found) {
|
|
233
|
+
const [rowFields, besideFields] = found.mixed;
|
|
234
|
+
return {
|
|
235
|
+
ok: false,
|
|
236
|
+
message: `error: --fields mixes the rows' fields (${rowFields.join(', ')}) with fields beside them ` +
|
|
237
|
+
`(${besideFields.join(', ')}); name one or the other. With --format compact, what lies ` +
|
|
238
|
+
'beside the rows prints on stderr.',
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
if ('unknown' in found) {
|
|
242
|
+
const lines = found.unknown.map((field) => {
|
|
243
|
+
const near = closest(field, found.available);
|
|
244
|
+
return `error: unknown field "${field}".${near.length ? ` Closest: ${near.join(', ')}.` : ' The answer has no fields.'}`;
|
|
245
|
+
});
|
|
246
|
+
return { ok: false, message: lines.join('\n') };
|
|
247
|
+
}
|
|
248
|
+
const { rows, list, key, paths } = found;
|
|
249
|
+
if (format === 'compact') {
|
|
250
|
+
const columns = fields?.length
|
|
251
|
+
? [...fields]
|
|
252
|
+
: rows.some(isRecord)
|
|
253
|
+
? [...new Set(rows.flatMap((row) => (isRecord(row) ? Object.keys(row) : [])))]
|
|
254
|
+
: rows.length
|
|
255
|
+
? ['value']
|
|
256
|
+
: [];
|
|
257
|
+
const readings = fields?.length ? paths : columns.map((column) => [column]);
|
|
258
|
+
const line = (row) => (rows.some(isRecord) || fields?.length
|
|
259
|
+
? readings.map((path) => cell(pick(row, path)))
|
|
260
|
+
: [cell(row)]).join('\t');
|
|
261
|
+
const beside = besideLine(envelope, value, key);
|
|
262
|
+
return {
|
|
263
|
+
ok: true,
|
|
264
|
+
// No rows and no fields named: no columns to head, so nothing to print.
|
|
265
|
+
compact: columns.length ? [columns.join('\t'), ...rows.map(line)].join('\n') : '',
|
|
266
|
+
...(beside ? { meta: beside } : {}),
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
if (!fields?.length)
|
|
270
|
+
return { ok: true, json: body };
|
|
271
|
+
const picked = list ? rows.map((row) => project(row, paths)) : project(rows[0], paths);
|
|
272
|
+
const data = key === undefined ? picked : { [key]: picked };
|
|
273
|
+
if (!envelope)
|
|
274
|
+
return { ok: true, json: data };
|
|
275
|
+
return {
|
|
276
|
+
ok: true,
|
|
277
|
+
json: { data, ...(envelope.meta === undefined ? {} : { meta: envelope.meta }) },
|
|
278
|
+
};
|
|
279
|
+
}
|
package/dist/runtime/help.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Help } from 'commander';
|
|
2
|
-
import { CLI_OUTPUT_FLAGS } from './arguments.js';
|
|
2
|
+
import { CLI_GLOBAL_FLAGS, CLI_OUTPUT_FLAGS } from './arguments.js';
|
|
3
3
|
/**
|
|
4
4
|
* The help an agent reads. Short by default: the usage line, the description's first sentence
|
|
5
5
|
* and one line per flag. `--help --long` prints the whole text. `kosuke help --compact` prints
|
|
@@ -31,7 +31,12 @@ export function setOptionShape(option, shape) {
|
|
|
31
31
|
*/
|
|
32
32
|
export function takeLongHelp(argv) {
|
|
33
33
|
const args = argv.slice(2);
|
|
34
|
-
|
|
34
|
+
// The first word past the global flags, a valued one's value skipped (`--fields id help`).
|
|
35
|
+
let at = 0;
|
|
36
|
+
while (at < args.length && (CLI_OUTPUT_FLAGS.has(args[at]) || CLI_GLOBAL_FLAGS.get(args[at]))) {
|
|
37
|
+
at += CLI_GLOBAL_FLAGS.get(args[at]) ? 2 : 1;
|
|
38
|
+
}
|
|
39
|
+
const command = args[at];
|
|
35
40
|
const asksHelp = args.includes('--help') || args.includes('-h') || command === 'help';
|
|
36
41
|
if (!asksHelp || !args.includes(LONG_HELP_FLAG))
|
|
37
42
|
return { argv: [...argv], long: false };
|
|
@@ -237,7 +242,7 @@ function leafList(group, fold = true) {
|
|
|
237
242
|
* their path from it. Sibling sub-groups with the same leaves share a line: `diff commits|files`.
|
|
238
243
|
*/
|
|
239
244
|
export function compactHelp(program) {
|
|
240
|
-
const lines = ['Flags: kosuke <command> --help.', LEGEND];
|
|
245
|
+
const lines = ['Flags: kosuke <command> --help; --fields a,b.c --format compact', LEGEND];
|
|
241
246
|
const top = leafList(program, false);
|
|
242
247
|
if (top)
|
|
243
248
|
lines.push(`kosuke: ${top}`);
|
|
@@ -273,7 +278,8 @@ export function compactHelp(program) {
|
|
|
273
278
|
return lines.join('\n');
|
|
274
279
|
}
|
|
275
280
|
// --- did you mean --------------------------------------------------------------------------
|
|
276
|
-
|
|
281
|
+
/** Edits from one word to another, a swap of two neighbours counted as one. */
|
|
282
|
+
export function editDistance(a, b) {
|
|
277
283
|
const d = Array.from({ length: a.length + 1 }, (_, i) => Array.from({ length: b.length + 1 }, (_, j) => (i === 0 ? j : j === 0 ? i : 0)));
|
|
278
284
|
for (let i = 1; i <= a.length; i++) {
|
|
279
285
|
for (let j = 1; j <= b.length; j++) {
|
package/dist/runtime/runtime.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createApiClient } from './client.js';
|
|
2
2
|
import { resolveCredentials, } from './credentials.js';
|
|
3
|
+
import { shape } from './fields.js';
|
|
3
4
|
/**
|
|
4
5
|
* Whether the call asked for JSON: `--json` or `KOSUKE_OUTPUT=json`, one switch. Every generated
|
|
5
6
|
* command already prints it, so only a command whose own default is text reads this.
|
|
@@ -32,12 +33,40 @@ export function createRuntime({ flags, env = process.env, fetch, stdout = (line)
|
|
|
32
33
|
notice(line) {
|
|
33
34
|
stderr(line);
|
|
34
35
|
},
|
|
35
|
-
output(outcome) {
|
|
36
|
+
output(outcome, context) {
|
|
36
37
|
if (outcome.ok) {
|
|
37
38
|
// 204 has nothing to print; anything else is passed through as JSON so
|
|
38
39
|
// `| jq` works without the platform CLI knowing the shape.
|
|
39
|
-
if (outcome.body
|
|
40
|
+
if (outcome.body === undefined)
|
|
41
|
+
return;
|
|
42
|
+
const shaping = flags.fields?.length || flags.format === 'compact';
|
|
43
|
+
if (!shaping || typeof outcome.body === 'string') {
|
|
44
|
+
if (shaping) {
|
|
45
|
+
stderr('error: this command printed text, not JSON; --fields and --format need JSON.');
|
|
46
|
+
exit(1);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
40
49
|
stdout(render(outcome.body, flags.pretty || tty.stdout));
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const shaped = shape(outcome.body, {
|
|
53
|
+
...(flags.fields ? { fields: flags.fields } : {}),
|
|
54
|
+
...(flags.format ? { format: flags.format } : {}),
|
|
55
|
+
list: context?.list === true,
|
|
56
|
+
});
|
|
57
|
+
if (!shaped.ok) {
|
|
58
|
+
stderr(shaped.message);
|
|
59
|
+
exit(1);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if ('compact' in shaped) {
|
|
63
|
+
if (shaped.compact)
|
|
64
|
+
stdout(shaped.compact);
|
|
65
|
+
if (shaped.meta)
|
|
66
|
+
stderr(shaped.meta);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
stdout(render(shaped.json, flags.pretty || tty.stdout));
|
|
41
70
|
return;
|
|
42
71
|
}
|
|
43
72
|
stderr(render(outcome.body ?? { error: `HTTP ${outcome.status}` }, flags.pretty || tty.stderr));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kosuke-ai/kosuke",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.108",
|
|
4
4
|
"description": "Sensei platform CLI. The client customers install to drive app.kosuke.ai from their own machine.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -43,10 +43,10 @@
|
|
|
43
43
|
"minimatch": "^11.0.1"
|
|
44
44
|
},
|
|
45
45
|
"kosukeRelease": {
|
|
46
|
-
"sourceCommit": "
|
|
47
|
-
"sourceIdentity": "
|
|
46
|
+
"sourceCommit": "8837ac3b31e09f8a562332119b992da3457a0d74",
|
|
47
|
+
"sourceIdentity": "39cc7a3b43270051704bdbb8a7f94f3c4da2b61e8abe320d4965ba2a268059b9",
|
|
48
48
|
"fragments": [
|
|
49
|
-
"
|
|
49
|
+
"fable-5-2-u7k3rw.json"
|
|
50
50
|
]
|
|
51
51
|
}
|
|
52
52
|
}
|