@langchain/google-common 0.2.0 → 0.2.2

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.
@@ -17,6 +17,15 @@ export interface AnthropicMessageContentImage extends AnthropicMessageContentBas
17
17
  data: string;
18
18
  };
19
19
  }
20
+ export interface AnthropicMessageContentThinking extends AnthropicMessageContentBase {
21
+ type: "thinking";
22
+ thinking: string;
23
+ signature: string;
24
+ }
25
+ export interface AnthropicMessageContentRedactedThinking extends AnthropicMessageContentBase {
26
+ type: "redacted_thinking";
27
+ data: string;
28
+ }
20
29
  export type AnthropicMessageContentToolUseInput = object;
21
30
  export interface AnthropicMessageContentToolUse extends AnthropicMessageContentBase {
22
31
  type: "tool_use";
@@ -31,7 +40,7 @@ export interface AnthropicMessageContentToolResult extends AnthropicMessageConte
31
40
  is_error?: boolean;
32
41
  content: string | AnthropicMessageContentToolResultContent[];
33
42
  }
34
- export type AnthropicMessageContent = AnthropicMessageContentText | AnthropicMessageContentImage | AnthropicMessageContentToolUse | AnthropicMessageContentToolResult;
43
+ export type AnthropicMessageContent = AnthropicMessageContentText | AnthropicMessageContentImage | AnthropicMessageContentToolUse | AnthropicMessageContentToolResult | AnthropicMessageContentThinking | AnthropicMessageContentRedactedThinking;
35
44
  export interface AnthropicMessage {
36
45
  role: string;
37
46
  content: string | AnthropicMessageContent[];
@@ -61,6 +70,14 @@ export interface AnthropicTool {
61
70
  cache_control?: AnthropicCacheControl;
62
71
  input_schema: AnthropicToolInputSchema;
63
72
  }
73
+ export interface AnthropicThinkingEnabled {
74
+ type: "enabled";
75
+ budget_tokens: number;
76
+ }
77
+ export interface AnthropicThinkingDisabled {
78
+ type: "disabled";
79
+ }
80
+ export type AnthropicThinking = AnthropicThinkingEnabled | AnthropicThinkingDisabled;
64
81
  export interface AnthropicRequest {
65
82
  anthropic_version: string;
66
83
  messages: AnthropicMessage[];
@@ -74,6 +91,7 @@ export interface AnthropicRequest {
74
91
  metadata?: AnthropicMetadata;
75
92
  tool_choice?: AnthropicToolChoice;
76
93
  tools?: AnthropicTool[];
94
+ thinking?: AnthropicThinking;
77
95
  }
78
96
  export type AnthropicRequestSettings = Pick<AnthropicRequest, "max_tokens" | "temperature" | "top_k" | "top_p" | "stop_sequences" | "stream">;
79
97
  export interface AnthropicContentText {
@@ -86,7 +104,16 @@ export interface AnthropicContentToolUse {
86
104
  name: string;
87
105
  input: object;
88
106
  }
89
- export type AnthropicContent = AnthropicContentText | AnthropicContentToolUse;
107
+ export interface AnthropicContentThinking {
108
+ type: "thinking";
109
+ thinking: string;
110
+ signature: string;
111
+ }
112
+ export interface AnthropicContentRedactedThinking {
113
+ type: "redacted_thinking";
114
+ data: string;
115
+ }
116
+ export type AnthropicContent = AnthropicContentText | AnthropicContentToolUse | AnthropicContentThinking | AnthropicContentRedactedThinking;
90
117
  export interface AnthropicUsage {
91
118
  input_tokens: number;
92
119
  output_tokens: number;
@@ -106,6 +133,7 @@ export interface AnthropicResponseMessage {
106
133
  }
107
134
  export interface AnthropicAPIConfig {
108
135
  version?: string;
136
+ thinking?: AnthropicThinking;
109
137
  }
110
138
  export type AnthropicStreamEventType = "message_start" | "content_block_start" | "content_block_delta" | "content_block_stop" | "message_delta" | "message_stop" | "ping" | "error";
111
139
  export type AnthropicStreamDeltaType = "text_delta" | "input_json_delta";
@@ -70,6 +70,18 @@ function getAnthropicAPI(config) {
70
70
  tool_calls: [tool],
71
71
  };
72
72
  }
73
+ function thinkingContentToMessageFields(thinkingContent) {
74
+ // TODO: Once a reasoning/thinking type is defined in LangChain, use it
75
+ return {
76
+ content: [thinkingContent],
77
+ };
78
+ }
79
+ function redactedThinkingContentToMessageFields(thinkingContent) {
80
+ // TODO: Once a reasoning/thinking type is defined in LangChain, use it
81
+ return {
82
+ content: [thinkingContent],
83
+ };
84
+ }
73
85
  function anthropicContentToMessageFields(anthropicContent) {
74
86
  const type = anthropicContent?.type;
75
87
  switch (type) {
@@ -77,7 +89,12 @@ function getAnthropicAPI(config) {
77
89
  return textContentToMessageFields(anthropicContent);
78
90
  case "tool_use":
79
91
  return toolUseContentToMessageFields(anthropicContent);
92
+ case "thinking":
93
+ return thinkingContentToMessageFields(anthropicContent);
94
+ case "redacted_thinking":
95
+ return redactedThinkingContentToMessageFields(anthropicContent);
80
96
  default:
97
+ console.error(`Unknown message type: ${type}`, anthropicContent);
81
98
  return undefined;
82
99
  }
83
100
  }
@@ -321,6 +338,25 @@ function getAnthropicAPI(config) {
321
338
  },
322
339
  };
323
340
  }
341
+ function thinkingContentToAnthropicContent(
342
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
343
+ content) {
344
+ // TODO: Once a Langchain Thinking type is defined, use it
345
+ return {
346
+ type: "thinking",
347
+ thinking: content.thinking,
348
+ signature: content.signature,
349
+ };
350
+ }
351
+ function redactedThinkingContentToAnthropicContent(
352
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
353
+ content) {
354
+ // TODO: Once a Langchain Thinking type is defined, use it
355
+ return {
356
+ type: "redacted_thinking",
357
+ data: content.data,
358
+ };
359
+ }
324
360
  function contentComplexToAnthropicContent(content) {
325
361
  const type = content?.type;
326
362
  switch (type) {
@@ -328,8 +364,12 @@ function getAnthropicAPI(config) {
328
364
  return textContentToAnthropicContent(content);
329
365
  case "image_url":
330
366
  return imageContentToAnthropicContent(content);
367
+ case "thinking":
368
+ return thinkingContentToAnthropicContent(content);
369
+ case "redacted_thinking":
370
+ return redactedThinkingContentToAnthropicContent(content);
331
371
  default:
332
- console.warn(`Unexpected content type: ${type}`);
372
+ console.warn(`Unexpected content type: ${type}`, content);
333
373
  return undefined;
334
374
  }
335
375
  }
@@ -344,6 +384,20 @@ function getAnthropicAPI(config) {
344
384
  });
345
385
  return ret;
346
386
  }
387
+ function toolCallToAnthropicContent(toolCall) {
388
+ return {
389
+ type: "tool_use",
390
+ id: toolCall.id,
391
+ name: toolCall.name,
392
+ input: toolCall.args,
393
+ };
394
+ }
395
+ function toolCallsToAnthropicContent(toolCalls) {
396
+ if (toolCalls === undefined) {
397
+ return [];
398
+ }
399
+ return toolCalls.map(toolCallToAnthropicContent);
400
+ }
347
401
  function baseRoleToAnthropicMessage(base, role) {
348
402
  const content = contentToAnthropicContent(base.content);
349
403
  return {
@@ -351,6 +405,15 @@ function getAnthropicAPI(config) {
351
405
  content,
352
406
  };
353
407
  }
408
+ function aiMessageToAnthropicMessage(base) {
409
+ const ret = baseRoleToAnthropicMessage(base, "assistant");
410
+ const toolContent = toolCallsToAnthropicContent(base.tool_calls);
411
+ if (toolContent.length > 0) {
412
+ const content = ret.content;
413
+ ret.content = [...content, ...toolContent];
414
+ }
415
+ return ret;
416
+ }
354
417
  function toolMessageToAnthropicMessage(base) {
355
418
  const role = "user";
356
419
  const toolUseId = base.tool_call_id;
@@ -373,10 +436,11 @@ function getAnthropicAPI(config) {
373
436
  case "human":
374
437
  return baseRoleToAnthropicMessage(base, "user");
375
438
  case "ai":
376
- return baseRoleToAnthropicMessage(base, "assistant");
439
+ return aiMessageToAnthropicMessage(base);
377
440
  case "tool":
378
441
  return toolMessageToAnthropicMessage(base);
379
442
  default:
443
+ console.warn(`Unknown BaseMessage type: ${type}`, base);
380
444
  return undefined;
381
445
  }
382
446
  }
@@ -519,6 +583,9 @@ function getAnthropicAPI(config) {
519
583
  if (system?.length) {
520
584
  ret.system = system;
521
585
  }
586
+ if (config?.thinking) {
587
+ ret.thinking = config?.thinking;
588
+ }
522
589
  return ret;
523
590
  }
524
591
  return {
@@ -67,6 +67,18 @@ export function getAnthropicAPI(config) {
67
67
  tool_calls: [tool],
68
68
  };
69
69
  }
70
+ function thinkingContentToMessageFields(thinkingContent) {
71
+ // TODO: Once a reasoning/thinking type is defined in LangChain, use it
72
+ return {
73
+ content: [thinkingContent],
74
+ };
75
+ }
76
+ function redactedThinkingContentToMessageFields(thinkingContent) {
77
+ // TODO: Once a reasoning/thinking type is defined in LangChain, use it
78
+ return {
79
+ content: [thinkingContent],
80
+ };
81
+ }
70
82
  function anthropicContentToMessageFields(anthropicContent) {
71
83
  const type = anthropicContent?.type;
72
84
  switch (type) {
@@ -74,7 +86,12 @@ export function getAnthropicAPI(config) {
74
86
  return textContentToMessageFields(anthropicContent);
75
87
  case "tool_use":
76
88
  return toolUseContentToMessageFields(anthropicContent);
89
+ case "thinking":
90
+ return thinkingContentToMessageFields(anthropicContent);
91
+ case "redacted_thinking":
92
+ return redactedThinkingContentToMessageFields(anthropicContent);
77
93
  default:
94
+ console.error(`Unknown message type: ${type}`, anthropicContent);
78
95
  return undefined;
79
96
  }
80
97
  }
@@ -318,6 +335,25 @@ export function getAnthropicAPI(config) {
318
335
  },
319
336
  };
320
337
  }
338
+ function thinkingContentToAnthropicContent(
339
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
340
+ content) {
341
+ // TODO: Once a Langchain Thinking type is defined, use it
342
+ return {
343
+ type: "thinking",
344
+ thinking: content.thinking,
345
+ signature: content.signature,
346
+ };
347
+ }
348
+ function redactedThinkingContentToAnthropicContent(
349
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
350
+ content) {
351
+ // TODO: Once a Langchain Thinking type is defined, use it
352
+ return {
353
+ type: "redacted_thinking",
354
+ data: content.data,
355
+ };
356
+ }
321
357
  function contentComplexToAnthropicContent(content) {
322
358
  const type = content?.type;
323
359
  switch (type) {
@@ -325,8 +361,12 @@ export function getAnthropicAPI(config) {
325
361
  return textContentToAnthropicContent(content);
326
362
  case "image_url":
327
363
  return imageContentToAnthropicContent(content);
364
+ case "thinking":
365
+ return thinkingContentToAnthropicContent(content);
366
+ case "redacted_thinking":
367
+ return redactedThinkingContentToAnthropicContent(content);
328
368
  default:
329
- console.warn(`Unexpected content type: ${type}`);
369
+ console.warn(`Unexpected content type: ${type}`, content);
330
370
  return undefined;
331
371
  }
332
372
  }
@@ -341,6 +381,20 @@ export function getAnthropicAPI(config) {
341
381
  });
342
382
  return ret;
343
383
  }
384
+ function toolCallToAnthropicContent(toolCall) {
385
+ return {
386
+ type: "tool_use",
387
+ id: toolCall.id,
388
+ name: toolCall.name,
389
+ input: toolCall.args,
390
+ };
391
+ }
392
+ function toolCallsToAnthropicContent(toolCalls) {
393
+ if (toolCalls === undefined) {
394
+ return [];
395
+ }
396
+ return toolCalls.map(toolCallToAnthropicContent);
397
+ }
344
398
  function baseRoleToAnthropicMessage(base, role) {
345
399
  const content = contentToAnthropicContent(base.content);
346
400
  return {
@@ -348,6 +402,15 @@ export function getAnthropicAPI(config) {
348
402
  content,
349
403
  };
350
404
  }
405
+ function aiMessageToAnthropicMessage(base) {
406
+ const ret = baseRoleToAnthropicMessage(base, "assistant");
407
+ const toolContent = toolCallsToAnthropicContent(base.tool_calls);
408
+ if (toolContent.length > 0) {
409
+ const content = ret.content;
410
+ ret.content = [...content, ...toolContent];
411
+ }
412
+ return ret;
413
+ }
351
414
  function toolMessageToAnthropicMessage(base) {
352
415
  const role = "user";
353
416
  const toolUseId = base.tool_call_id;
@@ -370,10 +433,11 @@ export function getAnthropicAPI(config) {
370
433
  case "human":
371
434
  return baseRoleToAnthropicMessage(base, "user");
372
435
  case "ai":
373
- return baseRoleToAnthropicMessage(base, "assistant");
436
+ return aiMessageToAnthropicMessage(base);
374
437
  case "tool":
375
438
  return toolMessageToAnthropicMessage(base);
376
439
  default:
440
+ console.warn(`Unknown BaseMessage type: ${type}`, base);
377
441
  return undefined;
378
442
  }
379
443
  }
@@ -516,6 +580,9 @@ export function getAnthropicAPI(config) {
516
580
  if (system?.length) {
517
581
  ret.system = system;
518
582
  }
583
+ if (config?.thinking) {
584
+ ret.thinking = config?.thinking;
585
+ }
519
586
  return ret;
520
587
  }
521
588
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/google-common",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Core types and classes for Google services.",
5
5
  "type": "module",
6
6
  "engines": {