march-ai-sdk 0.3.0 → 0.4.0

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
@@ -409,32 +409,6 @@ var Message = class _Message {
409
409
  return this.attachmentClient.downloadAsBase64(this.attachment.url);
410
410
  }
411
411
  };
412
- var ArtifactTypeSchema = z.enum([
413
- "document",
414
- "image",
415
- "iframe",
416
- "video",
417
- "audio",
418
- "code",
419
- "link",
420
- "file"
421
- ]);
422
- var ArtifactSchema = z.object({
423
- url: z.string(),
424
- type: ArtifactTypeSchema,
425
- title: z.string().optional(),
426
- description: z.string().optional(),
427
- metadata: z.record(z.string(), z.unknown()).optional()
428
- });
429
- function toArtifact(input) {
430
- return ArtifactSchema.parse({
431
- url: input.url,
432
- type: input.type,
433
- title: input.title,
434
- description: input.description,
435
- metadata: input.metadata
436
- });
437
- }
438
412
 
439
413
  // src/streamer.ts
440
414
  var Streamer = class {
@@ -446,9 +420,7 @@ var Streamer = class {
446
420
  awaiting;
447
421
  responseSchema;
448
422
  messageMetadata;
449
- artifacts = [];
450
423
  streamedContent = "";
451
- firstChunkSent = false;
452
424
  finished = false;
453
425
  constructor(options) {
454
426
  this.agentName = options.agentName;
@@ -473,18 +445,22 @@ var Streamer = class {
473
445
  return this;
474
446
  }
475
447
  /**
476
- * Add an artifact to the message (fluent API).
448
+ * Bind a structural streamer to this streamer for event sending.
449
+ *
450
+ * Returns the structural object itself with streaming capability enabled.
451
+ *
452
+ * @param structural - StructuralStreamer instance (Artifact, Surface, etc.)
453
+ * @returns The same structural object, now bound to this streamer
454
+ *
455
+ * @example
456
+ * ```typescript
457
+ * const artifact = new Artifact()
458
+ * s.streamBy(artifact).generating("Creating...")
459
+ * s.streamBy(artifact).done({ url: "...", type: "image" })
460
+ * ```
477
461
  */
478
- addArtifact(artifact) {
479
- this.artifacts.push(toArtifact(artifact));
480
- return this;
481
- }
482
- /**
483
- * Set all artifacts at once (replaces any existing).
484
- */
485
- setArtifacts(artifacts) {
486
- this.artifacts = artifacts.map(toArtifact);
487
- return this;
462
+ streamBy(structural) {
463
+ return structural._bindStreamer(this);
488
464
  }
489
465
  /**
490
466
  * Stream a content chunk.
@@ -498,7 +474,7 @@ var Streamer = class {
498
474
  if (persist) {
499
475
  this.streamedContent += content;
500
476
  }
501
- this.send(content, false, persist, eventType);
477
+ this._send(content, false, persist, eventType);
502
478
  }
503
479
  /**
504
480
  * Alias for stream() - write a content chunk.
@@ -518,7 +494,7 @@ var Streamer = class {
518
494
  if (this.responseSchema && awaitingOverride !== false) {
519
495
  finalAwaiting = true;
520
496
  }
521
- this.send("", true, false);
497
+ this._send("", true, false);
522
498
  if (this.responseSchema && this.conversationClient) {
523
499
  await this.setPendingResponseSchema();
524
500
  }
@@ -528,8 +504,9 @@ var Streamer = class {
528
504
  }
529
505
  /**
530
506
  * Send message to router via gateway.
507
+ * This method is used internally and by structural streamers.
531
508
  */
532
- send(content, done, persist = true, eventType) {
509
+ _send(content, done, persist = true, eventType) {
533
510
  const headers = {
534
511
  conversationId: this.originalMessage.conversationId,
535
512
  userId: this.originalMessage.userId,
@@ -540,14 +517,10 @@ var Streamer = class {
540
517
  if (eventType) {
541
518
  headers.eventType = eventType;
542
519
  }
543
- if (!this.firstChunkSent) {
544
- this.firstChunkSent = true;
520
+ if (this.streamedContent.length === 0 && !this.finished) {
545
521
  if (this.messageMetadata) {
546
522
  headers.messageMetadata = JSON.stringify(this.messageMetadata);
547
523
  }
548
- if (this.artifacts.length > 0) {
549
- headers.artifacts = JSON.stringify(this.artifacts);
550
- }
551
524
  if (this.responseSchema) {
552
525
  headers.responseSchema = JSON.stringify(this.responseSchema);
553
526
  }
@@ -1515,6 +1488,341 @@ var MarchAgentApp = class {
1515
1488
  }
1516
1489
  };
1517
1490
 
1491
+ // src/structural/base.ts
1492
+ var StructuralStreamer = class {
1493
+ id;
1494
+ _streamer;
1495
+ constructor(id) {
1496
+ this.id = id ?? this._generateId();
1497
+ }
1498
+ /**
1499
+ * Bind this structural streamer to a Streamer instance.
1500
+ * Called by Streamer.streamBy(). Returns self for chaining.
1501
+ */
1502
+ _bindStreamer(streamer) {
1503
+ this._streamer = streamer;
1504
+ return this;
1505
+ }
1506
+ /**
1507
+ * Send an event through the bound streamer.
1508
+ *
1509
+ * Creates event payload and sends via streamer._send().
1510
+ * Returns self for method chaining.
1511
+ */
1512
+ _sendEvent(action, data = {}) {
1513
+ if (!this._streamer) {
1514
+ throw new Error(
1515
+ `${this.constructor.name} not bound to a Streamer. Call streamer.streamBy() first.`
1516
+ );
1517
+ }
1518
+ const body = { id: this.id, ...data };
1519
+ const eventType = `${this.getEventTypePrefix()}:${action}`;
1520
+ this._streamer._send(
1521
+ JSON.stringify(body),
1522
+ false,
1523
+ // done
1524
+ false,
1525
+ // persist
1526
+ eventType
1527
+ );
1528
+ return this;
1529
+ }
1530
+ };
1531
+ function generateShortId() {
1532
+ return Math.random().toString(16).slice(2, 10);
1533
+ }
1534
+
1535
+ // src/structural/artifact.ts
1536
+ var Artifact = class extends StructuralStreamer {
1537
+ _generateId() {
1538
+ return `artifact-${generateShortId()}`;
1539
+ }
1540
+ getEventTypePrefix() {
1541
+ return "artifact";
1542
+ }
1543
+ /**
1544
+ * Signal artifact is being generated.
1545
+ *
1546
+ * @param message - Status message (e.g., "Creating chart...")
1547
+ * @param progress - Progress value 0.0-1.0
1548
+ * @returns this for method chaining
1549
+ */
1550
+ generating(message, progress) {
1551
+ const data = {};
1552
+ if (message !== void 0) {
1553
+ data.message = message;
1554
+ }
1555
+ if (progress !== void 0) {
1556
+ data.progress = progress;
1557
+ }
1558
+ return this._sendEvent("generating", data);
1559
+ }
1560
+ /**
1561
+ * Signal artifact is complete and persist to database.
1562
+ *
1563
+ * @param options - Artifact completion options
1564
+ * @param options.url - URL to artifact
1565
+ * @param options.type - Artifact type (image, iframe, document, video, audio, code, link, file)
1566
+ * @param options.title - Display title
1567
+ * @param options.description - Optional description
1568
+ * @param options.metadata - Additional metadata (size, mimeType, dimensions, etc.)
1569
+ * @returns this for method chaining
1570
+ */
1571
+ done(options) {
1572
+ const data = {
1573
+ url: options.url,
1574
+ type: options.type
1575
+ };
1576
+ if (options.title) {
1577
+ data.title = options.title;
1578
+ }
1579
+ if (options.description) {
1580
+ data.description = options.description;
1581
+ }
1582
+ if (options.metadata) {
1583
+ data.metadata = options.metadata;
1584
+ }
1585
+ return this._sendEvent("done", data);
1586
+ }
1587
+ /**
1588
+ * Signal artifact generation failed.
1589
+ *
1590
+ * @param message - Error message
1591
+ * @returns this for method chaining
1592
+ */
1593
+ error(message) {
1594
+ return this._sendEvent("error", { message });
1595
+ }
1596
+ };
1597
+
1598
+ // src/structural/surface.ts
1599
+ var Surface = class extends StructuralStreamer {
1600
+ _generateId() {
1601
+ return `surface-${generateShortId()}`;
1602
+ }
1603
+ getEventTypePrefix() {
1604
+ return "surface";
1605
+ }
1606
+ /**
1607
+ * Signal surface is loading.
1608
+ *
1609
+ * @param message - Status message (e.g., "Loading calendar...")
1610
+ * @param progress - Progress value 0.0-1.0
1611
+ * @returns this for method chaining
1612
+ */
1613
+ generating(message, progress) {
1614
+ const data = {};
1615
+ if (message !== void 0) {
1616
+ data.message = message;
1617
+ }
1618
+ if (progress !== void 0) {
1619
+ data.progress = progress;
1620
+ }
1621
+ return this._sendEvent("generating", data);
1622
+ }
1623
+ /**
1624
+ * Signal surface is ready and persist to database.
1625
+ *
1626
+ * @param options - Surface completion options
1627
+ * @param options.url - URL to surface
1628
+ * @param options.type - Surface type (default: iframe)
1629
+ * @param options.title - Display title
1630
+ * @param options.description - Optional description
1631
+ * @param options.metadata - Additional metadata
1632
+ * @returns this for method chaining
1633
+ */
1634
+ done(options) {
1635
+ const data = {
1636
+ url: options.url,
1637
+ type: options.type ?? "iframe"
1638
+ };
1639
+ if (options.title) {
1640
+ data.title = options.title;
1641
+ }
1642
+ if (options.description) {
1643
+ data.description = options.description;
1644
+ }
1645
+ if (options.metadata) {
1646
+ data.metadata = options.metadata;
1647
+ }
1648
+ return this._sendEvent("done", data);
1649
+ }
1650
+ /**
1651
+ * Signal surface loading failed.
1652
+ *
1653
+ * @param message - Error message
1654
+ * @returns this for method chaining
1655
+ */
1656
+ error(message) {
1657
+ return this._sendEvent("error", { message });
1658
+ }
1659
+ };
1660
+
1661
+ // src/structural/text-block.ts
1662
+ var TextBlock = class extends StructuralStreamer {
1663
+ initialTitle;
1664
+ constructor(options) {
1665
+ super(options?.id);
1666
+ this.initialTitle = options?.title;
1667
+ }
1668
+ _generateId() {
1669
+ return `text_block-${generateShortId()}`;
1670
+ }
1671
+ getEventTypePrefix() {
1672
+ return "text_block";
1673
+ }
1674
+ /**
1675
+ * Stream title content (appends to existing).
1676
+ *
1677
+ * @param content - Content to append to title
1678
+ * @returns this for method chaining
1679
+ */
1680
+ streamTitle(content) {
1681
+ return this._sendEvent("stream_title", { content });
1682
+ }
1683
+ /**
1684
+ * Stream body content (appends to existing).
1685
+ *
1686
+ * @param content - Content to append to body
1687
+ * @returns this for method chaining
1688
+ */
1689
+ streamBody(content) {
1690
+ return this._sendEvent("stream_body", { content });
1691
+ }
1692
+ /**
1693
+ * Replace entire title.
1694
+ *
1695
+ * @param title - New title (replaces existing)
1696
+ * @returns this for method chaining
1697
+ */
1698
+ updateTitle(title) {
1699
+ return this._sendEvent("update_title", { title });
1700
+ }
1701
+ /**
1702
+ * Replace entire body.
1703
+ *
1704
+ * @param body - New body (replaces existing)
1705
+ * @returns this for method chaining
1706
+ */
1707
+ updateBody(body) {
1708
+ return this._sendEvent("update_body", { body });
1709
+ }
1710
+ /**
1711
+ * Set visual variant.
1712
+ *
1713
+ * @param variant - Visual style (thinking, note, warning, error, success)
1714
+ * @returns this for method chaining
1715
+ */
1716
+ setVariant(variant) {
1717
+ return this._sendEvent("set_variant", { variant });
1718
+ }
1719
+ /**
1720
+ * Mark text block as complete.
1721
+ *
1722
+ * @returns this for method chaining
1723
+ */
1724
+ done() {
1725
+ return this._sendEvent("done");
1726
+ }
1727
+ };
1728
+
1729
+ // src/structural/stepper.ts
1730
+ var Stepper = class extends StructuralStreamer {
1731
+ steps;
1732
+ _initialized = false;
1733
+ constructor(options) {
1734
+ super(options?.id);
1735
+ this.steps = options?.steps ?? [];
1736
+ }
1737
+ _generateId() {
1738
+ return `stepper-${generateShortId()}`;
1739
+ }
1740
+ getEventTypePrefix() {
1741
+ return "stepper";
1742
+ }
1743
+ /**
1744
+ * Send initialization event with steps if not already sent.
1745
+ * This is automatically called before any other stepper event.
1746
+ */
1747
+ _ensureInitialized() {
1748
+ if (!this._initialized && this.steps.length > 0) {
1749
+ this._sendEvent("init", { steps: this.steps });
1750
+ this._initialized = true;
1751
+ }
1752
+ return this;
1753
+ }
1754
+ /**
1755
+ * Mark step as in progress.
1756
+ *
1757
+ * @param index - Step index to start
1758
+ * @returns this for method chaining
1759
+ */
1760
+ startStep(index) {
1761
+ this._ensureInitialized();
1762
+ return this._sendEvent("start_step", { index });
1763
+ }
1764
+ /**
1765
+ * Mark step as complete.
1766
+ *
1767
+ * @param index - Step index to complete
1768
+ * @returns this for method chaining
1769
+ */
1770
+ completeStep(index) {
1771
+ this._ensureInitialized();
1772
+ return this._sendEvent("complete_step", { index });
1773
+ }
1774
+ /**
1775
+ * Mark step as failed.
1776
+ *
1777
+ * @param index - Step index that failed
1778
+ * @param error - Optional error message
1779
+ * @returns this for method chaining
1780
+ */
1781
+ failStep(index, error) {
1782
+ this._ensureInitialized();
1783
+ const data = { index };
1784
+ if (error) {
1785
+ data.error = error;
1786
+ }
1787
+ return this._sendEvent("fail_step", data);
1788
+ }
1789
+ /**
1790
+ * Add a new step dynamically.
1791
+ *
1792
+ * @param label - Step label
1793
+ * @param index - Optional position to insert at
1794
+ * @returns this for method chaining
1795
+ */
1796
+ addStep(label, index) {
1797
+ this._ensureInitialized();
1798
+ const data = { label };
1799
+ if (index !== void 0) {
1800
+ data.index = index;
1801
+ }
1802
+ return this._sendEvent("add_step", data);
1803
+ }
1804
+ /**
1805
+ * Update step label.
1806
+ *
1807
+ * @param index - Step index to update
1808
+ * @param label - New label
1809
+ * @returns this for method chaining
1810
+ */
1811
+ updateStepLabel(index, label) {
1812
+ this._ensureInitialized();
1813
+ return this._sendEvent("update_step_label", { index, label });
1814
+ }
1815
+ /**
1816
+ * Mark stepper as complete (all steps finished).
1817
+ *
1818
+ * @returns this for method chaining
1819
+ */
1820
+ done() {
1821
+ this._ensureInitialized();
1822
+ return this._sendEvent("done");
1823
+ }
1824
+ };
1825
+
1518
1826
  // src/checkpoint-client.ts
1519
1827
  var CheckpointClient = class {
1520
1828
  baseUrl;
@@ -1688,8 +1996,8 @@ var AgentStateClient = class {
1688
1996
  };
1689
1997
 
1690
1998
  // src/index.ts
1691
- var VERSION = "0.3.0";
1999
+ var VERSION = "0.4.0";
1692
2000
 
1693
- export { AI_INVENTORY_PATHS, APIException, Agent, AgentStateClient, AttachmentClient, CONVERSATION_STORE_PATHS, CheckpointClient, ConfigurationError, Conversation, ConversationClient, ConversationMessage, GatewayClient, GatewayError, HeartbeatError, HeartbeatManager, KafkaError, MEMORY_PATHS, MarchAgentApp, MarchAgentError, Memory, MemoryClient, Message, RegistrationError, SERVICES, SenderFilter, Streamer, VERSION, createAttachmentInfo, toArtifact };
2001
+ export { AI_INVENTORY_PATHS, APIException, Agent, AgentStateClient, Artifact, AttachmentClient, CONVERSATION_STORE_PATHS, CheckpointClient, ConfigurationError, Conversation, ConversationClient, ConversationMessage, GatewayClient, GatewayError, HeartbeatError, HeartbeatManager, KafkaError, MEMORY_PATHS, MarchAgentApp, MarchAgentError, Memory, MemoryClient, Message, RegistrationError, SERVICES, SenderFilter, Stepper, Streamer, StructuralStreamer, Surface, TextBlock, VERSION, createAttachmentInfo };
1694
2002
  //# sourceMappingURL=index.js.map
1695
2003
  //# sourceMappingURL=index.js.map