Skip to content

Commit 1b2f336

Browse files
committed
refactor: streamline tool function parameters and enhance workspace management
- Updated tool functions in project.service.ts to accept workspaceId instead of userId for improved clarity and functionality. - Modified execToolByName in ask.service.ts to remove userId dependency, aligning with the new parameter structure. - Enhanced tools.constants.ts to reflect the updated parameter requirements for getProjects and getMembers functions. - Integrated workspaceId and projectId into frontend components for better state management and user context. - Cleaned up unnecessary console logs in messages.controller.ts and project-management.service.ts for improved code quality.
1 parent 80d05b0 commit 1b2f336

File tree

9 files changed

+104
-58
lines changed

9 files changed

+104
-58
lines changed

backend/src/ai-agent/ask.service.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,32 @@ export class AskService {
7171
* Helper: execute a tool by name (serial)
7272
* Returns the raw tool result (JS object/array) or { error: ... }
7373
*/
74-
private async execToolByName(fn: string, args: any, userId: string) {
74+
private async execToolByName(fn: string, args: any) {
7575
try {
7676
if (fn === "getRepos") {
7777
return await getRepos();
7878
} else if (fn === "getContributors") {
7979
return await getContributors(args.repo);
8080
} else if (fn === "getProjects") {
81-
return await getProjects(userId);
81+
return await getProjects(args.workspaceId);
8282
} else if (fn === "getMembers") {
83-
return await getMembers(userId);
83+
return await getMembers(args.workspaceId);
8484
} else if (fn === "getAllTasks") {
85-
return await getAllTasks(args?.projectId || "", userId);
85+
return await getAllTasks(args?.projectId);
8686
} else if (fn === "getTodoTasks") {
87-
return await getTodoTasks(args?.projectId || "", userId);
87+
return await getTodoTasks(args?.projectId);
8888
} else if (fn === "getInProgressTasks") {
89-
return await getInProgressTasks(args?.projectId || "", userId);
89+
return await getInProgressTasks(args?.projectId);
9090
} else if (fn === "getDoneTasks") {
91-
return await getDoneTasks(args?.projectId || "", userId);
91+
return await getDoneTasks(args?.projectId);
9292
} else if (fn === "getUnassignedTasks") {
93-
return await getUnassignedTasks(args?.projectId || "", userId);
93+
return await getUnassignedTasks(args?.projectId);
9494
} else if (fn === "getUrgentTasks") {
95-
return await getUrgentTasks(args?.projectId || "", userId);
95+
return await getUrgentTasks(args?.projectId);
9696
} else if (fn === "getLowTasks") {
97-
return await getLowTasks(args?.projectId || "", userId);
97+
return await getLowTasks(args?.projectId);
9898
} else if (fn === "getMediumTasks") {
99-
return await getMediumTasks(args?.projectId || "", userId);
99+
return await getMediumTasks(args?.projectId);
100100
} else {
101101
return { error: `Unknown tool: ${fn}` };
102102
}
@@ -132,6 +132,14 @@ export class AskService {
132132
content: SYSTEM_MESSAGE,
133133
},
134134
...userMessages.map((msg) => ({ role: msg.role, content: msg.content })),
135+
{
136+
role: "system",
137+
content: `User Selected Workspace ID: ${data.workspaceId}`,
138+
},
139+
{
140+
role: "system",
141+
content: `User Selected Project ID: ${data.projectId}`,
142+
},
135143
...data.messages,
136144
];
137145

@@ -223,7 +231,7 @@ export class AskService {
223231
} else {
224232
// Execute actual tool
225233
this.emitToolCall(socket, fn, args);
226-
toolResult = await this.execToolByName(fn, args, userId ?? "");
234+
toolResult = await this.execToolByName(fn, args);
227235
this.emitToolResult(socket, fn, toolResult);
228236

229237
// push tool result into conversation

backend/src/ai-agent/messages/messages.controller.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export class MessagesController {
2222

2323
@Delete(":id")
2424
delete(@Req() req: any, @Param("id") id: string) {
25-
console.log("delete message id: ", id);
2625
const userId = req.user.userId; // <-- ambil userId dari JWT
2726
return this.messagesService.delete(userId, id);
2827
}

backend/src/ai-agent/project.service.ts

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,14 @@ const prisma = new PrismaClient();
99
* PROJECTS
1010
* =====================================
1111
*/
12-
export async function getProjects(userId: string) {
12+
export async function getProjects(workspaceId: string) {
1313
try {
1414
console.log("getProjects");
15-
const team = await prisma.teamMember.findMany({
16-
where: {
17-
userId,
18-
},
19-
});
20-
const workspaceIds = team.map((item: any) => item.workspaceId);
2115

2216
const results = await prisma.project.findMany({
2317
where: {
2418
isTrash: false,
25-
workspaceId: {
26-
in: workspaceIds,
27-
},
19+
workspaceId,
2820
},
2921
include: {
3022
tasks: {
@@ -90,7 +82,7 @@ function baseTaskInclude() {
9082
};
9183
}
9284

93-
function buildTaskWhere(userId: string, projectId?: string, status?: string) {
85+
function buildTaskWhere(projectId?: string, status?: string) {
9486
// Only filter by project (no cross-workspace lookup)
9587
const where: any = { isTrash: false };
9688

@@ -111,12 +103,12 @@ function buildTaskWhere(userId: string, projectId?: string, status?: string) {
111103
* ALL TASKS
112104
* =====================================
113105
*/
114-
export async function getAllTasks(projectId = "", userId: string) {
106+
export async function getAllTasks(projectId = "") {
115107
try {
116108
console.log("getAllTasks");
117109

118110
const results = await prisma.task.findMany({
119-
where: await buildTaskWhere(userId, projectId),
111+
where: await buildTaskWhere(projectId),
120112
include: baseTaskInclude(),
121113
orderBy: { createdAt: "desc" },
122114
});
@@ -133,12 +125,12 @@ export async function getAllTasks(projectId = "", userId: string) {
133125
* TODO TASKS
134126
* =====================================
135127
*/
136-
export async function getTodoTasks(projectId = "", userId: string) {
128+
export async function getTodoTasks(projectId = "") {
137129
try {
138130
console.log("getTodoTasks");
139131

140132
const results = await prisma.task.findMany({
141-
where: await buildTaskWhere(userId, projectId, "todo"),
133+
where: await buildTaskWhere(projectId, "todo"),
142134
include: baseTaskInclude(),
143135
orderBy: { createdAt: "desc" },
144136
});
@@ -155,12 +147,12 @@ export async function getTodoTasks(projectId = "", userId: string) {
155147
* IN PROGRESS TASKS
156148
* =====================================
157149
*/
158-
export async function getInProgressTasks(projectId = "", userId: string) {
150+
export async function getInProgressTasks(projectId = "") {
159151
try {
160152
console.log("getInProgressTasks");
161153

162154
const results = await prisma.task.findMany({
163-
where: await buildTaskWhere(userId, projectId, "inprogress"),
155+
where: await buildTaskWhere(projectId, "inprogress"),
164156
include: baseTaskInclude(),
165157
orderBy: { createdAt: "desc" },
166158
});
@@ -177,12 +169,12 @@ export async function getInProgressTasks(projectId = "", userId: string) {
177169
* DONE TASKS
178170
* =====================================
179171
*/
180-
export async function getDoneTasks(projectId = "", userId: string) {
172+
export async function getDoneTasks(projectId = "") {
181173
try {
182174
console.log("getDoneTasks");
183175

184176
const results = await prisma.task.findMany({
185-
where: await buildTaskWhere(userId, projectId, "done"),
177+
where: await buildTaskWhere(projectId, "done"),
186178
include: baseTaskInclude(),
187179
orderBy: { createdAt: "desc" },
188180
});
@@ -199,12 +191,12 @@ export async function getDoneTasks(projectId = "", userId: string) {
199191
* MEMBERS
200192
* =====================================
201193
*/
202-
export async function getMembers(userId: string) {
194+
export async function getMembers(workspaceId: string) {
203195
try {
204196
console.log("getMembers");
205197

206198
const results = await prisma.teamMember.findMany({
207-
where: { isTrash: false },
199+
where: { isTrash: false, workspaceId },
208200
include: {
209201
Task: {
210202
where: { isTrash: false },
@@ -249,10 +241,7 @@ export async function getMembers(userId: string) {
249241
}
250242
}
251243

252-
export async function getUnassignedTasks(
253-
projectId: string = "",
254-
userId: string
255-
) {
244+
export async function getUnassignedTasks(projectId: string = "") {
256245
try {
257246
console.log("getUnassignedTasks");
258247

@@ -276,7 +265,7 @@ export async function getUnassignedTasks(
276265
}
277266
}
278267

279-
export async function getUrgentTasks(projectId: string = "", userId: string) {
268+
export async function getUrgentTasks(projectId: string = "") {
280269
try {
281270
console.log("getUrgentTasks");
282271

@@ -300,7 +289,7 @@ export async function getUrgentTasks(projectId: string = "", userId: string) {
300289
}
301290
}
302291

303-
export async function getLowTasks(projectId: string = "", userId: string) {
292+
export async function getLowTasks(projectId: string = "") {
304293
try {
305294
console.log("getLowTasks");
306295

@@ -324,7 +313,7 @@ export async function getLowTasks(projectId: string = "", userId: string) {
324313
}
325314
}
326315

327-
export async function getMediumTasks(projectId: string = "", userId: string) {
316+
export async function getMediumTasks(projectId: string = "") {
328317
try {
329318
console.log("getMediumTasks");
330319

backend/src/ai-agent/tools.constants.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ export const tools = [
9090
type: "function",
9191
function: {
9292
name: "getProjects",
93+
parameters: {
94+
type: "object",
95+
properties: {
96+
workspaceId: {
97+
type: "string",
98+
description: "The target workspaceId.",
99+
},
100+
},
101+
required: ["workspaceId"],
102+
},
93103
},
94104
outputFormat: {
95105
projects: [
@@ -125,6 +135,16 @@ export const tools = [
125135
type: "function",
126136
function: {
127137
name: "getMembers",
138+
parameters: {
139+
type: "object",
140+
properties: {
141+
workspaceId: {
142+
type: "string",
143+
description: "The target workspaceId.",
144+
},
145+
},
146+
required: ["workspaceId"],
147+
},
128148
},
129149
outputFormat: {
130150
members: [
@@ -162,10 +182,10 @@ export const tools = [
162182
properties: {
163183
projectId: {
164184
type: "string",
165-
description:
166-
"Optional project ID. If provided, return only tasks from that project.",
185+
description: "The target projectId.",
167186
},
168187
},
188+
required: ["projectId"],
169189
},
170190
},
171191
outputFormat: {
@@ -210,9 +230,10 @@ export const tools = [
210230
properties: {
211231
projectId: {
212232
type: "string",
213-
description: "Optional project ID.",
233+
description: "The target projectId.",
214234
},
215235
},
236+
required: ["projectId"],
216237
},
217238
},
218239
outputFormat: {
@@ -236,9 +257,10 @@ export const tools = [
236257
properties: {
237258
projectId: {
238259
type: "string",
239-
description: "Optional project ID.",
260+
description: "The target projectId.",
240261
},
241262
},
263+
required: ["projectId"],
242264
},
243265
},
244266
outputFormat: {
@@ -262,9 +284,10 @@ export const tools = [
262284
properties: {
263285
projectId: {
264286
type: "string",
265-
description: "Optional project ID.",
287+
description: "The target projectId.",
266288
},
267289
},
290+
required: ["projectId"],
268291
},
269292
},
270293
outputFormat: {
@@ -297,9 +320,10 @@ export const tools = [
297320
properties: {
298321
projectId: {
299322
type: "string",
300-
description: "Optional project ID.",
323+
description: "The target projectId.",
301324
},
302325
},
326+
required: ["projectId"],
303327
},
304328
},
305329
outputFormat: {
@@ -324,9 +348,10 @@ export const tools = [
324348
properties: {
325349
projectId: {
326350
type: "string",
327-
description: "Optional project ID.",
351+
description: "The target projectId.",
328352
},
329353
},
354+
required: ["projectId"],
330355
},
331356
},
332357
outputFormat: {
@@ -350,9 +375,10 @@ export const tools = [
350375
properties: {
351376
projectId: {
352377
type: "string",
353-
description: "Optional project ID.",
378+
description: "The target projectId.",
354379
},
355380
},
381+
required: ["projectId"],
356382
},
357383
},
358384
outputFormat: {
@@ -376,9 +402,10 @@ export const tools = [
376402
properties: {
377403
projectId: {
378404
type: "string",
379-
description: "Optional project ID.",
405+
description: "The target projectId.",
380406
},
381407
},
408+
required: ["projectId"],
382409
},
383410
},
384411
outputFormat: {

backend/src/common/ai.constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ EXAMPLE USER INPUT (CROSS ANALYSIS)
196196
GENERAL PRINCIPLES:
197197
====================================================================
198198
199-
- Final answers must always be plain text (not JSON).
199+
- Final answers must always be text with markdown format (not JSON).
200200
- Tool calls must strictly follow: { "tool": "...", "arguments": {...} }.
201201
- No process narration.
202202
- Do not invent data not found in tool results.

backend/src/project-management/project-management.service.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,7 @@ export class ProjectManagementService {
384384
where: { clientId: payload.clientId },
385385
});
386386
if (existing) {
387-
console.log(
388-
"[createTask] idempotent hit, returning existing id=",
389-
existing.id
390-
);
387+
console.log("[createTask] idempotent hit, returning existing");
391388
return existing;
392389
}
393390
}

0 commit comments

Comments
 (0)