Skip to content

armgit / kokon

Public Imported from GitHub
0

Edit photos by describing the change, on your own GPU. A free, open-source alternative to Nano Banana and ChatGPT image editing: photos stay on your computer, no daily limit. One installer for Windows, Mac and Linux sets up the best open model (Qwen-Image 2.1). MCP server for AI agents.

Browser: a page reloads itself once when the computer running Kokon has updated to another version

Gabriele Lozupone7ebc397329fe
diff --git a/docs/remote-access.md b/docs/remote-access.md
index d75b595..1ff1234 100644
--- a/docs/remote-access.md
+++ b/docs/remote-access.md
@@ -28,7 +28,8 @@ macOS). Allow it on **private** networks only.
It works while Kokon is open on that computer. To keep it running without the window, use a headless
server or Docker (below).
-**Changing the password** signs every browser out. **Remove** turns access off and deletes the
+A browser stays signed in when Kokon restarts or updates; after an update the page reloads itself
+with the new version. **Changing the password** signs every browser out. **Remove** turns access off and deletes the
password. Forgot it? Set a new one in the same place.
### On a server without a screen (Linux)
diff --git a/src/lib/logic.test.ts b/src/lib/logic.test.ts
index 9fb1871..73961ee 100644
--- a/src/lib/logic.test.ts
+++ b/src/lib/logic.test.ts
@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
import {
activeLoras, activeOverrides, buildRequest, composing, editing, reviewRequest, wantsPromptReview, toggleLora, MAX_LORAS, jobPercent, compareBase, composerFromRecord, enhancerApplies, diffPixels, emptyComposer, expansionPixels, expansionPreset, jobLabel, jobSecondsLeft, lutInfo, moveRef, needsPrompt, normalizeQuality, persistentAdvanced, pipelineSummary,
planPipeline, remainingSeconds, sizeForWidth, timeAgo, upscaleRequest, upscaleTargets, versionLabels, type JobClock, type Ref,
- neighbourAfterRemoval, previewIndex, starPromptDue, STAR_PROMPT_AFTER, formatBytes, formatDuration, installItemShort, installItemText, installProgress, installStep, installTimeLeft, timelineLinks, undoTarget, aspectLabel, parseRatio, sizeForRatio, expandToRatio, expandToSize, arcLayout,
+ neighbourAfterRemoval, pageOutdated, previewIndex, starPromptDue, STAR_PROMPT_AFTER, formatBytes, formatDuration, installItemShort, installItemText, installProgress, installStep, installTimeLeft, timelineLinks, undoTarget, aspectLabel, parseRatio, sizeForRatio, expandToRatio, expandToSize, arcLayout,
} from "./logic";
import type { GenerationRecord, InstallItem, JobView, LoraItem, Plan } from "./types";
@@ -549,3 +549,15 @@ describe("star prompt", () => {
expect(starPromptDue(STAR_PROMPT_AFTER + 3, false)).toBe(true);
});
});
+
+describe("page version", () => {
+ it("reloads once when the service runs another version", () => {
+ expect(pageOutdated("0.11.0", "0.11.0", null)).toBe(false);
+ expect(pageOutdated("0.11.0", "0.11.1", null)).toBe(true);
+ expect(pageOutdated("0.11.0", "0.11.1", "0.11.1")).toBe(false);
+ // a later update gets its own reload
+ expect(pageOutdated("0.11.1", "0.11.2", "0.11.1")).toBe(true);
+ expect(pageOutdated("0.11.0", undefined, null)).toBe(false);
+ expect(pageOutdated("", "0.11.0", null)).toBe(false);
+ });
+});
diff --git a/src/lib/logic.ts b/src/lib/logic.ts
index 35d028f..db6b83b 100644
--- a/src/lib/logic.ts
+++ b/src/lib/logic.ts
@@ -868,3 +868,11 @@ export const STAR_PROMPT_AFTER = 5;
export function starPromptDue(made: number, busy: boolean): boolean {
return made >= STAR_PROMPT_AFTER && !busy;
}
+
+/** Browser on another device: whether to reload because the page was built for another Kokon
+ * than the service now runs (the app on the server computer updated). `reloadedFor`: the service
+ * version this tab already reloaded for, so a page that still differs afterwards (an old copy
+ * in a cache) does not reload again and again. */
+export function pageOutdated(pageVersion: string, serviceVersion: string | undefined, reloadedFor: string | null): boolean {
+ return !!pageVersion && !!serviceVersion && pageVersion !== serviceVersion && reloadedFor !== serviceVersion;
+}
diff --git a/src/lib/store.ts b/src/lib/store.ts
index 88b9f56..f80b8c3 100644
--- a/src/lib/store.ts
+++ b/src/lib/store.ts
@@ -5,11 +5,11 @@
// every project is kept as ids in `projects` (persisted) and re-loaded from the service when
// opened again.
-import { isNarrow } from "./platform";
+import { inBrowser, isNarrow } from "./platform";
import { useSyncExternalStore } from "react";
import { markFresh } from "../components/Cocoon";
import { api, ApiError, setPrivateInputs, subscribe } from "./api";
-import { activeLoras, buildRequest, composing, emptyComposer, needsPrompt, newKey, persistentAdvanced, refFromGen, refFromInput, reviewRequest, starPromptDue, timelineLinks, undoTarget, versionLabels, wantsPromptReview, type CompareMode, type Composer, type Ref } from "./logic";
+import { activeLoras, buildRequest, composing, emptyComposer, needsPrompt, newKey, pageOutdated, persistentAdvanced, refFromGen, refFromInput, reviewRequest, starPromptDue, timelineLinks, undoTarget, versionLabels, wantsPromptReview, type CompareMode, type Composer, type Ref } from "./logic";
import type { AppErrorBody, GenerationRecord, GenerationRequest, InputRecord, JobView, LoraItem, ServerState, StatusResponse } from "./types";
export type Overlay = null | "gallery" | "settings" | "inpaint" | "expand" | "welcome" | "whats-new" | "prompt-tips" | "shortcuts";
@@ -395,12 +395,35 @@ export async function refreshStatus() {
}
}
setState({ status: st, server: { state: st.server.state, owned: st.server.owned, error: st.server.error } });
+ if (inBrowser) reloadIfOutdated(st.service.version);
return st;
} catch {
return null;
}
}
+const RELOADED_FOR_KEY = "kokon.reloadedFor";
+
+/** Browser: the app on the server computer updated while this page was open (or cached), so it
+ * runs the old UI against the new service: load the new one, once per version (logic.ts
+ * `pageOutdated`). The desktop app updates its UI and service together. */
+function reloadIfOutdated(serviceVersion: string) {
+ let reloadedFor: string | null = null;
+ try {
+ reloadedFor = sessionStorage.getItem(RELOADED_FOR_KEY);
+ } catch {
+ /* storage unavailable: the write below fails too, and then no reload */
+ }
+ if (!pageOutdated(__KOKON_VERSION__, serviceVersion, reloadedFor)) return;
+ try {
+ sessionStorage.setItem(RELOADED_FOR_KEY, serviceVersion);
+ } catch {
+ // Without storage a reload could loop: stay on this page.
+ return;
+ }
+ location.reload();
+}
+
const TEXT_GEN_KEY = "kokon.textGeneration";
let knownTextGeneration = (() => {
try {
diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts
new file mode 100644
index 0000000..0dba969
--- /dev/null
+++ b/src/vite-env.d.ts
@@ -0,0 +1,2 @@
+/** Kokon's version (package.json), set by vite.config.ts at build time. */
+declare const __KOKON_VERSION__: string;
diff --git a/tsconfig.json b/tsconfig.json
index 97634fd..e10dd04 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -11,6 +11,7 @@
"skipLibCheck": true,
"isolatedModules": true,
"noEmit": true,
+ "resolveJsonModule": true,
"types": ["vite/client"]
},
"include": ["src", "vite.config.ts"]
diff --git a/vite.config.ts b/vite.config.ts
index 9ac15fa..31cf5a7 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -1,9 +1,12 @@
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
+import pkg from "./package.json" with { type: "json" };
// Tauri expects a fixed dev port and no browser auto-open.
export default defineConfig({
plugins: [react()],
+ // A browser page compares it with the service's after an app update (store.ts refreshStatus).
+ define: { __KOKON_VERSION__: JSON.stringify(pkg.version) },
clearScreen: false,
// warmup: compile the app as soon as the dev server starts, not when the window asks for it.
server: { port: 1420, strictPort: true, host: "127.0.0.1", warmup: { clientFiles: ["./src/main.tsx"] } },