file.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. Type FileReadResponseType `json:"type,required"`
  126. Diff string `json:"diff"`
  127. Encoding FileReadResponseEncoding `json:"encoding"`
  128. MimeType string `json:"mimeType"`
  129. Patch FileReadResponsePatch `json:"patch"`
  130. JSON fileReadResponseJSON `json:"-"`
  131. }
  132. // fileReadResponseJSON contains the JSON metadata for the struct
  133. // [FileReadResponse]
  134. type fileReadResponseJSON struct {
  135. Content apijson.Field
  136. Type apijson.Field
  137. Diff apijson.Field
  138. Encoding apijson.Field
  139. MimeType apijson.Field
  140. Patch apijson.Field
  141. raw string
  142. ExtraFields map[string]apijson.Field
  143. }
  144. func (r *FileReadResponse) UnmarshalJSON(data []byte) (err error) {
  145. return apijson.UnmarshalRoot(data, r)
  146. }
  147. func (r fileReadResponseJSON) RawJSON() string {
  148. return r.raw
  149. }
  150. type FileReadResponseType string
  151. const (
  152. FileReadResponseTypeText FileReadResponseType = "text"
  153. )
  154. func (r FileReadResponseType) IsKnown() bool {
  155. switch r {
  156. case FileReadResponseTypeText:
  157. return true
  158. }
  159. return false
  160. }
  161. type FileReadResponseEncoding string
  162. const (
  163. FileReadResponseEncodingBase64 FileReadResponseEncoding = "base64"
  164. )
  165. func (r FileReadResponseEncoding) IsKnown() bool {
  166. switch r {
  167. case FileReadResponseEncodingBase64:
  168. return true
  169. }
  170. return false
  171. }
  172. type FileReadResponsePatch struct {
  173. Hunks []FileReadResponsePatchHunk `json:"hunks,required"`
  174. NewFileName string `json:"newFileName,required"`
  175. OldFileName string `json:"oldFileName,required"`
  176. Index string `json:"index"`
  177. NewHeader string `json:"newHeader"`
  178. OldHeader string `json:"oldHeader"`
  179. JSON fileReadResponsePatchJSON `json:"-"`
  180. }
  181. // fileReadResponsePatchJSON contains the JSON metadata for the struct
  182. // [FileReadResponsePatch]
  183. type fileReadResponsePatchJSON struct {
  184. Hunks apijson.Field
  185. NewFileName apijson.Field
  186. OldFileName apijson.Field
  187. Index apijson.Field
  188. NewHeader apijson.Field
  189. OldHeader apijson.Field
  190. raw string
  191. ExtraFields map[string]apijson.Field
  192. }
  193. func (r *FileReadResponsePatch) UnmarshalJSON(data []byte) (err error) {
  194. return apijson.UnmarshalRoot(data, r)
  195. }
  196. func (r fileReadResponsePatchJSON) RawJSON() string {
  197. return r.raw
  198. }
  199. type FileReadResponsePatchHunk struct {
  200. Lines []string `json:"lines,required"`
  201. NewLines float64 `json:"newLines,required"`
  202. NewStart float64 `json:"newStart,required"`
  203. OldLines float64 `json:"oldLines,required"`
  204. OldStart float64 `json:"oldStart,required"`
  205. JSON fileReadResponsePatchHunkJSON `json:"-"`
  206. }
  207. // fileReadResponsePatchHunkJSON contains the JSON metadata for the struct
  208. // [FileReadResponsePatchHunk]
  209. type fileReadResponsePatchHunkJSON struct {
  210. Lines apijson.Field
  211. NewLines apijson.Field
  212. NewStart apijson.Field
  213. OldLines apijson.Field
  214. OldStart apijson.Field
  215. raw string
  216. ExtraFields map[string]apijson.Field
  217. }
  218. func (r *FileReadResponsePatchHunk) UnmarshalJSON(data []byte) (err error) {
  219. return apijson.UnmarshalRoot(data, r)
  220. }
  221. func (r fileReadResponsePatchHunkJSON) RawJSON() string {
  222. return r.raw
  223. }
  224. type FileListParams struct {
  225. Path param.Field[string] `query:"path,required"`
  226. Directory param.Field[string] `query:"directory"`
  227. }
  228. // URLQuery serializes [FileListParams]'s query parameters as `url.Values`.
  229. func (r FileListParams) URLQuery() (v url.Values) {
  230. return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
  231. ArrayFormat: apiquery.ArrayQueryFormatComma,
  232. NestedFormat: apiquery.NestedQueryFormatBrackets,
  233. })
  234. }
  235. type FileReadParams struct {
  236. Path param.Field[string] `query:"path,required"`
  237. Directory param.Field[string] `query:"directory"`
  238. }
  239. // URLQuery serializes [FileReadParams]'s query parameters as `url.Values`.
  240. func (r FileReadParams) URLQuery() (v url.Values) {
  241. return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
  242. ArrayFormat: apiquery.ArrayQueryFormatComma,
  243. NestedFormat: apiquery.NestedQueryFormatBrackets,
  244. })
  245. }
  246. type FileStatusParams struct {
  247. Directory param.Field[string] `query:"directory"`
  248. }
  249. // URLQuery serializes [FileStatusParams]'s query parameters as `url.Values`.
  250. func (r FileStatusParams) URLQuery() (v url.Values) {
  251. return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
  252. ArrayFormat: apiquery.ArrayQueryFormatComma,
  253. NestedFormat: apiquery.NestedQueryFormatBrackets,
  254. })
  255. }