testchimp-runner-core 0.0.35 → 0.0.36
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 +6 -1
- package/plandocs/BEFORE_AFTER_VERIFICATION.md +0 -148
- package/plandocs/COORDINATE_MODE_DIAGNOSIS.md +0 -144
- package/plandocs/CREDIT_CALLBACK_ARCHITECTURE.md +0 -253
- package/plandocs/HUMAN_LIKE_IMPROVEMENTS.md +0 -642
- package/plandocs/IMPLEMENTATION_STATUS.md +0 -108
- package/plandocs/INTEGRATION_COMPLETE.md +0 -322
- package/plandocs/MULTI_AGENT_ARCHITECTURE_REVIEW.md +0 -844
- package/plandocs/ORCHESTRATOR_MVP_SUMMARY.md +0 -539
- package/plandocs/PHASE1_ABSTRACTION_COMPLETE.md +0 -241
- package/plandocs/PHASE1_FINAL_STATUS.md +0 -210
- package/plandocs/PHASE_1_COMPLETE.md +0 -165
- package/plandocs/PHASE_1_SUMMARY.md +0 -184
- package/plandocs/PLANNING_SESSION_SUMMARY.md +0 -372
- package/plandocs/PROMPT_OPTIMIZATION_ANALYSIS.md +0 -120
- package/plandocs/PROMPT_SANITY_CHECK.md +0 -120
- package/plandocs/SCRIPT_CLEANUP_FEATURE.md +0 -201
- package/plandocs/SCRIPT_GENERATION_ARCHITECTURE.md +0 -364
- package/plandocs/SELECTOR_IMPROVEMENTS.md +0 -139
- package/plandocs/SESSION_SUMMARY_v0.0.33.md +0 -151
- package/plandocs/TROUBLESHOOTING_SESSION.md +0 -72
- package/plandocs/VISION_DIAGNOSTICS_IMPROVEMENTS.md +0 -336
- package/plandocs/VISUAL_AGENT_EVOLUTION_PLAN.md +0 -396
- package/plandocs/WHATS_NEW_v0.0.33.md +0 -183
- package/plandocs/exploratory-mode-support-v2.plan.md +0 -953
- package/plandocs/exploratory-mode-support.plan.md +0 -928
- package/plandocs/journey-id-tracking-addendum.md +0 -227
- package/releasenotes/RELEASE_0.0.26.md +0 -165
- package/releasenotes/RELEASE_0.0.27.md +0 -236
- package/releasenotes/RELEASE_0.0.28.md +0 -286
- package/src/auth-config.ts +0 -84
- package/src/credit-usage-service.ts +0 -188
- package/src/env-loader.ts +0 -103
- package/src/execution-service.ts +0 -996
- package/src/file-handler.ts +0 -104
- package/src/index.ts +0 -432
- package/src/llm-facade.ts +0 -821
- package/src/llm-provider.ts +0 -53
- package/src/model-constants.ts +0 -35
- package/src/orchestrator/decision-parser.ts +0 -139
- package/src/orchestrator/index.ts +0 -58
- package/src/orchestrator/orchestrator-agent.ts +0 -1282
- package/src/orchestrator/orchestrator-prompts.ts +0 -786
- package/src/orchestrator/page-som-handler.ts +0 -1565
- package/src/orchestrator/som-types.ts +0 -188
- package/src/orchestrator/tool-registry.ts +0 -184
- package/src/orchestrator/tools/check-page-ready.ts +0 -75
- package/src/orchestrator/tools/extract-data.ts +0 -92
- package/src/orchestrator/tools/index.ts +0 -15
- package/src/orchestrator/tools/inspect-page.ts +0 -42
- package/src/orchestrator/tools/recall-history.ts +0 -72
- package/src/orchestrator/tools/refresh-som-markers.ts +0 -69
- package/src/orchestrator/tools/take-screenshot.ts +0 -128
- package/src/orchestrator/tools/verify-action-result.ts +0 -159
- package/src/orchestrator/tools/view-previous-screenshot.ts +0 -103
- package/src/orchestrator/types.ts +0 -291
- package/src/playwright-mcp-service.ts +0 -224
- package/src/progress-reporter.ts +0 -144
- package/src/prompts.ts +0 -842
- package/src/providers/backend-proxy-llm-provider.ts +0 -91
- package/src/providers/local-llm-provider.ts +0 -38
- package/src/scenario-service.ts +0 -252
- package/src/scenario-worker-class.ts +0 -1110
- package/src/script-utils.ts +0 -203
- package/src/types.ts +0 -239
- package/src/utils/browser-utils.ts +0 -348
- package/src/utils/coordinate-converter.ts +0 -162
- package/src/utils/page-info-retry.ts +0 -65
- package/src/utils/page-info-utils.ts +0 -285
- package/testchimp-runner-core-0.0.35.tgz +0 -0
- package/tsconfig.json +0 -19
package/package.json
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "testchimp-runner-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.36",
|
|
4
4
|
"description": "Core TestChimp functionality for test generation and AI repair",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist/",
|
|
9
|
+
"env.prod",
|
|
10
|
+
"env.staging"
|
|
11
|
+
],
|
|
7
12
|
"scripts": {
|
|
8
13
|
"build": "tsc",
|
|
9
14
|
"watch": "tsc --watch",
|
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
# Before/After Screenshot Verification
|
|
2
|
-
|
|
3
|
-
## Feature: Visual Goal Verification for Coordinate Actions
|
|
4
|
-
|
|
5
|
-
### Problem Solved:
|
|
6
|
-
When using coordinate-based actions (clicking at x,y%), the agent has no way to know if the click achieved the goal:
|
|
7
|
-
- No element reference to check state
|
|
8
|
-
- No selector feedback
|
|
9
|
-
- Can't verify if expected page loaded or modal opened
|
|
10
|
-
|
|
11
|
-
This led to:
|
|
12
|
-
- False positives (click succeeded but goal not achieved)
|
|
13
|
-
- Infinite loops (agent keeps clicking, unsure if it worked)
|
|
14
|
-
|
|
15
|
-
### Solution:
|
|
16
|
-
Automatic before/after screenshot comparison after coordinate clicks.
|
|
17
|
-
|
|
18
|
-
## How It Works:
|
|
19
|
-
|
|
20
|
-
### 1. **Automatic Trigger** (No Agent Action Required)
|
|
21
|
-
When agent uses coordinate action:
|
|
22
|
-
```typescript
|
|
23
|
-
Iteration 4: 🎯 Coordinate mode activated
|
|
24
|
-
Step 1: Capture BEFORE screenshot
|
|
25
|
-
Step 2: Execute coordinate click (x%, y%)
|
|
26
|
-
Step 3: Wait 1000ms for UI to settle
|
|
27
|
-
Step 4: Capture AFTER screenshot
|
|
28
|
-
Step 5: Call LLM with both images (labeled "BEFORE", "AFTER")
|
|
29
|
-
Step 6: LLM responds: { goalAchieved: true/false, reasoning: "..." }
|
|
30
|
-
Step 7a: If TRUE → Mark complete, exit step ✅
|
|
31
|
-
Step 7b: If FALSE → Continue to next iteration, try different coordinates
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
### 2. **LLM Prompt for Verification**
|
|
35
|
-
```
|
|
36
|
-
Goal: [Current step goal]
|
|
37
|
-
|
|
38
|
-
Compare the BEFORE and AFTER screenshots.
|
|
39
|
-
|
|
40
|
-
Did the action achieve the goal? Respond with JSON:
|
|
41
|
-
{
|
|
42
|
-
"goalAchieved": boolean,
|
|
43
|
-
"reasoning": "What changed (or didn't change)",
|
|
44
|
-
"visibleChanges": ["List of UI changes observed"]
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
Focus on:
|
|
48
|
-
- Did expected elements appear/disappear?
|
|
49
|
-
- Did page navigate or content change?
|
|
50
|
-
- Visual indicators of success (new panels, forms, highlights)?
|
|
51
|
-
|
|
52
|
-
Be strict: Only return true if you clearly see the expected change.
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
### 3. **Multi-Image LLM Interface**
|
|
56
|
-
```typescript
|
|
57
|
-
// NEW: LabeledImage interface
|
|
58
|
-
export interface LabeledImage {
|
|
59
|
-
label: string; // "Before", "After", etc.
|
|
60
|
-
dataUrl: string; // Base64 data URL
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// UPDATED: LLMRequest
|
|
64
|
-
export interface LLMRequest {
|
|
65
|
-
imageUrl?: string; // Backward compatible (single image)
|
|
66
|
-
images?: LabeledImage[]; // NEW - multi-image support
|
|
67
|
-
}
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
### 4. **Provider Implementation** (scriptservice-llm-provider.ts)
|
|
71
|
-
```typescript
|
|
72
|
-
if (request.images && request.images.length > 0) {
|
|
73
|
-
for (const img of request.images) {
|
|
74
|
-
contentParts.push({ type: 'text', text: `\n[${img.label}]:` });
|
|
75
|
-
contentParts.push({ type: 'image_url', image_url: { url: img.dataUrl } });
|
|
76
|
-
}
|
|
77
|
-
// Sends: [BEFORE]: <image1>, [AFTER]: <image2>
|
|
78
|
-
}
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
## When Verification Happens:
|
|
82
|
-
|
|
83
|
-
✅ **Always**: After first coordinate action attempt
|
|
84
|
-
❌ **Never**: After selector-based actions (have element state to check)
|
|
85
|
-
⚠️ **Conditional**: Can add for other scenarios where goal verification is unclear
|
|
86
|
-
|
|
87
|
-
## Cost Considerations:
|
|
88
|
-
|
|
89
|
-
**Per verification call:**
|
|
90
|
-
- 2 viewport screenshots (~50-100KB each)
|
|
91
|
-
- Vision model (gpt-5-mini): ~$0.001 per call
|
|
92
|
-
- Used only when coordinate mode activates (after 3 selector failures)
|
|
93
|
-
|
|
94
|
-
**Typical scenario:**
|
|
95
|
-
- Steps 1-10: Regular selectors → No verification cost
|
|
96
|
-
- Step 5 gets stuck → Coordinate mode → 1 verification call → $0.001
|
|
97
|
-
- Overall impact: Minimal, used sparingly
|
|
98
|
-
|
|
99
|
-
## Example Flow:
|
|
100
|
-
|
|
101
|
-
**Step 5: "Select Employee Information"**
|
|
102
|
-
```
|
|
103
|
-
Iteration 1: getByText('Employee Information') → Strict mode ❌
|
|
104
|
-
Iteration 2: locator('#collapse-1').getByText('Employee Information') → Click succeeds ✅
|
|
105
|
-
BUT: Didn't navigate to Employee Information page (false positive)
|
|
106
|
-
|
|
107
|
-
Iteration 3: Selector fails again
|
|
108
|
-
Iteration 4: 🎯 Coordinate mode
|
|
109
|
-
→ BEFORE: Homepage with sidebar
|
|
110
|
-
→ Click at (19.3%, 22.9%)
|
|
111
|
-
→ Wait 1s
|
|
112
|
-
→ AFTER: Check screenshot
|
|
113
|
-
→ LLM: "goalAchieved": true, "reasoning": "Employee Information page loaded with form"
|
|
114
|
-
→ ✅ Mark complete, exit
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
## Backward Compatibility:
|
|
118
|
-
|
|
119
|
-
✅ **Single image still works:**
|
|
120
|
-
```typescript
|
|
121
|
-
const request = {
|
|
122
|
-
imageUrl: 'data:image/png;base64,...' // Old way
|
|
123
|
-
};
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
✅ **Multi-image NEW:**
|
|
127
|
-
```typescript
|
|
128
|
-
const request = {
|
|
129
|
-
images: [
|
|
130
|
-
{ label: 'BEFORE', dataUrl: '...' },
|
|
131
|
-
{ label: 'AFTER', dataUrl: '...' }
|
|
132
|
-
]
|
|
133
|
-
};
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
## Files Modified:
|
|
137
|
-
|
|
138
|
-
1. `runner-core/src/llm-provider.ts` - Added LabeledImage interface and images field
|
|
139
|
-
2. `scriptservice/providers/scriptservice-llm-provider.ts` - Handle multiple images in OpenAI API
|
|
140
|
-
3. `runner-core/src/orchestrator/orchestrator-agent.ts` - Added verifyGoalWithScreenshotComparison method
|
|
141
|
-
4. Automatic trigger after coordinate actions
|
|
142
|
-
|
|
143
|
-
## Next Steps:
|
|
144
|
-
|
|
145
|
-
- ✅ Infrastructure ready
|
|
146
|
-
- ⏳ Need to test with real scenario
|
|
147
|
-
- 🔮 Future: Could expose as agent-callable tool if needed
|
|
148
|
-
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
# Coordinate Mode Diagnosis - Live Test Results
|
|
2
|
-
|
|
3
|
-
## Test Scenario: PeopleHR Employee Information Flow
|
|
4
|
-
|
|
5
|
-
### ✅ What Worked:
|
|
6
|
-
|
|
7
|
-
1. **Coordinate fallback DID activate** (after fix from >= 3 to >= 5)
|
|
8
|
-
2. **Agent successfully used coordinates** at (87.5%, 23.438%)
|
|
9
|
-
3. **Physical clicks succeeded** - page.mouse.click(1120, 169)
|
|
10
|
-
4. **Agent learned** to stick with coordinates after selectors failed
|
|
11
|
-
|
|
12
|
-
### ❌ What Didn't Work:
|
|
13
|
-
|
|
14
|
-
**Agent hit max iterations (8) without marking "complete"**
|
|
15
|
-
|
|
16
|
-
## Detailed Step 6 Flow:
|
|
17
|
-
|
|
18
|
-
```
|
|
19
|
-
Iteration 1: Selector attempt → Timeout ❌
|
|
20
|
-
Iteration 2: Selector attempt → Timeout ❌
|
|
21
|
-
Iteration 3: Selector attempt → Timeout ❌
|
|
22
|
-
Iteration 4: 🎯 COORDINATE MODE → Click (87.5%, 23.438%) → ✅ Success
|
|
23
|
-
Iteration 5: Repeat coordinate → ✅ Success
|
|
24
|
-
Iteration 6: Repeat coordinate → ✅ Success (?)
|
|
25
|
-
Iteration 7: Repeat coordinate → ✅ Success
|
|
26
|
-
Iteration 8: Repeat coordinate → ✅ Success
|
|
27
|
-
Result: ⚠️ Max iterations → system_limit
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
## Root Cause Analysis:
|
|
31
|
-
|
|
32
|
-
### Problem: **No Goal Verification After Coordinate Success**
|
|
33
|
-
|
|
34
|
-
**With selectors:**
|
|
35
|
-
```typescript
|
|
36
|
-
await page.getByRole('button').click();
|
|
37
|
-
// Can verify: await expect(button).toHaveState('pressed')
|
|
38
|
-
// Can check: New elements appeared, URL changed, etc.
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
**With coordinates:**
|
|
42
|
-
```typescript
|
|
43
|
-
await page.mouse.click(1120, 169);
|
|
44
|
-
// ❓ Did it work? No element reference!
|
|
45
|
-
// ❓ How to verify? Can't check button state
|
|
46
|
-
// ❓ What changed? Need to inspect DOM/screenshot
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
### Why Agent Kept Retrying:
|
|
50
|
-
|
|
51
|
-
**Agent's reasoning (iterations 5-8):**
|
|
52
|
-
- "Coordinate click succeeded (executed without error)"
|
|
53
|
-
- "But I don't know if goal was achieved"
|
|
54
|
-
- "Step says 'Click on New' - did the New form open?"
|
|
55
|
-
- "I should try again to be sure..."
|
|
56
|
-
- → **Loops until max iterations**
|
|
57
|
-
|
|
58
|
-
## Solutions to Consider:
|
|
59
|
-
|
|
60
|
-
### Option 1: **Trust Coordinate Success** (Simple)
|
|
61
|
-
After coordinate click succeeds:
|
|
62
|
-
- Wait 500ms for UI response
|
|
63
|
-
- Mark status="complete" automatically
|
|
64
|
-
- Assume click worked (trust the coordinates)
|
|
65
|
-
|
|
66
|
-
```typescript
|
|
67
|
-
if (coordinateAction && coordResult.allSucceeded) {
|
|
68
|
-
await page.waitForTimeout(500); // Let UI respond
|
|
69
|
-
return { status: 'complete', reasoning: 'Coordinate click succeeded' };
|
|
70
|
-
}
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
**Pros**: Simple, fast
|
|
74
|
-
**Cons**: No verification of actual goal achievement
|
|
75
|
-
|
|
76
|
-
### Option 2: **Visual Verification** (Better)
|
|
77
|
-
After coordinate click:
|
|
78
|
-
- Wait 500ms
|
|
79
|
-
- Take screenshot
|
|
80
|
-
- Compare before/after
|
|
81
|
-
- If changed → complete, else → retry with different coords
|
|
82
|
-
|
|
83
|
-
```typescript
|
|
84
|
-
const beforeScreenshot = await page.screenshot();
|
|
85
|
-
await page.mouse.click(x, y);
|
|
86
|
-
await page.waitForTimeout(500);
|
|
87
|
-
const afterScreenshot = await page.screenshot();
|
|
88
|
-
if (screenshotsAreDifferent(before, after)) {
|
|
89
|
-
return { status: 'complete' };
|
|
90
|
-
}
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
**Pros**: Validates something changed
|
|
94
|
-
**Cons**: Slower, more LLM calls
|
|
95
|
-
|
|
96
|
-
### Option 3: **DOM Change Detection** (Balanced)
|
|
97
|
-
After coordinate click:
|
|
98
|
-
- Capture DOM snapshot before
|
|
99
|
-
- Click coordinates
|
|
100
|
-
- Capture DOM snapshot after
|
|
101
|
-
- If new elements/navigation → complete
|
|
102
|
-
|
|
103
|
-
```typescript
|
|
104
|
-
const beforeUrl = page.url();
|
|
105
|
-
const beforeElements = await getEnhancedPageInfo(page);
|
|
106
|
-
await page.mouse.click(x, y);
|
|
107
|
-
await page.waitForTimeout(500);
|
|
108
|
-
const afterUrl = page.url();
|
|
109
|
-
const afterElements = await getEnhancedPageInfo(page);
|
|
110
|
-
|
|
111
|
-
if (afterUrl !== beforeUrl || afterElements.count !== beforeElements.count) {
|
|
112
|
-
return { status: 'complete', reasoning: 'Page state changed after coordinate click' };
|
|
113
|
-
}
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
**Pros**: Fast, objective verification
|
|
117
|
-
**Cons**: Might miss subtle changes (modal opens without URL/element count change)
|
|
118
|
-
|
|
119
|
-
### Option 4: **Prompt Guidance** (Immediate)
|
|
120
|
-
Update prompt to tell agent:
|
|
121
|
-
"After coordinate click succeeds, mark status='complete' unless you can clearly verify it failed"
|
|
122
|
-
|
|
123
|
-
**Pros**: No code changes
|
|
124
|
-
**Cons**: Relies on LLM judgment
|
|
125
|
-
|
|
126
|
-
## Recommendation:
|
|
127
|
-
|
|
128
|
-
**Hybrid approach:**
|
|
129
|
-
1. **Immediate** (Prompt): Tell agent to trust coordinate success
|
|
130
|
-
2. **Phase 2** (Code): Add DOM change detection for validation
|
|
131
|
-
|
|
132
|
-
## Current Status:
|
|
133
|
-
|
|
134
|
-
- ✅ Coordinate fallback works technically
|
|
135
|
-
- ✅ Physical clicks succeed
|
|
136
|
-
- ❌ Agent doesn't know when to stop
|
|
137
|
-
- 🔧 Need completion detection logic
|
|
138
|
-
|
|
139
|
-
## Test Results Summary:
|
|
140
|
-
|
|
141
|
-
**Steps 1-5**: ✅ All completed successfully
|
|
142
|
-
**Step 6**: ⚠️ Coordinates worked but hit max iterations (no completion detection)
|
|
143
|
-
**Overall**: Coordinate mode is functional but needs completion logic
|
|
144
|
-
|
|
@@ -1,253 +0,0 @@
|
|
|
1
|
-
# Credit Usage Callback Architecture
|
|
2
|
-
|
|
3
|
-
## Summary
|
|
4
|
-
Added callback-based credit usage reporting to allow server-side integration to update DB directly without axios calls, while client-side continues using axios calls to backend API.
|
|
5
|
-
|
|
6
|
-
## Architecture
|
|
7
|
-
|
|
8
|
-
### Callback-First Approach
|
|
9
|
-
|
|
10
|
-
```typescript
|
|
11
|
-
export interface CreditUsage {
|
|
12
|
-
credits: number;
|
|
13
|
-
usageReason: CreditUsageReason;
|
|
14
|
-
jobId?: string;
|
|
15
|
-
timestamp: number;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export type CreditUsageCallback = (usage: CreditUsage) => void | Promise<void>;
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
### Behavior
|
|
22
|
-
|
|
23
|
-
```typescript
|
|
24
|
-
async reportCreditUsage(credits, usageReason, jobId) {
|
|
25
|
-
const creditUsage = { credits, usageReason, jobId, timestamp: Date.now() };
|
|
26
|
-
|
|
27
|
-
// 1. If callback provided: Use callback (SERVER-SIDE)
|
|
28
|
-
if (this.creditUsageCallback) {
|
|
29
|
-
await this.creditUsageCallback(creditUsage);
|
|
30
|
-
return {}; // No axios call needed
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// 2. No callback but auth configured: Use axios (CLIENT-SIDE)
|
|
34
|
-
if (this.authConfig) {
|
|
35
|
-
await axios.post(`${backend}/localagent/insert_credit_usage`, ...);
|
|
36
|
-
return response.data;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// 3. No callback and no auth: Development mode
|
|
40
|
-
return {};
|
|
41
|
-
}
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
## Usage Scenarios
|
|
45
|
-
|
|
46
|
-
### Server-Side (scriptservice)
|
|
47
|
-
|
|
48
|
-
```typescript
|
|
49
|
-
import { TestChimpService, CreditUsage, CreditUsageReason } from 'testchimp-runner-core';
|
|
50
|
-
|
|
51
|
-
const service = new TestChimpService(
|
|
52
|
-
fileHandler,
|
|
53
|
-
undefined, // NO auth config
|
|
54
|
-
backendUrl,
|
|
55
|
-
maxWorkers,
|
|
56
|
-
llmProvider,
|
|
57
|
-
progressReporter,
|
|
58
|
-
orchestratorOptions,
|
|
59
|
-
async (creditUsage: CreditUsage) => {
|
|
60
|
-
// Update DB directly - NO axios calls
|
|
61
|
-
await db.insertCreditUsage({
|
|
62
|
-
credits: creditUsage.credits,
|
|
63
|
-
usageReason: creditUsage.usageReason,
|
|
64
|
-
jobId: creditUsage.jobId,
|
|
65
|
-
timestamp: creditUsage.timestamp
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
);
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
**Result:**
|
|
72
|
-
- ✅ Callback called → DB updated directly
|
|
73
|
-
- ❌ No axios calls (no auth configured)
|
|
74
|
-
- ✅ Full control over DB updates
|
|
75
|
-
|
|
76
|
-
### Client-Side (vs-extension, github-action)
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
import { TestChimpService } from 'testchimp-runner-core';
|
|
80
|
-
|
|
81
|
-
const service = new TestChimpService(
|
|
82
|
-
fileHandler,
|
|
83
|
-
authConfig, // Auth configured
|
|
84
|
-
backendUrl,
|
|
85
|
-
maxWorkers,
|
|
86
|
-
llmProvider,
|
|
87
|
-
progressReporter,
|
|
88
|
-
orchestratorOptions
|
|
89
|
-
// NO callback - uses axios
|
|
90
|
-
);
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
**Result:**
|
|
94
|
-
- ❌ No callback provided
|
|
95
|
-
- ✅ Axios call made to backend API (because auth configured)
|
|
96
|
-
- ✅ Backend handles DB update
|
|
97
|
-
|
|
98
|
-
### Development Mode (local testing)
|
|
99
|
-
|
|
100
|
-
```typescript
|
|
101
|
-
const service = new TestChimpService(
|
|
102
|
-
fileHandler
|
|
103
|
-
// No auth, no callback
|
|
104
|
-
);
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
**Result:**
|
|
108
|
-
- ❌ No callback
|
|
109
|
-
- ❌ No auth
|
|
110
|
-
- ⚠️ Warning logged: "Credit usage not tracked"
|
|
111
|
-
- ✅ Continues without error
|
|
112
|
-
|
|
113
|
-
## API
|
|
114
|
-
|
|
115
|
-
### Constructor
|
|
116
|
-
```typescript
|
|
117
|
-
new TestChimpService(
|
|
118
|
-
fileHandler?,
|
|
119
|
-
authConfig?,
|
|
120
|
-
backendUrl?,
|
|
121
|
-
maxWorkers?,
|
|
122
|
-
llmProvider?,
|
|
123
|
-
progressReporter?,
|
|
124
|
-
orchestratorOptions?,
|
|
125
|
-
creditUsageCallback? // NEW
|
|
126
|
-
)
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
### Method
|
|
130
|
-
```typescript
|
|
131
|
-
service.setCreditUsageCallback((creditUsage) => {
|
|
132
|
-
// Handle credit usage in your system
|
|
133
|
-
console.log(`Used ${creditUsage.credits} credits for ${creditUsage.usageReason}`);
|
|
134
|
-
});
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
## Exported Types
|
|
138
|
-
|
|
139
|
-
```typescript
|
|
140
|
-
export {
|
|
141
|
-
CreditUsageCallback, // Type for callback function
|
|
142
|
-
CreditUsage, // Interface for usage data
|
|
143
|
-
CreditUsageReason // Enum: SCRIPT_GENERATE, TEST_REPAIR, etc.
|
|
144
|
-
};
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
## Benefits
|
|
148
|
-
|
|
149
|
-
### For Server-Side
|
|
150
|
-
1. **No Network Calls** - Direct DB updates via callback
|
|
151
|
-
2. **Full Control** - Custom logic for credit tracking
|
|
152
|
-
3. **Performance** - No HTTP round-trip overhead
|
|
153
|
-
4. **Reliability** - No network failures
|
|
154
|
-
|
|
155
|
-
### For Client-Side
|
|
156
|
-
1. **Backward Compatible** - Existing code works unchanged
|
|
157
|
-
2. **Centralized** - Backend API handles all credit logic
|
|
158
|
-
3. **Simple** - Just configure auth, no callback needed
|
|
159
|
-
|
|
160
|
-
### For Both
|
|
161
|
-
1. **Flexible** - Each consumer decides how to handle credits
|
|
162
|
-
2. **Testable** - Can mock callbacks easily
|
|
163
|
-
3. **Observable** - Callback provides visibility into credit usage
|
|
164
|
-
|
|
165
|
-
## Implementation Notes
|
|
166
|
-
|
|
167
|
-
### Preservation Across Service Recreations
|
|
168
|
-
|
|
169
|
-
Credit callback is stored and reapplied when services are recreated:
|
|
170
|
-
|
|
171
|
-
```typescript
|
|
172
|
-
// Store in TestChimpService
|
|
173
|
-
private creditUsageCallback?: CreditUsageCallback;
|
|
174
|
-
|
|
175
|
-
// Pass to CreditUsageService on every recreation
|
|
176
|
-
this.creditUsageService = new CreditUsageService(
|
|
177
|
-
this.authConfig,
|
|
178
|
-
this.backendUrl,
|
|
179
|
-
this.creditUsageCallback // Always preserved
|
|
180
|
-
);
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
### Error Handling
|
|
184
|
-
|
|
185
|
-
**Callback-based (server-side):**
|
|
186
|
-
- Callback error → Throws (critical for DB updates)
|
|
187
|
-
|
|
188
|
-
**Axios-based (client-side):**
|
|
189
|
-
- Axios error → Throws (critical for credit tracking)
|
|
190
|
-
|
|
191
|
-
**Development mode:**
|
|
192
|
-
- No tracking → Logs warning, continues
|
|
193
|
-
|
|
194
|
-
## Example: Server-Side Integration
|
|
195
|
-
|
|
196
|
-
```typescript
|
|
197
|
-
// In scriptservice
|
|
198
|
-
import { TestChimpService, CreditUsage, CreditUsageReason } from 'testchimp-runner-core';
|
|
199
|
-
|
|
200
|
-
class ScriptService {
|
|
201
|
-
private testChimpService: TestChimpService;
|
|
202
|
-
|
|
203
|
-
constructor() {
|
|
204
|
-
this.testChimpService = new TestChimpService(
|
|
205
|
-
new CustomFileHandler(),
|
|
206
|
-
undefined, // No auth - server-side doesn't need it
|
|
207
|
-
'http://localhost:3000', // Internal backend URL
|
|
208
|
-
5,
|
|
209
|
-
new CustomLLMProvider(), // Server has its own LLM provider
|
|
210
|
-
customProgressReporter,
|
|
211
|
-
{ useOrchestrator: true },
|
|
212
|
-
async (creditUsage: CreditUsage) => {
|
|
213
|
-
// Direct DB update - no HTTP calls
|
|
214
|
-
await this.creditRepository.insert({
|
|
215
|
-
userId: this.getCurrentUserId(),
|
|
216
|
-
credits: creditUsage.credits,
|
|
217
|
-
reason: creditUsage.usageReason,
|
|
218
|
-
jobId: creditUsage.jobId,
|
|
219
|
-
timestamp: new Date(creditUsage.timestamp)
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
## Files Modified
|
|
228
|
-
|
|
229
|
-
1. `/src/credit-usage-service.ts` - Added callback support, callback-first logic
|
|
230
|
-
2. `/src/index.ts` - Accept credit callback in constructor, expose `setCreditUsageCallback()`, preserve across recreations
|
|
231
|
-
3. Exported types: `CreditUsage`, `CreditUsageCallback`, `CreditUsageReason`
|
|
232
|
-
|
|
233
|
-
## Testing
|
|
234
|
-
|
|
235
|
-
### Server-Side
|
|
236
|
-
1. Set credit callback
|
|
237
|
-
2. Generate script
|
|
238
|
-
3. Verify callback called with correct credit data
|
|
239
|
-
4. Verify NO axios calls made
|
|
240
|
-
|
|
241
|
-
### Client-Side
|
|
242
|
-
1. Configure auth (no callback)
|
|
243
|
-
2. Generate script
|
|
244
|
-
3. Verify axios call made to backend
|
|
245
|
-
4. Verify backend receives credit data
|
|
246
|
-
|
|
247
|
-
## Backward Compatibility
|
|
248
|
-
|
|
249
|
-
✅ Fully backward compatible
|
|
250
|
-
- Existing consumers work unchanged
|
|
251
|
-
- Optional callback parameter
|
|
252
|
-
- Existing axios behavior preserved for client-side
|
|
253
|
-
|