skybridge 0.0.0-dev.b095cd5 → 0.0.0-dev.b25e54f
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/README.md +196 -1
- package/dist/src/server/server.d.ts +1 -0
- package/dist/src/server/server.js +20 -10
- package/dist/src/server/server.js.map +1 -1
- package/dist/src/server/templateHelper.d.ts +9 -8
- package/dist/src/server/templateHelper.js +0 -8
- package/dist/src/server/templateHelper.js.map +1 -1
- package/dist/src/server/templates/development.hbs +8 -0
- package/dist/src/server/templates/production.hbs +3 -2
- package/dist/src/server/widgetsDevServer.d.ts +5 -2
- package/dist/src/server/widgetsDevServer.js +5 -2
- package/dist/src/server/widgetsDevServer.js.map +1 -1
- package/dist/src/test/widget.test.js +24 -6
- package/dist/src/test/widget.test.js.map +1 -1
- package/dist/src/web/hooks/index.d.ts +13 -0
- package/dist/src/web/hooks/index.js +14 -0
- package/dist/src/web/hooks/index.js.map +1 -0
- package/dist/src/web/hooks/use-call-tool.d.ts +62 -0
- package/dist/src/web/hooks/use-call-tool.js +68 -0
- package/dist/src/web/hooks/use-call-tool.js.map +1 -0
- package/dist/src/web/hooks/use-call-tool.test.js +163 -0
- package/dist/src/web/hooks/use-call-tool.test.js.map +1 -0
- package/dist/src/web/hooks/use-display-mode.d.ts +4 -0
- package/dist/src/web/hooks/use-display-mode.js +7 -0
- package/dist/src/web/hooks/use-display-mode.js.map +1 -0
- package/dist/src/web/hooks/use-display-mode.test.d.ts +1 -0
- package/dist/src/web/hooks/use-display-mode.test.js +40 -0
- package/dist/src/web/hooks/use-display-mode.test.js.map +1 -0
- package/dist/src/web/hooks/use-locale.d.ts +1 -0
- package/dist/src/web/hooks/use-locale.js +5 -0
- package/dist/src/web/hooks/use-locale.js.map +1 -0
- package/dist/src/web/hooks/use-locale.test.d.ts +1 -0
- package/dist/src/web/hooks/use-locale.test.js +21 -0
- package/dist/src/web/hooks/use-locale.test.js.map +1 -0
- package/dist/src/web/hooks/use-open-external.d.ts +1 -0
- package/dist/src/web/hooks/use-open-external.js +6 -0
- package/dist/src/web/hooks/use-open-external.js.map +1 -0
- package/dist/src/web/hooks/use-open-external.test.d.ts +1 -0
- package/dist/src/web/hooks/use-open-external.test.js +24 -0
- package/dist/src/web/hooks/use-open-external.test.js.map +1 -0
- package/dist/src/web/{use-openai-global.d.ts → hooks/use-openai-global.d.ts} +1 -1
- package/dist/src/web/{use-openai-global.js → hooks/use-openai-global.js} +4 -2
- package/dist/src/web/hooks/use-openai-global.js.map +1 -0
- package/dist/src/web/hooks/use-request-modal.d.ts +5 -0
- package/dist/src/web/hooks/use-request-modal.js +9 -0
- package/dist/src/web/hooks/use-request-modal.js.map +1 -0
- package/dist/src/web/hooks/use-request-modal.test.d.ts +1 -0
- package/dist/src/web/hooks/use-request-modal.test.js +24 -0
- package/dist/src/web/hooks/use-request-modal.test.js.map +1 -0
- package/dist/src/web/hooks/use-send-follow-up-message.d.ts +1 -0
- package/dist/src/web/hooks/use-send-follow-up-message.js +11 -0
- package/dist/src/web/hooks/use-send-follow-up-message.js.map +1 -0
- package/dist/src/web/hooks/use-theme.d.ts +1 -0
- package/dist/src/web/hooks/use-theme.js +5 -0
- package/dist/src/web/hooks/use-theme.js.map +1 -0
- package/dist/src/web/hooks/use-theme.test.d.ts +1 -0
- package/dist/src/web/hooks/use-theme.test.js +26 -0
- package/dist/src/web/hooks/use-theme.test.js.map +1 -0
- package/dist/src/web/hooks/use-tool-info.d.ts +25 -0
- package/dist/src/web/hooks/use-tool-info.js +22 -0
- package/dist/src/web/hooks/use-tool-info.js.map +1 -0
- package/dist/src/web/hooks/use-tool-info.test-d.d.ts +1 -0
- package/dist/src/web/hooks/use-tool-info.test-d.js +74 -0
- package/dist/src/web/hooks/use-tool-info.test-d.js.map +1 -0
- package/dist/src/web/hooks/use-tool-info.test.d.ts +1 -0
- package/dist/src/web/hooks/use-tool-info.test.js +59 -0
- package/dist/src/web/hooks/use-tool-info.test.js.map +1 -0
- package/dist/src/web/hooks/use-tool-output.d.ts +4 -0
- package/dist/src/web/hooks/use-tool-output.js +9 -0
- package/dist/src/web/hooks/use-tool-output.js.map +1 -0
- package/dist/src/web/hooks/use-tool-response-metadata.d.ts +4 -0
- package/dist/src/web/hooks/use-tool-response-metadata.js +8 -0
- package/dist/src/web/hooks/use-tool-response-metadata.js.map +1 -0
- package/dist/src/web/hooks/use-user-agent.d.ts +1 -0
- package/dist/src/web/hooks/use-user-agent.js +5 -0
- package/dist/src/web/hooks/use-user-agent.js.map +1 -0
- package/dist/src/web/hooks/use-user-agent.test.d.ts +1 -0
- package/dist/src/web/hooks/use-user-agent.test.js +31 -0
- package/dist/src/web/hooks/use-user-agent.test.js.map +1 -0
- package/dist/src/web/hooks/use-widget-state.d.ts +4 -0
- package/dist/src/web/hooks/use-widget-state.js +54 -0
- package/dist/src/web/hooks/use-widget-state.js.map +1 -0
- package/dist/src/web/hooks/use-widget-state.test.d.ts +1 -0
- package/dist/src/web/hooks/use-widget-state.test.js +60 -0
- package/dist/src/web/hooks/use-widget-state.test.js.map +1 -0
- package/dist/src/web/index.d.ts +3 -2
- package/dist/src/web/index.js +3 -2
- package/dist/src/web/index.js.map +1 -1
- package/dist/src/web/llm-describe.d.ts +13 -0
- package/dist/src/web/llm-describe.js +62 -0
- package/dist/src/web/llm-describe.js.map +1 -0
- package/dist/src/web/llm-describe.test.d.ts +1 -0
- package/dist/src/web/llm-describe.test.js +76 -0
- package/dist/src/web/llm-describe.test.js.map +1 -0
- package/dist/src/web/mount-widget.js +7 -3
- package/dist/src/web/mount-widget.js.map +1 -1
- package/dist/src/web/plugin/data-llm.d.ts +12 -0
- package/dist/src/web/plugin/data-llm.js +96 -0
- package/dist/src/web/plugin/data-llm.js.map +1 -0
- package/dist/src/web/plugin/data-llm.test.d.ts +1 -0
- package/dist/src/web/plugin/data-llm.test.js +81 -0
- package/dist/src/web/plugin/data-llm.test.js.map +1 -0
- package/dist/src/web/plugin/plugin.d.ts +2 -0
- package/dist/src/web/plugin/plugin.js +33 -0
- package/dist/src/web/plugin/plugin.js.map +1 -0
- package/dist/src/web/types.d.ts +26 -4
- package/dist/src/web/types.js +1 -0
- package/dist/src/web/types.js.map +1 -1
- package/dist/vitest.config.js +0 -1
- package/dist/vitest.config.js.map +1 -1
- package/package.json +13 -6
- package/dist/src/server/templates/vite-client.hbs +0 -7
- package/dist/src/test/setup.js +0 -9
- package/dist/src/test/setup.js.map +0 -1
- package/dist/src/web/use-openai-global.js.map +0 -1
- package/dist/src/web/use-tool-output.d.ts +0 -3
- package/dist/src/web/use-tool-output.js +0 -5
- package/dist/src/web/use-tool-output.js.map +0 -1
- /package/dist/src/{test/setup.d.ts → web/hooks/use-call-tool.test.d.ts} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { fireEvent, renderHook, waitFor, act } from "@testing-library/react";
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { useToolInfo } from "./use-tool-info.js";
|
|
4
|
+
import { SET_GLOBALS_EVENT_TYPE, SetGlobalsEvent, } from "../types.js";
|
|
5
|
+
describe("useToolInfo", () => {
|
|
6
|
+
let OpenaiMock;
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
OpenaiMock = {
|
|
9
|
+
toolInput: { name: "pokemon", args: { name: "pikachu" } },
|
|
10
|
+
toolOutput: null,
|
|
11
|
+
toolResponseMetadata: null,
|
|
12
|
+
};
|
|
13
|
+
vi.stubGlobal("openai", OpenaiMock);
|
|
14
|
+
});
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
vi.unstubAllGlobals();
|
|
17
|
+
vi.resetAllMocks();
|
|
18
|
+
});
|
|
19
|
+
it("should return toolInput on initial mount window.openai", () => {
|
|
20
|
+
const { result } = renderHook(() => useToolInfo());
|
|
21
|
+
expect(result.current).toMatchObject({
|
|
22
|
+
input: { name: "pokemon", args: { name: "pikachu" } },
|
|
23
|
+
status: "pending",
|
|
24
|
+
isPending: true,
|
|
25
|
+
isSuccess: false,
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
it("should eventually return tool output and response metadata once tool call completes", async () => {
|
|
29
|
+
const toolOutput = {
|
|
30
|
+
name: "pikachu",
|
|
31
|
+
color: "yellow",
|
|
32
|
+
description: "When several of these POKéMON gather, their\felectricity could build and cause lightning storms.",
|
|
33
|
+
};
|
|
34
|
+
const toolResponseMetadata = { id: 12 };
|
|
35
|
+
const { result } = renderHook(() => useToolInfo());
|
|
36
|
+
act(() => {
|
|
37
|
+
OpenaiMock.toolOutput = toolOutput;
|
|
38
|
+
OpenaiMock.toolResponseMetadata = toolResponseMetadata;
|
|
39
|
+
fireEvent(window, new SetGlobalsEvent(SET_GLOBALS_EVENT_TYPE, {
|
|
40
|
+
detail: {
|
|
41
|
+
globals: {
|
|
42
|
+
toolOutput,
|
|
43
|
+
toolResponseMetadata,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
}));
|
|
47
|
+
});
|
|
48
|
+
await waitFor(() => {
|
|
49
|
+
expect(result.current).toMatchObject({
|
|
50
|
+
status: "success",
|
|
51
|
+
isPending: false,
|
|
52
|
+
isSuccess: true,
|
|
53
|
+
output: toolOutput,
|
|
54
|
+
responseMetadata: toolResponseMetadata,
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
//# sourceMappingURL=use-tool-info.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-tool-info.test.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-tool-info.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,wBAAwB,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,QAAQ,CAA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MAAM,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,WAAW,EAAE,CAAC,CAAC;QAEnD,GAAG,CAAC,GAAG,EAAE;YACP,UAAU,CAAC,UAAU,GAAG,UAAU,CAAC;YACnC,UAAU,CAAC,oBAAoB,GAAG,oBAAoB,CAAC;YACvD,SAAS,CACP,MAAM,EACN,IAAI,eAAe,CAAC,sBAAsB,EAAE;gBAC1C,MAAM,EAAE;oBACN,OAAO,EAAE;wBACP,UAAU;wBACV,oBAAoB;qBACrB;iBACF;aACF,CAAC,CACH,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,MAAM,OAAO,CAAC,GAAG,EAAE;YACjB,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,aAAa,CAAC;gBACnC,MAAM,EAAE,SAAS;gBACjB,SAAS,EAAE,KAAK;gBAChB,SAAS,EAAE,IAAI;gBACf,MAAM,EAAE,UAAU;gBAClB,gBAAgB,EAAE,oBAAoB;aACvC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useToolInfo } from "./use-tool-info.js";
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated This hook is deprecated. Use `useToolInfo()` instead and access the `output` property.
|
|
4
|
+
*/
|
|
5
|
+
export function useToolOutput() {
|
|
6
|
+
const { output } = useToolInfo();
|
|
7
|
+
return output;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=use-tool-output.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-tool-output.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-tool-output.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD;;GAEG;AACH,MAAM,UAAU,aAAa;IAC3B,MAAM,EAAE,MAAM,EAAE,GAAG,WAAW,EAAE,CAAC;IAEjC,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { useOpenAiGlobal } from "./use-openai-global.js";
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated This hook is deprecated. Use `useToolInfo()` instead and access the `responseMetadata` property.
|
|
4
|
+
*/
|
|
5
|
+
export function useToolResponseMetadata() {
|
|
6
|
+
return useOpenAiGlobal("toolResponseMetadata");
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=use-tool-response-metadata.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-tool-response-metadata.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-tool-response-metadata.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD;;GAEG;AACH,MAAM,UAAU,uBAAuB;IACrC,OAAO,eAAe,CAAC,sBAAsB,CAAC,CAAC;AACjD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useUserAgent(): import("../types.js").UserAgent;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-user-agent.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-user-agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,MAAM,UAAU,YAAY;IAC1B,OAAO,eAAe,CAAC,WAAW,CAAE,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useUserAgent } from "./use-user-agent.js";
|
|
2
|
+
import { describe, it, expect, vi, beforeEach, afterEach, } from "vitest";
|
|
3
|
+
import { renderHook } from "@testing-library/react";
|
|
4
|
+
describe("useUserAgent", () => {
|
|
5
|
+
let OpenaiMock;
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
OpenaiMock = {
|
|
8
|
+
userAgent: {
|
|
9
|
+
device: { type: "mobile" },
|
|
10
|
+
capabilities: { hover: false, touch: true },
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
vi.stubGlobal("openai", OpenaiMock);
|
|
14
|
+
});
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
vi.unstubAllGlobals();
|
|
17
|
+
vi.resetAllMocks();
|
|
18
|
+
});
|
|
19
|
+
it("should return the current user agent from window.openai.userAgent", () => {
|
|
20
|
+
OpenaiMock.userAgent = {
|
|
21
|
+
device: { type: "mobile" },
|
|
22
|
+
capabilities: { hover: false, touch: true },
|
|
23
|
+
};
|
|
24
|
+
const { result } = renderHook(() => useUserAgent());
|
|
25
|
+
expect(result.current).toEqual({
|
|
26
|
+
device: { type: "mobile" },
|
|
27
|
+
capabilities: { hover: false, touch: true },
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
//# sourceMappingURL=use-user-agent.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-user-agent.test.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-user-agent.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EACL,QAAQ,EACR,EAAE,EACF,MAAM,EACN,EAAE,EACF,UAAU,EACV,SAAS,GAEV,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAGpD,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,IAAI,UAEH,CAAC;IAEF,UAAU,CAAC,GAAG,EAAE;QACd,UAAU,GAAG;YACX,SAAS,EAAE;gBACT,MAAM,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;gBAC1B,YAAY,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE;aAC5C;SACF,CAAC;QACF,EAAE,CAAC,UAAU,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;IAEH,SAAS,CAAC,GAAG,EAAE;QACb,EAAE,CAAC,gBAAgB,EAAE,CAAC;QACtB,EAAE,CAAC,aAAa,EAAE,CAAC;IACrB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,mEAAmE,EAAE,GAAG,EAAE;QAC3E,UAAU,CAAC,SAAS,GAAG;YACrB,MAAM,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;YAC1B,YAAY,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE;SAC5C,CAAC;QACF,MAAM,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,YAAY,EAAE,CAAC,CAAC;QAEpD,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC;YAC7B,MAAM,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;YAC1B,YAAY,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE;SAC5C,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type SetStateAction } from "react";
|
|
2
|
+
import type { UnknownObject } from "../types.js";
|
|
3
|
+
export declare function useWidgetState<T extends UnknownObject>(defaultState: T | (() => T)): readonly [T, (state: SetStateAction<T>) => void];
|
|
4
|
+
export declare function useWidgetState<T extends UnknownObject>(defaultState?: T | (() => T | null) | null): readonly [T | null, (state: SetStateAction<T | null>) => void];
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import { useOpenAiGlobal } from "./use-openai-global.js";
|
|
3
|
+
const WIDGET_CONTEXT_KEY = "__widget_context";
|
|
4
|
+
function filterWidgetContext(state) {
|
|
5
|
+
if (state === null) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
const { [WIDGET_CONTEXT_KEY]: _, ...filteredState } = state;
|
|
9
|
+
return filteredState;
|
|
10
|
+
}
|
|
11
|
+
function preserveWidgetContext(newState) {
|
|
12
|
+
if (newState === null) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const currentWindowState = window.openai?.widgetState;
|
|
16
|
+
if (currentWindowState !== null &&
|
|
17
|
+
currentWindowState !== undefined &&
|
|
18
|
+
WIDGET_CONTEXT_KEY in currentWindowState) {
|
|
19
|
+
return {
|
|
20
|
+
...newState,
|
|
21
|
+
[WIDGET_CONTEXT_KEY]: currentWindowState[WIDGET_CONTEXT_KEY],
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return newState;
|
|
25
|
+
}
|
|
26
|
+
export function useWidgetState(defaultState) {
|
|
27
|
+
const widgetStateFromWindow = useOpenAiGlobal("widgetState");
|
|
28
|
+
const [widgetState, _setWidgetState] = useState(() => {
|
|
29
|
+
if (widgetStateFromWindow !== null) {
|
|
30
|
+
return filterWidgetContext(widgetStateFromWindow);
|
|
31
|
+
}
|
|
32
|
+
return typeof defaultState === "function"
|
|
33
|
+
? defaultState()
|
|
34
|
+
: defaultState ?? null;
|
|
35
|
+
});
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
// Fixes openai implementation bug
|
|
38
|
+
if (widgetStateFromWindow !== null) {
|
|
39
|
+
_setWidgetState(filterWidgetContext(widgetStateFromWindow));
|
|
40
|
+
}
|
|
41
|
+
}, [widgetStateFromWindow]);
|
|
42
|
+
const setWidgetState = useCallback((state) => {
|
|
43
|
+
_setWidgetState((prevState) => {
|
|
44
|
+
const newState = typeof state === "function" ? state(prevState) : state;
|
|
45
|
+
const stateToSet = preserveWidgetContext(newState);
|
|
46
|
+
if (stateToSet !== null) {
|
|
47
|
+
window.openai.setWidgetState(stateToSet);
|
|
48
|
+
}
|
|
49
|
+
return filterWidgetContext(stateToSet);
|
|
50
|
+
});
|
|
51
|
+
}, []);
|
|
52
|
+
return [widgetState, setWidgetState];
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=use-widget-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-widget-state.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-widget-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAuB,MAAM,OAAO,CAAC;AAE9E,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,MAAM,kBAAkB,GAAG,kBAA2B,CAAC;AAEvD,SAAS,mBAAmB,CAC1B,KAAe;IAEf,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACnB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,EAAE,CAAC,kBAAkB,CAAC,EAAE,CAAC,EAAE,GAAG,aAAa,EAAE,GAAG,KAErD,CAAC;IAEF,OAAO,aAAkB,CAAC;AAC5B,CAAC;AAED,SAAS,qBAAqB,CAC5B,QAAkB;IAElB,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,kBAAkB,GAAG,MAAM,CAAC,MAAM,EAAE,WAG7B,CAAC;IAEd,IACE,kBAAkB,KAAK,IAAI;QAC3B,kBAAkB,KAAK,SAAS;QAChC,kBAAkB,IAAI,kBAAkB,EACxC,CAAC;QACD,OAAO;YACL,GAAG,QAAQ;YACX,CAAC,kBAAkB,CAAC,EAAE,kBAAkB,CAAC,kBAAkB,CAAC;SACxD,CAAC;IACT,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC;AAQD,MAAM,UAAU,cAAc,CAC5B,YAA0C;IAE1C,MAAM,qBAAqB,GAAG,eAAe,CAAC,aAAa,CAAM,CAAC;IAElE,MAAM,CAAC,WAAW,EAAE,eAAe,CAAC,GAAG,QAAQ,CAAW,GAAG,EAAE;QAC7D,IAAI,qBAAqB,KAAK,IAAI,EAAE,CAAC;YACnC,OAAO,mBAAmB,CAAC,qBAAqB,CAAC,CAAC;QACpD,CAAC;QAED,OAAO,OAAO,YAAY,KAAK,UAAU;YACvC,CAAC,CAAC,YAAY,EAAE;YAChB,CAAC,CAAC,YAAY,IAAI,IAAI,CAAC;IAC3B,CAAC,CAAC,CAAC;IAEH,SAAS,CAAC,GAAG,EAAE;QACb,kCAAkC;QAClC,IAAI,qBAAqB,KAAK,IAAI,EAAE,CAAC;YACnC,eAAe,CAAC,mBAAmB,CAAC,qBAAqB,CAAC,CAAC,CAAC;QAC9D,CAAC;IACH,CAAC,EAAE,CAAC,qBAAqB,CAAC,CAAC,CAAC;IAE5B,MAAM,cAAc,GAAG,WAAW,CAAC,CAAC,KAA+B,EAAE,EAAE;QACrE,eAAe,CAAC,CAAC,SAAS,EAAE,EAAE;YAC5B,MAAM,QAAQ,GAAG,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;YACxE,MAAM,UAAU,GAAG,qBAAqB,CAAC,QAAQ,CAAC,CAAC;YAEnD,IAAI,UAAU,KAAK,IAAI,EAAE,CAAC;gBACxB,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;YAC3C,CAAC;YAED,OAAO,mBAAmB,CAAC,UAAU,CAAC,CAAC;QACzC,CAAC,CAAC,CAAC;IACL,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,CAAC,WAAW,EAAE,cAAc,CAAU,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { useWidgetState } from "./use-widget-state.js";
|
|
2
|
+
import { describe, it, expect, vi, beforeEach, afterEach, } from "vitest";
|
|
3
|
+
import { renderHook, act } from "@testing-library/react";
|
|
4
|
+
describe("useWidgetState", () => {
|
|
5
|
+
let OpenaiMock;
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
OpenaiMock = {
|
|
8
|
+
widgetState: null,
|
|
9
|
+
setWidgetState: vi.fn().mockResolvedValue(undefined),
|
|
10
|
+
};
|
|
11
|
+
vi.stubGlobal("openai", OpenaiMock);
|
|
12
|
+
});
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
vi.unstubAllGlobals();
|
|
15
|
+
vi.resetAllMocks();
|
|
16
|
+
});
|
|
17
|
+
const defaultState = { count: 0, name: "test" };
|
|
18
|
+
const windowState = { count: 5, name: "window" };
|
|
19
|
+
it("should initialize with default state when window.openai.widgetState is null", () => {
|
|
20
|
+
OpenaiMock.widgetState = null;
|
|
21
|
+
const { result } = renderHook(() => useWidgetState(defaultState));
|
|
22
|
+
expect(result.current[0]).toEqual(defaultState);
|
|
23
|
+
});
|
|
24
|
+
it("should initialize with window.openai.widgetState when available", () => {
|
|
25
|
+
OpenaiMock.widgetState = windowState;
|
|
26
|
+
const { result } = renderHook(() => useWidgetState(defaultState));
|
|
27
|
+
expect(result.current[0]).toEqual(windowState);
|
|
28
|
+
});
|
|
29
|
+
it("should call window.openai.setWidgetState when setWidgetState is called with a new state", async () => {
|
|
30
|
+
const { result } = renderHook(() => useWidgetState(defaultState));
|
|
31
|
+
const newState = { count: 10, name: "updated" };
|
|
32
|
+
act(() => {
|
|
33
|
+
result.current[1](newState);
|
|
34
|
+
});
|
|
35
|
+
expect(OpenaiMock.setWidgetState).toHaveBeenCalledWith(newState);
|
|
36
|
+
expect(result.current[0]).toEqual(newState);
|
|
37
|
+
});
|
|
38
|
+
it("should call window.openai.setWidgetState when setWidgetState is called with a function updater", async () => {
|
|
39
|
+
const { result } = renderHook(() => useWidgetState(defaultState));
|
|
40
|
+
act(() => {
|
|
41
|
+
result.current[1]((prev) => ({ ...prev, count: prev.count + 1 }));
|
|
42
|
+
});
|
|
43
|
+
expect(OpenaiMock.setWidgetState).toHaveBeenCalledWith({
|
|
44
|
+
count: 1,
|
|
45
|
+
name: "test",
|
|
46
|
+
});
|
|
47
|
+
expect(result.current[0]).toEqual({ count: 1, name: "test" });
|
|
48
|
+
});
|
|
49
|
+
it("should update state when window.openai.widgetState changes", () => {
|
|
50
|
+
OpenaiMock.widgetState = defaultState;
|
|
51
|
+
const { result, rerender } = renderHook(() => useWidgetState(defaultState));
|
|
52
|
+
expect(result.current[0]).toEqual(defaultState);
|
|
53
|
+
// Simulate window.openai.widgetState changing
|
|
54
|
+
OpenaiMock.widgetState = windowState;
|
|
55
|
+
// Trigger re-render to simulate the useEffect running
|
|
56
|
+
rerender();
|
|
57
|
+
expect(result.current[0]).toEqual(windowState);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
//# sourceMappingURL=use-widget-state.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-widget-state.test.js","sourceRoot":"","sources":["../../../../src/web/hooks/use-widget-state.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EACL,QAAQ,EACR,EAAE,EACF,MAAM,EACN,EAAE,EACF,UAAU,EACV,SAAS,GAEV,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,wBAAwB,CAAC;AAEzD,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE;IAC9B,IAAI,UAA0D,CAAC;IAE/D,UAAU,CAAC,GAAG,EAAE;QACd,UAAU,GAAG;YACX,WAAW,EAAE,IAAI;YACjB,cAAc,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,iBAAiB,CAAC,SAAS,CAAC;SACrD,CAAC;QACF,EAAE,CAAC,UAAU,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;IAEH,SAAS,CAAC,GAAG,EAAE;QACb,EAAE,CAAC,gBAAgB,EAAE,CAAC;QACtB,EAAE,CAAC,aAAa,EAAE,CAAC;IACrB,CAAC,CAAC,CAAC;IAEH,MAAM,YA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AAC,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IACjD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/dist/src/web/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { useOpenAiGlobal } from "./use-openai-global.js";
|
|
2
|
-
export { useToolOutput } from "./use-tool-output.js";
|
|
3
1
|
export * from "./types.js";
|
|
2
|
+
export * from "./llm-describe.js";
|
|
4
3
|
export { mountWidget } from "./mount-widget.js";
|
|
4
|
+
export { skybridge } from "./plugin/plugin.js";
|
|
5
|
+
export * from "./hooks/index.js";
|
package/dist/src/web/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export { useOpenAiGlobal } from "./use-openai-global.js";
|
|
2
|
-
export { useToolOutput } from "./use-tool-output.js";
|
|
3
1
|
export * from "./types.js";
|
|
2
|
+
export * from "./llm-describe.js";
|
|
4
3
|
export { mountWidget } from "./mount-widget.js";
|
|
4
|
+
export { skybridge } from "./plugin/plugin.js";
|
|
5
|
+
export * from "./hooks/index.js";
|
|
5
6
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/web/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/web/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,cAAc,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export type LLMDescribeContent = string;
|
|
3
|
+
export interface LLMDescribeNode {
|
|
4
|
+
id: string;
|
|
5
|
+
parentId: string | null;
|
|
6
|
+
content: string | null;
|
|
7
|
+
}
|
|
8
|
+
interface LLMDescribeProps {
|
|
9
|
+
content: LLMDescribeContent | null | undefined;
|
|
10
|
+
children?: ReactNode;
|
|
11
|
+
}
|
|
12
|
+
export declare function LLMDescribe({ content, children }: LLMDescribeProps): import("react/jsx-runtime").JSX.Element;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useEffect, useId, } from "react";
|
|
3
|
+
const nodes = new Map();
|
|
4
|
+
function setNode(node) {
|
|
5
|
+
nodes.set(node.id, node);
|
|
6
|
+
onChange();
|
|
7
|
+
}
|
|
8
|
+
function removeNode(id) {
|
|
9
|
+
nodes.delete(id);
|
|
10
|
+
onChange();
|
|
11
|
+
}
|
|
12
|
+
function onChange() {
|
|
13
|
+
const description = getLLMDescriptionString();
|
|
14
|
+
window.openai.setWidgetState({
|
|
15
|
+
...window.openai.widgetState,
|
|
16
|
+
__widget_context: description,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
const ParentIdContext = createContext(null);
|
|
20
|
+
export function LLMDescribe({ content, children }) {
|
|
21
|
+
const parentId = useContext(ParentIdContext);
|
|
22
|
+
const id = useId();
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
setNode({
|
|
25
|
+
id,
|
|
26
|
+
parentId,
|
|
27
|
+
content: content ?? null,
|
|
28
|
+
});
|
|
29
|
+
return () => {
|
|
30
|
+
removeNode(id);
|
|
31
|
+
};
|
|
32
|
+
}, [id, parentId, content]);
|
|
33
|
+
return (_jsx(ParentIdContext.Provider, { value: id, children: children }));
|
|
34
|
+
}
|
|
35
|
+
function getLLMDescriptionString() {
|
|
36
|
+
const byParent = new Map();
|
|
37
|
+
for (const node of Array.from(nodes.values())) {
|
|
38
|
+
const key = node.parentId ?? null;
|
|
39
|
+
if (!byParent.has(key))
|
|
40
|
+
byParent.set(key, []);
|
|
41
|
+
byParent.get(key).push(node);
|
|
42
|
+
}
|
|
43
|
+
for (const list of byParent.values()) {
|
|
44
|
+
list.sort((a, b) => a.id.localeCompare(b.id));
|
|
45
|
+
}
|
|
46
|
+
const lines = [];
|
|
47
|
+
function traverseTree(parentId, depth) {
|
|
48
|
+
const children = byParent.get(parentId);
|
|
49
|
+
if (!children)
|
|
50
|
+
return;
|
|
51
|
+
for (const child of children) {
|
|
52
|
+
if (child.content && child.content.trim()) {
|
|
53
|
+
const indent = " ".repeat(depth);
|
|
54
|
+
lines.push(`${indent}- ${child.content.trim()}`);
|
|
55
|
+
}
|
|
56
|
+
traverseTree(child.id, depth + 1);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
traverseTree(null, 0);
|
|
60
|
+
return lines.join("\n");
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=llm-describe.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm-describe.js","sourceRoot":"","sources":["../../../src/web/llm-describe.tsx"],"names":[],"mappings":";AAAA,OAAO,EACL,aAAa,EACb,UAAU,EACV,SAAS,EACT,KAAK,GAEN,MAAM,OAAO,CAAC;AAUf,MAAM,KAAK,GAAG,IAAI,GAAG,EAA2B,CAAC;AAEjD,SAAS,OAAO,CAAC,IAAqB;IACpC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,CAAC,CAAC;IACzB,QAAQ,EAAE,CAAC;AACb,CAAC;AAED,SAAS,UAAU,CAAC,EAAU;IAC5B,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IACjB,QAAQ,EAAE,CAAC;AACb,CAAC;AAED,SAAS,QAAQ;IACf,MAAM,WAAW,GAAG,uBAAuB,EAAE,CAAC;IAC9C,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC;QAC3B,GAAG,MAAM,CAAC,MAAM,CAAC,WAAW;QAC5B,gBAAgB,EAAE,WAAW;KAC9B,CAAC,CAAC;AACL,CAAC;AAED,MAAM,eAA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C,CAAC;IAChD,CAAC;IAED,MAAM,KAAK,GAAa,EAAE,CAAC;IAE3B,SAAS,YAAY,CAAC,QAAuB,EAAE,KAAa;QAC1D,MAAM,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACxC,IAAI,CAAC,QAAQ;YAAE,OAAO;QAEtB,KAAK,MAAM,KAAK,IAAI,QAAQ,EAAE,CAAC;YAC7B,IAAI,KAAK,CAAC,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;gBAC1C,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAClC,KAAK,CAAC,IAAI,CAAC,GAAG,MAAM,KAAK,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YACnD,CAAC;YACD,YAAY,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;QACpC,CAAC;IACH,CAAC;IAED,YAAY,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;IAEtB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { render } from "@testing-library/react";
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi, } from "vitest";
|
|
4
|
+
import { LLMDescribe } from "./llm-describe.js";
|
|
5
|
+
describe("LLMDescribe", () => {
|
|
6
|
+
let OpenaiMock;
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
OpenaiMock = {
|
|
9
|
+
widgetState: {},
|
|
10
|
+
setWidgetState: vi.fn(),
|
|
11
|
+
};
|
|
12
|
+
// Use Object.defineProperty to ensure it persists
|
|
13
|
+
Object.defineProperty(globalThis, "openai", {
|
|
14
|
+
value: OpenaiMock,
|
|
15
|
+
writable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
});
|
|
18
|
+
// Also set on window for browser-like environment
|
|
19
|
+
if (typeof window !== "undefined") {
|
|
20
|
+
Object.defineProperty(window, "openai", {
|
|
21
|
+
value: OpenaiMock,
|
|
22
|
+
writable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
vi.stubGlobal("openai", OpenaiMock);
|
|
27
|
+
});
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
vi.resetAllMocks();
|
|
30
|
+
// Keep the mock available for React cleanup, but reset it
|
|
31
|
+
if (typeof window !== "undefined" && window.openai) {
|
|
32
|
+
window.openai.setWidgetState = vi.fn();
|
|
33
|
+
window.openai.widgetState = {};
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
it("should register a node with content and call setWidgetState", () => {
|
|
37
|
+
render(_jsx(LLMDescribe, { content: "Test content", children: _jsx("div", { children: "Child" }) }));
|
|
38
|
+
expect(OpenaiMock.setWidgetState).toHaveBeenCalled();
|
|
39
|
+
const callArgs = OpenaiMock.setWidgetState.mock.calls[0]?.[0];
|
|
40
|
+
expect(callArgs).toHaveProperty("__widget_context");
|
|
41
|
+
expect(callArgs?.__widget_context).toContain("- Test content");
|
|
42
|
+
});
|
|
43
|
+
it("should preserve existing widgetState when updating context", () => {
|
|
44
|
+
OpenaiMock.widgetState = { existingKey: "existingValue" };
|
|
45
|
+
render(_jsx(LLMDescribe, { content: "Test content", children: _jsx("div", { children: "Child" }) }));
|
|
46
|
+
const callArgs = OpenaiMock.setWidgetState.mock.calls[0]?.[0];
|
|
47
|
+
expect(callArgs).toHaveProperty("existingKey", "existingValue");
|
|
48
|
+
expect(callArgs).toHaveProperty("__widget_context");
|
|
49
|
+
});
|
|
50
|
+
it("should handle deeply nested LLMDescribe components", () => {
|
|
51
|
+
render(_jsxs(LLMDescribe, { content: "Level 1", children: [_jsx(LLMDescribe, { content: "Level 2A" }), _jsx(LLMDescribe, { content: "Level 2B", children: _jsx(LLMDescribe, { content: "Level 3", children: _jsx("div", { children: "Content" }) }) })] }));
|
|
52
|
+
const callArgs = OpenaiMock.setWidgetState.mock.calls[OpenaiMock.setWidgetState.mock.calls.length - 1]?.[0];
|
|
53
|
+
const context = callArgs?.__widget_context;
|
|
54
|
+
expect(context).toContain("- Level 1");
|
|
55
|
+
expect(context).toContain(" - Level 2A");
|
|
56
|
+
expect(context).toContain(" - Level 2B");
|
|
57
|
+
expect(context).toContain(" - Level 3");
|
|
58
|
+
});
|
|
59
|
+
it("should update context when content changes", () => {
|
|
60
|
+
const { rerender } = render(_jsx(LLMDescribe, { content: "Initial content", children: _jsx("div", { children: "Child" }) }));
|
|
61
|
+
const initialCalls = OpenaiMock.setWidgetState.mock.calls.length;
|
|
62
|
+
rerender(_jsx(LLMDescribe, { content: "Updated content", children: _jsx("div", { children: "Child" }) }));
|
|
63
|
+
expect(OpenaiMock.setWidgetState.mock.calls.length).toBeGreaterThan(initialCalls);
|
|
64
|
+
const lastCallArgs = OpenaiMock.setWidgetState.mock.calls[OpenaiMock.setWidgetState.mock.calls.length - 1]?.[0];
|
|
65
|
+
expect(lastCallArgs?.__widget_context).toContain("- Updated content");
|
|
66
|
+
});
|
|
67
|
+
it("should remove node and update context when component unmounts", () => {
|
|
68
|
+
const { unmount } = render(_jsx(LLMDescribe, { content: "Content to remove", children: _jsx("div", { children: "Child" }) }));
|
|
69
|
+
const callsBeforeUnmount = OpenaiMock.setWidgetState.mock.calls.length;
|
|
70
|
+
unmount();
|
|
71
|
+
expect(OpenaiMock.setWidgetState.mock.calls.length).toBeGreaterThan(callsBeforeUnmount);
|
|
72
|
+
const lastCallArgs = OpenaiMock.setWidgetState.mock.calls[OpenaiMock.setWidgetState.mock.calls.length - 1]?.[0];
|
|
73
|
+
expect(lastCallArgs?.__widget_context).not.toContain("Content to remove");
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
//# sourceMappingURL=llm-describe.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"llm-describe.test.js","sourceRoot":"","sources":["../../../src/web/llm-describe.test.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,EACL,SAAS,EACT,UAAU,EACV,QAAQ,EACR,MAAM,EACN,EAAE,EACF,EAAE,GAEH,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IAC3B,IAAI,UAA0D,CAAC;IAE/D,UAAU,CAAC,GAAG,EAAE;QACd,UAAU,GAAG;YACX,WAAW,EAAE,EAAE;YACf,cAAc,EAAE,EAAE,CAAC,EAAE,EAAE;SACxB,CAAC;QACF,kDAAkD;QAClD,MAAM,CAAC,cAAc,CAAC,UAAU,EAAE,QAAQ,EAAE;YAC1C,KAAK,EAAE,UAAU;YACjB,QAAQ,EAAE,IAAI;YACd,YAAY,EAAE,IAAI;SACnB,CAAC,CAAC;QACH,kDAAkD;QAClD,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;YAClC,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE;gBACtC,KAAK,EAAE,UAAU;gBACjB,QAAQ,EAAE,IAAI;gBACd,YAAY,EAAE,IAAI;aACnB,CAAC,CAAC;QACL,CAAC;QACD,EAAE,CAAC,UAAU,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;IAEH,SAAS,CAAC,GAAG,EAAE;QACb,EAAE,CAAC,aAAa,EAAE,CAAC;QACnB,0DAA0D;QAC1D,IAAI,OAAO,MAAM,KAAK,WAAW,IAAK,MAAc,CAAC,MAAM,EAAE,CAAC;YAC3D,MAAc,CAAC,MAAM,CAAC,cAAc,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;YAC/C,MAAc,CA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C,CAAC;QACT,MAAM,CAAC,YAAY,EAAE,gBAAgB,CAAC,CAAC,SAAS,CAAC,mBAAmB,CAAC,CAAC;IACxE,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,+DAA+D,EAAE,GAAG,EAAE;QACvE,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CACxB,KAAC,WAAW,IAAC,OAAO,EAAC,mBAAmB,YACtC,kCAAgB,GACJ,CACf,CAAC;QAEF,MAAM,kBAAkB,GAAG,UAAU,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;QAEvE,OAAO,EAAE,CAAC;QAEV,MAAM,CAAC,UAAU,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,eAAe,CACjE,kBAAkB,CACnB,CAAC;QACF,MAAM,YAAY,GAChB,UAAU,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,CAClC,UAAU,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAChD,EAAE,CAAC,CAAC,CAAC,CAAC;QACT,MAAM,CAAC,YAAY,EAAE,gBAAgB,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,mBAAmB,CAAC,CAAC;IAC5E,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { createElement, StrictMode } from "react";
|
|
2
2
|
import { createRoot } from "react-dom/client";
|
|
3
|
+
let rootInstance = null;
|
|
3
4
|
export const mountWidget = (component) => {
|
|
4
|
-
const
|
|
5
|
-
if (!
|
|
5
|
+
const rootElement = document.getElementById("root");
|
|
6
|
+
if (!rootElement) {
|
|
6
7
|
throw new Error("Root element not found");
|
|
7
8
|
}
|
|
8
|
-
|
|
9
|
+
if (!rootInstance) {
|
|
10
|
+
rootInstance = createRoot(rootElement);
|
|
11
|
+
}
|
|
12
|
+
rootInstance.render(createElement(StrictMode, null, component));
|
|
9
13
|
};
|
|
10
14
|
//# sourceMappingURL=mount-widget.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mount-widget.js","sourceRoot":"","sources":["../../../src/web/mount-widget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAClD,OAAO,EAAE,UAAU,
|
|
1
|
+
{"version":3,"file":"mount-widget.js","sourceRoot":"","sources":["../../../src/web/mount-widget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAClD,OAAO,EAAE,UAAU,EAAa,MAAM,kBAAkB,CAAC;AAEzD,IAAI,YAAY,GAAgB,IAAI,CAAC;AAErC,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,SAA0B,EAAE,EAAE;IACxD,MAAM,WAAW,GAAG,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IACpD,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;IAC5C,CAAC;IAED,IAAI,CAAC,YAAY,EAAE,CAAC;QAClB,YAAY,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;IACzC,CAAC;IAED,YAAY,CAAC,MAAM,CAAC,aAAa,CAAC,UAAU,EAAE,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC;AAClE,CAAC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const transform: (code: string, id: string) => Promise<{
|
|
2
|
+
code: string;
|
|
3
|
+
map: {
|
|
4
|
+
version: number;
|
|
5
|
+
sources: string[];
|
|
6
|
+
names: string[];
|
|
7
|
+
sourceRoot?: string | undefined;
|
|
8
|
+
sourcesContent?: string[] | undefined;
|
|
9
|
+
mappings: string;
|
|
10
|
+
file: string;
|
|
11
|
+
} | null;
|
|
12
|
+
} | null>;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
const LLM_IMPORT_SOURCE = "skybridge/web";
|
|
2
|
+
function createBabelPlugin(t) {
|
|
3
|
+
return {
|
|
4
|
+
name: "data-llm-babel",
|
|
5
|
+
visitor: {
|
|
6
|
+
Program: {
|
|
7
|
+
enter(path, state) {
|
|
8
|
+
state.hasLLMDescribeImport = false;
|
|
9
|
+
state.needsLLMDescribeImport = false;
|
|
10
|
+
for (const node of path.node.body) {
|
|
11
|
+
if (!t.isImportDeclaration(node))
|
|
12
|
+
continue;
|
|
13
|
+
if (node.source.value !== LLM_IMPORT_SOURCE)
|
|
14
|
+
continue;
|
|
15
|
+
const hasSpecifier = node.specifiers.some((s) => t.isImportSpecifier(s) &&
|
|
16
|
+
t.isIdentifier(s.imported, { name: "LLMDescribe" }));
|
|
17
|
+
if (hasSpecifier) {
|
|
18
|
+
state.hasLLMDescribeImport = true;
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
exit(path, state) {
|
|
24
|
+
if (state.needsLLMDescribeImport && !state.hasLLMDescribeImport) {
|
|
25
|
+
const importDecl = t.importDeclaration([
|
|
26
|
+
t.importSpecifier(t.identifier("LLMDescribe"), t.identifier("LLMDescribe")),
|
|
27
|
+
], t.stringLiteral(LLM_IMPORT_SOURCE));
|
|
28
|
+
path.node.body.unshift(importDecl);
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
JSXElement(path, state) {
|
|
33
|
+
const opening = path.node.openingElement;
|
|
34
|
+
const attrs = opening.attributes;
|
|
35
|
+
const llmAttrIndex = attrs.findIndex((attr) => t.isJSXAttribute(attr) &&
|
|
36
|
+
t.isJSXIdentifier(attr.name, { name: "data-llm" }));
|
|
37
|
+
if (llmAttrIndex === -1)
|
|
38
|
+
return;
|
|
39
|
+
const llmAttr = attrs[llmAttrIndex];
|
|
40
|
+
const newAttrs = [...attrs];
|
|
41
|
+
newAttrs.splice(llmAttrIndex, 1);
|
|
42
|
+
opening.attributes = newAttrs;
|
|
43
|
+
let contentExpression;
|
|
44
|
+
if (!llmAttr.value) {
|
|
45
|
+
contentExpression = t.stringLiteral("");
|
|
46
|
+
}
|
|
47
|
+
else if (t.isStringLiteral(llmAttr.value)) {
|
|
48
|
+
contentExpression = llmAttr.value;
|
|
49
|
+
}
|
|
50
|
+
else if (t.isJSXExpressionContainer(llmAttr.value)) {
|
|
51
|
+
contentExpression = llmAttr.value.expression;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const contentAttr = t.jsxAttribute(t.jsxIdentifier("content"), t.isStringLiteral(contentExpression)
|
|
57
|
+
? contentExpression
|
|
58
|
+
: t.jsxExpressionContainer(contentExpression));
|
|
59
|
+
const llmOpening = t.jsxOpeningElement(t.jsxIdentifier("LLMDescribe"), [
|
|
60
|
+
contentAttr,
|
|
61
|
+
]);
|
|
62
|
+
const llmClosing = t.jsxClosingElement(t.jsxIdentifier("LLMDescribe"));
|
|
63
|
+
const wrapped = t.jsxElement(llmOpening, llmClosing, [path.node], false);
|
|
64
|
+
state.needsLLMDescribeImport = true;
|
|
65
|
+
path.replaceWith(wrapped);
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
export const transform = async (code, id) => {
|
|
71
|
+
if (!/\.(jsx|tsx)$/.test(id)) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
if (id.includes("node_modules")) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
// Dynamic import to ensure @babel/core is only loaded in Node.js context
|
|
78
|
+
const { types: t, transformSync } = await import("@babel/core");
|
|
79
|
+
const babelOptions = {
|
|
80
|
+
plugins: [createBabelPlugin(t)],
|
|
81
|
+
parserOpts: {
|
|
82
|
+
plugins: ["jsx", "typescript"],
|
|
83
|
+
},
|
|
84
|
+
filename: id,
|
|
85
|
+
sourceFileName: id,
|
|
86
|
+
};
|
|
87
|
+
const result = transformSync(code, babelOptions);
|
|
88
|
+
if (!result || !result.code) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
code: result.code,
|
|
93
|
+
map: result.map || null,
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
//# sourceMappingURL=data-llm.js.map
|