@astratra/ai 1.6.0 → 1.7.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 +349 -6
- package/package.json +1 -1
- package/src/aiConfig.js +72 -0
- package/src/gemini.js +9 -3
- package/src/index.d.ts +290 -4
- package/src/index.js +9 -1
- package/src/localModels.js +99 -0
- package/src/managedRouter.js +258 -0
- package/src/modelAnswers.js +1 -0
- package/src/nativeTools.js +44 -5
- package/src/openaiCompatible.js +5 -1
- package/src/providerLimits.js +45 -0
- package/src/routingState.js +87 -0
- package/src/serviceHttp.js +56 -0
- package/src/serviceLanguage.js +23 -0
- package/src/voice.js +181 -0
package/src/index.d.ts
CHANGED
|
@@ -602,7 +602,7 @@ export function askChatModel(
|
|
|
602
602
|
target: { url: string; key: string; model: string; extra?: Record<string, unknown> },
|
|
603
603
|
request: { system?: string; messages: ChatMessage[]; tools?: Array<Record<string, unknown>>; maxTokens?: number; temperature?: number },
|
|
604
604
|
io: { fetch: FetchLike; signal?: AbortSignal; timeoutMs?: number }
|
|
605
|
-
): Promise<{ status: number; text?: string | null; toolCalls?: ChatToolCall[]; cut?: boolean }>;
|
|
605
|
+
): Promise<{ status: number; text?: string | null; toolCalls?: ChatToolCall[]; cut?: boolean; retryAfterMs?: number }>;
|
|
606
606
|
|
|
607
607
|
export function readToolArguments(raw: unknown): { args: Record<string, unknown> | null; invalid: boolean; reason?: 'not_json' | 'not_object'; detail?: string };
|
|
608
608
|
|
|
@@ -650,12 +650,14 @@ export function createOpenAICompatibleProvider(options: ChatProviderOptions & {
|
|
|
650
650
|
export function askGeminiModel(
|
|
651
651
|
model: string,
|
|
652
652
|
request: ChatRequest,
|
|
653
|
-
io: { key: string; fetch: FetchLike; signal?: AbortSignal; timeoutMs?: number; endpoint?: string; noSystemInstruction?: (model: string) => boolean }
|
|
654
|
-
): Promise<{ status: number; text?: string | null; toolCalls?: ChatToolCall[]; cut?: boolean }>;
|
|
653
|
+
io: { key: string; fetch: FetchLike; signal?: AbortSignal; timeoutMs?: number; endpoint?: string; noSystemInstruction?: (model: string) => boolean; extra?: Record<string, unknown> }
|
|
654
|
+
): Promise<{ status: number; text?: string | null; toolCalls?: ChatToolCall[]; cut?: boolean; retryAfterMs?: number }>;
|
|
655
655
|
|
|
656
656
|
export function createGeminiProvider(options: ChatProviderOptions & {
|
|
657
657
|
id?: string;
|
|
658
658
|
endpoint?: string;
|
|
659
|
+
external?: boolean;
|
|
660
|
+
extra?: Record<string, unknown>;
|
|
659
661
|
/** Défaut : les modèles Gemma, qui ne prennent pas de consigne système. */
|
|
660
662
|
noSystemInstruction?: (model: string) => boolean;
|
|
661
663
|
}): Provider;
|
|
@@ -677,6 +679,7 @@ export interface NativeTool<Ctx = Record<string, unknown>> {
|
|
|
677
679
|
description: string;
|
|
678
680
|
parameters: { type: 'object'; [key: string]: unknown };
|
|
679
681
|
kind: NativeToolKind;
|
|
682
|
+
risk?: DeclaredToolRisk;
|
|
680
683
|
summary?: (args: Record<string, unknown>) => Record<string, string | number>;
|
|
681
684
|
run(args: Record<string, unknown>, ctx: Ctx & { signal: AbortSignal }): Awaitable<NativeToolResult | null | undefined>;
|
|
682
685
|
perform?(args: Record<string, unknown>, ctx: Ctx): Awaitable<NativeToolResult | null | undefined>;
|
|
@@ -684,7 +687,7 @@ export interface NativeTool<Ctx = Record<string, unknown>> {
|
|
|
684
687
|
}
|
|
685
688
|
|
|
686
689
|
export const NATIVE_TOOL_KINDS: string[];
|
|
687
|
-
export function validateNativeTools<T extends NativeTool<any>>(tools: T[], options?: { kinds?: string[]; requireSummary?: boolean }): T[];
|
|
690
|
+
export function validateNativeTools<T extends NativeTool<any>>(tools: T[], options?: { kinds?: string[]; requireSummary?: boolean; requireRisk?: boolean }): T[];
|
|
688
691
|
export function toolSpecs(tools: NativeTool<any>[]): Array<{ name: string; description: string; parameters: Record<string, unknown> }>;
|
|
689
692
|
export function stepParams(tool: NativeTool<any> | null | undefined, args: unknown, options?: { max?: number }): Record<string, string | number>;
|
|
690
693
|
|
|
@@ -707,6 +710,9 @@ export interface ToolMessage {
|
|
|
707
710
|
export function createToolCaller<Ctx = Record<string, unknown>>(options: {
|
|
708
711
|
tools: Map<string, NativeTool<Ctx>> | NativeTool<Ctx>[];
|
|
709
712
|
context?: Ctx;
|
|
713
|
+
confirmationPolicy?: 'always' | 'never' | { threshold: ToolRisk };
|
|
714
|
+
exposure?: { untrusted: boolean };
|
|
715
|
+
readsUntrusted?: (tool: NativeTool<Ctx>, found: NativeToolResult | null | undefined) => boolean;
|
|
710
716
|
signal?: AbortSignal;
|
|
711
717
|
timeoutMs?: number;
|
|
712
718
|
now?: () => number;
|
|
@@ -817,3 +823,283 @@ export function createLlamaCppProvider(options?: LlamaCppConfigInput & {
|
|
|
817
823
|
fetch?: (url: string, init: object) => Awaitable<Response>;
|
|
818
824
|
sleep?: (ms: number) => Promise<void>;
|
|
819
825
|
}): LlamaCppProvider;
|
|
826
|
+
|
|
827
|
+
/* Configured routing, local model services and voice. */
|
|
828
|
+
export type AiUsage = 'quick' | 'chat' | 'tools' | 'pilot' | 'json' | 'vision';
|
|
829
|
+
export interface RoutingState {
|
|
830
|
+
day: string;
|
|
831
|
+
rpmWindow: number[];
|
|
832
|
+
rpdUsed: number;
|
|
833
|
+
tpdUsed: number;
|
|
834
|
+
cooldownUntil: number;
|
|
835
|
+
degradedUntil: number;
|
|
836
|
+
failures: number;
|
|
837
|
+
disabled: boolean;
|
|
838
|
+
}
|
|
839
|
+
/** update must be atomic across all users of the store. The callback is synchronous
|
|
840
|
+
* and side-effect free: a CAS adapter may call it more than once. */
|
|
841
|
+
export interface RoutingStateStore {
|
|
842
|
+
read(key: string): Awaitable<RoutingState | null>;
|
|
843
|
+
update<T>(key: string, change: (state: RoutingState | null) => { state: RoutingState | null; value?: T }): Awaitable<T | undefined>;
|
|
844
|
+
}
|
|
845
|
+
export function createMemoryRoutingStore(): RoutingStateStore;
|
|
846
|
+
export interface RoutingRedisClient {
|
|
847
|
+
get(key: string): Promise<string | null>;
|
|
848
|
+
eval(script: string, options: { keys: string[]; arguments: string[] }): Promise<unknown>;
|
|
849
|
+
}
|
|
850
|
+
export function createRedisRoutingStore(options: { client: RoutingRedisClient; prefix?: string; ttlMs?: number; maxRetries?: number }): RoutingStateStore;
|
|
851
|
+
export interface RoutingPgConnection {
|
|
852
|
+
query(sql: string, params?: unknown[]): Promise<{ rows: Array<{ state?: RoutingState }> }>;
|
|
853
|
+
release(): void;
|
|
854
|
+
}
|
|
855
|
+
export interface RoutingPgPool {
|
|
856
|
+
query(sql: string, params?: unknown[]): Promise<{ rows: Array<{ state?: RoutingState }> }>;
|
|
857
|
+
connect(): Promise<RoutingPgConnection>;
|
|
858
|
+
}
|
|
859
|
+
export function createPostgresRoutingStore(options: { pool: RoutingPgPool; table?: string }): RoutingStateStore & { initialize(): Promise<void> };
|
|
860
|
+
export type AiKeys = string[] | string | ((ctx: Record<string, unknown>) => Awaitable<string[] | string | null | undefined>);
|
|
861
|
+
export interface ConfiguredProviderModel extends ProviderModel {
|
|
862
|
+
vision?: boolean;
|
|
863
|
+
visionOnly?: boolean;
|
|
864
|
+
extra?: Record<string, unknown>;
|
|
865
|
+
maxTokens?: number;
|
|
866
|
+
disabled?: boolean;
|
|
867
|
+
preview?: boolean;
|
|
868
|
+
languages?: string[];
|
|
869
|
+
noAnalytics?: boolean;
|
|
870
|
+
maxInputTokens?: number;
|
|
871
|
+
}
|
|
872
|
+
export interface ConfiguredProvider {
|
|
873
|
+
id: string;
|
|
874
|
+
format: 'openai' | 'gemini';
|
|
875
|
+
/** Full OpenAI chat URL, or Gemini models endpoint (without the model). */
|
|
876
|
+
url: string;
|
|
877
|
+
keys?: AiKeys;
|
|
878
|
+
keysByUsage?: Record<string, AiKeys>;
|
|
879
|
+
keyLane?: (ctx: Record<string, unknown>) => string | null | undefined;
|
|
880
|
+
/** False by default. Local providers work without API credentials. */
|
|
881
|
+
local?: boolean;
|
|
882
|
+
vision?: boolean;
|
|
883
|
+
extra?: Record<string, unknown>;
|
|
884
|
+
models: ConfiguredProviderModel[];
|
|
885
|
+
/** Share model quotas between credentials when they belong to one account. */
|
|
886
|
+
sharedKeyQuotas?: boolean;
|
|
887
|
+
maxConcurrent?: number;
|
|
888
|
+
maxQueue?: number;
|
|
889
|
+
/** Provider variable stem used by readAiConfigFromEnv. */
|
|
890
|
+
envName?: string;
|
|
891
|
+
}
|
|
892
|
+
export interface ConfiguredRouterConfig {
|
|
893
|
+
cooldownJitterMs?: number;
|
|
894
|
+
providers?: ConfiguredProvider[];
|
|
895
|
+
usages?: Partial<Record<AiUsage, RouteCandidate[]>>;
|
|
896
|
+
intentRouting?: Record<string, { preferred: string[] }>;
|
|
897
|
+
store?: RoutingStateStore;
|
|
898
|
+
statePrefix?: string;
|
|
899
|
+
fetch?: FetchLike;
|
|
900
|
+
now?: () => number;
|
|
901
|
+
quotaMargin?: number | { rpm?: number; rpd?: number; tpd?: number };
|
|
902
|
+
tokenBuffer?: number;
|
|
903
|
+
cooldownMs?: number;
|
|
904
|
+
degradedMs?: number;
|
|
905
|
+
maxFailures?: number;
|
|
906
|
+
waitedTimeoutMs?: number;
|
|
907
|
+
backgroundTimeoutMs?: number;
|
|
908
|
+
whenAllCooling?: 'skip' | 'try';
|
|
909
|
+
reserveVision?: boolean;
|
|
910
|
+
}
|
|
911
|
+
export interface ConfiguredRouteOptions {
|
|
912
|
+
usage?: AiUsage;
|
|
913
|
+
complexity?: string;
|
|
914
|
+
intent?: string;
|
|
915
|
+
candidates?: RouteCandidate[];
|
|
916
|
+
exclude?: string[];
|
|
917
|
+
language?: string;
|
|
918
|
+
hasAnalytics?: boolean;
|
|
919
|
+
estimatedTokens?: number;
|
|
920
|
+
purpose?: string;
|
|
921
|
+
waitedOn?: boolean;
|
|
922
|
+
timeoutMs?: number;
|
|
923
|
+
signal?: AbortSignal;
|
|
924
|
+
context?: Record<string, unknown>;
|
|
925
|
+
json?: boolean;
|
|
926
|
+
fallbackWithoutTools?: boolean;
|
|
927
|
+
accepts?: (answer: DetailedAnswer) => boolean;
|
|
928
|
+
}
|
|
929
|
+
export interface ConfiguredRouteResult extends RouteResult<DetailedAnswer> {
|
|
930
|
+
stateKey: string;
|
|
931
|
+
usage: AiUsage;
|
|
932
|
+
complexity: string;
|
|
933
|
+
fallback: boolean;
|
|
934
|
+
}
|
|
935
|
+
export interface ConfiguredRouter {
|
|
936
|
+
route(input: ChatRequest | string, options?: ConfiguredRouteOptions, context?: Record<string, unknown>): Promise<ConfiguredRouteResult>;
|
|
937
|
+
ask(input: ChatRequest | string, options?: ConfiguredRouteOptions, context?: Record<string, unknown>): Promise<DetailedAnswer>;
|
|
938
|
+
getStats(): Promise<Record<string, RoutingState>>;
|
|
939
|
+
/** Resets only state keys seen by this instance. */
|
|
940
|
+
reset(): Promise<void>;
|
|
941
|
+
stop(): void;
|
|
942
|
+
store: RoutingStateStore;
|
|
943
|
+
}
|
|
944
|
+
export function createConfiguredRouter(config?: ConfiguredRouterConfig): ConfiguredRouter;
|
|
945
|
+
export function estimateRequestComplexity(input: ChatRequest | string): 'simple' | 'medium' | 'complex';
|
|
946
|
+
export function estimateRequestTokens(request: ChatRequest): number;
|
|
947
|
+
export function readEnvironmentKeys(env: Record<string, string | undefined>, name: string): string[];
|
|
948
|
+
export interface AiEnvironmentConfig extends ConfiguredRouterConfig {
|
|
949
|
+
localModels: LocalModelsOptions;
|
|
950
|
+
language: ServiceLanguageOptions;
|
|
951
|
+
voice: SpeechServiceOptions & TranscriptionServiceOptions;
|
|
952
|
+
}
|
|
953
|
+
export function readAiConfigFromEnv(options?: { env?: Record<string, string | undefined>; prefix?: string }): AiEnvironmentConfig;
|
|
954
|
+
|
|
955
|
+
export class AiServiceError extends Error {
|
|
956
|
+
constructor(code: string, status?: number | null);
|
|
957
|
+
code: string;
|
|
958
|
+
status: number | null;
|
|
959
|
+
statusCode: number | null;
|
|
960
|
+
}
|
|
961
|
+
export type ServiceFetch = (url: string, init: RequestInit) => Promise<Pick<Response, 'ok' | 'status' | 'json' | 'arrayBuffer'>>;
|
|
962
|
+
export function createTokenFileReader(options?: {
|
|
963
|
+
tokenFile?: string;
|
|
964
|
+
read?: (path: string, encoding: 'utf8') => Promise<string>;
|
|
965
|
+
now?: () => number;
|
|
966
|
+
refreshMs?: number;
|
|
967
|
+
}): () => Promise<string | null>;
|
|
968
|
+
export interface ServiceRequestOptions {
|
|
969
|
+
fetch?: ServiceFetch;
|
|
970
|
+
timeoutMs?: number;
|
|
971
|
+
signal?: AbortSignal;
|
|
972
|
+
headers?: Record<string, string>;
|
|
973
|
+
body?: unknown;
|
|
974
|
+
rawBody?: BodyInit;
|
|
975
|
+
read?: 'json' | 'bytes';
|
|
976
|
+
}
|
|
977
|
+
export function serviceRequest(url: string | null | undefined, options?: ServiceRequestOptions & { read?: 'json' }): Promise<any>;
|
|
978
|
+
export function serviceRequest(url: string | null | undefined, options: ServiceRequestOptions & { read: 'bytes' }): Promise<Buffer>;
|
|
979
|
+
export function endpoint(base: string | null | undefined, path: string): string | null;
|
|
980
|
+
export const LOCAL_VECTOR_MODEL: string;
|
|
981
|
+
export const LOCAL_VECTOR_DIMENSIONS: number;
|
|
982
|
+
export function validPcmAudio(audio: unknown): boolean;
|
|
983
|
+
export interface LocalModelsOptions {
|
|
984
|
+
baseUrl?: string;
|
|
985
|
+
tokenFile?: string;
|
|
986
|
+
readTokenFile?: (path: string, encoding: 'utf8') => Promise<string>;
|
|
987
|
+
tokenRefreshMs?: number;
|
|
988
|
+
timeoutMs?: number;
|
|
989
|
+
maxFailures?: number;
|
|
990
|
+
cooldownMs?: number;
|
|
991
|
+
now?: () => number;
|
|
992
|
+
fetch?: ServiceFetch;
|
|
993
|
+
vectorModel?: string;
|
|
994
|
+
vectorDimensions?: number;
|
|
995
|
+
onError?: (error: AiServiceError, route: string) => void;
|
|
996
|
+
}
|
|
997
|
+
export interface LocalCallOptions { timeoutMs?: number; signal?: AbortSignal }
|
|
998
|
+
export interface NliPair { premisse: string; hypothese: string }
|
|
999
|
+
export interface NliResult { accord: number; neutre: number; contradiction: number }
|
|
1000
|
+
export interface LocalEntity { texte: string; type: string; debut: number; fin: number; score: number }
|
|
1001
|
+
export type LocalVectors = number[][] & { readonly modele: string; readonly dimensions: number };
|
|
1002
|
+
export interface LocalTranscription { texte: string; langue: string; logprob: number; sansParole: number; dureeMs: number }
|
|
1003
|
+
export interface LocalModelsClient {
|
|
1004
|
+
rerank(question: string, passages: string[], options?: LocalCallOptions): Promise<number[] | null>;
|
|
1005
|
+
compare(pairs: Array<{ premise: string; hypothesis: string }>, options?: LocalCallOptions): Promise<Array<{ entailment: number; neutral: number; contradiction: number }> | null>;
|
|
1006
|
+
entities(text: string, labels: string[], options?: LocalCallOptions): Promise<Array<{ text: string; type: string; start: number; end: number; score: number }> | null>;
|
|
1007
|
+
embed(texts: string[], options?: LocalCallOptions): Promise<LocalVectors | null>;
|
|
1008
|
+
reclasser(question: string, passages: string[], options?: LocalCallOptions): Promise<number[] | null>;
|
|
1009
|
+
nli(pairs: NliPair[], options?: LocalCallOptions): Promise<NliResult[] | null>;
|
|
1010
|
+
comparer(pairs: NliPair[], options?: LocalCallOptions): Promise<NliResult[] | null>;
|
|
1011
|
+
entites(text: string, labels: string[], options?: LocalCallOptions): Promise<LocalEntity[] | null>;
|
|
1012
|
+
vecteurs(texts: string[], options?: LocalCallOptions): Promise<LocalVectors | null>;
|
|
1013
|
+
transcrire(audio: string, options?: LocalCallOptions & { langue?: string; vocabulaire?: string }): Promise<LocalTranscription | null>;
|
|
1014
|
+
disponible(route: string): boolean;
|
|
1015
|
+
available(route: string): boolean;
|
|
1016
|
+
}
|
|
1017
|
+
export function createLocalModelsClient(options?: LocalModelsOptions): LocalModelsClient;
|
|
1018
|
+
export const DEFAULT_LANGUAGE_WORDS: Readonly<Record<string, string[]>>;
|
|
1019
|
+
export interface ServiceLanguageOptions extends Omit<Parameters<typeof createLanguageDetector>[0], 'words' | 'identify'> {
|
|
1020
|
+
words?: Record<string, string[]>;
|
|
1021
|
+
baseUrl?: string;
|
|
1022
|
+
fetch?: ServiceFetch;
|
|
1023
|
+
timeoutMs?: number;
|
|
1024
|
+
}
|
|
1025
|
+
export function createServiceLanguageDetector(options?: ServiceLanguageOptions): LanguageDetector;
|
|
1026
|
+
|
|
1027
|
+
export interface VoiceProviderOptions {
|
|
1028
|
+
keys?: string[] | string | (() => Awaitable<string[] | string | undefined | null>);
|
|
1029
|
+
models?: string[];
|
|
1030
|
+
timeoutMs?: number;
|
|
1031
|
+
cooldownMs?: number;
|
|
1032
|
+
}
|
|
1033
|
+
export interface TranscriptionServiceOptions {
|
|
1034
|
+
local?: Pick<LocalModelsClient, 'transcrire'>;
|
|
1035
|
+
localModels?: LocalModelsOptions;
|
|
1036
|
+
localTimeoutMs?: number;
|
|
1037
|
+
groq?: VoiceProviderOptions & { url?: string };
|
|
1038
|
+
cloudflare?: VoiceProviderOptions & { url?: string | ((model: string) => string) };
|
|
1039
|
+
fetch?: ServiceFetch;
|
|
1040
|
+
now?: () => number;
|
|
1041
|
+
maxAudioBytes?: number;
|
|
1042
|
+
noSpeechThreshold?: number;
|
|
1043
|
+
logprobThreshold?: number;
|
|
1044
|
+
stockPhrases?: readonly string[];
|
|
1045
|
+
}
|
|
1046
|
+
export interface TranscriptionInput {
|
|
1047
|
+
/** PCM16 little-endian, 16kHz mono by default; base64 or bytes. */
|
|
1048
|
+
audio: string | Uint8Array;
|
|
1049
|
+
format?: 'pcm16' | 'wav' | 'file';
|
|
1050
|
+
language?: string;
|
|
1051
|
+
vocabulary?: string;
|
|
1052
|
+
filename?: string;
|
|
1053
|
+
mimeType?: string;
|
|
1054
|
+
signal?: AbortSignal;
|
|
1055
|
+
allowRemote?: boolean;
|
|
1056
|
+
}
|
|
1057
|
+
export interface TranscriptionResult {
|
|
1058
|
+
text: string;
|
|
1059
|
+
language: string | null;
|
|
1060
|
+
provider: 'local' | 'groq' | 'cloudflare';
|
|
1061
|
+
model: string;
|
|
1062
|
+
fallback: boolean;
|
|
1063
|
+
confidence?: number;
|
|
1064
|
+
noSpeech?: boolean;
|
|
1065
|
+
}
|
|
1066
|
+
export function createTranscriptionService(options?: TranscriptionServiceOptions): { transcribe(input: TranscriptionInput): Promise<TranscriptionResult> };
|
|
1067
|
+
export interface SpeechServiceOptions {
|
|
1068
|
+
/** Synthesis order. Default: Gemini (online) first, Piper (local) as fallback. */
|
|
1069
|
+
order?: Array<'gemini' | 'piper'>;
|
|
1070
|
+
piper?: {
|
|
1071
|
+
baseUrl?: string;
|
|
1072
|
+
tokenFile?: string;
|
|
1073
|
+
readTokenFile?: (path: string, encoding: 'utf8') => Promise<string>;
|
|
1074
|
+
timeoutMs?: number;
|
|
1075
|
+
cooldownMs?: number;
|
|
1076
|
+
};
|
|
1077
|
+
gemini?: VoiceProviderOptions & { endpoint?: string; voice?: string };
|
|
1078
|
+
now?: () => number;
|
|
1079
|
+
fetch?: ServiceFetch;
|
|
1080
|
+
prepareText?: (text: string) => string;
|
|
1081
|
+
}
|
|
1082
|
+
export interface SpeechResult {
|
|
1083
|
+
audio: Buffer;
|
|
1084
|
+
format: 'wav';
|
|
1085
|
+
mimeType: 'audio/wav';
|
|
1086
|
+
sampleRate: number;
|
|
1087
|
+
channels: number;
|
|
1088
|
+
provider: 'piper' | 'gemini';
|
|
1089
|
+
model: string | null;
|
|
1090
|
+
fallback: boolean;
|
|
1091
|
+
}
|
|
1092
|
+
export function createSpeechService(options?: SpeechServiceOptions): { synthesize(input: { text: string; language?: string; signal?: AbortSignal; allowRemote?: boolean }): Promise<SpeechResult> };
|
|
1093
|
+
export function pcmToWav(pcm: Uint8Array, options?: { sampleRate?: number; channels?: number }): Buffer;
|
|
1094
|
+
export function readPcmWav(audio: Uint8Array): { pcm: Buffer; sampleRate: number; channels: number };
|
|
1095
|
+
export function speakableText(text: string, maxChars?: number): string;
|
|
1096
|
+
export function doubtfulTranscription(text: string, options?: { stockPhrases?: readonly string[] }): boolean;
|
|
1097
|
+
export const WHISPER_STOCK_PHRASES: readonly string[];
|
|
1098
|
+
export function toolRiskOf(tool: { risk?: string } | null | undefined): ToolRisk;
|
|
1099
|
+
export function waitsForToolConfirmation(tool: { risk?: string; kind?: string } | null | undefined, options?: { policy?: 'always' | 'never' | { threshold: ToolRisk }; untrusted?: boolean }): boolean;
|
|
1100
|
+
|
|
1101
|
+
export function readRetryAfter(response: { headers?: { get(name: string): string | null } }, now?: number): number | null;
|
|
1102
|
+
export function createInferenceQueue(options?: { maxConcurrent?: number; maxQueue?: number }): {
|
|
1103
|
+
run<T>(task: () => Awaitable<T>, options?: { signal?: AbortSignal }): Promise<T>;
|
|
1104
|
+
stats(): { active: number; waiting: number };
|
|
1105
|
+
};
|
package/src/index.js
CHANGED
|
@@ -18,5 +18,13 @@ module.exports = {
|
|
|
18
18
|
...require('./gemini'),
|
|
19
19
|
...require('./nativeTools'),
|
|
20
20
|
...require('./eventStream'),
|
|
21
|
-
...require('./webSearch')
|
|
21
|
+
...require('./webSearch'),
|
|
22
|
+
...require('./managedRouter'),
|
|
23
|
+
...require('./routingState'),
|
|
24
|
+
...require('./aiConfig'),
|
|
25
|
+
...require('./localModels'),
|
|
26
|
+
...require('./serviceHttp'),
|
|
27
|
+
...require('./serviceLanguage'),
|
|
28
|
+
...require('./voice'),
|
|
29
|
+
...require('./providerLimits')
|
|
22
30
|
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { createTokenFileReader, serviceRequest, endpoint, AiServiceError } = require('./serviceHttp');
|
|
3
|
+
|
|
4
|
+
const LOCAL_VECTOR_MODEL = 'bge-m3-onnx-int8';
|
|
5
|
+
const LOCAL_VECTOR_DIMENSIONS = 1024;
|
|
6
|
+
const text = (value) => typeof value === 'string' && Boolean(value.trim());
|
|
7
|
+
const finite = (value) => typeof value === 'number' && Number.isFinite(value);
|
|
8
|
+
const array = (value, max, validate) => Array.isArray(value) && value.length > 0 && value.length <= max && value.every(validate);
|
|
9
|
+
const probability = (value) => finite(value) && value >= 0 && value <= 1;
|
|
10
|
+
|
|
11
|
+
function validPcmAudio(audio) {
|
|
12
|
+
if (typeof audio !== 'string' || !audio || audio.length % 4 || !/^[A-Za-z0-9+/]*={0,2}$/.test(audio)) return false;
|
|
13
|
+
const size = audio.length / 4 * 3 - (audio.endsWith('==') ? 2 : audio.endsWith('=') ? 1 : 0);
|
|
14
|
+
return size >= 6400 && size <= 960000 && size % 2 === 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function createLocalModelsClient(options = {}) {
|
|
18
|
+
const circuits = new Map();
|
|
19
|
+
const now = options.now || Date.now;
|
|
20
|
+
const token = createTokenFileReader({ tokenFile: options.tokenFile, read: options.readTokenFile, now, refreshMs: options.tokenRefreshMs });
|
|
21
|
+
const model = options.vectorModel || LOCAL_VECTOR_MODEL;
|
|
22
|
+
const dimensions = options.vectorDimensions || LOCAL_VECTOR_DIMENSIONS;
|
|
23
|
+
const circuit = (path) => {
|
|
24
|
+
if (!circuits.has(path)) circuits.set(path, { failures: 0, until: 0, probing: false });
|
|
25
|
+
return circuits.get(path);
|
|
26
|
+
};
|
|
27
|
+
const report = (error, route) => {
|
|
28
|
+
if (typeof options.onError === 'function') {
|
|
29
|
+
try { options.onError(error, route); } catch { /* Diagnostics must not break an optional model. */ }
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
async function call(path, body, settings, defaultTimeout, validate) {
|
|
33
|
+
const state = circuit(path);
|
|
34
|
+
if (!options.baseUrl || now() < state.until || state.probing) return null;
|
|
35
|
+
const probe = state.until > 0;
|
|
36
|
+
if (probe) state.probing = true;
|
|
37
|
+
try {
|
|
38
|
+
const bearer = await token();
|
|
39
|
+
const timeoutMs = settings.timeoutMs ?? options.timeoutMs ?? defaultTimeout;
|
|
40
|
+
const result = await serviceRequest(endpoint(options.baseUrl, path), {
|
|
41
|
+
fetch: options.fetch, timeoutMs, signal: settings.signal, body,
|
|
42
|
+
headers: { 'X-Delai-Ms': String(timeoutMs), ...(bearer ? { Authorization: `Bearer ${bearer}` } : {}) }
|
|
43
|
+
});
|
|
44
|
+
const value = validate(result);
|
|
45
|
+
if (value === null) throw new AiServiceError('AI_SERVICE_RESPONSE');
|
|
46
|
+
Object.assign(state, { failures: 0, until: 0 });
|
|
47
|
+
return value;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if (settings.signal?.aborted) throw settings.signal.reason;
|
|
50
|
+
if (error.status >= 400 && error.status < 500) Object.assign(state, { failures: 0, until: 0 });
|
|
51
|
+
else {
|
|
52
|
+
state.failures += 1;
|
|
53
|
+
if (probe || state.failures >= (options.maxFailures ?? 3)) state.until = now() + (options.cooldownMs ?? 60000);
|
|
54
|
+
}
|
|
55
|
+
report(error, path);
|
|
56
|
+
return null;
|
|
57
|
+
} finally { state.probing = false; }
|
|
58
|
+
}
|
|
59
|
+
function invalid(path) { report(new AiServiceError('AI_SERVICE_INPUT'), path); return null; }
|
|
60
|
+
async function reclasser(question, passages, settings = {}) {
|
|
61
|
+
if (!text(question) || !array(passages, 50, (p) => typeof p === 'string')) return invalid('/reclasser');
|
|
62
|
+
return call('/reclasser', { question, passages }, settings, 1000, (data) => Array.isArray(data?.scores) && data.scores.length === passages.length && data.scores.every(finite) ? data.scores : null);
|
|
63
|
+
}
|
|
64
|
+
async function nli(paires, settings = {}) {
|
|
65
|
+
if (!array(paires, 20, (pair) => text(pair?.premisse) && text(pair?.hypothese))) return invalid('/nli');
|
|
66
|
+
return call('/nli', { paires }, settings, 1500, (data) => Array.isArray(data?.resultats) && data.resultats.length === paires.length && data.resultats.every((r) => probability(r?.accord) && probability(r?.neutre) && probability(r?.contradiction)) ? data.resultats : null);
|
|
67
|
+
}
|
|
68
|
+
async function entites(texte, etiquettes, settings = {}) {
|
|
69
|
+
if (!text(texte) || texte.length > 8000 || !array(etiquettes, 10, (label) => text(label) && label.length <= 50)) return invalid('/entites');
|
|
70
|
+
const labels = new Set(etiquettes.map((label) => label.trim()));
|
|
71
|
+
return call('/entites', { texte, etiquettes }, settings, 800, (data) => Array.isArray(data?.entites) && data.entites.every((e) => typeof e.texte === 'string' && labels.has(e.type) && Number.isInteger(e.debut) && Number.isInteger(e.fin) && e.debut >= 0 && e.fin > e.debut && e.fin <= texte.length && probability(e.score)) ? data.entites : null);
|
|
72
|
+
}
|
|
73
|
+
async function vecteurs(textes, settings = {}) {
|
|
74
|
+
if (!array(textes, 32, (t) => text(t) && t.length <= 2000)) return invalid('/vecteurs');
|
|
75
|
+
return call('/vecteurs', { textes }, settings, 1500, (data) => {
|
|
76
|
+
if (data?.modele !== model || data.dimensions !== dimensions || !Array.isArray(data.vecteurs) || data.vecteurs.length !== textes.length || !data.vecteurs.every((v) => Array.isArray(v) && v.length === dimensions && v.every(finite))) return null;
|
|
77
|
+
Object.defineProperties(data.vecteurs, { modele: { value: model }, dimensions: { value: dimensions } });
|
|
78
|
+
return data.vecteurs;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
async function transcrire(audio, { langue, vocabulaire = '', ...settings } = {}) {
|
|
82
|
+
if (!validPcmAudio(audio) || !['fr', 'en', 'es', 'ln'].includes(langue) || typeof vocabulaire !== 'string' || vocabulaire.length > 600) return invalid('/transcrire');
|
|
83
|
+
return call('/transcrire', { audio, langue, vocabulaire }, settings, 15000, (data) => typeof data?.texte === 'string' && text(data.langue) && finite(data.logprob) && probability(data.sans_parole) && finite(data.duree_ms) && data.duree_ms >= 0
|
|
84
|
+
? { texte: data.texte, langue: data.langue, logprob: data.logprob, sansParole: data.sans_parole, dureeMs: data.duree_ms } : null);
|
|
85
|
+
}
|
|
86
|
+
const disponible = (path) => Boolean(options.baseUrl) && now() >= circuit(path).until && !circuit(path).probing;
|
|
87
|
+
// English adapters plug directly into the package's generic source/mask APIs.
|
|
88
|
+
async function compare(pairs, settings = {}) {
|
|
89
|
+
if (!Array.isArray(pairs)) return invalid('/nli');
|
|
90
|
+
const results = await nli(pairs.map((pair) => ({ premisse: pair?.premise, hypothese: pair?.hypothesis })), settings);
|
|
91
|
+
return results?.map((result) => ({ entailment: result.accord, neutral: result.neutre, contradiction: result.contradiction })) ?? null;
|
|
92
|
+
}
|
|
93
|
+
async function entities(value, labels, settings = {}) {
|
|
94
|
+
const results = await entites(value, labels, settings);
|
|
95
|
+
return results?.map((entity) => ({ text: entity.texte, type: entity.type, start: entity.debut, end: entity.fin, score: entity.score })) ?? null;
|
|
96
|
+
}
|
|
97
|
+
return { reclasser, rerank: reclasser, nli, comparer: nli, compare, entites, entities, vecteurs, embed: vecteurs, transcrire, disponible, available: disponible };
|
|
98
|
+
}
|
|
99
|
+
module.exports = { createLocalModelsClient, LOCAL_VECTOR_MODEL, LOCAL_VECTOR_DIMENSIONS, validPcmAudio };
|