AI AgentStructured Tool Call

Table Markdown AI Integration Guide

Convert CSV, Excel-like rows, and Markdown tables in AI workflows with a stable tabular contract.

1. Function Schema

Import directly into Dify, Coze, Cursor, or any tool-calling agent runtime.

Function Schema
{
  "name": "table_markdown_convert",
  "description": "Convert CSV-like rows to Markdown tables or Markdown tables back to CSV.",
  "parameters": {
    "type": "object",
    "properties": {
      "mode": {
        "type": "string",
        "enum": ["to_markdown", "to_csv"]
      },
      "input": {
        "type": "string",
        "description": "CSV text or Markdown table text."
      }
    },
    "required": ["mode", "input"]
  }
}

2. System Prompt / SOP

Paste this into your system prompt to keep the generated tool implementation aligned with the contract.

System Prompt SOP
# Task Constraints: Table Markdown Converter

When converting tabular text, you MUST preserve row and column alignment.

## Core Prohibitions
- NEVER guess missing delimiters and fabricate cells without saying so.
- NEVER drop empty cells at the end of rows.
- NEVER reorder columns during conversion.

## Standard Implementation
1. Parse the input into a 2D table representation.
2. For to_markdown, render a Markdown header row, separator row, and body rows.
3. For to_csv, serialize every row back into comma-separated text with proper escaping.
4. Return a validation error if parsing fails.

## Guardrails
- Keep line order stable.
- Preserve blank values as empty cells.
- If the input contains inconsistent row widths, either normalize explicitly or fail with a clear error.

3. Core Script

This keeps the AI contract intentionally text-only. File upload parsing can stay in the UI while the agent tool handles deterministic table-string conversion.

Core Script
function main(args) {
  const { mode, input } = args;

  if (!input) {
    return { success: false, result: "", error: "input is required" };
  }

  try {
    if (mode === "to_markdown") {
      return { success: true, result: csvToMarkdown(input) };
    }

    if (mode === "to_csv") {
      return { success: true, result: markdownToCsv(input) };
    }

    return { success: false, result: "", error: "Unknown mode" };
  } catch (error) {
    return {
      success: false,
      result: "",
      error: "Conversion failed: " + (error instanceof Error ? error.message : String(error)),
    };
  }
}