@devvibex/aiflow 1.4.0 → 1.6.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/package.json +1 -1
- package/src/index.d.ts +88 -0
- package/src/index.js +115 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devvibex/aiflow",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "First-party LLM gateway SDK for apps generated on the VibeX platform — chat, streaming, image, video, music (Lyria), sound effects (ElevenLabs), embeddings and real-time speech translation (Gemini Live) without shipping any third-party API key.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
package/src/index.d.ts
CHANGED
|
@@ -61,16 +61,26 @@ export interface ChatUsage {
|
|
|
61
61
|
creditsUsed: number;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
/** Where the resolved `model` came from. */
|
|
65
|
+
export type ModelSource = 'request' | 'message' | 'default';
|
|
66
|
+
|
|
64
67
|
export interface ChatResponse {
|
|
65
68
|
id: string;
|
|
69
|
+
/** Resolved model id. */
|
|
66
70
|
model: string;
|
|
67
71
|
content: string;
|
|
68
72
|
usage: ChatUsage;
|
|
73
|
+
/** Where the resolved `model` came from. */
|
|
74
|
+
modelSource?: ModelSource;
|
|
69
75
|
}
|
|
70
76
|
|
|
71
77
|
export interface ChatStreamChunk {
|
|
72
78
|
delta: string;
|
|
73
79
|
done: boolean;
|
|
80
|
+
/** Resolved model id (present on the final `done: true` chunk). */
|
|
81
|
+
model?: string;
|
|
82
|
+
/** Where the resolved `model` came from (present on the final `done: true` chunk). */
|
|
83
|
+
modelSource?: ModelSource;
|
|
74
84
|
[key: string]: unknown;
|
|
75
85
|
}
|
|
76
86
|
|
|
@@ -411,6 +421,7 @@ export interface LiveAudioOut {
|
|
|
411
421
|
|
|
412
422
|
export interface LiveTurnComplete {
|
|
413
423
|
interrupted: boolean;
|
|
424
|
+
turnComplete: boolean;
|
|
414
425
|
}
|
|
415
426
|
|
|
416
427
|
export interface LiveError {
|
|
@@ -441,6 +452,11 @@ export interface LiveTranslateOptions {
|
|
|
441
452
|
onReady?: (payload: LiveReadyPayload) => void;
|
|
442
453
|
onTranscript?: (payload: LiveTranscript) => void;
|
|
443
454
|
onAudio?: (payload: LiveAudioOut) => void;
|
|
455
|
+
/**
|
|
456
|
+
* Barge-in: the model cancelled the in-flight turn. You MUST flush queued
|
|
457
|
+
* playback here so the abandoned translation doesn't talk over new speech.
|
|
458
|
+
*/
|
|
459
|
+
onInterrupted?: () => void;
|
|
444
460
|
onTurnComplete?: (payload: LiveTurnComplete) => void;
|
|
445
461
|
onError?: (payload: LiveError) => void;
|
|
446
462
|
onClosed?: (payload: LiveClosed) => void;
|
|
@@ -450,6 +466,7 @@ export type LiveTranslateEvent =
|
|
|
450
466
|
| 'ready'
|
|
451
467
|
| 'transcript'
|
|
452
468
|
| 'audio'
|
|
469
|
+
| 'interrupted'
|
|
453
470
|
| 'turn_complete'
|
|
454
471
|
| 'error'
|
|
455
472
|
| 'closed';
|
|
@@ -508,6 +525,8 @@ export interface AiflowConfig {
|
|
|
508
525
|
video: string[];
|
|
509
526
|
audio: string[];
|
|
510
527
|
sfx: string[];
|
|
528
|
+
/** Real-time translate models, when configured. */
|
|
529
|
+
translate?: string[];
|
|
511
530
|
};
|
|
512
531
|
defaults: {
|
|
513
532
|
chat: string;
|
|
@@ -516,6 +535,8 @@ export interface AiflowConfig {
|
|
|
516
535
|
video: string;
|
|
517
536
|
audio: string;
|
|
518
537
|
sfx: string;
|
|
538
|
+
/** Default real-time translate model, when configured. */
|
|
539
|
+
translate?: string;
|
|
519
540
|
};
|
|
520
541
|
limits: {
|
|
521
542
|
maxTokens: number;
|
|
@@ -527,6 +548,53 @@ export interface AiflowConfig {
|
|
|
527
548
|
creditValue: number;
|
|
528
549
|
/** Module-configured default system prompt, or null if none. */
|
|
529
550
|
systemPrompt?: string | null;
|
|
551
|
+
/** Whether server-side web search is enabled for chat by default. */
|
|
552
|
+
webSearch?: boolean;
|
|
553
|
+
/** Whether the real-time live-translate feature is enabled. */
|
|
554
|
+
liveTranslate?: boolean;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// ─── Admin (module config) ───────────────────────────────────────────────────
|
|
558
|
+
|
|
559
|
+
/** One AIFlow module in the app's project, as returned by `admin.listModules`. */
|
|
560
|
+
export interface AiflowAdminModule {
|
|
561
|
+
appId: string;
|
|
562
|
+
moduleId: number;
|
|
563
|
+
feature: string | null;
|
|
564
|
+
useCase: string;
|
|
565
|
+
useCaseName: string | null;
|
|
566
|
+
title: string | null;
|
|
567
|
+
systemPrompt: string | null;
|
|
568
|
+
defaults: Record<string, string>;
|
|
569
|
+
models: string[];
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/** Input for `admin.updateConfig`. */
|
|
573
|
+
export interface AiflowAdminUpdateConfigInput {
|
|
574
|
+
/**
|
|
575
|
+
* Target module. Optional — defaults to the module for this client's app_id.
|
|
576
|
+
* When set it must be a sibling module in the same project.
|
|
577
|
+
*/
|
|
578
|
+
moduleId?: number;
|
|
579
|
+
systemPrompt?: string;
|
|
580
|
+
defaults?: Record<string, string>;
|
|
581
|
+
/** Sugar: sets the chat default model (folded into `defaults.chat`). */
|
|
582
|
+
defaultModel?: string;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Admin namespace — manage the app's AIFlow module config. Requires an admin
|
|
587
|
+
* bearer token set via `setAdminToken()`; the gateway rejects non-admins (403).
|
|
588
|
+
*/
|
|
589
|
+
export interface AiflowAdminNamespace {
|
|
590
|
+
/** List every AIFlow module in this app's project. */
|
|
591
|
+
listModules(opts?: {
|
|
592
|
+
signal?: AbortSignal;
|
|
593
|
+
}): Promise<{ modules: AiflowAdminModule[] }>;
|
|
594
|
+
/** Update the target module's system prompt and/or default models. */
|
|
595
|
+
updateConfig(
|
|
596
|
+
input: AiflowAdminUpdateConfigInput,
|
|
597
|
+
): Promise<{ module: AiflowAdminModule }>;
|
|
530
598
|
}
|
|
531
599
|
|
|
532
600
|
export class AIFlowClient {
|
|
@@ -538,6 +606,8 @@ export class AIFlowClient {
|
|
|
538
606
|
endUserId: string | null;
|
|
539
607
|
/** Per-user knowledge base — upload / list / delete the current user's docs. */
|
|
540
608
|
readonly knowledge: KnowledgeNamespace;
|
|
609
|
+
/** Admin namespace — manage the app's AIFlow module config (needs `setAdminToken`). */
|
|
610
|
+
readonly admin: AiflowAdminNamespace;
|
|
541
611
|
|
|
542
612
|
constructor(options: AIFlowClientOptions);
|
|
543
613
|
|
|
@@ -549,6 +619,24 @@ export class AIFlowClient {
|
|
|
549
619
|
*/
|
|
550
620
|
setEndUser(endUserId: string | null): this;
|
|
551
621
|
|
|
622
|
+
/**
|
|
623
|
+
* Set a session-level default model for one generation kind. Wins over the
|
|
624
|
+
* server config default, but a per-call `model` still wins over this. Pass
|
|
625
|
+
* `null` to clear the override. Returns `this` for chaining.
|
|
626
|
+
*/
|
|
627
|
+
setModel(
|
|
628
|
+
kind: 'chat' | 'image' | 'embed' | 'video' | 'audio' | 'sfx',
|
|
629
|
+
model: string | null,
|
|
630
|
+
): this;
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* Set (or clear with `null`) the admin bearer token — the generated app's
|
|
634
|
+
* own logged-in admin JWT (the same bearer the app's `vibex` SDK reads from
|
|
635
|
+
* localStorage['access_token']). Required for every `admin.*` call. Returns
|
|
636
|
+
* `this` for chaining.
|
|
637
|
+
*/
|
|
638
|
+
setAdminToken(token: string | null): this;
|
|
639
|
+
|
|
552
640
|
/**
|
|
553
641
|
* Fetch (and cache) the server-side AIFlow config. Pass `{ refresh: true }`
|
|
554
642
|
* to force a reload.
|
package/src/index.js
CHANGED
|
@@ -64,6 +64,9 @@ export class AIFlowClient {
|
|
|
64
64
|
audio: defaultAudioModel,
|
|
65
65
|
sfx: defaultSfxModel,
|
|
66
66
|
};
|
|
67
|
+
// Admin bearer token (the generated app's own logged-in admin JWT).
|
|
68
|
+
// Set via setAdminToken(); required by the /admin/* routes.
|
|
69
|
+
this._adminToken = null;
|
|
67
70
|
this.timeout = timeout;
|
|
68
71
|
this.maxRetries = maxRetries;
|
|
69
72
|
this._fetch =
|
|
@@ -95,6 +98,18 @@ export class AIFlowClient {
|
|
|
95
98
|
/** Delete one of the current user's own knowledge sources by id. */
|
|
96
99
|
deleteMine: (id, opts) => this._knowledgeDelete(id, opts),
|
|
97
100
|
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Admin namespace — manage the app's AIFlow module config (system
|
|
104
|
+
* prompt + per-kind default models). Requires an admin bearer token set
|
|
105
|
+
* via `setAdminToken()`; the gateway rejects non-admin callers (403).
|
|
106
|
+
*/
|
|
107
|
+
this.admin = {
|
|
108
|
+
/** List every AIFlow module in this app's project. */
|
|
109
|
+
listModules: (opts) => this._adminListModules(opts),
|
|
110
|
+
/** Update this app's (or a sibling) module system prompt / defaults. */
|
|
111
|
+
updateConfig: (input) => this._adminUpdateConfig(input),
|
|
112
|
+
};
|
|
98
113
|
}
|
|
99
114
|
|
|
100
115
|
/**
|
|
@@ -109,6 +124,29 @@ export class AIFlowClient {
|
|
|
109
124
|
return this;
|
|
110
125
|
}
|
|
111
126
|
|
|
127
|
+
/**
|
|
128
|
+
* Set a session-level default model for one generation kind. Wins over the
|
|
129
|
+
* server-fetched config default, but a per-call `model` still wins over
|
|
130
|
+
* this. Pass `null` to clear the override and fall back to config.
|
|
131
|
+
* @param {'chat'|'image'|'embed'|'video'|'audio'|'sfx'} kind
|
|
132
|
+
* @param {string|null} model
|
|
133
|
+
*/
|
|
134
|
+
setModel(kind, model) {
|
|
135
|
+
this._userDefaults[kind] = model || null;
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Set (or clear) the admin bearer token. This is the generated app's own
|
|
141
|
+
* logged-in admin JWT (the same bearer the app's `vibex` SDK reads from
|
|
142
|
+
* localStorage['access_token']). Required for every `admin.*` call.
|
|
143
|
+
* @param {string|null} token
|
|
144
|
+
*/
|
|
145
|
+
setAdminToken(token) {
|
|
146
|
+
this._adminToken = token || null;
|
|
147
|
+
return this;
|
|
148
|
+
}
|
|
149
|
+
|
|
112
150
|
/**
|
|
113
151
|
* Fetch (and cache) the server-side AIFlow config. Subsequent calls
|
|
114
152
|
* return the cached object; pass `{ refresh: true }` to force a reload.
|
|
@@ -616,7 +654,8 @@ export class AIFlowClient {
|
|
|
616
654
|
* @param {(p:object)=>void} [opts.onReady]
|
|
617
655
|
* @param {(p:{role:'input'|'output',text:string,isFinal:boolean})=>void} [opts.onTranscript]
|
|
618
656
|
* @param {(p:{chunk:string,mimeType:string})=>void} [opts.onAudio]
|
|
619
|
-
* @param {(
|
|
657
|
+
* @param {()=>void} [opts.onInterrupted] Barge-in: the model cancelled the in-flight turn. You MUST flush queued playback here so stale audio doesn't talk over new speech.
|
|
658
|
+
* @param {(p:{interrupted:boolean,turnComplete:boolean})=>void} [opts.onTurnComplete]
|
|
620
659
|
* @param {(p:{code:string,message:string})=>void} [opts.onError]
|
|
621
660
|
* @param {(p:{reason:string,usage:object})=>void} [opts.onClosed]
|
|
622
661
|
* @returns {Promise<LiveTranslateSession>} Resolves once connected.
|
|
@@ -632,6 +671,7 @@ export class AIFlowClient {
|
|
|
632
671
|
onReady,
|
|
633
672
|
onTranscript,
|
|
634
673
|
onAudio,
|
|
674
|
+
onInterrupted,
|
|
635
675
|
onTurnComplete,
|
|
636
676
|
onError,
|
|
637
677
|
onClosed,
|
|
@@ -662,6 +702,7 @@ export class AIFlowClient {
|
|
|
662
702
|
onReady,
|
|
663
703
|
onTranscript,
|
|
664
704
|
onAudio,
|
|
705
|
+
onInterrupted,
|
|
665
706
|
onTurnComplete,
|
|
666
707
|
onError,
|
|
667
708
|
onClosed,
|
|
@@ -685,6 +726,19 @@ export class AIFlowClient {
|
|
|
685
726
|
return h;
|
|
686
727
|
}
|
|
687
728
|
|
|
729
|
+
/**
|
|
730
|
+
* Auth headers for the admin (`/admin/*`) routes: the standard app/end-user
|
|
731
|
+
* headers PLUS the admin bearer token (when set via `setAdminToken()`).
|
|
732
|
+
*/
|
|
733
|
+
_adminHeaders() {
|
|
734
|
+
return {
|
|
735
|
+
...this._authHeaders(),
|
|
736
|
+
...(this._adminToken
|
|
737
|
+
? { Authorization: 'Bearer ' + this._adminToken }
|
|
738
|
+
: {}),
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
|
|
688
742
|
/** POST a JSON body to the gateway. */
|
|
689
743
|
async _request(path, body, { signal, stream = false, timeout } = {}) {
|
|
690
744
|
const headers = {
|
|
@@ -699,6 +753,29 @@ export class AIFlowClient {
|
|
|
699
753
|
);
|
|
700
754
|
}
|
|
701
755
|
|
|
756
|
+
/**
|
|
757
|
+
* Request an admin (`/admin/*`) route. Mirrors {@link _request} but attaches
|
|
758
|
+
* the admin bearer via {@link _adminHeaders}. Supports GET (no body) and
|
|
759
|
+
* PATCH/POST (JSON body). Reuses the shared timeout/retry + error mapping.
|
|
760
|
+
*/
|
|
761
|
+
async _adminRequest(path, body, { method = 'GET', signal, timeout } = {}) {
|
|
762
|
+
const isBodyMethod = method !== 'GET' && method !== 'HEAD';
|
|
763
|
+
const headers = {
|
|
764
|
+
...this._adminHeaders(),
|
|
765
|
+
Accept: 'application/json',
|
|
766
|
+
};
|
|
767
|
+
if (isBodyMethod) headers['Content-Type'] = 'application/json';
|
|
768
|
+
return this._fetchWithRetry(
|
|
769
|
+
`${this.baseUrl}${path}`,
|
|
770
|
+
{
|
|
771
|
+
method,
|
|
772
|
+
headers,
|
|
773
|
+
...(isBodyMethod ? { body: JSON.stringify(body ?? {}) } : {}),
|
|
774
|
+
},
|
|
775
|
+
{ signal, timeout },
|
|
776
|
+
);
|
|
777
|
+
}
|
|
778
|
+
|
|
702
779
|
/** GET from the gateway (used by short status polls). */
|
|
703
780
|
async _get(path, { signal, timeout } = {}) {
|
|
704
781
|
return this._fetchWithRetry(
|
|
@@ -834,6 +911,42 @@ export class AIFlowClient {
|
|
|
834
911
|
);
|
|
835
912
|
return await res.json();
|
|
836
913
|
}
|
|
914
|
+
|
|
915
|
+
// ────────────────────────────────────────────────────────────────────────
|
|
916
|
+
// Admin (module config) internals
|
|
917
|
+
// ────────────────────────────────────────────────────────────────────────
|
|
918
|
+
|
|
919
|
+
async _adminListModules({ signal } = {}) {
|
|
920
|
+
const res = await this._adminRequest('/admin/modules', undefined, {
|
|
921
|
+
method: 'GET',
|
|
922
|
+
signal,
|
|
923
|
+
});
|
|
924
|
+
return await res.json();
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
async _adminUpdateConfig({
|
|
928
|
+
moduleId,
|
|
929
|
+
systemPrompt,
|
|
930
|
+
defaults,
|
|
931
|
+
defaultModel,
|
|
932
|
+
signal,
|
|
933
|
+
} = {}) {
|
|
934
|
+
// `defaultModel` is sugar for setting the chat default; fold it into
|
|
935
|
+
// `defaults.chat` without clobbering any explicit `defaults` passed.
|
|
936
|
+
let resolvedDefaults = defaults;
|
|
937
|
+
if (defaultModel != null) {
|
|
938
|
+
resolvedDefaults = { ...(defaults || {}), chat: defaultModel };
|
|
939
|
+
}
|
|
940
|
+
const body = {};
|
|
941
|
+
if (moduleId != null) body.moduleId = moduleId;
|
|
942
|
+
if (systemPrompt !== undefined) body.systemPrompt = systemPrompt;
|
|
943
|
+
if (resolvedDefaults !== undefined) body.defaults = resolvedDefaults;
|
|
944
|
+
const res = await this._adminRequest('/admin/config', body, {
|
|
945
|
+
method: 'PATCH',
|
|
946
|
+
signal,
|
|
947
|
+
});
|
|
948
|
+
return await res.json();
|
|
949
|
+
}
|
|
837
950
|
}
|
|
838
951
|
|
|
839
952
|
// ────────────────────────────────────────────────────────────────────────────
|
|
@@ -880,6 +993,7 @@ export class LiveTranslateSession {
|
|
|
880
993
|
});
|
|
881
994
|
fwd('translate:transcript', this._handlers.onTranscript);
|
|
882
995
|
fwd('translate:audio', this._handlers.onAudio);
|
|
996
|
+
fwd('translate:interrupted', this._handlers.onInterrupted);
|
|
883
997
|
fwd('translate:turn_complete', this._handlers.onTurnComplete);
|
|
884
998
|
fwd('translate:error', this._handlers.onError);
|
|
885
999
|
this.socket.on('translate:closed', (p) => {
|