@apollo/client-ai-apps 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -116,7 +116,7 @@ var ExtendedApolloClient = class extends ApolloClient {
116
116
  }
117
117
  async prefetchData() {
118
118
  this.manifest.operations.forEach((operation) => {
119
- if (operation.prefetch && operation.prefetchID && window.openai.toolOutput.prefetch[operation.prefetchID]) {
119
+ if (operation.prefetch && operation.prefetchID && window.openai.toolOutput.prefetch?.[operation.prefetchID]) {
120
120
  this.writeQuery({
121
121
  query: parse(operation.body),
122
122
  data: window.openai.toolOutput.prefetch[operation.prefetchID].data
@@ -6,13 +6,14 @@ export type ApplicationManifest = {
6
6
  hash: string;
7
7
  resource: string;
8
8
  operations: ManifestOperation[];
9
+ csp: ManifestCsp;
9
10
  };
10
11
  export type ManifestOperation = {
11
12
  id: string;
12
13
  name: string;
13
14
  type: "query" | "mutation";
14
15
  body: string;
15
- variables: Record<string, string>;
16
+ variables?: Record<string, string | undefined>;
16
17
  prefetch: boolean;
17
18
  prefetchID?: string;
18
19
  tools: ManifestTool[];
@@ -27,3 +28,7 @@ export type ManifestExtraInput = {
27
28
  description: string;
28
29
  type: "string" | "boolean" | "number";
29
30
  };
31
+ export type ManifestCsp = {
32
+ connectDomains: string[];
33
+ resourceDomains: string[];
34
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apollo/client-ai-apps",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -59,7 +59,7 @@ export class ExtendedApolloClient extends ApolloClient {
59
59
  async prefetchData() {
60
60
  // Write prefetched data to the cache
61
61
  this.manifest.operations.forEach((operation) => {
62
- if (operation.prefetch && operation.prefetchID && window.openai.toolOutput.prefetch[operation.prefetchID]) {
62
+ if (operation.prefetch && operation.prefetchID && window.openai.toolOutput.prefetch?.[operation.prefetchID]) {
63
63
  this.writeQuery({
64
64
  query: parse(operation.body),
65
65
  data: window.openai.toolOutput.prefetch[operation.prefetchID].data,
@@ -0,0 +1,13 @@
1
+ import { expect, test, vi } from "vitest";
2
+ import { useSendFollowUpMessage } from "./useSendFollowUpMessage";
3
+
4
+ test("Should set display mode when returned function is called", async () => {
5
+ vi.stubGlobal("openai", {
6
+ sendFollowUpMessage: vi.fn(async (args: { prompt: string }) => {}),
7
+ });
8
+
9
+ const sendFollowUpMessage = useSendFollowUpMessage();
10
+ await sendFollowUpMessage("Do a cool thing!");
11
+
12
+ expect(window.openai.sendFollowUpMessage).toBeCalledWith({ prompt: "Do a cool thing!" });
13
+ });
@@ -0,0 +1,50 @@
1
+ import { expect, test, vi } from "vitest";
2
+ import { useToolEffect, ToolUseProvider } from "./useToolEffect";
3
+ import { renderHook } from "@testing-library/react";
4
+
5
+ test("Should trigger effect when tool name matches toolResponseMetadata", async () => {
6
+ vi.stubGlobal("openai", {
7
+ toolResponseMetadata: { toolName: "my-app--my-tool" },
8
+ });
9
+ const navigate = vi.fn();
10
+ const wrapper = ({ children }: { children: any }) => <ToolUseProvider appName="my-app">{children}</ToolUseProvider>;
11
+
12
+ renderHook(() => useToolEffect("my-tool", () => navigate(), [navigate]), { wrapper });
13
+
14
+ expect(navigate).toBeCalled();
15
+ });
16
+
17
+ test("Should trigger effect when one of multiple tool name matches toolResponseMetadata", async () => {
18
+ vi.stubGlobal("openai", {
19
+ toolResponseMetadata: { toolName: "my-app--my-tool" },
20
+ });
21
+ const navigate = vi.fn();
22
+ const wrapper = ({ children }: { children: any }) => <ToolUseProvider appName="my-app">{children}</ToolUseProvider>;
23
+
24
+ renderHook(() => useToolEffect(["my-tool", "my-similar-tool"], () => navigate(), [navigate]), { wrapper });
25
+
26
+ expect(navigate).toBeCalled();
27
+ });
28
+
29
+ test("Should not trigger effect when tool name does not match toolResponseMetadata", async () => {
30
+ vi.stubGlobal("openai", {
31
+ toolResponseMetadata: { toolName: "my-app--my-other-tool" },
32
+ });
33
+ const navigate = vi.fn();
34
+ const wrapper = ({ children }: { children: any }) => <ToolUseProvider appName="my-app">{children}</ToolUseProvider>;
35
+
36
+ renderHook(() => useToolEffect("my-tool", () => navigate(), [navigate]), { wrapper });
37
+
38
+ expect(navigate).not.toBeCalled();
39
+ });
40
+
41
+ test("Should throw an error when used outside of a ToolUseProvider", async () => {
42
+ vi.stubGlobal("openai", {
43
+ toolResponseMetadata: { toolName: "my-app--my-other-tool" },
44
+ });
45
+ const navigate = vi.fn();
46
+
47
+ expect(() => renderHook(() => useToolEffect("my-tool", () => navigate(), [navigate]))).toThrowError(
48
+ "useToolEffect must be used within ToolUseProvider"
49
+ );
50
+ });
@@ -0,0 +1,13 @@
1
+ import { expect, test, vi } from "vitest";
2
+ import { useToolInput } from "./useToolInput";
3
+ import { renderHook, act } from "@testing-library/react";
4
+
5
+ test("Should return tool input when called", async () => {
6
+ vi.stubGlobal("openai", {
7
+ toolInput: { name: "John" },
8
+ });
9
+
10
+ const { result } = renderHook(() => useToolInput());
11
+
12
+ expect(result.current).toEqual({ name: "John" });
13
+ });
@@ -6,6 +6,7 @@ export type ApplicationManifest = {
6
6
  hash: string;
7
7
  resource: string;
8
8
  operations: ManifestOperation[];
9
+ csp: ManifestCsp;
9
10
  };
10
11
 
11
12
  export type ManifestOperation = {
@@ -13,7 +14,7 @@ export type ManifestOperation = {
13
14
  name: string;
14
15
  type: "query" | "mutation";
15
16
  body: string;
16
- variables: Record<string, string>;
17
+ variables?: Record<string, string | undefined>;
17
18
  prefetch: boolean;
18
19
  prefetchID?: string;
19
20
  tools: ManifestTool[];
@@ -30,3 +31,8 @@ export type ManifestExtraInput = {
30
31
  description: string;
31
32
  type: "string" | "boolean" | "number";
32
33
  };
34
+
35
+ export type ManifestCsp = {
36
+ connectDomains: string[];
37
+ resourceDomains: string[];
38
+ };