berget 1.0.0 → 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 (37) hide show
  1. package/README.md +92 -0
  2. package/dist/index.js +7 -471
  3. package/dist/src/client.js +193 -102
  4. package/dist/src/commands/api-keys.js +271 -0
  5. package/dist/src/commands/auth.js +65 -0
  6. package/dist/src/commands/autocomplete.js +24 -0
  7. package/dist/src/commands/billing.js +53 -0
  8. package/dist/src/commands/chat.js +276 -0
  9. package/dist/src/commands/clusters.js +69 -0
  10. package/dist/src/commands/index.js +25 -0
  11. package/dist/src/commands/models.js +69 -0
  12. package/dist/src/commands/users.js +43 -0
  13. package/dist/src/constants/command-structure.js +14 -0
  14. package/dist/src/services/auth-service.js +49 -47
  15. package/dist/src/services/chat-service.js +177 -0
  16. package/dist/src/utils/config-checker.js +50 -0
  17. package/dist/src/utils/default-api-key.js +111 -0
  18. package/dist/src/utils/token-manager.js +165 -0
  19. package/index.ts +5 -566
  20. package/package.json +6 -1
  21. package/src/client.ts +262 -80
  22. package/src/commands/api-keys.ts +364 -0
  23. package/src/commands/auth.ts +58 -0
  24. package/src/commands/autocomplete.ts +19 -0
  25. package/src/commands/billing.ts +41 -0
  26. package/src/commands/chat.ts +345 -0
  27. package/src/commands/clusters.ts +65 -0
  28. package/src/commands/index.ts +23 -0
  29. package/src/commands/models.ts +63 -0
  30. package/src/commands/users.ts +37 -0
  31. package/src/constants/command-structure.ts +16 -0
  32. package/src/services/auth-service.ts +90 -50
  33. package/src/services/chat-service.ts +177 -0
  34. package/src/types/api.d.ts +58 -192
  35. package/src/utils/config-checker.ts +23 -0
  36. package/src/utils/default-api-key.ts +94 -0
  37. package/src/utils/token-manager.ts +150 -0
@@ -0,0 +1,165 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.TokenManager = void 0;
30
+ const fs = __importStar(require("fs"));
31
+ const path = __importStar(require("path"));
32
+ const os = __importStar(require("os"));
33
+ const chalk_1 = __importDefault(require("chalk"));
34
+ /**
35
+ * Manages authentication tokens including refresh functionality
36
+ */
37
+ class TokenManager {
38
+ constructor() {
39
+ this.tokenData = null;
40
+ // Set up token file path in user's home directory
41
+ const bergetDir = path.join(os.homedir(), '.berget');
42
+ if (!fs.existsSync(bergetDir)) {
43
+ fs.mkdirSync(bergetDir, { recursive: true });
44
+ }
45
+ this.tokenFilePath = path.join(bergetDir, 'auth.json');
46
+ this.loadToken();
47
+ }
48
+ static getInstance() {
49
+ if (!TokenManager.instance) {
50
+ TokenManager.instance = new TokenManager();
51
+ }
52
+ return TokenManager.instance;
53
+ }
54
+ /**
55
+ * Load token data from file
56
+ */
57
+ loadToken() {
58
+ try {
59
+ if (fs.existsSync(this.tokenFilePath)) {
60
+ const data = fs.readFileSync(this.tokenFilePath, 'utf8');
61
+ this.tokenData = JSON.parse(data);
62
+ }
63
+ }
64
+ catch (error) {
65
+ console.error(chalk_1.default.dim('Failed to load authentication token'));
66
+ this.tokenData = null;
67
+ }
68
+ }
69
+ /**
70
+ * Save token data to file
71
+ */
72
+ saveToken() {
73
+ try {
74
+ if (this.tokenData) {
75
+ fs.writeFileSync(this.tokenFilePath, JSON.stringify(this.tokenData, null, 2));
76
+ // Set file permissions to be readable only by the owner
77
+ fs.chmodSync(this.tokenFilePath, 0o600);
78
+ }
79
+ else {
80
+ // If token data is null, remove the file
81
+ if (fs.existsSync(this.tokenFilePath)) {
82
+ fs.unlinkSync(this.tokenFilePath);
83
+ }
84
+ }
85
+ }
86
+ catch (error) {
87
+ console.error(chalk_1.default.dim('Failed to save authentication token'));
88
+ }
89
+ }
90
+ /**
91
+ * Get the current access token
92
+ * @returns The access token or null if not available
93
+ */
94
+ getAccessToken() {
95
+ if (!this.tokenData)
96
+ return null;
97
+ return this.tokenData.access_token;
98
+ }
99
+ /**
100
+ * Get the refresh token
101
+ * @returns The refresh token or null if not available
102
+ */
103
+ getRefreshToken() {
104
+ if (!this.tokenData)
105
+ return null;
106
+ return this.tokenData.refresh_token;
107
+ }
108
+ /**
109
+ * Check if the access token is expired
110
+ * @returns true if expired or about to expire (within 5 minutes), false otherwise
111
+ */
112
+ isTokenExpired() {
113
+ if (!this.tokenData || !this.tokenData.expires_at)
114
+ return true;
115
+ try {
116
+ // Consider token expired if it's within 10 minutes of expiration
117
+ // Using a larger buffer to be more proactive about refreshing
118
+ const expirationBuffer = 10 * 60 * 1000; // 10 minutes in milliseconds
119
+ const isExpired = Date.now() + expirationBuffer >= this.tokenData.expires_at;
120
+ if (isExpired && process.argv.includes('--debug')) {
121
+ console.log(chalk_1.default.yellow(`DEBUG: Token expired or expiring soon. Current time: ${new Date().toISOString()}, Expiry: ${new Date(this.tokenData.expires_at).toISOString()}`));
122
+ }
123
+ return isExpired;
124
+ }
125
+ catch (error) {
126
+ // If there's any error checking expiration, assume token is expired
127
+ console.error(chalk_1.default.dim(`Error checking token expiration: ${error instanceof Error ? error.message : String(error)}`));
128
+ return true;
129
+ }
130
+ }
131
+ /**
132
+ * Set new token data
133
+ * @param accessToken The new access token
134
+ * @param refreshToken The new refresh token
135
+ * @param expiresIn Expiration time in seconds
136
+ */
137
+ setTokens(accessToken, refreshToken, expiresIn) {
138
+ this.tokenData = {
139
+ access_token: accessToken,
140
+ refresh_token: refreshToken,
141
+ expires_at: Date.now() + (expiresIn * 1000)
142
+ };
143
+ this.saveToken();
144
+ }
145
+ /**
146
+ * Update just the access token and its expiration
147
+ * @param accessToken The new access token
148
+ * @param expiresIn Expiration time in seconds
149
+ */
150
+ updateAccessToken(accessToken, expiresIn) {
151
+ if (!this.tokenData)
152
+ return;
153
+ this.tokenData.access_token = accessToken;
154
+ this.tokenData.expires_at = Date.now() + (expiresIn * 1000);
155
+ this.saveToken();
156
+ }
157
+ /**
158
+ * Clear all token data
159
+ */
160
+ clearTokens() {
161
+ this.tokenData = null;
162
+ this.saveToken();
163
+ }
164
+ }
165
+ exports.TokenManager = TokenManager;