fetch_hive 0.2.2

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 (28) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +64 -0
  3. data/lib/fetch_hive/client.rb +160 -0
  4. data/lib/fetch_hive/generated/api/agents_api.rb +90 -0
  5. data/lib/fetch_hive/generated/api/prompts_api.rb +90 -0
  6. data/lib/fetch_hive/generated/api/workflows_api.rb +90 -0
  7. data/lib/fetch_hive/generated/api_client.rb +441 -0
  8. data/lib/fetch_hive/generated/api_error.rb +58 -0
  9. data/lib/fetch_hive/generated/api_model_base.rb +88 -0
  10. data/lib/fetch_hive/generated/configuration.rb +415 -0
  11. data/lib/fetch_hive/generated/models/agent_message.rb +228 -0
  12. data/lib/fetch_hive/generated/models/async_config.rb +175 -0
  13. data/lib/fetch_hive/generated/models/error_response.rb +158 -0
  14. data/lib/fetch_hive/generated/models/invoke_agent_request.rb +250 -0
  15. data/lib/fetch_hive/generated/models/invoke_agent_response.rb +200 -0
  16. data/lib/fetch_hive/generated/models/invoke_prompt_request.rb +211 -0
  17. data/lib/fetch_hive/generated/models/invoke_prompt_response.rb +177 -0
  18. data/lib/fetch_hive/generated/models/invoke_workflow_async_response.rb +193 -0
  19. data/lib/fetch_hive/generated/models/invoke_workflow_request.rb +208 -0
  20. data/lib/fetch_hive/generated/models/invoke_workflow_response.rb +223 -0
  21. data/lib/fetch_hive/generated/models/sse_chunk.rb +228 -0
  22. data/lib/fetch_hive/generated/models/token_usage.rb +169 -0
  23. data/lib/fetch_hive/generated/models/tool_invocation.rb +169 -0
  24. data/lib/fetch_hive/generated/version.rb +15 -0
  25. data/lib/fetch_hive/streaming.rb +65 -0
  26. data/lib/fetch_hive/version.rb +5 -0
  27. data/lib/fetch_hive.rb +10 -0
  28. metadata +101 -0
