Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3.0.5 #14

Merged
merged 30 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5860a5b
feat: interrupted tool call
meta-d Dec 11, 2024
965dfc9
feat: sensitive tools
meta-d Dec 11, 2024
35d4ea0
feat: Interrupted chat
meta-d Dec 12, 2024
c227eba
feat: human in the loop
meta-d Dec 12, 2024
bedd050
feat: reject INTERRUPTED chat
meta-d Dec 12, 2024
b3ee9c0
feat: mutiple tool calls info
meta-d Dec 12, 2024
0e12a68
feat: sensitive operation tool call
meta-d Dec 13, 2024
44f2c35
Merge branch 'develop' into v3.0.4
meta-d Dec 13, 2024
82aaa73
version 3.0.4
meta-d Dec 13, 2024
6a37d8b
feat: primary agent not sensitive
meta-d Dec 13, 2024
d1c7743
feat: conversation summarize
meta-d Dec 14, 2024
7f3e7c7
feat: long term memory
meta-d Dec 15, 2024
4eb5b4b
feat: feeback to summarize long-term memory
meta-d Dec 16, 2024
2bd4bc2
feat: fix agent primary
meta-d Dec 16, 2024
37eef2c
feat: xpert's memory page
meta-d Dec 17, 2024
8d7d0ff
feat: long term memory of xpert
meta-d Dec 17, 2024
2aa1b8a
feat: use long-term memory in chat
meta-d Dec 18, 2024
ff0cc77
feat: add findMyAll in curd service
meta-d Dec 18, 2024
6ab7736
feat: register user
meta-d Dec 18, 2024
b33162c
feat: long-term memory every user
meta-d Dec 18, 2024
2a97e95
feat: long-term memory
meta-d Dec 18, 2024
49b660e
feat: execution tools events
meta-d Dec 18, 2024
73561a6
feat: fix xpert memory
meta-d Dec 18, 2024
3431705
feat: semantic search for long-term store
meta-d Dec 19, 2024
8dd154f
feat: long-term trigger events
meta-d Dec 19, 2024
803fc7a
feat: memory profile
meta-d Dec 20, 2024
7756017
version 3.0.5
meta-d Dec 20, 2024
867226a
feat: update mysql client to 2
meta-d Dec 20, 2024
c90a8ff
feat: memory of xpert
meta-d Dec 20, 2024
6c0cc56
fix: null memory store
meta-d Dec 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: reject INTERRUPTED chat
  • Loading branch information
