@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,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The checks every provider's server tools share: what a request's `server`
|
|
3
|
+
* must look like before a driver turns it into its own fields.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { ChatRequest, EmbedRequest, ServerTools } from "./types.js";
|
|
7
|
+
|
|
8
|
+
const REDACTED = "[redacted]";
|
|
9
|
+
|
|
10
|
+
/** What is wrong with `server`, or undefined when nothing is. */
|
|
11
|
+
export function serverToolsProblem(server: ServerTools): string | undefined {
|
|
12
|
+
if (server.container !== undefined && !isName(server.container)) {
|
|
13
|
+
return "`server.container` must be a container id";
|
|
14
|
+
}
|
|
15
|
+
for (const skill of server.skills ?? []) {
|
|
16
|
+
if (skill.source !== "anthropic" && skill.source !== "custom") {
|
|
17
|
+
return "a server skill's `source` must be 'anthropic' or 'custom'";
|
|
18
|
+
}
|
|
19
|
+
if (!isName(skill.id)) return "a server skill needs an `id`";
|
|
20
|
+
if (skill.version !== undefined && !isName(skill.version)) {
|
|
21
|
+
return `server skill '${skill.id}' has an empty \`version\``;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const names = new Set<string>();
|
|
25
|
+
for (const mcp of server.mcpServers ?? []) {
|
|
26
|
+
if (!isName(mcp.name)) return "an MCP server needs a `name`";
|
|
27
|
+
if (names.has(mcp.name)) return `two MCP servers are named '${mcp.name}'`;
|
|
28
|
+
names.add(mcp.name);
|
|
29
|
+
if (!isHttps(mcp.url)) {
|
|
30
|
+
return `MCP server '${mcp.name}' needs an https:// \`url\``;
|
|
31
|
+
}
|
|
32
|
+
if (mcp.tools !== undefined && !mcp.tools.every(isName)) {
|
|
33
|
+
return `MCP server '${mcp.name}' lists a tool without a name`;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The MCP tokens a request carries, to keep out of errors. */
|
|
40
|
+
export function serverSecrets(server: ServerTools | undefined): string[] {
|
|
41
|
+
return (server?.mcpServers ?? []).flatMap((mcp) =>
|
|
42
|
+
typeof mcp.authorizationToken === "string" ? [mcp.authorizationToken] : [],
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** `request` with its MCP tokens replaced, for a log. */
|
|
47
|
+
export function withoutServerSecrets(
|
|
48
|
+
request: ChatRequest | EmbedRequest,
|
|
49
|
+
): ChatRequest | EmbedRequest {
|
|
50
|
+
if (!("messages" in request)) return request;
|
|
51
|
+
const server = request.server;
|
|
52
|
+
if (server?.mcpServers === undefined) return request;
|
|
53
|
+
return {
|
|
54
|
+
...request,
|
|
55
|
+
server: {
|
|
56
|
+
...server,
|
|
57
|
+
mcpServers: server.mcpServers.map((mcp) =>
|
|
58
|
+
mcp.authorizationToken === undefined
|
|
59
|
+
? mcp
|
|
60
|
+
: { ...mcp, authorizationToken: REDACTED },
|
|
61
|
+
),
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function isName(value: unknown): value is string {
|
|
67
|
+
return typeof value === "string" && value.trim() !== "";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function isHttps(url: unknown): boolean {
|
|
71
|
+
if (typeof url !== "string") return false;
|
|
72
|
+
try {
|
|
73
|
+
return new URL(url).protocol === "https:";
|
|
74
|
+
} catch {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The application's tool and skill catalog, ready to import:
|
|
3
|
+
*
|
|
4
|
+
* import catalog from '@c9up/polaris/services/catalog'
|
|
5
|
+
*
|
|
6
|
+
* await polaris.run({ messages, tools: await catalog.tools(['canton_rate']) })
|
|
7
|
+
*
|
|
8
|
+
* Read from `polaris.json` by `PolarisProvider.boot()`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { Catalog } from "../catalog/catalog.js";
|
|
12
|
+
import { E_POLARIS_NOT_BOOTED } from "../errors.js";
|
|
13
|
+
|
|
14
|
+
let instance: Catalog | undefined;
|
|
15
|
+
|
|
16
|
+
/** @internal Bind the catalog (called by PolarisProvider). */
|
|
17
|
+
export function setCatalog(value: Catalog): void {
|
|
18
|
+
instance = value;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** @internal Read the catalog, or `undefined` before boot. */
|
|
22
|
+
export function getCatalog(): Catalog | undefined {
|
|
23
|
+
return instance;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** @internal Release the catalog; the caller checks it still owns it. */
|
|
27
|
+
export function clearCatalog(): void {
|
|
28
|
+
instance = undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Never read through: every trap forwards to `instance`.
|
|
32
|
+
const target: Catalog = Object.create(null);
|
|
33
|
+
|
|
34
|
+
const catalog: Catalog = new Proxy(target, {
|
|
35
|
+
get(_target, prop) {
|
|
36
|
+
if (typeof prop === "symbol" || prop === "then") return undefined;
|
|
37
|
+
if (!instance) throw new E_POLARIS_NOT_BOOTED();
|
|
38
|
+
const value = Reflect.get(instance, prop, instance);
|
|
39
|
+
return typeof value === "function" ? value.bind(instance) : value;
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
export default catalog;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The application's polaris manager, ready to import:
|
|
3
|
+
*
|
|
4
|
+
* import polaris from '@c9up/polaris/services/main'
|
|
5
|
+
*
|
|
6
|
+
* const reply = await polaris.use('claude').chat({ messages })
|
|
7
|
+
*
|
|
8
|
+
* Bound by `PolarisProvider.boot()`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { E_POLARIS_NOT_BOOTED } from "../errors.js";
|
|
12
|
+
import type { PolarisService } from "../Polaris.js";
|
|
13
|
+
|
|
14
|
+
let instance: PolarisService | undefined;
|
|
15
|
+
|
|
16
|
+
/** @internal Bind the singleton (called by PolarisProvider). */
|
|
17
|
+
export function setPolaris(value: PolarisService): void {
|
|
18
|
+
instance = value;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** @internal Read the singleton, or `undefined` before boot. */
|
|
22
|
+
export function getPolaris(): PolarisService | undefined {
|
|
23
|
+
return instance;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @internal Release the singleton, so a stopped application leaves no manager
|
|
28
|
+
* reachable here. The caller checks it still owns it: two applications share
|
|
29
|
+
* this module in one process.
|
|
30
|
+
*/
|
|
31
|
+
export function clearPolaris(): void {
|
|
32
|
+
instance = undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Never read through: every trap forwards to `instance`.
|
|
36
|
+
const target: PolarisService = Object.create(null);
|
|
37
|
+
|
|
38
|
+
const polaris: PolarisService = new Proxy(target, {
|
|
39
|
+
get(_target, prop) {
|
|
40
|
+
// A module loader reads `then` and a few symbols on what it imports,
|
|
41
|
+
// before anyone uses it. Throwing there would crash the import itself.
|
|
42
|
+
if (typeof prop === "symbol" || prop === "then") return undefined;
|
|
43
|
+
if (!instance) throw new E_POLARIS_NOT_BOOTED();
|
|
44
|
+
const value = Reflect.get(instance, prop, instance);
|
|
45
|
+
return typeof value === "function" ? value.bind(instance) : value;
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
export default polaris;
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A skill read from a folder: a `SKILL.md` (frontmatter `name` and
|
|
3
|
+
* `description`, then the instructions), the files beside it, which the model
|
|
4
|
+
* may read, and the JS modules of its `tools/` folder. Nothing else in the
|
|
5
|
+
* folder runs: a `scripts/` folder is only text the model can read.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Dirent } from "node:fs";
|
|
9
|
+
import { lstat, readdir, readFile, realpath } from "node:fs/promises";
|
|
10
|
+
import { basename, isAbsolute, join, resolve, sep } from "node:path";
|
|
11
|
+
import { pathToFileURL } from "node:url";
|
|
12
|
+
import { E_POLARIS_INVALID_SKILL } from "../errors.js";
|
|
13
|
+
import { importToolModule, isToolModuleFile } from "../tool-modules.js";
|
|
14
|
+
import type { PolarisTool } from "../tools.js";
|
|
15
|
+
import { parseSkillDocument } from "./frontmatter.js";
|
|
16
|
+
import {
|
|
17
|
+
MAX_SKILL_BYTES,
|
|
18
|
+
type PolarisSkill,
|
|
19
|
+
SkillFileError,
|
|
20
|
+
type SkillFiles,
|
|
21
|
+
skillProblem,
|
|
22
|
+
toolsProblem,
|
|
23
|
+
} from "./skill.js";
|
|
24
|
+
|
|
25
|
+
/** The most files of a skill listed to the model. */
|
|
26
|
+
export const MAX_SKILL_FILES = 500;
|
|
27
|
+
const MAX_DEPTH = 8;
|
|
28
|
+
|
|
29
|
+
export interface ReadSkillOptions {
|
|
30
|
+
/**
|
|
31
|
+
* Import the modules of `tools/`, which run in the application's process
|
|
32
|
+
* like its own code. Default `true`. Pass `false` for a skill you did not
|
|
33
|
+
* write: the model then gets its instructions and files only.
|
|
34
|
+
*/
|
|
35
|
+
readonly importTools?: boolean;
|
|
36
|
+
/** Tools the application adds to the skill: an MCP server's (`client.tools`)… */
|
|
37
|
+
readonly tools?: readonly PolarisTool[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Read the skill in `directory`. Throws `E_POLARIS_INVALID_SKILL` when its
|
|
42
|
+
* `SKILL.md` is missing, too large or lacks `name` or `description`, or when
|
|
43
|
+
* a tool module exports no tools.
|
|
44
|
+
*/
|
|
45
|
+
export async function readSkill(
|
|
46
|
+
directory: string,
|
|
47
|
+
options: ReadSkillOptions = {},
|
|
48
|
+
): Promise<PolarisSkill> {
|
|
49
|
+
const fail = (reason: string): never => {
|
|
50
|
+
throw new E_POLARIS_INVALID_SKILL([directory, reason]);
|
|
51
|
+
};
|
|
52
|
+
const root = await realpath(resolve(directory)).catch(() =>
|
|
53
|
+
fail("the folder does not exist"),
|
|
54
|
+
);
|
|
55
|
+
const document = await readText(join(root, "SKILL.md")).catch(
|
|
56
|
+
(error: unknown) =>
|
|
57
|
+
fail(
|
|
58
|
+
error instanceof SkillFileError ? error.message : "it has no SKILL.md",
|
|
59
|
+
),
|
|
60
|
+
);
|
|
61
|
+
const parsed = parseSkillDocument(document);
|
|
62
|
+
if (parsed === undefined) {
|
|
63
|
+
return fail("SKILL.md does not start with a --- frontmatter");
|
|
64
|
+
}
|
|
65
|
+
const { name, description } = parsed.data;
|
|
66
|
+
const problem = skillProblem(name, description, parsed.body);
|
|
67
|
+
if (
|
|
68
|
+
problem !== undefined ||
|
|
69
|
+
name === undefined ||
|
|
70
|
+
description === undefined
|
|
71
|
+
) {
|
|
72
|
+
return fail(problem ?? "");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const imported =
|
|
76
|
+
options.importTools === false ? [] : await toolModules(root, fail);
|
|
77
|
+
const tools = [...imported, ...(options.tools ?? [])];
|
|
78
|
+
const toolProblem = toolsProblem(name, tools);
|
|
79
|
+
if (toolProblem !== undefined) fail(toolProblem);
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
name,
|
|
83
|
+
description: description.trim(),
|
|
84
|
+
instructions: parsed.body,
|
|
85
|
+
tools,
|
|
86
|
+
files: folderFiles(root, name),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Read every skill of `directory`: each sub-folder that holds a `SKILL.md`.
|
|
92
|
+
* Two skills under one name throw `E_POLARIS_INVALID_SKILL`.
|
|
93
|
+
*/
|
|
94
|
+
export async function readSkills(
|
|
95
|
+
directory: string,
|
|
96
|
+
options: Omit<ReadSkillOptions, "tools"> = {},
|
|
97
|
+
): Promise<PolarisSkill[]> {
|
|
98
|
+
const entries = await readdir(directory, { withFileTypes: true }).catch(
|
|
99
|
+
() => {
|
|
100
|
+
throw new E_POLARIS_INVALID_SKILL([
|
|
101
|
+
directory,
|
|
102
|
+
"the folder does not exist",
|
|
103
|
+
]);
|
|
104
|
+
},
|
|
105
|
+
);
|
|
106
|
+
const skills: PolarisSkill[] = [];
|
|
107
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
108
|
+
if (!entry.isDirectory() || entry.name.startsWith(".")) continue;
|
|
109
|
+
const folder = join(directory, entry.name);
|
|
110
|
+
const marker = await lstat(join(folder, "SKILL.md")).catch(() => undefined);
|
|
111
|
+
if (marker === undefined) continue;
|
|
112
|
+
const skill = await readSkill(folder, options);
|
|
113
|
+
const twin = skills.find((other) => other.name === skill.name);
|
|
114
|
+
if (twin !== undefined) {
|
|
115
|
+
throw new E_POLARIS_INVALID_SKILL([
|
|
116
|
+
folder,
|
|
117
|
+
`another skill of '${directory}' is named '${skill.name}'`,
|
|
118
|
+
]);
|
|
119
|
+
}
|
|
120
|
+
skills.push(skill);
|
|
121
|
+
}
|
|
122
|
+
return skills;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The tools exported by the modules of `tools/`, in file order. */
|
|
126
|
+
async function toolModules(
|
|
127
|
+
root: string,
|
|
128
|
+
fail: (reason: string) => never,
|
|
129
|
+
): Promise<PolarisTool[]> {
|
|
130
|
+
const folder = join(root, "tools");
|
|
131
|
+
let entries: Dirent[];
|
|
132
|
+
try {
|
|
133
|
+
entries = await readdir(folder, { withFileTypes: true });
|
|
134
|
+
} catch {
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
137
|
+
const tools: PolarisTool[] = [];
|
|
138
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
139
|
+
if (!entry.isFile() || !isToolModuleFile(entry.name)) continue;
|
|
140
|
+
const found = await importToolModule(
|
|
141
|
+
pathToFileURL(join(folder, entry.name)).href,
|
|
142
|
+
);
|
|
143
|
+
if (typeof found === "string") return fail(`tools/${entry.name} ${found}`);
|
|
144
|
+
tools.push(...found);
|
|
145
|
+
}
|
|
146
|
+
return tools;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** A file's text, if it is a regular file within the size limit. */
|
|
150
|
+
async function readText(path: string): Promise<string> {
|
|
151
|
+
const stat = await lstat(path);
|
|
152
|
+
if (!stat.isFile())
|
|
153
|
+
throw new SkillFileError(`'${basename(path)}' is not a file.`);
|
|
154
|
+
if (stat.size > MAX_SKILL_BYTES) {
|
|
155
|
+
throw new SkillFileError(
|
|
156
|
+
`'${basename(path)}' is larger than ${MAX_SKILL_BYTES} bytes.`,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
const bytes = await readFile(path);
|
|
160
|
+
try {
|
|
161
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
162
|
+
} catch {
|
|
163
|
+
throw new SkillFileError(`'${basename(path)}' is not text.`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** The files of a skill folder, confined to it. */
|
|
168
|
+
function folderFiles(root: string, skill: string): SkillFiles {
|
|
169
|
+
return {
|
|
170
|
+
async list() {
|
|
171
|
+
const found: string[] = [];
|
|
172
|
+
const walk = async (
|
|
173
|
+
folder: string,
|
|
174
|
+
prefix: string,
|
|
175
|
+
depth: number,
|
|
176
|
+
): Promise<void> => {
|
|
177
|
+
if (depth > MAX_DEPTH) return;
|
|
178
|
+
const entries = await readdir(folder, { withFileTypes: true }).catch(
|
|
179
|
+
() => [],
|
|
180
|
+
);
|
|
181
|
+
for (const entry of entries.sort((a, b) =>
|
|
182
|
+
a.name.localeCompare(b.name),
|
|
183
|
+
)) {
|
|
184
|
+
if (found.length >= MAX_SKILL_FILES) return;
|
|
185
|
+
if (entry.name.startsWith(".") || entry.name === "node_modules")
|
|
186
|
+
continue;
|
|
187
|
+
const path = `${prefix}${entry.name}`;
|
|
188
|
+
if (entry.isDirectory())
|
|
189
|
+
await walk(join(folder, entry.name), `${path}/`, depth + 1);
|
|
190
|
+
else if (entry.isFile() && path !== "SKILL.md") found.push(path);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
await walk(root, "", 0);
|
|
194
|
+
return found;
|
|
195
|
+
},
|
|
196
|
+
async read(path) {
|
|
197
|
+
const shown = String(path).slice(0, 200);
|
|
198
|
+
const missing = new SkillFileError(
|
|
199
|
+
`The skill '${skill}' has no file '${shown}'.`,
|
|
200
|
+
);
|
|
201
|
+
if (
|
|
202
|
+
typeof path !== "string" ||
|
|
203
|
+
path === "" ||
|
|
204
|
+
path.includes("\0") ||
|
|
205
|
+
path.includes("\\") ||
|
|
206
|
+
isAbsolute(path) ||
|
|
207
|
+
path.split("/").some((part) => part === ".." || part === "")
|
|
208
|
+
) {
|
|
209
|
+
throw new SkillFileError(
|
|
210
|
+
`'${shown}' is not a path within the skill '${skill}': give it relative, with '/', and no '..'.`,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
// The real path, so a symlink cannot lead out of the skill.
|
|
214
|
+
const real = await realpath(join(root, path)).catch(() => {
|
|
215
|
+
throw missing;
|
|
216
|
+
});
|
|
217
|
+
if (!real.startsWith(`${root}${sep}`)) {
|
|
218
|
+
throw new SkillFileError(
|
|
219
|
+
`'${shown}' leads out of the skill '${skill}'.`,
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
return readText(real).catch((error: unknown) => {
|
|
223
|
+
if (error instanceof SkillFileError)
|
|
224
|
+
throw new SkillFileError(
|
|
225
|
+
`${error.message.slice(0, -1)} (skill '${skill}', file '${shown}').`,
|
|
226
|
+
);
|
|
227
|
+
throw missing;
|
|
228
|
+
});
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The frontmatter of a `SKILL.md`: the YAML block between two `---` lines.
|
|
3
|
+
* polaris reads only what a skill needs: top-level keys whose value is a
|
|
4
|
+
* plain, quoted or block (`|`, `>`) string. A nested map (`metadata:`) or a
|
|
5
|
+
* list is skipped. No YAML library: nothing in a skill runs.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface SkillDocument {
|
|
9
|
+
/** The top-level string values, by key. */
|
|
10
|
+
readonly data: Readonly<Record<string, string>>;
|
|
11
|
+
/** What follows the frontmatter. */
|
|
12
|
+
readonly body: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const KEY = /^([A-Za-z0-9_-]+):(?:[ \t]+(.*))?$/;
|
|
16
|
+
|
|
17
|
+
/** Split a `SKILL.md` into its frontmatter and its body, or `undefined` without one. */
|
|
18
|
+
export function parseSkillDocument(text: string): SkillDocument | undefined {
|
|
19
|
+
const lines = text.replace(/^/, "").split(/\r?\n/);
|
|
20
|
+
if (lines[0]?.trim() !== "---") return undefined;
|
|
21
|
+
const close = lines.findIndex((line, i) => i > 0 && line.trimEnd() === "---");
|
|
22
|
+
if (close === -1) return undefined;
|
|
23
|
+
const data: Record<string, string> = {};
|
|
24
|
+
const block = lines.slice(1, close);
|
|
25
|
+
let i = 0;
|
|
26
|
+
while (i < block.length) {
|
|
27
|
+
const line = block[i] ?? "";
|
|
28
|
+
i++;
|
|
29
|
+
const match = KEY.exec(line.trimEnd());
|
|
30
|
+
if (match === null) continue;
|
|
31
|
+
const key = match[1] ?? "";
|
|
32
|
+
const raw = (match[2] ?? "").trim();
|
|
33
|
+
// The lines that belong to this key: indented, or empty.
|
|
34
|
+
const nested: string[] = [];
|
|
35
|
+
while (i < block.length) {
|
|
36
|
+
const next = block[i] ?? "";
|
|
37
|
+
if (next.trim() !== "" && !/^[ \t]/.test(next)) break;
|
|
38
|
+
nested.push(next);
|
|
39
|
+
i++;
|
|
40
|
+
}
|
|
41
|
+
const value = scalar(raw, nested);
|
|
42
|
+
if (value !== undefined) data[key] = value;
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
data,
|
|
46
|
+
body: lines
|
|
47
|
+
.slice(close + 1)
|
|
48
|
+
.join("\n")
|
|
49
|
+
.trim(),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function scalar(raw: string, nested: readonly string[]): string | undefined {
|
|
54
|
+
const style = /^([|>])[+-]?$/.exec(raw);
|
|
55
|
+
if (style) {
|
|
56
|
+
const indents = nested
|
|
57
|
+
.filter((line) => line.trim() !== "")
|
|
58
|
+
.map((line) => /^[ \t]*/.exec(line)?.[0].length ?? 0);
|
|
59
|
+
const indent = indents.length > 0 ? Math.min(...indents) : 0;
|
|
60
|
+
const kept = nested.map((line) => line.slice(indent).trimEnd());
|
|
61
|
+
while (kept.at(-1) === "") kept.pop();
|
|
62
|
+
if (style[1] === "|") return kept.join("\n");
|
|
63
|
+
// Folded: lines join with a space, an empty line stays a newline.
|
|
64
|
+
return kept
|
|
65
|
+
.join("\n")
|
|
66
|
+
.replace(/([^\n])\n(?!\n)/g, "$1 ")
|
|
67
|
+
.replace(/\n\n/g, "\n");
|
|
68
|
+
}
|
|
69
|
+
if (raw === "") {
|
|
70
|
+
// A nested map or a list: not a string.
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
if (raw.startsWith('"')) {
|
|
74
|
+
try {
|
|
75
|
+
const parsed: unknown = JSON.parse(raw);
|
|
76
|
+
return typeof parsed === "string" ? parsed : undefined;
|
|
77
|
+
} catch {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (raw.startsWith("'")) {
|
|
82
|
+
return raw.endsWith("'") && raw.length >= 2
|
|
83
|
+
? raw.slice(1, -1).replace(/''/g, "'")
|
|
84
|
+
: undefined;
|
|
85
|
+
}
|
|
86
|
+
if (raw.startsWith("[") || raw.startsWith("{")) return undefined;
|
|
87
|
+
// A plain scalar, without a trailing comment; continuation lines fold in.
|
|
88
|
+
const continued = nested
|
|
89
|
+
.map((line) => line.trim())
|
|
90
|
+
.filter((line) => line !== "");
|
|
91
|
+
return [raw.replace(/\s+#.*$/, ""), ...continued].join(" ");
|
|
92
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@c9up/polaris/skills`: skills given to a run. Only each skill's name and
|
|
3
|
+
* description reach the model at first; it loads a skill when a task calls
|
|
4
|
+
* for it, and the skill's tools come in then. Not loaded by the rest of
|
|
5
|
+
* polaris.
|
|
6
|
+
*
|
|
7
|
+
* const skills = useSkills([payroll, ...(await readSkills('resources/skills'))])
|
|
8
|
+
* await polaris.run({
|
|
9
|
+
* system: skills.system,
|
|
10
|
+
* messages,
|
|
11
|
+
* tools: [...tools, ...skills.tools],
|
|
12
|
+
* inactiveTools: skills.inactiveTools,
|
|
13
|
+
* })
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
MAX_SKILL_FILES,
|
|
18
|
+
type ReadSkillOptions,
|
|
19
|
+
readSkill,
|
|
20
|
+
readSkills,
|
|
21
|
+
} from "./folder.js";
|
|
22
|
+
export {
|
|
23
|
+
MAX_SKILLS,
|
|
24
|
+
type SkillKit,
|
|
25
|
+
type SkillsOptions,
|
|
26
|
+
useSkills,
|
|
27
|
+
} from "./kit.js";
|
|
28
|
+
export {
|
|
29
|
+
defineSkill,
|
|
30
|
+
isSkill,
|
|
31
|
+
MAX_SKILL_BYTES,
|
|
32
|
+
MAX_SKILL_DESCRIPTION,
|
|
33
|
+
MAX_SKILL_NAME,
|
|
34
|
+
type PolarisSkill,
|
|
35
|
+
SkillFileError,
|
|
36
|
+
type SkillFiles,
|
|
37
|
+
type SkillSpec,
|
|
38
|
+
} from "./skill.js";
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Skills given to a run: their names and descriptions in the system prompt,
|
|
3
|
+
* a `load_skill` tool that returns a skill's instructions and lets its tools
|
|
4
|
+
* in, and a `read_skill_file` tool for the files the instructions refer to.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { E_POLARIS_INVALID_REQUEST } from "../errors.js";
|
|
8
|
+
import { defineTool, type PolarisTool, ToolError } from "../tools.js";
|
|
9
|
+
import { isSkill, type PolarisSkill, SkillFileError } from "./skill.js";
|
|
10
|
+
|
|
11
|
+
/** The most skills one run is given. */
|
|
12
|
+
export const MAX_SKILLS = 64;
|
|
13
|
+
|
|
14
|
+
export interface SkillsOptions {
|
|
15
|
+
/**
|
|
16
|
+
* When the skills' tools are sent to the model. `when-loaded` (default):
|
|
17
|
+
* from the request after the model loaded the skill. `from-start`: from
|
|
18
|
+
* the first request, but not callable until then, so the tools sent never
|
|
19
|
+
* change and a cached prompt prefix stays valid.
|
|
20
|
+
*/
|
|
21
|
+
readonly sendTools?: "when-loaded" | "from-start";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** What to give a run: put `system` in its system prompt, and the tools in. */
|
|
25
|
+
export interface SkillKit {
|
|
26
|
+
/** The skills' names and descriptions, and how to load one. */
|
|
27
|
+
readonly system: string;
|
|
28
|
+
/** `load_skill` and `read_skill_file`: give them to `tools`. */
|
|
29
|
+
readonly tools: readonly PolarisTool[];
|
|
30
|
+
/** The skills' tools, with `sendTools: 'from-start'`: give them to `inactiveTools`. */
|
|
31
|
+
readonly inactiveTools: readonly PolarisTool[];
|
|
32
|
+
readonly skills: readonly PolarisSkill[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const RESERVED = new Set(["load_skill", "read_skill_file"]);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* const skills = useSkills([payroll, await readSkill('resources/skills/vat')])
|
|
39
|
+
* await polaris.run({
|
|
40
|
+
* system: `${prompt}\n\n${skills.system}`,
|
|
41
|
+
* messages,
|
|
42
|
+
* tools: [...tools, ...skills.tools],
|
|
43
|
+
* inactiveTools: skills.inactiveTools,
|
|
44
|
+
* })
|
|
45
|
+
*/
|
|
46
|
+
export function useSkills(
|
|
47
|
+
skills: readonly PolarisSkill[],
|
|
48
|
+
options: SkillsOptions = {},
|
|
49
|
+
): SkillKit {
|
|
50
|
+
const byName = new Map<string, PolarisSkill>();
|
|
51
|
+
const owner = new Map<string, string>();
|
|
52
|
+
for (const skill of skills) {
|
|
53
|
+
if (!isSkill(skill)) {
|
|
54
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
55
|
+
"skills must be built by defineSkill() or readSkill()",
|
|
56
|
+
]);
|
|
57
|
+
}
|
|
58
|
+
if (byName.has(skill.name)) {
|
|
59
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
60
|
+
`two skills are named '${skill.name}'`,
|
|
61
|
+
]);
|
|
62
|
+
}
|
|
63
|
+
byName.set(skill.name, skill);
|
|
64
|
+
for (const tool of skill.tools) {
|
|
65
|
+
const other = RESERVED.has(tool.name) ? "polaris" : owner.get(tool.name);
|
|
66
|
+
if (other !== undefined) {
|
|
67
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
68
|
+
`the skill '${skill.name}' has a tool named '${tool.name}', as ${other === "polaris" ? "polaris does" : `the skill '${other}' does`}`,
|
|
69
|
+
]);
|
|
70
|
+
}
|
|
71
|
+
owner.set(tool.name, skill.name);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (byName.size === 0) {
|
|
75
|
+
throw new E_POLARIS_INVALID_REQUEST(["useSkills() needs a skill"]);
|
|
76
|
+
}
|
|
77
|
+
if (byName.size > MAX_SKILLS) {
|
|
78
|
+
throw new E_POLARIS_INVALID_REQUEST([
|
|
79
|
+
`a run is given at most ${MAX_SKILLS} skills`,
|
|
80
|
+
]);
|
|
81
|
+
}
|
|
82
|
+
const names = [...byName.keys()];
|
|
83
|
+
const find = (name: string): PolarisSkill => {
|
|
84
|
+
const skill = byName.get(name);
|
|
85
|
+
if (skill === undefined) {
|
|
86
|
+
throw new ToolError(
|
|
87
|
+
`There is no skill named '${name.slice(0, 64)}'. The skills are: ${names.join(", ")}.`,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
return skill;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const loadSkill = defineTool<{ name: string }>({
|
|
94
|
+
name: "load_skill",
|
|
95
|
+
description:
|
|
96
|
+
"Load a skill: returns its instructions, and makes its tools available from your next turn. Load the skill whose description matches the task before doing the task.",
|
|
97
|
+
inputSchema: {
|
|
98
|
+
type: "object",
|
|
99
|
+
properties: { name: { type: "string", enum: names } },
|
|
100
|
+
required: ["name"],
|
|
101
|
+
},
|
|
102
|
+
run: async ({ name }, context) => {
|
|
103
|
+
const skill = find(name);
|
|
104
|
+
if (skill.tools.length > 0) context.addTools(skill.tools);
|
|
105
|
+
const files = await skill.files.list().catch(() => []);
|
|
106
|
+
const parts = [
|
|
107
|
+
`<skill name="${skill.name}">\n${skill.instructions}\n</skill>`,
|
|
108
|
+
];
|
|
109
|
+
if (files.length > 0) {
|
|
110
|
+
parts.push(
|
|
111
|
+
`Files of this skill, to read with read_skill_file when the instructions call for them:\n${files.map((file) => `- ${file}`).join("\n")}`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
if (skill.tools.length > 0) {
|
|
115
|
+
parts.push(
|
|
116
|
+
`Tools available from your next turn: ${skill.tools.map((tool) => tool.name).join(", ")}.`,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
return parts.join("\n\n");
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const readSkillFile = defineTool<{ skill: string; path: string }>({
|
|
124
|
+
name: "read_skill_file",
|
|
125
|
+
description:
|
|
126
|
+
"Read a file of a skill, by its path in the skill as load_skill listed it (`reference/rates.md`). Its scripts are text to read, never to run.",
|
|
127
|
+
inputSchema: {
|
|
128
|
+
type: "object",
|
|
129
|
+
properties: {
|
|
130
|
+
skill: { type: "string", enum: names },
|
|
131
|
+
path: { type: "string" },
|
|
132
|
+
},
|
|
133
|
+
required: ["skill", "path"],
|
|
134
|
+
},
|
|
135
|
+
run: async ({ skill, path }) => {
|
|
136
|
+
try {
|
|
137
|
+
return await find(skill).files.read(path);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
if (error instanceof SkillFileError) throw new ToolError(error.message);
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const system = [
|
|
146
|
+
"You have skills: instructions for some kinds of task, loaded when needed. When a task matches a skill's description, call load_skill with its name before doing the task, then follow its instructions.",
|
|
147
|
+
"",
|
|
148
|
+
"Skills:",
|
|
149
|
+
...[...byName.values()].map(
|
|
150
|
+
(skill) => `- ${skill.name}: ${skill.description.replace(/\s+/g, " ")}`,
|
|
151
|
+
),
|
|
152
|
+
].join("\n");
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
system,
|
|
156
|
+
tools: [loadSkill, readSkillFile],
|
|
157
|
+
inactiveTools:
|
|
158
|
+
options.sendTools === "from-start"
|
|
159
|
+
? [...byName.values()].flatMap((skill) => skill.tools)
|
|
160
|
+
: [],
|
|
161
|
+
skills: [...byName.values()],
|
|
162
|
+
};
|
|
163
|
+
}
|