@meshagent/meshagent 0.3.1 → 0.4.1

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 (84) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/browser/entrypoint.js +7956 -5611
  3. package/dist/browser/helpers.d.ts +10 -4
  4. package/dist/browser/helpers.js +14 -39
  5. package/dist/browser/index.d.ts +1 -0
  6. package/dist/browser/index.js +1 -0
  7. package/dist/browser/participant-token.d.ts +4 -4
  8. package/dist/browser/participant-token.js +4 -9
  9. package/dist/browser/protocol.d.ts +5 -1
  10. package/dist/browser/protocol.js +7 -2
  11. package/dist/browser/room-client.d.ts +4 -1
  12. package/dist/browser/room-client.js +2 -2
  13. package/dist/browser/sync-client.d.ts +5 -1
  14. package/dist/browser/sync-client.js +13 -2
  15. package/dist/esm/agent-client.d.ts +88 -0
  16. package/dist/esm/agent-client.js +166 -0
  17. package/dist/esm/agent.d.ts +103 -0
  18. package/dist/esm/agent.js +218 -0
  19. package/dist/esm/client.d.ts +90 -0
  20. package/dist/esm/client.js +443 -0
  21. package/dist/esm/completer.d.ts +9 -0
  22. package/dist/esm/completer.js +21 -0
  23. package/dist/esm/data-types.d.ts +44 -0
  24. package/dist/esm/data-types.js +110 -0
  25. package/dist/esm/database-client.d.ts +77 -0
  26. package/dist/esm/database-client.js +109 -0
  27. package/dist/esm/developer-client.d.ts +13 -0
  28. package/dist/esm/developer-client.js +31 -0
  29. package/dist/esm/document.d.ts +84 -0
  30. package/dist/esm/document.js +522 -0
  31. package/dist/esm/entrypoint.d.ts +49722 -0
  32. package/dist/esm/entrypoint.js +6313 -0
  33. package/dist/esm/event-emitter.d.ts +13 -0
  34. package/dist/esm/event-emitter.js +34 -0
  35. package/dist/esm/helpers.d.ts +32 -0
  36. package/dist/esm/helpers.js +46 -0
  37. package/dist/esm/index.d.ts +25 -0
  38. package/dist/esm/index.js +25 -0
  39. package/dist/esm/messaging-client.d.ts +76 -0
  40. package/dist/esm/messaging-client.js +241 -0
  41. package/dist/esm/participant-token.d.ts +36 -0
  42. package/dist/esm/participant-token.js +91 -0
  43. package/dist/esm/participant.d.ts +18 -0
  44. package/dist/esm/participant.js +36 -0
  45. package/dist/esm/protocol.d.ts +91 -0
  46. package/dist/esm/protocol.js +287 -0
  47. package/dist/esm/queues-client.d.ts +26 -0
  48. package/dist/esm/queues-client.js +42 -0
  49. package/dist/esm/requirement.d.ts +25 -0
  50. package/dist/esm/requirement.js +42 -0
  51. package/dist/esm/response.d.ts +60 -0
  52. package/dist/esm/response.js +128 -0
  53. package/dist/esm/room-client.d.ts +46 -0
  54. package/dist/esm/room-client.js +106 -0
  55. package/dist/esm/room-event.d.ts +60 -0
  56. package/dist/esm/room-event.js +72 -0
  57. package/dist/esm/room-server-client.d.ts +19 -0
  58. package/dist/esm/room-server-client.js +45 -0
  59. package/dist/esm/runtime.d.ts +6 -0
  60. package/dist/esm/runtime.js +1 -0
  61. package/dist/esm/schema.d.ts +83 -0
  62. package/dist/esm/schema.js +312 -0
  63. package/dist/esm/storage-client.d.ts +38 -0
  64. package/dist/esm/storage-client.js +79 -0
  65. package/dist/esm/stream-controller.d.ts +8 -0
  66. package/dist/esm/stream-controller.js +51 -0
  67. package/dist/esm/sync-client.d.ts +37 -0
  68. package/dist/esm/sync-client.js +125 -0
  69. package/dist/esm/utils.d.ts +14 -0
  70. package/dist/esm/utils.js +44 -0
  71. package/dist/node/entrypoint.js +9 -4
  72. package/dist/node/helpers.d.ts +10 -4
  73. package/dist/node/helpers.js +14 -39
  74. package/dist/node/index.d.ts +1 -0
  75. package/dist/node/index.js +1 -0
  76. package/dist/node/participant-token.d.ts +4 -4
  77. package/dist/node/participant-token.js +4 -9
  78. package/dist/node/protocol.d.ts +5 -1
  79. package/dist/node/protocol.js +7 -2
  80. package/dist/node/room-client.d.ts +4 -1
  81. package/dist/node/room-client.js +2 -2
  82. package/dist/node/sync-client.d.ts +5 -1
  83. package/dist/node/sync-client.js +13 -2
  84. package/package.json +4 -3
