@apollo/client-ai-apps 0.5.2 → 0.5.3

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.
Files changed (96) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/dist/core/ApolloClient.d.ts +1 -1
  3. package/dist/core/ApolloClient.d.ts.map +1 -1
  4. package/dist/core/ApolloClient.js +1 -1
  5. package/dist/core/ApolloClient.js.map +1 -1
  6. package/dist/mcp/core/ApolloClient.d.ts +6 -1
  7. package/dist/mcp/core/ApolloClient.d.ts.map +1 -1
  8. package/dist/mcp/core/ApolloClient.js +36 -3
  9. package/dist/mcp/core/ApolloClient.js.map +1 -1
  10. package/dist/mcp/core/McpAppManager.d.ts +2 -2
  11. package/dist/mcp/core/McpAppManager.d.ts.map +1 -1
  12. package/dist/mcp/core/McpAppManager.js +3 -3
  13. package/dist/mcp/core/McpAppManager.js.map +1 -1
  14. package/dist/mcp/react/hooks/createHydrationUtils.d.ts +15 -0
  15. package/dist/mcp/react/hooks/createHydrationUtils.d.ts.map +1 -0
  16. package/dist/mcp/react/hooks/createHydrationUtils.js +113 -0
  17. package/dist/mcp/react/hooks/createHydrationUtils.js.map +1 -0
  18. package/dist/mcp/react/index.d.ts +1 -0
  19. package/dist/mcp/react/index.d.ts.map +1 -1
  20. package/dist/mcp/react/index.js +1 -0
  21. package/dist/mcp/react/index.js.map +1 -1
  22. package/dist/openai/core/ApolloClient.d.ts +6 -1
  23. package/dist/openai/core/ApolloClient.d.ts.map +1 -1
  24. package/dist/openai/core/ApolloClient.js +37 -3
  25. package/dist/openai/core/ApolloClient.js.map +1 -1
  26. package/dist/openai/core/McpAppManager.d.ts +2 -2
  27. package/dist/openai/core/McpAppManager.d.ts.map +1 -1
  28. package/dist/openai/core/McpAppManager.js +5 -5
  29. package/dist/openai/core/McpAppManager.js.map +1 -1
  30. package/dist/openai/react/hooks/createHydrationUtils.d.ts +15 -0
  31. package/dist/openai/react/hooks/createHydrationUtils.d.ts.map +1 -0
  32. package/dist/openai/react/hooks/createHydrationUtils.js +113 -0
  33. package/dist/openai/react/hooks/createHydrationUtils.js.map +1 -0
  34. package/dist/openai/react/index.d.ts +1 -0
  35. package/dist/openai/react/index.d.ts.map +1 -1
  36. package/dist/openai/react/index.js +1 -0
  37. package/dist/openai/react/index.js.map +1 -1
  38. package/dist/react/ApolloProvider.js +1 -1
  39. package/dist/react/ApolloProvider.js.map +1 -1
  40. package/dist/react/index.d.ts +4 -0
  41. package/dist/react/index.d.ts.map +1 -1
  42. package/dist/react/index.js +3 -0
  43. package/dist/react/index.js.map +1 -1
  44. package/dist/react/index.mcp.d.ts +1 -1
  45. package/dist/react/index.mcp.d.ts.map +1 -1
  46. package/dist/react/index.mcp.js +1 -1
  47. package/dist/react/index.mcp.js.map +1 -1
  48. package/dist/react/index.openai.d.ts +1 -1
  49. package/dist/react/index.openai.d.ts.map +1 -1
  50. package/dist/react/index.openai.js +1 -1
  51. package/dist/react/index.openai.js.map +1 -1
  52. package/dist/react/reactive.d.ts +9 -0
  53. package/dist/react/reactive.d.ts.map +1 -0
  54. package/dist/react/reactive.js +11 -0
  55. package/dist/react/reactive.js.map +1 -0
  56. package/dist/utilities/getToolNamesFromDocument.d.ts +3 -0
  57. package/dist/utilities/getToolNamesFromDocument.d.ts.map +1 -0
  58. package/dist/utilities/getToolNamesFromDocument.js +12 -0
  59. package/dist/utilities/getToolNamesFromDocument.js.map +1 -0
  60. package/dist/utilities/getVariableNamesFromDocument.d.ts +3 -0
  61. package/dist/utilities/getVariableNamesFromDocument.d.ts.map +1 -0
  62. package/dist/utilities/getVariableNamesFromDocument.js +6 -0
  63. package/dist/utilities/getVariableNamesFromDocument.js.map +1 -0
  64. package/dist/utilities/index.d.ts +3 -0
  65. package/dist/utilities/index.d.ts.map +1 -1
  66. package/dist/utilities/index.js +3 -0
  67. package/dist/utilities/index.js.map +1 -1
  68. package/dist/utilities/warnOnVariableMismatch.d.ts +3 -0
  69. package/dist/utilities/warnOnVariableMismatch.d.ts.map +1 -0
  70. package/dist/utilities/warnOnVariableMismatch.js +10 -0
  71. package/dist/utilities/warnOnVariableMismatch.js.map +1 -0
  72. package/package.json +2 -1
  73. package/src/core/ApolloClient.ts +1 -1
  74. package/src/mcp/core/ApolloClient.ts +67 -2
  75. package/src/mcp/core/McpAppManager.ts +3 -3
  76. package/src/mcp/core/__tests__/ApolloClient.test.ts +109 -6
  77. package/src/mcp/link/__tests__/ToolCallLink.test.ts +1 -1
  78. package/src/mcp/react/hooks/__tests__/createHydrationUtils.test.tsx +1228 -0
  79. package/src/mcp/react/hooks/createHydrationUtils.ts +182 -0
  80. package/src/mcp/react/index.ts +1 -0
  81. package/src/openai/core/ApolloClient.ts +68 -2
  82. package/src/openai/core/McpAppManager.ts +5 -5
  83. package/src/openai/core/__tests__/ApolloClient.test.ts +113 -7
  84. package/src/openai/react/hooks/__tests__/createHydrationUtils.test.tsx +1333 -0
  85. package/src/openai/react/hooks/createHydrationUtils.ts +182 -0
  86. package/src/openai/react/index.ts +1 -0
  87. package/src/react/ApolloProvider.tsx +1 -1
  88. package/src/react/index.mcp.ts +1 -0
  89. package/src/react/index.openai.ts +1 -0
  90. package/src/react/index.ts +7 -0
  91. package/src/react/reactive.ts +19 -0
  92. package/src/testing/internal/mcp/graphqlToolResult.ts +5 -5
  93. package/src/utilities/getToolNamesFromDocument.ts +15 -0
  94. package/src/utilities/getVariableNamesFromDocument.ts +9 -0
  95. package/src/utilities/index.ts +3 -0
  96. package/src/utilities/warnOnVariableMismatch.ts +20 -0
