1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
import { BaseTool } from './BaseTool';
import { Observation } from '../types/types';
import { ParametersType, ToolInfo } from '../types/tool_types';
import { AgentDocumentManager } from '../utils/AgentDocumentManager';
import { gptAPICall, GPTCallType } from '../../../../apis/gpt/GPT';
import { ChatSortField } from '../../../collections/CollectionSubView';
import { v4 as uuidv4 } from 'uuid';
const parameterRules = [
{
name: 'sortCriteria',
type: 'string',
description: 'Criteria provided by the user to sort the documents.',
required: true,
},
] as const;
const toolInfo: ToolInfo<typeof parameterRules> = {
name: 'sortDocs',
description:
'Sorts documents within the current Dash environment based on user-specified criteria. Provide clear sorting criteria, such as by date, title, relevance, or custom metadata fields.',
parameterRules,
citationRules: 'No citation needed for sorting operations.',
};
export class SortDocsTool extends BaseTool<typeof parameterRules> {
private _docManager: AgentDocumentManager;
constructor(docManager: AgentDocumentManager) {
super(toolInfo);
this._docManager = docManager;
this._docManager.initializeFindDocsFreeform();
}
async execute(args: ParametersType<typeof parameterRules>): Promise<Observation[]> {
const chunkId = uuidv4();
try {
const docs = this._docManager.docIds.map((id) => this._docManager.extractDocumentMetadata(id));
const descriptions = docs
.filter((doc): doc is NonNullable<typeof doc> => doc !== null)
.map(
(doc) => `${doc.id}: ${doc.title} - ${doc.fields.layout.summary || ''}`
)
.join('\n');
const sortedIdsResponse = await gptAPICall(args.sortCriteria, GPTCallType.SORTDOCS, descriptions);
const sortedIds = sortedIdsResponse.trim().split('\n');
console.log(sortedIdsResponse);
console.log(sortedIds);
sortedIds.forEach((id, index) => {
this._docManager.editDocumentField(id, ChatSortField, index);
});
return [{
type: 'text',
text: `<chunk chunk_id="${chunkId}" chunk_type="sort_status">Successfully sorted ${sortedIds.length} documents based on "${args.sortCriteria}".</chunk>`,
}];
} catch (error) {
return [{
type: 'text',
text: `<chunk chunk_id="${chunkId}" chunk_type="error">Sorting failed: ${error instanceof Error ? error.message : String(error)}</chunk>`,
}];
}
}
}
|