Skip to content

Commit

Permalink
updates, tiptap, embedding options
Browse files Browse the repository at this point in the history
lint

lint

lint

lint

lint
  • Loading branch information
rjmacarthy committed Aug 10, 2024
1 parent d4f0825 commit dd22854
Show file tree
Hide file tree
Showing 15 changed files with 1,162 additions and 146 deletions.
679 changes: 677 additions & 2 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,12 @@
},
"dependencies": {
"@lancedb/lancedb": "^0.5.2",
"@tiptap/extension-mention": "^2.5.9",
"@tiptap/extension-placeholder": "^2.5.9",
"@tiptap/pm": "^2.5.9",
"@tiptap/react": "^2.5.9",
"@tiptap/starter-kit": "^2.5.9",
"@tiptap/suggestion": "^2.5.9",
"@types/react": "^18.2.46",
"@vscode/codicons": "^0.0.35",
"@vscode/vsce": "^2.31.1",
Expand All @@ -454,22 +460,23 @@
"async-lock": "^1.4.1",
"b4a": "^1.6.6",
"classnames": "^2.5.1",
"handlebars-loader": "^1.7.3",
"handlebars": "^4.7.8",
"handlebars-loader": "^1.7.3",
"hypercore-crypto": "^3.4.2",
"hyperswarm": "^4.7.15",
"minimatch": "^9.0.4",
"node-polyfill-webpack-plugin": "^3.0.0",
"onnxruntime-web": "^1.18.0",
"pear-stdio": "^1.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-markdown": "^9.0.1",
"react-syntax-highlighter": "^15.5.0",
"react": "^18.2.0",
"rehype-raw": "^7.0.0",
"remark-gfm": "^4.0.0",
"stream-http": "^3.2.0",
"string_score": "^0.1.22",
"tippy.js": "^6.3.7",
"toxe": "^1.1.0",
"uuid": "^9.0.1"
},
Expand Down
12 changes: 10 additions & 2 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ export const MIN_COMPLETION_CHUNKS = 2
export const MAX_EMPTY_COMPLETION_CHARS = 250
export const DEFAULT_RERANK_THRESHOLD = 0.5

export const defaultChunkOptions = {
maxSize: 500,
minSize: 50,
overlap: 50
}

