@librechat/agents 1.4.4 → 1.4.5
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/run.cjs +5 -1
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/esm/run.mjs +5 -1
- package/dist/esm/run.mjs.map +1 -1
- package/dist/types/run.d.ts +1 -0
- package/dist/types/types/run.d.ts +1 -0
- package/package.json +1 -1
- package/src/run.ts +6 -1
- package/src/types/run.ts +1 -0
package/dist/cjs/run.cjs
CHANGED
|
@@ -12,6 +12,7 @@ class Run {
|
|
|
12
12
|
Graph;
|
|
13
13
|
provider;
|
|
14
14
|
run_id;
|
|
15
|
+
returnContent = false;
|
|
15
16
|
constructor(config) {
|
|
16
17
|
const handlerRegistry = new events.HandlerRegistry();
|
|
17
18
|
if (config.customHandlers) {
|
|
@@ -27,6 +28,7 @@ class Run {
|
|
|
27
28
|
this.Graph.handlerRegistry = handlerRegistry;
|
|
28
29
|
}
|
|
29
30
|
}
|
|
31
|
+
this.returnContent = config.returnContent ?? false;
|
|
30
32
|
}
|
|
31
33
|
createStandardGraph(config) {
|
|
32
34
|
const { runId, llmConfig, instructions, additional_instructions, tools = [] } = config;
|
|
@@ -84,7 +86,9 @@ class Run {
|
|
|
84
86
|
handler.handle(eventName, data, metadata, this.Graph);
|
|
85
87
|
}
|
|
86
88
|
}
|
|
87
|
-
|
|
89
|
+
if (this.returnContent) {
|
|
90
|
+
return this.Graph.getContentParts();
|
|
91
|
+
}
|
|
88
92
|
}
|
|
89
93
|
createSystemCallback(clientCallbacks, key) {
|
|
90
94
|
return ((...args) => {
|
package/dist/cjs/run.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.cjs","sources":["../../src/run.ts"],"sourcesContent":["// src/run.ts\nimport type { BaseCallbackHandler, CallbackHandlerMethods } from '@langchain/core/callbacks/base';\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 { 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 private Graph: StandardGraph | undefined;\n provider: Providers | undefined;\n run_id: string | undefined;\n\n private constructor(config: t.RunConfig) {\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.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\n private createStandardGraph(config: t.StandardGraphConfig): t.CompiledWorkflow<t.IState, Partial<t.IState>, string> {\n const { runId, llmConfig, instructions, additional_instructions, tools = [] } = config;\n const { provider, ...clientOptions } = llmConfig;\n\n const standardGraph = new StandardGraph({\n runId,\n tools,\n provider,\n instructions,\n clientOptions,\n additional_instructions,\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' },\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 (BaseCallbackHandler | CallbackHandlerMethods)[] || [];\n config.callbacks = callbacks.concat(this.getCallbacks(clientCallbacks));\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 const isDoubleCallProvider = provider === Providers.ANTHROPIC || provider === Providers.BEDROCK;\n if (hasTools && isDoubleCallProvider && eventName === GraphEvents.CHAT_MODEL_STREAM) {\n /* Skipping CHAT_MODEL_STREAM event for Anthropic 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 return this.Graph.getContentParts();\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"],"names":["HandlerRegistry","StandardGraph","Providers","GraphEvents","Callback"],"mappings":";;;;;;MAUa,GAAG,CAAA;AACd,IAAA,aAAa,CAA6C;;;AAGlD,IAAA,eAAe,CAAkB;AACjC,IAAA,KAAK,CAA4B;AACzC,IAAA,QAAQ,CAAwB;AAChC,IAAA,MAAM,CAAqB;
|
|
1
|
+
{"version":3,"file":"run.cjs","sources":["../../src/run.ts"],"sourcesContent":["// src/run.ts\nimport type { BaseCallbackHandler, CallbackHandlerMethods } from '@langchain/core/callbacks/base';\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 { 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 private Graph: StandardGraph | undefined;\n provider: Providers | undefined;\n run_id: string | undefined;\n returnContent: boolean = false;\n\n private constructor(config: t.RunConfig) {\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.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 { runId, llmConfig, instructions, additional_instructions, tools = [] } = config;\n const { provider, ...clientOptions } = llmConfig;\n\n const standardGraph = new StandardGraph({\n runId,\n tools,\n provider,\n instructions,\n clientOptions,\n additional_instructions,\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' },\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 (BaseCallbackHandler | CallbackHandlerMethods)[] || [];\n config.callbacks = callbacks.concat(this.getCallbacks(clientCallbacks));\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 const isDoubleCallProvider = provider === Providers.ANTHROPIC || provider === Providers.BEDROCK;\n if (hasTools && isDoubleCallProvider && eventName === GraphEvents.CHAT_MODEL_STREAM) {\n /* Skipping CHAT_MODEL_STREAM event for Anthropic 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"],"names":["HandlerRegistry","StandardGraph","Providers","GraphEvents","Callback"],"mappings":";;;;;;MAUa,GAAG,CAAA;AACd,IAAA,aAAa,CAA6C;;;AAGlD,IAAA,eAAe,CAAkB;AACjC,IAAA,KAAK,CAA4B;AACzC,IAAA,QAAQ,CAAwB;AAChC,IAAA,MAAM,CAAqB;IAC3B,aAAa,GAAY,KAAK,CAAC;AAE/B,IAAA,WAAA,CAAoB,MAAmB,EAAA;AACrC,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,MAAM,CAAC,WAAW,CAAC,IAAI,KAAK,UAAU,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE;YAC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erRegistry = new HandlerRegistry();
|
|
15
16
|
if (config.customHandlers) {
|
|
@@ -25,6 +26,7 @@ class Run {
|
|
|
25
26
|
this.Graph.handlerRegistry = handlerRegistry;
|
|
26
27
|
}
|
|
27
28
|
}
|
|
29
|
+
this.returnContent = config.returnContent ?? false;
|
|
28
30
|
}
|
|
29
31
|
createStandardGraph(config) {
|
|
30
32
|
const { runId, llmConfig, instructions, additional_instructions, tools = [] } = config;
|
|
@@ -82,7 +84,9 @@ class Run {
|
|
|
82
84
|
handler.handle(eventName, data, metadata, this.Graph);
|
|
83
85
|
}
|
|
84
86
|
}
|
|
85
|
-
|
|
87
|
+
if (this.returnContent) {
|
|
88
|
+
return this.Graph.getContentParts();
|
|
89
|
+
}
|
|
86
90
|
}
|
|
87
91
|
createSystemCallback(clientCallbacks, key) {
|
|
88
92
|
return ((...args) => {
|
package/dist/esm/run.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.mjs","sources":["../../src/run.ts"],"sourcesContent":["// src/run.ts\nimport type { BaseCallbackHandler, CallbackHandlerMethods } from '@langchain/core/callbacks/base';\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 { 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 private Graph: StandardGraph | undefined;\n provider: Providers | undefined;\n run_id: string | undefined;\n\n private constructor(config: t.RunConfig) {\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.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\n private createStandardGraph(config: t.StandardGraphConfig): t.CompiledWorkflow<t.IState, Partial<t.IState>, string> {\n const { runId, llmConfig, instructions, additional_instructions, tools = [] } = config;\n const { provider, ...clientOptions } = llmConfig;\n\n const standardGraph = new StandardGraph({\n runId,\n tools,\n provider,\n instructions,\n clientOptions,\n additional_instructions,\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' },\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 (BaseCallbackHandler | CallbackHandlerMethods)[] || [];\n config.callbacks = callbacks.concat(this.getCallbacks(clientCallbacks));\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 const isDoubleCallProvider = provider === Providers.ANTHROPIC || provider === Providers.BEDROCK;\n if (hasTools && isDoubleCallProvider && eventName === GraphEvents.CHAT_MODEL_STREAM) {\n /* Skipping CHAT_MODEL_STREAM event for Anthropic 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 return this.Graph.getContentParts();\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"],"names":[],"mappings":";;;;MAUa,GAAG,CAAA;AACd,IAAA,aAAa,CAA6C;;;AAGlD,IAAA,eAAe,CAAkB;AACjC,IAAA,KAAK,CAA4B;AACzC,IAAA,QAAQ,CAAwB;AAChC,IAAA,MAAM,CAAqB;
|
|
1
|
+
{"version":3,"file":"run.mjs","sources":["../../src/run.ts"],"sourcesContent":["// src/run.ts\nimport type { BaseCallbackHandler, CallbackHandlerMethods } from '@langchain/core/callbacks/base';\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 { 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 private Graph: StandardGraph | undefined;\n provider: Providers | undefined;\n run_id: string | undefined;\n returnContent: boolean = false;\n\n private constructor(config: t.RunConfig) {\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.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 { runId, llmConfig, instructions, additional_instructions, tools = [] } = config;\n const { provider, ...clientOptions } = llmConfig;\n\n const standardGraph = new StandardGraph({\n runId,\n tools,\n provider,\n instructions,\n clientOptions,\n additional_instructions,\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' },\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 (BaseCallbackHandler | CallbackHandlerMethods)[] || [];\n config.callbacks = callbacks.concat(this.getCallbacks(clientCallbacks));\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 const isDoubleCallProvider = provider === Providers.ANTHROPIC || provider === Providers.BEDROCK;\n if (hasTools && isDoubleCallProvider && eventName === GraphEvents.CHAT_MODEL_STREAM) {\n /* Skipping CHAT_MODEL_STREAM event for Anthropic 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"],"names":[],"mappings":";;;;MAUa,G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,IAAI,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,CAAC;aACvE;AACH,SAAC,EAAwB;KAC1B;AAED,IAAA,YAAY,CAAC,eAAgC,EAAA;QAC3C,OAAO;AACL,YAAA,CAAC,QAAQ,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,CAAC,eAAe,EAAE,QAAQ,CAAC,UAAU,CAAC;AACtF,YAAA,CAAC,QAAQ,CAAC,UAAU,GAAG,IAAI,CAAC,oBAAoB,CAAC,eAAe,EAAE,QAAQ,CAAC,UAAU,CAAC;AACtF,YAAA,CAAC,QAAQ,CAAC,QAAQ,GAAG,IAAI,CAAC,oBAAoB,CAAC,eAAe,EAAE,QAAQ,CAAC,QAAQ,CAAC;SACnF,CAAC;KACH;AACF;;;;"}
|
package/dist/types/run.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export declare class Run<T extends t.BaseGraphState> {
|
|
|
9
9
|
private Graph;
|
|
10
10
|
provider: Providers | undefined;
|
|
11
11
|
run_id: string | undefined;
|
|
12
|
+
returnContent: boolean;
|
|
12
13
|
private constructor();
|
|
13
14
|
private createStandardGraph;
|
|
14
15
|
static create<T extends t.BaseGraphState>(config: t.RunConfig): Promise<Run<T>>;
|
package/package.json
CHANGED
package/src/run.ts
CHANGED
|
@@ -16,6 +16,7 @@ export class Run<T extends t.BaseGraphState> {
|
|
|
16
16
|
private Graph: StandardGraph | undefined;
|
|
17
17
|
provider: Providers | undefined;
|
|
18
18
|
run_id: string | undefined;
|
|
19
|
+
returnContent: boolean = false;
|
|
19
20
|
|
|
20
21
|
private constructor(config: t.RunConfig) {
|
|
21
22
|
const handlerRegistry = new HandlerRegistry();
|
|
@@ -35,6 +36,8 @@ export class Run<T extends t.BaseGraphState> {
|
|
|
35
36
|
this.Graph.handlerRegistry = handlerRegistry;
|
|
36
37
|
}
|
|
37
38
|
}
|
|
39
|
+
|
|
40
|
+
this.returnContent = config.returnContent ?? false;
|
|
38
41
|
}
|
|
39
42
|
|
|
40
43
|
private createStandardGraph(config: t.StandardGraphConfig): t.CompiledWorkflow<t.IState, Partial<t.IState>, string> {
|
|
@@ -108,7 +111,9 @@ export class Run<T extends t.BaseGraphState> {
|
|
|
108
111
|
}
|
|
109
112
|
}
|
|
110
113
|
|
|
111
|
-
|
|
114
|
+
if (this.returnContent) {
|
|
115
|
+
return this.Graph.getContentParts();
|
|
116
|
+
}
|
|
112
117
|
}
|
|
113
118
|
|
|
114
119
|
private createSystemCallback<K extends keyof ClientCallbacks>(
|
package/src/types/run.ts
CHANGED