@distri/core 0.1.8 → 0.2.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.
package/dist/index.js CHANGED
@@ -17,23 +17,25 @@ var __copyProps = (to, from, except, desc) => {
17
17
  return to;
18
18
  };
19
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
- var __publicField = (obj, key, value) => {
21
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
22
- return value;
23
- };
20
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
24
21
 
25
22
  // src/index.ts
26
- var src_exports = {};
27
- __export(src_exports, {
28
- A2AClient: () => A2AClient,
29
- A2AProtocolError: () => A2AProtocolError,
30
- ApiError: () => ApiError,
31
- ConnectionError: () => ConnectionError,
23
+ var index_exports = {};
24
+ __export(index_exports, {
25
+ Agent: () => Agent,
32
26
  DistriClient: () => DistriClient,
33
- DistriError: () => DistriError,
27
+ convertA2AMessageToDistri: () => convertA2AMessageToDistri,
28
+ convertA2APartToDistri: () => convertA2APartToDistri,
29
+ convertDistriMessageToA2A: () => convertDistriMessageToA2A,
30
+ convertDistriPartToA2A: () => convertDistriPartToA2A,
31
+ extractTextFromDistriMessage: () => extractTextFromDistriMessage,
32
+ extractToolCallsFromDistriMessage: () => extractToolCallsFromDistriMessage,
33
+ extractToolResultsFromDistriMessage: () => extractToolResultsFromDistriMessage,
34
+ isDistriEvent: () => isDistriEvent,
35
+ isDistriMessage: () => isDistriMessage,
34
36
  uuidv4: () => uuidv4
35
37
  });
36
- module.exports = __toCommonJS(src_exports);
38
+ module.exports = __toCommonJS(index_exports);
37
39
 
38
40
  // ../../node_modules/.pnpm/@a2a-js+sdk@https+++codeload.github.com+v3g42+a2a-js+tar.gz+51444c9/node_modules/@a2a-js/sdk/dist/chunk-CUGIRVQB.js
39
41
  var A2AClient = class {
@@ -152,8 +154,7 @@ var A2AClient = class {
152
154
  throw new Error(`HTTP error for ${method}! Status: ${httpResponse.status} ${httpResponse.statusText}. Response: ${errorBodyText}`);
153
155
  }
154
156
  } catch (e) {
155
- if (e.message.startsWith("RPC error for") || e.message.startsWith("HTTP error for"))
156
- throw e;
157
+ if (e.message.startsWith("RPC error for") || e.message.startsWith("HTTP error for")) throw e;
157
158
  throw new Error(`HTTP error for ${method}! Status: ${httpResponse.status} ${httpResponse.statusText}. Response: ${errorBodyText}`);
158
159
  }
159
160
  }
@@ -215,8 +216,7 @@ var A2AClient = class {
215
216
  throw new Error(`HTTP error establishing stream for message/stream: ${response.status} ${response.statusText}. RPC Error: ${errorJson.error.message} (Code: ${errorJson.error.code})`);
216
217
  }
217
218
  } catch (e) {
218
- if (e.message.startsWith("HTTP error establishing stream"))
219
- throw e;
219
+ if (e.message.startsWith("HTTP error establishing stream")) throw e;
220
220
  throw new Error(`HTTP error establishing stream for message/stream: ${response.status} ${response.statusText}. Response: ${errorBody || "(empty)"}`);
221
221
  }
222
222
  throw new Error(`HTTP error establishing stream for message/stream: ${response.status} ${response.statusText}`);
@@ -307,8 +307,7 @@ var A2AClient = class {
307
307
  throw new Error(`HTTP error establishing stream for tasks/resubscribe: ${response.status} ${response.statusText}. RPC Error: ${errorJson.error.message} (Code: ${errorJson.error.code})`);
308
308
  }
309
309
  } catch (e) {
310
- if (e.message.startsWith("HTTP error establishing stream"))
311
- throw e;
310
+ if (e.message.startsWith("HTTP error establishing stream")) throw e;
312
311
  throw new Error(`HTTP error establishing stream for tasks/resubscribe: ${response.status} ${response.statusText}. Response: ${errorBody || "(empty)"}`);
313
312
  }
