Skip to content

Commit

Permalink
bin: introduce mention-items subcommand
Browse files Browse the repository at this point in the history
It is quite clunky to copy paste the output mention JSON into the items
subcommand. So this introduces a new subcommand "mention-items" which
automates that process. It will take an item from the mentions results
and send it to items. By default it is the first mention, but can does
accept an argument for another mention.

Test Plan: Ran locally. See this example:

  $ OPENCTX_CONFIG=../provider/devdocs/dist/bundle.js pnpm openctx mentions 'abort controller'
  #1 AbortController — https://devdocs.io/dom/abortcontroller
  #2 AbortController.abort — https://devdocs.io/dom/abortcontroller/abort
  #3 AbortController.signal — https://devdocs.io/dom/abortcontroller/signal
  #4 AbortController.constructor — https://devdocs.io/dom/abortcontroller/abortcontroller
  #5 ReadableByteStreamController.byobRequest — https://devdocs.io/dom/readablebytestreamcontroller/byobrequest

  $ OPENCTX_CONFIG=../provider/devdocs/dist/bundle.js pnpm openctx mention-items 'abort controller'
  #1 AbortController — https://devdocs.io/dom/abortcontroller
     - ai.content: (13659 characters)
  • Loading branch information
keegancsmith committed May 31, 2024
1 parent 9c8b2e5 commit 6bddb44
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions bin/cli.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { pathToFileURL } from 'url'
import {
type Client,
type ClientConfiguration,
type Item,
type Mention,
type Range,
createClient,
Expand All @@ -19,6 +20,7 @@ function usageFatal(message: string): never {
'meta [providerUri]',
'mentions <query> [providerUri]',
'items <message> [providerUri] [mentionJSON]',
'mention-items <query> [mentionIndex] [providerUri]',
]
for (const subcommand of subcommands) {
console.error(` ${subcommand}`)
Expand Down Expand Up @@ -80,7 +82,22 @@ async function subcommandItems(client: Client<Range>, args: string[]): Promise<v
}

const items = await client.items({ message, mention }, providerUri)
printItems(items)
}

async function subcommandMentionItems(client: Client<Range>, args: string[]): Promise<void> {
if (args.length === 0 || args.length > 3) {
usageFatal('Error: invalid args')
}
const [query, itemIndex, providerUri] = args

const mentions = await client.mentions({ query }, providerUri)
const mention = mentions[parseInt(itemIndex ?? 0)]
const items = await client.items({ mention }, mention.providerUri)
printItems(items)
}

function printItems(items: Item[]): void {
if (process.env.OUTPUT_JSON) {
console.log(JSON.stringify(items, null, 2))
} else {
Expand Down Expand Up @@ -163,6 +180,11 @@ switch (subcommand) {
case 'items':
await subcommandItems(client, args)
break
case 'mention-items':
await subcommandMentionItems(client, args)
break
default:
usageFatal(`Error: unrecognized subcommand ${JSON.stringify(subcommand)}`)
}

process.exit(0)

0 comments on commit 6bddb44

Please sign in to comment.