Skip to content

Commit aedc6bd

Browse files
committed
ai: new flow, crack consumption trackers 2
1 parent f1a62a9 commit aedc6bd

7 files changed

Lines changed: 92 additions & 2 deletions

File tree

apps/backend/api/ai/chat.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { PROMPTS } from "@potential/consts";
77
import { desc, eq, trackableLogs, trackables } from "@potential/db";
88

99
import type { AppContext } from "../index";
10+
import { createConsumptionTrackables } from "./functions";
1011

1112
const ai = new Hono<AppContext>();
1213

@@ -109,6 +110,7 @@ ai.post("/chat", async (c: Context<AppContext>) => {
109110
PROMPTS.TOOLS.GENERATE_CONSUMPTION_TRACKABLES.PARAMETERS,
110111
execute: async () => {
111112
console.log("🍕 GENERATE CONSUMPTION TRACKABLES");
113+
await createConsumptionTrackables({ userId: user!.id });
112114
return { completed: true };
113115
},
114116
},

apps/backend/api/ai/functions.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { InferInsertModel } from "@potential/db";
2+
import type { CloudTypeId } from "@potential/utils";
3+
import { and, db, eq, trackables } from "@potential/db";
4+
import { TRACKABLE_TEMPLATES } from "@potential/templates";
5+
import { cloudTypeIdGenerator } from "@potential/utils";
6+
7+
export async function createConsumptionTrackables({
8+
userId,
9+
}: {
10+
userId: CloudTypeId<"user">;
11+
}) {
12+
console.log("🍕 CREATING CONSUMPTION TRACKABLES FUNCTION");
13+
const userTrackables = await db.query.trackables.findMany({
14+
where: and(
15+
eq(trackables.ownerId, userId),
16+
eq(trackables.type, "consumption"),
17+
),
18+
columns: {
19+
id: true,
20+
},
21+
});
22+
if (userTrackables.length > 0) {
23+
return;
24+
}
25+
26+
const templates = TRACKABLE_TEMPLATES.consumption;
27+
28+
const newTrackablesToInsertArray: InferInsertModel<typeof trackables>[] = [];
29+
for (const template of Object.values(templates)) {
30+
newTrackablesToInsertArray.push({
31+
id: cloudTypeIdGenerator("trackable"),
32+
ownerId: userId,
33+
name: template.name,
34+
description: template.description,
35+
color: null,
36+
configType: template.defaultConfig.type,
37+
public: false,
38+
type: "consumption",
39+
subType: template.subType,
40+
customConfig: template.defaultConfig,
41+
});
42+
}
43+
console.log("🍕 INSERTING CONSUMPTION TRACKABLES", {
44+
newTrackablesToInsertArray,
45+
});
46+
await db.insert(trackables).values(newTrackablesToInsertArray);
47+
48+
return;
49+
}

apps/backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@potential/db": "workspace:*",
2121
"@potential/env": "workspace:*",
2222
"@potential/storage": "workspace:*",
23+
"@potential/templates": "workspace:*",
2324
"@potential/trpc": "workspace:*",
2425
"@potential/utils": "workspace:*",
2526
"@react-email/components": "^0.0.38",

apps/mobile/src/app/(home)/new.tsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Button } from "~/components/ui/button";
1717
import { Card } from "~/components/ui/card";
1818
import { LongText } from "~/components/ui/long-text";
1919
import { Text as UIText } from "~/components/ui/text";
20-
import { getAuthHeaders } from "~/utils/api";
20+
import { getAuthHeaders, queryClient, trpc } from "~/utils/api";
2121
import { getApiUrl } from "~/utils/base-url";
2222
import { iconColor } from "~/utils/ui";
2323

@@ -243,7 +243,7 @@ function ToolInvocationComponent({
243243
</Card>
244244
);
245245
case "call": {
246-
const args = toolInvocation.args as
246+
const _args = toolInvocation.args as
247247
| { description?: string }
248248
| undefined;
249249
return (
@@ -353,10 +353,40 @@ export default function NewTrackableScreen() {
353353
},
354354
);
355355

356+
const processedToolCallIds = useRef(new Set<string>());
357+
356358
useEffect(() => {
357359
if (scrollViewRef.current) {
358360
scrollViewRef.current.scrollToEnd({ animated: true });
359361
}
362+
363+
const lastMessage = messages[messages.length - 1];
364+
if (lastMessage) {
365+
for (const part of lastMessage.parts) {
366+
if (
367+
part.type === "tool-invocation" &&
368+
part.toolInvocation.toolName === "generateConsumptionTrackables" &&
369+
part.toolInvocation.state === "result" &&
370+
!processedToolCallIds.current.has(part.toolInvocation.toolCallId)
371+
) {
372+
queryClient
373+
.invalidateQueries({
374+
queryKey: trpc.trackables.getTrackablesForParentType.queryKey({
375+
trackableParentType: "consumption",
376+
}),
377+
})
378+
.then(() => {
379+
processedToolCallIds.current.add(part.toolInvocation.toolCallId);
380+
})
381+
.catch((error: Error) => {
382+
console.error(
383+
"Failed to invalidate consumption trackables queries:",
384+
error,
385+
);
386+
});
387+
}
388+
}
389+
}
360390
}, [messages]);
361391

362392
const onActualSubmit = () => {

packages/db/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export type { InferInsertModel, InferSelectModel } from "drizzle-orm";
12
export { alias } from "drizzle-orm/mysql-core";
23
export * from "drizzle-orm/sql";
34
export * from "./client";
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
// packages/templates/src/templates/index.ts
22
import { bodyTemplates } from "./body";
3+
import { consumptionTemplates } from "./consumption";
34
import { mindTemplates } from "./mind";
45
import { TRACKABLE_TEMPLATES } from "./templates-registry";
56

67
// Populate the registry
78
TRACKABLE_TEMPLATES.body = bodyTemplates;
89
TRACKABLE_TEMPLATES.mind = mindTemplates;
10+
TRACKABLE_TEMPLATES.consumption = consumptionTemplates;
911

1012
export * from "./body";
13+
export * from "./consumption";
1114
export * from "./mind";
15+
1216
export { TRACKABLE_TEMPLATES };

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)