@cotestdev/ai-runner 0.0.5 → 0.0.6
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/dist/agents/logger.d.ts.map +1 -0
- package/dist/agents/logger.js.map +1 -0
- package/dist/agents/playwright-executor.d.ts.map +1 -0
- package/dist/agents/playwright-executor.js.map +1 -0
- package/dist/agents/tools/playwright-backend-adapter.d.ts.map +1 -0
- package/dist/agents/tools/playwright-backend-adapter.js.map +1 -0
- package/dist/agents/types.d.ts.map +1 -0
- package/dist/{core-infra/src → agents}/types.js.map +1 -1
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js.map +1 -0
- package/dist/runner.d.ts.map +1 -0
- package/dist/runner.js.map +1 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/playwright-groups.d.ts.map +1 -0
- package/dist/tools/playwright-groups.js.map +1 -0
- package/dist/types/external.d.ts.map +1 -0
- package/dist/types/external.js.map +1 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +2 -7
- package/playwright.config.ts +38 -0
- package/src/agents/logger.ts +20 -0
- package/src/agents/playwright-executor.ts +252 -0
- package/src/agents/tools/playwright-backend-adapter.ts +134 -0
- package/src/agents/tools/playwright-mcp-types.d.ts +71 -0
- package/src/agents/types.ts +80 -0
- package/src/index.ts +27 -0
- package/src/runner.ts +224 -0
- package/src/tools/index.ts +48 -0
- package/src/tools/playwright-groups.ts +54 -0
- package/src/types/external.ts +7 -0
- package/src/types/index.ts +118 -0
- package/tests/agent/test-heal-agent.spec.ts +54 -0
- package/tsconfig.json +26 -0
- package/dist/ai-runner/src/agents/logger.d.ts.map +0 -1
- package/dist/ai-runner/src/agents/logger.js.map +0 -1
- package/dist/ai-runner/src/agents/playwright-executor.d.ts.map +0 -1
- package/dist/ai-runner/src/agents/playwright-executor.js.map +0 -1
- package/dist/ai-runner/src/agents/tools/playwright-backend-adapter.d.ts.map +0 -1
- package/dist/ai-runner/src/agents/tools/playwright-backend-adapter.js.map +0 -1
- package/dist/ai-runner/src/agents/types.d.ts.map +0 -1
- package/dist/ai-runner/src/agents/types.js.map +0 -1
- package/dist/ai-runner/src/index.d.ts.map +0 -1
- package/dist/ai-runner/src/index.js.map +0 -1
- package/dist/ai-runner/src/runner.d.ts.map +0 -1
- package/dist/ai-runner/src/runner.js.map +0 -1
- package/dist/ai-runner/src/tools/index.d.ts.map +0 -1
- package/dist/ai-runner/src/tools/index.js.map +0 -1
- package/dist/ai-runner/src/tools/playwright-groups.d.ts.map +0 -1
- package/dist/ai-runner/src/tools/playwright-groups.js.map +0 -1
- package/dist/ai-runner/src/types/external.d.ts.map +0 -1
- package/dist/ai-runner/src/types/external.js.map +0 -1
- package/dist/ai-runner/src/types/index.d.ts.map +0 -1
- package/dist/ai-runner/src/types/index.js.map +0 -1
- package/dist/core-infra/src/directory-validator.d.ts +0 -29
- package/dist/core-infra/src/directory-validator.d.ts.map +0 -1
- package/dist/core-infra/src/directory-validator.js +0 -91
- package/dist/core-infra/src/directory-validator.js.map +0 -1
- package/dist/core-infra/src/index.d.ts +0 -14
- package/dist/core-infra/src/index.d.ts.map +0 -1
- package/dist/core-infra/src/index.js +0 -44
- package/dist/core-infra/src/index.js.map +0 -1
- package/dist/core-infra/src/mcp/file-mcp-manager.d.ts +0 -14
- package/dist/core-infra/src/mcp/file-mcp-manager.d.ts.map +0 -1
- package/dist/core-infra/src/mcp/file-mcp-manager.js +0 -46
- package/dist/core-infra/src/mcp/file-mcp-manager.js.map +0 -1
- package/dist/core-infra/src/mcp/index.d.ts +0 -21
- package/dist/core-infra/src/mcp/index.d.ts.map +0 -1
- package/dist/core-infra/src/mcp/index.js +0 -17
- package/dist/core-infra/src/mcp/index.js.map +0 -1
- package/dist/core-infra/src/mcp/mcp-client.d.ts +0 -128
- package/dist/core-infra/src/mcp/mcp-client.d.ts.map +0 -1
- package/dist/core-infra/src/mcp/mcp-client.js +0 -163
- package/dist/core-infra/src/mcp/mcp-client.js.map +0 -1
- package/dist/core-infra/src/mcp/mcp-manager.d.ts +0 -21
- package/dist/core-infra/src/mcp/mcp-manager.d.ts.map +0 -1
- package/dist/core-infra/src/mcp/mcp-manager.js +0 -99
- package/dist/core-infra/src/mcp/mcp-manager.js.map +0 -1
- package/dist/core-infra/src/mcp/playwright-mcp-manager.d.ts +0 -19
- package/dist/core-infra/src/mcp/playwright-mcp-manager.d.ts.map +0 -1
- package/dist/core-infra/src/mcp/playwright-mcp-manager.js +0 -121
- package/dist/core-infra/src/mcp/playwright-mcp-manager.js.map +0 -1
- package/dist/core-infra/src/model.d.ts +0 -13
- package/dist/core-infra/src/model.d.ts.map +0 -1
- package/dist/core-infra/src/model.js +0 -191
- package/dist/core-infra/src/model.js.map +0 -1
- package/dist/core-infra/src/repositories/BaseRepository.d.ts +0 -69
- package/dist/core-infra/src/repositories/BaseRepository.d.ts.map +0 -1
- package/dist/core-infra/src/repositories/BaseRepository.js +0 -213
- package/dist/core-infra/src/repositories/BaseRepository.js.map +0 -1
- package/dist/core-infra/src/repositories/DirectoryRepository.d.ts +0 -70
- package/dist/core-infra/src/repositories/DirectoryRepository.d.ts.map +0 -1
- package/dist/core-infra/src/repositories/DirectoryRepository.js +0 -336
- package/dist/core-infra/src/repositories/DirectoryRepository.js.map +0 -1
- package/dist/core-infra/src/repositories/ExplorationRepository.d.ts +0 -34
- package/dist/core-infra/src/repositories/ExplorationRepository.d.ts.map +0 -1
- package/dist/core-infra/src/repositories/ExplorationRepository.js +0 -54
- package/dist/core-infra/src/repositories/ExplorationRepository.js.map +0 -1
- package/dist/core-infra/src/repositories/FileRepository.d.ts +0 -56
- package/dist/core-infra/src/repositories/FileRepository.d.ts.map +0 -1
- package/dist/core-infra/src/repositories/FileRepository.js +0 -132
- package/dist/core-infra/src/repositories/FileRepository.js.map +0 -1
- package/dist/core-infra/src/repositories/ModelConfigRepository.d.ts +0 -38
- package/dist/core-infra/src/repositories/ModelConfigRepository.d.ts.map +0 -1
- package/dist/core-infra/src/repositories/ModelConfigRepository.js +0 -59
- package/dist/core-infra/src/repositories/ModelConfigRepository.js.map +0 -1
- package/dist/core-infra/src/repositories/ProjectRepository.d.ts +0 -32
- package/dist/core-infra/src/repositories/ProjectRepository.d.ts.map +0 -1
- package/dist/core-infra/src/repositories/ProjectRepository.js +0 -67
- package/dist/core-infra/src/repositories/ProjectRepository.js.map +0 -1
- package/dist/core-infra/src/repositories/SettingsRepository.d.ts +0 -19
- package/dist/core-infra/src/repositories/SettingsRepository.d.ts.map +0 -1
- package/dist/core-infra/src/repositories/SettingsRepository.js +0 -72
- package/dist/core-infra/src/repositories/SettingsRepository.js.map +0 -1
- package/dist/core-infra/src/repositories/TableDataRepository.d.ts +0 -22
- package/dist/core-infra/src/repositories/TableDataRepository.d.ts.map +0 -1
- package/dist/core-infra/src/repositories/TableDataRepository.js +0 -33
- package/dist/core-infra/src/repositories/TableDataRepository.js.map +0 -1
- package/dist/core-infra/src/repositories/TestCaseRepository.d.ts +0 -120
- package/dist/core-infra/src/repositories/TestCaseRepository.d.ts.map +0 -1
- package/dist/core-infra/src/repositories/TestCaseRepository.js +0 -463
- package/dist/core-infra/src/repositories/TestCaseRepository.js.map +0 -1
- package/dist/core-infra/src/repositories/TestPlanRepository.d.ts +0 -35
- package/dist/core-infra/src/repositories/TestPlanRepository.d.ts.map +0 -1
- package/dist/core-infra/src/repositories/TestPlanRepository.js +0 -80
- package/dist/core-infra/src/repositories/TestPlanRepository.js.map +0 -1
- package/dist/core-infra/src/repositories/TestResultRepository.d.ts +0 -30
- package/dist/core-infra/src/repositories/TestResultRepository.d.ts.map +0 -1
- package/dist/core-infra/src/repositories/TestResultRepository.js +0 -54
- package/dist/core-infra/src/repositories/TestResultRepository.js.map +0 -1
- package/dist/core-infra/src/repositories/index.d.ts +0 -17
- package/dist/core-infra/src/repositories/index.d.ts.map +0 -1
- package/dist/core-infra/src/repositories/index.js +0 -31
- package/dist/core-infra/src/repositories/index.js.map +0 -1
- package/dist/core-infra/src/storageService.d.ts +0 -131
- package/dist/core-infra/src/storageService.d.ts.map +0 -1
- package/dist/core-infra/src/storageService.js +0 -304
- package/dist/core-infra/src/storageService.js.map +0 -1
- package/dist/core-infra/src/types.d.ts +0 -219
- package/dist/core-infra/src/types.d.ts.map +0 -1
- package/dist/core-infra/src/types.js +0 -3
- /package/dist/{ai-runner/src/agents → agents}/logger.d.ts +0 -0
- /package/dist/{ai-runner/src/agents → agents}/logger.js +0 -0
- /package/dist/{ai-runner/src/agents → agents}/playwright-executor.d.ts +0 -0
- /package/dist/{ai-runner/src/agents → agents}/playwright-executor.js +0 -0
- /package/dist/{ai-runner/src/agents → agents}/tools/playwright-backend-adapter.d.ts +0 -0
- /package/dist/{ai-runner/src/agents → agents}/tools/playwright-backend-adapter.js +0 -0
- /package/dist/{ai-runner/src/agents → agents}/types.d.ts +0 -0
- /package/dist/{ai-runner/src/agents → agents}/types.js +0 -0
- /package/dist/{ai-runner/src/index.d.ts → index.d.ts} +0 -0
- /package/dist/{ai-runner/src/index.js → index.js} +0 -0
- /package/dist/{ai-runner/src/runner.d.ts → runner.d.ts} +0 -0
- /package/dist/{ai-runner/src/runner.js → runner.js} +0 -0
- /package/dist/{ai-runner/src/tools → tools}/index.d.ts +0 -0
- /package/dist/{ai-runner/src/tools → tools}/index.js +0 -0
- /package/dist/{ai-runner/src/tools → tools}/playwright-groups.d.ts +0 -0
- /package/dist/{ai-runner/src/tools → tools}/playwright-groups.js +0 -0
- /package/dist/{ai-runner/src/types → types}/external.d.ts +0 -0
- /package/dist/{ai-runner/src/types → types}/external.js +0 -0
- /package/dist/{ai-runner/src/types → types}/index.d.ts +0 -0
- /package/dist/{ai-runner/src/types → types}/index.js +0 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Playwright MCP 模块类型定义
|
|
3
|
+
*
|
|
4
|
+
* 为动态加载的 Playwright 内部模块提供 TypeScript 类型支持
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { BrowserContext } from 'playwright';
|
|
8
|
+
|
|
9
|
+
export interface BrowserContextFactory {
|
|
10
|
+
createContext: (
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
|
+
clientInfo: any,
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
abortSignal: any,
|
|
15
|
+
toolName: string
|
|
16
|
+
) => Promise<{
|
|
17
|
+
browserContext: BrowserContext;
|
|
18
|
+
close: () => Promise<void>;
|
|
19
|
+
}>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface BackendConfig {
|
|
23
|
+
capabilities?: string[];
|
|
24
|
+
saveSession?: boolean;
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
+
[key: string]: any;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface BrowserServerBackendInstance {
|
|
30
|
+
initialize(clientInfo: { name: string; version: string }): Promise<void>;
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
+
listTools(): Promise<Array<{ name: string; description: string; inputSchema: any }>>;
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
34
|
+
callTool(name: string, params: any): Promise<any>;
|
|
35
|
+
serverClosed(): void;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface BrowserServerBackend {
|
|
39
|
+
new (config: BackendConfig, factory: BrowserContextFactory): BrowserServerBackendInstance;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface DefaultConfig {
|
|
43
|
+
capabilities?: string[];
|
|
44
|
+
saveSession?: boolean;
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
46
|
+
[key: string]: any;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 官方的 identityFactory 函数类型
|
|
51
|
+
* 参考:playwright/lib/mcp/test/browserBackend.js 第 94-104 行
|
|
52
|
+
*/
|
|
53
|
+
export type IdentityFactory = (browserContext: BrowserContext) => BrowserContextFactory;
|
|
54
|
+
|
|
55
|
+
export interface MCPTool {
|
|
56
|
+
schema: {
|
|
57
|
+
name: string;
|
|
58
|
+
description: string;
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
60
|
+
inputSchema: any;
|
|
61
|
+
};
|
|
62
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
63
|
+
handle: (context: any, params: any, response: any) => Promise<void>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 动态加载的模块类型
|
|
67
|
+
export interface PlaywrightMCPModules {
|
|
68
|
+
BrowserServerBackend: BrowserServerBackend;
|
|
69
|
+
defaultConfig: DefaultConfig;
|
|
70
|
+
identityFactory: IdentityFactory;
|
|
71
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Playwright Agent Type Definitions
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { ModelConfig } from '../types/external';
|
|
6
|
+
import type { ReActStep } from '../types';
|
|
7
|
+
import { BrowserContext, Page } from '@playwright/test';
|
|
8
|
+
import { Logger } from './logger';
|
|
9
|
+
import { PlaywrightGroup } from '../tools/playwright-groups';
|
|
10
|
+
import { PlaywrightBackendAdapter } from './tools/playwright-backend-adapter';
|
|
11
|
+
import { StorageService } from '@cotestdev/core-infra';
|
|
12
|
+
import { BaseLanguageModel } from '@langchain/core/language_models/base';
|
|
13
|
+
|
|
14
|
+
// ============================================================================
|
|
15
|
+
// Agent Execution Mode
|
|
16
|
+
// ============================================================================
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Agent execution mode
|
|
20
|
+
*/
|
|
21
|
+
export type AgentMode = 'heal' | 'execute';
|
|
22
|
+
|
|
23
|
+
// ============================================================================
|
|
24
|
+
// Agent State
|
|
25
|
+
// ============================================================================
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Playwright Agent State
|
|
29
|
+
*/
|
|
30
|
+
export interface PlaywrightAgentState {
|
|
31
|
+
logger: Logger;
|
|
32
|
+
/** Playwright Page object */
|
|
33
|
+
page: Page;
|
|
34
|
+
/** Playwright BrowserContext object */
|
|
35
|
+
context: BrowserContext;
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
+
variables: Map<string, any>;
|
|
38
|
+
model?: BaseLanguageModel;
|
|
39
|
+
originalScript: string;
|
|
40
|
+
playwrightGroup: PlaywrightGroup;
|
|
41
|
+
adapter: PlaywrightBackendAdapter;
|
|
42
|
+
storageService: StorageService;
|
|
43
|
+
testCaseId: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ============================================================================
|
|
47
|
+
// Agent Configuration
|
|
48
|
+
// ============================================================================
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Playwright Agent Configuration
|
|
52
|
+
*/
|
|
53
|
+
export interface PlaywrightAgentConfig {
|
|
54
|
+
/** Model configuration (for LLM Agent) */
|
|
55
|
+
model: ModelConfig;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ============================================================================
|
|
59
|
+
// Agent Result
|
|
60
|
+
// ============================================================================
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Playwright Agent Execution Result
|
|
64
|
+
*/
|
|
65
|
+
export interface PlaywrightAgentResult {
|
|
66
|
+
/** Whether the goal was successfully completed */
|
|
67
|
+
success: boolean;
|
|
68
|
+
|
|
69
|
+
/** Agent's reasoning process */
|
|
70
|
+
reasoning?: string;
|
|
71
|
+
|
|
72
|
+
/** If failed, the error reason */
|
|
73
|
+
errorMessage?: string;
|
|
74
|
+
|
|
75
|
+
/** How many steps were executed */
|
|
76
|
+
steps?: number;
|
|
77
|
+
|
|
78
|
+
/** Detailed step records of ReAct loop */
|
|
79
|
+
reactSteps?: ReActStep[];
|
|
80
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ai-runner SDK Main Entry Point
|
|
3
|
+
* Provides AI-driven test execution capabilities
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Types
|
|
7
|
+
export type {
|
|
8
|
+
RunnerConfig,
|
|
9
|
+
ExecutionResult,
|
|
10
|
+
HealContext,
|
|
11
|
+
HealSummary,
|
|
12
|
+
HealingDetail,
|
|
13
|
+
RunStepOptions,
|
|
14
|
+
} from './types';
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
// Core Classes
|
|
18
|
+
export { Runner } from './runner';
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
// Agent
|
|
22
|
+
export type {
|
|
23
|
+
PlaywrightAgentState,
|
|
24
|
+
PlaywrightAgentResult,
|
|
25
|
+
PlaywrightAgentConfig,
|
|
26
|
+
} from './agents/types';
|
|
27
|
+
|
package/src/runner.ts
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import type {
|
|
3
|
+
RunnerConfig,
|
|
4
|
+
HealContext,
|
|
5
|
+
} from './types';
|
|
6
|
+
import type { Page, BrowserContext } from 'playwright';
|
|
7
|
+
import { createModel, StorageService } from '@cotestdev/core-infra';
|
|
8
|
+
import { expect } from '@playwright/test';
|
|
9
|
+
import { PlaywrightAgentState } from './agents/types';
|
|
10
|
+
import { Logger } from './agents/logger';
|
|
11
|
+
import { PlaywrightExecutor } from './agents/playwright-executor';
|
|
12
|
+
import { PlaywrightGroup } from './tools/playwright-groups';
|
|
13
|
+
import { PlaywrightBackendAdapter } from './agents/tools/playwright-backend-adapter';
|
|
14
|
+
import { BaseLanguageModel } from '@langchain/core/language_models/base';
|
|
15
|
+
|
|
16
|
+
export class Runner {
|
|
17
|
+
private config: RunnerConfig;
|
|
18
|
+
private model?: BaseLanguageModel;
|
|
19
|
+
private storageService: StorageService;
|
|
20
|
+
private logger: Logger;
|
|
21
|
+
private canHeal: boolean = false;
|
|
22
|
+
private executor: PlaywrightExecutor | undefined;
|
|
23
|
+
private state: PlaywrightAgentState | undefined;
|
|
24
|
+
private script?: string;
|
|
25
|
+
private variables: Map<string, any> = new Map<string, any>();
|
|
26
|
+
|
|
27
|
+
private constructor(config: RunnerConfig) {
|
|
28
|
+
this.config = config;
|
|
29
|
+
this.storageService = StorageService.getInstance(config.projectId);
|
|
30
|
+
this.logger = new Logger();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static NewInstance(projectId: string, testCaseId: string): Runner {
|
|
34
|
+
const runner = new Runner({ projectId, testCaseId });
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
const script = runner.loadOriginalScript();
|
|
38
|
+
if (!script) {
|
|
39
|
+
runner.logger.error('No script found for the specified test case.');
|
|
40
|
+
return runner;
|
|
41
|
+
}
|
|
42
|
+
runner.script = script;
|
|
43
|
+
|
|
44
|
+
// 同步加载模型配置
|
|
45
|
+
const allConfigs = runner.storageService.listModelConfigsSync();
|
|
46
|
+
const config = allConfigs.find(c => c.isDefault === true);
|
|
47
|
+
if (config) {
|
|
48
|
+
runner.model = createModel(config);
|
|
49
|
+
runner.canHeal = true;
|
|
50
|
+
} else {
|
|
51
|
+
runner.logger.error('No default model configuration available in the system.');
|
|
52
|
+
}
|
|
53
|
+
} catch (error) {
|
|
54
|
+
runner.logger.error(`Failed to initialize runner: ${error}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return runner;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async init(page: Page, context: BrowserContext, data?: Record<string, any>): Promise<void> {
|
|
61
|
+
const adapter = await PlaywrightBackendAdapter.create(context);
|
|
62
|
+
this.state = {
|
|
63
|
+
page,
|
|
64
|
+
context,
|
|
65
|
+
variables: this.variables,
|
|
66
|
+
logger: this.logger,
|
|
67
|
+
model: this.model,
|
|
68
|
+
originalScript: this.state?.originalScript || this.script || '',
|
|
69
|
+
adapter: adapter,
|
|
70
|
+
playwrightGroup: new PlaywrightGroup(adapter),
|
|
71
|
+
testCaseId: this.config.testCaseId,
|
|
72
|
+
storageService: this.storageService,
|
|
73
|
+
};
|
|
74
|
+
if (data) {
|
|
75
|
+
for (const [key, value] of Object.entries(data)) {
|
|
76
|
+
this.set(key, value);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
this.executor = new PlaywrightExecutor(this.state);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private loadOriginalScript(): string {
|
|
83
|
+
if (!this.config.testCaseId) {
|
|
84
|
+
throw new Error('Cannot load original script: testCaseId is not provided');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
this.logger.log(`Attempting to load script from database: ${this.config.projectId}/${this.config.testCaseId}`);
|
|
88
|
+
const script = this.storageService.loadTestScript(this.config.testCaseId);
|
|
89
|
+
if (!script) {
|
|
90
|
+
throw new Error(`Cannot load original script: Test case not found in database (ID: ${this.config.testCaseId})`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return script;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
forEach(data: Record<string, any>[], callback: (index: number) => void) {
|
|
97
|
+
for (const [index, item] of data.entries()) {
|
|
98
|
+
for (const [key, value] of Object.entries(item)) {
|
|
99
|
+
this.set(key, value);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
callback(index);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async runStep(description: string, fn: () => Promise<void>) {
|
|
107
|
+
const startTime = Date.now();
|
|
108
|
+
const context: HealContext = {
|
|
109
|
+
stepDescription: description,
|
|
110
|
+
originalCode: fn.toString(),
|
|
111
|
+
success: false
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
await fn();
|
|
116
|
+
context.success = true;
|
|
117
|
+
return;
|
|
118
|
+
} catch (error) {
|
|
119
|
+
if (!this.canHeal || !(error instanceof Error)) {
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const healable = this.isHealableError(error);
|
|
124
|
+
if (!healable) {
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
context.error = (error as Error).message;
|
|
129
|
+
context.stackTrace = (error as Error).stack;
|
|
130
|
+
|
|
131
|
+
this.logger.error(`Step failed: ${description}, Error: ${context.error}`);
|
|
132
|
+
this.logger.log(`Agent will attempt to self-heal...`);
|
|
133
|
+
|
|
134
|
+
await this.executor!.execute(context);
|
|
135
|
+
|
|
136
|
+
const duration = (Date.now() - startTime) / 1000;
|
|
137
|
+
if (context.success) {
|
|
138
|
+
this.logger.log(`Test step healed, duration: ${duration}s`);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
this.logger.error(`Failed to fix step: ${context.healError}, duration: ${duration}s`);
|
|
143
|
+
throw new Error(`original error: ${context.error}; heal error: ${context.healError}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private isHealableError(err: Error): boolean {
|
|
148
|
+
// 方式1:枚举所有 Symbol 属性(最常见尝试)
|
|
149
|
+
const symbols = Object.getOwnPropertySymbols(err);
|
|
150
|
+
|
|
151
|
+
for (const sym of symbols) {
|
|
152
|
+
try {
|
|
153
|
+
const value = (err as any)[sym];
|
|
154
|
+
|
|
155
|
+
// 如果有 category 字段,大概率就是我们要找的 step
|
|
156
|
+
if (value && typeof value === 'object' && 'category' in value) {
|
|
157
|
+
if (value.category === 'expect' || value.category === 'pw:api') {
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
} catch (e) {
|
|
162
|
+
// 访问可能会抛错,忽略即可
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
set(key: string, value: any): void {
|
|
170
|
+
this.variables.set(key, value);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
get(key: string): any {
|
|
174
|
+
return this.variables.get(key);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
has(key: string): boolean {
|
|
178
|
+
return this.variables.has(key);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
delete(key: string): boolean {
|
|
182
|
+
return this.variables.delete(key);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
clear(): void {
|
|
186
|
+
this.variables.clear();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
keys(): string[] {
|
|
190
|
+
return Array.from(this.variables.keys());
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
getAll(): Record<string, unknown> {
|
|
194
|
+
return Object.fromEntries(this.variables);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async reuseTest(useTestCaseId: string, description: string): Promise<Map<string, unknown>> {
|
|
198
|
+
const scriptContent = this.storageService.loadTestScript(useTestCaseId);
|
|
199
|
+
if (!scriptContent) {
|
|
200
|
+
throw new Error(`Reusable test script not found: ${useTestCaseId}`);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const reuseRunner = Runner.NewInstance(this.config.projectId, useTestCaseId);
|
|
204
|
+
await reuseRunner.init(this.state!.page, this.state!.context);
|
|
205
|
+
|
|
206
|
+
this.logger.log(`Starting to execute reusable test case: ${description}`);
|
|
207
|
+
|
|
208
|
+
try {
|
|
209
|
+
const executeScript = new Function('runner', 'page', 'context', 'expect', `
|
|
210
|
+
return (async () => {
|
|
211
|
+
${scriptContent}
|
|
212
|
+
})();
|
|
213
|
+
`);
|
|
214
|
+
|
|
215
|
+
await executeScript(reuseRunner, this.state!.page, this.state!.context, expect);
|
|
216
|
+
|
|
217
|
+
this.logger.log(`Reusable test case executed successfully`);
|
|
218
|
+
|
|
219
|
+
return reuseRunner.state?.variables || new Map<string, unknown>();
|
|
220
|
+
} catch (error) {
|
|
221
|
+
throw new Error(`reuseTest execution failed, error: ${error instanceof Error ? error.message : String(error)}`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { DynamicStructuredTool } from "langchain";
|
|
2
|
+
|
|
3
|
+
export interface ToolGroup {
|
|
4
|
+
name: string;
|
|
5
|
+
description: string;
|
|
6
|
+
tools?: string[];
|
|
7
|
+
loadByDefault: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export abstract class AbstractGroup {
|
|
11
|
+
groups: ToolGroup[];
|
|
12
|
+
|
|
13
|
+
constructor(groups: ToolGroup[]) {
|
|
14
|
+
this.groups = groups;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
abstract getAllTools(): DynamicStructuredTool[];
|
|
18
|
+
|
|
19
|
+
getDefaultTools(): DynamicStructuredTool[] {
|
|
20
|
+
return this.getAllTools().filter(tool => {
|
|
21
|
+
return this.groups.some(group => group.loadByDefault && (!group.tools || group.tools.includes(tool.name)));
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
getAvailableTools(groupName: string): DynamicStructuredTool[] {
|
|
26
|
+
const group = this.groups.find(g => g.name === groupName);
|
|
27
|
+
if (group) {
|
|
28
|
+
const toolNames = group.tools;
|
|
29
|
+
const all = this.getAllTools();
|
|
30
|
+
if (!toolNames) {
|
|
31
|
+
return all;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return toolNames.filter(name => all.some(tool => tool.name === name)).map(name => all.find(tool => tool.name === name)!);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
throw new Error(`Tool group ${groupName} not found`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
getNonDefaultGroupInfo(): Record<string, string> {
|
|
41
|
+
return this.groups
|
|
42
|
+
.filter(group => !group.loadByDefault)
|
|
43
|
+
.reduce((acc, group) => {
|
|
44
|
+
acc[group.name] = group.description;
|
|
45
|
+
return acc;
|
|
46
|
+
}, {} as Record<string, string>);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
/**
|
|
3
|
+
* MCP (Model Context Protocol) Client for Playwright integration
|
|
4
|
+
* Integrates with https://github.com/microsoft/playwright-mcp
|
|
5
|
+
*/
|
|
6
|
+
import { DynamicStructuredTool } from 'langchain';
|
|
7
|
+
import { AbstractGroup } from '.';
|
|
8
|
+
import { PlaywrightBackendAdapter } from 'src/agents/tools/playwright-backend-adapter';
|
|
9
|
+
export interface PlaywrightMCPOpts {
|
|
10
|
+
language: string,
|
|
11
|
+
headless?: boolean,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class PlaywrightGroup extends AbstractGroup {
|
|
15
|
+
private allTools: DynamicStructuredTool[];
|
|
16
|
+
private adapter: PlaywrightBackendAdapter;
|
|
17
|
+
|
|
18
|
+
constructor(adapter: PlaywrightBackendAdapter) {
|
|
19
|
+
super([{
|
|
20
|
+
name: 'playwright_basic',
|
|
21
|
+
description: 'Basic Playwright tools for browser interactions',
|
|
22
|
+
tools: ['browser_navigate', 'browser_click', 'browser_fill_form', 'browser_press_key', 'browser_run_code', 'browser_select_option', 'browser_snapshot', 'browser_type', 'browser_wait_for'],
|
|
23
|
+
loadByDefault: true,
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: 'playwright_browser_manage',
|
|
27
|
+
description: `Playwright tools tab management, including: list, create, close, select a tab`,
|
|
28
|
+
tools: ['browser_tabs'],
|
|
29
|
+
loadByDefault: false,
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: 'playwright_other',
|
|
33
|
+
description: `Other Playwright tools for browser interactions, including: mouse hover on element, drag and drop an element, upload file, handle dialog, evaluate JavaScript on page or element
|
|
34
|
+
1. mouse hover on element
|
|
35
|
+
2. drag and drop an element
|
|
36
|
+
3. upload file
|
|
37
|
+
4. handle dialog
|
|
38
|
+
5. evaluate JavaScript on page or element`,
|
|
39
|
+
tools: ['browser_hover', 'browser_drag', 'browser_file_upload', 'browser_handle_dialog', 'browser_evaluate'],
|
|
40
|
+
loadByDefault: false,
|
|
41
|
+
}
|
|
42
|
+
]);
|
|
43
|
+
this.adapter = adapter;
|
|
44
|
+
this.allTools = this.adapter.getTools();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async callTool(toolName: string, args: any): Promise<any> {
|
|
48
|
+
return this.adapter.callTool(toolName, args);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getAllTools(): DynamicStructuredTool[] {
|
|
52
|
+
return this.allTools
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ai-heal Core Type Definitions (MVP)
|
|
3
|
+
* Maintain simplicity, include only necessary types
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Playwright types are used in type definitions below
|
|
7
|
+
|
|
8
|
+
export interface RunnerConfig {
|
|
9
|
+
/** Test Case ID */
|
|
10
|
+
testCaseId: string;
|
|
11
|
+
/** Project ID */
|
|
12
|
+
projectId: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// ============================================================================
|
|
16
|
+
// Execution Context
|
|
17
|
+
// ============================================================================
|
|
18
|
+
|
|
19
|
+
export interface RunStepOptions {
|
|
20
|
+
/** Step description for AI to understand */
|
|
21
|
+
description: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ReActStep {
|
|
25
|
+
/** Step number */
|
|
26
|
+
stepNumber: number;
|
|
27
|
+
/** Step type */
|
|
28
|
+
stepType: 'thought' | 'action' | 'observation' | 'reflection';
|
|
29
|
+
/** Timestamp */
|
|
30
|
+
timestamp: number;
|
|
31
|
+
/** LLM thought process (thought step) */
|
|
32
|
+
thought?: string;
|
|
33
|
+
/** Tool call information (action step) */
|
|
34
|
+
toolCall?: {
|
|
35
|
+
name: string;
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
+
input: any;
|
|
38
|
+
};
|
|
39
|
+
/** Tool execution result (observation step) */
|
|
40
|
+
toolResult?: {
|
|
41
|
+
output: string;
|
|
42
|
+
success: boolean;
|
|
43
|
+
duration?: number;
|
|
44
|
+
};
|
|
45
|
+
/** Reflection content (reflection step) */
|
|
46
|
+
reflection?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Context information for a single self-healing process
|
|
51
|
+
*/
|
|
52
|
+
export interface HealContext {
|
|
53
|
+
/** Step description */
|
|
54
|
+
stepDescription: string;
|
|
55
|
+
/** original error */
|
|
56
|
+
error?: string;
|
|
57
|
+
/** Stack trace */
|
|
58
|
+
stackTrace?: string;
|
|
59
|
+
/** Original code */
|
|
60
|
+
originalCode: string;
|
|
61
|
+
/** Whether successful */
|
|
62
|
+
success: boolean;
|
|
63
|
+
/** Total steps executed by Agent */
|
|
64
|
+
agentSteps?: number;
|
|
65
|
+
healError?: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ============================================================================
|
|
69
|
+
// Execution Result
|
|
70
|
+
// ============================================================================
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Code execution result
|
|
74
|
+
*/
|
|
75
|
+
export interface ExecutionResult {
|
|
76
|
+
/** Whether successful */
|
|
77
|
+
success: boolean;
|
|
78
|
+
/** Return value */
|
|
79
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
80
|
+
returnValue: any;
|
|
81
|
+
/** Local variables */
|
|
82
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
83
|
+
locals: Record<string, any>;
|
|
84
|
+
/** Error message */
|
|
85
|
+
error?: Error;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ============================================================================
|
|
89
|
+
// Self-Healing Summary
|
|
90
|
+
// ============================================================================
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Self-healing summary information
|
|
94
|
+
*/
|
|
95
|
+
export interface HealSummary {
|
|
96
|
+
/** Total number of steps */
|
|
97
|
+
totalSteps: number;
|
|
98
|
+
/** Number of successfully healed steps */
|
|
99
|
+
healedSteps: number;
|
|
100
|
+
/** Fixed script */
|
|
101
|
+
modifiedScript?: string;
|
|
102
|
+
/** List of self-healing details */
|
|
103
|
+
healingDetails: HealingDetail[];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Single self-healing detail
|
|
108
|
+
*/
|
|
109
|
+
export interface HealingDetail {
|
|
110
|
+
/** Step description */
|
|
111
|
+
step: string;
|
|
112
|
+
/** Error message */
|
|
113
|
+
error: string;
|
|
114
|
+
/** Recovery time */
|
|
115
|
+
recoveryTime: number;
|
|
116
|
+
/** Number of LLM interactions */
|
|
117
|
+
llmInteractions: number;
|
|
118
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
|
|
2
|
+
import { test, expect } from '@playwright/test';
|
|
3
|
+
import { Runner } from '@cotestdev/ai-runner';
|
|
4
|
+
|
|
5
|
+
test.describe(`Register user`, () => {
|
|
6
|
+
const runner = Runner.NewInstance('43af4db4-3249-4135-b279-97b24f27106b', '2e189c6f-6c2b-4086-8fe7-837bf1a6b1df');
|
|
7
|
+
|
|
8
|
+
test.afterEach(async () => {
|
|
9
|
+
runner.clear();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
[
|
|
13
|
+
{
|
|
14
|
+
"password": "abcd1234"
|
|
15
|
+
}
|
|
16
|
+
].forEach((data, index) => {
|
|
17
|
+
test(`#${index + 1}`, async ({ page, context }) => {
|
|
18
|
+
await runner.init(page, context, data);
|
|
19
|
+
|
|
20
|
+
await runner.runStep('进入注册页面', async () => {
|
|
21
|
+
await page.goto('https://practice.expandtesting.com/register');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
await runner.runStep('填写并提交注册信息,用户名使用唯一格式(如未提供则自动生成)', async () => {
|
|
25
|
+
// 获取测试参数中的用户名和密码,如未提供用户名则自动生成
|
|
26
|
+
let username: string;
|
|
27
|
+
if (runner.has('username')) {
|
|
28
|
+
username = runner.get('username');
|
|
29
|
+
} else {
|
|
30
|
+
const timestamp = Date.now();
|
|
31
|
+
username = `user${timestamp}`;
|
|
32
|
+
}
|
|
33
|
+
const password = runner.get('password') || 'abcd1234';
|
|
34
|
+
|
|
35
|
+
// 填写注册表单
|
|
36
|
+
await page.locator('input[type="text"]').first().fill(username);
|
|
37
|
+
await page.locator('input[type="password"]').first().fill(password);
|
|
38
|
+
await page.locator('input[type="password"]').nth(1).fill(password);
|
|
39
|
+
|
|
40
|
+
// 提交注册表单
|
|
41
|
+
await page.getByRole('button', { name: 'Register' }).click();
|
|
42
|
+
|
|
43
|
+
// 设置输出参数
|
|
44
|
+
runner.set('username', username);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
await runner.runStep('确认注册成功', async () => {
|
|
48
|
+
// 验证注册成功后跳转到登录页面或显示成功提示
|
|
49
|
+
await expect(page).toHaveURL(/login/);
|
|
50
|
+
await expect(page.locator('.alert-success')).toContainText('Registration successful');
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
});
|