@@ -0,0 +1,182 @@
1
+ import { useState, useCallback, useRef, useMemo, useLayoutEffect } from "react";
2
+ import type {
3
+ DocumentNode,
4
+ OperationVariables,
5
+ TypedDocumentNode,
6
+ } from "@apollo/client";
7
+ import { useApolloClient } from "./useApolloClient.js";
8
+ import { useToolName } from "./useToolName.js";
9
+ import { isReactive } from "../../../react/reactive.js";
10
+ import type { Reactive } from "../../../react/reactive.js";
11
+ import { equal } from "@wry/equality";
12
+ import { __DEV__ } from "@apollo/client/utilities/environment";
13
+ import {
14
+ getToolNamesFromDocument,
15
+ getVariableNamesFromDocument,
16
+ } from "../../../utilities/index.js";
17
+
18
+ type HydratedVariablesInput<TVariables> = {
19
+ [K in keyof TVariables]: TVariables[K] | Reactive<TVariables[K]>;
20
+ };
21
+
22
+ type StateVariables<TVariables, Input> = {
23
+ [K in keyof TVariables as K extends keyof Input ?
24
+ Input[K] extends Reactive<any> ?
25
+ never
26
+ : K
27
+ : K]: K extends keyof TVariables ? TVariables[K] : never;
28
+ };
29
+
30
+ type SetVariables<TState> = (
31
+ update: Partial<TState> | ((prev: TState) => Partial<TState>)
32
+ ) => void;
33
+
34
+ /** @experimental */
35
+ export function createHydrationUtils<
36
+ TVariables extends OperationVariables = OperationVariables,
37
+ >(document: TypedDocumentNode<any, TVariables> | DocumentNode) {
38
+ const documentToolNames = getToolNamesFromDocument(document);
39
+ const variableNames = getVariableNamesFromDocument(document);
40
+
41
+ function useHydratedVariables<
42
+ TInputVariables extends HydratedVariablesInput<TVariables>,
43
+ >(
44
+ variables: TInputVariables &
45
+ Record<Exclude<keyof TInputVariables, keyof TVariables>, never>
46
+ ): [
47
+ variables: TVariables,
48
+ setVariables: SetVariables<StateVariables<TVariables, TInputVariables>>,
49
+ ] {
50
+ const client = useApolloClient();
51
+ const toolName = useToolName();
52
+ const [toolInput] = useState(() => client.toolInput);
53
+
54
+ const toolMatches =
55
+ toolInput !== undefined &&
56
+ toolName !== undefined &&
57
+ documentToolNames.has(toolName);
58
+
59
+ const [stateVars, setStateVars] = useState<Record<string, unknown>>(() => {
60
+ const values: Record<string, unknown> = {};
61
+
62
+ for (const [key, value] of Object.entries(
63
+ toolMatches ? toolInput : variables
64
+ )) {
65
+ if (variableNames.has(key) && !isReactive(value)) {
66
+ values[key] = value;
67
+ }
68
+ }
69
+
70
+ return values;
71
+ });
72
+
73
+ const [initialReactiveValues] = useState<Record<string, unknown>>(() => {
74
+ const values: Record<string, unknown> = {};
75
+
76
+ for (const [key, value] of Object.entries(variables)) {
77
+ if (variableNames.has(key) && isReactive(value)) {
78
+ values[key] = value.value;
79
+ }
80
+ }
81
+
82
+ return values;
83
+ });
84
+
85
+ const [reactiveVars, setReactiveVars] = useState(() => {
86
+ const values: Record<string, unknown> = {};
87
+
88
+ for (const [key, value] of Object.entries(initialReactiveValues)) {
89
+ if (toolMatches && key in toolInput) {
90
+ values[key] = toolInput[key];
91
+ } else if (!toolMatches) {
92
+ values[key] = value;
93
+ }
94
+ }
95
+
96
+ return values;
97
+ });
98
+
99
+ const changedKeysRef = useRef(new Set<string>());
100
+ const nextReactiveVars: Record<string, unknown> = {};
101
+
102
+ for (const [key, value] of Object.entries(variables)) {
103
+ if (!variableNames.has(key) || !isReactive(value)) continue;
104
+
105
+ const hasChanged =
106
+ changedKeysRef.current.has(key) ||
107
+ !equal(value.value, initialReactiveValues[key]);
108
+
109
+ if (toolMatches && !hasChanged) {
110
+ if (key in toolInput) {
111
+ nextReactiveVars[key] = toolInput[key];
112
+ }
113
+ } else {
114
+ nextReactiveVars[key] = value.value;
115
+ }
116
+ }
117
+
118
+ if (!equal(nextReactiveVars, reactiveVars)) {
119
+ setReactiveVars(nextReactiveVars);
120
+ }
121
+
122
+ // Clear the tool input after first mount so that remounting the component
123
+ // uses the user-provided variables rather than the hydrated tool input.
124
+ // This runs once on mount; watchQuery also clears it when useQuery is
125
+ // present, so both paths are idempotent.
126
+ useLayoutEffect(() => {
127
+ if (toolMatches) {
128
+ client.clearToolInput();
129
+ }
130
+ // eslint-disable-next-line react-hooks/exhaustive-deps
131
+ }, []);
132
+
133
+ useLayoutEffect(() => {
134
+ for (const [key, value] of Object.entries(variables)) {
135
+ if (
136
+ variableNames.has(key) &&
137
+ isReactive(value) &&
138
+ !changedKeysRef.current.has(key) &&
139
+ !equal(value.value, initialReactiveValues[key])
140
+ ) {
141
+ changedKeysRef.current.add(key);
142
+ }
143
+ }
144
+ });
145
+
146
+ const resolvedVariables = useMemo(() => {
147
+ return { ...stateVars, ...reactiveVars } as TVariables;
148
+ }, [stateVars, reactiveVars]);
149
+
150
+ const setVariables = useCallback<
151
+ SetVariables<StateVariables<TVariables, TInputVariables>>
152
+ >((update) => {
153
+ setStateVars((prev) => {
154
+ const updates =
155
+ typeof update === "function" ? update(prev as any) : update;
156
+
157
+ const filtered = Object.fromEntries(
158
+ Object.entries(updates).filter(([key]) => {
159
+ if (key in initialReactiveValues) {
160
+ if (__DEV__) {
161
+ console.warn(
162
+ `Attempted to set reactive variable "${key}" via setVariables. ` +
163
+ `Reactive variables are read-only and are ignored. `
164
+ );
165
+ }
166
+ return false;
167
+ }
168
+ return true;
169
+ })
170
+ );
171
+
172
+ if (Object.keys(filtered).length === 0) return prev;
173
+
174
+ return { ...prev, ...filtered };
175
+ });
176
+ }, []);
177
+
178
+ return [resolvedVariables, setVariables];
179
+ }
180
+
181
+ return { useHydratedVariables };
182
+ }
@@ -2,4 +2,5 @@ export { useApp } from "./hooks/useApp.js";
2
2
  export { useToolName } from "./hooks/useToolName.js";
