@kortix/ui 0.14.15 → 0.14.17

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 (33) hide show
  1. package/dist/_vendor/shared/artifact-annotations.d.ts +126 -1
  2. package/dist/_vendor/shared/artifact-comments.d.ts +30 -1
  3. package/dist/_vendor/shared/channel-descriptors.d.ts +5 -1
  4. package/dist/customize/settings/tabs/experimental-tab.js +1 -4
  5. package/dist/customize/shared/channels-switch-dialog.d.ts +4 -8
  6. package/dist/customize/shared/channels-switch-dialog.js +5 -16
  7. package/dist/customize/shared/feature-gate-screen.js +5 -7
  8. package/dist/files/drives/drive-path-param.d.ts +4 -0
  9. package/dist/files/drives/drive-path-param.js +50 -0
  10. package/dist/files/index.d.ts +1 -0
  11. package/dist/files/index.js +1 -0
  12. package/dist/i18n/en.d.ts +48 -18
  13. package/dist/i18n/en.js +62 -32
  14. package/dist/internal/lib/use-project-feature-flags.js +0 -2
  15. package/dist/internal/lib/utils/kortix-system-tags.d.ts +2 -8
  16. package/dist/internal/lib/utils/kortix-system-tags.js +4 -58
  17. package/dist/markdown/doc-editor/doc-menus.d.ts +6 -0
  18. package/dist/markdown/doc-editor/doc-menus.js +74 -9
  19. package/dist/session/status/provisioning-failure.d.ts +22 -1
  20. package/dist/session/status/provisioning-failure.js +35 -7
  21. package/dist/session/status/session-starting-loader.js +15 -5
  22. package/dist/session/tool/tools/kortix-tool-card.d.ts +4 -81
  23. package/dist/session/tool/tools/kortix-tool-card.js +4 -184
  24. package/dist/session/tool/tools/workflow-run.d.ts +4 -126
  25. package/dist/session/tool/tools/workflow-run.js +4 -249
  26. package/dist/session/turn/subagent-notice.d.ts +4 -56
  27. package/dist/session/turn/subagent-notice.js +4 -109
  28. package/dist/styles.css +1 -1
  29. package/dist/workspace/sidebar/project-session-list.js +13 -26
  30. package/dist/workspace/sidebar/session-brief-hover-card.d.ts +0 -3
  31. package/dist/workspace/sidebar/session-brief-hover-card.js +0 -3
  32. package/package.json +3 -3
  33. package/skills/kortix-ui/references/modules.md +1 -1
@@ -1,83 +1,6 @@
1
1
  /**
2
- * The My Kortix platform tools' result, parsed defensively.
3
- *
4
- * The shape is `KortixToolResult` in `packages/api-contract/src/kortix-tool-card.ts`.
5
- * `@kortix/ui` does not depend on that package, so the types are mirrored here
6
- * and every field is checked. A result that does not parse is `null`, and the
7
- * call renders as the generic tool card instead of a half-drawn one.
2
+ * The My Kortix platform tools' and the multi-agent tools' `KortixToolResult`
3
+ * parser. It lives in `@kortix/sdk` (`core/turns/tools/kortix-tool-card.ts`);
4
+ * this file keeps the old import path for the web cards.
8
5
  */
