@langchain/core 1.0.0-alpha.6 → 1.0.0
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.
- package/CHANGELOG.md +131 -0
- package/LICENSE +6 -6
- package/README.md +2 -23
- package/dist/agents.d.ts.map +1 -1
- package/dist/caches/base.d.cts.map +1 -1
- package/dist/callbacks/base.d.cts.map +1 -1
- package/dist/callbacks/base.d.ts.map +1 -1
- package/dist/callbacks/manager.cjs +9 -64
- package/dist/callbacks/manager.cjs.map +1 -1
- package/dist/callbacks/manager.d.cts +1 -23
- package/dist/callbacks/manager.d.cts.map +1 -1
- package/dist/callbacks/manager.d.ts +1 -23
- package/dist/callbacks/manager.d.ts.map +1 -1
- package/dist/callbacks/manager.js +10 -63
- package/dist/callbacks/manager.js.map +1 -1
- package/dist/chat_history.cjs +0 -4
- package/dist/chat_history.cjs.map +1 -1
- package/dist/chat_history.d.cts +1 -3
- package/dist/chat_history.d.cts.map +1 -1
- package/dist/chat_history.d.ts +1 -3
- package/dist/chat_history.d.ts.map +1 -1
- package/dist/chat_history.js +0 -4
- package/dist/chat_history.js.map +1 -1
- package/dist/document_loaders/base.cjs +1 -13
- package/dist/document_loaders/base.cjs.map +1 -1
- package/dist/document_loaders/base.d.cts +0 -9
- package/dist/document_loaders/base.d.cts.map +1 -1
- package/dist/document_loaders/base.d.ts +0 -9
- package/dist/document_loaders/base.d.ts.map +1 -1
- package/dist/document_loaders/base.js +1 -13
- package/dist/document_loaders/base.js.map +1 -1
- package/dist/document_loaders/langsmith.d.cts +1 -1
- package/dist/language_models/base.cjs.map +1 -1
- package/dist/language_models/base.d.cts +0 -16
- package/dist/language_models/base.d.cts.map +1 -1
- package/dist/language_models/base.d.ts +0 -16
- package/dist/language_models/base.d.ts.map +1 -1
- package/dist/language_models/base.js.map +1 -1
- package/dist/language_models/chat_models.cjs +10 -83
- package/dist/language_models/chat_models.cjs.map +1 -1
- package/dist/language_models/chat_models.d.cts +21 -55
- package/dist/language_models/chat_models.d.cts.map +1 -1
- package/dist/language_models/chat_models.d.ts +21 -55
- package/dist/language_models/chat_models.d.ts.map +1 -1
- package/dist/language_models/chat_models.js +11 -83
- package/dist/language_models/chat_models.js.map +1 -1
- package/dist/language_models/llms.cjs +0 -56
- package/dist/language_models/llms.cjs.map +1 -1
- package/dist/language_models/llms.d.cts +1 -43
- package/dist/language_models/llms.d.cts.map +1 -1
- package/dist/language_models/llms.d.ts +1 -43
- package/dist/language_models/llms.d.ts.map +1 -1
- package/dist/language_models/llms.js +0 -56
- package/dist/language_models/llms.js.map +1 -1
- package/dist/load/import_map.cjs +1 -7
- package/dist/load/import_map.cjs.map +1 -1
- package/dist/load/import_map.js +1 -7
- package/dist/load/import_map.js.map +1 -1
- package/dist/messages/ai.cjs +4 -0
- package/dist/messages/ai.cjs.map +1 -1
- package/dist/messages/ai.js +4 -0
- package/dist/messages/ai.js.map +1 -1
- package/dist/messages/block_translators/anthropic.cjs +191 -144
- package/dist/messages/block_translators/anthropic.cjs.map +1 -1
- package/dist/messages/block_translators/anthropic.js +191 -144
- package/dist/messages/block_translators/anthropic.js.map +1 -1
- package/dist/messages/block_translators/index.cjs +2 -2
- package/dist/messages/block_translators/index.cjs.map +1 -1
- package/dist/messages/block_translators/index.js +4 -4
- package/dist/messages/block_translators/index.js.map +1 -1
- package/dist/messages/block_translators/openai.cjs +78 -22
- package/dist/messages/block_translators/openai.cjs.map +1 -1
- package/dist/messages/block_translators/openai.js +78 -22
- package/dist/messages/block_translators/openai.js.map +1 -1
- package/dist/messages/content/tools.cjs +1 -5
- package/dist/messages/content/tools.cjs.map +1 -1
- package/dist/messages/content/tools.d.cts +1 -85
- package/dist/messages/content/tools.d.cts.map +1 -1
- package/dist/messages/content/tools.d.ts +1 -85
- package/dist/messages/content/tools.d.ts.map +1 -1
- package/dist/messages/content/tools.js +1 -5
- package/dist/messages/content/tools.js.map +1 -1
- package/dist/messages/metadata.cjs.map +1 -1
- package/dist/messages/metadata.d.cts +3 -0
- package/dist/messages/metadata.d.cts.map +1 -1
- package/dist/messages/metadata.d.ts +3 -0
- package/dist/messages/metadata.d.ts.map +1 -1
- package/dist/messages/metadata.js.map +1 -1
- package/dist/messages/tool.cjs +2 -0
- package/dist/messages/tool.cjs.map +1 -1
- package/dist/messages/tool.d.cts +2 -0
- package/dist/messages/tool.d.cts.map +1 -1
- package/dist/messages/tool.d.ts +2 -0
- package/dist/messages/tool.d.ts.map +1 -1
- package/dist/messages/tool.js +2 -0
- package/dist/messages/tool.js.map +1 -1
- package/dist/output_parsers/json.cjs +5 -0
- package/dist/output_parsers/json.cjs.map +1 -1
- package/dist/output_parsers/json.d.cts +2 -0
- package/dist/output_parsers/json.d.cts.map +1 -1
- package/dist/output_parsers/json.d.ts +2 -0
- package/dist/output_parsers/json.d.ts.map +1 -1
- package/dist/output_parsers/json.js +5 -0
- package/dist/output_parsers/json.js.map +1 -1
- package/dist/prompts/base.cjs +0 -36
- package/dist/prompts/base.cjs.map +1 -1
- package/dist/prompts/base.d.cts +0 -16
- package/dist/prompts/base.d.cts.map +1 -1
- package/dist/prompts/base.d.ts +0 -16
- package/dist/prompts/base.d.ts.map +1 -1
- package/dist/prompts/base.js +0 -36
- package/dist/prompts/base.js.map +1 -1
- package/dist/prompts/chat.cjs +1 -5
- package/dist/prompts/chat.cjs.map +1 -1
- package/dist/prompts/chat.d.cts +1 -4
- package/dist/prompts/chat.d.cts.map +1 -1
- package/dist/prompts/chat.d.ts +1 -4
- package/dist/prompts/chat.d.ts.map +1 -1
- package/dist/prompts/chat.js +1 -5
- package/dist/prompts/chat.js.map +1 -1
- package/dist/prompts/dict.d.cts +1 -1
- package/dist/prompts/dict.d.ts +1 -1
- package/dist/prompts/few_shot.d.cts +2 -2
- package/dist/prompts/few_shot.d.ts +2 -2
- package/dist/prompts/image.d.cts +1 -1
- package/dist/prompts/image.d.ts +1 -1
- package/dist/prompts/index.d.cts +2 -2
- package/dist/prompts/index.d.ts +2 -2
- package/dist/prompts/pipeline.d.cts +1 -1
- package/dist/prompts/pipeline.d.ts +1 -1
- package/dist/prompts/prompt.d.cts +2 -2
- package/dist/prompts/prompt.d.ts +2 -2
- package/dist/retrievers/index.cjs +3 -18
- package/dist/retrievers/index.cjs.map +1 -1
- package/dist/retrievers/index.d.cts +2 -27
- package/dist/retrievers/index.d.cts.map +1 -1
- package/dist/retrievers/index.d.ts +2 -27
- package/dist/retrievers/index.d.ts.map +1 -1
- package/dist/retrievers/index.js +3 -18
- package/dist/retrievers/index.js.map +1 -1
- package/dist/runnables/base.cjs +24 -63
- package/dist/runnables/base.cjs.map +1 -1
- package/dist/runnables/base.d.cts +10 -39
- package/dist/runnables/base.d.cts.map +1 -1
- package/dist/runnables/base.d.ts +10 -39
- package/dist/runnables/base.d.ts.map +1 -1
- package/dist/runnables/base.js +24 -63
- package/dist/runnables/base.js.map +1 -1
- package/dist/runnables/graph.cjs +1 -1
- package/dist/runnables/graph.cjs.map +1 -1
- package/dist/runnables/graph.js +2 -2
- package/dist/runnables/graph.js.map +1 -1
- package/dist/runnables/graph_mermaid.cjs +1 -10
- package/dist/runnables/graph_mermaid.cjs.map +1 -1
- package/dist/runnables/graph_mermaid.js +1 -10
- package/dist/runnables/graph_mermaid.js.map +1 -1
- package/dist/runnables/history.cjs +1 -1
- package/dist/runnables/history.cjs.map +1 -1
- package/dist/runnables/history.d.cts +2 -2
- package/dist/runnables/history.d.cts.map +1 -1
- package/dist/runnables/history.d.ts +2 -2
- package/dist/runnables/history.d.ts.map +1 -1
- package/dist/runnables/history.js +1 -1
- package/dist/runnables/history.js.map +1 -1
- package/dist/stores.cjs.map +1 -1
- package/dist/stores.d.cts +3 -29
- package/dist/stores.d.cts.map +1 -1
- package/dist/stores.d.ts +3 -29
- package/dist/stores.d.ts.map +1 -1
- package/dist/stores.js.map +1 -1
- package/dist/tools/index.cjs +12 -4
- package/dist/tools/index.cjs.map +1 -1
- package/dist/tools/index.js +12 -4
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.d.cts +4 -0
- package/dist/tools/types.d.cts.map +1 -1
- package/dist/tools/types.d.ts +4 -0
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/types.js.map +1 -1
- package/dist/tracers/base.cjs +1 -1
- package/dist/tracers/base.cjs.map +1 -1
- package/dist/tracers/base.d.cts +1 -1
- package/dist/tracers/base.js +2 -2
- package/dist/tracers/base.js.map +1 -1
- package/dist/tracers/log_stream.d.cts +1 -1
- package/dist/tracers/log_stream.d.ts +1 -1
- package/dist/tracers/tracer_langchain.cjs +1 -0
- package/dist/tracers/tracer_langchain.cjs.map +1 -1
- package/dist/tracers/tracer_langchain.d.cts +2 -2
- package/dist/tracers/tracer_langchain.js +1 -0
- package/dist/tracers/tracer_langchain.js.map +1 -1
- package/dist/utils/env.cjs +1 -9
- package/dist/utils/env.cjs.map +1 -1
- package/dist/utils/env.d.cts +2 -6
- package/dist/utils/env.d.cts.map +1 -1
- package/dist/utils/env.d.ts +2 -6
- package/dist/utils/env.d.ts.map +1 -1
- package/dist/utils/env.js +2 -9
- package/dist/utils/env.js.map +1 -1
- package/dist/utils/testing/message_history.cjs +1 -1
- package/dist/utils/testing/message_history.cjs.map +1 -1
- package/dist/utils/testing/message_history.d.cts +1 -1
- package/dist/utils/testing/message_history.d.cts.map +1 -1
- package/dist/utils/testing/message_history.d.ts +1 -1
- package/dist/utils/testing/message_history.d.ts.map +1 -1
- package/dist/utils/testing/message_history.js +1 -1
- package/dist/utils/testing/message_history.js.map +1 -1
- package/dist/utils/types/index.cjs +6 -0
- package/dist/utils/types/index.d.cts +2 -2
- package/dist/utils/types/index.d.ts +2 -2
- package/dist/utils/types/index.js +5 -2
- package/dist/utils/types/zod.cjs +23 -0
- package/dist/utils/types/zod.cjs.map +1 -1
- package/dist/utils/types/zod.d.cts +11 -1
- package/dist/utils/types/zod.d.cts.map +1 -1
- package/dist/utils/types/zod.d.ts +11 -1
- package/dist/utils/types/zod.d.ts.map +1 -1
- package/dist/utils/types/zod.js +21 -1
- package/dist/utils/types/zod.js.map +1 -1
- package/package.json +121 -154
- package/dist/runnables/remote.cjs +0 -399
- package/dist/runnables/remote.cjs.map +0 -1
- package/dist/runnables/remote.d.cts +0 -73
- package/dist/runnables/remote.d.cts.map +0 -1
- package/dist/runnables/remote.d.ts +0 -73
- package/dist/runnables/remote.d.ts.map +0 -1
- package/dist/runnables/remote.js +0 -393
- package/dist/runnables/remote.js.map +0 -1
- package/dist/tracers/initialize.cjs +0 -46
- package/dist/tracers/initialize.cjs.map +0 -1
- package/dist/tracers/initialize.d.cts +0 -26
- package/dist/tracers/initialize.d.cts.map +0 -1
- package/dist/tracers/initialize.d.ts +0 -26
- package/dist/tracers/initialize.d.ts.map +0 -1
- package/dist/tracers/initialize.js +0 -39
- package/dist/tracers/initialize.js.map +0 -1
- package/dist/tracers/tracer_langchain_v1.cjs +0 -168
- package/dist/tracers/tracer_langchain_v1.cjs.map +0 -1
- package/dist/tracers/tracer_langchain_v1.d.cts +0 -64
- package/dist/tracers/tracer_langchain_v1.d.cts.map +0 -1
- package/dist/tracers/tracer_langchain_v1.d.ts +0 -64
- package/dist/tracers/tracer_langchain_v1.d.ts.map +0 -1
- package/dist/tracers/tracer_langchain_v1.js +0 -162
- package/dist/tracers/tracer_langchain_v1.js.map +0 -1
package/dist/runnables/graph.cjs
CHANGED
|
@@ -186,7 +186,7 @@ var Graph = class Graph {
|
|
|
186
186
|
}
|
|
187
187
|
async drawMermaidPng(params) {
|
|
188
188
|
const mermaidSyntax = this.drawMermaid(params);
|
|
189
|
-
return require_graph_mermaid.
|
|
189
|
+
return require_graph_mermaid.drawMermaidImage(mermaidSyntax, { backgroundColor: params?.backgroundColor });
|
|
190
190
|
}
|
|
191
191
|
};
|
|
192
192
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph.cjs","names":["id: string | undefined","data: RunnableInterface | RunnableIOSchema","isRunnableInterface","node: Node","toJsonSchema","params?: { nodes: Record<string, Node>; edges: Edge[] }","stableNodeIds: Record<string, string | number>","item: Record<string, unknown>","id?: string","metadata?: Record<string, any>","source: Node","target: Node","data?: string","conditional?: boolean","edge: Edge","graph: Graph","isUuid","id: string","nodeLabels: Record<string, string>","nodeId: string","params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }","drawMermaid","params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n backgroundColor?: string;\n }","drawMermaidPng","exclude: string[]","found: Node[]"],"sources":["../../src/runnables/graph.ts"],"sourcesContent":["import { v4 as uuidv4, validate as isUuid } from \"uuid\";\nimport type {\n RunnableInterface,\n RunnableIOSchema,\n Node,\n Edge,\n} from \"./types.js\";\nimport { isRunnableInterface } from \"./utils.js\";\nimport { drawMermaid, drawMermaidPng } from \"./graph_mermaid.js\";\nimport { toJsonSchema } from \"../utils/json_schema.js\";\n\nexport { Node, Edge };\n\nfunction nodeDataStr(\n id: string | undefined,\n data: RunnableInterface | RunnableIOSchema\n): string {\n if (id !== undefined && !isUuid(id)) {\n return id;\n } else if (isRunnableInterface(data)) {\n try {\n let dataStr = data.getName();\n dataStr = dataStr.startsWith(\"Runnable\")\n ? dataStr.slice(\"Runnable\".length)\n : dataStr;\n return dataStr;\n } catch {\n return data.getName();\n }\n } else {\n return data.name ?? \"UnknownSchema\";\n }\n}\n\nfunction nodeDataJson(node: Node) {\n // if node.data implements Runnable\n if (isRunnableInterface(node.data)) {\n return {\n type: \"runnable\",\n data: {\n id: node.data.lc_id,\n name: node.data.getName(),\n },\n };\n } else {\n return {\n type: \"schema\",\n data: { ...toJsonSchema(node.data.schema), title: node.data.name },\n };\n }\n}\n\nexport class Graph {\n nodes: Record<string, Node> = {};\n\n edges: Edge[] = [];\n\n constructor(params?: { nodes: Record<string, Node>; edges: Edge[] }) {\n this.nodes = params?.nodes ?? this.nodes;\n this.edges = params?.edges ?? this.edges;\n }\n\n // Convert the graph to a JSON-serializable format.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n toJSON(): Record<string, any> {\n const stableNodeIds: Record<string, string | number> = {};\n Object.values(this.nodes).forEach((node, i) => {\n stableNodeIds[node.id] = isUuid(node.id) ? i : node.id;\n });\n\n return {\n nodes: Object.values(this.nodes).map((node) => ({\n id: stableNodeIds[node.id],\n ...nodeDataJson(node),\n })),\n edges: this.edges.map((edge) => {\n const item: Record<string, unknown> = {\n source: stableNodeIds[edge.source],\n target: stableNodeIds[edge.target],\n };\n\n if (typeof edge.data !== \"undefined\") {\n item.data = edge.data;\n }\n\n if (typeof edge.conditional !== \"undefined\") {\n item.conditional = edge.conditional;\n }\n return item;\n }),\n };\n }\n\n addNode(\n data: RunnableInterface | RunnableIOSchema,\n id?: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n metadata?: Record<string, any>\n ): Node {\n if (id !== undefined && this.nodes[id] !== undefined) {\n throw new Error(`Node with id ${id} already exists`);\n }\n const nodeId = id ?? uuidv4();\n const node: Node = {\n id: nodeId,\n data,\n name: nodeDataStr(id, data),\n metadata,\n };\n this.nodes[nodeId] = node;\n return node;\n }\n\n removeNode(node: Node): void {\n // Remove the node from the nodes map\n delete this.nodes[node.id];\n\n // Filter out edges connected to the node\n this.edges = this.edges.filter(\n (edge) => edge.source !== node.id && edge.target !== node.id\n );\n }\n\n addEdge(\n source: Node,\n target: Node,\n data?: string,\n conditional?: boolean\n ): Edge {\n if (this.nodes[source.id] === undefined) {\n throw new Error(`Source node ${source.id} not in graph`);\n }\n if (this.nodes[target.id] === undefined) {\n throw new Error(`Target node ${target.id} not in graph`);\n }\n const edge: Edge = {\n source: source.id,\n target: target.id,\n data,\n conditional,\n };\n this.edges.push(edge);\n return edge;\n }\n\n firstNode(): Node | undefined {\n return _firstNode(this);\n }\n\n lastNode(): Node | undefined {\n return _lastNode(this);\n }\n\n /**\n * Add all nodes and edges from another graph.\n * Note this doesn't check for duplicates, nor does it connect the graphs.\n */\n extend(graph: Graph, prefix = \"\") {\n let finalPrefix = prefix;\n const nodeIds = Object.values(graph.nodes).map((node) => node.id);\n if (nodeIds.every(isUuid)) {\n finalPrefix = \"\";\n }\n\n const prefixed = (id: string) => {\n return finalPrefix ? `${finalPrefix}:${id}` : id;\n };\n\n Object.entries(graph.nodes).forEach(([key, value]) => {\n this.nodes[prefixed(key)] = { ...value, id: prefixed(key) };\n });\n\n const newEdges = graph.edges.map((edge) => {\n return {\n ...edge,\n source: prefixed(edge.source),\n target: prefixed(edge.target),\n };\n });\n // Add all edges from the other graph\n this.edges = [...this.edges, ...newEdges];\n const first = graph.firstNode();\n const last = graph.lastNode();\n return [\n first ? { id: prefixed(first.id), data: first.data } : undefined,\n last ? { id: prefixed(last.id), data: last.data } : undefined,\n ];\n }\n\n trimFirstNode(): void {\n const firstNode = this.firstNode();\n if (firstNode && _firstNode(this, [firstNode.id])) {\n this.removeNode(firstNode);\n }\n }\n\n trimLastNode(): void {\n const lastNode = this.lastNode();\n if (lastNode && _lastNode(this, [lastNode.id])) {\n this.removeNode(lastNode);\n }\n }\n\n /**\n * Return a new graph with all nodes re-identified,\n * using their unique, readable names where possible.\n */\n reid(): Graph {\n const nodeLabels: Record<string, string> = Object.fromEntries(\n Object.values(this.nodes).map((node) => [node.id, node.name])\n );\n const nodeLabelCounts = new Map<string, number>();\n Object.values(nodeLabels).forEach((label) => {\n nodeLabelCounts.set(label, (nodeLabelCounts.get(label) || 0) + 1);\n });\n\n const getNodeId = (nodeId: string): string => {\n const label = nodeLabels[nodeId];\n if (isUuid(nodeId) && nodeLabelCounts.get(label) === 1) {\n return label;\n } else {\n return nodeId;\n }\n };\n\n return new Graph({\n nodes: Object.fromEntries(\n Object.entries(this.nodes).map(([id, node]) => [\n getNodeId(id),\n { ...node, id: getNodeId(id) },\n ])\n ),\n edges: this.edges.map((edge) => ({\n ...edge,\n source: getNodeId(edge.source),\n target: getNodeId(edge.target),\n })),\n });\n }\n\n drawMermaid(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }): string {\n const {\n withStyles,\n curveStyle,\n nodeColors = {\n default: \"fill:#f2f0ff,line-height:1.2\",\n first: \"fill-opacity:0\",\n last: \"fill:#bfb6fc\",\n },\n wrapLabelNWords,\n } = params ?? {};\n const graph = this.reid();\n const firstNode = graph.firstNode();\n\n const lastNode = graph.lastNode();\n\n return drawMermaid(graph.nodes, graph.edges, {\n firstNode: firstNode?.id,\n lastNode: lastNode?.id,\n withStyles,\n curveStyle,\n nodeColors,\n wrapLabelNWords,\n });\n }\n\n async drawMermaidPng(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n backgroundColor?: string;\n }): Promise<Blob> {\n const mermaidSyntax = this.drawMermaid(params);\n return drawMermaidPng(mermaidSyntax, {\n backgroundColor: params?.backgroundColor,\n });\n }\n}\n/**\n * Find the single node that is not a target of any edge.\n * Exclude nodes/sources with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the origin.\n */\nfunction _firstNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const targets = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.source))\n .map((edge) => edge.target)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !targets.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n\n/**\n * Find the single node that is not a source of any edge.\n * Exclude nodes/targets with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the destination.\n */\nfunction _lastNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const sources = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.target))\n .map((edge) => edge.source)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !sources.has(node.id))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IAAI,CAAC,QAAQ,IAAI,KAAK,GAAG,EACrD,MAAM,KAAK,KAAK;AAGpB,QAAO,MAAM,WAAW,IAAI,MAAM,KAAK;AACxC;;;;;;;AAQD,SAAS,UAAUV,OAAcS,UAAoB,CAAE,GAAoB;CACzE,MAAM,UAAU,IAAI,IAClB,MAAM,MACH,OAAO,CAAC,SAAS,CAAC,QAAQ,SAAS,KAAK,OAAO,CAAC,CAChD,IAAI,CAAC,SAAS,KAAK,OAAO;CAG/B,MAAMC,QAAgB,CAAE;AACxB,MAAK,MAAM,QAAQ,OAAO,OAAO,MAAM,MAAM,CAC3C,KAAI,CAAC,QAAQ,SAAS,KAAK,GAAG,IAAI,CAAC,QAAQ,IAAI,KAAK,GAAG,EACrD,MAAM,KAAK,KAAK;AAGpB,QAAO,MAAM,WAAW,IAAI,MAAM,KAAK;AACxC"}
|
|
1
|
+
{"version":3,"file":"graph.cjs","names":["id: string | undefined","data: RunnableInterface | RunnableIOSchema","isRunnableInterface","node: Node","toJsonSchema","params?: { nodes: Record<string, Node>; edges: Edge[] }","stableNodeIds: Record<string, string | number>","item: Record<string, unknown>","id?: string","metadata?: Record<string, any>","source: Node","target: Node","data?: string","conditional?: boolean","edge: Edge","graph: Graph","isUuid","id: string","nodeLabels: Record<string, string>","nodeId: string","params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }","drawMermaid","params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n backgroundColor?: string;\n }","drawMermaidImage","exclude: string[]","found: Node[]"],"sources":["../../src/runnables/graph.ts"],"sourcesContent":["import { v4 as uuidv4, validate as isUuid } from \"uuid\";\nimport type {\n RunnableInterface,\n RunnableIOSchema,\n Node,\n Edge,\n} from \"./types.js\";\nimport { isRunnableInterface } from \"./utils.js\";\nimport { drawMermaid, drawMermaidImage } from \"./graph_mermaid.js\";\nimport { toJsonSchema } from \"../utils/json_schema.js\";\n\nexport { Node, Edge };\n\nfunction nodeDataStr(\n id: string | undefined,\n data: RunnableInterface | RunnableIOSchema\n): string {\n if (id !== undefined && !isUuid(id)) {\n return id;\n } else if (isRunnableInterface(data)) {\n try {\n let dataStr = data.getName();\n dataStr = dataStr.startsWith(\"Runnable\")\n ? dataStr.slice(\"Runnable\".length)\n : dataStr;\n return dataStr;\n } catch {\n return data.getName();\n }\n } else {\n return data.name ?? \"UnknownSchema\";\n }\n}\n\nfunction nodeDataJson(node: Node) {\n // if node.data implements Runnable\n if (isRunnableInterface(node.data)) {\n return {\n type: \"runnable\",\n data: {\n id: node.data.lc_id,\n name: node.data.getName(),\n },\n };\n } else {\n return {\n type: \"schema\",\n data: { ...toJsonSchema(node.data.schema), title: node.data.name },\n };\n }\n}\n\nexport class Graph {\n nodes: Record<string, Node> = {};\n\n edges: Edge[] = [];\n\n constructor(params?: { nodes: Record<string, Node>; edges: Edge[] }) {\n this.nodes = params?.nodes ?? this.nodes;\n this.edges = params?.edges ?? this.edges;\n }\n\n // Convert the graph to a JSON-serializable format.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n toJSON(): Record<string, any> {\n const stableNodeIds: Record<string, string | number> = {};\n Object.values(this.nodes).forEach((node, i) => {\n stableNodeIds[node.id] = isUuid(node.id) ? i : node.id;\n });\n\n return {\n nodes: Object.values(this.nodes).map((node) => ({\n id: stableNodeIds[node.id],\n ...nodeDataJson(node),\n })),\n edges: this.edges.map((edge) => {\n const item: Record<string, unknown> = {\n source: stableNodeIds[edge.source],\n target: stableNodeIds[edge.target],\n };\n\n if (typeof edge.data !== \"undefined\") {\n item.data = edge.data;\n }\n\n if (typeof edge.conditional !== \"undefined\") {\n item.conditional = edge.conditional;\n }\n return item;\n }),\n };\n }\n\n addNode(\n data: RunnableInterface | RunnableIOSchema,\n id?: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n metadata?: Record<string, any>\n ): Node {\n if (id !== undefined && this.nodes[id] !== undefined) {\n throw new Error(`Node with id ${id} already exists`);\n }\n const nodeId = id ?? uuidv4();\n const node: Node = {\n id: nodeId,\n data,\n name: nodeDataStr(id, data),\n metadata,\n };\n this.nodes[nodeId] = node;\n return node;\n }\n\n removeNode(node: Node): void {\n // Remove the node from the nodes map\n delete this.nodes[node.id];\n\n // Filter out edges connected to the node\n this.edges = this.edges.filter(\n (edge) => edge.source !== node.id && edge.target !== node.id\n );\n }\n\n addEdge(\n source: Node,\n target: Node,\n data?: string,\n conditional?: boolean\n ): Edge {\n if (this.nodes[source.id] === undefined) {\n throw new Error(`Source node ${source.id} not in graph`);\n }\n if (this.nodes[target.id] === undefined) {\n throw new Error(`Target node ${target.id} not in graph`);\n }\n const edge: Edge = {\n source: source.id,\n target: target.id,\n data,\n conditional,\n };\n this.edges.push(edge);\n return edge;\n }\n\n firstNode(): Node | undefined {\n return _firstNode(this);\n }\n\n lastNode(): Node | undefined {\n return _lastNode(this);\n }\n\n /**\n * Add all nodes and edges from another graph.\n * Note this doesn't check for duplicates, nor does it connect the graphs.\n */\n extend(graph: Graph, prefix = \"\") {\n let finalPrefix = prefix;\n const nodeIds = Object.values(graph.nodes).map((node) => node.id);\n if (nodeIds.every(isUuid)) {\n finalPrefix = \"\";\n }\n\n const prefixed = (id: string) => {\n return finalPrefix ? `${finalPrefix}:${id}` : id;\n };\n\n Object.entries(graph.nodes).forEach(([key, value]) => {\n this.nodes[prefixed(key)] = { ...value, id: prefixed(key) };\n });\n\n const newEdges = graph.edges.map((edge) => {\n return {\n ...edge,\n source: prefixed(edge.source),\n target: prefixed(edge.target),\n };\n });\n // Add all edges from the other graph\n this.edges = [...this.edges, ...newEdges];\n const first = graph.firstNode();\n const last = graph.lastNode();\n return [\n first ? { id: prefixed(first.id), data: first.data } : undefined,\n last ? { id: prefixed(last.id), data: last.data } : undefined,\n ];\n }\n\n trimFirstNode(): void {\n const firstNode = this.firstNode();\n if (firstNode && _firstNode(this, [firstNode.id])) {\n this.removeNode(firstNode);\n }\n }\n\n trimLastNode(): void {\n const lastNode = this.lastNode();\n if (lastNode && _lastNode(this, [lastNode.id])) {\n this.removeNode(lastNode);\n }\n }\n\n /**\n * Return a new graph with all nodes re-identified,\n * using their unique, readable names where possible.\n */\n reid(): Graph {\n const nodeLabels: Record<string, string> = Object.fromEntries(\n Object.values(this.nodes).map((node) => [node.id, node.name])\n );\n const nodeLabelCounts = new Map<string, number>();\n Object.values(nodeLabels).forEach((label) => {\n nodeLabelCounts.set(label, (nodeLabelCounts.get(label) || 0) + 1);\n });\n\n const getNodeId = (nodeId: string): string => {\n const label = nodeLabels[nodeId];\n if (isUuid(nodeId) && nodeLabelCounts.get(label) === 1) {\n return label;\n } else {\n return nodeId;\n }\n };\n\n return new Graph({\n nodes: Object.fromEntries(\n Object.entries(this.nodes).map(([id, node]) => [\n getNodeId(id),\n { ...node, id: getNodeId(id) },\n ])\n ),\n edges: this.edges.map((edge) => ({\n ...edge,\n source: getNodeId(edge.source),\n target: getNodeId(edge.target),\n })),\n });\n }\n\n drawMermaid(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }): string {\n const {\n withStyles,\n curveStyle,\n nodeColors = {\n default: \"fill:#f2f0ff,line-height:1.2\",\n first: \"fill-opacity:0\",\n last: \"fill:#bfb6fc\",\n },\n wrapLabelNWords,\n } = params ?? {};\n const graph = this.reid();\n const firstNode = graph.firstNode();\n\n const lastNode = graph.lastNode();\n\n return drawMermaid(graph.nodes, graph.edges, {\n firstNode: firstNode?.id,\n lastNode: lastNode?.id,\n withStyles,\n curveStyle,\n nodeColors,\n wrapLabelNWords,\n });\n }\n\n async drawMermaidPng(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n backgroundColor?: string;\n }): Promise<Blob> {\n const mermaidSyntax = this.drawMermaid(params);\n return drawMermaidImage(mermaidSyntax, {\n backgroundColor: params?.backgroundColor,\n });\n }\n}\n/**\n * Find the single node that is not a target of any edge.\n * Exclude nodes/sources with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the origin.\n */\nfunction _firstNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const targets = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.source))\n .map((edge) => edge.target)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !targets.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n\n/**\n * Find the single node that is not a source of any edge.\n * Exclude nodes/targets with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the destination.\n */\nfunction _lastNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const sources = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.target))\n .map((edge) => edge.source)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !sources.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n"],"mappings":";;;;;;;;;AAaA,SAAS,YACPA,IACAC,MACQ;AACR,KAAI,OAAO,UAAa,oBAAQ,GAAG,CACjC,QAAO;UACEC,kCAAoB,KAAK,CAClC,KAAI;EACF,IAAI,UAAU,KAAK,SAAS;EAC5B,UAAU,QAAQ,WAAW,WAAW,GACpC,QAAQ,MAAM,EAAkB,GAChC;AACJ,SAAO;CACR,QAAO;AACN,SAAO,KAAK,SAAS;CACtB;KAED,QAAO,KAAK,QAAQ;AAEvB;AAED,SAAS,aAAaC,MAAY;AAEhC,KAAID,kCAAoB,KAAK,KAAK,CAChC,QAAO;EACL,MAAM;EACN,MAAM;GACJ,IAAI,KAAK,KAAK;GACd,MAAM,KAAK,KAAK,SAAS;EAC1B;CACF;KAED,QAAO;EACL,MAAM;EACN,MAAM;GAAE,GAAGE,uCAAa,KAAK,KAAK,OAAO;GAAE,OAAO,KAA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} from "../_virtual/rolldown_runtime.js";
|
|
2
2
|
import { isRunnableInterface } from "./utils.js";
|
|
3
|
-
import { drawMermaid,
|
|
3
|
+
import { drawMermaid, drawMermaidImage } from "./graph_mermaid.js";
|
|
4
4
|
import { toJsonSchema } from "../utils/json_schema.js";
|
|
5
5
|
import { v4, validate } from "uuid";
|
|
6
6
|
|
|
@@ -186,7 +186,7 @@ var Graph = class Graph {
|
|
|
186
186
|
}
|
|
187
187
|
async drawMermaidPng(params) {
|
|
188
188
|
const mermaidSyntax = this.drawMermaid(params);
|
|
189
|
-
return
|
|
189
|
+
return drawMermaidImage(mermaidSyntax, { backgroundColor: params?.backgroundColor });
|
|
190
190
|
}
|
|
191
191
|
};
|
|
192
192
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph.js","names":["id: string | undefined","data: RunnableInterface | RunnableIOSchema","isUuid","node: Node","params?: { nodes: Record<string, Node>; edges: Edge[] }","stableNodeIds: Record<string, string | number>","item: Record<string, unknown>","id?: string","metadata?: Record<string, any>","uuidv4","source: Node","target: Node","data?: string","conditional?: boolean","edge: Edge","graph: Graph","id: string","nodeLabels: Record<string, string>","nodeId: string","params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }","params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n backgroundColor?: string;\n }","exclude: string[]","found: Node[]"],"sources":["../../src/runnables/graph.ts"],"sourcesContent":["import { v4 as uuidv4, validate as isUuid } from \"uuid\";\nimport type {\n RunnableInterface,\n RunnableIOSchema,\n Node,\n Edge,\n} from \"./types.js\";\nimport { isRunnableInterface } from \"./utils.js\";\nimport { drawMermaid, drawMermaidPng } from \"./graph_mermaid.js\";\nimport { toJsonSchema } from \"../utils/json_schema.js\";\n\nexport { Node, Edge };\n\nfunction nodeDataStr(\n id: string | undefined,\n data: RunnableInterface | RunnableIOSchema\n): string {\n if (id !== undefined && !isUuid(id)) {\n return id;\n } else if (isRunnableInterface(data)) {\n try {\n let dataStr = data.getName();\n dataStr = dataStr.startsWith(\"Runnable\")\n ? dataStr.slice(\"Runnable\".length)\n : dataStr;\n return dataStr;\n } catch {\n return data.getName();\n }\n } else {\n return data.name ?? \"UnknownSchema\";\n }\n}\n\nfunction nodeDataJson(node: Node) {\n // if node.data implements Runnable\n if (isRunnableInterface(node.data)) {\n return {\n type: \"runnable\",\n data: {\n id: node.data.lc_id,\n name: node.data.getName(),\n },\n };\n } else {\n return {\n type: \"schema\",\n data: { ...toJsonSchema(node.data.schema), title: node.data.name },\n };\n }\n}\n\nexport class Graph {\n nodes: Record<string, Node> = {};\n\n edges: Edge[] = [];\n\n constructor(params?: { nodes: Record<string, Node>; edges: Edge[] }) {\n this.nodes = params?.nodes ?? this.nodes;\n this.edges = params?.edges ?? this.edges;\n }\n\n // Convert the graph to a JSON-serializable format.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n toJSON(): Record<string, any> {\n const stableNodeIds: Record<string, string | number> = {};\n Object.values(this.nodes).forEach((node, i) => {\n stableNodeIds[node.id] = isUuid(node.id) ? i : node.id;\n });\n\n return {\n nodes: Object.values(this.nodes).map((node) => ({\n id: stableNodeIds[node.id],\n ...nodeDataJson(node),\n })),\n edges: this.edges.map((edge) => {\n const item: Record<string, unknown> = {\n source: stableNodeIds[edge.source],\n target: stableNodeIds[edge.target],\n };\n\n if (typeof edge.data !== \"undefined\") {\n item.data = edge.data;\n }\n\n if (typeof edge.conditional !== \"undefined\") {\n item.conditional = edge.conditional;\n }\n return item;\n }),\n };\n }\n\n addNode(\n data: RunnableInterface | RunnableIOSchema,\n id?: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n metadata?: Record<string, any>\n ): Node {\n if (id !== undefined && this.nodes[id] !== undefined) {\n throw new Error(`Node with id ${id} already exists`);\n }\n const nodeId = id ?? uuidv4();\n const node: Node = {\n id: nodeId,\n data,\n name: nodeDataStr(id, data),\n metadata,\n };\n this.nodes[nodeId] = node;\n return node;\n }\n\n removeNode(node: Node): void {\n // Remove the node from the nodes map\n delete this.nodes[node.id];\n\n // Filter out edges connected to the node\n this.edges = this.edges.filter(\n (edge) => edge.source !== node.id && edge.target !== node.id\n );\n }\n\n addEdge(\n source: Node,\n target: Node,\n data?: string,\n conditional?: boolean\n ): Edge {\n if (this.nodes[source.id] === undefined) {\n throw new Error(`Source node ${source.id} not in graph`);\n }\n if (this.nodes[target.id] === undefined) {\n throw new Error(`Target node ${target.id} not in graph`);\n }\n const edge: Edge = {\n source: source.id,\n target: target.id,\n data,\n conditional,\n };\n this.edges.push(edge);\n return edge;\n }\n\n firstNode(): Node | undefined {\n return _firstNode(this);\n }\n\n lastNode(): Node | undefined {\n return _lastNode(this);\n }\n\n /**\n * Add all nodes and edges from another graph.\n * Note this doesn't check for duplicates, nor does it connect the graphs.\n */\n extend(graph: Graph, prefix = \"\") {\n let finalPrefix = prefix;\n const nodeIds = Object.values(graph.nodes).map((node) => node.id);\n if (nodeIds.every(isUuid)) {\n finalPrefix = \"\";\n }\n\n const prefixed = (id: string) => {\n return finalPrefix ? `${finalPrefix}:${id}` : id;\n };\n\n Object.entries(graph.nodes).forEach(([key, value]) => {\n this.nodes[prefixed(key)] = { ...value, id: prefixed(key) };\n });\n\n const newEdges = graph.edges.map((edge) => {\n return {\n ...edge,\n source: prefixed(edge.source),\n target: prefixed(edge.target),\n };\n });\n // Add all edges from the other graph\n this.edges = [...this.edges, ...newEdges];\n const first = graph.firstNode();\n const last = graph.lastNode();\n return [\n first ? { id: prefixed(first.id), data: first.data } : undefined,\n last ? { id: prefixed(last.id), data: last.data } : undefined,\n ];\n }\n\n trimFirstNode(): void {\n const firstNode = this.firstNode();\n if (firstNode && _firstNode(this, [firstNode.id])) {\n this.removeNode(firstNode);\n }\n }\n\n trimLastNode(): void {\n const lastNode = this.lastNode();\n if (lastNode && _lastNode(this, [lastNode.id])) {\n this.removeNode(lastNode);\n }\n }\n\n /**\n * Return a new graph with all nodes re-identified,\n * using their unique, readable names where possible.\n */\n reid(): Graph {\n const nodeLabels: Record<string, string> = Object.fromEntries(\n Object.values(this.nodes).map((node) => [node.id, node.name])\n );\n const nodeLabelCounts = new Map<string, number>();\n Object.values(nodeLabels).forEach((label) => {\n nodeLabelCounts.set(label, (nodeLabelCounts.get(label) || 0) + 1);\n });\n\n const getNodeId = (nodeId: string): string => {\n const label = nodeLabels[nodeId];\n if (isUuid(nodeId) && nodeLabelCounts.get(label) === 1) {\n return label;\n } else {\n return nodeId;\n }\n };\n\n return new Graph({\n nodes: Object.fromEntries(\n Object.entries(this.nodes).map(([id, node]) => [\n getNodeId(id),\n { ...node, id: getNodeId(id) },\n ])\n ),\n edges: this.edges.map((edge) => ({\n ...edge,\n source: getNodeId(edge.source),\n target: getNodeId(edge.target),\n })),\n });\n }\n\n drawMermaid(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }): string {\n const {\n withStyles,\n curveStyle,\n nodeColors = {\n default: \"fill:#f2f0ff,line-height:1.2\",\n first: \"fill-opacity:0\",\n last: \"fill:#bfb6fc\",\n },\n wrapLabelNWords,\n } = params ?? {};\n const graph = this.reid();\n const firstNode = graph.firstNode();\n\n const lastNode = graph.lastNode();\n\n return drawMermaid(graph.nodes, graph.edges, {\n firstNode: firstNode?.id,\n lastNode: lastNode?.id,\n withStyles,\n curveStyle,\n nodeColors,\n wrapLabelNWords,\n });\n }\n\n async drawMermaidPng(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n backgroundColor?: string;\n }): Promise<Blob> {\n const mermaidSyntax = this.drawMermaid(params);\n return drawMermaidPng(mermaidSyntax, {\n backgroundColor: params?.backgroundColor,\n });\n }\n}\n/**\n * Find the single node that is not a target of any edge.\n * Exclude nodes/sources with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the origin.\n */\nfunction _firstNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const targets = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.source))\n .map((edge) => edge.target)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !targets.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n\n/**\n * Find the single node that is not a source of any edge.\n * Exclude nodes/targets with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the destination.\n */\nfunction _lastNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const sources = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.target))\n .map((edge) => edge.source)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !sources.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n"],"mappings":";;;;;;;;;AAaA,SAAS,YACPA,IACAC,MACQ;AACR,KAAI,OAAO,UAAa,CAACC,SAAO,GAAG,CACjC,QAAO;UACE,oBAAoB,KAAK,CAClC,KAAI;EACF,IAAI,UAAU,KAAK,SAAS;EAC5B,UAAU,QAAQ,WAAW,WAAW,GACpC,QAAQ,MAAM,EAAkB,GAChC;AACJ,SAAO;CACR,QAAO;AACN,SAAO,KAAK,SAAS;CACtB;KAED,QAAO,KAAK,QAAQ;AAEvB;AAED,SAAS,aAAaC,MAAY;AAEhC,KAAI,oBAAoB,KAAK,KAAK,CAChC,QAAO;EACL,MAAM;EACN,MAAM;GACJ,IAAI,KAAK,KAAK;GACd,MAAM,KAAK,KAAK,SAAS;EAC1B;CACF;KAED,QAAO;EACL,MAAM;EACN,MAAM;GAAE,GAAG,aAAa,KAAK,KAAK,OAAO;GAAE,OAAO,KAAK,KAAK;EAAM;CACnE;AAEJ;AAED,IAAa,QAAb,MAAa,MAAM;CACjB,QAA8B,CAAE;CAEhC,QAAg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;AAEjC,SAAO,YAAY,MAAM,OAAO,MAAM,OAAO;GAC3C,WAAW,WAAW;GACtB,UAAU,UAAU;GACpB;GACA;GACA;GACA;EACD,EAAC;CACH;CAED,MAAM,eAAeC,QAMH;EAChB,MAAM,gBAAgB,KAAK,YAAY,OAAO;AAC9C,SAAO,eAAe,eAAe,EACnC,iBAAiB,QAAQ,gBAC1B,EAAC;CACH;AACF;;;;;;;AAOD,SAAS,WAAWL,OAAcM,UAAoB,CAAE,GAAoB;CAC1E,MAAM,UAAU,IAAI,IAClB,MAAM,MACH,OAAO,CAAC,SAAS,CAAC,QAAQ,SAAS,KAAK,OAAO,CAAC,CAChD,IAAI,CAAC,SAAS,KAAK,OAAO;CAG/B,MAAMC,QAAgB,CAAE;AACxB,MAAK,MAAM,QAAQ,OAAO,OAAO,MAAM,MAAM,CAC3C,KAAI,CAAC,QAAQ,SAAS,KAAK,GAAG,IAAI,CAAC,QAAQ,IAAI,KAAK,GAAG,EACrD,MAAM,KAAK,KAAK;AAGpB,QAAO,MAAM,WAAW,IAAI,MAAM,KAAK;AACxC;;;;;;;AAQD,SAAS,UAAUP,OAAcM,UAAoB,CAAE,GAAoB;CACzE,MAAM,UAAU,IAAI,IAClB,MAAM,MACH,OAAO,CAAC,SAAS,CAAC,QAAQ,SAAS,KAAK,OAAO,CAAC,CAChD,IAAI,CAAC,SAAS,KAAK,OAAO;CAG/B,MAAMC,QAAgB,CAAE;AACxB,MAAK,MAAM,QAAQ,OAAO,OAAO,MAAM,MAAM,CAC3C,KAAI,CAAC,QAAQ,SAAS,KAAK,GAAG,IAAI,CAAC,QAAQ,IAAI,KAAK,GAAG,EACrD,MAAM,KAAK,KAAK;AAGpB,QAAO,MAAM,WAAW,IAAI,MAAM,KAAK;AACxC"}
|
|
1
|
+
{"version":3,"file":"graph.js","names":["id: string | undefined","data: RunnableInterface | RunnableIOSchema","isUuid","node: Node","params?: { nodes: Record<string, Node>; edges: Edge[] }","stableNodeIds: Record<string, string | number>","item: Record<string, unknown>","id?: string","metadata?: Record<string, any>","uuidv4","source: Node","target: Node","data?: string","conditional?: boolean","edge: Edge","graph: Graph","id: string","nodeLabels: Record<string, string>","nodeId: string","params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }","params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n backgroundColor?: string;\n }","exclude: string[]","found: Node[]"],"sources":["../../src/runnables/graph.ts"],"sourcesContent":["import { v4 as uuidv4, validate as isUuid } from \"uuid\";\nimport type {\n RunnableInterface,\n RunnableIOSchema,\n Node,\n Edge,\n} from \"./types.js\";\nimport { isRunnableInterface } from \"./utils.js\";\nimport { drawMermaid, drawMermaidImage } from \"./graph_mermaid.js\";\nimport { toJsonSchema } from \"../utils/json_schema.js\";\n\nexport { Node, Edge };\n\nfunction nodeDataStr(\n id: string | undefined,\n data: RunnableInterface | RunnableIOSchema\n): string {\n if (id !== undefined && !isUuid(id)) {\n return id;\n } else if (isRunnableInterface(data)) {\n try {\n let dataStr = data.getName();\n dataStr = dataStr.startsWith(\"Runnable\")\n ? dataStr.slice(\"Runnable\".length)\n : dataStr;\n return dataStr;\n } catch {\n return data.getName();\n }\n } else {\n return data.name ?? \"UnknownSchema\";\n }\n}\n\nfunction nodeDataJson(node: Node) {\n // if node.data implements Runnable\n if (isRunnableInterface(node.data)) {\n return {\n type: \"runnable\",\n data: {\n id: node.data.lc_id,\n name: node.data.getName(),\n },\n };\n } else {\n return {\n type: \"schema\",\n data: { ...toJsonSchema(node.data.schema), title: node.data.name },\n };\n }\n}\n\nexport class Graph {\n nodes: Record<string, Node> = {};\n\n edges: Edge[] = [];\n\n constructor(params?: { nodes: Record<string, Node>; edges: Edge[] }) {\n this.nodes = params?.nodes ?? this.nodes;\n this.edges = params?.edges ?? this.edges;\n }\n\n // Convert the graph to a JSON-serializable format.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n toJSON(): Record<string, any> {\n const stableNodeIds: Record<string, string | number> = {};\n Object.values(this.nodes).forEach((node, i) => {\n stableNodeIds[node.id] = isUuid(node.id) ? i : node.id;\n });\n\n return {\n nodes: Object.values(this.nodes).map((node) => ({\n id: stableNodeIds[node.id],\n ...nodeDataJson(node),\n })),\n edges: this.edges.map((edge) => {\n const item: Record<string, unknown> = {\n source: stableNodeIds[edge.source],\n target: stableNodeIds[edge.target],\n };\n\n if (typeof edge.data !== \"undefined\") {\n item.data = edge.data;\n }\n\n if (typeof edge.conditional !== \"undefined\") {\n item.conditional = edge.conditional;\n }\n return item;\n }),\n };\n }\n\n addNode(\n data: RunnableInterface | RunnableIOSchema,\n id?: string,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n metadata?: Record<string, any>\n ): Node {\n if (id !== undefined && this.nodes[id] !== undefined) {\n throw new Error(`Node with id ${id} already exists`);\n }\n const nodeId = id ?? uuidv4();\n const node: Node = {\n id: nodeId,\n data,\n name: nodeDataStr(id, data),\n metadata,\n };\n this.nodes[nodeId] = node;\n return node;\n }\n\n removeNode(node: Node): void {\n // Remove the node from the nodes map\n delete this.nodes[node.id];\n\n // Filter out edges connected to the node\n this.edges = this.edges.filter(\n (edge) => edge.source !== node.id && edge.target !== node.id\n );\n }\n\n addEdge(\n source: Node,\n target: Node,\n data?: string,\n conditional?: boolean\n ): Edge {\n if (this.nodes[source.id] === undefined) {\n throw new Error(`Source node ${source.id} not in graph`);\n }\n if (this.nodes[target.id] === undefined) {\n throw new Error(`Target node ${target.id} not in graph`);\n }\n const edge: Edge = {\n source: source.id,\n target: target.id,\n data,\n conditional,\n };\n this.edges.push(edge);\n return edge;\n }\n\n firstNode(): Node | undefined {\n return _firstNode(this);\n }\n\n lastNode(): Node | undefined {\n return _lastNode(this);\n }\n\n /**\n * Add all nodes and edges from another graph.\n * Note this doesn't check for duplicates, nor does it connect the graphs.\n */\n extend(graph: Graph, prefix = \"\") {\n let finalPrefix = prefix;\n const nodeIds = Object.values(graph.nodes).map((node) => node.id);\n if (nodeIds.every(isUuid)) {\n finalPrefix = \"\";\n }\n\n const prefixed = (id: string) => {\n return finalPrefix ? `${finalPrefix}:${id}` : id;\n };\n\n Object.entries(graph.nodes).forEach(([key, value]) => {\n this.nodes[prefixed(key)] = { ...value, id: prefixed(key) };\n });\n\n const newEdges = graph.edges.map((edge) => {\n return {\n ...edge,\n source: prefixed(edge.source),\n target: prefixed(edge.target),\n };\n });\n // Add all edges from the other graph\n this.edges = [...this.edges, ...newEdges];\n const first = graph.firstNode();\n const last = graph.lastNode();\n return [\n first ? { id: prefixed(first.id), data: first.data } : undefined,\n last ? { id: prefixed(last.id), data: last.data } : undefined,\n ];\n }\n\n trimFirstNode(): void {\n const firstNode = this.firstNode();\n if (firstNode && _firstNode(this, [firstNode.id])) {\n this.removeNode(firstNode);\n }\n }\n\n trimLastNode(): void {\n const lastNode = this.lastNode();\n if (lastNode && _lastNode(this, [lastNode.id])) {\n this.removeNode(lastNode);\n }\n }\n\n /**\n * Return a new graph with all nodes re-identified,\n * using their unique, readable names where possible.\n */\n reid(): Graph {\n const nodeLabels: Record<string, string> = Object.fromEntries(\n Object.values(this.nodes).map((node) => [node.id, node.name])\n );\n const nodeLabelCounts = new Map<string, number>();\n Object.values(nodeLabels).forEach((label) => {\n nodeLabelCounts.set(label, (nodeLabelCounts.get(label) || 0) + 1);\n });\n\n const getNodeId = (nodeId: string): string => {\n const label = nodeLabels[nodeId];\n if (isUuid(nodeId) && nodeLabelCounts.get(label) === 1) {\n return label;\n } else {\n return nodeId;\n }\n };\n\n return new Graph({\n nodes: Object.fromEntries(\n Object.entries(this.nodes).map(([id, node]) => [\n getNodeId(id),\n { ...node, id: getNodeId(id) },\n ])\n ),\n edges: this.edges.map((edge) => ({\n ...edge,\n source: getNodeId(edge.source),\n target: getNodeId(edge.target),\n })),\n });\n }\n\n drawMermaid(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }): string {\n const {\n withStyles,\n curveStyle,\n nodeColors = {\n default: \"fill:#f2f0ff,line-height:1.2\",\n first: \"fill-opacity:0\",\n last: \"fill:#bfb6fc\",\n },\n wrapLabelNWords,\n } = params ?? {};\n const graph = this.reid();\n const firstNode = graph.firstNode();\n\n const lastNode = graph.lastNode();\n\n return drawMermaid(graph.nodes, graph.edges, {\n firstNode: firstNode?.id,\n lastNode: lastNode?.id,\n withStyles,\n curveStyle,\n nodeColors,\n wrapLabelNWords,\n });\n }\n\n async drawMermaidPng(params?: {\n withStyles?: boolean;\n curveStyle?: string;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n backgroundColor?: string;\n }): Promise<Blob> {\n const mermaidSyntax = this.drawMermaid(params);\n return drawMermaidImage(mermaidSyntax, {\n backgroundColor: params?.backgroundColor,\n });\n }\n}\n/**\n * Find the single node that is not a target of any edge.\n * Exclude nodes/sources with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the origin.\n */\nfunction _firstNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const targets = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.source))\n .map((edge) => edge.target)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !targets.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n\n/**\n * Find the single node that is not a source of any edge.\n * Exclude nodes/targets with ids in the exclude list.\n * If there is no such node, or there are multiple, return undefined.\n * When drawing the graph, this node would be the destination.\n */\nfunction _lastNode(graph: Graph, exclude: string[] = []): Node | undefined {\n const sources = new Set(\n graph.edges\n .filter((edge) => !exclude.includes(edge.target))\n .map((edge) => edge.source)\n );\n\n const found: Node[] = [];\n for (const node of Object.values(graph.nodes)) {\n if (!exclude.includes(node.id) && !sources.has(node.id)) {\n found.push(node);\n }\n }\n return found.length === 1 ? found[0] : undefined;\n}\n"],"mappings":";;;;;;;;;AAaA,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KAAK;AAGpB,QAAO,MAAM,WAAW,IAAI,MAAM,KAAK;AACxC;;;;;;;AAQD,SAAS,UAAUP,OAAcM,UAAoB,CAAE,GAAoB;CACzE,MAAM,UAAU,IAAI,IAClB,MAAM,MACH,OAAO,CAAC,SAAS,CAAC,QAAQ,SAAS,KAAK,OAAO,CAAC,CAChD,IAAI,CAAC,SAAS,KAAK,OAAO;CAG/B,MAAMC,QAAgB,CAAE;AACxB,MAAK,MAAM,QAAQ,OAAO,OAAO,MAAM,MAAM,CAC3C,KAAI,CAAC,QAAQ,SAAS,KAAK,GAAG,IAAI,CAAC,QAAQ,IAAI,KAAK,GAAG,EACrD,MAAM,KAAK,KAAK;AAGpB,QAAO,MAAM,WAAW,IAAI,MAAM,KAAK;AACxC"}
|
|
@@ -70,15 +70,6 @@ function drawMermaid(nodes, edges, config) {
|
|
|
70
70
|
return mermaidGraph;
|
|
71
71
|
}
|
|
72
72
|
/**
|
|
73
|
-
* @deprecated Use `drawMermaidImage` instead.
|
|
74
|
-
*/
|
|
75
|
-
async function drawMermaidPng(mermaidSyntax, config) {
|
|
76
|
-
return drawMermaidImage(mermaidSyntax, {
|
|
77
|
-
...config,
|
|
78
|
-
imageType: "png"
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
73
|
* Renders Mermaid graph using the Mermaid.INK API.
|
|
83
74
|
*
|
|
84
75
|
* @example
|
|
@@ -115,5 +106,5 @@ async function drawMermaidImage(mermaidSyntax, config) {
|
|
|
115
106
|
|
|
116
107
|
//#endregion
|
|
117
108
|
exports.drawMermaid = drawMermaid;
|
|
118
|
-
exports.
|
|
109
|
+
exports.drawMermaidImage = drawMermaidImage;
|
|
119
110
|
//# sourceMappingURL=graph_mermaid.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph_mermaid.cjs","names":["nodeLabel: string","nodeColors: Record<string, string>","nodes: Record<string, Node>","edges: Edge[]","config?: {\n firstNode?: string;\n lastNode?: string;\n curveStyle?: string;\n withStyles?: boolean;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }","formatDict: Record<string, string>","edgeGroups: Record<string, Edge[]>","prefix: string","edges","mermaidSyntax: string","config?: {\n backgroundColor?: string;\n }","config?: {\n /**\n * The type of image to render.\n * @default \"png\"\n */\n imageType?: \"png\" | \"jpeg\" | \"webp\";\n backgroundColor?: string;\n }"],"sources":["../../src/runnables/graph_mermaid.ts"],"sourcesContent":["import { Edge, Node } from \"./types.js\";\n\nfunction _escapeNodeLabel(nodeLabel: string): string {\n // Escapes the node label for Mermaid syntax.\n return nodeLabel.replace(/[^a-zA-Z-_0-9]/g, \"_\");\n}\n\nconst MARKDOWN_SPECIAL_CHARS = [\"*\", \"_\", \"`\"];\n\nfunction _generateMermaidGraphStyles(\n nodeColors: Record<string, string>\n): string {\n let styles = \"\";\n for (const [className, color] of Object.entries(nodeColors)) {\n styles += `\\tclassDef ${className} ${color};\\n`;\n }\n return styles;\n}\n\n/**\n * Draws a Mermaid graph using the provided graph data\n */\nexport function drawMermaid(\n nodes: Record<string, Node>,\n edges: Edge[],\n config?: {\n firstNode?: string;\n lastNode?: string;\n curveStyle?: string;\n withStyles?: boolean;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }\n): string {\n const {\n firstNode,\n lastNode,\n nodeColors,\n withStyles = true,\n curveStyle = \"linear\",\n wrapLabelNWords = 9,\n } = config ?? {};\n // Initialize Mermaid graph configuration\n let mermaidGraph = withStyles\n ? `%%{init: {'flowchart': {'curve': '${curveStyle}'}}}%%\\ngraph TD;\\n`\n : \"graph TD;\\n\";\n if (withStyles) {\n // Node formatting templates\n const defaultClassLabel = \"default\";\n const formatDict: Record<string, string> = {\n [defaultClassLabel]: \"{0}({1})\",\n };\n if (firstNode !== undefined) {\n formatDict[firstNode] = \"{0}([{1}]):::first\";\n }\n if (lastNode !== undefined) {\n formatDict[lastNode] = \"{0}([{1}]):::last\";\n }\n\n // Add nodes to the graph\n for (const [key, node] of Object.entries(nodes)) {\n const nodeName = node.name.split(\":\").pop() ?? \"\";\n const label = MARKDOWN_SPECIAL_CHARS.some(\n (char) => nodeName.startsWith(char) && nodeName.endsWith(char)\n )\n ? `<p>${nodeName}</p>`\n : nodeName;\n\n let finalLabel = label;\n if (Object.keys(node.metadata ?? {}).length) {\n finalLabel += `<hr/><small><em>${Object.entries(node.metadata ?? {})\n .map(([k, v]) => `${k} = ${v}`)\n .join(\"\\n\")}</em></small>`;\n }\n\n const nodeLabel = (formatDict[key] ?? formatDict[defaultClassLabel])\n .replace(\"{0}\", _escapeNodeLabel(key))\n .replace(\"{1}\", finalLabel);\n\n mermaidGraph += `\\t${nodeLabel}\\n`;\n }\n }\n\n // Group edges by their common prefixes\n const edgeGroups: Record<string, Edge[]> = {};\n for (const edge of edges) {\n const srcParts = edge.source.split(\":\");\n const tgtParts = edge.target.split(\":\");\n const commonPrefix = srcParts\n .filter((src, i) => src === tgtParts[i])\n .join(\":\");\n if (!edgeGroups[commonPrefix]) {\n edgeGroups[commonPrefix] = [];\n }\n edgeGroups[commonPrefix].push(edge);\n }\n\n const seenSubgraphs = new Set<string>();\n\n function addSubgraph(edges: Edge[], prefix: string): void {\n const selfLoop = edges.length === 1 && edges[0].source === edges[0].target;\n if (prefix && !selfLoop) {\n const subgraph = prefix.split(\":\").pop()!;\n if (seenSubgraphs.has(subgraph)) {\n throw new Error(\n `Found duplicate subgraph '${subgraph}' -- this likely means that ` +\n \"you're reusing a subgraph node with the same name. \" +\n \"Please adjust your graph to have subgraph nodes with unique names.\"\n );\n }\n\n seenSubgraphs.add(subgraph);\n mermaidGraph += `\\tsubgraph ${subgraph}\\n`;\n }\n\n for (const edge of edges) {\n const { source, target, data, conditional } = edge;\n\n let edgeLabel = \"\";\n if (data !== undefined) {\n let edgeData = data;\n const words = edgeData.split(\" \");\n if (words.length > wrapLabelNWords) {\n edgeData = Array.from(\n { length: Math.ceil(words.length / wrapLabelNWords) },\n (_, i) =>\n words\n .slice(i * wrapLabelNWords, (i + 1) * wrapLabelNWords)\n .join(\" \")\n ).join(\" <br> \");\n }\n edgeLabel = conditional\n ? ` -. ${edgeData} .-> `\n : ` -- ${edgeData} --> `;\n } else {\n edgeLabel = conditional ? \" -.-> \" : \" --> \";\n }\n\n mermaidGraph += `\\t${_escapeNodeLabel(\n source\n )}${edgeLabel}${_escapeNodeLabel(target)};\\n`;\n }\n\n // Recursively add nested subgraphs\n for (const nestedPrefix in edgeGroups) {\n if (nestedPrefix.startsWith(`${prefix}:`) && nestedPrefix !== prefix) {\n addSubgraph(edgeGroups[nestedPrefix], nestedPrefix);\n }\n }\n\n if (prefix && !selfLoop) {\n mermaidGraph += \"\\tend\\n\";\n }\n }\n\n // Start with the top-level edges (no common prefix)\n addSubgraph(edgeGroups[\"\"] ?? [], \"\");\n\n // Add remaining subgraphs\n for (const prefix in edgeGroups) {\n if (!prefix.includes(\":\") && prefix !== \"\") {\n addSubgraph(edgeGroups[prefix], prefix);\n }\n }\n\n // Add custom styles for nodes\n if (withStyles) {\n mermaidGraph += _generateMermaidGraphStyles(nodeColors ?? {});\n }\n\n return mermaidGraph;\n}\n\n/**\n * @deprecated Use `drawMermaidImage` instead.\n */\nexport async function drawMermaidPng(\n mermaidSyntax: string,\n config?: {\n backgroundColor?: string;\n }\n) {\n return drawMermaidImage(mermaidSyntax, {\n ...config,\n imageType: \"png\",\n });\n}\n\n/**\n * Renders Mermaid graph using the Mermaid.INK API.\n *\n * @example\n * ```javascript\n * const image = await drawMermaidImage(mermaidSyntax, {\n * backgroundColor: \"white\",\n * imageType: \"png\",\n * });\n * fs.writeFileSync(\"image.png\", image);\n * ```\n *\n * @param mermaidSyntax - The Mermaid syntax to render.\n * @param config - The configuration for the image.\n * @returns The image as a Blob.\n */\nexport async function drawMermaidImage(\n mermaidSyntax: string,\n config?: {\n /**\n * The type of image to render.\n * @default \"png\"\n */\n imageType?: \"png\" | \"jpeg\" | \"webp\";\n backgroundColor?: string;\n }\n) {\n let backgroundColor = config?.backgroundColor ?? \"white\";\n const imageType = config?.imageType ?? \"png\";\n\n // Use btoa for compatibility, assume ASCII\n const mermaidSyntaxEncoded = btoa(mermaidSyntax);\n // Check if the background color is a hexadecimal color code using regex\n if (backgroundColor !== undefined) {\n const hexColorPattern = /^#(?:[0-9a-fA-F]{3}){1,2}$/;\n if (!hexColorPattern.test(backgroundColor)) {\n backgroundColor = `!${backgroundColor}`;\n }\n }\n const imageUrl = `https://mermaid.ink/img/${mermaidSyntaxEncoded}?bgColor=${backgroundColor}&type=${imageType}`;\n const res = await fetch(imageUrl);\n if (!res.ok) {\n throw new Error(\n [\n `Failed to render the graph using the Mermaid.INK API.`,\n `Status code: ${res.status}`,\n `Status text: ${res.statusText}`,\n ].join(\"\\n\")\n );\n }\n const content = await res.blob();\n return content;\n}\n"],"mappings":";;AAEA,SAAS,iBAAiBA,WAA2B;AAEnD,QAAO,UAAU,QAAQ,mBAAmB,IAAI;AACjD;AAED,MAAM,yBAAyB;CAAC;CAAK;CAAK;AAAI;AAE9C,SAAS,4BACPC,YACQ;CACR,IAAI,SAAS;AACb,MAAK,MAAM,CAAC,WAAW,MAAM,IAAI,OAAO,QAAQ,WAAW,EACzD,UAAU,CAAC,WAAW,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,CAAC;AAEjD,QAAO;AACR;;;;AAKD,SAAgB,YACdC,OACAC,OACAC,QAQQ;CACR,MAAM,EACJ,WACA,UACA,YACA,aAAa,MACb,aAAa,UACb,kBAAkB,GACnB,GAAG,UAAU,CAAE;CAEhB,IAAI,eAAe,aACf,CAAC,kCAAkC,EAAE,WAAW,mBAAmB,CAAC,GACpE;AACJ,KAAI,YAAY;EAEd,MAAM,oBAAoB;EAC1B,MAAMC,aAAqC,GACxC,oBAAoB,WACtB;AACD,MAAI,cAAc,QAChB,WAAW,aAAa;AAE1B,MAAI,aAAa,QACf,WAAW,YAAY;AAIzB,OAAK,MAAM,CAAC,KAAK,KAAK,IAAI,OAAO,QAAQ,MAAM,EA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D,eAAsB,iBACpBD,eACAE,QAQA;CACA,IAAI,kBAAkB,QAAQ,mBAAmB;CACjD,MAAM,YAAY,QAAQ,aAAa;CAGvC,MAAM,uBAAuB,KAAK,cAAc;AAEhD,KAAI,oBAAoB,QAAW;EACjC,MAAM,kBAAkB;AACxB,MAAI,CAAC,gBAAgB,KAAK,gBAAgB,EACxC,kBAAkB,CAAC,CAAC,EAAE,iBAAiB;CAE1C;CACD,MAAM,WAAW,CAAC,wBAAwB,EAAE,qBAAqB,SAAS,EAAE,gBAAgB,MAAM,EAAE,WAAW;CAC/G,MAAM,MAAM,MAAM,MAAM,SAAS;AACjC,KAAI,CAAC,IAAI,GACP,OAAM,IAAI,MACR;EACE,CAAC,qDAAqD,CAAC;EACvD,CAAC,aAAa,EAAE,IAAI,QAAQ;EAC5B,CAAC,aAAa,EAAE,IAAI,YAAY;CACjC,EAAC,KAAK,KAAK;CAGhB,MAAM,UAAU,MAAM,IAAI,MAAM;AAChC,QAAO;AACR"}
|
|
1
|
+
{"version":3,"file":"graph_mermaid.cjs","names":["nodeLabel: string","nodeColors: Record<string, string>","nodes: Record<string, Node>","edges: Edge[]","config?: {\n firstNode?: string;\n lastNode?: string;\n curveStyle?: string;\n withStyles?: boolean;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }","formatDict: Record<string, string>","edgeGroups: Record<string, Edge[]>","prefix: string","edges","mermaidSyntax: string","config?: {\n /**\n * The type of image to render.\n * @default \"png\"\n */\n imageType?: \"png\" | \"jpeg\" | \"webp\";\n backgroundColor?: string;\n }"],"sources":["../../src/runnables/graph_mermaid.ts"],"sourcesContent":["import { Edge, Node } from \"./types.js\";\n\nfunction _escapeNodeLabel(nodeLabel: string): string {\n // Escapes the node label for Mermaid syntax.\n return nodeLabel.replace(/[^a-zA-Z-_0-9]/g, \"_\");\n}\n\nconst MARKDOWN_SPECIAL_CHARS = [\"*\", \"_\", \"`\"];\n\nfunction _generateMermaidGraphStyles(\n nodeColors: Record<string, string>\n): string {\n let styles = \"\";\n for (const [className, color] of Object.entries(nodeColors)) {\n styles += `\\tclassDef ${className} ${color};\\n`;\n }\n return styles;\n}\n\n/**\n * Draws a Mermaid graph using the provided graph data\n */\nexport function drawMermaid(\n nodes: Record<string, Node>,\n edges: Edge[],\n config?: {\n firstNode?: string;\n lastNode?: string;\n curveStyle?: string;\n withStyles?: boolean;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }\n): string {\n const {\n firstNode,\n lastNode,\n nodeColors,\n withStyles = true,\n curveStyle = \"linear\",\n wrapLabelNWords = 9,\n } = config ?? {};\n // Initialize Mermaid graph configuration\n let mermaidGraph = withStyles\n ? `%%{init: {'flowchart': {'curve': '${curveStyle}'}}}%%\\ngraph TD;\\n`\n : \"graph TD;\\n\";\n if (withStyles) {\n // Node formatting templates\n const defaultClassLabel = \"default\";\n const formatDict: Record<string, string> = {\n [defaultClassLabel]: \"{0}({1})\",\n };\n if (firstNode !== undefined) {\n formatDict[firstNode] = \"{0}([{1}]):::first\";\n }\n if (lastNode !== undefined) {\n formatDict[lastNode] = \"{0}([{1}]):::last\";\n }\n\n // Add nodes to the graph\n for (const [key, node] of Object.entries(nodes)) {\n const nodeName = node.name.split(\":\").pop() ?? \"\";\n const label = MARKDOWN_SPECIAL_CHARS.some(\n (char) => nodeName.startsWith(char) && nodeName.endsWith(char)\n )\n ? `<p>${nodeName}</p>`\n : nodeName;\n\n let finalLabel = label;\n if (Object.keys(node.metadata ?? {}).length) {\n finalLabel += `<hr/><small><em>${Object.entries(node.metadata ?? {})\n .map(([k, v]) => `${k} = ${v}`)\n .join(\"\\n\")}</em></small>`;\n }\n\n const nodeLabel = (formatDict[key] ?? formatDict[defaultClassLabel])\n .replace(\"{0}\", _escapeNodeLabel(key))\n .replace(\"{1}\", finalLabel);\n\n mermaidGraph += `\\t${nodeLabel}\\n`;\n }\n }\n\n // Group edges by their common prefixes\n const edgeGroups: Record<string, Edge[]> = {};\n for (const edge of edges) {\n const srcParts = edge.source.split(\":\");\n const tgtParts = edge.target.split(\":\");\n const commonPrefix = srcParts\n .filter((src, i) => src === tgtParts[i])\n .join(\":\");\n if (!edgeGroups[commonPrefix]) {\n edgeGroups[commonPrefix] = [];\n }\n edgeGroups[commonPrefix].push(edge);\n }\n\n const seenSubgraphs = new Set<string>();\n\n function addSubgraph(edges: Edge[], prefix: string): void {\n const selfLoop = edges.length === 1 && edges[0].source === edges[0].target;\n if (prefix && !selfLoop) {\n const subgraph = prefix.split(\":\").pop()!;\n if (seenSubgraphs.has(subgraph)) {\n throw new Error(\n `Found duplicate subgraph '${subgraph}' -- this likely means that ` +\n \"you're reusing a subgraph node with the same name. \" +\n \"Please adjust your graph to have subgraph nodes with unique names.\"\n );\n }\n\n seenSubgraphs.add(subgraph);\n mermaidGraph += `\\tsubgraph ${subgraph}\\n`;\n }\n\n for (const edge of edges) {\n const { source, target, data, conditional } = edge;\n\n let edgeLabel = \"\";\n if (data !== undefined) {\n let edgeData = data;\n const words = edgeData.split(\" \");\n if (words.length > wrapLabelNWords) {\n edgeData = Array.from(\n { length: Math.ceil(words.length / wrapLabelNWords) },\n (_, i) =>\n words\n .slice(i * wrapLabelNWords, (i + 1) * wrapLabelNWords)\n .join(\" \")\n ).join(\" <br> \");\n }\n edgeLabel = conditional\n ? ` -. ${edgeData} .-> `\n : ` -- ${edgeData} --> `;\n } else {\n edgeLabel = conditional ? \" -.-> \" : \" --> \";\n }\n\n mermaidGraph += `\\t${_escapeNodeLabel(\n source\n )}${edgeLabel}${_escapeNodeLabel(target)};\\n`;\n }\n\n // Recursively add nested subgraphs\n for (const nestedPrefix in edgeGroups) {\n if (nestedPrefix.startsWith(`${prefix}:`) && nestedPrefix !== prefix) {\n addSubgraph(edgeGroups[nestedPrefix], nestedPrefix);\n }\n }\n\n if (prefix && !selfLoop) {\n mermaidGraph += \"\\tend\\n\";\n }\n }\n\n // Start with the top-level edges (no common prefix)\n addSubgraph(edgeGroups[\"\"] ?? [], \"\");\n\n // Add remaining subgraphs\n for (const prefix in edgeGroups) {\n if (!prefix.includes(\":\") && prefix !== \"\") {\n addSubgraph(edgeGroups[prefix], prefix);\n }\n }\n\n // Add custom styles for nodes\n if (withStyles) {\n mermaidGraph += _generateMermaidGraphStyles(nodeColors ?? {});\n }\n\n return mermaidGraph;\n}\n\n/**\n * Renders Mermaid graph using the Mermaid.INK API.\n *\n * @example\n * ```javascript\n * const image = await drawMermaidImage(mermaidSyntax, {\n * backgroundColor: \"white\",\n * imageType: \"png\",\n * });\n * fs.writeFileSync(\"image.png\", image);\n * ```\n *\n * @param mermaidSyntax - The Mermaid syntax to render.\n * @param config - The configuration for the image.\n * @returns The image as a Blob.\n */\nexport async function drawMermaidImage(\n mermaidSyntax: string,\n config?: {\n /**\n * The type of image to render.\n * @default \"png\"\n */\n imageType?: \"png\" | \"jpeg\" | \"webp\";\n backgroundColor?: string;\n }\n) {\n let backgroundColor = config?.backgroundColor ?? \"white\";\n const imageType = config?.imageType ?? \"png\";\n\n // Use btoa for compatibility, assume ASCII\n const mermaidSyntaxEncoded = btoa(mermaidSyntax);\n // Check if the background color is a hexadecimal color code using regex\n if (backgroundColor !== undefined) {\n const hexColorPattern = /^#(?:[0-9a-fA-F]{3}){1,2}$/;\n if (!hexColorPattern.test(backgroundColor)) {\n backgroundColor = `!${backgroundColor}`;\n }\n }\n const imageUrl = `https://mermaid.ink/img/${mermaidSyntaxEncoded}?bgColor=${backgroundColor}&type=${imageType}`;\n const res = await fetch(imageUrl);\n if (!res.ok) {\n throw new Error(\n [\n `Failed to render the graph using the Mermaid.INK API.`,\n `Status code: ${res.status}`,\n `Status text: ${res.statusText}`,\n ].join(\"\\n\")\n );\n }\n const content = await res.blob();\n return content;\n}\n"],"mappings":";;AAEA,SAAS,iBAAiBA,WAA2B;AAEnD,QAAO,UAAU,QAAQ,mBAAmB,IAAI;AACjD;AAED,MAAM,yBAAyB;CAAC;CAAK;CAAK;AAAI;AAE9C,SAAS,4BACPC,YACQ;CACR,IAAI,SAAS;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
|
74
|
-
async function drawMermaidPng(mermaidSyntax, config) {
|
|
75
|
-
return drawMermaidImage(mermaidSyntax, {
|
|
76
|
-
...config,
|
|
77
|
-
imageType: "png"
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
72
|
* Renders Mermaid graph using the Mermaid.INK API.
|
|
82
73
|
*
|
|
83
74
|
* @example
|
|
@@ -113,5 +104,5 @@ async function drawMermaidImage(mermaidSyntax, config) {
|
|
|
113
104
|
}
|
|
114
105
|
|
|
115
106
|
//#endregion
|
|
116
|
-
export { drawMermaid,
|
|
107
|
+
export { drawMermaid, drawMermaidImage };
|
|
117
108
|
//# sourceMappingURL=graph_mermaid.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graph_mermaid.js","names":["nodeLabel: string","nodeColors: Record<string, string>","nodes: Record<string, Node>","edges: Edge[]","config?: {\n firstNode?: string;\n lastNode?: string;\n curveStyle?: string;\n withStyles?: boolean;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }","formatDict: Record<string, string>","edgeGroups: Record<string, Edge[]>","prefix: string","edges","mermaidSyntax: string","config?: {\n backgroundColor?: string;\n }","config?: {\n /**\n * The type of image to render.\n * @default \"png\"\n */\n imageType?: \"png\" | \"jpeg\" | \"webp\";\n backgroundColor?: string;\n }"],"sources":["../../src/runnables/graph_mermaid.ts"],"sourcesContent":["import { Edge, Node } from \"./types.js\";\n\nfunction _escapeNodeLabel(nodeLabel: string): string {\n // Escapes the node label for Mermaid syntax.\n return nodeLabel.replace(/[^a-zA-Z-_0-9]/g, \"_\");\n}\n\nconst MARKDOWN_SPECIAL_CHARS = [\"*\", \"_\", \"`\"];\n\nfunction _generateMermaidGraphStyles(\n nodeColors: Record<string, string>\n): string {\n let styles = \"\";\n for (const [className, color] of Object.entries(nodeColors)) {\n styles += `\\tclassDef ${className} ${color};\\n`;\n }\n return styles;\n}\n\n/**\n * Draws a Mermaid graph using the provided graph data\n */\nexport function drawMermaid(\n nodes: Record<string, Node>,\n edges: Edge[],\n config?: {\n firstNode?: string;\n lastNode?: string;\n curveStyle?: string;\n withStyles?: boolean;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }\n): string {\n const {\n firstNode,\n lastNode,\n nodeColors,\n withStyles = true,\n curveStyle = \"linear\",\n wrapLabelNWords = 9,\n } = config ?? {};\n // Initialize Mermaid graph configuration\n let mermaidGraph = withStyles\n ? `%%{init: {'flowchart': {'curve': '${curveStyle}'}}}%%\\ngraph TD;\\n`\n : \"graph TD;\\n\";\n if (withStyles) {\n // Node formatting templates\n const defaultClassLabel = \"default\";\n const formatDict: Record<string, string> = {\n [defaultClassLabel]: \"{0}({1})\",\n };\n if (firstNode !== undefined) {\n formatDict[firstNode] = \"{0}([{1}]):::first\";\n }\n if (lastNode !== undefined) {\n formatDict[lastNode] = \"{0}([{1}]):::last\";\n }\n\n // Add nodes to the graph\n for (const [key, node] of Object.entries(nodes)) {\n const nodeName = node.name.split(\":\").pop() ?? \"\";\n const label = MARKDOWN_SPECIAL_CHARS.some(\n (char) => nodeName.startsWith(char) && nodeName.endsWith(char)\n )\n ? `<p>${nodeName}</p>`\n : nodeName;\n\n let finalLabel = label;\n if (Object.keys(node.metadata ?? {}).length) {\n finalLabel += `<hr/><small><em>${Object.entries(node.metadata ?? {})\n .map(([k, v]) => `${k} = ${v}`)\n .join(\"\\n\")}</em></small>`;\n }\n\n const nodeLabel = (formatDict[key] ?? formatDict[defaultClassLabel])\n .replace(\"{0}\", _escapeNodeLabel(key))\n .replace(\"{1}\", finalLabel);\n\n mermaidGraph += `\\t${nodeLabel}\\n`;\n }\n }\n\n // Group edges by their common prefixes\n const edgeGroups: Record<string, Edge[]> = {};\n for (const edge of edges) {\n const srcParts = edge.source.split(\":\");\n const tgtParts = edge.target.split(\":\");\n const commonPrefix = srcParts\n .filter((src, i) => src === tgtParts[i])\n .join(\":\");\n if (!edgeGroups[commonPrefix]) {\n edgeGroups[commonPrefix] = [];\n }\n edgeGroups[commonPrefix].push(edge);\n }\n\n const seenSubgraphs = new Set<string>();\n\n function addSubgraph(edges: Edge[], prefix: string): void {\n const selfLoop = edges.length === 1 && edges[0].source === edges[0].target;\n if (prefix && !selfLoop) {\n const subgraph = prefix.split(\":\").pop()!;\n if (seenSubgraphs.has(subgraph)) {\n throw new Error(\n `Found duplicate subgraph '${subgraph}' -- this likely means that ` +\n \"you're reusing a subgraph node with the same name. \" +\n \"Please adjust your graph to have subgraph nodes with unique names.\"\n );\n }\n\n seenSubgraphs.add(subgraph);\n mermaidGraph += `\\tsubgraph ${subgraph}\\n`;\n }\n\n for (const edge of edges) {\n const { source, target, data, conditional } = edge;\n\n let edgeLabel = \"\";\n if (data !== undefined) {\n let edgeData = data;\n const words = edgeData.split(\" \");\n if (words.length > wrapLabelNWords) {\n edgeData = Array.from(\n { length: Math.ceil(words.length / wrapLabelNWords) },\n (_, i) =>\n words\n .slice(i * wrapLabelNWords, (i + 1) * wrapLabelNWords)\n .join(\" \")\n ).join(\" <br> \");\n }\n edgeLabel = conditional\n ? ` -. ${edgeData} .-> `\n : ` -- ${edgeData} --> `;\n } else {\n edgeLabel = conditional ? \" -.-> \" : \" --> \";\n }\n\n mermaidGraph += `\\t${_escapeNodeLabel(\n source\n )}${edgeLabel}${_escapeNodeLabel(target)};\\n`;\n }\n\n // Recursively add nested subgraphs\n for (const nestedPrefix in edgeGroups) {\n if (nestedPrefix.startsWith(`${prefix}:`) && nestedPrefix !== prefix) {\n addSubgraph(edgeGroups[nestedPrefix], nestedPrefix);\n }\n }\n\n if (prefix && !selfLoop) {\n mermaidGraph += \"\\tend\\n\";\n }\n }\n\n // Start with the top-level edges (no common prefix)\n addSubgraph(edgeGroups[\"\"] ?? [], \"\");\n\n // Add remaining subgraphs\n for (const prefix in edgeGroups) {\n if (!prefix.includes(\":\") && prefix !== \"\") {\n addSubgraph(edgeGroups[prefix], prefix);\n }\n }\n\n // Add custom styles for nodes\n if (withStyles) {\n mermaidGraph += _generateMermaidGraphStyles(nodeColors ?? {});\n }\n\n return mermaidGraph;\n}\n\n/**\n * @deprecated Use `drawMermaidImage` instead.\n */\nexport async function drawMermaidPng(\n mermaidSyntax: string,\n config?: {\n backgroundColor?: string;\n }\n) {\n return drawMermaidImage(mermaidSyntax, {\n ...config,\n imageType: \"png\",\n });\n}\n\n/**\n * Renders Mermaid graph using the Mermaid.INK API.\n *\n * @example\n * ```javascript\n * const image = await drawMermaidImage(mermaidSyntax, {\n * backgroundColor: \"white\",\n * imageType: \"png\",\n * });\n * fs.writeFileSync(\"image.png\", image);\n * ```\n *\n * @param mermaidSyntax - The Mermaid syntax to render.\n * @param config - The configuration for the image.\n * @returns The image as a Blob.\n */\nexport async function drawMermaidImage(\n mermaidSyntax: string,\n config?: {\n /**\n * The type of image to render.\n * @default \"png\"\n */\n imageType?: \"png\" | \"jpeg\" | \"webp\";\n backgroundColor?: string;\n }\n) {\n let backgroundColor = config?.backgroundColor ?? \"white\";\n const imageType = config?.imageType ?? \"png\";\n\n // Use btoa for compatibility, assume ASCII\n const mermaidSyntaxEncoded = btoa(mermaidSyntax);\n // Check if the background color is a hexadecimal color code using regex\n if (backgroundColor !== undefined) {\n const hexColorPattern = /^#(?:[0-9a-fA-F]{3}){1,2}$/;\n if (!hexColorPattern.test(backgroundColor)) {\n backgroundColor = `!${backgroundColor}`;\n }\n }\n const imageUrl = `https://mermaid.ink/img/${mermaidSyntaxEncoded}?bgColor=${backgroundColor}&type=${imageType}`;\n const res = await fetch(imageUrl);\n if (!res.ok) {\n throw new Error(\n [\n `Failed to render the graph using the Mermaid.INK API.`,\n `Status code: ${res.status}`,\n `Status text: ${res.statusText}`,\n ].join(\"\\n\")\n );\n }\n const content = await res.blob();\n return content;\n}\n"],"mappings":";AAEA,SAAS,iBAAiBA,WAA2B;AAEnD,QAAO,UAAU,QAAQ,mBAAmB,IAAI;AACjD;AAED,MAAM,yBAAyB;CAAC;CAAK;CAAK;AAAI;AAE9C,SAAS,4BACPC,YACQ;C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eLabel: string","nodeColors: Record<string, string>","nodes: Record<string, Node>","edges: Edge[]","config?: {\n firstNode?: string;\n lastNode?: string;\n curveStyle?: string;\n withStyles?: boolean;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }","formatDict: Record<string, string>","edgeGroups: Record<string, Edge[]>","prefix: string","edges","mermaidSyntax: string","config?: {\n /**\n * The type of image to render.\n * @default \"png\"\n */\n imageType?: \"png\" | \"jpeg\" | \"webp\";\n backgroundColor?: string;\n }"],"sources":["../../src/runnables/graph_mermaid.ts"],"sourcesContent":["import { Edge, Node } from \"./types.js\";\n\nfunction _escapeNodeLabel(nodeLabel: string): string {\n // Escapes the node label for Mermaid syntax.\n return nodeLabel.replace(/[^a-zA-Z-_0-9]/g, \"_\");\n}\n\nconst MARKDOWN_SPECIAL_CHARS = [\"*\", \"_\", \"`\"];\n\nfunction _generateMermaidGraphStyles(\n nodeColors: Record<string, string>\n): string {\n let styles = \"\";\n for (const [className, color] of Object.entries(nodeColors)) {\n styles += `\\tclassDef ${className} ${color};\\n`;\n }\n return styles;\n}\n\n/**\n * Draws a Mermaid graph using the provided graph data\n */\nexport function drawMermaid(\n nodes: Record<string, Node>,\n edges: Edge[],\n config?: {\n firstNode?: string;\n lastNode?: string;\n curveStyle?: string;\n withStyles?: boolean;\n nodeColors?: Record<string, string>;\n wrapLabelNWords?: number;\n }\n): string {\n const {\n firstNode,\n lastNode,\n nodeColors,\n withStyles = true,\n curveStyle = \"linear\",\n wrapLabelNWords = 9,\n } = config ?? {};\n // Initialize Mermaid graph configuration\n let mermaidGraph = withStyles\n ? `%%{init: {'flowchart': {'curve': '${curveStyle}'}}}%%\\ngraph TD;\\n`\n : \"graph TD;\\n\";\n if (withStyles) {\n // Node formatting templates\n const defaultClassLabel = \"default\";\n const formatDict: Record<string, string> = {\n [defaultClassLabel]: \"{0}({1})\",\n };\n if (firstNode !== undefined) {\n formatDict[firstNode] = \"{0}([{1}]):::first\";\n }\n if (lastNode !== undefined) {\n formatDict[lastNode] = \"{0}([{1}]):::last\";\n }\n\n // Add nodes to the graph\n for (const [key, node] of Object.entries(nodes)) {\n const nodeName = node.name.split(\":\").pop() ?? \"\";\n const label = MARKDOWN_SPECIAL_CHARS.some(\n (char) => nodeName.startsWith(char) && nodeName.endsWith(char)\n )\n ? `<p>${nodeName}</p>`\n : nodeName;\n\n let finalLabel = label;\n if (Object.keys(node.metadata ?? {}).length) {\n finalLabel += `<hr/><small><em>${Object.entries(node.metadata ?? {})\n .map(([k, v]) => `${k} = ${v}`)\n .join(\"\\n\")}</em></small>`;\n }\n\n const nodeLabel = (formatDict[key] ?? formatDict[defaultClassLabel])\n .replace(\"{0}\", _escapeNodeLabel(key))\n .replace(\"{1}\", finalLabel);\n\n mermaidGraph += `\\t${nodeLabel}\\n`;\n }\n }\n\n // Group edges by their common prefixes\n const edgeGroups: Record<string, Edge[]> = {};\n for (const edge of edges) {\n const srcParts = edge.source.split(\":\");\n const tgtParts = edge.target.split(\":\");\n const commonPrefix = srcParts\n .filter((src, i) => src === tgtParts[i])\n .join(\":\");\n if (!edgeGroups[commonPrefix]) {\n edgeGroups[commonPrefix] = [];\n }\n edgeGroups[commonPrefix].push(edge);\n }\n\n const seenSubgraphs = new Set<string>();\n\n function addSubgraph(edges: Edge[], prefix: string): void {\n const selfLoop = edges.length === 1 && edges[0].source === edges[0].target;\n if (prefix && !selfLoop) {\n const subgraph = prefix.split(\":\").pop()!;\n if (seenSubgraphs.has(subgraph)) {\n throw new Error(\n `Found duplicate subgraph '${subgraph}' -- this likely means that ` +\n \"you're reusing a subgraph node with the same name. \" +\n \"Please adjust your graph to have subgraph nodes with unique names.\"\n );\n }\n\n seenSubgraphs.add(subgraph);\n mermaidGraph += `\\tsubgraph ${subgraph}\\n`;\n }\n\n for (const edge of edges) {\n const { source, target, data, conditional } = edge;\n\n let edgeLabel = \"\";\n if (data !== undefined) {\n let edgeData = data;\n const words = edgeData.split(\" \");\n if (words.length > wrapLabelNWords) {\n edgeData = Array.from(\n { length: Math.ceil(words.length / wrapLabelNWords) },\n (_, i) =>\n words\n .slice(i * wrapLabelNWords, (i + 1) * wrapLabelNWords)\n .join(\" \")\n ).join(\" <br> \");\n }\n edgeLabel = conditional\n ? ` -. ${edgeData} .-> `\n : ` -- ${edgeData} --> `;\n } else {\n edgeLabel = conditional ? \" -.-> \" : \" --> \";\n }\n\n mermaidGraph += `\\t${_escapeNodeLabel(\n source\n )}${edgeLabel}${_escapeNodeLabel(target)};\\n`;\n }\n\n // Recursively add nested subgraphs\n for (const nestedPrefix in edgeGroups) {\n if (nestedPrefix.startsWith(`${prefix}:`) && nestedPrefix !== prefix) {\n addSubgraph(edgeGroups[nestedPrefix], nestedPrefix);\n }\n }\n\n if (prefix && !selfLoop) {\n mermaidGraph += \"\\tend\\n\";\n }\n }\n\n // Start with the top-level edges (no common prefix)\n addSubgraph(edgeGroups[\"\"] ?? [], \"\");\n\n // Add remaining subgraphs\n for (const prefix in edgeGroups) {\n if (!prefix.includes(\":\") && prefix !== \"\") {\n addSubgraph(edgeGroups[prefix], prefix);\n }\n }\n\n // Add custom styles for nodes\n if (withStyles) {\n mermaidGraph += _generateMermaidGraphStyles(nodeColors ?? {});\n }\n\n return mermaidGraph;\n}\n\n/**\n * Renders Mermaid graph using the Mermaid.INK API.\n *\n * @example\n * ```javascript\n * const image = await drawMermaidImage(mermaidSyntax, {\n * backgroundColor: \"white\",\n * imageType: \"png\",\n * });\n * fs.writeFileSync(\"image.png\", image);\n * ```\n *\n * @param mermaidSyntax - The Mermaid syntax to render.\n * @param config - The configuration for the image.\n * @returns The image as a Blob.\n */\nexport async function drawMermaidImage(\n mermaidSyntax: string,\n config?: {\n /**\n * The type of image to render.\n * @default \"png\"\n */\n imageType?: \"png\" | \"jpeg\" | \"webp\";\n backgroundColor?: string;\n }\n) {\n let backgroundColor = config?.backgroundColor ?? \"white\";\n const imageType = config?.imageType ?? \"png\";\n\n // Use btoa for compatibility, assume ASCII\n const mermaidSyntaxEncoded = btoa(mermaidSyntax);\n // Check if the background color is a hexadecimal color code using regex\n if (backgroundColor !== undefined) {\n const hexColorPattern = /^#(?:[0-9a-fA-F]{3}){1,2}$/;\n if (!hexColorPattern.test(backgroundColor)) {\n backgroundColor = `!${backgroundColor}`;\n }\n }\n const imageUrl = `https://mermaid.ink/img/${mermaidSyntaxEncoded}?bgColor=${backgroundColor}&type=${imageType}`;\n const res = await fetch(imageUrl);\n if (!res.ok) {\n throw new Error(\n [\n `Failed to render the graph using the Mermaid.INK API.`,\n `Status code: ${res.status}`,\n `Status text: ${res.statusText}`,\n ].join(\"\\n\")\n );\n }\n const content = await res.blob();\n return content;\n}\n"],"mappings":";AAEA,SAAS,iBAAiBA,WAA2B;AAEnD,QAAO,UAAU,QAAQ,mBAAmB,IAAI;AACjD;AAED,MAAM,yBAAyB;CAAC;CAAK;CAAK;AAAI;AAE9C,SAAS,4BACPC,YACQ;CACR,IAAI,SAAS;AACb,MAAK,MAAM,CAAC,WAAW,MAAM,IAAI,OAAO,QAAQ,WAAW,EACzD,UAAU,CAAC,WAAW,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,CAAC;AAEjD,QAAO;AACR;;;;AAKD,SAAgB,YACdC,OACAC,OACAC,QAQQ;CACR,MAAM,EACJ,WACA,UACA,YACA,aAAa,MACb,aAAa,UACb,kBAAkB,GACnB,GAAG,UAAU,CAAE;CAEhB,IAAI,eAAe,aACf,CAAC,kCAAkC,EAAE,WAAW,mBAAmB,CAAC,GACpE;AACJ,KAAI,YAAY;EAEd,MAAM,oBAAoB;EAC1B,MAAMC,aAAqC,GACxC,oBAAoB,WACtB;AACD,MAAI,cAAc,QAChB,WAAW,aAAa;AAE1B,MAAI,aAAa,QACf,WAAW,YAAY;AAIzB,OAAK,MAAM,CAAC,KAAK,KAAK,IAAI,OAAO,QAAQ,MAAM,EAAE;GAC/C,MAAM,WAAW,KAAK,KAAK,MAAM,IAAI,CAAC,KAAK,IAAI;GAC/C,MAAM,QAAQ,uBAAuB,KACnC,C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B,KAAK,gBAAgB,EACxC,kBAAkB,CAAC,CAAC,EAAE,iBAAiB;CAE1C;CACD,MAAM,WAAW,CAAC,wBAAwB,EAAE,qBAAqB,SAAS,EAAE,gBAAgB,MAAM,EAAE,WAAW;CAC/G,MAAM,MAAM,MAAM,MAAM,SAAS;AACjC,KAAI,CAAC,IAAI,GACP,OAAM,IAAI,MACR;EACE,CAAC,qDAAqD,CAAC;EACvD,CAAC,aAAa,EAAE,IAAI,QAAQ;EAC5B,CAAC,aAAa,EAAE,IAAI,YAAY;CACjC,EAAC,KAAK,KAAK;CAGhB,MAAM,UAAU,MAAM,IAAI,MAAM;AAChC,QAAO;AACR"}
|
|
@@ -21,7 +21,7 @@ const require_passthrough = require('./passthrough.cjs');
|
|
|
21
21
|
* import { ChatAnthropic } from "@langchain/anthropic";
|
|
22
22
|
* import { UpstashRedisChatMessageHistory } from "@langchain/community/stores/message/upstash_redis";
|
|
23
23
|
* // For demos, you can also use an in-memory store:
|
|
24
|
-
* // import { ChatMessageHistory } from "langchain/stores/message/in_memory";
|
|
24
|
+
* // import { ChatMessageHistory } from "@langchain/classic/stores/message/in_memory";
|
|
25
25
|
*
|
|
26
26
|
* const prompt = ChatPromptTemplate.fromMessages([
|
|
27
27
|
* ["system", "You're an assistant who's good at {ability}"],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"history.cjs","names":["RunnableBinding","fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>","historyChain: Runnable","RunnableLambda","RunnablePassthrough","config","inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>","isBaseMessage","HumanMessage","outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>","AIMessage","input: any","kwargs?: RunnableConfig","run: Run","config: RunnableConfig"],"sources":["../../src/runnables/history.ts"],"sourcesContent":["import {\n BaseChatMessageHistory,\n BaseListChatMessageHistory,\n} from \"../chat_history.js\";\nimport {\n AIMessage,\n BaseMessage,\n HumanMessage,\n isBaseMessage,\n} from \"../messages/index.js\";\nimport { Run } from \"../tracers/base.js\";\nimport {\n Runnable,\n RunnableBinding,\n type RunnableBindingArgs,\n RunnableLambda,\n} from \"./base.js\";\nimport { RunnableConfig } from \"./config.js\";\nimport { RunnablePassthrough } from \"./passthrough.js\";\n\ntype GetSessionHistoryCallable = (\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ...args: Array<any>\n) =>\n | Promise<BaseChatMessageHistory | BaseListChatMessageHistory>\n | BaseChatMessageHistory\n | BaseListChatMessageHistory;\n\nexport interface RunnableWithMessageHistoryInputs<RunInput, RunOutput>\n extends Omit<RunnableBindingArgs<RunInput, RunOutput>, \"bound\" | \"config\"> {\n runnable: Runnable<RunInput, RunOutput>;\n getMessageHistory: GetSessionHistoryCallable;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n config?: RunnableConfig;\n}\n\n/**\n * Wraps a LCEL chain and manages history. It appends input messages\n * and chain outputs as history, and adds the current history messages to\n * the chain input.\n * @example\n * ```typescript\n * // pnpm install @langchain/anthropic @langchain/community @upstash/redis\n *\n * import {\n * ChatPromptTemplate,\n * MessagesPlaceholder,\n * } from \"@langchain/core/prompts\";\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n * import { UpstashRedisChatMessageHistory } from \"@langchain/community/stores/message/upstash_redis\";\n * // For demos, you can also use an in-memory store:\n * // import { ChatMessageHistory } from \"langchain/stores/message/in_memory\";\n *\n * const prompt = ChatPromptTemplate.fromMessages([\n * [\"system\", \"You're an assistant who's good at {ability}\"],\n * new MessagesPlaceholder(\"history\"),\n * [\"human\", \"{question}\"],\n * ]);\n *\n * const chain = prompt.pipe(new ChatAnthropic({}));\n *\n * const chainWithHistory = new RunnableWithMessageHistory({\n * runnable: chain,\n * getMessageHistory: (sessionId) =>\n * new UpstashRedisChatMessageHistory({\n * sessionId,\n * config: {\n * url: process.env.UPSTASH_REDIS_REST_URL!,\n * token: process.env.UPSTASH_REDIS_REST_TOKEN!,\n * },\n * }),\n * inputMessagesKey: \"question\",\n * historyMessagesKey: \"history\",\n * });\n *\n * const result = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What does cosine mean?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n *\n * const result2 = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What's its inverse?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n * ```\n */\nexport class RunnableWithMessageHistory<\n RunInput,\n RunOutput\n> extends RunnableBinding<RunInput, RunOutput> {\n runnable: Runnable<RunInput, RunOutput>;\n\n inputMessagesKey?: string;\n\n outputMessagesKey?: string;\n\n historyMessagesKey?: string;\n\n getMessageHistory: GetSessionHistoryCallable;\n\n constructor(fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>) {\n let historyChain: Runnable = RunnableLambda.from((input, options) =>\n this._enterHistory(input, options ?? {})\n ).withConfig({ runName: \"loadHistory\" });\n\n const messagesKey = fields.historyMessagesKey ?? fields.inputMessagesKey;\n if (messagesKey) {\n historyChain = RunnablePassthrough.assign({\n [messagesKey]: historyChain,\n }).withConfig({ runName: \"insertHistory\" });\n }\n\n const bound = historyChain\n .pipe(\n fields.runnable.withListeners({\n onEnd: (run, config) => this._exitHistory(run, config ?? {}),\n })\n )\n .withConfig({ runName: \"RunnableWithMessageHistory\" });\n\n const config = fields.config ?? {};\n\n super({\n ...fields,\n config,\n bound,\n });\n this.runnable = fields.runnable;\n this.getMessageHistory = fields.getMessageHistory;\n this.inputMessagesKey = fields.inputMessagesKey;\n this.outputMessagesKey = fields.outputMessagesKey;\n this.historyMessagesKey = fields.historyMessagesKey;\n }\n\n _getInputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedInputValue;\n if (\n typeof inputValue === \"object\" &&\n !Array.isArray(inputValue) &&\n !isBaseMessage(inputValue)\n ) {\n let key;\n if (this.inputMessagesKey) {\n key = this.inputMessagesKey;\n } else if (Object.keys(inputValue).length === 1) {\n key = Object.keys(inputValue)[0];\n } else {\n key = \"input\";\n }\n if (Array.isArray(inputValue[key]) && Array.isArray(inputValue[key][0])) {\n parsedInputValue = inputValue[key][0];\n } else {\n parsedInputValue = inputValue[key];\n }\n } else {\n parsedInputValue = inputValue;\n }\n if (typeof parsedInputValue === \"string\") {\n return [new HumanMessage(parsedInputValue)];\n } else if (Array.isArray(parsedInputValue)) {\n return parsedInputValue;\n } else if (isBaseMessage(parsedInputValue)) {\n return [parsedInputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages.\\nGot ${JSON.stringify(\n parsedInputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n _getOutputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedOutputValue;\n if (\n !Array.isArray(outputValue) &&\n !isBaseMessage(outputValue) &&\n typeof outputValue !== \"string\"\n ) {\n let key;\n if (this.outputMessagesKey !== undefined) {\n key = this.outputMessagesKey;\n } else if (Object.keys(outputValue).length === 1) {\n key = Object.keys(outputValue)[0];\n } else {\n key = \"output\";\n }\n // If you are wrapping a chat model directly\n // The output is actually this weird generations object\n if (outputValue.generations !== undefined) {\n parsedOutputValue = outputValue.generations[0][0].message;\n } else {\n parsedOutputValue = outputValue[key];\n }\n } else {\n parsedOutputValue = outputValue;\n }\n\n if (typeof parsedOutputValue === \"string\") {\n return [new AIMessage(parsedOutputValue)];\n } else if (Array.isArray(parsedOutputValue)) {\n return parsedOutputValue;\n } else if (isBaseMessage(parsedOutputValue)) {\n return [parsedOutputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages. Received: ${JSON.stringify(\n parsedOutputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n async _enterHistory(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n input: any,\n kwargs?: RunnableConfig\n ): Promise<BaseMessage[]> {\n const history = kwargs?.configurable?.messageHistory;\n const messages = await history.getMessages();\n if (this.historyMessagesKey === undefined) {\n return messages.concat(this._getInputMessages(input));\n }\n return messages;\n }\n\n async _exitHistory(run: Run, config: RunnableConfig): Promise<void> {\n const history = config.configurable?.messageHistory;\n\n // Get input messages\n let inputs;\n // Chat model inputs are nested arrays\n if (Array.isArray(run.inputs) && Array.isArray(run.inputs[0])) {\n inputs = run.inputs[0];\n } else {\n inputs = run.inputs;\n }\n let inputMessages = this._getInputMessages(inputs);\n // If historic messages were prepended to the input messages, remove them to\n // avoid adding duplicate messages to history.\n if (this.historyMessagesKey === undefined) {\n const existingMessages = await history.getMessages();\n inputMessages = inputMessages.slice(existingMessages.length);\n }\n // Get output messages\n const outputValue = run.outputs;\n if (!outputValue) {\n throw new Error(\n `Output values from 'Run' undefined. Run: ${JSON.stringify(\n run,\n null,\n 2\n )}`\n );\n }\n const outputMessages = this._getOutputMessages(outputValue);\n await history.addMessages([...inputMessages, ...outputMessages]);\n }\n\n async _mergeConfig(...configs: Array<RunnableConfig | undefined>) {\n const config = await super._mergeConfig(...configs);\n // Extract sessionId\n if (!config.configurable || !config.configurable.sessionId) {\n const exampleInput = {\n [this.inputMessagesKey ?? \"input\"]: \"foo\",\n };\n const exampleConfig = { configurable: { sessionId: \"123\" } };\n throw new Error(\n `sessionId is required. Pass it in as part of the config argument to .invoke() or .stream()\\n` +\n `eg. chain.invoke(${JSON.stringify(exampleInput)}, ${JSON.stringify(\n exampleConfig\n )})`\n );\n }\n // attach messageHistory\n const { sessionId } = config.configurable;\n config.configurable.messageHistory = await this.getMessageHistory(\n sessionId\n );\n return config;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsGA,IAAa,6BAAb,cAGUA,+BAAqC;CAC7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D,gBAAgB,cAAc,MAAM,iBAAiB,OAAO;EAC7D;EAED,MAAM,cAAc,IAAI;AACxB,MAAI,CAAC,YACH,OAAM,IAAI,MACR,CAAC,yCAAyC,EAAE,KAAK,UAC/C,KACA,MACA,EACD,EAAE;EAGP,MAAM,iBAAiB,KAAK,mBAAmB,YAAY;EAC3D,MAAM,QAAQ,YAAY,CAAC,GAAG,eAAe,GAAG,cAAe,EAAC;CACjE;CAED,MAAM,aAAa,GAAG,SAA4C;EAChE,MAAM,SAAS,MAAM,MAAM,aAAa,GAAG,QAAQ;AAEnD,MAAI,CAAC,OAAO,gBAAgB,CAAC,OAAO,aAAa,WAAW;GAC1D,MAAM,eAAe,GAClB,KAAK,oBAAoB,UAAU,MACrC;GACD,MAAM,gBAAgB,EAAE,cAAc,EAAE,WAAW,MAAO,EAAE;AAC5D,SAAM,IAAI,MACR,CAAC,6GAA4F,EACvE,KAAK,UAAU,aAAa,CAAC,EAAE,EAAE,KAAK,UACxD,cACD,CAAC,CAAC,CAAC;EAET;EAED,MAAM,EAAE,WAAW,GAAG,OAAO;EAC7B,OAAO,aAAa,iBAAiB,MAAM,KAAK,kBAC9C,UACD;AACD,SAAO;CACR;AACF"}
|
|
1
|
+
{"version":3,"file":"history.cjs","names":["RunnableBinding","fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>","historyChain: Runnable","RunnableLambda","RunnablePassthrough","config","inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>","isBaseMessage","HumanMessage","outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>","AIMessage","input: any","kwargs?: RunnableConfig","run: Run","config: RunnableConfig"],"sources":["../../src/runnables/history.ts"],"sourcesContent":["import {\n BaseChatMessageHistory,\n BaseListChatMessageHistory,\n} from \"../chat_history.js\";\nimport {\n AIMessage,\n BaseMessage,\n HumanMessage,\n isBaseMessage,\n} from \"../messages/index.js\";\nimport { Run } from \"../tracers/base.js\";\nimport {\n Runnable,\n RunnableBinding,\n type RunnableBindingArgs,\n RunnableLambda,\n} from \"./base.js\";\nimport { RunnableConfig } from \"./config.js\";\nimport { RunnablePassthrough } from \"./passthrough.js\";\n\ntype GetSessionHistoryCallable = (\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ...args: Array<any>\n) =>\n | Promise<BaseChatMessageHistory | BaseListChatMessageHistory>\n | BaseChatMessageHistory\n | BaseListChatMessageHistory;\n\nexport interface RunnableWithMessageHistoryInputs<RunInput, RunOutput>\n extends Omit<RunnableBindingArgs<RunInput, RunOutput>, \"bound\" | \"config\"> {\n runnable: Runnable<RunInput, RunOutput>;\n getMessageHistory: GetSessionHistoryCallable;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n config?: RunnableConfig;\n}\n\n/**\n * Wraps a LCEL chain and manages history. It appends input messages\n * and chain outputs as history, and adds the current history messages to\n * the chain input.\n * @example\n * ```typescript\n * // pnpm install @langchain/anthropic @langchain/community @upstash/redis\n *\n * import {\n * ChatPromptTemplate,\n * MessagesPlaceholder,\n * } from \"@langchain/core/prompts\";\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n * import { UpstashRedisChatMessageHistory } from \"@langchain/community/stores/message/upstash_redis\";\n * // For demos, you can also use an in-memory store:\n * // import { ChatMessageHistory } from \"@langchain/classic/stores/message/in_memory\";\n *\n * const prompt = ChatPromptTemplate.fromMessages([\n * [\"system\", \"You're an assistant who's good at {ability}\"],\n * new MessagesPlaceholder(\"history\"),\n * [\"human\", \"{question}\"],\n * ]);\n *\n * const chain = prompt.pipe(new ChatAnthropic({}));\n *\n * const chainWithHistory = new RunnableWithMessageHistory({\n * runnable: chain,\n * getMessageHistory: (sessionId) =>\n * new UpstashRedisChatMessageHistory({\n * sessionId,\n * config: {\n * url: process.env.UPSTASH_REDIS_REST_URL!,\n * token: process.env.UPSTASH_REDIS_REST_TOKEN!,\n * },\n * }),\n * inputMessagesKey: \"question\",\n * historyMessagesKey: \"history\",\n * });\n *\n * const result = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What does cosine mean?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n *\n * const result2 = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What's its inverse?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n * ```\n */\nexport class RunnableWithMessageHistory<\n RunInput,\n RunOutput\n> extends RunnableBinding<RunInput, RunOutput> {\n runnable: Runnable<RunInput, RunOutput>;\n\n inputMessagesKey?: string;\n\n outputMessagesKey?: string;\n\n historyMessagesKey?: string;\n\n getMessageHistory: GetSessionHistoryCallable;\n\n constructor(fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>) {\n let historyChain: Runnable = RunnableLambda.from((input, options) =>\n this._enterHistory(input, options ?? {})\n ).withConfig({ runName: \"loadHistory\" });\n\n const messagesKey = fields.historyMessagesKey ?? fields.inputMessagesKey;\n if (messagesKey) {\n historyChain = RunnablePassthrough.assign({\n [messagesKey]: historyChain,\n }).withConfig({ runName: \"insertHistory\" });\n }\n\n const bound = historyChain\n .pipe(\n fields.runnable.withListeners({\n onEnd: (run, config) => this._exitHistory(run, config ?? {}),\n })\n )\n .withConfig({ runName: \"RunnableWithMessageHistory\" });\n\n const config = fields.config ?? {};\n\n super({\n ...fields,\n config,\n bound,\n });\n this.runnable = fields.runnable;\n this.getMessageHistory = fields.getMessageHistory;\n this.inputMessagesKey = fields.inputMessagesKey;\n this.outputMessagesKey = fields.outputMessagesKey;\n this.historyMessagesKey = fields.historyMessagesKey;\n }\n\n _getInputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedInputValue;\n if (\n typeof inputValue === \"object\" &&\n !Array.isArray(inputValue) &&\n !isBaseMessage(inputValue)\n ) {\n let key;\n if (this.inputMessagesKey) {\n key = this.inputMessagesKey;\n } else if (Object.keys(inputValue).length === 1) {\n key = Object.keys(inputValue)[0];\n } else {\n key = \"input\";\n }\n if (Array.isArray(inputValue[key]) && Array.isArray(inputValue[key][0])) {\n parsedInputValue = inputValue[key][0];\n } else {\n parsedInputValue = inputValue[key];\n }\n } else {\n parsedInputValue = inputValue;\n }\n if (typeof parsedInputValue === \"string\") {\n return [new HumanMessage(parsedInputValue)];\n } else if (Array.isArray(parsedInputValue)) {\n return parsedInputValue;\n } else if (isBaseMessage(parsedInputValue)) {\n return [parsedInputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages.\\nGot ${JSON.stringify(\n parsedInputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n _getOutputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedOutputValue;\n if (\n !Array.isArray(outputValue) &&\n !isBaseMessage(outputValue) &&\n typeof outputValue !== \"string\"\n ) {\n let key;\n if (this.outputMessagesKey !== undefined) {\n key = this.outputMessagesKey;\n } else if (Object.keys(outputValue).length === 1) {\n key = Object.keys(outputValue)[0];\n } else {\n key = \"output\";\n }\n // If you are wrapping a chat model directly\n // The output is actually this weird generations object\n if (outputValue.generations !== undefined) {\n parsedOutputValue = outputValue.generations[0][0].message;\n } else {\n parsedOutputValue = outputValue[key];\n }\n } else {\n parsedOutputValue = outputValue;\n }\n\n if (typeof parsedOutputValue === \"string\") {\n return [new AIMessage(parsedOutputValue)];\n } else if (Array.isArray(parsedOutputValue)) {\n return parsedOutputValue;\n } else if (isBaseMessage(parsedOutputValue)) {\n return [parsedOutputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages. Received: ${JSON.stringify(\n parsedOutputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n async _enterHistory(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n input: any,\n kwargs?: RunnableConfig\n ): Promise<BaseMessage[]> {\n const history = kwargs?.configurable?.messageHistory;\n const messages = await history.getMessages();\n if (this.historyMessagesKey === undefined) {\n return messages.concat(this._getInputMessages(input));\n }\n return messages;\n }\n\n async _exitHistory(run: Run, config: RunnableConfig): Promise<void> {\n const history = config.configurable?.messageHistory;\n\n // Get input messages\n let inputs;\n // Chat model inputs are nested arrays\n if (Array.isArray(run.inputs) && Array.isArray(run.inputs[0])) {\n inputs = run.inputs[0];\n } else {\n inputs = run.inputs;\n }\n let inputMessages = this._getInputMessages(inputs);\n // If historic messages were prepended to the input messages, remove them to\n // avoid adding duplicate messages to history.\n if (this.historyMessagesKey === undefined) {\n const existingMessages = await history.getMessages();\n inputMessages = inputMessages.slice(existingMessages.length);\n }\n // Get output messages\n const outputValue = run.outputs;\n if (!outputValue) {\n throw new Error(\n `Output values from 'Run' undefined. Run: ${JSON.stringify(\n run,\n null,\n 2\n )}`\n );\n }\n const outputMessages = this._getOutputMessages(outputValue);\n await history.addMessages([...inputMessages, ...outputMessages]);\n }\n\n async _mergeConfig(...configs: Array<RunnableConfig | undefined>) {\n const config = await super._mergeConfig(...configs);\n // Extract sessionId\n if (!config.configurable || !config.configurable.sessionId) {\n const exampleInput = {\n [this.inputMessagesKey ?? \"input\"]: \"foo\",\n };\n const exampleConfig = { configurable: { sessionId: \"123\" } };\n throw new Error(\n `sessionId is required. Pass it in as part of the config argument to .invoke() or .stream()\\n` +\n `eg. chain.invoke(${JSON.stringify(exampleInput)}, ${JSON.stringify(\n exampleConfig\n )})`\n );\n }\n // attach messageHistory\n const { sessionId } = config.configurable;\n config.configurable.messageHistory = await this.getMessageHistory(\n sessionId\n );\n return config;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsGA,IAAa,6BAAb,cAGUA,+BAAqC;CAC7C;CAEA;CAEA;CAEA;CAEA;CAEA,YAAYC,QAA+D;EACzE,IAAIC,eAAyBC,8B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B,MAAI,CAAC,YACH,OAAM,IAAI,MACR,CAAC,yCAAyC,EAAE,KAAK,UAC/C,KACA,MACA,EACD,EAAE;EAGP,MAAM,iBAAiB,KAAK,mBAAmB,YAAY;EAC3D,MAAM,QAAQ,YAAY,CAAC,GAAG,eAAe,GAAG,cAAe,EAAC;CACjE;CAED,MAAM,aAAa,GAAG,SAA4C;EAChE,MAAM,SAAS,MAAM,MAAM,aAAa,GAAG,QAAQ;AAEnD,MAAI,CAAC,OAAO,gBAAgB,CAAC,OAAO,aAAa,WAAW;GAC1D,MAAM,eAAe,GAClB,KAAK,oBAAoB,UAAU,MACrC;GACD,MAAM,gBAAgB,EAAE,cAAc,EAAE,WAAW,MAAO,EAAE;AAC5D,SAAM,IAAI,MACR,CAAC,6GAA4F,EACvE,KAAK,UAAU,aAAa,CAAC,EAAE,EAAE,KAAK,UACxD,cACD,CAAC,CAAC,CAAC;EAET;EAED,MAAM,EAAE,WAAW,GAAG,OAAO;EAC7B,OAAO,aAAa,iBAAiB,MAAM,KAAK,kBAC9C,UACD;AACD,SAAO;CACR;AACF"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseMessage } from "../messages/base.cjs";
|
|
2
|
-
import { Run } from "../tracers/base.cjs";
|
|
3
2
|
import { RunnableConfig } from "./types.cjs";
|
|
3
|
+
import { Run } from "../tracers/base.cjs";
|
|
4
4
|
import { Runnable, RunnableBinding, RunnableBindingArgs } from "./base.cjs";
|
|
5
5
|
import { BaseChatMessageHistory, BaseListChatMessageHistory } from "../chat_history.cjs";
|
|
6
6
|
|
|
@@ -31,7 +31,7 @@ interface RunnableWithMessageHistoryInputs<RunInput, RunOutput> extends Omit<Run
|
|
|
31
31
|
* import { ChatAnthropic } from "@langchain/anthropic";
|
|
32
32
|
* import { UpstashRedisChatMessageHistory } from "@langchain/community/stores/message/upstash_redis";
|
|
33
33
|
* // For demos, you can also use an in-memory store:
|
|
34
|
-
* // import { ChatMessageHistory } from "langchain/stores/message/in_memory";
|
|
34
|
+
* // import { ChatMessageHistory } from "@langchain/classic/stores/message/in_memory";
|
|
35
35
|
*
|
|
36
36
|
* const prompt = ChatPromptTemplate.fromMessages([
|
|
37
37
|
* ["system", "You're an assistant who's good at {ability}"],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"history.d.cts","names":["BaseChatMessageHistory","BaseListChatMessageHistory","BaseMessage","Run","Runnable","RunnableBinding","RunnableBindingArgs","RunnableConfig","GetSessionHistoryCallable","Array","Promise","RunnableWithMessageHistoryInputs","RunInput","RunOutput","Omit","RunnableWithMessageHistory","Record","Partial"],"sources":["../../src/runnables/history.d.ts"],"sourcesContent":["import { BaseChatMessageHistory, BaseListChatMessageHistory } from \"../chat_history.js\";\nimport { BaseMessage } from \"../messages/index.js\";\nimport { Run } from \"../tracers/base.js\";\nimport { Runnable, RunnableBinding, type RunnableBindingArgs } from \"./base.js\";\nimport { RunnableConfig } from \"./config.js\";\ntype GetSessionHistoryCallable = (\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\n...args: Array<any>) => Promise<BaseChatMessageHistory | BaseListChatMessageHistory> | BaseChatMessageHistory | BaseListChatMessageHistory;\nexport interface RunnableWithMessageHistoryInputs<RunInput, RunOutput> extends Omit<RunnableBindingArgs<RunInput, RunOutput>, \"bound\" | \"config\"> {\n runnable: Runnable<RunInput, RunOutput>;\n getMessageHistory: GetSessionHistoryCallable;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n config?: RunnableConfig;\n}\n/**\n * Wraps a LCEL chain and manages history. It appends input messages\n * and chain outputs as history, and adds the current history messages to\n * the chain input.\n * @example\n * ```typescript\n * // pnpm install @langchain/anthropic @langchain/community @upstash/redis\n *\n * import {\n * ChatPromptTemplate,\n * MessagesPlaceholder,\n * } from \"@langchain/core/prompts\";\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n * import { UpstashRedisChatMessageHistory } from \"@langchain/community/stores/message/upstash_redis\";\n * // For demos, you can also use an in-memory store:\n * // import { ChatMessageHistory } from \"langchain/stores/message/in_memory\";\n *\n * const prompt = ChatPromptTemplate.fromMessages([\n * [\"system\", \"You're an assistant who's good at {ability}\"],\n * new MessagesPlaceholder(\"history\"),\n * [\"human\", \"{question}\"],\n * ]);\n *\n * const chain = prompt.pipe(new ChatAnthropic({}));\n *\n * const chainWithHistory = new RunnableWithMessageHistory({\n * runnable: chain,\n * getMessageHistory: (sessionId) =>\n * new UpstashRedisChatMessageHistory({\n * sessionId,\n * config: {\n * url: process.env.UPSTASH_REDIS_REST_URL!,\n * token: process.env.UPSTASH_REDIS_REST_TOKEN!,\n * },\n * }),\n * inputMessagesKey: \"question\",\n * historyMessagesKey: \"history\",\n * });\n *\n * const result = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What does cosine mean?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n *\n * const result2 = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What's its inverse?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n * ```\n */\nexport declare class RunnableWithMessageHistory<RunInput, RunOutput> extends RunnableBinding<RunInput, RunOutput> {\n runnable: Runnable<RunInput, RunOutput>;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n getMessageHistory: GetSessionHistoryCallable;\n constructor(fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>);\n _getInputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>): Array<BaseMessage>;\n _getOutputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>): Array<BaseMessage>;\n _enterHistory(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n input: any, kwargs?: RunnableConfig): Promise<BaseMessage[]>;\n _exitHistory(run: Run, config: RunnableConfig): Promise<void>;\n _mergeConfig(...configs: Array<RunnableConfig | undefined>): Promise<Partial<RunnableConfig<Record<string, any>>>>;\n}\nexport {};\n"],"mappings":";;;;;;;KAKKQ,yBAAAA;;AADwC,GAAA,IACxCA,EAEIC,KAFJD,CAAAA,GAAAA,CAAAA,EAAAA,GAEmBE,OAFnBF,CAE2BR,sBAFF,GAE2BC,0BAF3B,CAAA,GAEyDD,sBAFzD,GAEkFC,0BAFlF;AAAA,UAGbU,gCAHa,CAAA,QAAA,EAAA,SAAA,CAAA,SAGiDG,IAHjD,CAGsDR,mBAHtD,CAG0EM,QAH1E,EAGoFC,SAHpF,CAAA,EAAA,OAAA,GAAA,QAAA,CAAA,CAAA;EAAA,QAErBJ,EAEKL,QAFLK,CAEcG,QAFdH,EAEwBI,SAFxBJ,CAAAA;EAAK,iBAAkBT,EAGTQ,yBAHSR;EAAsB,gBAAGC,CAAAA,EAAAA,MAAAA;EAA0B,iBAA3DS,CAAAA,EAAAA,MAAAA;EAAO,kBAAwDV,CAAAA,EAAAA,MAAAA;EAAsB,MAAGC,CAAAA,EAOnGM,cAPmGN;AAA0B;AAC1I;;;;;;;;;;;AAAmF;AAwEnF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAA4F;;;;;;;;;;;;;;;;cAAvEc,wDAAwDV,gBAAgBO,UAAUC;YACzFT,SAASQ,UAAUC;;;;qBAIVL;sBACCG,iCAAiCC,UAAUC;;;uBAG1CX,cAAcO,MAAMP,eAAec,sBAAsBP,MAAMP;;;wBAG9DA,cAAcO,MAAMP,eAAec,sBAAsBP,MAAMP;;;uBAGhEK,iBAAiBG,QAAQR;oBAC5BC,aAAaI,iBAAiBG;2BACvBD,MAAMF,8BAA8BG,QAAQO,QAAQV,eAAeS"}
|
|
1
|
+
{"version":3,"file":"history.d.cts","names":["BaseChatMessageHistory","BaseListChatMessageHistory","BaseMessage","Run","Runnable","RunnableBinding","RunnableBindingArgs","RunnableConfig","GetSessionHistoryCallable","Array","Promise","RunnableWithMessageHistoryInputs","RunInput","RunOutput","Omit","RunnableWithMessageHistory","Record","Partial"],"sources":["../../src/runnables/history.d.ts"],"sourcesContent":["import { BaseChatMessageHistory, BaseListChatMessageHistory } from \"../chat_history.js\";\nimport { BaseMessage } from \"../messages/index.js\";\nimport { Run } from \"../tracers/base.js\";\nimport { Runnable, RunnableBinding, type RunnableBindingArgs } from \"./base.js\";\nimport { RunnableConfig } from \"./config.js\";\ntype GetSessionHistoryCallable = (\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\n...args: Array<any>) => Promise<BaseChatMessageHistory | BaseListChatMessageHistory> | BaseChatMessageHistory | BaseListChatMessageHistory;\nexport interface RunnableWithMessageHistoryInputs<RunInput, RunOutput> extends Omit<RunnableBindingArgs<RunInput, RunOutput>, \"bound\" | \"config\"> {\n runnable: Runnable<RunInput, RunOutput>;\n getMessageHistory: GetSessionHistoryCallable;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n config?: RunnableConfig;\n}\n/**\n * Wraps a LCEL chain and manages history. It appends input messages\n * and chain outputs as history, and adds the current history messages to\n * the chain input.\n * @example\n * ```typescript\n * // pnpm install @langchain/anthropic @langchain/community @upstash/redis\n *\n * import {\n * ChatPromptTemplate,\n * MessagesPlaceholder,\n * } from \"@langchain/core/prompts\";\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n * import { UpstashRedisChatMessageHistory } from \"@langchain/community/stores/message/upstash_redis\";\n * // For demos, you can also use an in-memory store:\n * // import { ChatMessageHistory } from \"@langchain/classic/stores/message/in_memory\";\n *\n * const prompt = ChatPromptTemplate.fromMessages([\n * [\"system\", \"You're an assistant who's good at {ability}\"],\n * new MessagesPlaceholder(\"history\"),\n * [\"human\", \"{question}\"],\n * ]);\n *\n * const chain = prompt.pipe(new ChatAnthropic({}));\n *\n * const chainWithHistory = new RunnableWithMessageHistory({\n * runnable: chain,\n * getMessageHistory: (sessionId) =>\n * new UpstashRedisChatMessageHistory({\n * sessionId,\n * config: {\n * url: process.env.UPSTASH_REDIS_REST_URL!,\n * token: process.env.UPSTASH_REDIS_REST_TOKEN!,\n * },\n * }),\n * inputMessagesKey: \"question\",\n * historyMessagesKey: \"history\",\n * });\n *\n * const result = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What does cosine mean?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n *\n * const result2 = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What's its inverse?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n * ```\n */\nexport declare class RunnableWithMessageHistory<RunInput, RunOutput> extends RunnableBinding<RunInput, RunOutput> {\n runnable: Runnable<RunInput, RunOutput>;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n getMessageHistory: GetSessionHistoryCallable;\n constructor(fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>);\n _getInputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>): Array<BaseMessage>;\n _getOutputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>): Array<BaseMessage>;\n _enterHistory(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n input: any, kwargs?: RunnableConfig): Promise<BaseMessage[]>;\n _exitHistory(run: Run, config: RunnableConfig): Promise<void>;\n _mergeConfig(...configs: Array<RunnableConfig | undefined>): Promise<Partial<RunnableConfig<Record<string, any>>>>;\n}\nexport {};\n"],"mappings":";;;;;;;KAKKQ,yBAAAA;;AADwC,GAAA,IACxCA,EAEIC,KAFJD,CAAAA,GAAAA,CAAAA,EAAAA,GAEmBE,OAFnBF,CAE2BR,sBAFF,GAE2BC,0BAF3B,CAAA,GAEyDD,sBAFzD,GAEkFC,0BAFlF;AAAA,UAGbU,gCAHa,CAAA,QAAA,EAAA,SAAA,CAAA,SAGiDG,IAHjD,CAGsDR,mBAHtD,CAG0EM,QAH1E,EAGoFC,SAHpF,CAAA,EAAA,OAAA,GAAA,QAAA,CAAA,CAAA;EAAA,QAErBJ,EAEKL,QAFLK,CAEcG,QAFdH,EAEwBI,SAFxBJ,CAAAA;EAAK,iBAAkBT,EAGTQ,yBAHSR;EAAsB,gBAAGC,CAAAA,EAAAA,MAAAA;EAA0B,iBAA3DS,CAAAA,EAAAA,MAAAA;EAAO,kBAAwDV,CAAAA,EAAAA,MAAAA;EAAsB,MAAGC,CAAAA,EAOnGM,cAPmGN;AAA0B;AAC1I;;;;;;;;;;;AAAmF;AAwEnF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAA4F;;;;;;;;;;;;;;;;cAAvEc,wDAAwDV,gBAAgBO,UAAUC;YACzFT,SAASQ,UAAUC;;;;qBAIVL;sBACCG,iCAAiCC,UAAUC;;;uBAG1CX,cAAcO,MAAMP,eAAec,sBAAsBP,MAAMP;;;wBAG9DA,cAAcO,MAAMP,eAAec,sBAAsBP,MAAMP;;;uBAGhEK,iBAAiBG,QAAQR;oBAC5BC,aAAaI,iBAAiBG;2BACvBD,MAAMF,8BAA8BG,QAAQO,QAAQV,eAAeS"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseMessage } from "../messages/base.js";
|
|
2
|
-
import { Run } from "../tracers/base.js";
|
|
3
2
|
import { RunnableConfig } from "./types.js";
|
|
3
|
+
import { Run } from "../tracers/base.js";
|
|
4
4
|
import { Runnable, RunnableBinding, RunnableBindingArgs } from "./base.js";
|
|
5
5
|
import { BaseChatMessageHistory, BaseListChatMessageHistory } from "../chat_history.js";
|
|
6
6
|
|
|
@@ -31,7 +31,7 @@ interface RunnableWithMessageHistoryInputs<RunInput, RunOutput> extends Omit<Run
|
|
|
31
31
|
* import { ChatAnthropic } from "@langchain/anthropic";
|
|
32
32
|
* import { UpstashRedisChatMessageHistory } from "@langchain/community/stores/message/upstash_redis";
|
|
33
33
|
* // For demos, you can also use an in-memory store:
|
|
34
|
-
* // import { ChatMessageHistory } from "langchain/stores/message/in_memory";
|
|
34
|
+
* // import { ChatMessageHistory } from "@langchain/classic/stores/message/in_memory";
|
|
35
35
|
*
|
|
36
36
|
* const prompt = ChatPromptTemplate.fromMessages([
|
|
37
37
|
* ["system", "You're an assistant who's good at {ability}"],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"history.d.ts","names":["BaseChatMessageHistory","BaseListChatMessageHistory","BaseMessage","Run","Runnable","RunnableBinding","RunnableBindingArgs","RunnableConfig","GetSessionHistoryCallable","Array","Promise","RunnableWithMessageHistoryInputs","RunInput","RunOutput","Omit","RunnableWithMessageHistory","Record","Partial"],"sources":["../../src/runnables/history.d.ts"],"sourcesContent":["import { BaseChatMessageHistory, BaseListChatMessageHistory } from \"../chat_history.js\";\nimport { BaseMessage } from \"../messages/index.js\";\nimport { Run } from \"../tracers/base.js\";\nimport { Runnable, RunnableBinding, type RunnableBindingArgs } from \"./base.js\";\nimport { RunnableConfig } from \"./config.js\";\ntype GetSessionHistoryCallable = (\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\n...args: Array<any>) => Promise<BaseChatMessageHistory | BaseListChatMessageHistory> | BaseChatMessageHistory | BaseListChatMessageHistory;\nexport interface RunnableWithMessageHistoryInputs<RunInput, RunOutput> extends Omit<RunnableBindingArgs<RunInput, RunOutput>, \"bound\" | \"config\"> {\n runnable: Runnable<RunInput, RunOutput>;\n getMessageHistory: GetSessionHistoryCallable;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n config?: RunnableConfig;\n}\n/**\n * Wraps a LCEL chain and manages history. It appends input messages\n * and chain outputs as history, and adds the current history messages to\n * the chain input.\n * @example\n * ```typescript\n * // pnpm install @langchain/anthropic @langchain/community @upstash/redis\n *\n * import {\n * ChatPromptTemplate,\n * MessagesPlaceholder,\n * } from \"@langchain/core/prompts\";\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n * import { UpstashRedisChatMessageHistory } from \"@langchain/community/stores/message/upstash_redis\";\n * // For demos, you can also use an in-memory store:\n * // import { ChatMessageHistory } from \"langchain/stores/message/in_memory\";\n *\n * const prompt = ChatPromptTemplate.fromMessages([\n * [\"system\", \"You're an assistant who's good at {ability}\"],\n * new MessagesPlaceholder(\"history\"),\n * [\"human\", \"{question}\"],\n * ]);\n *\n * const chain = prompt.pipe(new ChatAnthropic({}));\n *\n * const chainWithHistory = new RunnableWithMessageHistory({\n * runnable: chain,\n * getMessageHistory: (sessionId) =>\n * new UpstashRedisChatMessageHistory({\n * sessionId,\n * config: {\n * url: process.env.UPSTASH_REDIS_REST_URL!,\n * token: process.env.UPSTASH_REDIS_REST_TOKEN!,\n * },\n * }),\n * inputMessagesKey: \"question\",\n * historyMessagesKey: \"history\",\n * });\n *\n * const result = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What does cosine mean?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n *\n * const result2 = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What's its inverse?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n * ```\n */\nexport declare class RunnableWithMessageHistory<RunInput, RunOutput> extends RunnableBinding<RunInput, RunOutput> {\n runnable: Runnable<RunInput, RunOutput>;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n getMessageHistory: GetSessionHistoryCallable;\n constructor(fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>);\n _getInputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>): Array<BaseMessage>;\n _getOutputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>): Array<BaseMessage>;\n _enterHistory(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n input: any, kwargs?: RunnableConfig): Promise<BaseMessage[]>;\n _exitHistory(run: Run, config: RunnableConfig): Promise<void>;\n _mergeConfig(...configs: Array<RunnableConfig | undefined>): Promise<Partial<RunnableConfig<Record<string, any>>>>;\n}\nexport {};\n"],"mappings":";;;;;;;KAKKQ,yBAAAA;;AADwC,GAAA,IACxCA,EAEIC,KAFJD,CAAAA,GAAAA,CAAAA,EAAAA,GAEmBE,OAFnBF,CAE2BR,sBAFF,GAE2BC,0BAF3B,CAAA,GAEyDD,sBAFzD,GAEkFC,0BAFlF;AAAA,UAGbU,gCAHa,CAAA,QAAA,EAAA,SAAA,CAAA,SAGiDG,IAHjD,CAGsDR,mBAHtD,CAG0EM,QAH1E,EAGoFC,SAHpF,CAAA,EAAA,OAAA,GAAA,QAAA,CAAA,CAAA;EAAA,QAErBJ,EAEKL,QAFLK,CAEcG,QAFdH,EAEwBI,SAFxBJ,CAAAA;EAAK,iBAAkBT,EAGTQ,yBAHSR;EAAsB,gBAAGC,CAAAA,EAAAA,MAAAA;EAA0B,iBAA3DS,CAAAA,EAAAA,MAAAA;EAAO,kBAAwDV,CAAAA,EAAAA,MAAAA;EAAsB,MAAGC,CAAAA,EAOnGM,cAPmGN;AAA0B;AAC1I;;;;;;;;;;;AAAmF;AAwEnF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAA4F;;;;;;;;;;;;;;;;cAAvEc,wDAAwDV,gBAAgBO,UAAUC;YACzFT,SAASQ,UAAUC;;;;qBAIVL;sBACCG,iCAAiCC,UAAUC;;;uBAG1CX,cAAcO,MAAMP,eAAec,sBAAsBP,MAAMP;;;wBAG9DA,cAAcO,MAAMP,eAAec,sBAAsBP,MAAMP;;;uBAGhEK,iBAAiBG,QAAQR;oBAC5BC,aAAaI,iBAAiBG;2BACvBD,MAAMF,8BAA8BG,QAAQO,QAAQV,eAAeS"}
|
|
1
|
+
{"version":3,"file":"history.d.ts","names":["BaseChatMessageHistory","BaseListChatMessageHistory","BaseMessage","Run","Runnable","RunnableBinding","RunnableBindingArgs","RunnableConfig","GetSessionHistoryCallable","Array","Promise","RunnableWithMessageHistoryInputs","RunInput","RunOutput","Omit","RunnableWithMessageHistory","Record","Partial"],"sources":["../../src/runnables/history.d.ts"],"sourcesContent":["import { BaseChatMessageHistory, BaseListChatMessageHistory } from \"../chat_history.js\";\nimport { BaseMessage } from \"../messages/index.js\";\nimport { Run } from \"../tracers/base.js\";\nimport { Runnable, RunnableBinding, type RunnableBindingArgs } from \"./base.js\";\nimport { RunnableConfig } from \"./config.js\";\ntype GetSessionHistoryCallable = (\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\n...args: Array<any>) => Promise<BaseChatMessageHistory | BaseListChatMessageHistory> | BaseChatMessageHistory | BaseListChatMessageHistory;\nexport interface RunnableWithMessageHistoryInputs<RunInput, RunOutput> extends Omit<RunnableBindingArgs<RunInput, RunOutput>, \"bound\" | \"config\"> {\n runnable: Runnable<RunInput, RunOutput>;\n getMessageHistory: GetSessionHistoryCallable;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n config?: RunnableConfig;\n}\n/**\n * Wraps a LCEL chain and manages history. It appends input messages\n * and chain outputs as history, and adds the current history messages to\n * the chain input.\n * @example\n * ```typescript\n * // pnpm install @langchain/anthropic @langchain/community @upstash/redis\n *\n * import {\n * ChatPromptTemplate,\n * MessagesPlaceholder,\n * } from \"@langchain/core/prompts\";\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n * import { UpstashRedisChatMessageHistory } from \"@langchain/community/stores/message/upstash_redis\";\n * // For demos, you can also use an in-memory store:\n * // import { ChatMessageHistory } from \"@langchain/classic/stores/message/in_memory\";\n *\n * const prompt = ChatPromptTemplate.fromMessages([\n * [\"system\", \"You're an assistant who's good at {ability}\"],\n * new MessagesPlaceholder(\"history\"),\n * [\"human\", \"{question}\"],\n * ]);\n *\n * const chain = prompt.pipe(new ChatAnthropic({}));\n *\n * const chainWithHistory = new RunnableWithMessageHistory({\n * runnable: chain,\n * getMessageHistory: (sessionId) =>\n * new UpstashRedisChatMessageHistory({\n * sessionId,\n * config: {\n * url: process.env.UPSTASH_REDIS_REST_URL!,\n * token: process.env.UPSTASH_REDIS_REST_TOKEN!,\n * },\n * }),\n * inputMessagesKey: \"question\",\n * historyMessagesKey: \"history\",\n * });\n *\n * const result = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What does cosine mean?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n *\n * const result2 = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What's its inverse?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n * ```\n */\nexport declare class RunnableWithMessageHistory<RunInput, RunOutput> extends RunnableBinding<RunInput, RunOutput> {\n runnable: Runnable<RunInput, RunOutput>;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n getMessageHistory: GetSessionHistoryCallable;\n constructor(fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>);\n _getInputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>): Array<BaseMessage>;\n _getOutputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>): Array<BaseMessage>;\n _enterHistory(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n input: any, kwargs?: RunnableConfig): Promise<BaseMessage[]>;\n _exitHistory(run: Run, config: RunnableConfig): Promise<void>;\n _mergeConfig(...configs: Array<RunnableConfig | undefined>): Promise<Partial<RunnableConfig<Record<string, any>>>>;\n}\nexport {};\n"],"mappings":";;;;;;;KAKKQ,yBAAAA;;AADwC,GAAA,IACxCA,EAEIC,KAFJD,CAAAA,GAAAA,CAAAA,EAAAA,GAEmBE,OAFnBF,CAE2BR,sBAFF,GAE2BC,0BAF3B,CAAA,GAEyDD,sBAFzD,GAEkFC,0BAFlF;AAAA,UAGbU,gCAHa,CAAA,QAAA,EAAA,SAAA,CAAA,SAGiDG,IAHjD,CAGsDR,mBAHtD,CAG0EM,QAH1E,EAGoFC,SAHpF,CAAA,EAAA,OAAA,GAAA,QAAA,CAAA,CAAA;EAAA,QAErBJ,EAEKL,QAFLK,CAEcG,QAFdH,EAEwBI,SAFxBJ,CAAAA;EAAK,iBAAkBT,EAGTQ,yBAHSR;EAAsB,gBAAGC,CAAAA,EAAAA,MAAAA;EAA0B,iBAA3DS,CAAAA,EAAAA,MAAAA;EAAO,kBAAwDV,CAAAA,EAAAA,MAAAA;EAAsB,MAAGC,CAAAA,EAOnGM,cAPmGN;AAA0B;AAC1I;;;;;;;;;;;AAAmF;AAwEnF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAA4F;;;;;;;;;;;;;;;;cAAvEc,wDAAwDV,gBAAgBO,UAAUC;YACzFT,SAASQ,UAAUC;;;;qBAIVL;sBACCG,iCAAiCC,UAAUC;;;uBAG1CX,cAAcO,MAAMP,eAAec,sBAAsBP,MAAMP;;;wBAG9DA,cAAcO,MAAMP,eAAec,sBAAsBP,MAAMP;;;uBAGhEK,iBAAiBG,QAAQR;oBAC5BC,aAAaI,iBAAiBG;2BACvBD,MAAMF,8BAA8BG,QAAQO,QAAQV,eAAeS"}
|
|
@@ -21,7 +21,7 @@ import { RunnablePassthrough } from "./passthrough.js";
|
|
|
21
21
|
* import { ChatAnthropic } from "@langchain/anthropic";
|
|
22
22
|
* import { UpstashRedisChatMessageHistory } from "@langchain/community/stores/message/upstash_redis";
|
|
23
23
|
* // For demos, you can also use an in-memory store:
|
|
24
|
-
* // import { ChatMessageHistory } from "langchain/stores/message/in_memory";
|
|
24
|
+
* // import { ChatMessageHistory } from "@langchain/classic/stores/message/in_memory";
|
|
25
25
|
*
|
|
26
26
|
* const prompt = ChatPromptTemplate.fromMessages([
|
|
27
27
|
* ["system", "You're an assistant who's good at {ability}"],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"history.js","names":["fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>","historyChain: Runnable","config","inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>","outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>","input: any","kwargs?: RunnableConfig","run: Run","config: RunnableConfig"],"sources":["../../src/runnables/history.ts"],"sourcesContent":["import {\n BaseChatMessageHistory,\n BaseListChatMessageHistory,\n} from \"../chat_history.js\";\nimport {\n AIMessage,\n BaseMessage,\n HumanMessage,\n isBaseMessage,\n} from \"../messages/index.js\";\nimport { Run } from \"../tracers/base.js\";\nimport {\n Runnable,\n RunnableBinding,\n type RunnableBindingArgs,\n RunnableLambda,\n} from \"./base.js\";\nimport { RunnableConfig } from \"./config.js\";\nimport { RunnablePassthrough } from \"./passthrough.js\";\n\ntype GetSessionHistoryCallable = (\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ...args: Array<any>\n) =>\n | Promise<BaseChatMessageHistory | BaseListChatMessageHistory>\n | BaseChatMessageHistory\n | BaseListChatMessageHistory;\n\nexport interface RunnableWithMessageHistoryInputs<RunInput, RunOutput>\n extends Omit<RunnableBindingArgs<RunInput, RunOutput>, \"bound\" | \"config\"> {\n runnable: Runnable<RunInput, RunOutput>;\n getMessageHistory: GetSessionHistoryCallable;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n config?: RunnableConfig;\n}\n\n/**\n * Wraps a LCEL chain and manages history. It appends input messages\n * and chain outputs as history, and adds the current history messages to\n * the chain input.\n * @example\n * ```typescript\n * // pnpm install @langchain/anthropic @langchain/community @upstash/redis\n *\n * import {\n * ChatPromptTemplate,\n * MessagesPlaceholder,\n * } from \"@langchain/core/prompts\";\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n * import { UpstashRedisChatMessageHistory } from \"@langchain/community/stores/message/upstash_redis\";\n * // For demos, you can also use an in-memory store:\n * // import { ChatMessageHistory } from \"langchain/stores/message/in_memory\";\n *\n * const prompt = ChatPromptTemplate.fromMessages([\n * [\"system\", \"You're an assistant who's good at {ability}\"],\n * new MessagesPlaceholder(\"history\"),\n * [\"human\", \"{question}\"],\n * ]);\n *\n * const chain = prompt.pipe(new ChatAnthropic({}));\n *\n * const chainWithHistory = new RunnableWithMessageHistory({\n * runnable: chain,\n * getMessageHistory: (sessionId) =>\n * new UpstashRedisChatMessageHistory({\n * sessionId,\n * config: {\n * url: process.env.UPSTASH_REDIS_REST_URL!,\n * token: process.env.UPSTASH_REDIS_REST_TOKEN!,\n * },\n * }),\n * inputMessagesKey: \"question\",\n * historyMessagesKey: \"history\",\n * });\n *\n * const result = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What does cosine mean?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n *\n * const result2 = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What's its inverse?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n * ```\n */\nexport class RunnableWithMessageHistory<\n RunInput,\n RunOutput\n> extends RunnableBinding<RunInput, RunOutput> {\n runnable: Runnable<RunInput, RunOutput>;\n\n inputMessagesKey?: string;\n\n outputMessagesKey?: string;\n\n historyMessagesKey?: string;\n\n getMessageHistory: GetSessionHistoryCallable;\n\n constructor(fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>) {\n let historyChain: Runnable = RunnableLambda.from((input, options) =>\n this._enterHistory(input, options ?? {})\n ).withConfig({ runName: \"loadHistory\" });\n\n const messagesKey = fields.historyMessagesKey ?? fields.inputMessagesKey;\n if (messagesKey) {\n historyChain = RunnablePassthrough.assign({\n [messagesKey]: historyChain,\n }).withConfig({ runName: \"insertHistory\" });\n }\n\n const bound = historyChain\n .pipe(\n fields.runnable.withListeners({\n onEnd: (run, config) => this._exitHistory(run, config ?? {}),\n })\n )\n .withConfig({ runName: \"RunnableWithMessageHistory\" });\n\n const config = fields.config ?? {};\n\n super({\n ...fields,\n config,\n bound,\n });\n this.runnable = fields.runnable;\n this.getMessageHistory = fields.getMessageHistory;\n this.inputMessagesKey = fields.inputMessagesKey;\n this.outputMessagesKey = fields.outputMessagesKey;\n this.historyMessagesKey = fields.historyMessagesKey;\n }\n\n _getInputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedInputValue;\n if (\n typeof inputValue === \"object\" &&\n !Array.isArray(inputValue) &&\n !isBaseMessage(inputValue)\n ) {\n let key;\n if (this.inputMessagesKey) {\n key = this.inputMessagesKey;\n } else if (Object.keys(inputValue).length === 1) {\n key = Object.keys(inputValue)[0];\n } else {\n key = \"input\";\n }\n if (Array.isArray(inputValue[key]) && Array.isArray(inputValue[key][0])) {\n parsedInputValue = inputValue[key][0];\n } else {\n parsedInputValue = inputValue[key];\n }\n } else {\n parsedInputValue = inputValue;\n }\n if (typeof parsedInputValue === \"string\") {\n return [new HumanMessage(parsedInputValue)];\n } else if (Array.isArray(parsedInputValue)) {\n return parsedInputValue;\n } else if (isBaseMessage(parsedInputValue)) {\n return [parsedInputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages.\\nGot ${JSON.stringify(\n parsedInputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n _getOutputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedOutputValue;\n if (\n !Array.isArray(outputValue) &&\n !isBaseMessage(outputValue) &&\n typeof outputValue !== \"string\"\n ) {\n let key;\n if (this.outputMessagesKey !== undefined) {\n key = this.outputMessagesKey;\n } else if (Object.keys(outputValue).length === 1) {\n key = Object.keys(outputValue)[0];\n } else {\n key = \"output\";\n }\n // If you are wrapping a chat model directly\n // The output is actually this weird generations object\n if (outputValue.generations !== undefined) {\n parsedOutputValue = outputValue.generations[0][0].message;\n } else {\n parsedOutputValue = outputValue[key];\n }\n } else {\n parsedOutputValue = outputValue;\n }\n\n if (typeof parsedOutputValue === \"string\") {\n return [new AIMessage(parsedOutputValue)];\n } else if (Array.isArray(parsedOutputValue)) {\n return parsedOutputValue;\n } else if (isBaseMessage(parsedOutputValue)) {\n return [parsedOutputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages. Received: ${JSON.stringify(\n parsedOutputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n async _enterHistory(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n input: any,\n kwargs?: RunnableConfig\n ): Promise<BaseMessage[]> {\n const history = kwargs?.configurable?.messageHistory;\n const messages = await history.getMessages();\n if (this.historyMessagesKey === undefined) {\n return messages.concat(this._getInputMessages(input));\n }\n return messages;\n }\n\n async _exitHistory(run: Run, config: RunnableConfig): Promise<void> {\n const history = config.configurable?.messageHistory;\n\n // Get input messages\n let inputs;\n // Chat model inputs are nested arrays\n if (Array.isArray(run.inputs) && Array.isArray(run.inputs[0])) {\n inputs = run.inputs[0];\n } else {\n inputs = run.inputs;\n }\n let inputMessages = this._getInputMessages(inputs);\n // If historic messages were prepended to the input messages, remove them to\n // avoid adding duplicate messages to history.\n if (this.historyMessagesKey === undefined) {\n const existingMessages = await history.getMessages();\n inputMessages = inputMessages.slice(existingMessages.length);\n }\n // Get output messages\n const outputValue = run.outputs;\n if (!outputValue) {\n throw new Error(\n `Output values from 'Run' undefined. Run: ${JSON.stringify(\n run,\n null,\n 2\n )}`\n );\n }\n const outputMessages = this._getOutputMessages(outputValue);\n await history.addMessages([...inputMessages, ...outputMessages]);\n }\n\n async _mergeConfig(...configs: Array<RunnableConfig | undefined>) {\n const config = await super._mergeConfig(...configs);\n // Extract sessionId\n if (!config.configurable || !config.configurable.sessionId) {\n const exampleInput = {\n [this.inputMessagesKey ?? \"input\"]: \"foo\",\n };\n const exampleConfig = { configurable: { sessionId: \"123\" } };\n throw new Error(\n `sessionId is required. Pass it in as part of the config argument to .invoke() or .stream()\\n` +\n `eg. chain.invoke(${JSON.stringify(exampleInput)}, ${JSON.stringify(\n exampleConfig\n )})`\n );\n }\n // attach messageHistory\n const { sessionId } = config.configurable;\n config.configurable.messageHistory = await this.getMessageHistory(\n sessionId\n );\n return config;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsGA,IAAa,6BAAb,cAGU,gBAAqC;CAC7C;CAEA;CAEA;CAEA;CAEA;CAEA,YAAYA,QAA+D;EACzE,IAAIC,eAAyB,eAAe,KAAK,CAAC,OAAO,YACvD,KAAK,cAAc,OAAO,WAAW,CAAE,EAAC,CACzC,CAAC,WAAW,EAAE,SAAS,cAAe,EAAC;EAExC,MAAM,cAAc,OAAO,sBAAsB,OAAO;AACxD,MAAI,aACF,eAAe,oBAAoB,OAAO,GACvC,cAAc,aAChB,EAAC,CAAC,WAAW,EAAE,SAAS,gBAAiB,EAAC;EAG7C,MAAM,QAAQ,aACX,KACC,OAAO,SAAS,cAAc,EAC5B,OAAO,CAAC,KAAKC,aAAW,KAAK,aAAa,KAAKA,YAAU,CAAE,EAAC,CAC7D,EAAC,CACH,CACA,WAAW,EAAE,SAAS,6BAA8B,EAAC;EAExD,MAAM,SAAS,OAAO,UAAU,CAAE;EAElC,MAAM;GACJ,GAAG;GACH;GACA;EACD,EAAC;EACF,KAAK,WAAW,OAAO;EACvB,KAAK,oBAAoB,OAAO;EAChC,KAAK,mBAAmB,OAAO;EAC/B,KAAK,oBAAoB,OAAO;EAChC,KAAK,qBAAqB,OAAO;CAClC;CAED,kBAEEC,YACoB;EACpB,IAAI;AACJ,MACE,OAAO,eAAe,YACtB,CAAC,MAAM,Q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ageHistoryInputs<RunInput, RunOutput>","historyChain: Runnable","config","inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>","outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>","input: any","kwargs?: RunnableConfig","run: Run","config: RunnableConfig"],"sources":["../../src/runnables/history.ts"],"sourcesContent":["import {\n BaseChatMessageHistory,\n BaseListChatMessageHistory,\n} from \"../chat_history.js\";\nimport {\n AIMessage,\n BaseMessage,\n HumanMessage,\n isBaseMessage,\n} from \"../messages/index.js\";\nimport { Run } from \"../tracers/base.js\";\nimport {\n Runnable,\n RunnableBinding,\n type RunnableBindingArgs,\n RunnableLambda,\n} from \"./base.js\";\nimport { RunnableConfig } from \"./config.js\";\nimport { RunnablePassthrough } from \"./passthrough.js\";\n\ntype GetSessionHistoryCallable = (\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n ...args: Array<any>\n) =>\n | Promise<BaseChatMessageHistory | BaseListChatMessageHistory>\n | BaseChatMessageHistory\n | BaseListChatMessageHistory;\n\nexport interface RunnableWithMessageHistoryInputs<RunInput, RunOutput>\n extends Omit<RunnableBindingArgs<RunInput, RunOutput>, \"bound\" | \"config\"> {\n runnable: Runnable<RunInput, RunOutput>;\n getMessageHistory: GetSessionHistoryCallable;\n inputMessagesKey?: string;\n outputMessagesKey?: string;\n historyMessagesKey?: string;\n config?: RunnableConfig;\n}\n\n/**\n * Wraps a LCEL chain and manages history. It appends input messages\n * and chain outputs as history, and adds the current history messages to\n * the chain input.\n * @example\n * ```typescript\n * // pnpm install @langchain/anthropic @langchain/community @upstash/redis\n *\n * import {\n * ChatPromptTemplate,\n * MessagesPlaceholder,\n * } from \"@langchain/core/prompts\";\n * import { ChatAnthropic } from \"@langchain/anthropic\";\n * import { UpstashRedisChatMessageHistory } from \"@langchain/community/stores/message/upstash_redis\";\n * // For demos, you can also use an in-memory store:\n * // import { ChatMessageHistory } from \"@langchain/classic/stores/message/in_memory\";\n *\n * const prompt = ChatPromptTemplate.fromMessages([\n * [\"system\", \"You're an assistant who's good at {ability}\"],\n * new MessagesPlaceholder(\"history\"),\n * [\"human\", \"{question}\"],\n * ]);\n *\n * const chain = prompt.pipe(new ChatAnthropic({}));\n *\n * const chainWithHistory = new RunnableWithMessageHistory({\n * runnable: chain,\n * getMessageHistory: (sessionId) =>\n * new UpstashRedisChatMessageHistory({\n * sessionId,\n * config: {\n * url: process.env.UPSTASH_REDIS_REST_URL!,\n * token: process.env.UPSTASH_REDIS_REST_TOKEN!,\n * },\n * }),\n * inputMessagesKey: \"question\",\n * historyMessagesKey: \"history\",\n * });\n *\n * const result = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What does cosine mean?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n *\n * const result2 = await chainWithHistory.invoke(\n * {\n * ability: \"math\",\n * question: \"What's its inverse?\",\n * },\n * {\n * configurable: {\n * sessionId: \"some_string_identifying_a_user\",\n * },\n * }\n * );\n * ```\n */\nexport class RunnableWithMessageHistory<\n RunInput,\n RunOutput\n> extends RunnableBinding<RunInput, RunOutput> {\n runnable: Runnable<RunInput, RunOutput>;\n\n inputMessagesKey?: string;\n\n outputMessagesKey?: string;\n\n historyMessagesKey?: string;\n\n getMessageHistory: GetSessionHistoryCallable;\n\n constructor(fields: RunnableWithMessageHistoryInputs<RunInput, RunOutput>) {\n let historyChain: Runnable = RunnableLambda.from((input, options) =>\n this._enterHistory(input, options ?? {})\n ).withConfig({ runName: \"loadHistory\" });\n\n const messagesKey = fields.historyMessagesKey ?? fields.inputMessagesKey;\n if (messagesKey) {\n historyChain = RunnablePassthrough.assign({\n [messagesKey]: historyChain,\n }).withConfig({ runName: \"insertHistory\" });\n }\n\n const bound = historyChain\n .pipe(\n fields.runnable.withListeners({\n onEnd: (run, config) => this._exitHistory(run, config ?? {}),\n })\n )\n .withConfig({ runName: \"RunnableWithMessageHistory\" });\n\n const config = fields.config ?? {};\n\n super({\n ...fields,\n config,\n bound,\n });\n this.runnable = fields.runnable;\n this.getMessageHistory = fields.getMessageHistory;\n this.inputMessagesKey = fields.inputMessagesKey;\n this.outputMessagesKey = fields.outputMessagesKey;\n this.historyMessagesKey = fields.historyMessagesKey;\n }\n\n _getInputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n inputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedInputValue;\n if (\n typeof inputValue === \"object\" &&\n !Array.isArray(inputValue) &&\n !isBaseMessage(inputValue)\n ) {\n let key;\n if (this.inputMessagesKey) {\n key = this.inputMessagesKey;\n } else if (Object.keys(inputValue).length === 1) {\n key = Object.keys(inputValue)[0];\n } else {\n key = \"input\";\n }\n if (Array.isArray(inputValue[key]) && Array.isArray(inputValue[key][0])) {\n parsedInputValue = inputValue[key][0];\n } else {\n parsedInputValue = inputValue[key];\n }\n } else {\n parsedInputValue = inputValue;\n }\n if (typeof parsedInputValue === \"string\") {\n return [new HumanMessage(parsedInputValue)];\n } else if (Array.isArray(parsedInputValue)) {\n return parsedInputValue;\n } else if (isBaseMessage(parsedInputValue)) {\n return [parsedInputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages.\\nGot ${JSON.stringify(\n parsedInputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n _getOutputMessages(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>\n ): Array<BaseMessage> {\n let parsedOutputValue;\n if (\n !Array.isArray(outputValue) &&\n !isBaseMessage(outputValue) &&\n typeof outputValue !== \"string\"\n ) {\n let key;\n if (this.outputMessagesKey !== undefined) {\n key = this.outputMessagesKey;\n } else if (Object.keys(outputValue).length === 1) {\n key = Object.keys(outputValue)[0];\n } else {\n key = \"output\";\n }\n // If you are wrapping a chat model directly\n // The output is actually this weird generations object\n if (outputValue.generations !== undefined) {\n parsedOutputValue = outputValue.generations[0][0].message;\n } else {\n parsedOutputValue = outputValue[key];\n }\n } else {\n parsedOutputValue = outputValue;\n }\n\n if (typeof parsedOutputValue === \"string\") {\n return [new AIMessage(parsedOutputValue)];\n } else if (Array.isArray(parsedOutputValue)) {\n return parsedOutputValue;\n } else if (isBaseMessage(parsedOutputValue)) {\n return [parsedOutputValue];\n } else {\n throw new Error(\n `Expected a string, BaseMessage, or array of BaseMessages. Received: ${JSON.stringify(\n parsedOutputValue,\n null,\n 2\n )}`\n );\n }\n }\n\n async _enterHistory(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n input: any,\n kwargs?: RunnableConfig\n ): Promise<BaseMessage[]> {\n const history = kwargs?.configurable?.messageHistory;\n const messages = await history.getMessages();\n if (this.historyMessagesKey === undefined) {\n return messages.concat(this._getInputMessages(input));\n }\n return messages;\n }\n\n async _exitHistory(run: Run, config: RunnableConfig): Promise<void> {\n const history = config.configurable?.messageHistory;\n\n // Get input messages\n let inputs;\n // Chat model inputs are nested arrays\n if (Array.isArray(run.inputs) && Array.isArray(run.inputs[0])) {\n inputs = run.inputs[0];\n } else {\n inputs = run.inputs;\n }\n let inputMessages = this._getInputMessages(inputs);\n // If historic messages were prepended to the input messages, remove them to\n // avoid adding duplicate messages to history.\n if (this.historyMessagesKey === undefined) {\n const existingMessages = await history.getMessages();\n inputMessages = inputMessages.slice(existingMessages.length);\n }\n // Get output messages\n const outputValue = run.outputs;\n if (!outputValue) {\n throw new Error(\n `Output values from 'Run' undefined. Run: ${JSON.stringify(\n run,\n null,\n 2\n )}`\n );\n }\n const outputMessages = this._getOutputMessages(outputValue);\n await history.addMessages([...inputMessages, ...outputMessages]);\n }\n\n async _mergeConfig(...configs: Array<RunnableConfig | undefined>) {\n const config = await super._mergeConfig(...configs);\n // Extract sessionId\n if (!config.configurable || !config.configurable.sessionId) {\n const exampleInput = {\n [this.inputMessagesKey ?? \"input\"]: \"foo\",\n };\n const exampleConfig = { configurable: { sessionId: \"123\" } };\n throw new Error(\n `sessionId is required. Pass it in as part of the config argument to .invoke() or .stream()\\n` +\n `eg. chain.invoke(${JSON.stringify(exampleInput)}, ${JSON.stringify(\n exampleConfig\n )})`\n );\n }\n // attach messageHistory\n const { sessionId } = config.configurable;\n config.configurable.messageHistory = await this.getMessageHistory(\n sessionId\n );\n return config;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsGA,IAAa,6BAAb,cAGU,gBAAqC;CAC7C;CAEA;CAEA;CAEA;CAEA;CAEA,YAAYA,QAA+D;EACzE,IAAIC,eAAyB,eAAe,KAAK,CAAC,OAAO,YACvD,KAAK,cAAc,OAAO,WAAW,CAAE,EAAC,CACzC,CAAC,WAAW,EAAE,SAAS,cAAe,EAAC;EAExC,MAAM,cAAc,OAAO,sBAAsB,OAAO;AACxD,MAAI,aACF,eAAe,oBAAoB,OAAO,GACvC,cAAc,aAChB,EAAC,CAAC,WAAW,EAAE,SAAS,gBAAiB,EAAC;EAG7C,MAAM,QAAQ,aACX,KACC,OAAO,SAAS,cAAc,EAC5B,OAAO,CAAC,KAAKC,aAAW,KAAK,aAAa,KAAKA,YAAU,CAAE,EAAC,CAC7D,EAAC,CACH,CACA,WAAW,EAAE,SAAS,6BAA8B,EAAC;EAExD,MAAM,SAAS,OAAO,UAAU,CAAE;EAElC,MAAM;GACJ,GAAG;GACH;GACA;EACD,EAAC;EACF,KAAK,WAAW,OAAO;EACvB,KAAK,oBAAoB,OAAO;EAChC,KAAK,mBAAmB,OAAO;EAC/B,KAAK,oBAAoB,OAAO;EAChC,KAAK,qBAAqB,OAAO;CAClC;CAED,kBAEEC,YACoB;EACpB,IAAI;AACJ,MACE,OAAO,eAAe,YACtB,CAAC,MAAM,QAAQ,WAAW,IAC1B,CAAC,cAAc,WAAW,EAC1B;GACA,IAAI;AACJ,OAAI,KAAK,kBACP,MAAM,KAAK;YACF,OAAO,KAAK,WAAW,CAAC,WAAW,GAC5C,MAAM,OAAO,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