From 7dcb08b09ab03c06c69429643c303cd72c36316d Mon Sep 17 00:00:00 2001 From: Mathew Pareles Date: Wed, 12 Feb 2025 20:50:04 -0800 Subject: [PATCH 01/47] comment out unwanted code --- .../void/browser/inlineDiffsService.ts | 322 +++++++++--------- .../contrib/void/browser/void.contribution.ts | 2 +- 2 files changed, 162 insertions(+), 162 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts index 998a286f..2be7ae7b 100644 --- a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts +++ b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts @@ -25,7 +25,7 @@ import * as dom from '../../../../base/browser/dom.js'; import { Widget } from '../../../../base/browser/ui/widget.js'; import { URI } from '../../../../base/common/uri.js'; import { IConsistentEditorItemService, IConsistentItemService } from './helperServices/consistentItemService.js'; -import { voidPrefixAndSuffix, ctrlKStream_userMessage, ctrlKStream_systemMessage, fastApply_rewritewholething_userMessage, fastApply_rewritewholething_systemMessage, defaultFimTags, fastApply_searchreplace_systemMessage, fastApply_searchreplace_userMessage } from './prompt/prompts.js'; +import { voidPrefixAndSuffix, ctrlKStream_userMessage, ctrlKStream_systemMessage, fastApply_rewritewholething_userMessage, fastApply_rewritewholething_systemMessage, defaultFimTags } from './prompt/prompts.js'; import { mountCtrlK } from '../browser/react/out/quick-edit-tsx/index.js' import { QuickEditPropsType } from './quickEditActions.js'; @@ -1207,39 +1207,39 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService { return null } - private _generateSearchAndReplaceBlocks({ filename, applyStr }: { filename: URI, applyStr: string }): DiffZone | undefined { + // private _generateSearchAndReplaceBlocks({ filename, applyStr }: { filename: URI, applyStr: string }): DiffZone | undefined { - // call LLM to generate search and replace blocks (outputs something like [{search: 'this is my code', replace: 'this is m'}, ... ]) + // // call LLM to generate search and replace blocks (outputs something like [{search: 'this is my code', replace: 'this is m'}, ... ]) - // 1a output search block + // // 1a output search block - let uri: URI + // let uri: URI - const uri_ = this._getActiveEditorURI() - if (!uri_) return - uri = uri_ + // const uri_ = this._getActiveEditorURI() + // if (!uri_) return + // uri = uri_ - // reject all diffZones on this URI, adding to history (there can't possibly be overlap after this) - this.removeDiffAreas({ uri, behavior: 'reject', removeCtrlKs: true }) + // // reject all diffZones on this URI, adding to history (there can't possibly be overlap after this) + // this.removeDiffAreas({ uri, behavior: 'reject', removeCtrlKs: true }) - // in ctrl+L the start and end lines are the full document + // // in ctrl+L the start and end lines are the full document - const numLines = this._getNumLines(uri) - if (numLines === null) return + // const numLines = this._getNumLines(uri) + // if (numLines === null) return - let startLine: number - let endLine: number + // let startLine: number + // let endLine: number - startLine = 1 - endLine = numLines + // startLine = 1 + // endLine = numLines - const currentFileStr = this._readURI(uri) - if (currentFileStr === null) return - const originalCode = currentFileStr.split('\n').slice((startLine - 1), (endLine - 1) + 1).join('\n') + // const currentFileStr = this._readURI(uri) + // if (currentFileStr === null) return + // const originalCode = currentFileStr.split('\n').slice((startLine - 1), (endLine - 1) + 1).join('\n') - // 1b find the start and end line that the search block lives on (if can't find it, retry 1a) + // // 1b find the start and end line that the search block lives on (if can't find it, retry 1a) @@ -1247,174 +1247,174 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService { - let streamRequestIdRef: { current: string | null } = { current: null } + // let streamRequestIdRef: { current: string | null } = { current: null } - // add to history - const { onFinishEdit } = this._addToHistory(uri) + // // add to history + // const { onFinishEdit } = this._addToHistory(uri) - // __TODO__ let users customize modelFimTags - const isOllamaFIM = false // this._voidSettingsService.state.modelSelectionOfFeature['Ctrl+K']?.providerName === 'ollama' - const modelFimTags = defaultFimTags + // // __TODO__ let users customize modelFimTags + // const isOllamaFIM = false // this._voidSettingsService.state.modelSelectionOfFeature['Ctrl+K']?.providerName === 'ollama' + // const modelFimTags = defaultFimTags - const adding: Omit = { - type: 'DiffZone', - originalCode, - startLine, - endLine, - _URI: uri, - _streamState: { - isStreaming: true, - streamRequestIdRef, - line: startLine, - }, - _diffOfId: {}, // added later - _removeStylesFns: new Set(), - } - const diffZone = this._addDiffArea(adding) - this._onDidChangeStreaming.fire({ uri, diffareaid: diffZone.diffareaid }) - this._onDidAddOrDeleteDiffZones.fire({ uri }) + // const adding: Omit = { + // type: 'DiffZone', + // originalCode, + // startLine, + // endLine, + // _URI: uri, + // _streamState: { + // isStreaming: true, + // streamRequestIdRef, + // line: startLine, + // }, + // _diffOfId: {}, // added later + // _removeStylesFns: new Set(), + // } + // const diffZone = this._addDiffArea(adding) + // this._onDidChangeStreaming.fire({ uri, diffareaid: diffZone.diffareaid }) + // this._onDidAddOrDeleteDiffZones.fire({ uri }) - if (from === 'QuickEdit') { - const { diffareaid } = opts - const ctrlKZone = this.diffAreaOfId[diffareaid] - if (ctrlKZone.type !== 'CtrlKZone') return + // if (from === 'QuickEdit') { + // const { diffareaid } = opts + // const ctrlKZone = this.diffAreaOfId[diffareaid] + // if (ctrlKZone.type !== 'CtrlKZone') return - ctrlKZone._linkedStreamingDiffZone = diffZone.diffareaid - } + // ctrlKZone._linkedStreamingDiffZone = diffZone.diffareaid + // } - // now handle messages - let messages: LLMChatMessage[] + // // now handle messages + // let messages: LLMChatMessage[] - if (from === 'Chat') { - const userContent = fastApply_searchreplace_userMessage({ originalCode, applyStr: opts.applyStr, uri }) - messages = [ - { role: 'system', content: fastApply_rewritewholething_systemMessage, }, - { role: 'user', content: userContent, } - ] - } - else if (from === 'QuickEdit') { - const { diffareaid } = opts - const ctrlKZone = this.diffAreaOfId[diffareaid] - if (ctrlKZone.type !== 'CtrlKZone') return - const { _mountInfo } = ctrlKZone - const instructions = _mountInfo?.textAreaRef.current?.value ?? '' + // if (from === 'Chat') { + // const userContent = fastApply_searchreplace_userMessage({ originalCode, applyStr: opts.applyStr, uri }) + // messages = [ + // { role: 'system', content: fastApply_rewritewholething_systemMessage, }, + // { role: 'user', content: userContent, } + // ] + // } + // else if (from === 'QuickEdit') { + // const { diffareaid } = opts + // const ctrlKZone = this.diffAreaOfId[diffareaid] + // if (ctrlKZone.type !== 'CtrlKZone') return + // const { _mountInfo } = ctrlKZone + // const instructions = _mountInfo?.textAreaRef.current?.value ?? '' - // __TODO__ use Ollama's FIM api, if (isOllamaFIM) {...} else: - const { prefix, suffix } = voidPrefixAndSuffix({ fullFileStr: currentFileStr, startLine, endLine }) - // if (isOllamaFIM) { - // messages = { - // type: 'ollamaFIM', - // prefix, - // suffix, - // } + // // __TODO__ use Ollama's FIM api, if (isOllamaFIM) {...} else: + // const { prefix, suffix } = voidPrefixAndSuffix({ fullFileStr: currentFileStr, startLine, endLine }) + // // if (isOllamaFIM) { + // // messages = { + // // type: 'ollamaFIM', + // // prefix, + // // suffix, + // // } - // } - // else { - const language = filenameToVscodeLanguage(uri.fsPath) ?? '' - const userContent = ctrlKStream_userMessage({ selection: originalCode, instructions: instructions, prefix, suffix, isOllamaFIM: false, fimTags: modelFimTags, language }) - // type: 'messages', - messages = [ - { role: 'system', content: ctrlKStream_systemMessage({ fimTags: modelFimTags }), }, - { role: 'user', content: userContent, } - ] - // } - } - else { throw new Error(`featureName ${from} is invalid`) } + // // } + // // else { + // const language = filenameToVscodeLanguage(uri.fsPath) ?? '' + // const userContent = ctrlKStream_userMessage({ selection: originalCode, instructions: instructions, prefix, suffix, isOllamaFIM: false, fimTags: modelFimTags, language }) + // // type: 'messages', + // messages = [ + // { role: 'system', content: ctrlKStream_systemMessage({ fimTags: modelFimTags }), }, + // { role: 'user', content: userContent, } + // ] + // // } + // } + // else { throw new Error(`featureName ${from} is invalid`) } - const onDone = (hadError: boolean) => { - diffZone._streamState = { isStreaming: false, } - this._onDidChangeStreaming.fire({ uri, diffareaid: diffZone.diffareaid }) + // const onDone = (hadError: boolean) => { + // diffZone._streamState = { isStreaming: false, } + // this._onDidChangeStreaming.fire({ uri, diffareaid: diffZone.diffareaid }) - if (from === 'QuickEdit') { - const ctrlKZone = this.diffAreaOfId[opts.diffareaid] as CtrlKZone + // if (from === 'QuickEdit') { + // const ctrlKZone = this.diffAreaOfId[opts.diffareaid] as CtrlKZone - ctrlKZone._linkedStreamingDiffZone = null - this._deleteCtrlKZone(ctrlKZone) - } - this._refreshStylesAndDiffsInURI(uri) - onFinishEdit() + // ctrlKZone._linkedStreamingDiffZone = null + // this._deleteCtrlKZone(ctrlKZone) + // } + // this._refreshStylesAndDiffsInURI(uri) + // onFinishEdit() - // if had error, revert! - if (hadError) { - this._undoHistory(diffZone._URI) - } - } + // // if had error, revert! + // if (hadError) { + // this._undoHistory(diffZone._URI) + // } + // } - // refresh now in case onText takes a while to get 1st message - this._refreshStylesAndDiffsInURI(uri) + // // refresh now in case onText takes a while to get 1st message + // this._refreshStylesAndDiffsInURI(uri) - const extractText = (fullText: string, recentlyAddedTextLen: number) => { - if (from === 'QuickEdit') { - if (isOllamaFIM) return fullText - return extractCodeFromFIM({ text: fullText, recentlyAddedTextLen, midTag: modelFimTags.midTag }) - } - else if (from === 'Chat') { - return extractCodeFromRegular({ text: fullText, recentlyAddedTextLen }) - } - throw 1 - } + // const extractText = (fullText: string, recentlyAddedTextLen: number) => { + // if (from === 'QuickEdit') { + // if (isOllamaFIM) return fullText + // return extractCodeFromFIM({ text: fullText, recentlyAddedTextLen, midTag: modelFimTags.midTag }) + // } + // else if (from === 'Chat') { + // return extractCodeFromRegular({ text: fullText, recentlyAddedTextLen }) + // } + // throw 1 + // } - const latestStreamInfo = { line: diffZone.startLine, addedSplitYet: false, col: 1, originalCodeStartLine: 1 } + // const latestStreamInfo = { line: diffZone.startLine, addedSplitYet: false, col: 1, originalCodeStartLine: 1 } - // state used in onText: - let fullText = '' - let prevIgnoredSuffix = '' + // // state used in onText: + // let fullText = '' + // let prevIgnoredSuffix = '' - streamRequestIdRef.current = this._llmMessageService.sendLLMMessage({ - messagesType: 'chatMessages', - useProviderFor: opts.from === 'Chat' ? 'FastApply' : 'Ctrl+K', - logging: { loggingName: `startApplying - ${from}` }, - messages, - onText: ({ newText: newText_ }) => { + // streamRequestIdRef.current = this._llmMessageService.sendLLMMessage({ + // messagesType: 'chatMessages', + // useProviderFor: opts.from === 'Chat' ? 'FastApply' : 'Ctrl+K', + // logging: { loggingName: `startApplying - ${from}` }, + // messages, + // onText: ({ newText: newText_ }) => { - const newText = prevIgnoredSuffix + newText_ // add the previously ignored suffix because it's no longer the suffix! - fullText += prevIgnoredSuffix + newText + // const newText = prevIgnoredSuffix + newText_ // add the previously ignored suffix because it's no longer the suffix! + // fullText += prevIgnoredSuffix + newText - const [text, deltaText, ignoredSuffix] = extractText(fullText, newText.length) - this._writeStreamedDiffZoneLLMText(diffZone, text, deltaText, latestStreamInfo) - this._refreshStylesAndDiffsInURI(uri) + // const [text, deltaText, ignoredSuffix] = extractText(fullText, newText.length) + // this._writeStreamedDiffZoneLLMText(diffZone, text, deltaText, latestStreamInfo) + // this._refreshStylesAndDiffsInURI(uri) - prevIgnoredSuffix = ignoredSuffix - }, - onFinalMessage: ({ fullText }) => { - // console.log('DONE! FULL TEXT\n', extractText(fullText), diffZone.startLine, diffZone.endLine) - // at the end, re-write whole thing to make sure no sync errors - const [text, _] = extractText(fullText, 0) - this._writeText(uri, text, - { startLineNumber: diffZone.startLine, startColumn: 1, endLineNumber: diffZone.endLine, endColumn: Number.MAX_SAFE_INTEGER }, // 1-indexed - { shouldRealignDiffAreas: true } - ) - onDone(false) - }, - onError: (e) => { - const details = errorDetails(e.fullError) - this._notificationService.notify({ - severity: Severity.Warning, - message: `Void Error: ${e.message}`, - actions: { - secondary: [{ - id: 'void.onerror.opensettings', - enabled: true, - label: 'Open Void settings', - tooltip: '', - class: undefined, - run: () => { this._commandService.executeCommand(VOID_OPEN_SETTINGS_ACTION_ID) } - }] - }, - source: details ? `(Hold ${isMacintosh ? 'Option' : 'Alt'} to hover) - ${details}` : undefined - }) - onDone(true) - }, + // prevIgnoredSuffix = ignoredSuffix + // }, + // onFinalMessage: ({ fullText }) => { + // // console.log('DONE! FULL TEXT\n', extractText(fullText), diffZone.startLine, diffZone.endLine) + // // at the end, re-write whole thing to make sure no sync errors + // const [text, _] = extractText(fullText, 0) + // this._writeText(uri, text, + // { startLineNumber: diffZone.startLine, startColumn: 1, endLineNumber: diffZone.endLine, endColumn: Number.MAX_SAFE_INTEGER }, // 1-indexed + // { shouldRealignDiffAreas: true } + // ) + // onDone(false) + // }, + // onError: (e) => { + // const details = errorDetails(e.fullError) + // this._notificationService.notify({ + // severity: Severity.Warning, + // message: `Void Error: ${e.message}`, + // actions: { + // secondary: [{ + // id: 'void.onerror.opensettings', + // enabled: true, + // label: 'Open Void settings', + // tooltip: '', + // class: undefined, + // run: () => { this._commandService.executeCommand(VOID_OPEN_SETTINGS_ACTION_ID) } + // }] + // }, + // source: details ? `(Hold ${isMacintosh ? 'Option' : 'Alt'} to hover) - ${details}` : undefined + // }) + // onDone(true) + // }, - }) + // }) - return diffZone + // return diffZone - } + // } diff --git a/src/vs/workbench/contrib/void/browser/void.contribution.ts b/src/vs/workbench/contrib/void/browser/void.contribution.ts index 19d20201..c10ca414 100644 --- a/src/vs/workbench/contrib/void/browser/void.contribution.ts +++ b/src/vs/workbench/contrib/void/browser/void.contribution.ts @@ -22,7 +22,7 @@ import './chatThreadService.js' import './autocompleteService.js' // register Context services -import './contextGatheringService.js' +// import './contextGatheringService.js' // import './contextUserChangesService.js' // settings pane From 4f9f16b93f31c580027b30a09a0e67cd235ca707 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Wed, 12 Feb 2025 21:27:39 -0800 Subject: [PATCH 02/47] almost works (but doesn't delete old text) --- .../browser/helpers/extractCodeFromResult.ts | 74 +++++++++ .../void/browser/inlineDiffsService.ts | 153 +++++++----------- .../react/src/markdown/ChatMarkdownRender.tsx | 2 +- 3 files changed, 133 insertions(+), 96 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/helpers/extractCodeFromResult.ts b/src/vs/workbench/contrib/void/browser/helpers/extractCodeFromResult.ts index 1cb53e5c..a0addcae 100644 --- a/src/vs/workbench/contrib/void/browser/helpers/extractCodeFromResult.ts +++ b/src/vs/workbench/contrib/void/browser/helpers/extractCodeFromResult.ts @@ -175,3 +175,77 @@ export const extractCodeFromFIM = ({ text, recentlyAddedTextLen, midTag, }: { te } + + + +export type ExtractedSearchReplaceBlock = { + state: 'writingOriginal' | 'writingFinal' | 'done', + orig: string, + final: string, +} + + +const endsWithAnyPrefixOf = (str: string, anyPrefix: string) => { + // for each prefix + for (let i = anyPrefix.length; i >= 0; i--) { + const prefix = anyPrefix.slice(0, i) + if (str.endsWith(prefix)) return prefix + } + return null +} + +// guarantees if you keep adding text, array length will strictly grow and state will progress without going back +export const extractSearchReplaceBlocks = (str: string, { ORIGINAL, DIVIDER, FINAL }: { ORIGINAL: string, DIVIDER: string, FINAL: string }) => { + + const ORIGINAL_ = ORIGINAL + `\n` + const DIVIDER_ = '\n' + DIVIDER + `\n` + const FINAL_ = '\n' + FINAL + + + const blocks: ExtractedSearchReplaceBlock[] = [] + + let i = 0 // search i and beyond (this is done by plain index, not by line number. much simpler this way) + while (true) { + let origStart = str.indexOf(ORIGINAL_, i) + if (origStart === -1) { return blocks } + origStart += ORIGINAL_.length + i = origStart + // wrote <<<< ORIGINAL + + let dividerStart = str.indexOf(DIVIDER_, i) + if (dividerStart === -1) { // if didnt find DIVIDER_, either writing originalStr or DIVIDER_ right now + const isWritingDIVIDER = endsWithAnyPrefixOf(str, DIVIDER_) + blocks.push({ + orig: str.substring(origStart, str.length - (isWritingDIVIDER?.length ?? 0)), + final: '', + state: 'writingOriginal' + }) + return blocks + } + const origStrDone = str.substring(origStart, dividerStart) + dividerStart += DIVIDER_.length + i = dividerStart + // wrote ===== + + let finalStart = str.indexOf(FINAL_, i) + if (finalStart === -1) { // if didnt find FINAL_, either writing finalStr or FINAL_ right now + const isWritingFINAL = endsWithAnyPrefixOf(str, FINAL_) + blocks.push({ + orig: origStrDone, + final: str.substring(dividerStart, str.length - (isWritingFINAL?.length ?? 0)), + state: 'writingFinal' + }) + return blocks + } + const finalStrDone = str.substring(dividerStart, finalStart) + finalStart += FINAL_.length + i = finalStart + // wrote >>>>> FINAL + + blocks.push({ + orig: origStrDone, + final: finalStrDone, + state: 'done' + }) + } +} diff --git a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts index ed435a26..81e5dcbc 100644 --- a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts +++ b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts @@ -30,7 +30,7 @@ import { voidPrefixAndSuffix, ctrlKStream_userMessage, ctrlKStream_systemMessage import { mountCtrlK } from '../browser/react/out/quick-edit-tsx/index.js' import { QuickEditPropsType } from './quickEditActions.js'; import { IModelContentChangedEvent } from '../../../../editor/common/textModelEvents.js'; -import { extractCodeFromFIM, extractCodeFromRegular } from './helpers/extractCodeFromResult.js'; +import { extractCodeFromFIM, extractCodeFromRegular, ExtractedSearchReplaceBlock, extractSearchReplaceBlocks } from './helpers/extractCodeFromResult.js'; import { filenameToVscodeLanguage } from './helpers/detectLanguage.js'; import { INotificationService, Severity } from '../../../../platform/notification/common/notification.js'; import { isMacintosh } from '../../../../base/common/platform.js'; @@ -138,12 +138,6 @@ export type Diff = { -type ExtractedCodeBlock = { - state: 'writingOriginal' | 'writingFinal' | 'done', - orig: string, - final: string, -} - // _ means anything we don't include if we clone it // DiffArea.originalStartLine is the line in originalCode (not the file) @@ -1249,71 +1243,6 @@ INSTRUCTIONS Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggested SEARCH/REPLACE blocks, without any explanation. ` - const endsWithAnyPrefixOf = (str: string, anyPrefix: string) => { - // for each prefix - for (let i = anyPrefix.length; i >= 0; i--) { - const prefix = anyPrefix.slice(0, i) - if (str.endsWith(prefix)) return prefix - } - return null - } - - const extractBlocks = (str: string) => { - - const ORIGINAL_ = ORIGINAL + `\n` - const DIVIDER_ = '\n' + DIVIDER + `\n` - const FINAL_ = '\n' + FINAL - - - const blocks: ExtractedCodeBlock[] = [] - - let i = 0 // search i and beyond (this is done by plain index, not by line number. much simpler this way) - while (true) { - let origStart = str.indexOf(ORIGINAL_, i) - if (origStart === -1) { return blocks } - origStart += ORIGINAL_.length - i = origStart - // wrote <<<< ORIGINAL - - let dividerStart = str.indexOf(DIVIDER_, i) - if (dividerStart === -1) { // if didnt find DIVIDER_, either writing originalStr or DIVIDER_ right now - const isWritingDIVIDER = endsWithAnyPrefixOf(str, DIVIDER_) - blocks.push({ - orig: str.substring(origStart, str.length - (isWritingDIVIDER?.length ?? 0)), - final: '', - state: 'writingOriginal' - }) - return blocks - } - const origStrDone = str.substring(origStart, dividerStart) - dividerStart += DIVIDER_.length - i = dividerStart - // wrote ===== - - let finalStart = str.indexOf(FINAL_, i) - if (finalStart === -1) { // if didnt find FINAL_, either writing finalStr or FINAL_ right now - const isWritingFINAL = endsWithAnyPrefixOf(str, FINAL_) - blocks.push({ - orig: origStrDone, - final: str.substring(origStart, str.length - (isWritingFINAL?.length ?? 0)), - state: 'writingFinal' - }) - return blocks - } - const finalStrDone = str.substring(dividerStart, finalStart) - finalStart += FINAL_.length - i = finalStart - // wrote >>>>> FINAL - - blocks.push({ - orig: origStrDone, - final: finalStrDone, - state: 'done' - }) - } - } - - // reject all diffZones on this URI, adding to history (there can't possibly be overlap after this) this.removeDiffAreas({ uri, behavior: 'reject', removeCtrlKs: true }) @@ -1326,8 +1255,16 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest const diffareaidOfBlockNum: number[] = [] + // TODO replace all these with whatever block we're on initially if already started + let latestStreamInfoMutable: { line: number, col: number, addedSplitYet: boolean, originalCodeStartLine: number } | null = null + let currStreamingBlockNum = 0 + let oldBlocks: ExtractedSearchReplaceBlock[] = [] + const onText = ({ newText, fullText }: { newText: string, fullText: string }) => { - const blocks = extractBlocks(fullText) + console.log('FULLTEXT', fullText) + console.log('NEW', newText) + + const blocks = extractSearchReplaceBlocks(fullText, { ORIGINAL, DIVIDER, FINAL }) // find block.orig in fileContents and return its range in file const findTextInCode = (text: string, fileContents: string) => { @@ -1341,22 +1278,27 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest return [startLine, endLine] } - let latestStreamInfoMutable: any = {} - for (let blockNum = 0; blockNum < blocks.length; blockNum += 1) { + for (let blockNum = currStreamingBlockNum; blockNum < blocks.length; blockNum += 1) { const block = blocks[blockNum] - if (block.state === 'writingOriginal') continue - const foundInCode = findTextInCode(block.orig, fileContents) - if (typeof foundInCode === 'string') { - console.log('ERROR!!!!', foundInCode) + if (block.state === 'done') + currStreamingBlockNum = blockNum + + if (block.state === 'writingOriginal') continue - } - - const [startLine, endLine] = foundInCode + let deltaFinalText: string // if should add new diffarea - if (blockNum > diffareaidOfBlockNum.length) { + if (!(blockNum in diffareaidOfBlockNum)) { + const foundInCode = findTextInCode(block.orig, fileContents) + if (typeof foundInCode === 'string') { + console.log('NOT FOUND IN CODE!!!!', foundInCode) + break + } + const [startLine, endLine] = foundInCode + + console.log('ADDING', blockNum) const adding: Omit = { type: 'DiffZone', originalCode: block.orig, @@ -1378,19 +1320,30 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest diffareaidOfBlockNum.push(diffZone.diffareaid) latestStreamInfoMutable = { line: diffZone.startLine, addedSplitYet: false, col: 1, originalCodeStartLine: 1 } + + deltaFinalText = block.final + } + else { + deltaFinalText = block.final.substring((oldBlocks[blockNum]?.final ?? '').length, Infinity) } + console.log('DELTA', deltaFinalText) + oldBlocks = blocks + + // write new text to diffarea const diffareaid = diffareaidOfBlockNum[blockNum] const diffZone = this.diffAreaOfId[diffareaid] - if (diffZone.type !== 'DiffZone') continue + if (diffZone?.type !== 'DiffZone') continue - this._writeStreamedDiffZoneLLMText(diffZone, fullText, newText, latestStreamInfoMutable) - this._refreshStylesAndDiffsInURI(uri) + + if (!latestStreamInfoMutable) continue + this._writeStreamedDiffZoneLLMText(diffZone, block.final, deltaFinalText, latestStreamInfoMutable) } - + this._refreshStylesAndDiffsInURI(uri) } + const { onFinishEdit } = this._addToHistory(uri) // TODO turn this into a service and provide it @@ -1400,8 +1353,18 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest logging: { loggingName: `generateSearchAndReplace` }, messages, onText: ({ newText, fullText }) => { onText({ newText, fullText }) }, - onFinalMessage: ({ fullText }) => { }, - onError: (e) => { console.log('ERROR', e) }, + onFinalMessage: ({ fullText }) => { + // 1. wait 500ms and fix lint errors - call lint error workflow + // (update react state to say "Fixing errors") + this._refreshStylesAndDiffsInURI(uri) + onFinishEdit() + + }, + onError: (e) => { + console.log('ERROR', e); + this._refreshStylesAndDiffsInURI(uri) + onFinishEdit() + }, }) @@ -1564,19 +1527,19 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest onText: ({ newText: newText_ }) => { const newText = prevIgnoredSuffix + newText_ // add the previously ignored suffix because it's no longer the suffix! - fullText += prevIgnoredSuffix + newText + fullText += prevIgnoredSuffix + newText // full text, including ```, etc - const [text, deltaText, ignoredSuffix] = extractText(fullText, newText.length) - this._writeStreamedDiffZoneLLMText(diffZone, text, deltaText, latestStreamInfoMutable) + const [croppedText, deltaCroppedText, croppedSuffix] = extractText(fullText, newText.length) + this._writeStreamedDiffZoneLLMText(diffZone, croppedText, deltaCroppedText, latestStreamInfoMutable) this._refreshStylesAndDiffsInURI(uri) - prevIgnoredSuffix = ignoredSuffix + prevIgnoredSuffix = croppedSuffix }, onFinalMessage: ({ fullText }) => { // console.log('DONE! FULL TEXT\n', extractText(fullText), diffZone.startLine, diffZone.endLine) // at the end, re-write whole thing to make sure no sync errors - const [text, _] = extractText(fullText, 0) - this._writeText(uri, text, + const [croppedText, _1, _2] = extractText(fullText, 0) + this._writeText(uri, croppedText, { startLineNumber: diffZone.startLine, startColumn: 1, endLineNumber: diffZone.endLine, endColumn: Number.MAX_SAFE_INTEGER }, // 1-indexed { shouldRealignDiffAreas: true } ) diff --git a/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx b/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx index 7b03f068..c6f88762 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx @@ -102,7 +102,7 @@ const RenderToken = ({ token, nested = false, noSpace = false, chatMessageLocati // deal with built-in tokens first (assume marked token) const t = token as MarkedToken - console.log(t.raw) + // console.log('render:', t.raw) if (t.type === "space") { return {t.raw} From 7cdb003c47a9ec50e06160c0255f9a3539e5ee08 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Wed, 12 Feb 2025 21:51:58 -0800 Subject: [PATCH 03/47] stream state --- .../void/browser/inlineDiffsService.ts | 54 +++++++++++++------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts index 81e5dcbc..78a02e40 100644 --- a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts +++ b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts @@ -211,6 +211,10 @@ type HistorySnapshot = { +// line/col is the location, originalCodeStartLine is the start line of the original code being displayed +type StreamLocationMutable = { line: number, col: number, addedSplitYet: boolean, originalCodeStartLine: number } + + export interface IInlineDiffsService { readonly _serviceBrand: undefined; startApplying(opts: StartApplyingOpts): number | undefined; @@ -996,7 +1000,7 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService { // @throttle(100) - private _writeStreamedDiffZoneLLMText(diffZone: DiffZone, llmText: string, deltaText: string, latestMutable: { line: number, col: number, addedSplitYet: boolean, originalCodeStartLine: number }) { + private _writeStreamedDiffZoneLLMText(diffZone: DiffZone, llmText: string, deltaText: string, latestMutable: StreamLocationMutable) { // ----------- 1. Write the new code to the document ----------- // figure out where to highlight based on where the AI is in the stream right now, use the last diff to figure that out @@ -1256,13 +1260,11 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest const diffareaidOfBlockNum: number[] = [] // TODO replace all these with whatever block we're on initially if already started - let latestStreamInfoMutable: { line: number, col: number, addedSplitYet: boolean, originalCodeStartLine: number } | null = null + let latestStreamLocationMutable: StreamLocationMutable | null = null let currStreamingBlockNum = 0 let oldBlocks: ExtractedSearchReplaceBlock[] = [] const onText = ({ newText, fullText }: { newText: string, fullText: string }) => { - console.log('FULLTEXT', fullText) - console.log('NEW', newText) const blocks = extractSearchReplaceBlocks(fullText, { ORIGINAL, DIVIDER, FINAL }) @@ -1297,6 +1299,7 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest break } const [startLine, endLine] = foundInCode + console.log('FOUND!', foundInCode) console.log('ADDING', blockNum) const adding: Omit = { @@ -1319,7 +1322,7 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest diffareaidOfBlockNum.push(diffZone.diffareaid) - latestStreamInfoMutable = { line: diffZone.startLine, addedSplitYet: false, col: 1, originalCodeStartLine: 1 } + latestStreamLocationMutable = { line: diffZone.endLine, addedSplitYet: false, col: 1, originalCodeStartLine: 1 } deltaFinalText = block.final } @@ -1327,7 +1330,7 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest deltaFinalText = block.final.substring((oldBlocks[blockNum]?.final ?? '').length, Infinity) } - console.log('DELTA', deltaFinalText) + console.log('FULLTEXT', block.final) oldBlocks = blocks // write new text to diffarea @@ -1336,13 +1339,34 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest if (diffZone?.type !== 'DiffZone') continue - if (!latestStreamInfoMutable) continue - this._writeStreamedDiffZoneLLMText(diffZone, block.final, deltaFinalText, latestStreamInfoMutable) - } + if (!latestStreamLocationMutable) continue + this._writeStreamedDiffZoneLLMText(diffZone, block.final, deltaFinalText, latestStreamLocationMutable) + } // end for + this._refreshStylesAndDiffsInURI(uri) } + + const onDone = (hadError: boolean) => { + for (const blockNum in diffareaidOfBlockNum) { + const diffareaid = diffareaidOfBlockNum[blockNum] + const diffZone = this.diffAreaOfId[diffareaid] + if (diffZone?.type !== 'DiffZone') continue + + diffZone._streamState = { isStreaming: false, } + this._onDidChangeStreaming.fire({ uri, diffareaid: diffZone.diffareaid }) + + this._refreshStylesAndDiffsInURI(uri) + onFinishEdit() + + // if had error, revert! + if (hadError) this._undoHistory(diffZone._URI) + } + } + + + const { onFinishEdit } = this._addToHistory(uri) @@ -1352,18 +1376,18 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest useProviderFor: 'FastApply', logging: { loggingName: `generateSearchAndReplace` }, messages, - onText: ({ newText, fullText }) => { onText({ newText, fullText }) }, + onText: ({ newText, fullText }) => { + onText({ newText, fullText }) + }, onFinalMessage: ({ fullText }) => { // 1. wait 500ms and fix lint errors - call lint error workflow // (update react state to say "Fixing errors") - this._refreshStylesAndDiffsInURI(uri) - onFinishEdit() + onDone(false) }, onError: (e) => { console.log('ERROR', e); - this._refreshStylesAndDiffsInURI(uri) - onFinishEdit() + onDone(true) }, }) @@ -1513,7 +1537,7 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest throw 1 } - const latestStreamInfoMutable = { line: diffZone.startLine, addedSplitYet: false, col: 1, originalCodeStartLine: 1 } + const latestStreamInfoMutable: StreamLocationMutable = { line: diffZone.startLine, addedSplitYet: false, col: 1, originalCodeStartLine: 1 } // state used in onText: let fullText = '' From a479a32ad5ab0382180d6d3b8906c9db7312c237 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Wed, 12 Feb 2025 21:55:58 -0800 Subject: [PATCH 04/47] small fix --- .../contrib/void/browser/inlineDiffsService.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts index 78a02e40..1ee8a8a3 100644 --- a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts +++ b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts @@ -1287,7 +1287,7 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest if (block.state === 'done') currStreamingBlockNum = blockNum - if (block.state === 'writingOriginal') + if (block.state === 'writingOriginal') // must be done writing original continue let deltaFinalText: string @@ -1353,16 +1353,12 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest const diffareaid = diffareaidOfBlockNum[blockNum] const diffZone = this.diffAreaOfId[diffareaid] if (diffZone?.type !== 'DiffZone') continue - diffZone._streamState = { isStreaming: false, } this._onDidChangeStreaming.fire({ uri, diffareaid: diffZone.diffareaid }) - - this._refreshStylesAndDiffsInURI(uri) - onFinishEdit() - - // if had error, revert! - if (hadError) this._undoHistory(diffZone._URI) } + this._refreshStylesAndDiffsInURI(uri) + if (hadError) this._undoHistory(uri) + onFinishEdit() } From f7af9c336b09d3bbb812acc1ead6a59ff4fbeb90 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Wed, 12 Feb 2025 21:58:57 -0800 Subject: [PATCH 05/47] tokenIdx --- .../browser/react/src/markdown/ChatMarkdownRender.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx b/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx index c6f88762..8f569074 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx @@ -21,7 +21,7 @@ const COPY_FEEDBACK_TIMEOUT = 1000 // amount of time to say 'Copied!' -type ApplyBoxLocation = ChatMessageLocation & { tokenIdx: number } +type ApplyBoxLocation = ChatMessageLocation & { tokenIdx: string } const getApplyBoxId = ({ threadId, messageIdx, tokenIdx }: ApplyBoxLocation) => { return `${threadId}-${messageIdx}-${tokenIdx}` @@ -97,7 +97,7 @@ export const CodeSpan = ({ children, className }: { children: React.ReactNode, c } -const RenderToken = ({ token, nested = false, noSpace = false, chatMessageLocation: chatLocation, tokenIdx }: { token: Token | string, nested?: boolean, noSpace?: boolean, chatMessageLocation?: ChatMessageLocation, tokenIdx: number }): JSX.Element => { +const RenderToken = ({ token, nested = false, noSpace = false, chatMessageLocation: chatLocation, tokenIdx }: { token: Token | string, nested?: boolean, noSpace?: boolean, chatMessageLocation?: ChatMessageLocation, tokenIdx: string }): JSX.Element => { // deal with built-in tokens first (assume marked token) @@ -206,7 +206,7 @@ const RenderToken = ({ token, nested = false, noSpace = false, chatMessageLocati if (t.type === "paragraph") { const contents = <> {t.tokens.map((token, index) => ( - // assign a unique tokenId to nested components + // assign a unique tokenId to nested components ))} if (nested) return contents @@ -294,7 +294,7 @@ export const ChatMarkdownRender = ({ string, nested = false, noSpace, chatMessag return ( <> {tokens.map((token, index) => ( - + ))} ) From 34b8027fb944fd7330aaaa4e4a102d627f8e06e2 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Thu, 13 Feb 2025 00:08:23 -0800 Subject: [PATCH 06/47] streaming progress --- .../void/browser/inlineDiffsService.ts | 173 +++++++++--------- 1 file changed, 88 insertions(+), 85 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts index 1ee8a8a3..38a1a46f 100644 --- a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts +++ b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts @@ -1264,88 +1264,19 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest let currStreamingBlockNum = 0 let oldBlocks: ExtractedSearchReplaceBlock[] = [] - const onText = ({ newText, fullText }: { newText: string, fullText: string }) => { - - const blocks = extractSearchReplaceBlocks(fullText, { ORIGINAL, DIVIDER, FINAL }) - - // find block.orig in fileContents and return its range in file - const findTextInCode = (text: string, fileContents: string) => { - const idx = fileContents.indexOf(text) - if (idx === -1) return 'Not found' as const - const lastIdx = fileContents.lastIndexOf(text) - if (lastIdx !== idx) return 'Not unique' as const - const startLine = fileContents.substring(0, idx).split('\n').length - const numLines = text.split('\n').length - const endLine = startLine + numLines - 1 - return [startLine, endLine] - } - - - for (let blockNum = currStreamingBlockNum; blockNum < blocks.length; blockNum += 1) { - const block = blocks[blockNum] - - if (block.state === 'done') - currStreamingBlockNum = blockNum - - if (block.state === 'writingOriginal') // must be done writing original - continue - - let deltaFinalText: string - // if should add new diffarea - if (!(blockNum in diffareaidOfBlockNum)) { - const foundInCode = findTextInCode(block.orig, fileContents) - if (typeof foundInCode === 'string') { - console.log('NOT FOUND IN CODE!!!!', foundInCode) - break - } - const [startLine, endLine] = foundInCode - console.log('FOUND!', foundInCode) - - console.log('ADDING', blockNum) - const adding: Omit = { - type: 'DiffZone', - originalCode: block.orig, - startLine, - endLine, - _URI: uri, - _streamState: { - isStreaming: true, - streamRequestIdRef, - line: startLine, - }, - _diffOfId: {}, // added later - _removeStylesFns: new Set(), - } - const diffZone = this._addDiffArea(adding) - this._onDidChangeStreaming.fire({ uri, diffareaid: diffZone.diffareaid }) - this._onDidAddOrDeleteDiffZones.fire({ uri }) - - diffareaidOfBlockNum.push(diffZone.diffareaid) - - latestStreamLocationMutable = { line: diffZone.endLine, addedSplitYet: false, col: 1, originalCodeStartLine: 1 } - - deltaFinalText = block.final - } - else { - deltaFinalText = block.final.substring((oldBlocks[blockNum]?.final ?? '').length, Infinity) - } - - console.log('FULLTEXT', block.final) - oldBlocks = blocks - - // write new text to diffarea - const diffareaid = diffareaidOfBlockNum[blockNum] - const diffZone = this.diffAreaOfId[diffareaid] - if (diffZone?.type !== 'DiffZone') continue - - - if (!latestStreamLocationMutable) continue - this._writeStreamedDiffZoneLLMText(diffZone, block.final, deltaFinalText, latestStreamLocationMutable) - } // end for - - this._refreshStylesAndDiffsInURI(uri) + // find block.orig in fileContents and return its range in file + const findTextInCode = (text: string, fileContents: string) => { + const idx = fileContents.indexOf(text) + if (idx === -1) return 'Not found' as const + const lastIdx = fileContents.lastIndexOf(text) + if (lastIdx !== idx) return 'Not unique' as const + const startLine = fileContents.substring(0, idx).split('\n').length + const numLines = text.split('\n').length + const endLine = startLine + numLines - 1 + return [startLine, endLine] } + const { onFinishEdit } = this._addToHistory(uri) const onDone = (hadError: boolean) => { @@ -1363,7 +1294,6 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest - const { onFinishEdit } = this._addToHistory(uri) // TODO turn this into a service and provide it @@ -1372,14 +1302,87 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest useProviderFor: 'FastApply', logging: { loggingName: `generateSearchAndReplace` }, messages, - onText: ({ newText, fullText }) => { - onText({ newText, fullText }) + onText: ({ fullText }) => { + const blocks = extractSearchReplaceBlocks(fullText, { ORIGINAL, DIVIDER, FINAL }) + + for (let blockNum = currStreamingBlockNum; blockNum < blocks.length; blockNum += 1) { + const block = blocks[blockNum] + + if (block.state === 'done') + currStreamingBlockNum = blockNum + + if (block.state === 'writingOriginal') // must be done writing original + continue + + // if should add new diffarea + if (!(blockNum in diffareaidOfBlockNum)) { + const foundInCode = findTextInCode(block.orig, fileContents) + if (typeof foundInCode === 'string') { + console.log('NOT FOUND IN CODE!!!!', foundInCode) + continue + } + const [startLine, endLine] = foundInCode + console.log('FOUND!', foundInCode) + + console.log('ADDING', blockNum) + const adding: Omit = { + type: 'DiffZone', + originalCode: block.orig, + startLine, + endLine, + _URI: uri, + _streamState: { + isStreaming: true, + streamRequestIdRef, + line: startLine, + }, + _diffOfId: {}, // added later + _removeStylesFns: new Set(), + } + const diffZone = this._addDiffArea(adding) + this._onDidChangeStreaming.fire({ uri, diffareaid: diffZone.diffareaid }) + this._onDidAddOrDeleteDiffZones.fire({ uri }) + + diffareaidOfBlockNum.push(diffZone.diffareaid) + + latestStreamLocationMutable = { line: diffZone.endLine, addedSplitYet: false, col: 1, originalCodeStartLine: 1 } + + + } + const deltaFinalText = block.final.substring((oldBlocks[blockNum]?.final ?? '').length, Infinity) + oldBlocks = blocks + + // write new text to diffarea + const diffareaid = diffareaidOfBlockNum[blockNum] + const diffZone = this.diffAreaOfId[diffareaid] + if (diffZone?.type !== 'DiffZone') continue + + + if (!latestStreamLocationMutable) continue + this._writeStreamedDiffZoneLLMText(diffZone, block.final, deltaFinalText, latestStreamLocationMutable) + } // end for + + this._refreshStylesAndDiffsInURI(uri) }, - onFinalMessage: ({ fullText }) => { + onFinalMessage: async ({ fullText }) => { + // 1. wait 500ms and fix lint errors - call lint error workflow // (update react state to say "Fixing errors") - onDone(false) + const blocks = extractSearchReplaceBlocks(fullText, { ORIGINAL, DIVIDER, FINAL }) + console.log('FULLTEXT', fullText, blocks) + for (let blockNum = 0; blockNum < blocks.length; blockNum += 1) { + const block = blocks[blockNum] + const diffareaid = diffareaidOfBlockNum[blockNum] + const diffZone = this.diffAreaOfId[diffareaid] + if (diffZone?.type !== 'DiffZone') continue + + await this._writeText(uri, block.final, + { startLineNumber: diffZone.startLine, startColumn: 1, endLineNumber: diffZone.endLine, endColumn: Number.MAX_SAFE_INTEGER }, // 1-indexed + { shouldRealignDiffAreas: true } + ) + } + onDone(false) }, onError: (e) => { console.log('ERROR', e); From b01684393a122886374ee6c1aea0f739fb729d9b Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Thu, 13 Feb 2025 00:37:43 -0800 Subject: [PATCH 07/47] streaming works! --- .../contrib/void/browser/inlineDiffsService.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts index 38a1a46f..ead98264 100644 --- a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts +++ b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts @@ -700,7 +700,7 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService { } weAreWriting = false - private async _writeText(uri: URI, text: string, range: IRange, { shouldRealignDiffAreas }: { shouldRealignDiffAreas: boolean }) { + private _writeText(uri: URI, text: string, range: IRange, { shouldRealignDiffAreas }: { shouldRealignDiffAreas: boolean }) { const model = this._getModel(uri) if (!model) return const uriStr = this._readURI(uri, range) @@ -1322,9 +1322,7 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest continue } const [startLine, endLine] = foundInCode - console.log('FOUND!', foundInCode) - console.log('ADDING', blockNum) const adding: Omit = { type: 'DiffZone', originalCode: block.orig, @@ -1345,8 +1343,7 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest diffareaidOfBlockNum.push(diffZone.diffareaid) - latestStreamLocationMutable = { line: diffZone.endLine, addedSplitYet: false, col: 1, originalCodeStartLine: 1 } - + latestStreamLocationMutable = { line: diffZone.startLine, addedSplitYet: false, col: 1, originalCodeStartLine: 1 } } const deltaFinalText = block.final.substring((oldBlocks[blockNum]?.final ?? '').length, Infinity) @@ -1365,11 +1362,9 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest this._refreshStylesAndDiffsInURI(uri) }, onFinalMessage: async ({ fullText }) => { - // 1. wait 500ms and fix lint errors - call lint error workflow // (update react state to say "Fixing errors") const blocks = extractSearchReplaceBlocks(fullText, { ORIGINAL, DIVIDER, FINAL }) - console.log('FULLTEXT', fullText, blocks) for (let blockNum = 0; blockNum < blocks.length; blockNum += 1) { const block = blocks[blockNum] @@ -1377,7 +1372,7 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest const diffZone = this.diffAreaOfId[diffareaid] if (diffZone?.type !== 'DiffZone') continue - await this._writeText(uri, block.final, + this._writeText(uri, block.final, { startLineNumber: diffZone.startLine, startColumn: 1, endLineNumber: diffZone.endLine, endColumn: Number.MAX_SAFE_INTEGER }, // 1-indexed { shouldRealignDiffAreas: true } ) From d9e4679b654cb7fde5b1c4eea0ed817c639d1afe Mon Sep 17 00:00:00 2001 From: Mathew Pareles Date: Thu, 13 Feb 2025 22:58:42 -0800 Subject: [PATCH 08/47] breaking changes - ai regex --- .../contrib/void/browser/aiRegexService.ts | 187 ++++++++++++++++++ .../browser/helpers/extractCodeFromResult.ts | 4 +- .../void/browser/inlineDiffsService.ts | 71 +------ .../contrib/void/browser/prompt/prompts.ts | 126 ++++++++++++ .../void/browser/searchAndReplaceService.ts | 71 ------- .../contrib/void/common/toolsService.ts | 4 +- 6 files changed, 324 insertions(+), 139 deletions(-) create mode 100644 src/vs/workbench/contrib/void/browser/aiRegexService.ts delete mode 100644 src/vs/workbench/contrib/void/browser/searchAndReplaceService.ts diff --git a/src/vs/workbench/contrib/void/browser/aiRegexService.ts b/src/vs/workbench/contrib/void/browser/aiRegexService.ts new file mode 100644 index 00000000..9f96da76 --- /dev/null +++ b/src/vs/workbench/contrib/void/browser/aiRegexService.ts @@ -0,0 +1,187 @@ +/*-------------------------------------------------------------------------------------- + * Copyright 2025 Glass Devtools, Inc. All rights reserved. + * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. + *--------------------------------------------------------------------------------------*/ + +import { Emitter, Event } from '../../../../base/common/event.js'; +import { Disposable } from '../../../../base/common/lifecycle.js'; +import { URI } from '../../../../base/common/uri.js'; +import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js'; +import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js'; +import { IToolService, ToolService } from '../common/toolsService.js'; + + + +export type ChatMessageLocation = { + threadId: string; + messageIdx: number; +} + + +export type SearchAndReplaceBlock = { + search: string; + replace: string; +} + +// service that manages state +export type ApplyState = { + [applyBoxId: string]: { + searchAndReplaceBlocks: SearchAndReplaceBlock; + } +} + +// the purpose of this service is to generate search and replace blocks for a given codeblock `codeblockId` and on a file `fileName` and version `fileVersion` + +export interface IFastApplyService { + readonly _serviceBrand: undefined; + + // readonly state: ApplyState; // readonly to the user + // setState(newState: Partial): void; + // onDidChangeState: Event; +} + +export const IVoidFastApplyService = createDecorator('voidFastApplyService'); +class VoidFastApplyService extends Disposable implements IFastApplyService { + _serviceBrand: undefined; + + static readonly ID = 'voidFastApplyService'; + + private readonly _onDidChangeState = new Emitter(); + readonly onDidChangeState: Event = this._onDidChangeState.event; + + + // state + // state: ApplyState + + constructor( + @IToolService private readonly toolService: ToolService + ) { + super() + + // initial state + // this.state = { currentUri: undefined } + } + + setState(newState: Partial) { + + // this.state = { ...this.state, ...newState } + this._onDidChangeState.fire() + } + + aiSearch(searchStr: string) { + + } + + aiReplace(searchStr: string, replaceStr: string) { + + } + + + // 1. search(ai) + // - tool use to find all possible changes + // - if search only: is this file related to the search? + // - if search + replace: should I modify this file? + // 2. replace(ai) + // - what changes to make? + // 3. postprocess errors + // -fastapply changes simultaneously + // -iterate on syntax errors (all files can be changed from a syntax error, not just the one with the error) + + + private async _searchUsingAI({ searchClause }: { searchClause: string }) { + + const relevantURIs: URI[] = [] + const gatherPrompt = `\ +asdasdas +` + const filterPrompt = `\ +Is this file relevant? +` + + + // optimizations (DO THESE LATER!!!!!!) + // if tool includes a uri in uriSet, skip it obviously + let uriSet = new Set() + // gather + let messages = [] + while (true) { + const result = await new Promise((res, rej) => { + sendLLMMessage({ + messages, + tools: ['search'], + onFinalMessage: ({ result: r, }) => { + res(r) + }, + onError: (error) => { + rej(error) + } + }) + }) + + messages.push({ role: 'tool', content: turnToString(result) }) + + sendLLMMessage({ + messages: { 'Output ': result }, + onFinalMessage: (r) => { + // output is file1\nfile2\nfile3\n... + } + }) + + uriSet.add(...) + } + + // writes + if (!replaceClause) return + + for (const uri of uriSet) { + // in future, batch these + applyWorkflow({ uri, applyStr: replaceClause }) + } + + + + + + + // while (true) { + // const result = new Promise((res, rej) => { + // sendLLMMessage({ + // messages, + // tools: ['search'], + // onResult: (r) => { + // res(r) + // } + // }) + // }) + + // messages.push(result) + + // } + + + } + + + private async _replaceUsingAI({ searchClause, replaceClause, relevantURIs }: { searchClause: string, replaceClause: string, relevantURIs: URI[] }) { + + for (const uri of relevantURIs) { + + uri + + } + + + + // should I change this file? + // if so what changes to make? + + + + // fast apply the changes + } + + + +} + +registerSingleton(IVoidFastApplyService, VoidFastApplyService, InstantiationType.Eager); diff --git a/src/vs/workbench/contrib/void/browser/helpers/extractCodeFromResult.ts b/src/vs/workbench/contrib/void/browser/helpers/extractCodeFromResult.ts index a0addcae..bb1ed350 100644 --- a/src/vs/workbench/contrib/void/browser/helpers/extractCodeFromResult.ts +++ b/src/vs/workbench/contrib/void/browser/helpers/extractCodeFromResult.ts @@ -3,6 +3,8 @@ * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. *--------------------------------------------------------------------------------------*/ +import { DIVIDER, FINAL, ORIGINAL } from '../prompt/prompts' + class SurroundingsRemover { readonly originalS: string i: number @@ -195,7 +197,7 @@ const endsWithAnyPrefixOf = (str: string, anyPrefix: string) => { } // guarantees if you keep adding text, array length will strictly grow and state will progress without going back -export const extractSearchReplaceBlocks = (str: string, { ORIGINAL, DIVIDER, FINAL }: { ORIGINAL: string, DIVIDER: string, FINAL: string }) => { +export const extractSearchReplaceBlocks = (str: string) => { const ORIGINAL_ = ORIGINAL + `\n` const DIVIDER_ = '\n' + DIVIDER + `\n` diff --git a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts index ead98264..ed436b61 100644 --- a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts +++ b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts @@ -25,7 +25,7 @@ import * as dom from '../../../../base/browser/dom.js'; import { Widget } from '../../../../base/browser/ui/widget.js'; import { URI } from '../../../../base/common/uri.js'; import { IConsistentEditorItemService, IConsistentItemService } from './helperServices/consistentItemService.js'; -import { voidPrefixAndSuffix, ctrlKStream_userMessage, ctrlKStream_systemMessage, fastApply_rewritewholething_userMessage, fastApply_rewritewholething_systemMessage, defaultQuickEditFimTags, tripleTick } from './prompt/prompts.js'; +import { voidPrefixAndSuffix, ctrlKStream_userMessage, ctrlKStream_systemMessage, fastApply_rewritewholething_userMessage, fastApply_rewritewholething_systemMessage, defaultQuickEditFimTags, searchReplace_userMessage, searchReplace_systemMessage } from './prompt/prompts.js'; import { mountCtrlK } from '../browser/react/out/quick-edit-tsx/index.js' import { QuickEditPropsType } from './quickEditActions.js'; @@ -1173,59 +1173,7 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService { private async _initializeSearchAndReplaceStream({ applyStr }: { applyStr: string }) { - const ORIGINAL = `<<<<<<< ORIGINAL` - const DIVIDER = `=======` - const FINAL = `>>>>>>> UPDATED` - const searchReplaceSysMessage = `\ -You are a coding assistant that generates SEARCH/REPLACE code blocks that will be used to edit a file. - -A SEARCH/REPLACE block describes the code before and after a change. Here is the format: -${ORIGINAL} -// ... original code goes here -${DIVIDER} -// ... final code goes here -${FINAL} - -You will be given the original file \`ORIGINAL_FILE\` and a description of a change \`CHANGE\` to make. -Output SEARCH/REPLACE blocks to edit the file according to the desired change. You may output multiple SEARCH/REPLACE blocks. - -Directions: -1. Your OUTPUT should consist ONLY of SEARCH/REPLACE blocks. Do NOT output any text or explanations before or after this. -2. The "original" code in each SEARCH/REPLACE block must EXACTLY match lines of code in the original file. -3. The "original" code in each SEARCH/REPLACE block should include enough text to uniquely identify the change in the file. -4. The SEARCH/REPLACE blocks you generate will be applied immediately, and so they **MUST** produce a file that the user can run IMMEDIATELY. - - Make sure you add all necessary imports. - - Make sure the "final" code is complete and will not result in syntax/lint errors. -5. Follow coding convention (spaces, semilcolons, comments, etc). - -## EXAMPLE 1 -ORIGINAL_FILE -${tripleTick[0]} -let w = 5 -let x = 6 -let y = 7 -let z = 8 -${tripleTick[1]} - -CHANGE -Make x equal to 6.5, not 6. -${tripleTick[0]} -// ... existing code -let x = 6.5 -// ... existing code -${tripleTick[1]} - - -## ACCEPTED OUTPUT -${tripleTick[0]} -${ORIGINAL} -let x = 6 -${DIVIDER} -let x = 6.5 -${FINAL} -${tripleTick[1]} -` const uri_ = this._getActiveEditorURI() if (!uri_) return @@ -1236,23 +1184,14 @@ ${tripleTick[1]} if (fileContents === null) return - const searchReplaceUserMessage = ({ originalCode, applyStr }: { originalCode: string, applyStr: string }) => `\ -ORIGINAL_FILE -${originalCode} -CHANGE -${applyStr} - -INSTRUCTIONS -Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggested SEARCH/REPLACE blocks, without any explanation. -` // reject all diffZones on this URI, adding to history (there can't possibly be overlap after this) this.removeDiffAreas({ uri, behavior: 'reject', removeCtrlKs: true }) - const userMessageContent = searchReplaceUserMessage({ originalCode: fileContents, applyStr: applyStr }) + const userMessageContent = searchReplace_userMessage({ originalCode: fileContents, applyStr: applyStr }) const messages: LLMChatMessage[] = [ - { role: 'system', content: searchReplaceSysMessage }, + { role: 'system', content: searchReplace_systemMessage }, { role: 'user', content: userMessageContent } ] let streamRequestIdRef: { current: string | null } = { current: null } @@ -1303,7 +1242,7 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest logging: { loggingName: `generateSearchAndReplace` }, messages, onText: ({ fullText }) => { - const blocks = extractSearchReplaceBlocks(fullText, { ORIGINAL, DIVIDER, FINAL }) + const blocks = extractSearchReplaceBlocks(fullText) for (let blockNum = currStreamingBlockNum; blockNum < blocks.length; blockNum += 1) { const block = blocks[blockNum] @@ -1364,7 +1303,7 @@ Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggest onFinalMessage: async ({ fullText }) => { // 1. wait 500ms and fix lint errors - call lint error workflow // (update react state to say "Fixing errors") - const blocks = extractSearchReplaceBlocks(fullText, { ORIGINAL, DIVIDER, FINAL }) + const blocks = extractSearchReplaceBlocks(fullText) for (let blockNum = 0; blockNum < blocks.length; blockNum += 1) { const block = blocks[blockNum] diff --git a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts index 428625fd..9185e0dc 100644 --- a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts +++ b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts @@ -224,7 +224,133 @@ Please finish writing the new file by applying the change to the original file. +const aiRegex_computeReplacementsForFile_systemMessage = `\ +You are a "search and replace" coding assistant. +You are given a FILE that the user is editing, and your job is to search for all occurences of a SEARCH_CLAUSE, and change them according to a REPLACE_CLAUSE. + +The SEARCH_CLAUSE may be a string, regex, or high-level description of what the user is searching for. + +The REPLACE_CLAUSE will always be a high-level description of what the user wants to replace. + +The user's request may be "fuzzy" or not well-specified, and it is your job to interpret all of the changes they want to make for them. For example, the user may ask you to search and replace all instances of a variable, but this may involve changing parameters, function names, types, and so on to agree with the change they want to make. Feel free to make all of the changes you *think* that the user wants to make, but also make sure not to make unnessecary or unrelated changes. + +## Instructions + +1. If you do not want to make any changes, you should respond with the word "no". + +2. If you want to make changes, you should return a single CODE BLOCK of the changes that you want to make. +For example, if the user is asking you to "make this variable a better name", make sure your output includes all the changes that are needed to improve the variable name. + - Do not re-write the entire file in the code block + - You can write comments like "// ... existing code" to indicate existing code + - Make sure you give enough context in the code block to apply the changes to the correct location in the code` + + +const aiRegex_computeReplacementsForFile_userMessage = async ({ searchClause, replaceClause, fileURI, modelService }: { searchClause: string, replaceClause: string, fileURI: URI, modelService: IModelService }) => { + + // we may want to do this in batches + const fileSelection: FileSelection = { type: 'File', fileURI, selectionStr: null, range: null } + + const file = await stringifyFileSelections([fileSelection], modelService) + + return `\ +## FILE +${file} + +## SEARCH_CLAUSE +Here is what the user is searching for: +${searchClause} + +## REPLACE_CLAUSE +Here is what the user wants to replace it with: +${replaceClause} + +## INSTRUCTIONS +Please return the changes you want to make to the file in a codeblock, or return "no" if you do not want to make changes.` +} + + + + +// don't have to tell it it will be given the history; just give it to it +const aiRegex_search_systemMessage = `\ +You are a coding assistant that executes the SEARCH part of a user's search and replace query. + +You will be given the user's search query, SEARCH, which is the user's query for what files to search for in the codebase. You may also be given the user's REPLACE query for additional context. + +Output +- Regex query +- Files to Include (optional) +- Files to Exclude? (optional) + +` + + + +export const ORIGINAL = `<<<<<<< ORIGINAL` +export const DIVIDER = `=======` +export const FINAL = `>>>>>>> UPDATED` + +export const searchReplace_systemMessage = `\ +You are a coding assistant that generates SEARCH/REPLACE code blocks that will be used to edit a file. + +A SEARCH/REPLACE block describes the code before and after a change. Here is the format: +${ORIGINAL} +// ... original code goes here +${DIVIDER} +// ... final code goes here +${FINAL} + +You will be given the original file \`ORIGINAL_FILE\` and a description of a change \`CHANGE\` to make. +Output SEARCH/REPLACE blocks to edit the file according to the desired change. You may output multiple SEARCH/REPLACE blocks. + +Directions: +1. Your OUTPUT should consist ONLY of SEARCH/REPLACE blocks. Do NOT output any text or explanations before or after this. +2. The "original" code in each SEARCH/REPLACE block must EXACTLY match lines of code in the original file. +3. The "original" code in each SEARCH/REPLACE block should include enough text to uniquely identify the change in the file. +4. The SEARCH/REPLACE blocks you generate will be applied immediately, and so they **MUST** produce a file that the user can run IMMEDIATELY. +- Make sure you add all necessary imports. +- Make sure the "final" code is complete and will not result in syntax/lint errors. +5. Follow coding convention (spaces, semilcolons, comments, etc). + +## EXAMPLE 1 +ORIGINAL_FILE +${tripleTick[0]} +let w = 5 +let x = 6 +let y = 7 +let z = 8 +${tripleTick[1]} + +CHANGE +Make x equal to 6.5, not 6. +${tripleTick[0]} +// ... existing code +let x = 6.5 +// ... existing code +${tripleTick[1]} + + +## ACCEPTED OUTPUT +${tripleTick[0]} +${ORIGINAL} +let x = 6 +${DIVIDER} +let x = 6.5 +${FINAL} +${tripleTick[1]} +` + +export const searchReplace_userMessage = ({ originalCode, applyStr }: { originalCode: string, applyStr: string }) => `\ +ORIGINAL_FILE +${originalCode} + +CHANGE +${applyStr} + +INSTRUCTIONS +Please output SEARCH/REPLACE blocks to make the change. Return ONLY your suggested SEARCH/REPLACE blocks, without any explanation. +` diff --git a/src/vs/workbench/contrib/void/browser/searchAndReplaceService.ts b/src/vs/workbench/contrib/void/browser/searchAndReplaceService.ts deleted file mode 100644 index c668ae26..00000000 --- a/src/vs/workbench/contrib/void/browser/searchAndReplaceService.ts +++ /dev/null @@ -1,71 +0,0 @@ -/*-------------------------------------------------------------------------------------- - * Copyright 2025 Glass Devtools, Inc. All rights reserved. - * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. - *--------------------------------------------------------------------------------------*/ - -import { Emitter, Event } from '../../../../base/common/event.js'; -import { Disposable } from '../../../../base/common/lifecycle.js'; -import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js'; -import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js'; - - - -export type ChatMessageLocation = { - threadId: string; - messageIdx: number; -} - - -export type SearchAndReplaceBlock = { - search: string; - replace: string; -} - -// service that manages state -export type ApplyState = { - [applyBoxId: string]: { - searchAndReplaceBlocks: SearchAndReplaceBlock; - } -} - -// the purpose of this service is to generate search and replace blocks for a given codeblock `codeblockId` and on a file `fileName` and version `fileVersion` - -export interface IFastApplyService { - readonly _serviceBrand: undefined; - - // readonly state: ApplyState; // readonly to the user - // setState(newState: Partial): void; - // onDidChangeState: Event; -} - -export const IVoidFastApplyService = createDecorator('voidFastApplyService'); -class VoidFastApplyService extends Disposable implements IFastApplyService { - _serviceBrand: undefined; - - static readonly ID = 'voidFastApplyService'; - - private readonly _onDidChangeState = new Emitter(); - readonly onDidChangeState: Event = this._onDidChangeState.event; - - - // state - // state: ApplyState - - constructor( - ) { - super() - - // initial state - // this.state = { currentUri: undefined } - } - - setState(newState: Partial) { - - // this.state = { ...this.state, ...newState } - this._onDidChangeState.fire() - } - - -} - -registerSingleton(IVoidFastApplyService, VoidFastApplyService, InstantiationType.Eager); diff --git a/src/vs/workbench/contrib/void/common/toolsService.ts b/src/vs/workbench/contrib/void/common/toolsService.ts index 8ffd6b9b..1eda93d1 100644 --- a/src/vs/workbench/contrib/void/common/toolsService.ts +++ b/src/vs/workbench/contrib/void/common/toolsService.ts @@ -161,7 +161,7 @@ export class ToolService implements IToolService { const query = queryBuilder.text({ pattern: queryStr, }, workspaceContextService.getWorkspace().folders.map(f => f.uri)); const data = await searchService.textSearch(query, CancellationToken.None); - const str = data.results.map(({ resource, results }) => resource.fsPath).join('\n') + const str = data.results.map(({ resource, results }) => resource) return str }, @@ -171,6 +171,8 @@ export class ToolService implements IToolService { } + + callContextTool: IToolService['callContextTool'] = (toolName, params) => { return this.contextToolCallFns[toolName](params) } From 343ee5eb9483993b163cb1310717dcca2e465ebd Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Thu, 13 Feb 2025 22:59:59 -0800 Subject: [PATCH 09/47] Apply name --- .../workbench/contrib/void/browser/inlineDiffsService.ts | 4 ++-- .../workbench/contrib/void/common/voidSettingsService.ts | 4 ++-- src/vs/workbench/contrib/void/common/voidSettingsTypes.ts | 8 +++++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts index ed436b61..f8449720 100644 --- a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts +++ b/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts @@ -1238,7 +1238,7 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService { // TODO turn this into a service and provide it streamRequestIdRef.current = this._llmMessageService.sendLLMMessage({ messagesType: 'chatMessages', - useProviderFor: 'FastApply', + useProviderFor: 'Apply', logging: { loggingName: `generateSearchAndReplace` }, messages, onText: ({ fullText }) => { @@ -1478,7 +1478,7 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService { streamRequestIdRef.current = this._llmMessageService.sendLLMMessage({ messagesType: 'chatMessages', - useProviderFor: opts.from === 'ClickApply' ? 'FastApply' : 'Ctrl+K', + useProviderFor: opts.from === 'ClickApply' ? 'Apply' : 'Ctrl+K', logging: { loggingName: `startApplying - ${from}` }, messages, onText: ({ newText: newText_ }) => { diff --git a/src/vs/workbench/contrib/void/common/voidSettingsService.ts b/src/vs/workbench/contrib/void/common/voidSettingsService.ts index 4322eaf4..59089230 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsService.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsService.ts @@ -131,7 +131,7 @@ const _updatedValidatedState = (state: Omit) const defaultState = () => { const d: VoidSettingsState = { settingsOfProvider: deepClone(defaultSettingsOfProvider), - modelSelectionOfFeature: { 'Ctrl+L': null, 'Ctrl+K': null, 'Autocomplete': null, 'FastApply': null }, + modelSelectionOfFeature: { 'Ctrl+L': null, 'Ctrl+K': null, 'Autocomplete': null, 'Apply': null }, globalSettings: deepClone(defaultGlobalSettings), _modelOptions: [], // computed later } @@ -189,7 +189,7 @@ class VoidSettingsService extends Disposable implements IVoidSettingsService { const newModelSelectionOfFeature = { // A HACK BECAUSE WE ADDED FastApply - ...{ 'FastApply': null }, + ...{ 'Apply': null }, ...readS.modelSelectionOfFeature, } diff --git a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts index 0a5bdc64..a31eb771 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts @@ -436,18 +436,20 @@ export const modelSelectionsEqual = (m1: ModelSelection, m2: ModelSelection) => } // this is a state -export const featureNames = ['Ctrl+L', 'Ctrl+K', 'Autocomplete', 'FastApply'] as const +export const featureNames = ['Ctrl+L', 'Ctrl+K', 'Autocomplete', 'Apply'] as const export type ModelSelectionOfFeature = Record<(typeof featureNames)[number], ModelSelection | null> export type FeatureName = keyof ModelSelectionOfFeature export const displayInfoOfFeatureName = (featureName: FeatureName) => { + // editor: if (featureName === 'Autocomplete') return 'Autocomplete' else if (featureName === 'Ctrl+K') - return 'Quick-Edit' + return 'Quick Edit' + // sidebar: else if (featureName === 'Ctrl+L') return 'Chat' - else if (featureName === 'FastApply') + else if (featureName === 'Apply') return 'Apply' else throw new Error(`Feature Name ${featureName} not allowed`) From 0bcd88dad6fd7e28d52b69a291d7b19132ebcff3 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Thu, 13 Feb 2025 23:54:22 -0800 Subject: [PATCH 10/47] fix --- .../contrib/void/browser/aiRegexService.ts | 130 +++++++++--------- .../browser/helpers/extractCodeFromResult.ts | 2 +- .../contrib/void/browser/prompt/prompts.ts | 6 +- .../contrib/void/common/toolsService.ts | 2 +- .../electron-main/llmMessage/anthropic.ts | 32 ++++- .../void/electron-main/llmMessage/openai.ts | 13 +- 6 files changed, 110 insertions(+), 75 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/aiRegexService.ts b/src/vs/workbench/contrib/void/browser/aiRegexService.ts index 9f96da76..c0ae27fa 100644 --- a/src/vs/workbench/contrib/void/browser/aiRegexService.ts +++ b/src/vs/workbench/contrib/void/browser/aiRegexService.ts @@ -5,10 +5,10 @@ import { Emitter, Event } from '../../../../base/common/event.js'; import { Disposable } from '../../../../base/common/lifecycle.js'; -import { URI } from '../../../../base/common/uri.js'; +// import { URI } from '../../../../base/common/uri.js'; import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js'; import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js'; -import { IToolService, ToolService } from '../common/toolsService.js'; +// import { IToolService, ToolService } from '../common/toolsService.js'; @@ -54,7 +54,7 @@ class VoidFastApplyService extends Disposable implements IFastApplyService { // state: ApplyState constructor( - @IToolService private readonly toolService: ToolService + // @IToolService private readonly toolService: ToolService ) { super() @@ -88,97 +88,97 @@ class VoidFastApplyService extends Disposable implements IFastApplyService { // -iterate on syntax errors (all files can be changed from a syntax error, not just the one with the error) - private async _searchUsingAI({ searchClause }: { searchClause: string }) { + // private async _searchUsingAI({ searchClause }: { searchClause: string }) { - const relevantURIs: URI[] = [] - const gatherPrompt = `\ -asdasdas -` - const filterPrompt = `\ -Is this file relevant? -` + // // const relevantURIs: URI[] = [] + // // const gatherPrompt = `\ + // // asdasdas + // // ` + // // const filterPrompt = `\ + // // Is this file relevant? + // // ` - // optimizations (DO THESE LATER!!!!!!) - // if tool includes a uri in uriSet, skip it obviously - let uriSet = new Set() - // gather - let messages = [] - while (true) { - const result = await new Promise((res, rej) => { - sendLLMMessage({ - messages, - tools: ['search'], - onFinalMessage: ({ result: r, }) => { - res(r) - }, - onError: (error) => { - rej(error) - } - }) - }) + // // // optimizations (DO THESE LATER!!!!!!) + // // // if tool includes a uri in uriSet, skip it obviously + // // let uriSet = new Set() + // // // gather + // // let messages = [] + // // while (true) { + // // const result = await new Promise((res, rej) => { + // // sendLLMMessage({ + // // messages, + // // tools: ['search'], + // // onFinalMessage: ({ result: r, }) => { + // // res(r) + // // }, + // // onError: (error) => { + // // rej(error) + // // } + // // }) + // // }) - messages.push({ role: 'tool', content: turnToString(result) }) + // // messages.push({ role: 'tool', content: turnToString(result) }) - sendLLMMessage({ - messages: { 'Output ': result }, - onFinalMessage: (r) => { - // output is file1\nfile2\nfile3\n... - } - }) + // // sendLLMMessage({ + // // messages: { 'Output ': result }, + // // onFinalMessage: (r) => { + // // // output is file1\nfile2\nfile3\n... + // // } + // // }) - uriSet.add(...) - } + // // uriSet.add(...) + // // } - // writes - if (!replaceClause) return + // // // writes + // // if (!replaceClause) return - for (const uri of uriSet) { - // in future, batch these - applyWorkflow({ uri, applyStr: replaceClause }) - } + // // for (const uri of uriSet) { + // // // in future, batch these + // // applyWorkflow({ uri, applyStr: replaceClause }) + // // } - // while (true) { - // const result = new Promise((res, rej) => { - // sendLLMMessage({ - // messages, - // tools: ['search'], - // onResult: (r) => { - // res(r) - // } - // }) - // }) + // // while (true) { + // // const result = new Promise((res, rej) => { + // // sendLLMMessage({ + // // messages, + // // tools: ['search'], + // // onResult: (r) => { + // // res(r) + // // } + // // }) + // // }) - // messages.push(result) + // // messages.push(result) - // } + // // } - } + // } - private async _replaceUsingAI({ searchClause, replaceClause, relevantURIs }: { searchClause: string, replaceClause: string, relevantURIs: URI[] }) { + // private async _replaceUsingAI({ searchClause, replaceClause, relevantURIs }: { searchClause: string, replaceClause: string, relevantURIs: URI[] }) { - for (const uri of relevantURIs) { + // for (const uri of relevantURIs) { - uri + // uri - } + // } - // should I change this file? - // if so what changes to make? + // // should I change this file? + // // if so what changes to make? - // fast apply the changes - } + // // fast apply the changes + // } diff --git a/src/vs/workbench/contrib/void/browser/helpers/extractCodeFromResult.ts b/src/vs/workbench/contrib/void/browser/helpers/extractCodeFromResult.ts index bb1ed350..b7665eca 100644 --- a/src/vs/workbench/contrib/void/browser/helpers/extractCodeFromResult.ts +++ b/src/vs/workbench/contrib/void/browser/helpers/extractCodeFromResult.ts @@ -3,7 +3,7 @@ * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. *--------------------------------------------------------------------------------------*/ -import { DIVIDER, FINAL, ORIGINAL } from '../prompt/prompts' +import { DIVIDER, FINAL, ORIGINAL } from '../prompt/prompts.js' class SurroundingsRemover { readonly originalS: string diff --git a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts index 9185e0dc..45a573ae 100644 --- a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts +++ b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts @@ -224,7 +224,7 @@ Please finish writing the new file by applying the change to the original file. -const aiRegex_computeReplacementsForFile_systemMessage = `\ +export const aiRegex_computeReplacementsForFile_systemMessage = `\ You are a "search and replace" coding assistant. You are given a FILE that the user is editing, and your job is to search for all occurences of a SEARCH_CLAUSE, and change them according to a REPLACE_CLAUSE. @@ -246,7 +246,7 @@ For example, if the user is asking you to "make this variable a better name", ma - Make sure you give enough context in the code block to apply the changes to the correct location in the code` -const aiRegex_computeReplacementsForFile_userMessage = async ({ searchClause, replaceClause, fileURI, modelService }: { searchClause: string, replaceClause: string, fileURI: URI, modelService: IModelService }) => { +export const aiRegex_computeReplacementsForFile_userMessage = async ({ searchClause, replaceClause, fileURI, modelService }: { searchClause: string, replaceClause: string, fileURI: URI, modelService: IModelService }) => { // we may want to do this in batches const fileSelection: FileSelection = { type: 'File', fileURI, selectionStr: null, range: null } @@ -273,7 +273,7 @@ Please return the changes you want to make to the file in a codeblock, or return // don't have to tell it it will be given the history; just give it to it -const aiRegex_search_systemMessage = `\ +export const aiRegex_search_systemMessage = `\ You are a coding assistant that executes the SEARCH part of a user's search and replace query. You will be given the user's search query, SEARCH, which is the user's query for what files to search for in the codebase. You may also be given the user's REPLACE query for additional context. diff --git a/src/vs/workbench/contrib/void/common/toolsService.ts b/src/vs/workbench/contrib/void/common/toolsService.ts index 1eda93d1..c8b8bb2d 100644 --- a/src/vs/workbench/contrib/void/common/toolsService.ts +++ b/src/vs/workbench/contrib/void/common/toolsService.ts @@ -162,7 +162,7 @@ export class ToolService implements IToolService { const data = await searchService.textSearch(query, CancellationToken.None); const str = data.results.map(({ resource, results }) => resource) - return str + return str as any }, } diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts index 91461b16..97cd3ed9 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts @@ -53,11 +53,37 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages, onText({ newText, fullText }) }) + + // can do tool use streaming + const toolCallOfIndex: { [index: string]: { name: string, args: string } } = {} + stream.on('streamEvent', e => { + if (e.type === 'content_block_start') { + if (e.content_block.type !== 'tool_use') return + const index = e.index + const tool = e.content_block + if (!toolCallOfIndex[index]) + toolCallOfIndex[index] = { name: '', args: '' } + toolCallOfIndex[index].name += tool.name ?? '' + toolCallOfIndex[index].args += tool.input ?? '' + + } + else if (e.type === 'content_block_delta') { + if (e.delta.type !== 'input_json_delta') return + toolCallOfIndex[e.index].args += e.delta.partial_json + } + // TODO!!!!! + // onText({}) + }) + // when we get the final message on this stream (or when error/fail) - stream.on('finalMessage', (claude_response) => { + stream.on('finalMessage', (response) => { // stringify the response's content - const content = claude_response.content.map(c => c.type === 'text' ? c.text : c.type).join('\n'); - onFinalMessage({ fullText: content }) + const content = response.content.map(c => c.type === 'text' ? c.text : '').join('\n') + const tools = response.content.map(c => c.type === 'tool_use' ? { name: c.name, input: c.input } : null) + + console.log("TOOLS!!!!", typeof tools[0]?.input, JSON.stringify(tools, null, 2)) + + onFinalMessage({ fullText: content, }) }) stream.on('error', (error) => { diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts index df4d2322..30e80bc6 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts @@ -121,6 +121,7 @@ export const sendOpenAIFIM: _InternalSendLLMFIMMessageFnType = ({ messages, onTe export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName }) => { let fullText = '' + const toolCallOfIndex: { [index: string]: { name: string, args: string } } = {} const openai: OpenAI = newOpenAI({ providerName, settingsOfProvider }) const options: OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming = { @@ -137,11 +138,19 @@ export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages, on // when receive text for await (const chunk of response) { + // tool call + for (const tool of chunk.choices[0]?.delta?.tool_calls ?? []) { + const index = tool.index + if (!toolCallOfIndex[index]) toolCallOfIndex[index] = { name: '', args: '' } + toolCallOfIndex[index].name += tool.function?.name ?? '' + toolCallOfIndex[index].args += tool.function?.arguments ?? '' + } + + // message let newText = '' - newText += chunk.choices[0]?.delta?.tool_calls?.[0]?.function?.name ?? '' - newText += chunk.choices[0]?.delta?.tool_calls?.[0]?.function?.arguments ?? '' newText += chunk.choices[0]?.delta?.content ?? '' fullText += newText; + onText({ newText, fullText }); } onFinalMessage({ fullText }); From 9cfcf396c1bd51a9b68e30d29b2d88a78243853f Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Fri, 14 Feb 2025 01:05:10 -0800 Subject: [PATCH 11/47] update types for tool support --- .../contrib/void/common/llmMessageTypes.ts | 5 ++ .../contrib/void/common/toolsService.ts | 56 ++++++++----------- .../electron-main/llmMessage/anthropic.ts | 18 +++--- .../void/electron-main/llmMessage/ollama.ts | 5 ++ .../void/electron-main/llmMessage/openai.ts | 6 +- .../llmMessage/sendLLMMessage.ts | 15 ++--- 6 files changed, 53 insertions(+), 52 deletions(-) diff --git a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts index fb6a94fc..40ab1f2a 100644 --- a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts +++ b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts @@ -3,6 +3,7 @@ * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. *--------------------------------------------------------------------------------------*/ +import { InternalToolInfo } from './toolsService.js' import { FeatureName, ProviderName, SettingsOfProvider } from './voidSettingsTypes.js' @@ -44,9 +45,11 @@ type _InternalSendFIMMessage = { type SendLLMType = { messagesType: 'chatMessages'; messages: LLMChatMessage[]; + tools?: InternalToolInfo[]; } | { messagesType: 'FIMMessage'; messages: _InternalSendFIMMessage; + tools?: undefined; } // service types @@ -96,6 +99,8 @@ export type _InternalSendLLMChatMessageFnType = ( modelName: string; _setAborter: (aborter: () => void) => void; + tools?: InternalToolInfo[], + messages: _InternalLLMChatMessage[]; } ) => void diff --git a/src/vs/workbench/contrib/void/common/toolsService.ts b/src/vs/workbench/contrib/void/common/toolsService.ts index c8b8bb2d..86cc4356 100644 --- a/src/vs/workbench/contrib/void/common/toolsService.ts +++ b/src/vs/workbench/contrib/void/common/toolsService.ts @@ -15,6 +15,7 @@ import { ISearchService } from '../../../../workbench/services/search/common/sea // we do this using Anthropic's style and convert to OpenAI style later export type InternalToolInfo = { + name: string, description: string, params: { [paramName: string]: { type: string, description: string | undefined } // name -> type @@ -23,13 +24,14 @@ export type InternalToolInfo = { } // helper -const pagination = { +const paginationHelper = { desc: `Very large results may be paginated (indicated in the result). Pagination fails gracefully if out of bounds or invalid page number.`, param: { pageNumber: { type: 'number', description: 'The page number (optional, default is 1).' }, } } as const export const contextTools = { read_file: { + name: 'read_file', description: 'Returns file contents of a given URI.', params: { uri: { type: 'string', description: undefined }, @@ -38,28 +40,31 @@ export const contextTools = { }, list_dir: { - description: `Returns all file names and folder names in a given URI. ${pagination.desc}`, + name: 'list_dir', + description: `Returns all file names and folder names in a given URI. ${paginationHelper.desc}`, params: { uri: { type: 'string', description: undefined }, - ...pagination.param + ...paginationHelper.param }, required: ['uri'], }, pathname_search: { - description: `Returns all pathnames that match a given grep query. You should use this when looking for a file with a specific name or path. This does NOT search file content. ${pagination.desc}`, + name: 'pathname_search', + description: `Returns all pathnames that match a given grep query. You should use this when looking for a file with a specific name or path. This does NOT search file content. ${paginationHelper.desc}`, params: { query: { type: 'string', description: undefined }, - ...pagination.param, + ...paginationHelper.param, }, required: ['query'] }, search: { - description: `Returns all code excerpts containing the given string or grep query. This does NOT search pathname. As a follow-up, you may want to use read_file to view the full file contents of the results. ${pagination.desc}`, + name: 'search', + description: `Returns all code excerpts containing the given string or grep query. This does NOT search pathname. As a follow-up, you may want to use read_file to view the full file contents of the results. ${paginationHelper.desc}`, params: { query: { type: 'string', description: undefined }, - ...pagination.param, + ...paginationHelper.param, }, required: ['query'], }, @@ -69,26 +74,18 @@ export const contextTools = { // // RAG // }, -} as const satisfies { [name: string]: InternalToolInfo } +} export type ContextToolName = keyof typeof contextTools type ContextToolParamNames = keyof typeof contextTools[T]['params'] type ContextToolParams = { [paramName in ContextToolParamNames]: unknown } -type AllContextToolCallFns = { - [ToolName in ContextToolName]: ((p: (ContextToolParams)) => Promise) -} - -// TODO check to make sure in workspace -// TODO check to make sure is not gitignored - - async function generateDirectoryTreeMd(fileService: IFileService, rootURI: URI): Promise { let output = '' function traverseChildren(children: IFileStat[], depth: number) { @@ -116,7 +113,6 @@ const validateURI = (uriStr: unknown) => { export interface IToolService { readonly _serviceBrand: undefined; - callContextTool: (toolName: T, params: ContextToolParams) => Promise } export const IToolService = createDecorator('ToolService'); @@ -125,7 +121,7 @@ export class ToolService implements IToolService { readonly _serviceBrand: undefined; - contextToolCallFns: AllContextToolCallFns + public contextToolCallFns constructor( @IFileService fileService: IFileService, @@ -138,31 +134,33 @@ export class ToolService implements IToolService { const queryBuilder = instantiationService.createInstance(QueryBuilder); this.contextToolCallFns = { - read_file: async ({ uri: uriStr }) => { + read_file: async ({ uri: uriStr }: ContextToolParams<'read_file'>) => { const uri = validateURI(uriStr) const fileContents = await VSReadFileRaw(fileService, uri) return fileContents ?? '(could not read file)' }, - list_dir: async ({ uri: uriStr }) => { + list_dir: async ({ uri: uriStr }: ContextToolParams<'list_dir'>) => { const uri = validateURI(uriStr) + // TODO!!!! check to make sure in workspace + // TODO check to make sure is not gitignored const treeStr = await generateDirectoryTreeMd(fileService, uri) return treeStr }, - pathname_search: async ({ query: queryStr }) => { + pathname_search: async ({ query: queryStr }: ContextToolParams<'pathname_search'>) => { if (typeof queryStr !== 'string') return '(Error: query was not a string)' const query = queryBuilder.file(workspaceContextService.getWorkspace().folders.map(f => f.uri), { filePattern: queryStr, }); const data = await searchService.fileSearch(query, CancellationToken.None); - const str = data.results.map(({ resource, results }) => resource.fsPath).join('\n') - return str + const URIs = data.results.map(({ resource, results }) => resource.fsPath) + return URIs }, - search: async ({ query: queryStr }) => { + search: async ({ query: queryStr }: ContextToolParams<'search'>) => { if (typeof queryStr !== 'string') return '(Error: query was not a string)' const query = queryBuilder.text({ pattern: queryStr, }, workspaceContextService.getWorkspace().folders.map(f => f.uri)); const data = await searchService.textSearch(query, CancellationToken.None); - const str = data.results.map(({ resource, results }) => resource) - return str as any + const URIs = data.results.map(({ resource, results }) => resource) + return URIs }, } @@ -172,12 +170,6 @@ export class ToolService implements IToolService { } - - callContextTool: IToolService['callContextTool'] = (toolName, params) => { - return this.contextToolCallFns[toolName](params) - } - - } registerSingleton(IToolService, ToolService, InstantiationType.Eager); diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts index 97cd3ed9..e957c83a 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts @@ -11,10 +11,10 @@ import { InternalToolInfo } from '../../common/toolsService.js'; -export const toAnthropicTool = (toolName: string, toolInfo: InternalToolInfo) => { - const { description, params, required } = toolInfo +export const toAnthropicTool = (toolInfo: InternalToolInfo) => { + const { name, description, params, required } = toolInfo return { - name: toolName, + name: name, description: description, input_schema: { type: 'object', @@ -45,6 +45,7 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages, messages: messages, model: modelName, max_tokens: maxTokens, + // tools: [toAnthropicTool(contextTools.list_dir)] }); @@ -60,12 +61,9 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages, if (e.type === 'content_block_start') { if (e.content_block.type !== 'tool_use') return const index = e.index - const tool = e.content_block - if (!toolCallOfIndex[index]) - toolCallOfIndex[index] = { name: '', args: '' } - toolCallOfIndex[index].name += tool.name ?? '' - toolCallOfIndex[index].args += tool.input ?? '' - + if (!toolCallOfIndex[index]) toolCallOfIndex[index] = { name: '', args: '' } + toolCallOfIndex[index].name += e.content_block.name ?? '' + toolCallOfIndex[index].args += e.content_block.input ?? '' } else if (e.type === 'content_block_delta') { if (e.delta.type !== 'input_json_delta') return @@ -79,7 +77,7 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages, stream.on('finalMessage', (response) => { // stringify the response's content const content = response.content.map(c => c.type === 'text' ? c.text : '').join('\n') - const tools = response.content.map(c => c.type === 'tool_use' ? { name: c.name, input: c.input } : null) + const tools = response.content.map(c => c.type === 'tool_use' ? { name: c.name, input: c.input } : null).filter(c => !!c) console.log("TOOLS!!!!", typeof tools[0]?.input, JSON.stringify(tools, null, 2)) diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/ollama.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/ollama.ts index 43c817a3..5a69698b 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/ollama.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/ollama.ts @@ -99,6 +99,11 @@ export const sendOllamaChat: _InternalSendLLMChatMessageFnType = ({ messages, on // iterate through the stream for await (const chunk of stream) { const newText = chunk.message.content; + + + + // chunk.message.tool_calls[0].function.arguments + fullText += newText; onText({ newText, fullText }); } diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts index 30e80bc6..ead592d3 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts @@ -14,12 +14,12 @@ import { InternalToolInfo } from '../../common/toolsService.js'; // prompting - https://platform.openai.com/docs/guides/reasoning#advice-on-prompting -export const toOpenAITool = (toolName: string, toolInfo: InternalToolInfo) => { - const { description, params, required } = toolInfo +export const toOpenAITool = (toolInfo: InternalToolInfo) => { + const { name, description, params, required } = toolInfo return { type: 'function', function: { - name: toolName, + name: name, description: description, parameters: { type: 'object', diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts index dc70c36c..86a77362 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts @@ -61,6 +61,7 @@ export const sendLLMMessage = ({ settingsOfProvider, providerName, modelName, + tools, }: SendLLMMessageParams, metricsService: IMetricsService @@ -141,27 +142,27 @@ export const sendLLMMessage = ({ case 'deepseek': case 'openAICompatible': if (messagesType === 'FIMMessage') sendOpenAIFIM({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName }); - else /* */ sendOpenAIChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName }); + else /* */ sendOpenAIChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; case 'ollama': - if (messagesType === 'FIMMessage') sendOllamaFIM({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName }) - else /* */ sendOllamaChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName }) + if (messagesType === 'FIMMessage') sendOllamaFIM({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName }); + else /* */ sendOllamaChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; case 'anthropic': if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Anthropic FIM' }) - else /* */ sendAnthropicChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName }); + else /* */ sendAnthropicChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; case 'gemini': if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Gemini FIM' }) - else /* */ sendGeminiChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName }); + else /* */ sendGeminiChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; case 'groq': if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Groq FIM' }) - else /* */ sendGroqChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName }); + else /* */ sendGroqChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; case 'mistral': if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Mistral FIM' }) - else /* */ sendMistralChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName }); + else /* */ sendMistralChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; default: onError({ message: `Error: Void provider was "${providerName}", which is not recognized.`, fullError: null }) From 0975f1bf5f6cc01a7d161809ec196200f84d3fce Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Fri, 14 Feb 2025 01:52:17 -0800 Subject: [PATCH 12/47] add developer info for models --- .../void/common/voidSettingsService.ts | 23 +- .../contrib/void/common/voidSettingsTypes.ts | 292 ++++++++++++------ .../void/electron-main/templates/templates.ts | 13 - 3 files changed, 206 insertions(+), 122 deletions(-) delete mode 100644 src/vs/workbench/contrib/void/electron-main/templates/templates.ts diff --git a/src/vs/workbench/contrib/void/common/voidSettingsService.ts b/src/vs/workbench/contrib/void/common/voidSettingsService.ts index 59089230..e44a294a 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsService.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsService.ts @@ -11,7 +11,7 @@ import { registerSingleton, InstantiationType } from '../../../../platform/insta import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js'; import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js'; import { IMetricsService } from './metricsService.js'; -import { defaultSettingsOfProvider, FeatureName, ProviderName, ModelSelectionOfFeature, SettingsOfProvider, SettingName, providerNames, ModelSelection, modelSelectionsEqual, featureNames, modelInfoOfDefaultModelNames, VoidModelInfo, GlobalSettings, GlobalSettingName, defaultGlobalSettings, displayInfoOfProviderName, defaultProviderSettings } from './voidSettingsTypes.js'; +import { defaultSettingsOfProvider, FeatureName, ProviderName, ModelSelectionOfFeature, SettingsOfProvider, SettingName, providerNames, ModelSelection, modelSelectionsEqual, featureNames, VoidModelInfo, GlobalSettings, GlobalSettingName, defaultGlobalSettings, displayInfoOfProviderName, defaultProviderSettings, developerInfoOfRecognizedModel, modelInfoOfAutodetectedModelNames } from './voidSettingsTypes.js'; const STORAGE_KEY = 'void.settingsServiceStorage' @@ -289,27 +289,26 @@ class VoidSettingsService extends Disposable implements IVoidSettingsService { - setAutodetectedModels(providerName: ProviderName, newDefaultModelNames: string[], logging: object) { + setAutodetectedModels(providerName: ProviderName, autodetectedModelNames: string[], logging: object) { const { models } = this.state.settingsOfProvider[providerName] - const oldModelNames = models.map(m => m.modelName) - const newDefaultModelInfo = modelInfoOfDefaultModelNames(newDefaultModelNames, { isAutodetected: true, existingModels: models }) - const newModelInfo = [ - ...newDefaultModelInfo, // swap out all the default models for the new default models - ...models.filter(m => !m.isDefault), // keep any non-defaul (custom) models + + const newDefaultModels = modelInfoOfAutodetectedModelNames(autodetectedModelNames, { existingModels: models }) + const newModels = [ + ...newDefaultModels, // swap out all the default models for the new default models + ...models.filter(m => !m.isDefault), // keep any non-default (custom) models ] - - this.setSettingOfProvider(providerName, 'models', newModelInfo) + this.setSettingOfProvider(providerName, 'models', newModels) // if the models changed, log it - const new_names = newModelInfo.map(m => m.modelName) + const new_names = newModels.map(m => m.modelName) if (!(oldModelNames.length === new_names.length && oldModelNames.every((_, i) => oldModelNames[i] === new_names[i])) ) { - this._metricsService.capture('Autodetect Models', { providerName, newModels: newModelInfo, ...logging }) + this._metricsService.capture('Autodetect Models', { providerName, newModels: newModels, ...logging }) } } toggleModelHidden(providerName: ProviderName, modelName: string) { @@ -335,7 +334,7 @@ class VoidSettingsService extends Disposable implements IVoidSettingsService { if (existingIdx !== -1) return // if exists, do nothing const newModels = [ ...models, - { modelName, isDefault: false, isHidden: false } + { ...developerInfoOfRecognizedModel(modelName), modelName, isDefault: false, isHidden: false } ] this.setSettingOfProvider(providerName, 'models', newModels) diff --git a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts index a31eb771..10bee1c0 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts @@ -7,45 +7,217 @@ import { VoidSettingsState } from './voidSettingsService.js' -export type VoidModelInfo = { + +// developer info used in sendLLMMessage +type VoidModelDeveloperInfo = { + supportsSystemMessage: 'system' | 'developer' | false, // if null, we will just do a string of system message + supportsTools: boolean, // we will just do a string of tool use if it doesn't support + supportsAutocompleteFIM: boolean, // we will just do a description of FIM if it doens't support <|fim_hole|> + supportsStreaming: boolean, // (o1 does NOT) we will just dump the final result if doesn't support it + maxTokens: number, // required, DEFAULT is Infinity +} + +export type VoidModelInfo = { // <-- STATEFUL modelName: string, isDefault: boolean, // whether or not it's a default for its provider isHidden: boolean, // whether or not the user is hiding it (switched off) isAutodetected?: boolean, // whether the model was autodetected by polling -} +} & VoidModelDeveloperInfo -// creates `modelInfo` from `modelNames` -export const modelInfoOfDefaultModelNames = (defaultModelNames: string[], options?: { isAutodetected: true, existingModels: VoidModelInfo[] }): VoidModelInfo[] => { - const { isAutodetected, existingModels } = options ?? {} - if (!existingModels) { // default settings - return defaultModelNames.map((modelName, i) => ({ - modelName, - isDefault: true, - isAutodetected: isAutodetected, - isHidden: defaultModelNames.length >= 10 // hide all models if there are a ton of them, and make user enable them individually - })) - } else { // settings if there are existing models (keep existing `isHidden` property) - const existingModelsMap: Record = {} - for (const existingModel of existingModels) { - existingModelsMap[existingModel.modelName] = existingModel - } - return defaultModelNames.map((modelName, i) => ({ - modelName, - isDefault: true, - isAutodetected: isAutodetected, - isHidden: !!existingModelsMap[modelName]?.isHidden, - })) +export const recognizedModels = [ + // chat + 'OpenAI 4o', + 'Anthropic Claude', + 'Llama 3.x', + 'Deepseek Chat', // deepseek coder v2 is now merged into chat (V3) https://api-docs.deepseek.com/updates#deepseek-coder--deepseek-chat-upgraded-to-deepseek-v25-model + // 'xAI Grok', + // 'Google Gemini, Gemma', + // 'Microsoft Phi4', + + + // coding (autocomplete) + 'Alibaba Qwen2.5 Coder Instruct', // we recommend this over Qwen2.5 + 'Mistral Codestral', + + // thinking + 'OpenAI o1, o3', + 'Deepseek R1', + + // general + // 'Mixtral 8x7b' + // 'Qwen2.5', + +] as const + + + + +type RecognizedModel = (typeof recognizedModels)[number] | '' + + +// const modelCapabilities: { [recognizedModel in RecognizedModel]: ({ }) => string } = { +// 'OpenAI 4o': { +// template: ({ prefix, suffix, }: { prefix: string; suffix: string; }) => `\ +// ` +// } +// } + +export function getRecognizedModel(modelName: string): RecognizedModel { + const lower = modelName.toLowerCase(); + + if (lower.includes('gpt-4o')) { + return 'OpenAI 4o'; + } + if (lower.includes('claude')) { + return 'Anthropic Claude'; + } + if (lower.includes('llama')) { + return 'Llama 3.x'; + } + if (lower.includes('qwen2.5-coder')) { + return 'Alibaba Qwen2.5 Coder Instruct'; + } + if (lower.includes('mistral')) { + return 'Mistral Codestral'; + } + // Check for "o1" or "o3" + if (/\bo1\b/.test(lower) || /\bo3\b/.test(lower)) { + return 'OpenAI o1, o3'; + } + if (lower.includes('deepseek-r1') || lower.includes('deepseek-reasoner')) { + return 'Deepseek R1'; } + // Fallback: + return ''; } + + +export const developerInfoOfRecognizedModel = (modelName: string) => { + const devInfo: { [recognizedModel in RecognizedModel]: VoidModelDeveloperInfo } = { + 'OpenAI 4o': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, + + 'Anthropic Claude': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, + + 'Llama 3.x': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, + + 'Deepseek Chat': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, + + 'Alibaba Qwen2.5 Coder Instruct': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, + + 'Mistral Codestral': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, + + 'OpenAI o1, o3': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, + + 'Deepseek R1': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, + + '': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, + } + + + const modelName_ = getRecognizedModel(modelName) + return devInfo[modelName_] +} + + + + + + +// creates `modelInfo` from `modelNames` +export const modelInfoOfDefaultModelNames = (defaultModelNames: string[]): VoidModelInfo[] => { + return defaultModelNames.map((modelName, i) => ({ + modelName, + isDefault: true, + isAutodetected: false, + isHidden: defaultModelNames.length >= 10, // hide all models if there are a ton of them, and make user enable them individually + ...developerInfoOfRecognizedModel(modelName) + })) +} + +export const modelInfoOfAutodetectedModelNames = (defaultModelNames: string[], options: { existingModels: VoidModelInfo[] }) => { + const { existingModels } = options + + const existingModelsMap: Record = {} + for (const existingModel of existingModels) { + existingModelsMap[existingModel.modelName] = existingModel + } + + return defaultModelNames.map((modelName, i) => ({ + modelName, + isDefault: true, + isAutodetected: true, + isHidden: !!existingModelsMap[modelName]?.isHidden, + ...developerInfoOfRecognizedModel(modelName) + })) +} + + + + + // https://docs.anthropic.com/en/docs/about-claude/models export const defaultAnthropicModels = modelInfoOfDefaultModelNames([ 'claude-3-5-sonnet-20241022', @@ -530,77 +702,3 @@ export const globalSettingNames = Object.keys(defaultGlobalSettings) as GlobalSe - - - -export const recognizedModels = [ - - // chat - 'OpenAI 4o', - 'Anthropic Claude', - 'Llama 3.x', - 'Deepseek Chat', // deepseek coder v2 is now merged into chat (V3) https://api-docs.deepseek.com/updates#deepseek-coder--deepseek-chat-upgraded-to-deepseek-v25-model - // 'xAI Grok', - // 'Google Gemini, Gemma', - // 'Microsoft Phi4', - - - // coding (autocomplete) - 'Alibaba Qwen2.5 Coder Instruct', // we recommend this over Qwen2.5 - 'Mistral Codestral', - - // thinking - 'OpenAI o1, o3', - 'Deepseek R1', - - // general - '' - // 'Mixtral 8x7b' - // 'Qwen2.5', - -] as const - - - - -type RecognizedModel = (typeof recognizedModels)[number] - - -// const modelCapabilities: { [recognizedModel in RecognizedModel]: ({ }) => string } = { -// 'OpenAI 4o': { -// template: ({ prefix, suffix, }: { prefix: string; suffix: string; }) => `\ -// ` -// } -// } - -export function getRecognizedModel(modelName: string): RecognizedModel { - const lower = modelName.toLowerCase(); - - if (lower.includes('gpt-4o')) { - return 'OpenAI 4o'; - } - if (lower.includes('claude')) { - return 'Anthropic Claude'; - } - if (lower.includes('llama')) { - return 'Llama 3.x'; - } - if (lower.includes('qwen2.5-coder')) { - return 'Alibaba Qwen2.5 Coder Instruct'; - } - if (lower.includes('mistral')) { - return 'Mistral Codestral'; - } - // Check for "o1" or "o3" - if (/\bo1\b/.test(lower) || /\bo3\b/.test(lower)) { - return 'OpenAI o1, o3'; - } - if (lower.includes('deepseek-r1') || lower.includes('deepseek-reasoner')) { - return 'Deepseek R1'; - } - - - - // Fallback: - return ''; -} diff --git a/src/vs/workbench/contrib/void/electron-main/templates/templates.ts b/src/vs/workbench/contrib/void/electron-main/templates/templates.ts deleted file mode 100644 index 138f7be3..00000000 --- a/src/vs/workbench/contrib/void/electron-main/templates/templates.ts +++ /dev/null @@ -1,13 +0,0 @@ -/* - -modelName -> { - system_message_type: 'system' | 'developer' (openai) | null // if null, we will just do a string of system message - supports_tools: boolean // we will just do a string of tool use if it doesn't support - supports_autocomplete_FIM (suffix) // we will just do a description of FIM if it doens't support <|fim_hole|> - - supports_streaming: boolean // (o1 does NOT) we will just dump the final result if doesn't support it - max_tokens: number // required, DEFAULT is Infinity - -} - -*/ From 6ad48ffa20c71cd437de15cbd5151b0fe10c4551 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Fri, 14 Feb 2025 01:59:17 -0800 Subject: [PATCH 13/47] recognizedModels --- .../contrib/void/common/voidSettingsTypes.ts | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts index 10bee1c0..5d371b5d 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts @@ -9,14 +9,23 @@ import { VoidSettingsState } from './voidSettingsService.js' // developer info used in sendLLMMessage -type VoidModelDeveloperInfo = { - supportsSystemMessage: 'system' | 'developer' | false, // if null, we will just do a string of system message +export type VoidModelDeveloperInfo = { + // USED: + + // TODO!!!! + // UNUSED (coming soon): + recognizedModelName: RecognizedModel, // used to show user if model was auto-recognized supportsTools: boolean, // we will just do a string of tool use if it doesn't support + supportsSystemMessage: 'system' | 'developer' | false, // if null, we will just do a string of system message supportsAutocompleteFIM: boolean, // we will just do a description of FIM if it doens't support <|fim_hole|> supportsStreaming: boolean, // (o1 does NOT) we will just dump the final result if doesn't support it maxTokens: number, // required, DEFAULT is Infinity } + + + + export type VoidModelInfo = { // <-- STATEFUL modelName: string, isDefault: boolean, // whether or not it's a default for its provider @@ -28,9 +37,6 @@ export type VoidModelInfo = { // <-- STATEFUL - - - export const recognizedModels = [ // chat 'OpenAI 4o', @@ -102,7 +108,7 @@ export function getRecognizedModel(modelName: string): RecognizedModel { export const developerInfoOfRecognizedModel = (modelName: string) => { - const devInfo: { [recognizedModel in RecognizedModel]: VoidModelDeveloperInfo } = { + const devInfo: { [recognizedModel in RecognizedModel]: Omit } = { 'OpenAI 4o': { supportsSystemMessage: false, supportsTools: false, @@ -176,9 +182,12 @@ export const developerInfoOfRecognizedModel = (modelName: string) => { }, } + const recognizedModelName = getRecognizedModel(modelName) - const modelName_ = getRecognizedModel(modelName) - return devInfo[modelName_] + return { + recognizedModelName: recognizedModelName, + ...devInfo[recognizedModelName], + } } @@ -193,7 +202,7 @@ export const modelInfoOfDefaultModelNames = (defaultModelNames: string[]): VoidM isDefault: true, isAutodetected: false, isHidden: defaultModelNames.length >= 10, // hide all models if there are a ton of them, and make user enable them individually - ...developerInfoOfRecognizedModel(modelName) + ...developerInfoOfRecognizedModel(modelName), })) } From 198a948f6c060c52399cb4d832a6de9d59aedad0 Mon Sep 17 00:00:00 2001 From: Mathew Pareles Date: Fri, 14 Feb 2025 21:30:20 -0800 Subject: [PATCH 14/47] star button commit --- .../browser/parts/editor/editorActions.ts | 20 +++++++++++++++- .../editor/media/multieditortabscontrol.css | 18 ++++----------- .../parts/editor/multiEditorTabsControl.ts | 23 ++++++++++--------- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/vs/workbench/browser/parts/editor/editorActions.ts b/src/vs/workbench/browser/parts/editor/editorActions.ts index 1b1a39b3..39c4275b 100644 --- a/src/vs/workbench/browser/parts/editor/editorActions.ts +++ b/src/vs/workbench/browser/parts/editor/editorActions.ts @@ -432,7 +432,7 @@ export class UnpinEditorAction extends Action { label: string, @ICommandService private readonly commandService: ICommandService ) { - super(id, label, ThemeIcon.asClassName(Codicon.pinned)); + super(id, label, ThemeIcon.asClassName(Codicon.starFull)); } override run(context?: IEditorCommandsContext): Promise { @@ -440,6 +440,24 @@ export class UnpinEditorAction extends Action { } } +export class PinEditorAction extends Action { + + static readonly ID = 'workbench.action.pinEditor'; + static readonly LABEL = localize('pinEditor', "Pin Editor"); + + constructor( + id: string, + label: string, + @ICommandService private readonly commandService: ICommandService + ) { + super(id, label, ThemeIcon.asClassName(Codicon.star)); + } + + override async run(context?: IEditorCommandsContext): Promise { + return this.commandService.executeCommand('workbench.action.pinEditor', undefined, context); + } +} + export class CloseEditorTabAction extends Action { static readonly ID = 'workbench.action.closeActiveEditor'; diff --git a/src/vs/workbench/browser/parts/editor/media/multieditortabscontrol.css b/src/vs/workbench/browser/parts/editor/media/multieditortabscontrol.css index 93559402..dd6c233f 100644 --- a/src/vs/workbench/browser/parts/editor/media/multieditortabscontrol.css +++ b/src/vs/workbench/browser/parts/editor/media/multieditortabscontrol.css @@ -385,7 +385,7 @@ .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab.tab-actions-right.sizing-shrink > .tab-actions, .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab.tab-actions-right.sizing-fixed > .tab-actions { flex: 0; - overflow: hidden; /* let the tab actions be pushed out of view when sizing is set to shrink/fixed to make more room */ + overflow: visible; /* ensure tab actions are always visible */ } .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab.dirty.tab-actions-right.sizing-shrink > .tab-actions, @@ -399,18 +399,8 @@ overflow: visible; /* ...but still show the tab actions on hover, focus and when dirty or sticky */ } -.monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab.close-action-off:not(.dirty) > .tab-actions, .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab.sticky-compact > .tab-actions { - display: none; /* hide the tab actions when we are configured to hide it (unless dirty, but always when sticky-compact) */ -} - -.monaco-workbench .part.editor > .content .editor-group-container.active > .title .tabs-container > .tab.active > .tab-actions .action-label, /* always show tab actions for active tab */ -.monaco-workbench .part.editor > .content .editor-group-container.active > .title .tabs-container > .tab > .tab-actions .action-label:focus, /* always show tab actions on focus */ -.monaco-workbench .part.editor > .content .editor-group-container.active > .title .tabs-container > .tab:hover > .tab-actions .action-label, /* always show tab actions on hover */ -.monaco-workbench .part.editor > .content .editor-group-container.active > .title .tabs-container > .tab.active:hover > .tab-actions .action-label, /* always show tab actions on hover */ -.monaco-workbench .part.editor > .content .editor-group-container.active > .title .tabs-container > .tab.sticky:not(.pinned-action-off) > .tab-actions .action-label, /* always show tab actions for sticky tabs */ -.monaco-workbench .part.editor > .content .editor-group-container.active > .title .tabs-container > .tab.dirty > .tab-actions .action-label { /* always show tab actions for dirty tabs */ - opacity: 1; + display: none; /* only hide tab actions when sticky-compact */ } .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab > .tab-actions .actions-container { @@ -444,11 +434,11 @@ .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab.dirty > .tab-actions .action-label, .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab.sticky:not(.pinned-action-off) > .tab-actions .action-label, .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab:hover > .tab-actions .action-label { - opacity: 0.5; /* show tab actions dimmed for inactive group */ + opacity: 1; } .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab > .tab-actions .action-label { - opacity: 0; + opacity: 1; } /* Tab Actions: Off */ diff --git a/src/vs/workbench/browser/parts/editor/multiEditorTabsControl.ts b/src/vs/workbench/browser/parts/editor/multiEditorTabsControl.ts index b23be82e..bbda32c4 100644 --- a/src/vs/workbench/browser/parts/editor/multiEditorTabsControl.ts +++ b/src/vs/workbench/browser/parts/editor/multiEditorTabsControl.ts @@ -35,7 +35,7 @@ import { MergeGroupMode, IMergeGroupOptions } from '../../../services/editor/com import { addDisposableListener, EventType, EventHelper, Dimension, scheduleAtNextAnimationFrame, findParentWithClass, clearNode, DragAndDropObserver, isMouseEvent, getWindow } from '../../../../base/browser/dom.js'; import { localize } from '../../../../nls.js'; import { IEditorGroupsView, EditorServiceImpl, IEditorGroupView, IInternalEditorOpenOptions, IEditorPartsView } from './editor.js'; -import { CloseEditorTabAction, UnpinEditorAction } from './editorActions.js'; +import { CloseEditorTabAction, PinEditorAction, UnpinEditorAction } from './editorActions.js'; import { assertAllDefined, assertIsDefined } from '../../../../base/common/types.js'; import { IEditorService } from '../../../services/editor/common/editorService.js'; import { basenameOrAuthority } from '../../../../base/common/resources.js'; @@ -113,6 +113,7 @@ export class MultiEditorTabsControl extends EditorTabsControl { private readonly closeEditorAction = this._register(this.instantiationService.createInstance(CloseEditorTabAction, CloseEditorTabAction.ID, CloseEditorTabAction.LABEL)); private readonly unpinEditorAction = this._register(this.instantiationService.createInstance(UnpinEditorAction, UnpinEditorAction.ID, UnpinEditorAction.LABEL)); + private readonly pinEditorAction = this._register(this.instantiationService.createInstance(PinEditorAction, PinEditorAction.ID, PinEditorAction.LABEL)); // Add this line private readonly tabResourceLabels = this._register(this.instantiationService.createInstance(ResourceLabels, DEFAULT_LABELS_CONTAINER)); private tabLabels: IEditorInputLabel[] = []; @@ -1518,28 +1519,28 @@ export class MultiEditorTabsControl extends EditorTabsControl { this.redrawTabLabel(editor, tabIndex, tabContainer, tabLabelWidget, tabLabel); // Action - const hasUnpinAction = isTabSticky && options.tabActionUnpinVisibility; - const hasCloseAction = !hasUnpinAction && options.tabActionCloseVisibility; - const hasAction = hasUnpinAction || hasCloseAction; + const hasCloseAction = options.tabActionCloseVisibility; + const hasAction = true; // Always show actions + // Determine which action to show let tabAction; - if (hasAction) { - tabAction = hasUnpinAction ? this.unpinEditorAction : this.closeEditorAction; + if (isTabSticky) { + tabAction = this.unpinEditorAction; } else { - // Even if the action is not visible, add it as it contains the dirty indicator - tabAction = isTabSticky ? this.unpinEditorAction : this.closeEditorAction; + tabAction = this.pinEditorAction; // Use pin action instead of close action } + // Update action bar if (!tabActionBar.hasAction(tabAction)) { if (!tabActionBar.isEmpty()) { tabActionBar.clear(); } - tabActionBar.push(tabAction, { icon: true, label: false, keybinding: this.getKeybindingLabel(tabAction) }); } - tabContainer.classList.toggle(`pinned-action-off`, isTabSticky && !hasUnpinAction); - tabContainer.classList.toggle(`close-action-off`, !hasUnpinAction && !hasCloseAction); + tabContainer.classList.toggle('sticky', isTabSticky); + tabContainer.classList.toggle(`pinned-action-off`, false); + tabContainer.classList.toggle(`close-action-off`, !hasCloseAction); for (const option of ['left', 'right']) { tabContainer.classList.toggle(`tab-actions-${option}`, hasAction && options.tabActionLocation === option); From d91ec9da2e07fefb7f10932b67491c9a7147b54a Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Fri, 14 Feb 2025 21:43:38 -0800 Subject: [PATCH 15/47] inlineDiffsService -> editCodeService --- ...lineDiffsService.ts => editCodeService.ts} | 26 +++++-------------- .../contrib/void/browser/quickEditActions.ts | 6 ++--- .../react/src/markdown/ChatMarkdownRender.tsx | 8 +++--- .../src/quick-edit-tsx/QuickEditChat.tsx | 14 +++++----- .../react/src/sidebar-tsx/SidebarChat.tsx | 2 +- .../void/browser/react/src/util/services.tsx | 8 +++--- .../contrib/void/browser/void.contribution.ts | 2 +- 7 files changed, 26 insertions(+), 40 deletions(-) rename src/vs/workbench/contrib/void/browser/{inlineDiffsService.ts => editCodeService.ts} (98%) diff --git a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts b/src/vs/workbench/contrib/void/browser/editCodeService.ts similarity index 98% rename from src/vs/workbench/contrib/void/browser/inlineDiffsService.ts rename to src/vs/workbench/contrib/void/browser/editCodeService.ts index f8449720..d8bae574 100644 --- a/src/vs/workbench/contrib/void/browser/inlineDiffsService.ts +++ b/src/vs/workbench/contrib/void/browser/editCodeService.ts @@ -27,7 +27,7 @@ import { URI } from '../../../../base/common/uri.js'; import { IConsistentEditorItemService, IConsistentItemService } from './helperServices/consistentItemService.js'; import { voidPrefixAndSuffix, ctrlKStream_userMessage, ctrlKStream_systemMessage, fastApply_rewritewholething_userMessage, fastApply_rewritewholething_systemMessage, defaultQuickEditFimTags, searchReplace_userMessage, searchReplace_systemMessage } from './prompt/prompts.js'; -import { mountCtrlK } from '../browser/react/out/quick-edit-tsx/index.js' +import { mountCtrlK } from './react/out/quick-edit-tsx/index.js' import { QuickEditPropsType } from './quickEditActions.js'; import { IModelContentChangedEvent } from '../../../../editor/common/textModelEvents.js'; import { extractCodeFromFIM, extractCodeFromRegular, ExtractedSearchReplaceBlock, extractSearchReplaceBlocks } from './helpers/extractCodeFromResult.js'; @@ -215,7 +215,7 @@ type HistorySnapshot = { type StreamLocationMutable = { line: number, col: number, addedSplitYet: boolean, originalCodeStartLine: number } -export interface IInlineDiffsService { +export interface IEditCodeService { readonly _serviceBrand: undefined; startApplying(opts: StartApplyingOpts): number | undefined; interruptStreaming(diffareaid: number): void; @@ -224,9 +224,9 @@ export interface IInlineDiffsService { // testDiffs(): void; } -export const IInlineDiffsService = createDecorator('inlineDiffAreasService'); +export const IEditCodeService = createDecorator('editCodeService'); -class InlineDiffsService extends Disposable implements IInlineDiffsService { +class EditCodeService extends Disposable implements IEditCodeService { _serviceBrand: undefined; @@ -810,7 +810,7 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService { type: UndoRedoElementType.Resource, resource: uri, label: 'Void Changes', - code: 'undoredo.inlineDiffs', + code: 'undoredo.editCode', undo: () => { restoreDiffAreas(beforeSnapshot) }, redo: () => { if (afterSnapshot) restoreDiffAreas(afterSnapshot) } } @@ -1814,7 +1814,7 @@ class InlineDiffsService extends Disposable implements IInlineDiffsService { } -registerSingleton(IInlineDiffsService, InlineDiffsService, InstantiationType.Eager); +registerSingleton(IEditCodeService, EditCodeService, InstantiationType.Eager); const acceptBg = '#1a7431' const acceptAllBg = '#1e8538' @@ -2018,17 +2018,3 @@ class AcceptAllRejectAllWidget extends Widget implements IOverlayWidget { -// registerAction2(class extends Action2 { -// constructor() { -// super({ -// id: 'void.testDiff', -// title: localize2('voidTestDiff', 'Void Test Diff'), -// f1: true, -// }); -// } -// async run(accessor: ServicesAccessor): Promise { -// const inlineDiffsService = accessor.get(IInlineDiffsService) -// // inlineDiffsService.testDiffs() - -// } -// }) diff --git a/src/vs/workbench/contrib/void/browser/quickEditActions.ts b/src/vs/workbench/contrib/void/browser/quickEditActions.ts index 1a6e0deb..1099e74c 100644 --- a/src/vs/workbench/contrib/void/browser/quickEditActions.ts +++ b/src/vs/workbench/contrib/void/browser/quickEditActions.ts @@ -8,7 +8,7 @@ import { Action2, registerAction2 } from '../../../../platform/actions/common/ac import { ServicesAccessor } from '../../../../platform/instantiation/common/instantiation.js'; import { KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js'; import { ICodeEditorService } from '../../../../editor/browser/services/codeEditorService.js'; -import { IInlineDiffsService } from './inlineDiffsService.js'; +import { IEditCodeService } from './editCodeService.js'; import { roundRangeToLines } from './sidebarActions.js'; import { VOID_CTRL_K_ACTION_ID } from './actionIDs.js'; import { localize2 } from '../../../../nls.js'; @@ -63,7 +63,7 @@ registerAction2(class extends Action2 { const { startLineNumber: startLine, endLineNumber: endLine } = selection - const inlineDiffsService = accessor.get(IInlineDiffsService) - inlineDiffsService.addCtrlKZone({ startLine, endLine, editor }) + const editCodeService = accessor.get(IEditCodeService) + editCodeService.addCtrlKZone({ startLine, endLine, editor }) } }); diff --git a/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx b/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx index 8f569074..ded3eaff 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx @@ -7,7 +7,7 @@ import React, { JSX, useCallback, useEffect, useState } from 'react' import { marked, MarkedToken, Token } from 'marked' import { BlockCode } from './BlockCode.js' import { useAccessor, useChatThreadsState, useChatThreadsStreamState } from '../util/services.js' -import { ChatMessageLocation, } from '../../../searchAndReplaceService.js' +import { ChatMessageLocation, } from '../../../aiRegexService.js' import { nameToVscodeLanguage } from '../../../helpers/detectLanguage.js' @@ -33,7 +33,7 @@ const ApplyButtonsOnHover = ({ applyStr, applyBoxId }: { applyStr: string, apply const accessor = useAccessor() const [copyButtonState, setCopyButtonState] = useState(CopyButtonState.Copy) - const inlineDiffService = accessor.get('IInlineDiffsService') + const editCodeService = accessor.get('IEditCodeService') const clipboardService = accessor.get('IClipboardService') const metricsService = accessor.get('IMetricsService') @@ -56,13 +56,13 @@ const ApplyButtonsOnHover = ({ applyStr, applyBoxId }: { applyStr: string, apply const onApply = useCallback(() => { - inlineDiffService.startApplying({ + editCodeService.startApplying({ from: 'ClickApply', type: 'searchReplace', applyStr, }) metricsService.capture('Apply Code', { length: applyStr.length }) // capture the length only - }, [metricsService, inlineDiffService, applyStr]) + }, [metricsService, editCodeService, applyStr]) const isSingleLine = !applyStr.includes('\n') diff --git a/src/vs/workbench/contrib/void/browser/react/src/quick-edit-tsx/QuickEditChat.tsx b/src/vs/workbench/contrib/void/browser/react/src/quick-edit-tsx/QuickEditChat.tsx index 57dbb472..f79c9af9 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/quick-edit-tsx/QuickEditChat.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/quick-edit-tsx/QuickEditChat.tsx @@ -24,7 +24,7 @@ export const QuickEditChat = ({ }: QuickEditPropsType) => { const accessor = useAccessor() - const inlineDiffsService = accessor.get('IInlineDiffsService') + const editCodeService = accessor.get('IEditCodeService') const sizerRef = useRef(null) const textAreaRef = useRef(null) const textAreaFnsRef = useRef(null) @@ -57,26 +57,26 @@ export const QuickEditChat = ({ if (currStreamingDiffZoneRef.current !== null) return textAreaFnsRef.current?.disable() - const id = inlineDiffsService.startApplying({ + const id = editCodeService.startApplying({ from: 'QuickEdit', type:'rewrite', diffareaid: diffareaid, }) setCurrentlyStreamingDiffZone(id ?? null) - }, [currStreamingDiffZoneRef, setCurrentlyStreamingDiffZone, isDisabled, inlineDiffsService, diffareaid]) + }, [currStreamingDiffZoneRef, setCurrentlyStreamingDiffZone, isDisabled, editCodeService, diffareaid]) const onInterrupt = useCallback(() => { if (currStreamingDiffZoneRef.current === null) return - inlineDiffsService.interruptStreaming(currStreamingDiffZoneRef.current) + editCodeService.interruptStreaming(currStreamingDiffZoneRef.current) setCurrentlyStreamingDiffZone(null) textAreaFnsRef.current?.enable() - }, [currStreamingDiffZoneRef, setCurrentlyStreamingDiffZone, inlineDiffsService]) + }, [currStreamingDiffZoneRef, setCurrentlyStreamingDiffZone, editCodeService]) const onX = useCallback(() => { onInterrupt() - inlineDiffsService.removeCtrlKZone({ diffareaid }) - }, [inlineDiffsService, diffareaid]) + editCodeService.removeCtrlKZone({ diffareaid }) + }, [editCodeService, diffareaid]) useScrollbarStyles(sizerRef) diff --git a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx index d8b4ef93..c658b10b 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx @@ -24,7 +24,7 @@ import { VOID_OPEN_SETTINGS_ACTION_ID } from '../../../voidSettingsPane.js'; import { Pencil, X } from 'lucide-react'; import { FeatureName, isFeatureNameDisabled } from '../../../../../../../workbench/contrib/void/common/voidSettingsTypes.js'; import { WarningBox } from '../void-settings-tsx/WarningBox.js'; -import { ChatMessageLocation } from '../../../searchAndReplaceService.js'; +import { ChatMessageLocation } from '../../../aiRegexService.js'; diff --git a/src/vs/workbench/contrib/void/browser/react/src/util/services.tsx b/src/vs/workbench/contrib/void/browser/react/src/util/services.tsx index 52af76f2..2d516ace 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/util/services.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/util/services.tsx @@ -28,7 +28,7 @@ import { IThemeService } from '../../../../../../../platform/theme/common/themeS import { ILLMMessageService } from '../../../../../../../workbench/contrib/void/common/llmMessageService.js'; import { IRefreshModelService } from '../../../../../../../workbench/contrib/void/common/refreshModelService.js'; import { IVoidSettingsService } from '../../../../../../../workbench/contrib/void/common/voidSettingsService.js'; -import { IInlineDiffsService } from '../../../inlineDiffsService.js'; +import { IEditCodeService } from '../../../editCodeService.js'; import { IVoidUriStateService } from '../../../voidUriStateService.js'; import { IQuickEditStateService } from '../../../quickEditStateService.js'; import { ISidebarStateService } from '../../../sidebarStateService.js'; @@ -103,10 +103,10 @@ export const _registerServices = (accessor: ServicesAccessor) => { settingsStateService: accessor.get(IVoidSettingsService), refreshModelService: accessor.get(IRefreshModelService), themeService: accessor.get(IThemeService), - inlineDiffsService: accessor.get(IInlineDiffsService), + editCodeService: accessor.get(IEditCodeService), } - const { uriStateService, sidebarStateService, quickEditStateService, settingsStateService, chatThreadsStateService, refreshModelService, themeService, inlineDiffsService } = stateServices + const { uriStateService, sidebarStateService, quickEditStateService, settingsStateService, chatThreadsStateService, refreshModelService, themeService, editCodeService } = stateServices uriState = uriStateService.state disposables.push( @@ -192,7 +192,7 @@ const getReactAccessor = (accessor: ServicesAccessor) => { ILLMMessageService: accessor.get(ILLMMessageService), IRefreshModelService: accessor.get(IRefreshModelService), IVoidSettingsService: accessor.get(IVoidSettingsService), - IInlineDiffsService: accessor.get(IInlineDiffsService), + IEditCodeService: accessor.get(IEditCodeService), IVoidUriStateService: accessor.get(IVoidUriStateService), IQuickEditStateService: accessor.get(IQuickEditStateService), ISidebarStateService: accessor.get(ISidebarStateService), diff --git a/src/vs/workbench/contrib/void/browser/void.contribution.ts b/src/vs/workbench/contrib/void/browser/void.contribution.ts index 19d20201..18fa1949 100644 --- a/src/vs/workbench/contrib/void/browser/void.contribution.ts +++ b/src/vs/workbench/contrib/void/browser/void.contribution.ts @@ -5,7 +5,7 @@ // register inline diffs -import './inlineDiffsService.js' +import './editCodeService.js' // register Sidebar pane, state, actions (keybinds, menus) (Ctrl+L) import './sidebarActions.js' From 05d8b3a982a27d8a0ecb43743eaa23917c8a85cb Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Sat, 15 Feb 2025 00:01:37 -0800 Subject: [PATCH 16/47] handle updates with version number instead of weird check --- .../contrib/void/browser/chatThreadService.ts | 68 +++++++++---------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index a3452eb2..ff20a6ec 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -154,64 +154,56 @@ class ChatThreadService extends Disposable implements IChatThreadService { ) { super() + const oldVersionNum = this._storageService.get(THREAD_VERSION_KEY, StorageScope.APPLICATION) + + + const readThreads = this._readAllThreads() + const updatedThreads = this._updatedThreadsToVersion(readThreads, oldVersionNum) + + if (updatedThreads !== null) { + this._storeAllThreads(updatedThreads) + } + + const allThreads = updatedThreads ?? readThreads this.state = { - allThreads: this._readAllThreads(), + allThreads: allThreads, currentThreadId: null as unknown as string, // gets set in startNewThread() } // always be in a thread this.openNewThread() - // for now just write the version, anticipating bigger changes in the future where we'll want to access this this._storageService.store(THREAD_VERSION_KEY, THREAD_VERSION, StorageScope.APPLICATION, StorageTarget.USER) } private _readAllThreads(): ChatThreads { - // PUT ANY VERSION CHANGE FORMAT CONVERSION CODE HERE - // CAN ADD "v0" TAG IN STORAGE AND CONVERT - - const threadsStr = this._storageService.get(THREAD_STORAGE_KEY, StorageScope.APPLICATION) - const threads: ChatThreads = threadsStr ? JSON.parse(threadsStr) : {} - this._updateThreadsToVersion(threads, THREAD_VERSION) - return threads } - private _updateThreadsToVersion(oldThreadsObject: any, toVersion: string) { + // returns if should update + private _updatedThreadsToVersion(oldThreadsObject: any, oldVersion: string | undefined): ChatThreads | null { - if (toVersion === 'v2') { + if (!oldVersion) { - const threads: ChatThreads = oldThreadsObject + // unknown, just reset chat? + return null + } - /** v1 -> v2 - - threadsState.currentStagingSelections: CodeStagingSelection[] | null; - + thread.staging: StagingInfo - + thread.focusedMessageIdx?: number | undefined; - - + chatMessage.staging: StagingInfo | null - */ - - // check if we need to update - let shouldUpdate = false - for (const thread of Object.values(threads)) { - if (!thread.staging) { - shouldUpdate = true - } - for (const chatMessage of Object.values(thread.messages)) { - if (chatMessage.role === 'user' && !chatMessage.staging) { - shouldUpdate = true - } - } - } - - if (!shouldUpdate) return; + /** v1 -> v2 + - threadsState.currentStagingSelections: CodeStagingSelection[] | null; + + thread.staging: StagingInfo + + thread.focusedMessageIdx?: number | undefined; + + chatMessage.staging: StagingInfo | null + */ + else if (oldVersion === 'v1') { + const threads = oldThreadsObject as Omit // update the threads for (const thread of Object.values(threads)) { if (!thread.staging) { @@ -226,8 +218,14 @@ class ChatThreadService extends Disposable implements IChatThreadService { } // push the update - this._storeAllThreads(threads) + return threads } + else if (oldVersion === 'v2') { + return null + } + + // up to date + return null } From bc6150aeac459b9371ae33b655b6a57b987c3f88 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Sat, 15 Feb 2025 00:02:17 -0800 Subject: [PATCH 17/47] tools --- .../contrib/void/browser/editCodeService.ts | 40 +++++++++++++++- .../contrib/void/common/llmMessageTypes.ts | 2 +- .../contrib/void/common/toolsService.ts | 29 ++++++------ .../electron-main/llmMessage/anthropic.ts | 46 +++++++++---------- .../void/electron-main/llmMessage/gemini.ts | 2 +- .../void/electron-main/llmMessage/groq.ts | 2 +- .../void/electron-main/llmMessage/mistral.ts | 2 +- .../void/electron-main/llmMessage/ollama.ts | 7 ++- .../void/electron-main/llmMessage/openai.ts | 10 ++-- .../llmMessage/sendLLMMessage.ts | 16 +++---- .../void/electron-main/llmMessageChannel.ts | 2 +- 11 files changed, 97 insertions(+), 61 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/editCodeService.ts b/src/vs/workbench/contrib/void/browser/editCodeService.ts index d8bae574..2db9cb90 100644 --- a/src/vs/workbench/contrib/void/browser/editCodeService.ts +++ b/src/vs/workbench/contrib/void/browser/editCodeService.ts @@ -42,6 +42,7 @@ import { ILLMMessageService } from '../common/llmMessageService.js'; import { LLMChatMessage, _InternalLLMChatMessage, errorDetails } from '../common/llmMessageTypes.js'; import { IMetricsService } from '../common/metricsService.js'; import { VSReadFile } from './helpers/readFile.js'; +import { voidTools } from '../common/toolsService.js'; const configOfBG = (color: Color) => { return { dark: color, light: color, hcDark: color, hcLight: color, } @@ -65,7 +66,6 @@ registerColor('void.sweepIdxBG', configOfBG(sweepIdxBG), '', true); - const getLeadingWhitespacePx = (editor: ICodeEditor, startLine: number): number => { const model = editor.getModel(); @@ -1138,6 +1138,44 @@ class EditCodeService extends Disposable implements IEditCodeService { + + + async startAgent(queryStr: string) { + // agent loop + const messages: LLMChatMessage[] = [] + + while (true) { + await new Promise((res, rej) => { + this._llmMessageService.sendLLMMessage({ + messagesType: 'chatMessages', + tools: [voidTools['read_file']], + useProviderFor: 'Apply', + logging: { loggingName: `Agent` }, + messages, + onText: ({ fullText }) => { + + }, + onFinalMessage: async ({ fullText, tools }) => { + res(tools) + }, + onError: (e) => { + }, + }) + }) + } + + + + + } + + + stopAgent() { + + } + + + public startApplying(opts: StartApplyingOpts) { if (opts.type === 'rewrite') { diff --git a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts index 40ab1f2a..dcfd2c67 100644 --- a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts +++ b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts @@ -22,7 +22,7 @@ export const errorDetails = (fullError: Error | null): string | null => { } export type OnText = (p: { newText: string, fullText: string }) => void -export type OnFinalMessage = (p: { fullText: string }) => void +export type OnFinalMessage = (p: { fullText: string, tools: { name: string, args: string }[] }) => void export type OnError = (p: { message: string, fullError: Error | null }) => void export type AbortRef = { current: (() => void) | null } diff --git a/src/vs/workbench/contrib/void/common/toolsService.ts b/src/vs/workbench/contrib/void/common/toolsService.ts index 86cc4356..43b18cb8 100644 --- a/src/vs/workbench/contrib/void/common/toolsService.ts +++ b/src/vs/workbench/contrib/void/common/toolsService.ts @@ -29,7 +29,7 @@ const paginationHelper = { param: { pageNumber: { type: 'number', description: 'The page number (optional, default is 1).' }, } } as const -export const contextTools = { +export const voidTools: { [name: string]: InternalToolInfo } = { read_file: { name: 'read_file', description: 'Returns file contents of a given URI.', @@ -73,12 +73,11 @@ export const contextTools = { // description: 'Searches files semantically for the given string query.', // // RAG // }, - } -export type ContextToolName = keyof typeof contextTools -type ContextToolParamNames = keyof typeof contextTools[T]['params'] -type ContextToolParams = { [paramName in ContextToolParamNames]: unknown } +export type ToolName = keyof typeof voidTools +type ToolParamNames = keyof typeof voidTools[T]['params'] +type ToolParamsObj = { [paramName in ToolParamNames]: unknown } @@ -121,7 +120,7 @@ export class ToolService implements IToolService { readonly _serviceBrand: undefined; - public contextToolCallFns + public toolFns constructor( @IFileService fileService: IFileService, @@ -133,32 +132,32 @@ export class ToolService implements IToolService { const queryBuilder = instantiationService.createInstance(QueryBuilder); - this.contextToolCallFns = { - read_file: async ({ uri: uriStr }: ContextToolParams<'read_file'>) => { + this.toolFns = { + read_file: async ({ uri: uriStr }: ToolParamsObj<'read_file'>) => { const uri = validateURI(uriStr) const fileContents = await VSReadFileRaw(fileService, uri) return fileContents ?? '(could not read file)' }, - list_dir: async ({ uri: uriStr }: ContextToolParams<'list_dir'>) => { + list_dir: async ({ uri: uriStr }: ToolParamsObj<'list_dir'>) => { const uri = validateURI(uriStr) // TODO!!!! check to make sure in workspace // TODO check to make sure is not gitignored const treeStr = await generateDirectoryTreeMd(fileService, uri) return treeStr }, - pathname_search: async ({ query: queryStr }: ContextToolParams<'pathname_search'>) => { + pathname_search: async ({ query: queryStr }: ToolParamsObj<'pathname_search'>) => { if (typeof queryStr !== 'string') return '(Error: query was not a string)' - const query = queryBuilder.file(workspaceContextService.getWorkspace().folders.map(f => f.uri), { filePattern: queryStr, }); + const query = queryBuilder.file(workspaceContextService.getWorkspace().folders.map(f => f.uri), { filePattern: queryStr, }) - const data = await searchService.fileSearch(query, CancellationToken.None); + const data = await searchService.fileSearch(query, CancellationToken.None) const URIs = data.results.map(({ resource, results }) => resource.fsPath) return URIs }, - search: async ({ query: queryStr }: ContextToolParams<'search'>) => { + search: async ({ query: queryStr }: ToolParamsObj<'search'>) => { if (typeof queryStr !== 'string') return '(Error: query was not a string)' - const query = queryBuilder.text({ pattern: queryStr, }, workspaceContextService.getWorkspace().folders.map(f => f.uri)); + const query = queryBuilder.text({ pattern: queryStr, }, workspaceContextService.getWorkspace().folders.map(f => f.uri)) - const data = await searchService.textSearch(query, CancellationToken.None); + const data = await searchService.textSearch(query, CancellationToken.None) const URIs = data.results.map(({ resource, results }) => resource) return URIs }, diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts index e957c83a..75ae7d89 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts @@ -6,7 +6,7 @@ import Anthropic from '@anthropic-ai/sdk'; import { _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes.js'; import { anthropicMaxPossibleTokens } from '../../common/voidSettingsTypes.js'; -import { InternalToolInfo } from '../../common/toolsService.js'; +import { InternalToolInfo, voidTools } from '../../common/toolsService.js'; @@ -45,7 +45,7 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages, messages: messages, model: modelName, max_tokens: maxTokens, - // tools: [toAnthropicTool(contextTools.list_dir)] + tools: [toAnthropicTool(voidTools.list_dir)] }); @@ -55,33 +55,31 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages, }) - // can do tool use streaming - const toolCallOfIndex: { [index: string]: { name: string, args: string } } = {} - stream.on('streamEvent', e => { - if (e.type === 'content_block_start') { - if (e.content_block.type !== 'tool_use') return - const index = e.index - if (!toolCallOfIndex[index]) toolCallOfIndex[index] = { name: '', args: '' } - toolCallOfIndex[index].name += e.content_block.name ?? '' - toolCallOfIndex[index].args += e.content_block.input ?? '' - } - else if (e.type === 'content_block_delta') { - if (e.delta.type !== 'input_json_delta') return - toolCallOfIndex[e.index].args += e.delta.partial_json - } - // TODO!!!!! - // onText({}) - }) + // // can do tool use streaming + // const toolCallOfIndex: { [index: string]: { name: string, args: string } } = {} + // stream.on('streamEvent', e => { + // if (e.type === 'content_block_start') { + // if (e.content_block.type !== 'tool_use') return + // const index = e.index + // if (!toolCallOfIndex[index]) toolCallOfIndex[index] = { name: '', args: '' } + // toolCallOfIndex[index].name += e.content_block.name ?? '' + // toolCallOfIndex[index].args += e.content_block.input ?? '' + // } + // else if (e.type === 'content_block_delta') { + // if (e.delta.type !== 'input_json_delta') return + // toolCallOfIndex[e.index].args += e.delta.partial_json + // } + // // TODO!!!!! + // // onText({}) + // }) // when we get the final message on this stream (or when error/fail) stream.on('finalMessage', (response) => { // stringify the response's content - const content = response.content.map(c => c.type === 'text' ? c.text : '').join('\n') - const tools = response.content.map(c => c.type === 'tool_use' ? { name: c.name, input: c.input } : null).filter(c => !!c) + const content = response.content.map(c => c.type === 'text' ? c.text : '').join('\n\n') + const tools = response.content.map(c => c.type === 'tool_use' ? { name: c.name, args: JSON.stringify(c.input) } : null).filter(c => !!c) - console.log("TOOLS!!!!", typeof tools[0]?.input, JSON.stringify(tools, null, 2)) - - onFinalMessage({ fullText: content, }) + onFinalMessage({ fullText: content, tools }) }) stream.on('error', (error) => { diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/gemini.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/gemini.ts index eef8cc3a..2732fbe4 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/gemini.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/gemini.ts @@ -35,7 +35,7 @@ export const sendGeminiChat: _InternalSendLLMChatMessageFnType = async ({ messag fullText += newText; onText({ newText, fullText }); } - onFinalMessage({ fullText }); + onFinalMessage({ fullText, tools: [] }); }) .catch((error) => { onError({ message: error + '', fullError: error }) diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/groq.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/groq.ts index 8f7efd14..c6fcb290 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/groq.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/groq.ts @@ -32,7 +32,7 @@ export const sendGroqChat: _InternalSendLLMChatMessageFnType = async ({ messages onText({ newText, fullText }); } - onFinalMessage({ fullText }); + onFinalMessage({ fullText, tools: [] }); }) .catch(error => { onError({ message: error + '', fullError: error }); diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/mistral.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/mistral.ts index cfddc2a5..ea3179ed 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/mistral.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/mistral.ts @@ -36,7 +36,7 @@ export const sendMistralChat: _InternalSendLLMChatMessageFnType = async ({ messa onText({ newText, fullText }); } - onFinalMessage({ fullText }); + onFinalMessage({ fullText, tools: [] }); }) .catch(error => { onError({ message: error + '', fullError: error }); diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/ollama.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/ollama.ts index 5a69698b..daff3a29 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/ollama.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/ollama.ts @@ -68,7 +68,7 @@ export const sendOllamaFIM: _InternalSendLLMFIMMessageFnType = ({ messages, onTe fullText += newText; onText({ newText, fullText }); } - onFinalMessage({ fullText }); + onFinalMessage({ fullText, tools: [] }); }) // when error/fail .catch((error) => { @@ -100,14 +100,13 @@ export const sendOllamaChat: _InternalSendLLMChatMessageFnType = ({ messages, on for await (const chunk of stream) { const newText = chunk.message.content; - - // chunk.message.tool_calls[0].function.arguments fullText += newText; onText({ newText, fullText }); } - onFinalMessage({ fullText }); + + onFinalMessage({ fullText, tools: [] }); }) // when error/fail diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts index ead592d3..4744db62 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts @@ -111,30 +111,32 @@ export const sendOpenAIFIM: _InternalSendLLMFIMMessageFnType = ({ messages, onTe // openai.completions has a FIM parameter called `suffix`, but it's deprecated and only works for ~GPT 3 era models - onFinalMessage({ fullText: 'TODO' }) + } // OpenAI, OpenRouter, OpenAICompatible -export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName }) => { +export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }) => { let fullText = '' const toolCallOfIndex: { [index: string]: { name: string, args: string } } = {} + const openai: OpenAI = newOpenAI({ providerName, settingsOfProvider }) const options: OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming = { model: modelName, messages: messages, stream: true, - // tools: Object.keys(contextTools).map(name => toOpenAITool(name, contextTools[name as ContextToolName])), + tools: tools?.map(tool => toOpenAITool(tool)), } openai.chat.completions .create(options) .then(async response => { _setAborter(() => response.controller.abort()) + // when receive text for await (const chunk of response) { @@ -153,7 +155,7 @@ export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages, on onText({ newText, fullText }); } - onFinalMessage({ fullText }); + onFinalMessage({ fullText, tools: Object.keys(toolCallOfIndex).map(index => toolCallOfIndex[index]) }); }) // when error/fail - this catches errors of both .create() and .then(for await) .catch(error => { diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts index 86a77362..6ebdbdf6 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts @@ -8,7 +8,7 @@ import { IMetricsService } from '../../common/metricsService.js'; import { sendAnthropicChat } from './anthropic.js'; import { sendOllamaFIM, sendOllamaChat } from './ollama.js'; -import { sendOpenAIChat, sendOpenAIFIM } from './openai.js'; +import { sendOpenAIChat } from './openai.js'; import { sendGeminiChat } from './gemini.js'; import { sendGroqChat } from './groq.js'; import { sendMistralChat } from './mistral.js'; @@ -107,10 +107,10 @@ export const sendLLMMessage = ({ _fullTextSoFar = fullText } - const onFinalMessage: OnFinalMessage = ({ fullText }) => { + const onFinalMessage: OnFinalMessage = ({ fullText, tools }) => { if (_didAbort) return captureLLMEvent(`${loggingName} - Received Full Message`, { messageLength: fullText.length, duration: new Date().getMilliseconds() - submit_time.getMilliseconds() }) - onFinalMessage_({ fullText }) + onFinalMessage_({ fullText, tools }) } const onError: OnError = ({ message: error, fullError }) => { @@ -141,7 +141,7 @@ export const sendLLMMessage = ({ case 'openRouter': case 'deepseek': case 'openAICompatible': - if (messagesType === 'FIMMessage') sendOpenAIFIM({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName }); + if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - OpenAI FIM', tools: [] }) else /* */ sendOpenAIChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; case 'ollama': @@ -149,19 +149,19 @@ export const sendLLMMessage = ({ else /* */ sendOllamaChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; case 'anthropic': - if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Anthropic FIM' }) + if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Anthropic FIM', tools: [] }) else /* */ sendAnthropicChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; case 'gemini': - if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Gemini FIM' }) + if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Gemini FIM', tools: [] }) else /* */ sendGeminiChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; case 'groq': - if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Groq FIM' }) + if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Groq FIM', tools: [] }) else /* */ sendGroqChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; case 'mistral': - if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Mistral FIM' }) + if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Mistral FIM', tools: [] }) else /* */ sendMistralChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; default: diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessageChannel.ts b/src/vs/workbench/contrib/void/electron-main/llmMessageChannel.ts index 98725631..9db9a68f 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessageChannel.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessageChannel.ts @@ -100,7 +100,7 @@ export class LLMMessageChannel implements IServerChannel { const mainThreadParams: SendLLMMessageParams = { ...params, onText: ({ newText, fullText }) => { this._onText_llm.fire({ requestId, newText, fullText }); }, - onFinalMessage: ({ fullText }) => { this._onFinalMessage_llm.fire({ requestId, fullText }); }, + onFinalMessage: ({ fullText, tools }) => { this._onFinalMessage_llm.fire({ requestId, fullText, tools }); }, onError: ({ message: error, fullError }) => { console.log('sendLLM: firing err'); this._onError_llm.fire({ requestId, message: error, fullError }); }, abortRef: this._abortRefOfRequestId_llm[requestId], } From 152e605856a93e22557d0cb7cba4786dc67c807d Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Sat, 15 Feb 2025 02:05:37 -0800 Subject: [PATCH 18/47] tool progress! --- .../contrib/void/browser/chatThreadService.ts | 169 +++++++++++++----- .../contrib/void/browser/editCodeService.ts | 35 ---- .../contrib/void/browser/helpers/readFile.ts | 10 +- .../react/src/sidebar-tsx/SidebarChat.tsx | 10 +- .../contrib/void/browser/sidebarActions.ts | 2 +- .../contrib/void/common/llmMessageTypes.ts | 2 +- .../contrib/void/common/toolsService.ts | 92 ++++++++-- .../electron-main/llmMessage/anthropic.ts | 8 +- 8 files changed, 215 insertions(+), 113 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index ff20a6ec..0c5b761a 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -14,6 +14,7 @@ import { IRange } from '../../../../editor/common/core/range.js'; import { ILLMMessageService } from '../common/llmMessageService.js'; import { IModelService } from '../../../../editor/common/services/model.js'; import { chat_userMessage, chat_systemMessage } from './prompt/prompts.js'; +import { IToolsService, ToolName, voidTools } from '../common/toolsService.js'; // one of the square items that indicates a selection in a chat bubble (NOT a file, a Selection of text) export type CodeSelection = { @@ -60,6 +61,13 @@ export type ChatMessage = content: string; displayContent?: undefined; } + | { + role: 'tool'; + name: string; // internal use + params: string | null; // internal use + content: string | null; // summary of the tool to the LLM + displayContent: string | null; // text message of result + } // a 'thread' means a chat message history export type ChatThreads = { @@ -124,7 +132,7 @@ export interface IChatThreadService { isFocusingMessage(): boolean; setFocusedMessageIdx(messageIdx: number | undefined): void; - _useFocusedStagingState(messageIdx?: number | undefined): readonly [StagingInfo, (stagingInfo: StagingInfo) => void]; + useFocusedStagingState(messageIdx?: number | undefined): readonly [StagingInfo, (stagingInfo: StagingInfo) => void]; editUserMessageAndStreamResponse(userMessage: string, messageIdx: number): Promise; addUserMessageAndStreamResponse(userMessage: string): Promise; @@ -151,6 +159,7 @@ class ChatThreadService extends Disposable implements IChatThreadService { @IStorageService private readonly _storageService: IStorageService, @IModelService private readonly _modelService: IModelService, @ILLMMessageService private readonly _llmMessageService: ILLMMessageService, + @IToolsService private readonly _toolsService: IToolsService, ) { super() @@ -254,14 +263,120 @@ class ChatThreadService extends Disposable implements IChatThreadService { // ---------- streaming ---------- - finishStreaming = (threadId: string, content: string, error?: { message: string, fullError: Error | null }) => { + private _finishStreamingTextMessage = (threadId: string, content: string, error?: { message: string, fullError: Error | null }) => { // add assistant's message to chat history, and clear selection - const assistantHistoryElt: ChatMessage = { role: 'assistant', content, displayContent: content || null } - this._addMessageToThread(threadId, assistantHistoryElt) + this._addMessageToThread(threadId, { role: 'assistant', content, displayContent: content || null }) this._setStreamState(threadId, { messageSoFar: undefined, streamingToken: undefined, error }) } + + + async addUserMessageAndStreamResponse(userMessage: string, stagingOverride?: StagingInfo | null) { + + const thread = this.getCurrentThread() + const threadId = thread.id + + let threadStaging = thread.staging + + const currStaging = stagingOverride ?? threadStaging ?? defaultStaging // don't use _useFocusedStagingState to avoid race conditions with focusing + const { selections: currSelns, } = currStaging + + // add user's message to chat history + const instructions = userMessage + const content = await chat_userMessage(instructions, currSelns, this._modelService) + const userHistoryElt: ChatMessage = { role: 'user', content: content, displayContent: instructions, selections: currSelns, staging: null, } + this._addMessageToThread(threadId, userHistoryElt) + + this._setStreamState(threadId, { error: undefined }) + + + + // agent loop + + + let shouldContinue = false + do { + shouldContinue = false + + console.log('Q') + + let res_: () => void + const awaitable = new Promise((res, rej) => { res_ = res }) + + const llmCancelToken = this._llmMessageService.sendLLMMessage({ + messagesType: 'chatMessages', + useProviderFor: 'Ctrl+L', + logging: { loggingName: `Agent` }, + messages: [ + { role: 'system', content: chat_systemMessage }, + ...this.getCurrentThread().messages.map(m => ({ role: m.role, content: m.content || '(empty model output)' })), + ], + tools: [voidTools['read_file']], + + onText: ({ fullText }) => { + this._setStreamState(threadId, { messageSoFar: fullText }) + }, + onFinalMessage: async ({ fullText, tools }) => { + if (tools.length === 0) { + this._finishStreamingTextMessage(threadId, fullText) + } + else { + for (const tool of tools) { + if (!(tool.name in this._toolsService.toolFns)) { + this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.args, content: `Error: This tool was not recognized, so it was not called.`, displayContent: `Error: tool not recognized.`, }) + } + else { + const toolName = tool.name as ToolName + const toolResult = await this._toolsService.toolFns[toolName](JSON.parse(tool.args)) + const string = this._toolsService.toolResultToString[toolName](toolResult as any) + this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.args, content: string, displayContent: string, }) + shouldContinue = true + } + } + } + res_() + }, + onError: (error) => { + this._finishStreamingTextMessage(threadId, this.streamState[threadId]?.messageSoFar ?? '', error) + res_() + }, + }) + if (llmCancelToken === null) return + this._setStreamState(threadId, { streamingToken: llmCancelToken }) + + await awaitable + } + while (shouldContinue); + + + + + // const llmCancelToken = this._llmMessageService.sendLLMMessage({ + // messagesType: 'chatMessages', + // logging: { loggingName: 'Chat' }, + // useProviderFor: 'Ctrl+L', + // messages: [ + // { role: 'system', content: chat_systemMessage }, + // ...this.getCurrentThread().messages.map(m => ({ role: m.role, content: m.content || '(empty model output)' })), + // ], + // onText: ({ newText, fullText }) => { + // this._setStreamState(threadId, { messageSoFar: fullText }) + // }, + // onFinalMessage: ({ fullText: content }) => { + // this._finishStreaming(threadId, content) + // }, + // onError: (error) => { + // this._finishStreaming(threadId, this.streamState[threadId]?.messageSoFar ?? '', error) + // }, + + // }) + // if (llmCancelToken === null) return + // this._setStreamState(threadId, { streamingToken: llmCancelToken }) + + } + + async editUserMessageAndStreamResponse(userMessage: string, messageIdx: number) { const thread = this.getCurrentThread() @@ -284,58 +399,18 @@ class ChatThreadService extends Disposable implements IChatThreadService { } }, true) - // stream the edit + // re-add the message and stream it this.addUserMessageAndStreamResponse(userMessage, messageToReplace.staging) } - async addUserMessageAndStreamResponse(userMessage: string, stagingOverride?: StagingInfo | null) { - const thread = this.getCurrentThread() - const threadId = thread.id - - let threadStaging = thread.staging - - const currStaging = stagingOverride ?? threadStaging ?? defaultStaging // don't use _useFocusedStagingState to avoid race conditions with focusing - const { selections: currSelns, } = currStaging - - // add user's message to chat history - const instructions = userMessage - const content = await chat_userMessage(instructions, currSelns, this._modelService) - const userHistoryElt: ChatMessage = { role: 'user', content: content, displayContent: instructions, selections: currSelns, staging: null, } - this._addMessageToThread(threadId, userHistoryElt) - - this._setStreamState(threadId, { error: undefined }) - - const llmCancelToken = this._llmMessageService.sendLLMMessage({ - messagesType: 'chatMessages', - logging: { loggingName: 'Chat' }, - useProviderFor: 'Ctrl+L', - messages: [ - { role: 'system', content: chat_systemMessage }, - ...this.getCurrentThread().messages.map(m => ({ role: m.role, content: m.content || '(empty model output)' })), - ], - onText: ({ newText, fullText }) => { - this._setStreamState(threadId, { messageSoFar: fullText }) - }, - onFinalMessage: ({ fullText: content }) => { - this.finishStreaming(threadId, content) - }, - onError: (error) => { - this.finishStreaming(threadId, this.streamState[threadId]?.messageSoFar ?? '', error) - }, - - }) - if (llmCancelToken === null) return - this._setStreamState(threadId, { streamingToken: llmCancelToken }) - - } cancelStreaming(threadId: string) { const llmCancelToken = this.streamState[threadId]?.streamingToken if (llmCancelToken !== undefined) this._llmMessageService.abort(llmCancelToken) - this.finishStreaming(threadId, this.streamState[threadId]?.messageSoFar ?? '') + this._finishStreamingTextMessage(threadId, this.streamState[threadId]?.messageSoFar ?? '') } dismissStreamError(threadId: string): void { @@ -475,7 +550,7 @@ class ChatThreadService extends Disposable implements IChatThreadService { } // gets `staging` and `setStaging` of the currently focused element, given the index of the currently selected message (or undefined if no message is selected) - _useFocusedStagingState(messageIdx?: number | undefined) { + useFocusedStagingState(messageIdx?: number | undefined) { const defaultStaging = { isBeingEdited: false, selections: [], text: '' } diff --git a/src/vs/workbench/contrib/void/browser/editCodeService.ts b/src/vs/workbench/contrib/void/browser/editCodeService.ts index 2db9cb90..52af46c2 100644 --- a/src/vs/workbench/contrib/void/browser/editCodeService.ts +++ b/src/vs/workbench/contrib/void/browser/editCodeService.ts @@ -42,7 +42,6 @@ import { ILLMMessageService } from '../common/llmMessageService.js'; import { LLMChatMessage, _InternalLLMChatMessage, errorDetails } from '../common/llmMessageTypes.js'; import { IMetricsService } from '../common/metricsService.js'; import { VSReadFile } from './helpers/readFile.js'; -import { voidTools } from '../common/toolsService.js'; const configOfBG = (color: Color) => { return { dark: color, light: color, hcDark: color, hcLight: color, } @@ -1140,40 +1139,6 @@ class EditCodeService extends Disposable implements IEditCodeService { - async startAgent(queryStr: string) { - // agent loop - const messages: LLMChatMessage[] = [] - - while (true) { - await new Promise((res, rej) => { - this._llmMessageService.sendLLMMessage({ - messagesType: 'chatMessages', - tools: [voidTools['read_file']], - useProviderFor: 'Apply', - logging: { loggingName: `Agent` }, - messages, - onText: ({ fullText }) => { - - }, - onFinalMessage: async ({ fullText, tools }) => { - res(tools) - }, - onError: (e) => { - }, - }) - }) - } - - - - - } - - - stopAgent() { - - } - public startApplying(opts: StartApplyingOpts) { diff --git a/src/vs/workbench/contrib/void/browser/helpers/readFile.ts b/src/vs/workbench/contrib/void/browser/helpers/readFile.ts index b0f154d1..39cd310d 100644 --- a/src/vs/workbench/contrib/void/browser/helpers/readFile.ts +++ b/src/vs/workbench/contrib/void/browser/helpers/readFile.ts @@ -11,7 +11,11 @@ export const VSReadFile = async (modelService: IModelService, uri: URI): Promise } export const VSReadFileRaw = async (fileService: IFileService, uri: URI) => { - const res = await fileService.readFile(uri) - const str = res.value.toString() - return str + try { + const res = await fileService.readFile(uri) + const str = res.value.toString() + return str + } catch (e) { + return null + } } diff --git a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx index c658b10b..73118cf8 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx @@ -551,7 +551,7 @@ const ChatBubble = ({ chatMessage, isLoading, messageIdx }: { chatMessage: ChatM const chatThreadsService = accessor.get('IChatThreadService') // edit mode state - const [staging, setStaging] = chatThreadsService._useFocusedStagingState(messageIdx) + const [staging, setStaging] = chatThreadsService.useFocusedStagingState(messageIdx) const mode: ChatBubbleMode = staging.isBeingEdited ? 'edit' : 'display' const [isFocused, setIsFocused] = useState(false) const [isHovered, setIsHovered] = useState(false) @@ -682,6 +682,9 @@ const ChatBubble = ({ chatMessage, isLoading, messageIdx }: { chatMessage: ChatM chatbubbleContents = } + else if (role === 'tool'){ + chatbubbleContents = chatMessage.name + } return
{ const currentThread = chatThreadsService.getCurrentThread() const previousMessages = currentThread?.messages ?? [] - const [staging, setStaging] = chatThreadsService._useFocusedStagingState() + const [staging, setStaging] = chatThreadsService.useFocusedStagingState() // stream state const currThreadStreamState = useChatThreadsStreamState(chatThreadsState.currentThreadId) @@ -822,7 +825,7 @@ export const SidebarChat = () => { const prevMessagesHTML = useMemo(() => { return previousMessages.map((message, i) => - + ) }, [previousMessages]) @@ -836,6 +839,7 @@ export const SidebarChat = () => { const messagesHTML = setStaging({ ...staging, selections: s }) diff --git a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts index dcfd2c67..f6ea2a2f 100644 --- a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts +++ b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts @@ -27,7 +27,7 @@ export type OnError = (p: { message: string, fullError: Error | null }) => void export type AbortRef = { current: (() => void) | null } export type LLMChatMessage = { - role: 'system' | 'user' | 'assistant'; + role: 'system' | 'user' | 'assistant' | 'tool'; content: string; } diff --git a/src/vs/workbench/contrib/void/common/toolsService.ts b/src/vs/workbench/contrib/void/common/toolsService.ts index 43b18cb8..32004ff2 100644 --- a/src/vs/workbench/contrib/void/common/toolsService.ts +++ b/src/vs/workbench/contrib/void/common/toolsService.ts @@ -29,7 +29,7 @@ const paginationHelper = { param: { pageNumber: { type: 'number', description: 'The page number (optional, default is 1).' }, } } as const -export const voidTools: { [name: string]: InternalToolInfo } = { +export const voidTools = { read_file: { name: 'read_file', description: 'Returns file contents of a given URI.', @@ -73,16 +73,22 @@ export const voidTools: { [name: string]: InternalToolInfo } = { // description: 'Searches files semantically for the given string query.', // // RAG // }, -} +} satisfies { [name: string]: InternalToolInfo } export type ToolName = keyof typeof voidTools -type ToolParamNames = keyof typeof voidTools[T]['params'] -type ToolParamsObj = { [paramName in ToolParamNames]: unknown } - - +export type ToolParamNames = keyof typeof voidTools[T]['params'] +export type ToolParamsObj = { [paramName in ToolParamNames]: unknown } +export type ToolCallReturnType + = T extends 'read_file' ? Promise + : T extends 'list_dir' ? Promise + : T extends 'pathname_search' ? Promise + : T extends 'search' ? Promise + : never +export type ToolFns = { [T in ToolName]: (p: string) => ToolCallReturnType } +export type ToolResultToString = { [T in ToolName]: (result: Awaited>) => string } async function generateDirectoryTreeMd(fileService: IFileService, rootURI: URI): Promise { @@ -110,17 +116,21 @@ const validateURI = (uriStr: unknown) => { return uri } -export interface IToolService { +export interface IToolsService { readonly _serviceBrand: undefined; + toolFns: ToolFns; + toolResultToString: ToolResultToString; } -export const IToolService = createDecorator('ToolService'); +export const IToolsService = createDecorator('ToolsService'); -export class ToolService implements IToolService { +export class ToolsService implements IToolsService { readonly _serviceBrand: undefined; - public toolFns + public toolFns: ToolFns + public toolResultToString: ToolResultToString + constructor( @IFileService fileService: IFileService, @@ -132,29 +142,56 @@ export class ToolService implements IToolService { const queryBuilder = instantiationService.createInstance(QueryBuilder); + const parseObj = (s: string): { [s: string]: unknown } | null => { + try { + const o = JSON.parse(s) + return o + } + catch (e) { + return null + } + } + + const invalidToolParamMsg = '(LLM parameter format was invalid for this tool)' this.toolFns = { - read_file: async ({ uri: uriStr }: ToolParamsObj<'read_file'>) => { + read_file: async (s: string) => { + const o = parseObj(s) + if (!o) return invalidToolParamMsg + const { uri: uriStr } = o + const uri = validateURI(uriStr) const fileContents = await VSReadFileRaw(fileService, uri) - return fileContents ?? '(could not read file)' + return fileContents ?? invalidToolParamMsg }, - list_dir: async ({ uri: uriStr }: ToolParamsObj<'list_dir'>) => { + list_dir: async (s: string) => { + const o = parseObj(s) + if (!o) return invalidToolParamMsg + const { uri: uriStr } = o + const uri = validateURI(uriStr) // TODO!!!! check to make sure in workspace // TODO check to make sure is not gitignored const treeStr = await generateDirectoryTreeMd(fileService, uri) return treeStr }, - pathname_search: async ({ query: queryStr }: ToolParamsObj<'pathname_search'>) => { - if (typeof queryStr !== 'string') return '(Error: query was not a string)' + pathname_search: async (s: string) => { + const o = parseObj(s) + if (!o) return invalidToolParamMsg + const { query: queryStr } = o + + if (typeof queryStr !== 'string') return 'Error: query was not a string' const query = queryBuilder.file(workspaceContextService.getWorkspace().folders.map(f => f.uri), { filePattern: queryStr, }) const data = await searchService.fileSearch(query, CancellationToken.None) - const URIs = data.results.map(({ resource, results }) => resource.fsPath) + const URIs = data.results.map(({ resource, results }) => resource) return URIs }, - search: async ({ query: queryStr }: ToolParamsObj<'search'>) => { - if (typeof queryStr !== 'string') return '(Error: query was not a string)' + search: async (s: string) => { + const o = parseObj(s) + if (!o) return '(could not search)' + const { query: queryStr } = o + + if (typeof queryStr !== 'string') return 'Error: query was not a string' const query = queryBuilder.text({ pattern: queryStr, }, workspaceContextService.getWorkspace().folders.map(f => f.uri)) const data = await searchService.textSearch(query, CancellationToken.None) @@ -164,6 +201,23 @@ export class ToolService implements IToolService { } + this.toolResultToString = { + read_file: (URIs) => { + return URIs + }, + list_dir: (URIs) => { + return URIs + }, + pathname_search: (URIs) => { + if (typeof URIs === 'string') return URIs + return URIs.map(uri => uri.fsPath).join('\n') + }, + search: (URIs) => { + if (typeof URIs === 'string') return URIs + return URIs.map(uri => uri.fsPath).join('\n') + }, + } + } @@ -171,5 +225,5 @@ export class ToolService implements IToolService { } -registerSingleton(IToolService, ToolService, InstantiationType.Eager); +registerSingleton(IToolsService, ToolsService, InstantiationType.Eager); diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts index 75ae7d89..aefe6c34 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts @@ -6,7 +6,7 @@ import Anthropic from '@anthropic-ai/sdk'; import { _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes.js'; import { anthropicMaxPossibleTokens } from '../../common/voidSettingsTypes.js'; -import { InternalToolInfo, voidTools } from '../../common/toolsService.js'; +import { InternalToolInfo } from '../../common/toolsService.js'; @@ -28,7 +28,7 @@ export const toAnthropicTool = (toolInfo: InternalToolInfo) => { -export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter }) => { +export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, tools }) => { const thisConfig = settingsOfProvider.anthropic @@ -45,8 +45,8 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages, messages: messages, model: modelName, max_tokens: maxTokens, - tools: [toAnthropicTool(voidTools.list_dir)] - }); + tools: tools?.map(tool => toAnthropicTool(tool)) + }) // when receive text From 8591d06244fc936a8110fe734a3d9d0f9c53fcba Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Sat, 15 Feb 2025 19:23:15 -0800 Subject: [PATCH 19/47] tool use plugboard progress --- .../contrib/void/browser/chatThreadService.ts | 33 ++- .../contrib/void/browser/editCodeService.ts | 6 +- .../contrib/void/browser/prompt/prompts.ts | 6 +- .../react/src/sidebar-tsx/SidebarChat.tsx | 6 +- .../contrib/void/common/llmMessageTypes.ts | 17 +- .../void/common/voidSettingsService.ts | 4 +- .../contrib/void/common/voidSettingsTypes.ts | 238 ++++++++++-------- .../electron-main/llmMessage/anthropic.ts | 5 +- .../void/electron-main/llmMessage/groq.ts | 42 ---- .../void/electron-main/llmMessage/mistral.ts | 44 ---- .../void/electron-main/llmMessage/openai.ts | 14 +- .../llmMessage/sendLLMMessage.ts | 206 ++++++++++++--- 12 files changed, 367 insertions(+), 254 deletions(-) delete mode 100644 src/vs/workbench/contrib/void/electron-main/llmMessage/groq.ts delete mode 100644 src/vs/workbench/contrib/void/electron-main/llmMessage/mistral.ts diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index 0c5b761a..98165ce0 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -65,6 +65,7 @@ export type ChatMessage = role: 'tool'; name: string; // internal use params: string | null; // internal use + tool_use_id: string; // apis require this content: string | null; // summary of the tool to the LLM displayContent: string | null; // text message of result } @@ -111,10 +112,12 @@ const newThreadObject = () => { } const THREAD_VERSION_KEY = 'void.chatThreadVersion' -const THREAD_VERSION = 'v2' +const LATEST_THREAD_VERSION = 'v2' const THREAD_STORAGE_KEY = 'void.chatThreadStorage' + +type ChatMode = 'agent' | 'chat' export interface IChatThreadService { readonly _serviceBrand: undefined; @@ -134,8 +137,8 @@ export interface IChatThreadService { useFocusedStagingState(messageIdx?: number | undefined): readonly [StagingInfo, (stagingInfo: StagingInfo) => void]; - editUserMessageAndStreamResponse(userMessage: string, messageIdx: number): Promise; - addUserMessageAndStreamResponse(userMessage: string): Promise; + editUserMessageAndStreamResponse({ userMessage, chatMode, messageIdx }: { userMessage: string, chatMode: ChatMode, messageIdx: number }): Promise; + addUserMessageAndStreamResponse({ userMessage, chatMode }: { userMessage: string, chatMode: ChatMode }): Promise; cancelStreaming(threadId: string): void; dismissStreamError(threadId: string): void; @@ -182,7 +185,7 @@ class ChatThreadService extends Disposable implements IChatThreadService { // always be in a thread this.openNewThread() - this._storageService.store(THREAD_VERSION_KEY, THREAD_VERSION, StorageScope.APPLICATION, StorageTarget.USER) + this._storageService.store(THREAD_VERSION_KEY, LATEST_THREAD_VERSION, StorageScope.APPLICATION, StorageTarget.USER) } @@ -272,7 +275,7 @@ class ChatThreadService extends Disposable implements IChatThreadService { - async addUserMessageAndStreamResponse(userMessage: string, stagingOverride?: StagingInfo | null) { + async addUserMessageAndStreamResponse({ userMessage, chatMode, stagingOverride }: { userMessage: string, chatMode: ChatMode, stagingOverride?: StagingInfo | null }) { const thread = this.getCurrentThread() const threadId = thread.id @@ -293,14 +296,10 @@ class ChatThreadService extends Disposable implements IChatThreadService { // agent loop - - let shouldContinue = false do { shouldContinue = false - console.log('Q') - let res_: () => void const awaitable = new Promise((res, rej) => { res_ = res }) @@ -310,9 +309,9 @@ class ChatThreadService extends Disposable implements IChatThreadService { logging: { loggingName: `Agent` }, messages: [ { role: 'system', content: chat_systemMessage }, - ...this.getCurrentThread().messages.map(m => ({ role: m.role, content: m.content || '(empty model output)' })), + ...this.getCurrentThread().messages.map(m => ({ ...m, content: m.content || '(empty model output)' })), ], - tools: [voidTools['read_file']], + tools: [voidTools['read_file']], // TODO!!!!! make this change on agent | chat | search onText: ({ fullText }) => { this._setStreamState(threadId, { messageSoFar: fullText }) @@ -324,13 +323,13 @@ class ChatThreadService extends Disposable implements IChatThreadService { else { for (const tool of tools) { if (!(tool.name in this._toolsService.toolFns)) { - this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.args, content: `Error: This tool was not recognized, so it was not called.`, displayContent: `Error: tool not recognized.`, }) + this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.args, tool_use_id: tool.tool_use_id, content: `Error: This tool was not recognized, so it was not called.`, displayContent: `Error: tool not recognized.`, }) } else { const toolName = tool.name as ToolName - const toolResult = await this._toolsService.toolFns[toolName](JSON.parse(tool.args)) - const string = this._toolsService.toolResultToString[toolName](toolResult as any) - this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.args, content: string, displayContent: string, }) + const toolResult = await this._toolsService.toolFns[toolName](tool.args) + const string = this._toolsService.toolResultToString[toolName](toolResult as any) // typescript is so bad it doesn't even couple the type of ToolResult with the type of the function being called here + this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.args, tool_use_id: tool.tool_use_id, content: string, displayContent: string, }) shouldContinue = true } } @@ -377,7 +376,7 @@ class ChatThreadService extends Disposable implements IChatThreadService { } - async editUserMessageAndStreamResponse(userMessage: string, messageIdx: number) { + async editUserMessageAndStreamResponse({ userMessage, chatMode, messageIdx }: { userMessage: string, chatMode: ChatMode, messageIdx: number }) { const thread = this.getCurrentThread() @@ -400,7 +399,7 @@ class ChatThreadService extends Disposable implements IChatThreadService { }, true) // re-add the message and stream it - this.addUserMessageAndStreamResponse(userMessage, messageToReplace.staging) + this.addUserMessageAndStreamResponse({ userMessage, chatMode, stagingOverride: messageToReplace.staging }) } diff --git a/src/vs/workbench/contrib/void/browser/editCodeService.ts b/src/vs/workbench/contrib/void/browser/editCodeService.ts index 52af46c2..36173d68 100644 --- a/src/vs/workbench/contrib/void/browser/editCodeService.ts +++ b/src/vs/workbench/contrib/void/browser/editCodeService.ts @@ -25,7 +25,7 @@ import * as dom from '../../../../base/browser/dom.js'; import { Widget } from '../../../../base/browser/ui/widget.js'; import { URI } from '../../../../base/common/uri.js'; import { IConsistentEditorItemService, IConsistentItemService } from './helperServices/consistentItemService.js'; -import { voidPrefixAndSuffix, ctrlKStream_userMessage, ctrlKStream_systemMessage, fastApply_rewritewholething_userMessage, fastApply_rewritewholething_systemMessage, defaultQuickEditFimTags, searchReplace_userMessage, searchReplace_systemMessage } from './prompt/prompts.js'; +import { voidPrefixAndSuffix, ctrlKStream_userMessage, ctrlKStream_systemMessage, rewriteCode_userMessage, rewriteCode_systemMessage, defaultQuickEditFimTags, searchReplace_userMessage, searchReplace_systemMessage } from './prompt/prompts.js'; import { mountCtrlK } from './react/out/quick-edit-tsx/index.js' import { QuickEditPropsType } from './quickEditActions.js'; @@ -1415,9 +1415,9 @@ class EditCodeService extends Disposable implements IEditCodeService { let messages: LLMChatMessage[] if (from === 'ClickApply') { - const userContent = fastApply_rewritewholething_userMessage({ originalCode, applyStr: opts.applyStr, uri }) + const userContent = rewriteCode_userMessage({ originalCode, applyStr: opts.applyStr, uri }) messages = [ - { role: 'system', content: fastApply_rewritewholething_systemMessage, }, + { role: 'system', content: rewriteCode_systemMessage, }, { role: 'user', content: userContent, } ] } diff --git a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts index 45a573ae..415a0c87 100644 --- a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts +++ b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts @@ -187,7 +187,7 @@ export const chat_userMessage = async (instructions: string, selections: Staging -export const fastApply_rewritewholething_systemMessage = `\ +export const rewriteCode_systemMessage = `\ You are a coding assistant that re-writes an entire file to make a change. You are given the original file \`ORIGINAL_FILE\` and a change \`CHANGE\`. Directions: @@ -199,7 +199,7 @@ Directions: -export const fastApply_rewritewholething_userMessage = ({ originalCode, applyStr, uri }: { originalCode: string, applyStr: string, uri: URI }) => { +export const rewriteCode_userMessage = ({ originalCode, applyStr, uri }: { originalCode: string, applyStr: string, uri: URI }) => { const language = filenameToVscodeLanguage(uri.fsPath) ?? '' @@ -311,7 +311,7 @@ Directions: 4. The SEARCH/REPLACE blocks you generate will be applied immediately, and so they **MUST** produce a file that the user can run IMMEDIATELY. - Make sure you add all necessary imports. - Make sure the "final" code is complete and will not result in syntax/lint errors. -5. Follow coding convention (spaces, semilcolons, comments, etc). +5. Follow coding conventions of the user (spaces, semilcolons, comments, etc). If the user spaces or formats things a certain way, CONTINUE formatting it that way, even if you prefer otherwise. ## EXAMPLE 1 ORIGINAL_FILE diff --git a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx index 73118cf8..4f337a60 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx @@ -619,7 +619,7 @@ const ChatBubble = ({ chatMessage, isLoading, messageIdx }: { chatMessage: ChatM // stream the edit const userMessage = textAreaRefState.value; - await chatThreadsService.editUserMessageAndStreamResponse(userMessage, messageIdx) + await chatThreadsService.editUserMessageAndStreamResponse({ userMessage, chatMode: 'agent', messageIdx }) } const onAbort = () => { @@ -682,7 +682,7 @@ const ChatBubble = ({ chatMessage, isLoading, messageIdx }: { chatMessage: ChatM chatbubbleContents = } - else if (role === 'tool'){ + else if (role === 'tool') { chatbubbleContents = chatMessage.name } @@ -798,7 +798,7 @@ export const SidebarChat = () => { // send message to LLM const userMessage = textAreaRef.current?.value ?? '' - await chatThreadsService.addUserMessageAndStreamResponse(userMessage) + await chatThreadsService.addUserMessageAndStreamResponse({ userMessage, chatMode: 'agent' }) setStaging({ ...staging, selections: [], }) // clear staging textAreaFnsRef.current?.setValue('') diff --git a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts index f6ea2a2f..27ce34c1 100644 --- a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts +++ b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts @@ -22,17 +22,28 @@ export const errorDetails = (fullError: Error | null): string | null => { } export type OnText = (p: { newText: string, fullText: string }) => void -export type OnFinalMessage = (p: { fullText: string, tools: { name: string, args: string }[] }) => void +export type OnFinalMessage = (p: { fullText: string, tools: { name: string, args: string, tool_use_id: string, }[] }) => void export type OnError = (p: { message: string, fullError: Error | null }) => void export type AbortRef = { current: (() => void) | null } export type LLMChatMessage = { - role: 'system' | 'user' | 'assistant' | 'tool'; + role: 'system' | 'user'; + content: string; +} | { + role: 'tool'; + tool_use_id: string; + content: string; +} | { + role: 'assistant', + tool_calls?: { name: string, tool_use_id: string, params: string }[]; content: string; } + + export type _InternalLLMChatMessage = { - role: 'user' | 'assistant'; + role: any; + tool_use_id?: any; content: string; } diff --git a/src/vs/workbench/contrib/void/common/voidSettingsService.ts b/src/vs/workbench/contrib/void/common/voidSettingsService.ts index e44a294a..af68ea38 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsService.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsService.ts @@ -11,7 +11,7 @@ import { registerSingleton, InstantiationType } from '../../../../platform/insta import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js'; import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js'; import { IMetricsService } from './metricsService.js'; -import { defaultSettingsOfProvider, FeatureName, ProviderName, ModelSelectionOfFeature, SettingsOfProvider, SettingName, providerNames, ModelSelection, modelSelectionsEqual, featureNames, VoidModelInfo, GlobalSettings, GlobalSettingName, defaultGlobalSettings, displayInfoOfProviderName, defaultProviderSettings, developerInfoOfRecognizedModel, modelInfoOfAutodetectedModelNames } from './voidSettingsTypes.js'; +import { defaultSettingsOfProvider, FeatureName, ProviderName, ModelSelectionOfFeature, SettingsOfProvider, SettingName, providerNames, ModelSelection, modelSelectionsEqual, featureNames, VoidModelInfo, GlobalSettings, GlobalSettingName, defaultGlobalSettings, displayInfoOfProviderName, defaultProviderSettings, developerInfoOfModelName, modelInfoOfAutodetectedModelNames } from './voidSettingsTypes.js'; const STORAGE_KEY = 'void.settingsServiceStorage' @@ -334,7 +334,7 @@ class VoidSettingsService extends Disposable implements IVoidSettingsService { if (existingIdx !== -1) return // if exists, do nothing const newModels = [ ...models, - { ...developerInfoOfRecognizedModel(modelName), modelName, isDefault: false, isHidden: false } + { ...developerInfoOfModelName(modelName), modelName, isDefault: false, isHidden: false } ] this.setSettingOfProvider(providerName, 'models', newModels) diff --git a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts index 5d371b5d..da5aa81b 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts @@ -9,17 +9,25 @@ import { VoidSettingsState } from './voidSettingsService.js' // developer info used in sendLLMMessage -export type VoidModelDeveloperInfo = { +export type DeveloperInfoAtModel = { // USED: + // TODO!!! think tokens - deepseek + // TODO!!!! // UNUSED (coming soon): - recognizedModelName: RecognizedModel, // used to show user if model was auto-recognized + recognizedModelName: RecognizedModelName, // used to show user if model was auto-recognized supportsTools: boolean, // we will just do a string of tool use if it doesn't support - supportsSystemMessage: 'system' | 'developer' | false, // if null, we will just do a string of system message + supportsSystemMessage: 'developer' | 'system' | false, // if null, we will just do a string of system message supportsAutocompleteFIM: boolean, // we will just do a description of FIM if it doens't support <|fim_hole|> supportsStreaming: boolean, // (o1 does NOT) we will just dump the final result if doesn't support it - maxTokens: number, // required, DEFAULT is Infinity + maxTokens: number, // required +} + +export type DeveloperInfoAtProvider = { + separateSystemMessage?: boolean; + toolsGoInRole?: boolean; // whether to do {role:'tool'} or {role:'user' tool:...} + modelOverrides?: Partial; // any overrides for models that a provider might have (e.g. if a provider always supports tool use, even if we don't recognize the model we can set tools to true) } @@ -31,7 +39,7 @@ export type VoidModelInfo = { // <-- STATEFUL isDefault: boolean, // whether or not it's a default for its provider isHidden: boolean, // whether or not the user is hiding it (switched off) isAutodetected?: boolean, // whether the model was autodetected by polling -} & VoidModelDeveloperInfo +} & DeveloperInfoAtModel @@ -62,131 +70,155 @@ export const recognizedModels = [ ] as const +type RecognizedModelName = (typeof recognizedModels)[number] | '' - -type RecognizedModel = (typeof recognizedModels)[number] | '' - - -// const modelCapabilities: { [recognizedModel in RecognizedModel]: ({ }) => string } = { -// 'OpenAI 4o': { -// template: ({ prefix, suffix, }: { prefix: string; suffix: string; }) => `\ -// ` -// } -// } - -export function getRecognizedModel(modelName: string): RecognizedModel { +export function recognizedModelOfModelName(modelName: string): RecognizedModelName { const lower = modelName.toLowerCase(); - if (lower.includes('gpt-4o')) { + if (lower.includes('gpt-4o')) return 'OpenAI 4o'; - } - if (lower.includes('claude')) { + if (lower.includes('claude')) return 'Anthropic Claude'; - } - if (lower.includes('llama')) { + if (lower.includes('llama')) return 'Llama 3.x'; - } - if (lower.includes('qwen2.5-coder')) { + if (lower.includes('qwen2.5-coder')) return 'Alibaba Qwen2.5 Coder Instruct'; - } - if (lower.includes('mistral')) { + if (lower.includes('mistral')) return 'Mistral Codestral'; - } - // Check for "o1" or "o3" - if (/\bo1\b/.test(lower) || /\bo3\b/.test(lower)) { + if (/\bo1\b/.test(lower) || /\bo3\b/.test(lower)) // o1, o3 return 'OpenAI o1, o3'; - } - if (lower.includes('deepseek-r1') || lower.includes('deepseek-reasoner')) { + if (lower.includes('deepseek-r1') || lower.includes('deepseek-reasoner')) return 'Deepseek R1'; - } + if (lower.includes('deepseek')) + return 'Deepseek Chat' - // Fallback: return ''; } +const developerInfoAtProvider: { [providerName in ProviderName]: DeveloperInfoAtProvider } = { + 'anthropic': { + separateSystemMessage: true, + toolsGoInRole: false, + modelOverrides: { + supportsTools: true, + } + }, + 'deepseek': { + separateSystemMessage: true, + }, + 'openAI': { + separateSystemMessage: false, + toolsGoInRole: true, + }, + 'gemini': { + separateSystemMessage: true, + toolsGoInRole: false + }, + 'mistral': { + separateSystemMessage: true, + }, + 'groq': { + separateSystemMessage: true, + }, + 'ollama': { + separateSystemMessage: false, + }, + 'openRouter': { + separateSystemMessage: true, + }, + 'openAICompatible': { + separateSystemMessage: true, + }, +} +export const developerInfoOfProviderName = (providerName: ProviderName): Partial => { + return developerInfoAtProvider[providerName] ?? {} +} -export const developerInfoOfRecognizedModel = (modelName: string) => { - const devInfo: { [recognizedModel in RecognizedModel]: Omit } = { - 'OpenAI 4o': { - supportsSystemMessage: false, - supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, - }, - 'Anthropic Claude': { - supportsSystemMessage: false, - supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, - }, - 'Llama 3.x': { - supportsSystemMessage: false, - supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, - }, - 'Deepseek Chat': { - supportsSystemMessage: false, - supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, - }, +// providerName is optional, but gives some extra fallbacks if provided +const developerInfoOfRecognizedModelName: { [recognizedModel in RecognizedModelName]: Omit } = { + 'OpenAI 4o': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, - 'Alibaba Qwen2.5 Coder Instruct': { - supportsSystemMessage: false, - supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, - }, + 'Anthropic Claude': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, - 'Mistral Codestral': { - supportsSystemMessage: false, - supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, - }, + 'Llama 3.x': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, - 'OpenAI o1, o3': { - supportsSystemMessage: false, - supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, - }, + 'Deepseek Chat': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, - 'Deepseek R1': { - supportsSystemMessage: false, - supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, - }, + 'Alibaba Qwen2.5 Coder Instruct': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, - '': { - supportsSystemMessage: false, - supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, - }, - } + 'Mistral Codestral': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, - const recognizedModelName = getRecognizedModel(modelName) + 'OpenAI o1, o3': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, + 'Deepseek R1': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, + + '': { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: false, + maxTokens: 4096, + }, +} +export const developerInfoOfModelName = (modelName: string, overrides?: Partial): DeveloperInfoAtModel => { + const recognizedModelName = recognizedModelOfModelName(modelName) return { recognizedModelName: recognizedModelName, - ...devInfo[recognizedModelName], + ...developerInfoOfRecognizedModelName[recognizedModelName], + ...overrides } } @@ -202,7 +234,7 @@ export const modelInfoOfDefaultModelNames = (defaultModelNames: string[]): VoidM isDefault: true, isAutodetected: false, isHidden: defaultModelNames.length >= 10, // hide all models if there are a ton of them, and make user enable them individually - ...developerInfoOfRecognizedModel(modelName), + ...developerInfoOfModelName(modelName), })) } @@ -219,7 +251,7 @@ export const modelInfoOfAutodetectedModelNames = (defaultModelNames: string[], o isDefault: true, isAutodetected: true, isHidden: !!existingModelsMap[modelName]?.isHidden, - ...developerInfoOfRecognizedModel(modelName) + ...developerInfoOfModelName(modelName) })) } diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts index aefe6c34..b443cca1 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts @@ -45,7 +45,8 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages, messages: messages, model: modelName, max_tokens: maxTokens, - tools: tools?.map(tool => toAnthropicTool(tool)) + tools: tools?.map(tool => toAnthropicTool(tool)), + tool_choice: { type: 'auto', disable_parallel_tool_use: true } // one tool use at a time }) @@ -77,7 +78,7 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages, stream.on('finalMessage', (response) => { // stringify the response's content const content = response.content.map(c => c.type === 'text' ? c.text : '').join('\n\n') - const tools = response.content.map(c => c.type === 'tool_use' ? { name: c.name, args: JSON.stringify(c.input) } : null).filter(c => !!c) + const tools = response.content.map(c => c.type === 'tool_use' ? { name: c.name, args: JSON.stringify(c.input), tool_use_id: c.id } : null).filter(c => !!c) onFinalMessage({ fullText: content, tools }) }) diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/groq.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/groq.ts deleted file mode 100644 index c6fcb290..00000000 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/groq.ts +++ /dev/null @@ -1,42 +0,0 @@ -/*-------------------------------------------------------------------------------------- - * Copyright 2025 Glass Devtools, Inc. All rights reserved. - * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. - *--------------------------------------------------------------------------------------*/ - -import Groq from 'groq-sdk'; -import { _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes.js'; - -// Groq -export const sendGroqChat: _InternalSendLLMChatMessageFnType = async ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter }) => { - let fullText = ''; - - const thisConfig = settingsOfProvider.groq - - const groq = new Groq({ - apiKey: thisConfig.apiKey, - dangerouslyAllowBrowser: true - }); - - await groq.chat.completions - .create({ - messages: messages, - model: modelName, - stream: true, - }) - .then(async response => { - _setAborter(() => response.controller.abort()) - // when receive text - for await (const chunk of response) { - const newText = chunk.choices[0]?.delta?.content || ''; - fullText += newText; - onText({ newText, fullText }); - } - - onFinalMessage({ fullText, tools: [] }); - }) - .catch(error => { - onError({ message: error + '', fullError: error }); - }) - - -}; diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/mistral.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/mistral.ts deleted file mode 100644 index ea3179ed..00000000 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/mistral.ts +++ /dev/null @@ -1,44 +0,0 @@ -/*-------------------------------------------------------------------------------------- - * Copyright 2025 Glass Devtools, Inc. All rights reserved. - * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. - *--------------------------------------------------------------------------------------*/ - -import { Mistral } from '@mistralai/mistralai'; -import { _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes.js'; - -// Mistral -export const sendMistralChat: _InternalSendLLMChatMessageFnType = async ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter }) => { - let fullText = ''; - - const thisConfig = settingsOfProvider.mistral; - - const mistral = new Mistral({ - apiKey: thisConfig.apiKey, - }) - - await mistral.chat - .stream({ - messages: messages, - model: modelName, - stream: true, - }) - .then(async response => { - // Mistral has a really nonstandard API - no interrupt and weird stream types - _setAborter(() => { console.log('Mistral does not support interrupts! Further messages will just be ignored.') }); - // when receive text - for await (const chunk of response) { - const c = chunk.data.choices[0].delta.content || '' - const newText = ( - typeof c === 'string' ? c - : c?.map(c => c.type === 'text' ? c.text : c.type).join('\n') - ) - fullText += newText; - onText({ newText, fullText }); - } - - onFinalMessage({ fullText, tools: [] }); - }) - .catch(error => { - onError({ message: error + '', fullError: error }); - }) -} diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts index 4744db62..370d411a 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts @@ -64,6 +64,18 @@ const newOpenAI = ({ settingsOfProvider, providerName }: NewParams) => { baseURL: thisConfig.endpoint, apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true }) } + else if (providerName === 'mistral') { + const thisConfig = settingsOfProvider.mistral + return new OpenAI({ + baseURL: 'https://api.mistral.ai/v1', apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true, + }) + } + else if (providerName === 'groq') { + const thisConfig = settingsOfProvider.groq + return new OpenAI({ + baseURL: '"https://api.groq.com/openai/v1"', apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true, + }) + } else { console.error(`sendOpenAIMsg: invalid providerName: ${providerName}`) throw new Error(`providerName was invalid: ${providerName}`) @@ -167,4 +179,4 @@ export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages, on } }) -}; +} diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts index 6ebdbdf6..22255292 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts @@ -10,42 +10,189 @@ import { sendAnthropicChat } from './anthropic.js'; import { sendOllamaFIM, sendOllamaChat } from './ollama.js'; import { sendOpenAIChat } from './openai.js'; import { sendGeminiChat } from './gemini.js'; -import { sendGroqChat } from './groq.js'; -import { sendMistralChat } from './mistral.js'; -import { displayInfoOfProviderName } from '../../common/voidSettingsTypes.js'; +import { developerInfoOfModelName, developerInfoOfProviderName, displayInfoOfProviderName, ProviderName, recognizedModelOfModelName } from '../../common/voidSettingsTypes.js'; + + +const cleanChatMessages = (modelName: string, providerName: ProviderName, messages: LLMChatMessage[]): { separateSystemMessageStr?: string, messages: _InternalLLMChatMessage[] } => { + const recognizedModel = recognizedModelOfModelName(modelName) + const { separateSystemMessage, toolsGoInRole, modelOverrides } = developerInfoOfProviderName(providerName) + + const { supportsSystemMessage, maxTokens, /* supportsTools, supportsAutocompleteFIM, supportsStreaming */ } = developerInfoOfModelName(recognizedModel, modelOverrides) -const cleanChatMessages = (messages: LLMChatMessage[]): _InternalLLMChatMessage[] => { // trim message content (Anthropic and other providers give an error if there is trailing whitespace) messages = messages.map(m => ({ ...m, content: m.content.trim() })) + + // 1. SYSTEM MESSAGE // find system messages and concatenate them - const systemMessage = messages + const systemMessageStr = messages .filter(msg => msg.role === 'system') .map(msg => msg.content) .join('\n') || undefined; - // remove all system messages - const noSystemMessages = messages - .filter(msg => msg.role !== 'system') as _InternalLLMChatMessage[] + let separateSystemMessageStr = undefined - // add system mesasges to first message (should be a user message) - if (systemMessage && (noSystemMessages.length !== 0)) { - const newFirstMessage = { - role: noSystemMessages[0].role, - content: ('' - + '\n' - + systemMessage - + '\n' - + '\n' - + noSystemMessages[0].content - ) + // remove all system messages + const noSystemMessages: _InternalLLMChatMessage[] = messages.filter(msg => msg.role !== 'system') + + if (systemMessageStr) { + // if supports system message + if (supportsSystemMessage) { + if (separateSystemMessage) + separateSystemMessageStr = systemMessageStr + else { + noSystemMessages.unshift({ role: supportsSystemMessage, content: systemMessageStr }) // add new first message + } + } + // if does not support system message + else { + if (supportsSystemMessage) { + if (noSystemMessages.length === 0) + noSystemMessages.push({ role: 'user', content: systemMessageStr }) + // add system mesasges to first message (should be a user message) + else { + const newFirstMessage = { + role: noSystemMessages[0].role, + content: ('' + + '\n' + + systemMessageStr + + '\n' + + '\n' + + noSystemMessages[0].content + ) + } + noSystemMessages.splice(0, 1) // delete first message + noSystemMessages.unshift(newFirstMessage) // add new first message + } + } } - noSystemMessages.splice(0, 1) // delete first message - noSystemMessages.unshift(newFirstMessage) // add new first message } - return noSystemMessages + // 2. TOOLS + + const newMessages = noSystemMessages; + + if (toolsGoInRole) { + let index = 0; + while (index < newMessages.length) { + + // merge tool with the previous assistant and the following user message + + // take prev message and add + /* +openai assistant message will have: https://platform.openai.com/docs/guides/function-calling#function-calling-steps +"tool_calls":[ +{ + "id": "call_12345xyz", + "type": "function", + "function": { + "name": "get_weather", + "arguments": "{\"latitude\":48.8566,\"longitude\":2.3522}" + } +}] + +openai user response will be: +{ + "role": "tool", + "tool_call_id": tool_call.id, + "content": str(result) +} + +anthropic assistant message will have: https://docs.anthropic.com/en/docs/build-with-claude/tool-use#tool-use-examples +"content": [ + { + "type": "text", + "text": "I need to call the get_weather function, and the user wants SF, which is likely San Francisco, CA." + }, + { + "type": "tool_use", + "id": "toolu_01A09q90qw90lq917835lq9", + "name": "get_weather", + "input": {"location": "San Francisco, CA", "unit": "celsius"} + } + ] + +anthropic user message response will be: +"content": [ + { + "type": "tool_result", + "tool_use_id": "toolu_01A09q90qw90lq917835lq9", + "content": "15 degrees" + } +] + + +ACCORDING TO 4o: gemini: similar to openai, but function_call, and only 1 call per message (no id because only 1 message) +gemini request: { + "role": "assistant", + "content": null, + "function_call": { + "name": "get_weather", + "arguments": { + "latitude": 48.8566, + "longitude": 2.3522 + } + } +} +gemini response: +{ + "role": "assistant", + "function_response": { + "name": "get_weather", + "response": { + "temperature": "15°C", + "condition": "Cloudy" + } + } +} + + ++ anthropic + ++ openai-compat (4) + + gemini + +ollama + + +mistral: same as openai + + */ + + + if (newMessages[index].role === 'tool') { + const toolMessage = newMessages[index]; + const assistantMessage = newMessages[index - 1]; + const userMessage = newMessages[index + 1]; + + // while ((toolIndex = newMessages.findIndex((msg, idx) => idx > toolIndex && msg.role === 'tool')) !== -1) { + + // tool_use goes in assistant + if (assistantMessage?.role === 'assistant') { + assistantMessage.tool_use += `\n${toolMessage.content}`; + } + + // tool_result goes in user + if (userMessage?.role === 'user') { + + userMessage.content = `${toolMessage.content}\n${userMessage.content}`; + } + + // Remove the tool message after merging its content + newMessages.splice(index, 1); + } else { + index++; + } + } + } + + + + return { + separateSystemMessageStr, + messages: newMessages + } } @@ -68,11 +215,14 @@ export const sendLLMMessage = ({ ) => { let messagesArr: _InternalLLMChatMessage[] = [] + + // TODO!!! move this to the actual providers if (messagesType === 'chatMessages') { - messagesArr = cleanChatMessages([ + const { messages: cleanedMessages, separateSystemMessageStr } = cleanChatMessages(modelName, providerName, [ { role: 'system', content: aiInstructions }, ...messages_ ]) + messagesArr = cleanedMessages } // only captures number of messages and message "shape", no actual code, instructions, prompts, etc @@ -141,6 +291,8 @@ export const sendLLMMessage = ({ case 'openRouter': case 'deepseek': case 'openAICompatible': + case 'mistral': + case 'groq': if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - OpenAI FIM', tools: [] }) else /* */ sendOpenAIChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; @@ -156,14 +308,6 @@ export const sendLLMMessage = ({ if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Gemini FIM', tools: [] }) else /* */ sendGeminiChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; - case 'groq': - if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Groq FIM', tools: [] }) - else /* */ sendGroqChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); - break; - case 'mistral': - if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Mistral FIM', tools: [] }) - else /* */ sendMistralChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); - break; default: onError({ message: `Error: Void provider was "${providerName}", which is not recognized.`, fullError: null }) break; From 131493b5e17581dc3f043fa875b3d2824bd8fe20 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Sun, 16 Feb 2025 00:43:46 -0800 Subject: [PATCH 20/47] tool structure --- .../contrib/void/browser/chatThreadService.ts | 122 +++++----- .../contrib/void/browser/editCodeService.ts | 13 +- .../react/src/void-settings-tsx/Settings.tsx | 39 +-- .../contrib/void/common/llmMessageTypes.ts | 27 ++- .../void/common/voidSettingsService.ts | 4 +- .../contrib/void/common/voidSettingsTypes.ts | 43 ++-- .../void/electron-main/llmMessage/_old.ts | 96 ++++++++ .../electron-main/llmMessage/addSupport.ts | 177 ++++++++++++++ .../electron-main/llmMessage/anthropic.ts | 17 +- .../void/electron-main/llmMessage/gemini.ts | 43 ---- .../void/electron-main/llmMessage/ollama.ts | 137 +++++------ .../void/electron-main/llmMessage/openai.ts | 54 +++-- .../llmMessage/sendLLMMessage.ts | 224 +----------------- 13 files changed, 530 insertions(+), 466 deletions(-) create mode 100644 src/vs/workbench/contrib/void/electron-main/llmMessage/_old.ts create mode 100644 src/vs/workbench/contrib/void/electron-main/llmMessage/addSupport.ts delete mode 100644 src/vs/workbench/contrib/void/electron-main/llmMessage/gemini.ts diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index 98165ce0..ec1719b8 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -15,6 +15,7 @@ import { ILLMMessageService } from '../common/llmMessageService.js'; import { IModelService } from '../../../../editor/common/services/model.js'; import { chat_userMessage, chat_systemMessage } from './prompt/prompts.js'; import { IToolsService, ToolName, voidTools } from '../common/toolsService.js'; +import { toLLMChatMessage } from '../common/llmMessageTypes.js'; // one of the square items that indicates a selection in a chat bubble (NOT a file, a Selection of text) export type CodeSelection = { @@ -53,6 +54,7 @@ export type ChatMessage = } | { role: 'assistant'; + tool_calls?: { name: string, id: string, params: string }[]; content: string | null; // content received from LLM - allowed to be '', will be replaced with (empty) displayContent: string | null; // content displayed to user (this is the same as content for now) - allowed to be '', will be ignored } @@ -65,7 +67,7 @@ export type ChatMessage = role: 'tool'; name: string; // internal use params: string | null; // internal use - tool_use_id: string; // apis require this + id: string; // apis require this tool use id content: string | null; // summary of the tool to the LLM displayContent: string | null; // text message of result } @@ -296,82 +298,64 @@ class ChatThreadService extends Disposable implements IChatThreadService { // agent loop - let shouldContinue = false - do { - shouldContinue = false + const agentLoop = async () => { - let res_: () => void - const awaitable = new Promise((res, rej) => { res_ = res }) + let shouldContinue = false + do { + shouldContinue = false - const llmCancelToken = this._llmMessageService.sendLLMMessage({ - messagesType: 'chatMessages', - useProviderFor: 'Ctrl+L', - logging: { loggingName: `Agent` }, - messages: [ - { role: 'system', content: chat_systemMessage }, - ...this.getCurrentThread().messages.map(m => ({ ...m, content: m.content || '(empty model output)' })), - ], - tools: [voidTools['read_file']], // TODO!!!!! make this change on agent | chat | search + let res_: () => void + const awaitable = new Promise((res, rej) => { res_ = res }) - onText: ({ fullText }) => { - this._setStreamState(threadId, { messageSoFar: fullText }) - }, - onFinalMessage: async ({ fullText, tools }) => { - if (tools.length === 0) { - this._finishStreamingTextMessage(threadId, fullText) - } - else { - for (const tool of tools) { - if (!(tool.name in this._toolsService.toolFns)) { - this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.args, tool_use_id: tool.tool_use_id, content: `Error: This tool was not recognized, so it was not called.`, displayContent: `Error: tool not recognized.`, }) - } - else { - const toolName = tool.name as ToolName - const toolResult = await this._toolsService.toolFns[toolName](tool.args) - const string = this._toolsService.toolResultToString[toolName](toolResult as any) // typescript is so bad it doesn't even couple the type of ToolResult with the type of the function being called here - this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.args, tool_use_id: tool.tool_use_id, content: string, displayContent: string, }) - shouldContinue = true + const llmCancelToken = this._llmMessageService.sendLLMMessage({ + messagesType: 'chatMessages', + useProviderFor: 'Ctrl+L', + logging: { loggingName: `Agent` }, + messages: [ + { role: 'system', content: chat_systemMessage }, + ...this.getCurrentThread().messages.map(m => (toLLMChatMessage(m))), + ], + + // TODO!!!!! make this change on 'agent' | 'chat' + tools: Object.keys(voidTools).map(toolName => voidTools[toolName as ToolName]), + + onText: ({ fullText }) => { + this._setStreamState(threadId, { messageSoFar: fullText }) + }, + onFinalMessage: async ({ fullText, tools }) => { + if (tools.length === 0) { + this._finishStreamingTextMessage(threadId, fullText) + } + else { + for (const tool of tools) { + if (!(tool.name in this._toolsService.toolFns)) { + this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.args, id: tool.id, content: `Error: This tool was not recognized, so it was not called.`, displayContent: `Error: tool not recognized.`, }) + } + else { + const toolName = tool.name as ToolName + const toolResult = await this._toolsService.toolFns[toolName](tool.args) + const string = this._toolsService.toolResultToString[toolName](toolResult as any) // typescript is so bad it doesn't even couple the type of ToolResult with the type of the function being called here + this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.args, id: tool.id, content: string, displayContent: string, }) + shouldContinue = true + } } } - } - res_() - }, - onError: (error) => { - this._finishStreamingTextMessage(threadId, this.streamState[threadId]?.messageSoFar ?? '', error) - res_() - }, - }) - if (llmCancelToken === null) return - this._setStreamState(threadId, { streamingToken: llmCancelToken }) + res_() + }, + onError: (error) => { + this._finishStreamingTextMessage(threadId, this.streamState[threadId]?.messageSoFar ?? '', error) + res_() + }, + }) + if (llmCancelToken === null) break + this._setStreamState(threadId, { streamingToken: llmCancelToken }) - await awaitable + await awaitable + } + while (shouldContinue); } - while (shouldContinue); - - - - // const llmCancelToken = this._llmMessageService.sendLLMMessage({ - // messagesType: 'chatMessages', - // logging: { loggingName: 'Chat' }, - // useProviderFor: 'Ctrl+L', - // messages: [ - // { role: 'system', content: chat_systemMessage }, - // ...this.getCurrentThread().messages.map(m => ({ role: m.role, content: m.content || '(empty model output)' })), - // ], - // onText: ({ newText, fullText }) => { - // this._setStreamState(threadId, { messageSoFar: fullText }) - // }, - // onFinalMessage: ({ fullText: content }) => { - // this._finishStreaming(threadId, content) - // }, - // onError: (error) => { - // this._finishStreaming(threadId, this.streamState[threadId]?.messageSoFar ?? '', error) - // }, - - // }) - // if (llmCancelToken === null) return - // this._setStreamState(threadId, { streamingToken: llmCancelToken }) + agentLoop() // DO NOT AWAIT THIS, this fn should resolve when ready to clear inputs } diff --git a/src/vs/workbench/contrib/void/browser/editCodeService.ts b/src/vs/workbench/contrib/void/browser/editCodeService.ts index 36173d68..c2828084 100644 --- a/src/vs/workbench/contrib/void/browser/editCodeService.ts +++ b/src/vs/workbench/contrib/void/browser/editCodeService.ts @@ -39,7 +39,7 @@ import { Emitter } from '../../../../base/common/event.js'; import { VOID_OPEN_SETTINGS_ACTION_ID } from './voidSettingsPane.js'; import { ICommandService } from '../../../../platform/commands/common/commands.js'; import { ILLMMessageService } from '../common/llmMessageService.js'; -import { LLMChatMessage, _InternalLLMChatMessage, errorDetails } from '../common/llmMessageTypes.js'; +import { LLMChatMessage, errorDetails } from '../common/llmMessageTypes.js'; import { IMetricsService } from '../common/metricsService.js'; import { VSReadFile } from './helpers/readFile.js'; @@ -1178,13 +1178,16 @@ class EditCodeService extends Disposable implements IEditCodeService { private async _initializeSearchAndReplaceStream({ applyStr }: { applyStr: string }) { + console.log('SEARCHREPLACE') const uri_ = this._getActiveEditorURI() if (!uri_) return const uri = uri_ + console.log('/* AAAA */') // generate search/replace block text const fileContents = await VSReadFile(this._modelService, uri) if (fileContents === null) return + console.log('/* BBB*/') @@ -1236,9 +1239,7 @@ class EditCodeService extends Disposable implements IEditCodeService { - - - // TODO turn this into a service and provide it + // TODO!!! turn this into a service and provide it streamRequestIdRef.current = this._llmMessageService.sendLLMMessage({ messagesType: 'chatMessages', useProviderFor: 'Apply', @@ -1304,6 +1305,8 @@ class EditCodeService extends Disposable implements IEditCodeService { this._refreshStylesAndDiffsInURI(uri) }, onFinalMessage: async ({ fullText }) => { + console.log('/* ONFIN */', fullText) + // 1. wait 500ms and fix lint errors - call lint error workflow // (update react state to say "Fixing errors") const blocks = extractSearchReplaceBlocks(fullText) @@ -1322,6 +1325,8 @@ class EditCodeService extends Disposable implements IEditCodeService { onDone(false) }, onError: (e) => { + console.log('/* ERRRRRR */') + console.log('ERROR', e); onDone(true) }, diff --git a/src/vs/workbench/contrib/void/browser/react/src/void-settings-tsx/Settings.tsx b/src/vs/workbench/contrib/void/browser/react/src/void-settings-tsx/Settings.tsx index 62511563..b1079587 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/void-settings-tsx/Settings.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/void-settings-tsx/Settings.tsx @@ -395,7 +395,16 @@ export const AIInstructionsBox = () => { export const FeaturesTab = () => { return <> -

Local Providers

+

Models

+ + + + + + + + +

Local Providers

{/*

{`Keep your data private by hosting AI locally on your computer.`}

*/} {/*

{`Instructions:`}

*/} {/*

{`Void can access any model that you host locally. We automatically detect your local models by default.`}

*/} @@ -420,13 +429,20 @@ export const FeaturesTab = () => { -

Models

+ + +

Feature Options

- - - - + {featureNames.map(featureName => +
+

{displayInfoOfFeatureName(featureName)}

+ +
+ )}
+ } @@ -588,17 +604,6 @@ const GeneralTab = () => {
-
-

Model Selection

- {featureNames.map(featureName => -
-

{displayInfoOfFeatureName(featureName)}

- -
- )} -
} diff --git a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts index 27ce34c1..0af2e31e 100644 --- a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts +++ b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts @@ -3,6 +3,7 @@ * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. *--------------------------------------------------------------------------------------*/ +import { ChatMessage } from '../browser/chatThreadService.js' import { InternalToolInfo } from './toolsService.js' import { FeatureName, ProviderName, SettingsOfProvider } from './voidSettingsTypes.js' @@ -22,7 +23,7 @@ export const errorDetails = (fullError: Error | null): string | null => { } export type OnText = (p: { newText: string, fullText: string }) => void -export type OnFinalMessage = (p: { fullText: string, tools: { name: string, args: string, tool_use_id: string, }[] }) => void +export type OnFinalMessage = (p: { fullText: string, tools: { name: string, args: string, id: string, }[] }) => void // id is tool_use_id export type OnError = (p: { message: string, fullError: Error | null }) => void export type AbortRef = { current: (() => void) | null } @@ -30,20 +31,32 @@ export type LLMChatMessage = { role: 'system' | 'user'; content: string; } | { - role: 'tool'; - tool_use_id: string; + role: 'assistant', + tool_calls?: { name: string, id: string, params: string }[]; content: string; } | { - role: 'assistant', - tool_calls?: { name: string, tool_use_id: string, params: string }[]; + role: 'tool'; + id: string; content: string; } +export const toLLMChatMessage = (c: ChatMessage): LLMChatMessage => { + if (c.role === 'system' || c.role === 'user') { + return { role: c.role, content: c.content ?? '(empty)' } + } + else if (c.role === 'assistant') + return { role: c.role, tool_calls: c.tool_calls, content: c.content ?? '(empty model output)' } + else if (c.role === 'tool') + return { role: c.role, id: c.id, content: c.content ?? '(empty output)' } + else { + throw 1 + } +} export type _InternalLLMChatMessage = { role: any; - tool_use_id?: any; + id?: any; content: string; } @@ -112,7 +125,7 @@ export type _InternalSendLLMChatMessageFnType = ( tools?: InternalToolInfo[], - messages: _InternalLLMChatMessage[]; + messages: LLMChatMessage[]; } ) => void diff --git a/src/vs/workbench/contrib/void/common/voidSettingsService.ts b/src/vs/workbench/contrib/void/common/voidSettingsService.ts index af68ea38..eac87692 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsService.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsService.ts @@ -11,7 +11,7 @@ import { registerSingleton, InstantiationType } from '../../../../platform/insta import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js'; import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js'; import { IMetricsService } from './metricsService.js'; -import { defaultSettingsOfProvider, FeatureName, ProviderName, ModelSelectionOfFeature, SettingsOfProvider, SettingName, providerNames, ModelSelection, modelSelectionsEqual, featureNames, VoidModelInfo, GlobalSettings, GlobalSettingName, defaultGlobalSettings, displayInfoOfProviderName, defaultProviderSettings, developerInfoOfModelName, modelInfoOfAutodetectedModelNames } from './voidSettingsTypes.js'; +import { defaultSettingsOfProvider, FeatureName, ProviderName, ModelSelectionOfFeature, SettingsOfProvider, SettingName, providerNames, ModelSelection, modelSelectionsEqual, featureNames, VoidModelInfo, GlobalSettings, GlobalSettingName, defaultGlobalSettings, defaultProviderSettings, developerInfoOfModelName, modelInfoOfAutodetectedModelNames } from './voidSettingsTypes.js'; const STORAGE_KEY = 'void.settingsServiceStorage' @@ -89,7 +89,7 @@ const _updatedValidatedState = (state: Omit) // update model options let newModelOptions: ModelOption[] = [] for (const providerName of providerNames) { - const providerTitle = displayInfoOfProviderName(providerName).title.toLowerCase() // looks better lowercase, best practice to not use raw providerName + const providerTitle = providerName // displayInfoOfProviderName(providerName).title.toLowerCase() // looks better lowercase, best practice to not use raw providerName if (!newSettingsOfProvider[providerName]._didFillInProviderSettings) continue // if disabled, don't display model options for (const { modelName, isHidden } of newSettingsOfProvider[providerName].models) { if (isHidden) continue diff --git a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts index da5aa81b..9bc1638d 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts @@ -25,9 +25,7 @@ export type DeveloperInfoAtModel = { } export type DeveloperInfoAtProvider = { - separateSystemMessage?: boolean; - toolsGoInRole?: boolean; // whether to do {role:'tool'} or {role:'user' tool:...} - modelOverrides?: Partial; // any overrides for models that a provider might have (e.g. if a provider always supports tool use, even if we don't recognize the model we can set tools to true) + overrideSettingsForAllModels?: Partial; // any overrides for models that a provider might have (e.g. if a provider always supports tool use, even if we don't recognize the model we can set tools to true) } @@ -99,37 +97,34 @@ export function recognizedModelOfModelName(modelName: string): RecognizedModelNa const developerInfoAtProvider: { [providerName in ProviderName]: DeveloperInfoAtProvider } = { 'anthropic': { - separateSystemMessage: true, - toolsGoInRole: false, - modelOverrides: { + overrideSettingsForAllModels: { + supportsSystemMessage: 'system', supportsTools: true, + supportsAutocompleteFIM: false, + supportsStreaming: true, } }, 'deepseek': { - separateSystemMessage: true, - }, - 'openAI': { - separateSystemMessage: false, - toolsGoInRole: true, - }, - 'gemini': { - separateSystemMessage: true, - toolsGoInRole: false - }, - 'mistral': { - separateSystemMessage: true, - }, - 'groq': { - separateSystemMessage: true, + overrideSettingsForAllModels: { + supportsSystemMessage: false, + supportsTools: false, + supportsAutocompleteFIM: false, + supportsStreaming: true, + } }, 'ollama': { - separateSystemMessage: false, }, 'openRouter': { - separateSystemMessage: true, }, 'openAICompatible': { - separateSystemMessage: true, + }, + 'openAI': { + }, + 'gemini': { + }, + 'mistral': { + }, + 'groq': { }, } export const developerInfoOfProviderName = (providerName: ProviderName): Partial => { diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/_old.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/_old.ts new file mode 100644 index 00000000..e1e90245 --- /dev/null +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/_old.ts @@ -0,0 +1,96 @@ +// /*-------------------------------------------------------------------------------------- +// * Copyright 2025 Glass Devtools, Inc. All rights reserved. +// * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. +// *--------------------------------------------------------------------------------------*/ + +// import Groq from 'groq-sdk'; +// import { _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes.js'; + +// // Groq +// export const sendGroqChat: _InternalSendLLMChatMessageFnType = async ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter }) => { +// let fullText = ''; + +// const thisConfig = settingsOfProvider.groq + +// const groq = new Groq({ +// apiKey: thisConfig.apiKey, +// dangerouslyAllowBrowser: true +// }); + +// await groq.chat.completions +// .create({ +// messages: messages, +// model: modelName, +// stream: true, +// }) +// .then(async response => { +// _setAborter(() => response.controller.abort()) +// // when receive text +// for await (const chunk of response) { +// const newText = chunk.choices[0]?.delta?.content || ''; +// fullText += newText; +// onText({ newText, fullText }); +// } + +// onFinalMessage({ fullText, tools: [] }); +// }) +// .catch(error => { +// onError({ message: error + '', fullError: error }); +// }) + + +// }; + + + +// /*-------------------------------------------------------------------------------------- +// * Copyright 2025 Glass Devtools, Inc. All rights reserved. +// * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. +// *--------------------------------------------------------------------------------------*/ + +// import { Mistral } from '@mistralai/mistralai'; +// import { _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes.js'; + +// // Mistral +// export const sendMistralChat: _InternalSendLLMChatMessageFnType = async ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter }) => { +// let fullText = ''; + +// const thisConfig = settingsOfProvider.mistral; + +// const mistral = new Mistral({ +// apiKey: thisConfig.apiKey, +// }) + +// await mistral.chat +// .stream({ +// messages: messages, +// model: modelName, +// stream: true, +// }) +// .then(async response => { +// // Mistral has a really nonstandard API - no interrupt and weird stream types +// _setAborter(() => { console.log('Mistral does not support interrupts! Further messages will just be ignored.') }); +// // when receive text +// for await (const chunk of response) { +// const c = chunk.data.choices[0].delta.content || '' +// const newText = ( +// typeof c === 'string' ? c +// : c?.map(c => c.type === 'text' ? c.text : c.type).join('\n') +// ) +// fullText += newText; +// onText({ newText, fullText }); +// } + +// onFinalMessage({ fullText, tools: [] }); +// }) +// .catch(error => { +// onError({ message: error + '', fullError: error }); +// }) +// } + + + + + + + diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/addSupport.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/addSupport.ts new file mode 100644 index 00000000..ef9dfdd5 --- /dev/null +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/addSupport.ts @@ -0,0 +1,177 @@ +import { _InternalLLMChatMessage, LLMChatMessage } from '../../common/llmMessageTypes.js'; +import { DeveloperInfoAtModel, developerInfoOfModelName, developerInfoOfProviderName, ProviderName } from '../../common/voidSettingsTypes.js'; + + +// no matter whether the model supports a system message or not (or what format it supports), add it in some way +// also take into account tools if the model doesn't support tool use +export const addSystemMessageAndToolSupport = (modelName: string, providerName: ProviderName, messages_: LLMChatMessage[], { separateSystemMessage }: { separateSystemMessage: boolean }): { separateSystemMessageStr?: string, messages: _InternalLLMChatMessage[], devInfo: DeveloperInfoAtModel } => { + + const messages: _InternalLLMChatMessage[] = messages_.map(m => ({ ...m, content: m.content.trim(), })) + + const { overrideSettingsForAllModels } = developerInfoOfProviderName(providerName) + const devInfo = developerInfoOfModelName(modelName, overrideSettingsForAllModels) + const { supportsSystemMessage } = devInfo + + // 1. SYSTEM MESSAGE + // find system messages and concatenate them + let systemMessageStr = messages + .filter(msg => msg.role === 'system') + .map(msg => msg.content) + .join('\n') || undefined; + + let separateSystemMessageStr: string | undefined = undefined + + // remove all system messages + const newMessages: _InternalLLMChatMessage[] = messages.filter(msg => msg.role !== 'system') + + + // if (!supportsTools) { + // if (!systemMessageStr) systemMessageStr = '' + // systemMessageStr += '' // TODO!!! add tool use system message here + // } + + + if (systemMessageStr) { + // if supports system message + if (supportsSystemMessage) { + if (separateSystemMessage) + separateSystemMessageStr = systemMessageStr + else { + newMessages.unshift({ role: supportsSystemMessage, content: systemMessageStr }) // add new first message + } + } + // if does not support system message + else { + if (supportsSystemMessage) { + if (newMessages.length === 0) + newMessages.push({ role: 'user', content: systemMessageStr }) + // add system mesasges to first message (should be a user message) + else { + const newFirstMessage = { + role: newMessages[0].role, + content: ('' + + '\n' + + systemMessageStr + + '\n' + + '\n' + + newMessages[0].content + ) + } + newMessages.splice(0, 1) // delete first message + newMessages.unshift(newFirstMessage) // add new first message + } + } + } + } + + + return { + separateSystemMessageStr, + messages: newMessages, + devInfo, + } +} + + + + + +// const { maxTokens, supportsTools, supportsAutocompleteFIM, supportsStreaming, } = developerInfoOfModelName(recognizedModel) + + + + + + +// let index = 0; +// while (index < newMessages.length) { + +// merge tool with the previous assistant and the following user message + +// take prev message and add +/* +openai assistant message will have: https://platform.openai.com/docs/guides/function-calling#function-calling-steps +"tool_calls":[ +{ +"id": "call_12345xyz", +"type": "function", +"function": { +"name": "get_weather", +"arguments": "{\"latitude\":48.8566,\"longitude\":2.3522}" +} +}] + +openai user response will be: +{ +"role": "tool", +"tool_call_id": tool_call.id, +"content": str(result) +} + +anthropic assistant message will have: https://docs.anthropic.com/en/docs/build-with-claude/tool-use#tool-use-examples +"content": [ +{ +"type": "text", +"text": "I need to call the get_weather function, and the user wants SF, which is likely San Francisco, CA." +}, +{ +"type": "tool_use", +"id": "toolu_01A09q90qw90lq917835lq9", +"name": "get_weather", +"input": {"location": "San Francisco, CA", "unit": "celsius"} +} +] + +anthropic user message response will be: +"content": [ +{ +"type": "tool_result", +"tool_use_id": "toolu_01A09q90qw90lq917835lq9", +"content": "15 degrees" +} +] + + +*/ + + + +/* + + +ACCORDING TO 4o: gemini: similar to openai, but function_call, and only 1 call per message (no id because only 1 message) +gemini request: { +"role": "assistant", +"content": null, +"function_call": { +"name": "get_weather", +"arguments": { +"latitude": 48.8566, +"longitude": 2.3522 +} +} +} +gemini response: +{ +"role": "assistant", +"function_response": { +"name": "get_weather", +"response": { +"temperature": "15°C", +"condition": "Cloudy" +} +} +} + + ++ anthropic + ++ openai-compat (4) ++ gemini + +ollama + + +mistral: same as openai + +*/ diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts index b443cca1..77bbcb82 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts @@ -7,6 +7,7 @@ import Anthropic from '@anthropic-ai/sdk'; import { _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes.js'; import { anthropicMaxPossibleTokens } from '../../common/voidSettingsTypes.js'; import { InternalToolInfo } from '../../common/toolsService.js'; +import { addSystemMessageAndToolSupport } from './addSupport.js'; @@ -28,7 +29,7 @@ export const toAnthropicTool = (toolInfo: InternalToolInfo) => { -export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, tools }) => { +export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages: messages_, providerName, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, tools: tools_ }) => { const thisConfig = settingsOfProvider.anthropic @@ -38,15 +39,19 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages, return } + const { messages, separateSystemMessageStr, devInfo } = addSystemMessageAndToolSupport(modelName, providerName, messages_, { separateSystemMessage: true }) + const anthropic = new Anthropic({ apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true }); + const tools = devInfo?.supportsTools && (tools_?.length ?? 0) !== 0 ? tools_?.map(tool => toAnthropicTool(tool)) : undefined + const stream = anthropic.messages.stream({ - // system: systemMessage, + system: separateSystemMessageStr, messages: messages, model: modelName, max_tokens: maxTokens, - tools: tools?.map(tool => toAnthropicTool(tool)), - tool_choice: { type: 'auto', disable_parallel_tool_use: true } // one tool use at a time + tools: tools, + tool_choice: tools ? { type: 'auto', disable_parallel_tool_use: true } : undefined // one tool use at a time }) @@ -78,9 +83,9 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages, stream.on('finalMessage', (response) => { // stringify the response's content const content = response.content.map(c => c.type === 'text' ? c.text : '').join('\n\n') - const tools = response.content.map(c => c.type === 'tool_use' ? { name: c.name, args: JSON.stringify(c.input), tool_use_id: c.id } : null).filter(c => !!c) + // const tools = response.content.map(c => c.type === 'tool_use' ? { name: c.name, args: JSON.stringify(c.input), tool_use_id: c.id } : null).filter(c => !!c) - onFinalMessage({ fullText: content, tools }) + onFinalMessage({ fullText: content, tools: [] }) }) stream.on('error', (error) => { diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/gemini.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/gemini.ts deleted file mode 100644 index 2732fbe4..00000000 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/gemini.ts +++ /dev/null @@ -1,43 +0,0 @@ -/*-------------------------------------------------------------------------------------- - * Copyright 2025 Glass Devtools, Inc. All rights reserved. - * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. - *--------------------------------------------------------------------------------------*/ - -import { Content, GoogleGenerativeAI } from '@google/generative-ai'; -import { _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes.js'; - -// Gemini -export const sendGeminiChat: _InternalSendLLMChatMessageFnType = async ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter }) => { - - let fullText = '' - - const thisConfig = settingsOfProvider.gemini - - const genAI = new GoogleGenerativeAI(thisConfig.apiKey); - const model = genAI.getGenerativeModel({ model: modelName }); - - // Convert messages to Gemini format - const geminiMessages: Content[] = messages - .map((msg, i) => ({ - parts: [{ text: msg.content }], - role: msg.role === 'assistant' ? 'model' : 'user' - })) - - model.generateContentStream({ - // systemInstruction: systemMessage, - contents: geminiMessages, - }) - .then(async response => { - _setAborter(() => response.stream.return(fullText)) - - for await (const chunk of response.stream) { - const newText = chunk.text(); - fullText += newText; - onText({ newText, fullText }); - } - onFinalMessage({ fullText, tools: [] }); - }) - .catch((error) => { - onError({ message: error + '', fullError: error }) - }) -} diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/ollama.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/ollama.ts index daff3a29..da6715c0 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/ollama.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/ollama.ts @@ -1,3 +1,4 @@ + /*-------------------------------------------------------------------------------------- * Copyright 2025 Glass Devtools, Inc. All rights reserved. * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. @@ -38,84 +39,86 @@ export const ollamaList: _InternalModelListFnType = async ( } -export const sendOllamaFIM: _InternalSendLLMFIMMessageFnType = ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter }) => { - - const thisConfig = settingsOfProvider.ollama - // if endpoint is empty, normally ollama will send to 11434, but we want it to fail - the user should type it in - if (!thisConfig.endpoint) throw new Error(`Ollama Endpoint was empty (please enter ${defaultProviderSettings.ollama.endpoint} if you want the default).`) - - let fullText = '' - - const ollama = new Ollama({ host: thisConfig.endpoint }) - - ollama.generate({ - model: modelName, - prompt: messages.prefix, - suffix: messages.suffix, - options: { - stop: messages.stopTokens, - num_predict: 300, // max tokens - // repeat_penalty: 1, - }, - raw: true, - stream: true, - }) - .then(async stream => { - _setAborter(() => stream.abort()) - // iterate through the stream - for await (const chunk of stream) { - const newText = chunk.response; - fullText += newText; - onText({ newText, fullText }); - } - onFinalMessage({ fullText, tools: [] }); - }) - // when error/fail - .catch((error) => { - onError({ message: error + '', fullError: error }) - }) -}; -// Ollama -export const sendOllamaChat: _InternalSendLLMChatMessageFnType = ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter }) => { +// export const sendOllamaFIM: _InternalSendLLMFIMMessageFnType = ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter }) => { - const thisConfig = settingsOfProvider.ollama - // if endpoint is empty, normally ollama will send to 11434, but we want it to fail - the user should type it in - if (!thisConfig.endpoint) throw new Error(`Ollama Endpoint was empty (please enter ${defaultProviderSettings.ollama.endpoint} if you want the default).`) +// const thisConfig = settingsOfProvider.ollama +// // if endpoint is empty, normally ollama will send to 11434, but we want it to fail - the user should type it in +// if (!thisConfig.endpoint) throw new Error(`Ollama Endpoint was empty (please enter ${defaultProviderSettings.ollama.endpoint} if you want the default).`) - let fullText = '' +// let fullText = '' - const ollama = new Ollama({ host: thisConfig.endpoint }) +// const ollama = new Ollama({ host: thisConfig.endpoint }) - ollama.chat({ - model: modelName, - messages: messages, - stream: true, - // options: { num_predict: parseMaxTokensStr(thisConfig.maxTokens) } // this is max_tokens - }) - .then(async stream => { - _setAborter(() => stream.abort()) - // iterate through the stream - for await (const chunk of stream) { - const newText = chunk.message.content; +// ollama.generate({ +// model: modelName, +// prompt: messages.prefix, +// suffix: messages.suffix, +// options: { +// stop: messages.stopTokens, +// num_predict: 300, // max tokens +// // repeat_penalty: 1, +// }, +// raw: true, +// stream: true, +// }) +// .then(async stream => { +// _setAborter(() => stream.abort()) +// // iterate through the stream +// for await (const chunk of stream) { +// const newText = chunk.response; +// fullText += newText; +// onText({ newText, fullText }); +// } +// onFinalMessage({ fullText, tools: [] }); +// }) +// // when error/fail +// .catch((error) => { +// onError({ message: error + '', fullError: error }) +// }) +// }; - // chunk.message.tool_calls[0].function.arguments - fullText += newText; - onText({ newText, fullText }); - } +// // Ollama +// export const sendOllamaChat: _InternalSendLLMChatMessageFnType = ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter }) => { - onFinalMessage({ fullText, tools: [] }); +// const thisConfig = settingsOfProvider.ollama +// // if endpoint is empty, normally ollama will send to 11434, but we want it to fail - the user should type it in +// if (!thisConfig.endpoint) throw new Error(`Ollama Endpoint was empty (please enter ${defaultProviderSettings.ollama.endpoint} if you want the default).`) - }) - // when error/fail - .catch((error) => { - onError({ message: error + '', fullError: error }) - }) +// let fullText = '' -}; +// const ollama = new Ollama({ host: thisConfig.endpoint }) + +// ollama.chat({ +// model: modelName, +// messages: messages, +// stream: true, +// // options: { num_predict: parseMaxTokensStr(thisConfig.maxTokens) } // this is max_tokens +// }) +// .then(async stream => { +// _setAborter(() => stream.abort()) +// // iterate through the stream +// for await (const chunk of stream) { +// const newText = chunk.message.content; + +// // chunk.message.tool_calls[0].function.arguments + +// fullText += newText; +// onText({ newText, fullText }); +// } + +// onFinalMessage({ fullText, tools: [] }); + +// }) +// // when error/fail +// .catch((error) => { +// onError({ message: error + '', fullError: error }) +// }) + +// }; -// ['codestral', 'qwen2.5-coder', 'qwen2.5-coder:0.5b', 'qwen2.5-coder:1.5b', 'qwen2.5-coder:3b', 'qwen2.5-coder:7b', 'qwen2.5-coder:14b', 'qwen2.5-coder:32b', 'codegemma', 'codegemma:2b', 'codegemma:7b', 'codellama', 'codellama:7b', 'codellama:13b', 'codellama:34b', 'codellama:70b', 'codellama:code', 'codellama:python', 'command-r', 'command-r:35b', 'command-r-plus', 'command-r-plus:104b', 'deepseek-coder-v2', 'deepseek-coder-v2:16b', 'deepseek-coder-v2:236b', 'falcon2', 'falcon2:11b', 'firefunction-v2', 'firefunction-v2:70b', 'gemma', 'gemma:2b', 'gemma:7b', 'gemma2', 'gemma2:2b', 'gemma2:9b', 'gemma2:27b', 'llama2', 'llama2:7b', 'llama2:13b', 'llama2:70b', 'llama3', 'llama3:8b', 'llama3:70b', 'llama3-chatqa', 'llama3-chatqa:8b', 'llama3-chatqa:70b', 'llama3-gradient', 'llama3-gradient:8b', 'llama3-gradient:70b', 'llama3.1', 'llama3.1:8b', 'llama3.1:70b', 'llama3.1:405b', 'llava', 'llava:7b', 'llava:13b', 'llava:34b', 'llava-llama3', 'llava-llama3:8b', 'llava-phi3', 'llava-phi3:3.8b', 'mistral', 'mistral:7b', 'mistral-large', 'mistral-large:123b', 'mistral-nemo', 'mistral-nemo:12b', 'mixtral', 'mixtral:8x7b', 'mixtral:8x22b', 'moondream', 'moondream:1.8b', 'openhermes', 'openhermes:v2.5', 'phi3', 'phi3:3.8b', 'phi3:14b', 'phi3.5', 'phi3.5:3.8b', 'qwen', 'qwen:7b', 'qwen:14b', 'qwen:32b', 'qwen:72b', 'qwen:110b', 'qwen2', 'qwen2:0.5b', 'qwen2:1.5b', 'qwen2:7b', 'qwen2:72b', 'smollm', 'smollm:135m', 'smollm:360m', 'smollm:1.7b',] +// // ['codestral', 'qwen2.5-coder', 'qwen2.5-coder:0.5b', 'qwen2.5-coder:1.5b', 'qwen2.5-coder:3b', 'qwen2.5-coder:7b', 'qwen2.5-coder:14b', 'qwen2.5-coder:32b', 'codegemma', 'codegemma:2b', 'codegemma:7b', 'codellama', 'codellama:7b', 'codellama:13b', 'codellama:34b', 'codellama:70b', 'codellama:code', 'codellama:python', 'command-r', 'command-r:35b', 'command-r-plus', 'command-r-plus:104b', 'deepseek-coder-v2', 'deepseek-coder-v2:16b', 'deepseek-coder-v2:236b', 'falcon2', 'falcon2:11b', 'firefunction-v2', 'firefunction-v2:70b', 'gemma', 'gemma:2b', 'gemma:7b', 'gemma2', 'gemma2:2b', 'gemma2:9b', 'gemma2:27b', 'llama2', 'llama2:7b', 'llama2:13b', 'llama2:70b', 'llama3', 'llama3:8b', 'llama3:70b', 'llama3-chatqa', 'llama3-chatqa:8b', 'llama3-chatqa:70b', 'llama3-gradient', 'llama3-gradient:8b', 'llama3-gradient:70b', 'llama3.1', 'llama3.1:8b', 'llama3.1:70b', 'llama3.1:405b', 'llava', 'llava:7b', 'llava:13b', 'llava:34b', 'llava-llama3', 'llava-llama3:8b', 'llava-phi3', 'llava-phi3:3.8b', 'mistral', 'mistral:7b', 'mistral-large', 'mistral-large:123b', 'mistral-nemo', 'mistral-nemo:12b', 'mixtral', 'mixtral:8x7b', 'mixtral:8x22b', 'moondream', 'moondream:1.8b', 'openhermes', 'openhermes:v2.5', 'phi3', 'phi3:3.8b', 'phi3:14b', 'phi3.5', 'phi3.5:3.8b', 'qwen', 'qwen:7b', 'qwen:14b', 'qwen:32b', 'qwen:72b', 'qwen:110b', 'qwen2', 'qwen2:0.5b', 'qwen2:1.5b', 'qwen2:7b', 'qwen2:72b', 'smollm', 'smollm:135m', 'smollm:360m', 'smollm:1.7b',] diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts index 370d411a..b7c81563 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts @@ -7,6 +7,7 @@ import OpenAI from 'openai'; import { _InternalModelListFnType, _InternalSendLLMFIMMessageFnType, _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes.js'; import { Model } from 'openai/resources/models.js'; import { InternalToolInfo } from '../../common/toolsService.js'; +import { addSystemMessageAndToolSupport } from './addSupport.js'; // import { parseMaxTokensStr } from './util.js'; @@ -38,11 +39,19 @@ type NewParams = Pick[0] & Paramet const newOpenAI = ({ settingsOfProvider, providerName }: NewParams) => { if (providerName === 'openAI') { - const thisConfig = settingsOfProvider.openAI - return new OpenAI({ apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true }); + const thisConfig = settingsOfProvider[providerName] + return new OpenAI({ + apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true + }) + } + else if (providerName === 'ollama') { + const thisConfig = settingsOfProvider[providerName] + return new OpenAI({ + baseURL: `${thisConfig.endpoint}/v1`, apiKey: 'noop', dangerouslyAllowBrowser: true, + }) } else if (providerName === 'openRouter') { - const thisConfig = settingsOfProvider.openRouter + const thisConfig = settingsOfProvider[providerName] return new OpenAI({ baseURL: 'https://openrouter.ai/api/v1', apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true, defaultHeaders: { @@ -51,33 +60,38 @@ const newOpenAI = ({ settingsOfProvider, providerName }: NewParams) => { }, }) } + else if (providerName === 'gemini') { + const thisConfig = settingsOfProvider[providerName] + return new OpenAI({ + baseURL: 'https://generativelanguage.googleapis.com/v1beta/openai', apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true, + }) + } else if (providerName === 'deepseek') { - const thisConfig = settingsOfProvider.deepseek + const thisConfig = settingsOfProvider[providerName] return new OpenAI({ baseURL: 'https://api.deepseek.com/v1', apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true, }) - } else if (providerName === 'openAICompatible') { - const thisConfig = settingsOfProvider.openAICompatible + const thisConfig = settingsOfProvider[providerName] return new OpenAI({ - baseURL: thisConfig.endpoint, apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true + baseURL: thisConfig.endpoint, apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true, }) } else if (providerName === 'mistral') { - const thisConfig = settingsOfProvider.mistral + const thisConfig = settingsOfProvider[providerName] return new OpenAI({ baseURL: 'https://api.mistral.ai/v1', apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true, }) } else if (providerName === 'groq') { - const thisConfig = settingsOfProvider.groq + const thisConfig = settingsOfProvider[providerName] return new OpenAI({ - baseURL: '"https://api.groq.com/openai/v1"', apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true, + baseURL: 'https://api.groq.com/openai/v1', apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true, }) } else { - console.error(`sendOpenAIMsg: invalid providerName: ${providerName}`) + console.error(`sendOpenAICompatibleMsg: invalid providerName: ${providerName}`) throw new Error(`providerName was invalid: ${providerName}`) } } @@ -130,10 +144,14 @@ export const sendOpenAIFIM: _InternalSendLLMFIMMessageFnType = ({ messages, onTe // OpenAI, OpenRouter, OpenAICompatible -export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }) => { +export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools: tools_ }) => { let fullText = '' - const toolCallOfIndex: { [index: string]: { name: string, args: string } } = {} + const toolCallOfIndex: { [index: string]: { name: string, args: string, id: string } } = {} + + const { messages, devInfo } = addSystemMessageAndToolSupport(modelName, providerName, messages_, { separateSystemMessage: false }) + + const tools = devInfo?.supportsTools && (tools_?.length ?? 0) !== 0 ? tools_?.map(tool => toOpenAITool(tool)) : undefined const openai: OpenAI = newOpenAI({ providerName, settingsOfProvider }) @@ -141,7 +159,9 @@ export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages, on model: modelName, messages: messages, stream: true, - tools: tools?.map(tool => toOpenAITool(tool)), + tools: tools, + tool_choice: tools ? 'auto' : undefined, + parallel_tool_calls: tools ? false : undefined, } openai.chat.completions @@ -155,9 +175,11 @@ export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages, on // tool call for (const tool of chunk.choices[0]?.delta?.tool_calls ?? []) { const index = tool.index - if (!toolCallOfIndex[index]) toolCallOfIndex[index] = { name: '', args: '' } + if (!toolCallOfIndex[index]) toolCallOfIndex[index] = { name: '', args: '', id: '' } toolCallOfIndex[index].name += tool.function?.name ?? '' - toolCallOfIndex[index].args += tool.function?.arguments ?? '' + toolCallOfIndex[index].args += tool.function?.arguments ?? ''; + toolCallOfIndex[index].id = tool.id ?? '' + } // message diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts index 22255292..dec84841 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts @@ -3,197 +3,12 @@ * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. *--------------------------------------------------------------------------------------*/ -import { SendLLMMessageParams, OnText, OnFinalMessage, OnError, LLMChatMessage, _InternalLLMChatMessage } from '../../common/llmMessageTypes.js'; +import { SendLLMMessageParams, OnText, OnFinalMessage, OnError, _InternalLLMChatMessage } from '../../common/llmMessageTypes.js'; import { IMetricsService } from '../../common/metricsService.js'; +import { displayInfoOfProviderName } from '../../common/voidSettingsTypes.js'; import { sendAnthropicChat } from './anthropic.js'; -import { sendOllamaFIM, sendOllamaChat } from './ollama.js'; import { sendOpenAIChat } from './openai.js'; -import { sendGeminiChat } from './gemini.js'; -import { developerInfoOfModelName, developerInfoOfProviderName, displayInfoOfProviderName, ProviderName, recognizedModelOfModelName } from '../../common/voidSettingsTypes.js'; - - -const cleanChatMessages = (modelName: string, providerName: ProviderName, messages: LLMChatMessage[]): { separateSystemMessageStr?: string, messages: _InternalLLMChatMessage[] } => { - const recognizedModel = recognizedModelOfModelName(modelName) - const { separateSystemMessage, toolsGoInRole, modelOverrides } = developerInfoOfProviderName(providerName) - - const { supportsSystemMessage, maxTokens, /* supportsTools, supportsAutocompleteFIM, supportsStreaming */ } = developerInfoOfModelName(recognizedModel, modelOverrides) - - - // trim message content (Anthropic and other providers give an error if there is trailing whitespace) - messages = messages.map(m => ({ ...m, content: m.content.trim() })) - - - // 1. SYSTEM MESSAGE - // find system messages and concatenate them - const systemMessageStr = messages - .filter(msg => msg.role === 'system') - .map(msg => msg.content) - .join('\n') || undefined; - - let separateSystemMessageStr = undefined - - // remove all system messages - const noSystemMessages: _InternalLLMChatMessage[] = messages.filter(msg => msg.role !== 'system') - - if (systemMessageStr) { - // if supports system message - if (supportsSystemMessage) { - if (separateSystemMessage) - separateSystemMessageStr = systemMessageStr - else { - noSystemMessages.unshift({ role: supportsSystemMessage, content: systemMessageStr }) // add new first message - } - } - // if does not support system message - else { - if (supportsSystemMessage) { - if (noSystemMessages.length === 0) - noSystemMessages.push({ role: 'user', content: systemMessageStr }) - // add system mesasges to first message (should be a user message) - else { - const newFirstMessage = { - role: noSystemMessages[0].role, - content: ('' - + '\n' - + systemMessageStr - + '\n' - + '\n' - + noSystemMessages[0].content - ) - } - noSystemMessages.splice(0, 1) // delete first message - noSystemMessages.unshift(newFirstMessage) // add new first message - } - } - } - } - - // 2. TOOLS - - const newMessages = noSystemMessages; - - if (toolsGoInRole) { - let index = 0; - while (index < newMessages.length) { - - // merge tool with the previous assistant and the following user message - - // take prev message and add - /* -openai assistant message will have: https://platform.openai.com/docs/guides/function-calling#function-calling-steps -"tool_calls":[ -{ - "id": "call_12345xyz", - "type": "function", - "function": { - "name": "get_weather", - "arguments": "{\"latitude\":48.8566,\"longitude\":2.3522}" - } -}] - -openai user response will be: -{ - "role": "tool", - "tool_call_id": tool_call.id, - "content": str(result) -} - -anthropic assistant message will have: https://docs.anthropic.com/en/docs/build-with-claude/tool-use#tool-use-examples -"content": [ - { - "type": "text", - "text": "I need to call the get_weather function, and the user wants SF, which is likely San Francisco, CA." - }, - { - "type": "tool_use", - "id": "toolu_01A09q90qw90lq917835lq9", - "name": "get_weather", - "input": {"location": "San Francisco, CA", "unit": "celsius"} - } - ] - -anthropic user message response will be: -"content": [ - { - "type": "tool_result", - "tool_use_id": "toolu_01A09q90qw90lq917835lq9", - "content": "15 degrees" - } -] - - -ACCORDING TO 4o: gemini: similar to openai, but function_call, and only 1 call per message (no id because only 1 message) -gemini request: { - "role": "assistant", - "content": null, - "function_call": { - "name": "get_weather", - "arguments": { - "latitude": 48.8566, - "longitude": 2.3522 - } - } -} -gemini response: -{ - "role": "assistant", - "function_response": { - "name": "get_weather", - "response": { - "temperature": "15°C", - "condition": "Cloudy" - } - } -} - - -+ anthropic - -+ openai-compat (4) - + gemini - -ollama - - -mistral: same as openai - - */ - - - if (newMessages[index].role === 'tool') { - const toolMessage = newMessages[index]; - const assistantMessage = newMessages[index - 1]; - const userMessage = newMessages[index + 1]; - - // while ((toolIndex = newMessages.findIndex((msg, idx) => idx > toolIndex && msg.role === 'tool')) !== -1) { - - // tool_use goes in assistant - if (assistantMessage?.role === 'assistant') { - assistantMessage.tool_use += `\n${toolMessage.content}`; - } - - // tool_result goes in user - if (userMessage?.role === 'user') { - - userMessage.content = `${toolMessage.content}\n${userMessage.content}`; - } - - // Remove the tool message after merging its content - newMessages.splice(index, 1); - } else { - index++; - } - } - } - - - - return { - separateSystemMessageStr, - messages: newMessages - } -} export const sendLLMMessage = ({ @@ -214,16 +29,6 @@ export const sendLLMMessage = ({ metricsService: IMetricsService ) => { - let messagesArr: _InternalLLMChatMessage[] = [] - - // TODO!!! move this to the actual providers - if (messagesType === 'chatMessages') { - const { messages: cleanedMessages, separateSystemMessageStr } = cleanChatMessages(modelName, providerName, [ - { role: 'system', content: aiInstructions }, - ...messages_ - ]) - messagesArr = cleanedMessages - } // only captures number of messages and message "shape", no actual code, instructions, prompts, etc const captureLLMEvent = (eventId: string, extras?: object) => { @@ -231,8 +36,8 @@ export const sendLLMMessage = ({ providerName, modelName, ...messagesType === 'chatMessages' ? { - numMessages: messagesArr?.length, - messagesShape: messagesArr?.map(msg => ({ role: msg.role, length: msg.content.length })), + numMessages: messages_?.length, + messagesShape: messages_?.map(msg => ({ role: msg.role, length: msg.content.length })), origNumMessages: messages_?.length, origMessagesShape: messages_?.map(msg => ({ role: msg.role, length: msg.content.length })), @@ -283,7 +88,10 @@ export const sendLLMMessage = ({ } abortRef_.current = onAbort - captureLLMEvent(`${loggingName} - Sending Message`, { messageLength: messagesArr[messagesArr.length - 1]?.content.length }) + if (messagesType === 'chatMessages') + captureLLMEvent(`${loggingName} - Sending Message`, { messageLength: messages_[messages_.length - 1]?.content.length }) + else if (messagesType === 'FIMMessage') + captureLLMEvent(`${loggingName} - Sending FIM`, {}) // TODO!!! add more metrics try { switch (providerName) { @@ -292,21 +100,15 @@ export const sendLLMMessage = ({ case 'deepseek': case 'openAICompatible': case 'mistral': - case 'groq': - if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - OpenAI FIM', tools: [] }) - else /* */ sendOpenAIChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); - break; case 'ollama': - if (messagesType === 'FIMMessage') sendOllamaFIM({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName }); - else /* */ sendOllamaChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); + case 'groq': + case 'gemini': + if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - OpenAI FIM', tools: [] }) + else /* */ sendOpenAIChat({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; case 'anthropic': if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Anthropic FIM', tools: [] }) - else /* */ sendAnthropicChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); - break; - case 'gemini': - if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Gemini FIM', tools: [] }) - else /* */ sendGeminiChat({ messages: messagesArr, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); + else /* */ sendAnthropicChat({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); break; default: onError({ message: `Error: Void provider was "${providerName}", which is not recognized.`, fullError: null }) From 7244d433dda9cf874d2e2e636782e9971997b455 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Sun, 16 Feb 2025 02:53:56 -0800 Subject: [PATCH 21/47] tools should work! --- .../contrib/void/browser/chatThreadService.ts | 15 +- .../contrib/void/browser/editCodeService.ts | 5 +- .../react/src/void-settings-tsx/Settings.tsx | 2 +- .../contrib/void/common/llmMessageTypes.ts | 17 +- .../electron-main/llmMessage/addSupport.ts | 177 ----------- .../electron-main/llmMessage/anthropic.ts | 6 +- .../void/electron-main/llmMessage/openai.ts | 6 +- .../llmMessage/processMessages.ts | 294 ++++++++++++++++++ .../llmMessage/sendLLMMessage.ts | 6 +- 9 files changed, 322 insertions(+), 206 deletions(-) delete mode 100644 src/vs/workbench/contrib/void/electron-main/llmMessage/addSupport.ts create mode 100644 src/vs/workbench/contrib/void/electron-main/llmMessage/processMessages.ts diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index ec1719b8..d3dedf84 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -14,7 +14,7 @@ import { IRange } from '../../../../editor/common/core/range.js'; import { ILLMMessageService } from '../common/llmMessageService.js'; import { IModelService } from '../../../../editor/common/services/model.js'; import { chat_userMessage, chat_systemMessage } from './prompt/prompts.js'; -import { IToolsService, ToolName, voidTools } from '../common/toolsService.js'; +import { InternalToolInfo, IToolsService, ToolName, voidTools } from '../common/toolsService.js'; import { toLLMChatMessage } from '../common/llmMessageTypes.js'; // one of the square items that indicates a selection in a chat bubble (NOT a file, a Selection of text) @@ -66,10 +66,10 @@ export type ChatMessage = | { role: 'tool'; name: string; // internal use - params: string | null; // internal use + params: string; // internal use id: string; // apis require this tool use id - content: string | null; // summary of the tool to the LLM - displayContent: string | null; // text message of result + content: string; // result + displayContent: string; // text message of result } // a 'thread' means a chat message history @@ -296,6 +296,10 @@ class ChatThreadService extends Disposable implements IChatThreadService { this._setStreamState(threadId, { error: undefined }) + const tools: InternalToolInfo[] | undefined = ( + chatMode === 'chat' ? undefined + : chatMode === 'agent' ? Object.keys(voidTools).map(toolName => voidTools[toolName as ToolName]) + : undefined) // agent loop const agentLoop = async () => { @@ -316,8 +320,7 @@ class ChatThreadService extends Disposable implements IChatThreadService { ...this.getCurrentThread().messages.map(m => (toLLMChatMessage(m))), ], - // TODO!!!!! make this change on 'agent' | 'chat' - tools: Object.keys(voidTools).map(toolName => voidTools[toolName as ToolName]), + tools: tools, onText: ({ fullText }) => { this._setStreamState(threadId, { messageSoFar: fullText }) diff --git a/src/vs/workbench/contrib/void/browser/editCodeService.ts b/src/vs/workbench/contrib/void/browser/editCodeService.ts index c2828084..0138cfc1 100644 --- a/src/vs/workbench/contrib/void/browser/editCodeService.ts +++ b/src/vs/workbench/contrib/void/browser/editCodeService.ts @@ -1261,6 +1261,7 @@ class EditCodeService extends Disposable implements IEditCodeService { if (!(blockNum in diffareaidOfBlockNum)) { const foundInCode = findTextInCode(block.orig, fileContents) if (typeof foundInCode === 'string') { + // TODO!!! log and retry console.log('NOT FOUND IN CODE!!!!', foundInCode) continue } @@ -1305,7 +1306,7 @@ class EditCodeService extends Disposable implements IEditCodeService { this._refreshStylesAndDiffsInURI(uri) }, onFinalMessage: async ({ fullText }) => { - console.log('/* ONFIN */', fullText) + console.log('/* ON FINALMESSAGE */', fullText) // 1. wait 500ms and fix lint errors - call lint error workflow // (update react state to say "Fixing errors") @@ -1325,8 +1326,6 @@ class EditCodeService extends Disposable implements IEditCodeService { onDone(false) }, onError: (e) => { - console.log('/* ERRRRRR */') - console.log('ERROR', e); onDone(true) }, diff --git a/src/vs/workbench/contrib/void/browser/react/src/void-settings-tsx/Settings.tsx b/src/vs/workbench/contrib/void/browser/react/src/void-settings-tsx/Settings.tsx index b1079587..cbf8607c 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/void-settings-tsx/Settings.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/void-settings-tsx/Settings.tsx @@ -385,7 +385,7 @@ export const AIInstructionsBox = () => { return { voidSettingsService.setGlobalSetting('aiInstructions', newText) diff --git a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts index 0af2e31e..68aafa64 100644 --- a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts +++ b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts @@ -32,12 +32,13 @@ export type LLMChatMessage = { content: string; } | { role: 'assistant', - tool_calls?: { name: string, id: string, params: string }[]; content: string; } | { role: 'tool'; + content: string; // result + name: string; + params: string; id: string; - content: string; } export const toLLMChatMessage = (c: ChatMessage): LLMChatMessage => { @@ -45,21 +46,15 @@ export const toLLMChatMessage = (c: ChatMessage): LLMChatMessage => { return { role: c.role, content: c.content ?? '(empty)' } } else if (c.role === 'assistant') - return { role: c.role, tool_calls: c.tool_calls, content: c.content ?? '(empty model output)' } + return { role: c.role, content: c.content ?? '(empty model output)' } else if (c.role === 'tool') - return { role: c.role, id: c.id, content: c.content ?? '(empty output)' } + return { role: c.role, id: c.id, name: c.name, params: c.params, content: c.content ?? '(empty output)' } else { throw 1 } } -export type _InternalLLMChatMessage = { - role: any; - id?: any; - content: string; -} - type _InternalSendFIMMessage = { prefix: string; suffix: string; @@ -115,6 +110,8 @@ export type EventLLMMessageOnErrorParams = Parameters[0] & { requestId: export type _InternalSendLLMChatMessageFnType = ( params: { + aiInstructions: string; + onText: OnText; onFinalMessage: OnFinalMessage; onError: OnError; diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/addSupport.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/addSupport.ts deleted file mode 100644 index ef9dfdd5..00000000 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/addSupport.ts +++ /dev/null @@ -1,177 +0,0 @@ -import { _InternalLLMChatMessage, LLMChatMessage } from '../../common/llmMessageTypes.js'; -import { DeveloperInfoAtModel, developerInfoOfModelName, developerInfoOfProviderName, ProviderName } from '../../common/voidSettingsTypes.js'; - - -// no matter whether the model supports a system message or not (or what format it supports), add it in some way -// also take into account tools if the model doesn't support tool use -export const addSystemMessageAndToolSupport = (modelName: string, providerName: ProviderName, messages_: LLMChatMessage[], { separateSystemMessage }: { separateSystemMessage: boolean }): { separateSystemMessageStr?: string, messages: _InternalLLMChatMessage[], devInfo: DeveloperInfoAtModel } => { - - const messages: _InternalLLMChatMessage[] = messages_.map(m => ({ ...m, content: m.content.trim(), })) - - const { overrideSettingsForAllModels } = developerInfoOfProviderName(providerName) - const devInfo = developerInfoOfModelName(modelName, overrideSettingsForAllModels) - const { supportsSystemMessage } = devInfo - - // 1. SYSTEM MESSAGE - // find system messages and concatenate them - let systemMessageStr = messages - .filter(msg => msg.role === 'system') - .map(msg => msg.content) - .join('\n') || undefined; - - let separateSystemMessageStr: string | undefined = undefined - - // remove all system messages - const newMessages: _InternalLLMChatMessage[] = messages.filter(msg => msg.role !== 'system') - - - // if (!supportsTools) { - // if (!systemMessageStr) systemMessageStr = '' - // systemMessageStr += '' // TODO!!! add tool use system message here - // } - - - if (systemMessageStr) { - // if supports system message - if (supportsSystemMessage) { - if (separateSystemMessage) - separateSystemMessageStr = systemMessageStr - else { - newMessages.unshift({ role: supportsSystemMessage, content: systemMessageStr }) // add new first message - } - } - // if does not support system message - else { - if (supportsSystemMessage) { - if (newMessages.length === 0) - newMessages.push({ role: 'user', content: systemMessageStr }) - // add system mesasges to first message (should be a user message) - else { - const newFirstMessage = { - role: newMessages[0].role, - content: ('' - + '\n' - + systemMessageStr - + '\n' - + '\n' - + newMessages[0].content - ) - } - newMessages.splice(0, 1) // delete first message - newMessages.unshift(newFirstMessage) // add new first message - } - } - } - } - - - return { - separateSystemMessageStr, - messages: newMessages, - devInfo, - } -} - - - - - -// const { maxTokens, supportsTools, supportsAutocompleteFIM, supportsStreaming, } = developerInfoOfModelName(recognizedModel) - - - - - - -// let index = 0; -// while (index < newMessages.length) { - -// merge tool with the previous assistant and the following user message - -// take prev message and add -/* -openai assistant message will have: https://platform.openai.com/docs/guides/function-calling#function-calling-steps -"tool_calls":[ -{ -"id": "call_12345xyz", -"type": "function", -"function": { -"name": "get_weather", -"arguments": "{\"latitude\":48.8566,\"longitude\":2.3522}" -} -}] - -openai user response will be: -{ -"role": "tool", -"tool_call_id": tool_call.id, -"content": str(result) -} - -anthropic assistant message will have: https://docs.anthropic.com/en/docs/build-with-claude/tool-use#tool-use-examples -"content": [ -{ -"type": "text", -"text": "I need to call the get_weather function, and the user wants SF, which is likely San Francisco, CA." -}, -{ -"type": "tool_use", -"id": "toolu_01A09q90qw90lq917835lq9", -"name": "get_weather", -"input": {"location": "San Francisco, CA", "unit": "celsius"} -} -] - -anthropic user message response will be: -"content": [ -{ -"type": "tool_result", -"tool_use_id": "toolu_01A09q90qw90lq917835lq9", -"content": "15 degrees" -} -] - - -*/ - - - -/* - - -ACCORDING TO 4o: gemini: similar to openai, but function_call, and only 1 call per message (no id because only 1 message) -gemini request: { -"role": "assistant", -"content": null, -"function_call": { -"name": "get_weather", -"arguments": { -"latitude": 48.8566, -"longitude": 2.3522 -} -} -} -gemini response: -{ -"role": "assistant", -"function_response": { -"name": "get_weather", -"response": { -"temperature": "15°C", -"condition": "Cloudy" -} -} -} - - -+ anthropic - -+ openai-compat (4) -+ gemini - -ollama - - -mistral: same as openai - -*/ diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts index 77bbcb82..b047a601 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts @@ -7,7 +7,7 @@ import Anthropic from '@anthropic-ai/sdk'; import { _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes.js'; import { anthropicMaxPossibleTokens } from '../../common/voidSettingsTypes.js'; import { InternalToolInfo } from '../../common/toolsService.js'; -import { addSystemMessageAndToolSupport } from './addSupport.js'; +import { addSystemMessageAndToolSupport } from './processMessages.js'; @@ -29,7 +29,7 @@ export const toAnthropicTool = (toolInfo: InternalToolInfo) => { -export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages: messages_, providerName, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, tools: tools_ }) => { +export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages: messages_, providerName, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, aiInstructions, tools: tools_ }) => { const thisConfig = settingsOfProvider.anthropic @@ -39,7 +39,7 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages: return } - const { messages, separateSystemMessageStr, devInfo } = addSystemMessageAndToolSupport(modelName, providerName, messages_, { separateSystemMessage: true }) + const { messages, separateSystemMessageStr, devInfo } = addSystemMessageAndToolSupport(modelName, providerName, messages_, aiInstructions, { separateSystemMessage: true }) const anthropic = new Anthropic({ apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true }); diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts index b7c81563..60a66881 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts @@ -7,7 +7,7 @@ import OpenAI from 'openai'; import { _InternalModelListFnType, _InternalSendLLMFIMMessageFnType, _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes.js'; import { Model } from 'openai/resources/models.js'; import { InternalToolInfo } from '../../common/toolsService.js'; -import { addSystemMessageAndToolSupport } from './addSupport.js'; +import { addSystemMessageAndToolSupport } from './processMessages.js'; // import { parseMaxTokensStr } from './util.js'; @@ -144,12 +144,12 @@ export const sendOpenAIFIM: _InternalSendLLMFIMMessageFnType = ({ messages, onTe // OpenAI, OpenRouter, OpenAICompatible -export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools: tools_ }) => { +export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, aiInstructions, tools: tools_ }) => { let fullText = '' const toolCallOfIndex: { [index: string]: { name: string, args: string, id: string } } = {} - const { messages, devInfo } = addSystemMessageAndToolSupport(modelName, providerName, messages_, { separateSystemMessage: false }) + const { messages, devInfo } = addSystemMessageAndToolSupport(modelName, providerName, messages_, aiInstructions, { separateSystemMessage: false }) const tools = devInfo?.supportsTools && (tools_?.length ?? 0) !== 0 ? tools_?.map(tool => toOpenAITool(tool)) : undefined diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/processMessages.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/processMessages.ts new file mode 100644 index 00000000..f6f6cd5e --- /dev/null +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/processMessages.ts @@ -0,0 +1,294 @@ + + +import { LLMChatMessage } from '../../common/llmMessageTypes.js'; +import { DeveloperInfoAtModel, developerInfoOfModelName, developerInfoOfProviderName, ProviderName } from '../../common/voidSettingsTypes.js'; +import { deepClone } from '../../../../../base/common/objects.js'; + + + + +// no matter whether the model supports a system message or not (or what format it supports), add it in some way +// also take into account tools if the model doesn't support tool use +export const addSystemMessageAndToolSupport = (modelName: string, providerName: ProviderName, messages_: LLMChatMessage[], aiInstructions: string, { separateSystemMessage }: { separateSystemMessage: boolean }): { separateSystemMessageStr?: string, messages: any[], devInfo: DeveloperInfoAtModel } => { + + const messages = deepClone(messages_).map(m => ({ ...m, content: m.content.trim(), })) + + const { overrideSettingsForAllModels } = developerInfoOfProviderName(providerName) + const devInfo = developerInfoOfModelName(modelName, overrideSettingsForAllModels) + const { supportsSystemMessage, supportsTools } = devInfo + + // 1. SYSTEM MESSAGE + // find system messages and concatenate them + let systemMessageStr = messages + .filter(msg => msg.role === 'system') + .map(msg => msg.content) + .join('\n') || undefined; + + if (aiInstructions) + systemMessageStr = `${(systemMessageStr ? `${systemMessageStr}\n\n` : '')}GUIDELINES\n${aiInstructions}` + + + let separateSystemMessageStr: string | undefined = undefined + + // remove all system messages + const newMessages: (LLMChatMessage | { role: 'developer', content: string })[] = messages.filter(msg => msg.role !== 'system') + + + // if (!supportsTools) { + // if (!systemMessageStr) systemMessageStr = '' + // systemMessageStr += '' // TODO!!! add tool use system message here + // } + + + if (systemMessageStr) { + // if supports system message + if (supportsSystemMessage) { + if (separateSystemMessage) + separateSystemMessageStr = systemMessageStr + else { + newMessages.unshift({ role: supportsSystemMessage, content: systemMessageStr }) // add new first message + } + } + // if does not support system message + else { + if (supportsSystemMessage) { + if (newMessages.length === 0) + newMessages.push({ role: 'user', content: systemMessageStr }) + // add system mesasges to first message (should be a user message) + else { + const newFirstMessage = { + role: 'user', + content: ('' + + '\n' + + systemMessageStr + + '\n' + + '\n' + + newMessages[0].content + ) + } as const + newMessages.splice(0, 1) // delete first message + newMessages.unshift(newFirstMessage) // add new first message + } + } + } + } + + + + + + + // 2. MAKE TOOLS FORMAT CORRECT in messages + let finalMessages: any[] + if (!supportsTools) { + // do nothing + finalMessages = newMessages + } + + // anthropic assistant message will have: https://docs.anthropic.com/en/docs/build-with-claude/tool-use#tool-use-examples + // "content": [ + // { + // "type": "text", + // "text": "I need to call the get_weather function, and the user wants SF, which is likely San Francisco, CA." + // }, + // { + // "type": "tool_use", + // "id": "toolu_01A09q90qw90lq917835lq9", + // "name": "get_weather", + // "input": { "location": "San Francisco, CA", "unit": "celsius" } + // } + // ] + + // anthropic user message response will be: + // "content": [ + // { + // "type": "tool_result", + // "tool_use_id": "toolu_01A09q90qw90lq917835lq9", + // "content": "15 degrees" + // } + // ] + + + else if (providerName === 'anthropic') { // convert role:'tool' to anthropic's type + const newMessagesTools: ( + Exclude | { + role: 'assistant', + content: string | ({ + type: 'text'; + text: string; + } | { + type: 'tool_use'; + name: string; + input: string; + id: string; + })[] + } | { + role: 'user', + content: string | ({ + type: 'text'; + text: string; + } | { + type: 'tool_response'; + tool_use_id: string; + content: string; + })[] + } + )[] = newMessages; + + + for (let i = 0; i < newMessagesTools.length; i += 1) { + const currMsg = newMessagesTools[i] + + if (currMsg.role !== 'tool') continue + + const prevMsg = 0 <= i - 1 && i - 1 <= newMessagesTools.length ? newMessagesTools[i - 1] : undefined + const nextMsg = 0 <= i + 1 && i + 1 <= newMessagesTools.length ? newMessagesTools[i + 1] : undefined + + if (prevMsg?.role === 'assistant') { + if (typeof prevMsg.content === 'string') prevMsg.content = [{ type: 'text', text: typeof prevMsg.content }] + prevMsg.content.push({ type: 'tool_use', name: currMsg.name, input: currMsg.params, id: currMsg.id }) + } + if (nextMsg?.role === 'user') { + if (typeof nextMsg.content === 'string') nextMsg.content = [{ type: 'text', text: typeof nextMsg.content }] + nextMsg.content.push({ type: 'tool_response', tool_use_id: currMsg.id, content: currMsg.content }) + } + } + finalMessages = newMessagesTools + } + + // openai assistant message will have: https://platform.openai.com/docs/guides/function-calling#function-calling-steps + // "tool_calls":[ + // { + // "type": "function", + // "id": "call_12345xyz", + // "function": { + // "name": "get_weather", + // "arguments": "{\"latitude\":48.8566,\"longitude\":2.3522}" + // } + // }] + + // openai user response will be: + // { + // "role": "tool", + // "tool_call_id": tool_call.id, + // "content": str(result) + // } + + // treat all other providers like openai tool message for now + else { + + const newMessagesTools: ( + Exclude | { + role: 'assistant', + content: string; + tool_calls?: { + type: 'function'; + id: string; + function: { + name: string; + arguments: string; + } + }[] + } | { + role: 'tool', + id: string; // old val + tool_call_id: string; // new val + content: string; + } + )[] = []; + + for (let i = 0; i < newMessages.length; i += 1) { + const currMsg = newMessages[i] + + if (currMsg.role !== 'tool') { + newMessagesTools.push(currMsg) + continue + } + + // edit previous assistant message to have called the tool + const prevMsg = 0 <= i - 1 && i - 1 <= newMessagesTools.length ? newMessagesTools[i - 1] : undefined + if (prevMsg?.role === 'assistant') { + prevMsg.tool_calls = [{ + type: 'function', + id: currMsg.id, + function: { + name: currMsg.name, + arguments: currMsg.params + } + }] + } + + // add the tool + newMessagesTools.push({ + role: 'tool', + id: currMsg.id, + content: currMsg.content, + tool_call_id: currMsg.id, + }) + } + finalMessages = newMessagesTools + } + + + + + // 3. CROP MESSAGES SO EVERYTHING FITS IN CONTEXT + // TODO!!! + + + + + return { + separateSystemMessageStr, + messages: finalMessages, + devInfo, + } +} + + + + + + + + + +/* + + +ACCORDING TO 4o: gemini: similar to openai, but function_call, and only 1 call per message (no id because only 1 message) +gemini request: { +"role": "assistant", +"content": null, +"function_call": { +"name": "get_weather", +"arguments": { +"latitude": 48.8566, +"longitude": 2.3522 +} +} +} +gemini response: +{ +"role": "assistant", +"function_response": { +"name": "get_weather", +"response": { +"temperature": "15°C", +"condition": "Cloudy" +} +} +} + + ++ anthropic + ++ openai-compat (4) ++ gemini + +ollama + + +mistral: same as openai + +*/ diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts index dec84841..e568d3b5 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts @@ -3,7 +3,7 @@ * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. *--------------------------------------------------------------------------------------*/ -import { SendLLMMessageParams, OnText, OnFinalMessage, OnError, _InternalLLMChatMessage } from '../../common/llmMessageTypes.js'; +import { SendLLMMessageParams, OnText, OnFinalMessage, OnError } from '../../common/llmMessageTypes.js'; import { IMetricsService } from '../../common/metricsService.js'; import { displayInfoOfProviderName } from '../../common/voidSettingsTypes.js'; @@ -104,11 +104,11 @@ export const sendLLMMessage = ({ case 'groq': case 'gemini': if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - OpenAI FIM', tools: [] }) - else /* */ sendOpenAIChat({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); + else /* */ sendOpenAIChat({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, aiInstructions, tools }); break; case 'anthropic': if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Anthropic FIM', tools: [] }) - else /* */ sendAnthropicChat({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, tools }); + else /* */ sendAnthropicChat({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, aiInstructions, tools }); break; default: onError({ message: `Error: Void provider was "${providerName}", which is not recognized.`, fullError: null }) From af41d6a43932d9c91cdceb04acb4596e6cda5a38 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Sun, 16 Feb 2025 03:23:37 -0800 Subject: [PATCH 22/47] tool improvements --- .../contrib/void/browser/chatThreadService.ts | 12 ++++---- .../contrib/void/common/llmMessageTypes.ts | 2 +- .../contrib/void/common/voidSettingsTypes.ts | 28 +++++++++---------- .../electron-main/llmMessage/anthropic.ts | 9 ++++-- .../void/electron-main/llmMessage/openai.ts | 13 +++++---- .../llmMessage/processMessages.ts | 10 +++---- 6 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index d3dedf84..4565b6a6 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -326,19 +326,21 @@ class ChatThreadService extends Disposable implements IChatThreadService { this._setStreamState(threadId, { messageSoFar: fullText }) }, onFinalMessage: async ({ fullText, tools }) => { - if (tools.length === 0) { + this._addMessageToThread(threadId, { role: 'assistant', content: fullText, displayContent: fullText, tool_calls: tools }) + + if ((tools?.length ?? 0) === 0) { this._finishStreamingTextMessage(threadId, fullText) } else { - for (const tool of tools) { + for (const tool of tools ?? []) { if (!(tool.name in this._toolsService.toolFns)) { - this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.args, id: tool.id, content: `Error: This tool was not recognized, so it was not called.`, displayContent: `Error: tool not recognized.`, }) + this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.params, id: tool.id, content: `Error: This tool was not recognized, so it was not called.`, displayContent: `Error: tool not recognized.`, }) } else { const toolName = tool.name as ToolName - const toolResult = await this._toolsService.toolFns[toolName](tool.args) + const toolResult = await this._toolsService.toolFns[toolName](tool.params) const string = this._toolsService.toolResultToString[toolName](toolResult as any) // typescript is so bad it doesn't even couple the type of ToolResult with the type of the function being called here - this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.args, id: tool.id, content: string, displayContent: string, }) + this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.params, id: tool.id, content: string, displayContent: string, }) shouldContinue = true } } diff --git a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts index 68aafa64..e82da2cb 100644 --- a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts +++ b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts @@ -23,7 +23,7 @@ export const errorDetails = (fullError: Error | null): string | null => { } export type OnText = (p: { newText: string, fullText: string }) => void -export type OnFinalMessage = (p: { fullText: string, tools: { name: string, args: string, id: string, }[] }) => void // id is tool_use_id +export type OnFinalMessage = (p: { fullText: string, tools?: { name: string, params: string, id: string, }[] }) => void // id is tool_use_id export type OnError = (p: { message: string, fullError: Error | null }) => void export type AbortRef = { current: (() => void) | null } diff --git a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts index 9bc1638d..0bbcfcde 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts @@ -18,7 +18,7 @@ export type DeveloperInfoAtModel = { // UNUSED (coming soon): recognizedModelName: RecognizedModelName, // used to show user if model was auto-recognized supportsTools: boolean, // we will just do a string of tool use if it doesn't support - supportsSystemMessage: 'developer' | 'system' | false, // if null, we will just do a string of system message + supportsSystemMessageRole: 'developer' | 'system' | false, // if null, we will just do a string of system message. this is independent from separateSystemMessage, which takes priority and is passed directly in each provider's implementation. supportsAutocompleteFIM: boolean, // we will just do a description of FIM if it doens't support <|fim_hole|> supportsStreaming: boolean, // (o1 does NOT) we will just dump the final result if doesn't support it maxTokens: number, // required @@ -98,7 +98,7 @@ export function recognizedModelOfModelName(modelName: string): RecognizedModelNa const developerInfoAtProvider: { [providerName in ProviderName]: DeveloperInfoAtProvider } = { 'anthropic': { overrideSettingsForAllModels: { - supportsSystemMessage: 'system', + supportsSystemMessageRole: 'system', supportsTools: true, supportsAutocompleteFIM: false, supportsStreaming: true, @@ -106,7 +106,7 @@ const developerInfoAtProvider: { [providerName in ProviderName]: DeveloperInfoAt }, 'deepseek': { overrideSettingsForAllModels: { - supportsSystemMessage: false, + supportsSystemMessageRole: false, supportsTools: false, supportsAutocompleteFIM: false, supportsStreaming: true, @@ -137,15 +137,15 @@ export const developerInfoOfProviderName = (providerName: ProviderName): Partial // providerName is optional, but gives some extra fallbacks if provided const developerInfoOfRecognizedModelName: { [recognizedModel in RecognizedModelName]: Omit } = { 'OpenAI 4o': { - supportsSystemMessage: false, - supportsTools: false, + supportsSystemMessageRole: 'system', + supportsTools: true, supportsAutocompleteFIM: false, - supportsStreaming: false, + supportsStreaming: true, maxTokens: 4096, }, 'Anthropic Claude': { - supportsSystemMessage: false, + supportsSystemMessageRole: 'system', supportsTools: false, supportsAutocompleteFIM: false, supportsStreaming: false, @@ -153,7 +153,7 @@ const developerInfoOfRecognizedModelName: { [recognizedModel in RecognizedModelN }, 'Llama 3.x': { - supportsSystemMessage: false, + supportsSystemMessageRole: false, supportsTools: false, supportsAutocompleteFIM: false, supportsStreaming: false, @@ -161,7 +161,7 @@ const developerInfoOfRecognizedModelName: { [recognizedModel in RecognizedModelN }, 'Deepseek Chat': { - supportsSystemMessage: false, + supportsSystemMessageRole: false, supportsTools: false, supportsAutocompleteFIM: false, supportsStreaming: false, @@ -169,7 +169,7 @@ const developerInfoOfRecognizedModelName: { [recognizedModel in RecognizedModelN }, 'Alibaba Qwen2.5 Coder Instruct': { - supportsSystemMessage: false, + supportsSystemMessageRole: false, supportsTools: false, supportsAutocompleteFIM: false, supportsStreaming: false, @@ -177,7 +177,7 @@ const developerInfoOfRecognizedModelName: { [recognizedModel in RecognizedModelN }, 'Mistral Codestral': { - supportsSystemMessage: false, + supportsSystemMessageRole: false, supportsTools: false, supportsAutocompleteFIM: false, supportsStreaming: false, @@ -185,7 +185,7 @@ const developerInfoOfRecognizedModelName: { [recognizedModel in RecognizedModelN }, 'OpenAI o1, o3': { - supportsSystemMessage: false, + supportsSystemMessageRole: false, supportsTools: false, supportsAutocompleteFIM: false, supportsStreaming: false, @@ -193,7 +193,7 @@ const developerInfoOfRecognizedModelName: { [recognizedModel in RecognizedModelN }, 'Deepseek R1': { - supportsSystemMessage: false, + supportsSystemMessageRole: false, supportsTools: false, supportsAutocompleteFIM: false, supportsStreaming: false, @@ -201,7 +201,7 @@ const developerInfoOfRecognizedModelName: { [recognizedModel in RecognizedModelN }, '': { - supportsSystemMessage: false, + supportsSystemMessageRole: false, supportsTools: false, supportsAutocompleteFIM: false, supportsStreaming: false, diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts index b047a601..8d93b0f7 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts @@ -5,7 +5,7 @@ import Anthropic from '@anthropic-ai/sdk'; import { _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes.js'; -import { anthropicMaxPossibleTokens } from '../../common/voidSettingsTypes.js'; +import { anthropicMaxPossibleTokens, developerInfoOfModelName, developerInfoOfProviderName } from '../../common/voidSettingsTypes.js'; import { InternalToolInfo } from '../../common/toolsService.js'; import { addSystemMessageAndToolSupport } from './processMessages.js'; @@ -39,11 +39,14 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages: return } - const { messages, separateSystemMessageStr, devInfo } = addSystemMessageAndToolSupport(modelName, providerName, messages_, aiInstructions, { separateSystemMessage: true }) + const { messages, separateSystemMessageStr } = addSystemMessageAndToolSupport(modelName, providerName, messages_, aiInstructions, { separateSystemMessage: true }) + + const { overrideSettingsForAllModels } = developerInfoOfProviderName(providerName) + const { supportsTools } = developerInfoOfModelName(modelName, overrideSettingsForAllModels) const anthropic = new Anthropic({ apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true }); - const tools = devInfo?.supportsTools && (tools_?.length ?? 0) !== 0 ? tools_?.map(tool => toAnthropicTool(tool)) : undefined + const tools = (supportsTools && ((tools_?.length ?? 0) !== 0)) ? tools_?.map(tool => toAnthropicTool(tool)) : undefined const stream = anthropic.messages.stream({ system: separateSystemMessageStr, diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts index 60a66881..86c41a9c 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts @@ -8,6 +8,7 @@ import { _InternalModelListFnType, _InternalSendLLMFIMMessageFnType, _InternalSe import { Model } from 'openai/resources/models.js'; import { InternalToolInfo } from '../../common/toolsService.js'; import { addSystemMessageAndToolSupport } from './processMessages.js'; +import { developerInfoOfModelName, developerInfoOfProviderName } from '../../common/voidSettingsTypes.js'; // import { parseMaxTokensStr } from './util.js'; @@ -147,12 +148,14 @@ export const sendOpenAIFIM: _InternalSendLLMFIMMessageFnType = ({ messages, onTe export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, aiInstructions, tools: tools_ }) => { let fullText = '' - const toolCallOfIndex: { [index: string]: { name: string, args: string, id: string } } = {} + const toolCallOfIndex: { [index: string]: { name: string, params: string, id: string } } = {} - const { messages, devInfo } = addSystemMessageAndToolSupport(modelName, providerName, messages_, aiInstructions, { separateSystemMessage: false }) + const { overrideSettingsForAllModels } = developerInfoOfProviderName(providerName) + const { supportsTools } = developerInfoOfModelName(modelName, overrideSettingsForAllModels) - const tools = devInfo?.supportsTools && (tools_?.length ?? 0) !== 0 ? tools_?.map(tool => toOpenAITool(tool)) : undefined + const { messages } = addSystemMessageAndToolSupport(modelName, providerName, messages_, aiInstructions, { separateSystemMessage: false }) + const tools = (supportsTools && ((tools_?.length ?? 0) !== 0)) ? tools_?.map(tool => toOpenAITool(tool)) : undefined const openai: OpenAI = newOpenAI({ providerName, settingsOfProvider }) const options: OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming = { @@ -175,9 +178,9 @@ export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages: me // tool call for (const tool of chunk.choices[0]?.delta?.tool_calls ?? []) { const index = tool.index - if (!toolCallOfIndex[index]) toolCallOfIndex[index] = { name: '', args: '', id: '' } + if (!toolCallOfIndex[index]) toolCallOfIndex[index] = { name: '', params: '', id: '' } toolCallOfIndex[index].name += tool.function?.name ?? '' - toolCallOfIndex[index].args += tool.function?.arguments ?? ''; + toolCallOfIndex[index].params += tool.function?.arguments ?? ''; toolCallOfIndex[index].id = tool.id ?? '' } diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/processMessages.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/processMessages.ts index f6f6cd5e..2ae792fb 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/processMessages.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/processMessages.ts @@ -1,7 +1,7 @@ import { LLMChatMessage } from '../../common/llmMessageTypes.js'; -import { DeveloperInfoAtModel, developerInfoOfModelName, developerInfoOfProviderName, ProviderName } from '../../common/voidSettingsTypes.js'; +import { developerInfoOfModelName, developerInfoOfProviderName, ProviderName } from '../../common/voidSettingsTypes.js'; import { deepClone } from '../../../../../base/common/objects.js'; @@ -9,13 +9,12 @@ import { deepClone } from '../../../../../base/common/objects.js'; // no matter whether the model supports a system message or not (or what format it supports), add it in some way // also take into account tools if the model doesn't support tool use -export const addSystemMessageAndToolSupport = (modelName: string, providerName: ProviderName, messages_: LLMChatMessage[], aiInstructions: string, { separateSystemMessage }: { separateSystemMessage: boolean }): { separateSystemMessageStr?: string, messages: any[], devInfo: DeveloperInfoAtModel } => { +export const addSystemMessageAndToolSupport = (modelName: string, providerName: ProviderName, messages_: LLMChatMessage[], aiInstructions: string, { separateSystemMessage }: { separateSystemMessage: boolean }): { separateSystemMessageStr?: string, messages: any[] } => { const messages = deepClone(messages_).map(m => ({ ...m, content: m.content.trim(), })) const { overrideSettingsForAllModels } = developerInfoOfProviderName(providerName) - const devInfo = developerInfoOfModelName(modelName, overrideSettingsForAllModels) - const { supportsSystemMessage, supportsTools } = devInfo + const { supportsSystemMessageRole: supportsSystemMessage, supportsTools } = developerInfoOfModelName(modelName, overrideSettingsForAllModels) // 1. SYSTEM MESSAGE // find system messages and concatenate them @@ -236,12 +235,13 @@ export const addSystemMessageAndToolSupport = (modelName: string, providerName: // TODO!!! + console.log('SYSMG', separateSystemMessage) + console.log('FINAL MESSAGES', finalMessages) return { separateSystemMessageStr, messages: finalMessages, - devInfo, } } From 249eee341d8b745bae6946783326e9aa24bc7f8e Mon Sep 17 00:00:00 2001 From: Mathew Pareles Date: Sun, 16 Feb 2025 00:13:26 -0800 Subject: [PATCH 23/47] better context and file reading --- .../contrib/void/browser/chatThreadService.ts | 206 +++++++++++------- .../contrib/void/browser/helpers/readFile.ts | 39 +++- .../contrib/void/browser/prompt/prompts.ts | 64 ++++-- .../react/src/markdown/ChatMarkdownRender.tsx | 1 - .../react/src/sidebar-tsx/SidebarChat.tsx | 65 +++--- .../browser/react/src/sidebar-tsx/delete.tsx | 1 + .../contrib/void/browser/sidebarActions.ts | 17 +- .../contrib/void/common/toolsService.ts | 4 +- 8 files changed, 265 insertions(+), 132 deletions(-) create mode 100644 src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/delete.tsx diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index a3452eb2..248ab441 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -13,7 +13,9 @@ import { Emitter, Event } from '../../../../base/common/event.js'; import { IRange } from '../../../../editor/common/core/range.js'; import { ILLMMessageService } from '../common/llmMessageService.js'; import { IModelService } from '../../../../editor/common/services/model.js'; -import { chat_userMessage, chat_systemMessage } from './prompt/prompts.js'; +import { chat_userMessageContent, chat_systemMessage, chat_userMessageContentWithAllFilesToo as chat_userMessageContentWithAllFiles } from './prompt/prompts.js'; +import { LLMChatMessage } from '../common/llmMessageTypes.js'; +import { IFileService } from '../../../../platform/files/common/files.js'; // one of the square items that indicates a selection in a chat bubble (NOT a file, a Selection of text) export type CodeSelection = { @@ -32,23 +34,17 @@ export type FileSelection = { export type StagingSelectionItem = CodeSelection | FileSelection - -export type StagingInfo = { - isBeingEdited: boolean; - selections: StagingSelectionItem[] | null; // staging selections in edit mode -} - -const defaultStaging: StagingInfo = { isBeingEdited: false, selections: [] } - - // WARNING: changing this format is a big deal!!!!!! need to migrate old format to new format on users' computers so people don't get errors. export type ChatMessage = | { role: 'user'; - content: string | null; // content sent to the llm - allowed to be '', will be replaced with (empty) + content: string | null; // content displayed to the LLM on future calls - allowed to be '', will be replaced with (empty) displayContent: string | null; // content displayed to user - allowed to be '', will be ignored selections: StagingSelectionItem[] | null; // the user's selection - staging: StagingInfo | null + state: { + stagingSelections: StagingSelectionItem[]; + isBeingEdited: boolean; + } } | { role: 'assistant'; @@ -61,6 +57,11 @@ export type ChatMessage = displayContent?: undefined; } +type UserMessageType = ChatMessage & { role: 'user' } +type UserMessageState = UserMessageType['state'] + +export const defaultMessageState: UserMessageState = { stagingSelections: [], isBeingEdited: false } + // a 'thread' means a chat message history export type ChatThreads = { [id: string]: { @@ -68,11 +69,18 @@ export type ChatThreads = { createdAt: string; // ISO string lastModified: string; // ISO string messages: ChatMessage[]; - staging: StagingInfo | null; - focusedMessageIdx?: number | undefined; // index of the message that is being edited (undefined if none) + state: { + stagingSelections: StagingSelectionItem[]; + focusedMessageIdx: number | undefined; // index of the message that is being edited (undefined if none) + isCheckedOfSelectionId: { [selectionId: string]: boolean }; + } }; } +type ThreadType = ChatThreads[string] + +const defaultThreadState: ThreadType['state'] = { stagingSelections: [], focusedMessageIdx: undefined, isCheckedOfSelectionId: {} } + export type ThreadsState = { allThreads: ChatThreads; currentThreadId: string; // intended for internal use only @@ -94,11 +102,12 @@ const newThreadObject = () => { createdAt: now, lastModified: now, messages: [], - focusedMessageIdx: undefined, - staging: { - isBeingEdited: true, - selections: [], - } + state: { + stagingSelections: [], + focusedMessageIdx: undefined, + isCheckedOfSelectionId: {} + }, + } satisfies ChatThreads[string] } @@ -124,7 +133,9 @@ export interface IChatThreadService { isFocusingMessage(): boolean; setFocusedMessageIdx(messageIdx: number | undefined): void; - _useFocusedStagingState(messageIdx?: number | undefined): readonly [StagingInfo, (stagingInfo: StagingInfo) => void]; + // _useFocusedStagingState(messageIdx?: number | undefined): readonly [StagingInfo, (stagingInfo: StagingInfo) => void]; + _useCurrentThreadState(): readonly [ThreadType['state'], (newState: Partial) => void]; + _useCurrentMessageState(messageIdx: number): readonly [UserMessageState, (newState: Partial) => void]; editUserMessageAndStreamResponse(userMessage: string, messageIdx: number): Promise; addUserMessageAndStreamResponse(userMessage: string): Promise; @@ -150,6 +161,7 @@ class ChatThreadService extends Disposable implements IChatThreadService { constructor( @IStorageService private readonly _storageService: IStorageService, @IModelService private readonly _modelService: IModelService, + @IFileService private readonly _fileService: IFileService, @ILLMMessageService private readonly _llmMessageService: ILLMMessageService, ) { super() @@ -190,21 +202,19 @@ class ChatThreadService extends Disposable implements IChatThreadService { const threads: ChatThreads = oldThreadsObject /** v1 -> v2 - - threadsState.currentStagingSelections: CodeStagingSelection[] | null; - + thread.staging: StagingInfo - + thread.focusedMessageIdx?: number | undefined; - - + chatMessage.staging: StagingInfo | null - */ + - threads.state.currentStagingSelections: CodeStagingSelection[] | null; + + thread[threadIdx].state + + message.state +*/ // check if we need to update let shouldUpdate = false for (const thread of Object.values(threads)) { - if (!thread.staging) { + if (!thread.state) { shouldUpdate = true } for (const chatMessage of Object.values(thread.messages)) { - if (chatMessage.role === 'user' && !chatMessage.staging) { + if (chatMessage.role === 'user' && !chatMessage.state) { shouldUpdate = true } } @@ -214,13 +224,12 @@ class ChatThreadService extends Disposable implements IChatThreadService { // update the threads for (const thread of Object.values(threads)) { - if (!thread.staging) { - thread.staging = defaultStaging - thread.focusedMessageIdx = undefined + if (!thread.state) { + thread.state = defaultThreadState } for (const chatMessage of Object.values(thread.messages)) { - if (chatMessage.role === 'user' && !chatMessage.staging) { - chatMessage.staging = defaultStaging + if (chatMessage.role === 'user' && !chatMessage.state) { + chatMessage.state = defaultMessageState } } } @@ -245,6 +254,17 @@ class ChatThreadService extends Disposable implements IChatThreadService { this._onDidChangeCurrentThread.fire() } + private _getAllSelections() { + const thread = this.getCurrentThread() + return thread.messages.flatMap(m => m.role === 'user' && m.selections || []) + } + + private _getSelectionsUpToMessageIdx(messageIdx: number) { + const thread = this.getCurrentThread() + const prevMessages = thread.messages.slice(0, messageIdx) + return prevMessages.flatMap(m => m.role === 'user' && m.selections || []) + } + private _setStreamState(threadId: string, state: Partial>) { this.streamState[threadId] = { ...this.streamState[threadId], @@ -268,12 +288,14 @@ class ChatThreadService extends Disposable implements IChatThreadService { const thread = this.getCurrentThread() - const messageToReplace = thread.messages[messageIdx] - if (messageToReplace?.role !== 'user') { - console.log(`Error: tried to edit non-user message. messageIdx=${messageIdx}, numMessages=${thread.messages.length}`) - return + if (thread.messages?.[messageIdx]?.role !== 'user') { + throw new Error("Error: editing a message with role !=='user'") } + // get prev and curr selections before clearing the message + const prevSelns = this._getSelectionsUpToMessageIdx(messageIdx) + const currSelns = thread.messages[messageIdx].selections || [] + // clear messages up to the index const slicedMessages = thread.messages.slice(0, messageIdx) this._setState({ @@ -287,36 +309,45 @@ class ChatThreadService extends Disposable implements IChatThreadService { }, true) // stream the edit - this.addUserMessageAndStreamResponse(userMessage, messageToReplace.staging) + this.addUserMessageAndStreamResponse(userMessage, { prevSelns, currSelns }) } - async addUserMessageAndStreamResponse(userMessage: string, stagingOverride?: StagingInfo | null) { - + async addUserMessageAndStreamResponse(userMessage: string, options?: { prevSelns?: StagingSelectionItem[], currSelns?: StagingSelectionItem[] }) { const thread = this.getCurrentThread() const threadId = thread.id - let threadStaging = thread.staging - - const currStaging = stagingOverride ?? threadStaging ?? defaultStaging // don't use _useFocusedStagingState to avoid race conditions with focusing - const { selections: currSelns, } = currStaging - // add user's message to chat history const instructions = userMessage - const content = await chat_userMessage(instructions, currSelns, this._modelService) - const userHistoryElt: ChatMessage = { role: 'user', content: content, displayContent: instructions, selections: currSelns, staging: null, } + + const prevSelns: StagingSelectionItem[] = options?.prevSelns ?? this._getAllSelections() + const currSelns: StagingSelectionItem[] = options?.currSelns ?? thread.state.stagingSelections + + // read all curr+previous files on demand instead of adding them to the history + const messageContent = await chat_userMessageContent(instructions, prevSelns, currSelns) + const messageContentWithAllFiles = await chat_userMessageContentWithAllFiles(instructions, prevSelns, currSelns, this._modelService, this._fileService) + const prevLLMMessages = this.getCurrentThread().messages.map(m => ({ role: m.role, content: m.content || '(empty model output)' })) + const currLLMMessage: LLMChatMessage = { role: 'user', content: messageContentWithAllFiles } + + const userHistoryElt: ChatMessage = { role: 'user', content: messageContent, displayContent: instructions, selections: currSelns, state: defaultMessageState } this._addMessageToThread(threadId, userHistoryElt) this._setStreamState(threadId, { error: undefined }) + console.log(`messageContent`) + console.log([{ role: 'system', content: chat_systemMessage }, + ...prevLLMMessages, + currLLMMessage,]) + const llmCancelToken = this._llmMessageService.sendLLMMessage({ messagesType: 'chatMessages', logging: { loggingName: 'Chat' }, useProviderFor: 'Ctrl+L', messages: [ { role: 'system', content: chat_systemMessage }, - ...this.getCurrentThread().messages.map(m => ({ role: m.role, content: m.content || '(empty model output)' })), + ...prevLLMMessages, + currLLMMessage, ], onText: ({ newText, fullText }) => { this._setStreamState(threadId, { messageSoFar: fullText }) @@ -357,13 +388,13 @@ class ChatThreadService extends Disposable implements IChatThreadService { const thread = this.getCurrentThread() // get the focusedMessageIdx - const focusedMessageIdx = thread.focusedMessageIdx + const focusedMessageIdx = thread.state.focusedMessageIdx if (focusedMessageIdx === undefined) return; // check that the message is actually being edited const focusedMessage = thread.messages[focusedMessageIdx] if (focusedMessage.role !== 'user') return; - if (!focusedMessage.staging?.isBeingEdited) return; + if (!focusedMessage.state) return; return focusedMessageIdx } @@ -429,28 +460,34 @@ class ChatThreadService extends Disposable implements IChatThreadService { ...this.state.allThreads, [threadId]: { ...thread, - focusedMessageIdx: messageIdx + state: { + ...thread.state, + focusedMessageIdx: messageIdx, + } } } }, true) } - // set thread.messages[messageIdx].stagingSelections - private setEditMessageStaging(staging: StagingInfo, messageIdx: number): void { + // set message.state + private _setCurrentMessageState(state: Partial, messageIdx: number): void { - const thread = this.getCurrentThread() - const message = thread.messages[messageIdx] - if (message.role !== 'user') return; + const threadId = this.state.currentThreadId + const thread = this.state.allThreads[threadId] + if (!thread) return this._setState({ allThreads: { ...this.state.allThreads, - [thread.id]: { + [threadId]: { ...thread, messages: thread.messages.map((m, i) => - i === messageIdx ? { + i === messageIdx && m.role === 'user' ? { ...m, - staging, + state: { + ...m.state, + ...state + }, } : m ) } @@ -459,48 +496,53 @@ class ChatThreadService extends Disposable implements IChatThreadService { } - // set thread.stagingSelections - private setDefaultStaging(staging: StagingInfo): void { + // set thread.state + private _setCurrentThreadState(state: Partial): void { - const thread = this.getCurrentThread() + const threadId = this.state.currentThreadId + const thread = this.state.allThreads[threadId] + if (!thread) return this._setState({ allThreads: { ...this.state.allThreads, [thread.id]: { ...thread, - staging, + state: { + ...thread.state, + ...state + } } } }, true) } - // gets `staging` and `setStaging` of the currently focused element, given the index of the currently selected message (or undefined if no message is selected) - _useFocusedStagingState(messageIdx?: number | undefined) { - const defaultStaging = { isBeingEdited: false, selections: [], text: '' } - - let staging: StagingInfo = defaultStaging - let setStaging: (selections: StagingInfo) => void = () => { } + _useCurrentMessageState(messageIdx: number) { const thread = this.getCurrentThread() - const isFocusingMessage = messageIdx !== undefined - if (isFocusingMessage) { // is editing message + const messages = thread.messages + const currMessage = messages[messageIdx] - const message = thread.messages[messageIdx!] - if (message.role === 'user') { - staging = message.staging || defaultStaging - setStaging = (s) => this.setEditMessageStaging(s, messageIdx) - } - - } - else { // is editing the default input box - staging = thread.staging || defaultStaging - setStaging = this.setDefaultStaging.bind(this) + if (currMessage.role !== 'user') { + return [defaultMessageState, (s: any) => { }] as const } - return [staging, setStaging] as const + const state = currMessage.state + const setState = (newState: Partial) => this._setCurrentMessageState(newState, messageIdx) + + return [state, setState] as const + + } + + _useCurrentThreadState() { + const thread = this.getCurrentThread() + + const state = thread.state + const setState = this._setCurrentThreadState.bind(this) + + return [state, setState] as const } diff --git a/src/vs/workbench/contrib/void/browser/helpers/readFile.ts b/src/vs/workbench/contrib/void/browser/helpers/readFile.ts index b0f154d1..f7752b84 100644 --- a/src/vs/workbench/contrib/void/browser/helpers/readFile.ts +++ b/src/vs/workbench/contrib/void/browser/helpers/readFile.ts @@ -3,14 +3,41 @@ import { EndOfLinePreference } from '../../../../../editor/common/model' import { IModelService } from '../../../../../editor/common/services/model.js' import { IFileService } from '../../../../../platform/files/common/files' -// read files from VSCode. preferred (but appears to only work if the model of this URI already exists. If it doesn't use the other function.) -export const VSReadFile = async (modelService: IModelService, uri: URI): Promise => { - const model = modelService.getModel(uri) - if (!model) return null - return model.getValue(EndOfLinePreference.LF) + +// attempts to read URI of currently opened model, then of raw file +export const VSReadFile = async (modelService: IModelService, fileService: IFileService, uri: URI) => { + + const modelResult = await _VSReadModel(modelService, uri) + if (modelResult) return modelResult + + const fileResult = await _VSReadFileRaw(fileService, uri) + if (fileResult) return fileResult + + return '' + } -export const VSReadFileRaw = async (fileService: IFileService, uri: URI) => { +// read files from VSCode. preferred (but appears to only work if the model of this URI already exists. If it doesn't use the other function.) +export const _VSReadModel = async (modelService: IModelService, uri: URI): Promise => { + + // attempt to read saved model (sometimes doesn't work if page is reloaded) + const model = modelService.getModel(uri) + if (model) { + return model.getValue(EndOfLinePreference.LF) + } + + // look at all opened models and check if they have the same `fsPath` + const models = modelService.getModels(); + for (const model of models) { + if (model.uri.fsPath.toString() === uri.fsPath.toString()) { + return model.getValue(EndOfLinePreference.LF); + } + } + + return null +} + +export const _VSReadFileRaw = async (fileService: IFileService, uri: URI) => { const res = await fileService.readFile(uri) const str = res.value.toString() return str diff --git a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts index b3fb4482..5ecb924a 100644 --- a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts +++ b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts @@ -7,8 +7,9 @@ import { URI } from '../../../../../base/common/uri.js'; import { filenameToVscodeLanguage } from '../helpers/detectLanguage.js'; import { CodeSelection, StagingSelectionItem, FileSelection } from '../chatThreadService.js'; -import { VSReadFile } from '../helpers/readFile.js'; +import { _VSReadModel, VSReadFile } from '../helpers/readFile.js'; import { IModelService } from '../../../../../editor/common/services/model.js'; +import { IFileService } from '../../../../../platform/files/common/files.js'; // this is just for ease of readability @@ -156,10 +157,10 @@ ${tripleTick[1]} } const failToReadStr = 'Could not read content. This file may have been deleted. If you expected content here, you can tell the user about this as they might not know.' -const stringifyFileSelections = async (fileSelections: FileSelection[], modelService: IModelService) => { +const stringifyFileSelections = async (fileSelections: FileSelection[], modelService: IModelService, fileService: IFileService) => { if (fileSelections.length === 0) return null const fileSlns: FileSelnLocal[] = await Promise.all(fileSelections.map(async (sel) => { - const content = await VSReadFile(modelService, sel.fileURI) ?? failToReadStr + const content = await VSReadFile(modelService, fileService, sel.fileURI) ?? failToReadStr return { ...sel, content } })) return fileSlns.map(sel => stringifyFileSelection(sel)).join('\n') @@ -167,23 +168,60 @@ const stringifyFileSelections = async (fileSelections: FileSelection[], modelSer const stringifyCodeSelections = (codeSelections: CodeSelection[]) => { return codeSelections.map(sel => stringifyCodeSelection(sel)).join('\n') } +const stringifySelectionNames = (currSelns: StagingSelectionItem[] | null): string => { + if (!currSelns) return '' + return currSelns.map(s => `${s.fileURI.fsPath}${s.range ? ` (lines ${s.range.startLineNumber}:${s.range.endLineNumber})` : ''}`).join('\n') +} +export const chat_userMessageContent = async (instructions: string, prevSelns: StagingSelectionItem[] | null, currSelns: StagingSelectionItem[] | null) => { - -export const chat_userMessage = async (instructions: string, selections: StagingSelectionItem[] | null, modelService: IModelService) => { - const fileSelections = selections?.filter(s => s.type === 'File') as FileSelection[] - const codeSelections = selections?.filter(s => s.type === 'Selection') as CodeSelection[] - - const filesStr = await stringifyFileSelections(fileSelections, modelService) - const codeStr = stringifyCodeSelections(codeSelections) + const selnsStr = stringifySelectionNames(currSelns) let str = '' - if (filesStr) str += `FILES\n${filesStr}\n` - if (codeStr) str += `SELECTIONS\n${codeStr}\n` - str += `INSTRUCTIONS\n${instructions}` + if (selnsStr) { str += `SELECTIONS\n${selnsStr}\n` } + str += `\nINSTRUCTIONS\n${instructions}` return str; }; +export const chat_userMessageContentWithAllFilesToo = async (instructions: string, prevSelns: StagingSelectionItem[] | null, currSelns: StagingSelectionItem[] | null, modelService: IModelService, fileService: IFileService) => { + + // ADD IN FILES AT TOP + const allSelections = [...currSelns || [], ...prevSelns || []] + + const codeSelections: CodeSelection[] = [] + const fileSelections: FileSelection[] = [] + const filesURIs = new Set() + + for (const selection of allSelections) { + if (selection.type === 'Selection') { + codeSelections.push(selection) + } + else if (selection.type === 'File') { + const fileSelection = selection + const path = fileSelection.fileURI.fsPath + if (!filesURIs.has(path)) { + filesURIs.add(path) + fileSelections.push(fileSelection) + } + } + } + + const filesStr = await stringifyFileSelections(fileSelections, modelService, fileService) + const selnsStr = stringifyCodeSelections(codeSelections) + + // ACTUAL MESSAGE CONTENT + const messageContent = await chat_userMessageContent(instructions, prevSelns, currSelns) + + + let str = '' + + str += 'ALL FILE CONTENTS\n' + if (filesStr) str += `${filesStr}\n` + if (selnsStr) str += `${selnsStr}\n` + if (messageContent) str += `\n${messageContent}\n` + + return str; +}; diff --git a/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx b/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx index 86afcc33..351a399a 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx @@ -92,7 +92,6 @@ const RenderToken = ({ token, nested = false, noSpace = false, chatLocation, tok // deal with built-in tokens first (assume marked token) const t = token as MarkedToken - console.log(t.raw) if (t.type === "space") { return {t.raw} diff --git a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx index 2aaf9dd2..52944476 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx @@ -7,7 +7,7 @@ import React, { ButtonHTMLAttributes, FormEvent, FormHTMLAttributes, Fragment, K import { useAccessor, useSidebarState, useChatThreadsState, useChatThreadsStreamState, useUriState, useSettingsState } from '../util/services.js'; -import { ChatMessage, StagingInfo, StagingSelectionItem } from '../../../chatThreadService.js'; +import { ChatMessage, StagingSelectionItem } from '../../../chatThreadService.js'; import { BlockCode } from '../markdown/BlockCode.js'; import { ChatMarkdownRender } from '../markdown/ChatMarkdownRender.js'; @@ -156,8 +156,8 @@ interface VoidChatAreaProps { showSelections?: boolean; showProspectiveSelections?: boolean; - staging?: StagingInfo - setStaging?: (s: StagingInfo) => void + selections?: StagingSelectionItem[] + setSelections?: (s: StagingSelectionItem[]) => void // selections?: any[]; // onSelectionsChange?: (selections: any[]) => void; @@ -180,8 +180,8 @@ export const VoidChatArea: React.FC = ({ featureName, showSelections = false, showProspectiveSelections = true, - staging, - setStaging, + selections, + setSelections, }) => { return (
= ({ }} > {/* Selections section */} - {showSelections && staging && setStaging && ( + {showSelections && selections && setSelections && ( setStaging({ ...staging, selections })} + selections={selections} + setSelections={setSelections} showProspectiveSelections={showProspectiveSelections} /> )} @@ -550,9 +550,23 @@ const ChatBubble = ({ chatMessage, isLoading, messageIdx }: { chatMessage: ChatM const accessor = useAccessor() const chatThreadsService = accessor.get('IChatThreadService') - // edit mode state - const [staging, setStaging] = chatThreadsService._useFocusedStagingState(messageIdx) - const mode: ChatBubbleMode = staging.isBeingEdited ? 'edit' : 'display' + // global state + let isBeingEdited = false + let setIsBeingEdited = (v: boolean) => { } + let stagingSelections: StagingSelectionItem[] = [] + let setStagingSelections = (s: StagingSelectionItem[]) => { } + + if (messageIdx !== undefined) { + const [_state, _setState] = chatThreadsService._useCurrentMessageState(messageIdx) + isBeingEdited = _state.isBeingEdited + setIsBeingEdited = (v) => _setState({ isBeingEdited: v }) + stagingSelections = _state.stagingSelections + setStagingSelections = (s) => { _setState({ stagingSelections: s }) } + } + + + // local state + const mode: ChatBubbleMode = isBeingEdited ? 'edit' : 'display' const [isFocused, setIsFocused] = useState(false) const [isHovered, setIsHovered] = useState(false) const [isDisabled, setIsDisabled] = useState(false) @@ -565,10 +579,8 @@ const ChatBubble = ({ chatMessage, isLoading, messageIdx }: { chatMessage: ChatM const canInitialize = role === 'user' && mode === 'edit' && textAreaRefState const shouldInitialize = _justEnabledEdit.current || _mustInitialize.current if (canInitialize && shouldInitialize) { - setStaging({ - ...staging, - selections: chatMessage.selections || [], - }) + setStagingSelections(chatMessage.selections || []) + if (textAreaFnsRef.current) textAreaFnsRef.current.setValue(chatMessage.displayContent || '') @@ -581,14 +593,14 @@ const ChatBubble = ({ chatMessage, isLoading, messageIdx }: { chatMessage: ChatM }, [role, mode, _justEnabledEdit, textAreaRefState, textAreaFnsRef.current, _justEnabledEdit.current, _mustInitialize.current]) const EditSymbol = mode === 'display' ? Pencil : X const onOpenEdit = () => { - setStaging({ ...staging, isBeingEdited: true }) + setIsBeingEdited(true) chatThreadsService.setFocusedMessageIdx(messageIdx) _justEnabledEdit.current = true } const onCloseEdit = () => { setIsFocused(false) setIsHovered(false) - setStaging({ ...staging, isBeingEdited: false }) + setIsBeingEdited(false) chatThreadsService.setFocusedMessageIdx(undefined) } @@ -614,7 +626,7 @@ const ChatBubble = ({ chatMessage, isLoading, messageIdx }: { chatMessage: ChatM chatThreadsService.cancelStreaming(thread.id) // reset state - setStaging({ ...staging, isBeingEdited: false }) + setIsBeingEdited(false) chatThreadsService.setFocusedMessageIdx(undefined) // stream the edit @@ -649,8 +661,8 @@ const ChatBubble = ({ chatMessage, isLoading, messageIdx }: { chatMessage: ChatM showSelections={true} showProspectiveSelections={false} featureName="Ctrl+L" - staging={staging} - setStaging={setStaging} + selections={stagingSelections} + setSelections={setStagingSelections} > { const currentThread = chatThreadsService.getCurrentThread() const previousMessages = currentThread?.messages ?? [] - const [staging, setStaging] = chatThreadsService._useFocusedStagingState() + + const [_state, _setState] = chatThreadsService._useCurrentThreadState() + const selections = _state.stagingSelections + const setSelections = (s: StagingSelectionItem[]) => { _setState({ stagingSelections: s }) } // stream state const currThreadStreamState = useChatThreadsStreamState(chatThreadsState.currentThreadId) @@ -797,11 +812,11 @@ export const SidebarChat = () => { const userMessage = textAreaRef.current?.value ?? '' await chatThreadsService.addUserMessageAndStreamResponse(userMessage) - setStaging({ ...staging, selections: [], }) // clear staging + setSelections([]) // clear staging textAreaFnsRef.current?.setValue('') textAreaRef.current?.focus() // focus input after submit - }, [chatThreadsService, isDisabled, isStreaming, textAreaRef, textAreaFnsRef, staging, setStaging]) + }, [chatThreadsService, isDisabled, isStreaming, textAreaRef, textAreaFnsRef, selections, setSelections]) const onAbort = () => { const threadId = currentThread.id @@ -887,8 +902,8 @@ export const SidebarChat = () => { isDisabled={isDisabled} showSelections={true} showProspectiveSelections={prevMessagesHTML.length === 0} - staging={staging} - setStaging={setStaging} + selections={selections} + setSelections={setSelections} onClickAnywhere={() => { textAreaRef.current?.focus() }} featureName="Ctrl+L" > diff --git a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/delete.tsx b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/delete.tsx new file mode 100644 index 00000000..07a09338 --- /dev/null +++ b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/delete.tsx @@ -0,0 +1 @@ +A B C diff --git a/src/vs/workbench/contrib/void/browser/sidebarActions.ts b/src/vs/workbench/contrib/void/browser/sidebarActions.ts index d65c51a7..2e64c53f 100644 --- a/src/vs/workbench/contrib/void/browser/sidebarActions.ts +++ b/src/vs/workbench/contrib/void/browser/sidebarActions.ts @@ -135,9 +135,20 @@ registerAction2(class extends Action2 { const chatThreadService = accessor.get(IChatThreadService) const focusedMessageIdx = chatThreadService.getFocusedMessageIdx() - const [staging, setStaging] = chatThreadService._useFocusedStagingState(focusedMessageIdx) - const selections = staging.selections || [] - const setSelections = (s: StagingSelectionItem[]) => setStaging({ ...staging, selections: s }) + + // set the selections to the proper value + let selections: StagingSelectionItem[] = [] + let setSelections = (s: StagingSelectionItem[]) => { } + + if (focusedMessageIdx === undefined) { + const [state, setState] = chatThreadService._useCurrentThreadState() + selections = state.stagingSelections + setSelections = (s) => setState({ stagingSelections: s }) + } else { + const [state, setState] = chatThreadService._useCurrentMessageState(focusedMessageIdx) + selections = state.stagingSelections + setSelections = (s) => setState({ stagingSelections: s }) + } // if matches with existing selection, overwrite (since text may change) const matchingStagingEltIdx = findMatchingStagingIndex(selections, selection) diff --git a/src/vs/workbench/contrib/void/common/toolsService.ts b/src/vs/workbench/contrib/void/common/toolsService.ts index 8ffd6b9b..e96186c9 100644 --- a/src/vs/workbench/contrib/void/common/toolsService.ts +++ b/src/vs/workbench/contrib/void/common/toolsService.ts @@ -4,7 +4,7 @@ import { IFileService, IFileStat } from '../../../../platform/files/common/files import { registerSingleton, InstantiationType } from '../../../../platform/instantiation/common/extensions.js' import { createDecorator, IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js' import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js' -import { VSReadFileRaw } from '../../../../workbench/contrib/void/browser/helpers/readFile.js' +import { _VSReadFileRaw } from '../../../../workbench/contrib/void/browser/helpers/readFile.js' import { QueryBuilder } from '../../../../workbench/services/search/common/queryBuilder.js' import { ISearchService } from '../../../../workbench/services/search/common/search.js' @@ -140,7 +140,7 @@ export class ToolService implements IToolService { this.contextToolCallFns = { read_file: async ({ uri: uriStr }) => { const uri = validateURI(uriStr) - const fileContents = await VSReadFileRaw(fileService, uri) + const fileContents = await _VSReadFileRaw(fileService, uri) return fileContents ?? '(could not read file)' }, list_dir: async ({ uri: uriStr }) => { From 36b1b56690870b1d99565e44837fc867ff672aa6 Mon Sep 17 00:00:00 2001 From: Mathew Pareles Date: Sun, 16 Feb 2025 00:53:11 -0800 Subject: [PATCH 24/47] bug --- .../contrib/void/browser/react/src/sidebar-tsx/delete.tsx | 1 - 1 file changed, 1 deletion(-) delete mode 100644 src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/delete.tsx diff --git a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/delete.tsx b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/delete.tsx deleted file mode 100644 index 07a09338..00000000 --- a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/delete.tsx +++ /dev/null @@ -1 +0,0 @@ -A B C From 41fe5c50e2567fd58083bf52e26c30530df6aec0 Mon Sep 17 00:00:00 2001 From: Mathew Pareles Date: Sun, 16 Feb 2025 01:48:25 -0800 Subject: [PATCH 25/47] fix scrollbars --- .../react/src/util/useScrollbarStyles.tsx | 188 ++++++++++-------- 1 file changed, 109 insertions(+), 79 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/react/src/util/useScrollbarStyles.tsx b/src/vs/workbench/contrib/void/browser/react/src/util/useScrollbarStyles.tsx index 7f8ceb34..94df3aac 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/util/useScrollbarStyles.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/util/useScrollbarStyles.tsx @@ -1,7 +1,6 @@ import { useEffect } from 'react'; export const useScrollbarStyles = (containerRef: React.MutableRefObject) => { - useEffect(() => { if (!containerRef.current) return; @@ -12,90 +11,121 @@ export const useScrollbarStyles = (containerRef: React.MutableRefObject { + // Get all matching elements within the container, including the container itself + const scrollElements = [ + ...(containerRef.current?.matches(overflowSelector) ? [containerRef.current] : []), + ...Array.from(containerRef.current?.querySelectorAll(overflowSelector) || []) + ]; - // Apply styles and listeners to each scroll element - scrollElements.forEach(element => { - // Add the scrollable class directly to the overflow element - element.classList.add('void-scrollable-element'); - - let fadeTimeout: NodeJS.Timeout | null = null; - let fadeInterval: NodeJS.Timeout | null = null; - - const fadeIn = () => { - if (fadeInterval) clearInterval(fadeInterval); - - let step = 0; - fadeInterval = setInterval(() => { - if (step <= 10) { - element.classList.remove(`show-scrollbar-${step - 1}`); - element.classList.add(`show-scrollbar-${step}`); - step++; - } else { - clearInterval(fadeInterval!); - } - }, 10); - }; - - const fadeOut = () => { - if (fadeInterval) clearInterval(fadeInterval); - - let step = 10; - fadeInterval = setInterval(() => { - if (step >= 0) { - element.classList.remove(`show-scrollbar-${step + 1}`); - element.classList.add(`show-scrollbar-${step}`); - step--; - } else { - clearInterval(fadeInterval!); - } - }, 60); - }; - - const onMouseEnter = () => { - if (fadeTimeout) clearTimeout(fadeTimeout); - if (fadeInterval) clearInterval(fadeInterval); - fadeIn(); - }; - - const onMouseLeave = () => { - if (fadeTimeout) clearTimeout(fadeTimeout); - fadeTimeout = setTimeout(() => { - fadeOut(); - }, 10); - }; - - element.addEventListener('mouseenter', onMouseEnter); - element.addEventListener('mouseleave', onMouseLeave); - - // Store cleanup function - const cleanup = () => { - element.removeEventListener('mouseenter', onMouseEnter); - element.removeEventListener('mouseleave', onMouseLeave); - if (fadeTimeout) clearTimeout(fadeTimeout); - if (fadeInterval) clearInterval(fadeInterval); - element.classList.remove('void-scrollable-element'); - // Remove any remaining show-scrollbar classes - for (let i = 0; i <= 10; i++) { - element.classList.remove(`show-scrollbar-${i}`); - } - }; - - // Store the cleanup function on the element for later use - (element as any).__scrollbarCleanup = cleanup; - }); - - return () => { - // Clean up all scroll elements + // Clean up existing elements first scrollElements.forEach(element => { if ((element as any).__scrollbarCleanup) { (element as any).__scrollbarCleanup(); } }); + + // Apply styles and listeners to each scroll element + scrollElements.forEach(element => { + // Add the scrollable class directly to the overflow element + element.classList.add('void-scrollable-element'); + + let fadeTimeout: NodeJS.Timeout | null = null; + let fadeInterval: NodeJS.Timeout | null = null; + + const fadeIn = () => { + if (fadeInterval) clearInterval(fadeInterval); + + let step = 0; + fadeInterval = setInterval(() => { + if (step <= 10) { + element.classList.remove(`show-scrollbar-${step - 1}`); + element.classList.add(`show-scrollbar-${step}`); + step++; + } else { + clearInterval(fadeInterval!); + } + }, 10); + }; + + const fadeOut = () => { + if (fadeInterval) clearInterval(fadeInterval); + + let step = 10; + fadeInterval = setInterval(() => { + if (step >= 0) { + element.classList.remove(`show-scrollbar-${step + 1}`); + element.classList.add(`show-scrollbar-${step}`); + step--; + } else { + clearInterval(fadeInterval!); + } + }, 60); + }; + + const onMouseEnter = () => { + if (fadeTimeout) clearTimeout(fadeTimeout); + if (fadeInterval) clearInterval(fadeInterval); + fadeIn(); + }; + + const onMouseLeave = () => { + if (fadeTimeout) clearTimeout(fadeTimeout); + fadeTimeout = setTimeout(() => { + fadeOut(); + }, 10); + }; + + element.addEventListener('mouseenter', onMouseEnter); + element.addEventListener('mouseleave', onMouseLeave); + + // Store cleanup function + const cleanup = () => { + element.removeEventListener('mouseenter', onMouseEnter); + element.removeEventListener('mouseleave', onMouseLeave); + if (fadeTimeout) clearTimeout(fadeTimeout); + if (fadeInterval) clearInterval(fadeInterval); + element.classList.remove('void-scrollable-element'); + // Remove any remaining show-scrollbar classes + for (let i = 0; i <= 10; i++) { + element.classList.remove(`show-scrollbar-${i}`); + } + }; + + // Store the cleanup function on the element for later use + (element as any).__scrollbarCleanup = cleanup; + }); + }; + + // Initialize for the first time + initializeScrollbarStyles(); + + // Set up mutation observer + const observer = new MutationObserver((mutations) => { + initializeScrollbarStyles(); + }); + + // Start observing the container for child changes + observer.observe(containerRef.current, { + childList: true, + subtree: true + }); + + return () => { + observer.disconnect(); + // Your existing cleanup code... + if (containerRef.current) { + const scrollElements = [ + ...(containerRef.current.matches(overflowSelector) ? [containerRef.current] : []), + ...Array.from(containerRef.current.querySelectorAll(overflowSelector)) + ]; + scrollElements.forEach(element => { + if ((element as any).__scrollbarCleanup) { + (element as any).__scrollbarCleanup(); + } + }); + } }; }, [containerRef]); }; From 2bc3d67e39720aaa8bbf9db5f20ecb42112a90a5 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Sun, 16 Feb 2025 19:51:58 -0800 Subject: [PATCH 26/47] minor tool use fix --- src/vs/workbench/contrib/void/browser/chatThreadService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index 4565b6a6..81e4a434 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -326,12 +326,12 @@ class ChatThreadService extends Disposable implements IChatThreadService { this._setStreamState(threadId, { messageSoFar: fullText }) }, onFinalMessage: async ({ fullText, tools }) => { - this._addMessageToThread(threadId, { role: 'assistant', content: fullText, displayContent: fullText, tool_calls: tools }) if ((tools?.length ?? 0) === 0) { this._finishStreamingTextMessage(threadId, fullText) } else { + this._addMessageToThread(threadId, { role: 'assistant', content: fullText, displayContent: fullText, tool_calls: tools }) for (const tool of tools ?? []) { if (!(tool.name in this._toolsService.toolFns)) { this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.params, id: tool.id, content: `Error: This tool was not recognized, so it was not called.`, displayContent: `Error: tool not recognized.`, }) From 74f8303803c6308770e0d525004f5b6decc64da2 Mon Sep 17 00:00:00 2001 From: Mathew Pareles Date: Sun, 16 Feb 2025 20:11:13 -0800 Subject: [PATCH 27/47] discard star changes --- .../browser/parts/editor/editorActions.ts | 20 +--------------- .../editor/media/multieditortabscontrol.css | 18 +++++++++++---- .../parts/editor/multiEditorTabsControl.ts | 23 +++++++++---------- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/src/vs/workbench/browser/parts/editor/editorActions.ts b/src/vs/workbench/browser/parts/editor/editorActions.ts index 39c4275b..1b1a39b3 100644 --- a/src/vs/workbench/browser/parts/editor/editorActions.ts +++ b/src/vs/workbench/browser/parts/editor/editorActions.ts @@ -432,7 +432,7 @@ export class UnpinEditorAction extends Action { label: string, @ICommandService private readonly commandService: ICommandService ) { - super(id, label, ThemeIcon.asClassName(Codicon.starFull)); + super(id, label, ThemeIcon.asClassName(Codicon.pinned)); } override run(context?: IEditorCommandsContext): Promise { @@ -440,24 +440,6 @@ export class UnpinEditorAction extends Action { } } -export class PinEditorAction extends Action { - - static readonly ID = 'workbench.action.pinEditor'; - static readonly LABEL = localize('pinEditor', "Pin Editor"); - - constructor( - id: string, - label: string, - @ICommandService private readonly commandService: ICommandService - ) { - super(id, label, ThemeIcon.asClassName(Codicon.star)); - } - - override async run(context?: IEditorCommandsContext): Promise { - return this.commandService.executeCommand('workbench.action.pinEditor', undefined, context); - } -} - export class CloseEditorTabAction extends Action { static readonly ID = 'workbench.action.closeActiveEditor'; diff --git a/src/vs/workbench/browser/parts/editor/media/multieditortabscontrol.css b/src/vs/workbench/browser/parts/editor/media/multieditortabscontrol.css index dd6c233f..93559402 100644 --- a/src/vs/workbench/browser/parts/editor/media/multieditortabscontrol.css +++ b/src/vs/workbench/browser/parts/editor/media/multieditortabscontrol.css @@ -385,7 +385,7 @@ .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab.tab-actions-right.sizing-shrink > .tab-actions, .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab.tab-actions-right.sizing-fixed > .tab-actions { flex: 0; - overflow: visible; /* ensure tab actions are always visible */ + overflow: hidden; /* let the tab actions be pushed out of view when sizing is set to shrink/fixed to make more room */ } .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab.dirty.tab-actions-right.sizing-shrink > .tab-actions, @@ -399,8 +399,18 @@ overflow: visible; /* ...but still show the tab actions on hover, focus and when dirty or sticky */ } +.monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab.close-action-off:not(.dirty) > .tab-actions, .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab.sticky-compact > .tab-actions { - display: none; /* only hide tab actions when sticky-compact */ + display: none; /* hide the tab actions when we are configured to hide it (unless dirty, but always when sticky-compact) */ +} + +.monaco-workbench .part.editor > .content .editor-group-container.active > .title .tabs-container > .tab.active > .tab-actions .action-label, /* always show tab actions for active tab */ +.monaco-workbench .part.editor > .content .editor-group-container.active > .title .tabs-container > .tab > .tab-actions .action-label:focus, /* always show tab actions on focus */ +.monaco-workbench .part.editor > .content .editor-group-container.active > .title .tabs-container > .tab:hover > .tab-actions .action-label, /* always show tab actions on hover */ +.monaco-workbench .part.editor > .content .editor-group-container.active > .title .tabs-container > .tab.active:hover > .tab-actions .action-label, /* always show tab actions on hover */ +.monaco-workbench .part.editor > .content .editor-group-container.active > .title .tabs-container > .tab.sticky:not(.pinned-action-off) > .tab-actions .action-label, /* always show tab actions for sticky tabs */ +.monaco-workbench .part.editor > .content .editor-group-container.active > .title .tabs-container > .tab.dirty > .tab-actions .action-label { /* always show tab actions for dirty tabs */ + opacity: 1; } .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab > .tab-actions .actions-container { @@ -434,11 +444,11 @@ .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab.dirty > .tab-actions .action-label, .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab.sticky:not(.pinned-action-off) > .tab-actions .action-label, .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab:hover > .tab-actions .action-label { - opacity: 1; + opacity: 0.5; /* show tab actions dimmed for inactive group */ } .monaco-workbench .part.editor > .content .editor-group-container > .title .tabs-container > .tab > .tab-actions .action-label { - opacity: 1; + opacity: 0; } /* Tab Actions: Off */ diff --git a/src/vs/workbench/browser/parts/editor/multiEditorTabsControl.ts b/src/vs/workbench/browser/parts/editor/multiEditorTabsControl.ts index bbda32c4..b23be82e 100644 --- a/src/vs/workbench/browser/parts/editor/multiEditorTabsControl.ts +++ b/src/vs/workbench/browser/parts/editor/multiEditorTabsControl.ts @@ -35,7 +35,7 @@ import { MergeGroupMode, IMergeGroupOptions } from '../../../services/editor/com import { addDisposableListener, EventType, EventHelper, Dimension, scheduleAtNextAnimationFrame, findParentWithClass, clearNode, DragAndDropObserver, isMouseEvent, getWindow } from '../../../../base/browser/dom.js'; import { localize } from '../../../../nls.js'; import { IEditorGroupsView, EditorServiceImpl, IEditorGroupView, IInternalEditorOpenOptions, IEditorPartsView } from './editor.js'; -import { CloseEditorTabAction, PinEditorAction, UnpinEditorAction } from './editorActions.js'; +import { CloseEditorTabAction, UnpinEditorAction } from './editorActions.js'; import { assertAllDefined, assertIsDefined } from '../../../../base/common/types.js'; import { IEditorService } from '../../../services/editor/common/editorService.js'; import { basenameOrAuthority } from '../../../../base/common/resources.js'; @@ -113,7 +113,6 @@ export class MultiEditorTabsControl extends EditorTabsControl { private readonly closeEditorAction = this._register(this.instantiationService.createInstance(CloseEditorTabAction, CloseEditorTabAction.ID, CloseEditorTabAction.LABEL)); private readonly unpinEditorAction = this._register(this.instantiationService.createInstance(UnpinEditorAction, UnpinEditorAction.ID, UnpinEditorAction.LABEL)); - private readonly pinEditorAction = this._register(this.instantiationService.createInstance(PinEditorAction, PinEditorAction.ID, PinEditorAction.LABEL)); // Add this line private readonly tabResourceLabels = this._register(this.instantiationService.createInstance(ResourceLabels, DEFAULT_LABELS_CONTAINER)); private tabLabels: IEditorInputLabel[] = []; @@ -1519,28 +1518,28 @@ export class MultiEditorTabsControl extends EditorTabsControl { this.redrawTabLabel(editor, tabIndex, tabContainer, tabLabelWidget, tabLabel); // Action - const hasCloseAction = options.tabActionCloseVisibility; - const hasAction = true; // Always show actions + const hasUnpinAction = isTabSticky && options.tabActionUnpinVisibility; + const hasCloseAction = !hasUnpinAction && options.tabActionCloseVisibility; + const hasAction = hasUnpinAction || hasCloseAction; - // Determine which action to show let tabAction; - if (isTabSticky) { - tabAction = this.unpinEditorAction; + if (hasAction) { + tabAction = hasUnpinAction ? this.unpinEditorAction : this.closeEditorAction; } else { - tabAction = this.pinEditorAction; // Use pin action instead of close action + // Even if the action is not visible, add it as it contains the dirty indicator + tabAction = isTabSticky ? this.unpinEditorAction : this.closeEditorAction; } - // Update action bar if (!tabActionBar.hasAction(tabAction)) { if (!tabActionBar.isEmpty()) { tabActionBar.clear(); } + tabActionBar.push(tabAction, { icon: true, label: false, keybinding: this.getKeybindingLabel(tabAction) }); } - tabContainer.classList.toggle('sticky', isTabSticky); - tabContainer.classList.toggle(`pinned-action-off`, false); - tabContainer.classList.toggle(`close-action-off`, !hasCloseAction); + tabContainer.classList.toggle(`pinned-action-off`, isTabSticky && !hasUnpinAction); + tabContainer.classList.toggle(`close-action-off`, !hasUnpinAction && !hasCloseAction); for (const option of ['left', 'right']) { tabContainer.classList.toggle(`tab-actions-${option}`, hasAction && options.tabActionLocation === option); From 432a1766afce98b2e596d4da271a2480cec1755c Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Sun, 16 Feb 2025 20:50:07 -0800 Subject: [PATCH 28/47] anthropic tool use fix --- .../contrib/void/browser/chatThreadService.ts | 24 +++++++------ .../contrib/void/common/llmMessageTypes.ts | 17 +++++++--- .../contrib/void/common/toolsService.ts | 2 +- .../electron-main/llmMessage/anthropic.ts | 6 ++-- .../void/electron-main/llmMessage/openai.ts | 9 +++-- ...ssMessages.ts => preprocessLLMMessages.ts} | 34 +++++++++++++------ .../llmMessage/sendLLMMessage.ts | 8 ++--- .../void/electron-main/llmMessageChannel.ts | 2 +- 8 files changed, 65 insertions(+), 37 deletions(-) rename src/vs/workbench/contrib/void/electron-main/llmMessage/{processMessages.ts => preprocessLLMMessages.ts} (88%) diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index 81e4a434..03286d38 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -46,24 +46,25 @@ const defaultStaging: StagingInfo = { isBeingEdited: false, selections: [] } // WARNING: changing this format is a big deal!!!!!! need to migrate old format to new format on users' computers so people don't get errors. export type ChatMessage = | { + role: 'system'; + content: string; + displayContent?: undefined; + } | { role: 'user'; content: string | null; // content sent to the llm - allowed to be '', will be replaced with (empty) displayContent: string | null; // content displayed to user - allowed to be '', will be ignored selections: StagingSelectionItem[] | null; // the user's selection staging: StagingInfo | null - } - | { + } | { role: 'assistant'; - tool_calls?: { name: string, id: string, params: string }[]; + tool_calls?: { + name: string, + id: string, + params: string + }[]; content: string | null; // content received from LLM - allowed to be '', will be replaced with (empty) displayContent: string | null; // content displayed to user (this is the same as content for now) - allowed to be '', will be ignored - } - | { - role: 'system'; - content: string; - displayContent?: undefined; - } - | { + } | { role: 'tool'; name: string; // internal use params: string; // internal use @@ -325,7 +326,8 @@ class ChatThreadService extends Disposable implements IChatThreadService { onText: ({ fullText }) => { this._setStreamState(threadId, { messageSoFar: fullText }) }, - onFinalMessage: async ({ fullText, tools }) => { + onFinalMessage: async ({ fullText, toolCalls: tools }) => { + console.log('FINAL MESSAGE', fullText, tools) if ((tools?.length ?? 0) === 0) { this._finishStreamingTextMessage(threadId, fullText) diff --git a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts index e82da2cb..79960826 100644 --- a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts +++ b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts @@ -22,10 +22,6 @@ export const errorDetails = (fullError: Error | null): string | null => { return null } -export type OnText = (p: { newText: string, fullText: string }) => void -export type OnFinalMessage = (p: { fullText: string, tools?: { name: string, params: string, id: string, }[] }) => void // id is tool_use_id -export type OnError = (p: { message: string, fullError: Error | null }) => void -export type AbortRef = { current: (() => void) | null } export type LLMChatMessage = { role: 'system' | 'user'; @@ -41,6 +37,19 @@ export type LLMChatMessage = { id: string; } +export type LLMToolCallType = { + name: string; + params: string; + id: string; +} + + +export type OnText = (p: { newText: string, fullText: string }) => void +export type OnFinalMessage = (p: { fullText: string, toolCalls?: LLMToolCallType[] }) => void // id is tool_use_id +export type OnError = (p: { message: string, fullError: Error | null }) => void +export type AbortRef = { current: (() => void) | null } + + export const toLLMChatMessage = (c: ChatMessage): LLMChatMessage => { if (c.role === 'system' || c.role === 'user') { return { role: c.role, content: c.content ?? '(empty)' } diff --git a/src/vs/workbench/contrib/void/common/toolsService.ts b/src/vs/workbench/contrib/void/common/toolsService.ts index 32004ff2..0c7c7f82 100644 --- a/src/vs/workbench/contrib/void/common/toolsService.ts +++ b/src/vs/workbench/contrib/void/common/toolsService.ts @@ -142,7 +142,7 @@ export class ToolsService implements IToolsService { const queryBuilder = instantiationService.createInstance(QueryBuilder); - const parseObj = (s: string): { [s: string]: unknown } | null => { + const parseObj = (s: string): { [s: string]: unknown } | null => { try { const o = JSON.parse(s) return o diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts index 8d93b0f7..fec9dc07 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts @@ -7,7 +7,7 @@ import Anthropic from '@anthropic-ai/sdk'; import { _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes.js'; import { anthropicMaxPossibleTokens, developerInfoOfModelName, developerInfoOfProviderName } from '../../common/voidSettingsTypes.js'; import { InternalToolInfo } from '../../common/toolsService.js'; -import { addSystemMessageAndToolSupport } from './processMessages.js'; +import { addSystemMessageAndToolSupport } from './preprocessLLMMessages.js'; @@ -86,9 +86,9 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages: stream.on('finalMessage', (response) => { // stringify the response's content const content = response.content.map(c => c.type === 'text' ? c.text : '').join('\n\n') - // const tools = response.content.map(c => c.type === 'tool_use' ? { name: c.name, args: JSON.stringify(c.input), tool_use_id: c.id } : null).filter(c => !!c) + const tools = response.content.map(c => c.type === 'tool_use' ? { name: c.name, params: JSON.stringify(c.input), id: c.id } : null).filter(c => !!c) - onFinalMessage({ fullText: content, tools: [] }) + onFinalMessage({ fullText: content, toolCalls: tools }) }) stream.on('error', (error) => { diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts index 86c41a9c..49dd0bfd 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts @@ -7,7 +7,7 @@ import OpenAI from 'openai'; import { _InternalModelListFnType, _InternalSendLLMFIMMessageFnType, _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes.js'; import { Model } from 'openai/resources/models.js'; import { InternalToolInfo } from '../../common/toolsService.js'; -import { addSystemMessageAndToolSupport } from './processMessages.js'; +import { addSystemMessageAndToolSupport } from './preprocessLLMMessages.js'; import { developerInfoOfModelName, developerInfoOfProviderName } from '../../common/voidSettingsTypes.js'; // import { parseMaxTokensStr } from './util.js'; @@ -192,7 +192,12 @@ export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages: me onText({ newText, fullText }); } - onFinalMessage({ fullText, tools: Object.keys(toolCallOfIndex).map(index => toolCallOfIndex[index]) }); + onFinalMessage({ + fullText, toolCalls: Object.keys(toolCallOfIndex).map(index => { + const tool = toolCallOfIndex[index] + return { name: tool.name, id: tool.id, params: tool.params } + }) + }); }) // when error/fail - this catches errors of both .create() and .then(for await) .catch(error => { diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/processMessages.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/preprocessLLMMessages.ts similarity index 88% rename from src/vs/workbench/contrib/void/electron-main/llmMessage/processMessages.ts rename to src/vs/workbench/contrib/void/electron-main/llmMessage/preprocessLLMMessages.ts index 2ae792fb..8bde8459 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/processMessages.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/preprocessLLMMessages.ts @@ -5,7 +5,14 @@ import { developerInfoOfModelName, developerInfoOfProviderName, ProviderName } f import { deepClone } from '../../../../../base/common/objects.js'; - +export const parseObject = (args: unknown) => { + if (typeof args === 'object') + return args + if (typeof args === 'string') + try { return JSON.parse(args) } + catch (e) { return { args } } + return {} +} // no matter whether the model supports a system message or not (or what format it supports), add it in some way // also take into account tools if the model doesn't support tool use @@ -118,7 +125,7 @@ export const addSystemMessageAndToolSupport = (modelName: string, providerName: } | { type: 'tool_use'; name: string; - input: string; + input: Record; id: string; })[] } | { @@ -127,7 +134,7 @@ export const addSystemMessageAndToolSupport = (modelName: string, providerName: type: 'text'; text: string; } | { - type: 'tool_response'; + type: 'tool_result'; tool_use_id: string; content: string; })[] @@ -141,17 +148,22 @@ export const addSystemMessageAndToolSupport = (modelName: string, providerName: if (currMsg.role !== 'tool') continue const prevMsg = 0 <= i - 1 && i - 1 <= newMessagesTools.length ? newMessagesTools[i - 1] : undefined - const nextMsg = 0 <= i + 1 && i + 1 <= newMessagesTools.length ? newMessagesTools[i + 1] : undefined if (prevMsg?.role === 'assistant') { - if (typeof prevMsg.content === 'string') prevMsg.content = [{ type: 'text', text: typeof prevMsg.content }] - prevMsg.content.push({ type: 'tool_use', name: currMsg.name, input: currMsg.params, id: currMsg.id }) + if (typeof prevMsg.content === 'string') prevMsg.content = [{ type: 'text', text: prevMsg.content }] + prevMsg.content.push({ type: 'tool_use', id: currMsg.id, name: currMsg.name, input: parseObject(currMsg.params) }) } - if (nextMsg?.role === 'user') { - if (typeof nextMsg.content === 'string') nextMsg.content = [{ type: 'text', text: typeof nextMsg.content }] - nextMsg.content.push({ type: 'tool_response', tool_use_id: currMsg.id, content: currMsg.content }) + + // turn each tool into a user message with tool results at the end + newMessagesTools[i] = { + role: 'user', + content: [ + ...[{ type: 'tool_result', tool_use_id: currMsg.id, content: currMsg.content }] as const, + ...currMsg.content ? [{ type: 'text', text: currMsg.content }] as const : [], + ] } } + finalMessages = newMessagesTools } @@ -212,7 +224,7 @@ export const addSystemMessageAndToolSupport = (modelName: string, providerName: id: currMsg.id, function: { name: currMsg.name, - arguments: currMsg.params + arguments: JSON.stringify(currMsg.params) } }] } @@ -236,7 +248,7 @@ export const addSystemMessageAndToolSupport = (modelName: string, providerName: console.log('SYSMG', separateSystemMessage) - console.log('FINAL MESSAGES', finalMessages) + console.log('FINAL MESSAGES', JSON.stringify(finalMessages, null, 2)) return { diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts index e568d3b5..9d179fd8 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts @@ -62,10 +62,10 @@ export const sendLLMMessage = ({ _fullTextSoFar = fullText } - const onFinalMessage: OnFinalMessage = ({ fullText, tools }) => { + const onFinalMessage: OnFinalMessage = ({ fullText, toolCalls: tools }) => { if (_didAbort) return captureLLMEvent(`${loggingName} - Received Full Message`, { messageLength: fullText.length, duration: new Date().getMilliseconds() - submit_time.getMilliseconds() }) - onFinalMessage_({ fullText, tools }) + onFinalMessage_({ fullText, toolCalls: tools }) } const onError: OnError = ({ message: error, fullError }) => { @@ -103,11 +103,11 @@ export const sendLLMMessage = ({ case 'ollama': case 'groq': case 'gemini': - if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - OpenAI FIM', tools: [] }) + if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - OpenAI FIM', toolCalls: [] }) else /* */ sendOpenAIChat({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, aiInstructions, tools }); break; case 'anthropic': - if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Anthropic FIM', tools: [] }) + if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - Anthropic FIM', toolCalls: [] }) else /* */ sendAnthropicChat({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, aiInstructions, tools }); break; default: diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessageChannel.ts b/src/vs/workbench/contrib/void/electron-main/llmMessageChannel.ts index 9db9a68f..333f3919 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessageChannel.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessageChannel.ts @@ -100,7 +100,7 @@ export class LLMMessageChannel implements IServerChannel { const mainThreadParams: SendLLMMessageParams = { ...params, onText: ({ newText, fullText }) => { this._onText_llm.fire({ requestId, newText, fullText }); }, - onFinalMessage: ({ fullText, tools }) => { this._onFinalMessage_llm.fire({ requestId, fullText, tools }); }, + onFinalMessage: ({ fullText, toolCalls: tools }) => { this._onFinalMessage_llm.fire({ requestId, fullText, toolCalls: tools }); }, onError: ({ message: error, fullError }) => { console.log('sendLLM: firing err'); this._onError_llm.fire({ requestId, message: error, fullError }); }, abortRef: this._abortRefOfRequestId_llm[requestId], } From 628adedaec3f76322539604dd7b79cb5611e4ed8 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Sun, 16 Feb 2025 20:51:42 -0800 Subject: [PATCH 29/47] toolcalls --- .../contrib/void/browser/chatThreadService.ts | 10 +++++----- .../contrib/void/electron-main/llmMessage/anthropic.ts | 4 ++-- .../void/electron-main/llmMessage/sendLLMMessage.ts | 4 ++-- .../contrib/void/electron-main/llmMessageChannel.ts | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index 03286d38..98900354 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -326,15 +326,15 @@ class ChatThreadService extends Disposable implements IChatThreadService { onText: ({ fullText }) => { this._setStreamState(threadId, { messageSoFar: fullText }) }, - onFinalMessage: async ({ fullText, toolCalls: tools }) => { - console.log('FINAL MESSAGE', fullText, tools) + onFinalMessage: async ({ fullText, toolCalls }) => { + console.log('FINAL MESSAGE', fullText, toolCalls) - if ((tools?.length ?? 0) === 0) { + if ((toolCalls?.length ?? 0) === 0) { this._finishStreamingTextMessage(threadId, fullText) } else { - this._addMessageToThread(threadId, { role: 'assistant', content: fullText, displayContent: fullText, tool_calls: tools }) - for (const tool of tools ?? []) { + this._addMessageToThread(threadId, { role: 'assistant', content: fullText, displayContent: fullText, tool_calls: toolCalls }) + for (const tool of toolCalls ?? []) { if (!(tool.name in this._toolsService.toolFns)) { this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.params, id: tool.id, content: `Error: This tool was not recognized, so it was not called.`, displayContent: `Error: tool not recognized.`, }) } diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts index fec9dc07..8308bb44 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts @@ -86,9 +86,9 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages: stream.on('finalMessage', (response) => { // stringify the response's content const content = response.content.map(c => c.type === 'text' ? c.text : '').join('\n\n') - const tools = response.content.map(c => c.type === 'tool_use' ? { name: c.name, params: JSON.stringify(c.input), id: c.id } : null).filter(c => !!c) + const toolCalls = response.content.map(c => c.type === 'tool_use' ? { name: c.name, params: JSON.stringify(c.input), id: c.id } : null).filter(c => !!c) - onFinalMessage({ fullText: content, toolCalls: tools }) + onFinalMessage({ fullText: content, toolCalls }) }) stream.on('error', (error) => { diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts index 9d179fd8..980cf5b9 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts @@ -62,10 +62,10 @@ export const sendLLMMessage = ({ _fullTextSoFar = fullText } - const onFinalMessage: OnFinalMessage = ({ fullText, toolCalls: tools }) => { + const onFinalMessage: OnFinalMessage = ({ fullText, toolCalls }) => { if (_didAbort) return captureLLMEvent(`${loggingName} - Received Full Message`, { messageLength: fullText.length, duration: new Date().getMilliseconds() - submit_time.getMilliseconds() }) - onFinalMessage_({ fullText, toolCalls: tools }) + onFinalMessage_({ fullText, toolCalls }) } const onError: OnError = ({ message: error, fullError }) => { diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessageChannel.ts b/src/vs/workbench/contrib/void/electron-main/llmMessageChannel.ts index 333f3919..b00ade9c 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessageChannel.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessageChannel.ts @@ -100,7 +100,7 @@ export class LLMMessageChannel implements IServerChannel { const mainThreadParams: SendLLMMessageParams = { ...params, onText: ({ newText, fullText }) => { this._onText_llm.fire({ requestId, newText, fullText }); }, - onFinalMessage: ({ fullText, toolCalls: tools }) => { this._onFinalMessage_llm.fire({ requestId, fullText, toolCalls: tools }); }, + onFinalMessage: ({ fullText, toolCalls }) => { this._onFinalMessage_llm.fire({ requestId, fullText, toolCalls }); }, onError: ({ message: error, fullError }) => { console.log('sendLLM: firing err'); this._onError_llm.fire({ requestId, message: error, fullError }); }, abortRef: this._abortRefOfRequestId_llm[requestId], } From a78a6169f8a7eccedad1759d7146a993e7b4c381 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Mon, 17 Feb 2025 00:07:11 -0800 Subject: [PATCH 30/47] tools + misc fixes --- .../contrib/void/browser/chatThreadService.ts | 43 ++++++++++++------- .../void/browser/helpers/detectLanguage.ts | 4 ++ .../contrib/void/browser/helpers/readFile.ts | 5 +++ .../void/browser/helpers/systemInfo.ts | 17 ++++++++ .../contrib/void/browser/prompt/prompts.ts | 10 ++++- .../react/src/void-settings-tsx/Settings.tsx | 3 +- .../contrib/void/common/llmMessageTypes.ts | 6 +-- .../contrib/void/common/toolsService.ts | 2 +- 8 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 src/vs/workbench/contrib/void/browser/helpers/systemInfo.ts diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index 98900354..98aad294 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -14,7 +14,7 @@ import { IRange } from '../../../../editor/common/core/range.js'; import { ILLMMessageService } from '../common/llmMessageService.js'; import { IModelService } from '../../../../editor/common/services/model.js'; import { chat_userMessage, chat_systemMessage } from './prompt/prompts.js'; -import { InternalToolInfo, IToolsService, ToolName, voidTools } from '../common/toolsService.js'; +import { InternalToolInfo, IToolsService, ToolFns, ToolName, voidTools } from '../common/toolsService.js'; import { toLLMChatMessage } from '../common/llmMessageTypes.js'; // one of the square items that indicates a selection in a chat bubble (NOT a file, a Selection of text) @@ -57,11 +57,6 @@ export type ChatMessage = staging: StagingInfo | null } | { role: 'assistant'; - tool_calls?: { - name: string, - id: string, - params: string - }[]; content: string | null; // content received from LLM - allowed to be '', will be replaced with (empty) displayContent: string | null; // content displayed to user (this is the same as content for now) - allowed to be '', will be ignored } | { @@ -327,25 +322,43 @@ class ChatThreadService extends Disposable implements IChatThreadService { this._setStreamState(threadId, { messageSoFar: fullText }) }, onFinalMessage: async ({ fullText, toolCalls }) => { + toolCalls = toolCalls?.filter(tool => tool.name in this._toolsService.toolFns) + console.log('FINAL MESSAGE', fullText, toolCalls) if ((toolCalls?.length ?? 0) === 0) { this._finishStreamingTextMessage(threadId, fullText) } else { - this._addMessageToThread(threadId, { role: 'assistant', content: fullText, displayContent: fullText, tool_calls: toolCalls }) + this._addMessageToThread(threadId, { role: 'assistant', content: fullText, displayContent: fullText }) + this._setStreamState(threadId, { messageSoFar: undefined }) // clear streaming message for (const tool of toolCalls ?? []) { - if (!(tool.name in this._toolsService.toolFns)) { - this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.params, id: tool.id, content: `Error: This tool was not recognized, so it was not called.`, displayContent: `Error: tool not recognized.`, }) + const toolName = tool.name as ToolName + + // 1. + let toolResult: Awaited> + try { + toolResult = await this._toolsService.toolFns[toolName](tool.params) + } catch (e) { + this._setStreamState(threadId, { error: e }) + shouldContinue = false + break } - else { - const toolName = tool.name as ToolName - const toolResult = await this._toolsService.toolFns[toolName](tool.params) - const string = this._toolsService.toolResultToString[toolName](toolResult as any) // typescript is so bad it doesn't even couple the type of ToolResult with the type of the function being called here - this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.params, id: tool.id, content: string, displayContent: string, }) - shouldContinue = true + + // 2. + let toolResultStr: string + try { + toolResultStr = this._toolsService.toolResultToString[toolName](toolResult as any) // typescript is so bad it doesn't even couple the type of ToolResult with the type of the function being called here + } catch (e) { + this._setStreamState(threadId, { error: e }) + shouldContinue = false + break } + + this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.params, id: tool.id, content: toolResultStr, displayContent: toolResultStr, }) + shouldContinue = true } + } res_() }, diff --git a/src/vs/workbench/contrib/void/browser/helpers/detectLanguage.ts b/src/vs/workbench/contrib/void/browser/helpers/detectLanguage.ts index b4b9d513..9bc3c9bd 100644 --- a/src/vs/workbench/contrib/void/browser/helpers/detectLanguage.ts +++ b/src/vs/workbench/contrib/void/browser/helpers/detectLanguage.ts @@ -1,3 +1,7 @@ +/*-------------------------------------------------------------------------------------- + * Copyright 2025 Glass Devtools, Inc. All rights reserved. + * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. + *--------------------------------------------------------------------------------------*/ // eg "bash" -> "shell" export const nameToVscodeLanguage: { [key: string]: string } = { diff --git a/src/vs/workbench/contrib/void/browser/helpers/readFile.ts b/src/vs/workbench/contrib/void/browser/helpers/readFile.ts index 39cd310d..4b9e05da 100644 --- a/src/vs/workbench/contrib/void/browser/helpers/readFile.ts +++ b/src/vs/workbench/contrib/void/browser/helpers/readFile.ts @@ -1,3 +1,8 @@ +/*-------------------------------------------------------------------------------------- + * Copyright 2025 Glass Devtools, Inc. All rights reserved. + * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. + *--------------------------------------------------------------------------------------*/ + import { URI } from '../../../../../base/common/uri' import { EndOfLinePreference } from '../../../../../editor/common/model' import { IModelService } from '../../../../../editor/common/services/model.js' diff --git a/src/vs/workbench/contrib/void/browser/helpers/systemInfo.ts b/src/vs/workbench/contrib/void/browser/helpers/systemInfo.ts new file mode 100644 index 00000000..ab1dabad --- /dev/null +++ b/src/vs/workbench/contrib/void/browser/helpers/systemInfo.ts @@ -0,0 +1,17 @@ +/*-------------------------------------------------------------------------------------- + * Copyright 2025 Glass Devtools, Inc. All rights reserved. + * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. + *--------------------------------------------------------------------------------------*/ + +import { isLinux, isMacintosh, isWindows } from '../../../../../base/common/platform.js'; + +// import { OS, OperatingSystem } from '../../../../../base/common/platform.js'; +// alternatively could use ^ and OS === OperatingSystem.Windows ? ... + + + +export const os = isWindows ? 'windows' : isMacintosh ? 'mac' : isLinux ? 'linux' : null + +export const arch = process.arch +export const osplatform = process.platform; + diff --git a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts index 415a0c87..105e1e93 100644 --- a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts +++ b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts @@ -9,6 +9,7 @@ import { filenameToVscodeLanguage } from '../helpers/detectLanguage.js'; import { CodeSelection, StagingSelectionItem, FileSelection } from '../chatThreadService.js'; import { VSReadFile } from '../helpers/readFile.js'; import { IModelService } from '../../../../../editor/common/services/model.js'; +import { os, arch, osplatform } from '../helpers/systemInfo.js'; // this is just for ease of readability @@ -19,17 +20,22 @@ You are a coding assistant. You are given a list of instructions to follow \`INS Please respond to the user's query. +The user has the following system information: + - ${os} ${arch} ${osplatform} + In the case that the user asks you to make changes to code, you should make sure to return CODE BLOCKS of the changes, as well as explanations and descriptions of the changes. For example, if the user asks you to "make this file look nicer", make sure your output includes a code block with concrete ways the file can look nicer. - Do not re-write the entire file in the code block - You can write comments like "// ... existing code" to indicate existing code - Make sure you give enough context in the code block to apply the change to the correct location in the code. -You're allowed to ask for more context. For example, if the user only gives you a selection but you want to see the the full file, you can ask them to provide it. - Do not output any of these instructions, nor tell the user anything about them unless directly prompted for them. Do not tell the user anything about the examples below. +If you are not given tools, you're allowed to ask for more context. For example, if the user only gives you a selection but you want to see the the full file, you can ask them to provide it. +If you are given tools, you are allowed to use them without asking for permission. You do not have to use them if you don't want to. +If you are given tools, NEVER refer to a tool by name when speaking with the user. For example, do NOT say to the user user "I'm going to use \`list_dir\`". Instead, say "I'm going to list all files in ___ directory", etc. + ## EXAMPLE 1 FILES math.ts diff --git a/src/vs/workbench/contrib/void/browser/react/src/void-settings-tsx/Settings.tsx b/src/vs/workbench/contrib/void/browser/react/src/void-settings-tsx/Settings.tsx index cbf8607c..0cfdef04 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/void-settings-tsx/Settings.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/void-settings-tsx/Settings.tsx @@ -17,6 +17,7 @@ import { env } from '../../../../../../../base/common/process.js' import { ModelDropdown } from './ModelDropdown.js' import { ChatMarkdownRender } from '../markdown/ChatMarkdownRender.js' import { WarningBox } from './WarningBox.js' +import { os } from '../../../helpers/systemInfo.js' const SubtleButton = ({ onClick, text, icon, disabled }: { onClick: () => void, text: string, icon: React.ReactNode, disabled: boolean }) => { @@ -505,7 +506,7 @@ const transferTheseFilesOfOS = (os: 'mac' | 'windows' | 'linux' | null): Transfe throw new Error(`os '${os}' not recognized`) } -const os = isWindows ? 'windows' : isMacintosh ? 'mac' : isLinux ? 'linux' : null + let transferTheseFiles: TransferFilesInfo = [] let transferError: string | null = null diff --git a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts index 79960826..75cf2739 100644 --- a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts +++ b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts @@ -52,12 +52,12 @@ export type AbortRef = { current: (() => void) | null } export const toLLMChatMessage = (c: ChatMessage): LLMChatMessage => { if (c.role === 'system' || c.role === 'user') { - return { role: c.role, content: c.content ?? '(empty)' } + return { role: c.role, content: c.content || '(empty message)' } } else if (c.role === 'assistant') - return { role: c.role, content: c.content ?? '(empty model output)' } + return { role: c.role, content: c.content || '(empty message)' } else if (c.role === 'tool') - return { role: c.role, id: c.id, name: c.name, params: c.params, content: c.content ?? '(empty output)' } + return { role: c.role, id: c.id, name: c.name, params: c.params, content: c.content || '(empty output)' } else { throw 1 } diff --git a/src/vs/workbench/contrib/void/common/toolsService.ts b/src/vs/workbench/contrib/void/common/toolsService.ts index 0c7c7f82..2dde2219 100644 --- a/src/vs/workbench/contrib/void/common/toolsService.ts +++ b/src/vs/workbench/contrib/void/common/toolsService.ts @@ -111,7 +111,7 @@ async function generateDirectoryTreeMd(fileService: IFileService, rootURI: URI): const validateURI = (uriStr: unknown) => { - if (typeof uriStr !== 'string') throw new Error('(uri was not a string)') + if (typeof uriStr !== 'string') throw new Error('(provided uri must be a string)') const uri = URI.file(uriStr) return uri } From fecfbd924ad1c22f0efb71e0e523a5e409a8b20a Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Mon, 17 Feb 2025 00:07:33 -0800 Subject: [PATCH 31/47] error --- .../workbench/contrib/void/browser/chatThreadService.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index 98aad294..ea435ed4 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -339,8 +339,8 @@ class ChatThreadService extends Disposable implements IChatThreadService { let toolResult: Awaited> try { toolResult = await this._toolsService.toolFns[toolName](tool.params) - } catch (e) { - this._setStreamState(threadId, { error: e }) + } catch (error) { + this._setStreamState(threadId, { error }) shouldContinue = false break } @@ -349,8 +349,8 @@ class ChatThreadService extends Disposable implements IChatThreadService { let toolResultStr: string try { toolResultStr = this._toolsService.toolResultToString[toolName](toolResult as any) // typescript is so bad it doesn't even couple the type of ToolResult with the type of the function being called here - } catch (e) { - this._setStreamState(threadId, { error: e }) + } catch (error) { + this._setStreamState(threadId, { error }) shouldContinue = false break } From 5608aca1689001bf658bc1a434a5eddc5e4936bb Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Mon, 17 Feb 2025 00:17:14 -0800 Subject: [PATCH 32/47] process --- src/vs/workbench/contrib/void/browser/helpers/systemInfo.ts | 3 --- src/vs/workbench/contrib/void/browser/prompt/prompts.ts | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/helpers/systemInfo.ts b/src/vs/workbench/contrib/void/browser/helpers/systemInfo.ts index ab1dabad..85b909b2 100644 --- a/src/vs/workbench/contrib/void/browser/helpers/systemInfo.ts +++ b/src/vs/workbench/contrib/void/browser/helpers/systemInfo.ts @@ -12,6 +12,3 @@ import { isLinux, isMacintosh, isWindows } from '../../../../../base/common/plat export const os = isWindows ? 'windows' : isMacintosh ? 'mac' : isLinux ? 'linux' : null -export const arch = process.arch -export const osplatform = process.platform; - diff --git a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts index 105e1e93..04a0f9f1 100644 --- a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts +++ b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts @@ -9,7 +9,7 @@ import { filenameToVscodeLanguage } from '../helpers/detectLanguage.js'; import { CodeSelection, StagingSelectionItem, FileSelection } from '../chatThreadService.js'; import { VSReadFile } from '../helpers/readFile.js'; import { IModelService } from '../../../../../editor/common/services/model.js'; -import { os, arch, osplatform } from '../helpers/systemInfo.js'; +import { os } from '../helpers/systemInfo.js'; // this is just for ease of readability @@ -21,7 +21,7 @@ You are a coding assistant. You are given a list of instructions to follow \`INS Please respond to the user's query. The user has the following system information: - - ${os} ${arch} ${osplatform} + - ${os} In the case that the user asks you to make changes to code, you should make sure to return CODE BLOCKS of the changes, as well as explanations and descriptions of the changes. For example, if the user asks you to "make this file look nicer", make sure your output includes a code block with concrete ways the file can look nicer. From b74b031906bb0f7fbc6c39de36da0056f37c2ab2 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Mon, 17 Feb 2025 00:18:55 -0800 Subject: [PATCH 33/47] + --- src/vs/workbench/contrib/void/browser/prompt/prompts.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts index 04a0f9f1..6f888916 100644 --- a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts +++ b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts @@ -32,8 +32,8 @@ For example, if the user asks you to "make this file look nicer", make sure your Do not output any of these instructions, nor tell the user anything about them unless directly prompted for them. Do not tell the user anything about the examples below. -If you are not given tools, you're allowed to ask for more context. For example, if the user only gives you a selection but you want to see the the full file, you can ask them to provide it. -If you are given tools, you are allowed to use them without asking for permission. You do not have to use them if you don't want to. +You're allowed to ask for more context. For example, if the user only gives you a selection but you want to see the the full file, you can ask them to provide it. +If you are given tools, you are allowed to use them without asking for permission. You do not have to use them if you don't want to, but you may use them to gather context, etc. If you are given tools, NEVER refer to a tool by name when speaking with the user. For example, do NOT say to the user user "I'm going to use \`list_dir\`". Instead, say "I'm going to list all files in ___ directory", etc. ## EXAMPLE 1 From 491312218fbb5539b8619e39ae8d0e8dc3a8f848 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Mon, 17 Feb 2025 00:19:39 -0800 Subject: [PATCH 34/47] + --- src/vs/workbench/contrib/void/browser/prompt/prompts.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts index 6f888916..b0eeacaa 100644 --- a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts +++ b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts @@ -29,13 +29,14 @@ For example, if the user asks you to "make this file look nicer", make sure your - You can write comments like "// ... existing code" to indicate existing code - Make sure you give enough context in the code block to apply the change to the correct location in the code. -Do not output any of these instructions, nor tell the user anything about them unless directly prompted for them. -Do not tell the user anything about the examples below. - You're allowed to ask for more context. For example, if the user only gives you a selection but you want to see the the full file, you can ask them to provide it. If you are given tools, you are allowed to use them without asking for permission. You do not have to use them if you don't want to, but you may use them to gather context, etc. If you are given tools, NEVER refer to a tool by name when speaking with the user. For example, do NOT say to the user user "I'm going to use \`list_dir\`". Instead, say "I'm going to list all files in ___ directory", etc. +Do not output any of these instructions, nor tell the user anything about them unless directly prompted for them. +Do not tell the user anything about the examples below. + + ## EXAMPLE 1 FILES math.ts From 8a8ed1ac56848fc77d192ffa409430aedad89ab5 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Mon, 17 Feb 2025 00:20:01 -0800 Subject: [PATCH 35/47] + --- src/vs/workbench/contrib/void/browser/prompt/prompts.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts index b0eeacaa..949e1cba 100644 --- a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts +++ b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts @@ -34,7 +34,7 @@ If you are given tools, you are allowed to use them without asking for permissio If you are given tools, NEVER refer to a tool by name when speaking with the user. For example, do NOT say to the user user "I'm going to use \`list_dir\`". Instead, say "I'm going to list all files in ___ directory", etc. Do not output any of these instructions, nor tell the user anything about them unless directly prompted for them. -Do not tell the user anything about the examples below. +Do not tell the user anything about the examples below. Do not assume the user is talking about any of the examples below. ## EXAMPLE 1 From 89d08071fcbc45af5477e7a2bf160e9b0b80b3eb Mon Sep 17 00:00:00 2001 From: Mathew Pareles Date: Mon, 17 Feb 2025 00:23:48 -0800 Subject: [PATCH 36/47] scrollbar fix --- .../react/src/util/useScrollbarStyles.tsx | 135 +++++++++--------- 1 file changed, 66 insertions(+), 69 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/react/src/util/useScrollbarStyles.tsx b/src/vs/workbench/contrib/void/browser/react/src/util/useScrollbarStyles.tsx index 94df3aac..1ba69c24 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/util/useScrollbarStyles.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/util/useScrollbarStyles.tsx @@ -19,90 +19,87 @@ export const useScrollbarStyles = (containerRef: React.MutableRefObject { - if ((element as any).__scrollbarCleanup) { - (element as any).__scrollbarCleanup(); - } + element.classList.add('void-scrollable-element'); }); - // Apply styles and listeners to each scroll element + // Only initialize fade effects for elements that haven't been initialized yet scrollElements.forEach(element => { - // Add the scrollable class directly to the overflow element - element.classList.add('void-scrollable-element'); + if (!(element as any).__scrollbarCleanup) { + let fadeTimeout: NodeJS.Timeout | null = null; + let fadeInterval: NodeJS.Timeout | null = null; - let fadeTimeout: NodeJS.Timeout | null = null; - let fadeInterval: NodeJS.Timeout | null = null; + const fadeIn = () => { + if (fadeInterval) clearInterval(fadeInterval); - const fadeIn = () => { - if (fadeInterval) clearInterval(fadeInterval); + let step = 0; + fadeInterval = setInterval(() => { + if (step <= 10) { + element.classList.remove(`show-scrollbar-${step - 1}`); + element.classList.add(`show-scrollbar-${step}`); + step++; + } else { + clearInterval(fadeInterval!); + } + }, 10); + }; - let step = 0; - fadeInterval = setInterval(() => { - if (step <= 10) { - element.classList.remove(`show-scrollbar-${step - 1}`); - element.classList.add(`show-scrollbar-${step}`); - step++; - } else { - clearInterval(fadeInterval!); + const fadeOut = () => { + if (fadeInterval) clearInterval(fadeInterval); + + let step = 10; + fadeInterval = setInterval(() => { + if (step >= 0) { + element.classList.remove(`show-scrollbar-${step + 1}`); + element.classList.add(`show-scrollbar-${step}`); + step--; + } else { + clearInterval(fadeInterval!); + } + }, 60); + }; + + const onMouseEnter = () => { + if (fadeTimeout) clearTimeout(fadeTimeout); + if (fadeInterval) clearInterval(fadeInterval); + fadeIn(); + }; + + const onMouseLeave = () => { + if (fadeTimeout) clearTimeout(fadeTimeout); + fadeTimeout = setTimeout(() => { + fadeOut(); + }, 10); + }; + + element.addEventListener('mouseenter', onMouseEnter); + element.addEventListener('mouseleave', onMouseLeave); + + // Store cleanup function + const cleanup = () => { + element.removeEventListener('mouseenter', onMouseEnter); + element.removeEventListener('mouseleave', onMouseLeave); + if (fadeTimeout) clearTimeout(fadeTimeout); + if (fadeInterval) clearInterval(fadeInterval); + element.classList.remove('void-scrollable-element'); + // Remove any remaining show-scrollbar classes + for (let i = 0; i <= 10; i++) { + element.classList.remove(`show-scrollbar-${i}`); } - }, 10); - }; + }; - const fadeOut = () => { - if (fadeInterval) clearInterval(fadeInterval); - - let step = 10; - fadeInterval = setInterval(() => { - if (step >= 0) { - element.classList.remove(`show-scrollbar-${step + 1}`); - element.classList.add(`show-scrollbar-${step}`); - step--; - } else { - clearInterval(fadeInterval!); - } - }, 60); - }; - - const onMouseEnter = () => { - if (fadeTimeout) clearTimeout(fadeTimeout); - if (fadeInterval) clearInterval(fadeInterval); - fadeIn(); - }; - - const onMouseLeave = () => { - if (fadeTimeout) clearTimeout(fadeTimeout); - fadeTimeout = setTimeout(() => { - fadeOut(); - }, 10); - }; - - element.addEventListener('mouseenter', onMouseEnter); - element.addEventListener('mouseleave', onMouseLeave); - - // Store cleanup function - const cleanup = () => { - element.removeEventListener('mouseenter', onMouseEnter); - element.removeEventListener('mouseleave', onMouseLeave); - if (fadeTimeout) clearTimeout(fadeTimeout); - if (fadeInterval) clearInterval(fadeInterval); - element.classList.remove('void-scrollable-element'); - // Remove any remaining show-scrollbar classes - for (let i = 0; i <= 10; i++) { - element.classList.remove(`show-scrollbar-${i}`); - } - }; - - // Store the cleanup function on the element for later use - (element as any).__scrollbarCleanup = cleanup; + // Store the cleanup function on the element for later use + (element as any).__scrollbarCleanup = cleanup; + } }); }; // Initialize for the first time initializeScrollbarStyles(); - // Set up mutation observer - const observer = new MutationObserver((mutations) => { + // Set up mutation observer to do the same + const observer = new MutationObserver(() => { initializeScrollbarStyles(); }); From 366dbf0b52470a6d734524745d498e8235b66788 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Mon, 17 Feb 2025 01:06:21 -0800 Subject: [PATCH 37/47] add xAI and update system/tool information and system prompt --- .../contrib/void/browser/chatThreadService.ts | 4 +- .../contrib/void/browser/prompt/prompts.ts | 3 +- .../react/src/markdown/ChatMarkdownRender.tsx | 15 +- .../void/common/voidSettingsService.ts | 3 + .../contrib/void/common/voidSettingsTypes.ts | 162 +++++++++++------- .../void/electron-main/llmMessage/openai.ts | 8 +- .../llmMessage/preprocessLLMMessages.ts | 4 +- .../llmMessage/sendLLMMessage.ts | 1 + 8 files changed, 123 insertions(+), 77 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index ea435ed4..dab3e62e 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -16,6 +16,7 @@ import { IModelService } from '../../../../editor/common/services/model.js'; import { chat_userMessage, chat_systemMessage } from './prompt/prompts.js'; import { InternalToolInfo, IToolsService, ToolFns, ToolName, voidTools } from '../common/toolsService.js'; import { toLLMChatMessage } from '../common/llmMessageTypes.js'; +import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js'; // one of the square items that indicates a selection in a chat bubble (NOT a file, a Selection of text) export type CodeSelection = { @@ -161,6 +162,7 @@ class ChatThreadService extends Disposable implements IChatThreadService { @IModelService private readonly _modelService: IModelService, @ILLMMessageService private readonly _llmMessageService: ILLMMessageService, @IToolsService private readonly _toolsService: IToolsService, + @IWorkspaceContextService private readonly _workspaceContextService: IWorkspaceContextService, ) { super() @@ -312,7 +314,7 @@ class ChatThreadService extends Disposable implements IChatThreadService { useProviderFor: 'Ctrl+L', logging: { loggingName: `Agent` }, messages: [ - { role: 'system', content: chat_systemMessage }, + { role: 'system', content: chat_systemMessage(this._workspaceContextService.getWorkspace().folders.map(f => f.uri.fsPath)) }, ...this.getCurrentThread().messages.map(m => (toLLMChatMessage(m))), ], diff --git a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts index 949e1cba..88e9144d 100644 --- a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts +++ b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts @@ -15,13 +15,14 @@ import { os } from '../helpers/systemInfo.js'; // this is just for ease of readability export const tripleTick = ['```', '```'] -export const chat_systemMessage = `\ +export const chat_systemMessage = (workspaces: string[]) => `\ You are a coding assistant. You are given a list of instructions to follow \`INSTRUCTIONS\`, and optionally a list of relevant files \`FILES\`, and selections inside of files \`SELECTIONS\`. Please respond to the user's query. The user has the following system information: - ${os} + - Open workspaces: ${workspaces.join(', ')} In the case that the user asks you to make changes to code, you should make sure to return CODE BLOCKS of the changes, as well as explanations and descriptions of the changes. For example, if the user asks you to "make this file look nicer", make sure your output includes a code block with concrete ways the file can look nicer. diff --git a/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx b/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx index ded3eaff..f8184db1 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx @@ -97,7 +97,7 @@ export const CodeSpan = ({ children, className }: { children: React.ReactNode, c } -const RenderToken = ({ token, nested = false, noSpace = false, chatMessageLocation: chatLocation, tokenIdx }: { token: Token | string, nested?: boolean, noSpace?: boolean, chatMessageLocation?: ChatMessageLocation, tokenIdx: string }): JSX.Element => { +const RenderToken = ({ token, nested = false, noSpace = false, chatMessageLocation, tokenIdx }: { token: Token | string, nested?: boolean, noSpace?: boolean, chatMessageLocation?: ChatMessageLocation, tokenIdx: string }): JSX.Element => { // deal with built-in tokens first (assume marked token) @@ -111,16 +111,17 @@ const RenderToken = ({ token, nested = false, noSpace = false, chatMessageLocati if (t.type === "code") { const isCodeblockClosed = t.raw?.startsWith('```') && t.raw?.endsWith('```'); - const applyBoxId = getApplyBoxId({ - threadId: chatLocation!.threadId, - messageIdx: chatLocation!.messageIdx, + // this should never be + const applyBoxId = chatMessageLocation ? getApplyBoxId({ + threadId: chatMessageLocation.threadId, + messageIdx: chatMessageLocation.messageIdx, tokenIdx: tokenIdx, - }) + }) : null return } + buttonsOnHover={applyBoxId && } /> } @@ -195,7 +196,7 @@ const RenderToken = ({ token, nested = false, noSpace = false, chatMessageLocati )} - + ))} diff --git a/src/vs/workbench/contrib/void/common/voidSettingsService.ts b/src/vs/workbench/contrib/void/common/voidSettingsService.ts index eac87692..8fd4aa79 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsService.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsService.ts @@ -175,6 +175,9 @@ class VoidSettingsService extends Disposable implements IVoidSettingsService { // A HACK BECAUSE WE ADDED MISTRAL (did not exist before, comes before readS) ...{ mistral: defaultSettingsOfProvider.mistral }, + // A HACK BECAUSE WE ADDED XAI (did not exist before, comes before readS) + ...{ mistral: defaultSettingsOfProvider.xAI }, + ...readS.settingsOfProvider, // A HACK BECAUSE WE ADDED NEW GEMINI MODELS (existed before, comes after readS) diff --git a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts index 0bbcfcde..abef16d8 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts @@ -11,17 +11,15 @@ import { VoidSettingsState } from './voidSettingsService.js' // developer info used in sendLLMMessage export type DeveloperInfoAtModel = { // USED: - - // TODO!!! think tokens - deepseek - - // TODO!!!! - // UNUSED (coming soon): - recognizedModelName: RecognizedModelName, // used to show user if model was auto-recognized + supportsSystemMessage: 'developer' | boolean, // if null, we will just do a string of system message. this is independent from separateSystemMessage, which takes priority and is passed directly in each provider's implementation. supportsTools: boolean, // we will just do a string of tool use if it doesn't support - supportsSystemMessageRole: 'developer' | 'system' | false, // if null, we will just do a string of system message. this is independent from separateSystemMessage, which takes priority and is passed directly in each provider's implementation. - supportsAutocompleteFIM: boolean, // we will just do a description of FIM if it doens't support <|fim_hole|> - supportsStreaming: boolean, // (o1 does NOT) we will just dump the final result if doesn't support it - maxTokens: number, // required + + // UNUSED (coming soon): + // TODO!!! think tokens - deepseek + _recognizedModelName: RecognizedModelName, // used to show user if model was auto-recognized + _supportsStreaming: boolean, // we will just dump the final result if doesn't support it + _supportsAutocompleteFIM: boolean, // we will just do a description of FIM if it doens't support <|fim_hole|> + _maxTokens: number, // required } export type DeveloperInfoAtProvider = { @@ -49,6 +47,7 @@ export const recognizedModels = [ 'Anthropic Claude', 'Llama 3.x', 'Deepseek Chat', // deepseek coder v2 is now merged into chat (V3) https://api-docs.deepseek.com/updates#deepseek-coder--deepseek-chat-upgraded-to-deepseek-v25-model + 'xAI Grok', // 'xAI Grok', // 'Google Gemini, Gemma', // 'Microsoft Phi4', @@ -59,7 +58,7 @@ export const recognizedModels = [ 'Mistral Codestral', // thinking - 'OpenAI o1, o3', + 'OpenAI o1', 'Deepseek R1', // general @@ -85,11 +84,13 @@ export function recognizedModelOfModelName(modelName: string): RecognizedModelNa if (lower.includes('mistral')) return 'Mistral Codestral'; if (/\bo1\b/.test(lower) || /\bo3\b/.test(lower)) // o1, o3 - return 'OpenAI o1, o3'; + return 'OpenAI o1'; if (lower.includes('deepseek-r1') || lower.includes('deepseek-reasoner')) return 'Deepseek R1'; if (lower.includes('deepseek')) return 'Deepseek Chat' + if (lower.includes('grok')) + return 'xAI Grok' return ''; } @@ -98,18 +99,14 @@ export function recognizedModelOfModelName(modelName: string): RecognizedModelNa const developerInfoAtProvider: { [providerName in ProviderName]: DeveloperInfoAtProvider } = { 'anthropic': { overrideSettingsForAllModels: { - supportsSystemMessageRole: 'system', + supportsSystemMessage: true, supportsTools: true, - supportsAutocompleteFIM: false, - supportsStreaming: true, + _supportsAutocompleteFIM: false, + _supportsStreaming: true, } }, 'deepseek': { overrideSettingsForAllModels: { - supportsSystemMessageRole: false, - supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: true, } }, 'ollama': { @@ -126,6 +123,8 @@ const developerInfoAtProvider: { [providerName in ProviderName]: DeveloperInfoAt }, 'groq': { }, + 'xAI': { + }, } export const developerInfoOfProviderName = (providerName: ProviderName): Partial => { return developerInfoAtProvider[providerName] ?? {} @@ -135,83 +134,93 @@ export const developerInfoOfProviderName = (providerName: ProviderName): Partial // providerName is optional, but gives some extra fallbacks if provided -const developerInfoOfRecognizedModelName: { [recognizedModel in RecognizedModelName]: Omit } = { +const developerInfoOfRecognizedModelName: { [recognizedModel in RecognizedModelName]: Omit } = { 'OpenAI 4o': { - supportsSystemMessageRole: 'system', + supportsSystemMessage: true, supportsTools: true, - supportsAutocompleteFIM: false, - supportsStreaming: true, - maxTokens: 4096, + _supportsAutocompleteFIM: false, + _supportsStreaming: true, + _maxTokens: 4096, }, 'Anthropic Claude': { - supportsSystemMessageRole: 'system', + supportsSystemMessage: true, supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, + _supportsAutocompleteFIM: false, + _supportsStreaming: false, + _maxTokens: 4096, }, 'Llama 3.x': { - supportsSystemMessageRole: false, - supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, + supportsSystemMessage: true, + supportsTools: true, + _supportsAutocompleteFIM: false, + _supportsStreaming: false, + _maxTokens: 4096, + }, + + 'xAI Grok': { + supportsSystemMessage: true, + supportsTools: true, + _supportsAutocompleteFIM: false, + _supportsStreaming: true, + _maxTokens: 4096, + }, 'Deepseek Chat': { - supportsSystemMessageRole: false, + supportsSystemMessage: true, supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, + _supportsAutocompleteFIM: false, + _supportsStreaming: false, + _maxTokens: 4096, }, 'Alibaba Qwen2.5 Coder Instruct': { - supportsSystemMessageRole: false, - supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, + supportsSystemMessage: true, + supportsTools: true, + _supportsAutocompleteFIM: false, + _supportsStreaming: false, + _maxTokens: 4096, }, 'Mistral Codestral': { - supportsSystemMessageRole: false, - supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, + supportsSystemMessage: true, + supportsTools: true, + _supportsAutocompleteFIM: false, + _supportsStreaming: false, + _maxTokens: 4096, }, - 'OpenAI o1, o3': { - supportsSystemMessageRole: false, + 'OpenAI o1': { + supportsSystemMessage: 'developer', supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, + _supportsAutocompleteFIM: false, + _supportsStreaming: true, + _maxTokens: 4096, }, 'Deepseek R1': { - supportsSystemMessageRole: false, + supportsSystemMessage: false, supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, + _supportsAutocompleteFIM: false, + _supportsStreaming: false, + _maxTokens: 4096, }, + '': { - supportsSystemMessageRole: false, + supportsSystemMessage: false, supportsTools: false, - supportsAutocompleteFIM: false, - supportsStreaming: false, - maxTokens: 4096, + _supportsAutocompleteFIM: false, + _supportsStreaming: false, + _maxTokens: 4096, }, } export const developerInfoOfModelName = (modelName: string, overrides?: Partial): DeveloperInfoAtModel => { const recognizedModelName = recognizedModelOfModelName(modelName) return { - recognizedModelName: recognizedModelName, + _recognizedModelName: recognizedModelName, ...developerInfoOfRecognizedModelName[recognizedModelName], ...overrides } @@ -323,6 +332,10 @@ export const defaultMistralModels = modelInfoOfDefaultModelNames([ "mistral-small-latest", ]) +export const defaultXAIModels = modelInfoOfDefaultModelNames([ + 'grok-2-latest', + 'grok-3-latest', +]) // export const parseMaxTokensStr = (maxTokensStr: string) => { // // parse the string but only if the full string is a valid number, eg parseInt('100abc') should return NaN // const int = isNaN(Number(maxTokensStr)) ? undefined : parseInt(maxTokensStr) @@ -378,6 +391,9 @@ export const defaultProviderSettings = { }, mistral: { apiKey: '' + }, + xAI: { + apiKey: '' } } as const @@ -446,7 +462,6 @@ export const displayInfoOfProviderName = (providerName: ProviderName): DisplayIn else if (providerName === 'ollama') { return { title: 'Ollama', - } } else if (providerName === 'openAICompatible') { @@ -469,6 +484,12 @@ export const displayInfoOfProviderName = (providerName: ProviderName): DisplayIn title: 'Mistral API', } } + else if (providerName === 'xAI') { + return { + title: 'xAI API', + } + } + throw new Error(`descOfProviderName: Unknown provider name: "${providerName}"`) } @@ -493,7 +514,8 @@ export const displayInfoOfSettingName = (providerName: ProviderName, settingName providerName === 'groq' ? 'gsk_key...' : providerName === 'mistral' ? 'key...' : providerName === 'openAICompatible' ? 'sk-key...' : - '', + providerName === 'xAI' ? 'xai-key...' : + '', subTextMd: providerName === 'anthropic' ? 'Get your [API Key here](https://console.anthropic.com/settings/keys).' : providerName === 'openAI' ? 'Get your [API Key here](https://platform.openai.com/api-keys).' : @@ -502,8 +524,9 @@ export const displayInfoOfSettingName = (providerName: ProviderName, settingName providerName === 'gemini' ? 'Get your [API Key here](https://aistudio.google.com/apikey).' : providerName === 'groq' ? 'Get your [API Key here](https://console.groq.com/keys).' : providerName === 'mistral' ? 'Get your [API Key here](https://console.mistral.ai/api-keys/).' : - providerName === 'openAICompatible' ? undefined : - '', + providerName === 'xAI' ? 'Get your [API Key here](https://console.x.ai).' : + providerName === 'openAICompatible' ? undefined : + '', } } else if (settingName === 'endpoint') { @@ -574,6 +597,9 @@ export const voidInitModelOptions = { }, mistral: { models: defaultMistralModels, + }, + xAI: { + models: defaultXAIModels, } } @@ -610,6 +636,12 @@ export const defaultSettingsOfProvider: SettingsOfProvider = { ...voidInitModelOptions.mistral, _didFillInProviderSettings: undefined, }, + xAI: { + ...defaultCustomSettings, + ...defaultProviderSettings.xAI, + ...voidInitModelOptions.xAI, + _didFillInProviderSettings: undefined, + }, groq: { // aggregator ...defaultCustomSettings, ...defaultProviderSettings.groq, diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts index 49dd0bfd..4b9ab724 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts @@ -91,9 +91,15 @@ const newOpenAI = ({ settingsOfProvider, providerName }: NewParams) => { baseURL: 'https://api.groq.com/openai/v1', apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true, }) } + else if (providerName === 'xAI') { + const thisConfig = settingsOfProvider[providerName] + return new OpenAI({ + baseURL: 'https://api.x.ai/v1', apiKey: thisConfig.apiKey, dangerouslyAllowBrowser: true, + }) + } else { console.error(`sendOpenAICompatibleMsg: invalid providerName: ${providerName}`) - throw new Error(`providerName was invalid: ${providerName}`) + throw new Error(`Void providerName was invalid: ${providerName}`) } } diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/preprocessLLMMessages.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/preprocessLLMMessages.ts index 8bde8459..eba90468 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/preprocessLLMMessages.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/preprocessLLMMessages.ts @@ -21,7 +21,7 @@ export const addSystemMessageAndToolSupport = (modelName: string, providerName: const messages = deepClone(messages_).map(m => ({ ...m, content: m.content.trim(), })) const { overrideSettingsForAllModels } = developerInfoOfProviderName(providerName) - const { supportsSystemMessageRole: supportsSystemMessage, supportsTools } = developerInfoOfModelName(modelName, overrideSettingsForAllModels) + const { supportsSystemMessage, supportsTools } = developerInfoOfModelName(modelName, overrideSettingsForAllModels) // 1. SYSTEM MESSAGE // find system messages and concatenate them @@ -52,7 +52,7 @@ export const addSystemMessageAndToolSupport = (modelName: string, providerName: if (separateSystemMessage) separateSystemMessageStr = systemMessageStr else { - newMessages.unshift({ role: supportsSystemMessage, content: systemMessageStr }) // add new first message + newMessages.unshift({ role: supportsSystemMessage === 'developer' ? 'developer' : 'system', content: systemMessageStr }) // add new first message } } // if does not support system message diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts index 980cf5b9..7d83cff2 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts @@ -103,6 +103,7 @@ export const sendLLMMessage = ({ case 'ollama': case 'groq': case 'gemini': + case 'xAI': if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - OpenAI FIM', toolCalls: [] }) else /* */ sendOpenAIChat({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, aiInstructions, tools }); break; From 137e0068a3679b4705ecb20c73cc37b6eb40ff0b Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Mon, 17 Feb 2025 01:50:28 -0800 Subject: [PATCH 38/47] add vllm --- .../contrib/void/browser/editCodeService.ts | 6 ---- .../void/common/refreshModelService.ts | 10 ++++-- .../void/common/voidSettingsService.ts | 6 +++- .../contrib/void/common/voidSettingsTypes.ts | 35 +++++++++++++++---- .../void/electron-main/llmMessage/openai.ts | 6 ++++ .../llmMessage/sendLLMMessage.ts | 3 +- 6 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/editCodeService.ts b/src/vs/workbench/contrib/void/browser/editCodeService.ts index 0138cfc1..8468a241 100644 --- a/src/vs/workbench/contrib/void/browser/editCodeService.ts +++ b/src/vs/workbench/contrib/void/browser/editCodeService.ts @@ -1177,19 +1177,13 @@ class EditCodeService extends Disposable implements IEditCodeService { private async _initializeSearchAndReplaceStream({ applyStr }: { applyStr: string }) { - - console.log('SEARCHREPLACE') const uri_ = this._getActiveEditorURI() if (!uri_) return const uri = uri_ - console.log('/* AAAA */') // generate search/replace block text const fileContents = await VSReadFile(this._modelService, uri) if (fileContents === null) return - console.log('/* BBB*/') - - // reject all diffZones on this URI, adding to history (there can't possibly be overlap after this) diff --git a/src/vs/workbench/contrib/void/common/refreshModelService.ts b/src/vs/workbench/contrib/void/common/refreshModelService.ts index ff61e8a8..1c95a4ad 100644 --- a/src/vs/workbench/contrib/void/common/refreshModelService.ts +++ b/src/vs/workbench/contrib/void/common/refreshModelService.ts @@ -45,6 +45,7 @@ export type RefreshModelStateOfProvider = Record { this._clearProviderTimeout(providerName) @@ -158,8 +160,9 @@ export class RefreshModelService extends Disposable implements IRefreshModelServ } } const listFn = providerName === 'ollama' ? this.llmMessageService.ollamaList - : providerName === 'openAICompatible' ? this.llmMessageService.openAICompatibleList - : () => { } + : providerName === 'vLLM' ? this.llmMessageService.openAICompatibleList + : providerName === 'openAICompatible' ? this.llmMessageService.openAICompatibleList + : () => { } listFn({ onSuccess: ({ models }) => { @@ -169,6 +172,7 @@ export class RefreshModelService extends Disposable implements IRefreshModelServ providerName, models.map(model => { if (providerName === 'ollama') return (model as OllamaModelResponse).name; + else if (providerName === 'vLLM') return (model as OpenaiCompatibleModelResponse).id; else if (providerName === 'openAICompatible') return (model as OpenaiCompatibleModelResponse).id; else throw new Error('refreshMode fn: unknown provider', providerName); }), diff --git a/src/vs/workbench/contrib/void/common/voidSettingsService.ts b/src/vs/workbench/contrib/void/common/voidSettingsService.ts index 8fd4aa79..7a35c678 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsService.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsService.ts @@ -176,7 +176,11 @@ class VoidSettingsService extends Disposable implements IVoidSettingsService { ...{ mistral: defaultSettingsOfProvider.mistral }, // A HACK BECAUSE WE ADDED XAI (did not exist before, comes before readS) - ...{ mistral: defaultSettingsOfProvider.xAI }, + ...{ xAI: defaultSettingsOfProvider.xAI }, + + // A HACK BECAUSE WE ADDED VLLM (did not exist before, comes before readS) + ...{ vLLM: defaultSettingsOfProvider.vLLM }, + ...readS.settingsOfProvider, diff --git a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts index abef16d8..14371a22 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts @@ -125,6 +125,8 @@ const developerInfoAtProvider: { [providerName in ProviderName]: DeveloperInfoAt }, 'xAI': { }, + 'vLLM': { + }, } export const developerInfoOfProviderName = (providerName: ProviderName): Partial => { return developerInfoAtProvider[providerName] ?? {} @@ -376,6 +378,9 @@ export const defaultProviderSettings = { ollama: { endpoint: 'http://127.0.0.1:11434', }, + vLLM: { + endpoint: 'http://localhost:8000', + }, openRouter: { apiKey: '', }, @@ -394,13 +399,13 @@ export const defaultProviderSettings = { }, xAI: { apiKey: '' - } + }, } as const export type ProviderName = keyof typeof defaultProviderSettings export const providerNames = Object.keys(defaultProviderSettings) as ProviderName[] -export const localProviderNames = ['ollama'] satisfies ProviderName[] // all local names +export const localProviderNames = ['ollama', 'vLLM'] satisfies ProviderName[] // all local names export const nonlocalProviderNames = providerNames.filter((name) => !(localProviderNames as string[]).includes(name)) // all non-local names type CustomSettingName = UnionOfKeys @@ -464,6 +469,11 @@ export const displayInfoOfProviderName = (providerName: ProviderName): DisplayIn title: 'Ollama', } } + else if (providerName === 'vLLM') { + return { + title: 'vLLM', + } + } else if (providerName === 'openAICompatible') { return { title: 'OpenAI-Compatible', @@ -532,12 +542,14 @@ export const displayInfoOfSettingName = (providerName: ProviderName, settingName else if (settingName === 'endpoint') { return { title: providerName === 'ollama' ? 'Endpoint' : - providerName === 'openAICompatible' ? 'baseURL' // (do not include /chat/completions) - : '(never)', + providerName === 'vLLM' ? 'Endpoint' : + providerName === 'openAICompatible' ? 'baseURL' :// (do not include /chat/completions) + '(never)', placeholder: providerName === 'ollama' ? defaultProviderSettings.ollama.endpoint - : providerName === 'openAICompatible' ? 'https://my-website.com/v1' - : '(never)', + : providerName === 'vLLM' ? defaultProviderSettings.vLLM.endpoint + : providerName === 'openAICompatible' ? 'https://my-website.com/v1' + : '(never)', subTextMd: providerName === 'ollama' ? 'If you would like to change this endpoint, please read more about [Endpoints here](https://github.com/ollama/ollama/blob/main/docs/faq.md#how-can-i-expose-ollama-on-my-network).' : undefined, @@ -583,6 +595,9 @@ export const voidInitModelOptions = { ollama: { models: [], }, + vLLM: { + models: [], + }, openRouter: { models: [], // any string }, @@ -601,7 +616,7 @@ export const voidInitModelOptions = { xAI: { models: defaultXAIModels, } -} +} satisfies Record // used when waiting and for a type reference @@ -666,6 +681,12 @@ export const defaultSettingsOfProvider: SettingsOfProvider = { ...voidInitModelOptions.ollama, _didFillInProviderSettings: undefined, }, + vLLM: { // aggregator + ...defaultCustomSettings, + ...defaultProviderSettings.vLLM, + ...voidInitModelOptions.vLLM, + _didFillInProviderSettings: undefined, + }, } diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts index 4b9ab724..80db8d73 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts @@ -51,6 +51,12 @@ const newOpenAI = ({ settingsOfProvider, providerName }: NewParams) => { baseURL: `${thisConfig.endpoint}/v1`, apiKey: 'noop', dangerouslyAllowBrowser: true, }) } + else if (providerName === 'vLLM') { + const thisConfig = settingsOfProvider[providerName] + return new OpenAI({ + baseURL: `${thisConfig.endpoint}/v1`, apiKey: 'noop', dangerouslyAllowBrowser: true, + }) + } else if (providerName === 'openRouter') { const thisConfig = settingsOfProvider[providerName] return new OpenAI({ diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts index 7d83cff2..8e29bff4 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/sendLLMMessage.ts @@ -101,10 +101,11 @@ export const sendLLMMessage = ({ case 'openAICompatible': case 'mistral': case 'ollama': + case 'vLLM': case 'groq': case 'gemini': case 'xAI': - if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - OpenAI FIM', toolCalls: [] }) + if (messagesType === 'FIMMessage') onFinalMessage({ fullText: 'TODO - OpenAI API FIM', toolCalls: [] }) else /* */ sendOpenAIChat({ messages: messages_, onText, onFinalMessage, onError, settingsOfProvider, modelName, _setAborter, providerName, aiInstructions, tools }); break; case 'anthropic': From 0fd10f404e3f4f316b259379819bc2df15ddc75d Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Mon, 17 Feb 2025 14:12:52 -0800 Subject: [PATCH 39/47] tool results in ChatMessage --- .../contrib/void/browser/chatThreadService.ts | 27 ++++++++++--------- .../contrib/void/browser/prompt/prompts.ts | 6 ++--- .../contrib/void/common/toolsService.ts | 12 ++++----- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index dab3e62e..22c99c67 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -14,7 +14,7 @@ import { IRange } from '../../../../editor/common/core/range.js'; import { ILLMMessageService } from '../common/llmMessageService.js'; import { IModelService } from '../../../../editor/common/services/model.js'; import { chat_userMessage, chat_systemMessage } from './prompt/prompts.js'; -import { InternalToolInfo, IToolsService, ToolFns, ToolName, voidTools } from '../common/toolsService.js'; +import { InternalToolInfo, IToolsService, ToolCallReturnType, ToolFns, ToolName, voidTools } from '../common/toolsService.js'; import { toLLMChatMessage } from '../common/llmMessageTypes.js'; import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js'; @@ -43,6 +43,15 @@ export type StagingInfo = { const defaultStaging: StagingInfo = { isBeingEdited: false, selections: [] } +type ToolMessage = { + role: 'tool'; + name: T; // internal use + params: string; // internal use + id: string; // apis require this tool use id + content: string; // result + result: ToolCallReturnType; // text message of result +} + // WARNING: changing this format is a big deal!!!!!! need to migrate old format to new format on users' computers so people don't get errors. export type ChatMessage = @@ -60,14 +69,7 @@ export type ChatMessage = role: 'assistant'; content: string | null; // content received from LLM - allowed to be '', will be replaced with (empty) displayContent: string | null; // content displayed to user (this is the same as content for now) - allowed to be '', will be ignored - } | { - role: 'tool'; - name: string; // internal use - params: string; // internal use - id: string; // apis require this tool use id - content: string; // result - displayContent: string; // text message of result - } + } | ToolMessage // a 'thread' means a chat message history export type ChatThreads = { @@ -323,8 +325,9 @@ class ChatThreadService extends Disposable implements IChatThreadService { onText: ({ fullText }) => { this._setStreamState(threadId, { messageSoFar: fullText }) }, - onFinalMessage: async ({ fullText, toolCalls }) => { - toolCalls = toolCalls?.filter(tool => tool.name in this._toolsService.toolFns) + onFinalMessage: async ({ fullText, toolCalls: toolCalls_ }) => { + // make sure all tool names are valid so we can cast to ToolName below + const toolCalls = toolCalls_?.filter(tool => tool.name in this._toolsService.toolFns) console.log('FINAL MESSAGE', fullText, toolCalls) @@ -357,7 +360,7 @@ class ChatThreadService extends Disposable implements IChatThreadService { break } - this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.params, id: tool.id, content: toolResultStr, displayContent: toolResultStr, }) + this._addMessageToThread(threadId, { role: 'tool', name: toolName, params: tool.params, id: tool.id, content: toolResultStr, result: toolResult, }) shouldContinue = true } diff --git a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts index 88e9144d..2b840f83 100644 --- a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts +++ b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts @@ -18,7 +18,7 @@ export const tripleTick = ['```', '```'] export const chat_systemMessage = (workspaces: string[]) => `\ You are a coding assistant. You are given a list of instructions to follow \`INSTRUCTIONS\`, and optionally a list of relevant files \`FILES\`, and selections inside of files \`SELECTIONS\`. -Please respond to the user's query. +Please respond to the user's query. The user's query is never invalid. The user has the following system information: - ${os} @@ -26,8 +26,8 @@ The user has the following system information: In the case that the user asks you to make changes to code, you should make sure to return CODE BLOCKS of the changes, as well as explanations and descriptions of the changes. For example, if the user asks you to "make this file look nicer", make sure your output includes a code block with concrete ways the file can look nicer. - - Do not re-write the entire file in the code block - - You can write comments like "// ... existing code" to indicate existing code + - Do not re-write the entire file in the code block. + - You can write comments like "// ... existing code" to indicate existing code. - Make sure you give enough context in the code block to apply the change to the correct location in the code. You're allowed to ask for more context. For example, if the user only gives you a selection but you want to see the the full file, you can ask them to provide it. diff --git a/src/vs/workbench/contrib/void/common/toolsService.ts b/src/vs/workbench/contrib/void/common/toolsService.ts index 2dde2219..07733c86 100644 --- a/src/vs/workbench/contrib/void/common/toolsService.ts +++ b/src/vs/workbench/contrib/void/common/toolsService.ts @@ -81,14 +81,14 @@ export type ToolParamsObj = { [paramName in ToolParamNames - = T extends 'read_file' ? Promise - : T extends 'list_dir' ? Promise - : T extends 'pathname_search' ? Promise - : T extends 'search' ? Promise + = T extends 'read_file' ? string + : T extends 'list_dir' ? string + : T extends 'pathname_search' ? string | URI[] + : T extends 'search' ? string | URI[] : never -export type ToolFns = { [T in ToolName]: (p: string) => ToolCallReturnType } -export type ToolResultToString = { [T in ToolName]: (result: Awaited>) => string } +export type ToolFns = { [T in ToolName]: (p: string) => Promise> } +export type ToolResultToString = { [T in ToolName]: (result: ToolCallReturnType) => string } async function generateDirectoryTreeMd(fileService: IFileService, rootURI: URI): Promise { From b92420012c01a69a698152d3b5d5db44a90e7f94 Mon Sep 17 00:00:00 2001 From: Mathew Pareles Date: Mon, 17 Feb 2025 15:18:11 -0800 Subject: [PATCH 40/47] minor --- .../contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx index 52944476..88b47355 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/sidebar-tsx/SidebarChat.tsx @@ -703,7 +703,6 @@ const ChatBubble = ({ chatMessage, isLoading, messageIdx }: { chatMessage: ChatM : role === 'user' ? `px-2 self-end w-fit max-w-full whitespace-pre-wrap` // user words should be pre : role === 'assistant' ? `px-2 self-start w-full max-w-full` : '' } - ${role !== 'assistant' ? 'my-2' : ''} `} onMouseEnter={() => setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} From d3547134e7552eff9b650cfb00f0e5d83d042004 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Mon, 17 Feb 2025 15:22:24 -0800 Subject: [PATCH 41/47] get ready to add searchReplaceService --- .../contrib/void/browser/editCodeService.ts | 4 +- .../void/browser/searchReplaceService.ts | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/vs/workbench/contrib/void/browser/searchReplaceService.ts diff --git a/src/vs/workbench/contrib/void/browser/editCodeService.ts b/src/vs/workbench/contrib/void/browser/editCodeService.ts index 8468a241..989086a6 100644 --- a/src/vs/workbench/contrib/void/browser/editCodeService.ts +++ b/src/vs/workbench/contrib/void/browser/editCodeService.ts @@ -1192,7 +1192,7 @@ class EditCodeService extends Disposable implements IEditCodeService { const userMessageContent = searchReplace_userMessage({ originalCode: fileContents, applyStr: applyStr }) const messages: LLMChatMessage[] = [ { role: 'system', content: searchReplace_systemMessage }, - { role: 'user', content: userMessageContent } + { role: 'user', content: userMessageContent }, ] let streamRequestIdRef: { current: string | null } = { current: null } @@ -1232,8 +1232,10 @@ class EditCodeService extends Disposable implements IEditCodeService { } + // any time there's an error, add assistant's message, then user message saying the problem and to retry // TODO!!! turn this into a service and provide it + // this generates >>>>>>> ORIGINAL <<<<<<< REPLACE blocks and and simultaneously applies it streamRequestIdRef.current = this._llmMessageService.sendLLMMessage({ messagesType: 'chatMessages', useProviderFor: 'Apply', diff --git a/src/vs/workbench/contrib/void/browser/searchReplaceService.ts b/src/vs/workbench/contrib/void/browser/searchReplaceService.ts new file mode 100644 index 00000000..9bf958c4 --- /dev/null +++ b/src/vs/workbench/contrib/void/browser/searchReplaceService.ts @@ -0,0 +1,37 @@ +/*-------------------------------------------------------------------------------------- + * Copyright 2025 Glass Devtools, Inc. All rights reserved. + * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. + *--------------------------------------------------------------------------------------*/ + +import { Emitter, Event } from '../../../../base/common/event.js'; +import { Disposable } from '../../../../base/common/lifecycle.js'; +import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js'; +import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js'; +import { ILLMMessageService } from '../common/llmMessageService.js'; + + + +export interface ISearchReplaceService { + readonly _serviceBrand: undefined; +} + +export const ISearchReplaceService = createDecorator('SearchReplaceService'); +class SearchReplaceService extends Disposable implements ISearchReplaceService { + _serviceBrand: undefined; + + static readonly ID = 'SearchReplaceService'; + + private readonly _onDidChangeState = new Emitter(); + readonly onDidChangeState: Event = this._onDidChangeState.event; + + constructor( + @ILLMMessageService private readonly llmMessageService: ILLMMessageService, + ) { + super() + // this.llmMessageService.sendLLMMessage({}) + + } + +} + +registerSingleton(ISearchReplaceService, SearchReplaceService, InstantiationType.Eager); From d3aa0bc3cc5a5da3cfc29d42ba5c33814c1112e2 Mon Sep 17 00:00:00 2001 From: Mathew Pareles Date: Mon, 17 Feb 2025 17:56:20 -0800 Subject: [PATCH 42/47] fix readfile --- .../contrib/void/browser/helpers/readFile.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/helpers/readFile.ts b/src/vs/workbench/contrib/void/browser/helpers/readFile.ts index f7752b84..7c03f036 100644 --- a/src/vs/workbench/contrib/void/browser/helpers/readFile.ts +++ b/src/vs/workbench/contrib/void/browser/helpers/readFile.ts @@ -20,7 +20,7 @@ export const VSReadFile = async (modelService: IModelService, fileService: IFile // read files from VSCode. preferred (but appears to only work if the model of this URI already exists. If it doesn't use the other function.) export const _VSReadModel = async (modelService: IModelService, uri: URI): Promise => { - // attempt to read saved model (sometimes doesn't work if page is reloaded) + // attempt to read saved model (doesn't work if application was reloaded...) const model = modelService.getModel(uri) if (model) { return model.getValue(EndOfLinePreference.LF) @@ -38,7 +38,12 @@ export const _VSReadModel = async (modelService: IModelService, uri: URI): Promi } export const _VSReadFileRaw = async (fileService: IFileService, uri: URI) => { - const res = await fileService.readFile(uri) - const str = res.value.toString() - return str + try { + const res = await fileService.readFile(uri) + const str = res.value.toString() + return str + } catch (e) { + return '' + } + } From 667769c987741d38b8c102348bce99fa68bf1a58 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Mon, 17 Feb 2025 23:57:59 -0800 Subject: [PATCH 43/47] tool update; multiple SEARCH/REPLACE blocks --- .../contrib/void/browser/aiRegexService.ts | 2 +- .../contrib/void/browser/chatThreadService.ts | 18 +- .../contrib/void/browser/editCodeService.ts | 243 ++++++++++++------ .../contrib/void/browser/prompt/prompts.ts | 37 +-- ...ervice.ts => searchReplaceCacheService.ts} | 17 +- .../contrib/void/common/voidSettingsTypes.ts | 3 +- 6 files changed, 206 insertions(+), 114 deletions(-) rename src/vs/workbench/contrib/void/browser/{searchReplaceService.ts => searchReplaceCacheService.ts} (75%) diff --git a/src/vs/workbench/contrib/void/browser/aiRegexService.ts b/src/vs/workbench/contrib/void/browser/aiRegexService.ts index c0ae27fa..f38236a9 100644 --- a/src/vs/workbench/contrib/void/browser/aiRegexService.ts +++ b/src/vs/workbench/contrib/void/browser/aiRegexService.ts @@ -44,7 +44,7 @@ export const IVoidFastApplyService = createDecorator('voidFas class VoidFastApplyService extends Disposable implements IFastApplyService { _serviceBrand: undefined; - static readonly ID = 'voidFastApplyService'; + // static readonly ID = 'voidFastApplyService'; private readonly _onDidChangeState = new Emitter(); readonly onDidChangeState: Event = this._onDidChangeState.event; diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index 22c99c67..244256a6 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -304,9 +304,12 @@ class ChatThreadService extends Disposable implements IChatThreadService { // agent loop const agentLoop = async () => { - let shouldContinue = false - do { - shouldContinue = false + let shouldSendAnotherMessage = true + let nMessagesSent = 0 + + while (shouldSendAnotherMessage) { + shouldSendAnotherMessage = false + nMessagesSent += 1 let res_: () => void const awaitable = new Promise((res, rej) => { res_ = res }) @@ -329,8 +332,6 @@ class ChatThreadService extends Disposable implements IChatThreadService { // make sure all tool names are valid so we can cast to ToolName below const toolCalls = toolCalls_?.filter(tool => tool.name in this._toolsService.toolFns) - console.log('FINAL MESSAGE', fullText, toolCalls) - if ((toolCalls?.length ?? 0) === 0) { this._finishStreamingTextMessage(threadId, fullText) } @@ -346,7 +347,7 @@ class ChatThreadService extends Disposable implements IChatThreadService { toolResult = await this._toolsService.toolFns[toolName](tool.params) } catch (error) { this._setStreamState(threadId, { error }) - shouldContinue = false + shouldSendAnotherMessage = false break } @@ -356,12 +357,12 @@ class ChatThreadService extends Disposable implements IChatThreadService { toolResultStr = this._toolsService.toolResultToString[toolName](toolResult as any) // typescript is so bad it doesn't even couple the type of ToolResult with the type of the function being called here } catch (error) { this._setStreamState(threadId, { error }) - shouldContinue = false + shouldSendAnotherMessage = false break } this._addMessageToThread(threadId, { role: 'tool', name: toolName, params: tool.params, id: tool.id, content: toolResultStr, result: toolResult, }) - shouldContinue = true + shouldSendAnotherMessage = true } } @@ -377,7 +378,6 @@ class ChatThreadService extends Disposable implements IChatThreadService { await awaitable } - while (shouldContinue); } agentLoop() // DO NOT AWAIT THIS, this fn should resolve when ready to clear inputs diff --git a/src/vs/workbench/contrib/void/browser/editCodeService.ts b/src/vs/workbench/contrib/void/browser/editCodeService.ts index 989086a6..b0a0d197 100644 --- a/src/vs/workbench/contrib/void/browser/editCodeService.ts +++ b/src/vs/workbench/contrib/void/browser/editCodeService.ts @@ -1182,14 +1182,14 @@ class EditCodeService extends Disposable implements IEditCodeService { const uri = uri_ // generate search/replace block text - const fileContents = await VSReadFile(this._modelService, uri) - if (fileContents === null) return + const origFileContents = await VSReadFile(this._modelService, uri) + if (origFileContents === null) return // reject all diffZones on this URI, adding to history (there can't possibly be overlap after this) this.removeDiffAreas({ uri, behavior: 'reject', removeCtrlKs: true }) - const userMessageContent = searchReplace_userMessage({ originalCode: fileContents, applyStr: applyStr }) + const userMessageContent = searchReplace_userMessage({ originalCode: origFileContents, applyStr: applyStr }) const messages: LLMChatMessage[] = [ { role: 'system', content: searchReplace_systemMessage }, { role: 'user', content: userMessageContent }, @@ -1197,6 +1197,7 @@ class EditCodeService extends Disposable implements IEditCodeService { let streamRequestIdRef: { current: string | null } = { current: null } const diffareaidOfBlockNum: number[] = [] + const diffAreaOriginalLines: [number, number][] = [] // TODO replace all these with whatever block we're on initially if already started let latestStreamLocationMutable: StreamLocationMutable | null = null @@ -1215,10 +1216,15 @@ class EditCodeService extends Disposable implements IEditCodeService { return [startLine, endLine] } - const { onFinishEdit } = this._addToHistory(uri) + let { onFinishEdit } = this._addToHistory(uri) + const revertAndContinueHistory = () => { + this._undoHistory(uri) + const { onFinishEdit: onFinishEdit_ } = this._addToHistory(uri) + onFinishEdit = onFinishEdit_ + } - const onDone = (hadError: boolean) => { + const onDone = (errorMessage: false | string) => { for (const blockNum in diffareaidOfBlockNum) { const diffareaid = diffareaidOfBlockNum[blockNum] const diffZone = this.diffAreaOfId[diffareaid] @@ -1227,106 +1233,175 @@ class EditCodeService extends Disposable implements IEditCodeService { this._onDidChangeStreaming.fire({ uri, diffareaid: diffZone.diffareaid }) } this._refreshStylesAndDiffsInURI(uri) - if (hadError) this._undoHistory(uri) + if (errorMessage) { + this._notificationService.info(`Void had an error when running Apply: ${errorMessage}.\nIf this persists, feel free to [report](https://github.com/voideditor/void/issues/new) this error.`) + this._metricsService.capture('Error - Apply', { errorMessage }) + this._undoHistory(uri) + } onFinishEdit() } - // any time there's an error, add assistant's message, then user message saying the problem and to retry + const onNewBlockStart = (blockNum: number, block: ExtractedSearchReplaceBlock): { errorStartingBlock?: undefined } | { errorStartingBlock: string } => { + console.log('STARTING BLOCK', JSON.stringify(block, null, 2)) - // TODO!!! turn this into a service and provide it + + const foundInCode = findTextInCode(block.orig, origFileContents) + if (typeof foundInCode === 'string') { + console.log('Apply error:', foundInCode, '; trying again.') + return { errorStartingBlock: foundInCode } + } + const [originalStart, originalEnd] = foundInCode + + let lineOffset = 0 + // compute line offset given multiple changes + for (let i = 0; i < blockNum; i += 1) { + const [diffAreaOriginalStart, diffAreaOriginalEnd] = diffAreaOriginalLines[i] + console.log('ROIGGINAL!!!', diffAreaOriginalStart, diffAreaOriginalEnd) + if (diffAreaOriginalStart > originalEnd) continue + + const diffareaid = diffareaidOfBlockNum[i] + const diffArea = this.diffAreaOfId[diffareaid] + + + const numNewLines = diffArea.endLine - diffArea.startLine + const numOldLines = diffAreaOriginalEnd - diffAreaOriginalStart + console.log('NUM NEW', numNewLines, numOldLines) + + lineOffset += numNewLines - numOldLines + } + + const startLine = originalStart + lineOffset + const endLine = originalEnd + lineOffset + console.log('adding to', startLine, endLine) + + const adding: Omit = { + type: 'DiffZone', + originalCode: block.orig, + startLine, + endLine, + _URI: uri, + _streamState: { + isStreaming: true, + streamRequestIdRef, + line: startLine, + }, + _diffOfId: {}, // added later + _removeStylesFns: new Set(), + } + const diffZone = this._addDiffArea(adding) + this._onDidChangeStreaming.fire({ uri, diffareaid: diffZone.diffareaid }) + this._onDidAddOrDeleteDiffZones.fire({ uri }) + + diffareaidOfBlockNum.push(diffZone.diffareaid) + diffAreaOriginalLines.push([originalStart, originalEnd]) + + latestStreamLocationMutable = { line: diffZone.startLine, addedSplitYet: false, col: 1, originalCodeStartLine: 1 } + return { errorStartingBlock: undefined } + } + + + + + + + let shouldSendAnotherMessage = true + let nMessagesSent = 0 // this generates >>>>>>> ORIGINAL <<<<<<< REPLACE blocks and and simultaneously applies it - streamRequestIdRef.current = this._llmMessageService.sendLLMMessage({ - messagesType: 'chatMessages', - useProviderFor: 'Apply', - logging: { loggingName: `generateSearchAndReplace` }, - messages, - onText: ({ fullText }) => { - const blocks = extractSearchReplaceBlocks(fullText) + while (shouldSendAnotherMessage) { + shouldSendAnotherMessage = false + nMessagesSent += 1 - for (let blockNum = currStreamingBlockNum; blockNum < blocks.length; blockNum += 1) { - const block = blocks[blockNum] + streamRequestIdRef.current = this._llmMessageService.sendLLMMessage({ + messagesType: 'chatMessages', + useProviderFor: 'Apply', + logging: { loggingName: `generateSearchAndReplace` }, + messages, + onText: ({ fullText }) => { + const blocks = extractSearchReplaceBlocks(fullText) - if (block.state === 'done') - currStreamingBlockNum = blockNum + for (let blockNum = currStreamingBlockNum; blockNum < blocks.length; blockNum += 1) { + const block = blocks[blockNum] - if (block.state === 'writingOriginal') // must be done writing original - continue + if (block.state === 'done') + currStreamingBlockNum = blockNum - // if should add new diffarea - if (!(blockNum in diffareaidOfBlockNum)) { - const foundInCode = findTextInCode(block.orig, fileContents) - if (typeof foundInCode === 'string') { - // TODO!!! log and retry - console.log('NOT FOUND IN CODE!!!!', foundInCode) + if (block.state === 'writingOriginal') // must be done writing original continue + + // if this is the first time we're seeing this block, add it as a diffarea + if (!(blockNum in diffareaidOfBlockNum)) { + console.log('FULLTEXT!!!!!\n', fullText) + const { errorStartingBlock } = onNewBlockStart(blockNum, block) + + if (errorStartingBlock) { + console.log('ERROR STARTING BLOCK SPOT!!!!!', errorStartingBlock) + + const errMsgForLLM = errorStartingBlock === 'Not found' ? + 'I interrupted you because the latest ORIGINAL code could not be found in the file. Please output all SEARCH/REPLACE blocks again, making sure the code in ORIGINAL is identical to a code snippet in the file.' + : errorStartingBlock === 'Not unique' ? + 'I interrupted you because the latest ORIGINAL code shows up multiple times in the file. Please output all SEARCH/REPLACE blocks again, making sure the code in each ORIGINAL section is unique in the file.' + : '' + + messages.push( + { role: 'assistant', content: fullText }, // latest output + { role: 'user', content: errMsgForLLM } // user explanation of what's wrong + ) + if (streamRequestIdRef.current) this._llmMessageService.abort(streamRequestIdRef.current) + + shouldSendAnotherMessage = true + revertAndContinueHistory() + return + } + } - const [startLine, endLine] = foundInCode + const deltaFinalText = block.final.substring((oldBlocks[blockNum]?.final ?? '').length, Infinity) + oldBlocks = blocks - const adding: Omit = { - type: 'DiffZone', - originalCode: block.orig, - startLine, - endLine, - _URI: uri, - _streamState: { - isStreaming: true, - streamRequestIdRef, - line: startLine, - }, - _diffOfId: {}, // added later - _removeStylesFns: new Set(), - } - const diffZone = this._addDiffArea(adding) - this._onDidChangeStreaming.fire({ uri, diffareaid: diffZone.diffareaid }) - this._onDidAddOrDeleteDiffZones.fire({ uri }) + // write new text to diffarea + const diffareaid = diffareaidOfBlockNum[blockNum] + const diffZone = this.diffAreaOfId[diffareaid] + if (diffZone?.type !== 'DiffZone') continue - diffareaidOfBlockNum.push(diffZone.diffareaid) - latestStreamLocationMutable = { line: diffZone.startLine, addedSplitYet: false, col: 1, originalCodeStartLine: 1 } + if (!latestStreamLocationMutable) continue + this._writeStreamedDiffZoneLLMText(diffZone, block.final, deltaFinalText, latestStreamLocationMutable) + } // end for + this._refreshStylesAndDiffsInURI(uri) + }, + onFinalMessage: async ({ fullText }) => { + console.log('final message!!', fullText) + + // 1. wait 500ms and fix lint errors - call lint error workflow + // (update react state to say "Fixing errors") + const blocks = extractSearchReplaceBlocks(fullText) + + if (blocks.length === 0) { + this._notificationService.info(`Void: When running Apply, your model didn't output any changes we recognized. You might need to use a smarter model for Apply.`) } - const deltaFinalText = block.final.substring((oldBlocks[blockNum]?.final ?? '').length, Infinity) - oldBlocks = blocks - // write new text to diffarea - const diffareaid = diffareaidOfBlockNum[blockNum] - const diffZone = this.diffAreaOfId[diffareaid] - if (diffZone?.type !== 'DiffZone') continue + for (let blockNum = 0; blockNum < blocks.length; blockNum += 1) { + const block = blocks[blockNum] + const diffareaid = diffareaidOfBlockNum[blockNum] + const diffZone = this.diffAreaOfId[diffareaid] + if (diffZone?.type !== 'DiffZone') continue + this._writeText(uri, block.final, + { startLineNumber: diffZone.startLine, startColumn: 1, endLineNumber: diffZone.endLine, endColumn: Number.MAX_SAFE_INTEGER }, // 1-indexed + { shouldRealignDiffAreas: true } + ) + } + onDone(false) + }, + onError: (e) => { + console.log('ERROR in SearchReplace:', e.message) + onDone(e.message) + }, - if (!latestStreamLocationMutable) continue - this._writeStreamedDiffZoneLLMText(diffZone, block.final, deltaFinalText, latestStreamLocationMutable) - } // end for + }) + } - this._refreshStylesAndDiffsInURI(uri) - }, - onFinalMessage: async ({ fullText }) => { - console.log('/* ON FINALMESSAGE */', fullText) - - // 1. wait 500ms and fix lint errors - call lint error workflow - // (update react state to say "Fixing errors") - const blocks = extractSearchReplaceBlocks(fullText) - - for (let blockNum = 0; blockNum < blocks.length; blockNum += 1) { - const block = blocks[blockNum] - const diffareaid = diffareaidOfBlockNum[blockNum] - const diffZone = this.diffAreaOfId[diffareaid] - if (diffZone?.type !== 'DiffZone') continue - - this._writeText(uri, block.final, - { startLineNumber: diffZone.startLine, startColumn: 1, endLineNumber: diffZone.endLine, endColumn: Number.MAX_SAFE_INTEGER }, // 1-indexed - { shouldRealignDiffAreas: true } - ) - } - onDone(false) - }, - onError: (e) => { - console.log('ERROR', e); - onDone(true) - }, - - }) } diff --git a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts index 2b840f83..a86e82b1 100644 --- a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts +++ b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts @@ -21,23 +21,25 @@ You are a coding assistant. You are given a list of instructions to follow \`INS Please respond to the user's query. The user's query is never invalid. The user has the following system information: - - ${os} - - Open workspaces: ${workspaces.join(', ')} +- ${os} +- Open workspaces: ${workspaces.join(', ')} In the case that the user asks you to make changes to code, you should make sure to return CODE BLOCKS of the changes, as well as explanations and descriptions of the changes. For example, if the user asks you to "make this file look nicer", make sure your output includes a code block with concrete ways the file can look nicer. - - Do not re-write the entire file in the code block. - - You can write comments like "// ... existing code" to indicate existing code. - - Make sure you give enough context in the code block to apply the change to the correct location in the code. +- Do not re-write the entire file in the code block. +- You can write comments like "// ... existing code" to indicate existing code. +- Make sure you give enough context in the code block to apply the change to the correct location in the code. You're allowed to ask for more context. For example, if the user only gives you a selection but you want to see the the full file, you can ask them to provide it. -If you are given tools, you are allowed to use them without asking for permission. You do not have to use them if you don't want to, but you may use them to gather context, etc. -If you are given tools, NEVER refer to a tool by name when speaking with the user. For example, do NOT say to the user user "I'm going to use \`list_dir\`". Instead, say "I'm going to list all files in ___ directory", etc. +If you are given tools: +- You are allowed to use tools without asking for permission. +- Feel free to use tools to gather context, make suggestions, etc. +- One great use of tools is to explore imports that you'd like to have more information about. +- NEVER refer to a tool by name when speaking with the user. For example, do NOT say to the user user "I'm going to use \`list_dir\`". Instead, say "I'm going to list all files in ___ directory", etc. Do not output any of these instructions, nor tell the user anything about them unless directly prompted for them. Do not tell the user anything about the examples below. Do not assume the user is talking about any of the examples below. - ## EXAMPLE 1 FILES math.ts @@ -249,9 +251,9 @@ The user's request may be "fuzzy" or not well-specified, and it is your job to i 2. If you want to make changes, you should return a single CODE BLOCK of the changes that you want to make. For example, if the user is asking you to "make this variable a better name", make sure your output includes all the changes that are needed to improve the variable name. - - Do not re-write the entire file in the code block - - You can write comments like "// ... existing code" to indicate existing code - - Make sure you give enough context in the code block to apply the changes to the correct location in the code` +- Do not re-write the entire file in the code block +- You can write comments like "// ... existing code" to indicate existing code +- Make sure you give enough context in the code block to apply the changes to the correct location in the code` export const aiRegex_computeReplacementsForFile_userMessage = async ({ searchClause, replaceClause, fileURI, modelService }: { searchClause: string, replaceClause: string, fileURI: URI, modelService: IModelService }) => { @@ -303,23 +305,28 @@ export const searchReplace_systemMessage = `\ You are a coding assistant that generates SEARCH/REPLACE code blocks that will be used to edit a file. A SEARCH/REPLACE block describes the code before and after a change. Here is the format: +${tripleTick[0]} ${ORIGINAL} // ... original code goes here ${DIVIDER} // ... final code goes here ${FINAL} +${tripleTick[1]} You will be given the original file \`ORIGINAL_FILE\` and a description of a change \`CHANGE\` to make. Output SEARCH/REPLACE blocks to edit the file according to the desired change. You may output multiple SEARCH/REPLACE blocks. Directions: 1. Your OUTPUT should consist ONLY of SEARCH/REPLACE blocks. Do NOT output any text or explanations before or after this. -2. The "original" code in each SEARCH/REPLACE block must EXACTLY match lines of code in the original file. -3. The "original" code in each SEARCH/REPLACE block should include enough text to uniquely identify the change in the file. -4. The SEARCH/REPLACE blocks you generate will be applied immediately, and so they **MUST** produce a file that the user can run IMMEDIATELY. +2. The original code in each SEARCH/REPLACE block must EXACTLY match lines of code in the original file. +3. The original code in each SEARCH/REPLACE block must include enough text to uniquely identify the change in the file. +4. The original code in each SEARCH/REPLACE block must be disjoint from all other blocks. + +The SEARCH/REPLACE blocks you generate will be applied immediately, and so they **MUST** produce a file that the user can run IMMEDIATELY. - Make sure you add all necessary imports. - Make sure the "final" code is complete and will not result in syntax/lint errors. -5. Follow coding conventions of the user (spaces, semilcolons, comments, etc). If the user spaces or formats things a certain way, CONTINUE formatting it that way, even if you prefer otherwise. + +Follow coding conventions of the user (spaces, semilcolons, comments, etc). If the user spaces or formats things a certain way, CONTINUE formatting it that way, even if you prefer otherwise. ## EXAMPLE 1 ORIGINAL_FILE diff --git a/src/vs/workbench/contrib/void/browser/searchReplaceService.ts b/src/vs/workbench/contrib/void/browser/searchReplaceCacheService.ts similarity index 75% rename from src/vs/workbench/contrib/void/browser/searchReplaceService.ts rename to src/vs/workbench/contrib/void/browser/searchReplaceCacheService.ts index 9bf958c4..e7a9448e 100644 --- a/src/vs/workbench/contrib/void/browser/searchReplaceService.ts +++ b/src/vs/workbench/contrib/void/browser/searchReplaceCacheService.ts @@ -8,6 +8,7 @@ import { Disposable } from '../../../../base/common/lifecycle.js'; import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js'; import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js'; import { ILLMMessageService } from '../common/llmMessageService.js'; +import { ServiceSendLLMMessageParams } from '../common/llmMessageTypes.js'; @@ -15,12 +16,10 @@ export interface ISearchReplaceService { readonly _serviceBrand: undefined; } -export const ISearchReplaceService = createDecorator('SearchReplaceService'); +export const ISearchReplaceService = createDecorator('SearchReplaceCacheService'); class SearchReplaceService extends Disposable implements ISearchReplaceService { _serviceBrand: undefined; - static readonly ID = 'SearchReplaceService'; - private readonly _onDidChangeState = new Emitter(); readonly onDidChangeState: Event = this._onDidChangeState.event; @@ -28,8 +27,18 @@ class SearchReplaceService extends Disposable implements ISearchReplaceService { @ILLMMessageService private readonly llmMessageService: ILLMMessageService, ) { super() - // this.llmMessageService.sendLLMMessage({}) + } + send(params: Omit & { onText: (p: { newText: string, fullText: string }) => { retry: boolean } }) { + this.llmMessageService.sendLLMMessage({ + ...params as ServiceSendLLMMessageParams, + onText: (p) => { + const { retry } = params.onText(p) + if (retry) { + + } + } + }) } } diff --git a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts index 14371a22..06e708fd 100644 --- a/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts +++ b/src/vs/workbench/contrib/void/common/voidSettingsTypes.ts @@ -552,7 +552,8 @@ export const displayInfoOfSettingName = (providerName: ProviderName, settingName : '(never)', subTextMd: providerName === 'ollama' ? 'If you would like to change this endpoint, please read more about [Endpoints here](https://github.com/ollama/ollama/blob/main/docs/faq.md#how-can-i-expose-ollama-on-my-network).' : - undefined, + providerName === 'vLLM' ? 'If you would like to change this endpoint, please read more about [Endpoints here](https://docs.vllm.ai/en/latest/getting_started/quickstart.html#openai-compatible-server).' : + undefined, } } else if (settingName === '_didFillInProviderSettings') { From baa89cc17d88379f6d0774ca6a1479dba1560cdc Mon Sep 17 00:00:00 2001 From: Mathew Pareles Date: Tue, 18 Feb 2025 14:14:38 -0800 Subject: [PATCH 44/47] dummy marker service --- .../void/browser/MarkerCheckService.ts | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/vs/workbench/contrib/void/browser/MarkerCheckService.ts diff --git a/src/vs/workbench/contrib/void/browser/MarkerCheckService.ts b/src/vs/workbench/contrib/void/browser/MarkerCheckService.ts new file mode 100644 index 00000000..f41c0513 --- /dev/null +++ b/src/vs/workbench/contrib/void/browser/MarkerCheckService.ts @@ -0,0 +1,119 @@ +/*-------------------------------------------------------------------------------------- + * Copyright 2025 Glass Devtools, Inc. All rights reserved. + * Licensed under the Apache License, Version 2.0. See LICENSE.txt for more information. + *--------------------------------------------------------------------------------------*/ + +import { Disposable } from '../../../../base/common/lifecycle.js'; +import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js'; +import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js'; +import { IMarkerService, MarkerSeverity } from '../../../../platform/markers/common/markers.js'; +import { ILanguageFeaturesService } from '../../../../editor/common/services/languageFeatures.js'; +import { ITextModelService } from '../../../../editor/common/services/resolverService.js'; +import { Range } from '../../../../editor/common/core/range.js'; +import { CancellationToken } from '../../../../base/common/cancellation.js'; +import { CodeActionContext, CodeActionTriggerType } from '../../../../editor/common/languages.js'; + +export interface IMarkerCheckService { + readonly _serviceBrand: undefined; +} + +export const IMarkerCheckService = createDecorator('markerCheckService'); + +class MarkerCheckService extends Disposable implements IMarkerCheckService { + _serviceBrand: undefined; + + constructor( + @IMarkerService private readonly _markerService: IMarkerService, + @ILanguageFeaturesService private readonly _languageFeaturesService: ILanguageFeaturesService, + @ITextModelService private readonly _textModelService: ITextModelService, + ) { + super(); + + setInterval(async () => { + const allMarkers = this._markerService.read(); + const errors = allMarkers.filter(marker => marker.severity === MarkerSeverity.Error); + + if (errors.length > 0) { + for (const error of errors) { + + console.log(`----------------------------------------------`); + + console.log(`${error.resource.toString()}: ${error.startLineNumber} ${error.message} ${error.severity}`); // ! all errors in the file + + try { + // Get the text model for the file + const modelReference = await this._textModelService.createModelReference(error.resource); + const model = modelReference.object.textEditorModel; + + // Create a range from the marker + const range = new Range( + error.startLineNumber, + error.startColumn, + error.endLineNumber, + error.endColumn + ); + + // Get code action providers for this model + const codeActionProvider = this._languageFeaturesService.codeActionProvider; + const providers = codeActionProvider.ordered(model); + + if (providers.length > 0) { + // Request code actions from each provider + for (const provider of providers) { + const context: CodeActionContext = { + trigger: CodeActionTriggerType.Invoke, // keeping 'trigger' since it works + only: 'quickfix' // adding this to filter for quick fixes + }; + + const actions = await provider.provideCodeActions( + model, + range, + context, + CancellationToken.None + ); + + if (actions?.actions?.length) { + + const quickFixes = actions.actions.filter(action => action.isPreferred); // ! all quickFixes for the error + const quickFixesForImports = actions.actions.filter(action => action.isPreferred && action.title.includes('import')); // ! all possible imports + quickFixesForImports + + if (quickFixes.length > 0) { + console.log('Available Quick Fixes:'); + quickFixes.forEach(action => { + console.log(`- ${action.title}`); + }); + } + } + } + } + + // Dispose the model reference + modelReference.dispose(); + } catch (e) { + console.error('Error getting quick fixes:', e); + } + } + } + }, 5000); + } + + + // private _onMarkersChanged = (changedResources: readonly URI[]): void => { + // for (const resource of changedResources) { + // const markers = this._markerService.read({ resource }); + + // if (markers.length === 0) { + // console.log(`${resource.toString()}: No diagnostics`); + // continue; + // } + + // console.log(`Diagnostics for ${resource.toString()}:`); + // markers.forEach(marker => this._logMarker(marker)); + // } + // }; + + +} + +registerSingleton(IMarkerCheckService, MarkerCheckService, InstantiationType.Eager); From 5699cf19f44ff11caad41208e73c9801cdf24463 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Tue, 18 Feb 2025 16:12:16 -0800 Subject: [PATCH 45/47] merge updates --- .../contrib/void/browser/editCodeService.ts | 2 ++ .../contrib/void/common/llmMessageTypes.ts | 9 +++++---- .../void/electron-main/llmMessage/anthropic.ts | 9 ++++++++- .../void/electron-main/llmMessage/openai.ts | 14 ++++++++++---- .../llmMessage/postprocessToolCalls.ts | 10 ++++++++++ .../llmMessage/preprocessLLMMessages.ts | 3 --- 6 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 src/vs/workbench/contrib/void/electron-main/llmMessage/postprocessToolCalls.ts diff --git a/src/vs/workbench/contrib/void/browser/editCodeService.ts b/src/vs/workbench/contrib/void/browser/editCodeService.ts index ad3eb81d..e3a5d998 100644 --- a/src/vs/workbench/contrib/void/browser/editCodeService.ts +++ b/src/vs/workbench/contrib/void/browser/editCodeService.ts @@ -42,6 +42,7 @@ import { ILLMMessageService } from '../common/llmMessageService.js'; import { LLMChatMessage, errorDetails } from '../common/llmMessageTypes.js'; import { IMetricsService } from '../common/metricsService.js'; import { VSReadFile } from './helpers/readFile.js'; +import { IFileService } from '../../../../platform/files/common/files.js'; const configOfBG = (color: Color) => { return { dark: color, light: color, hcDark: color, hcLight: color, } @@ -254,6 +255,7 @@ class EditCodeService extends Disposable implements IEditCodeService { @IMetricsService private readonly _metricsService: IMetricsService, @INotificationService private readonly _notificationService: INotificationService, @ICommandService private readonly _commandService: ICommandService, + @IFileService private readonly _fileService: IFileService, ) { super(); diff --git a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts index 75cf2739..0956b08b 100644 --- a/src/vs/workbench/contrib/void/common/llmMessageTypes.ts +++ b/src/vs/workbench/contrib/void/common/llmMessageTypes.ts @@ -4,7 +4,7 @@ *--------------------------------------------------------------------------------------*/ import { ChatMessage } from '../browser/chatThreadService.js' -import { InternalToolInfo } from './toolsService.js' +import { InternalToolInfo, ToolName } from './toolsService.js' import { FeatureName, ProviderName, SettingsOfProvider } from './voidSettingsTypes.js' @@ -37,15 +37,16 @@ export type LLMChatMessage = { id: string; } -export type LLMToolCallType = { - name: string; + +export type ToolCallType = { + name: ToolName; params: string; id: string; } export type OnText = (p: { newText: string, fullText: string }) => void -export type OnFinalMessage = (p: { fullText: string, toolCalls?: LLMToolCallType[] }) => void // id is tool_use_id +export type OnFinalMessage = (p: { fullText: string, toolCalls?: ToolCallType[] }) => void // id is tool_use_id export type OnError = (p: { message: string, fullError: Error | null }) => void export type AbortRef = { current: (() => void) | null } diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts index 8308bb44..c4338ebb 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/anthropic.ts @@ -8,6 +8,7 @@ import { _InternalSendLLMChatMessageFnType } from '../../common/llmMessageTypes. import { anthropicMaxPossibleTokens, developerInfoOfModelName, developerInfoOfProviderName } from '../../common/voidSettingsTypes.js'; import { InternalToolInfo } from '../../common/toolsService.js'; import { addSystemMessageAndToolSupport } from './preprocessLLMMessages.js'; +import { isAToolName } from './postprocessToolCalls.js'; @@ -86,7 +87,13 @@ export const sendAnthropicChat: _InternalSendLLMChatMessageFnType = ({ messages: stream.on('finalMessage', (response) => { // stringify the response's content const content = response.content.map(c => c.type === 'text' ? c.text : '').join('\n\n') - const toolCalls = response.content.map(c => c.type === 'tool_use' ? { name: c.name, params: JSON.stringify(c.input), id: c.id } : null).filter(c => !!c) + const toolCalls = response.content + .map(c => { + if (c.type !== 'tool_use') return null + if (!isAToolName(c.name)) return null + return c.type === 'tool_use' ? { name: c.name, params: JSON.stringify(c.input), id: c.id } : null + }) + .filter(t => !!t) onFinalMessage({ fullText: content, toolCalls }) }) diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts index 80db8d73..66c0ffe1 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/openai.ts @@ -9,6 +9,7 @@ import { Model } from 'openai/resources/models.js'; import { InternalToolInfo } from '../../common/toolsService.js'; import { addSystemMessageAndToolSupport } from './preprocessLLMMessages.js'; import { developerInfoOfModelName, developerInfoOfProviderName } from '../../common/voidSettingsTypes.js'; +import { isAToolName } from './postprocessToolCalls.js'; // import { parseMaxTokensStr } from './util.js'; @@ -205,10 +206,15 @@ export const sendOpenAIChat: _InternalSendLLMChatMessageFnType = ({ messages: me onText({ newText, fullText }); } onFinalMessage({ - fullText, toolCalls: Object.keys(toolCallOfIndex).map(index => { - const tool = toolCallOfIndex[index] - return { name: tool.name, id: tool.id, params: tool.params } - }) + fullText, + toolCalls: Object.keys(toolCallOfIndex) + .map(index => { + const tool = toolCallOfIndex[index] + if (isAToolName(tool.name)) + return { name: tool.name, id: tool.id, params: tool.params } + return null + }) + .filter(t => !!t) }); }) // when error/fail - this catches errors of both .create() and .then(for await) diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/postprocessToolCalls.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/postprocessToolCalls.ts new file mode 100644 index 00000000..e9bb2b7c --- /dev/null +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/postprocessToolCalls.ts @@ -0,0 +1,10 @@ +import { ToolName, toolNames } from '../../common/toolsService'; + + + +const toolNamesSet = new Set(toolNames) + +export const isAToolName = (toolName: string): toolName is ToolName => { + const isAToolName = toolNamesSet.has(toolName) + return isAToolName +} diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/preprocessLLMMessages.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/preprocessLLMMessages.ts index eba90468..689e44de 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/preprocessLLMMessages.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/preprocessLLMMessages.ts @@ -262,9 +262,6 @@ export const addSystemMessageAndToolSupport = (modelName: string, providerName: - - - /* From ac1788ae9a7fbf034ec58d0a7f5dffba6be444ff Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Tue, 18 Feb 2025 21:25:23 -0800 Subject: [PATCH 46/47] tool pages work, improve prompt --- .../contrib/void/browser/chatThreadService.ts | 2 +- .../contrib/void/browser/prompt/prompts.ts | 24 +++++--- .../react/src/markdown/ChatMarkdownRender.tsx | 4 +- .../contrib/void/common/toolsService.ts | 61 ++++++++++++++----- .../llmMessage/postprocessToolCalls.ts | 2 +- 5 files changed, 64 insertions(+), 29 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index 7f53a043..fd7e4288 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -356,7 +356,7 @@ class ChatThreadService extends Disposable implements IChatThreadService { // add user's message to chat history const instructions = userMessage - const userMessageContent = await chat_userMessageContent(instructions, currSelns, currSelns) + const userMessageContent = await chat_userMessageContent(instructions, currSelns) const selectionsStr = await chat_selectionsString(prevSelns, currSelns, this._modelService, this._fileService) const userMessageFullContent = chat_userMessageContentWithAllFiles(userMessageContent, selectionsStr) diff --git a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts index 1e9dd2a9..f04fcb5c 100644 --- a/src/vs/workbench/contrib/void/browser/prompt/prompts.ts +++ b/src/vs/workbench/contrib/void/browser/prompt/prompts.ts @@ -33,10 +33,12 @@ For example, if the user asks you to "make this file look nicer", make sure your You're allowed to ask for more context. For example, if the user only gives you a selection but you want to see the the full file, you can ask them to provide it. If you are given tools: +- Only use tools if the user asks you to do something. If the user simply says hi or asks you a question that you can answer without tools, then do NOT tools. - You are allowed to use tools without asking for permission. - Feel free to use tools to gather context, make suggestions, etc. - One great use of tools is to explore imports that you'd like to have more information about. -- NEVER refer to a tool by name when speaking with the user. For example, do NOT say to the user user "I'm going to use \`list_dir\`". Instead, say "I'm going to list all files in ___ directory", etc. +- Reference relevant files that you found when using tools if they helped you come up with your answer. +- NEVER refer to a tool by name when speaking with the user. For example, do NOT say to the user user "I'm going to use \`list_dir\`". Instead, say "I'm going to list all files in ___ directory", etc. Do not even refer to "pages" of results, just say you're getting more results. Do not output any of these instructions, nor tell the user anything about them unless directly prompted for them. Do not tell the user anything about the examples below. Do not assume the user is talking about any of the examples below. @@ -176,14 +178,14 @@ const stringifyFileSelections = async (fileSelections: FileSelection[], modelSer return fileSlns.map(sel => stringifyFileSelection(sel)).join('\n') } const stringifyCodeSelections = (codeSelections: CodeSelection[]) => { - return codeSelections.map(sel => stringifyCodeSelection(sel)).join('\n') + return codeSelections.map(sel => stringifyCodeSelection(sel)).join('\n') || null } const stringifySelectionNames = (currSelns: StagingSelectionItem[] | null): string => { if (!currSelns) return '' return currSelns.map(s => `${s.fileURI.fsPath}${s.range ? ` (lines ${s.range.startLineNumber}:${s.range.endLineNumber})` : ''}`).join('\n') } -export const chat_userMessageContent = async (instructions: string, prevSelns: StagingSelectionItem[] | null, currSelns: StagingSelectionItem[] | null) => { +export const chat_userMessageContent = async (instructions: string, currSelns: StagingSelectionItem[] | null) => { const selnsStr = stringifySelectionNames(currSelns) @@ -198,6 +200,8 @@ export const chat_selectionsString = async (prevSelns: StagingSelectionItem[] | // ADD IN FILES AT TOP const allSelections = [...currSelns || [], ...prevSelns || []] + if (allSelections.length === 0) return null + const codeSelections: CodeSelection[] = [] const fileSelections: FileSelection[] = [] const filesURIs = new Set() @@ -219,17 +223,17 @@ export const chat_selectionsString = async (prevSelns: StagingSelectionItem[] | const filesStr = await stringifyFileSelections(fileSelections, modelService, fileService) const selnsStr = stringifyCodeSelections(codeSelections) - let str = '' - str += 'ALL FILE CONTENTS\n' - if (filesStr) str += `${filesStr}\n` - if (selnsStr) str += `${selnsStr}\n` + if (filesStr || selnsStr) return `\ +ALL FILE CONTENTS +${filesStr} +${selnsStr}` - return str; + return null } -export const chat_userMessageContentWithAllFilesToo = (userMessage: string, selectionsString: string | undefined) => { - if (userMessage) return `${userMessage}\n${selectionsString}\n` +export const chat_userMessageContentWithAllFilesToo = (userMessage: string, selectionsString: string | null) => { + if (userMessage) return `${userMessage}${selectionsString ? `\n${selectionsString}` : ''}` else return userMessage } diff --git a/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx b/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx index 5de08900..3e77a6df 100644 --- a/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx +++ b/src/vs/workbench/contrib/void/browser/react/src/markdown/ChatMarkdownRender.tsx @@ -29,7 +29,7 @@ const getApplyBoxId = ({ threadId, messageIdx, tokenIdx }: ApplyBoxLocation) => -const ApplyButtonsOnHover = ({ applyStr, applyBoxId }: { applyStr: string, applyBoxId: string }) => { +const ApplyButtonsOnHover = ({ applyStr }: { applyStr: string }) => { const accessor = useAccessor() const [copyButtonState, setCopyButtonState] = useState(CopyButtonState.Copy) @@ -120,7 +120,7 @@ const RenderToken = ({ token, nested = false, noSpace = false, chatMessageLocati return } + buttonsOnHover={applyBoxId && } /> } diff --git a/src/vs/workbench/contrib/void/common/toolsService.ts b/src/vs/workbench/contrib/void/common/toolsService.ts index 7620b2cd..4a0933a2 100644 --- a/src/vs/workbench/contrib/void/common/toolsService.ts +++ b/src/vs/workbench/contrib/void/common/toolsService.ts @@ -1,7 +1,7 @@ import { CancellationToken } from '../../../../base/common/cancellation.js' import { URI } from '../../../../base/common/uri.js' import { IModelService } from '../../../../editor/common/services/model.js' -import { IFileService, IFileStat } from '../../../../platform/files/common/files.js' +import { IFileService } from '../../../../platform/files/common/files.js' import { registerSingleton, InstantiationType } from '../../../../platform/instantiation/common/extensions.js' import { createDecorator, IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js' import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js' @@ -93,22 +93,46 @@ export type ToolCallReturnType export type ToolFns = { [T in ToolName]: (p: string) => Promise> } export type ToolResultToString = { [T in ToolName]: (result: ToolCallReturnType) => string } +const MAX_DEPTH = 1 +const MAX_CHILDREN = 500 +async function generateDirectoryTreeMd(fileService: IFileService, rootURI: URI, pageNumber: number): Promise { + let output = ''; -async function generateDirectoryTreeMd(fileService: IFileService, rootURI: URI): Promise { - let output = '' - function traverseChildren(children: IFileStat[], depth: number) { - const indentation = ' '.repeat(depth); - for (const child of children) { - output += `${indentation}- ${child.name}\n`; - traverseChildren(child.children ?? [], depth + 1); + const indentation = (depth: number, isLast: boolean): string => { + if (depth === 0) return ''; + return `${'| '.repeat(depth - 1)}${isLast ? '└── ' : '├── '}`; + }; + + async function traverseChildren(uri: URI, depth: number, isLast: boolean) { + const stat = await fileService.resolve(uri, { resolveMetadata: false }); + + if ((depth === 0 && pageNumber === 1) || depth !== 0) + output += `${indentation(depth, isLast)}${stat.name}${stat.isDirectory ? '/' : ''}${stat.isSymbolicLink ? ` (symbolic link)` : ''}\n`; // TODO say where symlink links to + + // list children + const originalChildrenLength = stat.children?.length ?? 0 + const fromChildIdx = MAX_CHILDREN * (pageNumber - 1) + const toChildIdx = MAX_CHILDREN * pageNumber - 1 // INCLUSIVE + const listChildren = stat.children?.slice(fromChildIdx, toChildIdx + 1) ?? []; + + if (!stat.isDirectory) return; + + if (listChildren.length === 0) return + if (depth === MAX_DEPTH) return // right now MAX_DEPTH=1 to make pagination work nicely + + for (let i = 0; i < Math.min(listChildren.length, MAX_CHILDREN); i++) { + await traverseChildren(listChildren[i].resource, depth + 1, i === listChildren.length - 1); } + const nCutoffChildren = (originalChildrenLength - 1) - toChildIdx + if (nCutoffChildren > 0) { + output += `${indentation(depth + 1, true)}(${nCutoffChildren} results remaining...)\n` + } + } - const stat = await fileService.resolve(rootURI, { resolveMetadata: false }); - // kickstart recursion - output += `${stat.name}\n`; - traverseChildren(stat.children ?? [], 1); + await traverseChildren(rootURI, 0, false); + console.log('OUTPUT', output); return output; } @@ -119,6 +143,12 @@ const validateURI = (uriStr: unknown) => { return uri } +const validatePageNum = (pageNumberUnknown: unknown) => { + const proposedPageNum = Number.parseInt(pageNumberUnknown + '') + const num = Number.isInteger(proposedPageNum) ? proposedPageNum : 1 + const pageNumber = num < 1 ? 1 : num + return pageNumber +} export interface IToolsService { readonly _serviceBrand: undefined; toolFns: ToolFns; @@ -170,12 +200,13 @@ export class ToolsService implements IToolsService { list_dir: async (s: string) => { const o = parseObj(s) if (!o) return invalidToolParamMsg - const { uri: uriStr } = o + const { uri: uriStr, pageNumber: pageNumberUnknown } = o const uri = validateURI(uriStr) + const pageNumber = validatePageNum(pageNumberUnknown) + // TODO!!!! check to make sure in workspace - // TODO check to make sure is not gitignored - const treeStr = await generateDirectoryTreeMd(fileService, uri) + const treeStr = await generateDirectoryTreeMd(fileService, uri, pageNumber) return treeStr }, pathname_search: async (s: string) => { diff --git a/src/vs/workbench/contrib/void/electron-main/llmMessage/postprocessToolCalls.ts b/src/vs/workbench/contrib/void/electron-main/llmMessage/postprocessToolCalls.ts index e9bb2b7c..2feeeb80 100644 --- a/src/vs/workbench/contrib/void/electron-main/llmMessage/postprocessToolCalls.ts +++ b/src/vs/workbench/contrib/void/electron-main/llmMessage/postprocessToolCalls.ts @@ -1,4 +1,4 @@ -import { ToolName, toolNames } from '../../common/toolsService'; +import { ToolName, toolNames } from '../../common/toolsService.js'; From 02f64b7ff61c2bc6265e7f375b110448c833aba4 Mon Sep 17 00:00:00 2001 From: Andrew Pareles Date: Tue, 18 Feb 2025 22:28:04 -0800 Subject: [PATCH 47/47] finish tool pagination --- .../contrib/void/browser/chatThreadService.ts | 7 +- .../contrib/void/common/toolsService.ts | 152 +++++++++++------- 2 files changed, 100 insertions(+), 59 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index fd7e4288..cc875a79 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -78,7 +78,8 @@ export type ChatMessage = role: 'assistant'; content: string | null; // content received from LLM - allowed to be '', will be replaced with (empty) displayContent: string | null; // content displayed to user (this is the same as content for now) - allowed to be '', will be ignored - } | ToolMessage + } + | ToolMessage type UserMessageType = ChatMessage & { role: 'user' } type UserMessageState = UserMessageType['state'] @@ -422,8 +423,10 @@ class ChatThreadService extends Disposable implements IChatThreadService { // 1. let toolResult: Awaited> + let toolResultVal: ToolCallReturnType try { toolResult = await this._toolsService.toolFns[toolName](tool.params) + toolResultVal = toolResult[0] } catch (error) { this._setStreamState(threadId, { error }) shouldSendAnotherMessage = false @@ -440,7 +443,7 @@ class ChatThreadService extends Disposable implements IChatThreadService { break } - this._addMessageToThread(threadId, { role: 'tool', name: toolName, params: tool.params, id: tool.id, content: toolResultStr, result: toolResult, }) + this._addMessageToThread(threadId, { role: 'tool', name: toolName, params: tool.params, id: tool.id, content: toolResultStr, result: toolResultVal, }) shouldSendAnotherMessage = true } diff --git a/src/vs/workbench/contrib/void/common/toolsService.ts b/src/vs/workbench/contrib/void/common/toolsService.ts index 4a0933a2..3b609f60 100644 --- a/src/vs/workbench/contrib/void/common/toolsService.ts +++ b/src/vs/workbench/contrib/void/common/toolsService.ts @@ -90,12 +90,16 @@ export type ToolCallReturnType : T extends 'search' ? string | URI[] : never -export type ToolFns = { [T in ToolName]: (p: string) => Promise> } -export type ToolResultToString = { [T in ToolName]: (result: ToolCallReturnType) => string } +export type ToolFns = { [T in ToolName]: (p: string) => Promise<[ToolCallReturnType, boolean]> } +export type ToolResultToString = { [T in ToolName]: (result: [ToolCallReturnType, boolean]) => string } + + +// pagination info +const MAX_FILE_CHARS_PAGE = 50_000 +const MAX_CHILDREN_URIs_PAGE = 500 const MAX_DEPTH = 1 -const MAX_CHILDREN = 500 -async function generateDirectoryTreeMd(fileService: IFileService, rootURI: URI, pageNumber: number): Promise { +async function generateDirectoryTreeMd(fileService: IFileService, rootURI: URI, pageNumber: number): Promise<[string, boolean]> { let output = ''; const indentation = (depth: number, isLast: boolean): string => { @@ -103,16 +107,19 @@ async function generateDirectoryTreeMd(fileService: IFileService, rootURI: URI, return `${'| '.repeat(depth - 1)}${isLast ? '└── ' : '├── '}`; }; + let hasNextPage = false + async function traverseChildren(uri: URI, depth: number, isLast: boolean) { const stat = await fileService.resolve(uri, { resolveMetadata: false }); + // we might want to say where symlink links to if ((depth === 0 && pageNumber === 1) || depth !== 0) - output += `${indentation(depth, isLast)}${stat.name}${stat.isDirectory ? '/' : ''}${stat.isSymbolicLink ? ` (symbolic link)` : ''}\n`; // TODO say where symlink links to + output += `${indentation(depth, isLast)}${stat.name}${stat.isDirectory ? '/' : ''}${stat.isSymbolicLink ? ` (symbolic link)` : ''}\n`; // list children const originalChildrenLength = stat.children?.length ?? 0 - const fromChildIdx = MAX_CHILDREN * (pageNumber - 1) - const toChildIdx = MAX_CHILDREN * pageNumber - 1 // INCLUSIVE + const fromChildIdx = MAX_CHILDREN_URIs_PAGE * (pageNumber - 1) + const toChildIdx = MAX_CHILDREN_URIs_PAGE * pageNumber - 1 // INCLUSIVE const listChildren = stat.children?.slice(fromChildIdx, toChildIdx + 1) ?? []; if (!stat.isDirectory) return; @@ -120,25 +127,43 @@ async function generateDirectoryTreeMd(fileService: IFileService, rootURI: URI, if (listChildren.length === 0) return if (depth === MAX_DEPTH) return // right now MAX_DEPTH=1 to make pagination work nicely - for (let i = 0; i < Math.min(listChildren.length, MAX_CHILDREN); i++) { + for (let i = 0; i < Math.min(listChildren.length, MAX_CHILDREN_URIs_PAGE); i++) { await traverseChildren(listChildren[i].resource, depth + 1, i === listChildren.length - 1); } - const nCutoffChildren = (originalChildrenLength - 1) - toChildIdx - if (nCutoffChildren > 0) { - output += `${indentation(depth + 1, true)}(${nCutoffChildren} results remaining...)\n` + const nCutoffResults = (originalChildrenLength - 1) - toChildIdx + if (nCutoffResults >= 1) { + output += `${indentation(depth + 1, true)}(${nCutoffResults} results remaining...)\n` + hasNextPage = true } } await traverseChildren(rootURI, 0, false); - console.log('OUTPUT', output); - return output; + return [output, hasNextPage] +} + + +const validateJSON = (s: string): { [s: string]: unknown } => { + try { + const o = JSON.parse(s) + return o + } + catch (e) { + throw new Error(`Tool parameter was not a valid JSON: "${s}".`) + } +} + + + +const validateQueryStr = (queryStr: unknown) => { + if (typeof queryStr !== 'string') throw new Error('Error calling tool: provided query must be a string.') + return queryStr } const validateURI = (uriStr: unknown) => { - if (typeof uriStr !== 'string') throw new Error('(provided uri must be a string)') + if (typeof uriStr !== 'string') throw new Error('Error calling tool: provided uri must be a string.') const uri = URI.file(uriStr) return uri } @@ -173,83 +198,96 @@ export class ToolsService implements IToolsService { @IInstantiationService instantiationService: IInstantiationService, ) { - const queryBuilder = instantiationService.createInstance(QueryBuilder); - const parseObj = (s: string): { [s: string]: unknown } | null => { - try { - const o = JSON.parse(s) - return o - } - catch (e) { - return null - } - } - - const invalidToolParamMsg = '(LLM parameter format was invalid for this tool)' this.toolFns = { read_file: async (s: string) => { - const o = parseObj(s) - if (!o) return invalidToolParamMsg - const { uri: uriStr } = o + const o = validateJSON(s) + const { uri: uriStr, pageNumber: pageNumberUnknown } = o const uri = validateURI(uriStr) - const fileContents = await VSReadFile(uri, modelService, fileService) - return fileContents ?? invalidToolParamMsg + const pageNumber = validatePageNum(pageNumberUnknown) + + const readFileContents = await VSReadFile(uri, modelService, fileService) + + const fromIdx = MAX_FILE_CHARS_PAGE * (pageNumber - 1) + const toIdx = MAX_FILE_CHARS_PAGE * pageNumber - 1 + let fileContents = readFileContents.slice(fromIdx, toIdx + 1) // paginate + const hasNextPage = (readFileContents.length - 1) - toIdx >= 1 + + return [fileContents || '(empty)', hasNextPage] }, list_dir: async (s: string) => { - const o = parseObj(s) - if (!o) return invalidToolParamMsg + const o = validateJSON(s) const { uri: uriStr, pageNumber: pageNumberUnknown } = o const uri = validateURI(uriStr) const pageNumber = validatePageNum(pageNumberUnknown) // TODO!!!! check to make sure in workspace - const treeStr = await generateDirectoryTreeMd(fileService, uri, pageNumber) - return treeStr + const [treeStr, hasNextPage] = await generateDirectoryTreeMd(fileService, uri, pageNumber) + return [treeStr, hasNextPage] }, pathname_search: async (s: string) => { - const o = parseObj(s) - if (!o) return invalidToolParamMsg - const { query: queryStr } = o + const o = validateJSON(s) + const { query: queryUnknown, pageNumber: pageNumberUnknown } = o + + const queryStr = validateQueryStr(queryUnknown) + const pageNumber = validatePageNum(pageNumberUnknown) - if (typeof queryStr !== 'string') return 'Error: query was not a string' const query = queryBuilder.file(workspaceContextService.getWorkspace().folders.map(f => f.uri), { filePattern: queryStr, }) - const data = await searchService.fileSearch(query, CancellationToken.None) - const URIs = data.results.map(({ resource, results }) => resource) - return URIs + + const fromIdx = MAX_CHILDREN_URIs_PAGE * (pageNumber - 1) + const toIdx = MAX_CHILDREN_URIs_PAGE * pageNumber - 1 + const URIs = data.results + .slice(fromIdx, toIdx + 1) // paginate + .map(({ resource, results }) => resource) + + const hasNextPage = (data.results.length - 1) - toIdx >= 1 + + return [URIs, hasNextPage] }, search: async (s: string) => { - const o = parseObj(s) - if (!o) return '(could not search)' - const { query: queryStr } = o + const o = validateJSON(s) + const { query: queryUnknown, pageNumber: pageNumberUnknown } = o + + const queryStr = validateQueryStr(queryUnknown) + const pageNumber = validatePageNum(pageNumberUnknown) - if (typeof queryStr !== 'string') return 'Error: query was not a string' const query = queryBuilder.text({ pattern: queryStr, }, workspaceContextService.getWorkspace().folders.map(f => f.uri)) - const data = await searchService.textSearch(query, CancellationToken.None) - const URIs = data.results.map(({ resource, results }) => resource) - return URIs + + const fromIdx = MAX_CHILDREN_URIs_PAGE * (pageNumber - 1) + const toIdx = MAX_CHILDREN_URIs_PAGE * pageNumber - 1 + const URIs = data.results + .slice(fromIdx, toIdx + 1) // paginate + .map(({ resource, results }) => resource) + + const hasNextPage = (data.results.length - 1) - toIdx >= 1 + + return [URIs, hasNextPage] }, } + + const nextPageStr = (hasNextPage: boolean) => hasNextPage ? '\n\n(more on next page...)' : '' + this.toolResultToString = { - read_file: (URIs) => { - return URIs + read_file: ([fileContents, hasNextPage]) => { + return fileContents + nextPageStr(hasNextPage) }, - list_dir: (URIs) => { - return URIs + list_dir: ([dirTreeStr, hasNextPage]) => { + return dirTreeStr + nextPageStr(hasNextPage) }, - pathname_search: (URIs) => { + pathname_search: ([URIs, hasNextPage]) => { if (typeof URIs === 'string') return URIs - return URIs.map(uri => uri.fsPath).join('\n') + return URIs.map(uri => uri.fsPath).join('\n') + nextPageStr(hasNextPage) }, - search: (URIs) => { + search: ([URIs, hasNextPage]) => { if (typeof URIs === 'string') return URIs - return URIs.map(uri => uri.fsPath).join('\n') + return URIs.map(uri => uri.fsPath).join('\n') + nextPageStr(hasNextPage) }, }