9
- /** `KORTIX_PLATFORM_TOOL_NAMES` in `packages/api-contract/src/kortix-tool-card.ts`. */
10
- export declare const KORTIX_PLATFORM_TOOLS: readonly ["kortix_agents", "kortix_sessions", "kortix_schedules", "kortix_connectors", "kortix_people", "kortix_access", "kortix_usage", "kortix_change_requests", "kortix_memory"];
11
- export type KortixPlatformTool = (typeof KORTIX_PLATFORM_TOOLS)[number];
12
- /**
13
- * The multi-agent tools (`packages/api-contract/src/multi-agent.ts`): the same
14
- * `KortixToolResult`, with `data` = the contract object the card body draws.
15
- * `task_create`, `task_list`, `task_get` and `task_update` are also the names of
16
- * older plugin tools: `multi-agent-tools.tsx` tells the two apart by the output.
17
- */
18
- export declare const MULTI_AGENT_TOOLS: readonly ["send_message", "list_agents", "task_create", "task_list", "task_get", "task_update", "task_stop"];
19
- export type MultiAgentTool = (typeof MULTI_AGENT_TOOLS)[number];
20
- /** Every tool whose result is a `KortixToolResult` card. */
21
- export type KortixCardTool = KortixPlatformTool | MultiAgentTool;
22
- export type KortixCardStatus = 'done' | 'running' | 'needs_you' | 'denied' | 'error';
23
- export interface KortixCardRow {
24
- title: string;
25
- detail?: string;
26
- badge?: string;
27
- /** In-app path (`/projects/…`). A row never links outside the app. */
28
- href?: string;
29
- }
30
- /** A `before` of `null` is a new setting; an `after` of `null` is a removed one. */
31
- export interface KortixCardChange {
32
- field: string;
33
- before: string | null;
34
- after: string | null;
35
- }
36
- export interface KortixCardLink {
37
- label: string;
38
- /** An in-app path, or an absolute `https://` URL (a connector sign-in link). */
39
- href: string;
40
- /** True for an absolute `https://` URL: it opens in a new tab and is the card's primary action. */
41
- external: boolean;
42
- }
43
- export interface KortixToolCard {
44
- title: string;
45
- subtitle?: string;
46
- status: KortixCardStatus;
47
- rows: KortixCardRow[];
48
- changes: KortixCardChange[];
49
- links: KortixCardLink[];
50
- }
51
- export interface KortixGrantor {
52
- name: string;
53
- role: string;
54
- }
55
- export interface KortixToolError {
56
- kind: 'denied' | 'not_found' | 'invalid' | 'failed';
57
- message: string;
58
- grantors: KortixGrantor[];
59
- }
60
- export interface KortixToolResult {
61
- ok: boolean;
62
- tool: KortixCardTool;
63
- action: string;
64
- card: KortixToolCard;
65
- error?: KortixToolError;
66
- /** What the model reads. Only the multi-agent card bodies draw it, each through its own defensive parser. */
67
- data?: unknown;
68
- }
69
- /** The platform tool a call name names: `kortix_agents`, also as `kortix-agents` or `oc-kortix_agents`. */
70
- export declare function kortixPlatformTool(name: string): KortixPlatformTool | undefined;
71
- /** A multi-agent tool a call name names: `send_message`, also as `send-message` or `oc-send_message`. */
72
- export declare function multiAgentTool(name: string): MultiAgentTool | undefined;
73
- /** Any tool that answers with a `KortixToolResult` card. */
74
- export declare function kortixCardTool(name: string): KortixCardTool | undefined;
75
- /** An in-app path: one leading `/`, never `//` (a protocol-relative URL leaves the app). */
76
- export declare function inAppHref(value: unknown): string | undefined;
77
- /**
78
- * The result in a platform call's output (or its error text, when the call threw
79
- * it): one JSON object. `null` when the text is not that object. `callName` names
80
- * the tool when the result leaves `tool` out.
81
- */
82
- export declare function parseKortixToolResult(raw: string | undefined, callName?: string): KortixToolResult | null;
83
- export declare function isReadAction(action: unknown): boolean;
6
+ export { KORTIX_PLATFORM_TOOLS, MULTI_AGENT_TOOLS, inAppHref, isReadAction, kortixCardTool, kortixPlatformTool, multiAgentTool, parseKortixToolResult, type KortixCardChange, type KortixCardLink, type KortixCardRow, type KortixCardStatus, type KortixCardTool, type KortixGrantor, type KortixPlatformTool, type KortixToolCard, type KortixToolError, type KortixToolResult, type MultiAgentTool, } from '@kortix/sdk';
@@ -1,186 +1,6 @@
1
1
  /**
2
- * The My Kortix platform tools' result, parsed defensively.
3
- *
4
- * The shape is `KortixToolResult` in `packages/api-contract/src/kortix-tool-card.ts`.
5
- * `@kortix/ui` does not depend on that package, so the types are mirrored here
6
- * and every field is checked. A result that does not parse is `null`, and the
7
- * call renders as the generic tool card instead of a half-drawn one.
2
+ * The My Kortix platform tools' and the multi-agent tools' `KortixToolResult`
3
+ * parser. It lives in `@kortix/sdk` (`core/turns/tools/kortix-tool-card.ts`);
4
+ * this file keeps the old import path for the web cards.
8
5
  */
