@theokit/sdk-tools 0.4.0 → 0.6.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.
package/dist/index.d.cts CHANGED
@@ -199,6 +199,15 @@ declare class SsrfBlockedError extends ConfigurationError {
199
199
  readonly name = "SsrfBlockedError";
200
200
  constructor(host: string, detail?: string);
201
201
  }
202
+ /**
203
+ * Thrown when a request is refused by the REDIRECT policy (the `maxRedirects` hop limit was
204
+ * exceeded) — a distinct event from {@link SsrfBlockedError} (a blocked host). With `maxRedirects: 0`
205
+ * this fires on the first 3xx (block-all-redirects). Carries `code: "redirect_blocked"`.
206
+ */
207
+ declare class RedirectBlockedError extends ConfigurationError {
208
+ readonly name = "RedirectBlockedError";
209
+ constructor(url: string, detail?: string);
210
+ }
202
211
  /**
203
212
  * True if `ip` is a blocked address (private/loopback/link-local/CGNAT/metadata/
204
213
  * reserved). A non-IP literal returns `true` (fail closed — callers resolve names
@@ -361,6 +370,14 @@ declare function injectGuidance(handlerOutput: string, guidance: ToolGuidanceMap
361
370
  declare function withToolResultGuidance(tool: CustomTool, guidance: ToolGuidanceMap): CustomTool;
362
371
  /** `withToolResultGuidance` pre-bound to {@link DEFAULT_TOOL_GUIDANCE}. */
363
372
  declare function withDefaultGuidance(tool: CustomTool): CustomTool;
373
+ /**
374
+ * Wrap `shell_exec` so a SOFT failure — `{ ok: true, exit_code != 0 }` (the TOOL ran, the COMMAND
375
+ * failed) — gains a `guidance` hint. This is the one case {@link injectGuidance} does not cover (it
376
+ * is `ok:false`-only by design). ADDITIVE, IDEMPOTENT, NEVER-THROW; a no-op for any other tool, for
377
+ * `exit_code 0`, and for non-JSON output. Compose AFTER {@link withDefaultGuidance} (the two domains —
378
+ * `ok:false` vs `ok:true` soft-fail — are disjoint, so there is no double-injection).
379
+ */
380
+ declare function withShellExitGuidance(tool: CustomTool): CustomTool;
364
381
 
365
382
  /**
366
383
  * `list_dir` — built-in tool for coding agents.
@@ -699,7 +716,7 @@ declare function truncateOutput(output: string, opts?: TruncationOptions): Trunc
699
716
  * Return shape (always a JSON string):
700
717
  * - `{ ok: true, content, status_code, content_type }`
701
718
  * - `{ ok: false, error: 'invalid_url' | 'fetch_failed' |
702
- * 'timeout' | 'too_large' }`
719
+ * 'timeout' | 'too_large' | 'ssrf_blocked' | 'redirect_blocked' }`
703
720
  */
704
721
 
