Skip to content

Commit

Permalink
meh
Browse files Browse the repository at this point in the history
  • Loading branch information
umpox committed Jul 28, 2023
1 parent 6100f9a commit 2dec946
Show file tree
Hide file tree
Showing 26 changed files with 146 additions and 556 deletions.
63 changes: 5 additions & 58 deletions lib/shared/src/chat/preamble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ const rules = `In your responses, obey the following rules:
- Answer questions only if you know the answer or can make a well-informed guess. Otherwise, tell me you don't know and what context I need to provide you for you to answer the question.
- Only reference file names, repository names or URLs if you are sure they exist.`

const multiRepoRules = `In your responses, obey the following rules:
- If you do not have access to code, files or repositories always stay in character as Cody when you apologize.
- Be as brief and concise as possible without losing clarity.
- All code snippets have to be markdown-formatted, and placed in-between triple backticks like this \`\`\`.
- Answer questions only if you know the answer or can make a well-informed guess. Otherwise, tell me you don't know and what context I need to provide you for you to answer the question.
- If you do not have access to a repository, tell me to add additional repositories to the chat context using repositories selector below the input box to help you answer the question.
- Only reference file names, repository names or URLs if you are sure they exist.`

const answer = `Understood. I am Cody, an AI assistant made by Sourcegraph to help with programming tasks.
I work inside a text editor. I have access to your currently open files in the editor.
I will answer questions, explain code, and generate code as concisely and clearly as possible.
Expand Down Expand Up @@ -68,68 +60,23 @@ export function getPreamble(codebase: string | undefined, customPreamble?: Pream
]
}

export function getMultiRepoPreamble(codebases: string[], customPreamble?: Preamble): Message[] {
const actionsText = customPreamble?.actions ?? actions
const rulesText = customPreamble?.rules ?? multiRepoRules
const answerText = customPreamble?.answer ?? answer
const preamble = [actionsText, rulesText]
const preambleResponse = [answerText]

if (codebases.length === 1) {
return getPreamble(codebases[0])
}

if (codebases.length) {
preamble.push(
`You have access to ${codebases.length} repositories:\n` +
codebases.map((name, index) => `${index + 1}. ${name}`).join('\n') +
'\n You are able to answer questions about all the above repositories. ' +
'I will provide the relevant code snippets from the files present in the above repositories when necessary to answer my questions. ' +
'If I ask you a question about a repository which is not listed above, please tell me to add additional repositories to the chat context using the repositories selector below the input box to help you answer the question.' +
'\n If the repository is listed above but you do not know the answer to the quesstion, tell me you do not know and what context I need to provide you for you to answer the question.'
)

preambleResponse.push(
'I have access to files present in the following repositories:\n' +
codebases.map((name, index) => `${index + 1}. ${name}`).join('\n') +
'\\n I can answer questions about code and files present in all the above repositories. ' +
'If you ask a question about a repository which I do not have access to, I will ask you to add additional repositories to the chat context using the repositories selector below the input box to help me answer the question. ' +
'If I have access to the repository but do not know the answer to the question, I will tell you I do not know and what context you need to provide me for me to answer the question.'
)
}

return [
{
speaker: 'human',
text: preamble.join('\n\n'),
},
{
speaker: 'assistant',
text: preambleResponse.join('\n'),
},
]
}

interface GeneratePreambleParameters {
actions: string
rules: string
answer: string
options: {
/** A callback to provide a codebase specific example to help guide Cody's context */
getCodebasePreamble: (codebase: string) => { preamble: string; answer: string }
}
getCodebasePreamble: (codebases: string[]) => { preamble: string; answer: string }
}

export function generatePreambleGetter({ actions, rules, answer, options }: GeneratePreambleParameters) {
return function (codebase: string | undefined, customPreamble?: Preamble): Message[] {
export function generatePreambleGetter({ actions, rules, answer, getCodebasePreamble }: GeneratePreambleParameters) {
return function (codebases?: string[], customPreamble?: Preamble): Message[] {
const actionsText = customPreamble?.actions ?? actions
const rulesText = customPreamble?.rules ?? rules
const answerText = customPreamble?.answer ?? answer
const preamble = [actionsText, rulesText]
const preambleResponse = [answerText]

if (codebase) {
const codebasePreamble = options.getCodebasePreamble(codebase)
if (codebases) {
const codebasePreamble = getCodebasePreamble(codebases)
preamble.push(codebasePreamble.preamble)
preambleResponse.push(codebasePreamble.answer)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/chat-question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Interaction } from '../transcript/interaction'
import { numResults } from './helpers'
import { Recipe, RecipeContext, RecipeID } from './recipe'

export class ChatQuestion implements Recipe {
export class ChatQuestion extends Recipe {
public id: RecipeID = 'chat-question'

constructor(private debug: (filterLabel: string, text: string, ...args: unknown[]) => void) {}
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/context-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Functionality:
- Sanitizes the content by removing newlines, tabs and backticks before displaying.
*/

export class ContextSearch implements Recipe {
export class ContextSearch extends Recipe {
public id: RecipeID = 'context-search'

public async getInteraction(humanChatInput: string, context: RecipeContext): Promise<Interaction | null> {
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/explain-code-detailed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Interaction } from '../transcript/interaction'
import { getContextMessagesFromSelection, getNormalizedLanguageName, MARKDOWN_FORMAT_PROMPT } from './helpers'
import { Recipe, RecipeContext, RecipeID } from './recipe'

export class ExplainCodeDetailed implements Recipe {
export class ExplainCodeDetailed extends Recipe {
public id: RecipeID = 'explain-code-detailed'

public async getInteraction(_humanChatInput: string, context: RecipeContext): Promise<Interaction | null> {
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/explain-code-high-level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Interaction } from '../transcript/interaction'
import { getContextMessagesFromSelection, getNormalizedLanguageName, MARKDOWN_FORMAT_PROMPT } from './helpers'
import { Recipe, RecipeContext, RecipeID } from './recipe'

export class ExplainCodeHighLevel implements Recipe {
export class ExplainCodeHighLevel extends Recipe {
public id: RecipeID = 'explain-code-high-level'

public async getInteraction(_humanChatInput: string, context: RecipeContext): Promise<Interaction | null> {
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/find-code-smells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Interaction } from '../transcript/interaction'
import { getNormalizedLanguageName } from './helpers'
import { Recipe, RecipeContext, RecipeID } from './recipe'

export class FindCodeSmells implements Recipe {
export class FindCodeSmells extends Recipe {
public id: RecipeID = 'find-code-smells'

public async getInteraction(_humanChatInput: string, context: RecipeContext): Promise<Interaction | null> {
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/fixup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Interaction } from '../transcript/interaction'
import { contentSanitizer, numResults } from './helpers'
import { Recipe, RecipeContext, RecipeID } from './recipe'

export class Fixup implements Recipe {
export class Fixup extends Recipe {
public id: RecipeID = 'fixup'

public async getInteraction(humanChatInput: string, context: RecipeContext): Promise<Interaction | null> {
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/generate-docstring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from './helpers'
import { Recipe, RecipeContext, RecipeID } from './recipe'

export class GenerateDocstring implements Recipe {
export class GenerateDocstring extends Recipe {
public id: RecipeID = 'generate-docstring'

public async getInteraction(_humanChatInput: string, context: RecipeContext): Promise<Interaction | null> {
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/generate-pr-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Interaction } from '../transcript/interaction'

import { Recipe, RecipeContext, RecipeID } from './recipe'

export class PrDescription implements Recipe {
export class PrDescription extends Recipe {
public id: RecipeID = 'pr-description'

public async getInteraction(_humanChatInput: string, context: RecipeContext): Promise<Interaction | null> {
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/generate-release-notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Interaction } from '../transcript/interaction'

import { Recipe, RecipeContext, RecipeID } from './recipe'

export class ReleaseNotes implements Recipe {
export class ReleaseNotes extends Recipe {
public id: RecipeID = 'release-notes'

public async getInteraction(_humanChatInput: string, context: RecipeContext): Promise<Interaction | null> {
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/generate-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from './helpers'
import { Recipe, RecipeContext, RecipeID } from './recipe'

export class GenerateTest implements Recipe {
export class GenerateTest extends Recipe {
public id: RecipeID = 'generate-unit-test'

public async getInteraction(_humanChatInput: string, context: RecipeContext): Promise<Interaction | null> {
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/git-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Interaction } from '../transcript/interaction'

import { Recipe, RecipeContext, RecipeID } from './recipe'

export class GitHistory implements Recipe {
export class GitHistory extends Recipe {
public id: RecipeID = 'git-history'

public async getInteraction(_humanChatInput: string, context: RecipeContext): Promise<Interaction | null> {
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/improve-variable-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from './helpers'
import { Recipe, RecipeContext, RecipeID } from './recipe'

export class ImproveVariableNames implements Recipe {
export class ImproveVariableNames extends Recipe {
public id: RecipeID = 'improve-variable-names'

public async getInteraction(_humanChatInput: string, context: RecipeContext): Promise<Interaction | null> {
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/inline-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { commandRegex } from './helpers'
import { InlineTouch } from './inline-touch'
import { Recipe, RecipeContext, RecipeID } from './recipe'

export class InlineChat implements Recipe {
export class InlineChat extends Recipe {
public id: RecipeID = 'inline-chat'

constructor(private debug: (filterLabel: string, text: string, ...args: unknown[]) => void) {}
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/inline-touch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Recipe, RecipeContext, RecipeID } from './recipe'
/** ======================================================
* Recipe for Generating a New File
====================================================== **/
export class InlineTouch implements Recipe {
export class InlineTouch extends Recipe {
public id: RecipeID = 'inline-touch'
private workspacePath = vscode.workspace.workspaceFolders?.[0].uri

Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/my-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const defaultCodyPromptContext: CodyPromptContext = {
* Recipe for running custom prompts from the cody.json files
* Works with VS Code only
====================================================== **/
export class MyPrompt implements Recipe {
export class MyPrompt extends Recipe {
public id: RecipeID = 'my-prompt'
private promptStore = new Map<string, string>()

Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/next-questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Interaction } from '../transcript/interaction'
import { numResults } from './helpers'
import { Recipe, RecipeContext, RecipeID } from './recipe'

export class NextQuestions implements Recipe {
export class NextQuestions extends Recipe {
public id: RecipeID = 'next-questions'

public async getInteraction(humanChatInput: string, context: RecipeContext): Promise<Interaction | null> {
Expand Down
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/non-stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { numResults } from './helpers'
import { Recipe, RecipeContext, RecipeID } from './recipe'

// TODO: Disconnect recipe from chat
export class NonStop implements Recipe {
export class NonStop extends Recipe {
public id: RecipeID = 'non-stop'

public async getInteraction(taskId: string, context: RecipeContext): Promise<Interaction | null> {
Expand Down
19 changes: 16 additions & 3 deletions lib/shared/src/chat/recipes/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@ import { CodebaseContext } from '../../codebase-context'
import { Editor } from '../../editor'
import { IntentDetector } from '../../intent-detector'
import { BotResponseMultiplexer } from '../bot-response-multiplexer'
import { generatePreambleGetter } from '../preamble'
import { Interaction } from '../transcript/interaction'

/**
* Recipes should produce slightly different outputs depending on their goal.
* It is important that this is distinguished so we can optimize our prompt.
*
* Examples:
* - `edit`: Cody is inserting code directly into the editor.
* - `explain`: Cody is producing a text output into a chat window.
*/
type RecipeContextGoal = 'explain' | 'edit'

/** Tools and context recipes can use at the point they are invoked. */
export interface RecipeContext {
goal: RecipeContextGoal
editor: Editor
intentDetector: IntentDetector
codebaseContext: CodebaseContext
Expand Down Expand Up @@ -33,7 +45,8 @@ export type RecipeID =
| 'release-notes'
| 'translate-to-language'

export interface Recipe {
id: RecipeID
getInteraction(humanChatInput: string, context: RecipeContext): Promise<Interaction | null>
export abstract class Recipe {
constructor(public getPreamble: ReturnType<typeof generatePreambleGetter>) {}
public abstract id: RecipeID
public abstract getInteraction(humanChatInput: string, context: RecipeContext): Promise<Interaction | null>
}
2 changes: 1 addition & 1 deletion lib/shared/src/chat/recipes/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Interaction } from '../transcript/interaction'
import { languageMarkdownID, languageNames } from './langs'
import { Recipe, RecipeContext, RecipeID } from './recipe'

export class TranslateToLanguage implements Recipe {
export class TranslateToLanguage extends Recipe {
public id: RecipeID = 'translate-to-language'

public static options = languageNames
Expand Down
Loading

0 comments on commit 2dec946

Please sign in to comment.