@apollo/client-ai-apps 0.5.1 → 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.
- package/CHANGELOG.md +45 -0
- package/dist/core/ApolloClient.d.ts +1 -1
- package/dist/core/ApolloClient.d.ts.map +1 -1
- package/dist/core/ApolloClient.js +1 -1
- package/dist/core/ApolloClient.js.map +1 -1
- package/dist/core/types.d.ts +6 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/mcp/core/ApolloClient.d.ts +6 -1
- package/dist/mcp/core/ApolloClient.d.ts.map +1 -1
- package/dist/mcp/core/ApolloClient.js +36 -3
- package/dist/mcp/core/ApolloClient.js.map +1 -1
- package/dist/mcp/core/McpAppManager.d.ts +4 -9
- package/dist/mcp/core/McpAppManager.d.ts.map +1 -1
- package/dist/mcp/core/McpAppManager.js +13 -4
- package/dist/mcp/core/McpAppManager.js.map +1 -1
- package/dist/mcp/react/hooks/createHydrationUtils.d.ts +15 -0
- package/dist/mcp/react/hooks/createHydrationUtils.d.ts.map +1 -0
- package/dist/mcp/react/hooks/createHydrationUtils.js +113 -0
- package/dist/mcp/react/hooks/createHydrationUtils.js.map +1 -0
- package/dist/mcp/react/hooks/useToolMetadata.d.ts +1 -7
- package/dist/mcp/react/hooks/useToolMetadata.d.ts.map +1 -1
- package/dist/mcp/react/index.d.ts +1 -0
- package/dist/mcp/react/index.d.ts.map +1 -1
- package/dist/mcp/react/index.js +1 -0
- package/dist/mcp/react/index.js.map +1 -1
- package/dist/openai/core/ApolloClient.d.ts +6 -1
- package/dist/openai/core/ApolloClient.d.ts.map +1 -1
- package/dist/openai/core/ApolloClient.js +37 -3
- package/dist/openai/core/ApolloClient.js.map +1 -1
- package/dist/openai/core/McpAppManager.d.ts +2 -2
- package/dist/openai/core/McpAppManager.d.ts.map +1 -1
- package/dist/openai/core/McpAppManager.js +16 -18
- package/dist/openai/core/McpAppManager.js.map +1 -1
- package/dist/openai/react/hooks/createHydrationUtils.d.ts +15 -0
- package/dist/openai/react/hooks/createHydrationUtils.d.ts.map +1 -0
- package/dist/openai/react/hooks/createHydrationUtils.js +113 -0
- package/dist/openai/react/hooks/createHydrationUtils.js.map +1 -0
- package/dist/openai/react/index.d.ts +1 -0
- package/dist/openai/react/index.d.ts.map +1 -1
- package/dist/openai/react/index.js +1 -0
- package/dist/openai/react/index.js.map +1 -1
- package/dist/react/ApolloProvider.js +1 -1
- package/dist/react/ApolloProvider.js.map +1 -1
- package/dist/react/index.d.ts +4 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +3 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mcp.d.ts +1 -1
- package/dist/react/index.mcp.d.ts.map +1 -1
- package/dist/react/index.mcp.js +1 -1
- package/dist/react/index.mcp.js.map +1 -1
- package/dist/react/index.openai.d.ts +1 -1
- package/dist/react/index.openai.d.ts.map +1 -1
- package/dist/react/index.openai.js +1 -1
- package/dist/react/index.openai.js.map +1 -1
- package/dist/react/reactive.d.ts +9 -0
- package/dist/react/reactive.d.ts.map +1 -0
- package/dist/react/reactive.js +11 -0
- package/dist/react/reactive.js.map +1 -0
- package/dist/utilities/getToolNamesFromDocument.d.ts +3 -0
- package/dist/utilities/getToolNamesFromDocument.d.ts.map +1 -0
- package/dist/utilities/getToolNamesFromDocument.js +12 -0
- package/dist/utilities/getToolNamesFromDocument.js.map +1 -0
- package/dist/utilities/getVariableNamesFromDocument.d.ts +3 -0
- package/dist/utilities/getVariableNamesFromDocument.d.ts.map +1 -0
- package/dist/utilities/getVariableNamesFromDocument.js +6 -0
- package/dist/utilities/getVariableNamesFromDocument.js.map +1 -0
- package/dist/utilities/index.d.ts +3 -0
- package/dist/utilities/index.d.ts.map +1 -1
- package/dist/utilities/index.js +3 -0
- package/dist/utilities/index.js.map +1 -1
- package/dist/utilities/warnOnVariableMismatch.d.ts +3 -0
- package/dist/utilities/warnOnVariableMismatch.d.ts.map +1 -0
- package/dist/utilities/warnOnVariableMismatch.js +10 -0
- package/dist/utilities/warnOnVariableMismatch.js.map +1 -0
- package/package.json +2 -1
- package/src/core/ApolloClient.ts +1 -1
- package/src/core/types.ts +7 -0
- package/src/mcp/core/ApolloClient.ts +67 -2
- package/src/mcp/core/McpAppManager.ts +14 -5
- package/src/mcp/core/__tests__/ApolloClient.test.ts +109 -6
- package/src/mcp/link/__tests__/ToolCallLink.test.ts +13 -4
- package/src/mcp/react/hooks/__tests__/createHydrationUtils.test.tsx +1228 -0
- package/src/mcp/react/hooks/createHydrationUtils.ts +182 -0
- package/src/mcp/react/index.ts +1 -0
- package/src/openai/core/ApolloClient.ts +68 -2
- package/src/openai/core/McpAppManager.ts +17 -20
- package/src/openai/core/__tests__/ApolloClient.test.ts +117 -11
- package/src/openai/react/hooks/__tests__/createHydrationUtils.test.tsx +1333 -0
- package/src/openai/react/hooks/__tests__/useToolInput.test.tsx +1 -1
- package/src/openai/react/hooks/createHydrationUtils.ts +182 -0
- package/src/openai/react/index.ts +1 -0
- package/src/react/ApolloProvider.tsx +1 -1
- package/src/react/index.mcp.ts +1 -0
- package/src/react/index.openai.ts +1 -0
- package/src/react/index.ts +7 -0
- package/src/react/reactive.ts +19 -0
- package/src/testing/internal/mcp/graphqlToolResult.ts +5 -5
- package/src/utilities/getToolNamesFromDocument.ts +15 -0
- package/src/utilities/getVariableNamesFromDocument.ts +9 -0
- package/src/utilities/index.ts +3 -0
- package/src/utilities/warnOnVariableMismatch.ts +20 -0
|
@@ -2,15 +2,18 @@ import { ApolloClient as BaseApolloClient } from "@apollo/client";
|
|
|
2
2
|
import { DocumentTransform } from "@apollo/client";
|
|
3
3
|
import { removeDirectivesFromDocument } from "@apollo/client/utilities/internal";
|
|
4
4
|
import { parse } from "graphql";
|
|
5
|
+
import { equal } from "@wry/equality";
|
|
5
6
|
import { __DEV__ } from "@apollo/client/utilities/environment";
|
|
6
7
|
import { ToolCallLink } from "../link/ToolCallLink.js";
|
|
7
|
-
import { aiClientSymbol, cacheAsync, getVariablesForOperationFromToolInput, } from "../../utilities/index.js";
|
|
8
|
+
import { aiClientSymbol, cacheAsync, getToolNamesFromDocument, getVariablesForOperationFromToolInput, warnOnVariableMismatch, } from "../../utilities/index.js";
|
|
8
9
|
import { McpAppManager } from "./McpAppManager.js";
|
|
10
|
+
import { getVariableNamesFromDocument } from "../../utilities/getVariableNamesFromDocument.js";
|
|
9
11
|
export class ApolloClient extends BaseApolloClient {
|
|
10
12
|
manifest;
|
|
11
13
|
appManager;
|
|
12
14
|
/** @internal */
|
|
13
15
|
[aiClientSymbol] = true;
|
|
16
|
+
#toolInput;
|
|
14
17
|
constructor(options) {
|
|
15
18
|
const link = options.link ?? new ToolCallLink();
|
|
16
19
|
if (__DEV__) {
|
|
@@ -31,8 +34,39 @@ export class ApolloClient extends BaseApolloClient {
|
|
|
31
34
|
super.stop();
|
|
32
35
|
this.appManager.close().catch(() => { });
|
|
33
36
|
}
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
get toolInput() {
|
|
38
|
+
return this.#toolInput;
|
|
39
|
+
}
|
|
40
|
+
clearToolInput() {
|
|
41
|
+
this.#toolInput = undefined;
|
|
42
|
+
}
|
|
43
|
+
watchQuery(options) {
|
|
44
|
+
if (__DEV__) {
|
|
45
|
+
const toolInput = this.#toolInput;
|
|
46
|
+
if (toolInput) {
|
|
47
|
+
const toolName = this.appManager.toolName;
|
|
48
|
+
const hasMatchingTool = !!toolName && getToolNamesFromDocument(options.query).has(toolName);
|
|
49
|
+
if (hasMatchingTool) {
|
|
50
|
+
// Clear after first matching comparison so this only fires once and
|
|
51
|
+
// remounting doesn't produce spurious warnings.
|
|
52
|
+
this.#toolInput = undefined;
|
|
53
|
+
const variableNames = getVariableNamesFromDocument(options.query);
|
|
54
|
+
if (variableNames.size > 0) {
|
|
55
|
+
const { variables } = options;
|
|
56
|
+
const toolInputVariables = Object.entries(toolInput).filter(([key]) => variableNames.has(key));
|
|
57
|
+
const hasToolInputMismatch = toolInputVariables.some(([key, value]) => !equal(value, variables?.[key]));
|
|
58
|
+
if (hasToolInputMismatch) {
|
|
59
|
+
warnOnVariableMismatch(options.query, Object.fromEntries(toolInputVariables), variables);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return super.watchQuery(options);
|
|
66
|
+
}
|
|
67
|
+
connect = cacheAsync(async () => {
|
|
68
|
+
const { prefetch, result, toolName, args } = await this.appManager.connect();
|
|
69
|
+
this.#toolInput = args;
|
|
36
70
|
this.manifest.operations.forEach((operation) => {
|
|
37
71
|
if (operation.prefetchID && prefetch?.[operation.prefetchID]) {
|
|
38
72
|
this.writeQuery({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApolloClient.js","sourceRoot":"","sources":["../../../src/openai/core/ApolloClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,IAAI,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"ApolloClient.js","sourceRoot":"","sources":["../../../src/openai/core/ApolloClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,IAAI,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAMnD,OAAO,EAAE,4BAA4B,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,sCAAsC,CAAC;AAE/D,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EACL,cAAc,EACd,UAAU,EACV,wBAAwB,EACxB,qCAAqC,EACrC,sBAAsB,GACvB,MAAM,0BAA0B,CA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,MAAM,IAAI,KAAK,CACb,0JAA0J,CAC3J,CAAC;IACJ,CAAC;AACH,CAAC;AAED,SAAS,WAAW,CAAC,IAAgB;IACnC,OAAO,MAAM,IAAI,IAAI,CAAC;AACxB,CAAC","sourcesContent":["import type { ApolloLink } from \"@apollo/client\";\nimport { ApolloClient as BaseApolloClient } from \"@apollo/client\";\nimport { DocumentTransform } from \"@apollo/client\";\nimport type {\n WatchQueryOptions,\n ObservableQuery,\n OperationVariables,\n} from \"@apollo/client\";\nimport { removeDirectivesFromDocument } from \"@apollo/client/utilities/internal\";\nimport { parse } from \"graphql\";\nimport { equal } from \"@wry/equality\";\nimport { __DEV__ } from \"@apollo/client/utilities/environment\";\nimport type { ApplicationManifest } from \"../../types/application-manifest.js\";\nimport { ToolCallLink } from \"../link/ToolCallLink.js\";\nimport {\n aiClientSymbol,\n cacheAsync,\n getToolNamesFromDocument,\n getVariablesForOperationFromToolInput,\n warnOnVariableMismatch,\n} from \"../../utilities/index.js\";\nimport { McpAppManager } from \"./McpAppManager.js\";\nimport { getVariableNamesFromDocument } from \"../../utilities/getVariableNamesFromDocument.js\";\n\nexport declare namespace ApolloClient {\n export interface Options extends Omit<BaseApolloClient.Options, \"link\"> {\n link?: BaseApolloClient.Options[\"link\"];\n manifest: ApplicationManifest;\n }\n}\n\nexport class ApolloClient extends BaseApolloClient {\n manifest: ApplicationManifest;\n private readonly appManager: McpAppManager;\n\n /** @internal */\n readonly [aiClientSymbol] = true;\n\n #toolInput: Record<string, unknown> | undefined;\n\n constructor(options: ApolloClient.Options) {\n const link = options.link ?? new ToolCallLink();\n\n if (__DEV__) {\n validateTerminatingLink(link);\n }\n\n super({\n ...options,\n link,\n // Strip out the prefetch/tool directives so they don't get sent with the operation to the server\n documentTransform: new DocumentTransform((document) => {\n return removeDirectivesFromDocument(\n [{ name: \"prefetch\" }, { name: \"tool\" }],\n document\n )!;\n }).concat(options.documentTransform ?? DocumentTransform.identity()),\n });\n\n this.manifest = options.manifest;\n this.appManager = new McpAppManager(this.manifest);\n }\n\n stop() {\n super.stop();\n this.appManager.close().catch(() => {});\n }\n\n get toolInput() {\n return this.#toolInput;\n }\n\n clearToolInput() {\n this.#toolInput = undefined;\n }\n\n watchQuery<\n T = any,\n TVariables extends OperationVariables = OperationVariables,\n >(options: WatchQueryOptions<TVariables, T>): ObservableQuery<T, TVariables> {\n if (__DEV__) {\n const toolInput = this.#toolInput;\n\n if (toolInput) {\n const toolName = this.appManager.toolName;\n const hasMatchingTool =\n !!toolName && getToolNamesFromDocument(options.query).has(toolName);\n\n if (hasMatchingTool) {\n // Clear after first matching comparison so this only fires once and\n // remounting doesn't produce spurious warnings.\n this.#toolInput = undefined;\n\n const variableNames = getVariableNamesFromDocument(options.query);\n\n if (variableNames.size > 0) {\n const { variables } = options;\n\n const toolInputVariables = Object.entries(toolInput).filter(\n ([key]) => variableNames.has(key)\n );\n\n const hasToolInputMismatch = toolInputVariables.some(\n ([key, value]) => !equal(value, variables?.[key])\n );\n\n if (hasToolInputMismatch) {\n warnOnVariableMismatch(\n options.query,\n Object.fromEntries(toolInputVariables),\n variables\n );\n }\n }\n }\n }\n }\n\n return super.watchQuery(options);\n }\n\n connect = cacheAsync(async () => {\n const { prefetch, result, toolName, args } =\n await this.appManager.connect();\n\n this.#toolInput = args;\n\n this.manifest.operations.forEach((operation) => {\n if (operation.prefetchID && prefetch?.[operation.prefetchID]) {\n this.writeQuery({\n query: parse(operation.body),\n data: prefetch[operation.prefetchID].data,\n });\n }\n\n if (operation.tools.find((tool) => tool.name === toolName)) {\n this.writeQuery({\n query: parse(operation.body),\n data: result.data,\n variables: getVariablesForOperationFromToolInput(operation, args),\n });\n }\n });\n });\n}\n\nfunction validateTerminatingLink(link: ApolloLink) {\n let terminatingLink = link;\n\n while (terminatingLink.right) {\n terminatingLink = terminatingLink.right;\n }\n\n if (\n !isNamedLink(terminatingLink) ||\n terminatingLink.name !== \"ToolCallLink\"\n ) {\n throw new Error(\n \"The terminating link must be a `ToolCallLink`. If you are using a `split` link, ensure the `right` branch uses a `ToolCallLink` as the terminating link.\"\n );\n }\n}\n\nfunction isNamedLink(link: ApolloLink): link is ApolloLink & { name: string } {\n return \"name\" in link;\n}\n"]}
|
|
@@ -10,7 +10,7 @@ export declare class McpAppManager {
|
|
|
10
10
|
get toolName(): string | undefined;
|
|
11
11
|
get toolMetadata(): Record<string, unknown> | null;
|
|
12
12
|
get toolInput(): Record<string, unknown> | undefined;
|
|
13
|
-
|
|
13
|
+
connect: (() => Promise<{
|
|
14
14
|
toolName: string | undefined;
|
|
15
15
|
args: Record<string, unknown> | undefined;
|
|
16
16
|
result: FormattedExecutionResult;
|
|
@@ -23,6 +23,6 @@ export declare class McpAppManager {
|
|
|
23
23
|
query: DocumentNode;
|
|
24
24
|
variables: OperationVariables | undefined;
|
|
25
25
|
}): Promise<FormattedExecutionResult<import("graphql/jsutils/ObjMap").ObjMap<unknown>, import("graphql/jsutils/ObjMap").ObjMap<unknown>>>;
|
|
26
|
-
private
|
|
26
|
+
private connectToHost;
|
|
27
27
|
}
|
|
28
28
|
//# sourceMappingURL=McpAppManager.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"McpAppManager.d.ts","sourceRoot":"","sources":["../../../src/openai/core/McpAppManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAwB,MAAM,gCAAgC,CAAC;AAE3E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAC5E,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAC;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AASvE,gBAAgB;AAChB,qBAAa,aAAa;;IACxB,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;gBAMN,QAAQ,EAAE,mBAAmB;IAIzC,IAAI,QAAQ,uBAEX;IAED,IAAI,YAAY,mCAEf;IAED,IAAI,SAAS,wCAEZ;IAED,
|
|
1
|
+
{"version":3,"file":"McpAppManager.d.ts","sourceRoot":"","sources":["../../../src/openai/core/McpAppManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAwB,MAAM,gCAAgC,CAAC;AAE3E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAC5E,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAC;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AASvE,gBAAgB;AAChB,qBAAa,aAAa;;IACxB,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;gBAMN,QAAQ,EAAE,mBAAmB;IAIzC,IAAI,QAAQ,uBAEX;IAED,IAAI,YAAY,mCAEf;IAED,IAAI,SAAS,wCAEZ;IAED,OAAO;;;;;;;MAqCJ;IAEH,KAAK;IAIC,YAAY,CAAC,EACjB,KAAK,EACL,SAAS,GACV,EAAE;QACD,KAAK,EAAE,YAAY,CAAC;QACpB,SAAS,EAAE,kBAAkB,GAAG,SAAS,CAAC;KAC3C;YASa,aAAa;CAW5B"}
|
|
@@ -19,35 +19,33 @@ export class McpAppManager {
|
|
|
19
19
|
get toolInput() {
|
|
20
20
|
return this.#toolInput;
|
|
21
21
|
}
|
|
22
|
-
|
|
22
|
+
connect = cacheAsync(async () => {
|
|
23
23
|
let toolResult = promiseWithResolvers();
|
|
24
|
-
let toolInput = promiseWithResolvers();
|
|
25
24
|
this.app.ontoolresult = (params) => {
|
|
26
25
|
toolResult.resolve(params);
|
|
27
|
-
// OpenAI is not consistent about sending `ui/notifications/tool-input`
|
|
28
|
-
// before we get the rool result (which should happen according to the
|
|
29
|
-
// spec). We resolve this promise in case it wasn't sent to avoid stalling
|
|
30
|
-
// initialization indefinitely.
|
|
31
|
-
//
|
|
32
|
-
// When OpenAI fixes this issue and sends `ui/notifications/tool-input`
|
|
33
|
-
// consistently, this can be removed.
|
|
34
|
-
toolInput.resolve({});
|
|
35
26
|
};
|
|
36
|
-
this.
|
|
37
|
-
toolInput.resolve(params);
|
|
38
|
-
};
|
|
39
|
-
await this.connect();
|
|
27
|
+
await this.connectToHost();
|
|
40
28
|
const { structuredContent } = await toolResult.promise;
|
|
41
|
-
const { arguments: args } = await toolInput.promise;
|
|
42
29
|
this.#toolName = this.app.getHostContext()?.toolInfo?.tool.name;
|
|
43
|
-
|
|
30
|
+
// OpenAI is not consistent about sending `ui/notifications/tool-input`.
|
|
31
|
+
// Sometimes it doesn't send at all, other times it sends more than once
|
|
32
|
+
// before we get the tool result (which should always happen and at most
|
|
33
|
+
// once according to the spec). Rather than relying on the
|
|
34
|
+
// `ui/notifications/tool-input` notification to set the tool input value,
|
|
35
|
+
// we read from `window.openai.toolInput so that we have the most recent
|
|
36
|
+
// set value.
|
|
37
|
+
//
|
|
38
|
+
// When OpenAI fixes this issue and sends `ui/notifications/tool-input`
|
|
39
|
+
// consistently according to the MCP Apps specification, this can be
|
|
40
|
+
// reverted to use the `app.ontoolinput` callback.
|
|
41
|
+
this.#toolInput = window.openai.toolInput;
|
|
44
42
|
// OpenAI doesn't provide access to `_meta`, so we need to use
|
|
45
43
|
// window.openai.toolResponseMetadata directly
|
|
46
44
|
this.#toolMetadata = window.openai.toolResponseMetadata;
|
|
47
45
|
return {
|
|
48
46
|
...structuredContent,
|
|
49
47
|
toolName: this.toolName,
|
|
50
|
-
args,
|
|
48
|
+
args: this.toolInput,
|
|
51
49
|
};
|
|
52
50
|
});
|
|
53
51
|
close() {
|
|
@@ -60,7 +58,7 @@ export class McpAppManager {
|
|
|
60
58
|
}));
|
|
61
59
|
return result.structuredContent;
|
|
62
60
|
}
|
|
63
|
-
async
|
|
61
|
+
async connectToHost() {
|
|
64
62
|
try {
|
|
65
63
|
return await this.app.connect(new PostMessageTransport(window.parent, window.parent));
|
|
66
64
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"McpAppManager.js","sourceRoot":"","sources":["../../../src/openai/core/McpAppManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AAK3E,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAOnE,gBAAgB;AAChB,MAAM,OAAO,aAAa;IACf,GAAG,CAAM;IAElB,SAAS,CAAqB;IAC9B,aAAa,GAAmC,IAAI,CAAC;IACrD,UAAU,CAAsC;IAEhD,YAAY,QAA6B;QACvC,IAAI,CAAC,GAAG,GAAG,IAAI,GAAG,CAAC,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,EAAE,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC;IAC5E,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,IAAI,YAAY;QACd,OAAO,IAAI,CAAC,aAAa,CAAC;IAC5B,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAED,
|
|
1
|
+
{"version":3,"file":"McpAppManager.js","sourceRoot":"","sources":["../../../src/openai/core/McpAppManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AAK3E,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAOnE,gBAAgB;AAChB,MAAM,OAAO,aAAa;IACf,GAAG,CAAM;IAElB,SAAS,CAAqB;IAC9B,aAAa,GAAmC,IAAI,CAAC;IACrD,UAAU,CAAsC;IAEhD,YAAY,QAA6B;QACvC,IAAI,CAAC,GAAG,GAAG,IAAI,GAAG,CAAC,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE,O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AC;IAC1B,CAAC;IAED,KAAK,CAAC,YAAY,CAAC,EACjB,KAAK,EACL,SAAS,GAIV;QACC,MAAM,MAAM,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,cAAc,CAAC;YAC5C,IAAI,EAAE,SAAS;YACf,SAAS,EAAE,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE;SAC9C,CAAC,CAA+B,CAAC;QAElC,OAAO,MAAM,CAAC,iBAAiB,CAAC;IAClC,CAAC;IAEO,KAAK,CAAC,aAAa;QACzB,IAAI,CAAC;YACH,OAAO,MAAM,IAAI,CAAC,GAAG,CAAC,OAAO,CAC3B,IAAI,oBAAoB,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CACvD,CAAC;QACJ,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,MAAM,KAAK,GAAG,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;YAEtE,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF","sourcesContent":["import { App, PostMessageTransport } from \"@modelcontextprotocol/ext-apps\";\nimport type { CallToolResult } from \"@modelcontextprotocol/sdk/types.js\";\nimport type { ApplicationManifest } from \"../../types/application-manifest\";\nimport type { FormattedExecutionResult } from \"graphql\";\nimport type { DocumentNode, OperationVariables } from \"@apollo/client\";\nimport { print } from \"@apollo/client/utilities\";\nimport { cacheAsync, promiseWithResolvers } from \"../../utilities\";\nimport type { ApolloMcpServerApps } from \"../../core/types\";\n\ntype ExecuteQueryCallToolResult = Omit<CallToolResult, \"structuredContent\"> & {\n structuredContent: FormattedExecutionResult;\n};\n\n/** @internal */\nexport class McpAppManager {\n readonly app: App;\n\n #toolName: string | undefined;\n #toolMetadata: Record<string, unknown> | null = null;\n #toolInput: Record<string, unknown> | undefined;\n\n constructor(manifest: ApplicationManifest) {\n this.app = new App({ name: manifest.name, version: manifest.appVersion });\n }\n\n get toolName() {\n return this.#toolName;\n }\n\n get toolMetadata() {\n return this.#toolMetadata;\n }\n\n get toolInput() {\n return this.#toolInput;\n }\n\n connect = cacheAsync(async () => {\n let toolResult = promiseWithResolvers<ApolloMcpServerApps.CallToolResult>();\n\n this.app.ontoolresult = (params) => {\n toolResult.resolve(\n params as unknown as ApolloMcpServerApps.CallToolResult\n );\n };\n\n await this.connectToHost();\n\n const { structuredContent } = await toolResult.promise;\n\n this.#toolName = this.app.getHostContext()?.toolInfo?.tool.name;\n\n // OpenAI is not consistent about sending `ui/notifications/tool-input`.\n // Sometimes it doesn't send at all, other times it sends more than once\n // before we get the tool result (which should always happen and at most\n // once according to the spec). Rather than relying on the\n // `ui/notifications/tool-input` notification to set the tool input value,\n // we read from `window.openai.toolInput so that we have the most recent\n // set value.\n //\n // When OpenAI fixes this issue and sends `ui/notifications/tool-input`\n // consistently according to the MCP Apps specification, this can be\n // reverted to use the `app.ontoolinput` callback.\n this.#toolInput = window.openai.toolInput;\n\n // OpenAI doesn't provide access to `_meta`, so we need to use\n // window.openai.toolResponseMetadata directly\n this.#toolMetadata = window.openai.toolResponseMetadata;\n\n return {\n ...structuredContent,\n toolName: this.toolName,\n args: this.toolInput,\n };\n });\n\n close() {\n return this.app.close();\n }\n\n async executeQuery({\n query,\n variables,\n }: {\n query: DocumentNode;\n variables: OperationVariables | undefined;\n }) {\n const result = (await this.app.callServerTool({\n name: \"execute\",\n arguments: { query: print(query), variables },\n })) as ExecuteQueryCallToolResult;\n\n return result.structuredContent;\n }\n\n private async connectToHost() {\n try {\n return await this.app.connect(\n new PostMessageTransport(window.parent, window.parent)\n );\n } catch (e) {\n const error = e instanceof Error ? e : new Error(\"Failed to connect\");\n\n throw error;\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { DocumentNode, OperationVariables, TypedDocumentNode } from "@apollo/client";
|
|
2
|
+
import type { Reactive } from "../../../react/reactive.js";
|
|
3
|
+
type HydratedVariablesInput<TVariables> = {
|
|
4
|
+
[K in keyof TVariables]: TVariables[K] | Reactive<TVariables[K]>;
|
|
5
|
+
};
|
|
6
|
+
type StateVariables<TVariables, Input> = {
|
|
7
|
+
[K in keyof TVariables as K extends keyof Input ? Input[K] extends Reactive<any> ? never : K : K]: K extends keyof TVariables ? TVariables[K] : never;
|
|
8
|
+
};
|
|
9
|
+
type SetVariables<TState> = (update: Partial<TState> | ((prev: TState) => Partial<TState>)) => void;
|
|
10
|
+
/** @experimental */
|
|
11
|
+
export declare function createHydrationUtils<TVariables extends OperationVariables = OperationVariables>(document: TypedDocumentNode<any, TVariables> | DocumentNode): {
|
|
12
|
+
useHydratedVariables: <TInputVariables extends HydratedVariablesInput<TVariables>>(variables: TInputVariables & Record<Exclude<keyof TInputVariables, keyof TVariables>, never>) => [variables: TVariables, setVariables: SetVariables<StateVariables<TVariables, TInputVariables>>];
|
|
13
|
+
};
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=createHydrationUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createHydrationUtils.d.ts","sourceRoot":"","sources":["../../../../src/openai/react/hooks/createHydrationUtils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EAClB,MAAM,gBAAgB,CAAC;AAIxB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AAQ3D,KAAK,sBAAsB,CAAC,UAAU,IAAI;KACvC,CAAC,IAAI,MAAM,UAAU,GAAG,UAAU,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;CACjE,CAAC;AAEF,KAAK,cAAc,CAAC,UAAU,EAAE,KAAK,IAAI;KACtC,CAAC,IAAI,MAAM,UAAU,IAAI,CAAC,SAAS,MAAM,KAAK,GAC7C,KAAK,CAAC,CAAC,CAAC,SAAS,QAAQ,CAAC,GAAG,CAAC,GAC5B,KAAK,GACL,CAAC,GACH,CAAC,GAAG,CAAC,SAAS,MAAM,UAAU,GAAG,UAAU,CAAC,CAAC,CAAC,GAAG,KAAK;CACzD,CAAC;AAEF,KAAK,YAAY,CAAC,MAAM,IAAI,CAC1B,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC,KAC1D,IAAI,CAAC;AAEV,oBAAoB;AACpB,wBAAgB,oBAAoB,CAClC,UAAU,SAAS,kBAAkB,GAAG,kBAAkB,EAC1D,QAAQ,EAAE,iBAAiB,CAAC,GAAG,EAAE,UAAU,CAAC,GAAG,YAAY;2BAKzD,eAAe,SAAS,sBAAsB,CAAC,UAAU,CAAC,aAE/C,eAAe,GACxB,MAAM,CAAC,OAAO,CAAC,MAAM,eAAe,EAAE,MAAM,UAAU,CAAC,EAAE,KAAK,CAAC,KAChE,CACD,SAAS,EAAE,UAAU,EACrB,YAAY,EAAE,YAAY,CAAC,cAAc,CAAC,UAAU,EAAE,eAAe,CAAC,CAAC,CACxE;EAqIF"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { useState, useCallback, useRef, useMemo, useLayoutEffect } from "react";
|
|
2
|
+
import { useApolloClient } from "./useApolloClient.js";
|
|
3
|
+
import { useToolName } from "./useToolName.js";
|
|
4
|
+
import { isReactive } from "../../../react/reactive.js";
|
|
5
|
+
import { equal } from "@wry/equality";
|
|
6
|
+
import { __DEV__ } from "@apollo/client/utilities/environment";
|
|
7
|
+
import { getToolNamesFromDocument, getVariableNamesFromDocument, } from "../../../utilities/index.js";
|
|
8
|
+
/** @experimental */
|
|
9
|
+
export function createHydrationUtils(document) {
|
|
10
|
+
const documentToolNames = getToolNamesFromDocument(document);
|
|
11
|
+
const variableNames = getVariableNamesFromDocument(document);
|
|
12
|
+
function useHydratedVariables(variables) {
|
|
13
|
+
const client = useApolloClient();
|
|
14
|
+
const toolName = useToolName();
|
|
15
|
+
const [toolInput] = useState(() => client.toolInput);
|
|
16
|
+
const toolMatches = toolInput !== undefined &&
|
|
17
|
+
toolName !== undefined &&
|
|
18
|
+
documentToolNames.has(toolName);
|
|
19
|
+
const [stateVars, setStateVars] = useState(() => {
|
|
20
|
+
const values = {};
|
|
21
|
+
for (const [key, value] of Object.entries(toolMatches ? toolInput : variables)) {
|
|
22
|
+
if (variableNames.has(key) && !isReactive(value)) {
|
|
23
|
+
values[key] = value;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return values;
|
|
27
|
+
});
|
|
28
|
+
const [initialReactiveValues] = useState(() => {
|
|
29
|
+
const values = {};
|
|
30
|
+
for (const [key, value] of Object.entries(variables)) {
|
|
31
|
+
if (variableNames.has(key) && isReactive(value)) {
|
|
32
|
+
values[key] = value.value;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return values;
|
|
36
|
+
});
|
|
37
|
+
const [reactiveVars, setReactiveVars] = useState(() => {
|
|
38
|
+
const values = {};
|
|
39
|
+
for (const [key, value] of Object.entries(initialReactiveValues)) {
|
|
40
|
+
if (toolMatches && key in toolInput) {
|
|
41
|
+
values[key] = toolInput[key];
|
|
42
|
+
}
|
|
43
|
+
else if (!toolMatches) {
|
|
44
|
+
values[key] = value;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return values;
|
|
48
|
+
});
|
|
49
|
+
const changedKeysRef = useRef(new Set());
|
|
50
|
+
const nextReactiveVars = {};
|
|
51
|
+
for (const [key, value] of Object.entries(variables)) {
|
|
52
|
+
if (!variableNames.has(key) || !isReactive(value))
|
|
53
|
+
continue;
|
|
54
|
+
const hasChanged = changedKeysRef.current.has(key) ||
|
|
55
|
+
!equal(value.value, initialReactiveValues[key]);
|
|
56
|
+
if (toolMatches && !hasChanged) {
|
|
57
|
+
if (key in toolInput) {
|
|
58
|
+
nextReactiveVars[key] = toolInput[key];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
nextReactiveVars[key] = value.value;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (!equal(nextReactiveVars, reactiveVars)) {
|
|
66
|
+
setReactiveVars(nextReactiveVars);
|
|
67
|
+
}
|
|
68
|
+
// Clear the tool input after first mount so that remounting the component
|
|
69
|
+
// uses the user-provided variables rather than the hydrated tool input.
|
|
70
|
+
// This runs once on mount; watchQuery also clears it when useQuery is
|
|
71
|
+
// present, so both paths are idempotent.
|
|
72
|
+
useLayoutEffect(() => {
|
|
73
|
+
if (toolMatches) {
|
|
74
|
+
client.clearToolInput();
|
|
75
|
+
}
|
|
76
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
77
|
+
}, []);
|
|
78
|
+
useLayoutEffect(() => {
|
|
79
|
+
for (const [key, value] of Object.entries(variables)) {
|
|
80
|
+
if (variableNames.has(key) &&
|
|
81
|
+
isReactive(value) &&
|
|
82
|
+
!changedKeysRef.current.has(key) &&
|
|
83
|
+
!equal(value.value, initialReactiveValues[key])) {
|
|
84
|
+
changedKeysRef.current.add(key);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
const resolvedVariables = useMemo(() => {
|
|
89
|
+
return { ...stateVars, ...reactiveVars };
|
|
90
|
+
}, [stateVars, reactiveVars]);
|
|
91
|
+
const setVariables = useCallback((update) => {
|
|
92
|
+
setStateVars((prev) => {
|
|
93
|
+
const updates = typeof update === "function" ? update(prev) : update;
|
|
94
|
+
const filtered = Object.fromEntries(Object.entries(updates).filter(([key]) => {
|
|
95
|
+
if (key in initialReactiveValues) {
|
|
96
|
+
if (__DEV__) {
|
|
97
|
+
console.warn(`Attempted to set reactive variable "${key}" via setVariables. ` +
|
|
98
|
+
`Reactive variables are read-only and are ignored. `);
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
return true;
|
|
103
|
+
}));
|
|
104
|
+
if (Object.keys(filtered).length === 0)
|
|
105
|
+
return prev;
|
|
106
|
+
return { ...prev, ...filtered };
|
|
107
|
+
});
|
|
108
|
+
}, []);
|
|
109
|
+
return [resolvedVariables, setVariables];
|
|
110
|
+
}
|
|
111
|
+
return { useHydratedVariables };
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=createHydrationUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createHydrationUtils.js","sourceRoot":"","sources":["../../../../src/openai/react/hooks/createHydrationUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,OAAO,CAAC;AAMhF,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAC;AAExD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,sCAAsC,CAAC;AAC/D,OAAO,EACL,wBAAwB,EACxB,4BAA4B,GAC7B,MAAM,6BAA6B,CAAC;AAkBrC,oBAAoB;AACpB,MAAM,UAAU,oBAAoB,CAElC,QAA2D;IAC3D,MAAM,iBAAiB,GAAG,wBAAwB,CAAC,QAAQ,CAAC,CAAC;IAC7D,MAAM,aAAa,GAAG,4BAA4B,CAAC,QAAQ,CAAC,CAAC;IAE7D,SAAS,oBAAoB,CAG3B,SACiE;QAKjE,MAAM,MAAM,GAAG,eAAe,EAAE,CAAC;QACjC,MAAM,QAAQ,GAAG,WAAW,EAAE,CAAC;QAC/B,MAAM,CAAC,SAAS,CAAC,GAAG,QAAQ,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QAErD,MAAM,WAAW,GACf,SAAS,KAAK,SAAS;YACvB,QAAQ,KAAK,SAAS;YACt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AG,sBAAsB;gCAC9D,oDAAoD,CACvD,CAAC;wBACJ,CAAC;wBACD,OAAO,KAAK,CAAC;oBACf,CAAC;oBACD,OAAO,IAAI,CAAC;gBACd,CAAC,CAAC,CACH,CAAC;gBAEF,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,MAAM,KAAK,CAAC;oBAAE,OAAO,IAAI,CAAC;gBAEpD,OAAO,EAAE,GAAG,IAAI,EAAE,GAAG,QAAQ,EAAE,CAAC;YAClC,CAAC,CAAC,CAAC;QACL,CAAC,EAAE,EAAE,CAAC,CAAC;QAEP,OAAO,CAAC,iBAAiB,EAAE,YAAY,CAAC,CAAC;IAC3C,CAAC;IAED,OAAO,EAAE,oBAAoB,EAAE,CAAC;AAClC,CAAC","sourcesContent":["import { useState, useCallback, useRef, useMemo, useLayoutEffect } from \"react\";\nimport type {\n DocumentNode,\n OperationVariables,\n TypedDocumentNode,\n} from \"@apollo/client\";\nimport { useApolloClient } from \"./useApolloClient.js\";\nimport { useToolName } from \"./useToolName.js\";\nimport { isReactive } from \"../../../react/reactive.js\";\nimport type { Reactive } from \"../../../react/reactive.js\";\nimport { equal } from \"@wry/equality\";\nimport { __DEV__ } from \"@apollo/client/utilities/environment\";\nimport {\n getToolNamesFromDocument,\n getVariableNamesFromDocument,\n} from \"../../../utilities/index.js\";\n\ntype HydratedVariablesInput<TVariables> = {\n [K in keyof TVariables]: TVariables[K] | Reactive<TVariables[K]>;\n};\n\ntype StateVariables<TVariables, Input> = {\n [K in keyof TVariables as K extends keyof Input ?\n Input[K] extends Reactive<any> ?\n never\n : K\n : K]: K extends keyof TVariables ? TVariables[K] : never;\n};\n\ntype SetVariables<TState> = (\n update: Partial<TState> | ((prev: TState) => Partial<TState>)\n) => void;\n\n/** @experimental */\nexport function createHydrationUtils<\n TVariables extends OperationVariables = OperationVariables,\n>(document: TypedDocumentNode<any, TVariables> | DocumentNode) {\n const documentToolNames = getToolNamesFromDocument(document);\n const variableNames = getVariableNamesFromDocument(document);\n\n function useHydratedVariables<\n TInputVariables extends HydratedVariablesInput<TVariables>,\n >(\n variables: TInputVariables &\n Record<Exclude<keyof TInputVariables, keyof TVariables>, never>\n ): [\n variables: TVariables,\n setVariables: SetVariables<StateVariables<TVariables, TInputVariables>>,\n ] {\n const client = useApolloClient();\n const toolName = useToolName();\n const [toolInput] = useState(() => client.toolInput);\n\n const toolMatches =\n toolInput !== undefined &&\n toolName !== undefined &&\n documentToolNames.has(toolName);\n\n const [stateVars, setStateVars] = useState<Record<string, unknown>>(() => {\n const values: Record<string, unknown> = {};\n\n for (const [key, value] of Object.entries(\n toolMatches ? toolInput : variables\n )) {\n if (variableNames.has(key) && !isReactive(value)) {\n values[key] = value;\n }\n }\n\n return values;\n });\n\n const [initialReactiveValues] = useState<Record<string, unknown>>(() => {\n const values: Record<string, unknown> = {};\n\n for (const [key, value] of Object.entries(variables)) {\n if (variableNames.has(key) && isReactive(value)) {\n values[key] = value.value;\n }\n }\n\n return values;\n });\n\n const [reactiveVars, setReactiveVars] = useState(() => {\n const values: Record<string, unknown> = {};\n\n for (const [key, value] of Object.entries(initialReactiveValues)) {\n if (toolMatches && key in toolInput) {\n values[key] = toolInput[key];\n } else if (!toolMatches) {\n values[key] = value;\n }\n }\n\n return values;\n });\n\n const changedKeysRef = useRef(new Set<string>());\n const nextReactiveVars: Record<string, unknown> = {};\n\n for (const [key, value] of Object.entries(variables)) {\n if (!variableNames.has(key) || !isReactive(value)) continue;\n\n const hasChanged =\n changedKeysRef.current.has(key) ||\n !equal(value.value, initialReactiveValues[key]);\n\n if (toolMatches && !hasChanged) {\n if (key in toolInput) {\n nextReactiveVars[key] = toolInput[key];\n }\n } else {\n nextReactiveVars[key] = value.value;\n }\n }\n\n if (!equal(nextReactiveVars, reactiveVars)) {\n setReactiveVars(nextReactiveVars);\n }\n\n // Clear the tool input after first mount so that remounting the component\n // uses the user-provided variables rather than the hydrated tool input.\n // This runs once on mount; watchQuery also clears it when useQuery is\n // present, so both paths are idempotent.\n useLayoutEffect(() => {\n if (toolMatches) {\n client.clearToolInput();\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n useLayoutEffect(() => {\n for (const [key, value] of Object.entries(variables)) {\n if (\n variableNames.has(key) &&\n isReactive(value) &&\n !changedKeysRef.current.has(key) &&\n !equal(value.value, initialReactiveValues[key])\n ) {\n changedKeysRef.current.add(key);\n }\n }\n });\n\n const resolvedVariables = useMemo(() => {\n return { ...stateVars, ...reactiveVars } as TVariables;\n }, [stateVars, reactiveVars]);\n\n const setVariables = useCallback<\n SetVariables<StateVariables<TVariables, TInputVariables>>\n >((update) => {\n setStateVars((prev) => {\n const updates =\n typeof update === \"function\" ? update(prev as any) : update;\n\n const filtered = Object.fromEntries(\n Object.entries(updates).filter(([key]) => {\n if (key in initialReactiveValues) {\n if (__DEV__) {\n console.warn(\n `Attempted to set reactive variable \"${key}\" via setVariables. ` +\n `Reactive variables are read-only and are ignored. `\n );\n }\n return false;\n }\n return true;\n })\n );\n\n if (Object.keys(filtered).length === 0) return prev;\n\n return { ...prev, ...filtered };\n });\n }, []);\n\n return [resolvedVariables, setVariables];\n }\n\n return { useHydratedVariables };\n}\n"]}
|
|
@@ -2,5 +2,6 @@ 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";
|
|
6
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/openai/react/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/openai/react/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC"}
|
|
@@ -2,5 +2,6 @@ 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";
|
|
6
7
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/openai/react/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC","sourcesContent":["export { useApp } from \"./hooks/useApp.js\";\nexport { useToolName } from \"./hooks/useToolName.js\";\nexport { useToolMetadata } from \"./hooks/useToolMetadata.js\";\nexport { useToolInput } from \"./hooks/useToolInput.js\";\nexport { useWidgetState } from \"./hooks/useWidgetState.js\";\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/openai/react/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC","sourcesContent":["export { useApp } from \"./hooks/useApp.js\";\nexport { useToolName } from \"./hooks/useToolName.js\";\nexport { useToolMetadata } from \"./hooks/useToolMetadata.js\";\nexport { useToolInput } from \"./hooks/useToolInput.js\";\nexport { createHydrationUtils } from \"./hooks/createHydrationUtils.js\";\nexport { useWidgetState } from \"./hooks/useWidgetState.js\";\n"]}
|
|
@@ -9,7 +9,7 @@ export function ApolloProvider({ children, client }) {
|
|
|
9
9
|
if (__DEV__) {
|
|
10
10
|
invariant(client[aiClientSymbol], 'The "client" instance provided to <ApolloProvider /> is the wrong instance. You might have imported `ApolloClient` from `@apollo/client`. Please import `ApolloClient` from `@apollo/client-ai-apps` instead.');
|
|
11
11
|
}
|
|
12
|
-
use(client.
|
|
12
|
+
use(client.connect());
|
|
13
13
|
return (_jsx(BaseApolloProvider, { client: client, children: children }));
|
|
14
14
|
}
|
|
15
15
|
//# sourceMappingURL=ApolloProvider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApolloProvider.js","sourceRoot":"","sources":["../../src/react/ApolloProvider.tsx"],"names":[],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,CAAC;IAEtB,OAAO,CACL,KAAC,kBAAkB,IAAC,MAAM,EAAE,MAA0B,YACnD,QAAQ,GACU,CACtB,CAAC;AACJ,CAAC","sourcesContent":["import type { ReactNode } from \"react\";\nimport { use } from \"react\";\nimport { ApolloProvider as BaseApolloProvider } from \"@apollo/client/react\";\nimport type { ApolloClient as BaseApolloClient } from \"@apollo/client\";\nimport { ApolloClient as OpenAiApolloClient } from \"../openai/core/ApolloClient.js\";\nimport { ApolloClient as McpApolloClient } from \"../mcp/core/ApolloClient.js\";\nimport type { ApolloClient as FallbackApolloClient } from \"../core/ApolloClient.js\";\nimport { __DEV__ } from \"@apollo/client/utilities/environment\";\nimport { aiClientSymbol, invariant } from \"../utilities/index.js\";\n\ntype ApolloClient = OpenAiApolloClient | McpApolloClient | FallbackApolloClient;\n\nexport declare namespace ApolloProvider {\n export interface Props {\n children?: ReactNode;\n client: ApolloClient;\n }\n}\n\nexport function ApolloProvider({ children, client }: ApolloProvider.Props) {\n if (__DEV__) {\n invariant(\n client[aiClientSymbol],\n 'The \"client\" instance provided to <ApolloProvider /> is the wrong instance. You might have imported `ApolloClient` from `@apollo/client`. Please import `ApolloClient` from `@apollo/client-ai-apps` instead.'\n );\n }\n\n use(client.connect());\n\n return (\n <BaseApolloProvider client={client as BaseApolloClient}>\n {children}\n </BaseApolloProvider>\n );\n}\n"]}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
export { ApolloProvider } from "./ApolloProvider.js";
|
|
2
|
+
export { reactive } from "./reactive.js";
|
|
3
|
+
export type { Reactive } from "./reactive.js";
|
|
2
4
|
export declare const useApp: typeof import("./index.mcp.js").useApp;
|
|
3
5
|
export declare const useToolInput: typeof import("./index.mcp.js").useToolInput;
|
|
4
6
|
export declare const useToolMetadata: typeof import("./index.mcp.js").useToolMetadata;
|
|
5
7
|
export declare const useToolName: typeof import("./index.mcp.js").useToolName;
|
|
8
|
+
/** @experimental */
|
|
9
|
+
export declare const createHydrationUtils: typeof import("./index.mcp.js").createHydrationUtils;
|
|
6
10
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAI9C,eAAO,MAAM,MAAM,wCAC4C,CAAC;AAEhE,eAAO,MAAM,YAAY,8CACkD,CAAC;AAE5E,eAAO,MAAM,eAAe,iDAGzB,CAAC;AAEJ,eAAO,MAAM,WAAW,6CACiD,CAAC;AAE1E,oBAAoB;AACpB,eAAO,MAAM,oBAAoB,sDAER,CAAC"}
|
package/dist/react/index.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { missingHook } from "./missingHook.js";
|
|
2
2
|
export { ApolloProvider } from "./ApolloProvider.js";
|
|
3
|
+
export { reactive } from "./reactive.js";
|
|
3
4
|
// Use `mcp` related types since these are the most common between the two
|
|
4
5
|
// targets
|
|
5
6
|
export const useApp = missingHook("useApp");
|
|
6
7
|
export const useToolInput = missingHook("useToolInput");
|
|
7
8
|
export const useToolMetadata = missingHook("useToolMetadata");
|
|
8
9
|
export const useToolName = missingHook("useToolName");
|
|
10
|
+
/** @experimental */
|
|
11
|
+
export const createHydrationUtils = missingHook("createHydrationUtils");
|
|
9
12
|
//# sourceMappingURL=index.js.map
|
package/dist/react/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAGzC,0EAA0E;AAC1E,UAAU;AACV,MAAM,CAAC,MAAM,MAAM,GACjB,WAAW,CAAyC,QAAQ,CAAC,CAAC;AAEhE,MAAM,CAAC,MAAM,YAAY,GACvB,WAAW,CAA+C,cAAc,CAAC,CAAC;AAE5E,MAAM,CAAC,MAAM,eAAe,GAC1B,WAAW,CACT,iBAAiB,CAClB,CAAC;AAEJ,MAAM,CAAC,MAAM,WAAW,GACtB,WAAW,CAA8C,aAAa,CAAC,CAAC;AAE1E,oBAAoB;AACpB,MAAM,CAAC,MAAM,oBAAoB,GAAG,WAAW,CAE7C,sBAAsB,CAAC,CAAC","sourcesContent":["import { missingHook } from \"./missingHook.js\";\n\nexport { ApolloProvider } from \"./ApolloProvider.js\";\nexport { reactive } from \"./reactive.js\";\nexport type { Reactive } from \"./reactive.js\";\n\n// Use `mcp` related types since these are the most common between the two\n// targets\nexport const useApp =\n missingHook<typeof import(\"./index.mcp.js\").useApp>(\"useApp\");\n\nexport const useToolInput =\n missingHook<typeof import(\"./index.mcp.js\").useToolInput>(\"useToolInput\");\n\nexport const useToolMetadata =\n missingHook<typeof import(\"./index.mcp.js\").useToolMetadata>(\n \"useToolMetadata\"\n );\n\nexport const useToolName =\n missingHook<typeof import(\"./index.mcp.js\").useToolName>(\"useToolName\");\n\n/** @experimental */\nexport const createHydrationUtils = missingHook<\n typeof import(\"./index.mcp.js\").createHydrationUtils\n>(\"createHydrationUtils\");\n"]}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export * from "./index.js";
|
|
2
|
-
export { useApp, useToolInput, useToolMetadata, useToolName, } from "../mcp/react/index.js";
|
|
2
|
+
export { useApp, useToolInput, createHydrationUtils, useToolMetadata, useToolName, } from "../mcp/react/index.js";
|
|
3
3
|
//# sourceMappingURL=index.mcp.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mcp.d.ts","sourceRoot":"","sources":["../../src/react/index.mcp.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EACL,MAAM,EACN,YAAY,EACZ,eAAe,EACf,WAAW,GACZ,MAAM,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.mcp.d.ts","sourceRoot":"","sources":["../../src/react/index.mcp.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EACL,MAAM,EACN,YAAY,EACZ,oBAAoB,EACpB,eAAe,EACf,WAAW,GACZ,MAAM,uBAAuB,CAAC"}
|
package/dist/react/index.mcp.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export * from "./index.js";
|
|
2
|
-
export { useApp, useToolInput, useToolMetadata, useToolName, } from "../mcp/react/index.js";
|
|
2
|
+
export { useApp, useToolInput, createHydrationUtils, useToolMetadata, useToolName, } from "../mcp/react/index.js";
|
|
3
3
|
//# sourceMappingURL=index.mcp.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mcp.js","sourceRoot":"","sources":["../../src/react/index.mcp.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EACL,MAAM,EACN,YAAY,EACZ,eAAe,EACf,WAAW,GACZ,MAAM,uBAAuB,CAAC","sourcesContent":["export * from \"./index.js\";\nexport {\n useApp,\n useToolInput,\n useToolMetadata,\n useToolName,\n} from \"../mcp/react/index.js\";\n"]}
|
|
1
|
+
{"version":3,"file":"index.mcp.js","sourceRoot":"","sources":["../../src/react/index.mcp.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EACL,MAAM,EACN,YAAY,EACZ,oBAAoB,EACpB,eAAe,EACf,WAAW,GACZ,MAAM,uBAAuB,CAAC","sourcesContent":["export * from \"./index.js\";\nexport {\n useApp,\n useToolInput,\n createHydrationUtils,\n useToolMetadata,\n useToolName,\n} from \"../mcp/react/index.js\";\n"]}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export * from "./index.js";
|
|
2
|
-
export { useApp, useToolInput, useToolMetadata, useToolName, } from "../openai/react/index.js";
|
|
2
|
+
export { useApp, useToolInput, createHydrationUtils, useToolMetadata, useToolName, } from "../openai/react/index.js";
|
|
3
3
|
//# sourceMappingURL=index.openai.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.openai.d.ts","sourceRoot":"","sources":["../../src/react/index.openai.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EACL,MAAM,EACN,YAAY,EACZ,eAAe,EACf,WAAW,GACZ,MAAM,0BAA0B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.openai.d.ts","sourceRoot":"","sources":["../../src/react/index.openai.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EACL,MAAM,EACN,YAAY,EACZ,oBAAoB,EACpB,eAAe,EACf,WAAW,GACZ,MAAM,0BAA0B,CAAC"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export * from "./index.js";
|
|
2
|
-
export { useApp, useToolInput, useToolMetadata, useToolName, } from "../openai/react/index.js";
|
|
2
|
+
export { useApp, useToolInput, createHydrationUtils, useToolMetadata, useToolName, } from "../openai/react/index.js";
|
|
3
3
|
//# sourceMappingURL=index.openai.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.openai.js","sourceRoot":"","sources":["../../src/react/index.openai.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EACL,MAAM,EACN,YAAY,EACZ,eAAe,EACf,WAAW,GACZ,MAAM,0BAA0B,CAAC","sourcesContent":["export * from \"./index.js\";\nexport {\n useApp,\n useToolInput,\n useToolMetadata,\n useToolName,\n} from \"../openai/react/index.js\";\n"]}
|
|
1
|
+
{"version":3,"file":"index.openai.js","sourceRoot":"","sources":["../../src/react/index.openai.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EACL,MAAM,EACN,YAAY,EACZ,oBAAoB,EACpB,eAAe,EACf,WAAW,GACZ,MAAM,0BAA0B,CAAC","sourcesContent":["export * from \"./index.js\";\nexport {\n useApp,\n useToolInput,\n createHydrationUtils,\n useToolMetadata,\n useToolName,\n} from \"../openai/react/index.js\";\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
declare const REACTIVE: unique symbol;
|
|
2
|
+
export interface Reactive<T> {
|
|
3
|
+
readonly [REACTIVE]: true;
|
|
4
|
+
readonly value: T;
|
|
5
|
+
}
|
|
6
|
+
export declare function reactive<T>(value: T): Reactive<T>;
|
|
7
|
+
export declare function isReactive(value: unknown): value is Reactive<unknown>;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=reactive.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reactive.d.ts","sourceRoot":"","sources":["../../src/react/reactive.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,QAAQ,eAA4B,CAAC;AAE3C,MAAM,WAAW,QAAQ,CAAC,CAAC;IACzB,QAAQ,CAAC,CAAC,QAAQ,CAAC,EAAE,IAAI,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;CACnB;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAEjD;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,QAAQ,CAAC,OAAO,CAAC,CAOrE"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const REACTIVE = Symbol("apollo.reactive");
|
|
2
|
+
export function reactive(value) {
|
|
3
|
+
return { [REACTIVE]: true, value };
|
|
4
|
+
}
|
|
5
|
+
export function isReactive(value) {
|
|
6
|
+
return (typeof value === "object" &&
|
|
7
|
+
value !== null &&
|
|
8
|
+
REACTIVE in value &&
|
|
9
|
+
value[REACTIVE] === true);
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=reactive.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reactive.js","sourceRoot":"","sources":["../../src/react/reactive.ts"],"names":[],"mappings":"AAAA,MAAM,QAAQ,GAAG,MAAM,CAAC,iBAAiB,CAAC,CAAC;AAO3C,MAAM,UAAU,QAAQ,CAAI,KAAQ;IAClC,OAAO,EAAE,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AACrC,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,KAAc;IACvC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,QAAQ,IAAI,KAAK;QAChB,KAAiC,CAAC,QAAQ,CAAC,KAAK,IAAI,CACtD,CAAC;AACJ,CAAC","sourcesContent":["const REACTIVE = Symbol(\"apollo.reactive\");\n\nexport interface Reactive<T> {\n readonly [REACTIVE]: true;\n readonly value: T;\n}\n\nexport function reactive<T>(value: T): Reactive<T> {\n return { [REACTIVE]: true, value };\n}\n\nexport function isReactive(value: unknown): value is Reactive<unknown> {\n return (\n typeof value === \"object\" &&\n value !== null &&\n REACTIVE in value &&\n (value as Record<symbol, unknown>)[REACTIVE] === true\n );\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getToolNamesFromDocument.d.ts","sourceRoot":"","sources":["../../src/utilities/getToolNamesFromDocument.ts"],"names":[],"mappings":"AACA,OAAO,EAAQ,KAAK,YAAY,EAAE,MAAM,SAAS,CAAC;AAElD,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,YAAY,eAW9D"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { getOperationDefinition } from "@apollo/client/utilities/internal";
|
|
2
|
+
import { Kind } from "graphql";
|
|
3
|
+
export function getToolNamesFromDocument(document) {
|
|
4
|
+
const operationDef = getOperationDefinition(document);
|
|
5
|
+
return new Set(operationDef?.directives
|
|
6
|
+
?.filter((d) => d.name.value === "tool")
|
|
7
|
+
.flatMap((d) => {
|
|
8
|
+
const nameArg = d.arguments?.find((arg) => arg.name.value === "name");
|
|
9
|
+
return nameArg?.value.kind === Kind.STRING ? [nameArg.value.value] : [];
|
|
10
|
+
}));
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=getToolNamesFromDocument.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getToolNamesFromDocument.js","sourceRoot":"","sources":["../../src/utilities/getToolNamesFromDocument.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,mCAAmC,CAAC;AAC3E,OAAO,EAAE,IAAI,EAAqB,MAAM,SAAS,CAAC;AAElD,MAAM,UAAU,wBAAwB,CAAC,QAAsB;IAC7D,MAAM,YAAY,GAAG,sBAAsB,CAAC,QAAQ,CAAC,CAAC;IAEtD,OAAO,IAAI,GAAG,CACZ,YAAY,EAAE,UAAU;QACtB,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,KAAK,MAAM,CAAC;SACvC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE;QACb,MAAM,OAAO,GAAG,CAAC,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,KAAK,MAAM,CAAC,CAAC;QACtE,OAAO,OAAO,EAAE,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAC1E,CAAC,CAAC,CACL,CAAC;AACJ,CAAC","sourcesContent":["import { getOperationDefinition } from \"@apollo/client/utilities/internal\";\nimport { Kind, type DocumentNode } from \"graphql\";\n\nexport function getToolNamesFromDocument(document: DocumentNode) {\n const operationDef = getOperationDefinition(document);\n\n return new Set(\n operationDef?.directives\n ?.filter((d) => d.name.value === \"tool\")\n .flatMap((d) => {\n const nameArg = d.arguments?.find((arg) => arg.name.value === \"name\");\n return nameArg?.value.kind === Kind.STRING ? [nameArg.value.value] : [];\n })\n );\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getVariableNamesFromDocument.d.ts","sourceRoot":"","sources":["../../src/utilities/getVariableNamesFromDocument.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5C,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,YAAY,eAKlE"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { getOperationDefinition } from "@apollo/client/utilities/internal";
|
|
2
|
+
export function getVariableNamesFromDocument(document) {
|
|
3
|
+
const operationDef = getOperationDefinition(document);
|
|
4
|
+
return new Set(operationDef?.variableDefinitions?.map((v) => v.variable.name.value) ?? []);
|
|
5
|
+
}
|
|
6
|
+
//# sourceMappingURL=getVariableNamesFromDocument.js.map
|