@agentica/rpc 0.16.4 → 0.16.7

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.
@@ -0,0 +1,89 @@
1
+ import type { IAgenticaEventJson } from "@agentica/core";
2
+ /**
3
+ * RPC interface of Micro agent listener.
4
+ *
5
+ * `IMicroAgenticaRpcListener` is an interface defining an AI agent listener
6
+ * provided from the client to server through the RPC (Remote Procedure Call)
7
+ * paradigm in the websocket protocol.
8
+ *
9
+ * It has defined the event listener functions of {@link MicroAgenticaEvent}
10
+ * types. If you skip some event typed functions' implementations,
11
+ * the skipped event would be ignored.
12
+ *
13
+ * Also, the event like listener functions of `IMicroAgenticaRpcListener` type
14
+ * are remotely called when a client calls the
15
+ * {@link IAgenticaRpcService.conversate} function remotely, so that the
16
+ * server responses to the client by the event listener functions.
17
+ *
18
+ * You can connect to the WebSocket server of the AI agent like below:
19
+ *
20
+ * ```typescript
21
+ * import {
22
+ * IMicroAgenticaRpcListener,
23
+ * IAgenticaRpcService
24
+ * } from "@agentica/core";
25
+ * import { Driver, WebSocketConnector } from "tgrid";
26
+ *
27
+ * const connector: WebSocketConnector<
28
+ * null,
29
+ * IMicroAgenticaRpcListener,
30
+ * IAgenticaRpcService
31
+ * > = new WebSocketConnector(null, {
32
+ * text: async (evt) => {
33
+ * console.log(evt.role, evt.text);
34
+ * },
35
+ * describe: async (evt) => {
36
+ * console.log("describer", evt.text);
37
+ * },
38
+ * });
39
+ * await connector.connect("ws://localhost:3001");
40
+ *
41
+ * const driver: Driver<IAgenticaRpcService> = connector.getDriver();
42
+ * await driver.conversate("Hello, what you can do?");
43
+ * ```
44
+ *
45
+ * @author Samchon
46
+ */
47
+ export interface IMicroAgenticaRpcListener {
48
+ /**
49
+ * Describe the function executions' results.
50
+ *
51
+ * Inform description message of the function execution's results from
52
+ * the AI agent server to client.
53
+ *
54
+ * @param evt Event of a description of function execution results
55
+ */
56
+ describe: (evt: IAgenticaEventJson.IDescribe) => Promise<void>;
57
+ /**
58
+ * Text conversation message.
59
+ *
60
+ * @param evt Event of a text conversation message
61
+ */
62
+ text: (evt: IAgenticaEventJson.IText) => Promise<void>;
63
+ /**
64
+ * Call a function.
65
+ *
66
+ * Informs a function calling from the AI agent server to client.
67
+ *
68
+ * This event comes before the function execution, so that if you return
69
+ * a different value from the original {@link IAgenticaEventJson.ICall.arguments},
70
+ * you can modify the arguments of the function calling.
71
+ *
72
+ * Otherwise you do not return anything (`undefined`) or `null` value, the
73
+ * arguments of the function calling would not be modified. Also, if you are
74
+ * not interested in the function calling event, you can skit its
75
+ * implementation.
76
+ *
77
+ * @param evt Event of a function calling
78
+ * @return New arguments if you want to modify, otherwise null or undefined
79
+ */
80
+ call?: (evt: IAgenticaEventJson.ICall) => Promise<object | null | void | undefined>;
81
+ /**
82
+ * Executition of a function.
83
+ *
84
+ * Informs a function execution from the AI agent server to client.
85
+ *
86
+ * @param evt Event of a function execution
87
+ */
88
+ execute?: (evt: IAgenticaEventJson.IExecute) => Promise<void>;
89
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=IMicroAgenticaRpcListener.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IMicroAgenticaRpcListener.js","sourceRoot":"","sources":["../src/IMicroAgenticaRpcListener.ts"],"names":[],"mappings":""}
@@ -0,0 +1,79 @@
1
+ import type { IAgenticaController, MicroAgentica } from "@agentica/core";
2
+ import type { ILlmSchema } from "@samchon/openapi";
3
+ import type { IAgenticaRpcService } from "./IAgenticaRpcService";
4
+ import type { IMicroAgenticaRpcListener } from "./IMicroAgenticaRpcListener";
5
+ /**
6
+ * RPC service for the {@link MicroAgentica}.
7
+ *
8
+ * `MicroAgenticaRpcService` is class defining an AI agent service
9
+ * provided from the server to clients through the RPC (Remote Procedure Call)
10
+ * paradigm in the websocket protocol.
11
+ *
12
+ * Client connecting to the `MicroAgenticaRpcService` providing websocket server
13
+ * will call the {@link conversate} function remotely through its basic
14
+ * interface type {@link IAgenticaRpcService} with the RPC paradigm.
15
+ *
16
+ * Also, the client provides the {@link IMicroAgenticaRpcListener} type to the
17
+ * server, so that `MicroAgenticaRpcService` will remotely call the
18
+ * {@link IMicroAgenticaRpcListener listener}'s functions internally.
19
+ *
20
+ * You can open the WebSocket server of the AI agent like below:
21
+ *
22
+ * ```typescript
23
+ * import {
24
+ * IMicroAgenticaRpcListener,
25
+ * IAgenticaRpcService,
26
+ * Agentica,
27
+ * MicroAgenticaRpcService,
28
+ * } from "@agentica/core";
29
+ * import { WebSocketServer } from "tgrid";
30
+ *
31
+ * const server: WebSocketServer<
32
+ * null,
33
+ * IAgenticaRpcService,
34
+ * IMicroAgenticaRpcListener
35
+ * > = new WebSocketServer();
36
+ * await server.open(3001, async (acceptor) => {
37
+ * await acceptor.accept(
38
+ * new MicroAgenticaRpcService({
39
+ * agent: new MicroAgentica({ ... }),
40
+ * listener: acceptor.getDriver(),
41
+ * }),
42
+ * );
43
+ * });
44
+ * ```
45
+ *
46
+ * @author Samchon
47
+ */
48
+ export declare class MicroAgenticaRpcService<Model extends ILlmSchema.Model> implements IAgenticaRpcService<Model> {
49
+ private readonly props;
50
+ /**
51
+ * Initializer Constructor.
52
+ *
53
+ * @param props Properties to construct the RPC service
54
+ */
55
+ constructor(props: MicroAgenticaRpcService.IProps<Model>);
56
+ /**
57
+ * @inheritDoc
58
+ */
59
+ conversate(content: string): Promise<void>;
60
+ /**
61
+ * @inheritDoc
62
+ */
63
+ getControllers(): Promise<IAgenticaController<Model>[]>;
64
+ }
65
+ export declare namespace MicroAgenticaRpcService {
66
+ /**
67
+ * Properties to construct the RPC service.
68
+ */
69
+ interface IProps<Model extends ILlmSchema.Model> {
70
+ /**
71
+ * AI agent to be controlled.
72
+ */
73
+ agent: MicroAgentica<Model>;
74
+ /**
75
+ * Listener to be notified.
76
+ */
77
+ listener: IMicroAgenticaRpcListener;
78
+ }
79
+ }
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MicroAgenticaRpcService = void 0;
13
+ /**
14
+ * RPC service for the {@link MicroAgentica}.
15
+ *
16
+ * `MicroAgenticaRpcService` is class defining an AI agent service
17
+ * provided from the server to clients through the RPC (Remote Procedure Call)
18
+ * paradigm in the websocket protocol.
19
+ *
20
+ * Client connecting to the `MicroAgenticaRpcService` providing websocket server
21
+ * will call the {@link conversate} function remotely through its basic
22
+ * interface type {@link IAgenticaRpcService} with the RPC paradigm.
23
+ *
24
+ * Also, the client provides the {@link IMicroAgenticaRpcListener} type to the
25
+ * server, so that `MicroAgenticaRpcService` will remotely call the
26
+ * {@link IMicroAgenticaRpcListener listener}'s functions internally.
27
+ *
28
+ * You can open the WebSocket server of the AI agent like below:
29
+ *
30
+ * ```typescript
31
+ * import {
32
+ * IMicroAgenticaRpcListener,
33
+ * IAgenticaRpcService,
34
+ * Agentica,
35
+ * MicroAgenticaRpcService,
36
+ * } from "@agentica/core";
37
+ * import { WebSocketServer } from "tgrid";
38
+ *
39
+ * const server: WebSocketServer<
40
+ * null,
41
+ * IAgenticaRpcService,
42
+ * IMicroAgenticaRpcListener
43
+ * > = new WebSocketServer();
44
+ * await server.open(3001, async (acceptor) => {
45
+ * await acceptor.accept(
46
+ * new MicroAgenticaRpcService({
47
+ * agent: new MicroAgentica({ ... }),
48
+ * listener: acceptor.getDriver(),
49
+ * }),
50
+ * );
51
+ * });
52
+ * ```
53
+ *
54
+ * @author Samchon
55
+ */
56
+ class MicroAgenticaRpcService {
57
+ /**
58
+ * Initializer Constructor.
59
+ *
60
+ * @param props Properties to construct the RPC service
61
+ */
62
+ constructor(props) {
63
+ this.props = props;
64
+ const { agent, listener } = props;
65
+ // ESSENTIAL LISTENERS
66
+ agent.on("text", (evt) => __awaiter(this, void 0, void 0, function* () {
67
+ yield evt.join();
68
+ listener.text(evt.toJSON()).catch(() => { });
69
+ }));
70
+ agent.on("describe", (evt) => __awaiter(this, void 0, void 0, function* () {
71
+ yield evt.join();
72
+ listener.describe(evt.toJSON()).catch(() => { });
73
+ }));
74
+ // OPTIONAL LISTENERS
75
+ agent.on("call", (evt) => __awaiter(this, void 0, void 0, function* () {
76
+ const args = yield listener.call(evt.toJSON());
77
+ if (args != null) {
78
+ evt.arguments = args;
79
+ }
80
+ }));
81
+ agent.on("execute", (evt) => __awaiter(this, void 0, void 0, function* () {
82
+ listener.execute(evt.toJSON()).catch(() => { });
83
+ }));
84
+ }
85
+ /**
86
+ * @inheritDoc
87
+ */
88
+ conversate(content) {
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ yield this.props.agent.conversate(content);
91
+ });
92
+ }
93
+ /**
94
+ * @inheritDoc
95
+ */
96
+ getControllers() {
97
+ return __awaiter(this, void 0, void 0, function* () {
98
+ return this.props.agent.getControllers();
99
+ });
100
+ }
101
+ }
102
+ exports.MicroAgenticaRpcService = MicroAgenticaRpcService;
103
+ //# sourceMappingURL=MicroAgenticaRpcService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MicroAgenticaRpcService.js","sourceRoot":"","sources":["../src/MicroAgenticaRpcService.ts"],"names":[],"mappings":";;;;;;;;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,MAAa,uBAAuB;IAElC;;;;OAIG;IACH,YAAoC,KAA4C;QAA5C,UAAK,GAAL,KAAK,CAAuC;QAC9E,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC;QAElC,sBAAsB;QACtB,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,CAAO,GAAG,EAAE,EAAE;YAC7B,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;YACjB,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QAC9C,CAAC,CAAA,CAAC,CAAC;QACH,KAAK,CAAC,EAAE,CAAC,UAAU,EAAE,CAAO,GAAG,EAAE,EAAE;YACjC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;YACjB,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QAClD,CAAC,CAAA,CAAC,CAAC;QAEH,qBAAqB;QACrB,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,CAAO,GAAG,EAAE,EAAE;YAC7B,MAAM,IAAI,GAAqC,MAAM,QAAQ,CAAC,IAAK,CACjE,GAAG,CAAC,MAAM,EAAE,CACb,CAAC;YACF,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;gBACjB,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC;YACvB,CAAC;QACH,CAAC,CAAA,CAAC,CAAC;QACH,KAAK,CAAC,EAAE,CAAC,SAAS,EAAE,CAAO,GAAG,EAAE,EAAE;YAChC,QAAQ,CAAC,OAAQ,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QAClD,CAAC,CAAA,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACU,UAAU,CAAC,OAAe;;YACrC,MAAM,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QAC7C,CAAC;KAAA;IAED;;OAEG;IACU,cAAc;;YACzB,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,cAAc,EAAkC,CAAC;QAC3E,CAAC;KAAA;CACF;AA/CD,0DA+CC"}
package/lib/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  export * from "./AgenticaRpcService";
2
2
  export * from "./IAgenticaRpcListener";
