| 1943 | SELECT COUNT(*) as count FROM models |
| 1944 | WHERE ${type} = ? AND ${type} IS NOT NULL AND ${type} != '' |
| 1945 | `).get(newName); |
| 1946 | const existingCount = existing ? existing.count : 0; |
| 1947 | const result = db.prepare(`UPDATE models SET ${type} = ? WHERE ${type} = ?`).run(newName, oldName); |
| 1948 | return { |
| 1949 | success: true, |
| 1950 | updated: result.changes, |
| 1951 | merged: existingCount > 0, |
| 1952 | existingCount |
| 1953 | }; |
| 1954 | } |
| 1955 | |
| 1956 | function deleteMetadataForMcp(args) { |
| 1957 | const type = String(args && args.type || '').trim(); |
| 1958 | const name = String(args && args.name || '').trim(); |
| 1959 | if (!MCP_METADATA_TYPES.has(type)) throw new Error('type must be designer, parentModel, or license'); |
| 1960 | if (!name) throw new Error('name is required'); |
| 1961 | const result = db.prepare(`UPDATE models SET ${type} = NULL WHERE ${type} = ?`).run(name); |
| 1962 | return { success: true, updated: result.changes }; |
| 1963 | } |
| 1964 | |
| 1965 | function requireMcpConfirm(args, action) { |
| 1966 | if (!args || args.confirm !== true) { |
| 1967 | throw new Error(`Refusing ${action}. Pass confirm: true to proceed.`); |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | function mcpIpcEvent() { |
| 1972 | return { sender: { send() {} } }; |
| 1973 | } |
| 1974 | |
| 1975 | function filtersFromMcpArgs(args) { |
| 1976 | if (!args) return null; |
| 1977 | const filters = {}; |
| 1978 | for (const key of ['search', 'designer', 'tags', 'directory', 'fileType', 'printed']) { |
| 1979 | if (args[key] !== undefined && args[key] !== null && args[key] !== '') filters[key] = args[key]; |
| 1980 | } |
| 1981 | return Object.keys(filters).length ? filters : null; |
| 1982 | } |
| 1983 | |
| 1984 | function resolveMcpFilePaths(args) { |
| 1985 | const paths = []; |
| 1986 | const seen = new Set(); |
| 1987 | const addPath = (filePath) => { |
| 1988 | const p = String(filePath || '').trim(); |
| 1989 | if (!p || seen.has(p)) return; |
| 1990 | seen.add(p); |
| 1991 | paths.push(p); |
| 1992 | }; |
| 1993 | if (Array.isArray(args && args.filePaths)) args.filePaths.forEach(addPath); |
| 1994 | if (args && args.filePath) addPath(args.filePath); |
| 1995 | const ids = []; |
| 1996 | if (args && args.id != null) ids.push(args.id); |
| 1997 | if (Array.isArray(args && args.ids)) ids.push(...args.ids); |
| 1998 | for (const raw of ids) { |
| 1999 | const id = Number(raw); |
| 2000 | if (!Number.isInteger(id) || id <= 0) continue; |
| 2001 | const model = getModelById(id); |
| 2002 | if (model) addPath(model.filePath); |