@mujian/js-sdk 0.0.6-beta.73 → 0.0.6-beta.74

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/lite.d.ts CHANGED
@@ -14,6 +14,17 @@ declare global {
14
14
  $mujian_lite: MujianSdkLite;
15
15
  }
16
16
  }
17
+ /**
18
+ * @class MujianSdkLite
19
+ * @description 幕间 SDK 的轻量级客户端,用于在浏览器中获取 API 配置。
20
+ * @example
21
+ * ```typescript
22
+ * (window as any).$mujian_lite.init().then(() => {
23
+ * console.log((window as any).$mujian_lite.openapi.baseURL); // API 的 URL
24
+ * console.log((window as any).$mujian_lite.openapi.apiKey); // API 的 KEY (当前用户唯一)
25
+ * });
26
+ * ```
27
+ */
17
28
  declare class MujianSdkLite {
18
29
  /** @hidden */
19
30
  private constructor();
@@ -21,7 +32,9 @@ declare class MujianSdkLite {
21
32
  * 事件
22
33
  * @enum {string}
23
34
  */
35
+ /** @hidden */
24
36
  static EVENT: typeof EVENT;
37
+ /** @hidden */
25
38
  static getInstance(): MujianSdkLite;
26
39
  /** @hidden */
27
40
  private parent;
@@ -30,11 +43,18 @@ declare class MujianSdkLite {
30
43
  /**
31
44
  * 是否初始化
32
45
  */
46
+ /** @hidden */
33
47
  get isReady(): boolean;
34
48
  /** @hidden */
35
49
  private pendingRequests;
50
+ /** @hidden */
36
51
  private _config?;
52
+ /**
53
+ * 获取 API 配置
54
+ * @returns {{ baseURL: string, apiKey: string }} { baseURL: API 的 URL, apiKey: API 的 KEY }
55
+ */
37
56
  get openapi(): Config['openapi'] | undefined;
57
+ /** @hidden */
38
58
  private initPromise;
39
59
  /**
40
60
  * 初始化
package/dist/lite.js ADDED
@@ -0,0 +1,120 @@
1
+ import postmate from "postmate";
2
+ var events_EVENT = /*#__PURE__*/ function(EVENT) {
3
+ EVENT["MUJIAN_INIT"] = "mujian:init";
4
+ EVENT["MUJIAN_AI_CHAT_STOP"] = "mujian:ai:chat:stop";
5
+ EVENT["MUJIAN_AI_CHAT_COMPLETE"] = "mujian:ai:chat:complete";
6
+ EVENT["MUJIAN_AI_CHAT_APPLY_REGEX"] = "mujian:ai:chat:applyRegex";
7
+ EVENT["MUJIAN_AI_CHAT_RENDER_MESSAGE"] = "mujian:ai:chat:renderMessage";
8
+ EVENT["MUJIAN_AI_TEXT_GENERATE"] = "mujian:ai:text:generate";
9
+ EVENT["MUJIAN_AI_OPENAI_COMPLETIONS_CREATE"] = "mujian:ai:openai:completions:create";
10
+ EVENT["MUJIAN_AI_OPENAI_CHAT_COMPLETIONS_CREATE"] = "mujian:ai:openai:chat:completions:create";
11
+ EVENT["MUJIAN_AI_OPENAI_RESPONSES_CREATE"] = "mujian:ai:openai:responses:create";
12
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_ALL"] = "mujian:ai:chat:message:getAll";
13
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_PAGE"] = "mujian:ai:chat:message:getPage";
14
+ EVENT["MUJIAN_AI_CHAT_PROJECT_GET_INFO"] = "mujian:ai:chat:project:getInfo";
15
+ EVENT["MUJIAN_AI_SETTINGS_PERSONA_GET_ACTIVE"] = "mujian:ai:settings:persona:getActive";
16
+ EVENT["MUJIAN_AI_SETTINGS_PERSONA_SET_ACTIVE"] = "mujian:ai:settings:persona:setActive";
17
+ EVENT["MUJIAN_AI_SETTINGS_MODEL_GET_ALL"] = "mujian:ai:settings:model:getAll";
18
+ EVENT["MUJIAN_AI_SETTINGS_MODEL_SET_ACTIVE"] = "mujian:ai:settings:model:setActive";
19
+ EVENT["MUJIAN_AI_SETTINGS_MODEL_GET_ACTIVE"] = "mujian:ai:settings:model:getActive";
20
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_DELETE_ONE"] = "mujian:ai:chat:message:deleteOne";
21
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_EDIT_ONE"] = "mujian:ai:chat:message:editOne";
22
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_SWIPE"] = "mujian:ai:chat:message:swipe";
23
+ EVENT["MUJIAN_AI_CHAT_MESSAGE_GET_PROMPT"] = "mujian:ai:chat:message:getPrompt";
24
+ EVENT["MUJIAN_AI_OPENAI_IMAGES_GENERATE"] = "mujian:ai:openai:images:generate";
25
+ EVENT["MUJIAN_UTILS_CLIPBOARD_WRITE_TEXT"] = "mujian:utils:clipboard:writeText";
26
+ EVENT["MUJIAN_GET_CONFIG"] = "mujian:getConfig";
27
+ return EVENT;
28
+ }({});
29
+ const Log = {
30
+ i (...msg) {
31
+ console.log('[MujianSDK] ', ...msg);
32
+ },
33
+ e (...msg) {
34
+ console.error('[MujianSDK] ', ...msg);
35
+ }
36
+ };
37
+ class MujianSdkLite {
38
+ constructor(){}
39
+ static EVENT = events_EVENT;
40
+ static getInstance() {
41
+ if (!window.$mujian_lite) window.$mujian_lite = new MujianSdkLite();
42
+ return window.$mujian_lite;
43
+ }
44
+ parent = null;
45
+ ready = false;
46
+ get isReady() {
47
+ return this.ready;
48
+ }
49
+ pendingRequests = new Map();
50
+ _config;
51
+ get openapi() {
52
+ return this._config?.openapi;
53
+ }
54
+ initPromise = null;
55
+ async init() {
56
+ if (this.initPromise) return this.initPromise;
57
+ this.initPromise = (async ()=>{
58
+ const handshake = new postmate.Model({
59
+ reply: ({ id, complete, data, error })=>{
60
+ const call = this.pendingRequests.get(id);
61
+ if (!call) return;
62
+ if (error) {
63
+ call.reject(error);
64
+ this.pendingRequests.delete(id);
65
+ return;
66
+ }
67
+ call.onData?.(data);
68
+ if (complete) {
69
+ call.onComplete?.();
70
+ call.resolve(data);
71
+ this.pendingRequests.delete(id);
72
+ }
73
+ }
74
+ });
75
+ try {
76
+ const parent = await handshake;
77
+ this.parent = parent;
78
+ this.ready = true;
79
+ Log.i('mujian sdk lite client init');
80
+ await this.call(events_EVENT.MUJIAN_INIT);
81
+ this._config = await this.call(events_EVENT.MUJIAN_GET_CONFIG);
82
+ } catch (error) {
83
+ Log.e('init error', error);
84
+ throw error;
85
+ }
86
+ })();
87
+ return this.initPromise;
88
+ }
89
+ ensureReady() {
90
+ if (!this.ready) throw new Error('MujianLite is not initialized');
91
+ }
92
+ emit(event, data) {
93
+ this.ensureReady();
94
+ this.parent?.emit(event, data);
95
+ }
96
+ nextCallId = 0;
97
+ getCallId() {
98
+ const id = this.nextCallId;
99
+ this.nextCallId += 1;
100
+ return `lite_${id}`;
101
+ }
102
+ async call(event, data, controller) {
103
+ this.ensureReady();
104
+ const callId = this.getCallId();
105
+ return new Promise((resolve, reject)=>{
106
+ this.pendingRequests.set(callId, {
107
+ resolve,
108
+ reject,
109
+ ...controller
110
+ });
111
+ this.emit(event, {
112
+ id: callId,
113
+ data
114
+ });
115
+ });
116
+ }
117
+ }
118
+ MujianSdkLite.getInstance().init();
119
+ const lite = MujianSdkLite;
120
+ export { events_EVENT as EVENT, lite as default };
@@ -1,4 +1,10 @@
1
1
  import { type MujianSdk } from '../index.ts';
2
+ /**
3
+ * @interface Config
4
+ * @description 配置接口
5
+ * @property {string} baseUrl API 的 URL
6
+ * @property {string} apiKey API 的 KEY (当前用户唯一)
7
+ */
2
8
  export type Config = {
3
9
  openapi: {
4
10
  baseUrl: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mujian/js-sdk",
3
- "version": "0.0.6-beta.73",
3
+ "version": "0.0.6-beta.74",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -14,6 +14,10 @@
14
14
  "types": "./dist/react/index.d.ts",
15
15
  "import": "./dist/react.js"
16
16
  },
17
+ "./lite": {
18
+ "types": "./dist/lite.d.ts",
19
+ "import": "./dist/lite.js"
20
+ },
17
21
  "./react.css": {
18
22
  "import": "./dist/react.css"
19
23
  }