3
3
  export * from "./IAgenticaRpcService";
4
+ export * from "./IMicroAgenticaRpcListener";
5
+ export * from "./MicroAgenticaRpcService";
package/lib/index.js CHANGED
@@ -17,4 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./AgenticaRpcService"), exports);
18
18
  __exportStar(require("./IAgenticaRpcListener"), exports);
19
19
  __exportStar(require("./IAgenticaRpcService"), exports);
20
+ __exportStar(require("./IMicroAgenticaRpcListener"), exports);
21
+ __exportStar(require("./MicroAgenticaRpcService"), exports);
20
22
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uDAAqC;AACrC,yDAAuC;AACvC,wDAAsC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,uDAAqC;AACrC,yDAAuC;AACvC,wDAAsC;AACtC,8DAA4C;AAC5C,4DAA0C"}
package/lib/index.mjs CHANGED
@@ -37,5 +37,35 @@ class AgenticaRpcService {
37
37
  }
38
38
  }
39
39
 
40
- export { AgenticaRpcService };
40
+ class MicroAgenticaRpcService {
41
+ constructor(props) {
42
+ this.props = props;
43
+ const {agent, listener} = props;
44
+ agent.on("text", (async evt => {
45
+ await evt.join();
46
+ listener.text(evt.toJSON()).catch((() => {}));
47
+ }));
48
+ agent.on("describe", (async evt => {
49
+ await evt.join();
50
+ listener.describe(evt.toJSON()).catch((() => {}));
51
+ }));
52
+ agent.on("call", (async evt => {
53
+ const args = await listener.call(evt.toJSON());
54
+ if (args != null) {
55
+ evt.arguments = args;
56
+ }
57
+ }));
58
+ agent.on("execute", (async evt => {
59
+ listener.execute(evt.toJSON()).catch((() => {}));
60
+ }));
61
+ }
62
+ async conversate(content) {
63
+ await this.props.agent.conversate(content);
64
+ }
65
+ async getControllers() {
66
+ return this.props.agent.getControllers();
67
+ }
68
+ }
69
+
70
+ export { AgenticaRpcService, MicroAgenticaRpcService };
41
71
  //# sourceMappingURL=index.mjs.map
