diff options
Diffstat (limited to 'libpanto-lua')
| -rw-r--r-- | libpanto-lua/src/module.zig | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/libpanto-lua/src/module.zig b/libpanto-lua/src/module.zig index f905765..8d07b42 100644 --- a/libpanto-lua/src/module.zig +++ b/libpanto-lua/src/module.zig @@ -356,9 +356,10 @@ fn registerMetatables(L: *c.lua_State) void { c.lua_settop(L, c.lua_gettop(L) - 1); if (c.luaL_newmetatable(L, STREAM_MT) != 0) { - c.lua_createtable(L, 0, 2); // methods + c.lua_createtable(L, 0, 3); // methods setMethod(L, "next", streamNext); setMethod(L, "events", streamEvents); + setMethod(L, "reopen", streamReopen); c.lua_setfield(L, -2, "__index"); setMethod(L, "__gc", streamGc); } @@ -1758,6 +1759,19 @@ fn streamIterStep(L_opt: ?*c.lua_State) callconv(.c) c_int { return 1; } +/// `stream:reopen()`: reset a failed stream back to its turn-open boundary so +/// the caller can resume `next()` after changing the agent config (e.g. catch +/// a terminal error, swap the provider config via `agent:set_config(...)`, and +/// retry the SAME turn without re-appending the user message). Errors if the +/// stream has not failed (or is closed). +fn streamReopen(L_opt: ?*c.lua_State) callconv(.c) c_int { + const L = L_opt.?; + const sbox = checkStream(L, 1); + if (sbox.closed) return luaErr(L, "panto: stream is closed"); + sbox.stream.reopen() catch return luaErr(L, "panto: stream reopen failed"); + return 0; +} + fn streamGc(L_opt: ?*c.lua_State) callconv(.c) c_int { const L = L_opt.?; const sbox = checkStream(L, 1); |
