File size: 2,031 Bytes
391c43e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { configManager } from '@/lib/config/storage';
import { getProvider } from '@/lib/llm/providers/registry';
import { resolveActiveAssignment } from './template-store';
import { type ResolvedAssignment } from './assignment';

/**
 * Whether the GLOBAL active template's agent provider has the credentials needed
 * to chat. Resolves the provider from the global active template's assignment and
 * runs the usual key / local / oauth readiness checks on that provider.
 */
export function isProjectProviderReady(): boolean {
  const agent = resolveActiveAssignment().agent;
  const provider = agent?.provider ?? configManager.getSelectedProvider();
  const providerConfig = getProvider(provider);
  if (providerConfig.isLocal) return true;
  if (providerConfig.apiKeyRequired || providerConfig.usesOAuth) {
    return !!configManager.getProviderApiKey(provider);
  }
  return true;
}

/**
 * True when the global active template's agent provider is not ready (no key/token), i.e. the user
 * has not chosen a working model yet, so onboarding may auto-assign a default. Guards against
 * overwriting an existing user's already-configured model when they merely re-auth a provider.
 */
export function shouldAutoAssignAgent(): boolean {
  return !isProjectProviderReady();
}

// Choose which model to select when auto-assigning a freshly connected provider:
// prefer the curated default if the provider actually serves it, otherwise the first available.
export function pickModelForProvider(defaultModel: string, availableModelIds: string[]): string {
  if (defaultModel && availableModelIds.includes(defaultModel)) return defaultModel;
  return availableModelIds[0] ?? defaultModel ?? '';
}

export async function getProjectAssignment(): Promise<ResolvedAssignment> {
  // ensure the Default template exists (one-time, idempotent); part B also runs this on settings load
  configManager.migrateModels();
  // Model selection is fully global now: resolve the single global active template.
  return resolveActiveAssignment();
}