fix(ai): sort sessions by recency

This commit is contained in:
Guillaume ARM 2026-06-09 05:42:53 +02:00
parent 8467c30dd8
commit 5b46662f2a
7 changed files with 29 additions and 11 deletions

View File

@ -1,4 +1,4 @@
local _VERSION = '0.4.0'; local _VERSION = '0.4.1';
local PING_PROMPT = 'reply with exactly: pong'; local PING_PROMPT = 'reply with exactly: pong';
@ -56,6 +56,13 @@ local function extractTextParts(parts)
return table.concat(texts, ''); return table.concat(texts, '');
end end
local function sessionTime(session)
if type(session) ~= 'table' or type(session.time) ~= 'table' then
return 0;
end
return tonumber(session.time.updated or session.time.created) or 0;
end
local function createAi(opts) local function createAi(opts)
opts = opts or {}; opts = opts or {};
@ -153,6 +160,9 @@ local function createAi(opts)
if type(decoded) ~= 'table' then if type(decoded) ~= 'table' then
return false, 'reponse invalide'; return false, 'reponse invalide';
end end
table.sort(decoded, function(a, b)
return sessionTime(a) > sessionTime(b);
end);
return true, decoded; return true, decoded;
end end

View File

@ -1,6 +1,6 @@
{ {
"name": "TrapOS", "name": "TrapOS",
"version": "0.5.4", "version": "0.5.5",
"branch": "next", "branch": "next",
"packages": [ "packages": [
"trapos" "trapos"

View File

@ -5,7 +5,7 @@
"trapos-boot": "0.2.1", "trapos-boot": "0.2.1",
"trapos-net": "0.2.0", "trapos-net": "0.2.0",
"trapos-ui": "0.2.1", "trapos-ui": "0.2.1",
"trapos-ai": "0.4.0", "trapos-ai": "0.4.1",
"trapos": "0.5.4" "trapos": "0.5.5"
} }
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "trapos-ai", "name": "trapos-ai",
"version": "0.4.0", "version": "0.4.1",
"description": "TrapOS AI client for opencode serve", "description": "TrapOS AI client for opencode serve",
"dependencies": ["trapos-core"], "dependencies": ["trapos-core"],
"files": [ "files": [

View File

@ -1,6 +1,6 @@
{ {
"name": "trapos", "name": "trapos",
"version": "0.5.4", "version": "0.5.5",
"description": "TrapOS full install meta-package", "description": "TrapOS full install meta-package",
"dependencies": ["trapos-boot", "trapos-net", "trapos-ui", "trapos-test", "trapos-ai"], "dependencies": ["trapos-boot", "trapos-net", "trapos-ui", "trapos-test", "trapos-ai"],
"files": [], "files": [],

View File

@ -1,4 +1,4 @@
local _VERSION = '0.4.0'; local _VERSION = '0.4.1';
local createAi = require('/apis/libai'); local createAi = require('/apis/libai');

View File

@ -104,8 +104,13 @@ end);
-- listSessions -- -- listSessions --
testlib.test('listSessions returns parsed session list', function() testlib.test('listSessions returns newest sessions first', function()
local sessions = { { id = 'ses_1', title = 'hello' }, { id = 'ses_2', title = 'world' } }; local sessions = {
{ id = 'ses_old', title = 'old', time = { updated = 10 } },
{ id = 'ses_created', title = 'created', time = { created = 20 } },
{ id = 'ses_new', title = 'new', time = { updated = 30 } },
{ id = 'ses_missing', title = 'missing' },
};
local httpStub = fakeHttp({}, { response(200, textutils.serializeJSON(sessions)) }); local httpStub = fakeHttp({}, { response(200, textutils.serializeJSON(sessions)) });
local settingsStub = fakeSettings({ ['opencc.server_url'] = 'http://host' }); local settingsStub = fakeSettings({ ['opencc.server_url'] = 'http://host' });
local ai = createAi({ http = httpStub, settings = settingsStub }); local ai = createAi({ http = httpStub, settings = settingsStub });
@ -113,8 +118,11 @@ testlib.test('listSessions returns parsed session list', function()
local ok, result = ai.listSessions(); local ok, result = ai.listSessions();
testlib.assertTrue(ok, tostring(result)); testlib.assertTrue(ok, tostring(result));
testlib.assertEquals(#result, 2); testlib.assertEquals(#result, 4);
testlib.assertEquals(result[1].id, 'ses_1'); testlib.assertEquals(result[1].id, 'ses_new');
testlib.assertEquals(result[2].id, 'ses_created');
testlib.assertEquals(result[3].id, 'ses_old');
testlib.assertEquals(result[4].id, 'ses_missing');
end); end);
testlib.test('listSessions fails when server_url missing', function() testlib.test('listSessions fails when server_url missing', function()