@@ -3,17 +3,21 @@ import { toast } from "sonner";
33
44type RegisterTriggerResponse =
55 | {
6- authUrl : string ;
6+ success : boolean ;
77 registered : false ;
8+ auth_required : true ;
9+ auth_url : string ;
10+ auth_id : string ;
811 }
912 | {
13+ success : boolean ;
1014 registered : true ;
1115 } ;
1216
13- export interface ListUserTriggersData {
17+ export interface ListTriggerRegistrationsData {
1418 id : string ;
1519 user_id : string ;
16- provider_id : string ;
20+ template_id : string ;
1721 resource : unknown ;
1822 linked_assistant_ids ?: string [ ] ;
1923 created_at : string ;
@@ -76,10 +80,10 @@ export function useTriggers() {
7680 return triggers . data ;
7781 } ;
7882
79- const listUserTriggers = async (
83+ const listTriggerRegistrations = async (
8084 accessToken : string ,
81- ) : Promise < ListUserTriggersData [ ] | undefined > => {
82- const triggersApiUrl = constructTriggerUrl ( "/api/user- triggers" ) ;
85+ ) : Promise < ListTriggerRegistrationsData [ ] | undefined > => {
86+ const triggersApiUrl = constructTriggerUrl ( "/api/triggers/registrations " ) ;
8387 if ( ! triggersApiUrl ) {
8488 return ;
8589 }
@@ -122,8 +126,11 @@ export function useTriggers() {
122126 } ,
123127 body :
124128 Object . keys ( args . payload ) . length > 0
125- ? JSON . stringify ( args . payload )
126- : undefined ,
129+ ? JSON . stringify ( {
130+ type : args . id ,
131+ ...args . payload ,
132+ } )
133+ : JSON . stringify ( { type : args . id } ) ,
127134 } ) ;
128135
129136 if ( ! response . ok ) {
@@ -143,29 +150,29 @@ export function useTriggers() {
143150 agentId : string ;
144151 } ,
145152 ) : Promise < boolean > => {
146- const triggerApiUrl = constructTriggerUrl (
147- "/api/user-triggers/linked-assistants" ,
148- ) ;
149- if ( ! triggerApiUrl ) {
150- return false ;
151- }
152-
153- const response = await fetch ( triggerApiUrl , {
154- method : "PATCH" ,
155- headers : {
156- Authorization : `Bearer ${ accessToken } ` ,
157- } ,
158- body : JSON . stringify ( {
159- trigger_ids : args . selectedTriggerIds ,
160- assistant_id : args . agentId ,
161- } ) ,
162- } ) ;
163-
164- if ( ! response . ok ) {
165- toast . error ( "Failed to setup agent trigger" , {
166- richColors : true ,
153+ // Link the agent to each selected trigger individually
154+ for ( const triggerId of args . selectedTriggerIds ) {
155+ const triggerApiUrl = constructTriggerUrl (
156+ `/api/triggers/registrations/${ triggerId } /agents/${ args . agentId } ` ,
157+ ) ;
158+ if ( ! triggerApiUrl ) {
159+ return false ;
160+ }
161+
162+ const response = await fetch ( triggerApiUrl , {
163+ method : "POST" ,
164+ headers : {
165+ Authorization : `Bearer ${ accessToken } ` ,
166+ "Content-Type" : "application/json" ,
167+ } ,
167168 } ) ;
168- return false ;
169+
170+ if ( ! response . ok ) {
171+ toast . error ( "Failed to setup agent trigger" , {
172+ richColors : true ,
173+ } ) ;
174+ return false ;
175+ }
169176 }
170177
171178 return true ;
@@ -178,37 +185,40 @@ export function useTriggers() {
178185 selectedTriggerIds : string [ ] ;
179186 } ,
180187 ) : Promise < boolean > => {
181- const triggerApiUrl = constructTriggerUrl (
182- "/api/user-triggers/edit-assistant" ,
183- ) ;
184- if ( ! triggerApiUrl ) {
185- return false ;
186- }
187-
188- const response = await fetch ( triggerApiUrl , {
189- method : "PUT" ,
190- headers : {
191- Authorization : `Bearer ${ accessToken } ` ,
192- } ,
193- body : JSON . stringify ( {
194- trigger_ids : args . selectedTriggerIds ,
195- assistant_id : args . agentId ,
196- } ) ,
197- } ) ;
198-
199- if ( ! response . ok ) {
200- toast . error ( "Failed to update agent triggers" , {
201- richColors : true ,
188+ // For RESTful API, we need to update each registration individually
189+ for ( const triggerId of args . selectedTriggerIds ) {
190+ const triggerApiUrl = constructTriggerUrl (
191+ `/api/triggers/registrations/${ triggerId } /assistants` ,
192+ ) ;
193+ if ( ! triggerApiUrl ) {
194+ return false ;
195+ }
196+
197+ const response = await fetch ( triggerApiUrl , {
198+ method : "PUT" ,
199+ headers : {
200+ Authorization : `Bearer ${ accessToken } ` ,
201+ "Content-Type" : "application/json" ,
202+ } ,
203+ body : JSON . stringify ( {
204+ assistant_id : args . agentId ,
205+ } ) ,
202206 } ) ;
203- return false ;
207+
208+ if ( ! response . ok ) {
209+ toast . error ( "Failed to update agent triggers" , {
210+ richColors : true ,
211+ } ) ;
212+ return false ;
213+ }
204214 }
205215
206216 return true ;
207217 } ;
208218
209219 return {
210220 listTriggers,
211- listUserTriggers,
221+ listUserTriggers : listTriggerRegistrations ,
212222 registerTrigger,
213223 setupAgentTrigger,
214224 updateAgentTriggers,
0 commit comments