Skip to content

Commit 20eb3d0

Browse files
committed
feat: notify users when a newer release is available
OpenCode resolves plugin versions once and caches them forever (anomalyco/opencode#6774, #10546, #30631) — users never auto-update. On startup the plugin now checks the npm registry (5s timeout, fully silent on failure) and toasts the current → latest version with update instructions when behind.
1 parent c815ec6 commit 20eb3d0

4 files changed

Lines changed: 109 additions & 0 deletions

File tree

‎docs/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- **Update notification** — On startup the plugin checks npm for a newer release and shows a toast with update instructions if one exists (OpenCode pins plugin versions in its cache, so users otherwise never learn about updates). Silent when offline or up-to-date.
13+
1014
## [2.20.1] - 2026-08-02
1115

1216
### Fixed

‎src/index.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
} from "./services/memory-lifecycle.js";
2525
import { getAISessionManager } from "./services/ai/session/ai-session-manager.js";
2626
import { embeddingService } from "./services/embedding.js";
27+
import { checkForUpdate } from "./services/update-checker.js";
2728

2829
import { isConfigured, CONFIG, initConfig } from "./config.js";
2930
import { log } from "./services/logger.js";
@@ -148,6 +149,21 @@ export const OpenCodeMemPlugin: Plugin = async (ctx: PluginInput) => {
148149
}
149150
}
150151

152+
// Notify when a newer release exists (OpenCode pins plugin versions in its
153+
// cache, so users never see updates otherwise). Non-blocking, silent on failure.
154+
if (isConfigured()) {
155+
checkForUpdate().then((update) => {
156+
if (update) {
157+
showToast(ctx, {
158+
title: "opencode-mem0",
159+
message: `Update available: v${update.current} → v${update.latest}. Run: npm install opencode-mem0@latest (or clear ~/.cache/opencode) and restart.`,
160+
variant: "info",
161+
duration: 10000,
162+
});
163+
}
164+
});
165+
}
166+
151167
// Fresh install, not yet activated: stay inert and point at the init CLI.
152168
if (!isConfigured()) {
153169
const NOTICE_KEY = Symbol.for("opencode-mem0.activate.noticed");

‎src/services/update-checker.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pkg from "../../package.json" with { type: "json" };
2+
import { log } from "./logger.js";
3+
4+
const REGISTRY_URL = `https://registry.npmjs.org/${pkg.name}/latest`;
5+
const TIMEOUT_MS = 5000;
6+
7+
/** Returns positive if b is newer than a, 0 if equal, negative if b is older. */
8+
export function compareVersions(a: string, b: string): number {
9+
const pa = (a.split("-")[0] ?? a).split(".").map(Number);
10+
const pb = (b.split("-")[0] ?? b).split(".").map(Number);
11+
for (let i = 0; i < 3; i++) {
12+
const d = (pb[i] || 0) - (pa[i] || 0);
13+
if (d !== 0) return d;
14+
}
15+
return 0;
16+
}
17+
18+
export interface UpdateInfo {
19+
current: string;
20+
latest: string;
21+
}
22+
23+
export type FetchLike = (
24+
url: string,
25+
init?: { signal?: AbortSignal; headers?: Record<string, string> }
26+
) => Promise<{ ok: boolean; json: () => Promise<unknown> }>;
27+
28+
/** Checks npm for a newer release. Returns null if up-to-date or on any failure. */
29+
export async function checkForUpdate(fetchFn: FetchLike = fetch): Promise<UpdateInfo | null> {
30+
try {
31+
const res = await fetchFn(REGISTRY_URL, {
32+
signal: AbortSignal.timeout(TIMEOUT_MS),
33+
headers: { Accept: "application/json" },
34+
});
35+
if (!res.ok) return null;
36+
const data = (await res.json()) as { version?: string };
37+
const latest = data.version;
38+
if (!latest || compareVersions(pkg.version, latest) <= 0) return null;
39+
return { current: pkg.version, latest };
40+
} catch (error) {
41+
log("Update check failed (offline or registry unreachable)", { error: String(error) });
42+
return null;
43+
}
44+
}

‎tests/update-checker.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { describe, it, expect, vi } from "vitest";
2+
import { compareVersions, checkForUpdate } from "../src/services/update-checker.js";
3+
4+
describe("compareVersions", () => {
5+
it("detects newer patch/minor/major", () => {
6+
expect(compareVersions("2.20.1", "2.20.2")).toBeGreaterThan(0);
7+
expect(compareVersions("2.20.1", "2.21.0")).toBeGreaterThan(0);
8+
expect(compareVersions("2.20.1", "3.0.0")).toBeGreaterThan(0);
9+
});
10+
11+
it("returns 0 for equal and negative for older", () => {
12+
expect(compareVersions("2.20.1", "2.20.1")).toBe(0);
13+
expect(compareVersions("2.20.1", "2.20.0")).toBeLessThan(0);
14+
});
15+
16+
it("handles missing segments and ignores prerelease suffixes", () => {
17+
expect(compareVersions("2.20", "2.20.1")).toBeGreaterThan(0);
18+
// Prereleases are ignored: we only compare against npm's stable 'latest'
19+
expect(compareVersions("2.20.1-beta", "2.20.1")).toBe(0);
20+
expect(compareVersions("2.20.1", "2.20.1-rc.1")).toBe(0);
21+
});
22+
});
23+
24+
describe("checkForUpdate", () => {
25+
const jsonResponse = (version: string) =>
26+
({ ok: true, json: async () => ({ version }) }) as Response;
27+
28+
it("returns update info when registry has a newer version", async () => {
29+
const info = await checkForUpdate(vi.fn().mockResolvedValue(jsonResponse("999.0.0")));
30+
expect(info?.latest).toBe("999.0.0");
31+
expect(info?.current).toMatch(/^\d+\.\d+\.\d+/);
32+
});
33+
34+
it("returns null when up-to-date or older", async () => {
35+
expect(await checkForUpdate(vi.fn().mockResolvedValue(jsonResponse("0.0.1")))).toBeNull();
36+
});
37+
38+
it("returns null on network failure or bad response", async () => {
39+
expect(await checkForUpdate(vi.fn().mockRejectedValue(new Error("offline")))).toBeNull();
40+
expect(await checkForUpdate(vi.fn().mockResolvedValue({ ok: false }))).toBeNull();
41+
expect(
42+
await checkForUpdate(vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }))
43+
).toBeNull();
44+
});
45+
});

0 commit comments

Comments
 (0)