Skip to content

Commit 7f6f30c

Browse files
authored
feat: add metadata to code apply call for debugging (#2580)
1 parent 4519b71 commit 7f6f30c

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

apps/web/client/src/components/tools.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,16 @@ async function handleEditFileTool(
148148
throw new Error('Binary files are not supported for editing');
149149
}
150150

151+
const metadata = {
152+
projectId: editorEngine.projectId,
153+
conversationId: editorEngine.chat.conversation.current?.id,
154+
};
155+
151156
const updatedContent = await api.code.applyDiff.mutate({
152157
originalCode: originalFile.content,
153158
updateSnippet: args.content,
154159
instruction: args.instruction,
160+
metadata,
155161
});
156162
if (!updatedContent.result) {
157163
throw new Error('Error applying code change: ' + updatedContent.error);

apps/web/client/src/server/api/routers/code.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@ export const codeRouter = createTRPCRouter({
99
.input(z.object({
1010
originalCode: z.string(),
1111
updateSnippet: z.string(),
12-
instruction: z.string()
12+
instruction: z.string(),
13+
metadata: z.object({
14+
projectId: z.string().optional(),
15+
conversationId: z.string().optional(),
16+
}).optional(),
1317
}))
14-
.mutation(async ({ input }): Promise<{ result: string | null, error: string | null }> => {
18+
.mutation(async ({ input, ctx }): Promise<{ result: string | null, error: string | null }> => {
1519
try {
16-
const result = await applyCodeChange(input.originalCode, input.updateSnippet, input.instruction);
20+
const user = ctx.user;
21+
const metadata = {
22+
...input.metadata,
23+
userId: user.id,
24+
};
25+
const result = await applyCodeChange(input.originalCode, input.updateSnippet, input.instruction, metadata);
1726
if (!result) {
1827
throw new Error('Failed to apply code change. Please try again.');
1928
}

packages/ai/src/apply/client.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import OpenAI from 'openai';
22

3+
export interface ApplyCodeChangeMetadata {
4+
userId?: string;
5+
projectId?: string;
6+
conversationId?: string;
7+
}
8+
39
const createPrompt = (originalCode: string, updateSnippet: string, instruction: string) =>
410
`<instruction>${instruction}</instruction>\n<code>${originalCode}</code>\n<update>${updateSnippet}</update>`;
511

@@ -37,6 +43,8 @@ export async function applyCodeChangeWithMorph(
3743
export async function applyCodeChangeWithRelace(
3844
originalCode: string,
3945
updateSnippet: string,
46+
instruction: string,
47+
metadata?: ApplyCodeChangeMetadata,
4048
): Promise<string | null> {
4149
const apiKey = process.env.RELACE_API_KEY;
4250
if (!apiKey) {
@@ -51,6 +59,14 @@ export async function applyCodeChangeWithRelace(
5159
const data = {
5260
initialCode: originalCode,
5361
editSnippet: updateSnippet,
62+
instructions: instruction,
63+
relaceMetadata: metadata
64+
? {
65+
onlookUserId: metadata.userId,
66+
onlookProjectId: metadata.projectId,
67+
onlookConversationId: metadata.conversationId,
68+
}
69+
: undefined,
5470
};
5571

5672
const response = await fetch(url, {
@@ -69,6 +85,7 @@ export async function applyCodeChange(
6985
originalCode: string,
7086
updateSnippet: string,
7187
instruction: string,
88+
metadata?: ApplyCodeChangeMetadata,
7289
preferredProvider: FastApplyProvider = FastApplyProvider.RELACE,
7390
): Promise<string | null> {
7491
const providerAttempts = [
@@ -104,6 +121,8 @@ export async function applyCodeChange(
104121
: await (applyFn as typeof applyCodeChangeWithRelace)(
105122
originalCode,
106123
updateSnippet,
124+
instruction,
125+
metadata,
107126
);
108127
if (result) return result;
109128
} catch (error) {

packages/ai/test/apply.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ async function fetchUserData(userId: string): Promise<User> {
4848
originalCode,
4949
updateSnippet,
5050
'I will add email field to User interface and improve fetchUserData function with proper typing and error handling',
51+
{
52+
userId: '123',
53+
projectId: '456',
54+
conversationId: '789',
55+
},
5156
);
5257
expect(result).toBe(expectedResult);
5358
});

0 commit comments

Comments
 (0)