@algolia/wizard 0.65.0 → 0.67.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 (2) hide show
  1. package/dist/main.js +450 -62
  2. package/package.json +1 -1
package/dist/main.js CHANGED
@@ -1490,7 +1490,106 @@ async function clearWorkflowState(workflowId) {
1490
1490
  await rm(stateFile(workflowId), { force: true });
1491
1491
  }
1492
1492
 
1493
+ // src/lib/modelProfiles.ts
1494
+ var TOKENS_PER_MILLION = 1e6;
1495
+ var INITIAL_MODEL_ATTEMPT = 1;
1496
+ var NO_TOKENS = 0;
1497
+ var QUICK_MAX_OUTPUT_TOKENS = 8192;
1498
+ var QUICK_MAX_STEPS = 16;
1499
+ var ANALYSIS_MAX_OUTPUT_TOKENS = 8192;
1500
+ var ANALYSIS_MAX_STEPS = 24;
1501
+ var IMPLEMENTATION_MAX_OUTPUT_TOKENS = 65536;
1502
+ var IMPLEMENTATION_MAX_STEPS = 48;
1503
+ var VALIDATION_MAX_OUTPUT_TOKENS = 8192;
1504
+ var VALIDATION_MAX_STEPS = 24;
1505
+ var HAIKU_4_5_PRICES = {
1506
+ input: 1,
1507
+ output: 5,
1508
+ cacheRead: 0.1,
1509
+ cacheWrite: 1.25
1510
+ };
1511
+ var SONNET_5_PRICES = {
1512
+ input: 2,
1513
+ output: 10,
1514
+ cacheRead: 0.2,
1515
+ cacheWrite: 2.5
1516
+ };
1517
+ var MODEL_PROFILES = {
1518
+ quick: {
1519
+ model: "claude-haiku-4-5",
1520
+ thinking: "disabled",
1521
+ maxOutputTokens: QUICK_MAX_OUTPUT_TOKENS,
1522
+ maxSteps: QUICK_MAX_STEPS,
1523
+ prices: HAIKU_4_5_PRICES
1524
+ },
1525
+ analysis: {
1526
+ model: "claude-sonnet-5",
1527
+ thinking: "adaptive",
1528
+ effort: "low",
1529
+ maxOutputTokens: ANALYSIS_MAX_OUTPUT_TOKENS,
1530
+ maxSteps: ANALYSIS_MAX_STEPS,
1531
+ prices: SONNET_5_PRICES
1532
+ },
1533
+ implementation: {
1534
+ model: "claude-sonnet-5",
1535
+ thinking: "adaptive",
1536
+ effort: "medium",
1537
+ maxOutputTokens: IMPLEMENTATION_MAX_OUTPUT_TOKENS,
1538
+ maxSteps: IMPLEMENTATION_MAX_STEPS,
1539
+ prices: SONNET_5_PRICES
1540
+ },
1541
+ implementationRetry: {
1542
+ model: "claude-sonnet-5",
1543
+ thinking: "adaptive",
1544
+ effort: "high",
1545
+ maxOutputTokens: IMPLEMENTATION_MAX_OUTPUT_TOKENS,
1546
+ maxSteps: IMPLEMENTATION_MAX_STEPS,
1547
+ prices: SONNET_5_PRICES
1548
+ },
1549
+ validation: {
1550
+ model: "claude-sonnet-5",
1551
+ thinking: "adaptive",
1552
+ effort: "low",
1553
+ maxOutputTokens: VALIDATION_MAX_OUTPUT_TOKENS,
1554
+ maxSteps: VALIDATION_MAX_STEPS,
1555
+ prices: SONNET_5_PRICES
1556
+ }
1557
+ };
1558
+ function getModelProfile(profileName) {
1559
+ return MODEL_PROFILES[profileName];
1560
+ }
1561
+ function providerOptionsForProfile(profileName) {
1562
+ const profile = getModelProfile(profileName);
1563
+ return {
1564
+ thinking: { type: profile.thinking },
1565
+ ...profile.effort !== void 0 && { effort: profile.effort }
1566
+ };
1567
+ }
1568
+ function tokenCount(value = NO_TOKENS) {
1569
+ return value;
1570
+ }
1571
+ function estimateModelCost(profileName, usage) {
1572
+ const { prices } = getModelProfile(profileName);
1573
+ const inputDetails = Object.assign({}, usage.inputTokenDetails);
1574
+ const cacheRead = tokenCount(inputDetails.cacheReadTokens);
1575
+ const cacheWrite = tokenCount(inputDetails.cacheWriteTokens);
1576
+ const input = Math.max(
1577
+ NO_TOKENS,
1578
+ tokenCount(usage.inputTokens) - cacheRead - cacheWrite
1579
+ );
1580
+ const output = tokenCount(usage.outputTokens);
1581
+ return (input * prices.input + cacheRead * prices.cacheRead + cacheWrite * prices.cacheWrite + output * prices.output) / TOKENS_PER_MILLION;
1582
+ }
1583
+
1493
1584
  // src/lib/telemetry.ts
1585
+ var COUNT_METRIC = 1;
1586
+ var GAUGE_METRIC = 3;
1587
+ var FIRST_ATTEMPT = 1;
1588
+ var NO_TOKENS2 = 0;
1589
+ var AGENT_TELEMETRY_CONTEXT = {
1590
+ appId: void 0,
1591
+ workflowId: void 0
1592
+ };
1494
1593
  function isTelemetryOptedOut() {
1495
1594
  return process.env.WIZARD_TELEMETRY === "false";
1496
1595
  }
