@copilotkit/react-core 0.19.0 → 0.20.0-demofixes.2
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/.turbo/turbo-build.log +215 -197
- package/CHANGELOG.md +33 -0
- package/dist/{chunk-VP2EU4US.mjs → chunk-4LKP4DMT.mjs} +3 -3
- package/dist/{chunk-4CFJ6A6A.mjs → chunk-4OWTRVNI.mjs} +6 -2
- package/dist/chunk-4OWTRVNI.mjs.map +1 -0
- package/dist/{chunk-IQEWY7P4.mjs → chunk-ABXVFQLN.mjs} +46 -29
- package/dist/chunk-ABXVFQLN.mjs.map +1 -0
- package/dist/{chunk-YOXWT7I6.mjs → chunk-CPEUPJQJ.mjs} +2 -2
- package/dist/{chunk-UAKLDQMP.mjs → chunk-JIKWSWIX.mjs} +4 -4
- package/dist/chunk-JIKWSWIX.mjs.map +1 -0
- package/dist/{chunk-DGUVNY3V.mjs → chunk-K2LVBFRY.mjs} +2 -2
- package/dist/{chunk-GBZZPQ5X.mjs → chunk-KAZJPE2I.mjs} +14 -8
- package/dist/chunk-KAZJPE2I.mjs.map +1 -0
- package/dist/{chunk-5GS7FQPW.mjs → chunk-KZNQLITJ.mjs} +5 -5
- package/dist/chunk-KZNQLITJ.mjs.map +1 -0
- package/dist/chunk-LUGEI4YQ.mjs +1 -0
- package/dist/chunk-LUGEI4YQ.mjs.map +1 -0
- package/dist/chunk-TZPIY754.mjs +102 -0
- package/dist/chunk-TZPIY754.mjs.map +1 -0
- package/dist/{chunk-IQH5AQQC.mjs → chunk-XBLCD24W.mjs} +2 -2
- package/dist/components/copilot-provider/copilotkit.js +12 -6
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +439 -8
- package/dist/components/copilot-provider/copilotkit.mjs.map +1 -1
- package/dist/components/copilot-provider/index.js +12 -6
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +439 -9
- package/dist/components/copilot-provider/index.mjs.map +1 -1
- package/dist/components/copilot-provider/standard-copilot-api-config.mjs +9 -4
- package/dist/components/copilot-provider/standard-copilot-api-config.mjs.map +1 -1
- package/dist/components/index.js +12 -6
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +439 -10
- package/dist/components/index.mjs.map +1 -1
- package/dist/context/copilot-context.d.ts +5 -4
- package/dist/context/copilot-context.js +7 -2
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +65 -5
- package/dist/context/copilot-context.mjs.map +1 -1
- package/dist/context/index.d.ts +1 -1
- package/dist/context/index.js +7 -2
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +65 -6
- package/dist/context/index.mjs.map +1 -1
- package/dist/hooks/index.js +47 -29
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +392 -21
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/hooks/use-chat.d.ts +2 -2
- package/dist/hooks/use-chat.js +46 -28
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +204 -4
- package/dist/hooks/use-chat.mjs.map +1 -1
- package/dist/hooks/use-copilot-chat.js +47 -29
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +340 -13
- package/dist/hooks/use-copilot-chat.mjs.map +1 -1
- package/dist/hooks/use-flat-category-store.mjs +65 -4
- package/dist/hooks/use-flat-category-store.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-actionable.js.map +1 -1
- package/dist/hooks/use-make-copilot-actionable.mjs +85 -5
- package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +77 -5
- package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-readable.mjs +77 -5
- package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
- package/dist/hooks/use-tree.mjs +150 -4
- package/dist/hooks/use-tree.mjs.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +154 -35
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +981 -41
- package/dist/index.mjs.map +1 -1
- package/dist/lib/copilot-task.d.ts +34 -0
- package/dist/lib/copilot-task.js +206 -0
- package/dist/lib/copilot-task.js.map +1 -0
- package/dist/lib/copilot-task.mjs +188 -0
- package/dist/lib/copilot-task.mjs.map +1 -0
- package/dist/lib/index.d.ts +6 -0
- package/dist/lib/index.js +206 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/index.mjs +188 -0
- package/dist/lib/index.mjs.map +1 -0
- package/dist/openai-assistants/hooks/index.js.map +1 -1
- package/dist/openai-assistants/hooks/index.mjs +228 -14
- package/dist/openai-assistants/hooks/index.mjs.map +1 -1
- package/dist/openai-assistants/hooks/use-assistants.mjs +45 -7
- package/dist/openai-assistants/hooks/use-assistants.mjs.map +1 -1
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.js.map +1 -1
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +228 -14
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map +1 -1
- package/dist/openai-assistants/index.js.map +1 -1
- package/dist/openai-assistants/index.mjs +228 -17
- package/dist/openai-assistants/index.mjs.map +1 -1
- package/dist/openai-assistants/utils/index.mjs +43 -5
- package/dist/openai-assistants/utils/index.mjs.map +1 -1
- package/dist/openai-assistants/utils/process-message-stream.mjs +43 -4
- package/dist/openai-assistants/utils/process-message-stream.mjs.map +1 -1
- package/dist/types/index.mjs +0 -1
- package/dist/utils/fetch-chat-completion.d.ts +3 -3
- package/dist/utils/fetch-chat-completion.js +3 -3
- package/dist/utils/fetch-chat-completion.js.map +1 -1
- package/dist/utils/fetch-chat-completion.mjs +99 -5
- package/dist/utils/fetch-chat-completion.mjs.map +1 -1
- package/package.json +4 -4
- package/src/components/copilot-provider/copilotkit.tsx +17 -8
- package/src/context/copilot-context.tsx +11 -3
- package/src/context/index.ts +1 -1
- package/src/hooks/use-chat.ts +52 -30
- package/src/hooks/use-copilot-chat.ts +3 -3
- package/src/index.tsx +1 -0
- package/src/lib/copilot-task.ts +131 -0
- package/src/lib/index.ts +1 -0
- package/src/utils/fetch-chat-completion.ts +6 -6
- package/dist/chunk-4CFJ6A6A.mjs.map +0 -1
- package/dist/chunk-5GS7FQPW.mjs.map +0 -1
- package/dist/chunk-GBZZPQ5X.mjs.map +0 -1
- package/dist/chunk-IQEWY7P4.mjs.map +0 -1
- package/dist/chunk-UAKLDQMP.mjs.map +0 -1
- /package/dist/{chunk-VP2EU4US.mjs.map → chunk-4LKP4DMT.mjs.map} +0 -0
- /package/dist/{chunk-YOXWT7I6.mjs.map → chunk-CPEUPJQJ.mjs.map} +0 -0
- /package/dist/{chunk-DGUVNY3V.mjs.map → chunk-K2LVBFRY.mjs.map} +0 -0
- /package/dist/{chunk-IQH5AQQC.mjs.map → chunk-XBLCD24W.mjs.map} +0 -0
package/dist/hooks/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/use-copilot-chat.ts","../../src/context/copilot-context.tsx","../../src/hooks/use-chat.ts","../../src/utils/fetch-chat-completion.ts","../../src/components/copilot-provider/copilotkit.tsx","../../src/hooks/use-make-copilot-actionable.ts","../../src/hooks/use-make-copilot-readable.ts","../../src/hooks/use-make-copilot-document-readable.ts"],"sourcesContent":["import { useMemo, useContext } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { Message, ToolDefinition } from \"@copilotkit/shared\";\nimport { SystemMessageFunction } from \"../types\";\nimport { UseChatOptions, useChat } from \"./use-chat\";\nimport { defaultCopilotContextCategories } from \"../components\";\n\nexport interface UseCopilotChatOptions extends UseChatOptions {\n makeSystemMessage?: SystemMessageFunction;\n additionalInstructions?: 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 additionalInstructions,\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, additionalInstructions),\n role: \"system\",\n };\n }, [getContextString, makeSystemMessage]);\n\n const functionDescriptions: ToolDefinition[] = 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 tools: 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(\n contextString: string,\n additionalInstructions?: string,\n): string {\n return (\n `\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` + (additionalInstructions ? `\\n\\n${additionalInstructions}` : \"\")\n );\n}\n","\"use client\";\n\nimport { FunctionCallHandler, AnnotatedFunction, ToolDefinition } from \"@copilotkit/shared\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\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: (\n customEntryPoints?: Record<string, AnnotatedFunction<any[]>>,\n ) => ToolDefinition[];\n getFunctionCallHandler: (\n customEntryPoints?: Record<string, AnnotatedFunction<any[]>>,\n ) => 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(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n }\n get chatApiEndpointV2(): string {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\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\nexport function useCopilotContext(): CopilotContextParams {\n return React.useContext(CopilotContext);\n}\n\nfunction returnAndThrowInDebug<T>(value: T): T {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n return value;\n}\n","import { useRef, useState } from \"react\";\nimport { Message, ToolDefinition, FunctionCallHandler } from \"@copilotkit/shared\";\nimport { nanoid } from \"nanoid\";\nimport { fetchAndDecodeChatCompletion } from \"../utils/fetch-chat-completion\";\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 tools?: ToolDefinition[];\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 const abortControllerRef = useRef<AbortController>();\n\n const runChatCompletion = async (messages: Message[]): Promise<Message[]> => {\n setIsLoading(true);\n\n const newMessages: Message[] = [\n {\n id: nanoid(),\n createdAt: new Date(),\n content: \"\",\n role: \"assistant\",\n },\n ];\n const abortController = new AbortController();\n abortControllerRef.current = abortController;\n\n setMessages([...messages, ...newMessages]);\n\n const messagesWithContext = [...(options.initialMessages || []), ...messages];\n const response = await fetchAndDecodeChatCompletion({\n copilotConfig: options.copilotConfig,\n messages: messagesWithContext,\n tools: options.tools,\n headers: options.headers,\n signal: abortController.signal,\n });\n\n if (!response.events) {\n throw new Error(\"Failed to fetch chat completion\");\n }\n\n const reader = response.events.getReader();\n try {\n while (true) {\n const { done, value } = await reader.read();\n\n if (done) {\n return newMessages.slice();\n }\n\n let currentMessage = Object.assign({}, newMessages[newMessages.length - 1]);\n\n if (value.type === \"content\") {\n if (currentMessage.function_call) {\n // Create a new message if the previous one is a function call\n currentMessage = {\n id: nanoid(),\n createdAt: new Date(),\n content: \"\",\n role: \"assistant\",\n };\n newMessages.push(currentMessage);\n }\n currentMessage.content += value.content;\n newMessages[newMessages.length - 1] = currentMessage;\n setMessages([...messages, ...newMessages]);\n } else if (value.type === \"function\") {\n // Create a new message if the previous one is not empty\n if (currentMessage.content != \"\" || currentMessage.function_call) {\n currentMessage = {\n id: nanoid(),\n createdAt: new Date(),\n content: \"\",\n role: \"assistant\",\n };\n newMessages.push(currentMessage);\n }\n currentMessage.function_call = {\n name: value.name,\n arguments: JSON.stringify(value.arguments),\n };\n\n newMessages[newMessages.length - 1] = currentMessage;\n setMessages([...messages, ...newMessages]);\n\n // Execute the function call\n await options.onFunctionCall?.(messages, currentMessage.function_call);\n }\n }\n } finally {\n setIsLoading(false);\n }\n };\n\n const runChatCompletionAndHandleFunctionCall = async (messages: Message[]): Promise<void> => {\n await runChatCompletion(messages);\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 abortControllerRef.current?.abort();\n };\n\n return {\n messages,\n append,\n reload,\n stop,\n isLoading,\n input,\n setInput,\n };\n}\n","import {\n Message,\n ToolDefinition,\n ChatCompletionEvent,\n decodeChatCompletion,\n parseChatCompletion,\n decodeChatCompletionAsText,\n} from \"@copilotkit/shared\";\nimport { CopilotApiConfig } from \"../context\";\n\nexport interface FetchChatCompletionParams {\n copilotConfig: CopilotApiConfig;\n model?: string;\n messages: Message[];\n tools?: ToolDefinition[];\n temperature?: number;\n maxTokens?: number;\n headers?: Record<string, string> | Headers;\n body?: object;\n signal?: AbortSignal;\n}\n\nexport async function fetchChatCompletion({\n copilotConfig,\n model,\n messages,\n tools,\n temperature,\n headers,\n body,\n signal,\n}: FetchChatCompletionParams): Promise<Response> {\n temperature ||= 0.5;\n tools ||= [];\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 const response = await fetch(copilotConfig.chatApiEndpoint, {\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 ...(tools.length ? { tools } : {}),\n ...(temperature ? { temperature } : {}),\n ...(tools.length != 0 ? { tool_choice: \"auto\" } : {}),\n ...copilotConfig.body,\n ...(body ? { ...body } : {}),\n }),\n signal,\n });\n\n return response;\n}\n\nexport interface DecodedChatCompletionResponse extends Response {\n events: ReadableStream<ChatCompletionEvent> | null;\n}\n\nexport async function fetchAndDecodeChatCompletion(\n params: FetchChatCompletionParams,\n): Promise<DecodedChatCompletionResponse> {\n const response = await fetchChatCompletion(params);\n if (!response.ok || !response.body) {\n return { ...response, events: null };\n }\n const events = await decodeChatCompletion(parseChatCompletion(response.body));\n return { ...response, events };\n}\n\nexport interface DecodedChatCompletionResponseAsText extends Response {\n events: ReadableStream<string> | null;\n}\n\nexport async function fetchAndDecodeChatCompletionAsText(\n params: FetchChatCompletionParams,\n): Promise<DecodedChatCompletionResponseAsText> {\n const response = await fetchChatCompletion(params);\n if (!response.ok || !response.body) {\n return { ...response, events: null };\n }\n const events = await decodeChatCompletionAsText(\n decodeChatCompletion(parseChatCompletion(response.body)),\n );\n return { ...response, events };\n}\n","\"use client\";\n\nimport { FunctionDefinition } from \"@copilotkit/shared\";\nimport { useCallback, useState } from \"react\";\nimport { CopilotContext, CopilotApiConfig } from \"../../context/copilot-context\";\nimport useTree from \"../../hooks/use-tree\";\nimport { DocumentPointer } from \"../../types\";\nimport {\n FunctionCallHandler,\n AnnotatedFunction,\n annotatedFunctionToChatCompletionFunction,\n} from \"@copilotkit/shared\";\nimport useFlatCategoryStore from \"../../hooks/use-flat-category-store\";\nimport { StandardCopilotApiConfig } from \"./standard-copilot-api-config\";\nimport { CopilotKitProps } from \"./copilotkit-props\";\nimport { ToolDefinition } from \"@copilotkit/shared\";\n\n/**\n * The CopilotKit 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 * <CopilotKit url=\"https://your.copilotkit.api\">\n * <App />\n * </CopilotKit>\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 * <CopilotKit chatApiConfig={copilotApiConfig}>\n * <App />\n * </CopilotKit>\n * ```\n *\n * @param props - The props for the component.\n * @returns The CopilotKit component.\n */\nexport function CopilotKit({ children, ...props }: CopilotKitProps) {\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 (customEntryPoints?: Record<string, AnnotatedFunction<any[]>>) => {\n return entryPointsToChatCompletionFunctions(Object.values(customEntryPoints || entryPoints));\n },\n [entryPoints],\n );\n\n const getFunctionCallHandler = useCallback(\n (customEntryPoints?: Record<string, AnnotatedFunction<any[]>>) => {\n return entryPointsToFunctionCallHandler(Object.values(customEntryPoints || entryPoints));\n },\n [entryPoints],\n );\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 const copilotApiConfig: CopilotApiConfig = new StandardCopilotApiConfig(\n props.url,\n `${props.url}/v2`,\n props.headers || {},\n props.body || {},\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): ToolDefinition[] {\n return entryPoints.map(annotatedFunctionToChatCompletionFunction);\n}\n","\"use client\";\n\nimport { useRef, useContext, useEffect, useMemo } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { AnnotatedFunction } from \"@copilotkit/shared\";\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 { 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 { 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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,SAAS,kBAAkB;;;ACGpC,OAAO,WAAW;AAqElB,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,MAAM,uEAAuE;AAAA,IACzF;AAAA,IACA,IAAI,oBAA4B;AAC9B,YAAM,IAAI,MAAM,uEAAuE;AAAA,IACzF;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;AAM3F,SAAS,sBAAyB,OAAa;AAC7C,QAAM,IAAI,MAAM,uEAAuE;AACvF,SAAO;AACT;;;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AAAA,EACF,GAAG,CAAC,aAAa,UAAU,YAAY,aAAa,CAAC;AAErD,SAAO,MAAM;AACf;;;AC5BA,SAAS,cAAAC,aAAY,aAAAC,YAAW,UAAAC,eAAc;AAWvC,SAAS,+BACd,UACA,YACA,eAAsB,CAAC,GACH;AACpB,QAAM,EAAE,oBAAoB,sBAAsB,IAAIC,YAAW,cAAc;AAC/E,QAAM,QAAQC,QAAe;AAE7B,EAAAC,WAAU,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":["messages","useState","useRef","useContext","useMemo","nanoid","useRef","useContext","useMemo","useContext","useEffect","useRef","useContext","useRef","useEffect","useContext","useEffect","useRef","useContext","useRef","useEffect"]}
|
package/dist/hooks/use-chat.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Message, FunctionCallHandler,
|
|
1
|
+
import { Message, FunctionCallHandler, ToolDefinition } from '@copilotkit/shared';
|
|
2
2
|
import { CopilotApiConfig } from '../context/copilot-context.js';
|
|
3
3
|
import 'react';
|
|
4
4
|
import './use-tree.js';
|
|
@@ -46,7 +46,7 @@ type UseChatOptions = {
|
|
|
46
46
|
/**
|
|
47
47
|
* Function definitions to be sent to the API.
|
|
48
48
|
*/
|
|
49
|
-
|
|
49
|
+
tools?: ToolDefinition[];
|
|
50
50
|
};
|
|
51
51
|
type UseChatHelpers = {
|
|
52
52
|
/** Current messages in the chat */
|
package/dist/hooks/use-chat.js
CHANGED
|
@@ -70,14 +70,14 @@ function fetchChatCompletion(_0) {
|
|
|
70
70
|
copilotConfig,
|
|
71
71
|
model,
|
|
72
72
|
messages,
|
|
73
|
-
|
|
73
|
+
tools,
|
|
74
74
|
temperature,
|
|
75
75
|
headers,
|
|
76
76
|
body,
|
|
77
77
|
signal
|
|
78
78
|
}) {
|
|
79
79
|
temperature || (temperature = 0.5);
|
|
80
|
-
|
|
80
|
+
tools || (tools = []);
|
|
81
81
|
const cleanedMessages = messages.map((message) => {
|
|
82
82
|
const { content, role, name, function_call } = message;
|
|
83
83
|
return { content, role, name, function_call };
|
|
@@ -91,7 +91,7 @@ function fetchChatCompletion(_0) {
|
|
|
91
91
|
model,
|
|
92
92
|
messages: cleanedMessages,
|
|
93
93
|
stream: true
|
|
94
|
-
},
|
|
94
|
+
}, tools.length ? { tools } : {}), temperature ? { temperature } : {}), tools.length != 0 ? { tool_choice: "auto" } : {}), copilotConfig.body), body ? __spreadValues({}, body) : {})),
|
|
95
95
|
signal
|
|
96
96
|
});
|
|
97
97
|
return response;
|
|
@@ -115,21 +115,24 @@ function useChat(options) {
|
|
|
115
115
|
const [isLoading, setIsLoading] = (0, import_react.useState)(false);
|
|
116
116
|
const abortControllerRef = (0, import_react.useRef)();
|
|
117
117
|
const runChatCompletion = (messages2) => __async(this, null, function* () {
|
|
118
|
+
var _a;
|
|
118
119
|
setIsLoading(true);
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
120
|
+
const newMessages = [
|
|
121
|
+
{
|
|
122
|
+
id: (0, import_nanoid.nanoid)(),
|
|
123
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
124
|
+
content: "",
|
|
125
|
+
role: "assistant"
|
|
126
|
+
}
|
|
127
|
+
];
|
|
125
128
|
const abortController = new AbortController();
|
|
126
129
|
abortControllerRef.current = abortController;
|
|
127
|
-
setMessages([...messages2,
|
|
130
|
+
setMessages([...messages2, ...newMessages]);
|
|
128
131
|
const messagesWithContext = [...options.initialMessages || [], ...messages2];
|
|
129
132
|
const response = yield fetchAndDecodeChatCompletion({
|
|
130
133
|
copilotConfig: options.copilotConfig,
|
|
131
134
|
messages: messagesWithContext,
|
|
132
|
-
|
|
135
|
+
tools: options.tools,
|
|
133
136
|
headers: options.headers,
|
|
134
137
|
signal: abortController.signal
|
|
135
138
|
});
|
|
@@ -137,36 +140,51 @@ function useChat(options) {
|
|
|
137
140
|
throw new Error("Failed to fetch chat completion");
|
|
138
141
|
}
|
|
139
142
|
const reader = response.events.getReader();
|
|
140
|
-
|
|
141
|
-
|
|
143
|
+
try {
|
|
144
|
+
while (true) {
|
|
142
145
|
const { done, value } = yield reader.read();
|
|
143
146
|
if (done) {
|
|
144
|
-
|
|
145
|
-
return __spreadValues({}, assistantMessage);
|
|
147
|
+
return newMessages.slice();
|
|
146
148
|
}
|
|
149
|
+
let currentMessage = Object.assign({}, newMessages[newMessages.length - 1]);
|
|
147
150
|
if (value.type === "content") {
|
|
148
|
-
|
|
149
|
-
|
|
151
|
+
if (currentMessage.function_call) {
|
|
152
|
+
currentMessage = {
|
|
153
|
+
id: (0, import_nanoid.nanoid)(),
|
|
154
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
155
|
+
content: "",
|
|
156
|
+
role: "assistant"
|
|
157
|
+
};
|
|
158
|
+
newMessages.push(currentMessage);
|
|
159
|
+
}
|
|
160
|
+
currentMessage.content += value.content;
|
|
161
|
+
newMessages[newMessages.length - 1] = currentMessage;
|
|
162
|
+
setMessages([...messages2, ...newMessages]);
|
|
150
163
|
} else if (value.type === "function") {
|
|
151
|
-
|
|
164
|
+
if (currentMessage.content != "" || currentMessage.function_call) {
|
|
165
|
+
currentMessage = {
|
|
166
|
+
id: (0, import_nanoid.nanoid)(),
|
|
167
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
168
|
+
content: "",
|
|
169
|
+
role: "assistant"
|
|
170
|
+
};
|
|
171
|
+
newMessages.push(currentMessage);
|
|
172
|
+
}
|
|
173
|
+
currentMessage.function_call = {
|
|
152
174
|
name: value.name,
|
|
153
175
|
arguments: JSON.stringify(value.arguments)
|
|
154
176
|
};
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
177
|
+
newMessages[newMessages.length - 1] = currentMessage;
|
|
178
|
+
setMessages([...messages2, ...newMessages]);
|
|
179
|
+
yield (_a = options.onFunctionCall) == null ? void 0 : _a.call(options, messages2, currentMessage.function_call);
|
|
158
180
|
}
|
|
159
|
-
} catch (error) {
|
|
160
|
-
setIsLoading(false);
|
|
161
|
-
throw error;
|
|
162
181
|
}
|
|
182
|
+
} finally {
|
|
183
|
+
setIsLoading(false);
|
|
163
184
|
}
|
|
164
185
|
});
|
|
165
186
|
const runChatCompletionAndHandleFunctionCall = (messages2) => __async(this, null, function* () {
|
|
166
|
-
|
|
167
|
-
if (message.function_call && options.onFunctionCall) {
|
|
168
|
-
yield options.onFunctionCall(messages2, message.function_call);
|
|
169
|
-
}
|
|
187
|
+
yield runChatCompletion(messages2);
|
|
170
188
|
});
|
|
171
189
|
const append = (message) => __async(this, null, function* () {
|
|
172
190
|
if (isLoading) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/use-chat.ts","../../src/utils/fetch-chat-completion.ts"],"sourcesContent":["import { useRef, useState } from \"react\";\nimport { Message, Function, FunctionCallHandler, FunctionCall } from \"@copilotkit/shared\";\nimport { nanoid } from \"nanoid\";\nimport { fetchAndDecodeChatCompletion } from \"../utils/fetch-chat-completion\";\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 const abortControllerRef = useRef<AbortController>();\n\n const runChatCompletion = async (messages: Message[]): Promise<Message> => {\n setIsLoading(true);\n const assistantMessage: Message = {\n id: nanoid(),\n createdAt: new Date(),\n content: \"\",\n role: \"assistant\",\n };\n const abortController = new AbortController();\n abortControllerRef.current = abortController;\n\n setMessages([...messages, { ...assistantMessage }]);\n\n const messagesWithContext = [...(options.initialMessages || []), ...messages];\n const response = await fetchAndDecodeChatCompletion({\n copilotConfig: options.copilotConfig,\n messages: messagesWithContext,\n functions: options.functions,\n headers: options.headers,\n signal: abortController.signal,\n });\n\n if (!response.events) {\n throw new Error(\"Failed to fetch chat completion\");\n }\n\n const reader = response.events.getReader();\n\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n if (done) {\n setIsLoading(false);\n return { ...assistantMessage };\n }\n\n if (value.type === \"content\") {\n assistantMessage.content += value.content;\n setMessages([...messages, { ...assistantMessage }]);\n } else if (value.type === \"function\") {\n assistantMessage.function_call = {\n name: value.name,\n arguments: JSON.stringify(value.arguments),\n };\n setMessages([...messages, { ...assistantMessage }]);\n // quit early if we get a function call\n setIsLoading(false);\n return { ...assistantMessage };\n }\n } catch (error) {\n setIsLoading(false);\n throw error;\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 as FunctionCall);\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 abortControllerRef.current?.abort();\n };\n\n return {\n messages,\n append,\n reload,\n stop,\n isLoading,\n input,\n setInput,\n };\n}\n","import {\n Message,\n Function,\n ChatCompletionEvent,\n decodeChatCompletion,\n parseChatCompletion,\n decodeChatCompletionAsText,\n} from \"@copilotkit/shared\";\nimport { CopilotApiConfig } from \"../context\";\n\nexport interface FetchChatCompletionParams {\n copilotConfig: CopilotApiConfig;\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 signal?: AbortSignal;\n}\n\nexport async function fetchChatCompletion({\n copilotConfig,\n model,\n messages,\n functions,\n temperature,\n headers,\n body,\n signal,\n}: FetchChatCompletionParams): Promise<Response> {\n temperature ||= 0.5;\n functions ||= [];\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 const response = await fetch(copilotConfig.chatApiEndpoint, {\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 return response;\n}\n\nexport interface DecodedChatCompletionResponse extends Response {\n events: ReadableStream<ChatCompletionEvent> | null;\n}\n\nexport async function fetchAndDecodeChatCompletion(\n params: FetchChatCompletionParams,\n): Promise<DecodedChatCompletionResponse> {\n const response = await fetchChatCompletion(params);\n if (!response.ok || !response.body) {\n return { ...response, events: null };\n }\n const events = await decodeChatCompletion(parseChatCompletion(response.body));\n return { ...response, events };\n}\n\nexport interface DecodedChatCompletionResponseAsText extends Response {\n events: ReadableStream<string> | null;\n}\n\nexport async function fetchAndDecodeChatCompletionAsText(\n params: FetchChatCompletionParams,\n): Promise<DecodedChatCompletionResponseAsText> {\n const response = await fetchChatCompletion(params);\n if (!response.ok || !response.body) {\n return { ...response, events: null };\n }\n const events = await decodeChatCompletionAsText(\n decodeChatCompletion(parseChatCompletion(response.body)),\n );\n return { ...response, events };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAiC;AAEjC,oBAAuB;;;ACFvB,oBAOO;AAeP,SAAsB,oBAAoB,IASO;AAAA,6CATP;AAAA,IACxC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAAiD;AAC/C,kCAAgB;AAChB,8BAAc,CAAC;AAGf,UAAM,kBAAkB,SAAS,IA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AAM,QAAQ,eAAeA,WAAU,QAAQ,aAA6B;AAAA,IAC9E;AAAA,EACF;AAEA,QAAM,SAAS,CAAO,YAAoC;AACxD,QAAI,WAAW;AACb;AAAA,IACF;AACA,UAAM,cAAc,CAAC,GAAG,UAAU,OAAO;AACzC,gBAAY,WAAW;AACvB,WAAO,uCAAuC,WAAW;AAAA,EAC3D;AAEA,QAAM,SAAS,MAA2B;AACxC,QAAI,aAAa,SAAS,WAAW,GAAG;AACtC;AAAA,IACF;AACA,QAAI,cAAc,CAAC,GAAG,QAAQ;AAC9B,UAAM,cAAc,SAAS,SAAS,SAAS,CAAC;AAEhD,QAAI,YAAY,SAAS,aAAa;AACpC,oBAAc,YAAY,MAAM,GAAG,EAAE;AAAA,IACvC;AACA,gBAAY,WAAW;AAEvB,WAAO,uCAAuC,WAAW;AAAA,EAC3D;AAEA,QAAM,OAAO,MAAY;AAhL3B;AAiLI,6BAAmB,YAAnB,mBAA4B;AAAA,EAC9B;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":["messages"]}
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/use-chat.ts","../../src/utils/fetch-chat-completion.ts"],"sourcesContent":["import { useRef, useState } from \"react\";\nimport { Message, ToolDefinition, FunctionCallHandler } from \"@copilotkit/shared\";\nimport { nanoid } from \"nanoid\";\nimport { fetchAndDecodeChatCompletion } from \"../utils/fetch-chat-completion\";\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 tools?: ToolDefinition[];\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 const abortControllerRef = useRef<AbortController>();\n\n const runChatCompletion = async (messages: Message[]): Promise<Message[]> => {\n setIsLoading(true);\n\n const newMessages: Message[] = [\n {\n id: nanoid(),\n createdAt: new Date(),\n content: \"\",\n role: \"assistant\",\n },\n ];\n const abortController = new AbortController();\n abortControllerRef.current = abortController;\n\n setMessages([...messages, ...newMessages]);\n\n const messagesWithContext = [...(options.initialMessages || []), ...messages];\n const response = await fetchAndDecodeChatCompletion({\n copilotConfig: options.copilotConfig,\n messages: messagesWithContext,\n tools: options.tools,\n headers: options.headers,\n signal: abortController.signal,\n });\n\n if (!response.events) {\n throw new Error(\"Failed to fetch chat completion\");\n }\n\n const reader = response.events.getReader();\n try {\n while (true) {\n const { done, value } = await reader.read();\n\n if (done) {\n return newMessages.slice();\n }\n\n let currentMessage = Object.assign({}, newMessages[newMessages.length - 1]);\n\n if (value.type === \"content\") {\n if (currentMessage.function_call) {\n // Create a new message if the previous one is a function call\n currentMessage = {\n id: nanoid(),\n createdAt: new Date(),\n content: \"\",\n role: \"assistant\",\n };\n newMessages.push(currentMessage);\n }\n currentMessage.content += value.content;\n newMessages[newMessages.length - 1] = currentMessage;\n setMessages([...messages, ...newMessages]);\n } else if (value.type === \"function\") {\n // Create a new message if the previous one is not empty\n if (currentMessage.content != \"\" || currentMessage.function_call) {\n currentMessage = {\n id: nanoid(),\n createdAt: new Date(),\n content: \"\",\n role: \"assistant\",\n };\n newMessages.push(currentMessage);\n }\n currentMessage.function_call = {\n name: value.name,\n arguments: JSON.stringify(value.arguments),\n };\n\n newMessages[newMessages.length - 1] = currentMessage;\n setMessages([...messages, ...newMessages]);\n\n // Execute the function call\n await options.onFunctionCall?.(messages, currentMessage.function_call);\n }\n }\n } finally {\n setIsLoading(false);\n }\n };\n\n const runChatCompletionAndHandleFunctionCall = async (messages: Message[]): Promise<void> => {\n await runChatCompletion(messages);\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 abortControllerRef.current?.abort();\n };\n\n return {\n messages,\n append,\n reload,\n stop,\n isLoading,\n input,\n setInput,\n };\n}\n","import {\n Message,\n ToolDefinition,\n ChatCompletionEvent,\n decodeChatCompletion,\n parseChatCompletion,\n decodeChatCompletionAsText,\n} from \"@copilotkit/shared\";\nimport { CopilotApiConfig } from \"../context\";\n\nexport interface FetchChatCompletionParams {\n copilotConfig: CopilotApiConfig;\n model?: string;\n messages: Message[];\n tools?: ToolDefinition[];\n temperature?: number;\n maxTokens?: number;\n headers?: Record<string, string> | Headers;\n body?: object;\n signal?: AbortSignal;\n}\n\nexport async function fetchChatCompletion({\n copilotConfig,\n model,\n messages,\n tools,\n temperature,\n headers,\n body,\n signal,\n}: FetchChatCompletionParams): Promise<Response> {\n temperature ||= 0.5;\n tools ||= [];\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 const response = await fetch(copilotConfig.chatApiEndpoint, {\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 ...(tools.length ? { tools } : {}),\n ...(temperature ? { temperature } : {}),\n ...(tools.length != 0 ? { tool_choice: \"auto\" } : {}),\n ...copilotConfig.body,\n ...(body ? { ...body } : {}),\n }),\n signal,\n });\n\n return response;\n}\n\nexport interface DecodedChatCompletionResponse extends Response {\n events: ReadableStream<ChatCompletionEvent> | null;\n}\n\nexport async function fetchAndDecodeChatCompletion(\n params: FetchChatCompletionParams,\n): Promise<DecodedChatCompletionResponse> {\n const response = await fetchChatCompletion(params);\n if (!response.ok || !response.body) {\n return { ...response, events: null };\n }\n const events = await decodeChatCompletion(parseChatCompletion(response.body));\n return { ...response, events };\n}\n\nexport interface DecodedChatCompletionResponseAsText extends Response {\n events: ReadableStream<string> | null;\n}\n\nexport async function fetchAndDecodeChatCompletionAsText(\n params: FetchChatCompletionParams,\n): Promise<DecodedChatCompletionResponseAsText> {\n const response = await fetchChatCompletion(params);\n if (!response.ok || !response.body) {\n return { ...response, events: null };\n }\n const events = await decodeChatCompletionAsText(\n decodeChatCompletion(parseChatCompletion(response.body)),\n );\n return { ...response, events };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAiC;AAEjC,oBAAuB;;;ACFvB,oBAOO;AAeP,SAAsB,oBAAoB,IASO;AAAA,6CATP;AAAA,IACxC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAAiD;AAC/C,kCAAgB;AAChB,sBAAU,CAAC;AAGX,UAAM,kBAAkB,SAAS,IAAI,CAAC,YAAY;AAChD,YAAM,EAAE,SAAS,MAAM,MAAM,cAAc,IAAI;AAC/C,aAAO,EAAE,SAAS,MAAM,MAAM,cAAc;AAAA,IAC9C,CAAC;AAED,UAAM,WAAW,MAAM,MAAM,cAAc,iBAAiB;AAAA,MAC1D,QAAQ;AAAA,MACR,SAAS;AAAA,QACP,gBAAgB;AAAA,SACb,cAAc,UACb,UAAU,mBAAK,WAAY,CAAC;AAAA,MAElC,MAAM,KAAK,UAAU;AAAA,QACnB;AAAA,QACA,UAAU;AAAA,QACV,QAAQ;AAAA,SACJ,MAAM,SAAS,EAAE,MAAM,IAAI,CAAC,IAC5B,cAAc,EAAE,YAAY,IAAI,CAAC,IACjC,MAAM,UAAU,IAAI,EAAE,aAAa,OAAO,IAAI,CAAC,IAChD,cAAc,OACb,OAAO,mBAAK,QAAS,CAAC,EAC3B;AAAA,MACD;AAAA,IACF,CAAC;AAED,WAAO;AAAA,EACT;AAAA;AAMA,SAAsB,6BACpB,QACwC;AAAA;AACxC,UAAM,WAAW,MAAM,oBAAoB,MAAM;AACjD,QAAI,CAAC,SAAS,MAAM,CAAC,SAAS,MAAM;AAClC,aAAO,iCAAK,WAAL,EAAe,QAAQ,KAAK;AAAA,IACrC;AACA,UAAM,SAAS,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S,CAAC;AAEhD,QAAI,YAAY,SAAS,aAAa;AACpC,oBAAc,YAAY,MAAM,GAAG,EAAE;AAAA,IACvC;AACA,gBAAY,WAAW;AAEvB,WAAO,uCAAuC,WAAW;AAAA,EAC3D;AAEA,QAAM,OAAO,MAAY;AAtM3B;AAuMI,6BAAmB,YAAnB,mBAA4B;AAAA,EAC9B;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":["messages"]}
|
package/dist/hooks/use-chat.mjs
CHANGED
|
@@ -1,8 +1,208 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __async = (__this, __arguments, generator) => {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
var fulfilled = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
step(generator.next(value));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
reject(e);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var rejected = (value) => {
|
|
30
|
+
try {
|
|
31
|
+
step(generator.throw(value));
|
|
32
|
+
} catch (e) {
|
|
33
|
+
reject(e);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/hooks/use-chat.ts
|
|
42
|
+
import { useRef, useState } from "react";
|
|
43
|
+
import { nanoid } from "nanoid";
|
|
44
|
+
|
|
45
|
+
// src/utils/fetch-chat-completion.ts
|
|
1
46
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
47
|
+
decodeChatCompletion,
|
|
48
|
+
parseChatCompletion,
|
|
49
|
+
decodeChatCompletionAsText
|
|
50
|
+
} from "@copilotkit/shared";
|
|
51
|
+
function fetchChatCompletion(_0) {
|
|
52
|
+
return __async(this, arguments, function* ({
|
|
53
|
+
copilotConfig,
|
|
54
|
+
model,
|
|
55
|
+
messages,
|
|
56
|
+
tools,
|
|
57
|
+
temperature,
|
|
58
|
+
headers,
|
|
59
|
+
body,
|
|
60
|
+
signal
|
|
61
|
+
}) {
|
|
62
|
+
temperature || (temperature = 0.5);
|
|
63
|
+
tools || (tools = []);
|
|
64
|
+
const cleanedMessages = messages.map((message) => {
|
|
65
|
+
const { content, role, name, function_call } = message;
|
|
66
|
+
return { content, role, name, function_call };
|
|
67
|
+
});
|
|
68
|
+
const response = yield fetch(copilotConfig.chatApiEndpoint, {
|
|
69
|
+
method: "POST",
|
|
70
|
+
headers: __spreadValues(__spreadValues({
|
|
71
|
+
"Content-Type": "application/json"
|
|
72
|
+
}, copilotConfig.headers), headers ? __spreadValues({}, headers) : {}),
|
|
73
|
+
body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
74
|
+
model,
|
|
75
|
+
messages: cleanedMessages,
|
|
76
|
+
stream: true
|
|
77
|
+
}, tools.length ? { tools } : {}), temperature ? { temperature } : {}), tools.length != 0 ? { tool_choice: "auto" } : {}), copilotConfig.body), body ? __spreadValues({}, body) : {})),
|
|
78
|
+
signal
|
|
79
|
+
});
|
|
80
|
+
return response;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
function fetchAndDecodeChatCompletion(params) {
|
|
84
|
+
return __async(this, null, function* () {
|
|
85
|
+
const response = yield fetchChatCompletion(params);
|
|
86
|
+
if (!response.ok || !response.body) {
|
|
87
|
+
return __spreadProps(__spreadValues({}, response), { events: null });
|
|
88
|
+
}
|
|
89
|
+
const events = yield decodeChatCompletion(parseChatCompletion(response.body));
|
|
90
|
+
return __spreadProps(__spreadValues({}, response), { events });
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/hooks/use-chat.ts
|
|
95
|
+
function useChat(options) {
|
|
96
|
+
const [messages, setMessages] = useState([]);
|
|
97
|
+
const [input, setInput] = useState("");
|
|
98
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
99
|
+
const abortControllerRef = useRef();
|
|
100
|
+
const runChatCompletion = (messages2) => __async(this, null, function* () {
|
|
101
|
+
var _a;
|
|
102
|
+
setIsLoading(true);
|
|
103
|
+
const newMessages = [
|
|
104
|
+
{
|
|
105
|
+
id: nanoid(),
|
|
106
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
107
|
+
content: "",
|
|
108
|
+
role: "assistant"
|
|
109
|
+
}
|
|
110
|
+
];
|
|
111
|
+
const abortController = new AbortController();
|
|
112
|
+
abortControllerRef.current = abortController;
|
|
113
|
+
setMessages([...messages2, ...newMessages]);
|
|
114
|
+
const messagesWithContext = [...options.initialMessages || [], ...messages2];
|
|
115
|
+
const response = yield fetchAndDecodeChatCompletion({
|
|
116
|
+
copilotConfig: options.copilotConfig,
|
|
117
|
+
messages: messagesWithContext,
|
|
118
|
+
tools: options.tools,
|
|
119
|
+
headers: options.headers,
|
|
120
|
+
signal: abortController.signal
|
|
121
|
+
});
|
|
122
|
+
if (!response.events) {
|
|
123
|
+
throw new Error("Failed to fetch chat completion");
|
|
124
|
+
}
|
|
125
|
+
const reader = response.events.getReader();
|
|
126
|
+
try {
|
|
127
|
+
while (true) {
|
|
128
|
+
const { done, value } = yield reader.read();
|
|
129
|
+
if (done) {
|
|
130
|
+
return newMessages.slice();
|
|
131
|
+
}
|
|
132
|
+
let currentMessage = Object.assign({}, newMessages[newMessages.length - 1]);
|
|
133
|
+
if (value.type === "content") {
|
|
134
|
+
if (currentMessage.function_call) {
|
|
135
|
+
currentMessage = {
|
|
136
|
+
id: nanoid(),
|
|
137
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
138
|
+
content: "",
|
|
139
|
+
role: "assistant"
|
|
140
|
+
};
|
|
141
|
+
newMessages.push(currentMessage);
|
|
142
|
+
}
|
|
143
|
+
currentMessage.content += value.content;
|
|
144
|
+
newMessages[newMessages.length - 1] = currentMessage;
|
|
145
|
+
setMessages([...messages2, ...newMessages]);
|
|
146
|
+
} else if (value.type === "function") {
|
|
147
|
+
if (currentMessage.content != "" || currentMessage.function_call) {
|
|
148
|
+
currentMessage = {
|
|
149
|
+
id: nanoid(),
|
|
150
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
151
|
+
content: "",
|
|
152
|
+
role: "assistant"
|
|
153
|
+
};
|
|
154
|
+
newMessages.push(currentMessage);
|
|
155
|
+
}
|
|
156
|
+
currentMessage.function_call = {
|
|
157
|
+
name: value.name,
|
|
158
|
+
arguments: JSON.stringify(value.arguments)
|
|
159
|
+
};
|
|
160
|
+
newMessages[newMessages.length - 1] = currentMessage;
|
|
161
|
+
setMessages([...messages2, ...newMessages]);
|
|
162
|
+
yield (_a = options.onFunctionCall) == null ? void 0 : _a.call(options, messages2, currentMessage.function_call);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
} finally {
|
|
166
|
+
setIsLoading(false);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
const runChatCompletionAndHandleFunctionCall = (messages2) => __async(this, null, function* () {
|
|
170
|
+
yield runChatCompletion(messages2);
|
|
171
|
+
});
|
|
172
|
+
const append = (message) => __async(this, null, function* () {
|
|
173
|
+
if (isLoading) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const newMessages = [...messages, message];
|
|
177
|
+
setMessages(newMessages);
|
|
178
|
+
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
179
|
+
});
|
|
180
|
+
const reload = () => __async(this, null, function* () {
|
|
181
|
+
if (isLoading || messages.length === 0) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
let newMessages = [...messages];
|
|
185
|
+
const lastMessage = messages[messages.length - 1];
|
|
186
|
+
if (lastMessage.role === "assistant") {
|
|
187
|
+
newMessages = newMessages.slice(0, -1);
|
|
188
|
+
}
|
|
189
|
+
setMessages(newMessages);
|
|
190
|
+
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
191
|
+
});
|
|
192
|
+
const stop = () => {
|
|
193
|
+
var _a;
|
|
194
|
+
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
195
|
+
};
|
|
196
|
+
return {
|
|
197
|
+
messages,
|
|
198
|
+
append,
|
|
199
|
+
reload,
|
|
200
|
+
stop,
|
|
201
|
+
isLoading,
|
|
202
|
+
input,
|
|
203
|
+
setInput
|
|
204
|
+
};
|
|
205
|
+
}
|
|
6
206
|
export {
|
|
7
207
|
useChat
|
|
8
208
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/use-chat.ts","../../src/utils/fetch-chat-completion.ts"],"sourcesContent":["import { useRef, useState } from \"react\";\nimport { Message, ToolDefinition, FunctionCallHandler } from \"@copilotkit/shared\";\nimport { nanoid } from \"nanoid\";\nimport { fetchAndDecodeChatCompletion } from \"../utils/fetch-chat-completion\";\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 tools?: ToolDefinition[];\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 const abortControllerRef = useRef<AbortController>();\n\n const runChatCompletion = async (messages: Message[]): Promise<Message[]> => {\n setIsLoading(true);\n\n const newMessages: Message[] = [\n {\n id: nanoid(),\n createdAt: new Date(),\n content: \"\",\n role: \"assistant\",\n },\n ];\n const abortController = new AbortController();\n abortControllerRef.current = abortController;\n\n setMessages([...messages, ...newMessages]);\n\n const messagesWithContext = [...(options.initialMessages || []), ...messages];\n const response = await fetchAndDecodeChatCompletion({\n copilotConfig: options.copilotConfig,\n messages: messagesWithContext,\n tools: options.tools,\n headers: options.headers,\n signal: abortController.signal,\n });\n\n if (!response.events) {\n throw new Error(\"Failed to fetch chat completion\");\n }\n\n const reader = response.events.getReader();\n try {\n while (true) {\n const { done, value } = await reader.read();\n\n if (done) {\n return newMessages.slice();\n }\n\n let currentMessage = Object.assign({}, newMessages[newMessages.length - 1]);\n\n if (value.type === \"content\") {\n if (currentMessage.function_call) {\n // Create a new message if the previous one is a function call\n currentMessage = {\n id: nanoid(),\n createdAt: new Date(),\n content: \"\",\n role: \"assistant\",\n };\n newMessages.push(currentMessage);\n }\n currentMessage.content += value.content;\n newMessages[newMessages.length - 1] = currentMessage;\n setMessages([...messages, ...newMessages]);\n } else if (value.type === \"function\") {\n // Create a new message if the previous one is not empty\n if (currentMessage.content != \"\" || currentMessage.function_call) {\n currentMessage = {\n id: nanoid(),\n createdAt: new Date(),\n content: \"\",\n role: \"assistant\",\n };\n newMessages.push(currentMessage);\n }\n currentMessage.function_call = {\n name: value.name,\n arguments: JSON.stringify(value.arguments),\n };\n\n newMessages[newMessages.length - 1] = currentMessage;\n setMessages([...messages, ...newMessages]);\n\n // Execute the function call\n await options.onFunctionCall?.(messages, currentMessage.function_call);\n }\n }\n } finally {\n setIsLoading(false);\n }\n };\n\n const runChatCompletionAndHandleFunctionCall = async (messages: Message[]): Promise<void> => {\n await runChatCompletion(messages);\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 abortControllerRef.current?.abort();\n };\n\n return {\n messages,\n append,\n reload,\n stop,\n isLoading,\n input,\n setInput,\n };\n}\n","import {\n Message,\n ToolDefinition,\n ChatCompletionEvent,\n decodeChatCompletion,\n parseChatCompletion,\n decodeChatCompletionAsText,\n} from \"@copilotkit/shared\";\nimport { CopilotApiConfig } from \"../context\";\n\nexport interface FetchChatCompletionParams {\n copilotConfig: CopilotApiConfig;\n model?: string;\n messages: Message[];\n tools?: ToolDefinition[];\n temperature?: number;\n maxTokens?: number;\n headers?: Record<string, string> | Headers;\n body?: object;\n signal?: AbortSignal;\n}\n\nexport async function fetchChatCompletion({\n copilotConfig,\n model,\n messages,\n tools,\n temperature,\n headers,\n body,\n signal,\n}: FetchChatCompletionParams): Promise<Response> {\n temperature ||= 0.5;\n tools ||= [];\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 const response = await fetch(copilotConfig.chatApiEndpoint, {\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 ...(tools.length ? { tools } : {}),\n ...(temperature ? { temperature } : {}),\n ...(tools.length != 0 ? { tool_choice: \"auto\" } : {}),\n ...copilotConfig.body,\n ...(body ? { ...body } : {}),\n }),\n signal,\n });\n\n return response;\n}\n\nexport interface DecodedChatCompletionResponse extends Response {\n events: ReadableStream<ChatCompletionEvent> | null;\n}\n\nexport async function fetchAndDecodeChatCompletion(\n params: FetchChatCompletionParams,\n): Promise<DecodedChatCompletionResponse> {\n const response = await fetchChatCompletion(params);\n if (!response.ok || !response.body) {\n return { ...response, events: null };\n }\n const events = await decodeChatCompletion(parseChatCompletion(response.body));\n return { ...response, events };\n}\n\nexport interface DecodedChatCompletionResponseAsText extends Response {\n events: ReadableStream<string> | null;\n}\n\nexport async function fetchAndDecodeChatCompletionAsText(\n params: FetchChatCompletionParams,\n): Promise<DecodedChatCompletionResponseAsText> {\n const response = await fetchChatCompletion(params);\n if (!response.ok || !response.body) {\n return { ...response, events: null };\n }\n const events = await decodeChatCompletionAsText(\n decodeChatCompletion(parseChatCompletion(response.body)),\n );\n return { ...response, events };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,QAAQ,gBAAgB;AAEjC,SAAS,cAAc;;;ACFvB;AAAA,EAIE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAeP,SAAsB,oBAAoB,IASO;AAAA,6CATP;AAAA,IACxC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAAiD;AAC/C,kCAAgB;AAChB,sBAAU,CAAC;AAGX,UAAM,kBAAkB,SAAS,IAAI,CAAC,YAAY;AAChD,YAAM,EAAE,SAAS,MAAM,MAAM,cAAc,IAAI;AAC/C,aAAO,EAAE,SAAS,MAAM,MAAM,cAAc;AAAA,IAC9C,CAAC;AAED,UAAM,WAAW,MAAM,MAAM,cAAc,iBAAiB;AAAA,MAC1D,QAAQ;AAAA,MACR,SAAS;AAAA,QACP,gBAAgB;AAAA,SACb,cAAc,UACb,UAAU,mBAAK,WAAY,CAAC;AAAA,MAElC,MAAM,KAAK,UAAU;AAAA,QACnB;AAAA,QACA,UAAU;AAAA,QACV,QAAQ;AAAA,SACJ,MAAM,SAAS,EAAE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