forked from janhq/jan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: introduce inference tools (janhq#2493)
- Loading branch information
Showing
15 changed files
with
240 additions
and
193 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,2 @@ | ||
export * from './manager' | ||
export * from './tool' |
This file contains 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,47 @@ | ||
import { AssistantTool, MessageRequest } from '../../types' | ||
import { InferenceTool } from './tool' | ||
|
||
/** | ||
* Manages the registration and retrieval of inference tools. | ||
*/ | ||
export class ToolManager { | ||
public tools = new Map<string, InferenceTool>() | ||
|
||
/** | ||
* Registers a tool. | ||
* @param tool - The tool to register. | ||
*/ | ||
register<T extends InferenceTool>(tool: T) { | ||
this.tools.set(tool.name, tool) | ||
} | ||
|
||
/** | ||
* Retrieves a tool by it's name. | ||
* @param name - The name of the tool to retrieve. | ||
* @returns The tool, if found. | ||
*/ | ||
get<T extends InferenceTool>(name: string): T | undefined { | ||
return this.tools.get(name) as T | undefined | ||
} | ||
|
||
/* | ||
** Process the message request with the tools. | ||
*/ | ||
process(request: MessageRequest, tools: AssistantTool[]): Promise<MessageRequest> { | ||
return tools.reduce((prevPromise, currentTool) => { | ||
return prevPromise.then((prevResult) => { | ||
return currentTool.enabled | ||
? this.get(currentTool.type)?.process(prevResult, currentTool) ?? | ||
Promise.resolve(prevResult) | ||
: Promise.resolve(prevResult) | ||
}) | ||
}, Promise.resolve(request)) | ||
} | ||
|
||
/** | ||
* The instance of the tool manager. | ||
*/ | ||
static instance(): ToolManager { | ||
return (window.core?.toolManager as ToolManager) ?? new ToolManager() | ||
} | ||
} |
This file contains 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 @@ | ||
import { AssistantTool, MessageRequest } from '../../types' | ||
|
||
/** | ||
* Represents a base inference tool. | ||
*/ | ||
export abstract class InferenceTool { | ||
abstract name: string | ||
/* | ||
** Process a message request and return the processed message request. | ||
*/ | ||
abstract process(request: MessageRequest, tool?: AssistantTool): Promise<MessageRequest> | ||
} |
This file contains 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 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.