@dainprotocol/cli 1.2.28 → 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.
package/dist/utils.js CHANGED
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -39,6 +50,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
39
50
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
51
  };
41
52
  Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.DEFAULT_API_BASE_URL = exports.DEFAULT_PLATFORM_BASE_URL = exports.DEFAULT_TUNNEL_BASE_URL = void 0;
54
+ exports.normalizeBaseUrl = normalizeBaseUrl;
55
+ exports.joinUrl = joinUrl;
56
+ exports.parseEnvContent = parseEnvContent;
42
57
  exports.getDainConfig = getDainConfig;
43
58
  exports.displayTunnelUrl = displayTunnelUrl;
44
59
  exports.setupProxy = setupProxy;
@@ -47,51 +62,101 @@ exports.logSuccess = logSuccess;
47
62
  exports.logInfo = logInfo;
48
63
  exports.getStaticFilesPath = getStaticFilesPath;
49
64
  exports.extractOrgId = extractOrgId;
65
+ exports.resolveOrgId = resolveOrgId;
66
+ exports.fetchWithTimeout = fetchWithTimeout;
67
+ exports.buildPlatformUrl = buildPlatformUrl;
68
+ exports.validatePlatformConfig = validatePlatformConfig;
69
+ exports.platformRequest = platformRequest;
70
+ exports.createIdempotentCleanup = createIdempotentCleanup;
50
71
  var fs_1 = __importDefault(require("fs"));
51
72
  var path_1 = __importDefault(require("path"));
52
73
  var client_1 = require("@dainprotocol/tunnel/client");
53
74
  var ora_1 = __importDefault(require("ora"));
54
75
  var chalk_1 = __importDefault(require("chalk"));
55
76
  var dotenv_1 = __importDefault(require("dotenv"));
77
+ exports.DEFAULT_TUNNEL_BASE_URL = "wss://tunnel.dain-local.com";
78
+ exports.DEFAULT_PLATFORM_BASE_URL = "https://codegen-deploy-service.dainapp.com";
79
+ exports.DEFAULT_API_BASE_URL = "https://dain-platform-ochre.vercel.app";
80
+ function normalizeBaseUrl(value) {
81
+ var trimmed = value.trim();
82
+ if (!trimmed)
83
+ return trimmed;
84
+ try {
85
+ return new URL(trimmed).toString().replace(/\/+$/, "");
86
+ }
87
+ catch (_a) {
88
+ return trimmed.replace(/\/+$/, "");
89
+ }
90
+ }
91
+ function joinUrl(baseUrl, pathname) {
92
+ return "".concat(normalizeBaseUrl(baseUrl), "/").concat(pathname.replace(/^\/+/, ""));
93
+ }
94
+ function parseEnvContent(envContent) {
95
+ return Object.entries(dotenv_1.default.parse(envContent))
96
+ .map(function (_a) {
97
+ var name = _a[0], value = _a[1];
98
+ return ({ name: name.trim(), value: value });
99
+ })
100
+ .filter(function (env) { return env.name !== "" && env.value !== ""; });
101
+ }
102
+ function parseDainApiKey(apiKey) {
103
+ if (!(apiKey === null || apiKey === void 0 ? void 0 : apiKey.startsWith("sk_agent_")))
104
+ return null;
105
+ var parts = apiKey.split("_");
106
+ if (parts.length < 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];
113
+ var secret_1 = parts.slice(6).join("_");
114
+ if (!orgId_1 || !isAgentKeyword || !agentId_1 || !secret_1)
115
+ return null;
116
+ return { agentId: agentId_1, orgId: orgId_1, secret: secret_1 };
117
+ }
118
+ var agentId = parts[2];
119
+ var orgId = parts[3];
120
+ var secret = parts.slice(4).join("_");
121
+ if (!agentId || !orgId || !secret)
122
+ return null;
123
+ return { agentId: agentId, orgId: orgId, secret: secret };
124
+ }
56
125
  function loadEnvFiles() {
57
126
  var envFiles = [".env.local", ".env", ".env.development", ".env.production"];
58
- envFiles.forEach(function (file) {
127
+ for (var _i = 0, envFiles_1 = envFiles; _i < envFiles_1.length; _i++) {
128
+ var file = envFiles_1[_i];
59
129
  var envPath = path_1.default.join(process.cwd(), file);
60
130
  if (fs_1.default.existsSync(envPath)) {
61
131
  dotenv_1.default.config({ path: envPath });
62
132
  }
63
- });
133
+ }
64
134
  }
