@xyd-js/ask-ai-widget 0.0.0-build-2bb31f3-20251012215032 → 0.0.0-build-c348243-20251013000610

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xyd-js/ask-ai-widget",
3
- "version": "0.0.0-build-2bb31f3-20251012215032",
3
+ "version": "0.0.0-build-c348243-20251013000610",
4
4
  "type": "module",
5
5
  "main": "dist/server.js",
6
6
  "files": [
@@ -17,8 +17,8 @@
17
17
  "express": "^4.18.2",
18
18
  "react": "^19.0.0",
19
19
  "react-dom": "^19.0.0",
20
- "@xyd-js/ask-ai": "0.0.0-build-2bb31f3-20251012215032",
21
- "@xyd-js/mcp-server": "0.0.0-build-2bb31f3-20251012215032"
20
+ "@xyd-js/ask-ai": "0.0.0-build-c348243-20251013000610",
21
+ "@xyd-js/mcp-server": "0.0.0-build-c348243-20251013000610"
22
22
  },
23
23
  "scripts": {
24
24
  "build": "bun run build:widget && bun run build:server && bun run build:server-standalone",
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { readFileSync, existsSync } from "node:fs";
2
- import { join } from "node:path";
2
+ import { join, dirname } from "node:path";
3
3
  import { createServer } from "node:http";
4
+ import { fileURLToPath } from "node:url";
4
5
  import express from "express";
5
6
 
6
7
  import { handler as askAiHandler } from "@xyd-js/ask-ai/node";
@@ -8,14 +9,8 @@ import type { MCPServer } from "@xyd-js/mcp-server/mcp";
8
9
 
9
10
  // Start the server
10
11
  export async function startServer(mcpServer?: MCPServer) {
11
- // Check if widget is built
12
- const widgetPath = join(process.cwd(), "dist", "widget.js");
13
-
14
- if (!existsSync(widgetPath)) {
15
- console.log("❌ Widget not found!");
16
- console.log("🔨 Please run: bun run build:widget");
17
- process.exit(1);
18
- }
12
+ const widgetPath = findWidgetPath();
13
+ console.log("✅ Widget found:", widgetPath);
19
14
 
20
15
  // Find an available port
21
16
  const port = await findAvailablePort(parseInt(process.env.PORT || "3500"));
@@ -79,7 +74,7 @@ export async function startServer(mcpServer?: MCPServer) {
79
74
  while (true) {
80
75
  const { done, value } = await reader.read();
81
76
  if (done) break;
82
-
77
+
83
78
  const chunk = decoder.decode(value, { stream: true });
84
79
  res.write(chunk);
85
80
  }
@@ -121,6 +116,30 @@ export async function startServer(mcpServer?: MCPServer) {
121
116
  });
122
117
  }
123
118
 
119
+ // Find the widget file in multiple possible locations
120
+ function findWidgetPath(): string {
121
+ const __filename = fileURLToPath(import.meta.url);
122
+ const __dirname = dirname(__filename);
123
+
124
+ // Look for widget in multiple possible locations
125
+ const possiblePaths = [
126
+ join(process.cwd(), "dist", "widget.js"), // Local development
127
+ join(__dirname, "..", "dist", "widget.js"), // Package installation
128
+ join(__dirname, "dist", "widget.js"), // Alternative package location
129
+ ];
130
+
131
+ for (const path of possiblePaths) {
132
+ if (existsSync(path)) {
133
+ return path;
134
+ }
135
+ }
136
+
137
+ console.log("❌ Widget not found!");
138
+ console.log("🔨 Please run: bun run build:widget");
139
+ console.log("💡 Searched in:", possiblePaths);
140
+ process.exit(1);
141
+ }
142
+
124
143
  // Find an available port
125
144
  async function findAvailablePort(startPort = 3500, maxAttempts = 10) {
126
145
  for (let i = 0; i < maxAttempts; i++) {
package/widget.tsx CHANGED
@@ -8,14 +8,9 @@ import { AskWidget } from "./src/Widget";
8
8
 
9
9
  // Extract data attributes
10
10
  const config = {
11
- askAiServer:
12
- currentScript?.dataset["data-server-url"] || window.__askAi?.serverUrl,
11
+ askAiServer: resolveServerUrl(currentScript),
13
12
  };
14
13
 
15
- if (!config?.askAiServer) {
16
- config.askAiServer = "http://localhost:3500";
17
- }
18
-
19
14
  // Create a container div and append it to body
20
15
  const container = document.createElement("div");
21
16
  container.id = "xyd-ask-ai-widget";
@@ -31,3 +26,29 @@ import { AskWidget } from "./src/Widget";
31
26
  />
32
27
  );
33
28
  })();
29
+
30
+ // Function to resolve server URL with fallback logic
31
+ function resolveServerUrl(currentScript: HTMLScriptElement): string {
32
+ // Priority 1: data-server-url attribute
33
+ if (currentScript?.dataset["serverUrl"]) {
34
+ return currentScript.dataset["serverUrl"];
35
+ }
36
+
37
+ // Priority 2: window.__askAi?.serverUrl
38
+ if (window.__askAi?.serverUrl) {
39
+ return window.__askAi.serverUrl;
40
+ }
41
+
42
+ // Priority 3: Extract from current script's src
43
+ if (currentScript?.src) {
44
+ try {
45
+ const url = new URL(currentScript.src);
46
+ return `${url.protocol}//${url.hostname}${url.port ? `:${url.port}` : ''}`;
47
+ } catch (error) {
48
+ console.warn("Failed to parse script src URL:", error);
49
+ }
50
+ }
51
+
52
+ // Priority 4: Default fallback
53
+ return "http://localhost:3500";
54
+ }