705
722
  interface CreateWebFetchToolOptions {
@@ -711,6 +728,16 @@ interface CreateWebFetchToolOptions {
711
728
  * trusted local-dev tooling.
712
729
  */
713
730
  allowPrivateHosts?: boolean;
731
+ /**
732
+ * Max redirect hops to follow (each SSRF-screened). Default 5. Set `0` to BLOCK ALL
733
+ * redirects — a 3xx then returns `{ ok: false, error: "redirect_blocked" }` (a strict
734
+ * no-redirect policy for untrusted, model-chosen URLs).
735
+ */
736
+ maxRedirects?: number;
737
+ /** Fetch implementation (injectable for tests — drive paths with no real network). */
738
+ fetchImpl?: ScreenedFetchOptions["fetchImpl"];
739
+ /** DNS resolver (injectable for tests — drive the SSRF path with no real DNS). */
740
+ lookup?: ScreenedFetchOptions["lookup"];
714
741
  }
715
742
  declare function createWebFetchTool(opts?: CreateWebFetchToolOptions): CustomTool;
716
743
 
@@ -785,4 +812,4 @@ interface CreateWriteFileToolOptions {
785
812
  }
786
813
  declare function createWriteFileTool(opts: CreateWriteFileToolOptions): CustomTool;
787
814
 
788
- export { CatastrophicCommandError, type CommandPolicy, type CreateApplyPatchToolOptions, type CreateBraveWebSearchAdapterOptions, type CreateEditFileToolOptions, type CreateGitDiffToolOptions, type CreateGlobToolOptions, type CreateListDirToolOptions, type CreateReadFileToolOptions, type CreateRunVitestToolOptions, type CreateSearchTextToolOptions, type CreateShellToolOptions, type CreateWebFetchToolOptions, type CreateWebSearchToolOptions, type CreateWriteFileToolOptions, DEFAULT_TOOL_GUIDANCE, type PlanModeTool, type PlanModeToolOptions, type PlanModeToolWithStore, type PlanNode, type QuestionTool, type QuestionToolOptions, type RepoMapOptions, type ResolveAndScreenOptions, type ScreenedFetchOptions, type SessionArtifactStore, type SessionArtifactStoreOptions, SsrfBlockedError, type TodoItem, type TodolistTool, type ToolGuidanceMap, type TruncationOptions, type TruncationResult, type VitestSummary, type WebSearchCallback, type WebSearchResult, buildEnvContext, buildRepoMap, catastrophicShellReason, commandDenialReason, createApplyPatchTool, createBraveWebSearchAdapter, createEditFileTool, createGitDiffTool, createGlobTool, createListDirTool, createPlanModeTool, createQuestionTool, createReadFileTool, createRunVitestTool, createSearchTextTool, createSessionArtifactStore, createShellTool, createTodolistTool, createWebFetchTool, createWebSearchTool, createWriteFileTool, denyCatastrophicCommands, formatCode, formatDiff, formatError, formatFileList, injectGuidance, isBlockedIp, isCommandAllowed, renderToolList, resolveAndScreen, screenedFetch, todoItemsToPlanNodes, truncateOutput, withDefaultGuidance, withDescription, withToolResultGuidance };
815
+ export { CatastrophicCommandError, type CommandPolicy, type CreateApplyPatchToolOptions, type CreateBraveWebSearchAdapterOptions, type CreateEditFileToolOptions, type CreateGitDiffToolOptions, type CreateGlobToolOptions, type CreateListDirToolOptions, type CreateReadFileToolOptions, type CreateRunVitestToolOptions, type CreateSearchTextToolOptions, type CreateShellToolOptions, type CreateWebFetchToolOptions, type CreateWebSearchToolOptions, type CreateWriteFileToolOptions, DEFAULT_TOOL_GUIDANCE, type PlanModeTool, type PlanModeToolOptions, type PlanModeToolWithStore, type PlanNode, type QuestionTool, type QuestionToolOptions, RedirectBlockedError, type RepoMapOptions, type ResolveAndScreenOptions, type ScreenedFetchOptions, type SessionArtifactStore, type SessionArtifactStoreOptions, SsrfBlockedError, type TodoItem, type TodolistTool, type ToolGuidanceMap, type TruncationOptions, type TruncationResult, type VitestSummary, type WebSearchCallback, type WebSearchResult, buildEnvContext, buildRepoMap, catastrophicShellReason, commandDenialReason, createApplyPatchTool, createBraveWebSearchAdapter, createEditFileTool, createGitDiffTool, createGlobTool, createListDirTool, createPlanModeTool, createQuestionTool, createReadFileTool, createRunVitestTool, createSearchTextTool, createSessionArtifactStore, createShellTool, createTodolistTool, createWebFetchTool, createWebSearchTool, createWriteFileTool, denyCatastrophicCommands, formatCode, formatDiff, formatError, formatFileList, injectGuidance, isBlockedIp, isCommandAllowed, renderToolList, resolveAndScreen, screenedFetch, todoItemsToPlanNodes, truncateOutput, withDefaultGuidance, withDescription, withShellExitGuidance, withToolResultGuidance };
package/dist/index.d.ts CHANGED
@@ -199,6 +199,15 @@ declare class SsrfBlockedError extends ConfigurationError {
199
199
  readonly name = "SsrfBlockedError";
200
200
  constructor(host: string, detail?: string);
201
201
  }
202
+ /**
203
+ * Thrown when a request is refused by the REDIRECT policy (the `maxRedirects` hop limit was
204
+ * exceeded) — a distinct event from {@link SsrfBlockedError} (a blocked host). With `maxRedirects: 0`
205
+ * this fires on the first 3xx (block-all-redirects). Carries `code: "redirect_blocked"`.
206
+ */
207
+ declare class RedirectBlockedError extends ConfigurationError {
208
+ readonly name = "RedirectBlockedError";
209
+ constructor(url: string, detail?: string);
210
+ }
202
211
  /**
203
212
  * True if `ip` is a blocked address (private/loopback/link-local/CGNAT/metadata/
204
213
  * reserved). A non-IP literal returns `true` (fail closed — callers resolve names
@@ -361,6 +370,14 @@ declare function injectGuidance(handlerOutput: string, guidance: ToolGuidanceMap
361
370
  declare function withToolResultGuidance(tool: CustomTool, guidance: ToolGuidanceMap): CustomTool;
362
371
  /** `withToolResultGuidance` pre-bound to {@link DEFAULT_TOOL_GUIDANCE}. */
363
372
  declare function withDefaultGuidance(tool: CustomTool): CustomTool;
373
+ /**
374
+ * Wrap `shell_exec` so a SOFT failure — `{ ok: true, exit_code != 0 }` (the TOOL ran, the COMMAND
375
+ * failed) — gains a `guidance` hint. This is the one case {@link injectGuidance} does not cover (it
376
+ * is `ok:false`-only by design). ADDITIVE, IDEMPOTENT, NEVER-THROW; a no-op for any other tool, for
377
+ * `exit_code 0`, and for non-JSON output. Compose AFTER {@link withDefaultGuidance} (the two domains —
378
+ * `ok:false` vs `ok:true` soft-fail — are disjoint, so there is no double-injection).
379
+ */
380
+ declare function withShellExitGuidance(tool: CustomTool): CustomTool;
364
381
 
365
382
  /**
366
383
  * `list_dir` — built-in tool for coding agents.
@@ -699,7 +716,7 @@ declare function truncateOutput(output: string, opts?: TruncationOptions): Trunc
699
716
  * Return shape (always a JSON string):
700
717
  * - `{ ok: true, content, status_code, content_type }`
701
718
  * - `{ ok: false, error: 'invalid_url' | 'fetch_failed' |
702
- * 'timeout' | 'too_large' }`
719
+ * 'timeout' | 'too_large' | 'ssrf_blocked' | 'redirect_blocked' }`
703
720
  */
704
721
 
705
722
  interface CreateWebFetchToolOptions {
@@ -711,6 +728,16 @@ interface CreateWebFetchToolOptions {
711
728
  * trusted local-dev tooling.
712
729
  */
713
730
  allowPrivateHosts?: boolean;
731
+ /**
732
+ * Max redirect hops to follow (each SSRF-screened). Default 5. Set `0` to BLOCK ALL
733
+ * redirects — a 3xx then returns `{ ok: false, error: "redirect_blocked" }` (a strict
734
+ * no-redirect policy for untrusted, model-chosen URLs).
735
+ */
736
+ maxRedirects?: number;
737
+ /** Fetch implementation (injectable for tests — drive paths with no real network). */
738
+ fetchImpl?: ScreenedFetchOptions["fetchImpl"];
739
+ /** DNS resolver (injectable for tests — drive the SSRF path with no real DNS). */
740
+ lookup?: ScreenedFetchOptions["lookup"];
714
741
  }
715
742
  declare function createWebFetchTool(opts?: CreateWebFetchToolOptions): CustomTool;
716
743
 
@@ -785,4 +812,4 @@ interface CreateWriteFileToolOptions {
785
812
  }
786
813
  declare function createWriteFileTool(opts: CreateWriteFileToolOptions): CustomTool;
787
814
 
788
- export { CatastrophicCommandError, type CommandPolicy, type CreateApplyPatchToolOptions, type CreateBraveWebSearchAdapterOptions, type CreateEditFileToolOptions, type CreateGitDiffToolOptions, type CreateGlobToolOptions, type CreateListDirToolOptions, type CreateReadFileToolOptions, type CreateRunVitestToolOptions, type CreateSearchTextToolOptions, type CreateShellToolOptions, type CreateWebFetchToolOptions, type CreateWebSearchToolOptions, type CreateWriteFileToolOptions, DEFAULT_TOOL_GUIDANCE, type PlanModeTool, type PlanModeToolOptions, type PlanModeToolWithStore, type PlanNode, type QuestionTool, type QuestionToolOptions, type RepoMapOptions, type ResolveAndScreenOptions, type ScreenedFetchOptions, type SessionArtifactStore, type SessionArtifactStoreOptions, SsrfBlockedError, type TodoItem, type TodolistTool, type ToolGuidanceMap, type TruncationOptions, type TruncationResult, type VitestSummary, type WebSearchCallback, type WebSearchResult, buildEnvContext, buildRepoMap, catastrophicShellReason, commandDenialReason, createApplyPatchTool, createBraveWebSearchAdapter, createEditFileTool, createGitDiffTool, createGlobTool, createListDirTool, createPlanModeTool, createQuestionTool, createReadFileTool, createRunVitestTool, createSearchTextTool, createSessionArtifactStore, createShellTool, createTodolistTool, createWebFetchTool, createWebSearchTool, createWriteFileTool, denyCatastrophicCommands, formatCode, formatDiff, formatError, formatFileList, injectGuidance, isBlockedIp, isCommandAllowed, renderToolList, resolveAndScreen, screenedFetch, todoItemsToPlanNodes, truncateOutput, withDefaultGuidance, withDescription, withToolResultGuidance };
815
+ export { CatastrophicCommandError, type CommandPolicy, type CreateApplyPatchToolOptions, type CreateBraveWebSearchAdapterOptions, type CreateEditFileToolOptions, type CreateGitDiffToolOptions, type CreateGlobToolOptions, type CreateListDirToolOptions, type CreateReadFileToolOptions, type CreateRunVitestToolOptions, type CreateSearchTextToolOptions, type CreateShellToolOptions, type CreateWebFetchToolOptions, type CreateWebSearchToolOptions, type CreateWriteFileToolOptions, DEFAULT_TOOL_GUIDANCE, type PlanModeTool, type PlanModeToolOptions, type PlanModeToolWithStore, type PlanNode, type QuestionTool, type QuestionToolOptions, RedirectBlockedError, type RepoMapOptions, type ResolveAndScreenOptions, type ScreenedFetchOptions, type SessionArtifactStore, type SessionArtifactStoreOptions, SsrfBlockedError, type TodoItem, type TodolistTool, type ToolGuidanceMap, type TruncationOptions, type TruncationResult, type VitestSummary, type WebSearchCallback, type WebSearchResult, buildEnvContext, buildRepoMap, catastrophicShellReason, commandDenialReason, createApplyPatchTool, createBraveWebSearchAdapter, createEditFileTool, createGitDiffTool, createGlobTool, createListDirTool, createPlanModeTool, createQuestionTool, createReadFileTool, createRunVitestTool, createSearchTextTool, createSessionArtifactStore, createShellTool, createTodolistTool, createWebFetchTool, createWebSearchTool, createWriteFileTool, denyCatastrophicCommands, formatCode, formatDiff, formatError, formatFileList, injectGuidance, isBlockedIp, isCommandAllowed, renderToolList, resolveAndScreen, screenedFetch, todoItemsToPlanNodes, truncateOutput, withDefaultGuidance, withDescription, withShellExitGuidance, withToolResultGuidance };
package/dist/index.js CHANGED
@@ -719,6 +719,15 @@ var SsrfBlockedError = class extends ConfigurationError {
719
719
  );
720
720
  }
721
721
  };
