@xyd-js/ask-ai-widget 0.0.0-build-87e0566-20251013171826 → 0.0.0-build-6675456-20251014012658

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.
@@ -57211,15 +57211,15 @@ class wa {
57211
57211
  return console.error("Error:", { status: e3, message: t2 }), new Response(JSON.stringify({ error: t2 }), { status: e3, headers: { ...this.corsHeaders, "Content-Type": "application/json" } });
57212
57212
  }
57213
57213
  }
57214
- async function ba(e3) {
57214
+ function ba(e3) {
57215
57215
  if (e3 instanceof Request) {
57216
- return (await wa.New({ ...wa.defaultConfig() }))(e3);
57216
+ return wa.New({ ...wa.defaultConfig() })(e3);
57217
57217
  }
57218
57218
  return wa.New(e3);
57219
57219
  }
57220
57220
 
57221
57221
  // src/index.ts
57222
- async function startServer(mcpServer) {
57222
+ async function startServer(settings, mcpServer) {
57223
57223
  const widgetPath = findWidgetPath();
57224
57224
  console.log("✅ Widget found:", widgetPath);
57225
57225
  const port = await findAvailablePort(parseInt(process.env.PORT || "3500"));
@@ -57227,11 +57227,12 @@ async function startServer(mcpServer) {
57227
57227
  const app = import_express.default();
57228
57228
  app.use(import_express.default.json());
57229
57229
  app.use((req, res, next) => {
57230
- res.header("Access-Control-Allow-Origin", "*");
57231
- res.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
57232
- res.header("Access-Control-Allow-Headers", "Content-Type");
57230
+ res.header(settings.headers);
57233
57231
  next();
57234
57232
  });
57233
+ app.options("*", (req, res) => {
57234
+ res.status(200).end();
57235
+ });
57235
57236
  app.get("/widget.js", (req, res) => {
57236
57237
  try {
57237
57238
  const widgetCode = readFileSync(widgetPath, "utf-8");
@@ -57244,7 +57245,7 @@ async function startServer(mcpServer) {
57244
57245
  res.status(404).send("Widget not found. Please run 'bun run build:widget' first.");
57245
57246
  }
57246
57247
  });
57247
- app.all("/ask", async (req, res) => {
57248
+ app.post("/ask", async (req, res) => {
57248
57249
  try {
57249
57250
  const body = req.method === "POST" ? JSON.stringify(req.body) : undefined;
57250
57251
  const request = new Request(`http://localhost:${port}${req.url}`, {
@@ -57252,7 +57253,18 @@ async function startServer(mcpServer) {
57252
57253
  headers: new Headers(req.headers),
57253
57254
  body
57254
57255
  });
57255
- const response = await ba(request);
57256
+ let mcpUrl = settings.mcp?.url || "";
57257
+ if (Array.isArray(mcpUrl)) {
57258
+ console.warn("MCP as array is not supported, using the first one");
57259
+ mcpUrl = mcpUrl[0];
57260
+ }
57261
+ const askAiHandlerFn = ba({
57262
+ mcpUrl,
57263
+ aiProvider: settings.ai.provider || "",
57264
+ aiModel: settings.ai.model || "",
57265
+ aiToken: settings.ai.token || ""
57266
+ });
57267
+ const response = await askAiHandlerFn(request);
57256
57268
  for (const [key, value] of response.headers.entries()) {
57257
57269
  res.set(key, value);
57258
57270
  }
@@ -57338,5 +57350,129 @@ async function findAvailablePort(startPort = 3500, maxAttempts = 10) {
57338
57350
  throw new Error(`No available ports found between ${startPort} and ${startPort + maxAttempts - 1}`);
57339
57351
  }
57340
57352
 
57353
+ // src/utils.ts
57354
+ import path from "node:path";
57355
+ import fs from "node:fs/promises";
57356
+
57357
+ // ../cli-sdk/dist/index.js
57358
+ function replaceEnvVars(obj, removeUndefined = false) {
57359
+ if (obj === null || obj === undefined) {
57360
+ return obj;
57361
+ }
57362
+ if (typeof obj === "string") {
57363
+ if (obj.includes("$")) {
57364
+ return obj.replace(/\$([A-Z_][A-Z0-9_]*)/g, (match, varName) => {
57365
+ const envValue = process.env[varName];
57366
+ if (envValue === undefined) {
57367
+ if (removeUndefined) {
57368
+ return "";
57369
+ }
57370
+ console.warn(`
57371
+ ⚠️ Environment variable "${varName}" is not set, keeping placeholder: ${match}`);
57372
+ return match;
57373
+ }
57374
+ return envValue;
57375
+ });
57376
+ }
57377
+ return obj;
57378
+ }
57379
+ if (Array.isArray(obj)) {
57380
+ return obj.map((item) => replaceEnvVars(item, removeUndefined));
57381
+ }
57382
+ if (typeof obj === "object") {
57383
+ const result = {};
57384
+ for (const [key, value] of Object.entries(obj)) {
57385
+ result[key] = replaceEnvVars(value, removeUndefined);
57386
+ }
57387
+ return result;
57388
+ }
57389
+ return obj;
57390
+ }
57391
+
57392
+ // src/utils.ts
57393
+ function getSettingsPath() {
57394
+ return path.join(process.cwd(), "askai.json");
57395
+ }
57396
+ async function loadSetting() {
57397
+ const settingsPath = getSettingsPath();
57398
+ try {
57399
+ await fs.access(settingsPath);
57400
+ } catch (error40) {
57401
+ const settings2 = newSettings();
57402
+ ensureSettings(settings2);
57403
+ return settings2;
57404
+ }
57405
+ const settingsJSON = await fs.readFile(settingsPath, "utf-8");
57406
+ let settings = JSON.parse(settingsJSON);
57407
+ settings = replaceEnvVars(settings, true);
57408
+ ensureSettings(settings);
57409
+ return settings;
57410
+ }
57411
+ function newSettings() {
57412
+ return {
57413
+ ai: {
57414
+ provider: "",
57415
+ model: "",
57416
+ token: ""
57417
+ },
57418
+ mcp: {
57419
+ url: ""
57420
+ },
57421
+ headers: {}
57422
+ };
57423
+ }
57424
+ function ensureSettings(settings) {
57425
+ if (!settings.ai) {
57426
+ settings.ai = {
57427
+ provider: "",
57428
+ model: "",
57429
+ token: ""
57430
+ };
57431
+ }
57432
+ if (!settings.mcp) {
57433
+ settings.mcp = {
57434
+ url: ""
57435
+ };
57436
+ }
57437
+ if (!settings.sources) {
57438
+ settings.sources = {
57439
+ openapi: "",
57440
+ llms: ""
57441
+ };
57442
+ }
57443
+ if (process.env.OPENAPI_SOURCE) {
57444
+ settings.sources.openapi = process.env.OPENAPI_SOURCE;
57445
+ console.log("(env settings): using OPENAPI_SOURCE");
57446
+ }
57447
+ if (process.env.LLMS_SOURCE) {
57448
+ settings.sources.llms = process.env.LLMS_SOURCE;
57449
+ console.log("(env settings): using LLMS_SOURCE");
57450
+ }
57451
+ if (process.env.MCP_URL) {
57452
+ settings.mcp.url = process.env.MCP_URL;
57453
+ console.log("(env settings): using MCP_URL");
57454
+ }
57455
+ if (process.env.AI_PROVIDER) {
57456
+ settings.ai.provider = process.env.AI_PROVIDER;
57457
+ console.log("(env settings): using AI_PROVIDER");
57458
+ }
57459
+ if (process.env.AI_MODEL) {
57460
+ settings.ai.model = process.env.AI_MODEL;
57461
+ console.log("(env settings): using AI_MODEL");
57462
+ }
57463
+ if (process.env.AI_TOKEN) {
57464
+ settings.ai.token = process.env.AI_TOKEN;
57465
+ console.log("(env settings): using AI_TOKEN");
57466
+ }
57467
+ if (!settings.headers || !Object.keys(settings.headers).length) {
57468
+ settings.headers = {
57469
+ "Access-Control-Allow-Origin": "*",
57470
+ "Access-Control-Allow-Headers": "Content-Type, Authorization, X-Requested-With",
57471
+ "Access-Control-Allow-Methods": "GET, POST, OPTIONS, PUT, DELETE",
57472
+ "Access-Control-Max-Age": "86400"
57473
+ };
57474
+ }
57475
+ }
57476
+
57341
57477
  // server-standalone.ts
57342
- startServer().catch(console.error);
57478
+ loadSetting().then(startServer).catch(console.error);