Skip to content

Commit 17dff13

Browse files
bzqzhengclaudezanesq
authored
Fix 'Edit In Place' and 'Fork Session' features (#6970)
Signed-off-by: Bright Zheng <bright@squareup.com> Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com> Co-authored-by: Zane Staggs <zane@squareup.com>
1 parent 5b15593 commit 17dff13

File tree

4 files changed

+40
-54
lines changed

4 files changed

+40
-54
lines changed

ui/desktop/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ export function AppInner() {
593593
console.log('[App] Processing initial message from launcher:', initialMessage);
594594
navigate('/pair', {
595595
state: {
596-
initialMessage,
596+
initialMessage: { msg: initialMessage, images: [] },
597597
},
598598
});
599599
setTimeout(() => {

ui/desktop/src/components/BaseChat.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ export default function BaseChat({
279279
navigate(`/pair?${params.toString()}`, {
280280
state: {
281281
disableAnimation: true,
282-
initialMessage: editedMessage,
282+
initialMessage: editedMessage ? { msg: editedMessage, images: [] } : undefined,
283283
},
284284
});
285285
};

ui/desktop/src/hooks/useAutoSubmit.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,12 @@ export function useAutoSubmit({
7676

7777
// Scenario 2: Forked session with edited message
7878
if (shouldStartAgent && initialMessage) {
79-
hasAutoSubmittedRef.current = true;
80-
handleSubmit(initialMessage);
81-
clearInitialMessage();
79+
if (messages.length > 0) {
80+
hasAutoSubmittedRef.current = true;
81+
handleSubmit(initialMessage);
82+
clearInitialMessage();
83+
return;
84+
}
8285
return;
8386
}
8487

ui/desktop/src/hooks/useChatStream.ts

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,6 @@ export function useChatStream({
369369

370370
const isNewSession = sessionId && sessionId.match(/^\d{8}_\d{6}$/);
371371
if (isNewSession) {
372-
console.log(
373-
'useChatStream: Message stream finished for new session, emitting message-stream-finished event'
374-
);
375372
window.dispatchEvent(new CustomEvent(AppEvents.MESSAGE_STREAM_FINISHED));
376373
}
377374

@@ -591,6 +588,7 @@ export function useChatStream({
591588
body: {
592589
session_id: sessionId,
593590
user_message: newMessage,
591+
...(hasExistingMessages && { conversation_so_far: currentState.messages }),
594592
},
595593
throwOnError: true,
596594
signal: abortControllerRef.current.signal,
@@ -751,55 +749,40 @@ export function useChatStream({
751749
});
752750

753751
if (sessionResponse.data?.conversation) {
754-
const updatedMessages = [...sessionResponse.data.conversation];
755-
756-
if (updatedMessages.length > 0) {
757-
const lastMessage = updatedMessages[updatedMessages.length - 1];
758-
if (lastMessage.role === 'user') {
759-
lastMessage.content = [{ type: 'text', text: newContent }];
760-
761-
for (const content of message.content) {
762-
if (content.type === 'image') {
763-
lastMessage.content.push(content);
764-
}
765-
}
766-
767-
dispatch({ type: 'SET_MESSAGES', payload: updatedMessages });
768-
dispatch({ type: 'START_STREAMING' });
769-
abortControllerRef.current = new AbortController();
770-
771-
try {
772-
const placeholderMessage = createUserMessage(newContent);
773-
774-
const { stream } = await reply({
775-
body: {
776-
session_id: targetSessionId,
777-
user_message: placeholderMessage,
778-
},
779-
throwOnError: true,
780-
signal: abortControllerRef.current.signal,
781-
});
782-
783-
await streamFromResponse(
784-
stream,
785-
updatedMessages,
786-
dispatch,
787-
onFinish,
788-
targetSessionId
789-
);
790-
} catch (error) {
791-
if (error instanceof Error && error.name === 'AbortError') {
792-
dispatch({ type: 'SET_CHAT_STATE', payload: ChatState.Idle });
793-
} else {
794-
throw error;
795-
}
796-
}
797-
return;
752+
const truncatedMessages = [...sessionResponse.data.conversation];
753+
const updatedUserMessage = createUserMessage(newContent);
754+
755+
for (const content of message.content) {
756+
if (content.type === 'image') {
757+
updatedUserMessage.content.push(content);
798758
}
799759
}
800760

801-
dispatch({ type: 'SET_MESSAGES', payload: sessionResponse.data.conversation });
802-
await handleSubmit({ msg: newContent, images: [] });
761+
const messagesForUI = [...truncatedMessages, updatedUserMessage];
762+
dispatch({ type: 'SET_MESSAGES', payload: messagesForUI });
763+
dispatch({ type: 'START_STREAMING' });
764+
765+
abortControllerRef.current = new AbortController();
766+
767+
try {
768+
const { stream } = await reply({
769+
body: {
770+
session_id: targetSessionId,
771+
user_message: updatedUserMessage,
772+
conversation_so_far: truncatedMessages,
773+
},
774+
throwOnError: true,
775+
signal: abortControllerRef.current.signal,
776+
});
777+
778+
await streamFromResponse(stream, messagesForUI, dispatch, onFinish, targetSessionId);
779+
} catch (error) {
780+
if (error instanceof Error && error.name === 'AbortError') {
781+
dispatch({ type: 'SET_CHAT_STATE', payload: ChatState.Idle });
782+
} else {
783+
throw error;
784+
}
785+
}
803786
} else {
804787
await handleSubmit({ msg: newContent, images: [] });
805788
}

0 commit comments

Comments
 (0)