export const EVENT_NAME = {
twinngAddMessage: 'twinny-add-message',
twinnyAcceptSolution: 'twinny-accept-solution',
Expand Down Expand Up @@ -118,8 +124,8 @@ export const PROVIDER_EVENT_NAME = {
updateProvider: 'twinny.update-provider'
}

export const ACTIVE_CHAT_PROVIDER_STORAGE_KEY = 'twinny.active-chat-provider'
export const ACTIVE_CONVERSATION_STORAGE_KEY = 'twinny.active-conversation'
export const ACTIVE_CHAT_PROVIDER_STORAGE_KEY = 'twinny.active-chat-provider'
export const ACTIVE_EMBEDDINGS_PROVIDER_STORAGE_KEY =
'twinny.active-embeddings-provider'
export const ACTIVE_FIM_PROVIDER_STORAGE_KEY = 'twinny.active-fim-provider'
Expand All @@ -145,11 +151,13 @@ export const EXTENSION_SETTING_KEY = {

export const EXTENSION_CONTEXT_NAME = {
twinnyConversationHistory: 'twinnyConversationHistory',
twinnyEnableRag: 'twinnyEnableRag',
twinnyGeneratingText: 'twinnyGeneratingText',
twinnyManageProviders: 'twinnyManageProviders',
twinnyManageTemplates: 'twinnyManageTemplates',
twinnyRerankThreshold: 'twinnyRerankThreshold',
twinnyMaxChunkSize: 'twinnyMaxChunkSize',
twinnyMinChunkSize: 'twinnyMinChunkSize',
twinnyOverlapSize: 'twinnyOverlapSize',
twinnySymmetryTab: 'twinnySymmetryTab'
}

Expand Down
8 changes: 4 additions & 4 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const apiProviders = {
LMStudio: 'lmstudio',
Ollama: 'ollama',
Oobabooga: 'oobabooga',
OpenWebUI: 'openwebui',
OpenWebUI: 'openwebui'
} as const

export interface ApiModel {
Expand Down Expand Up @@ -247,9 +247,9 @@ export interface InferenceRequest {
}

export interface ChunkOptions {
minSize?: number
maxSize?: number
overlap?: number
minSize: number
maxSize: number
overlap: number
}

export type Embedding = {
Expand Down
1 change: 1 addition & 0 deletions src/extension.global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare module 'hyperswarm'
declare module 'b4a'
declare module '@tiptap/extension-placeholder'

declare module 'hypercore-crypto' {
const hyperCoreCrypto: {
Expand Down
6 changes: 3 additions & 3 deletions src/extension/chat-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,14 @@ export class ChatService {
public async getRagContext(
text?: string,
): Promise<string | null> {
const ragContextKey = `${EVENT_NAME.twinnyWorkspaceContext}-${EXTENSION_CONTEXT_NAME.twinnyEnableRag}`
const isRagEnabled = this._context?.workspaceState.get(ragContextKey)

const symmetryConnected = this._sessionManager?.get(
EXTENSION_SESSION_NAME.twinnySymmetryConnection
)

if (!isRagEnabled || symmetryConnected)
const workspaceMentioned = text?.includes('@workspace')

if (symmetryConnected || !workspaceMentioned)
return null

updateLoadingMessage(this._view, 'Exploring knowledge base')
Expand Down
3 changes: 2 additions & 1 deletion src/extension/embeddings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ export class EmbeddingDatabase {
cancellable: true,
},
async (progress) => {
if (!this._extensionContext) return
const promises = filePaths.map(async (filePath) => {
const content = await fs.promises.readFile(filePath, 'utf-8')
const chunks = await getDocumentSplitChunks(content, filePath)
const chunks = await getDocumentSplitChunks(content, filePath, this._extensionContext)
const filePathEmbedding = await this.fetchModelEmbedding(filePath)

this._filePaths.push({
Expand Down
84 changes: 42 additions & 42 deletions src/extension/providers/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import {
TWINNY_COMMAND_NAME,
SYMMETRY_DATA_MESSAGE,
SYMMETRY_EMITTER_KEY,
SYSTEM,
SYSTEM
} from '../../common/constants'
import { ChatService } from '../chat-service'
import {
ClientMessage,
Message,
ApiModel,
ServerMessage,
InferenceRequest
InferenceRequest,
} from '../../common/types'
import { TemplateProvider } from '../template-provider'
import { OllamaService } from '../ollama-service'
Expand Down Expand Up @@ -85,7 +85,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
webviewView,
this._db,
this._sessionManager,
this.symmetryService,
this.symmetryService
)

this.conversationHistory = new ConversationHistory(
Expand Down Expand Up @@ -127,7 +127,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
})

webviewView.webview.onDidReceiveMessage(
(message: ClientMessage<string | boolean> & ClientMessage<Message[]>) => {
(message) => {
const eventHandlers = {
[EVENT_NAME.twinnyAcceptSolution]: this.acceptSolution,
[EVENT_NAME.twinnyChatMessage]: this.streamChatCompletion,
Expand All @@ -142,8 +142,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
[EVENT_NAME.twinnySendLanguage]: this.getCurrentLanguage,
[EVENT_NAME.twinnySendTheme]: this.getTheme,
[EVENT_NAME.twinnySetGlobalContext]: this.setGlobalContext,
[EVENT_NAME.twinnySetWorkspaceContext]:
this.setTwinnyWorkspaceContext,
[EVENT_NAME.twinnySetWorkspaceContext]: this.setWorkspaceContext,
[EVENT_NAME.twinnyTextSelection]: this.getSelectedText,
[EVENT_NAME.twinnyWorkspaceContext]: this.getTwinnyWorkspaceContext,
[EVENT_NAME.twinnySetConfigValue]: this.setConfigurationValue,
Expand All @@ -153,7 +152,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
[EVENT_NAME.twinnyEmbedDocuments]: this.embedDocuments,
[EVENT_NAME.twinnyConnectSymmetry]: this.connectToSymmetry,
[EVENT_NAME.twinnyDisconnectSymmetry]: this.disconnectSymmetry,
[EVENT_NAME.twinnySessionContext]: this.getSessionContext
[EVENT_NAME.twinnySessionContext]: this.getSessionContext,
}
eventHandlers[message.type as string]?.(message)
}
Expand Down Expand Up @@ -185,22 +184,22 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
}
}

public getConfigurationValue = (data: ClientMessage) => {
if (!data.key) return
public getConfigurationValue = (message: ClientMessage) => {
if (!message.key) return
const config = vscode.workspace.getConfiguration('twinny')
this.view?.webview.postMessage({
type: EVENT_NAME.twinnyGetConfigValue,
value: {
data: config.get(data.key as string),
type: data.key
data: config.get(message.key as string),
type: message.key
}
} as ServerMessage<string>)
}

public setConfigurationValue = (data: ClientMessage) => {
if (!data.key) return
public setConfigurationValue = (message: ClientMessage) => {
if (!message.key) return
const config = vscode.workspace.getConfiguration('twinny')
config.update(data.key, data.data, vscode.ConfigurationTarget.Global)
config.update(message.key, message.data, vscode.ConfigurationTarget.Global)
}

public fetchOllamaModels = async () => {
Expand Down Expand Up @@ -230,14 +229,14 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
} as ServerMessage<string[]>)
}

public sendNotification = (data: ClientMessage) => {
vscode.window.showInformationMessage(data.data as string)
public sendNotification = (message: ClientMessage) => {
vscode.window.showInformationMessage(message.data as string)
}

public clickSuggestion = (data: ClientMessage) => {
public clickSuggestion = (message: ClientMessage) => {
vscode.commands.executeCommand(
'twinny.templateCompletion',
data.data as string
message.data as string
)
}

Expand Down Expand Up @@ -270,6 +269,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
)
)
}

this.chatService?.streamChatCompletion(data.data || [])
}

Expand Down Expand Up @@ -307,24 +307,34 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
})
}

public acceptSolution = (data: ClientMessage) => {
public acceptSolution = (message: ClientMessage) => {
const editor = vscode.window.activeTextEditor
const selection = editor?.selection
if (!selection) return
vscode.window.activeTextEditor?.edit((editBuilder) => {
editBuilder.replace(selection, data.data as string)
editBuilder.replace(selection, message.data as string)
})
}

public createNewUntitledDocument = async (data: ClientMessage) => {
public createNewUntitledDocument = async (message: ClientMessage) => {
const lang = getLanguage()
const document = await vscode.workspace.openTextDocument({
content: data.data as string,
content: message.data as string,
language: lang.languageId
})
await vscode.window.showTextDocument(document)
}

public getGlobalContext = (message: ClientMessage) => {
const storedData = this._context?.globalState.get(
`${EVENT_NAME.twinnyGlobalContext}-${message.key}`
)
this.view?.webview.postMessage({
type: `${EVENT_NAME.twinnyGlobalContext}-${message.key}`,
value: storedData
})
}

public getTheme = () => {
this.view?.webview.postMessage({
type: EVENT_NAME.twinnySendTheme,
Expand Down Expand Up @@ -370,41 +380,31 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
})
}

public getGlobalContext = (data: ClientMessage) => {
const storedData = this._context?.globalState.get(
`${EVENT_NAME.twinnyGlobalContext}-${data.key}`
)
this.view?.webview.postMessage({
type: `${EVENT_NAME.twinnyGlobalContext}-${data.key}`,
value: storedData
})
}

public setGlobalContext = (data: ClientMessage) => {
public setGlobalContext = (message: ClientMessage) => {
this._context?.globalState.update(
`${EVENT_NAME.twinnyGlobalContext}-${data.key}`,
data.data
`${EVENT_NAME.twinnyGlobalContext}-${message.key}`,
message.data
)
}

public getTwinnyWorkspaceContext = (data: ClientMessage) => {
public getTwinnyWorkspaceContext = (message: ClientMessage) => {
const storedData = this._context?.workspaceState.get(
`${EVENT_NAME.twinnyWorkspaceContext}-${data.key}`
`${EVENT_NAME.twinnyWorkspaceContext}-${message.key}`
)
this.view?.webview.postMessage({
type: `${EVENT_NAME.twinnyWorkspaceContext}-${data.key}`,
type: `${EVENT_NAME.twinnyWorkspaceContext}-${message.key}`,
value: storedData
} as ServerMessage)
}

public setTwinnyWorkspaceContext = <T>(data: ClientMessage<T>) => {
const value = data.data
public setWorkspaceContext = <T>(message: ClientMessage<T>) => {
const value = message.data
this._context.workspaceState.update(
`${EVENT_NAME.twinnyWorkspaceContext}-${data.key}`,
`${EVENT_NAME.twinnyWorkspaceContext}-${message.key}`,
value
)
this.view?.webview.postMessage({
type: `${EVENT_NAME.twinnyWorkspaceContext}-${data.key}`,
type: `${EVENT_NAME.twinnyWorkspaceContext}-${message.key}`,
value
})
}
Expand Down
Loading

0 comments on commit dd22854

Please sign in to comment.