314
313
  throw new Error(`HTTP error establishing stream for tasks/resubscribe: ${response.status} ${response.statusText}`);
@@ -430,12 +429,113 @@ var ApiError = class extends DistriError {
430
429
  this.name = "ApiError";
431
430
  }
432
431
  };
433
- var ConnectionError = class extends DistriError {
434
- constructor(message, details) {
435
- super(message, "CONNECTION_ERROR", details);
436
- this.name = "ConnectionError";
432
+ function isDistriMessage(event) {
433
+ return "id" in event && "role" in event && "parts" in event;
434
+ }
435
+ function isDistriEvent(event) {
436
+ return "type" in event && "metadata" in event;
437
+ }
438
+
439
+ // src/encoder.ts
440
+ function convertA2AMessageToDistri(a2aMessage) {
441
+ const role = a2aMessage.role === "agent" ? "assistant" : "user";
442
+ return {
443
+ id: a2aMessage.messageId,
444
+ role,
445
+ parts: a2aMessage.parts.map(convertA2APartToDistri),
446
+ created_at: a2aMessage.createdAt
447
+ };
448
+ }
449
+ function convertA2APartToDistri(a2aPart) {
450
+ switch (a2aPart.kind) {
451
+ case "text":
452
+ return { type: "text", text: a2aPart.text };
453
+ case "file":
454
+ if ("uri" in a2aPart.file) {
455
+ return { type: "image_url", image: { mime_type: a2aPart.file.mimeType, url: a2aPart.file.uri } };
456
+ } else {
457
+ return { type: "image_bytes", image: { mime_type: a2aPart.file.mimeType, data: a2aPart.file.bytes } };
458
+ }
459
+ case "data":
460
+ switch (a2aPart.data.part_type) {
461
+ case "tool_call":
462
+ return { type: "tool_call", tool_call: a2aPart.data };
463
+ case "tool_result":
464
+ return { type: "tool_result", tool_result: a2aPart.data };
465
+ case "code_observation":
466
+ return { type: "code_observation", thought: a2aPart.data.thought, code: a2aPart.data.code };
467
+ case "plan":
468
+ return { type: "plan", plan: a2aPart.data.plan };
469
+ default:
470
+ return { type: "data", data: a2aPart.data };
471
+ }
472
+ default:
473
+ return { type: "text", text: JSON.stringify(a2aPart) };
437
474
  }
438
- };
475
+ }
476
+ function convertDistriMessageToA2A(distriMessage, context) {
477
+ let role;
478
+ switch (distriMessage.role) {
479
+ case "assistant":
480
+ role = "agent";
481
+ break;
482
+ case "user":
483
+ role = "user";
484
+ break;
485
+ case "system":
486
+ case "tool":
487
+ role = "user";
488
+ break;
489
+ default:
490
+ role = "user";
491
+ }
492
+ return {
493
+ messageId: distriMessage.id,
494
+ role,
495
+ parts: distriMessage.parts.map(convertDistriPartToA2A),
496
+ kind: "message",
497
+ contextId: context.thread_id,
498
+ taskId: context.run_id
499
+ };
500
+ }
501
+ function convertDistriPartToA2A(distriPart) {
502
+ switch (distriPart.type) {
503
+ case "text":
504
+ return { kind: "text", text: distriPart.text };
505
+ case "image_url":
506
+ return { kind: "file", file: { mimeType: distriPart.image.mime_type, uri: distriPart.image.url } };
507
+ case "image_bytes":
508
+ return { kind: "file", file: { mimeType: distriPart.image.mime_type, bytes: distriPart.image.data } };
509
+ case "tool_call":
510
+ return { kind: "data", data: { part_type: "tool_call", tool_call: distriPart.tool_call } };
511
+ case "tool_result":
512
+ let val = {
513
+ kind: "data",
514
+ data: {
515
+ tool_call_id: distriPart.tool_result.tool_call_id,
516
+ result: distriPart.tool_result.result,
517
+ part_type: "tool_result"
518
+ }
519
+ };
520
+ console.log("<> val", val);
521
+ return val;
522
+ case "code_observation":
523
+ return { kind: "data", data: { ...distriPart, part_type: "code_observation" } };
524
+ case "plan":
525
+ return { kind: "data", data: { ...distriPart, part_type: "plan" } };
526
+ case "data":
527
+ return { kind: "data", ...distriPart.data };
528
+ }
529
+ }
530
+ function extractTextFromDistriMessage(message) {
531
+ return message.parts.filter((part) => part.type === "text").map((part) => part.text).join("\n");
532
+ }
533
+ function extractToolCallsFromDistriMessage(message) {
534
+ return message.parts.filter((part) => part.type === "tool_call").map((part) => part.tool_call);
535
+ }
536
+ function extractToolResultsFromDistriMessage(message) {
537
+ return message.parts.filter((part) => part.type === "tool_result").map((part) => part.tool_result);
538
+ }
439
539
 
440
540
  // src/distri-client.ts
441
541
  var DistriClient = class {
@@ -474,8 +574,7 @@ var DistriClient = class {
474
574
  });
475
575
  return agents;
476
576
  } catch (error) {
477
- if (error instanceof ApiError)
478
- throw error;
577
+ if (error instanceof ApiError) throw error;
479
578
  throw new DistriError("Failed to fetch agents", "FETCH_ERROR", error);
480
579
  }
