File size: 5,033 Bytes
0c8b3c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Hono } from "hono";
import { stream } from "hono/streaming";
import { getUpdateState, checkForUpdate, isUpdateInProgress } from "../../update-checker.js";
import { getProxyInfo, canSelfUpdate, checkProxySelfUpdate, applyProxySelfUpdate, isProxyUpdateInProgress, getCachedProxyUpdateResult, getDeployMode } from "../../self-update.js";
import { isEmbedded } from "../../paths.js";

export function createUpdateRoutes(): Hono {
  const app = new Hono();

  app.get("/admin/update-status", (c) => {
    const proxyInfo = getProxyInfo();
    const codexState = getUpdateState();
    const cached = getCachedProxyUpdateResult();

    return c.json({
      proxy: {
        version: proxyInfo.version,
        commit: proxyInfo.commit,
        can_self_update: canSelfUpdate(),
        mode: getDeployMode(),
        commits_behind: cached?.commitsBehind ?? null,
        commits: cached?.commits ?? [],
        release: cached?.release ? { version: cached.release.version, body: cached.release.body, url: cached.release.url } : null,
        update_available: cached?.updateAvailable ?? false,
        update_in_progress: isProxyUpdateInProgress(),
      },
      codex: {
        current_version: codexState?.current_version ?? null,
        current_build: codexState?.current_build ?? null,
        latest_version: codexState?.latest_version ?? null,
        latest_build: codexState?.latest_build ?? null,
        update_available: codexState?.update_available ?? false,
        update_in_progress: isUpdateInProgress(),
        last_check: codexState?.last_check ?? null,
      },
    });
  });

  app.post("/admin/check-update", async (c) => {
    const results: {
      proxy?: {
        commits_behind: number;
        current_commit: string | null;
        latest_commit: string | null;
        commits: Array<{ hash: string; message: string }>;
        release: { version: string; body: string; url: string } | null;
        update_available: boolean;
        mode: string;
        error?: string;
      };
      codex?: { update_available: boolean; current_version: string; latest_version: string | null; version_changed?: boolean; error?: string };
    } = {};

    try {
      const proxyResult = await checkProxySelfUpdate();
      results.proxy = {
        commits_behind: proxyResult.commitsBehind,
        current_commit: proxyResult.currentCommit,
        latest_commit: proxyResult.latestCommit,
        commits: proxyResult.commits,
        release: proxyResult.release ? { version: proxyResult.release.version, body: proxyResult.release.body, url: proxyResult.release.url } : null,
        update_available: proxyResult.updateAvailable,
        mode: proxyResult.mode,
      };
    } catch (err) {
      results.proxy = {
        commits_behind: 0,
        current_commit: null,
        latest_commit: null,
        commits: [],
        release: null,
        update_available: false,
        mode: getDeployMode(),
        error: err instanceof Error ? err.message : String(err),
      };
    }

    if (!isEmbedded()) {
      try {
        const prevVersion = getUpdateState()?.current_version ?? null;
        const codexState = await checkForUpdate();
        results.codex = {
          update_available: codexState.update_available,
          current_version: codexState.current_version,
          latest_version: codexState.latest_version,
          version_changed: prevVersion !== null && codexState.current_version !== prevVersion,
        };
      } catch (err) {
        results.codex = {
          update_available: false,
          current_version: "unknown",
          latest_version: null,
          error: err instanceof Error ? err.message : String(err),
        };
      }
    }

    return c.json({
      ...results,
      proxy_update_in_progress: isProxyUpdateInProgress(),
      codex_update_in_progress: isUpdateInProgress(),
    });
  });

  app.post("/admin/apply-update", async (c) => {
    if (!canSelfUpdate()) {
      const mode = getDeployMode();
      c.status(400);
      return c.json({
        started: false,
        error: "Self-update not available in this deploy mode",
        mode,
        hint: mode === "docker"
          ? "Run: docker compose pull && docker compose up -d (or enable Watchtower for automatic updates)"
          : mode === "electron"
            ? "Updates are handled automatically by the desktop app. Check the system tray for update notifications, or restart the app to trigger a check."
            : "Git is not available in this environment",
      });
    }

    c.header("Content-Type", "text/event-stream");
    c.header("Cache-Control", "no-cache");
    c.header("Connection", "keep-alive");

    return stream(c, async (s) => {
      const send = (data: Record<string, unknown>) => s.write(`data: ${JSON.stringify(data)}\n\n`);

      const result = await applyProxySelfUpdate((step, status, detail) => {
        void send({ step, status, detail });
      });

      await send({ ...result, done: true });
    });
  });

  return app;
}