@minded-ai/mindedjs 1.0.103 → 1.0.105

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/dist/agent.d.ts +1 -0
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +21 -5
  4. package/dist/agent.js.map +1 -1
  5. package/dist/cli/lambdaHandlerTemplate.d.ts.map +1 -1
  6. package/dist/cli/lambdaHandlerTemplate.js +0 -1
  7. package/dist/cli/lambdaHandlerTemplate.js.map +1 -1
  8. package/dist/cli/lambdaHandlerTemplate.ts +0 -1
  9. package/dist/index.d.ts +3 -0
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +40 -1
  12. package/dist/index.js.map +1 -1
  13. package/dist/internalTools/appActionRunnerTool.d.ts +0 -1
  14. package/dist/internalTools/appActionRunnerTool.d.ts.map +1 -1
  15. package/dist/internalTools/appActionRunnerTool.js +0 -7
  16. package/dist/internalTools/appActionRunnerTool.js.map +1 -1
  17. package/dist/internalTools/documentExtraction/documentExtraction.d.ts +129 -0
  18. package/dist/internalTools/documentExtraction/documentExtraction.d.ts.map +1 -0
  19. package/dist/internalTools/documentExtraction/documentExtraction.js +788 -0
  20. package/dist/internalTools/documentExtraction/documentExtraction.js.map +1 -0
  21. package/dist/internalTools/documentExtraction/types.d.ts +29 -0
  22. package/dist/internalTools/documentExtraction/types.d.ts.map +1 -0
  23. package/dist/internalTools/documentExtraction/types.js +35 -0
  24. package/dist/internalTools/documentExtraction/types.js.map +1 -0
  25. package/dist/internalTools/libraryActionRunnerTool.d.ts +4 -0
  26. package/dist/internalTools/libraryActionRunnerTool.d.ts.map +1 -0
  27. package/dist/internalTools/libraryActionRunnerTool.js +54 -0
  28. package/dist/internalTools/libraryActionRunnerTool.js.map +1 -0
  29. package/dist/nodes/addAppToolNode.d.ts +3 -1
  30. package/dist/nodes/addAppToolNode.d.ts.map +1 -1
  31. package/dist/nodes/addAppToolNode.js +8 -4
  32. package/dist/nodes/addAppToolNode.js.map +1 -1
  33. package/dist/nodes/addToolNode.d.ts.map +1 -1
  34. package/dist/nodes/addToolNode.js +0 -1
  35. package/dist/nodes/addToolNode.js.map +1 -1
  36. package/dist/nodes/nodeFactory.js +1 -1
  37. package/dist/nodes/nodeFactory.js.map +1 -1
  38. package/dist/platform/utils/parseAttachments.d.ts +14 -0
  39. package/dist/platform/utils/parseAttachments.d.ts.map +1 -0
  40. package/dist/platform/utils/parseAttachments.js +54 -0
  41. package/dist/platform/utils/parseAttachments.js.map +1 -0
  42. package/dist/toolsLibrary/index.d.ts +5 -0
  43. package/dist/toolsLibrary/index.d.ts.map +1 -0
  44. package/dist/toolsLibrary/index.js +42 -0
  45. package/dist/toolsLibrary/index.js.map +1 -0
  46. package/dist/toolsLibrary/parseDocument.d.ts +30 -0
  47. package/dist/toolsLibrary/parseDocument.d.ts.map +1 -0
  48. package/dist/toolsLibrary/parseDocument.js +127 -0
  49. package/dist/toolsLibrary/parseDocument.js.map +1 -0
  50. package/docs/SUMMARY.md +1 -0
  51. package/docs/getting-started/installation.md +0 -42
  52. package/docs/tooling/document-processing.md +389 -0
  53. package/package.json +5 -1
  54. package/src/agent.ts +44 -14
  55. package/src/cli/lambdaHandlerTemplate.ts +0 -1
  56. package/src/index.ts +12 -0
  57. package/src/internalTools/appActionRunnerTool.ts +0 -7
  58. package/src/internalTools/documentExtraction/documentExtraction.ts +861 -0
  59. package/src/internalTools/documentExtraction/types.ts +59 -0
  60. package/src/internalTools/libraryActionRunnerTool.ts +63 -0
  61. package/src/nodes/addAppToolNode.ts +13 -3
  62. package/src/nodes/addToolNode.ts +0 -1
  63. package/src/nodes/nodeFactory.ts +2 -2
  64. package/src/platform/utils/parseAttachments.ts +56 -0
  65. package/src/toolsLibrary/index.ts +6 -0
  66. package/src/toolsLibrary/parseDocument.ts +136 -0
