@crossworks/voice-client 0.300.61 → 0.300.64

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crossworks/voice-client",
3
- "version": "0.300.61",
3
+ "version": "0.300.64",
4
4
  "description": "Browser-safe surface of the voice/model layer — provider catalogue, model catalogs, audio tags, and the adapter type/metadata contract. Zero deps by design: nothing here may reach the network adapters or node builtins (the jackdaw-repo-split P0 boundary).",
5
5
  "exports": {
6
6
  ".": "./src/index.ts",
@@ -36,6 +36,28 @@ import type {
36
36
  import type { TtsModelInfo, SttModelInfo } from '../catalog';
37
37
  import type { DiscoveryResult } from '../catalog';
38
38
 
39
+ /**
40
+ * What a provider call is made with: the key, plus what its key row carries
41
+ * besides the value. THE handle a key lookup (@mantle/api-keys:
42
+ * getApiKeyById, getApiKey, resolveProviderKey) returns and every adapter
43
+ * call takes (ChatOptions and VisionExtractOptions extend it; every
44
+ * `discoverModels` takes it), so a call sends exactly the row it was opened
45
+ * from: two rows holding the same key value with different workspace ids each
46
+ * send their own. Nothing is looked up by the key value.
47
+ *
48
+ * A field a provider needs on the wire is added here, filled where the row is
49
+ * opened (openKeyRow), copied by `credentialOf` (provider-credential.ts), and
50
+ * turned into a header by that provider's one header builder (Anthropic:
51
+ * anthropicHeaders). An OpenAI organization or project would go the same way.
52
+ */
53
+ export interface ProviderCredential {
54
+ /** The key value. Never logged. */
55
+ apiKey: string;
56
+ /** Anthropic only: the workspace id a key not scoped to a workspace names
57
+ * on every call (header `anthropic-workspace-id`). Not secret. */
58
+ anthropicWorkspaceId?: string;
59
+ }
60
+
39
61
  /** Common shape every adapter exposes — used by the registry to log
40
62
  * and surface the right one in errors. */
41
63
  export interface AdapterMeta {
@@ -107,7 +129,7 @@ export interface TtsDispatcher extends AdapterMeta {
107
129
  * for ElevenLabs we'd hit /v1/models too but the catalogue is
108
130
  * different. If absent, the UI falls back to whatever static
109
131
  * catalogue the adapter consults. */
110
- discoverModels?(apiKey: string): Promise<DiscoveryResult<TtsModelInfo>>;
132
+ discoverModels?(credential: ProviderCredential): Promise<DiscoveryResult<TtsModelInfo>>;
111
133
 
112
134
  /** Optional. Returns the voices a given model supports. For OpenAI
113
135
  * this is a static per-model list (we hardcode 13 voices for
@@ -402,8 +424,7 @@ export interface ChatCacheControl {
402
424
  * `reasoning.mandatory` in GET /models reject an explicit none. */
403
425
  export type ThinkingEffort = 'low' | 'medium' | 'high' | 'xhigh' | 'max';
404
426
 
405
- export interface ChatOptions {
406
- apiKey: string;
427
+ export interface ChatOptions extends ProviderCredential {
407
428
  model: string;
408
429
  /** Standard chat-completion messages. The adapter is free to
409
430
  * transform these into the provider's native shape (e.g. Anthropic's
@@ -522,7 +543,7 @@ export interface ChatDispatcher extends AdapterMeta {
522
543
  /** Live-discover available chat models. Adapter does the cross-
523
544
  * reference between provider's /v1/models response and its own
524
545
  * static catalog. */
525
- discoverModels?(apiKey: string): Promise<DiscoveryResult<ChatModelInfo>>;
546
+ discoverModels?(credential: ProviderCredential): Promise<DiscoveryResult<ChatModelInfo>>;
526
547
  /** Adapters expose their static catalog for the UI to render before
527
548
  * discovery completes (or when discovery isn't supported). */
528
549
  staticCatalog?(): readonly ChatModelInfo[];
@@ -548,7 +569,7 @@ export interface SttDispatcher extends AdapterMeta {
548
569
  transcribe(audio: Buffer, opts: TranscribeOptions): Promise<TranscribeResult>;
549
570
 
550
571
  /** Optional. Live-discover available transcription models. */
551
- discoverModels?(apiKey: string): Promise<DiscoveryResult<SttModelInfo>>;
572
+ discoverModels?(credential: ProviderCredential): Promise<DiscoveryResult<SttModelInfo>>;
552
573
  }
553
574
 
554
575
  // ─── Vision ─────────────────────────────────────────────────────────
@@ -571,8 +592,7 @@ export interface VisionModelInfo {
571
592
  tier?: 'fast' | 'balanced' | 'quality';
572
593
  }
573
594
 
574
- export interface VisionExtractOptions {
575
- apiKey: string;
595
+ export interface VisionExtractOptions extends ProviderCredential {
576
596
  /** MIME of the image bytes (image/jpeg, image/png, image/webp, image/gif).
577
597
  * Each provider accepts a slightly different list — adapters error
578
598
  * on unsupported MIMEs rather than silently sending bytes the API
@@ -624,7 +644,9 @@ export interface VisionDispatcher extends AdapterMeta {
624
644
  /** Live-discover which vision-capable models the api key can use.
625
645
  * Implementation parity with ChatDispatcher.discoverModels — when
626
646
  * absent, the form falls back to the adapter's static catalog. */
627
- discoverModels?(apiKey: string): Promise<import('../catalog').DiscoveryResult<VisionModelInfo>>;
647
+ discoverModels?(
648
+ credential: ProviderCredential,
649
+ ): Promise<import('../catalog').DiscoveryResult<VisionModelInfo>>;
628
650
 
629
651
  /** Static catalog the UI renders before live discovery returns or
630
652
  * when the adapter doesn't support discovery. */
@@ -868,7 +890,7 @@ export interface EmbeddingDispatcher extends AdapterMeta {
868
890
  * in `/v1/models` (filtered by id pattern); Google requires a
869
891
  * capability filter (`supportedGenerationMethods` includes
870
892
  * `embedContent`). */
871
- discoverModels?(apiKey: string): Promise<DiscoveryResult<EmbeddingModelInfo>>;
893
+ discoverModels?(credential: ProviderCredential): Promise<DiscoveryResult<EmbeddingModelInfo>>;
872
894
 
873
895
  /** Curated fallback list. Used by the workers form when no API key
874
896
  * is configured yet (so the dropdown isn't empty in create mode)
package/src/index.ts CHANGED
@@ -21,6 +21,9 @@ export * from './providers';
21
21
  export * from './catalog';
22
22
  export * from './audio-tags';
23
23
 
24
+ // The credential an adapter call takes, and its helpers (pure).
25
+ export * from './provider-credential';
26
+
24
27
  // Per-provider model catalogs (data only).
25
28
  export * from './catalogs/anthropic';
26
29
  export * from './catalogs/google';
@@ -0,0 +1,55 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ ANTHROPIC_WORKSPACE_ID_MAX,
4
+ credentialOf,
5
+ normalizeAnthropicWorkspaceId,
6
+ } from './provider-credential';
7
+
8
+ // Placeholder strings only: never a real key.
9
+
10
+ describe('credentialOf', () => {
11
+ it("copies only the credential's own fields", () => {
12
+ const route = {
13
+ adapter: {},
14
+ model: 'm',
15
+ baseUrl: 'http://x',
16
+ apiKey: 'test-key',
17
+ anthropicWorkspaceId: 'wrkspc_a',
18
+ };
19
+ expect(credentialOf(route)).toEqual({ apiKey: 'test-key', anthropicWorkspaceId: 'wrkspc_a' });
20
+ expect(credentialOf({ apiKey: 'test-key' })).toEqual({ apiKey: 'test-key' });
21
+ expect(credentialOf({ apiKey: 'k', anthropicWorkspaceId: '' })).toEqual({ apiKey: 'k' });
22
+ });
23
+ });
24
+
25
+ describe('normalizeAnthropicWorkspaceId', () => {
26
+ it('trims, and reads empty as none', () => {
27
+ expect(normalizeAnthropicWorkspaceId(' wrkspc_abc ')).toEqual({
28
+ ok: true,
29
+ value: 'wrkspc_abc',
30
+ });
31
+ expect(normalizeAnthropicWorkspaceId(' ')).toEqual({ ok: true, value: null });
32
+ expect(normalizeAnthropicWorkspaceId(null)).toEqual({ ok: true, value: null });
33
+ });
34
+
35
+ it('takes ASCII letters, digits, _ and - only, up to the cap', () => {
36
+ for (const ok of ['wrkspc_01ABC', 'any-shape_123', 'x'.repeat(ANTHROPIC_WORKSPACE_ID_MAX)]) {
37
+ expect(normalizeAnthropicWorkspaceId(ok).ok, ok).toBe(true);
38
+ }
39
+ for (const bad of [
40
+ 'a b',
41
+ 'a\nb',
42
+ 'a\tb',
43
+ 'wrkspc_é',
44
+ 'wrkspc_中',
45
+ 'wrkspc_\u0085',
46
+ 'wrkspc_\u009f',
47
+ 'wrkspc_\u0000',
48
+ 'wrkspc.x',
49
+ 'wrkspc/x',
50
+ 'x'.repeat(ANTHROPIC_WORKSPACE_ID_MAX + 1),
51
+ ]) {
52
+ expect(normalizeAnthropicWorkspaceId(bad).ok, JSON.stringify(bad)).toBe(false);
53
+ }
54
+ });
55
+ });
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Helpers for {@link ProviderCredential}, the handle a key lookup returns and
3
+ * every adapter call takes (adapters/types.ts). Browser-safe, no imports but
4
+ * the type.
5
+ */
6
+ import type { ProviderCredential } from './adapters/types';
7
+
8
+ /**
9
+ * Only the credential's own fields of `from` (an options object, a route, a
10
+ * loop's args that extend ProviderCredential), for handing to an adapter
11
+ * call without the rest of the object. THE one copy: a field added to
12
+ * ProviderCredential is added here, and every layer that passes a credential
13
+ * on carries it.
14
+ */
15
+ export function credentialOf(from: ProviderCredential): ProviderCredential {
16
+ return {
17
+ apiKey: from.apiKey,
18
+ ...(from.anthropicWorkspaceId ? { anthropicWorkspaceId: from.anthropicWorkspaceId } : {}),
19
+ };
20
+ }
21
+
22
+ /** Longest Anthropic workspace id accepted. */
23
+ export const ANTHROPIC_WORKSPACE_ID_MAX = 128;
24
+
25
+ /**
26
+ * The shape an Anthropic workspace id is accepted in: ASCII letters, digits,
27
+ * `_` and `-`, 1 to 128 of them. Anthropic publishes no format; its ids are
28
+ * `wrkspc_` plus letters and digits. ASCII only, so the value is always a
29
+ * legal header value (a non-ASCII or control character makes fetch throw a
30
+ * TypeError before any request). The 0292 table check is the same pattern.
31
+ */
32
+ export const ANTHROPIC_WORKSPACE_ID_RE = /^[A-Za-z0-9_-]{1,128}$/;
33
+
34
+ /**
35
+ * The trimmed workspace id, or an error sentence. Empty after trimming is
36
+ * `null` (no workspace id: clear it).
37
+ */
38
+ export function normalizeAnthropicWorkspaceId(
39
+ raw: string | null | undefined,
40
+ ): { ok: true; value: string | null } | { ok: false; error: string } {
41
+ const value = (raw ?? '').trim();
42
+ if (!value) return { ok: true, value: null };
43
+ if (value.length > ANTHROPIC_WORKSPACE_ID_MAX) {
44
+ return {
45
+ ok: false,
46
+ error: `The workspace id is longer than ${ANTHROPIC_WORKSPACE_ID_MAX} characters.`,
47
+ };
48
+ }
49
+ if (!ANTHROPIC_WORKSPACE_ID_RE.test(value)) {
50
+ return {
51
+ ok: false,
52
+ error: 'The workspace id can hold only letters A to Z, digits, _ and -.',
53
+ };
54
+ }
55
+ return { ok: true, value };
56
+ }