Skip to content

Commit 6d90579

Browse files
committed
Use a tool to get the userId
1 parent 31d6db1 commit 6d90579

File tree

1 file changed

+19
-7
lines changed
  • references/d3-chat/src/trigger

1 file changed

+19
-7
lines changed

references/d3-chat/src/trigger/chat.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,27 @@ const getUserTodos = tool({
119119
},
120120
});
121121

122+
const getUserId = tool({
123+
description: "Use this tool to get the user_id for the current user",
124+
parameters: z.object({}),
125+
execute: async () => {
126+
const userId = metadata.get("user_id");
127+
128+
if (!userId) {
129+
throw new Error("No user_id found");
130+
}
131+
132+
return userId;
133+
},
134+
});
135+
122136
export type TOOLS = {
123137
queryApproval: typeof queryApproval;
124138
executeSql: typeof executeSql;
125139
generateId: typeof generateId;
126140
getUserTodos: typeof getUserTodos;
127141
crawler: typeof crawler;
142+
getUserId: typeof getUserId;
128143
};
129144

130145
export type STREAMS = {
@@ -144,6 +159,8 @@ export const todoChat = schemaTask({
144159
userId: z.string(),
145160
}),
146161
run: async ({ model, input, userId }) => {
162+
metadata.set("user_id", userId);
163+
147164
const system = `
148165
You are a SQL (postgres) expert who can turn natural language descriptions for a todo app
149166
into a SQL query which can then be executed against a SQL database. Here is the schema:
@@ -165,8 +182,6 @@ export const todoChat = schemaTask({
165182
166183
Only Create, Read, Update, and Delete operations are allowed.
167184
168-
The input will be a user_id and a prompt.
169-
170185
The output will be a SQL query.
171186
172187
If the query produced is a mutation, you will need to get approval first from an admin using the queryApproval tool.
@@ -188,11 +203,7 @@ export const todoChat = schemaTask({
188203
If the user specifies a URL, you can use the crawler tool to crawl the URL and return the markdown, helping inform the SQL query.
189204
`;
190205

191-
const prompt = `
192-
User ${userId} has the following prompt: ${input}
193-
194-
Generate a SQL query to execute.
195-
`;
206+
const prompt = input;
196207

197208
const result = streamText({
198209
model: openai(model),
@@ -205,6 +216,7 @@ export const todoChat = schemaTask({
205216
generateId,
206217
getUserTodos,
207218
crawler,
219+
getUserId,
208220
},
209221
experimental_telemetry: {
210222
isEnabled: true,

0 commit comments

Comments
 (0)