fix(tools): handle binary file downloads in tool execution
Merged
JE
6mo ago+529+529+012345678901234567890123456789-37-37-01234567890123456789
Summary
- File-download actions (e.g.
googledrive_unified_download_file) serve raw binary with the file's own MIME type and aContent-Dispositionheader. Both execution paths calledresponse.json()unconditionally and raisedSyntaxError: Unexpected token '%'on the first non-JSON byte:RpcClient.rpcAction(src/rpc-client.ts) andRequestBuilder.execute(src/requestBuilder.ts). EveryStackOneToolSetunified action runs through the RPC path, so downloads failed outright. - Both paths now branch on
Content-Type: JSON media types (application/jsonand+jsonsuffixes) are parsed as before; any other type returns the file as{ content: Buffer, contentType, statusCode, headers, fileName }, matching the StackOne generated SDKs' download response shape.fileNameis parsed fromContent-Disposition(including RFC 5987filename*). - Shared logic lives in
src/utils/binary-response.ts. The publicisBinaryDownloadResulttype guard andBinaryDownloadResulttype let callers narrow the result without casts. - Existing JSON responses are unchanged.
Note: content holds raw bytes (a Buffer) and is not JSON-serializable ā callers that re-serialize tool results (e.g. for an LLM) should handle that key. This is the node port of StackOneHQ/stackone-ai-python#190.
Summary by cubic
Fixes tool execution to correctly handle binary file downloads by detecting Content-Type and returning raw bytes plus metadata instead of parsing JSON. This unblocks all *_unified_download_file actions over RPC and HTTP (e.g. googledrive_unified_download_file).
- Bug Fixes
- Branch on
Content-Type: parse JSON forapplication/jsonand+json; otherwise returnBinaryDownloadResult{ content: Buffer, contentType, statusCode, headers, fileName }. Non-JSON error bodies now raiseStackOneAPIError(notSyntaxError). - Shared helpers in
src/utils/binary-response.ts(isJsonContentType,isBinaryDownloadResult, filename parsing incl. RFC 5987 with safe fallback) used by bothRpcClient.actions.rpcActionandRequestBuilder.execute; JSON behavior unchanged. - Exported
isBinaryDownloadResultandBinaryDownloadResultfrom the public API; widenedrpcActionreturn type toRpcActionResponse | BinaryDownloadResultand updated tests to narrow with the guard; added README docs. - Note:
contentis aBufferand not JSON-serializable; encode or omit when forwarding to an LLM.
- Branch on