refactor: inline message parts with batched SSE updates (#137)

This commit is contained in:
Chris Scott authored and GitHub committed 2026-02-16 10:47:37 -05:00
1 parent 63561640fd
commit 725122b588
12 files changed
+408 -337

No files matched your search

+32 -20
View File
@@ -1,4 +1,4 @@
import { useMemo } from 'react'
import { useMemo, useRef } from 'react'
import { useMessages } from './useOpenCode'
import type { components } from '@/api/opencode-types'
@@ -6,39 +6,51 @@ type UserMessage = components['schemas']['UserMessage']
const DEFAULT_AGENT = 'plan'
interface SessionAgentResult {
agent: string
model: { providerID: string; modelID: string } | undefined
variant: string | undefined
}
export function useSessionAgent(
opcodeUrl: string | null | undefined,
sessionID: string | undefined,
directory?: string
) {
const { data: messages } = useMessages(opcodeUrl, sessionID, directory)
const prevRef = useRef<SessionAgentResult>({ agent: DEFAULT_AGENT, model: undefined, variant: undefined })
return useMemo(() => {
if (!messages || messages.length === 0) {
return {
agent: DEFAULT_AGENT,
model: undefined as { providerID: string; modelID: string } | undefined,
variant: undefined as string | undefined,
}
}
let agent = DEFAULT_AGENT
let model: { providerID: string; modelID: string } | undefined
let variant: string | undefined
for (let i = messages.length - 1; i >= 0; i--) {
const msg = messages[i]
if (msg.role === 'user') {
const userInfo = msg as UserMessage
return {
agent: userInfo.agent || DEFAULT_AGENT,
model: userInfo.model,
variant: userInfo.variant,
if (messages && messages.length > 0) {
for (let i = messages.length - 1; i >= 0; i--) {
const msgWithParts = messages[i]
if (msgWithParts.info.role === 'user') {
const userInfo = msgWithParts.info as UserMessage
agent = userInfo.agent || DEFAULT_AGENT
model = userInfo.model
variant = userInfo.variant
break
}
}
}
return {
agent: DEFAULT_AGENT,
model: undefined,
variant: undefined,
const prev = prevRef.current
if (
prev.agent === agent &&
prev.variant === variant &&
prev.model?.providerID === model?.providerID &&
prev.model?.modelID === model?.modelID
) {
return prev
}
const next: SessionAgentResult = { agent, model, variant }
prevRef.current = next
return next
}, [messages])
}