log.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 quesiton: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. }
  99. export type CompletionConversationGeneralDetail = {
  100. id: string
  101. status: 'normal' | 'finished'
  102. from_source: 'api' | 'console'
  103. from_end_user_id: string
  104. from_end_user_session_id: string
  105. from_account_id: string
  106. read_at: Date
  107. created_at: number
  108. annotation: Annotation
  109. user_feedback_stats: {
  110. like: number
  111. dislike: number
  112. }
  113. admin_feedback_stats: {
  114. like: number
  115. dislike: number
  116. }
  117. model_config: {
  118. provider: string
  119. model_id: string
  120. configs: Pick<ModelConfigDetail, 'prompt_template'>
  121. }
  122. message: Pick<MessageContent, 'inputs' | 'query' | 'answer' | 'message'>
  123. }
  124. export type CompletionConversationFullDetailResponse = {
  125. id: string
  126. status: 'normal' | 'finished'
  127. from_source: 'api' | 'console'
  128. from_end_user_id: string
  129. from_account_id: string
  130. // read_at: Date
  131. created_at: number
  132. model_config: {
  133. provider: string
  134. model_id: string
  135. configs: ModelConfigDetail
  136. }
  137. message: MessageContent
  138. }
  139. export type CompletionConversationsResponse = {
  140. data: Array<CompletionConversationGeneralDetail>
  141. has_more: boolean
  142. limit: number
  143. total: number
  144. page: number
  145. }
  146. export type CompletionConversationsRequest = {
  147. keyword: string
  148. start: string
  149. end: string
  150. annotation_status: string
  151. page: number
  152. limit: number // The default value is 20 and the range is 1-100
  153. }
  154. export type ChatConversationGeneralDetail = Omit<CompletionConversationGeneralDetail, 'message' | 'annotation'> & {
  155. summary: string
  156. message_count: number
  157. annotated: boolean
  158. }
  159. export type ChatConversationsResponse = {
  160. data: Array<ChatConversationGeneralDetail>
  161. has_more: boolean
  162. limit: number
  163. total: number
  164. page: number
  165. }
  166. export type ChatConversationsRequest = CompletionConversationsRequest & { message_count: number }
  167. export type ChatConversationFullDetailResponse = Omit<CompletionConversationGeneralDetail, 'message' | 'model_config'> & {
  168. message_count: number
  169. model_config: {
  170. provider: string
  171. model_id: string
  172. configs: ModelConfigDetail
  173. model: LogModelConfig
  174. }
  175. }
  176. export type ChatMessagesRequest = {
  177. conversation_id: string
  178. first_id?: string
  179. limit: number
  180. }
  181. export type ChatMessage = MessageContent
  182. export type ChatMessagesResponse = {
  183. data: Array<ChatMessage>
  184. has_more: boolean
  185. limit: number
  186. }
  187. export const MessageRatings = ['like', 'dislike', null] as const
  188. export type MessageRating = typeof MessageRatings[number]
  189. export type LogMessageFeedbacksRequest = {
  190. message_id: string
  191. rating: MessageRating
  192. content?: string
  193. }
  194. export type LogMessageFeedbacksResponse = {
  195. result: 'success' | 'error'
  196. }
  197. export type LogMessageAnnotationsRequest = Omit<LogMessageFeedbacksRequest, 'rating'>
  198. export type LogMessageAnnotationsResponse = LogMessageFeedbacksResponse
  199. export type AnnotationsCountResponse = {
  200. count: number
  201. }
  202. export type WorkflowRunDetail = {
  203. id: string
  204. version: string
  205. status: 'running' | 'succeeded' | 'failed' | 'stopped'
  206. error?: string
  207. elapsed_time: number
  208. total_tokens: number
  209. total_price: number
  210. currency: string
  211. total_steps: number
  212. finished_at: number
  213. }
  214. export type AccountInfo = {
  215. id: string
  216. name: string
  217. email: string
  218. }
  219. export type EndUserInfo = {
  220. id: string
  221. type: 'browser' | 'service_api'
  222. is_anonymous: boolean
  223. session_id: string
  224. }
  225. export type WorkflowAppLogDetail = {
  226. id: string
  227. workflow_run: WorkflowRunDetail
  228. created_from: 'service-api' | 'web-app' | 'explore'
  229. created_by_role: 'account' | 'end_user'
  230. created_by_account?: AccountInfo
  231. created_by_end_user?: EndUserInfo
  232. created_at: number
  233. read_at?: number
  234. }
  235. export type WorkflowLogsResponse = {
  236. data: Array<WorkflowAppLogDetail>
  237. has_more: boolean
  238. limit: number
  239. total: number
  240. page: number
  241. }
  242. export type WorkflowLogsRequest = {
  243. keyword: string
  244. status: string
  245. page: number
  246. limit: number // The default value is 20 and the range is 1-100
  247. }
  248. export type WorkflowRunDetailResponse = {
  249. id: string
  250. sequence_number: number
  251. version: string
  252. graph: {
  253. nodes: Node[]
  254. edges: Edge[]
  255. viewport?: Viewport
  256. }
  257. inputs: string
  258. status: 'running' | 'succeeded' | 'failed' | 'stopped'
  259. outputs?: string
  260. error?: string
  261. elapsed_time?: number
  262. total_tokens?: number
  263. total_steps: number
  264. created_by_role: 'account' | 'end_user'
  265. created_by_account?: AccountInfo
  266. created_by_end_user?: EndUserInfo
  267. created_at: number
  268. finished_at: number
  269. }
  270. export type AgentLogMeta = {
  271. status: string
  272. executor: string
  273. start_time: string
  274. elapsed_time: number
  275. total_tokens: number
  276. agent_mode: string
  277. iterations: number
  278. error?: string
  279. }
  280. export type ToolCall = {
  281. status: string
  282. error?: string | null
  283. time_cost?: number
  284. tool_icon: any
  285. tool_input?: any
  286. tool_output?: any
  287. tool_name?: string
  288. tool_label?: any
  289. tool_parameters?: any
  290. }
  291. export type AgentIteration = {
  292. created_at: string
  293. files: string[]
  294. thought: string
  295. tokens: number
  296. tool_calls: ToolCall[]
  297. tool_raw: {
  298. inputs: string
  299. outputs: string
  300. }
  301. }
  302. export type AgentLogFile = {
  303. id: string
  304. type: string
  305. url: string
  306. name: string
  307. belongs_to: string
  308. }
  309. export type AgentLogDetailRequest = {
  310. conversation_id: string
  311. message_id: string
  312. }
  313. export type AgentLogDetailResponse = {
  314. meta: AgentLogMeta
  315. iterations: AgentIteration[]
  316. files: AgentLogFile[]
  317. }