diff --git a/public/__redirects b/public/__redirects index 215dbf3648d..48e082ac595 100644 --- a/public/__redirects +++ b/public/__redirects @@ -386,6 +386,10 @@ /agents/concepts/agent-class/ /agents/agent-class/internals/ 301 /agents/concepts/memory/ /agents/concepts/context/ 301 /agents/concepts/conversation-state-and-memory/ /agents/concepts/sessions/ 301 +/agents/concepts/agent-skills/ /agents/concepts/skills/ 301 +/agents/skills/sources/computer/ /agents/skills/sources/#workspace 301 +/agents/skills/sources/r2/ /agents/skills/sources/#r2 301 +/agents/skills/sources/code/ /agents/skills/sources/#code 301 /agents/getting-started/prompting/ /workers/get-started/prompting/ 301 /agents/platform/prompting/ /workers/get-started/prompting/ 301 /agents/platform/prompt.txt/ /workers/prompt.txt 301 diff --git a/src/content/changelog/agents/2026-10-29-agent-skills-from-files.mdx b/src/content/changelog/agents/2026-10-29-agent-skills-from-files.mdx index d8f64d867af..6fee44e11c7 100644 --- a/src/content/changelog/agents/2026-10-29-agent-skills-from-files.mdx +++ b/src/content/changelog/agents/2026-10-29-agent-skills-from-files.mdx @@ -29,7 +29,7 @@ export class MyAgent extends Think { This release includes: -- **Three sources.** Load skills from a [Computer Workspace](/agents/skills/sources/computer/), an [R2 bucket](/agents/skills/sources/r2/), or [code](/agents/skills/sources/code/). Think lists every source again before each turn. +- **Three sources.** Load skills from a [Computer Workspace](/agents/skills/sources/#workspace), an [R2 bucket](/agents/skills/sources/#r2), or [code](/agents/skills/sources/#code). Think lists every source again before each turn. - **The open format.** Every `SKILL.md` is checked against the [Agent Skills specification](https://agentskills.io/specification). As the specification's guide for agents recommends, a skill that breaks a rule still loads, with a logged warning. Use `parseSkill()` to check a skill before you write it. - **Scripts in the Workspace.** Skill scripts are Workspace files, and the model runs them with the Workspace `exec` tool. Refer to [Execution](/agents/skills/execution/). - **Skill tools for any harness.** Use the same `activate_skill` and `read_skill_resource` tools in Think, Pi Durable, or the AI SDK. Refer to [Skills tools](/agents/tools/skills/). diff --git a/src/content/docs/agents/agent-class/index.mdx b/src/content/docs/agents/agent-class/index.mdx index 6009f0be474..27be3817256 100644 --- a/src/content/docs/agents/agent-class/index.mdx +++ b/src/content/docs/agents/agent-class/index.mdx @@ -107,7 +107,7 @@ flowchart TD | **Diagnostics channels** | `subscribe()`, diagnostics channels | [Diagnostics channels](/agents/runtime/observability/diagnostics-channels/) | | **Sub-agents** | `subAgent()`, `abortSubAgent()`, `deleteSubAgent()` | [Sub-agents](/agents/agent-class/sub-agents/) | | **Agents as tools** | `runAgentTool()`, `clearAgentToolRuns()`, `hasAgentToolRun()` | [Agents as tools](/agents/agent-class/agent-tools/) | -| **Agent Skills** | `SKILL.md` parser, Workspace, R2, and manifest sources, script runner | [Agent Skills](/agents/skills/) | +| **Skills** | `SKILL.md` parser, Workspace, R2, and manifest sources, script runner | [Skills](/agents/skills/) | | **Sessions** | `Sessions` capability: message history, branches, compaction, search | [Sessions](/agents/runtime/capabilities/sessions/) | | **Think** | `Think` base class, workspace tools, lifecycle hooks, extensions | [Think](/agents/harnesses/think/) | | **Chat SDK** | `createChatSdkState()`, `ChatSdkStateAgent` | [Chat SDK](/agents/agent-class/communication-channels/chat-sdk/) | diff --git a/src/content/docs/agents/concepts/context.mdx b/src/content/docs/agents/concepts/context.mdx index d9697eb7b91..b157789829e 100644 --- a/src/content/docs/agents/concepts/context.mdx +++ b/src/content/docs/agents/concepts/context.mdx @@ -18,7 +18,7 @@ Context is everything the model is told on a request besides the conversation it **Tool descriptions.** Each tool's name, description, and input schema. The model reads them to decide which tool to call, so they go in every request, and a long tool list costs tokens on every turn. -**Skills.** A [skill](/agents/concepts/agent-skills/) is a set of instructions and files for one kind of task. The request carries a short catalog, one line per skill, and the model loads a skill's instructions with a tool call only when a task needs them. For most agents, skills are the way to add knowledge and procedures: they cost little until they are used, and other agents can read the same skill files. +**Skills.** A [skill](/agents/concepts/skills/) is a set of instructions and files for one kind of task. The request carries a short catalog, one line per skill, and the model loads a skill's instructions with a tool call only when a task needs them. For most agents, skills are the way to add knowledge and procedures: they cost little until they are used, and other agents can read the same skill files. **Memory.** Facts the agent saves so they outlast the conversation that taught them, such as the user's time zone or the decision made last week. The agent writes them with a tool to storage you choose, such as SQLite in the Durable Object, and they reach the model again either in the system prompt or when the agent looks them up with a search tool. Memory is not the transcript: compaction can summarize the conversation that taught a fact, and the saved fact stays as it was. @@ -58,12 +58,12 @@ To see whether you are hitting the cache, read the usage the provider returns wi ## How each harness builds context - **[Think](/agents/harnesses/think/context/)** builds its system prompt from context blocks that you declare: read-only instructions, memory the model can write, and knowledge it can search. It freezes the prompt after the first turn, refreshes it after compaction, and sends changes to the environment after the latest message. Its skill catalog is one of those blocks. -- **[Pi](/agents/harnesses/pi/extensions/)** builds its system prompt from sections that extensions add. Its [skills extension](/agents/harnesses/pi/extensions/#add-skills) adds the skill catalog as a section. +- **[Pi](/agents/harnesses/pi/extensions/)** builds its system prompt from sections that extensions add. Its [skills extension](/agents/harnesses/pi/skills/) adds the skill catalog as a section. - **[OpenCode](/agents/harnesses/opencode/)** builds its prompt from its own configuration and the plugins you bundle. - **Your own loop** builds the request itself. [`agents/skills`](/agents/skills/) returns a skill catalog to add to your system prompt, and the order rules in [Prompt caching](#prompt-caching) are yours to follow. ## Related - [Sessions](/agents/concepts/sessions/): the transcript, and how it becomes a request. -- [Skills](/agents/concepts/agent-skills/): instructions and files the model loads on demand. +- [Skills](/agents/concepts/skills/): instructions and files the model loads on demand. - [Think context](/agents/harnesses/think/context/): context blocks in Think. diff --git a/src/content/docs/agents/concepts/agent-skills.mdx b/src/content/docs/agents/concepts/skills.mdx similarity index 73% rename from src/content/docs/agents/concepts/agent-skills.mdx rename to src/content/docs/agents/concepts/skills.mdx index d8b98bd96da..9cf599e6958 100644 --- a/src/content/docs/agents/concepts/agent-skills.mdx +++ b/src/content/docs/agents/concepts/skills.mdx @@ -1,7 +1,7 @@ --- -title: Agent Skills +title: Skills pcx_content_type: concept -description: What Agent Skills are, how agents load them through progressive disclosure, what the open SKILL.md format specifies, and what you can build with them. +description: What skills are, how agents load them through progressive disclosure, what the open Agent Skills format specifies, and what you can build with them. tags: - AI sidebar: @@ -10,9 +10,9 @@ products: - agents --- -[Agent Skills](https://agentskills.io/) are an open format for giving an agent specialized knowledge and repeatable workflows. A skill is a folder with a `SKILL.md` file. The file holds a name, a description, and instructions for one kind of task. The folder can also hold scripts, reference documents, templates, and other files. +A skill gives an agent specialized knowledge or a repeatable workflow. Skills follow [Agent Skills](https://agentskills.io/), an open format: a skill is a folder with a `SKILL.md` file. The file holds a name, a description, and instructions for one kind of task. The folder can also hold scripts, reference documents, templates, and other files. -Agent Skills was released as an open standard. Many agents support the standard, so a skill written once works across compatible agents. The [specification](https://agentskills.io/specification) defines what goes inside a skill folder. It does not define where skills are stored or how an agent loads them. +Many agents support the format, so a skill written once works across compatible agents. The [specification](https://agentskills.io/specification) defines what goes inside a skill folder. It does not define where skills are stored or how an agent loads them. ## Why skills @@ -119,19 +119,16 @@ A skill adds instructions to the agent's context, and its scripts can run code. ## Skills in the Agents SDK -The Agents SDK reads the open format in `agents/skills` and checks every `SKILL.md` against the specification. As the specification's guide for agents recommends, it loads skills that break a rule and logs a warning, so skills written for other agents still work. Skills that cannot be used, such as ones without a description, are skipped. To check a skill when you write it, parse it with `parseSkill()` and require no warnings. It loads skills from three sources: +The Agents SDK reads the format in `agents/skills` and checks every `SKILL.md` against the specification. As the specification's guide for agents recommends, it loads skills that break a rule and logs a warning, so skills written for other agents still work. Skills that cannot be used, such as ones without a description, are skipped. To check a skill when you write it, parse it with `parseSkill()` and require no warnings. -| Source | Use it when | -| ------------------------------------------------------ | ---------------------------------------------------------------------------------- | -| [Computer Workspace](/agents/skills/sources/computer/) | Skills should be files the agent, a user, or your code can add and edit at runtime | -| [R2](/agents/skills/sources/r2/) | Many agents share one set of skills managed outside your Worker | -| [Code](/agents/skills/sources/code/) | Tests, or a small fixed set of skills that ships with your Worker | +A skill is one kind of [context](/agents/concepts/context/), and it reaches the model in two parts. The catalog, one line per skill with its name and description, goes into the system prompt. The model then loads a skill's instructions with the `activate_skill` tool and its files with `read_skill_resource`. A loaded skill comes back as a tool result, so it is part of the [session](/agents/concepts/sessions/) from then on, like any other message. -The SDK uses the `.agents/skills/` folder convention that several agents share, so `/workspace/.agents/skills/` in a Workspace holds the agent's skills. The model loads skills with the [skill tools](/agents/tools/skills/) `activate_skill` and `read_skill_resource`. With a Workspace backend, the model runs skill scripts with the Workspace `exec` tool. Refer to [Execution](/agents/skills/execution/). +`agents/skills` builds the catalog and the tools. It does not decide where the catalog goes or how often it is reloaded: each harness does. Think puts the catalog in a [context block](/agents/harnesses/think/context/) and lists the sources again before each turn. Pi adds it as a section of its system prompt when its skills extension is built. + +Skills come from three kinds of [source](/agents/skills/sources/): a Computer Workspace, an R2 bucket, or code. The SDK uses the `.agents/skills/` folder convention that several agents share, so `/workspace/.agents/skills/` in a Workspace holds the agent's skills. Refer to [Execution](/agents/skills/execution/) for running skill scripts. ## Related -- [Agent Skills](/agents/skills/): use skills in Think, Pi, and the AI SDK +- [Skills](/agents/skills/): load skills into an agent with `agents/skills` - [Skills tools](/agents/tools/skills/): add the skill tools to your harness -- [Sessions](/agents/concepts/sessions/): the transcript, where a loaded skill's instructions are kept - [Agent Skills specification](https://agentskills.io/specification): the full format diff --git a/src/content/docs/agents/harnesses/opencode.mdx b/src/content/docs/agents/harnesses/opencode.mdx index 89c45bad53c..444efa842a5 100644 --- a/src/content/docs/agents/harnesses/opencode.mdx +++ b/src/content/docs/agents/harnesses/opencode.mdx @@ -310,7 +310,7 @@ export default Plugin.define({ Import this plugin and add it to `plugins`. Refer to [OpenCode MCP transforms](https://opencode.ai/v2/docs/build/plugins/#mcp) for server configuration. -## Add an Agent Skill +## Add a skill Register skill content through a bundled plugin: diff --git a/src/content/docs/agents/harnesses/pi/extensions.mdx b/src/content/docs/agents/harnesses/pi/extensions.mdx index 5c89011f4e2..c9692d017a7 100644 --- a/src/content/docs/agents/harnesses/pi/extensions.mdx +++ b/src/content/docs/agents/harnesses/pi/extensions.mdx @@ -130,32 +130,7 @@ this.registry.install({ ## Add skills -`skills(sources)` from `agents/harness/pi` turns [Agent Skills](/agents/skills/) sources from `agents/skills` into a Pi extension named `agents.skills`. Sources can read skills from a [Computer Workspace](/agents/skills/sources/computer/), [R2](/agents/skills/sources/r2/), or a [manifest](/agents/skills/sources/code/). The extension has two tools, `activate_skill` and `read_skill_resource`, and a `skills` section that lists the skills. It reads the sources, so install it in an `async` factory: - -```ts -import { skills } from "agents/harness/pi"; -import { manifest } from "agents/skills"; - -const handbook = manifest({ - id: "handbook", - skills: [ - { - name: "release-notes", - description: "Write release notes.", - body: "One line per change.", - }, - ], -}); - -harness = new PiHarness({ - harness: async ({ storage, context, onWake }) => { - this.registry.install(await skills([handbook])); - // ...models, then Harness.open() with onWake - }, -}); -``` - -The tools match the ones [Think](/agents/harnesses/think/) offers, so a skill written for one works in the other. The sources are read once, when the factory builds the extension. The model can read a skill's instructions and resources. To run skill scripts, give the model a shell or Workspace tool. Refer to [Execution](/agents/skills/execution/). +The `skills()` extension from `agents/harness/pi` adds the skill tools and a `skills` prompt section. Refer to [Skills](/agents/harnesses/pi/skills/). ## Use more of Pi Durable diff --git a/src/content/docs/agents/harnesses/pi/skills.mdx b/src/content/docs/agents/harnesses/pi/skills.mdx new file mode 100644 index 00000000000..47108238321 --- /dev/null +++ b/src/content/docs/agents/harnesses/pi/skills.mdx @@ -0,0 +1,87 @@ +--- +title: Skills +pcx_content_type: how-to +description: Load skills into a Pi agent with the skills() extension from agents/harness/pi, from a Workspace, R2, or code. +sidebar: + order: 2 +products: + - agents +--- + +import { TypeScriptExample } from "~/components"; + +`skills(sources)` from `agents/harness/pi` turns [skills](/agents/skills/) sources from `agents/skills` into a Pi [extension](/agents/harnesses/pi/extensions/) named `agents.skills`. Sources can read skills from a [Computer Workspace](/agents/skills/sources/#workspace), [R2](/agents/skills/sources/#r2), or a [manifest](/agents/skills/sources/#code). + +:::note +Skills are experimental. This page describes `agents` v0.34.0 with Pi 1.6. +::: + +## Install the extension + +The extension has two tools, `activate_skill` and `read_skill_resource`, and a `skills` section in Pi's system prompt that lists each skill's name and description. It reads the sources when it is built, so install it in an `async` harness factory, before `Harness.open()`: + + + +```ts +import { DurableObject } from "cloudflare:workers"; +import { createModels } from "@earendil-works/pi-ai/models"; +import { createRegistry, Harness } from "@earendil-works/pi-durable"; +import { PiHarness, skills } from "agents/harness/pi"; +import { Lifecycle } from "agents/lifecycle"; +import { createAI } from "agents/models/pi-ai"; +import { manifest, r2 } from "agents/skills"; + +const handbook = manifest({ + id: "handbook", + skills: [ + { + name: "release-notes", + description: "Write release notes.", + body: "One line per change.", + }, + ], +}); + +export class Assistant extends DurableObject { + ai = createAI({ binding: this.env.AI }); + registry = createRegistry(); + + harness = new PiHarness({ + harness: async ({ storage, context, onWake }) => { + this.registry.install( + await skills([ + r2(this.env.SKILLS_BUCKET, { prefix: "skills/" }), + handbook, + ]), + ); + const models = createModels(); + models.setProvider(this.ai.provider); + return Harness.open( + storage, + { models, registry: this.registry, onWake }, + context, + ); + }, + defaults: { model: this.ai("@cf/deepseek-ai/deepseek-v4.1-flash-1017") }, + }); + + lifecycle = Lifecycle.install(this).use(this.harness); +} +``` + + + +Both tools are marked `replay: "safe"`. A tool failure, such as an unknown skill or a missing file, returns `isError: true`, so the model sees the error and can try again. The extension logs problems with a source or a skill with the prefix `pi skills:`. + +## When skills change + +The extension reads the sources once, when the factory builds it. A skill added to the Workspace or the bucket after that is not in the catalog until the factory runs again, which happens when the object starts. + +## Run skill scripts + +The model can read a skill's instructions and resources. The extension has no `run_skill_script` tool. To run skill scripts, give the model a shell or Workspace tool. Refer to [Execution](/agents/skills/execution/). + +## Related + +- [Skills](/agents/skills/): the format, sources, and validation +- [Extensions](/agents/harnesses/pi/extensions/): install extensions in the Pi harness factory diff --git a/src/content/docs/agents/harnesses/think/configuration.mdx b/src/content/docs/agents/harnesses/think/configuration.mdx index fa391473b60..dfd36f83466 100644 --- a/src/content/docs/agents/harnesses/think/configuration.mdx +++ b/src/content/docs/agents/harnesses/think/configuration.mdx @@ -32,7 +32,7 @@ Think is configured by overriding methods and properties on your `Think` subclas | `hydrationByteBudget` | 32 MiB | Byte budget for loading the transcript at startup | | `mediaEviction` | `true` | Move aged media out of the conversation and into a workspace file. `false` keeps it in the conversation | | `truncationStep` | `8` | Move the read-time truncation cutoff once every this many messages. `1` cuts every turn — refer to [Sessions](/agents/harnesses/think/sessions/#prompt-caching) | -| `getSkills()` | `[]` | Return Agent Skills sources for on-demand skill activation — refer to [Agent Skills](/agents/skills/) | +| `getSkills()` | `[]` | Return skill sources for on-demand skill activation — refer to [Skills](/agents/harnesses/think/skills/) | | `getSkillScriptRunner()` | `null` | Enable the experimental `run_skill_script` tool — refer to [Execution](/agents/skills/execution/) | | `fetchTools` | `false` | Turn on the allowlisted, read-only fetch tools — refer to [Fetch tool](/agents/harnesses/think/tools/#fetch-tool) | | `messageConcurrency` | `"queue"` | How overlapping submits behave — refer to [Client tools](/agents/harnesses/think/client-tools/#message-concurrency) | @@ -134,4 +134,4 @@ Bundled with `@cloudflare/think`: | ---------------------- | ---------------------------------------- | | `@cloudflare/codemode` | Code execution for `createExecuteTool()` | -The Agent Skills parser, sources, and script runner live in [`agents/skills`](/agents/skills/), so the script runner pulls `just-bash` through `agents`, not Think. +The skills parser, sources, and script runner live in [`agents/skills`](/agents/skills/), so the script runner pulls `just-bash` through `agents`, not Think. diff --git a/src/content/docs/agents/harnesses/think/index.mdx b/src/content/docs/agents/harnesses/think/index.mdx index a9099b7c300..abab7f736b7 100644 --- a/src/content/docs/agents/harnesses/think/index.mdx +++ b/src/content/docs/agents/harnesses/think/index.mdx @@ -318,16 +318,31 @@ Use [`startFiber()`](/agents/agent-class/durable-execution/#startfiber) outside href="/agents/harnesses/think/getting-started/" description="Build a Think agent step by step." /> - + + + + - - - ## Acknowledgments diff --git a/src/content/docs/agents/harnesses/think/skills.mdx b/src/content/docs/agents/harnesses/think/skills.mdx new file mode 100644 index 00000000000..78f6113c67e --- /dev/null +++ b/src/content/docs/agents/harnesses/think/skills.mdx @@ -0,0 +1,177 @@ +--- +title: Skills +pcx_content_type: how-to +description: Load skills into a Think agent with getSkills(), from a Workspace, R2, or code, and run skill scripts with exec or the script runner. +sidebar: + order: 2.7 +products: + - agents +--- + +import { TypeScriptExample, WranglerConfig } from "~/components"; + +Think loads [skills](/agents/skills/) for you. Return sources from `getSkills()`, and Think builds the catalog, adds it to the system prompt, and gives the model the skill tools. This page covers what Think does on top of `agents/skills`. For the format, the sources, and validation, refer to the [Skills](/agents/skills/) section. + +:::note +Skills are experimental. This page describes `@cloudflare/think` v0.24.0. +::: + +## Return sources from getSkills() + +`@cloudflare/think` re-exports `agents/skills` as the `skills` namespace. `this.workspace` is a Computer Workspace, so `skills.workspace(this.workspace)` reads skills from `/workspace/.agents/skills/`: + + + +```ts +import { Think, skills } from "@cloudflare/think"; + +export class MyAgent extends Think { + getSkills() { + return [ + skills.workspace(this.workspace), + skills.r2(this.env.SKILLS_BUCKET, { prefix: "skills/" }), + ]; + } +} +``` + + + +| Source | Reads skills from | +| --------------------------------------------------------- | ---------------------------------------------- | +| [`skills.workspace()`](/agents/skills/sources/#workspace) | Files in the agent's Workspace, or another one | +| [`skills.r2()`](/agents/skills/sources/#r2) | Objects in an R2 bucket | +| [`skills.manifest()`](/agents/skills/sources/#code) | Skills defined in code | + +Sources apply in order, and the first source to provide a skill name wins. Think logs each problem a source reports, such as a duplicate name or a skill with no description, once per object lifetime. If a source fails when the agent starts, Think logs the error and the agent runs without skills. + +## When skills change + +Think lists every source again before each turn, so a new, edited, or deleted skill takes effect on the next turn. If a source fails to list, Think keeps its skills from the last turn. + +The catalog goes in its own [context block](/agents/harnesses/think/context/), below your `getSystemPrompt()` instructions. When the catalog changes mid-conversation, Think leaves the system prompt as it was, so the provider's [prompt cache](/agents/concepts/context/#prompt-caching) still covers it. The new catalog reaches the model as a reminder at the end of the next request, and moves into the system prompt at the next compaction or history clear. + +Use `getSystemPrompt()` or a context block from `configureContext()` for instructions that apply to every turn. Use skills for procedures, references, and scripts that only some tasks need. + +## Skill tools + +When at least one skill is available, Think adds `activate_skill` and `read_skill_resource` to the turn's tools, and `run_skill_script` when you set a script runner. Refer to [Tools](/agents/harnesses/think/tools/) for how they merge with the workspace, custom, MCP, and client tools. + +## Add skills to the Workspace + +Skills in the Workspace are plain files, so the agent's own `write` tool, a user upload, or your code can add one. For example, write a starter skill when the agent starts: + + + +```ts +import { Think, skills } from "@cloudflare/think"; + +const ROOT = "/workspace/.agents/skills"; + +const STARTER_SKILLS: Record = { + "pirate-voice/SKILL.md": `--- +name: pirate-voice +description: Answer in a pirate voice. Use when the user asks for a pirate tone. +--- + +Answer accurately, then rewrite the answer in a light pirate voice. +`, +}; + +export class MyAgent extends Think { + getSkills() { + return [skills.workspace(this.workspace)]; + } + + async onStart() { + for (const [path, content] of Object.entries(STARTER_SKILLS)) { + const file = `${ROOT}/${path}`; + await this.workspace.fs.mkdir(file.slice(0, file.lastIndexOf("/")), { + recursive: true, + }); + await this.workspace.fs.writeFile(file, content); + } + } +} +``` + + + +## Run skill scripts + +### With exec + +Give the Workspace a backend, and Think adds the `exec` tool. Activating a skill tells the model where the skill's folder is, so it can run a script from there: + + + +```ts +import { Workspace } from "@cloudflare/computer"; +import { CodemodeBackend } from "@cloudflare/computer/backends/codemode"; +import { Think, skills } from "@cloudflare/think"; + +export class MyAgent extends Think { + workspace = new Workspace({ + storage: this.ctx.storage, + backends: [ + new CodemodeBackend({ + id: "js", + loader: this.env.LOADER, + access: "read-write", + }), + ], + }); + + getSkills() { + return [skills.workspace(this.workspace)]; + } +} +``` + + + + + +```jsonc +{ + "worker_loaders": [{ "binding": "LOADER" }], +} +``` + + + +Refer to [Execution](/agents/skills/execution/#run-scripts-in-the-workspace) for how scripts run in the Workspace. + +### With the script runner + +For skills that do not live in a Workspace, such as R2 or manifest skills, return the experimental script runner from `getSkillScriptRunner()`. Think then adds `run_skill_script`: + + + +```ts +import { Think, skills } from "@cloudflare/think"; + +export class MyAgent extends Think { + getSkills() { + return [skills.r2(this.env.SKILLS_BUCKET, { prefix: "skills/" })]; + } + + getSkillScriptRunner() { + return skills.runner({ loader: this.env.LOADER }); + } +} +``` + + + +`skills.runner()` takes the options listed in [Execution](/agents/skills/execution/#runner-options). + +## Examples + +- The [`computer-skills` example](https://github.com/cloudflare/agents/tree/main/examples/computer-skills) stores skills in a Workspace, runs their scripts with `exec`, and lets you add and delete skills from the UI. +- The [`r2-skills` example](https://github.com/cloudflare/agents/tree/main/examples/r2-skills) loads skills from a bucket, runs a script with the script runner, and lets you upload and delete skills from the UI. + +## Related + +- [Skills](/agents/skills/): the format, sources, and validation +- [Configuration](/agents/harnesses/think/configuration/): `getSkills()` and `getSkillScriptRunner()` with the other overrides diff --git a/src/content/docs/agents/harnesses/think/tools.mdx b/src/content/docs/agents/harnesses/think/tools.mdx index be1dd8726e4..56cdfdab3d8 100644 --- a/src/content/docs/agents/harnesses/think/tools.mdx +++ b/src/content/docs/agents/harnesses/think/tools.mdx @@ -3,7 +3,7 @@ title: Tools pcx_content_type: reference description: Connect a workspace, and use built-in workspace tools, custom tools, approvals, MCP tools, the fetch tool, code execution, and extensions in Think agents. sidebar: - order: 4 + order: 2.5 products: - agents --- @@ -20,7 +20,7 @@ On every turn, Think merges tools from multiple sources. Later sources override 2. **`getTools()`** — your custom server-side tools 3. **Extension tools** — tools from loaded extensions (prefixed by extension name) 4. **Context tools** — `set_context`, `search_context` (from `configureContext()` and `configureSession()`, refer to [Context](/agents/harnesses/think/context/)) -5. **Skill tools** — `activate_skill`, `read_skill_resource`, `run_skill_script` (from `getSkills()`, refer to [Agent Skills](/agents/skills/)) +5. **Skill tools** — `activate_skill`, `read_skill_resource`, `run_skill_script` (from `getSkills()`, refer to [Skills](/agents/harnesses/think/skills/)) 6. **MCP tools** — from connected MCP servers when `includeMcpTools` is `true` 7. **Client tools** — from the browser (refer to [Client tools](/agents/harnesses/think/client-tools/)) diff --git a/src/content/docs/agents/skills/execution.mdx b/src/content/docs/agents/skills/execution.mdx index c2e86008161..617c9e3cd1e 100644 --- a/src/content/docs/agents/skills/execution.mdx +++ b/src/content/docs/agents/skills/execution.mdx @@ -1,7 +1,7 @@ --- title: Execution pcx_content_type: reference -description: Run Agent Skills scripts with a Computer Workspace backend, or with the experimental skill script runner. +description: Run skill scripts with a Computer Workspace backend, or with the experimental skill script runner. sidebar: order: 3 products: @@ -10,16 +10,65 @@ products: import { TypeScriptExample, WranglerConfig } from "~/components"; -[Agent Skills](/agents/skills/) can bundle scripts under `scripts/`. An agent runs them in one of two ways: +[Skills](/agents/skills/) can bundle scripts under `scripts/`. An agent runs them in one of two ways: -| Where the skill lives | How scripts run | -| ------------------------------------------------------ | ------------------------------------------------------------- | -| [Computer Workspace](/agents/skills/sources/computer/) | The model runs the file with the Workspace `exec` tool | -| [R2](/agents/skills/sources/r2/) or a [manifest](/agents/skills/sources/code/) | The experimental script runner, through `run_skill_script` | +| Where the skill lives | How scripts run | +| ------------------------------------------------------------------------------ | ---------------------------------------------------------- | +| [Computer Workspace](/agents/skills/sources/#workspace) | The model runs the file with the Workspace `exec` tool | +| [R2](/agents/skills/sources/#r2) or a [manifest](/agents/skills/sources/#code) | The experimental script runner, through `run_skill_script` | ## Run scripts in the Workspace -When skills are files in a Workspace, a script is a file the agent can run like any other. Give the Workspace a backend, and Think adds the `exec` tool. Refer to [Computer](/agents/skills/sources/computer/#run-skill-scripts) for the setup. +When skills are files in a Workspace, a script is a file the agent can run like any other. Give the Workspace a backend, and add the Workspace tools from `@cloudflare/computer/tools/ai-sdk` next to the skill tools. The `exec` tool runs code with the Workspace's backends: + + + +```ts +import { DurableObject } from "cloudflare:workers"; +import { Workspace } from "@cloudflare/computer"; +import { CodemodeBackend } from "@cloudflare/computer/backends/codemode"; +import { createAITools } from "@cloudflare/computer/tools/ai-sdk"; +import { Lifecycle } from "agents/lifecycle"; +import { loadSkillCatalog, workspace } from "agents/skills"; +import { createSkillTools } from "agents/skills/tools/ai-sdk"; + +export class Assistant extends DurableObject { + readonly lifecycle = Lifecycle.install(this); + readonly workspace = new Workspace({ + storage: this.ctx.storage, + backends: [ + new CodemodeBackend({ + id: "js", + loader: this.env.LOADER, + access: "read-write", + }), + ], + }); + + async tools() { + const catalog = await loadSkillCatalog([workspace(this.workspace)]); + return { + // read, write, edit, exec, and the other Workspace tools + ...createAITools({ workspace: this.workspace }), + ...createSkillTools(catalog), + }; + } +} +``` + + + + + +```jsonc +{ + "worker_loaders": [{ "binding": "LOADER" }], +} +``` + + + +Pass the result of `tools()` to `streamText()`, as in the [overview](/agents/skills/#load-skills-into-an-agent). Activating a skill returns its folder as `location`, for example `/workspace/.agents/skills/release-notes`. With the JavaScript backend, the skill's instructions can tell the model to run a script from that folder: @@ -44,7 +93,7 @@ export default async function (input) { The script runner is experimental, and its options may change. Prefer storing skills in a Computer Workspace and running scripts with `exec`. ::: -For skills that do not live in a Workspace, `skills.runner()` runs bundled scripts in a Dynamic Worker and adds the `run_skill_script` tool. It needs a Worker Loader binding: +For skills that do not live in a Workspace, `runner()` from `agents/skills/runner` runs bundled scripts in a Dynamic Worker. Pass it to `createSkillTools()` and the model gets the `run_skill_script` tool. The runner needs a Worker Loader binding: @@ -56,38 +105,27 @@ For skills that do not live in a Workspace, `skills.runner()` runs bundled scrip -Return the runner from `getSkillScriptRunner()`: - - - -```ts -import { Think, skills } from "@cloudflare/think"; - -export class MyAgent extends Think { - getSkills() { - return [skills.r2(this.env.SKILLS_BUCKET, { prefix: "skills/" })]; - } - - getSkillScriptRunner() { - return skills.runner({ loader: this.env.LOADER }); - } -} -``` - - - -Outside Think, import the runner from `agents/skills/runner`. It is a separate entry point because it loads `just-bash` and Code Mode: - ```ts +import { loadSkillCatalog, r2 } from "agents/skills"; import { runner } from "agents/skills/runner"; - -const scriptRunner = runner({ loader: env.LOADER }); +import { createSkillTools } from "agents/skills/tools/ai-sdk"; + +export async function skillTools(env: Env) { + const catalog = await loadSkillCatalog([ + r2(env.SKILLS_BUCKET, { prefix: "skills/" }), + ]); + return createSkillTools(catalog, { + scriptRunner: runner({ loader: env.LOADER }), + }); +} ``` +`agents/skills/runner` is a separate entry point because it loads `just-bash` and Code Mode. + The runner runs JavaScript, Python, and Bash scripts under `scripts/`. Python runs as a Python Dynamic Worker, and Bash runs through `just-bash`. There is no bundler at runtime: a JavaScript script must be one self-contained module. Compile TypeScript and multi-file scripts, for example with esbuild, before you store the skill. ### JavaScript scripts @@ -106,12 +144,12 @@ export default async function run(input, ctx) { `ctx` is typed as `SkillRunContext` and contains: -| Field | Description | -| ----------- | --------------------------------------------------------------------------------- | -| `skill` | The skill's name, description, and metadata | -| `files` | The skill's text resources, by relative path | -| `workspace` | Workspace reads and writes, gated by the `workspace` option | -| `tools` | Only the tools passed to the runner, as `tools.call(name, input)` or `tools.name(input)` | +| Field | Description | +| ----------- | -------------------------------------------------------------------------------------------- | +| `skill` | The skill's name, description, and metadata | +| `files` | The skill's text resources, by relative path | +| `workspace` | Workspace reads and writes, gated by the `workspace` option | +| `tools` | Only the tools passed to the runner, as `tools.call(name, input)` or `tools.name(input)` | | `output` | `writeFile(name, content)` returns scratch files to the model without changing the Workspace | ### Python and Bash scripts @@ -127,18 +165,18 @@ Python scripts can also define `def run(input, ctx)`. ### Runner options -| Option | Default | Description | -| ------------------- | ---------------------------------------- | ---------------------------------------------------- | -| `loader` | Required | Worker Loader binding | -| `workspaceInstance` | None | A Computer Workspace that scripts can access | +| Option | Default | Description | +| ------------------- | ------------------------------------------------ | -------------------------------------------- | +| `loader` | Required | Worker Loader binding | +| `workspaceInstance` | None | A Computer Workspace that scripts can access | | `workspace` | `"read"` with `workspaceInstance`, else `"none"` | `"none"`, `"read"`, or `"read-write"` | -| `tools` | None | Tools scripts can call | -| `network` | `false` | Allow outbound network access | -| `timeout` | `30000` | Timeout in milliseconds | +| `tools` | None | Tools scripts can call | +| `network` | `false` | Allow outbound network access | +| `timeout` | `30000` | Timeout in milliseconds | Scripts get no network access and no tools unless you grant them. Relative paths in `ctx.workspace` resolve against `/workspace`. ## Related -- [Think tools](/agents/harnesses/think/tools/): how skill tools merge with workspace, custom, MCP, and client tools +- [File system](/agents/tools/filesystem/): the Workspace tools, including `exec` - [Sandbox](/agents/tools/sandbox/): run code in a container diff --git a/src/content/docs/agents/skills/index.mdx b/src/content/docs/agents/skills/index.mdx index 5914137777e..07035d6f68e 100644 --- a/src/content/docs/agents/skills/index.mdx +++ b/src/content/docs/agents/skills/index.mdx @@ -1,24 +1,23 @@ --- -title: Agent Skills +title: Skills pcx_content_type: overview -description: Give an agent on-demand instructions, references, and scripts as Agent Skills, loaded from a Computer Workspace, R2, or code. +description: Give an agent on-demand instructions, references, and scripts as skills, loaded from a Computer Workspace, R2, or code with agents/skills. sidebar: order: 4.8 group: hideIndex: false - label: Skills products: - agents --- import { DirectoryListing, TypeScriptExample } from "~/components"; -[Agent Skills](https://agentskills.io/) are on-demand instructions, references, and scripts for an agent. Each skill is a directory with a `SKILL.md` file. The agent sees a short catalog of skill names and descriptions. It loads a full skill only when a task matches it, so a large library of skills does not fill every prompt. +A skill is a set of instructions, references, and scripts that an agent loads when a task needs it. Each skill is a directory with a `SKILL.md` file, in the open [Agent Skills](https://agentskills.io/) format. The agent sees a short catalog of skill names and descriptions, and loads a full skill only when a task matches it, so a large library of skills does not fill every prompt. -For background on the format and how agents use it, refer to [Agent Skills concepts](/agents/concepts/agent-skills/). +`agents/skills` reads skills from a source, merges them into a catalog, and builds the tools the model uses to load them. It works with any harness. For background on the format, refer to [Skills concepts](/agents/concepts/skills/). :::note -Agent Skills are experimental. The API may change in a future release. These pages describe [Agents SDK v0.32.0](https://github.com/cloudflare/agents/releases/tag/agents%400.32.0) and Think v0.23.0 or later. +Skills are experimental. The API may change in a future release. These pages describe `agents` v0.34.0. ::: ## What a skill looks like @@ -51,52 +50,101 @@ The files in `scripts/`, `references/`, and `assets/` are the skill's resources. ## Load skills into an agent -Skills come from a source. Return sources from `getSkills()` in a [Think](/agents/harnesses/think/) agent: +To give a model skills, you need three pieces: - +1. One or more [sources](/agents/skills/sources/) say where the skills live. +2. `loadSkillCatalog()` lists every source and merges the skills into a catalog. +3. `catalog.prompt()` returns the catalog text for the system prompt, and `createSkillTools()` from `agents/skills/tools/ai-sdk` returns the [AI SDK](https://ai-sdk.dev/) tools that load a skill. + +This Durable Object stores its conversation in [`Sessions`](/agents/runtime/capabilities/sessions/) and loads skills from an R2 bucket on every turn: + + ```ts -import { Think, skills } from "@cloudflare/think"; - -export class MyAgent extends Think { - getSkills() { - return [ - skills.workspace(this.workspace), - skills.r2(this.env.SKILLS_BUCKET, { prefix: "skills/" }), - ]; +import { DurableObject } from "cloudflare:workers"; +import { + convertToModelMessages, + stepCountIs, + streamText, + type UIMessage, +} from "ai"; +import { Lifecycle } from "agents/lifecycle"; +import { createAI } from "agents/models/ai-sdk"; +import { Sessions } from "agents/sessions"; +import { loadSkillCatalog, r2, type SkillCatalog } from "agents/skills"; +import { createSkillTools } from "agents/skills/tools/ai-sdk"; + +export class Assistant extends DurableObject { + readonly ai = createAI({ binding: this.env.AI }); + readonly sessions = new Sessions(); + readonly lifecycle = Lifecycle.install(this).use(this.sessions); + readonly session = this.sessions.session(); + readonly sources = [r2(this.env.SKILLS_BUCKET, { prefix: "skills/" })]; + catalog: SkillCatalog | undefined; + + async onRequest(request: Request): Promise { + await this.session.appendMessage(await request.json(), { + source: "client", + }); + + // List the sources again each turn, so new and edited skills take effect. + // `previous` keeps a source's last skills if it fails to list. + this.catalog = await loadSkillCatalog(this.sources, { + previous: this.catalog, + }); + + const { messages } = await this.session.getRecentHistory(8 * 1024 * 1024); + const result = streamText({ + model: this.ai("@cf/moonshotai/kimi-k2.7-code"), + system: ["You are a helpful assistant.", this.catalog.prompt()] + .filter(Boolean) + .join("\n\n"), + messages: await convertToModelMessages(messages as UIMessage[]), + tools: createSkillTools(this.catalog), + stopWhen: stepCountIs(10), + }); + + return result.toUIMessageStreamResponse({ + onFinish: async ({ responseMessage }) => { + await this.session.appendMessage(responseMessage); + }, + }); } } ``` -| Source | Reads skills from | -| -------------------------------------------------------- | ---------------------------------------------------------- | -| [`skills.workspace()`](/agents/skills/sources/computer/) | Files in a `@cloudflare/computer` Workspace | -| [`skills.r2()`](/agents/skills/sources/r2/) | Objects in an R2 bucket | -| [`skills.manifest()`](/agents/skills/sources/code/) | Skills defined in code | +Sources apply in order. The first source to provide a skill name wins, and later duplicates are reported in `catalog.diagnostics`. A source that fails to list, or a skill that cannot be used, becomes a diagnostic too, so one bad source never hides the others. + +## Skill tools -Sources are applied in order. The first source to provide a skill name wins. Later duplicates are skipped with a logged warning. Think lists every source again before each turn, so new, edited, and deleted skills take effect on the next turn. +`createSkillTools(catalog)` returns these tools when the catalog has at least one skill, and none otherwise: -The [Pi harness](/agents/harnesses/pi/extensions/#add-skills) takes the same sources through `skills(sources)` from `agents/harness/pi`. +| Tool | Purpose | +| --------------------- | -------------------------------------------------------------------------------- | +| `activate_skill` | Load a skill's instructions, its folder when it has one, and its resource list | +| `read_skill_resource` | Read a resource by `{ name, path }` or a qualified path such as `name/path` | +| `run_skill_script` | Run a bundled script, when you pass a [script runner](/agents/skills/execution/) | -## Skill tools +The model can only pass the name of a skill in the catalog. The tools throw on a failure, such as a missing file, and the AI SDK returns the error to the model. For a harness that is not built on the AI SDK, call `catalog.activate()` and `catalog.readResource()` from your own tool handlers. Refer to [Skills tools](/agents/tools/skills/). -When at least one skill is available, the agent gets these tools: +## The catalog and the prompt cache -| Tool | Purpose | -| --------------------- | ----------------------------------------------------------------------------- | -| `activate_skill` | Load a skill's instructions and its list of resources | -| `read_skill_resource` | Read a resource by `{ name, path }` or a qualified path such as `name/path` | -| `run_skill_script` | Run a bundled script, when a [script runner](/agents/skills/execution/) is set | +`catalog.prompt()` lists each skill's name and description: -The catalog is added to the system prompt as its own context block, below your `getSystemPrompt()` instructions. When a skill changes mid-conversation, Think does not rebuild the system prompt, so the model provider's prompt cache stays warm. The new catalog reaches the model as a reminder on the next turn and moves into the system prompt at the next compaction or history clear. +```txt +Available skills. When a task matches a skill, use activate_skill with its name before proceeding. -Use `getSystemPrompt()` or a context block from `configureContext()` for instructions that apply to every turn. Use skills for procedures, references, and scripts that only some tasks need. +- release-notes: Turn a list of changes into release notes. +- pirate-voice: Answer in a pirate voice. +``` + +It returns `null` when there are no skills. The catalog text is part of the system prompt, so adding, editing, or deleting a skill mid-conversation changes the prompt, and the provider's [prompt cache](/agents/concepts/context/#prompt-caching) starts over from that point. `catalog.fingerprint` changes whenever `prompt()` would, so compare it between turns to tell whether the prompt changed. Your harness decides how often to reload the catalog and where in the prompt to put it, and each harness's skills page says what it does. ## Validation -Every `SKILL.md` is checked against these rules from the [Agent Skills specification](https://agentskills.io/specification): +`agents/skills` checks every `SKILL.md` against these rules from the [Agent Skills specification](https://agentskills.io/specification): - `name` must be 1 to 64 lowercase letters, digits, and hyphens. It cannot start or end with a hyphen or contain `--`. It must match the skill's directory name. - `description` is required and can be at most 1,024 characters. @@ -104,22 +152,22 @@ Every `SKILL.md` is checked against these rules from the [Agent Skills specifica - `metadata`, when present, must map string keys to string values. - `allowed-tools`, when present, must be a space-separated string. -Skills load the way the specification's [guide for agents](https://agentskills.io/client-implementation/adding-skills-support) recommends. Skills written for other agents often break a rule, so a skill that breaks one still loads, and the agent logs a warning: +Skills load the way the specification's [guide for agents](https://agentskills.io/client-implementation/adding-skills-support) recommends. Skills written for other agents often break a rule, so a skill that breaks one still loads, with a warning: -| Problem | What happens | -| --- | --- | -| `name` breaks the format, is too long, or does not match the directory | Loads, with a warning | -| `name` is missing | Loads with the directory name, with a warning | -| `description` or `compatibility` is too long | Loads, with a warning | -| `license`, `metadata`, or `allowed-tools` is invalid | Loads without that field, with a warning | -| A value contains an unquoted `: `, so the YAML does not parse | Loads after quoting the value, with a warning | -| `description` is missing | Skipped and logged | -| `name` contains `/`, `\`, or control characters, or is `.` or `..` | Skipped and logged | -| The frontmatter is missing or still not YAML | Skipped and logged | +| Problem | What happens | +| ---------------------------------------------------------------------- | --------------------------------------------- | +| `name` breaks the format, is too long, or does not match the directory | Loads, with a warning | +| `name` is missing | Loads with the directory name, with a warning | +| `description` or `compatibility` is too long | Loads, with a warning | +| `license`, `metadata`, or `allowed-tools` is invalid | Loads without that field, with a warning | +| A value contains an unquoted `: `, so the YAML does not parse | Loads after quoting the value, with a warning | +| `description` is missing | Skipped, with a diagnostic | +| `name` contains `/`, `\`, or control characters, or is `.` or `..` | Skipped, with a diagnostic | +| The frontmatter is missing or still not YAML | Skipped, with a diagnostic | Skipped skills are not shown to the model. Frontmatter keys that the specification does not define are kept in `skill.extraFields`. A skill with `disable-model-invocation: true` is not shown to the model. -To check a skill before you write or publish it, parse it with `parseSkill()` from `agents/skills`. A skill follows the specification when it parses with no warnings: +To check a skill before you write or publish it, parse it with `parseSkill()`. A skill follows the specification when it parses with no warnings: @@ -138,29 +186,9 @@ if (!parsed.ok) { -## Use skills without Think - -The parser, sources, and catalog live in `agents/skills` and do not depend on Think. `loadSkillCatalog()` merges sources. `createSkillTools()` from `agents/skills/tools/ai-sdk` builds the [AI SDK](https://ai-sdk.dev/) tools for a catalog: +## Skills in a harness - - -```ts -import { streamText } from "ai"; -import { loadSkillCatalog, workspace } from "agents/skills"; -import { createSkillTools } from "agents/skills/tools/ai-sdk"; - -// `computerWorkspace` is a Workspace from @cloudflare/computer. -const catalog = await loadSkillCatalog([workspace(computerWorkspace)]); - -const result = streamText({ - model, - system: catalog.prompt() ?? undefined, - tools: createSkillTools(catalog), - messages, -}); -``` - - +A harness can do all of this for you. [Think](/agents/harnesses/think/skills/) takes sources from `getSkills()`, and [Pi](/agents/harnesses/pi/skills/) takes them through its `skills()` extension. Both use the same sources and the same tools as the example in [Load skills into an agent](#load-skills-into-an-agent). ## Pages in this section diff --git a/src/content/docs/agents/skills/sources.mdx b/src/content/docs/agents/skills/sources.mdx new file mode 100644 index 00000000000..d49f383820f --- /dev/null +++ b/src/content/docs/agents/skills/sources.mdx @@ -0,0 +1,215 @@ +--- +title: Sources +pcx_content_type: how-to +description: Load skills from a Computer Workspace, an R2 bucket, or code, and combine sources with agents/skills. +sidebar: + order: 2 +products: + - agents +--- + +import { TypeScriptExample, WranglerConfig } from "~/components"; + +A source tells an agent where its [skills](/agents/skills/) live. `agents/skills` has three: + +| Source | Reads skills from | Use it when | +| --------------------------- | ------------------------------------------- | -------------------------------------------------------------------------------- | +| [`workspace()`](#workspace) | Files in a `@cloudflare/computer` Workspace | Skills should be files the agent, a user, or your code adds and edits at runtime | +| [`r2()`](#r2) | Objects in an R2 bucket | Many agents share one set of skills managed outside your Worker | +| [`manifest()`](#code) | Skills defined in code | Tests, or a small fixed set of skills that ships with your Worker | + +Pass sources to `loadSkillCatalog()`. A source lists every skill it holds each time the catalog is loaded, so a change in the store shows up the next time you load it. `agents/skills` checks each `SKILL.md` against the [specification](/agents/skills/#validation) when it lists the source. + +## Workspace + +`workspace()` reads skills from files in a Workspace from [`@cloudflare/computer`](https://github.com/cloudflare/computer). A skill is a folder in the Workspace. Writing, editing, or deleting a `SKILL.md` changes the agent's skills the next time the catalog is loaded, with no deploy. + +By default, every `/workspace/.agents/skills//SKILL.md` is a skill, and the files beside it are its resources: + +```text +/workspace/.agents/skills/ + release-notes/ + SKILL.md + references/style-guide.md + scripts/format-release-notes.js + pirate-voice/ + SKILL.md +``` + +Skills are plain files, so anything that writes to the Workspace can add one: the agent's own `write` tool, a user upload, or your code. This Durable Object writes a starter skill when it starts: + + + +```ts +import { DurableObject } from "cloudflare:workers"; +import { Workspace } from "@cloudflare/computer"; +import { Lifecycle } from "agents/lifecycle"; +import { loadSkillCatalog, workspace } from "agents/skills"; + +const ROOT = "/workspace/.agents/skills"; + +const STARTER_SKILLS: Record = { + "pirate-voice/SKILL.md": `--- +name: pirate-voice +description: Answer in a pirate voice. Use when the user asks for a pirate tone. +--- + +Answer accurately, then rewrite the answer in a light pirate voice. +`, +}; + +export class Assistant extends DurableObject { + readonly lifecycle = Lifecycle.install(this); + readonly workspace = new Workspace({ storage: this.ctx.storage }); + + async onStart() { + for (const [path, content] of Object.entries(STARTER_SKILLS)) { + const file = `${ROOT}/${path}`; + await this.workspace.fs.mkdir(file.slice(0, file.lastIndexOf("/")), { + recursive: true, + }); + await this.workspace.fs.writeFile(file, content); + } + } + + skills() { + return loadSkillCatalog([workspace(this.workspace)]); + } +} +``` + + + +To read skills from another directory, pass an absolute `root`: + + + +```ts +import { workspace } from "agents/skills"; + +const source = workspace(this.workspace, { root: "/workspace/skills" }); +``` + + + +If the root directory does not exist, the source lists no skills. `.git`, `node_modules`, and `.DS_Store` inside a skill directory are ignored. + +A Workspace skill has a folder, so the model can run its scripts with the Workspace `exec` tool. Refer to [Execution](/agents/skills/execution/#run-scripts-in-the-workspace). + +## R2 + +`r2()` reads skills from an [R2](/r2/) bucket. The bucket uses the same layout as a skills folder, under a key prefix. Every `/SKILL.md` object is a skill, and the objects beside it are its resources: + +```text +skills/release-notes/SKILL.md +skills/release-notes/references/style-guide.md +skills/release-notes/scripts/format-release-notes.js +skills/pirate-voice/SKILL.md +``` + +Upload skills with any R2 interface. For example, with Wrangler: + +```sh +npx wrangler r2 object put my-agent-skills/skills/pirate-voice/SKILL.md --file ./pirate-voice/SKILL.md --remote +``` + +Bind the bucket to your Worker: + + + +```jsonc +{ + "r2_buckets": [ + { "binding": "SKILLS_BUCKET", "bucket_name": "my-agent-skills" }, + ], +} +``` + + + +Then pass the bucket to `r2()`: + + + +```ts +import { loadSkillCatalog, r2 } from "agents/skills"; + +const catalog = await loadSkillCatalog([ + r2(env.SKILLS_BUCKET, { prefix: "skills/" }), +]); +``` + + + +| Option | Default | Description | +| ------------------- | ------------- | -------------------------------------------------------------------------------- | +| `prefix` | `""` | Key prefix that holds the skill directories | +| `skills` | all skills | Only serve these skill names | +| `refreshIntervalMs` | `60000` | How long to reuse a bucket listing before listing again. `0` lists on every call | +| `id` | `r2:` | Source ID used in diagnostics | + +R2 stores files but cannot run them. To run scripts from R2 skills, pass the experimental [script runner](/agents/skills/execution/#script-runner) to `createSkillTools()`. + +## Code + +`manifest()` creates a source from skills defined in code. It needs no Workspace or bucket. Each entry holds the `SKILL.md` fields and, optionally, its resources: + + + +```ts +import { manifest } from "agents/skills"; + +export const handbook = manifest({ + id: "handbook", + skills: [ + { + name: "release-notes", + description: "Turn a list of changes into release notes.", + body: "Read `references/style-guide.md`, then write one line per change.", + metadata: { author: "my-team" }, + resources: [ + { + path: "references/style-guide.md", + content: "Start each line with a past-tense verb.", + }, + ], + }, + ], +}); +``` + + + +| Field | Required | Description | +| --------------- | -------- | ----------------------------------------------------------------------- | +| `name` | Yes | Skill name. Must follow the [naming rules](/agents/skills/#validation) | +| `description` | Yes | What the skill does and when to use it | +| `body` | Yes | Markdown instructions | +| `license` | No | License name or bundled license file | +| `compatibility` | No | Environment requirements | +| `metadata` | No | String key-value metadata | +| `allowedTools` | No | Space-separated list of pre-approved tools | +| `resources` | No | Files as `{ path, content }`. Use `encoding: "base64"` for binary files | + +Entries pass the same checks as `SKILL.md` files. `fromManifest()` is a deprecated alias of `manifest()`. + +## Combine sources + +Sources apply in order, so put the source you want to win first: + + + +```ts +import { loadSkillCatalog, workspace } from "agents/skills"; +import { handbook } from "./handbook"; + +const catalog = await loadSkillCatalog([workspace(this.workspace), handbook]); +``` + + + +Here, a Workspace skill named `release-notes` replaces the manifest's version, and `catalog.diagnostics` reports the duplicate. + +## Write your own source + +A source is any object that implements `SkillSource`: an `id`, a `list()` method that returns every skill it holds, and a `readResource()` method that reads one file. Both return a result instead of throwing for expected failures, such as a store that cannot be read. Use `parseSkill()` to turn each `SKILL.md` into a skill. diff --git a/src/content/docs/agents/skills/sources/code.mdx b/src/content/docs/agents/skills/sources/code.mdx deleted file mode 100644 index 64a1c59a92d..00000000000 --- a/src/content/docs/agents/skills/sources/code.mdx +++ /dev/null @@ -1,84 +0,0 @@ ---- -title: Code -pcx_content_type: how-to -description: Define Agent Skills in code with skills.manifest(), for tests and small fixed sets of skills. -sidebar: - order: 3 -products: - - agents ---- - -import { TypeScriptExample } from "~/components"; - -`skills.manifest()` creates a source from [Agent Skills](/agents/skills/) defined in code. It needs no Workspace or bucket, which suits tests and small sets of skills that ship with your Worker. - -## Define skills - -Each entry holds the `SKILL.md` fields and, optionally, its resources: - - - -```ts -import { Think, skills } from "@cloudflare/think"; - -const handbook = skills.manifest({ - id: "handbook", - skills: [ - { - name: "release-notes", - description: "Turn a list of changes into release notes.", - body: "Read `references/style-guide.md`, then write one line per change.", - metadata: { author: "my-team" }, - resources: [ - { - path: "references/style-guide.md", - content: "Start each line with a past-tense verb.", - }, - ], - }, - ], -}); - -export class MyAgent extends Think { - getSkills() { - return [handbook]; - } -} -``` - - - -## Entry fields - -| Field | Required | Description | -| --------------- | -------- | ---------------------------------------------------------------- | -| `name` | Yes | Skill name. Must follow the [naming rules](/agents/skills/#validation) | -| `description` | Yes | What the skill does and when to use it | -| `body` | Yes | Markdown instructions | -| `license` | No | License name or bundled license file | -| `compatibility` | No | Environment requirements | -| `metadata` | No | String key-value metadata | -| `allowedTools` | No | Space-separated list of pre-approved tools | -| `resources` | No | Files as `{ path, content }`. Use `encoding: "base64"` for binary files | - -Entries pass the same [checks](/agents/skills/#validation) as `SKILL.md` files: an entry that breaks a rule loads with a warning, and one with no `description` is skipped and logged. - -`skills.fromManifest()` is a deprecated alias of `skills.manifest()`. - -## Combine with other sources - -A manifest works next to other sources. Sources are applied in order, so put the source you want to win first: - - - -```ts -export class MyAgent extends Think { - getSkills() { - return [skills.workspace(this.workspace), handbook]; - } -} -``` - - - -Here, a Workspace skill named `release-notes` replaces the manifest's version. diff --git a/src/content/docs/agents/skills/sources/computer.mdx b/src/content/docs/agents/skills/sources/computer.mdx deleted file mode 100644 index 08818395a47..00000000000 --- a/src/content/docs/agents/skills/sources/computer.mdx +++ /dev/null @@ -1,146 +0,0 @@ ---- -title: Computer -pcx_content_type: how-to -description: Store Agent Skills as files in a Computer Workspace, so skills the agent or a user writes are available on the next turn. -sidebar: - order: 1 -products: - - agents ---- - -import { TypeScriptExample, WranglerConfig } from "~/components"; - -`skills.workspace()` reads [Agent Skills](/agents/skills/) from files in a Workspace from [`@cloudflare/computer`](https://github.com/cloudflare/computer). A skill is a folder in the Workspace. Writing, editing, or deleting a `SKILL.md` changes the agent's skills on its next turn, with no deploy. - -## Read skills from the Workspace - -By default, every `/workspace/.agents/skills//SKILL.md` is a skill. The files beside it are its resources: - -```text -/workspace/.agents/skills/ - release-notes/ - SKILL.md - references/style-guide.md - scripts/format-release-notes.js - pirate-voice/ - SKILL.md -``` - -Return the source from `getSkills()`. In [Think](/agents/harnesses/think/), `this.workspace` is already a Computer Workspace: - - - -```ts -import { Think, skills } from "@cloudflare/think"; - -export class MyAgent extends Think { - getSkills() { - return [skills.workspace(this.workspace)]; - } -} -``` - - - -To read skills from another directory, pass an absolute `root`: - -```ts -skills.workspace(this.workspace, { root: "/workspace/skills" }); -``` - -If the root directory does not exist, the source lists no skills. `.git`, `node_modules`, and `.DS_Store` inside a skill directory are ignored. - -## Add skills to the Workspace - -Skills are plain files, so anything that writes to the Workspace can add one: the agent's own `write` tool, a user upload, or your code. For example, write the starter skills the first time the agent starts: - - - -```ts -import { Think, skills } from "@cloudflare/think"; - -const ROOT = "/workspace/.agents/skills"; - -const STARTER_SKILLS: Record = { - "pirate-voice/SKILL.md": `--- -name: pirate-voice -description: Answer in a pirate voice. Use when the user asks for a pirate tone. ---- - -Answer accurately, then rewrite the answer in a light pirate voice. -`, -}; - -export class MyAgent extends Think { - getSkills() { - return [skills.workspace(this.workspace)]; - } - - async onStart() { - for (const [path, content] of Object.entries(STARTER_SKILLS)) { - const file = `${ROOT}/${path}`; - await this.workspace.fs.mkdir(file.slice(0, file.lastIndexOf("/")), { - recursive: true, - }); - await this.workspace.fs.writeFile(file, content); - } - } -} -``` - - - -Each skill is checked against the [specification](/agents/skills/#validation) when it is listed. A skill whose `name` does not match its folder still loads with a warning. A skill with no `description` is skipped and logged. Use `skills.parseSkill()` to check a skill before you write it: it follows the specification when it parses with no warnings. - -## Run skill scripts - -Skill scripts are Workspace files too. Give the Workspace a backend, and the model runs scripts with the `exec` tool. Activating a skill tells the model where the skill's folder is, so it can run a script from there: - - - -```ts -import { Workspace } from "@cloudflare/computer"; -import { CodemodeBackend } from "@cloudflare/computer/backends/codemode"; -import { Think, skills } from "@cloudflare/think"; - -export class MyAgent extends Think { - workspace = new Workspace({ - storage: this.ctx.storage, - backends: [ - new CodemodeBackend({ - id: "js", - loader: this.env.LOADER, - access: "read-write", - }), - ], - }); - - getSkills() { - return [skills.workspace(this.workspace)]; - } -} -``` - - - - - -```jsonc -{ - "worker_loaders": [{ "binding": "LOADER" }], -} -``` - - - -With the JavaScript backend, a skill can tell the model to run a script by re-exporting it, with `cwd` set to the skill's folder: - -```js -export { default } from "./scripts/format-release-notes.js"; -``` - -Refer to [Execution](/agents/skills/execution/) for the options. - -## Example - -The [`computer-skills` example](https://github.com/cloudflare/agents/tree/main/examples/computer-skills) stores skills in a Workspace, runs their scripts with `exec`, and lets you add and delete skills from the UI. diff --git a/src/content/docs/agents/skills/sources/index.mdx b/src/content/docs/agents/skills/sources/index.mdx deleted file mode 100644 index eb36afe877a..00000000000 --- a/src/content/docs/agents/skills/sources/index.mdx +++ /dev/null @@ -1,17 +0,0 @@ ---- -title: Sources -pcx_content_type: navigation -description: Load Agent Skills from a Computer Workspace, an R2 bucket, or code. -sidebar: - order: 2 - group: - hideIndex: true -products: - - agents ---- - -import { DirectoryListing } from "~/components"; - -A source tells an agent where its [Agent Skills](/agents/skills/) live. - - diff --git a/src/content/docs/agents/skills/sources/r2.mdx b/src/content/docs/agents/skills/sources/r2.mdx deleted file mode 100644 index 03930668d0c..00000000000 --- a/src/content/docs/agents/skills/sources/r2.mdx +++ /dev/null @@ -1,79 +0,0 @@ ---- -title: R2 -pcx_content_type: how-to -description: Load Agent Skills from an R2 bucket that uses the Agent Skills directory layout. -sidebar: - order: 2 -products: - - agents ---- - -import { TypeScriptExample, WranglerConfig } from "~/components"; - -`skills.r2()` reads [Agent Skills](/agents/skills/) from an [R2](/r2/) bucket. Use it to share one set of skills across many agents, or to manage skills outside your Worker's code. - -## Store skills in a bucket - -The bucket uses the same layout as a skills folder, under a key prefix. Every `/SKILL.md` object is a skill, and the objects beside it are its resources: - -```text -skills/release-notes/SKILL.md -skills/release-notes/references/style-guide.md -skills/release-notes/scripts/format-release-notes.js -skills/pirate-voice/SKILL.md -``` - -Upload skills with any R2 interface. For example, with Wrangler: - -```sh -npx wrangler r2 object put my-agent-skills/skills/pirate-voice/SKILL.md --file ./pirate-voice/SKILL.md --remote -``` - -## Read skills from the bucket - -Bind the bucket to your Worker: - - - -```jsonc -{ - "r2_buckets": [{ "binding": "SKILLS_BUCKET", "bucket_name": "my-agent-skills" }], -} -``` - - - -Return the source from `getSkills()`: - - - -```ts -import { Think, skills } from "@cloudflare/think"; - -export class MyAgent extends Think { - getSkills() { - return [skills.r2(this.env.SKILLS_BUCKET, { prefix: "skills/" })]; - } -} -``` - - - -## Options - -| Option | Default | Description | -| ------------------- | ------------------ | ----------------------------------------------------------------------- | -| `prefix` | `""` | Key prefix that holds the skill directories | -| `skills` | all skills | Only serve these skill names | -| `refreshIntervalMs` | `60000` | How long to reuse a bucket listing before listing again. `0` lists on every call | -| `id` | `r2:` | Source id used in diagnostics | - -Each `SKILL.md` is checked against the [specification](/agents/skills/#validation) when the bucket is listed. Skills that break a rule load with a warning, and skills with no `description` are skipped and logged. - -## Run skill scripts - -R2 stores files but cannot run them. To run scripts from R2 skills, set the experimental [script runner](/agents/skills/execution/#script-runner), which adds the `run_skill_script` tool. To store and run skills as files, use a [Computer Workspace](/agents/skills/sources/computer/) instead. - -## Example - -The [`r2-skills` example](https://github.com/cloudflare/agents/tree/main/examples/r2-skills) loads skills from a bucket, runs a script with the script runner, and lets you upload and delete skills from the UI. diff --git a/src/content/docs/agents/tools/skills.mdx b/src/content/docs/agents/tools/skills.mdx index 4f92969d618..38af588860f 100644 --- a/src/content/docs/agents/tools/skills.mdx +++ b/src/content/docs/agents/tools/skills.mdx @@ -1,6 +1,6 @@ --- title: Skills -description: Give your agent Agent Skills tools to discover, load, and read skills on demand. Use Think, Pi Durable, AI SDK, or your own harness. +description: Give your agent skill tools to discover, load, and read skills on demand. Use Think, Pi Durable, AI SDK, or your own harness. pcx_content_type: integration-guide sidebar: order: 5 @@ -10,18 +10,18 @@ products: import { PackageManagers, Steps, TypeScriptExample } from "~/components"; -Give your agent tools to use [Agent Skills](/agents/skills/): on-demand instructions, references, and scripts stored as `SKILL.md` folders. The model sees a short catalog of skill names and descriptions, then loads a skill only when a task matches it. +Give your agent tools to use [skills](/agents/skills/): on-demand instructions, references, and scripts stored as `SKILL.md` folders. The model sees a short catalog of skill names and descriptions, then loads a skill only when a task matches it. -Skills come from [sources](/agents/skills/sources/computer/): a Computer Workspace, an R2 bucket, or code. The tools are the same whichever source you use. +Skills come from [sources](/agents/skills/sources/): a Computer Workspace, an R2 bucket, or code. The tools are the same whichever source you use. Use the supplied adapters with [Think](/agents/harnesses/think/), [Pi Durable](/agents/harnesses/pi/), or the AI SDK. For another harness, use the catalog functions in `agents/skills`. ## What the tools add -| Tool | What it does | -| --------------------- | ------------------------------------------------------------------------------------------------------------- | -| `activate_skill` | Returns a skill's instructions, its folder when it has one, and the list of files bundled with it. | -| `read_skill_resource` | Returns one bundled file, by `{ name, path }` or a qualified path such as `release-notes/references/guide.md`. | +| Tool | What it does | +| --------------------- | ------------------------------------------------------------------------------------------------------------------------- | +| `activate_skill` | Returns a skill's instructions, its folder when it has one, and the list of files bundled with it. | +| `read_skill_resource` | Returns one bundled file, by `{ name, path }` or a qualified path such as `release-notes/references/guide.md`. | | `run_skill_script` | Runs a bundled script. Only added when you set the experimental [script runner](/agents/skills/execution/#script-runner). | The tools only appear when at least one skill is available. `name` is limited to the skills in the catalog. Binary files are returned as base64. @@ -45,7 +45,7 @@ These examples require an existing harness. For a new agent, start with [Think]( -2. Choose a [source](/agents/skills/sources/computer/) for your skills. +2. Choose a [source](/agents/skills/sources/) for your skills. 3. Register the tools with your harness using one of the examples that follow. @@ -53,43 +53,11 @@ These examples require an existing harness. For a new agent, start with [Think]( ### Think -Think adds the tools and the catalog for you. Return your sources from `getSkills()`: - - - -```ts -import { Think, skills } from "@cloudflare/think"; - -export class MyAgent extends Think { - getSkills() { - return [skills.workspace(this.workspace)]; - } -} -``` - - - -Think lists the sources again before each turn, so new and edited skills take effect on the next turn. +Think adds the tools and the catalog for you. Return your sources from `getSkills()`. Refer to [Think skills](/agents/harnesses/think/skills/). ### Pi Durable -In your [Pi harness factory](/agents/harnesses/pi/extensions/#add-skills), install the `skills()` extension. It adds `activate_skill`, `read_skill_resource`, and a `skills` system prompt section: - - - -```ts -import { skills } from "agents/harness/pi"; -import { r2 } from "agents/skills"; - -// Inside the harness factory, before Harness.open(): -this.registry.install( - await skills([r2(this.env.SKILLS_BUCKET, { prefix: "skills/" })]), -); -``` - - - -The extension reads the sources once, when the factory builds it. Both tools are marked `replay: "safe"`. +Install the `skills()` extension from `agents/harness/pi` in your harness factory. It adds `activate_skill`, `read_skill_resource`, and a `skills` system prompt section. Refer to [Pi skills](/agents/harnesses/pi/skills/). ### AI SDK @@ -167,18 +135,18 @@ Define `name` as one of `catalog.skills.map((entry) => entry.skill.name)`. Add ` A broken skill never stops the agent. `loadSkillCatalog()` reports problems in `catalog.diagnostics` and keeps the other skills: -| Diagnostic | Cause | -| ---------------- | ---------------------------------------------------------------------------------------------- | -| `SkillWarning` | A skill breaks a [specification rule](/agents/skills/#validation) but was loaded anyway. | +| Diagnostic | Cause | +| ---------------- | ------------------------------------------------------------------------------------------------------- | +| `SkillWarning` | A skill breaks a [specification rule](/agents/skills/#validation) but was loaded anyway. | | `InvalidSkill` | A skill could not be loaded, for example because it has no `description`. It is not shown to the model. | -| `SourceFailed` | A source could not be listed, for example because the bucket was unavailable. | -| `DuplicateSkill` | Two sources provide the same skill name. The first source wins. | +| `SourceFailed` | A source could not be listed, for example because the bucket was unavailable. | +| `DuplicateSkill` | Two sources provide the same skill name. The first source wins. | Tool failures, such as an unknown skill or a missing file, are returned to the model as tool errors so it can try again. The AI SDK tools throw, and the Pi tools return `isError: true`. ## Related -- [Agent Skills concepts](/agents/concepts/agent-skills/): the open format and progressive disclosure -- [Agent Skills](/agents/skills/): sources and validation in the Agents SDK +- [Skills concepts](/agents/concepts/skills/): the open format and progressive disclosure +- [Skills](/agents/skills/): sources and validation in the Agents SDK - [Execution](/agents/skills/execution/): run skill scripts - [File system](/agents/tools/filesystem/): give the agent a Workspace to store skills in