@minded-ai/mindedjs 1.0.96 → 1.0.97-beta-1236

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 (77) hide show
  1. package/dist/browserTask/README.md +419 -0
  2. package/dist/browserTask/browserAgent.py +632 -0
  3. package/dist/browserTask/captcha_isolated.png +0 -0
  4. package/dist/browserTask/executeBrowserTask.d.ts +2 -0
  5. package/dist/browserTask/executeBrowserTask.d.ts.map +1 -0
  6. package/dist/browserTask/executeBrowserTask.js +78 -0
  7. package/dist/browserTask/executeBrowserTask.js.map +1 -0
  8. package/dist/browserTask/executeBrowserTask.ts +78 -0
  9. package/dist/browserTask/requirements.txt +8 -0
  10. package/dist/browserTask/setup.sh +144 -0
  11. package/dist/cli/index.js +103 -1
  12. package/dist/cli/index.js.map +1 -1
  13. package/dist/nodes/addAppToolNode.js +2 -2
  14. package/dist/nodes/addAppToolNode.js.map +1 -1
  15. package/dist/nodes/addBrowserTaskNode.d.ts +11 -0
  16. package/dist/nodes/addBrowserTaskNode.d.ts.map +1 -0
  17. package/dist/nodes/addBrowserTaskNode.js +100 -0
  18. package/dist/nodes/addBrowserTaskNode.js.map +1 -0
  19. package/dist/nodes/nodeFactory.d.ts.map +1 -1
  20. package/dist/nodes/nodeFactory.js +4 -0
  21. package/dist/nodes/nodeFactory.js.map +1 -1
  22. package/dist/types/Flows.types.d.ts +9 -2
  23. package/dist/types/Flows.types.d.ts.map +1 -1
  24. package/dist/types/Flows.types.js +1 -0
  25. package/dist/types/Flows.types.js.map +1 -1
  26. package/dist/utils/logger.js +1 -1
  27. package/dist/utils/logger.js.map +1 -1
  28. package/docs/getting-started/installation.md +42 -0
  29. package/package.json +13 -5
  30. package/src/agent.ts +0 -897
  31. package/src/checkpointer/checkpointSaverFactory.ts +0 -18
  32. package/src/cli/index.ts +0 -170
  33. package/src/cli/lambdaHandlerTemplate.ts +0 -46
  34. package/src/edges/createDirectEdge.ts +0 -11
  35. package/src/edges/createLogicalRouter.ts +0 -98
  36. package/src/edges/createPromptRouter.ts +0 -210
  37. package/src/edges/edgeFactory.ts +0 -125
  38. package/src/events/AgentEvents.ts +0 -47
  39. package/src/events/index.ts +0 -3
  40. package/src/index.ts +0 -51
  41. package/src/interfaces/zendesk.ts +0 -157
  42. package/src/internalTools/appActionRunnerTool.ts +0 -75
  43. package/src/internalTools/sendPlaceholderMessage.ts +0 -27
  44. package/src/internalTools/timer.ts +0 -137
  45. package/src/llm/createLlmInstance.ts +0 -10
  46. package/src/nodes/addAppToolNode.ts +0 -95
  47. package/src/nodes/addHumanInTheLoopNode.ts +0 -25
  48. package/src/nodes/addJumpToNode.ts +0 -24
  49. package/src/nodes/addJunctionNode.ts +0 -19
  50. package/src/nodes/addPromptNode.ts +0 -117
  51. package/src/nodes/addToolNode.ts +0 -71
  52. package/src/nodes/addToolRunNode.ts +0 -74
  53. package/src/nodes/addTriggerNode.ts +0 -26
  54. package/src/nodes/nodeFactory.ts +0 -53
  55. package/src/platform/config.ts +0 -77
  56. package/src/platform/mindedChatOpenAI.ts +0 -19
  57. package/src/platform/mindedCheckpointSaver.ts +0 -146
  58. package/src/platform/mindedConnection.ts +0 -199
  59. package/src/platform/mindedConnectionTypes.ts +0 -191
  60. package/src/platform/piiGateway/gateway.ts +0 -103
  61. package/src/platform/piiGateway/index.ts +0 -5
  62. package/src/platform/piiGateway/types.ts +0 -29
  63. package/src/playbooks/playbooks.ts +0 -209
  64. package/src/triggers/triggerTypeToDefaultMessage.ts +0 -9
  65. package/src/types/Agent.types.ts +0 -67
  66. package/src/types/Flows.types.ts +0 -184
  67. package/src/types/LLM.types.ts +0 -15
  68. package/src/types/LangGraph.types.ts +0 -48
  69. package/src/types/Platform.types.ts +0 -1
  70. package/src/types/Tools.types.ts +0 -31
  71. package/src/types/Voice.types.ts +0 -4
  72. package/src/utils/extractStateMemoryResponse.ts +0 -16
  73. package/src/utils/history.ts +0 -9
  74. package/src/utils/logger.ts +0 -22
  75. package/src/utils/wait.ts +0 -1
  76. package/src/voice/elevenLabsUtils.ts +0 -81
  77. package/src/voice/voiceSession.ts +0 -295
