aicomputer 0.1.19 → 0.1.20

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.
@@ -0,0 +1,255 @@
1
+ // src/lib/config.ts
2
+ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "fs";
3
+ import { homedir } from "os";
4
+ import { join } from "path";
5
+ var CONFIG_DIR = join(homedir(), ".computer");
6
+ var CONFIG_FILE = join(CONFIG_DIR, "config.json");
7
+ function ensureConfigDir() {
8
+ if (!existsSync(CONFIG_DIR)) {
9
+ mkdirSync(CONFIG_DIR, { recursive: true, mode: 448 });
10
+ }
11
+ }
12
+ function readConfig() {
13
+ ensureConfigDir();
14
+ if (!existsSync(CONFIG_FILE)) {
15
+ return {};
16
+ }
17
+ try {
18
+ return JSON.parse(readFileSync(CONFIG_FILE, "utf8"));
19
+ } catch {
20
+ return {};
21
+ }
22
+ }
23
+ function writeConfig(config) {
24
+ ensureConfigDir();
25
+ const tempFile = `${CONFIG_FILE}.${process.pid}.tmp`;
26
+ writeFileSync(tempFile, JSON.stringify(config, null, 2), { mode: 384 });
27
+ renameSync(tempFile, CONFIG_FILE);
28
+ }
29
+ function getAPIKey() {
30
+ const envValue = process.env.COMPUTER_API_KEY ?? process.env.AGENTCOMPUTER_API_KEY;
31
+ if (envValue) {
32
+ return envValue.trim();
33
+ }
34
+ return getStoredAPIKey();
35
+ }
36
+ function getStoredAPIKey() {
37
+ return readConfig().auth?.apiKey?.trim() || null;
38
+ }
39
+ function hasEnvAPIKey() {
40
+ return Boolean(process.env.COMPUTER_API_KEY ?? process.env.AGENTCOMPUTER_API_KEY);
41
+ }
42
+ function setAPIKey(apiKey) {
43
+ const config = readConfig();
44
+ config.auth = { apiKey: apiKey.trim() };
45
+ writeConfig(config);
46
+ }
47
+ function clearAPIKey() {
48
+ const config = readConfig();
49
+ delete config.auth;
50
+ writeConfig(config);
51
+ }
52
+
53
+ // src/lib/api.ts
54
+ var BASE_URL = process.env.COMPUTER_API_URL ?? process.env.AGENTCOMPUTER_API_URL ?? "https://api.computer.agentcomputer.ai";
55
+ var WEB_URL = process.env.COMPUTER_WEB_URL ?? process.env.AGENTCOMPUTER_WEB_URL ?? resolveDefaultWebURL(BASE_URL);
56
+ var ApiError = class extends Error {
57
+ constructor(status, message) {
58
+ super(message);
59
+ this.status = status;
60
+ this.name = "ApiError";
61
+ }
62
+ };
63
+ function getBaseURL() {
64
+ return BASE_URL;
65
+ }
66
+ function getWebURL() {
67
+ return WEB_URL;
68
+ }
69
+ async function api(path, options = {}) {
70
+ const apiKey = getAPIKey();
71
+ if (!apiKey) {
72
+ throw new ApiError(401, "not logged in; run 'computer login' first");
73
+ }
74
+ return requestWithKey(apiKey, path, options);
75
+ }
76
+ function resolveDefaultWebURL(apiURL) {
77
+ try {
78
+ const parsed = new URL(apiURL);
79
+ if (parsed.hostname === "api.computer.agentcomputer.ai") {
80
+ return "https://agentcomputer.ai";
81
+ }
82
+ if (parsed.hostname === "localhost" || parsed.hostname === "127.0.0.1") {
83
+ return `${parsed.protocol}//${parsed.hostname}:3000`;
84
+ }
85
+ } catch {
86
+ return "https://agentcomputer.ai";
87
+ }
88
+ return "https://agentcomputer.ai";
89
+ }
90
+ async function apiWithKey(apiKey, path, options = {}) {
91
+ return requestWithKey(apiKey, path, options);
92
+ }
93
+ async function requestWithKey(apiKey, path, options) {
94
+ const headers = {
95
+ Accept: "application/json",
96
+ ...options.headers ?? {}
97
+ };
98
+ if (options.body !== void 0) {
99
+ headers["Content-Type"] = "application/json";
100
+ }
101
+ if (apiKey) {
102
+ headers.Authorization = `Bearer ${apiKey}`;
103
+ }
104
+ const response = await fetch(`${BASE_URL}${path}`, {
105
+ ...options,
106
+ headers
107
+ });
108
+ if (!response.ok) {
109
+ throw new ApiError(response.status, await readErrorMessage(response));
110
+ }
111
+ if (response.status === 204) {
112
+ return void 0;
113
+ }
114
+ return await response.json();
115
+ }
116
+ async function readErrorMessage(response) {
117
+ const contentType = response.headers.get("content-type") ?? "";
118
+ if (contentType.includes("application/json")) {
119
+ try {
120
+ const payload = await response.json();
121
+ if (payload.error) {
122
+ return payload.error;
123
+ }
124
+ return JSON.stringify(payload);
125
+ } catch {
126
+ return response.statusText || "request failed";
127
+ }
128
+ }
129
+ const body = await response.text();
130
+ return body || response.statusText || "request failed";
131
+ }
132
+
133
+ // src/lib/computers.ts
134
+ async function listComputers(signal) {
135
+ const response = await api("/v1/computers", { signal });
136
+ return response.computers;
137
+ }
138
+ async function getComputerByID(id) {
139
+ return api(`/v1/computers/${id}`);
140
+ }
141
+ async function createComputer(input) {
142
+ return api("/v1/computers", {
143
+ method: "POST",
144
+ body: JSON.stringify(input)
145
+ });
146
+ }
147
+ async function deleteComputer(computerID) {
148
+ return api(`/v1/computers/${computerID}`, {
149
+ method: "DELETE"
150
+ });
151
+ }
152
+ async function powerOnComputer(computerID) {
153
+ return api(`/v1/computers/${computerID}/power-on`, {
154
+ method: "POST"
155
+ });
156
+ }
157
+ async function powerOffComputer(computerID) {
158
+ return api(`/v1/computers/${computerID}/power-off`, {
159
+ method: "POST"
160
+ });
161
+ }
162
+ async function getFilesystemSettings() {
163
+ return api("/v1/me/filesystem");
164
+ }
165
+ async function getConnectionInfo(computerID, signal) {
166
+ return api(`/v1/computers/${computerID}/connection`, {
167
+ signal
168
+ });
169
+ }
170
+ async function createBrowserAccess(computerID) {
171
+ return api(`/v1/computers/${computerID}/access/browser`, {
172
+ method: "POST"
173
+ });
174
+ }
175
+ async function listPublishedPorts(computerID) {
176
+ const response = await api(`/v1/computers/${computerID}/ports`);
177
+ return response.ports;
178
+ }
179
+ async function publishPort(computerID, input) {
180
+ return api(`/v1/computers/${computerID}/ports`, {
181
+ method: "POST",
182
+ body: JSON.stringify(input)
183
+ });
184
+ }
185
+ async function deletePublishedPort(computerID, targetPort) {
186
+ return api(`/v1/computers/${computerID}/ports/${targetPort}`, {
187
+ method: "DELETE"
188
+ });
189
+ }
190
+ async function resolveComputer(identifier) {
191
+ try {
192
+ return await getComputerByID(identifier);
193
+ } catch (error) {
194
+ if (!(error instanceof Error) || !("status" in error)) {
195
+ throw error;
196
+ }
197
+ const status = Reflect.get(error, "status");
198
+ if (status !== 404) {
199
+ throw error;
200
+ }
201
+ }
202
+ const computers = await listComputers();
203
+ const exact = computers.find(
204
+ (computer) => computer.handle === identifier || computer.id === identifier
205
+ );
206
+ if (exact) {
207
+ return exact;
208
+ }
209
+ throw new Error(`computer '${identifier}' not found`);
210
+ }
211
+ function webURL(computer) {
212
+ return `https://${computer.primary_web_host}${normalizePrimaryPath(computer.primary_path)}`;
213
+ }
214
+ function vncURL(computer) {
215
+ if (!computer.vnc_enabled) {
216
+ return null;
217
+ }
218
+ const domain = computer.primary_web_host.replace(/^[^.]+\./, "");
219
+ return `https://6080--${computer.handle}.${domain}`;
220
+ }
221
+ function normalizePrimaryPath(primaryPath) {
222
+ const trimmed = primaryPath?.trim();
223
+ if (!trimmed) {
224
+ return "/";
225
+ }
226
+ return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
227
+ }
228
+
229
+ export {
230
+ getAPIKey,
231
+ getStoredAPIKey,
232
+ hasEnvAPIKey,
233
+ setAPIKey,
234
+ clearAPIKey,
235
+ ApiError,
236
+ getBaseURL,
237
+ getWebURL,
238
+ api,
239
+ apiWithKey,
240
+ listComputers,
241
+ getComputerByID,
242
+ createComputer,
243
+ deleteComputer,
244
+ powerOnComputer,
245
+ powerOffComputer,
246
+ getFilesystemSettings,
247
+ getConnectionInfo,
248
+ createBrowserAccess,
249
+ listPublishedPorts,
250
+ publishPort,
251
+ deletePublishedPort,
252
+ resolveComputer,
253
+ webURL,
254
+ vncURL
255
+ };
@@ -5,7 +5,7 @@ import {
5
5
  } from "./chunk-KXLTHWW3.js";
