@distri/core 0.1.8 → 0.2.2

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.mjs CHANGED
@@ -1,9 +1,6 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => {
4
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- return value;
6
- };
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
4
 
8
5
  // ../../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
9
6
  var A2AClient = class {
@@ -122,8 +119,7 @@ var A2AClient = class {
122
119
  throw new Error(`HTTP error for ${method}! Status: ${httpResponse.status} ${httpResponse.statusText}. Response: ${errorBodyText}`);
123
120
  }
124
121
  } catch (e) {
125
- if (e.message.startsWith("RPC error for") || e.message.startsWith("HTTP error for"))
126
- throw e;
122
+ if (e.message.startsWith("RPC error for") || e.message.startsWith("HTTP error for")) throw e;
127
123
  throw new Error(`HTTP error for ${method}! Status: ${httpResponse.status} ${httpResponse.statusText}. Response: ${errorBodyText}`);
128
124
  }
129
125
  }
@@ -185,8 +181,7 @@ var A2AClient = class {
185
181
  throw new Error(`HTTP error establishing stream for message/stream: ${response.status} ${response.statusText}. RPC Error: ${errorJson.error.message} (Code: ${errorJson.error.code})`);
186
182
  }
187
183
  } catch (e) {
188
- if (e.message.startsWith("HTTP error establishing stream"))
189
- throw e;
184
+ if (e.message.startsWith("HTTP error establishing stream")) throw e;
190
185
  throw new Error(`HTTP error establishing stream for message/stream: ${response.status} ${response.statusText}. Response: ${errorBody || "(empty)"}`);
191
186
  }
192
187
  throw new Error(`HTTP error establishing stream for message/stream: ${response.status} ${response.statusText}`);
@@ -277,8 +272,7 @@ var A2AClient = class {
277
272
  throw new Error(`HTTP error establishing stream for tasks/resubscribe: ${response.status} ${response.statusText}. RPC Error: ${errorJson.error.message} (Code: ${errorJson.error.code})`);
278
273
  }
279
274
  } catch (e) {
280
- if (e.message.startsWith("HTTP error establishing stream"))
281
- throw e;
275
+ if (e.message.startsWith("HTTP error establishing stream")) throw e;
282
276
  throw new Error(`HTTP error establishing stream for tasks/resubscribe: ${response.status} ${response.statusText}. Response: ${errorBody || "(empty)"}`);
283
277
  }
284
278
  throw new Error(`HTTP error establishing stream for tasks/resubscribe: ${response.status} ${response.statusText}`);
@@ -400,12 +394,113 @@ var ApiError = class extends DistriError {
400
394
  this.name = "ApiError";
401
395
  }
402
396
  };
403
- var ConnectionError = class extends DistriError {
404
- constructor(message, details) {
405
- super(message, "CONNECTION_ERROR", details);
406
- this.name = "ConnectionError";
397
+ function isDistriMessage(event) {
398
+ return "id" in event && "role" in event && "parts" in event;
399
+ }
400
+ function isDistriEvent(event) {
401
+ return "type" in event && "metadata" in event;
402
+ }
403
+
404
+ // src/encoder.ts
405
+ function convertA2AMessageToDistri(a2aMessage) {
406
+ const role = a2aMessage.role === "agent" ? "assistant" : "user";
407
+ return {
408
+ id: a2aMessage.messageId,
409
+ role,
410
+ parts: a2aMessage.parts.map(convertA2APartToDistri),
411
+ created_at: a2aMessage.createdAt
412
+ };
413
+ }
414
+ function convertA2APartToDistri(a2aPart) {
415
+ switch (a2aPart.kind) {
416
+ case "text":
417
+ return { type: "text", text: a2aPart.text };
418
+ case "file":
419
+ if ("uri" in a2aPart.file) {
420
+ return { type: "image_url", image: { mime_type: a2aPart.file.mimeType, url: a2aPart.file.uri } };
421
+ } else {
422
+ return { type: "image_bytes", image: { mime_type: a2aPart.file.mimeType, data: a2aPart.file.bytes } };
423
+ }
424
+ case "data":
425
+ switch (a2aPart.data.part_type) {
426
+ case "tool_call":
427
+ return { type: "tool_call", tool_call: a2aPart.data };
428
+ case "tool_result":
429
+ return { type: "tool_result", tool_result: a2aPart.data };
430
+ case "code_observation":
431
+ return { type: "code_observation", thought: a2aPart.data.thought, code: a2aPart.data.code };
432
+ case "plan":
433
+ return { type: "plan", plan: a2aPart.data.plan };
434
+ default:
435
+ return { type: "data", data: a2aPart.data };
436
+ }
437
+ default:
438
+ return { type: "text", text: JSON.stringify(a2aPart) };
407
439
  }
408
- };
440
+ }
441
+ function convertDistriMessageToA2A(distriMessage, context) {
442
+ let role;
443
+ switch (distriMessage.role) {
444
+ case "assistant":
445
+ role = "agent";
446
+ break;
447
+ case "user":
448
+ role = "user";
449
+ break;
450
+ case "system":
451
+ case "tool":
452
+ role = "user";
453
+ break;
454
+ default:
455
+ role = "user";
456
+ }
457
+ return {
458
+ messageId: distriMessage.id,
459
+ role,
460
+ parts: distriMessage.parts.map(convertDistriPartToA2A),
461
+ kind: "message",
462
+ contextId: context.thread_id,
463
+ taskId: context.run_id
464
+ };
465
+ }
466
+ function convertDistriPartToA2A(distriPart) {
467
+ switch (distriPart.type) {
468
+ case "text":
469
+ return { kind: "text", text: distriPart.text };
470
+ case "image_url":
471
+ return { kind: "file", file: { mimeType: distriPart.image.mime_type, uri: distriPart.image.url } };
472
+ case "image_bytes":
473
+ return { kind: "file", file: { mimeType: distriPart.image.mime_type, bytes: distriPart.image.data } };
474
+ case "tool_call":
475
+ return { kind: "data", data: { part_type: "tool_call", tool_call: distriPart.tool_call } };
476
+ case "tool_result":
477
+ let val = {
478
+ kind: "data",
479
+ data: {
480
+ tool_call_id: distriPart.tool_result.tool_call_id,
481
+ result: distriPart.tool_result.result,
482
+ part_type: "tool_result"
483
+ }
484
+ };
485
+ console.log("<> val", val);
486
+ return val;
487
+ case "code_observation":
488
+ return { kind: "data", data: { ...distriPart, part_type: "code_observation" } };
489
+ case "plan":
490
+ return { kind: "data", data: { ...distriPart, part_type: "plan" } };
491
+ case "data":
492
+ return { kind: "data", ...distriPart.data };
493
+ }
494
+ }
495
+ function extractTextFromDistriMessage(message) {
496
+ return message.parts.filter((part) => part.type === "text").map((part) => part.text).join("\n");
497
+ }
498
+ function extractToolCallsFromDistriMessage(message) {
499
+ return message.parts.filter((part) => part.type === "tool_call").map((part) => part.tool_call);
500
+ }
501
+ function extractToolResultsFromDistriMessage(message) {
502
+ return message.parts.filter((part) => part.type === "tool_result").map((part) => part.tool_result);
503
+ }
409
504
 
410
505
  // src/distri-client.ts
411
506
  var DistriClient = class {
@@ -444,8 +539,7 @@ var DistriClient = class {
444
539
  });
445
540
  return agents;
446
541
  } catch (error) {
447
- if (error instanceof ApiError)
448
- throw error;
542
+ if (error instanceof ApiError) throw error;
449
543
  throw new DistriError("Failed to fetch agents", "FETCH_ERROR", error);
450
544
  }
451
545
  }
@@ -471,8 +565,7 @@ var DistriClient = class {
471
565
  }
472
566
  return agent;
473
567
  } catch (error) {
474
- if (error instanceof ApiError)
475
- throw error;
568
+ if (error instanceof ApiError) throw error;
476
569
  throw new DistriError(`Failed to fetch agent ${agentId}`, "FETCH_ERROR", error);
477
570
  }
478
571
  }
@@ -506,8 +599,7 @@ var DistriClient = class {
506
599
  }
507
600
  throw new DistriError("Invalid response format", "INVALID_RESPONSE");
508
601
  } catch (error) {
509
- if (error instanceof A2AProtocolError || error instanceof DistriError)
510
- throw error;
602
+ if (error instanceof A2AProtocolError || error instanceof DistriError) throw error;
511
603
  throw new DistriError(`Failed to send message to agent ${agentId}`, "SEND_MESSAGE_ERROR", error);
512
604
  }
513
605
  }
@@ -539,8 +631,7 @@ var DistriClient = class {
539
631
  }
540
632
  throw new DistriError("Invalid response format", "INVALID_RESPONSE");
541
633
  } catch (error) {
542
- if (error instanceof A2AProtocolError || error instanceof DistriError)
543
- throw error;
634
+ if (error instanceof A2AProtocolError || error instanceof DistriError) throw error;
544
635
  throw new DistriError(`Failed to get task ${taskId} from agent ${agentId}`, "GET_TASK_ERROR", error);
545
636
  }
546
637
  }
@@ -567,8 +658,7 @@ var DistriClient = class {
567
658
  }
568
659
  return await response.json();
569
660
  } catch (error) {
570
- if (error instanceof ApiError)
571
- throw error;
661
+ if (error instanceof ApiError) throw error;
572
662
  throw new DistriError("Failed to fetch threads", "FETCH_ERROR", error);
573
663
  }
574
664
  }
@@ -580,8 +670,7 @@ var DistriClient = class {
580
670
  }
581
671
  return await response.json();
582
672
  } catch (error) {
583
- if (error instanceof ApiError)
584
- throw error;
673
+ if (error instanceof ApiError) throw error;
585
674
  throw new DistriError(`Failed to fetch thread ${threadId}`, "FETCH_ERROR", error);
586
675
  }
587
676
  }
@@ -599,11 +688,28 @@ var DistriClient = class {
599
688
  }
600
689
  return await response.json();
601
690
  } catch (error) {
602
- if (error instanceof ApiError)
603
- throw error;
691
+ if (error instanceof ApiError) throw error;
604
692
  throw new DistriError(`Failed to fetch messages for thread ${threadId}`, "FETCH_ERROR", error);
605
693
  }
606
694
  }
695
+ /**
696
+ * Get messages from a thread as DistriMessage format
697
+ */
698
+ async getThreadMessagesAsDistri(threadId) {
699
+ const messages = await this.getThreadMessages(threadId);
700
+ return messages.map(convertA2AMessageToDistri);
701
+ }
702
+ /**
703
+ * Send a DistriMessage to a thread
704
+ */
705
+ async sendDistriMessage(threadId, message, context) {
706
+ const a2aMessage = convertDistriMessageToA2A(message, context);
707
+ const params = {
708
+ message: a2aMessage,
709
+ metadata: context.metadata
710
+ };
711
+ await this.sendMessage(threadId, params);
712
+ }
607
713
  /**
608
714
  * Get the base URL for making direct requests
609
715
  */
