berget 2.2.5 → 2.2.7

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 (148) hide show
  1. package/.github/workflows/publish.yml +8 -8
  2. package/.github/workflows/test.yml +12 -6
  3. package/.husky/pre-commit +1 -0
  4. package/.prettierignore +15 -0
  5. package/.prettierrc +5 -3
  6. package/CONTRIBUTING.md +38 -0
  7. package/README.md +2 -148
  8. package/dist/index.js +21 -21
  9. package/dist/package.json +30 -2
  10. package/dist/src/agents/app.js +28 -0
  11. package/dist/src/agents/backend.js +25 -0
  12. package/dist/src/agents/devops.js +34 -0
  13. package/dist/src/agents/frontend.js +25 -0
  14. package/dist/src/agents/fullstack.js +25 -0
  15. package/dist/src/agents/index.js +61 -0
  16. package/dist/src/agents/quality.js +70 -0
  17. package/dist/src/agents/security.js +26 -0
  18. package/dist/src/agents/types.js +2 -0
  19. package/dist/src/client.js +54 -62
  20. package/dist/src/commands/api-keys.js +132 -140
  21. package/dist/src/commands/auth.js +9 -9
  22. package/dist/src/commands/autocomplete.js +9 -9
  23. package/dist/src/commands/billing.js +7 -9
  24. package/dist/src/commands/chat.js +90 -92
  25. package/dist/src/commands/clusters.js +12 -12
  26. package/dist/src/commands/code/__tests__/auth-sync.test.js +348 -0
  27. package/dist/src/commands/code/__tests__/fake-api-key-service.js +23 -0
  28. package/dist/src/commands/code/__tests__/fake-auth-service.js +55 -0
  29. package/dist/src/commands/code/__tests__/fake-command-runner.js +50 -0
  30. package/dist/src/commands/code/__tests__/fake-file-store.js +55 -0
  31. package/dist/src/commands/code/__tests__/fake-prompter.js +133 -0
  32. package/dist/src/commands/code/__tests__/setup-flow.test.js +505 -0
  33. package/dist/src/commands/code/adapters/clack-prompter.js +81 -0
  34. package/dist/src/commands/code/adapters/fs-file-store.js +80 -0
  35. package/dist/src/commands/code/adapters/spawn-command-runner.js +53 -0
  36. package/dist/src/commands/code/auth-sync.js +283 -0
  37. package/dist/src/commands/code/errors.js +27 -0
  38. package/dist/src/commands/code/ports/auth-services.js +2 -0
  39. package/dist/src/commands/code/ports/command-runner.js +2 -0
  40. package/dist/src/commands/code/ports/file-store.js +2 -0
  41. package/dist/src/commands/code/ports/prompter.js +2 -0
  42. package/dist/src/commands/code/setup.js +533 -0
  43. package/dist/src/commands/code.js +223 -779
  44. package/dist/src/commands/models.js +13 -15
  45. package/dist/src/commands/users.js +6 -8
  46. package/dist/src/constants/command-structure.js +116 -114
  47. package/dist/src/services/api-key-service.js +43 -48
  48. package/dist/src/services/auth-service.js +60 -299
  49. package/dist/src/services/browser-auth.js +278 -0
  50. package/dist/src/services/chat-service.js +78 -91
  51. package/dist/src/services/cluster-service.js +6 -6
  52. package/dist/src/services/collaborator-service.js +5 -8
  53. package/dist/src/services/flux-service.js +5 -8
  54. package/dist/src/services/helm-service.js +5 -8
  55. package/dist/src/services/kubectl-service.js +7 -10
  56. package/dist/src/utils/config-checker.js +5 -5
  57. package/dist/src/utils/config-loader.js +25 -25
  58. package/dist/src/utils/default-api-key.js +23 -23
  59. package/dist/src/utils/env-manager.js +7 -7
  60. package/dist/src/utils/error-handler.js +60 -61
  61. package/dist/src/utils/logger.js +7 -7
  62. package/dist/src/utils/markdown-renderer.js +2 -2
  63. package/dist/src/utils/opencode-validator.js +17 -20
  64. package/dist/src/utils/token-manager.js +38 -11
  65. package/dist/tests/commands/chat.test.js +24 -24
  66. package/dist/tests/commands/code.test.js +169 -138
  67. package/dist/tests/utils/config-loader.test.js +114 -114
  68. package/dist/tests/utils/env-manager.test.js +57 -57
  69. package/dist/tests/utils/opencode-validator.test.js +44 -43
  70. package/dist/vitest.config.js +1 -1
  71. package/eslint.config.mjs +47 -0
  72. package/index.ts +42 -48
  73. package/package.json +30 -2
  74. package/src/agents/app.ts +27 -0
  75. package/src/agents/backend.ts +24 -0
  76. package/src/agents/devops.ts +33 -0
  77. package/src/agents/frontend.ts +24 -0
  78. package/src/agents/fullstack.ts +24 -0
  79. package/src/agents/index.ts +71 -0
  80. package/src/agents/quality.ts +69 -0
  81. package/src/agents/security.ts +26 -0
  82. package/src/agents/types.ts +17 -0
  83. package/src/client.ts +125 -167
  84. package/src/commands/api-keys.ts +261 -358
  85. package/src/commands/auth.ts +24 -30
  86. package/src/commands/autocomplete.ts +12 -12
  87. package/src/commands/billing.ts +22 -27
  88. package/src/commands/chat.ts +230 -323
  89. package/src/commands/clusters.ts +33 -33
  90. package/src/commands/code/__tests__/auth-sync.test.ts +481 -0
  91. package/src/commands/code/__tests__/fake-api-key-service.ts +13 -0
  92. package/src/commands/code/__tests__/fake-auth-service.ts +50 -0
  93. package/src/commands/code/__tests__/fake-command-runner.ts +44 -0
  94. package/src/commands/code/__tests__/fake-file-store.ts +44 -0
  95. package/src/commands/code/__tests__/fake-prompter.ts +121 -0
  96. package/src/commands/code/__tests__/setup-flow.test.ts +628 -0
  97. package/src/commands/code/adapters/clack-prompter.ts +55 -0
  98. package/src/commands/code/adapters/fs-file-store.ts +37 -0
  99. package/src/commands/code/adapters/spawn-command-runner.ts +40 -0
  100. package/src/commands/code/auth-sync.ts +329 -0
  101. package/src/commands/code/errors.ts +23 -0
  102. package/src/commands/code/ports/auth-services.ts +14 -0
  103. package/src/commands/code/ports/command-runner.ts +10 -0
  104. package/src/commands/code/ports/file-store.ts +7 -0
  105. package/src/commands/code/ports/prompter.ts +29 -0
  106. package/src/commands/code/setup.ts +630 -0
  107. package/src/commands/code.ts +335 -1074
  108. package/src/commands/index.ts +19 -19
  109. package/src/commands/models.ts +32 -37
  110. package/src/commands/users.ts +15 -22
  111. package/src/constants/command-structure.ts +120 -140
  112. package/src/services/api-key-service.ts +96 -113
  113. package/src/services/auth-service.ts +92 -339
  114. package/src/services/browser-auth.ts +296 -0
  115. package/src/services/chat-service.ts +246 -279
  116. package/src/services/cluster-service.ts +29 -32
  117. package/src/services/collaborator-service.ts +13 -18
  118. package/src/services/flux-service.ts +16 -18
  119. package/src/services/helm-service.ts +16 -18
  120. package/src/services/kubectl-service.ts +12 -14
  121. package/src/types/api.d.ts +924 -926
  122. package/src/types/json.d.ts +3 -3
  123. package/src/utils/config-checker.ts +10 -10
  124. package/src/utils/config-loader.ts +110 -127
  125. package/src/utils/default-api-key.ts +81 -93
  126. package/src/utils/env-manager.ts +36 -40
  127. package/src/utils/error-handler.ts +83 -78
  128. package/src/utils/logger.ts +41 -41
  129. package/src/utils/markdown-renderer.ts +11 -11
  130. package/src/utils/opencode-validator.ts +51 -56
  131. package/src/utils/token-manager.ts +84 -64
  132. package/templates/agents/app.md +23 -0
  133. package/templates/agents/backend.md +23 -0
  134. package/templates/agents/devops.md +30 -0
  135. package/templates/agents/frontend.md +25 -0
  136. package/templates/agents/fullstack.md +23 -0
  137. package/templates/agents/quality.md +69 -0
  138. package/templates/agents/security.md +21 -0
  139. package/tests/commands/chat.test.ts +60 -70
  140. package/tests/commands/code.test.ts +346 -345
  141. package/tests/utils/config-loader.test.ts +260 -260
  142. package/tests/utils/env-manager.test.ts +127 -134
  143. package/tests/utils/opencode-validator.test.ts +65 -69
  144. package/tsconfig.json +2 -2
  145. package/vitest.config.ts +3 -3
  146. package/AGENTS.md +0 -374
  147. package/TODO.md +0 -19
  148. package/opencode.json +0 -146
