Skip to content

Commit d88ca90

Browse files
committed
feat: dynamic category in AI chat + keywords support in controller
1 parent a372d34 commit d88ca90

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

app/Http/Controllers/Api/CategoryController.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function index(Request $request): JsonResponse
2222
'name' => $cat->name,
2323
'icon' => $cat->icon ?? '📌',
2424
'type' => $cat->type,
25+
'keywords' => $cat->keywords ?? [],
2526
]);
2627

2728
return $this->successResponse($categories, 'Daftar kategori berhasil diambil');
@@ -33,6 +34,8 @@ public function store(Request $request): JsonResponse
3334
'name' => 'required|string|max:50|unique:categories,name',
3435
'icon' => 'required|string|max:4',
3536
'type' => 'required|in:income,expense,both',
37+
'keywords' => 'nullable|array',
38+
'keywords.*' => 'string|max:50',
3639
]);
3740

3841
$category = Category::create($validated);
@@ -42,6 +45,7 @@ public function store(Request $request): JsonResponse
4245
'name' => $category->name,
4346
'icon' => $category->icon,
4447
'type' => $category->type,
48+
'keywords' => $category->keywords ?? [],
4549
], 'Kategori berhasil ditambahkan', 201);
4650
}
4751

@@ -56,6 +60,8 @@ public function update(Request $request, int $id): JsonResponse
5660
'name' => 'sometimes|required|string|max:50|unique:categories,name,' . $id,
5761
'icon' => 'sometimes|required|string|max:4',
5862
'type' => 'sometimes|required|in:income,expense,both',
63+
'keywords' => 'nullable|array',
64+
'keywords.*' => 'string|max:50',
5965
]);
6066

6167
$category->update($validated);
@@ -65,6 +71,7 @@ public function update(Request $request, int $id): JsonResponse
6571
'name' => $category->name,
6672
'icon' => $category->icon,
6773
'type' => $category->type,
74+
'keywords' => $category->keywords ?? [],
6875
], 'Kategori berhasil diperbarui');
6976
}
7077

app/Services/AiTransactionParserService.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,20 @@ private function nullableString(mixed $value): ?string
202202

203203
private function systemPrompt(): string
204204
{
205-
return <<<'PROMPT'
205+
$categories = Category::query()
206+
->orderBy('name')
207+
->get()
208+
->map(fn (Category $cat) => $cat->name)
209+
->implode(', ');
210+
211+
return <<<PROMPT
206212
Extract one Indonesian finance transaction from the user message.
207213
Return only valid JSON with keys: response, description, amount, category, type.
208214
amount must be an integer rupiah value or null.
209215
type must be "expense", "income", or null.
210216
Use "income" if the message contains: dapat, dapet, dpt, gaji, bonus, dibayar, transfer masuk, pendapatan, fee.
211217
Use "expense" if the message contains: beli, bayar, belanja, isi, sewa, tagihan, potong, hutang.
212-
category should be one of: MAKANAN, TRANSPORT, BELANJA, TAGIHAN, HIBURAN, GAJI, LAINNYA.
218+
category should be one of: {$categories}.
213219
If the message has no amount, set description, amount, category, and type to null.
214220
PROMPT;
215221
}

0 commit comments

Comments
 (0)