@dainprotocol/cli 1.2.30 → 1.2.31

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.
@@ -165,7 +165,7 @@ function copyWasmDependencies(bundlePath, nodeModulesPath, outDir) {
165
165
  case 7: return [3 /*break*/, 9];
166
166
  case 8:
167
167
  error_1 = _a.sent();
168
- console.warn('Warning: Could not process WASM dependencies:', error_1);
168
+ (0, utils_1.logInfo)("Warning: Could not process WASM dependencies: ".concat(error_1));
169
169
  return [3 /*break*/, 9];
170
170
  case 9: return [2 /*return*/, copiedFiles];
171
171
  }
@@ -77,7 +77,8 @@ function deploy(options) {
77
77
  spinner.fail("No project found on platform linked to this repository.");
78
78
  _d.label = 4;
79
79
  case 4:
80
- _d.trys.push([4, 18, , 19]);
80
+ _d.trys.push([4, 16, , 17]);
81
+ result = void 0;
81
82
  if (!(projectId && repoName && branchName)) return [3 /*break*/, 7];
82
83
  spinner.info("Deploying project using github repository...").start();
83
84
  return [4 /*yield*/, (0, utils_1.fetchWithTimeout)((0, utils_1.joinUrl)(basePath, "/api/app/codegen/data/projects/".concat(projectId, "/start-deploy")), {
@@ -114,26 +115,19 @@ function deploy(options) {
114
115
  case 13:
115
116
  _c = _d.sent();
116
117
  return [3 /*break*/, 14];
117
- case 14:
118
- if (!result) return [3 /*break*/, 16];
119
- return [4 /*yield*/, updateDainJson(result)];
118
+ case 14: return [4 /*yield*/, updateDainJson(result)];
120
119
  case 15:
121
120
  _d.sent();
122
121
  printDeploymentResult(result, spinner);
123
122
  process.exit(0);
124
123
  return [3 /*break*/, 17];
125
124
  case 16:
126
- spinner.fail("Deployment failed.");
127
- process.exit(1);
128
- _d.label = 17;
129
- case 17: return [3 /*break*/, 19];
130
- case 18:
131
125
  error_1 = _d.sent();
132
126
  spinner.fail("Deployment failed.");
133
127
  (0, utils_1.logError)("Error during deployment: ", error_1);
134
128
  process.exit(1);
135
- return [3 /*break*/, 19];
136
- case 19: return [2 /*return*/];
129
+ return [3 /*break*/, 17];
130
+ case 17: return [2 /*return*/];
137
131
  }
138
132
  });
139
133
  });
@@ -67,6 +67,7 @@ var tunnelUrl = null;
67
67
  var isFirstStart = true;
68
68
  var proxyServer = null;
69
69
  var isCleaningUp = false;
70
+ var debounceTimer = null;
70
71
  function isPortAvailable(port) {
71
72
  return new Promise(function (resolve) {
72
73
  var server = (0, net_1.createServer)()
@@ -78,6 +79,10 @@ function cleanup() {
78
79
  if (isCleaningUp)
79
80
  return;
80
81
  isCleaningUp = true;
82
+ if (debounceTimer) {
83
+ clearTimeout(debounceTimer);
84
+ debounceTimer = null;
85
+ }
81
86
  if (childProcess) {
82
87
  childProcess.kill('SIGTERM');
83
88
  childProcess = null;
@@ -172,7 +177,7 @@ function startProcess(mainFile, envVars, isRestart) {
172
177
  }
173
178
  function dev(options) {
174
179
  return __awaiter(this, void 0, void 0, function () {
175
- var config, port, portNumber, runtime, mainFile, resolvedMain, envVars, proxySetup, watchPaths, dainDir, outFile, MFconfig_1, debounceTimer_1, error_1;
180
+ var config, port, portNumber, runtime, mainFile, resolvedMain, envVars, proxySetup, watchPaths, dainDir, outFile, MFconfig_1, error_1;
176
181
  return __generator(this, function (_a) {
177
182
  switch (_a.label) {
178
183
  case 0:
@@ -228,7 +233,14 @@ function dev(options) {
228
233
  config['static-dir'] ? path_1.default.join(process.cwd(), config['static-dir']) : (0, utils_1.getStaticFilesPath)(),
229
234
  ].filter(function (p) { return fs_extra_1.default.existsSync(p); });
230
235
  watcher = chokidar_1.default.watch(watchPaths, { ignored: /(^|[\/\\])\./, persistent: true, ignoreInitial: true });
231
- watcher.on('change', function (changedPath) { (0, utils_1.logInfo)("File ".concat(changedPath, " changed. Restarting...")); startProcess(mainFile, envVars, true); });
236
+ watcher.on('change', function (changedPath) {
237
+ if (debounceTimer)
238
+ clearTimeout(debounceTimer);
239
+ debounceTimer = setTimeout(function () {
240
+ (0, utils_1.logInfo)("File ".concat(changedPath, " changed. Restarting..."));
241
+ startProcess(mainFile, envVars, true);
242
+ }, 300);
243
+ });
232
244
  (0, utils_1.logInfo)('Watching for file changes...');
233
245
  return [3 /*break*/, 8];
234
246
  case 5:
@@ -241,11 +253,10 @@ function dev(options) {
241
253
  MFconfig_1 = { scriptPath: outFile, modules: true, port: parseInt(port, 10), log: new miniflare_1.Log(miniflare_1.LogLevel.DEBUG), liveReload: true };
242
254
  mf = new miniflare_1.Miniflare(MFconfig_1);
243
255
  (0, utils_1.logSuccess)("Miniflare server started on port ".concat(port));
244
- debounceTimer_1 = null;
245
256
  fs_extra_1.default.watch(dainDir, { recursive: true }, function (_eventType, filename) {
246
- if (debounceTimer_1)
247
- clearTimeout(debounceTimer_1);
248
- debounceTimer_1 = setTimeout(function () { if (mf) {
257
+ if (debounceTimer)
258
+ clearTimeout(debounceTimer);
259
+ debounceTimer = setTimeout(function () { if (mf) {
249
260
  mf.setOptions(MFconfig_1);
250
261
  (0, utils_1.logInfo)("Build updated (".concat(filename, ")"));
251
262
  } }, 300);
@@ -56,6 +56,8 @@ var utils_1 = require("../utils");
56
56
  var watchInterval = null;
57
57
  var isShuttingDown = false;
58
58
  function cleanup() {
59
+ if (isShuttingDown)
60
+ return;
59
61
  isShuttingDown = true;
60
62
  if (watchInterval) {
61
63
  clearInterval(watchInterval);
@@ -64,40 +66,24 @@ function cleanup() {
64
66
  }
65
67
  function logs(options) {
66
68
  return __awaiter(this, void 0, void 0, function () {
67
- var config, orgId, apiKey, deploymentId, serviceId, logsUrl, spinner, result, error_1;
69
+ var config, ctx, spinner, result, error_1;
68
70
  var _this = this;
69
71
  return __generator(this, function (_a) {
70
72
  switch (_a.label) {
71
73
  case 0:
72
74
  config = (0, utils_1.getDainConfig)(options.config);
73
- orgId = (0, utils_1.resolveOrgId)(config);
74
- apiKey = config["api-key"];
75
- deploymentId = config["deployment-id"];
76
- serviceId = config["service-id"];
77
- if (!orgId) {
78
- (0, utils_1.logError)("Org ID not found. Ensure your API key or DAIN_ORG_ID is set.");
79
- process.exit(1);
80
- }
81
- if (!deploymentId || !serviceId) {
82
- (0, utils_1.logError)("Deployment ID or service ID not found");
83
- process.exit(1);
84
- }
85
- if (!apiKey) {
86
- (0, utils_1.logError)("API key not found");
87
- process.exit(1);
88
- }
75
+ ctx = (0, utils_1.validatePlatformConfig)(config);
89
76
  process.once("SIGINT", function () { cleanup(); process.exit(0); });
90
77
  process.once("SIGTERM", function () { cleanup(); process.exit(0); });
91
- logsUrl = (0, utils_1.buildPlatformUrl)(config, orgId, "logs");
92
78
  spinner = (0, ora_1.default)("Fetching logs for project ".concat(config["project-id"], "...")).start();
93
79
  _a.label = 1;
94
80
  case 1:
95
81
  _a.trys.push([1, 3, , 4]);
96
- return [4 /*yield*/, fetchLogs(logsUrl, apiKey)];
82
+ return [4 /*yield*/, fetchLogs(ctx)];
97
83
  case 2:
98
84
  result = _a.sent();
99
85
  spinner.stop();
100
- (0, utils_1.logInfo)("Project logs: ".concat(formatProjectLogs(result, deploymentId)));
86
+ (0, utils_1.logInfo)("Project logs: ".concat(formatProjectLogs(result, ctx.deploymentId)));
101
87
  if (options.watch) {
102
88
  (0, utils_1.logInfo)("Watching for log updates (Ctrl+C to stop)...");
103
89
  watchInterval = setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
@@ -110,11 +96,11 @@ function logs(options) {
110
96
  _a.label = 1;
111
97
  case 1:
112
98
  _a.trys.push([1, 3, , 4]);
113
- return [4 /*yield*/, fetchLogs(logsUrl, apiKey)];
99
+ return [4 /*yield*/, fetchLogs(ctx)];
114
100
  case 2:
115
101
  newResult = _a.sent();
116
102
  process.stdout.write("\x1Bc");
117
- (0, utils_1.logInfo)("Project logs: ".concat(formatProjectLogs(newResult, deploymentId)));
103
+ (0, utils_1.logInfo)("Project logs: ".concat(formatProjectLogs(newResult, ctx.deploymentId)));
118
104
  return [3 /*break*/, 4];
119
105
  case 3:
120
106
  error_2 = _a.sent();
@@ -140,15 +126,12 @@ function logs(options) {
140
126
  });
141
127
  });
142
128
  }
143
- function fetchLogs(url, apiKey) {
129
+ function fetchLogs(ctx) {
144
130
  return __awaiter(this, void 0, void 0, function () {
145
131
  var response;
146
132
  return __generator(this, function (_a) {
147
133
  switch (_a.label) {
148
- case 0: return [4 /*yield*/, (0, utils_1.fetchWithTimeout)(url, {
149
- method: "GET",
150
- headers: { "Content-Type": "application/json", Authorization: "Bearer ".concat(apiKey) },
151
- })];
134
+ case 0: return [4 /*yield*/, (0, utils_1.platformRequest)(ctx, "logs")];
152
135
  case 1:
153
136
  response = _a.sent();
154
137
  if (!response.ok) {
@@ -164,10 +147,10 @@ function formatProjectLogs(projectLogs, deploymentId) {
164
147
  var _a = projectLogs.logs, logs_1 = _a === void 0 ? "" : _a, metadata = __rest(projectLogs, ["logs"]);
165
148
  var formattedMetadata = Object.entries(metadata)
166
149
  .map(function (_a) {
167
- var _b;
168
150
  var key = _a[0], value = _a[1];
169
- var formattedValue = ((_b = value === null || value === void 0 ? void 0 : value.includes) === null || _b === void 0 ? void 0 : _b.call(value, "T"))
170
- ? new Date(value).toLocaleString()
151
+ var stringValue = String(value);
152
+ var formattedValue = stringValue.includes("T")
153
+ ? new Date(stringValue).toLocaleString()
171
154
  : value;
172
155
  return "\u001B[36m".concat(key.replace(/([A-Z])/g, " $1").toLowerCase(), ":\u001B[0m ").concat(formattedValue);
173
156
  })
@@ -44,35 +44,21 @@ var ora_1 = __importDefault(require("ora"));
44
44
  var utils_1 = require("../utils");
45
45
  function status(options, preDefinedDeploymentId, preDefinedEnvironmentName, preDefinedServiceId) {
46
46
  return __awaiter(this, void 0, void 0, function () {
47
- var config, orgId, apiKey, deploymentId, serviceId, statusUrl, spinner, response, result, error_1;
47
+ var config, ctx, spinner, response, result, error_1;
48
48
  return __generator(this, function (_a) {
49
49
  switch (_a.label) {
50
50
  case 0:
51
51
  config = (0, utils_1.getDainConfig)(options.config);
52
- orgId = (0, utils_1.resolveOrgId)(config);
53
- apiKey = config["api-key"];
54
- deploymentId = preDefinedDeploymentId || config["deployment-id"];
55
- serviceId = preDefinedServiceId || config["service-id"];
56
- if (!orgId) {
57
- (0, utils_1.logError)("Org ID not found. Ensure your API key or DAIN_ORG_ID is set.");
58
- process.exit(1);
59
- }
60
- if (!deploymentId || !serviceId) {
61
- (0, utils_1.logError)("Deployment ID or service ID not found");
62
- process.exit(1);
63
- }
64
- statusUrl = (0, utils_1.buildPlatformUrl)(config, orgId, "status", {
65
- deploymentId: deploymentId,
66
- environment: preDefinedEnvironmentName,
67
- serviceId: serviceId,
52
+ ctx = (0, utils_1.validatePlatformConfig)(config, {
53
+ deploymentId: preDefinedDeploymentId,
54
+ serviceId: preDefinedServiceId,
68
55
  });
69
56
  spinner = (0, ora_1.default)("Checking status for project ".concat(config["project-id"], "...")).start();
70
57
  _a.label = 1;
71
58
  case 1:
72
59
  _a.trys.push([1, 4, , 5]);
73
- return [4 /*yield*/, (0, utils_1.fetchWithTimeout)(statusUrl, {
74
- method: "GET",
75
- headers: { "Content-Type": "application/json", Authorization: "Bearer ".concat(apiKey) },
60
+ return [4 /*yield*/, (0, utils_1.platformRequest)(ctx, "status", "GET", {
61
+ environment: preDefinedEnvironmentName,
76
62
  })];
77
63
  case 2:
78
64
  response = _a.sent();
@@ -44,32 +44,17 @@ var ora_1 = __importDefault(require("ora"));
44
44
  var utils_1 = require("../utils");
45
45
  function undeploy(options) {
46
46
  return __awaiter(this, void 0, void 0, function () {
47
- var config, orgId, apiKey, deploymentId, serviceId, spinner, undeployUrl, response, error_1;
47
+ var config, ctx, spinner, response, error_1;
48
48
  return __generator(this, function (_a) {
49
49
  switch (_a.label) {
50
50
  case 0:
51
51
  config = (0, utils_1.getDainConfig)(options.config);
52
- orgId = (0, utils_1.resolveOrgId)(config);
53
- apiKey = config["api-key"];
54
- deploymentId = config["deployment-id"];
55
- serviceId = config["service-id"];
56
- spinner = (0, ora_1.default)("Undeploying deployment ".concat(deploymentId, "...")).start();
57
- if (!orgId) {
58
- (0, utils_1.logError)("Org ID not found. Ensure your API key or DAIN_ORG_ID is set.");
59
- process.exit(1);
60
- }
61
- if (!deploymentId || !serviceId) {
62
- (0, utils_1.logError)("Deployment ID or service ID not found");
63
- process.exit(1);
64
- }
65
- undeployUrl = (0, utils_1.buildPlatformUrl)(config, orgId, "undeploy");
52
+ ctx = (0, utils_1.validatePlatformConfig)(config);
53
+ spinner = (0, ora_1.default)("Undeploying deployment ".concat(ctx.deploymentId, "...")).start();
66
54
  _a.label = 1;
67
55
  case 1:
68
56
  _a.trys.push([1, 3, , 4]);
69
- return [4 /*yield*/, (0, utils_1.fetchWithTimeout)(undeployUrl, {
70
- method: "DELETE",
71
- headers: { "Content-Type": "application/json", Authorization: "Bearer ".concat(apiKey) },
72
- })];
57
+ return [4 /*yield*/, (0, utils_1.platformRequest)(ctx, "undeploy", "DELETE")];
73
58
  case 2:
74
59
  response = _a.sent();
75
60
  if (!response.ok) {
package/dist/utils.js CHANGED
@@ -65,6 +65,9 @@ exports.extractOrgId = extractOrgId;
65
65
  exports.resolveOrgId = resolveOrgId;
66
66
  exports.fetchWithTimeout = fetchWithTimeout;
67
67
  exports.buildPlatformUrl = buildPlatformUrl;
68
+ exports.validatePlatformConfig = validatePlatformConfig;
69
+ exports.platformRequest = platformRequest;
70
+ exports.createIdempotentCleanup = createIdempotentCleanup;
68
71
  var fs_1 = __importDefault(require("fs"));
69
72
  var path_1 = __importDefault(require("path"));
70
73
  var client_1 = require("@dainprotocol/tunnel/client");
@@ -102,21 +105,32 @@ function parseDainApiKey(apiKey) {
102
105
  var parts = apiKey.split("_");
103
106
  if (parts.length < 5)
104
107
  return null;
105
- if (parts[2] === "org") {
106
- if (!parts[3] || parts[4] !== "agent" || !parts[5])
107
- return null;
108
+ var isOrgFormat = parts[2] === "org";
109
+ if (isOrgFormat) {
110
+ var orgId_1 = parts[3];
111
+ var isAgentKeyword = parts[4] === "agent";
112
+ var agentId_1 = parts[5];
108
113
  var secret_1 = parts.slice(6).join("_");
109
- return secret_1 ? { agentId: parts[5], orgId: parts[3], secret: secret_1 } : null;
114
+ if (!orgId_1 || !isAgentKeyword || !agentId_1 || !secret_1)
115
+ return null;
116
+ return { agentId: agentId_1, orgId: orgId_1, secret: secret_1 };
110
117
  }
118
+ var agentId = parts[2];
119
+ var orgId = parts[3];
111
120
  var secret = parts.slice(4).join("_");
112
- return parts[2] && parts[3] && secret ? { agentId: parts[2], orgId: parts[3], secret: secret } : null;
121
+ if (!agentId || !orgId || !secret)
122
+ return null;
123
+ return { agentId: agentId, orgId: orgId, secret: secret };
113
124
  }
114
125
  function loadEnvFiles() {
115
- [".env.local", ".env", ".env.development", ".env.production"].forEach(function (file) {
126
+ var envFiles = [".env.local", ".env", ".env.development", ".env.production"];
127
+ for (var _i = 0, envFiles_1 = envFiles; _i < envFiles_1.length; _i++) {
128
+ var file = envFiles_1[_i];
116
129
  var envPath = path_1.default.join(process.cwd(), file);
117
- if (fs_1.default.existsSync(envPath))
130
+ if (fs_1.default.existsSync(envPath)) {
118
131
  dotenv_1.default.config({ path: envPath });
119
- });
132
+ }
133
+ }
120
134
  }
121
135
  function getDainConfig(configFile) {
122
136
  loadEnvFiles();
@@ -228,3 +242,48 @@ function buildPlatformUrl(config, orgId, endpoint, overrides) {
228
242
  var serviceId = (overrides === null || overrides === void 0 ? void 0 : overrides.serviceId) || config["service-id"];
229
243
  return joinUrl(baseUrl, "/codegen-deploy/".concat(endpoint, "/").concat(orgId, "/").concat(serviceId, "/").concat(deploymentId, "/").concat(environment));
230
244
  }
245
+ function validatePlatformConfig(config, overrides) {
246
+ var orgId = resolveOrgId(config);
247
+ var apiKey = config["api-key"];
248
+ var deploymentId = (overrides === null || overrides === void 0 ? void 0 : overrides.deploymentId) || config["deployment-id"];
249
+ var serviceId = (overrides === null || overrides === void 0 ? void 0 : overrides.serviceId) || config["service-id"];
250
+ if (!orgId) {
251
+ logError("Org ID not found. Ensure your API key or DAIN_ORG_ID is set.");
252
+ process.exit(1);
253
+ }
254
+ if (!deploymentId || !serviceId) {
255
+ logError("Deployment ID or service ID not found");
256
+ process.exit(1);
257
+ }
258
+ if (!apiKey) {
259
+ logError("API key not found");
260
+ process.exit(1);
261
+ }
262
+ return { config: config, orgId: orgId, apiKey: apiKey, deploymentId: deploymentId, serviceId: serviceId };
263
+ }
264
+ function platformRequest(ctx_1, endpoint_1) {
265
+ return __awaiter(this, arguments, void 0, function (ctx, endpoint, method, overrides) {
266
+ var url;
267
+ if (method === void 0) { method = "GET"; }
268
+ return __generator(this, function (_a) {
269
+ url = buildPlatformUrl(ctx.config, ctx.orgId, endpoint, {
270
+ deploymentId: ctx.deploymentId,
271
+ serviceId: ctx.serviceId,
272
+ environment: overrides === null || overrides === void 0 ? void 0 : overrides.environment,
273
+ });
274
+ return [2 /*return*/, fetchWithTimeout(url, {
275
+ method: method,
276
+ headers: { "Content-Type": "application/json", Authorization: "Bearer ".concat(ctx.apiKey) },
277
+ })];
278
+ });
279
+ });
280
+ }
281
+ function createIdempotentCleanup(cleanupFn) {
282
+ var hasRun = false;
283
+ return function idempotentCleanup() {
284
+ if (hasRun)
285
+ return;
286
+ hasRun = true;
287
+ cleanupFn();
288
+ };
289
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dainprotocol/cli",
3
- "version": "1.2.30",
3
+ "version": "1.2.31",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -23,8 +23,8 @@
23
23
  ],
24
24
  "dependencies": {
25
25
  "@ai-sdk/anthropic": "^0.0.50",
26
- "@dainprotocol/service-sdk": "^2.0.77",
27
- "@dainprotocol/tunnel": "^1.1.26",
26
+ "@dainprotocol/service-sdk": "^2.0.79",
27
+ "@dainprotocol/tunnel": "^1.1.29",
28
28
  "@types/fs-extra": "^11.0.4",
29
29
  "@types/localtunnel": "^2.0.4",
30
30
  "ai": "^3.3.41",