AGENT SKILL

flowtools-add-cli

Transform an existing Flow Tools plugin into a CLI-callable plugin by adding `run()` and `inputSchema`.

License 许可
MIT
最近更新
2026-05-15
Stars
★ 0
Forks
0

01 前言 Front Matter

name
flowtools-add-cli
metadata
{ "authors": "FlowToolsOrg", "version": "0.0.1" }
description
Transform an existing Flow Tools plugin into a CLI-callable plugin by adding `run()` and `inputSchema`.

03 SKILL.md 正文

Add CLI Compatibility Skill

Transform an existing Flow Tools plugin into a CLI-callable plugin by adding run() and inputSchema.

When to Use

  • Plugin only has setup() (UI) but needs CLI invocation
  • Want to enable AI agent integration via command line
  • Need structured input validation for headless execution

Prerequisites

  • Existing plugin with setup() method
  • Understanding of plugin's input requirements
  • Plugin already uses definePlugin() from @flowtools/sdk

Step-by-Step Workflow

1. Analyze Plugin Inputs

Identify what inputs your plugin needs:

  • Form fields in UI
  • Configuration options
  • User inputs that affect behavior

2. Define Input Schema

Create Zod schema for input validation:

import { z } from 'zod'
// or: import { z } from '@flowtools/sdk'

const inputSchema = z.object({
  // Define each input with type, default, and description
  text: z.string().default('').describe('Input text'),
  count: z.number().default(1).describe('Number of items'),
  format: z.enum(['json', 'text']).default('json').describe('Output format'),
})

3. Extract Core Logic

Move shared logic into standalone functions:

// Shared between UI and CLI
function processInput(text: string, count: number): string[] {
  return Array.from({ length: count }, () => text.toUpperCase())
}

4. Implement run() Method

Add run() to plugin definition:

export default definePlugin({
  type: 'app',
  meta: {
    id: 'my-plugin',
    name: 'My Plugin',
    version: '0.1.0',
    permissions: ['storage'],
  },
  inputSchema,
  async run(ctx, input) {
    // Use shared logic
    const results = processInput(input.text, input.count)
    
    // Log for debugging
    ctx.log('info', 'Plugin executed', { input })
    
    // Return structured result
    return result.json({ results })
  },
  setup() {
    // Existing UI implementation
  },
})

5. Update setup() to Use Shared Logic

Modify UI to use the same core functions:

setup() {
  return function Panel() {
    const [text, setText] = useState('')
    const [count, setCount] = useState(1)
    
    const handleRun = () => {
      const results = processInput(text, count)
      // Update UI with results
    }
    
    return (
      <div>
        <input value={text} onChange={e => setText(e.target.value)} />
        <input type="number" value={count} onChange={e => setCount(Number(e.target.value))} />
        <button onClick={handleRun}>Run</button>
      </div>
    )
  }
}

6. Test CLI Execution

Verify the plugin works via CLI:

# List plugins to confirm it appears
bun run packages/cli/src/cli.ts list

# Test with schema flags
bun run packages/cli/src/cli.ts run my-plugin --text "hello" --count 3

# Test with JSON input
bun run packages/cli/src/cli.ts run my-plugin --input '{"text":"hello","count":3}'

# Test text output format
bun run packages/cli/src/cli.ts run my-plugin --text "hello" --format text

Result Helpers

Use result.* for structured output:

import { result } from '@flowtools/sdk/result'

// Plain text
result.text('Hello World')

// JSON data
result.json({ items: ['a', 'b', 'c'] })

// Table format
result.table(
  ['Name', 'Value'],
  [['key1', 'value1'], ['key2', 'value2']]
)

// Open link/file
result.open('https://example.com')

// Multiple results
result.multi([
  result.text('Step 1 complete'),
  result.json({ data: [1, 2, 3] })
])

Validation Checklist

  • [ ] inputSchema defined with Zod
  • [ ] All inputs have .describe() for CLI help text
  • [ ] Default values provided where appropriate
  • [ ] run() returns result.* structured response
  • [ ] Core logic shared between setup() and run()
  • [ ] Plugin appears in bun run packages/cli/src/cli.ts list
  • [ ] CLI execution produces expected output

Common Issues

Plugin Not Listed

Problem: Plugin doesn't appear in flowtools list

Solution: Ensure run() method is implemented and exported

Input Validation Errors

Problem: CLI reports validation failures

Solution: Check Zod schema matches expected input format

Missing Capabilities

Problem: MissingCapabilityError during CLI execution

Solution: Declare required permissions in meta.permissions

Example: Complete Transformation

Before (UI only):

export default definePlugin({
  type: 'app',
  meta: { id: 'greeter', name: 'Greeter', version: '0.1.0', permissions: [] },
  setup() {
    return function Panel() {
      const [name, setName] = useState('')
      return (
        <div>
          <input value={name} onChange={e => setName(e.target.value)} />
          <p>Hello, {name}!</p>
        </div>
      )
    }
  },
})

After (UI + CLI):

import { definePlugin } from '@flowtools/sdk'
import { result } from '@flowtools/sdk/result'
import { z } from 'zod'

const inputSchema = z.object({
  name: z.string().default('World').describe('Name to greet'),
})

export default definePlugin({
  type: 'app',
  meta: { id: 'greeter', name: 'Greeter', version: '0.1.0', permissions: [] },
  inputSchema,
  async run(_ctx, input) {
    return result.text(`Hello, ${input.name}!`)
  },
  setup() {
    return function Panel() {
      const [name, setName] = useState('')
      return (
        <div>
          <input value={name} onChange={e => setName(e.target.value)} />
          <p>Hello, {name}!</p>
        </div>
      )
    }
  },
})

04 同仓库更多 Skills