9
- /** `KORTIX_PLATFORM_TOOL_NAMES` in `packages/api-contract/src/kortix-tool-card.ts`. */
10
- export const KORTIX_PLATFORM_TOOLS = [
11
- 'kortix_agents',
12
- 'kortix_sessions',
13
- 'kortix_schedules',
14
- 'kortix_connectors',
15
- 'kortix_people',
16
- 'kortix_access',
17
- 'kortix_usage',
18
- 'kortix_change_requests',
19
- 'kortix_memory',
20
- ];
21
- /**
22
- * The multi-agent tools (`packages/api-contract/src/multi-agent.ts`): the same
23
- * `KortixToolResult`, with `data` = the contract object the card body draws.
24
- * `task_create`, `task_list`, `task_get` and `task_update` are also the names of
25
- * older plugin tools: `multi-agent-tools.tsx` tells the two apart by the output.
26
- */
27
- export const MULTI_AGENT_TOOLS = [
28
- 'send_message',
29
- 'list_agents',
30
- 'task_create',
31
- 'task_list',
32
- 'task_get',
33
- 'task_update',
34
- 'task_stop',
35
- ];
36
- const STATUSES = new Set(['done', 'running', 'needs_you', 'denied', 'error']);
37
- const ERROR_KINDS = new Set(['denied', 'not_found', 'invalid', 'failed']);
38
- /** A card holds a short list; a longer one is the tool's mistake, not a page to render. */
39
- const MAX_ITEMS = 50;
40
- const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
41
- const text = (value) => typeof value === 'string' && value.trim() ? value.trim() : undefined;
42
- /** The platform tool a call name names: `kortix_agents`, also as `kortix-agents` or `oc-kortix_agents`. */
43
- export function kortixPlatformTool(name) {
44
- const normalized = name.replace(/^oc[-_]/, '').replace(/-/g, '_');
45
- return KORTIX_PLATFORM_TOOLS.includes(normalized)
46
- ? normalized
47
- : undefined;
48
- }
49
- /** A multi-agent tool a call name names: `send_message`, also as `send-message` or `oc-send_message`. */
50
- export function multiAgentTool(name) {
51
- const normalized = name.replace(/^oc[-_]/, '').replace(/-/g, '_');
52
- return MULTI_AGENT_TOOLS.includes(normalized) ? normalized : undefined;
53
- }
54
- /** Any tool that answers with a `KortixToolResult` card. */
55
- export function kortixCardTool(name) {
56
- return kortixPlatformTool(name) ?? multiAgentTool(name);
57
- }
58
- /** An in-app path: one leading `/`, never `//` (a protocol-relative URL leaves the app). */
59
- export function inAppHref(value) {
60
- const href = text(value);
61
- return href && href.startsWith('/') && !href.startsWith('//') && !href.includes('\\') ? href : undefined;
62
- }
63
- function externalHref(value) {
64
- const href = text(value);
65
- if (!href)
66
- return undefined;
67
- try {
68
- return new URL(href).protocol === 'https:' ? href : undefined;
69
- }
70
- catch {
71
- return undefined;
72
- }
73
- }
74
- function list(value, item) {
75
- if (!Array.isArray(value))
76
- return [];
77
- const out = [];
78
- for (const entry of value.slice(0, MAX_ITEMS)) {
79
- const parsed = item(entry);
80
- if (parsed !== undefined)
81
- out.push(parsed);
82
- }
83
- return out;
84
- }
85
- function row(entry) {
86
- if (!isObject(entry))
87
- return undefined;
88
- const title = text(entry.title);
89
- if (!title)
90
- return undefined;
91
- const detail = text(entry.detail);
92
- const badge = text(entry.badge);
93
- const href = inAppHref(entry.href);
94
- return { title, ...(detail ? { detail } : {}), ...(badge ? { badge } : {}), ...(href ? { href } : {}) };
95
- }
96
- const setting = (value) => value === null ? null : typeof value === 'string' ? value : typeof value === 'number' || typeof value === 'boolean' ? String(value) : undefined;
97
- function change(entry) {
98
- if (!isObject(entry))
99
- return undefined;
100
- const field = text(entry.field);
101
- const before = setting(entry.before);
102
- const after = setting(entry.after);
103
- if (!field || before === undefined || after === undefined || (before === null && after === null))
104
- return undefined;
105
- return { field, before, after };
106
- }
107
- function link(entry) {
108
- if (!isObject(entry))
109
- return undefined;
110
- const label = text(entry.label);
111
- if (!label)
112
- return undefined;
113
- const internal = inAppHref(entry.href);
114
- if (internal)
115
- return { label, href: internal, external: false };
116
- const external = externalHref(entry.href);
117
- return external ? { label, href: external, external: true } : undefined;
118
- }
119
- function grantor(entry) {
120
- if (!isObject(entry))
121
- return undefined;
122
- const name = text(entry.name);
123
- return name ? { name, role: text(entry.role) ?? '' } : undefined;
124
- }
125
- function toolError(value) {
126
- if (!isObject(value) || !ERROR_KINDS.has(value.kind))
127
- return undefined;
128
- return {
129
- kind: value.kind,
130
- message: text(value.message) ?? '',
131
- grantors: list(value.grantors, grantor),
132
- };
133
- }
134
- /**
135
- * The result in a platform call's output (or its error text, when the call threw
136
- * it): one JSON object. `null` when the text is not that object. `callName` names
137
- * the tool when the result leaves `tool` out.
138
- */
139
- export function parseKortixToolResult(raw, callName) {
140
- const source = raw?.trim();
141
- if (!source || !source.startsWith('{'))
142
- return null;
143
- let value;
144
- try {
145
- value = JSON.parse(source);
146
- }
147
- catch {
148
- return null;
149
- }
150
- if (!isObject(value) || typeof value.ok !== 'boolean' || !isObject(value.card))
151
- return null;
152
- const tool = typeof value.tool === 'string' ? kortixCardTool(value.tool) : callName !== undefined ? kortixCardTool(callName) : undefined;
153
- const title = text(value.card.title);
154
- if (!tool || !title)
155
- return null;
156
- const error = toolError(value.error);
157
- // A refusal reads as one whatever status the card names: the error kind is the fact.
158
- const status = error?.kind === 'denied'
159
- ? 'denied'
160
- : STATUSES.has(value.card.status)
161
- ? value.card.status
162
- : value.ok
163
- ? 'done'
164
- : 'error';
165
- const subtitle = text(value.card.subtitle);
166
- return {
167
- ok: value.ok,
168
- tool,
169
- action: text(value.action) ?? '',
170
- card: {
171
- title,
172
- ...(subtitle ? { subtitle } : {}),
173
- status,
174
- rows: list(value.card.rows, row),
175
- changes: list(value.card.changes, change),
176
- links: list(value.card.links, link),
177
- },
178
- ...(error ? { error } : {}),
179
- ...(value.data !== undefined ? { data: value.data } : {}),
180
- };
181
- }
182
- /** The actions that only read; every other action changes something (the SDK narration uses the same set). */
183
- const READ_ACTIONS = new Set(['', 'list', 'get', 'read', 'find', 'show', 'view', 'events', 'runs', 'test', 'search', 'check', 'status', 'pending', 'who']);
184
- export function isReadAction(action) {
185
- return READ_ACTIONS.has(typeof action === 'string' ? action : '');
186
- }
6
+ export { KORTIX_PLATFORM_TOOLS, MULTI_AGENT_TOOLS, inAppHref, isReadAction, kortixCardTool, kortixPlatformTool, multiAgentTool, parseKortixToolResult, } from '@kortix/sdk';
@@ -1,128 +1,6 @@
1
1
  /**
2
- * The `workflow` tool's part metadata, parsed defensively, and the pure helpers
3
- * its card draws from.
4
- *
5
- * The shape is `WorkflowRunMetadata` in `packages/api-contract/src/kortix-tool-card.ts`.
6
- * `@kortix/ui` does not depend on that package, so the types are mirrored here
7
- * and every field is checked. pi pushes it live with every change of the run,
8
- * and the final copy when the call returns.
2
+ * The `workflow` run document's parser and the helpers its card draws from.
3
+ * They live in `@kortix/sdk` (`core/turns/tools/workflow-run.ts`); this file
4
+ * keeps the old import path for the web card.
9
5
  */
