fito-tui 0.10.0 → 0.11.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.
@@ -0,0 +1,143 @@
1
+ import { existsSync, readdirSync, readFileSync } from "node:fs";
2
+ import { dirname, join, resolve } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+
5
+ import { parse as parseYaml } from "yaml";
6
+ import { z } from "zod";
7
+
8
+ /**
9
+ * The installed Fito package's own agent-facing files, as `/fito-docs` reads them when the guide
10
+ * does not cover a TUI question (Task 17.35, decision b): its prompts (`prompts/*.md`), its skills
11
+ * (`skills/*\/SKILL.md`) and its workers (`agents/*.md`). Nothing else.
12
+ *
13
+ * **A closed set, named by kind and name, never by path.** A caller names a kind — `prompt`,
14
+ * `skill`, `worker` — and a name; the file comes from a listing this module builds itself, so what
15
+ * a worker can reach is a property of this code, as `shared/case-briefing/` makes it for the tutor.
16
+ * A name is a single kebab-case word, so `../`, a slash, a dot or a file extension is refused before
17
+ * the disk is touched. Tests, `__fixtures__` folders and every non-`.md` file are never listed, and
18
+ * so never served; nor is `~/.fito/`, nor any file outside the package.
19
+ *
20
+ * **The package root comes from this module's own location** (`import.meta.url`, two folders up).
21
+ * That holds wherever the package runs: from a developer's clone, from the built `.js` under Node,
22
+ * and from `node_modules`, where pi loads the extension through its own loader (jiti), which gives
23
+ * each module its real file location.
24
+ */
25
+
26
+ export const PACKAGE_KINDS = ["prompt", "skill", "worker"] as const;
27
+ export type PackageKind = (typeof PACKAGE_KINDS)[number];
28
+
29
+ /** A prompt as the listing shows it: the command a user types, and its front matter. */
30
+ export interface PackagePrompt {
31
+ command: string;
32
+ description: string;
33
+ argumentHint: string | null;
34
+ }
35
+
36
+ /** A skill or a worker as the listing shows it. */
37
+ export interface PackageNamedEntry {
38
+ name: string;
39
+ description: string;
40
+ }
41
+
42
+ export interface PackageListing {
43
+ prompts: PackagePrompt[];
44
+ skills: PackageNamedEntry[];
45
+ workers: PackageNamedEntry[];
46
+ }
47
+
48
+ /** One file, read: its kind, its name, and its text as written, front matter included. */
49
+ export interface PackageFile {
50
+ kind: PackageKind;
51
+ name: string;
52
+ text: string;
53
+ }
54
+
55
+ export interface PackageOptions {
56
+ /** The package root; defaults to `packageRoot()`. Tests pass a fixture. */
57
+ root?: string;
58
+ }
59
+
60
+ /** A name: one kebab-case word, as Fito names every prompt, skill and worker (`fito-tutor`). */
61
+ const NAME = /^[a-z0-9][a-z0-9-]*$/;
62
+
63
+ /** The front matter fields the listing shows. A field that is absent or not text reads as empty. */
64
+ const frontMatterSchema = z.object({
65
+ description: z.string().catch(""),
66
+ "argument-hint": z.string().nullable().catch(null),
67
+ });
68
+
69
+ /** The installed package's root: this module lives at `<root>/shared/tui-package/`. */
70
+ export function packageRoot(): string {
71
+ return resolve(dirname(fileURLToPath(import.meta.url)), "..", "..");
72
+ }
73
+
74
+ /** The folder each kind lives in. */
75
+ const KIND_FOLDERS: Record<PackageKind, string> = { prompt: "prompts", skill: "skills", worker: "agents" };
76
+
77
+ /**
78
+ * Each name of one kind, with its file — the closed set every read goes through. A name that is not
79
+ * a kebab-case word (a test, a fixture folder) is skipped, so it is neither listed nor served.
80
+ */
81
+ function filesOf(kind: PackageKind, root: string): Map<string, string> {
82
+ const folder = join(root, KIND_FOLDERS[kind]);
83
+ const files = new Map<string, string>();
84
+ if (!existsSync(folder)) return files;
85
+ for (const entry of readdirSync(folder, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
86
+ if (kind === "skill") {
87
+ const skillFile = join(folder, entry.name, "SKILL.md");
88
+ if (entry.isDirectory() && NAME.test(entry.name) && existsSync(skillFile)) files.set(entry.name, skillFile);
89
+ } else if (entry.isFile() && entry.name.endsWith(".md")) {
90
+ const name = entry.name.slice(0, -".md".length);
91
+ if (NAME.test(name)) files.set(name, join(folder, entry.name));
92
+ }
93
+ }
94
+ return files;
95
+ }
96
+
97
+ /** A file's front matter, or nothing when it has none or it does not parse. */
98
+ function frontMatterOf(file: string): z.infer<typeof frontMatterSchema> {
99
+ const text = readFileSync(file, "utf8").replace(/\r\n/g, "\n");
100
+ const block = text.match(/^---\n([\s\S]*?)\n---\n/);
101
+ let parsed: unknown = {};
102
+ try {
103
+ parsed = block ? (parseYaml(block[1]) ?? {}) : {};
104
+ } catch {
105
+ parsed = {};
106
+ }
107
+ return frontMatterSchema.parse(typeof parsed === "object" ? parsed : {});
108
+ }
109
+
110
+ /** Every prompt, skill and worker the package holds, each with its description. */
111
+ export function listPackage(options: PackageOptions = {}): PackageListing {
112
+ const root = options.root ?? packageRoot();
113
+ const named = (kind: PackageKind): PackageNamedEntry[] =>
114
+ [...filesOf(kind, root)].map(([name, file]) => ({ name, description: frontMatterOf(file).description }));
115
+ return {
116
+ prompts: [...filesOf("prompt", root)].map(([name, file]) => {
117
+ const front = frontMatterOf(file);
118
+ return { command: `/${name}`, description: front.description, argumentHint: front["argument-hint"] };
119
+ }),
120
+ skills: named("skill"),
121
+ workers: named("worker"),
122
+ };
123
+ }
124
+
125
+ /**
126
+ * Read one file by kind and name: a prompt by its command (`fito-tutor` or `/fito-tutor`), a skill
127
+ * by its folder, a worker by its file. A name that is not a kebab-case word, or that the listing
128
+ * does not hold, is refused.
129
+ */
130
+ export function readPackageFile(kind: PackageKind, name: string, options: PackageOptions = {}): PackageFile {
131
+ if (!PACKAGE_KINDS.includes(kind)) {
132
+ throw new Error(`"${kind}" is not a kind of Fito file — use one of: ${PACKAGE_KINDS.join(", ")}`);
133
+ }
134
+ const bare = kind === "prompt" ? name.trim().replace(/^\//, "") : name.trim();
135
+ if (!NAME.test(bare)) {
136
+ throw new Error(`"${name}" is not a name — name a ${kind} as the listing shows it, never a path or a file`);
137
+ }
138
+ const file = filesOf(kind, options.root ?? packageRoot()).get(bare);
139
+ if (file === undefined) {
140
+ throw new Error(`Fito has no ${kind} named "${bare}" — list the package for the names it holds`);
141
+ }
142
+ return { kind, name: bare, text: readFileSync(file, "utf8") };
143
+ }