chore: update to ai sdk v5 beta (#1074)
This commit is contained in:
parent
7d8e71383f
commit
4c281fe09d
54 changed files with 1372 additions and 1060 deletions
|
|
@ -1,46 +1,51 @@
|
|||
import { generateUUID } from '@/lib/utils';
|
||||
import { DataStreamWriter, tool } from 'ai';
|
||||
import { tool, type UIMessageStreamWriter } from 'ai';
|
||||
import { z } from 'zod';
|
||||
import { Session } from 'next-auth';
|
||||
import type { Session } from 'next-auth';
|
||||
import {
|
||||
artifactKinds,
|
||||
documentHandlersByArtifactKind,
|
||||
} from '@/lib/artifacts/server';
|
||||
import type { ChatMessage } from '@/lib/types';
|
||||
|
||||
interface CreateDocumentProps {
|
||||
session: Session;
|
||||
dataStream: DataStreamWriter;
|
||||
dataStream: UIMessageStreamWriter<ChatMessage>;
|
||||
}
|
||||
|
||||
export const createDocument = ({ session, dataStream }: CreateDocumentProps) =>
|
||||
tool({
|
||||
description:
|
||||
'Create a document for a writing or content creation activities. This tool will call other functions that will generate the contents of the document based on the title and kind.',
|
||||
parameters: z.object({
|
||||
inputSchema: z.object({
|
||||
title: z.string(),
|
||||
kind: z.enum(artifactKinds),
|
||||
}),
|
||||
execute: async ({ title, kind }) => {
|
||||
const id = generateUUID();
|
||||
|
||||
dataStream.writeData({
|
||||
type: 'kind',
|
||||
content: kind,
|
||||
dataStream.write({
|
||||
type: 'data-kind',
|
||||
data: kind,
|
||||
transient: true,
|
||||
});
|
||||
|
||||
dataStream.writeData({
|
||||
type: 'id',
|
||||
content: id,
|
||||
dataStream.write({
|
||||
type: 'data-id',
|
||||
data: id,
|
||||
transient: true,
|
||||
});
|
||||
|
||||
dataStream.writeData({
|
||||
type: 'title',
|
||||
content: title,
|
||||
dataStream.write({
|
||||
type: 'data-title',
|
||||
data: title,
|
||||
transient: true,
|
||||
});
|
||||
|
||||
dataStream.writeData({
|
||||
type: 'clear',
|
||||
content: '',
|
||||
dataStream.write({
|
||||
type: 'data-clear',
|
||||
data: null,
|
||||
transient: true,
|
||||
});
|
||||
|
||||
const documentHandler = documentHandlersByArtifactKind.find(
|
||||
|
|
@ -59,7 +64,7 @@ export const createDocument = ({ session, dataStream }: CreateDocumentProps) =>
|
|||
session,
|
||||
});
|
||||
|
||||
dataStream.writeData({ type: 'finish', content: '' });
|
||||
dataStream.write({ type: 'data-finish', data: null, transient: true });
|
||||
|
||||
return {
|
||||
id,
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { z } from 'zod';
|
|||
|
||||
export const getWeather = tool({
|
||||
description: 'Get the current weather at a location',
|
||||
parameters: z.object({
|
||||
inputSchema: z.object({
|
||||
latitude: z.number(),
|
||||
longitude: z.number(),
|
||||
}),
|
||||
|
|
|
|||
|
|
@ -1,14 +1,15 @@
|
|||
import { z } from 'zod';
|
||||
import { Session } from 'next-auth';
|
||||
import { DataStreamWriter, streamObject, tool } from 'ai';
|
||||
import type { Session } from 'next-auth';
|
||||
import { streamObject, tool, type UIMessageStreamWriter } from 'ai';
|
||||
import { getDocumentById, saveSuggestions } from '@/lib/db/queries';
|
||||
import { Suggestion } from '@/lib/db/schema';
|
||||
import type { Suggestion } from '@/lib/db/schema';
|
||||
import { generateUUID } from '@/lib/utils';
|
||||
import { myProvider } from '../providers';
|
||||
import type { ChatMessage } from '@/lib/types';
|
||||
|
||||
interface RequestSuggestionsProps {
|
||||
session: Session;
|
||||
dataStream: DataStreamWriter;
|
||||
dataStream: UIMessageStreamWriter<ChatMessage>;
|
||||
}
|
||||
|
||||
export const requestSuggestions = ({
|
||||
|
|
@ -17,7 +18,7 @@ export const requestSuggestions = ({
|
|||
}: RequestSuggestionsProps) =>
|
||||
tool({
|
||||
description: 'Request suggestions for a document',
|
||||
parameters: z.object({
|
||||
inputSchema: z.object({
|
||||
documentId: z
|
||||
.string()
|
||||
.describe('The ID of the document to request edits'),
|
||||
|
|
@ -49,7 +50,8 @@ export const requestSuggestions = ({
|
|||
});
|
||||
|
||||
for await (const element of elementStream) {
|
||||
const suggestion = {
|
||||
// @ts-ignore todo: fix type
|
||||
const suggestion: Suggestion = {
|
||||
originalText: element.originalSentence,
|
||||
suggestedText: element.suggestedSentence,
|
||||
description: element.description,
|
||||
|
|
@ -58,9 +60,10 @@ export const requestSuggestions = ({
|
|||
isResolved: false,
|
||||
};
|
||||
|
||||
dataStream.writeData({
|
||||
type: 'suggestion',
|
||||
content: suggestion,
|
||||
dataStream.write({
|
||||
type: 'data-suggestion',
|
||||
data: suggestion,
|
||||
transient: true,
|
||||
});
|
||||
|
||||
suggestions.push(suggestion);
|
||||
|
|
|
|||
|
|
@ -1,18 +1,19 @@
|
|||
import { DataStreamWriter, tool } from 'ai';
|
||||
import { Session } from 'next-auth';
|
||||
import { tool, type UIMessageStreamWriter } from 'ai';
|
||||
import type { Session } from 'next-auth';
|
||||
import { z } from 'zod';
|
||||
import { getDocumentById, saveDocument } from '@/lib/db/queries';
|
||||
import { getDocumentById } from '@/lib/db/queries';
|
||||
import { documentHandlersByArtifactKind } from '@/lib/artifacts/server';
|
||||
import type { ChatMessage } from '@/lib/types';
|
||||
|
||||
interface UpdateDocumentProps {
|
||||
session: Session;
|
||||
dataStream: DataStreamWriter;
|
||||
dataStream: UIMessageStreamWriter<ChatMessage>;
|
||||
}
|
||||
|
||||
export const updateDocument = ({ session, dataStream }: UpdateDocumentProps) =>
|
||||
tool({
|
||||
description: 'Update a document with the given description.',
|
||||
parameters: z.object({
|
||||
inputSchema: z.object({
|
||||
id: z.string().describe('The ID of the document to update'),
|
||||
description: z
|
||||
.string()
|
||||
|
|
@ -27,9 +28,10 @@ export const updateDocument = ({ session, dataStream }: UpdateDocumentProps) =>
|
|||
};
|
||||
}
|
||||
|
||||
dataStream.writeData({
|
||||
type: 'clear',
|
||||
content: document.title,
|
||||
dataStream.write({
|
||||
type: 'data-clear',
|
||||
data: null,
|
||||
transient: true,
|
||||
});
|
||||
|
||||
const documentHandler = documentHandlersByArtifactKind.find(
|
||||
|
|
@ -48,7 +50,7 @@ export const updateDocument = ({ session, dataStream }: UpdateDocumentProps) =>
|
|||
session,
|
||||
});
|
||||
|
||||
dataStream.writeData({ type: 'finish', content: '' });
|
||||
dataStream.write({ type: 'data-finish', data: null, transient: true });
|
||||
|
||||
return {
|
||||
id,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue