sessionpermission.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. package opencode
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "reflect"
  10. "slices"
  11. "github.com/sst/opencode-sdk-go/internal/apijson"
  12. "github.com/sst/opencode-sdk-go/internal/apiquery"
  13. "github.com/sst/opencode-sdk-go/internal/param"
  14. "github.com/sst/opencode-sdk-go/internal/requestconfig"
  15. "github.com/sst/opencode-sdk-go/option"
  16. "github.com/sst/opencode-sdk-go/shared"
  17. "github.com/tidwall/gjson"
  18. )
  19. // SessionPermissionService contains methods and other services that help with
  20. // interacting with the opencode API.
  21. //
  22. // Note, unlike clients, this service does not read variables from the environment
  23. // automatically. You should not instantiate this service directly, and instead use
  24. // the [NewSessionPermissionService] method instead.
  25. type SessionPermissionService struct {
  26. Options []option.RequestOption
  27. }
  28. // NewSessionPermissionService generates a new service that applies the given
  29. // options to each request. These options are applied after the parent client's
  30. // options (if there is one), and before any request-specific options.
  31. func NewSessionPermissionService(opts ...option.RequestOption) (r *SessionPermissionService) {
  32. r = &SessionPermissionService{}
  33. r.Options = opts
  34. return
  35. }
  36. // Respond to a permission request
  37. func (r *SessionPermissionService) Respond(ctx context.Context, id string, permissionID string, params SessionPermissionRespondParams, opts ...option.RequestOption) (res *bool, err error) {
  38. opts = slices.Concat(r.Options, opts)
  39. if id == "" {
  40. err = errors.New("missing required id parameter")
  41. return
  42. }
  43. if permissionID == "" {
  44. err = errors.New("missing required permissionID parameter")
  45. return
  46. }
  47. path := fmt.Sprintf("session/%s/permissions/%s", id, permissionID)
  48. err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
  49. return
  50. }
  51. type Permission struct {
  52. ID string `json:"id,required"`
  53. MessageID string `json:"messageID,required"`
  54. Metadata map[string]interface{} `json:"metadata,required"`
  55. SessionID string `json:"sessionID,required"`
  56. Time PermissionTime `json:"time,required"`
  57. Title string `json:"title,required"`
  58. Type string `json:"type,required"`
  59. CallID string `json:"callID"`
  60. Pattern PermissionPatternUnion `json:"pattern"`
  61. JSON permissionJSON `json:"-"`
  62. }
  63. // permissionJSON contains the JSON metadata for the struct [Permission]
  64. type permissionJSON struct {
  65. ID apijson.Field
  66. MessageID apijson.Field
  67. Metadata apijson.Field
  68. SessionID apijson.Field
  69. Time apijson.Field
  70. Title apijson.Field
  71. Type apijson.Field
  72. CallID apijson.Field
  73. Pattern apijson.Field
  74. raw string
  75. ExtraFields map[string]apijson.Field
  76. }
  77. func (r *Permission) UnmarshalJSON(data []byte) (err error) {
  78. return apijson.UnmarshalRoot(data, r)
  79. }
  80. func (r permissionJSON) RawJSON() string {
  81. return r.raw
  82. }
  83. type PermissionTime struct {
  84. Created float64 `json:"created,required"`
  85. JSON permissionTimeJSON `json:"-"`
  86. }
  87. // permissionTimeJSON contains the JSON metadata for the struct [PermissionTime]
  88. type permissionTimeJSON struct {
  89. Created apijson.Field
  90. raw string
  91. ExtraFields map[string]apijson.Field
  92. }
  93. func (r *PermissionTime) UnmarshalJSON(data []byte) (err error) {
  94. return apijson.UnmarshalRoot(data, r)
  95. }
  96. func (r permissionTimeJSON) RawJSON() string {
  97. return r.raw
  98. }
  99. // Union satisfied by [shared.UnionString] or [PermissionPatternArray].
  100. type PermissionPatternUnion interface {
  101. ImplementsPermissionPatternUnion()
  102. }
  103. func init() {
  104. apijson.RegisterUnion(
  105. reflect.TypeOf((*PermissionPatternUnion)(nil)).Elem(),
  106. "",
  107. apijson.UnionVariant{
  108. TypeFilter: gjson.String,
  109. Type: reflect.TypeOf(shared.UnionString("")),
  110. },
  111. apijson.UnionVariant{
  112. TypeFilter: gjson.JSON,
  113. Type: reflect.TypeOf(PermissionPatternArray{}),
  114. },
  115. )
  116. }
  117. type PermissionPatternArray []string
  118. func (r PermissionPatternArray) ImplementsPermissionPatternUnion() {}
  119. type SessionPermissionRespondParams struct {
  120. Response param.Field[SessionPermissionRespondParamsResponse] `json:"response,required"`
  121. Directory param.Field[string] `query:"directory"`
  122. }
  123. func (r SessionPermissionRespondParams) MarshalJSON() (data []byte, err error) {
  124. return apijson.MarshalRoot(r)
  125. }
  126. // URLQuery serializes [SessionPermissionRespondParams]'s query parameters as
  127. // `url.Values`.
  128. func (r SessionPermissionRespondParams) URLQuery() (v url.Values) {
  129. return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
  130. ArrayFormat: apiquery.ArrayQueryFormatComma,
  131. NestedFormat: apiquery.NestedQueryFormatBrackets,
  132. })
  133. }
  134. type SessionPermissionRespondParamsResponse string
  135. const (
  136. SessionPermissionRespondParamsResponseOnce SessionPermissionRespondParamsResponse = "once"
  137. SessionPermissionRespondParamsResponseAlways SessionPermissionRespondParamsResponse = "always"
  138. SessionPermissionRespondParamsResponseReject SessionPermissionRespondParamsResponse = "reject"
  139. )
  140. func (r SessionPermissionRespondParamsResponse) IsKnown() bool {
  141. switch r {
  142. case SessionPermissionRespondParamsResponseOnce, SessionPermissionRespondParamsResponseAlways, SessionPermissionRespondParamsResponseReject:
  143. return true
  144. }
  145. return false
  146. }