Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions apps/web/src/components/cloud-agent-next/NewSessionPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ import {
import {
getDevcontainerEnabled,
getLastUsedModel,
getLastUsedRepo,
getLastUsedVariant,
getPreferredInitialModel,
getPreferredInitialVariant,
setDevcontainerEnabled,
setLastUsedModel,
setLastUsedRepo,
setLastUsedVariant,
} from '@/components/cloud-agent-next/model-preferences';

Expand Down Expand Up @@ -438,20 +440,40 @@ export function NewSessionPanel({ organizationId, isDevcontainerAvailable }: New
const repo = unifiedRepositories.find(r => r.fullName === repoFullName);
if (repo?.platform) {
setSelectedPlatform(repo.platform);
if (userInitiated) setLastUsedRepo(repoFullName, organizationId);
}
},
[unifiedRepositories]
[unifiedRepositories, organizationId]
);

// ---------------------------------------------------------------------------
// Auto-select repo from last session (most recently used)
// Auto-select repo: restore from localStorage first, then fall back to the
// most recently used repo from the server.
// ---------------------------------------------------------------------------
useEffect(() => {
if (selectedRepo || isRepoUserSelected || recentRepos.length === 0) return;
if (selectedRepo || isRepoUserSelected || unifiedRepositories.length === 0) return;

const saved = getLastUsedRepo(organizationId);
if (saved) {
const match = unifiedRepositories.find(r => r.fullName === saved.fullName);
if (match) {
handleRepoSelect(match.fullName, false);
return;
}
}

if (recentRepos.length === 0) return;
const firstRecent = recentRepos[0];
if (!firstRecent) return;
handleRepoSelect(firstRecent.fullName, false);
}, [recentRepos, selectedRepo, isRepoUserSelected, handleRepoSelect]);
}, [
recentRepos,
selectedRepo,
isRepoUserSelected,
handleRepoSelect,
unifiedRepositories,
organizationId,
]);

// ---------------------------------------------------------------------------
// Auto-select repo from pasted GitHub/GitLab URLs
Expand Down
23 changes: 23 additions & 0 deletions apps/web/src/components/cloud-agent-next/model-preferences.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { describe, expect, it } from '@jest/globals';
import {
getDevcontainerEnabledStorageKey,
getLastUsedModelStorageKey,
getLastUsedRepoStorageKey,
getLastUsedVariantsStorageKey,
getPreferredInitialModel,
getPreferredInitialVariant,
parseDevcontainerEnabled,
parseLastUsedRepo,
} from './model-preferences';
import type { ModelOption } from '@/components/shared/ModelCombobox';

Expand Down Expand Up @@ -74,6 +76,27 @@ describe('getLastUsedVariantsStorageKey', () => {
});
});

describe('repository preference', () => {
it('uses separate keys for personal and organization contexts', () => {
expect(getLastUsedRepoStorageKey()).toBe('cloud-agent:last-used-repo:personal');
expect(getLastUsedRepoStorageKey('org_123')).toBe(
'cloud-agent:last-used-repo:organization:org_123'
);
});

it('restores only the repository full name from previously stored data', () => {
expect(parseLastUsedRepo('{"fullName":"kilo/cloud","platform":"invalid"}')).toEqual({
fullName: 'kilo/cloud',
});
});

it('ignores malformed stored data', () => {
expect(parseLastUsedRepo('{"platform":"github"}')).toBeNull();
expect(parseLastUsedRepo('not json')).toBeNull();
expect(parseLastUsedRepo(null)).toBeNull();
});
});

describe('devcontainer preference', () => {
it('uses one browser-wide storage key', () => {
expect(getDevcontainerEnabledStorageKey()).toBe('cloud-agent:devcontainer-enabled');
Expand Down
39 changes: 39 additions & 0 deletions apps/web/src/components/cloud-agent-next/model-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,45 @@ import type { ModelOption } from '@/components/shared/ModelCombobox';
const MODEL_STORAGE_KEY_PREFIX = 'cloud-agent:last-used-model';
const VARIANTS_STORAGE_KEY_PREFIX = 'cloud-agent:last-used-variants';
const DEVCONTAINER_ENABLED_STORAGE_KEY = 'cloud-agent:devcontainer-enabled';
const REPO_STORAGE_KEY_PREFIX = 'cloud-agent:last-used-repo';

type LastUsedRepo = { fullName: string };

export function getLastUsedRepoStorageKey(organizationId?: string) {
return organizationId
? `${REPO_STORAGE_KEY_PREFIX}:organization:${organizationId}`
: `${REPO_STORAGE_KEY_PREFIX}:personal`;
}

export function parseLastUsedRepo(rawValue: string | null): LastUsedRepo | null {
if (!rawValue) return null;
try {
const parsed: unknown = JSON.parse(rawValue);
if (
parsed &&
typeof parsed === 'object' &&
!Array.isArray(parsed) &&
'fullName' in parsed &&
typeof parsed.fullName === 'string'
) {
return { fullName: parsed.fullName };
}
return null;
} catch {
return null;
}
}

export function getLastUsedRepo(organizationId?: string): LastUsedRepo | null {
return parseLastUsedRepo(safeLocalStorage.getItem(getLastUsedRepoStorageKey(organizationId)));
}

export function setLastUsedRepo(fullName: string, organizationId?: string) {
safeLocalStorage.setItem(
getLastUsedRepoStorageKey(organizationId),
JSON.stringify({ fullName } satisfies LastUsedRepo)
);
}

export function getLastUsedModelStorageKey(organizationId?: string) {
return organizationId
Expand Down