session-event.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. import { Schema } from "effect"
  2. import { EventV2 } from "./event"
  3. import { ModelV2 } from "./model"
  4. import { NonNegativeInt } from "./schema"
  5. import { Session } from "./session"
  6. import { FileAttachment, Prompt } from "./session-prompt"
  7. import { ToolOutput } from "./tool-output"
  8. import { V2Schema } from "./v2-schema"
  9. export { FileAttachment }
  10. export const Source = Schema.Struct({
  11. start: NonNegativeInt,
  12. end: NonNegativeInt,
  13. text: Schema.String,
  14. }).annotate({
  15. identifier: "session.next.event.source",
  16. })
  17. export type Source = typeof Source.Type
  18. const Base = {
  19. timestamp: V2Schema.DateTimeUtcFromMillis,
  20. sessionID: Session.ID,
  21. }
  22. const options = {
  23. aggregate: "sessionID",
  24. version: 1,
  25. } as const
  26. export const UnknownError = Schema.Struct({
  27. type: Schema.Literal("unknown"),
  28. message: Schema.String,
  29. }).annotate({
  30. identifier: "Session.Error.Unknown",
  31. })
  32. export type UnknownError = typeof UnknownError.Type
  33. export const AgentSwitched = EventV2.define({
  34. type: "session.next.agent.switched",
  35. ...options,
  36. schema: {
  37. ...Base,
  38. agent: Schema.String,
  39. },
  40. })
  41. export type AgentSwitched = typeof AgentSwitched.Type
  42. export const ModelSwitched = EventV2.define({
  43. type: "session.next.model.switched",
  44. ...options,
  45. schema: {
  46. ...Base,
  47. model: ModelV2.Ref,
  48. },
  49. })
  50. export type ModelSwitched = typeof ModelSwitched.Type
  51. export const Prompted = EventV2.define({
  52. type: "session.next.prompted",
  53. ...options,
  54. schema: {
  55. ...Base,
  56. prompt: Prompt,
  57. },
  58. })
  59. export type Prompted = typeof Prompted.Type
  60. export const Synthetic = EventV2.define({
  61. type: "session.next.synthetic",
  62. ...options,
  63. schema: {
  64. ...Base,
  65. text: Schema.String,
  66. },
  67. })
  68. export type Synthetic = typeof Synthetic.Type
  69. export namespace Shell {
  70. export const Started = EventV2.define({
  71. type: "session.next.shell.started",
  72. ...options,
  73. schema: {
  74. ...Base,
  75. callID: Schema.String,
  76. command: Schema.String,
  77. },
  78. })
  79. export type Started = typeof Started.Type
  80. export const Ended = EventV2.define({
  81. type: "session.next.shell.ended",
  82. ...options,
  83. schema: {
  84. ...Base,
  85. callID: Schema.String,
  86. output: Schema.String,
  87. },
  88. })
  89. export type Ended = typeof Ended.Type
  90. }
  91. export namespace Step {
  92. export const Started = EventV2.define({
  93. type: "session.next.step.started",
  94. ...options,
  95. schema: {
  96. ...Base,
  97. agent: Schema.String,
  98. model: ModelV2.Ref,
  99. snapshot: Schema.String.pipe(Schema.optional),
  100. },
  101. })
  102. export type Started = typeof Started.Type
  103. export const Ended = EventV2.define({
  104. type: "session.next.step.ended",
  105. ...options,
  106. schema: {
  107. ...Base,
  108. finish: Schema.String,
  109. cost: Schema.Finite,
  110. tokens: Schema.Struct({
  111. input: Schema.Finite,
  112. output: Schema.Finite,
  113. reasoning: Schema.Finite,
  114. cache: Schema.Struct({
  115. read: Schema.Finite,
  116. write: Schema.Finite,
  117. }),
  118. }),
  119. snapshot: Schema.String.pipe(Schema.optional),
  120. },
  121. })
  122. export type Ended = typeof Ended.Type
  123. export const Failed = EventV2.define({
  124. type: "session.next.step.failed",
  125. ...options,
  126. schema: {
  127. ...Base,
  128. error: UnknownError,
  129. },
  130. })
  131. export type Failed = typeof Failed.Type
  132. }
  133. export namespace Text {
  134. export const Started = EventV2.define({
  135. type: "session.next.text.started",
  136. ...options,
  137. schema: {
  138. ...Base,
  139. },
  140. })
  141. export type Started = typeof Started.Type
  142. export const Delta = EventV2.define({
  143. type: "session.next.text.delta",
  144. ...options,
  145. schema: {
  146. ...Base,
  147. delta: Schema.String,
  148. },
  149. })
  150. export type Delta = typeof Delta.Type
  151. export const Ended = EventV2.define({
  152. type: "session.next.text.ended",
  153. ...options,
  154. schema: {
  155. ...Base,
  156. text: Schema.String,
  157. },
  158. })
  159. export type Ended = typeof Ended.Type
  160. }
  161. export namespace Reasoning {
  162. export const Started = EventV2.define({
  163. type: "session.next.reasoning.started",
  164. ...options,
  165. schema: {
  166. ...Base,
  167. reasoningID: Schema.String,
  168. },
  169. })
  170. export type Started = typeof Started.Type
  171. export const Delta = EventV2.define({
  172. type: "session.next.reasoning.delta",
  173. ...options,
  174. schema: {
  175. ...Base,
  176. reasoningID: Schema.String,
  177. delta: Schema.String,
  178. },
  179. })
  180. export type Delta = typeof Delta.Type
  181. export const Ended = EventV2.define({
  182. type: "session.next.reasoning.ended",
  183. ...options,
  184. schema: {
  185. ...Base,
  186. reasoningID: Schema.String,
  187. text: Schema.String,
  188. },
  189. })
  190. export type Ended = typeof Ended.Type
  191. }
  192. export namespace Tool {
  193. export namespace Input {
  194. export const Started = EventV2.define({
  195. type: "session.next.tool.input.started",
  196. ...options,
  197. schema: {
  198. ...Base,
  199. callID: Schema.String,
  200. name: Schema.String,
  201. },
  202. })
  203. export type Started = typeof Started.Type
  204. export const Delta = EventV2.define({
  205. type: "session.next.tool.input.delta",
  206. ...options,
  207. schema: {
  208. ...Base,
  209. callID: Schema.String,
  210. delta: Schema.String,
  211. },
  212. })
  213. export type Delta = typeof Delta.Type
  214. export const Ended = EventV2.define({
  215. type: "session.next.tool.input.ended",
  216. ...options,
  217. schema: {
  218. ...Base,
  219. callID: Schema.String,
  220. text: Schema.String,
  221. },
  222. })
  223. export type Ended = typeof Ended.Type
  224. }
  225. export const Called = EventV2.define({
  226. type: "session.next.tool.called",
  227. ...options,
  228. schema: {
  229. ...Base,
  230. callID: Schema.String,
  231. tool: Schema.String,
  232. input: Schema.Record(Schema.String, Schema.Unknown),
  233. provider: Schema.Struct({
  234. executed: Schema.Boolean,
  235. metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
  236. }),
  237. },
  238. })
  239. export type Called = typeof Called.Type
  240. export const Progress = EventV2.define({
  241. type: "session.next.tool.progress",
  242. ...options,
  243. schema: {
  244. ...Base,
  245. callID: Schema.String,
  246. structured: ToolOutput.Structured,
  247. content: Schema.Array(ToolOutput.Content),
  248. },
  249. })
  250. export type Progress = typeof Progress.Type
  251. export const Success = EventV2.define({
  252. type: "session.next.tool.success",
  253. ...options,
  254. schema: {
  255. ...Base,
  256. callID: Schema.String,
  257. structured: ToolOutput.Structured,
  258. content: Schema.Array(ToolOutput.Content),
  259. provider: Schema.Struct({
  260. executed: Schema.Boolean,
  261. metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
  262. }),
  263. },
  264. })
  265. export type Success = typeof Success.Type
  266. export const Failed = EventV2.define({
  267. type: "session.next.tool.failed",
  268. ...options,
  269. schema: {
  270. ...Base,
  271. callID: Schema.String,
  272. error: UnknownError,
  273. provider: Schema.Struct({
  274. executed: Schema.Boolean,
  275. metadata: Schema.Record(Schema.String, Schema.Unknown).pipe(Schema.optional),
  276. }),
  277. },
  278. })
  279. export type Failed = typeof Failed.Type
  280. }
  281. export const RetryError = Schema.Struct({
  282. message: Schema.String,
  283. statusCode: Schema.Finite.pipe(Schema.optional),
  284. isRetryable: Schema.Boolean,
  285. responseHeaders: Schema.Record(Schema.String, Schema.String).pipe(Schema.optional),
  286. responseBody: Schema.String.pipe(Schema.optional),
  287. metadata: Schema.Record(Schema.String, Schema.String).pipe(Schema.optional),
  288. }).annotate({
  289. identifier: "session.next.retry_error",
  290. })
  291. export type RetryError = typeof RetryError.Type
  292. export const Retried = EventV2.define({
  293. type: "session.next.retried",
  294. ...options,
  295. schema: {
  296. ...Base,
  297. attempt: Schema.Finite,
  298. error: RetryError,
  299. },
  300. })
  301. export type Retried = typeof Retried.Type
  302. export namespace Compaction {
  303. export const Started = EventV2.define({
  304. type: "session.next.compaction.started",
  305. ...options,
  306. schema: {
  307. ...Base,
  308. reason: Schema.Union([Schema.Literal("auto"), Schema.Literal("manual")]),
  309. },
  310. })
  311. export type Started = typeof Started.Type
  312. export const Delta = EventV2.define({
  313. type: "session.next.compaction.delta",
  314. ...options,
  315. schema: {
  316. ...Base,
  317. text: Schema.String,
  318. },
  319. })
  320. export type Delta = typeof Delta.Type
  321. export const Ended = EventV2.define({
  322. type: "session.next.compaction.ended",
  323. ...options,
  324. schema: {
  325. ...Base,
  326. text: Schema.String,
  327. include: Schema.String.pipe(Schema.optional),
  328. },
  329. })
  330. export type Ended = typeof Ended.Type
  331. }
  332. export const All = Schema.Union(
  333. [
  334. AgentSwitched,
  335. ModelSwitched,
  336. Prompted,
  337. Synthetic,
  338. Shell.Started,
  339. Shell.Ended,
  340. Step.Started,
  341. Step.Ended,
  342. Step.Failed,
  343. Text.Started,
  344. Text.Delta,
  345. Text.Ended,
  346. Tool.Input.Started,
  347. Tool.Input.Delta,
  348. Tool.Input.Ended,
  349. Tool.Called,
  350. Tool.Progress,
  351. Tool.Success,
  352. Tool.Failed,
  353. Reasoning.Started,
  354. Reasoning.Delta,
  355. Reasoning.Ended,
  356. Retried,
  357. Compaction.Started,
  358. Compaction.Delta,
  359. Compaction.Ended,
  360. ],
  361. {
  362. mode: "oneOf",
  363. },
  364. ).pipe(Schema.toTaggedUnion("type"))
  365. export type Event = typeof All.Type
  366. export type Type = Event["type"]
  367. export * as SessionEvent from "./session-event"