@octen.ai/cli 0.12.0 → 0.13.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/README.md +48 -1
- package/dist/api/constants.d.ts +11 -0
- package/dist/api/constants.js +5 -0
- package/dist/api/designSearch.d.ts +55 -0
- package/dist/api/designSearch.js +136 -0
- package/dist/api/premierSearch.d.ts +19 -0
- package/dist/api/premierSearch.js +37 -0
- package/dist/cli.js +9 -1
- package/dist/commands/designSearch.d.ts +2 -0
- package/dist/commands/designSearch.js +63 -0
- package/dist/commands/premierSearch.d.ts +8 -0
- package/dist/commands/premierSearch.js +37 -0
- package/dist/output/pretty/designSearch.d.ts +15 -0
- package/dist/output/pretty/designSearch.js +202 -0
- package/dist/output/pretty/envelope.d.ts +2 -0
- package/dist/output/pretty/envelope.js +4 -0
- package/dist/output/pretty/search.d.ts +9 -3
- package/dist/output/pretty/search.js +26 -11
- package/dist/util/terminalText.d.ts +14 -0
- package/dist/util/terminalText.js +30 -0
- package/package.json +1 -1
- package/skills/manifest.json +1 -0
- package/skills/octen-design-search/SKILL.md +312 -0
- package/skills/octen-design-search/agents/openai.yaml +4 -0
- package/skills/octen-search/SKILL.md +53 -5
- package/skills/octen-web-search/SKILL.md +14 -2
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import pc from "picocolors";
|
|
2
|
+
import { quotePath } from "../../util/quotePath.js";
|
|
3
|
+
import { printable, printableLine } from "../../util/terminalText.js";
|
|
4
|
+
import { envelopeError, isRecord, unwrap } from "./envelope.js";
|
|
5
|
+
import { formatResultList } from "./search.js";
|
|
6
|
+
/** Where a description or a block of code is cut, as the other renderers cut a snippet. */
|
|
7
|
+
const MAX_TEXT = 300;
|
|
8
|
+
/** Where one line of design values is cut. */
|
|
9
|
+
const MAX_VALUE_LINE = 200;
|
|
10
|
+
/** The most images one call returns, so the most a group lookup can ask for. */
|
|
11
|
+
const MAX_COUNT = 100;
|
|
12
|
+
/**
|
|
13
|
+
* How deep a design value is walked. Real ones are three or four levels deep;
|
|
14
|
+
* the bound is there so one pathological value is cut short instead of
|
|
15
|
+
* exhausting the stack and taking the whole listing down.
|
|
16
|
+
*/
|
|
17
|
+
const MAX_DEPTH = 12;
|
|
18
|
+
/**
|
|
19
|
+
* A string field, as one line that is safe to print. Everything this renderer
|
|
20
|
+
* prints from the response goes through here or through `printable`: the text
|
|
21
|
+
* is somebody else's page, and must not be able to drive the terminal.
|
|
22
|
+
*/
|
|
23
|
+
const text = (v) => (typeof v === "string" ? printableLine(v).trim() : "");
|
|
24
|
+
const count = (v) => typeof v === "number" && Number.isFinite(v) ? v : undefined;
|
|
25
|
+
/**
|
|
26
|
+
* One line for a design value of any shape.
|
|
27
|
+
*
|
|
28
|
+
* The API says the fields vary by image, and they do: design tokens for a web
|
|
29
|
+
* page, plain lists for a slide, counts for an app screen. So this walks
|
|
30
|
+
* whatever it is given instead of naming fields. Three shapes are read down to
|
|
31
|
+
* the value a person wants — a `$value` token wrapper, a colour carrying a
|
|
32
|
+
* `hex`, a `{value, unit}` dimension — and anything else prints as `key value`
|
|
33
|
+
* pairs, so a shape never seen before still reaches the terminal.
|
|
34
|
+
*/
|
|
35
|
+
function inline(v, depth = 0) {
|
|
36
|
+
if (v == null)
|
|
37
|
+
return { text: "", composite: false };
|
|
38
|
+
if (depth >= MAX_DEPTH && typeof v === "object")
|
|
39
|
+
return { text: "…", composite: false };
|
|
40
|
+
if (typeof v !== "object")
|
|
41
|
+
return { text: printableLine(String(v)), composite: false };
|
|
42
|
+
if (Array.isArray(v)) {
|
|
43
|
+
const parts = v.map((x) => inline(x, depth + 1).text).filter(Boolean);
|
|
44
|
+
return { text: parts.join(", "), composite: parts.length > 1 };
|
|
45
|
+
}
|
|
46
|
+
const o = v;
|
|
47
|
+
if ("$value" in o)
|
|
48
|
+
return inline(o.$value, depth + 1);
|
|
49
|
+
if (typeof o.hex === "string") {
|
|
50
|
+
const alpha = count(o.alpha);
|
|
51
|
+
const hex = text(o.hex);
|
|
52
|
+
return { text: alpha != null && alpha !== 1 ? `${hex} (alpha ${alpha})` : hex, composite: false };
|
|
53
|
+
}
|
|
54
|
+
if (typeof o.unit === "string" && typeof o.value === "number")
|
|
55
|
+
return { text: `${o.value}${text(o.unit)}`, composite: false };
|
|
56
|
+
const parts = Object.entries(o)
|
|
57
|
+
.map(([k, x]) => {
|
|
58
|
+
const r = inline(x, depth + 1);
|
|
59
|
+
return r.text ? `${printableLine(k)} ${r.composite ? `(${r.text})` : r.text}` : "";
|
|
60
|
+
})
|
|
61
|
+
.filter(Boolean);
|
|
62
|
+
return { text: parts.join(", "), composite: parts.length > 0 };
|
|
63
|
+
}
|
|
64
|
+
function cut(s, max, shortened) {
|
|
65
|
+
if (s.length <= max)
|
|
66
|
+
return s;
|
|
67
|
+
shortened.any = true;
|
|
68
|
+
// Not through the middle of a character that takes two code units: half of
|
|
69
|
+
// one prints as a replacement mark.
|
|
70
|
+
const end = /[\uD800-\uDBFF]/.test(s[max - 1]) ? max - 1 : max;
|
|
71
|
+
return `${s.slice(0, end)}…`;
|
|
72
|
+
}
|
|
73
|
+
/** An id that prints unchanged: printable ASCII. Anything else is left to --json. */
|
|
74
|
+
const PASTEABLE_ID = /^[\x20-\x7e]+$/;
|
|
75
|
+
function groupNoun(type) {
|
|
76
|
+
return type === "deck" ? "deck" : type === "web_page" ? "page" : "group";
|
|
77
|
+
}
|
|
78
|
+
function formatImage(im, i, ctx, shortened) {
|
|
79
|
+
const lines = [];
|
|
80
|
+
lines.push(`${i + 1}. ${pc.bold(text(im.title) || "(untitled)")}`);
|
|
81
|
+
const width = count(im.width);
|
|
82
|
+
const height = count(im.height);
|
|
83
|
+
const kind = [text(im.type), width != null && height != null ? `${width}x${height}` : ""].filter(Boolean).join(" · ");
|
|
84
|
+
if (kind)
|
|
85
|
+
lines.push(` ${pc.dim(kind)}`);
|
|
86
|
+
if (text(im.url))
|
|
87
|
+
lines.push(` ${pc.dim(text(im.url))}`);
|
|
88
|
+
if (text(im.source_page))
|
|
89
|
+
lines.push(` ${pc.dim(`Source: ${text(im.source_page)}`)}`);
|
|
90
|
+
const group = isRecord(im.group) ? im.group : undefined;
|
|
91
|
+
if (group) {
|
|
92
|
+
const name = [text(group.name), text(group.type) ? `(${text(group.type)})` : ""].filter(Boolean).join(" ");
|
|
93
|
+
const position = count(group.position);
|
|
94
|
+
const total = count(group.total);
|
|
95
|
+
const place = position != null && total != null ? `${position} of ${total}` : "";
|
|
96
|
+
const where = [name, place].filter(Boolean).join(" — ");
|
|
97
|
+
if (where)
|
|
98
|
+
lines.push(` ${where}`);
|
|
99
|
+
// The id is only useful as the argument that fetches the rest, so it is
|
|
100
|
+
// printed as that command. Skipped during a lookup: it is the command that
|
|
101
|
+
// was just run.
|
|
102
|
+
// Taken as sent, not through `text()`: the command is meant to be pasted,
|
|
103
|
+
// so the id in it has to be the id the API returned. One that cannot be
|
|
104
|
+
// shown unchanged — a control character, a tab, a line break — gets no
|
|
105
|
+
// command, rather than one that looks up a different id.
|
|
106
|
+
const id = typeof group.group_id === "string" ? group.group_id : "";
|
|
107
|
+
if (id && !ctx.group) {
|
|
108
|
+
const noun = groupNoun(group.type);
|
|
109
|
+
if (!PASTEABLE_ID.test(id)) {
|
|
110
|
+
lines.push(` ${pc.dim(`The ${noun}'s group_id is in the --json output.`)}`);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
const base = ctx.baseUrl ? ` --base-url ${quotePath(ctx.baseUrl)}` : "";
|
|
114
|
+
const command = `octen design-search --group ${quotePath(id)}`;
|
|
115
|
+
// One call returns at most MAX_COUNT, so for a larger group the
|
|
116
|
+
// command fetches a part and is labelled as one; and with no total the
|
|
117
|
+
// count is unknown, so it is neither given nor claimed.
|
|
118
|
+
const hint = total == null || total < 1
|
|
119
|
+
? `More of the ${noun}: ${command} -n <count>${base} (returns 5 without -n)`
|
|
120
|
+
: total > MAX_COUNT
|
|
121
|
+
? `First ${MAX_COUNT} of the ${noun}: ${command} -n ${MAX_COUNT}${base}`
|
|
122
|
+
: `Whole ${noun}: ${command} -n ${total}${base}`;
|
|
123
|
+
lines.push(` ${pc.dim(hint)}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const theme = isRecord(group.master) ? inline(group.master).text : "";
|
|
127
|
+
if (theme)
|
|
128
|
+
lines.push(` ${pc.dim(`Deck theme: ${cut(theme, MAX_VALUE_LINE, shortened)}`)}`);
|
|
129
|
+
}
|
|
130
|
+
if (text(im.description))
|
|
131
|
+
lines.push(` ${cut(text(im.description), MAX_TEXT, shortened)}`);
|
|
132
|
+
if (isRecord(im.design_facts)) {
|
|
133
|
+
const values = Object.entries(im.design_facts)
|
|
134
|
+
.map(([k, v]) => [k, inline(v).text])
|
|
135
|
+
.filter(([, v]) => v);
|
|
136
|
+
if (values.length) {
|
|
137
|
+
lines.push(` ${pc.bold("Design values")}`);
|
|
138
|
+
for (const [k, v] of values)
|
|
139
|
+
lines.push(` ${printableLine(k)}: ${cut(v, MAX_VALUE_LINE, shortened)}`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const code = isRecord(im.snippet) && typeof im.snippet.code === "string" ? im.snippet.code : "";
|
|
143
|
+
if (code) {
|
|
144
|
+
const format = text(im.snippet.format);
|
|
145
|
+
lines.push(` ${pc.bold(`Code (${format ? `${format}, ` : ""}${code.length} chars)`)}`);
|
|
146
|
+
// Each line indented, so a line of the code cannot read as a field.
|
|
147
|
+
lines.push(...printable(cut(code, MAX_TEXT, shortened)).split("\n").map((l) => ` ${pc.dim(l)}`));
|
|
148
|
+
}
|
|
149
|
+
return lines.join("\n");
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The API returns five images of a group unless told otherwise, and a lookup
|
|
153
|
+
* that shows part of a page reads as the whole of it. Says so when it applies.
|
|
154
|
+
*/
|
|
155
|
+
function groupRemainder(images) {
|
|
156
|
+
const total = images.map((im) => count(im.group?.total)).find((t) => t != null);
|
|
157
|
+
if (total == null || images.length >= total)
|
|
158
|
+
return undefined;
|
|
159
|
+
const shown = `Showing ${images.length} of ${total} in this group`;
|
|
160
|
+
if (images.length >= MAX_COUNT)
|
|
161
|
+
return `${shown} — one call returns at most ${MAX_COUNT}.`;
|
|
162
|
+
return total > MAX_COUNT
|
|
163
|
+
? `${shown} — add -n ${MAX_COUNT} for the first ${MAX_COUNT}, the most one call returns.`
|
|
164
|
+
: `${shown} — add -n ${total} for all of them.`;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Design references live under `data.images`; `data.results` holds the web
|
|
168
|
+
* results that come back alongside.
|
|
169
|
+
*/
|
|
170
|
+
export function renderDesignSearch(data, ctx = {}) {
|
|
171
|
+
const body = unwrap(data);
|
|
172
|
+
// Entries that are not objects are skipped, so one of them cannot take the
|
|
173
|
+
// entries beside it down with it.
|
|
174
|
+
const images = Array.isArray(body?.images) ? body.images.filter(isRecord) : [];
|
|
175
|
+
const web = Array.isArray(body?.results) ? body.results.filter(isRecord) : [];
|
|
176
|
+
// An unknown group id is answered with an empty success, so "No results"
|
|
177
|
+
// would hide the likeliest cause: the id.
|
|
178
|
+
const noGroup = ctx.group && !images.length ? pc.dim(`No images in group ${printableLine(ctx.group)}.`) : undefined;
|
|
179
|
+
if (!images.length && !web.length) {
|
|
180
|
+
const err = envelopeError(data);
|
|
181
|
+
if (err)
|
|
182
|
+
return pc.red(`error: ${printableLine(err)}`);
|
|
183
|
+
return noGroup ?? pc.dim("No results.");
|
|
184
|
+
}
|
|
185
|
+
const shortened = { any: false };
|
|
186
|
+
const blocks = [];
|
|
187
|
+
if (images.length)
|
|
188
|
+
blocks.push(images.map((im, i) => formatImage(im, i, ctx, shortened)).join("\n\n"));
|
|
189
|
+
if (noGroup)
|
|
190
|
+
blocks.push(noGroup);
|
|
191
|
+
if (web.length) {
|
|
192
|
+
if (blocks.length)
|
|
193
|
+
blocks.push(pc.bold("Web results"));
|
|
194
|
+
blocks.push(formatResultList(web));
|
|
195
|
+
}
|
|
196
|
+
const remainder = ctx.group ? groupRemainder(images) : undefined;
|
|
197
|
+
if (remainder)
|
|
198
|
+
blocks.push(pc.dim(remainder));
|
|
199
|
+
if (shortened.any)
|
|
200
|
+
blocks.push(pc.dim("Shortened for the terminal — --json prints the values and the code in full."));
|
|
201
|
+
return blocks.join("\n\n");
|
|
202
|
+
}
|
|
@@ -12,6 +12,8 @@ export interface Envelope {
|
|
|
12
12
|
msg?: string;
|
|
13
13
|
data?: unknown;
|
|
14
14
|
}
|
|
15
|
+
/** A JSON object — not null, not an array. */
|
|
16
|
+
export declare function isRecord(v: unknown): v is Record<string, any>;
|
|
15
17
|
/** The inner body, or the input itself when it is not an envelope. */
|
|
16
18
|
export declare function unwrap(data: unknown): any;
|
|
17
19
|
/**
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/** A JSON object — not null, not an array. */
|
|
2
|
+
export function isRecord(v) {
|
|
3
|
+
return v !== null && typeof v === "object" && !Array.isArray(v);
|
|
4
|
+
}
|
|
1
5
|
/** The inner body, or the input itself when it is not an envelope. */
|
|
2
6
|
export function unwrap(data) {
|
|
3
7
|
const inner = data?.data;
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import type { SearchResult } from "../../api/search.js";
|
|
2
1
|
export declare function renderSearch(data: any): string;
|
|
3
|
-
/**
|
|
4
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Render a list of search results as numbered, titled blocks.
|
|
4
|
+
*
|
|
5
|
+
* Shared by every command that prints web results, so it takes whatever the
|
|
6
|
+
* response held: an entry that is not an object is skipped rather than taking
|
|
7
|
+
* the entries beside it down, a field of the wrong type is ignored, and the
|
|
8
|
+
* text — which is somebody else's page — is made safe for a terminal.
|
|
9
|
+
*/
|
|
10
|
+
export declare function formatResultList(results: unknown): string;
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import pc from "picocolors";
|
|
2
|
+
import { printable, printableLine } from "../../util/terminalText.js";
|
|
3
|
+
import { isRecord } from "./envelope.js";
|
|
2
4
|
const MAX_SNIPPET = 300;
|
|
3
5
|
export function renderSearch(data) {
|
|
4
6
|
// The Octen API wraps the payload in an envelope: { data: { results }, code, msg, ... }.
|
|
@@ -8,37 +10,50 @@ export function renderSearch(data) {
|
|
|
8
10
|
const body = data && typeof data === "object" && inner && typeof inner === "object" && !Array.isArray(inner)
|
|
9
11
|
? inner
|
|
10
12
|
: data;
|
|
11
|
-
|
|
13
|
+
// Entries that are not objects are dropped here, so a response made only of
|
|
14
|
+
// them reads as no results rather than as an empty listing.
|
|
15
|
+
const res = Array.isArray(body?.results) ? body.results.filter(isRecord) : [];
|
|
12
16
|
if (!res.length) {
|
|
13
17
|
// Surface app-level API errors (non-zero code) instead of a bland "No results."
|
|
14
18
|
const code = data?.code;
|
|
15
19
|
const msg = data?.msg;
|
|
16
20
|
if (code != null && code !== 0 && msg) {
|
|
17
|
-
return pc.red(`error: ${msg}`);
|
|
21
|
+
return pc.red(`error: ${printableLine(String(msg))}`);
|
|
18
22
|
}
|
|
19
23
|
return pc.dim("No results.");
|
|
20
24
|
}
|
|
21
25
|
return formatResultList(res);
|
|
22
26
|
}
|
|
23
|
-
|
|
27
|
+
const str = (v) => (typeof v === "string" ? v : "");
|
|
28
|
+
/**
|
|
29
|
+
* Render a list of search results as numbered, titled blocks.
|
|
30
|
+
*
|
|
31
|
+
* Shared by every command that prints web results, so it takes whatever the
|
|
32
|
+
* response held: an entry that is not an object is skipped rather than taking
|
|
33
|
+
* the entries beside it down, a field of the wrong type is ignored, and the
|
|
34
|
+
* text — which is somebody else's page — is made safe for a terminal.
|
|
35
|
+
*/
|
|
24
36
|
export function formatResultList(results) {
|
|
25
|
-
|
|
37
|
+
const list = Array.isArray(results) ? results.filter(isRecord) : [];
|
|
38
|
+
return list
|
|
26
39
|
.map((r, i) => {
|
|
27
40
|
const lines = [];
|
|
28
41
|
// Numbered + bold title
|
|
29
|
-
const title = r.title
|
|
42
|
+
const title = printableLine(str(r.title)) || "(no title)";
|
|
30
43
|
lines.push(`${i + 1}. ${pc.bold(title)}`);
|
|
31
44
|
// Dim URL
|
|
32
|
-
|
|
33
|
-
|
|
45
|
+
const url = printableLine(str(r.url));
|
|
46
|
+
if (url)
|
|
47
|
+
lines.push(` ${pc.dim(url)}`);
|
|
34
48
|
// Dim publication time
|
|
35
|
-
|
|
36
|
-
|
|
49
|
+
const published = printableLine(str(r.time_published));
|
|
50
|
+
if (published)
|
|
51
|
+
lines.push(` ${pc.dim(published)}`);
|
|
37
52
|
// Snippet from highlight or full_content (truncated)
|
|
38
|
-
const raw = r.highlight
|
|
53
|
+
const raw = str(r.highlight) || str(r.full_content);
|
|
39
54
|
if (raw) {
|
|
40
55
|
const snippet = raw.length > MAX_SNIPPET ? raw.slice(0, MAX_SNIPPET) + "…" : raw;
|
|
41
|
-
lines.push(` ${snippet}`);
|
|
56
|
+
lines.push(` ${printable(snippet)}`);
|
|
42
57
|
}
|
|
43
58
|
return lines.join("\n");
|
|
44
59
|
})
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Make text from a response safe to write to a terminal.
|
|
3
|
+
*
|
|
4
|
+
* Titles, descriptions and code come from somebody else's page. Written as
|
|
5
|
+
* they are, an escape sequence in them drives the terminal — clears the
|
|
6
|
+
* screen, rewrites a line already printed, retitles the window — and a
|
|
7
|
+
* carriage return lets one line overwrite the one before it. These show the
|
|
8
|
+
* character instead of sending it. `--json` output is not touched: there the
|
|
9
|
+
* same characters are already escaped by JSON.
|
|
10
|
+
*/
|
|
11
|
+
/** For text that may run over several lines, such as code. Tabs and line breaks are kept. */
|
|
12
|
+
export declare function printable(s: string): string;
|
|
13
|
+
/** For a value that belongs on one line, such as a title or a URL. */
|
|
14
|
+
export declare function printableLine(s: string): string;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Make text from a response safe to write to a terminal.
|
|
3
|
+
*
|
|
4
|
+
* Titles, descriptions and code come from somebody else's page. Written as
|
|
5
|
+
* they are, an escape sequence in them drives the terminal — clears the
|
|
6
|
+
* screen, rewrites a line already printed, retitles the window — and a
|
|
7
|
+
* carriage return lets one line overwrite the one before it. These show the
|
|
8
|
+
* character instead of sending it. `--json` output is not touched: there the
|
|
9
|
+
* same characters are already escaped by JSON.
|
|
10
|
+
*/
|
|
11
|
+
// Every C0 control but tab and line feed, DEL, the C1 controls (a lone 0x9b is
|
|
12
|
+
// a whole CSI introducer on terminals that honour them), and the bidirectional
|
|
13
|
+
// overrides, which reorder what is displayed without changing what is there.
|
|
14
|
+
// eslint-disable-next-line no-control-regex
|
|
15
|
+
const UNSAFE = /[\u0000-\u0008\u000B-\u001F\u007F-\u009F\u202A-\u202E\u2066-\u2069]/g;
|
|
16
|
+
function shown(c) {
|
|
17
|
+
const code = c.charCodeAt(0);
|
|
18
|
+
return code <= 0xff
|
|
19
|
+
? `\\x${code.toString(16).padStart(2, "0")}`
|
|
20
|
+
: `\\u${code.toString(16).padStart(4, "0")}`;
|
|
21
|
+
}
|
|
22
|
+
/** For text that may run over several lines, such as code. Tabs and line breaks are kept. */
|
|
23
|
+
export function printable(s) {
|
|
24
|
+
// A carriage return is meant as a line break, so it becomes one.
|
|
25
|
+
return s.replace(/\r\n?/g, "\n").replace(UNSAFE, shown);
|
|
26
|
+
}
|
|
27
|
+
/** For a value that belongs on one line, such as a title or a URL. */
|
|
28
|
+
export function printableLine(s) {
|
|
29
|
+
return printable(s).replace(/[\t\n]+/g, " ");
|
|
30
|
+
}
|
package/package.json
CHANGED