berget 2.2.7 → 2.2.8
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.
- package/.github/workflows/publish.yml +6 -6
- package/.github/workflows/test.yml +1 -1
- package/.prettierrc +5 -3
- package/dist/index.js +24 -25
- package/dist/package.json +5 -3
- package/dist/src/agents/app.js +8 -8
- package/dist/src/agents/backend.js +3 -3
- package/dist/src/agents/devops.js +8 -8
- package/dist/src/agents/frontend.js +3 -3
- package/dist/src/agents/fullstack.js +3 -3
- package/dist/src/agents/index.js +18 -18
- package/dist/src/agents/quality.js +8 -8
- package/dist/src/agents/security.js +8 -8
- package/dist/src/client.js +115 -127
- package/dist/src/commands/api-keys.js +195 -202
- package/dist/src/commands/auth.js +16 -25
- package/dist/src/commands/autocomplete.js +8 -8
- package/dist/src/commands/billing.js +10 -19
- package/dist/src/commands/chat.js +139 -170
- package/dist/src/commands/clusters.js +21 -30
- package/dist/src/commands/code/__tests__/auth-sync.test.js +189 -186
- package/dist/src/commands/code/__tests__/fake-api-key-service.js +3 -13
- package/dist/src/commands/code/__tests__/fake-auth-service.js +21 -29
- package/dist/src/commands/code/__tests__/fake-command-runner.js +22 -33
- package/dist/src/commands/code/__tests__/fake-file-store.js +19 -41
- package/dist/src/commands/code/__tests__/fake-prompter.js +81 -97
- package/dist/src/commands/code/__tests__/setup-flow.test.js +295 -295
- package/dist/src/commands/code/adapters/clack-prompter.js +15 -32
- package/dist/src/commands/code/adapters/fs-file-store.js +25 -44
- package/dist/src/commands/code/adapters/spawn-command-runner.js +27 -41
- package/dist/src/commands/code/auth-sync.js +215 -228
- package/dist/src/commands/code/errors.js +15 -12
- package/dist/src/commands/code/setup.js +390 -425
- package/dist/src/commands/code.js +279 -294
- package/dist/src/commands/index.js +5 -5
- package/dist/src/commands/models.js +16 -25
- package/dist/src/commands/users.js +9 -18
- package/dist/src/constants/command-structure.js +138 -138
- package/dist/src/services/api-key-service.js +132 -152
- package/dist/src/services/auth-service.js +81 -95
- package/dist/src/services/browser-auth.js +121 -131
- package/dist/src/services/chat-service.js +369 -386
- package/dist/src/services/cluster-service.js +47 -62
- package/dist/src/services/collaborator-service.js +9 -21
- package/dist/src/services/flux-service.js +13 -25
- package/dist/src/services/helm-service.js +9 -21
- package/dist/src/services/kubectl-service.js +15 -29
- package/dist/src/utils/config-checker.js +7 -7
- package/dist/src/utils/config-loader.js +109 -109
- package/dist/src/utils/default-api-key.js +129 -139
- package/dist/src/utils/env-manager.js +55 -66
- package/dist/src/utils/error-handler.js +62 -62
- package/dist/src/utils/logger.js +74 -67
- package/dist/src/utils/markdown-renderer.js +28 -28
- package/dist/src/utils/opencode-validator.js +67 -69
- package/dist/src/utils/token-manager.js +67 -65
- package/dist/tests/commands/chat.test.js +30 -39
- package/dist/tests/commands/code.test.js +186 -195
- package/dist/tests/utils/config-loader.test.js +107 -107
- package/dist/tests/utils/env-manager.test.js +81 -90
- package/dist/tests/utils/opencode-validator.test.js +42 -41
- package/dist/vitest.config.js +1 -1
- package/eslint.config.mjs +50 -30
- package/index.ts +30 -31
- package/package.json +5 -3
- package/src/agents/app.ts +9 -9
- package/src/agents/backend.ts +4 -4
- package/src/agents/devops.ts +9 -9
- package/src/agents/frontend.ts +4 -4
- package/src/agents/fullstack.ts +4 -4
- package/src/agents/index.ts +27 -25
- package/src/agents/quality.ts +9 -9
- package/src/agents/security.ts +9 -9
- package/src/agents/types.ts +10 -10
- package/src/client.ts +85 -77
- package/src/commands/api-keys.ts +190 -185
- package/src/commands/auth.ts +15 -14
- package/src/commands/autocomplete.ts +10 -10
- package/src/commands/billing.ts +13 -12
- package/src/commands/chat.ts +145 -142
- package/src/commands/clusters.ts +20 -19
- package/src/commands/code/__tests__/auth-sync.test.ts +176 -175
- package/src/commands/code/__tests__/fake-api-key-service.ts +2 -2
- package/src/commands/code/__tests__/fake-auth-service.ts +18 -18
- package/src/commands/code/__tests__/fake-command-runner.ts +28 -22
- package/src/commands/code/__tests__/fake-file-store.ts +15 -15
- package/src/commands/code/__tests__/fake-prompter.ts +86 -85
- package/src/commands/code/__tests__/setup-flow.test.ts +253 -251
- package/src/commands/code/adapters/clack-prompter.ts +32 -30
- package/src/commands/code/adapters/fs-file-store.ts +18 -17
- package/src/commands/code/adapters/spawn-command-runner.ts +20 -15
- package/src/commands/code/auth-sync.ts +210 -210
- package/src/commands/code/errors.ts +11 -11
- package/src/commands/code/ports/auth-services.ts +7 -7
- package/src/commands/code/ports/command-runner.ts +2 -2
- package/src/commands/code/ports/file-store.ts +3 -3
- package/src/commands/code/ports/prompter.ts +13 -13
- package/src/commands/code/setup.ts +408 -406
- package/src/commands/code.ts +288 -287
- package/src/commands/index.ts +11 -10
- package/src/commands/models.ts +19 -18
- package/src/commands/users.ts +11 -10
- package/src/constants/command-structure.ts +159 -159
- package/src/services/api-key-service.ts +85 -85
- package/src/services/auth-service.ts +55 -54
- package/src/services/browser-auth.ts +62 -62
- package/src/services/chat-service.ts +169 -170
- package/src/services/cluster-service.ts +28 -28
- package/src/services/collaborator-service.ts +6 -6
- package/src/services/flux-service.ts +17 -17
- package/src/services/helm-service.ts +11 -11
- package/src/services/kubectl-service.ts +12 -12
- package/src/types/api.d.ts +1933 -1933
- package/src/types/json.d.ts +1 -1
- package/src/utils/config-checker.ts +6 -6
- package/src/utils/config-loader.ts +130 -129
- package/src/utils/default-api-key.ts +81 -80
- package/src/utils/env-manager.ts +37 -37
- package/src/utils/error-handler.ts +64 -64
- package/src/utils/logger.ts +72 -66
- package/src/utils/markdown-renderer.ts +28 -28
- package/src/utils/opencode-validator.ts +72 -71
- package/src/utils/token-manager.ts +69 -68
- package/tests/commands/chat.test.ts +32 -31
- package/tests/commands/code.test.ts +182 -181
- package/tests/utils/config-loader.test.ts +111 -110
- package/tests/utils/env-manager.test.ts +83 -79
- package/tests/utils/opencode-validator.test.ts +43 -42
- package/tsconfig.json +2 -1
- package/vitest.config.ts +2 -2
package/dist/src/client.js
CHANGED
|
@@ -1,45 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
14
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
6
|
exports.createAuthenticatedClient = exports.clearAuthToken = exports.saveAuthToken = exports.getAuthToken = exports.apiClient = exports.API_BASE_URL = void 0;
|
|
16
|
-
const openapi_fetch_1 = __importDefault(require("openapi-fetch"));
|
|
17
7
|
const chalk_1 = __importDefault(require("chalk"));
|
|
18
|
-
const
|
|
8
|
+
const openapi_fetch_1 = __importDefault(require("openapi-fetch"));
|
|
19
9
|
const logger_1 = require("./utils/logger");
|
|
10
|
+
const token_manager_1 = require("./utils/token-manager");
|
|
20
11
|
// API Base URL
|
|
21
12
|
// Use --local flag to test against local API
|
|
22
13
|
// Use --stage flag to test against stage API
|
|
23
|
-
const isLocalMode = process.argv.includes(
|
|
24
|
-
const isStageMode = process.argv.includes(
|
|
14
|
+
const isLocalMode = process.argv.includes('--local');
|
|
15
|
+
const isStageMode = process.argv.includes('--stage');
|
|
25
16
|
exports.API_BASE_URL = process.env.BERGET_API_URL ||
|
|
26
17
|
(isLocalMode
|
|
27
|
-
?
|
|
18
|
+
? 'http://localhost:3000'
|
|
28
19
|
: isStageMode
|
|
29
|
-
?
|
|
30
|
-
:
|
|
20
|
+
? 'https://api.stage.berget.ai'
|
|
21
|
+
: 'https://api.berget.ai'); // production default
|
|
31
22
|
if (isLocalMode && !process.env.BERGET_API_URL) {
|
|
32
|
-
logger_1.logger.debug(
|
|
23
|
+
logger_1.logger.debug('Using local API endpoint: http://localhost:3000');
|
|
33
24
|
}
|
|
34
25
|
else if (isStageMode && !process.env.BERGET_API_URL) {
|
|
35
|
-
logger_1.logger.debug(
|
|
26
|
+
logger_1.logger.debug('Using stage API endpoint: https://api.stage.berget.ai');
|
|
36
27
|
}
|
|
37
28
|
// Create a typed client for the Berget API
|
|
38
29
|
exports.apiClient = (0, openapi_fetch_1.default)({
|
|
39
30
|
baseUrl: exports.API_BASE_URL,
|
|
40
31
|
headers: {
|
|
41
|
-
|
|
42
|
-
|
|
32
|
+
Accept: 'application/json',
|
|
33
|
+
'Content-Type': 'application/json',
|
|
43
34
|
},
|
|
44
35
|
});
|
|
45
36
|
// Authentication functions
|
|
@@ -62,46 +53,45 @@ exports.clearAuthToken = clearAuthToken;
|
|
|
62
53
|
const createAuthenticatedClient = () => {
|
|
63
54
|
const tokenManager = token_manager_1.TokenManager.getInstance();
|
|
64
55
|
if (!tokenManager.getAccessToken()) {
|
|
65
|
-
logger_1.logger.debug(
|
|
56
|
+
logger_1.logger.debug('No authentication token found. Please run `berget auth login` first.');
|
|
66
57
|
}
|
|
67
58
|
// Create the base client
|
|
68
59
|
const client = (0, openapi_fetch_1.default)({
|
|
69
60
|
baseUrl: exports.API_BASE_URL,
|
|
70
61
|
headers: tokenManager.getAccessToken()
|
|
71
62
|
? {
|
|
63
|
+
Accept: 'application/json',
|
|
72
64
|
Authorization: `Bearer ${tokenManager.getAccessToken()}`,
|
|
73
|
-
|
|
74
|
-
Accept: "application/json",
|
|
65
|
+
'Content-Type': 'application/json',
|
|
75
66
|
}
|
|
76
67
|
: {
|
|
77
|
-
|
|
78
|
-
|
|
68
|
+
Accept: 'application/json',
|
|
69
|
+
'Content-Type': 'application/json',
|
|
79
70
|
},
|
|
80
71
|
});
|
|
81
72
|
// Wrap the client to handle token refresh
|
|
82
73
|
return new Proxy(client, {
|
|
83
|
-
get(target,
|
|
74
|
+
get(target, property) {
|
|
84
75
|
// For HTTP methods (GET, POST, etc.), add token refresh logic
|
|
85
|
-
if (typeof target[
|
|
86
|
-
[
|
|
87
|
-
return (...
|
|
88
|
-
var _a, _b, _c, _d;
|
|
76
|
+
if (typeof target[property] === 'function' &&
|
|
77
|
+
['DELETE', 'GET', 'PATCH', 'POST', 'PUT'].includes(String(property))) {
|
|
78
|
+
return async (...arguments_) => {
|
|
89
79
|
// Check if token is expired before making the request
|
|
90
80
|
if (tokenManager.isTokenExpired() && tokenManager.getRefreshToken()) {
|
|
91
|
-
|
|
81
|
+
await refreshAccessToken(tokenManager);
|
|
92
82
|
}
|
|
93
83
|
// Update the Authorization header with the current token
|
|
94
|
-
if (!
|
|
95
|
-
if (!
|
|
96
|
-
|
|
97
|
-
if (!
|
|
98
|
-
|
|
99
|
-
|
|
84
|
+
if (!arguments_[1]?.headers?.Authorization && tokenManager.getAccessToken()) {
|
|
85
|
+
if (!arguments_[1])
|
|
86
|
+
arguments_[1] = {};
|
|
87
|
+
if (!arguments_[1].headers)
|
|
88
|
+
arguments_[1].headers = {};
|
|
89
|
+
arguments_[1].headers.Authorization = `Bearer ${tokenManager.getAccessToken()}`;
|
|
100
90
|
}
|
|
101
91
|
// Make the original request
|
|
102
92
|
let result;
|
|
103
93
|
try {
|
|
104
|
-
result =
|
|
94
|
+
result = await target[property](...arguments_);
|
|
105
95
|
}
|
|
106
96
|
catch (requestError) {
|
|
107
97
|
logger_1.logger.debug(`Request error: ${requestError instanceof Error ? requestError.message : String(requestError)}`);
|
|
@@ -117,138 +107,136 @@ const createAuthenticatedClient = () => {
|
|
|
117
107
|
let isAuthError = false;
|
|
118
108
|
try {
|
|
119
109
|
// Standard 401 Unauthorized
|
|
120
|
-
if (typeof result.error ===
|
|
110
|
+
if (typeof result.error === 'object' && result.error.status === 401) {
|
|
121
111
|
isAuthError = true;
|
|
122
112
|
}
|
|
123
113
|
// OAuth specific errors
|
|
124
114
|
else if (result.error.error &&
|
|
125
|
-
(result.error.error.code ===
|
|
126
|
-
result.error.error.code ===
|
|
127
|
-
result.error.error.message ===
|
|
128
|
-
|
|
129
|
-
|
|
115
|
+
(result.error.error.code === 'invalid_token' ||
|
|
116
|
+
result.error.error.code === 'token_expired' ||
|
|
117
|
+
result.error.error.message === 'Invalid API key' ||
|
|
118
|
+
result.error.error.message?.toLowerCase().includes('token') ||
|
|
119
|
+
result.error.error.message?.toLowerCase().includes('unauthorized'))) {
|
|
130
120
|
isAuthError = true;
|
|
131
121
|
}
|
|
132
122
|
// Message-based detection as fallback
|
|
133
|
-
else if (typeof result.error ===
|
|
134
|
-
(result.error.toLowerCase().includes(
|
|
135
|
-
result.error.toLowerCase().includes(
|
|
136
|
-
result.error.toLowerCase().includes(
|
|
123
|
+
else if (typeof result.error === 'string' &&
|
|
124
|
+
(result.error.toLowerCase().includes('unauthorized') ||
|
|
125
|
+
result.error.toLowerCase().includes('token') ||
|
|
126
|
+
result.error.toLowerCase().includes('auth'))) {
|
|
137
127
|
isAuthError = true;
|
|
138
128
|
}
|
|
139
129
|
}
|
|
140
|
-
catch
|
|
130
|
+
catch {
|
|
141
131
|
// If we can't parse the error structure, do a simple string check
|
|
142
|
-
const
|
|
143
|
-
if (
|
|
144
|
-
|
|
145
|
-
|
|
132
|
+
const errorString = String(result.error);
|
|
133
|
+
if (errorString.toLowerCase().includes('unauthorized') ||
|
|
134
|
+
errorString.toLowerCase().includes('token') ||
|
|
135
|
+
errorString.toLowerCase().includes('auth')) {
|
|
146
136
|
isAuthError = true;
|
|
147
137
|
}
|
|
148
138
|
}
|
|
149
139
|
if (isAuthError && tokenManager.getRefreshToken()) {
|
|
150
|
-
logger_1.logger.debug(
|
|
140
|
+
logger_1.logger.debug('Auth error detected, attempting token refresh');
|
|
151
141
|
logger_1.logger.debug(`Error details: ${JSON.stringify(result.error, null, 2)}`);
|
|
152
|
-
const refreshed =
|
|
142
|
+
const refreshed = await refreshAccessToken(tokenManager);
|
|
153
143
|
if (refreshed) {
|
|
154
|
-
logger_1.logger.debug(
|
|
144
|
+
logger_1.logger.debug('Token refreshed successfully, retrying request');
|
|
155
145
|
// Update the Authorization header with the new token
|
|
156
|
-
if (!
|
|
157
|
-
|
|
158
|
-
if (!
|
|
159
|
-
|
|
160
|
-
|
|
146
|
+
if (!arguments_[1])
|
|
147
|
+
arguments_[1] = {};
|
|
148
|
+
if (!arguments_[1].headers)
|
|
149
|
+
arguments_[1].headers = {};
|
|
150
|
+
arguments_[1].headers.Authorization = `Bearer ${tokenManager.getAccessToken()}`;
|
|
161
151
|
// Retry the request
|
|
162
|
-
return
|
|
152
|
+
return await target[property](...arguments_);
|
|
163
153
|
}
|
|
164
154
|
else {
|
|
165
|
-
logger_1.logger.debug(
|
|
155
|
+
logger_1.logger.debug('Token refresh failed');
|
|
166
156
|
// Add a more helpful error message for users
|
|
167
|
-
if (typeof result.error ===
|
|
157
|
+
if (typeof result.error === 'object') {
|
|
168
158
|
result.error.userMessage =
|
|
169
|
-
|
|
159
|
+
'Your session has expired. Please run `berget auth login` to log in again.';
|
|
170
160
|
}
|
|
171
161
|
}
|
|
172
162
|
}
|
|
173
163
|
}
|
|
174
164
|
return result;
|
|
175
|
-
}
|
|
165
|
+
};
|
|
176
166
|
}
|
|
177
167
|
// For other properties, just return the original
|
|
178
|
-
return target[
|
|
168
|
+
return target[property];
|
|
179
169
|
},
|
|
180
170
|
});
|
|
181
171
|
};
|
|
182
172
|
exports.createAuthenticatedClient = createAuthenticatedClient;
|
|
183
173
|
// Keycloak configuration for token refresh (must match auth-service.ts)
|
|
184
|
-
const KEYCLOAK_URL = isStageMode || isLocalMode ?
|
|
185
|
-
const KEYCLOAK_REALM =
|
|
186
|
-
const KEYCLOAK_CLIENT_ID =
|
|
174
|
+
const KEYCLOAK_URL = isStageMode || isLocalMode ? 'https://keycloak.stage.berget.ai' : 'https://keycloak.berget.ai';
|
|
175
|
+
const KEYCLOAK_REALM = 'berget';
|
|
176
|
+
const KEYCLOAK_CLIENT_ID = 'berget-code';
|
|
187
177
|
// Helper function to refresh the access token
|
|
188
|
-
function refreshAccessToken(tokenManager) {
|
|
189
|
-
|
|
178
|
+
async function refreshAccessToken(tokenManager) {
|
|
179
|
+
try {
|
|
180
|
+
const refreshToken = tokenManager.getRefreshToken();
|
|
181
|
+
if (!refreshToken)
|
|
182
|
+
return false;
|
|
183
|
+
logger_1.logger.debug('Attempting to refresh access token');
|
|
184
|
+
// Refresh directly against Keycloak (berget-code is a public PKCE client)
|
|
190
185
|
try {
|
|
191
|
-
const
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
logger_1.logger.debug(`Token refresh error: HTTP ${response.status} ${response.statusText}`);
|
|
211
|
-
// Check if the refresh token itself is expired or invalid
|
|
212
|
-
if (response.status === 401 || response.status === 403) {
|
|
213
|
-
console.warn(chalk_1.default.yellow("Your refresh token has expired. Please run `berget auth login` again."));
|
|
214
|
-
// Clear tokens if unauthorized - they're invalid
|
|
215
|
-
tokenManager.clearTokens();
|
|
216
|
-
}
|
|
217
|
-
else {
|
|
218
|
-
console.warn(chalk_1.default.yellow(`Failed to refresh token: ${response.status} ${response.statusText}`));
|
|
219
|
-
}
|
|
220
|
-
return false;
|
|
186
|
+
const response = await fetch(`${KEYCLOAK_URL}/realms/${KEYCLOAK_REALM}/protocol/openid-connect/token`, {
|
|
187
|
+
body: new URLSearchParams({
|
|
188
|
+
client_id: KEYCLOAK_CLIENT_ID,
|
|
189
|
+
grant_type: 'refresh_token',
|
|
190
|
+
refresh_token: refreshToken,
|
|
191
|
+
}),
|
|
192
|
+
headers: {
|
|
193
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
194
|
+
},
|
|
195
|
+
method: 'POST',
|
|
196
|
+
});
|
|
197
|
+
// Handle HTTP errors
|
|
198
|
+
if (!response.ok) {
|
|
199
|
+
logger_1.logger.debug(`Token refresh error: HTTP ${response.status} ${response.statusText}`);
|
|
200
|
+
// Check if the refresh token itself is expired or invalid
|
|
201
|
+
if (response.status === 401 || response.status === 403) {
|
|
202
|
+
console.warn(chalk_1.default.yellow('Your refresh token has expired. Please run `berget auth login` again.'));
|
|
203
|
+
// Clear tokens if unauthorized - they're invalid
|
|
204
|
+
tokenManager.clearTokens();
|
|
221
205
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
if (!contentType || !contentType.includes("application/json")) {
|
|
225
|
-
console.warn(chalk_1.default.yellow(`Unexpected content type in response: ${contentType}`));
|
|
226
|
-
return false;
|
|
227
|
-
}
|
|
228
|
-
const data = yield response.json();
|
|
229
|
-
// Validate the response data
|
|
230
|
-
if (!data || !data.token) {
|
|
231
|
-
console.warn(chalk_1.default.yellow("Invalid token response. Please run `berget auth login` again."));
|
|
232
|
-
return false;
|
|
233
|
-
}
|
|
234
|
-
logger_1.logger.debug("Token refreshed successfully");
|
|
235
|
-
// Update the token
|
|
236
|
-
tokenManager.updateAccessToken(data.token, data.expires_in || 3600);
|
|
237
|
-
// If a new refresh token was provided, update that too
|
|
238
|
-
if (data.refresh_token) {
|
|
239
|
-
tokenManager.setTokens(data.token, data.refresh_token, data.expires_in || 3600);
|
|
240
|
-
logger_1.logger.debug("Refresh token also updated");
|
|
206
|
+
else {
|
|
207
|
+
console.warn(chalk_1.default.yellow(`Failed to refresh token: ${response.status} ${response.statusText}`));
|
|
241
208
|
}
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
// Parse the response
|
|
212
|
+
const contentType = response.headers.get('content-type');
|
|
213
|
+
if (!contentType || !contentType.includes('application/json')) {
|
|
214
|
+
console.warn(chalk_1.default.yellow(`Unexpected content type in response: ${contentType}`));
|
|
215
|
+
return false;
|
|
242
216
|
}
|
|
243
|
-
|
|
244
|
-
|
|
217
|
+
const data = (await response.json());
|
|
218
|
+
// Validate the response data
|
|
219
|
+
if (!data || !data.token) {
|
|
220
|
+
console.warn(chalk_1.default.yellow('Invalid token response. Please run `berget auth login` again.'));
|
|
245
221
|
return false;
|
|
246
222
|
}
|
|
247
|
-
|
|
223
|
+
logger_1.logger.debug('Token refreshed successfully');
|
|
224
|
+
// Update the token
|
|
225
|
+
tokenManager.updateAccessToken(data.token, data.expires_in || 3600);
|
|
226
|
+
// If a new refresh token was provided, update that too
|
|
227
|
+
if (data.refresh_token) {
|
|
228
|
+
tokenManager.setTokens(data.token, data.refresh_token, data.expires_in || 3600);
|
|
229
|
+
logger_1.logger.debug('Refresh token also updated');
|
|
230
|
+
}
|
|
248
231
|
}
|
|
249
|
-
catch (
|
|
250
|
-
console.warn(chalk_1.default.yellow(`Failed to refresh
|
|
232
|
+
catch (fetchError) {
|
|
233
|
+
console.warn(chalk_1.default.yellow(`Failed to refresh token: ${fetchError instanceof Error ? fetchError.message : String(fetchError)}`));
|
|
251
234
|
return false;
|
|
252
235
|
}
|
|
253
|
-
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
catch (error) {
|
|
239
|
+
console.warn(chalk_1.default.yellow(`Failed to refresh authentication token: ${error instanceof Error ? error.message : String(error)}`));
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
254
242
|
}
|