Skip to content

Commit 3773903

Browse files
committed
feat: Add Speak API for conversational AI service with request and response handling
1 parent ffbec72 commit 3773903

File tree

7 files changed

+147
-0
lines changed

7 files changed

+147
-0
lines changed

examples/convoai/service/service.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,23 @@ func (s *Service) RunWithCustomTTS(ttsVendor req.TTSVendor, ttsParam req.TTSVend
197197

198198
time.Sleep(time.Second * 1)
199199

200+
speakResp, err := convoaiClient.Speak(ctx, agentId, &req.SpeakBody{
201+
Text: agoraUtils.Ptr("Hello, how can I help you?"),
202+
Priority: agoraUtils.Ptr("INTERRUPT"),
203+
Interruptable: agoraUtils.Ptr(true),
204+
})
205+
if err != nil {
206+
log.Fatalln(err)
207+
}
208+
209+
if speakResp.IsSuccess() {
210+
log.Printf("Speak success:%+v", speakResp)
211+
} else {
212+
log.Printf("Speak failed:%+v", speakResp)
213+
return
214+
}
215+
time.Sleep(time.Second * 1)
216+
200217
updateResp, err := convoaiClient.Update(ctx, agentId, &req.UpdateReqBody{
201218
Token: updateToken,
202219
})
File renamed without changes.

services/convoai/api/speak.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package api
2+
3+
import (
4+
"context"
5+
"errors"
6+
"net/http"
7+
8+
"github.com/AgoraIO-Community/agora-rest-client-go/agora"
9+
"github.com/AgoraIO-Community/agora-rest-client-go/agora/client"
10+
"github.com/AgoraIO-Community/agora-rest-client-go/agora/log"
11+
"github.com/AgoraIO-Community/agora-rest-client-go/services/convoai/req"
12+
"github.com/AgoraIO-Community/agora-rest-client-go/services/convoai/resp"
13+
)
14+
15+
type Speak struct {
16+
baseHandler
17+
}
18+
19+
func NewSpeak(module string, logger log.Logger, client client.Client, prefixPath string) *Speak {
20+
return &Speak{
21+
baseHandler: baseHandler{
22+
module: module,
23+
logger: logger,
24+
client: client,
25+
prefixPath: prefixPath,
26+
},
27+
}
28+
}
29+
30+
// buildPath returns the request path.
31+
//
32+
// /api/conversational-ai-agent/v2/projects/{appid}/agents/{agentId}/speak
33+
func (s *Speak) buildPath(agentId string) string {
34+
return s.prefixPath + "/agents/" + agentId + "/speak"
35+
}
36+
37+
func (s *Speak) Do(ctx context.Context, agentId string, body *req.SpeakBody) (*resp.SpeakResp, error) {
38+
path := s.buildPath(agentId)
39+
responseData, err := s.client.DoREST(ctx, path, http.MethodPost, body)
40+
if err != nil {
41+
var internalErr *agora.InternalErr
42+
if !errors.As(err, &internalErr) {
43+
return nil, err
44+
}
45+
}
46+
47+
var speakResp resp.SpeakResp
48+
49+
speakResp.BaseResponse = responseData
50+
51+
if responseData.HttpStatusCode == http.StatusOK {
52+
var successResponse resp.SpeakSuccessResp
53+
if err = responseData.UnmarshalToTarget(&successResponse); err != nil {
54+
return nil, err
55+
}
56+
speakResp.SuccessRes = successResponse
57+
} else {
58+
var errResponse resp.ErrResponse
59+
if err = responseData.UnmarshalToTarget(&errResponse); err != nil {
60+
return nil, err
61+
}
62+
speakResp.ErrResponse = errResponse
63+
}
64+
65+
return &speakResp, nil
66+
}

services/convoai/client.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Client struct {
2525
updateAPI *api.Update
2626
interruptAPI *api.Interrupt
2727
historyAPI *api.History
28+
speakAPI *api.Speak
2829
}
2930

3031
// @brief ServiceRegion represents the region of the Conversational AI engine service
@@ -113,6 +114,7 @@ func NewClient(config *Config) (*Client, error) {
113114
updateAPI: api.NewUpdate("convoai:update", config.Logger, c, prefixPath),
114115
interruptAPI: api.NewInterrupt("convoai:interrupt", config.Logger, c, prefixPath),
115116
historyAPI: api.NewHistory("convoai:history", config.Logger, c, prefixPath),
117+
speakAPI: api.NewSpeak("convoai:speak", config.Logger, c, prefixPath),
116118
}, nil
117119
}
118120

@@ -266,3 +268,22 @@ func (c *Client) Interrupt(ctx context.Context, agentId string) (*resp.Interrupt
266268
func (c *Client) GetHistory(ctx context.Context, agentId string) (*resp.HistoryResp, error) {
267269
return c.historyAPI.Do(ctx, agentId)
268270
}
271+
272+
// Speak
273+
//
274+
// @brief Speaks a custom message for the specified agent instance
275+
//
276+
// @since v0.9.0
277+
//
278+
// @param ctx Context to control the request lifecycle.
279+
//
280+
// @param agentId Agent ID.
281+
//
282+
// @param payload Request body for the specified agent to speak a custom message. See api.SpeakBody for details.
283+
//
284+
// @return Returns the response *SpeakResp. See api.SpeakResp for details.
285+
//
286+
// @return Returns an error object. If the request fails, the error object is not nil and contains error information.
287+
func (c *Client) Speak(ctx context.Context, agentId string, payload *req.SpeakBody) (*resp.SpeakResp, error) {
288+
return c.speakAPI.Do(ctx, agentId, payload)
289+
}

services/convoai/req/speak.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package req
2+
3+
// @brief Defines the request body for the Speak API
4+
//
5+
// @since v0.9.0
6+
type SpeakBody struct {
7+
// The text content to be spoken, with a maximum length of 512 bytes.(Required)
8+
Text *string `json:"text"`
9+
// The priority of the speech behavior, which supports the following values(Optional):
10+
//
11+
// - "INTERRUPT" (default): High priority, interrupt and speak. The agent will terminate the current interaction and speak the message directly.
12+
// - "APPEND": Middle priority, append and speak. The agent will speak the message after the current interaction.
13+
// - "IGNORE": Low priority, speak when idle. If the agent is currently interacting, it will directly ignore and discard the message to be spoken; only when the agent is not interacting will it speak the message.
14+
Priority *string `json:"priority"`
15+
// Whether to allow the user to speak to interrupt the agent's speech(Optional):
16+
//
17+
// - true (default): Allow.
18+
// - false: Disallow.
19+
Interruptable *bool `json:"interrupt"`
20+
}
File renamed without changes.

services/convoai/resp/speak.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package resp
2+
3+
// @brief SpeakResp returned by the Conversational AI engine Speak API
4+
//
5+
// @since v0.9.0
6+
type SpeakResp struct {
7+
// Response returned by the Conversational AI engine API, see Response for details
8+
Response
9+
// Successful response, see SpeakSuccessResp for details
10+
SuccessRes SpeakSuccessResp
11+
}
12+
13+
// @brief Successful response returned by the Conversational AI engine Speak API
14+
//
15+
// @since v0.9.0
16+
type SpeakSuccessResp struct {
17+
// The channel name of the agent
18+
Channel string `json:"channel"`
19+
// Unique identifier of the agent
20+
AgentId string `json:"agent_id"`
21+
// The start timestamp of the agent
22+
StartTs int64 `json:"start_ts"`
23+
}

0 commit comments

Comments
 (0)