-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
vscode/src/completions/context/retrievers/rules-retriever.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { | ||
type AutocompleteContextSnippet, | ||
type RuleService, | ||
firstValueFrom, | ||
} from '@sourcegraph/cody-shared' | ||
import type { Disposable } from 'vscode' | ||
import { URI } from 'vscode-uri' | ||
import { createFileSystemRuleService } from '../../../rules/fs-rules' | ||
import type { ContextRetriever, ContextRetrieverOptions } from '../../types' | ||
import { RetrieverIdentifier } from '../utils' | ||
|
||
export class RulesRetriever implements Disposable, ContextRetriever { | ||
public identifier = RetrieverIdentifier.RulesRetriever | ||
|
||
private ruleService: RuleService | ||
public constructor() { | ||
this.ruleService = createFileSystemRuleService() | ||
} | ||
|
||
public async retrieve({ document }: ContextRetrieverOptions): Promise<AutocompleteContextSnippet[]> { | ||
const rules = await firstValueFrom(this.ruleService.rulesForPaths([document.uri])) | ||
|
||
return rules.map( | ||
rule => | ||
({ | ||
type: 'base', | ||
identifier: this.identifier, | ||
content: rule.instruction, | ||
uri: URI.parse(rule.uri), | ||
}) satisfies AutocompleteContextSnippet | ||
) | ||
} | ||
|
||
public isSupportedForLanguageId(): boolean { | ||
return true | ||
} | ||
|
||
public dispose() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { | ||
RULES_PROVIDER_URI, | ||
type RulePromptPart, | ||
firstValueFrom, | ||
pluralize, | ||
} from '@sourcegraph/cody-shared' | ||
import { URI } from 'vscode-uri' | ||
import { createFileSystemRuleService } from '../../rules/fs-rules' | ||
import type { OpenCtxProvider } from './types' | ||
|
||
/** | ||
* An OpenCtx provider that attaches rules for the current file. | ||
*/ | ||
export function createRulesProvider(): OpenCtxProvider { | ||
const ruleService = createFileSystemRuleService() | ||
return { | ||
providerUri: RULES_PROVIDER_URI, | ||
|
||
meta() { | ||
return { | ||
name: 'Rules', | ||
mentions: { autoInclude: true }, | ||
} | ||
}, | ||
|
||
async mentions({ autoInclude, uri }) { | ||
if (!uri || !autoInclude) { | ||
return [] | ||
} | ||
|
||
const rules = await firstValueFrom(ruleService.rulesForPaths([URI.parse(uri)])) | ||
return rules.length === 0 | ||
? [] | ||
: [ | ||
{ | ||
title: `${rules.length} ${pluralize('rule', rules.length)}`, | ||
uri: 'rules+openctx://rules', // dummy URI | ||
data: { rules }, | ||
}, | ||
] | ||
}, | ||
|
||
async items(params) { | ||
const rules = params.mention?.data?.rules as RulePromptPart[] | undefined | ||
return ( | ||
rules?.map(rule => ({ | ||
title: rule.title ?? rule.display_name, | ||
uri: rule.uri, | ||
url: rule.uri, | ||
ai: { content: rule.instruction }, | ||
})) ?? [] | ||
) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters