log.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. import type { Viewport } from 'reactflow'
  2. import type { VisionFile } from '@/types/app'
  3. import type {
  4. Edge,
  5. Node,
  6. } from '@/app/components/workflow/types'
  7. // Log type contains key:string conversation_id:string created_at:string quesiton:string answer:string
  8. export type Conversation = {
  9. id: string
  10. key: string
  11. conversationId: string
  12. question: string
  13. answer: string
  14. userRate: number
  15. adminRate: number
  16. }
  17. export type ConversationListResponse = {
  18. logs: Conversation[]
  19. }
  20. export const fetchLogs = (url: string) =>
  21. fetch(url).then<ConversationListResponse>(r => r.json())
  22. export const CompletionParams = ['temperature', 'top_p', 'presence_penalty', 'max_token', 'stop', 'frequency_penalty'] as const
  23. export type CompletionParamType = typeof CompletionParams[number]
  24. export type CompletionParamsType = {
  25. max_tokens: number
  26. temperature: number
  27. top_p: number
  28. stop: string[]
  29. presence_penalty: number
  30. frequency_penalty: number
  31. }
  32. export type LogModelConfig = {
  33. name: string
  34. provider: string
  35. completion_params: CompletionParamsType
  36. }
  37. export type ModelConfigDetail = {
  38. introduction: string
  39. prompt_template: string
  40. prompt_variables: Array<{
  41. key: string
  42. name: string
  43. description: string
  44. type: string | number
  45. default: string
  46. options: string[]
  47. }>
  48. completion_params: CompletionParamsType
  49. }
  50. export type LogAnnotation = {
  51. id: string
  52. content: string
  53. account: {
  54. id: string
  55. name: string
  56. email: string
  57. }
  58. created_at: number
  59. }
  60. export type Annotation = {
  61. id: string
  62. authorName: string
  63. logAnnotation?: LogAnnotation
  64. created_at?: number
  65. }
  66. export type MessageContent = {
  67. id: string
  68. conversation_id: string
  69. query: string
  70. inputs: Record<string, any>
  71. message: { role: string; text: string; files?: VisionFile[] }[]
  72. message_tokens: number
  73. answer_tokens: number
  74. answer: string
  75. provider_response_latency: number
  76. created_at: number
  77. annotation: LogAnnotation
  78. annotation_hit_history: {
  79. annotation_id: string
  80. annotation_create_account: {
  81. id: string
  82. name: string
  83. email: string
  84. }
  85. created_at: number
  86. }
  87. feedbacks: Array<{
  88. rating: 'like' | 'dislike' | null
  89. content: string | null
  90. from_source?: 'admin' | 'user'
  91. from_end_user_id?: string
  92. }>
  93. message_files: VisionFile[]
  94. agent_thoughts: any[] // TODO
  95. workflow_run_id: string
  96. }
  97. export type CompletionConversationGeneralDetail = {
  98. id: string
  99. status: 'normal' | 'finished'
  100. from_source: 'api' | 'console'
  101. from_end_user_id: string
  102. from_end_user_session_id: string
  103. from_account_id: string
  104. read_at: Date
  105. created_at: number
  106. annotation: Annotation
  107. user_feedback_stats: {
  108. like: number
  109. dislike: number
  110. }
  111. admin_feedback_stats: {
  112. like: number
  113. dislike: number
  114. }
  115. model_config: {
  116. provider: string
  117. model_id: string
  118. configs: Pick<ModelConfigDetail, 'prompt_template'>
  119. }
  120. message: Pick<MessageContent, 'inputs' | 'query' | 'answer' | 'message'>
  121. }
  122. export type CompletionConversationFullDetailResponse = {
  123. id: string
  124. status: 'normal' | 'finished'
  125. from_source: 'api' | 'console'
  126. from_end_user_id: string
  127. from_account_id: string
  128. // read_at: Date
  129. created_at: number
  130. model_config: {
  131. provider: string
  132. model_id: string
  133. configs: ModelConfigDetail
  134. }
  135. message: MessageContent
  136. }
  137. export type CompletionConversationsResponse = {
  138. data: Array<CompletionConversationGeneralDetail>
  139. has_more: boolean
  140. limit: number
  141. total: number
  142. page: number
  143. }
  144. export type CompletionConversationsRequest = {
  145. keyword: string
  146. start: string
  147. end: string
  148. annotation_status: string
  149. page: number
  150. limit: number // The default value is 20 and the range is 1-100
  151. }
  152. export type ChatConversationGeneralDetail = Omit<CompletionConversationGeneralDetail, 'message' | 'annotation'> & {
  153. summary: string
  154. message_count: number
  155. annotated: boolean
  156. }
  157. export type ChatConversationsResponse = {
  158. data: Array<ChatConversationGeneralDetail>
  159. has_more: boolean
  160. limit: number
  161. total: number
  162. page: number
  163. }
  164. export type ChatConversationsRequest = CompletionConversationsRequest & { message_count: number }
  165. export type ChatConversationFullDetailResponse = Omit<CompletionConversationGeneralDetail, 'message' | 'model_config'> & {
  166. message_count: number
  167. model_config: {
  168. provider: string
  169. model_id: string
  170. configs: ModelConfigDetail
  171. model: LogModelConfig
  172. }
  173. }
  174. export type ChatMessagesRequest = {
  175. conversation_id: string
  176. first_id?: string
  177. limit: number
  178. }
  179. export type ChatMessage = MessageContent
  180. export type ChatMessagesResponse = {
  181. data: Array<ChatMessage>
  182. has_more: boolean
  183. limit: number
  184. }
  185. export const MessageRatings = ['like', 'dislike', null] as const
  186. export type MessageRating = typeof MessageRatings[number]
  187. export type LogMessageFeedbacksRequest = {
  188. message_id: string
  189. rating: MessageRating
  190. content?: string
  191. }
  192. export type LogMessageFeedbacksResponse = {
  193. result: 'success' | 'error'
  194. }
  195. export type LogMessageAnnotationsRequest = Omit<LogMessageFeedbacksRequest, 'rating'>
  196. export type LogMessageAnnotationsResponse = LogMessageFeedbacksResponse
  197. export type AnnotationsCountResponse = {
  198. count: number
  199. }
  200. export type WorkflowRunDetail = {
  201. id: string
  202. version: string
  203. status: 'running' | 'succeeded' | 'failed' | 'stopped'
  204. error?: string
  205. elapsed_time: number
  206. total_tokens: number
  207. total_price: number
  208. currency: string
  209. total_steps: number
  210. finished_at: number
  211. }
  212. export type AccountInfo = {
  213. id: string
  214. name: string
  215. email: string
  216. }
  217. export type EndUserInfo = {
  218. id: string
  219. type: 'browser' | 'service_api'
  220. is_anonymous: boolean
  221. session_id: string
  222. }
  223. export type WorkflowAppLogDetail = {
  224. id: string
  225. workflow_run: WorkflowRunDetail
  226. created_from: 'service-api' | 'web-app' | 'explore'
  227. created_by_role: 'account' | 'end_user'
  228. created_by_account?: AccountInfo
  229. created_by_end_user?: EndUserInfo
  230. created_at: number
  231. read_at?: number
  232. }
  233. export type WorkflowLogsResponse = {
  234. data: Array<WorkflowAppLogDetail>
  235. has_more: boolean
  236. limit: number
  237. total: number
  238. page: number
  239. }
  240. export type WorkflowLogsRequest = {
  241. keyword: string
  242. status: string
  243. page: number
  244. limit: number // The default value is 20 and the range is 1-100
  245. }
  246. export type WorkflowRunDetailResponse = {
  247. id: string
  248. sequence_number: number
  249. version: string
  250. graph: {
  251. nodes: Node[]
  252. edges: Edge[]
  253. viewport?: Viewport
  254. }
  255. inputs: string
  256. status: 'running' | 'succeeded' | 'failed' | 'stopped'
  257. outputs?: string
  258. error?: string
  259. elapsed_time?: number
  260. total_tokens?: number
  261. total_steps: number
  262. created_by_role: 'account' | 'end_user'
  263. created_by_account?: AccountInfo
  264. created_by_end_user?: EndUserInfo
  265. created_at: number
  266. finished_at: number
  267. }
  268. export type AgentLogMeta = {
  269. status: string
  270. executor: string
  271. start_time: string
  272. elapsed_time: number
  273. total_tokens: number
  274. agent_mode: string
  275. iterations: number
  276. error?: string
  277. }
  278. export type ToolCall = {
  279. status: string
  280. error?: string | null
  281. time_cost?: number
  282. tool_icon: any
  283. tool_input?: any
  284. tool_output?: any
  285. tool_name?: string
  286. tool_label?: any
  287. tool_parameters?: any
  288. }
  289. export type AgentIteration = {
  290. created_at: string
  291. files: string[]
  292. thought: string
  293. tokens: number
  294. tool_calls: ToolCall[]
  295. tool_raw: {
  296. inputs: string
  297. outputs: string
  298. }
  299. }
  300. export type AgentLogFile = {
  301. id: string
  302. type: string
  303. url: string
  304. name: string
  305. belongs_to: string
  306. }
  307. export type AgentLogDetailRequest = {
  308. conversation_id: string
  309. message_id: string
  310. }
  311. export type AgentLogDetailResponse = {
  312. meta: AgentLogMeta
  313. iterations: AgentIteration[]
  314. files: AgentLogFile[]
  315. }