@@ -0,0 +1,228 @@
1
+ =begin
2
+ #Fetch Hive Public API
3
+
4
+ #The Fetch Hive public API lets you invoke prompts, workflows, and agents programmatically using an API token. ## Authentication All requests require a Bearer token in the `Authorization` header: ``` Authorization: Bearer <your-api-token> ``` ## Streaming Pass `\"streaming\": true` in the request body to receive a `text/event-stream` response. Each event is a JSON object sent as `data: <json>\\n\\n`. The stream ends with `data: [DONE]\\n\\n`.
5
+
6
+ The version of the OpenAPI document: 0.2.2
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ Generator version: 7.22.0
10
+
11
+ =end
12
+
13
+ require 'date'
14
+ require 'time'
15
+
16
+ module FetchHive::Generated
17
+ # A single event in a Server-Sent Events stream. The type field is a runtime discriminator. Known values: delta, done, tool_start, tool_end, error.
18
+ class SseChunk < ApiModelBase
19
+ # Event type discriminator.
20
+ attr_accessor :type
21
+
22
+ # Text delta content (present for type \"delta\").
23
+ attr_accessor :content
24
+
25
+ # Present on the final \"done\" event.
26
+ attr_accessor :request_id
27
+
28
+ # Present on the final \"done\" event.
29
+ attr_accessor :model
30
+
31
+ # Tool name (present for \"tool_start\" / \"tool_end\").
32
+ attr_accessor :tool_name
33
+
34
+ # Serialised JSON tool input (present for \"tool_start\").
35
+ attr_accessor :tool_input
36
+
37
+ # Serialised JSON tool result (present for \"tool_end\").
38
+ attr_accessor :observation
39
+
40
+ # Error message (present for \"error\" events).
41
+ attr_accessor :error
42
+
43
+ attr_accessor :usage
44
+
45
+ # Attribute mapping from ruby-style variable name to JSON key.
46
+ def self.attribute_map
47
+ {
48
+ :'type' => :'type',
49
+ :'content' => :'content',
50
+ :'request_id' => :'request_id',
51
+ :'model' => :'model',
52
+ :'tool_name' => :'tool_name',
53
+ :'tool_input' => :'tool_input',
54
+ :'observation' => :'observation',
55
+ :'error' => :'error',
56
+ :'usage' => :'usage'
57
+ }
58
+ end
59
+
60
+ # Returns attribute mapping this model knows about
61
+ def self.acceptable_attribute_map
62
+ attribute_map
63
+ end
64
+
65
+ # Returns all the JSON keys this model knows about
66
+ def self.acceptable_attributes
67
+ acceptable_attribute_map.values
68
+ end
69
+
70
+ # Attribute type mapping.
71
+ def self.openapi_types
72
+ {
73
+ :'type' => :'String',
74
+ :'content' => :'String',
75
+ :'request_id' => :'String',
76
+ :'model' => :'String',
77
+ :'tool_name' => :'String',
78
+ :'tool_input' => :'String',
79
+ :'observation' => :'String',
80
+ :'error' => :'String',
81
+ :'usage' => :'TokenUsage'
82
+ }
83
+ end
84
+
85
+ # List of attributes with nullable: true
86
+ def self.openapi_nullable
87
+ Set.new([
88
+ ])
89
+ end
90
+
91
+ # Initializes the object
92
+ # @param [Hash] attributes Model attributes in the form of hash
93
+ def initialize(attributes = {})
94
+ if (!attributes.is_a?(Hash))
95
+ fail ArgumentError, "The input argument (attributes) must be a hash in `FetchHive::Generated::SseChunk` initialize method"
96
+ end
97
+
98
+ # check to see if the attribute exists and convert string to symbol for hash key
99
+ acceptable_attribute_map = self.class.acceptable_attribute_map
100
+ attributes = attributes.each_with_object({}) { |(k, v), h|
101
+ if (!acceptable_attribute_map.key?(k.to_sym))
102
+ fail ArgumentError, "`#{k}` is not a valid attribute in `FetchHive::Generated::SseChunk`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
103
+ end
104
+ h[k.to_sym] = v
105
+ }
106
+
107
+ if attributes.key?(:'type')
108
+ self.type = attributes[:'type']
109
+ end
110
+
111
+ if attributes.key?(:'content')
112
+ self.content = attributes[:'content']
113
+ end
114
+
115
+ if attributes.key?(:'request_id')
116
+ self.request_id = attributes[:'request_id']
117
+ end
118
+
119
+ if attributes.key?(:'model')
120
+ self.model = attributes[:'model']
121
+ end
122
+
123
+ if attributes.key?(:'tool_name')
124
+ self.tool_name = attributes[:'tool_name']
125
+ end
126
+
127
+ if attributes.key?(:'tool_input')
128
+ self.tool_input = attributes[:'tool_input']
129
+ end
130
+
131
+ if attributes.key?(:'observation')
132
+ self.observation = attributes[:'observation']
133
+ end
134
+
135
+ if attributes.key?(:'error')
136
+ self.error = attributes[:'error']
137
+ end
138
+
139
+ if attributes.key?(:'usage')
140
+ self.usage = attributes[:'usage']
141
+ end
142
+ end
143
+
144
+ # Show invalid properties with the reasons. Usually used together with valid?
145
+ # @return Array for valid properties with the reasons
146
+ def list_invalid_properties
147
+ warn '[DEPRECATED] the `list_invalid_properties` method is obsolete'
148
+ invalid_properties = Array.new
149
+ invalid_properties
150
+ end
151
+
152
+ # Check to see if the all the properties in the model are valid
153
+ # @return true if the model is valid
154
+ def valid?
155
+ warn '[DEPRECATED] the `valid?` method is obsolete'
156
+ true
157
+ end
158
+
159
+ # Checks equality by comparing each attribute.
160
+ # @param [Object] Object to be compared
161
+ def ==(o)
162
+ return true if self.equal?(o)
163
+ self.class == o.class &&
164
+ type == o.type &&
165
+ content == o.content &&
166
+ request_id == o.request_id &&
167
+ model == o.model &&
168
+ tool_name == o.tool_name &&
169
+ tool_input == o.tool_input &&
170
+ observation == o.observation &&
171
+ error == o.error &&
172
+ usage == o.usage
173
+ end
174
+
175
+ # @see the `==` method
176
+ # @param [Object] Object to be compared
177
+ def eql?(o)
178
+ self == o
179
+ end
180
+
181
+ # Calculates hash code according to all attributes.
182
+ # @return [Integer] Hash code
183
+ def hash
184
+ [type, content, request_id, model, tool_name, tool_input, observation, error, usage].hash
185
+ end
186
+
187
+ # Builds the object from hash
188
+ # @param [Hash] attributes Model attributes in the form of hash
189
+ # @return [Object] Returns the model itself
190
+ def self.build_from_hash(attributes)
191
+ return nil unless attributes.is_a?(Hash)
192
+ attributes = attributes.transform_keys(&:to_sym)
193
+ transformed_hash = {}
194
+ openapi_types.each_pair do |key, type|
195
+ if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil?
196
+ transformed_hash["#{key}"] = nil
197
+ elsif type =~ /\AArray<(.*)>/i
198
+ # check to ensure the input is an array given that the attribute
199
+ # is documented as an array but the input is not
200
+ if attributes[attribute_map[key]].is_a?(Array)
201
+ transformed_hash["#{key}"] = attributes[attribute_map[key]].map { |v| _deserialize($1, v) }
202
+ end
203
+ elsif !attributes[attribute_map[key]].nil?
204
+ transformed_hash["#{key}"] = _deserialize(type, attributes[attribute_map[key]])
205
+ end
206
+ end
207
+ new(transformed_hash)
208
+ end
209
+
210
+ # Returns the object in the form of hash
211
+ # @return [Hash] Returns the object in the form of hash
212
+ def to_hash
213
+ hash = {}
214
+ self.class.attribute_map.each_pair do |attr, param|
215
+ value = self.send(attr)
216
+ if value.nil?
217
+ is_nullable = self.class.openapi_nullable.include?(attr)
218
+ next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
219
+ end
220
+
221
+ hash[param] = _to_hash(value)
222
+ end
223
+ hash
224
+ end
225
+
226
+ end
227
+
228
+ end
@@ -0,0 +1,169 @@
1
+ =begin
2
+ #Fetch Hive Public API
3
+
4
+ #The Fetch Hive public API lets you invoke prompts, workflows, and agents programmatically using an API token. ## Authentication All requests require a Bearer token in the `Authorization` header: ``` Authorization: Bearer <your-api-token> ``` ## Streaming Pass `\"streaming\": true` in the request body to receive a `text/event-stream` response. Each event is a JSON object sent as `data: <json>\\n\\n`. The stream ends with `data: [DONE]\\n\\n`.
5
+
6
+ The version of the OpenAPI document: 0.2.2
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ Generator version: 7.22.0
10
+
11
+ =end
12
+
13
+ require 'date'
14
+ require 'time'
15
+
16
+ module FetchHive::Generated
17
+ # Token consumption for this request.
18
+ class TokenUsage < ApiModelBase
19
+ # Number of tokens in the prompt / input.
20
+ attr_accessor :prompt_tokens
21
+
22
+ # Number of tokens in the completion / output.
23
+ attr_accessor :completion_tokens
24
+
25
+ # Total tokens consumed.
26
+ attr_accessor :total_tokens
27
+
28
+ # Attribute mapping from ruby-style variable name to JSON key.
29
+ def self.attribute_map
30
+ {
31
+ :'prompt_tokens' => :'prompt_tokens',
32
+ :'completion_tokens' => :'completion_tokens',
33
+ :'total_tokens' => :'total_tokens'
34
+ }
35
+ end
36
+
37
+ # Returns attribute mapping this model knows about
38
+ def self.acceptable_attribute_map
39
+ attribute_map
40
+ end
41
+
42
+ # Returns all the JSON keys this model knows about
43
+ def self.acceptable_attributes
44
+ acceptable_attribute_map.values
45
+ end
46
+
47
+ # Attribute type mapping.
48
+ def self.openapi_types
49
+ {
50
+ :'prompt_tokens' => :'Integer',
51
+ :'completion_tokens' => :'Integer',
52
+ :'total_tokens' => :'Integer'
53
+ }
54
+ end
55
+
56
+ # List of attributes with nullable: true
57
+ def self.openapi_nullable
58
+ Set.new([
59
+ ])
60
+ end
61
+
62
+ # Initializes the object
63
+ # @param [Hash] attributes Model attributes in the form of hash
64
+ def initialize(attributes = {})
65
+ if (!attributes.is_a?(Hash))
66
+ fail ArgumentError, "The input argument (attributes) must be a hash in `FetchHive::Generated::TokenUsage` initialize method"
67
+ end
68
+
69
+ # check to see if the attribute exists and convert string to symbol for hash key
70
+ acceptable_attribute_map = self.class.acceptable_attribute_map
71
+ attributes = attributes.each_with_object({}) { |(k, v), h|
72
+ if (!acceptable_attribute_map.key?(k.to_sym))
73
+ fail ArgumentError, "`#{k}` is not a valid attribute in `FetchHive::Generated::TokenUsage`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
74
+ end
75
+ h[k.to_sym] = v
76
+ }
77
+
78
+ if attributes.key?(:'prompt_tokens')
79
+ self.prompt_tokens = attributes[:'prompt_tokens']
80
+ end
81
+
82
+ if attributes.key?(:'completion_tokens')
83
+ self.completion_tokens = attributes[:'completion_tokens']
84
+ end
85
+
86
+ if attributes.key?(:'total_tokens')
87
+ self.total_tokens = attributes[:'total_tokens']
88
+ end
89
+ end
90
+
91
+ # Show invalid properties with the reasons. Usually used together with valid?
92
+ # @return Array for valid properties with the reasons
93
+ def list_invalid_properties
94
+ warn '[DEPRECATED] the `list_invalid_properties` method is obsolete'
95
+ invalid_properties = Array.new
96
+ invalid_properties
97
+ end
98
+
99
+ # Check to see if the all the properties in the model are valid
100
+ # @return true if the model is valid
101
+ def valid?
102
+ warn '[DEPRECATED] the `valid?` method is obsolete'
103
+ true
104
+ end
105
+
106
+ # Checks equality by comparing each attribute.
107
+ # @param [Object] Object to be compared
108
+ def ==(o)
109
+ return true if self.equal?(o)
110
+ self.class == o.class &&
111
+ prompt_tokens == o.prompt_tokens &&
112
+ completion_tokens == o.completion_tokens &&
113
+ total_tokens == o.total_tokens
114
+ end
115
+
116
+ # @see the `==` method
117
+ # @param [Object] Object to be compared
118
+ def eql?(o)
119
+ self == o
120
+ end
121
+
122
+ # Calculates hash code according to all attributes.
123
+ # @return [Integer] Hash code
124
+ def hash
125
+ [prompt_tokens, completion_tokens, total_tokens].hash
126
+ end
127
+
128
+ # Builds the object from hash
129
+ # @param [Hash] attributes Model attributes in the form of hash
130
+ # @return [Object] Returns the model itself
131
+ def self.build_from_hash(attributes)
132
+ return nil unless attributes.is_a?(Hash)
133
+ attributes = attributes.transform_keys(&:to_sym)
134
+ transformed_hash = {}
135
+ openapi_types.each_pair do |key, type|
136
+ if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil?
137
+ transformed_hash["#{key}"] = nil
138
+ elsif type =~ /\AArray<(.*)>/i
139
+ # check to ensure the input is an array given that the attribute
140
+ # is documented as an array but the input is not
141
+ if attributes[attribute_map[key]].is_a?(Array)
142
+ transformed_hash["#{key}"] = attributes[attribute_map[key]].map { |v| _deserialize($1, v) }
143
+ end
144
+ elsif !attributes[attribute_map[key]].nil?
145
+ transformed_hash["#{key}"] = _deserialize(type, attributes[attribute_map[key]])
146
+ end
147
+ end
148
+ new(transformed_hash)
149
+ end
150
+
151
+ # Returns the object in the form of hash
152
+ # @return [Hash] Returns the object in the form of hash
153
+ def to_hash
154
+ hash = {}
155
+ self.class.attribute_map.each_pair do |attr, param|
156
+ value = self.send(attr)
157
+ if value.nil?
158
+ is_nullable = self.class.openapi_nullable.include?(attr)
159
+ next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
160
+ end
161
+
162
+ hash[param] = _to_hash(value)
163
+ end
164
+ hash
165
+ end
166
+
167
+ end
168
+
169
+ end
@@ -0,0 +1,169 @@
1
+ =begin
2
+ #Fetch Hive Public API
3
+
4
+ #The Fetch Hive public API lets you invoke prompts, workflows, and agents programmatically using an API token. ## Authentication All requests require a Bearer token in the `Authorization` header: ``` Authorization: Bearer <your-api-token> ``` ## Streaming Pass `\"streaming\": true` in the request body to receive a `text/event-stream` response. Each event is a JSON object sent as `data: <json>\\n\\n`. The stream ends with `data: [DONE]\\n\\n`.
5
+
6
+ The version of the OpenAPI document: 0.2.2
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ Generator version: 7.22.0
10
+
11
+ =end
12
+
13
+ require 'date'
14
+ require 'time'
15
+
16
+ module FetchHive::Generated
17
+ # A single tool call made by the agent during execution.
18
+ class ToolInvocation < ApiModelBase
19
+ # Name of the tool that was called.
20
+ attr_accessor :tool_name
21
+
22
+ # Arguments passed to the tool. Contains serialised JSON; parse client-side as needed.
23
+ attr_accessor :tool_input
24
+
25
+ # The tool's return value. Contains serialised JSON; parse client-side as needed.
26
+ attr_accessor :observation
27
+
28
+ # Attribute mapping from ruby-style variable name to JSON key.
29
+ def self.attribute_map
30
+ {
31
+ :'tool_name' => :'tool_name',
32
+ :'tool_input' => :'tool_input',
33
+ :'observation' => :'observation'
34
+ }
35
+ end
36
+
37
+ # Returns attribute mapping this model knows about
38
+ def self.acceptable_attribute_map
39
+ attribute_map
40
+ end
41
+
42
+ # Returns all the JSON keys this model knows about
43
+ def self.acceptable_attributes
44
+ acceptable_attribute_map.values
45
+ end
46
+
47
+ # Attribute type mapping.
48
+ def self.openapi_types
49
+ {
50
+ :'tool_name' => :'String',
51
+ :'tool_input' => :'String',
52
+ :'observation' => :'String'
53
+ }
54
+ end
55
+
56
+ # List of attributes with nullable: true
57
+ def self.openapi_nullable
58
+ Set.new([
59
+ ])
60
+ end
61
+
62
+ # Initializes the object
63
+ # @param [Hash] attributes Model attributes in the form of hash
64
+ def initialize(attributes = {})
65
+ if (!attributes.is_a?(Hash))
66
+ fail ArgumentError, "The input argument (attributes) must be a hash in `FetchHive::Generated::ToolInvocation` initialize method"
67
+ end
68
+
69
+ # check to see if the attribute exists and convert string to symbol for hash key
70
+ acceptable_attribute_map = self.class.acceptable_attribute_map
71
+ attributes = attributes.each_with_object({}) { |(k, v), h|
72
+ if (!acceptable_attribute_map.key?(k.to_sym))
73
+ fail ArgumentError, "`#{k}` is not a valid attribute in `FetchHive::Generated::ToolInvocation`. Please check the name to make sure it's valid. List of attributes: " + acceptable_attribute_map.keys.inspect
74
+ end
75
+ h[k.to_sym] = v
76
+ }
77
+
78
+ if attributes.key?(:'tool_name')
79
+ self.tool_name = attributes[:'tool_name']
80
+ end
81
+
82
+ if attributes.key?(:'tool_input')
83
+ self.tool_input = attributes[:'tool_input']
84
+ end
85
+
86
+ if attributes.key?(:'observation')
87
+ self.observation = attributes[:'observation']
88
+ end
89
+ end
90
+
91
+ # Show invalid properties with the reasons. Usually used together with valid?
92
+ # @return Array for valid properties with the reasons
93
+ def list_invalid_properties
94
+ warn '[DEPRECATED] the `list_invalid_properties` method is obsolete'
95
+ invalid_properties = Array.new
96
+ invalid_properties
97
+ end
98
+
99
+ # Check to see if the all the properties in the model are valid
100
+ # @return true if the model is valid
101
+ def valid?
102
+ warn '[DEPRECATED] the `valid?` method is obsolete'
103
+ true
104
+ end
105
+
106
+ # Checks equality by comparing each attribute.
107
+ # @param [Object] Object to be compared
108
+ def ==(o)
109
+ return true if self.equal?(o)
110
+ self.class == o.class &&
111
+ tool_name == o.tool_name &&
112
+ tool_input == o.tool_input &&
113
+ observation == o.observation
114
+ end
115
+
116
+ # @see the `==` method
117
+ # @param [Object] Object to be compared
118
+ def eql?(o)
119
+ self == o
120
+ end
121
+
122
+ # Calculates hash code according to all attributes.
123
+ # @return [Integer] Hash code
124
+ def hash
125
+ [tool_name, tool_input, observation].hash
126
+ end
127
+
128
+ # Builds the object from hash
129
+ # @param [Hash] attributes Model attributes in the form of hash
130
+ # @return [Object] Returns the model itself
131
+ def self.build_from_hash(attributes)
132
+ return nil unless attributes.is_a?(Hash)
133
+ attributes = attributes.transform_keys(&:to_sym)
134
+ transformed_hash = {}
135
+ openapi_types.each_pair do |key, type|
136
+ if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil?
137
+ transformed_hash["#{key}"] = nil
138
+ elsif type =~ /\AArray<(.*)>/i
139
+ # check to ensure the input is an array given that the attribute
140
+ # is documented as an array but the input is not
141
+ if attributes[attribute_map[key]].is_a?(Array)
142
+ transformed_hash["#{key}"] = attributes[attribute_map[key]].map { |v| _deserialize($1, v) }
143
+ end
144
+ elsif !attributes[attribute_map[key]].nil?
145
+ transformed_hash["#{key}"] = _deserialize(type, attributes[attribute_map[key]])
146
+ end
147
+ end
148
+ new(transformed_hash)
149
+ end
150
+
151
+ # Returns the object in the form of hash
152
+ # @return [Hash] Returns the object in the form of hash
153
+ def to_hash
154
+ hash = {}
155
+ self.class.attribute_map.each_pair do |attr, param|
156
+ value = self.send(attr)
157
+ if value.nil?
158
+ is_nullable = self.class.openapi_nullable.include?(attr)
159
+ next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
160
+ end
161
+
162
+ hash[param] = _to_hash(value)
163
+ end
164
+ hash
165
+ end
166
+
167
+ end
168
+
169
+ end
@@ -0,0 +1,15 @@
1
+ =begin
2
+ #Fetch Hive Public API
3
+
4
+ #The Fetch Hive public API lets you invoke prompts, workflows, and agents programmatically using an API token. ## Authentication All requests require a Bearer token in the `Authorization` header: ``` Authorization: Bearer <your-api-token> ``` ## Streaming Pass `\"streaming\": true` in the request body to receive a `text/event-stream` response. Each event is a JSON object sent as `data: <json>\\n\\n`. The stream ends with `data: [DONE]\\n\\n`.
5
+
6
+ The version of the OpenAPI document: 0.2.2
7
+
8
+ Generated by: https://openapi-generator.tech
9
+ Generator version: 7.22.0
10
+
11
+ =end
12
+
13
+ module FetchHive::Generated
14
+ VERSION = '{{VERSION}}'
15
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module FetchHive
6
+ # Lightweight Server-Sent Events (SSE) parser for streaming Fetch Hive responses.
7
+ #
8
+ # Sync example:
9
+ #
10
+ # io = response.body # any IO or string
11
+ # FetchHive::Streaming.parse_sse(io) do |event|
12
+ # puts event["content"] if event["type"] == "delta"
13
+ # end
14
+ #
15
+ module Streaming
16
+ # Yields each parsed SSE event hash from +io_or_string+.
17
+ # Stops when it encounters +data: [DONE]+ or the stream is exhausted.
18
+ # Non-data lines, blank lines, and malformed JSON are silently skipped.
19
+ #
20
+ # @param io_or_string [String, IO, #each_line] the SSE response body
21
+ # @yield [Hash] parsed JSON event
22
+ def self.parse_sse(io_or_string, &block)
23
+ return enum_for(:parse_sse, io_or_string) unless block
24
+
25
+ buf = +""
26
+ reader = io_or_string.respond_to?(:read) ? io_or_string : StringIO.new(io_or_string)
27
+
28
+ loop do
29
+ chunk = reader.read(4096)
30
+ break if chunk.nil? || chunk.empty?
31
+
32
+ buf << chunk
33
+
34
+ while (idx = buf.index("\n"))
35
+ line = buf.slice!(0, idx + 1).chomp
36
+ next unless line.start_with?("data: ")
37
+
38
+ payload = line[6..]
39
+ return if payload.strip == "[DONE]"
40
+
41
+ begin
42
+ yield JSON.parse(payload)
43
+ rescue JSON::ParserError
44
+ # skip malformed lines
45
+ end
46
+ end
47
+ end
48
+
49
+ # Process any remaining buffer content after stream ends
50
+ buf.each_line do |line|
51
+ line = line.chomp
52
+ next unless line.start_with?("data: ")
53
+
54
+ payload = line[6..]
55
+ next if payload.strip == "[DONE]"
56
+
57
+ begin
58
+ yield JSON.parse(payload)
59
+ rescue JSON::ParserError
60
+ # skip malformed lines
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FetchHive
4
+ VERSION = "{{VERSION}}"
5
+ end
data/lib/fetch_hive.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "fetch_hive/version"
4
+ require_relative "fetch_hive/streaming"
5
+ require_relative "fetch_hive/client"
6
+
7
+ module FetchHive
8
+ # Generated API constants are loaded from lib/fetch_hive/generated/ after assembly.
9
+ # autoload :Generated, "fetch_hive/generated"
10
+ end