Normalize chat workflow selections to reset invalid or outdated explicit choices to 'direct'
This commit is contained in:
@@ -3,9 +3,17 @@ import { CHAT_WORKFLOW_MAP_KEY } from './appConfig'
|
|||||||
|
|
||||||
const DIRECT_SELECTION = Object.freeze({ mode: 'direct', workflowId: null })
|
const DIRECT_SELECTION = Object.freeze({ mode: 'direct', workflowId: null })
|
||||||
|
|
||||||
|
function normalizeSelection(selection, validIds = null) {
|
||||||
|
if (selection?.mode === 'explicit' && (!validIds || validIds.has(selection.workflowId))) {
|
||||||
|
return selection
|
||||||
|
}
|
||||||
|
return DIRECT_SELECTION
|
||||||
|
}
|
||||||
|
|
||||||
function loadMap() {
|
function loadMap() {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(localStorage.getItem(CHAT_WORKFLOW_MAP_KEY) || '{}')
|
const raw = JSON.parse(localStorage.getItem(CHAT_WORKFLOW_MAP_KEY) || '{}')
|
||||||
|
return Object.fromEntries(Object.entries(raw).map(([sessionId, selection]) => [sessionId, normalizeSelection(selection)]))
|
||||||
} catch {
|
} catch {
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
@@ -26,7 +34,8 @@ export function useChatWorkflowSelection({ activeSessionId, workflows }) {
|
|||||||
let changed = false
|
let changed = false
|
||||||
const next = { ...previous }
|
const next = { ...previous }
|
||||||
Object.entries(next).forEach(([sessionId, selection]) => {
|
Object.entries(next).forEach(([sessionId, selection]) => {
|
||||||
if (selection?.mode === 'explicit' && !validIds.has(selection.workflowId)) {
|
const normalized = normalizeSelection(selection, validIds)
|
||||||
|
if (normalized !== selection) {
|
||||||
next[sessionId] = DIRECT_SELECTION
|
next[sessionId] = DIRECT_SELECTION
|
||||||
changed = true
|
changed = true
|
||||||
}
|
}
|
||||||
@@ -36,7 +45,7 @@ export function useChatWorkflowSelection({ activeSessionId, workflows }) {
|
|||||||
}, [workflows])
|
}, [workflows])
|
||||||
|
|
||||||
const selection = activeSessionId
|
const selection = activeSessionId
|
||||||
? (selectionBySession[activeSessionId] || DIRECT_SELECTION)
|
? normalizeSelection(selectionBySession[activeSessionId])
|
||||||
: DIRECT_SELECTION
|
: DIRECT_SELECTION
|
||||||
|
|
||||||
const selectedWorkflow = useMemo(() => {
|
const selectedWorkflow = useMemo(() => {
|
||||||
@@ -46,14 +55,14 @@ export function useChatWorkflowSelection({ activeSessionId, workflows }) {
|
|||||||
}, [selection, workflows])
|
}, [selection, workflows])
|
||||||
|
|
||||||
function getSelectionForSession(sessionId) {
|
function getSelectionForSession(sessionId) {
|
||||||
return selectionBySession[sessionId] || DIRECT_SELECTION
|
return normalizeSelection(selectionBySession[sessionId])
|
||||||
}
|
}
|
||||||
|
|
||||||
function setSelectionForSession(sessionId, nextSelection) {
|
function setSelectionForSession(sessionId, nextSelection) {
|
||||||
if (!sessionId) return
|
if (!sessionId) return
|
||||||
setSelectionBySession(previous => ({
|
setSelectionBySession(previous => ({
|
||||||
...previous,
|
...previous,
|
||||||
[sessionId]: nextSelection?.mode ? nextSelection : DIRECT_SELECTION,
|
[sessionId]: normalizeSelection(nextSelection),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user