Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add streaming support #119

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ delete-me
# generated files
*.rockspec
src/resty/aws/raw-api
.DS_Store
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ Release process:
1. upload using: `VERSION=x.y.z APIKEY=abc... make upload`
1. test installing the rock from LuaRocks

### 1.6.0 (04-Jun-2024)

- feat: update the AWS SDK to v2.1353.0
[117](https://github.com/Kong/lua-resty-aws/pull/117)

### 1.5.0 (20-May-2024)

Expand Down
2 changes: 1 addition & 1 deletion docs/classes/AWS.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ <h3>Usage:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2024-05-20 09:07:14 </i>
<i style="float:right;">Last updated 2024-06-04 09:33:12 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/classes/ChainableTemporaryCredentials.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ <h3>Usage:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2024-05-20 09:07:14 </i>
<i style="float:right;">Last updated 2024-06-04 09:33:12 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/classes/CredentialProviderChain.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ <h3>Parameters:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2024-05-20 09:07:14 </i>
<i style="float:right;">Last updated 2024-06-04 09:33:12 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/classes/Credentials.html
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ <h3>Returns:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2024-05-20 09:07:14 </i>
<i style="float:right;">Last updated 2024-06-04 09:33:12 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/classes/EC2MetadataCredentials.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ <h3>Parameters:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2024-05-20 09:07:14 </i>
<i style="float:right;">Last updated 2024-06-04 09:33:12 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/classes/EnvironmentCredentials.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ <h3>Parameters:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2024-05-20 09:07:14 </i>
<i style="float:right;">Last updated 2024-06-04 09:33:12 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/classes/RemoteCredentials.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ <h3>Parameters:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2024-05-20 09:07:14 </i>
<i style="float:right;">Last updated 2024-06-04 09:33:12 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/classes/SharedFileCredentials.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ <h3>Parameters:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2024-05-20 09:07:14 </i>
<i style="float:right;">Last updated 2024-06-04 09:33:12 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/classes/TokenFileWebIdentityCredentials.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ <h3>Parameters:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2024-05-20 09:07:14 </i>
<i style="float:right;">Last updated 2024-06-04 09:33:12 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ <h2>Topics</h2>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2024-05-20 09:07:14 </i>
<i style="float:right;">Last updated 2024-06-04 09:33:12 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/resty.aws.config.html
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ <h3>Returns:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2024-05-20 09:07:14 </i>
<i style="float:right;">Last updated 2024-06-04 09:33:12 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/resty.aws.service.rds.signer.html
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ <h3>Usage:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2024-05-20 09:07:14 </i>
<i style="float:right;">Last updated 2024-06-04 09:33:12 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/resty.aws.utils.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ <h3>Returns:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2024-05-20 09:07:14 </i>
<i style="float:right;">Last updated 2024-06-04 09:33:12 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
8 changes: 7 additions & 1 deletion docs/topics/README.md.html
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ <h2>History</h2>
<li>test installing the rock from LuaRocks</li>
</ol>

<h3>1.6.0 (04-Jun-2024)</h3>

<ul>
<li>feat: update the AWS SDK to v2.1353.0
<a href="https://github.com/Kong/lua-resty-aws/pull/117">117</a></li>
</ul>

<h3>1.5.0 (20-May-2024)</h3>

Expand Down Expand Up @@ -563,7 +569,7 @@ <h3>0.1 (03-Feb-2021) Initial released version</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/lunarmodules/LDoc">LDoc 1.5.0</a></i>
<i style="float:right;">Last updated 2024-05-20 09:07:14 </i>
<i style="float:right;">Last updated 2024-06-04 09:33:12 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
124 changes: 124 additions & 0 deletions spec/01-generic/04-stream-decoder_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
local tablex = require "pl.tablex"

describe("stream decoder", function()
setup(function()
AWS = require "resty.aws"
end)

teardown(function()
AWS = nil
package.loaded["resty.aws"] = nil
end)


it("decodes multiple messages from a single chunk", function()
local chunk_in_hex = "000000760000005296d5fade0b3a6576656e742d7479706507000c6d65737361676553746172740d3a636f6e74656e742d747970650700106170706c69636174696f6e2f6a736f6e0d3a6d6573736167652d747970650700056576656e747b22726f6c65223a22617373697374616e74227d6fa8c599000001110000005706f176a90b3a6576656e742d74797065070011636f6e74656e74426c6f636b44656c74610d3a636f6e74656e742d747970650700106170706c69636174696f6e2f6a736f6e0d3a6d6573736167652d747970650700056576656e747b22636f6e74656e74426c6f636b496e646578223a302c2264656c7461223a7b2274657874223a2250692028cf80292069732061206d617468656d61746963616c20636f6e7374616e74207468617420726570726573656e7473207468652063697263756d666572656e63652d746f2d6469616d6574657220726174696f206f66206120636972636c652e204974277320617070726f78696d6174656c7920332e31343135392e227d7d7d4e31940000007d00000056e6680fd60b3a6576656e742d74797065070010636f6e74656e74426c6f636b53746f700d3a636f6e74656e742d747970650700106170706c69636174696f6e2f6a736f6e0d3a6d6573736167652d747970650700056576656e747b22636f6e74656e74426c6f636b496e646578223a307db32e4d340000007a00000051ca2c46650b3a6576656e742d7479706507000b6d65737361676553746f700d3a636f6e74656e742d747970650700106170706c69636174696f6e2f6a736f6e0d3a6d6573736167652d747970650700056576656e747b2273746f70526561736f6e223a22656e645f7475726e227d5fbf09fc000000b90000004ee991d99b0b3a6576656e742d747970650700086d657461646174610d3a636f6e74656e742d747970650700106170706c69636174696f6e2f6a736f6e0d3a6d6573736167652d747970650700056576656e747b226d657472696373223a7b226c6174656e63794d73223a313530367d2c227573616765223a7b22696e707574546f6b656e73223a382c226f7574707574546f6b656e73223a33392c22746f74616c546f6b656e73223a34377d7d5ad2dd7b"
local _STREAM = require("resty.aws.stream")
local parser, err = _STREAM:new(chunk_in_hex, true)

if err then
assert.equal(err, nil)
return
end

local messages = {}

while true do
local msg = parser:next_message()

if not msg then
break
end

messages[#messages+1] = msg
end

assert.same(messages, {
{
body = "{\"role\":\"assistant\"}",
headers = {
{
key = ":event-type",
value = "messageStart"
},
{
key = ":content-type",
value = "application/json"
},
{
key = ":message-type",
value = "event"
}
}
},
{
body = "{\"contentBlockIndex\":0,\"delta\":{\"text\":\"Pi (π) is a mathematical constant that represents the circumference-to-diameter ratio of a circle. It's approximately 3.14159.\"}}",
headers = {
{
key = ":event-type",
value = "contentBlockDelta"
},
{
key = ":content-type",
value = "application/json"
},
{
key = ":message-type",
value = "event"
}
}
},
{
body = "{\"contentBlockIndex\":0}",
headers = {
{
key = ":event-type",
value = "contentBlockStop"
},
{
key = ":content-type",
value = "application/json"
},
{
key = ":message-type",
value = "event"
}
}
},
{
body = "{\"stopReason\":\"end_turn\"}",
headers = {
{
key = ":event-type",
value = "messageStop"
},
{
key = ":content-type",
value = "application/json"
},
{
key = ":message-type",
value = "event"
}
}
},
{
body = "{\"metrics\":{\"latencyMs\":1506},\"usage\":{\"inputTokens\":8,\"outputTokens\":39,\"totalTokens\":47}}",
headers = {
{
key = ":event-type",
value = "metadata"
},
{
key = ":content-type",
value = "application/json"
},
{
key = ":message-type",
value = "event"
}
}
}
})
end)
end)
27 changes: 18 additions & 9 deletions src/resty/aws/request/execute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local json_decode = json_safe.decode
-- * reason: status description
-- * headers: table with response headers
-- * body: string with the raw body
-- * body_reader: if resposne mimetype is eventstream, returns the stream reader handle
--
-- Input parameters:
-- * signed_request table
Expand Down Expand Up @@ -50,15 +51,22 @@ local function execute_request(signed_request)
tostring(err))
end

local body, body_reader

local body do
if response.has_body then
body, err = response:read_body()
if not body then
return nil, ("failed reading response body from '%s:%s': %s"):format(
tostring(signed_request.host),
tostring(signed_request.port),
tostring(err))
if response.headers["application/vnd.amazon.eventstream"] then

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be if response.headers["Content-Type"] == "application/vnd.amazon.eventstream" then?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this locally in my test environment and applied other changes from f0ac80e , works like a charm in my lambda streaming case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DiscreteTom I have made changes to the header format in the stream handler, otherwise we are wasting CPU by scanning for "header_key == x"

Can you check your plugin code again?

Minor change, now headers are:

{
  body = "...",
  headers = {
    key = "value",
    key2 = "value2",
  },
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. I updated my code locally against this change and it is ok. Gonna push to my PR when this lib is released.

body_reader = response.body_reader
else
local this_body do
if response.has_body then
this_body, err = response:read_body()
if not this_body then
return nil, ("failed reading response body from '%s:%s': %s"):format(
tostring(signed_request.host),
tostring(signed_request.port),
tostring(err))
end

body = this_body
end
end
end
Expand All @@ -82,7 +90,8 @@ local function execute_request(signed_request)
status = response.status,
reason = response.reason,
headers = response.headers,
body = body
body = body,
body_reader = body_reader,
}
end

Expand Down
Loading
Loading