Files
tkmind_go/ui/desktop/src/sessions.ts
T
dorien-koelemeijer 7343b6f432 fix(desktop): require recipe consent before session/new spawns extensions (#12094)
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Jasper Hugo <jasper@jasperhugo.com>
2026-09-16 15:08:33 +00:00

174 lines
5.7 KiB
TypeScript

import type { Session } from './types/session';
import type { ExtensionConfig } from './types/extensions';
import type { GooseExtension } from '@aaif/goose-acp-client';
import type { setViewType } from './hooks/useNavigation';
import type { FixedExtensionEntry } from './components/ConfigContext';
import { AppEvents } from './constants/events';
import { acpChatSessionController } from './acp/chatSessionController';
import { getConfiguredGooseExtensions, gooseExtensionName } from './acp/extensions';
import { beginConfiguredRecipeParameterScope } from './acp/recipeParamRequests';
import { getAcpFeatureCapabilities } from './acp/capabilities';
import { RecipeDeclinedError, RecipeParameterScopesUnsupportedError } from './acp/errors';
import { decodeRecipe } from './acp/recipe';
import { scanRecipe, type Recipe } from './recipe';
import { listSavedRecipes } from './recipe/recipe_management';
import { requestRecipeConsent } from './recipe/consent';
export function getSessionDisplayName(session: Session): string {
if (session.user_set_name) {
return session.name;
}
if (session.recipe?.title) {
return session.recipe.title;
}
return session.name;
}
interface CreateSessionOptions {
recipeDeeplink?: string;
recipeId?: string;
extensionConfigs?: ExtensionConfig[];
allExtensions?: FixedExtensionEntry[];
}
/**
* Three-valued on purpose. `undefined` means the caller is not naming a set and the
* backend should use the configured one; `[]` means the user asked for a session with
* no extensions at all. Collapsing the two is what made an all-off selection come back
* with the default extensions.
*/
function selectedExtensionConfigs(options?: CreateSessionOptions): ExtensionConfig[] | undefined {
if (options?.extensionConfigs) {
return options.extensionConfigs;
}
if (options?.allExtensions) {
const enabled = options.allExtensions
.filter((extension) => extension.enabled)
.map((extension) => {
const { enabled: _enabled, ...config } = extension;
return config as ExtensionConfig;
});
// An empty configured list is also what this looks like before the config
// finishes loading, so it stays "not specified" rather than becoming an
// explicit empty selection. Only `extensionConfigs` can express that.
return enabled.length > 0 ? enabled : undefined;
}
return undefined;
}
async function resolveGooseExtensions(
selected: ExtensionConfig[] | undefined
): Promise<GooseExtension[] | undefined> {
if (selected === undefined) {
return undefined;
}
if (selected.length === 0) {
return [];
}
const selectedNames = new Set(selected.map((config) => config.name));
return (await getConfiguredGooseExtensions())
.filter((entry) => selectedNames.has(gooseExtensionName(entry.extension)))
.map((entry) => entry.extension);
}
async function resolveRecipe(options?: CreateSessionOptions): Promise<Recipe | undefined> {
if (options?.recipeId) {
const entry = (await listSavedRecipes()).find((manifest) => manifest.id === options.recipeId);
if (!entry) {
throw new Error(`Recipe ${options.recipeId} was not found in the recipe library`);
}
return entry.recipe;
}
if (options?.recipeDeeplink) {
return decodeRecipe(options.recipeDeeplink);
}
return undefined;
}
// Recipes can declare commands, endpoints, and shell checks that run as soon as the
// session exists, so consent has to be settled before session/new is ever sent.
async function ensureRecipeConsent(options?: CreateSessionOptions): Promise<void> {
const recipe = await resolveRecipe(options);
if (!recipe || (await window.electron.hasAcceptedRecipeBefore(recipe))) {
return;
}
const scan = await scanRecipe(recipe);
const accepted = await requestRecipeConsent({
recipe,
hasSecurityWarnings: scan.has_security_warnings,
});
if (!accepted) {
throw new RecipeDeclinedError();
}
await window.electron.recordRecipeHash(recipe);
}
async function createAcpSession(
workingDir: string,
options?: CreateSessionOptions
): Promise<Session> {
await ensureRecipeConsent(options);
const configuredParameterScope = options?.recipeDeeplink
? beginConfiguredRecipeParameterScope()
: undefined;
try {
if (configuredParameterScope) {
const capabilities = await getAcpFeatureCapabilities();
if (!capabilities.recipeParameterScopes) {
throw new RecipeParameterScopesUnsupportedError();
}
}
const gooseExtensions = await resolveGooseExtensions(selectedExtensionConfigs(options));
return await acpChatSessionController.createSession(workingDir, gooseExtensions, {
recipeId: options?.recipeId,
recipeDeeplink: options?.recipeDeeplink,
recipeParameterScopeId: configuredParameterScope?.id,
});
} finally {
configuredParameterScope?.finish();
}
}
export async function createSession(
workingDir: string,
options?: CreateSessionOptions
): Promise<Session> {
return createAcpSession(workingDir, options);
}
export async function startNewSession(
initialText: string | undefined,
setView: setViewType,
workingDir: string,
options?: {
recipeDeeplink?: string;
recipeId?: string;
allExtensions?: FixedExtensionEntry[];
}
): Promise<Session> {
const session = await createSession(workingDir, options);
window.dispatchEvent(new CustomEvent(AppEvents.SESSION_CREATED, { detail: { session } }));
const initialMessage = initialText ? { msg: initialText, images: [] } : undefined;
const eventDetail = {
sessionId: session.id,
initialMessage,
};
window.dispatchEvent(
new CustomEvent(AppEvents.ADD_ACTIVE_SESSION, {
detail: eventDetail,
})
);
setView('pair', {
disableAnimation: true,
initialMessage,
resumeSessionId: session.id,
});
return session;
}