-
-
Notifications
You must be signed in to change notification settings - Fork 14
Define a new framework for chat commands #161
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
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6ae1a80
minimal working example
dlqqq b397f2b
Automatic application of license header
github-actions[bot] cb1bfc3
move use-chat-commands.ts
dlqqq 04aea09
revise ChatCommand and IChatCommandProvider types
dlqqq 571b3df
simplify code by using functions that act on words
dlqqq 985b14e
clean up examples
dlqqq d54bb3b
Automatic application of license header
github-actions[bot] e7c4456
do not call handleChatCommand() if replaceWith is set
dlqqq 6b69ee1
fix bugs identified by @brichet
dlqqq c46c974
update regexes and add a space after emojis
dlqqq cd20e4f
close command menu on empty input
dlqqq 8ddf972
move registry to jupyter-chat
dlqqq 620c313
include tabs and newlines as word boundaries
dlqqq c9a4fcf
remove empty registry.ts file
dlqqq d63a0c0
remove demo slash command plugin
dlqqq 179dd4e
remove old autocompletion tests
dlqqq 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
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,7 @@ | ||
/* | ||
* Copyright (c) Jupyter Development Team. | ||
* Distributed under the terms of the Modified BSD License. | ||
*/ | ||
|
||
export * from './types'; | ||
export * from './registry'; |
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,67 @@ | ||
/* | ||
* Copyright (c) Jupyter Development Team. | ||
* Distributed under the terms of the Modified BSD License. | ||
*/ | ||
|
||
import { Token } from '@lumino/coreutils'; | ||
import { ChatCommand, IChatCommandProvider } from './types'; | ||
|
||
/** | ||
* Interface of a chat command registry, which tracks a list of chat command | ||
* providers. Providers provide a list of commands given a user's partial input, | ||
* and define how commands are handled when accepted in the chat commands menu. | ||
*/ | ||
export interface IChatCommandRegistry { | ||
addProvider(provider: IChatCommandProvider): void; | ||
getProviders(): IChatCommandProvider[]; | ||
|
||
/** | ||
* Handles a chat command by calling `handleChatCommand()` on the provider | ||
* corresponding to this chat command. | ||
*/ | ||
handleChatCommand( | ||
command: ChatCommand, | ||
currentWord: string, | ||
replaceCurrentWord: (newWord: string) => void | ||
): void; | ||
} | ||
|
||
/** | ||
* Default chat command registry implementation. | ||
*/ | ||
export class ChatCommandRegistry implements IChatCommandRegistry { | ||
constructor() { | ||
this._providers = new Map<string, IChatCommandProvider>(); | ||
} | ||
|
||
addProvider(provider: IChatCommandProvider): void { | ||
this._providers.set(provider.id, provider); | ||
} | ||
|
||
getProviders(): IChatCommandProvider[] { | ||
return Array.from(this._providers.values()); | ||
} | ||
|
||
handleChatCommand( | ||
command: ChatCommand, | ||
currentWord: string, | ||
replaceCurrentWord: (newWord: string) => void | ||
) { | ||
const provider = this._providers.get(command.providerId); | ||
if (!provider) { | ||
console.error( | ||
'Error in handling chat command: No command provider has an ID of ' + | ||
command.providerId | ||
); | ||
return; | ||
} | ||
|
||
provider.handleChatCommand(command, currentWord, replaceCurrentWord); | ||
} | ||
|
||
private _providers: Map<string, IChatCommandProvider>; | ||
} | ||
|
||
export const IChatCommandRegistry = new Token<IChatCommandRegistry>( | ||
'@jupyter/chat:IChatCommandRegistry' | ||
); |
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,73 @@ | ||
/* | ||
* Copyright (c) Jupyter Development Team. | ||
* Distributed under the terms of the Modified BSD License. | ||
*/ | ||
|
||
import { LabIcon } from '@jupyterlab/ui-components'; | ||
|
||
export type ChatCommand = { | ||
/** | ||
* The name of the command. This defines what the user should type in the | ||
* input to have the command appear in the chat commands menu. | ||
*/ | ||
name: string; | ||
|
||
/** | ||
* ID of the provider the command originated from. | ||
*/ | ||
providerId: string; | ||
|
||
/** | ||
* If set, this will be rendered as the icon for the command in the chat | ||
* commands menu. Jupyter Chat will choose a default if this is unset. | ||
*/ | ||
icon?: LabIcon; | ||
|
||
/** | ||
* If set, this will be rendered as the description for the command in the | ||
* chat commands menu. Jupyter Chat will choose a default if this is unset. | ||
*/ | ||
description?: string; | ||
|
||
/** | ||
* If set, Jupyter Chat will replace the current word with this string after | ||
* the command is run from the chat commands menu. | ||
* | ||
* If all commands from a provider have this property set, then | ||
* `handleChatCommands()` can just return on the first line. | ||
*/ | ||
replaceWith?: string; | ||
}; | ||
|
||
/** | ||
* Interface of a command provider. | ||
*/ | ||
export interface IChatCommandProvider { | ||
/** | ||
* ID of this command provider. | ||
*/ | ||
id: string; | ||
|
||
/** | ||
* Async function which accepts the current word and returns a list of | ||
* valid chat commands that match the current word. The current word is | ||
* space-separated word at the user's cursor. | ||
* | ||
* TODO: Pass a ChatModel/InputModel instance here to give the command access | ||
* to more than the current word. | ||
*/ | ||
getChatCommands(currentWord: string): Promise<ChatCommand[]>; | ||
|
||
/** | ||
* Function called when a chat command is run by the user through the chat | ||
* commands menu. | ||
* | ||
* TODO: Pass a ChatModel/InputModel instance here to provide a function to | ||
* replace the current word. | ||
*/ | ||
handleChatCommand( | ||
command: ChatCommand, | ||
currentWord: string, | ||
replaceCurrentWord: (newWord: string) => void | ||
): Promise<void>; | ||
} |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.