@librechat/agents 1.8.0 → 1.8.2
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/dist/cjs/graphs/Graph.cjs +5 -3
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +8 -30
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/run.cjs +4 -5
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +5 -3
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +8 -30
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/run.mjs +4 -5
- package/dist/esm/run.mjs.map +1 -1
- package/dist/types/graphs/Graph.d.ts +1 -1
- package/dist/types/run.d.ts +1 -1
- package/dist/types/types/run.d.ts +5 -0
- package/package.json +5 -5
- package/src/graphs/Graph.ts +5 -3
- package/src/llm/anthropic/utils/message_inputs.ts +8 -29
- package/src/proto/collab_design_v5.ts +1 -1
- package/src/run.ts +4 -6
- package/src/scripts/cli3.ts +2 -0
- package/src/types/run.ts +6 -0
|
@@ -63,13 +63,15 @@ class StandardGraph extends Graph {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
/* Init */
|
|
66
|
-
resetValues() {
|
|
66
|
+
resetValues(keepContent) {
|
|
67
67
|
this.messages = [];
|
|
68
68
|
this.config = graph.resetIfNotEmpty(this.config, undefined);
|
|
69
|
-
|
|
69
|
+
if (keepContent !== true) {
|
|
70
|
+
this.contentData = graph.resetIfNotEmpty(this.contentData, []);
|
|
71
|
+
this.contentIndexMap = graph.resetIfNotEmpty(this.contentIndexMap, new Map());
|
|
72
|
+
}
|
|
70
73
|
this.stepKeyIds = graph.resetIfNotEmpty(this.stepKeyIds, new Map());
|
|
71
74
|
this.toolCallStepIds = graph.resetIfNotEmpty(this.toolCallStepIds, new Map());
|
|
72
|
-
this.contentIndexMap = graph.resetIfNotEmpty(this.contentIndexMap, new Map());
|
|
73
75
|
this.messageIdsByStepKey = graph.resetIfNotEmpty(this.messageIdsByStepKey, new Map());
|
|
74
76
|
this.prelimMessageIdsByStepKey = graph.resetIfNotEmpty(this.prelimMessageIdsByStepKey, new Map());
|
|
75
77
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Graph.cjs","sources":["../../../src/graphs/Graph.ts"],"sourcesContent":["// src/graphs/Graph.ts\nimport { nanoid } from 'nanoid';\nimport { concat } from '@langchain/core/utils/stream';\nimport { ToolNode } from '@langchain/langgraph/prebuilt';\nimport { START, END, StateGraph } from '@langchain/langgraph';\nimport { Runnable, RunnableConfig } from '@langchain/core/runnables';\nimport { dispatchCustomEvent } from '@langchain/core/callbacks/dispatch';\nimport { AIMessageChunk, ToolMessage, SystemMessage } from '@langchain/core/messages';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport type * as t from '@/types';\nimport { Providers, GraphEvents, GraphNodeKeys, StepTypes, Callback } from '@/common';\nimport { getChatModelClass, manualToolStreamProviders } from '@/llm/providers';\nimport { ToolNode as CustomToolNode, toolsCondition } from '@/tools/ToolNode';\nimport { modifyDeltaProperties, convertMessagesToContent } from '@/messages';\nimport { resetIfNotEmpty, joinKeys, sleep } from '@/utils';\nimport { HandlerRegistry } from '@/events';\n\nconst { AGENT, TOOLS } = GraphNodeKeys;\nexport type GraphNode = GraphNodeKeys | typeof START;\nexport type ClientCallback<T extends unknown[]> = (graph: StandardGraph, ...args: T) => void;\nexport type ClientCallbacks = {\n [Callback.TOOL_ERROR]?: ClientCallback<[Error, string]>;\n [Callback.TOOL_START]?: ClientCallback<unknown[]>;\n [Callback.TOOL_END]?: ClientCallback<unknown[]>;\n}\nexport type SystemCallbacks = {\n [K in keyof ClientCallbacks]: ClientCallbacks[K] extends ClientCallback<infer Args>\n ? (...args: Args) => void\n : never;\n};\n\nexport abstract class Graph<\n T extends t.BaseGraphState = t.BaseGraphState,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n TNodeName extends string = string,\n> {\n abstract resetValues(): void;\n abstract createGraphState(): t.GraphStateChannels<T>;\n abstract initializeTools(): CustomToolNode<T> | ToolNode<T>;\n abstract initializeModel(): Runnable;\n abstract getRunMessages(): BaseMessage[] | undefined;\n abstract getContentParts(): t.MessageContentComplex[] | undefined;\n abstract generateStepId(stepKey: string): [string, number];\n abstract getKeyList(metadata: Record<string, unknown> | undefined): (string | number | undefined)[];\n abstract getStepKey(metadata: Record<string, unknown> | undefined): string;\n abstract checkKeyList(keyList: (string | number | undefined)[]): boolean;\n abstract getStepIdByKey(stepKey: string, index?: number): string\n abstract getRunStep(stepId: string): t.RunStep | undefined;\n abstract dispatchRunStep(stepKey: string, stepDetails: t.StepDetails): string;\n abstract dispatchRunStepDelta(id: string, delta: t.ToolCallDelta): void;\n abstract dispatchMessageDelta(id: string, delta: t.MessageDelta): void;\n abstract handleToolCallCompleted(data: t.ToolEndData, metadata?: Record<string, unknown>): void;\n\n abstract createCallModel(): (state: T, config?: RunnableConfig) => Promise<Partial<T>>;\n abstract createWorkflow(): t.CompiledWorkflow<T>;\n messageIdsByStepKey: Map<string, string> = new Map();\n prelimMessageIdsByStepKey: Map<string, string> = new Map();\n config: RunnableConfig | undefined;\n contentData: t.RunStep[] = [];\n stepKeyIds: Map<string, string[]> = new Map<string, string[]>();\n contentIndexMap: Map<string, number> = new Map();\n toolCallStepIds: Map<string, string> = new Map();\n /** The amount of time that should pass before another consecutive API call */\n streamBuffer: number | undefined;\n signal?: AbortSignal;\n}\n\nexport class StandardGraph extends Graph<\n t.BaseGraphState,\n GraphNode\n> {\n private graphState: t.GraphStateChannels<t.BaseGraphState>;\n private clientOptions: Record<string, unknown>;\n boundModel: Runnable;\n /** The last recorded timestamp that a stream API call was invoked */\n lastStreamCall: number | undefined;\n handlerRegistry: HandlerRegistry | undefined;\n systemMessage: SystemMessage | undefined;\n messages: BaseMessage[] = [];\n runId: string | undefined;\n tools?: t.GenericTool[];\n toolMap?: t.ToolMap;\n startIndex: number = 0;\n provider: Providers;\n toolEnd: boolean;\n signal: AbortSignal | undefined;\n\n constructor({\n runId,\n tools,\n toolMap,\n provider,\n clientOptions,\n instructions,\n signal,\n additional_instructions = '',\n streamBuffer,\n toolEnd = false,\n } : {\n runId?: string;\n signal?: AbortSignal;\n provider: Providers;\n tools?: t.GenericTool[];\n toolMap?: t.ToolMap;\n clientOptions: Record<string, unknown>;\n instructions?: string;\n additional_instructions?: string;\n streamBuffer?: number;\n toolEnd?: boolean;\n }) {\n super();\n this.runId = runId;\n this.tools = tools;\n this.toolMap = toolMap;\n this.provider = provider;\n this.signal = signal;\n this.clientOptions = clientOptions;\n this.streamBuffer = streamBuffer;\n this.graphState = this.createGraphState();\n this.boundModel = this.initializeModel();\n this.toolEnd = toolEnd;\n\n let finalInstructions = instructions ?? '';\n if (additional_instructions) {\n finalInstructions = finalInstructions ? `${finalInstructions}\\n\\n${additional_instructions}` : additional_instructions;\n }\n\n if (finalInstructions) {\n this.systemMessage = new SystemMessage(finalInstructions);\n }\n }\n\n /* Init */\n\n resetValues(): void {\n this.messages = [];\n this.config = resetIfNotEmpty(this.config, undefined);\n this.contentData = resetIfNotEmpty(this.contentData, []);\n this.stepKeyIds = resetIfNotEmpty(this.stepKeyIds, new Map());\n this.toolCallStepIds = resetIfNotEmpty(this.toolCallStepIds, new Map());\n this.contentIndexMap = resetIfNotEmpty(this.contentIndexMap, new Map());\n this.messageIdsByStepKey = resetIfNotEmpty(this.messageIdsByStepKey, new Map());\n this.prelimMessageIdsByStepKey = resetIfNotEmpty(this.prelimMessageIdsByStepKey, new Map());\n }\n\n /* Run Step Processing */\n\n getRunStep(stepId: string): t.RunStep | undefined {\n const index = this.contentIndexMap.get(stepId);\n if (index !== undefined) {\n return this.contentData[index];\n }\n return undefined;\n }\n\n getStepKey(metadata: Record<string, unknown> | undefined): string {\n if (!metadata) return '';\n\n const keyList = this.getKeyList(metadata);\n if (this.checkKeyList(keyList)) {\n throw new Error('Missing metadata');\n }\n\n return joinKeys(keyList);\n }\n\n getStepIdByKey(stepKey: string, index?: number): string {\n const stepIds = this.stepKeyIds.get(stepKey);\n if (!stepIds) {\n throw new Error(`No step IDs found for stepKey ${stepKey}`);\n }\n\n if (index === undefined) {\n return stepIds[stepIds.length - 1];\n }\n\n return stepIds[index];\n }\n\n generateStepId(stepKey: string): [string, number] {\n const stepIds = this.stepKeyIds.get(stepKey);\n let newStepId: string | undefined;\n let stepIndex = 0;\n if (stepIds) {\n stepIndex = stepIds.length;\n newStepId = `step_${nanoid()}`;\n stepIds.push(newStepId);\n this.stepKeyIds.set(stepKey, stepIds);\n } else {\n newStepId = `step_${nanoid()}`;\n this.stepKeyIds.set(stepKey, [newStepId]);\n }\n\n return [newStepId, stepIndex];\n }\n\n getKeyList(metadata: Record<string, unknown> | undefined): (string | number | undefined)[] {\n if (!metadata) return [];\n\n return [\n metadata.run_id as string,\n metadata.thread_id as string,\n metadata.langgraph_node as string,\n metadata.langgraph_step as number,\n metadata.checkpoint_ns as string,\n ];\n }\n\n checkKeyList(keyList: (string | number | undefined)[]): boolean {\n return keyList.some((key) => key === undefined);\n }\n\n /* Misc.*/\n\n getRunMessages(): BaseMessage[] | undefined {\n return this.messages.slice(this.startIndex);\n }\n\n getContentParts(): t.MessageContentComplex[] | undefined {\n return convertMessagesToContent(this.messages.slice(this.startIndex));\n }\n\n /* Graph */\n\n createGraphState(): t.GraphStateChannels<t.BaseGraphState> {\n return {\n messages: {\n value: (x: BaseMessage[], y: BaseMessage[]): BaseMessage[] => {\n if (!x.length) {\n if (this.systemMessage) {\n x.push(this.systemMessage);\n }\n\n this.startIndex = x.length + y.length;\n }\n const current = x.concat(y);\n this.messages = current;\n return current;\n },\n default: () => [],\n },\n };\n }\n\n initializeTools(): CustomToolNode<t.BaseGraphState> | ToolNode<t.BaseGraphState> {\n // return new ToolNode<t.BaseGraphState>(this.tools);\n return new CustomToolNode<t.BaseGraphState>({\n tools: this.tools || [],\n toolMap: this.toolMap,\n });\n }\n\n initializeModel(): Runnable {\n const ChatModelClass = getChatModelClass(this.provider);\n const model = new ChatModelClass(this.clientOptions);\n\n if (!this.tools || this.tools.length === 0) {\n return model as unknown as Runnable;\n }\n\n return (model as t.ModelWithTools).bindTools(this.tools);\n }\n\n getNewModel({\n clientOptions = {},\n omitOriginalOptions,\n } : {\n clientOptions?: t.ClientOptions;\n omitOriginalOptions?: string[]\n }): t.ChatModelInstance {\n const ChatModelClass = getChatModelClass(this.provider);\n const _options = omitOriginalOptions ? Object.fromEntries(\n Object.entries(this.clientOptions).filter(([key]) => !omitOriginalOptions.includes(key)),\n ) : this.clientOptions;\n const options = Object.assign(_options, clientOptions);\n return new ChatModelClass(options);\n }\n\n createCallModel() {\n return async (state: t.BaseGraphState, config?: RunnableConfig): Promise<Partial<t.BaseGraphState>> => {\n const { provider = '' } = (config?.configurable as t.GraphConfig | undefined) ?? {} ;\n if (!config || !provider) {\n throw new Error(`No ${config ? 'provider' : 'config'} provided`);\n }\n if (!config.signal) {\n config.signal = this.signal;\n }\n this.config = config;\n const { messages } = state;\n\n const finalMessages = messages;\n const lastMessageX = finalMessages[finalMessages.length - 2];\n const lastMessageY = finalMessages[finalMessages.length - 1];\n\n if (\n provider === Providers.BEDROCK\n && lastMessageX instanceof AIMessageChunk\n && lastMessageY instanceof ToolMessage\n && typeof lastMessageX.content === 'string'\n ) {\n finalMessages[finalMessages.length - 2].content = '';\n }\n\n if (this.lastStreamCall != null && this.streamBuffer != null) {\n const timeSinceLastCall = Date.now() - this.lastStreamCall;\n if (timeSinceLastCall < this.streamBuffer) {\n const timeToWait = Math.ceil((this.streamBuffer - timeSinceLastCall) / 1000) * 1000;\n await sleep(timeToWait);\n }\n }\n\n this.lastStreamCall = Date.now();\n\n if ((this.tools?.length ?? 0) > 0 && manualToolStreamProviders.has(provider)) {\n const stream = await this.boundModel.stream(finalMessages, config);\n let finalChunk: AIMessageChunk | undefined;\n for await (const chunk of stream) {\n dispatchCustomEvent(GraphEvents.CHAT_MODEL_STREAM, { chunk }, config);\n if (!finalChunk) {\n finalChunk = chunk;\n } else {\n finalChunk = concat(finalChunk, chunk);\n }\n }\n\n finalChunk = modifyDeltaProperties(finalChunk);\n return { messages: [finalChunk as AIMessageChunk] };\n }\n\n const finalMessage = await this.boundModel.invoke(finalMessages, config);\n return { messages: [finalMessage as AIMessageChunk] };\n };\n }\n\n createWorkflow(): t.CompiledWorkflow<t.BaseGraphState> {\n const routeMessage = (state: t.BaseGraphState, config?: RunnableConfig): string => {\n this.config = config;\n // const lastMessage = state.messages[state.messages.length - 1] as AIMessage;\n // if (!lastMessage?.tool_calls?.length) {\n // return END;\n // }\n // return TOOLS;\n return toolsCondition(state);\n };\n\n const workflow = new StateGraph<t.BaseGraphState>({\n channels: this.graphState,\n })\n .addNode(AGENT, this.createCallModel())\n .addNode(TOOLS, this.initializeTools())\n .addEdge(START, AGENT)\n .addConditionalEdges(AGENT, routeMessage)\n .addEdge(TOOLS, this.toolEnd ? END : AGENT);\n\n return workflow.compile();\n }\n\n /* Dispatchers */\n\n /**\n * Dispatches a run step to the client, returns the step ID\n */\n dispatchRunStep(stepKey: string, stepDetails: t.StepDetails): string {\n if (!this.config) {\n throw new Error('No config provided');\n }\n\n const [stepId, stepIndex] = this.generateStepId(stepKey);\n if (stepDetails.type === StepTypes.TOOL_CALLS && stepDetails.tool_calls) {\n for (const tool_call of stepDetails.tool_calls) {\n const toolCallId = tool_call.id ?? '';\n if (!toolCallId || this.toolCallStepIds.has(toolCallId)) {\n continue;\n }\n this.toolCallStepIds.set(toolCallId, stepId);\n }\n }\n\n const runStep: t.RunStep = {\n stepIndex,\n id: stepId,\n type: stepDetails.type,\n index: this.contentData.length,\n stepDetails,\n usage: null,\n };\n\n const runId = this.runId ?? '';\n if (runId) {\n runStep.runId = runId;\n }\n\n this.contentData.push(runStep);\n this.contentIndexMap.set(stepId, runStep.index);\n dispatchCustomEvent(GraphEvents.ON_RUN_STEP, runStep, this.config);\n return stepId;\n }\n\n handleToolCallCompleted(data: t.ToolEndData, metadata?: Record<string, unknown>): void {\n if (!this.config) {\n throw new Error('No config provided');\n }\n\n if (!data.output) {\n return;\n }\n\n const { input, output } = data;\n const { tool_call_id } = output;\n const stepId = this.toolCallStepIds.get(tool_call_id) ?? '';\n if (!stepId) {\n throw new Error(`No stepId found for tool_call_id ${tool_call_id}`);\n }\n\n const runStep = this.getRunStep(stepId);\n if (!runStep) {\n throw new Error(`No run step found for stepId ${stepId}`);\n }\n\n const args = typeof input === 'string' ? input : input.input;\n const tool_call = {\n args: typeof args === 'string' ? args : JSON.stringify(args),\n name: output.name ?? '',\n id: output.tool_call_id,\n output: typeof output.content === 'string'\n ? output.content\n : JSON.stringify(output.content),\n progress: 1,\n };\n\n this.handlerRegistry?.getHandler(GraphEvents.ON_RUN_STEP_COMPLETED)?.handle(\n GraphEvents.ON_RUN_STEP_COMPLETED,\n { result: {\n id: stepId,\n index: runStep.index,\n type: 'tool_call',\n tool_call\n } as t.ToolCompleteEvent,\n },\n metadata,\n this,\n );\n }\n\n dispatchRunStepDelta(id: string, delta: t.ToolCallDelta): void {\n if (!this.config) {\n throw new Error('No config provided');\n } else if (!id) {\n throw new Error('No step ID found');\n }\n const runStepDelta: t.RunStepDeltaEvent = {\n id,\n delta,\n };\n dispatchCustomEvent(GraphEvents.ON_RUN_STEP_DELTA, runStepDelta, this.config);\n }\n\n dispatchMessageDelta(id: string, delta: t.MessageDelta): void {\n if (!this.config) {\n throw new Error('No config provided');\n }\n const messageDelta: t.MessageDeltaEvent = {\n id,\n delta,\n };\n dispatchCustomEvent(GraphEvents.ON_MESSAGE_DELTA, messageDelta, this.config);\n }\n}\n"],"names":["GraphNodeKeys","SystemMessage","resetIfNotEmpty","joinKeys","nanoid","convertMessagesToContent","CustomToolNode","getChatModelClass","messages","Providers","AIMessageChunk","ToolMessage","sleep","manualToolStreamProviders","stream","dispatchCustomEv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\nimport { nanoid } from 'nanoid';\nimport { concat } from '@langchain/core/utils/stream';\nimport { ToolNode } from '@langchain/langgraph/prebuilt';\nimport { START, END, StateGraph } from '@langchain/langgraph';\nimport { Runnable, RunnableConfig } from '@langchain/core/runnables';\nimport { dispatchCustomEvent } from '@langchain/core/callbacks/dispatch';\nimport { AIMessageChunk, ToolMessage, SystemMessage } from '@langchain/core/messages';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport type * as t from '@/types';\nimport { Providers, GraphEvents, GraphNodeKeys, StepTypes, Callback } from '@/common';\nimport { getChatModelClass, manualToolStreamProviders } from '@/llm/providers';\nimport { ToolNode as CustomToolNode, toolsCondition } from '@/tools/ToolNode';\nimport { modifyDeltaProperties, convertMessagesToContent } from '@/messages';\nimport { resetIfNotEmpty, joinKeys, sleep } from '@/utils';\nimport { HandlerRegistry } from '@/events';\n\nconst { AGENT, TOOLS } = GraphNodeKeys;\nexport type GraphNode = GraphNodeKeys | typeof START;\nexport type ClientCallback<T extends unknown[]> = (graph: StandardGraph, ...args: T) => void;\nexport type ClientCallbacks = {\n [Callback.TOOL_ERROR]?: ClientCallback<[Error, string]>;\n [Callback.TOOL_START]?: ClientCallback<unknown[]>;\n [Callback.TOOL_END]?: ClientCallback<unknown[]>;\n}\nexport type SystemCallbacks = {\n [K in keyof ClientCallbacks]: ClientCallbacks[K] extends ClientCallback<infer Args>\n ? (...args: Args) => void\n : never;\n};\n\nexport abstract class Graph<\n T extends t.BaseGraphState = t.BaseGraphState,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n TNodeName extends string = string,\n> {\n abstract resetValues(): void;\n abstract createGraphState(): t.GraphStateChannels<T>;\n abstract initializeTools(): CustomToolNode<T> | ToolNode<T>;\n abstract initializeModel(): Runnable;\n abstract getRunMessages(): BaseMessage[] | undefined;\n abstract getContentParts(): t.MessageContentComplex[] | undefined;\n abstract generateStepId(stepKey: string): [string, number];\n abstract getKeyList(metadata: Record<string, unknown> | undefined): (string | number | undefined)[];\n abstract getStepKey(metadata: Record<string, unknown> | undefined): string;\n abstract checkKeyList(keyList: (string | number | undefined)[]): boolean;\n abstract getStepIdByKey(stepKey: string, index?: number): string\n abstract getRunStep(stepId: string): t.RunStep | undefined;\n abstract dispatchRunStep(stepKey: string, stepDetails: t.StepDetails): string;\n abstract dispatchRunStepDelta(id: string, delta: t.ToolCallDelta): void;\n abstract dispatchMessageDelta(id: string, delta: t.MessageDelta): void;\n abstract handleToolCallCompleted(data: t.ToolEndData, metadata?: Record<string, unknown>): void;\n\n abstract createCallModel(): (state: T, config?: RunnableConfig) => Promise<Partial<T>>;\n abstract createWorkflow(): t.CompiledWorkflow<T>;\n messageIdsByStepKey: Map<string, string> = new Map();\n prelimMessageIdsByStepKey: Map<string, string> = new Map();\n config: RunnableConfig | undefined;\n contentData: t.RunStep[] = [];\n stepKeyIds: Map<string, string[]> = new Map<string, string[]>();\n contentIndexMap: Map<string, number> = new Map();\n toolCallStepIds: Map<string, string> = new Map();\n /** The amount of time that should pass before another consecutive API call */\n streamBuffer: number | undefined;\n signal?: AbortSignal;\n}\n\nexport class StandardGraph extends Graph<\n t.BaseGraphState,\n GraphNode\n> {\n private graphState: t.GraphStateChannels<t.BaseGraphState>;\n private clientOptions: Record<string, unknown>;\n boundModel: Runnable;\n /** The last recorded timestamp that a stream API call was invoked */\n lastStreamCall: number | undefined;\n handlerRegistry: HandlerRegistry | undefined;\n systemMessage: SystemMessage | undefined;\n messages: BaseMessage[] = [];\n runId: string | undefined;\n tools?: t.GenericTool[];\n toolMap?: t.ToolMap;\n startIndex: number = 0;\n provider: Providers;\n toolEnd: boolean;\n signal: AbortSignal | undefined;\n\n constructor({\n runId,\n tools,\n toolMap,\n provider,\n clientOptions,\n instructions,\n signal,\n additional_instructions = '',\n streamBuffer,\n toolEnd = false,\n } : {\n runId?: string;\n signal?: AbortSignal;\n provider: Providers;\n tools?: t.GenericTool[];\n toolMap?: t.ToolMap;\n clientOptions: Record<string, unknown>;\n instructions?: string;\n additional_instructions?: string;\n streamBuffer?: number;\n toolEnd?: boolean;\n }) {\n super();\n this.runId = runId;\n this.tools = tools;\n this.toolMap = toolMap;\n this.provider = provider;\n this.signal = signal;\n this.clientOptions = clientOptions;\n this.streamBuffer = streamBuffer;\n this.graphState = this.createGraphState();\n this.boundModel = this.initializeModel();\n this.toolEnd = toolEnd;\n\n let finalInstructions = instructions ?? '';\n if (additional_instructions) {\n finalInstructions = finalInstructions ? `${finalInstructions}\\n\\n${additional_instructions}` : additional_instructions;\n }\n\n if (finalInstructions) {\n this.systemMessage = new SystemMessage(finalInstructions);\n }\n }\n\n /* Init */\n\n resetValues(keepContent?: boolean): void {\n this.messages = [];\n this.config = resetIfNotEmpty(this.config, undefined);\n if (keepContent !== true) {\n this.contentData = resetIfNotEmpty(this.contentData, []);\n this.contentIndexMap = resetIfNotEmpty(this.contentIndexMap, new Map());\n }\n this.stepKeyIds = resetIfNotEmpty(this.stepKeyIds, new Map());\n this.toolCallStepIds = resetIfNotEmpty(this.toolCallStepIds, new Map());\n this.messageIdsByStepKey = resetIfNotEmpty(this.messageIdsByStepKey, new Map());\n this.prelimMessageIdsByStepKey = resetIfNotEmpty(this.prelimMessageIdsByStepKey, new Map());\n }\n\n /* Run Step Processing */\n\n getRunStep(stepId: string): t.RunStep | undefined {\n const index = this.contentIndexMap.get(stepId);\n if (index !== undefined) {\n return this.contentData[index];\n }\n return undefined;\n }\n\n getStepKey(metadata: Record<string, unknown> | undefined): string {\n if (!metadata) return '';\n\n const keyList = this.getKeyList(metadata);\n if (this.checkKeyList(keyList)) {\n throw new Error('Missing metadata');\n }\n\n return joinKeys(keyList);\n }\n\n getStepIdByKey(stepKey: string, index?: number): string {\n const stepIds = this.stepKeyIds.get(stepKey);\n if (!stepIds) {\n throw new Error(`No step IDs found for stepKey ${stepKey}`);\n }\n\n if (index === undefined) {\n return stepIds[stepIds.length - 1];\n }\n\n return stepIds[index];\n }\n\n generateStepId(stepKey: string): [string, number] {\n const stepIds = this.stepKeyIds.get(stepKey);\n let newStepId: string | undefined;\n let stepIndex = 0;\n if (stepIds) {\n stepIndex = stepIds.length;\n newStepId = `step_${nanoid()}`;\n stepIds.push(newStepId);\n this.stepKeyIds.set(stepKey, stepIds);\n } else {\n newStepId = `step_${nanoid()}`;\n this.stepKeyIds.set(stepKey, [newStepId]);\n }\n\n return [newStepId, stepIndex];\n }\n\n getKeyList(metadata: Record<string, unknown> | undefined): (string | number | undefined)[] {\n if (!metadata) return [];\n\n return [\n metadata.run_id as string,\n metadata.thread_id as string,\n metadata.langgraph_node as string,\n metadata.langgraph_step as number,\n metadata.checkpoint_ns as string,\n ];\n }\n\n checkKeyList(keyList: (string | number | undefined)[]): boolean {\n return keyList.some((key) => key === undefined);\n }\n\n /* Misc.*/\n\n getRunMessages(): BaseMessage[] | undefined {\n return this.messages.slice(this.startIndex);\n }\n\n getContentParts(): t.MessageContentComplex[] | undefined {\n return convertMessagesToContent(this.messages.slice(this.startIndex));\n }\n\n /* Graph */\n\n createGraphState(): t.GraphStateChannels<t.BaseGraphState> {\n return {\n messages: {\n value: (x: BaseMessage[], y: BaseMessage[]): BaseMessage[] => {\n if (!x.length) {\n if (this.systemMessage) {\n x.push(this.systemMessage);\n }\n\n this.startIndex = x.length + y.length;\n }\n const current = x.concat(y);\n this.messages = current;\n return current;\n },\n default: () => [],\n },\n };\n }\n\n initializeTools(): CustomToolNode<t.BaseGraphState> | ToolNode<t.BaseGraphState> {\n // return new ToolNode<t.BaseGraphState>(this.tools);\n return new CustomToolNode<t.BaseGraphState>({\n tools: this.tools || [],\n toolMap: this.toolMap,\n });\n }\n\n initializeModel(): Runnable {\n const ChatModelClass = getChatModelClass(this.provider);\n const model = new ChatModelClass(this.clientOptions);\n\n if (!this.tools || this.tools.length === 0) {\n return model as unknown as Runnable;\n }\n\n return (model as t.ModelWithTools).bindTools(this.tools);\n }\n\n getNewModel({\n clientOptions = {},\n omitOriginalOptions,\n } : {\n clientOptions?: t.ClientOptions;\n omitOriginalOptions?: string[]\n }): t.ChatModelInstance {\n const ChatModelClass = getChatModelClass(this.provider);\n const _options = omitOriginalOptions ? Object.fromEntries(\n Object.entries(this.clientOptions).filter(([key]) => !omitOriginalOptions.includes(key)),\n ) : this.clientOptions;\n const options = Object.assign(_options, clientOptions);\n return new ChatModelClass(options);\n }\n\n createCallModel() {\n return async (state: t.BaseGraphState, config?: RunnableConfig): Promise<Partial<t.BaseGraphState>> => {\n const { provider = '' } = (config?.configurable as t.GraphConfig | undefined) ?? {} ;\n if (!config || !provider) {\n throw new Error(`No ${config ? 'provider' : 'config'} provided`);\n }\n if (!config.signal) {\n config.signal = this.signal;\n }\n this.config = config;\n const { messages } = state;\n\n const finalMessages = messages;\n const lastMessageX = finalMessages[finalMessages.length - 2];\n const lastMessageY = finalMessages[finalMessages.length - 1];\n\n if (\n provider === Providers.BEDROCK\n && lastMessageX instanceof AIMessageChunk\n && lastMessageY instanceof ToolMessage\n && typeof lastMessageX.content === 'string'\n ) {\n finalMessages[finalMessages.length - 2].content = '';\n }\n\n if (this.lastStreamCall != null && this.streamBuffer != null) {\n const timeSinceLastCall = Date.now() - this.lastStreamCall;\n if (timeSinceLastCall < this.streamBuffer) {\n const timeToWait = Math.ceil((this.streamBuffer - timeSinceLastCall) / 1000) * 1000;\n await sleep(timeToWait);\n }\n }\n\n this.lastStreamCall = Date.now();\n\n if ((this.tools?.length ?? 0) > 0 && manualToolStreamProviders.has(provider)) {\n const stream = await this.boundModel.stream(finalMessages, config);\n let finalChunk: AIMessageChunk | undefined;\n for await (const chunk of stream) {\n dispatchCustomEvent(GraphEvents.CHAT_MODEL_STREAM, { chunk }, config);\n if (!finalChunk) {\n finalChunk = chunk;\n } else {\n finalChunk = concat(finalChunk, chunk);\n }\n }\n\n finalChunk = modifyDeltaProperties(finalChunk);\n return { messages: [finalChunk as AIMessageChunk] };\n }\n\n const finalMessage = await this.boundModel.invoke(finalMessages, config);\n return { messages: [finalMessage as AIMessageChunk] };\n };\n }\n\n createWorkflow(): t.CompiledWorkflow<t.BaseGraphState> {\n const routeMessage = (state: t.BaseGraphState, config?: RunnableConfig): string => {\n this.config = config;\n // const lastMessage = state.messages[state.messages.length - 1] as AIMessage;\n // if (!lastMessage?.tool_calls?.length) {\n // return END;\n // }\n // return TOOLS;\n return toolsCondition(state);\n };\n\n const workflow = new StateGraph<t.BaseGraphState>({\n channels: this.graphState,\n })\n .addNode(AGENT, this.createCallModel())\n .addNode(TOOLS, this.initializeTools())\n .addEdge(START, AGENT)\n .addConditionalEdges(AGENT, routeMessage)\n .addEdge(TOOLS, this.toolEnd ? END : AGENT);\n\n return workflow.compile();\n }\n\n /* Dispatchers */\n\n /**\n * Dispatches a run step to the client, returns the step ID\n */\n dispatchRunStep(stepKey: string, stepDetails: t.StepDetails): string {\n if (!this.config) {\n throw new Error('No config provided');\n }\n\n const [stepId, stepIndex] = this.generateStepId(stepKey);\n if (stepDetails.type === StepTypes.TOOL_CALLS && stepDetails.tool_calls) {\n for (const tool_call of stepDetails.tool_calls) {\n const toolCallId = tool_call.id ?? '';\n if (!toolCallId || this.toolCallStepIds.has(toolCallId)) {\n continue;\n }\n this.toolCallStepIds.set(toolCallId, stepId);\n }\n }\n\n const runStep: t.RunStep = {\n stepIndex,\n id: stepId,\n type: stepDetails.type,\n index: this.contentData.length,\n stepDetails,\n usage: null,\n };\n\n const runId = this.runId ?? '';\n if (runId) {\n runStep.runId = runId;\n }\n\n this.contentData.push(runStep);\n this.contentIndexMap.set(stepId, runStep.index);\n dispatchCustomEvent(GraphEvents.ON_RUN_STEP, runStep, this.config);\n return stepId;\n }\n\n handleToolCallCompleted(data: t.ToolEndData, metadata?: Record<string, unknown>): void {\n if (!this.config) {\n throw new Error('No config provided');\n }\n\n if (!data.output) {\n return;\n }\n\n const { input, output } = data;\n const { tool_call_id } = output;\n const stepId = this.toolCallStepIds.get(tool_call_id) ?? '';\n if (!stepId) {\n throw new Error(`No stepId found for tool_call_id ${tool_call_id}`);\n }\n\n const runStep = this.getRunStep(stepId);\n if (!runStep) {\n throw new Error(`No run step found for stepId ${stepId}`);\n }\n\n const args = typeof input === 'string' ? input : input.input;\n const tool_call = {\n args: typeof args === 'string' ? args : JSON.stringify(args),\n name: output.name ?? '',\n id: output.tool_call_id,\n output: typeof output.content === 'string'\n ? output.content\n : JSON.stringify(output.content),\n progress: 1,\n };\n\n this.handlerRegistry?.getHandler(GraphEvents.ON_RUN_STEP_COMPLETED)?.handle(\n GraphEvents.ON_RUN_STEP_COMPLETED,\n { result: {\n id: stepId,\n index: runStep.index,\n type: 'tool_call',\n tool_call\n } as t.ToolCompleteEvent,\n },\n metadata,\n this,\n );\n }\n\n dispatchRunStepDelta(id: string, delta: t.ToolCallDelta): void {\n if (!this.config) {\n throw new Error('No config provided');\n } else if (!id) {\n throw new Error('No step ID found');\n }\n const runStepDelta: t.RunStepDeltaEvent = {\n id,\n delta,\n };\n dispatchCustomEvent(GraphEvents.ON_RUN_STEP_DELTA, runStepDelta, this.config);\n }\n\n dispatchMessageDelta(id: string, delta: t.MessageDelta): void {\n if (!this.config) {\n throw new Error('No config provided');\n }\n const messageDelta: t.MessageDeltaEvent = {\n id,\n delta,\n };\n dispatchCustomEvent(GraphEvents.ON_MESSAGE_DELTA, messageDelta, this.config);\n }\n}\n"],"names":["GraphNodeKeys","SystemMessage","resetIfNotEmpty","joinKeys","nanoid","convertMessagesToContent","CustomToolNode","getChatModelClass","messages","Providers","AIMessageChunk","ToolMessage","sleep","manualToolStreamProviders","stream","dispatchCustomEvent","GraphEvents","concat","modifyDeltaProperties","toolsCondition","StateGraph","START","END","StepTypes"],"mappings":";;;;;;;;;;;;;;AAAA;AAiBA,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAGA,mBAAa,CAAC;MAcjB,KAAK,CAAA;AAwBzB,IAAA,mBAAmB,GAAwB,IAAI,GAAG,EAAE,CAAC;AACrD,IAAA,yBAAyB,GAAwB,IAAI,GAAG,EAAE,CAAC;AAC3D,IAAA,MAAM,CAA6B;IACnC,WAAW,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AAA,EAAE,EAAE,MAAM;gBACV,KAAK,EAAE,OAAO,CAAC,KAAK;AACpB,gBAAA,IAAI,EAAE,WAAW;gBACjB,SAAS;AACa,aAAA;AACvB,SAAA,EACD,QAAQ,EACR,IAAI,CACL,CAAC;KACH;IAED,oBAAoB,CAAC,EAAU,EAAE,KAAsB,EAAA;AACrD,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;AAChB,YAAA,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;SACvC;aAAM,IAAI,CAAC,EAAE,EAAE;AACd,YAAA,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;SACrC;AACD,QAAA,MAAM,YAAY,GAAwB;YACxC,EAAE;YACF,KAAK;SACN,CAAC;QACFD,4BAAmB,CAACC,iBAAW,CAAC,iBAAiB,EAAE,YAAY,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;KAC/E;IAED,oBAAoB,CAAC,EAAU,EAAE,KAAqB,EAAA;AACpD,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;AAChB,YAAA,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;SACvC;AACD,QAAA,MAAM,YAAY,GAAwB;YACxC,EAAE;YACF,KAAK;SACN,CAAC;QACFD,4BAAmB,CAACC,iBAAW,CAAC,gBAAgB,EAAE,YAAY,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;KAC9E;AACF;;;;;"}
|
|
@@ -21,13 +21,13 @@ function _formatImage(imageUrl) {
|
|
|
21
21
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
22
|
};
|
|
23
23
|
}
|
|
24
|
-
function
|
|
24
|
+
function _ensureMessageContents(messages$1) {
|
|
25
25
|
// Merge runs of human/tool messages into single human messages with content blocks.
|
|
26
|
-
const
|
|
26
|
+
const updatedMsgs = [];
|
|
27
27
|
for (const message of messages$1) {
|
|
28
28
|
if (message._getType() === 'tool') {
|
|
29
29
|
if (typeof message.content === 'string') {
|
|
30
|
-
const previousMessage =
|
|
30
|
+
const previousMessage = updatedMsgs[updatedMsgs.length - 1];
|
|
31
31
|
if (previousMessage &&
|
|
32
32
|
previousMessage._getType() === 'human' &&
|
|
33
33
|
Array.isArray(previousMessage.content) &&
|
|
@@ -42,7 +42,7 @@ function _mergeMessages(messages$1) {
|
|
|
42
42
|
}
|
|
43
43
|
else {
|
|
44
44
|
// If not, we create a new human message with the tool result.
|
|
45
|
-
|
|
45
|
+
updatedMsgs.push(new messages.HumanMessage({
|
|
46
46
|
content: [
|
|
47
47
|
{
|
|
48
48
|
type: 'tool_result',
|
|
@@ -54,7 +54,7 @@ function _mergeMessages(messages$1) {
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
else {
|
|
57
|
-
|
|
57
|
+
updatedMsgs.push(new messages.HumanMessage({
|
|
58
58
|
content: [
|
|
59
59
|
{
|
|
60
60
|
type: 'tool_result',
|
|
@@ -66,32 +66,10 @@ function _mergeMessages(messages$1) {
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
else {
|
|
69
|
-
|
|
70
|
-
if (previousMessage &&
|
|
71
|
-
previousMessage._getType() === 'human' &&
|
|
72
|
-
message._getType() === 'human') {
|
|
73
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
74
|
-
let combinedContent;
|
|
75
|
-
if (typeof previousMessage.content === 'string') {
|
|
76
|
-
combinedContent = [{ type: 'text', text: previousMessage.content }];
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
combinedContent = previousMessage.content;
|
|
80
|
-
}
|
|
81
|
-
if (typeof message.content === 'string') {
|
|
82
|
-
combinedContent.push({ type: 'text', text: message.content });
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
combinedContent = combinedContent.concat(message.content);
|
|
86
|
-
}
|
|
87
|
-
previousMessage.content = combinedContent;
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
merged.push(message);
|
|
91
|
-
}
|
|
69
|
+
updatedMsgs.push(message);
|
|
92
70
|
}
|
|
93
71
|
}
|
|
94
|
-
return
|
|
72
|
+
return updatedMsgs;
|
|
95
73
|
}
|
|
96
74
|
function _convertLangChainToolCallToAnthropic(toolCall) {
|
|
97
75
|
if (toolCall.id === undefined) {
|
|
@@ -178,7 +156,7 @@ function _formatContent(content) {
|
|
|
178
156
|
* @returns The formatted prompt.
|
|
179
157
|
*/
|
|
180
158
|
function _convertMessagesToAnthropicPayload(messages$1) {
|
|
181
|
-
const mergedMessages =
|
|
159
|
+
const mergedMessages = _ensureMessageContents(messages$1);
|
|
182
160
|
let system;
|
|
183
161
|
if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {
|
|
184
162
|
system = messages$1[0].content;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message_inputs.cjs","sources":["../../../../../src/llm/anthropic/utils/message_inputs.ts"],"sourcesContent":["/**\n * This util file contains functions for converting LangChain messages to Anthropic messages.\n */\nimport {\n BaseMessage,\n SystemMessage,\n HumanMessage,\n AIMessage,\n ToolMessage,\n MessageContent,\n isAIMessage,\n} from '@langchain/core/messages';\nimport { ToolCall } from '@langchain/core/messages/tool';\nimport type {\n AnthropicMessageCreateParams,\n AnthropicToolResponse,\n} from '@/llm/anthropic/types';\n\nfunction _formatImage(imageUrl: string): { type: string; media_type: string; data: string } {\n const regex = /^data:(image\\/.+);base64,(.+)$/;\n const match = imageUrl.match(regex);\n if (match === null) {\n throw new Error(\n [\n 'Anthropic only supports base64-encoded images currently.',\n 'Example: data:image/png;base64,/9j/4AAQSk...',\n ].join('\\n\\n')\n );\n }\n return {\n type: 'base64',\n media_type: match[1] ?? '',\n data: match[2] ?? '',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n}\n\nfunction _mergeMessages(\n messages: BaseMessage[]\n): (SystemMessage | HumanMessage | AIMessage)[] {\n // Merge runs of human/tool messages into single human messages with content blocks.\n const merged = [];\n for (const message of messages) {\n if (message._getType() === 'tool') {\n if (typeof message.content === 'string') {\n const previousMessage = merged[merged.length - 1] as BaseMessage | undefined;\n if (\n previousMessage &&\n previousMessage._getType() === 'human' &&\n Array.isArray(previousMessage.content) &&\n 'type' in previousMessage.content[0] &&\n previousMessage.content[0].type === 'tool_result'\n ) {\n // If the previous message was a tool result, we merge this tool message into it.\n previousMessage.content.push({\n type: 'tool_result',\n content: message.content,\n tool_use_id: (message as ToolMessage).tool_call_id,\n });\n } else {\n // If not, we create a new human message with the tool result.\n merged.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n content: message.content,\n tool_use_id: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n merged.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n content: _formatContent(message.content),\n tool_use_id: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n const previousMessage = merged[merged.length - 1] as BaseMessage | undefined;\n if (\n previousMessage &&\n previousMessage._getType() === 'human' &&\n message._getType() === 'human'\n ) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let combinedContent: Record<string, any>[];\n if (typeof previousMessage.content === 'string') {\n combinedContent = [{ type: 'text', text: previousMessage.content }];\n } else {\n combinedContent = previousMessage.content;\n }\n if (typeof message.content === 'string') {\n combinedContent.push({ type: 'text', text: message.content });\n } else {\n combinedContent = combinedContent.concat(message.content);\n }\n previousMessage.content = combinedContent;\n } else {\n merged.push(message);\n }\n }\n }\n return merged;\n}\n\nexport function _convertLangChainToolCallToAnthropic(\n toolCall: ToolCall\n): AnthropicToolResponse {\n if (toolCall.id === undefined) {\n throw new Error('Anthropic requires all tool calls to have an \"id\".');\n }\n return {\n type: 'tool_use',\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.args,\n };\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction _formatContent(content: MessageContent): string | Record<string, any>[] {\n const toolTypes = ['tool_use', 'tool_result', 'input_json_delta'];\n const textTypes = ['text', 'text_delta'];\n\n if (typeof content === 'string') {\n return content;\n } else {\n const contentBlocks = content.map((contentPart) => {\n const cacheControl =\n 'cache_control' in contentPart ? contentPart.cache_control : undefined;\n\n if (contentPart.type === 'image_url') {\n let source;\n if (typeof contentPart.image_url === 'string') {\n source = _formatImage(contentPart.image_url);\n } else {\n source = _formatImage(contentPart.image_url.url);\n }\n return {\n type: 'image' as const, // Explicitly setting the type as \"image\"\n source,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n } else if (\n textTypes.find((t) => t === contentPart.type) &&\n 'text' in contentPart\n ) {\n // Assuming contentPart is of type MessageContentText here\n return {\n type: 'text' as const, // Explicitly setting the type as \"text\"\n text: contentPart.text,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n } else if (toolTypes.find((t) => t === contentPart.type)) {\n const contentPartCopy = { ...contentPart };\n if ('index' in contentPartCopy) {\n // Anthropic does not support passing the index field here, so we remove it.\n delete contentPartCopy.index;\n }\n\n if (contentPartCopy.type === 'input_json_delta') {\n // `input_json_delta` type only represents yielding partial tool inputs\n // and is not a valid type for Anthropic messages.\n contentPartCopy.type = 'tool_use';\n }\n\n if ('input' in contentPartCopy) {\n // Anthropic tool use inputs should be valid objects, when applicable.\n try {\n contentPartCopy.input = JSON.parse(contentPartCopy.input);\n } catch {\n // no-op\n }\n }\n\n // TODO: Fix when SDK types are fixed\n return {\n ...contentPartCopy,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n } else {\n throw new Error('Unsupported message content format');\n }\n });\n return contentBlocks;\n }\n}\n\n/**\n * Formats messages as a prompt for the model.\n * Used in LangSmith, export is important here.\n * @param messages The base messages to format as a prompt.\n * @returns The formatted prompt.\n */\nexport function _convertMessagesToAnthropicPayload(\n messages: BaseMessage[]\n): AnthropicMessageCreateParams {\n const mergedMessages = _mergeMessages(messages);\n let system;\n if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {\n system = messages[0].content;\n }\n const conversationMessages =\n system !== undefined ? mergedMessages.slice(1) : mergedMessages;\n const formattedMessages = conversationMessages.map((message) => {\n let role;\n if (message._getType() === 'human') {\n role = 'user' as const;\n } else if (message._getType() === 'ai') {\n role = 'assistant' as const;\n } else if (message._getType() === 'tool') {\n role = 'user' as const;\n } else if (message._getType() === 'system') {\n throw new Error(\n 'System messages are only permitted as the first passed message.'\n );\n } else {\n throw new Error(`Message type \"${message._getType()}\" is not supported.`);\n }\n if (isAIMessage(message) && !!message.tool_calls?.length) {\n if (typeof message.content === 'string') {\n if (message.content === '') {\n return {\n role,\n content: message.tool_calls.map(\n _convertLangChainToolCallToAnthropic\n ),\n };\n } else {\n return {\n role,\n content: [\n { type: 'text', text: message.content },\n ...message.tool_calls.map(_convertLangChainToolCallToAnthropic),\n ],\n };\n }\n } else {\n const { content } = message;\n const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) =>\n content.find(\n (contentPart) =>\n (contentPart.type === 'tool_use' ||\n contentPart.type === 'input_json_delta') &&\n contentPart.id === toolCall.id\n )\n );\n if (hasMismatchedToolCalls) {\n console.warn(\n 'The \"tool_calls\" field on a message is only respected if content is a string.'\n );\n }\n return {\n role,\n content: _formatContent(message.content),\n };\n }\n } else {\n return {\n role,\n content: _formatContent(message.content),\n };\n }\n });\n return {\n messages: formattedMessages,\n system,\n } as AnthropicMessageCreateParams;\n}"],"names":["messages","HumanMessage","isAIMessage"],"mappings":";;;;AAAA;;AAEG;AAgBH,SAAS,YAAY,CAAC,QAAgB,EAAA;IACpC,MAAM,KAAK,GAAG,gCAAgC,CAAC;IAC/C,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACpC,IAAA,IAAI,KAAK,KAAK,IAAI,EAAE;QAClB,MAAM,IAAI,KAAK,CACb;YACE,0DAA0D;YAC1D,8CAA8C;AAC/C,SAAA,CAAC,IAAI,CAAC,MAAM,CAAC,CACf,CAAC;KACH;IACD,OAAO;AAC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AAI;wBACJ,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC,GAAG,CAC7B,oCAAoC,CACrC;qBACF,CAAC;iBACH;qBAAM;oBACL,OAAO;wBACL,IAAI;AACJ,wBAAA,OAAO,EAAE;4BACP,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,EAAE;AACvC,4BAAA,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,oCAAoC,CAAC;AAChE,yBAAA;qBACF,CAAC;iBACH;aACF;iBAAM;AACL,gBAAA,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;AAC5B,gBAAA,MAAM,sBAAsB,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,QAAQ,KAChE,OAAO,CAAC,IAAI,CACV,CAAC,WAAW,KACV,CAAC,WAAW,CAAC,IAAI,KAAK,UAAU;AAC9B,oBAAA,WAAW,CAAC,IAAI,KAAK,kBAAkB;oBACzC,WAAW,CAAC,EAAE,KAAK,QAAQ,CAAC,EAAE,CACjC,CACF,CAAC;gBACF,IAAI,sBAAsB,EAAE;AAC1B,oBAAA,OAAO,CAAC,IAAI,CACV,+EAA+E,CAChF,CAAC;iBACH;gBACD,OAAO;oBACL,IAAI;AACJ,oBAAA,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC,OAAO,CAAC;iBACzC,CAAC;aACH;SACF;aAAM;YACL,OAAO;gBACL,IAAI;AACJ,gBAAA,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC,OAAO,CAAC;aACzC,CAAC;SACH;AACH,KAAC,CAAC,CAAC;IACH,OAAO;AACL,QAAA,QAAQ,EAAE,iBAAiB;QAC3B,MAAM;KACyB,CAAC;AACpC;;;;;"}
|
|
1
|
+
{"version":3,"file":"message_inputs.cjs","sources":["../../../../../src/llm/anthropic/utils/message_inputs.ts"],"sourcesContent":["/**\n * This util file contains functions for converting LangChain messages to Anthropic messages.\n */\nimport {\n BaseMessage,\n SystemMessage,\n HumanMessage,\n AIMessage,\n ToolMessage,\n MessageContent,\n isAIMessage,\n} from '@langchain/core/messages';\nimport { ToolCall } from '@langchain/core/messages/tool';\nimport type {\n AnthropicMessageCreateParams,\n AnthropicToolResponse,\n} from '@/llm/anthropic/types';\n\nfunction _formatImage(imageUrl: string): { type: string; media_type: string; data: string } {\n const regex = /^data:(image\\/.+);base64,(.+)$/;\n const match = imageUrl.match(regex);\n if (match === null) {\n throw new Error(\n [\n 'Anthropic only supports base64-encoded images currently.',\n 'Example: data:image/png;base64,/9j/4AAQSk...',\n ].join('\\n\\n')\n );\n }\n return {\n type: 'base64',\n media_type: match[1] ?? '',\n data: match[2] ?? '',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n}\n\nfunction _ensureMessageContents(\n messages: BaseMessage[]\n): (SystemMessage | HumanMessage | AIMessage)[] {\n // Merge runs of human/tool messages into single human messages with content blocks.\n const updatedMsgs = [];\n for (const message of messages) {\n if (message._getType() === 'tool') {\n if (typeof message.content === 'string') {\n const previousMessage = updatedMsgs[updatedMsgs.length - 1] as BaseMessage | undefined;\n if (\n previousMessage &&\n previousMessage._getType() === 'human' &&\n Array.isArray(previousMessage.content) &&\n 'type' in previousMessage.content[0] &&\n previousMessage.content[0].type === 'tool_result'\n ) {\n // If the previous message was a tool result, we merge this tool message into it.\n previousMessage.content.push({\n type: 'tool_result',\n content: message.content,\n tool_use_id: (message as ToolMessage).tool_call_id,\n });\n } else {\n // If not, we create a new human message with the tool result.\n updatedMsgs.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n content: message.content,\n tool_use_id: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n updatedMsgs.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n content: _formatContent(message.content),\n tool_use_id: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n updatedMsgs.push(message);\n }\n }\n return updatedMsgs;\n}\n\nexport function _convertLangChainToolCallToAnthropic(\n toolCall: ToolCall\n): AnthropicToolResponse {\n if (toolCall.id === undefined) {\n throw new Error('Anthropic requires all tool calls to have an \"id\".');\n }\n return {\n type: 'tool_use',\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.args,\n };\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction _formatContent(content: MessageContent): string | Record<string, any>[] {\n const toolTypes = ['tool_use', 'tool_result', 'input_json_delta'];\n const textTypes = ['text', 'text_delta'];\n\n if (typeof content === 'string') {\n return content;\n } else {\n const contentBlocks = content.map((contentPart) => {\n const cacheControl =\n 'cache_control' in contentPart ? contentPart.cache_control : undefined;\n\n if (contentPart.type === 'image_url') {\n let source;\n if (typeof contentPart.image_url === 'string') {\n source = _formatImage(contentPart.image_url);\n } else {\n source = _formatImage(contentPart.image_url.url);\n }\n return {\n type: 'image' as const, // Explicitly setting the type as \"image\"\n source,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n } else if (\n textTypes.find((t) => t === contentPart.type) &&\n 'text' in contentPart\n ) {\n // Assuming contentPart is of type MessageContentText here\n return {\n type: 'text' as const, // Explicitly setting the type as \"text\"\n text: contentPart.text,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n } else if (toolTypes.find((t) => t === contentPart.type)) {\n const contentPartCopy = { ...contentPart };\n if ('index' in contentPartCopy) {\n // Anthropic does not support passing the index field here, so we remove it.\n delete contentPartCopy.index;\n }\n\n if (contentPartCopy.type === 'input_json_delta') {\n // `input_json_delta` type only represents yielding partial tool inputs\n // and is not a valid type for Anthropic messages.\n contentPartCopy.type = 'tool_use';\n }\n\n if ('input' in contentPartCopy) {\n // Anthropic tool use inputs should be valid objects, when applicable.\n try {\n contentPartCopy.input = JSON.parse(contentPartCopy.input);\n } catch {\n // no-op\n }\n }\n\n // TODO: Fix when SDK types are fixed\n return {\n ...contentPartCopy,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n } else {\n throw new Error('Unsupported message content format');\n }\n });\n return contentBlocks;\n }\n}\n\n/**\n * Formats messages as a prompt for the model.\n * Used in LangSmith, export is important here.\n * @param messages The base messages to format as a prompt.\n * @returns The formatted prompt.\n */\nexport function _convertMessagesToAnthropicPayload(\n messages: BaseMessage[]\n): AnthropicMessageCreateParams {\n const mergedMessages = _ensureMessageContents(messages);\n let system;\n if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {\n system = messages[0].content;\n }\n const conversationMessages =\n system !== undefined ? mergedMessages.slice(1) : mergedMessages;\n const formattedMessages = conversationMessages.map((message) => {\n let role;\n if (message._getType() === 'human') {\n role = 'user' as const;\n } else if (message._getType() === 'ai') {\n role = 'assistant' as const;\n } else if (message._getType() === 'tool') {\n role = 'user' as const;\n } else if (message._getType() === 'system') {\n throw new Error(\n 'System messages are only permitted as the first passed message.'\n );\n } else {\n throw new Error(`Message type \"${message._getType()}\" is not supported.`);\n }\n if (isAIMessage(message) && !!message.tool_calls?.length) {\n if (typeof message.content === 'string') {\n if (message.content === '') {\n return {\n role,\n content: message.tool_calls.map(\n _convertLangChainToolCallToAnthropic\n ),\n };\n } else {\n return {\n role,\n content: [\n { type: 'text', text: message.content },\n ...message.tool_calls.map(_convertLangChainToolCallToAnthropic),\n ],\n };\n }\n } else {\n const { content } = message;\n const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) =>\n content.find(\n (contentPart) =>\n (contentPart.type === 'tool_use' ||\n contentPart.type === 'input_json_delta') &&\n contentPart.id === toolCall.id\n )\n );\n if (hasMismatchedToolCalls) {\n console.warn(\n 'The \"tool_calls\" field on a message is only respected if content is a string.'\n );\n }\n return {\n role,\n content: _formatContent(message.content),\n };\n }\n } else {\n return {\n role,\n content: _formatContent(message.content),\n };\n }\n });\n return {\n messages: formattedMessages,\n system,\n } as AnthropicMessageCreateParams;\n}"],"names":["messages","HumanMessage","isAIMessage"],"mappings":";;;;AAAA;;AAEG;AAgBH,SAAS,YAAY,C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AAC,WAAW,CAAC,IAAI,KAAK,UAAU;AAC9B,oBAAA,WAAW,CAAC,IAAI,KAAK,kBAAkB;oBACzC,WAAW,CAAC,EAAE,KAAK,QAAQ,CAAC,EAAE,CACjC,CACF,CAAC;gBACF,IAAI,sBAAsB,EAAE;AAC1B,oBAAA,OAAO,CAAC,IAAI,CACV,+EAA+E,CAChF,CAAC;iBACH;gBACD,OAAO;oBACL,IAAI;AACJ,oBAAA,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC,OAAO,CAAC;iBACzC,CAAC;aACH;SACF;aAAM;YACL,OAAO;gBACL,IAAI;AACJ,gBAAA,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC,OAAO,CAAC;aACzC,CAAC;SACH;AACH,KAAC,CAAC,CAAC;IACH,OAAO;AACL,QAAA,QAAQ,EAAE,iBAAiB;QAC3B,MAAM;KACyB,CAAC;AACpC;;;;;"}
|
package/dist/cjs/run.cjs
CHANGED
|
@@ -68,20 +68,20 @@ class Run {
|
|
|
68
68
|
}
|
|
69
69
|
return this.Graph.getRunMessages();
|
|
70
70
|
}
|
|
71
|
-
async processStream(inputs, config,
|
|
71
|
+
async processStream(inputs, config, streamOptions) {
|
|
72
72
|
if (!this.graphRunnable) {
|
|
73
73
|
throw new Error('Run not initialized. Make sure to use Run.create() to instantiate the Run.');
|
|
74
74
|
}
|
|
75
75
|
if (!this.Graph) {
|
|
76
76
|
throw new Error('Graph not initialized. Make sure to use Run.create() to instantiate the Run.');
|
|
77
77
|
}
|
|
78
|
-
this.Graph.resetValues();
|
|
78
|
+
this.Graph.resetValues(streamOptions?.keepContent);
|
|
79
79
|
const provider = this.Graph.provider;
|
|
80
80
|
const hasTools = this.Graph.tools ? this.Graph.tools.length > 0 : false;
|
|
81
|
-
if (
|
|
81
|
+
if (streamOptions?.callbacks) {
|
|
82
82
|
/* TODO: conflicts with callback manager */
|
|
83
83
|
const callbacks = config.callbacks ?? [];
|
|
84
|
-
config.callbacks = callbacks.concat(this.getCallbacks(
|
|
84
|
+
config.callbacks = callbacks.concat(this.getCallbacks(streamOptions.callbacks));
|
|
85
85
|
}
|
|
86
86
|
if (!this.id) {
|
|
87
87
|
throw new Error('Run ID not provided');
|
|
@@ -99,7 +99,6 @@ class Run {
|
|
|
99
99
|
if (eventName && eventName === _enum.GraphEvents.ON_CUSTOM_EVENT) {
|
|
100
100
|
eventName = name;
|
|
101
101
|
}
|
|
102
|
-
// console.log(`Event: ${event.event} | Executing Event: ${eventName}`);
|
|
103
102
|
const handler = this.handlerRegistry.getHandler(eventName);
|
|
104
103
|
if (handler) {
|
|
105
104
|
handler.handle(eventName, data, metadata, this.Graph);
|
package/dist/cjs/run.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.cjs","sources":["../../src/run.ts"],"sourcesContent":["// src/run.ts\nimport { PromptTemplate } from '@langchain/core/prompts';\n\nimport type { BaseMessage, MessageContentComplex } from '@langchain/core/messages';\nimport type { RunnableConfig } from '@langchain/core/runnables';\nimport type { ClientCallbacks, SystemCallbacks } from '@/graphs/Graph';\nimport type * as t from '@/types';\nimport { GraphEvents, Providers, Callback } from '@/common';\nimport { manualToolStreamProviders } from '@/llm/providers';\nimport { createTitleRunnable } from '@/utils/title';\nimport { StandardGraph } from '@/graphs/Graph';\nimport { HandlerRegistry } from '@/events';\n\nexport class Run<T extends t.BaseGraphState> {\n graphRunnable?: t.CompiledWorkflow<T, Partial<T>, string>;\n // private collab!: CollabGraph;\n // private taskManager!: TaskManager;\n private handlerRegistry: HandlerRegistry;\n id: string;\n Graph: StandardGraph | undefined;\n provider: Providers | undefined;\n returnContent: boolean = false;\n\n private constructor(config: Partial<t.RunConfig>) {\n const runId = config.runId ?? '';\n if (!runId) {\n throw new Error('Run ID not provided');\n }\n\n this.id = runId;\n\n const handlerRegistry = new HandlerRegistry();\n\n if (config.customHandlers) {\n for (const [eventType, handler] of Object.entries(config.customHandlers)) {\n handlerRegistry.register(eventType, handler);\n }\n }\n\n this.handlerRegistry = handlerRegistry;\n\n if (!config.graphConfig) {\n throw new Error('Graph config not provided');\n }\n\n if (config.graphConfig.type === 'standard' || !config.graphConfig.type) {\n this.provider = config.graphConfig.llmConfig.provider;\n this.graphRunnable = this.createStandardGraph(config.graphConfig) as unknown as t.CompiledWorkflow<T, Partial<T>, string>;\n if (this.Graph) {\n this.Graph.handlerRegistry = handlerRegistry;\n }\n }\n\n this.returnContent = config.returnContent ?? false;\n }\n\n private createStandardGraph(config: t.StandardGraphConfig): t.CompiledWorkflow<t.IState, Partial<t.IState>, string> {\n const { llmConfig, instructions, additional_instructions, signal, streamBuffer, toolEnd, tools = [] } = config;\n const { provider, ...clientOptions } = llmConfig;\n\n const standardGraph = new StandardGraph({\n runId: this.id,\n tools,\n provider,\n signal,\n instructions,\n clientOptions,\n additional_instructions,\n streamBuffer,\n toolEnd,\n });\n this.Graph = standardGraph;\n return standardGraph.createWorkflow();\n }\n\n static async create<T extends t.BaseGraphState>(config: t.RunConfig): Promise<Run<T>> {\n return new Run<T>(config);\n }\n\n getRunMessages(): BaseMessage[] | undefined {\n if (!this.Graph) {\n throw new Error('Graph not initialized. Make sure to use Run.create() to instantiate the Run.');\n }\n return this.Graph.getRunMessages();\n }\n\n async processStream(\n inputs: t.IState,\n config: Partial<RunnableConfig> & { version: 'v1' | 'v2'; run_id?: string },\n clientCallbacks?: ClientCallbacks,\n ): Promise<MessageContentComplex[] | undefined> {\n if (!this.graphRunnable) {\n throw new Error('Run not initialized. Make sure to use Run.create() to instantiate the Run.');\n }\n if (!this.Graph) {\n throw new Error('Graph not initialized. Make sure to use Run.create() to instantiate the Run.');\n }\n\n this.Graph.resetValues();\n const provider = this.Graph.provider;\n const hasTools = this.Graph.tools ? this.Graph.tools.length > 0 : false;\n if (clientCallbacks) {\n /* TODO: conflicts with callback manager */\n const callbacks = config.callbacks as t.ProvidedCallbacks ?? [];\n config.callbacks = callbacks.concat(this.getCallbacks(clientCallbacks));\n }\n\n if (!this.id) {\n throw new Error('Run ID not provided');\n }\n\n config.run_id = this.id;\n config.configurable = Object.assign(config.configurable ?? {}, { run_id: this.id, provider: this.provider });\n\n const stream = this.graphRunnable.streamEvents(inputs, config);\n\n for await (const event of stream) {\n const { data, name, metadata, ...info } = event;\n\n let eventName: t.EventName = info.event;\n if (hasTools && manualToolStreamProviders.has(provider) && eventName === GraphEvents.CHAT_MODEL_STREAM) {\n /* Skipping CHAT_MODEL_STREAM event due to double-call edge case */\n continue;\n }\n\n if (eventName && eventName === GraphEvents.ON_CUSTOM_EVENT) {\n eventName = name;\n }\n\n // console.log(`Event: ${event.event} | Executing Event: ${eventName}`);\n\n const handler = this.handlerRegistry.getHandler(eventName);\n if (handler) {\n handler.handle(eventName, data, metadata, this.Graph);\n }\n }\n\n if (this.returnContent) {\n return this.Graph.getContentParts();\n }\n }\n\n private createSystemCallback<K extends keyof ClientCallbacks>(\n clientCallbacks: ClientCallbacks,\n key: K\n ): SystemCallbacks[K] {\n return ((...args: unknown[]) => {\n const clientCallback = clientCallbacks[key];\n if (clientCallback && this.Graph) {\n (clientCallback as (...args: unknown[]) => void)(this.Graph, ...args);\n }\n }) as SystemCallbacks[K];\n }\n\n getCallbacks(clientCallbacks: ClientCallbacks): SystemCallbacks {\n return {\n [Callback.TOOL_ERROR]: this.createSystemCallback(clientCallbacks, Callback.TOOL_ERROR),\n [Callback.TOOL_START]: this.createSystemCallback(clientCallbacks, Callback.TOOL_START),\n [Callback.TOOL_END]: this.createSystemCallback(clientCallbacks, Callback.TOOL_END),\n };\n }\n\n async generateTitle({\n inputText,\n contentParts,\n titlePrompt,\n clientOptions,\n chainOptions,\n skipLanguage,\n } : {\n inputText: string;\n contentParts: (t.MessageContentComplex | undefined)[];\n titlePrompt?: string;\n skipLanguage?: boolean;\n clientOptions?: t.ClientOptions;\n chainOptions?: Partial<RunnableConfig> | undefined;\n }): Promise<{ language: string; title: string }> {\n const convoTemplate = PromptTemplate.fromTemplate('User: {input}\\nAI: {output}');\n const response = contentParts.map((part) => {\n if (part?.type === 'text') return part.text;\n return '';\n }).join('\\n');\n const convo = (await convoTemplate.invoke({ input: inputText, output: response })).value;\n const model = this.Graph?.getNewModel({\n clientOptions,\n omitOriginalOptions: ['streaming'],\n });\n if (!model) {\n return { language: '', title: '' };\n }\n const chain = await createTitleRunnable(model, titlePrompt);\n return await chain.invoke({ convo, inputText, skipLanguage }, chainOptions) as { language: string; title: string };\n }\n}\n"],"names":["HandlerRegistry","StandardGraph","manualToolStreamProviders","GraphEvents","Callback","PromptTemplate","createTitleRunnable"],"mappings":";;;;;;;;;AAAA;MAaa,GAAG,CAAA;AACd,IAAA,aAAa,CAA6C;;;AAGlD,IAAA,eAAe,CAAkB;AACzC,IAAA,EAAE,CAAS;AACX,IAAA,KAAK,CAA4B;AACjC,IAAA,QAAQ,CAAwB;IAChC,aAAa,GAAY,KAAK,CAAC;AAE/B,IAAA,WAAA,CAAoB,MAA4B,EAAA;AAC9C,QAAA,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,EAAE,CAAC;QACjC,IAAI,CAAC,KAAK,EAAE;AACV,YAAA,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;SACxC;AAED,QAAA,IAAI,CAAC,EAAE,GAAG,KAAK,CAAC;AAEhB,QAAA,MAAM,eAAe,GAAG,IAAIA,sBAAe,EAAE,CAAC;AAE9C,QAAA,IAAI,MAAM,CAAC,cAAc,EAAE;AACzB,YAAA,KAAK,MAAM,CAAC,SAAS,EAAE,OAAO,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE;AACxE,gBAAA,eAAe,CAAC,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;aAC9C;SACF;AAED,QAAA,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;AAEvC,QAAA,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE;AACvB,YAAA,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;SA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AAK,EAAE,SAAS,EAAE,YAAY,EAAE,EAAE,YAAY,CAAwC,CAAC;KACpH;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"run.cjs","sources":["../../src/run.ts"],"sourcesContent":["// src/run.ts\nimport { PromptTemplate } from '@langchain/core/prompts';\n\nimport type { BaseMessage, MessageContentComplex } from '@langchain/core/messages';\nimport type { RunnableConfig } from '@langchain/core/runnables';\nimport type { ClientCallbacks, SystemCallbacks } from '@/graphs/Graph';\nimport type * as t from '@/types';\nimport { GraphEvents, Providers, Callback } from '@/common';\nimport { manualToolStreamProviders } from '@/llm/providers';\nimport { createTitleRunnable } from '@/utils/title';\nimport { StandardGraph } from '@/graphs/Graph';\nimport { HandlerRegistry } from '@/events';\n\nexport class Run<T extends t.BaseGraphState> {\n graphRunnable?: t.CompiledWorkflow<T, Partial<T>, string>;\n // private collab!: CollabGraph;\n // private taskManager!: TaskManager;\n private handlerRegistry: HandlerRegistry;\n id: string;\n Graph: StandardGraph | undefined;\n provider: Providers | undefined;\n returnContent: boolean = false;\n\n private constructor(config: Partial<t.RunConfig>) {\n const runId = config.runId ?? '';\n if (!runId) {\n throw new Error('Run ID not provided');\n }\n\n this.id = runId;\n\n const handlerRegistry = new HandlerRegistry();\n\n if (config.customHandlers) {\n for (const [eventType, handler] of Object.entries(config.customHandlers)) {\n handlerRegistry.register(eventType, handler);\n }\n }\n\n this.handlerRegistry = handlerRegistry;\n\n if (!config.graphConfig) {\n throw new Error('Graph config not provided');\n }\n\n if (config.graphConfig.type === 'standard' || !config.graphConfig.type) {\n this.provider = config.graphConfig.llmConfig.provider;\n this.graphRunnable = this.createStandardGraph(config.graphConfig) as unknown as t.CompiledWorkflow<T, Partial<T>, string>;\n if (this.Graph) {\n this.Graph.handlerRegistry = handlerRegistry;\n }\n }\n\n this.returnContent = config.returnContent ?? false;\n }\n\n private createStandardGraph(config: t.StandardGraphConfig): t.CompiledWorkflow<t.IState, Partial<t.IState>, string> {\n const { llmConfig, instructions, additional_instructions, signal, streamBuffer, toolEnd, tools = [] } = config;\n const { provider, ...clientOptions } = llmConfig;\n\n const standardGraph = new StandardGraph({\n runId: this.id,\n tools,\n provider,\n signal,\n instructions,\n clientOptions,\n additional_instructions,\n streamBuffer,\n toolEnd,\n });\n this.Graph = standardGraph;\n return standardGraph.createWorkflow();\n }\n\n static async create<T extends t.BaseGraphState>(config: t.RunConfig): Promise<Run<T>> {\n return new Run<T>(config);\n }\n\n getRunMessages(): BaseMessage[] | undefined {\n if (!this.Graph) {\n throw new Error('Graph not initialized. Make sure to use Run.create() to instantiate the Run.');\n }\n return this.Graph.getRunMessages();\n }\n\n async processStream(\n inputs: t.IState,\n config: Partial<RunnableConfig> & { version: 'v1' | 'v2'; run_id?: string },\n streamOptions?: t.EventStreamOptions,\n ): Promise<MessageContentComplex[] | undefined> {\n if (!this.graphRunnable) {\n throw new Error('Run not initialized. Make sure to use Run.create() to instantiate the Run.');\n }\n if (!this.Graph) {\n throw new Error('Graph not initialized. Make sure to use Run.create() to instantiate the Run.');\n }\n\n this.Graph.resetValues(streamOptions?.keepContent);\n const provider = this.Graph.provider;\n const hasTools = this.Graph.tools ? this.Graph.tools.length > 0 : false;\n if (streamOptions?.callbacks) {\n /* TODO: conflicts with callback manager */\n const callbacks = config.callbacks as t.ProvidedCallbacks ?? [];\n config.callbacks = callbacks.concat(this.getCallbacks(streamOptions.callbacks));\n }\n\n if (!this.id) {\n throw new Error('Run ID not provided');\n }\n\n config.run_id = this.id;\n config.configurable = Object.assign(config.configurable ?? {}, { run_id: this.id, provider: this.provider });\n\n const stream = this.graphRunnable.streamEvents(inputs, config);\n\n for await (const event of stream) {\n const { data, name, metadata, ...info } = event;\n\n let eventName: t.EventName = info.event;\n if (hasTools && manualToolStreamProviders.has(provider) && eventName === GraphEvents.CHAT_MODEL_STREAM) {\n /* Skipping CHAT_MODEL_STREAM event due to double-call edge case */\n continue;\n }\n\n if (eventName && eventName === GraphEvents.ON_CUSTOM_EVENT) {\n eventName = name;\n }\n\n const handler = this.handlerRegistry.getHandler(eventName);\n if (handler) {\n handler.handle(eventName, data, metadata, this.Graph);\n }\n }\n\n if (this.returnContent) {\n return this.Graph.getContentParts();\n }\n }\n\n private createSystemCallback<K extends keyof ClientCallbacks>(\n clientCallbacks: ClientCallbacks,\n key: K\n ): SystemCallbacks[K] {\n return ((...args: unknown[]) => {\n const clientCallback = clientCallbacks[key];\n if (clientCallback && this.Graph) {\n (clientCallback as (...args: unknown[]) => void)(this.Graph, ...args);\n }\n }) as SystemCallbacks[K];\n }\n\n getCallbacks(clientCallbacks: ClientCallbacks): SystemCallbacks {\n return {\n [Callback.TOOL_ERROR]: this.createSystemCallback(clientCallbacks, Callback.TOOL_ERROR),\n [Callback.TOOL_START]: this.createSystemCallback(clientCallbacks, Callback.TOOL_START),\n [Callback.TOOL_END]: this.createSystemCallback(clientCallbacks, Callback.TOOL_END),\n };\n }\n\n async generateTitle({\n inputText,\n contentParts,\n titlePrompt,\n clientOptions,\n chainOptions,\n skipLanguage,\n } : {\n inputText: string;\n contentParts: (t.MessageContentComplex | undefined)[];\n titlePrompt?: string;\n skipLanguage?: boolean;\n clientOptions?: t.ClientOptions;\n chainOptions?: Partial<RunnableConfig> | undefined;\n }): Promise<{ language: string; title: string }> {\n const convoTemplate = PromptTemplate.fromTemplate('User: {input}\\nAI: {output}');\n const response = contentParts.map((part) => {\n if (part?.type === 'text') return part.text;\n return '';\n }).join('\\n');\n const convo = (await convoTemplate.invoke({ input: inputText, output: response })).value;\n const model = this.Graph?.getNewModel({\n clientOptions,\n omitOriginalOptions: ['streaming'],\n });\n if (!model) {\n return { language: '', title: '' };\n }\n const chain = await createTitleRunnable(model, titlePrompt);\n return await chain.invoke({ convo, inputText, skipLanguage }, chainOptions) as { language: string; title: string };\n }\n}\n"],"names":["HandlerRegistry","StandardGraph","manualToolStreamProviders","GraphEvents","Callback","PromptTemplate","createTitleRunnable"],"mappings":";;;;;;;;;AAAA;MAaa,GAAG,CAAA;AACd,IAAA,aA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YAAY,EACZ,WAAW,EACX,aAAa,EACb,YAAY,EACZ,YAAY,GAQb,EAAA;QACC,MAAM,aAAa,GAAGC,sBAAc,CAAC,YAAY,CAAC,6BAA6B,CAAC,CAAC;QACjF,MAAM,QAAQ,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC,IAAI,KAAI;AACzC,YAAA,IAAI,IAAI,EAAE,IAAI,KAAK,MAAM;gBAAE,OAAO,IAAI,CAAC,IAAI,CAAC;AAC5C,YAAA,OAAO,EAAE,CAAC;AACZ,SAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACd,MAAM,KAAK,GAAG,CAAC,MAAM,aAAa,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,EAAE,KAAK,CAAC;AACzF,QAAA,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,EAAE,WAAW,CAAC;YACpC,aAAa;YACb,mBAAmB,EAAE,CAAC,WAAW,CAAC;AACnC,SAAA,CAAC,CAAC;QACH,IAAI,CAAC,KAAK,EAAE;YACV,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;SACpC;QACD,MAAM,KAAK,GAAG,MAAMC,yBAAmB,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;AAC5D,QAAA,OAAO,MAAM,KAAK,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,EAAE,YAAY,CAAwC,CAAC;KACpH;AACF;;;;"}
|
|
@@ -61,13 +61,15 @@ class StandardGraph extends Graph {
|
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
/* Init */
|
|
64
|
-
resetValues() {
|
|
64
|
+
resetValues(keepContent) {
|
|
65
65
|
this.messages = [];
|
|
66
66
|
this.config = resetIfNotEmpty(this.config, undefined);
|
|
67
|
-
|
|
67
|
+
if (keepContent !== true) {
|
|
68
|
+
this.contentData = resetIfNotEmpty(this.contentData, []);
|
|
69
|
+
this.contentIndexMap = resetIfNotEmpty(this.contentIndexMap, new Map());
|
|
70
|
+
}
|
|
68
71
|
this.stepKeyIds = resetIfNotEmpty(this.stepKeyIds, new Map());
|
|
69
72
|
this.toolCallStepIds = resetIfNotEmpty(this.toolCallStepIds, new Map());
|
|
70
|
-
this.contentIndexMap = resetIfNotEmpty(this.contentIndexMap, new Map());
|
|
71
73
|
this.messageIdsByStepKey = resetIfNotEmpty(this.messageIdsByStepKey, new Map());
|
|
72
74
|
this.prelimMessageIdsByStepKey = resetIfNotEmpty(this.prelimMessageIdsByStepKey, new Map());
|
|
73
75
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Graph.mjs","sources":["../../../src/graphs/Graph.ts"],"sourcesContent":["// src/graphs/Graph.ts\nimport { nanoid } from 'nanoid';\nimport { concat } from '@langchain/core/utils/stream';\nimport { ToolNode } from '@langchain/langgraph/prebuilt';\nimport { START, END, StateGraph } from '@langchain/langgraph';\nimport { Runnable, RunnableConfig } from '@langchain/core/runnables';\nimport { dispatchCustomEvent } from '@langchain/core/callbacks/dispatch';\nimport { AIMessageChunk, ToolMessage, SystemMessage } from '@langchain/core/messages';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport type * as t from '@/types';\nimport { Providers, GraphEvents, GraphNodeKeys, StepTypes, Callback } from '@/common';\nimport { getChatModelClass, manualToolStreamProviders } from '@/llm/providers';\nimport { ToolNode as CustomToolNode, toolsCondition } from '@/tools/ToolNode';\nimport { modifyDeltaProperties, convertMessagesToContent } from '@/messages';\nimport { resetIfNotEmpty, joinKeys, sleep } from '@/utils';\nimport { HandlerRegistry } from '@/events';\n\nconst { AGENT, TOOLS } = GraphNodeKeys;\nexport type GraphNode = GraphNodeKeys | typeof START;\nexport type ClientCallback<T extends unknown[]> = (graph: StandardGraph, ...args: T) => void;\nexport type ClientCallbacks = {\n [Callback.TOOL_ERROR]?: ClientCallback<[Error, string]>;\n [Callback.TOOL_START]?: ClientCallback<unknown[]>;\n [Callback.TOOL_END]?: ClientCallback<unknown[]>;\n}\nexport type SystemCallbacks = {\n [K in keyof ClientCallbacks]: ClientCallbacks[K] extends ClientCallback<infer Args>\n ? (...args: Args) => void\n : never;\n};\n\nexport abstract class Graph<\n T extends t.BaseGraphState = t.BaseGraphState,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n TNodeName extends string = string,\n> {\n abstract resetValues(): void;\n abstract createGraphState(): t.GraphStateChannels<T>;\n abstract initializeTools(): CustomToolNode<T> | ToolNode<T>;\n abstract initializeModel(): Runnable;\n abstract getRunMessages(): BaseMessage[] | undefined;\n abstract getContentParts(): t.MessageContentComplex[] | undefined;\n abstract generateStepId(stepKey: string): [string, number];\n abstract getKeyList(metadata: Record<string, unknown> | undefined): (string | number | undefined)[];\n abstract getStepKey(metadata: Record<string, unknown> | undefined): string;\n abstract checkKeyList(keyList: (string | number | undefined)[]): boolean;\n abstract getStepIdByKey(stepKey: string, index?: number): string\n abstract getRunStep(stepId: string): t.RunStep | undefined;\n abstract dispatchRunStep(stepKey: string, stepDetails: t.StepDetails): string;\n abstract dispatchRunStepDelta(id: string, delta: t.ToolCallDelta): void;\n abstract dispatchMessageDelta(id: string, delta: t.MessageDelta): void;\n abstract handleToolCallCompleted(data: t.ToolEndData, metadata?: Record<string, unknown>): void;\n\n abstract createCallModel(): (state: T, config?: RunnableConfig) => Promise<Partial<T>>;\n abstract createWorkflow(): t.CompiledWorkflow<T>;\n messageIdsByStepKey: Map<string, string> = new Map();\n prelimMessageIdsByStepKey: Map<string, string> = new Map();\n config: RunnableConfig | undefined;\n contentData: t.RunStep[] = [];\n stepKeyIds: Map<string, string[]> = new Map<string, string[]>();\n contentIndexMap: Map<string, number> = new Map();\n toolCallStepIds: Map<string, string> = new Map();\n /** The amount of time that should pass before another consecutive API call */\n streamBuffer: number | undefined;\n signal?: AbortSignal;\n}\n\nexport class StandardGraph extends Graph<\n t.BaseGraphState,\n GraphNode\n> {\n private graphState: t.GraphStateChannels<t.BaseGraphState>;\n private clientOptions: Record<string, unknown>;\n boundModel: Runnable;\n /** The last recorded timestamp that a stream API call was invoked */\n lastStreamCall: number | undefined;\n handlerRegistry: HandlerRegistry | undefined;\n systemMessage: SystemMessage | undefined;\n messages: BaseMessage[] = [];\n runId: string | undefined;\n tools?: t.GenericTool[];\n toolMap?: t.ToolMap;\n startIndex: number = 0;\n provider: Providers;\n toolEnd: boolean;\n signal: AbortSignal | undefined;\n\n constructor({\n runId,\n tools,\n toolMap,\n provider,\n clientOptions,\n instructions,\n signal,\n additional_instructions = '',\n streamBuffer,\n toolEnd = false,\n } : {\n runId?: string;\n signal?: AbortSignal;\n provider: Providers;\n tools?: t.GenericTool[];\n toolMap?: t.ToolMap;\n clientOptions: Record<string, unknown>;\n instructions?: string;\n additional_instructions?: string;\n streamBuffer?: number;\n toolEnd?: boolean;\n }) {\n super();\n this.runId = runId;\n this.tools = tools;\n this.toolMap = toolMap;\n this.provider = provider;\n this.signal = signal;\n this.clientOptions = clientOptions;\n this.streamBuffer = streamBuffer;\n this.graphState = this.createGraphState();\n this.boundModel = this.initializeModel();\n this.toolEnd = toolEnd;\n\n let finalInstructions = instructions ?? '';\n if (additional_instructions) {\n finalInstructions = finalInstructions ? `${finalInstructions}\\n\\n${additional_instructions}` : additional_instructions;\n }\n\n if (finalInstructions) {\n this.systemMessage = new SystemMessage(finalInstructions);\n }\n }\n\n /* Init */\n\n resetValues(): void {\n this.messages = [];\n this.config = resetIfNotEmpty(this.config, undefined);\n this.contentData = resetIfNotEmpty(this.contentData, []);\n this.stepKeyIds = resetIfNotEmpty(this.stepKeyIds, new Map());\n this.toolCallStepIds = resetIfNotEmpty(this.toolCallStepIds, new Map());\n this.contentIndexMap = resetIfNotEmpty(this.contentIndexMap, new Map());\n this.messageIdsByStepKey = resetIfNotEmpty(this.messageIdsByStepKey, new Map());\n this.prelimMessageIdsByStepKey = resetIfNotEmpty(this.prelimMessageIdsByStepKey, new Map());\n }\n\n /* Run Step Processing */\n\n getRunStep(stepId: string): t.RunStep | undefined {\n const index = this.contentIndexMap.get(stepId);\n if (index !== undefined) {\n return this.contentData[index];\n }\n return undefined;\n }\n\n getStepKey(metadata: Record<string, unknown> | undefined): string {\n if (!metadata) return '';\n\n const keyList = this.getKeyList(metadata);\n if (this.checkKeyList(keyList)) {\n throw new Error('Missing metadata');\n }\n\n return joinKeys(keyList);\n }\n\n getStepIdByKey(stepKey: string, index?: number): string {\n const stepIds = this.stepKeyIds.get(stepKey);\n if (!stepIds) {\n throw new Error(`No step IDs found for stepKey ${stepKey}`);\n }\n\n if (index === undefined) {\n return stepIds[stepIds.length - 1];\n }\n\n return stepIds[index];\n }\n\n generateStepId(stepKey: string): [string, number] {\n const stepIds = this.stepKeyIds.get(stepKey);\n let newStepId: string | undefined;\n let stepIndex = 0;\n if (stepIds) {\n stepIndex = stepIds.length;\n newStepId = `step_${nanoid()}`;\n stepIds.push(newStepId);\n this.stepKeyIds.set(stepKey, stepIds);\n } else {\n newStepId = `step_${nanoid()}`;\n this.stepKeyIds.set(stepKey, [newStepId]);\n }\n\n return [newStepId, stepIndex];\n }\n\n getKeyList(metadata: Record<string, unknown> | undefined): (string | number | undefined)[] {\n if (!metadata) return [];\n\n return [\n metadata.run_id as string,\n metadata.thread_id as string,\n metadata.langgraph_node as string,\n metadata.langgraph_step as number,\n metadata.checkpoint_ns as string,\n ];\n }\n\n checkKeyList(keyList: (string | number | undefined)[]): boolean {\n return keyList.some((key) => key === undefined);\n }\n\n /* Misc.*/\n\n getRunMessages(): BaseMessage[] | undefined {\n return this.messages.slice(this.startIndex);\n }\n\n getContentParts(): t.MessageContentComplex[] | undefined {\n return convertMessagesToContent(this.messages.slice(this.startIndex));\n }\n\n /* Graph */\n\n createGraphState(): t.GraphStateChannels<t.BaseGraphState> {\n return {\n messages: {\n value: (x: BaseMessage[], y: BaseMessage[]): BaseMessage[] => {\n if (!x.length) {\n if (this.systemMessage) {\n x.push(this.systemMessage);\n }\n\n this.startIndex = x.length + y.length;\n }\n const current = x.concat(y);\n this.messages = current;\n return current;\n },\n default: () => [],\n },\n };\n }\n\n initializeTools(): CustomToolNode<t.BaseGraphState> | ToolNode<t.BaseGraphState> {\n // return new ToolNode<t.BaseGraphState>(this.tools);\n return new CustomToolNode<t.BaseGraphState>({\n tools: this.tools || [],\n toolMap: this.toolMap,\n });\n }\n\n initializeModel(): Runnable {\n const ChatModelClass = getChatModelClass(this.provider);\n const model = new ChatModelClass(this.clientOptions);\n\n if (!this.tools || this.tools.length === 0) {\n return model as unknown as Runnable;\n }\n\n return (model as t.ModelWithTools).bindTools(this.tools);\n }\n\n getNewModel({\n clientOptions = {},\n omitOriginalOptions,\n } : {\n clientOptions?: t.ClientOptions;\n omitOriginalOptions?: string[]\n }): t.ChatModelInstance {\n const ChatModelClass = getChatModelClass(this.provider);\n const _options = omitOriginalOptions ? Object.fromEntries(\n Object.entries(this.clientOptions).filter(([key]) => !omitOriginalOptions.includes(key)),\n ) : this.clientOptions;\n const options = Object.assign(_options, clientOptions);\n return new ChatModelClass(options);\n }\n\n createCallModel() {\n return async (state: t.BaseGraphState, config?: RunnableConfig): Promise<Partial<t.BaseGraphState>> => {\n const { provider = '' } = (config?.configurable as t.GraphConfig | undefined) ?? {} ;\n if (!config || !provider) {\n throw new Error(`No ${config ? 'provider' : 'config'} provided`);\n }\n if (!config.signal) {\n config.signal = this.signal;\n }\n this.config = config;\n const { messages } = state;\n\n const finalMessages = messages;\n const lastMessageX = finalMessages[finalMessages.length - 2];\n const lastMessageY = finalMessages[finalMessages.length - 1];\n\n if (\n provider === Providers.BEDROCK\n && lastMessageX instanceof AIMessageChunk\n && lastMessageY instanceof ToolMessage\n && typeof lastMessageX.content === 'string'\n ) {\n finalMessages[finalMessages.length - 2].content = '';\n }\n\n if (this.lastStreamCall != null && this.streamBuffer != null) {\n const timeSinceLastCall = Date.now() - this.lastStreamCall;\n if (timeSinceLastCall < this.streamBuffer) {\n const timeToWait = Math.ceil((this.streamBuffer - timeSinceLastCall) / 1000) * 1000;\n await sleep(timeToWait);\n }\n }\n\n this.lastStreamCall = Date.now();\n\n if ((this.tools?.length ?? 0) > 0 && manualToolStreamProviders.has(provider)) {\n const stream = await this.boundModel.stream(finalMessages, config);\n let finalChunk: AIMessageChunk | undefined;\n for await (const chunk of stream) {\n dispatchCustomEvent(GraphEvents.CHAT_MODEL_STREAM, { chunk }, config);\n if (!finalChunk) {\n finalChunk = chunk;\n } else {\n finalChunk = concat(finalChunk, chunk);\n }\n }\n\n finalChunk = modifyDeltaProperties(finalChunk);\n return { messages: [finalChunk as AIMessageChunk] };\n }\n\n const finalMessage = await this.boundModel.invoke(finalMessages, config);\n return { messages: [finalMessage as AIMessageChunk] };\n };\n }\n\n createWorkflow(): t.CompiledWorkflow<t.BaseGraphState> {\n const routeMessage = (state: t.BaseGraphState, config?: RunnableConfig): string => {\n this.config = config;\n // const lastMessage = state.messages[state.messages.length - 1] as AIMessage;\n // if (!lastMessage?.tool_calls?.length) {\n // return END;\n // }\n // return TOOLS;\n return toolsCondition(state);\n };\n\n const workflow = new StateGraph<t.BaseGraphState>({\n channels: this.graphState,\n })\n .addNode(AGENT, this.createCallModel())\n .addNode(TOOLS, this.initializeTools())\n .addEdge(START, AGENT)\n .addConditionalEdges(AGENT, routeMessage)\n .addEdge(TOOLS, this.toolEnd ? END : AGENT);\n\n return workflow.compile();\n }\n\n /* Dispatchers */\n\n /**\n * Dispatches a run step to the client, returns the step ID\n */\n dispatchRunStep(stepKey: string, stepDetails: t.StepDetails): string {\n if (!this.config) {\n throw new Error('No config provided');\n }\n\n const [stepId, stepIndex] = this.generateStepId(stepKey);\n if (stepDetails.type === StepTypes.TOOL_CALLS && stepDetails.tool_calls) {\n for (const tool_call of stepDetails.tool_calls) {\n const toolCallId = tool_call.id ?? '';\n if (!toolCallId || this.toolCallStepIds.has(toolCallId)) {\n continue;\n }\n this.toolCallStepIds.set(toolCallId, stepId);\n }\n }\n\n const runStep: t.RunStep = {\n stepIndex,\n id: stepId,\n type: stepDetails.type,\n index: this.contentData.length,\n stepDetails,\n usage: null,\n };\n\n const runId = this.runId ?? '';\n if (runId) {\n runStep.runId = runId;\n }\n\n this.contentData.push(runStep);\n this.contentIndexMap.set(stepId, runStep.index);\n dispatchCustomEvent(GraphEvents.ON_RUN_STEP, runStep, this.config);\n return stepId;\n }\n\n handleToolCallCompleted(data: t.ToolEndData, metadata?: Record<string, unknown>): void {\n if (!this.config) {\n throw new Error('No config provided');\n }\n\n if (!data.output) {\n return;\n }\n\n const { input, output } = data;\n const { tool_call_id } = output;\n const stepId = this.toolCallStepIds.get(tool_call_id) ?? '';\n if (!stepId) {\n throw new Error(`No stepId found for tool_call_id ${tool_call_id}`);\n }\n\n const runStep = this.getRunStep(stepId);\n if (!runStep) {\n throw new Error(`No run step found for stepId ${stepId}`);\n }\n\n const args = typeof input === 'string' ? input : input.input;\n const tool_call = {\n args: typeof args === 'string' ? args : JSON.stringify(args),\n name: output.name ?? '',\n id: output.tool_call_id,\n output: typeof output.content === 'string'\n ? output.content\n : JSON.stringify(output.content),\n progress: 1,\n };\n\n this.handlerRegistry?.getHandler(GraphEvents.ON_RUN_STEP_COMPLETED)?.handle(\n GraphEvents.ON_RUN_STEP_COMPLETED,\n { result: {\n id: stepId,\n index: runStep.index,\n type: 'tool_call',\n tool_call\n } as t.ToolCompleteEvent,\n },\n metadata,\n this,\n );\n }\n\n dispatchRunStepDelta(id: string, delta: t.ToolCallDelta): void {\n if (!this.config) {\n throw new Error('No config provided');\n } else if (!id) {\n throw new Error('No step ID found');\n }\n const runStepDelta: t.RunStepDeltaEvent = {\n id,\n delta,\n };\n dispatchCustomEvent(GraphEvents.ON_RUN_STEP_DELTA, runStepDelta, this.config);\n }\n\n dispatchMessageDelta(id: string, delta: t.MessageDelta): void {\n if (!this.config) {\n throw new Error('No config provided');\n }\n const messageDelta: t.MessageDeltaEvent = {\n id,\n delta,\n };\n dispatchCustomEvent(GraphEvents.ON_MESSAGE_DELTA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AAE,CAAC;SACZ,CAAC;AAEF,QAAA,IAAI,CAAC,eAAe,EAAE,UAAU,CAAC,WAAW,CAAC,qBAAqB,CAAC,EAAE,MAAM,CACzE,WAAW,CAAC,qBAAqB,EACjC,EAAE,MAAM,EAAE;AACR,gBAAA,EAAE,EAAE,MAAM;gBACV,KAAK,EAAE,OAAO,CAAC,KAAK;AACpB,gBAAA,IAAI,EAAE,WAAW;gBACjB,SAAS;AACa,aAAA;AACvB,SAAA,EACD,QAAQ,EACR,IAAI,CACL,CAAC;KACH;IAED,oBAAoB,CAAC,EAAU,EAAE,KAAsB,EAAA;AACrD,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;AAChB,YAAA,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;SACvC;aAAM,IAAI,CAAC,EAAE,EAAE;AACd,YAAA,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;SACrC;AACD,QAAA,MAAM,YAAY,GAAwB;YACxC,EAAE;YACF,KAAK;SACN,CAAC;QACF,mBAAmB,CAAC,WAAW,CAAC,iBAAiB,EAAE,YAAY,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;KAC/E;IAED,oBAAoB,CAAC,EAAU,EAAE,KAAqB,EAAA;AACpD,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;AAChB,YAAA,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;SACvC;AACD,QAAA,MAAM,YAAY,GAAwB;YACxC,EAAE;YACF,KAAK;SACN,CAAC;QACF,mBAAmB,CAAC,WAAW,CAAC,gBAAgB,EAAE,YAAY,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;KAC9E;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"Graph.mjs","sources":["../../../src/graphs/Graph.ts"],"sourcesContent":["// src/graphs/Graph.ts\nimport { nanoid } from 'nanoid';\nimport { concat } from '@langchain/core/utils/stream';\nimport { ToolNode } from '@langchain/langgraph/prebuilt';\nimport { START, END, StateGraph } from '@langchain/langgraph';\nimport { Runnable, RunnableConfig } from '@langchain/core/runnables';\nimport { dispatchCustomEvent } from '@langchain/core/callbacks/dispatch';\nimport { AIMessageChunk, ToolMessage, SystemMessage } from '@langchain/core/messages';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport type * as t from '@/types';\nimport { Providers, GraphEvents, GraphNodeKeys, StepTypes, Callback } from '@/common';\nimport { getChatModelClass, manualToolStreamProviders } from '@/llm/providers';\nimport { ToolNode as CustomToolNode, toolsCondition } from '@/tools/ToolNode';\nimport { modifyDeltaProperties, convertMessagesToContent } from '@/messages';\nimport { resetIfNotEmpty, joinKeys, sleep } from '@/utils';\nimport { HandlerRegistry } from '@/events';\n\nconst { AGENT, TOOLS } = GraphNodeKeys;\nexport type GraphNode = GraphNodeKeys | typeof START;\nexport type ClientCallback<T extends unknown[]> = (graph: StandardGraph, ...args: T) => void;\nexport type ClientCallbacks = {\n [Callback.TOOL_ERROR]?: ClientCallback<[Error, string]>;\n [Callback.TOOL_START]?: ClientCallback<unknown[]>;\n [Callback.TOOL_END]?: ClientCallback<unknown[]>;\n}\nexport type SystemCallbacks = {\n [K in keyof ClientCallbacks]: ClientCallbacks[K] extends ClientCallback<infer Args>\n ? (...args: Args) => void\n : never;\n};\n\nexport abstract class Graph<\n T extends t.BaseGraphState = t.BaseGraphState,\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n TNodeName extends string = string,\n> {\n abstract resetValues(): void;\n abstract createGraphState(): t.GraphStateChannels<T>;\n abstract initializeTools(): CustomToolNode<T> | ToolNode<T>;\n abstract initializeModel(): Runnable;\n abstract getRunMessages(): BaseMessage[] | undefined;\n abstract getContentParts(): t.MessageContentComplex[] | undefined;\n abstract generateStepId(stepKey: string): [string, number];\n abstract getKeyList(metadata: Record<string, unknown> | undefined): (string | number | undefined)[];\n abstract getStepKey(metadata: Record<string, unknown> | undefined): string;\n abstract checkKeyList(keyList: (string | number | undefined)[]): boolean;\n abstract getStepIdByKey(stepKey: string, index?: number): string\n abstract getRunStep(stepId: string): t.RunStep | undefined;\n abstract dispatchRunStep(stepKey: string, stepDetails: t.StepDetails): string;\n abstract dispatchRunStepDelta(id: string, delta: t.ToolCallDelta): void;\n abstract dispatchMessageDelta(id: string, delta: t.MessageDelta): void;\n abstract handleToolCallCompleted(data: t.ToolEndData, metadata?: Record<string, unknown>): void;\n\n abstract createCallModel(): (state: T, config?: RunnableConfig) => Promise<Partial<T>>;\n abstract createWorkflow(): t.CompiledWorkflow<T>;\n messageIdsByStepKey: Map<string, string> = new Map();\n prelimMessageIdsByStepKey: Map<string, string> = new Map();\n config: RunnableConfig | undefined;\n contentData: t.RunStep[] = [];\n stepKeyIds: Map<string, string[]> = new Map<string, string[]>();\n contentIndexMap: Map<string, number> = new Map();\n toolCallStepIds: Map<string, string> = new Map();\n /** The amount of time that should pass before another consecutive API call */\n streamBuffer: number | undefined;\n signal?: AbortSignal;\n}\n\nexport class StandardGraph extends Graph<\n t.BaseGraphState,\n GraphNode\n> {\n private graphState: t.GraphStateChannels<t.BaseGraphState>;\n private clientOptions: Record<string, unknown>;\n boundModel: Runnable;\n /** The last recorded timestamp that a stream API call was invoked */\n lastStreamCall: number | undefined;\n handlerRegistry: HandlerRegistry | undefined;\n systemMessage: SystemMessage | undefined;\n messages: BaseMessage[] = [];\n runId: string | undefined;\n tools?: t.GenericTool[];\n toolMap?: t.ToolMap;\n startIndex: number = 0;\n provider: Providers;\n toolEnd: boolean;\n signal: AbortSignal | undefined;\n\n constructor({\n runId,\n tools,\n toolMap,\n provider,\n clientOptions,\n instructions,\n signal,\n additional_instructions = '',\n streamBuffer,\n toolEnd = false,\n } : {\n runId?: string;\n signal?: AbortSignal;\n provider: Providers;\n tools?: t.GenericTool[];\n toolMap?: t.ToolMap;\n clientOptions: Record<string, unknown>;\n instructions?: string;\n additional_instructions?: string;\n streamBuffer?: number;\n toolEnd?: boolean;\n }) {\n super();\n this.runId = runId;\n this.tools = tools;\n this.toolMap = toolMap;\n this.provider = provider;\n this.signal = signal;\n this.clientOptions = clientOptions;\n this.streamBuffer = streamBuffer;\n this.graphState = this.createGraphState();\n this.boundModel = this.initializeModel();\n this.toolEnd = toolEnd;\n\n let finalInstructions = instructions ?? '';\n if (additional_instructions) {\n finalInstructions = finalInstructions ? `${finalInstructions}\\n\\n${additional_instructions}` : additional_instructions;\n }\n\n if (finalInstructions) {\n this.systemMessage = new SystemMessage(finalInstructions);\n }\n }\n\n /* Init */\n\n resetValues(keepContent?: boolean): void {\n this.messages = [];\n this.config = resetIfNotEmpty(this.config, undefined);\n if (keepContent !== true) {\n this.contentData = resetIfNotEmpty(this.contentData, []);\n this.contentIndexMap = resetIfNotEmpty(this.contentIndexMap, new Map());\n }\n this.stepKeyIds = resetIfNotEmpty(this.stepKeyIds, new Map());\n this.toolCallStepIds = resetIfNotEmpty(this.toolCallStepIds, new Map());\n this.messageIdsByStepKey = resetIfNotEmpty(this.messageIdsByStepKey, new Map());\n this.prelimMessageIdsByStepKey = resetIfNotEmpty(this.prelimMessageIdsByStepKey, new Map());\n }\n\n /* Run Step Processing */\n\n getRunStep(stepId: string): t.RunStep | undefined {\n const index = this.contentIndexMap.get(stepId);\n if (index !== undefined) {\n return this.contentData[index];\n }\n return undefined;\n }\n\n getStepKey(metadata: Record<string, unknown> | undefined): string {\n if (!metadata) return '';\n\n const keyList = this.getKeyList(metadata);\n if (this.checkKeyList(keyList)) {\n throw new Error('Missing metadata');\n }\n\n return joinKeys(keyList);\n }\n\n getStepIdByKey(stepKey: string, index?: number): string {\n const stepIds = this.stepKeyIds.get(stepKey);\n if (!stepIds) {\n throw new Error(`No step IDs found for stepKey ${stepKey}`);\n }\n\n if (index === undefined) {\n return stepIds[stepIds.length - 1];\n }\n\n return stepIds[index];\n }\n\n generateStepId(stepKey: string): [string, number] {\n const stepIds = this.stepKeyIds.get(stepKey);\n let newStepId: string | undefined;\n let stepIndex = 0;\n if (stepIds) {\n stepIndex = stepIds.length;\n newStepId = `step_${nanoid()}`;\n stepIds.push(newStepId);\n this.stepKeyIds.set(stepKey, stepIds);\n } else {\n newStepId = `step_${nanoid()}`;\n this.stepKeyIds.set(stepKey, [newStepId]);\n }\n\n return [newStepId, stepIndex];\n }\n\n getKeyList(metadata: Record<string, unknown> | undefined): (string | number | undefined)[] {\n if (!metadata) return [];\n\n return [\n metadata.run_id as string,\n metadata.thread_id as string,\n metadata.langgraph_node as string,\n metadata.langgraph_step as number,\n metadata.checkpoint_ns as string,\n ];\n }\n\n checkKeyList(keyList: (string | number | undefined)[]): boolean {\n return keyList.some((key) => key === undefined);\n }\n\n /* Misc.*/\n\n getRunMessages(): BaseMessage[] | undefined {\n return this.messages.slice(this.startIndex);\n }\n\n getContentParts(): t.MessageContentComplex[] | undefined {\n return convertMessagesToContent(this.messages.slice(this.startIndex));\n }\n\n /* Graph */\n\n createGraphState(): t.GraphStateChannels<t.BaseGraphState> {\n return {\n messages: {\n value: (x: BaseMessage[], y: BaseMessage[]): BaseMessage[] => {\n if (!x.length) {\n if (this.systemMessage) {\n x.push(this.systemMessage);\n }\n\n this.startIndex = x.length + y.length;\n }\n const current = x.concat(y);\n this.messages = current;\n return current;\n },\n default: () => [],\n },\n };\n }\n\n initializeTools(): CustomToolNode<t.BaseGraphState> | ToolNode<t.BaseGraphState> {\n // return new ToolNode<t.BaseGraphState>(this.tools);\n return new CustomToolNode<t.BaseGraphState>({\n tools: this.tools || [],\n toolMap: this.toolMap,\n });\n }\n\n initializeModel(): Runnable {\n const ChatModelClass = getChatModelClass(this.provider);\n const model = new ChatModelClass(this.clientOptions);\n\n if (!this.tools || this.tools.length === 0) {\n return model as unknown as Runnable;\n }\n\n return (model as t.ModelWithTools).bindTools(this.tools);\n }\n\n getNewModel({\n clientOptions = {},\n omitOriginalOptions,\n } : {\n clientOptions?: t.ClientOptions;\n omitOriginalOptions?: string[]\n }): t.ChatModelInstance {\n const ChatModelClass = getChatModelClass(this.provider);\n const _options = omitOriginalOptions ? Object.fromEntries(\n Object.entries(this.clientOptions).filter(([key]) => !omitOriginalOptions.includes(key)),\n ) : this.clientOptions;\n const options = Object.assign(_options, clientOptions);\n return new ChatModelClass(options);\n }\n\n createCallModel() {\n return async (state: t.BaseGraphState, config?: RunnableConfig): Promise<Partial<t.BaseGraphState>> => {\n const { provider = '' } = (config?.configurable as t.GraphConfig | undefined) ?? {} ;\n if (!config || !provider) {\n throw new Error(`No ${config ? 'provider' : 'config'} provided`);\n }\n if (!config.signal) {\n config.signal = this.signal;\n }\n this.config = config;\n const { messages } = state;\n\n const finalMessages = messages;\n const lastMessageX = finalMessages[finalMessages.length - 2];\n const lastMessageY = finalMessages[finalMessages.length - 1];\n\n if (\n provider === Providers.BEDROCK\n && lastMessageX instanceof AIMessageChunk\n && lastMessageY instanceof ToolMessage\n && typeof lastMessageX.content === 'string'\n ) {\n finalMessages[finalMessages.length - 2].content = '';\n }\n\n if (this.lastStreamCall != null && this.streamBuffer != null) {\n const timeSinceLastCall = Date.now() - this.lastStreamCall;\n if (timeSinceLastCall < this.streamBuffer) {\n const timeToWait = Math.ceil((this.streamBuffer - timeSinceLastCall) / 1000) * 1000;\n await sleep(timeToWait);\n }\n }\n\n this.lastStreamCall = Date.now();\n\n if ((this.tools?.length ?? 0) > 0 && manualToolStreamProviders.has(provider)) {\n const stream = await this.boundModel.stream(finalMessages, config);\n let finalChunk: AIMessageChunk | undefined;\n for await (const chunk of stream) {\n dispatchCustomEvent(GraphEvents.CHAT_MODEL_STREAM, { chunk }, config);\n if (!finalChunk) {\n finalChunk = chunk;\n } else {\n finalChunk = concat(finalChunk, chunk);\n }\n }\n\n finalChunk = modifyDeltaProperties(finalChunk);\n return { messages: [finalChunk as AIMessageChunk] };\n }\n\n const finalMessage = await this.boundModel.invoke(finalMessages, config);\n return { messages: [finalMessage as AIMessageChunk] };\n };\n }\n\n createWorkflow(): t.CompiledWorkflow<t.BaseGraphState> {\n const routeMessage = (state: t.BaseGraphState, config?: RunnableConfig): string => {\n this.config = config;\n // const lastMessage = state.messages[state.messages.length - 1] as AIMessage;\n // if (!lastMessage?.tool_calls?.length) {\n // return END;\n // }\n // return TOOLS;\n return toolsCondition(state);\n };\n\n const workflow = new StateGraph<t.BaseGraphState>({\n channels: this.graphState,\n })\n .addNode(AGENT, this.createCallModel())\n .addNode(TOOLS, this.initializeTools())\n .addEdge(START, AGENT)\n .addConditionalEdges(AGENT, routeMessage)\n .addEdge(TOOLS, this.toolEnd ? END : AGENT);\n\n return workflow.compile();\n }\n\n /* Dispatchers */\n\n /**\n * Dispatches a run step to the client, returns the step ID\n */\n dispatchRunStep(stepKey: string, stepDetails: t.StepDetails): string {\n if (!this.config) {\n throw new Error('No config provided');\n }\n\n const [stepId, stepIndex] = this.generateStepId(stepKey);\n if (stepDetails.type === StepTypes.TOOL_CALLS && stepDetails.tool_calls) {\n for (const tool_call of stepDetails.tool_calls) {\n const toolCallId = tool_call.id ?? '';\n if (!toolCallId || this.toolCallStepIds.has(toolCallId)) {\n continue;\n }\n this.toolCallStepIds.set(toolCallId, stepId);\n }\n }\n\n const runStep: t.RunStep = {\n stepIndex,\n id: stepId,\n type: stepDetails.type,\n index: this.contentData.length,\n stepDetails,\n usage: null,\n };\n\n const runId = this.runId ?? '';\n if (runId) {\n runStep.runId = runId;\n }\n\n this.contentData.push(runStep);\n this.contentIndexMap.set(stepId, runStep.index);\n dispatchCustomEvent(GraphEvents.ON_RUN_STEP, runStep, this.config);\n return stepId;\n }\n\n handleToolCallCompleted(data: t.ToolEndData, metadata?: Record<string, unknown>): void {\n if (!this.config) {\n throw new Error('No config provided');\n }\n\n if (!data.output) {\n return;\n }\n\n const { input, output } = data;\n const { tool_call_id } = output;\n const stepId = this.toolCallStepIds.get(tool_call_id) ?? '';\n if (!stepId) {\n throw new Error(`No stepId found for tool_call_id ${tool_call_id}`);\n }\n\n const runStep = this.getRunStep(stepId);\n if (!runStep) {\n throw new Error(`No run step found for stepId ${stepId}`);\n }\n\n const args = typeof input === 'string' ? input : input.input;\n const tool_call = {\n args: typeof args === 'string' ? args : JSON.stringify(args),\n name: output.name ?? '',\n id: output.tool_call_id,\n output: typeof output.content === 'string'\n ? output.content\n : JSON.stringify(output.content),\n progress: 1,\n };\n\n this.handlerRegistry?.getHandler(GraphEvents.ON_RUN_STEP_COMPLETED)?.handle(\n GraphEvents.ON_RUN_STEP_COMPLETED,\n { result: {\n id: stepId,\n index: runStep.index,\n type: 'tool_call',\n tool_call\n } as t.ToolCompleteEvent,\n },\n metadata,\n this,\n );\n }\n\n dispatchRunStepDelta(id: string, delta: t.ToolCallDelta): void {\n if (!this.config) {\n throw new Error('No config provided');\n } else if (!id) {\n throw new Error('No step ID found');\n }\n const runStepDelta: t.RunStepDeltaEvent = {\n id,\n delta,\n };\n dispatchCustomEvent(GraphEvents.ON_RUN_STEP_DELTA, runStepDelta, this.config);\n }\n\n dispatchMessageDelta(id: string, delta: t.MessageDelta): void {\n if (!this.config) {\n throw new Error('No config provided');\n }\n const messageDelta: t.MessageDeltaEvent = {\n id,\n delta,\n };\n dispatchCustomEvent(GraphEvents.ON_MESSAGE_DELTA, messageDelta, this.config);\n }\n}\n"],"names":["CustomToolNode"],"mappings":";;;;;;;;;;;;AAAA;AAiBA,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,aAAa,CAAC;MAcjB,KAAK,CAAA;AAwBzB,IAAA,mBAAmB,GAAwB,IAAI,GAAG,EAAE,CAAC;AACrD,IAAA,yBAAyB,GAAwB,IAAI,GAAG,EAAE,CAAC;AAC3D,IAAA,MAAM,CAA6B;IACnC,WAAW,GAAgB,EAAE,CAAC;AAC9B,IAAA,UAAU,GAA0B,IAAI,GAAG,EAAoB,CAAC;AAChE,IAAA,eAAe,GAAwB,IAAI,GAAG,EAAE,CAAC;AACjD,IAAA,eAAe,GAAwB,IAAI,GAAG,EAAE,CAAC;;AAEjD,IAAA,YAAY,CAAqB;AACjC,IAAA,MAAM,CAAe;AACtB,CAAA;AAEK,MAAO,aAAc,SAAQ,KAGlC,CAAA;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AI,CAAC,EAAE,EAAE;AACd,YAAA,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;SACrC;AACD,QAAA,MAAM,YAAY,GAAwB;YACxC,EAAE;YACF,KAAK;SACN,CAAC;QACF,mBAAmB,CAAC,WAAW,CAAC,iBAAiB,EAAE,YAAY,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;KAC/E;IAED,oBAAoB,CAAC,EAAU,EAAE,KAAqB,EAAA;AACpD,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;AAChB,YAAA,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;SACvC;AACD,QAAA,MAAM,YAAY,GAAwB;YACxC,EAAE;YACF,KAAK;SACN,CAAC;QACF,mBAAmB,CAAC,WAAW,CAAC,gBAAgB,EAAE,YAAY,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;KAC9E;AACF;;;;"}
|
|
@@ -19,13 +19,13 @@ function _formatImage(imageUrl) {
|
|
|
19
19
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
|
-
function
|
|
22
|
+
function _ensureMessageContents(messages) {
|
|
23
23
|
// Merge runs of human/tool messages into single human messages with content blocks.
|
|
24
|
-
const
|
|
24
|
+
const updatedMsgs = [];
|
|
25
25
|
for (const message of messages) {
|
|
26
26
|
if (message._getType() === 'tool') {
|
|
27
27
|
if (typeof message.content === 'string') {
|
|
28
|
-
const previousMessage =
|
|
28
|
+
const previousMessage = updatedMsgs[updatedMsgs.length - 1];
|
|
29
29
|
if (previousMessage &&
|
|
30
30
|
previousMessage._getType() === 'human' &&
|
|
31
31
|
Array.isArray(previousMessage.content) &&
|
|
@@ -40,7 +40,7 @@ function _mergeMessages(messages) {
|
|
|
40
40
|
}
|
|
41
41
|
else {
|
|
42
42
|
// If not, we create a new human message with the tool result.
|
|
43
|
-
|
|
43
|
+
updatedMsgs.push(new HumanMessage({
|
|
44
44
|
content: [
|
|
45
45
|
{
|
|
46
46
|
type: 'tool_result',
|
|
@@ -52,7 +52,7 @@ function _mergeMessages(messages) {
|
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
else {
|
|
55
|
-
|
|
55
|
+
updatedMsgs.push(new HumanMessage({
|
|
56
56
|
content: [
|
|
57
57
|
{
|
|
58
58
|
type: 'tool_result',
|
|
@@ -64,32 +64,10 @@ function _mergeMessages(messages) {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
else {
|
|
67
|
-
|
|
68
|
-
if (previousMessage &&
|
|
69
|
-
previousMessage._getType() === 'human' &&
|
|
70
|
-
message._getType() === 'human') {
|
|
71
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
-
let combinedContent;
|
|
73
|
-
if (typeof previousMessage.content === 'string') {
|
|
74
|
-
combinedContent = [{ type: 'text', text: previousMessage.content }];
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
combinedContent = previousMessage.content;
|
|
78
|
-
}
|
|
79
|
-
if (typeof message.content === 'string') {
|
|
80
|
-
combinedContent.push({ type: 'text', text: message.content });
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
combinedContent = combinedContent.concat(message.content);
|
|
84
|
-
}
|
|
85
|
-
previousMessage.content = combinedContent;
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
merged.push(message);
|
|
89
|
-
}
|
|
67
|
+
updatedMsgs.push(message);
|
|
90
68
|
}
|
|
91
69
|
}
|
|
92
|
-
return
|
|
70
|
+
return updatedMsgs;
|
|
93
71
|
}
|
|
94
72
|
function _convertLangChainToolCallToAnthropic(toolCall) {
|
|
95
73
|
if (toolCall.id === undefined) {
|
|
@@ -176,7 +154,7 @@ function _formatContent(content) {
|
|
|
176
154
|
* @returns The formatted prompt.
|
|
177
155
|
*/
|
|
178
156
|
function _convertMessagesToAnthropicPayload(messages) {
|
|
179
|
-
const mergedMessages =
|
|
157
|
+
const mergedMessages = _ensureMessageContents(messages);
|
|
180
158
|
let system;
|
|
181
159
|
if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {
|
|
182
160
|
system = messages[0].content;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message_inputs.mjs","sources":["../../../../../src/llm/anthropic/utils/message_inputs.ts"],"sourcesContent":["/**\n * This util file contains functions for converting LangChain messages to Anthropic messages.\n */\nimport {\n BaseMessage,\n SystemMessage,\n HumanMessage,\n AIMessage,\n ToolMessage,\n MessageContent,\n isAIMessage,\n} from '@langchain/core/messages';\nimport { ToolCall } from '@langchain/core/messages/tool';\nimport type {\n AnthropicMessageCreateParams,\n AnthropicToolResponse,\n} from '@/llm/anthropic/types';\n\nfunction _formatImage(imageUrl: string): { type: string; media_type: string; data: string } {\n const regex = /^data:(image\\/.+);base64,(.+)$/;\n const match = imageUrl.match(regex);\n if (match === null) {\n throw new Error(\n [\n 'Anthropic only supports base64-encoded images currently.',\n 'Example: data:image/png;base64,/9j/4AAQSk...',\n ].join('\\n\\n')\n );\n }\n return {\n type: 'base64',\n media_type: match[1] ?? '',\n data: match[2] ?? '',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n}\n\nfunction _mergeMessages(\n messages: BaseMessage[]\n): (SystemMessage | HumanMessage | AIMessage)[] {\n // Merge runs of human/tool messages into single human messages with content blocks.\n const merged = [];\n for (const message of messages) {\n if (message._getType() === 'tool') {\n if (typeof message.content === 'string') {\n const previousMessage = merged[merged.length - 1] as BaseMessage | undefined;\n if (\n previousMessage &&\n previousMessage._getType() === 'human' &&\n Array.isArray(previousMessage.content) &&\n 'type' in previousMessage.content[0] &&\n previousMessage.content[0].type === 'tool_result'\n ) {\n // If the previous message was a tool result, we merge this tool message into it.\n previousMessage.content.push({\n type: 'tool_result',\n content: message.content,\n tool_use_id: (message as ToolMessage).tool_call_id,\n });\n } else {\n // If not, we create a new human message with the tool result.\n merged.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n content: message.content,\n tool_use_id: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n merged.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n content: _formatContent(message.content),\n tool_use_id: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n const previousMessage = merged[merged.length - 1] as BaseMessage | undefined;\n if (\n previousMessage &&\n previousMessage._getType() === 'human' &&\n message._getType() === 'human'\n ) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let combinedContent: Record<string, any>[];\n if (typeof previousMessage.content === 'string') {\n combinedContent = [{ type: 'text', text: previousMessage.content }];\n } else {\n combinedContent = previousMessage.content;\n }\n if (typeof message.content === 'string') {\n combinedContent.push({ type: 'text', text: message.content });\n } else {\n combinedContent = combinedContent.concat(message.content);\n }\n previousMessage.content = combinedContent;\n } else {\n merged.push(message);\n }\n }\n }\n return merged;\n}\n\nexport function _convertLangChainToolCallToAnthropic(\n toolCall: ToolCall\n): AnthropicToolResponse {\n if (toolCall.id === undefined) {\n throw new Error('Anthropic requires all tool calls to have an \"id\".');\n }\n return {\n type: 'tool_use',\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.args,\n };\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction _formatContent(content: MessageContent): string | Record<string, any>[] {\n const toolTypes = ['tool_use', 'tool_result', 'input_json_delta'];\n const textTypes = ['text', 'text_delta'];\n\n if (typeof content === 'string') {\n return content;\n } else {\n const contentBlocks = content.map((contentPart) => {\n const cacheControl =\n 'cache_control' in contentPart ? contentPart.cache_control : undefined;\n\n if (contentPart.type === 'image_url') {\n let source;\n if (typeof contentPart.image_url === 'string') {\n source = _formatImage(contentPart.image_url);\n } else {\n source = _formatImage(contentPart.image_url.url);\n }\n return {\n type: 'image' as const, // Explicitly setting the type as \"image\"\n source,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n } else if (\n textTypes.find((t) => t === contentPart.type) &&\n 'text' in contentPart\n ) {\n // Assuming contentPart is of type MessageContentText here\n return {\n type: 'text' as const, // Explicitly setting the type as \"text\"\n text: contentPart.text,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n } else if (toolTypes.find((t) => t === contentPart.type)) {\n const contentPartCopy = { ...contentPart };\n if ('index' in contentPartCopy) {\n // Anthropic does not support passing the index field here, so we remove it.\n delete contentPartCopy.index;\n }\n\n if (contentPartCopy.type === 'input_json_delta') {\n // `input_json_delta` type only represents yielding partial tool inputs\n // and is not a valid type for Anthropic messages.\n contentPartCopy.type = 'tool_use';\n }\n\n if ('input' in contentPartCopy) {\n // Anthropic tool use inputs should be valid objects, when applicable.\n try {\n contentPartCopy.input = JSON.parse(contentPartCopy.input);\n } catch {\n // no-op\n }\n }\n\n // TODO: Fix when SDK types are fixed\n return {\n ...contentPartCopy,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n } else {\n throw new Error('Unsupported message content format');\n }\n });\n return contentBlocks;\n }\n}\n\n/**\n * Formats messages as a prompt for the model.\n * Used in LangSmith, export is important here.\n * @param messages The base messages to format as a prompt.\n * @returns The formatted prompt.\n */\nexport function _convertMessagesToAnthropicPayload(\n messages: BaseMessage[]\n): AnthropicMessageCreateParams {\n const mergedMessages = _mergeMessages(messages);\n let system;\n if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {\n system = messages[0].content;\n }\n const conversationMessages =\n system !== undefined ? mergedMessages.slice(1) : mergedMessages;\n const formattedMessages = conversationMessages.map((message) => {\n let role;\n if (message._getType() === 'human') {\n role = 'user' as const;\n } else if (message._getType() === 'ai') {\n role = 'assistant' as const;\n } else if (message._getType() === 'tool') {\n role = 'user' as const;\n } else if (message._getType() === 'system') {\n throw new Error(\n 'System messages are only permitted as the first passed message.'\n );\n } else {\n throw new Error(`Message type \"${message._getType()}\" is not supported.`);\n }\n if (isAIMessage(message) && !!message.tool_calls?.length) {\n if (typeof message.content === 'string') {\n if (message.content === '') {\n return {\n role,\n content: message.tool_calls.map(\n _convertLangChainToolCallToAnthropic\n ),\n };\n } else {\n return {\n role,\n content: [\n { type: 'text', text: message.content },\n ...message.tool_calls.map(_convertLangChainToolCallToAnthropic),\n ],\n };\n }\n } else {\n const { content } = message;\n const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) =>\n content.find(\n (contentPart) =>\n (contentPart.type === 'tool_use' ||\n contentPart.type === 'input_json_delta') &&\n contentPart.id === toolCall.id\n )\n );\n if (hasMismatchedToolCalls) {\n console.warn(\n 'The \"tool_calls\" field on a message is only respected if content is a string.'\n );\n }\n return {\n role,\n content: _formatContent(message.content),\n };\n }\n } else {\n return {\n role,\n content: _formatContent(message.content),\n };\n }\n });\n return {\n messages: formattedMessages,\n system,\n } as AnthropicMessageCreateParams;\n}"],"names":[],"mappings":";;AAAA;;AAEG;AAgBH,SAAS,YAAY,CAAC,QAAgB,EAAA;IACpC,MAAM,KAAK,GAAG,gCAAgC,CAAC;IAC/C,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACpC,IAAA,IAAI,KAAK,KAAK,IAAI,EAAE;QAClB,MAAM,IAAI,KAAK,CACb;YACE,0DAA0D;YAC1D,8CAA8C;AAC/C,SAAA,CAAC,IAAI,CAAC,MAAM,CAAC,CACf,CAAC;KACH;IACD,OAAO;AACL,QAAA,IAAI,EAAE,QAAQ;AACd,QAAA,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE;AAC1B,QAAA,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE;;KAEd,CAAC;AACX,CAAC;AAED,SAAS,cAAc,CACrB,QAAuB,EAAA;;IAGvB,MAAM,MAAM,GAAG,EAAE,CAAC;AAClB,IAAA,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;AAC9B,QAAA,IAAI,OAAO,CAAC,QAAQ,EAAE,KAAK,MAAM,EAAE;AACjC,YAAA,IAAI,OAAO,O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,gBAAA,MAAM,sBAAsB,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,QAAQ,KAChE,OAAO,CAAC,IAAI,CACV,CAAC,WAAW,KACV,CAAC,WAAW,CAAC,IAAI,KAAK,UAAU;AAC9B,oBAAA,WAAW,CAAC,IAAI,KAAK,kBAAkB;oBACzC,WAAW,CAAC,EAAE,KAAK,QAAQ,CAAC,EAAE,CACjC,CACF,CAAC;gBACF,IAAI,sBAAsB,EAAE;AAC1B,oBAAA,OAAO,CAAC,IAAI,CACV,+EAA+E,CAChF,CAAC;iBACH;gBACD,OAAO;oBACL,IAAI;AACJ,oBAAA,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC,OAAO,CAAC;iBACzC,CAAC;aACH;SACF;aAAM;YACL,OAAO;gBACL,IAAI;AACJ,gBAAA,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC,OAAO,CAAC;aACzC,CAAC;SACH;AACH,KAAC,CAAC,CAAC;IACH,OAAO;AACL,QAAA,QAAQ,EAAE,iBAAiB;QAC3B,MAAM;KACyB,CAAC;AACpC;;;;"}
|
|
1
|
+
{"version":3,"file":"message_inputs.mjs","sources":["../../../../../src/llm/anthropic/utils/message_inputs.ts"],"sourcesContent":["/**\n * This util file contains functions for converting LangChain messages to Anthropic messages.\n */\nimport {\n BaseMessage,\n SystemMessage,\n HumanMessage,\n AIMessage,\n ToolMessage,\n MessageContent,\n isAIMessage,\n} from '@langchain/core/messages';\nimport { ToolCall } from '@langchain/core/messages/tool';\nimport type {\n AnthropicMessageCreateParams,\n AnthropicToolResponse,\n} from '@/llm/anthropic/types';\n\nfunction _formatImage(imageUrl: string): { type: string; media_type: string; data: string } {\n const regex = /^data:(image\\/.+);base64,(.+)$/;\n const match = imageUrl.match(regex);\n if (match === null) {\n throw new Error(\n [\n 'Anthropic only supports base64-encoded images currently.',\n 'Example: data:image/png;base64,/9j/4AAQSk...',\n ].join('\\n\\n')\n );\n }\n return {\n type: 'base64',\n media_type: match[1] ?? '',\n data: match[2] ?? '',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n}\n\nfunction _ensureMessageContents(\n messages: BaseMessage[]\n): (SystemMessage | HumanMessage | AIMessage)[] {\n // Merge runs of human/tool messages into single human messages with content blocks.\n const updatedMsgs = [];\n for (const message of messages) {\n if (message._getType() === 'tool') {\n if (typeof message.content === 'string') {\n const previousMessage = updatedMsgs[updatedMsgs.length - 1] as BaseMessage | undefined;\n if (\n previousMessage &&\n previousMessage._getType() === 'human' &&\n Array.isArray(previousMessage.content) &&\n 'type' in previousMessage.content[0] &&\n previousMessage.content[0].type === 'tool_result'\n ) {\n // If the previous message was a tool result, we merge this tool message into it.\n previousMessage.content.push({\n type: 'tool_result',\n content: message.content,\n tool_use_id: (message as ToolMessage).tool_call_id,\n });\n } else {\n // If not, we create a new human message with the tool result.\n updatedMsgs.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n content: message.content,\n tool_use_id: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n updatedMsgs.push(\n new HumanMessage({\n content: [\n {\n type: 'tool_result',\n content: _formatContent(message.content),\n tool_use_id: (message as ToolMessage).tool_call_id,\n },\n ],\n })\n );\n }\n } else {\n updatedMsgs.push(message);\n }\n }\n return updatedMsgs;\n}\n\nexport function _convertLangChainToolCallToAnthropic(\n toolCall: ToolCall\n): AnthropicToolResponse {\n if (toolCall.id === undefined) {\n throw new Error('Anthropic requires all tool calls to have an \"id\".');\n }\n return {\n type: 'tool_use',\n id: toolCall.id,\n name: toolCall.name,\n input: toolCall.args,\n };\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction _formatContent(content: MessageContent): string | Record<string, any>[] {\n const toolTypes = ['tool_use', 'tool_result', 'input_json_delta'];\n const textTypes = ['text', 'text_delta'];\n\n if (typeof content === 'string') {\n return content;\n } else {\n const contentBlocks = content.map((contentPart) => {\n const cacheControl =\n 'cache_control' in contentPart ? contentPart.cache_control : undefined;\n\n if (contentPart.type === 'image_url') {\n let source;\n if (typeof contentPart.image_url === 'string') {\n source = _formatImage(contentPart.image_url);\n } else {\n source = _formatImage(contentPart.image_url.url);\n }\n return {\n type: 'image' as const, // Explicitly setting the type as \"image\"\n source,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n } else if (\n textTypes.find((t) => t === contentPart.type) &&\n 'text' in contentPart\n ) {\n // Assuming contentPart is of type MessageContentText here\n return {\n type: 'text' as const, // Explicitly setting the type as \"text\"\n text: contentPart.text,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n };\n } else if (toolTypes.find((t) => t === contentPart.type)) {\n const contentPartCopy = { ...contentPart };\n if ('index' in contentPartCopy) {\n // Anthropic does not support passing the index field here, so we remove it.\n delete contentPartCopy.index;\n }\n\n if (contentPartCopy.type === 'input_json_delta') {\n // `input_json_delta` type only represents yielding partial tool inputs\n // and is not a valid type for Anthropic messages.\n contentPartCopy.type = 'tool_use';\n }\n\n if ('input' in contentPartCopy) {\n // Anthropic tool use inputs should be valid objects, when applicable.\n try {\n contentPartCopy.input = JSON.parse(contentPartCopy.input);\n } catch {\n // no-op\n }\n }\n\n // TODO: Fix when SDK types are fixed\n return {\n ...contentPartCopy,\n ...(cacheControl ? { cache_control: cacheControl } : {}),\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n } else {\n throw new Error('Unsupported message content format');\n }\n });\n return contentBlocks;\n }\n}\n\n/**\n * Formats messages as a prompt for the model.\n * Used in LangSmith, export is important here.\n * @param messages The base messages to format as a prompt.\n * @returns The formatted prompt.\n */\nexport function _convertMessagesToAnthropicPayload(\n messages: BaseMessage[]\n): AnthropicMessageCreateParams {\n const mergedMessages = _ensureMessageContents(messages);\n let system;\n if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {\n system = messages[0].content;\n }\n const conversationMessages =\n system !== undefined ? mergedMessages.slice(1) : mergedMessages;\n const formattedMessages = conversationMessages.map((message) => {\n let role;\n if (message._getType() === 'human') {\n role = 'user' as const;\n } else if (message._getType() === 'ai') {\n role = 'assistant' as const;\n } else if (message._getType() === 'tool') {\n role = 'user' as const;\n } else if (message._getType() === 'system') {\n throw new Error(\n 'System messages are only permitted as the first passed message.'\n );\n } else {\n throw new Error(`Message type \"${message._getType()}\" is not supported.`);\n }\n if (isAIMessage(message) && !!message.tool_calls?.length) {\n if (typeof message.content === 'string') {\n if (message.content === '') {\n return {\n role,\n content: message.tool_calls.map(\n _convertLangChainToolCallToAnthropic\n ),\n };\n } else {\n return {\n role,\n content: [\n { type: 'text', text: message.content },\n ...message.tool_calls.map(_convertLangChainToolCallToAnthropic),\n ],\n };\n }\n } else {\n const { content } = message;\n const hasMismatchedToolCalls = !message.tool_calls.every((toolCall) =>\n content.find(\n (contentPart) =>\n (contentPart.type === 'tool_use' ||\n contentPart.type === 'input_json_delta') &&\n contentPart.id === toolCall.id\n )\n );\n if (hasMismatchedToolCalls) {\n console.warn(\n 'The \"tool_calls\" field on a message is only respected if content is a string.'\n );\n }\n return {\n role,\n content: _formatContent(message.content),\n };\n }\n } else {\n return {\n role,\n content: _formatContent(message.content),\n };\n }\n });\n return {\n messages: formattedMessages,\n system,\n } as AnthropicMessageCreateParams;\n}"],"names":[],"mappings":";;AAAA;;AAEG;AAgBH,SAAS,YAAY,CAAC,QAAgB,EAAA;IACpC,MAAM,KAAK,GAAG,gCAAgC,CAAC;IAC/C,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACpC,IAAA,IAAI,KAAK,KAAK,IAAI,EAAE;QAClB,MAAM,IAAI,KAAK,CACb;YACE,0DAA0D;YAC1D,8CAA8C;AAC/C,SAAA,CAAC,IAAI,CAAC,MAAM,CAAC,CACf,CAAC;KACH;IACD,OAAO;AACL,QAAA,IAAI,EAAE,QAAQ;AACd,QAAA,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE;AAC1B,QAAA,IAAI,EAAE,KAAK,CAAC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CAAC;aACzC,CAAC;SACH;AACH,KAAC,CAAC,CAAC;IACH,OAAO;AACL,QAAA,QAAQ,EAAE,iBAAiB;QAC3B,MAAM;KACyB,CAAC;AACpC;;;;"}
|
package/dist/esm/run.mjs
CHANGED
|
@@ -66,20 +66,20 @@ class Run {
|
|
|
66
66
|
}
|
|
67
67
|
return this.Graph.getRunMessages();
|
|
68
68
|
}
|
|
69
|
-
async processStream(inputs, config,
|
|
69
|
+
async processStream(inputs, config, streamOptions) {
|
|
70
70
|
if (!this.graphRunnable) {
|
|
71
71
|
throw new Error('Run not initialized. Make sure to use Run.create() to instantiate the Run.');
|
|
72
72
|
}
|
|
73
73
|
if (!this.Graph) {
|
|
74
74
|
throw new Error('Graph not initialized. Make sure to use Run.create() to instantiate the Run.');
|
|
75
75
|
}
|
|
76
|
-
this.Graph.resetValues();
|
|
76
|
+
this.Graph.resetValues(streamOptions?.keepContent);
|
|
77
77
|
const provider = this.Graph.provider;
|
|
78
78
|
const hasTools = this.Graph.tools ? this.Graph.tools.length > 0 : false;
|
|
79
|
-
if (
|
|
79
|
+
if (streamOptions?.callbacks) {
|
|
80
80
|
/* TODO: conflicts with callback manager */
|
|
81
81
|
const callbacks = config.callbacks ?? [];
|
|
82
|
-
config.callbacks = callbacks.concat(this.getCallbacks(
|
|
82
|
+
config.callbacks = callbacks.concat(this.getCallbacks(streamOptions.callbacks));
|
|
83
83
|
}
|
|
84
84
|
if (!this.id) {
|
|
85
85
|
throw new Error('Run ID not provided');
|
|
@@ -97,7 +97,6 @@ class Run {
|
|
|
97
97
|
if (eventName && eventName === GraphEvents.ON_CUSTOM_EVENT) {
|
|
98
98
|
eventName = name;
|
|
99
99
|
}
|
|
100
|
-
// console.log(`Event: ${event.event} | Executing Event: ${eventName}`);
|
|
101
100
|
const handler = this.handlerRegistry.getHandler(eventName);
|
|
102
101
|
if (handler) {
|
|
103
102
|
handler.handle(eventName, data, metadata, this.Graph);
|
package/dist/esm/run.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.mjs","sources":["../../src/run.ts"],"sourcesContent":["// src/run.ts\nimport { PromptTemplate } from '@langchain/core/prompts';\n\nimport type { BaseMessage, MessageContentComplex } from '@langchain/core/messages';\nimport type { RunnableConfig } from '@langchain/core/runnables';\nimport type { ClientCallbacks, SystemCallbacks } from '@/graphs/Graph';\nimport type * as t from '@/types';\nimport { GraphEvents, Providers, Callback } from '@/common';\nimport { manualToolStreamProviders } from '@/llm/providers';\nimport { createTitleRunnable } from '@/utils/title';\nimport { StandardGraph } from '@/graphs/Graph';\nimport { HandlerRegistry } from '@/events';\n\nexport class Run<T extends t.BaseGraphState> {\n graphRunnable?: t.CompiledWorkflow<T, Partial<T>, string>;\n // private collab!: CollabGraph;\n // private taskManager!: TaskManager;\n private handlerRegistry: HandlerRegistry;\n id: string;\n Graph: StandardGraph | undefined;\n provider: Providers | undefined;\n returnContent: boolean = false;\n\n private constructor(config: Partial<t.RunConfig>) {\n const runId = config.runId ?? '';\n if (!runId) {\n throw new Error('Run ID not provided');\n }\n\n this.id = runId;\n\n const handlerRegistry = new HandlerRegistry();\n\n if (config.customHandlers) {\n for (const [eventType, handler] of Object.entries(config.customHandlers)) {\n handlerRegistry.register(eventType, handler);\n }\n }\n\n this.handlerRegistry = handlerRegistry;\n\n if (!config.graphConfig) {\n throw new Error('Graph config not provided');\n }\n\n if (config.graphConfig.type === 'standard' || !config.graphConfig.type) {\n this.provider = config.graphConfig.llmConfig.provider;\n this.graphRunnable = this.createStandardGraph(config.graphConfig) as unknown as t.CompiledWorkflow<T, Partial<T>, string>;\n if (this.Graph) {\n this.Graph.handlerRegistry = handlerRegistry;\n }\n }\n\n this.returnContent = config.returnContent ?? false;\n }\n\n private createStandardGraph(config: t.StandardGraphConfig): t.CompiledWorkflow<t.IState, Partial<t.IState>, string> {\n const { llmConfig, instructions, additional_instructions, signal, streamBuffer, toolEnd, tools = [] } = config;\n const { provider, ...clientOptions } = llmConfig;\n\n const standardGraph = new StandardGraph({\n runId: this.id,\n tools,\n provider,\n signal,\n instructions,\n clientOptions,\n additional_instructions,\n streamBuffer,\n toolEnd,\n });\n this.Graph = standardGraph;\n return standardGraph.createWorkflow();\n }\n\n static async create<T extends t.BaseGraphState>(config: t.RunConfig): Promise<Run<T>> {\n return new Run<T>(config);\n }\n\n getRunMessages(): BaseMessage[] | undefined {\n if (!this.Graph) {\n throw new Error('Graph not initialized. Make sure to use Run.create() to instantiate the Run.');\n }\n return this.Graph.getRunMessages();\n }\n\n async processStream(\n inputs: t.IState,\n config: Partial<RunnableConfig> & { version: 'v1' | 'v2'; run_id?: string },\n clientCallbacks?: ClientCallbacks,\n ): Promise<MessageContentComplex[] | undefined> {\n if (!this.graphRunnable) {\n throw new Error('Run not initialized. Make sure to use Run.create() to instantiate the Run.');\n }\n if (!this.Graph) {\n throw new Error('Graph not initialized. Make sure to use Run.create() to instantiate the Run.');\n }\n\n this.Graph.resetValues();\n const provider = this.Graph.provider;\n const hasTools = this.Graph.tools ? this.Graph.tools.length > 0 : false;\n if (clientCallbacks) {\n /* TODO: conflicts with callback manager */\n const callbacks = config.callbacks as t.ProvidedCallbacks ?? [];\n config.callbacks = callbacks.concat(this.getCallbacks(clientCallbacks));\n }\n\n if (!this.id) {\n throw new Error('Run ID not provided');\n }\n\n config.run_id = this.id;\n config.configurable = Object.assign(config.configurable ?? {}, { run_id: this.id, provider: this.provider });\n\n const stream = this.graphRunnable.streamEvents(inputs, config);\n\n for await (const event of stream) {\n const { data, name, metadata, ...info } = event;\n\n let eventName: t.EventName = info.event;\n if (hasTools && manualToolStreamProviders.has(provider) && eventName === GraphEvents.CHAT_MODEL_STREAM) {\n /* Skipping CHAT_MODEL_STREAM event due to double-call edge case */\n continue;\n }\n\n if (eventName && eventName === GraphEvents.ON_CUSTOM_EVENT) {\n eventName = name;\n }\n\n // console.log(`Event: ${event.event} | Executing Event: ${eventName}`);\n\n const handler = this.handlerRegistry.getHandler(eventName);\n if (handler) {\n handler.handle(eventName, data, metadata, this.Graph);\n }\n }\n\n if (this.returnContent) {\n return this.Graph.getContentParts();\n }\n }\n\n private createSystemCallback<K extends keyof ClientCallbacks>(\n clientCallbacks: ClientCallbacks,\n key: K\n ): SystemCallbacks[K] {\n return ((...args: unknown[]) => {\n const clientCallback = clientCallbacks[key];\n if (clientCallback && this.Graph) {\n (clientCallback as (...args: unknown[]) => void)(this.Graph, ...args);\n }\n }) as SystemCallbacks[K];\n }\n\n getCallbacks(clientCallbacks: ClientCallbacks): SystemCallbacks {\n return {\n [Callback.TOOL_ERROR]: this.createSystemCallback(clientCallbacks, Callback.TOOL_ERROR),\n [Callback.TOOL_START]: this.createSystemCallback(clientCallbacks, Callback.TOOL_START),\n [Callback.TOOL_END]: this.createSystemCallback(clientCallbacks, Callback.TOOL_END),\n };\n }\n\n async generateTitle({\n inputText,\n contentParts,\n titlePrompt,\n clientOptions,\n chainOptions,\n skipLanguage,\n } : {\n inputText: string;\n contentParts: (t.MessageContentComplex | undefined)[];\n titlePrompt?: string;\n skipLanguage?: boolean;\n clientOptions?: t.ClientOptions;\n chainOptions?: Partial<RunnableConfig> | undefined;\n }): Promise<{ language: string; title: string }> {\n const convoTemplate = PromptTemplate.fromTemplate('User: {input}\\nAI: {output}');\n const response = contentParts.map((part) => {\n if (part?.type === 'text') return part.text;\n return '';\n }).join('\\n');\n const convo = (await convoTemplate.invoke({ input: inputText, output: response })).value;\n const model = this.Graph?.getNewModel({\n clientOptions,\n omitOriginalOptions: ['streaming'],\n });\n if (!model) {\n return { language: '', title: '' };\n }\n const chain = await createTitleRunnable(model, titlePrompt);\n return await chain.invoke({ convo, inputText, skipLanguage }, chainOptions) as { language: string; title: string };\n }\n}\n"],"names":[],"mappings":";;;;;;;AAAA;MAaa,GAAG,CAAA;AACd,IAAA,aAAa,CAA6C;;;AAGlD,IAAA,eAAe,CAAkB;AACzC,IAAA,EAAE,CAAS;AACX,IAAA,KAAK,CAA4B;AACjC,IAAA,QAAQ,CAAwB;IACh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,CAAC,IAAI,KAAI;AACzC,YAAA,IAAI,IAAI,EAAE,IAAI,KAAK,MAAM;gBAAE,OAAO,IAAI,CAAC,IAAI,CAAC;AAC5C,YAAA,OAAO,EAAE,CAAC;AACZ,SAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACd,MAAM,KAAK,GAAG,CAAC,MAAM,aAAa,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,EAAE,KAAK,CAAC;AACzF,QAAA,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,EAAE,WAAW,CAAC;YACpC,aAAa;YACb,mBAAmB,EAAE,CAAC,WAAW,CAAC;AACnC,SAAA,CAAC,CAAC;QACH,IAAI,CAAC,KAAK,EAAE;YACV,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;SACpC;QACD,MAAM,KAAK,GAAG,MAAM,mBAAmB,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;AAC5D,QAAA,OAAO,MAAM,KAAK,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,EAAE,YAAY,CAAwC,CAAC;KACpH;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"run.mjs","sources":["../../src/run.ts"],"sourcesContent":["// src/run.ts\nimport { PromptTemplate } from '@langchain/core/prompts';\n\nimport type { BaseMessage, MessageContentComplex } from '@langchain/core/messages';\nimport type { RunnableConfig } from '@langchain/core/runnables';\nimport type { ClientCallbacks, SystemCallbacks } from '@/graphs/Graph';\nimport type * as t from '@/types';\nimport { GraphEvents, Providers, Callback } from '@/common';\nimport { manualToolStreamProviders } from '@/llm/providers';\nimport { createTitleRunnable } from '@/utils/title';\nimport { StandardGraph } from '@/graphs/Graph';\nimport { HandlerRegistry } from '@/events';\n\nexport class Run<T extends t.BaseGraphState> {\n graphRunnable?: t.CompiledWorkflow<T, Partial<T>, string>;\n // private collab!: CollabGraph;\n // private taskManager!: TaskManager;\n private handlerRegistry: HandlerRegistry;\n id: string;\n Graph: StandardGraph | undefined;\n provider: Providers | undefined;\n returnContent: boolean = false;\n\n private constructor(config: Partial<t.RunConfig>) {\n const runId = config.runId ?? '';\n if (!runId) {\n throw new Error('Run ID not provided');\n }\n\n this.id = runId;\n\n const handlerRegistry = new HandlerRegistry();\n\n if (config.customHandlers) {\n for (const [eventType, handler] of Object.entries(config.customHandlers)) {\n handlerRegistry.register(eventType, handler);\n }\n }\n\n this.handlerRegistry = handlerRegistry;\n\n if (!config.graphConfig) {\n throw new Error('Graph config not provided');\n }\n\n if (config.graphConfig.type === 'standard' || !config.graphConfig.type) {\n this.provider = config.graphConfig.llmConfig.provider;\n this.graphRunnable = this.createStandardGraph(config.graphConfig) as unknown as t.CompiledWorkflow<T, Partial<T>, string>;\n if (this.Graph) {\n this.Graph.handlerRegistry = handlerRegistry;\n }\n }\n\n this.returnContent = config.returnContent ?? false;\n }\n\n private createStandardGraph(config: t.StandardGraphConfig): t.CompiledWorkflow<t.IState, Partial<t.IState>, string> {\n const { llmConfig, instructions, additional_instructions, signal, streamBuffer, toolEnd, tools = [] } = config;\n const { provider, ...clientOptions } = llmConfig;\n\n const standardGraph = new StandardGraph({\n runId: this.id,\n tools,\n provider,\n signal,\n instructions,\n clientOptions,\n additional_instructions,\n streamBuffer,\n toolEnd,\n });\n this.Graph = standardGraph;\n return standardGraph.createWorkflow();\n }\n\n static async create<T extends t.BaseGraphState>(config: t.RunConfig): Promise<Run<T>> {\n return new Run<T>(config);\n }\n\n getRunMessages(): BaseMessage[] | undefined {\n if (!this.Graph) {\n throw new Error('Graph not initialized. Make sure to use Run.create() to instantiate the Run.');\n }\n return this.Graph.getRunMessages();\n }\n\n async processStream(\n inputs: t.IState,\n config: Partial<RunnableConfig> & { version: 'v1' | 'v2'; run_id?: string },\n streamOptions?: t.EventStreamOptions,\n ): Promise<MessageContentComplex[] | undefined> {\n if (!this.graphRunnable) {\n throw new Error('Run not initialized. Make sure to use Run.create() to instantiate the Run.');\n }\n if (!this.Graph) {\n throw new Error('Graph not initialized. Make sure to use Run.create() to instantiate the Run.');\n }\n\n this.Graph.resetValues(streamOptions?.keepContent);\n const provider = this.Graph.provider;\n const hasTools = this.Graph.tools ? this.Graph.tools.length > 0 : false;\n if (streamOptions?.callbacks) {\n /* TODO: conflicts with callback manager */\n const callbacks = config.callbacks as t.ProvidedCallbacks ?? [];\n config.callbacks = callbacks.concat(this.getCallbacks(streamOptions.callbacks));\n }\n\n if (!this.id) {\n throw new Error('Run ID not provided');\n }\n\n config.run_id = this.id;\n config.configurable = Object.assign(config.configurable ?? {}, { run_id: this.id, provider: this.provider });\n\n const stream = this.graphRunnable.streamEvents(inputs, config);\n\n for await (const event of stream) {\n const { data, name, metadata, ...info } = event;\n\n let eventName: t.EventName = info.event;\n if (hasTools && manualToolStreamProviders.has(provider) && eventName === GraphEvents.CHAT_MODEL_STREAM) {\n /* Skipping CHAT_MODEL_STREAM event due to double-call edge case */\n continue;\n }\n\n if (eventName && eventName === GraphEvents.ON_CUSTOM_EVENT) {\n eventName = name;\n }\n\n const handler = this.handlerRegistry.getHandler(eventName);\n if (handler) {\n handler.handle(eventName, data, metadata, this.Graph);\n }\n }\n\n if (this.returnContent) {\n return this.Graph.getContentParts();\n }\n }\n\n private createSystemCallback<K extends keyof ClientCallbacks>(\n clientCallbacks: ClientCallbacks,\n key: K\n ): SystemCallbacks[K] {\n return ((...args: unknown[]) => {\n const clientCallback = clientCallbacks[key];\n if (clientCallback && this.Graph) {\n (clientCallback as (...args: unknown[]) => void)(this.Graph, ...args);\n }\n }) as SystemCallbacks[K];\n }\n\n getCallbacks(clientCallbacks: ClientCallbacks): SystemCallbacks {\n return {\n [Callback.TOOL_ERROR]: this.createSystemCallback(clientCallbacks, Callback.TOOL_ERROR),\n [Callback.TOOL_START]: this.createSystemCallback(clientCallbacks, Callback.TOOL_START),\n [Callback.TOOL_END]: this.createSystemCallback(clientCallbacks, Callback.TOOL_END),\n };\n }\n\n async generateTitle({\n inputText,\n contentParts,\n titlePrompt,\n clientOptions,\n chainOptions,\n skipLanguage,\n } : {\n inputText: string;\n contentParts: (t.MessageContentComplex | undefined)[];\n titlePrompt?: string;\n skipLanguage?: boolean;\n clientOptions?: t.ClientOptions;\n chainOptions?: Partial<RunnableConfig> | undefined;\n }): Promise<{ language: string; title: string }> {\n const convoTemplate = PromptTemplate.fromTemplate('User: {input}\\nAI: {output}');\n const response = contentParts.map((part) => {\n if (part?.type === 'text') return part.text;\n return '';\n }).join('\\n');\n const convo = (await convoTemplate.invoke({ input: inputText, output: response })).value;\n const model = this.Graph?.getNewModel({\n clientOptions,\n omitOriginalOptions: ['streaming'],\n });\n if (!model) {\n return { language: '', title: '' };\n }\n const chain = await createTitleRunnable(model, titlePrompt);\n return await chain.invoke({ convo, inputText, skipLanguage }, chainOptions) as { language: string; title: string };\n }\n}\n"],"names":[],"mappings":";;;;;;;AAAA;MAaa,GAAG,CAAA;AACd,IAAA,aAAa,CAA6C;;;AAGlD,IAAA,eAAe,CAAkB;AACzC,IAAA,EAAE,CAAS;AACX,IAAA,KAAK,CAA4B;AACjC,IAAA,QAAQ,CAAwB;IAChC,aAAa,GAAY,KAAK,CAAC;AAE/B,IAAA,WAAA,CAAoB,MAA4B,EAAA;AAC9C,QAAA,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,EAAE,CAAC;QACjC,IAAI,CAAC,KAAK,EAAE;AACV,YAAA,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;SACxC;AAED,QAAA,IAAI,CAAC,EAAE,GAAG,KAAK,CAAC;AAEhB,QAAA,MAAM,eAAe,GAAG,IAAI,eAAe,EAAE,CAAC;AAE9C,QAAA,IAAI,MAAM,CAAC,cAAc,EAAE;AACzB,YAAA,KAAK,MAAM,CAAC,SAAS,EAAE,OAAO,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,cAAc,CAAC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,WAAW,CAAC;AACnC,SAAA,CAAC,CAAC;QACH,IAAI,CAAC,KAAK,EAAE;YACV,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;SACpC;QACD,MAAM,KAAK,GAAG,MAAM,mBAAmB,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;AAC5D,QAAA,OAAO,MAAM,KAAK,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,EAAE,YAAY,CAAwC,CAAC;KACpH;AACF;;;;"}
|
|
@@ -75,7 +75,7 @@ export declare class StandardGraph extends Graph<t.BaseGraphState, GraphNode> {
|
|
|
75
75
|
streamBuffer?: number;
|
|
76
76
|
toolEnd?: boolean;
|
|
77
77
|
});
|
|
78
|
-
resetValues(): void;
|
|
78
|
+
resetValues(keepContent?: boolean): void;
|
|
79
79
|
getRunStep(stepId: string): t.RunStep | undefined;
|
|
80
80
|
getStepKey(metadata: Record<string, unknown> | undefined): string;
|
|
81
81
|
getStepIdByKey(stepKey: string, index?: number): string;
|
package/dist/types/run.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export declare class Run<T extends t.BaseGraphState> {
|
|
|
18
18
|
processStream(inputs: t.IState, config: Partial<RunnableConfig> & {
|
|
19
19
|
version: 'v1' | 'v2';
|
|
20
20
|
run_id?: string;
|
|
21
|
-
},
|
|
21
|
+
}, streamOptions?: t.EventStreamOptions): Promise<MessageContentComplex[] | undefined>;
|
|
22
22
|
private createSystemCallback;
|
|
23
23
|
getCallbacks(clientCallbacks: ClientCallbacks): SystemCallbacks;
|
|
24
24
|
generateTitle({ inputText, contentParts, titlePrompt, clientOptions, chainOptions, skipLanguage, }: {
|
|
@@ -2,6 +2,7 @@ import type * as z from 'zod';
|
|
|
2
2
|
import type { BaseMessage } from '@langchain/core/messages';
|
|
3
3
|
import type { StructuredTool } from '@langchain/core/tools';
|
|
4
4
|
import type { BaseCallbackHandler, CallbackHandlerMethods } from '@langchain/core/callbacks/base';
|
|
5
|
+
import type * as graph from '@/graphs/Graph';
|
|
5
6
|
import type * as e from '@/common/enum';
|
|
6
7
|
import type * as g from '@/types/graph';
|
|
7
8
|
import type * as t from '@/types/tools';
|
|
@@ -57,3 +58,7 @@ export type RunConfig = {
|
|
|
57
58
|
returnContent?: boolean;
|
|
58
59
|
};
|
|
59
60
|
export type ProvidedCallbacks = (BaseCallbackHandler | CallbackHandlerMethods)[] | undefined;
|
|
61
|
+
export type EventStreamOptions = {
|
|
62
|
+
callbacks?: graph.ClientCallbacks;
|
|
63
|
+
keepContent?: boolean;
|
|
64
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@librechat/agents",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.2",
|
|
4
4
|
"main": "./dist/cjs/main.cjs",
|
|
5
5
|
"module": "./dist/esm/main.mjs",
|
|
6
6
|
"types": "./dist/types/index.d.ts",
|
|
@@ -42,9 +42,9 @@
|
|
|
42
42
|
"start:cli": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/cli.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
43
43
|
"content": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/content.ts --provider 'anthropic' --name 'Jo' --location 'New York, NY'",
|
|
44
44
|
"stream": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/stream.ts --provider 'anthropic' --name 'Jo' --location 'New York, NY'",
|
|
45
|
-
"code_exec": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec.ts --provider '
|
|
45
|
+
"code_exec": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec.ts --provider 'anthropic' --name 'Jo' --location 'New York, NY'",
|
|
46
46
|
"code_exec_simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_simple.ts --provider 'anthropic' --name 'Jo' --location 'New York, NY'",
|
|
47
|
-
"simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider '
|
|
47
|
+
"simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
48
48
|
"memory": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/memory.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
49
49
|
"tool-test": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/tools.ts --provider 'anthropic' --name 'Jo' --location 'New York, NY'",
|
|
50
50
|
"abort": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/abort.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"@aws-crypto/sha256-js": "^5.2.0",
|
|
70
70
|
"@aws-sdk/credential-provider-node": "^3.613.0",
|
|
71
71
|
"@aws-sdk/types": "^3.609.0",
|
|
72
|
-
"@langchain/anthropic": "^0.3.
|
|
72
|
+
"@langchain/anthropic": "^0.3.8",
|
|
73
73
|
"@langchain/aws": "^0.1.1",
|
|
74
74
|
"@langchain/community": "^0.3.14",
|
|
75
75
|
"@langchain/core": "^0.3.18",
|
|
@@ -77,7 +77,7 @@
|
|
|
77
77
|
"@langchain/langgraph": "^0.2.19",
|
|
78
78
|
"@langchain/mistralai": "^0.0.26",
|
|
79
79
|
"@langchain/ollama": "^0.1.1",
|
|
80
|
-
"@langchain/openai": "^0.3.
|
|
80
|
+
"@langchain/openai": "^0.3.14",
|
|
81
81
|
"@smithy/eventstream-codec": "^2.2.0",
|
|
82
82
|
"@smithy/protocol-http": "^3.0.6",
|
|
83
83
|
"@smithy/signature-v4": "^2.0.10",
|
package/src/graphs/Graph.ts
CHANGED
|
@@ -132,13 +132,15 @@ export class StandardGraph extends Graph<
|
|
|
132
132
|
|
|
133
133
|
/* Init */
|
|
134
134
|
|
|
135
|
-
resetValues(): void {
|
|
135
|
+
resetValues(keepContent?: boolean): void {
|
|
136
136
|
this.messages = [];
|
|
137
137
|
this.config = resetIfNotEmpty(this.config, undefined);
|
|
138
|
-
|
|
138
|
+
if (keepContent !== true) {
|
|
139
|
+
this.contentData = resetIfNotEmpty(this.contentData, []);
|
|
140
|
+
this.contentIndexMap = resetIfNotEmpty(this.contentIndexMap, new Map());
|
|
141
|
+
}
|
|
139
142
|
this.stepKeyIds = resetIfNotEmpty(this.stepKeyIds, new Map());
|
|
140
143
|
this.toolCallStepIds = resetIfNotEmpty(this.toolCallStepIds, new Map());
|
|
141
|
-
this.contentIndexMap = resetIfNotEmpty(this.contentIndexMap, new Map());
|
|
142
144
|
this.messageIdsByStepKey = resetIfNotEmpty(this.messageIdsByStepKey, new Map());
|
|
143
145
|
this.prelimMessageIdsByStepKey = resetIfNotEmpty(this.prelimMessageIdsByStepKey, new Map());
|
|
144
146
|
}
|
|
@@ -35,15 +35,15 @@ function _formatImage(imageUrl: string): { type: string; media_type: string; dat
|
|
|
35
35
|
} as any;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
function
|
|
38
|
+
function _ensureMessageContents(
|
|
39
39
|
messages: BaseMessage[]
|
|
40
40
|
): (SystemMessage | HumanMessage | AIMessage)[] {
|
|
41
41
|
// Merge runs of human/tool messages into single human messages with content blocks.
|
|
42
|
-
const
|
|
42
|
+
const updatedMsgs = [];
|
|
43
43
|
for (const message of messages) {
|
|
44
44
|
if (message._getType() === 'tool') {
|
|
45
45
|
if (typeof message.content === 'string') {
|
|
46
|
-
const previousMessage =
|
|
46
|
+
const previousMessage = updatedMsgs[updatedMsgs.length - 1] as BaseMessage | undefined;
|
|
47
47
|
if (
|
|
48
48
|
previousMessage &&
|
|
49
49
|
previousMessage._getType() === 'human' &&
|
|
@@ -59,7 +59,7 @@ function _mergeMessages(
|
|
|
59
59
|
});
|
|
60
60
|
} else {
|
|
61
61
|
// If not, we create a new human message with the tool result.
|
|
62
|
-
|
|
62
|
+
updatedMsgs.push(
|
|
63
63
|
new HumanMessage({
|
|
64
64
|
content: [
|
|
65
65
|
{
|
|
@@ -72,7 +72,7 @@ function _mergeMessages(
|
|
|
72
72
|
);
|
|
73
73
|
}
|
|
74
74
|
} else {
|
|
75
|
-
|
|
75
|
+
updatedMsgs.push(
|
|
76
76
|
new HumanMessage({
|
|
77
77
|
content: [
|
|
78
78
|
{
|
|
@@ -85,31 +85,10 @@ function _mergeMessages(
|
|
|
85
85
|
);
|
|
86
86
|
}
|
|
87
87
|
} else {
|
|
88
|
-
|
|
89
|
-
if (
|
|
90
|
-
previousMessage &&
|
|
91
|
-
previousMessage._getType() === 'human' &&
|
|
92
|
-
message._getType() === 'human'
|
|
93
|
-
) {
|
|
94
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
95
|
-
let combinedContent: Record<string, any>[];
|
|
96
|
-
if (typeof previousMessage.content === 'string') {
|
|
97
|
-
combinedContent = [{ type: 'text', text: previousMessage.content }];
|
|
98
|
-
} else {
|
|
99
|
-
combinedContent = previousMessage.content;
|
|
100
|
-
}
|
|
101
|
-
if (typeof message.content === 'string') {
|
|
102
|
-
combinedContent.push({ type: 'text', text: message.content });
|
|
103
|
-
} else {
|
|
104
|
-
combinedContent = combinedContent.concat(message.content);
|
|
105
|
-
}
|
|
106
|
-
previousMessage.content = combinedContent;
|
|
107
|
-
} else {
|
|
108
|
-
merged.push(message);
|
|
109
|
-
}
|
|
88
|
+
updatedMsgs.push(message);
|
|
110
89
|
}
|
|
111
90
|
}
|
|
112
|
-
return
|
|
91
|
+
return updatedMsgs;
|
|
113
92
|
}
|
|
114
93
|
|
|
115
94
|
export function _convertLangChainToolCallToAnthropic(
|
|
@@ -205,7 +184,7 @@ function _formatContent(content: MessageContent): string | Record<string, any>[]
|
|
|
205
184
|
export function _convertMessagesToAnthropicPayload(
|
|
206
185
|
messages: BaseMessage[]
|
|
207
186
|
): AnthropicMessageCreateParams {
|
|
208
|
-
const mergedMessages =
|
|
187
|
+
const mergedMessages = _ensureMessageContents(messages);
|
|
209
188
|
let system;
|
|
210
189
|
if (mergedMessages.length > 0 && mergedMessages[0]._getType() === 'system') {
|
|
211
190
|
system = messages[0].content;
|
|
@@ -77,7 +77,7 @@ export class CollaborativeProcessor {
|
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
private async createGraph(): Promise<t.CompiledWorkflow> {
|
|
80
|
-
const agentStateChannels: StateGraphArgs['channels'] = {
|
|
80
|
+
const agentStateChannels: StateGraphArgs<AgentStateChannels>['channels'] = {
|
|
81
81
|
messages: {
|
|
82
82
|
value: (x?: BaseMessage[], y?: BaseMessage[]) => (x ?? []).concat(y ?? []),
|
|
83
83
|
default: () => [],
|
package/src/run.ts
CHANGED
|
@@ -87,7 +87,7 @@ export class Run<T extends t.BaseGraphState> {
|
|
|
87
87
|
async processStream(
|
|
88
88
|
inputs: t.IState,
|
|
89
89
|
config: Partial<RunnableConfig> & { version: 'v1' | 'v2'; run_id?: string },
|
|
90
|
-
|
|
90
|
+
streamOptions?: t.EventStreamOptions,
|
|
91
91
|
): Promise<MessageContentComplex[] | undefined> {
|
|
92
92
|
if (!this.graphRunnable) {
|
|
93
93
|
throw new Error('Run not initialized. Make sure to use Run.create() to instantiate the Run.');
|
|
@@ -96,13 +96,13 @@ export class Run<T extends t.BaseGraphState> {
|
|
|
96
96
|
throw new Error('Graph not initialized. Make sure to use Run.create() to instantiate the Run.');
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
this.Graph.resetValues();
|
|
99
|
+
this.Graph.resetValues(streamOptions?.keepContent);
|
|
100
100
|
const provider = this.Graph.provider;
|
|
101
101
|
const hasTools = this.Graph.tools ? this.Graph.tools.length > 0 : false;
|
|
102
|
-
if (
|
|
102
|
+
if (streamOptions?.callbacks) {
|
|
103
103
|
/* TODO: conflicts with callback manager */
|
|
104
104
|
const callbacks = config.callbacks as t.ProvidedCallbacks ?? [];
|
|
105
|
-
config.callbacks = callbacks.concat(this.getCallbacks(
|
|
105
|
+
config.callbacks = callbacks.concat(this.getCallbacks(streamOptions.callbacks));
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
if (!this.id) {
|
|
@@ -127,8 +127,6 @@ export class Run<T extends t.BaseGraphState> {
|
|
|
127
127
|
eventName = name;
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
// console.log(`Event: ${event.event} | Executing Event: ${eventName}`);
|
|
131
|
-
|
|
132
130
|
const handler = this.handlerRegistry.getHandler(eventName);
|
|
133
131
|
if (handler) {
|
|
134
132
|
handler.handle(eventName, data, metadata, this.Graph);
|
package/src/scripts/cli3.ts
CHANGED
|
@@ -131,9 +131,11 @@ async function testStandardStreaming(): Promise<void> {
|
|
|
131
131
|
messages: conversationHistory,
|
|
132
132
|
};
|
|
133
133
|
const contentParts = await run.processStream(inputs, config, {
|
|
134
|
+
callbacks: {
|
|
134
135
|
[Callback.TOOL_ERROR]: (graph, error, toolId) => {
|
|
135
136
|
console.error(`Tool ${toolId} failed with error: ${error.message}`);
|
|
136
137
|
},
|
|
138
|
+
},
|
|
137
139
|
});
|
|
138
140
|
const finalMessages = run.getRunMessages();
|
|
139
141
|
if (finalMessages) {
|
package/src/types/run.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type * as z from 'zod';
|
|
|
3
3
|
import type { BaseMessage } from '@langchain/core/messages';
|
|
4
4
|
import type { StructuredTool } from '@langchain/core/tools';
|
|
5
5
|
import type { BaseCallbackHandler, CallbackHandlerMethods } from '@langchain/core/callbacks/base';
|
|
6
|
+
import type * as graph from '@/graphs/Graph';
|
|
6
7
|
import type * as e from '@/common/enum';
|
|
7
8
|
import type * as g from '@/types/graph';
|
|
8
9
|
import type * as t from '@/types/tools';
|
|
@@ -61,3 +62,8 @@ export type RunConfig = {
|
|
|
61
62
|
};
|
|
62
63
|
|
|
63
64
|
export type ProvidedCallbacks = (BaseCallbackHandler | CallbackHandlerMethods)[] | undefined;
|
|
65
|
+
|
|
66
|
+
export type EventStreamOptions = {
|
|
67
|
+
callbacks?: graph.ClientCallbacks;
|
|
68
|
+
keepContent?: boolean;
|
|
69
|
+
}
|