File size: 3,251 Bytes
87337b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { genUUID } from "./utils"
import { Language } from "@/types"
import axios from "axios"

export interface StartRequestConfig {
  channel: string
  userId: number
  graphName: string
  language: Language
  voiceType: "male" | "female"
  prompt?: string
  greeting?: string
  coze_token?: string
  coze_bot_id?: string
  coze_base_url?: string
  dify_api_key?: string
}

interface GenAgoraDataConfig {
  userId: string | number
  channel: string
}

export const apiGenAgoraData = async (config: GenAgoraDataConfig) => {
  // the request will be rewrite at next.config.mjs to send to $AGENT_SERVER_URL
  const url = `/api/token/generate`
  const { userId, channel } = config
  const data = {
    request_id: genUUID(),
    uid: userId,
    channel_name: channel,
  }
  let resp: any = await axios.post(url, data)
  resp = resp.data || {}
  return resp
}

export const apiStartService = async (
  config: StartRequestConfig,
): Promise<any> => {
  // look at app/api/agents/start/route.tsx for the server-side implementation
  const url = `/api/agents/start`
  const {
    channel,
    userId,
    graphName,
    language,
    voiceType,
    greeting,
    prompt,
    coze_token,
    coze_bot_id,
    coze_base_url,
    dify_api_key,
  } = config
  const data = {
    request_id: genUUID(),
    channel_name: channel,
    user_uid: userId,
    graph_name: graphName,
    language,
    voice_type: voiceType,
    greeting: greeting ?? undefined,
    prompt: prompt ?? undefined,
    coze_token: coze_token ?? undefined,
    coze_bot_id: coze_bot_id ?? undefined,
    coze_base_url: coze_base_url ?? undefined,
    dify_api_key: dify_api_key ?? undefined
  }
  let resp: any = await axios.post(url, data)
  resp = resp.data || {}
  return resp
}

export const apiStopService = async (channel: string) => {
  // the request will be rewrite at middleware.tsx to send to $AGENT_SERVER_URL
  const url = `/api/agents/stop`
  const data = {
    request_id: genUUID(),
    channel_name: channel,
  }
  let resp: any = await axios.post(url, data)
  resp = resp.data || {}
  return resp
}

export const apiGetDocumentList = async () => {
  // the request will be rewrite at middleware.tsx to send to $AGENT_SERVER_URL
  const url = `/api/vector/document/preset/list`
  let resp: any = await axios.get(url)
  resp = resp.data || {}
  if (resp.code !== "0") {
    throw new Error(resp.msg)
  }
  return resp
}

export const apiUpdateDocument = async (options: {
  channel: string
  collection: string
  fileName: string
}) => {
  // the request will be rewrite at middleware.tsx to send to $AGENT_SERVER_URL
  const url = `/api/vector/document/update`
  const { channel, collection, fileName } = options
  const data = {
    request_id: genUUID(),
    channel_name: channel,
    collection: collection,
    file_name: fileName,
  }
  let resp: any = await axios.post(url, data)
  resp = resp.data || {}
  return resp
}

// ping/pong
export const apiPing = async (channel: string) => {
  // the request will be rewrite at middleware.tsx to send to $AGENT_SERVER_URL
  const url = `/api/agents/ping`
  const data = {
    request_id: genUUID(),
    channel_name: channel,
  }
  let resp: any = await axios.post(url, data)
  resp = resp.data || {}
  return resp
}