Yusuke Wada
Workers Tech Talks in Kyoto #2
::right::

AIエージェントは、AIを活用したコンピュータプログラムで、明示的な指示がなくても自律的にタスクを実行して人間のユーザーを支援することができます。
https://www.cloudflare.com/ja-jp/learning/ai/what-is-agentic-ai/


import { Agent, callable } from 'agents'
export class CounterAgent extends Agent {
initialState = { count: 0 }
@callable()
increment() {
this.setState({ count: this.state.count + 1 })
return this.state.count
}
}
import { useState } from 'react'
import { useAgent } from 'agents/react'
export function CounterWidget() {
const [count, setCount] = useState(0)
const agent = useAgent({
agent: 'CounterAgent',
onStateUpdate: (state) => setCount(state.count)
})
return (
<>
{count}
<button onClick={() => agent.stub.increment()}>+</button>
</>
)
}

::right::

app.get('/', async (c) => {
const prompt = c.req.query('q') ?? 'Explain Cloudflare Workers in one sentence.'
const result = await c.env.AI.run('@cf/meta/llama-3.1-8b-instruct-fast', {
messages: [{ role: 'user', content: prompt }]
})
return c.json(result)
})
import { Agent } from 'agents'
export class Reminder extends Agent<Env, { message?: string }> {
async remind(seconds: number, text: string) {
await this.schedule(seconds, 'notify', { text })
}
// 時間が来たら呼ばれる
async notify({ text }: { text: string }) {
this.setState({ message: text }) // クライアントに自動同期
}
}
await this.schedule(60, 'notify', { text }) // 60秒後
await this.schedule('0 9 * * *', 'notify', { text }) // 毎朝9時(cron)
await this.schedule(new Date('2026-07-08 19:00'), 'notify', { text }) // 特定日時

app.get('/', (c) => {
const url = c.req.query('url') ?? 'https://example.com'
return c.env.BROWSER.quickAction('screenshot', { url })
})
/content - Fetch HTML/screenshot - Capture screenshot/pdf - Render PDF/markdown - Extract Markdown from a webpage/snapshot - Capture multiple page formats/scrape - Scrape HTML elements/json - Capture structured data using AI/links - Retrieve links from a webpage/crawl - Crawl web content