PluginProbe
Extendify / 3.2.2
Extendify v3.2.2
3.2.2 3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 All 128 releases
← All changes | src/Agent/state/chat.js +76 -32 3.0.6 → 3.2.2 View file →
@@ -1,43 +1,32 @@
1 -import { isChangeSiteDesignWorkflowAvailable, makeId } from '@agent/lib/util';
1 +import { buildToolMessages } from '@agent/lib/tool-messages';
2 +import { makeId } from '@agent/lib/util';
3 +import { useStatusStore } from '@agent/state/status';
2 4 import apiFetch from '@wordpress/api-fetch';
3 -import { __ } from '@wordpress/i18n';
4 5 import { create } from 'zustand';
5 6 import { createJSONStorage, devtools, persist } from 'zustand/middleware';
6 7
7 8 const { chatHistory } = window.extAgentData;
8 9
9 -const welcomeMessage = [
10 - {
11 - id: 1,
12 - type: 'message',
13 - details: {
14 - role: 'assistant',
15 - // translators: this is the initial message in the agent chat, welcoming the user. Keep it short and friendly and follow the same markdown format and emoji.
16 - content: isChangeSiteDesignWorkflowAvailable()
17 - ? __(
18 - '#### Your site is ready 🎉\nWant to explore other website designs?',
19 - 'extendify-local',
20 - )
21 - : __(
22 - '#### Your site is ready 🎉\nWant to explore other site colors?',
23 - 'extendify-local',
24 - ),
25 - },
26 - },
27 -];
28 10 const state = (set, get) => ({
29 - messages: chatHistory?.length ? chatHistory.toReversed() : welcomeMessage,
30 - // Messages sent to the api, user and assistant only. Up until the last workflow
31 - getMessagesForAI: () => {
11 + messages: chatHistory?.length ? chatHistory.toReversed() : [],
12 + // API messages, back to the last finished workflow.
13 + getCurrentMessages: ({ includeTools = true } = {}) => {
32 14 const messages = [];
33 15 let foundUserMessage = false;
34 16 for (const { type, details } of get().messages.toReversed()) {
35 - const finished =
36 - ['completed', 'canceled'].includes(details.status) ||
37 - (['status'].includes(type) && details.type === 'workflow-canceled');
17 + const finished = ['completed', 'canceled'].includes(details.status);
38 18 if (type === 'workflow' && finished) break;
39 19 if (type === 'workflow-component' && finished) break;
20 + if (type === 'message' && details.error) break;
21 + // A call with no result leaves the model answering a phantom.
22 + if (type === 'tool' && !('result' in details)) continue;
23 + if (type === 'tool' && includeTools) {
24 + // buildToolMessages returns [call, result]; push reversed so the
25 + // final toReversed() restores call-before-result order.
26 + for (const m of buildToolMessages(details).toReversed())
27 + messages.push(m);
28 + }
40 29 // This prevents a loop of assistant messages from being at the end
41 30 if (type === 'message' && details.role === 'user') {
42 31 foundUserMessage = true;
43 32 }
@@ -45,8 +34,44 @@
45 34 if (type === 'message') messages.push(details);
46 35 }
47 36 return messages.toReversed();
48 37 },
38 + // Most recent consecutive runs — another workflow in between resets the
39 + // memory.
40 + getMessagesFor: (workflowId) => {
41 + if (!workflowId) return [];
42 + const segments = [];
43 + let segment = [];
44 + for (const { type, details } of get().messages) {
45 + const finished = ['completed', 'canceled'].includes(details.status);
46 + if (['workflow', 'workflow-component'].includes(type) && finished) {
47 + // A canceled run's reply claims a change the next run then skips.
48 + const kept =
49 + details.status === 'canceled'
50 + ? segment.filter(
51 + (m) => m.role !== 'assistant' || typeof m.content !== 'string',
52 + )
53 + : segment;
54 + segments.push({ workflowId: details.workflowId, segment: kept });
55 + segment = [];
56 + continue;
57 + }
58 + // An error ended a run without a marker; what precedes it is dead.
59 + if (type === 'message' && details.error) {
60 + segment = [];
61 + continue;
62 + }
63 + if (type === 'tool' && 'result' in details) {
64 + segment.push(...buildToolMessages(details, { summarize: true }));
65 + continue;
66 + }
67 + if (type === 'message') segment.push(details);
68 + }
69 + const broken = segments.findLastIndex(
70 + (finished) => finished.workflowId !== workflowId,
71 + );
72 + return segments.slice(broken + 1).flatMap(({ segment }) => segment);
73 + },
49 74 getLastAssistantMessage: () =>
50 75 get()?.messages?.findLast(
51 76 (message) =>
52 77 message.type === 'message' && message.details?.role === 'assistant',
@@ -54,21 +79,30 @@
54 79 hasMessages: () => get().messages.length > 0,
55 80 addMessage: (type, details) => {
56 81 const id = makeId();
57 82 set((state) => {
58 - // max 150 messages
59 - const max = Math.max(0, state.messages.length - 149);
83 + // max 250 messages
84 + const max = Math.max(0, state.messages.length - 249);
60 85 const next = { id, type, details };
61 86 return {
62 87 // { id: 1, type: message, details: { role: 'user', content: 'Hello' } }
63 88 // { id: 2, type: message, details: { role: 'assistant', content: 'Hi there!' } }
64 89 // { id: 3, type: workflow, details: { name: 'Workflow 1' } }
65 - // { id: 5, type: status, details: { type: 'calling-agent' }
66 90 messages: [...state.messages.toSpliced(0, max), next],
67 91 };
68 92 });
93 + // A real message supersedes any in-flight progress status.
94 + useStatusStore.getState().clearStatuses();
69 95 return id;
70 96 },
97 + updateMessage: (id, details) =>
98 + set((state) => ({
99 + messages: state.messages.map((message) =>
100 + message.id === id
101 + ? { ...message, details: { ...message.details, ...details } }
102 + : message,
103 + ),
104 + })),
71 105 // pop messages all the way back to the last agent message
72 106 popMessage: () => {
73 107 set((state) => ({
74 108 messages: state.messages?.slice(0, -1) || [],
@@ -77,13 +111,23 @@
77 111 clearMessages: () => set({ messages: [] }),
78 112 });
79 113
80 114 const path = '/extendify/v1/agent/chat-events';
115 +let lastSave = Promise.resolve();
81 116 const storage = {
82 117 getItem: async () => await apiFetch({ path }),
83 - setItem: async (_name, state) =>
84 - await apiFetch({ path, method: 'POST', data: { state } }),
118 + setItem: (_name, state) => {
119 + lastSave = apiFetch({ path, method: 'POST', data: { state } });
120 + return lastSave;
121 + },
85 122 };
123 +
124 +// Await before navigating — an aborted in-flight save loses the newest messages.
125 +export const flushChatStorage = (timeout = 5000) =>
126 + Promise.race([
127 + lastSave.catch(() => null),
128 + new Promise((resolve) => setTimeout(resolve, timeout)),
129 + ]);
86 130
87 131 export const useChatStore = create()(
88 132 persist(devtools(state, { name: 'Extendify Agent Chat' }), {
89 133 name: `extendify-agent-chat-${window.extSharedData.siteId}`,