playwright-mimic 0.1.0 → 0.1.1
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/README.md +633 -446
- package/dist/agentic/agent.d.ts +106 -0
- package/dist/agentic/agent.d.ts.map +1 -0
- package/dist/agentic/agent.js +528 -0
- package/dist/agentic/agent.js.map +1 -0
- package/dist/agentic/index.d.ts +13 -0
- package/dist/agentic/index.d.ts.map +1 -0
- package/dist/agentic/index.js +12 -0
- package/dist/agentic/index.js.map +1 -0
- package/dist/agentic/planner.d.ts +41 -0
- package/dist/agentic/planner.d.ts.map +1 -0
- package/dist/agentic/planner.js +136 -0
- package/dist/agentic/planner.js.map +1 -0
- package/dist/agentic/react.d.ts +35 -0
- package/dist/agentic/react.d.ts.map +1 -0
- package/dist/agentic/react.js +170 -0
- package/dist/agentic/react.js.map +1 -0
- package/dist/agentic/recovery.d.ts +55 -0
- package/dist/agentic/recovery.d.ts.map +1 -0
- package/dist/agentic/recovery.js +200 -0
- package/dist/agentic/recovery.js.map +1 -0
- package/dist/agentic/reflection.d.ts +40 -0
- package/dist/agentic/reflection.d.ts.map +1 -0
- package/dist/agentic/reflection.js +142 -0
- package/dist/agentic/reflection.js.map +1 -0
- package/dist/agentic/types.d.ts +177 -0
- package/dist/agentic/types.d.ts.map +1 -0
- package/dist/agentic/types.js +8 -0
- package/dist/agentic/types.js.map +1 -0
- package/dist/agentic/wait.d.ts +50 -0
- package/dist/agentic/wait.d.ts.map +1 -0
- package/dist/agentic/wait.js +140 -0
- package/dist/agentic/wait.js.map +1 -0
- package/dist/agentic-mimic.d.ts +56 -0
- package/dist/agentic-mimic.d.ts.map +1 -0
- package/dist/agentic-mimic.js +98 -0
- package/dist/agentic-mimic.js.map +1 -0
- package/dist/index.d.ts +11 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -9
- package/dist/index.js.map +1 -1
- package/dist/mimic/actionType.d.ts +7 -0
- package/dist/mimic/actionType.d.ts.map +1 -0
- package/dist/mimic/actionType.js +44 -0
- package/dist/mimic/actionType.js.map +1 -0
- package/dist/mimic/annotations.d.ts +20 -0
- package/dist/mimic/annotations.d.ts.map +1 -0
- package/dist/mimic/annotations.js +30 -0
- package/dist/mimic/annotations.js.map +1 -0
- package/dist/mimic/cli.d.ts +15 -0
- package/dist/mimic/cli.d.ts.map +1 -0
- package/dist/mimic/cli.js +17 -0
- package/dist/mimic/cli.js.map +1 -0
- package/dist/mimic/click.d.ts +39 -0
- package/dist/mimic/click.d.ts.map +1 -0
- package/dist/mimic/click.js +233 -0
- package/dist/mimic/click.js.map +1 -0
- package/dist/mimic/forms.d.ts +47 -0
- package/dist/mimic/forms.d.ts.map +1 -0
- package/dist/mimic/forms.js +264 -0
- package/dist/mimic/forms.js.map +1 -0
- package/dist/mimic/navigation.d.ts +19 -0
- package/dist/mimic/navigation.d.ts.map +1 -0
- package/dist/mimic/navigation.js +117 -0
- package/dist/mimic/navigation.js.map +1 -0
- package/dist/mimic/replay.d.ts +21 -0
- package/dist/mimic/replay.d.ts.map +1 -0
- package/dist/mimic/replay.js +133 -0
- package/dist/mimic/replay.js.map +1 -0
- package/dist/mimic/schema/action.d.ts +315 -0
- package/dist/mimic/schema/action.d.ts.map +1 -0
- package/dist/mimic/schema/action.js +204 -0
- package/dist/mimic/schema/action.js.map +1 -0
- package/dist/mimic/selector.d.ts +139 -0
- package/dist/mimic/selector.d.ts.map +1 -0
- package/dist/mimic/selector.js +1103 -0
- package/dist/mimic/selector.js.map +1 -0
- package/dist/mimic/selectorDescriptor.d.ts +13 -0
- package/dist/mimic/selectorDescriptor.d.ts.map +1 -0
- package/dist/mimic/selectorDescriptor.js +9 -0
- package/dist/mimic/selectorDescriptor.js.map +1 -0
- package/dist/mimic/selectorSerialization.d.ts +30 -0
- package/dist/mimic/selectorSerialization.d.ts.map +1 -0
- package/dist/mimic/selectorSerialization.js +170 -0
- package/dist/mimic/selectorSerialization.js.map +1 -0
- package/dist/mimic/selectorTypes.d.ts +200 -0
- package/dist/mimic/selectorTypes.d.ts.map +1 -0
- package/dist/mimic/selectorTypes.js +2 -0
- package/dist/mimic/selectorTypes.js.map +1 -0
- package/dist/mimic/selectorUtils.d.ts +26 -0
- package/dist/mimic/selectorUtils.d.ts.map +1 -0
- package/dist/mimic/selectorUtils.js +144 -0
- package/dist/mimic/selectorUtils.js.map +1 -0
- package/dist/mimic/storage.d.ts +75 -0
- package/dist/mimic/storage.d.ts.map +1 -0
- package/dist/mimic/storage.js +197 -0
- package/dist/mimic/storage.js.map +1 -0
- package/dist/mimic/types.d.ts +63 -0
- package/dist/mimic/types.d.ts.map +1 -0
- package/dist/mimic/types.js +7 -0
- package/dist/mimic/types.js.map +1 -0
- package/dist/mimic.d.ts +28 -4
- package/dist/mimic.d.ts.map +1 -1
- package/dist/mimic.js +374 -32
- package/dist/mimic.js.map +1 -1
- package/dist/mimicry.d.ts +4 -4
- package/dist/mimicry.d.ts.map +1 -1
- package/dist/mimicry.js +22 -13
- package/dist/mimicry.js.map +1 -1
- package/package.json +5 -2
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error Recovery Module
|
|
3
|
+
*
|
|
4
|
+
* Implements error recovery strategies and retry logic for the agentic system.
|
|
5
|
+
*/
|
|
6
|
+
import { generateText, Output } from 'ai';
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
import { countTokens } from '../utils/token-counter.js';
|
|
9
|
+
/**
|
|
10
|
+
* Recovery strategy schema
|
|
11
|
+
*/
|
|
12
|
+
const zRecoveryStrategy = z.object({
|
|
13
|
+
shouldRetry: z.boolean().describe('Whether to retry the failed action'),
|
|
14
|
+
retryAction: z.string().optional().describe('Modified action to retry'),
|
|
15
|
+
alternativeApproach: z.string().optional().describe('Alternative approach to try'),
|
|
16
|
+
skipAndContinue: z.boolean().describe('Whether to skip this action and continue'),
|
|
17
|
+
abort: z.boolean().describe('Whether to abort execution'),
|
|
18
|
+
reasoning: z.string().describe('Reasoning behind the recovery strategy'),
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* Analyze an error and determine recovery strategy
|
|
22
|
+
*
|
|
23
|
+
* @param brain - Language model for recovery planning
|
|
24
|
+
* @param error - The error that occurred
|
|
25
|
+
* @param failedAction - The action that failed
|
|
26
|
+
* @param state - Current agent state
|
|
27
|
+
* @returns Promise resolving to recovery strategy
|
|
28
|
+
*/
|
|
29
|
+
export async function determineRecoveryStrategy(brain, error, failedAction, state) {
|
|
30
|
+
const recentErrors = state.errors
|
|
31
|
+
.slice(-5)
|
|
32
|
+
.map(e => `- ${e.type}: ${e.message} (${e.context})`)
|
|
33
|
+
.join('\n');
|
|
34
|
+
const prompt = `You are an error recovery specialist for browser automation.
|
|
35
|
+
|
|
36
|
+
**Failed Action:**
|
|
37
|
+
- Type: ${failedAction.actionType}
|
|
38
|
+
- Description: ${failedAction.description}
|
|
39
|
+
- Parameters: ${JSON.stringify(failedAction.parameters, null, 2)}
|
|
40
|
+
- Error: ${error.type} - ${error.message}
|
|
41
|
+
- Context: ${error.context}
|
|
42
|
+
|
|
43
|
+
**Current State:**
|
|
44
|
+
- URL: ${state.currentUrl}
|
|
45
|
+
- Actions Taken: ${state.actionHistory.length}
|
|
46
|
+
- Goal: ${state.overallObjective}
|
|
47
|
+
|
|
48
|
+
**Recent Errors:**
|
|
49
|
+
${recentErrors || 'No previous errors'}
|
|
50
|
+
|
|
51
|
+
**Instructions:**
|
|
52
|
+
Analyze the error and determine the best recovery strategy:
|
|
53
|
+
|
|
54
|
+
1. **Should Retry?**
|
|
55
|
+
- Is this a transient error (network, timing, element not ready)?
|
|
56
|
+
- Would retrying with the same action likely succeed?
|
|
57
|
+
- Consider: if this is the 3rd+ retry, probably don't retry again
|
|
58
|
+
|
|
59
|
+
2. **Retry Action**
|
|
60
|
+
- If retrying, suggest modifications (e.g., add wait, use different selector)
|
|
61
|
+
- Keep it concise and actionable
|
|
62
|
+
|
|
63
|
+
3. **Alternative Approach**
|
|
64
|
+
- If retry won't work, suggest a completely different approach
|
|
65
|
+
- Consider: different element, different action type, different strategy
|
|
66
|
+
|
|
67
|
+
4. **Skip and Continue**
|
|
68
|
+
- Is this action non-critical? Can we proceed without it?
|
|
69
|
+
- Only skip if the goal can still be achieved
|
|
70
|
+
|
|
71
|
+
5. **Abort**
|
|
72
|
+
- Should we give up? Only if goal seems unachievable
|
|
73
|
+
- Consider: too many failures, fundamental issue, goal impossible
|
|
74
|
+
|
|
75
|
+
Provide a clear recovery strategy with reasoning.`;
|
|
76
|
+
const result = await generateText({
|
|
77
|
+
model: brain,
|
|
78
|
+
prompt,
|
|
79
|
+
maxRetries: 2,
|
|
80
|
+
output: Output.object({ schema: zRecoveryStrategy, name: 'recoveryStrategy' }),
|
|
81
|
+
});
|
|
82
|
+
await countTokens(result);
|
|
83
|
+
const output = result.output;
|
|
84
|
+
const result_obj = {
|
|
85
|
+
shouldRetry: output.shouldRetry,
|
|
86
|
+
skipAndContinue: output.skipAndContinue,
|
|
87
|
+
abort: output.abort,
|
|
88
|
+
reasoning: output.reasoning,
|
|
89
|
+
};
|
|
90
|
+
if (output.retryAction !== undefined) {
|
|
91
|
+
result_obj.retryAction = output.retryAction;
|
|
92
|
+
}
|
|
93
|
+
if (output.alternativeApproach !== undefined) {
|
|
94
|
+
result_obj.alternativeApproach = output.alternativeApproach;
|
|
95
|
+
}
|
|
96
|
+
return result_obj;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Classify error type for appropriate recovery
|
|
100
|
+
*
|
|
101
|
+
* @param error - The error record
|
|
102
|
+
* @returns Error classification
|
|
103
|
+
*/
|
|
104
|
+
export function classifyError(error) {
|
|
105
|
+
const message = error.message.toLowerCase();
|
|
106
|
+
const type = error.type.toLowerCase();
|
|
107
|
+
// Transient errors (network, timing, element not ready)
|
|
108
|
+
if (message.includes('timeout') ||
|
|
109
|
+
message.includes('wait') ||
|
|
110
|
+
message.includes('not ready') ||
|
|
111
|
+
message.includes('loading') ||
|
|
112
|
+
message.includes('network') ||
|
|
113
|
+
type.includes('timeout')) {
|
|
114
|
+
return {
|
|
115
|
+
category: 'transient',
|
|
116
|
+
recoverable: true,
|
|
117
|
+
suggestedWait: 2000,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
// Environment errors (page not found, element not found)
|
|
121
|
+
if (message.includes('not found') ||
|
|
122
|
+
message.includes('element') && message.includes('missing') ||
|
|
123
|
+
message.includes('selector') ||
|
|
124
|
+
type.includes('not_found')) {
|
|
125
|
+
return {
|
|
126
|
+
category: 'environment',
|
|
127
|
+
recoverable: true,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
// Logic errors (invalid action, wrong parameters)
|
|
131
|
+
if (message.includes('invalid') ||
|
|
132
|
+
message.includes('parameter') ||
|
|
133
|
+
message.includes('required') ||
|
|
134
|
+
type.includes('validation')) {
|
|
135
|
+
return {
|
|
136
|
+
category: 'logic',
|
|
137
|
+
recoverable: false,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
// Permanent errors (permission denied, blocked)
|
|
141
|
+
if (message.includes('permission') ||
|
|
142
|
+
message.includes('blocked') ||
|
|
143
|
+
message.includes('forbidden') ||
|
|
144
|
+
message.includes('unauthorized')) {
|
|
145
|
+
return {
|
|
146
|
+
category: 'permanent',
|
|
147
|
+
recoverable: false,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
category: 'unknown',
|
|
152
|
+
recoverable: true,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Get retry delay based on attempt number
|
|
157
|
+
*
|
|
158
|
+
* Implements exponential backoff for retries.
|
|
159
|
+
*
|
|
160
|
+
* @param attemptNumber - Current retry attempt (1-based)
|
|
161
|
+
* @param baseDelay - Base delay in milliseconds (default: 1000)
|
|
162
|
+
* @returns Delay in milliseconds
|
|
163
|
+
*/
|
|
164
|
+
export function getRetryDelay(attemptNumber, baseDelay = 1000) {
|
|
165
|
+
// Exponential backoff: 1s, 2s, 4s, 8s, max 10s
|
|
166
|
+
const delay = Math.min(baseDelay * Math.pow(2, attemptNumber - 1), 10000);
|
|
167
|
+
return delay;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Check if an action should be retried based on error classification
|
|
171
|
+
*
|
|
172
|
+
* @param error - The error that occurred
|
|
173
|
+
* @param retryCount - Number of times this action has been retried
|
|
174
|
+
* @param maxRetries - Maximum number of retries allowed
|
|
175
|
+
* @returns Whether to retry
|
|
176
|
+
*/
|
|
177
|
+
export function shouldRetryBasedOnError(error, retryCount, maxRetries) {
|
|
178
|
+
if (retryCount >= maxRetries) {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
const classification = classifyError(error);
|
|
182
|
+
// Don't retry permanent or logic errors
|
|
183
|
+
if (!classification.recoverable) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
// Retry transient errors
|
|
187
|
+
if (classification.category === 'transient') {
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
// Retry environment errors (element might appear)
|
|
191
|
+
if (classification.category === 'environment' && retryCount < 2) {
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
// Unknown errors: retry once
|
|
195
|
+
if (classification.category === 'unknown' && retryCount < 1) {
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=recovery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recovery.js","sourceRoot":"","sources":["../../src/agentic/recovery.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAsB,YAAY,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC9D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAExD;;GAEG;AACH,MAAM,iBAAiB,GAAG,CAAC,C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,CAAC,aAAqB,EAAE,YAAoB,IAAI;IAC3E,+CAA+C;IAC/C,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,aAAa,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;IAC1E,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,uBAAuB,CACrC,KAAkB,EAClB,UAAkB,EAClB,UAAkB;IAElB,IAAI,UAAU,IAAI,UAAU,EAAE,CAAC;QAC7B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,cAAc,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IAE5C,wCAAwC;IACxC,IAAI,CAAC,cAAc,CAAC,WAAW,EAAE,CAAC;QAChC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,yBAAyB;IACzB,IAAI,cAAc,CAAC,QAAQ,KAAK,WAAW,EAAE,CAAC;QAC5C,OAAO,IAAI,CAAC;IACd,CAAC;IAED,kDAAkD;IAClD,IAAI,cAAc,CAAC,QAAQ,KAAK,aAAa,IAAI,UAAU,GAAG,CAAC,EAAE,CAAC;QAChE,OAAO,IAAI,CAAC;IACd,CAAC;IAED,6BAA6B;IAC7B,IAAI,cAAc,CAAC,QAAQ,KAAK,SAAS,IAAI,UAAU,GAAG,CAAC,EAAE,CAAC;QAC5D,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reflection Module
|
|
3
|
+
*
|
|
4
|
+
* Implements the Reflection Pattern for agentic systems.
|
|
5
|
+
* Evaluates actions and outcomes to improve future performance.
|
|
6
|
+
*/
|
|
7
|
+
import { type LanguageModel } from 'ai';
|
|
8
|
+
import type { ActionReflection, ActionRecord, AgentState } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Reflect on an action's outcome and effectiveness
|
|
11
|
+
*
|
|
12
|
+
* Evaluates whether the action was successful, what was learned,
|
|
13
|
+
* and whether it should be retried or if alternatives should be considered.
|
|
14
|
+
*
|
|
15
|
+
* @param brain - Language model for reflection
|
|
16
|
+
* @param action - The action that was executed
|
|
17
|
+
* @param state - Current agent state after the action
|
|
18
|
+
* @param goal - The overall goal being pursued
|
|
19
|
+
* @returns Promise resolving to reflection on the action
|
|
20
|
+
*/
|
|
21
|
+
export declare function reflectOnAction(brain: LanguageModel, action: ActionRecord, state: AgentState, goal: string): Promise<ActionReflection>;
|
|
22
|
+
/**
|
|
23
|
+
* Reflect on overall progress toward the goal
|
|
24
|
+
*
|
|
25
|
+
* Evaluates the agent's overall performance and suggests strategic changes.
|
|
26
|
+
*
|
|
27
|
+
* @param brain - Language model for reflection
|
|
28
|
+
* @param state - Current agent state
|
|
29
|
+
* @param goal - The overall goal
|
|
30
|
+
* @returns Promise resolving to strategic reflection
|
|
31
|
+
*/
|
|
32
|
+
export declare function reflectOnProgress(brain: LanguageModel, state: AgentState, goal: string): Promise<{
|
|
33
|
+
onTrack: boolean;
|
|
34
|
+
progressPercentage: number;
|
|
35
|
+
insights: string[];
|
|
36
|
+
recommendations: string[];
|
|
37
|
+
shouldPivot: boolean;
|
|
38
|
+
pivotStrategy?: string | undefined;
|
|
39
|
+
}>;
|
|
40
|
+
//# sourceMappingURL=reflection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reflection.d.ts","sourceRoot":"","sources":["../../src/agentic/reflection.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,KAAK,aAAa,EAAwB,MAAM,IAAI,CAAC;AAE9D,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAe7E;;;;;;;;;;;GAWG;AACH,wBAAsB,eAAe,CACnC,KAAK,EAAE,aAAa,EACpB,MAAM,EAAE,YAAY,EACpB,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,gBAAgB,CAAC,CAmD3B;AAED;;;;;;;;;GASG;AACH,wBAAsB,iBAAiB,CACrC,KAAK,EAAE,aAAa,EACpB,KAAK,EAAE,UAAU,EACjB,IAAI,EAAE,MAAM,GACX,OAAO,CAAC;IACT,OAAO,EAAE,OAAO,CAAC;IACjB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,eAAe,EAAE,MAAM,EAAE,CAAC;IAC1B,WAAW,EAAE,OAAO,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACpC,CAAC,CAsDD"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reflection Module
|
|
3
|
+
*
|
|
4
|
+
* Implements the Reflection Pattern for agentic systems.
|
|
5
|
+
* Evaluates actions and outcomes to improve future performance.
|
|
6
|
+
*/
|
|
7
|
+
import { generateText, Output } from 'ai';
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
import { countTokens } from '../utils/token-counter.js';
|
|
10
|
+
/**
|
|
11
|
+
* Schema for action reflection validation
|
|
12
|
+
*/
|
|
13
|
+
const zActionReflection = z.object({
|
|
14
|
+
progressMade: z.boolean().describe('Whether this action moved closer to the goal'),
|
|
15
|
+
confidence: z.number().min(0).max(1).describe('Confidence that the action was successful (0-1)'),
|
|
16
|
+
learnings: z.array(z.string()).default([]).describe('What was learned from this action'),
|
|
17
|
+
suggestions: z.array(z.string()).default([]).describe('Suggestions for improving future actions'),
|
|
18
|
+
shouldRetry: z.boolean().describe('Whether this action should be retried'),
|
|
19
|
+
alternatives: z.array(z.string()).default([]).describe('Alternative approaches to consider'),
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* Reflect on an action's outcome and effectiveness
|
|
23
|
+
*
|
|
24
|
+
* Evaluates whether the action was successful, what was learned,
|
|
25
|
+
* and whether it should be retried or if alternatives should be considered.
|
|
26
|
+
*
|
|
27
|
+
* @param brain - Language model for reflection
|
|
28
|
+
* @param action - The action that was executed
|
|
29
|
+
* @param state - Current agent state after the action
|
|
30
|
+
* @param goal - The overall goal being pursued
|
|
31
|
+
* @returns Promise resolving to reflection on the action
|
|
32
|
+
*/
|
|
33
|
+
export async function reflectOnAction(brain, action, state, goal) {
|
|
34
|
+
const previousState = state.actionHistory.length > 1
|
|
35
|
+
? state.actionHistory[state.actionHistory.length - 2]
|
|
36
|
+
: null;
|
|
37
|
+
const prompt = `You are an intelligent agent reflecting on an action you just took.
|
|
38
|
+
|
|
39
|
+
**Your Goal:**
|
|
40
|
+
${goal}
|
|
41
|
+
|
|
42
|
+
**Action Taken:**
|
|
43
|
+
- Type: ${action.actionType}
|
|
44
|
+
- Description: ${action.description}
|
|
45
|
+
- Parameters: ${JSON.stringify(action.parameters, null, 2)}
|
|
46
|
+
- Success: ${action.success ? 'Yes' : 'No'}
|
|
47
|
+
- Result: ${action.result || 'No result recorded'}
|
|
48
|
+
${action.error ? `- Error: ${action.error}` : ''}
|
|
49
|
+
|
|
50
|
+
**Current State After Action:**
|
|
51
|
+
- URL: ${state.currentUrl}
|
|
52
|
+
- Page Title: ${state.pageTitle}
|
|
53
|
+
- Available Elements: ${state.availableElements.length}
|
|
54
|
+
- Total Actions: ${state.actionHistory.length}
|
|
55
|
+
- Goal Achieved: ${state.goalAchieved ? 'Yes' : 'No'}
|
|
56
|
+
|
|
57
|
+
**Previous Action (for comparison):**
|
|
58
|
+
${previousState ? `- ${previousState.description} (${previousState.success ? 'success' : 'failed'})` : 'This was the first action'}
|
|
59
|
+
|
|
60
|
+
**Instructions:**
|
|
61
|
+
1. **Evaluate Progress**: Did this action move you closer to the goal?
|
|
62
|
+
2. **Assess Confidence**: How confident are you that this action was successful?
|
|
63
|
+
3. **Identify Learnings**: What did you learn from this action?
|
|
64
|
+
- What worked?
|
|
65
|
+
- What didn't work?
|
|
66
|
+
- What patterns do you notice?
|
|
67
|
+
4. **Generate Suggestions**: How could similar actions be improved in the future?
|
|
68
|
+
5. **Decide on Retry**: Should this action be retried? (Consider: was it a transient failure? Is there a better approach?)
|
|
69
|
+
6. **Consider Alternatives**: What other approaches could achieve the same goal?
|
|
70
|
+
|
|
71
|
+
Think critically and provide honest, actionable reflection.`;
|
|
72
|
+
const result = await generateText({
|
|
73
|
+
model: brain,
|
|
74
|
+
prompt,
|
|
75
|
+
maxRetries: 3,
|
|
76
|
+
output: Output.object({ schema: zActionReflection, name: 'actionReflection' }),
|
|
77
|
+
});
|
|
78
|
+
await countTokens(result);
|
|
79
|
+
return result.output;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Reflect on overall progress toward the goal
|
|
83
|
+
*
|
|
84
|
+
* Evaluates the agent's overall performance and suggests strategic changes.
|
|
85
|
+
*
|
|
86
|
+
* @param brain - Language model for reflection
|
|
87
|
+
* @param state - Current agent state
|
|
88
|
+
* @param goal - The overall goal
|
|
89
|
+
* @returns Promise resolving to strategic reflection
|
|
90
|
+
*/
|
|
91
|
+
export async function reflectOnProgress(brain, state, goal) {
|
|
92
|
+
const recentActions = state.actionHistory.slice(-10);
|
|
93
|
+
const successRate = recentActions.length > 0
|
|
94
|
+
? recentActions.filter(a => a.success).length / recentActions.length
|
|
95
|
+
: 0;
|
|
96
|
+
const prompt = `You are evaluating overall progress toward a goal.
|
|
97
|
+
|
|
98
|
+
**Goal:**
|
|
99
|
+
${goal}
|
|
100
|
+
|
|
101
|
+
**Current State:**
|
|
102
|
+
- Actions Taken: ${state.actionHistory.length}
|
|
103
|
+
- Recent Success Rate: ${(successRate * 100).toFixed(1)}%
|
|
104
|
+
- Goal Achieved: ${state.goalAchieved ? 'Yes' : 'No'}
|
|
105
|
+
- Current URL: ${state.currentUrl}
|
|
106
|
+
- Errors Encountered: ${state.errors.length}
|
|
107
|
+
|
|
108
|
+
**Recent Actions:**
|
|
109
|
+
${recentActions.map(a => `- ${a.description} (${a.success ? '✓' : '✗'})`).join('\n')}
|
|
110
|
+
|
|
111
|
+
**Errors:**
|
|
112
|
+
${state.errors.slice(-5).map(e => `- ${e.type}: ${e.message}`).join('\n')}
|
|
113
|
+
|
|
114
|
+
**Instructions:**
|
|
115
|
+
1. Assess if the agent is on track to achieve the goal
|
|
116
|
+
2. Estimate progress percentage (0-100%)
|
|
117
|
+
3. Identify key insights about the approach
|
|
118
|
+
4. Provide strategic recommendations
|
|
119
|
+
5. Determine if a pivot in strategy is needed
|
|
120
|
+
6. If pivoting, suggest a new strategy
|
|
121
|
+
|
|
122
|
+
Be honest and strategic in your assessment.`;
|
|
123
|
+
const result = await generateText({
|
|
124
|
+
model: brain,
|
|
125
|
+
prompt,
|
|
126
|
+
maxRetries: 3,
|
|
127
|
+
output: Output.object({
|
|
128
|
+
schema: z.object({
|
|
129
|
+
onTrack: z.boolean().describe('Whether the agent is on track to achieve the goal'),
|
|
130
|
+
progressPercentage: z.number().min(0).max(100).describe('Estimated progress percentage'),
|
|
131
|
+
insights: z.array(z.string()).describe('Key insights about the approach'),
|
|
132
|
+
recommendations: z.array(z.string()).describe('Strategic recommendations'),
|
|
133
|
+
shouldPivot: z.boolean().describe('Whether the strategy should be changed'),
|
|
134
|
+
pivotStrategy: z.string().optional().describe('New strategy if pivoting is recommended'),
|
|
135
|
+
}),
|
|
136
|
+
name: 'progressReflection',
|
|
137
|
+
}),
|
|
138
|
+
});
|
|
139
|
+
await countTokens(result);
|
|
140
|
+
return result.output;
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=reflection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reflection.js","sourceRoot":"","sources":["../../src/agentic/reflection.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAsB,YAAY,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC9D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,WAAW,EAAE,MAA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AC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,QAAQ,CAAC,iCAAiC,CAAC;gBACzE,eAAe,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,QAAQ,CAAC,2BAA2B,CAAC;gBAC1E,WAAW,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,CAAC,wCAAwC,CAAC;gBAC3E,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,yCAAyC,CAAC;aACzF,CAAC;YACF,IAAI,EAAE,oBAAoB;SAC3B,CAAC;KACH,CAAC,CAAC;IAEH,MAAM,WAAW,CAAC,MAAM,CAAC,CAAC;IAE1B,OAAO,MAAM,CAAC,MAAM,CAAC;AACvB,CAAC"}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agentic System Types
|
|
3
|
+
*
|
|
4
|
+
* Core type definitions for the agentic browser automation system.
|
|
5
|
+
* This system implements planning, reasoning, acting, and reflection patterns.
|
|
6
|
+
*/
|
|
7
|
+
import { Page } from '@playwright/test';
|
|
8
|
+
import type { LanguageModel } from 'ai';
|
|
9
|
+
import type { TargetInfo } from '../mimic/selector.js';
|
|
10
|
+
/**
|
|
11
|
+
* Agent State: Represents the current state of the agent and the page
|
|
12
|
+
*/
|
|
13
|
+
export interface AgentState {
|
|
14
|
+
/** Current URL of the page */
|
|
15
|
+
currentUrl: string;
|
|
16
|
+
/** Page title */
|
|
17
|
+
pageTitle: string;
|
|
18
|
+
/** Available interactive elements on the page */
|
|
19
|
+
availableElements: TargetInfo[];
|
|
20
|
+
/** History of actions taken so far */
|
|
21
|
+
actionHistory: ActionRecord[];
|
|
22
|
+
/** Current goal or objective */
|
|
23
|
+
currentGoal: string;
|
|
24
|
+
/** Overall objective the agent is trying to achieve */
|
|
25
|
+
overallObjective: string;
|
|
26
|
+
/** Whether the goal has been achieved */
|
|
27
|
+
goalAchieved: boolean;
|
|
28
|
+
/** Any errors encountered during execution */
|
|
29
|
+
errors: ErrorRecord[];
|
|
30
|
+
/** Metadata about the current execution context */
|
|
31
|
+
metadata: {
|
|
32
|
+
stepCount: number;
|
|
33
|
+
startTime: number;
|
|
34
|
+
lastActionTime: number;
|
|
35
|
+
};
|
|
36
|
+
/** Whether the page is currently in a loading state */
|
|
37
|
+
isLoading: boolean;
|
|
38
|
+
/** Number of visible loading indicators */
|
|
39
|
+
loadingIndicatorCount: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Action Record: Tracks a single action execution
|
|
43
|
+
*/
|
|
44
|
+
export interface ActionRecord {
|
|
45
|
+
/** Unique identifier for this action */
|
|
46
|
+
id: string;
|
|
47
|
+
/** Timestamp when action was planned */
|
|
48
|
+
plannedAt: number;
|
|
49
|
+
/** Timestamp when action was executed */
|
|
50
|
+
executedAt?: number;
|
|
51
|
+
/** Type of action (click, navigation, form, etc.) */
|
|
52
|
+
actionType: string;
|
|
53
|
+
/** Description of what the action was supposed to do */
|
|
54
|
+
description: string;
|
|
55
|
+
/** Parameters used for the action */
|
|
56
|
+
parameters: Record<string, unknown>;
|
|
57
|
+
/** Whether the action was successful */
|
|
58
|
+
success: boolean;
|
|
59
|
+
/** Result or outcome of the action */
|
|
60
|
+
result?: string;
|
|
61
|
+
/** Error if the action failed */
|
|
62
|
+
error?: string;
|
|
63
|
+
/** Reflection on the action's effectiveness */
|
|
64
|
+
reflection?: ActionReflection;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Action Reflection: Agent's evaluation of an action's effectiveness
|
|
68
|
+
*/
|
|
69
|
+
export interface ActionReflection {
|
|
70
|
+
/** Whether the action moved closer to the goal */
|
|
71
|
+
progressMade: boolean;
|
|
72
|
+
/** Confidence in the action's success (0-1) */
|
|
73
|
+
confidence: number;
|
|
74
|
+
/** What was learned from this action */
|
|
75
|
+
learnings: string[];
|
|
76
|
+
/** Suggestions for improvement */
|
|
77
|
+
suggestions: string[];
|
|
78
|
+
/** Whether a retry is recommended */
|
|
79
|
+
shouldRetry: boolean;
|
|
80
|
+
/** Alternative approaches to consider */
|
|
81
|
+
alternatives: string[];
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Error Record: Tracks errors and failures
|
|
85
|
+
*/
|
|
86
|
+
export interface ErrorRecord {
|
|
87
|
+
/** Timestamp when error occurred */
|
|
88
|
+
timestamp: number;
|
|
89
|
+
/** Type of error */
|
|
90
|
+
type: string;
|
|
91
|
+
/** Error message */
|
|
92
|
+
message: string;
|
|
93
|
+
/** Context where error occurred */
|
|
94
|
+
context: string;
|
|
95
|
+
/** Whether the error was recovered from */
|
|
96
|
+
recovered: boolean;
|
|
97
|
+
/** Recovery action taken */
|
|
98
|
+
recoveryAction?: string;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Planning Result: Output from the planning phase
|
|
102
|
+
*/
|
|
103
|
+
export interface PlanningResult {
|
|
104
|
+
/** High-level plan broken into steps */
|
|
105
|
+
steps: PlanStep[];
|
|
106
|
+
/** Estimated complexity/difficulty */
|
|
107
|
+
complexity: 'low' | 'medium' | 'high';
|
|
108
|
+
/** Potential challenges or risks */
|
|
109
|
+
challenges: string[];
|
|
110
|
+
/** Prerequisites or dependencies */
|
|
111
|
+
prerequisites: string[];
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Plan Step: A single step in the execution plan
|
|
115
|
+
*/
|
|
116
|
+
export interface PlanStep {
|
|
117
|
+
/** Step number/order */
|
|
118
|
+
order: number;
|
|
119
|
+
/** Description of what this step should accomplish */
|
|
120
|
+
description: string;
|
|
121
|
+
/** Expected action type */
|
|
122
|
+
expectedActionType: string;
|
|
123
|
+
/** Success criteria for this step */
|
|
124
|
+
successCriteria: string;
|
|
125
|
+
/** Dependencies on previous steps */
|
|
126
|
+
dependencies: number[];
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Reasoning Result: Output from the reasoning phase (ReAct pattern)
|
|
130
|
+
*/
|
|
131
|
+
export interface ReasoningResult {
|
|
132
|
+
/** What the agent observed about the current state */
|
|
133
|
+
observation: string;
|
|
134
|
+
/** What the agent is thinking about doing */
|
|
135
|
+
thought: string;
|
|
136
|
+
/** The action the agent decided to take */
|
|
137
|
+
action: DecidedAction;
|
|
138
|
+
/** Confidence in this decision (0-1) */
|
|
139
|
+
confidence: number;
|
|
140
|
+
/** Alternative actions considered */
|
|
141
|
+
alternatives: DecidedAction[];
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Decided Action: An action the agent has decided to take
|
|
145
|
+
*/
|
|
146
|
+
export interface DecidedAction {
|
|
147
|
+
/** Type of action */
|
|
148
|
+
type: 'navigation' | 'click' | 'form' | 'assertion' | 'wait' | 'retry' | 'abort';
|
|
149
|
+
/** Description of the action */
|
|
150
|
+
description: string;
|
|
151
|
+
/** Parameters for the action */
|
|
152
|
+
parameters: Record<string, unknown>;
|
|
153
|
+
/** Expected outcome */
|
|
154
|
+
expectedOutcome: string;
|
|
155
|
+
/** Rationale for choosing this action */
|
|
156
|
+
rationale: string;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Agent Configuration: Configuration for the agentic system
|
|
160
|
+
*/
|
|
161
|
+
export interface AgentConfig {
|
|
162
|
+
/** Language model to use for reasoning */
|
|
163
|
+
brain: LanguageModel;
|
|
164
|
+
/** Playwright page object */
|
|
165
|
+
page: Page;
|
|
166
|
+
/** Maximum number of actions before aborting */
|
|
167
|
+
maxActions?: number;
|
|
168
|
+
/** Maximum retries per action */
|
|
169
|
+
maxRetries?: number;
|
|
170
|
+
/** Whether to enable reflection */
|
|
171
|
+
enableReflection?: boolean;
|
|
172
|
+
/** Whether to enable planning */
|
|
173
|
+
enablePlanning?: boolean;
|
|
174
|
+
/** Timeout for actions in milliseconds */
|
|
175
|
+
actionTimeout?: number;
|
|
176
|
+
}
|
|
177
|
+
//# sourceMappingURL=types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C;CACxB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/agentic/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Smart Wait Module
|
|
3
|
+
*
|
|
4
|
+
* Implements intelligent waiting that detects loading indicators and waits
|
|
5
|
+
* for them to disappear, rather than using fixed timeouts.
|
|
6
|
+
*/
|
|
7
|
+
import { Page } from '@playwright/test';
|
|
8
|
+
/**
|
|
9
|
+
* Detect if there are any loading indicators visible on the page
|
|
10
|
+
*
|
|
11
|
+
* @param page - Playwright Page object
|
|
12
|
+
* @returns Promise resolving to array of visible loading indicator selectors
|
|
13
|
+
*/
|
|
14
|
+
export declare function detectLoadingIndicators(page: Page): Promise<string[]>;
|
|
15
|
+
/**
|
|
16
|
+
* Wait for all loading indicators to disappear
|
|
17
|
+
*
|
|
18
|
+
* @param page - Playwright Page object
|
|
19
|
+
* @param timeout - Maximum time to wait in milliseconds (default: 10000)
|
|
20
|
+
* @returns Promise that resolves when all loaders are gone, or rejects on timeout
|
|
21
|
+
*/
|
|
22
|
+
export declare function waitForLoadersToDisappear(page: Page, timeout?: number): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Smart wait that detects and waits for loading indicators
|
|
25
|
+
* Falls back to fixed timeout if no loaders are detected
|
|
26
|
+
*
|
|
27
|
+
* @param page - Playwright Page object
|
|
28
|
+
* @param options - Wait options
|
|
29
|
+
* @returns Promise that resolves when wait is complete
|
|
30
|
+
*/
|
|
31
|
+
export declare function smartWait(page: Page, options?: {
|
|
32
|
+
/** Maximum time to wait for loaders (default: 10000ms) */
|
|
33
|
+
loaderTimeout?: number;
|
|
34
|
+
/** Fallback fixed timeout if no loaders detected (default: 1000ms) */
|
|
35
|
+
fallbackTimeout?: number;
|
|
36
|
+
/** Whether to wait for network idle (default: false) */
|
|
37
|
+
waitForNetworkIdle?: boolean;
|
|
38
|
+
}): Promise<{
|
|
39
|
+
waitedForLoaders: boolean;
|
|
40
|
+
loaderCount: number;
|
|
41
|
+
duration: number;
|
|
42
|
+
}>;
|
|
43
|
+
/**
|
|
44
|
+
* Check if the page appears to be in a loading state
|
|
45
|
+
*
|
|
46
|
+
* @param page - Playwright Page object
|
|
47
|
+
* @returns Promise resolving to whether page is loading
|
|
48
|
+
*/
|
|
49
|
+
export declare function isPageLoading(page: Page): Promise<boolean>;
|
|
50
|
+
//# sourceMappingURL=wait.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wait.d.ts","sourceRoot":"","sources":["../../src/agentic/wait.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AA8BxC;;;;;GAKG;AACH,wBAAsB,uBAAuB,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAsB3E;AAED;;;;;;GAMG;AACH,wBAAsB,yBAAyB,CAC7C,IAAI,EAAE,IAAI,EACV,OAAO,GAAE,MAAc,GACtB,OAAO,CAAC,IAAI,CAAC,CAoBf;AAED;;;;;;;GAOG;AACH,wBAAsB,SAAS,CAC7B,IAAI,EAAE,IAAI,EACV,OAAO,GAAE;IACP,0DAA0D;IAC1D,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,sEAAsE;IACtE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,wDAAwD;IACxD,kBAAkB,CAAC,EAAE,OAAO,CAAC;CACzB,GACL,OAAO,CAAC;IACT,gBAAgB,EAAE,OAAO,CAAC;IAC1B,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC,CA2CD;AAED;;;;;GAKG;AACH,wBAAsB,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,CAGhE"}
|