agentxjs 2.4.0 → 2.6.0
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/README.md +74 -34
- package/dist/{chunk-X44CQZPK.js → chunk-JTHR3AK6.js} +87 -4
- package/dist/chunk-JTHR3AK6.js.map +1 -0
- package/dist/index.d.ts +212 -39
- package/dist/index.js +294 -54
- package/dist/index.js.map +1 -1
- package/dist/server-UCQISBKH.js +7 -0
- package/package.json +3 -3
- package/src/AgentHandle.ts +65 -0
- package/src/CommandHandler.ts +131 -7
- package/src/LocalClient.ts +87 -27
- package/src/RemoteClient.ts +77 -17
- package/src/index.ts +19 -17
- package/src/namespaces/presentations.ts +11 -6
- package/src/namespaces/prototypes.ts +136 -0
- package/src/presentation/Presentation.ts +2 -2
- package/src/types.ts +254 -38
- package/dist/chunk-X44CQZPK.js.map +0 -1
- package/dist/server-BWI5JE4B.js +0 -7
- /package/dist/{server-BWI5JE4B.js.map → server-UCQISBKH.js.map} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CommandHandler,
|
|
3
3
|
createServer
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-JTHR3AK6.js";
|
|
5
5
|
|
|
6
6
|
// src/index.ts
|
|
7
7
|
import { createAgentXRuntime } from "@agentxjs/core/runtime";
|
|
@@ -9,6 +9,41 @@ import { createAgentXRuntime } from "@agentxjs/core/runtime";
|
|
|
9
9
|
// src/LocalClient.ts
|
|
10
10
|
import { createLogger } from "commonxjs/logger";
|
|
11
11
|
|
|
12
|
+
// src/AgentHandle.ts
|
|
13
|
+
var AgentHandleImpl = class {
|
|
14
|
+
agentId;
|
|
15
|
+
imageId;
|
|
16
|
+
containerId;
|
|
17
|
+
sessionId;
|
|
18
|
+
ns;
|
|
19
|
+
constructor(ids, ns) {
|
|
20
|
+
this.agentId = ids.agentId;
|
|
21
|
+
this.imageId = ids.imageId;
|
|
22
|
+
this.containerId = ids.containerId;
|
|
23
|
+
this.sessionId = ids.sessionId;
|
|
24
|
+
this.ns = ns;
|
|
25
|
+
}
|
|
26
|
+
async send(content) {
|
|
27
|
+
return this.ns.session.send(this.agentId, content);
|
|
28
|
+
}
|
|
29
|
+
async interrupt() {
|
|
30
|
+
return this.ns.session.interrupt(this.agentId);
|
|
31
|
+
}
|
|
32
|
+
async history() {
|
|
33
|
+
return this.ns.image.getMessages(this.imageId);
|
|
34
|
+
}
|
|
35
|
+
async present(options) {
|
|
36
|
+
return this.ns.present.create(this.agentId, options);
|
|
37
|
+
}
|
|
38
|
+
async update(updates) {
|
|
39
|
+
await this.ns.image.update(this.imageId, updates);
|
|
40
|
+
}
|
|
41
|
+
async delete() {
|
|
42
|
+
await this.ns.agent.destroy(this.agentId);
|
|
43
|
+
await this.ns.image.delete(this.imageId);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
12
47
|
// src/namespaces/agents.ts
|
|
13
48
|
function createLocalAgents(runtime) {
|
|
14
49
|
return {
|
|
@@ -705,7 +740,7 @@ var Presentation = class {
|
|
|
705
740
|
this.state = addUserConversation(this.state, content);
|
|
706
741
|
this.notify();
|
|
707
742
|
try {
|
|
708
|
-
await this.agentx.session.send(this.agentId, content);
|
|
743
|
+
await this.agentx.runtime.session.send(this.agentId, content);
|
|
709
744
|
} catch (error) {
|
|
710
745
|
this.notifyError(error instanceof Error ? error : new Error(String(error)));
|
|
711
746
|
}
|
|
@@ -715,7 +750,7 @@ var Presentation = class {
|
|
|
715
750
|
*/
|
|
716
751
|
async interrupt() {
|
|
717
752
|
try {
|
|
718
|
-
await this.agentx.session.interrupt(this.agentId);
|
|
753
|
+
await this.agentx.runtime.session.interrupt(this.agentId);
|
|
719
754
|
} catch (error) {
|
|
720
755
|
this.notifyError(error instanceof Error ? error : new Error(String(error)));
|
|
721
756
|
}
|
|
@@ -774,16 +809,113 @@ var Presentation = class {
|
|
|
774
809
|
};
|
|
775
810
|
|
|
776
811
|
// src/namespaces/presentations.ts
|
|
777
|
-
function createPresentations(agentx) {
|
|
812
|
+
function createPresentations(agentx, imageNs) {
|
|
778
813
|
return {
|
|
779
814
|
async create(agentId, options) {
|
|
780
|
-
const messages = await
|
|
815
|
+
const messages = await imageNs.getMessages(agentId);
|
|
781
816
|
const conversations = messagesToConversations(messages);
|
|
782
817
|
return new Presentation(agentx, agentId, options, conversations);
|
|
783
818
|
}
|
|
784
819
|
};
|
|
785
820
|
}
|
|
786
821
|
|
|
822
|
+
// src/namespaces/prototypes.ts
|
|
823
|
+
function createLocalPrototypes(platform) {
|
|
824
|
+
return {
|
|
825
|
+
async create(params) {
|
|
826
|
+
const repo = platform.prototypeRepository;
|
|
827
|
+
if (!repo) {
|
|
828
|
+
throw new Error("Prototype repository not available");
|
|
829
|
+
}
|
|
830
|
+
const now = Date.now();
|
|
831
|
+
const random = Math.random().toString(36).slice(2, 8);
|
|
832
|
+
const record = {
|
|
833
|
+
prototypeId: `proto_${now}_${random}`,
|
|
834
|
+
containerId: params.containerId,
|
|
835
|
+
name: params.name,
|
|
836
|
+
description: params.description,
|
|
837
|
+
contextId: params.contextId,
|
|
838
|
+
embody: params.embody,
|
|
839
|
+
customData: params.customData,
|
|
840
|
+
createdAt: now,
|
|
841
|
+
updatedAt: now
|
|
842
|
+
};
|
|
843
|
+
await repo.savePrototype(record);
|
|
844
|
+
return { record, requestId: "" };
|
|
845
|
+
},
|
|
846
|
+
async get(prototypeId) {
|
|
847
|
+
const repo = platform.prototypeRepository;
|
|
848
|
+
if (!repo) {
|
|
849
|
+
throw new Error("Prototype repository not available");
|
|
850
|
+
}
|
|
851
|
+
const record = await repo.findPrototypeById(prototypeId);
|
|
852
|
+
return { record, requestId: "" };
|
|
853
|
+
},
|
|
854
|
+
async list(containerId) {
|
|
855
|
+
const repo = platform.prototypeRepository;
|
|
856
|
+
if (!repo) {
|
|
857
|
+
throw new Error("Prototype repository not available");
|
|
858
|
+
}
|
|
859
|
+
const records = containerId ? await repo.findPrototypesByContainerId(containerId) : await repo.findAllPrototypes();
|
|
860
|
+
return { records, requestId: "" };
|
|
861
|
+
},
|
|
862
|
+
async update(prototypeId, updates) {
|
|
863
|
+
const repo = platform.prototypeRepository;
|
|
864
|
+
if (!repo) {
|
|
865
|
+
throw new Error("Prototype repository not available");
|
|
866
|
+
}
|
|
867
|
+
const existing = await repo.findPrototypeById(prototypeId);
|
|
868
|
+
if (!existing) {
|
|
869
|
+
throw new Error(`Prototype not found: ${prototypeId}`);
|
|
870
|
+
}
|
|
871
|
+
const { embody: embodyUpdates, ...otherUpdates } = updates;
|
|
872
|
+
const updatedRecord = {
|
|
873
|
+
...existing,
|
|
874
|
+
...otherUpdates,
|
|
875
|
+
embody: embodyUpdates ? { ...existing.embody, ...embodyUpdates } : existing.embody,
|
|
876
|
+
updatedAt: Date.now()
|
|
877
|
+
};
|
|
878
|
+
await repo.savePrototype(updatedRecord);
|
|
879
|
+
return { record: updatedRecord, requestId: "" };
|
|
880
|
+
},
|
|
881
|
+
async delete(prototypeId) {
|
|
882
|
+
const repo = platform.prototypeRepository;
|
|
883
|
+
if (!repo) {
|
|
884
|
+
throw new Error("Prototype repository not available");
|
|
885
|
+
}
|
|
886
|
+
await repo.deletePrototype(prototypeId);
|
|
887
|
+
return { requestId: "" };
|
|
888
|
+
}
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
function createRemotePrototypes(rpcClient) {
|
|
892
|
+
return {
|
|
893
|
+
async create(params) {
|
|
894
|
+
const result = await rpcClient.call("prototype.create", params);
|
|
895
|
+
return { ...result, requestId: "" };
|
|
896
|
+
},
|
|
897
|
+
async get(prototypeId) {
|
|
898
|
+
const result = await rpcClient.call("prototype.get", { prototypeId });
|
|
899
|
+
return { ...result, requestId: "" };
|
|
900
|
+
},
|
|
901
|
+
async list(containerId) {
|
|
902
|
+
const result = await rpcClient.call("prototype.list", { containerId });
|
|
903
|
+
return { ...result, requestId: "" };
|
|
904
|
+
},
|
|
905
|
+
async update(prototypeId, updates) {
|
|
906
|
+
const result = await rpcClient.call("prototype.update", {
|
|
907
|
+
prototypeId,
|
|
908
|
+
updates
|
|
909
|
+
});
|
|
910
|
+
return { ...result, requestId: "" };
|
|
911
|
+
},
|
|
912
|
+
async delete(prototypeId) {
|
|
913
|
+
const result = await rpcClient.call("prototype.delete", { prototypeId });
|
|
914
|
+
return { ...result, requestId: "" };
|
|
915
|
+
}
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
|
|
787
919
|
// src/namespaces/sessions.ts
|
|
788
920
|
function createLocalSessions(runtime) {
|
|
789
921
|
return {
|
|
@@ -829,24 +961,27 @@ function createRemoteSessions(rpcClient) {
|
|
|
829
961
|
// src/LocalClient.ts
|
|
830
962
|
var logger = createLogger("agentx/LocalClient");
|
|
831
963
|
var LocalClient = class {
|
|
832
|
-
|
|
964
|
+
_runtime;
|
|
833
965
|
commandHandler = null;
|
|
834
966
|
isDisposed = false;
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
const
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
this.
|
|
967
|
+
chat;
|
|
968
|
+
runtime;
|
|
969
|
+
provider;
|
|
970
|
+
prototype;
|
|
971
|
+
constructor(agentxRuntime) {
|
|
972
|
+
this._runtime = agentxRuntime;
|
|
973
|
+
const platform = agentxRuntime.platform;
|
|
974
|
+
const container = createLocalContainers(platform);
|
|
975
|
+
const image = createLocalImages(platform);
|
|
976
|
+
const agent = createLocalAgents(agentxRuntime);
|
|
977
|
+
const session = createLocalSessions(agentxRuntime);
|
|
978
|
+
const llm = createLocalLLM(platform);
|
|
979
|
+
const prototype = createLocalPrototypes(platform);
|
|
980
|
+
const present = createPresentations(this, image);
|
|
981
|
+
this.runtime = { container, image, agent, session, present, llm, prototype };
|
|
982
|
+
this.provider = llm;
|
|
983
|
+
this.prototype = prototype;
|
|
984
|
+
this.chat = this.createChatNamespace();
|
|
850
985
|
logger.info("LocalClient initialized");
|
|
851
986
|
}
|
|
852
987
|
// ==================== Properties ====================
|
|
@@ -854,27 +989,27 @@ var LocalClient = class {
|
|
|
854
989
|
return !this.isDisposed;
|
|
855
990
|
}
|
|
856
991
|
get events() {
|
|
857
|
-
return this.
|
|
992
|
+
return this._runtime.platform.eventBus;
|
|
858
993
|
}
|
|
859
994
|
// ==================== Event Subscription ====================
|
|
860
995
|
on(type, handler) {
|
|
861
|
-
return this.
|
|
996
|
+
return this._runtime.platform.eventBus.on(type, handler);
|
|
862
997
|
}
|
|
863
998
|
onAny(handler) {
|
|
864
|
-
return this.
|
|
999
|
+
return this._runtime.platform.eventBus.onAny(handler);
|
|
865
1000
|
}
|
|
866
1001
|
subscribe(_sessionId) {
|
|
867
1002
|
}
|
|
868
1003
|
// ==================== Error Handling ====================
|
|
869
1004
|
onError(handler) {
|
|
870
|
-
return this.
|
|
1005
|
+
return this._runtime.platform.eventBus.on("agentx_error", (event) => {
|
|
871
1006
|
handler(event.data);
|
|
872
1007
|
});
|
|
873
1008
|
}
|
|
874
1009
|
// ==================== RPC ====================
|
|
875
1010
|
async rpc(method, params) {
|
|
876
1011
|
if (!this.commandHandler) {
|
|
877
|
-
this.commandHandler = new CommandHandler(this.
|
|
1012
|
+
this.commandHandler = new CommandHandler(this._runtime);
|
|
878
1013
|
}
|
|
879
1014
|
const result = await this.commandHandler.handle(method, params);
|
|
880
1015
|
if (result.success) {
|
|
@@ -882,12 +1017,66 @@ var LocalClient = class {
|
|
|
882
1017
|
}
|
|
883
1018
|
throw new Error(result.message);
|
|
884
1019
|
}
|
|
1020
|
+
// ==================== Private ====================
|
|
1021
|
+
createChatNamespace() {
|
|
1022
|
+
const instance = this.runtime;
|
|
1023
|
+
return {
|
|
1024
|
+
async create(params) {
|
|
1025
|
+
const containerId = "default";
|
|
1026
|
+
let mergedParams = { ...params };
|
|
1027
|
+
if (params.prototypeId) {
|
|
1028
|
+
const protoRes = await instance.prototype.get(params.prototypeId);
|
|
1029
|
+
if (protoRes.record) {
|
|
1030
|
+
const proto = protoRes.record;
|
|
1031
|
+
mergedParams = {
|
|
1032
|
+
name: proto.name,
|
|
1033
|
+
description: proto.description,
|
|
1034
|
+
contextId: proto.contextId,
|
|
1035
|
+
embody: proto.embody,
|
|
1036
|
+
customData: proto.customData,
|
|
1037
|
+
...params
|
|
1038
|
+
// inline params override prototype
|
|
1039
|
+
};
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
const { prototypeId: _pid, ...imageParams } = mergedParams;
|
|
1043
|
+
const imgRes = await instance.image.create({ containerId, ...imageParams });
|
|
1044
|
+
const agentRes = await instance.agent.create({ imageId: imgRes.record.imageId });
|
|
1045
|
+
return new AgentHandleImpl(
|
|
1046
|
+
{
|
|
1047
|
+
agentId: agentRes.agentId,
|
|
1048
|
+
imageId: agentRes.imageId,
|
|
1049
|
+
containerId: agentRes.containerId,
|
|
1050
|
+
sessionId: agentRes.sessionId
|
|
1051
|
+
},
|
|
1052
|
+
instance
|
|
1053
|
+
);
|
|
1054
|
+
},
|
|
1055
|
+
async list() {
|
|
1056
|
+
return instance.image.list();
|
|
1057
|
+
},
|
|
1058
|
+
async get(id) {
|
|
1059
|
+
const res = await instance.image.get(id);
|
|
1060
|
+
if (!res.record) return null;
|
|
1061
|
+
const r = res.record;
|
|
1062
|
+
return new AgentHandleImpl(
|
|
1063
|
+
{
|
|
1064
|
+
agentId: r.imageId,
|
|
1065
|
+
imageId: r.imageId,
|
|
1066
|
+
containerId: r.containerId,
|
|
1067
|
+
sessionId: r.sessionId
|
|
1068
|
+
},
|
|
1069
|
+
instance
|
|
1070
|
+
);
|
|
1071
|
+
}
|
|
1072
|
+
};
|
|
1073
|
+
}
|
|
885
1074
|
// ==================== Lifecycle ====================
|
|
886
1075
|
async disconnect() {
|
|
887
1076
|
}
|
|
888
1077
|
async dispose() {
|
|
889
1078
|
if (this.isDisposed) return;
|
|
890
|
-
await this.
|
|
1079
|
+
await this._runtime.shutdown();
|
|
891
1080
|
this.isDisposed = true;
|
|
892
1081
|
logger.info("LocalClient disposed");
|
|
893
1082
|
}
|
|
@@ -902,12 +1091,10 @@ var RemoteClient = class {
|
|
|
902
1091
|
config;
|
|
903
1092
|
eventBus;
|
|
904
1093
|
rpcClient;
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
presentation;
|
|
910
|
-
llm;
|
|
1094
|
+
chat;
|
|
1095
|
+
runtime;
|
|
1096
|
+
provider;
|
|
1097
|
+
prototype;
|
|
911
1098
|
constructor(config) {
|
|
912
1099
|
this.config = config;
|
|
913
1100
|
this.eventBus = new EventBusImpl();
|
|
@@ -923,12 +1110,17 @@ var RemoteClient = class {
|
|
|
923
1110
|
logger2.debug("Received stream event", { topic, type: event.type });
|
|
924
1111
|
this.eventBus.emit(event);
|
|
925
1112
|
});
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
1113
|
+
const container = createRemoteContainers(this.rpcClient);
|
|
1114
|
+
const image = createRemoteImages(this.rpcClient, (sessionId) => this.subscribe(sessionId));
|
|
1115
|
+
const agent = createRemoteAgents(this.rpcClient);
|
|
1116
|
+
const session = createRemoteSessions(this.rpcClient);
|
|
1117
|
+
const llm = createRemoteLLM(this.rpcClient);
|
|
1118
|
+
const prototype = createRemotePrototypes(this.rpcClient);
|
|
1119
|
+
const present = createPresentations(this, image);
|
|
1120
|
+
this.runtime = { container, image, agent, session, present, llm, prototype };
|
|
1121
|
+
this.provider = llm;
|
|
1122
|
+
this.prototype = prototype;
|
|
1123
|
+
this.chat = this.createChatNamespace();
|
|
932
1124
|
}
|
|
933
1125
|
// ==================== Properties ====================
|
|
934
1126
|
get connected() {
|
|
@@ -972,6 +1164,60 @@ var RemoteClient = class {
|
|
|
972
1164
|
async rpc(method, params) {
|
|
973
1165
|
return this.rpcClient.call(method, params);
|
|
974
1166
|
}
|
|
1167
|
+
// ==================== Private ====================
|
|
1168
|
+
createChatNamespace() {
|
|
1169
|
+
const instance = this.runtime;
|
|
1170
|
+
return {
|
|
1171
|
+
async create(params) {
|
|
1172
|
+
const containerId = "default";
|
|
1173
|
+
let mergedParams = { ...params };
|
|
1174
|
+
if (params.prototypeId) {
|
|
1175
|
+
const protoRes = await instance.prototype.get(params.prototypeId);
|
|
1176
|
+
if (protoRes.record) {
|
|
1177
|
+
const proto = protoRes.record;
|
|
1178
|
+
mergedParams = {
|
|
1179
|
+
name: proto.name,
|
|
1180
|
+
description: proto.description,
|
|
1181
|
+
contextId: proto.contextId,
|
|
1182
|
+
embody: proto.embody,
|
|
1183
|
+
customData: proto.customData,
|
|
1184
|
+
...params
|
|
1185
|
+
// inline params override prototype
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
const { prototypeId: _pid, ...imageParams } = mergedParams;
|
|
1190
|
+
const imgRes = await instance.image.create({ containerId, ...imageParams });
|
|
1191
|
+
const agentRes = await instance.agent.create({ imageId: imgRes.record.imageId });
|
|
1192
|
+
return new AgentHandleImpl(
|
|
1193
|
+
{
|
|
1194
|
+
agentId: agentRes.agentId,
|
|
1195
|
+
imageId: agentRes.imageId,
|
|
1196
|
+
containerId: agentRes.containerId,
|
|
1197
|
+
sessionId: agentRes.sessionId
|
|
1198
|
+
},
|
|
1199
|
+
instance
|
|
1200
|
+
);
|
|
1201
|
+
},
|
|
1202
|
+
async list() {
|
|
1203
|
+
return instance.image.list();
|
|
1204
|
+
},
|
|
1205
|
+
async get(id) {
|
|
1206
|
+
const res = await instance.image.get(id);
|
|
1207
|
+
if (!res.record) return null;
|
|
1208
|
+
const r = res.record;
|
|
1209
|
+
return new AgentHandleImpl(
|
|
1210
|
+
{
|
|
1211
|
+
agentId: r.imageId,
|
|
1212
|
+
imageId: r.imageId,
|
|
1213
|
+
containerId: r.containerId,
|
|
1214
|
+
sessionId: r.sessionId
|
|
1215
|
+
},
|
|
1216
|
+
instance
|
|
1217
|
+
);
|
|
1218
|
+
}
|
|
1219
|
+
};
|
|
1220
|
+
}
|
|
975
1221
|
};
|
|
976
1222
|
|
|
977
1223
|
// src/index.ts
|
|
@@ -999,23 +1245,17 @@ function createAgentX(config) {
|
|
|
999
1245
|
get events() {
|
|
1000
1246
|
return getLocalClient().events;
|
|
1001
1247
|
},
|
|
1002
|
-
get
|
|
1003
|
-
return getLocalClient().
|
|
1004
|
-
},
|
|
1005
|
-
get image() {
|
|
1006
|
-
return getLocalClient().image;
|
|
1007
|
-
},
|
|
1008
|
-
get agent() {
|
|
1009
|
-
return getLocalClient().agent;
|
|
1248
|
+
get runtime() {
|
|
1249
|
+
return getLocalClient().runtime;
|
|
1010
1250
|
},
|
|
1011
|
-
get
|
|
1012
|
-
return getLocalClient().
|
|
1251
|
+
get provider() {
|
|
1252
|
+
return getLocalClient().provider;
|
|
1013
1253
|
},
|
|
1014
|
-
get
|
|
1015
|
-
return getLocalClient().
|
|
1254
|
+
get chat() {
|
|
1255
|
+
return getLocalClient().chat;
|
|
1016
1256
|
},
|
|
1017
|
-
get
|
|
1018
|
-
return getLocalClient().
|
|
1257
|
+
get prototype() {
|
|
1258
|
+
return getLocalClient().prototype;
|
|
1019
1259
|
},
|
|
1020
1260
|
on(type, handler) {
|
|
1021
1261
|
return getLocalClient().on(type, handler);
|
|
@@ -1057,7 +1297,7 @@ function createAgentX(config) {
|
|
|
1057
1297
|
"serve() requires platform.channelServer. Ensure your platform supports server mode."
|
|
1058
1298
|
);
|
|
1059
1299
|
}
|
|
1060
|
-
const { createServer: createServer2 } = await import("./server-
|
|
1300
|
+
const { createServer: createServer2 } = await import("./server-UCQISBKH.js");
|
|
1061
1301
|
return createServer2({
|
|
1062
1302
|
platform: config.platform,
|
|
1063
1303
|
createDriver: config.createDriver,
|