doubao-cli 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/automation.mjs +20 -4
package/package.json
CHANGED
package/src/automation.mjs
CHANGED
|
@@ -46,6 +46,14 @@ async function readFromClient(client) {
|
|
|
46
46
|
return await client.evaluate(READ_MESSAGES_EXPRESSION) || [];
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
export function replyAfterLastUserMessage(messages, message) {
|
|
50
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
51
|
+
if (messages[index].role !== 'user' || messages[index].text !== message) continue;
|
|
52
|
+
return messages.slice(index + 1).find((item) => item.role === 'assistant' && item.text) || null;
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
49
57
|
export async function readConversation(id, options = {}) {
|
|
50
58
|
const timeoutMs = options.timeoutMs || 10_000;
|
|
51
59
|
openConversation(id);
|
|
@@ -66,7 +74,6 @@ export async function sendMessage(id, message, options = {}) {
|
|
|
66
74
|
return withChatClient(async (client) => {
|
|
67
75
|
await waitForConversation(client, id, Math.min(timeoutMs, 15_000));
|
|
68
76
|
const before = await readFromClient(client);
|
|
69
|
-
const assistantCountBefore = before.filter((item) => item.role === 'assistant').length;
|
|
70
77
|
const matchingUserCountBefore = before.filter((item) => item.role === 'user' && item.text === message).length;
|
|
71
78
|
const encodedMessage = JSON.stringify(message);
|
|
72
79
|
|
|
@@ -103,9 +110,18 @@ export async function sendMessage(id, message, options = {}) {
|
|
|
103
110
|
let stablePolls = 0;
|
|
104
111
|
while (Date.now() < deadline) {
|
|
105
112
|
messages = await readFromClient(client);
|
|
106
|
-
const
|
|
107
|
-
const
|
|
108
|
-
|
|
113
|
+
const reply = replyAfterLastUserMessage(messages, message);
|
|
114
|
+
const generating = await client.evaluate(`[
|
|
115
|
+
...document.querySelectorAll('[data-testid="chat_input_local_break_button"], [data-testid="chat_input_end_button"]'),
|
|
116
|
+
].some((element) => {
|
|
117
|
+
const style = getComputedStyle(element);
|
|
118
|
+
const rect = element.getBoundingClientRect();
|
|
119
|
+
return style.display !== 'none'
|
|
120
|
+
&& style.visibility !== 'hidden'
|
|
121
|
+
&& Number(style.opacity) !== 0
|
|
122
|
+
&& rect.width > 0
|
|
123
|
+
&& rect.height > 0;
|
|
124
|
+
})`);
|
|
109
125
|
if (reply?.text && reply.text === stableText && !generating) stablePolls += 1;
|
|
110
126
|
else stablePolls = 0;
|
|
111
127
|
stableText = reply?.text || '';
|