Skip to content

Commit 4fb9537

Browse files
committed
fix: diff apply failed and edit app issues
1 parent 0918264 commit 4fb9537

File tree

6 files changed

+29
-11
lines changed

6 files changed

+29
-11
lines changed

react-native/src/app/AppGalleryScreen.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ function AppGalleryScreen(): React.JSX.Element {
363363
navigation.navigate('Bedrock', {
364364
editAppCode: app.htmlCode,
365365
editAppName: app.name,
366+
editTimestamp: Date.now(),
366367
});
367368
}
368369
}

react-native/src/chat/ChatScreen.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ const findLatestHtmlCode = (messages: SwiftChatMessage[]): string => {
102102
return '';
103103
};
104104

105+
/**
106+
* Check if any AI message has diffCode (for detecting failed diff apply case)
107+
*/
108+
const hasAnyDiffCode = (messages: SwiftChatMessage[]): boolean => {
109+
const aiMessages = messages.filter(m => m.user._id === BOT_ID);
110+
return aiMessages.some(msg => msg.diffCode);
111+
};
112+
105113
const createBotMessage = (mode: string, isAppMode: boolean = false) => {
106114
return {
107115
_id: uuid.v4(),
@@ -132,6 +140,7 @@ function ChatScreen(): React.JSX.Element {
132140
const mode = route.params?.mode ?? currentMode;
133141
const editAppCode = route.params?.editAppCode;
134142
const editAppName = route.params?.editAppName;
143+
const editTimestamp = route.params?.editTimestamp;
135144
const modeRef = useRef(mode);
136145
const isNovaSonic =
137146
getTextModel().modelId.includes('sonic') &&
@@ -261,6 +270,7 @@ function ChatScreen(): React.JSX.Element {
261270
setMessages([]);
262271
bedrockMessages.current = [];
263272
clearLatestHtmlCode();
273+
setUsage(undefined);
264274
showKeyboard();
265275
}, [])
266276
);
@@ -354,8 +364,7 @@ function ChatScreen(): React.JSX.Element {
354364
const restoredHtmlCode = findLatestHtmlCode(msg as SwiftChatMessage[]);
355365
setLatestHtmlCode(restoredHtmlCode);
356366

357-
// If session has htmlCode, auto-select App prompt
358-
if (restoredHtmlCode) {
367+
if (restoredHtmlCode || hasAnyDiffCode(msg as SwiftChatMessage[])) {
359368
isAppModeRef.current = true;
360369
sendEventRef.current?.('selectAppPrompt');
361370
} else {
@@ -386,8 +395,9 @@ function ChatScreen(): React.JSX.Element {
386395

387396
// editAppCode handler - for editing saved apps from AppGallery
388397
useEffect(() => {
389-
if (editAppCode) {
398+
if (editAppCode && editTimestamp) {
390399
startNewChat.current();
400+
setUsage(undefined);
391401
setLatestHtmlCode(editAppCode);
392402
isAppModeRef.current = true;
393403
setTimeout(() => {
@@ -401,7 +411,7 @@ function ChatScreen(): React.JSX.Element {
401411
}
402412
}, 100);
403413
}
404-
}, [editAppCode, editAppName, navigation]);
414+
}, [editAppCode, editAppName, editTimestamp]);
405415

406416
// deleteChat listener
407417
useEffect(() => {
@@ -441,7 +451,7 @@ function ChatScreen(): React.JSX.Element {
441451
// diffApplied listener for App mode - update message.htmlCode and save diffCode
442452
// Note: placeholder replacement is done in ChatStatus.Complete handler
443453
useEffect(() => {
444-
if (event?.event === 'diffApplied' && event.params?.htmlCode) {
454+
if (event?.event === 'diffApplied' && event.params?.diffCode) {
445455
const { htmlCode, diffCode } = event.params;
446456
setMessages(prevMessages => {
447457
// update isLastHtml for all messages
@@ -520,7 +530,7 @@ function ChatScreen(): React.JSX.Element {
520530
if (isAppModeRef.current && msg.htmlCode) {
521531
msg.text = replaceHtmlWithPlaceholder(msg.text, msg.htmlCode);
522532
}
523-
if (isAppModeRef.current && msg.diffCode) {
533+
if (isAppModeRef.current && msg.diffCode && msg.htmlCode) {
524534
msg.text = replaceDiffWithPlaceholder(msg.text, msg.diffCode);
525535
}
526536
saveCurrentMessages();

react-native/src/chat/component/markdown/HtmlCodeRenderer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const HtmlCodeRenderer = forwardRef<HtmlCodeRendererRef, HtmlCodeRendererProps>(
8484
undefined
8585
);
8686
const [hasLoadedCode, setHasLoadedCode] = useState(
87-
() => !messageHtmlCode && !isCompleted
87+
() => (!messageHtmlCode && !isCompleted) || Boolean(messageDiffCode)
8888
);
8989
const [webViewLoaded, setWebViewLoaded] = useState(false);
9090
const htmlRendererRef = useRef<HtmlPreviewRendererRef>(null);
@@ -134,6 +134,7 @@ const HtmlCodeRenderer = forwardRef<HtmlCodeRendererRef, HtmlCodeRendererProps>(
134134
setShowPreview(true);
135135
sendEvent('diffApplied', { htmlCode: result, diffCode: text });
136136
} else {
137+
sendEvent('diffApplied', { htmlCode: undefined, diffCode: text });
137138
showInfo('Diff apply failed, please regenerate');
138139
}
139140
}

react-native/src/chat/util/ApplyDiff.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ function findAllMatches(
287287
return positions;
288288
}
289289

290-
291290
/**
292291
* Find block position using three-layer fallback strategy
293292
*/
@@ -358,7 +357,10 @@ function findBlockPosition(
358357
}
359358

360359
// Last resort: removals only
361-
if (removals.length >= 2) {
360+
if (
361+
removals.length >= 2 ||
362+
(removals.length === 1 && removals[0].trim().length >= 5)
363+
) {
362364
pos = trimmedMatch(sourceLines, removals, startFrom);
363365
if (pos !== -1) {
364366
return pos;
@@ -565,7 +567,10 @@ export function applyDiff(
565567
firstMiddleContext,
566568
searchAfter
567569
);
568-
if (middlePositions.length > 0 && middlePositions[0] < searchAfter + 100) {
570+
if (
571+
middlePositions.length > 0 &&
572+
middlePositions[0] < searchAfter + 100
573+
) {
569574
position = changePos;
570575
break;
571576
}

react-native/src/chat/util/DiffUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
export const HTML_CODE_PLACEHOLDER = '[HTML_OUTPUT_OMITTED]';
33

44
// Placeholder for diff code block - diff has been applied, no need to send again
5-
export const DIFF_CODE_PLACEHOLDER = '[DIFF_CHANGES_OMITTED]';
5+
export const DIFF_CODE_PLACEHOLDER = '[PREVIOUS_DIFF_APPLIED]';
66

77
// Global storage for latest HTML code in App mode
88
let latestHtmlCode: string = '';

react-native/src/types/RouteTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type RouteParamList = {
2020
mode?: ChatMode;
2121
editAppCode?: string;
2222
editAppName?: string;
23+
editTimestamp?: number;
2324
};
2425
Settings: NonNullable<unknown>;
2526
TokenUsage: NonNullable<unknown>;

0 commit comments

Comments
 (0)