Skip to content

Commit fd8a32a

Browse files
authored
Merge pull request #222 from opentiny/fix/json-patch-ids-apply-failed
fix(playground): harden jsonPatch apply and component id assignment
2 parents f87da70 + e821516 commit fd8a32a

13 files changed

Lines changed: 177 additions & 122 deletions
Lines changed: 9 additions & 0 deletions
Loading

sites/playground/web/src/components/genui-template/GenuiTemplateChat.vue

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ import type {
2626
} from './chat.types';
2727
import {
2828
finalizePendingSchemaCard,
29+
findLatestPendingSchemaCard,
30+
findSchemaCardByCardId,
31+
generateIdForComponents,
2932
getLastUserMessage,
3033
isManualSchemaSaveMessage,
34+
resolveJsonPatchApplyFailed,
35+
setJsonPatchApplyResult,
3136
} from './template-chat-utils';
3237
import { generateId } from '../../utils';
3338
import { useTemplateContext } from './composables';
@@ -48,7 +53,6 @@ const { setColorMode } = useTheme();
4853
const prevSchema = ref<string>('');
4954
const { schema, conversation, versionControl, stream, emitter } = useTemplateContext();
5055
const {
51-
errorMessagesMap,
5256
handleSchemaJsonChanged,
5357
resetLastPreviewSchema,
5458
} = stream;
@@ -195,7 +199,6 @@ const createSchemaMessageRenderer = (type: 'json-patch' | 'schema-card' | 'schem
195199
itemProps: props,
196200
type,
197201
prevSchema: prevSchema.value,
198-
errorMessagesMap: errorMessagesMap.value,
199202
});
200203
201204
const messageRenderers = {
@@ -292,21 +295,33 @@ const handleSendMessage = async () => {
292295
scrollToBottom();
293296
};
294297
295-
const finalizeStreamingSchemaCard = () => {
298+
const handleNotification = (event: INotificationPayload) => {
299+
if (event.type !== 'done') {
300+
return;
301+
}
302+
303+
const cardId =
304+
schema.currentCardId
305+
|| findLatestPendingSchemaCard(messages.value)?.cardId
306+
|| '';
307+
const card = cardId ? findSchemaCardByCardId(messages.value, cardId) : null;
308+
let applyFailed = false;
309+
if (card?.type === 'json-patch') {
310+
applyFailed = resolveJsonPatchApplyFailed(card, messages.value);
311+
setJsonPatchApplyResult(applyFailed ? 'failed' : 'success', messages.value, cardId);
312+
}
313+
const preview = schema.currentPreviewSchema;
314+
if (preview && !applyFailed) {
315+
generateIdForComponents(preview);
316+
}
317+
schema.setCurrentSchema(preview);
296318
finalizePendingSchemaCard(messages.value, {
297-
cardId: schema.currentCardId,
298-
schema: schema.currentPreviewSchema ?? schema.currentSchema,
319+
cardId: cardId || undefined,
320+
...(applyFailed || !preview ? {} : { schema: preview }),
299321
prevSchema: prevSchema.value || '',
300322
});
301323
};
302324
303-
const handleNotification = (event: INotificationPayload) => {
304-
if (event.type === 'done') {
305-
schema.setCurrentSchema(schema.currentPreviewSchema);
306-
finalizeStreamingSchemaCard();
307-
}
308-
};
309-
310325
watch(() => messages.value, throttledScrollToBottom, { deep: true });
311326
312327
onMounted(() => {

sites/playground/web/src/components/genui-template/SchemaVersionCard.vue

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface IRendererProps {
2828
generatedTime: string;
2929
schema: string;
3030
prevSchema: string;
31-
errorMessagesMap?: Map<string, string>;
31+
applyFailed?: boolean;
3232
}
3333
3434
defineOptions({ inheritAttrs: false });
@@ -66,7 +66,9 @@ const visible = ref(false);
6666
const currentSchema = ref<string>('');
6767
const jsonPatch = ref<string>('');
6868
const prevSchema = ref<string>('');
69-
const errorMessage = computed(() => props.errorMessagesMap?.get(props.cardId) ?? '');
69+
const errorMessage = computed(() =>
70+
props.applyFailed ? 'jsonPatch apply failed' : ''
71+
);
7072
7173
const handleClick = () => {
7274
emit('card-select', props.cardId);
@@ -85,7 +87,12 @@ const handleDev = () => {
8587
return;
8688
}
8789
88-
jsonPatch.value = JSON.stringify(formatJsonPatch(baseline, operations), null, 2);
90+
try {
91+
jsonPatch.value = JSON.stringify(formatJsonPatch(baseline, operations), null, 2);
92+
} catch (error) {
93+
console.error(error);
94+
jsonPatch.value = JSON.stringify(operations, null, 2);
95+
}
8996
prevSchema.value = prevSchemaStr;
9097
currentSchema.value = cardMessage.schema ?? '';
9198
visible.value = true;

sites/playground/web/src/components/genui-template/SchemaVersionHistoryPanel.vue

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { TinyButton } from '@opentiny/vue';
44
import { iconClose } from '@opentiny/vue-icon';
55
import type { PlaygroundColorTheme } from './composables/use-monaco-playground-theme';
66
import { useTemplateContext } from './composables';
7+
import chevronDownIcon from '../../assets/images/chevron-down.svg';
78
import { t } from '../../i18n';
89
910
const props = defineProps<{
@@ -68,23 +69,15 @@ const toggleGroup = (label: string) => {
6869
:aria-expanded="!isGroupCollapsed(group.label)"
6970
@click="toggleGroup(group.label)"
7071
>
71-
<svg
72+
<img
7273
class="schema-version-history-panel__section-chevron"
7374
:class="{ 'is-collapsed': isGroupCollapsed(group.label) }"
75+
:src="chevronDownIcon"
76+
alt=""
7477
width="10"
7578
height="10"
76-
viewBox="0 0 10 10"
7779
aria-hidden="true"
78-
>
79-
<path
80-
d="M2 3.5L5 6.5L8 3.5"
81-
fill="none"
82-
stroke="currentColor"
83-
stroke-width="1.2"
84-
stroke-linecap="round"
85-
stroke-linejoin="round"
86-
/>
87-
</svg>
80+
/>
8881
<span class="schema-version-history-panel__section-label">{{ group.label }}</span>
8982
</button>
9083
<div
@@ -96,7 +89,7 @@ const toggleGroup = (label: string) => {
9689
:key="entry.cardId"
9790
type="button"
9891
class="schema-version-history-panel__item"
99-
:class="{ 'is-active': entry.isCurrent, 'is-pending': entry.isPending }"
92+
:class="{ 'is-pending': entry.isPending }"
10093
@click="actions.handleHistoryEntrySelect(entry)"
10194
>
10295
<div class="schema-version-history-panel__item-main">
@@ -146,10 +139,6 @@ const toggleGroup = (label: string) => {
146139
}
147140
}
148141
149-
.schema-version-history-panel__section-chevron {
150-
color: #8c8c8c;
151-
}
152-
153142
.schema-version-history-panel__item {
154143
color: #f0f0f0;
155144
@@ -237,7 +226,6 @@ const toggleGroup = (label: string) => {
237226
&__section-chevron {
238227
flex-shrink: 0;
239228
display: block;
240-
color: #8c8c8c;
241229
transition: transform 0.15s ease;
242230
243231
&.is-collapsed {

sites/playground/web/src/components/genui-template/TemplateSchemaMessageRenderer.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const props = defineProps<{
1010
itemProps: any;
1111
type: 'json-patch' | 'schema-card' | 'schema-manual';
1212
prevSchema: string;
13-
errorMessagesMap: Map<string, string>;
1413
}>();
1514
1615
const { isMobile } = useIsMobile();
@@ -49,7 +48,6 @@ const handleSchemaVersionCardClick = (cardId: string) => {
4948
v-else
5049
:key="itemProps?.cardId"
5150
:type="type"
52-
:error-messages-map="errorMessagesMap"
5351
v-bind="schemaVersionCardProps"
5452
@card-select="handleSchemaVersionCardClick"
5553
/>

sites/playground/web/src/components/genui-template/chat.types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ export interface ISchemaCardMessageItem {
1313
cardId: string;
1414
generatedTime: string;
1515
schema: string;
16-
id?: string;
17-
state?: Record<string, any>;
1816
prevSchema: string;
1917
}
2018

@@ -26,6 +24,7 @@ export interface IJsonPatchMessageItem {
2624
generatedTime: string;
2725
schema: string;
2826
prevSchema: string;
27+
applyFailed?: boolean;
2928
}
3029

3130
export type SchemaManualInputType = 'manual_edit_save' | 'user';

sites/playground/web/src/components/genui-template/composables/use-template-schema.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useTemplateConversation } from './use-template-conversation';
44
import {
55
findLatestSchemaInConversation,
66
resolveRenderableSchemaFromMessages,
7+
backfillJsonPatchApplyFailedFlags,
78
} from '../template-chat-utils';
89

910
const currentSchema = shallowRef<any>(null);
@@ -12,7 +13,7 @@ const currentPreviewSchemaComplete = ref(true);
1213
const currentCardId = ref<string>('');
1314

1415
export function useTemplateSchema() {
15-
const { setTemplateSchema } = useTemplateConversation();
16+
const { setTemplateSchema, saveConversations } = useTemplateConversation();
1617

1718
function setCurrentPreviewSchema(schema: any, isComplete: boolean = true) {
1819
currentPreviewSchema.value = schema;
@@ -39,6 +40,9 @@ export function useTemplateSchema() {
3940
options: { clearIfMissing?: boolean } = {},
4041
) {
4142
const { clearIfMissing = true } = options;
43+
if (backfillJsonPatchApplyFailedFlags(messages)) {
44+
saveConversations();
45+
}
4246
const latestSchemaInfo = findLatestSchemaInConversation(messages);
4347

4448
if (latestSchemaInfo) {

sites/playground/web/src/components/genui-template/composables/use-template-stream-render.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ import {
44
validateJsonPatch,
55
PARSE_PARTIAL_JSON_STATE,
66
applyJsonPatchOperations,
7-
generateIdForComponents,
7+
setJsonPatchApplyResult,
88
} from '../template-chat-utils';
99
import { clonePlainJson } from '../template-chat-utils/json-patch-format';
1010
import { stripSchemaFieldsWhileStreaming } from '../../../utils';
1111
import { useTemplateSchema } from './use-template-schema';
12+
import { useTemplateConversation } from './use-template-conversation';
1213

13-
const errorMessagesMap = ref(new Map<string, string>());
1414
const lastPreviewSchema = ref<Record<string, unknown> | null>(null);
1515

1616
async function schemaCardRenderer(props: {
1717
content: string;
1818
cardId: string;
1919
}) {
2020
const {
21-
currentPreviewSchema,
2221
currentCardId,
2322
setCurrentPreviewSchema,
2423
} = useTemplateSchema();
@@ -41,13 +40,9 @@ async function schemaCardRenderer(props: {
4140
}
4241

4342
const json = stripSchemaFieldsWhileStreaming(value as Record<string, unknown>, isCompleted);
44-
const schemaWithId = generateIdForComponents(json, {
45-
previousSchema: currentPreviewSchema.value,
46-
});
47-
setCurrentPreviewSchema(schemaWithId, isCompleted);
43+
setCurrentPreviewSchema(json, isCompleted);
4844
} catch (error) {
4945
console.error('schemaCardRenderer error ===>', error);
50-
errorMessagesMap.value.set(props.cardId, (error as Error).message);
5146
}
5247
}
5348

@@ -69,9 +64,9 @@ async function jsonPatchRenderer(props: {
6964
currentSchema,
7065
currentPreviewSchema,
7166
currentCardId,
72-
setCurrentSchema,
7367
setCurrentPreviewSchema,
7468
} = useTemplateSchema();
69+
const { messages } = useTemplateConversation();
7570

7671
try {
7772
const { content, cardId, newMessage } = props;
@@ -116,23 +111,19 @@ async function jsonPatchRenderer(props: {
116111
return;
117112
}
118113

114+
const isStreamComplete = isSuccessfulParse || lastOperationComplete;
119115
const targetSchema = applyJsonPatchOperations(patchBaseline, operations as never[]);
120116
if (!targetSchema) {
117+
if (isStreamComplete) {
118+
setJsonPatchApplyResult('failed', messages.value, cardId);
119+
}
121120
return;
122121
}
123122

124-
const isStreamComplete = isSuccessfulParse || lastOperationComplete;
125123
const strippedSchema = stripSchemaFieldsWhileStreaming(targetSchema, isStreamComplete);
126-
127-
setCurrentPreviewSchema(
128-
generateIdForComponents(strippedSchema, { previousSchema: currentPreviewSchema.value }),
129-
isStreamComplete,
130-
);
131-
if (isStreamComplete) {
132-
setCurrentSchema(targetSchema);
133-
}
124+
setCurrentPreviewSchema(strippedSchema, isStreamComplete);
134125
} catch (error) {
135-
errorMessagesMap.value.set(props.cardId, (error as Error).message);
126+
setJsonPatchApplyResult('failed', messages.value, props.cardId);
136127
console.error('jsonPatch error ===>', error);
137128
}
138129
}
@@ -159,7 +150,6 @@ function resetLastPreviewSchema(schema: Record<string, unknown> | null) {
159150

160151
export function useTemplateStreamRender() {
161152
return {
162-
errorMessagesMap,
163153
lastPreviewSchema,
164154
handleSchemaJsonChanged,
165155
resetLastPreviewSchema,

sites/playground/web/src/components/genui-template/composables/use-template-version-control.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ export function useTemplateVersionControl() {
5050

5151
const allSchemaVersionHistoryEntries = computed(() =>
5252
collectSchemaVersionHistory(messages.value, {
53-
currentCardId: currentCardId.value,
5453
latestCardId: latestSchemaCardId.value,
5554
}),
5655
);

0 commit comments

Comments
 (0)