Elffuss BraidHub

kikocis/continue public

c04584525d2e · 5.0 KiBRawPermalink
1import * as fs from "fs";
2import * as path from "path";
3
4import React, { useEffect, useState } from "react";
5
6import {
7 getAccessToken,
8 getOrganizationId,
9 loadAuthConfig,
10 getAssistantSlug,
11 listUserOrganizations,
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 displayName = `[${org.name}] ${assistant.ownerSlug}/${assistant.packageSlug}`;
83 // Create unique ID that includes org info to avoid conflicts
84 const uniqueId = `${org.id || "personal"}-${assistant.packageSlug}`;
85 return {
86 id: uniqueId,
87 name: displayName,
88 type: "assistant" as const,
89 slug: `${assistant.ownerSlug}/${assistant.packageSlug}`,
90 organizationId: org.id,
91 };
92 });
93 } catch (err) {
94 console.error(
95 `Failed to load assistants for org ${org.name}:`,
96 err,
97 );
98 return [];
99 }
100 });
101
102 const assistantResults = await Promise.all(assistantPromises);
103 const allAssistants = assistantResults.flat();
104 options.push(...allAssistants);
105 } catch (err) {
106 console.error("Failed to load organizations or assistants:", err);
107 }
108 }
109
110 // Add "Create new assistant" option
111 options.push({
112 id: "create",
113 name: "Create new assistant",
114 type: "create",
115 displaySuffix: " (opens web)",
116 });
117
118 // Determine current config by checking auth config
119 const assistantSlug = getAssistantSlug(authConfig);
120 if (assistantSlug) {
121 // Find the matching config by slug and organization
122 const matchingConfig = options.find(
123 (opt) =>
124 opt.type === "assistant" &&
125 opt.slug === assistantSlug &&
126 opt.organizationId === currentOrganizationId,
127 );
128 currentId = matchingConfig?.id || null;
129 } else if (fs.existsSync(CONFIG_PATH)) {
130 // No assistant slug means local config is current
131 currentId = "local";
132 }
133
134 setConfigs(options);
135 setCurrentConfigId(currentId);
136 setLoading(false);
137 } catch (err: any) {
138 setError(err.message || "Failed to load configurations");
139 setLoading(false);
140 }
141 };
142
143 loadConfigs();
144 }, []);
145
146 return (
147 <Selector
148 title="Select Configuration"
149 options={configs}
150 selectedIndex={selectedIndex}
151 loading={loading}
152 error={error}
153 loadingMessage="Loading configurations..."
154 currentId={currentConfigId}
155 onSelect={onSelect}
156 onCancel={onCancel}
157 onNavigate={setSelectedIndex}
158 />
159 );
160};
161
162export { ConfigSelector };