65
135
  function getDainConfig(configFile) {
66
- loadEnvFiles(); // Loads all env files first
67
- var defaultConfigPath = path_1.default.join(process.cwd(), "dain.json");
68
- var configPath = configFile ? path_1.default.join(process.cwd(), configFile) : defaultConfigPath;
136
+ loadEnvFiles();
137
+ var configPath = path_1.default.join(process.cwd(), configFile || "dain.json");
69
138
  if (!fs_1.default.existsSync(configPath)) {
70
139
  logError("Configuration file not found: ".concat(configPath));
71
140
  process.exit(1);
72
141
  }
73
142
  try {
74
- var configData = fs_1.default.readFileSync(configPath, "utf8");
75
- var config = JSON.parse(configData);
76
- // Validate required fields
77
- if (!config["main-file"]) {
143
+ var config = JSON.parse(fs_1.default.readFileSync(configPath, "utf8"));
144
+ if (!config["main-file"])
78
145
  throw new Error("Configuration must include 'main-file'");
79
- }
80
- // Set default values for optional fields
81
- config["environment"] = config["environment"] || "development";
82
- config["version"] = config["version"] || "1.0.0";
83
- config["out-dir"] = config["out-dir"] || "dist"; // Default to 'dist' if not specified
84
- config["tunnel-base-url"] = config["tunnel-base-url"] || "wss://tunnel.dain-local.com"; // Default value if not specified
85
- config["runtime"] = config["runtime"] || "node"; // Add this line
86
- // Handle API key
87
- if (!config["api-key"] ||
88
- config["api-key"] === "env" ||
89
- config["api-key"] === "MUST PUT IN .env.development as DAIN_API_KEY=YOUR_API_KEY") {
146
+ config.environment || (config.environment = "development");
147
+ config.version || (config.version = "1.0.0");
148
+ config["out-dir"] || (config["out-dir"] = "dist");
149
+ config.runtime || (config.runtime = "node");
150
+ config["tunnel-base-url"] = normalizeBaseUrl(config["tunnel-base-url"] || exports.DEFAULT_TUNNEL_BASE_URL);
151
+ if (config["platform-base-url"])
152
+ config["platform-base-url"] = normalizeBaseUrl(config["platform-base-url"]);
153
+ if (config["api-base-url"])
154
+ config["api-base-url"] = normalizeBaseUrl(config["api-base-url"]);
155
+ if (!config["api-key"] || config["api-key"] === "env" || config["api-key"].startsWith("MUST PUT")) {
90
156
  config["api-key"] = process.env.DAIN_API_KEY;
91
157
  }
92
- if (!config["api-key"]) {
158
+ if (!config["api-key"])
93
159
  throw new Error("API key is not set in config or DAIN_API_KEY environment variable");
94
- }
95
160
  return config;
96
161
  }
97
162
  catch (error) {
@@ -99,13 +164,7 @@ function getDainConfig(configFile) {
99
164
  }
100
165
  }
101
166
  function displayTunnelUrl(tunnelUrl) {
102
- var divider = chalk_1.default.green("------------------------------------------------------------");
103
- var header = chalk_1.default.green("Your service is available publicly at:");
104
- var url = chalk_1.default.cyan.underline(tunnelUrl);
105
- var info = chalk_1.default.yellow("This service URL can be connected to by a DAIN client or assistant");
106
- var subInfo = chalk_1.default.yellow("(such as butterfly in development mode)");
107
- var warning = chalk_1.default.red("You should not visit this URL directly");
108
- console.log("\n".concat(divider, "\n").concat(header, "\n").concat(url, "\n\n").concat(info, "\n").concat(subInfo, "\n\n").concat(warning, "\n").concat(divider, "\n"));
167
+ console.log("\n".concat(chalk_1.default.green("------------------------------------------------------------"), "\n").concat(chalk_1.default.green("Your service is available publicly at:"), "\n").concat(chalk_1.default.cyan.underline(tunnelUrl), "\n\n").concat(chalk_1.default.yellow("This service URL can be connected to by a DAIN client or assistant"), "\n").concat(chalk_1.default.yellow("(such as butterfly in development mode)"), "\n\n").concat(chalk_1.default.red("You should not visit this URL directly"), "\n").concat(chalk_1.default.green("------------------------------------------------------------"), "\n"));
109
168
  }
110
169
  function setupProxy(port, apiKey, config) {
111
170
  return __awaiter(this, void 0, void 0, function () {
@@ -117,12 +176,11 @@ function setupProxy(port, apiKey, config) {
117
176
  _a.label = 1;
118
177
  case 1:
119
178
  _a.trys.push([1, 3, , 4]);
120
- client = new client_1.DainTunnel(config["tunnel-base-url"] || "wss://tunnel.dain-local.com", apiKey);
179
+ client = new client_1.DainTunnel(config["tunnel-base-url"] || exports.DEFAULT_TUNNEL_BASE_URL, apiKey);
121
180
  return [4 /*yield*/, client.start(parseInt(port))];
122
181
  case 2:
123
182
  tunnelUrl = _a.sent();
124
183
  spinner.succeed("Proxy setup complete");
125
- displayTunnelUrl(tunnelUrl);
126
184
  return [2 /*return*/, { client: client, tunnelUrl: tunnelUrl }];
127
185
  case 3:
128
186
  error_1 = _a.sent();
@@ -136,9 +194,8 @@ function setupProxy(port, apiKey, config) {
136
194
  }
137
195
  function logError(message, error) {
138
196
  console.error(chalk_1.default.red("\nError: ".concat(message)));
139
- if (error) {
197
+ if (error)
140
198
  console.error(chalk_1.default.red(error));
141
- }
142
199
  }
143
200
  function logSuccess(message) {
144
201
  console.log(chalk_1.default.green("\nSuccess: ".concat(message)));
@@ -150,6 +207,83 @@ function getStaticFilesPath() {
150
207
  return path_1.default.join(process.cwd(), "static");
151
208
  }
152
209
  function extractOrgId(apiKey) {
153
- var apiKeySplit = apiKey === null || apiKey === void 0 ? void 0 : apiKey.split("_");
154
- return apiKeySplit ? apiKeySplit[2] : "";
210
+ var _a;
211
+ return ((_a = parseDainApiKey(apiKey)) === null || _a === void 0 ? void 0 : _a.orgId) || "";
212
+ }
213
+ function resolveOrgId(config) {
214
+ return config["org-id"] || process.env.DAIN_ORG_ID || extractOrgId(config["api-key"]);
215
+ }
216
+ function fetchWithTimeout(url_1, options_1) {
217
+ return __awaiter(this, arguments, void 0, function (url, options, timeoutMs) {
218
+ var controller, timeoutId;
219
+ if (timeoutMs === void 0) { timeoutMs = 30000; }
220
+ return __generator(this, function (_a) {
221
+ switch (_a.label) {
222
+ case 0:
223
+ controller = new AbortController();
224
+ timeoutId = setTimeout(function () { return controller.abort(); }, timeoutMs);
225
+ _a.label = 1;
226
+ case 1:
227
+ _a.trys.push([1, , 3, 4]);
228
+ return [4 /*yield*/, fetch(url, __assign(__assign({}, options), { signal: controller.signal }))];
229
+ case 2: return [2 /*return*/, _a.sent()];
230
+ case 3:
231
+ clearTimeout(timeoutId);
232
+ return [7 /*endfinally*/];
233
+ case 4: return [2 /*return*/];
234
+ }
235
+ });
236
+ });
237
+ }
238
+ function buildPlatformUrl(config, orgId, endpoint, overrides) {
239
+ var baseUrl = config["platform-base-url"] || exports.DEFAULT_PLATFORM_BASE_URL;
240
+ var deploymentId = (overrides === null || overrides === void 0 ? void 0 : overrides.deploymentId) || config["deployment-id"];
241
+ var environment = (overrides === null || overrides === void 0 ? void 0 : overrides.environment) || config["environment"];
242
+ var serviceId = (overrides === null || overrides === void 0 ? void 0 : overrides.serviceId) || config["service-id"];
243
+ return joinUrl(baseUrl, "/codegen-deploy/".concat(endpoint, "/").concat(orgId, "/").concat(serviceId, "/").concat(deploymentId, "/").concat(environment));
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
+ };
155
289
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dainprotocol/cli",
3
- "version": "1.2.28",
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",