tool.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { BaseMessage, BaseMessageChunk, mergeContent, _mergeDicts, _mergeObj, _mergeStatus, } from "./base.js";
  2. export function isDirectToolOutput(x) {
  3. return (x != null &&
  4. typeof x === "object" &&
  5. "lc_direct_tool_output" in x &&
  6. x.lc_direct_tool_output === true);
  7. }
  8. /**
  9. * Represents a tool message in a conversation.
  10. */
  11. export class ToolMessage extends BaseMessage {
  12. static lc_name() {
  13. return "ToolMessage";
  14. }
  15. get lc_aliases() {
  16. // exclude snake case conversion to pascal case
  17. return { tool_call_id: "tool_call_id" };
  18. }
  19. constructor(fields, tool_call_id, name) {
  20. if (typeof fields === "string") {
  21. // eslint-disable-next-line no-param-reassign, @typescript-eslint/no-non-null-assertion
  22. fields = { content: fields, name, tool_call_id: tool_call_id };
  23. }
  24. super(fields);
  25. Object.defineProperty(this, "lc_direct_tool_output", {
  26. enumerable: true,
  27. configurable: true,
  28. writable: true,
  29. value: true
  30. });
  31. /**
  32. * Status of the tool invocation.
  33. * @version 0.2.19
  34. */
  35. Object.defineProperty(this, "status", {
  36. enumerable: true,
  37. configurable: true,
  38. writable: true,
  39. value: void 0
  40. });
  41. Object.defineProperty(this, "tool_call_id", {
  42. enumerable: true,
  43. configurable: true,
  44. writable: true,
  45. value: void 0
  46. });
  47. /**
  48. * Artifact of the Tool execution which is not meant to be sent to the model.
  49. *
  50. * Should only be specified if it is different from the message content, e.g. if only
  51. * a subset of the full tool output is being passed as message content but the full
  52. * output is needed in other parts of the code.
  53. */
  54. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  55. Object.defineProperty(this, "artifact", {
  56. enumerable: true,
  57. configurable: true,
  58. writable: true,
  59. value: void 0
  60. });
  61. this.tool_call_id = fields.tool_call_id;
  62. this.artifact = fields.artifact;
  63. this.status = fields.status;
  64. }
  65. _getType() {
  66. return "tool";
  67. }
  68. static isInstance(message) {
  69. return message._getType() === "tool";
  70. }
  71. get _printableFields() {
  72. return {
  73. ...super._printableFields,
  74. tool_call_id: this.tool_call_id,
  75. artifact: this.artifact,
  76. };
  77. }
  78. }
  79. /**
  80. * Represents a chunk of a tool message, which can be concatenated
  81. * with other tool message chunks.
  82. */
  83. export class ToolMessageChunk extends BaseMessageChunk {
  84. constructor(fields) {
  85. super(fields);
  86. Object.defineProperty(this, "tool_call_id", {
  87. enumerable: true,
  88. configurable: true,
  89. writable: true,
  90. value: void 0
  91. });
  92. /**
  93. * Status of the tool invocation.
  94. * @version 0.2.19
  95. */
  96. Object.defineProperty(this, "status", {
  97. enumerable: true,
  98. configurable: true,
  99. writable: true,
  100. value: void 0
  101. });
  102. /**
  103. * Artifact of the Tool execution which is not meant to be sent to the model.
  104. *
  105. * Should only be specified if it is different from the message content, e.g. if only
  106. * a subset of the full tool output is being passed as message content but the full
  107. * output is needed in other parts of the code.
  108. */
  109. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  110. Object.defineProperty(this, "artifact", {
  111. enumerable: true,
  112. configurable: true,
  113. writable: true,
  114. value: void 0
  115. });
  116. this.tool_call_id = fields.tool_call_id;
  117. this.artifact = fields.artifact;
  118. this.status = fields.status;
  119. }
  120. static lc_name() {
  121. return "ToolMessageChunk";
  122. }
  123. _getType() {
  124. return "tool";
  125. }
  126. concat(chunk) {
  127. return new ToolMessageChunk({
  128. content: mergeContent(this.content, chunk.content),
  129. additional_kwargs: _mergeDicts(this.additional_kwargs, chunk.additional_kwargs),
  130. response_metadata: _mergeDicts(this.response_metadata, chunk.response_metadata),
  131. artifact: _mergeObj(this.artifact, chunk.artifact),
  132. tool_call_id: this.tool_call_id,
  133. id: this.id ?? chunk.id,
  134. status: _mergeStatus(this.status, chunk.status),
  135. });
  136. }
  137. get _printableFields() {
  138. return {
  139. ...super._printableFields,
  140. tool_call_id: this.tool_call_id,
  141. artifact: this.artifact,
  142. };
  143. }
  144. }
  145. export function defaultToolCallParser(
  146. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  147. rawToolCalls) {
  148. const toolCalls = [];
  149. const invalidToolCalls = [];
  150. for (const toolCall of rawToolCalls) {
  151. if (!toolCall.function) {
  152. continue;
  153. }
  154. else {
  155. const functionName = toolCall.function.name;
  156. try {
  157. const functionArgs = JSON.parse(toolCall.function.arguments);
  158. const parsed = {
  159. name: functionName || "",
  160. args: functionArgs || {},
  161. id: toolCall.id,
  162. };
  163. toolCalls.push(parsed);
  164. }
  165. catch (error) {
  166. invalidToolCalls.push({
  167. name: functionName,
  168. args: toolCall.function.arguments,
  169. id: toolCall.id,
  170. error: "Malformed args.",
  171. });
  172. }
  173. }
  174. }
  175. return [toolCalls, invalidToolCalls];
  176. }
  177. export function isToolMessage(x) {
  178. return x._getType() === "tool";
  179. }
  180. export function isToolMessageChunk(x) {
  181. return x._getType() === "tool";
  182. }