@contractspec/integration.providers-impls 2.9.0 → 3.0.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.
Files changed (61) hide show
  1. package/README.md +59 -0
  2. package/dist/health.d.ts +1 -0
  3. package/dist/health.js +3 -0
  4. package/dist/impls/async-event-queue.d.ts +8 -0
  5. package/dist/impls/async-event-queue.js +47 -0
  6. package/dist/impls/health/base-health-provider.d.ts +98 -0
  7. package/dist/impls/health/base-health-provider.js +616 -0
  8. package/dist/impls/health/hybrid-health-providers.d.ts +34 -0
  9. package/dist/impls/health/hybrid-health-providers.js +1088 -0
  10. package/dist/impls/health/official-health-providers.d.ts +78 -0
  11. package/dist/impls/health/official-health-providers.js +968 -0
  12. package/dist/impls/health/provider-normalizers.d.ts +28 -0
  13. package/dist/impls/health/provider-normalizers.js +287 -0
  14. package/dist/impls/health/providers.d.ts +2 -0
  15. package/dist/impls/health/providers.js +1094 -0
  16. package/dist/impls/health-provider-factory.d.ts +3 -0
  17. package/dist/impls/health-provider-factory.js +1308 -0
  18. package/dist/impls/index.d.ts +8 -0
  19. package/dist/impls/index.js +2356 -176
  20. package/dist/impls/messaging-github.d.ts +17 -0
  21. package/dist/impls/messaging-github.js +110 -0
  22. package/dist/impls/messaging-slack.d.ts +14 -0
  23. package/dist/impls/messaging-slack.js +80 -0
  24. package/dist/impls/messaging-whatsapp-meta.d.ts +13 -0
  25. package/dist/impls/messaging-whatsapp-meta.js +52 -0
  26. package/dist/impls/messaging-whatsapp-twilio.d.ts +13 -0
  27. package/dist/impls/messaging-whatsapp-twilio.js +82 -0
  28. package/dist/impls/mistral-conversational.d.ts +23 -0
  29. package/dist/impls/mistral-conversational.js +476 -0
  30. package/dist/impls/mistral-conversational.session.d.ts +32 -0
  31. package/dist/impls/mistral-conversational.session.js +206 -0
  32. package/dist/impls/mistral-stt.d.ts +17 -0
  33. package/dist/impls/mistral-stt.js +167 -0
  34. package/dist/impls/provider-factory.d.ts +7 -1
  35. package/dist/impls/provider-factory.js +2338 -176
  36. package/dist/impls/stripe-payments.js +1 -1
  37. package/dist/index.d.ts +2 -0
  38. package/dist/index.js +2360 -174
  39. package/dist/messaging.d.ts +1 -0
  40. package/dist/messaging.js +3 -0
  41. package/dist/node/health.js +2 -0
  42. package/dist/node/impls/async-event-queue.js +46 -0
  43. package/dist/node/impls/health/base-health-provider.js +615 -0
  44. package/dist/node/impls/health/hybrid-health-providers.js +1087 -0
  45. package/dist/node/impls/health/official-health-providers.js +967 -0
  46. package/dist/node/impls/health/provider-normalizers.js +286 -0
  47. package/dist/node/impls/health/providers.js +1093 -0
  48. package/dist/node/impls/health-provider-factory.js +1307 -0
  49. package/dist/node/impls/index.js +2356 -176
  50. package/dist/node/impls/messaging-github.js +109 -0
  51. package/dist/node/impls/messaging-slack.js +79 -0
  52. package/dist/node/impls/messaging-whatsapp-meta.js +51 -0
  53. package/dist/node/impls/messaging-whatsapp-twilio.js +81 -0
  54. package/dist/node/impls/mistral-conversational.js +475 -0
  55. package/dist/node/impls/mistral-conversational.session.js +205 -0
  56. package/dist/node/impls/mistral-stt.js +166 -0
  57. package/dist/node/impls/provider-factory.js +2338 -176
  58. package/dist/node/impls/stripe-payments.js +1 -1
  59. package/dist/node/index.js +2360 -174
  60. package/dist/node/messaging.js +2 -0
  61. package/package.json +204 -12
@@ -1,4 +1,48 @@
1
1
  // @bun
2
+ // src/impls/async-event-queue.ts
3
+ class AsyncEventQueue {
4
+ values = [];
5
+ waiters = [];
6
+ done = false;
7
+ push(value) {
8
+ if (this.done) {
9
+ return;
10
+ }
11
+ const waiter = this.waiters.shift();
12
+ if (waiter) {
13
+ waiter({ value, done: false });
14
+ return;
15
+ }
16
+ this.values.push(value);
17
+ }
18
+ close() {
19
+ if (this.done) {
20
+ return;
21
+ }
22
+ this.done = true;
23
+ for (const waiter of this.waiters) {
24
+ waiter({ value: undefined, done: true });
25
+ }
26
+ this.waiters.length = 0;
27
+ }
28
+ [Symbol.asyncIterator]() {
29
+ return {
30
+ next: async () => {
31
+ const value = this.values.shift();
32
+ if (value != null) {
33
+ return { value, done: false };
34
+ }
35
+ if (this.done) {
36
+ return { value: undefined, done: true };
37
+ }
38
+ return new Promise((resolve) => {
39
+ this.waiters.push(resolve);
40
+ });
41
+ }
42
+ };
43
+ }
44
+ }
45
+
2
46
  // src/impls/elevenlabs-voice.ts
3
47
  import { ElevenLabsClient } from "@elevenlabs/elevenlabs-js";
4
48
  var FORMAT_MAP = {
@@ -1465,131 +1509,1436 @@ async function safeReadError3(response) {
1465
1509
  }
1466
1510
  }
1467
1511
 
1468
- // src/impls/mistral-llm.ts
1469
- import { Mistral } from "@mistralai/mistralai";
1512
+ // src/impls/health/provider-normalizers.ts
1513
+ var DEFAULT_LIST_KEYS = [
1514
+ "items",
1515
+ "data",
1516
+ "records",
1517
+ "activities",
1518
+ "workouts",
1519
+ "sleep",
1520
+ "biometrics",
1521
+ "nutrition"
1522
+ ];
1523
+ function asRecord(value) {
1524
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
1525
+ return;
1526
+ }
1527
+ return value;
1528
+ }
1529
+ function asArray2(value) {
1530
+ return Array.isArray(value) ? value : undefined;
1531
+ }
1532
+ function readString2(record, keys) {
1533
+ if (!record)
1534
+ return;
1535
+ for (const key of keys) {
1536
+ const value = record[key];
1537
+ if (typeof value === "string" && value.trim().length > 0) {
1538
+ return value;
1539
+ }
1540
+ }
1541
+ return;
1542
+ }
1543
+ function readNumber(record, keys) {
1544
+ if (!record)
1545
+ return;
1546
+ for (const key of keys) {
1547
+ const value = record[key];
1548
+ if (typeof value === "number" && Number.isFinite(value)) {
1549
+ return value;
1550
+ }
1551
+ if (typeof value === "string" && value.trim().length > 0) {
1552
+ const parsed = Number(value);
1553
+ if (Number.isFinite(parsed)) {
1554
+ return parsed;
1555
+ }
1556
+ }
1557
+ }
1558
+ return;
1559
+ }
1560
+ function readBoolean2(record, keys) {
1561
+ if (!record)
1562
+ return;
1563
+ for (const key of keys) {
1564
+ const value = record[key];
1565
+ if (typeof value === "boolean") {
1566
+ return value;
1567
+ }
1568
+ }
1569
+ return;
1570
+ }
1571
+ function extractList(payload, listKeys = DEFAULT_LIST_KEYS) {
1572
+ const root = asRecord(payload);
1573
+ if (!root) {
1574
+ return asArray2(payload)?.map((item) => asRecord(item)).filter((item) => Boolean(item)) ?? [];
1575
+ }
1576
+ for (const key of listKeys) {
1577
+ const arrayValue = asArray2(root[key]);
1578
+ if (!arrayValue)
1579
+ continue;
1580
+ return arrayValue.map((item) => asRecord(item)).filter((item) => Boolean(item));
1581
+ }
1582
+ return [];
1583
+ }
1584
+ function extractPagination(payload) {
1585
+ const root = asRecord(payload);
1586
+ const nestedPagination = asRecord(root?.pagination);
1587
+ const nextCursor = readString2(nestedPagination, ["nextCursor", "next_cursor"]) ?? readString2(root, [
1588
+ "nextCursor",
1589
+ "next_cursor",
1590
+ "cursor",
1591
+ "next_page_token"
1592
+ ]);
1593
+ const hasMore = readBoolean2(nestedPagination, ["hasMore", "has_more"]) ?? readBoolean2(root, ["hasMore", "has_more"]);
1594
+ return {
1595
+ nextCursor,
1596
+ hasMore: hasMore ?? Boolean(nextCursor)
1597
+ };
1598
+ }
1599
+ function toHealthActivity(item, context, fallbackType = "activity") {
1600
+ const externalId = readString2(item, ["external_id", "externalId", "uuid", "id"]) ?? `${context.providerKey}:${fallbackType}`;
1601
+ const id = readString2(item, ["id", "uuid", "workout_id"]) ?? `${context.providerKey}:activity:${externalId}`;
1602
+ return {
1603
+ id,
1604
+ externalId,
1605
+ tenantId: context.tenantId,
1606
+ connectionId: context.connectionId ?? "unknown",
1607
+ userId: readString2(item, ["user_id", "userId", "athlete_id"]),
1608
+ providerKey: context.providerKey,
1609
+ activityType: readString2(item, ["activity_type", "type", "sport_type", "sport"]) ?? fallbackType,
1610
+ startedAt: readIsoDate(item, [
1611
+ "started_at",
1612
+ "start_time",
1613
+ "start_date",
1614
+ "created_at"
1615
+ ]),
1616
+ endedAt: readIsoDate(item, ["ended_at", "end_time"]),
1617
+ durationSeconds: readNumber(item, [
1618
+ "duration_seconds",
1619
+ "duration",
1620
+ "elapsed_time"
1621
+ ]),
1622
+ distanceMeters: readNumber(item, ["distance_meters", "distance"]),
1623
+ caloriesKcal: readNumber(item, [
1624
+ "calories_kcal",
1625
+ "calories",
1626
+ "active_kilocalories"
1627
+ ]),
1628
+ steps: readNumber(item, ["steps"])?.valueOf(),
1629
+ metadata: item
1630
+ };
1631
+ }
1632
+ function toHealthWorkout(item, context, fallbackType = "workout") {
1633
+ const activity = toHealthActivity(item, context, fallbackType);
1634
+ return {
1635
+ id: activity.id,
1636
+ externalId: activity.externalId,
1637
+ tenantId: activity.tenantId,
1638
+ connectionId: activity.connectionId,
1639
+ userId: activity.userId,
1640
+ providerKey: activity.providerKey,
1641
+ workoutType: readString2(item, [
1642
+ "workout_type",
1643
+ "sport_type",
1644
+ "type",
1645
+ "activity_type"
1646
+ ]) ?? fallbackType,
1647
+ startedAt: activity.startedAt,
1648
+ endedAt: activity.endedAt,
1649
+ durationSeconds: activity.durationSeconds,
1650
+ distanceMeters: activity.distanceMeters,
1651
+ caloriesKcal: activity.caloriesKcal,
1652
+ averageHeartRateBpm: readNumber(item, [
1653
+ "average_heart_rate",
1654
+ "avg_hr",
1655
+ "average_heart_rate_bpm"
1656
+ ]),
1657
+ maxHeartRateBpm: readNumber(item, [
1658
+ "max_heart_rate",
1659
+ "max_hr",
1660
+ "max_heart_rate_bpm"
1661
+ ]),
1662
+ metadata: item
1663
+ };
1664
+ }
1665
+ function toHealthSleep(item, context) {
1666
+ const externalId = readString2(item, ["external_id", "externalId", "uuid", "id"]) ?? `${context.providerKey}:sleep`;
1667
+ const id = readString2(item, ["id", "uuid"]) ?? `${context.providerKey}:sleep:${externalId}`;
1668
+ const startedAt = readIsoDate(item, ["started_at", "start_time", "bedtime_start", "start"]) ?? new Date(0).toISOString();
1669
+ const endedAt = readIsoDate(item, ["ended_at", "end_time", "bedtime_end", "end"]) ?? startedAt;
1670
+ return {
1671
+ id,
1672
+ externalId,
1673
+ tenantId: context.tenantId,
1674
+ connectionId: context.connectionId ?? "unknown",
1675
+ userId: readString2(item, ["user_id", "userId"]),
1676
+ providerKey: context.providerKey,
1677
+ startedAt,
1678
+ endedAt,
1679
+ durationSeconds: readNumber(item, [
1680
+ "duration_seconds",
1681
+ "duration",
1682
+ "total_sleep_duration"
1683
+ ]),
1684
+ deepSleepSeconds: readNumber(item, [
1685
+ "deep_sleep_seconds",
1686
+ "deep_sleep_duration"
1687
+ ]),
1688
+ lightSleepSeconds: readNumber(item, [
1689
+ "light_sleep_seconds",
1690
+ "light_sleep_duration"
1691
+ ]),
1692
+ remSleepSeconds: readNumber(item, [
1693
+ "rem_sleep_seconds",
1694
+ "rem_sleep_duration"
1695
+ ]),
1696
+ awakeSeconds: readNumber(item, ["awake_seconds", "awake_time"]),
1697
+ sleepScore: readNumber(item, ["sleep_score", "score"]),
1698
+ metadata: item
1699
+ };
1700
+ }
1701
+ function toHealthBiometric(item, context, metricTypeFallback = "metric") {
1702
+ const externalId = readString2(item, ["external_id", "externalId", "uuid", "id"]) ?? `${context.providerKey}:biometric`;
1703
+ const id = readString2(item, ["id", "uuid"]) ?? `${context.providerKey}:biometric:${externalId}`;
1704
+ return {
1705
+ id,
1706
+ externalId,
1707
+ tenantId: context.tenantId,
1708
+ connectionId: context.connectionId ?? "unknown",
1709
+ userId: readString2(item, ["user_id", "userId"]),
1710
+ providerKey: context.providerKey,
1711
+ metricType: readString2(item, ["metric_type", "metric", "type", "name"]) ?? metricTypeFallback,
1712
+ value: readNumber(item, ["value", "score", "measurement"]) ?? 0,
1713
+ unit: readString2(item, ["unit"]),
1714
+ measuredAt: readIsoDate(item, ["measured_at", "timestamp", "created_at"]) ?? new Date().toISOString(),
1715
+ metadata: item
1716
+ };
1717
+ }
1718
+ function toHealthNutrition(item, context) {
1719
+ const externalId = readString2(item, ["external_id", "externalId", "uuid", "id"]) ?? `${context.providerKey}:nutrition`;
1720
+ const id = readString2(item, ["id", "uuid"]) ?? `${context.providerKey}:nutrition:${externalId}`;
1721
+ return {
1722
+ id,
1723
+ externalId,
1724
+ tenantId: context.tenantId,
1725
+ connectionId: context.connectionId ?? "unknown",
1726
+ userId: readString2(item, ["user_id", "userId"]),
1727
+ providerKey: context.providerKey,
1728
+ loggedAt: readIsoDate(item, ["logged_at", "created_at", "date", "timestamp"]) ?? new Date().toISOString(),
1729
+ caloriesKcal: readNumber(item, ["calories_kcal", "calories"]),
1730
+ proteinGrams: readNumber(item, ["protein_grams", "protein"]),
1731
+ carbsGrams: readNumber(item, ["carbs_grams", "carbs"]),
1732
+ fatGrams: readNumber(item, ["fat_grams", "fat"]),
1733
+ fiberGrams: readNumber(item, ["fiber_grams", "fiber"]),
1734
+ hydrationMl: readNumber(item, ["hydration_ml", "water_ml", "water"]),
1735
+ metadata: item
1736
+ };
1737
+ }
1738
+ function toHealthConnectionStatus(payload, params, source) {
1739
+ const record = asRecord(payload);
1740
+ const rawStatus = readString2(record, ["status", "connection_status", "health"]) ?? "healthy";
1741
+ return {
1742
+ tenantId: params.tenantId,
1743
+ connectionId: params.connectionId,
1744
+ status: rawStatus === "healthy" || rawStatus === "degraded" || rawStatus === "error" || rawStatus === "disconnected" ? rawStatus : "healthy",
1745
+ source,
1746
+ lastCheckedAt: readIsoDate(record, ["last_checked_at", "lastCheckedAt"]) ?? new Date().toISOString(),
1747
+ errorCode: readString2(record, ["error_code", "errorCode"]),
1748
+ errorMessage: readString2(record, ["error_message", "errorMessage"]),
1749
+ metadata: asRecord(record?.metadata)
1750
+ };
1751
+ }
1752
+ function toHealthWebhookEvent(payload, providerKey, verified) {
1753
+ const record = asRecord(payload);
1754
+ const entityType = readString2(record, ["entity_type", "entityType", "type"]);
1755
+ const normalizedEntityType = entityType === "activity" || entityType === "workout" || entityType === "sleep" || entityType === "biometric" || entityType === "nutrition" ? entityType : undefined;
1756
+ return {
1757
+ providerKey,
1758
+ eventType: readString2(record, ["event_type", "eventType", "event"]),
1759
+ externalEntityId: readString2(record, [
1760
+ "external_entity_id",
1761
+ "externalEntityId",
1762
+ "entity_id",
1763
+ "entityId",
1764
+ "id"
1765
+ ]),
1766
+ entityType: normalizedEntityType,
1767
+ receivedAt: new Date().toISOString(),
1768
+ verified,
1769
+ payload,
1770
+ metadata: asRecord(record?.metadata)
1771
+ };
1772
+ }
1773
+ function readIsoDate(record, keys) {
1774
+ const value = readString2(record, keys);
1775
+ if (!value)
1776
+ return;
1777
+ const parsed = new Date(value);
1778
+ if (Number.isNaN(parsed.getTime()))
1779
+ return;
1780
+ return parsed.toISOString();
1781
+ }
1470
1782
 
