feat(assistant): an admin can share their ChatGPT plan with everyone

The connection for everyone can now also be the admin's own ChatGPT sign-in.
Whether a plan may be shared that way is the self-hoster's call under their
plan and OpenAI's terms; the form says what it means before the sign-in.
The sign-in starts on the admin route and finishes on the shared one, which
refuses a sign-in for everyone to anyone who is not an admin. The ChatGPT
return reads the clipboard only once the paste handler exists.
This commit is contained in:
Maurice
2026-10-21 22:57:40 +02:00
parent 422e213438
commit a372313ea1
38 changed files with 220 additions and 68 deletions
@@ -349,10 +349,14 @@ function PrivacyNote({ provider }: { provider: string }) {
);
}
/** The two ways each scope offers: ChatGPT is never an instance's, an own server never a user's (#1772). */
/**
* The ways each scope offers. An own server is never a user's (#1772). An
* admin may share their own ChatGPT plan with everyone: whether their plan and
* OpenAI's terms allow that is theirs to judge, and the form says so.
*/
const SCOPE_MODES: Record<AiConnectionScope, readonly AiConnectMode[]> = {
user: ['chatgpt', 'key'],
instance: ['key', 'custom'],
instance: ['key', 'chatgpt', 'custom'],
};
const MODE_TILES: Record<
@@ -392,7 +396,7 @@ export default function AiConnectPanel({ scope, onConnected, initialMode, prefil
const first = initialMode ?? prefillModeOf(prefill);
return first && SCOPE_MODES[scope].includes(first) ? first : null;
});
const chatgpt = useChatgptConnect({ active: scope === 'user' && mode === 'chatgpt', onConnected });
const chatgpt = useChatgptConnect({ active: mode === 'chatgpt', onConnected, scope });
const keyConnect = useKeyConnect({ scope, prefill, onConnected });
const custom = useCustomConnect({ prefill, onConnected });
const mcpEnabled = useAddonStore((s) => s.isEnabled('mcp'));
@@ -431,6 +435,16 @@ export default function AiConnectPanel({ scope, onConnected, initialMode, prefil
</div>
{scope === 'instance' && prefill && <SettingsHint>{t('assistant.admin.prefilled')}</SettingsHint>}
{mode === 'chatgpt' && scope === 'instance' && (
// Sharing one person's plan with everyone is the admin's call to make; TREK only says what it means.
<p
role="note"
className="m-0 rounded-lg bg-warning-soft px-3 py-2 leading-snug text-content"
style={fs(12, 'body')}
>
{t('assistant.admin.chatgptShared')}
</p>
)}
{mode === 'chatgpt' && <ChatGptPasteStep chatgpt={chatgpt} onOtherProvider={() => setMode('key')} />}
{mode === 'key' && <KeyForm scope={scope} keyConnect={keyConnect} />}
{mode === 'custom' && <CustomForm custom={custom} />}
@@ -157,7 +157,16 @@ export function useCustomConnect({
* page; pasting that address back finishes the sign-in at once, and coming
* back to this tab puts the cursor in the field.
*/
export function useChatgptConnect({ active, onConnected }: { active: boolean; onConnected: () => void }) {
export function useChatgptConnect({
active,
onConnected,
scope = 'user',
}: {
active: boolean;
onConnected: () => void;
/** The admin's sign-in shares their plan with everyone here. */
scope?: AiConnectionScope;
}) {
const { t } = useTranslation();
const toast = useToast();
const [attempt, setAttempt] = useState(0);
@@ -171,7 +180,7 @@ export function useChatgptConnect({ active, onConnected }: { active: boolean; on
useEffect(() => {
if (!active) return;
let cancelled = false;
void aiConnectionsRepo.chatgptStart().then(
void aiConnectionsRepo.chatgptStart(scope).then(
(next) => {
if (!cancelled) setFlow(next);
},
@@ -182,35 +191,7 @@ export function useChatgptConnect({ active, onConnected }: { active: boolean; on
return () => {
cancelled = true;
};
}, [active, attempt]);
// Back from the sign-in window: the address the user just copied is taken
// off the clipboard where the browser allows it, else the field has the cursor.
const takeCopied = useEffectEvent(() => {
if (pasted.trim() || busy) return;
if (!navigator.clipboard?.readText || !window.isSecureContext) {
inputRef.current?.focus();
return;
}
navigator.clipboard.readText().then(
(text) => {
if (!paste(text)) inputRef.current?.focus();
},
() => inputRef.current?.focus()
);
});
useEffect(() => {
if (!opened) return;
const onBack = () => {
if (document.visibilityState === 'visible') takeCopied();
};
document.addEventListener('visibilitychange', onBack);
window.addEventListener('focus', onBack);
return () => {
document.removeEventListener('visibilitychange', onBack);
window.removeEventListener('focus', onBack);
};
}, [opened]);
}, [active, attempt, scope]);
/** A fresh sign-in: the old one ran out or was used up. */
const restart = () => {
@@ -274,6 +255,34 @@ export function useChatgptConnect({ active, onConnected }: { active: boolean; on
);
};
// Back from the sign-in window: the address the user just copied is taken
// off the clipboard where the browser allows it, else the field has the cursor.
const takeCopied = useEffectEvent(() => {
if (pasted.trim() || busy) return;
if (!navigator.clipboard?.readText || !window.isSecureContext) {
inputRef.current?.focus();
return;
}
navigator.clipboard.readText().then(
(text) => {
if (!paste(text)) inputRef.current?.focus();
},
() => inputRef.current?.focus()
);
});
useEffect(() => {
if (!opened) return;
const onBack = () => {
if (document.visibilityState === 'visible') takeCopied();
};
document.addEventListener('visibilitychange', onBack);
window.addEventListener('focus', onBack);
return () => {
document.removeEventListener('visibilitychange', onBack);
window.removeEventListener('focus', onBack);
};
}, [opened]);
return {
flow,
opened,