@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.js CHANGED
@@ -31,22 +31,79 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
33
  GeminiAgent: () => GeminiAgent,
34
+ GeminiClassifier: () => GeminiClassifier,
34
35
  GeminiLive: () => GeminiLive,
35
36
  GeminiSTT: () => GeminiSTT,
36
- GeminiTTS: () => GeminiTTS
37
+ GeminiTTS: () => GeminiTTS,
38
+ choice: () => choice,
39
+ noul: () => noul,
40
+ score: () => score
37
41
  });
38
42
  module.exports = __toCommonJS(index_exports);
39
43
 
40
44
  // src/GeminiAgent.ts
41
45
  var import_genai = require("@google/genai");
46
+ var import_server2 = require("@micdrop/server");
47
+
48
+ // src/models.ts
42
49
  var import_server = require("@micdrop/server");
50
+ function fetchAll(apiKey) {
51
+ return (0, import_server.cachedList)(["gemini-raw-models"], apiKey, async () => {
52
+ const models = [];
53
+ let pageToken;
54
+ do {
55
+ const params = new URLSearchParams({ pageSize: "1000", key: apiKey });
56
+ if (pageToken) params.set("pageToken", pageToken);
57
+ const response = await fetch(
58
+ `https://generativelanguage.googleapis.com/v1beta/models?${params}`
59
+ );
60
+ if (!response.ok) throw new Error(`Gemini answered ${response.status}`);
61
+ const page = await response.json();
62
+ models.push(...page.models);
63
+ pageToken = page.nextPageToken;
64
+ } while (pageToken);
65
+ return models;
66
+ });
67
+ }
68
+ function kindOf(model) {
69
+ const id = model.name.replace(/^models\//, "");
70
+ const methods = model.supportedGenerationMethods ?? [];
71
+ if (!id.startsWith("gemini-")) return void 0;
72
+ if (/-tts\b/.test(id)) return "tts";
73
+ if (methods.includes("bidiGenerateContent")) {
74
+ if (id.includes("transcribe")) return "stt";
75
+ if (/translate|robotics/.test(id)) return void 0;
76
+ return "live";
77
+ }
78
+ if (!methods.includes("generateContent")) return void 0;
79
+ if (/image|embedding|transcribe|robotics|computer-use|omni|nano-banana/.test(id)) {
80
+ return void 0;
81
+ }
82
+ return "agent";
83
+ }
84
+ function listGeminiModels(kind, options) {
85
+ return (0, import_server.cachedModels)(
86
+ ["gemini", kind],
87
+ options.apiKey,
88
+ async () => (await fetchAll(options.apiKey)).filter((model) => kindOf(model) === kind).map((model) => ({
89
+ id: model.name.replace(/^models\//, ""),
90
+ name: model.displayName,
91
+ description: model.description,
92
+ reasoning: !!model.thinking
93
+ })),
94
+ options.refresh
95
+ );
96
+ }
97
+
98
+ // src/GeminiAgent.ts
43
99
  var import_zod = __toESM(require("zod"));
44
100
  var DEFAULT_MODEL = "gemini-3.8-flash";
101
+ var API_VERSION = "v1beta";
45
102
  var DEFAULT_MAX_STEPS = 5;
46
103
  var DEFAULT_MAX_RETRIES = 3;
47
104
  var DEFAULT_RETRY_DELAY = 1e3;
48
105
  var FIRST_MESSAGE_PROMPT = "The call starts now.";
49
- var GeminiAgent = class extends import_server.Agent {
106
+ var GeminiAgent = class extends import_server2.Agent {
50
107
  constructor(options) {
51
108
  super(options);
52
109
  // Gemini asks for the signature of the thought behind a tool call when the
@@ -54,6 +111,15 @@ var GeminiAgent = class extends import_server.Agent {
54
111
  this.signatures = /* @__PURE__ */ new Map();
55
112
  this.genai = "genai" in options ? options.genai : new import_genai.GoogleGenAI({ apiKey: options.apiKey });
56
113
  }
114
+ /**
115
+ * Lists the models that can answer a call. `reasoning` marks the ones that think, see `thinkingLevel`.
116
+ *
117
+ * The list is fetched once and shared by every call of the process for a
118
+ * day. `refresh` fetches it again.
119
+ */
120
+ static listModels(options) {
121
+ return listGeminiModels("agent", options);
122
+ }
57
123
  generateAnswer(stream, signal) {
58
124
  return this.generateStep(stream, signal, 0);
59
125
  }
@@ -100,11 +166,11 @@ var GeminiAgent = class extends import_server.Agent {
100
166
  }
101
167
  /** Streams one request, writing the text of the answer as it comes */
102
168
  async streamStep(stream, signal, progress) {
103
- let extracting = false;
104
- const extractOptions = this.getExtractOptions();
169
+ const write = this.answerWriter(stream);
105
170
  const response = await this.genai.interactions.create(
106
171
  {
107
172
  model: this.options.model || DEFAULT_MODEL,
173
+ api_version: API_VERSION,
108
174
  store: false,
109
175
  stream: true,
110
176
  system_instruction: this.buildSystemInstruction(),
@@ -123,6 +189,12 @@ var GeminiAgent = class extends import_server.Agent {
123
189
  let skipAnswer = false;
124
190
  let calledTool = false;
125
191
  let signature = "";
192
+ const writeText = (text) => {
193
+ if (!text) return;
194
+ this.log(`Answer chunk: "${text}"`);
195
+ progress.answer += text;
196
+ write(text);
197
+ };
126
198
  for await (const event of response) {
127
199
  if (signal.aborted) break;
128
200
  switch (event.event_type) {
@@ -135,6 +207,11 @@ var GeminiAgent = class extends import_server.Agent {
135
207
  arguments: "",
136
208
  signature: ""
137
209
  };
210
+ if (event.step.type === "model_output") {
211
+ for (const content of event.step.content ?? []) {
212
+ if (content?.type === "text") writeText(content.text);
213
+ }
214
+ }
138
215
  break;
139
216
  case "step.delta": {
140
217
  const step = steps[event.index];
@@ -145,18 +222,7 @@ var GeminiAgent = class extends import_server.Agent {
145
222
  } else if (delta.type === "arguments_delta") {
146
223
  step.arguments += delta.arguments ?? "";
147
224
  } else if (delta.type === "text" && step.type === "model_output") {
148
- this.log(`Answer chunk: "${delta.text}"`);
149
- progress.answer += delta.text;
150
- if (extractOptions) {
151
- if (extracting) break;
152
- const startTagIndex = delta.text.indexOf(extractOptions.startTag);
153
- if (startTagIndex !== -1) {
154
- extracting = true;
155
- stream.write(delta.text.slice(0, startTagIndex).trimEnd());
156
- break;
157
- }
158
- }
159
- stream.write(delta.text);
225
+ writeText(delta.text);
160
226
  }
161
227
  break;
162
228
  }
@@ -277,12 +343,78 @@ function parseArguments(parameters) {
277
343
  }
278
344
  }
279
345
 
280
- // src/GeminiLive.ts
346
+ // src/GeminiClassifier.ts
347
+ var import_genai2 = require("@google/genai");
281
348
  var import_server3 = require("@micdrop/server");
349
+ function noul(instructions, criteria) {
350
+ return {
351
+ type: "noul",
352
+ instructions,
353
+ criteria: criteria && {
354
+ true_description: criteria.true,
355
+ false_description: criteria.false
356
+ }
357
+ };
358
+ }
359
+ function choice(instructions, criteria) {
360
+ return { type: "choice", instructions, criteria };
361
+ }
362
+ function score(instructions, levels) {
363
+ return { type: "score", instructions, criteria: levels };
364
+ }
365
+ var DEFAULT_MODEL2 = "gemini-decision-1";
366
+ var DEFAULT_TIMEOUT = 3e3;
367
+ var API_VERSION2 = "v1beta";
368
+ var GeminiClassifier = class extends import_server3.Classifier {
369
+ constructor(options) {
370
+ super();
371
+ this.options = options;
372
+ this.genai = "genai" in options ? options.genai : new import_genai2.GoogleGenAI({ apiKey: options.apiKey });
373
+ }
374
+ async evaluate(input, signal) {
375
+ const { questions, model, timeout } = this.options;
376
+ const asked = typeof questions === "function" ? questions(input) : questions;
377
+ const decision = await this.genai.decisions.create(
378
+ {
379
+ model: model || DEFAULT_MODEL2,
380
+ input: this.buildInput(input),
381
+ questions: asked,
382
+ // Only on v1beta, while the client defaults to v1
383
+ api_version: API_VERSION2
384
+ },
385
+ {
386
+ // The SDK drops its timeout when a signal is given
387
+ signal: AbortSignal.any([
388
+ signal,
389
+ AbortSignal.timeout(timeout ?? DEFAULT_TIMEOUT)
390
+ ]),
391
+ retries: { strategy: "none" }
392
+ }
393
+ );
394
+ const blocked = decision.prompt_feedback?.block_reason;
395
+ if (blocked) {
396
+ const message = decision.prompt_feedback?.block_reason_message;
397
+ throw new Error(
398
+ `Input blocked by Gemini (${blocked})${message ? `: ${message}` : ""}`
399
+ );
400
+ }
401
+ return {
402
+ answers: decision.answers ?? {},
403
+ usage: decision.usage
404
+ };
405
+ }
406
+ buildInput(input) {
407
+ if (this.options.input) return this.options.input(input);
408
+ return typeof input === "string" ? input : JSON.stringify(input);
409
+ }
410
+ };
411
+
412
+ // src/GeminiLive.ts
413
+ var import_server5 = require("@micdrop/server");
282
414
  var import_zod2 = require("zod");
283
415
 
284
416
  // src/LiveSocket.ts
285
- var import_server2 = require("@micdrop/server");
417
+ var import_server4 = require("@micdrop/server");
286
418
  var import_eventemitter3 = require("eventemitter3");
287
419
  var URL = "wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent";
288
420
  var SAMPLE_RATE = 16e3;
@@ -329,7 +461,7 @@ var LiveSocket = class extends import_eventemitter3.EventEmitter {
329
461
  this.clearPending();
330
462
  this.emit("Failed");
331
463
  };
332
- this.socket = new import_server2.ReconnectingSocket({
464
+ this.socket = new import_server4.ReconnectingSocket({
333
465
  url: `${URL}?key=${encodeURIComponent(options.apiKey)}`,
334
466
  connectionTimeout: options.connectionTimeout,
335
467
  retryDelay: options.retryDelay,
@@ -431,7 +563,7 @@ function parseMessage(text) {
431
563
  }
432
564
 
433
565
  // src/GeminiLive.ts
434
- var DEFAULT_MODEL2 = "gemini-3.8-live";
566
+ var DEFAULT_MODEL3 = "gemini-3.8-live";
435
567
  var SAMPLE_RATE2 = 16e3;
436
568
  var GEMINI_OUTPUT_SAMPLE_RATE = 24e3;
437
569
  var TOOL_FOLLOW_UP_TIMEOUT = 1e4;
@@ -439,15 +571,11 @@ function newToolRound() {
439
571
  return { running: 0, answerEndDeferred: false };
440
572
  }
441
573
  var FIRST_MESSAGE_PROMPT2 = "The call starts now. Speak first, as your instructions say.";
442
- var GeminiLive = class extends import_server3.Realtime {
574
+ var GeminiLive = class extends import_server5.Realtime {
443
575
  constructor(options) {
444
576
  super(options);
445
577
  this.everConnected = false;
446
578
  this.goingAway = false;
447
- this.outputResampler = new import_server3.Pcm16Resampler(
448
- GEMINI_OUTPUT_SAMPLE_RATE,
449
- SAMPLE_RATE2
450
- );
451
579
  this.hasTranscribedTurn = false;
452
580
  // Transcript of a dropped turn, which the conversation should not show
453
581
  this.ignoreUserTranscript = false;
@@ -521,6 +649,15 @@ var GeminiLive = class extends import_server3.Realtime {
521
649
  this.socket.on("Failed", () => this.emit("Failed"));
522
650
  this.socket.connect();
523
651
  }
652
+ /**
653
+ * Lists the Live models, which hear and speak.
654
+ *
655
+ * The list is fetched once and shared by every call of the process for a
656
+ * day. `refresh` fetches it again.
657
+ */
658
+ static listModels(options) {
659
+ return listGeminiModels("live", options);
660
+ }
524
661
  /**
525
662
  * Stops the answer. The tools it is waiting for still send their results,
526
663
  * silently, since a blocking call left unanswered would stall the model.
@@ -563,8 +700,10 @@ var GeminiLive = class extends import_server3.Realtime {
563
700
  `Say exactly the following text, word for word, and nothing else: ${text}`
564
701
  );
565
702
  }
703
+ get sampleRate() {
704
+ return GEMINI_OUTPUT_SAMPLE_RATE;
705
+ }
566
706
  cancelAnswer() {
567
- this.outputResampler.reset();
568
707
  }
569
708
  get isIdle() {
570
709
  return super.isIdle && this.toolRound.running === 0;
@@ -606,7 +745,7 @@ var GeminiLive = class extends import_server3.Realtime {
606
745
  }));
607
746
  return {
608
747
  setup: {
609
- model: `models/${this.options.model || DEFAULT_MODEL2}`,
748
+ model: `models/${this.options.model || DEFAULT_MODEL3}`,
610
749
  generationConfig: {
611
750
  responseModalities: ["AUDIO"],
612
751
  ...this.options.thinkingLevel ? { thinkingConfig: { thinkingLevel: this.options.thinkingLevel } } : {},
@@ -662,7 +801,7 @@ var GeminiLive = class extends import_server3.Realtime {
662
801
  const data = part.inlineData?.data;
663
802
  if (!data) continue;
664
803
  this.onModelOutput();
665
- const output = this.outputResampler.process(Buffer.from(data, "base64"));
804
+ const output = Buffer.from(data, "base64");
666
805
  if (output.length > 0) this.emitAudio(output);
667
806
  }
668
807
  if (content.outputTranscription?.text) {
@@ -671,14 +810,12 @@ var GeminiLive = class extends import_server3.Realtime {
671
810
  }
672
811
  if (content.interrupted) {
673
812
  this.log("Interrupted");
674
- this.outputResampler.reset();
675
813
  }
676
814
  if (content.generationComplete) {
677
815
  this.commitTurnTranscript();
678
816
  }
679
817
  if (content.turnComplete) {
680
818
  this.commitTurnTranscript();
681
- this.outputResampler.reset();
682
819
  if (this.toolRound.running > 0 || this.toolRound.followUp) {
683
820
  this.toolRound.answerEndDeferred = true;
684
821
  } else {
@@ -768,11 +905,11 @@ var GeminiLive = class extends import_server3.Realtime {
768
905
  };
769
906
 
770
907
  // src/GeminiSTT.ts
771
- var import_server4 = require("@micdrop/server");
772
- var DEFAULT_MODEL3 = "gemini-3.5-transcribe-live";
908
+ var import_server6 = require("@micdrop/server");
909
+ var DEFAULT_MODEL4 = "gemini-3.5-transcribe-live";
773
910
  var SAMPLE_RATE3 = 16e3;
774
911
  var DEFAULT_TRANSCRIPTION_TIMEOUT = 4e3;
775
- var GeminiSTT = class extends import_server4.STT {
912
+ var GeminiSTT = class extends import_server6.STT {
776
913
  constructor(options) {
777
914
  super();
778
915
  this.options = options;
@@ -832,6 +969,15 @@ var GeminiSTT = class extends import_server4.STT {
832
969
  this.socket.on("Failed", this.onFailed);
833
970
  this.socket.connect();
834
971
  }
972
+ /**
973
+ * Lists the models that transcribe a Live session.
974
+ *
975
+ * The list is fetched once and shared by every call of the process for a
976
+ * day. `refresh` fetches it again.
977
+ */
978
+ static listModels(options) {
979
+ return listGeminiModels("stt", options);
980
+ }
835
981
  transcribe(audioStream) {
836
982
  let utterance;
837
983
  audioStream.on("data", (chunk) => {
@@ -852,7 +998,7 @@ var GeminiSTT = class extends import_server4.STT {
852
998
  buildSetup() {
853
999
  return {
854
1000
  setup: {
855
- model: `models/${this.options.model || DEFAULT_MODEL3}`,
1001
+ model: `models/${this.options.model || DEFAULT_MODEL4}`,
856
1002
  generationConfig: { responseModalities: ["TEXT"] },
857
1003
  inputAudioTranscription: {
858
1004
  languageCodes: this.options.language ? [this.options.language] : [],
@@ -948,18 +1094,77 @@ var GeminiSTT = class extends import_server4.STT {
948
1094
  };
949
1095
 
950
1096
  // src/GeminiTTS.ts
951
- var import_genai2 = require("@google/genai");
952
- var import_server5 = require("@micdrop/server");
953
- var DEFAULT_MODEL4 = "gemini-2.5-flash-preview-tts";
1097
+ var import_genai3 = require("@google/genai");
1098
+ var import_server7 = require("@micdrop/server");
1099
+ var DEFAULT_MODEL5 = "gemini-3.8-flash-tts";
1100
+ var API_VERSION3 = "v1beta";
954
1101
  var DEFAULT_VOICE = "Kore";
955
1102
  var GEMINI_SAMPLE_RATE = 24e3;
956
- var OUTPUT_SAMPLE_RATE = 16e3;
957
1103
  var DEFAULT_MAX_RETRY = 2;
958
- var GeminiTTS = class extends import_server5.SentenceTTS {
1104
+ var VOICES = [
1105
+ ["Zephyr", "Bright", "female"],
1106
+ ["Puck", "Upbeat", "male"],
1107
+ ["Charon", "Informative", "male"],
1108
+ ["Kore", "Firm", "female"],
1109
+ ["Fenrir", "Excitable", "male"],
1110
+ ["Leda", "Youthful", "female"],
1111
+ ["Orus", "Firm", "male"],
1112
+ ["Aoede", "Breezy", "female"],
1113
+ ["Callirrhoe", "Easy-going", "female"],
1114
+ ["Laomedeia", "Upbeat", "female"],
1115
+ ["Autonoe", "Bright", "female"],
1116
+ ["Enceladus", "Breathy", "male"],
1117
+ ["Iapetus", "Clear", "male"],
1118
+ ["Umbriel", "Easy-going", "male"],
1119
+ ["Algieba", "Smooth", "male"],
1120
+ ["Despina", "Smooth", "female"],
1121
+ ["Erinome", "Clear", "female"],
1122
+ ["Algenib", "Gravelly", "male"],
1123
+ ["Rasalgethi", "Informative", "male"],
1124
+ ["Achernar", "Soft", "female"],
1125
+ ["Alnilam", "Firm", "male"],
1126
+ ["Schedar", "Even", "male"],
1127
+ ["Gacrux", "Mature", "female"],
1128
+ ["Pulcherrima", "Forward", "female"],
1129
+ ["Achird", "Friendly", "male"],
1130
+ ["Zubenelgenubi", "Casual", "male"],
1131
+ ["Vindemiatrix", "Gentle", "female"],
1132
+ ["Sadachbia", "Lively", "male"],
1133
+ ["Sadaltager", "Knowledgeable", "male"],
1134
+ ["Sulafat", "Warm", "female"]
1135
+ ];
1136
+ var GeminiTTS = class extends import_server7.SentenceTTS {
959
1137
  constructor(options) {
960
1138
  super();
961
1139
  this.options = options;
962
- this.genai = "genai" in options ? options.genai : new import_genai2.GoogleGenAI({ apiKey: options.apiKey });
1140
+ // Read from the audio, which states its rate
1141
+ this.rate = GEMINI_SAMPLE_RATE;
1142
+ this.genai = "genai" in options ? options.genai : new import_genai3.GoogleGenAI({ apiKey: options.apiKey });
1143
+ }
1144
+ /**
1145
+ * Lists the models that speak.
1146
+ *
1147
+ * The list is fetched once and shared by every call of the process for a
1148
+ * day. `refresh` fetches it again.
1149
+ */
1150
+ static listModels(options) {
1151
+ return listGeminiModels("tts", options);
1152
+ }
1153
+ /**
1154
+ * Lists the prebuilt voices, which speak every language the model does, so
1155
+ * `language` keeps them all. Gemini has no listing, so the list is written
1156
+ * here.
1157
+ */
1158
+ static async listVoices(options = {}) {
1159
+ return VOICES.map(([name, tone, gender]) => ({
1160
+ id: name,
1161
+ name,
1162
+ gender,
1163
+ description: tone
1164
+ }));
1165
+ }
1166
+ get sampleRate() {
1167
+ return this.rate;
963
1168
  }
964
1169
  async synthesize(text, signal) {
965
1170
  const input = this.options.instructions ? `${this.options.instructions}: ${text}` : text;
@@ -981,18 +1186,18 @@ var GeminiTTS = class extends import_server5.SentenceTTS {
981
1186
  async synthesizeOnce(input, signal, progress) {
982
1187
  const stream = await this.genai.interactions.create(
983
1188
  {
984
- model: this.options.model || DEFAULT_MODEL4,
1189
+ model: this.options.model || DEFAULT_MODEL5,
985
1190
  store: false,
986
1191
  stream: true,
987
1192
  input,
988
1193
  response_format: { type: "audio" },
989
1194
  generation_config: {
990
1195
  speech_config: [{ voice: this.options.voice || DEFAULT_VOICE }]
991
- }
1196
+ },
1197
+ api_version: API_VERSION3
992
1198
  },
993
1199
  { fetchOptions: { signal } }
994
1200
  );
995
- let resampler;
996
1201
  for await (const event of stream) {
997
1202
  if (signal.aborted) break;
998
1203
  if (event.event_type === "error") {
@@ -1006,11 +1211,8 @@ var GeminiTTS = class extends import_server5.SentenceTTS {
1006
1211
  }
1007
1212
  const { data, sample_rate } = event.delta;
1008
1213
  if (!data) continue;
1009
- resampler ?? (resampler = new import_server5.Pcm16Resampler(
1010
- sample_rate || GEMINI_SAMPLE_RATE,
1011
- OUTPUT_SAMPLE_RATE
1012
- ));
1013
- const audio = resampler.process(Buffer.from(data, "base64"));
1214
+ this.rate = sample_rate || GEMINI_SAMPLE_RATE;
1215
+ const audio = Buffer.from(data, "base64");
1014
1216
  if (audio.length > 0) progress.emitted = true;
1015
1217
  if (!this.emitAudio(audio)) break;
1016
1218
  }
@@ -1022,8 +1224,12 @@ function isBlocked(error) {
1022
1224
  // Annotate the CommonJS export names for ESM import in node:
1023
1225
  0 && (module.exports = {
1024
1226
  GeminiAgent,
1227
+ GeminiClassifier,
1025
1228
  GeminiLive,
1026
1229
  GeminiSTT,
1027
- GeminiTTS
1230
+ GeminiTTS,
1231
+ choice,
1232
+ noul,
1233
+ score
1028
1234
  });
1029
1235
  //# sourceMappingURL=index.js.map