donobu 5.57.1 → 5.60.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 (45) hide show
  1. package/dist/cli/donobu-cli.js +4 -0
  2. package/dist/codegen/CodeGenerator.js +83 -38
  3. package/dist/codegen/TargetCodeGenerator.d.ts +54 -0
  4. package/dist/codegen/TargetCodeGenerator.js +21 -0
  5. package/dist/esm/cli/donobu-cli.js +4 -0
  6. package/dist/esm/codegen/CodeGenerator.js +83 -38
  7. package/dist/esm/codegen/TargetCodeGenerator.d.ts +54 -0
  8. package/dist/esm/codegen/TargetCodeGenerator.js +21 -0
  9. package/dist/esm/lib/ai/cache/assertCache.d.ts +14 -1
  10. package/dist/esm/lib/ai/cache/cache.d.ts +12 -4
  11. package/dist/esm/lib/ai/cache/cache.js +40 -24
  12. package/dist/esm/lib/ai/cache/cacheEntryBuilder.d.ts +3 -3
  13. package/dist/esm/lib/ai/cache/cacheEntryBuilder.js +4 -6
  14. package/dist/esm/lib/page/extendPage.js +2 -0
  15. package/dist/esm/lib/test/testExtension.js +7 -0
  16. package/dist/esm/lib/test/utils/selfHealing.js +14 -0
  17. package/dist/esm/main.d.ts +7 -1
  18. package/dist/esm/main.js +7 -1
  19. package/dist/esm/managers/DonobuFlowsManager.js +17 -4
  20. package/dist/esm/managers/DonobuStack.js +9 -0
  21. package/dist/esm/managers/TestsManager.js +5 -0
  22. package/dist/esm/managers/ToolManager.js +11 -0
  23. package/dist/esm/models/CreateDonobuFlow.js +1 -1
  24. package/dist/esm/models/RunConfig.js +1 -1
  25. package/dist/esm/models/ToolTemplateDataSource.d.ts +5 -0
  26. package/dist/esm/targets/TargetRuntimePlugin.d.ts +13 -0
  27. package/dist/lib/ai/cache/assertCache.d.ts +14 -1
  28. package/dist/lib/ai/cache/cache.d.ts +12 -4
  29. package/dist/lib/ai/cache/cache.js +40 -24
  30. package/dist/lib/ai/cache/cacheEntryBuilder.d.ts +3 -3
  31. package/dist/lib/ai/cache/cacheEntryBuilder.js +4 -6
  32. package/dist/lib/page/extendPage.js +2 -0
  33. package/dist/lib/test/testExtension.js +7 -0
  34. package/dist/lib/test/utils/selfHealing.js +14 -0
  35. package/dist/main.d.ts +7 -1
  36. package/dist/main.js +7 -1
  37. package/dist/managers/DonobuFlowsManager.js +17 -4
  38. package/dist/managers/DonobuStack.js +9 -0
  39. package/dist/managers/TestsManager.js +5 -0
  40. package/dist/managers/ToolManager.js +11 -0
  41. package/dist/models/CreateDonobuFlow.js +1 -1
  42. package/dist/models/RunConfig.js +1 -1
  43. package/dist/models/ToolTemplateDataSource.d.ts +5 -0
  44. package/dist/targets/TargetRuntimePlugin.d.ts +13 -0
  45. package/package.json +1 -1
@@ -1177,6 +1177,10 @@ async function postProcessTriageRun(context, originalPlaywrightArgs, reportPath)
1177
1177
  entries = await fs_1.promises.readdir(context.runDir);
1178
1178
  }
1179
1179
  catch (error) {
1180
+ if (error.code === 'ENOENT') {
1181
+ Logger_1.appLogger.info(`No failure evidence found in ${context.runDir}.`);
1182
+ return generatedPlans;
1183
+ }
1180
1184
  Logger_1.appLogger.error(`Unable to read test-failure triage directory ${context.runDir}.`, error);
1181
1185
  return generatedPlans;
1182
1186
  }
@@ -76,6 +76,7 @@ const SummarizeLearningsTool_1 = require("../tools/SummarizeLearningsTool");
76
76
  const WaitTool_1 = require("../tools/WaitTool");
77
77
  const JsonUtils_1 = require("../utils/JsonUtils");
78
78
  const MiscUtils_1 = require("../utils/MiscUtils");
79
+ const TargetCodeGenerator_1 = require("./TargetCodeGenerator");
79
80
  function getLocalPlaywrightVersion() {
80
81
  const pkgPath = require.resolve('playwright/package.json');
81
82
  const pkgJson = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
@@ -112,6 +113,28 @@ function computeRuntimeCacheKeyFields(toolCalls, defaultToolNames, minimalToolNa
112
113
  maxToolCalls: resolvedMaxToolCalls,
113
114
  };
114
115
  }
