packages feed

praxo-core 0.7.1 → 0.8.0

raw patch · 13 files changed

+631/−46 lines, 13 filesdep +processPVP ok

version bump matches the API change (PVP)

Dependencies added: process

API changes (from Hackage documentation)

+ Praxo.Agent: [generateStream] :: AgentOptions -> Maybe ([Message] -> (String -> IO ()) -> IO (Either AgentError Response))
+ Praxo.Agent: [onDelta] :: AgentOptions -> Maybe (String -> IO ())
+ Praxo.Anthropic: generateAnthropicStream :: AnthropicConfig -> [Message] -> (String -> IO ()) -> IO (Either AgentError Response)
+ Praxo.Gemini: buildGeminiRequestWithStream :: Bool -> GeminiConfig -> [Message] -> Either AgentError HttpRequest
+ Praxo.Gemini: generateGeminiStream :: GeminiConfig -> [Message] -> (String -> IO ()) -> IO (Either AgentError Response)
+ Praxo.OpenAICompatible: generateStream :: OpenAIConfig -> [Message] -> (String -> IO ()) -> IO (Either AgentError Response)
+ Praxo.PluginProcess: PluginProcessConfig :: String -> String -> [String] -> Maybe FilePath -> Maybe [(String, String)] -> String -> String -> Int -> Int -> Int -> PluginProcessConfig
+ Praxo.PluginProcess: PluginProcessError :: String -> String -> PluginProcessError
+ Praxo.PluginProcess: [pluginArgs] :: PluginProcessConfig -> [String]
+ Praxo.PluginProcess: [pluginDir] :: PluginProcessConfig -> Maybe FilePath
+ Praxo.PluginProcess: [pluginEnv] :: PluginProcessConfig -> Maybe [(String, String)]
+ Praxo.PluginProcess: [pluginGracePeriod] :: PluginProcessConfig -> Int
+ Praxo.PluginProcess: [pluginId] :: PluginProcessConfig -> String
+ Praxo.PluginProcess: [pluginInvokeTimeout] :: PluginProcessConfig -> Int
+ Praxo.PluginProcess: [pluginPath] :: PluginProcessConfig -> String
+ Praxo.PluginProcess: [pluginStartTimeout] :: PluginProcessConfig -> Int
+ Praxo.PluginProcess: [pluginTokenEnvVar] :: PluginProcessConfig -> String
+ Praxo.PluginProcess: [pluginToken] :: PluginProcessConfig -> String
+ Praxo.PluginProcess: [processCode] :: PluginProcessError -> String
+ Praxo.PluginProcess: [processMessage] :: PluginProcessError -> String
+ Praxo.PluginProcess: data PluginProcessConfig
+ Praxo.PluginProcess: data PluginProcessError
+ Praxo.PluginProcess: data PluginProcessHost
+ Praxo.PluginProcess: instance GHC.Classes.Eq Praxo.PluginProcess.PluginProcessConfig
+ Praxo.PluginProcess: instance GHC.Classes.Eq Praxo.PluginProcess.PluginProcessError
+ Praxo.PluginProcess: instance GHC.Show.Show Praxo.PluginProcess.PluginProcessConfig
+ Praxo.PluginProcess: instance GHC.Show.Show Praxo.PluginProcess.PluginProcessError
+ Praxo.PluginProcess: invokePluginProcess :: PluginProcessConfig -> PluginProcessHost -> String -> String -> String -> IO (Either PluginProcessError String)
+ Praxo.PluginProcess: startPluginProcess :: PluginProcessConfig -> IO (Either PluginProcessError PluginProcessHost)
+ Praxo.PluginProcess: stopPluginProcess :: PluginProcessConfig -> PluginProcessHost -> IO ()
- Praxo.Agent: AgentOptions :: ([Message] -> IO (Either AgentError Response)) -> (ToolCall -> IO (Either AgentError Json)) -> (String -> IO (Either AgentError ())) -> Int -> Integer -> Maybe (Action -> Bool) -> [String] -> Bool -> Bool -> String -> String -> [String] -> Maybe InterruptHandler -> Maybe InterruptApproval -> Maybe (ToolCall -> IO (Either AgentError ToolCall)) -> AgentOptions
+ Praxo.Agent: AgentOptions :: ([Message] -> IO (Either AgentError Response)) -> (ToolCall -> IO (Either AgentError Json)) -> (String -> IO (Either AgentError ())) -> Int -> Integer -> Maybe (Action -> Bool) -> [String] -> Bool -> Bool -> String -> String -> [String] -> Maybe InterruptHandler -> Maybe InterruptApproval -> Maybe (ToolCall -> IO (Either AgentError ToolCall)) -> Maybe (String -> IO ()) -> Maybe ([Message] -> (String -> IO ()) -> IO (Either AgentError Response)) -> AgentOptions

Files

