Files
inkreach-teable-cli/src/lib/ctx.ts
T
yeuimu 5fa843df05 feat: add inkreach CLI + inkreach-teable-ops skill
A self-built CLI and companion agent skill for operating a deployed
inkreach-teable instance — configuring tables, fields, records, views, and
the project's OWN automation engine.

Why not the official 'teable' CLI:
  inkreach-teable's automation is self-built with a different contract:
  - conditions (not filter), fieldIds (not watchFieldIds)
  - recordCondition (not recordMatchesConditions)
  - webhook route /api/webhook/{baseId}/{wfId} (not the /base//workflow/ form)
  - scheduledTime backend honors only timing.type + timing.interval
  The official CLI targets the upstream contract and 404s/misconfigures here.
  Table/field/record/view APIs ARE upstream-compatible, but automation is not.

inkreach CLI (packages/inkreach-cli/):
  - Node.js + commander, zero extra runtime deps
  - Uses node:http (NOT fetch — Node 24 fetch drops Authorization header)
  - Commands: auth (login/status/token), base/table/field/record/view,
    workflow (list/get/create/update/delete/activate/deactivate/test),
    trigger set (8 types, builds correct config keys), action add/update/delete
    (6 types), secret, runs (list/get/rerun), fire (button-click/webhook), sql
  - --json mode for agent parsing; GET->modify->PUT whole-graph for node edits
  - Verified end-to-end: create workflow -> set webhook trigger -> add script
    action -> activate -> fire webhook -> runs list (succeeded)

inkreach-teable-ops skill (.agents/skills/inkreach-teable-ops/):
  - SKILL.md entry: module map, routing rules, 11 key constraints (the real
    contract keys), execution rules, error recovery
  - guides/: cli-reference, automation-guide, tables-fields, instance-access,
    development (build/restart, known gotchas)
  - api-reference/: trigger-types (8 triggers, exact config), action-types
    (6 actions, sandbox globals), field-types (21 types incl. Button)
  - metadata.json + install.sh
2026-08-05 02:04:39 +08:00

93 lines
2.9 KiB
TypeScript

/**
* Shared command context: resolves config from global flags + file + env,
* builds a Client, and normalizes error handling.
*/
import { Command } from 'commander';
import { resolveConfig } from './config';
import { Client, ApiError } from './client';
import { describeError } from './request';
export interface Ctx {
client: Client;
json: boolean;
baseId?: string;
/** The running subcommand (last in the chain). Use .opts() for its options. */
command: Command;
program: Command;
}
/**
* Build a Ctx. `command` is the subcommand being executed (so .opts() returns
* its local options); `program` is the top-level program (for global options).
*/
export function makeCtx(program: Command, command: Command): Ctx {
const g = program.opts() as {
endpoint?: string;
token?: string;
baseId?: string;
json?: boolean;
};
const cfg = resolveConfig({
endpoint: g.endpoint,
token: g.token,
baseId: g.baseId,
});
return {
client: new Client(cfg),
json: !!g.json,
baseId: cfg.baseId,
command,
program,
};
}
/**
* Wrap an async command action with normalized error handling.
*
* The wrapped fn receives (ctx, positionalArgs[], options). commander passes
* positional args before the trailing Command; we detect the trailing Command
* and forward everything else as positional, plus parsed options.
*/
export function run<TArgs extends unknown[] = unknown[]>(
fn: (ctx: Ctx, args: TArgs, options: Record<string, unknown>) => Promise<void>
): (this: Command, ...cmdArgs: unknown[]) => Promise<void> {
return async function (this: Command, ...cmdArgs: unknown[]) {
// commander convention: last arg is the Command instance (the subcommand).
const command = cmdArgs[cmdArgs.length - 1] as Command;
const program = command?.parent ? findRoot(command) : command;
const positional = cmdArgs.slice(0, -1) as TArgs;
const options = (command?.opts() || {}) as Record<string, unknown>;
const ctx = makeCtx(program, command);
try {
await fn(ctx, positional, options);
} catch (err) {
const { status, code, message } = describeError(err);
if (ctx.json) {
process.stdout.write(JSON.stringify({ error: { status, code, message } }) + '\n');
} else {
process.stderr.write(`✗ ${code}${status ? ` (${status})` : ''}: ${message}\n`);
}
process.exitCode = 1;
}
};
}
function findRoot(cmd: Command): Command {
let c = cmd;
while (c.parent) c = c.parent;
return c;
}
/** Require a baseId: from flag, else ctx default, else throw with guidance. */
export function requireBaseId(ctx: Ctx, flagVal?: string): string {
const baseId = flagVal || ctx.baseId;
if (!baseId) {
throw new ApiError(
0,
'MISSING_BASE_ID',
'No base id. Pass --base-id <id>, set a default with `inkreach auth login --base-id <id>`, or export INKREACH_BASE_ID.'
);
}
return baseId;
}