@optima-chat/browser-cli 0.1.0 → 0.1.2
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/dist/index.js +40 -7
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4,7 +4,26 @@
|
|
|
4
4
|
import { Command } from "commander";
|
|
5
5
|
|
|
6
6
|
// src/config.ts
|
|
7
|
-
|
|
7
|
+
import { readFileSync, existsSync } from "fs";
|
|
8
|
+
import { join } from "path";
|
|
9
|
+
import { homedir } from "os";
|
|
10
|
+
var TOKEN_PATH = join(homedir(), ".optima", "token.json");
|
|
11
|
+
var BACKEND_URLS = {
|
|
12
|
+
ci: "https://browser-api.stage.optima.onl",
|
|
13
|
+
stage: "https://browser-api.stage.optima.onl",
|
|
14
|
+
prod: "https://browser-api.optima.onl"
|
|
15
|
+
};
|
|
16
|
+
function loadTokenData() {
|
|
17
|
+
if (!existsSync(TOKEN_PATH)) return null;
|
|
18
|
+
try {
|
|
19
|
+
return JSON.parse(readFileSync(TOKEN_PATH, "utf-8"));
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
var tokenData = loadTokenData();
|
|
25
|
+
var BROWSER_API_URL = process.env.BROWSER_API_URL || (tokenData ? BACKEND_URLS[tokenData.env] : null) || "http://localhost:7292";
|
|
26
|
+
var ACCESS_TOKEN = tokenData?.access_token || null;
|
|
8
27
|
|
|
9
28
|
// src/api.ts
|
|
10
29
|
var BrowserAPIError = class extends Error {
|
|
@@ -23,6 +42,7 @@ async function request(path, options = {}) {
|
|
|
23
42
|
...options,
|
|
24
43
|
headers: {
|
|
25
44
|
"Content-Type": "application/json",
|
|
45
|
+
...ACCESS_TOKEN ? { Authorization: `Bearer ${ACCESS_TOKEN}` } : {},
|
|
26
46
|
...options.headers
|
|
27
47
|
}
|
|
28
48
|
});
|
|
@@ -171,21 +191,25 @@ var BrowserAPI = class {
|
|
|
171
191
|
var api = new BrowserAPI();
|
|
172
192
|
|
|
173
193
|
// src/session.ts
|
|
174
|
-
import { readFileSync, writeFileSync, unlinkSync, existsSync } from "fs";
|
|
175
|
-
|
|
194
|
+
import { readFileSync as readFileSync2, writeFileSync, unlinkSync, existsSync as existsSync2, mkdirSync } from "fs";
|
|
195
|
+
import { join as join2 } from "path";
|
|
196
|
+
import { homedir as homedir2 } from "os";
|
|
197
|
+
var OPTIMA_DIR = join2(homedir2(), ".optima");
|
|
198
|
+
var SESSION_FILE = join2(OPTIMA_DIR, "browser-session-id");
|
|
176
199
|
function saveSessionId(id) {
|
|
200
|
+
mkdirSync(OPTIMA_DIR, { recursive: true });
|
|
177
201
|
writeFileSync(SESSION_FILE, id, "utf-8");
|
|
178
202
|
}
|
|
179
203
|
function loadSessionId() {
|
|
180
|
-
if (!
|
|
204
|
+
if (!existsSync2(SESSION_FILE)) {
|
|
181
205
|
throw new Error(
|
|
182
206
|
"No active browser session. Run 'browser-cli launch' first."
|
|
183
207
|
);
|
|
184
208
|
}
|
|
185
|
-
return
|
|
209
|
+
return readFileSync2(SESSION_FILE, "utf-8").trim();
|
|
186
210
|
}
|
|
187
211
|
function clearSessionId() {
|
|
188
|
-
if (
|
|
212
|
+
if (existsSync2(SESSION_FILE)) {
|
|
189
213
|
unlinkSync(SESSION_FILE);
|
|
190
214
|
}
|
|
191
215
|
}
|
|
@@ -261,8 +285,17 @@ function formatTabsInline(tabs) {
|
|
|
261
285
|
|
|
262
286
|
// src/commands/lifecycle.ts
|
|
263
287
|
function registerLifecycleCommands(program2) {
|
|
264
|
-
program2.command("launch").description("Launch a browser session").option("--url <url>", "Navigate to URL after launch").action(async (opts) => {
|
|
288
|
+
program2.command("launch").description("Launch a browser session").option("--url <url>", "Navigate to URL after launch").option("--force", "Close existing session before launching").action(async (opts) => {
|
|
265
289
|
try {
|
|
290
|
+
if (opts.force) {
|
|
291
|
+
try {
|
|
292
|
+
const existingId = loadSessionId();
|
|
293
|
+
await api.closeSession(existingId);
|
|
294
|
+
clearSessionId();
|
|
295
|
+
} catch {
|
|
296
|
+
clearSessionId();
|
|
297
|
+
}
|
|
298
|
+
}
|
|
266
299
|
const state = await api.createSession(opts.url);
|
|
267
300
|
saveSessionId(state.session_id);
|
|
268
301
|
console.log(formatLaunch(state));
|