116
+ /**
117
+ * Built-in code generator for the web (Playwright) target. Owns the full web
118
+ * spec/cache output; non-web targets (API, mobile, …) register their own
119
+ * generators, which may emit an entirely different shape.
120
+ */
121
+ const webCodeGenerator = {
122
+ targetType: 'web',
123
+ generateClassicScript: (ctx) => getFlowAsPlaywrightScript(ctx.flowMetadata, ctx.toolCalls, ctx.options, ctx.toolRegistry),
124
+ generateAiScript: (ctx) => getFlowAsAiPlaywrightScript(ctx.flowMetadata, ctx.toolCalls, ctx.options, ctx.toolRegistry),
125
+ buildCacheEntry: (ctx) => cacheEntryBuilder_1.PageAiCacheEntryBuilder.fromMetadata(ctx.pageUrl, ctx.metadata, ctx.toolCalls),
126
+ cachePageUrl: (metadata) => {
127
+ const website = metadata.web?.targetWebsite ?? '';
128
+ try {
129
+ return new URL(website).hostname;
130
+ }
131
+ catch {
132
+ return website;
133
+ }
134
+ },
135
+ requiresBrowserInstall: () => true,
136
+ };
137
+ (0, TargetCodeGenerator_1.registerCodeGenerator)(webCodeGenerator);
115
138
  /** Creates a Node.js Microsoft Playwright script to replay the given flow. */
