-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from palico-ai/copilot-context
FEAT: Added Palico application context
- Loading branch information
Showing
12 changed files
with
173 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './copilot' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import React, { useEffect } from 'react' | ||
import { type ChatCompletionMessageToolCall } from 'openai/resources/chat/completions' | ||
import { AgentAPI, type AgentCallResponse, type AgentMessage, type ToolExecutionMessage } from '../agent_api' | ||
|
||
export interface ChatMessage { | ||
content: string | ||
role: 'user' | 'assistant' | ||
} | ||
|
||
export interface PalicoContextProps { | ||
loading: boolean | ||
deploymentId: number | ||
conversationHistory: ChatMessage[] | ||
sendMessage: (message: string) => Promise<void> | ||
} | ||
|
||
export const PalicoContext = React.createContext<PalicoContextProps>({ | ||
loading: false, | ||
deploymentId: -1, | ||
conversationHistory: [], | ||
sendMessage: async () => {} | ||
}) | ||
|
||
export type ToolHandler<Input, Output> = (input: Input) => Promise<Output> | ||
|
||
export interface PalicoContextProviderProps { | ||
tools: Record<string, ToolHandler<any, any>> | ||
deploymentId: number | ||
children?: any | ||
} | ||
|
||
export const PalicoContextProvider: React.FC<PalicoContextProviderProps> = ({ | ||
deploymentId, | ||
tools, | ||
children | ||
}) => { | ||
const [loading, setLoading] = React.useState<boolean>(false) | ||
const [conversationId, setConversationId] = React.useState<number>() | ||
const [messageHistory, setMessageHistory] = React.useState<ChatMessage[]>([]) | ||
// TODO: Convert to step-based pending message (create user reply -> handle user reply -> handle tool call -> end) | ||
const [pendingMessage, setPendingMessage] = React.useState<string>() | ||
|
||
useEffect(() => { | ||
const callTool = async (tool: ChatCompletionMessageToolCall): Promise<ToolExecutionMessage> => { | ||
const toolHandler = tools[tool.function.name] | ||
if (toolHandler === null || toolHandler === undefined) { | ||
throw new Error(`Tool ${tool.function.name} not found`) | ||
} | ||
const output = await toolHandler(JSON.parse(tool.function.arguments)) | ||
return { | ||
toolId: tool.id, | ||
functionName: tool.function.name, | ||
output | ||
} | ||
} | ||
|
||
const handleToolCall = async (message: AgentMessage, conversationId: number): Promise<void> => { | ||
if (!message.toolCalls) return | ||
const toolCallResponse = await Promise.all(message.toolCalls.map(callTool)) | ||
const response = await AgentAPI.replyToToolCall({ | ||
deploymentId, | ||
conversationId, | ||
toolOutputs: toolCallResponse | ||
}) | ||
await handleAgentResponse(response, conversationId) | ||
} | ||
|
||
const handleAgentResponse = async (response: AgentCallResponse, conversationId: number): Promise<void> => { | ||
if (response.message.toolCalls) { | ||
await handleToolCall(response.message, conversationId) | ||
} | ||
if (response.message.content) { | ||
setMessageHistory([ | ||
...messageHistory, | ||
{ | ||
content: response.message.content.toString(), | ||
role: 'assistant' | ||
} | ||
]) | ||
} | ||
} | ||
|
||
const handlePendingMessage = async (): Promise<void> => { | ||
console.log('Handle pending message') | ||
if (!pendingMessage) return | ||
try { | ||
setPendingMessage(undefined) | ||
if (!conversationId) { | ||
const response = await AgentAPI.newConversation({ | ||
deploymentId, | ||
message: pendingMessage | ||
}) | ||
setConversationId(response.conversationId) | ||
await handleAgentResponse(response, response.conversationId) | ||
} else { | ||
const response = await AgentAPI.replyAsUser({ | ||
deploymentId, | ||
conversationId, | ||
message: pendingMessage | ||
}) | ||
await handleAgentResponse(response, conversationId) | ||
} | ||
} catch (e) { | ||
console.log(e) | ||
} finally { | ||
setLoading(false) | ||
} | ||
} | ||
|
||
void handlePendingMessage() | ||
}, [conversationId, deploymentId, messageHistory, pendingMessage, tools]) | ||
|
||
const sendMessage = async (message: string): Promise<void> => { | ||
setLoading(true) | ||
setPendingMessage(message) | ||
setMessageHistory([ | ||
...messageHistory, | ||
{ | ||
content: message, | ||
role: 'user' | ||
} | ||
]) | ||
} | ||
|
||
return ( | ||
<PalicoContext.Provider | ||
value={{ deploymentId, conversationHistory: messageHistory, sendMessage, loading }} | ||
> | ||
{children} | ||
</PalicoContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './application_context' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from './hello_world' | ||
export * from './ui' | ||
export * from './context' | ||
export * from './components' | ||
export * from './agent_api' |
This file was deleted.
Oops, something went wrong.