@meshagent/meshagent 0.0.11 → 0.0.13

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.
Files changed (127) hide show
  1. package/dist/browser/agent-client.d.ts +88 -0
  2. package/dist/browser/agent-client.js +174 -0
  3. package/dist/browser/agent.d.ts +103 -0
  4. package/dist/browser/agent.js +229 -0
  5. package/dist/browser/client.d.ts +90 -0
  6. package/dist/browser/client.js +449 -0
  7. package/dist/browser/completer.d.ts +9 -0
  8. package/dist/browser/completer.js +25 -0
  9. package/dist/browser/data-types.d.ts +44 -0
  10. package/dist/browser/data-types.js +119 -0
  11. package/dist/browser/database-client.d.ts +77 -0
  12. package/dist/browser/database-client.js +113 -0
  13. package/dist/browser/developer-client.d.ts +13 -0
  14. package/dist/browser/developer-client.js +35 -0
  15. package/dist/browser/document.d.ts +84 -0
  16. package/dist/browser/document.js +531 -0
  17. package/dist/browser/entrypoint.d.ts +49722 -0
  18. package/dist/browser/entrypoint.js +8659 -0
  19. package/dist/browser/event-emitter.d.ts +13 -0
  20. package/dist/browser/event-emitter.js +38 -0
  21. package/dist/browser/helpers.d.ts +26 -0
  22. package/dist/browser/helpers.js +79 -0
  23. package/dist/browser/index.d.ts +24 -0
  24. package/dist/browser/index.js +40 -0
  25. package/dist/browser/messaging-client.d.ts +76 -0
  26. package/dist/browser/messaging-client.js +248 -0
  27. package/dist/browser/participant-token.d.ts +36 -0
  28. package/dist/browser/participant-token.js +104 -0
  29. package/dist/browser/participant.d.ts +18 -0
  30. package/dist/browser/participant.js +42 -0
  31. package/dist/browser/protocol.d.ts +87 -0
  32. package/dist/browser/protocol.js +305 -0
  33. package/dist/browser/queues-client.d.ts +26 -0
  34. package/dist/browser/queues-client.js +47 -0
  35. package/dist/browser/requirement.d.ts +25 -0
  36. package/dist/browser/requirement.js +49 -0
  37. package/dist/browser/response.d.ts +60 -0
  38. package/dist/browser/response.js +138 -0
  39. package/dist/browser/room-client.d.ts +43 -0
  40. package/dist/browser/room-client.js +109 -0
  41. package/dist/browser/room-event.d.ts +60 -0
  42. package/dist/browser/room-event.js +82 -0
  43. package/dist/browser/room-server-client.d.ts +19 -0
  44. package/dist/browser/room-server-client.js +50 -0
  45. package/dist/browser/runtime.d.ts +6 -0
  46. package/dist/browser/runtime.js +8 -0
  47. package/dist/browser/schema.d.ts +83 -0
  48. package/dist/browser/schema.js +321 -0
  49. package/dist/browser/storage-client.d.ts +38 -0
  50. package/dist/browser/storage-client.js +86 -0
  51. package/dist/browser/stream-controller.d.ts +8 -0
  52. package/dist/browser/stream-controller.js +55 -0
  53. package/dist/browser/sync-client.d.ts +33 -0
  54. package/dist/browser/sync-client.js +123 -0
  55. package/dist/browser/utils.d.ts +14 -0
  56. package/dist/browser/utils.js +59 -0
  57. package/dist/node/agent-client.d.ts +88 -0
  58. package/dist/node/agent-client.js +174 -0
  59. package/dist/node/agent.d.ts +103 -0
  60. package/dist/node/agent.js +229 -0
  61. package/dist/node/client.d.ts +90 -0
  62. package/dist/node/client.js +449 -0
  63. package/dist/node/completer.d.ts +9 -0
  64. package/dist/node/completer.js +25 -0
  65. package/dist/node/data-types.d.ts +44 -0
  66. package/dist/node/data-types.js +119 -0
  67. package/dist/node/database-client.d.ts +77 -0
  68. package/dist/node/database-client.js +113 -0
  69. package/dist/node/developer-client.d.ts +13 -0
  70. package/dist/node/developer-client.js +35 -0
  71. package/dist/node/document.d.ts +84 -0
  72. package/dist/node/document.js +531 -0
  73. package/dist/node/entrypoint.d.ts +49722 -0
  74. package/dist/node/entrypoint.js +6321 -0
  75. package/dist/node/event-emitter.d.ts +13 -0
  76. package/dist/node/event-emitter.js +38 -0
  77. package/dist/node/helpers.d.ts +26 -0
  78. package/dist/node/helpers.js +79 -0
  79. package/dist/node/index.d.ts +24 -0
  80. package/dist/node/index.js +40 -0
  81. package/dist/node/messaging-client.d.ts +76 -0
  82. package/dist/node/messaging-client.js +248 -0
  83. package/dist/node/participant-token.d.ts +36 -0
  84. package/dist/node/participant-token.js +104 -0
  85. package/dist/node/participant.d.ts +18 -0
  86. package/dist/node/participant.js +42 -0
  87. package/dist/node/protocol.d.ts +87 -0
  88. package/dist/node/protocol.js +305 -0
  89. package/dist/node/queues-client.d.ts +26 -0
  90. package/dist/node/queues-client.js +47 -0
  91. package/dist/node/requirement.d.ts +25 -0
  92. package/dist/node/requirement.js +49 -0
  93. package/dist/node/response.d.ts +60 -0
  94. package/dist/node/response.js +138 -0
  95. package/dist/node/room-client.d.ts +43 -0
  96. package/dist/node/room-client.js +109 -0
  97. package/dist/node/room-event.d.ts +60 -0
  98. package/dist/node/room-event.js +82 -0
  99. package/dist/node/room-server-client.d.ts +19 -0
  100. package/dist/node/room-server-client.js +50 -0
  101. package/dist/node/runtime.d.ts +6 -0
  102. package/dist/node/runtime.js +8 -0
  103. package/dist/node/schema.d.ts +83 -0
  104. package/dist/node/schema.js +321 -0
  105. package/dist/node/storage-client.d.ts +38 -0
  106. package/dist/node/storage-client.js +86 -0
  107. package/dist/node/stream-controller.d.ts +8 -0
  108. package/dist/node/stream-controller.js +55 -0
  109. package/dist/node/sync-client.d.ts +33 -0
  110. package/dist/node/sync-client.js +123 -0
  111. package/dist/node/utils.d.ts +14 -0
  112. package/dist/node/utils.js +59 -0
  113. package/package.json +35 -22
  114. package/README.md +0 -0
  115. package/babel.config.cjs +0 -3
  116. package/client.js +0 -422
  117. package/dist/entrypoint.js +0 -11262
  118. package/dist/entrypoint.js.map +0 -7
  119. package/document-server-client.js +0 -246
  120. package/entrypoint.js +0 -11568
  121. package/index.js +0 -2
  122. package/protocol.js +0 -459
  123. package/server.js +0 -310
  124. package/test/assert.js +0 -19
  125. package/test/document-server-client.test.js +0 -33
  126. package/test/document.test.js +0 -313
  127. package/test/protocol.test.js +0 -64
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RefCount = exports.decoder = exports.encoder = void 0;
4
+ exports.splitMessagePayload = splitMessagePayload;
5
+ exports.splitMessageHeader = splitMessageHeader;
6
+ exports.packMessage = packMessage;
7
+ exports.mergeUint8Arrays = mergeUint8Arrays;
8
+ exports.blobToArrayBuffer = blobToArrayBuffer;
9
+ const text_encoding_1 = require("@kayahr/text-encoding");
10
+ const encoder = new text_encoding_1.TextEncoder();
11
+ exports.encoder = encoder;
12
+ const decoder = new text_encoding_1.TextDecoder();
13
+ exports.decoder = decoder;
14
+ function splitMessagePayload(packet) {
15
+ const dataView = new DataView(packet.buffer, packet.byteOffset, packet.byteLength);
16
+ const headerSize = dataView.getUint32(4, false) + dataView.getUint32(0, false) * Math.pow(2, 32);
17
+ return packet.subarray(8 + headerSize, packet.length);
18
+ }
19
+ function splitMessageHeader(packet) {
20
+ const dataView = new DataView(packet.buffer, packet.byteOffset, packet.byteLength);
21
+ const headerSize = dataView.getUint32(4, false) + dataView.getUint32(0, false) * Math.pow(2, 32);
22
+ const payload = packet.subarray(8, 8 + headerSize);
23
+ return decoder.decode(payload);
24
+ }
25
+ function packMessage(request, data) {
26
+ const jsonMessage = encoder.encode(JSON.stringify(request));
27
+ const size = jsonMessage.length;
28
+ const header = new Uint8Array(4 * 2);
29
+ const dataView = new DataView(header.buffer);
30
+ dataView.setUint32(0, (size & 0x000fffff00000000) / Math.pow(2, 32), false);
31
+ dataView.setUint32(4, size & 0xffffffff, false);
32
+ return mergeUint8Arrays(header, jsonMessage, data ?? new Uint8Array(0));
33
+ }
34
+ function mergeUint8Arrays(...arrays) {
35
+ const totalSize = arrays.reduce((acc, e) => acc + e.length, 0);
36
+ const merged = new Uint8Array(totalSize);
37
+ arrays.forEach((array, i, arrays) => {
38
+ const offset = arrays
39
+ .slice(0, i)
40
+ .reduce((acc, e) => acc + e.length, 0);
41
+ merged.set(array, offset);
42
+ });
43
+ return merged;
44
+ }
45
+ function blobToArrayBuffer(blob) {
46
+ return new Promise((resolve, reject) => {
47
+ const reader = new FileReader();
48
+ reader.onload = () => resolve(reader.result);
49
+ reader.onerror = () => reject(reader.error);
50
+ reader.readAsArrayBuffer(blob);
51
+ });
52
+ }
53
+ class RefCount {
54
+ constructor(ref) {
55
+ this.ref = ref;
56
+ this.count = 1;
57
+ }
58
+ }
59
+ exports.RefCount = RefCount;
@@ -0,0 +1,88 @@
1
+ import { RoomClient } from "./room-client";
2
+ import { Response } from "./response";
3
+ import { Requirement } from "./requirement";
4
+ export declare class AgentDescription {
5
+ readonly name: string;
6
+ readonly title: string;
7
+ readonly description: string;
8
+ readonly outputSchema?: Record<string, any>;
9
+ readonly inputSchema?: Record<string, any>;
10
+ readonly requires: Requirement[];
11
+ readonly labels: string[];
12
+ readonly supportsTools: boolean;
13
+ constructor({ name, title, description, outputSchema, inputSchema, requires, labels, supportsTools, }: {
14
+ name: string;
15
+ title: string;
16
+ description: string;
17
+ outputSchema?: Record<string, any>;
18
+ inputSchema?: Record<string, any>;
19
+ requires: Requirement[];
20
+ labels: string[];
21
+ supportsTools: boolean;
22
+ });
23
+ static fromJson(a: Record<string, any>): AgentDescription;
24
+ }
25
+ export declare class ToolDescription {
26
+ title: string;
27
+ name: string;
28
+ description: string;
29
+ inputSchema: Record<string, any>;
30
+ defs?: Record<string, any>;
31
+ thumbnailUrl?: string;
32
+ constructor({ title, name, description, inputSchema, thumbnailUrl, defs }: {
33
+ title: string;
34
+ name: string;
35
+ description: string;
36
+ inputSchema: Record<string, any>;
37
+ thumbnailUrl?: string;
38
+ defs?: Record<string, any>;
39
+ });
40
+ }
41
+ export declare class ToolkitDescription {
42
+ readonly title: string;
43
+ readonly name: string;
44
+ readonly description: string;
45
+ readonly tools: ToolDescription[];
46
+ readonly thumbnailUrl?: string;
47
+ private _byName;
48
+ constructor({ title, name, description, tools, thumbnailUrl }: {
49
+ title: string;
50
+ name: string;
51
+ description: string;
52
+ tools: ToolDescription[];
53
+ thumbnailUrl?: string;
54
+ });
55
+ getTool(name: string): ToolDescription | undefined;
56
+ static fromJson(json: Record<string, any>, opts?: {
57
+ name?: string;
58
+ }): ToolkitDescription;
59
+ }
60
+ export declare class ToolkitConfiguration {
61
+ name: string;
62
+ use?: string[] | undefined;
63
+ constructor(name: string, use?: string[] | undefined);
64
+ toJson(): Record<string, any>;
65
+ }
66
+ export declare class AgentsClient {
67
+ private client;
68
+ constructor({ room }: {
69
+ room: RoomClient;
70
+ });
71
+ call(params: {
72
+ name: string;
73
+ url: string;
74
+ arguments: Record<string, any>;
75
+ }): Promise<void>;
76
+ ask(params: {
77
+ agentName: string;
78
+ toolkits?: ToolkitConfiguration[];
79
+ arguments: Record<string, any>;
80
+ }): Promise<Record<string, any>>;
81
+ listToolkits(): Promise<ToolkitDescription[]>;
82
+ listAgents(): Promise<AgentDescription[]>;
83
+ invokeTool(params: {
84
+ toolkit: string;
85
+ tool: string;
86
+ arguments: Record<string, any>;
87
+ }): Promise<Response>;
88
+ }
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AgentsClient = exports.ToolkitConfiguration = exports.ToolkitDescription = exports.ToolDescription = exports.AgentDescription = void 0;
4
+ const requirement_1 = require("./requirement");
5
+ class AgentDescription {
6
+ constructor({ name, title, description, outputSchema, inputSchema, requires, labels, supportsTools, }) {
7
+ this.name = name;
8
+ this.title = title;
9
+ this.description = description;
10
+ this.outputSchema = outputSchema;
11
+ this.inputSchema = inputSchema;
12
+ this.requires = requires;
13
+ this.labels = labels;
14
+ this.supportsTools = supportsTools;
15
+ }
16
+ static fromJson(a) {
17
+ let requires = [];
18
+ if (Array.isArray(a["requires"])) {
19
+ requires = a["requires"].map((e) => {
20
+ if (e["toolkit"] != null) {
21
+ return requirement_1.RequiredToolkit.fromJson(e);
22
+ }
23
+ if (e["schema"] != null) {
24
+ return requirement_1.RequiredSchema.fromJson(e);
25
+ }
26
+ throw new Error("Invalid requirement object");
27
+ });
28
+ }
29
+ let labels = [];
30
+ if (Array.isArray(a["labels"])) {
31
+ labels = a["labels"].filter((item) => typeof item === "string");
32
+ }
33
+ return new AgentDescription({
34
+ name: a["name"],
35
+ title: a["title"] ?? "",
36
+ description: a["description"] ?? "",
37
+ inputSchema: a["input_schema"] ?? undefined,
38
+ outputSchema: a["output_schema"] ?? undefined,
39
+ requires,
40
+ supportsTools: a["supports_tools"] === true,
41
+ labels,
42
+ });
43
+ }
44
+ }
45
+ exports.AgentDescription = AgentDescription;
46
+ class ToolDescription {
47
+ constructor({ title, name, description, inputSchema, thumbnailUrl, defs }) {
48
+ this.title = title;
49
+ this.name = name;
50
+ this.description = description;
51
+ this.inputSchema = inputSchema;
52
+ this.thumbnailUrl = thumbnailUrl;
53
+ this.defs = defs;
54
+ }
55
+ }
56
+ exports.ToolDescription = ToolDescription;
57
+ class ToolkitDescription {
58
+ constructor({ title, name, description, tools, thumbnailUrl }) {
59
+ this.title = title;
60
+ this.name = name;
61
+ this.description = description;
62
+ this.tools = tools;
63
+ this.thumbnailUrl = thumbnailUrl;
64
+ this._byName = new Map(this.tools.map((tool) => [tool.name, tool]));
65
+ }
66
+ getTool(name) {
67
+ return this._byName.get(name);
68
+ }
69
+ static fromJson(json, opts) {
70
+ const { name } = opts ?? {};
71
+ const title = json["title"] ?? "";
72
+ const finalName = name ?? json["name"] ?? "";
73
+ const description = json["description"] ?? "";
74
+ const thumbnailUrl = json["thumbnail_url"] ?? undefined;
75
+ const toolsList = [];
76
+ if (Array.isArray(json["tools"])) {
77
+ for (const tool of json["tools"]) {
78
+ toolsList.push(new ToolDescription({
79
+ title: tool["title"],
80
+ name: tool["name"],
81
+ description: tool["description"],
82
+ inputSchema: tool["input_schema"],
83
+ thumbnailUrl: tool["thumbnail_url"],
84
+ defs: tool["defs"],
85
+ }));
86
+ }
87
+ }
88
+ else if (typeof json["tools"] === "object" && json["tools"] !== null) {
89
+ const toolsMap = json["tools"];
90
+ for (const toolName of Object.keys(toolsMap)) {
91
+ const tool = toolsMap[toolName];
92
+ toolsList.push(new ToolDescription({
93
+ title: tool["title"],
94
+ name: toolName,
95
+ description: tool["description"],
96
+ inputSchema: tool["input_schema"],
97
+ thumbnailUrl: tool["thumbnail_url"],
98
+ defs: tool["defs"],
99
+ }));
100
+ }
101
+ }
102
+ return new ToolkitDescription({
103
+ title,
104
+ name: finalName,
105
+ description,
106
+ thumbnailUrl,
107
+ tools: toolsList,
108
+ });
109
+ }
110
+ }
111
+ exports.ToolkitDescription = ToolkitDescription;
112
+ class ToolkitConfiguration {
113
+ constructor(name, use) {
114
+ this.name = name;
115
+ this.use = use;
116
+ }
117
+ toJson() {
118
+ if (!this.use) {
119
+ return {
120
+ [this.name]: {},
121
+ };
122
+ }
123
+ else {
124
+ return {
125
+ [this.name]: {
126
+ use: this.use.reduce((acc, tool) => {
127
+ acc[tool] = {};
128
+ return acc;
129
+ }, {}),
130
+ },
131
+ };
132
+ }
133
+ }
134
+ }
135
+ exports.ToolkitConfiguration = ToolkitConfiguration;
136
+ class AgentsClient {
137
+ constructor({ room }) {
138
+ this.client = room;
139
+ }
140
+ async call(params) {
141
+ await this.client.sendRequest("agent.call", params);
142
+ }
143
+ async ask(params) {
144
+ const { agentName, toolkits = [], arguments: args } = params;
145
+ const usedToolkits = {};
146
+ for (const t of toolkits) {
147
+ Object.assign(usedToolkits, t.toJson());
148
+ }
149
+ const result = (await this.client.sendRequest("agent.ask", {
150
+ agent: agentName,
151
+ arguments: args,
152
+ toolkits: usedToolkits,
153
+ }));
154
+ return result.json["answer"];
155
+ }
156
+ async listToolkits() {
157
+ const result = (await this.client.sendRequest("agent.list_toolkits", {}));
158
+ const tools = result.json["tools"];
159
+ const toolkits = [];
160
+ for (const name of Object.keys(tools)) {
161
+ const data = tools[name];
162
+ toolkits.push(ToolkitDescription.fromJson(data, { name }));
163
+ }
164
+ return toolkits;
165
+ }
166
+ async listAgents() {
167
+ const result = (await this.client.sendRequest("agent.list_agents", {}));
168
+ return (result.json["agents"] || []).map(AgentDescription.fromJson);
169
+ }
170
+ async invokeTool(params) {
171
+ return await this.client.sendRequest("agent.invoke_tool", params);
172
+ }
173
+ }
174
+ exports.AgentsClient = AgentsClient;
@@ -0,0 +1,103 @@
1
+ import { RoomClient } from "./room-client";
2
+ import { RequiredToolkit } from "./requirement";
3
+ import { Response } from "./response";
4
+ export declare class AgentChatContext {
5
+ messages: Array<Record<string, any>>;
6
+ readonly systemRole: string;
7
+ constructor({ messages, systemRole }: {
8
+ messages?: Array<Record<string, any>>;
9
+ systemRole?: string;
10
+ });
11
+ appendRules(rules: string[]): void;
12
+ appendUserMessage(message: string): void;
13
+ appendUserImage(url: string): void;
14
+ copy(): AgentChatContext;
15
+ toJson(): Record<string, any>;
16
+ static fromJson(json: Record<string, any>): AgentChatContext;
17
+ }
18
+ export declare class AgentCallContext {
19
+ private readonly _jwt;
20
+ private readonly _chat;
21
+ private readonly _apiUrl;
22
+ constructor({ chat, jwt, api_url }: {
23
+ chat: AgentChatContext;
24
+ jwt: string;
25
+ api_url: string;
26
+ });
27
+ get chat(): AgentChatContext;
28
+ get jwt(): string;
29
+ get api_url(): string;
30
+ }
31
+ export declare abstract class Tool {
32
+ readonly name: string;
33
+ readonly description: string;
34
+ readonly title: string;
35
+ readonly inputSchema: Record<string, any>;
36
+ readonly thumbnailUrl?: string;
37
+ constructor({ name, description, title, inputSchema, thumbnailUrl }: {
38
+ name: string;
39
+ description: string;
40
+ title: string;
41
+ inputSchema: Record<string, any>;
42
+ thumbnailUrl?: string;
43
+ });
44
+ abstract execute(arguments_: Record<string, any>): Promise<Response>;
45
+ }
46
+ export declare abstract class Toolkit {
47
+ readonly tools: Tool[];
48
+ readonly rules: string[];
49
+ constructor({ tools, rules }: {
50
+ tools: Tool[];
51
+ rules?: string[];
52
+ });
53
+ getTool(name: string): Tool;
54
+ getTools(): Record<string, any>;
55
+ execute(name: string, args: Record<string, any>): Promise<Response>;
56
+ }
57
+ export declare abstract class RemoteToolkit extends Toolkit {
58
+ protected readonly client: RoomClient;
59
+ protected readonly name: string;
60
+ protected readonly title: string;
61
+ protected readonly description: string;
62
+ private _registrationId?;
63
+ constructor({ name, title, description, room, tools, rules }: {
64
+ name: string;
65
+ title: string;
66
+ description: string;
67
+ room: RoomClient;
68
+ tools: Tool[];
69
+ rules?: string[];
70
+ });
71
+ start({ public_: isPublic }?: {
72
+ public_?: boolean;
73
+ }): Promise<void>;
74
+ stop(): Promise<void>;
75
+ private _register;
76
+ private _unregister;
77
+ private _toolCall;
78
+ }
79
+ export declare abstract class RemoteTaskRunner {
80
+ protected readonly client: RoomClient;
81
+ protected readonly name: string;
82
+ protected readonly description: string;
83
+ protected readonly inputSchema?: Record<string, any>;
84
+ protected readonly outputSchema?: Record<string, any>;
85
+ protected readonly supportsTools: boolean;
86
+ protected readonly required: RequiredToolkit[];
87
+ private _registrationId?;
88
+ constructor({ name, description, client, inputSchema, outputSchema, supportsTools, required, }: {
89
+ name: string;
90
+ description: string;
91
+ client: RoomClient;
92
+ inputSchema?: Record<string, any>;
93
+ outputSchema?: Record<string, any>;
94
+ supportsTools?: boolean;
95
+ required?: RequiredToolkit[];
96
+ });
97
+ start(): Promise<void>;
98
+ stop(): Promise<void>;
99
+ protected _register(): Promise<void>;
100
+ protected _unregister(): Promise<void>;
101
+ abstract ask(context: AgentCallContext, arguments_: Record<string, any>): Promise<Record<string, any>>;
102
+ private _ask;
103
+ }
@@ -0,0 +1,229 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RemoteTaskRunner = exports.RemoteToolkit = exports.Toolkit = exports.Tool = exports.AgentCallContext = exports.AgentChatContext = void 0;
4
+ const response_1 = require("./response");
5
+ const utils_1 = require("./utils");
6
+ class AgentChatContext {
7
+ constructor({ messages, systemRole = "system" }) {
8
+ this.messages = messages ? [...messages] : [];
9
+ this.systemRole = systemRole;
10
+ }
11
+ appendRules(rules) {
12
+ let systemMessage = this.messages.find((m) => m["role"] === this.systemRole);
13
+ if (!systemMessage) {
14
+ systemMessage = { role: this.systemRole, content: "" };
15
+ this.messages.push(systemMessage);
16
+ }
17
+ const plan = `
18
+ Rules:
19
+ -${rules.join("\n-")}
20
+ `;
21
+ systemMessage["content"] += plan;
22
+ }
23
+ appendUserMessage(message) {
24
+ this.messages.push({ role: "user", content: message });
25
+ }
26
+ appendUserImage(url) {
27
+ this.messages.push({
28
+ role: "user",
29
+ content: [
30
+ {
31
+ type: "image_url",
32
+ image_url: { url: url, detail: "auto" },
33
+ },
34
+ ],
35
+ });
36
+ }
37
+ copy() {
38
+ const cloned = JSON.parse(JSON.stringify(this.messages));
39
+ return new AgentChatContext({
40
+ messages: cloned,
41
+ systemRole: this.systemRole,
42
+ });
43
+ }
44
+ toJson() {
45
+ return {
46
+ messages: this.messages,
47
+ system_role: this.systemRole,
48
+ };
49
+ }
50
+ static fromJson(json) {
51
+ return new AgentChatContext({
52
+ messages: json["messages"],
53
+ systemRole: json["system_role"] || "system",
54
+ });
55
+ }
56
+ }
57
+ exports.AgentChatContext = AgentChatContext;
58
+ class AgentCallContext {
59
+ constructor({ chat, jwt, api_url }) {
60
+ this._jwt = jwt;
61
+ this._chat = chat;
62
+ this._apiUrl = api_url;
63
+ }
64
+ get chat() {
65
+ return this._chat;
66
+ }
67
+ get jwt() {
68
+ return this._jwt;
69
+ }
70
+ get api_url() {
71
+ return this._apiUrl;
72
+ }
73
+ }
74
+ exports.AgentCallContext = AgentCallContext;
75
+ class Tool {
76
+ constructor({ name, description, title, inputSchema, thumbnailUrl }) {
77
+ this.name = name;
78
+ this.description = description;
79
+ this.title = title;
80
+ this.inputSchema = inputSchema;
81
+ this.thumbnailUrl = thumbnailUrl;
82
+ }
83
+ }
84
+ exports.Tool = Tool;
85
+ class Toolkit {
86
+ constructor({ tools, rules = [] }) {
87
+ this.tools = tools;
88
+ this.rules = rules;
89
+ }
90
+ getTool(name) {
91
+ const tool = this.tools.find((t) => t.name === name);
92
+ if (!tool) {
93
+ throw new Error(`Tool was not found ${name}`);
94
+ }
95
+ return tool;
96
+ }
97
+ getTools() {
98
+ const json = {};
99
+ for (const tool of this.tools) {
100
+ json[tool.name] = {
101
+ description: tool.description,
102
+ title: tool.title,
103
+ input_schema: tool.inputSchema,
104
+ thumbnail_url: tool.thumbnailUrl,
105
+ };
106
+ }
107
+ return json;
108
+ }
109
+ async execute(name, args) {
110
+ return this.getTool(name).execute(args);
111
+ }
112
+ }
113
+ exports.Toolkit = Toolkit;
114
+ class RemoteToolkit extends Toolkit {
115
+ constructor({ name, title, description, room, tools, rules = [] }) {
116
+ super({ tools, rules });
117
+ this.client = room;
118
+ this.name = name;
119
+ this.title = title;
120
+ this.description = description;
121
+ }
122
+ async start({ public_: isPublic = false } = {}) {
123
+ const handler = this._toolCall.bind(this);
124
+ this.client.protocol.addHandler(`agent.tool_call.${this.name}`, handler);
125
+ await this._register(isPublic);
126
+ }
127
+ async stop() {
128
+ await this._unregister();
129
+ this.client.protocol.removeHandler(`agent.tool_call.${this.name}`);
130
+ }
131
+ async _register(public_) {
132
+ const response = await this.client.sendRequest("agent.register_toolkit", {
133
+ name: this.name,
134
+ title: this.title,
135
+ description: this.description,
136
+ tools: this.getTools(),
137
+ public: public_,
138
+ });
139
+ const json = response.json;
140
+ this._registrationId = json["id"];
141
+ }
142
+ async _unregister() {
143
+ if (!this._registrationId)
144
+ return;
145
+ await this.client.sendRequest("agent.unregister_toolkit", {
146
+ id: this._registrationId,
147
+ });
148
+ }
149
+ async _toolCall(protocol, messageId, type, data) {
150
+ try {
151
+ const raw = utils_1.decoder.decode(data);
152
+ const message = JSON.parse(raw);
153
+ const toolName = message["name"];
154
+ const args = message["arguments"];
155
+ const response = await this.execute(toolName, args);
156
+ await this.client.protocol.send("agent.tool_call_response", response.pack(), messageId);
157
+ }
158
+ catch (e) {
159
+ const err = new response_1.ErrorResponse({ text: String(e) });
160
+ await this.client.protocol.send("agent.tool_call_response", err.pack(), messageId);
161
+ }
162
+ }
163
+ }
164
+ exports.RemoteToolkit = RemoteToolkit;
165
+ class RemoteTaskRunner {
166
+ constructor({ name, description, client, inputSchema, outputSchema, supportsTools = false, required = [], }) {
167
+ this.client = client;
168
+ this.name = name;
169
+ this.description = description;
170
+ this.inputSchema = inputSchema;
171
+ this.outputSchema = outputSchema;
172
+ this.supportsTools = supportsTools;
173
+ this.required = required;
174
+ }
175
+ async start() {
176
+ const handler = this._ask.bind(this);
177
+ this.client.protocol.addHandler("agent.ask", handler);
178
+ await this._register();
179
+ }
180
+ async stop() {
181
+ await this._unregister();
182
+ this.client.protocol.removeHandler("agent.ask");
183
+ }
184
+ async _register() {
185
+ const res = await this.client.sendRequest("agent.register_agent", {
186
+ name: this.name,
187
+ description: this.description,
188
+ input_schema: this.inputSchema,
189
+ output_schema: this.outputSchema,
190
+ supports_tools: this.supportsTools,
191
+ requires: this.required.map((r) => ({
192
+ toolkit: r.name,
193
+ tools: r.tools,
194
+ })),
195
+ });
196
+ this._registrationId = res.json["id"];
197
+ }
198
+ async _unregister() {
199
+ if (!this._registrationId)
200
+ return;
201
+ await this.client.sendRequest("agent.unregister_agent", { id: this._registrationId });
202
+ }
203
+ async _ask(protocol, messageId, msgType, data) {
204
+ console.info("_ask handler invoked with data", data);
205
+ try {
206
+ const raw = utils_1.decoder.decode(data);
207
+ const message = JSON.parse(raw);
208
+ console.info("got message", message);
209
+ const jwt = message["jwt"];
210
+ const args = message["arguments"];
211
+ const task_id = message["task_id"];
212
+ const context_json = message["context"];
213
+ const api_url = message["api_url"];
214
+ const chat = AgentChatContext.fromJson(context_json);
215
+ const callContext = new AgentCallContext({ chat, jwt, api_url });
216
+ const answer = await this.ask(callContext, args);
217
+ const encoded = utils_1.encoder.encode(JSON.stringify({ task_id, answer }));
218
+ await protocol.send("agent.ask_response", encoded);
219
+ }
220
+ catch (e) {
221
+ const rawError = JSON.stringify({
222
+ task_id: "",
223
+ error: String(e),
224
+ });
225
+ await protocol.send("agent.ask_response", utils_1.encoder.encode(rawError));
226
+ }
227
+ }
228
+ }
229
+ exports.RemoteTaskRunner = RemoteTaskRunner;