10
- export type WorkflowRunStatus = 'running' | 'done' | 'failed' | 'stopped';
11
- /** `pending`: declared in the script's `meta.phases`, not started yet. */
12
- export type WorkflowPhaseStatus = WorkflowRunStatus | 'pending';
13
- export type WorkflowAgentStatus = 'starting' | 'running' | 'done' | 'failed' | 'stopped';
14
- export interface WorkflowRunPhase {
15
- title: string;
16
- status: WorkflowPhaseStatus;
17
- }
18
- export interface WorkflowRunAgent {
19
- id: string;
20
- label: string;
21
- agent?: string;
22
- phase?: string;
23
- status: WorkflowAgentStatus;
24
- sessionId?: string;
25
- summary?: string;
26
- /** Resumed run: the saved result of the earlier run was reused; no session started. */
27
- cached?: true;
28
- /** The model the script asked for. */
29
- model?: string;
30
- /** The answer is JSON checked against a schema. */
31
- structured?: true;
32
- /** 1 for the first attempt; a stalled agent restarts with the same prompt. */
33
- attempt?: number;
34
- /** Epoch ms. */
35
- startedAt?: number;
36
- endedAt?: number;
37
- }
38
- export interface WorkflowRun {
39
- runId: string;
40
- title: string;
41
- status: WorkflowRunStatus;
42
- phases: WorkflowRunPhase[];
43
- agents: WorkflowRunAgent[];
44
- startedAt?: number;
45
- endedAt?: number;
46
- error?: string;
47
- /** The most agents this run may start. */
48
- maxAgents?: number;
49
- /** A saved workflow run by name. */
50
- name?: string;
51
- /** The script's newest `log()` lines, oldest first. */
52
- logs: string[];
53
- /** More than 25 agents: the card warns, as Claude Code's "Large workflow". */
54
- large?: true;
55
- /** The run this one resumed. */
56
- resumedFrom?: string;
57
- /** The run goes on in its own runner session; this call returned at once. */
58
- background?: {
59
- sessionId: string;
60
- };
61
- /** `pause` held new agents back; running agents finish. Cleared by `resume`. */
62
- paused?: true;
63
- }
64
- /** The run in a `workflow` call's metadata; `null` when the metadata is not a run. */
65
- export declare function parseWorkflowRun(metadata: unknown): WorkflowRun | null;
66
- /** True for pi's `workflow` tool, under any of its spellings, and for no other name ending in `workflow`. */
67
- export declare function isWorkflowTool(name: string): boolean;
68
- /** The filter of the agent list, as Claude Code's `f` cycles it. */
69
- export type AgentFilter = 'all' | 'running' | 'failed' | 'done';
70
- export declare const AGENT_FILTERS: readonly AgentFilter[];
71
- /** Whether an agent belongs under a filter. `stopped` is listed under `all` only. */
72
- export declare function agentMatches(agent: WorkflowRunAgent, filter: AgentFilter): boolean;
73
- export interface AgentCounts {
74
- total: number;
75
- running: number;
76
- failed: number;
77
- done: number;
78
- }
79
- export declare function countAgents(agents: readonly WorkflowRunAgent[]): AgentCounts;
80
- /** One group of the progress list: a phase and its agents, or the agents started before any phase. */
81
- export interface AgentGroup {
82
- /** `null`: the agents that belong to no phase. They come first and have no header. */
83
- phase: WorkflowRunPhase | null;
84
- agents: WorkflowRunAgent[];
85
- }
86
- /**
87
- * The agents grouped under their phases, in the run's phase order. An agent whose
88
- * phase the list does not name gets a group of its own after the named ones, as a
89
- * `phase()` title with no `meta.phases` entry does in Claude Code.
90
- */
91
- export declare function groupAgents(run: Pick<WorkflowRun, 'phases' | 'agents'>): AgentGroup[];
92
- /** `12s`, `3m 04s`, `1h 05m`. Seconds and minutes are zero-padded so a ticking label keeps its width. */
93
- export declare function formatElapsed(ms: number): string;
94
- /** How long a run or an agent took, or has taken by `now`; undefined when its start is unknown or it never ran. */
95
- export declare function elapsedMs(item: {
96
- startedAt?: number;
97
- endedAt?: number;
98
- }, live: boolean, now: number): number | undefined;
99
- export type StructuredSummary = {
100
- kind: 'object';
101
- keys: number;
102
- pretty: string;
103
- } | {
104
- kind: 'array';
105
- items: number;
106
- pretty: string;
107
- } | {
108
- kind: 'value';
109
- pretty: string;
110
- } | {
111
- kind: 'text';
112
- pretty: string;
113
- };
114
- /** A `structured` agent's summary: the JSON it holds, or its text when it does not parse (a cut answer). */
115
- export declare function structuredSummary(summary: string): StructuredSummary;
116
- export type WorkflowResult = {
117
- kind: 'json';
118
- text: string;
119
- } | {
120
- kind: 'markdown';
121
- text: string;
122
- };
123
- /**
124
- * The script's return value in a finished call's output (`<workflow_result>…</workflow_result>`):
125
- * pretty JSON when it parses as an object or a list, else Markdown. Null when the output has no
126
- * result block (an older run, or a run that failed).
127
- */
128
- export declare function parseWorkflowResult(output: string): WorkflowResult | null;
6
+ export { AGENT_FILTERS, agentMatches, countAgents, elapsedMs, formatElapsed, groupAgents, isWorkflowTool, parseWorkflowResult, parseWorkflowRun, structuredSummary, type AgentCounts, type AgentFilter, type AgentGroup, type StructuredSummary, type WorkflowAgentStatus, type WorkflowPhaseStatus, type WorkflowResult, type WorkflowRun, type WorkflowRunAgent, type WorkflowRunPhase, type WorkflowRunStatus, } from '@kortix/sdk';
@@ -1,251 +1,6 @@
1
1
  /**
2
- * The `workflow` tool's part metadata, parsed defensively, and the pure helpers
3
- * its card draws from.
4
- *
5
- * The shape is `WorkflowRunMetadata` in `packages/api-contract/src/kortix-tool-card.ts`.
6
- * `@kortix/ui` does not depend on that package, so the types are mirrored here
7
- * and every field is checked. pi pushes it live with every change of the run,
8
- * and the final copy when the call returns.
2
+ * The `workflow` run document's parser and the helpers its card draws from.
3
+ * They live in `@kortix/sdk` (`core/turns/tools/workflow-run.ts`); this file
4
+ * keeps the old import path for the web card.
9
5
  */
