From 6abdb3677c03c1012e9ef13e49eaa8089d2e679f Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Fri, 7 Jun 2024 10:29:18 -0700 Subject: [PATCH] better terminal code block detection --- binary/src/index.ts | 7 +++++-- .../providers/TerminalContextProvider.ts | 5 +++++ core/llm/llms/Ollama.ts | 20 +++++++++---------- core/util/paths.ts | 14 ++++++++++++- gui/src/components/gui/StepContainer.tsx | 4 +++- .../components/markdown/CodeBlockToolbar.tsx | 4 ++-- 6 files changed, 37 insertions(+), 17 deletions(-) diff --git a/binary/src/index.ts b/binary/src/index.ts index dcfe190a9..3a417785b 100644 --- a/binary/src/index.ts +++ b/binary/src/index.ts @@ -3,7 +3,7 @@ import { Command } from "commander"; import { Core } from "core/core"; import { FromCoreProtocol, ToCoreProtocol } from "core/protocol"; import { IMessenger } from "core/util/messenger"; -import { getCoreLogsPath } from "core/util/paths"; +import { getCoreLogsPath, getPromptLogsPath } from "core/util/paths"; import fs from "node:fs"; import { IpcIde } from "./IpcIde"; import { IpcMessenger } from "./IpcMessenger"; @@ -30,7 +30,10 @@ program.action(async () => { messenger = new IpcMessenger(); } const ide = new IpcIde(messenger); - const core = new Core(messenger, ide); + const promptLogsPath = getPromptLogsPath(); + const core = new Core(messenger, ide, async (text) => { + fs.appendFileSync(promptLogsPath, text + "\n\n"); + }); } catch (e) { fs.writeFileSync("./error.log", `${new Date().toISOString()} ${e}\n`); console.log("Error: ", e); diff --git a/core/context/providers/TerminalContextProvider.ts b/core/context/providers/TerminalContextProvider.ts index 601b338a4..353ca0457 100644 --- a/core/context/providers/TerminalContextProvider.ts +++ b/core/context/providers/TerminalContextProvider.ts @@ -17,6 +17,11 @@ class TerminalContextProvider extends BaseContextProvider { query: string, extras: ContextProviderExtras, ): Promise { + if ((await extras.ide.getIdeInfo()).ideType === "jetbrains") { + throw new Error( + "The terminal context provider is not currently supported in JetBrains IDEs", + ); + } const content = await extras.ide.getTerminalContents(); return [ { diff --git a/core/llm/llms/Ollama.ts b/core/llm/llms/Ollama.ts index cd1db4a3f..8789e80d4 100644 --- a/core/llm/llms/Ollama.ts +++ b/core/llm/llms/Ollama.ts @@ -21,14 +21,14 @@ class Ollama extends BaseLLM { if (options.model === "AUTODETECT") { return; } - this.fetch(this.getEndpoint("api/show"), { - method: "POST", - headers: { - Authorization: `Bearer ${this.apiKey}`, - }, - body: JSON.stringify({ name: this._getModel() }), - }) - .then(async (response) => { + try { + this.fetch(this.getEndpoint("api/show"), { + method: "POST", + headers: { + Authorization: `Bearer ${this.apiKey}`, + }, + body: JSON.stringify({ name: this._getModel() }), + }).then(async (response) => { if (response.status !== 200) { // console.warn( // "Error calling Ollama /api/show endpoint: ", @@ -67,10 +67,8 @@ class Ollama extends BaseLLM { } } } - }) - .catch((e) => { - // console.warn(`Error calling Ollama /api/show endpoint: ${e}`); }); + } catch (e) {} } private _getModel() { diff --git a/core/util/paths.ts b/core/util/paths.ts index af45ea865..12838c1be 100644 --- a/core/util/paths.ts +++ b/core/util/paths.ts @@ -236,8 +236,20 @@ export function getContinueDotEnv(): { [key: string]: string } { return {}; } +export function getLogsDirPath(): string { + const logsPath = path.join(getContinueGlobalPath(), "logs"); + if (!fs.existsSync(logsPath)) { + fs.mkdirSync(logsPath); + } + return logsPath; +} + export function getCoreLogsPath(): string { - return path.join(getContinueGlobalPath(), "core.log"); + return path.join(getLogsDirPath(), "core.log"); +} + +export function getPromptLogsPath(): string { + return path.join(getLogsDirPath(), "prompt.log"); } export function getGlobalPromptsPath(): string { diff --git a/gui/src/components/gui/StepContainer.tsx b/gui/src/components/gui/StepContainer.tsx index e7ed9fbd4..898e2193d 100644 --- a/gui/src/components/gui/StepContainer.tsx +++ b/gui/src/components/gui/StepContainer.tsx @@ -71,13 +71,15 @@ function StepContainer(props: StepContainerProps) { const [feedback, setFeedback] = useState(undefined); + const sessionId = useSelector((store: RootState) => store.state.sessionId); + const sendFeedback = (feedback: boolean) => { setFeedback(feedback); if (props.item.promptLogs?.length) { for (const promptLog of props.item.promptLogs) { ideMessenger.post("devdata/log", { tableName: "chat", - data: { ...promptLog, feedback }, + data: { ...promptLog, feedback, sessionId }, }); } } diff --git a/gui/src/components/markdown/CodeBlockToolbar.tsx b/gui/src/components/markdown/CodeBlockToolbar.tsx index bf14e441e..84e521192 100644 --- a/gui/src/components/markdown/CodeBlockToolbar.tsx +++ b/gui/src/components/markdown/CodeBlockToolbar.tsx @@ -64,7 +64,7 @@ const commonTerminalCommands = [ function isTerminalCodeBlock(language: string | undefined, text: string) { return ( terminalLanguages.includes(language) || - (language?.length === 0 && + ((!language || language?.length === 0) && (text.trim().split("\n").length === 1 || commonTerminalCommands.some((c) => text.trim().startsWith(c)))) ); @@ -91,7 +91,7 @@ function CodeBlockToolBar(props: CodeBlockToolBarProps) { disabled={applying} style={{ backgroundColor: vscEditorBackground }} onClick={() => { - if (terminalLanguages.includes(props.language)) { + if (isTerminalCodeBlock(props.language, props.text)) { let text = props.text; if (text.startsWith("$ ")) { text = text.slice(2);