11import type { Context } from "hono" ;
2- import { openai } from "@ai-sdk/openai" ;
3- import { createDataStreamResponse , generateObject , streamText } from "ai" ;
2+ import { createDataStreamResponse } from "ai" ;
43import { Hono } from "hono" ;
54
6- import { PROMPTS } from "@potential/consts" ;
7- import { desc , eq , trackableLogs , trackables } from "@potential/db" ;
5+ import { createNewTrackableChatStream } from "@potential/ai" ;
86
97import type { AppContext } from "../index" ;
10- import { createConsumptionTrackables } from "./functions" ;
118
129const ai = new Hono < AppContext > ( ) ;
1310
@@ -33,96 +30,10 @@ ai.post("/chat", async (c: Context<AppContext>) => {
3330 execute : ( dataStream ) => {
3431 let accumulatedResponse = "" ;
3532
36- const result = streamText ( {
37- model : openai ( "gpt-4o-mini" ) ,
38- maxSteps : 10 ,
39- toolCallStreaming : true ,
33+ const result = createNewTrackableChatStream ( {
4034 messages,
41- system : PROMPTS . CHAT . SYSTEM ,
42- tools : {
43- getUserExistingTrackables : {
44- description : PROMPTS . TOOLS . GET_USER_EXISTING_TRACKABLES . DESCRIPTION ,
45- parameters : PROMPTS . TOOLS . GET_USER_EXISTING_TRACKABLES . PARAMETERS ,
46- execute : async ( ) => {
47- const userTrackables = await db . query . trackables . findMany ( {
48- where : eq ( trackables . ownerId , user ! . id ) ,
49-
50- columns : {
51- id : true ,
52- name : true ,
53- description : true ,
54- type : true ,
55- subType : true ,
56- subTypeCustomName : true ,
57- } ,
58- with : {
59- logs : {
60- limit : 1 ,
61- orderBy : desc ( trackableLogs . createdAt ) ,
62- columns : {
63- createdAt : true ,
64- } ,
65- } ,
66- } ,
67- } ) ;
68- const consumptionTrackables = userTrackables . filter (
69- ( trackable ) => trackable . type === "consumption" ,
70- ) ;
71- const nonConsumptionTrackables = userTrackables . filter (
72- ( trackable ) => trackable . type !== "consumption" ,
73- ) ;
74- return {
75- trackables : nonConsumptionTrackables ,
76- hasConsumptionTrackables : consumptionTrackables . length > 0 ,
77- } ;
78- } ,
79- } ,
80- generateNewNonConsumptionTrackable : {
81- description :
82- PROMPTS . TOOLS . GENERATE_NEW_NON_CONSUMPTION_TRACKABLE . DESCRIPTION ,
83- parameters :
84- PROMPTS . TOOLS . GENERATE_NEW_NON_CONSUMPTION_TRACKABLE . PARAMETERS ,
85- execute : async ( { description } : { description : string } ) => {
86- const { object : newTrackable } = await generateObject ( {
87- // issue with thinking models and optional fields in structured output: https://github.com/vercel/ai/issues/4662
88- model : openai ( "gpt-4o-mini" ) ,
89- output : "array" ,
90- schema :
91- PROMPTS . TOOLS . GENERATE_NEW_NON_CONSUMPTION_TRACKABLE . EXECUTE
92- . PROMPT . SCHEMA ,
93- system :
94- PROMPTS . TOOLS . GENERATE_NEW_NON_CONSUMPTION_TRACKABLE . EXECUTE
95- . PROMPT . SYSTEM ,
96- prompt : description ,
97- } ) ;
98- newTrackable . forEach ( ( trackable ) => {
99- console . log ( "TRACKABLE" , {
100- trackable,
101- } ) ;
102- } ) ;
103- return { trackable : newTrackable } ;
104- } ,
105- } ,
106- generateConsumptionTrackables : {
107- description :
108- PROMPTS . TOOLS . GENERATE_CONSUMPTION_TRACKABLES . DESCRIPTION ,
109- parameters :
110- PROMPTS . TOOLS . GENERATE_CONSUMPTION_TRACKABLES . PARAMETERS ,
111- execute : async ( ) => {
112- console . log ( "🍕 GENERATE CONSUMPTION TRACKABLES" ) ;
113- await createConsumptionTrackables ( { userId : user ! . id } ) ;
114- return { completed : true } ;
115- } ,
116- } ,
117- } ,
118- onChunk ( delta ) {
119- if ( delta . chunk . type === "text-delta" ) {
120- accumulatedResponse += delta . chunk . textDelta ;
121- }
122- } ,
123- async onFinish ( ) {
124- await saveToDatabase ( { content : accumulatedResponse , chatId } ) ;
125- } ,
35+ userId : user ! . id ,
36+ chatId,
12637 } ) ;
12738
12839 result . mergeIntoDataStream ( dataStream ) ;
0 commit comments