@oh-my-pi/pi-ai 3.20.1 → 3.34.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.
@@ -19,7 +19,14 @@ import type {
19
19
  } from "../types";
20
20
  import { AssistantMessageEventStream } from "../utils/event-stream";
21
21
  import { sanitizeSurrogates } from "../utils/sanitize-unicode";
22
- import { convertMessages, convertTools, mapStopReasonString, mapToolChoice } from "./google-shared";
22
+ import {
23
+ convertMessages,
24
+ convertTools,
25
+ isThinkingPart,
26
+ mapStopReasonString,
27
+ mapToolChoice,
28
+ retainThoughtSignature,
29
+ } from "./google-shared";
23
30
 
24
31
  /**
25
32
  * Thinking level for Gemini 3 models.
@@ -69,6 +76,87 @@ const ANTIGRAVITY_HEADERS = {
69
76
  }),
70
77
  };
71
78
 
79
+ // Antigravity system instruction (ported from CLIProxyAPI v6.6.89).
80
+ const ANTIGRAVITY_SYSTEM_INSTRUCTION = `<identity>
81
+ You are Antigravity, a powerful agentic AI coding assistant designed by the Google DeepMind team working on Advanced Agentic Coding.
82
+ You are pair programming with a USER to solve their coding task. The task may require creating a new codebase, modifying or debugging an existing codebase, or simply answering a question.
83
+ The USER will send you requests, which you must always prioritize addressing. Along with each USER request, we will attach additional metadata about their current state, such as what files they have open and where their cursor is.
84
+ This information may or may not be relevant to the coding task, it is up for you to decide.
85
+ </identity>
86
+
87
+ <tool_calling>
88
+ Call tools as you normally would. The following list provides additional guidance to help you avoid errors:
89
+ - **Absolute paths only**. When using tools that accept file path arguments, ALWAYS use the absolute file path.
90
+ </tool_calling>
91
+
92
+ <web_application_development>
93
+ ## Technology Stack
94
+ Your web applications should be built using the following technologies:
95
+ 1. **Core**: Use HTML for structure and JavaScript for logic.
96
+ 2. **Styling (CSS)**: Use Vanilla CSS for maximum flexibility and control. Avoid using TailwindCSS unless the USER explicitly requests it; in this case, first confirm which TailwindCSS version to use.
97
+ 3. **Web App**: If the USER specifies that they want a more complex web app, use a framework like Next.js or Vite. Only do this if the USER explicitly requests a web app.
98
+ 4. **New Project Creation**: If you need to use a framework for a new app, use \`npx\` with the appropriate script, but there are some rules to follow:
99
+ - Use \`npx -y\` to automatically install the script and its dependencies
100
+ - You MUST run the command with \`--help\` flag to see all available options first
101
+ - Initialize the app in the current directory with \`./\` (example: \`npx -y create-vite-app@latest ./\`)
102
+ - You should run in non-interactive mode so that the user doesn't need to input anything
103
+ 5. **Running Locally**: When running locally, use \`npm run dev\` or equivalent dev server. Only build the production bundle if the USER explicitly requests it or you are validating the code for correctness.
104
+
105
+ # Design Aesthetics
106
+ 1. **Use Rich Aesthetics**: The USER should be wowed at first glance by the design. Use best practices in modern web design (e.g. vibrant colors, dark modes, glassmorphism, and dynamic animations) to create a stunning first impression. Failure to do this is UNACCEPTABLE.
107
+ 2. **Prioritize Visual Excellence**: Implement designs that will WOW the user and feel extremely premium:
108
+ - Avoid generic colors (plain red, blue, green). Use curated, harmonious color palettes (e.g., HSL tailored colors, sleek dark modes).
109
+ - Using modern typography (e.g., from Google Fonts like Inter, Roboto, or Outfit) instead of browser defaults.
110
+ - Use smooth gradients
111
+ - Add subtle micro-animations for enhanced user experience
112
+ 3. **Use a Dynamic Design**: An interface that feels responsive and alive encourages interaction. Achieve this with hover effects and interactive elements. Micro-animations, in particular, are highly effective for improving user engagement.
113
+ 4. **Premium Designs**: Make a design that feels premium and state of the art. Avoid creating simple minimum viable products.
114
+ 5. **Don't use placeholders**: If you need an image, use your generate_image tool to create a working demonstration.
115
+
116
+ ## Implementation Workflow
117
+ Follow this systematic approach when building web applications:
118
+ 1. **Plan and Understand**:
119
+ - Fully understand the user's requirements
120
+ - Draw inspiration from modern, beautiful, and dynamic web designs
121
+ - Outline the features needed for the initial version
122
+ 2. **Build the Foundation**:
123
+ - Start by creating/modifying \`index.css\`
124
+ - Implement the core design system with all tokens and utilities
125
+ 3. **Create Components**:
126
+ - Build necessary components using your design system
127
+ - Ensure all components use predefined styles, not ad-hoc utilities
128
+ - Keep components focused and reusable
129
+ 4. **Assemble Pages**:
130
+ - Update the main application to incorporate your design and components
131
+ - Ensure proper routing and navigation
132
+ - Implement responsive layouts
133
+ 5. **Polish and Optimize**:
134
+ - Review the overall user experience
135
+ - Ensure smooth interactions and transitions
136
+ - Optimize performance where needed
137
+
138
+ ## SEO Best Practices
139
+ Automatically implement SEO best practices on every page:
140
+ - **Title Tags**: Include proper, descriptive title tags for each page
141
+ - **Meta Descriptions**: Add compelling meta descriptions that accurately summarize page content
142
+ - **Heading Structure**: Use a single \`<h1>\` per page with proper heading hierarchy
143
+ - **Semantic HTML**: Use appropriate HTML5 semantic elements
144
+ - **Unique IDs**: Ensure all interactive elements have unique, descriptive IDs for browser testing
145
+ - **Performance**: Ensure fast page load times through optimization
146
+ CRITICAL REMINDER: AESTHETICS ARE VERY IMPORTANT. If your web app looks simple and basic then you have FAILED!
147
+ </web_application_development>
148
+ <ephemeral_message>
149
+ There will be an <EPHEMERAL_MESSAGE> appearing in the conversation at times. This is not coming from the user, but instead injected by the system as important information to pay attention to.
150
+ Do not respond to nor acknowledge those messages, but do follow them strictly.
151
+ </ephemeral_message>
152
+
153
+ <communication_style>
154
+ - **Formatting**. Format your responses in github-style markdown to make your responses easier for the USER to parse. For example, use headers to organize your responses and bolded or italicized text to highlight important keywords. Use backticks to format file, directory, function, and class names. If providing a URL to the user, format this in markdown as well, for example \`[label](example.com)\`.
155
+ - **Proactiveness**. As an agent, you are allowed to be proactive, but only in the course of completing the user's task. For example, if the user asks you to add a new component, you can edit the code, verify build and test statuses, and take any other obvious follow-up actions, such as performing additional research. However, avoid surprising the user. For example, if the user asks HOW to approach something, you should answer their question and instead of jumping into editing a file.
156
+ - **Helpfulness**. Respond like a helpful software engineer who is explaining your work to a friendly collaborator on the project. Acknowledge mistakes or any backtracking you do as a result of new information.
157
+ - **Ask for clarification**. If you are unsure about the USER's intent, always ask for clarification rather than making assumptions.
158
+ </communication_style>`;
159
+
72
160
  // Counter for generating unique tool call IDs
73
161
  let toolCallCounter = 0;
74
162
 
@@ -154,7 +242,7 @@ interface CloudCodeAssistRequest {
154
242
  model: string;
155
243
  request: {
156
244
  contents: Content[];
157
- systemInstruction?: { parts: { text: string }[] };
245
+ systemInstruction?: { role?: string; parts: { text: string }[] };
158
246
  generationConfig?: {
159
247
  maxOutputTokens?: number;
160
248
  temperature?: number;
@@ -167,6 +255,7 @@ interface CloudCodeAssistRequest {
167
255
  };
168
256
  };
169
257
  };
258
+ requestType?: string;
170
259
  userAgent?: string;
171
260
  requestId?: string;
172
261
  }
@@ -250,12 +339,12 @@ export const streamGoogleGeminiCli: StreamFunction<"google-gemini-cli"> = (
250
339
  throw new Error("Missing token or projectId in Google Cloud credentials. Use /login to re-authenticate.");
251
340
  }
252
341
 
253
- const requestBody = buildRequest(model, context, projectId, options);
254
342
  const endpoint = model.baseUrl || DEFAULT_ENDPOINT;
255
343
  const url = `${endpoint}/v1internal:streamGenerateContent?alt=sse`;
256
344
 
257
345
  // Use Antigravity headers for sandbox endpoint, otherwise Gemini CLI headers
258
346
  const isAntigravity = endpoint.includes("sandbox.googleapis.com");
347
+ const requestBody = buildRequest(model, context, projectId, options, isAntigravity);
259
348
  const headers = isAntigravity ? ANTIGRAVITY_HEADERS : GEMINI_CLI_HEADERS;
260
349
 
261
350
  // Fetch with retry logic for rate limits and transient errors
@@ -298,8 +387,11 @@ export const streamGoogleGeminiCli: StreamFunction<"google-gemini-cli"> = (
298
387
  // Not retryable or max retries exceeded
299
388
  throw new Error(`Cloud Code Assist API error (${response.status}): ${errorText}`);
300
389
  } catch (error) {
301
- if (error instanceof Error && error.message === "Request was aborted") {
302
- throw error;
390
+ // Check for abort - fetch throws AbortError, our code throws "Request was aborted"
391
+ if (error instanceof Error) {
392
+ if (error.name === "AbortError" || error.message === "Request was aborted") {
393
+ throw new Error("Request was aborted");
394
+ }
303
395
  }
304
396
  lastError = error instanceof Error ? error : new Error(String(error));
305
397
  // Network errors are retryable
@@ -331,46 +423,109 @@ export const streamGoogleGeminiCli: StreamFunction<"google-gemini-cli"> = (
331
423
  const decoder = new TextDecoder();
332
424
  let buffer = "";
333
425
 
334
- while (true) {
335
- const { done, value } = await reader.read();
336
- if (done) break;
426
+ // Set up abort handler to cancel reader when signal fires
427
+ const abortHandler = () => {
428
+ void reader.cancel().catch(() => {});
429
+ };
430
+ options?.signal?.addEventListener("abort", abortHandler);
431
+
432
+ try {
433
+ while (true) {
434
+ // Check abort signal before each read
435
+ if (options?.signal?.aborted) {
436
+ throw new Error("Request was aborted");
437
+ }
337
438
 
338
- buffer += decoder.decode(value, { stream: true });
339
- const lines = buffer.split("\n");
340
- buffer = lines.pop() || "";
439
+ const { done, value } = await reader.read();
440
+ if (done) break;
341
441
 
342
- for (const line of lines) {
343
- if (!line.startsWith("data:")) continue;
442
+ buffer += decoder.decode(value, { stream: true });
443
+ const lines = buffer.split("\n");
444
+ buffer = lines.pop() || "";
344
445
 
345
- const jsonStr = line.slice(5).trim();
346
- if (!jsonStr) continue;
446
+ for (const line of lines) {
447
+ if (!line.startsWith("data:")) continue;
347
448
 
348
- let chunk: CloudCodeAssistResponseChunk;
349
- try {
350
- chunk = JSON.parse(jsonStr);
351
- } catch {
352
- continue;
353
- }
449
+ const jsonStr = line.slice(5).trim();
450
+ if (!jsonStr) continue;
451
+
452
+ let chunk: CloudCodeAssistResponseChunk;
453
+ try {
454
+ chunk = JSON.parse(jsonStr);
455
+ } catch {
456
+ continue;
457
+ }
458
+
459
+ // Unwrap the response
460
+ const responseData = chunk.response;
461
+ if (!responseData) continue;
462
+
463
+ const candidate = responseData.candidates?.[0];
464
+ if (candidate?.content?.parts) {
465
+ for (const part of candidate.content.parts) {
466
+ if (part.text !== undefined) {
467
+ const isThinking = isThinkingPart(part);
468
+ if (
469
+ !currentBlock ||
470
+ (isThinking && currentBlock.type !== "thinking") ||
471
+ (!isThinking && currentBlock.type !== "text")
472
+ ) {
473
+ if (currentBlock) {
474
+ if (currentBlock.type === "text") {
475
+ stream.push({
476
+ type: "text_end",
477
+ contentIndex: blocks.length - 1,
478
+ content: currentBlock.text,
479
+ partial: output,
480
+ });
481
+ } else {
482
+ stream.push({
483
+ type: "thinking_end",
484
+ contentIndex: blockIndex(),
485
+ content: currentBlock.thinking,
486
+ partial: output,
487
+ });
488
+ }
489
+ }
490
+ if (isThinking) {
491
+ currentBlock = { type: "thinking", thinking: "", thinkingSignature: undefined };
492
+ output.content.push(currentBlock);
493
+ stream.push({ type: "thinking_start", contentIndex: blockIndex(), partial: output });
494
+ } else {
495
+ currentBlock = { type: "text", text: "" };
496
+ output.content.push(currentBlock);
497
+ stream.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
498
+ }
499
+ }
500
+ if (currentBlock.type === "thinking") {
501
+ currentBlock.thinking += part.text;
502
+ currentBlock.thinkingSignature = retainThoughtSignature(
503
+ currentBlock.thinkingSignature,
504
+ part.thoughtSignature,
505
+ );
506
+ stream.push({
507
+ type: "thinking_delta",
508
+ contentIndex: blockIndex(),
509
+ delta: part.text,
510
+ partial: output,
511
+ });
512
+ } else {
513
+ currentBlock.text += part.text;
514
+ stream.push({
515
+ type: "text_delta",
516
+ contentIndex: blockIndex(),
517
+ delta: part.text,
518
+ partial: output,
519
+ });
520
+ }
521
+ }
354
522
 
355
- // Unwrap the response
356
- const responseData = chunk.response;
357
- if (!responseData) continue;
358
-
359
- const candidate = responseData.candidates?.[0];
360
- if (candidate?.content?.parts) {
361
- for (const part of candidate.content.parts) {
362
- if (part.text !== undefined) {
363
- const isThinking = part.thought === true;
364
- if (
365
- !currentBlock ||
366
- (isThinking && currentBlock.type !== "thinking") ||
367
- (!isThinking && currentBlock.type !== "text")
368
- ) {
523
+ if (part.functionCall) {
369
524
  if (currentBlock) {
370
525
  if (currentBlock.type === "text") {
371
526
  stream.push({
372
527
  type: "text_end",
373
- contentIndex: blocks.length - 1,
528
+ contentIndex: blockIndex(),
374
529
  content: currentBlock.text,
375
530
  partial: output,
376
531
  });
@@ -382,115 +537,70 @@ export const streamGoogleGeminiCli: StreamFunction<"google-gemini-cli"> = (
382
537
  partial: output,
383
538
  });
384
539
  }
540
+ currentBlock = null;
385
541
  }
386
- if (isThinking) {
387
- currentBlock = { type: "thinking", thinking: "", thinkingSignature: undefined };
388
- output.content.push(currentBlock);
389
- stream.push({ type: "thinking_start", contentIndex: blockIndex(), partial: output });
390
- } else {
391
- currentBlock = { type: "text", text: "" };
392
- output.content.push(currentBlock);
393
- stream.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
394
- }
395
- }
396
- if (currentBlock.type === "thinking") {
397
- currentBlock.thinking += part.text;
398
- currentBlock.thinkingSignature = part.thoughtSignature;
399
- stream.push({
400
- type: "thinking_delta",
401
- contentIndex: blockIndex(),
402
- delta: part.text,
403
- partial: output,
404
- });
405
- } else {
406
- currentBlock.text += part.text;
542
+
543
+ const providedId = part.functionCall.id;
544
+ const needsNewId =
545
+ !providedId || output.content.some((b) => b.type === "toolCall" && b.id === providedId);
546
+ const toolCallId = needsNewId
547
+ ? `${part.functionCall.name}_${Date.now()}_${++toolCallCounter}`
548
+ : providedId;
549
+
550
+ const toolCall: ToolCall = {
551
+ type: "toolCall",
552
+ id: toolCallId,
553
+ name: part.functionCall.name || "",
554
+ arguments: part.functionCall.args as Record<string, unknown>,
555
+ ...(part.thoughtSignature && { thoughtSignature: part.thoughtSignature }),
556
+ };
557
+
558
+ output.content.push(toolCall);
559
+ stream.push({ type: "toolcall_start", contentIndex: blockIndex(), partial: output });
407
560
  stream.push({
408
- type: "text_delta",
561
+ type: "toolcall_delta",
409
562
  contentIndex: blockIndex(),
410
- delta: part.text,
563
+ delta: JSON.stringify(toolCall.arguments),
411
564
  partial: output,
412
565
  });
566
+ stream.push({ type: "toolcall_end", contentIndex: blockIndex(), toolCall, partial: output });
413
567
  }
414
568
  }
415
-
416
- if (part.functionCall) {
417
- if (currentBlock) {
418
- if (currentBlock.type === "text") {
419
- stream.push({
420
- type: "text_end",
421
- contentIndex: blockIndex(),
422
- content: currentBlock.text,
423
- partial: output,
424
- });
425
- } else {
426
- stream.push({
427
- type: "thinking_end",
428
- contentIndex: blockIndex(),
429
- content: currentBlock.thinking,
430
- partial: output,
431
- });
432
- }
433
- currentBlock = null;
434
- }
435
-
436
- const providedId = part.functionCall.id;
437
- const needsNewId =
438
- !providedId || output.content.some((b) => b.type === "toolCall" && b.id === providedId);
439
- const toolCallId = needsNewId
440
- ? `${part.functionCall.name}_${Date.now()}_${++toolCallCounter}`
441
- : providedId;
442
-
443
- const toolCall: ToolCall = {
444
- type: "toolCall",
445
- id: toolCallId,
446
- name: part.functionCall.name || "",
447
- arguments: part.functionCall.args as Record<string, unknown>,
448
- ...(part.thoughtSignature && { thoughtSignature: part.thoughtSignature }),
449
- };
450
-
451
- output.content.push(toolCall);
452
- stream.push({ type: "toolcall_start", contentIndex: blockIndex(), partial: output });
453
- stream.push({
454
- type: "toolcall_delta",
455
- contentIndex: blockIndex(),
456
- delta: JSON.stringify(toolCall.arguments),
457
- partial: output,
458
- });
459
- stream.push({ type: "toolcall_end", contentIndex: blockIndex(), toolCall, partial: output });
460
- }
461
569
  }
462
- }
463
570
 
464
- if (candidate?.finishReason) {
465
- output.stopReason = mapStopReasonString(candidate.finishReason);
466
- if (output.content.some((b) => b.type === "toolCall")) {
467
- output.stopReason = "toolUse";
571
+ if (candidate?.finishReason) {
572
+ output.stopReason = mapStopReasonString(candidate.finishReason);
573
+ if (output.content.some((b) => b.type === "toolCall")) {
574
+ output.stopReason = "toolUse";
575
+ }
468
576
  }
469
- }
470
577
 
471
- if (responseData.usageMetadata) {
472
- // promptTokenCount includes cachedContentTokenCount, so subtract to get fresh input
473
- const promptTokens = responseData.usageMetadata.promptTokenCount || 0;
474
- const cacheReadTokens = responseData.usageMetadata.cachedContentTokenCount || 0;
475
- output.usage = {
476
- input: promptTokens - cacheReadTokens,
477
- output:
478
- (responseData.usageMetadata.candidatesTokenCount || 0) +
479
- (responseData.usageMetadata.thoughtsTokenCount || 0),
480
- cacheRead: cacheReadTokens,
481
- cacheWrite: 0,
482
- totalTokens: responseData.usageMetadata.totalTokenCount || 0,
483
- cost: {
484
- input: 0,
485
- output: 0,
486
- cacheRead: 0,
578
+ if (responseData.usageMetadata) {
579
+ // promptTokenCount includes cachedContentTokenCount, so subtract to get fresh input
580
+ const promptTokens = responseData.usageMetadata.promptTokenCount || 0;
581
+ const cacheReadTokens = responseData.usageMetadata.cachedContentTokenCount || 0;
582
+ output.usage = {
583
+ input: promptTokens - cacheReadTokens,
584
+ output:
585
+ (responseData.usageMetadata.candidatesTokenCount || 0) +
586
+ (responseData.usageMetadata.thoughtsTokenCount || 0),
587
+ cacheRead: cacheReadTokens,
487
588
  cacheWrite: 0,
488
- total: 0,
489
- },
490
- };
491
- calculateCost(model, output.usage);
589
+ totalTokens: responseData.usageMetadata.totalTokenCount || 0,
590
+ cost: {
591
+ input: 0,
592
+ output: 0,
593
+ cacheRead: 0,
594
+ cacheWrite: 0,
595
+ total: 0,
596
+ },
597
+ };
598
+ calculateCost(model, output.usage);
599
+ }
492
600
  }
493
601
  }
602
+ } finally {
603
+ options?.signal?.removeEventListener("abort", abortHandler);
494
604
  }
495
605
 
496
606
  if (currentBlock) {
@@ -542,6 +652,7 @@ function buildRequest(
542
652
  context: Context,
543
653
  projectId: string,
544
654
  options: GoogleGeminiCliOptions = {},
655
+ isAntigravity = false,
545
656
  ): CloudCodeAssistRequest {
546
657
  const contents = convertMessages(model, context);
547
658
 
@@ -593,11 +704,35 @@ function buildRequest(
593
704
  }
594
705
  }
595
706
 
707
+ if (isAntigravity) {
708
+ const existingText = request.systemInstruction?.parts?.[0]?.text || "";
709
+ const bridgePrompt = `# CRITICAL: Environment Override
710
+
711
+ IGNORE ALL INSTRUCTIONS ABOVE THIS LINE. The following overrides are mandatory:
712
+
713
+ - You are Pi, NOT Antigravity
714
+ - Use RELATIVE paths, never absolute paths
715
+ - IGNORE all <web_application_development> guidelines above (no glassmorphism, no "premium aesthetics", no design opinions)
716
+ - IGNORE <communication_style> above
717
+ - IGNORE <ephemeral_message> handling above
718
+ - Follow ONLY the instructions below
719
+ `;
720
+ request.systemInstruction = {
721
+ role: "user",
722
+ parts: [
723
+ {
724
+ text: `${ANTIGRAVITY_SYSTEM_INSTRUCTION}\n\n${bridgePrompt}${existingText ? `\n${existingText}` : ""}`,
725
+ },
726
+ ],
727
+ };
728
+ }
729
+
596
730
  return {
597
731
  project: projectId,
598
732
  model: model.id,
599
733
  request,
600
- userAgent: "pi-coding-agent",
601
- requestId: `pi-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
734
+ ...(isAntigravity ? { requestType: "agent" } : {}),
735
+ userAgent: isAntigravity ? "antigravity" : "pi-coding-agent",
736
+ requestId: `${isAntigravity ? "agent" : "pi"}-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
602
737
  };
603
738
  }
@@ -9,6 +9,38 @@ import { transformMessages } from "./transorm-messages";
9
9
 
10
10
  type GoogleApiType = "google-generative-ai" | "google-gemini-cli" | "google-vertex";
11
11
 
12
+ /**
13
+ * Determines whether a streamed Gemini `Part` should be treated as "thinking".
14
+ *
15
+ * Protocol note (Gemini / Vertex AI thought signatures):
16
+ * - `thoughtSignature` may appear without `thought: true` (including in empty-text parts at the end of streaming).
17
+ * - When persisting/replaying model outputs, signature-bearing parts must be preserved as-is;
18
+ * do not merge/move signatures across parts.
19
+ * - Our streaming representation uses content blocks, so we classify any non-empty `thoughtSignature`
20
+ * as thinking to avoid leaking thought content into normal assistant text.
21
+ *
22
+ * Some Google backends send thought content with `thoughtSignature` but omit `thought: true`
23
+ * on subsequent deltas. We treat any non-empty `thoughtSignature` as thinking to avoid
24
+ * leaking thought text into the normal assistant text stream.
25
+ */
26
+ export function isThinkingPart(part: Pick<Part, "thought" | "thoughtSignature">): boolean {
27
+ return part.thought === true || (typeof part.thoughtSignature === "string" && part.thoughtSignature.length > 0);
28
+ }
29
+
30
+ /**
31
+ * Retain thought signatures during streaming.
32
+ *
33
+ * Some backends only send `thoughtSignature` on the first delta for a given part/block; later deltas may omit it.
34
+ * This helper preserves the last non-empty signature for the current block.
35
+ *
36
+ * Note: this does NOT merge or move signatures across distinct response parts. It only prevents
37
+ * a signature from being overwritten with `undefined` within the same streamed block.
38
+ */
39
+ export function retainThoughtSignature(existing: string | undefined, incoming: string | undefined): string | undefined {
40
+ if (typeof incoming === "string" && incoming.length > 0) return incoming;
41
+ return existing;
42
+ }
43
+
12
44
  /**
13
45
  * Convert internal messages to Gemini Content[] format.
14
46
  */
