ai-sdk-adapter.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * AI SDK Adapter for LangGraph
  3. *
  4. * Provides LangChain-compatible interface for LLM calls.
  5. * Uses the unified callLLM / streamLLM layer which goes through
  6. * Vercel AI SDK, supporting all providers (OpenAI, Anthropic, Google, etc.).
  7. */
  8. import { BaseChatModel } from '@langchain/core/language_models/chat_models';
  9. import { BaseMessage, HumanMessage, AIMessage, SystemMessage } from '@langchain/core/messages';
  10. import { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
  11. import { ChatResult } from '@langchain/core/outputs';
  12. import type { LanguageModel } from 'ai';
  13. import { callLLM, streamLLM } from '@/lib/ai/llm';
  14. import type { ThinkingConfig } from '@/lib/types/provider';
  15. import { createLogger } from '@/lib/logger';
  16. const log = createLogger('AISdkAdapter');
  17. /**
  18. * Stream chunk types for streaming generation
  19. */
  20. export type StreamChunk =
  21. | { type: 'delta'; content: string }
  22. | {
  23. type: 'tool_calls';
  24. toolCalls: {
  25. id: string;
  26. index: number;
  27. type: 'function';
  28. function: { name: string; arguments: string };
  29. }[];
  30. }
  31. | { type: 'done'; content: string };
  32. /**
  33. * Adapter to use any AI SDK LanguageModel with LangGraph
  34. *
  35. * Accepts a LanguageModel instance (from getModel()) instead of raw
  36. * API credentials, enabling support for all providers.
  37. */
  38. export class AISdkLangGraphAdapter extends BaseChatModel {
  39. private languageModel: LanguageModel;
  40. private thinking?: ThinkingConfig;
  41. constructor(languageModel: LanguageModel, thinking?: ThinkingConfig) {
  42. super({});
  43. this.languageModel = languageModel;
  44. this.thinking = thinking;
  45. }
  46. _llmType(): string {
  47. return 'ai-sdk';
  48. }
  49. _combineLLMOutput() {
  50. return {};
  51. }
  52. /**
  53. * Convert LangChain messages to AI SDK message format
  54. */
  55. private convertMessages(
  56. messages: BaseMessage[],
  57. ): { role: 'system' | 'user' | 'assistant'; content: string }[] {
  58. return messages.map((msg) => {
  59. if (msg instanceof HumanMessage) {
  60. return { role: 'user' as const, content: msg.content as string };
  61. } else if (msg instanceof AIMessage) {
  62. return { role: 'assistant' as const, content: msg.content as string };
  63. } else if (msg instanceof SystemMessage) {
  64. return { role: 'system' as const, content: msg.content as string };
  65. } else {
  66. return { role: 'user' as const, content: msg.content as string };
  67. }
  68. });
  69. }
  70. async _generate(
  71. messages: BaseMessage[],
  72. _options?: this['ParsedCallOptions'],
  73. _runManager?: CallbackManagerForLLMRun,
  74. ): Promise<ChatResult> {
  75. const aiMessages = this.convertMessages(messages);
  76. try {
  77. const result = await callLLM(
  78. {
  79. model: this.languageModel,
  80. messages: aiMessages,
  81. },
  82. 'chat-adapter',
  83. undefined,
  84. this.thinking,
  85. );
  86. const content = result.text || '';
  87. log.info('[AI SDK Adapter] Response:', {
  88. textLength: content.length,
  89. });
  90. // Create AI message
  91. const aiMessage = new AIMessage({ content });
  92. return {
  93. generations: [
  94. {
  95. text: content,
  96. message: aiMessage,
  97. },
  98. ],
  99. llmOutput: {},
  100. };
  101. } catch (error) {
  102. log.error('[AI SDK Adapter Error]', error);
  103. throw error;
  104. }
  105. }
  106. /**
  107. * Stream generate with text deltas
  108. *
  109. * Yields chunks of text as they arrive, then yields done with full content.
  110. * Uses streamLLM which goes through Vercel AI SDK's streamText.
  111. */
  112. async *streamGenerate(
  113. messages: BaseMessage[],
  114. options?: { tools?: Record<string, unknown>; signal?: AbortSignal },
  115. ): AsyncGenerator<StreamChunk> {
  116. const aiMessages = this.convertMessages(messages);
  117. const result = streamLLM(
  118. {
  119. model: this.languageModel,
  120. messages: aiMessages,
  121. abortSignal: options?.signal,
  122. },
  123. 'chat-adapter-stream',
  124. this.thinking,
  125. );
  126. let fullContent = '';
  127. for await (const chunk of result.textStream) {
  128. if (chunk) {
  129. fullContent += chunk;
  130. yield { type: 'delta', content: chunk };
  131. }
  132. }
  133. // Yield done with full content
  134. yield { type: 'done', content: fullContent };
  135. }
  136. }