@agentic-sdlc-kit/mod-sdk 0.0.0-stage → 0.9.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 (61) hide show
  1. package/LICENSE +21 -0
  2. package/dist/capabilities/analyzer.d.ts +13 -0
  3. package/dist/capabilities/analyzer.d.ts.map +1 -0
  4. package/dist/capabilities/analyzer.js +58 -0
  5. package/dist/capabilities/analyzer.js.map +1 -0
  6. package/dist/capabilities/index.d.ts +2 -0
  7. package/dist/capabilities/index.d.ts.map +1 -0
  8. package/dist/capabilities/index.js +3 -0
  9. package/dist/capabilities/index.js.map +1 -0
  10. package/dist/capabilities/manifest.d.ts +40 -0
  11. package/dist/capabilities/manifest.d.ts.map +1 -0
  12. package/dist/capabilities/manifest.js +35 -0
  13. package/dist/capabilities/manifest.js.map +1 -0
  14. package/dist/claude-code-shim.d.ts +22 -0
  15. package/dist/claude-code-shim.d.ts.map +1 -0
  16. package/dist/claude-code-shim.js +30 -0
  17. package/dist/claude-code-shim.js.map +1 -0
  18. package/dist/index.d.ts +5 -0
  19. package/dist/index.d.ts.map +1 -0
  20. package/dist/index.js +14 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/middleware/compose.d.ts +3 -0
  23. package/dist/middleware/compose.d.ts.map +1 -0
  24. package/dist/middleware/compose.js +28 -0
  25. package/dist/middleware/compose.js.map +1 -0
  26. package/dist/middleware/index.d.ts +2 -0
  27. package/dist/middleware/index.d.ts.map +1 -0
  28. package/dist/middleware/index.js +3 -0
  29. package/dist/middleware/index.js.map +1 -0
  30. package/dist/testing/index.d.ts +2 -0
  31. package/dist/testing/index.d.ts.map +1 -0
  32. package/dist/testing/index.js +3 -0
  33. package/dist/testing/index.js.map +1 -0
  34. package/dist/testing/mock-runtime.d.ts +96 -0
  35. package/dist/testing/mock-runtime.d.ts.map +1 -0
  36. package/dist/testing/mock-runtime.js +379 -0
  37. package/dist/testing/mock-runtime.js.map +1 -0
  38. package/dist/types/events.d.ts +158 -0
  39. package/dist/types/events.d.ts.map +1 -0
  40. package/dist/types/events.js +35 -0
  41. package/dist/types/events.js.map +1 -0
  42. package/dist/types/index.d.ts +2 -0
  43. package/dist/types/index.d.ts.map +1 -0
  44. package/dist/types/index.js +3 -0
  45. package/dist/types/index.js.map +1 -0
  46. package/package.json +37 -3
  47. package/src/capabilities/analyzer.test.ts +77 -0
  48. package/src/capabilities/analyzer.ts +68 -0
  49. package/src/capabilities/index.ts +2 -0
  50. package/src/capabilities/manifest.ts +38 -0
  51. package/src/claude-code-shim.ts +43 -0
  52. package/src/index.ts +14 -0
  53. package/src/middleware/compose.test.ts +70 -0
  54. package/src/middleware/compose.ts +29 -0
  55. package/src/middleware/index.ts +2 -0
  56. package/src/testing/index.ts +2 -0
  57. package/src/testing/mock-runtime.test.ts +264 -0
  58. package/src/testing/mock-runtime.ts +452 -0
  59. package/src/types/events.ts +275 -0
  60. package/src/types/index.ts +2 -0
  61. package/README.md +0 -3
@@ -0,0 +1,275 @@
1
+ // packages/mod-sdk/src/types/events.ts
2
+ //
3
+ // Typed event surface for Claude Code Mods. Name-maps to the official
4
+ // engine declarations (verified November 2026 against
5
+ // @anthropic-ai/claude-code 2.1.329 by probing `claude plugin validate`,
6
+ // whose type checker runs against the engine's own declarations — see
7
+ // plugins/<name>/.claude-plugin/types/claude-code/index.d.ts):
8
+ // - ModsApi ≡ the official EngineInterface (the `$` argument)
9
+ // - *Event ≡ the official *Input (ToolCallEvent ≡ ToolCallInput,
10
+ // AgentSpawnEvent ≡ AgentSpawnInput, ...)
11
+ // - ToolCallResult ≡ the official ToolCallResult<T>
12
+ //
13
+ // These shapes describe what the REAL host passes to a Mod:
14
+ // - tool.call: the tool's arguments are FIELDS of the event
15
+ // (e.command for Bash, e.file_path + e.content for Write, ...);
16
+ // tool.check carries them under e.input (untyped there officially).
17
+ // - turn.step streams: the hook is an async generator delegating with
18
+ // `const result = yield* next(e)`; the result carries the answering
19
+ // model under result.usage.model.
20
+ // - turn.start identifies the turn with e.turnId; turn.complete carries
21
+ // e.answer / e.durationMs / e.usage / e.isAborted / e.agentId.
22
+ // - $.clock.now / $.session.id / $.session.cwd / $.state.get are ASYNC
23
+ // on the official engine; state reads resolve to StateRead<T>, whose
24
+ // `.value` unwraps the stored value. MockRuntime mirrors all of this.
25
+ // - on(...) returns a registration whose .catch(handler) runs when the
26
+ // hook throws or times out (fail-closed error handling).
27
+ //
28
+ // The kit keeps its own export names above (ADR-0014: runtime-independent
29
+ // core); the plugins' hooks import the OFFICIAL names from 'claude-code'
30
+ // instead, vendored per plugin. When deployed in a real Claude Code
31
+ // environment, the engine lays its own declarations at
32
+ // .claude-plugin/types/claude-code/index.d.ts and they are the final
33
+ // authority.
34
+
35
+ // ─── Tool events ────────────────────────────────────────────────────────
36
+ export interface ToolCallEvent {
37
+ tool: string;
38
+ // Tool arguments are fields of the event itself. Common built-ins:
39
+ command?: string; // Bash
40
+ file_path?: string; // Edit / Write
41
+ content?: string; // Write
42
+ old_string?: string; // Edit
43
+ new_string?: string; // Edit
44
+ notebook_path?: string; // NotebookEdit
45
+ new_source?: string; // NotebookEdit
46
+ // Other tools' arguments appear as their own fields.
47
+ [key: string]: unknown;
48
+ }
49
+ export interface ToolCallResult {
50
+ // Returning { deny } cancels the call (pre-execution) or withholds the
51
+ // result (post-execution — the call already ran).
52
+ deny?: string;
53
+ // Returning { result } supplies a result without running the tool.
54
+ result?: unknown;
55
+ // Set on a failed result observed after `await next(e)`.
56
+ isError?: boolean;
57
+ // The text the model reads (tool results); redactable in place.
58
+ text?: string;
59
+ context?: string[];
60
+ [key: string]: unknown;
61
+ }
62
+ export interface ToolCheckEvent {
63
+ tool: string;
64
+ // For tool.check the tool's arguments live under e.input.
65
+ input: { command?: string; file_path?: string; [key: string]: unknown };
66
+ // True when the tool reports the call as read-only (absent otherwise).
67
+ isReadOnly?: boolean;
68
+ }
69
+ export interface ToolCheckResult {
70
+ decision: 'allow' | 'ask' | 'deny';
71
+ reason?: string;
72
+ }
73
+
74
+ // ─── Turn events ────────────────────────────────────────────────────────
75
+ export interface TurnUsage {
76
+ input_tokens?: number;
77
+ output_tokens?: number;
78
+ cache_read_input_tokens?: number;
79
+ cache_creation_input_tokens?: number;
80
+ // The model that actually answered (always present on the official
81
+ // engine; optional here only because queued test results may omit it).
82
+ model?: string;
83
+ }
84
+ export interface TurnStepEvent {
85
+ // The current model and effort; the Mod can rewrite these.
86
+ model?: string;
87
+ effort?: string;
88
+ // Set for a subagent's request.
89
+ agentId?: string;
90
+ // The prompt/request being sent to the model.
91
+ request?: unknown;
92
+ }
93
+ export interface TurnStepResult {
94
+ // The answering turn's usage (carries the model under usage.model on
95
+ // the official engine); the official engine may deliver null.
96
+ usage?: TurnUsage | null;
97
+ [key: string]: unknown;
98
+ }
99
+ export interface TurnStartEvent {
100
+ // Identifies the turn in the other turn events.
101
+ turnId: string;
102
+ }
103
+ export interface TurnCompleteEvent {
104
+ turnId?: string;
105
+ // Claude's final text.
106
+ answer: string;
107
+ durationMs: number;
108
+ usage?: TurnUsage;
109
+ isAborted?: boolean;
110
+ // Set for a subagent's turn.
111
+ agentId?: string;
112
+ }
113
+
114
+ // ─── Agent events ──────────────────────────────────────────────────────────
115
+ export interface AgentSpawnEvent {
116
+ name?: string;
117
+ subagentType?: string;
118
+ description?: string;
119
+ model?: string;
120
+ // True for an agent-team teammate.
121
+ isTeammate?: boolean;
122
+ // Set when a finished subagent is resumed; changes are IGNORED for
123
+ // resumed runs — a router must skip them.
124
+ resume?: boolean;
125
+ }
126
+
127
+ // ─── Session / command events ────────────────────────────────────────────
128
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type -- the host event carries no known fields
129
+ export interface SessionStartEvent {
130
+ // Fires once per loaded mod before the first prompt, and again on
131
+ // reload. Use $.session.id() for the session identity.
132
+ }
133
+ export interface SessionEndEvent {
134
+ reason?: 'clear' | 'resume' | 'logout' | 'prompt_input_exit' | 'other';
135
+ }
136
+ export interface CommandRunEvent {
137
+ command: string;
138
+ // The text typed after the command name (or '').
139
+ args?: string;
140
+ }
141
+
142
+ // ─── Event name → event type map ─────────────────────────────────────────
143
+ export interface EventMap {
144
+ 'tool.call': ToolCallEvent;
145
+ 'tool.check': ToolCheckEvent;
146
+ 'turn.start': TurnStartEvent;
147
+ 'turn.step': TurnStepEvent;
148
+ 'turn.complete': TurnCompleteEvent;
149
+ 'agent.spawn': AgentSpawnEvent;
150
+ 'session.start': SessionStartEvent;
151
+ 'session.end': SessionEndEvent;
152
+ 'command.run': CommandRunEvent;
153
+ }
154
+
155
+ export type EventName = keyof EventMap;
156
+ export type EventOf<N extends EventName> = EventMap[N];
157
+
158
+ // ─── Mods API ───────────────────────────────────────────────────────────
159
+ // The `$` argument passed to every handler: the mods API methods grouped in
160
+ // namespaces (official surface, subset used by this kit's mods).
161
+ export interface StateRef {
162
+ plugin: string;
163
+ key: string;
164
+ }
165
+
166
+ // What $.state.get resolves to on the official engine: the stored value
167
+ // (or undefined) behind `.value`. The official engine additionally types
168
+ // the ref against the merged PluginState; the kit's mirror keeps the ref
169
+ // permissive so the same hooks typecheck standalone.
170
+ export interface StateRead<T> {
171
+ value: T | undefined;
172
+ }
173
+
174
+ export interface ProcessResult {
175
+ stdout: string;
176
+ stderr: string;
177
+ exitCode: number;
178
+ }
179
+
180
+ export interface ModsApi {
181
+ ui: {
182
+ // Dim transcript line the model does NOT read.
183
+ log: (message: string) => void;
184
+ // Status line under the prompt.
185
+ status: (text: string) => void;
186
+ // Modal question; resolves to the chosen label string. Rejects when
187
+ // dismissed or nobody can answer (catch → treat as refuse). Time in
188
+ // ask does not count against the hook time budget.
189
+ ask: (question: string, options: ReadonlyArray<string>) => Promise<string>;
190
+ };
191
+ fs: {
192
+ // Paths resolve against the session working directory.
193
+ read: (path: string) => Promise<string>;
194
+ write: (path: string, content: string) => Promise<void>;
195
+ exists: (path: string) => Promise<boolean>;
196
+ list: (path: string) => Promise<string[]>;
197
+ };
198
+ // Argument array, NO shell.
199
+ process: {
200
+ run: (args: ReadonlyArray<string>) => Promise<ProcessResult>;
201
+ };
202
+ command: {
203
+ register: (cmd: { name: string; description: string; argumentHint?: string }) => Promise<void>;
204
+ };
205
+ session: {
206
+ id: () => Promise<string>;
207
+ cwd: () => Promise<string>;
208
+ };
209
+ clock: {
210
+ now: () => Promise<number>;
211
+ };
212
+ // State references use { plugin, key } literals so the host can list the
213
+ // values a module reads and writes. Reads resolve to StateRead<T>
214
+ // (unwrap with `.value`), matching the official engine.
215
+ state: {
216
+ get: <T>(ref: StateRef) => Promise<StateRead<T>>;
217
+ set: <T>(ref: StateRef, value: T) => Promise<void>;
218
+ };
219
+ // The host's HTTP fetch, gated by the `networkAccess` capability.
220
+ http: {
221
+ fetch: (url: string, init?: unknown) => Promise<unknown>;
222
+ };
223
+ }
224
+
225
+ // ─── Handler shape ───────────────────────────────────────────────────────
226
+ // A handler receives ($, e, next). It MUST return either:
227
+ // - a result object WITHOUT calling next — this answers the event,
228
+ // - the return value of next(e) or next(modifiedEvent) — observe/rewrite,
229
+ // - a Promise of either.
230
+ export type Handler<N extends EventName> = (
231
+ $: ModsApi,
232
+ e: EventOf<N>,
233
+ next: (e: EventOf<N>) => Promise<unknown> | unknown,
234
+ ) => Promise<unknown> | unknown;
235
+
236
+ // Streaming events (turn.step) take the generator form instead: the hook
237
+ // is an async generator that delegates with `yield* next(e)` (forwarding
238
+ // the response chunks) and returns the finished result.
239
+ export type StreamHandler<N extends EventName> = (
240
+ $: ModsApi,
241
+ e: EventOf<N>,
242
+ next: (e: EventOf<N>) => AsyncGenerator<unknown, unknown, unknown>,
243
+ ) => AsyncGenerator<unknown, unknown, unknown>;
244
+
245
+ // ─── Matchers and the `on` function ───────────────────────────────────────
246
+ // A matcher is an object whose fields are compared with the event's; each
247
+ // field can be a value, an array of allowed values, or a RegExp.
248
+ export interface Matcher {
249
+ tool?: string | ReadonlyArray<string> | RegExp;
250
+ command?: string;
251
+ isReadOnly?: boolean;
252
+ plugin?: string;
253
+ }
254
+
255
+ // on(...) returns a registration whose .catch(handler) sets the hook's
256
+ // error handler: it runs when the hook throws or times out, and its
257
+ // return value replaces the hook's contribution (fail-closed gating).
258
+ export interface Registration {
259
+ catch: (handler: (error: unknown) => unknown) => void;
260
+ }
261
+
262
+ export interface On {
263
+ <N extends EventName>(event: N, handler: Handler<N> | StreamHandler<N>): Registration;
264
+ <N extends EventName>(
265
+ event: N,
266
+ matcher: Matcher,
267
+ handler: Handler<N> | StreamHandler<N>,
268
+ ): Registration;
269
+ }
270
+
271
+ // The register function every Mod exports. The host calls it once on load
272
+ // with `on` and the userConfig `options`.
273
+ export interface RegisterFn {
274
+ (on: On, options?: Record<string, unknown>): void;
275
+ }
@@ -0,0 +1,2 @@
1
+ // packages/mod-sdk/src/types/index.ts
2
+ export * from './events.js';
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.