@contractspec/integration.providers-impls 2.10.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 (57) hide show
  1. package/README.md +7 -1
  2. package/dist/impls/async-event-queue.d.ts +8 -0
  3. package/dist/impls/async-event-queue.js +47 -0
  4. package/dist/impls/health/base-health-provider.d.ts +64 -13
  5. package/dist/impls/health/base-health-provider.js +506 -156
  6. package/dist/impls/health/hybrid-health-providers.d.ts +34 -0
  7. package/dist/impls/health/hybrid-health-providers.js +1088 -0
  8. package/dist/impls/health/official-health-providers.d.ts +78 -0
  9. package/dist/impls/health/official-health-providers.js +968 -0
  10. package/dist/impls/health/provider-normalizers.d.ts +28 -0
  11. package/dist/impls/health/provider-normalizers.js +287 -0
  12. package/dist/impls/health/providers.d.ts +2 -39
  13. package/dist/impls/health/providers.js +895 -184
  14. package/dist/impls/health-provider-factory.js +1009 -196
  15. package/dist/impls/index.d.ts +6 -0
  16. package/dist/impls/index.js +1950 -278
  17. package/dist/impls/messaging-github.d.ts +17 -0
  18. package/dist/impls/messaging-github.js +110 -0
  19. package/dist/impls/messaging-slack.d.ts +14 -0
  20. package/dist/impls/messaging-slack.js +80 -0
  21. package/dist/impls/messaging-whatsapp-meta.d.ts +13 -0
  22. package/dist/impls/messaging-whatsapp-meta.js +52 -0
  23. package/dist/impls/messaging-whatsapp-twilio.d.ts +13 -0
  24. package/dist/impls/messaging-whatsapp-twilio.js +82 -0
  25. package/dist/impls/mistral-conversational.d.ts +23 -0
  26. package/dist/impls/mistral-conversational.js +476 -0
  27. package/dist/impls/mistral-conversational.session.d.ts +32 -0
  28. package/dist/impls/mistral-conversational.session.js +206 -0
  29. package/dist/impls/mistral-stt.d.ts +17 -0
  30. package/dist/impls/mistral-stt.js +167 -0
  31. package/dist/impls/provider-factory.d.ts +5 -1
  32. package/dist/impls/provider-factory.js +1943 -277
  33. package/dist/impls/stripe-payments.js +1 -1
  34. package/dist/index.d.ts +1 -0
  35. package/dist/index.js +1953 -278
  36. package/dist/messaging.d.ts +1 -0
  37. package/dist/messaging.js +3 -0
  38. package/dist/node/impls/async-event-queue.js +46 -0
  39. package/dist/node/impls/health/base-health-provider.js +506 -156
  40. package/dist/node/impls/health/hybrid-health-providers.js +1087 -0
  41. package/dist/node/impls/health/official-health-providers.js +967 -0
  42. package/dist/node/impls/health/provider-normalizers.js +286 -0
  43. package/dist/node/impls/health/providers.js +895 -184
  44. package/dist/node/impls/health-provider-factory.js +1009 -196
  45. package/dist/node/impls/index.js +1950 -278
  46. package/dist/node/impls/messaging-github.js +109 -0
  47. package/dist/node/impls/messaging-slack.js +79 -0
  48. package/dist/node/impls/messaging-whatsapp-meta.js +51 -0
  49. package/dist/node/impls/messaging-whatsapp-twilio.js +81 -0
  50. package/dist/node/impls/mistral-conversational.js +475 -0
  51. package/dist/node/impls/mistral-conversational.session.js +205 -0
  52. package/dist/node/impls/mistral-stt.js +166 -0
  53. package/dist/node/impls/provider-factory.js +1943 -277
  54. package/dist/node/impls/stripe-payments.js +1 -1
  55. package/dist/node/index.js +1953 -278
  56. package/dist/node/messaging.js +2 -0
  57. package/package.json +156 -12
