Skip to content

Commit dbd8ef7

Browse files
fix: Fix issue with ambiguous references (#393)
1 parent da8d49f commit dbd8ef7

File tree

10 files changed

+191
-136
lines changed

10 files changed

+191
-136
lines changed

packages/web/src/features/chat/agent.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,19 +200,19 @@ When you have sufficient context, output your answer as a structured markdown re
200200
**Required Response Format:**
201201
- **CRITICAL**: You MUST always prefix your answer with a \`${ANSWER_TAG}\` tag at the very top of your response
202202
- **CRITICAL**: You MUST provide your complete response in markdown format with embedded code references
203-
- **CODE REFERENCE REQUIREMENT**: Whenever you mention, discuss, or refer to ANY specific part of the code (files, functions, variables, methods, classes, imports, etc.), you MUST immediately follow with a code reference using the format \`${fileReferenceToString({ fileName: 'filename'})}\` or \`${fileReferenceToString({ fileName: 'filename', range: { startLine: 1, endLine: 10 } })}\` (where the numbers are the start and end line numbers of the code snippet). This includes:
204-
- Files (e.g., "The \`auth.ts\` file" → must include \`${fileReferenceToString({ fileName: 'auth.ts' })}\`)
205-
- Function names (e.g., "The \`getRepos()\` function" → must include \`${fileReferenceToString({ fileName: 'auth.ts', range: { startLine: 15, endLine: 20 } })}\`)
206-
- Variable names (e.g., "The \`suggestionQuery\` variable" → must include \`${fileReferenceToString({ fileName: 'search.ts', range: { startLine: 42, endLine: 42 } })}\`)
207-
- Code patterns (e.g., "using \`file:\${suggestionQuery}\` pattern" → must include \`${fileReferenceToString({ fileName: 'search.ts', range: { startLine: 10, endLine: 15 } })}\`)
203+
- **CODE REFERENCE REQUIREMENT**: Whenever you mention, discuss, or refer to ANY specific part of the code (files, functions, variables, methods, classes, imports, etc.), you MUST immediately follow with a code reference using the format \`${fileReferenceToString({ repo: 'repository', path: 'filename'})}\` or \`${fileReferenceToString({ repo: 'repository', path: 'filename', range: { startLine: 1, endLine: 10 } })}\` (where the numbers are the start and end line numbers of the code snippet). This includes:
204+
- Files (e.g., "The \`auth.ts\` file" → must include \`${fileReferenceToString({ repo: 'repository', path: 'auth.ts' })}\`)
205+
- Function names (e.g., "The \`getRepos()\` function" → must include \`${fileReferenceToString({ repo: 'repository', path: 'auth.ts', range: { startLine: 15, endLine: 20 } })}\`)
206+
- Variable names (e.g., "The \`suggestionQuery\` variable" → must include \`${fileReferenceToString({ repo: 'repository', path: 'search.ts', range: { startLine: 42, endLine: 42 } })}\`)
208207
- Any code snippet or line you're explaining
209208
- Class names, method calls, imports, etc.
210209
- Some examples of both correct and incorrect code references:
211-
- Correct: @file:{path/to/file.ts}
212-
- Correct: @file:{path/to/file.ts:10-15}
213-
- Incorrect: @file{path/to/file.ts} (missing colon)
214-
- Incorrect: @file:path/to/file.ts (missing curly braces)
215-
- Incorrect: @file:{path/to/file.ts:10-25,30-35} (multiple ranges not supported)
210+
- Correct: @file:{repository::path/to/file.ts}
211+
- Correct: @file:{repository::path/to/file.ts:10-15}
212+
- Incorrect: @file{repository::path/to/file.ts} (missing colon)
213+
- Incorrect: @file:repository::path/to/file.ts (missing curly braces)
214+
- Incorrect: @file:{repository::path/to/file.ts:10-25,30-35} (multiple ranges not supported)
215+
- Incorrect: @file:{path/to/file.ts} (missing repository)
216216
- Be clear and very concise. Use bullet points where appropriate
217217
- Do NOT explain code without providing the exact location reference. Every code mention requires a corresponding \`${FILE_REFERENCE_PREFIX}\` reference
218218
- If you cannot provide a code reference for something you're discussing, do not mention that specific code element
@@ -221,7 +221,7 @@ When you have sufficient context, output your answer as a structured markdown re
221221
**Example answer structure:**
222222
\`\`\`markdown
223223
${ANSWER_TAG}
224-
Authentication in Sourcebot is built on NextAuth.js with a session-based approach using JWT tokens and Prisma as the database adapter ${fileReferenceToString({ fileName: 'auth.ts', range: { startLine: 135, endLine: 140 } })}. The system supports multiple authentication providers and implements organization-based authorization with role-defined permissions.
224+
Authentication in Sourcebot is built on NextAuth.js with a session-based approach using JWT tokens and Prisma as the database adapter ${fileReferenceToString({ repo: 'github.com/sourcebot-dev/sourcebot', path: 'auth.ts', range: { startLine: 135, endLine: 140 } })}. The system supports multiple authentication providers and implements organization-based authorization with role-defined permissions.
225225
\`\`\`
226226
227227
</answer_instructions>

packages/web/src/features/chat/components/chatThread/codeFoldingExtension.test.ts

Lines changed: 84 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ describe('calculateVisibleRanges', () => {
1717
test('applies padding to a single range', () => {
1818
const references: FileReference[] = [
1919
{
20-
fileName: 'test.ts',
20+
path: 'test.ts',
2121
id: '1',
2222
type: 'file',
2323
range: {
2424
startLine: 10,
2525
endLine: 15
26-
}
26+
},
27+
repo: 'github.com/sourcebot-dev/sourcebot'
2728
}
2829
];
2930

@@ -38,16 +39,18 @@ describe('calculateVisibleRanges', () => {
3839
test('merges overlapping ranges', () => {
3940
const references: FileReference[] = [
4041
{
41-
fileName: 'test.ts',
42+
path: 'test.ts',
4243
id: '1',
4344
type: 'file',
44-
range: { startLine: 10, endLine: 15 }
45+
range: { startLine: 10, endLine: 15 },
46+
repo: 'github.com/sourcebot-dev/sourcebot'
4547
},
4648
{
47-
fileName: 'test.ts',
49+
path: 'test.ts',
4850
id: '2',
4951
type: 'file',
50-
range: { startLine: 12, endLine: 20 }
52+
range: { startLine: 12, endLine: 20 },
53+
repo: 'github.com/sourcebot-dev/sourcebot'
5154
}
5255
];
5356

@@ -62,16 +65,18 @@ describe('calculateVisibleRanges', () => {
6265
test('merges adjacent ranges (including padding)', () => {
6366
const references: FileReference[] = [
6467
{
65-
fileName: 'test.ts',
68+
path: 'test.ts',
6669
id: '1',
6770
type: 'file',
68-
range: { startLine: 10, endLine: 15 }
71+
range: { startLine: 10, endLine: 15 },
72+
repo: 'github.com/sourcebot-dev/sourcebot'
6973
},
7074
{
71-
fileName: 'test.ts',
75+
path: 'test.ts',
7276
id: '2',
7377
type: 'file',
74-
range: { startLine: 19, endLine: 25 }
78+
range: { startLine: 19, endLine: 25 },
79+
repo: 'github.com/sourcebot-dev/sourcebot'
7580
}
7681
];
7782

@@ -88,16 +93,18 @@ describe('calculateVisibleRanges', () => {
8893
test('keeps separate ranges when they dont overlap', () => {
8994
const references: FileReference[] = [
9095
{
91-
fileName: 'test.ts',
96+
path: 'test.ts',
9297
id: '1',
9398
type: 'file',
94-
range: { startLine: 10, endLine: 15 }
99+
range: { startLine: 10, endLine: 15 },
100+
repo: 'github.com/sourcebot-dev/sourcebot'
95101
},
96102
{
97-
fileName: 'test.ts',
103+
path: 'test.ts',
98104
id: '2',
99105
type: 'file',
100-
range: { startLine: 25, endLine: 30 }
106+
range: { startLine: 25, endLine: 30 },
107+
repo: 'github.com/sourcebot-dev/sourcebot'
101108
}
102109
];
103110

@@ -112,10 +119,11 @@ describe('calculateVisibleRanges', () => {
112119
test('respects file boundaries - start of file', () => {
113120
const references: FileReference[] = [
114121
{
115-
fileName: 'test.ts',
122+
path: 'test.ts',
116123
id: '1',
117124
type: 'file',
118-
range: { startLine: 1, endLine: 5 }
125+
range: { startLine: 1, endLine: 5 },
126+
repo: 'github.com/sourcebot-dev/sourcebot'
119127
}
120128
];
121129

@@ -130,10 +138,11 @@ describe('calculateVisibleRanges', () => {
130138
test('respects file boundaries - end of file', () => {
131139
const references: FileReference[] = [
132140
{
133-
fileName: 'test.ts',
141+
path: 'test.ts',
134142
id: '1',
135143
type: 'file',
136-
range: { startLine: 95, endLine: 100 }
144+
range: { startLine: 95, endLine: 100 },
145+
repo: 'github.com/sourcebot-dev/sourcebot'
137146
}
138147
];
139148

@@ -148,28 +157,32 @@ describe('calculateVisibleRanges', () => {
148157
test('handles multiple ranges with complex overlaps', () => {
149158
const references: FileReference[] = [
150159
{
151-
fileName: 'test.ts',
160+
path: 'test.ts',
152161
id: '1',
153162
type: 'file',
154-
range: { startLine: 10, endLine: 15 }
163+
range: { startLine: 10, endLine: 15 },
164+
repo: 'github.com/sourcebot-dev/sourcebot'
155165
},
156166
{
157-
fileName: 'test.ts',
167+
path: 'test.ts',
158168
id: '2',
159169
type: 'file',
160-
range: { startLine: 20, endLine: 25 }
170+
range: { startLine: 20, endLine: 25 },
171+
repo: 'github.com/sourcebot-dev/sourcebot'
161172
},
162173
{
163-
fileName: 'test.ts',
174+
path: 'test.ts',
164175
id: '3',
165176
type: 'file',
166-
range: { startLine: 22, endLine: 30 }
177+
range: { startLine: 22, endLine: 30 },
178+
repo: 'github.com/sourcebot-dev/sourcebot'
167179
},
168180
{
169-
fileName: 'test.ts',
181+
path: 'test.ts',
170182
id: '4',
171183
type: 'file',
172-
range: { startLine: 50, endLine: 55 }
184+
range: { startLine: 50, endLine: 55 },
185+
repo: 'github.com/sourcebot-dev/sourcebot'
173186
}
174187
];
175188

@@ -195,16 +208,18 @@ describe('calculateVisibleRanges', () => {
195208
test('ignores references without ranges', () => {
196209
const references: FileReference[] = [
197210
{
198-
fileName: 'test.ts',
211+
path: 'test.ts',
199212
id: '1',
200213
type: 'file',
201214
// No range property
215+
repo: 'github.com/sourcebot-dev/sourcebot'
202216
},
203217
{
204-
fileName: 'test.ts',
218+
path: 'test.ts',
205219
id: '2',
206220
type: 'file',
207-
range: { startLine: 10, endLine: 15 }
221+
range: { startLine: 10, endLine: 15 },
222+
repo: 'github.com/sourcebot-dev/sourcebot'
208223
}
209224
];
210225

@@ -219,10 +234,11 @@ describe('calculateVisibleRanges', () => {
219234
test('works with zero padding', () => {
220235
const references: FileReference[] = [
221236
{
222-
fileName: 'test.ts',
237+
path: 'test.ts',
223238
id: '1',
224239
type: 'file',
225-
range: { startLine: 10, endLine: 15 }
240+
range: { startLine: 10, endLine: 15 },
241+
repo: 'github.com/sourcebot-dev/sourcebot'
226242
}
227243
];
228244

@@ -237,10 +253,11 @@ describe('calculateVisibleRanges', () => {
237253
test('handles single line ranges', () => {
238254
const references: FileReference[] = [
239255
{
240-
fileName: 'test.ts',
256+
path: 'test.ts',
241257
id: '1',
242258
type: 'file',
243-
range: { startLine: 10, endLine: 10 }
259+
range: { startLine: 10, endLine: 10 },
260+
repo: 'github.com/sourcebot-dev/sourcebot'
244261
}
245262
];
246263

@@ -255,22 +272,25 @@ describe('calculateVisibleRanges', () => {
255272
test('sorts ranges by start line', () => {
256273
const references: FileReference[] = [
257274
{
258-
fileName: 'test.ts',
275+
path: 'test.ts',
259276
id: '1',
260277
type: 'file',
261-
range: { startLine: 50, endLine: 55 }
278+
range: { startLine: 50, endLine: 55 },
279+
repo: 'github.com/sourcebot-dev/sourcebot'
262280
},
263281
{
264-
fileName: 'test.ts',
282+
path: 'test.ts',
265283
id: '2',
266284
type: 'file',
267-
range: { startLine: 10, endLine: 15 }
285+
range: { startLine: 10, endLine: 15 },
286+
repo: 'github.com/sourcebot-dev/sourcebot'
268287
},
269288
{
270-
fileName: 'test.ts',
289+
path: 'test.ts',
271290
id: '3',
272291
type: 'file',
273-
range: { startLine: 30, endLine: 35 }
292+
range: { startLine: 30, endLine: 35 },
293+
repo: 'github.com/sourcebot-dev/sourcebot'
274294
}
275295
];
276296

@@ -380,16 +400,18 @@ describe('StateField Integration', () => {
380400
test('initial state calculation with references', () => {
381401
const references: FileReference[] = [
382402
{
383-
fileName: 'test.ts',
403+
path: 'test.ts',
384404
id: '1',
385405
type: 'file',
386-
range: { startLine: 10, endLine: 15 }
406+
range: { startLine: 10, endLine: 15 },
407+
repo: 'github.com/sourcebot-dev/sourcebot'
387408
},
388409
{
389-
fileName: 'test.ts',
410+
path: 'test.ts',
390411
id: '2',
391412
type: 'file',
392-
range: { startLine: 25, endLine: 30 }
413+
range: { startLine: 25, endLine: 30 },
414+
repo: 'github.com/sourcebot-dev/sourcebot'
393415
}
394416
];
395417

@@ -457,7 +479,7 @@ describe('StateField Integration', () => {
457479
// Update references
458480
const newReferences: FileReference[] = [
459481
{
460-
fileName: 'test.ts',
482+
path: 'test.ts',
461483
id: '1',
462484
type: 'file',
463485
range: { startLine: 10, endLine: 15 }
@@ -482,10 +504,11 @@ describe('StateField Integration', () => {
482504
test('expandRegionEffect expands hidden region up', () => {
483505
const references: FileReference[] = [
484506
{
485-
fileName: 'test.ts',
507+
path: 'test.ts',
486508
id: '1',
487509
type: 'file',
488-
range: { startLine: 20, endLine: 25 }
510+
range: { startLine: 20, endLine: 25 },
511+
repo: 'github.com/sourcebot-dev/sourcebot'
489512
}
490513
];
491514

@@ -528,10 +551,11 @@ describe('StateField Integration', () => {
528551
test('expandRegionEffect expands hidden region down', () => {
529552
const references: FileReference[] = [
530553
{
531-
fileName: 'test.ts',
554+
path: 'test.ts',
532555
id: '1',
533556
type: 'file',
534-
range: { startLine: 20, endLine: 25 }
557+
range: { startLine: 20, endLine: 25 },
558+
repo: 'github.com/sourcebot-dev/sourcebot'
535559
}
536560
];
537561

@@ -564,10 +588,11 @@ describe('StateField Integration', () => {
564588
test('document changes recalculate state', () => {
565589
const references: FileReference[] = [
566590
{
567-
fileName: 'test.ts',
591+
path: 'test.ts',
568592
id: '1',
569593
type: 'file',
570-
range: { startLine: 10, endLine: 15 }
594+
range: { startLine: 10, endLine: 15 },
595+
repo: 'github.com/sourcebot-dev/sourcebot'
571596
}
572597
];
573598

@@ -606,10 +631,11 @@ describe('StateField Integration', () => {
606631
test('action creators work correctly', () => {
607632
const references: FileReference[] = [
608633
{
609-
fileName: 'test.ts',
634+
path: 'test.ts',
610635
id: '1',
611636
type: 'file',
612-
range: { startLine: 10, endLine: 15 }
637+
range: { startLine: 10, endLine: 15 },
638+
repo: 'github.com/sourcebot-dev/sourcebot'
613639
}
614640
];
615641

@@ -647,16 +673,18 @@ describe('StateField Integration', () => {
647673
// Add references
648674
const references: FileReference[] = [
649675
{
650-
fileName: 'test.ts',
676+
path: 'test.ts',
651677
id: '1',
652678
type: 'file',
653-
range: { startLine: 20, endLine: 25 }
679+
range: { startLine: 20, endLine: 25 },
680+
repo: 'github.com/sourcebot-dev/sourcebot'
654681
},
655682
{
656-
fileName: 'test.ts',
683+
path: 'test.ts',
657684
id: '2',
658685
type: 'file',
659-
range: { startLine: 60, endLine: 65 }
686+
range: { startLine: 60, endLine: 65 },
687+
repo: 'github.com/sourcebot-dev/sourcebot'
660688
}
661689
];
662690

0 commit comments

Comments
 (0)