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
140 changes: 124 additions & 16 deletions genkit-tools/common/src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
DataPartSchema,
MediaPartSchema,
MultipartToolResponseSchema,
PartSchema,
ReasoningPartSchema,
ResourcePartSchema,
TextPartSchema,
Expand All @@ -30,7 +29,6 @@ import {
type DataPart,
type MediaPart,
type MultipartToolResponse,
type Part,
type ReasoningPart,
type ResourcePart,
type TextPart,
Expand All @@ -42,7 +40,6 @@ export {
DataPartSchema,
MediaPartSchema,
MultipartToolResponseSchema,
PartSchema,
ReasoningPartSchema,
ResourcePartSchema,
TextPartSchema,
Expand All @@ -52,7 +49,6 @@ export {
type DataPart,
type MediaPart,
type MultipartToolResponse,
type Part,
type ReasoningPart,
type ResourcePart,
type TextPart,
Expand All @@ -61,11 +57,53 @@ export {
};

//
// IMPORTANT: Keep this file in sync with genkit/ai/src/model.ts!
// IMPORTANT: Keep this file in sync with genkit/ai/src/model-types.ts!
//

/** Descriptor for a registered middleware, returned by reflection API. */
export const MiddlewareDescSchema = z.object({
/** Unique name of the middleware. */
name: z.string(),
/** Human-readable description of what the middleware does. */
description: z.string().optional(),
/** JSON Schema for the middleware's configuration. */
configSchema: z.record(z.any()).nullish(),
/** User defined metadata for the middleware. */
metadata: z.record(z.any()).nullish(),
});
export type MiddlewareDesc = z.infer<typeof MiddlewareDescSchema>;

/**
* Zod schema of middleware reference.
*/
export const MiddlewareRefSchema = z.object({
name: z.string(),
config: z.any().optional(),
});

/**
* Middleware reference.
*/
export type MiddlewareRef = z.infer<typeof MiddlewareRefSchema>;

/**
* Zod schema of an opration representing a model reference.
*/
export const ModelReferenceSchema = z.object({
name: z.string(),
configSchema: z.any().optional(),
info: z.any().optional(),
version: z.string().optional(),
config: z.any().optional(),
});

/**
* Model Reference
*/
export type ModelReference = z.infer<typeof ModelReferenceSchema>;

/**
* Zod schema of an opration representing a background task.
* Zod schema of an operation representing a background task.
*/
export const OperationSchema = z.object({
action: z.string().optional(),
Expand All @@ -81,6 +119,25 @@ 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 Expand Up @@ -136,7 +193,7 @@ export const ModelInfoSchema = z.object({
constrained: z.enum(['none', 'all', 'no-tools']).optional(),
/** Model supports controlling tool choice, e.g. forced tool calling. */
toolChoice: z.boolean().optional(),
/** Model supports long running operations. */
/** Model can perform long-running operations. */
longRunning: z.boolean().optional(),
})
.optional(),
Expand Down Expand Up @@ -182,18 +239,65 @@ export const ToolDefinitionSchema = z.object({
*/
export type ToolDefinition = z.infer<typeof ToolDefinitionSchema>;

/**
* Configuration parameter descriptions.
*/
export const GenerationCommonConfigDescriptions = {
temperature:
'Controls the degree of randomness in token selection. A lower value is ' +
'good for a more predictable response. A higher value leads to more ' +
'diverse or unexpected results.',
maxOutputTokens: 'The maximum number of tokens to include in the response.',
topK: 'The maximum number of tokens to consider when sampling.',
topP:
'Decides how many possible words to consider. A higher value means ' +
'that the model looks at more possible words, even the less likely ' +
'ones, which makes the generated text more diverse.',
};

/**
* Zod schema of a common config object.
*/
export const GenerationCommonConfigSchema = z.object({
/** A specific version of a model family, e.g. `gemini-1.0-pro-001` for the `gemini-1.0-pro` family. */
version: z.string().optional(),
temperature: z.number().optional(),
maxOutputTokens: z.number().optional(),
topK: z.number().optional(),
topP: z.number().optional(),
stopSequences: z.array(z.string()).optional(),
});
export const GenerationCommonConfigSchema = z
.object({
version: z
.string()
.describe(
'A specific version of a model family, e.g. `gemini-2.5-flash` ' +
'for the `googleai` family.'
)
.optional(),
temperature: z
.number()
.describe(GenerationCommonConfigDescriptions.temperature)
.optional(),
maxOutputTokens: z
.number()
.describe(GenerationCommonConfigDescriptions.maxOutputTokens)
.optional(),
topK: z
.number()
.describe(GenerationCommonConfigDescriptions.topK)
.optional(),
topP: z
.number()
.describe(GenerationCommonConfigDescriptions.topP)
.optional(),
stopSequences: z
.array(z.string())
.max(5)
.describe(
'Set of character sequences (up to 5) that will stop output generation.'
)
.optional(),
apiKey: z
.string()
.describe(
'API Key to use for the model call, overrides API key provided in plugin config.'
)
.optional(),
})
.passthrough();

/**
* Common config object.
Expand Down Expand Up @@ -379,6 +483,8 @@ export const GenerateActionOptionsSchema = z.object({
messages: z.array(MessageSchema),
/** List of registered tool names for this generation if supported by the underlying model. */
tools: z.array(z.string()).optional(),
/** List of registered resource names for this generation if supported by the underlying model. */
resources: z.array(z.string()).optional(),
/** Tool calling mode. `auto` lets the model decide whether to use tools, `required` forces the model to choose a tool, and `none` forces the model not to use any tools. Defaults to `auto`. */
toolChoice: z.enum(['auto', 'required', 'none']).optional(),
/** Configuration for the generation request. */
Expand All @@ -399,5 +505,7 @@ export const GenerateActionOptionsSchema = z.object({
maxTurns: z.number().optional(),
/** Custom step name for this generate call to display in trace views. Defaults to "generate". */
stepName: z.string().optional(),
/** Middleware to apply to this generation. */
use: z.array(MiddlewareRefSchema).optional(),
});
export type GenerateActionOptions = z.infer<typeof GenerateActionOptionsSchema>;
40 changes: 18 additions & 22 deletions genkit-tools/common/src/types/parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const EmptyPartSchema = z.object({
* Zod schema for a text part.
*/
export const TextPartSchema = EmptyPartSchema.extend({
/** The text of the document. */
/** The text of the message. */
text: z.string(),
});

Expand Down Expand Up @@ -89,6 +89,7 @@ export const ToolRequestSchema = z.object({
/** Whether the request is a partial chunk. */
partial: z.boolean().optional(),
});
export type ToolRequest = z.infer<typeof ToolRequestSchema>;

/**
* Zod schema of a tool request part.
Expand All @@ -103,6 +104,9 @@ export const ToolRequestPartSchema = EmptyPartSchema.extend({
*/
export type ToolRequestPart = z.infer<typeof ToolRequestPartSchema>;

/**
* Zod schema of a tool response.
*/
const ToolResponseSchemaBase = z.object({
/** The call id or reference for a specific request. */
ref: z.string().optional(),
Expand All @@ -116,14 +120,14 @@ const ToolResponseSchemaBase = z.object({
* Tool response part.
*/
export type ToolResponse = z.infer<typeof ToolResponseSchemaBase> & {
content?: Part[];
content?: TextOrMediaPart[];
};

export const ToolResponseSchema: z.ZodType<ToolResponse> =
ToolResponseSchemaBase.extend({
content: z.array(z.any()).optional(),
// TODO: switch to this once we have effective recursive schema support across the board.
// content: z.array(z.lazy(() => PartSchema)).optional(),
// content: z.array(z.lazy(() => DocumentPartSchema)).optional(),
});

/**
Expand All @@ -134,6 +138,9 @@ export const ToolResponsePartSchema = EmptyPartSchema.extend({
toolResponse: ToolResponseSchema,
});

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

/**
Expand Down Expand Up @@ -174,28 +181,17 @@ export const ResourcePartSchema = EmptyPartSchema.extend({
*/
export type ResourcePart = z.infer<typeof ResourcePartSchema>;

/**
* 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>;
// Disclaimer: genkit/js/ai/parts.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
// exporting all types at root. We use a different name here and updated
// coresponding the imports.
export const TextOrMediaPartSchema = z.union([TextPartSchema, MediaPartSchema]);
export type TextOrMediaPart = z.infer<typeof TextOrMediaPartSchema>;

export const MultipartToolResponseSchema = z.object({
output: z.unknown().optional(),
content: z.array(PartSchema).optional(),
content: z.array(TextOrMediaPartSchema).optional(),
});

export type MultipartToolResponse = z.infer<typeof MultipartToolResponseSchema>;
Loading
Loading