@@ -0,0 +1,59 @@
1
+ import { ZodType as ZodSchema, ZodTypeAny } from 'zod';
2
+
3
+ export interface DocumentProcessorConfig {
4
+ llamaCloudApiKey?: string;
5
+ useBase64?: boolean;
6
+ maxImageWidth?: number;
7
+ imageQuality?: number;
8
+ }
9
+ export interface DocumentExtractionOptions {
10
+ documentPath?: string;
11
+ documentContent?: Buffer | string;
12
+ documentUrl?: string;
13
+ schema?: ZodSchema<any> | ZodTypeAny;
14
+ llmConfig?: {
15
+ model?: string;
16
+ temperature?: number;
17
+ };
18
+ systemPrompt?: string;
19
+ }
20
+ export interface DocumentProcessingResult<T = any> {
21
+ data: T;
22
+ metadata: {
23
+ fileSize?: number;
24
+ fileType: string;
25
+ processingTime: number;
26
+ contentLength: number;
27
+ };
28
+ }
29
+ export const SUPPORTED_DOCUMENT_TYPES = [
30
+ // Images
31
+ '.jpg',
32
+ '.jpeg',
33
+ '.png',
34
+ '.gif',
35
+ '.bmp',
36
+ '.webp',
37
+ '.tiff',
38
+ // Documents
39
+ '.pdf',
40
+ '.doc',
41
+ '.docx',
42
+ '.txt',
43
+ '.rtf',
44
+ '.odt',
45
+ // Spreadsheets
46
+ '.xls',
47
+ '.xlsx',
48
+ '.csv',
49
+ '.ods',
50
+ // Presentations
51
+ '.ppt',
52
+ '.pptx',
53
+ '.odp',
54
+ // Other formats
55
+ '.html',
56
+ '.htm',
57
+ '.md',
58
+ '.xml',
59
+ ];
@@ -0,0 +1,63 @@
1
+ import { Tool, ToolExecuteInput } from '../types/Tools.types';
2
+ import { tools } from '../toolsLibrary';
3
+ import { logger } from '../utils/logger';
4
+
5
+ const libraryActionRunnerToolCreator = (actionKey: string, nodeTitle: string): Tool<any, any> => {
6
+ // Find the corresponding library tool by key name (e.g., "parseDocument")
7
+ const libraryTool = (tools as any)[actionKey];
8
+
9
+ if (!libraryTool?.default) {
10
+ throw new Error(`Library tool '${actionKey}' not found in toolsLibrary`);
11
+ }
12
+
13
+ if (!libraryTool.schema) {
14
+ throw new Error(`Schema not found for library tool '${actionKey}'. Make sure the tool exports a 'schema' object.`);
15
+ }
16
+
17
+ const zodSchema = libraryTool.schema;
18
+
19
+ // Create a description using the library tool's own description
20
+ const description = libraryTool.default.description || `Run ${nodeTitle} action from Minded Tools library.`;
21
+
22
+ const tool = {
23
+ name: nodeTitle,
24
+ description,
25
+ input: zodSchema,
26
+ execute: async ({ input, state, agent }: ToolExecuteInput<typeof zodSchema>) => {
27
+ try {
28
+ // Find the corresponding library tool
29
+ const libraryTool = Object.values(tools).find((toolModule) => toolModule.default && toolModule.default.name === actionKey);
30
+
31
+ if (!libraryTool?.default) {
32
+ throw new Error(`Library tool '${actionKey}' not found in toolsLibrary`);
33
+ }
34
+
35
+ logger.debug({
36
+ msg: `[Tool] Executing library tool`,
37
+ toolName: actionKey,
38
+ nodeTitle,
39
+ });
40
+
41
+ // Execute the library tool directly
42
+ const result = await libraryTool.default.execute({ input, state, agent });
43
+
44
+ return {
45
+ state: result?.state,
46
+ result: result?.result,
47
+ };
48
+ } catch (error) {
49
+ logger.error({
50
+ msg: `[Tool] Error executing library tool`,
51
+ error,
52
+ toolName: actionKey,
53
+ nodeTitle,
54
+ });
55
+ throw error;
56
+ }
57
+ },
58
+ };
59
+
60
+ return tool;
61
+ };
62
+
63
+ export default libraryActionRunnerToolCreator;
@@ -5,26 +5,35 @@ import { SystemMessage } from '@langchain/core/messages';
5
5
  import { RunnableLike } from '@langchain/core/runnables';
