@oneie/cli 0.3.0 → 0.3.2

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.
@@ -2,3 +2,5 @@ import { Command } from 'commander';
2
2
  /** `one broadcast` — operate newsletters from the terminal.
3
3
  * Two verbs: list + send. Both hit broadcast:* receivers via ask(); no new route. */
4
4
  export declare function broadcastCmd(): Command;
5
+ /** `one newsletter` — pull/push newsletter block trees from the terminal. */
6
+ export declare function newsletterCmd(): Command;
@@ -43,3 +43,114 @@ export function broadcastCmd() {
43
43
  });
44
44
  return cmd;
45
45
  }
46
+ // ---------------------------------------------------------------------------
47
+ // Inline block serialization — mirrors serialize.ts; no Zod dep (CLI rule)
48
+ // ---------------------------------------------------------------------------
49
+ function serializeBlocksMd(blocks) {
50
+ return blocks.map(({ kind, ...rest }) => '```block ' + kind + '\n' + JSON.stringify(rest) + '\n```').join('\n\n');
51
+ }
52
+ function parseBlocksMd(md) {
53
+ const blocks = [];
54
+ const fence = /^```block ([a-z]+)\n([\s\S]*?)\n```$/gm;
55
+ let m;
56
+ while ((m = fence.exec(md)) !== null) {
57
+ const lineNo = md.slice(0, m.index).split('\n').length;
58
+ try {
59
+ const body = JSON.parse(m[2]);
60
+ blocks.push({ kind: m[1], ...body });
61
+ }
62
+ catch (e) {
63
+ throw new Error(`parseBlocks: invalid JSON at line ${lineNo}: ${e instanceof Error ? e.message : e}`);
64
+ }
65
+ }
66
+ return blocks;
67
+ }
68
+ function diffBlocks(before, after) {
69
+ const diffs = [];
70
+ const beforeById = new Map(before.map(b => [b.id, b]));
71
+ const afterById = new Map(after.map(b => [b.id, b]));
72
+ for (const [id, b] of beforeById) {
73
+ if (!afterById.has(id)) {
74
+ diffs.push({ op: '-', kind: b.kind, id });
75
+ }
76
+ }
77
+ for (const [id, a] of afterById) {
78
+ if (!beforeById.has(id)) {
79
+ diffs.push({ op: '+', kind: a.kind, id });
80
+ }
81
+ else {
82
+ const b = beforeById.get(id);
83
+ if (JSON.stringify(b) !== JSON.stringify(a)) {
84
+ diffs.push({ op: '~', kind: a.kind, id });
85
+ }
86
+ }
87
+ }
88
+ return diffs;
89
+ }
90
+ /** `one newsletter` — pull/push newsletter block trees from the terminal. */
91
+ export function newsletterCmd() {
92
+ const cmd = new Command('newsletter').description('Pull and push newsletter component trees');
93
+ cmd
94
+ .command('pull <broadcastId>')
95
+ .description('Fetch a broadcast and print its blocks as markdown')
96
+ .option('--api <url>', 'API base URL', DEFAULT_API)
97
+ .action(async (broadcastId, opts) => {
98
+ const res = await ask(opts.api, 'broadcast:get', { broadcastId });
99
+ const b = res.broadcast;
100
+ if (!b) {
101
+ process.stderr.write(`broadcast:get returned no broadcast\n`);
102
+ process.exit(1);
103
+ }
104
+ if (b.components && Array.isArray(b.components) && b.components.length > 0) {
105
+ process.stdout.write(serializeBlocksMd(b.components) + '\n');
106
+ }
107
+ else if (b.body_md) {
108
+ process.stdout.write(b.body_md + '\n');
109
+ }
110
+ else {
111
+ process.stdout.write('');
112
+ }
113
+ });
114
+ cmd
115
+ .command('push <broadcastId> <file>')
116
+ .description('Parse a markdown file and update broadcast components')
117
+ .option('--api <url>', 'API base URL', DEFAULT_API)
118
+ .option('--test', 'Dry-run: mark update as test_send')
119
+ .action(async (broadcastId, file, opts) => {
120
+ const { readFileSync } = await import('node:fs');
121
+ let md;
122
+ try {
123
+ md = readFileSync(file, 'utf8');
124
+ }
125
+ catch (e) {
126
+ process.stderr.write(`Cannot read ${file}: ${e instanceof Error ? e.message : e}\n`);
127
+ process.exit(1);
128
+ }
129
+ let components;
130
+ try {
131
+ components = parseBlocksMd(md);
132
+ }
133
+ catch (e) {
134
+ process.stderr.write(`${e instanceof Error ? e.message : e}\n`);
135
+ process.exit(1);
136
+ }
137
+ // Fetch current state for diff display
138
+ const current = await ask(opts.api, 'broadcast:get', { broadcastId });
139
+ const prev = (current.broadcast?.components ?? []);
140
+ const version = current.broadcast?.version;
141
+ const diffs = diffBlocks(prev, components);
142
+ for (const d of diffs) {
143
+ process.stdout.write(`${d.op} ${d.kind} (${d.id})\n`);
144
+ }
145
+ if (diffs.length === 0) {
146
+ process.stdout.write('(no changes)\n');
147
+ }
148
+ const payload = { broadcastId, components };
149
+ if (version !== undefined)
150
+ payload['version'] = version;
151
+ if (opts.test)
152
+ payload['test_send'] = true;
153
+ out(await ask(opts.api, 'broadcast:update', payload));
154
+ });
155
+ return cmd;
156
+ }
package/dist/src/index.js CHANGED
@@ -7,7 +7,8 @@ import { devCmd } from './dev.js';
7
7
  import { groupCmd } from './group.js';
