org selection

This commit is contained in:
Nate 2025-07-15 10:51:53 -07:00
parent 50d8b463b8
commit 5e73c07ba9
4 changed files with 156 additions and 38 deletions

View file

@ -35,7 +35,7 @@ export function getLlmApi(
env: {
apiKeyLocation: (model as any).apiKeyLocation,
// envSecretLocations: model.env,
orgScopeId: null, // TODO
orgScopeId: authConfig.organizationId ?? null,
proxyUrl: undefined, // TODO
},
}
@ -117,10 +117,10 @@ export async function initializeAssistant(
const config = await loadAssistant(
authConfig.accessToken,
configPath,
undefined
authConfig.organizationId ?? undefined
);
const [llmApi, model] = getLlmApi(config, authConfig);
const mcpService = await MCPService.create(config);
return { config, llmApi, model, mcpService };
}
}

View file

@ -1,3 +1,7 @@
import {
Configuration,
DefaultApi,
} from "@continuedev/sdk/dist/api/dist/index.js";
import axios from "axios";
import chalk from "chalk";
import * as fs from "fs";
@ -17,6 +21,7 @@ export interface AuthConfig {
accessToken?: string;
refreshToken?: string;
expiresAt?: number;
organizationId?: string | null;
}
/**
@ -211,6 +216,146 @@ export async function login(
}
}
/**
* Ensures the user has selected an organization, prompting if necessary
*/
export async function ensureOrganization(
authConfig: AuthConfig,
isHeadless: boolean = false
): Promise<AuthConfig> {
// If using CONTINUE_API_KEY environment variable, don't require organization selection
if (process.env.CONTINUE_API_KEY) {
return authConfig;
}
// If already have organization ID (including null for personal), return as-is
if (authConfig.organizationId !== undefined) {
return authConfig;
}
// In headless mode, default to personal organization if none saved
if (isHeadless) {
const updatedConfig = {
...authConfig,
organizationId: null, // Default to personal organization
};
saveAuthConfig(updatedConfig);
return updatedConfig;
}
// Need to select organization
const apiClient = new DefaultApi(
new Configuration({
accessToken: authConfig.accessToken,
})
);
try {
const resp = await apiClient.listOrganizations();
const organizations = resp.organizations;
if (organizations.length === 0) {
console.info(
chalk.green("No organizations found. Using personal organization.")
);
const updatedConfig = {
...authConfig,
organizationId: null,
};
saveAuthConfig(updatedConfig);
return updatedConfig;
}
if (organizations.length === 1) {
// Show choice between personal and the one organization
const org = organizations[0];
console.info(chalk.cyan("\nSelect an organization:"));
console.info(chalk.white(`1. Personal (default)`));
console.info(chalk.white(`2. ${org.name}`));
const selection = await prompt(
chalk.yellow("Enter your choice (number): ")
);
const selectedIndex = parseInt(selection) - 1;
if (selectedIndex < 0 || selectedIndex > 1) {
console.error(chalk.red("Invalid selection. Please try again."));
return await ensureOrganization(authConfig, isHeadless);
}
let selectedOrgId: string | null;
let selectedOrgName: string;
if (selectedIndex === 0) {
// Personal organization selected
selectedOrgId = null;
selectedOrgName = "Personal";
} else {
// The one organization selected
selectedOrgId = org.id;
selectedOrgName = org.name;
}
console.info(chalk.green(`Selected organization: ${selectedOrgName}`));
const updatedConfig = {
...authConfig,
organizationId: selectedOrgId,
};
saveAuthConfig(updatedConfig);
return updatedConfig;
}
// Multiple organizations - show selection including personal
console.info(chalk.cyan("\nSelect an organization:"));
console.info(chalk.white(`1. Personal (default)`));
organizations.forEach((org, index) => {
console.info(chalk.white(`${index + 2}. ${org.name}`));
});
const selection = await prompt(
chalk.yellow("Enter your choice (number): ")
);
const selectedIndex = parseInt(selection) - 1;
if (selectedIndex < 0 || selectedIndex > organizations.length) {
console.error(chalk.red("Invalid selection. Please try again."));
return await ensureOrganization(authConfig, isHeadless);
}
let selectedOrgId: string | null;
let selectedOrgName: string;
if (selectedIndex === 0) {
// Personal organization selected
selectedOrgId = null;
selectedOrgName = "Personal";
} else {
// Regular organization selected
const selectedOrg = organizations[selectedIndex - 1];
selectedOrgId = selectedOrg.id;
selectedOrgName = selectedOrg.name;
}
console.info(chalk.green(`Selected organization: ${selectedOrgName}`));
const updatedConfig = {
...authConfig,
organizationId: selectedOrgId,
};
saveAuthConfig(updatedConfig);
return updatedConfig;
} catch (error: any) {
console.error(
chalk.red("Error fetching organizations:"),
error.response?.data?.message || error.message || error
);
console.info(chalk.yellow("Continuing without organization selection."));
return authConfig;
}
}
/**
* Logs the user out by clearing saved credentials
*/

View file

@ -4,9 +4,9 @@ import chalk from "chalk";
import { ChatCompletionMessageParam } from "openai/resources.mjs";
import * as readlineSync from "readline-sync";
import { parseArgs } from "./args.js";
import { ensureAuthenticated } from "./auth/ensureAuth.js";
import { initializeAssistant } from "./assistant.js";
import { loadAuthConfig } from "./auth/workos.js";
import { ensureAuthenticated } from "./auth/ensureAuth.js";
import { loadAuthConfig, ensureOrganization } from "./auth/workos.js";
import { introMessage } from "./intro.js";
import { configureLogger } from "./logger.js";
import { loadSession, saveSession } from "./session.js";
@ -21,8 +21,6 @@ const args = parseArgs();
// Configure logger based on headless mode
configureLogger(args.isHeadless);
async function chat() {
const isAuthenticated = await ensureAuthenticated(true);
@ -33,37 +31,12 @@ async function chat() {
const authConfig = loadAuthConfig();
// This was the previous default behavior, but currently the SDK
// only supports slugs, so we've disabled reading local assistant files
// if (fs.existsSync(args.assistantPath)) {
// // If it's a file, load it directly
// console.info(
// chalk.yellow(`Loading assistant from file: ${args.assistantPath}`)
// );
// // We need to extract the assistant slug from the yaml to use with the SDK
// // For now, let's just use a placeholder slug and use the file content for assistant config
// // In a real implementation, we'd need to parse the YAML and extract the slug
// const assistantSlug = "default/assistant";
// try {
// sdkClient = await initializeContinueSDK(
// authConfig.accessToken,
// assistantSlug
// );
// // Since we're using a file, we need to manually set the assistant config
// assistant = JSON.parse(JSON.stringify(sdkClient.assistant.config));
// } catch (error) {
// console.error(
// chalk.red("Error initializing SDK with local file:"),
// error
// );
// throw error;
// }
// }
// Ensure organization is selected
const authConfigWithOrg = await ensureOrganization(authConfig, args.isHeadless);
// Initialize ContinueSDK and MCPService once
const { config, llmApi, model, mcpService } = await initializeAssistant(
authConfig,
authConfigWithOrg,
args.configPath
);

View file

@ -55,9 +55,9 @@ export function introMessage(
}
console.info("");
if (config.rules?.length) {
console.info(chalk.yellow("\nAssistant rules: " + config.rules.length));
}
// if (config.rules?.length) {
// console.info(chalk.yellow("\nRules: " + config.rules.length));
// }
if (config.mcpServers?.length) {
console.info(chalk.yellow("\nMCP Servers:"));