@kosuke-ai/kosuke 1.0.58 → 1.0.60

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.
Files changed (80) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +31 -2
  3. package/dist/commands/hand-written.js +15 -0
  4. package/dist/commands/help.js +52 -0
  5. package/dist/generated/commands/crons/create--personal.js +2 -1
  6. package/dist/generated/commands/crons/update--personal.js +2 -1
  7. package/dist/generated/commands/deploys/queue-update.js +2 -1
  8. package/dist/generated/commands/find/index.js +17 -0
  9. package/dist/generated/commands/index.js +279 -291
  10. package/dist/generated/commands/integrations/tools-call.js +2 -1
  11. package/dist/generated/commands/projects/batch-merging-get.js +1 -1
  12. package/dist/generated/commands/projects/batch-merging-picks-set.js +2 -1
  13. package/dist/generated/commands/projects/batch-merging-set.js +19 -1
  14. package/dist/generated/commands/projects/create.js +21 -9
  15. package/dist/generated/commands/projects/labels-delete.js +2 -1
  16. package/dist/generated/commands/projects/live-logs-list.js +1 -1
  17. package/dist/generated/commands/projects/live-queries-run.js +1 -1
  18. package/dist/generated/commands/projects/live-traffic-get.js +1 -1
  19. package/dist/generated/commands/projects/releases-list.js +1 -1
  20. package/dist/generated/commands/projects/services-create.js +6 -3
  21. package/dist/generated/commands/projects/services-update.js +6 -3
  22. package/dist/generated/commands/projects/settings-set.js +4 -2
  23. package/dist/generated/commands/projects/versions-list.js +1 -1
  24. package/dist/generated/commands/sensei/cards-mark-read.js +1 -1
  25. package/dist/generated/commands/sensei/cards-set.js +40 -10
  26. package/dist/generated/commands/sensei/connect-buttons-create.js +2 -1
  27. package/dist/generated/commands/sensei/describe-cards-create.js +1 -1
  28. package/dist/generated/commands/sensei/messages-send.js +2 -1
  29. package/dist/generated/commands/sensei/sessions-assessments-create.js +3 -1
  30. package/dist/generated/commands/sensei/sessions-facts-get.js +1 -1
  31. package/dist/generated/commands/sensei/suggestions-create.js +2 -1
  32. package/dist/generated/commands/sensei/threads-get.js +8 -1
  33. package/dist/generated/commands/sensei/threads-split.js +2 -1
  34. package/dist/generated/commands/sensei/tickets-merge.js +4 -2
  35. package/dist/generated/commands/sensei/watch-reads-finish.js +4 -2
  36. package/dist/generated/commands/sensei/writing-samples-create.js +2 -1
  37. package/dist/generated/commands/sessions/get.js +1 -1
  38. package/dist/generated/commands/sessions/labels-set.js +6 -3
  39. package/dist/generated/commands/sessions/labels-update.js +4 -2
  40. package/dist/generated/commands/sessions/list--all.js +2 -3
  41. package/dist/generated/commands/sessions/qa-runs-finish.js +2 -1
  42. package/dist/generated/commands/sessions/qa-runs-results-create.js +2 -1
  43. package/dist/generated/commands/sessions/qa-runs-start.js +2 -1
  44. package/dist/generated/commands/sessions/queue-update.js +2 -1
  45. package/dist/generated/commands/sessions/send.js +4 -2
  46. package/dist/generated/commands/sessions/update.js +2 -1
  47. package/dist/generated/commands/skills/create--personal.js +2 -1
  48. package/dist/generated/commands/skills/create.js +2 -1
  49. package/dist/generated/commands/skills/update--personal.js +2 -1
  50. package/dist/generated/commands/skills/update.js +2 -1
  51. package/dist/generated/commands/tasks/create--personal.js +235 -11
  52. package/dist/generated/commands/tasks/create.js +1 -1
  53. package/dist/generated/commands/tasks/delete--personal.js +6 -3
  54. package/dist/generated/commands/tasks/get--personal.js +6 -3
  55. package/dist/generated/commands/tasks/list--all.js +1 -1
  56. package/dist/generated/commands/tasks/list--personal.js +91 -9
  57. package/dist/generated/commands/tasks/move--personal.js +7 -4
  58. package/dist/generated/commands/tasks/start--personal.js +6 -3
  59. package/dist/generated/commands/tasks/update--personal.js +85 -10
  60. package/dist/generated/commands/tasks/update.js +3 -2
  61. package/dist/generated/commands/user/personal-space-get.js +19 -0
  62. package/dist/generated/commands/workspaces/sandboxes-stop.js +2 -1
  63. package/dist/program.js +13 -2
  64. package/dist/runtime/arguments.js +58 -0
  65. package/dist/runtime/attach.js +177 -18
  66. package/dist/runtime/help.js +398 -0
  67. package/dist/runtime/personal-space-id.js +26 -0
  68. package/dist/runtime/personal-space.js +10 -0
  69. package/dist/runtime/runtime.js +25 -5
  70. package/dist/runtime/thread-text.js +316 -0
  71. package/openapi.json +321 -599
  72. package/package.json +11 -4
  73. package/dist/generated/commands/projects/backlog-items-create.js +0 -35
  74. package/dist/generated/commands/projects/backlog-items-delete.js +0 -20
  75. package/dist/generated/commands/projects/backlog-items-list.js +0 -19
  76. package/dist/generated/commands/projects/backlog-items-send.js +0 -20
  77. package/dist/generated/commands/projects/backlog-items-update.js +0 -42
  78. package/dist/generated/commands/projects/live-writes-create.js +0 -38
  79. package/dist/generated/commands/projects/live-writes-get.js +0 -21
  80. package/dist/generated/commands/projects/workflow-set.js +0 -33
