@micdrop/gemini 1.1.0 → 1.3.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/dist/index.mjs CHANGED
@@ -1,8 +1,61 @@
1
1
  // src/GeminiAgent.ts
2
2
  import { GoogleGenAI } from "@google/genai";
3
3
  import { Agent } from "@micdrop/server";
4
+
5
+ // src/models.ts
6
+ import { cachedList, cachedModels } from "@micdrop/server";
7
+ function fetchAll(apiKey) {
8
+ return cachedList(["gemini-raw-models"], apiKey, async () => {
9
+ const models = [];
10
+ let pageToken;
11
+ do {
12
+ const params = new URLSearchParams({ pageSize: "1000", key: apiKey });
13
+ if (pageToken) params.set("pageToken", pageToken);
14
+ const response = await fetch(
15
+ `https://generativelanguage.googleapis.com/v1beta/models?${params}`
16
+ );
17
+ if (!response.ok) throw new Error(`Gemini answered ${response.status}`);
18
+ const page = await response.json();
19
+ models.push(...page.models);
20
+ pageToken = page.nextPageToken;
21
+ } while (pageToken);
22
+ return models;
23
+ });
24
+ }
25
+ function kindOf(model) {
26
+ const id = model.name.replace(/^models\//, "");
27
+ const methods = model.supportedGenerationMethods ?? [];
28
+ if (!id.startsWith("gemini-")) return void 0;
29
+ if (/-tts\b/.test(id)) return "tts";
30
+ if (methods.includes("bidiGenerateContent")) {
31
+ if (id.includes("transcribe")) return "stt";
32
+ if (/translate|robotics/.test(id)) return void 0;
33
+ return "live";
34
+ }
35
+ if (!methods.includes("generateContent")) return void 0;
36
+ if (/image|embedding|transcribe|robotics|computer-use|omni|nano-banana/.test(id)) {
37
+ return void 0;
38
+ }
39
+ return "agent";
40
+ }
41
+ function listGeminiModels(kind, options) {
42
+ return cachedModels(
43
+ ["gemini", kind],
44
+ options.apiKey,
45
+ async () => (await fetchAll(options.apiKey)).filter((model) => kindOf(model) === kind).map((model) => ({
46
+ id: model.name.replace(/^models\//, ""),
47
+ name: model.displayName,
48
+ description: model.description,
49
+ reasoning: !!model.thinking
50
+ })),
51
+ options.refresh
52
+ );
53
+ }
54
+
55
+ // src/GeminiAgent.ts
4
56
  import z, { toJSONSchema } from "zod";
5
57
  var DEFAULT_MODEL = "gemini-3.8-flash";
58
+ var API_VERSION = "v1beta";
6
59
  var DEFAULT_MAX_STEPS = 5;
7
60
  var DEFAULT_MAX_RETRIES = 3;
8
61
  var DEFAULT_RETRY_DELAY = 1e3;
@@ -15,6 +68,15 @@ var GeminiAgent = class extends Agent {
15
68
  this.signatures = /* @__PURE__ */ new Map();
16
69
  this.genai = "genai" in options ? options.genai : new GoogleGenAI({ apiKey: options.apiKey });
17
70
  }
71
+ /**
72
+ * Lists the models that can answer a call. `reasoning` marks the ones that think, see `thinkingLevel`.
73
+ *
74
+ * The list is fetched once and shared by every call of the process for a
75
+ * day. `refresh` fetches it again.
76
+ */
77
+ static listModels(options) {
78
+ return listGeminiModels("agent", options);
79
+ }
18
80
  generateAnswer(stream, signal) {
19
81
  return this.generateStep(stream, signal, 0);
20
82
  }
@@ -61,11 +123,11 @@ var GeminiAgent = class extends Agent {
61
123
  }
62
124
  /** Streams one request, writing the text of the answer as it comes */
63
125
  async streamStep(stream, signal, progress) {
64
- let extracting = false;
65
- const extractOptions = this.getExtractOptions();
126
+ const write = this.answerWriter(stream);
66
127
  const response = await this.genai.interactions.create(
67
128
  {
68
129
  model: this.options.model || DEFAULT_MODEL,
130
+ api_version: API_VERSION,
69
131
  store: false,
70
132
  stream: true,
71
133
  system_instruction: this.buildSystemInstruction(),
@@ -84,6 +146,12 @@ var GeminiAgent = class extends Agent {
84
146
  let skipAnswer = false;
85
147
  let calledTool = false;
86
148
  let signature = "";
149
+ const writeText = (text) => {
150
+ if (!text) return;
151
+ this.log(`Answer chunk: "${text}"`);
152
+ progress.answer += text;
153
+ write(text);
154
+ };
87
155
  for await (const event of response) {
88
156
  if (signal.aborted) break;
89
157
  switch (event.event_type) {
@@ -96,6 +164,11 @@ var GeminiAgent = class extends Agent {
96
164
  arguments: "",
97
165
  signature: ""
98
166
  };
167
+ if (event.step.type === "model_output") {
168
+ for (const content of event.step.content ?? []) {
169
+ if (content?.type === "text") writeText(content.text);
170
+ }
171
+ }
99
172
  break;
100
173
  case "step.delta": {
101
174
  const step = steps[event.index];
@@ -106,18 +179,7 @@ var GeminiAgent = class extends Agent {
106
179
  } else if (delta.type === "arguments_delta") {
107
180
  step.arguments += delta.arguments ?? "";
108
181
  } else if (delta.type === "text" && step.type === "model_output") {
109
- this.log(`Answer chunk: "${delta.text}"`);
110
- progress.answer += delta.text;
111
- if (extractOptions) {
112
- if (extracting) break;
113
- const startTagIndex = delta.text.indexOf(extractOptions.startTag);
114
- if (startTagIndex !== -1) {
115
- extracting = true;
116
- stream.write(delta.text.slice(0, startTagIndex).trimEnd());
117
- break;
118
- }
119
- }
120
- stream.write(delta.text);
182
+ writeText(delta.text);
121
183
  }
122
184
  break;
123
185
  }
@@ -238,9 +300,74 @@ function parseArguments(parameters) {
238
300
  }
239
301
  }
240
302
 
303
+ // src/GeminiClassifier.ts
304
+ import { GoogleGenAI as GoogleGenAI2 } from "@google/genai";
305
+ import { Classifier } from "@micdrop/server";
306
+ function noul(instructions, criteria) {
307
+ return {
308
+ type: "noul",
309
+ instructions,
310
+ criteria: criteria && {
311
+ true_description: criteria.true,
312
+ false_description: criteria.false
313
+ }
314
+ };
315
+ }
316
+ function choice(instructions, criteria) {
317
+ return { type: "choice", instructions, criteria };
318
+ }
319
+ function score(instructions, levels) {
320
+ return { type: "score", instructions, criteria: levels };
321
+ }
322
+ var DEFAULT_MODEL2 = "gemini-decision-1";
323
+ var DEFAULT_TIMEOUT = 3e3;
324
+ var API_VERSION2 = "v1beta";
325
+ var GeminiClassifier = class extends Classifier {
326
+ constructor(options) {
327
+ super();
328
+ this.options = options;
329
+ this.genai = "genai" in options ? options.genai : new GoogleGenAI2({ apiKey: options.apiKey });
330
+ }
331
+ async evaluate(input, signal) {
332
+ const { questions, model, timeout } = this.options;
333
+ const asked = typeof questions === "function" ? questions(input) : questions;
334
+ const decision = await this.genai.decisions.create(
335
+ {
336
+ model: model || DEFAULT_MODEL2,
337
+ input: this.buildInput(input),
338
+ questions: asked,
339
+ // Only on v1beta, while the client defaults to v1
340
+ api_version: API_VERSION2
341
+ },
342
+ {
343
+ // The SDK drops its timeout when a signal is given
344
+ signal: AbortSignal.any([
345
+ signal,
346
+ AbortSignal.timeout(timeout ?? DEFAULT_TIMEOUT)
347
+ ]),
348
+ retries: { strategy: "none" }
349
+ }
350
+ );
351
+ const blocked = decision.prompt_feedback?.block_reason;
352
+ if (blocked) {
353
+ const message = decision.prompt_feedback?.block_reason_message;
354
+ throw new Error(
355
+ `Input blocked by Gemini (${blocked})${message ? `: ${message}` : ""}`
356
+ );
357
+ }
358
+ return {
359
+ answers: decision.answers ?? {},
360
+ usage: decision.usage
361
+ };
362
+ }
363
+ buildInput(input) {
364
+ if (this.options.input) return this.options.input(input);
365
+ return typeof input === "string" ? input : JSON.stringify(input);
366
+ }
367
+ };
368
+
241
369
  // src/GeminiLive.ts
242
370
  import {
243
- Pcm16Resampler,
244
371
  Realtime
245
372
  } from "@micdrop/server";
246
373
  import { toJSONSchema as toJSONSchema2 } from "zod";
@@ -397,7 +524,7 @@ function parseMessage(text) {
397
524
  }
398
525
 
399
526
  // src/GeminiLive.ts
400
- var DEFAULT_MODEL2 = "gemini-3.8-live";
527
+ var DEFAULT_MODEL3 = "gemini-3.8-live";
401
528
  var SAMPLE_RATE2 = 16e3;
402
529
  var GEMINI_OUTPUT_SAMPLE_RATE = 24e3;
403
530
  var TOOL_FOLLOW_UP_TIMEOUT = 1e4;
@@ -410,10 +537,6 @@ var GeminiLive = class extends Realtime {
410
537
  super(options);
411
538
  this.everConnected = false;
412
539
  this.goingAway = false;
413
- this.outputResampler = new Pcm16Resampler(
414
- GEMINI_OUTPUT_SAMPLE_RATE,
415
- SAMPLE_RATE2
416
- );
417
540
  this.hasTranscribedTurn = false;
418
541
  // Transcript of a dropped turn, which the conversation should not show
419
542
  this.ignoreUserTranscript = false;
@@ -487,6 +610,15 @@ var GeminiLive = class extends Realtime {
487
610
  this.socket.on("Failed", () => this.emit("Failed"));
488
611
  this.socket.connect();
489
612
  }
613
+ /**
614
+ * Lists the Live models, which hear and speak.
615
+ *
616
+ * The list is fetched once and shared by every call of the process for a
617
+ * day. `refresh` fetches it again.
618
+ */
619
+ static listModels(options) {
620
+ return listGeminiModels("live", options);
621
+ }
490
622
  /**
491
623
  * Stops the answer. The tools it is waiting for still send their results,
492
624
  * silently, since a blocking call left unanswered would stall the model.
@@ -529,8 +661,10 @@ var GeminiLive = class extends Realtime {
529
661
  `Say exactly the following text, word for word, and nothing else: ${text}`
530
662
  );
531
663
  }
664
+ get sampleRate() {
665
+ return GEMINI_OUTPUT_SAMPLE_RATE;
666
+ }
532
667
  cancelAnswer() {
533
- this.outputResampler.reset();
534
668
  }
535
669
  get isIdle() {
536
670
  return super.isIdle && this.toolRound.running === 0;
@@ -572,7 +706,7 @@ var GeminiLive = class extends Realtime {
572
706
  }));
573
707
  return {
574
708
  setup: {
575
- model: `models/${this.options.model || DEFAULT_MODEL2}`,
709
+ model: `models/${this.options.model || DEFAULT_MODEL3}`,
576
710
  generationConfig: {
577
711
  responseModalities: ["AUDIO"],
578
712
  ...this.options.thinkingLevel ? { thinkingConfig: { thinkingLevel: this.options.thinkingLevel } } : {},
@@ -628,7 +762,7 @@ var GeminiLive = class extends Realtime {
628
762
  const data = part.inlineData?.data;
629
763
  if (!data) continue;
630
764
  this.onModelOutput();
631
- const output = this.outputResampler.process(Buffer.from(data, "base64"));
765
+ const output = Buffer.from(data, "base64");
632
766
  if (output.length > 0) this.emitAudio(output);
633
767
  }
634
768
  if (content.outputTranscription?.text) {
@@ -637,14 +771,12 @@ var GeminiLive = class extends Realtime {
637
771
  }
638
772
  if (content.interrupted) {
639
773
  this.log("Interrupted");
640
- this.outputResampler.reset();
641
774
  }
642
775
  if (content.generationComplete) {
643
776
  this.commitTurnTranscript();
644
777
  }
645
778
  if (content.turnComplete) {
646
779
  this.commitTurnTranscript();
647
- this.outputResampler.reset();
648
780
  if (this.toolRound.running > 0 || this.toolRound.followUp) {
649
781
  this.toolRound.answerEndDeferred = true;
650
782
  } else {
@@ -735,7 +867,7 @@ var GeminiLive = class extends Realtime {
735
867
 
736
868
  // src/GeminiSTT.ts
737
869
  import { STT } from "@micdrop/server";
738
- var DEFAULT_MODEL3 = "gemini-3.5-transcribe-live";
870
+ var DEFAULT_MODEL4 = "gemini-3.5-transcribe-live";
739
871
  var SAMPLE_RATE3 = 16e3;
740
872
  var DEFAULT_TRANSCRIPTION_TIMEOUT = 4e3;
741
873
  var GeminiSTT = class extends STT {
@@ -798,6 +930,15 @@ var GeminiSTT = class extends STT {
798
930
  this.socket.on("Failed", this.onFailed);
799
931
  this.socket.connect();
800
932
  }
933
+ /**
934
+ * Lists the models that transcribe a Live session.
935
+ *
936
+ * The list is fetched once and shared by every call of the process for a
937
+ * day. `refresh` fetches it again.
938
+ */
939
+ static listModels(options) {
940
+ return listGeminiModels("stt", options);
941
+ }
801
942
  transcribe(audioStream) {
802
943
  let utterance;
803
944
  audioStream.on("data", (chunk) => {
@@ -818,7 +959,7 @@ var GeminiSTT = class extends STT {
818
959
  buildSetup() {
819
960
  return {
820
961
  setup: {
821
- model: `models/${this.options.model || DEFAULT_MODEL3}`,
962
+ model: `models/${this.options.model || DEFAULT_MODEL4}`,
822
963
  generationConfig: { responseModalities: ["TEXT"] },
823
964
  inputAudioTranscription: {
824
965
  languageCodes: this.options.language ? [this.options.language] : [],
@@ -914,18 +1055,77 @@ var GeminiSTT = class extends STT {
914
1055
  };
915
1056
 
916
1057
  // src/GeminiTTS.ts
917
- import { GoogleGenAI as GoogleGenAI2 } from "@google/genai";
918
- import { Pcm16Resampler as Pcm16Resampler2, SentenceTTS } from "@micdrop/server";
919
- var DEFAULT_MODEL4 = "gemini-2.5-flash-preview-tts";
1058
+ import { GoogleGenAI as GoogleGenAI3 } from "@google/genai";
1059
+ import { SentenceTTS } from "@micdrop/server";
1060
+ var DEFAULT_MODEL5 = "gemini-3.8-flash-tts";
1061
+ var API_VERSION3 = "v1beta";
920
1062
  var DEFAULT_VOICE = "Kore";
921
1063
  var GEMINI_SAMPLE_RATE = 24e3;
922
- var OUTPUT_SAMPLE_RATE = 16e3;
923
1064
  var DEFAULT_MAX_RETRY = 2;
1065
+ var VOICES = [
1066
+ ["Zephyr", "Bright", "female"],
1067
+ ["Puck", "Upbeat", "male"],
1068
+ ["Charon", "Informative", "male"],
1069
+ ["Kore", "Firm", "female"],
1070
+ ["Fenrir", "Excitable", "male"],
1071
+ ["Leda", "Youthful", "female"],
1072
+ ["Orus", "Firm", "male"],
1073
+ ["Aoede", "Breezy", "female"],
1074
+ ["Callirrhoe", "Easy-going", "female"],
1075
+ ["Laomedeia", "Upbeat", "female"],
1076
+ ["Autonoe", "Bright", "female"],
1077
+ ["Enceladus", "Breathy", "male"],
1078
+ ["Iapetus", "Clear", "male"],
1079
+ ["Umbriel", "Easy-going", "male"],
1080
+ ["Algieba", "Smooth", "male"],
1081
+ ["Despina", "Smooth", "female"],
1082
+ ["Erinome", "Clear", "female"],
1083
+ ["Algenib", "Gravelly", "male"],
1084
+ ["Rasalgethi", "Informative", "male"],
1085
+ ["Achernar", "Soft", "female"],
1086
+ ["Alnilam", "Firm", "male"],
1087
+ ["Schedar", "Even", "male"],
1088
+ ["Gacrux", "Mature", "female"],
1089
+ ["Pulcherrima", "Forward", "female"],
1090
+ ["Achird", "Friendly", "male"],
1091
+ ["Zubenelgenubi", "Casual", "male"],
1092
+ ["Vindemiatrix", "Gentle", "female"],
1093
+ ["Sadachbia", "Lively", "male"],
1094
+ ["Sadaltager", "Knowledgeable", "male"],
1095
+ ["Sulafat", "Warm", "female"]
1096
+ ];
924
1097
  var GeminiTTS = class extends SentenceTTS {
925
1098
  constructor(options) {
926
1099
  super();
927
1100
  this.options = options;
928
- this.genai = "genai" in options ? options.genai : new GoogleGenAI2({ apiKey: options.apiKey });
1101
+ // Read from the audio, which states its rate
1102
+ this.rate = GEMINI_SAMPLE_RATE;
1103
+ this.genai = "genai" in options ? options.genai : new GoogleGenAI3({ apiKey: options.apiKey });
1104
+ }
1105
+ /**
1106
+ * Lists the models that speak.
1107
+ *
1108
+ * The list is fetched once and shared by every call of the process for a
1109
+ * day. `refresh` fetches it again.
1110
+ */
1111
+ static listModels(options) {
1112
+ return listGeminiModels("tts", options);
1113
+ }
1114
+ /**
1115
+ * Lists the prebuilt voices, which speak every language the model does, so
1116
+ * `language` keeps them all. Gemini has no listing, so the list is written
1117
+ * here.
1118
+ */
1119
+ static async listVoices(options = {}) {
1120
+ return VOICES.map(([name, tone, gender]) => ({
1121
+ id: name,
1122
+ name,
1123
+ gender,
1124
+ description: tone
1125
+ }));
1126
+ }
1127
+ get sampleRate() {
1128
+ return this.rate;
929
1129
  }
930
1130
  async synthesize(text, signal) {
931
1131
  const input = this.options.instructions ? `${this.options.instructions}: ${text}` : text;
@@ -947,18 +1147,18 @@ var GeminiTTS = class extends SentenceTTS {
947
1147
  async synthesizeOnce(input, signal, progress) {
948
1148
  const stream = await this.genai.interactions.create(
949
1149
  {
950
- model: this.options.model || DEFAULT_MODEL4,
1150
+ model: this.options.model || DEFAULT_MODEL5,
951
1151
  store: false,
952
1152
  stream: true,
953
1153
  input,
954
1154
  response_format: { type: "audio" },
955
1155
  generation_config: {
956
1156
  speech_config: [{ voice: this.options.voice || DEFAULT_VOICE }]
957
- }
1157
+ },
1158
+ api_version: API_VERSION3
958
1159
  },
959
1160
  { fetchOptions: { signal } }
960
1161
  );
961
- let resampler;
962
1162
  for await (const event of stream) {
963
1163
  if (signal.aborted) break;
964
1164
  if (event.event_type === "error") {
@@ -972,11 +1172,8 @@ var GeminiTTS = class extends SentenceTTS {
972
1172
  }
973
1173
  const { data, sample_rate } = event.delta;
974
1174
  if (!data) continue;
975
- resampler ?? (resampler = new Pcm16Resampler2(
976
- sample_rate || GEMINI_SAMPLE_RATE,
977
- OUTPUT_SAMPLE_RATE
978
- ));
979
- const audio = resampler.process(Buffer.from(data, "base64"));
1175
+ this.rate = sample_rate || GEMINI_SAMPLE_RATE;
1176
+ const audio = Buffer.from(data, "base64");
980
1177
  if (audio.length > 0) progress.emitted = true;
981
1178
  if (!this.emitAudio(audio)) break;
982
1179
  }
@@ -987,8 +1184,12 @@ function isBlocked(error) {
987
1184
  }
988
1185
  export {
989
1186
  GeminiAgent,
1187
+ GeminiClassifier,
990
1188
  GeminiLive,
991
1189
  GeminiSTT,
992
- GeminiTTS
1190
+ GeminiTTS,
1191
+ choice,
1192
+ noul,
1193
+ score
993
1194
  };
994
1195
  //# sourceMappingURL=index.mjs.map