Skip to content
Draft
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
9 changes: 9 additions & 0 deletions chat-client/src/client/messager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ export class Messager {

onSourceLinkClick = (params: SourceLinkClickParams): void => {
this.chatApi.sourceLinkClick(params)
this.onSourceLinkClickTelemetry(params)
}

/**
* Records the telemetry of every source link click: the clicks that chat sends to the IDE (onSourceLinkClick)
* and the clicks that it does not send because the link is not http or https. The event carries the link, and
* the server records it as the interaction target, so the scheme tells them apart.
*/
onSourceLinkClickTelemetry = (params: SourceLinkClickParams): void => {
this.chatApi.telemetry({ ...params, name: SOURCE_LINK_CLICK_TELEMETRY_EVENT })
}

Expand Down
49 changes: 49 additions & 0 deletions chat-client/src/client/mynahUi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,55 @@ describe('MynahUI', () => {
})
})

describe('source link clicks', () => {
const props = () => (mynahUi as any).props
const sourceLinkTelemetry = () =>
(outboundChatApi.telemetry as sinon.SinonStub).getCalls().filter(c => c.args[0].name === 'sourceLinkClick')

it('sends an https or http source link to the IDE and records telemetry', () => {
props().onSourceLinkClick('tab-1', 'answer-1', 'https://example.com/doc', undefined, 'event-1')
props().onSourceLinkClick('tab-1', 'answer-1', 'http://example.com/page', undefined, 'event-2')

const sourceLinkClick = outboundChatApi.sourceLinkClick as sinon.SinonStub
sinon.assert.calledTwice(sourceLinkClick)
deepStrictEqual(sourceLinkClick.firstCall.args[0], {
tabId: 'tab-1',
messageId: 'answer-1',
link: 'https://example.com/doc',
eventId: 'event-1',
})
strictEqual(sourceLinkClick.secondCall.args[0].link, 'http://example.com/page')
strictEqual(sourceLinkTelemetry().length, 2)
sinon.assert.notCalled(notifySpy)
})

it('does not send a source link with another scheme or a local path, and tells the user', () => {
const links = [
'javascript:x',
'file:///etc/hosts',
'vscode://settings/x',
'command:workbench.action.reloadWindow',
'src/foo.ts',
'\\\\host\\share\\x',
'',
]
for (const link of links) {
props().onSourceLinkClick('tab-1', 'answer-1', link, undefined, 'event-1')
}

sinon.assert.notCalled(outboundChatApi.sourceLinkClick as sinon.SinonStub)
sinon.assert.notCalled(outboundChatApi.fileClick as sinon.SinonStub)
sinon.assert.notCalled(outboundChatApi.linkClick as sinon.SinonStub)
strictEqual(notifySpy.callCount, links.length)
strictEqual(notifySpy.firstCall.args[0].type, NotificationType.WARNING)
strictEqual(notifySpy.firstCall.args[0].title, 'Link not opened')
deepStrictEqual(
sourceLinkTelemetry().map(c => c.args[0].link),
links
)
})
})

