Module: Legion::LLM::API::StreamAssembler::ChunkAdapter
- Defined in:
- lib/legion/llm/api/stream_assembler.rb
Class Method Summary collapse
-
.delta_text(delta) ⇒ Object
A canonical chunk delta is normally a String, but a non-incremental final message can arrive as a Canonical::ContentBlock (or array of them).
- .from_canonical(chunk) ⇒ Object
- .normalize(chunk) ⇒ Object
-
.server_tool_source?(source) ⇒ Boolean
R4: the chunk's tool_call member is the delta fragment (Hash) or a full Canonical::ToolCall; the source, when present, is the closed dispatch-type enum — server-executed tools are the registry/special/extension/mcp set (G24).
Class Method Details
.delta_text(delta) ⇒ Object
A canonical chunk delta is normally a String, but a non-incremental
final message can arrive as a Canonical::ContentBlock (or array of
them). Unwrap to text — never .to_s a value object onto the wire.
724 725 726 727 728 729 730 731 732 |
# File 'lib/legion/llm/api/stream_assembler.rb', line 724 def delta_text(delta) return '' if delta.nil? return delta if delta.is_a?(String) return delta.map { |part| delta_text(part) }.join if delta.is_a?(Array) return delta.text.to_s if delta.respond_to?(:text) && delta.text return delta.content.to_s if delta.respond_to?(:content) && delta.content '' end |
.from_canonical(chunk) ⇒ Object
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 |
# File 'lib/legion/llm/api/stream_assembler.rb', line 734 def from_canonical(chunk) case chunk.type when :text_delta AdaptedChunk.new(text: delta_text(chunk.delta)) when :thinking_delta AdaptedChunk.new(thinking_text: delta_text(chunk.delta), thinking_signature: chunk.signature) when :tool_call_delta tc = chunk.tool_call return AdaptedChunk.new if tc.nil? source = tc.respond_to?(:source) ? tc.source : nil AdaptedChunk.new( tool_calls: [{ id: tc.respond_to?(:id) ? tc.id : nil, name: tc.respond_to?(:name) ? tc.name : nil, arguments: tc.respond_to?(:arguments) ? tc.arguments : {}, result: tc.respond_to?(:result) ? tc.result : nil, server_tool: server_tool_source?(source) }] ) else AdaptedChunk.new end end |
.normalize(chunk) ⇒ Object
713 714 715 716 717 718 719 |
# File 'lib/legion/llm/api/stream_assembler.rb', line 713 def normalize(chunk) return nil if chunk.nil? raise ArgumentError, "ChunkAdapter expects Canonical::Chunk, got #{chunk.class}" unless chunk.is_a?(::Legion::Extensions::Llm::Canonical::Chunk) from_canonical(chunk) end |
.server_tool_source?(source) ⇒ Boolean
R4: the chunk's tool_call member is the delta fragment (Hash) or a full Canonical::ToolCall; the source, when present, is the closed dispatch-type enum — server-executed tools are the registry/special/extension/mcp set (G24).
763 764 765 766 767 768 |
# File 'lib/legion/llm/api/stream_assembler.rb', line 763 def server_tool_source?(source) return false if source.nil? type = source.is_a?(Hash) ? (source[:type] || source['type']) : source %i[special registry extension mcp].include?(type&.to_sym) end |