chore: more defensive
This commit is contained in:
@@ -131,6 +131,11 @@ function isAttachment(part: PartType | undefined) {
|
|||||||
return mime.startsWith("image/") || mime === "application/pdf"
|
return mime.startsWith("image/") || mime === "application/pdf"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function list<T>(value: T[] | undefined | null, fallback: T[]) {
|
||||||
|
if (Array.isArray(value)) return value
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
function AssistantMessageItem(props: {
|
function AssistantMessageItem(props: {
|
||||||
message: AssistantMessage
|
message: AssistantMessage
|
||||||
responsePartId: string | undefined
|
responsePartId: string | undefined
|
||||||
@@ -140,7 +145,7 @@ function AssistantMessageItem(props: {
|
|||||||
}) {
|
}) {
|
||||||
const data = useData()
|
const data = useData()
|
||||||
const emptyParts: PartType[] = []
|
const emptyParts: PartType[] = []
|
||||||
const msgParts = createMemo(() => data.store.part[props.message.id] ?? emptyParts)
|
const msgParts = createMemo(() => list(data.store.part?.[props.message.id], emptyParts))
|
||||||
const lastTextPart = createMemo(() => {
|
const lastTextPart = createMemo(() => {
|
||||||
const parts = msgParts()
|
const parts = msgParts()
|
||||||
for (let i = parts.length - 1; i >= 0; i--) {
|
for (let i = parts.length - 1; i >= 0; i--) {
|
||||||
@@ -206,7 +211,7 @@ export function SessionTurn(
|
|||||||
const emptyQuestionParts: { part: ToolPart; message: AssistantMessage }[] = []
|
const emptyQuestionParts: { part: ToolPart; message: AssistantMessage }[] = []
|
||||||
const idle = { type: "idle" as const }
|
const idle = { type: "idle" as const }
|
||||||
|
|
||||||
const allMessages = createMemo(() => data.store.message[props.sessionID] ?? emptyMessages)
|
const allMessages = createMemo(() => list(data.store.message?.[props.sessionID], emptyMessages))
|
||||||
|
|
||||||
const messageIndex = createMemo(() => {
|
const messageIndex = createMemo(() => {
|
||||||
const messages = allMessages() ?? emptyMessages
|
const messages = allMessages() ?? emptyMessages
|
||||||
@@ -248,7 +253,7 @@ export function SessionTurn(
|
|||||||
const parts = createMemo(() => {
|
const parts = createMemo(() => {
|
||||||
const msg = message()
|
const msg = message()
|
||||||
if (!msg) return emptyParts
|
if (!msg) return emptyParts
|
||||||
return data.store.part[msg.id] ?? emptyParts
|
return list(data.store.part?.[msg.id], emptyParts)
|
||||||
})
|
})
|
||||||
|
|
||||||
const attachmentParts = createMemo(() => {
|
const attachmentParts = createMemo(() => {
|
||||||
@@ -299,7 +304,7 @@ export function SessionTurn(
|
|||||||
const lastTextPart = createMemo(() => {
|
const lastTextPart = createMemo(() => {
|
||||||
const msgs = assistantMessages()
|
const msgs = assistantMessages()
|
||||||
for (let mi = msgs.length - 1; mi >= 0; mi--) {
|
for (let mi = msgs.length - 1; mi >= 0; mi--) {
|
||||||
const msgParts = data.store.part[msgs[mi].id] ?? emptyParts
|
const msgParts = list(data.store.part?.[msgs[mi].id], emptyParts)
|
||||||
for (let pi = msgParts.length - 1; pi >= 0; pi--) {
|
for (let pi = msgParts.length - 1; pi >= 0; pi--) {
|
||||||
const part = msgParts[pi]
|
const part = msgParts[pi]
|
||||||
if (part?.type === "text") return part as TextPart
|
if (part?.type === "text") return part as TextPart
|
||||||
@@ -310,8 +315,7 @@ export function SessionTurn(
|
|||||||
|
|
||||||
const hasSteps = createMemo(() => {
|
const hasSteps = createMemo(() => {
|
||||||
for (const m of assistantMessages()) {
|
for (const m of assistantMessages()) {
|
||||||
const msgParts = data.store.part[m.id]
|
const msgParts = list(data.store.part?.[m.id], emptyParts)
|
||||||
if (!msgParts) continue
|
|
||||||
for (const p of msgParts) {
|
for (const p of msgParts) {
|
||||||
if (p?.type === "tool") return true
|
if (p?.type === "tool") return true
|
||||||
}
|
}
|
||||||
@@ -319,10 +323,10 @@ export function SessionTurn(
|
|||||||
return false
|
return false
|
||||||
})
|
})
|
||||||
|
|
||||||
const permissions = createMemo(() => data.store.permission?.[props.sessionID] ?? emptyPermissions)
|
const permissions = createMemo(() => list(data.store.permission?.[props.sessionID], emptyPermissions))
|
||||||
const nextPermission = createMemo(() => permissions()[0])
|
const nextPermission = createMemo(() => permissions()[0])
|
||||||
|
|
||||||
const questions = createMemo(() => data.store.question?.[props.sessionID] ?? emptyQuestions)
|
const questions = createMemo(() => list(data.store.question?.[props.sessionID], emptyQuestions))
|
||||||
const nextQuestion = createMemo(() => questions()[0])
|
const nextQuestion = createMemo(() => questions()[0])
|
||||||
|
|
||||||
const hidden = createMemo(() => {
|
const hidden = createMemo(() => {
|
||||||
@@ -341,7 +345,7 @@ export function SessionTurn(
|
|||||||
const result: { part: ToolPart; message: AssistantMessage }[] = []
|
const result: { part: ToolPart; message: AssistantMessage }[] = []
|
||||||
|
|
||||||
for (const msg of assistantMessages()) {
|
for (const msg of assistantMessages()) {
|
||||||
const parts = data.store.part[msg.id] ?? emptyParts
|
const parts = list(data.store.part?.[msg.id], emptyParts)
|
||||||
for (const part of parts) {
|
for (const part of parts) {
|
||||||
if (part?.type !== "tool") continue
|
if (part?.type !== "tool") continue
|
||||||
const tool = part as ToolPart
|
const tool = part as ToolPart
|
||||||
@@ -365,7 +369,7 @@ export function SessionTurn(
|
|||||||
const msgs = assistantMessages()
|
const msgs = assistantMessages()
|
||||||
if (msgs.length !== 1) return
|
if (msgs.length !== 1) return
|
||||||
|
|
||||||
const msgParts = data.store.part[msgs[0].id] ?? emptyParts
|
const msgParts = list(data.store.part?.[msgs[0].id], emptyParts)
|
||||||
if (msgParts.length !== 1) return
|
if (msgParts.length !== 1) return
|
||||||
|
|
||||||
const assistantPart = msgParts[0]
|
const assistantPart = msgParts[0]
|
||||||
@@ -380,7 +384,7 @@ export function SessionTurn(
|
|||||||
let currentTask: ToolPart | undefined
|
let currentTask: ToolPart | undefined
|
||||||
|
|
||||||
for (let mi = msgs.length - 1; mi >= 0; mi--) {
|
for (let mi = msgs.length - 1; mi >= 0; mi--) {
|
||||||
const msgParts = data.store.part[msgs[mi].id] ?? emptyParts
|
const msgParts = list(data.store.part?.[msgs[mi].id], emptyParts)
|
||||||
for (let pi = msgParts.length - 1; pi >= 0; pi--) {
|
for (let pi = msgParts.length - 1; pi >= 0; pi--) {
|
||||||
const part = msgParts[pi]
|
const part = msgParts[pi]
|
||||||
if (!part) continue
|
if (!part) continue
|
||||||
@@ -407,12 +411,12 @@ export function SessionTurn(
|
|||||||
: undefined
|
: undefined
|
||||||
|
|
||||||
if (taskSessionId) {
|
if (taskSessionId) {
|
||||||
const taskMessages = data.store.message[taskSessionId] ?? emptyMessages
|
const taskMessages = list(data.store.message?.[taskSessionId], emptyMessages)
|
||||||
for (let mi = taskMessages.length - 1; mi >= 0; mi--) {
|
for (let mi = taskMessages.length - 1; mi >= 0; mi--) {
|
||||||
const msg = taskMessages[mi]
|
const msg = taskMessages[mi]
|
||||||
if (!msg || msg.role !== "assistant") continue
|
if (!msg || msg.role !== "assistant") continue
|
||||||
|
|
||||||
const msgParts = data.store.part[msg.id] ?? emptyParts
|
const msgParts = list(data.store.part?.[msg.id], emptyParts)
|
||||||
for (let pi = msgParts.length - 1; pi >= 0; pi--) {
|
for (let pi = msgParts.length - 1; pi >= 0; pi--) {
|
||||||
const part = msgParts[pi]
|
const part = msgParts[pi]
|
||||||
if (part) return computeStatusFromPart(part, i18n.t)
|
if (part) return computeStatusFromPart(part, i18n.t)
|
||||||
|
|||||||
Reference in New Issue
Block a user