forked from coze-dev/coze-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversation.ts
111 lines (97 loc) · 3.03 KB
/
conversation.ts
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
import assert from 'assert';
import { RoleType } from '@coze/api';
import { botId, client } from './client.js';
// eslint-disable-next-line @typescript-eslint/naming-convention
async function createConversation(bot_id?: string) {
const conversation = await client.conversations.create({
bot_id,
messages: [
{
role: RoleType.Assistant,
content_type: 'text',
content: 'Hi, you are an assistant',
},
{
role: RoleType.User,
content_type: 'object_string',
content: JSON.stringify([
{ type: 'text', text: '123' },
{
type: 'image',
file_url:
'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
} /*, { type: 'file', file_id: '{{file_id_1}}' }*/,
]),
},
],
meta_data: {
a: 'b',
c: 'd',
k: 'z',
},
});
return conversation;
}
async function main() {
const conversation = await createConversation();
console.log('client.conversations.create', conversation);
const conversation2 = await client.conversations.retrieve(conversation.id);
console.log('client.conversations.retrieve', conversation2);
const message = await client.conversations.messages.create(conversation.id, {
content_type: 'object_string',
role: RoleType.User,
content: JSON.stringify([
{ type: 'text', text: '是的方式的是否' },
{
type: 'image',
file_url:
'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
},
]),
meta_data: {
想想: '111',
'🚗': 'eee',
},
});
console.log('client.conversations.messages.create', message);
const message2 = await client.conversations.messages.retrieve(
conversation.id,
message.id,
);
console.log('client.conversations.messages.retrieve', message2);
const updatedMessage = await client.conversations.messages.update(
conversation.id,
message.id,
{
content: '121212121',
content_type: 'text',
meta_data: {
x: '1',
b: '2',
},
},
);
console.log('client.conversations.messages.update', updatedMessage);
const deletedMessage = await client.conversations.messages.delete(
conversation.id,
updatedMessage.id,
);
console.log('client.conversations.messages.delete', deletedMessage);
const messages = await client.conversations.messages.list(conversation.id);
console.log('client.conversations.messages.list', messages);
}
async function withBotId() {
assert(botId, 'botId is required');
const conversation = await createConversation(botId);
console.log('client.conversations.create', conversation);
const list = await client.conversations.list({
bot_id: botId,
page_num: 1,
page_size: 50,
});
console.log('client.conversations.list', list);
const session = await client.conversations.clear(conversation.id);
console.log('client.conversations.clear', session);
}
main().catch(console.error);
withBotId().catch(console.error);