From 779bc84938497facb22eeef320dab012b8ebf0cb Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 7 Nov 2024 15:58:05 -0800 Subject: [PATCH] broken on jetbrains --- core/config/promptFile.ts | 26 ++++++++++++------- core/config/types.ts | 2 +- core/promptFiles/handlebarUtils.ts | 4 +-- core/promptFiles/renderTemplatedString.ts | 2 +- .../promptFiles/slashCommandFromPromptFile.ts | 12 ++++++--- 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/core/config/promptFile.ts b/core/config/promptFile.ts index a22dfadde..728090fcc 100644 --- a/core/config/promptFile.ts +++ b/core/config/promptFile.ts @@ -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 = { + 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, diff --git a/core/config/types.ts b/core/config/types.ts index b886ea021..f457812d1 100644 --- a/core/config/types.ts +++ b/core/config/types.ts @@ -463,7 +463,7 @@ declare global { stepIndex: number, ): Promise; getOpenFiles(): Promise; - getCurrentFile(): Promise Promise, - inputData: any, + inputData: Record, 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 { const templateFn = Handlebars.compile(template); return templateFn(data); } diff --git a/core/promptFiles/renderTemplatedString.ts b/core/promptFiles/renderTemplatedString.ts index d31cce0b9..bcc645ace 100644 --- a/core/promptFiles/renderTemplatedString.ts +++ b/core/promptFiles/renderTemplatedString.ts @@ -8,7 +8,7 @@ import { export async function renderTemplatedString( template: string, readFile: (filepath: string) => Promise, - inputData: any, + inputData: Record, availableHelpers?: Array<[string, Handlebars.HelperDelegate]>, ): Promise { const helperPromises = availableHelpers diff --git a/core/promptFiles/slashCommandFromPromptFile.ts b/core/promptFiles/slashCommandFromPromptFile.ts index 3df3cb8fe..af865078e 100644 --- a/core/promptFiles/slashCommandFromPromptFile.ts +++ b/core/promptFiles/slashCommandFromPromptFile.ts @@ -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> { const currentFile = await context.ide.getCurrentFile(); - return { currentFile: currentFile?.contents, input: userInput }; + const vars: Record = { 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);