log.ts 7.6 KB

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