mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-06 03:00:16 -04:00
improvement: task routing optimizations
This commit is contained in:
@@ -10,16 +10,17 @@ import { useChat } from './hooks'
|
||||
interface HomeProps {
|
||||
chatId?: string
|
||||
streamId?: string
|
||||
initialMessage?: string
|
||||
}
|
||||
|
||||
export function Home({ chatId, streamId }: HomeProps = {}) {
|
||||
export function Home({ chatId, streamId, initialMessage }: HomeProps = {}) {
|
||||
const { workspaceId } = useParams<{ workspaceId: string }>()
|
||||
const router = useRouter()
|
||||
const [inputValue, setInputValue] = useState('')
|
||||
const { messages, isSending, currentChatId, sendMessage, stopGeneration, chatBottomRef } =
|
||||
useChat(workspaceId, chatId, streamId)
|
||||
useChat(workspaceId, chatId, streamId, initialMessage)
|
||||
|
||||
const handleSubmit = useCallback(async () => {
|
||||
const handleSubmit = useCallback(() => {
|
||||
const trimmed = inputValue.trim()
|
||||
if (!trimmed) return
|
||||
setInputValue('')
|
||||
@@ -31,26 +32,20 @@ export function Home({ chatId, streamId }: HomeProps = {}) {
|
||||
|
||||
const userMessageId = crypto.randomUUID()
|
||||
|
||||
try {
|
||||
const response = await fetch(MOTHERSHIP_CHAT_API_PATH, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
message: trimmed,
|
||||
workspaceId,
|
||||
userMessageId,
|
||||
createNewChat: true,
|
||||
}),
|
||||
})
|
||||
fetch(MOTHERSHIP_CHAT_API_PATH, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
message: trimmed,
|
||||
workspaceId,
|
||||
userMessageId,
|
||||
createNewChat: true,
|
||||
}),
|
||||
}).catch(() => {})
|
||||
|
||||
if (!response.ok) throw new Error('Failed to start task')
|
||||
response.body?.cancel()
|
||||
router.push(
|
||||
`/workspace/${workspaceId}/task/new?sid=${userMessageId}&m=${encodeURIComponent(trimmed)}`
|
||||
)
|
||||
} catch {
|
||||
setInputValue(trimmed)
|
||||
}
|
||||
router.push(
|
||||
`/workspace/${workspaceId}/task/new?sid=${userMessageId}&m=${encodeURIComponent(trimmed)}`
|
||||
)
|
||||
}, [inputValue, chatId, currentChatId, sendMessage, workspaceId, router])
|
||||
|
||||
const hasMessages = messages.length > 0
|
||||
|
||||
@@ -71,7 +71,8 @@ function getPayloadData(payload: SSEPayload): SSEPayloadData | undefined {
|
||||
export function useChat(
|
||||
workspaceId: string,
|
||||
initialChatId?: string,
|
||||
initialStreamId?: string
|
||||
initialStreamId?: string,
|
||||
initialMessage?: string
|
||||
): UseChatReturn {
|
||||
const queryClient = useQueryClient()
|
||||
const [messages, setMessages] = useState<ChatMessage[]>([])
|
||||
@@ -295,10 +296,9 @@ export function useChat(
|
||||
|
||||
const userMessageId = initialStreamId
|
||||
const assistantId = crypto.randomUUID()
|
||||
const userMessage = new URLSearchParams(window.location.search).get('m') || ''
|
||||
|
||||
setMessages([
|
||||
{ id: userMessageId, role: 'user', content: userMessage },
|
||||
{ id: userMessageId, role: 'user', content: initialMessage || '' },
|
||||
{ id: assistantId, role: 'assistant', content: '', contentBlocks: [] },
|
||||
])
|
||||
|
||||
@@ -327,7 +327,7 @@ export function useChat(
|
||||
return () => {
|
||||
abortController.abort()
|
||||
}
|
||||
}, [initialStreamId, workspaceId, processSSEStream, finalize])
|
||||
}, [initialStreamId, initialMessage, workspaceId, processSSEStream, finalize])
|
||||
|
||||
const sendMessage = useCallback(
|
||||
async (message: string) => {
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Loader2 } from 'lucide-react'
|
||||
|
||||
export default function TaskLoading() {
|
||||
return (
|
||||
<div className='flex h-full bg-[#FCFCFC] dark:bg-[var(--surface-2)]'>
|
||||
<div className='flex h-full min-w-0 flex-1 flex-col'>
|
||||
<div className='min-h-0 flex-1 overflow-y-auto px-[16px] py-[16px]'>
|
||||
<div className='mx-auto max-w-[768px] space-y-[16px]'>
|
||||
<div className='flex items-center gap-[8px] py-[8px] text-[13px] text-[var(--text-tertiary)]'>
|
||||
<Loader2 className='h-[14px] w-[14px] animate-spin' />
|
||||
Thinking...
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,6 +1,3 @@
|
||||
import { redirect } from 'next/navigation'
|
||||
import { getSession } from '@/lib/auth'
|
||||
import { verifyWorkspaceMembership } from '@/app/api/workflows/utils'
|
||||
import { Home } from '@/app/workspace/[workspaceId]/home/home'
|
||||
|
||||
interface TaskPageProps {
|
||||
@@ -15,21 +12,11 @@ interface TaskPageProps {
|
||||
}
|
||||
|
||||
export default async function TaskPage({ params, searchParams }: TaskPageProps) {
|
||||
const { workspaceId, taskId } = await params
|
||||
const session = await getSession()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
redirect('/')
|
||||
}
|
||||
|
||||
const hasPermission = await verifyWorkspaceMembership(session.user.id, workspaceId)
|
||||
if (!hasPermission) {
|
||||
redirect('/')
|
||||
}
|
||||
const { taskId } = await params
|
||||
|
||||
if (taskId === 'new') {
|
||||
const { sid } = await searchParams
|
||||
return <Home streamId={sid} />
|
||||
const { sid, m } = await searchParams
|
||||
return <Home streamId={sid} initialMessage={m} />
|
||||
}
|
||||
|
||||
return <Home chatId={taskId} />
|
||||
|
||||
Reference in New Issue
Block a user