{"version":3,"sources":["../src/hooks/make-autosuggestions-function/use-make-standard-autosuggestions-function.tsx"],"sourcesContent":["import { AI_CLOUD_PUBLIC_API_KEY_HEADER } from \"@vn-sdk/shared\";\nimport { useAiContext } from \"@vn-sdk/react-core\";\nimport { useCallback } from \"react\";\nimport { AutosuggestionsBareFunction } from \"../../types\";\nimport { retry } from \"../../lib/retry\";\nimport { InsertionEditorState } from \"../../types/base/autosuggestions-bare-function\";\nimport { SuggestionsApiConfig } from \"../../types/autosuggestions-config/suggestions-api-config\";\nimport {\n  Message,\n  Role,\n  TextMessage,\n  convertGqlOutputToMessages,\n  convertMessagesToGqlInput,\n  filterAgentStateMessages,\n  AiRequestType,\n} from \"@vn-sdk/runtime-client-gql\";\n\n/**\n * Returns a memoized function that sends a request to the specified API endpoint to get an autosuggestion for the user's input.\n * The function takes in the text before and after the cursor, and an abort signal.\n * It sends a POST request to the API endpoint with the messages array containing the system message, few shot messages, and user messages.\n * The function returns the suggestion from the API response.\n *\n * @param textareaPurpose - The purpose of the textarea. This is included in the system message.\n * @param apiEndpoint - The API endpoint to send the autosuggestion request to.\n * @param makeSystemMessage - A function that takes in a context string and returns a system message to include in the autosuggestion request.\n * @param fewShotMessages - An array of few shot messages to include in the autosuggestion request.\n * @param contextCategories - The categories of context strings we want to include. By default, we include the (default) \"global\" context category.\n * @returns A memoized function that sends a request to the specified API endpoint to get an autosuggestion for the user's input.\n */\nexport function useMakeStandardAutosuggestionFunction(\n  textareaPurpose: string,\n  contextCategories: string[],\n  apiConfig: SuggestionsApiConfig,\n): AutosuggestionsBareFunction {\n  const { getContextString, aiApiConfig, runtimeClient } = useAiContext();\n  const { chatApiEndpoint: url, publicApiKey, credentials, properties } = aiApiConfig;\n  const headers = {\n    ...aiApiConfig.headers,\n    ...(publicApiKey ? { [AI_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {}),\n  };\n  const { maxTokens, stop, temperature = 0 } = apiConfig;\n\n  return useCallback(\n    async (editorState: InsertionEditorState, abortSignal: AbortSignal) => {\n      const res = await retry(async () => {\n        // @ts-expect-error -- Passing null is forbidden, but we're filtering it later\n        const messages: Message[] = [\n          new TextMessage({\n            role: Role.System,\n            content: apiConfig.makeSystemPrompt(\n              textareaPurpose,\n              getContextString([], contextCategories),\n            ),\n          }),\n          ...apiConfig.fewShotMessages,\n          editorState.textAfterCursor != \"\"\n            ? new TextMessage({\n                role: Role.User,\n                content: editorState.textAfterCursor,\n              })\n            : null,\n          new TextMessage({\n            role: Role.User,\n            content: `<TextAfterCursor>${editorState.textAfterCursor}</TextAfterCursor>`,\n          }),\n          new TextMessage({\n            role: Role.User,\n            content: `<TextBeforeCursor>${editorState.textBeforeCursor}</TextBeforeCursor>`,\n          }),\n        ].filter(Boolean);\n\n        const response = await runtimeClient\n          .generateCopilotResponse({\n            data: {\n              frontend: {\n                actions: [],\n                url: window.location.href,\n              },\n              messages: convertMessagesToGqlInput(filterAgentStateMessages(messages)),\n              metadata: {\n                requestType: AiRequestType.TextareaCompletion,\n              },\n              forwardedParameters: {\n                maxTokens,\n                stop,\n                temperature,\n              },\n            },\n            properties,\n            signal: abortSignal,\n          })\n          .toPromise();\n\n        let result = \"\";\n        for (const message of convertGqlOutputToMessages(\n          response.data?.generateCopilotResponse?.messages ?? [],\n        )) {\n          if (abortSignal.aborted) {\n            break;\n          }\n          if (message.isTextMessage()) {\n            result += message.content;\n          }\n        }\n\n        return result;\n      });\n\n      return res;\n    },\n    [apiConfig, getContextString, contextCategories, textareaPurpose],\n  );\n}\n"],"mappings":";;;;;;;;;AAAA,SAAS,sCAAsC;AAC/C,SAAS,oBAAoB;AAC7B,SAAS,mBAAmB;AAK5B;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAeA,SAAS,sCACd,iBACA,mBACA,WAC6B;AAC7B,QAAM,EAAE,kBAAkB,aAAa,cAAc,IAAI,aAAa;AACtE,QAAM,EAAE,iBAAiB,KAAK,cAA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