mirror of
https://github.com/prdlk/dotfiles.git
synced 2026-11-03 03:37:36 +00:00
gitconfig: name/email from chezmoi data, gh credential helper by PATH lookup (portable to darwin). claude: no credentials, history, projects, sessions, or plugin caches. ssh config as private_ (0600). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
64 lines
1.6 KiB
Markdown
64 lines
1.6 KiB
Markdown
# Server-Driven Messages (Trigger Patterns)
|
|
|
|
Fetch https://developers.cloudflare.com/agents/api-reference/trigger-patterns/ for complete documentation.
|
|
|
|
Patterns for server-initiated LLM turns in `AIChatAgent` — from schedules, webhooks, email, or other agents.
|
|
|
|
## `saveMessages` — Trigger an LLM Turn
|
|
|
|
```typescript
|
|
await this.saveMessages((existingMessages) => [
|
|
...existingMessages,
|
|
{ role: "user", content: "Check for new notifications" }
|
|
]);
|
|
```
|
|
|
|
`saveMessages` persists the messages AND triggers `onChatMessage`.
|
|
|
|
## `persistMessages` — Save Without Triggering
|
|
|
|
```typescript
|
|
await this.persistMessages([
|
|
...this.messages,
|
|
{ role: "assistant", content: "System note: checked at " + new Date() }
|
|
]);
|
|
```
|
|
|
|
## `waitUntilStable`
|
|
|
|
**Always call before `saveMessages` from non-chat contexts** (schedules, webhooks, email):
|
|
|
|
```typescript
|
|
async checkNotifications(payload: unknown, schedule: Schedule) {
|
|
await this.waitUntilStable({ timeout: 30_000 });
|
|
await this.saveMessages((msgs) => [
|
|
...msgs,
|
|
{ role: "user", content: "Run scheduled notification check" }
|
|
]);
|
|
}
|
|
```
|
|
|
|
## `onChatResponse`
|
|
|
|
Runs after each LLM turn completes. Use for chaining:
|
|
|
|
```typescript
|
|
async onChatResponse(result: ChatResponseResult) {
|
|
if (result.type === "finish" && needsFollowUp(result)) {
|
|
await this.saveMessages((msgs) => [
|
|
...msgs,
|
|
{ role: "user", content: "Continue with next step" }
|
|
]);
|
|
}
|
|
}
|
|
```
|
|
|
|
## Client Status
|
|
|
|
```tsx
|
|
const { isStreaming, isServerStreaming } = useAgentChat({ agent });
|
|
```
|
|
|
|
- `isStreaming` — true during any streaming (user-initiated or server-initiated)
|
|
- `isServerStreaming` — true only during server-initiated streams
|