@algolia/wizard 0.31.0 → 0.33.0-rc.126.237

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 +203 -27
  2. package/package.json +1 -1
package/dist/main.js CHANGED
@@ -253,6 +253,7 @@ var useWizard = create((set, get) => ({
253
253
  cliOutput: [],
254
254
  targetIndex: null,
255
255
  writtenFiles: [],
256
+ approvedCommands: /* @__PURE__ */ new Set(),
256
257
  logs: [],
257
258
  error: null,
258
259
  inputReq: null,
@@ -348,6 +349,10 @@ var useWizard = create((set, get) => ({
348
349
  setTargetIndex: (index) => set({ targetIndex: index }),
349
350
  recordWrittenFile: (path) => set((s) => ({ writtenFiles: [...s.writtenFiles, path] })),
350
351
  clearWrittenFiles: () => set({ writtenFiles: [] }),
352
+ isCommandApproved: (command, cwd) => get().approvedCommands.has(`${cwd}\0${command}`),
353
+ approveCommand: (command, cwd) => set((s) => ({
354
+ approvedCommands: new Set(s.approvedCommands).add(`${cwd}\0${command}`)
355
+ })),
351
356
  logStart: (kind, name, input) => {
352
357
  const id = nanoid();
353
358
  set((s) => ({
@@ -396,6 +401,7 @@ var useWizard = create((set, get) => ({
396
401
  cliOutput: [],
397
402
  targetIndex: null,
398
403
  writtenFiles: [],
404
+ approvedCommands: /* @__PURE__ */ new Set(),
399
405
  logs: [],
400
406
  error: null,
401
407
  inputReq: null,
@@ -2710,8 +2716,8 @@ var selectIndexStep = async (ctx) => {
2710
2716
  };
2711
2717
 
2712
2718
  // src/lib/agent.ts
2713
- import { ToolLoopAgent, hasToolCall, Output as Output2 } from "ai";
2714
- import { createAnthropic as createAnthropic2 } from "@ai-sdk/anthropic";
2719
+ import { ToolLoopAgent, hasToolCall, Output as Output3 } from "ai";
2720
+ import { createAnthropic as createAnthropic3 } from "@ai-sdk/anthropic";
2715
2721
  import "zod";
2716
2722
 
2717
2723
  // src/lib/tools/index.ts
@@ -3393,7 +3399,8 @@ function searchFilesTool(ctx) {
3393
3399
  }
3394
3400
 
3395
3401
  // src/lib/tools/runShell.ts
3396
- import { tool as tool8 } from "ai";
3402
+ import { tool as tool8, generateText, Output } from "ai";
3403
+ import { createAnthropic } from "@ai-sdk/anthropic";
3397
3404
  import z15 from "zod";
3398
3405
  import { relative as relative4 } from "node:path";
3399
3406
 
@@ -3502,8 +3509,10 @@ function runShell(command, opts) {
3502
3509
  // src/lib/tools/runShell.ts
3503
3510
  function storeApproval(root) {
3504
3511
  return async (req) => {
3512
+ const store = useWizard.getState();
3513
+ if (store.isCommandApproved(req.command, req.cwd)) return "approve";
3505
3514
  const rel = relative4(root, req.cwd);
3506
- const answer = await useWizard.getState().requestUserInput({
3515
+ const answer = await store.requestUserInput({
3507
3516
  prompt: "Run this command?",
3508
3517
  promptType: "commandApproval",
3509
3518
  options: [],
@@ -3512,20 +3521,159 @@ function storeApproval(root) {
3512
3521
  cwd: rel === "" || rel.startsWith("..") ? req.cwd : rel
3513
3522
  }
3514
3523
  });
3515
- return answer === "approve" ? "approve" : "reject";
3524
+ if (answer !== "approve") return "reject";
3525
+ store.approveCommand(req.command, req.cwd);
3526
+ return "approve";
3516
3527
  };
3517
3528
  }
3518
3529
  var EXPLORATORY_COMMANDS = /* @__PURE__ */ new Set(["ls", "find", "tree", "dir"]);
3530
+ function commandSegments(command) {
3531
+ return command.split(/&&|;|\|/).map((segment) => segment.trim());
3532
+ }
3519
3533
  function isExploratoryCommand(command) {
3520
- return command.split(/&&|;|\|/).map((segment) => segment.trim().split(/\s+/)[0]).some((word) => word !== void 0 && EXPLORATORY_COMMANDS.has(word));
3534
+ return commandSegments(command).map((segment) => segment.split(/\s+/)[0]).some((word) => word !== void 0 && EXPLORATORY_COMMANDS.has(word));
3535
+ }
3536
+ var READ_ONLY_BINARIES = /* @__PURE__ */ new Set([
3537
+ "cat",
3538
+ "head",
3539
+ "tail",
3540
+ "wc",
3541
+ "pwd",
3542
+ "echo",
3543
+ "date",
3544
+ "whoami",
3545
+ "hostname",
3546
+ "uname",
3547
+ "which",
3548
+ "file",
3549
+ "stat",
3550
+ "grep",
3551
+ "egrep",
3552
+ "fgrep",
3553
+ "rg",
3554
+ "diff"
3555
+ ]);
3556
+ var READ_ONLY_NO_ARGS_BINARIES = /* @__PURE__ */ new Set(["env", "printenv"]);
3557
+ var GIT_READ_ONLY_SUBCOMMANDS = /* @__PURE__ */ new Set([
3558
+ "status",
3559
+ "log",
3560
+ "diff",
3561
+ "show",
3562
+ "describe",
3563
+ "blame",
3564
+ "ls-files",
3565
+ "rev-parse",
3566
+ "cat-file",
3567
+ "shortlog",
3568
+ "ls-remote"
3569
+ ]);
3570
+ var VERSION_CHECK_BINARIES = /* @__PURE__ */ new Set([
3571
+ "node",
3572
+ "tsc",
3573
+ "npm",
3574
+ "pnpm",
3575
+ "yarn",
3576
+ "python",
3577
+ "python3",
3578
+ "ruby",
3579
+ "go",
3580
+ "cargo",
3581
+ "rustc",
3582
+ "php",
3583
+ "composer",
3584
+ "java",
3585
+ "mvn",
3586
+ "gradle",
3587
+ "bundle",
3588
+ "git"
3589
+ ]);
3590
+ var VERSION_FLAGS = /* @__PURE__ */ new Set(["--version", "-v", "-V"]);
3591
+ function hasFileRedirect(segment) {
3592
+ return segment.replace(/\d>&\d/g, "").includes(">");
3521
3593
  }
3522
- async function approveAndRun(ctx, command, cwd, explanation) {
3523
- const decision = await ctx.shell.approve({ command, cwd, explanation });
3524
- if (decision === "reject") {
3525
- ctx.shell.executions.push({ command, cwd, approved: false });
3526
- logger.info({ command }, "runShell: user rejected the command");
3527
- return "The user rejected this command. Do not retry it. Propose a different command, or report the limitation via reportStatus.";
3594
+ function hasShellInjectionRisk(segment) {
3595
+ if (segment.includes("$(") || segment.includes("<(") || segment.includes("`")) {
3596
+ return true;
3597
+ }
3598
+ return segment.replace(/\d>&\d/g, "").includes("&");
3599
+ }
3600
+ function fastPathSegmentSafety(segment) {
3601
+ if (!segment) return true;
3602
+ if (hasFileRedirect(segment)) return false;
3603
+ if (hasShellInjectionRisk(segment)) return false;
3604
+ const [cmd0, ...rest] = segment.split(/\s+/);
3605
+ if (cmd0 === void 0) return true;
3606
+ if (VERSION_CHECK_BINARIES.has(cmd0) && rest.length === 1 && VERSION_FLAGS.has(rest[0])) {
3607
+ return true;
3608
+ }
3609
+ if (READ_ONLY_BINARIES.has(cmd0)) return true;
3610
+ if (READ_ONLY_NO_ARGS_BINARIES.has(cmd0)) {
3611
+ return rest.length === 0 ? true : void 0;
3612
+ }
3613
+ if (cmd0 === "git") {
3614
+ return GIT_READ_ONLY_SUBCOMMANDS.has(rest[0] ?? "") ? true : void 0;
3615
+ }
3616
+ return void 0;
3617
+ }
3618
+ function fastPathSafety(command) {
3619
+ const results = commandSegments(command).map(fastPathSegmentSafety);
3620
+ if (results.some((r) => r === false)) return false;
3621
+ if (results.every((r) => r === true)) return true;
3622
+ return void 0;
3623
+ }
3624
+ var CLASSIFIER_MODEL = "claude-haiku-4-5";
3625
+ var commandSafetySchema = z15.object({
3626
+ safe: z15.boolean(),
3627
+ reason: z15.string().describe("One short sentence explaining the verdict.")
3628
+ });
3629
+ function defaultCreateModel() {
3630
+ const token = getAuthToken();
3631
+ if (!token) {
3632
+ throw new Error("Not authenticated: no user token available");
3633
+ }
3634
+ return createAnthropic({
3635
+ apiKey: token,
3636
+ baseURL: PROXY_BASE_URL,
3637
+ fetch: proxyFetch
3638
+ });
3639
+ }
3640
+ async function classifyCommandSafety(createModel, command, cwd, explanation) {
3641
+ try {
3642
+ const anthropic = createModel();
3643
+ const { output } = await generateText({
3644
+ model: anthropic(CLASSIFIER_MODEL),
3645
+ temperature: 0,
3646
+ output: Output.object({ schema: commandSafetySchema }),
3647
+ prompt: [
3648
+ "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.",
3649
+ "Judge it SAFE only if it cannot modify, delete, move, rename, or install anything the project cares about, cannot push/publish/deploy/commit anything, and cannot make a network request that changes remote state.",
3650
+ "Three broad categories are safe, and most commands you will see fall into one of them:",
3651
+ "(1) Reporting or listing existing state, with no side effect \u2014 e.g. `git status`, `npm ls`, `npm outdated`, `pip show`, `pip freeze`, `cargo tree`, `docker ps`, `docker images`, `kubectl get pods`, `terraform state list`. This includes a read-only GET-style query to a remote registry or API that only fetches public metadata and changes nothing remote (e.g. `npm view <package>`, `pip index versions <package>`, `curl` with no -X/--request other than GET and no -d/--data) \u2014 safe because nothing changes, not because it stays local.",
3652
+ '(2) A "preview" or "check" mode that reports what a change WOULD do, or reports a problem, without making the change. This is a general pattern, not a fixed list \u2014 ANY command that takes a dry-run/check/plan/validate/diff/list-only flag is safe when that flag is present, regardless of whether the same flag would normally appear on a "safe" kind of command: `terraform plan`, `terraform validate`, `terraform fmt -check`, `black --check`, `prettier --check`, `isort --check`, `gofmt -l` (list-only), `stylelint` with no `--fix`, and equally `git push --dry-run`, `npm publish --dry-run`, `kubectl apply --dry-run=client` \u2014 these last three are otherwise-unsafe operations (push, publish, cluster changes) that the dry-run flag turns into a report. The corresponding command WITHOUT that flag (e.g. `terraform apply`, `black .`, `gofmt -w`, `git push`) is a different, unsafe command \u2014 the flag is what makes the difference, for any tool, not just the ones named here. This cuts both ways, so do not assume a bare invocation is the safe one: `cargo fmt`, `black`, `prettier`, `isort`, `rustfmt`, and `terraform fmt` all REWRITE FILES by default and need an explicit check/diff/dry-run flag to become safe, while `gofmt`, `eslint`, `stylelint`, `rubocop`, and `ruff check` default to a safe check-only mode and need an explicit fix/write flag to become unsafe \u2014 the same-looking bare command is safe for one group and unsafe for the other, so judge each by what its own flags actually say, not by resemblance to a tool you already judged.',
3653
+ "(3) Running the project's own tests, type checker, or linter, as long as it is not passed an autofix/write/update flag (e.g. --fix, -u, --write, rubocop -a) \u2014 this holds even for a compiled language, where running tests or a linter incidentally compiles code and writes build/cache/coverage output (e.g. a target/, build/, or __pycache__ directory): CREATING that incidental output does not make the command unsafe.",
3654
+ "Deleting or removing anything is unsafe, even something in a cache or build directory (e.g. `rm -rf __pycache__`, `cargo clean`, `git clean`), and even if the command otherwise fits one of the three categories above. Installing, uninstalling, or upgrading a dependency, changing a database schema, or writing to a path outside the project is also unsafe. A command chained with && or ; is only safe if every part of the chain is safe on its own.",
3655
+ "One specific, narrow exception to all of the above: when the command literally invokes one of these runner programs BY NAME as the leading command \u2014 npx, bunx, pnpm dlx, yarn dlx, pipx run, uvx \u2014 it is unsafe regardless of what it then runs, even something that looks like a harmless test/lint/typecheck command (e.g. `npx tsc --noEmit`, `uvx ruff check .`), because that runner can fetch and execute a different, unreviewed version of a package from a registry each time. This exception is about that specific syntax, not a general doubt about whether a tool is installed: a plain `ruff check .`, `pytest`, or any other bare command name is NOT this exception merely because you cannot verify from the string alone that it is installed \u2014 judge those the same as any other already-installed project tool, per the categories above. If you are unsure, judge it unsafe.",
3656
+ `Command: ${command}`,
3657
+ `Working directory: ${cwd}`,
3658
+ `Stated purpose: ${explanation}`
3659
+ ].join("\n")
3660
+ });
3661
+ if (!output.safe) {
3662
+ logger.info(
3663
+ { command, reason: output.reason },
3664
+ "runShell: classifier judged command unsafe, requiring approval"
3665
+ );
3666
+ }
3667
+ return output.safe;
3668
+ } catch (err) {
3669
+ logger.warn(
3670
+ { err, command },
3671
+ "runShell: command safety classifier failed, requiring approval"
3672
+ );
3673
+ return false;
3528
3674
  }
3675
+ }
3676
+ async function runAndRecord(ctx, command, cwd) {
3529
3677
  useWizard.getState().pushNotice({ messages: [`Running: ${command}`] });
3530
3678
  const env = await ctx.shell.env().catch((err) => {
3531
3679
  logger.warn({ err, command }, "runShell: could not resolve command env");
@@ -3561,9 +3709,18 @@ async function approveAndRun(ctx, command, cwd, explanation) {
3561
3709
  output: run2.output
3562
3710
  };
3563
3711
  }
3564
- function runShellTool(ctx) {
3712
+ async function approveAndRun(ctx, command, cwd, explanation) {
3713
+ const decision = await ctx.shell.approve({ command, cwd, explanation });
3714
+ if (decision === "reject") {
3715
+ ctx.shell.executions.push({ command, cwd, approved: false });
3716
+ logger.info({ command }, "runShell: user rejected the command");
3717
+ return "The user rejected this command. Do not retry it. Propose a different command, or report the limitation via reportStatus.";
3718
+ }
3719
+ return runAndRecord(ctx, command, cwd);
3720
+ }
3721
+ function runShellTool(ctx, createModel = defaultCreateModel) {
3565
3722
  return tool8({
3566
- description: "Run a shell command in the project. Use this for anything the project needs done in its own ecosystem: installing dependencies, running a script you wrote, running the project's lint/typecheck/test commands. To inspect the project, use listFiles or searchFiles instead of ls/find \u2014 this tool refuses those. The user sees and approves every command before it runs, so write a clear `explanation`. If the user rejects a command, do not retry it \u2014 propose a different approach.",
3723
+ description: "Run a shell command in the project. Use this for anything the project needs done in its own ecosystem: installing dependencies, running a script you wrote, running the project's lint/typecheck/test commands. Do not use this to find or read files \u2014 use listFiles, searchFiles, and readFile instead of ls/find/cat/head/tail/grep/rg. This tool refuses ls/find/tree/dir outright; a read command like cat is not refused (some read-only commands run without approval, see below), but it's still the wrong tool for reading a file \u2014 the dedicated tools exist for that and won't count against this tool's command budget. The user approves any command that could change the project before it runs, so write a clear `explanation`. A command judged read-only (inspection, or running tests/typecheck/lint without an autofix flag, in any language) runs immediately without approval. If the user rejects a command, do not retry it \u2014 propose a different approach.",
3567
3724
  inputSchema: z15.object({
3568
3725
  command: z15.string().describe(
3569
3726
  "The command to run, exactly as it would be typed in a shell. Pipes, && and redirects are allowed."
@@ -3582,9 +3739,27 @@ function runShellTool(ctx) {
3582
3739
  const resolved2 = resolveInRoot(ctx, cwd ?? ".");
3583
3740
  if (!resolved2.ok) return resolved2.error;
3584
3741
  if (isExploratoryCommand(command)) {
3585
- return "Refused: use listFiles or searchFiles to inspect the project instead of ls/find/tree.";
3742
+ return "Refused: use listFiles or searchFiles to find files, and readFile to read one, instead of ls/find/tree/dir.";
3586
3743
  }
3587
3744
  logger.info({ command, cwd: resolved2.target }, "called runShell tool");
3745
+ const fast = fastPathSafety(command);
3746
+ let isSafe = fast;
3747
+ if (isSafe === void 0) {
3748
+ isSafe = useWizard.getState().isCommandApproved(command, resolved2.target);
3749
+ if (!isSafe) {
3750
+ isSafe = await classifyCommandSafety(
3751
+ createModel,
3752
+ command,
3753
+ resolved2.target,
3754
+ explanation
3755
+ );
3756
+ }
3757
+ }
3758
+ if (isSafe) {
3759
+ return serializePrompt(
3760
+ () => runAndRecord(ctx, command, resolved2.target)
3761
+ );
3762
+ }
3588
3763
  return serializePrompt(
3589
3764
  () => approveAndRun(ctx, command, resolved2.target, explanation)
3590
3765
  );
@@ -3634,8 +3809,8 @@ function reviewScriptTool(ctx) {
3634
3809
  }
3635
3810
 
3636
3811
  // src/lib/tools/generateRecord.ts
3637
- import { tool as tool10, generateText, Output, NoObjectGeneratedError } from "ai";
3638
- import { createAnthropic } from "@ai-sdk/anthropic";
3812
+ import { tool as tool10, generateText as generateText2, Output as Output2, NoObjectGeneratedError } from "ai";
3813
+ import { createAnthropic as createAnthropic2 } from "@ai-sdk/anthropic";
3639
3814
  import { nanoid as nanoid2 } from "nanoid";
3640
3815
  import { mkdir as mkdir5, writeFile as writeFile6 } from "node:fs/promises";
3641
3816
  import { dirname as dirname6 } from "node:path";
@@ -3645,18 +3820,18 @@ var RECORD_MODEL = "claude-haiku-4-5";
3645
3820
  var MAX_RECORDS = 100;
3646
3821
  var BATCH_SIZE = 10;
3647
3822
  var MAX_BATCH_ATTEMPTS = 3;
3648
- function defaultCreateModel() {
3823
+ function defaultCreateModel2() {
3649
3824
  const token = getAuthToken();
3650
3825
  if (!token) {
3651
3826
  throw new Error("Not authenticated: no user token available");
3652
3827
  }
3653
- return createAnthropic({
3828
+ return createAnthropic2({
3654
3829
  apiKey: token,
3655
3830
  baseURL: PROXY_BASE_URL,
3656
3831
  fetch: proxyFetch
3657
3832
  });
3658
3833
  }
3659
- function generateRecordTool(ctx, createModel = defaultCreateModel) {
3834
+ function generateRecordTool(ctx, createModel = defaultCreateModel2) {
3660
3835
  return tool10({
3661
3836
  description: "Generate realistic sample records for an entity and write them to a JSON file. Provide the entity name and its attributes; this tool asks a model to invent varied, realistic values, each with a unique objectID, and returns the file path to read them from at runtime. Do not invent the record values or objectIDs yourself, and do not inline the returned records into the script \u2014 call this tool and read the file it writes.",
3662
3837
  inputSchema: z17.object({
@@ -3677,9 +3852,9 @@ function generateRecordTool(ctx, createModel = defaultCreateModel) {
3677
3852
  let lastError;
3678
3853
  for (let attempt = 1; attempt <= MAX_BATCH_ATTEMPTS; attempt++) {
3679
3854
  try {
3680
- const { output } = await generateText({
3855
+ const { output } = await generateText2({
3681
3856
  model: anthropic(RECORD_MODEL),
3682
- output: Output.object({
3857
+ output: Output2.object({
3683
3858
  schema: z17.object({
3684
3859
  records: z17.array(recordSchema).length(batchCount)
3685
3860
  })
@@ -3844,7 +4019,7 @@ async function runAgentAttempt(req, attempt) {
3844
4019
  if (!token) {
3845
4020
  throw new Error("Not authenticated: no user token available");
3846
4021
  }
3847
- const anthropic = createAnthropic2({
4022
+ const anthropic = createAnthropic3({
3848
4023
  apiKey: token,
3849
4024
  baseURL: PROXY_BASE_URL,
3850
4025
  fetch: proxyFetch
@@ -3881,7 +4056,7 @@ async function runAgentAttempt(req, attempt) {
3881
4056
  }
3882
4057
  };
3883
4058
  }),
3884
- output: Output2.object({ schema: req.outputSchema }),
4059
+ output: Output3.object({ schema: req.outputSchema }),
3885
4060
  tools: createTools(toolContext, {
3886
4061
  output: req.outputSchema,
3887
4062
  tools: req.tools
@@ -4064,7 +4239,7 @@ async function runAnalysis(mode, extraInstructions = []) {
4064
4239
  // package.json
4065
4240
  var package_default = {
4066
4241
  name: "@algolia/wizard",
4067
- version: "0.31.0",
4242
+ version: "0.33.0-rc.126.237",
4068
4243
  description: "Magically implement Algolia functionality in your codebase",
4069
4244
  type: "module",
4070
4245
  engines: {
@@ -4785,8 +4960,9 @@ function searchInstructions(input) {
4785
4960
  ] : [
4786
4961
  "No bundled Algolia SDK reference exists for this stack, so rely on the project's own conventions and Algolia's official client for its language. Do not invent APIs \u2014 keep to the documented search endpoint and its parameters."
4787
4962
  ],
4788
- `Add the search UI at ${input.searchLocation ? `"${input.searchLocation}"` : "the best shared, always-rendered layout location (e.g. a header/nav component)"} so it is reachable across the app \u2014 at least a working search input and results list against the target index.`,
4789
- "If a search box already exists, replace it with yours.",
4963
+ `Create the search experience as its own component in a new file, following the project's existing component conventions (location, naming, styling approach). Do not write it inline into an existing file.`,
4964
+ `Import and render that new component from ${input.searchLocation ? `"${input.searchLocation}"` : "the best shared, always-rendered layout location (e.g. a header/nav component)"} so it is reachable across the app \u2014 at least a working search input and results list against the target index.`,
4965
+ "If a search box already exists, replace its usage with an import and render of your new component; remove the old implementation.",
4790
4966
  `Read the index name from the ${publicIndexNameVar(input.publicEnvVarPrefix)} env var, which the wizard sets to "${input.targetIndex}". Never hardcode an index name or derive one from the project, file, or component name.`,
4791
4967
  "Read the App ID, the search-only API key, and the index name from env vars; never hardcode them. A search-only key is safe to expose client-side.",
4792
4968
  // The key is provisioned only after verification passes, and the wizard
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@algolia/wizard",
3
- "version": "0.31.0",
3
+ "version": "0.33.0-rc.126.237",
4
4
  "description": "Magically implement Algolia functionality in your codebase",
5
5
  "type": "module",
6
6
  "engines": {