@@ -16,6 +16,25 @@ param(
1616 [string ]$PlanPath
1717)
1818
19+ function Get-ConfigValue {
20+ param (
21+ [AllowNull ()][object ]$Object ,
22+ [Parameter (Mandatory = $true )][string ]$Key
23+ )
24+
25+ if ($null -eq $Object ) {
26+ return $null
27+ }
28+ if ($Object -is [System.Collections.IDictionary ]) {
29+ return $Object [$Key ]
30+ }
31+ $prop = $Object.PSObject.Properties [$Key ]
32+ if ($prop ) {
33+ return $prop.Value
34+ }
35+ return $null
36+ }
37+
1938$ErrorActionPreference = ' Stop'
2039$DefaultStart = ' <!-- SPECKIT START -->'
2140$DefaultEnd = ' <!-- SPECKIT END -->'
@@ -47,22 +66,30 @@ import json
4766import sys
4867try:
4968 import yaml
50- except Exception:
51- yaml = None
69+ except ImportError:
70+ print(
71+ "agent-context: PyYAML is required to parse extension config; cannot update context.",
72+ file=sys.stderr,
73+ )
74+ sys.exit(2)
5275
5376try:
5477 with open(sys.argv[1], "r", encoding="utf-8") as fh:
55- data = yaml.safe_load(fh) if yaml else {}
56- except Exception:
57- data = {}
78+ data = yaml.safe_load(fh)
79+ except Exception as exc:
80+ print(
81+ f"agent-context: unable to parse {sys.argv[1]} ({exc}); cannot update context.",
82+ file=sys.stderr,
83+ )
84+ sys.exit(2)
5885
5986if not isinstance(data, dict):
6087 data = {}
6188
6289print(json.dumps(data))
6390'@ $ExtConfig
6491 if ($LASTEXITCODE -eq 0 -and $jsonOut ) {
65- $Options = $jsonOut | ConvertFrom-Json - AsHashtable - ErrorAction Stop
92+ $Options = $jsonOut | ConvertFrom-Json - ErrorAction Stop
6693 }
6794 } catch {
6895 $Options = $null
@@ -75,21 +102,23 @@ print(json.dumps(data))
75102 }
76103}
77104
78- $ContextFile = $Options [ ' context_file' ]
105+ $ContextFile = Get-ConfigValue - Object $Options - Key ' context_file'
79106if (-not $ContextFile ) {
80107 Write-Host ' agent-context: context_file not set in extension config; nothing to do.'
81108 exit 0
82109}
83110
84111$MarkerStart = $DefaultStart
85112$MarkerEnd = $DefaultEnd
86- $cm = $Options [ ' context_markers' ]
113+ $cm = Get-ConfigValue - Object $Options - Key ' context_markers'
87114if ($cm ) {
88- if ($cm [' start' ] -is [string ] -and $cm [' start' ]) {
89- $MarkerStart = $cm [' start' ]
115+ $cmStart = Get-ConfigValue - Object $cm - Key ' start'
116+ if ($cmStart -is [string ] -and $cmStart ) {
117+ $MarkerStart = $cmStart
90118 }
91- if ($cm [' end' ] -is [string ] -and $cm [' end' ]) {
92- $MarkerEnd = $cm [' end' ]
119+ $cmEnd = Get-ConfigValue - Object $cm - Key ' end'
120+ if ($cmEnd -is [string ] -and $cmEnd ) {
121+ $MarkerEnd = $cmEnd
93122 }
94123}
95124
0 commit comments