tool.d.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { BaseMessage, BaseMessageChunk, type BaseMessageFields, type MessageType, type MessageContentComplex } from "./base.js";
  2. import type { DataContentBlock } from "./content_blocks.js";
  3. export interface ToolMessageFields extends BaseMessageFields {
  4. content: string | (MessageContentComplex | DataContentBlock)[];
  5. }
  6. export interface ToolMessageFieldsWithToolCallId extends ToolMessageFields {
  7. /**
  8. * Artifact of the Tool execution which is not meant to be sent to the model.
  9. *
  10. * Should only be specified if it is different from the message content, e.g. if only
  11. * a subset of the full tool output is being passed as message content but the full
  12. * output is needed in other parts of the code.
  13. */
  14. artifact?: any;
  15. tool_call_id: string;
  16. /**
  17. * Status of the tool invocation.
  18. * @version 0.2.19
  19. */
  20. status?: "success" | "error";
  21. }
  22. /**
  23. * Marker parameter for objects that tools can return directly.
  24. *
  25. * If a custom BaseTool is invoked with a ToolCall and the output of custom code is
  26. * not an instance of DirectToolOutput, the output will automatically be coerced to
  27. * a string and wrapped in a ToolMessage.
  28. */
  29. export interface DirectToolOutput {
  30. readonly lc_direct_tool_output: true;
  31. }
  32. export declare function isDirectToolOutput(x: unknown): x is DirectToolOutput;
  33. /**
  34. * Represents a tool message in a conversation.
  35. */
  36. export declare class ToolMessage extends BaseMessage implements DirectToolOutput {
  37. content: string | (MessageContentComplex | DataContentBlock)[];
  38. static lc_name(): string;
  39. get lc_aliases(): Record<string, string>;
  40. lc_direct_tool_output: true;
  41. /**
  42. * Status of the tool invocation.
  43. * @version 0.2.19
  44. */
  45. status?: "success" | "error";
  46. tool_call_id: string;
  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. artifact?: any;
  55. constructor(fields: ToolMessageFieldsWithToolCallId);
  56. constructor(fields: string | ToolMessageFields, tool_call_id: string, name?: string);
  57. _getType(): MessageType;
  58. static isInstance(message: BaseMessage): message is ToolMessage;
  59. get _printableFields(): Record<string, unknown>;
  60. }
  61. /**
  62. * Represents a chunk of a tool message, which can be concatenated
  63. * with other tool message chunks.
  64. */
  65. export declare class ToolMessageChunk extends BaseMessageChunk {
  66. content: string | (MessageContentComplex | DataContentBlock)[];
  67. tool_call_id: string;
  68. /**
  69. * Status of the tool invocation.
  70. * @version 0.2.19
  71. */
  72. status?: "success" | "error";
  73. /**
  74. * Artifact of the Tool execution which is not meant to be sent to the model.
  75. *
  76. * Should only be specified if it is different from the message content, e.g. if only
  77. * a subset of the full tool output is being passed as message content but the full
  78. * output is needed in other parts of the code.
  79. */
  80. artifact?: any;
  81. constructor(fields: ToolMessageFieldsWithToolCallId);
  82. static lc_name(): string;
  83. _getType(): MessageType;
  84. concat(chunk: ToolMessageChunk): ToolMessageChunk;
  85. get _printableFields(): Record<string, unknown>;
  86. }
  87. /**
  88. * A call to a tool.
  89. * @property {string} name - The name of the tool to be called
  90. * @property {Record<string, any>} args - The arguments to the tool call
  91. * @property {string} [id] - If provided, an identifier associated with the tool call
  92. */
  93. export type ToolCall = {
  94. name: string;
  95. args: Record<string, any>;
  96. id?: string;
  97. type?: "tool_call";
  98. };
  99. /**
  100. * A chunk of a tool call (e.g., as part of a stream).
  101. * When merging ToolCallChunks (e.g., via AIMessageChunk.__add__),
  102. * all string attributes are concatenated. Chunks are only merged if their
  103. * values of `index` are equal and not None.
  104. *
  105. * @example
  106. * ```ts
  107. * const leftChunks = [
  108. * {
  109. * name: "foo",
  110. * args: '{"a":',
  111. * index: 0
  112. * }
  113. * ];
  114. *
  115. * const leftAIMessageChunk = new AIMessageChunk({
  116. * content: "",
  117. * tool_call_chunks: leftChunks
  118. * });
  119. *
  120. * const rightChunks = [
  121. * {
  122. * name: undefined,
  123. * args: '1}',
  124. * index: 0
  125. * }
  126. * ];
  127. *
  128. * const rightAIMessageChunk = new AIMessageChunk({
  129. * content: "",
  130. * tool_call_chunks: rightChunks
  131. * });
  132. *
  133. * const result = leftAIMessageChunk.concat(rightAIMessageChunk);
  134. * // result.tool_call_chunks is equal to:
  135. * // [
  136. * // {
  137. * // name: "foo",
  138. * // args: '{"a":1}'
  139. * // index: 0
  140. * // }
  141. * // ]
  142. * ```
  143. *
  144. * @property {string} [name] - If provided, a substring of the name of the tool to be called
  145. * @property {string} [args] - If provided, a JSON substring of the arguments to the tool call
  146. * @property {string} [id] - If provided, a substring of an identifier for the tool call
  147. * @property {number} [index] - If provided, the index of the tool call in a sequence
  148. */
  149. export type ToolCallChunk = {
  150. name?: string;
  151. args?: string;
  152. id?: string;
  153. index?: number;
  154. type?: "tool_call_chunk";
  155. };
  156. export type InvalidToolCall = {
  157. name?: string;
  158. args?: string;
  159. id?: string;
  160. error?: string;
  161. type?: "invalid_tool_call";
  162. };
  163. export declare function defaultToolCallParser(rawToolCalls: Record<string, any>[]): [ToolCall[], InvalidToolCall[]];
  164. export declare function isToolMessage(x: BaseMessage): x is ToolMessage;
  165. export declare function isToolMessageChunk(x: BaseMessageChunk): x is ToolMessageChunk;