@clickminded/coauthor-cli 0.0.7 → 0.0.8

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/dist/commands.js CHANGED
@@ -18,7 +18,7 @@ export async function runCommand(input, context) {
18
18
  if (scope === 'workspace')
19
19
  return runWorkspaceCommand(action, input, context.client);
20
20
  if (scope === 'chat')
21
- return runChatCommand(input, context.client);
21
+ return runChatCommand(action, input, context.client);
22
22
  if (scope === 'contract')
23
23
  return runArtifactCommand('contract', action, input, context.client);
24
24
  if (scope === 'outline')
@@ -164,19 +164,37 @@ async function runWorkspaceCommand(action, input, client) {
164
164
  const data = await client.request({ path: `projects/${encodeURIComponent(projectId)}/workspace` });
165
165
  return { ok: true, data };
166
166
  }
167
- async function runChatCommand(input, client) {
167
+ async function runChatCommand(action, input, client) {
168
+ if (action === 'show' || action === 'list') {
169
+ const projectId = requireProjectId(input);
170
+ const data = await client.request({ path: `projects/${encodeURIComponent(projectId)}/workspace` });
171
+ return {
172
+ ok: true,
173
+ data: {
174
+ project_id: projectId,
175
+ workspace_chat_id: extractNestedString(data, ['workspaceChatId']) ?? null,
176
+ messages: extractNestedValue(data, ['messages']) ?? [],
177
+ },
178
+ };
179
+ }
168
180
  const projectId = requireProjectId(input);
169
181
  const message = getString(input.values, 'message') ?? input.positionals.slice(1).join(' ');
170
182
  if (!message)
171
183
  throw new Error('Usage: coauthor chat <projectId> --message "..."');
172
184
  const body = withNamedOptions(parseJsonOption(input.values), input.values, ['stage', 'execution-token']);
185
+ const stage = getString(input.values, 'stage');
186
+ if (stage)
187
+ body.activeArtifact = stage;
173
188
  body.message = message;
174
189
  const data = await client.request({
175
190
  method: 'POST',
176
191
  path: `projects/${encodeURIComponent(projectId)}/chat`,
177
192
  body,
178
193
  });
179
- return { ok: true, data };
194
+ return {
195
+ ok: true,
196
+ data: enrichChatResult(data, projectId, stage),
197
+ };
180
198
  }
181
199
  async function runWorkflowCommand(action, input, client) {
182
200
  const projectId = requireProjectId(input);
@@ -249,7 +267,9 @@ async function runArtifactCommand(artifact, action, input, client) {
249
267
  return { ok: true, data };
250
268
  }
251
269
  if (action === 'generate' || action === 'save' || action === 'approve') {
252
- const body = withNamedOptions(parseJsonOption(input.values), input.values, ['execution-token']);
270
+ const body = action === 'approve'
271
+ ? await buildArtifactApproveBody(artifact, input, client, basePath)
272
+ : withNamedOptions(parseJsonOption(input.values), input.values, ['execution-token']);
253
273
  const path = action === 'save' ? basePath : `${basePath}/${action}`;
254
274
  const data = await client.request({ method: 'POST', path, body });
255
275
  return { ok: true, data };
@@ -268,7 +288,7 @@ async function runResearchCommand(action, input, client) {
268
288
  return { ok: true, data };
269
289
  }
270
290
  if (action === 'status') {
271
- return { ok: true, data: await client.request({ path: `${basePath}/status` }) };
291
+ return { ok: true, data: enrichResearchStatus(await client.request({ path: `${basePath}/status` })) };
272
292
  }
273
293
  if (action === 'structured') {
274
294
  return { ok: true, data: await client.request({ path: `${basePath}/structured` }) };
@@ -579,6 +599,9 @@ async function waitForWorkflowDraft(client, projectId, initialData) {
579
599
  let latestResearch = null;
580
600
  let draftGenerate = null;
581
601
  let attemptedDraftStart = false;
602
+ if (workflowIndicatesBackgroundResearch(initialData)) {
603
+ return researchRunningWorkflowResult(projectId, initialData, null, null);
604
+ }
582
605
  while (Date.now() < deadline) {
583
606
  await sleep(3000);
584
607
  latestDraft = await client.request({ path: `projects/${encodeURIComponent(projectId)}/draft/status` });
@@ -645,6 +668,93 @@ async function waitForWorkflowDraft(client, projectId, initialData) {
645
668
  },
646
669
  };
647
670
  }
671
+ async function buildArtifactApproveBody(artifact, input, client, basePath) {
672
+ const explicitBody = parseJsonOption(input.values);
673
+ const body = withNamedOptions(explicitBody, input.values, ['execution-token']);
674
+ if (getString(input.values, 'data') !== undefined)
675
+ return body;
676
+ const current = await client.request({ path: basePath });
677
+ const payloadHash = extractNestedString(current, ['metadata', 'payload_hash']);
678
+ if (payloadHash)
679
+ body.expectedPayloadHash = payloadHash;
680
+ if (artifact === 'contract') {
681
+ const contract = extractNestedValue(current, ['artifact']);
682
+ if (contract && typeof contract === 'object' && !Array.isArray(contract)) {
683
+ body.contract = contract;
684
+ }
685
+ return body;
686
+ }
687
+ if (artifact === 'outline') {
688
+ const sections = extractNestedValue(current, ['artifact']);
689
+ if (!Array.isArray(sections) || sections.length === 0) {
690
+ throw new Error('Generate or save an outline before approving it.');
691
+ }
692
+ body.sections = sections;
693
+ return body;
694
+ }
695
+ const researchTasks = extractNestedValue(current, ['artifact']);
696
+ if (!Array.isArray(researchTasks) || researchTasks.length === 0) {
697
+ throw new Error('Generate or save a research plan before approving it.');
698
+ }
699
+ body.researchTasks = researchTasks;
700
+ return body;
701
+ }
702
+ function enrichChatResult(data, projectId, stage) {
703
+ if (!data || typeof data !== 'object' || Array.isArray(data))
704
+ return data;
705
+ const result = {
706
+ ...data,
707
+ transcript_command: `coauthor chat show ${projectId} --json`,
708
+ };
709
+ if (stage?.trim().toLowerCase() === 'draft') {
710
+ result.draft_saved = false;
711
+ result.draft_note = 'Draft-stage chat responses are saved to the workspace chat transcript, not to the draft document. Use transcript_command to retrieve the response.';
712
+ }
713
+ return result;
714
+ }
715
+ function enrichResearchStatus(data) {
716
+ if (!data || typeof data !== 'object' || Array.isArray(data))
717
+ return data;
718
+ const runStatus = extractNestedString(data, ['run', 'status']);
719
+ const projectState = getResponseString(data, 'project_state');
720
+ if (runStatus !== 'running' || projectState === 'research_running')
721
+ return data;
722
+ return {
723
+ ...data,
724
+ diagnostic: {
725
+ code: 'research_run_state_mismatch',
726
+ message: `Latest research run is still running while project_state is "${projectState ?? 'unknown'}".`,
727
+ recovery_commands: [
728
+ 'coauthor research status <project-id> --json',
729
+ 'coauthor workflow resume <project-id> --target research --wait --json',
730
+ 'coauthor workflow rewind <project-id> --target research --json',
731
+ ],
732
+ },
733
+ };
734
+ }
735
+ function workflowIndicatesBackgroundResearch(data) {
736
+ if (!data || typeof data !== 'object' || Array.isArray(data))
737
+ return false;
738
+ return getResponseString(data, 'project_state') === 'research_running'
739
+ || getResponseString(data, 'status') === 'research_running';
740
+ }
741
+ function researchRunningWorkflowResult(projectId, workflow, researchStatus, draftStatus) {
742
+ return {
743
+ ok: true,
744
+ status: 'research_running',
745
+ message: 'Research is running in the background. Draft generation has not started yet.',
746
+ data: {
747
+ workflow,
748
+ research_status: researchStatus,
749
+ draft_status: draftStatus,
750
+ next_commands: [
751
+ `coauthor research status ${projectId} --json`,
752
+ `coauthor workflow resume ${projectId} --target draft --wait --json`,
753
+ `coauthor workflow rewind ${projectId} --target research --json`,
754
+ ],
755
+ },
756
+ };
757
+ }
648
758
  function requiresKeyPauseResult(projectId, target, message, data) {
649
759
  const resumeCommand = `coauthor workflow resume ${projectId} --target ${target} --wait --json`;
650
760
  return {
@@ -658,13 +768,17 @@ function requiresKeyPauseResult(projectId, target, message, data) {
658
768
  };
659
769
  }
660
770
  function extractNestedString(value, path) {
771
+ const current = extractNestedValue(value, path);
772
+ return typeof current === 'string' ? current : undefined;
773
+ }
774
+ function extractNestedValue(value, path) {
661
775
  let current = value;
662
776
  for (const key of path) {
663
777
  if (!current || typeof current !== 'object' || Array.isArray(current))
664
778
  return undefined;
665
779
  current = current[key];
666
780
  }
667
- return typeof current === 'string' ? current : undefined;
781
+ return current;
668
782
  }
669
783
  function normalizeWorkflowWaitTarget(value) {
670
784
  if (!value)
@@ -718,7 +832,7 @@ export function helpResult() {
718
832
  ' auth login|status|logout',
719
833
  ' project create|list|show',
720
834
  ' workspace show',
721
- ' chat',
835
+ ' chat [show|list]',
722
836
  ' contract show|save|approve',
723
837
  ' outline show|generate|save|approve',
724
838
  ' research-plan show|generate|save|approve',
package/dist/index.js CHANGED
@@ -30,21 +30,86 @@ export async function main(argv = process.argv.slice(2)) {
30
30
  }
31
31
  catch (error) {
32
32
  const wantsJson = argv.includes('--json');
33
- const formatted = formatCliError(error, resolvedBaseUrl);
33
+ const formatted = formatCliError(error, resolvedBaseUrl, argv);
34
34
  process.stderr.write(renderError(formatted.result ?? formatted.error, wantsJson));
35
35
  return formatted.exitCode;
36
36
  }
37
37
  }
38
- function formatCliError(error, baseUrl) {
38
+ function formatCliError(error, baseUrl, argv = []) {
39
39
  if (error instanceof AgentHttpError) {
40
40
  const requiresKey = formatRequiresKeyResult(error.responseBody, baseUrl);
41
41
  if (requiresKey)
42
42
  return { error: new Error(requiresKey.message), result: requiresKey, exitCode: 2 };
43
43
  const message = extractErrorMessage(error.responseBody) ?? error.message;
44
+ const researchBlock = formatResearchRunningDraftBlock(message, argv);
45
+ if (researchBlock)
46
+ return { error: new Error(researchBlock.message), result: researchBlock, exitCode: 2 };
44
47
  return { error: new Error(message), exitCode: 1 };
45
48
  }
46
49
  return { error: error instanceof Error ? error : new Error(String(error)), exitCode: 1 };
47
50
  }
51
+ function formatResearchRunningDraftBlock(message, argv) {
52
+ if (!/Cannot generate draft in state "research_running"/i.test(message))
53
+ return null;
54
+ const projectId = resolveProjectIdFromArgv(argv);
55
+ const recoveryCommands = projectId
56
+ ? [
57
+ `coauthor research status ${projectId} --json`,
58
+ `coauthor workflow resume ${projectId} --target draft --wait --json`,
59
+ `coauthor workflow rewind ${projectId} --target research --json`,
60
+ ]
61
+ : [
62
+ 'coauthor research status <project-id> --json',
63
+ 'coauthor workflow resume <project-id> --target draft --wait --json',
64
+ 'coauthor workflow rewind <project-id> --target research --json',
65
+ ];
66
+ return {
67
+ ok: false,
68
+ status: 'blocked_by_research',
69
+ message: `${message}\nResearch is still marked running. Check status, resume, or rewind the research stage before generating a draft.`,
70
+ recovery_commands: recoveryCommands,
71
+ exitCode: 2,
72
+ };
73
+ }
74
+ function resolveProjectIdFromArgv(argv) {
75
+ const projectFlagIndex = argv.indexOf('--project');
76
+ if (projectFlagIndex >= 0 && argv[projectFlagIndex + 1])
77
+ return argv[projectFlagIndex + 1];
78
+ const commandWords = new Set([
79
+ 'auth',
80
+ 'login',
81
+ 'status',
82
+ 'logout',
83
+ 'project',
84
+ 'create',
85
+ 'list',
86
+ 'show',
87
+ 'workspace',
88
+ 'chat',
89
+ 'contract',
90
+ 'outline',
91
+ 'research-plan',
92
+ 'research',
93
+ 'run',
94
+ 'draft',
95
+ 'generate',
96
+ 'get',
97
+ 'save',
98
+ 'workflow',
99
+ 'advance',
100
+ 'rewind',
101
+ 'approve',
102
+ 'resume',
103
+ 'image',
104
+ 'thread',
105
+ 'publish',
106
+ 'export',
107
+ 'operation',
108
+ 'credits',
109
+ 'skills',
110
+ ]);
111
+ return argv.find((arg) => !arg.startsWith('-') && !commandWords.has(arg));
112
+ }
48
113
  function extractErrorMessage(body) {
49
114
  if (!body || typeof body !== 'object' || Array.isArray(body) || !('error' in body))
50
115
  return null;
package/dist/parser.js CHANGED
@@ -100,6 +100,8 @@ function commandLength(positionals) {
100
100
  return second && ['create', 'list', 'show'].includes(second) ? 2 : 1;
101
101
  if (first === 'workspace')
102
102
  return second === 'show' ? 2 : 1;
103
+ if (first === 'chat')
104
+ return second && ['show', 'list'].includes(second) ? 2 : 1;
103
105
  if (first === 'skills')
104
106
  return second && ['list', 'install'].includes(second) ? 2 : 1;
105
107
  if (first === 'workflow')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clickminded/coauthor-cli",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "coauthor": "dist/index.js",