Implements a comprehensive suite of AI SDK v6 tools across multiple categories: - Research (5): page-brief, compare-pages, source-credibility, claim-checklist, timeline-from-text - Web (10): fetch-text, links-catalog, extract-meta, extract-json-ld, redirect-trace, sitemap-read, rss-read, table-extract, robots-policy, url-normalize - Data (15): csv-parse, csv-stringify, json-repair, json-schema-validate, yaml-parse, yaml-stringify, text-chunk, normalize-whitespace, dedupe-by-key, pivot, rows-filter, rows-sort, rows-group-aggregate, rows-join, schema-infer - Doc (12): toc-generate, glossary-build, faq-from-text, executive-brief, decision-record-adr, prd-outline, acceptance-criteria, style-rewrite - Eng (12): diff-text-unified, env-var-docs-generate, dependency-audit-lite, conventional-commit-suggest, markdown-lint-basic, test-case-generate, stacktrace-parse, release-notes, changelog-entry, release-checklist - Security (7): redact-secrets, secret-scan-text, url-risk-heuristic, csp-compose, hardening-checklist-web, access-control-matrix, data-classification-heuristic - Stats (9): effect-size-suite, bootstrap-ci, permutation-test, multiple-testing-adjust, linear-regression-ols, logistic-regression, time-series-decompose-lite, anomaly-detect-mad - Ops (7): slo-draft, runbook-draft, postmortem-draft, postmortem-action-extractor, error-log-triage, coverage-tracker, monitoring-gap-analysis - Agent (15): prompt-to-workflow-skeleton, workflow-validate-io, workflow-explain, workflow-cost-estimate, tool-call-accuracy-score, eval-fixture-build, guardrail-policy-draft, workflow-auto-repair, tool-selection-plan, novelty-score-workflow, workflow-variant-generate, config-normalize, recipe-* - Utility (8): base64-encode, base64-decode, hash-text, regex-extract, template-render, date-parse, json-path-query, url-parse - HTML (3): html-sanitize, html-to-markdown, markdown-to-html All tools follow AI SDK v6 pattern with tool() and jsonSchema<T>(). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
3.7 KiB
3.7 KiB
@tpmjs/tools-pivot
Pivot array data from row format to column format (row-to-column transformation).
Installation
npm install @tpmjs/tools-pivot
Usage
import { pivotTool } from '@tpmjs/tools-pivot';
// Sample data: sales by region and quarter
const salesData = [
{ region: 'North', quarter: 'Q1', sales: 1000 },
{ region: 'North', quarter: 'Q2', sales: 1200 },
{ region: 'South', quarter: 'Q1', sales: 800 },
{ region: 'South', quarter: 'Q2', sales: 900 },
{ region: 'East', quarter: 'Q1', sales: 1100 },
{ region: 'East', quarter: 'Q2', sales: 1300 },
];
// Pivot the data
const result = await pivotTool.execute({
rows: salesData,
rowKey: 'region', // Rows will be regions
columnKey: 'quarter', // Columns will be quarters
valueKey: 'sales', // Cell values will be sales numbers
});
console.log(result.pivoted);
// [
// { region: 'East', Q1: 1100, Q2: 1300 },
// { region: 'North', Q1: 1000, Q2: 1200 },
// { region: 'South', Q1: 800, Q2: 900 },
// ]
console.log(result.columns);
// ['Q1', 'Q2']
console.log(result.metadata);
// {
// uniqueRows: 3,
// uniqueColumns: 2,
// totalCells: 6,
// nullCells: 0
// }
Features
- Row-to-column transformation: Converts long-format data to wide-format
- Automatic column detection: Discovers unique column values from data
- Null handling: Tracks and reports missing values
- Sorted output: Rows and columns are sorted alphabetically
- Metadata: Provides statistics about the pivot operation
Parameters
rows(array, required): Array of objects to pivotrowKey(string, required): Field name to use as row identifierscolumnKey(string, required): Field name whose values become column namesvalueKey(string, required): Field name whose values populate the cells
Returns
{
pivoted: Array<Record<string, unknown>>;
columns: string[];
rowCount: number;
metadata: {
uniqueRows: number;
uniqueColumns: number;
totalCells: number;
nullCells: number;
};
}
Use Cases
- Converting long-format data to wide-format for reporting
- Creating pivot tables from raw data
- Reshaping time-series data for visualization
- Preparing data for cross-tabulation analysis
- Transforming database query results for display
Example: Time Series Data
const temperatureData = [
{ city: 'NYC', month: 'Jan', temp: 32 },
{ city: 'NYC', month: 'Feb', temp: 35 },
{ city: 'LA', month: 'Jan', temp: 65 },
{ city: 'LA', month: 'Feb', temp: 67 },
];
const result = await pivotTool.execute({
rows: temperatureData,
rowKey: 'city',
columnKey: 'month',
valueKey: 'temp',
});
// Result:
// [
// { city: 'LA', Jan: 65, Feb: 67 },
// { city: 'NYC', Jan: 32, Feb: 35 },
// ]
Example with AI Agent
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
import { pivotTool } from '@tpmjs/tools-pivot';
const result = await generateText({
model: openai('gpt-4'),
tools: {
pivot: pivotTool,
},
prompt: 'Pivot this sales data by product and month: ...',
});
Handling Missing Values
When a row/column combination doesn't exist in the input data, the pivot tool sets the cell value to null:
const sparseData = [
{ product: 'A', region: 'North', sales: 100 },
{ product: 'B', region: 'South', sales: 200 },
// Note: Product A has no South sales, Product B has no North sales
];
const result = await pivotTool.execute({
rows: sparseData,
rowKey: 'product',
columnKey: 'region',
valueKey: 'sales',
});
// Result:
// [
// { product: 'A', North: 100, South: null },
// { product: 'B', North: null, South: 200 },
// ]
console.log(result.metadata.nullCells); // 2
License
MIT