481
580
  }
@@ -501,8 +600,7 @@ var DistriClient = class {
501
600
  }
502
601
  return agent;
503
602
  } catch (error) {
504
- if (error instanceof ApiError)
505
- throw error;
603
+ if (error instanceof ApiError) throw error;
506
604
  throw new DistriError(`Failed to fetch agent ${agentId}`, "FETCH_ERROR", error);
507
605
  }
508
606
  }
@@ -536,8 +634,7 @@ var DistriClient = class {
536
634
  }
537
635
  throw new DistriError("Invalid response format", "INVALID_RESPONSE");
538
636
  } catch (error) {
539
- if (error instanceof A2AProtocolError || error instanceof DistriError)
540
- throw error;
637
+ if (error instanceof A2AProtocolError || error instanceof DistriError) throw error;
541
638
  throw new DistriError(`Failed to send message to agent ${agentId}`, "SEND_MESSAGE_ERROR", error);
542
639
  }
543
640
  }
@@ -569,8 +666,7 @@ var DistriClient = class {
569
666
  }
570
667
  throw new DistriError("Invalid response format", "INVALID_RESPONSE");
571
668
  } catch (error) {
572
- if (error instanceof A2AProtocolError || error instanceof DistriError)
573
- throw error;
669
+ if (error instanceof A2AProtocolError || error instanceof DistriError) throw error;
574
670
  throw new DistriError(`Failed to get task ${taskId} from agent ${agentId}`, "GET_TASK_ERROR", error);
575
671
  }
576
672
  }
@@ -597,8 +693,7 @@ var DistriClient = class {
597
693
  }
598
694
  return await response.json();
599
695
  } catch (error) {
600
- if (error instanceof ApiError)
601
- throw error;
696
+ if (error instanceof ApiError) throw error;
602
697
  throw new DistriError("Failed to fetch threads", "FETCH_ERROR", error);
603
698
  }
604
699
  }
@@ -610,8 +705,7 @@ var DistriClient = class {
610
705
  }
611
706
  return await response.json();
612
707
  } catch (error) {
613
- if (error instanceof ApiError)
614
- throw error;
708
+ if (error instanceof ApiError) throw error;
615
709
  throw new DistriError(`Failed to fetch thread ${threadId}`, "FETCH_ERROR", error);
616
710
  }
617
711
  }
@@ -629,11 +723,28 @@ var DistriClient = class {
629
723
  }
630
724
  return await response.json();