@@ -0,0 +1,26 @@
1
+ import { execute } from './invoke.js';
2
+ import { PERSONAL_SPACE_PATH } from './personal-space.js';
3
+ /**
4
+ * The caller's Personal space id, which a personal form made from a workspace operation sends
5
+ * as its workspaceId. A refusal (an agent token that acts in its own workspace) is printed as
6
+ * the platform answered it, and the command sends nothing; null then.
7
+ */
8
+ export async function personalSpaceId(runtime) {
9
+ const outcome = await execute(runtime.client(), {
10
+ method: 'get',
11
+ path: PERSONAL_SPACE_PATH,
12
+ pathParams: {},
13
+ query: {},
14
+ body: null,
15
+ });
16
+ const data = isRecord(outcome.body) && isRecord(outcome.body.data) ? outcome.body.data : null;
17
+ if (outcome.ok && typeof data?.id === 'string' && data.id)
18
+ return data.id;
19
+ runtime.output(outcome.ok
20
+ ? { ok: false, status: outcome.status, body: { error: 'No Personal space id came back.' } }
21
+ : outcome);
22
+ return null;
23
+ }
24
+ function isRecord(value) {
25
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
26
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Where the word `personal` goes on a command whose workspace operation reaches the Personal
3
+ * space (`x-kosuke-personal-space`): the generator gives that command a personal form, and the
4
+ * runtime fills the form's `workspaceId` from this operation's `{ data: { id } }` before the
5
+ * call. Imports nothing, so the generator loads it on plain node.
6
+ */
7
+ /** `kosuke user personal-space get`: the caller's Personal space id, made on first use. */
8
+ export const PERSONAL_SPACE_PATH = '/api/user/personal-space';
9
+ /** The path param the Personal space id fills. */
10
+ export const PERSONAL_SPACE_PARAM = 'workspaceId';
@@ -2,7 +2,7 @@ import { createApiClient } from './client.js';
2
2
  import { resolveCredentials, } from './credentials.js';
3
3
  export function createRuntime({ flags, env = process.env, fetch, stdout = (line) => process.stdout.write(line + '\n'), stderr = (line) => process.stderr.write(line + '\n'), exit = (code) => {
4
4
  process.exitCode = code;
5
- }, }) {
5
+ }, tty = { stdout: process.stdout.isTTY === true, stderr: process.stderr.isTTY === true }, }) {
6
6
  let resolved = null;
7
7
  const credentials = () => (resolved ??= resolveCredentials(flags, env));
8
8
  return {
@@ -27,16 +27,36 @@ export function createRuntime({ flags, env = process.env, fetch, stdout = (line)
27
27
  // 204 has nothing to print; anything else is passed through as JSON so
28
28
  // `| jq` works without the platform CLI knowing the shape.
29
29
  if (outcome.body !== undefined)
30
- stdout(render(outcome.body));
30
+ stdout(render(outcome.body, flags.pretty || tty.stdout));
31
31
  return;
32
32
  }
33
- stderr(render(outcome.body ?? { error: `HTTP ${outcome.status}` }));
33
+ stderr(render(outcome.body ?? { error: `HTTP ${outcome.status}` }, flags.pretty || tty.stderr));
34
34
  exit(1);
35
35
  },
36
36
  };
37
37
  }
