@reminix/sdk 0.8.2 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +10 -43
  3. package/client.d.mts +12 -6
  4. package/client.d.mts.map +1 -1
  5. package/client.d.ts +12 -6
  6. package/client.d.ts.map +1 -1
  7. package/client.js +9 -3
  8. package/client.js.map +1 -1
  9. package/client.mjs +9 -3
  10. package/client.mjs.map +1 -1
  11. package/core/streaming.d.mts +33 -0
  12. package/core/streaming.d.mts.map +1 -0
  13. package/core/streaming.d.ts +33 -0
  14. package/core/streaming.d.ts.map +1 -0
  15. package/core/streaming.js +263 -0
  16. package/core/streaming.js.map +1 -0
  17. package/core/streaming.mjs +258 -0
  18. package/core/streaming.mjs.map +1 -0
  19. package/index.d.mts +0 -1
  20. package/index.d.mts.map +1 -1
  21. package/index.d.ts +0 -1
  22. package/index.d.ts.map +1 -1
  23. package/index.js +1 -5
  24. package/index.js.map +1 -1
  25. package/index.mjs +0 -2
  26. package/index.mjs.map +1 -1
  27. package/internal/decoders/line.d.mts +17 -0
  28. package/internal/decoders/line.d.mts.map +1 -0
  29. package/internal/decoders/line.d.ts +17 -0
  30. package/internal/decoders/line.d.ts.map +1 -0
  31. package/internal/decoders/line.js +113 -0
  32. package/internal/decoders/line.js.map +1 -0
  33. package/internal/decoders/line.mjs +108 -0
  34. package/internal/decoders/line.mjs.map +1 -0
  35. package/internal/parse.d.mts.map +1 -1
  36. package/internal/parse.d.ts.map +1 -1
  37. package/internal/parse.js +10 -0
  38. package/internal/parse.js.map +1 -1
  39. package/internal/parse.mjs +10 -0
  40. package/internal/parse.mjs.map +1 -1
  41. package/internal/request-options.d.mts +2 -0
  42. package/internal/request-options.d.mts.map +1 -1
  43. package/internal/request-options.d.ts +2 -0
  44. package/internal/request-options.d.ts.map +1 -1
  45. package/internal/request-options.js.map +1 -1
  46. package/internal/request-options.mjs.map +1 -1
  47. package/package.json +1 -1
  48. package/resources/agents.d.mts +49 -40
  49. package/resources/agents.d.mts.map +1 -1
  50. package/resources/agents.d.ts +49 -40
  51. package/resources/agents.d.ts.map +1 -1
  52. package/resources/agents.js +7 -127
  53. package/resources/agents.js.map +1 -1
  54. package/resources/agents.mjs +7 -126
  55. package/resources/agents.mjs.map +1 -1
  56. package/resources/deployments.d.mts +4 -0
  57. package/resources/deployments.d.mts.map +1 -0
  58. package/resources/deployments.d.ts +4 -0
  59. package/resources/deployments.d.ts.map +1 -0
  60. package/resources/deployments.js +9 -0
  61. package/resources/deployments.js.map +1 -0
  62. package/resources/deployments.mjs +5 -0
  63. package/resources/deployments.mjs.map +1 -0
  64. package/resources/index.d.mts +4 -2
  65. package/resources/index.d.mts.map +1 -1
  66. package/resources/index.d.ts +4 -2
  67. package/resources/index.d.ts.map +1 -1
  68. package/resources/index.js +7 -3
  69. package/resources/index.js.map +1 -1
  70. package/resources/index.mjs +3 -1
  71. package/resources/index.mjs.map +1 -1
  72. package/resources/{project.d.mts → projects.d.mts} +31 -7
  73. package/resources/projects.d.mts.map +1 -0
  74. package/resources/{project.d.ts → projects.d.ts} +31 -7
  75. package/resources/projects.d.ts.map +1 -0
  76. package/resources/{project.js → projects.js} +8 -7
  77. package/resources/projects.js.map +1 -0
  78. package/resources/{project.mjs → projects.mjs} +6 -5
  79. package/resources/projects.mjs.map +1 -0
  80. package/resources/secrets.d.mts +4 -0
  81. package/resources/secrets.d.mts.map +1 -0
  82. package/resources/secrets.d.ts +4 -0
  83. package/resources/secrets.d.ts.map +1 -0
  84. package/resources/secrets.js +9 -0
  85. package/resources/secrets.js.map +1 -0
  86. package/resources/secrets.mjs +5 -0
  87. package/resources/secrets.mjs.map +1 -0
  88. package/src/client.ts +25 -5
  89. package/src/core/streaming.ts +315 -0
  90. package/src/index.ts +0 -4
  91. package/src/internal/decoders/line.ts +135 -0
  92. package/src/internal/parse.ts +14 -0
  93. package/src/internal/request-options.ts +2 -0
  94. package/src/resources/agents.ts +101 -67
  95. package/src/resources/deployments.ts +5 -0
  96. package/src/resources/index.ts +8 -1
  97. package/src/resources/{project.ts → projects.ts} +35 -7
  98. package/src/resources/secrets.ts +5 -0
  99. package/src/streaming.ts +2 -87
  100. package/src/version.ts +1 -1
  101. package/streaming.d.mts +1 -30
  102. package/streaming.d.mts.map +1 -1
  103. package/streaming.d.ts +1 -30
  104. package/streaming.d.ts.map +1 -1
  105. package/streaming.js +3 -80
  106. package/streaming.js.map +1 -1
  107. package/streaming.mjs +1 -78
  108. package/streaming.mjs.map +1 -1
  109. package/version.d.mts +1 -1
  110. package/version.d.ts +1 -1
  111. package/version.js +1 -1
  112. package/version.mjs +1 -1
  113. package/resources/project.d.mts.map +0 -1
  114. package/resources/project.d.ts.map +0 -1
  115. package/resources/project.js.map +0 -1
  116. package/resources/project.mjs.map +0 -1