6
6
  import {
7
7
  resolveMutagenCommandPath
8
- } from "./chunk-MDSPJ57B.js";
8
+ } from "./chunk-GD42GHW3.js";
9
9
 
10
10
  // src/lib/mount-mutagen.ts
11
11
  import { chmodSync, readFileSync, symlinkSync, unlinkSync, writeFileSync } from "fs";
@@ -14,7 +14,10 @@ import { basename, join, relative, resolve } from "path";
14
14
  var SYNC_NAME_PREFIX = "agentcomputer-mount-";
15
15
  var DEFAULT_IGNORE_PATHS = [
16
16
  ".codex/tmp",
17
+ ".dbus",
17
18
  ".local",
19
+ ".run",
20
+ ".runtime",
18
21
  ".ssh/sshd.log"
19
22
  ];
20
23
  function ensureMutagenSshEnvironment(config, paths) {
@@ -112,6 +115,8 @@ async function listOwnedSessions(config, paths, signal) {
112
115
  betaConnected: session.betaConnected,
113
116
  status: session.status,
114
117
  lastError: session.lastError,
118
+ stagingProgress: session.stagingProgress,
119
+ currentFile: session.currentFile,
115
120
  scanProblemCount: session.scanProblemCount,
116
121
  conflictCount: session.conflictCount,
117
122
  legacy: session.name !== expectedName || session.betaUrl !== expectedBeta || alphaPath !== resolve(join(rootPath, handle))
@@ -176,6 +181,14 @@ function parseMutagenSyncList(output) {
176
181
  current.status = trimmed.slice("Status: ".length);
177
182
  continue;
178
183
  }
184
+ if (trimmed.startsWith("Staging progress: ")) {
185
+ current.stagingProgress = trimmed.slice("Staging progress: ".length);
186
+ continue;
187
+ }
188
+ if (trimmed.startsWith("Current file: ")) {
189
+ current.currentFile = trimmed.slice("Current file: ".length);
190
+ continue;
191
+ }
179
192
  if (trimmed.startsWith("Last error: ")) {
180
193
  current.lastError = trimmed.slice("Last error: ".length);
181
194
  continue;