@nextclaw/ui 0.10.0 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -1
- package/dist/assets/{ChannelsList-VSRZzxx2.js → ChannelsList-BX7KqEk7.js} +4 -4
- package/dist/assets/ChatPage-zXLBKIAY.js +38 -0
- package/dist/assets/{DocBrowser-C65Hbvnb.js → DocBrowser-Cdbh4cVD.js} +1 -1
- package/dist/assets/{LogoBadge-4qtguXEJ.js → LogoBadge-4801esOJ.js} +1 -1
- package/dist/assets/MarketplacePage-GZgus0Or.js +49 -0
- package/dist/assets/{McpMarketplacePage-CHLkD8yX.js → McpMarketplacePage-CAGGvoMo.js} +1 -1
- package/dist/assets/{ModelConfig-CjsGdmZa.js → ModelConfig-CfLYjQM3.js} +1 -1
- package/dist/assets/ProvidersList-CEo1kdf-.js +1 -0
- package/dist/assets/{RemoteAccessPage-rOZCnH1x.js → RemoteAccessPage-6GYzD7cc.js} +1 -1
- package/dist/assets/{RuntimeConfig-CmJh6g0R.js → RuntimeConfig-BZdbp8mH.js} +1 -1
- package/dist/assets/{SearchConfig-C_hUuzR4.js → SearchConfig-ifvYKix-.js} +1 -1
- package/dist/assets/{SecretsConfig-Bu_zIRlQ.js → SecretsConfig-tDPbhTeR.js} +1 -1
- package/dist/assets/{SessionsConfig-DA_nqkM_.js → SessionsConfig-DhkAIzGm.js} +1 -1
- package/dist/assets/{chat-message-BOdA4h43.js → chat-message-C5Gl-dCH.js} +1 -1
- package/dist/assets/index-BTt_JlNV.css +1 -0
- package/dist/assets/index-JN3V84h_.js +8 -0
- package/dist/assets/{label-BYZ62ajO.js → label-D8zWKdqp.js} +1 -1
- package/dist/assets/{page-layout-UC-h92sU.js → page-layout-qAJ47LNQ.js} +1 -1
- package/dist/assets/{popover-DASCEr3G.js → popover-hyBGxpxS.js} +1 -1
- package/dist/assets/{security-config-Cvujq4fH.js → security-config-BJYZSnCA.js} +1 -1
- package/dist/assets/skeleton-CUQLsNsM.js +1 -0
- package/dist/assets/{status-dot-C1AvPwDD.js → status-dot-DKcoD-iY.js} +1 -1
- package/dist/assets/{switch-D3wVuCSh.js → switch-DtUdQxr_.js} +1 -1
- package/dist/assets/tabs-custom-Dj1BWHGK.js +1 -0
- package/dist/assets/useConfirmDialog-nZdrtETU.js +1 -0
- package/dist/assets/{vendor-DJt0Azq5.js → vendor-CNhxtHCf.js} +1 -1
- package/dist/index.html +3 -3
- package/package.json +5 -5
- package/src/components/chat/ChatSidebar.tsx +41 -69
- package/src/components/chat/adapters/chat-input-bar.adapter.test.ts +32 -1
- package/src/components/chat/adapters/chat-input-bar.adapter.ts +6 -3
- package/src/components/chat/adapters/chat-message.adapter.test.ts +141 -163
- package/src/components/chat/adapters/chat-message.adapter.ts +35 -0
- package/src/components/chat/chat-composer-state.ts +38 -0
- package/src/components/chat/chat-stream/types.ts +2 -0
- package/src/components/chat/containers/chat-input-bar.container.tsx +116 -55
- package/src/components/chat/containers/chat-message-list.container.tsx +2 -0
- package/src/components/chat/managers/chat-session-list.manager.test.ts +16 -1
- package/src/components/chat/managers/chat-session-list.manager.ts +0 -2
- package/src/components/chat/managers/chat-thread.manager.ts +0 -1
- package/src/components/chat/ncp/NcpChatPage.tsx +18 -18
- package/src/components/chat/ncp/ncp-app-client-fetch.test.ts +50 -33
- package/src/components/chat/ncp/ncp-app-client-fetch.ts +5 -123
- package/src/components/chat/ncp/ncp-chat-input.manager.ts +56 -1
- package/src/components/chat/ncp/ncp-chat-page-data.test.ts +8 -0
- package/src/components/chat/ncp/ncp-chat-thread.manager.ts +0 -1
- package/src/components/chat/presenter/chat-presenter-context.tsx +6 -0
- package/src/components/chat/stores/chat-input.store.ts +3 -0
- package/src/components/config/ChannelsList.test.tsx +2 -1
- package/src/components/config/weixin-channel-auth-section.test.tsx +2 -1
- package/src/components/layout/Sidebar.tsx +62 -102
- package/src/components/layout/sidebar-items.tsx +172 -0
- package/src/components/layout/sidebar.layout.test.tsx +11 -4
- package/src/lib/i18n.chat.ts +117 -0
- package/src/lib/i18n.remote.ts +1 -1
- package/src/lib/i18n.ts +2 -112
- package/src/transport/remote.transport.test.ts +135 -0
- package/src/transport/remote.transport.ts +11 -1
- package/dist/assets/ChatPage-CX0ZKE5i.js +0 -41
- package/dist/assets/MarketplacePage-DPCYptfD.js +0 -49
- package/dist/assets/ProvidersList-aXp_mo4J.js +0 -1
- package/dist/assets/index-C63mHRbE.css +0 -1
- package/dist/assets/index-DS7D1-KS.js +0 -8
- package/dist/assets/skeleton-DlYEKkkj.js +0 -1
- package/dist/assets/tabs-custom-CbgS7tu0.js +0 -1
- package/dist/assets/useConfirmDialog-BYbFEIbQ.js +0 -1
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import { useMemo, useState } from 'react';
|
|
1
|
+
import { useCallback, useMemo, useRef, useState } from 'react';
|
|
2
2
|
import { ChatInputBar } from '@nextclaw/agent-chat-ui';
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_NCP_IMAGE_ATTACHMENT_ACCEPT,
|
|
5
|
+
DEFAULT_NCP_IMAGE_ATTACHMENT_MAX_BYTES,
|
|
6
|
+
readFilesAsNcpDraftAttachments
|
|
7
|
+
} from '@nextclaw/ncp-react';
|
|
3
8
|
import {
|
|
4
9
|
buildChatSlashItems,
|
|
5
10
|
buildModelStateHint,
|
|
@@ -14,6 +19,7 @@ import { usePresenter } from '@/components/chat/presenter/chat-presenter-context
|
|
|
14
19
|
import { useI18n } from '@/components/providers/I18nProvider';
|
|
15
20
|
import { useChatInputStore } from '@/components/chat/stores/chat-input.store';
|
|
16
21
|
import { t } from '@/lib/i18n';
|
|
22
|
+
import { toast } from 'sonner';
|
|
17
23
|
|
|
18
24
|
function buildThinkingLabels(): Record<ChatThinkingLevel, string> {
|
|
19
25
|
return {
|
|
@@ -70,6 +76,7 @@ export function ChatInputBarContainer() {
|
|
|
70
76
|
const { language } = useI18n();
|
|
71
77
|
const snapshot = useChatInputStore((state) => state.snapshot);
|
|
72
78
|
const [slashQuery, setSlashQuery] = useState<string | null>(null);
|
|
79
|
+
const fileInputRef = useRef<HTMLInputElement | null>(null);
|
|
73
80
|
|
|
74
81
|
const officialSkillBadgeLabel = useMemo(() => {
|
|
75
82
|
// Keep memo reactive to locale switches even though `t` is imported as a stable function.
|
|
@@ -102,6 +109,7 @@ export function ChatInputBarContainer() {
|
|
|
102
109
|
const isModelOptionsEmpty = snapshot.isProviderStateResolved && !hasModelOptions;
|
|
103
110
|
const inputDisabled =
|
|
104
111
|
((isModelOptionsLoading || isModelOptionsEmpty) && !snapshot.isSending) || snapshot.sessionTypeUnavailable;
|
|
112
|
+
const attachmentSupported = typeof presenter.chatInputManager.addAttachments === 'function';
|
|
105
113
|
const textareaPlaceholder = isModelOptionsLoading
|
|
106
114
|
? ''
|
|
107
115
|
: hasModelOptions
|
|
@@ -122,6 +130,33 @@ export function ChatInputBarContainer() {
|
|
|
122
130
|
? t('chatStopPreparing')
|
|
123
131
|
: snapshot.stopDisabledReason?.trim() || t('chatStopUnavailable');
|
|
124
132
|
|
|
133
|
+
const showAttachmentError = useCallback((reason: 'unsupported-type' | 'too-large' | 'read-failed') => {
|
|
134
|
+
if (reason === 'unsupported-type') {
|
|
135
|
+
toast.error(t('chatInputImageUnsupported'));
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (reason === 'too-large') {
|
|
139
|
+
toast.error(
|
|
140
|
+
t('chatInputImageTooLarge').replace('{maxMb}', String(DEFAULT_NCP_IMAGE_ATTACHMENT_MAX_BYTES / (1024 * 1024)))
|
|
141
|
+
);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
toast.error(t('chatInputImageReadFailed'));
|
|
145
|
+
}, []);
|
|
146
|
+
|
|
147
|
+
const handleFilesAdd = useCallback(async (files: File[]) => {
|
|
148
|
+
if (!attachmentSupported || files.length === 0) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const result = await readFilesAsNcpDraftAttachments(files);
|
|
152
|
+
if (result.attachments.length > 0) {
|
|
153
|
+
presenter.chatInputManager.addAttachments?.(result.attachments);
|
|
154
|
+
}
|
|
155
|
+
if (result.rejected.length > 0) {
|
|
156
|
+
showAttachmentError(result.rejected[0].reason);
|
|
157
|
+
}
|
|
158
|
+
}, [attachmentSupported, presenter.chatInputManager, showAttachmentError]);
|
|
159
|
+
|
|
125
160
|
const toolbarSelects = [
|
|
126
161
|
buildModelToolbarSelect({
|
|
127
162
|
modelOptions: modelRecords,
|
|
@@ -160,60 +195,86 @@ export function ChatInputBarContainer() {
|
|
|
160
195
|
});
|
|
161
196
|
|
|
162
197
|
return (
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
hint={buildModelStateHint({
|
|
183
|
-
isModelOptionsLoading,
|
|
184
|
-
isModelOptionsEmpty,
|
|
185
|
-
onGoToProviders: presenter.chatInputManager.goToProviders,
|
|
186
|
-
texts: {
|
|
187
|
-
noModelOptionsLabel: t('chatModelNoOptions'),
|
|
188
|
-
configureProviderLabel: t('chatGoConfigureProvider')
|
|
189
|
-
}
|
|
190
|
-
})}
|
|
191
|
-
toolbar={{
|
|
192
|
-
selects: toolbarSelects,
|
|
193
|
-
accessories: [
|
|
194
|
-
{
|
|
195
|
-
key: 'attach',
|
|
196
|
-
label: t('chatInputAttach'),
|
|
197
|
-
icon: 'paperclip',
|
|
198
|
-
iconOnly: true,
|
|
199
|
-
disabled: true,
|
|
200
|
-
tooltip: t('chatInputAttachComingSoon')
|
|
198
|
+
<>
|
|
199
|
+
<ChatInputBar
|
|
200
|
+
composer={{
|
|
201
|
+
nodes: snapshot.composerNodes,
|
|
202
|
+
placeholder: textareaPlaceholder,
|
|
203
|
+
disabled: inputDisabled,
|
|
204
|
+
onNodesChange: presenter.chatInputManager.setComposerNodes,
|
|
205
|
+
...(attachmentSupported ? { onFilesAdd: handleFilesAdd } : {}),
|
|
206
|
+
onSlashQueryChange: setSlashQuery
|
|
207
|
+
}}
|
|
208
|
+
slashMenu={{
|
|
209
|
+
isLoading: snapshot.isSkillsLoading,
|
|
210
|
+
items: slashItems,
|
|
211
|
+
texts: {
|
|
212
|
+
slashLoadingLabel: t('chatSlashLoading'),
|
|
213
|
+
slashSectionLabel: t('chatSlashSectionSkills'),
|
|
214
|
+
slashEmptyLabel: t('chatSlashNoResult'),
|
|
215
|
+
slashHintLabel: t('chatSlashHint'),
|
|
216
|
+
slashSkillHintLabel: t('chatSlashSkillHint')
|
|
201
217
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
+
}}
|
|
219
|
+
hint={buildModelStateHint({
|
|
220
|
+
isModelOptionsLoading,
|
|
221
|
+
isModelOptionsEmpty,
|
|
222
|
+
onGoToProviders: presenter.chatInputManager.goToProviders,
|
|
223
|
+
texts: {
|
|
224
|
+
noModelOptionsLabel: t('chatModelNoOptions'),
|
|
225
|
+
configureProviderLabel: t('chatGoConfigureProvider')
|
|
226
|
+
}
|
|
227
|
+
})}
|
|
228
|
+
toolbar={{
|
|
229
|
+
selects: toolbarSelects,
|
|
230
|
+
accessories: [
|
|
231
|
+
{
|
|
232
|
+
key: 'attach',
|
|
233
|
+
label: t('chatInputAttach'),
|
|
234
|
+
icon: 'paperclip',
|
|
235
|
+
iconOnly: true,
|
|
236
|
+
disabled: !attachmentSupported || inputDisabled || snapshot.isSending,
|
|
237
|
+
...(attachmentSupported
|
|
238
|
+
? {
|
|
239
|
+
onClick: () => fileInputRef.current?.click()
|
|
240
|
+
}
|
|
241
|
+
: {
|
|
242
|
+
tooltip: t('chatInputAttachComingSoon')
|
|
243
|
+
})
|
|
244
|
+
}
|
|
245
|
+
],
|
|
246
|
+
skillPicker,
|
|
247
|
+
actions: {
|
|
248
|
+
sendError: snapshot.sendError,
|
|
249
|
+
isSending: snapshot.isSending,
|
|
250
|
+
canStopGeneration: snapshot.canStopGeneration,
|
|
251
|
+
sendDisabled:
|
|
252
|
+
(snapshot.draft.trim().length === 0 && snapshot.attachments.length === 0) ||
|
|
253
|
+
!hasModelOptions ||
|
|
254
|
+
snapshot.sessionTypeUnavailable,
|
|
255
|
+
stopDisabled: !snapshot.canStopGeneration,
|
|
256
|
+
stopHint: resolvedStopHint,
|
|
257
|
+
sendButtonLabel: t('chatSend'),
|
|
258
|
+
stopButtonLabel: t('chatStop'),
|
|
259
|
+
onSend: presenter.chatInputManager.send,
|
|
260
|
+
onStop: presenter.chatInputManager.stop
|
|
261
|
+
}
|
|
262
|
+
}}
|
|
263
|
+
/>
|
|
264
|
+
{attachmentSupported ? (
|
|
265
|
+
<input
|
|
266
|
+
ref={fileInputRef}
|
|
267
|
+
type="file"
|
|
268
|
+
accept={DEFAULT_NCP_IMAGE_ATTACHMENT_ACCEPT}
|
|
269
|
+
multiple
|
|
270
|
+
className="hidden"
|
|
271
|
+
onChange={async (event) => {
|
|
272
|
+
const files = Array.from(event.target.files ?? []);
|
|
273
|
+
event.currentTarget.value = '';
|
|
274
|
+
await handleFilesAdd(files);
|
|
275
|
+
}}
|
|
276
|
+
/>
|
|
277
|
+
) : null}
|
|
278
|
+
</>
|
|
218
279
|
);
|
|
219
280
|
}
|
|
@@ -48,6 +48,8 @@ export function ChatMessageListContainer(props: ChatMessageListContainerProps) {
|
|
|
48
48
|
toolResultLabel: t("chatToolResult"),
|
|
49
49
|
toolNoOutputLabel: t("chatToolNoOutput"),
|
|
50
50
|
toolOutputLabel: t("chatToolOutput"),
|
|
51
|
+
imageAttachmentLabel: t("chatImageAttachment"),
|
|
52
|
+
fileAttachmentLabel: t("chatFileAttachment"),
|
|
51
53
|
unknownPartLabel: t("chatUnknownPart"),
|
|
52
54
|
},
|
|
53
55
|
}),
|
|
@@ -33,7 +33,22 @@ describe('ChatSessionListManager', () => {
|
|
|
33
33
|
|
|
34
34
|
expect(streamActionsManager.resetStreamState).toHaveBeenCalledTimes(1);
|
|
35
35
|
expect(uiManager.goToChatRoot).toHaveBeenCalledTimes(1);
|
|
36
|
-
expect(useChatSessionListStore.getState().snapshot.selectedSessionKey).
|
|
36
|
+
expect(useChatSessionListStore.getState().snapshot.selectedSessionKey).toBe('session-1');
|
|
37
37
|
expect(useChatInputStore.getState().snapshot.pendingSessionType).toBe('codex');
|
|
38
38
|
});
|
|
39
|
+
|
|
40
|
+
it('delegates existing-session selection to routing without eagerly mutating the selected session state', () => {
|
|
41
|
+
const uiManager = {
|
|
42
|
+
goToSession: vi.fn()
|
|
43
|
+
} as unknown as ConstructorParameters<typeof ChatSessionListManager>[0];
|
|
44
|
+
const streamActionsManager = {
|
|
45
|
+
resetStreamState: vi.fn()
|
|
46
|
+
} as unknown as ConstructorParameters<typeof ChatSessionListManager>[1];
|
|
47
|
+
|
|
48
|
+
const manager = new ChatSessionListManager(uiManager, streamActionsManager);
|
|
49
|
+
manager.selectSession('session-2');
|
|
50
|
+
|
|
51
|
+
expect(uiManager.goToSession).toHaveBeenCalledWith('session-2');
|
|
52
|
+
expect(useChatSessionListStore.getState().snapshot.selectedSessionKey).toBe('session-1');
|
|
53
|
+
});
|
|
39
54
|
});
|
|
@@ -62,13 +62,11 @@ export class ChatSessionListManager {
|
|
|
62
62
|
? sessionType.trim()
|
|
63
63
|
: defaultSessionType;
|
|
64
64
|
this.streamActionsManager.resetStreamState();
|
|
65
|
-
this.setSelectedSessionKey(null);
|
|
66
65
|
useChatInputStore.getState().setSnapshot({ pendingSessionType: nextSessionType });
|
|
67
66
|
this.uiManager.goToChatRoot();
|
|
68
67
|
};
|
|
69
68
|
|
|
70
69
|
selectSession = (sessionKey: string) => {
|
|
71
|
-
this.setSelectedSessionKey(sessionKey);
|
|
72
70
|
this.uiManager.goToSession(sessionKey);
|
|
73
71
|
};
|
|
74
72
|
|
|
@@ -78,7 +78,6 @@ export class ChatThreadManager {
|
|
|
78
78
|
try {
|
|
79
79
|
await deleteSessionApi(selectedSessionKey);
|
|
80
80
|
this.streamActionsManager.resetStreamState();
|
|
81
|
-
useChatSessionListStore.getState().setSnapshot({ selectedSessionKey: null });
|
|
82
81
|
this.uiManager.goToChatRoot({ replace: true });
|
|
83
82
|
await this.actions.refetchSessions();
|
|
84
83
|
} finally {
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
2
|
import { NcpHttpAgentClientEndpoint } from '@nextclaw/ncp-http-agent-client';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
buildNcpRequestEnvelope,
|
|
5
|
+
useHydratedNcpAgent,
|
|
6
|
+
type NcpConversationSeed
|
|
7
|
+
} from '@nextclaw/ncp-react';
|
|
4
8
|
import { useLocation, useNavigate, useParams } from 'react-router-dom';
|
|
5
9
|
import { API_BASE } from '@/api/api-base';
|
|
6
10
|
import { fetchNcpSessionMessages } from '@/api/ncp-session';
|
|
@@ -186,29 +190,25 @@ export function NcpChatPage({ view }: ChatPageProps) {
|
|
|
186
190
|
sessionType: payload.sessionType,
|
|
187
191
|
requestedSkills: payload.requestedSkills
|
|
188
192
|
});
|
|
193
|
+
const envelope = buildNcpRequestEnvelope({
|
|
194
|
+
sessionId: payload.sessionKey,
|
|
195
|
+
text: payload.message,
|
|
196
|
+
attachments: payload.attachments,
|
|
197
|
+
metadata
|
|
198
|
+
});
|
|
199
|
+
if (!envelope) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
189
202
|
try {
|
|
190
203
|
void sessionsQuery.refetch();
|
|
191
|
-
await agent.send(
|
|
192
|
-
sessionId: payload.sessionKey,
|
|
193
|
-
message: {
|
|
194
|
-
id: `user-${Date.now().toString(36)}`,
|
|
195
|
-
sessionId: payload.sessionKey,
|
|
196
|
-
role: 'user',
|
|
197
|
-
status: 'final',
|
|
198
|
-
parts: [{ type: 'text', text: payload.message }],
|
|
199
|
-
timestamp: new Date().toISOString(),
|
|
200
|
-
...(Object.keys(metadata).length > 0 ? { metadata } : {})
|
|
201
|
-
},
|
|
202
|
-
...(Object.keys(metadata).length > 0 ? { metadata } : {})
|
|
203
|
-
});
|
|
204
|
+
await agent.send(envelope);
|
|
204
205
|
await sessionsQuery.refetch();
|
|
205
206
|
} catch (error) {
|
|
206
207
|
if (payload.restoreDraftOnError) {
|
|
207
208
|
if (payload.composerNodes && payload.composerNodes.length > 0) {
|
|
208
|
-
presenter.chatInputManager.
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
: currentNodes
|
|
209
|
+
presenter.chatInputManager.restoreComposerState?.(
|
|
210
|
+
payload.composerNodes,
|
|
211
|
+
payload.attachments ?? []
|
|
212
212
|
);
|
|
213
213
|
} else {
|
|
214
214
|
presenter.chatInputManager.setDraft((currentDraft) =>
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
1
2
|
import { createNcpAppClientFetch } from '@/components/chat/ncp/ncp-app-client-fetch';
|
|
2
3
|
|
|
3
|
-
const
|
|
4
|
-
request: vi.fn(),
|
|
5
|
-
openStream: vi.fn()
|
|
6
|
-
}));
|
|
7
|
-
|
|
8
|
-
vi.mock('@/transport', () => ({
|
|
9
|
-
appClient: {
|
|
10
|
-
request: mocks.request,
|
|
11
|
-
openStream: mocks.openStream
|
|
12
|
-
}
|
|
13
|
-
}));
|
|
4
|
+
const fetchMock = vi.fn<typeof fetch>();
|
|
14
5
|
|
|
15
6
|
describe('ncp-app-client-fetch', () => {
|
|
16
7
|
beforeEach(() => {
|
|
17
|
-
|
|
18
|
-
|
|
8
|
+
fetchMock.mockReset();
|
|
9
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
vi.unstubAllGlobals();
|
|
19
14
|
});
|
|
20
15
|
|
|
21
|
-
it('
|
|
22
|
-
|
|
16
|
+
it('keeps native fetch semantics and only injects credentials', async () => {
|
|
17
|
+
fetchMock.mockResolvedValue(new Response(JSON.stringify({ ok: true }), {
|
|
18
|
+
status: 200,
|
|
19
|
+
headers: {
|
|
20
|
+
'content-type': 'application/json'
|
|
21
|
+
}
|
|
22
|
+
}));
|
|
23
23
|
const fetchImpl = createNcpAppClientFetch();
|
|
24
24
|
|
|
25
25
|
const response = await fetchImpl('http://127.0.0.1:55667/api/ncp/agent/abort', {
|
|
@@ -31,22 +31,40 @@ describe('ncp-app-client-fetch', () => {
|
|
|
31
31
|
body: JSON.stringify({ sessionId: 's1' })
|
|
32
32
|
});
|
|
33
33
|
|
|
34
|
-
expect(
|
|
34
|
+
expect(fetchMock).toHaveBeenCalledWith('http://127.0.0.1:55667/api/ncp/agent/abort', {
|
|
35
35
|
method: 'POST',
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
headers: {
|
|
37
|
+
accept: 'application/json',
|
|
38
|
+
'content-type': 'application/json'
|
|
39
|
+
},
|
|
40
|
+
body: JSON.stringify({ sessionId: 's1' }),
|
|
41
|
+
credentials: 'include'
|
|
38
42
|
});
|
|
39
43
|
expect(response.ok).toBe(true);
|
|
44
|
+
expect(await response.json()).toEqual({ ok: true });
|
|
40
45
|
});
|
|
41
46
|
|
|
42
|
-
it('
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
it('does not synthesize fake HTTP 500 responses for fetch failures', async () => {
|
|
48
|
+
fetchMock.mockRejectedValue(new Error('Failed to fetch'));
|
|
49
|
+
const fetchImpl = createNcpAppClientFetch();
|
|
50
|
+
|
|
51
|
+
await expect(fetchImpl('http://127.0.0.1:55667/api/ncp/agent/abort', {
|
|
52
|
+
method: 'POST',
|
|
53
|
+
headers: {
|
|
54
|
+
accept: 'application/json',
|
|
55
|
+
'content-type': 'application/json'
|
|
56
|
+
},
|
|
57
|
+
body: JSON.stringify({ sessionId: 's1' })
|
|
58
|
+
})).rejects.toThrow('Failed to fetch');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('preserves native SSE request headers', async () => {
|
|
62
|
+
fetchMock.mockResolvedValue(new Response('event: ncp-event\ndata: {"ok":true}\n\n', {
|
|
63
|
+
status: 200,
|
|
64
|
+
headers: {
|
|
65
|
+
'content-type': 'text/event-stream'
|
|
66
|
+
}
|
|
67
|
+
}));
|
|
50
68
|
const fetchImpl = createNcpAppClientFetch();
|
|
51
69
|
|
|
52
70
|
const response = await fetchImpl('http://127.0.0.1:55667/api/ncp/agent/stream?sessionId=s1', {
|
|
@@ -55,15 +73,14 @@ describe('ncp-app-client-fetch', () => {
|
|
|
55
73
|
accept: 'text/event-stream'
|
|
56
74
|
}
|
|
57
75
|
});
|
|
58
|
-
const text = await response.text();
|
|
59
76
|
|
|
60
|
-
expect(
|
|
77
|
+
expect(fetchMock).toHaveBeenCalledWith('http://127.0.0.1:55667/api/ncp/agent/stream?sessionId=s1', {
|
|
61
78
|
method: 'GET',
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
79
|
+
headers: {
|
|
80
|
+
accept: 'text/event-stream'
|
|
81
|
+
},
|
|
82
|
+
credentials: 'include'
|
|
65
83
|
});
|
|
66
|
-
expect(text).toContain('event: ncp-event');
|
|
67
|
-
expect(text).toContain('"text":"hello"');
|
|
84
|
+
expect(await response.text()).toContain('event: ncp-event');
|
|
68
85
|
});
|
|
69
86
|
});
|
|
@@ -1,127 +1,9 @@
|
|
|
1
|
-
import { API_BASE } from '@/api/api-base';
|
|
2
|
-
import { appClient } from '@/transport';
|
|
3
|
-
|
|
4
1
|
type FetchLike = typeof fetch;
|
|
5
2
|
|
|
6
3
|
export function createNcpAppClientFetch(): FetchLike {
|
|
7
|
-
return
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
const data = await appClient.request<unknown>({
|
|
15
|
-
method: request.method,
|
|
16
|
-
path: request.path,
|
|
17
|
-
...(request.body !== undefined ? { body: request.body } : {})
|
|
18
|
-
});
|
|
19
|
-
return new Response(JSON.stringify(data ?? {}), {
|
|
20
|
-
status: 200,
|
|
21
|
-
headers: {
|
|
22
|
-
'content-type': 'application/json'
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
} catch (error) {
|
|
26
|
-
return new Response(error instanceof Error ? error.message : String(error), {
|
|
27
|
-
status: 500,
|
|
28
|
-
headers: {
|
|
29
|
-
'content-type': 'text/plain; charset=utf-8'
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
type RequestSnapshot = {
|
|
37
|
-
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
38
|
-
path: string;
|
|
39
|
-
body?: unknown;
|
|
40
|
-
signal?: AbortSignal;
|
|
41
|
-
headers: Headers;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
function toRequestSnapshot(input: URL | string | Request, init?: RequestInit): RequestSnapshot {
|
|
45
|
-
const request = input instanceof Request ? input : null;
|
|
46
|
-
const url = new URL(
|
|
47
|
-
typeof input === 'string'
|
|
48
|
-
? input
|
|
49
|
-
: input instanceof URL
|
|
50
|
-
? input.toString()
|
|
51
|
-
: input.url,
|
|
52
|
-
API_BASE
|
|
53
|
-
);
|
|
54
|
-
const headers = new Headers(init?.headers ?? request?.headers);
|
|
55
|
-
const method = ((init?.method ?? request?.method ?? 'GET').toUpperCase()) as RequestSnapshot['method'];
|
|
56
|
-
return {
|
|
57
|
-
method,
|
|
58
|
-
path: `${url.pathname}${url.search}`,
|
|
59
|
-
body: parseRequestBody(init?.body),
|
|
60
|
-
signal: init?.signal ?? request?.signal ?? undefined,
|
|
61
|
-
headers
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function parseRequestBody(body: BodyInit | null | undefined): unknown {
|
|
66
|
-
if (body === undefined || body === null) {
|
|
67
|
-
return undefined;
|
|
68
|
-
}
|
|
69
|
-
if (typeof body === 'string') {
|
|
70
|
-
try {
|
|
71
|
-
return JSON.parse(body);
|
|
72
|
-
} catch {
|
|
73
|
-
return body;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return body;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function isSseRequest(request: RequestSnapshot): boolean {
|
|
80
|
-
const accept = request.headers.get('accept')?.toLowerCase() ?? '';
|
|
81
|
-
return accept.includes('text/event-stream');
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function createSseResponse(request: RequestSnapshot): Response {
|
|
85
|
-
const encoder = new TextEncoder();
|
|
86
|
-
let session: ReturnType<typeof appClient.openStream<unknown>> | null = null;
|
|
87
|
-
|
|
88
|
-
const stream = new ReadableStream<Uint8Array>({
|
|
89
|
-
start(controller) {
|
|
90
|
-
session = appClient.openStream<unknown>({
|
|
91
|
-
method: request.method === 'GET' ? 'GET' : 'POST',
|
|
92
|
-
path: request.path,
|
|
93
|
-
...(request.body !== undefined ? { body: request.body } : {}),
|
|
94
|
-
signal: request.signal,
|
|
95
|
-
onEvent: (event) => {
|
|
96
|
-
controller.enqueue(encoder.encode(encodeSseFrame(event.name, event.payload)));
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
void session.finished
|
|
101
|
-
.then(() => {
|
|
102
|
-
controller.close();
|
|
103
|
-
})
|
|
104
|
-
.catch((error) => {
|
|
105
|
-
controller.enqueue(encoder.encode(encodeSseFrame('error', {
|
|
106
|
-
message: error instanceof Error ? error.message : String(error)
|
|
107
|
-
})));
|
|
108
|
-
controller.close();
|
|
109
|
-
});
|
|
110
|
-
},
|
|
111
|
-
cancel() {
|
|
112
|
-
session?.cancel();
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
return new Response(stream, {
|
|
117
|
-
status: 200,
|
|
118
|
-
headers: {
|
|
119
|
-
'content-type': 'text/event-stream'
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
function encodeSseFrame(event: string, payload: unknown): string {
|
|
125
|
-
const data = payload === undefined ? '' : JSON.stringify(payload);
|
|
126
|
-
return `event: ${event}\ndata: ${data}\n\n`;
|
|
4
|
+
return (input, init) =>
|
|
5
|
+
fetch(input, {
|
|
6
|
+
credentials: 'include',
|
|
7
|
+
...init
|
|
8
|
+
});
|
|
127
9
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ChatComposerNode } from '@nextclaw/agent-chat-ui';
|
|
2
|
+
import type { NcpDraftAttachment } from '@nextclaw/ncp-react';
|
|
2
3
|
import type { SetStateAction } from 'react';
|
|
3
4
|
import type { ThinkingLevel } from '@/api/types';
|
|
4
5
|
import { updateNcpSession } from '@/api/ncp-session';
|
|
@@ -7,6 +8,8 @@ import {
|
|
|
7
8
|
createInitialChatComposerNodes,
|
|
8
9
|
deriveChatComposerDraft,
|
|
9
10
|
deriveSelectedSkillsFromComposer,
|
|
11
|
+
pruneComposerAttachments,
|
|
12
|
+
syncComposerAttachments,
|
|
10
13
|
syncComposerSkills
|
|
11
14
|
} from '@/components/chat/chat-composer-state';
|
|
12
15
|
import { useChatInputStore } from '@/components/chat/stores/chat-input.store';
|
|
@@ -48,13 +51,47 @@ export class NcpChatInputManager {
|
|
|
48
51
|
left.length === right.length && left.every((value, index) => value === right[index]);
|
|
49
52
|
|
|
50
53
|
private syncComposerSnapshot = (nodes: ChatComposerNode[]) => {
|
|
54
|
+
const currentAttachments = useChatInputStore.getState().snapshot.attachments;
|
|
55
|
+
const attachments = pruneComposerAttachments(nodes, currentAttachments);
|
|
51
56
|
useChatInputStore.getState().setSnapshot({
|
|
52
57
|
composerNodes: nodes,
|
|
58
|
+
attachments,
|
|
53
59
|
draft: deriveChatComposerDraft(nodes),
|
|
54
60
|
selectedSkills: deriveSelectedSkillsFromComposer(nodes)
|
|
55
61
|
});
|
|
56
62
|
};
|
|
57
63
|
|
|
64
|
+
private syncComposerSnapshotWithAttachments = (
|
|
65
|
+
nodes: ChatComposerNode[],
|
|
66
|
+
attachments: NcpDraftAttachment[]
|
|
67
|
+
) => {
|
|
68
|
+
useChatInputStore.getState().setSnapshot({
|
|
69
|
+
composerNodes: nodes,
|
|
70
|
+
attachments,
|
|
71
|
+
draft: deriveChatComposerDraft(nodes),
|
|
72
|
+
selectedSkills: deriveSelectedSkillsFromComposer(nodes)
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
private dedupeAttachments = (attachments: NcpDraftAttachment[]): NcpDraftAttachment[] => {
|
|
77
|
+
const seen = new Set<string>();
|
|
78
|
+
const output: NcpDraftAttachment[] = [];
|
|
79
|
+
for (const attachment of attachments) {
|
|
80
|
+
const signature = [
|
|
81
|
+
attachment.name,
|
|
82
|
+
attachment.mimeType,
|
|
83
|
+
String(attachment.sizeBytes),
|
|
84
|
+
attachment.contentBase64,
|
|
85
|
+
].join(':');
|
|
86
|
+
if (seen.has(signature)) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
seen.add(signature);
|
|
90
|
+
output.push(attachment);
|
|
91
|
+
}
|
|
92
|
+
return output;
|
|
93
|
+
};
|
|
94
|
+
|
|
58
95
|
syncSnapshot = (patch: Partial<ChatInputSnapshot>) => {
|
|
59
96
|
if (!this.hasSnapshotChanges(patch)) {
|
|
60
97
|
return;
|
|
@@ -88,6 +125,22 @@ export class NcpChatInputManager {
|
|
|
88
125
|
this.syncComposerSnapshot(value);
|
|
89
126
|
};
|
|
90
127
|
|
|
128
|
+
addAttachments = (attachments: NcpDraftAttachment[]) => {
|
|
129
|
+
if (attachments.length === 0) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const snapshot = useChatInputStore.getState().snapshot;
|
|
133
|
+
const nextAttachments = this.dedupeAttachments([...snapshot.attachments, ...attachments]);
|
|
134
|
+
const nextNodes = syncComposerAttachments(snapshot.composerNodes, nextAttachments);
|
|
135
|
+
this.syncComposerSnapshotWithAttachments(nextNodes, nextAttachments);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
restoreComposerState = (nodes: ChatComposerNode[], attachments: NcpDraftAttachment[]) => {
|
|
139
|
+
const nextAttachments = this.dedupeAttachments(attachments);
|
|
140
|
+
const nextNodes = syncComposerAttachments(nodes, nextAttachments);
|
|
141
|
+
this.syncComposerSnapshotWithAttachments(nextNodes, nextAttachments);
|
|
142
|
+
};
|
|
143
|
+
|
|
91
144
|
setPendingSessionType = (next: SetStateAction<string>) => {
|
|
92
145
|
const prev = useChatInputStore.getState().snapshot.pendingSessionType;
|
|
93
146
|
const value = this.resolveUpdateValue(prev, next);
|
|
@@ -101,7 +154,8 @@ export class NcpChatInputManager {
|
|
|
101
154
|
const inputSnapshot = useChatInputStore.getState().snapshot;
|
|
102
155
|
const sessionSnapshot = useChatSessionListStore.getState().snapshot;
|
|
103
156
|
const message = inputSnapshot.draft.trim();
|
|
104
|
-
|
|
157
|
+
const attachments = inputSnapshot.attachments;
|
|
158
|
+
if (!message && attachments.length === 0) {
|
|
105
159
|
return;
|
|
106
160
|
}
|
|
107
161
|
const { selectedSkills: requestedSkills, composerNodes } = inputSnapshot;
|
|
@@ -119,6 +173,7 @@ export class NcpChatInputManager {
|
|
|
119
173
|
thinkingLevel: inputSnapshot.selectedThinkingLevel ?? undefined,
|
|
120
174
|
stopSupported: true,
|
|
121
175
|
requestedSkills,
|
|
176
|
+
attachments,
|
|
122
177
|
restoreDraftOnError: true,
|
|
123
178
|
composerNodes
|
|
124
179
|
});
|