@zibby/core 0.1.0

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.
Files changed (93) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +147 -0
  3. package/package.json +94 -0
  4. package/src/agents/base.js +361 -0
  5. package/src/constants.js +47 -0
  6. package/src/enrichment/base.js +49 -0
  7. package/src/enrichment/enrichers/accessibility-enricher.js +197 -0
  8. package/src/enrichment/enrichers/dom-enricher.js +171 -0
  9. package/src/enrichment/enrichers/page-state-enricher.js +129 -0
  10. package/src/enrichment/enrichers/position-enricher.js +67 -0
  11. package/src/enrichment/index.js +96 -0
  12. package/src/enrichment/mcp-integration.js +149 -0
  13. package/src/enrichment/mcp-ref-enricher.js +78 -0
  14. package/src/enrichment/pipeline.js +192 -0
  15. package/src/enrichment/trace-text-enricher.js +115 -0
  16. package/src/framework/AGENTS.md +98 -0
  17. package/src/framework/agents/base.js +72 -0
  18. package/src/framework/agents/claude-strategy.js +278 -0
  19. package/src/framework/agents/cursor-strategy.js +459 -0
  20. package/src/framework/agents/index.js +105 -0
  21. package/src/framework/agents/utils/cursor-output-formatter.js +67 -0
  22. package/src/framework/agents/utils/openai-proxy-formatter.js +249 -0
  23. package/src/framework/code-generator.js +301 -0
  24. package/src/framework/constants.js +33 -0
  25. package/src/framework/context-loader.js +101 -0
  26. package/src/framework/function-bridge.js +78 -0
  27. package/src/framework/function-skill-registry.js +20 -0
  28. package/src/framework/graph-compiler.js +342 -0
  29. package/src/framework/graph.js +610 -0
  30. package/src/framework/index.js +28 -0
  31. package/src/framework/node-registry.js +163 -0
  32. package/src/framework/node.js +259 -0
  33. package/src/framework/output-parser.js +71 -0
  34. package/src/framework/skill-registry.js +55 -0
  35. package/src/framework/state-utils.js +52 -0
  36. package/src/framework/state.js +67 -0
  37. package/src/framework/tool-resolver.js +65 -0
  38. package/src/index.js +342 -0
  39. package/src/runtime/generation/base.js +46 -0
  40. package/src/runtime/generation/index.js +70 -0
  41. package/src/runtime/generation/mcp-ref-strategy.js +197 -0
  42. package/src/runtime/generation/stable-id-strategy.js +170 -0
  43. package/src/runtime/stable-id-runtime.js +248 -0
  44. package/src/runtime/verification/base.js +44 -0
  45. package/src/runtime/verification/index.js +67 -0
  46. package/src/runtime/verification/playwright-json-strategy.js +119 -0
  47. package/src/runtime/zibby-runtime.js +299 -0
  48. package/src/sync/index.js +2 -0
  49. package/src/sync/uploader.js +29 -0
  50. package/src/tools/run-playwright-test.js +158 -0
  51. package/src/utils/adf-converter.js +68 -0
  52. package/src/utils/ast-utils.js +37 -0
  53. package/src/utils/ci-setup.js +124 -0
  54. package/src/utils/cursor-utils.js +71 -0
  55. package/src/utils/logger.js +144 -0
  56. package/src/utils/mcp-config-writer.js +115 -0
  57. package/src/utils/node-schema-parser.js +522 -0
  58. package/src/utils/post-process-events.js +55 -0
  59. package/src/utils/result-handler.js +102 -0
  60. package/src/utils/ripple-effect.js +84 -0
  61. package/src/utils/selector-generator.js +239 -0
  62. package/src/utils/streaming-parser.js +387 -0
  63. package/src/utils/test-post-processor.js +211 -0
  64. package/src/utils/timeline.js +217 -0
  65. package/src/utils/trace-parser.js +325 -0
  66. package/src/utils/video-organizer.js +91 -0
  67. package/templates/browser-test-automation/README.md +114 -0
  68. package/templates/browser-test-automation/graph.js +54 -0
  69. package/templates/browser-test-automation/nodes/execute-live.js +250 -0
  70. package/templates/browser-test-automation/nodes/generate-script.js +77 -0
  71. package/templates/browser-test-automation/nodes/index.js +3 -0
  72. package/templates/browser-test-automation/nodes/preflight.js +59 -0
  73. package/templates/browser-test-automation/nodes/utils.js +154 -0
  74. package/templates/browser-test-automation/result-handler.js +286 -0
  75. package/templates/code-analysis/graph.js +72 -0
  76. package/templates/code-analysis/index.js +18 -0
  77. package/templates/code-analysis/nodes/analyze-ticket-node.js +204 -0
  78. package/templates/code-analysis/nodes/create-pr-node.js +175 -0
  79. package/templates/code-analysis/nodes/finalize-node.js +118 -0
  80. package/templates/code-analysis/nodes/generate-code-node.js +425 -0
  81. package/templates/code-analysis/nodes/generate-test-cases-node.js +376 -0
  82. package/templates/code-analysis/nodes/services/prMetaService.js +86 -0
  83. package/templates/code-analysis/nodes/setup-node.js +142 -0
  84. package/templates/code-analysis/prompts/analyze-ticket.md +181 -0
  85. package/templates/code-analysis/prompts/generate-code.md +33 -0
  86. package/templates/code-analysis/prompts/generate-test-cases.md +110 -0
  87. package/templates/code-analysis/state.js +40 -0
  88. package/templates/code-implementation/graph.js +35 -0
  89. package/templates/code-implementation/index.js +7 -0
  90. package/templates/code-implementation/state.js +14 -0
  91. package/templates/global-setup.js +56 -0
  92. package/templates/index.js +94 -0
  93. package/templates/register-nodes.js +24 -0
