@srmico/dsh-tinfoil-audio 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +81 -2
- package/cordis.patch.yml +3 -0
- package/icon.svg +6 -0
- package/lib/index.d.ts +23 -0
- package/lib/index.js +1048 -0
- package/locale/en.json +6 -0
- package/package.json +74 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 srmico
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,82 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @srmico/dsh-tinfoil-audio
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Audio transcription and text-to-speech for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness)
|
|
4
|
+
through Tinfoil's attested audio endpoints.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
# All Tinfoil capabilities:
|
|
10
|
+
dsh plugin add @srmico/dsh-tinfoil
|
|
11
|
+
|
|
12
|
+
# Audio alone:
|
|
13
|
+
dsh plugin add @srmico/dsh-tinfoil-audio
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The package is a plugin and a bundle, so a standalone install activates its row. Do not install the
|
|
17
|
+
root bundle and this package together; the duplicate row id becomes unaddressable in the Plugins UI.
|
|
18
|
+
|
|
19
|
+
## Tools
|
|
20
|
+
|
|
21
|
+
| Tool | Arguments | Returns |
|
|
22
|
+
| --- | --- | --- |
|
|
23
|
+
| `transcribe_audio` | `file_path`, optional `model`, `language`, `prompt`, `temperature` | The complete transcript. |
|
|
24
|
+
| `transcribe_audio_to_file` | `file_path`, `output_path`, same options, optional `response_format` | Paths, byte counts, model, and format; no transcript. |
|
|
25
|
+
| `synthesize_speech` | `text`, optional `model`, `voice`, `file_name` | Saved audio path, bytes, media type, model, voice, and optional duration. |
|
|
26
|
+
|
|
27
|
+
Supported transcription extensions: `.flac`, `.mp3`, `.mp4`, `.mpeg`, `.mpga`, `.m4a`, `.ogg`,
|
|
28
|
+
`.wav`, `.webm`. The current Web UI does not play the generated audio; open the returned path.
|
|
29
|
+
|
|
30
|
+
## Requirements
|
|
31
|
+
|
|
32
|
+
- DSH `>=0.2.2-rc.1 <0.3.0`, Node `^24.22.1 || >=26.12.1`.
|
|
33
|
+
- A profile with `tools`; transcription also needs `fs` and `workingDirectory`; speech needs
|
|
34
|
+
`attachments`.
|
|
35
|
+
- `TINFOIL_API_KEY` in the launch environment or `$DSH_HOME/.env`.
|
|
36
|
+
|
|
37
|
+
## Configuration
|
|
38
|
+
|
|
39
|
+
```yaml
|
|
40
|
+
- id: tinfoil-audio
|
|
41
|
+
config:
|
|
42
|
+
speechModel: qwen3-tts
|
|
43
|
+
defaultVoice: serena
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
| Key | Default | Meaning |
|
|
47
|
+
| --- | --- | --- |
|
|
48
|
+
| `apiKeyEnv` | `TINFOIL_API_KEY` | Environment variable holding the key. |
|
|
49
|
+
| `transcriptionModel` | `whisper-large-v3-turbo` | Default speech-to-text model. |
|
|
50
|
+
| `speechModel` | `qwen3-tts` | Default text-to-speech model. |
|
|
51
|
+
| `defaultVoice` | `serena` | Default voice. Voices are model-specific. |
|
|
52
|
+
| `defaultLanguage` | unset | Language hint when a call omits one. |
|
|
53
|
+
| `defaultTranscriptFormat` | `text` | Format written by `transcribe_audio_to_file`. |
|
|
54
|
+
| `speechFormat` | `wav` | Requested speech format. |
|
|
55
|
+
| `maxInputBytes` | `26214400` (25 MiB) | Transcription input cap. |
|
|
56
|
+
| `maxSpeechChars` | `4096` | Speech input cap; the hosted talker context is shared with generated audio. |
|
|
57
|
+
| `maxResponseBytes` | `33554432` (32 MiB) | Response cap. |
|
|
58
|
+
| `requestTimeoutMs` | `600000` | Deadline, including attestation. |
|
|
59
|
+
| `includeProviderErrorDetail` | `false` | Include bounded provider detail in model-visible errors. Treat it as untrusted. |
|
|
60
|
+
|
|
61
|
+
## Security and attestation
|
|
62
|
+
|
|
63
|
+
All calls use Tinfoil's `SecureClient` and the attested router. There is no endpoint, self-hosted, or
|
|
64
|
+
unattested mode. Audio bytes, transcripts, and requested speech text are never logged; no requests or
|
|
65
|
+
responses are cached. Generated speech is a durable attachment under the host's attachment root and
|
|
66
|
+
should be treated as sensitive.
|
|
67
|
+
|
|
68
|
+
## Limitations
|
|
69
|
+
|
|
70
|
+
- Hosted-only; no realtime streaming, translations, or speech-provider registration.
|
|
71
|
+
- Voices are model-specific; validate a configured voice against the selected speech model.
|
|
72
|
+
- TTS input is capped because the talker context is shared with generated audio.
|
|
73
|
+
- No automated tests yet.
|
|
74
|
+
|
|
75
|
+
## Development
|
|
76
|
+
|
|
77
|
+
Build and typecheck from the repository root; see
|
|
78
|
+
[Development](https://github.com/srmico/dsh-tinfoil/blob/main/docs/DEVELOPMENT.md).
|
|
79
|
+
|
|
80
|
+
## License
|
|
81
|
+
|
|
82
|
+
MIT.
|
package/cordis.patch.yml
ADDED
package/icon.svg
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" role="img" aria-label="Tinfoil audio">
|
|
2
|
+
<rect width="64" height="64" rx="14" fill="#0f172a"/>
|
|
3
|
+
<path d="M32 9 51 17v14.5C51 43 43 51.5 32 56 21 51.5 13 43 13 31.5V17z" fill="#e2e8f0"/>
|
|
4
|
+
<path d="M32 17 44 22v9.5C44 39 38.7 44.5 32 47.5 25.3 44.5 20 39 20 31.5V22z" fill="#0ea5e9"/>
|
|
5
|
+
<path d="M24 32v4M28 28v12M32 24v20M36 28v12M40 32v4" stroke="#0f172a" stroke-width="2.5" stroke-linecap="round"/>
|
|
6
|
+
</svg>
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import z from "@deepseek-ai/schemastery";
|
|
2
|
+
import { Context } from "@deepseek-ai/cordis";
|
|
3
|
+
type TranscriptFormat = 'text' | 'json' | 'verbose_json';
|
|
4
|
+
type SpeechFormat = 'wav' | 'mp3' | 'flac' | 'opus' | 'pcm';
|
|
5
|
+
export interface Config {
|
|
6
|
+
apiKeyEnv?: string;
|
|
7
|
+
transcriptionModel?: string;
|
|
8
|
+
speechModel?: string;
|
|
9
|
+
defaultVoice?: string;
|
|
10
|
+
defaultLanguage?: string;
|
|
11
|
+
defaultTranscriptFormat?: TranscriptFormat;
|
|
12
|
+
speechFormat?: SpeechFormat;
|
|
13
|
+
maxInputBytes?: number;
|
|
14
|
+
maxSpeechChars?: number;
|
|
15
|
+
maxResponseBytes?: number;
|
|
16
|
+
requestTimeoutMs?: number;
|
|
17
|
+
includeProviderErrorDetail?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export declare const Config: z<Config>;
|
|
20
|
+
export declare const name = "tinfoil-audio";
|
|
21
|
+
export declare const inject: string[];
|
|
22
|
+
export declare function apply(ctx: Context, config?: Config): void;
|
|
23
|
+
export type { Config as TinfoilAudioConfig };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,1048 @@
|
|
|
1
|
+
import { HarnessError } from "@deepseek-ai/dsh-llm";
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
3
|
+
import { basename } from "node:path";
|
|
4
|
+
import z from "@deepseek-ai/schemastery";
|
|
5
|
+
import { FsError } from "@deepseek-ai/dsh-fs";
|
|
6
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
7
|
+
// ../dsh-tinfoil-lib/lib/errors.js
|
|
8
|
+
/**
|
|
9
|
+
* Base error for Tinfoil transport and attestation failures.
|
|
10
|
+
*
|
|
11
|
+
* Plugin packages subclass it with their own code union so callers keep
|
|
12
|
+
* domain-specific codes while sharing the transport implementation.
|
|
13
|
+
*/
|
|
14
|
+
var TinfoilError = class extends HarnessError {
|
|
15
|
+
code;
|
|
16
|
+
constructor(message, code, options) {
|
|
17
|
+
super(message, code, options);
|
|
18
|
+
this.code = code;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
// ../dsh-tinfoil-lib/lib/config.js
|
|
22
|
+
const DEFAULT_API_KEY_ENV = "TINFOIL_API_KEY";
|
|
23
|
+
/** Resolve a configured environment-variable name. */
|
|
24
|
+
function resolveApiKeyEnv(value, label) {
|
|
25
|
+
const env = (value ?? "TINFOIL_API_KEY").trim();
|
|
26
|
+
if (env.length === 0) throw new Error(`${label} must not be empty`);
|
|
27
|
+
return env;
|
|
28
|
+
}
|
|
29
|
+
/** Require a positive integer. */
|
|
30
|
+
function positiveInteger(value, label) {
|
|
31
|
+
if (!Number.isInteger(value) || value < 1) throw new Error(`${label} must be a positive integer`);
|
|
32
|
+
return value;
|
|
33
|
+
}
|
|
34
|
+
/** Require a non-empty trimmed string. */
|
|
35
|
+
function nonEmptyString(value, label) {
|
|
36
|
+
const trimmed = value.trim();
|
|
37
|
+
if (trimmed.length === 0) throw new Error(`${label} must not be empty`);
|
|
38
|
+
return trimmed;
|
|
39
|
+
}
|
|
40
|
+
// ../dsh-tinfoil-lib/lib/http.js
|
|
41
|
+
const DEFAULT_DETAIL_CHARS = 300;
|
|
42
|
+
const DEFAULT_ERROR_DETAIL_BYTES = 4096;
|
|
43
|
+
const UNSAFE_DETAIL = /[\u0000-\u001f\u007f]+/gu;
|
|
44
|
+
/** Bound and flatten a provider or SDK detail for model-visible output. */
|
|
45
|
+
function sanitizeDetail(value, maxChars = DEFAULT_DETAIL_CHARS) {
|
|
46
|
+
return (typeof value === "string" ? value : value instanceof Error ? value.message : value === void 0 || value === null ? "" : String(value)).replace(UNSAFE_DETAIL, " ").replace(/\s+/gu, " ").trim().slice(0, maxChars);
|
|
47
|
+
}
|
|
48
|
+
/** Sanitized message of an error, or `unknown error`. */
|
|
49
|
+
function errorDetail(error) {
|
|
50
|
+
return error instanceof Error ? sanitizeDetail(error.message) : "unknown error";
|
|
51
|
+
}
|
|
52
|
+
/** Race a promise against an optional cancellation signal. */
|
|
53
|
+
async function raceWithSignal(promise, signal) {
|
|
54
|
+
if (signal === void 0) return await promise;
|
|
55
|
+
signal.throwIfAborted();
|
|
56
|
+
let detach = () => {};
|
|
57
|
+
const aborted = new Promise((_resolve, reject) => {
|
|
58
|
+
const onAbort = () => reject(signal.reason);
|
|
59
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
60
|
+
detach = () => signal.removeEventListener("abort", onAbort);
|
|
61
|
+
if (signal.aborted) onAbort();
|
|
62
|
+
});
|
|
63
|
+
try {
|
|
64
|
+
return await Promise.race([promise, aborted]);
|
|
65
|
+
} finally {
|
|
66
|
+
detach();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
async function readCapped(response, options) {
|
|
70
|
+
const limit = options.limit;
|
|
71
|
+
const lengthHeader = response.headers.get("content-length");
|
|
72
|
+
if (lengthHeader !== null) {
|
|
73
|
+
const length = Number(lengthHeader);
|
|
74
|
+
if (Number.isFinite(length) && length > limit) {
|
|
75
|
+
const body = response.body;
|
|
76
|
+
if (body !== null) await body.cancel().catch(() => {});
|
|
77
|
+
throw options.tooLarge();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const body = response.body;
|
|
81
|
+
if (body === null) return /* @__PURE__ */ new Uint8Array();
|
|
82
|
+
const reader = body.getReader();
|
|
83
|
+
const chunks = [];
|
|
84
|
+
let total = 0;
|
|
85
|
+
try {
|
|
86
|
+
while (true) {
|
|
87
|
+
const { done, value } = await reader.read();
|
|
88
|
+
if (done) break;
|
|
89
|
+
if (value === void 0) continue;
|
|
90
|
+
total += value.byteLength;
|
|
91
|
+
if (total > limit) {
|
|
92
|
+
await reader.cancel();
|
|
93
|
+
throw options.tooLarge();
|
|
94
|
+
}
|
|
95
|
+
chunks.push(value);
|
|
96
|
+
}
|
|
97
|
+
} finally {
|
|
98
|
+
try {
|
|
99
|
+
reader.releaseLock();
|
|
100
|
+
} catch {}
|
|
101
|
+
}
|
|
102
|
+
return Buffer.concat(chunks, total);
|
|
103
|
+
}
|
|
104
|
+
/** Read a bounded response body as UTF-8 text. */
|
|
105
|
+
async function readTextCapped(response, options) {
|
|
106
|
+
return Buffer.from(await readCapped(response, options)).toString("utf8");
|
|
107
|
+
}
|
|
108
|
+
/** Read a bounded response body as bytes. */
|
|
109
|
+
async function readBytesCapped(response, options) {
|
|
110
|
+
return await readCapped(response, options);
|
|
111
|
+
}
|
|
112
|
+
/** Read at most `maxBytes` from a response body, cancelling the remainder. */
|
|
113
|
+
async function readTextPrefix(response, maxBytes) {
|
|
114
|
+
const body = response.body;
|
|
115
|
+
if (body === null) return "";
|
|
116
|
+
const reader = body.getReader();
|
|
117
|
+
const chunks = [];
|
|
118
|
+
let total = 0;
|
|
119
|
+
try {
|
|
120
|
+
while (total < maxBytes) {
|
|
121
|
+
const { done, value } = await reader.read();
|
|
122
|
+
if (done) break;
|
|
123
|
+
if (value === void 0) continue;
|
|
124
|
+
const remaining = maxBytes - total;
|
|
125
|
+
const chunk = value.byteLength > remaining ? value.subarray(0, remaining) : value;
|
|
126
|
+
chunks.push(chunk);
|
|
127
|
+
total += chunk.byteLength;
|
|
128
|
+
if (chunk.byteLength < value.byteLength) {
|
|
129
|
+
await reader.cancel();
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
} finally {
|
|
134
|
+
try {
|
|
135
|
+
reader.releaseLock();
|
|
136
|
+
} catch {}
|
|
137
|
+
}
|
|
138
|
+
return Buffer.concat(chunks, total).toString("utf8");
|
|
139
|
+
}
|
|
140
|
+
function stringField(value) {
|
|
141
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Read one provider error body once: always extract a bounded error code,
|
|
145
|
+
* include the detail only when requested.
|
|
146
|
+
*/
|
|
147
|
+
async function readProviderErrorBody(response, options = {}) {
|
|
148
|
+
const text = await readTextPrefix(response, options.limitBytes ?? DEFAULT_ERROR_DETAIL_BYTES);
|
|
149
|
+
let code;
|
|
150
|
+
let detail;
|
|
151
|
+
try {
|
|
152
|
+
const parsed = JSON.parse(text);
|
|
153
|
+
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
|
|
154
|
+
const record = parsed;
|
|
155
|
+
code = stringField(record.code) ?? stringField(record.type);
|
|
156
|
+
const error = record.error;
|
|
157
|
+
if (typeof error === "string") detail = error;
|
|
158
|
+
else if (typeof error === "object" && error !== null && !Array.isArray(error)) {
|
|
159
|
+
const nested = error;
|
|
160
|
+
code = stringField(nested.code) ?? stringField(nested.type) ?? code;
|
|
161
|
+
detail = stringField(nested.message);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
} catch {}
|
|
165
|
+
if (options.includeDetail === true) detail = detail === void 0 || detail.length === 0 ? text : detail;
|
|
166
|
+
else detail = void 0;
|
|
167
|
+
return {
|
|
168
|
+
...code === void 0 ? {} : { code: sanitizeDetail(code) },
|
|
169
|
+
detail: detail === void 0 ? "" : sanitizeDetail(detail)
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/** Classify a provider HTTP status and optional provider error code. */
|
|
173
|
+
function classifyHttpStatus(status, code) {
|
|
174
|
+
if (status === 401 || status === 403) return "auth";
|
|
175
|
+
if (status === 402) return "billing";
|
|
176
|
+
if (status === 404) return "not-found";
|
|
177
|
+
if (status === 413) return "too-large";
|
|
178
|
+
if (status === 429) return code !== void 0 && /quota|insufficient_quota/iu.test(code) ? "quota" : "rate-limit";
|
|
179
|
+
if (status === 400 || status === 422) return "request-rejected";
|
|
180
|
+
if (status >= 500) return "unavailable";
|
|
181
|
+
return "failed";
|
|
182
|
+
}
|
|
183
|
+
// ../dsh-tinfoil-lib/lib/secure-client.js
|
|
184
|
+
function timeoutSeconds(ms) {
|
|
185
|
+
return Math.max(1, Math.round(ms / 1e3));
|
|
186
|
+
}
|
|
187
|
+
function mapSdkError(module, error, label, fail) {
|
|
188
|
+
const message = errorDetail(error);
|
|
189
|
+
if (error instanceof module.AttestationError) return fail(`${label} enclave attestation failed; no request body was sent. ${message}`, "TINFOIL_ATTESTATION_FAILED", { cause: error });
|
|
190
|
+
if (error instanceof module.ConfigurationError) return fail(`${label} client configuration failed: ${message}`, "TINFOIL_CONFIGURATION_FAILED", { cause: error });
|
|
191
|
+
if (error instanceof module.TinfoilError) return fail(`${label} client failed: ${message}`, "TINFOIL_UNAVAILABLE", { cause: error });
|
|
192
|
+
return fail(`Could not reach Tinfoil: ${message}`, "TINFOIL_UNAVAILABLE", { cause: error });
|
|
193
|
+
}
|
|
194
|
+
function sdkModuleError(fail, error) {
|
|
195
|
+
return fail("The Tinfoil SDK could not be loaded. Run pnpm install and rebuild the plugin.", "TINFOIL_UNAVAILABLE", { cause: error });
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Authenticated client for one attested Tinfoil endpoint.
|
|
199
|
+
*
|
|
200
|
+
* Owns the lazy SDK import, client identity, timeout composition, auth header,
|
|
201
|
+
* redirect policy, and error mapping. Endpoint-specific request bodies and
|
|
202
|
+
* response parsing stay in the plugin.
|
|
203
|
+
*/
|
|
204
|
+
var TinfoilSecureClient = class {
|
|
205
|
+
options;
|
|
206
|
+
modulePromise;
|
|
207
|
+
client;
|
|
208
|
+
constructor(options) {
|
|
209
|
+
this.options = options;
|
|
210
|
+
}
|
|
211
|
+
/** Read and validate the configured API key. */
|
|
212
|
+
assertApiKey() {
|
|
213
|
+
const key = process.env[this.options.apiKeyEnv]?.trim();
|
|
214
|
+
if (key === void 0 || key.length === 0) throw this.options.error(`Tinfoil API key is not set. Export ${this.options.apiKeyEnv} or add it to $DSH_HOME/.env.`, "TINFOIL_API_KEY_MISSING");
|
|
215
|
+
return key;
|
|
216
|
+
}
|
|
217
|
+
/** Run one authenticated request with a deadline and abort propagation. */
|
|
218
|
+
async request(path, init, signal, auth = true, timeoutMs) {
|
|
219
|
+
const deadlineMs = timeoutMs ?? this.options.requestTimeoutMs;
|
|
220
|
+
const timeout = AbortSignal.timeout(deadlineMs);
|
|
221
|
+
const combined = AbortSignal.any([signal, timeout]);
|
|
222
|
+
let module;
|
|
223
|
+
try {
|
|
224
|
+
const loaded = await this.loadModule();
|
|
225
|
+
module = loaded;
|
|
226
|
+
const client = this.ensureClient(loaded);
|
|
227
|
+
await this.awaitReady(client, combined);
|
|
228
|
+
const headers = new Headers(init.headers);
|
|
229
|
+
if (auth) headers.set("Authorization", `Bearer ${this.assertApiKey()}`);
|
|
230
|
+
return await client.fetch(path, {
|
|
231
|
+
...init,
|
|
232
|
+
headers,
|
|
233
|
+
redirect: "error",
|
|
234
|
+
signal: combined
|
|
235
|
+
});
|
|
236
|
+
} catch (error) {
|
|
237
|
+
if (error instanceof TinfoilError) throw error;
|
|
238
|
+
signal.throwIfAborted();
|
|
239
|
+
if (timeout.aborted) throw this.options.error(`${this.options.label} timed out after ${timeoutSeconds(deadlineMs)} seconds.`, "TINFOIL_TIMEOUT", { cause: error });
|
|
240
|
+
if (module === void 0) throw this.options.error(`Could not reach Tinfoil: ${errorDetail(error)}`, "TINFOIL_UNAVAILABLE", { cause: error });
|
|
241
|
+
throw mapSdkError(module, error, this.options.label, this.options.error);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/** Complete attestation and resolve once the client is usable. */
|
|
245
|
+
async ready(signal) {
|
|
246
|
+
const module = await this.loadModule();
|
|
247
|
+
const client = this.ensureClient(module);
|
|
248
|
+
try {
|
|
249
|
+
await this.awaitReady(client, signal);
|
|
250
|
+
} catch (error) {
|
|
251
|
+
if (error instanceof TinfoilError) throw error;
|
|
252
|
+
signal.throwIfAborted();
|
|
253
|
+
throw mapSdkError(module, error, this.options.label, this.options.error);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/** Current verification document, or undefined before the client is created. */
|
|
257
|
+
verification() {
|
|
258
|
+
return this.client?.getVerificationDocument();
|
|
259
|
+
}
|
|
260
|
+
/** Attest this client's enclave and return its verification document. */
|
|
261
|
+
async attest(signal) {
|
|
262
|
+
const module = await this.loadModule();
|
|
263
|
+
const client = this.ensureClient(module);
|
|
264
|
+
try {
|
|
265
|
+
await this.awaitReady(client, signal);
|
|
266
|
+
} catch (error) {
|
|
267
|
+
if (error instanceof TinfoilError) throw error;
|
|
268
|
+
signal.throwIfAborted();
|
|
269
|
+
throw mapSdkError(module, error, this.options.label, this.options.error);
|
|
270
|
+
}
|
|
271
|
+
return client.getVerificationDocument();
|
|
272
|
+
}
|
|
273
|
+
async loadModule() {
|
|
274
|
+
this.modulePromise ??= import("tinfoil");
|
|
275
|
+
try {
|
|
276
|
+
return await this.modulePromise;
|
|
277
|
+
} catch (error) {
|
|
278
|
+
this.modulePromise = void 0;
|
|
279
|
+
throw sdkModuleError(this.options.error, error);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
ensureClient(module) {
|
|
283
|
+
if (this.client !== void 0) return this.client;
|
|
284
|
+
const endpoint = this.options.endpoint;
|
|
285
|
+
this.client = endpoint === void 0 ? new module.SecureClient() : new module.SecureClient({
|
|
286
|
+
...endpoint.baseURL === void 0 ? {} : { baseURL: endpoint.baseURL },
|
|
287
|
+
...endpoint.enclaveURL === void 0 ? {} : { enclaveURL: endpoint.enclaveURL },
|
|
288
|
+
...endpoint.configRepo === void 0 ? {} : { configRepo: endpoint.configRepo }
|
|
289
|
+
});
|
|
290
|
+
return this.client;
|
|
291
|
+
}
|
|
292
|
+
async awaitReady(client, signal) {
|
|
293
|
+
const ready = client.ready();
|
|
294
|
+
ready.catch(() => {});
|
|
295
|
+
await raceWithSignal(ready, signal);
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
// ../dsh-tinfoil-lib/lib/attestation.js
|
|
299
|
+
/**
|
|
300
|
+
* Publish one plugin's verification surface when the verification plugin is mounted.
|
|
301
|
+
* @param ctx - host plugin context.
|
|
302
|
+
* @param target - this plugin's attestation target.
|
|
303
|
+
*/
|
|
304
|
+
function registerTinfoilAttestation(ctx, target) {
|
|
305
|
+
ctx.inject(["tinfoilAttestation"], (attestationCtx) => {
|
|
306
|
+
attestationCtx.effect(() => attestationCtx.tinfoilAttestation.register(target), `tinfoil ${target.id} attestation`);
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
// src/audio.ts
|
|
310
|
+
const AUDIO_MEDIA_TYPES = {
|
|
311
|
+
".flac": "audio/flac",
|
|
312
|
+
".mp3": "audio/mpeg",
|
|
313
|
+
".mp4": "audio/mp4",
|
|
314
|
+
".mpeg": "audio/mpeg",
|
|
315
|
+
".mpga": "audio/mpeg",
|
|
316
|
+
".m4a": "audio/mp4",
|
|
317
|
+
".ogg": "audio/ogg",
|
|
318
|
+
".wav": "audio/wav",
|
|
319
|
+
".webm": "audio/webm"
|
|
320
|
+
};
|
|
321
|
+
const SUPPORTED_AUDIO_EXTENSIONS = Object.keys(AUDIO_MEDIA_TYPES);
|
|
322
|
+
const AUDIO_EXTENSION_SET = new Set(SUPPORTED_AUDIO_EXTENSIONS);
|
|
323
|
+
const UNSAFE_FILENAME = /[\u0000-\u001f\u007f"\\/:*?<>|]+/gu;
|
|
324
|
+
const SUBTYPE = /[^a-z0-9]+/gu;
|
|
325
|
+
function ascii(data, start, length) {
|
|
326
|
+
return Buffer.from(data.subarray(start, start + length)).toString("latin1");
|
|
327
|
+
}
|
|
328
|
+
function isRiffWave(data) {
|
|
329
|
+
return data.byteLength >= 12 && ascii(data, 0, 4) === "RIFF" && ascii(data, 8, 4) === "WAVE";
|
|
330
|
+
}
|
|
331
|
+
function isMpeg(data) {
|
|
332
|
+
if (data.byteLength >= 3 && ascii(data, 0, 3) === "ID3") return true;
|
|
333
|
+
const first = data[0];
|
|
334
|
+
const second = data[1];
|
|
335
|
+
return first !== void 0 && second !== void 0 && first === 255 && (second & 224) === 224;
|
|
336
|
+
}
|
|
337
|
+
function isFlac(data) {
|
|
338
|
+
return data.byteLength >= 4 && ascii(data, 0, 4) === "fLaC";
|
|
339
|
+
}
|
|
340
|
+
function isOgg(data) {
|
|
341
|
+
return data.byteLength >= 4 && ascii(data, 0, 4) === "OggS";
|
|
342
|
+
}
|
|
343
|
+
function isWebm(data) {
|
|
344
|
+
if (data.byteLength < 4) return false;
|
|
345
|
+
return data[0] === 26 && data[1] === 69 && data[2] === 223 && data[3] === 163;
|
|
346
|
+
}
|
|
347
|
+
function requiresMagic(contentType) {
|
|
348
|
+
return contentType === "audio/wav" || contentType === "audio/x-wav" || contentType === "audio/mpeg" || contentType === "audio/mp3" || contentType === "audio/flac" || contentType === "audio/ogg" || contentType === "audio/webm" || contentType === "audio/opus";
|
|
349
|
+
}
|
|
350
|
+
function audioExtension(filePath) {
|
|
351
|
+
const dot = filePath.lastIndexOf(".");
|
|
352
|
+
if (dot < 0) return void 0;
|
|
353
|
+
const extension = filePath.slice(dot).toLowerCase();
|
|
354
|
+
return AUDIO_EXTENSION_SET.has(extension) ? extension : void 0;
|
|
355
|
+
}
|
|
356
|
+
function audioMediaType(extension) {
|
|
357
|
+
return AUDIO_MEDIA_TYPES[extension];
|
|
358
|
+
}
|
|
359
|
+
function sanitizeAudioFilename(filePath, extension) {
|
|
360
|
+
const bounded = basename(filePath).replace(UNSAFE_FILENAME, "_").trim().slice(0, 180);
|
|
361
|
+
if (bounded.length === 0) return `audio${extension}`;
|
|
362
|
+
return bounded.toLowerCase().endsWith(extension) ? bounded : `${bounded}${extension}`;
|
|
363
|
+
}
|
|
364
|
+
function sanitizeLeafName(value) {
|
|
365
|
+
const bounded = basename(value).replace(UNSAFE_FILENAME, "_").trim().slice(0, 180);
|
|
366
|
+
return bounded.length === 0 || bounded === "." || bounded === ".." ? "speech" : bounded;
|
|
367
|
+
}
|
|
368
|
+
function speechFileName(value, extension) {
|
|
369
|
+
const sanitized = sanitizeLeafName(value ?? "speech");
|
|
370
|
+
const dot = sanitized.lastIndexOf(".");
|
|
371
|
+
const stem = dot > 0 ? sanitized.slice(0, dot) : sanitized;
|
|
372
|
+
return `${stem.length === 0 ? "speech" : stem}.${extension}`;
|
|
373
|
+
}
|
|
374
|
+
function parseContentType(value) {
|
|
375
|
+
if (value === null) return void 0;
|
|
376
|
+
const type = value.split(";", 1)[0]?.trim().toLowerCase();
|
|
377
|
+
return type === void 0 || type.length === 0 ? void 0 : type;
|
|
378
|
+
}
|
|
379
|
+
function sniffAudioMediaType(prefix, contentType) {
|
|
380
|
+
const type = parseContentType(contentType ?? null);
|
|
381
|
+
if (isRiffWave(prefix)) return "audio/wav";
|
|
382
|
+
if (isMpeg(prefix)) return "audio/mpeg";
|
|
383
|
+
if (isFlac(prefix)) return "audio/flac";
|
|
384
|
+
if (isOgg(prefix)) return type === "audio/opus" ? "audio/opus" : "audio/ogg";
|
|
385
|
+
if (isWebm(prefix)) return "audio/webm";
|
|
386
|
+
if (type === "audio/pcm") return type;
|
|
387
|
+
if (type !== void 0 && type.startsWith("audio/") && !requiresMagic(type)) return type;
|
|
388
|
+
}
|
|
389
|
+
function audioExtensionForMediaType(mediaType) {
|
|
390
|
+
const type = parseContentType(mediaType) ?? "audio/wav";
|
|
391
|
+
switch (type) {
|
|
392
|
+
case "audio/wav":
|
|
393
|
+
case "audio/x-wav": return "wav";
|
|
394
|
+
case "audio/mpeg":
|
|
395
|
+
case "audio/mp3": return "mp3";
|
|
396
|
+
case "audio/flac": return "flac";
|
|
397
|
+
case "audio/ogg": return "ogg";
|
|
398
|
+
case "audio/opus": return "opus";
|
|
399
|
+
case "audio/webm": return "webm";
|
|
400
|
+
case "audio/pcm": return "pcm";
|
|
401
|
+
default: {
|
|
402
|
+
const subtype = type.split("/")[1]?.toLowerCase().replace(SUBTYPE, "");
|
|
403
|
+
return subtype === void 0 || subtype.length === 0 ? "wav" : subtype;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
function wavDurationSeconds(data) {
|
|
408
|
+
if (data.byteLength < 12 || !isRiffWave(data)) return void 0;
|
|
409
|
+
const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
410
|
+
let offset = 12;
|
|
411
|
+
let byteRate = 0;
|
|
412
|
+
let dataSize = 0;
|
|
413
|
+
while (offset + 8 <= data.byteLength) {
|
|
414
|
+
const id = ascii(data, offset, 4);
|
|
415
|
+
const size = view.getUint32(offset + 4, true);
|
|
416
|
+
const start = offset + 8;
|
|
417
|
+
if (id === "fmt " && size >= 16 && start + 16 <= data.byteLength) {
|
|
418
|
+
const channels = view.getUint16(start + 2, true);
|
|
419
|
+
const sampleRate = view.getUint32(start + 4, true);
|
|
420
|
+
byteRate = view.getUint32(start + 8, true);
|
|
421
|
+
if (channels === 0 || sampleRate === 0 || byteRate === 0) return void 0;
|
|
422
|
+
}
|
|
423
|
+
if (id === "data") {
|
|
424
|
+
dataSize = size;
|
|
425
|
+
break;
|
|
426
|
+
}
|
|
427
|
+
const next = start + size + size % 2;
|
|
428
|
+
if (next <= offset) return void 0;
|
|
429
|
+
offset = next;
|
|
430
|
+
}
|
|
431
|
+
if (byteRate <= 0 || dataSize <= 0 || dataSize === 4294967295) return void 0;
|
|
432
|
+
return dataSize / byteRate;
|
|
433
|
+
}
|
|
434
|
+
// src/errors.ts
|
|
435
|
+
var TinfoilAudioError = class extends TinfoilError {};
|
|
436
|
+
// src/payload.ts
|
|
437
|
+
function isRecord(value) {
|
|
438
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
439
|
+
}
|
|
440
|
+
function invalidResponse$1(cause) {
|
|
441
|
+
return new TinfoilAudioError("Tinfoil audio returned an unexpected response.", "TINFOIL_RESPONSE_INVALID", cause === void 0 ? void 0 : { cause });
|
|
442
|
+
}
|
|
443
|
+
function parseTranscriptionBody(body, format) {
|
|
444
|
+
if (format === "text") return body;
|
|
445
|
+
let value;
|
|
446
|
+
try {
|
|
447
|
+
value = JSON.parse(body);
|
|
448
|
+
} catch (error) {
|
|
449
|
+
throw invalidResponse$1(error);
|
|
450
|
+
}
|
|
451
|
+
if (!isRecord(value) || typeof value.text !== "string") throw invalidResponse$1();
|
|
452
|
+
return value.text;
|
|
453
|
+
}
|
|
454
|
+
function assertSpeechInput(text, maxChars) {
|
|
455
|
+
if (text.trim().length === 0) throw new TinfoilAudioError("The text to synthesize must not be empty.", "INPUT_EMPTY");
|
|
456
|
+
if (text.length > maxChars) throw new TinfoilAudioError(`Text is ${text.length} characters, which exceeds maxSpeechChars (${maxChars}). The hosted qwen3-tts talker has a 4096-token context shared by the input text and generated audio, so a single request cannot synthesize arbitrarily long input. Split the text at sentence or paragraph boundaries.`, "INPUT_TOO_LARGE");
|
|
457
|
+
}
|
|
458
|
+
// src/client.ts
|
|
459
|
+
const TRANSCRIBE_PATH = "/v1/audio/transcriptions";
|
|
460
|
+
const SPEECH_PATH = "/v1/audio/speech";
|
|
461
|
+
const AUDIO_PREFIX_BYTES = 65536;
|
|
462
|
+
function audioError(message, code, options) {
|
|
463
|
+
return new TinfoilAudioError(message, code, options);
|
|
464
|
+
}
|
|
465
|
+
function invalidResponse() {
|
|
466
|
+
return new TinfoilAudioError("Tinfoil audio returned an unexpected response.", "TINFOIL_RESPONSE_INVALID");
|
|
467
|
+
}
|
|
468
|
+
function parseDurationHeader(value) {
|
|
469
|
+
if (value === null) return void 0;
|
|
470
|
+
const duration = Number(value);
|
|
471
|
+
return Number.isFinite(duration) && duration > 0 ? duration : void 0;
|
|
472
|
+
}
|
|
473
|
+
var TinfoilAudioClient = class {
|
|
474
|
+
client;
|
|
475
|
+
options;
|
|
476
|
+
constructor(options) {
|
|
477
|
+
this.options = options;
|
|
478
|
+
this.client = new TinfoilSecureClient({
|
|
479
|
+
apiKeyEnv: options.apiKeyEnv,
|
|
480
|
+
requestTimeoutMs: options.requestTimeoutMs,
|
|
481
|
+
label: "Tinfoil audio",
|
|
482
|
+
error: audioError
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
assertApiKey() {
|
|
486
|
+
return this.client.assertApiKey();
|
|
487
|
+
}
|
|
488
|
+
async transcribe(input) {
|
|
489
|
+
this.client.assertApiKey();
|
|
490
|
+
const form = new FormData();
|
|
491
|
+
const bytes = input.data.slice();
|
|
492
|
+
form.append("file", new Blob([bytes], { type: input.mediaType }), input.filename);
|
|
493
|
+
form.append("model", input.model);
|
|
494
|
+
if (input.language !== void 0) form.append("language", input.language);
|
|
495
|
+
if (input.prompt !== void 0) form.append("prompt", input.prompt);
|
|
496
|
+
if (input.temperature !== void 0) form.append("temperature", String(input.temperature));
|
|
497
|
+
form.append("response_format", input.responseFormat);
|
|
498
|
+
const response = await this.client.request(TRANSCRIBE_PATH, {
|
|
499
|
+
method: "POST",
|
|
500
|
+
body: form
|
|
501
|
+
}, input.signal);
|
|
502
|
+
if (!response.ok) {
|
|
503
|
+
const providerError = await readProviderErrorBody(response, { includeDetail: this.options.includeProviderErrorDetail === true });
|
|
504
|
+
throw this.httpError(response.status, providerError.code, providerError.detail);
|
|
505
|
+
}
|
|
506
|
+
if (parseContentType(response.headers.get("content-type")) === "text/html") throw invalidResponse();
|
|
507
|
+
const text = await readTextCapped(response, {
|
|
508
|
+
limit: this.options.maxResponseBytes,
|
|
509
|
+
tooLarge: () => this.responseTooLarge()
|
|
510
|
+
});
|
|
511
|
+
return {
|
|
512
|
+
text: parseTranscriptionBody(text, input.responseFormat),
|
|
513
|
+
raw: text
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
async synthesize(input) {
|
|
517
|
+
this.client.assertApiKey();
|
|
518
|
+
const body = JSON.stringify({
|
|
519
|
+
model: input.model,
|
|
520
|
+
voice: input.voice,
|
|
521
|
+
input: input.text,
|
|
522
|
+
response_format: input.responseFormat
|
|
523
|
+
});
|
|
524
|
+
const response = await this.client.request(SPEECH_PATH, {
|
|
525
|
+
method: "POST",
|
|
526
|
+
headers: { "Content-Type": "application/json" },
|
|
527
|
+
body
|
|
528
|
+
}, input.signal);
|
|
529
|
+
if (!response.ok) {
|
|
530
|
+
const providerError = await readProviderErrorBody(response, { includeDetail: this.options.includeProviderErrorDetail === true });
|
|
531
|
+
throw this.httpError(response.status, providerError.code, providerError.detail);
|
|
532
|
+
}
|
|
533
|
+
const contentType = parseContentType(response.headers.get("content-type"));
|
|
534
|
+
const data = await readBytesCapped(response, {
|
|
535
|
+
limit: this.options.maxResponseBytes,
|
|
536
|
+
tooLarge: () => this.responseTooLarge()
|
|
537
|
+
});
|
|
538
|
+
if (data.byteLength === 0) throw invalidResponse();
|
|
539
|
+
const mediaType = sniffAudioMediaType(data.subarray(0, AUDIO_PREFIX_BYTES), contentType);
|
|
540
|
+
if (mediaType === void 0) throw invalidResponse();
|
|
541
|
+
const duration = parseDurationHeader(response.headers.get("x-audio-duration-seconds")) ?? wavDurationSeconds(data);
|
|
542
|
+
return {
|
|
543
|
+
data,
|
|
544
|
+
mediaType,
|
|
545
|
+
extension: audioExtensionForMediaType(mediaType),
|
|
546
|
+
...duration === void 0 ? {} : { durationSeconds: duration }
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
/** Current verification document, or undefined before this client is created. */
|
|
550
|
+
verification() {
|
|
551
|
+
return this.client.verification();
|
|
552
|
+
}
|
|
553
|
+
/** Attest the audio enclave and return its verification document. */
|
|
554
|
+
async attest(signal) {
|
|
555
|
+
return await this.client.attest(signal);
|
|
556
|
+
}
|
|
557
|
+
responseTooLarge() {
|
|
558
|
+
return new TinfoilAudioError(`Tinfoil audio response exceeded ${this.options.maxResponseBytes} bytes; no result was returned. Raise maxResponseBytes or use a shorter input.`, "TINFOIL_RESPONSE_TOO_LARGE");
|
|
559
|
+
}
|
|
560
|
+
httpError(status, code, detail) {
|
|
561
|
+
const suffix = detail.length > 0 ? `: ${detail}` : ".";
|
|
562
|
+
switch (classifyHttpStatus(status, code)) {
|
|
563
|
+
case "auth": return new TinfoilAudioError(`Tinfoil audio authentication failed (HTTP ${status}). Check ${this.options.apiKeyEnv}.`, "TINFOIL_AUTH_FAILED");
|
|
564
|
+
case "billing": return new TinfoilAudioError("Tinfoil account billing is paused (HTTP 402). Check the Tinfoil dashboard; retrying will not help.", "TINFOIL_BILLING_PAUSED");
|
|
565
|
+
case "quota": return new TinfoilAudioError("Tinfoil quota is exhausted (HTTP 429). Check the Tinfoil dashboard; retrying will not help.", "TINFOIL_QUOTA_EXCEEDED");
|
|
566
|
+
case "too-large": return new TinfoilAudioError("Tinfoil rejected the audio as too large (HTTP 413). The provider may cap uploads at 25 MB; use a shorter or more compressed file.", "INPUT_TOO_LARGE");
|
|
567
|
+
case "rate-limit": return new TinfoilAudioError("Tinfoil rate limit exceeded (HTTP 429). Retry later.", "TINFOIL_RATE_LIMITED");
|
|
568
|
+
case "request-rejected": return new TinfoilAudioError(`Tinfoil rejected the audio request (HTTP ${status})${suffix}`, "TINFOIL_REQUEST_REJECTED");
|
|
569
|
+
case "unavailable": return new TinfoilAudioError(`Tinfoil audio is unavailable (HTTP ${status}). Retry later.`, "TINFOIL_UNAVAILABLE");
|
|
570
|
+
default: return new TinfoilAudioError(`Tinfoil audio failed (HTTP ${status})${suffix}`, "TINFOIL_AUDIO_FAILED");
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
};
|
|
574
|
+
// src/config.ts
|
|
575
|
+
const Config = z.object({
|
|
576
|
+
apiKeyEnv: z.string().default(DEFAULT_API_KEY_ENV).description("Environment variable holding the Tinfoil API key."),
|
|
577
|
+
transcriptionModel: z.string().default("whisper-large-v3-turbo").description("Default Tinfoil speech-to-text model id."),
|
|
578
|
+
speechModel: z.string().default("qwen3-tts").description("Default Tinfoil text-to-speech model id."),
|
|
579
|
+
defaultVoice: z.string().default("serena").description("Default Tinfoil text-to-speech voice."),
|
|
580
|
+
defaultLanguage: z.string().description("ISO-639-1 language hint used when a transcription call omits language."),
|
|
581
|
+
defaultTranscriptFormat: z.union([
|
|
582
|
+
"text",
|
|
583
|
+
"json",
|
|
584
|
+
"verbose_json"
|
|
585
|
+
]).default("text").description("Default output format for transcribe_audio_to_file."),
|
|
586
|
+
speechFormat: z.union([
|
|
587
|
+
"wav",
|
|
588
|
+
"mp3",
|
|
589
|
+
"flac",
|
|
590
|
+
"opus",
|
|
591
|
+
"pcm"
|
|
592
|
+
]).default("wav").description("Audio format requested from the Tinfoil speech endpoint."),
|
|
593
|
+
maxInputBytes: z.number().default(26214400).description("Maximum audio file size in bytes for transcription."),
|
|
594
|
+
maxSpeechChars: z.number().default(4096).description("Maximum characters accepted by synthesize_speech. The hosted talker context is 4096 tokens, shared with generated audio."),
|
|
595
|
+
maxResponseBytes: z.number().default(33554432).description("Maximum provider response body in bytes."),
|
|
596
|
+
requestTimeoutMs: z.number().default(6e5).description("Tinfoil audio deadline in milliseconds, including attestation."),
|
|
597
|
+
includeProviderErrorDetail: z.boolean().default(false).description("Include bounded provider error detail in model-visible errors. Treat it as untrusted.")
|
|
598
|
+
});
|
|
599
|
+
function resolveConfig(config = {}) {
|
|
600
|
+
const defaultLanguage = config.defaultLanguage === void 0 ? void 0 : nonEmptyString(config.defaultLanguage, "tinfoil-audio: defaultLanguage");
|
|
601
|
+
return {
|
|
602
|
+
apiKeyEnv: resolveApiKeyEnv(config.apiKeyEnv, "tinfoil-audio: apiKeyEnv"),
|
|
603
|
+
transcriptionModel: nonEmptyString(config.transcriptionModel ?? "whisper-large-v3-turbo", "tinfoil-audio: transcriptionModel"),
|
|
604
|
+
speechModel: nonEmptyString(config.speechModel ?? "qwen3-tts", "tinfoil-audio: speechModel"),
|
|
605
|
+
defaultVoice: nonEmptyString(config.defaultVoice ?? "serena", "tinfoil-audio: defaultVoice"),
|
|
606
|
+
...defaultLanguage === void 0 ? {} : { defaultLanguage },
|
|
607
|
+
defaultTranscriptFormat: config.defaultTranscriptFormat ?? "text",
|
|
608
|
+
speechFormat: config.speechFormat ?? "wav",
|
|
609
|
+
maxInputBytes: positiveInteger(config.maxInputBytes ?? 26214400, "tinfoil-audio: maxInputBytes"),
|
|
610
|
+
maxSpeechChars: positiveInteger(config.maxSpeechChars ?? 4096, "tinfoil-audio: maxSpeechChars"),
|
|
611
|
+
maxResponseBytes: positiveInteger(config.maxResponseBytes ?? 33554432, "tinfoil-audio: maxResponseBytes"),
|
|
612
|
+
requestTimeoutMs: positiveInteger(config.requestTimeoutMs ?? 6e5, "tinfoil-audio: requestTimeoutMs"),
|
|
613
|
+
includeProviderErrorDetail: config.includeProviderErrorDetail ?? false
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
// src/tools.ts
|
|
617
|
+
const SUPPORTED_EXTENSION_LIST = SUPPORTED_AUDIO_EXTENSIONS.join(", ");
|
|
618
|
+
const UNSAFE_ERROR = /[\u0000-\u001f\u007f]+/gu;
|
|
619
|
+
function errorText(error) {
|
|
620
|
+
if (!(error instanceof Error)) return "unknown error";
|
|
621
|
+
return error.message.replace(UNSAFE_ERROR, " ").replace(/\s+/gu, " ").trim().slice(0, 300);
|
|
622
|
+
}
|
|
623
|
+
function errorCode(error) {
|
|
624
|
+
if (typeof error !== "object" || error === null || !("code" in error)) return void 0;
|
|
625
|
+
const code = error.code;
|
|
626
|
+
return typeof code === "string" ? code : void 0;
|
|
627
|
+
}
|
|
628
|
+
function attachmentFailure(error) {
|
|
629
|
+
if (errorCode(error) === "ATTACHMENT_FILES_UNSUPPORTED") return new TinfoilAudioError("The mounted attachment provider cannot store verbatim files, so the generated audio cannot be saved. Use a profile with the local attachment provider.", "TTS_OUTPUT_UNSUPPORTED", { cause: error });
|
|
630
|
+
return new TinfoilAudioError(`Could not save the generated audio: ${errorText(error)}`, "TTS_OUTPUT_FAILED", { cause: error });
|
|
631
|
+
}
|
|
632
|
+
async function saveAudio(ctx, data, name) {
|
|
633
|
+
try {
|
|
634
|
+
return await ctx.attachments.saveFile({
|
|
635
|
+
data,
|
|
636
|
+
name
|
|
637
|
+
});
|
|
638
|
+
} catch (error) {
|
|
639
|
+
throw attachmentFailure(error);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
function unsupportedAudio(filePath) {
|
|
643
|
+
return new TinfoilAudioError(`"${filePath}" is not a supported audio file. Supported extensions: ${SUPPORTED_EXTENSION_LIST}.`, "UNSUPPORTED_AUDIO");
|
|
644
|
+
}
|
|
645
|
+
function audioTooLarge(path, size, maxBytes, cause) {
|
|
646
|
+
return new TinfoilAudioError(`${size === void 0 ? "The audio file" : `"${path}" is ${size} bytes`}, which exceeds maxInputBytes (${maxBytes}). The Tinfoil whisper backend also caps uploads at 25 MB; use a shorter or more compressed file.`, "INPUT_TOO_LARGE", cause === void 0 ? void 0 : { cause });
|
|
647
|
+
}
|
|
648
|
+
async function loadAudio(ctx, exec, deps, filePath) {
|
|
649
|
+
const extension = audioExtension(filePath);
|
|
650
|
+
if (extension === void 0) throw unsupportedAudio(filePath);
|
|
651
|
+
deps.client.assertApiKey();
|
|
652
|
+
const cwd = exec.agent === void 0 ? void 0 : await ctx.workingDirectory.ensure(exec.agent, exec.signal);
|
|
653
|
+
const target = await ctx.fs.resolve(filePath, {
|
|
654
|
+
...cwd === void 0 ? {} : { cwd },
|
|
655
|
+
signal: exec.signal
|
|
656
|
+
});
|
|
657
|
+
const info = await ctx.fs.stat(target, exec.signal);
|
|
658
|
+
if (info === void 0) {
|
|
659
|
+
ctx.emit("fs/observed", target, { kind: "absent" }, exec);
|
|
660
|
+
throw new FsError(`cannot read "${target.displayPath}": not found`, "FS_NOT_FOUND");
|
|
661
|
+
}
|
|
662
|
+
if (info.type !== "file") throw new FsError(`cannot read "${target.displayPath}": not a regular file`, "FS_NOT_REGULAR_FILE");
|
|
663
|
+
if (info.size !== void 0 && info.size > deps.config.maxInputBytes) throw audioTooLarge(target.displayPath, info.size, deps.config.maxInputBytes);
|
|
664
|
+
let data;
|
|
665
|
+
try {
|
|
666
|
+
data = await ctx.fs.readBytes(target, exec.signal, deps.config.maxInputBytes);
|
|
667
|
+
} catch (error) {
|
|
668
|
+
if (error instanceof FsError && error.code === "FS_TOO_LARGE") throw audioTooLarge(target.displayPath, void 0, deps.config.maxInputBytes, error);
|
|
669
|
+
throw error;
|
|
670
|
+
}
|
|
671
|
+
ctx.emit("fs/observed", target, {
|
|
672
|
+
kind: "present",
|
|
673
|
+
version: info.version
|
|
674
|
+
}, exec);
|
|
675
|
+
if (data.byteLength === 0) throw new TinfoilAudioError("The audio file is empty.", "AUDIO_EMPTY");
|
|
676
|
+
return {
|
|
677
|
+
target,
|
|
678
|
+
data,
|
|
679
|
+
extension,
|
|
680
|
+
...cwd === void 0 ? {} : { cwd }
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
function resolveModel(value, fallback, name) {
|
|
684
|
+
const model = (value ?? fallback).trim();
|
|
685
|
+
if (model.length === 0) throw new TinfoilAudioError(`${name} must not be empty.`, "INPUT_INVALID");
|
|
686
|
+
return model;
|
|
687
|
+
}
|
|
688
|
+
function resolveLanguage(value, fallback) {
|
|
689
|
+
const raw = value ?? fallback;
|
|
690
|
+
if (raw === void 0) return void 0;
|
|
691
|
+
const language = raw.trim();
|
|
692
|
+
if (language.length === 0) throw new TinfoilAudioError("language must not be empty.", "INPUT_INVALID");
|
|
693
|
+
return language.toLowerCase() === "auto" ? void 0 : language;
|
|
694
|
+
}
|
|
695
|
+
function resolvePrompt(value) {
|
|
696
|
+
if (value === void 0) return void 0;
|
|
697
|
+
const prompt = value.trim();
|
|
698
|
+
return prompt.length === 0 ? void 0 : prompt;
|
|
699
|
+
}
|
|
700
|
+
function resolveTemperature(value) {
|
|
701
|
+
if (value === void 0) return void 0;
|
|
702
|
+
if (!Number.isFinite(value) || value < 0 || value > 1) throw new TinfoilAudioError("temperature must be between 0 and 1.", "INPUT_INVALID");
|
|
703
|
+
return value;
|
|
704
|
+
}
|
|
705
|
+
function resolveFormat(value, fallback) {
|
|
706
|
+
if (value === void 0) return fallback;
|
|
707
|
+
if (value !== "text" && value !== "json" && value !== "verbose_json") throw new TinfoilAudioError("response_format must be text, json, or verbose_json.", "INPUT_INVALID");
|
|
708
|
+
return value;
|
|
709
|
+
}
|
|
710
|
+
function requireNonEmptyPath(value, name) {
|
|
711
|
+
const path = value.trim();
|
|
712
|
+
if (path.length === 0) throw new TinfoilAudioError(`${name} must not be empty.`, "INPUT_INVALID");
|
|
713
|
+
return path;
|
|
714
|
+
}
|
|
715
|
+
function registerTranscribeAudio(ctx, deps) {
|
|
716
|
+
ctx.tools.register(defineTool({
|
|
717
|
+
name: "transcribe_audio",
|
|
718
|
+
description: "Transcribe a recorded audio file to text. Returns the complete transcript. Use transcribe_audio_to_file when the transcript should stay out of context.",
|
|
719
|
+
parameters: {
|
|
720
|
+
file_path: {
|
|
721
|
+
type: "string",
|
|
722
|
+
required: true,
|
|
723
|
+
description: `Path to the audio file. Supported extensions: ${SUPPORTED_EXTENSION_LIST}.`
|
|
724
|
+
},
|
|
725
|
+
model: {
|
|
726
|
+
type: "string",
|
|
727
|
+
description: "Provider model id. Defaults to the configured transcription model."
|
|
728
|
+
},
|
|
729
|
+
language: {
|
|
730
|
+
type: "string",
|
|
731
|
+
description: "ISO-639-1 language hint, for example en. Omit to auto-detect."
|
|
732
|
+
},
|
|
733
|
+
prompt: {
|
|
734
|
+
type: "string",
|
|
735
|
+
description: "Optional text to guide transcription style or vocabulary."
|
|
736
|
+
},
|
|
737
|
+
temperature: {
|
|
738
|
+
type: "number",
|
|
739
|
+
description: "Sampling temperature from 0 to 1."
|
|
740
|
+
}
|
|
741
|
+
},
|
|
742
|
+
output: {
|
|
743
|
+
schema: {
|
|
744
|
+
type: "object",
|
|
745
|
+
additionalProperties: false,
|
|
746
|
+
properties: { text: {
|
|
747
|
+
type: "string",
|
|
748
|
+
required: true
|
|
749
|
+
} }
|
|
750
|
+
},
|
|
751
|
+
render: (_args, value) => [{
|
|
752
|
+
type: "text",
|
|
753
|
+
text: value.text.length === 0 ? "(no speech recognized)" : value.text
|
|
754
|
+
}]
|
|
755
|
+
},
|
|
756
|
+
isConcurrencySafe: () => true,
|
|
757
|
+
async execute(args, exec) {
|
|
758
|
+
const filePath = requireNonEmptyPath(args.file_path, "file_path");
|
|
759
|
+
const loaded = await loadAudio(ctx, exec, deps, filePath);
|
|
760
|
+
const language = resolveLanguage(args.language, deps.config.defaultLanguage);
|
|
761
|
+
const prompt = resolvePrompt(args.prompt);
|
|
762
|
+
const temperature = resolveTemperature(args.temperature);
|
|
763
|
+
return { text: (await deps.client.transcribe({
|
|
764
|
+
data: loaded.data,
|
|
765
|
+
filename: sanitizeAudioFilename(filePath, loaded.extension),
|
|
766
|
+
mediaType: audioMediaType(loaded.extension),
|
|
767
|
+
model: resolveModel(args.model, deps.config.transcriptionModel, "model"),
|
|
768
|
+
...language === void 0 ? {} : { language },
|
|
769
|
+
...prompt === void 0 ? {} : { prompt },
|
|
770
|
+
...temperature === void 0 ? {} : { temperature },
|
|
771
|
+
responseFormat: "json",
|
|
772
|
+
signal: exec.signal
|
|
773
|
+
})).text };
|
|
774
|
+
},
|
|
775
|
+
presentCall(args) {
|
|
776
|
+
return {
|
|
777
|
+
card: "generic",
|
|
778
|
+
title: `Transcribe ${args.file_path}`,
|
|
779
|
+
kind: "other",
|
|
780
|
+
locations: [{ path: args.file_path }]
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
}));
|
|
784
|
+
}
|
|
785
|
+
function registerTranscribeAudioToFile(ctx, deps) {
|
|
786
|
+
ctx.tools.register(defineTool({
|
|
787
|
+
name: "transcribe_audio_to_file",
|
|
788
|
+
description: "Transcribe an audio file and write the transcript to another file. Returns paths, byte counts, model, and format, not the transcript. Use transcribe_audio when the text is needed in context.",
|
|
789
|
+
parameters: {
|
|
790
|
+
file_path: {
|
|
791
|
+
type: "string",
|
|
792
|
+
required: true,
|
|
793
|
+
description: `Path to the audio file. Supported extensions: ${SUPPORTED_EXTENSION_LIST}.`
|
|
794
|
+
},
|
|
795
|
+
output_path: {
|
|
796
|
+
type: "string",
|
|
797
|
+
required: true,
|
|
798
|
+
description: "Destination for the transcript. Must differ from file_path; an existing output is written subject to the filesystem write policy."
|
|
799
|
+
},
|
|
800
|
+
model: {
|
|
801
|
+
type: "string",
|
|
802
|
+
description: "Provider model id. Defaults to the configured transcription model."
|
|
803
|
+
},
|
|
804
|
+
language: {
|
|
805
|
+
type: "string",
|
|
806
|
+
description: "ISO-639-1 language hint, for example en. Omit to auto-detect."
|
|
807
|
+
},
|
|
808
|
+
prompt: {
|
|
809
|
+
type: "string",
|
|
810
|
+
description: "Optional text to guide transcription style or vocabulary."
|
|
811
|
+
},
|
|
812
|
+
temperature: {
|
|
813
|
+
type: "number",
|
|
814
|
+
description: "Sampling temperature from 0 to 1."
|
|
815
|
+
},
|
|
816
|
+
response_format: {
|
|
817
|
+
type: "string",
|
|
818
|
+
enum: [
|
|
819
|
+
"text",
|
|
820
|
+
"json",
|
|
821
|
+
"verbose_json"
|
|
822
|
+
],
|
|
823
|
+
description: "Format written to output_path. Defaults to the configured transcript format."
|
|
824
|
+
}
|
|
825
|
+
},
|
|
826
|
+
output: {
|
|
827
|
+
schema: {
|
|
828
|
+
type: "object",
|
|
829
|
+
additionalProperties: false,
|
|
830
|
+
properties: {
|
|
831
|
+
inputPath: {
|
|
832
|
+
type: "string",
|
|
833
|
+
required: true
|
|
834
|
+
},
|
|
835
|
+
outputPath: {
|
|
836
|
+
type: "string",
|
|
837
|
+
required: true
|
|
838
|
+
},
|
|
839
|
+
inputBytes: {
|
|
840
|
+
type: "integer",
|
|
841
|
+
required: true
|
|
842
|
+
},
|
|
843
|
+
outputBytes: {
|
|
844
|
+
type: "integer",
|
|
845
|
+
required: true
|
|
846
|
+
},
|
|
847
|
+
model: {
|
|
848
|
+
type: "string",
|
|
849
|
+
required: true
|
|
850
|
+
},
|
|
851
|
+
format: {
|
|
852
|
+
type: "string",
|
|
853
|
+
enum: [
|
|
854
|
+
"text",
|
|
855
|
+
"json",
|
|
856
|
+
"verbose_json"
|
|
857
|
+
],
|
|
858
|
+
required: true
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
},
|
|
862
|
+
render: (_args, value) => [{
|
|
863
|
+
type: "text",
|
|
864
|
+
text: `Wrote ${value.outputPath} (${value.outputBytes} bytes; input ${value.inputBytes} bytes) with ${value.model}.`
|
|
865
|
+
}]
|
|
866
|
+
},
|
|
867
|
+
isConcurrencySafe: () => true,
|
|
868
|
+
async execute(args, exec) {
|
|
869
|
+
const filePath = requireNonEmptyPath(args.file_path, "file_path");
|
|
870
|
+
const outputPath = requireNonEmptyPath(args.output_path, "output_path");
|
|
871
|
+
const loaded = await loadAudio(ctx, exec, deps, filePath);
|
|
872
|
+
const outputTarget = await ctx.fs.resolve(outputPath, {
|
|
873
|
+
...loaded.cwd === void 0 ? {} : { cwd: loaded.cwd },
|
|
874
|
+
signal: exec.signal
|
|
875
|
+
});
|
|
876
|
+
if (loaded.target.targetKey === outputTarget.targetKey) throw new TinfoilAudioError("output_path must differ from file_path; the source file is not overwritten.", "OUTPUT_IS_INPUT");
|
|
877
|
+
const format = resolveFormat(args.response_format, deps.config.defaultTranscriptFormat);
|
|
878
|
+
const model = resolveModel(args.model, deps.config.transcriptionModel, "model");
|
|
879
|
+
const language = resolveLanguage(args.language, deps.config.defaultLanguage);
|
|
880
|
+
const prompt = resolvePrompt(args.prompt);
|
|
881
|
+
const temperature = resolveTemperature(args.temperature);
|
|
882
|
+
const result = await deps.client.transcribe({
|
|
883
|
+
data: loaded.data,
|
|
884
|
+
filename: sanitizeAudioFilename(filePath, loaded.extension),
|
|
885
|
+
mediaType: audioMediaType(loaded.extension),
|
|
886
|
+
model,
|
|
887
|
+
...language === void 0 ? {} : { language },
|
|
888
|
+
...prompt === void 0 ? {} : { prompt },
|
|
889
|
+
...temperature === void 0 ? {} : { temperature },
|
|
890
|
+
responseFormat: format,
|
|
891
|
+
signal: exec.signal
|
|
892
|
+
});
|
|
893
|
+
const intent = await ctx.waterfall("fs/write-intent", outputTarget, exec, () => void 0);
|
|
894
|
+
const outcome = await ctx.fs.writeText(outputTarget, result.raw, intent, exec.signal);
|
|
895
|
+
ctx.emit("fs/observed", outputTarget, {
|
|
896
|
+
kind: "present",
|
|
897
|
+
version: outcome.version
|
|
898
|
+
}, exec);
|
|
899
|
+
return {
|
|
900
|
+
inputPath: ctx.fs.processPath(loaded.target),
|
|
901
|
+
outputPath: ctx.fs.processPath(outputTarget),
|
|
902
|
+
inputBytes: loaded.data.byteLength,
|
|
903
|
+
outputBytes: Buffer.byteLength(result.raw, "utf8"),
|
|
904
|
+
model,
|
|
905
|
+
format
|
|
906
|
+
};
|
|
907
|
+
},
|
|
908
|
+
presentCall(args) {
|
|
909
|
+
return {
|
|
910
|
+
card: "generic",
|
|
911
|
+
title: `Transcribe ${args.file_path} to ${args.output_path}`,
|
|
912
|
+
kind: "other",
|
|
913
|
+
locations: [{ path: args.file_path }, { path: args.output_path }]
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
}));
|
|
917
|
+
}
|
|
918
|
+
function registerSynthesizeSpeech(ctx, deps) {
|
|
919
|
+
ctx.tools.register(defineTool({
|
|
920
|
+
name: "synthesize_speech",
|
|
921
|
+
description: "Generate speech audio from text and save it as a durable file. Returns the saved path and metadata; it does not play the audio. The hosted talker context is shared with the generated audio, so long input can be rejected.",
|
|
922
|
+
parameters: {
|
|
923
|
+
text: {
|
|
924
|
+
type: "string",
|
|
925
|
+
required: true,
|
|
926
|
+
description: `Text to synthesize. Up to maxSpeechChars (${deps.config.maxSpeechChars}) characters; the hosted talker's 4096-token context is shared with the generated audio.`
|
|
927
|
+
},
|
|
928
|
+
model: {
|
|
929
|
+
type: "string",
|
|
930
|
+
description: "Provider model id. Defaults to the configured speech model."
|
|
931
|
+
},
|
|
932
|
+
voice: {
|
|
933
|
+
type: "string",
|
|
934
|
+
description: "Provider voice name. Defaults to the configured voice."
|
|
935
|
+
},
|
|
936
|
+
file_name: {
|
|
937
|
+
type: "string",
|
|
938
|
+
description: "Display name for the saved audio. The extension follows the returned format."
|
|
939
|
+
}
|
|
940
|
+
},
|
|
941
|
+
output: {
|
|
942
|
+
schema: {
|
|
943
|
+
type: "object",
|
|
944
|
+
additionalProperties: false,
|
|
945
|
+
properties: {
|
|
946
|
+
path: {
|
|
947
|
+
type: "string",
|
|
948
|
+
required: true
|
|
949
|
+
},
|
|
950
|
+
bytes: {
|
|
951
|
+
type: "integer",
|
|
952
|
+
required: true
|
|
953
|
+
},
|
|
954
|
+
mediaType: {
|
|
955
|
+
type: "string",
|
|
956
|
+
required: true
|
|
957
|
+
},
|
|
958
|
+
model: {
|
|
959
|
+
type: "string",
|
|
960
|
+
required: true
|
|
961
|
+
},
|
|
962
|
+
voice: {
|
|
963
|
+
type: "string",
|
|
964
|
+
required: true
|
|
965
|
+
},
|
|
966
|
+
durationSeconds: { type: "number" }
|
|
967
|
+
}
|
|
968
|
+
},
|
|
969
|
+
render: (_args, value) => {
|
|
970
|
+
const duration = value.durationSeconds === void 0 ? "" : `, ${value.durationSeconds.toFixed(1)}s`;
|
|
971
|
+
return [{
|
|
972
|
+
type: "text",
|
|
973
|
+
text: `Saved ${value.path} (${value.bytes} bytes${duration}) with ${value.model} (${value.voice}).`
|
|
974
|
+
}];
|
|
975
|
+
}
|
|
976
|
+
},
|
|
977
|
+
isConcurrencySafe: () => true,
|
|
978
|
+
async execute(args, exec) {
|
|
979
|
+
assertSpeechInput(args.text, deps.config.maxSpeechChars);
|
|
980
|
+
const model = resolveModel(args.model, deps.config.speechModel, "model");
|
|
981
|
+
const voice = resolveModel(args.voice, deps.config.defaultVoice, "voice");
|
|
982
|
+
const result = await deps.client.synthesize({
|
|
983
|
+
text: args.text,
|
|
984
|
+
model,
|
|
985
|
+
voice,
|
|
986
|
+
responseFormat: deps.config.speechFormat,
|
|
987
|
+
signal: exec.signal
|
|
988
|
+
});
|
|
989
|
+
const ref = await saveAudio(ctx, result.data, speechFileName(args.file_name, result.extension));
|
|
990
|
+
let path;
|
|
991
|
+
try {
|
|
992
|
+
path = ctx.attachments.fileHostPath(ref);
|
|
993
|
+
} catch (error) {
|
|
994
|
+
throw new TinfoilAudioError(`Could not resolve the saved audio path: ${errorText(error)}`, "TTS_OUTPUT_FAILED", { cause: error });
|
|
995
|
+
}
|
|
996
|
+
if (path === void 0) throw new TinfoilAudioError("The mounted attachment provider does not expose a readable audio path.", "TTS_OUTPUT_UNSUPPORTED");
|
|
997
|
+
return {
|
|
998
|
+
path,
|
|
999
|
+
bytes: ref.bytes,
|
|
1000
|
+
mediaType: result.mediaType,
|
|
1001
|
+
model,
|
|
1002
|
+
voice,
|
|
1003
|
+
...result.durationSeconds === void 0 ? {} : { durationSeconds: result.durationSeconds }
|
|
1004
|
+
};
|
|
1005
|
+
},
|
|
1006
|
+
presentCall(args) {
|
|
1007
|
+
return {
|
|
1008
|
+
card: "generic",
|
|
1009
|
+
title: `Synthesize speech (${args.text.length} characters)`,
|
|
1010
|
+
kind: "other"
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
1013
|
+
}));
|
|
1014
|
+
}
|
|
1015
|
+
// src/index.ts
|
|
1016
|
+
const name = "tinfoil-audio";
|
|
1017
|
+
const inject = ["tools"];
|
|
1018
|
+
function apply(ctx, config = {}) {
|
|
1019
|
+
const resolved = resolveConfig(config);
|
|
1020
|
+
const client = new TinfoilAudioClient({
|
|
1021
|
+
apiKeyEnv: resolved.apiKeyEnv,
|
|
1022
|
+
maxResponseBytes: resolved.maxResponseBytes,
|
|
1023
|
+
requestTimeoutMs: resolved.requestTimeoutMs,
|
|
1024
|
+
includeProviderErrorDetail: resolved.includeProviderErrorDetail
|
|
1025
|
+
});
|
|
1026
|
+
const dependencies = {
|
|
1027
|
+
config: resolved,
|
|
1028
|
+
client
|
|
1029
|
+
};
|
|
1030
|
+
registerTinfoilAttestation(ctx, {
|
|
1031
|
+
id: "audio",
|
|
1032
|
+
title: "Audio transcription and speech",
|
|
1033
|
+
description: "SDK attestation for transcription and speech synthesis.",
|
|
1034
|
+
mode: "sdk",
|
|
1035
|
+
read: () => client.verification(),
|
|
1036
|
+
verify: async (signal) => {
|
|
1037
|
+
await client.attest(signal);
|
|
1038
|
+
}
|
|
1039
|
+
});
|
|
1040
|
+
ctx.inject(["fs", "workingDirectory"], (fileCtx) => {
|
|
1041
|
+
registerTranscribeAudio(fileCtx, dependencies);
|
|
1042
|
+
registerTranscribeAudioToFile(fileCtx, dependencies);
|
|
1043
|
+
});
|
|
1044
|
+
ctx.inject(["attachments"], (attachmentCtx) => {
|
|
1045
|
+
registerSynthesizeSpeech(attachmentCtx, dependencies);
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
export { Config, apply, inject, name };
|
package/locale/en.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,77 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@srmico/dsh-tinfoil-audio",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Tinfoil audio transcription and speech plugin for DeepSeek Harness",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"icon": "./icon.svg",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/srmico/dsh-tinfoil.git",
|
|
14
|
+
"directory": "packages/dsh-tinfoil-audio"
|
|
15
|
+
},
|
|
16
|
+
"main": "./lib/index.js",
|
|
17
|
+
"types": "./lib/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./lib/index.d.ts",
|
|
21
|
+
"default": "./lib/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./package.json": "./package.json",
|
|
24
|
+
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
25
|
+
"./locale/*.json": "./locale/*.json",
|
|
26
|
+
"./icon": "./icon.svg"
|
|
27
|
+
},
|
|
28
|
+
"dsh": {
|
|
29
|
+
"bundle": {
|
|
30
|
+
"patch": "./cordis.patch.yml"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"LICENSE",
|
|
35
|
+
"lib",
|
|
36
|
+
"cordis.patch.yml",
|
|
37
|
+
"locale/*.json",
|
|
38
|
+
"icon.svg",
|
|
39
|
+
"README.md"
|
|
40
|
+
],
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": "^24.22.1 || >=26.12.1",
|
|
43
|
+
"dsh": ">=0.2.2-rc.1 <0.3.0"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@deepseek-ai/schemastery": "^3.19.0",
|
|
47
|
+
"tinfoil": "^1.4.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@deepseek-ai/cordis": "^4.1.0",
|
|
51
|
+
"@deepseek-ai/dsh-attachment": "^0.2.2-rc.1",
|
|
52
|
+
"@deepseek-ai/dsh-fs": "^0.2.2-rc.1",
|
|
53
|
+
"@deepseek-ai/dsh-llm": "^0.2.2-rc.1",
|
|
54
|
+
"@deepseek-ai/dsh-tools": "^0.2.2-rc.1",
|
|
55
|
+
"@deepseek-ai/dsh-working-directory": "^0.2.2-rc.1"
|
|
56
|
+
},
|
|
57
|
+
"peerDependenciesMeta": {
|
|
58
|
+
"@deepseek-ai/dsh-attachment": {
|
|
59
|
+
"optional": true
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"@deepseek-ai/cordis": "^4.1.0",
|
|
64
|
+
"@deepseek-ai/dsh-attachment": "^0.2.2-rc.1",
|
|
65
|
+
"@deepseek-ai/dsh-fs": "^0.2.2-rc.1",
|
|
66
|
+
"@deepseek-ai/dsh-llm": "^0.2.2-rc.1",
|
|
67
|
+
"@deepseek-ai/dsh-tools": "^0.2.2-rc.1",
|
|
68
|
+
"@deepseek-ai/dsh-working-directory": "^0.2.2-rc.1",
|
|
69
|
+
"@srmico/dsh-tinfoil-lib": "0.1.0",
|
|
70
|
+
"@types/node": "^26.6.6",
|
|
71
|
+
"typescript": "^6.0.3"
|
|
72
|
+
},
|
|
73
|
+
"scripts": {
|
|
74
|
+
"build": "tsc -p tsconfig.json --noEmit && tsdown",
|
|
75
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
76
|
+
}
|
|
6
77
|
}
|