feat: preconfigure session sharing with GOOSE_BASE_URL_SHARE (#1885)

Co-authored-by: Lily Delalande <ldelalande@squareup.com>
This commit is contained in:
Salman Mohammed
2025-03-28 18:52:21 -04:00
committed by GitHub
parent 165310bc72
commit 5c605c92e0
2 changed files with 76 additions and 44 deletions

View File

@@ -1,30 +1,37 @@
import React, { useState, useEffect } from 'react';
import { Input } from '../../ui/input';
import { Check } from 'lucide-react';
import { Check, Lock } from 'lucide-react';
export default function SessionSharingSection() {
const envBaseUrlShare = window.appConfig.get('GOOSE_BASE_URL_SHARE');
console.log('envBaseUrlShare', envBaseUrlShare);
// If env is set, force sharing enabled and set the baseUrl accordingly.
const [sessionSharingConfig, setSessionSharingConfig] = useState({
enabled: false,
baseUrl: '',
enabled: envBaseUrlShare ? true : false,
baseUrl: envBaseUrlShare || '',
});
const [urlError, setUrlError] = useState('');
// Show a checkmark temporarily when the users input is valid
const [urlSaved, setUrlSaved] = useState(false);
// isUrlConfigured is true if the user has configured a baseUrl and it is valid.
const isUrlConfigured =
!envBaseUrlShare && sessionSharingConfig.enabled && isValidUrl(sessionSharingConfig.baseUrl);
// Load session sharing config from localStorage
// Only load saved config from localStorage if the env variable is not provided.
useEffect(() => {
const savedSessionConfig = localStorage.getItem('session_sharing_config');
if (savedSessionConfig) {
try {
const config = JSON.parse(savedSessionConfig);
setSessionSharingConfig(config);
} catch (error) {
console.error('Error parsing session sharing config:', error);
if (!envBaseUrlShare) {
const savedSessionConfig = localStorage.getItem('session_sharing_config');
if (savedSessionConfig) {
try {
const config = JSON.parse(savedSessionConfig);
setSessionSharingConfig(config);
} catch (error) {
console.error('Error parsing session sharing config:', error);
}
}
}
}, []);
}, [envBaseUrlShare]);
// Helper to check if the users input is a valid URL
// Helper to check if the user's input is a valid URL
function isValidUrl(value: string): boolean {
if (!value) return false;
try {
@@ -35,8 +42,11 @@ export default function SessionSharingSection() {
}
}
// Handle toggling "Enable Session Sharing"
const handleEnableToggle = () => {
// Toggle sharing (only allowed when env is not set).
const toggleSharing = () => {
if (envBaseUrlShare) {
return; // Do nothing if the environment variable forces sharing.
}
setSessionSharingConfig((prev) => {
const updated = { ...prev, enabled: !prev.enabled };
localStorage.setItem('session_sharing_config', JSON.stringify(updated));
@@ -56,12 +66,6 @@ export default function SessionSharingSection() {
setUrlError('');
const updated = { ...sessionSharingConfig, baseUrl: newBaseUrl };
localStorage.setItem('session_sharing_config', JSON.stringify(updated));
// Show the checkmark temporarily
setUrlSaved(true);
setTimeout(() => {
setUrlSaved(false);
}, 2000);
} else {
setUrlError('Invalid URL format. Please enter a valid URL (e.g. https://example.com/api).');
}
@@ -74,30 +78,46 @@ export default function SessionSharingSection() {
</div>
<div className="px-8">
<p className="text-sm text-textStandard mb-4">
You can enable session sharing to share your sessions with others. You'll then need to
enter the base URL for the session sharing API endpoint. Anyone with access to the same
API and sharing session enabled will be able to see your sessions.
</p>
{envBaseUrlShare ? (
<p className="text-sm text-textStandard mb-4">
Session sharing is configured but fully opt-in your sessions are only shared when you
explicitly click the share button.
</p>
) : (
<p className="text-sm text-textStandard mb-4">
You can enable session sharing to share your sessions with others. You'll then need to
enter the base URL for the session sharing API endpoint. Anyone with access to the same
API and sharing session enabled will be able to see your sessions.
</p>
)}
<div className="space-y-4">
{/* Enable Session Sharing toggle */}
{/* Toggle for enabling session sharing */}
<div className="flex items-center justify-between">
<label className="text-sm font-medium text-textStandard cursor-pointer">
Enable Session Sharing
{envBaseUrlShare
? 'Session sharing has already been configured'
: 'Enable session sharing'}
</label>
<button
onClick={handleEnableToggle}
className={`relative inline-flex h-6 w-11 items-center rounded-full ${
sessionSharingConfig.enabled ? 'bg-indigo-500' : 'bg-bgProminent'
} transition-colors duration-200 ease-in-out focus:outline-none`}
>
<span
className={`inline-block h-5 w-5 transform rounded-full bg-white shadow ${
sessionSharingConfig.enabled ? 'translate-x-[22px]' : 'translate-x-[2px]'
} transition-transform duration-200 ease-in-out`}
/>
</button>
{envBaseUrlShare ? (
<Lock className="w-5 h-5 text-gray-600" />
) : (
<button
onClick={toggleSharing}
disabled={!!envBaseUrlShare}
className={`relative inline-flex h-6 w-11 items-center rounded-full ${
sessionSharingConfig.enabled ? 'bg-indigo-500' : 'bg-bgProminent'
} transition-colors duration-200 ease-in-out focus:outline-none ${
envBaseUrlShare ? 'cursor-not-allowed' : ''
}`}
>
<span
className={`inline-block h-5 w-5 transform rounded-full bg-white shadow ${
sessionSharingConfig.enabled ? 'translate-x-[22px]' : 'translate-x-[2px]'
} transition-transform duration-200 ease-in-out`}
/>
</button>
)}
</div>
{/* Base URL field (only visible if enabled) */}
@@ -110,7 +130,7 @@ export default function SessionSharingSection() {
>
Base URL
</label>
{urlSaved && <Check className="w-5 h-5 text-green-500" />}
{isUrlConfigured && <Check className="w-5 h-5 text-green-500" />}
</div>
<div className="flex items-center">
<Input
@@ -118,7 +138,8 @@ export default function SessionSharingSection() {
type="url"
placeholder="https://example.com/api"
value={sessionSharingConfig.baseUrl}
onChange={handleBaseUrlChange}
disabled={!!envBaseUrlShare}
onChange={envBaseUrlShare ? undefined : handleBaseUrlChange}
/>
</div>
{urlError && <p className="text-red-500 text-sm">{urlError}</p>}

View File

@@ -122,8 +122,18 @@ const generateSecretKey = () => {
return key;
};
const getSharingUrl = () => {
// checks app env for sharing url
loadShellEnv(app.isPackaged); // will try to take it from the zshrc file
// if GOOSE_BASE_URL_SHARE is found, we will set process.env.GOOSE_BASE_URL_SHARE, otherwise we return what it is set
// to in the env at bundle time
return process.env.GOOSE_BASE_URL_SHARE;
};
let [provider, model] = getGooseProvider();
let sharingUrl = getSharingUrl();
let appConfig = {
GOOSE_PROVIDER: provider,
GOOSE_MODEL: model,
@@ -170,6 +180,7 @@ const createChat = async (
GOOSE_PORT: port,
GOOSE_WORKING_DIR: working_dir,
REQUEST_DIR: dir,
GOOSE_BASE_URL_SHARE: sharingUrl,
botConfig: botConfig,
}),
],