Skip to content

Commit adedf9e

Browse files
authored
chore(core): update type definition for insight (#1323)
* feat(core): update type definition for insight dump * feat(core): update type definition for insight dump * fix(core): error state for waitFor * fix(core): error state for waitFor * fix(core): test cases * fix(core): cases in report
1 parent 548c4ab commit adedf9e

File tree

11 files changed

+42
-157
lines changed

11 files changed

+42
-157
lines changed

apps/report/src/components/sidebar/index.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,11 @@ const SideItem = (props: {
4949
}}
5050
bordered={false}
5151
>
52-
cache
52+
Cache
5353
</Tag>
5454
) : null;
5555

56-
const deepThinkEl = (task as ExecutionTaskInsightLocate)?.log?.dump
57-
?.deepThink ? (
56+
const deepThinkEl = (task as ExecutionTaskInsightLocate)?.param?.deepThink ? (
5857
<Tag
5958
bordered={false}
6059
style={{
@@ -66,7 +65,7 @@ const SideItem = (props: {
6665
lineHeight: '16px',
6766
}}
6867
>
69-
deepthink
68+
DeepThink
7069
</Tag>
7170
) : null;
7271

@@ -83,10 +82,7 @@ const SideItem = (props: {
8382
}
8483

8584
const isAssertFinishedWithWarning =
86-
isFinished &&
87-
task.subType === 'Assert' &&
88-
task.output === false &&
89-
task.log.isWaitForAssert;
85+
isFinished && task.subType === 'WaitFor' && task.output === false;
9086

9187
if (isAssertFinishedWithWarning) {
9288
return iconForStatus('finishedWithWarning');

apps/report/src/components/store/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export const useExecutionDump = create<DumpStoreType>((set, get) => {
204204
});
205205
console.log('will set task', task);
206206
if (task.type === 'Insight') {
207-
const dump = (task as ExecutionTaskInsightLocate).log?.dump!;
207+
const dump = (task as ExecutionTaskInsightLocate).log;
208208
set({
209209
insightDump: dump,
210210
});

packages/core/src/agent/agent.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export type TestStatus =
3636
import yaml from 'js-yaml';
3737

3838
import {
39+
getVersion,
3940
groupedActionDumpFileExt,
4041
processCacheConfig,
4142
reportHTMLContent,
@@ -336,6 +337,7 @@ export class Agent<
336337

337338
resetDump() {
338339
this.dump = {
340+
sdkVersion: getVersion(),
339341
groupName: this.opts.groupName!,
340342
groupDescription: this.opts.groupDescription,
341343
executions: [],
@@ -959,15 +961,19 @@ export class Agent<
959961
screenshotIncluded:
960962
opt?.screenshotIncluded ??
961963
defaultInsightExtractOption.screenshotIncluded,
962-
isWaitForAssert: opt?.isWaitForAssert,
963964
doNotThrowError: opt?.doNotThrowError,
964965
};
965966

966-
const { output, executor, thought } = await this.taskExecutor.assert(
967-
assertion,
968-
modelConfig,
969-
insightOpt,
970-
);
967+
const { textPrompt, multimodalPrompt } = parsePrompt(assertion);
968+
969+
const { output, executor, thought } =
970+
await this.taskExecutor.createTypeQueryExecution<boolean>(
971+
'Assert',
972+
textPrompt,
973+
modelConfig,
974+
insightOpt,
975+
multimodalPrompt,
976+
);
971977
await this.afterTaskRunning(executor, true);
972978

973979
const message = output
@@ -1114,7 +1120,6 @@ export class Agent<
11141120
};
11151121
// 4. build ExecutionDump
11161122
const executionDump: ExecutionDump = {
1117-
sdkVersion: '',
11181123
logTime: now,
11191124
name: `Log - ${title || 'untitled'}`,
11201125
description: opt?.content || '',

packages/core/src/agent/tasks.ts

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ export class TaskExecutor {
864864
}
865865

866866
private createTypeQueryTask(
867-
type: 'Query' | 'Boolean' | 'Number' | 'String' | 'Assert',
867+
type: 'Query' | 'Boolean' | 'Number' | 'String' | 'Assert' | 'WaitFor',
868868
demand: InsightExtractParam,
869869
modelConfig: IModelConfig,
870870
opt?: InsightExtractOption,
@@ -906,10 +906,14 @@ export class TaskExecutor {
906906
const ifTypeRestricted = type !== 'Query';
907907
let demandInput = demand;
908908
let keyOfResult = 'result';
909-
if (ifTypeRestricted && type === 'Assert') {
909+
if (ifTypeRestricted && (type === 'Assert' || type === 'WaitFor')) {
910910
keyOfResult = 'StatementIsTruthy';
911+
const booleanPrompt =
912+
type === 'Assert'
913+
? `Boolean, whether the following statement is true: ${demand}`
914+
: `Boolean, the user wants to do some 'wait for' operation, please check whether the following statement is true: ${demand}`;
911915
demandInput = {
912-
[keyOfResult]: `Boolean, whether the following statement is true: ${demand}`,
916+
[keyOfResult]: booleanPrompt,
913917
};
914918
} else if (ifTypeRestricted) {
915919
demandInput = {
@@ -931,7 +935,7 @@ export class TaskExecutor {
931935
outputResult = data;
932936
} else {
933937
assert(
934-
data?.[keyOfResult] !== undefined,
938+
type !== 'WaitFor' ? data?.[keyOfResult] !== undefined : true,
935939
'No result in query data',
936940
);
937941
outputResult = (data as any)[keyOfResult];
@@ -940,7 +944,7 @@ export class TaskExecutor {
940944

941945
return {
942946
output: outputResult,
943-
log: { dump: insightDump, isWaitForAssert: opt?.isWaitForAssert },
947+
log: insightDump,
944948
usage,
945949
thought,
946950
};
@@ -992,21 +996,6 @@ export class TaskExecutor {
992996
};
993997
}
994998

995-
async assert(
996-
assertion: TUserPrompt,
997-
modelConfig: IModelConfig,
998-
opt?: InsightExtractOption,
999-
): Promise<ExecutionResult<boolean>> {
1000-
const { textPrompt, multimodalPrompt } = parsePrompt(assertion);
1001-
return await this.createTypeQueryExecution<boolean>(
1002-
'Assert',
1003-
textPrompt,
1004-
modelConfig,
1005-
opt,
1006-
multimodalPrompt,
1007-
);
1008-
}
1009-
1010999
private async appendErrorPlan(
10111000
taskExecutor: Executor,
10121001
errorMsg: string,
@@ -1074,15 +1063,13 @@ export class TaskExecutor {
10741063
const overallStartTime = Date.now();
10751064
let startTime = Date.now();
10761065
let errorThought = '';
1077-
let hitError = false;
10781066
while (Date.now() - overallStartTime < timeoutMs) {
10791067
startTime = Date.now();
10801068
const queryTask = await this.createTypeQueryTask(
1081-
'Assert',
1069+
'WaitFor',
10821070
textPrompt,
10831071
modelConfig,
10841072
{
1085-
isWaitForAssert: true,
10861073
doNotThrowError: true,
10871074
},
10881075
multimodalPrompt,
@@ -1096,20 +1083,6 @@ export class TaskExecutor {
10961083
}
10971084
| undefined;
10981085

1099-
// If executor enters error state, stop polling immediately
1100-
if (taskExecutor.isInErrorState()) {
1101-
errorThought =
1102-
taskExecutor.latestErrorTask()?.errorMessage ||
1103-
`Error occurred during waitFor: ${textPrompt}`;
1104-
hitError = true;
1105-
break;
1106-
}
1107-
1108-
if (!result) {
1109-
errorThought = `No result from assertion: ${textPrompt}`;
1110-
break;
1111-
}
1112-
11131086
if (result?.output) {
11141087
return {
11151088
output: undefined,
@@ -1119,6 +1092,7 @@ export class TaskExecutor {
11191092

11201093
errorThought =
11211094
result?.thought ||
1095+
(!result && `No result from assertion: ${textPrompt}`) ||
11221096
`unknown error when waiting for assertion: ${textPrompt}`;
11231097
const now = Date.now();
11241098
if (now - startTime < checkIntervalMs) {
@@ -1128,15 +1102,6 @@ export class TaskExecutor {
11281102
}
11291103
}
11301104

1131-
// If executor is already in error state, don't try to append error plan
1132-
// Just return the executor with existing error information
1133-
if (hitError) {
1134-
return {
1135-
output: undefined,
1136-
executor: taskExecutor,
1137-
};
1138-
}
1139-
11401105
return this.appendErrorPlan(
11411106
taskExecutor,
11421107
`waitFor timeout: ${errorThought}`,

packages/core/src/ai-model/action-executor.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type {
77
ExecutionTaskReturn,
88
ExecutorContext,
99
} from '@/types';
10-
import { getVersion } from '@/utils';
1110
import { assert } from '@midscene/shared/utils';
1211

1312
export class Executor {
@@ -119,6 +118,7 @@ export class Executor {
119118
task.subType === 'Locate' ||
120119
task.subType === 'Query' ||
121120
task.subType === 'Assert' ||
121+
task.subType === 'WaitFor' ||
122122
task.subType === 'Boolean' ||
123123
task.subType === 'Number' ||
124124
task.subType === 'String',
@@ -199,7 +199,6 @@ export class Executor {
199199

200200
dump(): ExecutionDump {
201201
const dumpData: ExecutionDump = {
202-
sdkVersion: getVersion(),
203202
logTime: Date.now(),
204203
name: this.name,
205204
tasks: this.tasks,

packages/core/src/insight/utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import type {
44
InsightDump,
55
PartialInsightDumpFromSDK,
66
} from '@/types';
7-
import { getVersion } from '@/utils';
87
import { uuid } from '@midscene/shared/utils';
98

109
export function emitInsightDump(
1110
data: PartialInsightDumpFromSDK,
1211
dumpSubscriber?: DumpSubscriber,
1312
) {
1413
const baseData: DumpMeta = {
15-
sdkVersion: getVersion(),
1614
logTime: Date.now(),
1715
};
1816
const finalData: InsightDump = {

packages/core/src/types.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ export interface InsightTaskInfo {
170170
}
171171

172172
export interface DumpMeta {
173-
sdkVersion: string;
174173
logTime: number;
175174
}
176175

@@ -200,7 +199,7 @@ export interface InsightDump extends DumpMeta {
200199

201200
export type PartialInsightDumpFromSDK = Omit<
202201
InsightDump,
203-
'sdkVersion' | 'logTime' | 'logId' | 'model_name'
202+
'logTime' | 'logId' | 'model_name'
204203
>;
205204

206205
export type DumpSubscriber = (dump: InsightDump) => Promise<void> | void;
@@ -413,15 +412,13 @@ export interface ExecutionTaskInsightLocateOutput {
413412
element: LocateResultElement | null;
414413
}
415414

416-
export interface ExecutionTaskInsightDumpLog {
417-
dump?: InsightDump;
418-
}
415+
export type ExecutionTaskInsightDump = InsightDump;
419416

420417
export type ExecutionTaskInsightLocateApply = ExecutionTaskApply<
421418
'Insight',
422419
ExecutionTaskInsightLocateParam,
423420
ExecutionTaskInsightLocateOutput,
424-
ExecutionTaskInsightDumpLog
421+
ExecutionTaskInsightDump
425422
>;
426423

427424
export type ExecutionTaskInsightLocate =
@@ -442,7 +439,7 @@ export type ExecutionTaskInsightQueryApply = ExecutionTaskApply<
442439
'Insight',
443440
ExecutionTaskInsightQueryParam,
444441
any,
445-
ExecutionTaskInsightDumpLog
442+
ExecutionTaskInsightDump
446443
>;
447444

448445
export type ExecutionTaskInsightQuery =
@@ -459,7 +456,7 @@ export type ExecutionTaskInsightAssertionApply = ExecutionTaskApply<
459456
'Insight',
460457
ExecutionTaskInsightAssertionParam,
461458
InsightAssertionResponse,
462-
ExecutionTaskInsightDumpLog
459+
ExecutionTaskInsightDump
463460
>;
464461

465462
export type ExecutionTaskInsightAssertion =
@@ -507,6 +504,7 @@ export type ExecutionTaskPlanning = ExecutionTask<ExecutionTaskPlanningApply>;
507504
Grouped dump
508505
*/
509506
export interface GroupedActionDump {
507+
sdkVersion: string;
510508
groupName: string;
511509
groupDescription?: string;
512510
modelBriefs: string[];

packages/core/src/yaml.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ export interface LocateOption {
1313
export interface InsightExtractOption {
1414
domIncluded?: boolean | 'visible-only';
1515
screenshotIncluded?: boolean;
16-
// To make the assert in the "waitfor" section display the warning icon in report
17-
isWaitForAssert?: boolean;
1816
doNotThrowError?: boolean;
1917
}
2018

packages/core/tests/unit-test/executor/index.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type {
66
ExecutionTaskInsightLocateApply,
77
InsightDump,
88
} from '@/index';
9-
import { globalModelConfigManager } from '@midscene/shared/env';
109
import { fakeInsight } from 'tests/utils';
1110
import { describe, expect, it, vi } from 'vitest';
1211

@@ -105,7 +104,7 @@ describe(
105104
expect(tasks.length).toBe(inputTasks.length);
106105
expect(tasks[0].status).toBe('finished');
107106
expect(tasks[0].output).toMatchSnapshot();
108-
expect(tasks[0].log?.dump).toBeTruthy();
107+
expect(tasks[0].log).toBeTruthy();
109108
expect(tasks[0].timing?.end).toBeTruthy();
110109
expect(tasks[0].hitBy?.from).not.toBe('Cache');
111110

packages/visualizer/src/utils/replay-scripts.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,11 @@ export const allScriptsFromDump = (
140140
const dimensionsSet = new Set<string>();
141141
let firstWidth: number | undefined = undefined;
142142
let firstHeight: number | undefined = undefined;
143-
let sdkVersion: string | undefined = undefined;
143+
const sdkVersion: string | undefined = dump.sdkVersion;
144144

145145
const modelBriefsSet = new Set<string>();
146146

147147
dump.executions.forEach((execution) => {
148-
if (execution.sdkVersion) {
149-
sdkVersion = execution.sdkVersion;
150-
}
151-
152148
execution.tasks.forEach((task) => {
153149
if (task.uiContext?.size?.width) {
154150
const w = task.uiContext.size.width;
@@ -339,7 +335,7 @@ export const generateAnimationScripts = (
339335
}
340336
const context = insightTask.uiContext;
341337
if (context?.screenshotBase64) {
342-
const insightDump = insightTask.log?.dump;
338+
const insightDump = insightTask.log;
343339
const insightContentLength = context.tree
344340
? treeToList(context.tree).length
345341
: 0;

0 commit comments

Comments
 (0)