Skip to content

Commit fadda6a

Browse files
committed
improvement: task routing optimizations
1 parent 82f541e commit fadda6a

4 files changed

Lines changed: 43 additions & 43 deletions

File tree

apps/sim/app/workspace/[workspaceId]/home/home.tsx

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ import { useChat } from './hooks'
1010
interface HomeProps {
1111
chatId?: string
1212
streamId?: string
13+
initialMessage?: string
1314
}
1415

15-
export function Home({ chatId, streamId }: HomeProps = {}) {
16+
export function Home({ chatId, streamId, initialMessage }: HomeProps = {}) {
1617
const { workspaceId } = useParams<{ workspaceId: string }>()
1718
const router = useRouter()
1819
const [inputValue, setInputValue] = useState('')
1920
const { messages, isSending, currentChatId, sendMessage, stopGeneration, chatBottomRef } =
20-
useChat(workspaceId, chatId, streamId)
21+
useChat(workspaceId, chatId, streamId, initialMessage)
2122

22-
const handleSubmit = useCallback(async () => {
23+
const handleSubmit = useCallback(() => {
2324
const trimmed = inputValue.trim()
2425
if (!trimmed) return
2526
setInputValue('')
@@ -31,26 +32,20 @@ export function Home({ chatId, streamId }: HomeProps = {}) {
3132

3233
const userMessageId = crypto.randomUUID()
3334

34-
try {
35-
const response = await fetch(MOTHERSHIP_CHAT_API_PATH, {
36-
method: 'POST',
37-
headers: { 'Content-Type': 'application/json' },
38-
body: JSON.stringify({
39-
message: trimmed,
40-
workspaceId,
41-
userMessageId,
42-
createNewChat: true,
43-
}),
44-
})
45-
46-
if (!response.ok) throw new Error('Failed to start task')
47-
response.body?.cancel()
48-
router.push(
49-
`/workspace/${workspaceId}/task/new?sid=${userMessageId}&m=${encodeURIComponent(trimmed)}`
50-
)
51-
} catch {
52-
setInputValue(trimmed)
53-
}
35+
fetch(MOTHERSHIP_CHAT_API_PATH, {
36+
method: 'POST',
37+
headers: { 'Content-Type': 'application/json' },
38+
body: JSON.stringify({
39+
message: trimmed,
40+
workspaceId,
41+
userMessageId,
42+
createNewChat: true,
43+
}),
44+
}).catch(() => {})
45+
46+
router.push(
47+
`/workspace/${workspaceId}/task/new?sid=${userMessageId}&m=${encodeURIComponent(trimmed)}`
48+
)
5449
}, [inputValue, chatId, currentChatId, sendMessage, workspaceId, router])
5550

5651
const hasMessages = messages.length > 0

apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ function getPayloadData(payload: SSEPayload): SSEPayloadData | undefined {
7171
export function useChat(
7272
workspaceId: string,
7373
initialChatId?: string,
74-
initialStreamId?: string
74+
initialStreamId?: string,
75+
initialMessage?: string
7576
): UseChatReturn {
7677
const queryClient = useQueryClient()
7778
const [messages, setMessages] = useState<ChatMessage[]>([])
@@ -295,10 +296,9 @@ export function useChat(
295296

296297
const userMessageId = initialStreamId
297298
const assistantId = crypto.randomUUID()
298-
const userMessage = new URLSearchParams(window.location.search).get('m') || ''
299299

300300
setMessages([
301-
{ id: userMessageId, role: 'user', content: userMessage },
301+
{ id: userMessageId, role: 'user', content: initialMessage || '' },
302302
{ id: assistantId, role: 'assistant', content: '', contentBlocks: [] },
303303
])
304304

@@ -327,7 +327,7 @@ export function useChat(
327327
return () => {
328328
abortController.abort()
329329
}
330-
}, [initialStreamId, workspaceId, processSSEStream, finalize])
330+
}, [initialStreamId, initialMessage, workspaceId, processSSEStream, finalize])
331331

332332
const sendMessage = useCallback(
333333
async (message: string) => {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Loader2 } from 'lucide-react'
2+
3+
export default function TaskLoading() {
4+
return (
5+
<div className='flex h-full bg-[#FCFCFC] dark:bg-[var(--surface-2)]'>
6+
<div className='flex h-full min-w-0 flex-1 flex-col'>
7+
<div className='min-h-0 flex-1 overflow-y-auto px-[16px] py-[16px]'>
8+
<div className='mx-auto max-w-[768px] space-y-[16px]'>
9+
<div className='flex items-center gap-[8px] py-[8px] text-[13px] text-[var(--text-tertiary)]'>
10+
<Loader2 className='h-[14px] w-[14px] animate-spin' />
11+
Thinking...
12+
</div>
13+
</div>
14+
</div>
15+
</div>
16+
</div>
17+
)
18+
}

apps/sim/app/workspace/[workspaceId]/task/[taskId]/page.tsx

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { redirect } from 'next/navigation'
2-
import { getSession } from '@/lib/auth'
3-
import { verifyWorkspaceMembership } from '@/app/api/workflows/utils'
41
import { Home } from '@/app/workspace/[workspaceId]/home/home'
52

63
interface TaskPageProps {
@@ -15,21 +12,11 @@ interface TaskPageProps {
1512
}
1613

1714
export default async function TaskPage({ params, searchParams }: TaskPageProps) {
18-
const { workspaceId, taskId } = await params
19-
const session = await getSession()
20-
21-
if (!session?.user?.id) {
22-
redirect('/')
23-
}
24-
25-
const hasPermission = await verifyWorkspaceMembership(session.user.id, workspaceId)
26-
if (!hasPermission) {
27-
redirect('/')
28-
}
15+
const { taskId } = await params
2916

3017
if (taskId === 'new') {
31-
const { sid } = await searchParams
32-
return <Home streamId={sid} />
18+
const { sid, m } = await searchParams
19+
return <Home streamId={sid} initialMessage={m} />
3320
}
3421

3522
return <Home chatId={taskId} />

0 commit comments

Comments
 (0)