broken on jetbrains

This commit is contained in:
Dallin Romney 2024-11-07 15:58:05 -08:00
parent 8ffa1f0f75
commit 779bc84938
5 changed files with 29 additions and 17 deletions

View file

@ -1,11 +1,12 @@
import Handlebars from "handlebars";
import path from "path";
import * as YAML from "yaml";
import type { IDE, SlashCommand } from "..";
import type { ChatHistory, ChatHistoryItem, ChatMessage, ContextItem, ContinueSDK, IContextProvider, IDE, SlashCommand } from "..";
import { walkDir } from "../indexing/walkDir";
import { stripImages } from "../llm/images";
import { renderTemplatedString } from "../promptFiles/renderTemplatedString";
import { getBasename } from "../util/index";
import { BaseContextProvider } from "../context";
export const DEFAULT_PROMPTS_FOLDER = ".prompts";
@ -147,25 +148,32 @@ function extractUserInput(input: string, commandName: string): string {
return input;
}
async function renderPrompt(prompt: string, context: any, userInput: string) {
async function renderPrompt(prompt: string, context: ContinueSDK, userInput: string) {
const helpers = getContextProviderHelpers(context);
// A few context providers that don't need to be in config.json to work in .prompt files
const diff = await context.ide.getDiff(false);
const currentFile = await context.ide.getCurrentFile();
const inputData: Record<string, string> = {
diff,
input: userInput,
};
if (currentFile) {
inputData.currentFile = currentFile.path;
}
return renderTemplatedString(
prompt,
context.ide.readFile.bind(context.ide),
{ diff, currentFile: currentFile?.contents, input: userInput },
inputData,
helpers,
);
}
function getContextProviderHelpers(
context: any,
context: ContinueSDK,
): Array<[string, Handlebars.HelperDelegate]> | undefined {
return context.config.contextProviders?.map((provider: any) => [
return context.config.contextProviders?.map((provider: IContextProvider) => [
provider.description.title,
async (helperContext: any) => {
const items = await provider.getContextItems(helperContext, {
@ -179,16 +187,16 @@ function getContextProviderHelpers(
selectedCode: context.selectedCode,
});
items.forEach((item: any) =>
items.forEach((item) =>
context.addContextItem(createContextItem(item, provider)),
);
return items.map((item: any) => item.content).join("\n\n");
return items.map((item) => item.content).join("\n\n");
},
]);
}
function createContextItem(item: any, provider: any) {
function createContextItem(item: ContextItem, provider: IContextProvider) {
return {
...item,
id: {
@ -199,7 +207,7 @@ function createContextItem(item: any, provider: any) {
}
function updateChatHistory(
history: any[],
history: ChatMessage[],
commandName: string,
renderedPrompt: string,
systemMessage?: string,

View file

@ -463,7 +463,7 @@ declare global {
stepIndex: number,
): Promise<void>;
getOpenFiles(): Promise<string[]>;
getCurrentFile(): Promise<string | {
getCurrentFile(): Promise<undefined | {
isUntitled: boolean
path: string
contents: string

View file

@ -71,7 +71,7 @@ export function registerHelpers(
export async function prepareTemplateAndData(
template: string,
readFile: (filepath: string) => Promise<string>,
inputData: any,
inputData: Record<string, string>,
ctxProviderNames: string[],
): Promise<[string, any]> {
const [newTemplate, vars] = replaceFilepaths(template, ctxProviderNames);
@ -95,7 +95,7 @@ export async function prepareTemplateAndData(
return [newTemplate, data];
}
export function compileAndRenderTemplate(template: string, data: any): string {
export function compileAndRenderTemplate(template: string, data: Record<string, string>): string {
const templateFn = Handlebars.compile(template);
return templateFn(data);
}

View file

@ -8,7 +8,7 @@ import {
export async function renderTemplatedString(
template: string,
readFile: (filepath: string) => Promise<string>,
inputData: any,
inputData: Record<string, string>,
availableHelpers?: Array<[string, Handlebars.HelperDelegate]>,
): Promise<string> {
const helperPromises = availableHelpers

View file

@ -1,4 +1,4 @@
import { SlashCommand } from "..";
import { ContinueSDK, SlashCommand } from "..";
import { stripImages } from "../llm/images";
import { getContextProviderHelpers } from "./getContextProviderHelpers";
import { renderTemplatedString } from "./renderTemplatedString";
@ -37,14 +37,18 @@ export function extractUserInput(input: string, commandName: string): string {
return input;
}
export async function getDefaultVariables(context: any, userInput: string) {
export async function getDefaultVariables(context: ContinueSDK, userInput: string): Promise<Record<string, string>> {
const currentFile = await context.ide.getCurrentFile();
return { currentFile: currentFile?.contents, input: userInput };
const vars: Record<string, string> = { input: userInput };
if (currentFile) {
vars.currentFile = currentFile.path;
}
return vars;
}
export async function renderPrompt(
prompt: string,
context: any,
context: ContinueSDK,
userInput: string,
) {
const helpers = getContextProviderHelpers(context);