Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 1 addition & 148 deletions genkit-tools/common/src/types/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,159 +14,12 @@
* limitations under the License.
*/
import z from 'zod';
import { MediaPartSchema, TextPartSchema } from './parts';

//
// IMPORTANT: Keep this file in sync with genkit/ai/src/document.ts!
//

const EmptyPartSchema = z.object({
text: z.never().optional(),
media: z.never().optional(),
toolRequest: z.never().optional(),
toolResponse: z.never().optional(),
data: z.unknown().optional(),
metadata: z.record(z.unknown()).optional(),
custom: z.record(z.unknown()).optional(),
reasoning: z.never().optional(),
resource: z.never().optional(),
});

/**
* Zod schema for a text part.
*/
export const TextPartSchema = EmptyPartSchema.extend({
/** The text of the document. */
text: z.string(),
});

/**
* Text part.
*/
export type TextPart = z.infer<typeof TextPartSchema>;

/**
* Zod schema for a reasoning part.
*/
export const ReasoningPartSchema = EmptyPartSchema.extend({
/** The reasoning text of the message. */
reasoning: z.string(),
});

/**
* Reasoning part.
*/
export type ReasoningPart = z.infer<typeof ReasoningPartSchema>;

/**
* Zod schema of media.
*/
export const MediaSchema = z.object({
/** The media content type. Inferred from data uri if not provided. */
contentType: z.string().optional(),
/** A `data:` or `https:` uri containing the media content. */
url: z.string(),
});

/**
* Zod schema of a media part.
*/
export const MediaPartSchema = EmptyPartSchema.extend({
media: MediaSchema,
});

/**
* Media part.
*/
export type MediaPart = z.infer<typeof MediaPartSchema>;

/**
* Zod schema of a tool request.
*/
export const ToolRequestSchema = z.object({
/** The call id or reference for a specific request. */
ref: z.string().optional(),
/** The name of the tool to call. */
name: z.string(),
/** The input parameters for the tool, usually a JSON object. */
input: z.unknown().optional(),
});

/**
* Zod schema of a tool request part.
*/
export const ToolRequestPartSchema = EmptyPartSchema.extend({
/** A request for a tool to be executed, usually provided by a model. */
toolRequest: ToolRequestSchema,
});

/**
* Tool part.
*/
export type ToolRequestPart = z.infer<typeof ToolRequestPartSchema>;

/**
* Zod schema of a tool response.
*/
export const ToolResponseSchema = z.object({
/** The call id or reference for a specific request. */
ref: z.string().optional(),
/** The name of the tool. */
name: z.string(),
/** The output data returned from the tool, usually a JSON object. */
output: z.unknown().optional(),
});

/**
* Zod schema of a tool response part.
*/
export const ToolResponsePartSchema = EmptyPartSchema.extend({
/** A provided response to a tool call. */
toolResponse: ToolResponseSchema,
});

/**
* Tool response part.
*/
export type ToolResponsePart = z.infer<typeof ToolResponsePartSchema>;

/**
* Zod schema of a data part.
*/
export const DataPartSchema = EmptyPartSchema.extend({
data: z.unknown(),
});

/**
* Data part.
*/
export type DataPart = z.infer<typeof DataPartSchema>;

/**
* Zod schema of a custom part.
*/
export const CustomPartSchema = EmptyPartSchema.extend({
custom: z.record(z.any()),
});

/**
* Custom part.
*/
export type CustomPart = z.infer<typeof CustomPartSchema>;

/**
* Zod schema of a resource part.
*/
export const ResourcePartSchema = EmptyPartSchema.extend({
resource: z.object({
uri: z.string(),
}),
});

/**
* Resource part.
*/
export type ResourcePart = z.infer<typeof ResourcePartSchema>;

// Disclaimer: genkit/js/ai/document.ts defines the following schema, type pair
// as PartSchema and Part, respectively. genkit-tools cannot retain those names
// due to it clashing with similar schema in model.ts, and genkit-tools
Expand Down
27 changes: 6 additions & 21 deletions genkit-tools/common/src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
* limitations under the License.
*/
import { z } from 'zod';
import { DocumentDataSchema } from './document';
import {
CustomPartSchema,
DataPartSchema,
DocumentDataSchema,
MediaPartSchema,
PartSchema,
ReasoningPartSchema,
ResourcePartSchema,
TextPartSchema,
Expand All @@ -27,16 +28,18 @@ import {
type CustomPart,
type DataPart,
type MediaPart,
type Part,
type ReasoningPart,
type ResourcePart,
type TextPart,
type ToolRequestPart,
type ToolResponsePart,
} from './document';
} from './parts';
export {
CustomPartSchema,
DataPartSchema,
MediaPartSchema,
PartSchema,
ReasoningPartSchema,
ResourcePartSchema,
TextPartSchema,
Expand All @@ -45,6 +48,7 @@ export {
type CustomPart,
type DataPart,
type MediaPart,
type Part,
type ReasoningPart,
type ResourcePart,
type TextPart,
Expand Down Expand Up @@ -73,25 +77,6 @@ export const OperationSchema = z.object({
*/
export type OperationData = z.infer<typeof OperationSchema>;

/**
* Zod schema of message part.
*/
export const PartSchema = z.union([
TextPartSchema,
MediaPartSchema,
ToolRequestPartSchema,
ToolResponsePartSchema,
DataPartSchema,
CustomPartSchema,
ReasoningPartSchema,
ResourcePartSchema,
]);

/**
* Message part.
*/
export type Part = z.infer<typeof PartSchema>;

/**
* Zod schema of a message role.
*/
Expand Down
Loading
Loading