简介
Watch & Learn
原始描述
Automate browser tasks in Safari — Claude drives Safari step-by-step using the AX tree, recording every action as a replayable workflow. Supports single exec...
通过你的 Agent 添加
安装目录:watch-and-learn/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。
点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。
或者手动安装
包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可;
目录名不用换,也不要把包直接解压到 skills 根目录。
技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。
技能正文
name: watch-and-learn
description: Automate browser tasks in Safari — Claude drives Safari step-by-step using the AX tree, recording every action as a replayable workflow. Supports single execution and batch across multiple items.
Watch & Learn
Claude IS the browser agent. No separate API calls — you read the page, decide the action, execute it, and record every step as a replayable workflow.
How it works
- You capture the Safari page via
capture.py (returns a flattened AX tree)
- You decide the next action based on the AX tree + user's intent
- You execute the action via
navigate.py (click/type/navigate/wait)
- You record the step into a workflow being built in memory
- Repeat until done
- Save the recorded workflow to
~/.irisgo/workflows/<name>.json for replay
capture.py (returns a flattened AX tree)navigate.py (click/type/navigate/wait)~/.irisgo/workflows/<name>.json for replayEvery autonomous run produces a replayable workflow automatically.
Prerequisites
- Python 3.10+ with
pyobjc installed
- macOS Accessibility permission granted for the terminal/IDE running Claude Code
- Safari installed
- No API key needed — Claude Code is the brain
- No IrisGo app needed — everything uses pyobjc and AppleScript directly
Directory layout
.claude/skills/watch-and-learn/
SKILL.md — this file
capture.py — AX tree capture & flattening (read-only, no API calls)
navigate.py — action execution: click, type, navigate, wait (no API calls)
workflow.py — workflow save/load/batch replay (no API calls)
The agent loop (YOU are the agent)
pyobjc installed.claude/skills/watch-and-learn/
SKILL.md — this file
capture.py — AX tree capture & flattening (read-only, no API calls)
navigate.py — action execution: click, type, navigate, wait (no API calls)
workflow.py — workflow save/load/batch replay (no API calls)
The agent loop (YOU are the agent)
When the user asks you to do something in Safari (e.g., "order an Iced Latte from Starbucks"):
Step 0: Open Safari
cd <project_root>/.claude/skills/watch-and-learn
python3 navigate.py open
Step 1: Capture the initial page
cd <project_root>/.claude/skills/watch-and-learn
python3 capture.py
cd <project_root>/.claude/skills/watch-and-learn
python3 navigate.py open
cd <project_root>/.claude/skills/watch-and-learn
python3 capture.py
Returns JSON with the current URL and a flattened AX tree:
{"url": "https://starbucks.com", "flat": "[0] WebArea \"Starbucks\"\n[1] Link \"Menu\" href=https://starbucks.com/menu\n[2] TextField \"Search\"\n..."}
You only need capture.py for the initial page state. After that, every navigate.py command automatically captures and returns the new page state.
Step 2: Decide and execute
Based on the AX tree and the user's intent, decide ONE action and run it. Every command returns JSON with outcome, url, and flat (the updated AX tree):
# By index (preferred for speed — resolves to label+role automatically):
python3 navigate.py click 1 # Click element at index 1
python3 navigate.py type 2 "Iced Latte" # Type into element at index 2
# By label+role (explicit):
python3 navigate.py click "Menu" "Link" # Click the "Menu" link
python3 navigate.py type "Search" "TextField" "Iced Latte"
# Other actions:
python3 navigate.py navigate "https://starbucks.com" # Go to URL
python3 navigate.py wait 2 # Wait 2 seconds
Index vs label+role: Using an index is faster (one arg instead of two) and avoids quoting long labels. Under the hood it resolves to label+role via the AX tree, so workflows always record label+role — never the index.
Example output (index mode includes element_label and element_role):
{"outcome": "clicked", "url": "https://starbucks.com/menu", "flat": "[0] WebArea ...\n[1] Link ...", "element_label": "Menu", "element_role": "Link"}
Read the new flat AX tree from the output, decide your next action, and repeat. No need to call capture.py separately between steps.
Step 4: Record the step
Keep track of every action you take. Build up a list of steps in this format:
{
"index": 0,
"intent": "Navigate to the Starbucks menu page",
"action": {"type": "navigate", "url": "https://www.starbucks.com/menu"},
"element_label": null,
"element_role": null,
"outcome": "navigated"
}
For click/type actions, record these fields from the navigate.py output:
element_label: the AX label (from theelement_labelfield in the JSON output)element_role: the AX role (fromelement_role— e.g. "Button", "Link", "TextField")element_label_alts: alternative labels that could match the same element (e.g. if the button says "Add to Cart" but might say "Add to Order" on a different run). Think about what the label might be in a slightly different state and include 1-2 alternates.element_path: the ancestor role chain if you can infer it (e.g.["WebArea", "List", "ListItem", "Button"]) — helps disambiguate when multiple elements share the same label
Step 5: Loop
Read the AX tree from the previous command's output, decide the next action, execute, record. Continue until the task is done.
Max ~15 steps for a typical task. If you're stuck, try a different approach.
Step 6: Save the workflow
When done, save the recorded steps as a workflow:
python3 workflow.py save 'order-latte-starbucks' 'Order an Iced Latte from Starbucks' '[{"index":0,"intent":"Navigate to Starbucks","action":{"type":"navigate","url":"https://starbucks.com"},"outcome":"navigated"}, ...]'
Tell the user the workflow was saved and can be replayed with /watch-and-learn replay <name>.
Replaying a saved workflow
When the user asks to replay a workflow:
python3 workflow.py list # List saved workflows
python3 workflow.py show <name> # Show workflow steps
python3 workflow.py run <name> # Execute a workflow
python3 workflow.py run <name> --vars '{"item_name": "Cappuccino"}' # With variables
Replay executes steps mechanically. For each click/type step, it:
- Polls for the element by
element_label+element_role(every 250ms, up to 3s) - If not found, tries each label in
element_label_alts - If still not found or ambiguous, tries the
fallbackelement - If all fail → stops and returns
needs_llmwith the failed step info
Replay → Fail → Heal → Resume
When workflow.py run hits a step it can't resolve, it stops and returns:
{
"status": "needs_llm",
"steps_completed": 3,
"total_steps": 7,
"failed_step": {"step": 4, "intent": "Click Add to Cart", "error": "not_found"},
"remaining_steps": [...]
}
As the agent, you then:
- Read the AX tree from the output (or run
python3 capture.py) to see the current page - Find the right element — the label may have changed (e.g. "Add to Cart" → "Add to Order")
- Execute the fix —
python3 navigate.py click <correct_index> - Heal the workflow — patch the step so future replays try both labels:
This adds the new label topython3 workflow.py patch <name> <step_index> "<new_label>" "<new_role>"element_label_altsin the saved workflow. - Resume replay from the next step:
python3 workflow.py run <name> --start-step 5 --no-browser
The workflow gets smarter each time — every fix adds an alternate label, so the same failure won't happen again.
Handling state-changed elements
Websites often change button labels based on state. The most common case is e-commerce "Add to cart" buttons that become quantity controls once the item is already in the cart. For example:
"Add to cart - Great Value Corn, 15 oz"→"Increase quantity Great Value Corn, 15 oz, Current Quantity 1""Add to favorites - Product X"→"Remove from favorites - Product X""Subscribe"→"Unsubscribe"
When you take over from a failed replay step, always check whether the product/entity is still on the page under a different action prefix before trying a different product. Specifically:
- Extract the entity name from the failed label by stripping the action prefix (e.g. "Add to cart - ", "Increase quantity ", etc.)
- Search the AX tree for any element containing that entity name (e.g. grep for "Great Value Corn" instead of the full label)
- Click the state-changed element (e.g. the "Increase quantity" button for the same product)
- Patch the workflow with the new label as an alternate so future replays try both
Common action prefix pairs that swap based on state:
- "Add to cart - " ↔ "Increase quantity " / "Decrease quantity "
- "Add to favorites - " ↔ "Remove from favorites - "
- "Sign in to add to Favorites list, " (logged-out variant)
- "Add to bag - " / "Add to basket - " (other e-commerce sites)
Never assume the product is missing just because the exact button label changed. Always search for the product name first.
Batch execution
python3 workflow.py batch <name> --items '["Iced Latte", "Cappuccino", "Espresso"]'
python3 workflow.py batch <name> --items '["Iced Latte", "Cappuccino", "Espresso"]'
The batch loop:
- Load the workflow
- For each item: substitute
{{variable}}placeholders, navigate to start URL, execute all steps - Report: succeeded/total
Parameterization
When saving a workflow, detect values that should be parameterized:
- Text typed into search/form fields →
{{search_term}},{{item_name}} - Product names clicked →
{{product}} - Email addresses →
{{email}}
Replace these with {{variable}} placeholders and add them to workflow.variables.
Ask the user to confirm which fields should be parameterized before saving.
Interacting with the user
- "Order X from Y" / "Do X on website Y" → Run the agent loop yourself. Record steps. Save workflow.
- "Do X for [list]" → Run agent loop for first item, save workflow, batch the rest.
- "Replay X" / "Run workflow X" →
python3 workflow.py run <name>
- "Batch X for [items]" →
python3 workflow.py batch <name> --items '[...]'
- "Record a workflow" / "Watch me" → Run the agent loop while the user demonstrates, recording each step
- "List workflows" →
python3 workflow.py list
Error handling
- If capture fails (no Safari window), wait 2s and retry once
- If a click/type fails, try the coordinate fallback
- If an element can't be found, re-capture the page and look again
- After 3 failures on the same step, skip it and tell the user
- Never silently swallow errors — always report what happened
python3 workflow.py run <name>python3 workflow.py batch <name> --items '[...]'python3 workflow.py list- If capture fails (no Safari window), wait 2s and retry once
- If a click/type fails, try the coordinate fallback
- If an element can't be found, re-capture the page and look again
- After 3 failures on the same step, skip it and tell the user
- Never silently swallow errors — always report what happened
正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。