|
| 1 | +import { ApolloLink } from "@apollo/client"; |
| 2 | +import { print } from "graphql"; |
| 3 | +import { BASE_SYSTEM_PROMPT } from "./consts.js"; |
| 4 | + |
| 5 | +export declare namespace AIAdapter { |
| 6 | + export interface Options { |
| 7 | + systemPrompt?: string; |
| 8 | + } |
| 9 | + |
| 10 | + export type Result = ApolloLink.Result; |
| 11 | +} |
| 12 | + |
| 13 | +export abstract class AIAdapter { |
| 14 | + public providedSystemPrompt: string | undefined; |
| 15 | + protected systemPrompt: string; |
| 16 | + |
| 17 | + constructor(options: AIAdapter.Options = {}) { |
| 18 | + this.systemPrompt = BASE_SYSTEM_PROMPT; |
| 19 | + if (options.systemPrompt) { |
| 20 | + this.providedSystemPrompt = options.systemPrompt; |
| 21 | + this.systemPrompt += `\n\n${this.providedSystemPrompt}`; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public generateResponseForOperation( |
| 26 | + operation: ApolloLink.Operation, |
| 27 | + prompt: string |
| 28 | + ): Promise<AIAdapter.Result> { |
| 29 | + return Promise.resolve({ data: null }); |
| 30 | + } |
| 31 | + |
| 32 | + protected createPrompt( |
| 33 | + { query, variables }: ApolloLink.Operation, |
| 34 | + prompt: string |
| 35 | + ): string { |
| 36 | + // Try to get the GraphQL query document string |
| 37 | + // from the AST location if available, otherwise |
| 38 | + // use the `print` function to get the query string. |
| 39 | + // |
| 40 | + // The AST location may not be available if the query |
| 41 | + // was parsed with the `noLocation: true` option. |
| 42 | + // |
| 43 | + // If the query document string is available through |
| 44 | + // the AST location, it will save some processing time |
| 45 | + // over the `print` function. |
| 46 | + const queryString = query?.loc?.source?.body ?? print(query); |
| 47 | + |
| 48 | + let promptVariables = ""; |
| 49 | + if (variables) { |
| 50 | + promptVariables = ` |
| 51 | +
|
| 52 | + With variables: |
| 53 | + \`\`\`json |
| 54 | + ${JSON.stringify(variables, null, 2)} |
| 55 | + \`\`\``; |
| 56 | + } |
| 57 | + |
| 58 | + return `Give me mock data that fulfills this query: |
| 59 | + \`\`\`graphql |
| 60 | + ${queryString} |
| 61 | + \`\`\` |
| 62 | + ${promptVariables} |
| 63 | + ${prompt ? `\nAdditional instructions:\n${prompt}` : ""} |
| 64 | + `; |
| 65 | + } |
| 66 | +} |
0 commit comments