116
139
  async function getFlowAsPlaywrightScript(flowMetadata, toolCalls, options = {}, toolRegistry) {
117
140
  // These tools are not supported in the generated script because they have
@@ -166,12 +189,10 @@ async function getFlowAsPlaywrightScript(flowMetadata, toolCalls, options = {},
166
189
  .replace(/\n/g, '\\n')
167
190
  // Escape carriage returns.
168
191
  .replace(/\r/g, '\\r')
169
- : `Test for ${flowMetadata.web?.targetWebsite ?? 'flow'}`;
192
+ : `Test for ${getFlowTargetLabel(flowMetadata)}`;
170
193
  const scriptedToolCalls = toolCalls
171
194
  .filter((toolCall) => !unsupportedToolsByName.has(toolCall.name))
172
- .map((toolCall) => {
173
- return convertProposedToolCallToPlaywrightCode(toolCall);
174
- })
195
+ .map((toolCall) => convertProposedToolCallToPlaywrightCode(toolCall))
175
196
  .join('\n\n');
176
197
  const annotationsLiteral = combinedAnnotations.length > 0
177
198
  ? JSON.stringify({ annotation: combinedAnnotations }, null, 2)
@@ -185,7 +206,7 @@ const extractedObject = await page.ai.extract(
185
206
  testInfo.attach('extracted-object', { body: JSON.stringify(extractedObject), contentType: 'application/json' });`
186
207
  : '';
187
208
  const needsExpectImport = toolCalls.some((toolCall) => toolCall.name === AssertPageTool_1.AssertPageTool.NAME);
188
- const needsJsonSchemaToZodImport = flowMetadata.resultJsonSchema;
209
+ const needsJsonSchemaToZodImport = !!flowMetadata.resultJsonSchema;
189
210
  const preamble = gptSetupNote.trim().length > 0
190
211
  ? `/**
191
212
  ${gptSetupNote}*/`
@@ -207,9 +228,10 @@ async function getFlowAsAiPlaywrightScript(flowMetadata, toolCalls, options, too
207
228
  const [firstToolCall, ...remaingToolCalls] = toolCalls;
208
229
  // If the first tool call is "GoToWebpage", then we peel it off and treat it
209
230
  // specially.
210
- const specialCaseGoto = firstToolCall.name === GoToWebpageTool_1.GoToWebpageTool.NAME && remaingToolCalls.length > 0;
231
+ const specialCaseGoto = firstToolCall?.name === GoToWebpageTool_1.GoToWebpageTool.NAME && remaingToolCalls.length > 0;
211
232
  const cachePath = (0, cacheLocator_1.relativePageAiCachePathForSource)(node_path_1.default.join('tests', getTestFileName(flowMetadata)));
212
- const gptSetupNote = ` * This test replays a recorded Donobu flow via \`page.ai(...)\` using the cached
233
+ const aiHelper = 'page.ai';
234
+ const gptSetupNote = ` * This test replays a recorded Donobu flow via \`${aiHelper}(...)\` using the cached
213
235
  * tool calls stored for this spec in \`${cachePath}\`.
214
236
  * If the cache entry is missing or the parameters change, the run falls back
215
237
  * to autonomous mode and will require a GPT API key (e.g. DONOBU_API_KEY,
@@ -225,10 +247,10 @@ async function getFlowAsAiPlaywrightScript(flowMetadata, toolCalls, options, too
225
247
  .replace(/\n/g, '\\n')
226
248
  // Escape carriage returns.
227
249
  .replace(/\r/g, '\\r')
228
- : `Test for ${flowMetadata.web?.targetWebsite ?? 'flow'}`;
250
+ : `Test for ${getFlowTargetLabel(flowMetadata)}`;
229
251
  const instructionSource = flowMetadata.overallObjective?.trim()
230
252
  ? flowMetadata.overallObjective
231
- : `Replay the recorded flow for ${flowMetadata.web?.targetWebsite ?? 'flow'}`;
253
+ : `Replay the recorded flow for ${getFlowTargetLabel(flowMetadata)}`;
232
254
  const sanitizedInstruction = sanitizeForTemplateLiteral(instructionSource);
233
255
  const annotations = [
234
256
  ...(options?.flowAnnotations?.[flowMetadata.id] ?? []),
@@ -265,8 +287,8 @@ async function getFlowAsAiPlaywrightScript(flowMetadata, toolCalls, options, too
265
287
  }
266
288
  const aiOptionsLiteral = optionsLines.length > 0 ? `{${optionsLines.join(',')}}` : '';
267
289
  const aiCallExpression = optionsLines.length > 0
268
- ? `page.ai(\`${sanitizedInstruction}\`, ${aiOptionsLiteral})`
269
- : `page.ai(\`${sanitizedInstruction}\`)`;
290
+ ? `${aiHelper}(\`${sanitizedInstruction}\`, ${aiOptionsLiteral})`
291
+ : `${aiHelper}(\`${sanitizedInstruction}\`)`;
270
292
  const needsTestInfo = flowMetadata.resultJsonSchema !== null;
271
293
  const aiInvocation = needsTestInfo
272
294
  ? `const extractedObj = await ${aiCallExpression};
@@ -479,29 +501,29 @@ async function buildCacheContents(flowsWithToolCalls, toolRegistry) {
479
501
  const defaultToolNames = new Set(toolRegistry.defaultTools().map((tool) => tool.name));
480
502
  const minimalToolNames = new Set(toolRegistry.minimalTools().map((t) => t.name));
481
503
  const entries = flowsWithToolCalls
482
- // We can only create page.ai caches for flows that have an objective.
483
- .filter(({ metadata }) => metadata.overallObjective?.trim() &&
504
+ // We can only create ai caches for targets that codegen knows how to
505
+ // replay (web + any plugin with a registered generator), with an objective.
506
+ .filter(({ metadata }) => (0, TargetCodeGenerator_1.hasCodeGenerator)(metadata.target) &&
507
+ metadata.overallObjective?.trim() &&
484
508
  (metadata.runMode === 'AUTONOMOUS' ||
485
509
  metadata.runMode === 'SUPERVISED'))
486
510
  .map(({ metadata, toolCalls }) => {
511
+ // Filtered above on hasCodeGenerator, so this is defined.
512
+ const generator = (0, TargetCodeGenerator_1.getCodeGenerator)(metadata.target);
487
513
  const [firstToolCall, ...remaingToolCalls] = toolCalls;
488
- // If the first tool call is "GoToWebpage", then we peel it off and treat it
489
- // specially (i.e. it will be an explicit tool call in the generated test file).
514
+ // A leading GoToWebpage is peeled off (it becomes an explicit nav in the
515
+ // generated spec). Naturally a no-op for non-web targets, which never
516
+ // emit GoToWebpage.
490
517
  const specialCaseGoto = firstToolCall?.name === GoToWebpageTool_1.GoToWebpageTool.NAME &&
491
518
  remaingToolCalls.length > 0;
492
519
  const toolCallsForCache = specialCaseGoto ? remaingToolCalls : toolCalls;
493
- // Extract hostname from URL for cache key to allow caching across different
494
- // paths and query parameters on the same domain
495
- let pageUrlForCache;
496
- try {
497
- const url = new URL(metadata.web?.targetWebsite ?? '');
498
- pageUrlForCache = url.hostname;
499
- }
500
- catch {
501
- // Fallback to full URL if parsing fails
502
- pageUrlForCache = metadata.web?.targetWebsite ?? '';
503
- }
504
- const cacheEntry = cacheEntryBuilder_1.PageAiCacheEntryBuilder.fromMetadata(pageUrlForCache, metadata, toolCallsForCache);
520
+ // Cache key pageUrl (e.g. hostname) is target-specific.
521
+ const pageUrlForCache = generator.cachePageUrl(metadata);
522
+ const cacheEntry = generator.buildCacheEntry({
523
+ pageUrl: pageUrlForCache,
524
+ metadata,
525
+ toolCalls: toolCallsForCache,
526
+ });
505
527
  // Compute allowedTools and maxToolCalls as the runtime will see them,
506
528
  // so the cache lock file keys match the keys built by PageAi.buildDescriptor
507
529
  // when the generated test code is executed.
@@ -738,13 +760,18 @@ async function generatePlaywrightConfig(flows, storageStatePaths, options) {
738
760
  projects.push(generateProjectConfig(projectName, flow, storageStatePath));
739
761
  }
740
762
  const { areElementIdsVolatile, disableSelectorFailover, runInHeadedMode, slowMotionDelay, } = options;
741
- const useConfig = {
742
- screenshot: 'on',
743
- video: 'on',
744
- ...(runInHeadedMode && { headless: !runInHeadedMode }),
745
- ...(slowMotionDelay &&
746
- slowMotionDelay > 0 && { launchOptions: { slowMo: slowMotionDelay } }),
747
- };
763
+ const hasBrowserFlows = flows.some((flow) => (0, TargetCodeGenerator_1.getCodeGenerator)(flow.target)?.requiresBrowserInstall(flow) ?? false);
764
+ const useConfig = hasBrowserFlows
765
+ ? {
766
+ screenshot: 'on',
767
+ video: 'on',
768
+ ...(runInHeadedMode && { headless: !runInHeadedMode }),
769
+ ...(slowMotionDelay &&
770
+ slowMotionDelay > 0 && {
771
+ launchOptions: { slowMo: slowMotionDelay },
772
+ }),
773
+ }
774
+ : {};
748
775
  const selfHealingOptions = {
749
776
  areElementIdsVolatile,
750
777
  disableSelectorFailover,
@@ -783,6 +810,14 @@ function generateProjectConfig(projectName, flow, storageStatePath) {
783
810
  // Round up to the nearest 10000ms
784
811
  const timeoutMilliseconds = Math.max(minimumTimeoutMilliseconds, Math.ceil(calculatedTimeout / 10000) * 10000);
785
812
  const testMatch = `tests/${getTestFileName(flow)}`;
813
+ if (!((0, TargetCodeGenerator_1.getCodeGenerator)(flow.target)?.requiresBrowserInstall(flow) ?? true)) {
814
+ return `{
815
+ name: '${projectName}',
816
+ testMatch: '${testMatch}',
817
+ use: {},
818
+ timeout: ${timeoutMilliseconds}
819
+ }`;
820
+ }
786
821
  // Get device name from flow config, default to 'Desktop Chromium'
787
822
  const deviceName = flow.web?.browser?.using?.type === 'device'
788
823
  ? flow.web.browser.using.deviceName || 'Desktop Chromium'
@@ -804,12 +839,19 @@ async function generateTestFiles(flowsWithToolCalls, options, toolRegistry) {
804
839
  const files = [];
805
840
  const scriptVariant = options.playwrightScriptVariant === 'classic' ? 'classic' : 'ai';
806
841
  for (const { metadata, toolCalls } of flowsWithToolCalls) {
842
+ const generator = (0, TargetCodeGenerator_1.getCodeGenerator)(metadata.target) ?? webCodeGenerator;
807
843
  const fileName = getTestFileName(metadata);
844
+ const ctx = {
845
+ flowMetadata: metadata,
846
+ toolCalls,
847
+ options,
848
+ toolRegistry,
849
+ };
808
850
  const content = scriptVariant === 'classic' ||
809
851
  !metadata.overallObjective?.trim() ||
810
852
  (metadata.runMode !== 'AUTONOMOUS' && metadata.runMode !== 'SUPERVISED')
811
- ? await getFlowAsPlaywrightScript(metadata, toolCalls, options, toolRegistry)
812
- : await getFlowAsAiPlaywrightScript(metadata, toolCalls, options, toolRegistry);
853
+ ? await generator.generateClassicScript(ctx)
854
+ : await generator.generateAiScript(ctx);
813
855
  files.push({
814
856
  path: `tests/${fileName}`,
815
857
  content,
@@ -827,7 +869,7 @@ function generatePackageJson(options) {
827
869
  return JSON.stringify({
828
870
  name: 'playwright-tests',
829
871
  version: '1.0.0',
830
- description: 'Playwright-based website tests made with Donobu',
872
+ description: 'Playwright-based tests made with Donobu',
831
873
  scripts: {
832
874
  test: `donobu test${selfHealingArg}`,
833
875
  },
@@ -856,7 +898,7 @@ Some tests depend on pre-existing browser state (cookies, localStorage, etc.) fr
856
898
  : '';
857
899
  return `# Playwright Tests
858
900
 
859
- This project contains [Playwright](https://playwright.dev/)-based tests made with [Donobu](https://www.donobu.com/).
901
+ This project contains [Playwright](https://playwright.dev/)-based web and API tests made with [Donobu](https://www.donobu.com/).
860
902
 
861
903
  ## Installation
862
904
 
@@ -879,6 +921,9 @@ npm test
879
921
  \`\`\`
880
922
  ${browserStatesSection}`;
881
923
  }
924
+ function getFlowTargetLabel(flow) {
925
+ return flow.web?.targetWebsite ?? 'flow';
926
+ }
882
927
  /**
883
928
  * Gets a project name for a flow
884
929
  */
@@ -0,0 +1,54 @@
1
+ import type { PageAiCacheEntry } from '../lib/ai/cache/cache';
2
+ import type { ToolRegistry } from '../managers/ToolRegistry';
3
+ import type { CodeGenerationOptions } from '../models/CodeGenerationOptions';
4
+ import type { FlowMetadata } from '../models/FlowMetadata';
5
+ import type { ProposedToolCall } from '../models/ProposedToolCall';
6
+ /**
7
+ * Inputs for generating one flow's replay spec.
8
+ */
9
+ export interface CodeGenContext {
10
+ flowMetadata: FlowMetadata;
11
+ toolCalls: ProposedToolCall[];
12
+ options: CodeGenerationOptions;
13
+ toolRegistry: ToolRegistry;
14
+ }
15
+ export interface CacheEntryContext {
16
+ pageUrl: string;
17
+ metadata: FlowMetadata;
18
+ toolCalls: ProposedToolCall[];
19
+ }
20
+ /**
21
+ * A target's code generator. This is an opaque, whole-artifact contract: core
22
+ * asks a generator to produce a flow's replay spec / cache entry and makes NO
23
+ * assumptions about the artifact's internals. Web (Playwright specs) is the
24
+ * built-in; other targets (API, mobile, …) register their own and are free to
25
+ * emit a completely different shape — or none. Deliberately NOT parameterized
26
+ * with web-spec knobs (fixtures, AI helpers, etc.): those would presume every
27
+ * target produces a Playwright test, which they do not.
28
+ */
29
+ export interface TargetCodeGenerator {
30
+ readonly targetType: string;
31
+ /** Produce the deterministic ("classic") replay spec for one flow. */
32
+ generateClassicScript(ctx: CodeGenContext): Promise<string>;
33
+ /** Produce the AI/cache-backed replay spec for one flow. */
34
+ generateAiScript(ctx: CodeGenContext): Promise<string>;
35
+ /** Build the AI-cache entry (runSource) for one flow. */
36
+ buildCacheEntry(ctx: CacheEntryContext): PageAiCacheEntry;
37
+ /** Cache-key pageUrl for a flow of this target. */
38
+ cachePageUrl(metadata: FlowMetadata): string;
39
+ /**
40
+ * Whether flows of this target require a browser install in the generated
41
+ * project (drives CI setup and the Playwright project `use` block). A coarse
42
+ * capability question, not a directive about spec contents.
43
+ */
44
+ requiresBrowserInstall(metadata: FlowMetadata): boolean;
45
+ }
46
+ /** Register a target's code generator. Core registers `web`; plugins register
47
+ * their own at startup. */
48
+ export declare function registerCodeGenerator(generator: TargetCodeGenerator): void;
49
+ /** Resolve the generator for a target type, or `undefined` if none is
50
+ * registered (the caller decides how to handle unsupported targets). */
51
+ export declare function getCodeGenerator(targetType: string): TargetCodeGenerator | undefined;
52
+ /** Whether a code generator is registered for the given target type. */
53
+ export declare function hasCodeGenerator(targetType: string): boolean;
54
+ //# sourceMappingURL=TargetCodeGenerator.d.ts.map
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerCodeGenerator = registerCodeGenerator;
4
+ exports.getCodeGenerator = getCodeGenerator;
5
+ exports.hasCodeGenerator = hasCodeGenerator;
6
+ const registry = new Map();
7
+ /** Register a target's code generator. Core registers `web`; plugins register
8
+ * their own at startup. */
9
+ function registerCodeGenerator(generator) {
10
+ registry.set(generator.targetType, generator);
11
+ }
12
+ /** Resolve the generator for a target type, or `undefined` if none is
13
+ * registered (the caller decides how to handle unsupported targets). */
14
+ function getCodeGenerator(targetType) {
15
+ return registry.get(targetType);
16
+ }
17
+ /** Whether a code generator is registered for the given target type. */
18
+ function hasCodeGenerator(targetType) {
19
+ return registry.has(targetType);
20
+ }
21
+ //# sourceMappingURL=TargetCodeGenerator.js.map
@@ -1177,6 +1177,10 @@ async function postProcessTriageRun(context, originalPlaywrightArgs, reportPath)
1177
1177
  entries = await fs_1.promises.readdir(context.runDir);
1178
1178
  }
1179
1179
  catch (error) {
1180
+ if (error.code === 'ENOENT') {
1181
+ Logger_1.appLogger.info(`No failure evidence found in ${context.runDir}.`);
1182
+ return generatedPlans;
1183
+ }
1180
1184
  Logger_1.appLogger.error(`Unable to read test-failure triage directory ${context.runDir}.`, error);
1181
1185
  return generatedPlans;
1182
1186
  }
@@ -76,6 +76,7 @@ const SummarizeLearningsTool_1 = require("../tools/SummarizeLearningsTool");
76
76
  const WaitTool_1 = require("../tools/WaitTool");
77
77
  const JsonUtils_1 = require("../utils/JsonUtils");
78
78
  const MiscUtils_1 = require("../utils/MiscUtils");
79
+ const TargetCodeGenerator_1 = require("./TargetCodeGenerator");
79
80
  function getLocalPlaywrightVersion() {
80
81
  const pkgPath = require.resolve('playwright/package.json');
81
82
  const pkgJson = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
@@ -112,6 +113,28 @@ function computeRuntimeCacheKeyFields(toolCalls, defaultToolNames, minimalToolNa
112
113
  maxToolCalls: resolvedMaxToolCalls,
113
114
  };
114
115
  }
116
+ /**
117
+ * Built-in code generator for the web (Playwright) target. Owns the full web
118
+ * spec/cache output; non-web targets (API, mobile, …) register their own
119
+ * generators, which may emit an entirely different shape.
120
+ */
121
+ const webCodeGenerator = {
122
+ targetType: 'web',
123
+ generateClassicScript: (ctx) => getFlowAsPlaywrightScript(ctx.flowMetadata, ctx.toolCalls, ctx.options, ctx.toolRegistry),
124
+ generateAiScript: (ctx) => getFlowAsAiPlaywrightScript(ctx.flowMetadata, ctx.toolCalls, ctx.options, ctx.toolRegistry),
125
+ buildCacheEntry: (ctx) => cacheEntryBuilder_1.PageAiCacheEntryBuilder.fromMetadata(ctx.pageUrl, ctx.metadata, ctx.toolCalls),
126
+ cachePageUrl: (metadata) => {
127
+ const website = metadata.web?.targetWebsite ?? '';
128
+ try {
129
+ return new URL(website).hostname;
130
+ }
131
+ catch {
132
+ return website;
133
+ }
134
+ },
135
+ requiresBrowserInstall: () => true,
136
+ };
137
+ (0, TargetCodeGenerator_1.registerCodeGenerator)(webCodeGenerator);
115
138
  /** Creates a Node.js Microsoft Playwright script to replay the given flow. */
116
139
  async function getFlowAsPlaywrightScript(flowMetadata, toolCalls, options = {}, toolRegistry) {
117
140
  // These tools are not supported in the generated script because they have
@@ -166,12 +189,10 @@ async function getFlowAsPlaywrightScript(flowMetadata, toolCalls, options = {},
166
189
  .replace(/\n/g, '\\n')
167
190
  // Escape carriage returns.
168
191
  .replace(/\r/g, '\\r')
169
- : `Test for ${flowMetadata.web?.targetWebsite ?? 'flow'}`;
192
+ : `Test for ${getFlowTargetLabel(flowMetadata)}`;
170
193
  const scriptedToolCalls = toolCalls
171
194
  .filter((toolCall) => !unsupportedToolsByName.has(toolCall.name))
172
- .map((toolCall) => {
173
- return convertProposedToolCallToPlaywrightCode(toolCall);
174
- })
195
+ .map((toolCall) => convertProposedToolCallToPlaywrightCode(toolCall))
175
196
  .join('\n\n');
176
197
  const annotationsLiteral = combinedAnnotations.length > 0
177
198
  ? JSON.stringify({ annotation: combinedAnnotations }, null, 2)
@@ -185,7 +206,7 @@ const extractedObject = await page.ai.extract(
185
206
  testInfo.attach('extracted-object', { body: JSON.stringify(extractedObject), contentType: 'application/json' });`
186
207
  : '';
187
208
  const needsExpectImport = toolCalls.some((toolCall) => toolCall.name === AssertPageTool_1.AssertPageTool.NAME);
188
- const needsJsonSchemaToZodImport = flowMetadata.resultJsonSchema;
209
+ const needsJsonSchemaToZodImport = !!flowMetadata.resultJsonSchema;
189
210
  const preamble = gptSetupNote.trim().length > 0
190
211
  ? `/**
191
212
  ${gptSetupNote}*/`
@@ -207,9 +228,10 @@ async function getFlowAsAiPlaywrightScript(flowMetadata, toolCalls, options, too
207
228
  const [firstToolCall, ...remaingToolCalls] = toolCalls;
208
229
  // If the first tool call is "GoToWebpage", then we peel it off and treat it
209
230
  // specially.
210
- const specialCaseGoto = firstToolCall.name === GoToWebpageTool_1.GoToWebpageTool.NAME && remaingToolCalls.length > 0;
231
+ const specialCaseGoto = firstToolCall?.name === GoToWebpageTool_1.GoToWebpageTool.NAME && remaingToolCalls.length > 0;
211
232
  const cachePath = (0, cacheLocator_1.relativePageAiCachePathForSource)(node_path_1.default.join('tests', getTestFileName(flowMetadata)));
212
- const gptSetupNote = ` * This test replays a recorded Donobu flow via \`page.ai(...)\` using the cached
233
+ const aiHelper = 'page.ai';
234
+ const gptSetupNote = ` * This test replays a recorded Donobu flow via \`${aiHelper}(...)\` using the cached
213
235
  * tool calls stored for this spec in \`${cachePath}\`.
214
236
  * If the cache entry is missing or the parameters change, the run falls back
215
237
  * to autonomous mode and will require a GPT API key (e.g. DONOBU_API_KEY,
@@ -225,10 +247,10 @@ async function getFlowAsAiPlaywrightScript(flowMetadata, toolCalls, options, too
225
247
  .replace(/\n/g, '\\n')
226
248
  // Escape carriage returns.
227
249
  .replace(/\r/g, '\\r')
228
- : `Test for ${flowMetadata.web?.targetWebsite ?? 'flow'}`;
250
+ : `Test for ${getFlowTargetLabel(flowMetadata)}`;
229
251
  const instructionSource = flowMetadata.overallObjective?.trim()
230
252
  ? flowMetadata.overallObjective
231
- : `Replay the recorded flow for ${flowMetadata.web?.targetWebsite ?? 'flow'}`;
253
+ : `Replay the recorded flow for ${getFlowTargetLabel(flowMetadata)}`;
232
254
  const sanitizedInstruction = sanitizeForTemplateLiteral(instructionSource);
233
255
  const annotations = [
234
256
  ...(options?.flowAnnotations?.[flowMetadata.id] ?? []),
@@ -265,8 +287,8 @@ async function getFlowAsAiPlaywrightScript(flowMetadata, toolCalls, options, too
265
287
  }
266
288
  const aiOptionsLiteral = optionsLines.length > 0 ? `{${optionsLines.join(',')}}` : '';
267
289
  const aiCallExpression = optionsLines.length > 0
268
- ? `page.ai(\`${sanitizedInstruction}\`, ${aiOptionsLiteral})`
269
- : `page.ai(\`${sanitizedInstruction}\`)`;
290
+ ? `${aiHelper}(\`${sanitizedInstruction}\`, ${aiOptionsLiteral})`
291
+ : `${aiHelper}(\`${sanitizedInstruction}\`)`;
270
292
  const needsTestInfo = flowMetadata.resultJsonSchema !== null;
271
293
  const aiInvocation = needsTestInfo
272
294
  ? `const extractedObj = await ${aiCallExpression};
@@ -479,29 +501,29 @@ async function buildCacheContents(flowsWithToolCalls, toolRegistry) {
479
501
  const defaultToolNames = new Set(toolRegistry.defaultTools().map((tool) => tool.name));
480
502
  const minimalToolNames = new Set(toolRegistry.minimalTools().map((t) => t.name));
481
503
  const entries = flowsWithToolCalls
482
- // We can only create page.ai caches for flows that have an objective.
483
- .filter(({ metadata }) => metadata.overallObjective?.trim() &&
504
+ // We can only create ai caches for targets that codegen knows how to
505
+ // replay (web + any plugin with a registered generator), with an objective.
506
+ .filter(({ metadata }) => (0, TargetCodeGenerator_1.hasCodeGenerator)(metadata.target) &&
507
+ metadata.overallObjective?.trim() &&
484
508
  (metadata.runMode === 'AUTONOMOUS' ||
485
509
  metadata.runMode === 'SUPERVISED'))
486
510
  .map(({ metadata, toolCalls }) => {
511
+ // Filtered above on hasCodeGenerator, so this is defined.
512
+ const generator = (0, TargetCodeGenerator_1.getCodeGenerator)(metadata.target);
487
513
  const [firstToolCall, ...remaingToolCalls] = toolCalls;
488
- // If the first tool call is "GoToWebpage", then we peel it off and treat it
489
- // specially (i.e. it will be an explicit tool call in the generated test file).
514
+ // A leading GoToWebpage is peeled off (it becomes an explicit nav in the
515
+ // generated spec). Naturally a no-op for non-web targets, which never
516
+ // emit GoToWebpage.
490
517
  const specialCaseGoto = firstToolCall?.name === GoToWebpageTool_1.GoToWebpageTool.NAME &&
491
518
  remaingToolCalls.length > 0;
492
519
  const toolCallsForCache = specialCaseGoto ? remaingToolCalls : toolCalls;
493
- // Extract hostname from URL for cache key to allow caching across different
494
- // paths and query parameters on the same domain
495
- let pageUrlForCache;
496
- try {
497
- const url = new URL(metadata.web?.targetWebsite ?? '');
498
- pageUrlForCache = url.hostname;
499
- }
500
- catch {
501
- // Fallback to full URL if parsing fails
502
- pageUrlForCache = metadata.web?.targetWebsite ?? '';
503
- }
504
- const cacheEntry = cacheEntryBuilder_1.PageAiCacheEntryBuilder.fromMetadata(pageUrlForCache, metadata, toolCallsForCache);
520
+ // Cache key pageUrl (e.g. hostname) is target-specific.
521
+ const pageUrlForCache = generator.cachePageUrl(metadata);
522
+ const cacheEntry = generator.buildCacheEntry({
523
+ pageUrl: pageUrlForCache,
524
+ metadata,
525
+ toolCalls: toolCallsForCache,
526
+ });
505
527
  // Compute allowedTools and maxToolCalls as the runtime will see them,
506
528
  // so the cache lock file keys match the keys built by PageAi.buildDescriptor
507
529
  // when the generated test code is executed.
@@ -738,13 +760,18 @@ async function generatePlaywrightConfig(flows, storageStatePaths, options) {
738
760
  projects.push(generateProjectConfig(projectName, flow, storageStatePath));
739
761
  }
740
762
  const { areElementIdsVolatile, disableSelectorFailover, runInHeadedMode, slowMotionDelay, } = options;
741
- const useConfig = {
742
- screenshot: 'on',
743
- video: 'on',
744
- ...(runInHeadedMode && { headless: !runInHeadedMode }),
745
- ...(slowMotionDelay &&
746
- slowMotionDelay > 0 && { launchOptions: { slowMo: slowMotionDelay } }),
747
- };
763
+ const hasBrowserFlows = flows.some((flow) => (0, TargetCodeGenerator_1.getCodeGenerator)(flow.target)?.requiresBrowserInstall(flow) ?? false);
764
+ const useConfig = hasBrowserFlows
765
+ ? {
766
+ screenshot: 'on',
767
+ video: 'on',
768
+ ...(runInHeadedMode && { headless: !runInHeadedMode }),
769
+ ...(slowMotionDelay &&
770
+ slowMotionDelay > 0 && {
771
+ launchOptions: { slowMo: slowMotionDelay },
772
+ }),
773
+ }
774
+ : {};
748
775
  const selfHealingOptions = {
749
776
  areElementIdsVolatile,
750
777
  disableSelectorFailover,
@@ -783,6 +810,14 @@ function generateProjectConfig(projectName, flow, storageStatePath) {
783
810
  // Round up to the nearest 10000ms
784
811
  const timeoutMilliseconds = Math.max(minimumTimeoutMilliseconds, Math.ceil(calculatedTimeout / 10000) * 10000);
785
812
  const testMatch = `tests/${getTestFileName(flow)}`;
813
+ if (!((0, TargetCodeGenerator_1.getCodeGenerator)(flow.target)?.requiresBrowserInstall(flow) ?? true)) {
814
+ return `{
815
+ name: '${projectName}',
816
+ testMatch: '${testMatch}',
817
+ use: {},
818
+ timeout: ${timeoutMilliseconds}
819
+ }`;
820
+ }
786
821
  // Get device name from flow config, default to 'Desktop Chromium'
787
822
  const deviceName = flow.web?.browser?.using?.type === 'device'
788
823
  ? flow.web.browser.using.deviceName || 'Desktop Chromium'
@@ -804,12 +839,19 @@ async function generateTestFiles(flowsWithToolCalls, options, toolRegistry) {
804
839
  const files = [];
805
840
  const scriptVariant = options.playwrightScriptVariant === 'classic' ? 'classic' : 'ai';
806
841
  for (const { metadata, toolCalls } of flowsWithToolCalls) {
842
+ const generator = (0, TargetCodeGenerator_1.getCodeGenerator)(metadata.target) ?? webCodeGenerator;
807
843
  const fileName = getTestFileName(metadata);
844
+ const ctx = {
845
+ flowMetadata: metadata,
846
+ toolCalls,
847
+ options,
848
+ toolRegistry,
849
+ };
808
850
  const content = scriptVariant === 'classic' ||
809
851
  !metadata.overallObjective?.trim() ||
810
852
  (metadata.runMode !== 'AUTONOMOUS' && metadata.runMode !== 'SUPERVISED')
811
- ? await getFlowAsPlaywrightScript(metadata, toolCalls, options, toolRegistry)
812
- : await getFlowAsAiPlaywrightScript(metadata, toolCalls, options, toolRegistry);
853
+ ? await generator.generateClassicScript(ctx)
854
+ : await generator.generateAiScript(ctx);
813
855
  files.push({
814
856
  path: `tests/${fileName}`,
815
857
  content,
@@ -827,7 +869,7 @@ function generatePackageJson(options) {
827
869
  return JSON.stringify({
828
870
  name: 'playwright-tests',
829
871
  version: '1.0.0',
830
- description: 'Playwright-based website tests made with Donobu',
872
+ description: 'Playwright-based tests made with Donobu',
831
873
  scripts: {
832
874
  test: `donobu test${selfHealingArg}`,
833
875
  },
@@ -856,7 +898,7 @@ Some tests depend on pre-existing browser state (cookies, localStorage, etc.) fr
856
898
  : '';
857
899
  return `# Playwright Tests
858
900
 
859
- This project contains [Playwright](https://playwright.dev/)-based tests made with [Donobu](https://www.donobu.com/).
901
+ This project contains [Playwright](https://playwright.dev/)-based web and API tests made with [Donobu](https://www.donobu.com/).
860
902
 
861
903
  ## Installation
862
904
 
@@ -879,6 +921,9 @@ npm test
879
921
  \`\`\`
880
922
  ${browserStatesSection}`;
881
923
  }
924
+ function getFlowTargetLabel(flow) {
925
+ return flow.web?.targetWebsite ?? 'flow';
926
+ }
882
927
  /**
883
928
  * Gets a project name for a flow
884
929
  */
@@ -0,0 +1,54 @@
1
+ import type { PageAiCacheEntry } from '../lib/ai/cache/cache';
2
+ import type { ToolRegistry } from '../managers/ToolRegistry';
3
+ import type { CodeGenerationOptions } from '../models/CodeGenerationOptions';
4
+ import type { FlowMetadata } from '../models/FlowMetadata';
5
+ import type { ProposedToolCall } from '../models/ProposedToolCall';
6
+ /**
7
+ * Inputs for generating one flow's replay spec.
8
+ */
9
+ export interface CodeGenContext {
10
+ flowMetadata: FlowMetadata;
11
+ toolCalls: ProposedToolCall[];
12
+ options: CodeGenerationOptions;
13
+ toolRegistry: ToolRegistry;
14
+ }
15
+ export interface CacheEntryContext {
16
+ pageUrl: string;
17
+ metadata: FlowMetadata;
18
+ toolCalls: ProposedToolCall[];
19
+ }
20
+ /**
21
+ * A target's code generator. This is an opaque, whole-artifact contract: core
22
+ * asks a generator to produce a flow's replay spec / cache entry and makes NO
23
+ * assumptions about the artifact's internals. Web (Playwright specs) is the
24
+ * built-in; other targets (API, mobile, …) register their own and are free to
25
+ * emit a completely different shape — or none. Deliberately NOT parameterized
26
+ * with web-spec knobs (fixtures, AI helpers, etc.): those would presume every
27
+ * target produces a Playwright test, which they do not.
28
+ */
29
+ export interface TargetCodeGenerator {
30
+ readonly targetType: string;
31
+ /** Produce the deterministic ("classic") replay spec for one flow. */
32
+ generateClassicScript(ctx: CodeGenContext): Promise<string>;
33
+ /** Produce the AI/cache-backed replay spec for one flow. */
34
+ generateAiScript(ctx: CodeGenContext): Promise<string>;
35
+ /** Build the AI-cache entry (runSource) for one flow. */
36
+ buildCacheEntry(ctx: CacheEntryContext): PageAiCacheEntry;
37
+ /** Cache-key pageUrl for a flow of this target. */
38
+ cachePageUrl(metadata: FlowMetadata): string;
39
+ /**
40
+ * Whether flows of this target require a browser install in the generated
41
+ * project (drives CI setup and the Playwright project `use` block). A coarse
42
+ * capability question, not a directive about spec contents.
43
+ */
44
+ requiresBrowserInstall(metadata: FlowMetadata): boolean;
45
+ }
46
+ /** Register a target's code generator. Core registers `web`; plugins register
47
+ * their own at startup. */
48
+ export declare function registerCodeGenerator(generator: TargetCodeGenerator): void;
49
+ /** Resolve the generator for a target type, or `undefined` if none is
50
+ * registered (the caller decides how to handle unsupported targets). */
51
+ export declare function getCodeGenerator(targetType: string): TargetCodeGenerator | undefined;
52
+ /** Whether a code generator is registered for the given target type. */
53
+ export declare function hasCodeGenerator(targetType: string): boolean;
54
+ //# sourceMappingURL=TargetCodeGenerator.d.ts.map
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerCodeGenerator = registerCodeGenerator;
4
+ exports.getCodeGenerator = getCodeGenerator;
5
+ exports.hasCodeGenerator = hasCodeGenerator;
6
+ const registry = new Map();
7
+ /** Register a target's code generator. Core registers `web`; plugins register
8
+ * their own at startup. */
9
+ function registerCodeGenerator(generator) {
10
+ registry.set(generator.targetType, generator);
11
+ }
12
+ /** Resolve the generator for a target type, or `undefined` if none is
13
+ * registered (the caller decides how to handle unsupported targets). */
14
+ function getCodeGenerator(targetType) {
15
+ return registry.get(targetType);
16
+ }
17
+ /** Whether a code generator is registered for the given target type. */
18
+ function hasCodeGenerator(targetType) {
19
+ return registry.has(targetType);
20
+ }
21
+ //# sourceMappingURL=TargetCodeGenerator.js.map