forked from coze-dev/coze-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest-options.ts
208 lines (188 loc) · 4.88 KB
/
request-options.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/naming-convention */
import assert from 'assert';
import { ChatEventType, CozeAPI, RoleType } from '@coze/api';
import { botId, baseURL, apiKey } from './client.js';
async function test_timeout_global() {
try {
assert(baseURL, 'baseURL is required');
assert(apiKey, 'apiKey is required');
const client = new CozeAPI({
baseURL,
token: apiKey,
axiosOptions: {
timeout: 100, // timeout in ms
},
debug: true,
});
const result = await client.chat.stream({
bot_id: botId,
user_id: '123',
auto_save_history: true,
additional_messages: [
{
role: RoleType.User,
content: 'give me a joke',
content_type: 'text',
},
],
});
for await (const chunk of result) {
console.log(chunk);
}
} catch (err) {
assert(err instanceof CozeAPI.TimeoutError);
console.log('test_timeout_global', err.name);
}
}
async function test_timeout_with_options() {
try {
assert(baseURL, 'baseURL is required');
assert(apiKey, 'apiKey is required');
const client = new CozeAPI({
baseURL,
token: apiKey,
});
const result = await client.chat.create(
{
bot_id: botId,
user_id: '123',
auto_save_history: true,
additional_messages: [
{
role: RoleType.User,
content: 'Hello',
content_type: 'text',
},
],
},
{
timeout: 10, // timeout in ms
},
);
console.log(result);
} catch (err) {
assert(err instanceof CozeAPI.TimeoutError);
console.log('test_timeout_with_options', err.name);
}
}
async function test_timeout_with_signal() {
try {
assert(baseURL, 'baseURL is required');
assert(apiKey, 'apiKey is required');
const client = new CozeAPI({
baseURL,
token: apiKey,
});
const controller = new AbortController();
setTimeout(() => controller.abort(), 10);
const result = await client.chat.create(
{
bot_id: botId,
user_id: '123',
auto_save_history: true,
additional_messages: [
{
role: RoleType.User,
content: 'Hello',
content_type: 'text',
},
],
},
{
signal: controller.signal,
},
);
} catch (err) {
assert(err instanceof CozeAPI.UserAbortError);
console.log('test_timeout_with_signal', err.name);
}
}
async function test_headers() {
try {
assert(baseURL, 'baseURL is required');
assert(apiKey, 'apiKey is required');
const client = new CozeAPI({
baseURL,
token: apiKey,
debug: true,
});
const result = await client.chat.create(
{
bot_id: botId,
user_id: '123',
auto_save_history: true,
additional_messages: [
{
role: RoleType.User,
content: 'Hello',
content_type: 'text',
},
],
},
{
headers: {
'X-Custom-Header': 'value',
},
},
);
// console.log(result);
} catch (err) {
assert('error');
}
}
async function test_stream_with_signal() {
assert(baseURL, 'baseURL is required');
assert(apiKey, 'apiKey is required');
const client = new CozeAPI({
baseURL,
token: apiKey,
});
const controller = new AbortController();
setTimeout(() => {
console.log('\n=====abort=====');
controller.abort();
}, 1500);
const result = await client.chat.stream(
{
bot_id: botId,
user_id: '123',
auto_save_history: true,
additional_messages: [
{
role: RoleType.User,
content: 'Tell me a story about being in the forest.',
content_type: 'text',
},
],
},
{
signal: controller.signal,
},
);
for await (const chunk of result) {
if (chunk.event === ChatEventType.CONVERSATION_CHAT_CREATED) {
console.log('[START]');
} else if (chunk.event === ChatEventType.CONVERSATION_MESSAGE_DELTA) {
process.stdout.write(chunk.data.content);
} else if (chunk.event === ChatEventType.CONVERSATION_MESSAGE_COMPLETED) {
const { role, type, content } = chunk.data;
if (role === 'assistant' && type === 'answer') {
process.stdout.write('\n');
} else {
console.log('[%s]:[%s]:%s', role, type, content);
}
} else if (chunk.event === ChatEventType.CONVERSATION_CHAT_COMPLETED) {
console.log(chunk.data.usage);
} else if (chunk.event === ChatEventType.DONE) {
console.log(chunk.data);
} else if (chunk.event === ChatEventType.ERROR) {
console.error(chunk.data);
}
}
}
await test_timeout_global();
await test_timeout_with_options();
await test_timeout_with_signal();
await test_headers();
await test_stream_with_signal();