-
Notifications
You must be signed in to change notification settings - Fork 2
Add typed event payloads for frontend type safety #723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,7 +39,8 @@ import { | |
| BreadcrumbPage, | ||
| BreadcrumbSeparator, | ||
| } from "@/components/ui/breadcrumb"; | ||
| import type { Automation, AutomationRun, AutomationState, Event } from "@/lib/types"; | ||
| import type { Automation, AutomationRun, AutomationState, Event, AgentContentBlock, AgentParsedMessage, AgentToolInput } from "@/lib/types"; | ||
| import { isEventType } from "@/lib/types"; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // State badge configuration | ||
|
|
@@ -189,9 +190,9 @@ function parseAgentEvents(events: Event[]): ParsedBlock[] { | |
| for (const event of events) { | ||
| // Handle automation lifecycle events | ||
| if (event.type.startsWith("automation:run:")) { | ||
| if (event.type === "automation:run:output") { | ||
| if (isEventType(event, "automation:run:output")) { | ||
| // Streaming output chunk | ||
| const chunk = event.data?.chunk as string | undefined; | ||
| const chunk = event.data.chunk; | ||
| if (chunk) { | ||
| blocks.push({ kind: "output_chunk", content: chunk, timestamp: event.ts }); | ||
| } | ||
|
|
@@ -205,22 +206,23 @@ function parseAgentEvents(events: Event[]): ParsedBlock[] { | |
| } | ||
|
|
||
| // Handle agent errors | ||
| if (event.type === "agent:error") { | ||
| if (isEventType(event, "agent:error")) { | ||
| const d = event.data; | ||
| blocks.push({ | ||
| kind: "error", | ||
| content: typeof event.data?.text === "string" ? event.data.text : JSON.stringify(event.data), | ||
| content: d.text ?? JSON.stringify(event.data), | ||
| timestamp: event.ts, | ||
| }); | ||
| continue; | ||
| } | ||
|
|
||
| // Handle agent messages (same parsing as task-detail) | ||
| const raw = event.data?.text; | ||
| const raw = (event.data as Record<string, unknown>)?.text; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [IMPORTANT] Priority: Code Quality / Type Safety Same issue as in |
||
| if (typeof raw !== "string") continue; | ||
|
|
||
| let msg: Record<string, unknown>; | ||
| let msg: AgentParsedMessage; | ||
| try { | ||
| msg = JSON.parse(raw); | ||
| msg = JSON.parse(raw) as AgentParsedMessage; | ||
| } catch { | ||
| if (raw.trim()) blocks.push({ kind: "text", content: raw }); | ||
| continue; | ||
|
|
@@ -229,33 +231,28 @@ function parseAgentEvents(events: Event[]): ParsedBlock[] { | |
| if (msg.type === "system") continue; | ||
|
|
||
| if (msg.type === "result") { | ||
| const result = msg.result as Record<string, unknown> | undefined; | ||
| if (typeof result?.text === "string" && result.text) { | ||
| blocks.push({ kind: "text", content: result.text }); | ||
| if (msg.result?.text) { | ||
| blocks.push({ kind: "text", content: msg.result.text }); | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| const message = msg.message as Record<string, unknown> | undefined; | ||
| const contentBlocks = (message?.content ?? msg.content) as unknown[] | undefined; | ||
| const contentBlocks: AgentContentBlock[] | undefined = msg.message?.content ?? msg.content; | ||
| if (!Array.isArray(contentBlocks)) continue; | ||
|
|
||
| for (const block of contentBlocks) { | ||
| if (typeof block !== "object" || block === null) continue; | ||
| const b = block as Record<string, unknown>; | ||
|
|
||
| for (const b of contentBlocks) { | ||
| if (b.type === "thinking") continue; | ||
|
|
||
| if (b.type === "text" && typeof b.text === "string") { | ||
| if (b.type === "text") { | ||
| if (b.text.trim()) { | ||
| blocks.push({ kind: "text", content: b.text, timestamp: event.ts }); | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if (b.type === "tool_use") { | ||
| const name = typeof b.name === "string" ? b.name : "tool"; | ||
| const input = (b.input ?? {}) as Record<string, unknown>; | ||
| const name = b.name ?? "tool"; | ||
| const input: AgentToolInput = b.input ?? {}; | ||
| const filePath = input.file_path ?? input.filePath ?? input.path ?? input.pattern; | ||
| const command = input.command; | ||
| const description = input.description; | ||
|
|
@@ -271,7 +268,7 @@ function parseAgentEvents(events: Event[]): ParsedBlock[] { | |
| } | ||
|
|
||
| if (b.type === "tool_result") { | ||
| const content = typeof b.content === "string" ? b.content : ""; | ||
| const content = b.content ?? ""; | ||
| if (!content) continue; | ||
| const lines = content.split("\n"); | ||
| const preview = | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[SUGGESTION] Priority: Code Quality
TypedEvent(T)is defined butEvent = UntypedEventmeans it's never the inferred type of any value — no function signature accepts or returns it, andisEventTypenarrows toEvent & { type: T; data: EventDataMap[T] }rather thanTypedEvent(T). It's currently dead-exported.Two clean options:
TypedEvent(T)— theisEventTypeguard + intersection approach is self-contained.Eventa proper union:export type Event = TypedEvent | UntypedEvent— this would let consumers see the full discriminated union and TypeScript could help exhaustiveness-checkevent.typebranches.