38
- function render(body) {
39
- return typeof body === 'string' ? body : JSON.stringify(body, null, 2);
38
+ /**
39
+ * Indented for a person at a terminal or with `--pretty`; one line for a pipe, which an agent
40
+ * reads token by token, with each row of a top-level `data` list on its own line.
41
+ */
42
+ function render(body, pretty) {
43
+ if (typeof body === 'string')
44
+ return body;
45
+ if (pretty)
46
+ return JSON.stringify(body, null, 2);
47
+ if (!isRecord(body) || !Array.isArray(body.data) || !body.data.length) {
48
+ return JSON.stringify(body);
49
+ }
50
+ // Key order kept, and undefined left out, as JSON.stringify does.
51
+ const members = Object.entries(body)
52
+ .filter(([, value]) => value !== undefined)
53
+ .map(([key, value]) => key === 'data'
54
+ ? `"data":[\n${value.map((row) => JSON.stringify(row) ?? 'null').join(',\n')}\n]`
55
+ : `${JSON.stringify(key)}:${JSON.stringify(value)}`);
56
+ return `{${members.join(',')}}`;
57
+ }
58
+ function isRecord(value) {
59
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
40
60
  }
41
61
  /** Thrown after `exit` so the action stops without printing a second error. */
42
62
  export class ExitSignal extends Error {
@@ -0,0 +1,316 @@
1
+ /**
2
+ * `kosuke sensei threads get` as an agent reads it. By default the thread's words only: the
3
+ * member's messages, Sensei's replies, card headlines and one line per event, without tool
4
+ * blocks, agent payloads, recordings or presigned URLs. `--tools` prints the platform's
5
+ * response, with a full `agent summary` result cut to a marker. Pure: it reads the parsed
6
+ * body defensively, since `apps/cli` cannot import the platform's types, and returns any
7
+ * shape it does not know unchanged.
8
+ */
9
+ /** Help text for the CLI-only flag. */
10
+ export const TOOLS_FLAG_HELP = 'Include tool blocks (commands and results), as the platform returns them.';
11
+ /** The `sensei threads get` response as an agent reads it by default, or with `--tools`. Pure. */
12
+ export function threadView(body, options) {
13
+ try {
14
+ if (!isRecord(body) || !isRecord(body.data) || !Array.isArray(body.data.messages)) {
15
+ return body;
16
+ }
17
+ return options.tools
18
+ ? { ...body, data: withoutSummaryResults(body.data) }
19
+ : { ...body, data: textView(body.data) };
20
+ }
21
+ catch {
22
+ return body;
23
+ }
24
+ }
25
+ function isRecord(value) {
26
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
27
+ }
28
+ function str(value) {
29
+ return typeof value === 'string' ? value : undefined;
30
+ }
31
+ function records(value) {
32
+ return Array.isArray(value) ? value.filter(isRecord) : [];
33
+ }
34
+ /** The first non-empty line of a text. */
35
+ function firstLine(text) {
36
+ const line = text
37
+ ?.split('\n')
38
+ .map((part) => part.trim())
39
+ .find(Boolean);
40
+ return line || undefined;
41
+ }
42
+ /** A label short enough to keep beside a headline. */
43
+ const SHORT_LABEL_LENGTH = 60;
44
+ function short(value) {
45
+ const text = str(value)?.trim();
46
+ return text && text.length <= SHORT_LABEL_LENGTH ? text : undefined;
47
+ }
48
+ /** Drops the keys whose value is undefined, an empty string or an empty array. */
49
+ function compact(row) {
50
+ return Object.fromEntries(Object.entries(row).filter(([, value]) => value !== undefined && value !== '' && !(Array.isArray(value) && value.length === 0)));
51
+ }
52
+ // --- tools: true -------------------------------------------------------------------------
53
+ const AGENT_SUMMARY = /\bkosuke\s+agent\s+summary\s+get\b/;
54
+ function runsAgentSummary(input) {
55
+ if (!isRecord(input))
56
+ return false;
57
+ return Object.values(input).some((value) => typeof value === 'string' && AGENT_SUMMARY.test(value));
58
+ }
59
+ function withoutSummaryInBlocks(blocks) {
60
+ if (!Array.isArray(blocks))
61
+ return blocks;
62
+ return blocks.map((block) => {
63
+ if (!isRecord(block) || block.type !== 'tool' || typeof block.result !== 'string')
64
+ return block;
65
+ if (!runsAgentSummary(block.input))
66
+ return block;
67
+ return {
68
+ ...block,
69
+ result: `[agent summary output omitted: ${block.result.length} chars; run kosuke agent summary get --peek]`,
70
+ };
71
+ });
72
+ }
73
+ function withoutSummaryInRow(row) {
74
+ return isRecord(row) && Array.isArray(row.blocks)
75
+ ? { ...row, blocks: withoutSummaryInBlocks(row.blocks) }
76
+ : row;
77
+ }
78
+ function withoutSummaryResults(data) {
79
+ const copy = { ...data, messages: data.messages.map(withoutSummaryInRow) };
80
+ if ('root' in data)
81
+ copy.root = withoutSummaryInRow(data.root);
82
+ return copy;
83
+ }
84
+ function cardState(card) {
85
+ const decision = str(card.decision);
86
+ if (decision)
87
+ return decision;
88
+ if (card.closedAt)
89
+ return 'closed';
90
+ if (card.skippedAt)
91
+ return 'skipped';
92
+ if (card.answeredAt)
93
+ return 'answered';
94
+ return 'open';
95
+ }
96
+ /** The Turn statuses that still write their Sensei row (`ACTIVE_SENSEI_TURN_STATUSES`). */
97
+ const ACTIVE_TURN = new Set(['pending', 'running']);
98
+ function textView(data) {
99
+ const thread = isRecord(data.thread) ? data.thread : undefined;
100
+ const cards = new Map();
101
+ for (const card of records(thread?.cards)) {
102
+ const id = str(card.id);
103
+ if (id)
104
+ cards.set(id, card);
105
+ }
106
+ // Only a full read lists the open cards; on a page a card's state is unknown.
107
+ const context = { cards, knowsOpenCards: thread !== undefined };
108
+ const rawMessages = records(data.messages);
109
+ const messages = rawMessages.map((row) => messageView(row, context)).filter(isRecord);
110
+ const root = isRecord(data.root) ? messageView(data.root, context) : undefined;
111
+ // A Sensei row its running Turn is still writing is never the `--since` cursor: the reply
112
+ // filled into it later would be read as older than the cursor and never come back.
113
+ const running = isRecord(data.turn) && ACTIVE_TURN.has(str(data.turn.status) ?? '')
114
+ ? str(data.turn.id)
115
+ : undefined;
116
+ const settled = rawMessages.filter((row) => !(running && row.role === 'assistant' && str(row.turnId) === running));
117
+ const newest = settled.at(-1) ?? (rawMessages.length === 0 && isRecord(data.root) ? data.root : undefined);
118
+ const view = {};
119
+ if (thread)
120
+ view.thread = threadHeader(thread);
121
+ if (isRecord(data.turn)) {
122
+ view.turn = compact({ id: str(data.turn.id), status: str(data.turn.status) });
123
+ }
124
+ if (data.eventTurnActive === true)
125
+ view.eventTurnActive = true;
126
+ const delegates = records(data.delegates).map((delegate) => compact({
127
+ id: str(delegate.id),
128
+ kind: str(delegate.kind),
129
+ label: str(delegate.label),
130
+ state: str(delegate.state),
131
+ }));
132
+ if (delegates.length > 0)
133
+ view.delegates = delegates;
134
+ if (root)
135
+ view.root = root;
136
+ view.messages = messages;
137
+ if (typeof data.hasMoreBefore === 'boolean')
138
+ view.hasMoreBefore = data.hasMoreBefore;
139
+ if (typeof data.hasMoreAfter === 'boolean')
140
+ view.hasMoreAfter = data.hasMoreAfter;
141
+ // What `--before` takes to read older replies, kept even when that row says nothing.
142
+ const oldest = str(rawMessages[0]?.id);
143
+ if (oldest && data.hasMoreBefore === true)
144
+ view.oldestMessageId = oldest;
145
+ // What `--since` takes next time, kept even when that row was dropped here.
146
+ const newestId = str(newest?.id);
147
+ if (newestId)
148
+ view.newestMessageId = newestId;
149
+ if ('upNext' in data)
150
+ view.upNext = upNextView(data.upNext);
151
+ return view;
152
+ }
153
+ function threadHeader(thread) {
154
+ return compact({
155
+ id: str(thread.id),
156
+ title: str(thread.title),
157
+ kind: str(thread.kind),
158
+ state: str(thread.state),
159
+ projectId: str(thread.projectId),
160
+ turn: str(thread.turn),
161
+ sessions: records(thread.sessions).map((session) => compact({
162
+ id: str(session.id),
163
+ title: str(session.title),
164
+ projectId: str(session.projectId),
165
+ number: typeof session.pullRequestNumber === 'number' ? session.pullRequestNumber : undefined,
166
+ })),
167
+ openCards: records(thread.cards)
168
+ .filter((card) => cardState(card) === 'open')
169
+ .map((card) => compact({
170
+ id: str(card.id),
171
+ key: str(card.key),
172
+ headline: str(card.headline) ?? str(card.title),
173
+ })),
174
+ });
175
+ }
176
+ function upNextView(upNext) {
177
+ if (!isRecord(upNext) || !Array.isArray(upNext.messages)) {
178
+ return Array.isArray(upNext) ? { count: upNext.length } : upNext;
179
+ }
180
+ const view = {
181
+ messages: records(upNext.messages).map((message) => compact({
182
+ id: str(message.id),
183
+ text: str(message.content),
184
+ attachments: filenames(message.attachments),
185
+ })),
186
+ };
187
+ if (isRecord(upNext.paused))
188
+ view.paused = str(upNext.paused.reason) ?? true;
189
+ return view;
190
+ }
191
+ const ROLES = { user: 'member', assistant: 'sensei', system: 'system' };
192
+ function filenames(attachments) {
193
+ return records(attachments)
194
+ .map((attachment) => str(attachment.filename))
195
+ .filter((name) => Boolean(name));
196
+ }
197
+ function cardView(block, context) {
198
+ const id = str(block.cardId);
199
+ const card = id ? context.cards.get(id) : undefined;
200
+ if (!card) {
201
+ return compact({
202
+ id,
203
+ headline: firstLine(str(block.content)),
204
+ // The thread lists its open cards: one it does not list is no longer open.
205
+ state: context.knowsOpenCards ? 'closed' : undefined,
206
+ });
207
+ }
208
+ const action = records(card.promptActions)[0];
209
+ return compact({
210
+ id,
211
+ key: str(card.key),
212
+ headline: str(card.headline) ?? str(card.title) ?? firstLine(str(block.content)),
213
+ state: cardState(card),
214
+ verb: short(card.verb) ?? short(action?.label),
215
+ });
216
+ }
217
+ function eventLine(block) {
218
+ const kind = str(block.kind) ?? 'event';
219
+ const what = firstLine(str(block.content));
220
+ const subject = str(block.sessionId) ? ` (session ${str(block.sessionId)})` : '';
221
+ return `${kind}${what ? `: ${what}` : ''}${subject}`;
222
+ }
223
+ function clarificationQuestions(block) {
224
+ const data = isRecord(block.clarificationData) ? block.clarificationData : undefined;
225
+ const questions = records(data?.questions)
226
+ .map((question) => {
227
+ const text = str(question.question) ?? str(question.topic);
228
+ if (!text)
229
+ return undefined;
230
+ const choices = records(question.options)
231
+ .map((option) => str(option.title))
232
+ .filter(Boolean);
233
+ return choices.length > 0 ? `${text} (${choices.join(' / ')})` : text;
234
+ })
235
+ .filter((text) => Boolean(text));
236
+ if (questions.length > 0)
237
+ return questions;
238
+ const fallback = str(block.content)?.trim();
239
+ return fallback ? [fallback] : [];
240
+ }
241
+ /** One row as an agent reads it, or undefined for a row that carries no words. */
242
+ function messageView(row, context) {
243
+ const blocks = records(row.blocks);
244
+ const texts = [];
245
+ const notes = [];
246
+ const events = [];
247
+ const questions = [];
248
+ let card;
249
+ for (const block of blocks) {
250
+ const content = str(block.content);
251
+ switch (block.type) {
252
+ case 'text':
253
+ case 'plan':
254
+ if (content?.trim())
255
+ texts.push(content);
256
+ break;
257
+ case 'sensei_card':
258
+ card ??= cardView(block, context);
259
+ break;
260
+ case 'sensei_first_card':
261
+ card ??= compact({ headline: firstLine(content) });
262
+ break;
263
+ case 'sensei_event':
264
+ events.push(eventLine(block));
265
+ break;
266
+ case 'clarification':
267
+ questions.push(...clarificationQuestions(block));
268
+ break;
269
+ case 'sensei_receipt':
270
+ case 'sensei_welcome':
271
+ case 'sensei_events_notice':
272
+ case 'app_save':
273
+ case 'filed_sessions':
274
+ case 'artifact': {
275
+ const line = firstLine(content);
276
+ if (line)
277
+ notes.push(line);
278
+ break;
279
+ }
280
+ case 'sensei_delegate': {
281
+ const label = str(block.label);
282
+ const outcome = str(block.outcome);
283
+ if (label || outcome) {
284
+ notes.push(`delegate ${label ?? ''}: ${outcome ?? ''}${str(block.reason) ? ` (${firstLine(str(block.reason))})` : ''}`.trim());
285
+ }
286
+ break;
287
+ }
288
+ default:
289
+ // tool, recordings, explainers, connect, suggestions and the rest: nothing to read.
290
+ break;
291
+ }
292
+ }
293
+ const metadata = isRecord(row.metadata) ? row.metadata : undefined;
294
+ // An as-drawn Approve row shows the card's short words in place of its instruction.
295
+ const content = str(metadata?.cardRequest) ?? str(row.content);
296
+ const text = content?.trim() ? content : texts.join('\n\n');
297
+ const attachments = filenames(row.attachments);
298
+ if (!text && !card && events.length === 0 && questions.length === 0 && notes.length === 0) {
299
+ if (attachments.length === 0)
300
+ return undefined;
301
+ }
302
+ const role = str(row.role);
303
+ return compact({
304
+ id: str(row.id),
305
+ role: (role && ROLES[role]) ?? role,
306
+ at: str(row.createdAt),
307
+ text,
308
+ card,
309
+ events,
310
+ questions,
311
+ notes,
312
+ attachments,
313
+ // A Reply to Sensei about a card quotes the card's headline above the member's words.
314
+ aboutCard: str(metadata?.cardHeadline),
315
+ });
316
+ }