@@ -1537,6 +1636,110 @@ function sendMetric(name, value, type, tags = []) {
1537
1636
  function metricTags(workflowId, actionId) {
1538
1637
  return actionId ? [`workflow:${workflowId}`, `action:${actionId}`] : [`workflow:${workflowId}`];
1539
1638
  }
1639
+ function agentMetricTags(event, workflowId) {
1640
+ const profile = getModelProfile(event.profile);
1641
+ return [
1642
+ ...workflowId ? [`workflow:${workflowId}`] : [],
1643
+ `operation:${event.operation}`,
1644
+ `profile:${event.profile}`,
1645
+ `model:${profile.model}`,
1646
+ `effort:${profile.effort ?? "none"}`,
1647
+ `thinking:${profile.thinking}`
1648
+ ];
1649
+ }
1650
+ function agentRunValues(event) {
1651
+ const usage = Object.assign({}, event.usage);
1652
+ const inputTokenDetails = Object.assign({}, usage.inputTokenDetails);
1653
+ return {
1654
+ inputTokens: usage.inputTokens ?? NO_TOKENS2,
1655
+ outputTokens: usage.outputTokens ?? NO_TOKENS2,
1656
+ cacheReadTokens: inputTokenDetails.cacheReadTokens ?? NO_TOKENS2,
1657
+ cacheWriteTokens: inputTokenDetails.cacheWriteTokens ?? NO_TOKENS2,
1658
+ estimatedCostUsd: estimateModelCost(event.profile, usage)
1659
+ };
1660
+ }
1661
+ function agentRunMetrics(event, values, tags) {
1662
+ const runTags = [...tags];
1663
+ return [
1664
+ {
1665
+ name: "wizard.agent.duration_ms",
1666
+ value: event.durationMs,
1667
+ type: GAUGE_METRIC,
1668
+ tags: runTags
1669
+ },
1670
+ {
1671
+ name: "wizard.agent.input_tokens",
1672
+ value: values.inputTokens,
1673
+ type: COUNT_METRIC,
1674
+ tags: runTags
1675
+ },
1676
+ {
1677
+ name: "wizard.agent.output_tokens",
1678
+ value: values.outputTokens,
1679
+ type: COUNT_METRIC,
1680
+ tags: runTags
1681
+ },
1682
+ {
1683
+ name: "wizard.agent.cache_read_tokens",
1684
+ value: values.cacheReadTokens,
1685
+ type: COUNT_METRIC,
1686
+ tags: runTags
1687
+ },
1688
+ {
1689
+ name: "wizard.agent.cache_write_tokens",
1690
+ value: values.cacheWriteTokens,
1691
+ type: COUNT_METRIC,
1692
+ tags: runTags
1693
+ },
1694
+ {
1695
+ name: "wizard.agent.estimated_cost_usd",
1696
+ value: values.estimatedCostUsd,
1697
+ type: COUNT_METRIC,
1698
+ tags: runTags
1699
+ },
1700
+ {
1701
+ name: "wizard.agent.retry",
1702
+ value: Number(event.attempt > FIRST_ATTEMPT),
1703
+ type: COUNT_METRIC,
1704
+ tags: runTags
1705
+ }
1706
+ ];
1707
+ }
1708
+ function trackAgentRun(event) {
1709
+ const profile = getModelProfile(event.profile);
1710
+ const values = agentRunValues(event);
1711
+ const tagsForMetrics = agentMetricTags(
1712
+ event,
1713
+ AGENT_TELEMETRY_CONTEXT.workflowId
1714
+ );
1715
+ const tags = [
1716
+ ...tagsForMetrics,
1717
+ ...AGENT_TELEMETRY_CONTEXT.appId ? [`app_id:${AGENT_TELEMETRY_CONTEXT.appId}`] : []
1718
+ ];
1719
+ sendTelemetry({
1720
+ logs: [
1721
+ {
1722
+ status: "info",
1723
+ message: "wizard agent completed",
1724
+ attributes: {
1725
+ event: "wizard.agent.complete",
1726
+ operation: event.operation,
1727
+ profile: event.profile,
1728
+ model: profile.model,
1729
+ effort: profile.effort ?? "none",
1730
+ thinking: profile.thinking,
1731
+ attempt: event.attempt,
1732
+ durationMs: event.durationMs,
1733
+ workflow_id: AGENT_TELEMETRY_CONTEXT.workflowId,
1734
+ app_id: AGENT_TELEMETRY_CONTEXT.appId,
1735
+ ...values
1736
+ },
1737
+ tags
1738
+ }
1739
+ ],
1740
+ metrics: agentRunMetrics(event, values, tagsForMetrics)
1741
+ });
1742
+ }
1540
1743
  function logTags(workflowId, actionId, appId) {
1541
1744
  return [
1542
1745
  ...metricTags(workflowId, actionId),
@@ -1552,6 +1755,8 @@ function emitTelemetryLog(level, message, attributes, tags) {
1552
1755
  sendLog(level, message, attributes, tags);
1553
1756
  }
1554
1757
  function trackWorkflowStart(ctx) {
1758
+ AGENT_TELEMETRY_CONTEXT.appId = ctx.appId;
1759
+ AGENT_TELEMETRY_CONTEXT.workflowId = ctx.workflowId;
1555
1760
  const attributes = {
1556
1761
  event: "wizard.workflow.start",
1557
1762
  workflow_id: ctx.workflowId,
@@ -1677,6 +1882,8 @@ function trackWizardComplete(event) {
1677
1882
  attributes,
1678
1883
  logTags(event.workflowId, void 0, event.appId)
1679
1884
  );
1885
+ AGENT_TELEMETRY_CONTEXT.appId = void 0;
1886
+ AGENT_TELEMETRY_CONTEXT.workflowId = void 0;
1680
1887
  }
1681
1888
  function trackWorkflowError(ctx) {
1682
1889
  const attributes = {
@@ -1693,6 +1900,8 @@ function trackWorkflowError(ctx) {
1693
1900
  attributes,
1694
1901
  logTags(ctx.workflowId, ctx.actionId, ctx.appId)
1695
1902
  );
1903
+ AGENT_TELEMETRY_CONTEXT.appId = void 0;
1904
+ AGENT_TELEMETRY_CONTEXT.workflowId = void 0;
1696
1905
  }
1697
1906
 
1698
1907
  // src/lib/events.ts
@@ -1760,7 +1969,7 @@ function identify(traits) {
1760
1969
  // package.json
1761
1970
  var package_default = {
1762
1971
  name: "@algolia/wizard",
1763
- version: "0.65.0",
1972
+ version: "0.67.0",
1764
1973
  description: "Magically implement Algolia functionality in your codebase",
1765
1974
  type: "module",
1766
1975
  engines: {
@@ -2306,7 +2515,8 @@ import {
2306
2515
  hasToolCall,
2307
2516
  Output as Output3,
2308
2517
  APICallError,
2309
- NoOutputGeneratedError
2518
+ NoOutputGeneratedError,
2519
+ stepCountIs
2310
2520
  } from "ai";
2311
2521
  import { createAnthropic as createAnthropic3 } from "@ai-sdk/anthropic";
2312
2522
  import "zod";
@@ -3297,6 +3507,7 @@ function runShell(command, opts) {
3297
3507
  }
3298
3508
 
3299
3509
  // src/lib/tools/runShell.ts
3510
+ var CLASSIFIER_MAX_OUTPUT_TOKENS = 512;
3300
3511
  function storeApproval(root) {
3301
3512
  return async (req) => {
3302
3513
  const store = useWizard.getState();
@@ -3510,7 +3721,6 @@ function fastPathSafety(command) {
3510
3721
  if (results.every((r) => r === true)) return true;
3511
3722
  return void 0;
3512
3723
  }
3513
- var CLASSIFIER_MODEL = "claude-haiku-4-5";
3514
3724
  var commandSafetySchema = z16.object({
3515
3725
  safe: z16.boolean(),
3516
3726
  reason: z16.string().describe("One short sentence explaining the verdict.")
@@ -3535,9 +3745,21 @@ function approvedCommandHistory(approvedCommands) {
3535
3745
  async function classifyCommandSafety(createModel, command, cwd, explanation, approvedHistory) {
3536
3746
  try {
3537
3747
  const anthropic = createModel();
3538
- const { output } = await generateText({
3539
- model: anthropic(CLASSIFIER_MODEL),
3748
+ const profile = getModelProfile("quick" /* quick */);
3749
+ const modelOptions = providerOptionsForProfile("quick" /* quick */);
3750
+ const startedAt = Date.now();
3751
+ const { output, usage } = await generateText({
3752
+ model: anthropic(profile.model),
3753
+ maxOutputTokens: CLASSIFIER_MAX_OUTPUT_TOKENS,
3540
3754
  temperature: 0,
3755
+ providerOptions: {
3756
+ anthropic: {
3757
+ thinking: modelOptions.thinking,
3758
+ ...modelOptions.effort !== void 0 && {
3759
+ effort: modelOptions.effort
3760
+ }
3761
+ }
3762
+ },
3541
3763
  output: Output.object({ schema: commandSafetySchema }),
3542
3764
  prompt: [
3543
3765
  "A coding agent wants to run this shell command in a user's project without asking for approval first. The command may be in any programming language or ecosystem.",
@@ -3558,6 +3780,13 @@ async function classifyCommandSafety(createModel, command, cwd, explanation, app
3558
3780
  `Stated purpose: ${explanation}`
3559
3781
  ].join("\n")
3560
3782
  });
3783
+ trackAgentRun({
3784
+ operation: "shell-safety-classifier",
3785
+ profile: "quick" /* quick */,
3786
+ attempt: INITIAL_MODEL_ATTEMPT,
3787
+ durationMs: Date.now() - startedAt,
3788
+ usage
3789
+ });
3561
3790
  if (!output.safe) {
3562
3791
  logger.info(
3563
3792
  { command, reason: output.reason },
@@ -3702,7 +3931,6 @@ import { createAnthropic as createAnthropic2 } from "@ai-sdk/anthropic";
3702
3931
  import { nanoid as nanoid2 } from "nanoid";
3703
3932
  import z17 from "zod";
3704
3933
  var DATA_DIR = ".algolia-wizard/data";
3705
- var RECORD_MODEL = "claude-haiku-4-5";
3706
3934
  var MAX_RECORDS = 100;
3707
3935
  var BATCH_SIZE = 10;
3708
3936
  var MAX_BATCH_ATTEMPTS = 3;
@@ -3737,9 +3965,21 @@ function generateRecordTool(ctx, createModel = defaultCreateModel2) {
3737
3965
  const generateBatch = async (batchCount) => {
3738
3966
  let lastError;
3739
3967
  for (let attempt = 1; attempt <= MAX_BATCH_ATTEMPTS; attempt++) {
3968
+ const profile = getModelProfile("quick" /* quick */);
3969
+ const modelOptions = providerOptionsForProfile("quick" /* quick */);
3970
+ const startedAt = Date.now();
3740
3971
  try {
3741
- const { output } = await generateText2({
3742
- model: anthropic(RECORD_MODEL),
3972
+ const { output, usage } = await generateText2({
3973
+ model: anthropic(profile.model),
3974
+ maxOutputTokens: profile.maxOutputTokens,
3975
+ providerOptions: {
3976
+ anthropic: {
3977
+ thinking: modelOptions.thinking,
3978
+ ...modelOptions.effort !== void 0 && {
3979
+ effort: modelOptions.effort
3980
+ }
3981
+ }
3982
+ },
3743
3983
  output: Output2.object({
3744
3984
  schema: z17.object({
3745
3985
  records: z17.array(recordSchema2).length(batchCount)
@@ -3751,9 +3991,23 @@ function generateRecordTool(ctx, createModel = defaultCreateModel2) {
3751
3991
  `Variety seed: ${nanoid2()}. Use it to diversify values.`
3752
3992
  ].filter(Boolean).join("\n")
3753
3993
  });
3994
+ trackAgentRun({
3995
+ operation: "sample-record-generation",
3996
+ profile: "quick" /* quick */,
3997
+ attempt,
3998
+ durationMs: Date.now() - startedAt,
3999
+ usage
4000
+ });
3754
4001
  return output.records;
3755
4002
  } catch (err) {
3756
4003
  if (!NoObjectGeneratedError.isInstance(err)) throw err;
4004
+ trackAgentRun({
4005
+ operation: "sample-record-generation",
4006
+ profile: "quick" /* quick */,
4007
+ attempt,
4008
+ durationMs: Date.now() - startedAt,
4009
+ usage: err.usage
4010
+ });
3757
4011
  lastError = err;
3758
4012
  logger.warn(
3759
4013
  { entityName, batchCount, attempt, err },
@@ -3867,14 +4121,10 @@ var FS_READ_TOOLS = [
3867
4121
  ];
3868
4122
 
3869
4123
  // src/lib/agent.ts
3870
- var MODEL_BY_SIZE = {
3871
- small: "claude-haiku-4-5",
3872
- medium: "claude-sonnet-4-6",
3873
- large: "claude-opus-4-8"
3874
- };
3875
4124
  var MISSING_REPORT_STATUS_ERROR_MESSAGE = "Agent finished without calling reportStatus";
3876
4125
  var MISSING_REPORT_USER_MESSAGE = "This step ran into a problem finishing. Run the wizard again to retry it.";
3877
4126
  var REPORT_STATUS_RETRIES = 2;
4127
+ var ATTEMPT_NUMBER_OFFSET = 1;
3878
4128
  var PROVIDER_ERROR_USER_MESSAGE = "The AI service had trouble responding. Run the wizard again to retry this step.";
3879
4129
  function retryKind(err) {
3880
4130
  if (err instanceof Error && err.message === MISSING_REPORT_STATUS_ERROR_MESSAGE) {
@@ -3907,7 +4157,20 @@ async function runAgent(req) {
3907
4157
  }
3908
4158
  async function runAgentAttempt(req, attempt) {
3909
4159
  const start = Date.now();
3910
- logger.info({ startedAt: new Date(start).toISOString() }, "runAgent started");
4160
+ const profileName = req.modelProfile ?? "implementation" /* implementation */;
4161
+ const profile = getModelProfile(profileName);
4162
+ const modelOptions = providerOptionsForProfile(profileName);
4163
+ logger.info(
4164
+ {
4165
+ startedAt: new Date(start).toISOString(),
4166
+ operation: req.operation,
4167
+ profile: profileName,
4168
+ model: profile.model,
4169
+ effort: profile.effort,
4170
+ thinking: profile.thinking
4171
+ },
4172
+ "runAgent started"
4173
+ );
3911
4174
  const token = getAuthToken();
3912
4175
  if (!token) {
3913
4176
  throw new Error("Not authenticated: no user token available");
@@ -3934,7 +4197,16 @@ async function runAgentAttempt(req, attempt) {
3934
4197
  ] : []
3935
4198
  ];
3936
4199
  const agent = new ToolLoopAgent({
3937
- model: anthropic(MODEL_BY_SIZE[req.modelSize ?? "medium"]),
4200
+ model: anthropic(profile.model),
4201
+ maxOutputTokens: profile.maxOutputTokens,
4202
+ providerOptions: {
4203
+ anthropic: {
4204
+ thinking: modelOptions.thinking,
4205
+ ...modelOptions.effort !== void 0 && {
4206
+ effort: modelOptions.effort
4207
+ }
4208
+ }
4209
+ },
3938
4210
  // Cache tools + system on the last system block. Tools render before
3939
4211
  // system, so one breakpoint here caches both, reused on every loop turn
3940
4212
  // after the first.
@@ -3955,7 +4227,10 @@ async function runAgentAttempt(req, attempt) {
3955
4227
  tools: req.tools
3956
4228
  }),
3957
4229
  toolChoice: "required",
3958
- stopWhen: [hasToolCall("reportStatus")]
4230
+ stopWhen: [
4231
+ hasToolCall("reportStatus"),
4232
+ stepCountIs(profile.maxSteps)
4233
+ ]
3959
4234
  });
3960
4235
  const stream = await agent.stream({
3961
4236
  prompt: "Follow system instructions"
@@ -3986,13 +4261,20 @@ async function runAgentAttempt(req, attempt) {
3986
4261
  }
3987
4262
  const end = Date.now();
3988
4263
  const usage = await stream.totalUsage;
4264
+ trackAgentRun({
4265
+ operation: req.operation,
4266
+ profile: profileName,
4267
+ attempt: attempt + ATTEMPT_NUMBER_OFFSET,
4268
+ durationMs: end - start,
4269
+ usage
4270
+ });
3989
4271
  logger.info(
3990
4272
  {
3991
4273
  finishedAt: new Date(end).toISOString(),
3992
4274
  durationMs: end - start,
3993
- // cachedInputTokens > 0 confirms prompt caching engaged. If it stays 0
3994
- // across turns, the tools+system prefix is under the model's min
3995
- // cacheable size (2048 tokens for sonnet-4-6) and caching is a no-op.
4275
+ operation: req.operation,
4276
+ profile: profileName,
4277
+ model: profile.model,
3996
4278
  usage
3997
4279
  },
3998
4280
  "runAgent finished"
@@ -4073,6 +4355,7 @@ function prioritizeFrontendFrameworks(result) {
4073
4355
  }
4074
4356
  var detectLanguage = async () => {
4075
4357
  const result = await runAgent({
4358
+ operation: "language-detection",
4076
4359
  instructions: [
4077
4360
  "Analyze the codebase and determine the programming languages and frameworks used",
4078
4361
  "If a superset language is found, exclude the subset language (e.g. TypeScript over JavaScript).",
@@ -4088,7 +4371,7 @@ var detectLanguage = async () => {
4088
4371
  ],
4089
4372
  tools: ["listFiles", "changeDirectory", "readFile", "searchFiles"],
4090
4373
  outputSchema: detectLanguageSchema,
4091
- modelSize: "small"
4374
+ modelProfile: "quick" /* quick */
4092
4375
  });
4093
4376
  return prioritizeFrontendFrameworks(result);
4094
4377
  };
@@ -4164,9 +4447,11 @@ var MODE_CONFIG = {
4164
4447
  function runMode(mode, extraInstructions = []) {
4165
4448
  const { instructions, outputSchema } = MODE_CONFIG[mode];
4166
4449
  return runAgent({
4450
+ operation: `code-analysis-${mode}`,
4167
4451
  instructions: [...instructions, ...extraInstructions],
4168
4452
  tools: READONLY_TOOLS,
4169
- outputSchema
4453
+ outputSchema,
4454
+ modelProfile: "analysis" /* analysis */
4170
4455
  });
4171
4456
  }
4172
4457
  async function runAnalysis(mode, extraInstructions = []) {
@@ -4544,6 +4829,7 @@ ${JSON.stringify(s.output, null, 2)}`
4544
4829
  }
4545
4830
  var reviewStep = async (ctx, options) => {
4546
4831
  const result = await runAgent({
4832
+ operation: "workflow-review",
4547
4833
  instructions: [
4548
4834
  "Summarize what was accomplished in the workflow, leaving out verbose details.",
4549
4835
  "Base your summary only on the step outputs provided \u2014 do not read the repository.",
@@ -4558,7 +4844,7 @@ ${formatCompletedSteps(ctx.completedSteps)}`,
4558
4844
  ],
4559
4845
  tools: [],
4560
4846
  outputSchema: reviewSchema,
4561
- modelSize: "small"
4847
+ modelProfile: "quick" /* quick */
4562
4848
  });
4563
4849
  ctx.clearNotices();
4564
4850
  useWizard.getState().setReview(result);
@@ -4567,7 +4853,7 @@ ${formatCompletedSteps(ctx.completedSteps)}`,
4567
4853
 
4568
4854
  // src/actions/implement.ts
4569
4855
  import z28 from "zod";
4570
- import { mkdir as mkdir5, readFile as readFile8 } from "node:fs/promises";
4856
+ import { access, mkdir as mkdir5, readFile as readFile8 } from "node:fs/promises";
4571
4857
  import { join as join10, relative as relative6 } from "node:path";
4572
4858
 
4573
4859
  // src/lib/git.ts
@@ -4732,12 +5018,14 @@ var implementationOutputSchema = z28.object({
4732
5018
  // won't end up gitignored (it's public, meant to be committed).
4733
5019
  searchConfigFile: z28.string().optional()
4734
5020
  });
4735
- var verificationOutputSchema = z28.object({
5021
+ var validationOutputSchema = z28.object({
4736
5022
  summary: z28.string(),
4737
5023
  sufficient: z28.boolean(),
4738
- additionalInstructions: z28.string().optional()
5024
+ additionalInstructions: z28.string().optional(),
5025
+ unrelatedFailure: z28.string().optional()
4739
5026
  });
4740
- var MAX_IMPLEMENT_VERIFICATION_ATTEMPTS = 3;
5027
+ var MAX_IMPLEMENT_VALIDATION_ATTEMPTS = 3;
5028
+ var FIRST_IMPLEMENTATION_ATTEMPT = 1;
4741
5029
  var DEFAULT_IMPLEMENT_USE_CASES = ["ingestion", "search"];
4742
5030
  var INGEST_DIR = ".algolia-wizard";
4743
5031
  var INGESTION_SOURCE_PROMPT = "What data do you want to index?";
@@ -4746,6 +5034,9 @@ var INGESTION_SOURCE = {
4746
5034
  fileUpload: "Import a data file (CSV, JSON, TSV)",
4747
5035
  generated: "Generate sample data"
4748
5036
  };
5037
+ var BUILD_CHECK_PROMPT = "Run type-check and lint now? This can take several minutes on a large repository.";
5038
+ var MISSING_DEPENDENCIES_PROMPT = "Dependencies are not installed. Install them and run type-check and lint now?";
5039
+ var UNRELATED_FAILURE_PROMPT = "These checks are still failing and appear unrelated to the changes. Continue the investigation?";
4749
5040
  var FILE_UPLOAD_PATH_PROMPT = "Enter the path to your JSON, CSV, or TSV file (relative to the project root, or absolute):";
4750
5041
  var JS_LANGUAGES = ["javascript", "typescript", "jsx", "tsx", "node"];
4751
5042
  function lower(entries) {
@@ -4870,19 +5161,19 @@ function searchInstructions(input) {
4870
5161
  "The summary should be extremely concise; do not mention manual testing steps."
4871
5162
  ];
4872
5163
  }
4873
- function verificationInstructions(input) {
5164
+ function validationInstructions(input) {
4874
5165
  return [
4875
- "Verify the Algolia implementation changes.",
4876
- `Verification tools found in the codebase: ${JSON.stringify(input.findings.verification ?? [])}.`,
4877
- "Run the project's own checks (lint, type check, tests) via runShell, using the project's commands \u2014 its task runner, manifest scripts, etc. Run every check that applies, not just the first.",
4878
- "If a check fails because packages or modules are missing, install the dependencies via runShell and re-run it rather than changing the code.",
4879
- "For issues caused by the implementation, make minimal fixes with writeFile and re-run the checks.",
4880
- "Do not make speculative fixes when no checks exist, a check cannot run, or failures are unrelated to these changes \u2014 note the limitation in your summary.",
4881
- "Do not add new Algolia functionality here; only validate and make minimal correctness fixes.",
4882
- `Do not modify "${input.ingestDir}/" unless a check reports an actionable issue in its files.`,
4883
- "Always call reportStatus with status=success once verification has run, even when sufficient=false.",
4884
- "Set sufficient=true only when the implementation is complete and checks pass (or fail for a clearly unrelated reason).",
4885
- "Set sufficient=false when the implementation is incomplete or has implementation-caused failures; include concrete additionalInstructions for the next pass."
5166
+ "Validate the Algolia search implementation by inspecting the project source.",
5167
+ "Confirm that the application renders exactly one search bar. Check for an old search bar and duplicate mounts.",
5168
+ `Confirm that the search bar is mounted in ${input.searchLocation ? `"${input.searchLocation}"` : "the best always-rendered shared layout location"}.`,
5169
+ "Confirm that the search component is reachable across the application and fits the surrounding layout.",
5170
+ "Do not run the test suite or any test command.",
5171
+ "Do not modify files. Report each implementation issue as a concrete instruction for the next implementation pass.",
5172
+ `Available type-check and lint tools: ${JSON.stringify(input.findings.verification ?? [])}.`,
5173
+ "Always call reportStatus with status=success after validation, even when sufficient=false.",
5174
+ "Set sufficient=true only when the search bar is unique, correctly placed, and structurally complete.",
5175
+ "Set sufficient=false for an implementation issue. Include concrete additionalInstructions for the next pass.",
5176
+ "If a check fails only in untouched code, set unrelatedFailure to a concise failure description."
4886
5177
  ];
4887
5178
  }
4888
5179
  var IMPLEMENT_CONFIG = {
@@ -4894,9 +5185,9 @@ var IMPLEMENT_CONFIG = {
4894
5185
  title: "Algolia search",
4895
5186
  buildInstructions: searchInstructions
4896
5187
  },
4897
- verification: {
4898
- title: "Algolia verification",
4899
- buildInstructions: verificationInstructions
5188
+ validation: {
5189
+ title: "Algolia validation",
5190
+ buildInstructions: validationInstructions
4900
5191
  }
4901
5192
  };
4902
5193
  var useCaseToolMap = {
@@ -4909,7 +5200,7 @@ var useCaseToolMap = {
4909
5200
  "notifyUser"
4910
5201
  ],
4911
5202
  search: [...FS_READ_TOOLS, "writeFile", "runShell", "notifyUser"],
4912
- verification: [...FS_READ_TOOLS, "writeFile", "runShell", "notifyUser"]
5203
+ validation: [...FS_READ_TOOLS, "runShell", "notifyUser"]
4913
5204
  };
4914
5205
  function toolsForUseCase(useCase, ingestionSource) {
4915
5206
  const tools = useCaseToolMap[useCase];
@@ -4929,7 +5220,7 @@ function buildAgentInstructions(useCase, input, extraInstructions = []) {
4929
5220
  ];
4930
5221
  }
4931
5222
  function formatSummary(useCase, summary) {
4932
- const label = useCase === "ingestion" ? "Ingestion" : useCase === "search" ? "Search" : "Verification";
5223
+ const label = useCase === "ingestion" ? "Ingestion" : useCase === "search" ? "Search" : "Validation";
4933
5224
  return `${label}: ${summary}`;
4934
5225
  }
4935
5226
  function parseIngestRecordCount(output) {
@@ -5021,9 +5312,62 @@ function makeToolContext(root, env = async () => ({})) {
5021
5312
  createShellContext({ env, approve: storeApproval(root) })
5022
5313
  );
5023
5314
  }
5024
- function verificationRetryInstructions(verification) {
5315
+ function validationRetryInstructions(validation) {
5316
+ return [
5317
+ `Implementation insufficient. Address these findings before reporting completion: ${validation.additionalInstructions ?? validation.summary}`
5318
+ ];
5319
+ }
5320
+ async function pathExists(path) {
5321
+ try {
5322
+ await access(path);
5323
+ return true;
5324
+ } catch {
5325
+ return false;
5326
+ }
5327
+ }
5328
+ async function resolveBuildCheckPlan(ctx, repoRoot, input) {
5329
+ if (!input.findings.verification?.length) {
5330
+ return { run: false, installDependencies: false };
5331
+ }
5332
+ const installDependencies = isJsProject(input.language) && !await pathExists(join10(repoRoot, "node_modules"));
5333
+ const accepted = Boolean(
5334
+ await ctx.requestUserInput({
5335
+ prompt: installDependencies ? MISSING_DEPENDENCIES_PROMPT : BUILD_CHECK_PROMPT,
5336
+ promptType: "acceptReject",
5337
+ options: ["Yes", "No"],
5338
+ messages: []
5339
+ })
5340
+ );
5341
+ return {
5342
+ run: accepted,
5343
+ installDependencies: accepted && installDependencies
5344
+ };
5345
+ }
5346
+ function installCheckInstructions(plan, attempt) {
5347
+ if (!plan.installDependencies) {
5348
+ return [];
5349
+ }
5350
+ if (attempt === 1) {
5351
+ return [
5352
+ "Install dependencies once with the project's declared package manager before any check.",
5353
+ "Do not try alternate install commands. If installation fails, stop the checks and report the limitation."
5354
+ ];
5355
+ }
5356
+ return ["Dependencies were already installed. Do not install them again."];
5357
+ }
5358
+ function validationRunInstructions(plan, changedFiles, attempt) {
5359
+ if (!plan.run) {
5360
+ return [
5361
+ "The developer chose to skip build checks. Do not install dependencies or run shell checks."
5362
+ ];
5363
+ }
5025
5364
  return [
5026
- `Implementation insufficient. Address these findings before reporting completion: ${verification.additionalInstructions ?? verification.summary}`
5365
+ "The developer approved the optional build checks.",
5366
+ ...installCheckInstructions(plan, attempt),
5367
+ "Do not use npx, dlx, or another package runner that can download an undeclared tool.",
5368
+ "Run the type check once with the project's existing command, when one is available.",
5369
+ `Lint only these changed files: ${JSON.stringify(changedFiles)}. Do not run a full-project lint. If this list is empty, do not run lint.`,
5370
+ "Do not use an autofix option during validation."
5027
5371
  ];
5028
5372
  }
5029
5373
  async function resolveIngestionSource(ctx, repoRoot) {
@@ -5178,10 +5522,11 @@ async function implement(ctx, useCases = DEFAULT_IMPLEMENT_USE_CASES) {
5178
5522
  [INDEX_NAME_VAR]: targetIndex
5179
5523
  })) : void 0;
5180
5524
  const searchTools = makeToolContext(repoRoot);
5181
- async function runImplementationUseCase(currentUseCase, extraInstructions = []) {
5525
+ async function runImplementationUseCase(currentUseCase, extraInstructions = [], isRetry = false) {
5182
5526
  if (agentRuns > 0) ctx.recordStepExecution();
5183
5527
  agentRuns += 1;
5184
5528
  return runAgent({
5529
+ operation: `${currentUseCase}-implementation`,
5185
5530
  instructions: buildAgentInstructions(
5186
5531
  currentUseCase,
5187
5532
  input,
@@ -5189,16 +5534,23 @@ async function implement(ctx, useCases = DEFAULT_IMPLEMENT_USE_CASES) {
5189
5534
  ),
5190
5535
  tools: toolsForUseCase(currentUseCase, input.ingestionSource),
5191
5536
  outputSchema: implementationOutputSchema,
5537
+ modelProfile: isRetry ? "implementationRetry" /* implementationRetry */ : "implementation" /* implementation */,
5192
5538
  toolContext: currentUseCase === "ingestion" ? ingestionTools ?? searchTools : searchTools
5193
5539
  });
5194
5540
  }
5195
- async function runVerificationUseCase() {
5541
+ async function runValidationUseCase(extraInstructions) {
5196
5542
  if (agentRuns > 0) ctx.recordStepExecution();
5197
5543
  agentRuns += 1;
5198
5544
  return runAgent({
5199
- instructions: buildAgentInstructions("verification", input),
5200
- tools: toolsForUseCase("verification"),
5201
- outputSchema: verificationOutputSchema,
5545
+ operation: "search-validation",
5546
+ instructions: buildAgentInstructions(
5547
+ "validation",
5548
+ input,
5549
+ extraInstructions
5550
+ ),
5551
+ tools: toolsForUseCase("validation"),
5552
+ outputSchema: validationOutputSchema,
5553
+ modelProfile: "validation" /* validation */,
5202
5554
  toolContext: searchTools
5203
5555
  });
5204
5556
  }
@@ -5307,6 +5659,7 @@ ${detail}` : ""}`
5307
5659
  let searchConfigFile;
5308
5660
  if (useCases.includes("search")) {
5309
5661
  let extraInstructions = [];
5662
+ let buildCheckPlan;
5310
5663
  const confirmedFrameworks = language.frameworks ?? [];
5311
5664
  const detectedFrameworks = scan.frameworks ?? [];
5312
5665
  const framework = confirmedFrameworks[0]?.name ?? "unknown";
@@ -5331,40 +5684,75 @@ ${detail}` : ""}`
5331
5684
  });
5332
5685
  };
5333
5686
  useWizard.getState().clearWrittenFiles();
5334
- for (let attempt = 1; attempt <= MAX_IMPLEMENT_VERIFICATION_ATTEMPTS; attempt++) {
5335
- if (attempt > 1) {
5687
+ for (let attempt = FIRST_IMPLEMENTATION_ATTEMPT; attempt <= MAX_IMPLEMENT_VALIDATION_ATTEMPTS; attempt++) {
5688
+ if (attempt > FIRST_IMPLEMENTATION_ATTEMPT) {
5336
5689
  logger.info(
5337
5690
  {
5338
5691
  attempt,
5339
- maxAttempts: MAX_IMPLEMENT_VERIFICATION_ATTEMPTS,
5692
+ maxAttempts: MAX_IMPLEMENT_VALIDATION_ATTEMPTS,
5340
5693
  extraInstructions
5341
5694
  },
5342
- "implement: retrying search implementation after failed verification"
5695
+ "implement: retrying search implementation after failed validation"
5343
5696
  );
5344
5697
  }
5345
5698
  const searchResult = await runImplementationUseCase(
5346
5699
  "search",
5347
- extraInstructions
5700
+ extraInstructions,
5701
+ attempt > FIRST_IMPLEMENTATION_ATTEMPT
5348
5702
  );
5349
5703
  summaries.push(formatSummary("search", searchResult.summary));
5350
5704
  if (searchResult.searchConfigFile) {
5351
5705
  searchConfigFile = searchResult.searchConfigFile;
5352
5706
  }
5353
- const verification = await runVerificationUseCase();
5354
- summaries.push(formatSummary("verification", verification.summary));
5355
- if (verification.sufficient) {
5707
+ buildCheckPlan ??= await resolveBuildCheckPlan(ctx, repoRoot, input);
5708
+ const searchFilesChanged = [
5709
+ ...new Set(useWizard.getState().writtenFiles)
5710
+ ].map((file) => relative6(repoRoot, file));
5711
+ const validation = await runValidationUseCase(
5712
+ validationRunInstructions(buildCheckPlan, searchFilesChanged, attempt)
5713
+ );
5714
+ summaries.push(formatSummary("validation", validation.summary));
5715
+ const finalAttempt = attempt === MAX_IMPLEMENT_VALIDATION_ATTEMPTS;
5716
+ let investigateUnrelated = false;
5717
+ if (validation.unrelatedFailure && !finalAttempt) {
5718
+ investigateUnrelated = Boolean(
5719
+ await ctx.requestUserInput({
5720
+ prompt: UNRELATED_FAILURE_PROMPT,
5721
+ promptType: "acceptReject",
5722
+ options: ["Yes", "No"],
5723
+ messages: [validation.unrelatedFailure]
5724
+ })
5725
+ );
5726
+ if (!investigateUnrelated) {
5727
+ summaries.push("Validation: Skipped unrelated check failures.");
5728
+ if (validation.sufficient) {
5729
+ ctx.setUserInput("implementation", "success");
5730
+ trackComponentGenerated(true, attempt);
5731
+ break;
5732
+ }
5733
+ }
5734
+ }
5735
+ if (validation.sufficient && (!validation.unrelatedFailure || finalAttempt)) {
5736
+ if (validation.unrelatedFailure) {
5737
+ summaries.push("Validation: Unrelated check failures remain.");
5738
+ }
5356
5739
  ctx.setUserInput("implementation", "success");
5357
5740
  trackComponentGenerated(true, attempt);
5358
5741
  break;
5359
5742
  }
5360
- if (attempt === MAX_IMPLEMENT_VERIFICATION_ATTEMPTS) {
5743
+ if (finalAttempt) {
5361
5744
  ctx.setUserInput("implementation", "fail");
5362
5745
  trackComponentGenerated(false, attempt);
5363
5746
  throw new Error(
5364
- `Implementation verification failed after ${MAX_IMPLEMENT_VERIFICATION_ATTEMPTS} attempts: ${verification.additionalInstructions ?? verification.summary}`
5747
+ `Unable to validate the implementation after ${MAX_IMPLEMENT_VALIDATION_ATTEMPTS} attempts: ${validation.additionalInstructions ?? validation.unrelatedFailure ?? validation.summary}`
5748
+ );
5749
+ }
5750
+ extraInstructions = validation.sufficient ? [] : validationRetryInstructions(validation);
5751
+ if (validation.unrelatedFailure && investigateUnrelated) {
5752
+ extraInstructions.push(
5753
+ `The developer asked you to investigate this check failure: ${validation.unrelatedFailure}`
5365
5754
  );
5366
5755
  }
5367
- extraInstructions = verificationRetryInstructions(verification);
5368
5756
  }
5369
5757
  if (searchConfigFile) {
5370
5758
  const ignoreStatus = await gitIgnoreStatus(
@@ -7074,7 +7462,7 @@ function delay(ms) {
7074
7462
  // package.json with { type: 'json' }
7075
7463
  var package_default2 = {
7076
7464
  name: "@algolia/wizard",
7077
- version: "0.65.0",
7465
+ version: "0.67.0",
7078
7466
  description: "Magically implement Algolia functionality in your codebase",
7079
7467
  type: "module",
7080
7468
  engines: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@algolia/wizard",
3
- "version": "0.65.0",
3
+ "version": "0.67.0",
4
4
  "description": "Magically implement Algolia functionality in your codebase",
5
5
  "type": "module",
6
6
  "engines": {