Skip to content

Commit c06005b

Browse files
authored
feat(webapp,sdk): in-dashboard AI agent (#4018)
## Summary Adds an in-dashboard AI agent: a chat panel, reachable from any environment page, that answers questions about your runs, errors, tasks, and analytics, diagnoses why a run failed, charts your data, reads your connected repo's source, and answers product and how-to questions. It is gated behind the `hasDashboardAgentAccess` feature flag (global or per-org, default off), so this PR ships disabled: the launcher is hidden unless the flag is enabled. ## Design The agent runs as a standalone `chat.agent` Trigger task in its own internal package, with no access to the webapp database, Prisma, or ClickHouse. It reads the user's data over the public API, acting as the user via a short-lived delegated user-actor token minted server-side each turn (never in the browser), building on [#3997](#3997). The error and analytics tools use [#4005](#4005) and the TRQL query API. The first turn of a new chat streams from a warm webapp route (Head Start) while the durable agent boots in parallel. Structured answers (a run-failure diagnosis card, a live chart) render through a small typed view catalog rather than arbitrary markup. A knowledge lane forwards product and how-to questions to the support assistant. Conversation history lives in a separate Drizzle-backed store on its own Postgres schema, kept as a display read-model so it can never corrupt the agent's model context. The SDK changes add an `apiClient` option to `chat.createStartSessionAction` and `chat.headStart`, and keep the Head Start tool-approval tail intact across a custom `prepareMessages` hook so prompt caching and Head Start compose.
1 parent 2c82d4c commit c06005b

78 files changed

Lines changed: 9709 additions & 1838 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
---
4+
5+
Fix `chat.agent` / `AgentChat` when the agent is deployed to a Trigger.dev preview branch. The realtime message-append and stream-subscribe calls now send the `x-trigger-branch` header (sourced from the same resolver `sessions.start` uses), so messaging a preview-branch chat agent no longer fails with `x-trigger-branch header required for preview env`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
---
4+
5+
Fix Head Start handovers breaking when a `chat.agent` also defines a `prepareMessages` hook. A handover hands the first turn's pending tool call to the agent as a tool-approval round whose trailing tool message must reach the model untouched. A `prepareMessages` hook that rewrites the last message (for example the recommended prompt-caching breakpoint) could disturb it, so the turn failed with "tool_use ids were found without tool_result". The agent now preserves that approval tail across `prepareMessages`, so caching and Head Start compose cleanly.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
---
4+
5+
`chat.headStart` now accepts an `apiClient` option (base URL + access token), so the head-start route can create the session and trigger the agent run against a different project/environment than the warm server's ambient Trigger config. Useful when your `chat.agent` lives in a separate project from the app serving the route. Mirrors the `apiClient` option on `chat.createStartSessionAction`; your LLM provider keys stay in the `run` callback and are unaffected.
6+
7+
```ts
8+
export const POST = chat.headStart({
9+
agentId: "my-agent",
10+
apiClient: { baseURL, accessToken },
11+
run: async ({ chat }) =>
12+
streamText({ ...chat.toStreamTextOptions({ tools }), model: anthropic("claude-sonnet-4-6") }),
13+
});
14+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
---
4+
5+
`chat.createStartSessionAction` now accepts an `apiClient` option, so you can scope a chat session start to a specific environment's API config (`baseURL` / `accessToken`) without setting a global `TRIGGER_SECRET_KEY`. Useful when one server starts chats across more than one environment.
6+
7+
```ts
8+
const startSession = chat.createStartSessionAction("my-chat", {
9+
apiClient: { baseURL, accessToken },
10+
});
11+
12+
await startSession({ chatId, clientData });
13+
```

.claude/skills/drizzle/SKILL.md

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
---
2+
name: drizzle
3+
description: Use this skill when writing or modifying Drizzle ORM schemas, queries, or migrations in this repo — specifically the `@internal/dashboard-agent-db` package (the dashboard agent's conversation datastore). Covers pg-core schema definition, the postgres-js driver, drizzle-kit migrations, and this repo's conventions: a dedicated Postgres schema, foreign-key-free cross-database design, pooler-safe connections, and the access-pattern query layer. Drizzle is NOT the main database — that's Prisma.
4+
allowed-tools: Read, Write, Edit, Glob, Grep, Bash
5+
---
6+
7+
# Drizzle ORM (this repo)
8+
9+
Drizzle is used in exactly one place: **`internal-packages/dashboard-agent-db`** (`@internal/dashboard-agent-db`), the in-dashboard agent's conversation store. Everything else in the monorepo is **Prisma** (`@trigger.dev/database`). Keep them separate.
10+
11+
Pinned versions: **`drizzle-orm` ^0.45**, **`drizzle-kit` ^0.31** (dev), **`postgres` ^3.4** (postgres.js driver). drizzle-orm and drizzle-kit are intentionally on different version lines — 0.31.x is the correct companion for 0.45.x, there is no peer dependency between them.
12+
13+
## Critical rules
14+
15+
1. **Drizzle is only the agent's own datastore.** The agent (and its task bundle) must have **no access to the main Prisma database or ClickHouse**. Never import the Prisma client into the agent task or into `@internal/dashboard-agent-db`. Main data is reached via the API, not Drizzle.
16+
2. **Foreign-key-free.** In cloud this DB is a *separate* PlanetScale database, so it can't FK into the main DB. Reference main entities (`organizationId`, `userId`, …) **by id only — never `.references()`**. Joins happen in app code; tenant scoping is enforced in the query layer.
17+
3. **One dedicated Postgres schema.** All tables live under `pgSchema("trigger_dashboard_agent")` so they're schema-qualified and isolated from Prisma's `public` schema (this is what makes the OSS single-database fallback safe).
18+
4. **Pooler-safe connections.** Connections go through a transaction-mode pooler (PlanetScale / PgBouncer-style), so postgres.js must run with **`prepare: false`** — prepared statements don't survive a connection being handed to another client between checkouts.
19+
5. **Node16 module resolution.** Relative imports need explicit **`.js`** extensions (`import { chats } from "./schema.js"`), even though the source is `.ts`.
20+
6. **Scope every user query.** All queries that touch user data go through `src/queries.ts` and are scoped by `organizationId` / `userId`, so callers can't forget the `where`. Don't write ad-hoc cross-tenant queries elsewhere.
21+
22+
## Package layout
23+
24+
```text
25+
internal-packages/dashboard-agent-db/
26+
drizzle.config.ts # drizzle-kit config (schema path, out dir, schemaFilter)
27+
drizzle/ # generated migrations (committed)
28+
src/
29+
schema.ts # pgSchema + table definitions
30+
client.ts # createDashboardAgentDb() — postgres.js + drizzle
31+
queries.ts # the access-pattern layer (org/user-scoped)
32+
index.ts # barrel: re-exports schema, client, queries
33+
```
34+
35+
`package.json` points `main`/`types` at `./src/index.ts` (consumed as source, no build step) — same as other simple internal packages.
36+
37+
## Schema (pg-core)
38+
39+
Use `pgSchema(...).table(...)`, not the bare `pgTable`, so tables land in the dedicated schema. ([schemas](https://orm.drizzle.team/docs/schemas), [pg column types](https://orm.drizzle.team/docs/column-types/pg), [indexes](https://orm.drizzle.team/docs/indexes-constraints))
40+
41+
```ts
42+
import { sql } from "drizzle-orm";
43+
import { index, jsonb, pgSchema, text, timestamp } from "drizzle-orm/pg-core";
44+
45+
export const dashboardAgentSchema = pgSchema("trigger_dashboard_agent");
46+
47+
export const chats = dashboardAgentSchema.table(
48+
"chats",
49+
{
50+
id: text("id").primaryKey(),
51+
organizationId: text("organization_id").notNull(), // FK-free: id only, no .references()
52+
userId: text("user_id").notNull(),
53+
title: text("title").notNull().default("New chat"),
54+
// JSONB with a typed view; .default([]) / .default({}) emit '[]'::jsonb / '{}'::jsonb
55+
messages: jsonb("messages").$type<unknown[]>().notNull().default([]),
56+
metadata: jsonb("metadata").$type<Record<string, unknown>>().notNull().default({}),
57+
deletedAt: timestamp("deleted_at", { withTimezone: true }), // soft delete
58+
lastMessageAt: timestamp("last_message_at", { withTimezone: true }),
59+
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
60+
},
61+
// Extra config returns an ARRAY in drizzle-orm 0.36+ (not an object).
62+
(t) => [
63+
// Partial + ordered composite index. `.desc()` on the column, `.where(sql`...`)` for partial.
64+
index("chats_org_user_last_msg_idx")
65+
.on(t.organizationId, t.userId, t.lastMessageAt.desc())
66+
.where(sql`${t.deletedAt} is null`),
67+
]
68+
);
69+
70+
// Inferred row types for the query layer + consumers.
71+
export type Chat = typeof chats.$inferSelect;
72+
export type NewChat = typeof chats.$inferInsert;
73+
```
74+
75+
Notes:
76+
- `timestamp(..., { withTimezone: true })``timestamp with time zone`. Use `.defaultNow()` for `DEFAULT now()`.
77+
- For a "newest first, nulls last" sort the partial index uses `.desc()`; the *query* uses raw `sql` for `NULLS LAST` (see below).
78+
- Don't add `.references()` — see critical rule 2.
79+
80+
## Client (postgres.js + drizzle)
81+
82+
([connect overview](https://orm.drizzle.team/docs/connect-overview)) One small pool, `prepare: false`. In the agent task create it once in `onBoot` (per-process); in the webapp wrap it in the `singleton(...)` helper.
83+
84+
```ts
85+
import { drizzle, type PostgresJsDatabase } from "drizzle-orm/postgres-js";
86+
import postgres, { type Sql } from "postgres";
87+
import * as schema from "./schema.js";
88+
89+
export type DashboardAgentDb = PostgresJsDatabase<typeof schema>;
90+
91+
export function createDashboardAgentDb(connectionString: string, opts: { max?: number } = {}) {
92+
const sql: Sql = postgres(connectionString, {
93+
max: opts.max ?? 5, // small — the pooler does the real pooling
94+
idle_timeout: 20, // release conns when an agent run suspends
95+
prepare: false, // REQUIRED for transaction-mode poolers
96+
});
97+
return { db: drizzle(sql, { schema }), sql, close: () => sql.end() };
98+
}
99+
```
100+
101+
## Queries (the access-pattern layer)
102+
103+
([select](https://orm.drizzle.team/docs/select), [insert](https://orm.drizzle.team/docs/insert), [operators](https://orm.drizzle.team/docs/operators), [transactions](https://orm.drizzle.team/docs/transactions), [joins](https://orm.drizzle.team/docs/joins))
104+
105+
```ts
106+
import { and, desc, eq, isNull, sql } from "drizzle-orm";
107+
108+
// Select EXPLICIT columns for list views — never select a large blob (messages)
109+
// or a secret (tokens) you don't need. `NULLS LAST` needs raw sql in orderBy.
110+
await db
111+
.select({ id: chats.id, title: chats.title, lastMessageAt: chats.lastMessageAt })
112+
.from(chats)
113+
.where(and(eq(chats.organizationId, orgId), eq(chats.userId, userId), isNull(chats.deletedAt)))
114+
.orderBy(sql`${chats.pinnedAt} desc nulls last`, desc(chats.lastMessageAt))
115+
.limit(50);
116+
117+
// Idempotent create (avoids a duplicate-key race between two writers).
118+
await db.insert(chats).values({ id, organizationId: orgId, userId }).onConflictDoNothing();
119+
120+
// Upsert.
121+
await db
122+
.insert(chatSessions)
123+
.values({ chatId, publicAccessToken })
124+
.onConflictDoUpdate({ target: chatSessions.chatId, set: { publicAccessToken, updatedAt: sql`now()` } });
125+
126+
// Owner-scope a join (this DB is FK-free, so enforce ownership in the query).
127+
await db
128+
.select({ /* session cols */ })
129+
.from(chatSessions)
130+
.innerJoin(chats, eq(chats.id, chatSessions.chatId))
131+
.where(and(eq(chatSessions.chatId, chatId), eq(chats.userId, userId)));
132+
133+
// Multi-write that must be consistent on the next read → one transaction.
134+
await db.transaction(async (tx) => {
135+
await tx.update(chats).set({ messages, updatedAt: sql`now()` }).where(eq(chats.id, chatId));
136+
await tx.insert(chatSessions).values({ /* ... */ }).onConflictDoUpdate({ /* ... */ });
137+
});
138+
```
139+
140+
Use `sql\`now()\`` for DB-side timestamps in updates.
141+
142+
## Migrations (drizzle-kit)
143+
144+
([kit overview](https://orm.drizzle.team/docs/kit-overview), [generate](https://orm.drizzle.team/docs/drizzle-kit-generate), [migrate](https://orm.drizzle.team/docs/drizzle-kit-migrate))
145+
146+
`drizzle.config.ts` must set **`schemaFilter`** so drizzle-kit only ever manages our schema — never Prisma's `public` (critical in the OSS single-DB fallback):
147+
148+
```ts
149+
import { defineConfig } from "drizzle-kit";
150+
export default defineConfig({
151+
schema: "./src/schema.ts",
152+
out: "./drizzle",
153+
dialect: "postgresql",
154+
schemaFilter: ["trigger_dashboard_agent"],
155+
dbCredentials: { url: process.env.DASHBOARD_AGENT_DATABASE_URL ?? process.env.DATABASE_URL ?? "postgres://placeholder" },
156+
});
157+
```
158+
159+
Workflow:
160+
161+
```bash
162+
cd internal-packages/dashboard-agent-db
163+
pnpm run db:generate # diff schema.ts → emit SQL into drizzle/. OFFLINE (no DB needed).
164+
# review the generated drizzle/000N_*.sql before committing
165+
pnpm run db:migrate # apply pending migrations. Needs a real DATABASE URL.
166+
```
167+
168+
- `db:generate` is **offline** — it only reads `schema.ts`, so you can verify a schema change compiles to valid DDL with no database. Use it as a fast check.
169+
- drizzle-kit names migration files with a **random suffix** (`0000_magenta_lilandra.sql`). Don't regenerate a committed migration just to "refresh" it — that churns the filename. After the first migration is committed, schema changes produce a **new** `000N_*.sql`; commit that.
170+
- Generated DDL for a new schema is one `CREATE SCHEMA` + schema-qualified `CREATE TABLE`s + indexes, **no foreign keys** (by design here).
171+
172+
## Common gotchas
173+
174+
- **`prepare: false`** is not optional with a pooler — without it you'll get prepared-statement errors under load.
175+
- **Missing `.js` extension** on a relative import → TS2835 under Node16 resolution.
176+
- **Extra-config callback returns an array** `(t) => [ ... ]` in drizzle-orm 0.36+. The old object form `(t) => ({ ... })` is deprecated.
177+
- **`NULLS LAST` / `NULLS FIRST`** aren't on the `desc()` helper — use raw `sql\`col desc nulls last\`` in `orderBy`.
178+
- **Don't `SELECT *` into list views** — explicitly pick columns so you never ship a megabyte `messages` blob or a session token to a list query.
179+
- **Adding a dependency**: edit `package.json`, then `pnpm i` from the repo root (never `pnpm add`). Mind the repo's `minimumReleaseAge` (3 days) — pin with a caret range and let pnpm resolve an old-enough version.
180+
181+
## Reference (official docs)
182+
183+
- Schema declaration — https://orm.drizzle.team/docs/sql-schema-declaration
184+
- PostgreSQL column types — https://orm.drizzle.team/docs/column-types/pg
185+
- Schemas (`pgSchema`) — https://orm.drizzle.team/docs/schemas
186+
- Indexes & constraints — https://orm.drizzle.team/docs/indexes-constraints
187+
- Connect (postgres-js) — https://orm.drizzle.team/docs/connect-overview
188+
- Select / Insert / Update / Delete — https://orm.drizzle.team/docs/select · /insert · /update · /delete
189+
- Joins / Operators — https://orm.drizzle.team/docs/joins · /operators
190+
- Transactions — https://orm.drizzle.team/docs/transactions
191+
- drizzle-kit (generate / migrate / push) — https://orm.drizzle.team/docs/kit-overview

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ apps/**/public/build
6767
**/.claude/settings.local.json
6868
.claude/architecture/
6969
.claude/docs-plans/
70+
.claude/plans/
7071
.claude/review-guides/
7172
.claude/scheduled_tasks.lock
7273
.mcp.log
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import type { OutputColumnMetadata } from "@internal/clickhouse";
2+
import type { ChartBlock } from "@internal/dashboard-agent";
3+
import { useEffect, useState } from "react";
4+
import { QueryResultsChart } from "~/components/code/QueryResultsChart";
5+
import type { ChartConfiguration } from "~/components/metrics/QueryWidget";
6+
import { Spinner } from "~/components/primitives/Spinner";
7+
import { useOptionalEnvironment } from "~/hooks/useEnvironment";
8+
import { useOptionalOrganization } from "~/hooks/useOrganizations";
9+
import { useOptionalProject } from "~/hooks/useProject";
10+
11+
// Render an agent "chart" block by running its TRQL query through the dashboard's
12+
// own /resources/metric endpoint (session-authed, returns rows + real column
13+
// metadata) and feeding the result into QueryResultsChart. So the chart is live
14+
// and matches the Query page exactly: the agent only emits the query + chart
15+
// config, never the rows. Runs against the project/env the panel is open in.
16+
17+
type MetricResponse =
18+
| { success: false; error: string }
19+
| {
20+
success: true;
21+
data: {
22+
rows: Record<string, unknown>[];
23+
columns: OutputColumnMetadata[];
24+
timeRange: { from: string; to: string };
25+
};
26+
};
27+
28+
type ChartState =
29+
| { status: "loading" }
30+
| { status: "error"; error: string }
31+
| {
32+
status: "ready";
33+
rows: Record<string, unknown>[];
34+
columns: OutputColumnMetadata[];
35+
timeRange?: { from: string; to: string };
36+
};
37+
38+
export function AgentChart({ block }: { block: ChartBlock }) {
39+
const organization = useOptionalOrganization();
40+
const project = useOptionalProject();
41+
const environment = useOptionalEnvironment();
42+
const [state, setState] = useState<ChartState>({ status: "loading" });
43+
44+
const organizationId = organization?.id;
45+
const projectId = project?.id;
46+
const environmentId = environment?.id;
47+
48+
useEffect(() => {
49+
// The block can render before its `query` has finished streaming in; wait
50+
// for it rather than POST an empty query (which 400s).
51+
if (!block.query) return;
52+
if (!organizationId || !projectId || !environmentId) {
53+
setState({ status: "error", error: "No environment context to run the query." });
54+
return;
55+
}
56+
const controller = new AbortController();
57+
setState({ status: "loading" });
58+
fetch("/resources/metric", {
59+
method: "POST",
60+
headers: { "Content-Type": "application/json" },
61+
body: JSON.stringify({
62+
query: block.query,
63+
organizationId,
64+
projectId,
65+
environmentId,
66+
scope: "environment",
67+
period: block.period ?? null,
68+
from: null,
69+
to: null,
70+
}),
71+
signal: controller.signal,
72+
})
73+
.then(async (res) => (await res.json()) as MetricResponse)
74+
.then((data) => {
75+
if (controller.signal.aborted) return;
76+
if (!data.success) {
77+
setState({ status: "error", error: data.error });
78+
} else {
79+
setState({
80+
status: "ready",
81+
rows: data.data.rows,
82+
columns: data.data.columns,
83+
timeRange: data.data.timeRange,
84+
});
85+
}
86+
})
87+
.catch((err) => {
88+
if (controller.signal.aborted) return;
89+
setState({ status: "error", error: err?.message ?? "The query failed to run." });
90+
});
91+
return () => controller.abort();
92+
}, [block.query, block.period, organizationId, projectId, environmentId]);
93+
94+
const config: ChartConfiguration = {
95+
chartType: block.chartType,
96+
xAxisColumn: block.xAxisColumn,
97+
yAxisColumns: block.yAxisColumns ?? [],
98+
groupByColumn: block.groupByColumn ?? null,
99+
stacked: block.stacked ?? false,
100+
sortByColumn: null,
101+
sortDirection: "desc",
102+
aggregation: block.aggregation ?? "sum",
103+
};
104+
105+
return (
106+
<div className="overflow-hidden rounded-lg border border-charcoal-600 bg-charcoal-850">
107+
{block.title ? (
108+
<div className="border-b border-charcoal-700 bg-charcoal-800 px-3 py-2 text-xs font-medium text-text-dimmed">
109+
{block.title}
110+
</div>
111+
) : null}
112+
<div className="h-64 w-full p-2">
113+
{state.status === "loading" ? (
114+
<div className="flex h-full items-center justify-center gap-2 text-xs text-text-dimmed">
115+
<Spinner className="size-3" />
116+
Running query…
117+
</div>
118+
) : state.status === "error" ? (
119+
<div className="flex h-full items-center justify-center px-3 text-center text-xs text-error">
120+
{state.error}
121+
</div>
122+
) : (
123+
<QueryResultsChart
124+
rows={state.rows}
125+
columns={state.columns}
126+
config={config}
127+
timeRange={state.timeRange}
128+
/>
129+
)}
130+
</div>
131+
</div>
132+
);
133+
}

0 commit comments

Comments
 (0)