Skip to content

Commit

Permalink
Include initial context from OpenCtx providers. (#5433)
Browse files Browse the repository at this point in the history
context:
https://linear.app/sourcegraph/issue/PROD-264/one-pager-for-how-to-get-this-set-up-using-openctx

We have [added](sourcegraph/openctx#199)
`meta.mentions.autoInclude` property for OpenCtx providers. The
providers which set that to `true` will be used to include the mentions
they return as initial context items. We are also passing the active
file uri and codebase names to the `mentions` function.


## Test plan
Set the following as the vs code setting and see the hello world mention
item chip appearing as the default context item.
```json
"openctx.providers": {
        "https://openctx.org/npm/@openctx/provider-hello-world": true
}
```

## Changelog

Include initial context from OpenCtx providers.
  • Loading branch information
thenamankumar authored Sep 3, 2024
1 parent a17d9ee commit 2bc6daa
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 61 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"vitest": "^2.0.5"
},
"dependencies": {
"@openctx/client": "^0.0.25",
"@openctx/client": "^0.0.26",
"@sourcegraph/telemetry": "^0.18.0",
"ignore": "^5.3.1",
"observable-fns": "^0.6.1",
Expand Down
107 changes: 55 additions & 52 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1354,8 +1354,8 @@
},
"dependencies": {
"@anthropic-ai/sdk": "^0.20.8",
"@openctx/provider-linear-issues": "^0.0.6",
"@openctx/vscode-lib": "^0.0.21",
"@openctx/provider-linear-issues": "^0.0.7",
"@openctx/vscode-lib": "^0.0.22",
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/core": "^1.18.1",
"@opentelemetry/exporter-trace-otlp-http": "^0.45.1",
Expand Down
33 changes: 31 additions & 2 deletions vscode/src/chat/clientStateBroadcaster.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import {
type ContextItem,
type ContextItemOpenCtx,
ContextItemSource,
type ContextItemTree,
REMOTE_REPOSITORY_PROVIDER_URI,
contextFiltersProvider,
displayLineRange,
displayPathBasename,
expandToLineRange,
openCtx,
subscriptionDisposable,
} from '@sourcegraph/cody-shared'
import * as vscode from 'vscode'
import { URI } from 'vscode-uri'
import { getSelectionOrFileContext } from '../commands/context/selection'
import { createRepositoryMention } from '../context/openctx/common/get-repository-mentions'
import { workspaceReposMonitor } from '../repository/repo-metadata-from-git-api'
import { authProvider } from '../services/AuthProvider'
import type { ChatModel } from './chat-view/ChatModel'
import { contextItemMentionFromOpenCtxItem } from './context/chatContext'
import {
contextItemMentionFromOpenCtxItem,
getActiveEditorContextForOpenCtxMentions,
} from './context/chatContext'
import type { ExtensionMessage } from './protocol'

type PostMessage = (message: Extract<ExtensionMessage, { type: 'clientState' }>) => void
Expand Down Expand Up @@ -153,7 +159,30 @@ export async function getCorpusContextItemsForEditorState(useRemote: boolean): P
}
}

return items
const providers = (await openCtx.controller?.meta({}))?.filter(meta => meta.mentions?.autoInclude)
if (!providers) {
return items
}

const activeEditorContext = await getActiveEditorContextForOpenCtxMentions()

const openctxMentions = (
await Promise.all(
providers.map(async (provider): Promise<ContextItemOpenCtx[]> => {
const mentions =
(await openCtx?.controller?.mentions(activeEditorContext, provider)) || []

return mentions.map(mention => ({
...mention,
provider: 'openctx',
type: 'openctx',
uri: URI.parse(mention.uri),
}))
})
)
).flat()

return [...items, ...openctxMentions]
}

function idempotentPostMessage(rawPostMessage: PostMessage): PostMessage {
Expand Down
Loading

0 comments on commit 2bc6daa

Please sign in to comment.