@c9up/polaris 0.0.0-stage → 0.1.1
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 +1146 -2
- package/dist/Polaris.d.ts +80 -0
- package/dist/Polaris.d.ts.map +1 -0
- package/dist/Polaris.js +208 -0
- package/dist/Polaris.js.map +1 -0
- package/dist/PolarisClient.d.ts +184 -0
- package/dist/PolarisClient.d.ts.map +1 -0
- package/dist/PolarisClient.js +576 -0
- package/dist/PolarisClient.js.map +1 -0
- package/dist/PolarisProvider.d.ts +40 -0
- package/dist/PolarisProvider.d.ts.map +1 -0
- package/dist/PolarisProvider.js +79 -0
- package/dist/PolarisProvider.js.map +1 -0
- package/dist/augmentations.d.ts +18 -0
- package/dist/augmentations.d.ts.map +1 -0
- package/dist/augmentations.js +9 -0
- package/dist/augmentations.js.map +1 -0
- package/dist/budget.d.ts +77 -0
- package/dist/budget.d.ts.map +1 -0
- package/dist/budget.js +134 -0
- package/dist/budget.js.map +1 -0
- package/dist/catalog/catalog.d.ts +37 -0
- package/dist/catalog/catalog.d.ts.map +1 -0
- package/dist/catalog/catalog.js +145 -0
- package/dist/catalog/catalog.js.map +1 -0
- package/dist/catalog/discover.d.ts +64 -0
- package/dist/catalog/discover.d.ts.map +1 -0
- package/dist/catalog/discover.js +148 -0
- package/dist/catalog/discover.js.map +1 -0
- package/dist/catalog/index.d.ts +8 -0
- package/dist/catalog/index.d.ts.map +1 -0
- package/dist/catalog/index.js +8 -0
- package/dist/catalog/index.js.map +1 -0
- package/dist/commands/index.d.ts +82 -0
- package/dist/commands/index.d.ts.map +1 -0
- package/dist/commands/index.js +278 -0
- package/dist/commands/index.js.map +1 -0
- package/dist/cost.d.ts +50 -0
- package/dist/cost.d.ts.map +1 -0
- package/dist/cost.js +131 -0
- package/dist/cost.js.map +1 -0
- package/dist/drivers/anthropic.d.ts +44 -0
- package/dist/drivers/anthropic.d.ts.map +1 -0
- package/dist/drivers/anthropic.js +539 -0
- package/dist/drivers/anthropic.js.map +1 -0
- package/dist/drivers/openai-compatible.d.ts +54 -0
- package/dist/drivers/openai-compatible.d.ts.map +1 -0
- package/dist/drivers/openai-compatible.js +699 -0
- package/dist/drivers/openai-compatible.js.map +1 -0
- package/dist/drivers/status.d.ts +8 -0
- package/dist/drivers/status.d.ts.map +1 -0
- package/dist/drivers/status.js +21 -0
- package/dist/drivers/status.js.map +1 -0
- package/dist/errors.d.ts +79 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +86 -0
- package/dist/errors.js.map +1 -0
- package/dist/extract.d.ts +38 -0
- package/dist/extract.d.ts.map +1 -0
- package/dist/extract.js +144 -0
- package/dist/extract.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/json-schema.d.ts +33 -0
- package/dist/json-schema.d.ts.map +1 -0
- package/dist/json-schema.js +508 -0
- package/dist/json-schema.js.map +1 -0
- package/dist/log.d.ts +47 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +35 -0
- package/dist/log.js.map +1 -0
- package/dist/mcp/client.d.ts +81 -0
- package/dist/mcp/client.d.ts.map +1 -0
- package/dist/mcp/client.js +401 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/mcp/endpoint.d.ts +54 -0
- package/dist/mcp/endpoint.d.ts.map +1 -0
- package/dist/mcp/endpoint.js +158 -0
- package/dist/mcp/endpoint.js.map +1 -0
- package/dist/mcp/host.d.ts +42 -0
- package/dist/mcp/host.d.ts.map +1 -0
- package/dist/mcp/host.js +111 -0
- package/dist/mcp/host.js.map +1 -0
- package/dist/mcp/http.d.ts +32 -0
- package/dist/mcp/http.d.ts.map +1 -0
- package/dist/mcp/http.js +190 -0
- package/dist/mcp/http.js.map +1 -0
- package/dist/mcp/index.d.ts +23 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +21 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/manifest.d.ts +36 -0
- package/dist/mcp/manifest.d.ts.map +1 -0
- package/dist/mcp/manifest.js +40 -0
- package/dist/mcp/manifest.js.map +1 -0
- package/dist/mcp/rpc.d.ts +39 -0
- package/dist/mcp/rpc.d.ts.map +1 -0
- package/dist/mcp/rpc.js +149 -0
- package/dist/mcp/rpc.js.map +1 -0
- package/dist/mcp/stdio.d.ts +24 -0
- package/dist/mcp/stdio.d.ts.map +1 -0
- package/dist/mcp/stdio.js +157 -0
- package/dist/mcp/stdio.js.map +1 -0
- package/dist/mcp/worker-main.d.ts +6 -0
- package/dist/mcp/worker-main.d.ts.map +1 -0
- package/dist/mcp/worker-main.js +17 -0
- package/dist/mcp/worker-main.js.map +1 -0
- package/dist/mcp/worker.d.ts +54 -0
- package/dist/mcp/worker.d.ts.map +1 -0
- package/dist/mcp/worker.js +166 -0
- package/dist/mcp/worker.js.map +1 -0
- package/dist/meter.d.ts +63 -0
- package/dist/meter.d.ts.map +1 -0
- package/dist/meter.js +94 -0
- package/dist/meter.js.map +1 -0
- package/dist/providers.d.ts +23 -0
- package/dist/providers.d.ts.map +1 -0
- package/dist/providers.js +21 -0
- package/dist/providers.js.map +1 -0
- package/dist/redact.d.ts +12 -0
- package/dist/redact.d.ts.map +1 -0
- package/dist/redact.js +77 -0
- package/dist/redact.js.map +1 -0
- package/dist/redis-budget.d.ts +45 -0
- package/dist/redis-budget.d.ts.map +1 -0
- package/dist/redis-budget.js +56 -0
- package/dist/redis-budget.js.map +1 -0
- package/dist/response.d.ts +39 -0
- package/dist/response.d.ts.map +1 -0
- package/dist/response.js +109 -0
- package/dist/response.js.map +1 -0
- package/dist/schema.d.ts +45 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +43 -0
- package/dist/schema.js.map +1 -0
- package/dist/server-sent.d.ts +9 -0
- package/dist/server-sent.d.ts.map +1 -0
- package/dist/server-sent.js +74 -0
- package/dist/server-sent.js.map +1 -0
- package/dist/server-tools.d.ts +12 -0
- package/dist/server-tools.d.ts.map +1 -0
- package/dist/server-tools.js +71 -0
- package/dist/server-tools.js.map +1 -0
- package/dist/services/catalog.d.ts +19 -0
- package/dist/services/catalog.d.ts.map +1 -0
- package/dist/services/catalog.js +37 -0
- package/dist/services/catalog.js.map +1 -0
- package/dist/services/main.d.ts +23 -0
- package/dist/services/main.d.ts.map +1 -0
- package/dist/services/main.js +43 -0
- package/dist/services/main.js.map +1 -0
- package/dist/skills/folder.d.ts +32 -0
- package/dist/skills/folder.d.ts.map +1 -0
- package/dist/skills/folder.js +170 -0
- package/dist/skills/folder.js.map +1 -0
- package/dist/skills/frontmatter.d.ts +15 -0
- package/dist/skills/frontmatter.d.ts.map +1 -0
- package/dist/skills/frontmatter.js +92 -0
- package/dist/skills/frontmatter.js.map +1 -0
- package/dist/skills/index.d.ts +18 -0
- package/dist/skills/index.d.ts.map +1 -0
- package/dist/skills/index.js +18 -0
- package/dist/skills/index.js.map +1 -0
- package/dist/skills/kit.d.ts +39 -0
- package/dist/skills/kit.d.ts.map +1 -0
- package/dist/skills/kit.js +124 -0
- package/dist/skills/kit.js.map +1 -0
- package/dist/skills/skill.d.ts +67 -0
- package/dist/skills/skill.d.ts.map +1 -0
- package/dist/skills/skill.js +114 -0
- package/dist/skills/skill.js.map +1 -0
- package/dist/sse.d.ts +95 -0
- package/dist/sse.d.ts.map +1 -0
- package/dist/sse.js +117 -0
- package/dist/sse.js.map +1 -0
- package/dist/testing/FakeProvider.d.ts +85 -0
- package/dist/testing/FakeProvider.d.ts.map +1 -0
- package/dist/testing/FakeProvider.js +214 -0
- package/dist/testing/FakeProvider.js.map +1 -0
- package/dist/testing/index.d.ts +7 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +7 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/tool-modules.d.ts +17 -0
- package/dist/tool-modules.d.ts.map +1 -0
- package/dist/tool-modules.js +43 -0
- package/dist/tool-modules.js.map +1 -0
- package/dist/tools.d.ts +132 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +250 -0
- package/dist/tools.js.map +1 -0
- package/dist/transport.d.ts +23 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +83 -0
- package/dist/transport.js.map +1 -0
- package/dist/types.d.ts +397 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/package.json +97 -3
- package/src/Polaris.ts +304 -0
- package/src/PolarisClient.ts +828 -0
- package/src/PolarisProvider.ts +111 -0
- package/src/augmentations.ts +20 -0
- package/src/budget.ts +201 -0
- package/src/catalog/catalog.ts +180 -0
- package/src/catalog/discover.ts +234 -0
- package/src/catalog/index.ts +19 -0
- package/src/commands/index.ts +412 -0
- package/src/cost.ts +176 -0
- package/src/drivers/anthropic.ts +703 -0
- package/src/drivers/openai-compatible.ts +907 -0
- package/src/drivers/status.ts +30 -0
- package/src/errors.ts +202 -0
- package/src/extract.ts +214 -0
- package/src/index.ts +85 -0
- package/src/json-schema.ts +597 -0
- package/src/log.ts +86 -0
- package/src/mcp/client.ts +532 -0
- package/src/mcp/endpoint.ts +229 -0
- package/src/mcp/host.ts +140 -0
- package/src/mcp/http.ts +276 -0
- package/src/mcp/index.ts +51 -0
- package/src/mcp/manifest.ts +73 -0
- package/src/mcp/rpc.ts +197 -0
- package/src/mcp/stdio.ts +190 -0
- package/src/mcp/worker-main.ts +18 -0
- package/src/mcp/worker.ts +212 -0
- package/src/meter.ts +199 -0
- package/src/providers.ts +25 -0
- package/src/redact.ts +81 -0
- package/src/redis-budget.ts +85 -0
- package/src/response.ts +143 -0
- package/src/schema.ts +88 -0
- package/src/server-sent.ts +70 -0
- package/src/server-tools.ts +77 -0
- package/src/services/catalog.ts +43 -0
- package/src/services/main.ts +49 -0
- package/src/skills/folder.ts +231 -0
- package/src/skills/frontmatter.ts +92 -0
- package/src/skills/index.ts +38 -0
- package/src/skills/kit.ts +163 -0
- package/src/skills/skill.ts +171 -0
- package/src/sse.ts +194 -0
- package/src/testing/FakeProvider.ts +322 -0
- package/src/testing/index.ts +12 -0
- package/src/tool-modules.ts +49 -0
- package/src/tools.ts +399 -0
- package/src/transport.ts +107 -0
- package/src/types.ts +434 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finding the application's tools and skills by convention: the folders and
|
|
3
|
+
* files its glob patterns match. `ream polaris:index` runs this at build and
|
|
4
|
+
* writes the result, `polaris.json`, so the running application reads a list
|
|
5
|
+
* instead of importing every tool module to learn its name.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Dirent } from "node:fs";
|
|
9
|
+
import { glob, lstat, mkdir, readdir, writeFile } from "node:fs/promises";
|
|
10
|
+
import {
|
|
11
|
+
basename,
|
|
12
|
+
dirname,
|
|
13
|
+
isAbsolute,
|
|
14
|
+
join,
|
|
15
|
+
relative,
|
|
16
|
+
resolve,
|
|
17
|
+
sep,
|
|
18
|
+
} from "node:path";
|
|
19
|
+
import { pathToFileURL } from "node:url";
|
|
20
|
+
import { E_POLARIS_INVALID_CATALOG } from "../errors.js";
|
|
21
|
+
import { readSkill } from "../skills/folder.js";
|
|
22
|
+
import { importToolModule, isToolModuleFile } from "../tool-modules.js";
|
|
23
|
+
|
|
24
|
+
/** `catalog` in `config/polaris.ts`. Paths are relative to the application. */
|
|
25
|
+
export interface CatalogConfig {
|
|
26
|
+
/**
|
|
27
|
+
* Glob patterns of tool folders or tool files. A folder holds tool
|
|
28
|
+
* modules; a file is one. Default `['app/tools']`; a modular application
|
|
29
|
+
* names its modules: `['app/modules/*\/tools', 'app/modules/*\/http/*_tool.ts']`.
|
|
30
|
+
*/
|
|
31
|
+
readonly tools?: readonly string[];
|
|
32
|
+
/**
|
|
33
|
+
* Glob patterns of skill folders: a folder with a `SKILL.md` is a skill,
|
|
34
|
+
* another holds skills, one per sub-folder. Default `['resources/skills']`.
|
|
35
|
+
*/
|
|
36
|
+
readonly skills?: readonly string[];
|
|
37
|
+
/** Where `ream polaris:index` writes the list. Default `polaris.json`. */
|
|
38
|
+
readonly manifest?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const DEFAULT_TOOL_PATTERNS: readonly string[] = ["app/tools"];
|
|
42
|
+
export const DEFAULT_SKILL_PATTERNS: readonly string[] = ["resources/skills"];
|
|
43
|
+
export const DEFAULT_MANIFEST = "polaris.json";
|
|
44
|
+
|
|
45
|
+
/** What `polaris.json` holds. Paths are relative to it, with `/`. */
|
|
46
|
+
export interface CatalogManifest {
|
|
47
|
+
readonly version: 1;
|
|
48
|
+
readonly tools: readonly CatalogTool[];
|
|
49
|
+
readonly skills: readonly CatalogSkill[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface CatalogTool {
|
|
53
|
+
readonly name: string;
|
|
54
|
+
readonly description: string;
|
|
55
|
+
/** The module that exports it. */
|
|
56
|
+
readonly module: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface CatalogSkill {
|
|
60
|
+
readonly name: string;
|
|
61
|
+
readonly description: string;
|
|
62
|
+
/** The folder that holds its `SKILL.md`. */
|
|
63
|
+
readonly folder: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface IndexCatalogOptions extends CatalogConfig {
|
|
67
|
+
/** The application's root. Default the current directory. */
|
|
68
|
+
readonly root?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A path relative to `from`, with `/`. */
|
|
72
|
+
function relativePath(from: string, to: string): string {
|
|
73
|
+
return relative(from, to).split(sep).join("/");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The paths `pattern` matches under `root`, sorted, absolute. */
|
|
77
|
+
async function matches(root: string, pattern: string): Promise<string[]> {
|
|
78
|
+
if (
|
|
79
|
+
pattern.trim() === "" ||
|
|
80
|
+
isAbsolute(pattern) ||
|
|
81
|
+
pattern.split(/[\\/]/).includes("..")
|
|
82
|
+
) {
|
|
83
|
+
throw new E_POLARIS_INVALID_CATALOG([
|
|
84
|
+
`the pattern '${pattern}' must be a path within the application`,
|
|
85
|
+
]);
|
|
86
|
+
}
|
|
87
|
+
const found: string[] = [];
|
|
88
|
+
for await (const entry of glob(pattern, { cwd: root })) {
|
|
89
|
+
found.push(join(root, entry));
|
|
90
|
+
}
|
|
91
|
+
return found.sort();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const isDirectory = async (path: string): Promise<boolean> =>
|
|
95
|
+
(await lstat(path)).isDirectory();
|
|
96
|
+
|
|
97
|
+
async function subfolders(folder: string): Promise<Dirent[]> {
|
|
98
|
+
const entries = await readdir(folder, { withFileTypes: true });
|
|
99
|
+
return entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The tool modules the patterns lead to, each once, in pattern order. */
|
|
103
|
+
async function toolFiles(
|
|
104
|
+
root: string,
|
|
105
|
+
patterns: readonly string[],
|
|
106
|
+
): Promise<string[]> {
|
|
107
|
+
const files = new Set<string>();
|
|
108
|
+
for (const pattern of patterns) {
|
|
109
|
+
for (const path of await matches(root, pattern)) {
|
|
110
|
+
if (await isDirectory(path)) {
|
|
111
|
+
for (const entry of await subfolders(path)) {
|
|
112
|
+
if (entry.isFile() && isToolModuleFile(entry.name)) {
|
|
113
|
+
files.add(join(path, entry.name));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
} else if (isToolModuleFile(basename(path))) {
|
|
117
|
+
files.add(path);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return [...files];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The skill folders the patterns lead to, each once. */
|
|
125
|
+
async function skillFolders(
|
|
126
|
+
root: string,
|
|
127
|
+
patterns: readonly string[],
|
|
128
|
+
): Promise<string[]> {
|
|
129
|
+
const folders = new Set<string>();
|
|
130
|
+
const hasSkill = (folder: string) =>
|
|
131
|
+
lstat(join(folder, "SKILL.md")).then(
|
|
132
|
+
() => true,
|
|
133
|
+
() => false,
|
|
134
|
+
);
|
|
135
|
+
for (const pattern of patterns) {
|
|
136
|
+
for (const path of await matches(root, pattern)) {
|
|
137
|
+
if (!(await isDirectory(path))) continue;
|
|
138
|
+
if (await hasSkill(path)) {
|
|
139
|
+
folders.add(path);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
for (const entry of await subfolders(path)) {
|
|
143
|
+
const folder = join(path, entry.name);
|
|
144
|
+
if (
|
|
145
|
+
entry.isDirectory() &&
|
|
146
|
+
!entry.name.startsWith(".") &&
|
|
147
|
+
(await hasSkill(folder))
|
|
148
|
+
) {
|
|
149
|
+
folders.add(folder);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return [...folders];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Find the tools and skills the patterns lead to, importing each tool module
|
|
159
|
+
* and reading each `SKILL.md` once. Throws `E_POLARIS_INVALID_CATALOG` when a
|
|
160
|
+
* module exports no tools, or when two tools or two skills share a name,
|
|
161
|
+
* naming both files; `E_POLARIS_INVALID_SKILL` for a skill folder that
|
|
162
|
+
* cannot be read. Paths in the result are relative to `base`.
|
|
163
|
+
*/
|
|
164
|
+
export async function indexCatalog(
|
|
165
|
+
options: IndexCatalogOptions = {},
|
|
166
|
+
base?: string,
|
|
167
|
+
): Promise<CatalogManifest> {
|
|
168
|
+
const root = resolve(options.root ?? ".");
|
|
169
|
+
const from = base ?? root;
|
|
170
|
+
const tools: CatalogTool[] = [];
|
|
171
|
+
const toolFile = new Map<string, string>();
|
|
172
|
+
for (const file of await toolFiles(
|
|
173
|
+
root,
|
|
174
|
+
options.tools ?? DEFAULT_TOOL_PATTERNS,
|
|
175
|
+
)) {
|
|
176
|
+
const shown = relativePath(root, file);
|
|
177
|
+
const found = await importToolModule(pathToFileURL(file).href);
|
|
178
|
+
if (typeof found === "string") {
|
|
179
|
+
throw new E_POLARIS_INVALID_CATALOG([`'${shown}' ${found}`]);
|
|
180
|
+
}
|
|
181
|
+
for (const tool of found) {
|
|
182
|
+
const other = toolFile.get(tool.name);
|
|
183
|
+
if (other !== undefined) {
|
|
184
|
+
throw new E_POLARIS_INVALID_CATALOG([
|
|
185
|
+
`two tools are named '${tool.name}': in '${other}' and in '${shown}'`,
|
|
186
|
+
]);
|
|
187
|
+
}
|
|
188
|
+
toolFile.set(tool.name, shown);
|
|
189
|
+
tools.push({
|
|
190
|
+
name: tool.name,
|
|
191
|
+
description: tool.definition.description,
|
|
192
|
+
module: relativePath(from, file),
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const skills: CatalogSkill[] = [];
|
|
198
|
+
const skillFolder = new Map<string, string>();
|
|
199
|
+
for (const folder of await skillFolders(
|
|
200
|
+
root,
|
|
201
|
+
options.skills ?? DEFAULT_SKILL_PATTERNS,
|
|
202
|
+
)) {
|
|
203
|
+
const shown = relativePath(root, folder);
|
|
204
|
+
const skill = await readSkill(folder);
|
|
205
|
+
const other = skillFolder.get(skill.name);
|
|
206
|
+
if (other !== undefined) {
|
|
207
|
+
throw new E_POLARIS_INVALID_CATALOG([
|
|
208
|
+
`two skills are named '${skill.name}': in '${other}' and in '${shown}'`,
|
|
209
|
+
]);
|
|
210
|
+
}
|
|
211
|
+
skillFolder.set(skill.name, shown);
|
|
212
|
+
skills.push({
|
|
213
|
+
name: skill.name,
|
|
214
|
+
description: skill.description,
|
|
215
|
+
folder: relativePath(from, folder),
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
return { version: 1, tools, skills };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* {@link indexCatalog}, written to the manifest (`polaris.json` at the root
|
|
223
|
+
* unless `manifest` says otherwise). Returns the manifest and its path.
|
|
224
|
+
*/
|
|
225
|
+
export async function writeCatalog(
|
|
226
|
+
options: IndexCatalogOptions = {},
|
|
227
|
+
): Promise<{ manifest: CatalogManifest; path: string }> {
|
|
228
|
+
const root = resolve(options.root ?? ".");
|
|
229
|
+
const path = resolve(root, options.manifest ?? DEFAULT_MANIFEST);
|
|
230
|
+
const manifest = await indexCatalog(options, dirname(path));
|
|
231
|
+
await mkdir(dirname(path), { recursive: true });
|
|
232
|
+
await writeFile(path, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
233
|
+
return { manifest, path };
|
|
234
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@c9up/polaris/catalog`: the application's tools and skills, found by
|
|
3
|
+
* convention in the folders `config/polaris.ts` names, listed at build in
|
|
4
|
+
* `polaris.json`, and given to a run by name.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export { Catalog } from "./catalog.js";
|
|
8
|
+
export {
|
|
9
|
+
type CatalogConfig,
|
|
10
|
+
type CatalogManifest,
|
|
11
|
+
type CatalogSkill,
|
|
12
|
+
type CatalogTool,
|
|
13
|
+
DEFAULT_MANIFEST,
|
|
14
|
+
DEFAULT_SKILL_PATTERNS,
|
|
15
|
+
DEFAULT_TOOL_PATTERNS,
|
|
16
|
+
type IndexCatalogOptions,
|
|
17
|
+
indexCatalog,
|
|
18
|
+
writeCatalog,
|
|
19
|
+
} from "./discover.js";
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@c9up/polaris/commands`: the console commands polaris ships, registered
|
|
3
|
+
* once in the application's `reamrc.ts`:
|
|
4
|
+
*
|
|
5
|
+
* commands: [() => import('@c9up/polaris/commands')]
|
|
6
|
+
*
|
|
7
|
+
* ream polaris:index write polaris.json
|
|
8
|
+
* ream make:tool canton_rate app/tools/canton_rate.ts
|
|
9
|
+
* ream make:skill payroll --module hr where the skill pattern says
|
|
10
|
+
*
|
|
11
|
+
* The shape ream's kernel loads (`getMetaData` / `getCommand`), declared here
|
|
12
|
+
* so polaris does not import `@c9up/ream`. The commands do not boot the
|
|
13
|
+
* application: they read `catalog` from `config/polaris.ts` off disk.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { existsSync } from "node:fs";
|
|
17
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
18
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
19
|
+
import { pathToFileURL } from "node:url";
|
|
20
|
+
import {
|
|
21
|
+
type CatalogConfig,
|
|
22
|
+
DEFAULT_SKILL_PATTERNS,
|
|
23
|
+
DEFAULT_TOOL_PATTERNS,
|
|
24
|
+
writeCatalog,
|
|
25
|
+
} from "../catalog/discover.js";
|
|
26
|
+
import { isObject } from "../mcp/rpc.js";
|
|
27
|
+
|
|
28
|
+
interface ArgumentMetaData {
|
|
29
|
+
readonly type: "string";
|
|
30
|
+
readonly propertyName: string;
|
|
31
|
+
readonly argumentName: string;
|
|
32
|
+
readonly description: string;
|
|
33
|
+
readonly required: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface FlagMetaData {
|
|
37
|
+
readonly type: "string" | "boolean";
|
|
38
|
+
readonly propertyName: string;
|
|
39
|
+
readonly flagName: string;
|
|
40
|
+
readonly description: string;
|
|
41
|
+
readonly alias: string[];
|
|
42
|
+
readonly required: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The static side the kernel reads. */
|
|
46
|
+
export interface PolarisCommandClass {
|
|
47
|
+
new (): { run(): Promise<void> };
|
|
48
|
+
readonly commandName: string;
|
|
49
|
+
readonly description: string;
|
|
50
|
+
readonly options: { readonly startApp: boolean };
|
|
51
|
+
readonly args: readonly ArgumentMetaData[];
|
|
52
|
+
readonly flags: readonly FlagMetaData[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** What the kernel reads to list a command without importing it. */
|
|
56
|
+
export interface CommandMetaData {
|
|
57
|
+
commandName: string;
|
|
58
|
+
namespace: string | null;
|
|
59
|
+
description: string;
|
|
60
|
+
aliases: string[];
|
|
61
|
+
options: Record<string, unknown>;
|
|
62
|
+
args: readonly unknown[];
|
|
63
|
+
flags: readonly unknown[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const flag = (
|
|
67
|
+
propertyName: string,
|
|
68
|
+
flagName: string,
|
|
69
|
+
type: FlagMetaData["type"],
|
|
70
|
+
description: string,
|
|
71
|
+
): FlagMetaData => ({
|
|
72
|
+
type,
|
|
73
|
+
propertyName,
|
|
74
|
+
flagName,
|
|
75
|
+
description,
|
|
76
|
+
alias: [],
|
|
77
|
+
required: false,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const nameArgument = (description: string): ArgumentMetaData => ({
|
|
81
|
+
type: "string",
|
|
82
|
+
propertyName: "name",
|
|
83
|
+
argumentName: "name",
|
|
84
|
+
description,
|
|
85
|
+
required: true,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const MAKE_FLAGS: readonly FlagMetaData[] = [
|
|
89
|
+
flag(
|
|
90
|
+
"module",
|
|
91
|
+
"module",
|
|
92
|
+
"string",
|
|
93
|
+
"The module to write into: replaces the `*` of the configured pattern",
|
|
94
|
+
),
|
|
95
|
+
flag("dryRun", "dry-run", "boolean", "Show the file, write nothing"),
|
|
96
|
+
flag("force", "force", "boolean", "Overwrite a file that exists"),
|
|
97
|
+
];
|
|
98
|
+
|
|
99
|
+
/** Where the commands print, so a test can listen. */
|
|
100
|
+
export interface CommandOutput {
|
|
101
|
+
log(text: string): void;
|
|
102
|
+
error(text: string): void;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let output: CommandOutput = console;
|
|
106
|
+
|
|
107
|
+
/** @internal Send the commands' output elsewhere (tests). */
|
|
108
|
+
export function setCommandOutput(value: CommandOutput): void {
|
|
109
|
+
output = value;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const fail = (message: string): void => {
|
|
113
|
+
output.error(`[polaris] ${message}`);
|
|
114
|
+
process.exitCode = 1;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The `catalog` of `config/polaris.ts`, read off disk. A config that cannot
|
|
119
|
+
* be imported gives the defaults, and the reason is printed.
|
|
120
|
+
*/
|
|
121
|
+
export async function projectCatalogConfig(
|
|
122
|
+
cwd = process.cwd(),
|
|
123
|
+
): Promise<CatalogConfig | false> {
|
|
124
|
+
const file = join(cwd, "config", "polaris.ts");
|
|
125
|
+
if (!existsSync(file)) return {};
|
|
126
|
+
try {
|
|
127
|
+
const module: unknown = await import(pathToFileURL(file).href);
|
|
128
|
+
const config = isObject(module) ? module.default : undefined;
|
|
129
|
+
const catalog = isObject(config) ? config.catalog : undefined;
|
|
130
|
+
if (catalog === false) return false;
|
|
131
|
+
return isCatalogConfig(catalog) ? catalog : {};
|
|
132
|
+
} catch (error) {
|
|
133
|
+
output.error(
|
|
134
|
+
`[polaris] config/polaris.ts could not be read, the default folders are used: ${error instanceof Error ? error.message : String(error)}`,
|
|
135
|
+
);
|
|
136
|
+
return {};
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const isPatterns = (value: unknown): boolean =>
|
|
141
|
+
value === undefined ||
|
|
142
|
+
(Array.isArray(value) && value.every((item) => typeof item === "string"));
|
|
143
|
+
|
|
144
|
+
function isCatalogConfig(value: unknown): value is CatalogConfig {
|
|
145
|
+
return (
|
|
146
|
+
isObject(value) &&
|
|
147
|
+
isPatterns(value.tools) &&
|
|
148
|
+
isPatterns(value.skills) &&
|
|
149
|
+
(value.manifest === undefined || typeof value.manifest === "string")
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** `CantonRate`, `canton-rate`, `canton rate` -> `canton_rate`. */
|
|
154
|
+
export function snakeCase(name: string): string {
|
|
155
|
+
return name
|
|
156
|
+
.trim()
|
|
157
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
|
|
158
|
+
.replace(/[\s-]+/g, "_")
|
|
159
|
+
.toLowerCase();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The path a generator writes to, from the first pattern that fits:
|
|
164
|
+
* with `module`, the first with a `*` in a folder, which `module` replaces;
|
|
165
|
+
* without, the first with none. A `*` left in the last segment of a file
|
|
166
|
+
* pattern takes `file`; a folder pattern gets `file` appended.
|
|
167
|
+
*/
|
|
168
|
+
export function destination(
|
|
169
|
+
patterns: readonly string[],
|
|
170
|
+
module: string | undefined,
|
|
171
|
+
file: string,
|
|
172
|
+
): string | { problem: string } {
|
|
173
|
+
const folderStar = (pattern: string) =>
|
|
174
|
+
pattern
|
|
175
|
+
.split("/")
|
|
176
|
+
.slice(0, -1)
|
|
177
|
+
.some((part) => part.includes("*")) ||
|
|
178
|
+
(!isFilePattern(pattern) && pattern.includes("*"));
|
|
179
|
+
const pattern =
|
|
180
|
+
module === undefined
|
|
181
|
+
? patterns.find((item) => !folderStar(item))
|
|
182
|
+
: patterns.find(folderStar);
|
|
183
|
+
if (pattern === undefined) {
|
|
184
|
+
return {
|
|
185
|
+
problem:
|
|
186
|
+
module === undefined
|
|
187
|
+
? `every pattern (${patterns.join(", ")}) names a module: give --module`
|
|
188
|
+
: `no pattern (${patterns.join(", ")}) has a '*' for a module`,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
let path = module === undefined ? pattern : pattern.replace("*", module);
|
|
192
|
+
if (isFilePattern(path)) {
|
|
193
|
+
const at = path.lastIndexOf("/") + 1;
|
|
194
|
+
const extension = path.slice(path.search(/\.[mc]?[jt]s$/));
|
|
195
|
+
const last = path.slice(at, path.length - extension.length);
|
|
196
|
+
path = `${path.slice(0, at)}${last.includes("*") ? last.replace("*", file) : file}${extension}`;
|
|
197
|
+
} else {
|
|
198
|
+
path = `${path.replace(/\/+$/, "")}/${file}.ts`;
|
|
199
|
+
}
|
|
200
|
+
if (/[*?[\]{}]/.test(path)) {
|
|
201
|
+
return {
|
|
202
|
+
problem: `the pattern '${pattern}' does not say where to write: use a plainer one first`,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
return path;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function isFilePattern(pattern: string): boolean {
|
|
209
|
+
return /\.[mc]?[jt]s$/.test(pattern);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const MODULE_NAME = /^[a-z0-9][a-z0-9_-]*$/i;
|
|
213
|
+
|
|
214
|
+
/** Write `content` to `path` (relative to the cwd), honouring the flags. */
|
|
215
|
+
async function generate(
|
|
216
|
+
path: string,
|
|
217
|
+
content: string,
|
|
218
|
+
options: { dryRun: boolean; force: boolean },
|
|
219
|
+
): Promise<void> {
|
|
220
|
+
const absolute = resolve(path);
|
|
221
|
+
const exists = existsSync(absolute);
|
|
222
|
+
if (options.dryRun) {
|
|
223
|
+
output.log(`${path}${exists ? " (exists)" : ""} - dry run`);
|
|
224
|
+
output.log(content);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (exists && !options.force) {
|
|
228
|
+
output.log(`skip ${path}: the file exists, use --force`);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
await mkdir(dirname(absolute), { recursive: true });
|
|
232
|
+
await writeFile(absolute, content);
|
|
233
|
+
output.log(`create ${path}`);
|
|
234
|
+
output.log("Run `ream polaris:index` to add it to polaris.json.");
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export const TOOL_STUB = (
|
|
238
|
+
name: string,
|
|
239
|
+
): string => `import { defineTool } from '@c9up/polaris'
|
|
240
|
+
|
|
241
|
+
export default defineTool({
|
|
242
|
+
name: '${name}',
|
|
243
|
+
description: 'TODO: what the tool does, and when the model should call it.',
|
|
244
|
+
inputSchema: {
|
|
245
|
+
type: 'object',
|
|
246
|
+
properties: {},
|
|
247
|
+
additionalProperties: false,
|
|
248
|
+
},
|
|
249
|
+
run: async () => {
|
|
250
|
+
return 'TODO'
|
|
251
|
+
},
|
|
252
|
+
})
|
|
253
|
+
`;
|
|
254
|
+
|
|
255
|
+
export const SKILL_STUB = (name: string): string => `---
|
|
256
|
+
name: ${name}
|
|
257
|
+
description: TODO: what the skill is for, and when the model should load it.
|
|
258
|
+
---
|
|
259
|
+
# ${name}
|
|
260
|
+
|
|
261
|
+
TODO: the instructions the model follows once it loads this skill. Files
|
|
262
|
+
beside this one can be read with read_skill_file; tools go in tools/.
|
|
263
|
+
`;
|
|
264
|
+
|
|
265
|
+
class IndexCommand {
|
|
266
|
+
static readonly commandName = "polaris:index";
|
|
267
|
+
static readonly description =
|
|
268
|
+
"List the application's tools and skills in polaris.json";
|
|
269
|
+
static readonly options = { startApp: false };
|
|
270
|
+
static readonly args: readonly ArgumentMetaData[] = [];
|
|
271
|
+
static readonly flags: readonly FlagMetaData[] = [];
|
|
272
|
+
|
|
273
|
+
async run(): Promise<void> {
|
|
274
|
+
const config = await projectCatalogConfig();
|
|
275
|
+
if (config === false) {
|
|
276
|
+
fail("`catalog` is false in config/polaris.ts: nothing to index");
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
try {
|
|
280
|
+
const { manifest, path } = await writeCatalog(config);
|
|
281
|
+
output.log(
|
|
282
|
+
`Wrote ${relative(process.cwd(), path)}: ${manifest.tools.length} tool(s), ${manifest.skills.length} skill(s).`,
|
|
283
|
+
);
|
|
284
|
+
} catch (error) {
|
|
285
|
+
fail(error instanceof Error ? error.message : String(error));
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
class MakeToolCommand {
|
|
291
|
+
static readonly commandName = "make:tool";
|
|
292
|
+
static readonly description = "Generate a polaris tool";
|
|
293
|
+
static readonly options = { startApp: false };
|
|
294
|
+
static readonly args = [
|
|
295
|
+
nameArgument("The tool's name, as the model sees it"),
|
|
296
|
+
];
|
|
297
|
+
static readonly flags = MAKE_FLAGS;
|
|
298
|
+
|
|
299
|
+
declare name: string;
|
|
300
|
+
declare module: string | undefined;
|
|
301
|
+
declare dryRun: boolean | undefined;
|
|
302
|
+
declare force: boolean | undefined;
|
|
303
|
+
|
|
304
|
+
async run(): Promise<void> {
|
|
305
|
+
const name = snakeCase(this.name);
|
|
306
|
+
if (!/^[a-z][a-z0-9_]{0,63}$/.test(name)) {
|
|
307
|
+
fail(
|
|
308
|
+
`'${this.name}' is not a tool name: letters, digits and underscores, 64 at most, starting with a letter`,
|
|
309
|
+
);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
await make(this, name, name, "tools", TOOL_STUB(name));
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
class MakeSkillCommand {
|
|
317
|
+
static readonly commandName = "make:skill";
|
|
318
|
+
static readonly description = "Generate a polaris skill: a SKILL.md folder";
|
|
319
|
+
static readonly options = { startApp: false };
|
|
320
|
+
static readonly args = [nameArgument("The skill's name")];
|
|
321
|
+
static readonly flags = MAKE_FLAGS;
|
|
322
|
+
|
|
323
|
+
declare name: string;
|
|
324
|
+
declare module: string | undefined;
|
|
325
|
+
declare dryRun: boolean | undefined;
|
|
326
|
+
declare force: boolean | undefined;
|
|
327
|
+
|
|
328
|
+
async run(): Promise<void> {
|
|
329
|
+
const name = snakeCase(this.name).replace(/_/g, "-");
|
|
330
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name) || name.length > 64) {
|
|
331
|
+
fail(
|
|
332
|
+
`'${this.name}' is not a skill name: lower-case letters, digits and single hyphens, 64 at most`,
|
|
333
|
+
);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
await make(this, name, `${name}/SKILL`, "skills", SKILL_STUB(name));
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
async function make(
|
|
341
|
+
command: {
|
|
342
|
+
module: string | undefined;
|
|
343
|
+
dryRun: boolean | undefined;
|
|
344
|
+
force: boolean | undefined;
|
|
345
|
+
},
|
|
346
|
+
name: string,
|
|
347
|
+
file: string,
|
|
348
|
+
kind: "tools" | "skills",
|
|
349
|
+
content: string,
|
|
350
|
+
): Promise<void> {
|
|
351
|
+
if (command.module !== undefined && !MODULE_NAME.test(command.module)) {
|
|
352
|
+
fail(`'${command.module}' is not a module name`);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const config = await projectCatalogConfig();
|
|
356
|
+
const patterns =
|
|
357
|
+
config === false
|
|
358
|
+
? []
|
|
359
|
+
: (config[kind] ??
|
|
360
|
+
(kind === "tools" ? DEFAULT_TOOL_PATTERNS : DEFAULT_SKILL_PATTERNS));
|
|
361
|
+
if (patterns.length === 0) {
|
|
362
|
+
fail(`config/polaris.ts lists no ${kind} folder`);
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
// A skill is a folder: its SKILL.md never fits a file pattern.
|
|
366
|
+
const usable =
|
|
367
|
+
kind === "skills"
|
|
368
|
+
? patterns.filter((pattern) => !isFilePattern(pattern))
|
|
369
|
+
: patterns;
|
|
370
|
+
const found = destination(usable, command.module, file);
|
|
371
|
+
if (typeof found !== "string") {
|
|
372
|
+
fail(`cannot place '${name}': ${found.problem}`);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
const path = kind === "skills" ? found.replace(/\.ts$/, ".md") : found;
|
|
376
|
+
await generate(path, content, {
|
|
377
|
+
dryRun: command.dryRun === true,
|
|
378
|
+
force: command.force === true,
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const COMMANDS: readonly PolarisCommandClass[] = [
|
|
383
|
+
IndexCommand,
|
|
384
|
+
MakeToolCommand,
|
|
385
|
+
MakeSkillCommand,
|
|
386
|
+
];
|
|
387
|
+
|
|
388
|
+
function serialize(command: PolarisCommandClass): CommandMetaData {
|
|
389
|
+
const colon = command.commandName.indexOf(":");
|
|
390
|
+
return {
|
|
391
|
+
commandName: command.commandName,
|
|
392
|
+
namespace: colon === -1 ? null : command.commandName.slice(0, colon),
|
|
393
|
+
description: command.description,
|
|
394
|
+
aliases: [],
|
|
395
|
+
options: { ...command.options },
|
|
396
|
+
args: command.args,
|
|
397
|
+
flags: command.flags,
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export async function getMetaData(): Promise<CommandMetaData[]> {
|
|
402
|
+
return COMMANDS.map(serialize);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export async function getCommand(
|
|
406
|
+
metadata: CommandMetaData,
|
|
407
|
+
): Promise<PolarisCommandClass | null> {
|
|
408
|
+
return (
|
|
409
|
+
COMMANDS.find((command) => command.commandName === metadata.commandName) ??
|
|
410
|
+
null
|
|
411
|
+
);
|
|
412
|
+
}
|