forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
170 lines (147 loc) · 4.87 KB
/
Copy pathindex.ts
File metadata and controls
170 lines (147 loc) · 4.87 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import type { AutoUpdateCheckerOptions } from "./types";
import { getCachedVersion, getLocalDevVersion, findPluginEntry, getLatestVersion, updatePinnedVersion } from "./checker";
import { invalidatePackage } from "./cache";
import { PACKAGE_NAME } from "./constants";
import { logAutoUpdate } from "./logging";
interface PluginClient {
tui: {
showToast(options: {
body: {
title?: string;
message: string;
variant: "info" | "warning" | "success" | "error";
duration?: number;
};
}): Promise<unknown>;
};
}
interface SessionCreatedEvent {
type: "session.created";
properties?: {
info?: {
parentID?: string;
};
};
}
type PluginEvent = SessionCreatedEvent | { type: string; properties?: unknown };
export function createAutoUpdateCheckerHook(
client: PluginClient,
directory: string,
options: AutoUpdateCheckerOptions = {}
) {
const { showStartupToast = true, autoUpdate = true } = options;
let hasChecked = false;
return {
event: ({ event }: { event: PluginEvent }) => {
if (event.type !== "session.created") return;
if (hasChecked) return;
const props = event.properties as { info?: { parentID?: string } } | undefined;
if (props?.info?.parentID) return;
hasChecked = true;
setTimeout(() => {
const localDevVersion = getLocalDevVersion(directory);
if (localDevVersion) {
if (showStartupToast) {
showLocalDevToast(client, localDevVersion).catch(() => {});
}
logAutoUpdate("Local development mode");
return;
}
runBackgroundUpdateCheck(client, directory, autoUpdate).catch((err) => {
logAutoUpdate(`Background update check failed: ${err}`);
});
}, 0);
},
};
}
async function runBackgroundUpdateCheck(
client: PluginClient,
directory: string,
autoUpdate: boolean
): Promise<void> {
const pluginInfo = findPluginEntry(directory);
if (!pluginInfo) {
logAutoUpdate("Plugin not found in config");
return;
}
const cachedVersion = getCachedVersion();
const currentVersion = cachedVersion ?? pluginInfo.pinnedVersion;
if (!currentVersion) {
logAutoUpdate("No version found (cached or pinned)");
return;
}
if (currentVersion.includes('-')) {
logAutoUpdate(`Prerelease version (${currentVersion}), skipping auto-update`);
return;
}
const latestVersion = await getLatestVersion();
if (!latestVersion) {
logAutoUpdate("Failed to fetch latest version");
return;
}
if (currentVersion === latestVersion) {
logAutoUpdate("Already on latest version");
return;
}
logAutoUpdate(`Update available: ${currentVersion} → ${latestVersion}`);
if (!autoUpdate) {
await showUpdateAvailableToast(client, latestVersion);
logAutoUpdate("Auto-update disabled, notification only");
return;
}
if (pluginInfo.isPinned) {
const updated = updatePinnedVersion(pluginInfo.configPath, pluginInfo.entry, latestVersion);
if (updated) {
invalidatePackage(PACKAGE_NAME);
await showAutoUpdatedToast(client, currentVersion, latestVersion);
logAutoUpdate(`Config updated: ${pluginInfo.entry} → ${PACKAGE_NAME}@${latestVersion}`);
} else {
await showUpdateAvailableToast(client, latestVersion);
}
} else {
invalidatePackage(PACKAGE_NAME);
await showUpdateAvailableToast(client, latestVersion);
}
}
async function showUpdateAvailableToast(client: PluginClient, latestVersion: string): Promise<void> {
await client.tui
.showToast({
body: {
title: `Antigravity Auth Update`,
message: `v${latestVersion} available. Restart OpenCode to apply.`,
variant: "info" as const,
duration: 8000,
},
})
.catch(() => {});
logAutoUpdate(`Update available toast shown: v${latestVersion}`);
}
async function showAutoUpdatedToast(client: PluginClient, oldVersion: string, newVersion: string): Promise<void> {
await client.tui
.showToast({
body: {
title: `Antigravity Auth Updated!`,
message: `v${oldVersion} → v${newVersion}\nRestart OpenCode to apply.`,
variant: "success" as const,
duration: 8000,
},
})
.catch(() => {});
logAutoUpdate(`Auto-updated toast shown: v${oldVersion} → v${newVersion}`);
}
async function showLocalDevToast(client: PluginClient, version: string): Promise<void> {
await client.tui
.showToast({
body: {
title: `Antigravity Auth ${version} (dev)`,
message: "Running in local development mode.",
variant: "warning" as const,
duration: 5000,
},
})
.catch(() => {});
logAutoUpdate(`Local dev toast shown: v${version}`);
}
export type { UpdateCheckResult, AutoUpdateCheckerOptions } from "./types";
export { checkForUpdate, getCachedVersion, getLatestVersion } from "./checker";
export { invalidatePackage, invalidateCache } from "./cache";