Elffuss BraidHub

kikocis/continue public

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