|
| 1 | +import {Component, computed, inject, input, resource, signal} from '@angular/core'; |
| 2 | +import {AgentItem, BackendAgent, BackendAgentMessage, CommonMessage} from "fusio-sdk"; |
| 3 | +import {Agent, BackendAgentContent, ExecutionIndicator, Message} from "../../abstract/agent"; |
| 4 | +import {FusioService} from "../../service/fusio.service"; |
| 5 | +import {ErrorService} from "../../service/error.service"; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + selector: 'fusio-agent-chat-abstract', |
| 9 | + template: '' |
| 10 | +}) |
| 11 | +export abstract class ChatAbstract<TModel, TOptions = undefined> { |
| 12 | + |
| 13 | + agent = input.required<BackendAgent>(); |
| 14 | + chatId = input.required<string>(); |
| 15 | + |
| 16 | + model = signal<TModel|undefined>(undefined); |
| 17 | + |
| 18 | + output = signal<AgentItem|undefined>(undefined); |
| 19 | + loading = signal<boolean>(false); |
| 20 | + executeLoading = signal<boolean>(false); |
| 21 | + executeMessages = signal<Array<Message>>([]); |
| 22 | + response = signal<CommonMessage|undefined>(undefined); |
| 23 | + |
| 24 | + messagesResource = resource<Array<BackendAgentMessage>, { agent: BackendAgent, chatId: string, output: AgentItem|undefined }>({ |
| 25 | + params: () => ({ |
| 26 | + agent: this.agent(), |
| 27 | + chatId: this.chatId(), |
| 28 | + output: this.output(), |
| 29 | + }), |
| 30 | + loader: async (params) => { |
| 31 | + const collection = await this.api.getClient().backend().agent().message().getAll('' + params.params.agent.id, params.params.chatId); |
| 32 | + const entries = collection.entry || []; |
| 33 | + |
| 34 | + let lastMessage: BackendAgentMessage|undefined; |
| 35 | + const messages: Array<BackendAgentMessage> = []; |
| 36 | + entries.forEach((message) => { |
| 37 | + messages.push(message); |
| 38 | + |
| 39 | + if (message.role === 'assistant') { |
| 40 | + lastMessage = message; |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + if (lastMessage && lastMessage.item) { |
| 45 | + this.load(lastMessage.item); |
| 46 | + } |
| 47 | + |
| 48 | + return messages; |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + messages = computed<Array<BackendAgentMessage>|undefined>(() => { |
| 53 | + if (this.messagesResource.hasValue()) { |
| 54 | + return this.messagesResource.value(); |
| 55 | + } |
| 56 | + |
| 57 | + return undefined; |
| 58 | + }); |
| 59 | + |
| 60 | + protected api = inject(FusioService); |
| 61 | + protected error = inject(ErrorService); |
| 62 | + |
| 63 | + abstract getAgent(): Agent<TModel, TOptions>; |
| 64 | + |
| 65 | + async doSend(message: string) { |
| 66 | + if (!message) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + const agentId = this.agent().id; |
| 71 | + if (!agentId) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + this.loading.set(true); |
| 76 | + |
| 77 | + try { |
| 78 | + const content = await this.getAgent().prompt(agentId, message, this.chatId()); |
| 79 | + |
| 80 | + this.output.set(content); |
| 81 | + |
| 82 | + this.onSend(); |
| 83 | + |
| 84 | + this.scrollToBottom(); |
| 85 | + } catch (error) { |
| 86 | + this.response.set(this.error.convert(error)); |
| 87 | + } |
| 88 | + |
| 89 | + this.loading.set(false); |
| 90 | + } |
| 91 | + |
| 92 | + load(content?: BackendAgentContent): void { |
| 93 | + if (!content) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + this.executeMessages.set([]); |
| 98 | + this.model.set(this.getAgent().transform(content)); |
| 99 | + |
| 100 | + this.onLoad(); |
| 101 | + } |
| 102 | + |
| 103 | + async execute(): Promise<void> { |
| 104 | + const model = this.model(); |
| 105 | + if (!model) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + this.executeLoading.set(true); |
| 110 | + |
| 111 | + try { |
| 112 | + const executeMessages = this.executeMessages; |
| 113 | + const indicator = new ExecutionIndicator((message: Message) => { |
| 114 | + executeMessages.update((messages) => { |
| 115 | + return messages.concat([message]); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + const options = this.getOptions(); |
| 120 | + |
| 121 | + const response = await this.getAgent().execute(model, indicator, options); |
| 122 | + this.response.set(response); |
| 123 | + |
| 124 | + if (response) { |
| 125 | + this.onExecute(response); |
| 126 | + } |
| 127 | + } catch (error) { |
| 128 | + this.response.set(this.error.convert(error)); |
| 129 | + } |
| 130 | + |
| 131 | + this.executeLoading.set(false); |
| 132 | + } |
| 133 | + |
| 134 | + protected onLoad(): void { |
| 135 | + } |
| 136 | + |
| 137 | + protected onSend(): void { |
| 138 | + } |
| 139 | + |
| 140 | + protected onExecute(message: CommonMessage): void { |
| 141 | + } |
| 142 | + |
| 143 | + protected getOptions(): TOptions|undefined { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + scrollToBottom(): void { |
| 148 | + window.setTimeout(() => { |
| 149 | + let messagesBottom = document.getElementById('messages-bottom'); |
| 150 | + if (messagesBottom !== null) { |
| 151 | + messagesBottom.scrollIntoView(); |
| 152 | + } |
| 153 | + }, 500); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments