Elffuss BraidHub

kikocis/continue public

main · 5.5 KiBRawPermalink
1import * as fs from "fs";
2import * as path from "path";
3
4import React, { useEffect, useState } from "react";
5
6import {
7 getAccessToken,
8 getAssistantSlug,
9 getOrganizationId,
10 listUserOrganizations,
11 loadAuthConfig,
12} from "../auth/workos.js";
13import { getApiClient } from "../config.js";
14import { env } from "../env.js";
15import { services } from "../services/index.js";
16
17import { Selector, SelectorOption } from "./Selector.js";
18
19interface ConfigOption extends SelectorOption {
20 type: "local" | "assistant" | "create";
21 slug?: string;
22 organizationId?: string | null; // null for personal, string for org
23}
24
25interface ConfigSelectorProps {
26 onSelect: (config: ConfigOption) => void;
27 onCancel: () => void;
28}
29
30const CONFIG_PATH = path.join(env.continueHome, "config.yaml");
31
32const ConfigSelector: React.FC<ConfigSelectorProps> = ({
33 onSelect,
34 onCancel,
35}) => {
36 const [configs, setConfigs] = useState<ConfigOption[]>([]);
37 const [selectedIndex, setSelectedIndex] = useState(0);
38 const [loading, setLoading] = useState(true);
39 const [error, setError] = useState<string | null>(null);
40 const [currentConfigId, setCurrentConfigId] = useState<string | null>(null);
41
42 useEffect(() => {
43 const loadConfigs = async () => {
44 try {
45 const authConfig = loadAuthConfig();
46 const accessToken = getAccessToken(authConfig);
47 const currentOrganizationId = getOrganizationId(authConfig);
48
49 const options: ConfigOption[] = [];
50 let currentId: string | null = null;
51
52 // Add local config.yaml if it exists
53 if (fs.existsSync(CONFIG_PATH)) {
54 options.push({
55 id: "local",
56 name: "[Personal] Local config.yaml",
57 type: "local",
58 organizationId: null,
59 });
60 }
61
62 // Add assistants from all organizations if authenticated
63 if (accessToken) {
64 const apiClient = getApiClient(accessToken);
65
66 try {
67 // Get all organizations
68 const organizations = await listUserOrganizations();
69 const allOrgs = [
70 { id: null, name: "Personal" }, // Personal organization
71 ...(organizations || []),
72 ];
73
74 // Fetch assistants for each organization
75 const assistantPromises = allOrgs.map(async (org) => {
76 try {
77 const assistants = await apiClient.listAssistants({
78 alwaysUseProxy: "false",
79 organizationId: org.id ?? undefined,
80 });
81
82 return assistants.map((assistant) => {
83 const name =
84 (assistant.configResult.config as any)?.name ??
85 `${assistant.ownerSlug}/${assistant.packageSlug}`;
86 const displayName = `[${org.name}] ${name}`;
87 // Create unique ID that includes org info to avoid conflicts
88 const uniqueId = `${org.id || "personal"}-${assistant.packageSlug}`;
89 return {
90 id: uniqueId,
91 name: displayName,
92 type: "assistant" as const,
93 slug: `${assistant.ownerSlug}/${assistant.packageSlug}`,
94 organizationId: org.id,
95 };
96 });
97 } catch (err) {
98 console.error(
99 `Failed to load assistants for org ${org.name}:`,
100 err,
101 );
102 return [];
103 }
104 });
105
106 const assistantResults = await Promise.all(assistantPromises);
107 const allAssistants = assistantResults.flat();
108 options.push(...allAssistants);
109 } catch (err) {
110 console.error("Failed to load organizations or assistants:", err);
111 }
112 }
113
114 // Add "Create new assistant" option
115 options.push({
116 id: "create",
117 name: "Create new assistant",
118 type: "create",
119 displaySuffix: " (opens web)",
120 });
121
122 // Determine current config by checking both auth config and service state
123 const assistantSlug = getAssistantSlug(authConfig);
124 const currentConfigState = services.config.getState();
125
126 if (assistantSlug) {
127 // Find the matching config by slug and organization
128 const matchingConfig = options.find(
129 (opt) =>
130 opt.type === "assistant" &&
131 opt.slug === assistantSlug &&
132 opt.organizationId === currentOrganizationId,
133 );
134 currentId = matchingConfig?.id || null;
135 } else if (
136 currentConfigState.configPath === CONFIG_PATH &&
137 fs.existsSync(CONFIG_PATH)
138 ) {
139 // Only mark local config as current if it's actually the active config path in the service
140 // This ensures we don't show a green checkmark for first-time users just because the file exists
141 currentId = "local";
142 }
143
144 setConfigs(options);
145 setCurrentConfigId(currentId);
146 setLoading(false);
147 } catch (err: any) {
148 setError(err.message || "Failed to load configurations");
149 setLoading(false);
150 }
151 };
152
153 loadConfigs();
154 }, []);
155
156 return (
157 <Selector
158 title="Select Configuration"
159 options={configs}
160 selectedIndex={selectedIndex}
161 loading={loading}
162 error={error}
163 loadingMessage="Loading configurations..."
164 currentId={currentConfigId}
165 onSelect={onSelect}
166 onCancel={onCancel}
167 onNavigate={setSelectedIndex}
168 />
169 );
170};
171
172export { ConfigSelector };