Skip to content

[new tool] @agent-tools/source-map — Source map consumption, generation, and multi-step remapping #211

@burner-agent

Description

@burner-agent

Tool Name

@agent-tools/source-map

Description

Consume, generate, and compose source maps for mapping compiled/minified code back to original sources. Supports multi-step remapping through chained transformations.

Why It's Useful for Agents

AI agents debugging production errors, analyzing bundled code, or performing code transformations need to map minified stack traces and positions back to original source locations. When agents run build pipelines (transpile → minify → bundle), each step produces a source map — remapping composes the chain into a single map pointing at original code. The ecosystem is fragmented across 6+ packages (jridgewell/sourcemaps monorepo — 77.4% TypeScript, trace-mapping 196M weekly downloads, gen-mapping 148M, source-map 285M, remapping 36M) with no unified API. This tool provides one interface for the full lifecycle: parse a source map, query original positions, generate new maps during code transforms, and remap through multi-step build chains. Source maps are now standardized as ECMA-426 with active Stage 2–3 proposals for Scopes, Range Mappings, Debug ID, and Env.

Proposed API

import { SourceMap } from "@agent-tools/source-map";

// Consume: parse and query a source map
const map = SourceMap.fromJSON(rawSourceMap);
const original = map.originalPositionFor({ line: 1, column: 453 });
// => { source: "src/app.ts", line: 27, column: 4, name: "handleClick" }

const generated = map.generatedPositionFor({
  source: "src/app.ts", line: 27, column: 4
});

// Generate: build a source map during code transformation
const generator = SourceMap.create({ file: "output.js" });
generator.addMapping({
  generated: { line: 1, column: 0 },
  original: { line: 5, column: 2 },
  source: "input.ts",
  name: "myFunc",
});
generator.setSourceContent("input.ts", originalSource);
const outputMap = generator.toJSON();

// Remap: compose chained source maps (transpile → minify → bundle)
const finalMap = SourceMap.remap(bundleMap, (sourcePath) => {
  // Return intermediate source map or null for original source
  return intermediateSourceMaps.get(sourcePath) ?? null;
});

// Stack trace resolution: map minified traces to original locations
const resolved = SourceMap.resolveStackTrace(stackString, {
  loader: (url) => fetchSourceMap(url),
});
// => Array<{ file, line, column, name, originalSource }>

// Utilities
const encoded = SourceMap.encodeVLQ(segments);  // VLQ codec
const decoded = SourceMap.decodeVLQ(mappings);
const merged = SourceMap.merge([map1, map2]);    // Merge maps

Scope

In scope:

  • Source map v3 parsing and generation (ECMA-426 compliant)
  • Original/generated position lookup with binary search
  • Multi-step source map remapping/composition
  • VLQ encoding/decoding
  • Source map merging for concatenated files
  • Stack trace resolution through source maps
  • Source content embedding and retrieval
  • Sectioned/indexed source map support

Out of scope:

  • Build tool integration (webpack/vite/esbuild plugins)
  • Source map validation/linting
  • Browser devtools integration
  • Code transformation itself (see @agent-tools/codegen)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is neededinfrastructureCI, workflows, build toolingintegrationCross-package or external integrationnew-toolProposal for a new tool packagetier:autonomyTier 3 — self-extension, shell, orchestrationtier:perceptionTier 2 — browser, DOM, web understanding

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions