agent-first-data 0.3.0 → 0.4.1

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/README.md CHANGED
@@ -63,7 +63,7 @@ Plain: args.input_path=/data/backup.tar.gz code=log event=startup config.max_fil
63
63
 
64
64
  ## API Reference
65
65
 
66
- Total: **8 public APIs** + **AFDATA logging** (3 protocol builders + 3 output functions + 1 internal + 1 utility)
66
+ Total: **12 public APIs and 1 type** + **AFDATA logging** (3 protocol builders + 3 output functions + 1 internal + 1 utility + 4 CLI helpers + `OutputFormat`)
67
67
 
68
68
  ### Protocol Builders (returns JsonValue)
69
69
 
@@ -177,6 +177,42 @@ parseSize("1.5K"); // 1536
177
177
  parseSize("512"); // 512
178
178
  ```
179
179
 
180
+ ### CLI Helpers (for tools built on AFDATA)
181
+
182
+ Shared helpers that prevent flag-parsing drift between CLI tools. Use these instead of reimplementing `--output` and `--log` handling in each tool.
183
+
184
+ ```typescript
185
+ type OutputFormat = "json" | "yaml" | "plain"
186
+
187
+ cliParseOutput(s: string): OutputFormat // Parse --output flag; throws on unknown
188
+ cliParseLogFilters(entries: string[]): string[] // Normalize --log: trim, lowercase, dedup, remove empty
189
+ cliOutput(value: JsonValue, format: OutputFormat): string // Dispatch to outputJson/Yaml/Plain
190
+ buildCliError(message: string): JsonValue // {code:"error", error_code:"invalid_request", retryable:false, trace:{duration_ms:0}}
191
+ ```
192
+
193
+ **Canonical pattern** — parse all flags before doing work, emit JSONL errors to stdout:
194
+
195
+ ```typescript
196
+ import {
197
+ type OutputFormat, cliParseOutput, cliParseLogFilters,
198
+ cliOutput, buildCliError, outputJson,
199
+ } from "agent-first-data";
200
+
201
+ let fmt: OutputFormat;
202
+ try {
203
+ fmt = cliParseOutput(outputArg);
204
+ } catch (e) {
205
+ console.log(outputJson(buildCliError((e as Error).message)));
206
+ process.exit(2);
207
+ }
208
+
209
+ const log = cliParseLogFilters(logArg ? logArg.split(",") : []);
210
+ // ... do work ...
211
+ console.log(cliOutput(result, fmt));
212
+ ```
213
+
214
+ See `examples/agent_cli.ts` for the complete working example (`npx tsx --test examples/agent_cli.ts`).
215
+
180
216
  ## Usage Examples
181
217
 
182
218
  ### Example 1: REST API
@@ -265,6 +301,7 @@ const result = buildJsonOk(
265
301
  );
266
302
 
267
303
  // Print JSONL to stdout (secrets redacted, one JSON object per line)
304
+ // Channel policy: machine-readable protocol/log events must not use stderr.
268
305
  console.log(outputJson(result));
269
306
  // {"code":"ok","result":{"status":"success"},"trace":{"api_key_secret":"***","duration_ms":250}}
270
307
  ```
package/dist/cli.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ /**
2
+ * AFDATA CLI helpers — output format parsing, log filter normalization, error building.
3
+ */
4
+ import { JsonValue } from "./format.js";
5
+ /** Output format for CLI and pipe/MCP modes. */
6
+ export type OutputFormat = "json" | "yaml" | "plain";
7
+ /**
8
+ * Parse the --output flag value into an OutputFormat.
9
+ * Throws on unknown values; catch and pass message to buildCliError.
10
+ *
11
+ * @example
12
+ * cliParseOutput("json") // → "json"
13
+ * cliParseOutput("xml") // throws Error
14
+ */
15
+ export declare function cliParseOutput(s: string): OutputFormat;
16
+ /**
17
+ * Normalize --output flag entries: trim, lowercase, deduplicate, remove empty.
18
+ * Accepts pre-split entries (e.g. after splitting on comma).
19
+ *
20
+ * @example
21
+ * cliParseLogFilters(["Query", " error ", "query"]) // → ["query", "error"]
22
+ */
23
+ export declare function cliParseLogFilters(entries: string[]): string[];
24
+ /**
25
+ * Dispatch output formatting by OutputFormat.
26
+ * Equivalent to calling outputJson, outputYaml, or outputPlain directly.
27
+ *
28
+ * @example
29
+ * cliOutput({ code: "ok" }, "plain") // → "code=ok"
30
+ */
31
+ export declare function cliOutput(value: JsonValue, format: OutputFormat): string;
32
+ /**
33
+ * Build a standard CLI parse error value.
34
+ * Use when flag parsing fails or a flag value is invalid.
35
+ * Print with outputJson and exit with code 2.
36
+ *
37
+ * @example
38
+ * const err = buildCliError("--output: invalid value 'xml'");
39
+ * console.log(outputJson(err));
40
+ * process.exit(2);
41
+ */
42
+ export declare function buildCliError(message: string): JsonValue;
package/dist/cli.js ADDED
@@ -0,0 +1,68 @@
1
+ /**
2
+ * AFDATA CLI helpers — output format parsing, log filter normalization, error building.
3
+ */
4
+ import { outputJson, outputYaml, outputPlain } from "./format.js";
5
+ /**
6
+ * Parse the --output flag value into an OutputFormat.
7
+ * Throws on unknown values; catch and pass message to buildCliError.
8
+ *
9
+ * @example
10
+ * cliParseOutput("json") // → "json"
11
+ * cliParseOutput("xml") // throws Error
12
+ */
13
+ export function cliParseOutput(s) {
14
+ if (s === "json" || s === "yaml" || s === "plain") {
15
+ return s;
16
+ }
17
+ throw new Error(`invalid --output format '${s}': expected json, yaml, or plain`);
18
+ }
19
+ /**
20
+ * Normalize --output flag entries: trim, lowercase, deduplicate, remove empty.
21
+ * Accepts pre-split entries (e.g. after splitting on comma).
22
+ *
23
+ * @example
24
+ * cliParseLogFilters(["Query", " error ", "query"]) // → ["query", "error"]
25
+ */
26
+ export function cliParseLogFilters(entries) {
27
+ const out = [];
28
+ for (const entry of entries) {
29
+ const s = entry.trim().toLowerCase();
30
+ if (s && !out.includes(s)) {
31
+ out.push(s);
32
+ }
33
+ }
34
+ return out;
35
+ }
36
+ /**
37
+ * Dispatch output formatting by OutputFormat.
38
+ * Equivalent to calling outputJson, outputYaml, or outputPlain directly.
39
+ *
40
+ * @example
41
+ * cliOutput({ code: "ok" }, "plain") // → "code=ok"
42
+ */
43
+ export function cliOutput(value, format) {
44
+ if (format === "yaml")
45
+ return outputYaml(value);
46
+ if (format === "plain")
47
+ return outputPlain(value);
48
+ return outputJson(value);
49
+ }
50
+ /**
51
+ * Build a standard CLI parse error value.
52
+ * Use when flag parsing fails or a flag value is invalid.
53
+ * Print with outputJson and exit with code 2.
54
+ *
55
+ * @example
56
+ * const err = buildCliError("--output: invalid value 'xml'");
57
+ * console.log(outputJson(err));
58
+ * process.exit(2);
59
+ */
60
+ export function buildCliError(message) {
61
+ return {
62
+ code: "error",
63
+ error_code: "invalid_request",
64
+ error: message,
65
+ retryable: false,
66
+ trace: { duration_ms: 0 },
67
+ };
68
+ }
package/dist/index.d.ts CHANGED
@@ -3,3 +3,4 @@
3
3
  */
4
4
  export { buildJsonOk, buildJsonError, buildJson, outputJson, outputYaml, outputPlain, internalRedactSecrets, parseSize } from "./format.js";
5
5
  export { log, span, initJson, initPlain, initYaml } from "./afdata_logging.js";
6
+ export { type OutputFormat, cliParseOutput, cliParseLogFilters, cliOutput, buildCliError } from "./cli.js";
package/dist/index.js CHANGED
@@ -3,3 +3,4 @@
3
3
  */
4
4
  export { buildJsonOk, buildJsonError, buildJson, outputJson, outputYaml, outputPlain, internalRedactSecrets, parseSize } from "./format.js";
5
5
  export { log, span, initJson, initPlain, initYaml } from "./afdata_logging.js";
6
+ export { cliParseOutput, cliParseLogFilters, cliOutput, buildCliError } from "./cli.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-first-data",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "Agent-First Data (AFDATA) — suffix-driven output formatting and protocol templates for AI agents",
5
5
  "license": "MIT",
6
6
  "repository": {