Skip to content

Commit

Permalink
Merge pull request #2 from palico-ai/context
Browse files Browse the repository at this point in the history
FEAT: Added support for providing context with AgentAPI messages
  • Loading branch information
shikdernyc authored Jan 27, 2024
2 parents 1508f7b + 86d0d10 commit 45d2593
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
6 changes: 5 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export default [
resolve(),
commonjs(),
typescript({ tsconfig: './tsconfig.json' }),
terser()
terser({
compress: {
drop_console: true
}
})
]
},
{
Expand Down
2 changes: 2 additions & 0 deletions src/agent_api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ export interface AgentAPIParams {
export interface NewConversationParams {
deploymentId: number
message: string
context?: Record<string, unknown>
}

export interface ReplyAsUserParams {
deploymentId: number
conversationId: number
message: string
context?: Record<string, unknown>
}

export interface ToolExecutionMessage {
Expand Down
25 changes: 16 additions & 9 deletions src/context/application_context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface PalicoContextProps {
loading: boolean
deploymentId: number
conversationHistory: ChatMessage[]
sendMessage: (message: string) => Promise<void>
sendMessage: (message: string, context: Record<string, unknown>) => Promise<void>
}

export const PalicoContext = React.createContext<PalicoContextProps>({
Expand All @@ -29,6 +29,11 @@ export interface PalicoContextProviderProps {
children?: any
}

export interface PendingMessagePayload {
message: string
context: Record<string, unknown>
}

export const PalicoContextProvider: React.FC<PalicoContextProviderProps> = ({
deploymentId,
tools,
Expand All @@ -38,7 +43,7 @@ export const PalicoContextProvider: React.FC<PalicoContextProviderProps> = ({
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>()
const [pendingMessage, setPendingMessage] = React.useState<PendingMessagePayload>()

useEffect(() => {
const callTool = async (tool: ChatCompletionMessageToolCall): Promise<ToolExecutionMessage> => {
Expand Down Expand Up @@ -81,27 +86,26 @@ export const PalicoContextProvider: React.FC<PalicoContextProviderProps> = ({
}

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
message: pendingMessage.message,
context: pendingMessage.context
})
setConversationId(response.conversationId)
await handleAgentResponse(response, response.conversationId)
} else {
const response = await AgentAPI.replyAsUser({
deploymentId,
conversationId,
message: pendingMessage
message: pendingMessage.message,
context: pendingMessage.context
})
await handleAgentResponse(response, conversationId)
}
} catch (e) {
console.log(e)
} finally {
setLoading(false)
}
Expand All @@ -110,9 +114,12 @@ export const PalicoContextProvider: React.FC<PalicoContextProviderProps> = ({
void handlePendingMessage()
}, [conversationId, deploymentId, messageHistory, pendingMessage, tools])

const sendMessage = async (message: string): Promise<void> => {
const sendMessage = async (message: string, context: Record<string, unknown>): Promise<void> => {
setLoading(true)
setPendingMessage(message)
setPendingMessage({
message,
context
})
setMessageHistory([
...messageHistory,
{
Expand Down

0 comments on commit 45d2593

Please sign in to comment.