{"version":3,"sources":["../src/cli/commands/governance/ingestion-templates.ts"],"sourcesContent":["import chalk from \"chalk\";\n\nimport {\n  adminListIngestionTemplates,\n  archiveIngestionTemplate,\n  cloneIngestionTemplateFromPlatform,\n  createIngestionTemplate,\n  getIngestionTemplate,\n  GovernanceCliError,\n  updateIngestionTemplateOttlRules,\n} from \"@/cli/utils/governance/cli-api\";\nimport { isLoggedIn, loadConfig } from \"@/cli/utils/governance/config\";\n\nimport type { IngestionTemplateRow } from \"@/cli/utils/governance/cli-api\";\n\n/**\n * `langwatch governance ingestion-templates <verb>`\n *\n * Mirrors the Hono routes at /api/governance/ingestion-templates\n * (Sergey 0bb951160). Every mutating call carries\n * `X-LangWatch-Surface: cli` so audit rows land with\n * `metadata.surface = 'cli'`.\n *\n * Verbs: list / admin-list / get / create / update-ottl-rules /\n * archive / clone-from-platform.\n */\n\nfunction requireLogin() {\n  const cfg = loadConfig();\n  if (!isLoggedIn(cfg)) {\n    process.stderr.write(\n      \"Not logged in. Run `langwatch login --device` first.\\n\",\n    );\n    process.exit(1);\n  }\n  return cfg;\n}\n\nfunction handleError(err: unknown): never {\n  const msg = err instanceof GovernanceCliError ? err.message : String(err);\n  process.stderr.write(`Error: ${msg}\\n`);\n  process.exit(1);\n}\n\nfunction printTemplate(t: IngestionTemplateRow): void {\n  console.log(\n    `${chalk.bold(t.display_name)}  ${chalk.gray(`(${t.slug})`)}\\n` +\n      `  id:                 ${t.id}\\n` +\n      `  source_type:        ${t.source_type}\\n` +\n      `  scope:              ${\n        t.platform_published ? chalk.cyan(\"Platform\") : chalk.magenta(\"Org-authored\")\n      }\\n` +\n      `  enabled:            ${t.enabled ? chalk.green(\"yes\") : chalk.gray(\"no\")}\\n` +\n      `  credential_schema:  ${t.credential_schema ?? chalk.gray(\"(none)\")}\\n`,\n  );\n}\n\nfunction printTemplateList(rows: IngestionTemplateRow[]): void {\n  if (rows.length === 0) {\n    console.log(chalk.gray(\"No ingestion templates found.\"));\n    return;\n  }\n  for (const t of rows) {\n    console.log(\n      `${t.platform_published ? chalk.cyan(\"◆\") : chalk.magenta(\"◇\")} ${chalk.bold(\n        t.display_name,\n      )}  ${chalk.gray(t.slug)}  ${chalk.gray(t.source_type)}`,\n    );\n  }\n}\n\nexport async function adminListCommand(options: {\n  json?: boolean;\n}): Promise<void> {\n  const cfg = requireLogin();\n  let rows: IngestionTemplateRow[];\n  try {\n    rows = await adminListIngestionTemplates(cfg);\n  } catch (err) {\n    handleError(err);\n  }\n  if (options.json) {\n    console.log(JSON.stringify(rows, null, 2));\n    return;\n  }\n  printTemplateList(rows);\n}\n\nexport async function getCommand(\n  id: string,\n  options: { json?: boolean },\n): Promise<void> {\n  const cfg = requireLogin();\n  let row: IngestionTemplateRow;\n  try {\n    row = await getIngestionTemplate(cfg, id);\n  } catch (err) {\n    handleError(err);\n  }\n  if (options.json) {\n    console.log(JSON.stringify(row, null, 2));\n    return;\n  }\n  printTemplate(row);\n}\n\nexport async function createCommand(options: {\n  sourceType: string;\n  displayName: string;\n  description?: string;\n  iconAsset?: string;\n  credentialSchema?: string;\n  ottlRules?: string;\n  json?: boolean;\n}): Promise<void> {\n  const cfg = requireLogin();\n  if (\n    options.credentialSchema &&\n    ![\"otlp_token\", \"static_api_key\", \"agent_id\"].includes(\n      options.credentialSchema,\n    )\n  ) {\n    process.stderr.write(\n      `Error: --credential-schema must be one of: otlp_token, static_api_key, agent_id\\n`,\n    );\n    process.exit(1);\n  }\n  let row: IngestionTemplateRow;\n  try {\n    row = await createIngestionTemplate(cfg, {\n      source_type: options.sourceType,\n      display_name: options.displayName,\n      description: options.description,\n      icon_asset: options.iconAsset,\n      credential_schema: (options.credentialSchema ?? null) as\n        | \"otlp_token\"\n        | \"static_api_key\"\n        | \"agent_id\"\n        | null,\n      ottl_rules: options.ottlRules,\n    });\n  } catch (err) {\n    handleError(err);\n  }\n  if (options.json) {\n    console.log(JSON.stringify(row, null, 2));\n    return;\n  }\n  console.log(chalk.green(\"✓ Template created\"));\n  printTemplate(row);\n}\n\nexport async function updateOttlRulesCommand(\n  id: string,\n  options: { ottlRules: string; json?: boolean },\n): Promise<void> {\n  const cfg = requireLogin();\n  let row: IngestionTemplateRow;\n  try {\n    row = await updateIngestionTemplateOttlRules(cfg, id, options.ottlRules);\n  } catch (err) {\n    handleError(err);\n  }\n  if (options.json) {\n    console.log(JSON.stringify(row, null, 2));\n    return;\n  }\n  console.log(chalk.green(\"✓ OTTL rules updated\"));\n}\n\nexport async function archiveCommand(\n  id: string,\n  options: { json?: boolean },\n): Promise<void> {\n  const cfg = requireLogin();\n  try {\n    await archiveIngestionTemplate(cfg, id);\n  } catch (err) {\n    handleError(err);\n  }\n  if (options.json) {\n    console.log(JSON.stringify({ ok: true }, null, 2));\n    return;\n  }\n  console.log(chalk.green(`✓ Template ${id} archived`));\n}\n\nexport async function cloneFromPlatformCommand(\n  sourceTemplateId: string,\n  options: { json?: boolean },\n): Promise<void> {\n  const cfg = requireLogin();\n  let row: IngestionTemplateRow;\n  try {\n    row = await cloneIngestionTemplateFromPlatform(cfg, sourceTemplateId);\n  } catch (err) {\n    handleError(err);\n  }\n  if (options.json) {\n    console.log(JSON.stringify(row, null, 2));\n    return;\n  }\n  console.log(chalk.green(\"✓ Template cloned\"));\n  printTemplate(row);\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA,OAAO,WAAW;AA2BlB,SAAS,eAAe;AACtB,QAAM,MAAM,WAAW;AACvB,MAAI,CAAC,WAAW,GAAG,GAAG;AACpB,YAAQ,OAAO;AAAA,MACb;AAAA,IACF;AACA,YAAQ,KAAK,CAAC;AAAA,EAChB;AACA,SAAO;AACT;AAEA,SAAS,YAAY,KAAqB;AACxC,QAAM,MAAM,eAAe,qBAAqB,IAAI,UAAU,OAAO,GAAG;AACxE,UAAQ,OAAO,MAAM,UAAU,GAAG;AAAA,CAAI;AACtC,UAAQ,KAAK,CAAC;AAChB;AAEA,SAAS,cAAc,GAA+B;AA5CtD;AA6CE,UAAQ;AAAA,IACN,GAA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