@@ -45,6 +77,8 @@ export function convertMessages<T extends GoogleApiType>(model: Model<T>, contex
45
77
  }
46
78
  } else if (msg.role === "assistant") {
47
79
  const parts: Part[] = [];
80
+ // Check if message is from same provider and model - only then keep thinking blocks
81
+ const isSameProviderAndModel = msg.provider === model.provider && msg.model === model.id;
48
82
 
49
83
  for (const block of msg.content) {
50
84
  if (block.type === "text") {
@@ -52,17 +86,19 @@ export function convertMessages<T extends GoogleApiType>(model: Model<T>, contex
52
86
  if (!block.text || block.text.trim() === "") continue;
53
87
  parts.push({ text: sanitizeSurrogates(block.text) });
54
88
  } else if (block.type === "thinking") {
55
- // Thinking blocks require signatures for Claude via Antigravity.
56
- // If signature is missing (e.g. from GPT-OSS), convert to regular text with delimiters.
57
- if (block.thinkingSignature) {
89
+ // Skip empty thinking blocks
90
+ if (!block.thinking || block.thinking.trim() === "") continue;
91
+ // Only keep as thinking block if same provider AND same model
92
+ // Otherwise convert to plain text (no tags to avoid model mimicking them)
93
+ if (isSameProviderAndModel) {
58
94
  parts.push({
59
95
  thought: true,
60
96
  text: sanitizeSurrogates(block.thinking),
61
- thoughtSignature: block.thinkingSignature,
97
+ ...(block.thinkingSignature && { thoughtSignature: block.thinkingSignature }),
62
98
  });
63
99
  } else {
64
100
  parts.push({
65
- text: `<thinking>\n${sanitizeSurrogates(block.thinking)}\n</thinking>`,
101
+ text: sanitizeSurrogates(block.thinking),
66
102
  });
67
103
  }
68
104
  } else if (block.type === "toolCall") {
@@ -73,6 +109,9 @@ export function convertMessages<T extends GoogleApiType>(model: Model<T>, contex
73
109
  args: block.arguments,
74
110
  },
75
111
  };
112
+ if (model.provider === "google-vertex" && part?.functionCall?.id) {
113
+ delete part.functionCall.id; // Vertex AI does not support 'id' in functionCall
114
+ }
76
115
  if (block.thoughtSignature) {
77
116
  part.thoughtSignature = block.thoughtSignature;
78
117
  }
@@ -121,6 +160,10 @@ export function convertMessages<T extends GoogleApiType>(model: Model<T>, contex
121
160
  },
122
161
  };
123
162
 
163
+ if (model.provider === "google-vertex" && functionResponsePart.functionResponse?.id) {
164
+ delete functionResponsePart.functionResponse.id; // Vertex AI does not support 'id' in functionResponse
165
+ }
166
+
124
167
  // Cloud Code Assist API requires all function responses to be in a single user turn.
125
168
  // Check if the last content is already a user turn with function responses and merge.
126
169
  const lastContent = contents[contents.length - 1];
@@ -20,7 +20,14 @@ import type {
20
20
  import { AssistantMessageEventStream } from "../utils/event-stream";
21
21
  import { sanitizeSurrogates } from "../utils/sanitize-unicode";
22
22
  import type { GoogleThinkingLevel } from "./google-gemini-cli";
23
- import { convertMessages, convertTools, mapStopReason, mapToolChoice } from "./google-shared";
23
+ import {
24
+ convertMessages,
25
+ convertTools,
26
+ isThinkingPart,
27
+ mapStopReason,
28
+ mapToolChoice,
29
+ retainThoughtSignature,
30
+ } from "./google-shared";
24
31
 
25
32
  export interface GoogleVertexOptions extends StreamOptions {
26
33
  toolChoice?: "auto" | "none" | "any";
@@ -88,7 +95,7 @@ export const streamGoogleVertex: StreamFunction<"google-vertex"> = (
88
95
  if (candidate?.content?.parts) {
89
96
  for (const part of candidate.content.parts) {
90
97
  if (part.text !== undefined) {
91
- const isThinking = part.thought === true;
98
+ const isThinking = isThinkingPart(part);
92
99
  if (
93
100
  !currentBlock ||
94
101
  (isThinking && currentBlock.type !== "thinking") ||
@@ -123,7 +130,10 @@ export const streamGoogleVertex: StreamFunction<"google-vertex"> = (
123
130
  }
124
131
  if (currentBlock.type === "thinking") {
125
132
  currentBlock.thinking += part.text;
126
- currentBlock.thinkingSignature = part.thoughtSignature;
133
+ currentBlock.thinkingSignature = retainThoughtSignature(
134
+ currentBlock.thinkingSignature,
135
+ part.thoughtSignature,
136
+ );
127
137
  stream.push({
128
138
  type: "thinking_delta",
129
139
  contentIndex: blockIndex(),
@@ -20,7 +20,14 @@ import type {
20
20
  import { AssistantMessageEventStream } from "../utils/event-stream";
21
21
  import { sanitizeSurrogates } from "../utils/sanitize-unicode";
22
22
  import type { GoogleThinkingLevel } from "./google-gemini-cli";
23
- import { convertMessages, convertTools, mapStopReason, mapToolChoice } from "./google-shared";
23
+ import {
24
+ convertMessages,
25
+ convertTools,
26
+ isThinkingPart,
27
+ mapStopReason,
28
+ mapToolChoice,
29
+ retainThoughtSignature,
30
+ } from "./google-shared";
24
31
 
25
32
  export interface GoogleOptions extends StreamOptions {
26
33
  toolChoice?: "auto" | "none" | "any";
@@ -75,7 +82,7 @@ export const streamGoogle: StreamFunction<"google-generative-ai"> = (
75
82
  if (candidate?.content?.parts) {
76
83
  for (const part of candidate.content.parts) {
77
84
  if (part.text !== undefined) {
78
- const isThinking = part.thought === true;
85
+ const isThinking = isThinkingPart(part);
79
86
  if (
80
87
  !currentBlock ||
81
88
  (isThinking && currentBlock.type !== "thinking") ||
@@ -110,7 +117,10 @@ export const streamGoogle: StreamFunction<"google-generative-ai"> = (
110
117
  }
111
118
  if (currentBlock.type === "thinking") {
112
119
  currentBlock.thinking += part.text;
113
- currentBlock.thinkingSignature = part.thoughtSignature;
120
+ currentBlock.thinkingSignature = retainThoughtSignature(
121
+ currentBlock.thinkingSignature,
122
+ part.thoughtSignature,
123
+ );
114
124
  stream.push({
115
125
  type: "thinking_delta",
116
126
  contentIndex: blockIndex(),