Skip to content

Commit

Permalink
Merge pull request #108 from restlessronin/send-files-as-context
Browse files Browse the repository at this point in the history
Add all open workspace files as context for request
  • Loading branch information
lgrammel authored Nov 28, 2023
2 parents 3629c7c + 22e50cd commit 34dade2
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/vscode/asset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
},
"rubberduck.model": {
"type": "string",
"default": "gpt-3.5-turbo",
"default": "gpt-3.5-turbo-16k",
"enum": [
"gpt-3.5-turbo",
"gpt-3.5-turbo-16k",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class ConversationTypesProvider {
await this.loadBuiltinTemplate("task", "document-code.rdt.md"),
await this.loadBuiltinTemplate("task", "edit-code.rdt.md"),
await this.loadBuiltinTemplate("task", "explain-code.rdt.md"),
await this.loadBuiltinTemplate("task", "explain-code-w-context.rdt.md"),
await this.loadBuiltinTemplate("task", "find-bugs.rdt.md"),
await this.loadBuiltinTemplate("task", "generate-code.rdt.md"),
await this.loadBuiltinTemplate("task", "generate-unit-test.rdt.md"),
Expand Down
16 changes: 16 additions & 0 deletions lib/extension/src/conversation/input/getOpenFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as vscode from "vscode";
import { getActiveEditor } from "../../vscode/getActiveEditor";

export const getOpenFiles = async () => {
const contextDocuments = vscode.workspace.textDocuments.filter(
(document) => document.uri.scheme === "file"
);

return contextDocuments.map((document) => {
return {
name: document.fileName,
language: document.languageId,
content: document.getText(),
};
});
};
3 changes: 3 additions & 0 deletions lib/extension/src/conversation/input/resolveVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import { Variable } from "../template/RubberduckTemplate";
import { Message } from "../Message";
import { getSelectedLocationText } from "./getSelectedLocationText";
import { getSelectedTextWithDiagnostics } from "./getSelectionWithDiagnostics";
import { getOpenFiles } from "./getOpenFiles";

export async function resolveVariable(
variable: Variable,
{ messages }: { messages?: Array<Message> } = {}
): Promise<unknown> {
const variableType = variable.type;
switch (variableType) {
case "context":
return getOpenFiles();
case "constant":
return variable.value;
case "message":
Expand Down
4 changes: 4 additions & 0 deletions lib/extension/src/conversation/template/RubberduckTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ const variableSchema = zod.discriminatedUnion("type", [
index: zod.number(),
property: zod.enum(["content"]),
}),
variableBaseSchema.extend({
type: zod.literal("context"),
time: zod.enum(["conversation-start"]),
}),
variableBaseSchema.extend({
type: zod.literal("selected-text"),
time: zod.enum(["conversation-start", "message"]),
Expand Down
134 changes: 134 additions & 0 deletions template/task/explain-code-w-context.rdt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Explain Code

Explain the selected code.

## Template

### Configuration

```json conversation-template
{
"id": "explain-code-with-context",
"engineVersion": 0,
"label": "Explain Code with Context",
"description": "Explain the selected code in context of all the open files.",
"tags": ["debug", "understand"],
"header": {
"title": "Explain Code ({{location}}) in context",
"icon": {
"type": "codicon",
"value": "book"
}
},
"variables": [
{
"name": "openFiles",
"time": "conversation-start",
"type": "context"
},
{
"name": "selectedText",
"time": "conversation-start",
"type": "selected-text",
"constraints": [{ "type": "text-length", "min": 1 }]
},
{
"name": "location",
"time": "conversation-start",
"type": "selected-location-text"
},
{
"name": "firstMessage",
"time": "message",
"type": "message",
"property": "content",
"index": 0
},
{
"name": "lastMessage",
"time": "message",
"type": "message",
"property": "content",
"index": -1
}
],
"initialMessage": {
"placeholder": "Generating explanation",
"maxTokens": 2048
},
"response": {
"maxTokens": 2048,
"stop": ["Bot:", "Developer:"]
}
}
```

### Initial Message Prompt

```template-initial-message
## Instructions
Summarize the code below (emphasizing its key functionality) using the contents of the open files as context.
## Selected Code
\`\`\`{{language}}
{{selectedText}}
\`\`\`
## Open Files
{{#each openFiles}}
### File: {{name}}
\`\`\`{{language}}
{{content}}
\`\`\`
{{/each}}
## Task
Summarize the code at a high level (including goal and purpose) with an emphasis on its key functionality. Use the contents of the open files as context.
## Response
```

### Response Prompt

```template-response
## Instructions
Continue the conversation below.
Pay special attention to the current developer request.
## Current Request
Developer: {{lastMessage}}
{{#if selectedText}}
## Selected Code
\`\`\`{{language}}
{{selectedText}}
\`\`\`
{{/if}}
## Code Summary
{{firstMessage}}
## Conversation
{{#each messages}}
{{#if (neq @index 0)}}
{{#if (eq author "bot")}}
Bot: {{content}}
{{else}}
Developer: {{content}}
{{/if}}
{{/if}}
{{/each}}
## Task
Write a response that continues the conversation.
Stay focused on current developer request.
Consider the possibility that there might not be a solution.
Ask for clarification if the message does not make sense or more input is needed.
Use the style of a documentation article.
Omit any links.
Include code snippets (using Markdown) and examples where appropriate.
## Response
Bot:
```

0 comments on commit 34dade2

Please sign in to comment.