message.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import z from "zod";
  2. const ToolCall = z
  3. .object({
  4. state: z.literal("call"),
  5. step: z.number().optional(),
  6. toolCallId: z.string(),
  7. toolName: z.string(),
  8. args: z.record(z.string(), z.any()),
  9. })
  10. .openapi({
  11. ref: "Session.Message.ToolInvocation.ToolCall",
  12. });
  13. const ToolPartialCall = z
  14. .object({
  15. state: z.literal("partial-call"),
  16. step: z.number().optional(),
  17. toolCallId: z.string(),
  18. toolName: z.string(),
  19. args: z.record(z.string(), z.any()),
  20. })
  21. .openapi({
  22. ref: "Session.Message.ToolInvocation.ToolPartialCall",
  23. });
  24. const ToolResult = z
  25. .object({
  26. state: z.literal("result"),
  27. step: z.number().optional(),
  28. toolCallId: z.string(),
  29. toolName: z.string(),
  30. args: z.record(z.string(), z.any()),
  31. result: z.string(),
  32. })
  33. .openapi({
  34. ref: "Session.Message.ToolInvocation.ToolResult",
  35. });
  36. const ToolInvocation = z
  37. .discriminatedUnion("state", [ToolCall, ToolPartialCall, ToolResult])
  38. .openapi({
  39. ref: "Session.Message.ToolInvocation",
  40. });
  41. export type ToolInvocation = z.infer<typeof ToolInvocation>;
  42. const TextPart = z
  43. .object({
  44. type: z.literal("text"),
  45. text: z.string(),
  46. })
  47. .openapi({
  48. ref: "Session.Message.Part.Text",
  49. });
  50. const ReasoningPart = z
  51. .object({
  52. type: z.literal("reasoning"),
  53. text: z.string(),
  54. providerMetadata: z.record(z.any()).optional(),
  55. })
  56. .openapi({
  57. ref: "Session.Message.Part.Reasoning",
  58. });
  59. const ToolInvocationPart = z
  60. .object({
  61. type: z.literal("tool-invocation"),
  62. toolInvocation: ToolInvocation,
  63. })
  64. .openapi({
  65. ref: "Session.Message.Part.ToolInvocation",
  66. });
  67. const SourceUrlPart = z
  68. .object({
  69. type: z.literal("source-url"),
  70. sourceId: z.string(),
  71. url: z.string(),
  72. title: z.string().optional(),
  73. providerMetadata: z.record(z.any()).optional(),
  74. })
  75. .openapi({
  76. ref: "Session.Message.Part.SourceUrl",
  77. });
  78. const FilePart = z
  79. .object({
  80. type: z.literal("file"),
  81. mediaType: z.string(),
  82. filename: z.string().optional(),
  83. url: z.string(),
  84. })
  85. .openapi({
  86. ref: "Session.Message.Part.File",
  87. });
  88. const StepStartPart = z
  89. .object({
  90. type: z.literal("step-start"),
  91. })
  92. .openapi({
  93. ref: "Session.Message.Part.StepStart",
  94. });
  95. const Part = z
  96. .discriminatedUnion("type", [
  97. TextPart,
  98. ReasoningPart,
  99. ToolInvocationPart,
  100. SourceUrlPart,
  101. FilePart,
  102. StepStartPart,
  103. ])
  104. .openapi({
  105. ref: "Session.Message.Part",
  106. });
  107. export const SessionMessage = z
  108. .object({
  109. id: z.string(),
  110. role: z.enum(["system", "user", "assistant"]),
  111. parts: z.array(Part),
  112. metadata: z.object({
  113. time: z.object({
  114. created: z.number(),
  115. completed: z.number().optional(),
  116. }),
  117. sessionID: z.string(),
  118. tool: z.record(z.string(), z.any()),
  119. }),
  120. })
  121. .openapi({
  122. ref: "Session.Message",
  123. });