describe('onMessageDismiss', () => {
it('acknowledges the deprecation card and removes it from every open and future new chat', () => {
const updateTabDefaultsSpy = sinon.spy(mynahUi, 'updateTabDefaults')
Expand Down
8 changes: 7 additions & 1 deletion chat-client/src/client/mynahUi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,13 @@ export const createMynahUi = (
link,
eventId,
}
messager.onSourceLinkClick(payload)
// A source link comes from the service, so only an http or https URL goes to the IDE.
if (classifyChatLink(link).kind === 'link') {
messager.onSourceLinkClick(payload)
return
}
messager.onSourceLinkClickTelemetry(payload)
notifyUnsupportedLink()
},
onInfoLinkClick: (tabId, link, mouseEvent, eventId) => {
mouseEvent?.preventDefault()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,115 @@ describe('AgenticChatController', () => {

sinon.assert.calledOnce(getSubscriptionStatusStub)
})

it('logs the subscription status without the subscription URL', async () => {
getSubscriptionStatusStub.resolves({
status: 'none',
encodedVerificationUrl: 'https://signin.aws.amazon.com/oauth?activationToken=TOKEN-1234',
})

chatController.onTabAdd({ tabId: mockTabId })
await new Promise(resolve => setTimeout(resolve, 0))

sinon.assert.calledWith(
testFeatures.logging.log as sinon.SinonStub,
'setPaidTierMode: getSubscriptionStatus: none'
)
sinon.assert.neverCalledWithMatch(testFeatures.logging.log as sinon.SinonStub, sinon.match('TOKEN-1234'))
})
})

describe('onManageSubscription', () => {
// The form of the URL that the service returns. The activation token must not reach the log.
const activationToken = 'TOKEN-1234'
const signInUrl = `https://signin.aws.amazon.com/oauth?redirect_uri=https://us-east-1.console.aws.amazon.com/amazonq/developer/home?hashArgs=%23%2Fsubscriptions%2FbuilderId%3FactivationToken%3D${activationToken}&isauthcode=true&region=us-east-1&response_type=code`
let getCodewhispererServiceStub: sinon.SinonStub
let getSubscriptionStatusStub: sinon.SinonStub
let waitUntilSubscriptionActiveStub: sinon.SinonStub

const loggedText = () =>
(['log', 'info', 'warn', 'error', 'debug'] as const)
.flatMap(level => (testFeatures.logging[level] as sinon.SinonStub).args)
.map(args => args.join(' '))
.join('\n')

// Runs the "Upgrade Q" flow of a free-tier user whose subscription status response has this URL
const manageSubscription = async (encodedVerificationUrl: string | undefined) => {
getSubscriptionStatusStub.resolves({ status: 'none', encodedVerificationUrl })
await chatController.onManageSubscription(mockTabId)
// onManageSubscription handles the status response asynchronously
await new Promise(resolve => setTimeout(resolve, 0))
}

const upgradePendingUpdate = sinon.match({ paidTierMode: 'upgrade-pending' })

const assertRequestFailedMessage = () => {
sinon.assert.notCalled(testFeatures.lsp.window.showDocument as sinon.SinonStub)
sinon.assert.notCalled(waitUntilSubscriptionActiveStub)
sinon.assert.neverCalledWithMatch(testFeatures.chat.sendChatUpdate as sinon.SinonStub, upgradePendingUpdate)
sinon.assert.calledOnceWithExactly(testFeatures.lsp.window.showMessage as sinon.SinonStub, {
message: 'Subscription request failed.',
type: MessageType.Error,
})
}

beforeEach(() => {
getSubscriptionStatusStub = sinon.stub()
waitUntilSubscriptionActiveStub = sinon.stub().resolves(true)
getCodewhispererServiceStub = sinon
.stub(AmazonQTokenServiceManager.prototype, 'getCodewhispererService')
.returns({
getSubscriptionStatus: getSubscriptionStatusStub,
waitUntilSubscriptionActive: waitUntilSubscriptionActiveStub,
} as any)
;(testFeatures.lsp.window.showDocument as sinon.SinonStub).resolves({ success: true })
testFeatures.lsp.window.showMessage = sinon.stub().resolves()
})

afterEach(() => {
getCodewhispererServiceStub.restore()
})

it('opens an https URL on the AWS sign-in host in the external browser without logging it', async () => {
await manageSubscription(signInUrl)

sinon.assert.calledOnceWithExactly(testFeatures.lsp.window.showDocument as sinon.SinonStub, {
external: true,
uri: signInUrl,
})
sinon.assert.calledWithMatch(testFeatures.chat.sendChatUpdate as sinon.SinonStub, upgradePendingUpdate)
sinon.assert.calledOnce(waitUntilSubscriptionActiveStub)
assert.ok(!loggedText().includes(activationToken))
})

it('opens the URL in its normalized form', async () => {
await manageSubscription('HTTPS://SIGNIN.AWS.AMAZON.COM:443/oauth?state=1')

sinon.assert.calledOnceWithExactly(testFeatures.lsp.window.showDocument as sinon.SinonStub, {
external: true,
uri: 'https://signin.aws.amazon.com/oauth?state=1',
})
})

for (const uri of [
`vscode://publisher.extension/path?activationToken=${activationToken}`,
`file:///tmp/file.txt?activationToken=${activationToken}`,
`http://signin.aws.amazon.com/oauth?activationToken=${activationToken}`,
`https://example.com/oauth?activationToken=${activationToken}`,
]) {
it(`does not open ${uri.split('?')[0]} and shows an error without logging the URL`, async () => {
await manageSubscription(uri)

assertRequestFailedMessage()
assert.ok(!loggedText().includes(activationToken))
})
}

it('shows an error when the response has no URL', async () => {
await manageSubscription(undefined)

assertRequestFailedMessage()
})
})

it('onTabRemove unsets tab id if current tab is removed and emits metrics', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* Will be deleted or merged.
*/

import * as crypto from 'crypto'

Check warning on line 6 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test (Windows)

Do not import Node.js builtin module "crypto"

Check warning on line 6 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test

Do not import Node.js builtin module "crypto"
import * as path from 'path'

Check warning on line 7 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test (Windows)

Do not import Node.js builtin module "path"

Check warning on line 7 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test

Do not import Node.js builtin module "path"
import * as os from 'os'

Check warning on line 8 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test (Windows)

Do not import Node.js builtin module "os"

Check warning on line 8 in server/aws-lsp-codewhisperer/src/language-server/agenticChat/agenticChatController.ts

View workflow job for this annotation

GitHub Actions / Test

Do not import Node.js builtin module "os"
import {
ChatTriggerType,
Origin,
Expand Down Expand Up @@ -251,6 +251,7 @@
onPaidTierLearnMore,
paidTierManageSubscription,
PaidTierMode,
parseSubscriptionUpgradeUrl,
qProName,
} from '../paidTier/paidTier'
import {
Expand Down Expand Up @@ -4852,9 +4853,8 @@
?.getCodewhispererService()
.getSubscriptionStatus(true)
.then(o => {
this.#log(
`setPaidTierMode: getSubscriptionStatus: ${o.status} ${o.encodedVerificationUrl}`
)
// Log only the status: the subscription URL contains an activation token
this.#log(`setPaidTierMode: getSubscriptionStatus: ${o.status}`)
this.setPaidTierMode(tabId, o.status !== 'none' ? 'paidtier' : 'freetier')
})
.catch(err => {
Expand Down Expand Up @@ -4922,7 +4922,8 @@
client
?.getSubscriptionStatus()
.then(o => {
this.#log(`onManageSubscription: getSubscriptionStatus: ${o.status} ${o.encodedVerificationUrl}`)
// Log only the status: the subscription URL contains an activation token
this.#log(`onManageSubscription: getSubscriptionStatus: ${o.status}`)

if (o.status !== 'none') {
// Paid-tier user: navigate them to the "Manage Subscriptions" AWS console page.
Expand All @@ -4937,10 +4938,14 @@
})
} else {
// Free-tier user: navigate them to "Upgrade Q" flow in AWS console.
const uri = o.encodedVerificationUrl
// The client opens the URL in the external browser, so it must be on the AWS sign-in host.
const uri = parseSubscriptionUpgradeUrl(o.encodedVerificationUrl)

if (!uri) {
this.#log('onManageSubscription: missing encodedVerificationUrl in server response')
const reason = o.encodedVerificationUrl
? 'encodedVerificationUrl in server response is not an https URL on the AWS sign-in host'
: 'missing encodedVerificationUrl in server response'
this.#log(`onManageSubscription: ${reason}`)
this.#features.lsp.window
.showMessage({
message: 'Subscription request failed.',
Expand All @@ -4949,21 +4954,10 @@
.catch(e => {
this.#log(`onManageSubscription: showMessage failed: ${(e as Error).message}`)
})
return 'missing encodedVerificationUrl in server response'
return reason
}

try {
URI.parse(uri)
} catch (e) {
this.#log(
`onManageSubscription: invalid encodedVerificationUrl: '${uri}': ${(e as Error).message}`
)
return 'invalid encodedVerificationUrl'
}

this.#log(
`onManageSubscription: createSubscriptionToken status: ${o.status} encodedVerificationUrl: '${uri}'`
)
this.#log(`onManageSubscription: createSubscriptionToken status: ${o.status}`)
// Set UI to "progress" mode.
this.setPaidTierMode(tabId, 'upgrade-pending')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,74 @@ describe('AgenticChatEventParser', () => {
assert.strictEqual(result.success, true)
assert.strictEqual(result.data?.toolUses['tool-1'].stop, false)
})

describe('source links', () => {
const webLink = (url: string | undefined, title = 'title') => ({ title, snippet: 'snippet', url })

it('keeps only http and https source links, in order and unchanged', () => {
const chatEventParser = new AgenticChatEventParser(mockMessageId, new Metric<AddMessageEvent>(), logging)

chatEventParser.processPartialEvent({
supplementaryWebLinksEvent: {
supplementaryWebLinks: [
webLink('https://docs.example.com/guide?a=1#part', 'https link'),
webLink('javascript:x'),
webLink('HTTP://Example.com/Page', 'http link'),
webLink('data:text/html,hello'),
webLink('file:///etc/hosts'),
webLink('vscode://settings/x'),
webLink('command:workbench.action.reloadWindow'),
webLink('mailto:someone@example.com'),
webLink('//example.com/no-scheme'),
webLink('docs/relative.md'),
webLink('https://'),
webLink(''),
webLink(undefined),
],
},
})

const result = chatEventParser.getResult()
assert.deepStrictEqual(result.data?.chatResult.relatedContent, {
content: [
{ title: 'https link', url: 'https://docs.example.com/guide?a=1#part', body: 'snippet' },
{ title: 'http link', url: 'HTTP://Example.com/Page', body: 'snippet' },
],
})
assert.strictEqual(chatEventParser.totalEvents.supplementaryWebLinksEvent, 1)
})

it('adds no related content when an event has no http or https link', () => {
const chatEventParser = new AgenticChatEventParser(mockMessageId, new Metric<AddMessageEvent>(), logging)

chatEventParser.processPartialEvent({
supplementaryWebLinksEvent: {
supplementaryWebLinks: [webLink('javascript:x'), webLink('file:///etc/hosts')],
},
})

assert.strictEqual(chatEventParser.getResult().data?.chatResult.relatedContent, undefined)
assert.strictEqual(chatEventParser.totalEvents.supplementaryWebLinksEvent, 1)
})

it('keeps the links of earlier events when a later event has no web link', () => {
const chatEventParser = new AgenticChatEventParser(mockMessageId, new Metric<AddMessageEvent>(), logging)

chatEventParser.processPartialEvent({
supplementaryWebLinksEvent: { supplementaryWebLinks: [webLink('https://a.example.com/')] },
})
chatEventParser.processPartialEvent({
supplementaryWebLinksEvent: { supplementaryWebLinks: [webLink('vbscript:x')] },
})
chatEventParser.processPartialEvent({
supplementaryWebLinksEvent: { supplementaryWebLinks: [webLink('http://b.example.com/x')] },
})

assert.deepStrictEqual(
chatEventParser.getResult().data?.chatResult.relatedContent?.content.map(link => link.url),
['https://a.example.com/', 'http://b.example.com/x']
)
assert.strictEqual(chatEventParser.totalEvents.supplementaryWebLinksEvent, 3)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { AddMessageEvent } from '../../shared/telemetry/types'
import { Metric } from '../../shared/telemetry/metric'
import { Features } from '@aws/language-server-runtimes/server-interface/server'
import { loggingUtils } from '@aws/lsp-core'
import { isWebUrl } from '../../shared/utils'

export type ChatResultWithMetadata = {
chatResult: ChatResult
Expand Down Expand Up @@ -180,13 +181,15 @@ export class AgenticChatEventParser implements ChatResult {
supplementaryWebLinksEvent.supplementaryWebLinks.length > 0
) {
this.#totalEvents.supplementaryWebLinksEvent += 1
const sourceLinks = supplementaryWebLinksEvent.supplementaryWebLinks.map(
AgenticChatEventParser.mapRelatedData
)
const sourceLinks = supplementaryWebLinksEvent.supplementaryWebLinks
.filter(link => isWebUrl(link.url))
.map(AgenticChatEventParser.mapRelatedData)

this.relatedContent = {
...this.relatedContent,
content: [...(this.relatedContent?.content ?? []), ...sourceLinks],
if (sourceLinks.length > 0) {
this.relatedContent = {
...this.relatedContent,
content: [...(this.relatedContent?.content ?? []), ...sourceLinks],
}
}
} else if (codeReferenceEvent?.references && codeReferenceEvent.references.length > 0) {
this.#totalEvents.codeReferenceEvent += 1
Expand Down
Loading
Loading