File size: 5,621 Bytes
064bfd6 | 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | /**
* Flagged plugin tracking utilities
*
* Tracks plugins that were auto-removed because they were delisted from
* their marketplace. Data is stored in ~/.claude/plugins/flagged-plugins.json.
* Flagged plugins appear in a "Flagged" section in /plugins until the user
* dismisses them.
*
* Uses a module-level cache so that getFlaggedPlugins() can be called
* synchronously during React render. The cache is populated on the first
* async call (loadFlaggedPlugins or addFlaggedPlugin) and kept in sync
* with writes.
*/
import { randomBytes } from 'crypto'
import { readFile, rename, unlink, writeFile } from 'fs/promises'
import { join } from 'path'
import { logForDebugging } from '../debug.js'
import { getFsImplementation } from '../fsOperations.js'
import { logError } from '../log.js'
import { jsonParse, jsonStringify } from '../slowOperations.js'
import { getPluginsDirectory } from './pluginDirectories.js'
const FLAGGED_PLUGINS_FILENAME = 'flagged-plugins.json'
export type FlaggedPlugin = {
flaggedAt: string
seenAt?: string
}
const SEEN_EXPIRY_MS = 48 * 60 * 60 * 1000 // 48 hours
// Module-level cache — populated by loadFlaggedPlugins(), updated by writes.
let cache: Record<string, FlaggedPlugin> | null = null
function getFlaggedPluginsPath(): string {
return join(getPluginsDirectory(), FLAGGED_PLUGINS_FILENAME)
}
function parsePluginsData(content: string): Record<string, FlaggedPlugin> {
const parsed = jsonParse(content) as unknown
if (
typeof parsed !== 'object' ||
parsed === null ||
!('plugins' in parsed) ||
typeof (parsed as { plugins: unknown }).plugins !== 'object' ||
(parsed as { plugins: unknown }).plugins === null
) {
return {}
}
const plugins = (parsed as { plugins: Record<string, unknown> }).plugins
const result: Record<string, FlaggedPlugin> = {}
for (const [id, entry] of Object.entries(plugins)) {
if (
entry &&
typeof entry === 'object' &&
'flaggedAt' in entry &&
typeof (entry as { flaggedAt: unknown }).flaggedAt === 'string'
) {
const parsed: FlaggedPlugin = {
flaggedAt: (entry as { flaggedAt: string }).flaggedAt,
}
if (
'seenAt' in entry &&
typeof (entry as { seenAt: unknown }).seenAt === 'string'
) {
parsed.seenAt = (entry as { seenAt: string }).seenAt
}
result[id] = parsed
}
}
return result
}
async function readFromDisk(): Promise<Record<string, FlaggedPlugin>> {
try {
const content = await readFile(getFlaggedPluginsPath(), {
encoding: 'utf-8',
})
return parsePluginsData(content)
} catch {
return {}
}
}
async function writeToDisk(
plugins: Record<string, FlaggedPlugin>,
): Promise<void> {
const filePath = getFlaggedPluginsPath()
const tempPath = `${filePath}.${randomBytes(8).toString('hex')}.tmp`
try {
await getFsImplementation().mkdir(getPluginsDirectory())
const content = jsonStringify({ plugins }, null, 2)
await writeFile(tempPath, content, {
encoding: 'utf-8',
mode: 0o600,
})
await rename(tempPath, filePath)
cache = plugins
} catch (error) {
logError(error)
try {
await unlink(tempPath)
} catch {
// Ignore cleanup errors
}
}
}
/**
* Load flagged plugins from disk into the module cache.
* Must be called (and awaited) before getFlaggedPlugins() returns
* meaningful data. Called by useManagePlugins during plugin refresh.
*/
export async function loadFlaggedPlugins(): Promise<void> {
const all = await readFromDisk()
const now = Date.now()
let changed = false
for (const [id, entry] of Object.entries(all)) {
if (
entry.seenAt &&
now - new Date(entry.seenAt).getTime() >= SEEN_EXPIRY_MS
) {
delete all[id]
changed = true
}
}
cache = all
if (changed) {
await writeToDisk(all)
}
}
/**
* Get all flagged plugins from the in-memory cache.
* Returns an empty object if loadFlaggedPlugins() has not been called yet.
*/
export function getFlaggedPlugins(): Record<string, FlaggedPlugin> {
return cache ?? {}
}
/**
* Add a plugin to the flagged list.
*
* @param pluginId "name@marketplace" format
*/
export async function addFlaggedPlugin(pluginId: string): Promise<void> {
if (cache === null) {
cache = await readFromDisk()
}
const updated = {
...cache,
[pluginId]: {
flaggedAt: new Date().toISOString(),
},
}
await writeToDisk(updated)
logForDebugging(`Flagged plugin: ${pluginId}`)
}
/**
* Mark flagged plugins as seen. Called when the Installed view renders
* flagged plugins. Sets seenAt on entries that don't already have it.
* After 48 hours from seenAt, entries are auto-cleared on next load.
*/
export async function markFlaggedPluginsSeen(
pluginIds: string[],
): Promise<void> {
if (cache === null) {
cache = await readFromDisk()
}
const now = new Date().toISOString()
let changed = false
const updated = { ...cache }
for (const id of pluginIds) {
const entry = updated[id]
if (entry && !entry.seenAt) {
updated[id] = { ...entry, seenAt: now }
changed = true
}
}
if (changed) {
await writeToDisk(updated)
}
}
/**
* Remove a plugin from the flagged list. Called when the user dismisses
* a flagged plugin notification in /plugins.
*/
export async function removeFlaggedPlugin(pluginId: string): Promise<void> {
if (cache === null) {
cache = await readFromDisk()
}
if (!(pluginId in cache)) return
const { [pluginId]: _, ...rest } = cache
cache = rest
await writeToDisk(rest)
}
|