Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const OPENCODE_CONFIG_DIR = join(homedir(), ".config", "opencode");
const OPENCODE_COMMAND_DIR = join(OPENCODE_CONFIG_DIR, "command");
const OH_MY_OPENCODE_CONFIG = join(OPENCODE_CONFIG_DIR, "oh-my-opencode.json");
const PLUGIN_NAME = "opencode-supermemory@latest";
const DEFAULT_CONFIG_FILE = CONFIG_FILE ?? join(OPENCODE_CONFIG_DIR, "supermemory.json");

const SUPERMEMORY_INIT_COMMAND = `---
description: Initialize Supermemory with comprehensive codebase knowledge
Expand Down Expand Up @@ -377,7 +378,7 @@ interface InstallOptions {
async function install(options: InstallOptions): Promise<number> {
console.log("\n🧠 opencode-supermemory installer\n");

writeInstallDefaults(existsSync(CONFIG_FILE));
writeInstallDefaults(existsSync(DEFAULT_CONFIG_FILE));

const rl = options.tui ? createReadline() : null;

Expand Down
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function getApiKey(): string | undefined {

export const SUPERMEMORY_API_KEY = getApiKey();
export const CONFIG_FILE = CONFIG_FILES[1];
const DEFAULT_CONFIG_FILE = CONFIG_FILE ?? join(CONFIG_DIR, "supermemory.json");

export const CONFIG = {
similarityThreshold: fileConfig.similarityThreshold ?? DEFAULTS.similarityThreshold,
Expand Down Expand Up @@ -140,5 +141,5 @@ export function writeInstallDefaults(isExistingInstall: boolean): void {
next.autoRecallEveryPrompt = false;
next.captureEveryNTurns = 0;
}
writeFileSync(CONFIG_FILE, JSON.stringify(next, null, 2));
writeFileSync(DEFAULT_CONFIG_FILE, JSON.stringify(next, null, 2));
}
25 changes: 8 additions & 17 deletions src/services/auth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { exec } from "node:child_process";
import { join } from "node:path";
import { homedir } from "node:os";
import { openUrl } from "./openUrl.js";

const CREDENTIALS_DIR = join(homedir(), ".supermemory-opencode");
const CREDENTIALS_FILE = join(CREDENTIALS_DIR, "credentials.json");
Expand Down Expand Up @@ -40,21 +40,6 @@ export function clearCredentials(): boolean {
return true;
}

function openBrowser(url: string): void {
const platform = process.platform;

const commands: Record<string, string> = {
darwin: `open "${url}"`,
win32: `start "" "${url}"`,
linux: `xdg-open "${url}"`,
};

const cmd = commands[platform] ?? `xdg-open "${url}"`;
exec(cmd, (err) => {
if (err) console.error("Failed to open browser:", err.message);
});
}

export interface AuthResult {
success: boolean;
apiKey?: string;
Expand Down Expand Up @@ -129,7 +114,13 @@ export function startAuthFlow(timeoutMs = 120000): Promise<AuthResult> {

console.log("Opening browser for authentication...");
console.log(`If it doesn't open, visit: ${authUrl}`);
openBrowser(authUrl);
openUrl(authUrl).catch((error) => {
if (!resolved) {
resolved = true;
server.close();
resolve({ success: false, error: `Failed to open browser: ${error.message}` });
}
});
});

setTimeout(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/services/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class SupermemoryClient {
sm_source: "opencode",
sm_capture_mode: metadata?.sm_capture_mode ?? "tool",
...(metadata ?? {}),
} as Record<string, string | number | boolean | string[]>;
} as unknown as Record<string, string | number | boolean | string[]>;

const result = await withTimeout(
this.getClient().memories.add({
Expand Down
34 changes: 34 additions & 0 deletions src/services/openUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { execFile } from "node:child_process";

function run(command: string, args: string[]): Promise<void> {
return new Promise((resolve, reject) => {
execFile(command, args, { windowsHide: true }, (error) => {
if (error) reject(error);
else resolve();
});
});
}

export async function openUrl(url: string | URL): Promise<void> {
const href = url.toString();
if (!/^https?:\/\//i.test(href)) {
throw new Error("Refusing to open non-http URL");
}

if (process.platform === "win32") {
try {
await run("rundll32.exe", ["url.dll,FileProtocolHandler", href]);
return;
} catch {}

await run("cmd.exe", ["/c", "start", '""', href]);
return;
}

if (process.platform === "darwin") {
await run("open", [href]);
return;
}

await run("xdg-open", [href]);
}
Loading