@@ -1,117 +0,0 @@
1
- import { RunnableLike } from '@langchain/core/runnables';
2
- import { NodeType, PromptNode } from '../types/Flows.types';
3
- import { PreCompiledGraph, stateAnnotation } from '../types/LangGraph.types';
4
- import { z } from 'zod';
5
- import { BaseLanguageModel } from '@langchain/core/language_models/base';
6
- import { SystemMessage, AIMessage, ToolMessage } from '@langchain/core/messages';
7
- import { Tool } from '../types/Tools.types';
8
- import { tool as langchainTool } from '@langchain/core/tools';
9
- import { AgentEventRequestPayloads, AgentEvents } from '../events/AgentEvents';
10
- import { EmitSignature, HistoryStep } from '../types/Agent.types';
11
- import { createLlmInstance } from '../llm/createLlmInstance';
12
- import extractToolStateResponse from '../utils/extractStateMemoryResponse';
13
- import { Agent } from '../agent';
14
- import { logger } from '../utils/logger';
15
- import { compilePlaybooks } from '../playbooks/playbooks';
16
- import { createHistoryStep } from '../utils/history';
17
-
18
- type AddPromptNodeParams = {
19
- graph: PreCompiledGraph;
20
- node: PromptNode;
21
- llm: BaseLanguageModel;
22
- tools: Tool<any, any>[];
23
- emit: EmitSignature<any, keyof AgentEventRequestPayloads<any>>;
24
- agent: Agent;
25
- };
26
-
27
- export const addPromptNode = async ({ graph, node, llm, tools, emit, agent }: AddPromptNodeParams) => {
28
- const callback: RunnableLike = async (state: typeof stateAnnotation.State) => {
29
- logger.info(`Executing prompt node ${node.displayName}`);
30
- const llmToUse = node.llmConfig ? createLlmInstance(node.llmConfig) : llm;
31
-
32
- const globalTools = tools
33
- .filter((tool) => tool.isGlobal)
34
- .map((tool) =>
35
- langchainTool((input: z.infer<typeof tool.input>) => tool.execute({ input, state, agent }), {
36
- name: tool.name,
37
- description: tool.description,
38
- schema: tool.input,
39
- }),
40
- );
41
-
42
- // Get compiled playbooks with proper parameters
43
- const playbookParams = {
44
- ...state.memory, // Spread memory fields at the top level
45
- };
46
- const compiledPlaybooks = compilePlaybooks(agent.playbooks, playbookParams) || '';
47
-
48
- const message = `
49
- ${node.prompt ? `# Task instructions:\n${node.prompt}\n\n` : ''}
50
- ${state.memory ? `# Task context:\n${JSON.stringify(state.memory)}\n\n` : ''}
51
- ${compiledPlaybooks ? `# General guidelines:\n${compiledPlaybooks}\n\n` : ''}
52
- `;
53
-
54
- const result: AIMessage = await llmToUse.bindTools(globalTools).invoke([...state.messages, new SystemMessage(message)]);
55
-
56
- // Check if the result contains tool calls
57
- if (result.tool_calls && result.tool_calls.length > 0) {
58
- // Execute the tools
59
- const toolResults = [];
60
- let stateUpdates = {};
61
-
62
- for (const toolCall of result.tool_calls) {
63
- const matchedTool = globalTools.find((t) => t.name === toolCall.name);
64
- logger.info({ msg: `Model called tool ${matchedTool?.name}` });
65
- if (matchedTool) {
66
- try {
67
- // Invoke the LangChain tool directly
68
- const toolResult = await matchedTool.invoke(toolCall);
69
- const toolStateUpdate = extractToolStateResponse(toolResult);
70
- // Properly merge memory and other state updates
71
- stateUpdates = {
72
- ...stateUpdates,
73
- ...toolStateUpdate,
74
- memory: { ...(stateUpdates as any).memory, ...(toolStateUpdate as any).memory },
75
- };
76
- toolResults.push(toolResult);
77
- } catch (error) {
78
- logger.error({ msg: `Error executing tool ${toolCall.name}:`, error });
79
- const errorMessage = new ToolMessage({
80
- content: JSON.stringify({ error: error instanceof Error ? error.message : String(error) }),
81
- tool_call_id: toolCall.id!,
82
- });
83
- toolResults.push(errorMessage);
84
- }
85
- } else {
86
- logger.error({ msg: `Model tried to call tool ${toolCall.name} but it was not found` });
87
- }
88
- }
89
-
90
- // Return the tool call message and tool results with state updates spread at top level
91
- return {
92
- ...stateUpdates,
93
- messages: [result, ...toolResults],
94
- };
95
- }
96
-
97
- if (result.getType() === 'ai') {
98
- await emit(AgentEvents.AI_MESSAGE, {
99
- message: result.content as string,
100
- state,
101
- });
102
- console.log('AI Message', result.content);
103
- }
104
-
105
- return {
106
- history: createHistoryStep<HistoryStep>(state.history, {
107
- type: NodeType.PROMPT_NODE,
108
- nodeId: node.name,
109
- nodeDisplayName: node.displayName,
110
- raw: result.content,
111
- messageIds: [result.id!],
112
- }),
113
- messages: [result],
114
- };
115
- };
116
- graph.addNode(node.name, callback);
117
- };
@@ -1,71 +0,0 @@
1
- import { NodeType, ToolNode } from '../types/Flows.types';
2
- import { Tool } from '../types/Tools.types';
3
- import { tool as langchainTool } from '@langchain/core/tools';
4
- import { PreCompiledGraph, stateAnnotation } from '../types/LangGraph.types';
5
- import { AIMessage, SystemMessage } from '@langchain/core/messages';
6
- import { RunnableLike } from '@langchain/core/runnables';
7
- import { LLMProviders } from '../types/LLM.types';
8
- import { logger } from '../utils/logger';
9
- import { Agent } from '../agent';
10
- import { compilePlaybooks } from '../playbooks/playbooks';
11
- import { createHistoryStep } from '../utils/history';
12
- import { HistoryStep } from '../types/Agent.types';
13
-
14
- export const addToolNode = async ({
15
- graph,
16
- node,
17
- tools,
18
- llm,
19
- agent,
20
- }: {
21
- graph: PreCompiledGraph;
22
- node: ToolNode;
23
- tools: Tool<any, any>[];
24
- llm: (typeof LLMProviders)[keyof typeof LLMProviders];
25
- agent: Agent;
26
- }) => {
27
- const toolNode = node as ToolNode;
28
- const matchedTool = tools.find((tool) => tool.name === toolNode.toolName);
29
- if (!matchedTool) {
30
- throw new Error(`Tool not found: ${toolNode.toolName} in node ${node.name}`);
31
- }
32
- const callback: RunnableLike = async (state: typeof stateAnnotation.State) => {
33
- logger.info(`Executing tool node ${toolNode.displayName}`);
34
-
35
- const tool = langchainTool(() => {}, {
36
- name: matchedTool.name,
37
- description: matchedTool.description,
38
- schema: matchedTool.input,
39
- });
40
-
41
- // Get compiled playbooks with proper parameters
42
- const playbookParams = {
43
- ...state.memory, // Spread memory fields at the top level
44
- state, // Keep the full state for backward compatibility
45
- currentTime: new Date().toISOString(),
46
- // Add any other common parameters that playbooks might need
47
- };
48
- const compiledPlaybooks = compilePlaybooks(agent.playbooks, playbookParams) || '';
49
-
50
- const prompt = `${compiledPlaybooks ? compiledPlaybooks + '\n\n' : ''}
51
- Additional context:
52
- workflow memory: ${JSON.stringify(state.memory)}
53
- `;
54
- const AIToolCallMessage: AIMessage = await llm
55
- .bindTools([tool], {
56
- tool_choice: tool.name,
57
- })
58
- .invoke([...state.messages, new SystemMessage(prompt)]);
59
- return {
60
- messages: [AIToolCallMessage],
61
- history: createHistoryStep<HistoryStep>(state.history, {
62
- type: NodeType.TOOL,
63
- nodeId: node.name,
64
- nodeDisplayName: node.displayName,
65
- raw: AIToolCallMessage.tool_calls?.[0] || '',
66
- messageIds: [AIToolCallMessage.id!],
67
- }),
68
- };
69
- };
70
- graph.addNode(node.name, callback);
71
- };
@@ -1,74 +0,0 @@
1
- import { PreCompiledGraph, stateAnnotation } from '../types/LangGraph.types';
2
- import { RunnableLike } from '@langchain/core/runnables';
3
- import { Tool } from '../types/Tools.types';
4
- import { LLMProviders } from '../types/LLM.types';
5
- import { internalNodesSuffix, NodeType, ToolNode } from '../types/Flows.types';
6
- import { tool as langchainTool } from '@langchain/core/tools';
7
- import { ToolMessage } from '@langchain/core/messages';
8
- import { z } from 'zod';
9
- import extractToolStateResponse from '../utils/extractStateMemoryResponse';
10
- import { Agent } from '../agent';
11
- import { logger } from '../utils/logger';
12
- import { createHistoryStep } from '../utils/history';
13
- import { HistoryStep } from '../types/Agent.types';
14
-
15
- type AddToolRunNodeParams = {
16
- graph: PreCompiledGraph;
17
- tools: Tool<any, any>[];
18
- llm: (typeof LLMProviders)[keyof typeof LLMProviders];
19
- toolNode: ToolNode;
20
- attachedToNodeName: string;
21
- agent: Agent;
22
- };
23
-
24
- export const buildToolRunNodeName = (nodeName: string) => `${nodeName}${internalNodesSuffix.TOOL_RUN}`;
25
-
26
- export const addToolRunNode = async ({ graph, tools, toolNode, attachedToNodeName, agent }: AddToolRunNodeParams) => {
27
- const callback: RunnableLike = async (state: typeof stateAnnotation.State) => {
28
- const matchedTool = tools.find((tool) => tool.name === toolNode.toolName);
29
- if (!matchedTool) {
30
- throw new Error(`Tool not found: ${toolNode.toolName}`);
31
- }
32
-
33
- const executeWrapper = async (input: z.infer<typeof matchedTool.input>) => {
34
- try {
35
- const response = await matchedTool.execute({ input, state, agent });
36
- return response || {};
37
- } catch (error) {
38
- logger.error({ msg: 'Error executing tool', error });
39
- throw error;
40
- }
41
- };
42
- const tool = langchainTool(executeWrapper, {
43
- name: matchedTool.name,
44
- description: matchedTool.description,
45
- schema: matchedTool.input,
46
- });
47
-
48
- const toolCallObj = state.messages[state.messages.length - 1] as any;
49
- if (!toolCallObj.tool_calls) {
50
- throw new Error('Tool call not found');
51
- }
52
- const toolCallMessage = await tool.invoke(toolCallObj.tool_calls[0]);
53
- const toolStateUpdate = extractToolStateResponse(toolCallMessage as ToolMessage);
54
-
55
- // Push the toolCallMessage into the messages array from toolStateUpdate
56
- const updatedMessages = [toolCallMessage, ...((toolStateUpdate as any).messages || [])];
57
-
58
- // Return the full state update from the tool with the updated messages
59
- return {
60
- ...toolStateUpdate,
61
- messages: updatedMessages,
62
- history: createHistoryStep<HistoryStep>(state.history, {
63
- type: NodeType.TOOL,
64
- nodeId: toolNode.name,
65
- nodeDisplayName: toolNode.displayName,
66
- raw: toolCallMessage,
67
- messageIds: [],
68
- }),
69
- };
70
- };
71
-
72
- graph.addNode(buildToolRunNodeName(attachedToNodeName), callback);
73
- graph.addEdge(attachedToNodeName as any, buildToolRunNodeName(attachedToNodeName) as any);
74
- };
@@ -1,26 +0,0 @@
1
- import { RunnableLike } from '@langchain/core/runnables';
2
- import { NodeType, TriggerNode, TriggerType } from '../types/Flows.types';
3
- import { PreCompiledGraph, stateAnnotation } from '../types/LangGraph.types';
4
- import { logger } from '../utils/logger';
5
- import { HistoryStep } from '../types/Agent.types';
6
- import { createHistoryStep } from '../utils/history';
7
-
8
- export const addTriggerNode = async ({ graph, node }: { graph: PreCompiledGraph; node: TriggerNode }) => {
9
- const callback: RunnableLike = async (state: typeof stateAnnotation.State) => {
10
- logger.info(`Executing trigger node ${node.displayName}`);
11
- if (node.triggerType === TriggerType.MANUAL) {
12
- return {
13
- history: createHistoryStep<HistoryStep>(state.history, {
14
- type: NodeType.TRIGGER,
15
- nodeId: node.name,
16
- nodeDisplayName: node.displayName,
17
- raw: '',
18
- messageIds: [],
19
- }),
20
- };
21
- }
22
- return {};
23
- };
24
-
25
- graph.addNode(node.name, callback);
26
- };
@@ -1,53 +0,0 @@
1
- import { Node, NodeType } from '../types/Flows.types';
2
- import { PreCompiledGraph } from '../types/LangGraph.types';
3
- import { Tool } from '../types/Tools.types';
4
- import { addToolNode } from './addToolNode';
5
- import { addPromptNode } from './addPromptNode';
6
- import { AgentEventRequestPayloads } from '../events/AgentEvents';
7
- import { EmitSignature } from '../types/Agent.types';
8
- import { addAppToolNode } from './addAppToolNode';
9
- import { addTriggerNode } from './addTriggerNode';
10
- import { LLMProviders } from '../types/LLM.types';
11
- import { Agent } from '../agent';
12
- import { addJumpToNode } from './addJumpToNode';
13
- import { addJunctionNode } from './addJunctionNode';
14
-
15
- export const nodeFactory = ({
16
- graph,
17
- node,
18
- tools,
19
- llm,
20
- emit,
21
- agent,
22
- }: {
23
- graph: PreCompiledGraph;
24
- node: Node;
25
- tools: Tool<any, any>[];
26
- llm: (typeof LLMProviders)[keyof typeof LLMProviders];
27
- emit: EmitSignature<any, keyof AgentEventRequestPayloads<any>>;
28
- agent: Agent;
29
- }) => {
30
- const nodeType = node.type;
31
- switch (nodeType) {
32
- case NodeType.TRIGGER:
33
- addTriggerNode({ graph, node });
34
- break;
35
- case NodeType.TOOL:
36
- addToolNode({ graph, node, tools, llm, agent });
37
- break;
38
- case NodeType.JUNCTION:
39
- addJunctionNode({ graph, node });
40
- break;
41
- case NodeType.APP_TOOL:
42
- addAppToolNode({ graph, node, llm, agent });
43
- break;
44
- case NodeType.PROMPT_NODE:
45
- addPromptNode({ graph, node, tools, llm, emit, agent });
46
- break;
47
- case NodeType.JUMP_TO_NODE:
48
- addJumpToNode({ graph, node });
49
- break;
50
- default:
51
- throw new Error(`Unsupported node type: ${nodeType}`);
52
- }
53
- };
@@ -1,77 +0,0 @@
1
- import { resolve } from 'path';
2
- import { config } from 'dotenv';
3
- import { Environment } from '../types/Platform.types';
4
- const path = resolve(__dirname, '../../.env');
5
- config({ path });
6
-
7
- type MindedConfig = {
8
- baseUrl: string;
9
- env: Environment;
10
- token: string | null;
11
- runLocally: boolean;
12
- isDeployed: boolean;
13
- dashboardConnected: boolean;
14
- elevenLabsKey: string | null;
15
- elevenLabsAgentId: string | null;
16
- };
17
-
18
- export const getConfig = (): MindedConfig => {
19
- const environment = (process.env.NODE_ENV as Environment) || 'sandbox';
20
- switch (environment) {
21
- case 'test':
22
- return {
23
- baseUrl: 'http://localhost:8888',
24
- env: 'test',
25
- token: process.env.MINDED_CONNECTION_TOKEN || '',
26
- runLocally: process.env.RUN_LOCALLY !== 'false',
27
- isDeployed: process.env.IS_DEPLOYED_AGENT === 'true',
28
- dashboardConnected: false,
29
- elevenLabsKey: process.env.ELEVEN_LABS_API_KEY || null,
30
- elevenLabsAgentId: process.env.ELEVEN_LABS_AGENT_ID || null,
31
- };
32
- case 'development':
33
- return {
34
- baseUrl: 'http://localhost:8888',
35
- env: 'development',
36
- token: process.env.MINDED_CONNECTION_TOKEN || '',
37
- runLocally: process.env.RUN_LOCALLY === 'true',
38
- isDeployed: process.env.IS_DEPLOYED_AGENT === 'true',
39
- dashboardConnected: true,
40
- elevenLabsKey: process.env.ELEVEN_LABS_API_KEY || null,
41
- elevenLabsAgentId: process.env.ELEVEN_LABS_AGENT_ID || 'Iyr6DCOj80AdvVEVHSGt',
42
- };
43
- case 'sandbox-staging':
44
- return {
45
- baseUrl: 'https://dashboard-api.dev.minded.com',
46
- env: 'sandbox-staging',
47
- token: process.env.MINDED_CONNECTION_TOKEN || '',
48
- runLocally: false,
49
- isDeployed: process.env.IS_DEPLOYED_AGENT === 'true',
50
- elevenLabsKey: process.env.ELEVEN_LABS_API_KEY || null,
51
- dashboardConnected: true,
52
- elevenLabsAgentId: process.env.ELEVEN_LABS_AGENT_ID || 'agent_01jye194psezr909nwec27m2t9',
53
- };
54
- case 'sandbox':
55
- return {
56
- baseUrl: 'https://dashboard-api.minded.com',
57
- env: 'sandbox',
58
- token: process.env.MINDED_CONNECTION_TOKEN || '',
59
- runLocally: false,
60
- isDeployed: process.env.IS_DEPLOYED_AGENT === 'true',
61
- elevenLabsKey: process.env.ELEVEN_LABS_API_KEY || null,
62
- dashboardConnected: true,
63
- elevenLabsAgentId: process.env.ELEVEN_LABS_AGENT_ID || 'agent_01jye194psezr909nwec27m2t9',
64
- };
65
- case 'production':
66
- return {
67
- baseUrl: 'https://api.minded.com',
68
- env: 'production',
69
- token: process.env.MINDED_CONNECTION_TOKEN || '',
70
- runLocally: false,
71
- isDeployed: process.env.IS_DEPLOYED_AGENT === 'true',
72
- elevenLabsKey: process.env.ELEVEN_LABS_API_KEY || null,
73
- dashboardConnected: false,
74
- elevenLabsAgentId: process.env.ELEVEN_LABS_AGENT_ID || 'agent_01jye1ajkaf17axpcxg220qwcm',
75
- };
76
- }
77
- };
@@ -1,19 +0,0 @@
1
- import { ChatOpenAI, ChatOpenAIFields } from "@langchain/openai";
2
- import { getConfig } from "./config";
3
-
4
- export class MindedChatOpenAI extends ChatOpenAI {
5
- constructor(fields?: ChatOpenAIFields) {
6
- const { token, baseUrl } = getConfig();
7
- const mindedBaseUrl = `${baseUrl}/sdk/llmGateway/chatOpenAI`;
8
- if (!token) {
9
- throw new Error('Minded token not found');
10
- }
11
- super({
12
- ...fields,
13
- apiKey: token,
14
- configuration: {
15
- baseURL: mindedBaseUrl,
16
- },
17
- });
18
- }
19
- }
@@ -1,146 +0,0 @@
1
- import {
2
- BaseCheckpointSaver,
3
- CheckpointTuple,
4
- Checkpoint,
5
- CheckpointMetadata,
6
- ChannelVersions,
7
- PendingWrite,
8
- CheckpointListOptions,
9
- CheckpointPendingWrite,
10
- } from '@langchain/langgraph-checkpoint';
11
- import type { RunnableConfig } from '@langchain/core/runnables';
12
- import * as mindedConnection from './mindedConnection';
13
- import {
14
- mindedConnectionSocketMessageType,
15
- OnCheckpointGetTuple,
16
- OnCheckpointGetTupleResponse,
17
- OnCheckpointList,
18
- OnCheckpointListResponse,
19
- OnCheckpointPut,
20
- OnCheckpointPutResponse,
21
- OnCheckpointPutWrites,
22
- } from './mindedConnectionTypes';
23
- import { logger } from '../utils/logger';
24
-
25
- export class MindedCheckpointSaver extends BaseCheckpointSaver<number> {
26
- constructor() {
27
- super();
28
- }
29
-
30
- async getTuple(config: RunnableConfig<Record<string, any>>): Promise<CheckpointTuple | undefined> {
31
- const response = await mindedConnection.awaitEmit<OnCheckpointGetTuple, OnCheckpointGetTupleResponse>(
32
- mindedConnectionSocketMessageType.CHECKPOINT_GET_TUPLE,
33
- {
34
- type: mindedConnectionSocketMessageType.CHECKPOINT_GET_TUPLE,
35
- config,
36
- },
37
- );
38
-
39
- if (!response.tuple) {
40
- return undefined;
41
- }
42
-
43
- const tuple = response.tuple;
44
-
45
- // Re-serialize and deserialize checkpoint to restore class information
46
- const [checkpointType, serializedCheckpoint] = this.serde.dumpsTyped(tuple.checkpoint);
47
- const checkpoint = (await this.serde.loadsTyped(checkpointType, serializedCheckpoint)) as Checkpoint;
48
-
49
- // Re-serialize and deserialize metadata to restore class information
50
- const metadata = tuple.metadata
51
- ? (() => {
52
- const [metadataType, serializedMetadata] = this.serde.dumpsTyped(tuple.metadata);
53
- return this.serde.loadsTyped(metadataType, serializedMetadata) as CheckpointMetadata;
54
- })()
55
- : undefined;
56
-
57
- // Re-serialize and deserialize pending writes to restore class information
58
- const pendingWrites: CheckpointPendingWrite[] = tuple.pendingWrites
59
- ? await Promise.all(
60
- tuple.pendingWrites.map(async (write) => {
61
- const [taskId, channel, value] = write;
62
- const [valueType, serializedValue] = this.serde.dumpsTyped(value);
63
- const deserializedValue = await this.serde.loadsTyped(valueType, serializedValue);
64
- return [taskId, channel, deserializedValue] as CheckpointPendingWrite;
65
- }),
66
- )
67
- : [];
68
-
69
- return {
70
- config: tuple.config,
71
- checkpoint,
72
- metadata: await metadata,
73
- parentConfig: tuple.parentConfig,
74
- pendingWrites,
75
- };
76
- }
77
-
78
- async *list(config: RunnableConfig<Record<string, any>>, options?: CheckpointListOptions): AsyncGenerator<CheckpointTuple, any, any> {
79
- const response = await mindedConnection.awaitEmit<OnCheckpointList, OnCheckpointListResponse>(
80
- mindedConnectionSocketMessageType.CHECKPOINT_LIST,
81
- {
82
- type: mindedConnectionSocketMessageType.CHECKPOINT_LIST,
83
- config,
84
- options,
85
- },
86
- );
87
-
88
- for (const tuple of response.checkpoints || []) {
89
- // Re-serialize and deserialize checkpoint to restore class information
90
- const [checkpointType, serializedCheckpoint] = this.serde.dumpsTyped(tuple.checkpoint);
91
- const checkpoint = (await this.serde.loadsTyped(checkpointType, serializedCheckpoint)) as Checkpoint;
92
-
93
- // Re-serialize and deserialize metadata to restore class information
94
- const metadata = tuple.metadata
95
- ? (() => {
96
- const [metadataType, serializedMetadata] = this.serde.dumpsTyped(tuple.metadata);
97
- return this.serde.loadsTyped(metadataType, serializedMetadata) as CheckpointMetadata;
98
- })()
99
- : undefined;
100
-
101
- yield {
102
- config: tuple.config,
103
- checkpoint,
104
- metadata: await metadata,
105
- parentConfig: tuple.parentConfig,
106
- };
107
- }
108
- }
109
-
110
- async put(
111
- config: RunnableConfig<Record<string, any>>,
112
- checkpoint: Checkpoint<string, string>,
113
- metadata: CheckpointMetadata,
114
- newVersions: ChannelVersions,
115
- ): Promise<RunnableConfig<Record<string, any>>> {
116
- try {
117
- const response = await mindedConnection.awaitEmit<OnCheckpointPut, OnCheckpointPutResponse>(
118
- mindedConnectionSocketMessageType.CHECKPOINT_PUT,
119
- {
120
- type: mindedConnectionSocketMessageType.CHECKPOINT_PUT,
121
- config,
122
- checkpoint,
123
- metadata,
124
- newVersions,
125
- },
126
- );
127
- return response.config;
128
- } catch (error) {
129
- logger.error({ msg: 'Error putting checkpoint:', error });
130
- return config;
131
- }
132
- }
133
-
134
- async putWrites(config: RunnableConfig<Record<string, any>>, writes: PendingWrite[], taskId: string): Promise<void> {
135
- try {
136
- await mindedConnection.awaitEmit<OnCheckpointPutWrites, any>(mindedConnectionSocketMessageType.CHECKPOINT_PUT_WRITES, {
137
- type: mindedConnectionSocketMessageType.CHECKPOINT_PUT_WRITES,
138
- config,
139
- writes,
140
- taskId,
141
- });
142
- } catch (error) {
143
- logger.error({ msg: 'Error putting writes:', error });
144
- }
145
- }
146
- }