sovkora 0.0.0-stage → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +9 -0
  3. package/README.md +67 -2
  4. package/dist/commands/agent.command.js +267 -0
  5. package/dist/commands/chat.command.js +214 -0
  6. package/dist/commands/chat.handlers.js +150 -0
  7. package/dist/commands/chat.types.js +44 -0
  8. package/dist/commands/cinema.command.js +643 -0
  9. package/dist/commands/config.command.js +228 -0
  10. package/dist/commands/db.command.js +114 -0
  11. package/dist/commands/demo.command.js +55 -0
  12. package/dist/commands/dev.command.js +462 -0
  13. package/dist/commands/docs.command.js +135 -0
  14. package/dist/commands/doctor.command.js +119 -0
  15. package/dist/commands/forgot.command.js +57 -0
  16. package/dist/commands/generate.command.js +376 -0
  17. package/dist/commands/graph.command.js +54 -0
  18. package/dist/commands/init.command.js +380 -0
  19. package/dist/commands/init.steps.js +115 -0
  20. package/dist/commands/install.command.js +459 -0
  21. package/dist/commands/login.command.js +110 -0
  22. package/dist/commands/logs.command.js +215 -0
  23. package/dist/commands/logs.steps.js +130 -0
  24. package/dist/commands/mcp.command.js +104 -0
  25. package/dist/commands/models.command.js +164 -0
  26. package/dist/commands/onboard.command.js +236 -0
  27. package/dist/commands/pack.command.js +255 -0
  28. package/dist/commands/profile.command.js +51 -0
  29. package/dist/commands/recover.command.js +489 -0
  30. package/dist/commands/register.command.js +94 -0
  31. package/dist/commands/release.command.js +99 -0
  32. package/dist/commands/reset.command.js +79 -0
  33. package/dist/commands/runs.command.js +201 -0
  34. package/dist/commands/settings.command.js +85 -0
  35. package/dist/commands/start.command.js +590 -0
  36. package/dist/commands/status.command.js +167 -0
  37. package/dist/commands/stop.command.js +313 -0
  38. package/dist/commands/studio.command.js +266 -0
  39. package/dist/commands/test.command.js +134 -0
  40. package/dist/commands/verify.command.js +52 -0
  41. package/dist/commands/video.command.js +73 -0
  42. package/dist/commands/workspace-guard.js +72 -0
  43. package/dist/index.js +207 -0
  44. package/dist/renderers.js +250 -0
  45. package/dist/services/api-client.js +150 -0
  46. package/dist/services/auth.api.js +87 -0
  47. package/dist/services/chat.api.js +117 -0
  48. package/dist/services/cinema-estimate.json +42 -0
  49. package/dist/services/cinema.childsafe.js +52 -0
  50. package/dist/services/cinema.director.js +285 -0
  51. package/dist/services/cinema.production.js +417 -0
  52. package/dist/services/cinema.staging.js +323 -0
  53. package/dist/services/cinema.story.js +758 -0
  54. package/dist/services/cinema.treatment.js +393 -0
  55. package/dist/services/composer.run.js +94 -0
  56. package/dist/services/demo/demo.documents.js +108 -0
  57. package/dist/services/demo/demo.guard.js +49 -0
  58. package/dist/services/demo/demo.persona.js +114 -0
  59. package/dist/services/demo/demo.seed.js +281 -0
  60. package/dist/services/diagnostics.js +327 -0
  61. package/dist/services/image-model-recipes.json +93 -0
  62. package/dist/services/image-models.js +152 -0
  63. package/dist/services/local-store.js +145 -0
  64. package/dist/services/mcp.api.js +38 -0
  65. package/dist/services/model-catalog.js +70 -0
  66. package/dist/services/notify.js +55 -0
  67. package/dist/services/pack.api.js +23 -0
  68. package/dist/services/pack.bundle.js +241 -0
  69. package/dist/services/pack.sources.js +101 -0
  70. package/dist/services/release-manifest.js +266 -0
  71. package/dist/services/runs.view.js +80 -0
  72. package/dist/services/runs.wait.js +38 -0
  73. package/dist/services/semver.js +55 -0
  74. package/dist/services/settings.api.js +108 -0
  75. package/dist/services/string-utils.js +70 -0
  76. package/dist/services/studio.api.js +120 -0
  77. package/dist/services/templates.js +893 -0
  78. package/dist/services/tool-installer.js +353 -0
  79. package/dist/services/tool-path.js +72 -0
  80. package/dist/services/version-manager.js +253 -0
  81. package/dist/services/workspace-bootstrap.js +68 -0
  82. package/dist/threads.js +202 -0
  83. package/dist/types/api.types.js +6 -0
  84. package/dist/types/local.types.js +6 -0
  85. package/dist/ui/box.js +46 -0
  86. package/dist/ui/logger.js +28 -0
  87. package/dist/ui/platform.js +32 -0
  88. package/dist/ui/prompts.js +130 -0
  89. package/dist/ui/table.js +33 -0
  90. package/dist/utils/config.js +150 -0
  91. package/dist/utils/development-status.js +22 -0
  92. package/dist/utils/endpoints.js +41 -0
  93. package/dist/utils/health.js +94 -0
  94. package/dist/utils/pid.js +83 -0
  95. package/dist/utils/platform.js +358 -0
  96. package/dist/utils/process.js +139 -0
  97. package/dist/utils/state-dir.js +105 -0
  98. package/package.json +95 -4