package/lib/index.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/AgenticaRpcService.ts"],"sourcesContent":[null],"names":["AgenticaRpcService","constructor","props","this","agent","listener","on","async","evt","join","text","toJSON","catch","describe","initialize","select","cancel","args","call","arguments","execute","conversate","content","getControllers"],"mappings":"MAiDaA;IAOX,WAAAC,CAAoCC;QAAAC,KAAKD,QAALA;QAClC,OAAME,OAAOC,YAAeH;QAG5BE,MAAME,GAAG,SAAQC,MAAOC;kBAChBA,IAAIC;YACVJ,SAASK,KAAKF,IAAIG,UAAUC,OAAM;AAAS;QAE7CR,MAAME,GAAG,aAAYC,MAAOC;kBACpBA,IAAIC;YACVJ,SAASQ,SAASL,IAAIG,UAAUC,OAAM;AAAS;QAIjDR,MAAME,GAAG,eAAcC,MAAOC;YAC5BH,SAASS,WAAYN,IAAIG,UAAUC,OAAM;AAAS;QAEpDR,MAAME,GAAG,WAAUC,MAAOC;YACxBH,SAASU,OAAQP,IAAIG,UAAUC,OAAM;AAAS;QAEhDR,MAAME,GAAG,WAAUC,MAAOC;YACxBH,SAASW,OAAQR,IAAIG,UAAUC,OAAM;AAAS;QAEhDR,MAAME,GAAG,SAAQC,MAAOC;YACtB,MAAMS,aAA+CZ,SAASa,KAC5DV,IAAIG;YAEN,IAAIM,QAAQ,MAAM;gBAChBT,IAAIW,YAAYF;;;QAGpBb,MAAME,GAAG,YAAWC,MAAOC;YACzBH,SAASe,QAASZ,IAAIG,UAAUC,OAAM;AAAS;;IAO5C,gBAAMS,CAAWC;cAChBnB,KAAKD,MAAME,MAAMiB,WAAWC;;IAM7B,oBAAMC;QACX,OAAOpB,KAAKD,MAAME,MAAMmB;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":["../src/AgenticaRpcService.ts","../src/MicroAgenticaRpcService.ts"],"sourcesContent":[null,null],"names":["AgenticaRpcService","constructor","props","this","agent","listener","on","async","evt","join","text","toJSON","catch","describe","initialize","select","cancel","args","call","arguments","execute","conversate","content","getControllers","MicroAgenticaRpcService"],"mappings":"MAiDaA;IAOX,WAAAC,CAAoCC;QAAAC,KAAKD,QAALA;QAClC,OAAME,OAAOC,YAAeH;QAG5BE,MAAME,GAAG,SAAQC,MAAOC;kBAChBA,IAAIC;YACVJ,SAASK,KAAKF,IAAIG,UAAUC,OAAM;AAAS;QAE7CR,MAAME,GAAG,aAAYC,MAAOC;kBACpBA,IAAIC;YACVJ,SAASQ,SAASL,IAAIG,UAAUC,OAAM;AAAS;QAIjDR,MAAME,GAAG,eAAcC,MAAOC;YAC5BH,SAASS,WAAYN,IAAIG,UAAUC,OAAM;AAAS;QAEpDR,MAAME,GAAG,WAAUC,MAAOC;YACxBH,SAASU,OAAQP,IAAIG,UAAUC,OAAM;AAAS;QAEhDR,MAAME,GAAG,WAAUC,MAAOC;YACxBH,SAASW,OAAQR,IAAIG,UAAUC,OAAM;AAAS;QAEhDR,MAAME,GAAG,SAAQC,MAAOC;YACtB,MAAMS,aAA+CZ,SAASa,KAC5DV,IAAIG;YAEN,IAAIM,QAAQ,MAAM;gBAChBT,IAAIW,YAAYF;;;QAGpBb,MAAME,GAAG,YAAWC,MAAOC;YACzBH,SAASe,QAASZ,IAAIG,UAAUC,OAAM;AAAS;;IAO5C,gBAAMS,CAAWC;cAChBnB,KAAKD,MAAME,MAAMiB,WAAWC;;IAM7B,oBAAMC;QACX,OAAOpB,KAAKD,MAAME,MAAMmB;;;;MCtDfC;IAOX,WAAAvB,CAAoCC;QAAAC,KAAKD,QAALA;QAClC,OAAME,OAAOC,YAAeH;QAG5BE,MAAME,GAAG,SAAQC,MAAOC;kBAChBA,IAAIC;YACVJ,SAASK,KAAKF,IAAIG,UAAUC,OAAM;AAAS;QAE7CR,MAAME,GAAG,aAAYC,MAAOC;kBACpBA,IAAIC;YACVJ,SAASQ,SAASL,IAAIG,UAAUC,OAAM;AAAS;QAIjDR,MAAME,GAAG,SAAQC,MAAOC;YACtB,MAAMS,aAA+CZ,SAASa,KAC5DV,IAAIG;YAEN,IAAIM,QAAQ,MAAM;gBAChBT,IAAIW,YAAYF;;;QAGpBb,MAAME,GAAG,YAAWC,MAAOC;YACzBH,SAASe,QAASZ,IAAIG,UAAUC,OAAM;AAAS;;IAO5C,gBAAMS,CAAWC;cAChBnB,KAAKD,MAAME,MAAMiB,WAAWC;;IAM7B,oBAAMC;QACX,OAAOpB,KAAKD,MAAME,MAAMmB;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentica/rpc",
3
- "version": "0.16.4",
3
+ "version": "0.16.7",
4
4
  "description": "Agentic AI Library specialized in LLM Function Calling",
