unplugin-devpilot 0.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © 2025-PRESENT Huali (https://github.com/zcf0508)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,62 @@
1
+ //#region src/core/types.d.ts
2
+ interface ClientInfo {
3
+ clientId: string;
4
+ url: string;
5
+ title: string;
6
+ userAgent: string;
7
+ connectedAt: number;
8
+ lastActiveAt: number;
9
+ }
10
+ interface BaseServerFunctions {
11
+ ping: () => string;
12
+ updateClientInfo: (info: Omit<ClientInfo, "clientId" | "connectedAt" | "lastActiveAt">) => void;
13
+ }
14
+ interface PluginServerFunctions {}
15
+ type ServerFunctions = BaseServerFunctions & PluginServerFunctions;
16
+ interface BaseClientFunctions {
17
+ notifyTaskUpdate: (count: number) => void;
18
+ notifyTaskCompleted: (taskId: string) => void;
19
+ }
20
+ interface PluginClientFunctions {}
21
+ type ClientFunctions = BaseClientFunctions & PluginClientFunctions;
22
+ //#endregion
23
+ //#region src/client/types.d.ts
24
+ type RpcHandlers = ClientFunctions;
25
+ type PromisifyServerFunctions<T = ServerFunctions> = { [K in keyof T]: T[K] extends ((...args: infer Args) => infer Return) ? (...args: Args) => Promise<Awaited<Return>> : never };
26
+ interface DevpilotClient<S extends Record<string, any> = ServerFunctions> {
27
+ getClientId: () => string | null;
28
+ rpcCall: <K extends keyof PromisifyServerFunctions<S>>(method: K, ...args: Parameters<PromisifyServerFunctions<S>[K]>) => Promise<ReturnType<PromisifyServerFunctions<S>[K]>>;
29
+ isConnected: () => boolean;
30
+ onConnected: (callback: () => void) => () => void;
31
+ onDisconnected: (callback: () => void) => () => void;
32
+ }
33
+ interface DevpilotClientOptions {
34
+ wsPort: number;
35
+ rpcHandlers?: Partial<RpcHandlers>;
36
+ extendRpcHandlers?: Record<string, (...args: any[]) => any>;
37
+ }
38
+ //#endregion
39
+ //#region src/client/index.d.ts
40
+ declare function createDevpilotClient<S extends Record<string, any> = ServerFunctions>(options: DevpilotClientOptions): DevpilotClient<S>;
41
+ declare function initDevpilot<S extends Record<string, any> = ServerFunctions>(options: DevpilotClientOptions): DevpilotClient<S>;
42
+ declare function getDevpilotClient<S extends Record<string, any> = ServerFunctions>(): DevpilotClient<S> | null;
43
+ /**
44
+ * Define and type-check RPC handlers for the client
45
+ * This ensures the handlers are correctly typed and will be recognized by the RPC system
46
+ * @param handlers - RPC handlers object that extends the base RpcHandlers
47
+ * @returns The handlers object (for convenience)
48
+ * @example
49
+ * ```ts
50
+ * export const rpcHandlers = defineRpcHandlers({
51
+ * querySelector: async (selector: string) => {
52
+ * // implementation
53
+ * },
54
+ * getDOMTree: async (maxDepth: number) => {
55
+ * // implementation
56
+ * }
57
+ * });
58
+ * ```
59
+ */
60
+ declare function defineRpcHandlers<T extends { [K in keyof T]: (...args: any[]) => any }>(handlers: T): T;
61
+ //#endregion
62
+ export { type DevpilotClient, type DevpilotClientOptions, type RpcHandlers, createDevpilotClient, defineRpcHandlers, getDevpilotClient, initDevpilot };
@@ -0,0 +1,146 @@
1
+ //#region src/client/index.ts
2
+ function generateId() {
3
+ return Math.random().toString(36).slice(2, 12);
4
+ }
5
+ function createDevpilotClient(options) {
6
+ const { wsPort, rpcHandlers: customHandlers } = options;
7
+ let ws = null;
8
+ let clientId = null;
9
+ const pendingCalls = /* @__PURE__ */ new Map();
10
+ const connectedCallbacks = /* @__PURE__ */ new Set();
11
+ const disconnectedCallbacks = /* @__PURE__ */ new Set();
12
+ const rpcHandlers = {
13
+ notifyTaskUpdate(count) {
14
+ window.dispatchEvent(new CustomEvent("devpilot:taskUpdate", { detail: { count } }));
15
+ customHandlers?.notifyTaskUpdate?.(count);
16
+ },
17
+ notifyTaskCompleted(taskId) {
18
+ window.dispatchEvent(new CustomEvent("devpilot:taskCompleted", { detail: { taskId } }));
19
+ customHandlers?.notifyTaskCompleted?.(taskId);
20
+ },
21
+ ...customHandlers,
22
+ ...options.extendRpcHandlers || {}
23
+ };
24
+ function connect() {
25
+ ws = new WebSocket(`ws://localhost:${wsPort}`);
26
+ ws.onopen = () => {
27
+ console.log("[devpilot] Connected to server");
28
+ };
29
+ ws.onmessage = (event) => {
30
+ try {
31
+ const data = JSON.parse(event.data);
32
+ if (data.type === "connected") {
33
+ clientId = data.clientId;
34
+ console.log("[devpilot] Client ID:", clientId);
35
+ rpcCall("updateClientInfo", {
36
+ url: location.href,
37
+ title: document.title,
38
+ userAgent: navigator.userAgent
39
+ });
40
+ connectedCallbacks.forEach((cb) => cb());
41
+ return;
42
+ }
43
+ if (data.t === "q" && data.m && data.m in rpcHandlers) {
44
+ const handler = rpcHandlers[data.m];
45
+ Promise.resolve(handler(...data.a || [])).then((result) => {
46
+ if (data.i) ws?.send(JSON.stringify({
47
+ t: "s",
48
+ i: data.i,
49
+ r: result
50
+ }));
51
+ }).catch((error) => {
52
+ if (data.i) ws?.send(JSON.stringify({
53
+ t: "s",
54
+ i: data.i,
55
+ e: error instanceof Error ? error.message : String(error)
56
+ }));
57
+ });
58
+ return;
59
+ }
60
+ if (data.t === "s" && data.i && pendingCalls.has(data.i)) {
61
+ const { resolve, reject } = pendingCalls.get(data.i);
62
+ pendingCalls.delete(data.i);
63
+ if (data.e) reject(new Error(String(data.e)));
64
+ else resolve(data.r);
65
+ }
66
+ } catch (e) {
67
+ console.error("[devpilot] Message parse error:", e);
68
+ }
69
+ };
70
+ ws.onclose = () => {
71
+ console.log("[devpilot] Disconnected, reconnecting...");
72
+ clientId = null;
73
+ disconnectedCallbacks.forEach((cb) => cb());
74
+ setTimeout(connect, 2e3);
75
+ };
76
+ ws.onerror = (err) => {
77
+ console.error("[devpilot] WebSocket error:", err);
78
+ };
79
+ }
80
+ function rpcCall(method, ...args) {
81
+ return new Promise((resolve, reject) => {
82
+ if (!ws || ws.readyState !== WebSocket.OPEN) {
83
+ reject(/* @__PURE__ */ new Error("WebSocket not connected"));
84
+ return;
85
+ }
86
+ const id = generateId();
87
+ pendingCalls.set(id, {
88
+ resolve,
89
+ reject
90
+ });
91
+ ws.send(JSON.stringify({
92
+ t: "q",
93
+ m: method,
94
+ a: args,
95
+ i: id
96
+ }));
97
+ });
98
+ }
99
+ connect();
100
+ return {
101
+ getClientId: () => clientId,
102
+ rpcCall,
103
+ isConnected: () => ws !== null && ws.readyState === WebSocket.OPEN,
104
+ onConnected: (callback) => {
105
+ connectedCallbacks.add(callback);
106
+ return () => connectedCallbacks.delete(callback);
107
+ },
108
+ onDisconnected: (callback) => {
109
+ disconnectedCallbacks.add(callback);
110
+ return () => disconnectedCallbacks.delete(callback);
111
+ }
112
+ };
113
+ }
114
+ let globalClient = null;
115
+ function initDevpilot(options) {
116
+ if (globalClient) return globalClient;
117
+ globalClient = createDevpilotClient(options);
118
+ window.__devpilot = globalClient;
119
+ return globalClient;
120
+ }
121
+ function getDevpilotClient() {
122
+ return globalClient;
123
+ }
124
+ /**
125
+ * Define and type-check RPC handlers for the client
126
+ * This ensures the handlers are correctly typed and will be recognized by the RPC system
127
+ * @param handlers - RPC handlers object that extends the base RpcHandlers
128
+ * @returns The handlers object (for convenience)
129
+ * @example
130
+ * ```ts
131
+ * export const rpcHandlers = defineRpcHandlers({
132
+ * querySelector: async (selector: string) => {
133
+ * // implementation
134
+ * },
135
+ * getDOMTree: async (maxDepth: number) => {
136
+ * // implementation
137
+ * }
138
+ * });
139
+ * ```
140
+ */
141
+ function defineRpcHandlers(handlers) {
142
+ return handlers;
143
+ }
144
+
145
+ //#endregion
146
+ export { createDevpilotClient, defineRpcHandlers, getDevpilotClient, initDevpilot };
@@ -0,0 +1,6 @@
1
+ declare module 'virtual:devpilot-client' {
2
+ import type { DevpilotClient } from 'unplugin-devpilot/client';
3
+
4
+ export const client: DevpilotClient;
5
+ export const wsPort: number;
6
+ }
@@ -0,0 +1,19 @@
1
+ import { t as unpluginDevpilot } from "./index-CyKvTBcJ.mjs";
2
+
3
+ //#region src/farm.d.ts
4
+ /**
5
+ * Farm plugin
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * // farm.config.js
10
+ * import devpilot from 'unplugin-devpilot/farm'
11
+ *
12
+ * export default {
13
+ * plugins: [devpilot()],
14
+ * }
15
+ * ```
16
+ */
17
+ declare const farm: typeof unpluginDevpilot.farm;
18
+ //#endregion
19
+ export { farm as default, farm as "module.exports" };
package/dist/farm.mjs ADDED
@@ -0,0 +1,26 @@
1
+ import { n as unpluginDevpilot } from "./src-aMXlip6o.mjs";
2
+
3
+ //#region src/farm.ts
4
+ /**
5
+ * This entry file is for Farm plugin.
6
+ *
7
+ * @module
8
+ */
9
+ /**
10
+ * Farm plugin
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * // farm.config.js
15
+ * import devpilot from 'unplugin-devpilot/farm'
16
+ *
17
+ * export default {
18
+ * plugins: [devpilot()],
19
+ * }
20
+ * ```
21
+ */
22
+ const farm = unpluginDevpilot.farm;
23
+ var farm_default = farm;
24
+
25
+ //#endregion
26
+ export { farm_default as default, farm as "module.exports" };