TidGi-Desktop/src/services/agentInstance/tools/workspacesList.ts
lin onetwo 8a84d9b468
rename: handler -> agent framework; plugin -> tool
rename: handler -> agent framework; plugin -> tool

lint

refactor: more rename

further rename
2025-11-27 15:33:30 +08:00

139 lines
4.7 KiB
TypeScript

/**
* Workspaces List plugin
* Handles injection of available wiki workspaces list into prompts
*/
import { identity } from 'lodash';
import { z } from 'zod/v4';
const t = identity;
/**
* Workspaces List Parameter Schema
* Configuration parameters for the workspaces list plugin
*/
export const WorkspacesListParameterSchema = z.object({
targetId: z.string().meta({
title: t('Schema.WorkspacesList.TargetIdTitle'),
description: t('Schema.WorkspacesList.TargetId'),
}),
position: z.enum(['before', 'after']).meta({
title: t('Schema.WorkspacesList.PositionTitle'),
description: t('Schema.WorkspacesList.Position'),
}),
}).meta({
title: t('Schema.WorkspacesList.Title'),
description: t('Schema.WorkspacesList.Description'),
});
/**
* Type definition for workspaces list parameters
*/
export type WorkspacesListParameter = z.infer<typeof WorkspacesListParameterSchema>;
/**
* Get the workspaces list parameter schema
* @returns The schema for workspaces list parameters
*/
export function getWorkspacesListParameterSchema() {
return WorkspacesListParameterSchema;
}
import { container } from '@services/container';
import { logger } from '@services/libs/log';
import serviceIdentifier from '@services/serviceIdentifier';
import type { IWorkspaceService } from '@services/workspaces/interface';
import { isWikiWorkspace } from '@services/workspaces/interface';
import { findPromptById } from '../promptConcat/promptConcat';
import type { PromptConcatTool } from './types';
/**
* Workspaces List plugin - Prompt processing
* Handles injection of available wiki workspaces list
*/
export const workspacesListTool: PromptConcatTool = (hooks) => {
// Tool list injection
hooks.processPrompts.tapAsync('workspacesListTool-injection', async (context, callback) => {
const { toolConfig, prompts } = context;
if (toolConfig.toolId !== 'workspacesList' || !toolConfig.workspacesListParam) {
callback();
return;
}
const workspacesListParameter = toolConfig.workspacesListParam;
try {
// Handle workspaces list injection if targetId is configured
if (workspacesListParameter?.targetId) {
const target = findPromptById(prompts, workspacesListParameter.targetId);
if (!target) {
logger.warn('Workspaces list target prompt not found', {
targetId: workspacesListParameter.targetId,
toolId: toolConfig.id,
});
callback();
return;
}
// Get available wikis
const workspaceService = container.get<IWorkspaceService>(serviceIdentifier.Workspace);
const workspaces = await workspaceService.getWorkspacesAsList();
const wikiWorkspaces = workspaces.filter(isWikiWorkspace);
if (wikiWorkspaces.length > 0) {
// Use fixed list format for simplicity
const workspacesList = wikiWorkspaces
.map(workspace => `- ${workspace.name} (ID: ${workspace.id})`)
.join('\n');
const workspacesListContent = `Available Wiki Workspaces:\n${workspacesList}`;
// Insert the workspaces list content based on position
if (workspacesListParameter.position === 'after') {
if (!target.prompt.children) {
target.prompt.children = [];
}
const insertIndex = target.prompt.children.length;
target.prompt.children.splice(insertIndex, 0, {
id: `workspaces-list-${toolConfig.id}`,
caption: 'Available Workspaces',
text: workspacesListContent,
});
} else if (workspacesListParameter.position === 'before') {
if (!target.prompt.children) {
target.prompt.children = [];
}
target.prompt.children.unshift({
id: `workspaces-list-${toolConfig.id}`,
caption: 'Available Workspaces',
text: workspacesListContent,
});
} else {
// Default to appending text
target.prompt.text = (target.prompt.text || '') + '\n' + workspacesListContent;
}
logger.debug('Workspaces list injected successfully', {
targetId: workspacesListParameter.targetId,
position: workspacesListParameter.position,
toolId: toolConfig.id,
workspaceCount: wikiWorkspaces.length,
});
} else {
logger.debug('No wiki workspaces found to inject', {
toolId: toolConfig.id,
});
}
}
callback();
} catch (error) {
logger.error('Error in workspaces list injection', {
error,
toolId: toolConfig.id,
});
callback();
}
});
};