@@ -0,0 +1,353 @@
1
+ "use strict";
2
+ /**
3
+ * @file tool-installer.ts
4
+ * @description Centralized tool detection, version verification, and guided
5
+ * installation service. Platform-aware — provides macOS Homebrew, Linux apt,
6
+ * and manual download hints for each required tool.
7
+ */
8
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
+ if (k2 === undefined) k2 = k;
10
+ var desc = Object.getOwnPropertyDescriptor(m, k);
11
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
+ desc = { enumerable: true, get: function() { return m[k]; } };
13
+ }
14
+ Object.defineProperty(o, k2, desc);
15
+ }) : (function(o, m, k, k2) {
16
+ if (k2 === undefined) k2 = k;
17
+ o[k2] = m[k];
18
+ }));
19
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
21
+ }) : function(o, v) {
22
+ o["default"] = v;
23
+ });
24
+ var __importStar = (this && this.__importStar) || (function () {
25
+ var ownKeys = function(o) {
26
+ ownKeys = Object.getOwnPropertyNames || function (o) {
27
+ var ar = [];
28
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
29
+ return ar;
30
+ };
31
+ return ownKeys(o);
32
+ };
33
+ return function (mod) {
34
+ if (mod && mod.__esModule) return mod;
35
+ var result = {};
36
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
37
+ __setModuleDefault(result, mod);
38
+ return result;
39
+ };
40
+ })();
41
+ var __importDefault = (this && this.__importDefault) || function (mod) {
42
+ return (mod && mod.__esModule) ? mod : { "default": mod };
43
+ };
44
+ Object.defineProperty(exports, "__esModule", { value: true });
45
+ exports.getRequiredTools = getRequiredTools;
46
+ exports.checkTool = checkTool;
47
+ exports.detectAllTools = detectAllTools;
48
+ exports.installViaBrew = installViaBrew;
49
+ exports.setupMediaWorkerVenv = setupMediaWorkerVenv;
50
+ exports.resolveMediaWorkerPython = resolveMediaWorkerPython;
51
+ exports.formatToolResult = formatToolResult;
52
+ const node_child_process_1 = require("node:child_process");
53
+ const fs = __importStar(require("node:fs"));
54
+ const path = __importStar(require("node:path"));
55
+ const chalk_1 = __importDefault(require("chalk"));
56
+ const platform_1 = require("../utils/platform");
57
+ const config_1 = require("../utils/config");
58
+ /** All required tools for Sovkora. */
59
+ function getRequiredTools() {
60
+ const tools = [
61
+ {
62
+ name: "Docker",
63
+ command: "docker",
64
+ versionCmd: "docker --version",
65
+ minVersion: "20.0.0",
66
+ description: "Container runtime for middleware (Postgres, Redis, RabbitMQ)",
67
+ critical: true,
68
+ brewFormula: "docker",
69
+ aptPackage: "docker.io",
70
+ downloadUrl: "https://www.docker.com/products/docker-desktop",
71
+ category: "infra",
72
+ },
73
+ {
74
+ name: "Docker Compose",
75
+ command: "docker",
76
+ versionCmd: "docker compose version",
77
+ minVersion: "2.0.0",
78
+ description: "Multi-container orchestration",
79
+ critical: true,
80
+ downloadUrl: "https://docs.docker.com/compose/install/",
81
+ category: "infra",
82
+ },
83
+ {
84
+ name: "Java 21",
85
+ command: "java",
86
+ versionCmd: "java --version 2>&1",
87
+ minVersion: "21.0.0",
88
+ description: "JVM runtime for the Router and workers",
89
+ critical: true,
90
+ brewFormula: "openjdk@21",
91
+ aptPackage: "openjdk-21-jdk",
92
+ downloadUrl: "https://adoptium.net",
93
+ category: "runtime",
94
+ },
95
+ {
96
+ name: "Node.js",
97
+ command: "node",
98
+ versionCmd: "node --version",
99
+ minVersion: `${String(config_1.NODE_MIN_MAJOR)}.0.0`,
100
+ description: "JavaScript runtime for CLI and Next.js UI",
101
+ critical: true,
102
+ brewFormula: "node",
103
+ aptPackage: "nodejs",
104
+ downloadUrl: "https://nodejs.org",
105
+ category: "runtime",
106
+ },
107
+ {
108
+ name: "Ollama",
109
+ command: "ollama",
110
+ versionCmd: "ollama --version",
111
+ minVersion: "0.1.0",
112
+ description: "Local LLM inference engine — an AI engine, optional: the platform and its infrastructure run without it",
113
+ critical: false,
114
+ brewFormula: "ollama",
115
+ downloadUrl: "https://ollama.ai",
116
+ category: "ai",
117
+ },
118
+ {
119
+ name: "Python 3",
120
+ command: "python3",
121
+ versionCmd: "python3 --version",
122
+ minVersion: "3.11.0",
123
+ description: "Required for the media generation worker",
124
+ critical: false,
125
+ brewFormula: "python@3.11",
126
+ aptPackage: "python3.11",
127
+ downloadUrl: "https://python.org",
128
+ category: "runtime",
129
+ },
130
+ {
131
+ name: "FFmpeg",
132
+ command: "ffmpeg",
133
+ versionCmd: "ffmpeg -version",
134
+ minVersion: "5.0.0",
135
+ description: "Video encoding for cinematic output",
136
+ critical: false,
137
+ brewFormula: "ffmpeg",
138
+ aptPackage: "ffmpeg",
139
+ downloadUrl: "https://ffmpeg.org/download.html",
140
+ category: "media",
141
+ },
142
+ {
143
+ name: "Git",
144
+ command: "git",
145
+ versionCmd: "git --version",
146
+ minVersion: "2.30.0",
147
+ description: "Version control",
148
+ critical: false,
149
+ brewFormula: "git",
150
+ aptPackage: "git",
151
+ downloadUrl: "https://git-scm.com",
152
+ category: "build",
153
+ },
154
+ ];
155
+ // Add LocalAI only on macOS (M1/M2/M3)
156
+ if (process.platform === "darwin") {
157
+ tools.push({
158
+ name: "LocalAI",
159
+ command: "local-ai",
160
+ versionCmd: "local-ai --version",
161
+ minVersion: "1.0.0",
162
+ description: "GPU-accelerated image generation (Metal MPS on Apple Silicon)",
163
+ critical: false,
164
+ brewFormula: "localai",
165
+ downloadUrl: "https://localai.io/docs/getting-started/",
166
+ category: "ai",
167
+ });
168
+ }
169
+ return tools;
170
+ }
171
+ /**
172
+ * Extracts a semver-like version from a version string.
173
+ * Handles formats like "v20.1.0", "Docker version 20.10.7", "openjdk 21.0.1", etc.
174
+ */
175
+ function extractVersion(versionStr) {
176
+ const match = /(\d{1,9})\.(\d{1,9})\.?(\d{0,9})/.exec(versionStr);
177
+ if (!match)
178
+ return null;
179
+ return `${match[1]}.${match[2]}.${match[3] || "0"}`;
180
+ }
181
+ /**
182
+ * Compares two semver strings. Returns true if actual >= required.
183
+ */
184
+ function isVersionSatisfied(actual, required) {
185
+ const aParts = actual.split(".").map(Number);
186
+ const rParts = required.split(".").map(Number);
187
+ for (let i = 0; i < 3; i++) {
188
+ const a = aParts[i] ?? 0;
189
+ const r = rParts[i] ?? 0;
190
+ if (a > r)
191
+ return true;
192
+ if (a < r)
193
+ return false;
194
+ }
195
+ return true; // Equal
196
+ }
197
+ /**
198
+ * Returns a platform-aware install hint for a tool.
199
+ */
200
+ function getInstallHint(tool) {
201
+ if (process.platform === "darwin" && tool.brewFormula) {
202
+ return `brew install ${tool.brewFormula}`;
203
+ }
204
+ if (process.platform === "linux" && tool.aptPackage) {
205
+ return `sudo apt-get install ${tool.aptPackage}`;
206
+ }
207
+ return tool.downloadUrl;
208
+ }
209
+ /**
210
+ * Checks a single tool's availability and version.
211
+ */
212
+ function checkTool(tool) {
213
+ const available = (0, platform_1.hasTool)(tool.command);
214
+ if (!available) {
215
+ return {
216
+ tool,
217
+ available: false,
218
+ version: null,
219
+ versionOk: false,
220
+ installHint: getInstallHint(tool),
221
+ };
222
+ }
223
+ let version;
224
+ let versionOk;
225
+ try {
226
+ const raw = (0, node_child_process_1.execSync)(tool.versionCmd, { stdio: ["pipe", "pipe", "pipe"] })
227
+ .toString()
228
+ .trim()
229
+ .split("\n")[0] ?? "";
230
+ version = extractVersion(raw);
231
+ versionOk = version ? isVersionSatisfied(version, tool.minVersion) : true;
232
+ }
233
+ catch {
234
+ version = "unknown";
235
+ versionOk = true; // Can't verify, assume OK
236
+ }
237
+ return {
238
+ tool,
239
+ available: true,
240
+ version,
241
+ versionOk,
242
+ installHint: getInstallHint(tool),
243
+ };
244
+ }
245
+ /**
246
+ * Runs a full tool scan and returns a structured report.
247
+ */
248
+ function detectAllTools() {
249
+ const tools = getRequiredTools();
250
+ const results = tools.map(checkTool);
251
+ const missingCritical = results.filter((r) => r.tool.critical && (!r.available || !r.versionOk));
252
+ const missingOptional = results.filter((r) => !r.tool.critical && (!r.available || !r.versionOk));
253
+ return {
254
+ results,
255
+ allCriticalMet: missingCritical.length === 0,
256
+ missingCritical,
257
+ missingOptional,
258
+ };
259
+ }
260
+ /**
261
+ * Attempts to install a tool via Homebrew (macOS only).
262
+ * Returns true on success, false on failure.
263
+ */
264
+ function installViaBrew(formula) {
265
+ if (process.platform !== "darwin")
266
+ return false;
267
+ if (!(0, platform_1.hasTool)("brew"))
268
+ return false;
269
+ try {
270
+ (0, node_child_process_1.execSync)(`brew install ${formula}`, { stdio: "inherit" });
271
+ return true;
272
+ }
273
+ catch {
274
+ return false;
275
+ }
276
+ }
277
+ /** Absolute path to the native Python media worker (orazaka-worker-media). */
278
+ function mediaWorkerDir(workspaceRoot) {
279
+ return path.join(workspaceRoot, "workers", "media");
280
+ }
281
+ /**
282
+ * Sets up a Python virtual environment for the media worker.
283
+ * Creates .venv if it doesn't exist and installs requirements.
284
+ */
285
+ function setupMediaWorkerVenv(workspaceRoot) {
286
+ const workerDir = mediaWorkerDir(workspaceRoot);
287
+ const requirementsFile = path.join(workerDir, "requirements.txt");
288
+ const venvPath = path.join(workerDir, ".venv");
289
+ if (!fs.existsSync(requirementsFile)) {
290
+ return {
291
+ success: false,
292
+ venvPath,
293
+ error: "requirements.txt not found in media worker directory",
294
+ };
295
+ }
296
+ if (!(0, platform_1.hasTool)("python3")) {
297
+ return {
298
+ success: false,
299
+ venvPath,
300
+ error: "python3 not found on PATH",
301
+ };
302
+ }
303
+ try {
304
+ // Create venv if it doesn't exist
305
+ if (!fs.existsSync(venvPath)) {
306
+ (0, node_child_process_1.execSync)(`python3 -m venv "${venvPath}"`, {
307
+ cwd: workerDir,
308
+ stdio: "pipe",
309
+ });
310
+ }
311
+ // Install requirements
312
+ const pipPath = path.join(venvPath, "bin", "pip");
313
+ (0, node_child_process_1.execSync)(`"${pipPath}" install -r requirements.txt`, {
314
+ cwd: workerDir,
315
+ stdio: "pipe",
316
+ });
317
+ return { success: true, venvPath };
318
+ }
319
+ catch (err) {
320
+ return {
321
+ success: false,
322
+ venvPath,
323
+ error: err instanceof Error ? err.message : "Unknown error",
324
+ };
325
+ }
326
+ }
327
+ /**
328
+ * Resolves the Python executable for the media worker.
329
+ * Prefers .venv/bin/python3, falls back to system python3.
330
+ */
331
+ function resolveMediaWorkerPython(workspaceRoot) {
332
+ const venvPython = path.join(mediaWorkerDir(workspaceRoot), ".venv", "bin", "python3");
333
+ if (fs.existsSync(venvPython)) {
334
+ return venvPython;
335
+ }
336
+ if ((0, platform_1.hasTool)("python3")) {
337
+ return "python3";
338
+ }
339
+ return null;
340
+ }
341
+ /**
342
+ * Formats a ToolCheckResult for display.
343
+ */
344
+ function formatToolResult(result) {
345
+ if (result.available && result.versionOk) {
346
+ return `${chalk_1.default.green("✔")} ${result.tool.name.padEnd(18)} ${chalk_1.default.gray(result.version ?? "installed")}`;
347
+ }
348
+ if (result.available && !result.versionOk) {
349
+ return `${chalk_1.default.yellow("⚠")} ${result.tool.name.padEnd(18)} ${chalk_1.default.yellow(result.version ?? "?")} ${chalk_1.default.gray("(need " + result.tool.minVersion + "+)")}`;
350
+ }
351
+ const icon = result.tool.critical ? chalk_1.default.red("✖") : chalk_1.default.yellow("○");
352
+ return `${icon} ${result.tool.name.padEnd(18)} ${chalk_1.default.gray("not found")} → ${chalk_1.default.cyan(result.installHint)}`;
353
+ }
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ /**
3
+ * @file tool-path.ts
4
+ * @description The absolute path of a program the CLI spawns (SonarCloud S4036: a program found through PATH can be
5
+ * replaced by whoever controls PATH). Only the running node's own folder (npm and npx sit beside it, with nvm as on a
6
+ * CI runner) and the system and package-manager folders below are searched, in that order, never PATH: the same
7
+ * folders as the workspace scripts' scripts/lib/tool-path.mjs.
8
+ */
9
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ var desc = Object.getOwnPropertyDescriptor(m, k);
12
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
13
+ desc = { enumerable: true, get: function() { return m[k]; } };
14
+ }
15
+ Object.defineProperty(o, k2, desc);
16
+ }) : (function(o, m, k, k2) {
17
+ if (k2 === undefined) k2 = k;
18
+ o[k2] = m[k];
19
+ }));
20
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
22
+ }) : function(o, v) {
23
+ o["default"] = v;
24
+ });
25
+ var __importStar = (this && this.__importStar) || (function () {
26
+ var ownKeys = function(o) {
27
+ ownKeys = Object.getOwnPropertyNames || function (o) {
28
+ var ar = [];
29
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
30
+ return ar;
31
+ };
32
+ return ownKeys(o);
33
+ };
34
+ return function (mod) {
35
+ if (mod && mod.__esModule) return mod;
36
+ var result = {};
37
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
38
+ __setModuleDefault(result, mod);
39
+ return result;
40
+ };
41
+ })();
42
+ Object.defineProperty(exports, "__esModule", { value: true });
43
+ exports.TRUSTED_TOOL_FOLDERS = void 0;
44
+ exports.toolPath = toolPath;
45
+ const fs = __importStar(require("node:fs"));
46
+ const path = __importStar(require("node:path"));
47
+ exports.TRUSTED_TOOL_FOLDERS = [
48
+ path.dirname(process.execPath),
49
+ "/opt/homebrew/bin",
50
+ "/usr/local/bin",
51
+ "/usr/bin",
52
+ "/bin",
53
+ "/usr/sbin",
54
+ "/sbin",
55
+ ];
56
+ /** The first executable named `name` in the trusted folders; throws when there is none. */
57
+ function toolPath(name, search = exports.TRUSTED_TOOL_FOLDERS) {
58
+ if (!/^[a-z0-9._-]+$/i.test(name))
59
+ throw new Error(`${name} is not a program name`);
60
+ for (const dir of search) {
61
+ const candidate = path.join(dir, name);
62
+ try {
63
+ fs.accessSync(candidate, fs.constants.X_OK);
64
+ if (fs.statSync(candidate).isFile())
65
+ return candidate;
66
+ }
67
+ catch {
68
+ // not in this folder
69
+ }
70
+ }
71
+ throw new Error(`${name} not found in ${search.join(", ")}: install it and retry`);
72
+ }
@@ -0,0 +1,253 @@
1
+ "use strict";
2
+ /**
3
+ * @file version-manager.ts
4
+ * @description Version management system for Sovkora CLI.
5
+ * Checks for updates, manages version caching, and displays notifications.
6
+ * Inspired by npm, yarn, and GitHub CLI best practices.
7
+ */
8
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
+ if (k2 === undefined) k2 = k;
10
+ var desc = Object.getOwnPropertyDescriptor(m, k);
11
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
+ desc = { enumerable: true, get: function() { return m[k]; } };
13
+ }
14
+ Object.defineProperty(o, k2, desc);
15
+ }) : (function(o, m, k, k2) {
16
+ if (k2 === undefined) k2 = k;
17
+ o[k2] = m[k];
18
+ }));
19
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
21
+ }) : function(o, v) {
22
+ o["default"] = v;
23
+ });
24
+ var __importStar = (this && this.__importStar) || (function () {
25
+ var ownKeys = function(o) {
26
+ ownKeys = Object.getOwnPropertyNames || function (o) {
27
+ var ar = [];
28
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
29
+ return ar;
30
+ };
31
+ return ownKeys(o);
32
+ };
33
+ return function (mod) {
34
+ if (mod && mod.__esModule) return mod;
35
+ var result = {};
36
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
37
+ __setModuleDefault(result, mod);
38
+ return result;
39
+ };
40
+ })();
41
+ var __importDefault = (this && this.__importDefault) || function (mod) {
42
+ return (mod && mod.__esModule) ? mod : { "default": mod };
43
+ };
44
+ Object.defineProperty(exports, "__esModule", { value: true });
45
+ exports.VersionManager = void 0;
46
+ const fs = __importStar(require("node:fs"));
47
+ const path = __importStar(require("node:path"));
48
+ const state_dir_1 = require("../utils/state-dir");
49
+ const chalk_1 = __importDefault(require("chalk"));
50
+ const node_child_process_1 = require("node:child_process");
51
+ /** The npm package the CLI is published as. */
52
+ const PACKAGE_NAME = "sovkora";
53
+ class VersionManager {
54
+ cacheDir;
55
+ cachePath;
56
+ currentVersion;
57
+ cacheExpiry = 24 * 60 * 60 * 1000; // 24 hours
58
+ constructor(currentVersion) {
59
+ this.currentVersion = currentVersion;
60
+ this.cachePath = (0, state_dir_1.statePath)("version-cache.json");
61
+ this.cacheDir = path.dirname(this.cachePath);
62
+ this.ensureCacheDir();
63
+ }
64
+ /**
65
+ * Ensure cache directory exists.
66
+ */
67
+ ensureCacheDir() {
68
+ if (!fs.existsSync(this.cacheDir)) {
69
+ fs.mkdirSync(this.cacheDir, { recursive: true });
70
+ }
71
+ }
72
+ /**
73
+ * Get current version from package.json.
74
+ */
75
+ getCurrentVersion() {
76
+ return this.currentVersion;
77
+ }
78
+ /**
79
+ * Read cached version data.
80
+ */
81
+ readCache() {
82
+ try {
83
+ if (!fs.existsSync(this.cachePath)) {
84
+ return null;
85
+ }
86
+ const data = fs.readFileSync(this.cachePath, "utf-8");
87
+ const cache = JSON.parse(data);
88
+ // Check if cache is still valid
89
+ const now = Date.now();
90
+ if (now - cache.timestamp > this.cacheExpiry) {
91
+ return null;
92
+ }
93
+ return cache;
94
+ }
95
+ catch {
96
+ return null;
97
+ }
98
+ }
99
+ /**
100
+ * Write cache file.
101
+ */
102
+ writeCache(data) {
103
+ try {
104
+ fs.writeFileSync(this.cachePath, JSON.stringify(data, null, 2), "utf-8");
105
+ }
106
+ catch {
107
+ // Silently fail - don't break CLI if cache fails
108
+ }
109
+ }
110
+ /**
111
+ * Fetch latest version from npm registry.
112
+ * Timeout: 3 seconds to avoid blocking startup.
113
+ */
114
+ async fetchLatestVersion() {
115
+ try {
116
+ const result = (0, node_child_process_1.execSync)(`npm view ${PACKAGE_NAME} version --json 2>/dev/null || echo null`, {
117
+ timeout: 3000,
118
+ encoding: "utf-8",
119
+ }).trim();
120
+ if (result === "null" || !result) {
121
+ return null;
122
+ }
123
+ return result.replaceAll('"', "");
124
+ }
125
+ catch {
126
+ return null;
127
+ }
128
+ }
129
+ /**
130
+ * Compare two semantic versions.
131
+ * Returns: -1 (v1 < v2), 0 (equal), 1 (v1 > v2)
132
+ */
133
+ compareVersions(v1, v2) {
134
+ const parts1 = v1.split(".").map((x) => Number.parseInt(x, 10));
135
+ const parts2 = v2.split(".").map((x) => Number.parseInt(x, 10));
136
+ for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
137
+ const p1 = parts1[i] || 0;
138
+ const p2 = parts2[i] || 0;
139
+ if (p1 < p2)
140
+ return -1;
141
+ if (p1 > p2)
142
+ return 1;
143
+ }
144
+ return 0;
145
+ }
146
+ /**
147
+ * Determine if update is critical (major version bump).
148
+ */
149
+ isCriticalUpdate(current, latest) {
150
+ const currentMajor = Number.parseInt(current.split(".")[0], 10);
151
+ const latestMajor = Number.parseInt(latest.split(".")[0], 10);
152
+ return latestMajor > currentMajor;
153
+ }
154
+ /**
155
+ * Check for available updates (with caching).
156
+ */
157
+ async checkForUpdates() {
158
+ // Try cache first
159
+ const cached = this.readCache();
160
+ if (cached) {
161
+ const isOutdated = this.compareVersions(this.currentVersion, cached.latest) < 0;
162
+ return {
163
+ current: this.currentVersion,
164
+ latest: cached.latest,
165
+ isOutdated,
166
+ isCritical: isOutdated && this.isCriticalUpdate(this.currentVersion, cached.latest),
167
+ changelog: cached.changelog,
168
+ };
169
+ }
170
+ // Fetch latest version
171
+ const latest = await this.fetchLatestVersion();
172
+ if (!latest) {
173
+ // Return current version if fetch fails
174
+ return {
175
+ current: this.currentVersion,
176
+ latest: this.currentVersion,
177
+ isOutdated: false,
178
+ isCritical: false,
179
+ };
180
+ }
181
+ // Cache the result
182
+ this.writeCache({
183
+ timestamp: Date.now(),
184
+ current: this.currentVersion,
185
+ latest,
186
+ });
187
+ const isOutdated = this.compareVersions(this.currentVersion, latest) < 0;
188
+ return {
189
+ current: this.currentVersion,
190
+ latest,
191
+ isOutdated,
192
+ isCritical: isOutdated && this.isCriticalUpdate(this.currentVersion, latest),
193
+ };
194
+ }
195
+ /**
196
+ * Build update notification lines (inspired by npm, yarn, homebrew).
197
+ * Returns formatted lines for the caller (command layer) to display.
198
+ */
199
+ formatUpdateNotification(versionInfo) {
200
+ if (!versionInfo.isOutdated) {
201
+ return [];
202
+ }
203
+ const lines = [""];
204
+ if (versionInfo.isCritical) {
205
+ // Critical update: Red box
206
+ lines.push(chalk_1.default.red.bold("╭──────────────────────────────────────────────────────────────╮"), chalk_1.default.red.bold("│") +
207
+ chalk_1.default.red(" ⚠️ CRITICAL UPDATE AVAILABLE ") +
208
+ chalk_1.default.red.bold("│"), chalk_1.default.red.bold("│") +
209
+ chalk_1.default.red(` Your version: ${versionInfo.current.padEnd(8)} Latest: ${versionInfo.latest} `) +
210
+ chalk_1.default.red.bold("│"), chalk_1.default.red.bold("│") +
211
+ chalk_1.default.red(" ") +
212
+ chalk_1.default.red.bold("│"), chalk_1.default.red.bold("│") +
213
+ chalk_1.default.red(" Update immediately to get critical fixes and features: ") +
214
+ chalk_1.default.red.bold("│"), chalk_1.default.red.bold("│") + chalk_1.default.red(` $ npm install -g ${PACKAGE_NAME}@latest`.padEnd(62)) + chalk_1.default.red.bold("│"), chalk_1.default.red.bold("╰──────────────────────────────────────────────────────────────╯"));
215
+ }
216
+ else {
217
+ // Regular update: Yellow box
218
+ lines.push(chalk_1.default.yellow("╭──────────────────────────────────────────────────────────────╮"), chalk_1.default.yellow("│") +
219
+ chalk_1.default.yellow(" 📦 New version available: " + versionInfo.latest.padEnd(35) + " ") +
220
+ chalk_1.default.yellow("│"), chalk_1.default.yellow("│") +
221
+ chalk_1.default.yellow(` Run: $ npm install -g ${PACKAGE_NAME}@${versionInfo.latest}`.padEnd(62) + " ") +
222
+ chalk_1.default.yellow("│"), chalk_1.default.yellow("╰──────────────────────────────────────────────────────────────╯"));
223
+ }
224
+ lines.push("");
225
+ return lines;
226
+ }
227
+ /**
228
+ * Clear version cache (for testing or manual reset).
229
+ */
230
+ clearCache() {
231
+ try {
232
+ if (fs.existsSync(this.cachePath)) {
233
+ fs.unlinkSync(this.cachePath);
234
+ }
235
+ }
236
+ catch {
237
+ // Silently fail
238
+ }
239
+ }
240
+ /**
241
+ * Get cache file path (for testing).
242
+ */
243
+ getCachePath() {
244
+ return this.cachePath;
245
+ }
246
+ /**
247
+ * Suppress version check for this session (for CI/CD).
248
+ */
249
+ isSuppressed() {
250
+ return process.env.ORAZAKA_SKIP_VERSION_CHECK === "true";
251
+ }
252
+ }
253
+ exports.VersionManager = VersionManager;