Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(ai): create-tool skill (#2839)

Eric Hayes964bdfc4e7ca
diff --git a/.claude/skills/create-ai-tool/SKILL.md b/.claude/skills/create-ai-tool/SKILL.md
new file mode 100644
index 0000000000..7e427e95c1
--- /dev/null
+++ b/.claude/skills/create-ai-tool/SKILL.md
@@ -0,0 +1,130 @@
+---
+name: create-ai-tool
+description: Build a new AI tool end-to-end — Rust implementation, toolset wiring, infra, schema generation, and frontend UI.
+---
+
+# Create AI Tool
+
+This skill walks through building a new AI tool from scratch. Before writing any code, read the design guide at `rust/cloud-storage/ai_toolset/TOOL_DESIGN.md` and the framework docs/examples in `rust/cloud-storage/ai_toolset/src/lib.rs`.
+
+**IMPORTANT:** Never modify the `rust/cloud-storage/ai_toolset/` crate. It is the framework — you build tools that use it.
+
+## Step 1: Write the tool
+
+Decide which domain crate the tool belongs in. Tools live at `rust/cloud-storage/<crate>/src/inbound/toolset/`.
+
+Study an existing tool for patterns:
+- `rust/cloud-storage/documents/src/inbound/toolset/` — tools: `read_content.rs`, `read_metadata.rs`, `create_document.rs`
+- `rust/cloud-storage/email/src/inbound/toolset/` — tools: `send_email.rs`, `get_thread.rs`, `update_thread_labels.rs`
+- `rust/cloud-storage/soup/src/inbound/toolset/` — tool: `list_entities.rs`
+- `rust/cloud-storage/call/src/inbound/toolset/` — call-related tools
+
+Each tool is a struct that derives `JsonSchema` and `Deserialize`, with `#[schemars(title = "...", description = "...")]` on the struct and `#[schemars(description = "...")]` on each field. The struct implements `AsyncTool<Context>` from the `ai_toolset` crate.
+
+Create a new file for your tool (e.g. `my_tool.rs`), add it as a `mod` in the toolset's `mod.rs`, and wire it into the toolset's `AsyncToolSet::new().add_tool::<...>()` chain.
+
+## Step 2: Create the tool context
+
+If your tool needs dependencies (DB connections, service clients) that aren't already in an existing context, define a new context struct in the toolset's `mod.rs`. See `rust/cloud-storage/documents/src/inbound/toolset/mod.rs` for the `DocumentToolContext` pattern.
+
+The context must be `Clone` and derivable from the parent `ToolServiceContext` via `FromRef`.
+
+If the tool's dependencies are already available in an existing context (e.g. it only needs `Arc<ToolScribe>`), you can use that context directly — no new struct needed.
+
+## Step 3: Add the toolset to `all_tools` in the ai_tools crate
+
+Edit `rust/cloud-storage/ai_tools/src/lib.rs`:
+- Import your toolset function and context type
+- Add `.add_tool::<YourTool, YourContext>()` or `.add_subtoolset::<YourToolContext>(your_toolset())` to the `all_tools()` function
+
+## Step 4: Add the context to ToolServiceContext
+
+Edit `rust/cloud-storage/ai_tools/src/tool_context.rs`:
+- Add any new type aliases for your service implementations (follow the `Tool*` naming pattern)
+- Add your tool context field to the `ToolServiceContext` struct
+- Implement `FromRef<ToolServiceContext>` for your context if needed (or derive it — the struct uses `#[derive(FromRef)]`)
+
+## Step 5: Wire up env vars and service construction
+
+Edit `rust/cloud-storage/ai_tools/src/build_context.rs`:
+- Add any new env vars to the `env_var!` or `maybe_env_var!` blocks
+- Construct your service/context in `build_tool_service_context_from_env`
+- Add it to the returned `ToolServiceContext`
+
+## Step 6: Update infra (if new env vars or AWS resources are needed)
+
+Edit `infra/packages/shared/src/ai_tools.ts`:
+- Add new env vars to the `envVars` array in `getAiToolsInfra()`
+- Add any new secret ARNs, queue ARNs, or bucket ARNs to the respective arrays
+- Add any new Pulumi stack references needed to resolve the values
+
+## Step 7: Rust checks
+
+Run from `rust/cloud-storage/`:
+```bash
+cargo fmt
+cargo clippy -p ai_tools
+cargo test -p <your_domain_crate>
+```
+
+Fix any warnings or errors before proceeding.
+
+## Step 8: Generate frontend types
+
+Run from `js/app/`:
+```bash
+bun gen-tools
+```
+
+This builds `rust/cloud-storage/ai_tools/src/bin/gen_tool_schemas.rs`, generates `rust/cloud-storage/ai_tools/schemas/tools.json`, and transpiles the schemas into TypeScript at `js/app/packages/service-clients/service-cognition/generated/tools/`.
+
+## Step 9: Check what frontend UI is needed
+
+Run from `js/app/`:
+```bash
+bun check
+```
+
+This runs `tsc --noEmit` and will report type errors — specifically, the `toolHandlers` map in `js/app/packages/core/component/AI/component/tool/handler.tsx` will be missing your new tool name. The errors tell you exactly what to implement.
+
+## Step 10: Read existing tool UI for patterns
+
+The tool UI components live at `js/app/packages/core/component/AI/component/tool/`. Study existing renderers:
+- `Search.tsx` — search results rendering
+- `ReadContent.tsx` / `ReadMetadata.tsx` — document tool UI
+- `SendEmail.tsx` — email tool UI
+- `ListEntities.tsx` — list display
+- `Properties.tsx` — property get/set tools
+- `ListCallRecords.tsx` / `ReadCallRecord.tsx` — call tools
+- `BaseTool.tsx` — shared base component
+
+Each tool needs a handler object implementing `ToolHandler` (from `ToolRenderer.tsx`) with at minimum a `render` component. Use `createToolRenderer` to create it.
+
+## Step 11: Write the tool UI
+
+1. Create a new component file at `js/app/packages/core/component/AI/component/tool/YourTool.tsx`
+2. Export a handler using `createToolRenderer`
+3. Register it in `js/app/packages/core/component/AI/component/tool/handler.tsx`:
+ - Import your handler
+ - Add it to the `toolHandlers` map with the key matching your tool's schema title
+
+Every tool renderer must show results. Use the expandable dropdown pattern from `Search.tsx` / `ListEntities.tsx`:
+
+- Use `BaseTool` (from `BaseTool.tsx`) as the wrapper. It accepts a `response` prop for expandable content.
+- Create `const [isExpanded, setIsExpanded] = createSignal(false)` to track open/closed state.
+- Access the response via `ctx.response?.data` (typed from the generated schema).
+- Show a status summary (e.g. hit count, "No Results") on the right side of the tool row.
+- Render a `CaretRight` toggle button (`@icon/regular/caret-right.svg?component-solid`) that rotates 90deg when expanded.
+- Pass the expanded content into `BaseTool`'s `response` prop, gated on `isExpanded()`.
+
+For tools that return text/string results (not entity lists), render the response string in a `<pre>` or similar block inside the `response` prop. The key point: the response data must always be surfaced in the UI via the dropdown — never silently swallowed.
+
+## Step 12: Frontend checks
+
+Run from `js/app/`:
+```bash
+bun format
+bun check
+```
+
+Fix any remaining type or formatting errors.
diff --git a/rust/cloud-storage/ai_toolset/CLAUDE.md b/rust/cloud-storage/ai_toolset/CLAUDE.md
index feb2a7a36f..fc238d541f 100644
--- a/rust/cloud-storage/ai_toolset/CLAUDE.md
+++ b/rust/cloud-storage/ai_toolset/CLAUDE.md
@@ -1,5 +1,3 @@
You are not allowed to _EVER_ change this crate.
Guidelines for writing toolsets are in TOOL_DESIGN.md
-
-Guidelines for integrating tools are in TOOL_INTEGRATION.md
diff --git a/rust/cloud-storage/ai_toolset/TOOL_INTEGRATION.md b/rust/cloud-storage/ai_toolset/TOOL_INTEGRATION.md
deleted file mode 100644
index eac2f492a3..0000000000
--- a/rust/cloud-storage/ai_toolset/TOOL_INTEGRATION.md
+++ /dev/null
@@ -1,24 +0,0 @@
-A tool depends on dependencies that are injected via context. Dependencies
-are usually things like connections to services, and databases.
-
-If a group of tools share the same dependencies and do similar things they should
-be grouped into a toolset. Toolsets are composable and may
-
-The toolset object is defined so that it can provide _subcontext_ (a small peice of
-a large context) to tools. The example in `lib.rs` demonstrates this by
-narrowing a context with 2 objects to a context with a single object.
-
-UI is created on the frontend for every tool. To support this, toolsets generate
-schemas for all of their tools that the frontend transpiles into ts types. Any tool
-added to the `all_tools` toolset (or nested in a subtoolset) will have types generated.
-
-To write new tools:
-1. Define the context (what does your tool need??)
-2. Define your tool/s and toolset that depends on this context
-3. Add to the parent context of `all_tool`
- - if a field of subtools is the context you need then your context is derivable
-4. Add your toolset as a subtoolset to the `all_tools` toolset in `ai_tools`
-5. Generate schemas by running the binary in `ai_tools` with `cargo run --bin gen_tool_schemas`
-6. Generate the frontend types by running naving to `js/app` and running `bun gen-tools`.
-7. Use `bun check` will fail and tell you what you need to implement.
-8. Implement the handlers / renderers and verify with `bun check`