Skip to content

Commit 668a118

Browse files
committed
feat: Add DeleteMessage function to API client
1 parent 727944c commit 668a118

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

client_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ func TestClientReturnsRequestBuilderErrors(t *testing.T) {
348348
{"ModifyMessage", func() (any, error) {
349349
return client.ModifyMessage(ctx, "", "", nil)
350350
}},
351+
{"DeleteMessage", func() (any, error) {
352+
return client.DeleteMessage(ctx, "", "")
353+
}},
351354
{"RetrieveMessageFile", func() (any, error) {
352355
return client.RetrieveMessageFile(ctx, "", "", "")
353356
}},

messages.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ type MessageFilesList struct {
7373
httpHeader
7474
}
7575

76+
type MessageDeletionStatus struct {
77+
ID string `json:"id"`
78+
Object string `json:"object"`
79+
Deleted bool `json:"deleted"`
80+
81+
httpHeader
82+
}
83+
7684
// CreateMessage creates a new message.
7785
func (c *Client) CreateMessage(ctx context.Context, threadID string, request MessageRequest) (msg Message, err error) {
7886
urlSuffix := fmt.Sprintf("/threads/%s/%s", threadID, messagesSuffix)
@@ -186,3 +194,19 @@ func (c *Client) ListMessageFiles(
186194
err = c.sendRequest(req, &files)
187195
return
188196
}
197+
198+
// DeleteMessage deletes a message..
199+
func (c *Client) DeleteMessage(
200+
ctx context.Context,
201+
threadID, messageID string,
202+
) (status MessageDeletionStatus, err error) {
203+
urlSuffix := fmt.Sprintf("/threads/%s/%s/%s", threadID, messagesSuffix, messageID)
204+
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL(urlSuffix),
205+
withBetaAssistantVersion(c.config.AssistantVersion))
206+
if err != nil {
207+
return
208+
}
209+
210+
err = c.sendRequest(req, &status)
211+
return
212+
}

messages_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ func TestMessages(t *testing.T) {
115115
Metadata: nil,
116116
})
117117
fmt.Fprintln(w, string(resBytes))
118+
case http.MethodDelete:
119+
resBytes, _ := json.Marshal(openai.MessageDeletionStatus{
120+
ID: messageID,
121+
Object: "thread.message.deleted",
122+
Deleted: true,
123+
})
124+
fmt.Fprintln(w, string(resBytes))
118125
default:
119126
t.Fatalf("unsupported messages http method: %s", r.Method)
120127
}
@@ -225,6 +232,17 @@ func TestMessages(t *testing.T) {
225232
t.Fatalf("expected message metadata to get modified")
226233
}
227234

235+
msgDel, err := client.DeleteMessage(ctx, threadID, messageID)
236+
checks.NoError(t, err, "DeleteMessage error")
237+
if msgDel.ID != messageID {
238+
t.Fatalf("unexpected message id: '%s'", msg.ID)
239+
}
240+
if !msgDel.Deleted {
241+
t.Fatalf("expected deleted is true")
242+
}
243+
_, err = client.DeleteMessage(ctx, threadID, "not_exist_id")
244+
checks.HasError(t, err, "DeleteMessage error")
245+
228246
// message files
229247
var msgFile openai.MessageFile
230248
msgFile, err = client.RetrieveMessageFile(ctx, threadID, messageID, fileID)

0 commit comments

Comments
 (0)