@nextclaw/ui 0.9.11 → 0.9.12
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 +7 -0
- package/dist/assets/{ChannelsList-Brc1qLSU.js → ChannelsList-CJy2GG1a.js} +1 -1
- package/dist/assets/ChatPage-C7WxI8VY.js +41 -0
- package/dist/assets/{DocBrowser-xLVf1p4L.js → DocBrowser-Nu-ae-eS.js} +1 -1
- package/dist/assets/{LogoBadge-CcTyimdr.js → LogoBadge-DbbMxPlr.js} +1 -1
- package/dist/assets/{MarketplacePage-Bk-qXxyh.js → MarketplacePage-BQYQPeg2.js} +2 -2
- package/dist/assets/{McpMarketplacePage-gFqAYekc.js → McpMarketplacePage-kiMJbS8r.js} +1 -1
- package/dist/assets/{ModelConfig-DnKNTuw6.js → ModelConfig-DRQ07Snj.js} +1 -1
- package/dist/assets/{ProvidersList-Cjr8EFu_.js → ProvidersList-C0NjzKX1.js} +1 -1
- package/dist/assets/RemoteAccessPage-DVJ5hBNJ.js +1 -0
- package/dist/assets/{RuntimeConfig-CttN--Tv.js → RuntimeConfig-BkYWyRW7.js} +1 -1
- package/dist/assets/{SearchConfig-D-GzinsL.js → SearchConfig-DZTW8Wnq.js} +1 -1
- package/dist/assets/{SecretsConfig-BvqQq4Ds.js → SecretsConfig-WMcwg5KV.js} +2 -2
- package/dist/assets/{SessionsConfig-DbtnLmI6.js → SessionsConfig-CWtCXQRn.js} +1 -1
- package/dist/assets/{chat-message-DYQjL1tD.js → chat-message-BcjCODYN.js} +1 -1
- package/dist/assets/index-BOhlxC12.js +8 -0
- package/dist/assets/{index-DfEAJJsA.css → index-SGSkQCPi.css} +1 -1
- package/dist/assets/{label-DBSKOMGE.js → label-DOWMfYPL.js} +1 -1
- package/dist/assets/{page-layout-B5th9UzR.js → page-layout-DQtmTgqR.js} +1 -1
- package/dist/assets/popover-k11l1-ko.js +1 -0
- package/dist/assets/{security-config-D72JskP5.js → security-config-FFy-bOJb.js} +1 -1
- package/dist/assets/skeleton-DQ4QRdSe.js +1 -0
- package/dist/assets/{status-dot-CU5ZpOn1.js → status-dot-CsZRxe8p.js} +1 -1
- package/dist/assets/{switch-BdaXEtXk.js → switch-DfMy8G96.js} +1 -1
- package/dist/assets/{tabs-custom-BVhSoteN.js → tabs-custom-CITPDGXY.js} +1 -1
- package/dist/assets/{useConfirmDialog-Dugi9V-Z.js → useConfirmDialog-Dr39o-0I.js} +1 -1
- package/dist/assets/{vendor-CmQZsDAE.js → vendor-TJ2hy_Lv.js} +87 -82
- package/dist/index.html +3 -3
- package/package.json +4 -4
- package/src/account/managers/account.manager.ts +8 -1
- package/src/account/stores/account.store.ts +3 -0
- package/src/api/api-base.ts +16 -0
- package/src/api/client.test.ts +69 -0
- package/src/api/client.ts +29 -87
- package/src/api/config.stream.test.ts +115 -0
- package/src/api/config.ts +49 -121
- package/src/api/raw-client.ts +87 -0
- package/src/components/chat/ChatSidebar.test.tsx +134 -1
- package/src/components/chat/ChatSidebar.tsx +87 -37
- package/src/components/chat/chat-session-label.service.ts +34 -0
- package/src/components/chat/chat-sidebar-session-item.tsx +147 -0
- package/src/components/chat/ncp/NcpChatPage.tsx +3 -10
- package/src/components/chat/ncp/ncp-app-client-fetch.test.ts +69 -0
- package/src/components/chat/ncp/ncp-app-client-fetch.ts +127 -0
- package/src/components/remote/RemoteAccessPage.test.tsx +103 -0
- package/src/components/remote/RemoteAccessPage.tsx +28 -93
- package/src/lib/i18n.remote.ts +20 -8
- package/src/remote/managers/remote-access.manager.ts +13 -0
- package/src/remote/remote-access-feedback.service.test.ts +75 -0
- package/src/remote/remote-access-feedback.service.ts +195 -0
- package/src/transport/app-client.ts +1 -1
- package/src/transport/local.transport.ts +3 -2
- package/src/transport/remote.transport.ts +7 -2
- package/dist/assets/ChatPage-DmGI776q.js +0 -38
- package/dist/assets/RemoteAccessPage-Rzi5a6Gc.js +0 -1
- package/dist/assets/index-ClLy_7T2.js +0 -8
- package/dist/assets/popover-BEIWRoeP.js +0 -1
- package/dist/assets/skeleton-B_Pn9x0i.js +0 -1
package/dist/index.html
CHANGED
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
<link rel="icon" type="image/svg+xml" href="/logo.svg" />
|
|
7
7
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
8
8
|
<title>NextClaw - 系统配置</title>
|
|
9
|
-
<script type="module" crossorigin src="/assets/index-
|
|
10
|
-
<link rel="modulepreload" crossorigin href="/assets/vendor-
|
|
11
|
-
<link rel="stylesheet" crossorigin href="/assets/index-
|
|
9
|
+
<script type="module" crossorigin src="/assets/index-BOhlxC12.js"></script>
|
|
10
|
+
<link rel="modulepreload" crossorigin href="/assets/vendor-TJ2hy_Lv.js">
|
|
11
|
+
<link rel="stylesheet" crossorigin href="/assets/index-SGSkQCPi.css">
|
|
12
12
|
</head>
|
|
13
13
|
|
|
14
14
|
<body>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nextclaw/ui",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.12",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -27,11 +27,11 @@
|
|
|
27
27
|
"tailwind-merge": "^2.5.4",
|
|
28
28
|
"zod": "^3.23.8",
|
|
29
29
|
"zustand": "^5.0.2",
|
|
30
|
-
"@nextclaw/ncp-http-agent-client": "0.3.1",
|
|
31
|
-
"@nextclaw/agent-chat": "0.1.1",
|
|
32
30
|
"@nextclaw/ncp": "0.3.1",
|
|
31
|
+
"@nextclaw/ncp-http-agent-client": "0.3.1",
|
|
33
32
|
"@nextclaw/agent-chat-ui": "0.2.1",
|
|
34
|
-
"@nextclaw/ncp-react": "0.3.2"
|
|
33
|
+
"@nextclaw/ncp-react": "0.3.2",
|
|
34
|
+
"@nextclaw/agent-chat": "0.1.1"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@testing-library/react": "^16.3.0",
|
|
@@ -51,9 +51,16 @@ export class AccountManager {
|
|
|
51
51
|
return false;
|
|
52
52
|
};
|
|
53
53
|
|
|
54
|
-
startBrowserSignIn = async (params?: {
|
|
54
|
+
startBrowserSignIn = async (params?: {
|
|
55
|
+
apiBase?: string;
|
|
56
|
+
status?: RemoteAccessView;
|
|
57
|
+
pendingAction?: AccountPendingAction;
|
|
58
|
+
}) => {
|
|
55
59
|
try {
|
|
56
60
|
const status = params?.status ?? (await ensureRemoteStatus());
|
|
61
|
+
if (params?.pendingAction) {
|
|
62
|
+
useAccountStore.getState().setPendingAction(params.pendingAction);
|
|
63
|
+
}
|
|
57
64
|
const result = await startRemoteBrowserAuth({
|
|
58
65
|
apiBase: resolveRemotePlatformApiBase(status, params?.apiBase)
|
|
59
66
|
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const DEFAULT_API_BASE = 'http://127.0.0.1:55667';
|
|
2
|
+
|
|
3
|
+
export const API_BASE = (() => {
|
|
4
|
+
const envBase = import.meta.env.VITE_API_BASE?.trim();
|
|
5
|
+
if (envBase) {
|
|
6
|
+
return envBase.replace(/\/$/, '');
|
|
7
|
+
}
|
|
8
|
+
if (typeof window !== 'undefined' && window.location?.origin) {
|
|
9
|
+
return window.location.origin;
|
|
10
|
+
}
|
|
11
|
+
return DEFAULT_API_BASE;
|
|
12
|
+
})();
|
|
13
|
+
|
|
14
|
+
if (import.meta.env.DEV && !import.meta.env.VITE_API_BASE) {
|
|
15
|
+
console.warn('VITE_API_BASE is not set; falling back to window origin.');
|
|
16
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { api, requestApiResponse } from '@/api/client';
|
|
2
|
+
|
|
3
|
+
const mocks = vi.hoisted(() => ({
|
|
4
|
+
request: vi.fn()
|
|
5
|
+
}));
|
|
6
|
+
|
|
7
|
+
vi.mock('@/transport', () => ({
|
|
8
|
+
appClient: {
|
|
9
|
+
request: mocks.request
|
|
10
|
+
}
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
describe('api/client', () => {
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
mocks.request.mockReset();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('routes GET requests through appClient.request', async () => {
|
|
19
|
+
mocks.request.mockResolvedValue({ ok: true });
|
|
20
|
+
|
|
21
|
+
const response = await api.get<{ ok: boolean }>('/api/config');
|
|
22
|
+
|
|
23
|
+
expect(mocks.request).toHaveBeenCalledWith({
|
|
24
|
+
method: 'GET',
|
|
25
|
+
path: '/api/config'
|
|
26
|
+
});
|
|
27
|
+
expect(response).toEqual({
|
|
28
|
+
ok: true,
|
|
29
|
+
data: { ok: true }
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('parses JSON request bodies before sending to appClient.request', async () => {
|
|
34
|
+
mocks.request.mockResolvedValue({ success: true });
|
|
35
|
+
|
|
36
|
+
const response = await requestApiResponse<{ success: boolean }>('/api/auth/login', {
|
|
37
|
+
method: 'POST',
|
|
38
|
+
body: JSON.stringify({ password: 'secret' })
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
expect(mocks.request).toHaveBeenCalledWith({
|
|
42
|
+
method: 'POST',
|
|
43
|
+
path: '/api/auth/login',
|
|
44
|
+
body: { password: 'secret' }
|
|
45
|
+
});
|
|
46
|
+
expect(response).toEqual({
|
|
47
|
+
ok: true,
|
|
48
|
+
data: { success: true }
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('wraps transport failures as ApiResponse errors', async () => {
|
|
53
|
+
mocks.request.mockRejectedValue(new Error('Invalid token'));
|
|
54
|
+
|
|
55
|
+
const response = await api.get('/api/auth/status');
|
|
56
|
+
|
|
57
|
+
expect(response).toEqual({
|
|
58
|
+
ok: false,
|
|
59
|
+
error: {
|
|
60
|
+
code: 'REQUEST_FAILED',
|
|
61
|
+
message: 'Invalid token',
|
|
62
|
+
details: {
|
|
63
|
+
method: 'GET',
|
|
64
|
+
endpoint: '/api/auth/status'
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
});
|
package/src/api/client.ts
CHANGED
|
@@ -1,106 +1,34 @@
|
|
|
1
|
+
import { appClient } from '@/transport';
|
|
1
2
|
import type { ApiResponse } from './types';
|
|
2
3
|
|
|
3
|
-
const DEFAULT_API_BASE = 'http://127.0.0.1:55667';
|
|
4
|
-
const API_BASE = (() => {
|
|
5
|
-
const envBase = import.meta.env.VITE_API_BASE?.trim();
|
|
6
|
-
if (envBase) {
|
|
7
|
-
return envBase.replace(/\/$/, '');
|
|
8
|
-
}
|
|
9
|
-
if (typeof window !== 'undefined' && window.location?.origin) {
|
|
10
|
-
return window.location.origin;
|
|
11
|
-
}
|
|
12
|
-
return DEFAULT_API_BASE;
|
|
13
|
-
})();
|
|
14
|
-
|
|
15
|
-
if (import.meta.env.DEV && !import.meta.env.VITE_API_BASE) {
|
|
16
|
-
console.warn('VITE_API_BASE is not set; falling back to window origin.');
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export { API_BASE };
|
|
20
|
-
|
|
21
|
-
function compactSnippet(text: string): string {
|
|
22
|
-
return text.replace(/\s+/g, ' ').trim().slice(0, 200);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function inferNonJsonHint(endpoint: string, status: number): string | undefined {
|
|
26
|
-
if (
|
|
27
|
-
status === 404 &&
|
|
28
|
-
endpoint.startsWith('/api/config/providers/') &&
|
|
29
|
-
endpoint.endsWith('/test')
|
|
30
|
-
) {
|
|
31
|
-
return 'Provider test endpoint is missing. This usually means nextclaw runtime version is outdated.';
|
|
32
|
-
}
|
|
33
|
-
if (status === 401 || status === 403) {
|
|
34
|
-
return 'Authentication failed. Check apiKey and custom headers.';
|
|
35
|
-
}
|
|
36
|
-
if (status === 429) {
|
|
37
|
-
return 'Rate limited by upstream provider. Retry later or switch model/provider.';
|
|
38
|
-
}
|
|
39
|
-
if (status >= 500) {
|
|
40
|
-
return 'Upstream service error. Retry later and inspect server logs if it persists.';
|
|
41
|
-
}
|
|
42
|
-
return undefined;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
4
|
export async function requestApiResponse<T>(
|
|
46
5
|
endpoint: string,
|
|
47
6
|
options: RequestInit = {}
|
|
48
7
|
): Promise<ApiResponse<T>> {
|
|
49
|
-
const url = `${API_BASE}${endpoint}`;
|
|
50
8
|
const method = (options.method || 'GET').toUpperCase();
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
let data: ApiResponse<T> | null = null;
|
|
63
|
-
if (text) {
|
|
64
|
-
try {
|
|
65
|
-
data = JSON.parse(text) as ApiResponse<T>;
|
|
66
|
-
} catch {
|
|
67
|
-
// fall through to build a synthetic error response
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (!data) {
|
|
72
|
-
const snippet = text ? compactSnippet(text) : '';
|
|
73
|
-
const hint = inferNonJsonHint(endpoint, response.status);
|
|
74
|
-
const parts = [`Non-JSON response (${response.status} ${response.statusText}) on ${method} ${endpoint}`];
|
|
75
|
-
if (snippet) {
|
|
76
|
-
parts.push(`body=${snippet}`);
|
|
77
|
-
}
|
|
78
|
-
if (hint) {
|
|
79
|
-
parts.push(`hint=${hint}`);
|
|
80
|
-
}
|
|
9
|
+
try {
|
|
10
|
+
const data = await appClient.request<T>({
|
|
11
|
+
method: method as 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
|
|
12
|
+
path: endpoint,
|
|
13
|
+
...(options.body !== undefined ? { body: parseRequestBody(options.body) } : {})
|
|
14
|
+
});
|
|
15
|
+
return {
|
|
16
|
+
ok: true,
|
|
17
|
+
data
|
|
18
|
+
};
|
|
19
|
+
} catch (error) {
|
|
81
20
|
return {
|
|
82
21
|
ok: false,
|
|
83
22
|
error: {
|
|
84
|
-
code: '
|
|
85
|
-
message:
|
|
23
|
+
code: 'REQUEST_FAILED',
|
|
24
|
+
message: error instanceof Error ? error.message : String(error),
|
|
86
25
|
details: {
|
|
87
|
-
status: response.status,
|
|
88
|
-
statusText: response.statusText,
|
|
89
26
|
method,
|
|
90
|
-
endpoint
|
|
91
|
-
url,
|
|
92
|
-
bodySnippet: snippet || undefined,
|
|
93
|
-
hint
|
|
27
|
+
endpoint
|
|
94
28
|
}
|
|
95
29
|
}
|
|
96
30
|
};
|
|
97
31
|
}
|
|
98
|
-
|
|
99
|
-
if (!response.ok) {
|
|
100
|
-
return data as ApiResponse<T>;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return data as ApiResponse<T>;
|
|
104
32
|
}
|
|
105
33
|
|
|
106
34
|
export const api = {
|
|
@@ -120,3 +48,17 @@ export const api = {
|
|
|
120
48
|
method: 'DELETE'
|
|
121
49
|
})
|
|
122
50
|
};
|
|
51
|
+
|
|
52
|
+
function parseRequestBody(body: BodyInit | null | undefined): unknown {
|
|
53
|
+
if (body === undefined || body === null) {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
if (typeof body === 'string') {
|
|
57
|
+
try {
|
|
58
|
+
return JSON.parse(body);
|
|
59
|
+
} catch {
|
|
60
|
+
return body;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return body;
|
|
64
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { sendChatTurnStream, streamChatRun } from '@/api/config';
|
|
2
|
+
|
|
3
|
+
const mocks = vi.hoisted(() => ({
|
|
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
|
+
}));
|
|
14
|
+
|
|
15
|
+
describe('api/config stream routing', () => {
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
mocks.request.mockReset();
|
|
18
|
+
mocks.openStream.mockReset();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('routes sendChatTurnStream through appClient.openStream', async () => {
|
|
22
|
+
const onReady = vi.fn();
|
|
23
|
+
const onDelta = vi.fn();
|
|
24
|
+
const onSessionEvent = vi.fn();
|
|
25
|
+
|
|
26
|
+
mocks.openStream.mockImplementation(({ onEvent }) => {
|
|
27
|
+
onEvent({ name: 'ready', payload: { sessionKey: 's1' } });
|
|
28
|
+
onEvent({ name: 'delta', payload: { delta: 'hello' } });
|
|
29
|
+
onEvent({ name: 'session_event', payload: { type: 'session.updated' } });
|
|
30
|
+
onEvent({ name: 'final', payload: { sessionKey: 's1', reply: 'hello world' } });
|
|
31
|
+
return {
|
|
32
|
+
finished: Promise.resolve({ sessionKey: 's1', reply: 'hello world' }),
|
|
33
|
+
cancel: vi.fn()
|
|
34
|
+
};
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const result = await sendChatTurnStream(
|
|
38
|
+
{ message: 'hi' } as never,
|
|
39
|
+
{ onReady, onDelta, onSessionEvent }
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
expect(mocks.openStream).toHaveBeenCalledWith({
|
|
43
|
+
method: 'POST',
|
|
44
|
+
path: '/api/chat/turn/stream',
|
|
45
|
+
body: { message: 'hi' },
|
|
46
|
+
signal: undefined,
|
|
47
|
+
onEvent: expect.any(Function)
|
|
48
|
+
});
|
|
49
|
+
expect(onReady).toHaveBeenCalledWith({ sessionKey: 's1' });
|
|
50
|
+
expect(onDelta).toHaveBeenCalledWith({ delta: 'hello' });
|
|
51
|
+
expect(onSessionEvent).toHaveBeenCalledWith({ data: { type: 'session.updated' } });
|
|
52
|
+
expect(result).toEqual({ sessionKey: 's1', reply: 'hello world' });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('routes streamChatRun through appClient.openStream and preserves query params', async () => {
|
|
56
|
+
const onReady = vi.fn();
|
|
57
|
+
const onDelta = vi.fn();
|
|
58
|
+
const onSessionEvent = vi.fn();
|
|
59
|
+
|
|
60
|
+
mocks.openStream.mockImplementation(() => ({
|
|
61
|
+
finished: Promise.resolve({ sessionKey: 's1', reply: 'resumed' }),
|
|
62
|
+
cancel: vi.fn()
|
|
63
|
+
}));
|
|
64
|
+
|
|
65
|
+
const result = await streamChatRun(
|
|
66
|
+
{ runId: 'run-1', fromEventIndex: 42 },
|
|
67
|
+
{ onReady, onDelta, onSessionEvent }
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
expect(mocks.openStream).toHaveBeenCalledWith({
|
|
71
|
+
method: 'GET',
|
|
72
|
+
path: '/api/chat/runs/run-1/stream?fromEventIndex=42',
|
|
73
|
+
signal: undefined,
|
|
74
|
+
onEvent: expect.any(Function)
|
|
75
|
+
});
|
|
76
|
+
expect(onReady).not.toHaveBeenCalled();
|
|
77
|
+
expect(onDelta).not.toHaveBeenCalled();
|
|
78
|
+
expect(onSessionEvent).not.toHaveBeenCalled();
|
|
79
|
+
expect(result).toEqual({ sessionKey: 's1', reply: 'resumed' });
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('surfaces transport error events as rejected stream promises', async () => {
|
|
83
|
+
mocks.openStream.mockImplementation(({ onEvent }) => {
|
|
84
|
+
let resolveFinished!: () => void;
|
|
85
|
+
let rejectFinished!: (error: Error) => void;
|
|
86
|
+
const finished = new Promise<void>((resolve, reject) => {
|
|
87
|
+
resolveFinished = resolve;
|
|
88
|
+
rejectFinished = reject;
|
|
89
|
+
});
|
|
90
|
+
queueMicrotask(() => {
|
|
91
|
+
try {
|
|
92
|
+
onEvent({ name: 'error', payload: { message: 'chat stream failed' } });
|
|
93
|
+
resolveFinished();
|
|
94
|
+
} catch (error) {
|
|
95
|
+
rejectFinished(error instanceof Error ? error : new Error(String(error)));
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
return {
|
|
99
|
+
finished,
|
|
100
|
+
cancel: vi.fn()
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
await expect(
|
|
105
|
+
sendChatTurnStream(
|
|
106
|
+
{ message: 'hi' } as never,
|
|
107
|
+
{
|
|
108
|
+
onReady: vi.fn(),
|
|
109
|
+
onDelta: vi.fn(),
|
|
110
|
+
onSessionEvent: vi.fn()
|
|
111
|
+
}
|
|
112
|
+
)
|
|
113
|
+
).rejects.toThrow('chat stream failed');
|
|
114
|
+
});
|
|
115
|
+
});
|
package/src/api/config.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { api
|
|
1
|
+
import { api } from './client';
|
|
2
|
+
import { appClient } from '@/transport';
|
|
2
3
|
import type {
|
|
3
4
|
AuthEnabledUpdateRequest,
|
|
4
5
|
AuthLoginRequest,
|
|
@@ -376,32 +377,6 @@ export async function sendChatTurn(data: ChatTurnRequest): Promise<ChatTurnView>
|
|
|
376
377
|
return response.data;
|
|
377
378
|
}
|
|
378
379
|
|
|
379
|
-
function parseSseFrame(frame: string): { event: string; data: string } | null {
|
|
380
|
-
const lines = frame.split('\n');
|
|
381
|
-
let event = '';
|
|
382
|
-
const dataLines: string[] = [];
|
|
383
|
-
for (const raw of lines) {
|
|
384
|
-
const line = raw.trimEnd();
|
|
385
|
-
if (!line || line.startsWith(':')) {
|
|
386
|
-
continue;
|
|
387
|
-
}
|
|
388
|
-
if (line.startsWith('event:')) {
|
|
389
|
-
event = line.slice(6).trim();
|
|
390
|
-
continue;
|
|
391
|
-
}
|
|
392
|
-
if (line.startsWith('data:')) {
|
|
393
|
-
dataLines.push(line.slice(5).trimStart());
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
if (!event) {
|
|
397
|
-
return null;
|
|
398
|
-
}
|
|
399
|
-
return {
|
|
400
|
-
event,
|
|
401
|
-
data: dataLines.join('\n')
|
|
402
|
-
};
|
|
403
|
-
}
|
|
404
|
-
|
|
405
380
|
async function readSseStream(params: {
|
|
406
381
|
path: string;
|
|
407
382
|
method: 'GET' | 'POST';
|
|
@@ -411,115 +386,68 @@ async function readSseStream(params: {
|
|
|
411
386
|
onDelta: (event: ChatTurnStreamDeltaEvent) => void;
|
|
412
387
|
onSessionEvent: (event: ChatTurnStreamSessionEvent) => void;
|
|
413
388
|
}): Promise<{ sessionKey: string; reply: string }> {
|
|
414
|
-
const response = await fetch(`${API_BASE}${params.path}`, {
|
|
415
|
-
method: params.method,
|
|
416
|
-
credentials: 'include',
|
|
417
|
-
headers: {
|
|
418
|
-
'Content-Type': 'application/json',
|
|
419
|
-
Accept: 'text/event-stream'
|
|
420
|
-
},
|
|
421
|
-
...(params.body !== undefined ? { body: JSON.stringify(params.body) } : {}),
|
|
422
|
-
...(params.signal ? { signal: params.signal } : {})
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
if (!response.ok) {
|
|
426
|
-
const text = await response.text();
|
|
427
|
-
const fallback = `HTTP ${response.status}`;
|
|
428
|
-
const trimmed = text.trim();
|
|
429
|
-
throw new Error(trimmed || fallback);
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
const reader = response.body?.getReader();
|
|
433
|
-
if (!reader) {
|
|
434
|
-
throw new Error('SSE response body unavailable');
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
const decoder = new TextDecoder();
|
|
438
|
-
let buffer = '';
|
|
439
389
|
let finalResult: { sessionKey: string; reply: string } | null = null;
|
|
440
|
-
let readySessionKey
|
|
441
|
-
|
|
442
|
-
const consumeFrame = (frame: string) => {
|
|
443
|
-
const parsed = parseSseFrame(frame);
|
|
444
|
-
if (!parsed) {
|
|
445
|
-
return;
|
|
446
|
-
}
|
|
390
|
+
let readySessionKey = '';
|
|
447
391
|
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
392
|
+
const session = appClient.openStream<ChatTurnView>({
|
|
393
|
+
method: params.method,
|
|
394
|
+
path: params.path,
|
|
395
|
+
...(params.body !== undefined ? { body: params.body } : {}),
|
|
396
|
+
signal: params.signal,
|
|
397
|
+
onEvent: (event) => {
|
|
398
|
+
if (event.name === 'ready') {
|
|
399
|
+
const ready = (event.payload ?? {}) as ChatTurnStreamReadyEvent;
|
|
400
|
+
if (typeof ready.sessionKey === 'string' && ready.sessionKey.trim()) {
|
|
401
|
+
readySessionKey = ready.sessionKey;
|
|
402
|
+
}
|
|
403
|
+
params.onReady(ready);
|
|
404
|
+
return;
|
|
454
405
|
}
|
|
455
|
-
}
|
|
456
406
|
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
return;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
if (parsed.event === 'delta') {
|
|
465
|
-
params.onDelta((payload ?? { delta: '' }) as ChatTurnStreamDeltaEvent);
|
|
466
|
-
return;
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
if (parsed.event === 'session_event') {
|
|
470
|
-
params.onSessionEvent({ data: payload as ChatTurnStreamSessionEvent['data'] });
|
|
471
|
-
return;
|
|
472
|
-
}
|
|
407
|
+
if (event.name === 'delta') {
|
|
408
|
+
params.onDelta((event.payload ?? { delta: '' }) as ChatTurnStreamDeltaEvent);
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
473
411
|
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
? result.sessionKey
|
|
479
|
-
: (readySessionKey ?? ''),
|
|
480
|
-
reply: typeof result?.reply === 'string' ? result.reply : ''
|
|
481
|
-
};
|
|
482
|
-
return;
|
|
483
|
-
}
|
|
412
|
+
if (event.name === 'session_event') {
|
|
413
|
+
params.onSessionEvent({ data: event.payload as ChatTurnStreamSessionEvent['data'] });
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
484
416
|
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
const { value, done } = await reader.read();
|
|
495
|
-
if (done) {
|
|
496
|
-
isReading = false;
|
|
497
|
-
continue;
|
|
417
|
+
if (event.name === 'final') {
|
|
418
|
+
const result = event.payload as ChatTurnView;
|
|
419
|
+
finalResult = {
|
|
420
|
+
sessionKey: typeof result?.sessionKey === 'string' && result.sessionKey.trim()
|
|
421
|
+
? result.sessionKey
|
|
422
|
+
: readySessionKey,
|
|
423
|
+
reply: typeof result?.reply === 'string' ? result.reply : ''
|
|
424
|
+
};
|
|
425
|
+
return;
|
|
498
426
|
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
buffer = buffer.slice(boundary + 2);
|
|
504
|
-
consumeFrame(frame);
|
|
505
|
-
boundary = buffer.indexOf('\n\n');
|
|
427
|
+
|
|
428
|
+
if (event.name === 'error') {
|
|
429
|
+
const errorPayload = (event.payload ?? {}) as ChatTurnStreamErrorEvent;
|
|
430
|
+
throw new Error((errorPayload.message ?? '').trim() || 'chat stream failed');
|
|
506
431
|
}
|
|
507
432
|
}
|
|
508
|
-
|
|
509
|
-
consumeFrame(buffer);
|
|
510
|
-
}
|
|
511
|
-
} finally {
|
|
512
|
-
reader.releaseLock();
|
|
513
|
-
}
|
|
433
|
+
});
|
|
514
434
|
|
|
435
|
+
const result = await session.finished;
|
|
515
436
|
if (finalResult) {
|
|
516
437
|
return finalResult;
|
|
517
438
|
}
|
|
518
|
-
|
|
519
439
|
if (readySessionKey) {
|
|
520
|
-
return {
|
|
440
|
+
return {
|
|
441
|
+
sessionKey: readySessionKey,
|
|
442
|
+
reply: typeof result?.reply === 'string' ? result.reply : ''
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
if (typeof result?.sessionKey === 'string' && result.sessionKey.trim()) {
|
|
446
|
+
return {
|
|
447
|
+
sessionKey: result.sessionKey,
|
|
448
|
+
reply: typeof result?.reply === 'string' ? result.reply : ''
|
|
449
|
+
};
|
|
521
450
|
}
|
|
522
|
-
|
|
523
451
|
throw new Error('chat stream ended without final event');
|
|
524
452
|
}
|
|
525
453
|
|