@shellui/core 0.7.0 → 0.8.0
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/LICENSE +21 -0
- package/README.md +2 -0
- package/package.json +2 -2
- package/schemas/shellui.config.schema.json +45 -2
- package/src/app.tsx +2 -0
- package/src/features/auth/AuthProvider.tsx +212 -97
- package/src/features/auth/appBackend/configSchema.spec.ts +1 -0
- package/src/features/auth/appBackend/frameAuth.ts +5 -0
- package/src/features/auth/backends/shellui.spec.ts +110 -0
- package/src/features/auth/backends/shellui.ts +47 -20
- package/src/features/auth/backends/types.ts +5 -0
- package/src/features/auth/hooks/useAuth.tsx +6 -1
- package/src/features/auth/types.ts +5 -0
- package/src/features/auth/utils/bffAuthClient.spec.ts +313 -0
- package/src/features/auth/utils/bffAuthClient.ts +171 -49
- package/src/features/auth/utils/buildSessionFromParams.ts +5 -2
- package/src/features/auth/utils/buildSessionFromTokenPayload.spec.ts +12 -0
- package/src/features/auth/utils/buildSessionFromTokenPayload.ts +3 -1
- package/src/features/auth/utils/canRestoreStoredSession.spec.ts +140 -4
- package/src/features/auth/utils/canRestoreStoredSession.ts +47 -0
- package/src/features/auth/utils/cliCallback.ts +5 -0
- package/src/features/auth/utils/identityBrowserLogout.spec.ts +185 -0
- package/src/features/auth/utils/identityBrowserLogout.ts +174 -0
- package/src/features/auth/utils/index.ts +21 -3
- package/src/features/auth/utils/isBffAuthEnabled.ts +10 -2
- package/src/features/auth/utils/normalizeAuthSettings.spec.ts +9 -0
- package/src/features/auth/utils/normalizeAuthSettings.ts +3 -1
- package/src/features/auth/utils/pkce.spec.ts +29 -1
- package/src/features/auth/utils/pkce.ts +106 -2
- package/src/features/config/types.ts +37 -5
- package/src/features/mcp/mcpActivityErrorCodeI18n.spec.ts +32 -0
- package/src/features/mcp/mcpActivityErrorCodeI18n.ts +30 -0
- package/src/features/mcp/mcpAgentActivityMerge.spec.ts +91 -0
- package/src/features/mcp/mcpAgentActivityMerge.ts +53 -0
- package/src/features/mcp/mcpAgentActivityPresentation.spec.ts +129 -0
- package/src/features/mcp/mcpAgentActivityPresentation.ts +201 -0
- package/src/features/mcp/mcpAgentsConnect.spec.ts +215 -0
- package/src/features/mcp/mcpAgentsConnect.ts +189 -0
- package/src/features/mcp/mcpToolActivityRegistry.spec.ts +9 -0
- package/src/features/mcp/mcpToolActivityRegistry.ts +20 -0
- package/src/features/mcp/mcpToolApprovalsApi.spec.ts +113 -0
- package/src/features/mcp/mcpToolApprovalsApi.ts +162 -0
- package/src/features/mcp/personalAgentsApi.spec.ts +661 -0
- package/src/features/mcp/personalAgentsApi.ts +697 -0
- package/src/features/settings/SettingsIcons.tsx +28 -0
- package/src/features/settings/SettingsRoutes.tsx +8 -0
- package/src/features/settings/SettingsView.tsx +90 -26
- package/src/features/settings/components/McpAgentActivitySection.spec.tsx +255 -0
- package/src/features/settings/components/McpAgentActivitySection.tsx +481 -0
- package/src/features/settings/components/McpAgents.spec.tsx +336 -0
- package/src/features/settings/components/McpAgents.tsx +654 -0
- package/src/features/settings/components/McpPendingApprovals.spec.tsx +201 -0
- package/src/features/settings/components/McpPendingApprovals.tsx +168 -0
- package/src/features/settings/components/McpPersonalStatusPrefetch.tsx +21 -0
- package/src/features/settings/hooks/useMcpPendingToolApprovals.ts +69 -0
- package/src/features/settings/hooks/useMcpPersonalStatus.ts +83 -0
- package/src/features/settings/utils/buildSettingsForPropagation.spec.ts +44 -1
- package/src/features/settings/utils/buildSettingsForPropagation.ts +30 -1
- package/src/features/settings/utils/isMcpAgentsSettingsEligible.spec.ts +25 -0
- package/src/features/settings/utils/isMcpAgentsSettingsEligible.ts +14 -0
- package/src/features/settings/utils/loadMcpPersonalStatus.spec.ts +76 -0
- package/src/features/settings/utils/loadMcpPersonalStatus.ts +84 -0
- package/src/features/settings/utils/mcpPersonalStatusCache.spec.ts +77 -0
- package/src/features/settings/utils/mcpPersonalStatusCache.ts +95 -0
- package/src/features/settings/utils/shouldShowMcpAgentsSettings.spec.ts +66 -0
- package/src/features/settings/utils/shouldShowMcpAgentsSettings.ts +16 -0
- package/src/i18n/translations/en/settings.json +201 -0
- package/src/i18n/translations/fr/settings.json +201 -0
- package/src/index.ts +1 -0
- package/src/service-worker/sw.ts +4 -2
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { act } from 'react';
|
|
3
|
+
import { createRoot, type Root } from 'react-dom/client';
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
5
|
+
import en from '../../../i18n/translations/en/settings.json';
|
|
6
|
+
import { McpPendingApprovals } from './McpPendingApprovals';
|
|
7
|
+
|
|
8
|
+
const api = vi.hoisted(() => ({
|
|
9
|
+
fetchMcpPendingToolApprovals: vi.fn(),
|
|
10
|
+
decideMcpToolApproval: vi.fn(),
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
vi.mock('../../mcp/mcpToolApprovalsApi', async (importOriginal) => {
|
|
14
|
+
const actual = await importOriginal<typeof import('../../mcp/mcpToolApprovalsApi')>();
|
|
15
|
+
return {
|
|
16
|
+
...actual,
|
|
17
|
+
fetchMcpPendingToolApprovals: api.fetchMcpPendingToolApprovals,
|
|
18
|
+
decideMcpToolApproval: api.decideMcpToolApproval,
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
vi.mock('../../config/useConfig', () => ({
|
|
23
|
+
useConfig: () => ({ config: { title: 'Acme' } }),
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
vi.mock('react-i18next', () => ({
|
|
27
|
+
useTranslation: () => ({
|
|
28
|
+
t: (key: string, opts?: Record<string, string | number>) => {
|
|
29
|
+
const parts = key.replace('mcpAgents.', '').split('.');
|
|
30
|
+
let raw: unknown = en.mcpAgents;
|
|
31
|
+
for (const part of parts) {
|
|
32
|
+
if (raw && typeof raw === 'object' && part in raw) {
|
|
33
|
+
raw = (raw as Record<string, unknown>)[part];
|
|
34
|
+
} else {
|
|
35
|
+
raw = key;
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const text = typeof raw === 'string' ? raw : key;
|
|
40
|
+
return text.replace(/\{\{(\w+)\}\}/g, (_, k: string) => String(opts?.[k] ?? ''));
|
|
41
|
+
},
|
|
42
|
+
}),
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
const dialogMock = vi.fn();
|
|
46
|
+
const toastMock = vi.fn();
|
|
47
|
+
|
|
48
|
+
vi.mock('@shellui/sdk', () => ({
|
|
49
|
+
shellui: {
|
|
50
|
+
dialog: (...args: unknown[]) => dialogMock(...args),
|
|
51
|
+
toast: (...args: unknown[]) => toastMock(...args),
|
|
52
|
+
},
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
|
|
56
|
+
|
|
57
|
+
let container: HTMLDivElement;
|
|
58
|
+
let root: Root;
|
|
59
|
+
|
|
60
|
+
const futureExpiry = () => new Date(Date.now() + 5 * 60_000).toISOString();
|
|
61
|
+
|
|
62
|
+
beforeEach(() => {
|
|
63
|
+
container = document.createElement('div');
|
|
64
|
+
document.body.appendChild(container);
|
|
65
|
+
root = createRoot(container);
|
|
66
|
+
dialogMock.mockReset();
|
|
67
|
+
toastMock.mockReset();
|
|
68
|
+
api.fetchMcpPendingToolApprovals.mockReset();
|
|
69
|
+
api.decideMcpToolApproval.mockReset();
|
|
70
|
+
vi.useFakeTimers();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
afterEach(() => {
|
|
74
|
+
act(() => root.unmount());
|
|
75
|
+
container.remove();
|
|
76
|
+
vi.useRealTimers();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
async function renderPanelWithApprovals(
|
|
80
|
+
approvals: {
|
|
81
|
+
id: string;
|
|
82
|
+
toolName: string;
|
|
83
|
+
agentName: string;
|
|
84
|
+
expiresAt: string;
|
|
85
|
+
shortActionSummary: string | null;
|
|
86
|
+
isDestructive: boolean;
|
|
87
|
+
}[],
|
|
88
|
+
) {
|
|
89
|
+
act(() => {
|
|
90
|
+
root.render(
|
|
91
|
+
<McpPendingApprovals
|
|
92
|
+
backendUrl="https://id.example.com"
|
|
93
|
+
accessToken="tok"
|
|
94
|
+
companyId="1"
|
|
95
|
+
enabled
|
|
96
|
+
companyName="Acme"
|
|
97
|
+
approvals={approvals}
|
|
98
|
+
pendingCount={approvals.length ? 1 : 0}
|
|
99
|
+
onRefresh={async () => {}}
|
|
100
|
+
/>,
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
await act(async () => {
|
|
104
|
+
await Promise.resolve();
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
describe('McpPendingApprovals', () => {
|
|
109
|
+
it('renders pending rows with Wilson copy', async () => {
|
|
110
|
+
await renderPanelWithApprovals([
|
|
111
|
+
{
|
|
112
|
+
id: 'ap-1',
|
|
113
|
+
toolName: 'storage_delete',
|
|
114
|
+
agentName: 'ChatGPT on my laptop',
|
|
115
|
+
expiresAt: futureExpiry(),
|
|
116
|
+
shortActionSummary: 'Delete report.pdf',
|
|
117
|
+
isDestructive: true,
|
|
118
|
+
},
|
|
119
|
+
]);
|
|
120
|
+
|
|
121
|
+
expect(container.textContent).toContain('Pending approvals');
|
|
122
|
+
expect(container.textContent).toContain('storage_delete');
|
|
123
|
+
expect(container.textContent).toContain('ChatGPT on my laptop');
|
|
124
|
+
expect(container.textContent).toContain('Expires in');
|
|
125
|
+
expect(container.textContent).toContain('Delete report.pdf');
|
|
126
|
+
expect(container.textContent).toContain('Can change data');
|
|
127
|
+
expect(container.textContent).toContain('Approve');
|
|
128
|
+
expect(container.textContent).toContain('Deny');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('denies without a confirm dialog', async () => {
|
|
132
|
+
api.decideMcpToolApproval.mockResolvedValue(undefined);
|
|
133
|
+
|
|
134
|
+
await renderPanelWithApprovals([
|
|
135
|
+
{
|
|
136
|
+
id: 'ap-2',
|
|
137
|
+
toolName: 'identity_list_users',
|
|
138
|
+
agentName: 'Agent',
|
|
139
|
+
expiresAt: futureExpiry(),
|
|
140
|
+
shortActionSummary: null,
|
|
141
|
+
isDestructive: false,
|
|
142
|
+
},
|
|
143
|
+
]);
|
|
144
|
+
|
|
145
|
+
const denyBtn = Array.from(container.querySelectorAll('button')).find(
|
|
146
|
+
(btn) => btn.textContent === 'Deny',
|
|
147
|
+
);
|
|
148
|
+
expect(denyBtn).toBeTruthy();
|
|
149
|
+
await act(async () => {
|
|
150
|
+
denyBtn?.click();
|
|
151
|
+
await Promise.resolve();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
expect(dialogMock).not.toHaveBeenCalled();
|
|
155
|
+
expect(api.decideMcpToolApproval).toHaveBeenCalledWith(
|
|
156
|
+
'https://id.example.com',
|
|
157
|
+
'tok',
|
|
158
|
+
'1',
|
|
159
|
+
'ap-2',
|
|
160
|
+
'deny',
|
|
161
|
+
);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('opens destructive approve confirm', async () => {
|
|
165
|
+
await renderPanelWithApprovals([
|
|
166
|
+
{
|
|
167
|
+
id: 'ap-3',
|
|
168
|
+
toolName: 'hosting_deploy',
|
|
169
|
+
agentName: 'Cursor',
|
|
170
|
+
expiresAt: futureExpiry(),
|
|
171
|
+
shortActionSummary: '',
|
|
172
|
+
isDestructive: true,
|
|
173
|
+
},
|
|
174
|
+
]);
|
|
175
|
+
|
|
176
|
+
const approveBtn = Array.from(container.querySelectorAll('button')).find(
|
|
177
|
+
(btn) => btn.textContent === 'Approve',
|
|
178
|
+
);
|
|
179
|
+
await act(async () => {
|
|
180
|
+
approveBtn?.click();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
expect(dialogMock).toHaveBeenCalledOnce();
|
|
184
|
+
const dialogArgs = dialogMock.mock.calls[0]?.[0] as {
|
|
185
|
+
title?: string;
|
|
186
|
+
description?: string;
|
|
187
|
+
okLabel?: string;
|
|
188
|
+
cancelLabel?: string;
|
|
189
|
+
};
|
|
190
|
+
expect(dialogArgs.title).toBe('Approve hosting_deploy?');
|
|
191
|
+
expect(dialogArgs.description).toBe('This can change data in Acme.');
|
|
192
|
+
expect(dialogArgs.okLabel).toBe('Approve');
|
|
193
|
+
expect(dialogArgs.cancelLabel).toBe('Cancel');
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('hides the section when there are no pending items', async () => {
|
|
197
|
+
await renderPanelWithApprovals([]);
|
|
198
|
+
|
|
199
|
+
expect(container.textContent).not.toContain('Pending approvals');
|
|
200
|
+
});
|
|
201
|
+
});
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { forwardRef, useCallback, useState } from 'react';
|
|
2
|
+
import { useTranslation } from 'react-i18next';
|
|
3
|
+
import { shellui } from '@shellui/sdk';
|
|
4
|
+
import { Badge } from '../../../components/ui/badge';
|
|
5
|
+
import { Button } from '../../../components/ui/button';
|
|
6
|
+
import {
|
|
7
|
+
decideMcpToolApproval,
|
|
8
|
+
mcpApprovalExpiresInMinutes,
|
|
9
|
+
type McpPendingToolApproval,
|
|
10
|
+
} from '../../mcp/mcpToolApprovalsApi';
|
|
11
|
+
|
|
12
|
+
type Props = {
|
|
13
|
+
backendUrl: string;
|
|
14
|
+
accessToken: string;
|
|
15
|
+
companyId: string;
|
|
16
|
+
enabled: boolean;
|
|
17
|
+
companyName: string;
|
|
18
|
+
approvals: McpPendingToolApproval[];
|
|
19
|
+
pendingCount: number;
|
|
20
|
+
onRefresh: () => Promise<void>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const McpPendingApprovals = forwardRef<HTMLElement, Props>(function McpPendingApprovals(
|
|
24
|
+
{ backendUrl, accessToken, companyId, enabled, companyName, approvals, pendingCount, onRefresh },
|
|
25
|
+
ref,
|
|
26
|
+
) {
|
|
27
|
+
const { t } = useTranslation('settings');
|
|
28
|
+
|
|
29
|
+
const [decidingId, setDecidingId] = useState<string | null>(null);
|
|
30
|
+
|
|
31
|
+
const runDecide = useCallback(
|
|
32
|
+
async (approval: McpPendingToolApproval, decision: 'approve' | 'deny') => {
|
|
33
|
+
setDecidingId(approval.id);
|
|
34
|
+
try {
|
|
35
|
+
await decideMcpToolApproval(backendUrl, accessToken, companyId, approval.id, decision);
|
|
36
|
+
shellui.toast({
|
|
37
|
+
type: 'success',
|
|
38
|
+
title:
|
|
39
|
+
decision === 'approve'
|
|
40
|
+
? t('mcpAgents.pendingApprovals.toast.approvedTitle')
|
|
41
|
+
: t('mcpAgents.pendingApprovals.toast.deniedTitle'),
|
|
42
|
+
description: t('mcpAgents.pendingApprovals.toast.description', {
|
|
43
|
+
toolName: approval.toolName,
|
|
44
|
+
}),
|
|
45
|
+
});
|
|
46
|
+
await onRefresh();
|
|
47
|
+
} catch {
|
|
48
|
+
shellui.toast({
|
|
49
|
+
type: 'error',
|
|
50
|
+
title: t('mcpAgents.pendingApprovals.toast.errorTitle'),
|
|
51
|
+
});
|
|
52
|
+
} finally {
|
|
53
|
+
setDecidingId(null);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
[accessToken, backendUrl, companyId, onRefresh, t],
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const handleApprove = useCallback(
|
|
60
|
+
(approval: McpPendingToolApproval) => {
|
|
61
|
+
if (approval.isDestructive) {
|
|
62
|
+
shellui.dialog({
|
|
63
|
+
title: t('mcpAgents.pendingApprovals.confirmApprove.title', {
|
|
64
|
+
toolName: approval.toolName,
|
|
65
|
+
}),
|
|
66
|
+
description: t('mcpAgents.pendingApprovals.confirmApprove.description', {
|
|
67
|
+
companyName,
|
|
68
|
+
}),
|
|
69
|
+
mode: 'okCancel',
|
|
70
|
+
size: 'sm',
|
|
71
|
+
okLabel: t('mcpAgents.pendingApprovals.confirmApprove.confirm'),
|
|
72
|
+
cancelLabel: t('mcpAgents.pendingApprovals.confirmApprove.cancel'),
|
|
73
|
+
onOk: async () => {
|
|
74
|
+
await runDecide(approval, 'approve');
|
|
75
|
+
},
|
|
76
|
+
onCancel: () => {},
|
|
77
|
+
});
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
void runDecide(approval, 'approve');
|
|
81
|
+
},
|
|
82
|
+
[companyName, runDecide, t],
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
if (!enabled || pendingCount <= 0 || approvals.length === 0) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const countLabel = String(pendingCount);
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<section
|
|
93
|
+
ref={ref}
|
|
94
|
+
className="space-y-4 scroll-mt-8"
|
|
95
|
+
>
|
|
96
|
+
<div className="flex flex-wrap items-center gap-2">
|
|
97
|
+
<h2
|
|
98
|
+
className="text-sm font-medium leading-none"
|
|
99
|
+
style={{ fontFamily: 'var(--heading-font-family, inherit)' }}
|
|
100
|
+
>
|
|
101
|
+
{t('mcpAgents.pendingApprovals.title')}
|
|
102
|
+
</h2>
|
|
103
|
+
<Badge
|
|
104
|
+
variant="muted"
|
|
105
|
+
className="font-normal tabular-nums"
|
|
106
|
+
>
|
|
107
|
+
{countLabel}
|
|
108
|
+
</Badge>
|
|
109
|
+
</div>
|
|
110
|
+
<ul className="space-y-3">
|
|
111
|
+
{approvals.map((approval) => {
|
|
112
|
+
const expiresInMinutes = mcpApprovalExpiresInMinutes(approval.expiresAt);
|
|
113
|
+
const busy = decidingId === approval.id;
|
|
114
|
+
return (
|
|
115
|
+
<li
|
|
116
|
+
key={approval.id}
|
|
117
|
+
className="rounded-lg border bg-card p-4"
|
|
118
|
+
>
|
|
119
|
+
<div className="flex flex-wrap items-start justify-between gap-3">
|
|
120
|
+
<div className="min-w-0 space-y-1">
|
|
121
|
+
<p className="text-sm font-medium font-mono">{approval.toolName}</p>
|
|
122
|
+
<p className="text-sm text-muted-foreground">
|
|
123
|
+
{t('mcpAgents.pendingApprovals.meta', {
|
|
124
|
+
agentName: approval.agentName,
|
|
125
|
+
expiresInMinutes,
|
|
126
|
+
})}
|
|
127
|
+
</p>
|
|
128
|
+
{approval.shortActionSummary ? (
|
|
129
|
+
<p className="text-sm text-muted-foreground">{approval.shortActionSummary}</p>
|
|
130
|
+
) : null}
|
|
131
|
+
{approval.isDestructive ? (
|
|
132
|
+
<Badge
|
|
133
|
+
variant="muted"
|
|
134
|
+
className="mt-1 font-normal"
|
|
135
|
+
>
|
|
136
|
+
{t('mcpAgents.pendingApprovals.destructiveBadge')}
|
|
137
|
+
</Badge>
|
|
138
|
+
) : null}
|
|
139
|
+
</div>
|
|
140
|
+
<div className="flex shrink-0 flex-wrap gap-2">
|
|
141
|
+
<Button
|
|
142
|
+
type="button"
|
|
143
|
+
size="sm"
|
|
144
|
+
disabled={busy}
|
|
145
|
+
onClick={() => handleApprove(approval)}
|
|
146
|
+
>
|
|
147
|
+
{t('mcpAgents.pendingApprovals.approve')}
|
|
148
|
+
</Button>
|
|
149
|
+
<Button
|
|
150
|
+
type="button"
|
|
151
|
+
variant="outline"
|
|
152
|
+
size="sm"
|
|
153
|
+
disabled={busy}
|
|
154
|
+
onClick={() => void runDecide(approval, 'deny')}
|
|
155
|
+
>
|
|
156
|
+
{t('mcpAgents.pendingApprovals.deny')}
|
|
157
|
+
</Button>
|
|
158
|
+
</div>
|
|
159
|
+
</div>
|
|
160
|
+
</li>
|
|
161
|
+
);
|
|
162
|
+
})}
|
|
163
|
+
</ul>
|
|
164
|
+
</section>
|
|
165
|
+
);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
McpPendingApprovals.displayName = 'McpPendingApprovals';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { useAuth } from '../../auth/hooks/useAuth';
|
|
3
|
+
import { useConfig } from '../../config/useConfig';
|
|
4
|
+
import { loadMcpPersonalStatus } from '../utils/loadMcpPersonalStatus';
|
|
5
|
+
|
|
6
|
+
/** Prefetch identity MCP status once after sign-in so Settings nav can render from cache. */
|
|
7
|
+
export const McpPersonalStatusPrefetch = () => {
|
|
8
|
+
const { config } = useConfig();
|
|
9
|
+
const { session, isAuthenticated, user } = useAuth();
|
|
10
|
+
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
void loadMcpPersonalStatus({
|
|
13
|
+
config,
|
|
14
|
+
accessToken: session?.accessToken ?? null,
|
|
15
|
+
isAuthenticated,
|
|
16
|
+
userId: user?.id ?? null,
|
|
17
|
+
});
|
|
18
|
+
}, [config, session?.accessToken, isAuthenticated, user?.id]);
|
|
19
|
+
|
|
20
|
+
return null;
|
|
21
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
fetchMcpPendingToolApprovals,
|
|
4
|
+
mcpApprovalExpiresInMinutes,
|
|
5
|
+
type McpPendingToolApproval,
|
|
6
|
+
} from '../../mcp/mcpToolApprovalsApi';
|
|
7
|
+
|
|
8
|
+
const PENDING_POLL_MS = 5_000;
|
|
9
|
+
|
|
10
|
+
export type McpPendingApprovalsState = {
|
|
11
|
+
approvals: McpPendingToolApproval[];
|
|
12
|
+
pendingCount: number;
|
|
13
|
+
refresh: () => Promise<void>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function useMcpPendingToolApprovals(
|
|
17
|
+
backendUrl: string,
|
|
18
|
+
accessToken: string,
|
|
19
|
+
companyId: string,
|
|
20
|
+
enabled: boolean,
|
|
21
|
+
): McpPendingApprovalsState {
|
|
22
|
+
const [approvals, setApprovals] = useState<McpPendingToolApproval[]>([]);
|
|
23
|
+
const [pendingCount, setPendingCount] = useState(0);
|
|
24
|
+
const mountedRef = useRef(true);
|
|
25
|
+
|
|
26
|
+
const loadPending = useCallback(async () => {
|
|
27
|
+
if (!enabled || !backendUrl || !accessToken) {
|
|
28
|
+
setApprovals([]);
|
|
29
|
+
setPendingCount(0);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const list = await fetchMcpPendingToolApprovals(backendUrl, accessToken, companyId);
|
|
34
|
+
if (!mountedRef.current) return;
|
|
35
|
+
const live = list.approvals.filter((row) => mcpApprovalExpiresInMinutes(row.expiresAt) > 0);
|
|
36
|
+
setApprovals(live);
|
|
37
|
+
setPendingCount(list.pendingCount > 0 ? list.pendingCount : live.length);
|
|
38
|
+
} catch {
|
|
39
|
+
if (!mountedRef.current) return;
|
|
40
|
+
setApprovals([]);
|
|
41
|
+
setPendingCount(0);
|
|
42
|
+
}
|
|
43
|
+
}, [accessToken, backendUrl, companyId, enabled]);
|
|
44
|
+
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
mountedRef.current = true;
|
|
47
|
+
return () => {
|
|
48
|
+
mountedRef.current = false;
|
|
49
|
+
};
|
|
50
|
+
}, []);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
void loadPending();
|
|
54
|
+
if (!enabled) return undefined;
|
|
55
|
+
const timer = window.setInterval(() => {
|
|
56
|
+
void loadPending();
|
|
57
|
+
}, PENDING_POLL_MS);
|
|
58
|
+
return () => window.clearInterval(timer);
|
|
59
|
+
}, [enabled, loadPending]);
|
|
60
|
+
|
|
61
|
+
return useMemo(
|
|
62
|
+
() => ({
|
|
63
|
+
approvals,
|
|
64
|
+
pendingCount,
|
|
65
|
+
refresh: loadPending,
|
|
66
|
+
}),
|
|
67
|
+
[approvals, pendingCount, loadPending],
|
|
68
|
+
);
|
|
69
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import type { McpPersonalStatus } from '../../mcp/personalAgentsApi';
|
|
3
|
+
import type { ShellUIConfig } from '../../config/types';
|
|
4
|
+
import { getCachedMcpPersonalStatus, loadMcpPersonalStatus } from '../utils/loadMcpPersonalStatus';
|
|
5
|
+
import { isMcpAgentsSettingsEligible } from '../utils/isMcpAgentsSettingsEligible';
|
|
6
|
+
|
|
7
|
+
export type McpPersonalStatusState = {
|
|
8
|
+
status: McpPersonalStatus | null;
|
|
9
|
+
mcpEnabled: boolean | null;
|
|
10
|
+
loading: boolean;
|
|
11
|
+
navPending: boolean;
|
|
12
|
+
error: boolean;
|
|
13
|
+
refresh: () => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function useMcpPersonalStatus(
|
|
17
|
+
config: ShellUIConfig | undefined,
|
|
18
|
+
accessToken: string | null,
|
|
19
|
+
isAuthenticated: boolean,
|
|
20
|
+
userId: string | null,
|
|
21
|
+
): McpPersonalStatusState {
|
|
22
|
+
const eligible = isMcpAgentsSettingsEligible({ config, isAuthenticated });
|
|
23
|
+
|
|
24
|
+
const cacheInput = useMemo(
|
|
25
|
+
() => ({ config, accessToken, isAuthenticated, userId }),
|
|
26
|
+
[config, accessToken, isAuthenticated, userId],
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
const cachedStatus = useMemo(
|
|
30
|
+
() => (eligible ? getCachedMcpPersonalStatus(cacheInput) : null),
|
|
31
|
+
[eligible, cacheInput],
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const [status, setStatus] = useState<McpPersonalStatus | null>(() => cachedStatus);
|
|
35
|
+
const [loading, setLoading] = useState(() => eligible && cachedStatus === null);
|
|
36
|
+
const [error, setError] = useState(false);
|
|
37
|
+
const [tick, setTick] = useState(0);
|
|
38
|
+
|
|
39
|
+
const refresh = useCallback(() => setTick((value) => value + 1), []);
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (!eligible) {
|
|
43
|
+
setStatus(null);
|
|
44
|
+
setLoading(false);
|
|
45
|
+
setError(false);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const fromCache = getCachedMcpPersonalStatus(cacheInput);
|
|
50
|
+
if (fromCache) {
|
|
51
|
+
setStatus(fromCache);
|
|
52
|
+
} else {
|
|
53
|
+
setStatus(null);
|
|
54
|
+
setLoading(true);
|
|
55
|
+
}
|
|
56
|
+
setError(false);
|
|
57
|
+
|
|
58
|
+
let cancelled = false;
|
|
59
|
+
void loadMcpPersonalStatus(cacheInput)
|
|
60
|
+
.then((next) => {
|
|
61
|
+
if (cancelled) return;
|
|
62
|
+
if (next) {
|
|
63
|
+
setStatus(next);
|
|
64
|
+
setError(false);
|
|
65
|
+
} else if (!fromCache) {
|
|
66
|
+
setStatus(null);
|
|
67
|
+
setError(true);
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
.finally(() => {
|
|
71
|
+
if (!cancelled) setLoading(false);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
return () => {
|
|
75
|
+
cancelled = true;
|
|
76
|
+
};
|
|
77
|
+
}, [eligible, cacheInput, tick]);
|
|
78
|
+
|
|
79
|
+
const mcpEnabled = status ? status.mcpEnabled : loading ? null : false;
|
|
80
|
+
const navPending = eligible && mcpEnabled === null;
|
|
81
|
+
|
|
82
|
+
return { status, mcpEnabled, loading, navPending, error, refresh };
|
|
83
|
+
}
|
|
@@ -243,11 +243,12 @@ describe('buildSettingsForPropagation', () => {
|
|
|
243
243
|
});
|
|
244
244
|
});
|
|
245
245
|
|
|
246
|
-
it('sets administration, storage, and
|
|
246
|
+
it('sets administration, storage, email, and aiService to null when not configured', () => {
|
|
247
247
|
const result = buildSettingsForPropagation(baseSettings, undefined, 'en');
|
|
248
248
|
expect(result.administration).toBeNull();
|
|
249
249
|
expect(result.storage).toBeNull();
|
|
250
250
|
expect(result.email).toBeNull();
|
|
251
|
+
expect(result.aiService).toBeNull();
|
|
251
252
|
});
|
|
252
253
|
|
|
253
254
|
it('propagates showInAdmin false when hosting admin is disabled', () => {
|
|
@@ -323,6 +324,41 @@ describe('buildSettingsForPropagation', () => {
|
|
|
323
324
|
expect(result.email).toEqual({ url: 'https://email.shellui.com' });
|
|
324
325
|
});
|
|
325
326
|
|
|
327
|
+
it('propagates aiService url and strips a trailing slash', () => {
|
|
328
|
+
const config = {
|
|
329
|
+
aiService: {
|
|
330
|
+
url: 'http://localhost:8006/',
|
|
331
|
+
},
|
|
332
|
+
} as ShellUIConfig;
|
|
333
|
+
|
|
334
|
+
const result = buildSettingsForPropagation(baseSettings, config, 'en');
|
|
335
|
+
expect(result.aiService).toEqual({ url: 'http://localhost:8006' });
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
it('sends no aiService when aiService is set without url (no built-in default)', () => {
|
|
339
|
+
const config = {
|
|
340
|
+
aiService: {},
|
|
341
|
+
} as ShellUIConfig;
|
|
342
|
+
|
|
343
|
+
const result = buildSettingsForPropagation(baseSettings, config, 'en');
|
|
344
|
+
expect(result.aiService).toBeNull();
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it('propagates showInAdmin false for aiService', () => {
|
|
348
|
+
const config = {
|
|
349
|
+
aiService: {
|
|
350
|
+
url: 'http://localhost:8006',
|
|
351
|
+
showInAdmin: false,
|
|
352
|
+
},
|
|
353
|
+
} as ShellUIConfig;
|
|
354
|
+
|
|
355
|
+
const result = buildSettingsForPropagation(baseSettings, config, 'en');
|
|
356
|
+
expect(result.aiService).toEqual({
|
|
357
|
+
url: 'http://localhost:8006',
|
|
358
|
+
showInAdmin: false,
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
|
|
326
362
|
const shellToken = 'shell-access-token';
|
|
327
363
|
const foreignToken = 'foreign-access-token';
|
|
328
364
|
const shellUser = {
|
|
@@ -345,6 +381,7 @@ describe('buildSettingsForPropagation', () => {
|
|
|
345
381
|
storage: { url: 'https://files-api.example', filesUrl: 'https://files.example/' },
|
|
346
382
|
email: { url: 'https://email.example' },
|
|
347
383
|
hosting: { url: 'https://hosting.example/', app: 'shell-app' },
|
|
384
|
+
aiService: { url: 'https://ai.example' },
|
|
348
385
|
navigation: [
|
|
349
386
|
{
|
|
350
387
|
label: 'Billing',
|
|
@@ -407,17 +444,20 @@ describe('buildSettingsForPropagation', () => {
|
|
|
407
444
|
storage: { url: 'https://files-api.example', filesUrl: 'https://files.example/' },
|
|
408
445
|
email: { url: 'https://email.example' },
|
|
409
446
|
hosting: { url: 'https://hosting.example', app: 'shell-app' },
|
|
447
|
+
aiService: { url: 'https://ai.example' },
|
|
410
448
|
};
|
|
411
449
|
|
|
412
450
|
const expectNoShellServices = (result: ReturnType<typeof propagate>) => {
|
|
413
451
|
expect(result.storage).toBeNull();
|
|
414
452
|
expect(result.email).toBeNull();
|
|
415
453
|
expect(result.hosting).toBeNull();
|
|
454
|
+
expect(result.aiService).toBeNull();
|
|
416
455
|
const json = JSON.stringify(result);
|
|
417
456
|
expect(json).not.toContain('https://files-api.example');
|
|
418
457
|
expect(json).not.toContain('https://files.example');
|
|
419
458
|
expect(json).not.toContain('https://email.example');
|
|
420
459
|
expect(json).not.toContain('https://hosting.example');
|
|
460
|
+
expect(json).not.toContain('https://ai.example');
|
|
421
461
|
expect(json).not.toContain('https://email.shellui.com');
|
|
422
462
|
};
|
|
423
463
|
|
|
@@ -429,6 +469,7 @@ describe('buildSettingsForPropagation', () => {
|
|
|
429
469
|
expect(result.storage).toEqual(shellServices.storage);
|
|
430
470
|
expect(result.email).toEqual(shellServices.email);
|
|
431
471
|
expect(result.hosting).toEqual(shellServices.hosting);
|
|
472
|
+
expect(result.aiService).toEqual(shellServices.aiService);
|
|
432
473
|
});
|
|
433
474
|
|
|
434
475
|
it('sends each foreign app its own backend URL and token', () => {
|
|
@@ -473,6 +514,7 @@ describe('buildSettingsForPropagation', () => {
|
|
|
473
514
|
expect(files.authBackendBaseUrl).toBe('https://id.example.com');
|
|
474
515
|
expect(admin.storage).toEqual(shellServices.storage);
|
|
475
516
|
expect(admin.email).toEqual(shellServices.email);
|
|
517
|
+
expect(admin.aiService).toEqual(shellServices.aiService);
|
|
476
518
|
expect(files.hosting).toEqual(shellServices.hosting);
|
|
477
519
|
});
|
|
478
520
|
|
|
@@ -513,6 +555,7 @@ describe('buildSettingsForPropagation', () => {
|
|
|
513
555
|
expect(result.storage).toEqual(shellServices.storage);
|
|
514
556
|
expect(result.email).toEqual(shellServices.email);
|
|
515
557
|
expect(result.hosting).toEqual(shellServices.hosting);
|
|
558
|
+
expect(result.aiService).toEqual(shellServices.aiService);
|
|
516
559
|
});
|
|
517
560
|
|
|
518
561
|
it('sends a foreign app its own service URLs and not the shell URLs', () => {
|