-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
390 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { JSONValue, StreamingTextResponse } from 'ai'; | ||
import { StreamableValue } from 'ai/rsc'; | ||
|
||
/** | ||
* Converts a ReadableStream response from the chat() function into a StreamingTextResponse | ||
* suitable for use with the ai-sdk's useChat Next.js hook. | ||
* | ||
* @param response - The response object containing: | ||
* - output: A ReadableStream of LangChainAIMessageChunk from the chat function. | ||
* - isStream: A boolean indicating if the response is a stream. | ||
* @returns StreamingTextResponse - The adapted response for use with the useChat hook. | ||
*/ | ||
declare const aiUseChatAdapter: (response: { | ||
output: ReadableStream<string>; | ||
isStream: true; | ||
}, metadata?: JSONValue) => StreamingTextResponse; | ||
|
||
declare const readServerActionStream: (stream: StreamableValue<string>) => AsyncIterable<string | undefined>; | ||
|
||
declare const createServerActionStream: (stream: ReadableStream<string>) => StreamableValue<string>; | ||
|
||
export { aiUseChatAdapter, createServerActionStream, readServerActionStream }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { JSONValue, StreamingTextResponse } from 'ai'; | ||
import { StreamableValue } from 'ai/rsc'; | ||
|
||
/** | ||
* Converts a ReadableStream response from the chat() function into a StreamingTextResponse | ||
* suitable for use with the ai-sdk's useChat Next.js hook. | ||
* | ||
* @param response - The response object containing: | ||
* - output: A ReadableStream of LangChainAIMessageChunk from the chat function. | ||
* - isStream: A boolean indicating if the response is a stream. | ||
* @returns StreamingTextResponse - The adapted response for use with the useChat hook. | ||
*/ | ||
declare const aiUseChatAdapter: (response: { | ||
output: ReadableStream<string>; | ||
isStream: true; | ||
}, metadata?: JSONValue) => StreamingTextResponse; | ||
|
||
declare const readServerActionStream: (stream: StreamableValue<string>) => AsyncIterable<string | undefined>; | ||
|
||
declare const createServerActionStream: (stream: ReadableStream<string>) => StreamableValue<string>; | ||
|
||
export { aiUseChatAdapter, createServerActionStream, readServerActionStream }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"use strict"; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
|
||
// src/nextjs/index.ts | ||
var nextjs_exports = {}; | ||
__export(nextjs_exports, { | ||
aiUseChatAdapter: () => aiUseChatAdapter, | ||
readServerActionStream: () => readServerActionStream | ||
}); | ||
module.exports = __toCommonJS(nextjs_exports); | ||
|
||
// src/nextjs/chat-adapter.ts | ||
var import_ai = require("ai"); | ||
var aiUseChatAdapter = (response, metadata) => { | ||
const streamData = new import_ai.StreamData(); | ||
const wrappedStream = import_ai.LangChainAdapter.toAIStream(response.output, { | ||
onStart() { | ||
if (metadata) { | ||
streamData.append(metadata); | ||
} | ||
}, | ||
onFinal() { | ||
void streamData.close(); | ||
} | ||
}); | ||
return new import_ai.StreamingTextResponse(wrappedStream, {}, streamData); | ||
}; | ||
|
||
// src/nextjs/server-action-read-adapter.ts | ||
var import_rsc = require("ai/rsc"); | ||
var readServerActionStream = (stream) => { | ||
return (0, import_rsc.readStreamableValue)(stream); | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
aiUseChatAdapter, | ||
readServerActionStream | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// src/nextjs/chat-adapter.ts | ||
import { LangChainAdapter, StreamData, StreamingTextResponse } from "ai"; | ||
var aiUseChatAdapter = (response, metadata) => { | ||
const streamData = new StreamData(); | ||
const wrappedStream = LangChainAdapter.toAIStream(response.output, { | ||
onStart() { | ||
if (metadata) { | ||
streamData.append(metadata); | ||
} | ||
}, | ||
onFinal() { | ||
void streamData.close(); | ||
} | ||
}); | ||
return new StreamingTextResponse(wrappedStream, {}, streamData); | ||
}; | ||
|
||
// src/nextjs/server-action-read-adapter.ts | ||
import { readStreamableValue } from "ai/rsc"; | ||
var readServerActionStream = (stream) => { | ||
return readStreamableValue(stream); | ||
}; | ||
export { | ||
aiUseChatAdapter, | ||
readServerActionStream | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { StreamableValue } from 'ai/rsc'; | ||
import { JSONValue, StreamingTextResponse } from 'ai'; | ||
|
||
declare const createServerActionStream: (stream: ReadableStream<string>) => StreamableValue<string>; | ||
|
||
/** | ||
* Converts a ReadableStream response from the chat() function into a StreamingTextResponse | ||
* suitable for use with the ai-sdk's useChat Next.js hook. | ||
* | ||
* @param response - The response object containing: | ||
* - output: A ReadableStream of LangChainAIMessageChunk from the chat function. | ||
* - isStream: A boolean indicating if the response is a stream. | ||
* @returns StreamingTextResponse - The adapted response for use with the useChat hook. | ||
*/ | ||
declare const aiUseChatAdapter: (response: { | ||
output: ReadableStream<string>; | ||
isStream: true; | ||
}, metadata?: JSONValue) => StreamingTextResponse; | ||
|
||
export { aiUseChatAdapter, createServerActionStream }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// src/nextjs/server-action-write-adapter.ts | ||
import { createStreamableValue } from "ai/rsc"; | ||
var createServerActionStream = (stream) => { | ||
const streamableValue = createStreamableValue(""); | ||
const reader = stream.getReader(); | ||
(async () => { | ||
try { | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) break; | ||
if (typeof value === "string") { | ||
streamableValue.update(value); | ||
} | ||
} | ||
streamableValue.done(); | ||
} catch (error) { | ||
console.error("Error while reading stream:", error); | ||
streamableValue.error("An error occurred while processing the stream"); | ||
} | ||
})(); | ||
return streamableValue.value; | ||
}; | ||
|
||
// src/nextjs/chat-adapter.ts | ||
import { LangChainAdapter, StreamData, StreamingTextResponse } from "ai"; | ||
var aiUseChatAdapter = (response, metadata) => { | ||
const streamData = new StreamData(); | ||
const wrappedStream = LangChainAdapter.toAIStream(response.output, { | ||
onStart() { | ||
if (metadata) { | ||
streamData.append(metadata); | ||
} | ||
}, | ||
onFinal() { | ||
void streamData.close(); | ||
} | ||
}); | ||
return new StreamingTextResponse(wrappedStream, {}, streamData); | ||
}; | ||
export { | ||
aiUseChatAdapter, | ||
createServerActionStream | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { JSONValue, StreamingTextResponse } from 'ai'; | ||
import { StreamableValue } from 'ai/rsc'; | ||
|
||
/** | ||
* Converts a ReadableStream response from the chat() function into a StreamingTextResponse | ||
* suitable for use with the ai-sdk's useChat Next.js hook. | ||
* | ||
* @param response - The response object containing: | ||
* - output: A ReadableStream of LangChainAIMessageChunk from the chat function. | ||
* - isStream: A boolean indicating if the response is a stream. | ||
* @returns StreamingTextResponse - The adapted response for use with the useChat hook. | ||
*/ | ||
declare const aiUseChatAdapter: (response: { | ||
output: ReadableStream<string>; | ||
isStream: true; | ||
}, metadata?: JSONValue) => StreamingTextResponse; | ||
|
||
declare const readServerActionStream: (stream: StreamableValue<string>) => AsyncIterable<string | undefined>; | ||
|
||
declare const createServerActionStream: (stream: ReadableStream<string>) => StreamableValue<string>; | ||
|
||
export { aiUseChatAdapter, createServerActionStream, readServerActionStream }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { JSONValue, StreamingTextResponse } from 'ai'; | ||
import { StreamableValue } from 'ai/rsc'; | ||
|
||
/** | ||
* Converts a ReadableStream response from the chat() function into a StreamingTextResponse | ||
* suitable for use with the ai-sdk's useChat Next.js hook. | ||
* | ||
* @param response - The response object containing: | ||
* - output: A ReadableStream of LangChainAIMessageChunk from the chat function. | ||
* - isStream: A boolean indicating if the response is a stream. | ||
* @returns StreamingTextResponse - The adapted response for use with the useChat hook. | ||
*/ | ||
declare const aiUseChatAdapter: (response: { | ||
output: ReadableStream<string>; | ||
isStream: true; | ||
}, metadata?: JSONValue) => StreamingTextResponse; | ||
|
||
declare const readServerActionStream: (stream: StreamableValue<string>) => AsyncIterable<string | undefined>; | ||
|
||
declare const createServerActionStream: (stream: ReadableStream<string>) => StreamableValue<string>; | ||
|
||
export { aiUseChatAdapter, createServerActionStream, readServerActionStream }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"use strict"; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
|
||
// nextjs/index.ts | ||
var nextjs_exports = {}; | ||
__export(nextjs_exports, { | ||
aiUseChatAdapter: () => aiUseChatAdapter, | ||
readServerActionStream: () => readServerActionStream | ||
}); | ||
module.exports = __toCommonJS(nextjs_exports); | ||
|
||
// nextjs/chat-adapter.ts | ||
var import_ai = require("ai"); | ||
var aiUseChatAdapter = (response, metadata) => { | ||
const streamData = new import_ai.StreamData(); | ||
const wrappedStream = import_ai.LangChainAdapter.toAIStream(response.output, { | ||
onStart() { | ||
if (metadata) { | ||
streamData.append(metadata); | ||
} | ||
}, | ||
onFinal() { | ||
void streamData.close(); | ||
} | ||
}); | ||
return new import_ai.StreamingTextResponse(wrappedStream, {}, streamData); | ||
}; | ||
|
||
// nextjs/server-action-read-adapter.ts | ||
var import_rsc = require("ai/rsc"); | ||
var readServerActionStream = (stream) => { | ||
return (0, import_rsc.readStreamableValue)(stream); | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
aiUseChatAdapter, | ||
readServerActionStream | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// nextjs/chat-adapter.ts | ||
import { LangChainAdapter, StreamData, StreamingTextResponse } from "ai"; | ||
var aiUseChatAdapter = (response, metadata) => { | ||
const streamData = new StreamData(); | ||
const wrappedStream = LangChainAdapter.toAIStream(response.output, { | ||
onStart() { | ||
if (metadata) { | ||
streamData.append(metadata); | ||
} | ||
}, | ||
onFinal() { | ||
void streamData.close(); | ||
} | ||
}); | ||
return new StreamingTextResponse(wrappedStream, {}, streamData); | ||
}; | ||
|
||
// nextjs/server-action-read-adapter.ts | ||
import { readStreamableValue } from "ai/rsc"; | ||
var readServerActionStream = (stream) => { | ||
return readStreamableValue(stream); | ||
}; | ||
export { | ||
aiUseChatAdapter, | ||
readServerActionStream | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { StreamableValue } from 'ai/rsc'; | ||
import { JSONValue, StreamingTextResponse } from 'ai'; | ||
|
||
declare const createServerActionStream: (stream: ReadableStream<string>) => StreamableValue<string>; | ||
|
||
/** | ||
* Converts a ReadableStream response from the chat() function into a StreamingTextResponse | ||
* suitable for use with the ai-sdk's useChat Next.js hook. | ||
* | ||
* @param response - The response object containing: | ||
* - output: A ReadableStream of LangChainAIMessageChunk from the chat function. | ||
* - isStream: A boolean indicating if the response is a stream. | ||
* @returns StreamingTextResponse - The adapted response for use with the useChat hook. | ||
*/ | ||
declare const aiUseChatAdapter: (response: { | ||
output: ReadableStream<string>; | ||
isStream: true; | ||
}, metadata?: JSONValue) => StreamingTextResponse; | ||
|
||
export { aiUseChatAdapter, createServerActionStream }; |
Oops, something went wrong.