-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
189 lines (168 loc) · 4.56 KB
/
config.ts
File metadata and controls
189 lines (168 loc) · 4.56 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import type { OpenSyncConfig } from "./types.js";
const CONFIG_DIR = path.join(os.homedir(), ".config", "cursor-sync");
const CONFIG_FILE = path.join(CONFIG_DIR, "config.json");
const STATE_FILE = path.join(CONFIG_DIR, "state.json");
// Default configuration
const DEFAULT_CONFIG: Partial<OpenSyncConfig> = {
autoSync: true,
syncToolCalls: true,
syncThinking: false,
debug: false,
};
/**
* Ensure config directory exists
*/
function ensureConfigDir(): void {
if (!fs.existsSync(CONFIG_DIR)) {
fs.mkdirSync(CONFIG_DIR, { recursive: true });
}
}
/**
* Load configuration from disk
*/
export function loadConfig(): OpenSyncConfig | null {
// Check environment variables first
const envUrl = process.env.CURSOR_SYNC_CONVEX_URL;
const envKey = process.env.CURSOR_SYNC_API_KEY;
if (envUrl && envKey) {
return {
convexUrl: envUrl,
apiKey: envKey,
autoSync: process.env.CURSOR_SYNC_AUTO_SYNC !== "false",
syncToolCalls: process.env.CURSOR_SYNC_TOOL_CALLS !== "false",
syncThinking: process.env.CURSOR_SYNC_THINKING === "true",
debug: process.env.CURSOR_SYNC_DEBUG === "true",
};
}
// Fall back to config file
if (!fs.existsSync(CONFIG_FILE)) {
return null;
}
try {
const raw = fs.readFileSync(CONFIG_FILE, "utf-8");
const config = JSON.parse(raw) as Partial<OpenSyncConfig>;
return {
...DEFAULT_CONFIG,
...config,
} as OpenSyncConfig;
} catch {
return null;
}
}
/**
* Save configuration to disk
*/
export function saveConfig(config: OpenSyncConfig): void {
ensureConfigDir();
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
}
/**
* Clear stored configuration
*/
export function clearConfig(): void {
if (fs.existsSync(CONFIG_FILE)) {
fs.unlinkSync(CONFIG_FILE);
}
}
/**
* Get a specific config value
*/
export function getConfigValue(key: keyof OpenSyncConfig): string | boolean | undefined {
const config = loadConfig();
return config?.[key];
}
/**
* Set a specific config value
*/
export function setConfigValue<K extends keyof OpenSyncConfig>(
key: K,
value: OpenSyncConfig[K]
): void {
const config = loadConfig() || ({} as OpenSyncConfig);
config[key] = value;
saveConfig(config);
}
/**
* Get the API URL from Convex URL
* Converts .convex.cloud to .convex.site for HTTP endpoints
*/
export function getApiUrl(convexUrl: string): string {
return convexUrl.replace(".convex.cloud", ".convex.site");
}
/**
* Mask API key for display
*/
export function maskApiKey(apiKey: string): string {
if (apiKey.length <= 12) {
return apiKey.slice(0, 4) + "****";
}
return apiKey.slice(0, 4) + "****..." + apiKey.slice(-4);
}
/**
* Session state management for tracking in-progress sessions
*/
interface StateData {
sessions: Record<string, {
conversationId: string;
startedAt: number;
prompt?: string;
workspaceRoots: string[];
messageCount: number;
toolCallCount: number;
synced: boolean;
}>;
lastSyncedAt?: number;
}
export function loadState(): StateData {
ensureConfigDir();
if (!fs.existsSync(STATE_FILE)) {
return { sessions: {} };
}
try {
return JSON.parse(fs.readFileSync(STATE_FILE, "utf-8"));
} catch {
return { sessions: {} };
}
}
export function saveState(state: StateData): void {
ensureConfigDir();
fs.writeFileSync(STATE_FILE, JSON.stringify(state, null, 2));
}
export function getSession(conversationId: string): StateData["sessions"][string] | undefined {
const state = loadState();
return state.sessions[conversationId];
}
export function updateSession(
conversationId: string,
updates: Partial<StateData["sessions"][string]>
): void {
const state = loadState();
state.sessions[conversationId] = {
...state.sessions[conversationId],
conversationId,
startedAt: state.sessions[conversationId]?.startedAt || Date.now(),
workspaceRoots: state.sessions[conversationId]?.workspaceRoots || [],
messageCount: state.sessions[conversationId]?.messageCount || 0,
toolCallCount: state.sessions[conversationId]?.toolCallCount || 0,
synced: false,
...updates,
};
saveState(state);
}
export function markSessionSynced(conversationId: string): void {
const state = loadState();
if (state.sessions[conversationId]) {
state.sessions[conversationId].synced = true;
state.lastSyncedAt = Date.now();
}
saveState(state);
}
export function clearState(): void {
if (fs.existsSync(STATE_FILE)) {
fs.unlinkSync(STATE_FILE);
}
}
export { CONFIG_DIR, CONFIG_FILE };