8
8
  import { socialCmd } from './social.js';
9
9
  import { connectCmd } from './connect.js';
10
- import { broadcastCmd } from './broadcast.js';
10
+ import { broadcastCmd, newsletterCmd } from './broadcast.js';
11
+ import { staffCmd } from './staff.js';
11
12
  import { substrateCommands } from './substrate.js';
12
13
  import { readFileSync } from 'node:fs';
13
14
  import { fileURLToPath } from 'node:url';
@@ -27,6 +28,8 @@ program.addCommand(groupCmd());
27
28
  program.addCommand(socialCmd());
28
29
  program.addCommand(connectCmd());
29
30
  program.addCommand(broadcastCmd());
31
+ program.addCommand(newsletterCmd());
32
+ program.addCommand(staffCmd());
30
33
  substrateCommands(program);
31
34
  program.parseAsync(process.argv).catch((err) => {
32
35
  const isJson = process.argv.includes('--json');
@@ -0,0 +1,2 @@
1
+ import { Command } from 'commander';
2
+ export declare function staffCmd(): Command;
@@ -0,0 +1,73 @@
1
+ import { Command } from 'commander';
2
+ import { existsSync, readFileSync } from 'node:fs';
3
+ import { homedir } from 'node:os';
4
+ import { resolve } from 'node:path';
5
+ const DEFAULT_API = process.env.ONEIE_BASE_URL ?? process.env.ONE_BASE_URL ?? 'https://one.ie';
6
+ const KEY_FILE = resolve(homedir(), '.config', 'oneie', 'key');
7
+ const VALID_ROLES = ['owner', 'admin', 'member', 'viewer'];
8
+ function readApiKey() {
9
+ if (process.env.ONEIE_API_KEY)
10
+ return process.env.ONEIE_API_KEY;
11
+ if (existsSync(KEY_FILE))
12
+ return readFileSync(KEY_FILE, 'utf8').trim() || null;
13
+ return null;
14
+ }
15
+ function out(data) {
16
+ if (data.ok) {
17
+ const pairs = Object.entries(data)
18
+ .filter(([k]) => k !== 'ok')
19
+ .map(([k, v]) => `${k}=${JSON.stringify(v)}`);
20
+ process.stdout.write(`ok ${pairs.join(' ')}\n`);
21
+ }
22
+ else {
23
+ process.stderr.write(`error ${data.error ?? 'unknown'}\n`);
24
+ process.exit(1);
25
+ }
26
+ }
27
+ export function staffCmd() {
28
+ const cmd = new Command('staff').description('Workspace staff management');
29
+ cmd
30
+ .command('invite <email>')
31
+ .description('Invite a human to your workspace by role')
32
+ .option('--role <role>', `Role to assign: ${VALID_ROLES.join('|')}`, 'admin')
33
+ .option('--workspace <slug>', 'Workspace slug (defaults to authenticated workspace)')
34
+ .option('--dry-run', 'Print plan without sending invite')
35
+ .option('--api <url>', 'API base URL', DEFAULT_API)
36
+ .action(async (email, opts) => {
37
+ const role = opts.role;
38
+ if (!VALID_ROLES.includes(role)) {
39
+ out({ ok: false, error: `--role must be one of: ${VALID_ROLES.join(', ')}` });
40
+ return;
41
+ }
42
+ if (opts.dryRun) {
43
+ out({ ok: true, dry_run: true, email, role, workspace: opts.workspace ?? '(authenticated)' });
44
+ return;
45
+ }
46
+ const key = readApiKey();
47
+ if (!key) {
48
+ out({ ok: false, error: 'no API key — set ONEIE_API_KEY or run: oneie auth login' });
49
+ return;
50
+ }
51
+ const base = opts.api.replace(/\/$/, '');
52
+ const body = { email, role };
53
+ if (opts.workspace)
54
+ body.workspace = opts.workspace;
55
+ const res = await fetch(`${base}/api/ask/world:invite-member`, {
56
+ method: 'POST',
57
+ headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${key}` },
58
+ body: JSON.stringify({ data: body }),
59
+ });
60
+ if (!res.ok) {
61
+ const text = await res.text().catch(() => '');
62
+ out({ ok: false, error: `${res.status}: ${text}` });
63
+ return;
64
+ }
65
+ const json = (await res.json());
66
+ if (json.error) {
67
+ out({ ok: false, error: json.error });
68
+ return;
69
+ }
70
+ out({ ok: true, email, role, invite_url: json.inviteUrl ?? json.token ?? '(sent)' });
71
+ });
72
+ return cmd;
73
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oneie/cli",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "ONE agent CLI",
5
5
  "type": "module",
6
6
  "bin": {
@@ -12,7 +12,7 @@
12
12
  "README.md"
13
13
  ],
14
14
  "scripts": {
15
- "build": "tsc",
15
+ "build": "tsc && chmod +x dist/src/index.js",
16
16
  "dev": "tsx src/index.ts",
17
17
  "typecheck": "tsc --noEmit",
18
18
  "prepublishOnly": "bun run build"