5
5
  "author": "Wrtn Technologies",
6
6
  "license": "MIT",
@@ -37,7 +37,7 @@
37
37
  "dependencies": {
38
38
  "@samchon/openapi": "^4.0.0",
39
39
  "typia": "^9.0.1",
40
- "@agentica/core": "^0.16.4"
40
+ "@agentica/core": "^0.16.7"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@rollup/plugin-terser": "^0.4.4",
@@ -0,0 +1,93 @@
1
+ import type { IAgenticaEventJson } from "@agentica/core";
2
+
3
+ /**
4
+ * RPC interface of Micro agent listener.
5
+ *
6
+ * `IMicroAgenticaRpcListener` is an interface defining an AI agent listener
7
+ * provided from the client to server through the RPC (Remote Procedure Call)
8
+ * paradigm in the websocket protocol.
9
+ *
10
+ * It has defined the event listener functions of {@link MicroAgenticaEvent}
11
+ * types. If you skip some event typed functions' implementations,
12
+ * the skipped event would be ignored.
13
+ *
14
+ * Also, the event like listener functions of `IMicroAgenticaRpcListener` type
15
+ * are remotely called when a client calls the
16
+ * {@link IAgenticaRpcService.conversate} function remotely, so that the
17
+ * server responses to the client by the event listener functions.
18
+ *
19
+ * You can connect to the WebSocket server of the AI agent like below:
20
+ *
21
+ * ```typescript
22
+ * import {
23
+ * IMicroAgenticaRpcListener,
24
+ * IAgenticaRpcService
25
+ * } from "@agentica/core";
26
+ * import { Driver, WebSocketConnector } from "tgrid";
27
+ *
28
+ * const connector: WebSocketConnector<
29
+ * null,
30
+ * IMicroAgenticaRpcListener,
31
+ * IAgenticaRpcService
32
+ * > = new WebSocketConnector(null, {
33
+ * text: async (evt) => {
34
+ * console.log(evt.role, evt.text);
35
+ * },
36
+ * describe: async (evt) => {
37
+ * console.log("describer", evt.text);
38
+ * },
39
+ * });
40
+ * await connector.connect("ws://localhost:3001");
41
+ *
42
+ * const driver: Driver<IAgenticaRpcService> = connector.getDriver();
43
+ * await driver.conversate("Hello, what you can do?");
44
+ * ```
45
+ *
46
+ * @author Samchon
47
+ */
48
+ export interface IMicroAgenticaRpcListener {
49
+ /**
50
+ * Describe the function executions' results.
51
+ *
52
+ * Inform description message of the function execution's results from
53
+ * the AI agent server to client.
54
+ *
55
+ * @param evt Event of a description of function execution results
56
+ */
57
+ describe: (evt: IAgenticaEventJson.IDescribe) => Promise<void>;
58
+
59
+ /**
60
+ * Text conversation message.
61
+ *
62
+ * @param evt Event of a text conversation message
63
+ */
64
+ text: (evt: IAgenticaEventJson.IText) => Promise<void>;
65
+
66
+ /**
67
+ * Call a function.
68
+ *
69
+ * Informs a function calling from the AI agent server to client.
70
+ *
71
+ * This event comes before the function execution, so that if you return
72
+ * a different value from the original {@link IAgenticaEventJson.ICall.arguments},
73
+ * you can modify the arguments of the function calling.
74
+ *
75
+ * Otherwise you do not return anything (`undefined`) or `null` value, the
76
+ * arguments of the function calling would not be modified. Also, if you are
77
+ * not interested in the function calling event, you can skit its
78
+ * implementation.
79
+ *
80
+ * @param evt Event of a function calling
81
+ * @return New arguments if you want to modify, otherwise null or undefined
82
+ */
83
+ call?: (evt: IAgenticaEventJson.ICall) => Promise<object | null | void | undefined>;
84
+
85
+ /**
86
+ * Executition of a function.
87
+ *
88
+ * Informs a function execution from the AI agent server to client.
89
+ *
90
+ * @param evt Event of a function execution
91
+ */
92
+ execute?: (evt: IAgenticaEventJson.IExecute) => Promise<void>;
93
+ }
@@ -0,0 +1,113 @@
1
+ import type { IAgenticaController, MicroAgentica } from "@agentica/core";
2
+ import type { ILlmSchema } from "@samchon/openapi";
3
+
4
+ import type { IAgenticaRpcService } from "./IAgenticaRpcService";
5
+ import type { IMicroAgenticaRpcListener } from "./IMicroAgenticaRpcListener";
6
+
7
+ /**
8
+ * RPC service for the {@link MicroAgentica}.
9
+ *
10
+ * `MicroAgenticaRpcService` is class defining an AI agent service
11
+ * provided from the server to clients through the RPC (Remote Procedure Call)
12
+ * paradigm in the websocket protocol.
13
+ *
14
+ * Client connecting to the `MicroAgenticaRpcService` providing websocket server
15
+ * will call the {@link conversate} function remotely through its basic
16
+ * interface type {@link IAgenticaRpcService} with the RPC paradigm.
17
+ *
18
+ * Also, the client provides the {@link IMicroAgenticaRpcListener} type to the
19
+ * server, so that `MicroAgenticaRpcService` will remotely call the
20
+ * {@link IMicroAgenticaRpcListener listener}'s functions internally.
21
+ *
22
+ * You can open the WebSocket server of the AI agent like below:
23
+ *
24
+ * ```typescript
25
+ * import {
26
+ * IMicroAgenticaRpcListener,
27
+ * IAgenticaRpcService,
28
+ * Agentica,
29
+ * MicroAgenticaRpcService,
30
+ * } from "@agentica/core";
31
+ * import { WebSocketServer } from "tgrid";
32
+ *
33
+ * const server: WebSocketServer<
34
+ * null,
35
+ * IAgenticaRpcService,
36
+ * IMicroAgenticaRpcListener
37
+ * > = new WebSocketServer();
38
+ * await server.open(3001, async (acceptor) => {
39
+ * await acceptor.accept(
40
+ * new MicroAgenticaRpcService({
41
+ * agent: new MicroAgentica({ ... }),
42
+ * listener: acceptor.getDriver(),
43
+ * }),
44
+ * );
45
+ * });
46
+ * ```
47
+ *
48
+ * @author Samchon
49
+ */
50
+ export class MicroAgenticaRpcService<Model extends ILlmSchema.Model>
51
+ implements IAgenticaRpcService<Model> {
52
+ /**
53
+ * Initializer Constructor.
54
+ *
55
+ * @param props Properties to construct the RPC service
56
+ */
57
+ public constructor(private readonly props: MicroAgenticaRpcService.IProps<Model>) {
58
+ const { agent, listener } = props;
59
+
60
+ // ESSENTIAL LISTENERS
61
+ agent.on("text", async (evt) => {
62
+ await evt.join();
63
+ listener.text(evt.toJSON()).catch(() => {});
64
+ });
65
+ agent.on("describe", async (evt) => {
66
+ await evt.join();
67
+ listener.describe(evt.toJSON()).catch(() => {});
68
+ });
69
+
70
+ // OPTIONAL LISTENERS
71
+ agent.on("call", async (evt) => {
72
+ const args: object | null | undefined | void = await listener.call!(
73
+ evt.toJSON(),
74
+ );
75
+ if (args != null) {
76
+ evt.arguments = args;
77
+ }
78
+ });
79
+ agent.on("execute", async (evt) => {
80
+ listener.execute!(evt.toJSON()).catch(() => {});
81
+ });
82
+ }
83
+
84
+ /**
85
+ * @inheritDoc
86
+ */
87
+ public async conversate(content: string): Promise<void> {
88
+ await this.props.agent.conversate(content);
89
+ }
90
+
91
+ /**
92
+ * @inheritDoc
93
+ */
94
+ public async getControllers(): Promise<IAgenticaController<Model>[]> {
95
+ return this.props.agent.getControllers() as IAgenticaController<Model>[];
96
+ }
97
+ }
98
+ export namespace MicroAgenticaRpcService {
99
+ /**
100
+ * Properties to construct the RPC service.
101
+ */
102
+ export interface IProps<Model extends ILlmSchema.Model> {
103
+ /**
104
+ * AI agent to be controlled.
105
+ */
106
+ agent: MicroAgentica<Model>;
107
+
108
+ /**
109
+ * Listener to be notified.
110
+ */
111
+ listener: IMicroAgenticaRpcListener;
112
+ }
113
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  export * from "./AgenticaRpcService";
2
2
  export * from "./IAgenticaRpcListener";
3
3
  export * from "./IAgenticaRpcService";
4
+ export * from "./IMicroAgenticaRpcListener";
5
+ export * from "./MicroAgenticaRpcService";