@ampcode/plugin 0.0.0-20260422002301-gff243b1 → 0.0.0-20260424002713-g01c9b5d

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 (2) hide show
  1. package/index.d.ts +43 -20
  2. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -57,7 +57,7 @@ declare module '@ampcode/plugin' {
57
57
  */
58
58
  on<E extends keyof PluginEventMap>(
59
59
  event: E,
60
- handler: (event: PluginEventMap[E], ctx: PluginEventContext) => PluginHandlerResult<E>,
60
+ handler: (event: PluginEventMap[E], ctx: PluginEventContext<E>) => PluginHandlerResult<E>,
61
61
  ): Subscription
62
62
 
63
63
  /**
@@ -338,8 +338,8 @@ declare module '@ampcode/plugin' {
338
338
  * Thread API for manipulating the current thread.
339
339
  */
340
340
  export interface PluginThread {
341
- /** Active thread ID when available in the current invocation context */
342
- id?: ThreadID
341
+ /** Active thread ID for the current invocation context */
342
+ id: ThreadID
343
343
 
344
344
  /**
345
345
  * Append a user message to the thread.
@@ -426,13 +426,9 @@ declare module '@ampcode/plugin' {
426
426
  }
427
427
 
428
428
  /**
429
- * Event payload for tool.call event.
430
- * This is a request that expects a response from the handler.
429
+ * A tool call.
431
430
  */
432
- export interface ToolCallEvent {
433
- /** The active thread for this tool invocation, when available */
434
- thread?: { id: ThreadID }
435
-
431
+ export interface ToolCall {
436
432
  /** Unique identifier for this tool use (e.g., "toolu_xxx") */
437
433
  toolUseID: string
438
434
 
@@ -443,6 +439,15 @@ declare module '@ampcode/plugin' {
443
439
  input: Record<string, unknown>
444
440
  }
445
441
 
442
+ /**
443
+ * Event payload for tool.call event.
444
+ * This is a request that expects a response from the handler.
445
+ */
446
+ export interface ToolCallEvent extends ToolCall {
447
+ /** The active thread for this tool invocation */
448
+ thread: { id: ThreadID }
449
+ }
450
+
446
451
  /**
447
452
  * Result returned from a tool.call handler.
448
453
  * Determines how the tool execution should proceed.
@@ -464,9 +469,9 @@ declare module '@ampcode/plugin' {
464
469
  | { action: 'error'; message: string }
465
470
 
466
471
  /**
467
- * Event payload for tool.result event.
472
+ * A terminal tool result.
468
473
  */
469
- export interface ToolResultEvent {
474
+ export interface ToolResult {
470
475
  /** Unique identifier for this tool use (e.g., "toolu_xxx") */
471
476
  toolUseID: string
472
477
 
@@ -486,6 +491,14 @@ declare module '@ampcode/plugin' {
486
491
  output?: unknown
487
492
  }
488
493
 
494
+ /**
495
+ * Event payload for tool.result event.
496
+ */
497
+ export interface ToolResultEvent extends ToolResult {
498
+ /** The active thread for this tool result */
499
+ thread: { id: ThreadID }
500
+ }
501
+
489
502
  /**
490
503
  * Result returned from a tool.result handler.
491
504
  * Allows modifying the tool result before it is sent back to the model.
@@ -513,6 +526,9 @@ declare module '@ampcode/plugin' {
513
526
  * Fired when a user submits a prompt (initial or reply).
514
527
  */
515
528
  export interface AgentStartEvent {
529
+ /** The active thread for this agent turn */
530
+ thread: { id: ThreadID }
531
+
516
532
  /** The user's prompt message */
517
533
  message: string
518
534
 
@@ -537,6 +553,9 @@ declare module '@ampcode/plugin' {
537
553
  * Fired when the agent finishes handling a user prompt.
538
554
  */
539
555
  export interface AgentEndEvent {
556
+ /** The active thread for this agent turn */
557
+ thread: { id: ThreadID }
558
+
540
559
  /** The user's prompt message that started this turn */
541
560
  message: string
542
561
 
@@ -581,9 +600,9 @@ declare module '@ampcode/plugin' {
581
600
  }
582
601
 
583
602
  /**
584
- * Context passed as the second argument to event handlers.
603
+ * Context shared by all plugin event handlers.
585
604
  */
586
- export interface PluginEventContext {
605
+ export interface PluginEventContextBase {
587
606
  /** Scoped logger for plugin output. Log messages are appended to the handler's trace span events. */
588
607
  logger: PluginLogger
589
608
 
@@ -599,13 +618,17 @@ declare module '@ampcode/plugin' {
599
618
  /** System capabilities */
600
619
  system: PluginSystem
601
620
 
602
- /** Thread manipulation API */
603
- thread: PluginThread
604
-
605
621
  /** The trace span ID for this handler invocation, if tracing is enabled */
606
622
  span?: SpanID
607
623
  }
608
624
 
625
+ /**
626
+ * Context passed as the second argument to event handlers.
627
+ * `session.start` may be emitted before a thread exists, while all other events are thread-scoped.
628
+ */
629
+ export type PluginEventContext<E extends keyof PluginEventMap> = PluginEventContextBase &
630
+ (E extends 'session.start' ? { thread?: PluginThread } : { thread: PluginThread })
631
+
609
632
  /**
610
633
  * Handler return type based on whether the event expects a response.
611
634
  * Request events (in PluginRequestResultMap) must return a result.
@@ -628,15 +651,15 @@ declare module '@ampcode/plugin' {
628
651
  * A tool call and its corresponding terminal tool result extracted from thread messages.
629
652
  */
630
653
  export interface ToolCallWithResult {
631
- call: ToolCallEvent
632
- result: ToolResultEvent
654
+ call: ToolCall
655
+ result: ToolResult
633
656
  }
634
657
 
635
658
  /**
636
659
  * Extracts the shell command from a Bash or shell_command tool call.
637
660
  * Returns null if the event is not a shell command tool call.
638
661
  */
639
- export type ShellCommandFromToolCall = (event: ToolCallEvent) => ShellCommand | null
662
+ export type ShellCommandFromToolCall = (event: ToolCall) => ShellCommand | null
640
663
 
641
664
  /**
642
665
  * Extracts paired tool calls and terminal tool results from a list of thread messages.
@@ -647,7 +670,7 @@ declare module '@ampcode/plugin' {
647
670
  * Returns an array of file URIs modified by a tool call, or null if the tool doesn't modify files.
648
671
  * Supports edit/create/apply_patch tools and sed in-place shell commands.
649
672
  */
650
- export type FilesModifiedByToolCall = (event: ToolCallEvent | ToolResultEvent) => URI[] | null
673
+ export type FilesModifiedByToolCall = (event: ToolCall | ToolResult) => URI[] | null
651
674
 
652
675
  /**
653
676
  * Options for registering a command.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ampcode/plugin",
3
- "version": "0.0.0-20260422002301-gff243b1",
3
+ "version": "0.0.0-20260424002713-g01c9b5d",
4
4
  "description": "Amp Plugin API",
5
5
  "homepage": "https://ampcode.com/manual/plugin-api",
6
6
  "author": {