@@ -0,0 +1,166 @@
1
+ // src/impls/mistral-stt.ts
2
+ var DEFAULT_BASE_URL = "https://api.mistral.ai/v1";
3
+ var DEFAULT_MODEL = "voxtral-mini-latest";
4
+ var AUDIO_MIME_BY_FORMAT = {
5
+ mp3: "audio/mpeg",
6
+ wav: "audio/wav",
7
+ ogg: "audio/ogg",
8
+ pcm: "audio/pcm",
9
+ opus: "audio/opus"
10
+ };
11
+
12
+ class MistralSttProvider {
13
+ apiKey;
14
+ defaultModel;
15
+ defaultLanguage;
16
+ baseUrl;
17
+ fetchImpl;
18
+ constructor(options) {
19
+ if (!options.apiKey) {
20
+ throw new Error("MistralSttProvider requires an apiKey");
21
+ }
22
+ this.apiKey = options.apiKey;
23
+ this.defaultModel = options.defaultModel ?? DEFAULT_MODEL;
24
+ this.defaultLanguage = options.defaultLanguage;
25
+ this.baseUrl = normalizeBaseUrl(options.serverURL ?? DEFAULT_BASE_URL);
26
+ this.fetchImpl = options.fetchImpl ?? fetch;
27
+ }
28
+ async transcribe(input) {
29
+ const formData = new FormData;
30
+ const model = input.model ?? this.defaultModel;
31
+ const mimeType = AUDIO_MIME_BY_FORMAT[input.audio.format] ?? "audio/wav";
32
+ const fileName = `audio.${input.audio.format}`;
33
+ const audioBytes = new Uint8Array(input.audio.data);
34
+ const blob = new Blob([audioBytes], { type: mimeType });
35
+ formData.append("file", blob, fileName);
36
+ formData.append("model", model);
37
+ formData.append("response_format", "verbose_json");
38
+ const language = input.language ?? this.defaultLanguage;
39
+ if (language) {
40
+ formData.append("language", language);
41
+ }
42
+ const response = await this.fetchImpl(`${this.baseUrl}/audio/transcriptions`, {
43
+ method: "POST",
44
+ headers: {
45
+ Authorization: `Bearer ${this.apiKey}`
46
+ },
47
+ body: formData
48
+ });
49
+ if (!response.ok) {
50
+ const body = await response.text();
51
+ throw new Error(`Mistral transcription request failed (${response.status}): ${body}`);
52
+ }
53
+ const payload = await response.json();
54
+ return toTranscriptionResult(payload, input);
55
+ }
56
+ }
57
+ function toTranscriptionResult(payload, input) {
58
+ const record = asRecord(payload);
59
+ const text = readString(record, "text") ?? "";
60
+ const language = readString(record, "language") ?? input.language ?? "unknown";
61
+ const segments = parseSegments(record);
62
+ if (segments.length === 0 && text.length > 0) {
63
+ segments.push({
64
+ text,
65
+ startMs: 0,
66
+ endMs: input.audio.durationMs ?? 0
67
+ });
68
+ }
69
+ const durationMs = input.audio.durationMs ?? segments.reduce((max, segment) => Math.max(max, segment.endMs), 0);
70
+ const topLevelWords = parseWordTimings(record.words);
71
+ const flattenedWords = segments.flatMap((segment) => segment.wordTimings ?? []);
72
+ const wordTimings = topLevelWords.length > 0 ? topLevelWords : flattenedWords.length > 0 ? flattenedWords : undefined;
73
+ const speakers = dedupeSpeakers(segments);
74
+ return {
75
+ text,
76
+ segments,
77
+ language,
78
+ durationMs,
79
+ speakers: speakers.length > 0 ? speakers : undefined,
80
+ wordTimings
81
+ };
82
+ }
83
+ function parseSegments(record) {
84
+ if (!Array.isArray(record.segments)) {
85
+ return [];
86
+ }
87
+ const parsed = [];
88
+ for (const entry of record.segments) {
89
+ const segmentRecord = asRecord(entry);
90
+ const text = readString(segmentRecord, "text");
91
+ if (!text) {
92
+ continue;
93
+ }
94
+ const startSeconds = readNumber(segmentRecord, "start") ?? 0;
95
+ const endSeconds = readNumber(segmentRecord, "end") ?? startSeconds;
96
+ parsed.push({
97
+ text,
98
+ startMs: secondsToMs(startSeconds),
99
+ endMs: secondsToMs(endSeconds),
100
+ speakerId: readString(segmentRecord, "speaker") ?? undefined,
101
+ confidence: readNumber(segmentRecord, "confidence"),
102
+ wordTimings: parseWordTimings(segmentRecord.words)
103
+ });
104
+ }
105
+ return parsed;
106
+ }
107
+ function parseWordTimings(value) {
108
+ if (!Array.isArray(value)) {
109
+ return [];
110
+ }
111
+ const words = [];
112
+ for (const entry of value) {
113
+ const wordRecord = asRecord(entry);
114
+ const word = readString(wordRecord, "word");
115
+ const startSeconds = readNumber(wordRecord, "start");
116
+ const endSeconds = readNumber(wordRecord, "end");
117
+ if (!word || startSeconds == null || endSeconds == null) {
118
+ continue;
119
+ }
120
+ words.push({
121
+ word,
122
+ startMs: secondsToMs(startSeconds),
123
+ endMs: secondsToMs(endSeconds),
124
+ confidence: readNumber(wordRecord, "confidence")
125
+ });
126
+ }
127
+ return words;
128
+ }
129
+ function dedupeSpeakers(segments) {
130
+ const seen = new Set;
131
+ const speakers = [];
132
+ for (const segment of segments) {
133
+ if (!segment.speakerId || seen.has(segment.speakerId)) {
134
+ continue;
135
+ }
136
+ seen.add(segment.speakerId);
137
+ speakers.push({
138
+ id: segment.speakerId,
139
+ name: segment.speakerName
140
+ });
141
+ }
142
+ return speakers;
143
+ }
144
+ function normalizeBaseUrl(url) {
145
+ return url.endsWith("/") ? url.slice(0, -1) : url;
146
+ }
147
+ function asRecord(value) {
148
+ if (value && typeof value === "object") {
149
+ return value;
150
+ }
151
+ return {};
152
+ }
153
+ function readString(record, key) {
154
+ const value = record[key];
155
+ return typeof value === "string" ? value : undefined;
156
+ }
157
+ function readNumber(record, key) {
158
+ const value = record[key];
159
+ return typeof value === "number" ? value : undefined;
160
+ }
161
+ function secondsToMs(value) {
162
+ return Math.round(value * 1000);
163
+ }
164
+ export {
165
+ MistralSttProvider
166
+ };