631
725
  } catch (error) {
632
- if (error instanceof ApiError)
633
- throw error;
726
+ if (error instanceof ApiError) throw error;
634
727
  throw new DistriError(`Failed to fetch messages for thread ${threadId}`, "FETCH_ERROR", error);
635
728
  }
636
729
  }
730
+ /**
731
+ * Get messages from a thread as DistriMessage format
732
+ */
733
+ async getThreadMessagesAsDistri(threadId) {
734
+ const messages = await this.getThreadMessages(threadId);
735
+ return messages.map(convertA2AMessageToDistri);
736
+ }
737
+ /**
738
+ * Send a DistriMessage to a thread
739
+ */
740
+ async sendDistriMessage(threadId, message, context) {
741
+ const a2aMessage = convertDistriMessageToA2A(message, context);
742
+ const params = {
743
+ message: a2aMessage,
744
+ metadata: context.metadata
745
+ };
746
+ await this.sendMessage(threadId, params);
747
+ }
637
748
  /**
638
749
  * Get the base URL for making direct requests
639
750
  */
@@ -694,20 +805,32 @@ var DistriClient = class {
694
805
  /**
695
806
  * Helper method to create A2A messages
696
807
  */
697
- static initMessage(input, role = "user", contextId, messageId, taskId) {
808
+ static initMessage(parts, role = "user", message) {
698
809
  return {
699
- messageId: messageId || uuidv4(),
810
+ messageId: message.messageId || uuidv4(),
811
+ taskId: message.taskId || uuidv4(),
812
+ contextId: message.contextId,
700
813
  role,
701
- parts: [{ kind: "text", text: input.trim() }],
702
- contextId,
703
- taskId: taskId || uuidv4(),
814
+ parts: Array.isArray(parts) ? parts : [{ kind: "text", text: parts.trim() }],
815
+ ...message,
704
816
  kind: "message"
705
817
  };
706
818
  }
819
+ /**
820
+ * Create a DistriMessage instance
821
+ */
822
+ static initDistriMessage(role, parts, id, created_at) {
823
+ return {
824
+ id: id || uuidv4(),
825
+ role,
826
+ parts,
827
+ created_at
828
+ };
829
+ }
707
830
  /**
708
831
  * Helper method to create message send parameters
709
832
  */
710
- static initMessageParams(message, configuration) {
833
+ static initMessageParams(message, configuration, metadata) {
711
834
  return {
712
835
  message,
713
836
  configuration: {
@@ -715,7 +838,18 @@ var DistriClient = class {
715
838
  blocking: false,
716
839
  // Default to non-blocking for streaming
717
840
  ...configuration
718
- }
841
+ },
842
+ metadata
843
+ };
844
+ }
845
+ /**
846
+ * Create MessageSendParams from a DistriMessage using InvokeContext
847
+ */
848
+ static initDistriMessageParams(message, context) {
849
+ const a2aMessage = convertDistriMessageToA2A(message, context);
850
+ return {
851
+ message: a2aMessage,
852
+ metadata: context.metadata
719
853
  };
720
854
  }
721
855
  };
@@ -731,14 +865,139 @@ function uuidv4() {
731
865
  (b, i) => ([4, 6, 8, 10].includes(i) ? "-" : "") + b.toString(16).padStart(2, "0")
732
866
  ).join("");
733
867
  }
868
+
869
+ // src/agent.ts
870
+ var Agent = class _Agent {
871
+ constructor(agentDefinition, client) {
872
+ this.tools = /* @__PURE__ */ new Map();
873
+ this.agentDefinition = agentDefinition;
874
+ this.client = client;
875
+ }
876
+ /**
877
+ * Add a tool to the agent (AG-UI style)
878
+ */
879
+ registerTool(tool) {
880
+ this.tools.set(tool.name, tool);
881
+ }
882
+ /**
883
+ * Add multiple tools at once
884
+ */
885
+ registerTools(tools) {
886
+ tools.forEach((tool) => this.registerTool(tool));
887
+ }
888
+ /**
889
+ * Remove a tool
890
+ */
891
+ unregisterTool(toolName) {
892
+ this.tools.delete(toolName);
893
+ }
894
+ /**
895
+ * Get all registered tools
896
+ */
897
+ getTools() {
898
+ return Array.from(this.tools.values());
899
+ }
900
+ /**
901
+ * Check if a tool is registered
902
+ */
903
+ hasTool(toolName) {
904
+ return this.tools.has(toolName);
905
+ }
906
+ /**
907
+ * Get agent information
908
+ */
909
+ get id() {
910
+ return this.agentDefinition.id;
911
+ }
912
+ get name() {
913
+ return this.agentDefinition.name;
914
+ }
915
+ get description() {
916
+ return this.agentDefinition.description;
917
+ }
918
+ get iconUrl() {
919
+ return this.agentDefinition.icon_url;
920
+ }
921
+ /**
922
+ * Fetch messages for a thread (public method for useChat)
923
+ */
924
+ async getThreadMessages(threadId) {
925
+ return this.client.getThreadMessages(threadId);
926
+ }
927
+ /**
928
+ * Direct (non-streaming) invoke
929
+ */
930
+ async invoke(params) {
931
+ const enhancedParams = this.enhanceParamsWithTools(params);
932
+ console.log("enhancedParams", enhancedParams);
933
+ return await this.client.sendMessage(this.agentDefinition.id, enhancedParams);
934
+ }
935
+ /**
936
+ * Streaming invoke
937
+ */
938
+ async invokeStream(params) {
939
+ const enhancedParams = this.enhanceParamsWithTools(params);
940
+ console.log("enhancedParams", enhancedParams);
941
+ const a2aStream = this.client.sendMessageStream(this.agentDefinition.id, enhancedParams);
942
+ return async function* () {
943
+ for await (const event of a2aStream) {
944
+ if (event.kind === "message") {
945
+ yield convertA2AMessageToDistri(event);
946
+ } else if (event.kind === "status-update") {
947
+ yield event;
948
+ } else if (event.kind === "artifact-update") {
949
+ yield event;
950
+ } else {
951
+ yield event;
952
+ }
953
+ }
954
+ }();
955
+ }
956
+ /**
957
+ * Enhance message params with tool definitions
958
+ */
959
+ enhanceParamsWithTools(params) {
960
+ const tools = this.getTools();
961
+ return {
962
+ ...params,
963
+ metadata: {
964
+ ...params.metadata,
965
+ tools: tools.map((tool) => ({
966
+ name: tool.name,
967
+ description: tool.description,
968
+ input_schema: tool.input_schema
969
+ }))
970
+ }
971
+ };
972
+ }
973
+ /**
974
+ * Create an agent instance from an agent ID
975
+ */
976
+ static async create(agentId, client) {
977
+ const agentDefinition = await client.getAgent(agentId);
978
+ return new _Agent(agentDefinition, client);
979
+ }
980
+ /**
981
+ * List all available agents
982
+ */
983
+ static async list(client) {
984
+ const agentDefinitions = await client.getAgents();
985
+ return agentDefinitions.map((def) => new _Agent(def, client));
986
+ }
987
+ };
734
988
  // Annotate the CommonJS export names for ESM import in node:
735
989
  0 && (module.exports = {
736
- A2AClient,
737
- A2AProtocolError,
738
- ApiError,
739
- ConnectionError,
990
+ Agent,
740
991
  DistriClient,
741
- DistriError,
992
+ convertA2AMessageToDistri,
993
+ convertA2APartToDistri,
994
+ convertDistriMessageToA2A,
995
+ convertDistriPartToA2A,
996
+ extractTextFromDistriMessage,
997
+ extractToolCallsFromDistriMessage,
998
+ extractToolResultsFromDistriMessage,
999
+ isDistriEvent,
1000
+ isDistriMessage,
742
1001
  uuidv4
743
1002
  });
744
1003
  //# sourceMappingURL=index.js.map