Skip to content

Commit ecebff4

Browse files
committed
fix(agent): abort not break immediately
1 parent 58c80c4 commit ecebff4

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

src/main/agent/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ export class ComputerUseAgent {
226226
logger.info('[vlmParams_conversations]:', vlmParams.conversations);
227227
logger.info('[vlmParams_images_len]:', vlmParams.images.length);
228228

229-
const vlmRes = await vlm.invoke(vlmParams);
229+
const vlmRes = await vlm.invoke(vlmParams, {
230+
abortController,
231+
});
230232

231233
if (!vlmRes?.prediction) {
232234
continue;

src/main/agent/llm/base.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export interface VlmRequest {
99
images: string[];
1010
}
1111

12+
export interface VlmRequestOptions {
13+
abortController?: AbortController | null;
14+
}
15+
1216
export interface VlmResponse {
1317
prediction: string;
1418
reflections?: string[];
@@ -32,5 +36,8 @@ export abstract class VLM<
3236
K extends VlmResponse = VlmResponse,
3337
> {
3438
abstract get vlmModel(): string;
35-
abstract invoke({ conversations, images }: T): Promise<K>;
39+
abstract invoke(
40+
{ conversations, images }: T,
41+
options?: VlmRequestOptions,
42+
): Promise<K>;
3643
}

src/main/agent/llm/ui-tars.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { store } from '@main/store/create';
1010
import { preprocessResizeImage } from '@main/utils/image';
1111

1212
import { MAX_PIXELS } from '../constant';
13-
import { VLM, VlmRequest, VlmResponse } from './base';
13+
import { VLM, VlmRequest, VlmRequestOptions, VlmResponse } from './base';
1414

1515
export interface UITARSOptions {
1616
reflection: boolean;
@@ -31,7 +31,11 @@ export class UITARS implements VLM<VlmRequest, VlmResponse> {
3131

3232
// [image, prompt]
3333
// [gpt, image]
34-
async invoke({ conversations, images }: VlmRequest) {
34+
async invoke(
35+
{ conversations, images }: VlmRequest,
36+
options?: VlmRequestOptions,
37+
) {
38+
const { abortController } = options ?? {};
3539
const compressedImages = await Promise.all(
3640
images.map((image) => preprocessResizeImage(image, MAX_PIXELS)),
3741
);
@@ -51,19 +55,24 @@ export class UITARS implements VLM<VlmRequest, VlmResponse> {
5155

5256
const startTime = Date.now();
5357
const result = await openai.chat.completions
54-
.create({
55-
model: this.vlmModel,
56-
max_tokens: 1000,
57-
stream: false,
58-
temperature: 0,
59-
top_p: 0.7,
60-
seed: null,
61-
stop: null,
62-
frequency_penalty: null,
63-
presence_penalty: null,
64-
// messages
65-
messages,
66-
})
58+
.create(
59+
{
60+
model: this.vlmModel,
61+
max_tokens: 1000,
62+
stream: false,
63+
temperature: 0,
64+
top_p: 0.7,
65+
seed: null,
66+
stop: null,
67+
frequency_penalty: null,
68+
presence_penalty: null,
69+
// messages
70+
messages,
71+
},
72+
{
73+
signal: abortController?.signal,
74+
},
75+
)
6776
.finally(() => {
6877
logger.info(`[vlm_invoke_time_cost]: ${Date.now() - startTime}ms`);
6978
});

src/main/store/create.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { runAgent } from './runAgent';
2121
import { SettingStore } from './setting';
2222
import { AppState } from './types';
2323

24+
const abortController = new AbortController();
25+
2426
export const store = createStore<AppState>(
2527
(set, get) =>
2628
({
@@ -33,7 +35,7 @@ export const store = createStore<AppState>(
3335
getSetting: (key) => SettingStore.get(key),
3436
ensurePermissions: {},
3537

36-
abortController: null,
38+
abortController,
3739
thinking: false,
3840

3941
// dispatch for renderer
@@ -81,7 +83,7 @@ export const store = createStore<AppState>(
8183
},
8284

8385
RUN_AGENT: async () => {
84-
set({ abortController: new AbortController(), thinking: true });
86+
set({ thinking: true });
8587

8688
await runAgent(set, get);
8789

@@ -90,7 +92,8 @@ export const store = createStore<AppState>(
9092
STOP_RUN: () => {
9193
set({ status: StatusEnum.END, thinking: false });
9294
showWindow();
93-
get().abortController?.abort();
95+
abortController.abort();
96+
9497
closeScreenMarker();
9598
},
9699
SET_INSTRUCTIONS: (instructions) => {

0 commit comments

Comments
 (0)