6
6
  import { z } from 'zod';
7
7
  import { LLMProviders } from '../types/LLM.types';
8
- import { getAppActionRunnerTool } from '../internalTools/appActionRunnerTool';
9
8
  import { AppActionInvocationHistoryStep } from '../types/Agent.types';
10
9
  import { Agent } from '../agent';
11
10
  import { logger } from '../utils/logger';
12
11
  import { compilePlaybooks } from '../playbooks/playbooks';
13
12
  import { createHistoryStep } from '../utils/history';
13
+ import { Tool } from '../types/Tools.types';
14
14
 
15
15
  export const addAppToolNode = async ({
16
16
  graph,
17
17
  node,
18
18
  llm,
19
19
  agent,
20
+ tools,
20
21
  }: {
21
22
  graph: PreCompiledGraph;
22
23
  node: AppToolNode;
23
24
  llm: (typeof LLMProviders)[keyof typeof LLMProviders];
24
25
  agent: Agent;
26
+ tools: Tool<any, any>[];
25
27
  }) => {
26
28
  const cleanedParameters = Object.fromEntries(Object.entries(node.parameters || {}).filter(([, value]) => value !== ''));
27
- const appRunnerTool = getAppActionRunnerTool(node.displayName!);
29
+
30
+ // Find the tool from the tools array by name
31
+ const appRunnerTool = tools.find((tool) => tool.name === node.displayName);
32
+
33
+ if (!appRunnerTool) {
34
+ throw new Error(`Tool not found for node: ${node.displayName}`);
35
+ }
36
+
28
37
  const callback: RunnableLike = async (state: typeof stateAnnotation.State) => {
29
38
  logger.debug({ msg: `[Node] Executing tool node`, node: appRunnerTool.name });
30
39
 
@@ -45,6 +54,7 @@ export const addAppToolNode = async ({
45
54
  // Get compiled playbooks with proper parameters
46
55
  const playbookParams = {
47
56
  ...state.memory, // Spread memory fields at the top level
57
+ currentTime: new Date().toISOString(),
48
58
  };
49
59
  const compiledPlaybooks = compilePlaybooks(agent.playbooks, playbookParams) || '';
50
60
 
@@ -76,7 +86,7 @@ export const addAppToolNode = async ({
76
86
  const toolCallMessage = await tool.invoke(AIToolCallMessage.tool_calls[0]);
77
87
  AIToolCallMessage.additional_kwargs = {
78
88
  mindedMetadata: {
79
- nodeType: NodeType.APP_TOOL
89
+ nodeType: NodeType.APP_TOOL,
80
90
  },
81
91
  };
82
92
  return {
@@ -41,7 +41,6 @@ export const addToolNode = async ({
41
41
  // Get compiled playbooks with proper parameters
42
42
  const playbookParams = {
43
43
  ...state.memory, // Spread memory fields at the top level
44
- state, // Keep the full state for backward compatibility
45
44
  currentTime: new Date().toISOString(),
46
45
  // Add any other common parameters that playbooks might need
47
46
  };
@@ -40,7 +40,7 @@ export const nodeFactory = ({
40
40
  addJunctionNode({ graph, node });
41
41
  break;
42
42
  case NodeType.APP_TOOL:
43
- addAppToolNode({ graph, node, llm, agent });
43
+ addAppToolNode({ graph, node, llm, agent, tools });
44
44
  break;
45
45
  case NodeType.PROMPT_NODE:
46
46
  addPromptNode({ graph, node, tools, llm, emit, agent });
@@ -54,4 +54,4 @@ export const nodeFactory = ({
54
54
  default:
55
55
  throw new Error(`Unsupported node type: ${nodeType}`);
56
56
  }
57
- };
57
+ };
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Parses attachments from a trigger body and formats them as a string
3
+ * @param triggerBody - The trigger body containing potential attachments
4
+ * @returns Formatted string of attachments or empty string if no attachments
5
+ */
6
+ export const parseAttachments = (triggerBody: any): string => {
7
+ // Check if triggerBody has attachments in meta
8
+ const attachments = triggerBody?.meta?.attachments;
9
+
10
+ if (!attachments || !Array.isArray(attachments) || attachments.length === 0) {
11
+ return '';
12
+ }
13
+
14
+ // Format attachments
15
+ const attachmentStrings = attachments
16
+ .map((attachment: any) => {
17
+ const lines: string[] = [];
18
+
19
+ if (attachment.originalname) {
20
+ lines.push(`originalName: ${attachment.originalname}`);
21
+ } else if (attachment.name) {
22
+ lines.push(`originalName: ${attachment.name}`);
23
+ }
24
+
25
+ if (attachment.path) {
26
+ lines.push(`localPath: ${attachment.path}`);
27
+ }
28
+
29
+ return lines.join('\n');
30
+ })
31
+ .filter((str) => str.length > 0);
32
+
33
+ if (attachmentStrings.length === 0) {
34
+ return '';
35
+ }
36
+
37
+ return 'Attachments:\n' + attachmentStrings.join('\n\n');
38
+ };
39
+
40
+ /**
41
+ * Combines content with attachments string
42
+ * @param content - The main content string
43
+ * @param attachmentsString - The formatted attachments string
44
+ * @returns Combined content with attachments
45
+ */
46
+ export const combineContentWithAttachments = (content: string, attachmentsString: string): string => {
47
+ if (!attachmentsString) {
48
+ return content;
49
+ }
50
+
51
+ if (!content) {
52
+ return attachmentsString;
53
+ }
54
+
55
+ return `${content}\n${attachmentsString}`;
56
+ };
@@ -0,0 +1,6 @@
1
+ import * as parseDocument from './parseDocument';
2
+
3
+ // Export all tools as a collection for easy discovery
4
+ export const tools = {
5
+ 'minded-parse-documents': parseDocument,
6
+ };
@@ -0,0 +1,136 @@
1
+ import { z } from 'zod';
2
+ import { Tool } from '../types/Tools.types';
3
+ import { extractFromDocument } from '../internalTools/documentExtraction/documentExtraction';
4
+ import { logger } from '../utils/logger';
5
+
6
+ // Schema for the tool - these are the parameters that can be inferred by LLM
7
+ export const schema = z.object({
8
+ // Document source parameters - only one should be provided based on loadFrom setting
9
+ documentPath: z.string().optional().nullable().describe('Path to the document file to parse (when loadFrom is "path")'),
10
+ documentContent: z
11
+ .union([z.instanceof(Buffer), z.string()])
12
+ .optional()
13
+ .nullable()
14
+ .describe('Document content as Buffer or string (when loadFrom is "buffer" or "string")'),
15
+ documentUrl: z.string().optional().nullable().describe('URL to fetch the document from (when loadFrom is "url")'),
16
+
17
+ // Processing parameters - these can be predefined in the UI
18
+ extractRaw: z.boolean().optional().nullable().describe('Extract raw text without AI processing'),
19
+ schema: z.any().optional().nullable().describe('Zod schema for structured data extraction (when not extracting raw)'),
20
+
21
+ // Load source indicator - this would be set from UI
22
+ loadFrom: z.enum(['url', 'path', 'buffer', 'string']).optional().nullable().describe('Source type for the document'),
23
+ systemPrompt: z.string().optional().nullable().describe('Prompt for guiding extraction (when not using schema or extracting raw)'),
24
+ });
25
+
26
+ const parseDocumentTool: Tool<typeof schema, any> = {
27
+ name: 'minded-parse-documents',
28
+ description:
29
+ 'Parse and extract data from documents (PDFs, images, Word docs, etc.). Supports multiple input sources (URL, file path, buffer, or string) and can extract raw text, structured data with a schema, or unstructured data with a prompt. Parameters can be predefined in the flow configuration.',
30
+ input: schema,
31
+ isGlobal: false,
32
+ execute: async ({ input, state, agent }) => {
33
+ // The input here will be the combined input from both LLM and UI properties
34
+ const combinedInput = input as z.infer<typeof schema>;
35
+
36
+ logger.info({
37
+ msg: '*Action: Parse document*',
38
+ sessionId: state.sessionId,
39
+ loadFrom: combinedInput.loadFrom,
40
+ hasPath: !!combinedInput.documentPath,
41
+ hasContent: !!combinedInput.documentContent,
42
+ hasUrl: !!combinedInput.documentUrl,
43
+ hasSchema: !!combinedInput.schema,
44
+ hasPrompt: !!combinedInput.systemPrompt,
45
+ extractRaw: combinedInput.extractRaw,
46
+ });
47
+
48
+ try {
49
+ // Validate document source based on loadFrom parameter
50
+ if (combinedInput.loadFrom) {
51
+ switch (combinedInput.loadFrom) {
52
+ case 'url':
53
+ if (!combinedInput.documentUrl) {
54
+ throw new Error('documentUrl is required when loadFrom is "url"');
55
+ }
56
+ break;
57
+ case 'path':
58
+ if (!combinedInput.documentPath) {
59
+ throw new Error('documentPath is required when loadFrom is "path"');
60
+ }
61
+ break;
62
+ case 'buffer':
63
+ case 'string':
64
+ if (!combinedInput.documentContent) {
65
+ throw new Error('documentContent is required when loadFrom is "buffer" or "string"');
66
+ }
67
+ break;
68
+ }
69
+ } else {
70
+ // Fallback to original validation if loadFrom is not specified
71
+ if (!combinedInput.documentPath && !combinedInput.documentContent && !combinedInput.documentUrl) {
72
+ throw new Error('At least one document source must be provided: documentPath, documentContent, or documentUrl');
73
+ }
74
+ }
75
+
76
+ // Prepare extraction options, filtering out null/undefined values
77
+ const extractionOptions: Parameters<typeof extractFromDocument>[0] = {};
78
+
79
+ if (combinedInput.documentPath) {
80
+ extractionOptions.documentPath = combinedInput.documentPath;
81
+ }
82
+ if (combinedInput.documentContent) {
83
+ extractionOptions.documentContent = combinedInput.documentContent;
84
+ }
85
+ if (combinedInput.documentUrl) {
86
+ extractionOptions.documentUrl = combinedInput.documentUrl;
87
+ }
88
+
89
+ // Only include LLM if not extracting raw text
90
+ if (!combinedInput.extractRaw) {
91
+ extractionOptions.llm = agent.llm;
92
+
93
+ // Include schema or system prompt if provided
94
+ if (combinedInput.schema) {
95
+ extractionOptions.schema = combinedInput.schema;
96
+ } else if (combinedInput.systemPrompt) {
97
+ extractionOptions.systemPrompt = combinedInput.systemPrompt;
98
+ }
99
+ }
100
+
101
+ // Extract from document using the SDK's document extraction capabilities
102
+ const result = await extractFromDocument(extractionOptions);
103
+
104
+ // Return only the extracted data, not the metadata as requested
105
+ return {
106
+ result: result.data,
107
+ state: {
108
+ memory: {
109
+ lastParsedDocument: {
110
+ source: combinedInput.documentPath || combinedInput.documentUrl || 'content',
111
+ extractedAt: new Date().toISOString(),
112
+ extractedRaw: !!combinedInput.extractRaw,
113
+ },
114
+ },
115
+ },
116
+ };
117
+ } catch (error) {
118
+ logger.error({
119
+ msg: 'Failed to parse document',
120
+ sessionId: state.sessionId,
121
+ error: error instanceof Error ? error.message : String(error),
122
+ });
123
+
124
+ return {
125
+ result: `Failed to parse document: ${error instanceof Error ? error.message : String(error)}`,
126
+ state: {
127
+ memory: {
128
+ documentParsingError: error instanceof Error ? error.message : String(error),
129
+ },
130
+ },
131
+ };
132
+ }
133
+ },
134
+ };
135
+
136
+ export default parseDocumentTool;