file.go 7.7 KB

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