|
1 | | -import { WorkspaceAccessor } from '@sillsdev/lynx'; |
| 1 | +import { Document, DocumentAccessor, WorkspaceAccessor } from '@sillsdev/lynx'; |
2 | 2 | import { tool } from 'langchain'; |
3 | 3 | import { z } from 'zod'; |
4 | 4 |
|
| 5 | +import { DeepAgentToolDefinition } from './deep-agent-tool'; |
| 6 | + |
5 | 7 | const diagnosticSchema = z.object({ |
6 | 8 | code: z.union([z.string(), z.number()]), |
7 | 9 | source: z.string(), |
@@ -38,7 +40,8 @@ export function createWorkspaceTools<T>(workspace: WorkspaceAccessor<T>) { |
38 | 40 | }, |
39 | 41 | { |
40 | 42 | name: 'get_diagnostic_actions', |
41 | | - description: 'Get available fix actions for a specific diagnostic issue in a document', |
| 43 | + description: |
| 44 | + 'Get available fix actions for a specific diagnostic issue in a document. An action will either contain a `command` to execute by calling `execute_diagnostic_command` or `edits` to apply directly to the document.', |
42 | 45 | schema: z.object({ |
43 | 46 | uri: z.string().describe('The document URI'), |
44 | 47 | diagnostic: diagnosticSchema.describe('The diagnostic to get actions for'), |
@@ -76,3 +79,31 @@ export function createWorkspaceTools<T>(workspace: WorkspaceAccessor<T>) { |
76 | 79 | ), |
77 | 80 | ]; |
78 | 81 | } |
| 82 | + |
| 83 | +export function createDocumentAccessorTools<T extends Document = Document>( |
| 84 | + documents: DocumentAccessor<T>, |
| 85 | +): DeepAgentToolDefinition[] { |
| 86 | + return [ |
| 87 | + { |
| 88 | + name: 'get_active_documents', |
| 89 | + description: 'Returns a list of all documents currently open in the workspace with their URIs and formats.', |
| 90 | + schema: z.object({}), |
| 91 | + async execute(_args: unknown) { |
| 92 | + const docs = await documents.active(); |
| 93 | + return JSON.stringify(docs.map((doc) => ({ uri: doc.uri, format: doc.format }))); |
| 94 | + }, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: 'get_document', |
| 98 | + description: 'Returns the URI, format, version, and full text content of a workspace document by URI.', |
| 99 | + schema: z.object({ uri: z.string().describe('The URI of the document to retrieve.') }), |
| 100 | + async execute({ uri }: { uri: string }) { |
| 101 | + const doc = await documents.get(uri); |
| 102 | + if (doc == null) { |
| 103 | + return JSON.stringify({ error: `Document not found: ${uri}` }); |
| 104 | + } |
| 105 | + return JSON.stringify({ uri: doc.uri, format: doc.format, version: doc.version, content: doc.getText() }); |
| 106 | + }, |
| 107 | + }, |
| 108 | + ]; |
| 109 | +} |
0 commit comments