mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-12-25 13:08:15 +00:00
102 lines
2.4 KiB
TypeScript
102 lines
2.4 KiB
TypeScript
import { Tool, ToolCall } from '@/lib/models/types';
|
|
import {
|
|
ActionOutput,
|
|
AdditionalConfig,
|
|
ClassifierOutput,
|
|
ResearchAction,
|
|
SearchAgentConfig,
|
|
} from '../../types';
|
|
|
|
class ActionRegistry {
|
|
private static actions: Map<string, ResearchAction> = new Map();
|
|
|
|
static register(action: ResearchAction<any>) {
|
|
this.actions.set(action.name, action);
|
|
}
|
|
|
|
static get(name: string): ResearchAction | undefined {
|
|
return this.actions.get(name);
|
|
}
|
|
|
|
static getAvailableActions(config: {
|
|
classification: ClassifierOutput;
|
|
fileIds: string[];
|
|
mode: SearchAgentConfig['mode'];
|
|
}): ResearchAction[] {
|
|
return Array.from(
|
|
this.actions.values().filter((action) => action.enabled(config)),
|
|
);
|
|
}
|
|
|
|
static getAvailableActionTools(config: {
|
|
classification: ClassifierOutput;
|
|
fileIds: string[];
|
|
mode: SearchAgentConfig['mode'];
|
|
}): Tool[] {
|
|
const availableActions = this.getAvailableActions(config);
|
|
|
|
return availableActions.map((action) => ({
|
|
name: action.name,
|
|
description: action.getToolDescription({ mode: config.mode }),
|
|
schema: action.schema,
|
|
}));
|
|
}
|
|
|
|
static getAvailableActionsDescriptions(config: {
|
|
classification: ClassifierOutput;
|
|
fileIds: string[];
|
|
mode: SearchAgentConfig['mode'];
|
|
}): string {
|
|
const availableActions = this.getAvailableActions(config);
|
|
|
|
return availableActions
|
|
.map(
|
|
(action) =>
|
|
`<tool name="${action.name}">\n${action.getDescription({ mode: config.mode })}\n</tool>`,
|
|
)
|
|
.join('\n\n');
|
|
}
|
|
|
|
static async execute(
|
|
name: string,
|
|
params: any,
|
|
additionalConfig: AdditionalConfig & {
|
|
researchBlockId: string;
|
|
fileIds: string[];
|
|
},
|
|
) {
|
|
const action = this.actions.get(name);
|
|
|
|
if (!action) {
|
|
throw new Error(`Action with name ${name} not found`);
|
|
}
|
|
|
|
return action.execute(params, additionalConfig);
|
|
}
|
|
|
|
static async executeAll(
|
|
actions: ToolCall[],
|
|
additionalConfig: AdditionalConfig & {
|
|
researchBlockId: string;
|
|
fileIds: string[];
|
|
},
|
|
): Promise<ActionOutput[]> {
|
|
const results: ActionOutput[] = [];
|
|
|
|
await Promise.all(
|
|
actions.map(async (actionConfig) => {
|
|
const output = await this.execute(
|
|
actionConfig.name,
|
|
actionConfig.arguments,
|
|
additionalConfig,
|
|
);
|
|
results.push(output);
|
|
}),
|
|
);
|
|
|
|
return results;
|
|
}
|
|
}
|
|
|
|
export default ActionRegistry;
|