-
Notifications
You must be signed in to change notification settings - Fork 28
Improve example providers #148
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,35 @@ | ||
| # JSON API Example Provider for OpenCtx | ||
|
|
||
| [OpenCtx](https://openctx.org) context provider example that calls a JSON API to fetch context. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```json | ||
| "openctx.providers": { | ||
| // ...other providers... | ||
| "https://openctx.org/npm/@openctx/provider-json-api-example": true | ||
| }, | ||
| ``` | ||
|
|
||
| ## Mention support | ||
|
|
||
| - Vehicle list | ||
| - Vehicle search | ||
|
|
||
| ## Context included | ||
|
|
||
| - Vehicle data | ||
|
|
||
| ## Development | ||
|
|
||
| - [Source code](https://sourcegraph.com/github.com/sourcegraph/openctx/-/tree/provider/json-api-example) | ||
| - [Docs](https://openctx.org/docs/providers/json-api-example) | ||
| - License: Apache 2.0 | ||
|
|
||
| ### Local Testing | ||
|
|
||
| 1. Clone the repo | ||
| 1. `pnpm install` | ||
| 1. `pnpm -C provider/json-api-example bundle --watch` to automatically recompile on changes | ||
| 1. Run `echo file://$(pwd)/provider/json-api-example/dist/bundle.js` and use that URL in your OpenCtx instead of `"https://openctx.org/npm/@openctx/provider-json-api-example"` | ||
| 1. Reload your OpenCtx client |
This file contains hidden or 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,68 @@ | ||
| import type { | ||
| ItemsParams, | ||
| ItemsResult, | ||
| MentionsParams, | ||
| MentionsResult, | ||
| MetaParams, | ||
| MetaResult, | ||
| Provider, | ||
| ProviderSettings, | ||
| } from '@openctx/provider' | ||
|
|
||
| // https://swapi.dev/documentation#vehicles | ||
| type Vehicle = { | ||
| name: string | ||
| model: string | ||
| manufacturer: string | ||
| url: string | ||
| } | ||
|
|
||
| const provider: Provider = { | ||
| meta(params: MetaParams, settings: ProviderSettings): MetaResult { | ||
| return { | ||
| name: 'JSON API Example', | ||
| mentions: {}, | ||
| } | ||
| }, | ||
|
|
||
| async mentions(params: MentionsParams, settings: ProviderSettings): Promise<MentionsResult> { | ||
| const endpoint = 'https://swapi.dev/api/vehicles' | ||
| const url = params.query ? `${endpoint}?search=${encodeURIComponent(params.query)}` : endpoint | ||
|
|
||
| return fetch(url) | ||
| .then(response => response.json() as Promise<{ results: Vehicle[] }>) | ||
| .then(data => { | ||
| return data.results.map(vehicle => ({ | ||
| title: `${vehicle.name} (${vehicle.model})`, | ||
| description: vehicle.manufacturer, | ||
| uri: vehicle.url, | ||
| data: { | ||
| // Only minimal data should used here (e.g. an id or key) | ||
| id: vehicle.url.split('/').pop(), | ||
| }, | ||
| })) | ||
| }) | ||
| }, | ||
|
|
||
| async items(params: ItemsParams, settings: ProviderSettings): Promise<ItemsResult> { | ||
| // We just use the `mention.data`, but you can also make use of `params.message` | ||
| const data = params.mention?.data as { id: string } | ||
|
|
||
| const vehicle = await fetch(`https://swapi.dev/api/vehicles/${data.id}`).then( | ||
| response => response.json() as Promise<Vehicle> | ||
| ) | ||
|
|
||
| // We return one item, but you can return more to provide more context | ||
| return [ | ||
| { | ||
| title: vehicle.name, | ||
| url: vehicle.url, | ||
| ai: { | ||
| content: `Star wars vehicle named @${vehicle.name}: ${JSON.stringify(vehicle)}`, | ||
| }, | ||
| }, | ||
| ] | ||
| }, | ||
| } | ||
|
|
||
| export default provider |
This file contains hidden or 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,26 @@ | ||
| { | ||
| "name": "@openctx/provider-json-api-example", | ||
| "version": "0.0.1", | ||
| "description": "JSON API Example (OpenCtx provider)", | ||
| "license": "Apache-2.0", | ||
| "homepage": "https://openctx.org/docs/providers/json-api-example", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/sourcegraph/json-api-example", | ||
| "directory": "provider/json-api-example" | ||
| }, | ||
| "type": "module", | ||
| "main": "dist/bundle.js", | ||
| "types": "dist/index.d.ts", | ||
| "files": ["dist/bundle.js", "dist/index.d.ts"], | ||
| "sideEffects": false, | ||
| "scripts": { | ||
| "bundle": "tsc --build && esbuild --log-level=error --bundle --format=esm --outfile=dist/bundle.js index.ts", | ||
| "prepublishOnly": "tsc --build --clean && npm run --silent bundle", | ||
| "test": "vitest" | ||
| }, | ||
| "dependencies": { | ||
| "@openctx/provider": "workspace:*" | ||
| } | ||
| } | ||
|
|
This file contains hidden or 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,12 @@ | ||
| { | ||
| "extends": "../../.config/tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "rootDir": ".", | ||
| "outDir": "dist", | ||
| "lib": ["ESNext"], | ||
| }, | ||
| "include": ["*.ts"], | ||
| "exclude": ["dist", "vitest.config.ts"], | ||
| "references": [{ "path": "../../lib/provider" }], | ||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we mention testing with the cli as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's a good idea, will do. It's pretty hard to develop a provider without the cli tbh