@@ -664,20 +770,32 @@ var DistriClient = class {
664
770
  /**
665
771
  * Helper method to create A2A messages
666
772
  */
667
- static initMessage(input, role = "user", contextId, messageId, taskId) {
773
+ static initMessage(parts, role = "user", message) {
668
774
  return {
669
- messageId: messageId || uuidv4(),
775
+ messageId: message.messageId || uuidv4(),
776
+ taskId: message.taskId || uuidv4(),
777
+ contextId: message.contextId,
670
778
  role,
671
- parts: [{ kind: "text", text: input.trim() }],
672
- contextId,
673
- taskId: taskId || uuidv4(),
779
+ parts: Array.isArray(parts) ? parts : [{ kind: "text", text: parts.trim() }],
780
+ ...message,
674
781
  kind: "message"
675
782
  };
676
783
  }
784
+ /**
785
+ * Create a DistriMessage instance
786
+ */
787
+ static initDistriMessage(role, parts, id, created_at) {
788
+ return {
789
+ id: id || uuidv4(),
790
+ role,
791
+ parts,
792
+ created_at
793
+ };
794
+ }
677
795
  /**
678
796
  * Helper method to create message send parameters
679
797
  */
680
- static initMessageParams(message, configuration) {
798
+ static initMessageParams(message, configuration, metadata) {
681
799
  return {
682
800
  message,
683
801
  configuration: {
@@ -685,7 +803,18 @@ var DistriClient = class {
685
803
  blocking: false,
686
804
  // Default to non-blocking for streaming
687
805
  ...configuration
688
- }
806
+ },
807
+ metadata
808
+ };
809
+ }
810
+ /**
811
+ * Create MessageSendParams from a DistriMessage using InvokeContext
812
+ */
813
+ static initDistriMessageParams(message, context) {
814
+ const a2aMessage = convertDistriMessageToA2A(message, context);
815
+ return {
816
+ message: a2aMessage,
817
+ metadata: context.metadata
689
818
  };
690
819
  }
691
820
  };
@@ -701,13 +830,138 @@ function uuidv4() {
701
830
  (b, i) => ([4, 6, 8, 10].includes(i) ? "-" : "") + b.toString(16).padStart(2, "0")
702
831
  ).join("");
703
832
  }
833
+
834
+ // src/agent.ts
835
+ var Agent = class _Agent {
836
+ constructor(agentDefinition, client) {
837
+ this.tools = /* @__PURE__ */ new Map();
838
+ this.agentDefinition = agentDefinition;
839
+ this.client = client;
840
+ }
841
+ /**
842
+ * Add a tool to the agent (AG-UI style)
843
+ */
844
+ registerTool(tool) {
845
+ this.tools.set(tool.name, tool);
846
+ }
847
+ /**
848
+ * Add multiple tools at once
849
+ */
850
+ registerTools(tools) {
851
+ tools.forEach((tool) => this.registerTool(tool));
852
+ }
853
+ /**
854
+ * Remove a tool
855
+ */
856
+ unregisterTool(toolName) {
857
+ this.tools.delete(toolName);
858
+ }
859
+ /**
860
+ * Get all registered tools
861
+ */
862
+ getTools() {
863
+ return Array.from(this.tools.values());
864
+ }
865
+ /**
866
+ * Check if a tool is registered
867
+ */
868
+ hasTool(toolName) {
869
+ return this.tools.has(toolName);
870
+ }
871
+ /**
872
+ * Get agent information
873
+ */
874
+ get id() {
875
+ return this.agentDefinition.id;
876
+ }
877
+ get name() {
878
+ return this.agentDefinition.name;
879
+ }
880
+ get description() {
881
+ return this.agentDefinition.description;
882
+ }
883
+ get iconUrl() {
884
+ return this.agentDefinition.icon_url;
885
+ }
886
+ /**
887
+ * Fetch messages for a thread (public method for useChat)
888
+ */
889
+ async getThreadMessages(threadId) {
890
+ return this.client.getThreadMessages(threadId);
891
+ }
892
+ /**
893
+ * Direct (non-streaming) invoke
894
+ */
895
+ async invoke(params) {
896
+ const enhancedParams = this.enhanceParamsWithTools(params);
897
+ console.log("enhancedParams", enhancedParams);
898
+ return await this.client.sendMessage(this.agentDefinition.id, enhancedParams);
899
+ }
900
+ /**
901
+ * Streaming invoke
902
+ */
903
+ async invokeStream(params) {
904
+ const enhancedParams = this.enhanceParamsWithTools(params);
905
+ console.log("enhancedParams", enhancedParams);
906
+ const a2aStream = this.client.sendMessageStream(this.agentDefinition.id, enhancedParams);
907
+ return async function* () {
908
+ for await (const event of a2aStream) {
909
+ if (event.kind === "message") {
910
+ yield convertA2AMessageToDistri(event);
911
+ } else if (event.kind === "status-update") {
912
+ yield event;
913
+ } else if (event.kind === "artifact-update") {
914
+ yield event;
915
+ } else {
916
+ yield event;
917
+ }
918
+ }
919
+ }();
920
+ }
921
+ /**
922
+ * Enhance message params with tool definitions
923
+ */
924
+ enhanceParamsWithTools(params) {
925
+ const tools = this.getTools();
926
+ return {
927
+ ...params,
928
+ metadata: {
929
+ ...params.metadata,
930
+ tools: tools.map((tool) => ({
931
+ name: tool.name,
932
+ description: tool.description,
933
+ input_schema: tool.input_schema
934
+ }))
935
+ }
936
+ };
937
+ }
938
+ /**
939
+ * Create an agent instance from an agent ID
940
+ */
941
+ static async create(agentId, client) {
942
+ const agentDefinition = await client.getAgent(agentId);
943
+ return new _Agent(agentDefinition, client);
944
+ }
945
+ /**
946
+ * List all available agents
947
+ */
948
+ static async list(client) {
949
+ const agentDefinitions = await client.getAgents();
950
+ return agentDefinitions.map((def) => new _Agent(def, client));
951
+ }
952
+ };
704
953
  export {
705
- A2AClient,
706
- A2AProtocolError,
707
- ApiError,
708
- ConnectionError,
954
+ Agent,
709
955
  DistriClient,
710
- DistriError,
956
+ convertA2AMessageToDistri,
957
+ convertA2APartToDistri,
958
+ convertDistriMessageToA2A,
959
+ convertDistriPartToA2A,
960
+ extractTextFromDistriMessage,
961
+ extractToolCallsFromDistriMessage,
962
+ extractToolResultsFromDistriMessage,
963
+ isDistriEvent,
964
+ isDistriMessage,
711
965
  uuidv4
712
966
  };
713
967
  //# sourceMappingURL=index.mjs.map