722
+ var RedirectBlockedError = class extends ConfigurationError {
723
+ name = "RedirectBlockedError";
724
+ constructor(url, detail) {
725
+ super(
726
+ `Blocked redirect for "${url}"${detail ? ` (${detail})` : ""}: redirect-hop limit exceeded (redirect policy).`,
727
+ { code: "redirect_blocked" }
728
+ );
729
+ }
730
+ };
722
731
  function v4ToInt(ip) {
723
732
  const parts = ip.split(".");
724
733
  return (Number(parts[0]) << 24 | Number(parts[1]) << 16 | Number(parts[2]) << 8 | Number(parts[3])) >>> 0;
@@ -842,7 +851,7 @@ async function screenedFetch(url, options = {}) {
842
851
  if (next === void 0) return res;
843
852
  current = next;
844
853
  }
845
- throw new SsrfBlockedError(url, "too many redirects");
854
+ throw new RedirectBlockedError(url, "too many redirects");
846
855
  }
847
856
  var DEFAULT_BUDGET = 8e3;
848
857
  var DEFAULT_MAX_DEPTH = 4;
@@ -1027,6 +1036,28 @@ function withToolResultGuidance(tool, guidance) {
1027
1036
  function withDefaultGuidance(tool) {
1028
1037
  return withToolResultGuidance(tool, DEFAULT_TOOL_GUIDANCE);
1029
1038
  }
1039
+ function withShellExitGuidance(tool) {
1040
+ if (tool.name !== "shell_exec") return tool;
1041
+ return {
1042
+ name: tool.name,
1043
+ description: tool.description,
1044
+ inputSchema: tool.inputSchema,
1045
+ handler: async (input) => {
1046
+ const out = await tool.handler(input);
1047
+ let parsed;
1048
+ try {
1049
+ parsed = JSON.parse(out);
1050
+ } catch {
1051
+ return out;
1052
+ }
1053
+ if (isRecord(parsed) && parsed.ok === true && typeof parsed.exit_code === "number" && parsed.exit_code !== 0 && !("guidance" in parsed)) {
1054
+ const guidance = `The command exited ${parsed.exit_code}. Read the stderr above, fix the cause, then retry.`;
1055
+ return JSON.stringify({ ...parsed, guidance });
1056
+ }
1057
+ return out;
1058
+ }
1059
+ };
1060
+ }
1030
1061
  var DEFAULT_MAX_ENTRIES = 500;
1031
1062
  function createListDirTool(opts) {
1032
1063
  const { projectRoot, max = DEFAULT_MAX_ENTRIES } = opts;
@@ -1745,6 +1776,9 @@ var MAX_BODY_BYTES = 1 * 1024 * 1024;
1745
1776
  function createWebFetchTool(opts) {
1746
1777
  const defaultTimeoutMs = opts?.defaultTimeoutMs ?? DEFAULT_TIMEOUT_MS4;
1747
1778
  const allowPrivateHosts = opts?.allowPrivateHosts ?? false;
1779
+ const maxRedirects = opts?.maxRedirects;
1780
+ const fetchImpl = opts?.fetchImpl;
1781
+ const lookup = opts?.lookup;
1748
1782
  return defineTool({
1749
1783
  name: "web_fetch",
1750
1784
  description: "Fetch the contents of a URL via HTTP/HTTPS. Use only for URLs the user provided or that you are confident help with the task; never invent or guess URLs. Rejects non-http(s) URLs and is SSRF-guarded by default (private/loopback/link-local/cloud-metadata hosts are refused with an ssrf_blocked error). The response body is capped at 1 MB. Returns { ok, content, status_code, content_type } or { ok: false, error }.",
@@ -1774,7 +1808,10 @@ function createWebFetchTool(opts) {
1774
1808
  try {
1775
1809
  const response = await screenedFetch(url, {
1776
1810
  signal: controller.signal,
1777
- allowPrivateHosts
1811
+ allowPrivateHosts,
1812
+ ...maxRedirects !== void 0 ? { maxRedirects } : {},
1813
+ ...fetchImpl ? { fetchImpl } : {},
1814
+ ...lookup ? { lookup } : {}
1778
1815
  });
1779
1816
  clearTimeout(timer);
1780
1817
  const contentLength = response.headers.get("content-length");
@@ -1807,6 +1844,9 @@ function createWebFetchTool(opts) {
1807
1844
  });
1808
1845
  } catch (err) {
1809
1846
  clearTimeout(timer);
1847
+ if (err instanceof RedirectBlockedError) {
1848
+ return JSON.stringify({ ok: false, error: "redirect_blocked", url, reason: err.message });
1849
+ }
1810
1850
  if (err instanceof SsrfBlockedError) {
1811
1851
  return JSON.stringify({ ok: false, error: "ssrf_blocked", url, reason: err.message });
1812
1852
  }
@@ -1937,6 +1977,6 @@ async function isBinaryFile(absolutePath) {
1937
1977
  }
1938
1978
  }
1939
1979
 
1940
- export { CatastrophicCommandError, DEFAULT_TOOL_GUIDANCE, SsrfBlockedError, buildEnvContext, buildRepoMap, catastrophicShellReason, commandDenialReason, createApplyPatchTool, createBraveWebSearchAdapter, createEditFileTool, createGitDiffTool, createGlobTool, createListDirTool, createPlanModeTool, createQuestionTool, createReadFileTool, createRunVitestTool, createSearchTextTool, createSessionArtifactStore, createShellTool, createTodolistTool, createWebFetchTool, createWebSearchTool, createWriteFileTool, denyCatastrophicCommands, formatCode, formatDiff, formatError, formatFileList, injectGuidance, isBlockedIp, isCommandAllowed, renderToolList, resolveAndScreen, screenedFetch, todoItemsToPlanNodes, truncateOutput, withDefaultGuidance, withDescription, withToolResultGuidance };
1980
+ export { CatastrophicCommandError, DEFAULT_TOOL_GUIDANCE, RedirectBlockedError, SsrfBlockedError, buildEnvContext, buildRepoMap, catastrophicShellReason, commandDenialReason, createApplyPatchTool, createBraveWebSearchAdapter, createEditFileTool, createGitDiffTool, createGlobTool, createListDirTool, createPlanModeTool, createQuestionTool, createReadFileTool, createRunVitestTool, createSearchTextTool, createSessionArtifactStore, createShellTool, createTodolistTool, createWebFetchTool, createWebSearchTool, createWriteFileTool, denyCatastrophicCommands, formatCode, formatDiff, formatError, formatFileList, injectGuidance, isBlockedIp, isCommandAllowed, renderToolList, resolveAndScreen, screenedFetch, todoItemsToPlanNodes, truncateOutput, withDefaultGuidance, withDescription, withShellExitGuidance, withToolResultGuidance };
1941
1981
  //# sourceMappingURL=index.js.map
1942
1982
  //# sourceMappingURL=index.js.map