file.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. package opencode
  3. import (
  4. "context"
  5. "net/http"
  6. "net/url"
  7. "slices"
  8. "github.com/sst/opencode-sdk-go/internal/apijson"
  9. "github.com/sst/opencode-sdk-go/internal/apiquery"
  10. "github.com/sst/opencode-sdk-go/internal/param"
  11. "github.com/sst/opencode-sdk-go/internal/requestconfig"
  12. "github.com/sst/opencode-sdk-go/option"
  13. )
  14. // FileService contains methods and other services that help with interacting with
  15. // the opencode API.
  16. //
  17. // Note, unlike clients, this service does not read variables from the environment
  18. // automatically. You should not instantiate this service directly, and instead use
  19. // the [NewFileService] method instead.
  20. type FileService struct {
  21. Options []option.RequestOption
  22. }
  23. // NewFileService generates a new service that applies the given options to each
  24. // request. These options are applied after the parent client's options (if there
  25. // is one), and before any request-specific options.
  26. func NewFileService(opts ...option.RequestOption) (r *FileService) {
  27. r = &FileService{}
  28. r.Options = opts
  29. return
  30. }
  31. // List files and directories
  32. func (r *FileService) List(ctx context.Context, query FileListParams, opts ...option.RequestOption) (res *[]FileNode, err error) {
  33. opts = slices.Concat(r.Options, opts)
  34. path := "file"
  35. err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
  36. return
  37. }
  38. // Read a file
  39. func (r *FileService) Read(ctx context.Context, query FileReadParams, opts ...option.RequestOption) (res *FileReadResponse, err error) {
  40. opts = slices.Concat(r.Options, opts)
  41. path := "file/content"
  42. err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
  43. return
  44. }
  45. // Get file status
  46. func (r *FileService) Status(ctx context.Context, query FileStatusParams, opts ...option.RequestOption) (res *[]File, err error) {
  47. opts = slices.Concat(r.Options, opts)
  48. path := "file/status"
  49. err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
  50. return
  51. }
  52. type File struct {
  53. Added int64 `json:"added,required"`
  54. Path string `json:"path,required"`
  55. Removed int64 `json:"removed,required"`
  56. Status FileStatus `json:"status,required"`
  57. JSON fileJSON `json:"-"`
  58. }
  59. // fileJSON contains the JSON metadata for the struct [File]
  60. type fileJSON struct {
  61. Added apijson.Field
  62. Path apijson.Field
  63. Removed apijson.Field
  64. Status apijson.Field
  65. raw string
  66. ExtraFields map[string]apijson.Field
  67. }
  68. func (r *File) UnmarshalJSON(data []byte) (err error) {
  69. return apijson.UnmarshalRoot(data, r)
  70. }
  71. func (r fileJSON) RawJSON() string {
  72. return r.raw
  73. }
  74. type FileStatus string
  75. const (
  76. FileStatusAdded FileStatus = "added"
  77. FileStatusDeleted FileStatus = "deleted"
  78. FileStatusModified FileStatus = "modified"
  79. )
  80. func (r FileStatus) IsKnown() bool {
  81. switch r {
  82. case FileStatusAdded, FileStatusDeleted, FileStatusModified:
  83. return true
  84. }
  85. return false
  86. }
  87. type FileNode struct {
  88. Absolute string `json:"absolute,required"`
  89. Ignored bool `json:"ignored,required"`
  90. Name string `json:"name,required"`
  91. Path string `json:"path,required"`
  92. Type FileNodeType `json:"type,required"`
  93. JSON fileNodeJSON `json:"-"`
  94. }
  95. // fileNodeJSON contains the JSON metadata for the struct [FileNode]
  96. type fileNodeJSON struct {
  97. Absolute apijson.Field
  98. Ignored apijson.Field
  99. Name apijson.Field
  100. Path apijson.Field
  101. Type apijson.Field
  102. raw string
  103. ExtraFields map[string]apijson.Field
  104. }
  105. func (r *FileNode) UnmarshalJSON(data []byte) (err error) {
  106. return apijson.UnmarshalRoot(data, r)
  107. }
  108. func (r fileNodeJSON) RawJSON() string {
  109. return r.raw
  110. }
  111. type FileNodeType string
  112. const (
  113. FileNodeTypeFile FileNodeType = "file"
  114. FileNodeTypeDirectory FileNodeType = "directory"
  115. )
  116. func (r FileNodeType) IsKnown() bool {
  117. switch r {
  118. case FileNodeTypeFile, FileNodeTypeDirectory:
  119. return true
  120. }
  121. return false
  122. }
  123. type FileReadResponse struct {
  124. Content string `json:"content,required"`
  125. Diff string `json:"diff"`
  126. Patch FileReadResponsePatch `json:"patch"`
  127. JSON fileReadResponseJSON `json:"-"`
  128. }
  129. // fileReadResponseJSON contains the JSON metadata for the struct
  130. // [FileReadResponse]
  131. type fileReadResponseJSON struct {
  132. Content apijson.Field
  133. Diff apijson.Field
  134. Patch apijson.Field
  135. raw string
  136. ExtraFields map[string]apijson.Field
  137. }
  138. func (r *FileReadResponse) UnmarshalJSON(data []byte) (err error) {
  139. return apijson.UnmarshalRoot(data, r)
  140. }
  141. func (r fileReadResponseJSON) RawJSON() string {
  142. return r.raw
  143. }
  144. type FileReadResponsePatch struct {
  145. Hunks []FileReadResponsePatchHunk `json:"hunks,required"`
  146. NewFileName string `json:"newFileName,required"`
  147. OldFileName string `json:"oldFileName,required"`
  148. Index string `json:"index"`
  149. NewHeader string `json:"newHeader"`
  150. OldHeader string `json:"oldHeader"`
  151. JSON fileReadResponsePatchJSON `json:"-"`
  152. }
  153. // fileReadResponsePatchJSON contains the JSON metadata for the struct
  154. // [FileReadResponsePatch]
  155. type fileReadResponsePatchJSON struct {
  156. Hunks apijson.Field
  157. NewFileName apijson.Field
  158. OldFileName apijson.Field
  159. Index apijson.Field
  160. NewHeader apijson.Field
  161. OldHeader apijson.Field
  162. raw string
  163. ExtraFields map[string]apijson.Field
  164. }
  165. func (r *FileReadResponsePatch) UnmarshalJSON(data []byte) (err error) {
  166. return apijson.UnmarshalRoot(data, r)
  167. }
  168. func (r fileReadResponsePatchJSON) RawJSON() string {
  169. return r.raw
  170. }
  171. type FileReadResponsePatchHunk struct {
  172. Lines []string `json:"lines,required"`
  173. NewLines float64 `json:"newLines,required"`
  174. NewStart float64 `json:"newStart,required"`
  175. OldLines float64 `json:"oldLines,required"`
  176. OldStart float64 `json:"oldStart,required"`
  177. JSON fileReadResponsePatchHunkJSON `json:"-"`
  178. }
  179. // fileReadResponsePatchHunkJSON contains the JSON metadata for the struct
  180. // [FileReadResponsePatchHunk]
  181. type fileReadResponsePatchHunkJSON struct {
  182. Lines apijson.Field
  183. NewLines apijson.Field
  184. NewStart apijson.Field
  185. OldLines apijson.Field
  186. OldStart apijson.Field
  187. raw string
  188. ExtraFields map[string]apijson.Field
  189. }
  190. func (r *FileReadResponsePatchHunk) UnmarshalJSON(data []byte) (err error) {
  191. return apijson.UnmarshalRoot(data, r)
  192. }
  193. func (r fileReadResponsePatchHunkJSON) RawJSON() string {
  194. return r.raw
  195. }
  196. type FileListParams struct {
  197. Path param.Field[string] `query:"path,required"`
  198. Directory param.Field[string] `query:"directory"`
  199. }
  200. // URLQuery serializes [FileListParams]'s query parameters as `url.Values`.
  201. func (r FileListParams) URLQuery() (v url.Values) {
  202. return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
  203. ArrayFormat: apiquery.ArrayQueryFormatComma,
  204. NestedFormat: apiquery.NestedQueryFormatBrackets,
  205. })
  206. }
  207. type FileReadParams struct {
  208. Path param.Field[string] `query:"path,required"`
  209. Directory param.Field[string] `query:"directory"`
  210. }
  211. // URLQuery serializes [FileReadParams]'s query parameters as `url.Values`.
  212. func (r FileReadParams) URLQuery() (v url.Values) {
  213. return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
  214. ArrayFormat: apiquery.ArrayQueryFormatComma,
  215. NestedFormat: apiquery.NestedQueryFormatBrackets,
  216. })
  217. }
  218. type FileStatusParams struct {
  219. Directory param.Field[string] `query:"directory"`
  220. }
  221. // URLQuery serializes [FileStatusParams]'s query parameters as `url.Values`.
  222. func (r FileStatusParams) URLQuery() (v url.Values) {
  223. return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
  224. ArrayFormat: apiquery.ArrayQueryFormatComma,
  225. NestedFormat: apiquery.NestedQueryFormatBrackets,
  226. })
  227. }