Skip to content

Commit 604a4bf

Browse files
authored
telemetry: add request_id field for code block actions (#1586)
Context: https://sourcegraph.slack.com/archives/C05AGQYD528/p1698355247771849?thread_ts=1697738493.299929&cid=C05AGQYD528 From @chenkc805 > For a "chatID", I'd like to be able to link specific chain of events together: chat/command executed --> chat/command response --> code copied --> code pasted This PR adds request_id to each Interaction so that when we display the code block in the UI, we will be able to refer back to the request when the code was generated: - add requestID parameter to copy and insert callback functions ## Test plan <!-- Required. See https://docs.sourcegraph.com/dev/background-information/testing_principles. --> Ask Cody a question that generates a code block, and then copy the code. they should have the same request id as the request that generated the code ``` █ logEvent (telemetry disabled): CodyVSCodeExtension:recipe:chat-question:executed {"properties":{"embeddings":15,"local":1,"request_id":"2c951818-f4fe-4105-9113-0c055d8b264c"},"opts":{"hasV2Event":true}} █ logEvent (telemetry disabled): CodyVSCodeExtension:chatResponse:hasCode {"properties":{"lineCount":1,"charCount":25,"source":"chat-question","request_id":"2c951818-f4fe-4105-9113-0c055d8b264c"},"opts":{"hasV2Event":true}} █ logEvent (telemetry disabled): CodyVSCodeExtension:insertButton:clicked {"properties":{"op":"insert","charCount":25,"lineCount":1,"source":"chat-question","request_id":"2c951818-f4fe-4105-9113-0c055d8b264c"}} █ logEvent (telemetry disabled): CodyVSCodeExtension:copyButton:clicked {"properties":{"op":"copy","charCount":25,"lineCount":1,"source":"chat-question","request_id":"2c951818-f4fe-4105-9113-0c055d8b264c"}} █ logEvent (telemetry disabled): CodyVSCodeExtension:keyDown:Paste:clicked {"properties":{"op":"paste","lineCount":1,"charCount":25,"source":"chat-question","request_id":"2c951818-f4fe-4105-9113-0c055d8b264c"}} ```
1 parent 47f8d61 commit 604a4bf

33 files changed

+331
-301
lines changed

lib/shared/src/chat/prompts/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export interface MyPromptsJSON {
5757

5858
// The blueprint of a Cody Command
5959
export interface CodyPrompt {
60+
requestID?: string
6061
description?: string
6162
prompt: string
6263
context?: CodyPromptContext

lib/shared/src/chat/prompts/utils.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,35 @@ import { prompts } from './templates'
1616
*/
1717
export async function newInteraction(args: {
1818
text?: string
19-
displayText: string
19+
displayText?: string
2020
contextMessages?: Promise<ContextMessage[]>
2121
assistantText?: string
2222
assistantDisplayText?: string
23+
assistantPrefix?: string
2324
source?: ChatEventSource
25+
requestID?: string
2426
}): Promise<Interaction> {
25-
const { text, displayText, contextMessages, assistantText, assistantDisplayText, source } = args
27+
const {
28+
text,
29+
displayText,
30+
contextMessages,
31+
assistantText,
32+
assistantDisplayText,
33+
assistantPrefix,
34+
source,
35+
requestID,
36+
} = args
37+
const metadata = { source, requestID }
2638
return Promise.resolve(
2739
new Interaction(
28-
{ speaker: 'human', text, displayText, source },
29-
{ speaker: 'assistant', text: assistantText, displayText: assistantDisplayText, source },
40+
{ speaker: 'human', text, displayText, metadata },
41+
{
42+
speaker: 'assistant',
43+
text: assistantText,
44+
displayText: assistantDisplayText,
45+
prefix: assistantPrefix,
46+
metadata,
47+
},
3048
Promise.resolve(contextMessages || []),
3149
[]
3250
)

lib/shared/src/chat/recipes/chat-question.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export class ChatQuestion implements Recipe {
2525

2626
return Promise.resolve(
2727
new Interaction(
28-
{ speaker: 'human', text: truncatedText, displayText: humanChatInput, source },
29-
{ speaker: 'assistant', source },
28+
{ speaker: 'human', text: truncatedText, displayText: humanChatInput, metadata: { source } },
29+
{ speaker: 'assistant', metadata: { source } },
3030
this.getContextMessages(
3131
truncatedText,
3232
context.editor,

lib/shared/src/chat/recipes/code-question.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ export class CodeQuestion implements Recipe {
2525

2626
return Promise.resolve(
2727
new Interaction(
28-
{ speaker: 'human', text: truncatedText, displayText: humanChatInput, source },
28+
{ speaker: 'human', text: truncatedText, displayText: humanChatInput, metadata: { source } },
2929
{
3030
speaker: 'assistant',
3131
text: `\`\`\`${getFileExtension(context.editor.getActiveTextEditorSelection()?.fileName ?? '')}\n`,
32-
source,
32+
metadata: { source },
3333
},
3434
this.getContextMessages(
3535
truncatedText,

lib/shared/src/chat/recipes/context-search.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ export class ContextSearch implements Recipe {
4545
speaker: 'human',
4646
text: '',
4747
displayText: query,
48-
source,
48+
metadata: { source },
4949
},
5050
{
5151
speaker: 'assistant',
5252
text: '',
5353
displayText: await this.displaySearchResults(truncatedText, context.codebaseContext, workspaceRootUri),
54-
source,
54+
metadata: { source },
5555
},
5656
new Promise(resolve => resolve([])),
5757
[]

lib/shared/src/chat/recipes/explain-code-detailed.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MAX_RECIPE_INPUT_TOKENS, MAX_RECIPE_SURROUNDING_TOKENS } from '../../prompt/constants'
22
import { truncateText, truncateTextStart } from '../../prompt/truncation'
3+
import { newInteraction } from '../prompts/utils'
34
import { Interaction } from '../transcript/interaction'
45

56
import { getContextMessagesFromSelection, getNormalizedLanguageName, MARKDOWN_FORMAT_PROMPT } from './helpers'
@@ -25,17 +26,17 @@ export class ExplainCodeDetailed implements Recipe {
2526
const promptMessage = `Please explain the following ${languageName} code. Be very detailed and specific, and indicate when it is not clear to you what is going on. Format your response as an ordered list.\n\`\`\`\n${truncatedSelectedText}\n\`\`\`\n${MARKDOWN_FORMAT_PROMPT}`
2627
const displayText = `Explain the following code:\n\`\`\`\n${selection.selectedText}\n\`\`\``
2728

28-
return new Interaction(
29-
{ speaker: 'human', text: promptMessage, displayText, source },
30-
{ speaker: 'assistant', source },
31-
getContextMessagesFromSelection(
29+
return newInteraction({
30+
text: promptMessage,
31+
displayText,
32+
source,
33+
contextMessages: getContextMessagesFromSelection(
3234
truncatedSelectedText,
3335
truncatedPrecedingText,
3436
truncatedFollowingText,
3537
selection,
3638
context.codebaseContext
3739
),
38-
[]
39-
)
40+
})
4041
}
4142
}

lib/shared/src/chat/recipes/explain-code-high-level.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MAX_RECIPE_INPUT_TOKENS, MAX_RECIPE_SURROUNDING_TOKENS } from '../../prompt/constants'
22
import { truncateText, truncateTextStart } from '../../prompt/truncation'
3+
import { newInteraction } from '../prompts/utils'
34
import { Interaction } from '../transcript/interaction'
45

56
import { getContextMessagesFromSelection, getNormalizedLanguageName, MARKDOWN_FORMAT_PROMPT } from './helpers'
@@ -25,17 +26,17 @@ export class ExplainCodeHighLevel implements Recipe {
2526
const promptMessage = `Explain the following ${languageName} code at a high level. Only include details that are essential to an overall understanding of what's happening in the code.\n\`\`\`\n${truncatedSelectedText}\n\`\`\`\n${MARKDOWN_FORMAT_PROMPT}`
2627
const displayText = `Explain the following code at a high level:\n\`\`\`\n${selection.selectedText}\n\`\`\``
2728

28-
return new Interaction(
29-
{ speaker: 'human', text: promptMessage, displayText, source },
30-
{ speaker: 'assistant', source },
31-
getContextMessagesFromSelection(
29+
return newInteraction({
30+
text: promptMessage,
31+
displayText,
32+
source,
33+
contextMessages: getContextMessagesFromSelection(
3234
truncatedSelectedText,
3335
truncatedPrecedingText,
3436
truncatedFollowingText,
3537
selection,
3638
context.codebaseContext
3739
),
38-
[]
39-
)
40+
})
4041
}
4142
}

lib/shared/src/chat/recipes/find-code-smells.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CHARS_PER_TOKEN, MAX_AVAILABLE_PROMPT_LENGTH, MAX_RECIPE_INPUT_TOKENS } from '../../prompt/constants'
22
import { truncateText } from '../../prompt/truncation'
3+
import { newInteraction } from '../prompts/utils'
34
import { Interaction } from '../transcript/interaction'
45

56
import { getNormalizedLanguageName } from './helpers'
@@ -34,16 +35,12 @@ If you have no ideas because the code looks fine, feel free to say that it alrea
3435
const displayText = `Find code smells in the following code: \n\`\`\`\n${selection.selectedText}\n\`\`\``
3536

3637
const assistantResponsePrefix = ''
37-
return new Interaction(
38-
{ speaker: 'human', text: promptMessage, displayText, source },
39-
{
40-
speaker: 'assistant',
41-
prefix: assistantResponsePrefix,
42-
text: assistantResponsePrefix,
43-
source,
44-
},
45-
new Promise(resolve => resolve([])),
46-
[]
47-
)
38+
return newInteraction({
39+
text: promptMessage,
40+
displayText,
41+
source,
42+
assistantPrefix: assistantResponsePrefix,
43+
assistantText: assistantResponsePrefix,
44+
})
4845
}
4946
}

lib/shared/src/chat/recipes/fixup.ts

+6-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { VsCodeFixupTaskRecipeData } from '../../editor'
33
import { MAX_CURRENT_FILE_TOKENS, MAX_HUMAN_INPUT_TOKENS } from '../../prompt/constants'
44
import { populateCodeContextTemplate, populateCurrentEditorDiagnosticsTemplate } from '../../prompt/templates'
55
import { truncateText, truncateTextStart } from '../../prompt/truncation'
6+
import { newInteraction } from '../prompts/utils'
67
import { Interaction } from '../transcript/interaction'
78

89
import { getContextMessagesFromSelection } from './helpers'
@@ -39,19 +40,11 @@ export class Fixup implements Recipe {
3940
const promptText = this.getPrompt(fixupTask, intent)
4041
const quarterFileContext = Math.floor(MAX_CURRENT_FILE_TOKENS / 4)
4142

42-
return Promise.resolve(
43-
new Interaction(
44-
{
45-
speaker: 'human',
46-
text: promptText,
47-
},
48-
{
49-
speaker: 'assistant',
50-
},
51-
this.getContextFromIntent(intent, fixupTask, quarterFileContext, context),
52-
[]
53-
)
54-
)
43+
return newInteraction({
44+
text: promptText,
45+
source: this.id,
46+
contextMessages: this.getContextFromIntent(intent, fixupTask, quarterFileContext, context),
47+
})
5548
}
5649

5750
public getPrompt(task: VsCodeFixupTaskRecipeData, intent: FixupIntent): string {

lib/shared/src/chat/recipes/generate-docstring.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MAX_RECIPE_INPUT_TOKENS, MAX_RECIPE_SURROUNDING_TOKENS } from '../../prompt/constants'
22
import { truncateText, truncateTextStart } from '../../prompt/truncation'
3+
import { newInteraction } from '../prompts/utils'
34
import { Interaction } from '../transcript/interaction'
45

56
import {
@@ -49,22 +50,19 @@ export class GenerateDocstring implements Recipe {
4950
const displayText = `Generate documentation for the following code:\n\`\`\`\n${selection.selectedText}\n\`\`\``
5051

5152
const assistantResponsePrefix = `Here is the generated documentation:\n\`\`\`${extension}\n${docStart}`
52-
return new Interaction(
53-
{ speaker: 'human', text: promptMessage, displayText, source },
54-
{
55-
speaker: 'assistant',
56-
prefix: assistantResponsePrefix,
57-
text: assistantResponsePrefix,
58-
source,
59-
},
60-
getContextMessagesFromSelection(
53+
return newInteraction({
54+
text: promptMessage,
55+
displayText,
56+
source,
57+
assistantPrefix: assistantResponsePrefix,
58+
assistantText: assistantResponsePrefix,
59+
contextMessages: getContextMessagesFromSelection(
6160
truncatedSelectedText,
6261
truncatedPrecedingText,
6362
truncatedFollowingText,
6463
selection,
6564
context.codebaseContext
6665
),
67-
[]
68-
)
66+
})
6967
}
7068
}

lib/shared/src/chat/recipes/generate-pr-description.ts

+15-21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from 'path'
44

55
import { MAX_RECIPE_INPUT_TOKENS } from '../../prompt/constants'
66
import { truncateText } from '../../prompt/truncation'
7+
import { newInteraction } from '../prompts/utils'
78
import { Interaction } from '../transcript/interaction'
89

910
import { Recipe, RecipeContext, RecipeID } from './recipe'
@@ -52,17 +53,13 @@ export class PrDescription implements Recipe {
5253

5354
if (!gitCommitOutput) {
5455
const emptyGitCommitMessage = 'No commits history found in the current branch.'
55-
return new Interaction(
56-
{ speaker: 'human', displayText: rawDisplayText, source },
57-
{
58-
speaker: 'assistant',
59-
prefix: emptyGitCommitMessage,
60-
text: emptyGitCommitMessage,
61-
source,
62-
},
63-
Promise.resolve([]),
64-
[]
65-
)
56+
return newInteraction({
57+
text: rawDisplayText,
58+
displayText: rawDisplayText,
59+
source,
60+
assistantPrefix: emptyGitCommitMessage,
61+
assistantText: emptyGitCommitMessage,
62+
})
6663
}
6764

6865
const truncatedGitCommitOutput = truncateText(gitCommitOutput, MAX_RECIPE_INPUT_TOKENS)
@@ -73,15 +70,12 @@ export class PrDescription implements Recipe {
7370

7471
const promptMessage = `Summarise these changes:\n${gitCommitOutput}\n\n made while working in the current git branch.\nUse this pull request template to ${prTemplateContent} generate a pull request description based on the committed changes.\nIf the PR template mentions a requirement to check the contribution guidelines, then just summarise the changes in bulletin format.\n If it mentions a test plan for the changes use N/A\n.`
7572
const assistantResponsePrefix = `Here is the PR description for the work done in your current branch:\n${truncatedCommitMessage}`
76-
return new Interaction(
77-
{ speaker: 'human', text: promptMessage, displayText: rawDisplayText },
78-
{
79-
speaker: 'assistant',
80-
prefix: assistantResponsePrefix,
81-
text: assistantResponsePrefix,
82-
},
83-
Promise.resolve([]),
84-
[]
85-
)
73+
return newInteraction({
74+
text: promptMessage,
75+
displayText: rawDisplayText,
76+
source,
77+
assistantPrefix: assistantResponsePrefix,
78+
assistantText: assistantResponsePrefix,
79+
})
8680
}
8781
}

lib/shared/src/chat/recipes/generate-release-notes.ts

+15-21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { spawnSync } from 'child_process'
22

33
import { MAX_RECIPE_INPUT_TOKENS } from '../../prompt/constants'
44
import { truncateText } from '../../prompt/truncation'
5+
import { newInteraction } from '../prompts/utils'
56
import { Interaction } from '../transcript/interaction'
67

78
import { Recipe, RecipeContext, RecipeID } from './recipe'
@@ -66,17 +67,13 @@ export class ReleaseNotes implements Recipe {
6667

6768
if (!gitLogOutput) {
6869
const emptyGitLogMessage = 'No recent changes found to generate release notes.'
69-
return new Interaction(
70-
{ speaker: 'human', displayText: rawDisplayText, source },
71-
{
72-
speaker: 'assistant',
73-
prefix: emptyGitLogMessage,
74-
text: emptyGitLogMessage,
75-
source,
76-
},
77-
Promise.resolve([]),
78-
[]
79-
)
70+
return newInteraction({
71+
text: rawDisplayText,
72+
displayText: rawDisplayText,
73+
source,
74+
assistantPrefix: emptyGitLogMessage,
75+
assistantText: emptyGitLogMessage,
76+
})
8077
}
8178

8279
const truncatedGitLogOutput = truncateText(gitLogOutput, MAX_RECIPE_INPUT_TOKENS)
@@ -88,15 +85,12 @@ export class ReleaseNotes implements Recipe {
8885

8986
const promptMessage = `Generate release notes by summarising these commits:\n${truncatedGitLogOutput}\n\nUse proper heading format for the release notes.\n\n${tagsPromptText}.Do not include other changes and dependency updates.`
9087
const assistantResponsePrefix = `Here is the generated release notes for ${selectedLabel}\n${truncatedLogMessage}`
91-
return new Interaction(
92-
{ speaker: 'human', text: promptMessage, displayText: rawDisplayText },
93-
{
94-
speaker: 'assistant',
95-
prefix: assistantResponsePrefix,
96-
text: assistantResponsePrefix,
97-
},
98-
Promise.resolve([]),
99-
[]
100-
)
88+
return newInteraction({
89+
text: promptMessage,
90+
displayText: rawDisplayText,
91+
source,
92+
assistantPrefix: assistantResponsePrefix,
93+
assistantText: assistantResponsePrefix,
94+
})
10195
}
10296
}

0 commit comments

Comments
 (0)