@by-lua/lspec-subagents 1.0.1

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 (86) hide show
  1. package/CHANGELOG.md +482 -0
  2. package/LICENSE +21 -0
  3. package/README.md +123 -0
  4. package/dist/agent-manager.d.ts +108 -0
  5. package/dist/agent-manager.js +391 -0
  6. package/dist/agent-runner.d.ts +95 -0
  7. package/dist/agent-runner.js +377 -0
  8. package/dist/agent-types.d.ts +58 -0
  9. package/dist/agent-types.js +157 -0
  10. package/dist/context.d.ts +12 -0
  11. package/dist/context.js +56 -0
  12. package/dist/cross-extension-rpc.d.ts +46 -0
  13. package/dist/cross-extension-rpc.js +76 -0
  14. package/dist/custom-agents.d.ts +14 -0
  15. package/dist/custom-agents.js +127 -0
  16. package/dist/default-agents.d.ts +12 -0
  17. package/dist/default-agents.js +489 -0
  18. package/dist/env.d.ts +6 -0
  19. package/dist/env.js +28 -0
  20. package/dist/group-join.d.ts +32 -0
  21. package/dist/group-join.js +116 -0
  22. package/dist/index.d.ts +13 -0
  23. package/dist/index.js +1863 -0
  24. package/dist/invocation-config.d.ts +22 -0
  25. package/dist/invocation-config.js +15 -0
  26. package/dist/memory.d.ts +49 -0
  27. package/dist/memory.js +151 -0
  28. package/dist/model-config-loader.d.ts +58 -0
  29. package/dist/model-config-loader.js +157 -0
  30. package/dist/model-resolver.d.ts +19 -0
  31. package/dist/model-resolver.js +62 -0
  32. package/dist/output-file.d.ts +24 -0
  33. package/dist/output-file.js +86 -0
  34. package/dist/prompts.d.ts +29 -0
  35. package/dist/prompts.js +65 -0
  36. package/dist/schedule-store.d.ts +38 -0
  37. package/dist/schedule-store.js +155 -0
  38. package/dist/schedule.d.ts +109 -0
  39. package/dist/schedule.js +338 -0
  40. package/dist/settings.d.ts +66 -0
  41. package/dist/settings.js +130 -0
  42. package/dist/skill-loader.d.ts +24 -0
  43. package/dist/skill-loader.js +93 -0
  44. package/dist/types.d.ts +164 -0
  45. package/dist/types.js +8 -0
  46. package/dist/ui/agent-widget.d.ts +134 -0
  47. package/dist/ui/agent-widget.js +451 -0
  48. package/dist/ui/conversation-viewer.d.ts +35 -0
  49. package/dist/ui/conversation-viewer.js +252 -0
  50. package/dist/ui/schedule-menu.d.ts +16 -0
  51. package/dist/ui/schedule-menu.js +95 -0
  52. package/dist/usage.d.ts +50 -0
  53. package/dist/usage.js +49 -0
  54. package/dist/worktree.d.ts +36 -0
  55. package/dist/worktree.js +139 -0
  56. package/install.sh +77 -0
  57. package/lspec-model-config.example.json +17 -0
  58. package/package.json +50 -0
  59. package/src/agent-manager.ts +483 -0
  60. package/src/agent-runner.ts +486 -0
  61. package/src/agent-types.ts +188 -0
  62. package/src/context.ts +58 -0
  63. package/src/cross-extension-rpc.ts +122 -0
  64. package/src/custom-agents.ts +136 -0
  65. package/src/default-agents.ts +501 -0
  66. package/src/env.ts +33 -0
  67. package/src/group-join.ts +141 -0
  68. package/src/index.ts +2032 -0
  69. package/src/invocation-config.ts +40 -0
  70. package/src/memory.ts +165 -0
  71. package/src/model-config-loader.ts +193 -0
  72. package/src/model-resolver.ts +81 -0
  73. package/src/output-file.ts +96 -0
  74. package/src/prompts.ts +91 -0
  75. package/src/schedule-store.ts +153 -0
  76. package/src/schedule.ts +365 -0
  77. package/src/settings.ts +186 -0
  78. package/src/skill-loader.ts +102 -0
  79. package/src/types.ts +179 -0
  80. package/src/ui/agent-widget.ts +533 -0
  81. package/src/ui/conversation-viewer.ts +261 -0
  82. package/src/ui/schedule-menu.ts +104 -0
  83. package/src/usage.ts +60 -0
  84. package/src/worktree.ts +162 -0
  85. package/uninstall.sh +55 -0
  86. package/update.sh +64 -0
@@ -0,0 +1,164 @@
1
+ /**
2
+ * types.ts — Type definitions for the subagent system.
3
+ */
4
+ import type { ThinkingLevel } from "@mariozechner/pi-agent-core";
5
+ import type { AgentSession } from "@mariozechner/pi-coding-agent";
6
+ import type { LifetimeUsage } from "./usage.js";
7
+ export type { ThinkingLevel };
8
+ /** Agent type: any string name (built-in defaults or user-defined). */
9
+ export type SubagentType = string;
10
+ /** Names of the 9 embedded L-Spec default agents. */
11
+ export declare const DEFAULT_AGENT_NAMES: readonly ["orchestrator", "explorer", "librarian", "oracle", "designer", "fixer", "observer", "council", "councillor"];
12
+ /** Memory scope for persistent agent memory. */
13
+ export type MemoryScope = "user" | "project" | "local";
14
+ /** Isolation mode for agent execution. */
15
+ export type IsolationMode = "worktree";
16
+ /** Unified agent configuration — used for both default and user-defined agents. */
17
+ export interface AgentConfig {
18
+ name: string;
19
+ displayName?: string;
20
+ description: string;
21
+ builtinToolNames?: string[];
22
+ /** Tool denylist — these tools are removed even if `builtinToolNames` or extensions include them. */
23
+ disallowedTools?: string[];
24
+ /** true = inherit all, string[] = only listed, false = none */
25
+ extensions: true | string[] | false;
26
+ /** true = inherit all, string[] = only listed, false = none */
27
+ skills: true | string[] | false;
28
+ model?: string;
29
+ thinking?: ThinkingLevel;
30
+ maxTurns?: number;
31
+ systemPrompt: string;
32
+ promptMode: "replace" | "append";
33
+ /** Default for spawn: fork parent conversation. undefined = caller decides. */
34
+ inheritContext?: boolean;
35
+ /** Default for spawn: run in background. undefined = caller decides. */
36
+ runInBackground?: boolean;
37
+ /** Default for spawn: no extension tools. undefined = caller decides. */
38
+ isolated?: boolean;
39
+ /** Persistent memory scope — agents with memory get a persistent directory and MEMORY.md */
40
+ memory?: MemoryScope;
41
+ /** Isolation mode — "worktree" runs the agent in a temporary git worktree */
42
+ isolation?: IsolationMode;
43
+ /** true = this is an embedded default agent (informational) */
44
+ isDefault?: boolean;
45
+ /** false = agent is hidden from the registry */
46
+ enabled?: boolean;
47
+ /** Where this agent was loaded from */
48
+ source?: "default" | "project" | "global";
49
+ }
50
+ export type JoinMode = 'async' | 'group' | 'smart';
51
+ export interface AgentRecord {
52
+ id: string;
53
+ type: SubagentType;
54
+ description: string;
55
+ status: "queued" | "running" | "completed" | "steered" | "aborted" | "stopped" | "error";
56
+ result?: string;
57
+ error?: string;
58
+ toolUses: number;
59
+ startedAt: number;
60
+ completedAt?: number;
61
+ session?: AgentSession;
62
+ abortController?: AbortController;
63
+ promise?: Promise<string>;
64
+ groupId?: string;
65
+ joinMode?: JoinMode;
66
+ /** Set when result was already consumed via get_subagent_result — suppresses completion notification. */
67
+ resultConsumed?: boolean;
68
+ /** Steering messages queued before the session was ready. */
69
+ pendingSteers?: string[];
70
+ /** Worktree info if the agent is running in an isolated worktree. */
71
+ worktree?: {
72
+ path: string;
73
+ branch: string;
74
+ };
75
+ /** Worktree cleanup result after agent completion. */
76
+ worktreeResult?: {
77
+ hasChanges: boolean;
78
+ branch?: string;
79
+ };
80
+ /** The tool_use_id from the original Agent tool call. */
81
+ toolCallId?: string;
82
+ /** Path to the streaming output transcript file. */
83
+ outputFile?: string;
84
+ /** Cleanup function for the output file stream subscription. */
85
+ outputCleanup?: () => void;
86
+ /**
87
+ * Lifetime usage breakdown, accumulated via `message_end` events. Survives
88
+ * compaction. Total = input + output + cacheWrite (cacheRead deliberately
89
+ * excluded — see issue #38). Initialized to zeros at spawn.
90
+ */
91
+ lifetimeUsage: LifetimeUsage;
92
+ /** Number of times this agent's session has compacted. Initialized to 0 at spawn. */
93
+ compactionCount: number;
94
+ /** Resolved spawn params, captured for UI display. Fixed at spawn time. */
95
+ invocation?: AgentInvocation;
96
+ }
97
+ export interface AgentInvocation {
98
+ /** Short display name, e.g. "haiku" — only set when different from parent. */
99
+ modelName?: string;
100
+ thinking?: ThinkingLevel;
101
+ maxTurns?: number;
102
+ isolated?: boolean;
103
+ inheritContext?: boolean;
104
+ runInBackground?: boolean;
105
+ isolation?: IsolationMode;
106
+ }
107
+ /** Details attached to custom notification messages for visual rendering. */
108
+ export interface NotificationDetails {
109
+ id: string;
110
+ description: string;
111
+ status: string;
112
+ toolUses: number;
113
+ turnCount: number;
114
+ maxTurns?: number;
115
+ totalTokens: number;
116
+ durationMs: number;
117
+ outputFile?: string;
118
+ error?: string;
119
+ resultPreview: string;
120
+ /** Additional agents in a group notification. */
121
+ others?: NotificationDetails[];
122
+ }
123
+ export interface EnvInfo {
124
+ isGitRepo: boolean;
125
+ branch: string;
126
+ platform: string;
127
+ }
128
+ /**
129
+ * A subagent spawn registered to fire on a schedule.
130
+ *
131
+ * Stored at `<cwd>/.pi/subagent-schedules/<sessionId>.json`. Session-scoped:
132
+ * survives `/resume` but resets on `/new`, mirroring pi-chonky-tasks.
133
+ */
134
+ export interface ScheduledSubagent {
135
+ id: string;
136
+ /** Unique within store. Defaults to `description`. */
137
+ name: string;
138
+ description: string;
139
+ /** Raw user input — cron expr | "+10m" | ISO | "5m". */
140
+ schedule: string;
141
+ scheduleType: "cron" | "once" | "interval";
142
+ /** Computed at create time for interval/once. */
143
+ intervalMs?: number;
144
+ subagent_type: SubagentType;
145
+ prompt: string;
146
+ model?: string;
147
+ thinking?: ThinkingLevel;
148
+ max_turns?: number;
149
+ isolated?: boolean;
150
+ isolation?: IsolationMode;
151
+ enabled: boolean;
152
+ /** ISO timestamp. */
153
+ createdAt: string;
154
+ lastRun?: string;
155
+ lastStatus?: "success" | "error" | "running";
156
+ /** Refreshed on every fire and on store load. */
157
+ nextRun?: string;
158
+ runCount: number;
159
+ }
160
+ export interface ScheduleStoreData {
161
+ /** For future migrations. */
162
+ version: 1;
163
+ jobs: ScheduledSubagent[];
164
+ }
package/dist/types.js ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * types.ts — Type definitions for the subagent system.
3
+ */
4
+ /** Names of the 9 embedded L-Spec default agents. */
5
+ export const DEFAULT_AGENT_NAMES = [
6
+ "orchestrator", "explorer", "librarian", "oracle", "designer",
7
+ "fixer", "observer", "council", "councillor",
8
+ ];
@@ -0,0 +1,134 @@
1
+ /**
2
+ * agent-widget.ts — Persistent widget showing running/completed agents above the editor.
3
+ *
4
+ * Displays a tree of agents with animated spinners, live stats, and activity descriptions.
5
+ * Uses the callback form of setWidget for themed rendering.
6
+ */
7
+ import type { AgentManager } from "../agent-manager.js";
8
+ import type { AgentInvocation, SubagentType } from "../types.js";
9
+ import { type LifetimeUsage, type SessionLike } from "../usage.js";
10
+ /** Braille spinner frames for animated running indicator. */
11
+ export declare const SPINNER: string[];
12
+ /** Statuses that indicate an error/non-success outcome (used for linger behavior and icon rendering). */
13
+ export declare const ERROR_STATUSES: Set<string>;
14
+ export type Theme = {
15
+ fg(color: string, text: string): string;
16
+ bold(text: string): string;
17
+ };
18
+ export type UICtx = {
19
+ setStatus(key: string, text: string | undefined): void;
20
+ setWidget(key: string, content: undefined | ((tui: any, theme: Theme) => {
21
+ render(): string[];
22
+ invalidate(): void;
23
+ }), options?: {
24
+ placement?: "aboveEditor" | "belowEditor";
25
+ }): void;
26
+ };
27
+ /** Per-agent live activity state. */
28
+ export interface AgentActivity {
29
+ activeTools: Map<string, string>;
30
+ toolUses: number;
31
+ responseText: string;
32
+ session?: SessionLike;
33
+ /** Current turn count. */
34
+ turnCount: number;
35
+ /** Effective max turns for this agent (undefined = unlimited). */
36
+ maxTurns?: number;
37
+ /** Lifetime usage breakdown — see LifetimeUsage docs. */
38
+ lifetimeUsage: LifetimeUsage;
39
+ }
40
+ /** Metadata attached to Agent tool results for custom rendering. */
41
+ export interface AgentDetails {
42
+ displayName: string;
43
+ description: string;
44
+ subagentType: string;
45
+ toolUses: number;
46
+ tokens: string;
47
+ durationMs: number;
48
+ status: "queued" | "running" | "completed" | "steered" | "aborted" | "stopped" | "error" | "background";
49
+ /** Human-readable description of what the agent is currently doing. */
50
+ activity?: string;
51
+ /** Current spinner frame index (for animated running indicator). */
52
+ spinnerFrame?: number;
53
+ /** Short model name if different from parent (e.g. "haiku", "sonnet"). */
54
+ modelName?: string;
55
+ /** Notable config tags (e.g. ["thinking: high", "isolated"]). */
56
+ tags?: string[];
57
+ /** Current turn count. */
58
+ turnCount?: number;
59
+ /** Effective max turns (undefined = unlimited). */
60
+ maxTurns?: number;
61
+ agentId?: string;
62
+ error?: string;
63
+ }
64
+ /** Format a token count compactly: "33.8k token", "1.2M token". */
65
+ export declare function formatTokens(count: number): string;
66
+ /**
67
+ * Token count with optional context-fill % and compaction-count annotations.
68
+ * Thresholds for percent: <70% dim, 70–85% warning, ≥85% error.
69
+ * Compaction count rendered as `↻N` in dim.
70
+ *
71
+ * "12.3k token" — no annotations
72
+ * "12.3k token (45%)" — percent only
73
+ * "12.3k token (↻2)" — compactions only (e.g. right after compact)
74
+ * "12.3k token (45% · ↻2)" — both
75
+ */
76
+ export declare function formatSessionTokens(tokens: number, percent: number | null, theme: Theme, compactions?: number): string;
77
+ /** Format turn count with optional max limit: "⟳5≤30" or "⟳5". */
78
+ export declare function formatTurns(turnCount: number, maxTurns?: number | null): string;
79
+ /** Format milliseconds as human-readable duration. */
80
+ export declare function formatMs(ms: number): string;
81
+ /** Format duration from start/completed timestamps. */
82
+ export declare function formatDuration(startedAt: number, completedAt?: number): string;
83
+ /** Get display name for any agent type (built-in or custom). */
84
+ export declare function getDisplayName(type: SubagentType): string;
85
+ /** Short label for prompt mode: "twin" for append, nothing for replace (the default). */
86
+ export declare function getPromptModeLabel(type: SubagentType): string | undefined;
87
+ /** Mode label is not included — callers add it where they want it. */
88
+ export declare function buildInvocationTags(invocation: AgentInvocation | undefined): {
89
+ modelName?: string;
90
+ tags: string[];
91
+ };
92
+ /** Build a human-readable activity string from currently-running tools or response text. */
93
+ export declare function describeActivity(activeTools: Map<string, string>, responseText?: string): string;
94
+ export declare class AgentWidget {
95
+ private manager;
96
+ private agentActivity;
97
+ private uiCtx;
98
+ private widgetFrame;
99
+ private widgetInterval;
100
+ /** Tracks how many turns each finished agent has survived. Key: agent ID, Value: turns since finished. */
101
+ private finishedTurnAge;
102
+ /** How many extra turns errors/aborted agents linger (completed agents clear after 1 turn). */
103
+ private static readonly ERROR_LINGER_TURNS;
104
+ /** Whether the widget callback is currently registered with the TUI. */
105
+ private widgetRegistered;
106
+ /** Cached TUI reference from widget factory callback, used for requestRender(). */
107
+ private tui;
108
+ /** Last status bar text, used to avoid redundant setStatus calls. */
109
+ private lastStatusText;
110
+ constructor(manager: AgentManager, agentActivity: Map<string, AgentActivity>);
111
+ /** Set the UI context (grabbed from first tool execution). */
112
+ setUICtx(ctx: UICtx): void;
113
+ /**
114
+ * Called on each new turn (tool_execution_start).
115
+ * Ages finished agents and clears those that have lingered long enough.
116
+ */
117
+ onTurnStart(): void;
118
+ /** Ensure the widget update timer is running. */
119
+ ensureTimer(): void;
120
+ /** Check if a finished agent should still be shown in the widget. */
121
+ private shouldShowFinished;
122
+ /** Record an agent as finished (call when agent completes). */
123
+ markFinished(agentId: string): void;
124
+ /** Render a finished agent line. */
125
+ private renderFinishedLine;
126
+ /**
127
+ * Render the widget content. Called from the registered widget's render() callback,
128
+ * reading live state each time instead of capturing it in a closure.
129
+ */
130
+ private renderWidget;
131
+ /** Force an immediate widget update. */
132
+ update(): void;
133
+ dispose(): void;
134
+ }