-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathuse-project-ai.ts
More file actions
185 lines (163 loc) · 6.06 KB
/
Copy pathuse-project-ai.ts
File metadata and controls
185 lines (163 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { toast } from "sonner";
import { client } from "@/lib/rpc";
import { useTourActive, DUMMY_PROJECT_AI_CONTEXT } from "@/lib/tour-dummy-data";
import { ProjectAIContext, ProjectAIAnswer, AITaskResponse } from "../types/ai-context";
// Query key factory
export const PROJECT_AI_QUERY_KEYS = {
context: (projectId: string) => ["project-ai-context", projectId],
questions: (projectId: string) => ["project-ai-questions", projectId],
};
// Hook to get AI context for a project
export const useGetProjectAIContext = (projectId: string, workspaceId: string) => {
const isTourActive = useTourActive();
return useQuery<{ data: ProjectAIContext }>({
queryKey: [...PROJECT_AI_QUERY_KEYS.context(projectId), isTourActive],
queryFn: async () => {
// DUMMY DATA FOR TOUR
if (isTourActive) {
console.log("[Tour] Returning dummy project AI context");
return { data: DUMMY_PROJECT_AI_CONTEXT as ProjectAIContext };
}
const response = await client.api["project-docs"].ai.context.$get({
query: { projectId, workspaceId },
});
if (!response.ok) {
const error = await response.json() as { error?: string };
throw new Error(error.error || "Failed to fetch AI context");
}
return await response.json();
},
enabled: !!projectId && !!workspaceId,
staleTime: 1000 * 60 * 5, // Cache for 5 minutes
});
};
// Hook to ask a question about the project
export const useAskProjectQuestion = () => {
return useMutation<
{ data: ProjectAIAnswer },
Error,
{ projectId: string; workspaceId: string; question: string }
>({
mutationFn: async ({ projectId, workspaceId, question }) => {
const response = await client.api["project-docs"].ai.ask.$post({
json: { projectId, workspaceId, question },
});
if (!response.ok) {
const error = await response.json() as { error?: string };
throw new Error(error.error || "Failed to get AI response");
}
return await response.json();
},
onError: (error) => {
toast.error(error.message || "Failed to get AI response");
},
});
};
// Hook to create a task using AI
export const useAICreateTask = () => {
const queryClient = useQueryClient();
return useMutation<
AITaskResponse,
Error,
{ projectId: string; workspaceId: string; prompt: string; autoExecute?: boolean; type?: string }
>({
mutationFn: async ({ projectId, workspaceId, prompt, autoExecute, type }) => {
const response = await client.api["project-docs"].ai["create-task"].$post({
json: { projectId, workspaceId, prompt, autoExecute, type },
});
if (!response.ok) {
const error = await response.json() as { error?: string };
throw new Error(error.error || "Failed to create task via AI");
}
return await response.json() as AITaskResponse;
},
onSuccess: (data) => {
if (data.action?.executed && data.success) {
queryClient.invalidateQueries({ queryKey: ["work-items"] });
queryClient.invalidateQueries({ queryKey: ["project-analytics"] });
toast.success(data.message || "Task created successfully");
}
},
onError: (error) => {
toast.error(error.message || "Failed to create task via AI");
},
});
};
// Hook to update a task using AI
export const useAIUpdateTask = () => {
const queryClient = useQueryClient();
return useMutation<
AITaskResponse,
Error,
{ projectId: string; workspaceId: string; taskId: string; prompt: string; autoExecute?: boolean }
>({
mutationFn: async ({ projectId, workspaceId, taskId, prompt, autoExecute }) => {
const response = await client.api["project-docs"].ai["update-task"].$post({
json: { projectId, workspaceId, taskId, prompt, autoExecute },
});
if (!response.ok) {
const error = await response.json() as { error?: string };
throw new Error(error.error || "Failed to update task via AI");
}
return await response.json() as AITaskResponse;
},
onSuccess: (data) => {
if (data.action?.executed && data.success) {
queryClient.invalidateQueries({ queryKey: ["work-items"] });
queryClient.invalidateQueries({ queryKey: ["work-item", data.task?.id] });
queryClient.invalidateQueries({ queryKey: ["project-analytics"] });
toast.success(data.message || "Task updated successfully");
}
},
onError: (error) => {
toast.error(error.message || "Failed to update task via AI");
},
});
};
// Hook to execute a task suggestion (create or update)
export const useExecuteTaskSuggestion = () => {
const queryClient = useQueryClient();
return useMutation<
AITaskResponse,
Error,
{
projectId: string;
workspaceId: string;
taskData: {
name?: string;
description?: string | null;
status?: string;
priority?: string;
dueDate?: string | null;
endDate?: string | null;
assigneeIds?: string[];
labels?: string[];
estimatedHours?: number | null;
};
taskId?: string;
}
>({
mutationFn: async ({ projectId, workspaceId, taskData, taskId }) => {
const response = await client.api["project-docs"].ai["execute-task"].$post({
json: { projectId, workspaceId, taskData, taskId },
});
if (!response.ok) {
const error = await response.json() as { error?: string };
throw new Error(error.error || "Failed to execute task operation");
}
return await response.json() as AITaskResponse;
},
onSuccess: (data) => {
queryClient.invalidateQueries({ queryKey: ["work-items"] });
queryClient.invalidateQueries({ queryKey: ["project-analytics"] });
if (data.task?.id) {
queryClient.invalidateQueries({ queryKey: ["work-item", data.task.id] });
}
toast.success(data.message || "Task operation completed");
},
onError: (error) => {
toast.error(error.message || "Failed to execute task operation");
},
});
};