@soft-boy/agentschema 0.2.2 → 0.3.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/README.md +52 -212
- package/bin/agentschema.js +6 -2
- package/package.json +1 -1
- package/src/check.js +45 -3
- package/src/deploy.js +45 -5
- package/src/dotenv.js +50 -0
- package/src/instance.js +4 -2
- package/src/session.js +7 -1
- package/src/skills.js +79 -0
package/README.md
CHANGED
|
@@ -4,23 +4,26 @@ Define an AI-Agent as a folder of Markdown files.
|
|
|
4
4
|
|
|
5
5
|
```
|
|
6
6
|
my-agent/
|
|
7
|
-
├── agent.md
|
|
8
|
-
├── env.md
|
|
9
|
-
├── tools/
|
|
10
|
-
│ ├── agent_toolset_20260401.md
|
|
11
|
-
│ └── get_weather.md
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
├── agent.md # the agent
|
|
8
|
+
├── env.md # the environment it runs in
|
|
9
|
+
├── tools/
|
|
10
|
+
│ ├── agent_toolset_20260401.md # the pre-built tools to enable
|
|
11
|
+
│ └── get_weather.md # a custom tool
|
|
12
|
+
├── skills/
|
|
13
|
+
│ └── code-review/
|
|
14
|
+
│ └── SKILL.md # instructions the agent loads when needed
|
|
15
|
+
└── vaults/
|
|
16
|
+
└── secrets/
|
|
17
|
+
└── creds.md # a credential, without its secrets
|
|
15
18
|
```
|
|
16
19
|
|
|
17
|
-
**Status:** v0.2
|
|
20
|
+
**Status:** v0.2 draft. v0.2 breaks from the Claude Managed Agents schema: tools move out of `agent.md` and into `tools/`.
|
|
18
21
|
|
|
19
22
|
## Philosophy
|
|
20
23
|
|
|
21
|
-
1. **One folder, one agent.** A folder defines one agent,
|
|
22
|
-
2. **Everything is a Markdown file.**
|
|
23
|
-
3. **Inspired by Claude Managed Agents, not tied to it.**
|
|
24
|
+
1. **One folder, one agent.** A folder defines one agent, its environment, and what it needs to run. Sessions and each user's own credentials come from whatever runs the agent.
|
|
25
|
+
2. **Everything is a Markdown file.** Plain files you can read, diff, review and version. Settings go in YAML frontmatter, and prose goes in the body.
|
|
26
|
+
3. **Inspired by Claude Managed Agents, not tied to it.** The files use its field names, and tools are compiled into its format, but any runtime that can read the files can run the agent.
|
|
24
27
|
|
|
25
28
|
## Install
|
|
26
29
|
|
|
@@ -28,47 +31,25 @@ my-agent/
|
|
|
28
31
|
npm install -g @soft-boy/agentschema
|
|
29
32
|
```
|
|
30
33
|
|
|
31
|
-
This installs
|
|
34
|
+
This installs `agentschema`, and `ags` as a short name for it.
|
|
32
35
|
|
|
33
36
|
## Specification
|
|
34
37
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
An instance is a folder of Markdown files. `agent.md` and `env.md` are required.
|
|
38
|
-
|
|
39
|
-
```
|
|
40
|
-
my-agent/
|
|
41
|
-
├── agent.md # the agent
|
|
42
|
-
├── env.md # the environment (computer)
|
|
43
|
-
├── tools/ # tool files, nested however you like
|
|
44
|
-
│ ├── agent_toolset_20260401.md # the pre-built tools to enable
|
|
45
|
-
│ └── get_weather.md
|
|
46
|
-
└── vaults/ # optional: one folder per vault
|
|
47
|
-
└── Ops/
|
|
48
|
-
├── vault.md # optional: the vault's metadata
|
|
49
|
-
└── Slack.md # one file per credential
|
|
50
|
-
```
|
|
38
|
+
`agent.md` and `env.md` are required; everything else is optional. `agentschema check` validates a folder without calling the API.
|
|
51
39
|
|
|
52
40
|
### `agent.md`
|
|
53
|
-
Field reference: [Agent setup](https://platform.claude.com/docs/en/managed-agents/agent-setup).
|
|
54
41
|
|
|
55
|
-
|
|
56
|
-
- **Body** is the agent's system prompt, sent as `system` exactly as written (leading and trailing whitespace trimmed). An empty body means no system prompt.
|
|
57
|
-
- Setting `system` in the frontmatter is an error, because the body is the system prompt. So is `tools`, because tools live in `tools/`.
|
|
42
|
+
The frontmatter uses the fields of a [Managed Agents agent](https://platform.claude.com/docs/en/managed-agents/agent-setup), and `name` and `model` are required. The body is the system prompt.
|
|
58
43
|
|
|
59
44
|
### `env.md`
|
|
60
|
-
Field reference: [Environments](https://platform.claude.com/docs/en/managed-agents/environments).
|
|
61
45
|
|
|
62
|
-
|
|
63
|
-
- **Body** is a note for people reading the folder. It is not sent anywhere.
|
|
46
|
+
The frontmatter uses the fields of a [Managed Agents environment](https://platform.claude.com/docs/en/managed-agents/environments). The body is a note for people, and isn't sent anywhere.
|
|
64
47
|
|
|
65
48
|
### `tools/`
|
|
66
49
|
|
|
67
|
-
|
|
50
|
+
Every `.md` file under `tools/`, at any depth, is a tool file, and subfolders are only for organizing. `agentschema tools create` writes any of the three kinds.
|
|
68
51
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
**Pre-built tools** are the tools Claude Managed Agents runs in the container: `bash`, `edit`, `glob`, `grep`, `read`, `web_fetch`, `web_search` and `write`. One file lists the ones the agent gets. By convention it's `tools/agent_toolset_20260401.md`, but the file name isn't used:
|
|
52
|
+
**Pre-built tools** run in the agent's container. One file lists them, with a Permission of `always_allow`, `always_ask` or `auto`:
|
|
72
53
|
|
|
73
54
|
```markdown
|
|
74
55
|
---
|
|
@@ -80,51 +61,19 @@ type: agent_toolset_20260401
|
|
|
80
61
|
| Name | Enabled | Permission |
|
|
81
62
|
|---|---|---|
|
|
82
63
|
| `bash` | `true` | `always_ask` |
|
|
83
|
-
| `read` | `true` | |
|
|
84
|
-
| `web_search` | `false` | |
|
|
64
|
+
| `read` | `true` | `always_allow` |
|
|
85
65
|
```
|
|
86
66
|
|
|
87
|
-
|
|
88
|
-
- **`## Tools`** has a row for each tool you configure. **Enabled** is `true` or `false`. A tool without a row keeps the API's default, which is enabled. With no file at all, the agent gets no pre-built tools.
|
|
89
|
-
- **Permission** is `always_allow`, `always_ask` or `auto`. Leave the cell blank for the API's default (`always_allow`), or leave out the column entirely.
|
|
90
|
-
- There can be only one such file. Other sections are notes for people.
|
|
91
|
-
|
|
92
|
-
**MCP toolsets** give the agent the tools a remote MCP server offers. Declare the server in `agent.md`, with the API's fields:
|
|
93
|
-
|
|
94
|
-
```yaml
|
|
95
|
-
mcp_servers:
|
|
96
|
-
- type: url
|
|
97
|
-
name: calendar
|
|
98
|
-
url: https://calendarmcp.googleapis.com/mcp/v1
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
Then give it one file in `tools/`, named after the server:
|
|
67
|
+
**MCP toolsets** give the agent a remote server's tools. Declare the server under `mcp_servers` in `agent.md`, then add a file named after it. The `## Tools` table is optional:
|
|
102
68
|
|
|
103
69
|
```markdown
|
|
104
70
|
---
|
|
105
71
|
type: mcp_toolset
|
|
106
|
-
mcp_server_name:
|
|
107
|
-
default_config:
|
|
108
|
-
enabled: false
|
|
72
|
+
mcp_server_name: linear
|
|
109
73
|
---
|
|
110
|
-
|
|
111
|
-
## Tools
|
|
112
|
-
|
|
113
|
-
| Name | Enabled | Permission |
|
|
114
|
-
|---|---|---|
|
|
115
|
-
| `list_events` | `true` | `always_allow` |
|
|
116
|
-
| `create_event` | `true` | |
|
|
117
74
|
```
|
|
118
75
|
|
|
119
|
-
|
|
120
|
-
- **`default_config`** is optional, and applies to every tool without a row: `enabled` and `permission_policy`, as in the API. Without it, all the server's tools are on, and they ask for approval before each call (`always_ask`). `enabled: false`, as above, turns on only the tools you list.
|
|
121
|
-
- **`## Tools`** is optional, and configures tools by the names the server reports. The server defines its own tools, so `check` can't verify these names.
|
|
122
|
-
- **Sign-in:** a server that needs it gets its credentials from `vaults/`, matched by URL. `agentschema cred create` offers the servers in `agent.md`.
|
|
123
|
-
- **Networking:** with `limited` networking in `env.md`, set `allow_mcp_servers: true` or add the server's host to `allowed_hosts`, or the API won't start sessions. `check` warns about this.
|
|
124
|
-
|
|
125
|
-
An agent can have up to 20 MCP servers.
|
|
126
|
-
|
|
127
|
-
**Custom tools** run on your side: when the agent calls one, `agentschema run` runs its code on your machine and sends back the result.
|
|
76
|
+
**Custom tools** run on your machine when the agent calls them, and the code is never uploaded. `agentschema run` loads settings for them from a `.env` file in the folder:
|
|
128
77
|
|
|
129
78
|
````markdown
|
|
130
79
|
---
|
|
@@ -147,71 +96,55 @@ module.exports = ({ city }) =>
|
|
|
147
96
|
```
|
|
148
97
|
````
|
|
149
98
|
|
|
150
|
-
|
|
151
|
-
- **`## Input Schema`** is a table with the columns Name, Type, Description and Required. Type is `string`, `number`, `integer`, `boolean`, `object` or `array`, or one of those followed by `[]` for a list. Leave the section out for a tool that takes no input.
|
|
152
|
-
- **`## Code`** is required, and must be one JavaScript code block. It's a CommonJS module, and `module.exports` is the function. It's called with the call's input as an object. A string it returns (or resolves to) is sent as the result; anything else is sent as JSON. If it throws, the error message is sent as an error result. `require()` resolves from the tool's file, so packages installed in the agent folder work.
|
|
153
|
-
- Other sections are notes for people.
|
|
99
|
+
### `skills/`
|
|
154
100
|
|
|
155
|
-
|
|
101
|
+
Each folder is a [skill](https://platform.claude.com/docs/en/managed-agents/skills): a `SKILL.md` plus any files it needs. `name` must match the folder. Everything in the folder is uploaded to Anthropic and attached to the agent:
|
|
156
102
|
|
|
157
|
-
|
|
158
|
-
|
|
103
|
+
```markdown
|
|
104
|
+
---
|
|
105
|
+
name: code-review
|
|
106
|
+
description: Review code for bugs and missing tests. Use when asked to review a change.
|
|
107
|
+
---
|
|
159
108
|
|
|
160
|
-
|
|
109
|
+
Go through the change one file at a time, and report findings by file and line.
|
|
110
|
+
```
|
|
161
111
|
|
|
162
|
-
|
|
163
|
-
- **Every other `.md` file is a credential**, and the file name is the credential's name. It's either `mcp_oauth`, for an MCP server's sign-in, or `environment_variable`, for an API key. A vault holds at most 20, one per MCP server or variable.
|
|
112
|
+
### `vaults/`
|
|
164
113
|
|
|
165
|
-
|
|
114
|
+
Each folder is a [vault](https://platform.claude.com/docs/en/managed-agents/vaults), and each `.md` file in it is a credential, either `mcp_oauth` or `environment_variable`. Files never hold secrets, so they're safe to commit: `agentschema cred create` signs you in or asks for them, and sends them straight to the vault. For example, `vaults/secrets/creds.md`:
|
|
166
115
|
|
|
167
116
|
```markdown
|
|
168
117
|
---
|
|
169
118
|
auth:
|
|
170
119
|
type: mcp_oauth
|
|
171
|
-
mcp_server_url: https://mcp.
|
|
172
|
-
refresh:
|
|
173
|
-
token_endpoint: https://slack.com/api/oauth.v2.user.access
|
|
174
|
-
client_id: "1234567890.0987654321"
|
|
175
|
-
scope: channels:read chat:write
|
|
176
|
-
token_endpoint_auth:
|
|
177
|
-
type: client_secret_post
|
|
120
|
+
mcp_server_url: https://mcp.linear.app/mcp
|
|
178
121
|
---
|
|
179
|
-
|
|
180
|
-
The ops bot's Slack token.
|
|
181
122
|
```
|
|
182
123
|
|
|
183
|
-
|
|
184
|
-
- **`refresh`** is optional, but without it the credential stops working when the access token expires.
|
|
185
|
-
- **Some fields can't change** once the credential exists: `type`, `mcp_server_url`, whether there's a `refresh` block, and its `token_endpoint`, `client_id`, `resource` and `token_endpoint_auth.type`, or an environment variable's `secret_name`. Changing one is an error at `deploy`; delete the credential with `cred delete` and create it again. Other settings, the metadata and the note can change.
|
|
124
|
+
## Authentication
|
|
186
125
|
|
|
187
|
-
|
|
126
|
+
Commands that call the API use `ANTHROPIC_API_KEY` if it's exported, or otherwise your `ant auth login` profile. Everything is created in that key's workspace.
|
|
188
127
|
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
auth:
|
|
192
|
-
type: environment_variable
|
|
193
|
-
secret_name: STRIPE_API_KEY
|
|
194
|
-
networking:
|
|
195
|
-
type: limited
|
|
196
|
-
allowed_hosts:
|
|
197
|
-
- api.stripe.com
|
|
198
|
-
injection_location:
|
|
199
|
-
header: true
|
|
200
|
-
body: false
|
|
201
|
-
---
|
|
128
|
+
```sh
|
|
129
|
+
ANTHROPIC_API_KEY=sk-ant-…
|
|
202
130
|
```
|
|
203
131
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
132
|
+
## Getting started
|
|
133
|
+
|
|
134
|
+
Create an agent, deploy it, and chat with it:
|
|
135
|
+
|
|
136
|
+
```sh
|
|
137
|
+
agentschema init my-agent --yes
|
|
138
|
+
agentschema deploy my-agent
|
|
139
|
+
agentschema run my-agent
|
|
140
|
+
```
|
|
208
141
|
|
|
209
142
|
## Usage
|
|
210
143
|
|
|
211
144
|
```sh
|
|
212
145
|
agentschema init <name> # scaffold a new agent folder
|
|
213
146
|
agentschema check [dir] # validate the folder, no network
|
|
214
|
-
agentschema deploy [dir] # create or update the agent, environment and vaults
|
|
147
|
+
agentschema deploy [dir] # create or update the skills, agent, environment and vaults
|
|
215
148
|
agentschema run [dir] # chat with the deployed agent in the terminal
|
|
216
149
|
agentschema list # list the agents in your workspace
|
|
217
150
|
agentschema delete <agent_id> # archive an agent
|
|
@@ -224,100 +157,7 @@ agentschema cred login <file> # sign in to a credential's server in the
|
|
|
224
157
|
agentschema cred delete <file> # delete a credential
|
|
225
158
|
```
|
|
226
159
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
**`init`** creates the folder `<name>` with an `agent.md` and an `env.md` that pass `check`. It asks for:
|
|
230
|
-
- the agent's name and model
|
|
231
|
-
- its system prompt
|
|
232
|
-
- whether it gets the pre-built tools; if so, `init` writes `tools/agent_toolset_20260401.md` listing all eight
|
|
233
|
-
- the environment's name and networking
|
|
234
|
-
|
|
235
|
-
Press Enter to keep the default shown, or pass `--yes` to skip the questions. `init` won't write into a folder that already has files in it.
|
|
236
|
-
|
|
237
|
-
**`check`** reads every file and reports every problem it finds:
|
|
238
|
-
- a missing file, or a name that isn't lowercase
|
|
239
|
-
- frontmatter that is missing, unclosed, or invalid YAML
|
|
240
|
-
- a missing `name` or `model`, or a `system` or `tools` key in `agent.md`'s frontmatter
|
|
241
|
-
- a pre-built tools table that names an unknown tool or permission, an MCP server without both its entry in `agent.md` and its toolset file, or a custom tool with a missing description, a malformed input table, or code that isn't JavaScript or doesn't parse
|
|
242
|
-
- a credential file with a secret in it, a missing field, or two credentials for the same server
|
|
243
|
-
- values over the API's limits
|
|
244
|
-
- anything that looks like an API key or token
|
|
245
|
-
|
|
246
|
-
Unknown fields are only warnings, because they're sent to the API as written. `check` exits with status 1 when there are errors.
|
|
247
|
-
|
|
248
|
-
**`deploy`** runs `check` first, then creates or updates the environment, the agent and the vaults, and records their IDs in `claude-lock.json` in the folder. Commit that file.
|
|
249
|
-
- **Later runs update the same resources.** A file that hasn't changed since the last deploy is skipped. Each change to `agent.md` or `tools/` adds a new version of the agent, and sessions already running keep the version they started on.
|
|
250
|
-
- **The files are the source of truth.** A field you delete from a file is cleared on the next deploy, rather than left at its old value.
|
|
251
|
-
- **Changes made elsewhere aren't overwritten silently.** If the agent was changed outside the folder since the last deploy, `deploy` stops; `--force` overwrites it.
|
|
252
|
-
- **Credentials are updated, not created.** Creating a credential needs its secrets, which aren't in the files, so `deploy` lists each credential file that has no credential yet, with the `cred deploy` command to run. It updates the settings of credentials that exist.
|
|
253
|
-
|
|
254
|
-
`claude-lock.json` uses the same layout as `ant apply`'s, so `deploy` updates resources that `ant apply` created. The two tools hash files differently, though, so the first `deploy` after an `ant apply` updates both resources even if nothing changed.
|
|
255
|
-
|
|
256
|
-
**`run`** starts a session on the agent and environment in `claude-lock.json`, then opens a chat in the terminal. Deploy first; if you've edited the files since, `run` warns you and still uses the deployed version.
|
|
257
|
-
- **Messages:** type a message and press Enter. The agent's replies stream in, with a one-line summary of each tool call.
|
|
258
|
-
- **Approvals:** when a tool call needs approval (`always_ask`, or `auto` when the server can't decide), `run` asks you. Answer `y` to allow it, or type a reason to deny it; the agent sees the reason.
|
|
259
|
-
- **Custom tools:** `run` runs the tool's code from `tools/` and sends back the result. The code runs on your machine with your permissions, like any script in the folder. If the folder doesn't pass `check`, or the tool isn't in it (for example, when you resume a session from somewhere else), `run` shows the call's input and asks you to type the result.
|
|
260
|
-
- **Interrupting:** Ctrl+C interrupts the agent mid-turn, and a second Ctrl+C quits. Ctrl+D quits at the prompt.
|
|
261
|
-
- **Vaults:** the session gets every vault in `vaults/` that's been deployed, in folder name order. `run` warns about vaults that haven't been deployed and credentials that haven't been created.
|
|
262
|
-
- **Resuming:** quitting leaves the session in place, and `run` prints a command to resume it: `agentschema run --resume sesn_…`.
|
|
263
|
-
|
|
264
|
-
```sh
|
|
265
|
-
agentschema run --resume sesn_… # resume an existing session
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
To run the agent from your own code, create a session with the agent and environment IDs from the lock file.
|
|
269
|
-
|
|
270
|
-
**`list`** prints every agent in your workspace, with its ID, latest version, model and when it was last updated. It includes agents that weren't deployed with agentschema, and leaves out archived ones.
|
|
271
|
-
|
|
272
|
-
**`tools create`** adds a tool, asking a few questions like `init`. It only writes files; `deploy` sends them to the API.
|
|
273
|
-
- **Pre-built tools:** turn on all eight, or pick them one by one. It writes `tools/agent_toolset_20260401.md`, and stops if the agent already has that file.
|
|
274
|
-
- **Custom tool:** its name, what it does, and each input's name, type, description and whether it's required. It writes `tools/<name>.md` with a code block to fill in, which throws "isn't implemented yet" until you do.
|
|
275
|
-
- **MCP server:** its URL, its name and the permission for its tools. It asks the server for its tools, adds the server to `mcp_servers` in `agent.md`, keeping the rest of that file as written, and writes `tools/<name>.md` with a `## Tools` table listing them all as enabled, with that permission; edit the table to turn tools off. If `agent.md` already declares that server, it only writes the file. A server that won't list its tools without signing in gets no table, so every tool is on with that permission. A tool the server adds later isn't in the table, so it gets the API's defaults.
|
|
276
|
-
|
|
277
|
-
**`sessions list`** prints every session in your workspace, newest first, with its status, the agent and version it runs, and its title. Pass an ID to `run --resume` to pick a session back up.
|
|
278
|
-
|
|
279
|
-
**`delete`** archives an agent. The API can't delete agents outright, so this is as close as it gets. An archived agent drops out of `list`, and the API has no way to unarchive it. If the agent is in a folder's `claude-lock.json`, remove its entry so the next `deploy` creates a new one.
|
|
280
|
-
|
|
281
|
-
**`sessions delete`** permanently deletes a session and its event history. A running session can't be deleted; wait for it to go idle, or interrupt it, first.
|
|
282
|
-
|
|
283
|
-
Both ask for confirmation first, showing the agent's name or the session's title. Pass `--yes` to skip that, for example in scripts; without a terminal to ask in, they refuse unless you pass `--yes`.
|
|
284
|
-
|
|
285
|
-
**`cred create`** adds a credential, asking a few questions like `init`:
|
|
286
|
-
1. Which vault it goes in: one in `vaults/`, or a new one.
|
|
287
|
-
2. Whether it's for an MCP server's sign-in or an environment variable.
|
|
288
|
-
3. For an MCP server: which one (from `agent.md`'s `mcp_servers` where the vault has no credential for it yet, or any URL), its name, and how to get its tokens: sign in with the browser (when the server publishes an OAuth sign-in), paste them, or just write the file for later.
|
|
289
|
-
4. For an environment variable: its name, the hosts it may be sent to, where in a request it can appear, the credential's name, and whether to paste the value now or just write the file. The menu only writes `limited` networking; edit the file for `unrestricted`.
|
|
290
|
-
|
|
291
|
-
It writes `vaults/<vault>/<name>.md`, asking for the OAuth client's details when the file needs them. Then it signs you in as `cred login` does, or asks for the tokens without showing what you type: the access token, when it expires, and with a `refresh` block, the refresh token and client secret. It creates the vault first if it hasn't been deployed. If you chose to just write the file, create the credential later with `cred deploy`.
|
|
292
|
-
|
|
293
|
-
**`cred deploy`** creates the credential an existing file describes in Anthropic, such as one you wrote by hand or one `deploy` lists as missing: `agentschema cred deploy my-agent/vaults/Ops/Slack.md`. It asks whether to sign in with the browser or paste the tokens; for an environment variable, it just asks for the value. A credential that already exists is an error when pasting; to replace its tokens, run `cred login`, or delete it and deploy it again.
|
|
294
|
-
|
|
295
|
-
**`cred login`** creates a credential by signing you in to its MCP server in the browser, for example `agentschema cred login my-agent/vaults/Ops/Linear.md`. The file needs only `type` and `mcp_server_url`:
|
|
296
|
-
1. It finds the server's sign-in from its URL, using the OAuth metadata MCP servers publish.
|
|
297
|
-
2. It needs an OAuth client. With a `refresh` block in the file, it uses that block's `client_id`, and asks for the client secret unless `token_endpoint_auth.type` is `none`. Without one, it registers agentschema as a client, which servers such as Linear and Notion allow, and writes the `refresh` block into the file.
|
|
298
|
-
3. It opens your browser to sign in, and receives the result on `http://127.0.0.1:<port>/callback`. The port is any free one, or `--port`.
|
|
299
|
-
4. It creates the credential with the tokens, creating the vault first if needed. If the credential already exists, it replaces its tokens instead, so signing in again is how you renew a credential whose refresh token stopped working.
|
|
300
|
-
|
|
301
|
-
It stops without creating anything if the server doesn't return a refresh token, since the credential would stop working within the hour.
|
|
302
|
-
|
|
303
|
-
Some servers, such as Google's and Slack's, don't let apps register themselves. For those, create an OAuth client with the provider first, add its details to the file's `refresh` block (`cred login` prints what to add), and allow the redirect URI `http://127.0.0.1:<port>/callback`. A client that needs an exact redirect URI needs a fixed `--port`. For Google, `cred login` also asks for offline access, which Google requires before it issues a refresh token.
|
|
304
|
-
|
|
305
|
-
**`cred delete`** permanently deletes the credential a file describes and removes it from `claude-lock.json`. The file stays, so you can create it again. It asks for confirmation unless you pass `--yes`.
|
|
306
|
-
|
|
307
|
-
### Authentication
|
|
308
|
-
|
|
309
|
-
agentschema has no account setting of its own. Every command except `init` and `check` uses whichever Anthropic credentials are in your shell:
|
|
310
|
-
|
|
311
|
-
1. `ANTHROPIC_API_KEY`, if it's set
|
|
312
|
-
2. otherwise, the profile you signed in to with `ant auth login` (the active one, or the one `ANTHROPIC_PROFILE` names)
|
|
313
|
-
|
|
314
|
-
An API key belongs to one workspace, and the agent and environment are created in that workspace. Each person deploys with their own key, and to deploy somewhere else you switch the key:
|
|
315
|
-
|
|
316
|
-
```sh
|
|
317
|
-
ANTHROPIC_API_KEY=sk-ant-… agentschema deploy my-agent
|
|
318
|
-
```
|
|
319
|
-
|
|
320
|
-
`claude-lock.json` holds the IDs from one workspace. A committed lock works for everyone whose key is in that workspace, and they all update the same agent. If you deploy the folder from a key in a different workspace, `deploy` can't find those IDs and stops. In that case, deploy from a copy of the folder, or don't commit that lock file.
|
|
160
|
+
Use `agentschema --help` for more info.
|
|
321
161
|
|
|
322
162
|
## Development
|
|
323
163
|
|
package/bin/agentschema.js
CHANGED
|
@@ -10,6 +10,7 @@ import { credDelete, credLogin } from "../src/cred.js";
|
|
|
10
10
|
import { check } from "../src/check.js";
|
|
11
11
|
import { DeleteError, deleteAgent, deleteSession } from "../src/delete.js";
|
|
12
12
|
import { DeployError, deploy } from "../src/deploy.js";
|
|
13
|
+
import { loadEnvFile } from "../src/dotenv.js";
|
|
13
14
|
import { InitError, init } from "../src/init.js";
|
|
14
15
|
import { listAgents, listSessions } from "../src/list.js";
|
|
15
16
|
import { prompts } from "../src/prompts.js";
|
|
@@ -24,8 +25,8 @@ const USAGE = `Usage: agentschema <command> [dir] [options]
|
|
|
24
25
|
Commands:
|
|
25
26
|
init <name> Scaffold a new agent folder, asking a few questions
|
|
26
27
|
check [dir] Validate the folder's files, without calling the API
|
|
27
|
-
deploy [dir] Create or update the agent, environment and vaults, and write claude-lock.json
|
|
28
|
-
run [dir] Chat with the deployed agent in a new session
|
|
28
|
+
deploy [dir] Create or update the skills, agent, environment and vaults, and write claude-lock.json
|
|
29
|
+
run [dir] Chat with the deployed agent in a new session, loading dir/.env for your tools
|
|
29
30
|
list List the agents in your workspace
|
|
30
31
|
delete <agent_id> Archive an agent (agents can't be deleted outright)
|
|
31
32
|
tools <command> Add pre-built tools, custom tools and MCP servers (agentschema tools -h)
|
|
@@ -251,7 +252,10 @@ async function main(argv) {
|
|
|
251
252
|
}
|
|
252
253
|
|
|
253
254
|
if (command === "run") {
|
|
255
|
+
// Made first, so .env only reaches your tools: Anthropic credentials still come from the
|
|
256
|
+
// shell, the same as for every other command.
|
|
254
257
|
const client = makeClient(false);
|
|
258
|
+
for (const warning of loadEnvFile(dir)) console.warn(`warning: ${warning}`);
|
|
255
259
|
const { session, notes } = await openSession(dir, {
|
|
256
260
|
client,
|
|
257
261
|
sessionId: values.resume,
|
package/package.json
CHANGED
package/src/check.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AGENT_FILE, ENV_FILE, readInstance } from "./instance.js";
|
|
2
|
+
import { MAX_UPLOAD_BYTES, SKILLS_DIR, readSkills } from "./skills.js";
|
|
2
3
|
import { MCP_TOOLSET, TOOLS_DIR, checkTools, readTools } from "./tools.js";
|
|
3
4
|
import { CREDENTIAL_TYPES, ENV_VAR, SECRET_FIELDS, TOKEN_ENDPOINT_AUTH, VAULT_FILE, get, readVaults } from "./vaults.js";
|
|
4
5
|
|
|
@@ -24,7 +25,8 @@ export function check(dir) {
|
|
|
24
25
|
const { agent, env, errors } = readInstance(dir);
|
|
25
26
|
const { tools, errors: toolErrors } = readTools(dir);
|
|
26
27
|
const { vaults, errors: vaultErrors } = readVaults(dir);
|
|
27
|
-
errors
|
|
28
|
+
const { skills, errors: skillErrors } = readSkills(dir);
|
|
29
|
+
errors.push(...toolErrors, ...vaultErrors, ...skillErrors);
|
|
28
30
|
const warnings = [];
|
|
29
31
|
|
|
30
32
|
if (agent) checkAgent(agent, errors, warnings);
|
|
@@ -32,18 +34,22 @@ export function check(dir) {
|
|
|
32
34
|
checkTools(tools, errors);
|
|
33
35
|
if (agent) checkMcpServers(agent, tools, errors);
|
|
34
36
|
if (agent && env) checkMcpNetworking(agent, env, warnings);
|
|
37
|
+
for (const skill of skills) checkSkill(skill, errors);
|
|
38
|
+
const declaredSkills = Array.isArray(agent?.data.skills) ? agent.data.skills.length : 0;
|
|
39
|
+
if (declaredSkills + skills.length > 500) errors.push(`${declaredSkills + skills.length} skills; the limit is 500`);
|
|
35
40
|
for (const vault of vaults) checkVault(vault, errors, warnings);
|
|
36
41
|
checkEnvVarCredentials(vaults, env, warnings);
|
|
37
42
|
|
|
38
43
|
const vaultFiles = vaults.flatMap((v) => [...(v.file ? [[`${v.path}/${VAULT_FILE}`, v.file]] : []), ...v.credentials.map((c) => [c.file, c])]);
|
|
39
|
-
|
|
44
|
+
const skillTexts = skills.flatMap((s) => s.files.map((f) => [`${s.path}/${f.rel}`, { raw: f.bytes.toString("utf8") }]));
|
|
45
|
+
for (const [file, parsed] of [[AGENT_FILE, agent], [ENV_FILE, env], ...tools.map((t) => [t.file, t]), ...vaultFiles, ...skillTexts]) {
|
|
40
46
|
if (!parsed) continue;
|
|
41
47
|
for (const [pattern, what] of SECRET_PATTERNS) {
|
|
42
48
|
if (pattern.test(parsed.raw)) errors.push(`${file}: looks like it contains ${what}; keep secrets out of these files`);
|
|
43
49
|
}
|
|
44
50
|
}
|
|
45
51
|
|
|
46
|
-
return { errors, warnings, instance: agent && env ? { agent, env, tools, vaults } : null };
|
|
52
|
+
return { errors, warnings, instance: agent && env ? { agent, env, tools, vaults, skills } : null };
|
|
47
53
|
}
|
|
48
54
|
|
|
49
55
|
function checkAgent({ data, body }, errors, warnings) {
|
|
@@ -73,6 +79,10 @@ function checkAgent({ data, body }, errors, warnings) {
|
|
|
73
79
|
else if (data[field].length > max) err(`\`${field}\` has ${data[field].length} entries; the limit is ${max}`);
|
|
74
80
|
}
|
|
75
81
|
|
|
82
|
+
if (Array.isArray(data.skills)) {
|
|
83
|
+
for (const [index, skill] of data.skills.entries()) checkSkillRef(skill, (msg) => err(`\`skills[${index}]\`: ${msg}`));
|
|
84
|
+
}
|
|
85
|
+
|
|
76
86
|
checkMetadata(data.metadata, 16, err);
|
|
77
87
|
|
|
78
88
|
for (const key of Object.keys(data)) {
|
|
@@ -110,6 +120,38 @@ function checkEnv({ data }, errors, warnings) {
|
|
|
110
120
|
}
|
|
111
121
|
}
|
|
112
122
|
|
|
123
|
+
// An entry in agent.md's `skills` list, in the API's shape.
|
|
124
|
+
function checkSkillRef(skill, err) {
|
|
125
|
+
if (typeof skill === "string") {
|
|
126
|
+
return err(`write this as a mapping with \`type\` and \`skill_id\`; a skill in ${SKILLS_DIR}/ is attached without being listed`);
|
|
127
|
+
}
|
|
128
|
+
if (!isObject(skill)) return err("must be a mapping with `type` and `skill_id`");
|
|
129
|
+
if (!["anthropic", "custom"].includes(skill.type)) err("`type` must be `anthropic` or `custom`");
|
|
130
|
+
if (typeof skill.skill_id !== "string" || skill.skill_id === "") err("`skill_id` is required");
|
|
131
|
+
else if (skill.type === "custom" && !skill.skill_id.startsWith("skill_")) err("a custom skill's `skill_id` starts with skill_");
|
|
132
|
+
if (skill.version !== undefined && skill.version !== null && typeof skill.version !== "string") {
|
|
133
|
+
err("`version` must be a string (quote numbers)");
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function checkSkill(skill, errors) {
|
|
138
|
+
const err = (msg) => errors.push(`${skill.file}: ${msg}`);
|
|
139
|
+
const { name, description } = skill.data;
|
|
140
|
+
if (typeof name !== "string" || name === "") err("`name` is required");
|
|
141
|
+
else {
|
|
142
|
+
if (name !== skill.name) err(`\`name\` must match the folder's name (${skill.name})`);
|
|
143
|
+
if (!/^[a-z0-9-]{1,64}$/.test(name)) err("`name` may only use lowercase letters, digits and hyphens, up to 64 characters");
|
|
144
|
+
if (/anthropic|claude/.test(name)) err("`name` can't contain the reserved words `anthropic` or `claude`");
|
|
145
|
+
}
|
|
146
|
+
if (typeof description !== "string" || description.trim() === "") err("`description` is required");
|
|
147
|
+
else {
|
|
148
|
+
if (description.length > 1024) err(`\`description\` is ${description.length} characters; the limit is 1,024`);
|
|
149
|
+
if (/<[^>]+>/.test(description)) err("`description` can't contain XML tags");
|
|
150
|
+
}
|
|
151
|
+
const size = skill.files.reduce((total, f) => total + f.bytes.length, 0);
|
|
152
|
+
if (size > MAX_UPLOAD_BYTES) errors.push(`${skill.path}: its files add up to ${Math.ceil(size / 1024 / 1024)} MB; the limit is 30 MB`);
|
|
153
|
+
}
|
|
154
|
+
|
|
113
155
|
const mcpServers = (agent) => (Array.isArray(agent.data.mcp_servers) ? agent.data.mcp_servers.filter(isObject) : []);
|
|
114
156
|
|
|
115
157
|
// Each server agent.md declares needs a toolset file, and each toolset file a declared server;
|
package/src/deploy.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { join } from "node:path";
|
|
2
|
-
import Anthropic from "@anthropic-ai/sdk";
|
|
2
|
+
import Anthropic, { toFile } from "@anthropic-ai/sdk";
|
|
3
3
|
import { check } from "./check.js";
|
|
4
4
|
import { AGENT_FILE, ENV_FILE, agentBody, envBody } from "./instance.js";
|
|
5
5
|
import { LOCK_FILE, hashBody, readLock, writeLock } from "./lock.js";
|
|
6
|
+
import { skillFingerprint, skillKey, skillRefs } from "./skills.js";
|
|
6
7
|
import { ENV_VAR, changedImmutableFields, credentialKey, credentialSettings, vaultBody, vaultKey } from "./vaults.js";
|
|
7
8
|
|
|
8
9
|
export class DeployError extends Error {}
|
|
9
10
|
|
|
10
|
-
// Creates or updates the environment, agent and vaults a folder defines, recording their
|
|
11
|
-
// in claude-lock.json. Credentials are only updated here: creating one needs its secrets, so
|
|
11
|
+
// Creates or updates the skills, environment, agent and vaults a folder defines, recording their
|
|
12
|
+
// IDs in claude-lock.json. Skills go first, because the agent refers to their IDs and versions. Credentials are only updated here: creating one needs its secrets, so
|
|
12
13
|
// that's `cred deploy`'s job.
|
|
13
14
|
// Returns the plan: one { file, kind, action, id, version } entry per resource.
|
|
14
15
|
export async function deploy(dir, { client, dryRun = false, force = false, log = () => {} }) {
|
|
@@ -16,9 +17,18 @@ export async function deploy(dir, { client, dryRun = false, force = false, log =
|
|
|
16
17
|
if (errors.length > 0) throw new DeployError(`check failed:\n ${errors.join("\n ")}`);
|
|
17
18
|
const lock = openLock(dir, client);
|
|
18
19
|
|
|
20
|
+
const skillSteps = await deploySkills(instance.skills, { dir, client, lock, dryRun, log });
|
|
21
|
+
// A dry run has no new IDs or versions yet, so a stand-in shows that the agent will change too.
|
|
22
|
+
const pending = dryRun ? skillSteps.filter((s) => s.action !== "unchanged") : [];
|
|
23
|
+
const refs = instance.skills.flatMap((skill) => {
|
|
24
|
+
const step = pending.find((s) => s.key === skillKey(skill));
|
|
25
|
+
if (!step) return skillRefs([skill], lock);
|
|
26
|
+
return [{ type: "custom", skill_id: step.entry?.id ?? `pending:${skill.name}`, version: `pending:${step.hash}` }];
|
|
27
|
+
});
|
|
28
|
+
|
|
19
29
|
const steps = [
|
|
20
30
|
{ file: ENV_FILE, kind: "environment", body: envBody(instance) },
|
|
21
|
-
{ file: AGENT_FILE, kind: "agent", body: agentBody(instance) },
|
|
31
|
+
{ file: AGENT_FILE, kind: "agent", body: agentBody(instance, refs) },
|
|
22
32
|
];
|
|
23
33
|
for (const step of steps) {
|
|
24
34
|
step.key = `./${step.file}`;
|
|
@@ -51,7 +61,7 @@ export async function deploy(dir, { client, dryRun = false, force = false, log =
|
|
|
51
61
|
|
|
52
62
|
const vaultSteps = await deployVaults(dir, instance.vaults, { client, lock, dryRun, log });
|
|
53
63
|
|
|
54
|
-
return [...steps, ...vaultSteps].map(({ file, kind, action, key }) => ({
|
|
64
|
+
return [...skillSteps, ...steps, ...vaultSteps].map(({ file, kind, action, key }) => ({
|
|
55
65
|
file, kind, action,
|
|
56
66
|
id: lock.resources[key]?.id,
|
|
57
67
|
version: lock.resources[key]?.version,
|
|
@@ -71,6 +81,36 @@ export function openLock(dir, client) {
|
|
|
71
81
|
return lock;
|
|
72
82
|
}
|
|
73
83
|
|
|
84
|
+
// Uploads each skill that's new or changed. A change uploads the whole folder as a new version.
|
|
85
|
+
async function deploySkills(skills, { dir, client, lock, dryRun, log }) {
|
|
86
|
+
const steps = [];
|
|
87
|
+
for (const skill of skills) {
|
|
88
|
+
const step = planStep(lock, { file: skill.path, key: skillKey(skill), kind: "skill", body: skillFingerprint(skill) });
|
|
89
|
+
steps.push(step);
|
|
90
|
+
if (dryRun || step.action === "unchanged") {
|
|
91
|
+
log(`${step.action.padEnd(9)} ${step.kind} ${step.entry?.id ?? skill.name}`);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// The API keeps a skill's layout from these names, so each one starts with its folder.
|
|
96
|
+
const files = await Promise.all(skill.files.map((f) => toFile(f.bytes, `${skill.name}/${f.rel}`)));
|
|
97
|
+
let id;
|
|
98
|
+
let version;
|
|
99
|
+
if (step.action === "create") {
|
|
100
|
+
const created = await call(step, () => client.skills.create({ files }));
|
|
101
|
+
({ id, latest_version_id: version } = created);
|
|
102
|
+
} else {
|
|
103
|
+
({ id } = step.entry);
|
|
104
|
+
await retrieve(step, () => client.skills.retrieve(id));
|
|
105
|
+
({ id: version } = await call(step, () => client.skills.versions.create(id, { files })));
|
|
106
|
+
}
|
|
107
|
+
lock.resources[step.key] = { kind: "skill", id, version, hash: step.hash };
|
|
108
|
+
writeLock(dir, lock);
|
|
109
|
+
log(`${(step.action === "create" ? "created" : "updated").padEnd(9)} skill ${id} (version ${version})`);
|
|
110
|
+
}
|
|
111
|
+
return steps;
|
|
112
|
+
}
|
|
113
|
+
|
|
74
114
|
// Creates or updates each vault, then updates the settings of credentials already created.
|
|
75
115
|
// A credential file with no credential yet is reported, not created: that needs its secrets.
|
|
76
116
|
async function deployVaults(dir, vaults, { client, lock, dryRun, log }) {
|
package/src/dotenv.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
export const ENV_FILE_NAME = ".env";
|
|
5
|
+
|
|
6
|
+
// Parses KEY=VALUE lines. Returns { values, bad }: `bad` holds the numbers of lines that aren't
|
|
7
|
+
// valid, so they can be reported without showing what's on them.
|
|
8
|
+
export function parseEnvFile(text) {
|
|
9
|
+
const values = {};
|
|
10
|
+
const bad = [];
|
|
11
|
+
for (const [index, raw] of text.replace(/^/, "").split(/\r?\n/).entries()) {
|
|
12
|
+
const line = raw.trim();
|
|
13
|
+
if (line === "" || line.startsWith("#")) continue;
|
|
14
|
+
const match = /^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/.exec(line);
|
|
15
|
+
const value = match && parseValue(match[2]);
|
|
16
|
+
if (value === null || !match) bad.push(index + 1);
|
|
17
|
+
else values[match[1]] = value;
|
|
18
|
+
}
|
|
19
|
+
return { values, bad };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// A quoted value, where double quotes understand \n and \", or a bare value up to a " #" comment.
|
|
23
|
+
// Returns null for a quote that isn't closed.
|
|
24
|
+
function parseValue(text) {
|
|
25
|
+
const quote = text[0];
|
|
26
|
+
if (quote === "'" || quote === '"') {
|
|
27
|
+
const end = quote === "'" ? text.indexOf("'", 1) : /(?<!\\)"/.exec(text.slice(1))?.index + 1;
|
|
28
|
+
if (end === undefined || end < 1 || Number.isNaN(end)) return null;
|
|
29
|
+
const inner = text.slice(1, end);
|
|
30
|
+
return quote === "'" ? inner : inner.replace(/\\n/g, "\n").replace(/\\"/g, '"');
|
|
31
|
+
}
|
|
32
|
+
return text.replace(/\s+#.*$/, "").trim();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Loads a folder's .env into process.env, for code that runs on your side. A variable already
|
|
36
|
+
// set in the shell wins. A missing file is fine. Returns warnings to show.
|
|
37
|
+
export function loadEnvFile(dir, env = process.env) {
|
|
38
|
+
let text;
|
|
39
|
+
try {
|
|
40
|
+
text = readFileSync(join(dir, ENV_FILE_NAME), "utf8");
|
|
41
|
+
} catch (err) {
|
|
42
|
+
if (err.code === "ENOENT") return [];
|
|
43
|
+
return [`couldn't read ${ENV_FILE_NAME}: ${err.message}`];
|
|
44
|
+
}
|
|
45
|
+
const { values, bad } = parseEnvFile(text);
|
|
46
|
+
for (const [name, value] of Object.entries(values)) {
|
|
47
|
+
if (env[name] === undefined) env[name] = value;
|
|
48
|
+
}
|
|
49
|
+
return bad.map((line) => `${ENV_FILE_NAME}:${line}: not a KEY=VALUE line, so it was skipped`);
|
|
50
|
+
}
|
package/src/instance.js
CHANGED
|
@@ -41,9 +41,11 @@ export function readInstance(dir) {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
// The bodies sent to POST /v1/agents and POST /v1/environments.
|
|
44
|
-
// The agent's tools come from tools/, compiled into the API's `tools` list.
|
|
45
|
-
|
|
44
|
+
// The agent's tools come from tools/, compiled into the API's `tools` list. `skills` are the
|
|
45
|
+
// references to the skills deployed from skills/, added to any in agent.md's frontmatter.
|
|
46
|
+
export function agentBody({ agent, tools }, skills = []) {
|
|
46
47
|
const body = { ...agent.data, tools: compileTools(tools) };
|
|
48
|
+
if (skills.length > 0) body.skills = [...(agent.data.skills ?? []), ...skills];
|
|
47
49
|
if (agent.body) body.system = agent.body;
|
|
48
50
|
return body;
|
|
49
51
|
}
|
package/src/session.js
CHANGED
|
@@ -2,6 +2,7 @@ import { join } from "node:path";
|
|
|
2
2
|
import { check } from "./check.js";
|
|
3
3
|
import { AGENT_FILE, ENV_FILE, agentBody, envBody } from "./instance.js";
|
|
4
4
|
import { LOCK_FILE, hashBody, readLock } from "./lock.js";
|
|
5
|
+
import { skillFingerprint, skillKey, skillRefs } from "./skills.js";
|
|
5
6
|
import { TOOLS_DIR } from "./tools.js";
|
|
6
7
|
import { credentialKey, vaultKey } from "./vaults.js";
|
|
7
8
|
|
|
@@ -26,7 +27,12 @@ export async function openSession(dir, { client, sessionId }) {
|
|
|
26
27
|
const notes = [];
|
|
27
28
|
const { instance, errors } = check(dir);
|
|
28
29
|
if (instance && errors.length === 0) {
|
|
29
|
-
if (hashBody(agentBody(instance)) !== agent.hash)
|
|
30
|
+
if (hashBody(agentBody(instance, skillRefs(instance.skills, lock))) !== agent.hash) {
|
|
31
|
+
notes.push(`${AGENT_FILE} or ${TOOLS_DIR}/ has changed since the last deploy`);
|
|
32
|
+
}
|
|
33
|
+
for (const skill of instance.skills) {
|
|
34
|
+
if (hashBody(skillFingerprint(skill)) !== lock.resources[skillKey(skill)]?.hash) notes.push(`${skill.path} has changed since the last deploy`);
|
|
35
|
+
}
|
|
30
36
|
if (hashBody(envBody(instance)) !== env.hash) notes.push(`${ENV_FILE} has changed since the last deploy`);
|
|
31
37
|
if (notes.length > 0) notes.push(`this session uses the deployed version; run agentschema deploy to pick up the changes`);
|
|
32
38
|
}
|
package/src/skills.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFileSync, readdirSync, statSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { FileError, parseMarkdown } from "./markdown.js";
|
|
5
|
+
|
|
6
|
+
export const SKILLS_DIR = "skills";
|
|
7
|
+
export const SKILL_FILE = "SKILL.md";
|
|
8
|
+
export const MAX_UPLOAD_BYTES = 30 * 1024 * 1024;
|
|
9
|
+
|
|
10
|
+
// Reads skills/: each folder is a skill, with a SKILL.md at its top and any other files it needs,
|
|
11
|
+
// at any depth. Hidden files and node_modules are left out. Skills are sorted by name. A missing
|
|
12
|
+
// skills/ folder means no skills.
|
|
13
|
+
// Returns { skills, errors }; a skill that can't be read has an entry in errors.
|
|
14
|
+
export function readSkills(dir) {
|
|
15
|
+
const folder = join(dir, SKILLS_DIR);
|
|
16
|
+
try {
|
|
17
|
+
if (!statSync(folder).isDirectory()) return { skills: [], errors: [`${SKILLS_DIR}: not a directory`] };
|
|
18
|
+
} catch {
|
|
19
|
+
return { skills: [], errors: [] };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const skills = [];
|
|
23
|
+
const errors = [];
|
|
24
|
+
for (const entry of sorted(readdirSync(folder, { withFileTypes: true }))) {
|
|
25
|
+
if (entry.name.startsWith(".")) continue;
|
|
26
|
+
const path = `${SKILLS_DIR}/${entry.name}`;
|
|
27
|
+
if (!entry.isDirectory()) {
|
|
28
|
+
if (entry.name.endsWith(".md")) errors.push(`${path}: each skill is a folder, like ${SKILLS_DIR}/my-skill/${SKILL_FILE}`);
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const files = skillFiles(join(folder, entry.name), "");
|
|
33
|
+
// Compare exact names: macOS file systems would otherwise open skill.md as SKILL.md.
|
|
34
|
+
const main = files.find((f) => f.rel === SKILL_FILE);
|
|
35
|
+
if (!main) {
|
|
36
|
+
const wrongCase = files.find((f) => f.rel.toLowerCase() === SKILL_FILE.toLowerCase());
|
|
37
|
+
errors.push(`${path}: missing ${SKILL_FILE}${wrongCase ? ` (found ${wrongCase.rel}; the name must be uppercase)` : ""}`);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
try {
|
|
41
|
+
const parsed = parseMarkdown(main.bytes.toString("utf8"), `${path}/${SKILL_FILE}`);
|
|
42
|
+
skills.push({ name: entry.name, path, file: `${path}/${SKILL_FILE}`, files, ...parsed });
|
|
43
|
+
} catch (err) {
|
|
44
|
+
if (!(err instanceof FileError)) throw err;
|
|
45
|
+
errors.push(err.message);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return { skills, errors };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function skillFiles(folder, prefix) {
|
|
52
|
+
const files = [];
|
|
53
|
+
for (const entry of sorted(readdirSync(folder, { withFileTypes: true }))) {
|
|
54
|
+
if (entry.name.startsWith(".") || entry.name === "node_modules") continue;
|
|
55
|
+
const rel = `${prefix}${entry.name}`;
|
|
56
|
+
if (entry.isDirectory()) files.push(...skillFiles(join(folder, entry.name), `${rel}/`));
|
|
57
|
+
else if (entry.isFile()) files.push({ rel, bytes: readFileSync(join(folder, entry.name)) });
|
|
58
|
+
}
|
|
59
|
+
return files;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const sorted = (entries) => entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
63
|
+
|
|
64
|
+
// The lock file key for a skill.
|
|
65
|
+
export const skillKey = (skill) => `./${skill.path}`;
|
|
66
|
+
|
|
67
|
+
// What's hashed into the lock: each file's path and contents, so any change makes a new version.
|
|
68
|
+
export function skillFingerprint(skill) {
|
|
69
|
+
return { files: skill.files.map((f) => [f.rel, createHash("sha256").update(f.bytes).digest("hex")]) };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The agent's references to the skills deployed from this folder, pinned to their versions, so
|
|
73
|
+
// that a session runs exactly what deploy last sent.
|
|
74
|
+
export function skillRefs(skills = [], lock) {
|
|
75
|
+
return skills
|
|
76
|
+
.map((skill) => lock?.resources?.[skillKey(skill)])
|
|
77
|
+
.filter(Boolean)
|
|
78
|
+
.map((entry) => ({ type: "custom", skill_id: entry.id, version: entry.version }));
|
|
79
|
+
}
|