Skip to content

Commit 5b89898

Browse files
change triggers api to use new server format (#376)
Co-authored-by: Brace Sproul <[email protected]>
1 parent e0a0a55 commit 5b89898

File tree

10 files changed

+98
-81
lines changed

10 files changed

+98
-81
lines changed

apps/web-v2/src/components/config-fields/triggers.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import {
1717
} from "@/components/ui/popover";
1818
import { Button } from "@/components/ui/button";
1919
import { cn } from "@/lib/utils";
20-
import { useTriggers, ListUserTriggersData } from "@/hooks/use-triggers";
20+
import {
21+
useTriggers,
22+
ListTriggerRegistrationsData,
23+
} from "@/hooks/use-triggers";
2124
import { groupUserRegisteredTriggersByProvider } from "@/lib/triggers";
2225
import { Badge } from "@/components/ui/badge";
2326
import { toast } from "sonner";
@@ -36,7 +39,7 @@ export function ConfigFieldTriggers({
3639
const { listUserTriggers } = useTriggers();
3740

3841
const [userTriggers, setUserTriggers] = React.useState<
39-
ListUserTriggersData[]
42+
ListTriggerRegistrationsData[]
4043
>([]);
4144
const [loading, setLoading] = React.useState(false);
4245
const [open, setOpen] = React.useState(false);
@@ -94,7 +97,7 @@ export function ConfigFieldTriggers({
9497
variant="secondary"
9598
className="text-xs"
9699
>
97-
{trigger.provider_id}:{JSON.stringify(trigger.resource)}
100+
{trigger.template_id}:{JSON.stringify(trigger.resource)}
98101
</Badge>
99102
))}
100103
{selectedTriggers.length > 2 && (
@@ -183,7 +186,7 @@ export function ConfigFieldTriggers({
183186
className="flex items-center gap-1 text-xs"
184187
>
185188
<>
186-
{trigger.provider_id}:{JSON.stringify(trigger.resource)}
189+
{trigger.template_id}:{JSON.stringify(trigger.resource)}
187190
<TooltipIconButton
188191
tooltip="Remove trigger"
189192
onClick={() => handleTriggerToggle(trigger.id)}

apps/web-v2/src/features/triggers/components/trigger-card.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ import {
1212
import { TriggerForm } from "./trigger-form";
1313
import type { Trigger } from "@/types/triggers";
1414
import { Zap, ChevronDown } from "lucide-react";
15-
import { ListUserTriggersData } from "@/hooks/use-triggers";
15+
import { ListTriggerRegistrationsData } from "@/hooks/use-triggers";
1616
import { ResourceRenderer } from "./resource-renderer";
1717

1818
interface TriggerCardProps {
1919
trigger: Trigger;
20-
userTriggers: ListUserTriggersData[];
20+
userTriggers: ListTriggerRegistrationsData[];
2121
}
2222

23-
function ConfiguredAccounts(props: { userTriggers: ListUserTriggersData[] }) {
23+
function ConfiguredAccounts(props: {
24+
userTriggers: ListTriggerRegistrationsData[];
25+
}) {
2426
const { userTriggers } = props;
2527
const [isAccountsExpanded, setIsAccountsExpanded] = useState(false);
2628

apps/web-v2/src/features/triggers/components/trigger-form.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ export function TriggerForm({ trigger, onCancel }: TriggerFormProps) {
7979
return;
8080
}
8181

82-
// Check for auth URL in response
83-
if (!registerResponse.registered && registerResponse.authUrl) {
84-
setAuthUrl(registerResponse.authUrl);
82+
// Handle response based on auth requirements
83+
if ("auth_url" in registerResponse) {
84+
setAuthUrl(registerResponse.auth_url);
8585
setIsAuthenticating(true);
8686
} else {
8787
toast.success(

apps/web-v2/src/features/triggers/index.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import {
1010
CardTitle,
1111
} from "@/components/ui/card";
1212
import { Zap } from "lucide-react";
13-
import { ListUserTriggersData, useTriggers } from "@/hooks/use-triggers";
13+
import {
14+
ListTriggerRegistrationsData,
15+
useTriggers,
16+
} from "@/hooks/use-triggers";
1417
import { useEffect, useState } from "react";
1518
import type { Trigger } from "@/types/triggers";
1619
import { toast } from "sonner";
@@ -19,7 +22,9 @@ import { groupUserRegisteredTriggersByProvider } from "@/lib/triggers";
1922
export default function TriggersInterface() {
2023
const [triggersLoading, setTriggersLoading] = useState(true);
2124
const [triggers, setTriggers] = useState<Trigger[]>([]);
22-
const [userTriggers, setUserTriggers] = useState<ListUserTriggersData[]>([]);
25+
const [userTriggers, setUserTriggers] = useState<
26+
ListTriggerRegistrationsData[]
27+
>([]);
2328
const auth = useAuthContext();
2429
const { listTriggers, listUserTriggers } = useTriggers();
2530

@@ -141,7 +146,7 @@ export default function TriggersInterface() {
141146
trigger={trigger}
142147
userTriggers={
143148
groupUserRegisteredTriggersByProvider(userTriggers)?.[
144-
trigger.providerId
149+
trigger.id
145150
] || []
146151
}
147152
/>

apps/web-v2/src/hooks/use-triggers.tsx

Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@ import { toast } from "sonner";
33

44
type 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,

apps/web-v2/src/lib/agent-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,5 @@ export function isDefaultGraph(
125125
deployment: Deployment,
126126
graphId: string,
127127
): boolean {
128-
return deployment.agents.find((a) => a.isDefault)?.graphId === graphId;
128+
return deployment.graphs.find((g) => g.isDefault)?.id === graphId;
129129
}

apps/web-v2/src/lib/environment/deployments.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ export function getDeployments(): Deployment[] {
99
const deployments: Deployment[] = JSON.parse(
1010
process.env.NEXT_PUBLIC_DEPLOYMENTS || "[]",
1111
);
12+
1213
for (const deployment of deployments) {
1314
if (deployment.isDefault && !defaultExists) {
14-
if (!deployment.agents.find((a) => a.isDefault)) {
15+
if (!deployment.graphs.find((g) => g.isDefault)) {
1516
throw new Error("Default deployment must have a default graph ID");
1617
}
1718
defaultExists = true;

apps/web-v2/src/lib/triggers.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ListUserTriggersData } from "@/hooks/use-triggers";
1+
import { ListTriggerRegistrationsData } from "@/hooks/use-triggers";
22

33
export function generateFormFields(schema: Record<string, any>) {
44
const fields: Array<{
@@ -23,15 +23,15 @@ export function generateFormFields(schema: Record<string, any>) {
2323
}
2424

2525
export function groupUserRegisteredTriggersByProvider(
26-
triggers: ListUserTriggersData[],
27-
): Record<string, ListUserTriggersData[]> {
28-
const groupedTriggers: Record<string, ListUserTriggersData[]> = {};
26+
triggers: ListTriggerRegistrationsData[],
27+
): Record<string, ListTriggerRegistrationsData[]> {
28+
const groupedTriggers: Record<string, ListTriggerRegistrationsData[]> = {};
2929

3030
triggers.forEach((trigger) => {
31-
if (!groupedTriggers[trigger.provider_id]) {
32-
groupedTriggers[trigger.provider_id] = [];
31+
if (!groupedTriggers[trigger.template_id]) {
32+
groupedTriggers[trigger.template_id] = [];
3333
}
34-
groupedTriggers[trigger.provider_id].push(trigger);
34+
groupedTriggers[trigger.template_id].push(trigger);
3535
});
3636

3737
return groupedTriggers;

apps/web-v2/src/types/deployment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface Graph {
3131
/**
3232
* The ID of the graph.
3333
*/
34-
graphId: string;
34+
id: string;
3535
/**
3636
* Whether or not this graph is the default graph for this deployment.
3737
* This should only be set to true for one graph per deployment.

apps/web-v2/src/types/triggers.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
export interface Trigger {
22
/**
3-
* The provider ID of the trigger
4-
*/
5-
providerId: string;
6-
/**
7-
* A unique UUID v4 to identify the trigger by
3+
* A unique identifier for the trigger (e.g., "gmail-email-received")
84
*/
95
id: string;
106
/**

0 commit comments

Comments
 (0)