meta-d committed Dec 12, 2024
commit bedd05051370a88fc752139c6441db83b08084db
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<div class="flex flex-col gap-1">
@for (param of item.params; track param.name) {
<div class="flex justify-start items-start gap-2">
<div class="w-24 shrink-0 whitespace-normal">{{param.title}}
<div class="w-24 shrink-0 whitespace-normal text-sm">{{param.title}}
<div><ngm-slash-svg class="inline-block"/><span class="text-primary-500">{{param.name}}</span></div>
</div>
<textarea class="flex-1 p-1 rounded-md border border-gray-300 focus:ring-primary-500 focus:border-primary-500 text-sm text-text-secondary"
<textarea class="flex-1 p-1 rounded-md border border-gray-300 focus:ring-primary-500 focus:border-primary-500 text-sm text-text-secondary focus:text-text-primary"
[ngModel]="param.value"
(ngModelChange)="updateParam(index, param.name, $event)"
>{{param.value}}</textarea>
Expand All @@ -26,7 +26,7 @@
(click)="onReject()"
>
<div class="w-full text-center">
<i class="ri-stop-line"></i>
<i class="ri-close-line"></i>
{{ 'PAC.Xpert.Reject' | translate: { Default: 'Reject' } }}
</div>
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@
</div>

@if (status() === eExecutionStatusEnum.INTERRUPTED && lastAIMessage()) {
<xpert-tool-call-confirm class="w-full" [(message)]="lastAIMessage" [tools]="tools()" (confirm)="onConfirm()" />
<xpert-tool-call-confirm class="w-full" [(message)]="lastAIMessage" [tools]="tools()"
(confirm)="onConfirm()"
(reject)="onReject()"
/>
}

<div class="mt-4 p-2 text-sm min-h-[60px] max-h-80 overflow-auto rounded-xl bg-neutral-100 dark:bg-neutral-700">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class XpertStudioPanelAgentExecutionComponent {
this.executionService.setConversation(null)
}

startRunAgent() {
startRunAgent(options?: {reject: boolean}) {
const executionId = this.execution()?.id
this.loading.set(true)
// Clear
Expand All @@ -181,7 +181,8 @@ export class XpertStudioPanelAgentExecutionComponent {
agent: this.xpertAgent(),
xpert: this.xpert(),
executionId,
toolCalls: this.lastAIMessage()?.tool_calls
toolCalls: this.lastAIMessage()?.tool_calls,
reject: options?.reject
})
.subscribe({
next: (msg) => {
Expand Down Expand Up @@ -225,6 +226,11 @@ export class XpertStudioPanelAgentExecutionComponent {
this.input.set(null)
this.startRunAgent()
}

onReject() {
this.input.set(null)
this.startRunAgent({reject: true})
}
}

export function processEvents(event, executionService: XpertExecutionService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
[(message)]="lastAIMessage"
[tools]="tools()"
(confirm)="onConfirm()"
(reject)="onReject()"
/>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class XpertStudioPreviewComponent {
}, { allowSignalWrites: true })
}

chat(input: string) {
chat(input: string, options?: {reject: boolean}) {
this.loading.set(true)

if (input) {
Expand Down Expand Up @@ -173,7 +173,8 @@ export class XpertStudioPreviewComponent {
input: { input },
conversationId: this.conversation()?.id,
xpertId: this.xpert().id,
toolCalls: this.lastAIMessage()?.tool_calls
toolCalls: this.lastAIMessage()?.tool_calls,
reject: options?.reject
},
{
isDraft: true
Expand Down Expand Up @@ -267,4 +268,7 @@ export class XpertStudioPreviewComponent {
onConfirm() {
this.chat(null)
}
onReject() {
this.chat(null, {reject: true})
}
}
4 changes: 4 additions & 0 deletions packages/contracts/src/ai/xpert-agent.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export type TChatAgentParams = {
* Message to update parameters of last tool call message
*/
toolCalls?: ToolCall[]
/**
* Reject the sensitive tool calls
*/
reject?: boolean
}

export function agentLabel(agent: IXpertAgent) {
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/src/ai/xpert.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ export type TChatRequest = {
id?: string
language?: string
toolCalls?: ToolCall[]
reject?: boolean
}

export type TChatOptions = {
Expand Down
3 changes: 1 addition & 2 deletions packages/server-ai/src/xpert-agent/commands/chat.command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ToolCall } from '@langchain/core/dist/messages/tool'
import { StoredMessage } from '@langchain/core/messages'
import { IXpert, IXpertAgentExecution, TChatOptions } from '@metad/contracts'
import { ICommand } from '@nestjs/cqrs'

Expand All @@ -24,8 +23,8 @@ export class XpertAgentChatCommand implements ICommand {
*/
execution?: IXpertAgentExecution

// message?: StoredMessage
toolCalls?: ToolCall[]
reject?: boolean
}
) {}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ToolCall } from '@langchain/core/dist/messages/tool'
import { tool } from '@langchain/core/tools'
import { LangGraphRunnableConfig } from '@langchain/langgraph'
import { ChatMessageEventTypeEnum, ChatMessageTypeEnum, convertToUrlPath, IXpert, IXpertAgent, IXpertAgentExecution, TChatOptions, TXpertParameter, XpertAgentExecutionStatusEnum, XpertParameterTypeEnum } from '@metad/contracts'
Expand All @@ -7,8 +8,6 @@ import { lastValueFrom, Observable, reduce, Subscriber, tap } from 'rxjs'
import { z } from 'zod'
import { XpertAgentExecutionUpsertCommand } from '../../xpert-agent-execution/commands'
import { XpertAgentExecutionOneQuery } from '../../xpert-agent-execution/queries'
import { StoredMessage } from '@langchain/core/messages'
import { ToolCall } from '@langchain/core/dist/messages/tool'

export class XpertAgentExecuteCommand implements ICommand {
static readonly type = '[Xpert Agent] Execute'
Expand All @@ -32,8 +31,8 @@ export class XpertAgentExecuteCommand implements ICommand {
// The subscriber response to client
subscriber: Subscriber<MessageEvent>

// message?: StoredMessage
toolCalls?: ToolCall[]
reject?: boolean
}
) {}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { NotFoundException } from '@nestjs/common'
import { BaseChatModel } from '@langchain/core/language_models/chat_models'
import { AIMessage, AIMessageChunk, HumanMessage, isAIMessageChunk, mapStoredMessageToChatMessage, MessageContent, SystemMessage, ToolMessage } from '@langchain/core/messages'
import { AIMessageChunk, HumanMessage, isAIMessage, isAIMessageChunk, MessageContent, SystemMessage, ToolMessage } from '@langchain/core/messages'
import { get_lc_unique_name, Serializable } from '@langchain/core/load/serializable'
import { SystemMessagePromptTemplate } from '@langchain/core/prompts'
import { Annotation, LangGraphRunnableConfig, NodeInterrupt, StateGraphArgs } from '@langchain/langgraph'
import { Annotation, CompiledStateGraph, LangGraphRunnableConfig, NodeInterrupt } from '@langchain/langgraph'
import { agentLabel, ChatMessageEventTypeEnum, ChatMessageTypeEnum, convertToUrlPath, ICopilot, IXpert, IXpertAgent, XpertAgentExecutionStatusEnum } from '@metad/contracts'
import { AgentRecursionLimit, isNil } from '@metad/copilot'
import { RequestContext } from '@metad/server-core'
import { Logger } from '@nestjs/common'
import { CommandBus, CommandHandler, ICommandHandler, QueryBus } from '@nestjs/cqrs'
import { concat, filter, from, lastValueFrom, Observable, of, reduce, Subscriber, switchMap, tap } from 'rxjs'
import { AgentState, CopilotGetOneQuery, createCopilotAgentState } from '../../../copilot'
import { AgentState, CopilotGetOneQuery } from '../../../copilot'
import { CopilotCheckpointSaver } from '../../../copilot-checkpoint'
import { BaseToolset, ToolsetGetToolsCommand } from '../../../xpert-toolset'
import { createParameters, XpertAgentExecuteCommand } from '../execute.command'
Expand All @@ -26,13 +26,9 @@ import { RunnableLambda } from '@langchain/core/runnables'
import { XpertAgentExecutionOneQuery } from '../../../xpert-agent-execution/queries'
import { getErrorMessage } from '@metad/server-common'
import { AgentStateAnnotation } from './types'
import { ToolCall } from '@langchain/core/dist/messages/tool'


export type ChatAgentState = AgentState
export const chatAgentState: StateGraphArgs<ChatAgentState>['channels'] = {
...createCopilotAgentState()
}

@CommandHandler(XpertAgentExecuteCommand)
export class XpertAgentExecuteHandler implements ICommandHandler<XpertAgentExecuteCommand> {
readonly #logger = new Logger(XpertAgentExecuteHandler.name)
Expand All @@ -46,7 +42,7 @@ export class XpertAgentExecuteHandler implements ICommandHandler<XpertAgentExecu

public async execute(command: XpertAgentExecuteCommand): Promise<Observable<MessageContent>> {
const { input, agentKey, xpert, options } = command
const { execution, subscriber, toolCalls } = options
const { execution, subscriber, toolCalls, reject } = options
const tenantId = RequestContext.currentTenantId()
const organizationId = RequestContext.getOrganizationId()
const user = RequestContext.currentUser()
Expand Down Expand Up @@ -183,23 +179,10 @@ ${agent.prompt}
thread_id,
checkpoint_ns: '',
}
if (toolCalls) {
// Update parameters of the last tool call message
const state = await graph.getState({configurable: config},)
const messages = state.values.messages
const lastMessage = messages[messages.length - 1]
if (lastMessage.id) {
const newMessage = {
role: "assistant",
content: lastMessage.content,
tool_calls: lastMessage.tool_calls.map((toolCall) => {
const newToolCall = toolCalls.find((_) => _.id === toolCall.id)
return {...toolCall, args: {...toolCall.args, ...(newToolCall?.args ?? {})} }
}) ,
id: lastMessage.id
}
await graph.updateState({configurable: config}, { messages: [newMessage]}, "agent")
}
if (reject) {
await this.reject(graph, config)
} else if (toolCalls) {
await this.updateToolCalls(graph, config, toolCalls)
}

const eventStack: string[] = []
Expand Down Expand Up @@ -606,4 +589,39 @@ ${agent.prompt}
tool: agentTool
}
}

async reject(graph: CompiledStateGraph<any, any, any>, config: any) {
const state = await graph.getState({configurable: config},)
const messages = state.values.messages
const lastMessage = messages[messages.length - 1]
if (isAIMessage(lastMessage)) {
await graph.updateState({configurable: config}, { messages: lastMessage.tool_calls.map((call) => {
return new ToolMessage({
name: call.name,
content: `Error: Reject by user`,
tool_call_id: call.id,
})
}) }, "agent")
}
}

async updateToolCalls(graph: CompiledStateGraph<any, any, any>, config: any, toolCalls: ToolCall[]) {
// Update parameters of the last tool call message
const state = await graph.getState({configurable: config},)
const messages = state.values.messages
const lastMessage = messages[messages.length - 1]
if (lastMessage.id) {
const newMessage = {
role: "assistant",
content: lastMessage.content,
tool_calls: lastMessage.tool_calls.map((toolCall) => {
const newToolCall = toolCalls.find((_) => _.id === toolCall.id)
return {...toolCall, args: {...toolCall.args, ...(newToolCall?.args ?? {})} }
}) ,
id: lastMessage.id
}
await graph.updateState({configurable: config}, { messages: [newMessage]}, "agent")
}
}

}
3 changes: 2 additions & 1 deletion packages/server-ai/src/xpert-agent/xpert-agent.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export class XpertAgentService extends TenantOrganizationAwareCrudService<XpertA
execution: {
id: params.executionId
},
toolCalls: params.toolCalls
toolCalls: params.toolCalls,
reject: params.reject,
})
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class XpertChatContinueHandler implements ICommandHandler<XpertChatContin

public async execute(command: XpertChatContinueCommand): Promise<Observable<MessageEvent>> {
const { options } = command
const { xpertId, input, conversationId, toolCalls } = command.request
const { xpertId, input, conversationId, toolCalls, reject } = command.request

const timeStart = Date.now()

Expand All @@ -49,7 +49,8 @@ export class XpertChatContinueHandler implements ICommandHandler<XpertChatContin
...(options ?? {}),
isDraft: options?.isDraft,
execution: { id: executionId },
toolCalls
toolCalls,
reject
})
)

Expand Down