@langchain/core
Advanced tools
Comparing version 0.2.30 to 0.2.31
@@ -89,2 +89,7 @@ import { zodToJsonSchema } from "zod-to-json-schema"; | ||
for await (const chunk of this._streamResponseChunks(messages, callOptions, runManagers?.[0])) { | ||
if (chunk.message.id == null) { | ||
const runId = runManagers?.at(0)?.runId; | ||
if (runId != null) | ||
chunk.message._updateId(`run-${runId}`); | ||
} | ||
chunk.message.response_metadata = { | ||
@@ -150,2 +155,7 @@ ...chunk.generationInfo, | ||
for await (const chunk of stream) { | ||
if (chunk.message.id == null) { | ||
const runId = runManagers?.at(0)?.runId; | ||
if (runId != null) | ||
chunk.message._updateId(`run-${runId}`); | ||
} | ||
if (aggregated === undefined) { | ||
@@ -180,2 +190,7 @@ aggregated = chunk; | ||
for (const generation of result.generations) { | ||
if (generation.message.id == null) { | ||
const runId = runManagers?.at(0)?.runId; | ||
if (runId != null) | ||
generation.message._updateId(`run-${runId}`); | ||
} | ||
generation.message.response_metadata = { | ||
@@ -182,0 +197,0 @@ ...generation.generationInfo, |
@@ -137,2 +137,3 @@ import { Serializable } from "../load/serializable.js"; | ||
get _printableFields(): Record<string, unknown>; | ||
_updateId(value: string | undefined): void; | ||
get [Symbol.toStringTag](): any; | ||
@@ -157,5 +158,11 @@ } | ||
} | ||
export type MessageFieldWithRole = { | ||
role: StringWithAutocomplete<"user" | "assistant" | MessageType>; | ||
content: MessageContent; | ||
name?: string; | ||
} & Record<string, unknown>; | ||
export declare function _isMessageFieldWithRole(x: BaseMessageLike): x is MessageFieldWithRole; | ||
export type BaseMessageLike = BaseMessage | ({ | ||
type: MessageType | "user" | "assistant" | "placeholder"; | ||
} & BaseMessageFields & Record<string, unknown>) | [ | ||
} & BaseMessageFields & Record<string, unknown>) | MessageFieldWithRole | [ | ||
StringWithAutocomplete<MessageType | "user" | "assistant" | "placeholder">, | ||
@@ -162,0 +169,0 @@ MessageContent |
@@ -179,2 +179,10 @@ import { Serializable } from "../load/serializable.js"; | ||
} | ||
// this private method is used to update the ID for the runtime | ||
// value as well as in lc_kwargs for serialisation | ||
_updateId(value) { | ||
this.id = value; | ||
// lc_attributes wouldn't work here, because jest compares the | ||
// whole object | ||
this.lc_kwargs.id = value; | ||
} | ||
get [Symbol.toStringTag]() { | ||
@@ -310,2 +318,5 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} | ||
export function _isMessageFieldWithRole(x) { | ||
return typeof x.role === "string"; | ||
} | ||
export function isBaseMessage(messageLike) { | ||
@@ -312,0 +323,0 @@ return typeof messageLike?._getType === "function"; |
@@ -0,3 +1,4 @@ | ||
import { _isToolCall } from "../tools/utils.js"; | ||
import { AIMessage, AIMessageChunk } from "./ai.js"; | ||
import { isBaseMessage, } from "./base.js"; | ||
import { isBaseMessage, _isMessageFieldWithRole, } from "./base.js"; | ||
import { ChatMessage, ChatMessageChunk, } from "./chat.js"; | ||
@@ -7,3 +8,28 @@ import { FunctionMessage, FunctionMessageChunk, } from "./function.js"; | ||
import { SystemMessage, SystemMessageChunk } from "./system.js"; | ||
import { ToolMessage } from "./tool.js"; | ||
import { ToolMessage, } from "./tool.js"; | ||
function _coerceToolCall(toolCall) { | ||
if (_isToolCall(toolCall)) { | ||
return toolCall; | ||
} | ||
else if (typeof toolCall.id === "string" && | ||
toolCall.type === "function" && | ||
typeof toolCall.function === "object" && | ||
toolCall.function !== null && | ||
"arguments" in toolCall.function && | ||
typeof toolCall.function.arguments === "string" && | ||
"name" in toolCall.function && | ||
typeof toolCall.function.name === "string") { | ||
// Handle OpenAI tool call format | ||
return { | ||
id: toolCall.id, | ||
args: JSON.parse(toolCall.function.arguments), | ||
name: toolCall.function.name, | ||
type: "tool_call", | ||
}; | ||
} | ||
else { | ||
// TODO: Throw an error? | ||
return toolCall; | ||
} | ||
} | ||
function _constructMessageFromParams(params) { | ||
@@ -15,3 +41,8 @@ const { type, ...rest } = params; | ||
else if (type === "ai" || type === "assistant") { | ||
return new AIMessage(rest); | ||
const { tool_calls: rawToolCalls, ...other } = rest; | ||
if (!Array.isArray(rawToolCalls)) { | ||
return new AIMessage(rest); | ||
} | ||
const tool_calls = rawToolCalls.map(_coerceToolCall); | ||
return new AIMessage({ ...other, tool_calls }); | ||
} | ||
@@ -44,2 +75,6 @@ else if (type === "system") { | ||
} | ||
else if (_isMessageFieldWithRole(messageLike)) { | ||
const { role: type, ...rest } = messageLike; | ||
return _constructMessageFromParams({ ...rest, type }); | ||
} | ||
else { | ||
@@ -46,0 +81,0 @@ return _constructMessageFromParams(messageLike); |
@@ -278,3 +278,3 @@ import { BaseTracer } from "./base.js"; | ||
if (kwargs?.chunk === undefined) { | ||
chunk = new AIMessageChunk({ content: token }); | ||
chunk = new AIMessageChunk({ content: token, id: `run-${run.id}` }); | ||
} | ||
@@ -281,0 +281,0 @@ else { |
@@ -398,3 +398,6 @@ import { applyPatch, } from "../utils/fast-json-patch/index.js"; | ||
else { | ||
streamedOutputValue = new AIMessageChunk(token); | ||
streamedOutputValue = new AIMessageChunk({ | ||
id: `run-${run.id}`, | ||
content: token, | ||
}); | ||
} | ||
@@ -401,0 +404,0 @@ } |
{ | ||
"name": "@langchain/core", | ||
"version": "0.2.30", | ||
"version": "0.2.31", | ||
"description": "Core LangChain.js abstractions and schemas", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2181000
54550