Monorepo Pi package with outbound WebSocket, pairing/reconnect flow, presence reporting, and single-flight task.dispatch handling. Co-authored-by: Cursor <cursoragent@cursor.com>
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
|
|
export interface StoredReconnectCredential {
|
|
readonly projectName: string;
|
|
readonly credential: string;
|
|
readonly wsUrl: string;
|
|
readonly updatedAt: string;
|
|
}
|
|
|
|
export function credentialFilePath(baseDir: string): string {
|
|
return path.join(baseDir, 'local-agent-reconnect.json');
|
|
}
|
|
|
|
export async function loadReconnectCredential(baseDir: string): Promise<StoredReconnectCredential | undefined> {
|
|
try {
|
|
const raw = await readFile(credentialFilePath(baseDir), 'utf8');
|
|
const parsed = JSON.parse(raw) as StoredReconnectCredential;
|
|
if (!parsed?.projectName || !parsed?.credential || !parsed?.wsUrl) {
|
|
return undefined;
|
|
}
|
|
return parsed;
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
export async function saveReconnectCredential(
|
|
baseDir: string,
|
|
value: StoredReconnectCredential,
|
|
): Promise<void> {
|
|
await mkdir(baseDir, { recursive: true });
|
|
await writeFile(credentialFilePath(baseDir), JSON.stringify(value, null, 2), 'utf8');
|
|
}
|
|
|
|
export async function clearReconnectCredential(baseDir: string): Promise<void> {
|
|
try {
|
|
await rm(credentialFilePath(baseDir));
|
|
} catch {
|
|
/* already absent */
|
|
}
|
|
}
|