session-message.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. export * as SessionMessage from "./session-message"
  2. import { Schema } from "effect"
  3. import { ProviderMetadata, ToolContent } from "./llm"
  4. import { Model } from "./model"
  5. import { FileAttachment, Prompt } from "./prompt"
  6. import { DateTimeUtcFromMillis } from "./schema"
  7. import { SessionID } from "./session-id"
  8. import { SessionMessageID } from "./session-message-id"
  9. export const ID = SessionMessageID.ID
  10. export type ID = SessionMessageID.ID
  11. export interface UnknownError extends Schema.Schema.Type<typeof UnknownError> {}
  12. export const UnknownError = Schema.Struct({
  13. type: Schema.Literal("unknown"),
  14. message: Schema.String,
  15. }).annotate({ identifier: "Session.Error.Unknown" })
  16. const Base = {
  17. id: ID,
  18. metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
  19. time: Schema.Struct({ created: DateTimeUtcFromMillis }),
  20. }
  21. export interface AgentSwitched extends Schema.Schema.Type<typeof AgentSwitched> {}
  22. export const AgentSwitched = Schema.Struct({
  23. ...Base,
  24. type: Schema.Literal("agent-switched"),
  25. agent: Schema.String,
  26. }).annotate({ identifier: "Session.Message.AgentSwitched" })
  27. export interface ModelSwitched extends Schema.Schema.Type<typeof ModelSwitched> {}
  28. export const ModelSwitched = Schema.Struct({
  29. ...Base,
  30. type: Schema.Literal("model-switched"),
  31. model: Model.Ref,
  32. }).annotate({ identifier: "Session.Message.ModelSwitched" })
  33. export interface User extends Schema.Schema.Type<typeof User> {}
  34. export const User = Schema.Struct({
  35. ...Base,
  36. text: Prompt.fields.text,
  37. files: Prompt.fields.files,
  38. agents: Prompt.fields.agents,
  39. type: Schema.Literal("user"),
  40. }).annotate({ identifier: "Session.Message.User" })
  41. export interface Synthetic extends Schema.Schema.Type<typeof Synthetic> {}
  42. export const Synthetic = Schema.Struct({
  43. ...Base,
  44. sessionID: SessionID.ID,
  45. text: Schema.String,
  46. type: Schema.Literal("synthetic"),
  47. }).annotate({ identifier: "Session.Message.Synthetic" })
  48. export interface System extends Schema.Schema.Type<typeof System> {}
  49. export const System = Schema.Struct({
  50. ...Base,
  51. type: Schema.Literal("system"),
  52. text: Schema.String,
  53. }).annotate({ identifier: "Session.Message.System" })
  54. export interface Shell extends Schema.Schema.Type<typeof Shell> {}
  55. export const Shell = Schema.Struct({
  56. ...Base,
  57. type: Schema.Literal("shell"),
  58. callID: Schema.String,
  59. command: Schema.String,
  60. output: Schema.String,
  61. time: Schema.Struct({
  62. created: DateTimeUtcFromMillis,
  63. completed: DateTimeUtcFromMillis.pipe(Schema.optional),
  64. }),
  65. }).annotate({ identifier: "Session.Message.Shell" })
  66. export interface ToolStatePending extends Schema.Schema.Type<typeof ToolStatePending> {}
  67. export const ToolStatePending = Schema.Struct({
  68. status: Schema.Literal("pending"),
  69. input: Schema.String,
  70. }).annotate({ identifier: "Session.Message.ToolState.Pending" })
  71. export interface ToolStateRunning extends Schema.Schema.Type<typeof ToolStateRunning> {}
  72. export const ToolStateRunning = Schema.Struct({
  73. status: Schema.Literal("running"),
  74. input: Schema.Record(Schema.String, Schema.Unknown),
  75. structured: Schema.Record(Schema.String, Schema.Any),
  76. content: ToolContent.pipe(Schema.Array),
  77. }).annotate({ identifier: "Session.Message.ToolState.Running" })
  78. export interface ToolStateCompleted extends Schema.Schema.Type<typeof ToolStateCompleted> {}
  79. export const ToolStateCompleted = Schema.Struct({
  80. status: Schema.Literal("completed"),
  81. input: Schema.Record(Schema.String, Schema.Unknown),
  82. attachments: FileAttachment.pipe(Schema.Array, Schema.optional),
  83. content: ToolContent.pipe(Schema.Array),
  84. outputPaths: Schema.Array(Schema.String).pipe(Schema.optional),
  85. structured: Schema.Record(Schema.String, Schema.Any),
  86. result: Schema.Unknown.pipe(Schema.optional),
  87. }).annotate({ identifier: "Session.Message.ToolState.Completed" })
  88. export interface ToolStateError extends Schema.Schema.Type<typeof ToolStateError> {}
  89. export const ToolStateError = Schema.Struct({
  90. status: Schema.Literal("error"),
  91. input: Schema.Record(Schema.String, Schema.Unknown),
  92. content: ToolContent.pipe(Schema.Array),
  93. structured: Schema.Record(Schema.String, Schema.Any),
  94. error: UnknownError,
  95. result: Schema.Unknown.pipe(Schema.optional),
  96. }).annotate({ identifier: "Session.Message.ToolState.Error" })
  97. export const ToolState = Schema.Union([ToolStatePending, ToolStateRunning, ToolStateCompleted, ToolStateError]).pipe(
  98. Schema.toTaggedUnion("status"),
  99. )
  100. export type ToolState = ToolStatePending | ToolStateRunning | ToolStateCompleted | ToolStateError
  101. export interface AssistantTool extends Schema.Schema.Type<typeof AssistantTool> {}
  102. export const AssistantTool = Schema.Struct({
  103. type: Schema.Literal("tool"),
  104. id: Schema.String,
  105. name: Schema.String,
  106. provider: Schema.Struct({
  107. executed: Schema.Boolean,
  108. metadata: ProviderMetadata.pipe(Schema.optional),
  109. resultMetadata: ProviderMetadata.pipe(Schema.optional),
  110. }).pipe(Schema.optional),
  111. state: ToolState,
  112. time: Schema.Struct({
  113. created: DateTimeUtcFromMillis,
  114. ran: DateTimeUtcFromMillis.pipe(Schema.optional),
  115. completed: DateTimeUtcFromMillis.pipe(Schema.optional),
  116. pruned: DateTimeUtcFromMillis.pipe(Schema.optional),
  117. }),
  118. }).annotate({ identifier: "Session.Message.Assistant.Tool" })
  119. export interface AssistantText extends Schema.Schema.Type<typeof AssistantText> {}
  120. export const AssistantText = Schema.Struct({
  121. type: Schema.Literal("text"),
  122. id: Schema.String,
  123. text: Schema.String,
  124. }).annotate({ identifier: "Session.Message.Assistant.Text" })
  125. export interface AssistantReasoning extends Schema.Schema.Type<typeof AssistantReasoning> {}
  126. export const AssistantReasoning = Schema.Struct({
  127. type: Schema.Literal("reasoning"),
  128. id: Schema.String,
  129. text: Schema.String,
  130. providerMetadata: ProviderMetadata.pipe(Schema.optional),
  131. }).annotate({ identifier: "Session.Message.Assistant.Reasoning" })
  132. export const AssistantContent = Schema.Union([AssistantText, AssistantReasoning, AssistantTool]).pipe(
  133. Schema.toTaggedUnion("type"),
  134. )
  135. export type AssistantContent = AssistantText | AssistantReasoning | AssistantTool
  136. export interface Assistant extends Schema.Schema.Type<typeof Assistant> {}
  137. export const Assistant = Schema.Struct({
  138. ...Base,
  139. type: Schema.Literal("assistant"),
  140. agent: Schema.String,
  141. model: Model.Ref,
  142. content: AssistantContent.pipe(Schema.Array),
  143. snapshot: Schema.Struct({
  144. start: Schema.String.pipe(Schema.optional),
  145. end: Schema.String.pipe(Schema.optional),
  146. }).pipe(Schema.optional),
  147. finish: Schema.String.pipe(Schema.optional),
  148. cost: Schema.Finite.pipe(Schema.optional),
  149. tokens: Schema.Struct({
  150. input: Schema.Finite,
  151. output: Schema.Finite,
  152. reasoning: Schema.Finite,
  153. cache: Schema.Struct({ read: Schema.Finite, write: Schema.Finite }),
  154. }).pipe(Schema.optional),
  155. error: UnknownError.pipe(Schema.optional),
  156. time: Schema.Struct({
  157. created: DateTimeUtcFromMillis,
  158. completed: DateTimeUtcFromMillis.pipe(Schema.optional),
  159. }),
  160. }).annotate({ identifier: "Session.Message.Assistant" })
  161. export interface Compaction extends Schema.Schema.Type<typeof Compaction> {}
  162. export const Compaction = Schema.Struct({
  163. type: Schema.Literal("compaction"),
  164. reason: Schema.Literals(["auto", "manual"]),
  165. summary: Schema.String,
  166. recent: Schema.String,
  167. ...Base,
  168. }).annotate({ identifier: "Session.Message.Compaction" })
  169. export const Message = Schema.Union([
  170. AgentSwitched,
  171. ModelSwitched,
  172. User,
  173. Synthetic,
  174. System,
  175. Shell,
  176. Assistant,
  177. Compaction,
  178. ])
  179. .pipe(Schema.toTaggedUnion("type"))
  180. .annotate({ identifier: "Session.Message" })
  181. export type Message = AgentSwitched | ModelSwitched | User | Synthetic | System | Shell | Assistant | Compaction
  182. export type Type = Message["type"]