@apollo/client-ai-apps 0.2.4 → 0.3.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.
@@ -35,7 +35,9 @@ const getRawValue = (node: ValueNode): any => {
35
35
  return acc;
36
36
  }, {});
37
37
  default:
38
- throw new Error(`Error when parsing directive values: unexpected type '${node.kind}'`);
38
+ throw new Error(
39
+ `Error when parsing directive values: unexpected type '${node.kind}'`
40
+ );
39
41
  }
40
42
  };
41
43
 
@@ -48,7 +50,9 @@ const getTypedDirectiveArgument = (
48
50
  return undefined;
49
51
  }
50
52
 
51
- let argument = directiveArguments.find((directiveArgument) => directiveArgument.name.value === argumentName);
53
+ let argument = directiveArguments.find(
54
+ (directiveArgument) => directiveArgument.name.value === argumentName
55
+ );
52
56
 
53
57
  if (!argument) {
54
58
  return undefined;
@@ -80,39 +84,66 @@ export const ApplicationManifestPlugin = () => {
80
84
  const client = new ApolloClient({
81
85
  cache: clientCache,
82
86
  link: new ApolloLink((operation) => {
83
- const body = print(removeClientDirective(sortTopLevelDefinitions(operation.query)));
87
+ const body = print(
88
+ removeClientDirective(sortTopLevelDefinitions(operation.query))
89
+ );
84
90
  const name = operation.operationName;
85
91
  const variables = (
86
- operation.query.definitions.find((d) => d.kind === "OperationDefinition") as OperationDefinitionNode
92
+ operation.query.definitions.find(
93
+ (d) => d.kind === "OperationDefinition"
94
+ ) as OperationDefinitionNode
87
95
  ).variableDefinitions?.reduce(
88
- (obj, varDef) => ({ ...obj, [varDef.variable.name.value]: getTypeName(varDef.type) }),
96
+ (obj, varDef) => ({
97
+ ...obj,
98
+ [varDef.variable.name.value]: getTypeName(varDef.type),
99
+ }),
89
100
  {}
90
101
  );
91
102
  const type = (
92
- operation.query.definitions.find((d) => d.kind === "OperationDefinition") as OperationDefinitionNode
103
+ operation.query.definitions.find(
104
+ (d) => d.kind === "OperationDefinition"
105
+ ) as OperationDefinitionNode
93
106
  ).operation;
94
107
  const prefetch = (
95
- operation.query.definitions.find((d) => d.kind === "OperationDefinition") as OperationDefinitionNode
108
+ operation.query.definitions.find(
109
+ (d) => d.kind === "OperationDefinition"
110
+ ) as OperationDefinitionNode
96
111
  ).directives?.some((d) => d.name.value === "prefetch");
97
112
  const id = createHash("sha256").update(body).digest("hex");
98
113
  // TODO: For now, you can only have 1 operation marked as prefetch. In the future, we'll likely support more than 1, and the "prefetchId" will be defined on the `@prefetch` itself as an argument
99
114
  const prefetchID = prefetch ? "__anonymous" : undefined;
100
115
 
101
116
  const tools = (
102
- operation.query.definitions.find((d) => d.kind === "OperationDefinition") as OperationDefinitionNode
117
+ operation.query.definitions.find(
118
+ (d) => d.kind === "OperationDefinition"
119
+ ) as OperationDefinitionNode
103
120
  ).directives
104
121
  ?.filter((d) => d.name.value === "tool")
105
122
  .map((directive) => {
106
- const name = getTypedDirectiveArgument("name", Kind.STRING, directive.arguments);
107
- const description = getTypedDirectiveArgument("description", Kind.STRING, directive.arguments);
108
- const extraInputs = getTypedDirectiveArgument("extraInputs", Kind.LIST, directive.arguments);
123
+ const name = getTypedDirectiveArgument(
124
+ "name",
125
+ Kind.STRING,
126
+ directive.arguments
127
+ );
128
+ const description = getTypedDirectiveArgument(
129
+ "description",
130
+ Kind.STRING,
131
+ directive.arguments
132
+ );
133
+ const extraInputs = getTypedDirectiveArgument(
134
+ "extraInputs",
135
+ Kind.LIST,
136
+ directive.arguments
137
+ );
109
138
 
110
139
  if (!name) {
111
140
  throw new Error("'name' argument must be supplied for @tool");
112
141
  }
113
142
 
114
143
  if (!description) {
115
- throw new Error("'description' argument must be supplied for @tool");
144
+ throw new Error(
145
+ "'description' argument must be supplied for @tool"
146
+ );
116
147
  }
117
148
 
118
149
  return {
@@ -122,7 +153,9 @@ export const ApplicationManifestPlugin = () => {
122
153
  };
123
154
  });
124
155
 
125
- return Observable.of({ data: { id, name, type, body, variables, prefetch, prefetchID, tools } });
156
+ return Observable.of({
157
+ data: { id, name, type, body, variables, prefetch, prefetchID, tools },
158
+ });
126
159
  }),
127
160
  });
128
161
 
@@ -146,16 +179,27 @@ export const ApplicationManifestPlugin = () => {
146
179
 
147
180
  const operations = [];
148
181
  for (const source of sources) {
149
- const type = (source.node.definitions.find((d) => d.kind === "OperationDefinition") as OperationDefinitionNode)
150
- .operation;
182
+ const type = (
183
+ source.node.definitions.find(
184
+ (d) => d.kind === "OperationDefinition"
185
+ ) as OperationDefinitionNode
186
+ ).operation;
151
187
 
152
188
  let result;
153
189
  if (type === "query") {
154
- result = await client.query({ query: source.node, fetchPolicy: "no-cache" });
190
+ result = await client.query({
191
+ query: source.node,
192
+ fetchPolicy: "no-cache",
193
+ });
155
194
  } else if (type === "mutation") {
156
- result = await client.mutate({ mutation: source.node, fetchPolicy: "no-cache" });
195
+ result = await client.mutate({
196
+ mutation: source.node,
197
+ fetchPolicy: "no-cache",
198
+ });
157
199
  } else {
158
- throw new Error("Found an unsupported operation type. Only Query and Mutation are supported.");
200
+ throw new Error(
201
+ "Found an unsupported operation type. Only Query and Mutation are supported."
202
+ );
159
203
  }
160
204
  operations.push(result.data);
161
205
  }
@@ -168,7 +212,9 @@ export const ApplicationManifestPlugin = () => {
168
212
  };
169
213
 
170
214
  const generateManifest = async () => {
171
- const operations = Array.from(cache.values()).flatMap((entry) => entry.operations);
215
+ const operations = Array.from(cache.values()).flatMap(
216
+ (entry) => entry.operations
217
+ );
172
218
  if (operations.filter((o) => o.prefetch).length > 1) {
173
219
  throw new Error(
174
220
  "Found multiple operations marked as `@prefetch`. You can only mark 1 operation with `@prefetch`."
@@ -199,7 +245,9 @@ export const ApplicationManifestPlugin = () => {
199
245
  name: packageJson.name,
200
246
  description: packageJson.description,
201
247
  hash: createHash("sha256").update(Date.now().toString()).digest("hex"),
202
- operations: Array.from(cache.values()).flatMap((entry) => entry.operations),
248
+ operations: Array.from(cache.values()).flatMap(
249
+ (entry) => entry.operations
250
+ ),
203
251
  resource,
204
252
  csp: {
205
253
  connectDomains: packageJson.csp?.connectDomains ?? [],
@@ -208,7 +256,11 @@ export const ApplicationManifestPlugin = () => {
208
256
  };
209
257
 
210
258
  // Always write to build directory so the MCP server picks it up
211
- const dest = path.resolve(root, config.build.outDir, ".application-manifest.json");
259
+ const dest = path.resolve(
260
+ root,
261
+ config.build.outDir,
262
+ ".application-manifest.json"
263
+ );
212
264
  mkdirSync(path.dirname(dest), { recursive: true });
213
265
  writeFileSync(dest, JSON.stringify(manifest));
214
266
 
@@ -279,8 +331,14 @@ export function sortTopLevelDefinitions(query: DocumentNode): DocumentNode {
279
331
  // non-executable definitions don't have to have names (even though any
280
332
  // DocumentNode actually passed here should only have executable
281
333
  // definitions).
282
- const aName = a.kind === "OperationDefinition" || a.kind === "FragmentDefinition" ? a.name?.value ?? "" : "";
283
- const bName = b.kind === "OperationDefinition" || b.kind === "FragmentDefinition" ? b.name?.value ?? "" : "";
334
+ const aName =
335
+ a.kind === "OperationDefinition" || a.kind === "FragmentDefinition" ?
336
+ (a.name?.value ?? "")
337
+ : "";
338
+ const bName =
339
+ b.kind === "OperationDefinition" || b.kind === "FragmentDefinition" ?
340
+ (b.name?.value ?? "")
341
+ : "";
284
342
 
285
343
  // Sort by name ascending.
286
344
  if (aName < bName) {
@@ -306,7 +364,9 @@ function removeClientDirective(doc: DocumentNode) {
306
364
  OperationDefinition(node) {
307
365
  return {
308
366
  ...node,
309
- directives: node.directives?.filter((d) => d.name.value !== "prefetch" && d.name.value !== "tool"),
367
+ directives: node.directives?.filter(
368
+ (d) => d.name.value !== "prefetch" && d.name.value !== "tool"
369
+ ),
310
370
  };
311
371
  },
312
372
  });