1471
- class MistralLLMProvider {
1472
- client;
1473
- defaultModel;
1783
+ // src/impls/health/base-health-provider.ts
1784
+ class HealthProviderCapabilityError extends Error {
1785
+ code = "NOT_SUPPORTED";
1786
+ constructor(message) {
1787
+ super(message);
1788
+ this.name = "HealthProviderCapabilityError";
1789
+ }
1790
+ }
1791
+
1792
+ class BaseHealthProvider {
1793
+ providerKey;
1794
+ transport;
1795
+ apiBaseUrl;
1796
+ mcpUrl;
1797
+ apiKey;
1798
+ accessToken;
1799
+ refreshToken;
1800
+ mcpAccessToken;
1801
+ webhookSecret;
1802
+ webhookSignatureHeader;
1803
+ route;
1804
+ aggregatorKey;
1805
+ oauth;
1806
+ fetchFn;
1807
+ mcpRequestId = 0;
1474
1808
  constructor(options) {
1475
- if (!options.apiKey) {
1476
- throw new Error("MistralLLMProvider requires an apiKey");
1477
- }
1478
- this.client = options.client ?? new Mistral({
1479
- apiKey: options.apiKey,
1480
- serverURL: options.serverURL,
1481
- userAgent: options.userAgentSuffix ? `${options.userAgentSuffix}` : undefined
1809
+ this.providerKey = options.providerKey;
1810
+ this.transport = options.transport;
1811
+ this.apiBaseUrl = options.apiBaseUrl;
1812
+ this.mcpUrl = options.mcpUrl;
1813
+ this.apiKey = options.apiKey;
1814
+ this.accessToken = options.accessToken;
1815
+ this.refreshToken = options.oauth?.refreshToken;
1816
+ this.mcpAccessToken = options.mcpAccessToken;
1817
+ this.webhookSecret = options.webhookSecret;
1818
+ this.webhookSignatureHeader = options.webhookSignatureHeader ?? "x-webhook-signature";
1819
+ this.route = options.route ?? "primary";
1820
+ this.aggregatorKey = options.aggregatorKey;
1821
+ this.oauth = options.oauth ?? {};
1822
+ this.fetchFn = options.fetchFn ?? fetch;
1823
+ }
1824
+ async listActivities(_params) {
1825
+ throw this.unsupported("activities");
1826
+ }
1827
+ async listWorkouts(_params) {
1828
+ throw this.unsupported("workouts");
1829
+ }
1830
+ async listSleep(_params) {
1831
+ throw this.unsupported("sleep");
1832
+ }
1833
+ async listBiometrics(_params) {
1834
+ throw this.unsupported("biometrics");
1835
+ }
1836
+ async listNutrition(_params) {
1837
+ throw this.unsupported("nutrition");
1838
+ }
1839
+ async getConnectionStatus(params) {
1840
+ return this.fetchConnectionStatus(params, {
1841
+ mcpTool: `${this.providerSlug()}_connection_status`
1482
1842
  });
1483
- this.defaultModel = options.defaultModel ?? "mistral-large-latest";
1484
1843
  }
1485
- async chat(messages, options = {}) {
1486
- const request = this.buildChatRequest(messages, options);
1487
- const response = await this.client.chat.complete(request);
1488
- return this.buildLLMResponse(response);
1844
+ async syncActivities(params) {
1845
+ return this.syncFromList(() => this.listActivities(params));
1489
1846
  }
1490
- async* stream(messages, options = {}) {
1491
- const request = this.buildChatRequest(messages, options);
1492
- request.stream = true;
1493
- const stream = await this.client.chat.stream(request);
1494
- const aggregatedParts = [];
1495
- const aggregatedToolCalls = [];
1496
- let usage;
1497
- let finishReason;
1498
- for await (const event of stream) {
1499
- for (const choice of event.data.choices) {
1500
- const delta = choice.delta;
1501
- if (typeof delta.content === "string") {
1502
- if (delta.content.length > 0) {
1503
- aggregatedParts.push({ type: "text", text: delta.content });
1504
- yield {
1505
- type: "message_delta",
1506
- delta: { type: "text", text: delta.content },
1507
- index: choice.index
1508
- };
1509
- }
1510
- } else if (Array.isArray(delta.content)) {
1511
- for (const chunk of delta.content) {
1512
- if (chunk.type === "text" && "text" in chunk) {
1513
- aggregatedParts.push({ type: "text", text: chunk.text });
1514
- yield {
1515
- type: "message_delta",
1516
- delta: { type: "text", text: chunk.text },
1517
- index: choice.index
1518
- };
1519
- }
1520
- }
1521
- }
1522
- if (delta.toolCalls) {
1523
- let localIndex = 0;
1524
- for (const call of delta.toolCalls) {
1525
- const toolCall = this.fromMistralToolCall(call, localIndex);
1526
- aggregatedToolCalls.push(toolCall);
1527
- yield {
1528
- type: "tool_call",
1529
- call: toolCall,
1530
- index: choice.index
1531
- };
1532
- localIndex += 1;
1533
- }
1534
- }
1535
- if (choice.finishReason && choice.finishReason !== "null") {
1536
- finishReason = choice.finishReason;
1537
- }
1538
- }
1539
- if (event.data.usage) {
1540
- const usageEntry = this.fromUsage(event.data.usage);
1541
- if (usageEntry) {
1542
- usage = usageEntry;
1543
- yield { type: "usage", usage: usageEntry };
1544
- }
1545
- }
1546
- }
1547
- const message = {
1548
- role: "assistant",
1549
- content: aggregatedParts.length ? aggregatedParts : [{ type: "text", text: "" }]
1847
+ async syncWorkouts(params) {
1848
+ return this.syncFromList(() => this.listWorkouts(params));
1849
+ }
1850
+ async syncSleep(params) {
1851
+ return this.syncFromList(() => this.listSleep(params));
1852
+ }
1853
+ async syncBiometrics(params) {
1854
+ return this.syncFromList(() => this.listBiometrics(params));
1855
+ }
1856
+ async syncNutrition(params) {
1857
+ return this.syncFromList(() => this.listNutrition(params));
1858
+ }
1859
+ async parseWebhook(request) {
1860
+ const payload = request.parsedBody ?? safeJsonParse(request.rawBody);
1861
+ const verified = await this.verifyWebhook(request);
1862
+ return toHealthWebhookEvent(payload, this.providerKey, verified);
1863
+ }
1864
+ async verifyWebhook(request) {
1865
+ if (!this.webhookSecret)
1866
+ return true;
1867
+ const signature = readHeader(request.headers, this.webhookSignatureHeader);
1868
+ return signature === this.webhookSecret;
1869
+ }
1870
+ async fetchActivities(params, config) {
1871
+ const response = await this.fetchList(params, config);
1872
+ return {
1873
+ activities: response.items,
1874
+ nextCursor: response.nextCursor,
1875
+ hasMore: response.hasMore,
1876
+ source: this.currentSource()
1550
1877
  };
1551
- if (aggregatedToolCalls.length > 0) {
1552
- message.content = [
1553
- ...aggregatedToolCalls,
1554
- ...aggregatedParts.length ? aggregatedParts : []
1555
- ];
1556
- }
1557
- yield {
1558
- type: "end",
1559
- response: {
1560
- message,
1561
- usage,
1562
- finishReason: mapFinishReason(finishReason)
1563
- }
1878
+ }
1879
+ async fetchWorkouts(params, config) {
1880
+ const response = await this.fetchList(params, config);
1881
+ return {
1882
+ workouts: response.items,
1883
+ nextCursor: response.nextCursor,
1884
+ hasMore: response.hasMore,
1885
+ source: this.currentSource()
1564
1886
  };
1565
1887
  }
1566
- async countTokens(_messages) {
1567
- throw new Error("Mistral API does not currently support token counting");
1888
+ async fetchSleep(params, config) {
1889
+ const response = await this.fetchList(params, config);
1890
+ return {
1891
+ sleep: response.items,
1892
+ nextCursor: response.nextCursor,
1893
+ hasMore: response.hasMore,
1894
+ source: this.currentSource()
1895
+ };
1568
1896
  }
1569
- buildChatRequest(messages, options) {
1570
- const model = options.model ?? this.defaultModel;
1571
- const mappedMessages = messages.map((message) => this.toMistralMessage(message));
1572
- const request = {
1573
- model,
1574
- messages: mappedMessages
1897
+ async fetchBiometrics(params, config) {
1898
+ const response = await this.fetchList(params, config);
1899
+ return {
1900
+ biometrics: response.items,
1901
+ nextCursor: response.nextCursor,
1902
+ hasMore: response.hasMore,
1903
+ source: this.currentSource()
1575
1904
  };
1576
- if (options.temperature != null) {
1577
- request.temperature = options.temperature;
1578
- }
1579
- if (options.topP != null) {
1580
- request.topP = options.topP;
1905
+ }
1906
+ async fetchNutrition(params, config) {
1907
+ const response = await this.fetchList(params, config);
1908
+ return {
1909
+ nutrition: response.items,
1910
+ nextCursor: response.nextCursor,
1911
+ hasMore: response.hasMore,
1912
+ source: this.currentSource()
1913
+ };
1914
+ }
1915
+ async fetchConnectionStatus(params, config) {
1916
+ const payload = await this.fetchPayload(config, params);
1917
+ return toHealthConnectionStatus(payload, params, this.currentSource());
1918
+ }
1919
+ currentSource() {
1920
+ return {
1921
+ providerKey: this.providerKey,
1922
+ transport: this.transport,
1923
+ route: this.route,
1924
+ aggregatorKey: this.aggregatorKey
1925
+ };
1926
+ }
1927
+ providerSlug() {
1928
+ return this.providerKey.replace("health.", "").replace(/-/g, "_");
1929
+ }
1930
+ unsupported(capability) {
1931
+ return new HealthProviderCapabilityError(`${this.providerKey} does not support ${capability}`);
1932
+ }
1933
+ async syncFromList(executor) {
1934
+ const result = await executor();
1935
+ const records = countResultRecords(result);
1936
+ return {
1937
+ synced: records,
1938
+ failed: 0,
1939
+ nextCursor: undefined,
1940
+ source: result.source
1941
+ };
1942
+ }
1943
+ async fetchList(params, config) {
1944
+ const payload = await this.fetchPayload(config, params);
1945
+ const items = extractList(payload, config.listKeys).map((item) => config.mapItem(item, params)).filter((item) => Boolean(item));
1946
+ const pagination = extractPagination(payload);
1947
+ return {
1948
+ items,
1949
+ nextCursor: pagination.nextCursor,
1950
+ hasMore: pagination.hasMore
1951
+ };
1952
+ }
1953
+ async fetchPayload(config, params) {
1954
+ const method = config.method ?? "GET";
1955
+ const query = config.buildQuery?.(params);
1956
+ const body = config.buildBody?.(params);
1957
+ if (this.isMcpTransport()) {
1958
+ return this.callMcpTool(config.mcpTool, {
1959
+ ...query ?? {},
1960
+ ...body ?? {}
1961
+ });
1581
1962
  }
1582
- if (options.maxOutputTokens != null) {
1583
- request.maxTokens = options.maxOutputTokens;
1963
+ if (!config.apiPath || !this.apiBaseUrl) {
1964
+ throw new Error(`${this.providerKey} transport is missing an API path.`);
1584
1965
  }
1585
- if (options.stopSequences?.length) {
1586
- request.stop = options.stopSequences.length === 1 ? options.stopSequences[0] : options.stopSequences;
1966
+ if (method === "POST") {
1967
+ return this.requestApi(config.apiPath, "POST", undefined, body);
1587
1968
  }
1588
- if (options.tools?.length) {
1589
- request.tools = options.tools.map((tool) => ({
1590
- type: "function",
1591
- function: {
1592
- name: tool.name,
1969
+ return this.requestApi(config.apiPath, "GET", query, undefined);
1970
+ }
1971
+ isMcpTransport() {
1972
+ return this.transport.endsWith("mcp") || this.transport === "unofficial";
1973
+ }
1974
+ async requestApi(path, method, query, body) {
1975
+ const url = new URL(path, ensureTrailingSlash(this.apiBaseUrl ?? ""));
1976
+ if (query) {
1977
+ for (const [key, value] of Object.entries(query)) {
1978
+ if (value == null)
1979
+ continue;
1980
+ if (Array.isArray(value)) {
1981
+ value.forEach((entry) => {
1982
+ if (entry != null)
1983
+ url.searchParams.append(key, String(entry));
1984
+ });
1985
+ continue;
1986
+ }
1987
+ url.searchParams.set(key, String(value));
1988
+ }
1989
+ }
1990
+ const response = await this.fetchFn(url, {
1991
+ method,
1992
+ headers: this.authorizationHeaders(),
1993
+ body: method === "POST" ? JSON.stringify(body ?? {}) : undefined
1994
+ });
1995
+ if (response.status === 401 && await this.refreshAccessToken()) {
1996
+ const retryResponse = await this.fetchFn(url, {
1997
+ method,
1998
+ headers: this.authorizationHeaders(),
1999
+ body: method === "POST" ? JSON.stringify(body ?? {}) : undefined
2000
+ });
2001
+ return this.readResponsePayload(retryResponse, path);
2002
+ }
2003
+ return this.readResponsePayload(response, path);
2004
+ }
2005
+ async callMcpTool(toolName, args) {
2006
+ if (!this.mcpUrl) {
2007
+ throw new Error(`${this.providerKey} MCP URL is not configured.`);
2008
+ }
2009
+ const response = await this.fetchFn(this.mcpUrl, {
2010
+ method: "POST",
2011
+ headers: {
2012
+ "Content-Type": "application/json",
2013
+ ...this.mcpAccessToken ? { Authorization: `Bearer ${this.mcpAccessToken}` } : {}
2014
+ },
2015
+ body: JSON.stringify({
2016
+ jsonrpc: "2.0",
2017
+ id: ++this.mcpRequestId,
2018
+ method: "tools/call",
2019
+ params: {
2020
+ name: toolName,
2021
+ arguments: args
2022
+ }
2023
+ })
2024
+ });
2025
+ const payload = await this.readResponsePayload(response, toolName);
2026
+ const rpcEnvelope = asRecord(payload);
2027
+ if (!rpcEnvelope)
2028
+ return payload;
2029
+ const rpcResult = asRecord(rpcEnvelope.result);
2030
+ if (rpcResult) {
2031
+ return rpcResult.structuredContent ?? rpcResult.data ?? rpcResult;
2032
+ }
2033
+ return rpcEnvelope.structuredContent ?? rpcEnvelope.data ?? rpcEnvelope;
2034
+ }
2035
+ authorizationHeaders() {
2036
+ const token = this.accessToken ?? this.apiKey;
2037
+ return {
2038
+ "Content-Type": "application/json",
2039
+ ...token ? { Authorization: `Bearer ${token}` } : {}
2040
+ };
2041
+ }
2042
+ async refreshAccessToken() {
2043
+ if (!this.oauth.tokenUrl || !this.refreshToken) {
2044
+ return false;
2045
+ }
2046
+ const tokenUrl = new URL(this.oauth.tokenUrl);
2047
+ const body = new URLSearchParams({
2048
+ grant_type: "refresh_token",
2049
+ refresh_token: this.refreshToken,
2050
+ ...this.oauth.clientId ? { client_id: this.oauth.clientId } : {},
2051
+ ...this.oauth.clientSecret ? { client_secret: this.oauth.clientSecret } : {}
2052
+ });
2053
+ const response = await this.fetchFn(tokenUrl, {
2054
+ method: "POST",
2055
+ headers: {
2056
+ "Content-Type": "application/x-www-form-urlencoded"
2057
+ },
2058
+ body: body.toString()
2059
+ });
2060
+ if (!response.ok) {
2061
+ return false;
2062
+ }
2063
+ const payload = await response.json();
2064
+ this.accessToken = payload.access_token;
2065
+ this.refreshToken = payload.refresh_token ?? this.refreshToken;
2066
+ if (typeof payload.expires_in === "number") {
2067
+ this.oauth.tokenExpiresAt = new Date(Date.now() + payload.expires_in * 1000).toISOString();
2068
+ }
2069
+ return Boolean(this.accessToken);
2070
+ }
2071
+ async readResponsePayload(response, context) {
2072
+ if (!response.ok) {
2073
+ const message = await safeReadText2(response);
2074
+ throw new Error(`${this.providerKey} request ${context} failed (${response.status}): ${message}`);
2075
+ }
2076
+ if (response.status === 204) {
2077
+ return {};
2078
+ }
2079
+ return response.json();
2080
+ }
2081
+ }
2082
+ function readHeader(headers, key) {
2083
+ const target = key.toLowerCase();
2084
+ const entry = Object.entries(headers).find(([headerKey]) => headerKey.toLowerCase() === target);
2085
+ if (!entry)
2086
+ return;
2087
+ const value = entry[1];
2088
+ return Array.isArray(value) ? value[0] : value;
2089
+ }
2090
+ function countResultRecords(result) {
2091
+ const listKeys = [
2092
+ "activities",
2093
+ "workouts",
2094
+ "sleep",
2095
+ "biometrics",
2096
+ "nutrition"
2097
+ ];
2098
+ for (const key of listKeys) {
2099
+ const value = result[key];
2100
+ if (Array.isArray(value)) {
2101
+ return value.length;
2102
+ }
2103
+ }
2104
+ return 0;
2105
+ }
2106
+ function ensureTrailingSlash(value) {
2107
+ return value.endsWith("/") ? value : `${value}/`;
2108
+ }
2109
+ function safeJsonParse(raw) {
2110
+ try {
2111
+ return JSON.parse(raw);
2112
+ } catch {
2113
+ return { rawBody: raw };
2114
+ }
2115
+ }
2116
+ async function safeReadText2(response) {
2117
+ try {
2118
+ return await response.text();
2119
+ } catch {
2120
+ return response.statusText;
2121
+ }
2122
+ }
2123
+
2124
+ // src/impls/health/official-health-providers.ts
2125
+ function buildSharedQuery(params) {
2126
+ return {
2127
+ tenantId: params.tenantId,
2128
+ connectionId: params.connectionId,
2129
+ userId: params.userId,
2130
+ from: params.from,
2131
+ to: params.to,
2132
+ cursor: params.cursor,
2133
+ pageSize: params.pageSize
2134
+ };
2135
+ }
2136
+ function withMetricTypes(params) {
2137
+ return {
2138
+ ...buildSharedQuery(params),
2139
+ metricTypes: params.metricTypes
2140
+ };
2141
+ }
2142
+
2143
+ class OpenWearablesHealthProvider extends BaseHealthProvider {
2144
+ upstreamProvider;
2145
+ constructor(options) {
2146
+ super({
2147
+ providerKey: options.providerKey ?? "health.openwearables",
2148
+ apiBaseUrl: options.apiBaseUrl ?? "https://api.openwearables.io",
2149
+ webhookSignatureHeader: "x-openwearables-signature",
2150
+ ...options
2151
+ });
2152
+ this.upstreamProvider = options.upstreamProvider;
2153
+ }
2154
+ async listActivities(params) {
2155
+ return this.fetchActivities(params, {
2156
+ apiPath: "/v1/activities",
2157
+ mcpTool: "openwearables_list_activities",
2158
+ buildQuery: (input) => this.withUpstreamProvider(buildSharedQuery(input)),
2159
+ mapItem: (item, input) => toHealthActivity(item, this.context(input), "activity")
2160
+ });
2161
+ }
2162
+ async listWorkouts(params) {
2163
+ return this.fetchWorkouts(params, {
2164
+ apiPath: "/v1/workouts",
2165
+ mcpTool: "openwearables_list_workouts",
2166
+ buildQuery: (input) => this.withUpstreamProvider(buildSharedQuery(input)),
2167
+ mapItem: (item, input) => toHealthWorkout(item, this.context(input))
2168
+ });
2169
+ }
2170
+ async listSleep(params) {
2171
+ return this.fetchSleep(params, {
2172
+ apiPath: "/v1/sleep",
2173
+ mcpTool: "openwearables_list_sleep",
2174
+ buildQuery: (input) => this.withUpstreamProvider(buildSharedQuery(input)),
2175
+ mapItem: (item, input) => toHealthSleep(item, this.context(input))
2176
+ });
2177
+ }
2178
+ async listBiometrics(params) {
2179
+ return this.fetchBiometrics(params, {
2180
+ apiPath: "/v1/biometrics",
2181
+ mcpTool: "openwearables_list_biometrics",
2182
+ buildQuery: (input) => this.withUpstreamProvider(withMetricTypes(input)),
2183
+ mapItem: (item, input) => toHealthBiometric(item, this.context(input))
2184
+ });
2185
+ }
2186
+ async listNutrition(params) {
2187
+ return this.fetchNutrition(params, {
2188
+ apiPath: "/v1/nutrition",
2189
+ mcpTool: "openwearables_list_nutrition",
2190
+ buildQuery: (input) => this.withUpstreamProvider(buildSharedQuery(input)),
2191
+ mapItem: (item, input) => toHealthNutrition(item, this.context(input))
2192
+ });
2193
+ }
2194
+ async getConnectionStatus(params) {
2195
+ return this.fetchConnectionStatus(params, {
2196
+ apiPath: `/v1/connections/${encodeURIComponent(params.connectionId)}/status`,
2197
+ mcpTool: "openwearables_connection_status"
2198
+ });
2199
+ }
2200
+ withUpstreamProvider(query) {
2201
+ return {
2202
+ ...query,
2203
+ ...this.upstreamProvider ? { upstreamProvider: this.upstreamProvider } : {}
2204
+ };
2205
+ }
2206
+ context(params) {
2207
+ return {
2208
+ tenantId: params.tenantId,
2209
+ connectionId: params.connectionId,
2210
+ providerKey: this.providerKey
2211
+ };
2212
+ }
2213
+ }
2214
+
2215
+ class AppleHealthBridgeProvider extends OpenWearablesHealthProvider {
2216
+ constructor(options) {
2217
+ super({
2218
+ ...options,
2219
+ providerKey: "health.apple-health",
2220
+ upstreamProvider: "apple-health"
2221
+ });
2222
+ }
2223
+ }
2224
+
2225
+ class WhoopHealthProvider extends BaseHealthProvider {
2226
+ constructor(options) {
2227
+ super({
2228
+ providerKey: "health.whoop",
2229
+ apiBaseUrl: options.apiBaseUrl ?? "https://api.prod.whoop.com",
2230
+ webhookSignatureHeader: "x-whoop-signature",
2231
+ oauth: {
2232
+ tokenUrl: options.oauth?.tokenUrl ?? "https://api.prod.whoop.com/oauth/oauth2/token",
2233
+ ...options.oauth
2234
+ },
2235
+ ...options
2236
+ });
2237
+ }
2238
+ async listActivities(params) {
2239
+ return this.fetchActivities(params, {
2240
+ apiPath: "/v2/activity/workout",
2241
+ mcpTool: "whoop_list_activities",
2242
+ buildQuery: buildSharedQuery,
2243
+ mapItem: (item, input) => toHealthActivity(item, this.context(input), "workout")
2244
+ });
2245
+ }
2246
+ async listWorkouts(params) {
2247
+ return this.fetchWorkouts(params, {
2248
+ apiPath: "/v2/activity/workout",
2249
+ mcpTool: "whoop_list_workouts",
2250
+ buildQuery: buildSharedQuery,
2251
+ mapItem: (item, input) => toHealthWorkout(item, this.context(input))
2252
+ });
2253
+ }
2254
+ async listSleep(params) {
2255
+ return this.fetchSleep(params, {
2256
+ apiPath: "/v2/activity/sleep",
2257
+ mcpTool: "whoop_list_sleep",
2258
+ buildQuery: buildSharedQuery,
2259
+ mapItem: (item, input) => toHealthSleep(item, this.context(input))
2260
+ });
2261
+ }
2262
+ async listBiometrics(params) {
2263
+ return this.fetchBiometrics(params, {
2264
+ apiPath: "/v2/recovery",
2265
+ mcpTool: "whoop_list_biometrics",
2266
+ buildQuery: withMetricTypes,
2267
+ mapItem: (item, input) => toHealthBiometric(item, this.context(input), "recovery_score")
2268
+ });
2269
+ }
2270
+ async listNutrition(_params) {
2271
+ throw this.unsupported("nutrition");
2272
+ }
2273
+ async getConnectionStatus(params) {
2274
+ return this.fetchConnectionStatus(params, {
2275
+ apiPath: "/v2/user/profile/basic",
2276
+ mcpTool: "whoop_connection_status"
2277
+ });
2278
+ }
2279
+ context(params) {
2280
+ return {
2281
+ tenantId: params.tenantId,
2282
+ connectionId: params.connectionId,
2283
+ providerKey: this.providerKey
2284
+ };
2285
+ }
2286
+ }
2287
+
2288
+ class OuraHealthProvider extends BaseHealthProvider {
2289
+ constructor(options) {
2290
+ super({
2291
+ providerKey: "health.oura",
2292
+ apiBaseUrl: options.apiBaseUrl ?? "https://api.ouraring.com",
2293
+ webhookSignatureHeader: "x-oura-signature",
2294
+ oauth: {
2295
+ tokenUrl: options.oauth?.tokenUrl ?? "https://api.ouraring.com/oauth/token",
2296
+ ...options.oauth
2297
+ },
2298
+ ...options
2299
+ });
2300
+ }
2301
+ async listActivities(params) {
2302
+ return this.fetchActivities(params, {
2303
+ apiPath: "/v2/usercollection/daily_activity",
2304
+ mcpTool: "oura_list_activities",
2305
+ buildQuery: buildSharedQuery,
2306
+ mapItem: (item, input) => toHealthActivity(item, this.context(input))
2307
+ });
2308
+ }
2309
+ async listWorkouts(params) {
2310
+ return this.fetchWorkouts(params, {
2311
+ apiPath: "/v2/usercollection/workout",
2312
+ mcpTool: "oura_list_workouts",
2313
+ buildQuery: buildSharedQuery,
2314
+ mapItem: (item, input) => toHealthWorkout(item, this.context(input))
2315
+ });
2316
+ }
2317
+ async listSleep(params) {
2318
+ return this.fetchSleep(params, {
2319
+ apiPath: "/v2/usercollection/sleep",
2320
+ mcpTool: "oura_list_sleep",
2321
+ buildQuery: buildSharedQuery,
2322
+ mapItem: (item, input) => toHealthSleep(item, this.context(input))
2323
+ });
2324
+ }
2325
+ async listBiometrics(params) {
2326
+ return this.fetchBiometrics(params, {
2327
+ apiPath: "/v2/usercollection/daily_readiness",
2328
+ mcpTool: "oura_list_biometrics",
2329
+ buildQuery: withMetricTypes,
2330
+ mapItem: (item, input) => toHealthBiometric(item, this.context(input), "readiness_score")
2331
+ });
2332
+ }
2333
+ async listNutrition(_params) {
2334
+ throw this.unsupported("nutrition");
2335
+ }
2336
+ async getConnectionStatus(params) {
2337
+ return this.fetchConnectionStatus(params, {
2338
+ apiPath: "/v2/usercollection/personal_info",
2339
+ mcpTool: "oura_connection_status"
2340
+ });
2341
+ }
2342
+ context(params) {
2343
+ return {
2344
+ tenantId: params.tenantId,
2345
+ connectionId: params.connectionId,
2346
+ providerKey: this.providerKey
2347
+ };
2348
+ }
2349
+ }
2350
+
2351
+ class StravaHealthProvider extends BaseHealthProvider {
2352
+ constructor(options) {
2353
+ super({
2354
+ providerKey: "health.strava",
2355
+ apiBaseUrl: options.apiBaseUrl ?? "https://www.strava.com",
2356
+ webhookSignatureHeader: "x-strava-signature",
2357
+ oauth: {
2358
+ tokenUrl: options.oauth?.tokenUrl ?? "https://www.strava.com/oauth/token",
2359
+ ...options.oauth
2360
+ },
2361
+ ...options
2362
+ });
2363
+ }
2364
+ async listActivities(params) {
2365
+ return this.fetchActivities(params, {
2366
+ apiPath: "/api/v3/athlete/activities",
2367
+ mcpTool: "strava_list_activities",
2368
+ buildQuery: buildSharedQuery,
2369
+ mapItem: (item, input) => toHealthActivity(item, this.context(input))
2370
+ });
2371
+ }
2372
+ async listWorkouts(params) {
2373
+ return this.fetchWorkouts(params, {
2374
+ apiPath: "/api/v3/athlete/activities",
2375
+ mcpTool: "strava_list_workouts",
2376
+ buildQuery: buildSharedQuery,
2377
+ mapItem: (item, input) => toHealthWorkout(item, this.context(input))
2378
+ });
2379
+ }
2380
+ async listSleep(_params) {
2381
+ throw this.unsupported("sleep");
2382
+ }
2383
+ async listBiometrics(_params) {
2384
+ throw this.unsupported("biometrics");
2385
+ }
2386
+ async listNutrition(_params) {
2387
+ throw this.unsupported("nutrition");
2388
+ }
2389
+ async getConnectionStatus(params) {
2390
+ return this.fetchConnectionStatus(params, {
2391
+ apiPath: "/api/v3/athlete",
2392
+ mcpTool: "strava_connection_status"
2393
+ });
2394
+ }
2395
+ context(params) {
2396
+ return {
2397
+ tenantId: params.tenantId,
2398
+ connectionId: params.connectionId,
2399
+ providerKey: this.providerKey
2400
+ };
2401
+ }
2402
+ }
2403
+
2404
+ class FitbitHealthProvider extends BaseHealthProvider {
2405
+ constructor(options) {
2406
+ super({
2407
+ providerKey: "health.fitbit",
2408
+ apiBaseUrl: options.apiBaseUrl ?? "https://api.fitbit.com",
2409
+ webhookSignatureHeader: "x-fitbit-signature",
2410
+ oauth: {
2411
+ tokenUrl: options.oauth?.tokenUrl ?? "https://api.fitbit.com/oauth2/token",
2412
+ ...options.oauth
2413
+ },
2414
+ ...options
2415
+ });
2416
+ }
2417
+ async listActivities(params) {
2418
+ return this.fetchActivities(params, {
2419
+ apiPath: "/1/user/-/activities/list.json",
2420
+ mcpTool: "fitbit_list_activities",
2421
+ buildQuery: buildSharedQuery,
2422
+ mapItem: (item, input) => toHealthActivity(item, this.context(input))
2423
+ });
2424
+ }
2425
+ async listWorkouts(params) {
2426
+ return this.fetchWorkouts(params, {
2427
+ apiPath: "/1/user/-/activities/list.json",
2428
+ mcpTool: "fitbit_list_workouts",
2429
+ buildQuery: buildSharedQuery,
2430
+ mapItem: (item, input) => toHealthWorkout(item, this.context(input))
2431
+ });
2432
+ }
2433
+ async listSleep(params) {
2434
+ return this.fetchSleep(params, {
2435
+ apiPath: "/1.2/user/-/sleep/list.json",
2436
+ mcpTool: "fitbit_list_sleep",
2437
+ buildQuery: buildSharedQuery,
2438
+ mapItem: (item, input) => toHealthSleep(item, this.context(input))
2439
+ });
2440
+ }
2441
+ async listBiometrics(params) {
2442
+ return this.fetchBiometrics(params, {
2443
+ apiPath: "/1/user/-/body/log/weight/date/today/1m.json",
2444
+ mcpTool: "fitbit_list_biometrics",
2445
+ buildQuery: withMetricTypes,
2446
+ mapItem: (item, input) => toHealthBiometric(item, this.context(input), "weight")
2447
+ });
2448
+ }
2449
+ async listNutrition(params) {
2450
+ return this.fetchNutrition(params, {
2451
+ apiPath: "/1/user/-/foods/log/date/today.json",
2452
+ mcpTool: "fitbit_list_nutrition",
2453
+ buildQuery: buildSharedQuery,
2454
+ mapItem: (item, input) => toHealthNutrition(item, this.context(input))
2455
+ });
2456
+ }
2457
+ async getConnectionStatus(params) {
2458
+ return this.fetchConnectionStatus(params, {
2459
+ apiPath: "/1/user/-/profile.json",
2460
+ mcpTool: "fitbit_connection_status"
2461
+ });
2462
+ }
2463
+ context(params) {
2464
+ return {
2465
+ tenantId: params.tenantId,
2466
+ connectionId: params.connectionId,
2467
+ providerKey: this.providerKey
2468
+ };
2469
+ }
2470
+ }
2471
+
2472
+ // src/impls/health/hybrid-health-providers.ts
2473
+ var LIMITED_PROVIDER_SLUG = {
2474
+ "health.garmin": "garmin",
2475
+ "health.myfitnesspal": "myfitnesspal",
2476
+ "health.eightsleep": "eightsleep",
2477
+ "health.peloton": "peloton"
2478
+ };
2479
+ function buildSharedQuery2(params) {
2480
+ return {
2481
+ tenantId: params.tenantId,
2482
+ connectionId: params.connectionId,
2483
+ userId: params.userId,
2484
+ from: params.from,
2485
+ to: params.to,
2486
+ cursor: params.cursor,
2487
+ pageSize: params.pageSize
2488
+ };
2489
+ }
2490
+
2491
+ class GarminHealthProvider extends OpenWearablesHealthProvider {
2492
+ constructor(options) {
2493
+ super({
2494
+ ...options,
2495
+ providerKey: "health.garmin",
2496
+ upstreamProvider: "garmin"
2497
+ });
2498
+ }
2499
+ }
2500
+
2501
+ class MyFitnessPalHealthProvider extends OpenWearablesHealthProvider {
2502
+ constructor(options) {
2503
+ super({
2504
+ ...options,
2505
+ providerKey: "health.myfitnesspal",
2506
+ upstreamProvider: "myfitnesspal"
2507
+ });
2508
+ }
2509
+ }
2510
+
2511
+ class EightSleepHealthProvider extends OpenWearablesHealthProvider {
2512
+ constructor(options) {
2513
+ super({
2514
+ ...options,
2515
+ providerKey: "health.eightsleep",
2516
+ upstreamProvider: "eightsleep"
2517
+ });
2518
+ }
2519
+ }
2520
+
2521
+ class PelotonHealthProvider extends OpenWearablesHealthProvider {
2522
+ constructor(options) {
2523
+ super({
2524
+ ...options,
2525
+ providerKey: "health.peloton",
2526
+ upstreamProvider: "peloton"
2527
+ });
2528
+ }
2529
+ }
2530
+
2531
+ class UnofficialHealthAutomationProvider extends BaseHealthProvider {
2532
+ providerSlugValue;
2533
+ constructor(options) {
2534
+ super({
2535
+ ...options,
2536
+ providerKey: options.providerKey,
2537
+ webhookSignatureHeader: "x-unofficial-signature"
2538
+ });
2539
+ this.providerSlugValue = LIMITED_PROVIDER_SLUG[options.providerKey];
2540
+ }
2541
+ async listActivities(params) {
2542
+ return this.fetchActivities(params, {
2543
+ mcpTool: `${this.providerSlugValue}_list_activities`,
2544
+ buildQuery: buildSharedQuery2,
2545
+ mapItem: (item, input) => toHealthActivity(item, this.context(input), "activity")
2546
+ });
2547
+ }
2548
+ async listWorkouts(params) {
2549
+ return this.fetchWorkouts(params, {
2550
+ mcpTool: `${this.providerSlugValue}_list_workouts`,
2551
+ buildQuery: buildSharedQuery2,
2552
+ mapItem: (item, input) => toHealthWorkout(item, this.context(input))
2553
+ });
2554
+ }
2555
+ async listSleep(params) {
2556
+ return this.fetchSleep(params, {
2557
+ mcpTool: `${this.providerSlugValue}_list_sleep`,
2558
+ buildQuery: buildSharedQuery2,
2559
+ mapItem: (item, input) => toHealthSleep(item, this.context(input))
2560
+ });
2561
+ }
2562
+ async listBiometrics(params) {
2563
+ return this.fetchBiometrics(params, {
2564
+ mcpTool: `${this.providerSlugValue}_list_biometrics`,
2565
+ buildQuery: (input) => ({
2566
+ ...buildSharedQuery2(input),
2567
+ metricTypes: input.metricTypes
2568
+ }),
2569
+ mapItem: (item, input) => toHealthBiometric(item, this.context(input))
2570
+ });
2571
+ }
2572
+ async listNutrition(params) {
2573
+ return this.fetchNutrition(params, {
2574
+ mcpTool: `${this.providerSlugValue}_list_nutrition`,
2575
+ buildQuery: buildSharedQuery2,
2576
+ mapItem: (item, input) => toHealthNutrition(item, this.context(input))
2577
+ });
2578
+ }
2579
+ async getConnectionStatus(params) {
2580
+ return this.fetchConnectionStatus(params, {
2581
+ mcpTool: `${this.providerSlugValue}_connection_status`
2582
+ });
2583
+ }
2584
+ context(params) {
2585
+ return {
2586
+ tenantId: params.tenantId,
2587
+ connectionId: params.connectionId,
2588
+ providerKey: this.providerKey
2589
+ };
2590
+ }
2591
+ }
2592
+ // src/impls/health-provider-factory.ts
2593
+ import {
2594
+ isUnofficialHealthProviderAllowed,
2595
+ resolveHealthStrategyOrder
2596
+ } from "@contractspec/integration.runtime/runtime";
2597
+ var OFFICIAL_TRANSPORT_SUPPORTED_BY_PROVIDER = {
2598
+ "health.openwearables": false,
2599
+ "health.whoop": true,
2600
+ "health.apple-health": false,
2601
+ "health.oura": true,
2602
+ "health.strava": true,
2603
+ "health.garmin": false,
2604
+ "health.fitbit": true,
2605
+ "health.myfitnesspal": false,
2606
+ "health.eightsleep": false,
2607
+ "health.peloton": false
2608
+ };
2609
+ var UNOFFICIAL_SUPPORTED_BY_PROVIDER = {
2610
+ "health.openwearables": false,
2611
+ "health.whoop": false,
2612
+ "health.apple-health": false,
2613
+ "health.oura": false,
2614
+ "health.strava": false,
2615
+ "health.garmin": true,
2616
+ "health.fitbit": false,
2617
+ "health.myfitnesspal": true,
2618
+ "health.eightsleep": true,
2619
+ "health.peloton": true
2620
+ };
2621
+ function createHealthProviderFromContext(context, secrets) {
2622
+ const providerKey = context.spec.meta.key;
2623
+ const config = toFactoryConfig(context.config);
2624
+ const strategyOrder = buildStrategyOrder(config);
2625
+ const attemptLogs = [];
2626
+ for (let index = 0;index < strategyOrder.length; index += 1) {
2627
+ const strategy = strategyOrder[index];
2628
+ if (!strategy)
2629
+ continue;
2630
+ const route = index === 0 ? "primary" : "fallback";
2631
+ if (!supportsStrategy(providerKey, strategy)) {
2632
+ attemptLogs.push(`${strategy}: unsupported by ${providerKey}`);
2633
+ continue;
2634
+ }
2635
+ if (!hasCredentialsForStrategy(strategy, config, secrets)) {
2636
+ attemptLogs.push(`${strategy}: missing credentials`);
2637
+ continue;
2638
+ }
2639
+ const provider = createHealthProviderForStrategy(providerKey, strategy, route, config, secrets);
2640
+ if (provider) {
2641
+ return provider;
2642
+ }
2643
+ attemptLogs.push(`${strategy}: not available`);
2644
+ }
2645
+ throw new Error(`Unable to resolve health provider for ${providerKey}. Strategies attempted: ${attemptLogs.join(", ")}.`);
2646
+ }
2647
+ function createHealthProviderForStrategy(providerKey, strategy, route, config, secrets) {
2648
+ const options = {
2649
+ transport: strategy,
2650
+ apiBaseUrl: config.apiBaseUrl,
2651
+ mcpUrl: config.mcpUrl,
2652
+ apiKey: getSecretString(secrets, "apiKey"),
2653
+ accessToken: getSecretString(secrets, "accessToken"),
2654
+ mcpAccessToken: getSecretString(secrets, "mcpAccessToken"),
2655
+ webhookSecret: getSecretString(secrets, "webhookSecret"),
2656
+ route,
2657
+ oauth: {
2658
+ tokenUrl: config.oauthTokenUrl,
2659
+ refreshToken: getSecretString(secrets, "refreshToken"),
2660
+ clientId: getSecretString(secrets, "clientId"),
2661
+ clientSecret: getSecretString(secrets, "clientSecret"),
2662
+ tokenExpiresAt: getSecretString(secrets, "tokenExpiresAt")
2663
+ }
2664
+ };
2665
+ if (strategy === "aggregator-api" || strategy === "aggregator-mcp") {
2666
+ return createAggregatorProvider(providerKey, {
2667
+ ...options,
2668
+ aggregatorKey: "health.openwearables"
2669
+ });
2670
+ }
2671
+ if (strategy === "unofficial") {
2672
+ if (!isUnofficialHealthProviderAllowed(providerKey, config)) {
2673
+ return;
2674
+ }
2675
+ if (providerKey !== "health.myfitnesspal" && providerKey !== "health.eightsleep" && providerKey !== "health.peloton" && providerKey !== "health.garmin") {
2676
+ return;
2677
+ }
2678
+ return new UnofficialHealthAutomationProvider({
2679
+ ...options,
2680
+ providerKey
2681
+ });
2682
+ }
2683
+ if (strategy === "official-mcp") {
2684
+ return createOfficialProvider(providerKey, {
2685
+ ...options,
2686
+ transport: "official-mcp"
2687
+ });
2688
+ }
2689
+ return createOfficialProvider(providerKey, options);
2690
+ }
2691
+ function createAggregatorProvider(providerKey, options) {
2692
+ if (providerKey === "health.apple-health") {
2693
+ return new AppleHealthBridgeProvider(options);
2694
+ }
2695
+ if (providerKey === "health.garmin") {
2696
+ return new GarminHealthProvider(options);
2697
+ }
2698
+ if (providerKey === "health.myfitnesspal") {
2699
+ return new MyFitnessPalHealthProvider(options);
2700
+ }
2701
+ if (providerKey === "health.eightsleep") {
2702
+ return new EightSleepHealthProvider(options);
2703
+ }
2704
+ if (providerKey === "health.peloton") {
2705
+ return new PelotonHealthProvider(options);
2706
+ }
2707
+ if (providerKey === "health.openwearables") {
2708
+ return new OpenWearablesHealthProvider(options);
2709
+ }
2710
+ return new OpenWearablesHealthProvider({
2711
+ ...options,
2712
+ providerKey,
2713
+ upstreamProvider: providerKey.replace("health.", "")
2714
+ });
2715
+ }
2716
+ function createOfficialProvider(providerKey, options) {
2717
+ switch (providerKey) {
2718
+ case "health.openwearables":
2719
+ return new OpenWearablesHealthProvider(options);
2720
+ case "health.whoop":
2721
+ return new WhoopHealthProvider(options);
2722
+ case "health.apple-health":
2723
+ return new AppleHealthBridgeProvider(options);
2724
+ case "health.oura":
2725
+ return new OuraHealthProvider(options);
2726
+ case "health.strava":
2727
+ return new StravaHealthProvider(options);
2728
+ case "health.garmin":
2729
+ return new GarminHealthProvider(options);
2730
+ case "health.fitbit":
2731
+ return new FitbitHealthProvider(options);
2732
+ case "health.myfitnesspal":
2733
+ return new MyFitnessPalHealthProvider({
2734
+ ...options,
2735
+ transport: "aggregator-api"
2736
+ });
2737
+ case "health.eightsleep":
2738
+ return new EightSleepHealthProvider({
2739
+ ...options,
2740
+ transport: "aggregator-api"
2741
+ });
2742
+ case "health.peloton":
2743
+ return new PelotonHealthProvider({
2744
+ ...options,
2745
+ transport: "aggregator-api"
2746
+ });
2747
+ default:
2748
+ throw new Error(`Unsupported health provider key: ${providerKey}`);
2749
+ }
2750
+ }
2751
+ function toFactoryConfig(config) {
2752
+ if (!config || typeof config !== "object" || Array.isArray(config)) {
2753
+ return {};
2754
+ }
2755
+ const record = config;
2756
+ return {
2757
+ apiBaseUrl: asString(record.apiBaseUrl),
2758
+ mcpUrl: asString(record.mcpUrl),
2759
+ oauthTokenUrl: asString(record.oauthTokenUrl),
2760
+ defaultTransport: normalizeTransport(record.defaultTransport),
2761
+ strategyOrder: normalizeTransportArray(record.strategyOrder),
2762
+ allowUnofficial: typeof record.allowUnofficial === "boolean" ? record.allowUnofficial : false,
2763
+ unofficialAllowList: Array.isArray(record.unofficialAllowList) ? record.unofficialAllowList.map((item) => typeof item === "string" ? item : undefined).filter((item) => Boolean(item)) : undefined
2764
+ };
2765
+ }
2766
+ function buildStrategyOrder(config) {
2767
+ const order = resolveHealthStrategyOrder(config);
2768
+ if (!config.defaultTransport) {
2769
+ return order;
2770
+ }
2771
+ const withoutDefault = order.filter((item) => item !== config.defaultTransport);
2772
+ return [config.defaultTransport, ...withoutDefault];
2773
+ }
2774
+ function normalizeTransport(value) {
2775
+ if (typeof value !== "string")
2776
+ return;
2777
+ if (value === "official-api" || value === "official-mcp" || value === "aggregator-api" || value === "aggregator-mcp" || value === "unofficial") {
2778
+ return value;
2779
+ }
2780
+ return;
2781
+ }
2782
+ function normalizeTransportArray(value) {
2783
+ if (!Array.isArray(value))
2784
+ return;
2785
+ const transports = value.map((item) => normalizeTransport(item)).filter((item) => Boolean(item));
2786
+ return transports.length > 0 ? transports : undefined;
2787
+ }
2788
+ function supportsStrategy(providerKey, strategy) {
2789
+ if (strategy === "official-api" || strategy === "official-mcp") {
2790
+ return OFFICIAL_TRANSPORT_SUPPORTED_BY_PROVIDER[providerKey];
2791
+ }
2792
+ if (strategy === "unofficial") {
2793
+ return UNOFFICIAL_SUPPORTED_BY_PROVIDER[providerKey];
2794
+ }
2795
+ return true;
2796
+ }
2797
+ function hasCredentialsForStrategy(strategy, config, secrets) {
2798
+ const hasApiCredential = Boolean(getSecretString(secrets, "accessToken")) || Boolean(getSecretString(secrets, "apiKey"));
2799
+ const hasMcpCredential = Boolean(getSecretString(secrets, "mcpAccessToken")) || hasApiCredential;
2800
+ if (strategy === "official-api" || strategy === "aggregator-api") {
2801
+ return hasApiCredential;
2802
+ }
2803
+ if (strategy === "official-mcp" || strategy === "aggregator-mcp") {
2804
+ return Boolean(config.mcpUrl) && hasMcpCredential;
2805
+ }
2806
+ const hasAutomationCredential = hasMcpCredential || Boolean(getSecretString(secrets, "username")) && Boolean(getSecretString(secrets, "password"));
2807
+ return Boolean(config.mcpUrl) && hasAutomationCredential;
2808
+ }
2809
+ function getSecretString(secrets, key) {
2810
+ const value = secrets[key];
2811
+ return typeof value === "string" && value.trim().length > 0 ? value : undefined;
2812
+ }
2813
+ function asString(value) {
2814
+ return typeof value === "string" && value.trim().length > 0 ? value : undefined;
2815
+ }
2816
+
2817
+ // src/impls/mistral-llm.ts
2818
+ import { Mistral } from "@mistralai/mistralai";
2819
+
2820
+ class MistralLLMProvider {
2821
+ client;
2822
+ defaultModel;
2823
+ constructor(options) {
2824
+ if (!options.apiKey) {
2825
+ throw new Error("MistralLLMProvider requires an apiKey");
2826
+ }
2827
+ this.client = options.client ?? new Mistral({
2828
+ apiKey: options.apiKey,
2829
+ serverURL: options.serverURL,
2830
+ userAgent: options.userAgentSuffix ? `${options.userAgentSuffix}` : undefined
2831
+ });
2832
+ this.defaultModel = options.defaultModel ?? "mistral-large-latest";
2833
+ }
2834
+ async chat(messages, options = {}) {
2835
+ const request = this.buildChatRequest(messages, options);
2836
+ const response = await this.client.chat.complete(request);
2837
+ return this.buildLLMResponse(response);
2838
+ }
2839
+ async* stream(messages, options = {}) {
2840
+ const request = this.buildChatRequest(messages, options);
2841
+ request.stream = true;
2842
+ const stream = await this.client.chat.stream(request);
2843
+ const aggregatedParts = [];
2844
+ const aggregatedToolCalls = [];
2845
+ let usage;
2846
+ let finishReason;
2847
+ for await (const event of stream) {
2848
+ for (const choice of event.data.choices) {
2849
+ const delta = choice.delta;
2850
+ if (typeof delta.content === "string") {
2851
+ if (delta.content.length > 0) {
2852
+ aggregatedParts.push({ type: "text", text: delta.content });
2853
+ yield {
2854
+ type: "message_delta",
2855
+ delta: { type: "text", text: delta.content },
2856
+ index: choice.index
2857
+ };
2858
+ }
2859
+ } else if (Array.isArray(delta.content)) {
2860
+ for (const chunk of delta.content) {
2861
+ if (chunk.type === "text" && "text" in chunk) {
2862
+ aggregatedParts.push({ type: "text", text: chunk.text });
2863
+ yield {
2864
+ type: "message_delta",
2865
+ delta: { type: "text", text: chunk.text },
2866
+ index: choice.index
2867
+ };
2868
+ }
2869
+ }
2870
+ }
2871
+ if (delta.toolCalls) {
2872
+ let localIndex = 0;
2873
+ for (const call of delta.toolCalls) {
2874
+ const toolCall = this.fromMistralToolCall(call, localIndex);
2875
+ aggregatedToolCalls.push(toolCall);
2876
+ yield {
2877
+ type: "tool_call",
2878
+ call: toolCall,
2879
+ index: choice.index
2880
+ };
2881
+ localIndex += 1;
2882
+ }
2883
+ }
2884
+ if (choice.finishReason && choice.finishReason !== "null") {
2885
+ finishReason = choice.finishReason;
2886
+ }
2887
+ }
2888
+ if (event.data.usage) {
2889
+ const usageEntry = this.fromUsage(event.data.usage);
2890
+ if (usageEntry) {
2891
+ usage = usageEntry;
2892
+ yield { type: "usage", usage: usageEntry };
2893
+ }
2894
+ }
2895
+ }
2896
+ const message = {
2897
+ role: "assistant",
2898
+ content: aggregatedParts.length ? aggregatedParts : [{ type: "text", text: "" }]
2899
+ };
2900
+ if (aggregatedToolCalls.length > 0) {
2901
+ message.content = [
2902
+ ...aggregatedToolCalls,
2903
+ ...aggregatedParts.length ? aggregatedParts : []
2904
+ ];
2905
+ }
2906
+ yield {
2907
+ type: "end",
2908
+ response: {
2909
+ message,
2910
+ usage,
2911
+ finishReason: mapFinishReason(finishReason)
2912
+ }
2913
+ };
2914
+ }
2915
+ async countTokens(_messages) {
2916
+ throw new Error("Mistral API does not currently support token counting");
2917
+ }
2918
+ buildChatRequest(messages, options) {
2919
+ const model = options.model ?? this.defaultModel;
2920
+ const mappedMessages = messages.map((message) => this.toMistralMessage(message));
2921
+ const request = {
2922
+ model,
2923
+ messages: mappedMessages
2924
+ };
2925
+ if (options.temperature != null) {
2926
+ request.temperature = options.temperature;
2927
+ }
2928
+ if (options.topP != null) {
2929
+ request.topP = options.topP;
2930
+ }
2931
+ if (options.maxOutputTokens != null) {
2932
+ request.maxTokens = options.maxOutputTokens;
2933
+ }
2934
+ if (options.stopSequences?.length) {
2935
+ request.stop = options.stopSequences.length === 1 ? options.stopSequences[0] : options.stopSequences;
2936
+ }
2937
+ if (options.tools?.length) {
2938
+ request.tools = options.tools.map((tool) => ({
2939
+ type: "function",
2940
+ function: {
2941
+ name: tool.name,
1593
2942
  description: tool.description,
1594
2943
  parameters: typeof tool.inputSchema === "object" && tool.inputSchema !== null ? tool.inputSchema : {}
1595
2944
  }
@@ -1700,77 +3049,506 @@ class MistralLLMProvider {
1700
3049
  return null;
1701
3050
  return textParts.join("");
1702
3051
  }
1703
- extractToolCalls(message) {
1704
- const toolCallParts = message.content.filter((part) => part.type === "tool-call");
1705
- return toolCallParts.map((call, index) => ({
1706
- id: call.id ?? `call_${index}`,
1707
- type: "function",
1708
- index,
1709
- function: {
1710
- name: call.name,
1711
- arguments: call.arguments
1712
- }
1713
- }));
3052
+ extractToolCalls(message) {
3053
+ const toolCallParts = message.content.filter((part) => part.type === "tool-call");
3054
+ return toolCallParts.map((call, index) => ({
3055
+ id: call.id ?? `call_${index}`,
3056
+ type: "function",
3057
+ index,
3058
+ function: {
3059
+ name: call.name,
3060
+ arguments: call.arguments
3061
+ }
3062
+ }));
3063
+ }
3064
+ }
3065
+ function mapFinishReason(reason) {
3066
+ if (!reason)
3067
+ return;
3068
+ const normalized = reason.toLowerCase();
3069
+ switch (normalized) {
3070
+ case "stop":
3071
+ return "stop";
3072
+ case "length":
3073
+ return "length";
3074
+ case "tool_call":
3075
+ case "tool_calls":
3076
+ return "tool_call";
3077
+ case "content_filter":
3078
+ return "content_filter";
3079
+ default:
3080
+ return;
3081
+ }
3082
+ }
3083
+
3084
+ // src/impls/mistral-embedding.ts
3085
+ import { Mistral as Mistral2 } from "@mistralai/mistralai";
3086
+
3087
+ class MistralEmbeddingProvider {
3088
+ client;
3089
+ defaultModel;
3090
+ constructor(options) {
3091
+ if (!options.apiKey) {
3092
+ throw new Error("MistralEmbeddingProvider requires an apiKey");
3093
+ }
3094
+ this.client = options.client ?? new Mistral2({
3095
+ apiKey: options.apiKey,
3096
+ serverURL: options.serverURL
3097
+ });
3098
+ this.defaultModel = options.defaultModel ?? "mistral-embed";
3099
+ }
3100
+ async embedDocuments(documents, options) {
3101
+ if (documents.length === 0)
3102
+ return [];
3103
+ const model = options?.model ?? this.defaultModel;
3104
+ const response = await this.client.embeddings.create({
3105
+ model,
3106
+ inputs: documents.map((doc) => doc.text)
3107
+ });
3108
+ return response.data.map((item, index) => ({
3109
+ id: documents[index]?.id ?? (item.index != null ? `embedding-${item.index}` : `embedding-${index}`),
3110
+ vector: item.embedding ?? [],
3111
+ dimensions: item.embedding?.length ?? 0,
3112
+ model: response.model,
3113
+ metadata: documents[index]?.metadata ? Object.fromEntries(Object.entries(documents[index]?.metadata ?? {}).map(([key, value]) => [key, String(value)])) : undefined
3114
+ }));
3115
+ }
3116
+ async embedQuery(query, options) {
3117
+ const [result] = await this.embedDocuments([{ id: "query", text: query }], options);
3118
+ if (!result) {
3119
+ throw new Error("Failed to compute embedding for query");
3120
+ }
3121
+ return result;
3122
+ }
3123
+ }
3124
+
3125
+ // src/impls/mistral-stt.ts
3126
+ var DEFAULT_BASE_URL4 = "https://api.mistral.ai/v1";
3127
+ var DEFAULT_MODEL = "voxtral-mini-latest";
3128
+ var AUDIO_MIME_BY_FORMAT = {
3129
+ mp3: "audio/mpeg",
3130
+ wav: "audio/wav",
3131
+ ogg: "audio/ogg",
3132
+ pcm: "audio/pcm",
3133
+ opus: "audio/opus"
3134
+ };
3135
+
3136
+ class MistralSttProvider {
3137
+ apiKey;
3138
+ defaultModel;
3139
+ defaultLanguage;
3140
+ baseUrl;
3141
+ fetchImpl;
3142
+ constructor(options) {
3143
+ if (!options.apiKey) {
3144
+ throw new Error("MistralSttProvider requires an apiKey");
3145
+ }
3146
+ this.apiKey = options.apiKey;
3147
+ this.defaultModel = options.defaultModel ?? DEFAULT_MODEL;
3148
+ this.defaultLanguage = options.defaultLanguage;
3149
+ this.baseUrl = normalizeBaseUrl(options.serverURL ?? DEFAULT_BASE_URL4);
3150
+ this.fetchImpl = options.fetchImpl ?? fetch;
3151
+ }
3152
+ async transcribe(input) {
3153
+ const formData = new FormData;
3154
+ const model = input.model ?? this.defaultModel;
3155
+ const mimeType = AUDIO_MIME_BY_FORMAT[input.audio.format] ?? "audio/wav";
3156
+ const fileName = `audio.${input.audio.format}`;
3157
+ const audioBytes = new Uint8Array(input.audio.data);
3158
+ const blob = new Blob([audioBytes], { type: mimeType });
3159
+ formData.append("file", blob, fileName);
3160
+ formData.append("model", model);
3161
+ formData.append("response_format", "verbose_json");
3162
+ const language = input.language ?? this.defaultLanguage;
3163
+ if (language) {
3164
+ formData.append("language", language);
3165
+ }
3166
+ const response = await this.fetchImpl(`${this.baseUrl}/audio/transcriptions`, {
3167
+ method: "POST",
3168
+ headers: {
3169
+ Authorization: `Bearer ${this.apiKey}`
3170
+ },
3171
+ body: formData
3172
+ });
3173
+ if (!response.ok) {
3174
+ const body = await response.text();
3175
+ throw new Error(`Mistral transcription request failed (${response.status}): ${body}`);
3176
+ }
3177
+ const payload = await response.json();
3178
+ return toTranscriptionResult(payload, input);
3179
+ }
3180
+ }
3181
+ function toTranscriptionResult(payload, input) {
3182
+ const record = asRecord2(payload);
3183
+ const text = readString3(record, "text") ?? "";
3184
+ const language = readString3(record, "language") ?? input.language ?? "unknown";
3185
+ const segments = parseSegments(record);
3186
+ if (segments.length === 0 && text.length > 0) {
3187
+ segments.push({
3188
+ text,
3189
+ startMs: 0,
3190
+ endMs: input.audio.durationMs ?? 0
3191
+ });
3192
+ }
3193
+ const durationMs = input.audio.durationMs ?? segments.reduce((max, segment) => Math.max(max, segment.endMs), 0);
3194
+ const topLevelWords = parseWordTimings(record.words);
3195
+ const flattenedWords = segments.flatMap((segment) => segment.wordTimings ?? []);
3196
+ const wordTimings = topLevelWords.length > 0 ? topLevelWords : flattenedWords.length > 0 ? flattenedWords : undefined;
3197
+ const speakers = dedupeSpeakers(segments);
3198
+ return {
3199
+ text,
3200
+ segments,
3201
+ language,
3202
+ durationMs,
3203
+ speakers: speakers.length > 0 ? speakers : undefined,
3204
+ wordTimings
3205
+ };
3206
+ }
3207
+ function parseSegments(record) {
3208
+ if (!Array.isArray(record.segments)) {
3209
+ return [];
3210
+ }
3211
+ const parsed = [];
3212
+ for (const entry of record.segments) {
3213
+ const segmentRecord = asRecord2(entry);
3214
+ const text = readString3(segmentRecord, "text");
3215
+ if (!text) {
3216
+ continue;
3217
+ }
3218
+ const startSeconds = readNumber2(segmentRecord, "start") ?? 0;
3219
+ const endSeconds = readNumber2(segmentRecord, "end") ?? startSeconds;
3220
+ parsed.push({
3221
+ text,
3222
+ startMs: secondsToMs(startSeconds),
3223
+ endMs: secondsToMs(endSeconds),
3224
+ speakerId: readString3(segmentRecord, "speaker") ?? undefined,
3225
+ confidence: readNumber2(segmentRecord, "confidence"),
3226
+ wordTimings: parseWordTimings(segmentRecord.words)
3227
+ });
3228
+ }
3229
+ return parsed;
3230
+ }
3231
+ function parseWordTimings(value) {
3232
+ if (!Array.isArray(value)) {
3233
+ return [];
3234
+ }
3235
+ const words = [];
3236
+ for (const entry of value) {
3237
+ const wordRecord = asRecord2(entry);
3238
+ const word = readString3(wordRecord, "word");
3239
+ const startSeconds = readNumber2(wordRecord, "start");
3240
+ const endSeconds = readNumber2(wordRecord, "end");
3241
+ if (!word || startSeconds == null || endSeconds == null) {
3242
+ continue;
3243
+ }
3244
+ words.push({
3245
+ word,
3246
+ startMs: secondsToMs(startSeconds),
3247
+ endMs: secondsToMs(endSeconds),
3248
+ confidence: readNumber2(wordRecord, "confidence")
3249
+ });
3250
+ }
3251
+ return words;
3252
+ }
3253
+ function dedupeSpeakers(segments) {
3254
+ const seen = new Set;
3255
+ const speakers = [];
3256
+ for (const segment of segments) {
3257
+ if (!segment.speakerId || seen.has(segment.speakerId)) {
3258
+ continue;
3259
+ }
3260
+ seen.add(segment.speakerId);
3261
+ speakers.push({
3262
+ id: segment.speakerId,
3263
+ name: segment.speakerName
3264
+ });
3265
+ }
3266
+ return speakers;
3267
+ }
3268
+ function normalizeBaseUrl(url) {
3269
+ return url.endsWith("/") ? url.slice(0, -1) : url;
3270
+ }
3271
+ function asRecord2(value) {
3272
+ if (value && typeof value === "object") {
3273
+ return value;
3274
+ }
3275
+ return {};
3276
+ }
3277
+ function readString3(record, key) {
3278
+ const value = record[key];
3279
+ return typeof value === "string" ? value : undefined;
3280
+ }
3281
+ function readNumber2(record, key) {
3282
+ const value = record[key];
3283
+ return typeof value === "number" ? value : undefined;
3284
+ }
3285
+ function secondsToMs(value) {
3286
+ return Math.round(value * 1000);
3287
+ }
3288
+
3289
+ // src/impls/mistral-conversational.session.ts
3290
+ class MistralConversationSession {
3291
+ events;
3292
+ queue = new AsyncEventQueue;
3293
+ turns = [];
3294
+ history = [];
3295
+ sessionId = crypto.randomUUID();
3296
+ startedAt = Date.now();
3297
+ sessionConfig;
3298
+ defaultModel;
3299
+ complete;
3300
+ sttProvider;
3301
+ pending = Promise.resolve();
3302
+ closed = false;
3303
+ closedSummary;
3304
+ constructor(options) {
3305
+ this.sessionConfig = options.sessionConfig;
3306
+ this.defaultModel = options.defaultModel;
3307
+ this.complete = options.complete;
3308
+ this.sttProvider = options.sttProvider;
3309
+ this.events = this.queue;
3310
+ this.queue.push({
3311
+ type: "session_started",
3312
+ sessionId: this.sessionId
3313
+ });
3314
+ }
3315
+ sendAudio(chunk) {
3316
+ if (this.closed) {
3317
+ return;
3318
+ }
3319
+ this.pending = this.pending.then(async () => {
3320
+ const transcription = await this.sttProvider.transcribe({
3321
+ audio: {
3322
+ data: chunk,
3323
+ format: this.sessionConfig.inputFormat ?? "pcm",
3324
+ sampleRateHz: 16000
3325
+ },
3326
+ language: this.sessionConfig.language
3327
+ });
3328
+ const transcriptText = transcription.text.trim();
3329
+ if (transcriptText.length > 0) {
3330
+ await this.handleUserText(transcriptText);
3331
+ }
3332
+ }).catch((error) => {
3333
+ this.emitError(error);
3334
+ });
3335
+ }
3336
+ sendText(text) {
3337
+ if (this.closed) {
3338
+ return;
3339
+ }
3340
+ const normalized = text.trim();
3341
+ if (normalized.length === 0) {
3342
+ return;
3343
+ }
3344
+ this.pending = this.pending.then(() => this.handleUserText(normalized)).catch((error) => {
3345
+ this.emitError(error);
3346
+ });
3347
+ }
3348
+ interrupt() {
3349
+ if (this.closed) {
3350
+ return;
3351
+ }
3352
+ this.queue.push({
3353
+ type: "error",
3354
+ error: new Error("Interrupt is not supported for non-streaming sessions.")
3355
+ });
3356
+ }
3357
+ async close() {
3358
+ if (this.closedSummary) {
3359
+ return this.closedSummary;
3360
+ }
3361
+ this.closed = true;
3362
+ await this.pending;
3363
+ const durationMs = Date.now() - this.startedAt;
3364
+ const summary = {
3365
+ sessionId: this.sessionId,
3366
+ durationMs,
3367
+ turns: this.turns.map((turn) => ({
3368
+ role: turn.role === "assistant" ? "agent" : turn.role,
3369
+ text: turn.text,
3370
+ startMs: turn.startMs,
3371
+ endMs: turn.endMs
3372
+ })),
3373
+ transcript: this.turns.map((turn) => `${turn.role}: ${turn.text}`).join(`
3374
+ `)
3375
+ };
3376
+ this.closedSummary = summary;
3377
+ this.queue.push({
3378
+ type: "session_ended",
3379
+ reason: "closed_by_client",
3380
+ durationMs
3381
+ });
3382
+ this.queue.close();
3383
+ return summary;
3384
+ }
3385
+ async handleUserText(text) {
3386
+ if (this.closed) {
3387
+ return;
3388
+ }
3389
+ const userStart = Date.now();
3390
+ this.queue.push({ type: "user_speech_started" });
3391
+ this.queue.push({ type: "user_speech_ended", transcript: text });
3392
+ this.queue.push({
3393
+ type: "transcript",
3394
+ role: "user",
3395
+ text,
3396
+ timestamp: userStart
3397
+ });
3398
+ this.turns.push({
3399
+ role: "user",
3400
+ text,
3401
+ startMs: userStart,
3402
+ endMs: Date.now()
3403
+ });
3404
+ this.history.push({ role: "user", content: text });
3405
+ const assistantStart = Date.now();
3406
+ const assistantText = await this.complete(this.history, {
3407
+ ...this.sessionConfig,
3408
+ llmModel: this.sessionConfig.llmModel ?? this.defaultModel
3409
+ });
3410
+ if (this.closed) {
3411
+ return;
3412
+ }
3413
+ const normalizedAssistantText = assistantText.trim();
3414
+ const finalAssistantText = normalizedAssistantText.length > 0 ? normalizedAssistantText : "I was unable to produce a response.";
3415
+ this.queue.push({
3416
+ type: "agent_speech_started",
3417
+ text: finalAssistantText
3418
+ });
3419
+ this.queue.push({
3420
+ type: "transcript",
3421
+ role: "agent",
3422
+ text: finalAssistantText,
3423
+ timestamp: assistantStart
3424
+ });
3425
+ this.queue.push({ type: "agent_speech_ended" });
3426
+ this.turns.push({
3427
+ role: "assistant",
3428
+ text: finalAssistantText,
3429
+ startMs: assistantStart,
3430
+ endMs: Date.now()
3431
+ });
3432
+ this.history.push({ role: "assistant", content: finalAssistantText });
3433
+ }
3434
+ emitError(error) {
3435
+ if (this.closed) {
3436
+ return;
3437
+ }
3438
+ this.queue.push({ type: "error", error: toError(error) });
1714
3439
  }
1715
3440
  }
1716
- function mapFinishReason(reason) {
1717
- if (!reason)
1718
- return;
1719
- const normalized = reason.toLowerCase();
1720
- switch (normalized) {
1721
- case "stop":
1722
- return "stop";
1723
- case "length":
1724
- return "length";
1725
- case "tool_call":
1726
- case "tool_calls":
1727
- return "tool_call";
1728
- case "content_filter":
1729
- return "content_filter";
1730
- default:
1731
- return;
3441
+ function toError(error) {
3442
+ if (error instanceof Error) {
3443
+ return error;
1732
3444
  }
3445
+ return new Error(String(error));
1733
3446
  }
1734
3447
 
1735
- // src/impls/mistral-embedding.ts
1736
- import { Mistral as Mistral2 } from "@mistralai/mistralai";
3448
+ // src/impls/mistral-conversational.ts
3449
+ var DEFAULT_BASE_URL5 = "https://api.mistral.ai/v1";
3450
+ var DEFAULT_MODEL2 = "mistral-small-latest";
3451
+ var DEFAULT_VOICE = "default";
1737
3452
 
1738
- class MistralEmbeddingProvider {
1739
- client;
3453
+ class MistralConversationalProvider {
3454
+ apiKey;
1740
3455
  defaultModel;
3456
+ defaultVoiceId;
3457
+ baseUrl;
3458
+ fetchImpl;
3459
+ sttProvider;
1741
3460
  constructor(options) {
1742
3461
  if (!options.apiKey) {
1743
- throw new Error("MistralEmbeddingProvider requires an apiKey");
3462
+ throw new Error("MistralConversationalProvider requires an apiKey");
1744
3463
  }
1745
- this.client = options.client ?? new Mistral2({
3464
+ this.apiKey = options.apiKey;
3465
+ this.defaultModel = options.defaultModel ?? DEFAULT_MODEL2;
3466
+ this.defaultVoiceId = options.defaultVoiceId ?? DEFAULT_VOICE;
3467
+ this.baseUrl = normalizeBaseUrl2(options.serverURL ?? DEFAULT_BASE_URL5);
3468
+ this.fetchImpl = options.fetchImpl ?? fetch;
3469
+ this.sttProvider = options.sttProvider ?? new MistralSttProvider({
1746
3470
  apiKey: options.apiKey,
1747
- serverURL: options.serverURL
3471
+ defaultModel: options.sttOptions?.defaultModel,
3472
+ defaultLanguage: options.sttOptions?.defaultLanguage,
3473
+ serverURL: options.sttOptions?.serverURL ?? options.serverURL,
3474
+ fetchImpl: this.fetchImpl
1748
3475
  });
1749
- this.defaultModel = options.defaultModel ?? "mistral-embed";
1750
3476
  }
1751
- async embedDocuments(documents, options) {
1752
- if (documents.length === 0)
1753
- return [];
1754
- const model = options?.model ?? this.defaultModel;
1755
- const response = await this.client.embeddings.create({
1756
- model,
1757
- inputs: documents.map((doc) => doc.text)
3477
+ async startSession(config) {
3478
+ return new MistralConversationSession({
3479
+ sessionConfig: {
3480
+ ...config,
3481
+ voiceId: config.voiceId || this.defaultVoiceId
3482
+ },
3483
+ defaultModel: this.defaultModel,
3484
+ complete: (history, sessionConfig) => this.completeConversation(history, sessionConfig),
3485
+ sttProvider: this.sttProvider
1758
3486
  });
1759
- return response.data.map((item, index) => ({
1760
- id: documents[index]?.id ?? (item.index != null ? `embedding-${item.index}` : `embedding-${index}`),
1761
- vector: item.embedding ?? [],
1762
- dimensions: item.embedding?.length ?? 0,
1763
- model: response.model,
1764
- metadata: documents[index]?.metadata ? Object.fromEntries(Object.entries(documents[index]?.metadata ?? {}).map(([key, value]) => [key, String(value)])) : undefined
1765
- }));
1766
3487
  }
1767
- async embedQuery(query, options) {
1768
- const [result] = await this.embedDocuments([{ id: "query", text: query }], options);
1769
- if (!result) {
1770
- throw new Error("Failed to compute embedding for query");
3488
+ async listVoices() {
3489
+ return [
3490
+ {
3491
+ id: this.defaultVoiceId,
3492
+ name: "Mistral Default Voice",
3493
+ description: "Default conversational voice profile.",
3494
+ capabilities: ["conversational"]
3495
+ }
3496
+ ];
3497
+ }
3498
+ async completeConversation(history, sessionConfig) {
3499
+ const model = sessionConfig.llmModel ?? this.defaultModel;
3500
+ const messages = [];
3501
+ if (sessionConfig.systemPrompt) {
3502
+ messages.push({ role: "system", content: sessionConfig.systemPrompt });
1771
3503
  }
1772
- return result;
3504
+ for (const item of history) {
3505
+ messages.push({ role: item.role, content: item.content });
3506
+ }
3507
+ const response = await this.fetchImpl(`${this.baseUrl}/chat/completions`, {
3508
+ method: "POST",
3509
+ headers: {
3510
+ Authorization: `Bearer ${this.apiKey}`,
3511
+ "Content-Type": "application/json"
3512
+ },
3513
+ body: JSON.stringify({
3514
+ model,
3515
+ messages
3516
+ })
3517
+ });
3518
+ if (!response.ok) {
3519
+ const body = await response.text();
3520
+ throw new Error(`Mistral conversational request failed (${response.status}): ${body}`);
3521
+ }
3522
+ const payload = await response.json();
3523
+ return readAssistantText(payload);
3524
+ }
3525
+ }
3526
+ function normalizeBaseUrl2(url) {
3527
+ return url.endsWith("/") ? url.slice(0, -1) : url;
3528
+ }
3529
+ function readAssistantText(payload) {
3530
+ const record = asRecord3(payload);
3531
+ const choices = Array.isArray(record.choices) ? record.choices : [];
3532
+ const firstChoice = asRecord3(choices[0]);
3533
+ const message = asRecord3(firstChoice.message);
3534
+ if (typeof message.content === "string") {
3535
+ return message.content;
3536
+ }
3537
+ if (Array.isArray(message.content)) {
3538
+ const textParts = message.content.map((part) => {
3539
+ const entry = asRecord3(part);
3540
+ const text = entry.text;
3541
+ return typeof text === "string" ? text : "";
3542
+ }).filter((text) => text.length > 0);
3543
+ return textParts.join("");
3544
+ }
3545
+ return "";
3546
+ }
3547
+ function asRecord3(value) {
3548
+ if (value && typeof value === "object") {
3549
+ return value;
1773
3550
  }
3551
+ return {};
1774
3552
  }
1775
3553
 
1776
3554
  // src/impls/qdrant-vector.ts
@@ -2172,7 +3950,7 @@ function distanceToScore(distance, metric) {
2172
3950
 
2173
3951
  // src/impls/stripe-payments.ts
2174
3952
  import Stripe from "stripe";
2175
- var API_VERSION = "2026-01-28.clover";
3953
+ var API_VERSION = "2026-02-25.clover";
2176
3954
 
2177
3955
  class StripePaymentsProvider {
2178
3956
  stripe;
@@ -2837,8 +4615,320 @@ function mapStatus(status) {
2837
4615
  }
2838
4616
  }
2839
4617
 
4618
+ // src/impls/messaging-slack.ts
4619
+ class SlackMessagingProvider {
4620
+ botToken;
4621
+ defaultChannelId;
4622
+ apiBaseUrl;
4623
+ constructor(options) {
4624
+ this.botToken = options.botToken;
4625
+ this.defaultChannelId = options.defaultChannelId;
4626
+ this.apiBaseUrl = options.apiBaseUrl ?? "https://slack.com/api";
4627
+ }
4628
+ async sendMessage(input) {
4629
+ const channel = input.channelId ?? input.recipientId ?? this.defaultChannelId;
4630
+ if (!channel) {
4631
+ throw new Error("Slack sendMessage requires channelId, recipientId, or defaultChannelId.");
4632
+ }
4633
+ const payload = {
4634
+ channel,
4635
+ text: input.text,
4636
+ mrkdwn: input.markdown ?? true,
4637
+ thread_ts: input.threadId
4638
+ };
4639
+ const response = await fetch(`${this.apiBaseUrl}/chat.postMessage`, {
4640
+ method: "POST",
4641
+ headers: {
4642
+ authorization: `Bearer ${this.botToken}`,
4643
+ "content-type": "application/json"
4644
+ },
4645
+ body: JSON.stringify(payload)
4646
+ });
4647
+ const body = await response.json();
4648
+ if (!response.ok || !body.ok || !body.ts) {
4649
+ throw new Error(`Slack sendMessage failed: ${body.error ?? `HTTP_${response.status}`}`);
4650
+ }
4651
+ return {
4652
+ id: `slack:${body.channel ?? channel}:${body.ts}`,
4653
+ providerMessageId: body.ts,
4654
+ status: "sent",
4655
+ sentAt: new Date,
4656
+ metadata: {
4657
+ channelId: body.channel ?? channel
4658
+ }
4659
+ };
4660
+ }
4661
+ async updateMessage(messageId, input) {
4662
+ const channel = input.channelId ?? this.defaultChannelId;
4663
+ if (!channel) {
4664
+ throw new Error("Slack updateMessage requires channelId or defaultChannelId.");
4665
+ }
4666
+ const response = await fetch(`${this.apiBaseUrl}/chat.update`, {
4667
+ method: "POST",
4668
+ headers: {
4669
+ authorization: `Bearer ${this.botToken}`,
4670
+ "content-type": "application/json"
4671
+ },
4672
+ body: JSON.stringify({
4673
+ channel,
4674
+ ts: messageId,
4675
+ text: input.text,
4676
+ mrkdwn: input.markdown ?? true
4677
+ })
4678
+ });
4679
+ const body = await response.json();
4680
+ if (!response.ok || !body.ok || !body.ts) {
4681
+ throw new Error(`Slack updateMessage failed: ${body.error ?? `HTTP_${response.status}`}`);
4682
+ }
4683
+ return {
4684
+ id: `slack:${body.channel ?? channel}:${body.ts}`,
4685
+ providerMessageId: body.ts,
4686
+ status: "sent",
4687
+ sentAt: new Date,
4688
+ metadata: {
4689
+ channelId: body.channel ?? channel
4690
+ }
4691
+ };
4692
+ }
4693
+ }
4694
+
4695
+ // src/impls/messaging-github.ts
4696
+ class GithubMessagingProvider {
4697
+ token;
4698
+ defaultOwner;
4699
+ defaultRepo;
4700
+ apiBaseUrl;
4701
+ constructor(options) {
4702
+ this.token = options.token;
4703
+ this.defaultOwner = options.defaultOwner;
4704
+ this.defaultRepo = options.defaultRepo;
4705
+ this.apiBaseUrl = options.apiBaseUrl ?? "https://api.github.com";
4706
+ }
4707
+ async sendMessage(input) {
4708
+ const target = this.resolveTarget(input);
4709
+ const response = await fetch(`${this.apiBaseUrl}/repos/${target.owner}/${target.repo}/issues/${target.issueNumber}/comments`, {
4710
+ method: "POST",
4711
+ headers: {
4712
+ authorization: `Bearer ${this.token}`,
4713
+ accept: "application/vnd.github+json",
4714
+ "content-type": "application/json"
4715
+ },
4716
+ body: JSON.stringify({ body: input.text })
4717
+ });
4718
+ const body = await response.json();
4719
+ if (!response.ok || !body.id) {
4720
+ throw new Error(`GitHub sendMessage failed: ${body.message ?? `HTTP_${response.status}`}`);
4721
+ }
4722
+ return {
4723
+ id: String(body.id),
4724
+ providerMessageId: body.node_id,
4725
+ status: "sent",
4726
+ sentAt: new Date,
4727
+ metadata: {
4728
+ url: body.html_url ?? "",
4729
+ owner: target.owner,
4730
+ repo: target.repo,
4731
+ issueNumber: String(target.issueNumber)
4732
+ }
4733
+ };
4734
+ }
4735
+ async updateMessage(messageId, input) {
4736
+ const owner = input.metadata?.owner ?? this.defaultOwner;
4737
+ const repo = input.metadata?.repo ?? this.defaultRepo;
4738
+ if (!owner || !repo) {
4739
+ throw new Error("GitHub updateMessage requires owner and repo metadata.");
4740
+ }
4741
+ const response = await fetch(`${this.apiBaseUrl}/repos/${owner}/${repo}/issues/comments/${messageId}`, {
4742
+ method: "PATCH",
4743
+ headers: {
4744
+ authorization: `Bearer ${this.token}`,
4745
+ accept: "application/vnd.github+json",
4746
+ "content-type": "application/json"
4747
+ },
4748
+ body: JSON.stringify({ body: input.text })
4749
+ });
4750
+ const body = await response.json();
4751
+ if (!response.ok || !body.id) {
4752
+ throw new Error(`GitHub updateMessage failed: ${body.message ?? `HTTP_${response.status}`}`);
4753
+ }
4754
+ return {
4755
+ id: String(body.id),
4756
+ providerMessageId: body.node_id,
4757
+ status: "sent",
4758
+ sentAt: new Date,
4759
+ metadata: {
4760
+ url: body.html_url ?? "",
4761
+ owner,
4762
+ repo
4763
+ }
4764
+ };
4765
+ }
4766
+ resolveTarget(input) {
4767
+ const parsedRecipient = parseRecipient(input.recipientId);
4768
+ const owner = parsedRecipient?.owner ?? this.defaultOwner;
4769
+ const repo = parsedRecipient?.repo ?? this.defaultRepo;
4770
+ const issueNumber = parsedRecipient?.issueNumber ?? parseIssueNumber(input.threadId);
4771
+ if (!owner || !repo || issueNumber == null) {
4772
+ throw new Error("GitHub sendMessage requires owner/repo and issueNumber (use recipientId like owner/repo#123 or provide defaults + threadId).");
4773
+ }
4774
+ return {
4775
+ owner,
4776
+ repo,
4777
+ issueNumber
4778
+ };
4779
+ }
4780
+ }
4781
+ function parseRecipient(value) {
4782
+ if (!value)
4783
+ return null;
4784
+ const match = value.trim().match(/^([^/]+)\/([^#]+)#(\d+)$/);
4785
+ if (!match)
4786
+ return null;
4787
+ const owner = match[1];
4788
+ const repo = match[2];
4789
+ const issueNumber = Number(match[3]);
4790
+ if (!owner || !repo || !Number.isInteger(issueNumber)) {
4791
+ return null;
4792
+ }
4793
+ return { owner, repo, issueNumber };
4794
+ }
4795
+ function parseIssueNumber(value) {
4796
+ if (!value)
4797
+ return null;
4798
+ const numeric = Number(value);
4799
+ return Number.isInteger(numeric) ? numeric : null;
4800
+ }
4801
+
4802
+ // src/impls/messaging-whatsapp-meta.ts
4803
+ class MetaWhatsappMessagingProvider {
4804
+ accessToken;
4805
+ phoneNumberId;
4806
+ apiVersion;
4807
+ constructor(options) {
4808
+ this.accessToken = options.accessToken;
4809
+ this.phoneNumberId = options.phoneNumberId;
4810
+ this.apiVersion = options.apiVersion ?? "v22.0";
4811
+ }
4812
+ async sendMessage(input) {
4813
+ const to = input.recipientId;
4814
+ if (!to) {
4815
+ throw new Error("Meta WhatsApp sendMessage requires recipientId.");
4816
+ }
4817
+ const response = await fetch(`https://graph.facebook.com/${this.apiVersion}/${this.phoneNumberId}/messages`, {
4818
+ method: "POST",
4819
+ headers: {
4820
+ authorization: `Bearer ${this.accessToken}`,
4821
+ "content-type": "application/json"
4822
+ },
4823
+ body: JSON.stringify({
4824
+ messaging_product: "whatsapp",
4825
+ to,
4826
+ type: "text",
4827
+ text: {
4828
+ body: input.text,
4829
+ preview_url: false
4830
+ }
4831
+ })
4832
+ });
4833
+ const body = await response.json();
4834
+ const messageId = body.messages?.[0]?.id;
4835
+ if (!response.ok || !messageId) {
4836
+ const errorCode = body.error?.code != null ? String(body.error.code) : "";
4837
+ throw new Error(`Meta WhatsApp sendMessage failed: ${body.error?.message ?? `HTTP_${response.status}`}${errorCode ? ` (${errorCode})` : ""}`);
4838
+ }
4839
+ return {
4840
+ id: messageId,
4841
+ providerMessageId: messageId,
4842
+ status: "sent",
4843
+ sentAt: new Date,
4844
+ metadata: {
4845
+ phoneNumberId: this.phoneNumberId
4846
+ }
4847
+ };
4848
+ }
4849
+ }
4850
+
4851
+ // src/impls/messaging-whatsapp-twilio.ts
4852
+ import { Buffer as Buffer4 } from "buffer";
4853
+
4854
+ class TwilioWhatsappMessagingProvider {
4855
+ accountSid;
4856
+ authToken;
4857
+ fromNumber;
4858
+ constructor(options) {
4859
+ this.accountSid = options.accountSid;
4860
+ this.authToken = options.authToken;
4861
+ this.fromNumber = options.fromNumber;
4862
+ }
4863
+ async sendMessage(input) {
4864
+ const to = normalizeWhatsappAddress(input.recipientId);
4865
+ const from = normalizeWhatsappAddress(input.channelId ?? this.fromNumber);
4866
+ if (!to) {
4867
+ throw new Error("Twilio WhatsApp sendMessage requires recipientId.");
4868
+ }
4869
+ if (!from) {
4870
+ throw new Error("Twilio WhatsApp sendMessage requires channelId or configured fromNumber.");
4871
+ }
4872
+ const params = new URLSearchParams;
4873
+ params.set("To", to);
4874
+ params.set("From", from);
4875
+ params.set("Body", input.text);
4876
+ const auth = Buffer4.from(`${this.accountSid}:${this.authToken}`).toString("base64");
4877
+ const response = await fetch(`https://api.twilio.com/2010-04-01/Accounts/${this.accountSid}/Messages.json`, {
4878
+ method: "POST",
4879
+ headers: {
4880
+ authorization: `Basic ${auth}`,
4881
+ "content-type": "application/x-www-form-urlencoded"
4882
+ },
4883
+ body: params.toString()
4884
+ });
4885
+ const body = await response.json();
4886
+ if (!response.ok || !body.sid) {
4887
+ throw new Error(`Twilio WhatsApp sendMessage failed: ${body.error_message ?? `HTTP_${response.status}`}`);
4888
+ }
4889
+ return {
4890
+ id: body.sid,
4891
+ providerMessageId: body.sid,
4892
+ status: mapTwilioStatus(body.status),
4893
+ sentAt: new Date,
4894
+ errorCode: body.error_code != null ? String(body.error_code) : undefined,
4895
+ errorMessage: body.error_message ?? undefined,
4896
+ metadata: {
4897
+ from,
4898
+ to
4899
+ }
4900
+ };
4901
+ }
4902
+ }
4903
+ function normalizeWhatsappAddress(value) {
4904
+ if (!value)
4905
+ return null;
4906
+ if (value.startsWith("whatsapp:"))
4907
+ return value;
4908
+ return `whatsapp:${value}`;
4909
+ }
4910
+ function mapTwilioStatus(status) {
4911
+ switch (status) {
4912
+ case "queued":
4913
+ case "accepted":
4914
+ case "scheduled":
4915
+ return "queued";
4916
+ case "sending":
4917
+ return "sending";
4918
+ case "delivered":
4919
+ return "delivered";
4920
+ case "failed":
4921
+ case "undelivered":
4922
+ case "canceled":
4923
+ return "failed";
4924
+ case "sent":
4925
+ default:
4926
+ return "sent";
4927
+ }
4928
+ }
4929
+
2840
4930
  // src/impls/powens-client.ts
2841
- import { URL } from "url";
4931
+ import { URL as URL2 } from "url";
2842
4932
  var POWENS_BASE_URL = {
2843
4933
  sandbox: "https://api-sandbox.powens.com/v2",
2844
4934
  production: "https://api.powens.com/v2"
@@ -2924,7 +5014,7 @@ class PowensClient {
2924
5014
  });
2925
5015
  }
2926
5016
  async request(options) {
2927
- const url = new URL(options.path, this.baseUrl);
5017
+ const url = new URL2(options.path, this.baseUrl);
2928
5018
  if (options.searchParams) {
2929
5019
  for (const [key, value] of Object.entries(options.searchParams)) {
2930
5020
  if (value === undefined || value === null)
@@ -2994,7 +5084,7 @@ class PowensClient {
2994
5084
  return this.token.accessToken;
2995
5085
  }
2996
5086
  async fetchAccessToken() {
2997
- const url = new URL("/oauth/token", this.baseUrl);
5087
+ const url = new URL2("/oauth/token", this.baseUrl);
2998
5088
  const basicAuth = Buffer.from(`${this.clientId}:${this.clientSecret}`, "utf-8").toString("base64");
2999
5089
  const response = await this.fetchImpl(url, {
3000
5090
  method: "POST",
@@ -3343,7 +5433,7 @@ function resolveLabelIds(defaults, tags) {
3343
5433
  }
3344
5434
 
3345
5435
  // src/impls/jira.ts
3346
- import { Buffer as Buffer4 } from "buffer";
5436
+ import { Buffer as Buffer5 } from "buffer";
3347
5437
 
3348
5438
  class JiraProjectManagementProvider {
3349
5439
  siteUrl;
@@ -3420,7 +5510,7 @@ function normalizeSiteUrl(siteUrl) {
3420
5510
  return siteUrl.replace(/\/$/, "");
3421
5511
  }
3422
5512
  function buildAuthHeader(email, apiToken) {
3423
- const token = Buffer4.from(`${email}:${apiToken}`).toString("base64");
5513
+ const token = Buffer5.from(`${email}:${apiToken}`).toString("base64");
3424
5514
  return `Basic ${token}`;
3425
5515
  }
3426
5516
  function resolveIssueType(type, defaults) {
@@ -3623,7 +5713,7 @@ function buildParagraphBlocks(text) {
3623
5713
  }
3624
5714
 
3625
5715
  // src/impls/tldv-meeting-recorder.ts
3626
- var DEFAULT_BASE_URL4 = "https://pasta.tldv.io/v1alpha1";
5716
+ var DEFAULT_BASE_URL6 = "https://pasta.tldv.io/v1alpha1";
3627
5717
 
3628
5718
  class TldvMeetingRecorderProvider {
3629
5719
  apiKey;
@@ -3631,7 +5721,7 @@ class TldvMeetingRecorderProvider {
3631
5721
  defaultPageSize;
3632
5722
  constructor(options) {
3633
5723
  this.apiKey = options.apiKey;
3634
- this.baseUrl = options.baseUrl ?? DEFAULT_BASE_URL4;
5724
+ this.baseUrl = options.baseUrl ?? DEFAULT_BASE_URL6;
3635
5725
  this.defaultPageSize = options.pageSize;
3636
5726
  }
3637
5727
  async listMeetings(params) {
@@ -3766,7 +5856,7 @@ async function safeReadError4(response) {
3766
5856
  }
3767
5857
 
3768
5858
  // src/impls/provider-factory.ts
3769
- import { Buffer as Buffer5 } from "buffer";
5859
+ import { Buffer as Buffer6 } from "buffer";
3770
5860
  var SECRET_CACHE = new Map;
3771
5861
 
3772
5862
  class IntegrationProviderFactory {
@@ -3807,6 +5897,39 @@ class IntegrationProviderFactory {
3807
5897
  throw new Error(`Unsupported SMS integration: ${context.spec.meta.key}`);
3808
5898
  }
3809
5899
  }
5900
+ async createMessagingProvider(context) {
5901
+ const secrets = await this.loadSecrets(context);
5902
+ const config = context.config;
5903
+ switch (context.spec.meta.key) {
5904
+ case "messaging.slack":
5905
+ return new SlackMessagingProvider({
5906
+ botToken: requireSecret(secrets, "botToken", "Slack bot token is required"),
5907
+ defaultChannelId: config?.defaultChannelId,
5908
+ apiBaseUrl: config?.apiBaseUrl
5909
+ });
5910
+ case "messaging.github":
5911
+ return new GithubMessagingProvider({
5912
+ token: requireSecret(secrets, "token", "GitHub token is required"),
5913
+ defaultOwner: config?.defaultOwner,
5914
+ defaultRepo: config?.defaultRepo,
5915
+ apiBaseUrl: config?.apiBaseUrl
5916
+ });
5917
+ case "messaging.whatsapp.meta":
5918
+ return new MetaWhatsappMessagingProvider({
5919
+ accessToken: requireSecret(secrets, "accessToken", "Meta WhatsApp access token is required"),
5920
+ phoneNumberId: requireConfig(context, "phoneNumberId", "Meta WhatsApp phoneNumberId is required"),
5921
+ apiVersion: config?.apiVersion
5922
+ });
5923
+ case "messaging.whatsapp.twilio":
5924
+ return new TwilioWhatsappMessagingProvider({
5925
+ accountSid: requireSecret(secrets, "accountSid", "Twilio account SID is required"),
5926
+ authToken: requireSecret(secrets, "authToken", "Twilio auth token is required"),
5927
+ fromNumber: config?.fromNumber
5928
+ });
5929
+ default:
5930
+ throw new Error(`Unsupported messaging integration: ${context.spec.meta.key}`);
5931
+ }
5932
+ }
3810
5933
  async createVectorStoreProvider(context) {
3811
5934
  const secrets = await this.loadSecrets(context);
3812
5935
  const config = context.config;
@@ -3905,6 +6028,41 @@ class IntegrationProviderFactory {
3905
6028
  throw new Error(`Unsupported voice integration: ${context.spec.meta.key}`);
3906
6029
  }
3907
6030
  }
6031
+ async createSttProvider(context) {
6032
+ const secrets = await this.loadSecrets(context);
6033
+ const config = context.config;
6034
+ switch (context.spec.meta.key) {
6035
+ case "ai-voice-stt.mistral":
6036
+ return new MistralSttProvider({
6037
+ apiKey: requireSecret(secrets, "apiKey", "Mistral API key is required"),
6038
+ defaultModel: config?.model,
6039
+ defaultLanguage: config?.language,
6040
+ serverURL: config?.serverURL
6041
+ });
6042
+ default:
6043
+ throw new Error(`Unsupported STT integration: ${context.spec.meta.key}`);
6044
+ }
6045
+ }
6046
+ async createConversationalProvider(context) {
6047
+ const secrets = await this.loadSecrets(context);
6048
+ const config = context.config;
6049
+ switch (context.spec.meta.key) {
6050
+ case "ai-voice-conv.mistral":
6051
+ return new MistralConversationalProvider({
6052
+ apiKey: requireSecret(secrets, "apiKey", "Mistral API key is required"),
6053
+ defaultModel: config?.model,
6054
+ defaultVoiceId: config?.defaultVoice,
6055
+ serverURL: config?.serverURL,
6056
+ sttOptions: {
6057
+ defaultModel: config?.model,
6058
+ defaultLanguage: config?.language,
6059
+ serverURL: config?.serverURL
6060
+ }
6061
+ });
6062
+ default:
6063
+ throw new Error(`Unsupported conversational integration: ${context.spec.meta.key}`);
6064
+ }
6065
+ }
3908
6066
  async createProjectManagementProvider(context) {
3909
6067
  const secrets = await this.loadSecrets(context);
3910
6068
  const config = context.config;
@@ -4038,6 +6196,10 @@ class IntegrationProviderFactory {
4038
6196
  throw new Error(`Unsupported open banking integration: ${context.spec.meta.key}`);
4039
6197
  }
4040
6198
  }
6199
+ async createHealthProvider(context) {
6200
+ const secrets = await this.loadSecrets(context);
6201
+ return createHealthProviderFromContext(context, secrets);
6202
+ }
4041
6203
  async loadSecrets(context) {
4042
6204
  const cacheKey = context.connection.meta.id;
4043
6205
  if (SECRET_CACHE.has(cacheKey)) {
@@ -4051,7 +6213,7 @@ class IntegrationProviderFactory {
4051
6213
  }
4052
6214
  }
4053
6215
  function parseSecret(secret) {
4054
- const text = Buffer5.from(secret.data).toString("utf-8").trim();
6216
+ const text = Buffer6.from(secret.data).toString("utf-8").trim();
4055
6217
  if (!text)
4056
6218
  return {};
4057
6219
  try {