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