README.md view
@@ -109,7 +109,7 @@  `Praxo.OpenAICompatible` builds a `POST {baseUrl}/chat/completions` request with messages, tools, tool choice, assistant tool calls, and token usage, and maps-provider HTTP failures to `PROVIDER_HTTP`. Streaming is not implemented.+provider HTTP failures to `PROVIDER_HTTP`. `generateStream` sets `stream: true`,\nparses SSE, reassembles content, fragmented tool calls, and usage, and calls\nthe delta callback. Set `onDelta` and `generateStream` on `AgentOptions` to use\nit through the agent.  ```haskell import qualified Praxo.Agent as A@@ -148,8 +148,8 @@ import qualified Praxo.Gemini as G ` -They map tool calls, tool schemas, usage, and provider errors. No HTTP client is-bundled.+They map tool calls, tool schemas, usage, and provider errors.+`generateAnthropicStream` and `generateGeminiStream` reassemble SSE. No HTTP client is bundled.  ## Harmovela Event Sink 
REGISTRY.md view
@@ -1,25 +1,25 @@ # PRAXO-CORE Haskell Registry Introduction -**Status:** published. Source version `0.7.1`, source-tag `haskell/v0.7.1`. Published registry version: `0.7.0`. Registry+**Status:** published. Source version `0.8.0`, source+tag `haskell/v0.8.0`. Published registry version: `0.7.1`. Registry pages are release snapshots; verify the published version before relying on a feature.  ## Positioning -PRAXO-CORE is an embeddable, MIT-licensed agent framework. This package is its native Haskell Core. It implements all eleven declared vector groups and does not call Go or another Core. It includes a native callback-based agent loop with tools, policy hooks, typed events, memory, checkpoints, context budgets, middleware, JSON Schema validation, plugin manifest parsing, and runtime helpers.+PRAXO-CORE is an embeddable, MIT-licensed AI agent framework. This package is its native Haskell Core. It implements all eleven declared vector groups and does not call Go or another Core. It includes a native callback-based agent loop with tools, policy hooks, typed events, memory, checkpoints, context budgets, middleware, JSON Schema validation, plugin manifest parsing, and runtime helpers.  ## Package  | Field | Value | | --- | --- | | Package | `praxo-core` |-| Registry | [Hackage](https://hackage.haskell.org/package/praxo-core-0.7.0) |-| Source version | `0.7.1` |-| Published registry version | `0.7.0` |+| Registry | [Hackage](https://hackage.haskell.org/package/praxo-core-0.7.1) |+| Source version | `0.8.0` |+| Published registry version | `0.7.1` | | Install (published) | `cabal install praxo-core` | | Install (source/release prepared) | `cabal install praxo-core` |-| Source tag | `haskell/v0.7.1` |+| Source tag | `haskell/v0.8.0` | | Tier | Core | | Contract | `SPEC_VERSION` `0.2.0` | | Documentation | https://axisvela.github.io/praxo-core/languages/haskell/ |@@ -28,9 +28,9 @@  | Adapter | Status | Detail | | --- | --- | --- |-| OpenAI-compatible model | native (transport seam) | Caller supplies `Transport`; no bundled HTTP client |-| Anthropic model | native (transport seam) | `Praxo.Anthropic`; caller supplies transport |-| Gemini model | native (transport seam) | `Praxo.Gemini`; caller supplies transport |+| OpenAI-compatible model | native (transport seam) | Caller supplies `Transport`; no bundled HTTP client; SSE streaming reassembly |+| Anthropic model | native (transport seam) | `Praxo.Anthropic`; caller supplies transport; `generateAnthropicStream` reassembles Anthropic SSE |+| Gemini model | native (transport seam) | `Praxo.Gemini`; caller supplies transport; `generateGeminiStream` reassembles Gemini SSE | | MCP client | native (transport seam) | `Praxo.Mcp`; caller supplies `McpTransport` | | Harmovela sink | native (transport seam) | `Praxo.Harmovela`; caller supplies transport | | OpenTelemetry sink | native (transport seam) | `Praxo.Otel`; caller supplies `OtelTransport` |
conformance/Main.hs view
@@ -61,7 +61,7 @@                 then pure (Right (A.toolCallArguments call))                 else pure (Left (A.AgentError "TOOL_NOT_FOUND" "tool not found"))         sink event = modifyIORef' events (++ [JString event]) >> pure (Right ())-        options = A.AgentOptions model tool sink (fromInteger (number (field "maxSteps" input))) (number (field "maxTokens" input)) Nothing [] False False "run-conformance" "session-conformance" [] Nothing Nothing Nothing+        options = A.AgentOptions model tool sink (fromInteger (number (field "maxSteps" input))) (number (field "maxTokens" input)) Nothing [] False False "run-conformance" "session-conformance" [] Nothing Nothing Nothing Nothing Nothing     result <- A.runAgent options [] (asString (field "prompt" input))     actualEvents <- readIORef events     count <- readIORef invocations
llms.md view
@@ -7,21 +7,21 @@ ## Package  - package: `praxo-core`-- registry: [Hackage](https://hackage.haskell.org/package/praxo-core-0.7.0)-- source_version: `0.7.1`-- published_version: `0.7.0`+- registry: [Hackage](https://hackage.haskell.org/package/praxo-core-0.7.1)+- source_version: `0.8.0`+- published_version: `0.7.1` - install_published: `cabal install praxo-core` - install_source: `cabal install praxo-core`-- source_tag: `haskell/v0.7.1`+- source_tag: `haskell/v0.8.0` - tier: Core - contract: `SPEC_VERSION` `0.2.0` - documentation: https://axisvela.github.io/praxo-core/languages/haskell/  ## Capabilities and Status -- OpenAI-compatible model: native (transport seam) (Caller supplies `Transport`; no bundled HTTP client)-- Anthropic model: native (transport seam) (`Praxo.Anthropic`; caller supplies transport)-- Gemini model: native (transport seam) (`Praxo.Gemini`; caller supplies transport)+- OpenAI-compatible model: native (transport seam) (Caller supplies `Transport`; no bundled HTTP client; SSE streaming reassembly)+- Anthropic model: native (transport seam) (`Praxo.Anthropic`; caller supplies transport; `generateAnthropicStream` reassembles Anthropic SSE)+- Gemini model: native (transport seam) (`Praxo.Gemini`; caller supplies transport; `generateGeminiStream` reassembles Gemini SSE) - MCP client: native (transport seam) (`Praxo.Mcp`; caller supplies `McpTransport`) - Harmovela sink: native (transport seam) (`Praxo.Harmovela`; caller supplies transport) - OpenTelemetry sink: native (transport seam) (`Praxo.Otel`; caller supplies `OtelTransport`)
llms.txt view
@@ -7,21 +7,21 @@ # Package  - package: `praxo-core`-- registry: [Hackage](https://hackage.haskell.org/package/praxo-core-0.7.0)-- source_version: `0.7.1`-- published_version: `0.7.0`+- registry: [Hackage](https://hackage.haskell.org/package/praxo-core-0.7.1)+- source_version: `0.8.0`+- published_version: `0.7.1` - install_published: `cabal install praxo-core` - install_source: `cabal install praxo-core`-- source_tag: `haskell/v0.7.1`+- source_tag: `haskell/v0.8.0` - tier: Core - contract: `SPEC_VERSION` `0.2.0` - documentation: https://axisvela.github.io/praxo-core/languages/haskell/  # Capabilities and Status -- OpenAI-compatible model: native (transport seam) (Caller supplies `Transport`; no bundled HTTP client)-- Anthropic model: native (transport seam) (`Praxo.Anthropic`; caller supplies transport)-- Gemini model: native (transport seam) (`Praxo.Gemini`; caller supplies transport)+- OpenAI-compatible model: native (transport seam) (Caller supplies `Transport`; no bundled HTTP client; SSE streaming reassembly)+- Anthropic model: native (transport seam) (`Praxo.Anthropic`; caller supplies transport; `generateAnthropicStream` reassembles Anthropic SSE)+- Gemini model: native (transport seam) (`Praxo.Gemini`; caller supplies transport; `generateGeminiStream` reassembles Gemini SSE) - MCP client: native (transport seam) (`Praxo.Mcp`; caller supplies `McpTransport`) - Harmovela sink: native (transport seam) (`Praxo.Harmovela`; caller supplies transport) - OpenTelemetry sink: native (transport seam) (`Praxo.Otel`; caller supplies `OtelTransport`)
praxo-core.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.4 name: praxo-core-version: 0.7.1+version: 0.8.0 synopsis: Embeddable MIT-licensed AI agent framework for Haskell description: Native agent loop with tools, policy hooks, typed events, memory,              checkpoints, context budgets, middleware, JSON Schema validation,@@ -22,8 +22,8 @@  library   hs-source-dirs: src-  exposed-modules: Praxo.Agent, Praxo.Json, Praxo.ToolCall, Praxo.JsonSchema, Praxo.Pattern, Praxo.Plugin, Praxo.Runtime, Praxo.Middleware, Praxo.Wire, Praxo.OpenAICompatible, Praxo.Anthropic, Praxo.Gemini, Praxo.Harmovela, Praxo.Mcp, Praxo.Otel, Praxo.Retriever, Praxo.Delegation-  build-depends: base >=4.14 && <5, containers >=0.6 && <0.9+  exposed-modules: Praxo.Agent, Praxo.Json, Praxo.ToolCall, Praxo.JsonSchema, Praxo.Pattern, Praxo.Plugin, Praxo.PluginProcess, Praxo.Runtime, Praxo.Middleware, Praxo.Wire, Praxo.OpenAICompatible, Praxo.Anthropic, Praxo.Gemini, Praxo.Harmovela, Praxo.Mcp, Praxo.Otel, Praxo.Retriever, Praxo.Delegation+  build-depends: base >=4.14 && <5, containers >=0.6 && <0.9, process >=1.6, process >=1.6   default-language: Haskell2010   ghc-options: -Wall 
src/Praxo/Agent.hs view
@@ -25,7 +25,7 @@ type InterruptApproval = Interrupt -> IO Bool data Message = Message { messageRole :: String, messageContent :: String, messageToolCalls :: [ToolCall], messageToolCallId :: String } deriving (Eq, Show) data Response = Response { responseContent :: String, responseToolCalls :: [ToolCall], inputTokens :: Integer, outputTokens :: Integer } deriving (Eq, Show)-data AgentOptions = AgentOptions { generate :: [Message] -> IO (Either AgentError Response), invoke :: ToolCall -> IO (Either AgentError Json), emit :: String -> IO (Either AgentError ()), maxSteps :: Int, maxTokens :: Integer, policy :: Maybe (Action -> Bool), allowedTools :: [String], requirePolicy :: Bool, requireToolScope :: Bool, runId :: String, sessionId :: String, interruptOn :: [String], onInterruptDecision :: Maybe InterruptHandler, onInterrupt :: Maybe InterruptApproval, beforeTool :: Maybe (ToolCall -> IO (Either AgentError ToolCall)) }+data AgentOptions = AgentOptions { generate :: [Message] -> IO (Either AgentError Response), invoke :: ToolCall -> IO (Either AgentError Json), emit :: String -> IO (Either AgentError ()), maxSteps :: Int, maxTokens :: Integer, policy :: Maybe (Action -> Bool), allowedTools :: [String], requirePolicy :: Bool, requireToolScope :: Bool, runId :: String, sessionId :: String, interruptOn :: [String], onInterruptDecision :: Maybe InterruptHandler, onInterrupt :: Maybe InterruptApproval, beforeTool :: Maybe (ToolCall -> IO (Either AgentError ToolCall)), onDelta :: Maybe (String -> IO ()), generateStream :: Maybe ([Message] -> (String -> IO ()) -> IO (Either AgentError Response)) } data RunResult = RunResult { runOutput :: String, runSteps :: Int, runInputTokens :: Integer, runOutputTokens :: Integer, runMessages :: [Message] } deriving (Eq, Show)  data InterruptOutcome = InterruptOutcome@@ -81,7 +81,9 @@     loop step usedIn usedOut messages         | step > limit = pure (Left (AgentError "AGENT_MAX_STEPS" "agent: maximum steps exceeded"))         | otherwise = ensureAllowed (authorize "model.generate" (ToolCall "" "" JNull)) $ do-            generated <- generate options messages+            generated <- case (generateStream options, onDelta options) of+                (Just stream, Just delta) -> stream messages delta+                _ -> generate options messages             case generated of                 Left err -> pure (Left err)                 Right response | inputTokens response < 0 || outputTokens response < 0 ->
src/Praxo/Anthropic.hs view
@@ -4,9 +4,13 @@     , buildAnthropicRequest     , parseAnthropicResponse     , generateAnthropic+    , generateAnthropicStream     ) where -import Data.List (intercalate)+import Control.Monad (foldM)+import Data.Char (isSpace)+import Data.List (dropWhileEnd, intercalate, stripPrefix)+import qualified Data.Map.Strict as Map import Praxo.Agent (AgentError(..), Message(..), Response(..), ToolCall(..)) import Praxo.Json (Json(..), asArray, asString, encodeJson, field, parseJson) import Praxo.OpenAICompatible (HttpRequest(..), HttpResponse(..), Transport(..))@@ -29,7 +33,10 @@     }  buildAnthropicRequest :: AnthropicConfig -> [Message] -> Either AgentError HttpRequest-buildAnthropicRequest config messages+buildAnthropicRequest = buildAnthropicRequestWithStream False++buildAnthropicRequestWithStream :: Bool -> AnthropicConfig -> [Message] -> Either AgentError HttpRequest+buildAnthropicRequestWithStream stream config messages     | null (anthropicBaseURL config) = Left (AgentError "PROVIDER_HTTP" "provider base URL is required")     | otherwise = do         (system, encoded) <- messagesJson messages@@ -46,7 +53,8 @@                 Just "required" -> withTools ++ [("tool_choice", choice "any")]                 Just "none" -> withTools ++ [("tool_choice", choice "none")]                 _ -> withTools-        case encodeJson (JObject withChoice) of+            withStream = if stream then withChoice ++ [("stream", JBool True)] else withChoice+        case encodeJson (JObject withStream) of             Left detail -> Left (AgentError "PROVIDER_REQUEST" detail)             Right text ->                 Right HttpRequest@@ -54,7 +62,7 @@                     , httpURL = trimSlash (anthropicBaseURL config) ++ "/v1/messages"                     , httpHeaders =                         [ ("Content-Type", "application/json")-                        , ("Accept", "application/json")+                        , ("Accept", if stream then "text/event-stream" else "application/json")                         ] ++ case anthropicKey config of                             Nothing -> []                             Just key -> [("x-api-key", key), ("anthropic-version", anthropicVersion config)]@@ -158,6 +166,105 @@                         Left (AgentError "PROVIDER_HTTP"                             ("provider returned HTTP " ++ show (httpStatus response) ++ ": " ++ bounded (httpResponseBody response)))                     | otherwise -> parseAnthropicResponse (httpResponseBody response)++generateAnthropicStream :: AnthropicConfig -> [Message] -> (String -> IO ()) -> IO (Either AgentError Response)+generateAnthropicStream config messages onDelta =+    case buildAnthropicRequestWithStream True config messages of+        Left errorValue -> pure (Left errorValue)+        Right httpRequest -> do+            result <- sendTransport (anthropicTransport config) httpRequest+            case result of+                Left errorValue -> pure (Left errorValue)+                Right response+                    | httpStatus response < 200 || httpStatus response >= 300 ->+                        pure (Left (AgentError "PROVIDER_HTTP"+                            ("provider returned HTTP " ++ show (httpStatus response) ++ ": " ++ bounded (httpResponseBody response))))+                    | otherwise -> parseAnthropicSse (httpResponseBody response) onDelta++parseAnthropicSse :: String -> (String -> IO ()) -> IO (Either AgentError Response)+parseAnthropicSse body onDelta = go (lines body) "" Map.empty 0 0 Nothing False+  where+    go [] content calls inputTokens outputTokens finishReason done+        | done || maybe False (const True) finishReason = finish content calls inputTokens outputTokens+        | otherwise = pure (Left (AgentError "MODEL_STREAM" "stream ended before completion"))+    go (raw:rest) content calls inputTokens outputTokens finishReason done =+        case stripPrefix "data:" (trim raw) of+            Nothing -> go rest content calls inputTokens outputTokens finishReason done+            Just rawChunk ->+                let chunk = trim rawChunk+                in if null chunk+                    then go rest content calls inputTokens outputTokens finishReason done+                    else case parseJson chunk of+                        Left detail -> pure (Left (AgentError "MODEL_STREAM" ("invalid SSE packet: " ++ detail)))+                        Right event -> do+                            let kind = asString (field "type" event)+                            case kind of+                                "message_start" -> do+                                    let usage = field "usage" (field "message" event)+                                        inputTokens' = if field "input_tokens" usage == JNull+                                            then inputTokens else jsonInteger (field "input_tokens" usage)+                                    go rest content calls inputTokens' outputTokens finishReason done+                                "content_block_start" -> do+                                    let index = fromIntegral (jsonInteger (field "index" event)) :: Int+                                        block = field "content_block" event+                                        calls' = if asString (field "type" block) == "tool_use"+                                            then Map.insert index+                                                (asString (field "id" block), asString (field "name" block), "")+                                                calls+                                            else calls+                                    go rest content calls' inputTokens outputTokens finishReason done+                                "content_block_delta" -> do+                                    let index = fromIntegral (jsonInteger (field "index" event)) :: Int+                                        delta = field "delta" event+                                        deltaType = asString (field "type" delta)+                                    case deltaType of+                                        "text_delta" -> do+                                            let text = asString (field "text" delta)+                                            if null text+                                                then go rest content calls inputTokens outputTokens finishReason done+                                                else do+                                                    onDelta text+                                                    go rest (content ++ text) calls inputTokens outputTokens finishReason done+                                        "input_json_delta" -> do+                                            let partial = asString (field "partial_json" delta)+                                                calls' = Map.adjust+                                                    (\(identifier, name, rawArguments) ->+                                                        (identifier, name, rawArguments ++ partial))+                                                    index calls+                                            go rest content calls' inputTokens outputTokens finishReason done+                                        _ -> go rest content calls inputTokens outputTokens finishReason done+                                "message_delta" -> do+                                    let delta = field "delta" event+                                        reason = asString (field "stop_reason" delta)+                                        usage = field "usage" event+                                        outputTokens' = if field "output_tokens" usage == JNull+                                            then outputTokens else jsonInteger (field "output_tokens" usage)+                                        finishReason' = if null reason then finishReason else Just reason+                                    go rest content calls inputTokens outputTokens' finishReason' done+                                "message_stop" -> go rest content calls inputTokens outputTokens finishReason True+                                "error" -> pure (Left (AgentError "MODEL_STREAM" "provider stream error"))+                                _ -> go rest content calls inputTokens outputTokens finishReason done+    finish content calls inputTokens outputTokens =+        let built = foldl addCall (Right []) (Map.toAscList calls)+        in pure $ case built of+            Left errorValue -> Left errorValue+            Right toolCalls -> Right Response+                { responseContent = content+                , responseToolCalls = toolCalls+                , inputTokens = inputTokens+                , outputTokens = outputTokens+                }+      where+        addCall (Left errorValue) _ = Left errorValue+        addCall (Right acc) (_, (identifier, name, rawArguments))+            | null name = Right acc+            | otherwise = case if null rawArguments then Right (JObject []) else parseJson rawArguments of+                Left detail -> Left (AgentError "PROVIDER_JSON" ("tool arguments are not valid JSON: " ++ detail))+                Right value@(JObject _) -> Right (acc ++ [ToolCall identifier name value])+                Right _ -> Left (AgentError "PROVIDER_JSON" "tool arguments must be an object")++trim :: String -> String+trim = dropWhileEnd isSpace . dropWhile isSpace  orAgent :: Either String Json -> String -> Either AgentError Json orAgent (Right value) _ = Right value
src/Praxo/Gemini.hs view
@@ -2,10 +2,14 @@     ( GeminiTool(..)     , GeminiConfig(..)     , buildGeminiRequest+    , buildGeminiRequestWithStream     , parseGeminiResponse     , generateGemini+    , generateGeminiStream     ) where +import Data.Char (isSpace)+import Data.List (dropWhileEnd, stripPrefix) import Praxo.Agent (AgentError(..), Message(..), Response(..), ToolCall(..)) import Praxo.Json (Json(..), asArray, asString, encodeJson, field, parseJson) import Praxo.OpenAICompatible (HttpRequest(..), HttpResponse(..), Transport(..))@@ -27,7 +31,10 @@     }  buildGeminiRequest :: GeminiConfig -> [Message] -> Either AgentError HttpRequest-buildGeminiRequest config messages = do+buildGeminiRequest = buildGeminiRequestWithStream False++buildGeminiRequestWithStream :: Bool -> GeminiConfig -> [Message] -> Either AgentError HttpRequest+buildGeminiRequestWithStream stream config messages = do     (system, contents) <- contentsJson messages     let declarations = map toolJson (geminiTools config)         base = [("contents", JArray contents)]@@ -44,10 +51,11 @@         Right text ->             Right HttpRequest                 { httpMethod = "POST"-                , httpURL = trimSlash (geminiBaseURL config) ++ "/v1beta/models/" ++ geminiModel config ++ ":generateContent"+                , httpURL = trimSlash (geminiBaseURL config) ++ "/v1beta/models/" ++ geminiModel config +++                    (if stream then ":streamGenerateContent?alt=sse" else ":generateContent")                 , httpHeaders =                     [ ("Content-Type", "application/json")-                    , ("Accept", "application/json")+                    , ("Accept", if stream then "text/event-stream" else "application/json")                     ] ++ case geminiKey config of                         Nothing -> []                         Just key -> [("x-goog-api-key", key)]@@ -149,6 +157,75 @@                         Left (AgentError "PROVIDER_HTTP"                             ("provider returned HTTP " ++ show (httpStatus response) ++ ": " ++ bounded (httpResponseBody response)))                     | otherwise -> parseGeminiResponse (httpResponseBody response)++generateGeminiStream :: GeminiConfig -> [Message] -> (String -> IO ()) -> IO (Either AgentError Response)+generateGeminiStream config messages onDelta =+    case buildGeminiRequestWithStream True config messages of+        Left errorValue -> pure (Left errorValue)+        Right httpRequest -> do+            result <- sendTransport (geminiTransport config) httpRequest+            case result of+                Left errorValue -> pure (Left errorValue)+                Right response+                    | httpStatus response < 200 || httpStatus response >= 300 ->+                        pure (Left (AgentError "PROVIDER_HTTP"+                            ("provider returned HTTP " ++ show (httpStatus response) ++ ": " ++ bounded (httpResponseBody response))))+                    | otherwise -> parseGeminiSse (httpResponseBody response) onDelta++parseGeminiSse :: String -> (String -> IO ()) -> IO (Either AgentError Response)+parseGeminiSse body onDelta = go (lines body) "" [] 0 0 0+  where+    go [] content calls inputTokens outputTokens cachedTokens =+        pure (Right Response+            { responseContent = content+            , responseToolCalls = calls+            , inputTokens = inputTokens+            , outputTokens = outputTokens+            })+    go (raw:rest) content calls inputTokens outputTokens cachedTokens =+        case stripPrefix "data:" (trim raw) of+            Nothing -> go rest content calls inputTokens outputTokens cachedTokens+            Just rawChunk ->+                let chunk = trim rawChunk+                in if null chunk || chunk == "[DONE]"+                    then go rest content calls inputTokens outputTokens cachedTokens+                    else case parseJson chunk of+                        Left detail -> pure (Left (AgentError "MODEL_STREAM" ("gemini: invalid SSE packet: " ++ detail)))+                        Right event+                            | field "error" event /= JNull ->+                                pure (Left (AgentError "MODEL_STREAM" "gemini: provider stream error"))+                            | otherwise -> do+                                let usage = field "usageMetadata" event+                                    inputTokens' = if field "promptTokenCount" usage == JNull+                                        then inputTokens else jsonInteger (field "promptTokenCount" usage)+                                    outputTokens' = if field "candidatesTokenCount" usage == JNull+                                        then outputTokens else jsonInteger (field "candidatesTokenCount" usage)+                                    cachedTokens' = if field "cachedContentTokenCount" usage == JNull+                                        then cachedTokens else jsonInteger (field "cachedContentTokenCount" usage)+                                case asArray (field "candidates" event) of+                                    [] -> go rest content calls inputTokens' outputTokens' cachedTokens'+                                    candidate : _ -> do+                                        let parts = asArray (field "parts" (field "content" candidate))+                                        (content', calls') <- foldParts content calls parts+                                        go rest content' calls' inputTokens' outputTokens' cachedTokens'+    foldParts content calls [] = pure (content, calls)+    foldParts content calls (part:parts) = do+        let piece = asString (field "text" part)+        if null piece then pure () else onDelta piece+        let call = field "functionCall" part+            calls' = if field "name" call == JNull+                then calls+                else calls ++ [ToolCall+                    { toolCallId = "call-" ++ show (length calls + 1)+                    , toolCallName = asString (field "name" call)+                    , toolCallArguments = case field "args" call of+                        value@(JObject _) -> value+                        _ -> JObject []+                    }]+        foldParts (content ++ piece) calls' parts++trim :: String -> String+trim = dropWhileEnd isSpace . dropWhile isSpace  orAgent :: Either String Json -> String -> Either AgentError Json orAgent (Right value) _ = Right value
src/Praxo/OpenAICompatible.hs view
@@ -7,8 +7,14 @@     , buildRequest     , parseResponse     , generate+    , generateStream     ) where +import Control.Monad (foldM)+import Data.Char (isSpace)+import Data.List (dropWhileEnd, stripPrefix)+import qualified Data.Map.Strict as Map+ import Praxo.Agent (AgentError(..), Message(..), Response(..), ToolCall(..)) import Praxo.Json (Json(..), asArray, asString, encodeJson, field, parseJson) @@ -44,7 +50,10 @@     }  buildRequest :: OpenAIConfig -> [Message] -> Either AgentError HttpRequest-buildRequest config messages+buildRequest = buildRequestWithStream False++buildRequestWithStream :: Bool -> OpenAIConfig -> [Message] -> Either AgentError HttpRequest+buildRequestWithStream stream config messages     | null (openAIBaseURL config) = Left (AgentError "PROVIDER_HTTP" "provider base URL is required")     | otherwise = do         encoded <- mapM messageJson messages@@ -57,7 +66,8 @@             withChoice = case openAIToolChoice config of                 Nothing -> withTools                 Just choice -> withTools ++ [("tool_choice", JString choice)]-        case encodeJson (JObject withChoice) of+            withStream = if stream then withChoice ++ [("stream", JBool True)] else withChoice+        case encodeJson (JObject withStream) of             Left detail -> Left (AgentError "PROVIDER_REQUEST" detail)             Right text ->                 Right HttpRequest@@ -65,7 +75,7 @@                     , httpURL = trimSlash (openAIBaseURL config) ++ "/chat/completions"                     , httpHeaders =                         [ ("Content-Type", "application/json")-                        , ("Accept", "application/json")+                        , ("Accept", if stream then "text/event-stream" else "application/json")                         ] ++ case openAIKey config of                             Nothing -> []                             Just key -> [("Authorization", "Bearer " ++ key)]@@ -162,6 +172,92 @@     parameters = case openAIToolParameters tool of         JNull -> JObject [("type", JString "object")]         value -> value++generateStream :: OpenAIConfig -> [Message] -> (String -> IO ()) -> IO (Either AgentError Response)+generateStream config messages onDelta =+    case buildRequestWithStream True config messages of+        Left errorValue -> pure (Left errorValue)+        Right httpRequest -> do+            result <- sendTransport (openAITransport config) httpRequest+            case result of+                Left errorValue -> pure (Left errorValue)+                Right response+                    | httpStatus response < 200 || httpStatus response >= 300 ->+                        pure (Left (AgentError "PROVIDER_HTTP"+                            ("provider returned HTTP " ++ show (httpStatus response) ++ ": " ++ bounded (httpResponseBody response))))+                    | otherwise -> parseSse (httpResponseBody response) onDelta++parseSse :: String -> (String -> IO ()) -> IO (Either AgentError Response)+parseSse body onDelta = go (lines body) "" Map.empty 0 0 Nothing False+  where+    go [] content calls inputTokens outputTokens finishReason done+        | done = finish content calls inputTokens outputTokens finishReason+        | otherwise = pure (Left (AgentError "MODEL_STREAM" "stream ended before completion"))+    go (raw:rest) content calls inputTokens outputTokens finishReason done =+        case stripPrefix "data:" (trim raw) of+            Nothing -> go rest content calls inputTokens outputTokens finishReason done+            Just rawChunk ->+                let chunk = trim rawChunk+                in if null chunk+                    then go rest content calls inputTokens outputTokens finishReason done+                    else if chunk == "[DONE]"+                        then finish content calls inputTokens outputTokens finishReason+                        else case parseJson chunk of+                            Left detail -> pure (Left (AgentError "MODEL_STREAM" ("invalid SSE packet: " ++ detail)))+                            Right event -> do+                                let usage = field "usage" event+                                    inputTokens' = if field "prompt_tokens" usage == JNull+                                        then inputTokens else jsonInteger (field "prompt_tokens" usage)+                                    outputTokens' = if field "completion_tokens" usage == JNull+                                        then outputTokens else jsonInteger (field "completion_tokens" usage)+                                (content', calls', finishReason', done') <-+                                    foldM (handleChoice onDelta) (content, calls, finishReason, done)+                                        (asArray (field "choices" event))+                                go rest content' calls' inputTokens' outputTokens' finishReason' done'+    finish content calls inputTokens outputTokens finishReason =+        let built = foldl addCall (Right []) (Map.toAscList calls)+        in pure $ case built of+            Left errorValue -> Left errorValue+            Right toolCalls -> Right Response+                { responseContent = content+                , responseToolCalls = toolCalls+                , inputTokens = inputTokens+                , outputTokens = outputTokens+                }+      where+        addCall (Left errorValue) _ = Left errorValue+        addCall (Right acc) (_, (identifier, name, rawArguments))+            | null name = Right acc+            | otherwise = case if null rawArguments then Right (JObject []) else parseJson rawArguments of+                Left detail -> Left (AgentError "PROVIDER_JSON" ("tool arguments are not valid JSON: " ++ detail))+                Right value@(JObject _) ->+                    Right (acc ++ [ToolCall identifier name value])+                Right _ -> Left (AgentError "PROVIDER_JSON" "tool arguments must be an object")++handleChoice :: (String -> IO ()) -> (String, Map.Map Integer (String, String, String), Maybe String, Bool)+             -> Json -> IO (String, Map.Map Integer (String, String, String), Maybe String, Bool)+handleChoice onDelta (content, calls, finishReason, done) choice = do+    let delta = field "delta" choice+        piece = asString (field "content" delta)+    if null piece then pure () else onDelta piece+    let calls' = foldl updateCall calls (asArray (field "tool_calls" delta))+        reason = asString (field "finish_reason" choice)+        finishReason' = if null reason then finishReason else Just reason+        done' = done || not (null reason)+    pure (content ++ piece, calls', finishReason', done')+  where+    updateCall current item =+        let index = jsonInteger (field "index" item)+            function = field "function" item+            (identifier, name, arguments) = Map.findWithDefault ("", "", "") index current+        in Map.insert index+            ( identifier ++ asString (field "id" item)+            , name ++ asString (field "name" function)+            , arguments ++ asString (field "arguments" function)+            ) current++trim :: String -> String+trim = dropWhileEnd isSpace . dropWhile isSpace  jsonInteger :: Json -> Integer jsonInteger (JNumber value) = truncate value
+ src/Praxo/PluginProcess.hs view
@@ -0,0 +1,188 @@+{-# LANGUAGE ScopedTypeVariables #-}+module Praxo.PluginProcess+    ( PluginProcessConfig(..)+    , PluginProcessError(..)+    , PluginProcessHost+    , startPluginProcess+    , invokePluginProcess+    , stopPluginProcess+    ) where++import Control.Exception (SomeException, try)+import Data.Char (chr, digitToInt, intToDigit, isSpace)+import Data.IORef+import Data.List (dropWhileEnd)+import System.Environment (getEnvironment)+import System.IO+import System.Process+import System.Timeout (timeout)++import Praxo.Agent (AgentError(..))+import Praxo.Json (Json(..), asString, encodeJson, field, parseJson)++data PluginProcessConfig = PluginProcessConfig+    { pluginId :: String+    , pluginPath :: String+    , pluginArgs :: [String]+    , pluginDir :: Maybe FilePath+    , pluginEnv :: Maybe [(String, String)]+    , pluginToken :: String+    , pluginTokenEnvVar :: String+    , pluginStartTimeout :: Int+    , pluginInvokeTimeout :: Int+    , pluginGracePeriod :: Int+    } deriving (Eq, Show)++data PluginProcessError = PluginProcessError+    { processCode :: String+    , processMessage :: String+    } deriving (Eq, Show)++data PluginProcessHost = PluginProcessHost+    { hostId :: String+    , hostToken :: String+    , hostInput :: Handle+    , hostOutput :: Handle+    , hostProcess :: ProcessHandle+    , hostSeq :: IORef Int+    , hostActive :: IORef Bool+    }++startPluginProcess :: PluginProcessConfig -> IO (Either PluginProcessError PluginProcessHost)+startPluginProcess config+    | null (pluginId config) = pure (Left (err "INVALID_REQUEST" "plugin: PluginID is required"))+    | null (pluginPath config) = pure (Left (err "INVALID_REQUEST" "plugin: path is required"))+    | otherwise = do+        env0 <- maybe getEnvironment pure (pluginEnv config)+        let tokenEnv = if null (pluginTokenEnvVar config) then "PRAXOVELA_PLUGIN_TOKEN" else pluginTokenEnvVar config+            token = if null (pluginToken config) then "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" else pluginToken config+            env = (tokenEnv, token) : filter ((/= tokenEnv) . fst) env0+        result <- try $ createProcess (proc (pluginPath config) (pluginArgs config))+            { cwd = pluginDir config+            , env = Just env+            , std_in = CreatePipe+            , std_out = CreatePipe+            , std_err = CreatePipe+            }+        case result of+            Left (_ :: SomeException) -> pure (Left (err "INTERNAL" "plugin: spawn failed"))+            Right (Just input, Just output, _stderr, process) -> do+                seqRef <- newIORef 0+                activeRef <- newIORef False+                let host = PluginProcessHost (pluginId config) token input output process seqRef activeRef+                health <- callHost config host "host.control" "health" "" (pluginStartTimeout config)+                case health of+                    Left errorValue -> do+                        terminateProcess process+                        pure (Left errorValue)+                    Right _ -> do+                        writeIORef activeRef True+                        pure (Right host)+            Right _ -> pure (Left (err "INTERNAL" "plugin: pipe setup failed"))++invokePluginProcess :: PluginProcessConfig -> PluginProcessHost -> String -> String -> String -> IO (Either PluginProcessError String)+invokePluginProcess config host service method payload = do+    active <- readIORef (hostActive host)+    if not active+        then pure (Left (err "NOT_ACTIVE" "plugin: host is not active"))+        else callHost config host service method payload (pluginInvokeTimeout config)++stopPluginProcess :: PluginProcessConfig -> PluginProcessHost -> IO ()+stopPluginProcess config host = do+    _ <- callHost config host "host.control" "shutdown" "" (pluginGracePeriod config)+    writeIORef (hostActive host) False+    _ <- try (terminateProcess (hostProcess host)) :: IO (Either SomeException ())+    pure ()++callHost :: PluginProcessConfig -> PluginProcessHost -> String -> String -> String -> Int -> IO (Either PluginProcessError String)+callHost config host service method payload timeoutMs = do+    seqValue <- atomicModifyIORef' (hostSeq host) (\value -> (value + 1, value + 1))+    let requestId = "req-" ++ show seqValue+        frame = JObject+            [ ("id", JString requestId)+            , ("version", JNumber 1)+            , ("plugin_id", JString (hostId host))+            , ("service", JString service)+            , ("method", JString method)+            , ("token", JString (hostToken host))+            , ("payload", JString (base64Encode payload))+            ]+    case encodeJson frame of+        Left detail -> pure (Left (err "INVALID_REQUEST" detail))+        Right encoded -> do+            writeResult <- try $ do+                hPutStrLn (hostInput host) encoded+                hFlush (hostInput host)+            case writeResult of+                Left (_ :: SomeException) -> pure (Left (err "INTERNAL" "plugin: request write failed"))+                Right () -> readResponse (hostOutput host) requestId timeoutMs++readResponse :: Handle -> String -> Int -> IO (Either PluginProcessError String)+readResponse output requestId timeoutMs = do+    line <- timeout timeoutMs (hGetLine output)+    case line of+        Nothing -> pure (Left (err "TIMEOUT" "plugin: invocation timed out"))+        Just raw -> case parseJson raw of+            Left detail -> pure (Left (err "INTERNAL" ("plugin: invalid response: " ++ detail)))+            Right value+                | asString (field "id" value) /= requestId -> readResponse output requestId timeoutMs+                | field "ok" value /= JBool True ->+                    pure (Left (PluginProcessError+                        (normalizeCode (asString (field "code" value)))+                        (asString (field "message" value))))+                | otherwise -> pure $ case base64Decode (asString (field "payload" value)) of+                    Nothing -> Left (err "INTERNAL" "plugin: invalid payload")+                    Just decoded -> Right decoded++normalizeCode :: String -> String+normalizeCode code+    | code `elem` known = code+    | otherwise = "INTERNAL"+  where+    known = ["CAPABILITY_DENIED", "NOT_ACTIVE", "NOT_FOUND", "INVALID_REQUEST", "TIMEOUT", "INTERNAL", "CANCELED"]++err :: String -> String -> PluginProcessError+err code message = PluginProcessError code message++base64Encode :: String -> String+base64Encode = concatMap encodeChunk . chunksOf3+  where+    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"+    encodeChunk [a] =+        [ alphabet !! (fromEnum a `div` 4)+        , alphabet !! ((fromEnum a `mod` 4) * 16)+        , '=', '=']+    encodeChunk [a, b] =+        [ alphabet !! (fromEnum a `div` 4)+        , alphabet !! (((fromEnum a `mod` 4) * 16) + (fromEnum b `div` 16))+        , alphabet !! ((fromEnum b `mod` 16) * 4)+        , '=']+    encodeChunk (a:b:c:_) =+        [ alphabet !! (fromEnum a `div` 4)+        , alphabet !! (((fromEnum a `mod` 4) * 16) + (fromEnum b `div` 16))+        , alphabet !! (((fromEnum b `mod` 16) * 4) + (fromEnum c `div` 64))+        , alphabet !! (fromEnum c `mod` 64)]+    encodeChunk [] = []+    chunksOf3 [] = []+    chunksOf3 value = take 3 value : chunksOf3 (drop 3 value)++base64Decode :: String -> Maybe String+base64Decode text+    | any (not . valid) filtered = Nothing+    | otherwise = Just (go filtered)+  where+    filtered = takeWhile (/= '=') (filter (not . isSpace) text)+    valid c = c `elem` alphabet+    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"+    value c = case lookup c (zip alphabet [0 ..]) of+        Just index -> index+        Nothing -> 0+    go = goBits 0 0 []+    goBits _ _ acc [] = reverse acc+    goBits buffer bits acc (c:rest) =+        let next = buffer * 64 + value c+            bits2 = bits + 6+        in if bits2 >= 8+            then let output = (next `div` (2 ^ (bits2 - 8))) `mod` 256+                 in goBits next (bits2 - 8) (chr output : acc) rest+            else goBits next bits2 acc rest
src/Praxo/Wire.hs view
@@ -4,4 +4,4 @@ specVersion = "0.2.0"  sdkVersion :: String-sdkVersion = "0.7.1"+sdkVersion = "0.8.0"
tests/Main.hs view
@@ -34,7 +34,7 @@     harmovelaTests     mcpTests     seamTests-    assert "contract version" (specVersion == "0.2.0" && sdkVersion == "0.7.1")+    assert "contract version" (specVersion == "0.2.0" && sdkVersion == "0.8.0")     forM_ ["", "01", "1.", "+1", "NaN", "1e", "[1,]", "{\"a\":1,}", "{}{}", "\"\\q\"", "\"\\ud800\"", "\"\\udc00\"", "\"\\ud800\\u0041\"", "\"line\nbreak\"", "true garbage"] $ \raw ->         assert ("invalid JSON " ++ show raw) (isLeft (parseJson raw))     assert "unicode escape" (parseJson "\"\\u00e9\\ud83d\\ude00\"" == Right (JString "\x00e9\x1f600"))@@ -104,7 +104,7 @@             n <- readIORef calls             pure (Right (if n == 0 then A.Response "" [call] 2 1 else A.Response "done" [] 1 1))         tool _ = modifyIORef' calls (+1) >> pure (Right JNull)-        options = A.AgentOptions model tool sink 2 5 Nothing [] False False "run-test" "session-test" [] Nothing Nothing Nothing+        options = A.AgentOptions model tool sink 2 5 Nothing [] False False "run-test" "session-test" [] Nothing Nothing Nothing Nothing Nothing     result <- A.runAgent options [] "hello"     assert "agent success and usage" (case result of Right r -> A.runSteps r == 2 && A.runInputTokens r == 3 && A.runOutputTokens r == 2; _ -> False)     history <- readIORef requests@@ -200,6 +200,8 @@             , A.onInterruptDecision = Nothing             , A.onInterrupt = Nothing             , A.beforeTool = Nothing+            , A.onDelta = Nothing+            , A.generateStream = Nothing             }      approveCalls <- newIORef []@@ -383,6 +385,46 @@             assert "openai messages" (length payloadMessages == 2 && equalJson (field "tool_call_id" (payloadMessages !! 1)) (JString "call-1"))             let payloadTools = asArray (field "tools" payload)             assert "openai tools" (length payloadTools == 1 && equalJson (field "name" (field "function" (head payloadTools))) (JString "echo"))+    streamDeltas <- newIORef ""+    let sse = unlines+            [ "data: {\"choices\":[{\"delta\":{\"content\":\"he\"}}]}"+            , ""+            , "data: {\"choices\":[{\"delta\":{\"content\":\"llo\"}}]}"+            , ""+            , "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call-1\",\"function\":{\"name\":\"ec\",\"arguments\":\"{\\\"text\\\":\"}}]}}]}"+            , ""+            , "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"name\":\"ho\",\"arguments\":\"\\\"hi\\\"}\"}}]}}],\"usage\":{\"prompt_tokens\":4,\"completion_tokens\":2}}"+            , ""+            , "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"tool_calls\"}]}"+            , ""+            , "data: [DONE]"+            , ""+            ]+        streamTransport request = do+            writeIORef captured (Just request)+            pure (Right (O.HttpResponse 200 sse))+    streamed <- O.generateStream (config { O.openAITransport = O.Transport streamTransport }) messages+        (\delta -> modifyIORef' streamDeltas (++ delta))+    case streamed of+        Left err -> fail ("openai stream: " ++ A.errorMessage err)+        Right response -> do+            assert "openai stream content" (A.responseContent response == "hello")+            assert "openai stream usage" (A.inputTokens response == 4 && A.outputTokens response == 2)+            case A.responseToolCalls response of+                [call] -> do+                    assert "openai stream call id" (A.toolCallId call == "call-1")+                    assert "openai stream call name" (A.toolCallName call == "echo")+                    assert "openai stream call arguments" (A.toolCallArguments call == JObject [("text", JString "hi")])+                _ -> fail "openai stream call"+    deltas <- readIORef streamDeltas+    assert "openai stream deltas" (deltas == "hello")+    streamRequest <- readIORef captured+    case streamRequest of+        Just http -> do+            assert "openai stream accept" (lookup "Accept" (O.httpHeaders http) == Just "text/event-stream")+            let payload = json (O.httpBody http)+            assert "openai stream flag" (equalJson (field "stream" payload) (JBool True))+        Nothing -> fail "openai stream transport not called"     let failing = O.Transport (\_ -> pure (Right (O.HttpResponse 503 "down")))     failed <- O.generate (config { O.openAITransport = failing }) messages     assert "openai provider error" (case failed of Left err -> A.errorCode err == "PROVIDER_HTTP"; _ -> False)@@ -436,6 +478,45 @@             assert "anthropic system" (field "system" payload == JString "be brief")             assert "anthropic tools" (length (asArray (field "tools" payload)) == 1) +    capturedStream <- newIORef Nothing+    let sseBody = concatMap (\event -> "data: " ++ event ++ "\n\n")+            [ "{\"type\":\"message_start\",\"message\":{\"usage\":{\"input_tokens\":4,\"cache_read_input_tokens\":1}}}"+            , "{\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}"+            , "{\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"he\"}}"+            , "{\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"llo\"}}"+            , "{\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"tool_use\",\"id\":\"call-1\",\"name\":\"echo\",\"input\":{}}}"+            , "{\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"text\\\":\"}}"+            , "{\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"\\\"hi\\\"}\"}}"+            , "{\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\"},\"usage\":{\"output_tokens\":2}}"+            , "{\"type\":\"message_stop\"}"+            ]+        streamTransport = O.Transport (\request -> do+            writeIORef capturedStream (Just request)+            pure (Right (O.HttpResponse 200 sseBody)))+        streamConfig = anthropicConfig { An.anthropicTransport = streamTransport }+    deltas <- newIORef ([] :: [String])+    streamResult <- An.generateAnthropicStream streamConfig messages+        (\delta -> modifyIORef' deltas (++ [delta]))+    case streamResult of+        Left err -> fail ("anthropic stream: " ++ A.errorMessage err)+        Right response -> do+            assert "anthropic stream content" (A.responseContent response == "hello")+            assert "anthropic stream usage" (A.inputTokens response == 4 && A.outputTokens response == 2)+            case A.responseToolCalls response of+                [call] -> do+                    assert "anthropic stream call id" (A.toolCallId call == "call-1")+                    assert "anthropic stream call name" (A.toolCallName call == "echo")+                    assert "anthropic stream arguments" (A.toolCallArguments call == JObject [("text", JString "hi")])+                _ -> fail "anthropic stream call"+    emitted <- readIORef deltas+    assert "anthropic stream deltas" (emitted == ["he", "llo"])+    streamRequest <- readIORef capturedStream+    case streamRequest of+        Nothing -> fail "anthropic stream transport not called"+        Just http -> do+            assert "anthropic stream accept" (lookup "Accept" (O.httpHeaders http) == Just "text/event-stream")+            assert "anthropic stream flag" (field "stream" (json (O.httpBody http)) == JBool True)+     capturedGemini <- newIORef Nothing     let geminiTransport = O.Transport (\request -> do             writeIORef capturedGemini (Just request)@@ -467,6 +548,40 @@             let payload = json (O.httpBody http)             assert "gemini system" (length (asArray (field "parts" (field "system_instruction" payload))) == 1)             assert "gemini tools" (length (asArray (field "functionDeclarations" (head (asArray (field "tools" payload))))) == 1)++    capturedGeminiStream <- newIORef Nothing+    let geminiSse = concat+            [ "data: {\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"he\"}]}}]}\n\n"+            , "data: {\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"llo\"},"+            , "{\"functionCall\":{\"name\":\"echo\",\"args\":{\"text\":\"hi\"}}}]},"+            , "\"finishReason\":\"STOP\"}],\"usageMetadata\":{\"promptTokenCount\":4,"+            , "\"candidatesTokenCount\":2,\"cachedContentTokenCount\":1}}"+            ]+        geminiStreamTransport = O.Transport (\request -> do+            writeIORef capturedGeminiStream (Just request)+            pure (Right (O.HttpResponse 200 geminiSse)))+        geminiStreamConfig = geminiConfig { G.geminiTransport = geminiStreamTransport }+    geminiDeltas <- newIORef ([] :: [String])+    geminiStreamResult <- G.generateGeminiStream geminiStreamConfig messages+        (\delta -> modifyIORef' geminiDeltas (++ [delta]))+    case geminiStreamResult of+        Left err -> fail ("gemini stream: " ++ A.errorMessage err)+        Right response -> do+            assert "gemini stream content" (A.responseContent response == "hello")+            assert "gemini stream usage" (A.inputTokens response == 4 && A.outputTokens response == 2)+            case A.responseToolCalls response of+                [call] -> do+                    assert "gemini stream call name" (A.toolCallName call == "echo")+                    assert "gemini stream arguments" (A.toolCallArguments call == JObject [("text", JString "hi")])+                _ -> fail "gemini stream call"+    emitted <- readIORef geminiDeltas+    assert "gemini stream deltas" (emitted == ["he", "llo"])+    streamRequest <- readIORef capturedGeminiStream+    case streamRequest of+        Nothing -> fail "gemini stream transport not called"+        Just http -> do+            assert "gemini stream url" (":streamGenerateContent?alt=sse" `isInfixOf` O.httpURL http)+            assert "gemini stream accept" (lookup "Accept" (O.httpHeaders http) == Just "text/event-stream")  harmovelaTests :: IO () harmovelaTests = do