10
- const RUN_STATUSES = new Set(['running', 'done', 'failed', 'stopped']);
11
- const PHASE_STATUSES = new Set(['pending', 'running', 'done', 'failed', 'stopped']);
12
- const AGENT_STATUSES = new Set(['starting', 'running', 'done', 'failed', 'stopped']);
13
- /** A run's rows stay a list a person can read; the rest is the session list's job. */
14
- const MAX_AGENTS = 200;
15
- /** The contract sends at most 5 `log()` lines; a longer list keeps the newest 5. */
16
- const MAX_LOGS = 5;
17
- const MAX_LOG_CHARS = 500;
18
- const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
19
- const text = (value) => typeof value === 'string' && value.trim() ? value.trim() : undefined;
20
- const time = (value) => typeof value === 'number' && Number.isFinite(value) ? value : undefined;
21
- const count = (value) => typeof value === 'number' && Number.isFinite(value) && value >= 1 ? Math.floor(value) : undefined;
22
- function phase(entry) {
23
- if (!isObject(entry))
24
- return undefined;
25
- const title = text(entry.title);
26
- return title && PHASE_STATUSES.has(entry.status)
27
- ? { title, status: entry.status }
28
- : undefined;
29
- }
30
- function agent(entry, index) {
31
- if (!isObject(entry) || !AGENT_STATUSES.has(entry.status))
32
- return undefined;
33
- const label = text(entry.label) ?? text(entry.agent);
34
- if (!label)
35
- return undefined;
36
- const name = text(entry.agent);
37
- const phaseTitle = text(entry.phase);
38
- const sessionId = text(entry.sessionId);
39
- const summary = text(entry.summary);
40
- const model = text(entry.model);
41
- const attempt = count(entry.attempt);
42
- const startedAt = time(entry.startedAt);
43
- const endedAt = time(entry.endedAt);
44
- return {
45
- id: text(entry.id) ?? `a${index + 1}`,
46
- label,
47
- ...(name ? { agent: name } : {}),
48
- ...(phaseTitle ? { phase: phaseTitle } : {}),
49
- status: entry.status,
50
- ...(sessionId ? { sessionId } : {}),
51
- ...(summary ? { summary } : {}),
52
- ...(entry.cached === true ? { cached: true } : {}),
53
- ...(model ? { model } : {}),
54
- ...(entry.structured === true ? { structured: true } : {}),
55
- ...(attempt !== undefined ? { attempt } : {}),
56
- ...(startedAt !== undefined ? { startedAt } : {}),
57
- ...(endedAt !== undefined ? { endedAt } : {}),
58
- };
59
- }
60
- function logs(value) {
61
- if (!Array.isArray(value))
62
- return [];
63
- return value
64
- .map((line) => text(line))
65
- .filter((line) => line !== undefined)
66
- .slice(-MAX_LOGS)
67
- .map((line) => line.slice(0, MAX_LOG_CHARS));
68
- }
69
- /** The run in a `workflow` call's metadata; `null` when the metadata is not a run. */
70
- export function parseWorkflowRun(metadata) {
71
- if (!isObject(metadata))
72
- return null;
73
- // A background call that handed its run to a runner may say `started`: the run runs.
74
- if (metadata.status === 'started')
75
- metadata = { ...metadata, status: 'running', agents: Array.isArray(metadata.agents) ? metadata.agents : [] };
76
- if (!isObject(metadata) || !RUN_STATUSES.has(metadata.status))
77
- return null;
78
- if (!Array.isArray(metadata.agents))
79
- return null;
80
- const agents = [];
81
- for (const [index, entry] of metadata.agents.slice(0, MAX_AGENTS).entries()) {
82
- const parsed = agent(entry, index);
83
- if (parsed)
84
- agents.push(parsed);
85
- }
86
- const phases = Array.isArray(metadata.phases)
87
- ? metadata.phases.map(phase).filter((p) => p !== undefined)
88
- : [];
89
- const startedAt = time(metadata.startedAt);
90
- const endedAt = time(metadata.endedAt);
91
- const error = text(metadata.error);
92
- const maxAgents = count(metadata.maxAgents);
93
- const name = text(metadata.name);
94
- const resumedFrom = text(metadata.resumedFrom);
95
- // A background run names its runner session as `background.sessionId`, or (a started call's
96
- // details) as `runnerSessionId` / `runner_session_id`.
97
- const backgroundSession = (isObject(metadata.background) ? text(metadata.background.sessionId) : undefined) ??
98
- text(metadata.runnerSessionId) ??
99
- text(metadata.runner_session_id);
100
- return {
101
- runId: text(metadata.runId) ?? '',
102
- title: text(metadata.title) ?? '',
103
- status: metadata.status,
104
- phases,
105
- agents,
106
- ...(startedAt !== undefined ? { startedAt } : {}),
107
- ...(endedAt !== undefined ? { endedAt } : {}),
108
- ...(error ? { error } : {}),
109
- ...(maxAgents !== undefined ? { maxAgents } : {}),
110
- ...(name ? { name } : {}),
111
- logs: logs(metadata.logs),
112
- ...(metadata.large === true ? { large: true } : {}),
113
- ...(resumedFrom ? { resumedFrom } : {}),
114
- ...(backgroundSession ? { background: { sessionId: backgroundSession } } : {}),
115
- ...(metadata.paused === true ? { paused: true } : {}),
116
- };
117
- }
118
- /** True for pi's `workflow` tool, under any of its spellings, and for no other name ending in `workflow`. */
119
- export function isWorkflowTool(name) {
120
- return name.replace(/^oc[-_]/, '') === 'workflow';
121
- }
122
- export const AGENT_FILTERS = ['all', 'running', 'failed', 'done'];
123
- /** Whether an agent belongs under a filter. `stopped` is listed under `all` only. */
124
- export function agentMatches(agent, filter) {
125
- switch (filter) {
126
- case 'all':
127
- return true;
128
- case 'running':
129
- return agent.status === 'starting' || agent.status === 'running';
130
- case 'failed':
131
- return agent.status === 'failed';
132
- case 'done':
133
- return agent.status === 'done';
134
- }
135
- }
136
- export function countAgents(agents) {
137
- const counts = { total: agents.length, running: 0, failed: 0, done: 0 };
138
- for (const agent of agents) {
139
- if (agentMatches(agent, 'running'))
140
- counts.running += 1;
141
- else if (agent.status === 'failed')
142
- counts.failed += 1;
143
- else if (agent.status === 'done')
144
- counts.done += 1;
145
- }
146
- return counts;
147
- }
148
- /**
149
- * The agents grouped under their phases, in the run's phase order. An agent whose
150
- * phase the list does not name gets a group of its own after the named ones, as a
151
- * `phase()` title with no `meta.phases` entry does in Claude Code.
152
- */
153
- export function groupAgents(run) {
154
- const loose = [];
155
- const byPhase = new Map();
156
- for (const phase of run.phases)
157
- if (!byPhase.has(phase.title))
158
- byPhase.set(phase.title, []);
159
- const extra = [];
160
- for (const agent of run.agents) {
161
- if (!agent.phase) {
162
- loose.push(agent);
163
- continue;
164
- }
165
- let list = byPhase.get(agent.phase);
166
- if (!list) {
167
- list = [];
168
- byPhase.set(agent.phase, list);
169
- extra.push(agent.phase);
170
- }
171
- list.push(agent);
172
- }
173
- const groups = loose.length > 0 ? [{ phase: null, agents: loose }] : [];
174
- const seen = new Set();
175
- for (const phase of run.phases) {
176
- if (seen.has(phase.title))
177
- continue;
178
- seen.add(phase.title);
179
- groups.push({ phase, agents: byPhase.get(phase.title) ?? [] });
180
- }
181
- for (const title of extra) {
182
- const agents = byPhase.get(title) ?? [];
183
- groups.push({ phase: { title, status: derivedStatus(agents) }, agents });
184
- }
185
- return groups;
186
- }
187
- function derivedStatus(agents) {
188
- if (agents.some((agent) => agentMatches(agent, 'running')))
189
- return 'running';
190
- if (agents.some((agent) => agent.status === 'failed'))
191
- return 'failed';
192
- return 'done';
193
- }
194
- // ── Time ─────────────────────────────────────────────────────────────────────
195
- /** `12s`, `3m 04s`, `1h 05m`. Seconds and minutes are zero-padded so a ticking label keeps its width. */
196
- export function formatElapsed(ms) {
197
- const seconds = Math.max(0, Math.floor(ms / 1000));
198
- if (seconds < 60)
199
- return `${seconds}s`;
200
- const minutes = Math.floor(seconds / 60);
201
- if (minutes < 60)
202
- return `${minutes}m ${String(seconds % 60).padStart(2, '0')}s`;
203
- return `${Math.floor(minutes / 60)}h ${String(minutes % 60).padStart(2, '0')}m`;
204
- }
205
- /** How long a run or an agent took, or has taken by `now`; undefined when its start is unknown or it never ran. */
206
- export function elapsedMs(item, live, now) {
207
- if (item.startedAt === undefined)
208
- return undefined;
209
- if (item.endedAt !== undefined)
210
- return Math.max(0, item.endedAt - item.startedAt);
211
- return live ? Math.max(0, now - item.startedAt) : undefined;
212
- }
213
- /** A `structured` agent's summary: the JSON it holds, or its text when it does not parse (a cut answer). */
214
- export function structuredSummary(summary) {
215
- try {
216
- const value = JSON.parse(summary);
217
- const pretty = JSON.stringify(value, null, 2);
218
- if (Array.isArray(value))
219
- return { kind: 'array', items: value.length, pretty };
220
- if (isObject(value))
221
- return { kind: 'object', keys: Object.keys(value).length, pretty };
222
- return { kind: 'value', pretty };
223
- }
224
- catch {
225
- return { kind: 'text', pretty: summary };
226
- }
227
- }
228
- /**
229
- * The script's return value in a finished call's output (`<workflow_result>…</workflow_result>`):
230
- * pretty JSON when it parses as an object or a list, else Markdown. Null when the output has no
231
- * result block (an older run, or a run that failed).
232
- */
233
- export function parseWorkflowResult(output) {
234
- const open = output.indexOf('<workflow_result>');
235
- if (open === -1)
236
- return null;
237
- const start = open + '<workflow_result>'.length;
238
- const close = output.indexOf('</workflow_result>', start);
239
- const body = (close === -1 ? output.slice(start) : output.slice(start, close)).trim();
240
- if (!body)
241
- return null;
242
- if (body.startsWith('{') || body.startsWith('[')) {
243
- try {
244
- return { kind: 'json', text: JSON.stringify(JSON.parse(body), null, 2) };
245
- }
246
- catch {
247
- // A cut or hand-written block: show it as text.
248
- }
249
- }
250
- return { kind: 'markdown', text: body };
251
- }
6
+ export { AGENT_FILTERS, agentMatches, countAgents, elapsedMs, formatElapsed, groupAgents, isWorkflowTool, parseWorkflowResult, parseWorkflowRun, structuredSummary, } from '@kortix/sdk';