Skip to content

fix(amazonq): workspace rules and saved prompts not working #1063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { DEFAULT_HELP_FOLLOW_UP_PROMPT, HELP_MESSAGE } from '../chat/constants'
import { TelemetryService } from '../../shared/telemetry/telemetryService'
import { AmazonQTokenServiceManager } from '../../shared/amazonQServiceManager/AmazonQTokenServiceManager'
import { TabBarController } from './tabBarController'
import { getUserPromptsDirectory } from './context/contextUtils'
import { getUserPromptsDirectory, promptFileExtension } from './context/contextUtils'
import { AdditionalContextProvider } from './context/addtionalContextProvider'
import { ContextCommandsProvider } from './context/contextCommandsProvider'
import { ChatDatabase } from './tools/chatDb/chatDb'
Expand Down Expand Up @@ -1136,15 +1136,15 @@ describe('AgenticChatController', () => {
describe('onCreatePrompt', () => {
it('should create prompt file with given name', async () => {
const promptName = 'testPrompt'
const expectedPath = path.join(getUserPromptsDirectory(), 'testPrompt.prompt.md')
const expectedPath = path.join(getUserPromptsDirectory(), `testPrompt${promptFileExtension}`)

await chatController.onCreatePrompt({ promptName })

sinon.assert.calledOnceWithExactly(fsWriteFileStub, expectedPath, '', { mode: 0o600 })
})

it('should create default prompt file when no name provided', async () => {
const expectedPath = path.join(getUserPromptsDirectory(), 'default.prompt.md')
const expectedPath = path.join(getUserPromptsDirectory(), `default${promptFileExtension}`)

await chatController.onCreatePrompt({ promptName: '' })

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AdditionalContextPrompt } from 'local-indexing'
import { AdditionalContextProvider } from './addtionalContextProvider'
import { getUserPromptsDirectory } from './contextUtils'
import { LocalProjectContextController } from '../../../shared/localProjectContextController'
import { workspaceUtils } from '@aws/lsp-core'

describe('AdditionalContextProvider', () => {
let provider: AdditionalContextProvider
Expand Down Expand Up @@ -54,22 +55,23 @@ describe('AdditionalContextProvider', () => {
uri: URI.file('/workspace').toString(),
name: 'test',
}
sinon.stub(workspaceUtils, 'getWorkspaceFolderPaths').returns(['/workspace'])
const triggerContext = {
workspaceFolder: mockWorkspaceFolder,
context: [],
workspaceRulesCount: 0,
}

fsExistsStub.resolves(true)
fsReadDirStub.resolves([{ name: 'rule1.prompt.md', isFile: () => true }])
fsReadDirStub.resolves([{ name: 'rule1.md', isFile: () => true }])

getContextCommandPromptStub.resolves([
{
name: 'Test Rule',
description: 'Test Description',
content: 'Test Content',
filePath: '/workspace/.amazonq/rules/rule1.prompt.md',
relativePath: '.amazonq/rules/rule1.prompt.md',
filePath: '/workspace/.amazonq/rules/rule1.md',
relativePath: '.amazonq/rules/rule1.md',
startLine: 1,
endLine: 10,
},
Expand Down Expand Up @@ -134,8 +136,8 @@ describe('AdditionalContextProvider', () => {

describe('getContextType', () => {
const mockPrompt: AdditionalContextPrompt = {
filePath: path.join('/workspace', '.amazonq', 'rules', 'test.prompt.md'),
relativePath: path.join('.amazonq', 'rules', 'test.prompt.md'),
filePath: path.join('/workspace', '.amazonq', 'rules', 'test.md'),
relativePath: path.join('.amazonq', 'rules', 'test.md'),
content: 'Sample content',
name: 'Test Rule',
description: 'Test Description',
Expand All @@ -151,8 +153,8 @@ describe('AdditionalContextProvider', () => {
it('should identify prompt type for files in user prompts directory', () => {
const userPromptsDir = getUserPromptsDirectory()
const mockPrompt = {
filePath: path.join(userPromptsDir, 'test.prompt.md'),
relativePath: 'test.prompt.md',
filePath: path.join(userPromptsDir, 'test.md'),
relativePath: 'test.md',
content: 'Sample content',
name: 'Test Prompt',
description: 'Test Description',
Expand Down Expand Up @@ -184,37 +186,39 @@ describe('AdditionalContextProvider', () => {

describe('collectWorkspaceRules', () => {
it('should return empty array when no workspace folder', async () => {
const triggerContext = {
relativeFilePath: 'test.ts',
workspaceFolder: null,
}
// Mock empty workspace folders
sinon.stub(workspaceUtils, 'getWorkspaceFolderPaths').returns([])

const result = await provider.collectWorkspaceRules(triggerContext)
const result = await provider.collectWorkspaceRules()

assert.deepStrictEqual(result, [])
})

it('should return rules files when they exist', async () => {
const mockWorkspaceFolder = {
uri: URI.file('/workspace').toString(),
name: 'test',
}
const triggerContext = {
relativeFilePath: 'test.ts',
workspaceFolder: mockWorkspaceFolder,
}
// Mock workspace folders
sinon.stub(workspaceUtils, 'getWorkspaceFolderPaths').returns(['/workspace'])

fsExistsStub.resolves(true)
fsReadDirStub.resolves([
{ name: 'rule1.prompt.md', isFile: () => true },
{ name: 'rule2.prompt.md', isFile: () => true },
{ name: 'rule1.md', isFile: () => true },
{ name: 'rule2.md', isFile: () => true },
])

const result = await provider.collectWorkspaceRules(triggerContext)
const result = await provider.collectWorkspaceRules()

assert.deepStrictEqual(result, [
path.join('/workspace', '.amazonq', 'rules', 'rule1.prompt.md'),
path.join('/workspace', '.amazonq', 'rules', 'rule2.prompt.md'),
{
workspaceFolder: '/workspace',
type: 'file',
relativePath: path.join('.amazonq', 'rules', 'rule1.md'),
id: '',
},
{
workspaceFolder: '/workspace',
type: 'file',
relativePath: path.join('.amazonq', 'rules', 'rule2.md'),
id: '',
},
])
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FileDetails, QuickActionCommand, FileList, ContextCommand } from '@aws/language-server-runtimes/protocol'
import { AdditionalContextPrompt, ContextCommandItem, ContextCommandItemType } from 'local-indexing'
import * as path from 'path'

Check warning on line 3 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/context/addtionalContextProvider.ts

View workflow job for this annotation

GitHub Actions / Test

Do not import Node.js builtin module "path"

Check warning on line 3 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/context/addtionalContextProvider.ts

View workflow job for this annotation

GitHub Actions / Test (Windows)

Do not import Node.js builtin module "path"
import { AdditionalContentEntryAddition, TriggerContext } from './agenticChatTriggerContext'
import { URI } from 'vscode-uri'
import { Lsp, Workspace } from '@aws/language-server-runtimes/server-interface'
Expand All @@ -19,24 +19,29 @@
private readonly lsp: Lsp
) {}

async collectWorkspaceRules(triggerContext: TriggerContext): Promise<string[]> {
const rulesFiles: string[] = []
const folder = triggerContext.workspaceFolder
if (!folder) {
async collectWorkspaceRules(): Promise<ContextCommandItem[]> {
const rulesFiles: ContextCommandItem[] = []
let workspaceFolders = workspaceUtils.getWorkspaceFolderPaths(this.lsp)

if (!workspaceFolders.length) {
return rulesFiles
}
const workspaceRoot = folder.uri
? URI.parse(folder.uri).fsPath
: workspaceUtils.getWorkspaceFolderPaths(this.lsp)[0]
const rulesPath = path.join(workspaceRoot, '.amazonq', 'rules')
const folderExists = await this.workspace.fs.exists(rulesPath)
for (const workspaceFolder of workspaceFolders) {
const rulesPath = path.join(workspaceFolder, '.amazonq', 'rules')
const folderExists = await this.workspace.fs.exists(rulesPath)

if (folderExists) {
const entries = await this.workspace.fs.readdir(rulesPath)
if (folderExists) {
const entries = await this.workspace.fs.readdir(rulesPath)

for (const entry of entries) {
if (entry.isFile() && entry.name.endsWith(promptFileExtension)) {
rulesFiles.push(path.join(rulesPath, entry.name))
for (const entry of entries) {
if (entry.isFile() && entry.name.endsWith(promptFileExtension)) {
rulesFiles.push({
workspaceFolder: workspaceFolder,
type: 'file',
relativePath: path.relative(workspaceFolder, path.join(rulesPath, entry.name)),
id: '',
})
}
}
}
}
Expand All @@ -59,23 +64,13 @@
context?: ContextCommand[]
): Promise<AdditionalContentEntryAddition[]> {
const additionalContextCommands: ContextCommandItem[] = []
const workspaceRules = await this.collectWorkspaceRules(triggerContext)
const workspaceRules = await this.collectWorkspaceRules()
let workspaceFolderPath = triggerContext.workspaceFolder?.uri
? URI.parse(triggerContext.workspaceFolder.uri).fsPath
: workspaceUtils.getWorkspaceFolderPaths(this.lsp)[0]

if (workspaceRules.length > 0) {
additionalContextCommands.push(
...workspaceRules.map(
file =>
({
workspaceFolder: workspaceFolderPath,
type: 'file',
relativePath: path.relative(workspaceFolderPath, file),
id: '',
}) as ContextCommandItem
)
)
additionalContextCommands.push(...workspaceRules)
}
triggerContext.workspaceRulesCount = workspaceRules.length
if (context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getUserHomeDir } from '@aws/lsp-core/out/util/path'
import * as path from 'path'

Check warning on line 2 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/context/contextUtils.ts

View workflow job for this annotation

GitHub Actions / Test

Do not import Node.js builtin module "path"

Check warning on line 2 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/context/contextUtils.ts

View workflow job for this annotation

GitHub Actions / Test (Windows)

Do not import Node.js builtin module "path"

export const promptFileExtension = '.prompt.md'
export const promptFileExtension = '.md'
export const additionalContentInnerContextLimit = 8192
export const additionalContentNameLimit = 1024

Expand Down
Loading