@sourcegraph/amp-sdk 0.1.0-20251022202704-gd85048a → 0.1.0-20251126160117-g66ad0bc

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.
package/LICENSE.md CHANGED
@@ -1 +1 @@
1
- © Sourcegraph Inc. All rights reserved. Use of Amp is subject to Amp's [Terms of Service](https://ampcode.com/terms).
1
+ © Sourcegraph Inc. All rights reserved. Use of Amp is subject to Amp's [Terms of Service](https://ampcode.com/terms), or separate Amp terms that you have signed with Sourcegraph Inc.
package/README.md CHANGED
@@ -36,13 +36,11 @@ The Amp SDK enables a wide range of AI-powered applications:
36
36
 
37
37
  ### Installation
38
38
 
39
- Install the Amp SDK using `npm` or `yarn`:
40
-
41
39
  ```bash
42
- # npm
40
+ # Install the Amp SDK using npm
43
41
  npm install @sourcegraph/amp-sdk
44
42
 
45
- # yarn
43
+ # or yarn
46
44
  yarn add @sourcegraph/amp-sdk
47
45
  ```
48
46
 
@@ -397,3 +395,520 @@ for await (const message of execute({
397
395
  ```
398
396
 
399
397
  To find out more about Amp Toolboxes, see the [Toolboxes](https://ampcode.com/manual#toolboxes) section of the Amp documentation.
398
+
399
+ ## Functions
400
+
401
+ ### execute()
402
+
403
+ The main function for executing Amp CLI commands programmatically.
404
+
405
+ ```typescript
406
+ function execute(options: ExecuteOptions): AsyncIterable<StreamMessage>
407
+ ```
408
+
409
+ #### Parameters
410
+
411
+ - `options` ([`ExecuteOptions`](#executeoptions)) - Configuration for the execution
412
+
413
+ #### Returns
414
+
415
+ - `AsyncIterable<StreamMessage>` - Stream of messages from the Amp CLI
416
+
417
+ #### Example
418
+
419
+ ```typescript
420
+ import { execute } from '@sourcegraph/amp-sdk'
421
+
422
+ for await (const message of execute({
423
+ prompt: 'Analyze this codebase',
424
+ options: {
425
+ cwd: './my-project',
426
+ dangerouslyAllowAll: true,
427
+ },
428
+ })) {
429
+ if (message.type === 'assistant') {
430
+ console.log('Assistant:', message.message.content)
431
+ } else if (message.type === 'result') {
432
+ console.log('Final result:', message.result)
433
+ break
434
+ }
435
+ }
436
+ ```
437
+
438
+ ### createUserMessage()
439
+
440
+ Helper function to create properly formatted user input messages for streaming conversations.
441
+
442
+ ```typescript
443
+ function createUserMessage(text: string): UserInputMessage
444
+ ```
445
+
446
+ #### Parameters
447
+
448
+ - `text` (`string`) - The text content for the user message
449
+
450
+ #### Returns
451
+
452
+ - [`UserInputMessage`](#userinputmessage) - A formatted user input message
453
+
454
+ #### Example
455
+
456
+ ```typescript
457
+ import { createUserMessage } from '@sourcegraph/amp-sdk'
458
+
459
+ const message = createUserMessage('Analyze this code')
460
+ console.log(message)
461
+ // Output: { type: 'user', message: { role: 'user', content: [{ type: 'text', text: 'Analyze this code' }] } }
462
+ ```
463
+
464
+ ### createPermission()
465
+
466
+ Helper function to create permission objects for controlling tool usage.
467
+
468
+ ```typescript
469
+ function createPermission(
470
+ tool: string,
471
+ action: 'allow' | 'reject' | 'ask' | 'delegate',
472
+ options?: {
473
+ matches?: Record<string, PermissionMatchCondition>
474
+ context?: 'thread' | 'subagent'
475
+ to?: string
476
+ },
477
+ ): Permission
478
+ ```
479
+
480
+ #### Parameters
481
+
482
+ - `tool` (`string`) - The name of the tool to which this permission applies (supports glob patterns)
483
+ - `action` (`'allow' | 'reject' | 'ask' | 'delegate'`) - How Amp should proceed when matched
484
+ - `options` (`object`, optional) - Additional configuration for the permission
485
+ - `matches` ([`Record<string, PermissionMatchCondition>`](#permissionmatchcondition)) - Match conditions for tool arguments
486
+ - `context` (`'thread' | 'subagent'`) - Only apply this rule in specific context
487
+ - `to` (`string`) - Command to delegate to (required when action is `'delegate'`)
488
+
489
+ #### Returns
490
+
491
+ - [`Permission`](#permission) - A permission object that can be used in the permissions array
492
+
493
+ #### Examples
494
+
495
+ ```typescript
496
+ import { createPermission } from '@sourcegraph/amp-sdk'
497
+
498
+ // Allow all Bash commands
499
+ createPermission('Bash', 'allow')
500
+
501
+ // Allow specific git commands
502
+ createPermission('Bash', 'allow', {
503
+ matches: { cmd: 'git *' },
504
+ })
505
+
506
+ // Ask before allowing Read operations on sensitive paths
507
+ createPermission('Read', 'ask', {
508
+ matches: { path: '/etc/*' },
509
+ })
510
+
511
+ // Delegate web browsing to a custom command
512
+ createPermission('mcp__playwright__*', 'delegate', {
513
+ to: 'node browse.js',
514
+ })
515
+
516
+ // Only apply in subagent context
517
+ createPermission('Bash', 'reject', {
518
+ context: 'subagent',
519
+ })
520
+ ```
521
+
522
+ ## Types
523
+
524
+ ### ExecuteOptions
525
+
526
+ Configuration options for the `execute()` function.
527
+
528
+ ```typescript
529
+ interface ExecuteOptions {
530
+ prompt: string | AsyncIterable<UserInputMessage>
531
+ options?: AmpOptions
532
+ signal?: AbortSignal
533
+ }
534
+ ```
535
+
536
+ #### Properties
537
+
538
+ | Property | Type | Required | Description |
539
+ | --------- | ------------------------------------------- | -------- | -------------------------------------------------------------------------------------------- |
540
+ | `prompt` | `string \| AsyncIterable<UserInputMessage>` | Yes | The input prompt as a string or async iterable of user messages for multi-turn conversations |
541
+ | `options` | [`AmpOptions`](#ampoptions) | No | CLI configuration options |
542
+ | `signal` | `AbortSignal` | No | Signal for cancellation support |
543
+
544
+ ### AmpOptions
545
+
546
+ Configuration options that map to Amp CLI flags.
547
+
548
+ ```typescript
549
+ interface AmpOptions {
550
+ cwd?: string
551
+ dangerouslyAllowAll?: boolean
552
+ visibility?: 'public' | 'private' | 'workspace' | 'group'
553
+ settingsFile?: string
554
+ logLevel?: 'debug' | 'info' | 'warn' | 'error' | 'audit'
555
+ logFile?: string
556
+ mcpConfig?: string | MCPConfig
557
+ env?: Record<string, string>
558
+ continue?: boolean | string
559
+ toolbox?: string
560
+ permissions?: Permission[]
561
+ }
562
+ ```
563
+
564
+ #### Properties
565
+
566
+ | Property | Type | Default | Description |
567
+ | --------------------- | --------------------------------------------------- | --------------- | ------------------------------------------------------------------------ |
568
+ | `cwd` | `string` | `process.cwd()` | Current working directory for execution |
569
+ | `dangerouslyAllowAll` | `boolean` | `false` | Allow all tool usage without permission prompts |
570
+ | `visibility` | `'public' \| 'private' \| 'workspace' \| 'group'` | `'workspace'` | Thread visibility level |
571
+ | `settingsFile` | `string` | - | Path to custom settings file |
572
+ | `logLevel` | `'debug' \| 'info' \| 'warn' \| 'error' \| 'audit'` | `'info'` | Logging verbosity level |
573
+ | `logFile` | `string` | - | Path to write logs |
574
+ | `continue` | `boolean \| string` | `false` | Continue most recent thread (`true`) or specific thread by ID (`string`) |
575
+ | `mcpConfig` | `string \| MCPConfig` | - | MCP server configuration as JSON string, or config object |
576
+ | `env` | `Record<string, string>` | - | Additional environment variables |
577
+ | `toolbox` | `string` | - | Folder path with toolbox scripts |
578
+ | `permissions` | [`Permission[]`](#permission) | - | Permission rules for tool usage |
579
+
580
+ ## Message Types
581
+
582
+ The SDK streams various message types during execution. All messages implement the base `StreamMessage` type.
583
+
584
+ ### SystemMessage
585
+
586
+ Initial message containing session information and available tools.
587
+
588
+ ```typescript
589
+ interface SystemMessage {
590
+ type: 'system'
591
+ subtype: 'init'
592
+ session_id: string
593
+ cwd: string
594
+ tools: string[]
595
+ mcp_servers: Array<{
596
+ name: string
597
+ status: 'connected' | 'connecting' | 'connection-failed' | 'disabled'
598
+ }>
599
+ }
600
+ ```
601
+
602
+ #### Properties
603
+
604
+ | Property | Type | Description |
605
+ | ------------- | --------------------------------------- | -------------------------------------------- |
606
+ | `session_id` | `string` | Unique identifier for this execution session |
607
+ | `cwd` | `string` | Current working directory |
608
+ | `tools` | `string[]` | List of available tool names |
609
+ | `mcp_servers` | `Array<{name: string, status: string}>` | Status of MCP servers |
610
+
611
+ ### AssistantMessage
612
+
613
+ AI assistant responses with text content and tool usage.
614
+
615
+ ```typescript
616
+ interface AssistantMessage {
617
+ type: 'assistant'
618
+ session_id: string
619
+ message: {
620
+ id: string
621
+ type: 'message'
622
+ role: 'assistant'
623
+ model: string
624
+ content: Array<TextContent | ToolUseContent>
625
+ stop_reason: 'end_turn' | 'tool_use' | 'max_tokens' | null
626
+ stop_sequence: string | null
627
+ usage?: Usage
628
+ }
629
+ parent_tool_use_id: string | null
630
+ }
631
+ ```
632
+
633
+ #### Properties
634
+
635
+ | Property | Type | Description |
636
+ | -------------------- | ---------------- | ------------------------------------------------ |
637
+ | `session_id` | `string` | Unique identifier for this execution session |
638
+ | `message` | `object` | The assistant's message content |
639
+ | `parent_tool_use_id` | `string \| null` | ID of parent tool use if this is a tool response |
640
+
641
+ ### UserMessage
642
+
643
+ User input and tool results.
644
+
645
+ ```typescript
646
+ interface UserMessage {
647
+ type: 'user'
648
+ session_id: string
649
+ message: {
650
+ role: 'user'
651
+ content: Array<TextContent | ToolResultContent>
652
+ }
653
+ parent_tool_use_id: string | null
654
+ }
655
+ ```
656
+
657
+ #### Properties
658
+
659
+ | Property | Type | Description |
660
+ | -------------------- | ---------------- | ------------------------------------------------ |
661
+ | `session_id` | `string` | Unique identifier for this execution session |
662
+ | `message` | `object` | The user's message content |
663
+ | `parent_tool_use_id` | `string \| null` | ID of parent tool use if this is a tool response |
664
+
665
+ ### ResultMessage
666
+
667
+ Final successful execution result.
668
+
669
+ ```typescript
670
+ interface ResultMessage {
671
+ type: 'result'
672
+ subtype: 'success'
673
+ session_id: string
674
+ is_error: false
675
+ result: string
676
+ duration_ms: number
677
+ num_turns: number
678
+ usage?: Usage
679
+ permission_denials?: string[]
680
+ }
681
+ ```
682
+
683
+ #### Properties
684
+
685
+ | Property | Type | Description |
686
+ | -------------------- | ----------------- | -------------------------------------------- |
687
+ | `session_id` | `string` | Unique identifier for this execution session |
688
+ | `result` | `string` | The final result from the assistant |
689
+ | `duration_ms` | `number` | Total execution time in milliseconds |
690
+ | `num_turns` | `number` | Number of conversation turns |
691
+ | `usage` | [`Usage`](#usage) | Token usage information |
692
+ | `permission_denials` | `string[]` | List of permissions that were denied |
693
+
694
+ ### ErrorResultMessage
695
+
696
+ Final error result indicating execution failure.
697
+
698
+ ```typescript
699
+ interface ErrorResultMessage {
700
+ type: 'result'
701
+ subtype: 'error_during_execution' | 'error_max_turns'
702
+ session_id: string
703
+ is_error: true
704
+ error: string
705
+ duration_ms: number
706
+ num_turns: number
707
+ usage?: Usage
708
+ permission_denials?: string[]
709
+ }
710
+ ```
711
+
712
+ #### Properties
713
+
714
+ | Property | Type | Description |
715
+ | -------------------- | ----------------- | -------------------------------------------- |
716
+ | `session_id` | `string` | Unique identifier for this execution session |
717
+ | `error` | `string` | Error message describing what went wrong |
718
+ | `duration_ms` | `number` | Total execution time in milliseconds |
719
+ | `num_turns` | `number` | Number of conversation turns |
720
+ | `usage` | [`Usage`](#usage) | Token usage information |
721
+ | `permission_denials` | `string[]` | List of permissions that were denied |
722
+
723
+ ### TextContent
724
+
725
+ Plain text content block.
726
+
727
+ ```typescript
728
+ interface TextContent {
729
+ type: 'text'
730
+ text: string
731
+ }
732
+ ```
733
+
734
+ ### ToolUseContent
735
+
736
+ Tool execution request.
737
+
738
+ ```typescript
739
+ interface ToolUseContent {
740
+ type: 'tool_use'
741
+ id: string
742
+ name: string
743
+ input: Record<string, unknown>
744
+ }
745
+ ```
746
+
747
+ ### ToolResultContent
748
+
749
+ Result from tool execution.
750
+
751
+ ```typescript
752
+ interface ToolResultContent {
753
+ type: 'tool_result'
754
+ tool_use_id: string
755
+ content: string
756
+ is_error: boolean
757
+ }
758
+ ```
759
+
760
+ ### Usage
761
+
762
+ Token usage and billing information from API calls.
763
+
764
+ ```typescript
765
+ interface Usage {
766
+ input_tokens: number
767
+ cache_creation_input_tokens?: number
768
+ cache_read_input_tokens?: number
769
+ output_tokens: number
770
+ service_tier?: string
771
+ }
772
+ ```
773
+
774
+ #### Properties
775
+
776
+ | Property | Type | Description |
777
+ | ----------------------------- | -------- | ---------------------------------- |
778
+ | `input_tokens` | `number` | Number of input tokens used |
779
+ | `cache_creation_input_tokens` | `number` | Tokens used for cache creation |
780
+ | `cache_read_input_tokens` | `number` | Tokens read from cache |
781
+ | `output_tokens` | `number` | Number of output tokens generated |
782
+ | `service_tier` | `string` | Service tier used for this request |
783
+
784
+ ## Input Types
785
+
786
+ ### UserInputMessage
787
+
788
+ Formatted user input message for streaming conversations.
789
+
790
+ ```typescript
791
+ interface UserInputMessage {
792
+ type: 'user'
793
+ message: {
794
+ role: 'user'
795
+ content: Array<{
796
+ type: 'text'
797
+ text: string
798
+ }>
799
+ }
800
+ }
801
+ ```
802
+
803
+ ### MCPConfig
804
+
805
+ Configuration for MCP (Model Context Protocol) servers.
806
+
807
+ ```typescript
808
+ type MCPConfig = Record<string, MCPServer>
809
+
810
+ interface MCPServer {
811
+ command: string
812
+ args?: string[]
813
+ env?: Record<string, string>
814
+ disabled?: boolean
815
+ }
816
+ ```
817
+
818
+ #### Properties
819
+
820
+ | Property | Type | Required | Description |
821
+ | ---------- | ------------------------ | -------- | ------------------------------------ |
822
+ | `command` | `string` | Yes | Command to start the MCP server |
823
+ | `args` | `string[]` | No | Command line arguments |
824
+ | `env` | `Record<string, string>` | No | Environment variables for the server |
825
+ | `disabled` | `boolean` | No | Whether this server is disabled |
826
+
827
+ ### Permission
828
+
829
+ Individual permission rule for controlling tool usage.
830
+
831
+ ```typescript
832
+ interface Permission {
833
+ tool: string
834
+ matches?: Record<string, PermissionMatchCondition>
835
+ action: 'allow' | 'reject' | 'ask' | 'delegate'
836
+ context?: 'thread' | 'subagent'
837
+ to?: string
838
+ }
839
+ ```
840
+
841
+ #### Properties
842
+
843
+ | Property | Type | Required | Description |
844
+ | --------- | -------------------------------------------- | -------- | ----------------------------------------------------------- |
845
+ | `tool` | `string` | Yes | Tool name (supports glob patterns like `Bash` or `mcp__*`) |
846
+ | `matches` | `Record<string, PermissionMatchCondition>` | No | Match conditions for tool arguments |
847
+ | `action` | `'allow' \| 'reject' \| 'ask' \| 'delegate'` | Yes | How Amp should proceed when the rule matches |
848
+ | `context` | `'thread' \| 'subagent'` | No | Apply rule only in main thread or sub-agents |
849
+ | `to` | `string` | No | Command to delegate to (required when action is `delegate`) |
850
+
851
+ #### Example
852
+
853
+ ```typescript
854
+ import { execute, createPermission } from '@sourcegraph/amp-sdk'
855
+
856
+ for await (const message of execute({
857
+ prompt: 'Deploy the application',
858
+ options: {
859
+ permissions: [
860
+ // Allow git commands
861
+ createPermission('Bash', 'allow', { matches: { cmd: 'git *' } }),
862
+ // Allow reading files
863
+ createPermission('Read', 'allow'),
864
+ ],
865
+ },
866
+ })) {
867
+ // Handle messages
868
+ }
869
+ ```
870
+
871
+ ### PermissionMatchCondition
872
+
873
+ Match condition for tool arguments. Supports strings (with glob patterns or regex), arrays (OR logic), booleans, numbers, null, undefined, and nested objects.
874
+
875
+ ```typescript
876
+ type PermissionMatchCondition =
877
+ | string
878
+ | PermissionMatchCondition[]
879
+ | { [key: string]: PermissionMatchCondition }
880
+ | boolean
881
+ | number
882
+ | null
883
+ | undefined
884
+ ```
885
+
886
+ #### Examples
887
+
888
+ ```typescript
889
+ // String pattern with wildcard
890
+ {
891
+ cmd: 'npm *'
892
+ }
893
+
894
+ // Array for OR logic
895
+ {
896
+ cmd: ['npm install', 'npm test', 'npm run build']
897
+ }
898
+
899
+ // Regex pattern
900
+ {
901
+ cmd: '/^git (status|log|diff)$/'
902
+ }
903
+
904
+ // Nested object matching
905
+ {
906
+ env: {
907
+ NODE_ENV: 'production'
908
+ }
909
+ }
910
+ ```
911
+
912
+ ## Requirements
913
+
914
+ - Node.js 18 or higher
package/dist/index.js CHANGED
@@ -4154,7 +4154,7 @@ function buildEnvironmentVariables(options) {
4154
4154
  if (options.env) {
4155
4155
  Object.assign(env, options.env);
4156
4156
  }
4157
- env.AMP_SDK_VERSION = "0.1.0-20251022202704-gd85048a";
4157
+ env.AMP_SDK_VERSION = "0.1.0-20251126160117-g66ad0bc";
4158
4158
  return env;
4159
4159
  }
4160
4160
  function spawnAmpCli(args, options) {
@@ -4355,4 +4355,4 @@ export {
4355
4355
  AmpOptionsSchema
4356
4356
  };
4357
4357
 
4358
- //# debugId=55BAB45A56267D2564756E2164756E21
4358
+ //# debugId=B95AEF07534E4E7264756E2164756E21
package/package.json CHANGED
@@ -34,7 +34,7 @@
34
34
  "sdk",
35
35
  "automation"
36
36
  ],
37
- "license": "SEE LICENSE IN LICENSE.md",
37
+ "license": "Sourcegraph Commercial License",
38
38
  "main": "dist/index.js",
39
39
  "name": "@sourcegraph/amp-sdk",
40
40
  "repository": {
@@ -44,7 +44,7 @@
44
44
  },
45
45
  "type": "module",
46
46
  "types": "dist/index.d.ts",
47
- "version": "0.1.0-20251022202704-gd85048a",
47
+ "version": "0.1.0-20251126160117-g66ad0bc",
48
48
  "scripts": {
49
49
  "build": "bun run scripts/build.ts",
50
50
  "dev": "pnpm exec tsc -b --watch",
@@ -52,6 +52,7 @@
52
52
  "examples:basic": "pnpm run build && bun run examples/basic.ts",
53
53
  "test": "vitest run",
54
54
  "test:watch": "vitest",
55
+ "test:integration": "vitest run --config vitest.integration.config.js",
55
56
  "pin-cli-version": "bun run scripts/pin-cli-version.ts"
56
57
  }
57
58
  }