@@ -0,0 +1,278 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ Object.defineProperty(exports, "__esModule", { value: true });
35
+ exports.BrowserAuth = void 0;
36
+ const http = __importStar(require("http"));
37
+ const crypto = __importStar(require("crypto"));
38
+ class BrowserAuth {
39
+ constructor(options) {
40
+ this.options = options;
41
+ }
42
+ start() {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ const { keycloakUrl, realm, clientId, callbackPort, debug } = this.options;
45
+ // Generate PKCE code verifier and challenge
46
+ const codeVerifier = this.generateCodeVerifier();
47
+ const codeChallenge = this.generateCodeChallenge(codeVerifier);
48
+ const state = crypto.randomBytes(16).toString("hex");
49
+ const redirectUri = `http://localhost:${callbackPort}/callback`;
50
+ // Build authorization URL
51
+ const authUrl = new URL(`${keycloakUrl}/realms/${realm}/protocol/openid-connect/auth`);
52
+ authUrl.searchParams.set("client_id", clientId);
53
+ authUrl.searchParams.set("response_type", "code");
54
+ authUrl.searchParams.set("redirect_uri", redirectUri);
55
+ authUrl.searchParams.set("scope", "openid email profile");
56
+ authUrl.searchParams.set("state", state);
57
+ authUrl.searchParams.set("code_challenge", codeChallenge);
58
+ authUrl.searchParams.set("code_challenge_method", "S256");
59
+ // Create a promise that resolves when we receive the callback
60
+ const authResult = yield new Promise(resolve => {
61
+ let resolved = false;
62
+ const sockets = new Set();
63
+ const safeResolve = (result) => {
64
+ if (resolved)
65
+ return;
66
+ resolved = true;
67
+ clearTimeout(timeoutHandle);
68
+ server.close();
69
+ // Force-close all active sockets so the server stops immediately
70
+ for (const socket of sockets) {
71
+ socket.destroy();
72
+ }
73
+ sockets.clear();
74
+ resolve(result);
75
+ };
76
+ const server = http.createServer((req, res) => {
77
+ const requestUrl = new URL(req.url || "", `http://localhost:${callbackPort}`);
78
+ if (requestUrl.pathname === "/callback") {
79
+ const receivedState = requestUrl.searchParams.get("state") || "";
80
+ const code = requestUrl.searchParams.get("code") || "";
81
+ const error = requestUrl.searchParams.get("error") || "";
82
+ const errorPage = (title, message) => `
83
+ <!DOCTYPE html>
84
+ <html lang="en">
85
+ <head>
86
+ <meta charset="UTF-8">
87
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
88
+ <title>Berget - Authentication Failed</title>
89
+ <style>
90
+ * { margin: 0; padding: 0; box-sizing: border-box; }
91
+ body {
92
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
93
+ display: flex;
94
+ justify-content: center;
95
+ align-items: center;
96
+ min-height: 100vh;
97
+ background: linear-gradient(135deg, #0f0f1a 0%, #1a1a2e 50%, #16213e 100%);
98
+ color: #fff;
99
+ }
100
+ .container {
101
+ text-align: center;
102
+ padding: 3rem;
103
+ max-width: 400px;
104
+ }
105
+ .icon {
106
+ width: 80px;
107
+ height: 80px;
108
+ background: linear-gradient(135deg, #f87171 0%, #ef4444 100%);
109
+ border-radius: 50%;
110
+ display: flex;
111
+ align-items: center;
112
+ justify-content: center;
113
+ margin: 0 auto 1.5rem;
114
+ box-shadow: 0 4px 20px rgba(248, 113, 113, 0.3);
115
+ }
116
+ .icon svg { width: 40px; height: 40px; stroke: #fff; stroke-width: 3; }
117
+ h1 { font-size: 1.5rem; font-weight: 600; margin-bottom: 0.75rem; color: #fff; }
118
+ p { color: #94a3b8; font-size: 0.95rem; line-height: 1.5; }
119
+ .brand { margin-top: 2rem; opacity: 0.5; font-size: 0.8rem; letter-spacing: 0.05em; }
120
+ </style>
121
+ </head>
122
+ <body>
123
+ <div class="container">
124
+ <div class="icon">
125
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
126
+ <line x1="18" y1="6" x2="6" y2="18"></line>
127
+ <line x1="6" y1="6" x2="18" y2="18"></line>
128
+ </svg>
129
+ </div>
130
+ <h1>${title}</h1>
131
+ <p>${message}</p>
132
+ <div class="brand">BERGET</div>
133
+ </div>
134
+ </body>
135
+ </html>
136
+ `;
137
+ // Set Connection: close so the browser doesn't keep the socket alive
138
+ // after we respond, and force-end the connection
139
+ if (error) {
140
+ res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", Connection: "close" });
141
+ res.end(errorPage("Authentication Failed", requestUrl.searchParams.get("error_description") || error));
142
+ safeResolve({ success: false, error });
143
+ return;
144
+ }
145
+ if (receivedState !== state) {
146
+ res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", Connection: "close" });
147
+ res.end(errorPage("Authentication Failed", "Invalid state parameter. Please try again."));
148
+ safeResolve({ success: false, error: "Invalid state parameter" });
149
+ return;
150
+ }
151
+ res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", Connection: "close" });
152
+ res.end(`
153
+ <!DOCTYPE html>
154
+ <html lang="en">
155
+ <head>
156
+ <meta charset="UTF-8">
157
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
158
+ <title>Berget - Authentication Successful</title>
159
+ <style>
160
+ * { margin: 0; padding: 0; box-sizing: border-box; }
161
+ body {
162
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
163
+ display: flex;
164
+ justify-content: center;
165
+ align-items: center;
166
+ min-height: 100vh;
167
+ background: linear-gradient(135deg, #0f0f1a 0%, #1a1a2e 50%, #16213e 100%);
168
+ color: #fff;
169
+ }
170
+ .container {
171
+ text-align: center;
172
+ padding: 3rem;
173
+ max-width: 400px;
174
+ }
175
+ .icon {
176
+ width: 80px;
177
+ height: 80px;
178
+ background: linear-gradient(135deg, #4ade80 0%, #22c55e 100%);
179
+ border-radius: 50%;
180
+ display: flex;
181
+ align-items: center;
182
+ justify-content: center;
183
+ margin: 0 auto 1.5rem;
184
+ box-shadow: 0 4px 20px rgba(74, 222, 128, 0.3);
185
+ }
186
+ .icon svg { width: 40px; height: 40px; stroke: #fff; stroke-width: 3; }
187
+ h1 { font-size: 1.5rem; font-weight: 600; margin-bottom: 0.75rem; color: #fff; }
188
+ p { color: #94a3b8; font-size: 0.95rem; line-height: 1.5; }
189
+ .brand { margin-top: 2rem; opacity: 0.5; font-size: 0.8rem; letter-spacing: 0.05em; }
190
+ </style>
191
+ </head>
192
+ <body>
193
+ <div class="container">
194
+ <div class="icon">
195
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
196
+ <polyline points="20 6 9 17 4 12"></polyline>
197
+ </svg>
198
+ </div>
199
+ <h1>Authentication Successful</h1>
200
+ <p>You can close this window and return to your terminal.</p>
201
+ <div class="brand">BERGET</div>
202
+ </div>
203
+ </body>
204
+ </html>
205
+ `);
206
+ safeResolve({ success: true, code });
207
+ }
208
+ });
209
+ // Track sockets so we can destroy them on shutdown
210
+ server.on("connection", (socket) => {
211
+ sockets.add(socket);
212
+ socket.on("close", () => sockets.delete(socket));
213
+ });
214
+ server.listen(callbackPort, () => {
215
+ if (debug) {
216
+ console.log(`Callback server listening on port ${callbackPort}`);
217
+ }
218
+ });
219
+ // Set timeout for the server
220
+ const timeoutHandle = setTimeout(() => {
221
+ safeResolve({ success: false, error: "Authentication timed out" });
222
+ }, 5 * 60 * 1000); // 5 minute timeout
223
+ // Open browser
224
+ (() => __awaiter(this, void 0, void 0, function* () {
225
+ try {
226
+ const open = yield Promise.resolve().then(() => __importStar(require("open"))).then(m => m.default);
227
+ yield open(authUrl.toString());
228
+ }
229
+ catch (_a) {
230
+ // Browser failed to open - user must open URL manually
231
+ }
232
+ }))();
233
+ });
234
+ if (!authResult.success || !authResult.code) {
235
+ return {
236
+ success: false,
237
+ error: authResult.error || "Unknown error",
238
+ };
239
+ }
240
+ // Exchange authorization code for tokens
241
+ const tokenUrl = `${keycloakUrl}/realms/${realm}/protocol/openid-connect/token`;
242
+ const tokenResponse = yield fetch(tokenUrl, {
243
+ method: "POST",
244
+ headers: {
245
+ "Content-Type": "application/x-www-form-urlencoded",
246
+ },
247
+ body: new URLSearchParams({
248
+ grant_type: "authorization_code",
249
+ client_id: clientId,
250
+ code: authResult.code,
251
+ redirect_uri: redirectUri,
252
+ code_verifier: codeVerifier,
253
+ }).toString(),
254
+ });
255
+ if (!tokenResponse.ok) {
256
+ const errorText = yield tokenResponse.text();
257
+ return {
258
+ success: false,
259
+ error: `Failed to exchange code for tokens: ${errorText}`,
260
+ };
261
+ }
262
+ const tokenData = (yield tokenResponse.json());
263
+ return {
264
+ success: true,
265
+ accessToken: tokenData.access_token,
266
+ refreshToken: tokenData.refresh_token,
267
+ expiresIn: tokenData.expires_in,
268
+ };
269
+ });
270
+ }
271
+ generateCodeVerifier() {
272
+ return crypto.randomBytes(32).toString("base64url");
273
+ }
274
+ generateCodeChallenge(verifier) {
275
+ return crypto.createHash("sha256").update(verifier).digest("base64url");
276
+ }
277
+ }
278
+ exports.BrowserAuth = BrowserAuth;