@@ -1,23 +1,24 @@
1
1
  import { APIResource } from "../core/resource.mjs";
2
2
  import { APIPromise } from "../core/api-promise.mjs";
3
3
  import { RequestOptions } from "../internal/request-options.mjs";
4
- export declare class Project extends APIResource {
4
+ export declare class Projects extends APIResource {
5
5
  /**
6
- * Returns the project associated with the API key used for authentication.
6
+ * Returns the project associated with the API key or X-Project header.
7
7
  *
8
8
  * The project contains metadata about your organization's configuration,
9
9
  * including:
10
10
  *
11
11
  * - Project ID and organization ID
12
12
  * - Project name and slug
13
+ * - Organization details
13
14
  * - Creation and update timestamps
14
15
  *
15
16
  * This endpoint is useful for verifying your API key is valid and retrieving
16
17
  * project details.
17
18
  */
18
- retrieve(options?: RequestOptions): APIPromise<ProjectRetrieveResponse>;
19
+ retrieveCurrent(options?: RequestOptions): APIPromise<ProjectRetrieveCurrentResponse>;
19
20
  }
20
- export interface ProjectRetrieveResponse {
21
+ export interface ProjectRetrieveCurrentResponse {
21
22
  /**
22
23
  * Unique identifier for the project
23
24
  */
@@ -30,6 +31,10 @@ export interface ProjectRetrieveResponse {
30
31
  * Human-readable name of the project
31
32
  */
32
33
  name: string;
34
+ /**
35
+ * Organization that owns this project
36
+ */
37
+ organization: ProjectRetrieveCurrentResponse.Organization;
33
38
  /**
34
39
  * ID of the organization that owns this project
35
40
  */
@@ -43,7 +48,26 @@ export interface ProjectRetrieveResponse {
43
48
  */
44
49
  updatedAt: string;
45
50
  }
46
- export declare namespace Project {
47
- export { type ProjectRetrieveResponse as ProjectRetrieveResponse };
51
+ export declare namespace ProjectRetrieveCurrentResponse {
52
+ /**
53
+ * Organization that owns this project
54
+ */
55
+ interface Organization {
56
+ /**
57
+ * Unique identifier for the organization
58
+ */
59
+ id: string;
60
+ /**
61
+ * Human-readable name of the organization
62
+ */
63
+ name: string;
64
+ /**
65
+ * URL-friendly identifier for the organization
66
+ */
67
+ slug: string;
68
+ }
69
+ }
70
+ export declare namespace Projects {
71
+ export { type ProjectRetrieveCurrentResponse as ProjectRetrieveCurrentResponse };
48
72
  }
49
- //# sourceMappingURL=project.d.mts.map
73
+ //# sourceMappingURL=projects.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"projects.d.mts","sourceRoot":"","sources":["../src/resources/projects.ts"],"names":[],"mappings":"OAEO,EAAE,WAAW,EAAE;OACf,EAAE,UAAU,EAAE;OACd,EAAE,cAAc,EAAE;AAEzB,qBAAa,QAAS,SAAQ,WAAW;IACvC;;;;;;;;;;;;;OAaG;IACH,eAAe,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,UAAU,CAAC,8BAA8B,CAAC;CAGtF;AAED,MAAM,WAAW,8BAA8B;IAC7C;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IAEX;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,YAAY,EAAE,8BAA8B,CAAC,YAAY,CAAC;IAE1D;;OAEG;IACH,cAAc,EAAE,MAAM,CAAC;IAEvB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,yBAAiB,8BAA8B,CAAC;IAC9C;;OAEG;IACH,UAAiB,YAAY;QAC3B;;WAEG;QACH,EAAE,EAAE,MAAM,CAAC;QAEX;;WAEG;QACH,IAAI,EAAE,MAAM,CAAC;QAEb;;WAEG;QACH,IAAI,EAAE,MAAM,CAAC;KACd;CACF;AAED,MAAM,CAAC,OAAO,WAAW,QAAQ,CAAC;IAChC,OAAO,EAAE,KAAK,8BAA8B,IAAI,8BAA8B,EAAE,CAAC;CAClF"}
@@ -1,23 +1,24 @@
1
1
  import { APIResource } from "../core/resource.js";
2
2
  import { APIPromise } from "../core/api-promise.js";
3
3
  import { RequestOptions } from "../internal/request-options.js";
4
- export declare class Project extends APIResource {
4
+ export declare class Projects extends APIResource {
5
5
  /**
6
- * Returns the project associated with the API key used for authentication.
6
+ * Returns the project associated with the API key or X-Project header.
7
7
  *
8
8
  * The project contains metadata about your organization's configuration,
9
9
  * including:
10
10
  *
11
11
  * - Project ID and organization ID
12
12
  * - Project name and slug
13
+ * - Organization details
13
14
  * - Creation and update timestamps
14
15
  *
15
16
  * This endpoint is useful for verifying your API key is valid and retrieving
16
17
  * project details.
17
18
  */
18
- retrieve(options?: RequestOptions): APIPromise<ProjectRetrieveResponse>;
19
+ retrieveCurrent(options?: RequestOptions): APIPromise<ProjectRetrieveCurrentResponse>;
19
20
  }
20
- export interface ProjectRetrieveResponse {
21
+ export interface ProjectRetrieveCurrentResponse {
21
22
  /**
22
23
  * Unique identifier for the project
23
24
  */
@@ -30,6 +31,10 @@ export interface ProjectRetrieveResponse {
30
31
  * Human-readable name of the project
31
32
  */
32
33
  name: string;
34
+ /**
35
+ * Organization that owns this project
36
+ */
37
+ organization: ProjectRetrieveCurrentResponse.Organization;
33
38
  /**
34
39
  * ID of the organization that owns this project
35
40
  */
@@ -43,7 +48,26 @@ export interface ProjectRetrieveResponse {
43
48
  */
44
49
  updatedAt: string;
45
50
  }
46
- export declare namespace Project {
47
- export { type ProjectRetrieveResponse as ProjectRetrieveResponse };
51
+ export declare namespace ProjectRetrieveCurrentResponse {
52
+ /**
53
+ * Organization that owns this project
54
+ */
55
+ interface Organization {
56
+ /**
57
+ * Unique identifier for the organization
58
+ */
59
+ id: string;
60
+ /**
61
+ * Human-readable name of the organization
62
+ */
63
+ name: string;
64
+ /**
65
+ * URL-friendly identifier for the organization
66
+ */
67
+ slug: string;
68
+ }
69
+ }
70
+ export declare namespace Projects {
71
+ export { type ProjectRetrieveCurrentResponse as ProjectRetrieveCurrentResponse };
48
72
  }
49
- //# sourceMappingURL=project.d.ts.map
73
+ //# sourceMappingURL=projects.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"projects.d.ts","sourceRoot":"","sources":["../src/resources/projects.ts"],"names":[],"mappings":"OAEO,EAAE,WAAW,EAAE;OACf,EAAE,UAAU,EAAE;OACd,EAAE,cAAc,EAAE;AAEzB,qBAAa,QAAS,SAAQ,WAAW;IACvC;;;;;;;;;;;;;OAaG;IACH,eAAe,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,UAAU,CAAC,8BAA8B,CAAC;CAGtF;AAED,MAAM,WAAW,8BAA8B;IAC7C;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IAEX;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,YAAY,EAAE,8BAA8B,CAAC,YAAY,CAAC;IAE1D;;OAEG;IACH,cAAc,EAAE,MAAM,CAAC;IAEvB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,yBAAiB,8BAA8B,CAAC;IAC9C;;OAEG;IACH,UAAiB,YAAY;QAC3B;;WAEG;QACH,EAAE,EAAE,MAAM,CAAC;QAEX;;WAEG;QACH,IAAI,EAAE,MAAM,CAAC;QAEb;;WAEG;QACH,IAAI,EAAE,MAAM,CAAC;KACd;CACF;AAED,MAAM,CAAC,OAAO,WAAW,QAAQ,CAAC;IAChC,OAAO,EAAE,KAAK,8BAA8B,IAAI,8BAA8B,EAAE,CAAC;CAClF"}
@@ -1,25 +1,26 @@
1
1
  "use strict";
2
2
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.Project = void 0;
4
+ exports.Projects = void 0;
5
5
  const resource_1 = require("../core/resource.js");
6
- class Project extends resource_1.APIResource {
6
+ class Projects extends resource_1.APIResource {
7
7
  /**
8
- * Returns the project associated with the API key used for authentication.
8
+ * Returns the project associated with the API key or X-Project header.
9
9
  *
10
10
  * The project contains metadata about your organization's configuration,
11
11
  * including:
12
12
  *
13
13
  * - Project ID and organization ID
14
14
  * - Project name and slug
15
+ * - Organization details
15
16
  * - Creation and update timestamps
16
17
  *
17
18
  * This endpoint is useful for verifying your API key is valid and retrieving
18
19
  * project details.
19
20
  */
20
- retrieve(options) {
21
- return this._client.get('/project', options);
21
+ retrieveCurrent(options) {
22
+ return this._client.get('/projects/current', options);
22
23
  }
23
24
  }
24
- exports.Project = Project;
25
- //# sourceMappingURL=project.js.map
25
+ exports.Projects = Projects;
26
+ //# sourceMappingURL=projects.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"projects.js","sourceRoot":"","sources":["../src/resources/projects.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,kDAA+C;AAI/C,MAAa,QAAS,SAAQ,sBAAW;IACvC;;;;;;;;;;;;;OAaG;IACH,eAAe,CAAC,OAAwB;QACtC,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,EAAE,OAAO,CAAC,CAAC;IACxD,CAAC;CACF;AAlBD,4BAkBC"}
@@ -1,21 +1,22 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
  import { APIResource } from "../core/resource.mjs";
3
- export class Project extends APIResource {
3
+ export class Projects extends APIResource {
4
4
  /**
5
- * Returns the project associated with the API key used for authentication.
5
+ * Returns the project associated with the API key or X-Project header.
6
6
  *
7
7
  * The project contains metadata about your organization's configuration,
8
8
  * including:
9
9
  *
10
10
  * - Project ID and organization ID
11
11
  * - Project name and slug
12
+ * - Organization details
12
13
  * - Creation and update timestamps
13
14
  *
14
15
  * This endpoint is useful for verifying your API key is valid and retrieving
15
16
  * project details.
16
17
  */
17
- retrieve(options) {
18
- return this._client.get('/project', options);
18
+ retrieveCurrent(options) {
19
+ return this._client.get('/projects/current', options);
19
20
  }
20
21
  }
21
- //# sourceMappingURL=project.mjs.map
22
+ //# sourceMappingURL=projects.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"projects.mjs","sourceRoot":"","sources":["../src/resources/projects.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,WAAW,EAAE;AAItB,MAAM,OAAO,QAAS,SAAQ,WAAW;IACvC;;;;;;;;;;;;;OAaG;IACH,eAAe,CAAC,OAAwB;QACtC,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,mBAAmB,EAAE,OAAO,CAAC,CAAC;IACxD,CAAC;CACF"}
@@ -0,0 +1,4 @@
1
+ import { APIResource } from "../core/resource.mjs";
2
+ export declare class Secrets extends APIResource {
3
+ }
4
+ //# sourceMappingURL=secrets.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"secrets.d.mts","sourceRoot":"","sources":["../src/resources/secrets.ts"],"names":[],"mappings":"OAEO,EAAE,WAAW,EAAE;AAEtB,qBAAa,OAAQ,SAAQ,WAAW;CAAG"}
@@ -0,0 +1,4 @@
1
+ import { APIResource } from "../core/resource.js";
2
+ export declare class Secrets extends APIResource {
3
+ }
4
+ //# sourceMappingURL=secrets.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"secrets.d.ts","sourceRoot":"","sources":["../src/resources/secrets.ts"],"names":[],"mappings":"OAEO,EAAE,WAAW,EAAE;AAEtB,qBAAa,OAAQ,SAAQ,WAAW;CAAG"}
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Secrets = void 0;
5
+ const resource_1 = require("../core/resource.js");
6
+ class Secrets extends resource_1.APIResource {
7
+ }
8
+ exports.Secrets = Secrets;
9
+ //# sourceMappingURL=secrets.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"secrets.js","sourceRoot":"","sources":["../src/resources/secrets.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;AAEtF,kDAA+C;AAE/C,MAAa,OAAQ,SAAQ,sBAAW;CAAG;AAA3C,0BAA2C"}
@@ -0,0 +1,5 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+ import { APIResource } from "../core/resource.mjs";
3
+ export class Secrets extends APIResource {
4
+ }
5
+ //# sourceMappingURL=secrets.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"secrets.mjs","sourceRoot":"","sources":["../src/resources/secrets.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAE/E,EAAE,WAAW,EAAE;AAEtB,MAAM,OAAO,OAAQ,SAAQ,WAAW;CAAG"}
package/src/client.ts CHANGED
@@ -18,13 +18,20 @@ import * as API from './resources/index';
18
18
  import { APIPromise } from './core/api-promise';
19
19
  import {
20
20
  AgentChatParams,
21
+ AgentChatParamsNonStreaming,
22
+ AgentChatParamsStreaming,
21
23
  AgentChatResponse,
22
24
  AgentInvokeParams,
25
+ AgentInvokeParamsNonStreaming,
26
+ AgentInvokeParamsStreaming,
23
27
  AgentInvokeResponse,
24
28
  Agents,
25
29
  Context,
30
+ StreamChunk,
26
31
  } from './resources/agents';
27
- import { Project, ProjectRetrieveResponse } from './resources/project';
32
+ import { Deployments } from './resources/deployments';
33
+ import { ProjectRetrieveCurrentResponse, Projects } from './resources/projects';
34
+ import { Secrets } from './resources/secrets';
28
35
  import { type Fetch } from './internal/builtin-types';
29
36
  import { HeadersLike, NullableHeaders, buildHeaders } from './internal/headers';
30
37
  import { FinalRequestOptions, RequestOptions } from './internal/request-options';
@@ -40,7 +47,7 @@ import { isEmptyObj } from './internal/utils/values';
40
47
 
41
48
  export interface ClientOptions {
42
49
  /**
43
- * Project API key
50
+ * Project API key or Personal Access Token (PAT). When using PAT, include X-Project header.
44
51
  */
45
52
  apiKey?: string | undefined;
46
53
 
@@ -722,24 +729,37 @@ export class Reminix {
722
729
 
723
730
  static toFile = Uploads.toFile;
724
731
 
725
- project: API.Project = new API.Project(this);
732
+ projects: API.Projects = new API.Projects(this);
726
733
  agents: API.Agents = new API.Agents(this);
734
+ secrets: API.Secrets = new API.Secrets(this);
735
+ deployments: API.Deployments = new API.Deployments(this);
727
736
  }
728
737
 
729
- Reminix.Project = Project;
738
+ Reminix.Projects = Projects;
730
739
  Reminix.Agents = Agents;
740
+ Reminix.Secrets = Secrets;
741
+ Reminix.Deployments = Deployments;
731
742
 
732
743
  export declare namespace Reminix {
733
744
  export type RequestOptions = Opts.RequestOptions;
734
745
 
735
- export { Project as Project, type ProjectRetrieveResponse as ProjectRetrieveResponse };
746
+ export { Projects as Projects, type ProjectRetrieveCurrentResponse as ProjectRetrieveCurrentResponse };
736
747
 
737
748
  export {
738
749
  Agents as Agents,
739
750
  type Context as Context,
751
+ type StreamChunk as StreamChunk,
740
752
  type AgentChatResponse as AgentChatResponse,
741
753
  type AgentInvokeResponse as AgentInvokeResponse,
742
754
  type AgentChatParams as AgentChatParams,
755
+ type AgentChatParamsNonStreaming as AgentChatParamsNonStreaming,
756
+ type AgentChatParamsStreaming as AgentChatParamsStreaming,
743
757
  type AgentInvokeParams as AgentInvokeParams,
758
+ type AgentInvokeParamsNonStreaming as AgentInvokeParamsNonStreaming,
759
+ type AgentInvokeParamsStreaming as AgentInvokeParamsStreaming,
744
760
  };
761
+
762
+ export { Secrets as Secrets };
763
+
764
+ export { Deployments as Deployments };
745
765
  }
@@ -0,0 +1,315 @@
1
+ import { ReminixError } from './error';
2
+ import { type ReadableStream } from '../internal/shim-types';
3
+ import { makeReadableStream } from '../internal/shims';
4
+ import { findDoubleNewlineIndex, LineDecoder } from '../internal/decoders/line';
5
+ import { ReadableStreamToAsyncIterable } from '../internal/shims';
6
+ import { isAbortError } from '../internal/errors';
7
+ import { encodeUTF8 } from '../internal/utils/bytes';
8
+ import { loggerFor } from '../internal/utils/log';
9
+ import type { Reminix } from '../client';
10
+
11
+ type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;
12
+
13
+ export type ServerSentEvent = {
14
+ event: string | null;
15
+ data: string;
16
+ raw: string[];
17
+ };
18
+
19
+ export class Stream<Item> implements AsyncIterable<Item> {
20
+ controller: AbortController;
21
+ #client: Reminix | undefined;
22
+
23
+ constructor(
24
+ private iterator: () => AsyncIterator<Item>,
25
+ controller: AbortController,
26
+ client?: Reminix,
27
+ ) {
28
+ this.controller = controller;
29
+ this.#client = client;
30
+ }
31
+
32
+ static fromSSEResponse<Item>(
33
+ response: Response,
34
+ controller: AbortController,
35
+ client?: Reminix,
36
+ ): Stream<Item> {
37
+ let consumed = false;
38
+ const logger = client ? loggerFor(client) : console;
39
+
40
+ async function* iterator(): AsyncIterator<Item, any, undefined> {
41
+ if (consumed) {
42
+ throw new ReminixError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
43
+ }
44
+ consumed = true;
45
+ let done = false;
46
+ try {
47
+ for await (const sse of _iterSSEMessages(response, controller)) {
48
+ try {
49
+ yield JSON.parse(sse.data);
50
+ } catch (e) {
51
+ logger.error(`Could not parse message into JSON:`, sse.data);
52
+ logger.error(`From chunk:`, sse.raw);
53
+ throw e;
54
+ }
55
+ }
56
+ done = true;
57
+ } catch (e) {
58
+ // If the user calls `stream.controller.abort()`, we should exit without throwing.
59
+ if (isAbortError(e)) return;
60
+ throw e;
61
+ } finally {
62
+ // If the user `break`s, abort the ongoing request.
63
+ if (!done) controller.abort();
64
+ }
65
+ }
66
+
67
+ return new Stream(iterator, controller, client);
68
+ }
69
+
70
+ /**
71
+ * Generates a Stream from a newline-separated ReadableStream
72
+ * where each item is a JSON value.
73
+ */
74
+ static fromReadableStream<Item>(
75
+ readableStream: ReadableStream,
76
+ controller: AbortController,
77
+ client?: Reminix,
78
+ ): Stream<Item> {
79
+ let consumed = false;
80
+
81
+ async function* iterLines(): AsyncGenerator<string, void, unknown> {
82
+ const lineDecoder = new LineDecoder();
83
+
84
+ const iter = ReadableStreamToAsyncIterable<Bytes>(readableStream);
85
+ for await (const chunk of iter) {
86
+ for (const line of lineDecoder.decode(chunk)) {
87
+ yield line;
88
+ }
89
+ }
90
+
91
+ for (const line of lineDecoder.flush()) {
92
+ yield line;
93
+ }
94
+ }
95
+
96
+ async function* iterator(): AsyncIterator<Item, any, undefined> {
97
+ if (consumed) {
98
+ throw new ReminixError('Cannot iterate over a consumed stream, use `.tee()` to split the stream.');
99
+ }
100
+ consumed = true;
101
+ let done = false;
102
+ try {
103
+ for await (const line of iterLines()) {
104
+ if (done) continue;
105
+ if (line) yield JSON.parse(line);
106
+ }
107
+ done = true;
108
+ } catch (e) {
109
+ // If the user calls `stream.controller.abort()`, we should exit without throwing.
110
+ if (isAbortError(e)) return;
111
+ throw e;
112
+ } finally {
113
+ // If the user `break`s, abort the ongoing request.
114
+ if (!done) controller.abort();
115
+ }
116
+ }
117
+
118
+ return new Stream(iterator, controller, client);
119
+ }
120
+
121
+ [Symbol.asyncIterator](): AsyncIterator<Item> {
122
+ return this.iterator();
123
+ }
124
+
125
+ /**
126
+ * Splits the stream into two streams which can be
127
+ * independently read from at different speeds.
128
+ */
129
+ tee(): [Stream<Item>, Stream<Item>] {
130
+ const left: Array<Promise<IteratorResult<Item>>> = [];
131
+ const right: Array<Promise<IteratorResult<Item>>> = [];
132
+ const iterator = this.iterator();
133
+
134
+ const teeIterator = (queue: Array<Promise<IteratorResult<Item>>>): AsyncIterator<Item> => {
135
+ return {
136
+ next: () => {
137
+ if (queue.length === 0) {
138
+ const result = iterator.next();
139
+ left.push(result);
140
+ right.push(result);
141
+ }
142
+ return queue.shift()!;
143
+ },
144
+ };
145
+ };
146
+
147
+ return [
148
+ new Stream(() => teeIterator(left), this.controller, this.#client),
149
+ new Stream(() => teeIterator(right), this.controller, this.#client),
150
+ ];
151
+ }
152
+
153
+ /**
154
+ * Converts this stream to a newline-separated ReadableStream of
155
+ * JSON stringified values in the stream
156
+ * which can be turned back into a Stream with `Stream.fromReadableStream()`.
157
+ */
158
+ toReadableStream(): ReadableStream {
159
+ const self = this;
160
+ let iter: AsyncIterator<Item>;
161
+
162
+ return makeReadableStream({
163
+ async start() {
164
+ iter = self[Symbol.asyncIterator]();
165
+ },
166
+ async pull(ctrl: any) {
167
+ try {
168
+ const { value, done } = await iter.next();
169
+ if (done) return ctrl.close();
170
+
171
+ const bytes = encodeUTF8(JSON.stringify(value) + '\n');
172
+
173
+ ctrl.enqueue(bytes);
174
+ } catch (err) {
175
+ ctrl.error(err);
176
+ }
177
+ },
178
+ async cancel() {
179
+ await iter.return?.();
180
+ },
181
+ });
182
+ }
183
+ }
184
+
185
+ export async function* _iterSSEMessages(
186
+ response: Response,
187
+ controller: AbortController,
188
+ ): AsyncGenerator<ServerSentEvent, void, unknown> {
189
+ if (!response.body) {
190
+ controller.abort();
191
+ if (
192
+ typeof (globalThis as any).navigator !== 'undefined' &&
193
+ (globalThis as any).navigator.product === 'ReactNative'
194
+ ) {
195
+ throw new ReminixError(
196
+ `The default react-native fetch implementation does not support streaming. Please use expo/fetch: https://docs.expo.dev/versions/latest/sdk/expo/#expofetch-api`,
197
+ );
198
+ }
199
+ throw new ReminixError(`Attempted to iterate over a response with no body`);
200
+ }
201
+
202
+ const sseDecoder = new SSEDecoder();
203
+ const lineDecoder = new LineDecoder();
204
+
205
+ const iter = ReadableStreamToAsyncIterable<Bytes>(response.body);
206
+ for await (const sseChunk of iterSSEChunks(iter)) {
207
+ for (const line of lineDecoder.decode(sseChunk)) {
208
+ const sse = sseDecoder.decode(line);
209
+ if (sse) yield sse;
210
+ }
211
+ }
212
+
213
+ for (const line of lineDecoder.flush()) {
214
+ const sse = sseDecoder.decode(line);
215
+ if (sse) yield sse;
216
+ }
217
+ }
218
+
219
+ /**
220
+ * Given an async iterable iterator, iterates over it and yields full
221
+ * SSE chunks, i.e. yields when a double new-line is encountered.
222
+ */
223
+ async function* iterSSEChunks(iterator: AsyncIterableIterator<Bytes>): AsyncGenerator<Uint8Array> {
224
+ let data = new Uint8Array();
225
+
226
+ for await (const chunk of iterator) {
227
+ if (chunk == null) {
228
+ continue;
229
+ }
230
+
231
+ const binaryChunk =
232
+ chunk instanceof ArrayBuffer ? new Uint8Array(chunk)
233
+ : typeof chunk === 'string' ? encodeUTF8(chunk)
234
+ : chunk;
235
+
236
+ let newData = new Uint8Array(data.length + binaryChunk.length);
237
+ newData.set(data);
238
+ newData.set(binaryChunk, data.length);
239
+ data = newData;
240
+
241
+ let patternIndex;
242
+ while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
243
+ yield data.slice(0, patternIndex);
244
+ data = data.slice(patternIndex);
245
+ }
246
+ }
247
+
248
+ if (data.length > 0) {
249
+ yield data;
250
+ }
251
+ }
252
+
253
+ class SSEDecoder {
254
+ private data: string[];
255
+ private event: string | null;
256
+ private chunks: string[];
257
+
258
+ constructor() {
259
+ this.event = null;
260
+ this.data = [];
261
+ this.chunks = [];
262
+ }
263
+
264
+ decode(line: string) {
265
+ if (line.endsWith('\r')) {
266
+ line = line.substring(0, line.length - 1);
267
+ }
268
+
269
+ if (!line) {
270
+ // empty line and we didn't previously encounter any messages
271
+ if (!this.event && !this.data.length) return null;
272
+
273
+ const sse: ServerSentEvent = {
274
+ event: this.event,
275
+ data: this.data.join('\n'),
276
+ raw: this.chunks,
277
+ };
278
+
279
+ this.event = null;
280
+ this.data = [];
281
+ this.chunks = [];
282
+
283
+ return sse;
284
+ }
285
+
286
+ this.chunks.push(line);
287
+
288
+ if (line.startsWith(':')) {
289
+ return null;
290
+ }
291
+
292
+ let [fieldname, _, value] = partition(line, ':');
293
+
294
+ if (value.startsWith(' ')) {
295
+ value = value.substring(1);
296
+ }
297
+
298
+ if (fieldname === 'event') {
299
+ this.event = value;
300
+ } else if (fieldname === 'data') {
301
+ this.data.push(value);
302
+ }
303
+
304
+ return null;
305
+ }
306
+ }
307
+
308
+ function partition(str: string, delimiter: string): [string, string, string] {
309
+ const index = str.indexOf(delimiter);
310
+ if (index !== -1) {
311
+ return [str.substring(0, index), delimiter, str.substring(index + delimiter.length)];
312
+ }
313
+
314
+ return [str, '', ''];
315
+ }
package/src/index.ts CHANGED
@@ -20,7 +20,3 @@ export {
20
20
  PermissionDeniedError,
21
21
  UnprocessableEntityError,
22
22
  } from './core/error';
23
-
24
- // stainless-custom-start
25
- export { Stream, type StreamChunk } from './streaming';
26
- // stainless-custom-end