3
3
  export { useToolMetadata } from "./hooks/useToolMetadata.js";
4
4
  export { useToolInput } from "./hooks/useToolInput.js";
5
+ export { createHydrationUtils } from "./hooks/createHydrationUtils.js";
5
6
  export { useWidgetState } from "./hooks/useWidgetState.js";
@@ -25,7 +25,7 @@ export function ApolloProvider({ children, client }: ApolloProvider.Props) {
25
25
  );
26
26
  }
27
27
 
28
- use(client.waitForInitialization());
28
+ use(client.connect());
29
29
 
30
30
  return (
31
31
  <BaseApolloProvider client={client as BaseApolloClient}>
@@ -2,6 +2,7 @@ export * from "./index.js";
2
2
  export {
3
3
  useApp,
4
4
  useToolInput,
5
+ createHydrationUtils,
5
6
  useToolMetadata,
6
7
  useToolName,
7
8
  } from "../mcp/react/index.js";
@@ -2,6 +2,7 @@ export * from "./index.js";
2
2
  export {
3
3
  useApp,
4
4
  useToolInput,
5
+ createHydrationUtils,
5
6
  useToolMetadata,
6
7
  useToolName,
7
8
  } from "../openai/react/index.js";
@@ -1,6 +1,8 @@
1
1
  import { missingHook } from "./missingHook.js";
2
2
 
3
3
  export { ApolloProvider } from "./ApolloProvider.js";
4
+ export { reactive } from "./reactive.js";
5
+ export type { Reactive } from "./reactive.js";
4
6
 
5
7
  // Use `mcp` related types since these are the most common between the two
6
8
  // targets
@@ -17,3 +19,8 @@ export const useToolMetadata =
17
19
 
18
20
  export const useToolName =
19
21
  missingHook<typeof import("./index.mcp.js").useToolName>("useToolName");
22
+
23
+ /** @experimental */
24
+ export const createHydrationUtils = missingHook<
25
+ typeof import("./index.mcp.js").createHydrationUtils
26
+ >("createHydrationUtils");
@@ -0,0 +1,19 @@
1
+ const REACTIVE = Symbol("apollo.reactive");
2
+
3
+ export interface Reactive<T> {
4
+ readonly [REACTIVE]: true;
5
+ readonly value: T;
6
+ }
7
+
8
+ export function reactive<T>(value: T): Reactive<T> {
9
+ return { [REACTIVE]: true, value };
10
+ }
11
+
12
+ export function isReactive(value: unknown): value is Reactive<unknown> {
13
+ return (
14
+ typeof value === "object" &&
15
+ value !== null &&
16
+ REACTIVE in value &&
17
+ (value as Record<symbol, unknown>)[REACTIVE] === true
18
+ );
19
+ }
@@ -2,20 +2,20 @@ import type { McpUiToolResultNotification } from "@modelcontextprotocol/ext-apps
2
2
  import type { FormattedExecutionResult } from "graphql";
3
3
  import type { ApolloMcpServerApps } from "../../../core/types";
4
4
 
5
- export function graphqlToolResult(
5
+ export function graphqlToolResult<TData = Record<string, unknown>>(
6
6
  options:
7
- | FormattedExecutionResult
7
+ | FormattedExecutionResult<TData>
8
8
  | {
9
9
  prefetch?: Record<string, FormattedExecutionResult>;
10
- result: FormattedExecutionResult;
10
+ result: FormattedExecutionResult<TData>;
11
11
  }
12
12
  ): McpUiToolResultNotification["params"] {
13
13
  let structuredContent: ApolloMcpServerApps.StructuredContent;
14
14
 
15
15
  if ("data" in options) {
16
- structuredContent = { result: options };
16
+ structuredContent = { result: options as FormattedExecutionResult };
17
17
  } else if ("result" in options) {
18
- structuredContent = { result: options.result };
18
+ structuredContent = { result: options.result as FormattedExecutionResult };
19
19
 
20
20
  if (options.prefetch) {
21
21
  structuredContent.prefetch = options.prefetch;
@@ -0,0 +1,15 @@
1
+ import { getOperationDefinition } from "@apollo/client/utilities/internal";
2
+ import { Kind, type DocumentNode } from "graphql";
3
+
4
+ export function getToolNamesFromDocument(document: DocumentNode) {
5
+ const operationDef = getOperationDefinition(document);
6
+
7
+ return new Set(
8
+ operationDef?.directives
9
+ ?.filter((d) => d.name.value === "tool")
10
+ .flatMap((d) => {
11
+ const nameArg = d.arguments?.find((arg) => arg.name.value === "name");
12
+ return nameArg?.value.kind === Kind.STRING ? [nameArg.value.value] : [];
13
+ })
14
+ );
15
+ }
@@ -0,0 +1,9 @@
1
+ import { getOperationDefinition } from "@apollo/client/utilities/internal";
2
+ import type { DocumentNode } from "graphql";
3
+
4
+ export function getVariableNamesFromDocument(document: DocumentNode) {
5
+ const operationDef = getOperationDefinition(document);
6
+ return new Set(
7
+ operationDef?.variableDefinitions?.map((v) => v.variable.name.value) ?? []
8
+ );
9
+ }
@@ -1,5 +1,8 @@
1
1
  export { aiClientSymbol } from "./constants.js";
2
2
  export { getVariablesForOperationFromToolInput } from "./getVariablesForOperationFromToolInput.js";
3
+ export { getToolNamesFromDocument } from "./getToolNamesFromDocument.js";
4
+ export { getVariableNamesFromDocument } from "./getVariableNamesFromDocument.js";
3
5
  export { cacheAsync } from "./cacheAsync.js";
4
6
  export { invariant } from "./invariant.js";
5
7
  export { promiseWithResolvers } from "./promiseWithResolvers.js";
8
+ export { warnOnVariableMismatch } from "./warnOnVariableMismatch.js";
@@ -0,0 +1,20 @@
1
+ import type { DocumentNode, OperationVariables } from "@apollo/client";
2
+ import { getOperationName } from "@apollo/client/utilities/internal/internal.cjs";
3
+
4
+ export function warnOnVariableMismatch(
5
+ document: DocumentNode,
6
+ toolInput: OperationVariables,
7
+ actualVariables: OperationVariables | undefined
8
+ ) {
9
+ const operationName = getOperationName(document, "(anonymous)");
10
+
11
+ console.warn(
12
+ `The operation "${operationName}" has a @tool directive matching the current ` +
13
+ "tool call, but the variables provided to the query don't match the tool " +
14
+ "input. Use the `useHydratedVariables` hook returned from `createHydrationUtils` " +
15
+ "to provide the hydrated variables to the query. " +
16
+ "\n\nExpected variables:\n%o\n\nReceived:\n%o",
17
+ toolInput,
18
+ actualVariables
19
+ );
20
+ }