@copilotkit/react-core 0.17.0-alpha.0 → 0.17.0-alpha.1

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 (125) hide show
  1. package/.turbo/turbo-build.log +80 -80
  2. package/CHANGELOG.md +6 -0
  3. package/dist/chunk-5UGLWBZJ.mjs +3 -0
  4. package/dist/chunk-5UGLWBZJ.mjs.map +1 -0
  5. package/dist/chunk-6A4PCNMZ.mjs +48 -0
  6. package/dist/chunk-6A4PCNMZ.mjs.map +1 -0
  7. package/dist/chunk-6SOQYBNX.mjs +44 -0
  8. package/dist/chunk-6SOQYBNX.mjs.map +1 -0
  9. package/dist/chunk-7GFKOIO7.mjs +3 -0
  10. package/dist/chunk-7GFKOIO7.mjs.map +1 -0
  11. package/dist/chunk-7P4OYNP3.mjs +19 -0
  12. package/dist/chunk-7P4OYNP3.mjs.map +1 -0
  13. package/dist/chunk-BABVSMJR.mjs +3 -0
  14. package/dist/chunk-BABVSMJR.mjs.map +1 -0
  15. package/dist/chunk-EFZPSZWO.mjs +3 -0
  16. package/dist/chunk-EFZPSZWO.mjs.map +1 -0
  17. package/dist/chunk-F2JIAPZQ.mjs +13 -0
  18. package/dist/chunk-F2JIAPZQ.mjs.map +1 -0
  19. package/dist/chunk-FP4EXCGS.mjs +27 -0
  20. package/dist/chunk-FP4EXCGS.mjs.map +1 -0
  21. package/dist/chunk-FRAKUJWH.mjs +3 -0
  22. package/dist/chunk-FRAKUJWH.mjs.map +1 -0
  23. package/dist/chunk-HW5IH6PV.mjs +19 -0
  24. package/dist/chunk-HW5IH6PV.mjs.map +1 -0
  25. package/dist/chunk-JD7BAH7U.mjs +3 -0
  26. package/dist/chunk-JD7BAH7U.mjs.map +1 -0
  27. package/dist/chunk-MRXNTQOX.mjs +55 -0
  28. package/dist/chunk-MRXNTQOX.mjs.map +1 -0
  29. package/dist/chunk-MZ5UN3BY.mjs +28 -0
  30. package/dist/chunk-MZ5UN3BY.mjs.map +1 -0
  31. package/dist/chunk-PUFR5OAI.mjs +3 -0
  32. package/dist/chunk-PUFR5OAI.mjs.map +1 -0
  33. package/dist/chunk-QTET3FBV.mjs +165 -0
  34. package/dist/chunk-QTET3FBV.mjs.map +1 -0
  35. package/dist/chunk-SM3XH2NJ.mjs +77 -0
  36. package/dist/chunk-SM3XH2NJ.mjs.map +1 -0
  37. package/dist/chunk-SPCZTZCY.mjs +3 -0
  38. package/dist/chunk-SPCZTZCY.mjs.map +1 -0
  39. package/dist/chunk-TDH7XE4X.mjs +104 -0
  40. package/dist/chunk-TDH7XE4X.mjs.map +1 -0
  41. package/dist/chunk-TKXPMMND.mjs +177 -0
  42. package/dist/chunk-TKXPMMND.mjs.map +1 -0
  43. package/dist/chunk-TU4DNDO7.mjs +125 -0
  44. package/dist/chunk-TU4DNDO7.mjs.map +1 -0
  45. package/dist/chunk-VK25FIML.mjs +144 -0
  46. package/dist/chunk-VK25FIML.mjs.map +1 -0
  47. package/dist/chunk-VNRDQJXW.mjs +3 -0
  48. package/dist/chunk-VNRDQJXW.mjs.map +1 -0
  49. package/dist/chunk-VUY2K2DI.mjs +135 -0
  50. package/dist/chunk-VUY2K2DI.mjs.map +1 -0
  51. package/dist/chunk-YULKJPY3.mjs +70 -0
  52. package/dist/chunk-YULKJPY3.mjs.map +1 -0
  53. package/dist/components/copilot-provider/copilot-provider-props.mjs +2 -1
  54. package/dist/components/copilot-provider/copilot-provider-props.mjs.map +1 -1
  55. package/dist/components/copilot-provider/copilot-provider.mjs +7 -468
  56. package/dist/components/copilot-provider/copilot-provider.mjs.map +1 -1
  57. package/dist/components/copilot-provider/index.mjs +8 -467
  58. package/dist/components/copilot-provider/index.mjs.map +1 -1
  59. package/dist/components/copilot-provider/standard-copilot-api-config.mjs +3 -12
  60. package/dist/components/copilot-provider/standard-copilot-api-config.mjs.map +1 -1
  61. package/dist/components/index.mjs +9 -467
  62. package/dist/components/index.mjs.map +1 -1
  63. package/dist/context/copilot-context.mjs +3 -70
  64. package/dist/context/copilot-context.mjs.map +1 -1
  65. package/dist/context/index.mjs +4 -69
  66. package/dist/context/index.mjs.map +1 -1
  67. package/dist/hooks/index.mjs +17 -654
  68. package/dist/hooks/index.mjs.map +1 -1
  69. package/dist/hooks/use-chat.mjs +5 -460
  70. package/dist/hooks/use-chat.mjs.map +1 -1
  71. package/dist/hooks/use-copilot-chat.mjs +13 -599
  72. package/dist/hooks/use-copilot-chat.mjs.map +1 -1
  73. package/dist/hooks/use-flat-category-store.mjs +3 -68
  74. package/dist/hooks/use-flat-category-store.mjs.map +1 -1
  75. package/dist/hooks/use-make-copilot-actionable.mjs +4 -95
  76. package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -1
  77. package/dist/hooks/use-make-copilot-document-readable.mjs +4 -87
  78. package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -1
  79. package/dist/hooks/use-make-copilot-readable.mjs +4 -87
  80. package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
  81. package/dist/hooks/use-tree.mjs +3 -153
  82. package/dist/hooks/use-tree.mjs.map +1 -1
  83. package/dist/index.mjs +26 -1204
  84. package/dist/index.mjs.map +1 -1
  85. package/dist/openai/chat-completion-client.d.ts +0 -4
  86. package/dist/openai/chat-completion-client.mjs +4 -359
  87. package/dist/openai/chat-completion-client.mjs.map +1 -1
  88. package/dist/openai/chat-completion-stream.mjs +4 -220
  89. package/dist/openai/chat-completion-stream.mjs.map +1 -1
  90. package/dist/openai/chat-completion-transport.d.ts +3 -5
  91. package/dist/openai/chat-completion-transport.mjs +3 -180
  92. package/dist/openai/chat-completion-transport.mjs.map +1 -1
  93. package/dist/openai/index.mjs +5 -220
  94. package/dist/openai/index.mjs.map +1 -1
  95. package/dist/openai-assistants/hooks/index.mjs +14 -235
  96. package/dist/openai-assistants/hooks/index.mjs.map +1 -1
  97. package/dist/openai-assistants/hooks/use-assistants.mjs +8 -52
  98. package/dist/openai-assistants/hooks/use-assistants.mjs.map +1 -1
  99. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +13 -236
  100. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map +1 -1
  101. package/dist/openai-assistants/index.mjs +15 -236
  102. package/dist/openai-assistants/index.mjs.map +1 -1
  103. package/dist/openai-assistants/utils/index.mjs +4 -46
  104. package/dist/openai-assistants/utils/index.mjs.map +1 -1
  105. package/dist/openai-assistants/utils/process-message-stream.mjs +3 -46
  106. package/dist/openai-assistants/utils/process-message-stream.mjs.map +1 -1
  107. package/dist/types/annotated-function.mjs +2 -0
  108. package/dist/types/annotated-function.mjs.map +1 -1
  109. package/dist/types/base.mjs +2 -0
  110. package/dist/types/base.mjs.map +1 -1
  111. package/dist/types/document-pointer.mjs +2 -0
  112. package/dist/types/document-pointer.mjs.map +1 -1
  113. package/dist/types/index.mjs +2 -0
  114. package/dist/types/index.mjs.map +1 -1
  115. package/dist/types/message.mjs +2 -0
  116. package/dist/types/message.mjs.map +1 -1
  117. package/dist/utils/utils.mjs +2 -0
  118. package/dist/utils/utils.mjs.map +1 -1
  119. package/dist/utils/utils.test.mjs +1 -0
  120. package/dist/utils/utils.test.mjs.map +1 -1
  121. package/package.json +3 -3
  122. package/src/hooks/use-chat.ts +2 -2
  123. package/src/openai/chat-completion-client.ts +4 -15
  124. package/src/openai/chat-completion-stream.ts +1 -3
  125. package/src/openai/chat-completion-transport.ts +5 -9
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hooks/use-copilot-chat.ts","../../src/context/copilot-context.tsx","../../src/hooks/use-chat.ts","../../src/openai/chat-completion-client.ts","../../src/openai/chat-completion-transport.ts","../../src/components/copilot-provider/copilot-provider.tsx"],"sourcesContent":["import { useMemo, useContext } from \"react\";\nimport { CopilotContext, CopilotContextParams } from \"../context/copilot-context\";\nimport { Message } from \"../types\";\nimport { UseChatOptions, useChat } from \"./use-chat\";\nimport { defaultCopilotContextCategories } from \"../components\";\nimport { ChatCompletionCreateParams } from \"openai/resources/chat\";\n\nexport interface UseCopilotChatOptions extends UseChatOptions {\n makeSystemMessage?: (contextString: string) => string;\n}\n\nexport interface UseCopilotChatReturn {\n visibleMessages: Message[];\n append: (message: Message) => Promise<void>;\n reload: () => Promise<void>;\n stop: () => void;\n isLoading: boolean;\n input: string;\n setInput: React.Dispatch<React.SetStateAction<string>>;\n}\n\nexport function useCopilotChat({\n makeSystemMessage,\n ...options\n}: UseCopilotChatOptions): UseCopilotChatReturn {\n const {\n getContextString,\n getChatCompletionFunctionDescriptions,\n getFunctionCallHandler,\n copilotApiConfig,\n } = useContext(CopilotContext);\n\n const systemMessage: Message = useMemo(() => {\n const systemMessageMaker = makeSystemMessage || defaultSystemMessage;\n const contextString = getContextString([], defaultCopilotContextCategories); // TODO: make the context categories configurable\n\n return {\n id: \"system\",\n content: systemMessageMaker(contextString),\n role: \"system\",\n };\n }, [getContextString, makeSystemMessage]);\n\n const functionDescriptions: ChatCompletionCreateParams.Function[] = useMemo(() => {\n return getChatCompletionFunctionDescriptions();\n }, [getChatCompletionFunctionDescriptions]);\n\n const { messages, append, reload, stop, isLoading, input, setInput } = useChat({\n ...options,\n copilotConfig: copilotApiConfig,\n id: options.id,\n initialMessages: [systemMessage].concat(options.initialMessages || []),\n functions: functionDescriptions,\n onFunctionCall: getFunctionCallHandler(),\n headers: { ...options.headers },\n body: {\n ...options.body,\n },\n });\n\n const visibleMessages = messages.filter(\n (message) => message.role === \"user\" || message.role === \"assistant\",\n );\n\n return {\n visibleMessages,\n append,\n reload,\n stop,\n isLoading,\n input,\n setInput,\n };\n}\n\nexport function defaultSystemMessage(contextString: string): string {\n return `\nPlease act as an efficient, competent, conscientious, and industrious professional assistant.\n\nHelp the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism.\nAlways be polite and respectful, and prefer brevity over verbosity.\n\nThe user has provided you with the following context:\n\\`\\`\\`\n${contextString}\n\\`\\`\\`\n\nThey have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.\n\nPlease assist them as best you can.\n\nYou can ask them for clarifying questions if needed, but don't be annoying about it. If you can reasonably 'fill in the blanks' yourself, do so.\n\nIf you would like to call a function, call it without saying anything else.\n`;\n}\n","\"use client\";\n\nimport { FunctionCallHandler } from \"../types\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\nimport { ChatCompletionCreateParams } from \"openai/resources/chat\";\nimport { DocumentPointer } from \"../types\";\n\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * The endpoint for the chat API v2.\n */\n chatApiEndpointV2: string;\n\n /**\n * additional headers to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'Authorization': 'Bearer your_token_here'\n * }\n * ```\n */\n headers: Record<string, string>;\n\n /**\n * Additional body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'message': 'Hello, world!'\n * }\n * ```\n */\n body: Record<string, any>;\n}\n\nexport interface CopilotContextParams {\n // function-calling\n entryPoints: Record<string, AnnotatedFunction<any[]>>;\n setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;\n removeEntryPoint: (id: string) => void;\n getChatCompletionFunctionDescriptions: () => ChatCompletionCreateParams.Function[];\n getFunctionCallHandler: () => FunctionCallHandler;\n\n // text context\n addContext: (context: string, parentId?: string, categories?: string[]) => TreeNodeId;\n removeContext: (id: TreeNodeId) => void;\n getContextString: (documents: DocumentPointer[], categories: string[]) => string;\n\n // document context\n addDocumentContext: (documentPointer: DocumentPointer, categories?: string[]) => TreeNodeId;\n removeDocumentContext: (documentId: string) => void;\n getDocumentsContext: (categories: string[]) => DocumentPointer[];\n\n // api endpoints\n copilotApiConfig: CopilotApiConfig;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n entryPoints: {},\n setEntryPoint: () => {},\n removeEntryPoint: () => {},\n getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),\n getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),\n\n getContextString: (documents: DocumentPointer[], categories: string[]) =>\n returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n\n getDocumentsContext: (categories: string[]) => returnAndThrowInDebug([]),\n addDocumentContext: () => returnAndThrowInDebug(\"\"),\n removeDocumentContext: () => {},\n\n copilotApiConfig: new (class implements CopilotApiConfig {\n get chatApiEndpoint(): string {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n }\n get chatApiEndpointV2(): string {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n }\n get headers(): Record<string, string> {\n return {};\n }\n get body(): Record<string, any> {\n return {};\n }\n })(),\n};\n\nexport const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);\n\nfunction returnAndThrowInDebug<T>(value: T): T {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n return value;\n}\n","import { useState } from \"react\";\nimport { Message, Function, FunctionCallHandler } from \"../types\";\nimport { nanoid } from \"nanoid\";\nimport { ChatCompletionClient } from \"../openai/chat-completion-client\";\nimport { CopilotApiConfig } from \"../context\";\n\nexport type UseChatOptions = {\n /**\n * The API endpoint that accepts a `{ messages: Message[] }` object and returns\n * a stream of tokens of the AI chat response. Defaults to `/api/chat`.\n */\n api?: string;\n /**\n * A unique identifier for the chat. If not provided, a random one will be\n * generated. When provided, the `useChat` hook with the same `id` will\n * have shared states across components.\n */\n id?: string;\n /**\n * System messages of the chat. Defaults to an empty array.\n */\n initialMessages?: Message[];\n /**\n * Callback function to be called when a function call is received.\n * If the function returns a `ChatRequest` object, the request will be sent\n * automatically to the API and will be used to update the chat.\n */\n onFunctionCall?: FunctionCallHandler;\n /**\n * HTTP headers to be sent with the API request.\n */\n headers?: Record<string, string> | Headers;\n /**\n * Extra body object to be sent with the API request.\n * @example\n * Send a `sessionId` to the API along with the messages.\n * ```js\n * useChat({\n * body: {\n * sessionId: '123',\n * }\n * })\n * ```\n */\n body?: object;\n /**\n * Function definitions to be sent to the API.\n */\n functions?: Function[];\n};\n\nexport type UseChatHelpers = {\n /** Current messages in the chat */\n messages: Message[];\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n */\n append: (message: Message) => Promise<void>;\n /**\n * Reload the last AI chat response for the given chat history. If the last\n * message isn't from the assistant, it will request the API to generate a\n * new response.\n */\n reload: () => Promise<void>;\n /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n /** The current value of the input */\n input: string;\n /** setState-powered method to update the input value */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n /** Whether the API request is in progress */\n isLoading: boolean;\n};\n\nexport type UseChatOptionsWithCopilotConfig = UseChatOptions & {\n copilotConfig: CopilotApiConfig;\n};\n\nexport function useChat(options: UseChatOptionsWithCopilotConfig): UseChatHelpers {\n const [messages, setMessages] = useState<Message[]>([]);\n const [input, setInput] = useState(\"\");\n const [isLoading, setIsLoading] = useState(false);\n\n const runChatCompletion = async (messages: Message[]): Promise<Message> => {\n return new Promise<Message>((resolve, reject) => {\n setIsLoading(true);\n\n const assistantMessage: Message = {\n id: nanoid(),\n createdAt: new Date(),\n content: \"\",\n role: \"assistant\",\n };\n\n // Assistant messages are always copied when using setState\n setMessages([...messages, { ...assistantMessage }]);\n\n const messagesWithContext = [...(options.initialMessages || []), ...messages];\n\n const client = new ChatCompletionClient({\n url: options.api || \"/api/copilotkit/openai\",\n });\n\n const cleanup = () => {\n client.off(\"content\");\n client.off(\"end\");\n client.off(\"error\");\n client.off(\"function\");\n };\n\n client.on(\"content\", (content) => {\n assistantMessage.content += content;\n setMessages([...messages, { ...assistantMessage }]);\n });\n\n client.on(\"end\", () => {\n setIsLoading(false);\n cleanup();\n resolve({ ...assistantMessage });\n });\n\n client.on(\"error\", (error) => {\n setIsLoading(false);\n cleanup();\n reject(error);\n });\n\n client.on(\"function\", async (functionCall) => {\n assistantMessage.function_call = {\n name: functionCall.name,\n arguments: JSON.stringify(functionCall.arguments),\n };\n setMessages([...messages, { ...assistantMessage }]);\n // quit early if we get a function call\n setIsLoading(false);\n cleanup();\n resolve({ ...assistantMessage });\n });\n\n client.fetch({\n messages: messagesWithContext,\n functions: options.functions,\n headers: options.headers,\n copilotConfig: options.copilotConfig,\n });\n });\n };\n\n const runChatCompletionAndHandleFunctionCall = async (messages: Message[]): Promise<void> => {\n const message = await runChatCompletion(messages);\n if (message.function_call && options.onFunctionCall) {\n await options.onFunctionCall(messages, message.function_call);\n }\n };\n\n const append = async (message: Message): Promise<void> => {\n if (isLoading) {\n return;\n }\n const newMessages = [...messages, message];\n setMessages(newMessages);\n return runChatCompletionAndHandleFunctionCall(newMessages);\n };\n\n const reload = async (): Promise<void> => {\n if (isLoading || messages.length === 0) {\n return;\n }\n let newMessages = [...messages];\n const lastMessage = messages[messages.length - 1];\n\n if (lastMessage.role === \"assistant\") {\n newMessages = newMessages.slice(0, -1);\n }\n setMessages(newMessages);\n\n return runChatCompletionAndHandleFunctionCall(newMessages);\n };\n\n const stop = (): void => {\n throw new Error(\"Not implemented\");\n };\n\n return {\n messages,\n append,\n reload,\n stop,\n isLoading,\n input,\n setInput,\n };\n}\n","import EventEmitter from \"eventemitter3\";\nimport { Function, Message, Role } from \"../types\";\nimport {\n ChatCompletionTransport,\n ChatCompletionTransportFetchParams,\n} from \"./chat-completion-transport\";\n\ninterface ChatCompletionClientConfiguration {\n url: string;\n model?: string;\n}\n\ninterface ChatCompletionClientEvents {\n content: string;\n partial: [string, string];\n error: any;\n function: {\n name: string;\n arguments: any;\n };\n end: void;\n}\n\nexport interface ChatCompletionChunk {\n choices: {\n delta: {\n role: Role;\n content?: string | null;\n function_call?: {\n name?: string;\n arguments?: string;\n };\n };\n }[];\n}\n\nconst DEFAULT_MAX_TOKENS = 8192;\n\nexport class ChatCompletionClient extends EventEmitter<ChatCompletionClientEvents> {\n private url: string;\n private model?: string;\n\n private chatCompletionTransport: ChatCompletionTransport | null = null;\n private mode: \"function\" | \"message\" | null = null;\n private functionCallName: string = \"\";\n private functionCallArguments: string = \"\";\n\n constructor(params: ChatCompletionClientConfiguration) {\n super();\n this.model = params.model;\n this.url = params.url;\n }\n\n public async fetch(params: ChatCompletionTransportFetchParams) {\n params = { ...params };\n if (this.model && this.model in maxTokensByModel) {\n params.maxTokens ||= maxTokensByModel[this.model];\n } else {\n params.maxTokens ||= DEFAULT_MAX_TOKENS;\n }\n\n params.functions ||= [];\n params.model = this.model;\n params.messages = this.buildPrompt(params);\n return await this.runPrompt(params);\n }\n\n private buildPrompt(params: ChatCompletionTransportFetchParams): Message[] {\n let maxTokens = params.maxTokens!;\n const messages = params.messages!;\n const functions = params.functions!;\n const functionsNumTokens = countFunctionsTokens(functions);\n if (functionsNumTokens > maxTokens) {\n throw new Error(`Too many tokens in function calls: ${functionsNumTokens} > ${maxTokens}`);\n }\n maxTokens -= functionsNumTokens;\n\n for (const message of messages) {\n if (message.role === \"system\") {\n const numTokens = this.countTokens(message);\n maxTokens -= numTokens;\n\n if (maxTokens < 0) {\n throw new Error(\"Not enough tokens for system message.\");\n }\n }\n }\n\n const result: Message[] = [];\n let cutoff: boolean = false;\n\n const reversedMessages = [...messages].reverse();\n for (const message of reversedMessages) {\n if (message.role === \"system\") {\n result.unshift(message);\n continue;\n } else if (cutoff) {\n continue;\n }\n let numTokens = this.countTokens(message);\n if (maxTokens < numTokens) {\n cutoff = true;\n continue;\n }\n result.unshift(message);\n maxTokens -= numTokens;\n }\n\n return result;\n }\n\n private async runPrompt(params: ChatCompletionTransportFetchParams): Promise<void> {\n this.chatCompletionTransport = new ChatCompletionTransport({\n url: this.url,\n });\n\n this.chatCompletionTransport.on(\"data\", this.onData);\n this.chatCompletionTransport.on(\"error\", this.onError);\n this.chatCompletionTransport.on(\"end\", this.onEnd);\n\n await this.chatCompletionTransport.fetch(params);\n }\n\n private onData = (data: ChatCompletionChunk) => {\n // In case we are in a function call but the next message is not a function call, flush it.\n if (this.mode === \"function\" && !data.choices[0].delta.function_call) {\n const success = this.tryFlushFunctionCall();\n if (!success) {\n return;\n }\n }\n\n this.mode = data.choices[0].delta.function_call ? \"function\" : \"message\";\n\n if (this.mode === \"message\") {\n // if we get a message, emit the content and return;\n\n if (data.choices[0].delta.content) {\n this.emit(\"content\", data.choices[0].delta.content);\n }\n\n return;\n } else if (this.mode === \"function\") {\n // if we get a function call, we buffer the name and arguments, then emit a partial event.\n\n if (data.choices[0].delta.function_call!.name) {\n this.functionCallName = data.choices[0].delta.function_call!.name!;\n }\n if (data.choices[0].delta.function_call!.arguments) {\n this.functionCallArguments += data.choices[0].delta.function_call!.arguments!;\n }\n this.emit(\"partial\", this.functionCallName, this.functionCallArguments);\n\n return;\n }\n };\n\n private onError = (error: any) => {\n this.emit(\"error\", error);\n this.cleanup();\n };\n\n private onEnd = () => {\n if (this.mode === \"function\") {\n const success = this.tryFlushFunctionCall();\n if (!success) {\n return;\n }\n }\n this.emit(\"end\");\n this.cleanup();\n };\n\n private tryFlushFunctionCall(): boolean {\n let args: any = null;\n try {\n args = JSON.parse(this.functionCallArguments);\n } catch (error) {\n this.emit(\"error\", error);\n this.cleanup();\n return false;\n }\n this.emit(\"function\", {\n name: this.functionCallName,\n arguments: args,\n });\n this.mode = null;\n this.functionCallName = \"\";\n this.functionCallArguments = \"\";\n return true;\n }\n\n private cleanup() {\n if (this.chatCompletionTransport) {\n this.chatCompletionTransport.off(\"data\", this.onData);\n this.chatCompletionTransport.off(\"error\", this.onError);\n this.chatCompletionTransport.off(\"end\", this.onEnd);\n }\n this.chatCompletionTransport = null;\n this.mode = null;\n this.functionCallName = \"\";\n this.functionCallArguments = \"\";\n }\n\n public countTokens(message: Message): number {\n if (message.content) {\n return estimateTokens(message.content);\n } else if (message.function_call) {\n return estimateTokens(JSON.stringify(message.function_call));\n }\n return 0;\n }\n}\n\nconst maxTokensByModel: { [key: string]: number } = {\n \"gpt-3.5-turbo\": 4097,\n \"gpt-3.5-turbo-16k\": 16385,\n \"gpt-4\": 8192,\n \"gpt-4-1106-preview\": 8192,\n \"gpt-4-32k\": 32768,\n \"gpt-3.5-turbo-0301\": 4097,\n \"gpt-4-0314\": 8192,\n \"gpt-4-32k-0314\": 32768,\n \"gpt-3.5-turbo-0613\": 4097,\n \"gpt-4-0613\": 8192,\n \"gpt-4-32k-0613\": 32768,\n \"gpt-3.5-turbo-16k-0613\": 16385,\n};\n\nfunction estimateTokens(text: string): number {\n return text.length / 3;\n}\n\nfunction countFunctionsTokens(functions: Function[]): number {\n if (functions.length === 0) {\n return 0;\n }\n const json = JSON.stringify(functions);\n return estimateTokens(json);\n}\n","import EventEmitter from \"eventemitter3\";\nimport { Message, Function } from \"../types\";\nimport { CopilotApiConfig } from \"../context\";\n\nexport interface ChatCompletionTransportConfiguration {\n url: string;\n}\n\ninterface ChatCompletionTransportEvents {\n end: void;\n data: any;\n error: any;\n}\n\nexport interface ChatCompletionTransportFetchParams {\n model?: string;\n messages: Message[];\n functions?: Function[];\n temperature?: number;\n maxTokens?: number;\n headers?: Record<string, string> | Headers;\n body?: object;\n copilotConfig: CopilotApiConfig;\n signal?: AbortSignal;\n}\n\nconst DEFAULT_MODEL = \"gpt-4-1106-preview\";\n\nexport class ChatCompletionTransport extends EventEmitter<ChatCompletionTransportEvents> {\n private buffer = new Uint8Array();\n private bodyReader: ReadableStreamDefaultReader<Uint8Array> | null = null;\n private url: string;\n\n constructor({ url }: ChatCompletionTransportConfiguration) {\n super();\n this.url = url;\n }\n\n private async cleanup() {\n if (this.bodyReader) {\n try {\n await this.bodyReader.cancel();\n } catch (error) {\n console.warn(\"Failed to cancel body reader:\", error);\n }\n }\n this.bodyReader = null;\n this.buffer = new Uint8Array();\n }\n\n public async fetch({\n model,\n messages,\n copilotConfig,\n functions,\n temperature,\n headers,\n body,\n signal,\n }: ChatCompletionTransportFetchParams): Promise<void> {\n await this.cleanup();\n\n temperature ||= 0.5;\n functions ||= [];\n model ||= DEFAULT_MODEL;\n\n // clean up any extra properties from messages\n const cleanedMessages = messages.map((message) => {\n const { content, role, name, function_call } = message;\n return { content, role, name, function_call };\n });\n\n try {\n const response = await fetch(this.url, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n ...copilotConfig.headers,\n ...(headers ? { ...headers } : {}),\n },\n body: JSON.stringify({\n model,\n messages: cleanedMessages,\n stream: true,\n ...(functions.length ? { functions } : {}),\n ...(temperature ? { temperature } : {}),\n ...(functions.length != 0 ? { function_call: \"auto\" } : {}),\n ...copilotConfig.body,\n ...(body ? { ...body } : {}),\n }),\n signal,\n });\n\n if (!response.ok) {\n try {\n const errorText = await response.text();\n await this.cleanup();\n const msg = `Error ${response.status}: ${errorText}`;\n this.emit(\"error\", new Error(msg));\n } catch (_error) {\n await this.cleanup();\n const msg = `Error ${response.status}: ${response.statusText}`;\n this.emit(\"error\", new Error(msg));\n }\n return;\n }\n\n if (response.body == null) {\n await this.cleanup();\n const msg = \"Response body is null\";\n this.emit(\"error\", new Error(msg));\n return;\n }\n\n this.bodyReader = response.body.getReader();\n\n await this.streamBody();\n } catch (error) {\n await this.cleanup();\n this.emit(\"error\", error);\n return;\n }\n }\n\n private async streamBody() {\n while (true) {\n try {\n const { done, value } = await this.bodyReader!.read();\n\n if (done) {\n await this.cleanup();\n this.emit(\"end\");\n return;\n }\n\n const shouldContinue = await this.processData(value);\n\n if (!shouldContinue) {\n return;\n }\n } catch (error) {\n await this.cleanup();\n this.emit(\"error\", error);\n return;\n }\n }\n }\n\n private async processData(data: Uint8Array): Promise<boolean> {\n // Append new data to the temp buffer\n const newBuffer = new Uint8Array(this.buffer.length + data.length);\n newBuffer.set(this.buffer);\n newBuffer.set(data, this.buffer.length);\n this.buffer = newBuffer;\n\n const dataString = new TextDecoder(\"utf-8\").decode(this.buffer);\n\n let lines = dataString.split(\"\\n\").filter((line) => line.trim() !== \"\");\n\n // If the last line isn't complete, keep it in the buffer for next time\n if (!dataString.endsWith(\"\\n\")) {\n const lastLine = lines.pop() || \"\";\n const remainingBytes = new TextEncoder().encode(lastLine);\n this.buffer = new Uint8Array(remainingBytes);\n } else {\n this.buffer = new Uint8Array();\n }\n\n for (const line of lines) {\n const cleanedLine = line.replace(/^data: /, \"\");\n\n if (cleanedLine === \"[DONE]\") {\n await this.cleanup();\n this.emit(\"end\");\n return false;\n }\n\n let json;\n try {\n json = JSON.parse(cleanedLine);\n } catch (error) {\n console.error(\"Failed to parse JSON:\", error);\n continue;\n }\n\n this.emit(\"data\", json);\n }\n return true;\n }\n}\n","\"use client\";\n\nimport { useCallback, useState } from \"react\";\nimport { CopilotContext, CopilotApiConfig } from \"../../context/copilot-context\";\nimport useTree from \"../../hooks/use-tree\";\nimport { AnnotatedFunction } from \"../../types/annotated-function\";\nimport { ChatCompletionCreateParams } from \"openai/resources/chat\";\nimport { DocumentPointer, FunctionCallHandler } from \"../../types\";\nimport useFlatCategoryStore from \"../../hooks/use-flat-category-store\";\nimport { StandardCopilotApiConfig } from \"./standard-copilot-api-config\";\nimport { CopilotProviderProps } from \"./copilot-provider-props\";\n\n/**\n * The CopilotProvider component.\n * This component provides the Copilot context to its children.\n * It can be configured either with a chat API endpoint or a CopilotApiConfig.\n *\n * NOTE: The backend can use OpenAI, or you can bring your own LLM.\n * For examples of the backend api implementation, see `examples/next-openai` usage (under `src/api/copilotkit`),\n * or read the documentation at https://docs.copilotkit.ai\n * In particular, Getting-Started > Quickstart-Backend: https://docs.copilotkit.ai/getting-started/quickstart-backend\n *\n * Example usage:\n * ```\n * <CopilotProvider chatApiEndpoint=\"https://your.copilotkit.api\">\n * <App />\n * </CopilotProvider>\n * ```\n *\n * or\n *\n * ```\n * const copilotApiConfig = new StandardCopilotApiConfig(\n * \"https://your.copilotkit.api/v1\",\n * \"https://your.copilotkit.api/v2\",\n * {},\n * {}\n * );\n *\n * // ...\n *\n * <CopilotProvider chatApiConfig={copilotApiConfig}>\n * <App />\n * </CopilotProvider>\n * ```\n *\n * @param props - The props for the component.\n * @returns The CopilotProvider component.\n */\nexport function CopilotProvider({ children, ...props }: CopilotProviderProps): JSX.Element {\n // Compute all the functions and properties that we need to pass\n // to the CopilotContext.\n\n const [entryPoints, setEntryPoints] = useState<Record<string, AnnotatedFunction<any[]>>>({});\n\n const { addElement, removeElement, printTree } = useTree();\n\n const {\n addElement: addDocument,\n removeElement: removeDocument,\n allElements: allDocuments,\n } = useFlatCategoryStore<DocumentPointer>();\n\n const setEntryPoint = useCallback((id: string, entryPoint: AnnotatedFunction<any[]>) => {\n setEntryPoints((prevPoints) => {\n return {\n ...prevPoints,\n [id]: entryPoint,\n };\n });\n }, []);\n\n const removeEntryPoint = useCallback((id: string) => {\n setEntryPoints((prevPoints) => {\n const newPoints = { ...prevPoints };\n delete newPoints[id];\n return newPoints;\n });\n }, []);\n\n const getContextString = useCallback(\n (documents: DocumentPointer[], categories: string[]) => {\n const documentsString = documents\n .map((document) => {\n return `${document.name} (${document.sourceApplication}):\\n${document.getContents()}`;\n })\n .join(\"\\n\\n\");\n\n const nonDocumentStrings = printTree(categories);\n\n return `${documentsString}\\n\\n${nonDocumentStrings}`;\n },\n [printTree],\n );\n\n const addContext = useCallback(\n (\n context: string,\n parentId?: string,\n categories: string[] = defaultCopilotContextCategories,\n ) => {\n return addElement(context, categories, parentId);\n },\n [addElement],\n );\n\n const removeContext = useCallback(\n (id: string) => {\n removeElement(id);\n },\n [removeElement],\n );\n\n const getChatCompletionFunctionDescriptions = useCallback(() => {\n return entryPointsToChatCompletionFunctions(Object.values(entryPoints));\n }, [entryPoints]);\n\n const getFunctionCallHandler = useCallback(() => {\n return entryPointsToFunctionCallHandler(Object.values(entryPoints));\n }, [entryPoints]);\n\n const getDocumentsContext = useCallback(\n (categories: string[]) => {\n return allDocuments(categories);\n },\n [allDocuments],\n );\n\n const addDocumentContext = useCallback(\n (documentPointer: DocumentPointer, categories: string[] = defaultCopilotContextCategories) => {\n return addDocument(documentPointer, categories);\n },\n [addDocument],\n );\n\n const removeDocumentContext = useCallback(\n (documentId: string) => {\n removeDocument(documentId);\n },\n [removeDocument],\n );\n\n // get the appropriate CopilotApiConfig from the props\n let copilotApiConfig: CopilotApiConfig;\n if (\"chatApiEndpoint\" in props) {\n copilotApiConfig = new StandardCopilotApiConfig(\n props.chatApiEndpoint,\n props.chatApiEndpointV2 || `${props.chatApiEndpoint}/v2`,\n {},\n {},\n );\n } else {\n copilotApiConfig = props.chatApiConfig;\n }\n\n return (\n <CopilotContext.Provider\n value={{\n entryPoints,\n getChatCompletionFunctionDescriptions,\n getFunctionCallHandler,\n setEntryPoint,\n removeEntryPoint,\n getContextString,\n addContext,\n removeContext,\n getDocumentsContext,\n addDocumentContext,\n removeDocumentContext,\n copilotApiConfig: copilotApiConfig,\n }}\n >\n {children}\n </CopilotContext.Provider>\n );\n}\n\nexport const defaultCopilotContextCategories = [\"global\"];\n\nfunction entryPointsToFunctionCallHandler(\n entryPoints: AnnotatedFunction<any[]>[],\n): FunctionCallHandler {\n return async (chatMessages, functionCall) => {\n let entrypointsByFunctionName: Record<string, AnnotatedFunction<any[]>> = {};\n for (let entryPoint of entryPoints) {\n entrypointsByFunctionName[entryPoint.name] = entryPoint;\n }\n\n const entryPointFunction = entrypointsByFunctionName[functionCall.name || \"\"];\n if (entryPointFunction) {\n let functionCallArguments: Record<string, any>[] = [];\n if (functionCall.arguments) {\n functionCallArguments = JSON.parse(functionCall.arguments);\n }\n\n const paramsInCorrectOrder: any[] = [];\n for (let arg of entryPointFunction.argumentAnnotations) {\n paramsInCorrectOrder.push(\n functionCallArguments[arg.name as keyof typeof functionCallArguments],\n );\n }\n\n await entryPointFunction.implementation(...paramsInCorrectOrder);\n\n // commented out becasue for now we don't want to return anything\n // const result = await entryPointFunction.implementation(\n // ...parsedFunctionCallArguments\n // );\n // const functionResponse: ChatRequest = {\n // messages: [\n // ...chatMessages,\n // {\n // id: nanoid(),\n // name: functionCall.name,\n // role: 'function' as const,\n // content: JSON.stringify(result),\n // },\n // ],\n // };\n\n // return functionResponse;\n }\n };\n}\n\nfunction entryPointsToChatCompletionFunctions(\n entryPoints: AnnotatedFunction<any[]>[],\n): ChatCompletionCreateParams.Function[] {\n return entryPoints.map(annotatedFunctionToChatCompletionFunction);\n}\n\nfunction annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): ChatCompletionCreateParams.Function {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let arg of annotatedFunction.argumentAnnotations) {\n // isolate the args we should forward inline\n let { name, required, ...forwardedArgs } = arg;\n parameters[arg.name] = forwardedArgs;\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n if (arg.required) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ChatCompletionCreateParams.Function = {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n };\n\n return chatCompletionFunction;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,SAAS,kBAAkB;;;ACGpC,OAAO,WAAW;AAmElB,IAAM,sBAA4C;AAAA,EAChD,aAAa,CAAC;AAAA,EACd,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,uCAAuC,MAAM,sBAAsB,CAAC,CAAC;AAAA,EACrE,wBAAwB,MAAM,sBAAsB,MAAY;AAAA,EAAC,EAAC;AAAA,EAElE,kBAAkB,CAAC,WAA8B,eAC/C,sBAAsB,EAAE;AAAA,EAC1B,YAAY,MAAM;AAAA,EAClB,eAAe,MAAM;AAAA,EAAC;AAAA,EAEtB,qBAAqB,CAAC,eAAyB,sBAAsB,CAAC,CAAC;AAAA,EACvE,oBAAoB,MAAM,sBAAsB,EAAE;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AO,QAAQ,mBAAmB,CAAC,CAAC;AAAA,IACrE,WAAW;AAAA,IACX,gBAAgB,uBAAuB;AAAA,IACvC,SAAS,mBAAK,QAAQ;AAAA,IACtB,MAAM,mBACD,QAAQ;AAAA,EAEf,EAAC;AAED,QAAM,kBAAkB,SAAS;AAAA,IAC/B,CAAC,YAAY,QAAQ,SAAS,UAAU,QAAQ,SAAS;AAAA,EAC3D;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEO,SAAS,qBAAqB,eAA+B;AAClE,SAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWF;","names":["EventEmitter","EventEmitter","messages","useState"]}
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -1,69 +1,4 @@
1
- // src/hooks/use-flat-category-store.ts
2
- import { nanoid } from "nanoid";
3
- import { useCallback, useReducer } from "react";
4
- var useFlatCategoryStore = () => {
5
- const [elements, dispatch] = useReducer(flatCategoryStoreReducer, /* @__PURE__ */ new Map());
6
- const addElement = useCallback((value, categories) => {
7
- const newId = nanoid();
8
- dispatch({
9
- type: "ADD_ELEMENT",
10
- value,
11
- id: newId,
12
- categories
13
- });
14
- return newId;
15
- }, []);
16
- const removeElement = useCallback((id) => {
17
- dispatch({ type: "REMOVE_ELEMENT", id });
18
- }, []);
19
- const allElements = useCallback(
20
- (categories) => {
21
- const categoriesSet = new Set(categories);
22
- const result = [];
23
- elements.forEach((element) => {
24
- if (setsHaveIntersection(categoriesSet, element.categories)) {
25
- result.push(element.value);
26
- }
27
- });
28
- return result;
29
- },
30
- [elements]
31
- );
32
- return { addElement, removeElement, allElements };
33
- };
34
- var use_flat_category_store_default = useFlatCategoryStore;
35
- function flatCategoryStoreReducer(state, action) {
36
- switch (action.type) {
37
- case "ADD_ELEMENT": {
38
- const { value, id, categories } = action;
39
- const newElement = {
40
- id,
41
- value,
42
- categories: new Set(categories)
43
- };
44
- const newState = new Map(state);
45
- newState.set(id, newElement);
46
- return newState;
47
- }
48
- case "REMOVE_ELEMENT": {
49
- const newState = new Map(state);
50
- newState.delete(action.id);
51
- return newState;
52
- }
53
- default:
54
- return state;
55
- }
56
- }
57
- function setsHaveIntersection(setA, setB) {
58
- const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];
59
- for (let item of smallerSet) {
60
- if (largerSet.has(item)) {
61
- return true;
62
- }
63
- }
64
- return false;
65
- }
66
- export {
67
- use_flat_category_store_default as default
68
- };
1
+ export { use_flat_category_store_default as default } from '../chunk-YULKJPY3.mjs';
2
+ import '../chunk-MRXNTQOX.mjs';
3
+ //# sourceMappingURL=out.js.map
69
4
  //# sourceMappingURL=use-flat-category-store.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hooks/use-flat-category-store.ts"],"sourcesContent":["import { nanoid } from \"nanoid\";\nimport { useCallback, useReducer } from \"react\";\n\nexport type FlatCategoryStoreId = string;\n\nexport interface UseFlatCategoryStoreReturn<T> {\n addElement: (value: T, categories: string[]) => FlatCategoryStoreId;\n removeElement: (id: FlatCategoryStoreId) => void;\n allElements: (categories: string[]) => T[];\n}\n\ninterface FlatCategoryStoreElement<T> {\n id: FlatCategoryStoreId;\n value: T;\n categories: Set<string>;\n}\n\nconst useFlatCategoryStore = <T>(): UseFlatCategoryStoreReturn<T> => {\n const [elements, dispatch] = useReducer<\n React.Reducer<Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>, Action<T>>\n >(flatCategoryStoreReducer, new Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>());\n\n const addElement = useCallback((value: T, categories: string[]): FlatCategoryStoreId => {\n const newId = nanoid();\n dispatch({\n type: \"ADD_ELEMENT\",\n value,\n id: newId,\n categories,\n });\n return newId;\n }, []);\n\n const removeElement = useCallback((id: FlatCategoryStoreId): void => {\n dispatch({ type: \"REMOVE_ELEMENT\", id });\n }, []);\n\n const allElements = useCallback(\n (categories: string[]): T[] => {\n const categoriesSet = new Set(categories);\n const result: T[] = [];\n elements.forEach((element) => {\n if (setsHaveIntersection(categoriesSet, element.categories)) {\n result.push(element.value);\n }\n });\n return result;\n },\n [elements],\n );\n\n return { addElement, removeElement, allElements };\n};\n\nexport default useFlatCategoryStore;\n\n// Action types\ntype Action<T> =\n | {\n type: \"ADD_ELEMENT\";\n value: T;\n id: FlatCategoryStoreId;\n categories: string[];\n }\n | { type: \"REMOVE_ELEMENT\"; id: FlatCategoryStoreId };\n\n// Reducer\nfunction flatCategoryStoreReducer<T>(\n state: Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>>,\n action: Action<T>,\n): Map<FlatCategoryStoreId, FlatCategoryStoreElement<T>> {\n switch (action.type) {\n case \"ADD_ELEMENT\": {\n const { value, id, categories } = action;\n const newElement: FlatCategoryStoreElement<T> = {\n id,\n value,\n categories: new Set(categories),\n };\n const newState = new Map(state);\n newState.set(id, newElement);\n return newState;\n }\n case \"REMOVE_ELEMENT\": {\n const newState = new Map(state);\n newState.delete(action.id);\n return newState;\n }\n default:\n return state;\n }\n}\n\nfunction setsHaveIntersection<T>(setA: Set<T>, setB: Set<T>): boolean {\n const [smallerSet, largerSet] = setA.size <= setB.size ? [setA, setB] : [setB, setA];\n\n for (let item of smallerSet) {\n if (largerSet.has(item)) {\n return true;\n }\n }\n\n return false;\n}\n"],"mappings":";AAAA,SAAS,cAAc;AACvB,SAAS,aAAa,kBAAkB;AAgBxC,IAAM,uBAAuB,MAAwC;AACnE,QAAM,CAAC,UAAU,QAAQ,IAAI,WAE3B,0BAA0B,oBAAI,IAAsD,CAAC;AAEvF,QAAM,aAAa,YAAY,CAAC,OAAU,eAA8C;AACtF,UAAM,QAAQ,OAAO;AACrB,aAAS;AAAA,MACP,MAAM;AAAA,MACN;AAAA,MACA,IAAI;AAAA,MACJ;AAAA,IACF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T;","names":[]}
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -1,96 +1,5 @@
1
- "use client";
2
- var __async = (__this, __arguments, generator) => {
3
- return new Promise((resolve, reject) => {
4
- var fulfilled = (value) => {
5
- try {
6
- step(generator.next(value));
7
- } catch (e) {
8
- reject(e);
9
- }
10
- };
11
- var rejected = (value) => {
12
- try {
13
- step(generator.throw(value));
14
- } catch (e) {
15
- reject(e);
16
- }
17
- };
18
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
19
- step((generator = generator.apply(__this, __arguments)).next());
20
- });
21
- };
22
-
23
- // src/hooks/use-make-copilot-actionable.ts
24
- import { useRef, useContext, useEffect, useMemo } from "react";
25
-
26
- // src/context/copilot-context.tsx
27
- import React from "react";
28
- var emptyCopilotContext = {
29
- entryPoints: {},
30
- setEntryPoint: () => {
31
- },
32
- removeEntryPoint: () => {
33
- },
34
- getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),
35
- getFunctionCallHandler: () => returnAndThrowInDebug(() => __async(void 0, null, function* () {
36
- })),
37
- getContextString: (documents, categories) => returnAndThrowInDebug(""),
38
- addContext: () => "",
39
- removeContext: () => {
40
- },
41
- getDocumentsContext: (categories) => returnAndThrowInDebug([]),
42
- addDocumentContext: () => returnAndThrowInDebug(""),
43
- removeDocumentContext: () => {
44
- },
45
- copilotApiConfig: new class {
46
- get chatApiEndpoint() {
47
- throw new Error(
48
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
49
- );
50
- }
51
- get chatApiEndpointV2() {
52
- throw new Error(
53
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
54
- );
55
- }
56
- get headers() {
57
- return {};
58
- }
59
- get body() {
60
- return {};
61
- }
62
- }()
63
- };
64
- var CopilotContext = React.createContext(emptyCopilotContext);
65
- function returnAndThrowInDebug(value) {
66
- throw new Error(
67
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
68
- );
69
- return value;
70
- }
71
-
72
- // src/hooks/use-make-copilot-actionable.ts
73
- import { nanoid } from "nanoid";
74
- function useMakeCopilotActionable(annotatedFunction, dependencies) {
75
- const idRef = useRef(nanoid());
76
- const { setEntryPoint, removeEntryPoint } = useContext(CopilotContext);
77
- const memoizedAnnotatedFunction = useMemo(
78
- () => ({
79
- name: annotatedFunction.name,
80
- description: annotatedFunction.description,
81
- argumentAnnotations: annotatedFunction.argumentAnnotations,
82
- implementation: annotatedFunction.implementation
83
- }),
84
- dependencies
85
- );
86
- useEffect(() => {
87
- setEntryPoint(idRef.current, memoizedAnnotatedFunction);
88
- return () => {
89
- removeEntryPoint(idRef.current);
90
- };
91
- }, [memoizedAnnotatedFunction, setEntryPoint, removeEntryPoint]);
92
- }
93
- export {
94
- useMakeCopilotActionable
95
- };
1
+ export { useMakeCopilotActionable } from '../chunk-FP4EXCGS.mjs';
2
+ import '../chunk-6A4PCNMZ.mjs';
3
+ import '../chunk-MRXNTQOX.mjs';
4
+ //# sourceMappingURL=out.js.map
96
5
  //# sourceMappingURL=use-make-copilot-actionable.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hooks/use-make-copilot-actionable.ts","../../src/context/copilot-context.tsx"],"sourcesContent":["\"use client\";\n\nimport { useRef, useContext, useEffect, useMemo } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\nimport { nanoid } from \"nanoid\";\n\nexport function useMakeCopilotActionable<ActionInput extends any[]>(\n annotatedFunction: AnnotatedFunction<ActionInput>,\n dependencies: any[],\n) {\n const idRef = useRef(nanoid()); // generate a unique id\n const { setEntryPoint, removeEntryPoint } = useContext(CopilotContext);\n\n const memoizedAnnotatedFunction: AnnotatedFunction<ActionInput> = useMemo(\n () => ({\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n argumentAnnotations: annotatedFunction.argumentAnnotations,\n implementation: annotatedFunction.implementation,\n }),\n dependencies,\n );\n\n useEffect(() => {\n setEntryPoint(idRef.current, memoizedAnnotatedFunction as AnnotatedFunction<any[]>);\n\n return () => {\n removeEntryPoint(idRef.current);\n };\n }, [memoizedAnnotatedFunction, setEntryPoint, removeEntryPoint]);\n}\n","\"use client\";\n\nimport { FunctionCallHandler } from \"../types\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\nimport { ChatCompletionCreateParams } from \"openai/resources/chat\";\nimport { DocumentPointer } from \"../types\";\n\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * The endpoint for the chat API v2.\n */\n chatApiEndpointV2: string;\n\n /**\n * additional headers to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'Authorization': 'Bearer your_token_here'\n * }\n * ```\n */\n headers: Record<string, string>;\n\n /**\n * Additional body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'message': 'Hello, world!'\n * }\n * ```\n */\n body: Record<string, any>;\n}\n\nexport interface CopilotContextParams {\n // function-calling\n entryPoints: Record<string, AnnotatedFunction<any[]>>;\n setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;\n removeEntryPoint: (id: string) => void;\n getChatCompletionFunctionDescriptions: () => ChatCompletionCreateParams.Function[];\n getFunctionCallHandler: () => FunctionCallHandler;\n\n // text context\n addContext: (context: string, parentId?: string, categories?: string[]) => TreeNodeId;\n removeContext: (id: TreeNodeId) => void;\n getContextString: (documents: DocumentPointer[], categories: string[]) => string;\n\n // document context\n addDocumentContext: (documentPointer: DocumentPointer, categories?: string[]) => TreeNodeId;\n removeDocumentContext: (documentId: string) => void;\n getDocumentsContext: (categories: string[]) => DocumentPointer[];\n\n // api endpoints\n copilotApiConfig: CopilotApiConfig;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n entryPoints: {},\n setEntryPoint: () => {},\n removeEntryPoint: () => {},\n getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),\n getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),\n\n getContextString: (documents: DocumentPointer[], categories: string[]) =>\n returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n\n getDocumentsContext: (categories: string[]) => returnAndThrowInDebug([]),\n addDocumentContext: () => returnAndThrowInDebug(\"\"),\n removeDocumentContext: () => {},\n\n copilotApiConfig: new (class implements CopilotApiConfig {\n get chatApiEndpoint(): string {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n }\n get chatApiEndpointV2(): string {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n }\n get headers(): Record<string, string> {\n return {};\n }\n get body(): Record<string, any> {\n return {};\n }\n })(),\n};\n\nexport const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);\n\nfunction returnAndThrowInDebug<T>(value: T): T {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n return value;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAEA,SAAS,QAAQ,YAAY,WAAW,eAAe;;;ACCvD,OAAO,WAAW;AAmElB,IAAM,sBAA4C;AAAA,EAChD,aAAa,CAAC;AAAA,EACd,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,uCAAuC,MAAM,sBAAsB,CAAC,CAAC;AAAA,EACrE,wBAAwB,MAAM,sBAAsB,MAAY;AAAA,EAAC,EAAC;AAAA,EAElE,kBAAkB,CAAC,WAA8B,eAC/C,sBAAsB,EAAE;AAAA,EAC1B,YAAY,MAAM;AAAA,EAClB,eAAe,MAAM;AAAA,EAAC;AAAA,EAEtB,qBAAqB,CAAC,eAAyB,sBAAsB,CAAC,CAAC;AAAA,EACvE,oBAAoB,MAAM,sBAAsB,EAAE;AAAA,EAClD,uBAAuB,MAAM;AAAA,EAAC;AAAA,EAE9B,kBAAkB,IAAK,MAAkC;AAAA,IACvD,IAAI,kBAA0B;AAC5B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,IACA,IAAI,oBAA4B;AAC9B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,IACA,IAAI,UAAkC;AACpC,aAAO,CAAC;AAAA,IACV;AAAA,IACA,IAAI,OAA4B;AAC9B,aAAO,CAAC;AAAA,IACV;AAAA,EACF,EAAG;AACL;AAEO,IAAM,iBAAiB,MAAM,cAAoC,mBAAmB;AAE3F,SAAS,sBAAyB,OAAa;AAC7C,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACA,SAAO;AACT;;;AD5GA,SAAS,cAAc;AAEhB,SAAS,yBACd,mBACA,cACA;AACA,QAAM,QAAQ,OAAO,OAAO,CAAC;AAC7B,QAAM,EAAE,eAAe,iBAAiB,IAAI,WAAW,cAAc;AAErE,QAAM,4BAA4D;AAAA,IAChE,OAAO;AAAA,MACL,MAAM,kBAAkB;AAAA,MACxB,aAAa,kBAAkB;AAAA,MAC/B,qBAAqB,kBAAkB;AAAA,MACvC,gBAAgB,kBAAkB;AAAA,IACpC;AAAA,IACA;AAAA,EACF;AAEA,YAAU,MAAM;AACd,kBAAc,MAAM,SAAS,yBAAqD;AAElF,WAAO,MAAM;AACX,uBAAiB,MAAM,OAAO;AAAA,IAChC;AAAA,EACF,GAAG,CAAC,2BAA2B,eAAe,gBAAgB,CAAC;AACjE;","names":[]}
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -1,88 +1,5 @@
1
- "use client";
2
- var __async = (__this, __arguments, generator) => {
3
- return new Promise((resolve, reject) => {
4
- var fulfilled = (value) => {
5
- try {
6
- step(generator.next(value));
7
- } catch (e) {
8
- reject(e);
9
- }
10
- };
11
- var rejected = (value) => {
12
- try {
13
- step(generator.throw(value));
14
- } catch (e) {
15
- reject(e);
16
- }
17
- };
18
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
19
- step((generator = generator.apply(__this, __arguments)).next());
20
- });
21
- };
22
-
23
- // src/hooks/use-make-copilot-document-readable.ts
24
- import { useContext, useEffect, useRef } from "react";
25
-
26
- // src/context/copilot-context.tsx
27
- import React from "react";
28
- var emptyCopilotContext = {
29
- entryPoints: {},
30
- setEntryPoint: () => {
31
- },
32
- removeEntryPoint: () => {
33
- },
34
- getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),
35
- getFunctionCallHandler: () => returnAndThrowInDebug(() => __async(void 0, null, function* () {
36
- })),
37
- getContextString: (documents, categories) => returnAndThrowInDebug(""),
38
- addContext: () => "",
39
- removeContext: () => {
40
- },
41
- getDocumentsContext: (categories) => returnAndThrowInDebug([]),
42
- addDocumentContext: () => returnAndThrowInDebug(""),
43
- removeDocumentContext: () => {
44
- },
45
- copilotApiConfig: new class {
46
- get chatApiEndpoint() {
47
- throw new Error(
48
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
49
- );
50
- }
51
- get chatApiEndpointV2() {
52
- throw new Error(
53
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
54
- );
55
- }
56
- get headers() {
57
- return {};
58
- }
59
- get body() {
60
- return {};
61
- }
62
- }()
63
- };
64
- var CopilotContext = React.createContext(emptyCopilotContext);
65
- function returnAndThrowInDebug(value) {
66
- throw new Error(
67
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
68
- );
69
- return value;
70
- }
71
-
72
- // src/hooks/use-make-copilot-document-readable.ts
73
- function useMakeCopilotDocumentReadable(document, categories, dependencies = []) {
74
- const { addDocumentContext, removeDocumentContext } = useContext(CopilotContext);
75
- const idRef = useRef();
76
- useEffect(() => {
77
- const id = addDocumentContext(document, categories);
78
- idRef.current = id;
79
- return () => {
80
- removeDocumentContext(id);
81
- };
82
- }, [addDocumentContext, removeDocumentContext, ...dependencies]);
83
- return idRef.current;
84
- }
85
- export {
86
- useMakeCopilotDocumentReadable
87
- };
1
+ export { useMakeCopilotDocumentReadable } from '../chunk-7P4OYNP3.mjs';
2
+ import '../chunk-6A4PCNMZ.mjs';
3
+ import '../chunk-MRXNTQOX.mjs';
4
+ //# sourceMappingURL=out.js.map
88
5
  //# sourceMappingURL=use-make-copilot-document-readable.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hooks/use-make-copilot-document-readable.ts","../../src/context/copilot-context.tsx"],"sourcesContent":["\"use client\";\n\nimport { useContext, useEffect, useRef } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { DocumentPointer } from \"../types\";\n\n/**\n * Makes a document readable by Copilot.\n * @param document The document to make readable.\n * @param categories The categories to associate with the document.\n * @param dependencies The dependencies to use for the effect.\n * @returns The id of the document.\n */\nexport function useMakeCopilotDocumentReadable(\n document: DocumentPointer,\n categories?: string[],\n dependencies: any[] = [],\n): string | undefined {\n const { addDocumentContext, removeDocumentContext } = useContext(CopilotContext);\n const idRef = useRef<string>();\n\n useEffect(() => {\n const id = addDocumentContext(document, categories);\n idRef.current = id;\n\n return () => {\n removeDocumentContext(id);\n };\n }, [addDocumentContext, removeDocumentContext, ...dependencies]);\n\n return idRef.current;\n}\n","\"use client\";\n\nimport { FunctionCallHandler } from \"../types\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\nimport { ChatCompletionCreateParams } from \"openai/resources/chat\";\nimport { DocumentPointer } from \"../types\";\n\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * The endpoint for the chat API v2.\n */\n chatApiEndpointV2: string;\n\n /**\n * additional headers to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'Authorization': 'Bearer your_token_here'\n * }\n * ```\n */\n headers: Record<string, string>;\n\n /**\n * Additional body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'message': 'Hello, world!'\n * }\n * ```\n */\n body: Record<string, any>;\n}\n\nexport interface CopilotContextParams {\n // function-calling\n entryPoints: Record<string, AnnotatedFunction<any[]>>;\n setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;\n removeEntryPoint: (id: string) => void;\n getChatCompletionFunctionDescriptions: () => ChatCompletionCreateParams.Function[];\n getFunctionCallHandler: () => FunctionCallHandler;\n\n // text context\n addContext: (context: string, parentId?: string, categories?: string[]) => TreeNodeId;\n removeContext: (id: TreeNodeId) => void;\n getContextString: (documents: DocumentPointer[], categories: string[]) => string;\n\n // document context\n addDocumentContext: (documentPointer: DocumentPointer, categories?: string[]) => TreeNodeId;\n removeDocumentContext: (documentId: string) => void;\n getDocumentsContext: (categories: string[]) => DocumentPointer[];\n\n // api endpoints\n copilotApiConfig: CopilotApiConfig;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n entryPoints: {},\n setEntryPoint: () => {},\n removeEntryPoint: () => {},\n getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),\n getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),\n\n getContextString: (documents: DocumentPointer[], categories: string[]) =>\n returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n\n getDocumentsContext: (categories: string[]) => returnAndThrowInDebug([]),\n addDocumentContext: () => returnAndThrowInDebug(\"\"),\n removeDocumentContext: () => {},\n\n copilotApiConfig: new (class implements CopilotApiConfig {\n get chatApiEndpoint(): string {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n }\n get chatApiEndpointV2(): string {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n }\n get headers(): Record<string, string> {\n return {};\n }\n get body(): Record<string, any> {\n return {};\n }\n })(),\n};\n\nexport const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);\n\nfunction returnAndThrowInDebug<T>(value: T): T {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n return value;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAEA,SAAS,YAAY,WAAW,cAAc;;;ACC9C,OAAO,WAAW;AAmElB,IAAM,sBAA4C;AAAA,EAChD,aAAa,CAAC;AAAA,EACd,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,uCAAuC,MAAM,sBAAsB,CAAC,CAAC;AAAA,EACrE,wBAAwB,MAAM,sBAAsB,MAAY;AAAA,EAAC,EAAC;AAAA,EAElE,kBAAkB,CAAC,WAA8B,eAC/C,sBAAsB,EAAE;AAAA,EAC1B,YAAY,MAAM;AAAA,EAClB,eAAe,MAAM;AAAA,EAAC;AAAA,EAEtB,qBAAqB,CAAC,eAAyB,sBAAsB,CAAC,CAAC;AAAA,EACvE,oBAAoB,MAAM,sBAAsB,EAAE;AAAA,EAClD,uBAAuB,MAAM;AAAA,EAAC;AAAA,EAE9B,kBAAkB,IAAK,MAAkC;AAAA,IACvD,IAAI,kBAA0B;AAC5B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,IACA,IAAI,oBAA4B;AAC9B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,IACA,IAAI,UAAkC;AACpC,aAAO,CAAC;AAAA,IACV;AAAA,IACA,IAAI,OAA4B;AAC9B,aAAO,CAAC;AAAA,IACV;AAAA,EACF,EAAG;AACL;AAEO,IAAM,iBAAiB,MAAM,cAAoC,mBAAmB;AAE3F,SAAS,sBAAyB,OAAa;AAC7C,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACA,SAAO;AACT;;;ADpGO,SAAS,+BACd,UACA,YACA,eAAsB,CAAC,GACH;AACpB,QAAM,EAAE,oBAAoB,sBAAsB,IAAI,WAAW,cAAc;AAC/E,QAAM,QAAQ,OAAe;AAE7B,YAAU,MAAM;AACd,UAAM,KAAK,mBAAmB,UAAU,UAAU;AAClD,UAAM,UAAU;AAEhB,WAAO,MAAM;AACX,4BAAsB,EAAE;AAAA,IAC1B;AAAA,EACF,GAAG,CAAC,oBAAoB,uBAAuB,GAAG,YAAY,CAAC;AAE/D,SAAO,MAAM;AACf;","names":[]}
