@@ -21,6 +21,7 @@ import {
21
21
AI_AGENT_MODEL ,
22
22
AI_AGENT_TYPE ,
23
23
Agent ,
24
+ KnowledgeFile ,
24
25
MemberInfo ,
25
26
USER_ROLE ,
26
27
USER_STATUS ,
@@ -95,6 +96,7 @@ import { copyToClipboard } from "@/utils/copyToClipboard"
95
96
import { track } from "@/utils/mixpanelHelper"
96
97
import { ChatContext } from "../components/ChatContext"
97
98
import { ErrorText } from "../components/ErrorText"
99
+ import KnowledgeUpload from "../components/KnowledgeUpload"
98
100
import {
99
101
ChatSendRequestPayload ,
100
102
SenderType ,
@@ -126,6 +128,9 @@ import {
126
128
} from "./style"
127
129
import { agentData2JSONReport } from "./utils"
128
130
131
+ // TODO: WTF, can edit knowledge file
132
+ const CAN_EDIT_KNOWLEDGE_FILE = false
133
+
129
134
export const AIAgent : FC = ( ) => {
130
135
const data = useAsyncValue ( ) as {
131
136
agent : Agent
@@ -248,6 +253,7 @@ export const AIAgent: FC = () => {
248
253
model : getValues ( "model" ) ,
249
254
prompt : getValues ( "prompt" ) ,
250
255
agentType : getValues ( "agentType" ) ,
256
+ // TODO: add knowledge
251
257
} as Agent
252
258
} , [ getValues ] )
253
259
@@ -315,6 +321,7 @@ export const AIAgent: FC = () => {
315
321
threadID : v4 ( ) ,
316
322
prompt : getValues ( "prompt" ) ,
317
323
variables : getValues ( "variables" ) ,
324
+ // TODO: add knowledge
318
325
actionID : getValues ( "aiAgentID" ) ,
319
326
modelConfig : getValues ( "modelConfig" ) ,
320
327
model : getValues ( "model" ) ,
@@ -357,29 +364,39 @@ export const AIAgent: FC = () => {
357
364
}
358
365
359
366
const handleSubmitSave = async ( data : Agent ) => {
367
+ let currentData : Agent = { ...data }
368
+ if (
369
+ ! isPremiumModel ( currentData . model ) &&
370
+ currentData . knowledge ?. length > 0
371
+ ) {
372
+ currentData = {
373
+ ...currentData ,
374
+ knowledge : [ ] ,
375
+ }
376
+ }
360
377
track (
361
378
ILLA_MIXPANEL_EVENT_TYPE . CLICK ,
362
379
ILLA_MIXPANEL_BUILDER_PAGE_NAME . AI_AGENT_EDIT ,
363
380
{
364
381
element : "save" ,
365
- parameter1 : agentData2JSONReport ( data ) ,
366
- parameter5 : data . aiAgentID || "-1" ,
382
+ parameter1 : agentData2JSONReport ( currentData ) ,
383
+ parameter5 : currentData . aiAgentID || "-1" ,
367
384
} ,
368
385
)
369
386
let agentInfo : Agent
370
387
try {
371
- let updateIconURL = data . icon
372
- if ( data . icon !== undefined && data . icon !== "" ) {
373
- const iconURL = new URL ( data . icon )
388
+ let updateIconURL = currentData . icon
389
+ if ( currentData . icon !== undefined && currentData . icon !== "" ) {
390
+ const iconURL = new URL ( currentData . icon )
374
391
if ( iconURL . protocol !== "http:" && iconURL . protocol !== "https:" ) {
375
- updateIconURL = await uploadAgentIcon ( data . icon )
392
+ updateIconURL = await uploadAgentIcon ( currentData . icon )
376
393
}
377
394
}
378
- if ( data . aiAgentID === undefined || data . aiAgentID === "" ) {
395
+ if ( currentData . aiAgentID === undefined || currentData . aiAgentID === "" ) {
379
396
const resp = await createAgent ( {
380
- ...data ,
397
+ ...currentData ,
381
398
icon : updateIconURL ,
382
- variables : data . variables . filter (
399
+ variables : currentData . variables . filter (
383
400
( v ) => v . key !== "" && v . value !== "" ,
384
401
) ,
385
402
} )
@@ -399,8 +416,8 @@ export const AIAgent: FC = () => {
399
416
} )
400
417
agentInfo = resp . data
401
418
} else {
402
- const resp = await putAgentDetail ( data . aiAgentID , {
403
- ...data ,
419
+ const resp = await putAgentDetail ( currentData . aiAgentID , {
420
+ ...currentData ,
404
421
icon : updateIconURL ,
405
422
variables : data . variables . filter (
406
423
( v ) => v . key !== "" && v . value !== "" ,
@@ -438,6 +455,9 @@ export const AIAgent: FC = () => {
438
455
if ( ! ! errors . prompt ) {
439
456
handleScrollToElement ( SCROLL_ID . PROMPT )
440
457
validate = false
458
+ } else if ( ! ! errors . knowledge ) {
459
+ handleScrollToElement ( SCROLL_ID . KNOWLEDGE )
460
+ validate = false
441
461
} else if ( ! ! errors . variables ) {
442
462
handleScrollToElement ( SCROLL_ID . VARIABLES )
443
463
validate = false
@@ -512,6 +532,17 @@ export const AIAgent: FC = () => {
512
532
} )
513
533
handleScrollToElement ( SCROLL_ID . VARIABLES )
514
534
return false
535
+ } else if (
536
+ Array . isArray ( getValues ( "knowledge" ) ) &&
537
+ getValues ( "knowledge" ) . length > 0 &&
538
+ getValues ( "knowledge" ) . some ( ( param ) => param . value === "" )
539
+ ) {
540
+ setError ( "variables" , {
541
+ type : "knowledge" ,
542
+ message : t ( "dashboard.message.parsing_file_in_prog" ) ,
543
+ } )
544
+ handleScrollToElement ( SCROLL_ID . KNOWLEDGE )
545
+ return false
515
546
}
516
547
return true
517
548
}
@@ -722,6 +753,75 @@ export const AIAgent: FC = () => {
722
753
) }
723
754
/>
724
755
756
+ { CAN_EDIT_KNOWLEDGE_FILE &&
757
+ isPremiumModel ( getValues ( "model" ) ) && (
758
+ < Controller
759
+ name = "knowledge"
760
+ control = { control }
761
+ rules = { {
762
+ validate : ( value ) => {
763
+ const isValidate =
764
+ ! value ||
765
+ value . length === 0 ||
766
+ value . every ( ( param ) => param . value !== "" )
767
+ return isValidate ? isValidate : t ( "" )
768
+ } ,
769
+ } }
770
+ shouldUnregister = { false }
771
+ render = { ( { field } ) => (
772
+ < AIAgentBlock
773
+ title = { t ( "knowledge" ) }
774
+ scrollId = { SCROLL_ID . KNOWLEDGE }
775
+ >
776
+ < KnowledgeUpload
777
+ addFile = { (
778
+ file : KnowledgeFile ,
779
+ isUpdate ?: boolean ,
780
+ ) => {
781
+ const { name, type } = file
782
+ const files = field . value || [ ]
783
+ const index = files . findIndex (
784
+ ( item ) =>
785
+ item . name === name && item . type === type ,
786
+ )
787
+ if ( index !== - 1 ) {
788
+ if ( isUpdate ) {
789
+ let needUpdateFile = files [ index ]
790
+ files . splice ( index , 1 , {
791
+ ...needUpdateFile ,
792
+ ...file ,
793
+ } )
794
+ } else {
795
+ const fileNamePrefix = `${
796
+ file . name . split ( "." ) [ 0 ]
797
+ } (${ v4 ( ) . slice ( 0 , 3 ) } )`
798
+
799
+ files . push ( {
800
+ ...file ,
801
+ name : `${ fileNamePrefix } .${ file . name . split (
802
+ "." ,
803
+ ) ?. [ 1 ] } `,
804
+ } )
805
+ }
806
+ } else {
807
+ files . push ( file )
808
+ }
809
+ field . onChange ( files )
810
+ } }
811
+ removeFile = { ( name : string ) => {
812
+ const currentFiles = field . value || [ ]
813
+ const files = currentFiles . filter (
814
+ ( item ) => item . name !== name ,
815
+ )
816
+ field . onChange ( files )
817
+ } }
818
+ values = { field . value }
819
+ />
820
+ </ AIAgentBlock >
821
+ ) }
822
+ />
823
+ ) }
824
+
725
825
< Controller
726
826
name = "model"
727
827
control = { control }
@@ -1093,30 +1193,30 @@ export const AIAgent: FC = () => {
1093
1193
</ div >
1094
1194
< form onSubmit = { handleSubmit ( handleSubmitSave ) } >
1095
1195
< div css = { buttonContainerStyle } >
1096
- < Button
1097
- id = "save-button"
1098
- flex = "1"
1099
- colorScheme = "grayBlue"
1100
- onClick = { handleVerifyOnSave }
1101
- size = "large"
1102
- loading = { isSubmitting }
1103
- >
1104
- { t ( "editor.ai-agent.save" ) }
1105
- </ Button >
1106
1196
< Button
1107
1197
flex = "1"
1108
1198
size = "large"
1109
1199
type = "button"
1110
1200
loading = { isConnecting }
1111
- ml = "8px"
1112
- colorScheme = { getColor ( "grayBlue" , "02" ) }
1201
+ colorScheme = "grayBlue"
1113
1202
leftIcon = { isRunning ? < ResetIcon /> : < PlayFillIcon /> }
1114
1203
onClick = { handleClickStart }
1115
1204
>
1116
1205
{ ! isRunning
1117
1206
? t ( "editor.ai-agent.start" )
1118
1207
: t ( "editor.ai-agent.restart" ) }
1119
1208
</ Button >
1209
+ < Button
1210
+ id = "save-button"
1211
+ flex = "1"
1212
+ ml = "8px"
1213
+ onClick = { handleVerifyOnSave }
1214
+ colorScheme = { getColor ( "grayBlue" , "02" ) }
1215
+ size = "large"
1216
+ loading = { isSubmitting }
1217
+ >
1218
+ { t ( "editor.ai-agent.save" ) }
1219
+ </ Button >
1120
1220
</ div >
1121
1221
</ form >
1122
1222
</ div >
@@ -1147,9 +1247,12 @@ export const AIAgent: FC = () => {
1147
1247
currentTeamInfo . myRole ,
1148
1248
contributeField . value ,
1149
1249
) }
1250
+ showEditPanel = { false }
1150
1251
isRunning = { isRunning }
1252
+ isConnecting = { isConnecting }
1151
1253
hasCreated = { Boolean ( idField . value ) }
1152
1254
isMobile = { false }
1255
+ model = { getValues ( "model" ) }
1153
1256
editState = "EDIT"
1154
1257
agentType = { field . value }
1155
1258
chatMessages = { chatMessages }
@@ -1171,7 +1274,7 @@ export const AIAgent: FC = () => {
1171
1274
sendMessage (
1172
1275
{
1173
1276
threadID : message . threadID ,
1174
- prompt : encodeURIComponent ( message . message ) ,
1277
+ prompt : message . message ,
1175
1278
variables : [ ] ,
1176
1279
actionID : getValues ( "aiAgentID" ) ,
1177
1280
modelConfig : getValues ( "modelConfig" ) ,
0 commit comments