trigger-authoring-chat-agent

작성자: triggerdotdev

@trigger.dev/sdk/ai의 chat.agent를 사용하여 지속형 AI 채팅 에이전트를 작성하고 실행합니다: 턴별 실행 루프, ...chat.toStreamTextOptions()를 반드시 펼쳐야 하는 이유

npx skills add https://github.com/triggerdotdev/trigger.dev --skill trigger-authoring-chat-agent

Authoring a chat.agent

The full, version-pinned reference ships inside your installed @trigger.dev/sdk. Read it before writing code — it always matches the SDK version in this project, so it never drifts:

  • Skill: node_modules/@trigger.dev/sdk/skills/trigger-authoring-chat-agent/SKILL.md — the per-turn run loop, the managed streamText, the two server actions, typed tools/data parts, and the React transport.
  • Docs: the full, version-pinned docs ship bundled at node_modules/@trigger.dev/sdk/docs/ai-chat/; the skill above lists the exact pages it draws from in its sources: frontmatter. Grep for an API, e.g. grep -rl "toStreamTextOptions" node_modules/@trigger.dev/sdk/docs/.

If those paths don't exist, @trigger.dev/sdk isn't installed yet — install it first. In a non-hoisted layout, resolve the package with node -p "require.resolve('@trigger.dev/sdk/package.json')" and read skills/ + docs/ beside it.

Common mistakes

  • CRITICAL: calling the streamText imported from ai.

    // Wrong - compaction / steering / background injection silently no-op
    import { streamText } from "ai";
    run: async ({ messages, signal }) => streamText({ model, messages, abortSignal: signal });
    // Correct - the run argument's streamText carries the managed options
    run: async ({ messages, signal, streamText }) => streamText({ model, messages, abortSignal: signal });
    

    The SDK's one carries the prepareStep behind compaction, mid-turn steering and background injection, the system prompt from chat.prompt() or chat.agent({ system }), the registry-resolved model, and telemetry. The imported one carries none of it, with no error. ...chat.toStreamTextOptions() does the same job by hand, and is what a custom agent has to use, since it has no run argument. A chat.headStart route gets a bound streamText too, and there it also owns messages, stopWhen and abortSignal.

  • Declaring tools only on streamText. Also declare them on chat.agent({ tools }), read them back from run, and pass that set as tools. Otherwise each tool's toModelOutput runs on turn 1 but is dropped when history is re-converted on later turns.

  • Not forwarding signal for stop. Without abortSignal: signal, Stop updates the UI but the model keeps generating server-side.

  • Initializing chat.local in onChatStart. Initialize it in onBoot. onChatStart fires once per chat, so continuation runs skip it and crash with chat.local can only be modified after initialization. onBoot fires on every fresh worker.

  • Minting tokens in the browser. Never expose the environment secret key client-side. Mint via the two server actions; the transport calls them.

  • Clearing lastEventId on chat.endRun(). Keep the cursor for the Session lifetime; clear it only when the Session itself closes. It is sessionId-keyed, so clearing forces a resubscribe from seq_num=0 that can hit the prior turn's stale turn-complete and close the stream empty.

  • Returning the raw error from uiMessageStreamOptions.onError. It leaks internals (keys, stack traces). Return a sanitized string instead.

References

Sibling skills: trigger-chat-agent-advanced (Sessions primitive, custom transports, sub-agents, HITL, fast starts, resilience, testing, upgrades), trigger-authoring-tasks and trigger-realtime-and-frontend (the task + frontend foundations chat builds on).

triggerdotdev의 다른 스킬

trigger-dev-tasks
triggerdotdev
Trigger.dev 백그라운드 작업과 워크플로우를 작성, 설계 또는 최적화할 때 이 스킬을 사용하세요. 여기에는 안정적인 비동기 작업 생성, AI 구현 등이 포함됩니다.
agents
triggerdotdev
Trigger.dev를 활용한 AI 에이전트 패턴 - 오케스트레이션, 병렬화, 라우팅, 평가자-최적화기, 인간-루프 포함. LLM 기반 작업을 구축할 때 사용...
config
triggerdotdev
trigger.config.ts를 사용하여 Trigger.dev 프로젝트를 구성합니다. Prisma, Playwright, FFmpeg, Python용 빌드 확장을 설정하거나 배포를 사용자 지정할 때 사용합니다.
realtime
triggerdotdev
Trigger.dev 작업 실행을 프론트엔드와 백엔드에서 실시간으로 구독합니다. 진행 표시기, 라이브 대시보드, 스트리밍 AI/LLM 응답을 구축할 때 사용하세요.
setup
triggerdotdev
프로젝트에 Trigger.dev를 설정합니다. Trigger.dev를 처음 추가하거나, trigger.config.ts를 생성하거나, trigger 디렉토리를 초기화할 때 사용하세요.
tasks
triggerdotdev
Trigger.dev로 AI 에이전트, 워크플로우 및 내구성 있는 백그라운드 작업을 구축하세요. 작업 생성, 작업 트리거, 재시도 처리, 크론 작업 예약 시 사용하거나...
trigger-agents
triggerdotdev
Trigger.dev를 사용한 AI 에이전트 패턴 - 오케스트레이션, 병렬화, 라우팅, 평가자-최적화기, 인간 개입 루프. LLM 기반 작업을 구축할 때 사용하세요...
trigger-chat-agent-advanced
triggerdotdev
Trigger.dev용 고급 운영형 chat.agent 기능으로, 필요 시 로드됩니다. 원시 Sessions 프리미티브(sessions /…) 작업 시 로드하세요.