Skip to content

Commit

Permalink
core[minor]: Add status field to tool (#6268)
Browse files Browse the repository at this point in the history
* core[minor]: Add status field to tool

* chore: lint files

* update message uti;s

* drop custom error handling in tool

* undefined status if neither passed during concat

* refactor to default success
  • Loading branch information
bracesproul authored Jul 30, 2024
1 parent 12c4ca4 commit cb5c83e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
18 changes: 18 additions & 0 deletions langchain-core/src/messages/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ export function mergeContent(
}
}

/**
* 'Merge' two statuses. If either value passed is 'error', it will return 'error'. Else
* it will return 'success'.
*
* @param {"success" | "error" | undefined} left The existing value to 'merge' with the new value.
* @param {"success" | "error" | undefined} right The new value to 'merge' with the existing value
* @returns {"success" | "error"} The 'merged' value.
*/
export function _mergeStatus(
left?: "success" | "error",
right?: "success" | "error"
): "success" | "error" | undefined {
if (left === "error" || right === "error") {
return "error";
}
return "success";
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function stringifyWithDepthLimit(obj: any, depthLimit: number): string {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
21 changes: 21 additions & 0 deletions langchain-core/src/messages/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
_mergeDicts,
type MessageType,
_mergeObj,
_mergeStatus,
} from "./base.js";

export interface ToolMessageFieldsWithToolCallId extends BaseMessageFields {
Expand All @@ -19,6 +20,11 @@ export interface ToolMessageFieldsWithToolCallId extends BaseMessageFields {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
artifact?: any;
tool_call_id: string;
/**
* Status of the tool invocation.
* @version 0.2.19
*/
status?: "success" | "error";
}

/**
Expand All @@ -34,6 +40,12 @@ export class ToolMessage extends BaseMessage {
return { tool_call_id: "tool_call_id" };
}

/**
* Status of the tool invocation.
* @version 0.2.19
*/
status?: "success" | "error";

tool_call_id: string;

/**
Expand Down Expand Up @@ -66,6 +78,7 @@ export class ToolMessage extends BaseMessage {
super(fields);
this.tool_call_id = fields.tool_call_id;
this.artifact = fields.artifact;
this.status = fields.status;
}

_getType(): MessageType {
Expand All @@ -92,6 +105,12 @@ export class ToolMessage extends BaseMessage {
export class ToolMessageChunk extends BaseMessageChunk {
tool_call_id: string;

/**
* Status of the tool invocation.
* @version 0.2.19
*/
status?: "success" | "error";

/**
* Artifact of the Tool execution which is not meant to be sent to the model.
*
Expand All @@ -106,6 +125,7 @@ export class ToolMessageChunk extends BaseMessageChunk {
super(fields);
this.tool_call_id = fields.tool_call_id;
this.artifact = fields.artifact;
this.status = fields.status;
}

static lc_name() {
Expand All @@ -130,6 +150,7 @@ export class ToolMessageChunk extends BaseMessageChunk {
artifact: _mergeObj(this.artifact, chunk.artifact),
tool_call_id: this.tool_call_id,
id: this.id ?? chunk.id,
status: _mergeStatus(this.status, chunk.status),
});
}

Expand Down
2 changes: 2 additions & 0 deletions langchain-core/src/runnables/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function revive(obj: any): any {
return new ToolMessage({
content: obj.content,
tool_call_id: obj.tool_call_id,
status: obj.status,
});
}
if (obj.type === "AIMessage" || obj.type === "ai") {
Expand Down Expand Up @@ -119,6 +120,7 @@ function revive(obj: any): any {
return new ToolMessageChunk({
content: obj.content,
tool_call_id: obj.tool_call_id,
status: obj.status,
});
}
if (obj.type === "AIMessageChunk") {
Expand Down
9 changes: 4 additions & 5 deletions langchain-core/src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ export abstract class StructuredTool<
let result;
try {
result = await this._call(parsed, runManager, config);
} catch (e) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
await runManager?.handleToolError(e);
throw e;
}
Expand Down Expand Up @@ -512,12 +513,11 @@ export function tool<T extends ZodObjectAny | z.ZodString = ZodObjectAny>(
// If the schema is not provided, or it's a string schema, create a DynamicTool
if (!fields.schema || !("shape" in fields.schema) || !fields.schema.shape) {
return new DynamicTool({
name: fields.name,
...fields,
description:
fields.description ??
fields.schema?.description ??
`${fields.name} tool`,
responseFormat: fields.responseFormat,
func,
});
}
Expand All @@ -526,7 +526,7 @@ export function tool<T extends ZodObjectAny | z.ZodString = ZodObjectAny>(
fields.description ?? fields.schema.description ?? `${fields.name} tool`;

return new DynamicStructuredTool<T extends ZodObjectAny ? T : ZodObjectAny>({
name: fields.name,
...fields,
description,
schema: fields.schema as T extends ZodObjectAny ? T : ZodObjectAny,
// TODO: Consider moving into DynamicStructuredTool constructor
Expand All @@ -547,7 +547,6 @@ export function tool<T extends ZodObjectAny | z.ZodString = ZodObjectAny>(
);
});
},
responseFormat: fields.responseFormat,
});
}

Expand Down

0 comments on commit cb5c83e

Please sign in to comment.