@@ -0,0 +1,376 @@
1
+ /**
2
+ * Generate Test Cases Node - Generate human-readable test case specifications
3
+ * Used by: analysisGraph, implementationGraph
4
+ *
5
+ * Output: Plain-text test specifications for AI interpretation
6
+ * - High-level test steps in natural language
7
+ * - Includes credentials, prerequisites, and expected results
8
+ * - Can be executed by AI agents (like MCP) and converted to Playwright
9
+ */
10
+
11
+ import { invokeAgent } from '@zibby/core';
12
+ import { z } from 'zod';
13
+ import { randomBytes } from 'crypto';
14
+ import { adfToText } from '../../../src/utils/adf-converter.js';
15
+
16
+ // Generate a simple unique ID
17
+ const generateId = () => randomBytes(8).toString('hex');
18
+
19
+ // Schema for plain-text test specification
20
+ const TestSpecificationSchema = z.object({
21
+ id: z.string().optional(),
22
+ title: z.string().describe('Test name (e.g., "Login Functionality", "Create New User")'),
23
+ application: z.string().describe('Application name being tested'),
24
+ url: z.string().describe('Base URL of the application'),
25
+ testerRole: z.string().describe('Role performing the test (e.g., "Admin user", "End user")'),
26
+ feature: z.string().describe('Feature being tested (e.g., "Authentication - Login")'),
27
+ ticketId: z.string().describe('Associated ticket ID'),
28
+ testCredentials: z.array(z.object({
29
+ field: z.string().describe('Credential field name (e.g., "Email", "Password", "API Key")'),
30
+ value: z.string().describe('Credential value')
31
+ })).optional().describe('Test credentials needed (if any)'),
32
+ testObjective: z.string().describe('One clear sentence: what this test verifies'),
33
+ testSteps: z.array(z.string()).describe('Plain English steps - natural language instructions (e.g., "Navigate to /login", "Enter email: test@example.com", "Click the submit button")'),
34
+ expectedResults: z.array(z.string()).describe('What should happen - bullet points of expected outcomes'),
35
+ status: z.string().default('Ready for automation').describe('Test status'),
36
+ priority: z.enum(['Critical', 'High', 'Medium', 'Low']).describe('Test priority'),
37
+ category: z.string().describe('Test category (e.g., "Smoke Test", "Regression", "Integration")')
38
+ });
39
+
40
+ const CategoryCountSchema = z.object({
41
+ category: z.string().describe('Category name'),
42
+ count: z.number().describe('Number of tests in this category')
43
+ });
44
+
45
+ // Schema for AI agent output (what invokeAgent validates)
46
+ const TestSuiteSchema = z.object({
47
+ testCases: z.array(TestSpecificationSchema).describe('Array of test specifications'),
48
+ summary: z.object({
49
+ totalTests: z.number(),
50
+ byCategory: z.array(CategoryCountSchema).optional(),
51
+ coverageNotes: z.string().optional()
52
+ }).optional()
53
+ });
54
+
55
+ // Node output schema (what this node returns to state)
56
+ const GenerateTestCasesOutputSchema = z.object({
57
+ success: z.boolean(),
58
+ tests: z.object({
59
+ structured: z.object({
60
+ testCases: z.array(z.any()),
61
+ summary: z.object({
62
+ totalTests: z.number(),
63
+ byCategory: z.array(CategoryCountSchema).optional()
64
+ }).optional()
65
+ }),
66
+ timestamp: z.string()
67
+ }).nullable()
68
+ });
69
+
70
+ export const generateTestCasesNode = {
71
+ name: 'generate_test_cases',
72
+
73
+ // Output schema defines what this node returns to state
74
+ outputSchema: GenerateTestCasesOutputSchema,
75
+
76
+ execute: async (state) => {
77
+ console.log('\n🧪 Generating test cases...');
78
+
79
+ const { ticketContext, workspace, model, nodeConfigs = {} } = state;
80
+ const aiModel = model || ticketContext.model || 'auto';
81
+ const codeImpl = state.generate_code?.codeImplementation ||
82
+ state.implement_code?.codeImplementation;
83
+
84
+ if (!codeImpl || !codeImpl.diff) {
85
+ console.log('āš ļø No code changes detected, skipping test generation');
86
+ return {
87
+ success: true,
88
+ tests: null
89
+ };
90
+ }
91
+
92
+ // Get node config for this node (if user configured it)
93
+ const nodeConfig = nodeConfigs.generate_test_cases || {};
94
+ console.log('šŸ“‹ Node config:', JSON.stringify(nodeConfig, null, 2));
95
+
96
+ // Build test generation prompt with workspace path for codebase exploration
97
+ const testPrompt = buildTestGenerationPrompt(ticketContext, codeImpl, workspace, nodeConfig);
98
+
99
+ console.log(`šŸš€ Running AI Agent to generate tests with model: ${aiModel}...`);
100
+
101
+ const agentResult = await invokeAgent(testPrompt, {
102
+ state,
103
+ model: aiModel,
104
+ schema: TestSuiteSchema
105
+ });
106
+
107
+ const validatedOutput = agentResult?.structured || agentResult;
108
+
109
+ // Convert structured AI output → simple { id, title, priority, content } format
110
+ const testCases = (validatedOutput?.testCases || []).map(test => ({
111
+ id: generateId(),
112
+ title: test.title,
113
+ priority: test.priority || 'Medium',
114
+ content: renderTestCaseToText(test),
115
+ status: 'pending'
116
+ }));
117
+
118
+ console.log(`āœ… Generated ${testCases.length} test cases`);
119
+
120
+ return {
121
+ success: true,
122
+ tests: {
123
+ structured: {
124
+ testCases,
125
+ summary: validatedOutput.summary || {
126
+ totalTests: testCases.length,
127
+ byCategory: Object.entries(
128
+ testCases.reduce((acc, tc) => {
129
+ acc[tc.category] = (acc[tc.category] || 0) + 1;
130
+ return acc;
131
+ }, {})
132
+ ).map(([category, count]) => ({ category, count }))
133
+ }
134
+ },
135
+ timestamp: new Date().toISOString()
136
+ }
137
+ };
138
+ }
139
+ };
140
+
141
+ function renderTestCaseToText(test) {
142
+ const lines = [];
143
+
144
+ if (test.testObjective) {
145
+ lines.push('WHAT THIS TESTS');
146
+ lines.push(test.testObjective);
147
+ lines.push('');
148
+ }
149
+
150
+ if (test.url) {
151
+ lines.push('START URL');
152
+ lines.push(test.url);
153
+ lines.push('');
154
+ }
155
+
156
+ if (test.testCredentials && test.testCredentials.length > 0) {
157
+ lines.push('TEST CREDENTIALS');
158
+ test.testCredentials.forEach(cred => {
159
+ lines.push(`• ${cred.field}: ${cred.value}`);
160
+ });
161
+ lines.push('');
162
+ }
163
+
164
+ if (test.testSteps && test.testSteps.length > 0) {
165
+ lines.push('TEST STEPS');
166
+ test.testSteps.forEach((step, i) => {
167
+ lines.push(`${i + 1}. ${step}`);
168
+ });
169
+ lines.push('');
170
+ }
171
+
172
+ if (test.expectedResults && test.expectedResults.length > 0) {
173
+ lines.push('EXPECTED RESULTS');
174
+ test.expectedResults.forEach(result => {
175
+ lines.push(`• ${result}`);
176
+ });
177
+ }
178
+
179
+ return lines.join('\n').trim();
180
+ }
181
+
182
+ function buildTestGenerationPrompt(ticketContext, codeImpl, workspace, nodeConfig = {}) {
183
+ // Extract user-provided context (test URL, admin credentials, etc.)
184
+ const extractContext = nodeConfig.extractContext || {};
185
+ let contextSection = '';
186
+ if (extractContext.testBaseUrl || extractContext.adminUsername || extractContext.testAccountUsername) {
187
+ contextSection = `
188
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
189
+ PROVIDED TEST CONTEXT (USE THESE IN TEST CASES)
190
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
191
+
192
+ ${extractContext.testBaseUrl ? `Test Base URL: ${extractContext.testBaseUrl}` : ''}
193
+ ${extractContext.adminUsername ? `Admin Username: ${extractContext.adminUsername}` : ''}
194
+ ${extractContext.adminPassword ? `Admin Password: ${extractContext.adminPassword}` : ''}
195
+ ${extractContext.testAccountUsername ? `Test Account Username: ${extractContext.testAccountUsername}` : ''}
196
+ ${extractContext.testAccountPassword ? `Test Account Password: ${extractContext.testAccountPassword}` : ''}
197
+
198
+ **IMPORTANT**: Use these exact values in your test cases. Do NOT make up URLs or credentials.
199
+
200
+ `;
201
+ }
202
+
203
+ return `You are an AI test automation engineer creating human-readable test specifications.
204
+
205
+ WORKSPACE: ${workspace}
206
+
207
+ TICKET: ${ticketContext.ticketKey} - ${ticketContext.summary}
208
+ ${ticketContext.description ? `Description: ${typeof ticketContext.description === 'object' ? adfToText(ticketContext.description) : ticketContext.description}` : ''}
209
+
210
+ MODIFIED FILES:
211
+ ${codeImpl.changedFiles.map(f => ` - ${f}`).join('\n')}
212
+
213
+ FULL CODE DIFF:
214
+ \`\`\`diff
215
+ ${codeImpl.diff}
216
+ \`\`\`
217
+
218
+ ${contextSection}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
219
+
220
+ YOUR JOB: Generate 4-8 test specifications in plain-text format that AI agents can interpret.
221
+
222
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
223
+ STEP 1: EXPLORE THE CODEBASE (MANDATORY - DO NOT SKIP)
224
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
225
+
226
+ YOU MUST explore the codebase to understand the application structure BEFORE writing tests.
227
+
228
+ REQUIRED EXPLORATION:
229
+
230
+ 1. **Find and Read Routing Files**
231
+ - Search the workspace for routing files: look for common names like App, routes, router, main, index files in .js/.jsx/.ts/.tsx format
232
+ - Check common locations: src/, app/, client/, web/, components/, pages/ directories
233
+ - Identify ALL routes and their URL patterns
234
+ - Pay special attention to routes with parameters (e.g., /stores/:storeId/products, /users/:userId/orders, /items/:itemId)
235
+
236
+ 2. **Understand Route Parameters**
237
+ - Dynamic segments in routes (marked with :paramName or {paramName}) are PLACEHOLDERS
238
+ - You MUST include navigation steps to select/create that resource first
239
+ - Example: To reach /stores/:storeId/products, you must first navigate to /stores list and select a specific store
240
+ - Example: To reach /orders/:orderId/details, you must first navigate to /orders list and click on a specific order
241
+
242
+ 3. **Map Navigation Flow**
243
+ - Understand the hierarchy: authentication → landing/home → resource lists → individual resources → sub-features
244
+ - Find what links, buttons, tabs exist on each page to navigate to the next level
245
+ - Read page components to understand the UI structure and navigation elements
246
+
247
+ 4. **Find Authentication**
248
+ - Check for login/signin routes, auth components, protected routes, authentication guards
249
+ - Look for HOCs, route wrappers, or middleware that handle authentication
250
+ - Understand if authentication is required and what the login flow looks like
251
+
252
+ 5. **Identify Page Components**
253
+ - Find the actual page/view components referenced in routes
254
+ - Read their code to understand imports, props, state management, and structure
255
+ - Understand what data they load, what APIs they call, and what user actions they support
256
+
257
+ CRITICAL RULES:
258
+ - Routes with dynamic parameters (like :id, :userId, :orderId) REQUIRE parent resource selection
259
+ - You CANNOT jump directly to a URL with an ID - you must navigate through the parent list and SELECT that item
260
+ - Base your test steps on ACTUAL navigation paths you discover in the codebase
261
+ - If you don't explore properly, your tests will have WRONG navigation and be unusable by AI agents
262
+
263
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
264
+ STEP 2: GENERATE TEST SPECIFICATIONS
265
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
266
+
267
+ For each test, provide:
268
+ - **title**: Test name (e.g., "Filter Orders by Status")
269
+ - **application**: Application name (infer from package.json or codebase)
270
+ - **url**: Base application URL (usually http://localhost:3000 or staging URL if found)
271
+ - **testerRole**: Who performs this test (e.g., "Admin user", "End user", "Guest")
272
+ - **feature**: Feature being tested (match the modified files)
273
+ - **ticketId**: Use the ticket key provided above
274
+ - **testCredentials**: Array of credentials needed (if authentication required):
275
+ - field: "Email" / "Password" / "Username" / "API Key", etc.
276
+ - value: Use realistic test data (e.g., "admin@example.com", "TestPass123")
277
+ - **testObjective**: One clear sentence describing what this test verifies
278
+ - **testSteps**: Plain English steps - MUST match actual navigation flow you discovered. Example:
279
+ * "Navigate to login page http://localhost:3000/login"
280
+ * "Enter email: admin@example.com"
281
+ * "Enter password: TestPass123"
282
+ * "Click the Sign In button"
283
+ * "Navigate to Projects page http://localhost:3000/projects"
284
+ * "Click on project 'Test Project 1'"
285
+ * "Navigate to Orders tab"
286
+ * "Click the status filter dropdown"
287
+ * "Select 'Completed' from the filter"
288
+ * "Verify only completed orders are displayed"
289
+ DON'T use technical terms like "action", "target", "selector" - just natural language
290
+ - **expectedResults**: Bullet points of what should happen
291
+ - **priority**: Critical, High, Medium, or Low
292
+ - **category**: Test category (e.g., "Smoke Test", "Regression", "Integration")
293
+
294
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
295
+ EXAMPLE: Multi-Level Navigation (Generic Restaurant Ordering App)
296
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
297
+
298
+ Assume this routing structure (discovered from App.jsx):
299
+ /login
300
+ /restaurants
301
+ /restaurants/:restaurantId/menu
302
+ /restaurants/:restaurantId/menu/:itemId/details
303
+
304
+ CORRECT Test Steps (follows nested structure):
305
+ {
306
+ "title": "Add Menu Item to Cart with Size Selection",
307
+ "application": "Restaurant Ordering System",
308
+ "url": "http://localhost:3000",
309
+ "testerRole": "Customer",
310
+ "feature": "Menu - Item Size Selection",
311
+ "ticketId": "REST-456",
312
+ "testCredentials": [
313
+ { "field": "Email", "value": "customer@email.com" },
314
+ { "field": "Password", "value": "Order123" }
315
+ ],
316
+ "testObjective": "Verify customers can select different sizes when adding menu items to cart",
317
+ "testSteps": [
318
+ "Navigate to login page http://localhost:3000/login",
319
+ "Enter email: customer@email.com",
320
+ "Enter password: Order123",
321
+ "Click the Sign In button",
322
+ "Navigate to Restaurants page http://localhost:3000/restaurants",
323
+ "Click on restaurant 'Downtown Pizza'",
324
+ "Wait for menu to load",
325
+ "Scroll to 'Beverages' section",
326
+ "Click on menu item 'Lemonade'",
327
+ "Wait for item details to display",
328
+ "Click the size dropdown",
329
+ "Select 'Large' size option",
330
+ "Verify price updates to large size price",
331
+ "Click Add to Cart button",
332
+ "Verify item added to cart with Large size"
333
+ ],
334
+ "expectedResults": [
335
+ "Size dropdown displays all available sizes (Small, Medium, Large)",
336
+ "Price updates when different size is selected",
337
+ "Item is added to cart with correct size",
338
+ "Cart icon shows updated item count",
339
+ "No errors displayed during the process"
340
+ ],
341
+ "priority": "High",
342
+ "category": "Smoke Test"
343
+ }
344
+
345
+ WRONG Test Steps (skips required navigation):
346
+ āŒ "Navigate to Menu page" (Too vague - which restaurant?)
347
+ āŒ "Go to /restaurants/123/menu" (Can't directly use IDs without selection)
348
+ āŒ "Select item from menu" (Need to navigate to restaurant first)
349
+
350
+ CORRECT Test Steps (shows proper navigation):
351
+ āœ… "Navigate to Restaurants page http://localhost:3000/restaurants"
352
+ āœ… "Click on restaurant 'Downtown Pizza'"
353
+ āœ… "Wait for menu to load"
354
+ āœ… "Click on menu item 'Lemonade'"
355
+
356
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
357
+ FINAL REMINDERS
358
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
359
+
360
+ 1. ALWAYS explore routing files FIRST
361
+ 2. NEVER skip navigation steps - include full path from login to feature
362
+ 3. If routes have :parameters, include parent resource selection
363
+ 4. Use ACTUAL URLs and paths from the codebase
364
+ 5. Write plain English steps - no technical jargon
365
+ 6. Base tests on REAL navigation flow, not assumptions
366
+
367
+ Now generate test specifications for the ticket above.`;
368
+ }
369
+ /**
370
+ * Agent Strategy Handling:
371
+ * - Claude: Uses tool calling with schema → guaranteed structured output
372
+ * - Cursor: Post-processes raw output with LangChain withStructuredOutput
373
+ *
374
+ * Node receives validated structured data regardless of agent type.
375
+ */
376
+
@@ -0,0 +1,86 @@
1
+ /**
2
+ * PR Meta Service - Generate PR title and description for code changes
3
+ */
4
+
5
+ import { z } from 'zod';
6
+ import { invokeAgent } from '@zibby/core';
7
+ import { adfToText } from '../../../../src/utils/adf-converter.js';
8
+
9
+ const PRMetaSchema = z.object({
10
+ prTitle: z.string().describe('Short PR title that includes the ticket key'),
11
+ prDescription: z.string().describe('Markdown description with ## Why and ## How sections with bullet points')
12
+ });
13
+
14
+ /**
15
+ * Generate PR title and description for code changes
16
+ * @param {Object} params
17
+ * @param {Object} params.ticketContext - Ticket info (ticketKey, summary, description)
18
+ * @param {string[]} params.changedFiles - List of changed file paths
19
+ * @param {string} params.diffStat - Git diff statistics
20
+ * @param {Object} params.state - Execution state for invokeAgent
21
+ * @param {string} params.model - AI model to use
22
+ * @returns {Promise<{prTitle: string, prDescription: string}>}
23
+ */
24
+ export async function generatePRMeta({ ticketContext, changedFiles, diffStat, state, model }) {
25
+ const prompt = buildPrompt(ticketContext, changedFiles, diffStat);
26
+ const result = await invokeAgent(prompt, { state, model, schema: PRMetaSchema });
27
+
28
+ // invokeAgent with schema returns { raw, structured } — extract the structured data
29
+ const prMeta = result?.structured || result;
30
+
31
+ if (!prMeta?.prTitle || !prMeta?.prDescription) {
32
+ throw new Error(`PR meta generation returned incomplete data: ${JSON.stringify(prMeta)}`);
33
+ }
34
+
35
+ const prTitle = prMeta.prTitle.includes(ticketContext.ticketKey)
36
+ ? prMeta.prTitle
37
+ : `${ticketContext.ticketKey}: ${prMeta.prTitle}`;
38
+
39
+ return {
40
+ prTitle,
41
+ prDescription: prMeta.prDescription
42
+ };
43
+ }
44
+
45
+ function buildPrompt(ticketContext, changedFiles, diffStat) {
46
+ // Handle description (could be string or ADF object)
47
+ let descriptionPreview = '';
48
+ if (ticketContext.description) {
49
+ if (typeof ticketContext.description === 'string') {
50
+ descriptionPreview = ticketContext.description.slice(0, 500) +
51
+ (ticketContext.description.length > 500 ? '...' : '');
52
+ } else if (typeof ticketContext.description === 'object') {
53
+ const converted = adfToText(ticketContext.description);
54
+ descriptionPreview = converted.slice(0, 500) + (converted.length > 500 ? '...' : '');
55
+ }
56
+ }
57
+
58
+ return `Generate a pull request title and description for these code changes.
59
+
60
+ TICKET: ${ticketContext.ticketKey} - ${ticketContext.summary}
61
+ ${descriptionPreview ? `Description: ${descriptionPreview}\n` : ''}
62
+ FILES CHANGED:
63
+ ${changedFiles.map(f => ` - ${f}`).join('\n')}
64
+
65
+ DIFF STATS:
66
+ \`\`\`
67
+ ${diffStat}
68
+ \`\`\`
69
+
70
+ Return:
71
+ 1. prTitle: Short PR title that MUST include "${ticketContext.ticketKey}". Example: "${ticketContext.ticketKey}: Add search collapse animation"
72
+
73
+ 2. prDescription: Markdown with this exact format:
74
+ ## Why
75
+
76
+ - Bullet point explaining business/user value
77
+ - Another reason for the change
78
+
79
+ ## How
80
+
81
+ - Bullet point explaining technical approach
82
+ - Key files or patterns used
83
+
84
+ ---
85
+ *Created by Zibby*`;
86
+ }
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Setup Node - Clone repositories and initialize git baseline
3
+ * Used by: analysisGraph, implementationGraph
4
+ */
5
+
6
+ import { spawn } from 'child_process';
7
+ import { join } from 'path';
8
+ import { z } from 'zod';
9
+
10
+ const SetupOutputSchema = z.object({
11
+ success: z.boolean(),
12
+ clonedRepos: z.array(z.object({
13
+ name: z.string(),
14
+ path: z.string(),
15
+ isPrimary: z.boolean().optional()
16
+ })),
17
+ baselineCommit: z.string()
18
+ });
19
+
20
+ export const setupNode = {
21
+ name: 'setup',
22
+ outputSchema: SetupOutputSchema,
23
+ execute: async (state) => {
24
+ console.log('\nšŸ”§ Setting up environment...');
25
+
26
+ const { workspace, repos, githubToken } = state;
27
+ const gitlabToken = process.env.GITLAB_TOKEN || '';
28
+ const gitlabUrl = process.env.GITLAB_URL || '';
29
+
30
+ // DEBUG: Log token status
31
+ console.log(`šŸ”‘ GitHub Token: ${githubToken ? 'Present' : 'MISSING'}`);
32
+ console.log(`šŸ”‘ GitLab Token: ${gitlabToken ? 'Present' : 'MISSING'}`);
33
+ if (gitlabUrl) console.log(`šŸ”‘ GitLab URL: ${gitlabUrl}`);
34
+
35
+ // Log environment
36
+ console.log('Container: ECS Fargate');
37
+ console.log('Memory: 4GB');
38
+ console.log('CPU: 2 vCPU');
39
+ console.log('Tools: Node.js, Git, Cursor CLI, Zibby CLI');
40
+ console.log(`Working directory: ${workspace}`);
41
+
42
+ // Clone repositories
43
+ console.log('\nšŸ“¦ Cloning repositories...');
44
+
45
+ const clonedRepos = [];
46
+ for (const repo of repos) {
47
+ console.log(`Cloning ${repo.name}...`);
48
+
49
+ const repoDir = join(workspace, repo.name);
50
+
51
+ // Use token for authentication based on provider
52
+ let cloneUrl = repo.url;
53
+ let cloneEnv = {};
54
+ const isGitlab = repo.provider === 'gitlab' || (gitlabUrl && repo.url.includes(new URL(gitlabUrl).host));
55
+ const isGithub = repo.provider === 'github' || repo.url.includes('github.com');
56
+
57
+ if (isGithub && githubToken) {
58
+ cloneUrl = repo.url.replace('https://github.com', `https://x-access-token:${githubToken}@github.com`);
59
+ cloneEnv = { ...process.env, GIT_TERMINAL_PROMPT: '0', GIT_ASKPASS: 'echo' };
60
+ } else if (isGitlab && gitlabToken && gitlabUrl) {
61
+ try {
62
+ const gitlabHost = new URL(gitlabUrl).host;
63
+ cloneUrl = repo.url.replace(`https://${gitlabHost}`, `https://oauth2:${gitlabToken}@${gitlabHost}`);
64
+ } catch (e) {
65
+ console.warn(`āš ļø Failed to parse GITLAB_URL: ${e.message}`);
66
+ }
67
+ cloneEnv = { ...process.env, GIT_TERMINAL_PROMPT: '0', GIT_ASKPASS: 'echo' };
68
+ }
69
+
70
+ // Shallow clone with progress output (async, non-blocking)
71
+ await execCommand(
72
+ `git clone --progress --depth 1 --branch ${repo.branch} "${cloneUrl}" "${repoDir}"`,
73
+ workspace,
74
+ cloneEnv
75
+ );
76
+ console.log(`āœ“ Cloned ${repo.name} on branch ${repo.branch}`);
77
+
78
+ clonedRepos.push({
79
+ name: repo.name,
80
+ path: repoDir,
81
+ isPrimary: repo.isPrimary
82
+ });
83
+ }
84
+
85
+ // Initialize git in workspace for diff tracking
86
+ await execCommand('git init', workspace);
87
+ await execCommand('git config user.email "zibby@agent.com"', workspace);
88
+ await execCommand('git config user.name "Zibby Agent"', workspace);
89
+ await execCommand('git add .', workspace);
90
+ await execCommand('git commit --allow-empty -m "baseline"', workspace);
91
+
92
+ console.log('āœ… Environment ready');
93
+
94
+ const baselineCommit = await execCommand('git rev-parse HEAD', workspace);
95
+
96
+ return {
97
+ success: true,
98
+ clonedRepos,
99
+ baselineCommit: baselineCommit.trim()
100
+ };
101
+ }
102
+ };
103
+
104
+ // Async version using spawn - streams output in real-time, doesn't block event loop
105
+ async function execCommand(command, cwd, env = {}) {
106
+ return new Promise((resolve, reject) => {
107
+ const proc = spawn(command, {
108
+ cwd,
109
+ shell: true,
110
+ env: Object.keys(env).length > 0 ? env : process.env
111
+ });
112
+
113
+ let stdout = '';
114
+ let stderr = '';
115
+
116
+ // Stream stdout as it comes (triggers middleware setInterval!)
117
+ proc.stdout.on('data', (data) => {
118
+ const output = data.toString();
119
+ stdout += output;
120
+ console.log(output.trimEnd());
121
+ });
122
+
123
+ // Stream stderr as it comes
124
+ proc.stderr.on('data', (data) => {
125
+ const output = data.toString();
126
+ stderr += output;
127
+ console.log(output.trimEnd());
128
+ });
129
+
130
+ proc.on('close', (code) => {
131
+ if (code !== 0) {
132
+ reject(new Error(`Command failed with exit code ${code}: ${command}`));
133
+ } else {
134
+ resolve(stdout || stderr || '');
135
+ }
136
+ });
137
+
138
+ proc.on('error', (err) => {
139
+ reject(new Error(`Command error: ${command} - ${err.message}`));
140
+ });
141
+ });
142
+ }