skybridge 2.0.3 → 2.1.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/README.md +9 -9
- package/dist/cli/run-plain.js +2 -1
- package/dist/cli/run-plain.js.map +1 -1
- package/dist/cli/use-nodemon.d.ts +11 -1
- package/dist/cli/use-nodemon.js +19 -5
- package/dist/cli/use-nodemon.js.map +1 -1
- package/dist/commands/dev.js +3 -3
- package/dist/commands/dev.js.map +1 -1
- package/dist/server/app.d.ts +22 -5
- package/dist/server/app.js +24 -13
- package/dist/server/app.js.map +1 -1
- package/dist/server/app.test.js +81 -0
- package/dist/server/app.test.js.map +1 -1
- package/dist/server/auth/providers/custom.d.ts +4 -2
- package/dist/server/auth/providers/custom.js.map +1 -1
- package/dist/server/auth/providers/workos.d.ts +3 -2
- package/dist/server/auth/providers/workos.js +2 -1
- package/dist/server/auth/providers/workos.js.map +1 -1
- package/dist/server/auth/verify.d.ts +5 -3
- package/dist/server/auth/verify.js.map +1 -1
- package/dist/server/auth/verify.test.js +16 -0
- package/dist/server/auth/verify.test.js.map +1 -1
- package/dist/server/events.d.ts +113 -0
- package/dist/server/events.js +318 -0
- package/dist/server/events.js.map +1 -0
- package/dist/server/events.test.d.ts +1 -0
- package/dist/server/events.test.js +172 -0
- package/dist/server/events.test.js.map +1 -0
- package/dist/server/express.d.ts +3 -2
- package/dist/server/express.js +17 -11
- package/dist/server/express.js.map +1 -1
- package/dist/server/express.test.js +45 -0
- package/dist/server/express.test.js.map +1 -1
- package/dist/server/form-input.d.ts +120 -0
- package/dist/server/form-input.js +89 -0
- package/dist/server/form-input.js.map +1 -0
- package/dist/server/index.d.ts +4 -2
- package/dist/server/index.js +3 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/mcp-errors.test.js +188 -0
- package/dist/server/mcp-errors.test.js.map +1 -1
- package/dist/server/metric.d.ts +5 -3
- package/dist/server/metric.js +5 -5
- package/dist/server/metric.js.map +1 -1
- package/dist/server/register-tool.test.js +203 -0
- package/dist/server/register-tool.test.js.map +1 -1
- package/dist/server/server.d.ts +254 -10
- package/dist/server/server.js +262 -9
- package/dist/server/server.js.map +1 -1
- package/dist/server/tool-extra.test.js +79 -0
- package/dist/server/tool-extra.test.js.map +1 -1
- package/dist/web/bridges/adaptor.d.ts +14 -3
- package/dist/web/bridges/adaptor.js +164 -14
- package/dist/web/bridges/adaptor.js.map +1 -1
- package/dist/web/bridges/adaptor.test.js +62 -6
- package/dist/web/bridges/adaptor.test.js.map +1 -1
- package/dist/web/bridges/apps-sdk/bridge.js +1 -1
- package/dist/web/bridges/apps-sdk/bridge.js.map +1 -1
- package/dist/web/bridges/apps-sdk/types.d.ts +2 -3
- package/dist/web/bridges/apps-sdk/types.js.map +1 -1
- package/dist/web/bridges/apps-sdk/use-apps-sdk-context.d.ts +3 -3
- package/dist/web/bridges/apps-sdk/use-apps-sdk-context.js +3 -3
- package/dist/web/bridges/apps-sdk/use-apps-sdk-context.js.map +1 -1
- package/dist/web/bridges/mcp-app/bridge.d.ts +1 -1
- package/dist/web/bridges/mcp-app/bridge.js +9 -2
- package/dist/web/bridges/mcp-app/bridge.js.map +1 -1
- package/dist/web/bridges/mcp-app/types.d.ts +18 -3
- package/dist/web/bridges/mcp-app/types.js.map +1 -1
- package/dist/web/bridges/mcp-app/use-mcp-app-context.d.ts +2 -2
- package/dist/web/bridges/mcp-app/use-mcp-app-context.js +1 -1
- package/dist/web/bridges/mcp-app/use-mcp-app-context.js.map +1 -1
- package/dist/web/bridges/types.d.ts +57 -4
- package/dist/web/bridges/types.js +1 -1
- package/dist/web/bridges/types.js.map +1 -1
- package/dist/web/data-llm.js +3 -2
- package/dist/web/data-llm.js.map +1 -1
- package/dist/web/data-llm.test.js +6 -0
- package/dist/web/data-llm.test.js.map +1 -1
- package/dist/web/generate-helpers.d.ts +2 -2
- package/dist/web/generate-helpers.js +2 -2
- package/dist/web/generate-helpers.js.map +1 -1
- package/dist/web/hooks/index.d.ts +5 -1
- package/dist/web/hooks/index.js +5 -1
- package/dist/web/hooks/index.js.map +1 -1
- package/dist/web/hooks/openai-extensions.test.d.ts +1 -0
- package/dist/web/hooks/openai-extensions.test.js +107 -0
- package/dist/web/hooks/openai-extensions.test.js.map +1 -0
- package/dist/web/hooks/use-call-tool.d.ts +2 -2
- package/dist/web/hooks/use-call-tool.js +15 -10
- package/dist/web/hooks/use-call-tool.js.map +1 -1
- package/dist/web/hooks/use-call-tool.test.js +13 -2
- package/dist/web/hooks/use-call-tool.test.js.map +1 -1
- package/dist/web/hooks/use-deep-link.d.ts +22 -0
- package/dist/web/hooks/use-deep-link.js +34 -0
- package/dist/web/hooks/use-deep-link.js.map +1 -0
- package/dist/web/hooks/use-file-resource.d.ts +37 -0
- package/dist/web/hooks/use-file-resource.js +98 -0
- package/dist/web/hooks/use-file-resource.js.map +1 -0
- package/dist/web/hooks/use-file-resource.test.d.ts +1 -0
- package/dist/web/hooks/use-file-resource.test.js +65 -0
- package/dist/web/hooks/use-file-resource.test.js.map +1 -0
- package/dist/web/hooks/use-host.d.ts +27 -5
- package/dist/web/hooks/use-host.js +20 -5
- package/dist/web/hooks/use-host.js.map +1 -1
- package/dist/web/hooks/use-model-context.d.ts +44 -0
- package/dist/web/hooks/use-model-context.js +63 -0
- package/dist/web/hooks/use-model-context.js.map +1 -0
- package/dist/web/hooks/use-open-file.d.ts +20 -0
- package/dist/web/hooks/use-open-file.js +25 -0
- package/dist/web/hooks/use-open-file.js.map +1 -0
- package/dist/web/hooks/use-register-view-tool.d.ts +1 -1
- package/dist/web/hooks/use-register-view-tool.js +1 -1
- package/dist/web/hooks/use-register-view-tool.js.map +1 -1
- package/dist/web/hooks/use-send-follow-up-message.d.ts +8 -2
- package/dist/web/hooks/use-send-follow-up-message.js +6 -0
- package/dist/web/hooks/use-send-follow-up-message.js.map +1 -1
- package/package.json +6 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-call-tool.test.js","sourceRoot":"","sources":["../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M,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC,GAAG,EAAE,CACjC,WAAW,CAAqB,WAAW,CAAC,CAC7C,CAAC;QAEF,MAAM,YAAY,GAAiB;YACjC,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;YAClD,iBAAiB,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE;YACxC,OAAO,EAAE,KAAK;YACd,IAAI,EAAE,EAAE;SACT,CAAC;QAEF,YAAY,CAAC,qBAAqB,CAAC,YAAY,CAAC,CAAC;QAEjD,MAAM,aAAa,GAAG,MAAM,GAAG,CAAC,KAAK,IAAI,EAAE;YACzC,OAAO,MAAM,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC;QACxC,CAAC,CAAC,CAAC;QACH,YAAY,CAAsB,aAAa,CAAC,CAAC;QACjD,MAAM,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IAC9C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { act, renderHook, waitFor } from \"@testing-library/react\";\nimport {\n afterEach,\n beforeEach,\n describe,\n expect,\n expectTypeOf,\n it,\n type Mock,\n vi,\n} from \"vitest\";\nimport { HostAdaptor } from \"../bridges/adaptor.js\";\nimport { AppsSdkBridge } from \"../bridges/apps-sdk/bridge.js\";\nimport { getAdaptor } from \"../bridges/get-adaptor.js\";\nimport { McpAppBridge } from \"../bridges/mcp-app/bridge.js\";\nimport type { CallToolResponse } from \"../bridges/types.js\";\nimport { useCallTool } from \"./use-call-tool.js\";\n\ndescribe(\"useCallTool - onSuccess callback\", () => {\n let callToolMock: Mock;\n\n beforeEach(() => {\n HostAdaptor.resetInstance();\n McpAppBridge.resetInstance();\n AppsSdkBridge.resetInstance();\n vi.stubGlobal(\"parent\", { postMessage: vi.fn() });\n vi.stubGlobal(\"skybridge\", {});\n vi.stubGlobal(\"openai\", { callTool: vi.fn() });\n callToolMock = vi.spyOn(getAdaptor(), \"callTool\") as unknown as Mock;\n });\n\n afterEach(() => {\n vi.unstubAllGlobals();\n vi.resetAllMocks();\n HostAdaptor.resetInstance();\n McpAppBridge.resetInstance();\n AppsSdkBridge.resetInstance();\n });\n\n const toolName = \"test-tool\";\n const args = { input: \"test input\" };\n const data: CallToolResponse = {\n content: [{ type: \"text\" as const, text: \"test result\" }],\n structuredContent: { result: \"test\" },\n isError: false,\n meta: {},\n };\n const error = new Error(\"test error\");\n\n it(\"should normalize _meta to meta when SDK returns _meta instead of meta\", async () => {\n const rawSdkResponse = {\n content: [{ type: \"text\" as const, text: \"result\" }],\n structuredContent: { value: 1 },\n isError: false,\n meta: { secret: \"only visible to widget\" },\n };\n callToolMock.mockResolvedValueOnce(rawSdkResponse);\n const { result } = renderHook(() =>\n useCallTool<typeof args, typeof data>(toolName),\n );\n\n await act(async () => {\n result.current.callTool(args);\n });\n\n await waitFor(() => {\n expect(result.current.data).toMatchObject({\n meta: { secret: \"only visible to widget\" },\n });\n });\n });\n\n it(\"should call adaptor.callTool with correct arguments\", async () => {\n const { result } = renderHook(() =>\n useCallTool<typeof args, typeof data>(toolName),\n );\n await act(async () => {\n result.current.callTool(args);\n });\n expect(callToolMock).toHaveBeenCalledWith(toolName, args);\n });\n\n it(\"should call onSuccess callback with correct data and toolArgs on successful execution\", async () => {\n const onSuccess = vi.fn();\n const onError = vi.fn();\n callToolMock.mockResolvedValueOnce(data);\n const { result } = renderHook(() =>\n useCallTool<typeof args, typeof data>(toolName),\n );\n\n act(() => {\n result.current.callTool(args, {\n onSuccess,\n onError,\n });\n });\n\n await waitFor(() => {\n expect(onSuccess).toHaveBeenCalledWith(data, args);\n expect(onError).not.toHaveBeenCalled();\n });\n });\n\n it(\"should call onError callback with error and toolArgs on failed execution\", async () => {\n const onSuccess = vi.fn();\n const onError = vi.fn();\n callToolMock.mockRejectedValueOnce(error);\n const { result } = renderHook(() =>\n useCallTool<typeof args, typeof data>(toolName),\n );\n\n act(() => {\n result.current.callTool(args, {\n onSuccess,\n onError,\n });\n });\n\n await waitFor(() => {\n expect(onSuccess).not.toHaveBeenCalled();\n expect(onError).toHaveBeenCalledWith(error, args);\n });\n });\n\n it(\"should call onSettled callback with data and undefined error on successful execution\", async () => {\n const onSuccess = vi.fn();\n const onError = vi.fn();\n const onSettled = vi.fn();\n callToolMock.mockResolvedValueOnce(data);\n const { result } = renderHook(() =>\n useCallTool<typeof args, typeof data>(toolName),\n );\n\n act(() => {\n result.current.callTool(args, {\n onSuccess,\n onError,\n onSettled,\n });\n });\n\n await waitFor(() => {\n expect(onSuccess).toHaveBeenCalledWith(data, args);\n expect(onSettled).toHaveBeenCalledWith(data, undefined, args);\n expect(onError).not.toHaveBeenCalled();\n });\n });\n\n it(\"should call onSettled callback with undefined data and error on failed execution\", async () => {\n const onSuccess = vi.fn();\n const onError = vi.fn();\n const onSettled = vi.fn();\n callToolMock.mockRejectedValueOnce(error);\n const { result } = renderHook(() =>\n useCallTool<typeof args, typeof data>(toolName),\n );\n\n act(() => {\n result.current.callTool(args, {\n onSuccess,\n onError,\n onSettled,\n });\n });\n\n await waitFor(() => {\n expect(onError).toHaveBeenCalledWith(error, args);\n expect(onSettled).toHaveBeenCalledWith(undefined, error, args);\n expect(onSuccess).not.toHaveBeenCalled();\n });\n });\n\n it(\"should always return last call started state\", async () => {\n const { result } = renderHook(() =>\n useCallTool<typeof args, typeof data>(toolName),\n );\n\n const firstCallData = {\n ...data,\n structuredContent: { result: \"first call result\" },\n };\n const secondCallData = {\n ...data,\n structuredContent: { result: \"second call result\" },\n };\n const { promise: firstCallToolPromise, resolve: resolveFirstCallTool } =\n Promise.withResolvers();\n const { promise: secondCallToolPromise, resolve: resolveSecondCallTool } =\n Promise.withResolvers();\n callToolMock\n .mockImplementationOnce(() => firstCallToolPromise)\n .mockImplementationOnce(() => secondCallToolPromise);\n\n await act(() => {\n result.current.callTool(args);\n result.current.callTool(args);\n resolveFirstCallTool(firstCallData);\n return firstCallToolPromise;\n });\n\n expect(result.current.status).toEqual(\"pending\");\n expect(result.current.data).toEqual(undefined);\n resolveSecondCallTool(secondCallData);\n\n await waitFor(() => {\n expect(result.current.status).toEqual(\"success\");\n expect(result.current.data).toEqual(secondCallData);\n });\n });\n});\n\ndescribe(\"useCallTool - TypeScript typing\", () => {\n let callToolMock: Mock;\n\n beforeEach(() => {\n HostAdaptor.resetInstance();\n McpAppBridge.resetInstance();\n AppsSdkBridge.resetInstance();\n vi.stubGlobal(\"parent\", { postMessage: vi.fn() });\n vi.stubGlobal(\"skybridge\", {});\n vi.stubGlobal(\"openai\", { callTool: vi.fn() });\n callToolMock = vi.spyOn(getAdaptor(), \"callTool\") as unknown as Mock;\n });\n\n afterEach(() => {\n vi.unstubAllGlobals();\n vi.resetAllMocks();\n HostAdaptor.resetInstance();\n McpAppBridge.resetInstance();\n AppsSdkBridge.resetInstance();\n });\n\n it(\"should have correct return types when ToolArgs is null and ToolResponse is specified\", async () => {\n type TestResponse = CallToolResponse & {\n structuredContent: { result: string };\n meta: { id: number };\n };\n\n const { result } = renderHook(() =>\n useCallTool<null, TestResponse>(\"test-tool\"),\n );\n const data: TestResponse = {\n content: [{ type: \"text\" as const, text: \"test\" }],\n structuredContent: { result: \"test\" },\n isError: false,\n meta: { id: 123 },\n };\n\n callToolMock.mockResolvedValueOnce(data);\n\n await act(async () => {\n result.current.callTool();\n });\n\n expect(callToolMock).toHaveBeenCalledWith(\"test-tool\", null);\n expectTypeOf<typeof data | undefined>(result.current.data);\n });\n\n it(\"should correctly type callToolAsync return value\", async () => {\n type TestArgs = { query: string };\n type TestResponse = CallToolResponse & {\n structuredContent: { answer: string };\n };\n\n const { result } = renderHook(() =>\n useCallTool<TestArgs, TestResponse>(\"test-tool\"),\n );\n\n const testArgs: TestArgs = { query: \"test\" };\n const mockResponse: TestResponse = {\n content: [{ type: \"text\" as const, text: \"answer\" }],\n structuredContent: { answer: \"test answer\" },\n isError: false,\n meta: {},\n };\n\n callToolMock.mockResolvedValueOnce(mockResponse);\n\n const returnedValue = await act(async () => {\n return result.current.callToolAsync(testArgs);\n });\n expectTypeOf<typeof mockResponse>(returnedValue);\n expect(returnedValue).toEqual(mockResponse);\n });\n\n it(\"should correctly type callToolAsync when ToolArgs is null\", async () => {\n type TestResponse = CallToolResponse & {\n structuredContent: { data: string };\n };\n\n const { result } = renderHook(() =>\n useCallTool<null, TestResponse>(\"test-tool\"),\n );\n\n const mockResponse: TestResponse = {\n content: [{ type: \"text\" as const, text: \"data\" }],\n structuredContent: { data: \"test data\" },\n isError: false,\n meta: {},\n };\n\n callToolMock.mockResolvedValueOnce(mockResponse);\n\n const returnedValue = await act(async () => {\n return result.current.callToolAsync();\n });\n expectTypeOf<typeof mockResponse>(returnedValue);\n expect(returnedValue).toEqual(mockResponse);\n });\n});\n"]}
|
|
1
|
+
{"version":3,"file":"use-call-tool.test.js","sourceRoot":"","sources":["../../../src/web/hooks/use-call-tool.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAClE,OAAO,EACL,SAAS,EACT,UAAU,EACV,QAAQ,EACR,MAAM,EACN,YAAY,EACZ,EAAE,EAEF,EAAE,GACH,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAE5D,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,QAAQ,CAAC,kCAAkC,EAAE,GAAG,EAAE;IAChD,IAAI,YAAkB,CAAC;IAEvB,UAAU,CAAC,GAAG,EAAE;QACd,WAAW,CAAC,aAAa,EAAE,CAAC;QAC5B,YAAY,CAAC,aAAa,EAAE,CAAC;QAC7B,aAAa,CAAC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AC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;YAClD,iBAAiB,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE;YACxC,OAAO,EAAE,KAAK;YACd,IAAI,EAAE,EAAE;SACT,CAAC;QAEF,YAAY,CAAC,qBAAqB,CAAC,YAAY,CAAC,CAAC;QAEjD,MAAM,aAAa,GAAG,MAAM,GAAG,CAAC,KAAK,IAAI,EAAE;YACzC,OAAO,MAAM,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC;QACxC,CAAC,CAAC,CAAC;QACH,YAAY,CAAsB,aAAa,CAAC,CAAC;QACjD,MAAM,CAAC,aAAa,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IAC9C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { act, renderHook, waitFor } from \"@testing-library/react\";\nimport {\n afterEach,\n beforeEach,\n describe,\n expect,\n expectTypeOf,\n it,\n type Mock,\n vi,\n} from \"vitest\";\nimport { HostAdaptor } from \"../bridges/adaptor.js\";\nimport { AppsSdkBridge } from \"../bridges/apps-sdk/bridge.js\";\nimport { getAdaptor } from \"../bridges/get-adaptor.js\";\nimport { McpAppBridge } from \"../bridges/mcp-app/bridge.js\";\nimport type { CallToolResponse } from \"../bridges/types.js\";\nimport { useCallTool } from \"./use-call-tool.js\";\n\ndescribe(\"useCallTool - onSuccess callback\", () => {\n let callToolMock: Mock;\n\n beforeEach(() => {\n HostAdaptor.resetInstance();\n McpAppBridge.resetInstance();\n AppsSdkBridge.resetInstance();\n vi.stubGlobal(\"parent\", { postMessage: vi.fn() });\n vi.stubGlobal(\"skybridge\", {});\n vi.stubGlobal(\"openai\", { callTool: vi.fn() });\n callToolMock = vi.spyOn(getAdaptor(), \"callTool\") as unknown as Mock;\n });\n\n afterEach(() => {\n vi.unstubAllGlobals();\n vi.resetAllMocks();\n HostAdaptor.resetInstance();\n McpAppBridge.resetInstance();\n AppsSdkBridge.resetInstance();\n });\n\n const toolName = \"test-tool\";\n const args = { input: \"test input\" };\n const data: CallToolResponse = {\n content: [{ type: \"text\" as const, text: \"test result\" }],\n structuredContent: { result: \"test\" },\n isError: false,\n meta: {},\n };\n const error = new Error(\"test error\");\n\n it(\"should normalize _meta to meta when SDK returns _meta instead of meta\", async () => {\n const rawSdkResponse = {\n content: [{ type: \"text\" as const, text: \"result\" }],\n structuredContent: { value: 1 },\n isError: false,\n meta: { secret: \"only visible to widget\" },\n };\n callToolMock.mockResolvedValueOnce(rawSdkResponse);\n const { result } = renderHook(() =>\n useCallTool<typeof args, typeof data>(toolName),\n );\n\n await act(async () => {\n result.current.callTool(args);\n });\n\n await waitFor(() => {\n expect(result.current.data).toMatchObject({\n meta: { secret: \"only visible to widget\" },\n });\n });\n });\n\n it(\"should call adaptor.callTool with correct arguments\", async () => {\n const { result } = renderHook(() =>\n useCallTool<typeof args, typeof data>(toolName),\n );\n await act(async () => {\n result.current.callTool(args);\n });\n expect(callToolMock).toHaveBeenCalledWith(toolName, args, {\n timeout: undefined,\n });\n });\n\n it(\"should call onSuccess callback with correct data and toolArgs on successful execution\", async () => {\n const onSuccess = vi.fn();\n const onError = vi.fn();\n callToolMock.mockResolvedValueOnce(data);\n const { result } = renderHook(() =>\n useCallTool<typeof args, typeof data>(toolName),\n );\n\n act(() => {\n result.current.callTool(args, {\n onSuccess,\n onError,\n });\n });\n\n await waitFor(() => {\n expect(onSuccess).toHaveBeenCalledWith(data, args);\n expect(onError).not.toHaveBeenCalled();\n });\n });\n\n it(\"should call onError callback with error and toolArgs on failed execution\", async () => {\n const onSuccess = vi.fn();\n const onError = vi.fn();\n callToolMock.mockRejectedValueOnce(error);\n const { result } = renderHook(() =>\n useCallTool<typeof args, typeof data>(toolName),\n );\n\n act(() => {\n result.current.callTool(args, {\n onSuccess,\n onError,\n });\n });\n\n await waitFor(() => {\n expect(onSuccess).not.toHaveBeenCalled();\n expect(onError).toHaveBeenCalledWith(error, args);\n });\n });\n\n it(\"should call onSettled callback with data and undefined error on successful execution\", async () => {\n const onSuccess = vi.fn();\n const onError = vi.fn();\n const onSettled = vi.fn();\n callToolMock.mockResolvedValueOnce(data);\n const { result } = renderHook(() =>\n useCallTool<typeof args, typeof data>(toolName),\n );\n\n act(() => {\n result.current.callTool(args, {\n onSuccess,\n onError,\n onSettled,\n });\n });\n\n await waitFor(() => {\n expect(onSuccess).toHaveBeenCalledWith(data, args);\n expect(onSettled).toHaveBeenCalledWith(data, undefined, args);\n expect(onError).not.toHaveBeenCalled();\n });\n });\n\n it(\"should call onSettled callback with undefined data and error on failed execution\", async () => {\n const onSuccess = vi.fn();\n const onError = vi.fn();\n const onSettled = vi.fn();\n callToolMock.mockRejectedValueOnce(error);\n const { result } = renderHook(() =>\n useCallTool<typeof args, typeof data>(toolName),\n );\n\n act(() => {\n result.current.callTool(args, {\n onSuccess,\n onError,\n onSettled,\n });\n });\n\n await waitFor(() => {\n expect(onError).toHaveBeenCalledWith(error, args);\n expect(onSettled).toHaveBeenCalledWith(undefined, error, args);\n expect(onSuccess).not.toHaveBeenCalled();\n });\n });\n\n it(\"should always return last call started state\", async () => {\n const { result } = renderHook(() =>\n useCallTool<typeof args, typeof data>(toolName),\n );\n\n const firstCallData = {\n ...data,\n structuredContent: { result: \"first call result\" },\n };\n const secondCallData = {\n ...data,\n structuredContent: { result: \"second call result\" },\n };\n const { promise: firstCallToolPromise, resolve: resolveFirstCallTool } =\n Promise.withResolvers();\n const { promise: secondCallToolPromise, resolve: resolveSecondCallTool } =\n Promise.withResolvers();\n callToolMock\n .mockImplementationOnce(() => firstCallToolPromise)\n .mockImplementationOnce(() => secondCallToolPromise);\n\n await act(() => {\n result.current.callTool(args);\n result.current.callTool(args);\n resolveFirstCallTool(firstCallData);\n return firstCallToolPromise;\n });\n\n expect(result.current.status).toEqual(\"pending\");\n expect(result.current.data).toEqual(undefined);\n resolveSecondCallTool(secondCallData);\n\n await waitFor(() => {\n expect(result.current.status).toEqual(\"success\");\n expect(result.current.data).toEqual(secondCallData);\n });\n });\n\n it(\"should keep callTool and callToolAsync stable across renders\", () => {\n const { result, rerender } = renderHook(() => useCallTool(toolName));\n const { callTool, callToolAsync } = result.current;\n\n rerender();\n\n expect(result.current.callTool).toBe(callTool);\n expect(result.current.callToolAsync).toBe(callToolAsync);\n });\n});\n\ndescribe(\"useCallTool - TypeScript typing\", () => {\n let callToolMock: Mock;\n\n beforeEach(() => {\n HostAdaptor.resetInstance();\n McpAppBridge.resetInstance();\n AppsSdkBridge.resetInstance();\n vi.stubGlobal(\"parent\", { postMessage: vi.fn() });\n vi.stubGlobal(\"skybridge\", {});\n vi.stubGlobal(\"openai\", { callTool: vi.fn() });\n callToolMock = vi.spyOn(getAdaptor(), \"callTool\") as unknown as Mock;\n });\n\n afterEach(() => {\n vi.unstubAllGlobals();\n vi.resetAllMocks();\n HostAdaptor.resetInstance();\n McpAppBridge.resetInstance();\n AppsSdkBridge.resetInstance();\n });\n\n it(\"should have correct return types when ToolArgs is null and ToolResponse is specified\", async () => {\n type TestResponse = CallToolResponse & {\n structuredContent: { result: string };\n meta: { id: number };\n };\n\n const { result } = renderHook(() =>\n useCallTool<null, TestResponse>(\"test-tool\"),\n );\n const data: TestResponse = {\n content: [{ type: \"text\" as const, text: \"test\" }],\n structuredContent: { result: \"test\" },\n isError: false,\n meta: { id: 123 },\n };\n\n callToolMock.mockResolvedValueOnce(data);\n\n await act(async () => {\n result.current.callTool();\n });\n\n expect(callToolMock).toHaveBeenCalledWith(\"test-tool\", null, {\n timeout: undefined,\n });\n expectTypeOf<typeof data | undefined>(result.current.data);\n });\n\n it(\"should correctly type callToolAsync return value\", async () => {\n type TestArgs = { query: string };\n type TestResponse = CallToolResponse & {\n structuredContent: { answer: string };\n };\n\n const { result } = renderHook(() =>\n useCallTool<TestArgs, TestResponse>(\"test-tool\"),\n );\n\n const testArgs: TestArgs = { query: \"test\" };\n const mockResponse: TestResponse = {\n content: [{ type: \"text\" as const, text: \"answer\" }],\n structuredContent: { answer: \"test answer\" },\n isError: false,\n meta: {},\n };\n\n callToolMock.mockResolvedValueOnce(mockResponse);\n\n const returnedValue = await act(async () => {\n return result.current.callToolAsync(testArgs);\n });\n expectTypeOf<typeof mockResponse>(returnedValue);\n expect(returnedValue).toEqual(mockResponse);\n });\n\n it(\"should correctly type callToolAsync when ToolArgs is null\", async () => {\n type TestResponse = CallToolResponse & {\n structuredContent: { data: string };\n };\n\n const { result } = renderHook(() =>\n useCallTool<null, TestResponse>(\"test-tool\"),\n );\n\n const mockResponse: TestResponse = {\n content: [{ type: \"text\" as const, text: \"data\" }],\n structuredContent: { data: \"test data\" },\n isError: false,\n meta: {},\n };\n\n callToolMock.mockResolvedValueOnce(mockResponse);\n\n const returnedValue = await act(async () => {\n return result.current.callToolAsync();\n });\n expectTypeOf<typeof mockResponse>(returnedValue);\n expect(returnedValue).toEqual(mockResponse);\n });\n});\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* App-relative URL (path and query, e.g. `"/parts?tag=bolt"`) that ChatGPT
|
|
3
|
+
* opened the view on through a deep link. Updates when the user follows
|
|
4
|
+
* another deep link while the view is open. `undefined` when the view was not
|
|
5
|
+
* opened through a deep link, on hosts other than ChatGPT, or when the host
|
|
6
|
+
* sends a URL that isn't app-relative (it must start with a single `/`) or
|
|
7
|
+
* that contains a fragment.
|
|
8
|
+
*
|
|
9
|
+
* Deep links target global entrypoints: set `openai.entrypoints` to include
|
|
10
|
+
* `{ type: "global" }` on the tool.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* const deepLink = useDeepLink();
|
|
15
|
+
* useEffect(() => {
|
|
16
|
+
* if (deepLink) navigate(deepLink);
|
|
17
|
+
* }, [deepLink, navigate]);
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @see https://docs.skybridge.tech/api-reference/use-deep-link
|
|
21
|
+
*/
|
|
22
|
+
export declare function useDeepLink(): string | undefined;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { useMcpAppContext } from "../bridges/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* App-relative URL (path and query, e.g. `"/parts?tag=bolt"`) that ChatGPT
|
|
4
|
+
* opened the view on through a deep link. Updates when the user follows
|
|
5
|
+
* another deep link while the view is open. `undefined` when the view was not
|
|
6
|
+
* opened through a deep link, on hosts other than ChatGPT, or when the host
|
|
7
|
+
* sends a URL that isn't app-relative (it must start with a single `/`) or
|
|
8
|
+
* that contains a fragment.
|
|
9
|
+
*
|
|
10
|
+
* Deep links target global entrypoints: set `openai.entrypoints` to include
|
|
11
|
+
* `{ type: "global" }` on the tool.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```tsx
|
|
15
|
+
* const deepLink = useDeepLink();
|
|
16
|
+
* useEffect(() => {
|
|
17
|
+
* if (deepLink) navigate(deepLink);
|
|
18
|
+
* }, [deepLink, navigate]);
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @see https://docs.skybridge.tech/api-reference/use-deep-link
|
|
22
|
+
*/
|
|
23
|
+
export function useDeepLink() {
|
|
24
|
+
const url = useMcpAppContext("openai/deepLink")?.url;
|
|
25
|
+
if (typeof url !== "string" ||
|
|
26
|
+
!url.startsWith("/") ||
|
|
27
|
+
url.startsWith("//") ||
|
|
28
|
+
url.startsWith("/\\") ||
|
|
29
|
+
url.includes("#")) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
return url;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=use-deep-link.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-deep-link.js","sourceRoot":"","sources":["../../../src/web/hooks/use-deep-link.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,WAAW;IACzB,MAAM,GAAG,GAAG,gBAAgB,CAAC,iBAAiB,CAAC,EAAE,GAAG,CAAC;IACrD,IACE,OAAO,GAAG,KAAK,QAAQ;QACvB,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC;QACpB,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC;QACpB,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC;QACrB,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,EACjB,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["import { useMcpAppContext } from \"../bridges/index.js\";\n\n/**\n * App-relative URL (path and query, e.g. `\"/parts?tag=bolt\"`) that ChatGPT\n * opened the view on through a deep link. Updates when the user follows\n * another deep link while the view is open. `undefined` when the view was not\n * opened through a deep link, on hosts other than ChatGPT, or when the host\n * sends a URL that isn't app-relative (it must start with a single `/`) or\n * that contains a fragment.\n *\n * Deep links target global entrypoints: set `openai.entrypoints` to include\n * `{ type: \"global\" }` on the tool.\n *\n * @example\n * ```tsx\n * const deepLink = useDeepLink();\n * useEffect(() => {\n * if (deepLink) navigate(deepLink);\n * }, [deepLink, navigate]);\n * ```\n *\n * @see https://docs.skybridge.tech/api-reference/use-deep-link\n */\nexport function useDeepLink(): string | undefined {\n const url = useMcpAppContext(\"openai/deepLink\")?.url;\n if (\n typeof url !== \"string\" ||\n !url.startsWith(\"/\") ||\n url.startsWith(\"//\") ||\n url.startsWith(\"/\\\\\") ||\n url.includes(\"#\")\n ) {\n return undefined;\n }\n return url;\n}\n"]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type FileResource, type FileResourceContent, type FileResourceWriteResult, type ResourceRepresentation } from "../bridges/index.js";
|
|
2
|
+
/** State and actions returned by {@link useFileResource}. */
|
|
3
|
+
export type FileResourceState = {
|
|
4
|
+
data: FileResource | undefined;
|
|
5
|
+
isLoading: boolean;
|
|
6
|
+
error: unknown;
|
|
7
|
+
write: (content: FileResourceContent) => Promise<FileResourceWriteResult>;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Read a resource by URI, re-read it whenever the host reports a change, and
|
|
11
|
+
* write it back. Built for the file a ChatGPT file entrypoint opened, whose
|
|
12
|
+
* `resourceUri` arrives in the tool input as `file.resourceUri`.
|
|
13
|
+
*
|
|
14
|
+
* Reading uses the standard `resources/read`. `representation` asks ChatGPT
|
|
15
|
+
* for text or a base64 blob. `write` uses the OpenAI MCP extensions
|
|
16
|
+
* (`openai/resources/write`) and sends the current `etag` as `ifMatch`, so it
|
|
17
|
+
* resolves with `conflict` instead of overwriting a newer version, and then
|
|
18
|
+
* re-reads the latest one. When ChatGPT returned no `etag`, the write has no
|
|
19
|
+
* version check. It rejects with `NotSupportedError` when the host doesn't
|
|
20
|
+
* advertise `openai/resource`, or the resource isn't loaded or writable.
|
|
21
|
+
*
|
|
22
|
+
* Pass `undefined` or an empty string as `uri` to do nothing, for example outside a file
|
|
23
|
+
* entrypoint.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```tsx
|
|
27
|
+
* const { input } = useToolInfo<"open-part">();
|
|
28
|
+
* const { data, write } = useFileResource(input.file?.resourceUri, {
|
|
29
|
+
* representation: "text",
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @see https://docs.skybridge.tech/api-reference/use-file-resource
|
|
34
|
+
*/
|
|
35
|
+
export declare function useFileResource(uri: string | undefined, options?: {
|
|
36
|
+
representation?: ResourceRepresentation;
|
|
37
|
+
}): FileResourceState;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { getAdaptor, NotSupportedError, } from "../bridges/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Read a resource by URI, re-read it whenever the host reports a change, and
|
|
5
|
+
* write it back. Built for the file a ChatGPT file entrypoint opened, whose
|
|
6
|
+
* `resourceUri` arrives in the tool input as `file.resourceUri`.
|
|
7
|
+
*
|
|
8
|
+
* Reading uses the standard `resources/read`. `representation` asks ChatGPT
|
|
9
|
+
* for text or a base64 blob. `write` uses the OpenAI MCP extensions
|
|
10
|
+
* (`openai/resources/write`) and sends the current `etag` as `ifMatch`, so it
|
|
11
|
+
* resolves with `conflict` instead of overwriting a newer version, and then
|
|
12
|
+
* re-reads the latest one. When ChatGPT returned no `etag`, the write has no
|
|
13
|
+
* version check. It rejects with `NotSupportedError` when the host doesn't
|
|
14
|
+
* advertise `openai/resource`, or the resource isn't loaded or writable.
|
|
15
|
+
*
|
|
16
|
+
* Pass `undefined` or an empty string as `uri` to do nothing, for example outside a file
|
|
17
|
+
* entrypoint.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* const { input } = useToolInfo<"open-part">();
|
|
22
|
+
* const { data, write } = useFileResource(input.file?.resourceUri, {
|
|
23
|
+
* representation: "text",
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @see https://docs.skybridge.tech/api-reference/use-file-resource
|
|
28
|
+
*/
|
|
29
|
+
export function useFileResource(uri, options) {
|
|
30
|
+
const representation = options?.representation;
|
|
31
|
+
const [state, setState] = useState();
|
|
32
|
+
const sequence = useRef(0);
|
|
33
|
+
const current = uri && state?.uri === uri ? state : undefined;
|
|
34
|
+
const currentRef = useRef(current);
|
|
35
|
+
currentRef.current = current;
|
|
36
|
+
const read = useCallback(() => {
|
|
37
|
+
if (!uri) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const id = ++sequence.current;
|
|
41
|
+
getAdaptor()
|
|
42
|
+
.readResource(uri, representation)
|
|
43
|
+
.then((resource) => ({ resource, error: undefined }), (error) => ({ error }))
|
|
44
|
+
.then((next) => {
|
|
45
|
+
if (id === sequence.current) {
|
|
46
|
+
setState((previous) => ({
|
|
47
|
+
...(previous?.uri === uri && previous),
|
|
48
|
+
uri,
|
|
49
|
+
...next,
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}, [uri, representation]);
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
if (!uri) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
read();
|
|
59
|
+
const stopWatching = getAdaptor().watchResource(uri, read);
|
|
60
|
+
return () => {
|
|
61
|
+
sequence.current++;
|
|
62
|
+
stopWatching();
|
|
63
|
+
};
|
|
64
|
+
}, [uri, read]);
|
|
65
|
+
const write = useCallback(async (content) => {
|
|
66
|
+
const resource = currentRef.current?.resource;
|
|
67
|
+
if (!uri || currentRef.current?.uri !== uri || !resource?.writable) {
|
|
68
|
+
throw new NotSupportedError("writeResource", "the resource is not loaded or not writable");
|
|
69
|
+
}
|
|
70
|
+
const result = await getAdaptor().writeResource(uri, content, resource.etag);
|
|
71
|
+
if (currentRef.current?.uri !== uri) {
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
if (result.outcome === "saved") {
|
|
75
|
+
sequence.current++;
|
|
76
|
+
setState({
|
|
77
|
+
uri,
|
|
78
|
+
resource: {
|
|
79
|
+
...content,
|
|
80
|
+
mimeType: resource.mimeType,
|
|
81
|
+
writable: true,
|
|
82
|
+
etag: result.etag,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else if (result.outcome === "conflict") {
|
|
87
|
+
read();
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
}, [uri, read]);
|
|
91
|
+
return {
|
|
92
|
+
data: current?.resource,
|
|
93
|
+
isLoading: Boolean(uri) && current === undefined,
|
|
94
|
+
error: current?.error,
|
|
95
|
+
write,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=use-file-resource.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-file-resource.js","sourceRoot":"","sources":["../../../src/web/hooks/use-file-resource.ts"],"names":[],"mappings":"AA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V,QAAQ,EAAE,QAAQ,CAAC,QAAQ;oBAC3B,QAAQ,EAAE,IAAI;oBACd,IAAI,EAAE,MAAM,CAAC,IAAI;iBAClB;aACF,CAAC,CAAC;QACL,CAAC;aAAM,IAAI,MAAM,CAAC,OAAO,KAAK,UAAU,EAAE,CAAC;YACzC,IAAI,EAAE,CAAC;QACT,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,EACD,CAAC,GAAG,EAAE,IAAI,CAAC,CACZ,CAAC;IAEF,OAAO;QACL,IAAI,EAAE,OAAO,EAAE,QAAQ;QACvB,SAAS,EAAE,OAAO,CAAC,GAAG,CAAC,IAAI,OAAO,KAAK,SAAS;QAChD,KAAK,EAAE,OAAO,EAAE,KAAK;QACrB,KAAK;KACN,CAAC;AACJ,CAAC","sourcesContent":["import { useCallback, useEffect, useRef, useState } from \"react\";\nimport {\n type FileResource,\n type FileResourceContent,\n type FileResourceWriteResult,\n getAdaptor,\n NotSupportedError,\n type ResourceRepresentation,\n} from \"../bridges/index.js\";\n\n/** State and actions returned by {@link useFileResource}. */\nexport type FileResourceState = {\n data: FileResource | undefined;\n isLoading: boolean;\n error: unknown;\n write: (content: FileResourceContent) => Promise<FileResourceWriteResult>;\n};\n\n/**\n * Read a resource by URI, re-read it whenever the host reports a change, and\n * write it back. Built for the file a ChatGPT file entrypoint opened, whose\n * `resourceUri` arrives in the tool input as `file.resourceUri`.\n *\n * Reading uses the standard `resources/read`. `representation` asks ChatGPT\n * for text or a base64 blob. `write` uses the OpenAI MCP extensions\n * (`openai/resources/write`) and sends the current `etag` as `ifMatch`, so it\n * resolves with `conflict` instead of overwriting a newer version, and then\n * re-reads the latest one. When ChatGPT returned no `etag`, the write has no\n * version check. It rejects with `NotSupportedError` when the host doesn't\n * advertise `openai/resource`, or the resource isn't loaded or writable.\n *\n * Pass `undefined` or an empty string as `uri` to do nothing, for example outside a file\n * entrypoint.\n *\n * @example\n * ```tsx\n * const { input } = useToolInfo<\"open-part\">();\n * const { data, write } = useFileResource(input.file?.resourceUri, {\n * representation: \"text\",\n * });\n * ```\n *\n * @see https://docs.skybridge.tech/api-reference/use-file-resource\n */\nexport function useFileResource(\n uri: string | undefined,\n options?: { representation?: ResourceRepresentation },\n): FileResourceState {\n const representation = options?.representation;\n const [state, setState] = useState<{\n uri: string;\n resource?: FileResource;\n error?: unknown;\n }>();\n const sequence = useRef(0);\n const current = uri && state?.uri === uri ? state : undefined;\n const currentRef = useRef(current);\n currentRef.current = current;\n\n const read = useCallback(() => {\n if (!uri) {\n return;\n }\n const id = ++sequence.current;\n getAdaptor()\n .readResource(uri, representation)\n .then(\n (resource) => ({ resource, error: undefined }),\n (error: unknown) => ({ error }),\n )\n .then((next) => {\n if (id === sequence.current) {\n setState((previous) => ({\n ...(previous?.uri === uri && previous),\n uri,\n ...next,\n }));\n }\n });\n }, [uri, representation]);\n\n useEffect(() => {\n if (!uri) {\n return;\n }\n read();\n const stopWatching = getAdaptor().watchResource(uri, read);\n return () => {\n sequence.current++;\n stopWatching();\n };\n }, [uri, read]);\n\n const write = useCallback(\n async (content: FileResourceContent) => {\n const resource = currentRef.current?.resource;\n if (!uri || currentRef.current?.uri !== uri || !resource?.writable) {\n throw new NotSupportedError(\n \"writeResource\",\n \"the resource is not loaded or not writable\",\n );\n }\n const result = await getAdaptor().writeResource(\n uri,\n content,\n resource.etag,\n );\n if (currentRef.current?.uri !== uri) {\n return result;\n }\n if (result.outcome === \"saved\") {\n sequence.current++;\n setState({\n uri,\n resource: {\n ...content,\n mimeType: resource.mimeType,\n writable: true,\n etag: result.etag,\n },\n });\n } else if (result.outcome === \"conflict\") {\n read();\n }\n return result;\n },\n [uri, read],\n );\n\n return {\n data: current?.resource,\n isLoading: Boolean(uri) && current === undefined,\n error: current?.error,\n write,\n };\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { act, renderHook, waitFor } from "@testing-library/react";
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import { HostAdaptor } from "../bridges/adaptor.js";
|
|
4
|
+
import { getAdaptor } from "../bridges/get-adaptor.js";
|
|
5
|
+
import { McpAppBridge } from "../bridges/mcp-app/bridge.js";
|
|
6
|
+
import { useFileResource } from "./use-file-resource.js";
|
|
7
|
+
describe("useFileResource", () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
vi.stubGlobal("parent", { postMessage: vi.fn() });
|
|
10
|
+
vi.stubGlobal("skybridge", {});
|
|
11
|
+
vi.stubGlobal("openai", undefined);
|
|
12
|
+
});
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
vi.unstubAllGlobals();
|
|
15
|
+
HostAdaptor.resetInstance();
|
|
16
|
+
McpAppBridge.resetInstance();
|
|
17
|
+
});
|
|
18
|
+
it("reads the resource, re-reads it on update and writes with the current etag", async () => {
|
|
19
|
+
let version = 1;
|
|
20
|
+
let onUpdated = () => { };
|
|
21
|
+
const request = vi.fn(async ({ method }) => method === "openai/resources/write"
|
|
22
|
+
? { outcome: "saved", etag: "v3" }
|
|
23
|
+
: {});
|
|
24
|
+
const app = {
|
|
25
|
+
getHostCapabilities: () => ({ experimental: { "openai/resource": {} } }),
|
|
26
|
+
readServerResource: vi.fn(async ({ uri }) => ({
|
|
27
|
+
contents: [
|
|
28
|
+
{
|
|
29
|
+
uri,
|
|
30
|
+
text: `version ${version}`,
|
|
31
|
+
_meta: {
|
|
32
|
+
"openai/resource": { writable: true, etag: `v${version}` },
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
})),
|
|
37
|
+
setNotificationHandler: (_method, handler) => {
|
|
38
|
+
onUpdated = handler;
|
|
39
|
+
},
|
|
40
|
+
request,
|
|
41
|
+
};
|
|
42
|
+
getAdaptor().mcp.getApp = () => Promise.resolve(app);
|
|
43
|
+
const { result } = renderHook(() => useFileResource("host-resource://part", { representation: "text" }));
|
|
44
|
+
await waitFor(() => expect(result.current.data).toMatchObject({
|
|
45
|
+
text: "version 1",
|
|
46
|
+
etag: "v1",
|
|
47
|
+
}));
|
|
48
|
+
await waitFor(() => expect(request).toHaveBeenCalledWith({
|
|
49
|
+
method: "resources/subscribe",
|
|
50
|
+
params: { uri: "host-resource://part" },
|
|
51
|
+
}, expect.anything()));
|
|
52
|
+
version = 2;
|
|
53
|
+
act(() => onUpdated({ params: { uri: "host-resource://part" } }));
|
|
54
|
+
await waitFor(() => expect(result.current.data).toMatchObject({ etag: "v2" }));
|
|
55
|
+
await act(async () => {
|
|
56
|
+
await result.current.write({ text: "edited" });
|
|
57
|
+
});
|
|
58
|
+
expect(request).toHaveBeenCalledWith({
|
|
59
|
+
method: "openai/resources/write",
|
|
60
|
+
params: { uri: "host-resource://part", text: "edited", ifMatch: "v2" },
|
|
61
|
+
}, expect.anything());
|
|
62
|
+
expect(result.current.data).toMatchObject({ text: "edited", etag: "v3" });
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
//# sourceMappingURL=use-file-resource.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-file-resource.test.js","sourceRoot":"","sources":["../../../src/web/hooks/use-file-resource.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAClE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AACzE,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;IAC/B,UAAU,CAAC,GAAG,EAAE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ACH,MAAM,CAAC,OAAO,CAAC,CAAC,oBAAoB,CAClC;YACE,MAAM,EAAE,wBAAwB;YAChC,MAAM,EAAE,EAAE,GAAG,EAAE,sBAAsB,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE;SACvE,EACD,MAAM,CAAC,QAAQ,EAAE,CAClB,CAAC;QACF,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,aAAa,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;IAC5E,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { act, renderHook, waitFor } from \"@testing-library/react\";\nimport { afterEach, beforeEach, describe, expect, it, vi } from \"vitest\";\nimport { HostAdaptor } from \"../bridges/adaptor.js\";\nimport { getAdaptor } from \"../bridges/get-adaptor.js\";\nimport { McpAppBridge } from \"../bridges/mcp-app/bridge.js\";\nimport { useFileResource } from \"./use-file-resource.js\";\n\ndescribe(\"useFileResource\", () => {\n beforeEach(() => {\n vi.stubGlobal(\"parent\", { postMessage: vi.fn() });\n vi.stubGlobal(\"skybridge\", {});\n vi.stubGlobal(\"openai\", undefined);\n });\n\n afterEach(() => {\n vi.unstubAllGlobals();\n HostAdaptor.resetInstance();\n McpAppBridge.resetInstance();\n });\n\n it(\"reads the resource, re-reads it on update and writes with the current etag\", async () => {\n let version = 1;\n let onUpdated: (notification: { params: { uri: string } }) => void =\n () => {};\n const request = vi.fn(async ({ method }: { method: string }) =>\n method === \"openai/resources/write\"\n ? { outcome: \"saved\", etag: \"v3\" }\n : {},\n );\n const app = {\n getHostCapabilities: () => ({ experimental: { \"openai/resource\": {} } }),\n readServerResource: vi.fn(async ({ uri }: { uri: string }) => ({\n contents: [\n {\n uri,\n text: `version ${version}`,\n _meta: {\n \"openai/resource\": { writable: true, etag: `v${version}` },\n },\n },\n ],\n })),\n setNotificationHandler: (_method: string, handler: typeof onUpdated) => {\n onUpdated = handler;\n },\n request,\n };\n (\n getAdaptor() as unknown as { mcp: { getApp: () => Promise<object> } }\n ).mcp.getApp = () => Promise.resolve(app);\n\n const { result } = renderHook(() =>\n useFileResource(\"host-resource://part\", { representation: \"text\" }),\n );\n await waitFor(() =>\n expect(result.current.data).toMatchObject({\n text: \"version 1\",\n etag: \"v1\",\n }),\n );\n await waitFor(() =>\n expect(request).toHaveBeenCalledWith(\n {\n method: \"resources/subscribe\",\n params: { uri: \"host-resource://part\" },\n },\n expect.anything(),\n ),\n );\n\n version = 2;\n act(() => onUpdated({ params: { uri: \"host-resource://part\" } }));\n await waitFor(() =>\n expect(result.current.data).toMatchObject({ etag: \"v2\" }),\n );\n\n await act(async () => {\n await result.current.write({ text: \"edited\" });\n });\n expect(request).toHaveBeenCalledWith(\n {\n method: \"openai/resources/write\",\n params: { uri: \"host-resource://part\", text: \"edited\", ifMatch: \"v2\" },\n },\n expect.anything(),\n );\n expect(result.current.data).toMatchObject({ text: \"edited\", etag: \"v3\" });\n });\n});\n"]}
|
|
@@ -1,22 +1,44 @@
|
|
|
1
|
+
import type { McpUiHostCapabilities } from "@modelcontextprotocol/ext-apps";
|
|
1
2
|
/**
|
|
2
3
|
* Known host applications, as normalized slugs. Unrecognized hosts surface
|
|
3
4
|
* their raw `hostInfo.name` string instead.
|
|
4
5
|
*/
|
|
5
6
|
export type Host = "chatgpt" | "claude" | "cursor" | "goose" | "mistral-vibe" | "alpic";
|
|
7
|
+
/**
|
|
8
|
+
* OpenAI MCP extensions the host advertises. Each flag is `true` only when the
|
|
9
|
+
* host sends a truthy `hostCapabilities.experimental["openai/<name>"]`, so every flag is
|
|
10
|
+
* `false` before the handshake and on hosts other than ChatGPT.
|
|
11
|
+
*/
|
|
12
|
+
export type OpenAIHostCapabilities = {
|
|
13
|
+
/** Read, subscribe to and write the file opened through a file entrypoint. */
|
|
14
|
+
resource: boolean;
|
|
15
|
+
/** Titles, thumbnails and background content in `ui/update-model-context`. */
|
|
16
|
+
modelContext: boolean;
|
|
17
|
+
/** `ui/message` targets and titled items. */
|
|
18
|
+
message: boolean;
|
|
19
|
+
/** Open a local file with `openai/files/open`. */
|
|
20
|
+
files: boolean;
|
|
21
|
+
};
|
|
6
22
|
export type HostInfo = {
|
|
7
23
|
name: Host | (string & {}) | undefined;
|
|
8
24
|
version: string | undefined;
|
|
25
|
+
/** The host's MCP Apps capabilities, `undefined` until the handshake. */
|
|
26
|
+
capabilities: McpUiHostCapabilities | undefined;
|
|
27
|
+
/** OpenAI MCP extensions the host supports. */
|
|
28
|
+
openaiCapabilities: OpenAIHostCapabilities;
|
|
9
29
|
};
|
|
10
30
|
/**
|
|
11
|
-
* Identity of the host application rendering the view, from
|
|
12
|
-
* `ui/initialize` handshake. `name` is normalized to a
|
|
13
|
-
* recognized, otherwise the raw string;
|
|
14
|
-
*
|
|
31
|
+
* Identity and capabilities of the host application rendering the view, from
|
|
32
|
+
* the MCP Apps `ui/initialize` handshake. `name` is normalized to a
|
|
33
|
+
* {@link Host} slug when recognized, otherwise the raw string; `name`,
|
|
34
|
+
* `version` and `capabilities` are `undefined` until the handshake resolves
|
|
35
|
+
* (the view renders first and re-renders once it lands).
|
|
15
36
|
*
|
|
16
37
|
* @example
|
|
17
38
|
* ```tsx
|
|
18
|
-
* const { name } = useHost();
|
|
39
|
+
* const { name, openaiCapabilities } = useHost();
|
|
19
40
|
* if (name === "claude") return <ClaudeLayout />;
|
|
41
|
+
* if (openaiCapabilities.files) return <OpenFileButton />;
|
|
20
42
|
* ```
|
|
21
43
|
*/
|
|
22
44
|
export declare function useHost(): HostInfo;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
1
2
|
import { useMcpAppContext } from "../bridges/index.js";
|
|
2
3
|
const HOST_BY_REPORTED_NAME = {
|
|
3
4
|
chatgpt: "chatgpt",
|
|
@@ -8,23 +9,37 @@ const HOST_BY_REPORTED_NAME = {
|
|
|
8
9
|
"alpic-playground": "alpic",
|
|
9
10
|
};
|
|
10
11
|
/**
|
|
11
|
-
* Identity of the host application rendering the view, from
|
|
12
|
-
* `ui/initialize` handshake. `name` is normalized to a
|
|
13
|
-
* recognized, otherwise the raw string;
|
|
14
|
-
*
|
|
12
|
+
* Identity and capabilities of the host application rendering the view, from
|
|
13
|
+
* the MCP Apps `ui/initialize` handshake. `name` is normalized to a
|
|
14
|
+
* {@link Host} slug when recognized, otherwise the raw string; `name`,
|
|
15
|
+
* `version` and `capabilities` are `undefined` until the handshake resolves
|
|
16
|
+
* (the view renders first and re-renders once it lands).
|
|
15
17
|
*
|
|
16
18
|
* @example
|
|
17
19
|
* ```tsx
|
|
18
|
-
* const { name } = useHost();
|
|
20
|
+
* const { name, openaiCapabilities } = useHost();
|
|
19
21
|
* if (name === "claude") return <ClaudeLayout />;
|
|
22
|
+
* if (openaiCapabilities.files) return <OpenFileButton />;
|
|
20
23
|
* ```
|
|
21
24
|
*/
|
|
22
25
|
export function useHost() {
|
|
23
26
|
const hostInfo = useMcpAppContext("hostInfo");
|
|
27
|
+
const capabilities = useMcpAppContext("hostCapabilities") ?? undefined;
|
|
24
28
|
const name = hostInfo?.name;
|
|
29
|
+
const openaiCapabilities = useMemo(() => {
|
|
30
|
+
const supports = (extension) => Boolean(capabilities?.experimental?.[`openai/${extension}`]);
|
|
31
|
+
return {
|
|
32
|
+
resource: supports("resource"),
|
|
33
|
+
modelContext: supports("modelContext"),
|
|
34
|
+
message: supports("message"),
|
|
35
|
+
files: supports("files"),
|
|
36
|
+
};
|
|
37
|
+
}, [capabilities]);
|
|
25
38
|
return {
|
|
26
39
|
name: name !== undefined ? (HOST_BY_REPORTED_NAME[name] ?? name) : undefined,
|
|
27
40
|
version: hostInfo?.version,
|
|
41
|
+
capabilities,
|
|
42
|
+
openaiCapabilities,
|
|
28
43
|
};
|
|
29
44
|
}
|
|
30
45
|
//# sourceMappingURL=use-host.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-host.js","sourceRoot":"","sources":["../../../src/web/hooks/use-host.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-host.js","sourceRoot":"","sources":["../../../src/web/hooks/use-host.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAChC,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAcvD,MAAM,qBAAqB,GAAyB;IAClD,OAAO,EAAE,SAAS;IAClB,MAAM,EAAE,QAAQ;IAChB,MAAM,EAAE,QAAQ;IAChB,aAAa,EAAE,OAAO;IACtB,SAAS,EAAE,cAAc;IACzB,kBAAkB,EAAE,OAAO;CAC5B,CAAC;AA2BF;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,OAAO;IACrB,MAAM,QAAQ,GAAG,gBAAgB,CAAC,UAAU,CAAC,CAAC;IAC9C,MAAM,YAAY,GAAG,gBAAgB,CAAC,kBAAkB,CAAC,IAAI,SAAS,CAAC;IACvE,MAAM,IAAI,GAAG,QAAQ,EAAE,IAAI,CAAC;IAC5B,MAAM,kBAAkB,GAAG,OAAO,CAAC,GAAG,EAAE;QACtC,MAAM,QAAQ,GAAG,CAAC,SAAiB,EAAE,EAAE,CACrC,OAAO,CAAC,YAAY,EAAE,YAAY,EAAE,CAAC,UAAU,SAAS,EAAE,CAAC,CAAC,CAAC;QAC/D,OAAO;YACL,QAAQ,EAAE,QAAQ,CAAC,UAAU,CAAC;YAC9B,YAAY,EAAE,QAAQ,CAAC,cAAc,CAAC;YACtC,OAAO,EAAE,QAAQ,CAAC,SAAS,CAAC;YAC5B,KAAK,EAAE,QAAQ,CAAC,OAAO,CAAC;SACzB,CAAC;IACJ,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC;IAEnB,OAAO;QACL,IAAI,EACF,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,qBAAqB,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS;QACxE,OAAO,EAAE,QAAQ,EAAE,OAAO;QAC1B,YAAY;QACZ,kBAAkB;KACnB,CAAC;AACJ,CAAC","sourcesContent":["import type { McpUiHostCapabilities } from \"@modelcontextprotocol/ext-apps\";\nimport { useMemo } from \"react\";\nimport { useMcpAppContext } from \"../bridges/index.js\";\n\n/**\n * Known host applications, as normalized slugs. Unrecognized hosts surface\n * their raw `hostInfo.name` string instead.\n */\nexport type Host =\n | \"chatgpt\"\n | \"claude\"\n | \"cursor\"\n | \"goose\"\n | \"mistral-vibe\"\n | \"alpic\";\n\nconst HOST_BY_REPORTED_NAME: Record<string, Host> = {\n chatgpt: \"chatgpt\",\n Claude: \"claude\",\n Cursor: \"cursor\",\n \"MCP-UI Host\": \"goose\",\n \"Le Chat\": \"mistral-vibe\",\n \"alpic-playground\": \"alpic\",\n};\n\n/**\n * OpenAI MCP extensions the host advertises. Each flag is `true` only when the\n * host sends a truthy `hostCapabilities.experimental[\"openai/<name>\"]`, so every flag is\n * `false` before the handshake and on hosts other than ChatGPT.\n */\nexport type OpenAIHostCapabilities = {\n /** Read, subscribe to and write the file opened through a file entrypoint. */\n resource: boolean;\n /** Titles, thumbnails and background content in `ui/update-model-context`. */\n modelContext: boolean;\n /** `ui/message` targets and titled items. */\n message: boolean;\n /** Open a local file with `openai/files/open`. */\n files: boolean;\n};\n\nexport type HostInfo = {\n name: Host | (string & {}) | undefined;\n version: string | undefined;\n /** The host's MCP Apps capabilities, `undefined` until the handshake. */\n capabilities: McpUiHostCapabilities | undefined;\n /** OpenAI MCP extensions the host supports. */\n openaiCapabilities: OpenAIHostCapabilities;\n};\n\n/**\n * Identity and capabilities of the host application rendering the view, from\n * the MCP Apps `ui/initialize` handshake. `name` is normalized to a\n * {@link Host} slug when recognized, otherwise the raw string; `name`,\n * `version` and `capabilities` are `undefined` until the handshake resolves\n * (the view renders first and re-renders once it lands).\n *\n * @example\n * ```tsx\n * const { name, openaiCapabilities } = useHost();\n * if (name === \"claude\") return <ClaudeLayout />;\n * if (openaiCapabilities.files) return <OpenFileButton />;\n * ```\n */\nexport function useHost(): HostInfo {\n const hostInfo = useMcpAppContext(\"hostInfo\");\n const capabilities = useMcpAppContext(\"hostCapabilities\") ?? undefined;\n const name = hostInfo?.name;\n const openaiCapabilities = useMemo(() => {\n const supports = (extension: string) =>\n Boolean(capabilities?.experimental?.[`openai/${extension}`]);\n return {\n resource: supports(\"resource\"),\n modelContext: supports(\"modelContext\"),\n message: supports(\"message\"),\n files: supports(\"files\"),\n };\n }, [capabilities]);\n\n return {\n name:\n name !== undefined ? (HOST_BY_REPORTED_NAME[name] ?? name) : undefined,\n version: hostInfo?.version,\n capabilities,\n openaiCapabilities,\n };\n}\n"]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { OpenAIHostContext } from "../bridges/mcp-app/types.js";
|
|
2
|
+
import type { ModelContextParams } from "../bridges/types.js";
|
|
3
|
+
/** Model context the view attached, as ChatGPT reports it back. */
|
|
4
|
+
export type ModelContextState = OpenAIHostContext["openai/modelContext"];
|
|
5
|
+
/**
|
|
6
|
+
* Send content to the model through the MCP Apps `ui/update-model-context`
|
|
7
|
+
* request. Each `update` replaces what this hook sent before. Outside ChatGPT
|
|
8
|
+
* it is merged with the {@link useViewState} state, which wins on shared
|
|
9
|
+
* `structuredContent` keys.
|
|
10
|
+
*
|
|
11
|
+
* ChatGPT also reads the OpenAI presentation metadata, shows each block as a
|
|
12
|
+
* composer attachment and reports the attached context back:
|
|
13
|
+
* `_meta["openai/title"]`, `_meta["openai/thumbnail"]` on text blocks, and
|
|
14
|
+
* `annotations.audience: ["assistant"]` to hide a block from the user.
|
|
15
|
+
*
|
|
16
|
+
* - `supported`: whether the host advertises `updateModelContext` or
|
|
17
|
+
* `openai/modelContext`.
|
|
18
|
+
* - `context`: ChatGPT only, the attached context; `null` once the user
|
|
19
|
+
* removed it, `undefined` when the host never reported one.
|
|
20
|
+
* - `update`: replaces the context and resolves with ChatGPT's `updateId`,
|
|
21
|
+
* to compare with `context.updateId`. Rejects with `NotSupportedError` on
|
|
22
|
+
* hosts that don't advertise model context updates.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* const { supported, update } = useModelContext();
|
|
27
|
+
* update({
|
|
28
|
+
* content: [
|
|
29
|
+
* {
|
|
30
|
+
* type: "text",
|
|
31
|
+
* text: "Selected: M6 hex bolt",
|
|
32
|
+
* _meta: { "openai/title": "Hex bolt" },
|
|
33
|
+
* },
|
|
34
|
+
* ],
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @see https://docs.skybridge.tech/api-reference/use-model-context
|
|
39
|
+
*/
|
|
40
|
+
export declare function useModelContext(): {
|
|
41
|
+
supported: boolean;
|
|
42
|
+
context: ModelContextState;
|
|
43
|
+
update: (params: ModelContextParams) => Promise<string | undefined>;
|
|
44
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { ContentBlockSchema } from "@modelcontextprotocol/core";
|
|
2
|
+
import { useCallback, useMemo } from "react";
|
|
3
|
+
import * as z from "zod/v4";
|
|
4
|
+
import { getAdaptor, useMcpAppContext } from "../bridges/index.js";
|
|
5
|
+
import { useHost } from "./use-host.js";
|
|
6
|
+
const ModelContextSchema = z
|
|
7
|
+
.object({
|
|
8
|
+
updateId: z.string().min(1),
|
|
9
|
+
content: z.array(ContentBlockSchema).optional(),
|
|
10
|
+
structuredContent: z.record(z.string(), z.unknown()).optional(),
|
|
11
|
+
})
|
|
12
|
+
.nullable();
|
|
13
|
+
/**
|
|
14
|
+
* Send content to the model through the MCP Apps `ui/update-model-context`
|
|
15
|
+
* request. Each `update` replaces what this hook sent before. Outside ChatGPT
|
|
16
|
+
* it is merged with the {@link useViewState} state, which wins on shared
|
|
17
|
+
* `structuredContent` keys.
|
|
18
|
+
*
|
|
19
|
+
* ChatGPT also reads the OpenAI presentation metadata, shows each block as a
|
|
20
|
+
* composer attachment and reports the attached context back:
|
|
21
|
+
* `_meta["openai/title"]`, `_meta["openai/thumbnail"]` on text blocks, and
|
|
22
|
+
* `annotations.audience: ["assistant"]` to hide a block from the user.
|
|
23
|
+
*
|
|
24
|
+
* - `supported`: whether the host advertises `updateModelContext` or
|
|
25
|
+
* `openai/modelContext`.
|
|
26
|
+
* - `context`: ChatGPT only, the attached context; `null` once the user
|
|
27
|
+
* removed it, `undefined` when the host never reported one.
|
|
28
|
+
* - `update`: replaces the context and resolves with ChatGPT's `updateId`,
|
|
29
|
+
* to compare with `context.updateId`. Rejects with `NotSupportedError` on
|
|
30
|
+
* hosts that don't advertise model context updates.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
* const { supported, update } = useModelContext();
|
|
35
|
+
* update({
|
|
36
|
+
* content: [
|
|
37
|
+
* {
|
|
38
|
+
* type: "text",
|
|
39
|
+
* text: "Selected: M6 hex bolt",
|
|
40
|
+
* _meta: { "openai/title": "Hex bolt" },
|
|
41
|
+
* },
|
|
42
|
+
* ],
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @see https://docs.skybridge.tech/api-reference/use-model-context
|
|
47
|
+
*/
|
|
48
|
+
export function useModelContext() {
|
|
49
|
+
const { capabilities, openaiCapabilities } = useHost();
|
|
50
|
+
const reported = useMcpAppContext("openai/modelContext");
|
|
51
|
+
const context = useMemo(() => {
|
|
52
|
+
const parsed = ModelContextSchema.safeParse(reported);
|
|
53
|
+
return parsed.success ? parsed.data : undefined;
|
|
54
|
+
}, [reported]);
|
|
55
|
+
const update = useCallback((params) => getAdaptor().updateModelContext(params), []);
|
|
56
|
+
return {
|
|
57
|
+
supported: Boolean(capabilities?.updateModelContext) ||
|
|
58
|
+
openaiCapabilities.modelContext,
|
|
59
|
+
context,
|
|
60
|
+
update,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=use-model-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1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B,EAAE,EAAE,CAAC,UAAU,EAAE,CAAC,kBAAkB,CAAC,MAAM,CAAC,EACvE,EAAE,CACH,CAAC;IAEF,OAAO;QACL,SAAS,EACP,OAAO,CAAC,YAAY,EAAE,kBAAkB,CAAC;YACzC,kBAAkB,CAAC,YAAY;QACjC,OAAO;QACP,MAAM;KACP,CAAC;AACJ,CAAC","sourcesContent":["import { ContentBlockSchema } from \"@modelcontextprotocol/core\";\nimport { useCallback, useMemo } from \"react\";\nimport * as z from \"zod/v4\";\nimport { getAdaptor, useMcpAppContext } from \"../bridges/index.js\";\nimport type { OpenAIHostContext } from \"../bridges/mcp-app/types.js\";\nimport type { ModelContextParams } from \"../bridges/types.js\";\nimport { useHost } from \"./use-host.js\";\n\nconst ModelContextSchema = z\n .object({\n updateId: z.string().min(1),\n content: z.array(ContentBlockSchema).optional(),\n structuredContent: z.record(z.string(), z.unknown()).optional(),\n })\n .nullable();\n\n/** Model context the view attached, as ChatGPT reports it back. */\nexport type ModelContextState = OpenAIHostContext[\"openai/modelContext\"];\n\n/**\n * Send content to the model through the MCP Apps `ui/update-model-context`\n * request. Each `update` replaces what this hook sent before. Outside ChatGPT\n * it is merged with the {@link useViewState} state, which wins on shared\n * `structuredContent` keys.\n *\n * ChatGPT also reads the OpenAI presentation metadata, shows each block as a\n * composer attachment and reports the attached context back:\n * `_meta[\"openai/title\"]`, `_meta[\"openai/thumbnail\"]` on text blocks, and\n * `annotations.audience: [\"assistant\"]` to hide a block from the user.\n *\n * - `supported`: whether the host advertises `updateModelContext` or\n * `openai/modelContext`.\n * - `context`: ChatGPT only, the attached context; `null` once the user\n * removed it, `undefined` when the host never reported one.\n * - `update`: replaces the context and resolves with ChatGPT's `updateId`,\n * to compare with `context.updateId`. Rejects with `NotSupportedError` on\n * hosts that don't advertise model context updates.\n *\n * @example\n * ```tsx\n * const { supported, update } = useModelContext();\n * update({\n * content: [\n * {\n * type: \"text\",\n * text: \"Selected: M6 hex bolt\",\n * _meta: { \"openai/title\": \"Hex bolt\" },\n * },\n * ],\n * });\n * ```\n *\n * @see https://docs.skybridge.tech/api-reference/use-model-context\n */\nexport function useModelContext(): {\n supported: boolean;\n context: ModelContextState;\n update: (params: ModelContextParams) => Promise<string | undefined>;\n} {\n const { capabilities, openaiCapabilities } = useHost();\n const reported = useMcpAppContext(\"openai/modelContext\");\n const context = useMemo(() => {\n const parsed = ModelContextSchema.safeParse(reported);\n return parsed.success ? parsed.data : undefined;\n }, [reported]);\n const update = useCallback(\n (params: ModelContextParams) => getAdaptor().updateModelContext(params),\n [],\n );\n\n return {\n supported:\n Boolean(capabilities?.updateModelContext) ||\n openaiCapabilities.modelContext,\n context,\n update,\n };\n}\n"]}
|