@hosty-sdk/app 0.56.0 → 0.57.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/README.md CHANGED
@@ -310,6 +310,14 @@ static and named entries. Keep the installation, key and implementation ID with
310
310
  `displayName` is the current human-readable label. Pass the selection to `speechCapabilities`
311
311
  and `transcribe`, or to `textToSpeechCapabilities` and `synthesize(selected, { text }, signal)`.
312
312
  Synthesis returns a streaming fetch Response. Abort or cancel its body to stop the request.
313
+
314
+ For translated read-aloud, call `resolveSpeechTargetLanguage(requested, preferences, capabilities)`
315
+ from `@hosty-sdk/app/providers` before playback and pass the returned tag as `targetLanguage`
316
+ to `synthesize`. The resolver checks an explicit target first, then the acting user's
317
+ `{ preferredLanguage, autoTranslateReadAloud }` preferences. An unsupported explicit target raises
318
+ `speech_target_language_unsupported`. An unavailable automatic target resolves to `undefined`,
319
+ which retains the original-language request. Reuse the resolved target for all chunks of one
320
+ playback.
313
321
  The provider backend publishes with `publishImplementations` and resolves the exact implementation
314
322
  from `validate` on each request. See the [app manifest guide](../../skills/hosty-app-skill/references/app-manifest.md#named-provider-implementations).
315
323
 
@@ -46,7 +46,7 @@ export declare class ProviderClient {
46
46
  timeoutMs?: number;
47
47
  }): Promise<SpeechResult>;
48
48
  textToSpeechCapabilities(provider: ProviderSelection, signal?: AbortSignal): Promise<TextToSpeechCapabilities>;
49
- /** Stream 24 kHz mono signed 16-bit little-endian PCM. Cancel the signal or response body to stop. */
49
+ /** Stream 24 kHz mono PCM using a target resolved against the selected implementation, when provided. Cancel the signal or response body to stop. */
50
50
  synthesize(provider: ProviderSelection, input: SpeechSynthesisRequest, signal?: AbortSignal): Promise<Response>;
51
51
  translationCapabilities(provider: ProviderSelection, signal?: AbortSignal, options?: {
52
52
  connection: {
@@ -1,7 +1,7 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { Agent, fetch as speechFetch } from "undici";
3
3
  import { AssistantClient } from "@hosty-sdk/app/assistant";
4
- import { ProviderError } from "@hosty-sdk/app/providers";
4
+ import { normalizeLanguageTag, ProviderError } from "@hosty-sdk/app/providers";
5
5
  /** Server-only provider client. Its app service token must never be sent to a browser or provider. */
6
6
  export class ProviderClient {
7
7
  appId;
@@ -111,16 +111,19 @@ export class ProviderClient {
111
111
  return read(await fetch(`${binding.url}/capabilities`, { redirect: "error", signal: signal ? AbortSignal.any([signal, deadline]) : deadline,
112
112
  headers: { authorization: `Bearer ${binding.token}` } }));
113
113
  }
114
- /** Stream 24 kHz mono signed 16-bit little-endian PCM. Cancel the signal or response body to stop. */
114
+ /** Stream 24 kHz mono PCM using a target resolved against the selected implementation, when provided. Cancel the signal or response body to stop. */
115
115
  async synthesize(provider, input, signal) {
116
116
  if (typeof input.text !== "string" || !input.text.trim() || input.text.length > 4000)
117
117
  throw new ProviderError("speech_text_invalid", "Provide between 1 and 4000 UTF-16 code units of text.", 400);
118
+ const targetLanguage = normalizeLanguageTag(input.targetLanguage)?.tag;
119
+ if (input.targetLanguage !== undefined && targetLanguage === undefined)
120
+ throw new ProviderError("speech_target_language_invalid", "Provide a valid target language tag.", 400);
118
121
  const timeout = AbortSignal.timeout(300_000);
119
122
  const deadline = signal ? AbortSignal.any([signal, timeout]) : timeout;
120
123
  const binding = await this.connect({ ...provider, kind: "text-to-speech" }, deadline);
121
124
  const response = await fetch(`${binding.url}/speech`, { method: "POST", redirect: "error", signal: deadline,
122
125
  headers: { authorization: `Bearer ${binding.token}`, "content-type": "application/json", accept: "audio/pcm" },
123
- body: JSON.stringify({ text: input.text }) });
126
+ body: JSON.stringify({ text: input.text, targetLanguage }) });
124
127
  if (!response.ok)
125
128
  await read(response);
126
129
  if (!response.body || !/^audio\/pcm\s*;\s*rate=24000\s*$/i.test(response.headers.get("content-type") ?? "")) {
@@ -105,9 +105,17 @@ export type TextToSpeechCapabilities = {
105
105
  maxCharacters: number;
106
106
  backend: string;
107
107
  ready: boolean;
108
+ supportsTranslation?: boolean;
109
+ /** When omitted by a translation-capable provider, there is no advertised language restriction. */
110
+ targetLanguages?: string[];
108
111
  };
109
112
  export type SpeechSynthesisRequest = {
110
113
  text: string;
114
+ targetLanguage?: string;
115
+ };
116
+ export type SpeechLanguagePreferences = {
117
+ preferredLanguage?: string | null;
118
+ autoTranslateReadAloud?: boolean;
111
119
  };
112
120
  export type TranslationRequest = {
113
121
  text: string;
@@ -138,6 +146,13 @@ export declare class ProviderError extends Error {
138
146
  readonly status: number;
139
147
  constructor(code: string, message: string, status: number);
140
148
  }
149
+ /** @internal Shared language-tag validation for provider client and transport. */
150
+ export declare function normalizeLanguageTag(value: unknown): {
151
+ tag: string;
152
+ withoutRegion: string;
153
+ } | undefined;
154
+ /** Resolve the language once for playback using the selected implementation's capabilities. */
155
+ export declare function resolveSpeechTargetLanguage(requested: string | undefined, preferences: SpeechLanguagePreferences | undefined, capabilities: Pick<TextToSpeechCapabilities, "supportsTranslation" | "targetLanguages">): string | undefined;
141
156
  /** Non-secret receipt reference. Only its reviewed source and executor installations can use it. */
142
157
  export type TaskDispatchRef = {
143
158
  userId: string;
package/dist/providers.js CHANGED
@@ -8,3 +8,46 @@ export class ProviderError extends Error {
8
8
  this.status = status;
9
9
  }
10
10
  }
11
+ // Keep the language-tag grammar and casing aligned with the .NET SDK's LanguageTag.cs.
12
+ const languageTag = /^([a-z]{2,3}(?:-[a-z]{3}){0,3}|[a-z]{4,8})(-[a-z]{4})?(-(?:[a-z]{2}|[0-9]{3}))?((?:-(?:[a-z0-9]{5,8}|[0-9][a-z0-9]{3}))*)((?:-[0-9a-wy-z](?:-[a-z0-9]{2,8})+)*)(-x(?:-[a-z0-9]{1,8})+)?$/;
13
+ /** @internal Shared language-tag validation for provider client and transport. */
14
+ export function normalizeLanguageTag(value) {
15
+ if (typeof value !== "string")
16
+ return undefined;
17
+ // Match .NET's Unicode whitespace trimming, which retains a BOM for validation to reject.
18
+ const tag = value.replace(/^\p{White_Space}+|\p{White_Space}+$/gu, "");
19
+ if (tag.length > 63 || !/^[A-Za-z0-9-]+$/.test(tag))
20
+ return undefined;
21
+ const match = languageTag.exec(tag.toLowerCase());
22
+ if (!match)
23
+ return undefined;
24
+ const [, language = "", script = "", region = "", variants = "", extensions = "", privateUse = ""] = match;
25
+ const variantTags = variants.split("-").slice(1);
26
+ const extensionKeys = extensions.split("-").filter(part => part.length === 1);
27
+ if (new Set(variantTags).size !== variantTags.length || new Set(extensionKeys).size !== extensionKeys.length)
28
+ return undefined;
29
+ const prefix = script ? language + "-" + script[1].toUpperCase() + script.slice(2) : language;
30
+ const suffix = variants + extensions + privateUse;
31
+ return { tag: prefix + region.toUpperCase() + suffix, withoutRegion: prefix + suffix };
32
+ }
33
+ /** Resolve the language once for playback using the selected implementation's capabilities. */
34
+ export function resolveSpeechTargetLanguage(requested, preferences, capabilities) {
35
+ const explicit = requested !== undefined;
36
+ const preferred = preferences?.autoTranslateReadAloud === true ? preferences.preferredLanguage : undefined;
37
+ const language = normalizeLanguageTag(explicit ? requested : preferred);
38
+ if (!language) {
39
+ if (explicit)
40
+ throw new ProviderError("speech_target_language_invalid", "Provide a valid target language tag.", 400);
41
+ return undefined;
42
+ }
43
+ const targets = capabilities.targetLanguages;
44
+ if (capabilities.supportsTranslation === true && (targets == null ||
45
+ Array.isArray(targets) && targets.some(target => {
46
+ const advertised = normalizeLanguageTag(target)?.tag;
47
+ return advertised === language.tag || advertised === language.withoutRegion;
48
+ })))
49
+ return language.tag;
50
+ if (explicit)
51
+ throw new ProviderError("speech_target_language_unsupported", "The selected speech provider does not support this target language.", 400);
52
+ return undefined;
53
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hosty-sdk/app",
3
- "version": "0.56.0",
3
+ "version": "0.57.0",
4
4
  "description": "Hosty app auth: session state machine, recovery bridge, Core revalidation, and the embedder responder.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "type": "module",