tool.cjs 6.4 KB

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