@xalia/agent 0.6.2 → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/src/agent/agent.js +8 -5
- package/dist/agent/src/agent/agentUtils.js +9 -12
- package/dist/agent/src/chat/client/chatClient.js +88 -240
- package/dist/agent/src/chat/client/constants.js +1 -2
- package/dist/agent/src/chat/client/sessionClient.js +4 -13
- package/dist/agent/src/chat/client/sessionFiles.js +3 -3
- package/dist/agent/src/chat/protocol/messages.js +0 -1
- package/dist/agent/src/chat/server/chatContextManager.js +5 -9
- package/dist/agent/src/chat/server/connectionManager.test.js +1 -0
- package/dist/agent/src/chat/server/conversation.js +9 -4
- package/dist/agent/src/chat/server/openSession.js +241 -238
- package/dist/agent/src/chat/server/openSessionMessageSender.js +2 -0
- package/dist/agent/src/chat/server/sessionRegistry.js +17 -12
- package/dist/agent/src/chat/utils/approvalManager.js +82 -64
- package/dist/agent/src/chat/{client/responseHandler.js → utils/responseAwaiter.js} +41 -18
- package/dist/agent/src/test/agent.test.js +90 -53
- package/dist/agent/src/test/approvalManager.test.js +79 -35
- package/dist/agent/src/test/chatContextManager.test.js +12 -17
- package/dist/agent/src/test/responseAwaiter.test.js +74 -0
- package/dist/agent/src/tool/agentChat.js +1 -1
- package/dist/agent/src/tool/chatMain.js +2 -2
- package/package.json +1 -1
- package/scripts/setup_chat +2 -2
- package/scripts/test_chat +61 -60
- package/src/agent/agent.ts +9 -5
- package/src/agent/agentUtils.ts +14 -27
- package/src/chat/client/chatClient.ts +167 -296
- package/src/chat/client/constants.ts +0 -2
- package/src/chat/client/sessionClient.ts +15 -19
- package/src/chat/client/sessionFiles.ts +9 -12
- package/src/chat/data/dataModels.ts +1 -0
- package/src/chat/protocol/messages.ts +9 -12
- package/src/chat/server/chatContextManager.ts +7 -12
- package/src/chat/server/connectionManager.test.ts +1 -0
- package/src/chat/server/conversation.ts +19 -11
- package/src/chat/server/openSession.ts +383 -340
- package/src/chat/server/openSessionMessageSender.ts +4 -0
- package/src/chat/server/sessionRegistry.ts +33 -12
- package/src/chat/utils/approvalManager.ts +153 -81
- package/src/chat/{client/responseHandler.ts → utils/responseAwaiter.ts} +73 -23
- package/src/test/agent.test.ts +130 -62
- package/src/test/approvalManager.test.ts +108 -40
- package/src/test/chatContextManager.test.ts +19 -20
- package/src/test/responseAwaiter.test.ts +103 -0
- package/src/tool/agentChat.ts +2 -2
- package/src/tool/chatMain.ts +2 -2
- package/dist/agent/src/test/responseHandler.test.js +0 -61
- package/src/test/responseHandler.test.ts +0 -78
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { expect } from "vitest";
|
|
2
|
+
import { ResponseAwaiter } from "../chat/utils/responseAwaiter";
|
|
3
|
+
|
|
4
|
+
type ClientA = { type: "a"; client_message_id: string; value: number };
|
|
5
|
+
type ClientB = { type: "b"; client_message_id: string; data: string };
|
|
6
|
+
type ClientMsg = ClientA | ClientB;
|
|
7
|
+
|
|
8
|
+
type ServerA = { type: "a"; client_message_id: string; value: number };
|
|
9
|
+
type ServerB = { type: "b"; data: string };
|
|
10
|
+
type ServerErr = { type: "err"; client_message_id: string; message: string };
|
|
11
|
+
type ServerMsg = ServerA | ServerB | ServerErr;
|
|
12
|
+
|
|
13
|
+
const extractId = (msg: ServerMsg) =>
|
|
14
|
+
(msg as { client_message_id?: string }).client_message_id;
|
|
15
|
+
|
|
16
|
+
describe("ResponseAwaiter", () => {
|
|
17
|
+
const req: ClientMsg = {
|
|
18
|
+
type: "a",
|
|
19
|
+
client_message_id: "id_a",
|
|
20
|
+
value: 3,
|
|
21
|
+
};
|
|
22
|
+
const response: ServerMsg = {
|
|
23
|
+
type: "a",
|
|
24
|
+
client_message_id: "id_a",
|
|
25
|
+
value: 4,
|
|
26
|
+
};
|
|
27
|
+
const otherResponse: ServerMsg = {
|
|
28
|
+
type: "a",
|
|
29
|
+
client_message_id: "id_b",
|
|
30
|
+
value: 4,
|
|
31
|
+
};
|
|
32
|
+
const error: ServerMsg = {
|
|
33
|
+
type: "err",
|
|
34
|
+
client_message_id: "id_a",
|
|
35
|
+
message: "error",
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
it("should resolve", async function () {
|
|
39
|
+
const rh = ResponseAwaiter.init<ServerMsg>("err", extractId, 100);
|
|
40
|
+
const responseP = rh.waitForResponse(req.client_message_id);
|
|
41
|
+
expect(rh.onMessage(otherResponse)).eql(false);
|
|
42
|
+
expect(rh.onMessage(response)).eql(true);
|
|
43
|
+
expect(rh.onMessage(error)).eql(false);
|
|
44
|
+
|
|
45
|
+
expect(await responseP).eql(response);
|
|
46
|
+
|
|
47
|
+
// This (late) messages should be ignored.
|
|
48
|
+
expect(rh.onMessage(otherResponse)).eql(false);
|
|
49
|
+
expect(rh.onMessage(error)).eql(false);
|
|
50
|
+
expect(rh.onMessage(response)).eql(false);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should handle errors", async function () {
|
|
54
|
+
const rh = ResponseAwaiter.init<ServerMsg>("err", extractId, 100);
|
|
55
|
+
const responseP = rh.waitForResponse(req.client_message_id);
|
|
56
|
+
expect(rh.onMessage(otherResponse)).eql(false);
|
|
57
|
+
expect(rh.onMessage(error)).eql(true);
|
|
58
|
+
expect(rh.onMessage(response)).eql(false);
|
|
59
|
+
|
|
60
|
+
await expect(responseP).rejects.toThrow(error.message);
|
|
61
|
+
|
|
62
|
+
// This (late) messages should be ignored.
|
|
63
|
+
expect(rh.onMessage(otherResponse)).eql(false);
|
|
64
|
+
expect(rh.onMessage(error)).eql(false);
|
|
65
|
+
expect(rh.onMessage(response)).eql(false);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should handle timeouts", async function () {
|
|
69
|
+
const rh = ResponseAwaiter.init<ServerMsg>("err", extractId, 100);
|
|
70
|
+
const responseP = rh.waitForResponse(req.client_message_id);
|
|
71
|
+
expect(rh.onMessage(otherResponse)).eql(false);
|
|
72
|
+
expect(
|
|
73
|
+
rh.onMessage({ type: "a", client_message_id: "id_b", value: 4 })
|
|
74
|
+
).eql(false);
|
|
75
|
+
|
|
76
|
+
await expect(responseP).rejects.toThrow();
|
|
77
|
+
|
|
78
|
+
// This (late) messages should be ignored.
|
|
79
|
+
expect(rh.onMessage(otherResponse)).eql(false);
|
|
80
|
+
expect(rh.onMessage(error)).eql(false);
|
|
81
|
+
expect(rh.onMessage(response)).eql(false);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("should resolve with immediate", async function () {
|
|
85
|
+
const rh = ResponseAwaiter.initWithImmediate<ServerMsg, string>(
|
|
86
|
+
"err",
|
|
87
|
+
extractId,
|
|
88
|
+
(x: ServerMsg) => JSON.stringify(x),
|
|
89
|
+
100
|
|
90
|
+
);
|
|
91
|
+
const responseP = rh.waitForResponse(req.client_message_id);
|
|
92
|
+
expect(rh.onMessage(otherResponse)).eql(false);
|
|
93
|
+
expect(rh.onMessage(response)).eql(true);
|
|
94
|
+
expect(rh.onMessage(error)).eql(false);
|
|
95
|
+
|
|
96
|
+
expect(await responseP).eql(JSON.stringify(response));
|
|
97
|
+
|
|
98
|
+
// This (late) messages should be ignored.
|
|
99
|
+
expect(rh.onMessage(otherResponse)).eql(false);
|
|
100
|
+
expect(rh.onMessage(error)).eql(false);
|
|
101
|
+
expect(rh.onMessage(response)).eql(false);
|
|
102
|
+
});
|
|
103
|
+
});
|
package/src/tool/agentChat.ts
CHANGED
|
@@ -127,13 +127,13 @@ export async function runChat(
|
|
|
127
127
|
|
|
128
128
|
const [agent, sudoMcpServerManager] = await createAgentWithSkills(
|
|
129
129
|
llmUrl,
|
|
130
|
-
agentProfile,
|
|
131
|
-
DEFAULT_AGENT_LLM_MODEL,
|
|
130
|
+
agentProfile.model || DEFAULT_AGENT_LLM_MODEL,
|
|
132
131
|
eventHandler,
|
|
133
132
|
NODE_PLATFORM,
|
|
134
133
|
new ContextManager(agentProfile.system_prompt, conversation || []),
|
|
135
134
|
llmApiKey,
|
|
136
135
|
sudomcpConfig,
|
|
136
|
+
agentProfile.mcp_settings,
|
|
137
137
|
utils.FRONTEND_PROD_AUTHORIZED_URL,
|
|
138
138
|
stream
|
|
139
139
|
);
|
package/src/tool/chatMain.ts
CHANGED
|
@@ -440,9 +440,9 @@ function getCLIEventHandler(
|
|
|
440
440
|
|
|
441
441
|
switch (msg.type) {
|
|
442
442
|
case "user_msg":
|
|
443
|
-
stdout.write(`[${msg.
|
|
443
|
+
stdout.write(`[${msg.user_nickname}]: ${msg.message || ""}\n`);
|
|
444
444
|
if (msg.imageB64) {
|
|
445
|
-
stdout.write(`[${msg.
|
|
445
|
+
stdout.write(`[${msg.user_nickname}]: IMAGE: ${msg.imageB64}\n`);
|
|
446
446
|
}
|
|
447
447
|
break;
|
|
448
448
|
case "agent_msg":
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const vitest_1 = require("vitest");
|
|
4
|
-
const responseHandler_1 = require("../chat/client/responseHandler");
|
|
5
|
-
describe("ResponseHandler", () => {
|
|
6
|
-
const req = {
|
|
7
|
-
type: "a",
|
|
8
|
-
client_message_id: "id_a",
|
|
9
|
-
value: 3,
|
|
10
|
-
};
|
|
11
|
-
const response = {
|
|
12
|
-
type: "a",
|
|
13
|
-
client_message_id: "id_a",
|
|
14
|
-
value: 4,
|
|
15
|
-
};
|
|
16
|
-
const otherResponse = {
|
|
17
|
-
type: "a",
|
|
18
|
-
client_message_id: "id_b",
|
|
19
|
-
value: 4,
|
|
20
|
-
};
|
|
21
|
-
const error = {
|
|
22
|
-
type: "err",
|
|
23
|
-
client_message_id: "id_a",
|
|
24
|
-
message: "error",
|
|
25
|
-
};
|
|
26
|
-
it("should resolve", async function () {
|
|
27
|
-
const rh = new responseHandler_1.ResponseHandler("err", 100);
|
|
28
|
-
const responseP = rh.waitForResponse(req);
|
|
29
|
-
rh.onMessage(otherResponse);
|
|
30
|
-
rh.onMessage(response);
|
|
31
|
-
rh.onMessage(error);
|
|
32
|
-
(0, vitest_1.expect)(await responseP).eql(response);
|
|
33
|
-
// This (late) messages should be ignored.
|
|
34
|
-
rh.onMessage(otherResponse);
|
|
35
|
-
rh.onMessage(error);
|
|
36
|
-
rh.onMessage(response);
|
|
37
|
-
});
|
|
38
|
-
it("should handle errors", async function () {
|
|
39
|
-
const rh = new responseHandler_1.ResponseHandler("err", 100);
|
|
40
|
-
const responseP = rh.waitForResponse(req);
|
|
41
|
-
rh.onMessage(otherResponse);
|
|
42
|
-
rh.onMessage(error);
|
|
43
|
-
rh.onMessage(response);
|
|
44
|
-
await (0, vitest_1.expect)(responseP).rejects.toThrow(error.message);
|
|
45
|
-
// This (late) messages should be ignored.
|
|
46
|
-
rh.onMessage(otherResponse);
|
|
47
|
-
rh.onMessage(error);
|
|
48
|
-
rh.onMessage(response);
|
|
49
|
-
});
|
|
50
|
-
it("should handle timeouts", async function () {
|
|
51
|
-
const rh = new responseHandler_1.ResponseHandler("err", 100);
|
|
52
|
-
const responseP = rh.waitForResponse(req);
|
|
53
|
-
rh.onMessage(otherResponse);
|
|
54
|
-
rh.onMessage({ type: "a", client_message_id: "id_b", value: 4 });
|
|
55
|
-
await (0, vitest_1.expect)(responseP).rejects.toThrow();
|
|
56
|
-
// This (late) messages should be ignored.
|
|
57
|
-
rh.onMessage(otherResponse);
|
|
58
|
-
rh.onMessage(error);
|
|
59
|
-
rh.onMessage(response);
|
|
60
|
-
});
|
|
61
|
-
});
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { expect } from "vitest";
|
|
2
|
-
import { ResponseHandler } from "../chat/client/responseHandler";
|
|
3
|
-
|
|
4
|
-
type ClientA = { type: "a"; client_message_id: string; value: number };
|
|
5
|
-
type ClientB = { type: "b"; client_message_id: string; data: string };
|
|
6
|
-
type ClientMsg = ClientA | ClientB;
|
|
7
|
-
|
|
8
|
-
type ServerA = { type: "a"; client_message_id: string; value: number };
|
|
9
|
-
type ServerB = { type: "b"; data: string };
|
|
10
|
-
type ServerErr = { type: "err"; client_message_id: string; message: string };
|
|
11
|
-
type ServerMsg = ServerA | ServerB | ServerErr;
|
|
12
|
-
|
|
13
|
-
describe("ResponseHandler", () => {
|
|
14
|
-
const req: ClientMsg = {
|
|
15
|
-
type: "a",
|
|
16
|
-
client_message_id: "id_a",
|
|
17
|
-
value: 3,
|
|
18
|
-
};
|
|
19
|
-
const response: ServerMsg = {
|
|
20
|
-
type: "a",
|
|
21
|
-
client_message_id: "id_a",
|
|
22
|
-
value: 4,
|
|
23
|
-
};
|
|
24
|
-
const otherResponse: ServerMsg = {
|
|
25
|
-
type: "a",
|
|
26
|
-
client_message_id: "id_b",
|
|
27
|
-
value: 4,
|
|
28
|
-
};
|
|
29
|
-
const error: ServerMsg = {
|
|
30
|
-
type: "err",
|
|
31
|
-
client_message_id: "id_a",
|
|
32
|
-
message: "error",
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
it("should resolve", async function () {
|
|
36
|
-
const rh = new ResponseHandler<ClientMsg, ServerMsg>("err", 100);
|
|
37
|
-
const responseP = rh.waitForResponse(req);
|
|
38
|
-
rh.onMessage(otherResponse);
|
|
39
|
-
rh.onMessage(response);
|
|
40
|
-
rh.onMessage(error);
|
|
41
|
-
|
|
42
|
-
expect(await responseP).eql(response);
|
|
43
|
-
|
|
44
|
-
// This (late) messages should be ignored.
|
|
45
|
-
rh.onMessage(otherResponse);
|
|
46
|
-
rh.onMessage(error);
|
|
47
|
-
rh.onMessage(response);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("should handle errors", async function () {
|
|
51
|
-
const rh = new ResponseHandler<ClientMsg, ServerMsg>("err", 100);
|
|
52
|
-
const responseP = rh.waitForResponse(req);
|
|
53
|
-
rh.onMessage(otherResponse);
|
|
54
|
-
rh.onMessage(error);
|
|
55
|
-
rh.onMessage(response);
|
|
56
|
-
|
|
57
|
-
await expect(responseP).rejects.toThrow(error.message);
|
|
58
|
-
|
|
59
|
-
// This (late) messages should be ignored.
|
|
60
|
-
rh.onMessage(otherResponse);
|
|
61
|
-
rh.onMessage(error);
|
|
62
|
-
rh.onMessage(response);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it("should handle timeouts", async function () {
|
|
66
|
-
const rh = new ResponseHandler<ClientMsg, ServerMsg>("err", 100);
|
|
67
|
-
const responseP = rh.waitForResponse(req);
|
|
68
|
-
rh.onMessage(otherResponse);
|
|
69
|
-
rh.onMessage({ type: "a", client_message_id: "id_b", value: 4 });
|
|
70
|
-
|
|
71
|
-
await expect(responseP).rejects.toThrow();
|
|
72
|
-
|
|
73
|
-
// This (late) messages should be ignored.
|
|
74
|
-
rh.onMessage(otherResponse);
|
|
75
|
-
rh.onMessage(error);
|
|
76
|
-
rh.onMessage(response);
|
|
77
|
-
});
|
|
78
|
-
});
|