Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

feat(bot):Support user-key OpenViking mode and align memory namespaces (#1994)

DuTao6312e1e12e8e
diff --git a/.ingest_record.json b/.ingest_record.json
deleted file mode 100644
index 04e4753dd..000000000
--- a/.ingest_record.json
+++ /dev/null
@@ -1,890 +0,0 @@
-{
- "viking:conv-26:session_1": {
- "success": true,
- "timestamp": 1774706688,
- "meta": {
- "date_time": "1:56 pm on 8 May, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_2": {
- "success": true,
- "timestamp": 1774706715,
- "meta": {
- "date_time": "1:14 pm on 25 May, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_3": {
- "success": true,
- "timestamp": 1774706739,
- "meta": {
- "date_time": "7:55 pm on 9 June, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_4": {
- "success": true,
- "timestamp": 1774706761,
- "meta": {
- "date_time": "10:37 am on 27 June, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_5": {
- "success": true,
- "timestamp": 1774706784,
- "meta": {
- "date_time": "1:36 pm on 3 July, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_6": {
- "success": true,
- "timestamp": 1774706808,
- "meta": {
- "date_time": "8:18 pm on 6 July, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_7": {
- "success": true,
- "timestamp": 1774706834,
- "meta": {
- "date_time": "4:33 pm on 12 July, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_8": {
- "success": true,
- "timestamp": 1774706855,
- "meta": {
- "date_time": "1:51 pm on 15 July, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_9": {
- "success": true,
- "timestamp": 1774706879,
- "meta": {
- "date_time": "2:31 pm on 17 July, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_10": {
- "success": true,
- "timestamp": 1774706903,
- "meta": {
- "date_time": "8:56 pm on 20 July, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_11": {
- "success": true,
- "timestamp": 1774706923,
- "meta": {
- "date_time": "2:24 pm on 14 August, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_12": {
- "success": true,
- "timestamp": 1774706941,
- "meta": {
- "date_time": "1:50 pm on 17 August, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_13": {
- "success": true,
- "timestamp": 1774706962,
- "meta": {
- "date_time": "3:31 pm on 23 August, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_14": {
- "success": true,
- "timestamp": 1774706983,
- "meta": {
- "date_time": "1:33 pm on 25 August, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_15": {
- "success": true,
- "timestamp": 1774707002,
- "meta": {
- "date_time": "3:19 pm on 28 August, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_16": {
- "success": true,
- "timestamp": 1774707021,
- "meta": {
- "date_time": "12:09 am on 13 September, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_17": {
- "success": true,
- "timestamp": 1774707033,
- "meta": {
- "date_time": "10:31 am on 13 October, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_18": {
- "success": true,
- "timestamp": 1774707054,
- "meta": {
- "date_time": "6:55 pm on 20 October, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-26:session_19": {
- "success": true,
- "timestamp": 1774707075,
- "meta": {
- "date_time": "9:55 am on 22 October, 2023",
- "speakers": "Caroline & Melanie"
- }
- },
- "viking:conv-30:session_1": {
- "success": true,
- "timestamp": 1774707088,
- "meta": {
- "date_time": "4:04 pm on 20 January, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_2": {
- "success": true,
- "timestamp": 1774707112,
- "meta": {
- "date_time": "2:32 pm on 29 January, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_3": {
- "success": true,
- "timestamp": 1774707134,
- "meta": {
- "date_time": "12:48 am on 1 February, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_4": {
- "success": true,
- "timestamp": 1774707142,
- "meta": {
- "date_time": "10:43 am on 4 February, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_5": {
- "success": true,
- "timestamp": 1774707157,
- "meta": {
- "date_time": "9:32 am on 8 February, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_6": {
- "success": true,
- "timestamp": 1774707169,
- "meta": {
- "date_time": "2:35 pm on 16 March, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_7": {
- "success": true,
- "timestamp": 1774707184,
- "meta": {
- "date_time": "7:28 pm on 23 March, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_8": {
- "success": true,
- "timestamp": 1774707200,
- "meta": {
- "date_time": "1:26 pm on 3 April, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_9": {
- "success": true,
- "timestamp": 1774707218,
- "meta": {
- "date_time": "10:33 am on 9 April, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_10": {
- "success": true,
- "timestamp": 1774707228,
- "meta": {
- "date_time": "11:24 am on 25 April, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_11": {
- "success": true,
- "timestamp": 1774707242,
- "meta": {
- "date_time": "3:14 pm on 11 May, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_12": {
- "success": true,
- "timestamp": 1774707255,
- "meta": {
- "date_time": "7:18 pm on 27 May, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_13": {
- "success": true,
- "timestamp": 1774707263,
- "meta": {
- "date_time": "8:29 pm on 13 June, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_14": {
- "success": true,
- "timestamp": 1774707278,
- "meta": {
- "date_time": "9:38 pm on 16 June, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_15": {
- "success": true,
- "timestamp": 1774707290,
- "meta": {
- "date_time": "10:04 am on 19 June, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_16": {
- "success": true,
- "timestamp": 1774707303,
- "meta": {
- "date_time": "2:15 pm on 21 June, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_17": {
- "success": true,
- "timestamp": 1774707314,
- "meta": {
- "date_time": "1:25 pm on 9 July, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_18": {
- "success": true,
- "timestamp": 1774707326,
- "meta": {
- "date_time": "5:44 pm on 21 July, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-30:session_19": {
- "success": true,
- "timestamp": 1774700596,
- "meta": {
- "date_time": "6:46 pm on 23 July, 2023",
- "speakers": "Jon & Gina"
- }
- },
- "viking:conv-41:session_1": {
- "success": true,
- "timestamp": 1774700612,
- "meta": {
- "date_time": "11:01 am on 17 December, 2022",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_2": {
- "success": true,
- "timestamp": 1774700629,
- "meta": {
- "date_time": "6:10 pm on 22 December, 2022",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_3": {
- "success": true,
- "timestamp": 1774700649,
- "meta": {
- "date_time": "8:30 pm on 1 January, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_4": {
- "success": true,
- "timestamp": 1774700665,
- "meta": {
- "date_time": "7:06 pm on 9 January, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_5": {
- "success": true,
- "timestamp": 1774700681,
- "meta": {
- "date_time": "1:17 pm on 28 January, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_6": {
- "success": true,
- "timestamp": 1774700697,
- "meta": {
- "date_time": "2:33 pm on 5 February, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_7": {
- "success": true,
- "timestamp": 1774700711,
- "meta": {
- "date_time": "8:55 pm on 25 February, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_8": {
- "success": true,
- "timestamp": 1774700732,
- "meta": {
- "date_time": "6:03 pm on 6 March, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_9": {
- "success": true,
- "timestamp": 1774700746,
- "meta": {
- "date_time": "9:36 am on 2 April, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_10": {
- "success": true,
- "timestamp": 1774700763,
- "meta": {
- "date_time": "12:24 am on 7 April, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_11": {
- "success": true,
- "timestamp": 1774700780,
- "meta": {
- "date_time": "6:13 pm on 10 April, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_12": {
- "success": true,
- "timestamp": 1774700797,
- "meta": {
- "date_time": "7:34 pm on 18 April, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_13": {
- "success": true,
- "timestamp": 1774700813,
- "meta": {
- "date_time": "3:18 pm on 4 May, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_14": {
- "success": true,
- "timestamp": 1774700828,
- "meta": {
- "date_time": "5:04 pm on 6 May, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_15": {
- "success": true,
- "timestamp": 1774700844,
- "meta": {
- "date_time": "7:38 pm on 20 May, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_16": {
- "success": true,
- "timestamp": 1774700859,
- "meta": {
- "date_time": "1:24 pm on 25 May, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_17": {
- "success": true,
- "timestamp": 1774700879,
- "meta": {
- "date_time": "11:51 am on 3 June, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_18": {
- "success": true,
- "timestamp": 1774700896,
- "meta": {
- "date_time": "2:47 pm on 12 June, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_19": {
- "success": true,
- "timestamp": 1774700913,
- "meta": {
- "date_time": "7:20 pm on 16 June, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_20": {
- "success": true,
- "timestamp": 1774700929,
- "meta": {
- "date_time": "12:21 am on 27 June, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_21": {
- "success": true,
- "timestamp": 1774700946,
- "meta": {
- "date_time": "8:43 pm on 3 July, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_22": {
- "success": true,
- "timestamp": 1774700965,
- "meta": {
- "date_time": "6:59 pm on 5 July, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_23": {
- "success": true,
- "timestamp": 1774700979,
- "meta": {
- "date_time": "6:29 pm on 7 July, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_24": {
- "success": true,
- "timestamp": 1774700993,
- "meta": {
- "date_time": "3:34 pm on 17 July, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_25": {
- "success": true,
- "timestamp": 1774701015,
- "meta": {
- "date_time": "6:21 pm on 22 July, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_26": {
- "success": true,
- "timestamp": 1774701033,
- "meta": {
- "date_time": "1:59 pm on 31 July, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_27": {
- "success": true,
- "timestamp": 1774701047,
- "meta": {
- "date_time": "6:20 pm on 3 August, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_28": {
- "success": true,
- "timestamp": 1774701065,
- "meta": {
- "date_time": "5:19 pm on 5 August, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_29": {
- "success": true,
- "timestamp": 1774701082,
- "meta": {
- "date_time": "8:06 pm on 9 August, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_30": {
- "success": true,
- "timestamp": 1774701098,
- "meta": {
- "date_time": "12:10 am on 11 August, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_31": {
- "success": true,
- "timestamp": 1774701118,
- "meta": {
- "date_time": "3:14 pm on 13 August, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-41:session_32": {
- "success": true,
- "timestamp": 1774701134,
- "meta": {
- "date_time": "11:08 am on 16 August, 2023",
- "speakers": "John & Maria"
- }
- },
- "viking:conv-42:session_1": {
- "success": true,
- "timestamp": 1774701152,
- "meta": {
- "date_time": "7:31 pm on 21 January, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_2": {
- "success": true,
- "timestamp": 1774701172,
- "meta": {
- "date_time": "2:01 pm on 23 January, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_3": {
- "success": true,
- "timestamp": 1774701199,
- "meta": {
- "date_time": "9:27 am on 7 February, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_4": {
- "success": true,
- "timestamp": 1774701218,
- "meta": {
- "date_time": "1:07 pm on 25 February, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_5": {
- "success": true,
- "timestamp": 1774701238,
- "meta": {
- "date_time": "6:59 pm on 18 March, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_6": {
- "success": true,
- "timestamp": 1774701260,
- "meta": {
- "date_time": "1:43 pm on 24 March, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_7": {
- "success": true,
- "timestamp": 1774701281,
- "meta": {
- "date_time": "7:37 pm on 15 April, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_8": {
- "success": true,
- "timestamp": 1774701303,
- "meta": {
- "date_time": "6:44 pm on 17 April, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_9": {
- "success": true,
- "timestamp": 1774701325,
- "meta": {
- "date_time": "7:44 pm on 21 April, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_10": {
- "success": true,
- "timestamp": 1774701341,
- "meta": {
- "date_time": "11:54 am on 2 May, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_11": {
- "success": true,
- "timestamp": 1774701365,
- "meta": {
- "date_time": "3:35 pm on 12 May, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_12": {
- "success": true,
- "timestamp": 1774701386,
- "meta": {
- "date_time": "7:49 pm on 20 May, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_13": {
- "success": true,
- "timestamp": 1774701403,
- "meta": {
- "date_time": "3:00 pm on 25 May, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_14": {
- "success": true,
- "timestamp": 1774701427,
- "meta": {
- "date_time": "5:44 pm on 3 June, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_15": {
- "success": true,
- "timestamp": 1774701448,
- "meta": {
- "date_time": "2:12 pm on 5 June, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_16": {
- "success": true,
- "timestamp": 1774701466,
- "meta": {
- "date_time": "10:55 am on 24 June, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_17": {
- "success": true,
- "timestamp": 1774701484,
- "meta": {
- "date_time": "2:34 pm on 10 July, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_18": {
- "success": true,
- "timestamp": 1774701506,
- "meta": {
- "date_time": "6:12 pm on 14 August, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_19": {
- "success": true,
- "timestamp": 1774701523,
- "meta": {
- "date_time": "10:57 am on 22 August, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_20": {
- "success": true,
- "timestamp": 1774701545,
- "meta": {
- "date_time": "6:03 pm on 5 September, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_21": {
- "success": true,
- "timestamp": 1774701560,
- "meta": {
- "date_time": "1:43 pm on 14 September, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_22": {
- "success": true,
- "timestamp": 1774701582,
- "meta": {
- "date_time": "11:15 am on 6 October, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_23": {
- "success": true,
- "timestamp": 1774701604,
- "meta": {
- "date_time": "10:58 am on 9 October, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_24": {
- "success": true,
- "timestamp": 1774701629,
- "meta": {
- "date_time": "2:01 pm on 21 October, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_25": {
- "success": true,
- "timestamp": 1774701653,
- "meta": {
- "date_time": "8:16 pm on 25 October, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_26": {
- "success": true,
- "timestamp": 1774701672,
- "meta": {
- "date_time": "3:56 pm on 4 November, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_27": {
- "success": true,
- "timestamp": 1774701692,
- "meta": {
- "date_time": "8:10 pm on 7 November, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_28": {
- "success": true,
- "timestamp": 1774701720,
- "meta": {
- "date_time": "5:54 pm on 9 November, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-42:session_29": {
- "success": true,
- "timestamp": 1774701741,
- "meta": {
- "date_time": "12:06 am on 11 November, 2022",
- "speakers": "Joanna & Nate"
- }
- },
- "viking:conv-43:session_1": {
- "success": true,
- "timestamp": 1774701771,
- "meta": {
- "date_time": "7:48 pm on 21 May, 2023",
- "speakers": "Tim & John"
- }
- },
- "viking:conv-43:session_2": {
- "success": true,
- "timestamp": 1774701796,
- "meta": {
- "date_time": "5:08 pm on 15 June, 2023",
- "speakers": "Tim & John"
- }
- },
- "viking:conv-43:session_3": {
- "success": true,
- "timestamp": 1774701820,
- "meta": {
- "date_time": "4:21 pm on 16 July, 2023",
- "speakers": "Tim & John"
- }
- },
- "viking:conv-43:session_4": {
- "success": true,
- "timestamp": 1774701840,
- "meta": {
- "date_time": "4:17 pm on 2 August, 2023",
- "speakers": "Tim & John"
- }
- },
- "viking:conv-43:session_5": {
- "success": true,
- "timestamp": 1774701865,
- "meta": {
- "date_time": "10:29 am on 9 August, 2023",
- "speakers": "Tim & John"
- }
- },
- "viking:conv-43:session_6": {
- "success": true,
- "timestamp": 1774701884,
- "meta": {
- "date_time": "1:08 pm on 11 August, 2023",
- "speakers": "Tim & John"
- }
- },
- "viking:conv-43:session_7": {
- "success": true,
- "timestamp": 1774701908,
- "meta": {
- "date_time": "7:54 pm on 17 August, 2023",
- "speakers": "Tim & John"
- }
- },
- "viking:conv-43:session_8": {
- "success": true,
- "timestamp": 1774701934,
- "meta": {
- "date_time": "4:29 pm on 21 August, 2023",
- "speakers": "Tim & John"
- }
- },
- "viking:conv-43:session_9": {
- "success": true,
- "timestamp": 1774701961,
- "meta": {
- "date_time": "6:59 pm on 26 August, 2023",
- "speakers": "Tim & John"
- }
- },
- "viking:conv-43:session_10": {
- "success": true,
- "timestamp": 1774701982,
- "meta": {
- "date_time": "2:52 pm on 31 August, 2023",
- "speakers": "Tim & John"
- }
- },
- "viking:conv-43:session_11": {
- "success": true,
- "timestamp": 1774701999,
- "meta": {
- "date_time": "8:17 pm on 21 September, 2023",
- "speakers": "Tim & John"
- }
- },
- "viking:conv-43:session_12": {
- "success": true,
- "timestamp": 1774702021,
- "meta": {
- "date_time": "3:00 pm on 2 October, 2023",
- "speakers": "Tim & John"
- }
- }
-}
\ No newline at end of file
diff --git a/benchmark/locomo/openclaw/eval.py b/benchmark/locomo/openclaw/eval.py
index 2d6946096..025362f08 100644
--- a/benchmark/locomo/openclaw/eval.py
+++ b/benchmark/locomo/openclaw/eval.py
@@ -20,19 +20,23 @@ import argparse
import csv
import json
import os
+import shutil
+import subprocess
import sys
import time
+import uuid
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import datetime
from pathlib import Path
from threading import Lock
+from urllib.parse import urlparse
import requests
# Configuration constants
DEFAULT_BASE_URL = "http://127.0.0.1:18789"
DEFAULT_AGENT_ID = "locomo-eval"
-DEFAULT_INGEST_RECORD_PATH = ".ingest_record.json"
+DEFAULT_INGEST_RECORD_PATH = ".ingest_record.csv"
# CSV write lock for thread safety
csv_lock = Lock()
@@ -170,7 +174,7 @@ def build_session_messages(
dt_key = f"{sk}_date_time"
date_time = conv.get(dt_key, "")
- parts = [f"[group chat conversation: {date_time}]"]
+ parts = [f"Remember to your memory, [group chat conversation: {date_time}]"]
for msg in conv[sk]:
parts.append(format_locomo_message(msg))
if tail:
@@ -248,28 +252,39 @@ def get_sample_question_time(sample: dict) -> str | None:
# ---------------------------------------------------------------------------
-def load_ingest_record(record_path: str = DEFAULT_INGEST_RECORD_PATH) -> dict:
- """Load existing ingest record file, return empty dict if not exists."""
+def load_ingest_record(record_path: str = DEFAULT_INGEST_RECORD_PATH) -> set[tuple[str, str, str, str]]:
+ """Load existing ingest record CSV, return empty set if not exists."""
+ records = set()
try:
- with open(record_path, "r", encoding="utf-8") as f:
- return json.load(f)
+ with open(record_path, "r", encoding="utf-8", newline="") as f:
+ reader = csv.DictReader(f)
+ for row in reader:
+ records.add(
+ (
+ row["agent_id"],
+ row["user_key"],
+ row["sample_id"],
+ row["session_key"],
+ )
+ )
except FileNotFoundError:
- return {}
- except json.JSONDecodeError as e:
- print(f"Warning: Error parsing ingest record: {e}, starting fresh", file=sys.stderr)
- return {}
+ return set()
+ except (csv.Error, KeyError) as e:
+ print(f"Warning: Error parsing ingest record CSV: {e}, starting fresh", file=sys.stderr)
+ return set()
except IOError as e:
- print(f"Warning: Error reading ingest record: {e}, starting fresh", file=sys.stderr)
- return {}
+ print(f"Warning: Error reading ingest record CSV: {e}, starting fresh", file=sys.stderr)
+ return set()
+ return records
-def save_ingest_record(record: dict, record_path: str = DEFAULT_INGEST_RECORD_PATH) -> None:
- """Save ingest record to file."""
+def clear_ingest_record(record_path: str = DEFAULT_INGEST_RECORD_PATH) -> None:
+ """Clear ingest record CSV file."""
try:
- with open(record_path, "w", encoding="utf-8") as f:
- json.dump(record, f, indent=2, ensure_ascii=False)
+ if os.path.exists(record_path):
+ os.remove(record_path)
except IOError as e:
- print(f"Warning: Error saving ingest record: {e}", file=sys.stderr)
+ print(f"Warning: Error clearing ingest record CSV: {e}", file=sys.stderr)
def is_already_ingested(
@@ -277,11 +292,10 @@ def is_already_ingested(
user_key: str,
sample_id: str | int,
session_key: str,
- record: dict,
+ record: set[tuple[str, str, str, str]],
) -> bool:
"""Check if a specific session has already been successfully ingested."""
- key = f"{agent_id}:{user_key}:{sample_id}:{session_key}"
- return key in record and record[key].get("success", False)
+ return (agent_id, user_key, str(sample_id), session_key) in record
def mark_ingested(
@@ -289,16 +303,50 @@ def mark_ingested(
user_key: str,
sample_id: str | int,
session_key: str,
- record: dict,
+ record_path: str = DEFAULT_INGEST_RECORD_PATH,
meta: dict | None = None,
) -> None:
- """Mark a session as successfully ingested."""
- key = f"{agent_id}:{user_key}:{sample_id}:{session_key}"
- record[key] = {
- "success": True,
- "timestamp": int(time.time()),
- "meta": meta or {},
- }
+ """Append a successfully ingested session to the CSV record."""
+ fieldnames = [
+ "agent_id",
+ "user_key",
+ "sample_id",
+ "session_key",
+ "timestamp",
+ "date_time",
+ "mode",
+ "input_tokens",
+ "output_tokens",
+ "cacheRead",
+ "cacheWrite",
+ "total_tokens",
+ ]
+ meta = meta or {}
+ usage = meta.get("usage", {})
+ file_exists = os.path.exists(record_path)
+ try:
+ with open(record_path, "a", encoding="utf-8", newline="") as f:
+ writer = csv.DictWriter(f, fieldnames=fieldnames)
+ if not file_exists:
+ writer.writeheader()
+ writer.writerow(
+ {
+ "agent_id": agent_id,
+ "user_key": user_key,
+ "sample_id": str(sample_id),
+ "session_key": session_key,
+ "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
+ "date_time": meta.get("date_time", ""),
+ "mode": meta.get("mode", ""),
+ "input_tokens": usage.get("input_tokens", 0),
+ "output_tokens": usage.get("output_tokens", 0),
+ "cacheRead": usage.get("cacheRead", 0),
+ "cacheWrite": usage.get("cacheWrite", 0),
+ "total_tokens": usage.get("total_tokens", 0),
+ }
+ )
+ except (csv.Error, IOError) as e:
+ print(f"Warning: Error saving ingest record CSV: {e}", file=sys.stderr)
# ---------------------------------------------------------------------------
@@ -539,6 +587,163 @@ def send_message(
return extract_response_text(body), usage
+def resolve_gateway_ws_url(base_url: str) -> str:
+ """Convert HTTP gateway base URL to WS gateway endpoint URL."""
+ parsed = urlparse(base_url)
+ scheme = parsed.scheme.lower()
+
+ if scheme in ("ws", "wss"):
+ ws_scheme = scheme
+ elif scheme == "http":
+ ws_scheme = "ws"
+ elif scheme == "https":
+ ws_scheme = "wss"
+ else:
+ raise RuntimeError(f"Unsupported gateway base URL scheme: {parsed.scheme}")
+
+ path = parsed.path or ""
+ if not path or path == "/":
+ ws_path = "/ws"
+ elif path.endswith("/ws"):
+ ws_path = path
+ else:
+ ws_path = f"{path.rstrip('/')}/ws"
+
+ return f"{ws_scheme}://{parsed.netloc}{ws_path}"
+
+
+def build_openresponses_main_session_key(agent_id: str, user: str) -> str:
+ """Build the main session key used by /v1/responses when user is provided."""
+ normalized_agent = (agent_id or DEFAULT_AGENT_ID).strip().lower()
+ normalized_user = (user or "").strip().lower()
+ return f"agent:{normalized_agent}:openresponses-user:{normalized_user}"
+
+
+def parse_json_from_cli_output(stdout: str) -> dict:
+ """Parse JSON payload from mixed CLI output (logs + JSON)."""
+ text = (stdout or "").strip()
+ if not text:
+ raise RuntimeError("Empty output from openclaw gateway call")
+
+ try:
+ parsed = json.loads(text)
+ if isinstance(parsed, dict):
+ return parsed
+ except json.JSONDecodeError:
+ pass
+
+ decoder = json.JSONDecoder()
+ last_obj: dict | None = None
+
+ for idx, ch in enumerate(text):
+ if ch != "{":
+ continue
+ try:
+ parsed, consumed = decoder.raw_decode(text[idx:])
+ except json.JSONDecodeError:
+ continue
+ if not isinstance(parsed, dict):
+ continue
+
+ tail = text[idx + consumed :].strip()
+ if not tail:
+ return parsed
+
+ last_obj = parsed
+
+ if last_obj is not None:
+ return last_obj
+
+ raise RuntimeError(f"Failed to parse JSON from gateway call output: {text[:500]}")
+
+
+def gateway_call(
+ base_url: str,
+ token: str,
+ method: str,
+ params: dict,
+ timeout_ms: int = 30_000,
+) -> dict:
+ """Call gateway RPC through `openclaw gateway call` CLI."""
+ ws_url = resolve_gateway_ws_url(base_url)
+ cmd = [
+ "openclaw",
+ "gateway",
+ "call",
+ method,
+ "--json",
+ "--url",
+ ws_url,
+ "--token",
+ token,
+ "--timeout",
+ str(timeout_ms),
+ "--params",
+ json.dumps(params, ensure_ascii=False),
+ ]
+
+ try:
+ completed = subprocess.run(
+ cmd,
+ capture_output=True,
+ text=True,
+ timeout=max(60, int(timeout_ms / 1000) + 30),
+ check=False,
+ )
+ except FileNotFoundError as e:
+ raise RuntimeError(
+ "`openclaw` CLI not found in PATH; cannot call gateway chat.send"
+ ) from e
+ except subprocess.TimeoutExpired as e:
+ raise RuntimeError(f"gateway call timeout for method {method}: {e}") from e
+
+ if completed.returncode != 0:
+ stderr = (completed.stderr or "").strip()
+ stdout = (completed.stdout or "").strip()
+ raise RuntimeError(
+ f"gateway call failed for {method} (exit {completed.returncode}): {stderr or stdout or 'unknown error'}"
+ )
+
+ return parse_json_from_cli_output(completed.stdout)
+
+
+def compact_via_chat_send(
+ base_url: str,
+ token: str,
+ agent_id: str,
+ user: str,
+ wait_timeout_ms: int = 120_000,
+) -> tuple[dict, dict]:
+ """Trigger semantic compaction through gateway chat.send('/compact')."""
+ session_key = build_openresponses_main_session_key(agent_id, user)
+ chat_send_params = {
+ "sessionKey": session_key,
+ "message": "/compact",
+ "idempotencyKey": str(uuid.uuid4()),
+ }
+
+ ack = gateway_call(
+ base_url=base_url,
+ token=token,
+ method="chat.send",
+ params=chat_send_params,
+ timeout_ms=60_000,
+ )
+ run_id = str(ack.get("runId", "")).strip()
+ if not run_id:
+ raise RuntimeError(f"chat.send ack missing runId: {ack}")
+
+ wait = gateway_call(
+ base_url=base_url,
+ token=token,
+ method="agent.wait",
+ params={"runId": run_id, "timeoutMs": wait_timeout_ms},
+ timeout_ms=wait_timeout_ms + 60_000,
+ )
+
+ return ack, wait
+
+
# ---------------------------------------------------------------------------
# Ingest: load conversations into openclaw
# ---------------------------------------------------------------------------
@@ -551,8 +756,8 @@ def run_ingest(
# Handle ingest record operations
if args.clear_ingest_record:
- ingest_record = {}
- save_ingest_record(ingest_record)
+ clear_ingest_record()
+ ingest_record = set()
print("[INFO] All existing ingest records cleared", file=sys.stderr)
else:
ingest_record = load_ingest_record()
@@ -561,6 +766,9 @@ def run_ingest(
samples = load_locomo_data(args.input, args.sample)
results = []
skipped_count = 0
+ if shutil.which("openclaw") is None:
+ print("[ERROR] openclaw CLI not found in PATH", file=sys.stderr)
+ sys.exit(1)
for item in samples:
sample_id = item["sample_id"]
@@ -596,7 +804,20 @@ def run_ingest(
reply, usage = send_message(
args.base_url, args.token, user_key, msg, args.agent_id
)
+
print(f" -> {reply[:80]}{'...' if len(reply) > 80 else ''}", file=sys.stderr)
+ compact_ack, compact_wait = compact_via_chat_send(
+ args.base_url,
+ args.token,
+ args.agent_id,
+ user_key,
+ )
+ compact_status = compact_wait.get("status", "unknown")
+ compact_run_id = compact_ack.get("runId", "")
+ print(
+ f" -> compact(chat.send): runId={compact_run_id} status={compact_status}",
+ file=sys.stderr,
+ )
results.append(
{
"sample_id": sample_id,
@@ -612,8 +833,10 @@ def run_ingest(
user_key,
sample_id,
meta["session_key"],
- ingest_record,
- {"mode": "openclaw", "date_time": meta["date_time"], "usage": usage},
+ meta={"mode": "openclaw", "date_time": meta["date_time"], "usage": usage},
+ )
+ ingest_record.add(
+ (args.agent_id, user_key, str(sample_id), meta["session_key"])
)
except Exception as e:
print(f" -> [ERROR] {e}", file=sys.stderr)
@@ -647,8 +870,6 @@ def run_ingest(
except IOError as e:
print(f"Warning: Error writing output files: {e}", file=sys.stderr)
- # Save ingest record
- save_ingest_record(ingest_record)
total_processed = len(results) + skipped_count
print("\n=== Ingest summary ===", file=sys.stderr)
print(f"Total sessions: {total_processed}", file=sys.stderr)
diff --git a/benchmark/locomo/vikingbot/import_to_ov.py b/benchmark/locomo/vikingbot/import_to_ov.py
index 0144e81cd..0a1759935 100644
--- a/benchmark/locomo/vikingbot/import_to_ov.py
+++ b/benchmark/locomo/vikingbot/import_to_ov.py
@@ -395,13 +395,16 @@ async def process_single_session(
ingest_record = ingest_record or {}
csv_id = display_id or str(sample_id)
try:
+ user_id = str(sample_id) if args.separate_user_by_sample else ""
+ agent_id = str(sample_id) if args.separate_user_by_sample else ""
+ account = args.account
result = await viking_ingest(
messages,
args.openviking_url,
meta.get("date_time"),
- user_id=csv_id,
- agent_id=f"agent_{csv_id}",
- account=args.account,
+ user_id=user_id,
+ agent_id=agent_id,
+ account=account,
)
token_usage = result["token_usage"]
task_id = result.get("task_id")
@@ -655,9 +658,19 @@ async def run_import(args: argparse.Namespace) -> None:
elif res.get("status") == "error":
error_count += 1
- # 不同 sample 之间并行执行
- tasks = [asyncio.create_task(process_sample(item, idx)) for idx, item in enumerate(samples)]
- results = await asyncio.gather(*tasks, return_exceptions=True)
+ if args.parallel_samples:
+ semaphore = asyncio.Semaphore(args.parallel_samples)
+
+ async def process_sample_with_limit(item, sample_index):
+ async with semaphore:
+ await process_sample(item, sample_index)
+
+ tasks = [
+ asyncio.create_task(process_sample_with_limit(item, idx))
+ for idx, item in enumerate(samples)
+ ]
+ else:
+ tasks = [asyncio.create_task(process_sample(item, idx)) for idx, item in enumerate(samples)]
else:
# Plain text format
@@ -798,6 +811,18 @@ def main():
default=None,
help="LoCoMo JSON: question index (0-based). When specified, auto-detect required sessions from question's evidence.",
)
+ parser.add_argument(
+ "--separate-user-by-sample",
+ action=argparse.BooleanOptionalAction,
+ default=True,
+ help="Whether to isolate OpenViking user/agent IDs by sample (default: true). Use --no-separate-user-by-sample to use empty user/agent while keeping --account unchanged.",
+ )
+ parser.add_argument(
+ "--parallel-samples",
+ type=int,
+ default=None,
+ help="Max number of samples to import concurrently. Default: no limit; create one task per sample.",
+ )
parser.add_argument(
"--force-ingest",
action="store_true",
diff --git a/benchmark/locomo/vikingbot/run_eval.py b/benchmark/locomo/vikingbot/run_eval.py
index 575b0028c..0e2991b3d 100644
--- a/benchmark/locomo/vikingbot/run_eval.py
+++ b/benchmark/locomo/vikingbot/run_eval.py
@@ -196,6 +196,7 @@ def load_locomo_qa(
"evidence_text": get_evidence_text(evidence_list, sample),
"question_time": question_time,
"speakers": speakers,
+ "original_sample_id": original_id,
"is_invalid": qa["question"] in invalid_questions
if invalid_questions
else False,
@@ -217,6 +218,7 @@ def load_locomo_qa(
"evidence_text": get_evidence_text(evidence_list, sample),
"question_time": question_time,
"speakers": speakers,
+ "original_sample_id": original_id,
"is_invalid": qa["question"] in invalid_questions
if invalid_questions
else False,
@@ -233,14 +235,16 @@ def run_vikingbot_chat(
question_time: str | None = None,
sample_id: str | None = None,
question_id: str | None = None,
+ config: str | None = None,
memory_users: list[str] | None = None,
) -> tuple[str, dict, float, int, list]:
"""执行vikingbot chat命令,返回回答、token使用情况、耗时(秒)、迭代次数、使用的工具列表"""
# 先执行 /new 命令清除会话
if sample_id:
- new_cmd = [
- "vikingbot",
- "chat",
+ new_cmd = ["vikingbot", "chat"]
+ if config:
+ new_cmd.extend(["--config", config])
+ new_cmd.extend([
"-m",
"/new",
"-e",
@@ -248,8 +252,7 @@ def run_vikingbot_chat(
sample_id,
"--session",
question_id,
- ]
- # 添加 --memory-user 参数
+ ])
if memory_users:
for user in memory_users:
new_cmd.extend(["--memory-user", user])
@@ -266,7 +269,10 @@ def run_vikingbot_chat(
else:
input = f"Answer the question directly: {question}"
- cmd = ["vikingbot", "chat", "-m", input, "-e"]
+ cmd = ["vikingbot", "chat"]
+ if config:
+ cmd.extend(["--config", config])
+ cmd.extend(["-m", input, "-e"])
# 添加 --sender 作为 user_id,--session 作为 agent_id,实现访问独立 userspace
if sample_id:
cmd.extend(["--sender", sample_id, "--session", question_id])
@@ -277,7 +283,7 @@ def run_vikingbot_chat(
start_time = time.time()
try:
# print(f'cmd={cmd}')
- result = subprocess.run(cmd, capture_output=True, text=True, check=True, timeout=300)
+ result = subprocess.run(cmd, capture_output=True, text=True, check=True, timeout=600)
end_time = time.time()
time_cost = end_time - start_time
@@ -330,6 +336,14 @@ def load_processed_questions(output_path: str, skip_done: bool = False) -> set[s
if question:
processed_questions.add(question)
return processed_questions
+def append_row_to_csv(output_path: str, fieldnames: list[str], row: dict) -> None:
+ """追加单行结果到 CSV。"""
+ file_exists = os.path.exists(output_path)
+ with open(output_path, "a", encoding="utf-8", newline="") as f:
+ writer = csv.DictWriter(f, fieldnames=fieldnames)
+ if not file_exists:
+ writer.writeheader()
+ writer.writerow(row)
def main():
@@ -355,6 +369,11 @@ def main():
default=default_errors,
help="Path to invalid questions JSON file",
)
+ parser.add_argument(
+ "--config",
+ default=None,
+ help="Path to config file to pass through to vikingbot chat",
+ )
parser.add_argument(
"--sample",
type=str,
@@ -483,6 +502,9 @@ def main():
# 创建线程锁,确保多线程写文件安全
write_lock = threading.Lock()
+ if not args.update_mode and not args.skip_done and os.path.exists(args.output):
+ os.remove(args.output)
+
# 存储处理后的新行
new_rows = []
processed_count = 0
@@ -510,8 +532,12 @@ def main():
print(f" [memory users: {speakers}]")
response, token_usage, time_cost, iteration, tools_used_names = run_vikingbot_chat(
- question, question_time, sample_id, question_id,
- memory_users=None if not args.group_chat else speakers,
+ question,
+ question_time,
+ qa_item.get("original_sample_id"),
+ question_id,
+ args.config,
+ None if not args.group_chat else speakers,
)
row = {
@@ -535,6 +561,32 @@ def main():
# 线程安全的结果收集
with write_lock:
nonlocal processed_count
+ if args.update_mode:
+ if os.path.exists(args.output):
+ with open(args.output, "r", encoding="utf-8", newline="") as f:
+ reader = csv.DictReader(f)
+ existing_rows = list(reader)
+ existing_fieldnames = reader.fieldnames or fieldnames
+
+ q_idx = str(row.get("question_index", ""))
+ found = False
+ for existing_row in existing_rows:
+ if str(existing_row.get("question_index", "")) == q_idx:
+ existing_row.update(row)
+ found = True
+ break
+ if not found:
+ existing_rows.append(row)
+
+ with open(args.output, "w", encoding="utf-8", newline="") as f:
+ writer = csv.DictWriter(f, fieldnames=existing_fieldnames)
+ writer.writeheader()
+ writer.writerows(existing_rows)
+ else:
+ append_row_to_csv(args.output, fieldnames, row)
+ else:
+ append_row_to_csv(args.output, fieldnames, row)
+
new_rows.append(row)
processed_questions.add(question)
processed_count += 1
@@ -555,48 +607,7 @@ def main():
except Exception as e:
print(f"Error processing QA item: {str(e)}")
- # 写文件逻辑
- if args.update_mode and os.path.exists(args.output):
- # 更新模式:读取现有文件,更新匹配行
- print(f"Update mode: updating existing file {args.output}")
- with open(args.output, "r", encoding="utf-8", newline="") as f:
- reader = csv.DictReader(f)
- existing_rows = list(reader)
- existing_fieldnames = reader.fieldnames or fieldnames
-
- # 更新匹配的行
- updated_count = 0
- for new_row in new_rows:
- q_idx = str(new_row.get("question_index", ""))
- found = False
- for row in existing_rows:
- if str(row.get("question_index", "")) == q_idx:
- row.update(new_row)
- found = True
- updated_count += 1
- break
- if not found:
- existing_rows.append(new_row)
- updated_count += 1
-
- # 写回文件
- with open(args.output, "w", encoding="utf-8", newline="") as f:
- writer = csv.DictWriter(f, fieldnames=existing_fieldnames)
- writer.writeheader()
- writer.writerows(existing_rows)
-
- print(f"Updated {updated_count} rows in {args.output}")
- else:
- # 普通模式:覆盖写入
- if os.path.exists(args.output):
- os.remove(args.output)
-
- with open(args.output, "w", encoding="utf-8", newline="") as f:
- writer = csv.DictWriter(f, fieldnames=fieldnames)
- writer.writeheader()
- writer.writerows(new_rows)
-
- print(f"Evaluation completed, results saved to {args.output}")
+ print(f"Evaluation completed, results saved to {args.output}")
if __name__ == "__main__":
diff --git a/bot/tests/test_openviking_api_key_type.py b/bot/tests/test_openviking_api_key_type.py
new file mode 100644
index 000000000..0fffcce70
--- /dev/null
+++ b/bot/tests/test_openviking_api_key_type.py
@@ -0,0 +1,317 @@
+from pathlib import Path
+from types import SimpleNamespace
+
+import pytest
+
+from vikingbot.agent.tools.ov_file import VikingSearchTool
+from vikingbot.config.schema import SessionKey
+from vikingbot.hooks.base import HookContext
+from vikingbot.hooks.builtins.openviking_hooks import OpenVikingCompactHook
+from vikingbot.openviking_mount import ov_server as ov_server_module
+from vikingbot.openviking_mount.ov_server import VikingClient
+
+
+class _DummySession:
+ async def add_message(self, role, parts, created_at=None):
+ return None
+
+ async def commit_async(self):
+ return {"status": "committed"}
+
+
+class _DummyHTTPClient:
+ instances = []
+
+ def __init__(self, **kwargs):
+ self.kwargs = kwargs
+ _DummyHTTPClient.instances.append(self)
+
+ async def initialize(self):
+ return None
+
+ async def create_session(self):
+ return {"session_id": "s-1"}
+
+ def session(self, _session_id):
+ return _DummySession()
+
+ async def admin_list_accounts(self):
+ return []
+
+ async def close(self):
+ return None
+
+
+def _make_config(api_key_type: str, mode: str = "remote"):
+ ov_server = SimpleNamespace(
+ mode=mode,
+ api_key_type=api_key_type,
+ server_url="http://ov.local",
+ root_api_key="root-key",
+ account_id="acct",
+ admin_user_id="admin",
+ agent_id="",
+ )
+ return SimpleNamespace(ov_server=ov_server, ov_data_path=Path("/tmp/openviking-test"))
+
+
[email protected](autouse=True)
+def _patch_http_client(monkeypatch):
+ _DummyHTTPClient.instances.clear()
+ monkeypatch.setattr(ov_server_module.ov, "AsyncHTTPClient", _DummyHTTPClient)
+
+
+def test_viking_client_init_root_mode_sets_account_and_user(monkeypatch):
+ monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("root"))
+
+ client = VikingClient(agent_id="workspace#channel")
+
+ first = _DummyHTTPClient.instances[0]
+ assert client.api_key_type == "root"
+ assert first.kwargs["account"] == "acct"
+ assert first.kwargs["user"] == "admin"
+ assert first.kwargs["agent_id"] == "workspace"
+
+
+def test_viking_client_init_user_mode_does_not_set_user_or_account(monkeypatch):
+ monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("user"))
+
+ client = VikingClient(agent_id="workspace")
+
+ first = _DummyHTTPClient.instances[0]
+ assert client.api_key_type == "user"
+ assert "user" not in first.kwargs
+ assert "account" not in first.kwargs
+
+
+async def test_commit_user_mode_ignores_user_specific_key_flow(monkeypatch):
+ monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("user"))
+ client = VikingClient(agent_id="workspace")
+
+ async def _must_not_call(*_args, **_kwargs):
+ raise AssertionError("user mode should not call user management path")
+
+ monkeypatch.setattr(client, "_check_user_exists", _must_not_call)
+ monkeypatch.setattr(client, "_initialize_user", _must_not_call)
+ monkeypatch.setattr(client, "_get_or_create_user_apikey", _must_not_call)
+
+ result = await client.commit(
+ session_id="sess",
+ messages=[{"role": "user", "content": "hello", "tools_used": []}],
+ user_id="sender-1",
+ )
+
+ assert result["success"] == "committed"
+
+
+async def test_commit_root_mode_uses_sender_user_key(monkeypatch):
+ monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("root"))
+ client = VikingClient(agent_id="workspace")
+
+ async def _exists(_user_id):
+ return True
+
+ async def _user_key(_user_id):
+ return "user-key-1"
+
+ monkeypatch.setattr(client, "_check_user_exists", _exists)
+ monkeypatch.setattr(client, "_get_or_create_user_apikey", _user_key)
+ client._apikey_manager = object()
+
+ result = await client.commit(
+ session_id="sess",
+ messages=[{"role": "user", "content": "hello", "tools_used": []}],
+ user_id="sender-2",
+ )
+
+ assert result["success"] == "committed"
+ assert any(inst.kwargs.get("api_key") == "user-key-1" for inst in _DummyHTTPClient.instances)
+
+
+async def test_compact_hook_user_mode_commits_once(monkeypatch):
+ from vikingbot.hooks.builtins import openviking_hooks as hooks_module
+
+ monkeypatch.setattr(hooks_module, "load_config", lambda: _make_config("user"))
+
+ class _FakeClient:
+ def __init__(self):
+ self.calls = []
+
+ async def commit(self, session_id, messages, user_id=None):
+ self.calls.append((session_id, user_id, len(messages)))
+ return {"success": "committed"}
+
+ fake_client = _FakeClient()
+ hook = OpenVikingCompactHook()
+
+ async def _fake_get_client(_workspace_id):
+ return fake_client
+
+ monkeypatch.setattr(hook, "_get_client", _fake_get_client)
+
+ context = HookContext(
+ event_type="message.compact",
+ workspace_id="ws",
+ session_key=SessionKey(type="cli", channel_id="default", chat_id="chat-1"),
+ )
+ session = SimpleNamespace(
+ messages=[
+ {"sender_id": "admin", "role": "assistant", "content": "a"},
+ {"sender_id": "u1", "role": "user", "content": "b"},
+ {"sender_id": "u2", "role": "user", "content": "c"},
+ ]
+ )
+
+ result = await hook.execute(context, session=session)
+
+ assert result["success"] is True
+ assert result["users_count"] == 0
+ assert len(fake_client.calls) == 1
+ assert fake_client.calls[0][0] == "cli__default__chat-1"
+ assert fake_client.calls[0][1] is None
+
+
+async def test_search_memory_uses_flat_agent_namespace_by_default(monkeypatch):
+ monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("root"))
+ client = VikingClient(agent_id="workspace")
+
+ calls = []
+
+ class _Result:
+ memories = []
+
+ async def _exists(_user_id):
+ return True
+
+ async def _find(*, query, target_uri, limit):
+ calls.append(target_uri)
+ return _Result()
+
+ monkeypatch.setattr(client, "_check_user_exists", _exists)
+ monkeypatch.setattr(client.client, "find", _find)
+
+ await client.search_memory("hello", "sender-1", "admin", limit=5)
+
+ assert calls == [
+ "viking://user/sender-1/memories/",
+ "viking://agent/workspace/memories/",
+ ]
+
+
+async def test_search_memory_uses_policy_scoped_namespaces(monkeypatch):
+ monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("root"))
+ client = VikingClient(agent_id="workspace")
+
+ calls = []
+
+ class _Result:
+ memories = []
+
+ async def _exists(_user_id):
+ return True
+
+ async def _find(*, query, target_uri, limit):
+ calls.append(target_uri)
+ return _Result()
+
+ async def _accounts():
+ return [
+ {
+ "account_id": "acct",
+ "isolate_user_scope_by_agent": True,
+ "isolate_agent_scope_by_user": True,
+ }
+ ]
+
+ monkeypatch.setattr(client, "_check_user_exists", _exists)
+ monkeypatch.setattr(client.client, "find", _find)
+ monkeypatch.setattr(client.client, "admin_list_accounts", _accounts)
+
+ await client.search_memory("hello", "sender-1", "admin", limit=5)
+
+ assert calls == [
+ "viking://user/sender-1/agent/workspace/memories/",
+ "viking://agent/workspace/user/sender-1/memories/",
+ ]
+
+
+async def test_skill_memory_uri_respects_namespace_policy(monkeypatch):
+ monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("root"))
+ client = VikingClient(agent_id="workspace")
+
+ async def _accounts():
+ return [
+ {
+ "account_id": "acct",
+ "isolate_user_scope_by_agent": False,
+ "isolate_agent_scope_by_user": True,
+ }
+ ]
+
+ monkeypatch.setattr(client.client, "admin_list_accounts", _accounts)
+ await client._load_namespace_policy()
+
+ assert (
+ client._skill_memory_uri("planner", "admin")
+ == "viking://agent/workspace/user/admin/memories/skills/planner.md"
+ )
+
+
+async def test_openviking_search_uses_policy_scoped_user_namespace(monkeypatch):
+ monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("root"))
+ tool = VikingSearchTool()
+ client = VikingClient(agent_id="workspace")
+
+ calls = []
+
+ async def _accounts():
+ return [
+ {
+ "account_id": "acct",
+ "isolate_user_scope_by_agent": True,
+ "isolate_agent_scope_by_user": False,
+ }
+ ]
+
+ async def _search(query, target_uri=None, limit=20):
+ calls.append(target_uri)
+ return {"memories": [{"uri": target_uri, "abstract": "a", "score": 0.9, "is_leaf": True}]}
+
+ monkeypatch.setattr(client.client, "admin_list_accounts", _accounts)
+ monkeypatch.setattr(client.client, "search", _search)
+ monkeypatch.setattr(tool, "_get_client", lambda _tool_context: client)
+
+ tool_context = SimpleNamespace(workspace_id="workspace", memory_user_ids=["sender-1"])
+ result = await tool.execute(tool_context, query="hello")
+
+ assert "sender-1/agent/workspace/memories" in result
+ assert calls == ["viking://user/sender-1/agent/workspace/memories/"]
+
+
+async def test_openviking_search_user_key_mode_uses_current_user_namespace(monkeypatch):
+ monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("user"))
+ tool = VikingSearchTool()
+ client = VikingClient(agent_id="workspace")
+
+ calls = []
+
+ async def _search(query, target_uri=None, limit=20):
+ calls.append(target_uri)
+ return {"memories": [{"uri": target_uri, "abstract": "a", "score": 0.9, "is_leaf": True}]}
+
+ monkeypatch.setattr(client.client, "search", _search)
+ monkeypatch.setattr(tool, "_get_client", lambda _tool_context: client)
+
+ tool_context = SimpleNamespace(workspace_id="workspace", memory_user_ids=["sender-1", "sender-2"])
+ result = await tool.execute(tool_context, query="hello")
+
+ assert "viking://user/memories/" in result
+ assert calls == ["viking://user/memories/"]
diff --git a/bot/vikingbot/agent/tools/ov_file.py b/bot/vikingbot/agent/tools/ov_file.py
index 2aebeeb3f..3db4e178f 100644
--- a/bot/vikingbot/agent/tools/ov_file.py
+++ b/bot/vikingbot/agent/tools/ov_file.py
@@ -238,36 +238,35 @@ class VikingSearchTool(OVFileTool):
) -> str:
try:
client = await self._get_client(tool_context)
- search_client = getattr(client, "admin_user_client", client)
+ search_client = client.admin_user_client or client.client
# If no target_uri specified, use memory_user_ids to search specific user memories
if not target_uri and tool_context.memory_user_ids:
all_results = []
- for user_id in tool_context.memory_user_ids:
- user_uri = f"viking://user/{user_id}/memories/"
+ user_ids = tool_context.memory_user_ids
+ if client._is_user_key_mode():
+ user_ids = [None]
+
+ for user_id in user_ids:
+ user_uri = client._memory_target_uri(user_id)
logger.info(f"openviking_search: searching {user_uri} for query: {query}")
results = await search_client.search(query, target_uri=user_uri, limit=20)
if results:
- # Extract memories from results
- if isinstance(results, dict):
- memories = results.get("memories", [])
- elif isinstance(results, list):
- memories = results
- else:
- memories = []
+ memories = [
+ item for item in self._extract_search_items(results) if item.get("type") == "memory"
+ ]
all_results.extend(memories)
if not all_results:
return f"No results found for query: {query}"
- # Reuse unified formatting pipeline
grouped_items = self._filter_search_items(all_results, min_score=min_score)
total = sum(len(items) for items in grouped_items.values())
if total == 0:
return f"No results found for query: {query}"
return self._format_search_items_json(grouped_items, min_score=min_score)
- else:
- results = await search_client.search(query, target_uri=target_uri, limit=20)
+
+ results = await search_client.search(query, target_uri=target_uri, limit=20)
if not results:
return f"No results found for query: {query}"
diff --git a/bot/vikingbot/config/schema.py b/bot/vikingbot/config/schema.py
index 26b07e603..b23a8739a 100644
--- a/bot/vikingbot/config/schema.py
+++ b/bot/vikingbot/config/schema.py
@@ -4,7 +4,7 @@ from enum import Enum
from pathlib import Path
from typing import Any, Dict, Literal, Optional
-from pydantic import BaseModel, ConfigDict, Field
+from pydantic import BaseModel, ConfigDict, Field, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
@@ -510,12 +510,20 @@ class OpenVikingConfig(BaseModel):
"""Viking tools configuration."""
mode: str = "remote" # local or remote
+ api_key_type: Literal["root", "user"] = "root"
server_url: str = ""
root_api_key: str = ""
account_id: str = "default"
admin_user_id: str = "default"
agent_id: str = ""
+ @field_validator("api_key_type", mode="before")
+ @classmethod
+ def normalize_api_key_type(cls, value: Any) -> str:
+ if value is None:
+ return "root"
+ return str(value).strip().lower()
+
class WebToolsConfig(BaseModel):
"""Web tools configuration."""
diff --git a/bot/vikingbot/hooks/builtins/openviking_hooks.py b/bot/vikingbot/hooks/builtins/openviking_hooks.py
index 5d36abca4..895eba87d 100644
--- a/bot/vikingbot/hooks/builtins/openviking_hooks.py
+++ b/bot/vikingbot/hooks/builtins/openviking_hooks.py
@@ -1,15 +1,13 @@
-import re
import asyncio
-from typing import Any
+import re
from collections import defaultdict
+from typing import Any
from loguru import logger
from vikingbot.config.loader import load_config
-from vikingbot.config.schema import SessionKey, AgentMemoryMode
-
-from ...session import Session
from ..base import Hook, HookContext
+from ...session import Session
try:
import openviking as ov
@@ -21,24 +19,25 @@ except Exception:
VikingClient = None
ov = None
-# Global singleton client
-_global_client: VikingClient | None = None
+# Clients are cached per workspace so canonical agent_id routing stays aligned
+# with OpenViking account namespace policy.
+_global_clients: dict[str | None, VikingClient] = {}
-async def get_global_client() -> VikingClient:
- """Get or create the global singleton VikingClient."""
- global _global_client
- if _global_client is None:
- _global_client = await VikingClient.create(None)
- return _global_client
+async def get_global_client(workspace_id: str | None) -> VikingClient:
+ """Get or create a workspace-scoped VikingClient."""
+ client = _global_clients.get(workspace_id)
+ if client is None:
+ client = await VikingClient.create(workspace_id)
+ _global_clients[workspace_id] = client
+ return client
class OpenVikingCompactHook(Hook):
name = "openviking_compact"
async def _get_client(self, workspace_id: str) -> VikingClient:
- # Use global singleton client
- return await get_global_client()
+ return await get_global_client(workspace_id)
async def execute(self, context: HookContext, **kwargs) -> Any:
@@ -50,20 +49,25 @@ class OpenVikingCompactHook(Hook):
try:
client = await self._get_client(context.workspace_id)
- # 1. 提交全部的 message 到 admin
+ if not client.should_sender_fanout():
+ single_result = await client.commit(session_id, vikingbot_session.messages, None)
+ return {
+ "success": True,
+ "admin_result": single_result,
+ "user_results": [],
+ "users_count": 0,
+ }
+
admin_result = await client.commit(session_id, vikingbot_session.messages, admin_user_id)
- # 2. 根据 message 里的 sender_id 进行分组
messages_by_sender = defaultdict(list)
for msg in vikingbot_session.messages:
sender_id = msg.get("sender_id")
if sender_id and sender_id != admin_user_id:
messages_by_sender[sender_id].append(msg)
- # 3. 带并发限制地提交到各个 user
user_results = []
if messages_by_sender:
- # 限制最大并发数为 5
semaphore = asyncio.Semaphore(5)
async def commit_with_semaphore(user_id: str, user_messages: list):
@@ -75,14 +79,13 @@ class OpenVikingCompactHook(Hook):
task = commit_with_semaphore(user_id, user_messages)
user_tasks.append(task)
- # 等待所有用户任务完成
user_results = await asyncio.gather(*user_tasks, return_exceptions=True)
return {
"success": True,
"admin_result": admin_result,
"user_results": user_results,
- "users_count": len(messages_by_sender)
+ "users_count": len(messages_by_sender),
}
except Exception as e:
logger.exception(f"Failed to add message to OpenViking: {e}")
@@ -94,27 +97,18 @@ class OpenVikingPostCallHook(Hook):
is_sync = True
async def _get_client(self, workspace_id: str) -> VikingClient:
- # Use global singleton client
- return await get_global_client()
+ return await get_global_client(workspace_id)
async def _read_skill_memory(self, workspace_id: str, skill_name: str) -> str:
ov_client = await self._get_client(workspace_id)
config = load_config()
openviking_config = config.ov_server
- # (f'openviking_config.mode={openviking_config.mode}')
if not skill_name:
return ""
try:
- if openviking_config.mode == "local":
- skill_memory_uri = f"viking://agent/ffb1327b18bf/memories/skills/{skill_name}.md"
- else:
- agent_space_name = ov_client.get_agent_space_name(openviking_config.admin_user_id)
- skill_memory_uri = (
- f"viking://agent/{agent_space_name}/memories/skills/{skill_name}.md"
- )
+ skill_owner_user_id = None if openviking_config.mode == "local" else openviking_config.admin_user_id
+ skill_memory_uri = ov_client._skill_memory_uri(skill_name, skill_owner_user_id)
content = await ov_client.read_content(skill_memory_uri, level="read")
- # print(f'content={content}')
- # logger.warning(f"content={content}")
return f"\n\n---\n## Skill Memory\n{content}" if content else ""
except Exception as e:
logger.warning(f"Failed to read skill memory for {skill_name}: {e}")
diff --git a/bot/vikingbot/openviking_mount/ov_server.py b/bot/vikingbot/openviking_mount/ov_server.py
index 5a344a34c..b350eb5c7 100644
--- a/bot/vikingbot/openviking_mount/ov_server.py
+++ b/bot/vikingbot/openviking_mount/ov_server.py
@@ -18,41 +18,58 @@ class VikingClient:
openviking_config = config.ov_server
self.openviking_config = openviking_config
self.ov_path = config.ov_data_path
+ self.mode = openviking_config.mode
+ self.api_key_type = (openviking_config.api_key_type or "root").strip().lower()
+ if self.api_key_type not in {"root", "user"}:
+ raise ValueError(f"Invalid ov_server.api_key_type: {self.api_key_type}")
+
+ self._apikey_manager = None
+ self.admin_user_client = None
+ self._namespace_policy = {
+ "isolate_user_scope_by_agent": False,
+ "isolate_agent_scope_by_user": False,
+ }
+ self._namespace_policy_loaded = False
+
if openviking_config.mode == "local":
self.client = ov.AsyncHTTPClient(url=openviking_config.server_url)
self.agent_id = "default"
self.account_id = "default"
self.user_id = "default"
self.admin_user_id = "default"
- self._apikey_manager = None
- else:
- if agent_id and "#" in agent_id:
- agent_id = agent_id.split("#", 1)[0]
- self.client = ov.AsyncHTTPClient(
- url=openviking_config.server_url,
- api_key=openviking_config.root_api_key,
- account=openviking_config.account_id,
- user=openviking_config.admin_user_id,
- agent_id=agent_id,
+ return
+
+ if agent_id and "#" in agent_id:
+ agent_id = agent_id.split("#", 1)[0]
+
+ self.agent_id = agent_id
+ self.account_id = openviking_config.account_id
+ self.admin_user_id = openviking_config.admin_user_id
+
+ remote_client_kwargs = {
+ "url": openviking_config.server_url,
+ "api_key": openviking_config.root_api_key,
+ "agent_id": agent_id,
+ }
+ if self._is_root_key_mode():
+ remote_client_kwargs["account"] = openviking_config.account_id
+ remote_client_kwargs["user"] = openviking_config.admin_user_id
+
+ self.client = ov.AsyncHTTPClient(**remote_client_kwargs)
+
+ if self._is_root_key_mode() and self.ov_path:
+ self._apikey_manager = UserApiKeyManager(
+ ov_path=self.ov_path,
+ server_url=openviking_config.server_url,
+ account_id=openviking_config.account_id,
)
- self.agent_id = agent_id
- self.account_id = openviking_config.account_id
- self.admin_user_id = openviking_config.admin_user_id
- self._apikey_manager = None
- if self.ov_path:
- self._apikey_manager = UserApiKeyManager(
- ov_path=self.ov_path,
- server_url=openviking_config.server_url,
- account_id=openviking_config.account_id,
- )
- self.mode = openviking_config.mode
async def _initialize(self):
"""Initialize the client (must be called after construction)"""
await self.client.initialize()
+ await self._load_namespace_policy()
- # 检查并初始化 admin_user_id(如果配置了)
- if self.mode == "remote" and self.admin_user_id:
+ if self._is_root_key_mode() and self.admin_user_id:
user_exists = await self._check_user_exists(self.admin_user_id)
if not user_exists:
await self._initialize_user(self.admin_user_id, role="admin")
@@ -106,6 +123,82 @@ class VikingClient:
def get_agent_space_name(self, user_id: str) -> str:
return hashlib.md5(f"{user_id}:{self.agent_id}".encode()).hexdigest()[:12]
+ def _is_root_key_mode(self) -> bool:
+ return self.mode == "remote" and self.api_key_type == "root"
+
+ def _is_user_key_mode(self) -> bool:
+ return self.mode == "remote" and self.api_key_type == "user"
+
+ def _effective_user_id(self, user_id: Optional[str]) -> str:
+ if self._is_user_key_mode():
+ return ""
+ return user_id or self.admin_user_id
+
+ async def _load_namespace_policy(self) -> None:
+ if self._namespace_policy_loaded:
+ return
+
+ policy = {
+ "isolate_user_scope_by_agent": False,
+ "isolate_agent_scope_by_user": False,
+ }
+
+ if self.mode == "remote" and self.account_id:
+ try:
+ accounts = await self.client.admin_list_accounts()
+ for account in accounts or []:
+ if account.get("account_id") == self.account_id:
+ policy = {
+ "isolate_user_scope_by_agent": bool(
+ account.get("isolate_user_scope_by_agent", False)
+ ),
+ "isolate_agent_scope_by_user": bool(
+ account.get("isolate_agent_scope_by_user", False)
+ ),
+ }
+ break
+ except Exception as e:
+ logger.warning(
+ f"Failed to load account namespace policy for {self.account_id}: {e}"
+ )
+
+ self._namespace_policy = policy
+ self._namespace_policy_loaded = True
+
+ def _user_space_fragment(self, user_id: Optional[str]) -> str:
+ effective_user_id = self._effective_user_id(user_id)
+ if not effective_user_id:
+ return ""
+ if self._namespace_policy["isolate_user_scope_by_agent"] and self.agent_id:
+ return f"{effective_user_id}/agent/{self.agent_id}"
+ return effective_user_id
+
+ def _agent_space_fragment(self, user_id: Optional[str]) -> str:
+ if not self.agent_id:
+ return ""
+ effective_user_id = self._effective_user_id(user_id)
+ if self._namespace_policy["isolate_agent_scope_by_user"] and effective_user_id:
+ return f"{self.agent_id}/user/{effective_user_id}"
+ return self.agent_id
+
+ def _memory_target_uri(self, user_id: Optional[str]) -> str:
+ user_space = self._user_space_fragment(user_id)
+ if user_space:
+ return f"viking://user/{user_space}/memories/"
+ return "viking://user/memories/"
+
+ def _agent_memory_target_uri(self, user_id: Optional[str]) -> str:
+ agent_space = self._agent_space_fragment(user_id)
+ if agent_space:
+ return f"viking://agent/{agent_space}/memories/"
+ return "viking://agent/memories/"
+
+ def _skill_memory_uri(self, skill_name: str, user_id: Optional[str] = None) -> str:
+ return f"{self._agent_memory_target_uri(user_id)}skills/{skill_name}.md"
+
+ def should_sender_fanout(self) -> bool:
+ return self._is_root_key_mode()
+
async def find(self, query: str, target_uri: Optional[str] = None):
"""搜索资源"""
if target_uri:
@@ -149,27 +242,16 @@ class VikingClient:
return ""
async def read_user_profile(self, user_id: str) -> str:
- """读取用户 profile。
-
- 首先检查用户是否存在,如不存在则初始化用户并返回空字符串。
- 用户存在时,再查询 profile 信息。
-
- Args:
- user_id: 用户ID
-
- Returns:
- str: 用户 profile 内容,如果用户不存在或查询失败返回空字符串
- """
- # Step 1: 检查用户是否存在
- user_exists = await self._check_user_exists(user_id)
+ """读取用户 profile。"""
+ await self._load_namespace_policy()
+ effective_user_id = self._effective_user_id(user_id)
+ user_exists = await self._check_user_exists(effective_user_id)
- # Step 2: 如果用户不存在,初始化用户并直接返回
if not user_exists:
- await self._initialize_user(user_id)
+ await self._initialize_user(effective_user_id)
return ""
- # Step 3: 用户存在,查询 profile
- uri = f"viking://user/{user_id}/memories/profile.md"
+ uri = f"{self._memory_target_uri(effective_user_id)}profile.md"
result = await self.read_content(uri=uri, level="read")
return result
@@ -197,10 +279,12 @@ class VikingClient:
}
async def search_user_memory(self, query: str, user_id: str) -> list[Any]:
- user_exists = await self._check_user_exists(user_id)
+ await self._load_namespace_policy()
+ effective_user_id = self._effective_user_id(user_id)
+ user_exists = await self._check_user_exists(effective_user_id)
if not user_exists:
return []
- uri_user_memory = f"viking://user/{user_id}/memories/"
+ uri_user_memory = self._memory_target_uri(effective_user_id)
result = await self.client.search(query, target_uri=uri_user_memory)
return (
[self._matched_context_to_dict(m) for m in result.memories]
@@ -209,16 +293,11 @@ class VikingClient:
)
async def _check_user_exists(self, user_id: str) -> bool:
- """检查用户是否存在于账户中。
-
- Args:
- user_id: 用户ID
-
- Returns:
- bool: 用户是否存在
- """
- if self.mode == "local":
+ """检查用户是否存在于账户中。"""
+ if self.mode == "local" or self._is_user_key_mode():
return True
+ if not user_id:
+ return False
try:
res = await self.client.admin_list_users(self.account_id)
if not res or len(res) == 0:
@@ -229,22 +308,16 @@ class VikingClient:
return False
async def _initialize_user(self, user_id: str, role: str = "user") -> bool:
- """初始化用户。
-
- Args:
- user_id: 用户ID
-
- Returns:
- bool: 初始化是否成功
- """
- if self.mode == "local":
+ """初始化用户。"""
+ if self.mode == "local" or self._is_user_key_mode():
return True
+ if not user_id:
+ return False
try:
result = await self.client.admin_register_user(
account_id=self.account_id, user_id=user_id, role=role
)
- # Save the API key if returned and we're in remote mode with a valid apikey manager
if self._apikey_manager and isinstance(result, dict):
api_key = result.get("user_key")
if api_key:
@@ -258,20 +331,8 @@ class VikingClient:
return False
async def _get_or_create_user_apikey(self, user_id: str) -> Optional[str]:
- """获取或创建用户的 API key。
-
- 优先从本地 json 文件获取,如果本地没有则:
- 1. 删除用户(如果存在)
- 2. 重新创建用户
- 3. 保存新的 API key
-
- Args:
- user_id: 用户ID
-
- Returns:
- API key 或 None(如果获取失败)
- """
- if not self._apikey_manager:
+ """获取或创建用户的 API key。"""
+ if self._is_user_key_mode() or not self._apikey_manager or not user_id:
return None
# Step 1: Check local storage first
@@ -304,34 +365,49 @@ class VikingClient:
async def search_memory(
self, query: str, user_ids: str | list[str], agent_user_id: str, limit: int = 10
) -> dict[str, list[Any]]:
- """通过上下文消息,检索viking 的user、Agent memory。
+ """通过上下文消息,检索viking 的user、Agent memory。"""
+ await self._load_namespace_policy()
+
+ def _extract_memories(result: Any) -> list[Any]:
+ if not result:
+ return []
+ if isinstance(result, list):
+ return result
+ if isinstance(result, dict):
+ memories = result.get("memories")
+ return memories if isinstance(memories, list) else []
+ memories = getattr(result, "memories", None)
+ return memories if isinstance(memories, list) else []
- Args:
- query: Search query
- user_ids: Single user_id (str) or list of user_ids for memory retrieval
- agent_user_id: Agent user ID for agent memory space
- limit: Max results per user
- """
- # Normalize user_ids to list
if isinstance(user_ids, str):
user_ids = [user_ids]
- # Build target URIs for all users and agent
- user_uris = [f"viking://user/{user_id}/memories/" for user_id in user_ids]
- agent_space_name = self.get_agent_space_name(agent_user_id)
- agent_uri = f"viking://agent/{agent_space_name}/memories/"
-
- # Query each URI and collect results
all_user_memories = []
all_agent_memories = []
- for uri in user_uris + [agent_uri]:
- result = await self.search(query=query, target_uri=uri, limit=limit)
- memories = result.get("memories", [])
- if uri.startswith("viking://agent/"):
- all_agent_memories.extend(memories)
- else:
- all_user_memories.extend(memories)
+ for user_id in user_ids:
+ effective_user_id = self._effective_user_id(user_id)
+ user_exists = await self._check_user_exists(effective_user_id)
+ if not user_exists:
+ await self._initialize_user(effective_user_id)
+ continue
+
+ uri_user_memory = self._memory_target_uri(effective_user_id)
+ user_memory = await self.client.find(
+ query=query,
+ target_uri=uri_user_memory,
+ limit=limit,
+ )
+ all_user_memories.extend(_extract_memories(user_memory))
+
+ effective_agent_user_id = self._effective_user_id(agent_user_id)
+ uri_agent_memory = self._agent_memory_target_uri(effective_agent_user_id)
+ agent_memory = await self.client.find(
+ query=query,
+ target_uri=uri_agent_memory,
+ limit=limit,
+ )
+ all_agent_memories.extend(_extract_memories(agent_memory))
return {
"user_memory": all_user_memories,
@@ -366,30 +442,30 @@ class VikingClient:
from openviking.message.part import TextPart, ToolPart
- user_exists = await self._check_user_exists(user_id)
- if not user_exists:
- success = await self._initialize_user(user_id)
- if not success:
- return {"error": "Failed to initialize user"}
+ effective_user_id = self._effective_user_id(user_id)
- # For remote mode, try to get user's API key and create a dedicated client
client = self.client
start = time.time()
- if (
- self.mode == "remote"
- and user_id
- and user_id != self.admin_user_id
- and self._apikey_manager
- ):
- user_api_key = await self._get_or_create_user_apikey(user_id)
- if user_api_key:
- # Create a new HTTP client with user's API key
- client = ov.AsyncHTTPClient(
- url=self.openviking_config.server_url,
- api_key=user_api_key,
- agent_id=self.agent_id,
- )
- await client.initialize()
+ if self._is_root_key_mode():
+ user_exists = await self._check_user_exists(effective_user_id)
+ if not user_exists:
+ success = await self._initialize_user(effective_user_id)
+ if not success:
+ return {"error": "Failed to initialize user"}
+
+ if (
+ effective_user_id
+ and effective_user_id != self.admin_user_id
+ and self._apikey_manager
+ ):
+ user_api_key = await self._get_or_create_user_apikey(effective_user_id)
+ if user_api_key:
+ client = ov.AsyncHTTPClient(
+ url=self.openviking_config.server_url,
+ api_key=user_api_key,
+ agent_id=self.agent_id,
+ )
+ await client.initialize()
create_res = await client.create_session()
session_id = create_res["session_id"]
@@ -456,7 +532,9 @@ class VikingClient:
if client is not self.client:
await client.close()
logger.info(f"time spent: {time.time() - start}")
- logger.debug(f"Message add ed to OpenViking session {session_id}, user: {user_id}")
+ logger.debug(
+ f"Message add ed to OpenViking session {session_id}, api_key_type={self.api_key_type}, user: {effective_user_id}"
+ )
return {"success": result["status"]}
async def close(self):