1
+ {"version":3,"sources":[],"names":[],"mappings":""}
@@ -1,88 +1,5 @@
1
- "use client";
2
- var __async = (__this, __arguments, generator) => {
3
- return new Promise((resolve, reject) => {
4
- var fulfilled = (value) => {
5
- try {
6
- step(generator.next(value));
7
- } catch (e) {
8
- reject(e);
9
- }
10
- };
11
- var rejected = (value) => {
12
- try {
13
- step(generator.throw(value));
14
- } catch (e) {
15
- reject(e);
16
- }
17
- };
18
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
19
- step((generator = generator.apply(__this, __arguments)).next());
20
- });
21
- };
22
-
23
- // src/hooks/use-make-copilot-readable.ts
24
- import { useContext, useEffect, useRef } from "react";
25
-
26
- // src/context/copilot-context.tsx
27
- import React from "react";
28
- var emptyCopilotContext = {
29
- entryPoints: {},
30
- setEntryPoint: () => {
31
- },
32
- removeEntryPoint: () => {
33
- },
34
- getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),
35
- getFunctionCallHandler: () => returnAndThrowInDebug(() => __async(void 0, null, function* () {
36
- })),
37
- getContextString: (documents, categories) => returnAndThrowInDebug(""),
38
- addContext: () => "",
39
- removeContext: () => {
40
- },
41
- getDocumentsContext: (categories) => returnAndThrowInDebug([]),
42
- addDocumentContext: () => returnAndThrowInDebug(""),
43
- removeDocumentContext: () => {
44
- },
45
- copilotApiConfig: new class {
46
- get chatApiEndpoint() {
47
- throw new Error(
48
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
49
- );
50
- }
51
- get chatApiEndpointV2() {
52
- throw new Error(
53
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
54
- );
55
- }
56
- get headers() {
57
- return {};
58
- }
59
- get body() {
60
- return {};
61
- }
62
- }()
63
- };
64
- var CopilotContext = React.createContext(emptyCopilotContext);
65
- function returnAndThrowInDebug(value) {
66
- throw new Error(
67
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
68
- );
69
- return value;
70
- }
71
-
72
- // src/hooks/use-make-copilot-readable.ts
73
- function useMakeCopilotReadable(information, parentId, categories) {
74
- const { addContext, removeContext } = useContext(CopilotContext);
75
- const idRef = useRef();
76
- useEffect(() => {
77
- const id = addContext(information, parentId, categories);
78
- idRef.current = id;
79
- return () => {
80
- removeContext(id);
81
- };
82
- }, [information, parentId, addContext, removeContext]);
83
- return idRef.current;
84
- }
85
- export {
86
- useMakeCopilotReadable
87
- };
1
+ export { useMakeCopilotReadable } from '../chunk-HW5IH6PV.mjs';
2
+ import '../chunk-6A4PCNMZ.mjs';
3
+ import '../chunk-MRXNTQOX.mjs';
4
+ //# sourceMappingURL=out.js.map
88
5
  //# sourceMappingURL=use-make-copilot-readable.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hooks/use-make-copilot-readable.ts","../../src/context/copilot-context.tsx"],"sourcesContent":["\"use client\";\n\nimport { useContext, useEffect, useRef } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\n\n/**\n * Adds the given information to the Copilot context to make it readable by Copilot.\n * @param information - The information to be added to the Copilot context.\n * @param parentId - The ID of the parent context, if any.\n * @param categories - An array of categories to control which context are visible where. Particularly useful with CopilotTextarea (see `useMakeAutosuggestionFunction`)\n * @returns The ID of the added context.\n */\nexport function useMakeCopilotReadable(\n information: string,\n parentId?: string,\n categories?: string[],\n): string | undefined {\n const { addContext, removeContext } = useContext(CopilotContext);\n const idRef = useRef<string>();\n\n useEffect(() => {\n const id = addContext(information, parentId, categories);\n idRef.current = id;\n\n return () => {\n removeContext(id);\n };\n }, [information, parentId, addContext, removeContext]);\n\n return idRef.current;\n}\n","\"use client\";\n\nimport { FunctionCallHandler } from \"../types\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\nimport { ChatCompletionCreateParams } from \"openai/resources/chat\";\nimport { DocumentPointer } from \"../types\";\n\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * The endpoint for the chat API v2.\n */\n chatApiEndpointV2: string;\n\n /**\n * additional headers to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'Authorization': 'Bearer your_token_here'\n * }\n * ```\n */\n headers: Record<string, string>;\n\n /**\n * Additional body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'message': 'Hello, world!'\n * }\n * ```\n */\n body: Record<string, any>;\n}\n\nexport interface CopilotContextParams {\n // function-calling\n entryPoints: Record<string, AnnotatedFunction<any[]>>;\n setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;\n removeEntryPoint: (id: string) => void;\n getChatCompletionFunctionDescriptions: () => ChatCompletionCreateParams.Function[];\n getFunctionCallHandler: () => FunctionCallHandler;\n\n // text context\n addContext: (context: string, parentId?: string, categories?: string[]) => TreeNodeId;\n removeContext: (id: TreeNodeId) => void;\n getContextString: (documents: DocumentPointer[], categories: string[]) => string;\n\n // document context\n addDocumentContext: (documentPointer: DocumentPointer, categories?: string[]) => TreeNodeId;\n removeDocumentContext: (documentId: string) => void;\n getDocumentsContext: (categories: string[]) => DocumentPointer[];\n\n // api endpoints\n copilotApiConfig: CopilotApiConfig;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n entryPoints: {},\n setEntryPoint: () => {},\n removeEntryPoint: () => {},\n getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),\n getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),\n\n getContextString: (documents: DocumentPointer[], categories: string[]) =>\n returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n\n getDocumentsContext: (categories: string[]) => returnAndThrowInDebug([]),\n addDocumentContext: () => returnAndThrowInDebug(\"\"),\n removeDocumentContext: () => {},\n\n copilotApiConfig: new (class implements CopilotApiConfig {\n get chatApiEndpoint(): string {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n }\n get chatApiEndpointV2(): string {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n }\n get headers(): Record<string, string> {\n return {};\n }\n get body(): Record<string, any> {\n return {};\n }\n })(),\n};\n\nexport const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);\n\nfunction returnAndThrowInDebug<T>(value: T): T {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n return value;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAEA,SAAS,YAAY,WAAW,cAAc;;;ACC9C,OAAO,WAAW;AAmElB,IAAM,sBAA4C;AAAA,EAChD,aAAa,CAAC;AAAA,EACd,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,uCAAuC,MAAM,sBAAsB,CAAC,CAAC;AAAA,EACrE,wBAAwB,MAAM,sBAAsB,MAAY;AAAA,EAAC,EAAC;AAAA,EAElE,kBAAkB,CAAC,WAA8B,eAC/C,sBAAsB,EAAE;AAAA,EAC1B,YAAY,MAAM;AAAA,EAClB,eAAe,MAAM;AAAA,EAAC;AAAA,EAEtB,qBAAqB,CAAC,eAAyB,sBAAsB,CAAC,CAAC;AAAA,EACvE,oBAAoB,MAAM,sBAAsB,EAAE;AAAA,EAClD,uBAAuB,MAAM;AAAA,EAAC;AAAA,EAE9B,kBAAkB,IAAK,MAAkC;AAAA,IACvD,IAAI,kBAA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