@@ -0,0 +1,218 @@
1
+ import { ErrorResponse } from "./response";
2
+ import { packMessage, unpackMessage } from "./utils";
3
+ export class AgentChatContext {
4
+ constructor({ messages, systemRole = "system" }) {
5
+ this.messages = messages ? [...messages] : [];
6
+ this.systemRole = systemRole;
7
+ }
8
+ appendRules(rules) {
9
+ let systemMessage = this.messages.find((m) => m["role"] === this.systemRole);
10
+ if (!systemMessage) {
11
+ systemMessage = { role: this.systemRole, content: "" };
12
+ this.messages.push(systemMessage);
13
+ }
14
+ const plan = `
15
+ Rules:
16
+ -${rules.join("\n-")}
17
+ `;
18
+ systemMessage["content"] += plan;
19
+ }
20
+ appendUserMessage(message) {
21
+ this.messages.push({ role: "user", content: message });
22
+ }
23
+ appendUserImage(url) {
24
+ this.messages.push({
25
+ role: "user",
26
+ content: [
27
+ {
28
+ type: "image_url",
29
+ image_url: { url: url, detail: "auto" },
30
+ },
31
+ ],
32
+ });
33
+ }
34
+ copy() {
35
+ const cloned = JSON.parse(JSON.stringify(this.messages));
36
+ return new AgentChatContext({
37
+ messages: cloned,
38
+ systemRole: this.systemRole,
39
+ });
40
+ }
41
+ toJson() {
42
+ return {
43
+ messages: this.messages,
44
+ system_role: this.systemRole,
45
+ };
46
+ }
47
+ static fromJson(json) {
48
+ return new AgentChatContext({
49
+ messages: json["messages"],
50
+ systemRole: json["system_role"] || "system",
51
+ });
52
+ }
53
+ }
54
+ export class AgentCallContext {
55
+ constructor({ chat, jwt, api_url }) {
56
+ this._jwt = jwt;
57
+ this._chat = chat;
58
+ this._apiUrl = api_url;
59
+ }
60
+ get chat() {
61
+ return this._chat;
62
+ }
63
+ get jwt() {
64
+ return this._jwt;
65
+ }
66
+ get api_url() {
67
+ return this._apiUrl;
68
+ }
69
+ }
70
+ export class Tool {
71
+ constructor({ name, description, title, inputSchema, thumbnailUrl }) {
72
+ this.name = name;
73
+ this.description = description;
74
+ this.title = title;
75
+ this.inputSchema = inputSchema;
76
+ this.thumbnailUrl = thumbnailUrl;
77
+ }
78
+ }
79
+ export class Toolkit {
80
+ constructor({ tools, rules = [] }) {
81
+ this.tools = tools;
82
+ this.rules = rules;
83
+ }
84
+ getTool(name) {
85
+ const tool = this.tools.find((t) => t.name === name);
86
+ if (!tool) {
87
+ throw new Error(`Tool was not found ${name}`);
88
+ }
89
+ return tool;
90
+ }
91
+ getTools() {
92
+ const json = {};
93
+ for (const tool of this.tools) {
94
+ json[tool.name] = {
95
+ description: tool.description,
96
+ title: tool.title,
97
+ input_schema: tool.inputSchema,
98
+ thumbnail_url: tool.thumbnailUrl,
99
+ };
100
+ }
101
+ return json;
102
+ }
103
+ async execute(name, args) {
104
+ return this.getTool(name).execute(args);
105
+ }
106
+ }
107
+ export class RemoteToolkit extends Toolkit {
108
+ constructor({ name, title, description, room, tools, rules = [] }) {
109
+ super({ tools, rules });
110
+ this.client = room;
111
+ this.name = name;
112
+ this.title = title;
113
+ this.description = description;
114
+ }
115
+ async start({ public_: isPublic = false } = {}) {
116
+ const handler = this._toolCall.bind(this);
117
+ this.client.protocol.addHandler(`agent.tool_call.${this.name}`, handler);
118
+ await this._register(isPublic);
119
+ }
120
+ async stop() {
121
+ await this._unregister();
122
+ this.client.protocol.removeHandler(`agent.tool_call.${this.name}`);
123
+ }
124
+ async _register(public_) {
125
+ const response = await this.client.sendRequest("agent.register_toolkit", {
126
+ name: this.name,
127
+ title: this.title,
128
+ description: this.description,
129
+ tools: this.getTools(),
130
+ public: public_,
131
+ });
132
+ const json = response.json;
133
+ this._registrationId = json["id"];
134
+ }
135
+ async _unregister() {
136
+ if (!this._registrationId)
137
+ return;
138
+ await this.client.sendRequest("agent.unregister_toolkit", {
139
+ id: this._registrationId,
140
+ });
141
+ }
142
+ async _toolCall(protocol, messageId, type, data) {
143
+ try {
144
+ const [message, _] = unpackMessage(data);
145
+ const toolName = message["name"];
146
+ const args = message["arguments"];
147
+ const response = await this.execute(toolName, args);
148
+ await this.client.protocol.send("agent.tool_call_response", response.pack(), messageId);
149
+ }
150
+ catch (e) {
151
+ const err = new ErrorResponse({ text: String(e) });
152
+ await this.client.protocol.send("agent.tool_call_response", err.pack(), messageId);
153
+ }
154
+ }
155
+ }
156
+ export class RemoteTaskRunner {
157
+ constructor({ name, description, client, inputSchema, outputSchema, supportsTools = false, required = [], }) {
158
+ this.client = client;
159
+ this.name = name;
160
+ this.description = description;
161
+ this.inputSchema = inputSchema;
162
+ this.outputSchema = outputSchema;
163
+ this.supportsTools = supportsTools;
164
+ this.required = required;
165
+ }
166
+ async start() {
167
+ const handler = this._ask.bind(this);
168
+ this.client.protocol.addHandler("agent.ask", handler);
169
+ await this._register();
170
+ }
171
+ async stop() {
172
+ await this._unregister();
173
+ this.client.protocol.removeHandler("agent.ask");
174
+ }
175
+ async _register() {
176
+ const res = await this.client.sendRequest("agent.register_agent", {
177
+ name: this.name,
178
+ description: this.description,
179
+ input_schema: this.inputSchema,
180
+ output_schema: this.outputSchema,
181
+ supports_tools: this.supportsTools,
182
+ requires: this.required.map((r) => ({
183
+ toolkit: r.name,
184
+ tools: r.tools,
185
+ })),
186
+ });
187
+ this._registrationId = res.json["id"];
188
+ }
189
+ async _unregister() {
190
+ if (!this._registrationId)
191
+ return;
192
+ await this.client.sendRequest("agent.unregister_agent", { id: this._registrationId });
193
+ }
194
+ async _ask(protocol, messageId, msgType, data) {
195
+ console.info("_ask handler invoked with data", data);
196
+ try {
197
+ const [message, _] = unpackMessage(data);
198
+ console.info("got message", message);
199
+ const jwt = message["jwt"];
200
+ const args = message["arguments"];
201
+ const task_id = message["task_id"];
202
+ const context_json = message["context"];
203
+ const api_url = message["api_url"];
204
+ const chat = AgentChatContext.fromJson(context_json);
205
+ const callContext = new AgentCallContext({ chat, jwt, api_url });
206
+ const answer = await this.ask(callContext, args);
207
+ const encoded = packMessage({ task_id, answer });
208
+ await protocol.send("agent.ask_response", encoded);
209
+ }
210
+ catch (e) {
211
+ const rawError = {
212
+ task_id: "",
213
+ error: String(e),
214
+ };
215
+ await protocol.send("agent.ask_response", packMessage(rawError));
216
+ }
217
+ }
218
+ }
@@ -0,0 +1,90 @@
1
+ interface ElementData {
2
+ tagName: string;
3
+ attributes: Record<string, any>;
4
+ children: Array<XmlElement | XmlText>;
5
+ }
6
+ interface TextData {
7
+ delta: any;
8
+ }
9
+ export declare class XmlElement {
10
+ private _data;
11
+ private _parent;
12
+ private _doc;
13
+ constructor(parent: XmlElement | null, { tagName, attributes }: {
14
+ tagName: string;
15
+ attributes: Record<string, any>;
16
+ }, doc: ClientXmlDocument);
17
+ getNodeByID(id: string): XmlElement | XmlText | null;
18
+ get id(): string | undefined;
19
+ get doc(): ClientXmlDocument;
20
+ get data(): ElementData;
21
+ get tagName(): string;
22
+ get parent(): XmlElement | null;
23
+ getAttribute(name: string): any;
24
+ setAttribute(name: string, value: any): void;
25
+ removeAttribute(name: string): void;
26
+ createChildElement(tagName: string, attributes: Record<string, any>): XmlElement | XmlText | null;
27
+ createChildElementAt(index: number, tagName: string, attributes: Record<string, any>): XmlElement | XmlText | null;
28
+ createChildElementAfter(element: XmlElement | XmlText, tagName: string, attributes: Record<string, any>): XmlElement | XmlText | null;
29
+ protected _defaultChildren(tagName: string): any[];
30
+ delete(): void;
31
+ getChildren(): Array<XmlElement | XmlText>;
32
+ }
33
+ export declare class XmlText {
34
+ private _data;
35
+ parent: XmlElement;
36
+ doc: ClientXmlDocument;
37
+ constructor(parent: XmlElement, data: TextData, doc: ClientXmlDocument);
38
+ get delta(): any;
39
+ insert(index: number, text: string, attributes?: Record<string, any>): void;
40
+ format(from: number, length: number, attributes: Record<string, any>): void;
41
+ delete(index: number, length: number): void;
42
+ get id(): string | undefined;
43
+ }
44
+ type SendChangesFn = (message: any) => void;
45
+ interface InsertElementData {
46
+ element?: {
47
+ name: string;
48
+ attributes: Record<string, any>;
49
+ children: any[];
50
+ };
51
+ text?: {
52
+ delta: any;
53
+ };
54
+ }
55
+ interface ReceiveChangesMessage {
56
+ target: string;
57
+ root: boolean;
58
+ elements: Array<{
59
+ retain?: number;
60
+ insert?: InsertElementData[];
61
+ delete?: number;
62
+ }>;
63
+ text: Array<{
64
+ insert?: string;
65
+ delete?: number;
66
+ attributes?: Record<string, any>;
67
+ retain?: number;
68
+ }>;
69
+ attributes: {
70
+ set: Array<{
71
+ name: string;
72
+ value: any;
73
+ }>;
74
+ delete: string[];
75
+ };
76
+ }
77
+ export declare class ClientXmlDocument {
78
+ private _root;
79
+ private _id;
80
+ sendChanges: SendChangesFn;
81
+ constructor({ id, sendChanges, }: {
82
+ id: string;
83
+ sendChanges: SendChangesFn;
84
+ });
85
+ get id(): string;
86
+ get root(): XmlElement;
87
+ _createNode(parent: XmlElement, data: InsertElementData): XmlElement | XmlText;
88
+ receiveChanges(message: ReceiveChangesMessage): void;
89
+ }
90
+ export {};