Skip to content

Commit

Permalink
repository level completions fixes #327
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmacarthy committed Sep 26, 2024
1 parent c62f678 commit 9ee4eb2
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export const WASM_LANGUAGES: { [key: string]: string } = {
}

// TODO: We could have an extendable regex for this
export const EMBEDDING_IGNORE_LIST = [
export const FILE_IGNORE_LIST = [
'__mocks__',
'__tests__',
'.babelrc.js',
Expand Down
13 changes: 12 additions & 1 deletion src/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InlineCompletionItem, InlineCompletionList } from 'vscode'
import { InlineCompletionItem, InlineCompletionList, Uri } from 'vscode'
import { CodeLanguageDetails } from './languages'
import { ALL_BRACKETS } from './constants'
import { serverMessageKeys } from 'symmetry-core'
Expand Down Expand Up @@ -28,6 +28,15 @@ export interface PrefixSuffix {
suffix: string
}


export interface RepositoryLevelData {
uri: Uri;
text: string;
name: string;
isOpen: boolean;
relevanceScore: number;
}

export interface StreamResponse {
model: string
created_at: string
Expand Down Expand Up @@ -209,6 +218,8 @@ export interface InteractionItem {
name: string | null | undefined
sessionLength: number
visits: number | null | undefined
isOpen?: boolean
relevanceScore: number | null | undefined
activeLines: {
line: number
character: number
Expand Down
2 changes: 1 addition & 1 deletion src/extension.global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare module 'hypercore-crypto' {
verify: (challenge: Buffer, signature: Buffer, publicKey: Buffer) => boolean
}

export = hyperCoreCrypto
export default hyperCoreCrypto
}

declare module '*.css'
Expand Down
86 changes: 50 additions & 36 deletions src/extension/file-interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { InteractionItem } from '../common/types'
import { LRUCache } from './cache'

export class FileInteractionCache {
private _interactions = new LRUCache<InteractionItem>(20)
private _currentFile: string | null = null
private _sessionStartTime: Date | null = null
private _sessionPauseTime: Date | null = null
private _inactivityTimeout: ReturnType<typeof setTimeout> | null = null
private _interactions = new LRUCache<InteractionItem>(20)
private _sessionPauseTime: Date | null = null
private _sessionStartTime: Date | null = null
private readonly _inactivityThreshold = 5 * 60 * 1000 // 5 minutes
private static readonly KEY_STROKE_WEIGHT = 2
private static readonly OPEN_FILE_WEIGHT = 10
private static readonly RECENCY_WEIGHT = 2.1
private static readonly SESSION_LENGTH_WEIGHT = 1
private static readonly VISIT_WEIGHT = 0.5

constructor() {
this.resetInactivityTimeout()
Expand Down Expand Up @@ -41,36 +46,34 @@ export class FileInteractionCache {
this.resetInactivityTimeout()
}

getAll() {
const recencyWeight = 2.1
const keyStrokeWeight = 2
const sessionLengthWeight = 1
const visitWeight = 0.5
private calculateRelevanceScore(interaction: InteractionItem | null): number {
if (!interaction) return 0

const recency = Date.now() - (interaction.lastVisited || 0)
const score =
(interaction.keyStrokes || 0) * FileInteractionCache.KEY_STROKE_WEIGHT +
(interaction.visits || 0) * FileInteractionCache.VISIT_WEIGHT +
(interaction.sessionLength || 0) *
FileInteractionCache.SESSION_LENGTH_WEIGHT -
recency * FileInteractionCache.RECENCY_WEIGHT

return (
score + (interaction.isOpen ? FileInteractionCache.OPEN_FILE_WEIGHT : 0)
)
}

getAll(): InteractionItem[] {
return Array.from(this._interactions.getAll())
.map(([a, b]) => ({
name: a,
keyStrokes: b?.keyStrokes || 0,
visits: b?.visits || 0,
sessionLength: b?.sessionLength || 0,
lastVisited: b?.lastVisited || 0,
activeLines: b?.activeLines || [],
.map(([name, interaction]) => ({
name,
keyStrokes: interaction?.keyStrokes || 0,
visits: interaction?.visits || 0,
sessionLength: interaction?.sessionLength || 0,
lastVisited: interaction?.lastVisited || 0,
activeLines: interaction?.activeLines || [],
relevanceScore: this.calculateRelevanceScore(interaction)
}))
.sort((a, b) => {
const recencyA = Date.now() - (a.lastVisited || 0)
const recencyB = Date.now() - (b.lastVisited || 0)
const scoreA =
a.keyStrokes * keyStrokeWeight +
a.visits * visitWeight +
a.sessionLength * sessionLengthWeight -
recencyA * recencyWeight
const scoreB =
b.keyStrokes * keyStrokeWeight +
b.visits * visitWeight +
b.sessionLength * sessionLengthWeight -
recencyB * recencyWeight
return scoreB - scoreA
})
.sort((a, b) => b.relevanceScore - a.relevanceScore)
}

getCurrentFile(): string | null {
Expand All @@ -83,7 +86,8 @@ export class FileInteractionCache {
if (!item) return
this._interactions.set(this._currentFile, {
...item,
visits: (item.visits || 0) + 1
visits: (item.visits || 0) + 1,
lastVisited: Date.now()
})
}

Expand All @@ -98,15 +102,19 @@ export class FileInteractionCache {
activeLines: [
...item.activeLines,
{ line: currentLine, character: currentCharacter }
]
],
lastVisited: Date.now()
})

this.resumeSession()
this.resetInactivityTimeout()
}

startSession(filePath: string): void {
this._sessionStartTime = new Date()
this.put(filePath)
this.incrementVisits()
this.resetInactivityTimeout()
}

endSession(): void {
Expand All @@ -127,12 +135,14 @@ export class FileInteractionCache {
if (item) {
this._interactions.set(this._currentFile, {
...item,
sessionLength: (item.sessionLength || 0) + sessionLength
sessionLength: (item.sessionLength || 0) + sessionLength,
lastVisited: Date.now()
})
}

this._sessionStartTime = null
this._sessionPauseTime = null
this._currentFile = null
}

delete(filePath: string): void {
Expand All @@ -143,15 +153,19 @@ export class FileInteractionCache {
put(filePath: string): void {
this._currentFile = filePath.replace('.git', '').replace('.hg', '')
const fileExtension = this._currentFile.split('.').pop()
if (this._interactions.get(this._currentFile)) return
if (this._interactions.get(this._currentFile)) {
this.incrementVisits()
return
}
if (this._currentFile.includes('.') && fileExtension) {
this._interactions.set(this._currentFile, {
name: this._currentFile,
keyStrokes: 0,
visits: 0,
visits: 1,
sessionLength: 0,
activeLines: [],
lastVisited: Date.now()
lastVisited: Date.now(),
relevanceScore: 0
})
}
}
Expand Down
39 changes: 37 additions & 2 deletions src/extension/fim-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import {
STOP_DEEPSEEK,
STOP_LLAMA,
STOP_QWEN,
STOP_STARCODER,
STOP_STARCODER
} from '../common/constants'
import { supportedLanguages } from '../common/languages'
import { FimPromptTemplate } from '../common/types'
import {
RepositoryLevelData,
FimPromptTemplate,
PrefixSuffix
} from '../common/types'

const getFileContext = (
fileContextEnabled: boolean,
Expand Down Expand Up @@ -208,6 +212,37 @@ export const getStopWordsAuto = (fimModel: string) => {
return STOP_LLAMA
}

export const getFimTemplateRepositoryLevel = (
repo: string,
code: RepositoryLevelData[],
prefixSuffix: PrefixSuffix,
currentFileName: string | undefined
) => {
return getFimPromptTemplateQwenMulti(
repo,
code,
prefixSuffix,
currentFileName
)
}

export const getFimPromptTemplateQwenMulti = (
repo: string,
files: RepositoryLevelData[],
prefixSuffix: PrefixSuffix,
currentFileName: string | undefined
): string => {
let prompt = `<|repo_name|>${repo}\n`

for (const file of files) {
prompt += `<|file_sep|>${file.name}\n${file.text}\n`
}

prompt += `<|file_sep|>${currentFileName}\n${prefixSuffix.prefix}`

return prompt.trim()
}

export const getStopWordsChosen = (format: string) => {
if (format === FIM_TEMPLATE_FORMAT.codellama) return STOP_LLAMA
if (format === FIM_TEMPLATE_FORMAT.deepseek) return STOP_DEEPSEEK
Expand Down
1 change: 1 addition & 0 deletions src/extension/provider-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface TwinnyProvider {
type: string
apiKey?: string
fimTemplate?: string
repositoryLevel?: boolean
}

type Providers = Record<string, TwinnyProvider> | undefined
Expand Down
Loading

0 comments on commit 9ee4eb2

Please sign in to comment.