forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest-helpers.test.ts
More file actions
1953 lines (1732 loc) · 61.8 KB
/
Copy pathrequest-helpers.test.ts
File metadata and controls
1953 lines (1732 loc) · 61.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, expect, it } from "vitest";
import {
isThinkingCapableModel,
extractThinkingConfig,
extractVariantThinkingConfig,
resolveThinkingConfig,
filterUnsignedThinkingBlocks,
filterMessagesThinkingBlocks,
deepFilterThinkingBlocks,
transformThinkingParts,
normalizeThinkingConfig,
parseAntigravityApiBody,
extractUsageMetadata,
extractUsageFromSsePayload,
rewriteAntigravityPreviewAccessError,
DEFAULT_THINKING_BUDGET,
findOrphanedToolUseIds,
fixClaudeToolPairing,
validateAndFixClaudeToolPairing,
injectParameterSignatures,
injectToolHardeningInstruction,
cleanJSONSchemaForAntigravity,
createSyntheticErrorResponse,
recursivelyParseJsonStrings,
} from "./request-helpers";
import { deduplicateThinkingText, createThoughtBuffer } from "./core/streaming/transformer";
describe("sanitizeThinkingPart (covered via filtering)", () => {
it("extracts wrapped text and strips SDK fields for Gemini-style thought blocks", () => {
const validSignature = "s".repeat(60);
const thinkingText = "wrapped thought";
const getCachedSignatureFn = (_sessionId: string, text: string) =>
text === thinkingText ? validSignature : undefined;
const contents = [
{
role: "model",
parts: [
{
thought: true,
text: {
text: thinkingText,
cache_control: { type: "ephemeral" },
providerOptions: { injected: true },
},
thoughtSignature: validSignature,
cache_control: { type: "ephemeral" },
providerOptions: { injected: true },
},
],
},
{ role: "model", parts: [{ text: "trailing" }] },
];
const result = filterUnsignedThinkingBlocks(contents, "session-1", getCachedSignatureFn) as any;
expect(result[0].parts).toHaveLength(1);
expect(result[0].parts[0]).toEqual({
thought: true,
text: thinkingText,
thoughtSignature: validSignature,
});
expect(result[0].parts[0].cache_control).toBeUndefined();
expect(result[0].parts[0].providerOptions).toBeUndefined();
});
it("extracts wrapped thinking text and strips SDK fields for Anthropic-style thinking blocks", () => {
const validSignature = "a".repeat(60);
const thinkingText = "wrapped thinking";
const getCachedSignatureFn = (_sessionId: string, text: string) =>
text === thinkingText ? validSignature : undefined;
const contents = [
{
role: "model",
parts: [
{
type: "thinking",
thinking: {
text: thinkingText,
cache_control: { type: "ephemeral" },
providerOptions: { injected: true },
},
signature: validSignature,
cache_control: { type: "ephemeral" },
providerOptions: { injected: true },
},
],
},
{ role: "model", parts: [{ text: "trailing" }] },
];
const result = filterUnsignedThinkingBlocks(contents, "session-1", getCachedSignatureFn) as any;
expect(result[0].parts).toHaveLength(1);
expect(result[0].parts[0]).toEqual({
type: "thinking",
thinking: thinkingText,
signature: validSignature,
});
});
it("preserves signatures while dropping cache_control/providerOptions during signature restoration", () => {
const cachedSignature = "c".repeat(60);
const getCachedSignatureFn = (_sessionId: string, _text: string) => cachedSignature;
const messages = [
{
role: "assistant",
content: [
{
type: "thinking",
thinking: {
thinking: "restore me",
cache_control: { type: "ephemeral" },
},
providerOptions: { injected: true },
},
{ type: "text", text: "visible" },
],
},
{ role: "user", content: [{ type: "text", text: "next" }] },
{ role: "assistant", content: [{ type: "text", text: "last" }] },
];
const result = filterMessagesThinkingBlocks(messages, "session-1", getCachedSignatureFn) as any;
expect(result[0].content[0]).toEqual({
type: "thinking",
thinking: "restore me",
signature: cachedSignature,
});
});
it("sanitizes reasoning blocks keeping only allowed fields (type, text, signature)", () => {
const validSignature = "z".repeat(60);
const getCachedSignatureFn = (_sessionId: string, _text: string) => validSignature;
const contents = [
{
role: "model",
parts: [
{
type: "reasoning",
text: "reasoning text",
signature: validSignature,
cache_control: { type: "ephemeral" },
providerOptions: { injected: true },
meta: { keep: true },
},
{ type: "text", text: "visible" },
],
},
{ role: "user", parts: [{ text: "next" }] },
{ role: "model", parts: [{ text: "last" }] },
];
const result = filterUnsignedThinkingBlocks(contents, "session-1", getCachedSignatureFn) as any;
expect(result[0].parts[0]).toEqual({
type: "reasoning",
text: "reasoning text",
signature: validSignature,
});
});
});
describe("isThinkingCapableModel", () => {
it("returns true for models with 'thinking' in name", () => {
expect(isThinkingCapableModel("claude-thinking")).toBe(true);
expect(isThinkingCapableModel("CLAUDE-THINKING-4")).toBe(true);
expect(isThinkingCapableModel("model-thinking-v1")).toBe(true);
});
it("returns true for models with 'gemini-3' in name", () => {
expect(isThinkingCapableModel("gemini-3-pro")).toBe(true);
expect(isThinkingCapableModel("GEMINI-3-flash")).toBe(true);
expect(isThinkingCapableModel("gemini-3")).toBe(true);
});
it("returns true for models with 'opus' in name", () => {
expect(isThinkingCapableModel("claude-opus")).toBe(true);
expect(isThinkingCapableModel("claude-4-opus")).toBe(true);
expect(isThinkingCapableModel("OPUS")).toBe(true);
});
it("returns false for non-thinking models", () => {
expect(isThinkingCapableModel("claude-sonnet")).toBe(false);
expect(isThinkingCapableModel("gemini-2-pro")).toBe(false);
expect(isThinkingCapableModel("gpt-4")).toBe(false);
});
});
describe("extractThinkingConfig", () => {
it("extracts thinkingConfig from generationConfig", () => {
const result = extractThinkingConfig(
{},
{ thinkingConfig: { includeThoughts: true, thinkingBudget: 8000 } },
undefined,
);
expect(result).toEqual({ includeThoughts: true, thinkingBudget: 8000 });
});
it("extracts thinkingConfig from extra_body", () => {
const result = extractThinkingConfig(
{},
undefined,
{ thinkingConfig: { includeThoughts: true, thinkingBudget: 4000 } },
);
expect(result).toEqual({ includeThoughts: true, thinkingBudget: 4000 });
});
it("extracts thinkingConfig from requestPayload directly", () => {
const result = extractThinkingConfig(
{ thinkingConfig: { includeThoughts: false, thinkingBudget: 2000 } },
undefined,
undefined,
);
expect(result).toEqual({ includeThoughts: false, thinkingBudget: 2000 });
});
it("prioritizes generationConfig over extra_body", () => {
const result = extractThinkingConfig(
{},
{ thinkingConfig: { includeThoughts: true, thinkingBudget: 8000 } },
{ thinkingConfig: { includeThoughts: false, thinkingBudget: 4000 } },
);
expect(result).toEqual({ includeThoughts: true, thinkingBudget: 8000 });
});
it("converts Anthropic-style thinking config", () => {
const result = extractThinkingConfig(
{ thinking: { type: "enabled", budgetTokens: 10000 } },
undefined,
undefined,
);
expect(result).toEqual({ includeThoughts: true, thinkingBudget: 10000 });
});
it("uses default budget for Anthropic-style without budgetTokens", () => {
const result = extractThinkingConfig(
{ thinking: { type: "enabled" } },
undefined,
undefined,
);
expect(result).toEqual({ includeThoughts: true, thinkingBudget: DEFAULT_THINKING_BUDGET });
});
it("returns undefined when no config found", () => {
expect(extractThinkingConfig({}, undefined, undefined)).toBeUndefined();
});
it("uses default budget when thinkingBudget not specified", () => {
const result = extractThinkingConfig(
{},
{ thinkingConfig: { includeThoughts: true } },
undefined,
);
expect(result).toEqual({ includeThoughts: true, thinkingBudget: DEFAULT_THINKING_BUDGET });
});
});
describe("resolveThinkingConfig", () => {
it("keeps thinking enabled for Claude models with assistant history", () => {
const result = resolveThinkingConfig(
{ includeThoughts: true, thinkingBudget: 8000 },
true, // isThinkingModel
true, // isClaudeModel
true, // hasAssistantHistory
);
expect(result).toEqual({ includeThoughts: true, thinkingBudget: 8000 });
});
it("enables thinking for thinking-capable models without user config", () => {
const result = resolveThinkingConfig(
undefined,
true, // isThinkingModel
false, // isClaudeModel
false, // hasAssistantHistory
);
expect(result).toEqual({ includeThoughts: true, thinkingBudget: DEFAULT_THINKING_BUDGET });
});
it("respects user config for non-Claude models", () => {
const userConfig = { includeThoughts: false, thinkingBudget: 5000 };
const result = resolveThinkingConfig(
userConfig,
true,
false,
false,
);
expect(result).toEqual(userConfig);
});
it("returns user config for Claude without history", () => {
const userConfig = { includeThoughts: true, thinkingBudget: 8000 };
const result = resolveThinkingConfig(
userConfig,
true,
true, // isClaudeModel
false, // no history
);
expect(result).toEqual(userConfig);
});
it("returns undefined for non-thinking model without user config", () => {
const result = resolveThinkingConfig(
undefined,
false, // not thinking model
false,
false,
);
expect(result).toBeUndefined();
});
});
describe("filterUnsignedThinkingBlocks", () => {
it("filters out unsigned thinking parts", () => {
const contents = [
{
role: "model",
parts: [
{ type: "thinking", text: "thinking without signature" },
{ type: "text", text: "visible text" },
],
},
{ role: "user", parts: [{ text: "next" }] },
{ role: "model", parts: [{ text: "last" }] },
];
const result = filterUnsignedThinkingBlocks(contents);
expect(result[0].parts).toHaveLength(1);
expect(result[0].parts[0].type).toBe("text");
});
it("keeps signed thinking parts with valid signatures from our cache", () => {
const validSignature = "a".repeat(60);
const thinkingText = "thinking with signature";
const getCachedSignatureFn = (_sessionId: string, text: string) =>
text === thinkingText ? validSignature : undefined;
const contents = [
{
role: "model",
parts: [
{ type: "thinking", text: thinkingText, signature: validSignature },
{ type: "text", text: "visible text" },
],
},
];
const result = filterUnsignedThinkingBlocks(contents, "session-1", getCachedSignatureFn);
expect(result[0].parts).toHaveLength(2);
expect(result[0].parts[0].signature).toBe(validSignature);
});
it("strips thinking parts with foreign signatures not in our cache", () => {
const foreignSignature = "f".repeat(60);
const contents = [
{
role: "model",
parts: [
{ type: "thinking", text: "foreign thinking", signature: foreignSignature },
{ type: "text", text: "visible text" },
],
},
{ role: "user", parts: [{ text: "next" }] },
{ role: "model", parts: [{ text: "last" }] },
];
const result = filterUnsignedThinkingBlocks(contents);
expect(result[0].parts).toHaveLength(1);
expect(result[0].parts[0].type).toBe("text");
});
it("filters thinking parts with short signatures", () => {
const contents = [
{
role: "model",
parts: [
{ type: "thinking", text: "thinking with short signature", signature: "sig123" },
{ type: "text", text: "visible text" },
],
},
{ role: "user", parts: [{ text: "next" }] },
{ role: "model", parts: [{ text: "last" }] },
];
const result = filterUnsignedThinkingBlocks(contents);
expect(result[0].parts).toHaveLength(1);
expect(result[0].parts[0].type).toBe("text");
});
it("handles Gemini-style thought parts with valid signatures from our cache", () => {
const validSignature = "b".repeat(55);
const thinkingText = "has signature";
const getCachedSignatureFn = (_sessionId: string, text: string) =>
text === thinkingText ? validSignature : undefined;
const contents = [
{
role: "model",
parts: [
{ thought: true, text: "no signature" },
{ thought: true, text: thinkingText, thoughtSignature: validSignature },
],
},
{ role: "user", parts: [{ text: "next" }] },
{ role: "model", parts: [{ text: "last" }] },
];
const result = filterUnsignedThinkingBlocks(contents, "session-1", getCachedSignatureFn);
expect(result[0].parts).toHaveLength(1);
expect(result[0].parts[0].thoughtSignature).toBe(validSignature);
});
it("filters Gemini-style thought parts with short signatures", () => {
const contents = [
{
role: "model",
parts: [
{ thought: true, text: "has short signature", thoughtSignature: "sig" },
],
},
];
const result = filterUnsignedThinkingBlocks(contents);
expect(result[0].parts).toHaveLength(0);
});
it("preserves non-thinking parts", () => {
const contents = [
{
role: "user",
parts: [{ text: "hello" }],
},
];
const result = filterUnsignedThinkingBlocks(contents);
expect(result).toEqual(contents);
});
it("strips blocks with signature field even if type is unknown", () => {
const foreignSignature = "x".repeat(60);
const contents = [
{
role: "model",
parts: [
{ type: "unknown_thinking_type", text: "foreign block", signature: foreignSignature },
{ type: "text", text: "visible" },
],
},
{ role: "user", parts: [{ text: "next" }] },
{ role: "model", parts: [{ text: "last" }] },
];
const result = filterUnsignedThinkingBlocks(contents);
expect(result[0].parts).toHaveLength(1);
expect(result[0].parts[0].type).toBe("text");
});
it("handles empty parts array", () => {
const contents = [{ role: "model", parts: [] }];
const result = filterUnsignedThinkingBlocks(contents);
expect(result[0].parts).toEqual([]);
});
it("handles missing parts", () => {
const contents = [{ role: "model" }];
const result = filterUnsignedThinkingBlocks(contents);
expect(result).toEqual(contents);
});
it("preserves tool_use and tool_result blocks intact", () => {
const contents = [
{
role: "model",
parts: [
{ type: "tool_use", id: "tool_123", name: "bash", input: { command: "ls" } },
],
},
{
role: "user",
parts: [
{ type: "tool_result", tool_use_id: "tool_123", content: "file1.txt" },
],
},
];
const result = filterUnsignedThinkingBlocks(contents);
expect(result[0].parts[0]).toEqual({ type: "tool_use", id: "tool_123", name: "bash", input: { command: "ls" } });
expect(result[1].parts[0]).toEqual({ type: "tool_result", tool_use_id: "tool_123", content: "file1.txt" });
});
it("preserves tool blocks even if they have signature-like fields", () => {
const contents = [
{
role: "user",
parts: [
{ type: "tool_result", tool_use_id: "tool_456", content: "result", signature: "some_random_value" },
],
},
];
const result = filterUnsignedThinkingBlocks(contents);
expect(result[0].parts).toHaveLength(1);
expect(result[0].parts[0].tool_use_id).toBe("tool_456");
});
it("preserves nested tool_result format", () => {
const contents = [
{
role: "user",
parts: [
{ tool_result: { tool_use_id: "tool_789", content: "nested result" } },
],
},
];
const result = filterUnsignedThinkingBlocks(contents);
expect(result[0].parts).toHaveLength(1);
expect(result[0].parts[0].tool_result.tool_use_id).toBe("tool_789");
});
it("preserves functionCall and functionResponse blocks", () => {
const contents = [
{
role: "model",
parts: [
{ functionCall: { name: "get_weather", args: { city: "NYC" } } },
],
},
{
role: "function",
parts: [
{ functionResponse: { name: "get_weather", response: { temp: 72 } } },
],
},
];
const result = filterUnsignedThinkingBlocks(contents);
expect(result[0].parts[0].functionCall).toBeDefined();
expect(result[1].parts[0].functionResponse).toBeDefined();
});
});
describe("deepFilterThinkingBlocks", () => {
it("removes nested thinking blocks in extra_body messages", () => {
const payload = {
extra_body: {
messages: [
{
role: "assistant",
content: [
{ type: "thinking", thinking: "foreign", signature: "x".repeat(60) },
{ type: "text", text: "visible" },
],
},
{ role: "assistant", content: [{ type: "text", text: "last" }] },
],
},
};
deepFilterThinkingBlocks(payload);
const filtered = (payload as any).extra_body.messages[0].content;
expect(filtered).toHaveLength(1);
expect(filtered[0].type).toBe("text");
});
});
describe("filterMessagesThinkingBlocks", () => {
it("filters out unsigned thinking blocks in messages[].content", () => {
const messages = [
{
role: "assistant",
content: [
{ type: "thinking", thinking: "no signature" },
{ type: "text", text: "visible" },
],
},
{ role: "assistant", content: [{ type: "text", text: "last" }] },
];
const result = filterMessagesThinkingBlocks(messages) as any;
expect(result[0].content).toHaveLength(1);
expect(result[0].content[0].type).toBe("text");
});
it("keeps signed thinking blocks with valid signatures from our cache and sanitizes injected fields", () => {
const validSignature = "a".repeat(60);
const thinkingText = "wrapped";
const getCachedSignatureFn = (_sessionId: string, text: string) =>
text === thinkingText ? validSignature : undefined;
const messages = [
{
role: "assistant",
content: [
{
type: "thinking",
thinking: { text: thinkingText, cache_control: { type: "ephemeral" } },
signature: validSignature,
cache_control: { type: "ephemeral" },
providerOptions: { injected: true },
},
{ type: "text", text: "visible" },
],
},
{ role: "assistant", content: [{ type: "text", text: "last" }] },
];
const result = filterMessagesThinkingBlocks(messages, "session-1", getCachedSignatureFn) as any;
expect(result[0].content[0]).toEqual({
type: "thinking",
thinking: thinkingText,
signature: validSignature,
});
});
it("strips thinking blocks with foreign signatures not in our cache", () => {
const foreignSignature = "f".repeat(60);
const messages = [
{
role: "assistant",
content: [
{
type: "thinking",
thinking: "foreign thinking",
signature: foreignSignature,
},
{ type: "text", text: "visible" },
],
},
{ role: "assistant", content: [{ type: "text", text: "last" }] },
];
const result = filterMessagesThinkingBlocks(messages) as any;
expect(result[0].content).toHaveLength(1);
expect(result[0].content[0].type).toBe("text");
});
it("filters thinking blocks with short signatures", () => {
const messages = [
{
role: "assistant",
content: [
{ type: "thinking", thinking: "short sig", signature: "sig123" },
{ type: "text", text: "visible" },
],
},
{ role: "assistant", content: [{ type: "text", text: "last" }] },
];
const result = filterMessagesThinkingBlocks(messages) as any;
expect(result[0].content).toEqual([{ type: "text", text: "visible" }]);
});
it("restores a missing signature from cache and preserves it after sanitization", () => {
const cachedSignature = "c".repeat(60);
const getCachedSignatureFn = (_sessionId: string, _text: string) => cachedSignature;
const messages = [
{
role: "assistant",
content: [
{
type: "thinking",
thinking: { thinking: "restore me", providerOptions: { injected: true } },
// no signature present (forces restore)
cache_control: { type: "ephemeral" },
},
{ type: "text", text: "visible" },
],
},
{ role: "assistant", content: [{ type: "text", text: "last" }] },
];
const result = filterMessagesThinkingBlocks(messages, "session-1", getCachedSignatureFn) as any;
expect(result[0].content[0]).toEqual({
type: "thinking",
thinking: "restore me",
signature: cachedSignature,
});
});
it("handles Gemini-style thought blocks inside messages content with cached signatures", () => {
const validSignature = "b".repeat(60);
const thinkingText = "wrapped thought";
const getCachedSignatureFn = (_sessionId: string, text: string) =>
text === thinkingText ? validSignature : undefined;
const messages = [
{
role: "assistant",
content: [
{
thought: true,
text: { text: thinkingText, cache_control: { type: "ephemeral" } },
thoughtSignature: validSignature,
providerOptions: { injected: true },
},
{ type: "text", text: "visible" },
],
},
{ role: "assistant", content: [{ type: "text", text: "last" }] },
];
const result = filterMessagesThinkingBlocks(messages, "session-1", getCachedSignatureFn) as any;
expect(result[0].content[0]).toEqual({
thought: true,
text: thinkingText,
thoughtSignature: validSignature,
});
});
it("preserves non-thinking blocks and returns message unchanged when content is missing", () => {
const messages: any[] = [
{ role: "assistant", content: [{ type: "text", text: "hello" }] },
{ role: "assistant" },
];
const result = filterMessagesThinkingBlocks(messages) as any;
expect(result[0]).toEqual(messages[0]);
expect(result[1]).toEqual(messages[1]);
});
it("handles non-object messages gracefully", () => {
const messages: any[] = [null, "string", 123, { role: "assistant", content: [] }];
const result = filterMessagesThinkingBlocks(messages) as any;
expect(result).toEqual(messages);
});
});
describe("transformThinkingParts", () => {
it("transforms Anthropic-style thinking blocks to reasoning", () => {
const response = {
content: [
{ type: "thinking", thinking: "my thoughts" },
{ type: "text", text: "visible" },
],
};
const result = transformThinkingParts(response) as any;
expect(result.content[0].type).toBe("reasoning");
expect(result.content[0].thought).toBe(true);
expect(result.reasoning_content).toBe("my thoughts");
});
it("transforms Gemini-style candidates", () => {
const response = {
candidates: [
{
content: {
parts: [
{ thought: true, text: "thinking here" },
{ text: "output" },
],
},
},
],
};
const result = transformThinkingParts(response) as any;
expect(result.candidates[0].content.parts[0].type).toBe("reasoning");
expect(result.candidates[0].reasoning_content).toBe("thinking here");
});
it("handles non-object input", () => {
expect(transformThinkingParts(null)).toBeNull();
expect(transformThinkingParts(undefined)).toBeUndefined();
expect(transformThinkingParts("string")).toBe("string");
});
it("preserves other response properties", () => {
const response = {
content: [],
id: "resp-123",
model: "claude-4",
};
const result = transformThinkingParts(response) as any;
expect(result.id).toBe("resp-123");
expect(result.model).toBe("claude-4");
});
it("converts Gemini-style thoughtSignature to providerMetadata.anthropic.signature", () => {
const response = {
candidates: [
{
content: {
parts: [
{ thought: true, text: "thinking here", thoughtSignature: "sig123abc" },
{ text: "output" },
],
},
},
],
};
const result = transformThinkingParts(response) as any;
expect(result.candidates[0].content.parts[0].providerMetadata).toEqual({
anthropic: { signature: "sig123abc" }
});
expect(result.candidates[0].content.parts[0].thoughtSignature).toBeUndefined();
});
it("converts Anthropic-style signature to providerMetadata.anthropic.signature", () => {
const response = {
candidates: [
{
content: {
parts: [
{ type: "thinking", text: "thinking here", signature: "anthro_sig_xyz" },
{ text: "output" },
],
},
},
],
};
const result = transformThinkingParts(response) as any;
expect(result.candidates[0].content.parts[0].providerMetadata).toEqual({
anthropic: { signature: "anthro_sig_xyz" }
});
expect(result.candidates[0].content.parts[0].signature).toBeUndefined();
});
it("converts signature in content array (Anthropic-style)", () => {
const response = {
content: [
{ type: "thinking", thinking: "my thoughts", signature: "content_sig" },
{ type: "text", text: "visible" },
],
};
const result = transformThinkingParts(response) as any;
expect(result.content[0].providerMetadata).toEqual({
anthropic: { signature: "content_sig" }
});
expect(result.content[0].signature).toBeUndefined();
expect(result.content[0].thoughtSignature).toBeUndefined();
});
it("prefers signature over thoughtSignature when both present", () => {
const response = {
candidates: [
{
content: {
parts: [
{ thought: true, text: "thinking", signature: "sig_primary", thoughtSignature: "sig_fallback" },
],
},
},
],
};
const result = transformThinkingParts(response) as any;
expect(result.candidates[0].content.parts[0].providerMetadata).toEqual({
anthropic: { signature: "sig_primary" }
});
});
it("does not add providerMetadata when no signature present", () => {
const response = {
candidates: [
{
content: {
parts: [
{ thought: true, text: "thinking without signature" },
{ text: "output" },
],
},
},
],
};
const result = transformThinkingParts(response) as any;
expect(result.candidates[0].content.parts[0].providerMetadata).toBeUndefined();
});
});
describe("normalizeThinkingConfig", () => {
it("returns undefined for non-object input", () => {
expect(normalizeThinkingConfig(null)).toBeUndefined();
expect(normalizeThinkingConfig(undefined)).toBeUndefined();
expect(normalizeThinkingConfig("string")).toBeUndefined();
});
it("normalizes valid config", () => {
const result = normalizeThinkingConfig({
thinkingBudget: 8000,
includeThoughts: true,
});
expect(result).toEqual({
thinkingBudget: 8000,
includeThoughts: true,
});
});
it("handles snake_case property names", () => {
const result = normalizeThinkingConfig({
thinking_budget: 4000,
include_thoughts: true,
});
expect(result).toEqual({
thinkingBudget: 4000,
includeThoughts: true,
});
});
it("disables includeThoughts when budget is 0", () => {
const result = normalizeThinkingConfig({
thinkingBudget: 0,
includeThoughts: true,
});
expect(result?.includeThoughts).toBe(false);
});
it("returns undefined when both values are absent/undefined", () => {
const result = normalizeThinkingConfig({});
expect(result).toBeUndefined();
});
it("handles non-finite budget values", () => {
const result = normalizeThinkingConfig({
thinkingBudget: Infinity,
includeThoughts: true,
});
// When budget is non-finite (undefined), includeThoughts is forced to false
expect(result).toEqual({ includeThoughts: false });
});
});
describe("parseAntigravityApiBody", () => {
it("parses valid JSON object", () => {
const result = parseAntigravityApiBody('{"response": {"text": "hello"}}');
expect(result).toEqual({ response: { text: "hello" } });
});
it("extracts first object from array", () => {
const result = parseAntigravityApiBody('[{"response": "first"}, {"response": "second"}]');
expect(result).toEqual({ response: "first" });
});
it("returns null for invalid JSON", () => {
expect(parseAntigravityApiBody("not json")).toBeNull();
});
it("returns null for empty array", () => {
expect(parseAntigravityApiBody("[]")).toBeNull();
});
it("returns null for primitive values", () => {
expect(parseAntigravityApiBody('"string"')).toBeNull();
expect(parseAntigravityApiBody("123")).toBeNull();
});
it("handles array with null values", () => {
const result = parseAntigravityApiBody('[null, {"valid": true}]');
expect(result).toEqual({ valid: true });
});
});
describe("extractUsageMetadata", () => {
it("extracts usage from response.usageMetadata", () => {
const body = {
response: {
usageMetadata: {
totalTokenCount: 1000,
promptTokenCount: 500,
candidatesTokenCount: 500,
cachedContentTokenCount: 100,
},
},
};
const result = extractUsageMetadata(body);
expect(result).toEqual({
totalTokenCount: 1000,
promptTokenCount: 500,
candidatesTokenCount: 500,
cachedContentTokenCount: 100,
});
});
it("returns null when no usageMetadata", () => {
expect(extractUsageMetadata({ response: {} })).toBeNull();
expect(extractUsageMetadata({})).toBeNull();
});
it("handles partial usage data", () => {
const body = {
response: {
usageMetadata: {
totalTokenCount: 1000,
},
},
};
const result = extractUsageMetadata(body);
expect(result).toEqual({
totalTokenCount: 1000,
promptTokenCount: undefined,
candidatesTokenCount: undefined,
cachedContentTokenCount: undefined,
});
});
it("filters non-finite numbers", () => {
const body = {
response: {
usageMetadata: {
totalTokenCount: Infinity,
promptTokenCount: NaN,
candidatesTokenCount: 100,
},
},
};
const result = extractUsageMetadata(body);
expect(result?.totalTokenCount).toBeUndefined();
expect(result?.promptTokenCount).toBeUndefined();
expect(result?.candidatesTokenCount).toBe(100);
});