UNPKG

@genkit-ai/ai

Version:

Genkit AI framework generative AI APIs.

187 lines 6.03 kB
import { z } from "@genkit-ai/core"; import { MessageSchema, ModelResponseChunkSchema, PartSchema } from "./model-types.mjs"; import { ToolRequestPartSchema, ToolResponsePartSchema } from "./parts.mjs"; const RuntimeErrorSchema = z.object({ /** Canonical status name (e.g. `INTERNAL`, `FAILED_PRECONDITION`). */ status: z.string().optional(), /** Human-readable error message. */ message: z.string(), /** Optional structured details describing the failure. */ details: z.any().optional() }); const ArtifactSchema = z.object({ /** Name identifies the artifact (e.g., "generated_code.go", "diagram.png"). */ name: z.string().optional(), /** Parts contains the artifact content (text, media, etc.). */ parts: z.array(PartSchema), /** Metadata contains additional artifact-specific data. */ metadata: z.record(z.any()).optional() }); const SnapshotStatusSchema = z.enum([ "pending", "completed", "aborted", "failed", "expired" ]); const AgentFinishReasonSchema = z.enum([ // Mirror of generate's FinishReason: "stop", "length", "blocked", "interrupted", "other", "unknown", // Agent-specific additions: "aborted", "detached", "failed" ]); const SessionStateSchema = z.object({ sessionId: z.string().optional(), messages: z.array(MessageSchema).optional(), custom: z.any().optional(), artifacts: z.array(ArtifactSchema).optional() }); const AgentInputSchema = z.object({ /** User's input message for this turn. */ message: MessageSchema.optional(), /** Options for resuming an interrupted generation. */ resume: z.object({ respond: z.array(ToolResponsePartSchema).optional(), restart: z.array(ToolRequestPartSchema).optional() }).optional(), detach: z.boolean().optional() }); const AgentInitSchema = z.object({ snapshotId: z.string().optional(), sessionId: z.string().optional(), state: SessionStateSchema.optional() }); const AgentResultSchema = z.object({ message: MessageSchema.optional(), artifacts: z.array(ArtifactSchema).optional(), /** The reason the whole invocation finished (e.g. `stop`, `interrupted`). */ finishReason: AgentFinishReasonSchema.optional() }); const AgentOutputSchema = z.object({ /** * ID of the session this invocation belongs to, assigned by the framework * when the invocation starts. */ sessionId: z.string().optional(), snapshotId: z.string().optional(), state: SessionStateSchema.optional(), message: MessageSchema.optional(), artifacts: z.array(ArtifactSchema).optional(), /** The reason the invocation finished (e.g. `stop`, `interrupted`). */ finishReason: AgentFinishReasonSchema.optional(), /** * Present when `finishReason` is `failed`. Carries the original error * details (RuntimeError shape; the runtime resolves gracefully instead of * throwing). The accompanying `state`/`snapshotId` hold the last-good state - * the state the failed turn started with. */ error: RuntimeErrorSchema.optional() }); const TurnEndSchema = z.object({ snapshotId: z.string().optional(), /** The reason this turn finished (e.g. `stop`, `interrupted`). */ finishReason: AgentFinishReasonSchema.optional() }); const JsonPatchOpSchema = z.enum([ "add", "remove", "replace", "move", "copy", "test" ]); const JsonPatchOperationSchema = z.object({ op: JsonPatchOpSchema, /** A JSON Pointer (RFC 6901) to the target location, e.g. `"/agentStatus"`. */ path: z.string(), /** Source pointer; required for `move` and `copy`. */ from: z.string().optional(), /** New value; required for `add`, `replace`, and `test`. */ value: z.any().optional() }); const JsonPatchSchema = z.array(JsonPatchOperationSchema); const AgentStreamChunkSchema = z.object({ modelChunk: ModelResponseChunkSchema.optional(), /** * An RFC 6902 JSON Patch describing a delta applied to the session's * `custom` state. The runtime auto-emits these whenever custom state is * mutated during a turn; clients apply them to keep their tracked custom * state live mid-stream. */ customPatch: JsonPatchSchema.optional(), artifact: ArtifactSchema.optional(), turnEnd: TurnEndSchema.optional() }); const SessionSnapshotSchema = z.object({ snapshotId: z.string(), sessionId: z.string().optional(), parentId: z.string().optional(), createdAt: z.string(), updatedAt: z.string().optional(), heartbeatAt: z.string().optional(), status: SnapshotStatusSchema.optional(), finishReason: AgentFinishReasonSchema.optional(), error: RuntimeErrorSchema.optional(), state: SessionStateSchema.optional() }); const GetSnapshotRequestSchema = z.object({ snapshotId: z.string().optional(), sessionId: z.string().optional() }); const AgentAbortRequestSchema = z.object({ snapshotId: z.string() }); const AgentAbortResponseSchema = z.object({ snapshotId: z.string(), status: SnapshotStatusSchema.optional() }); const AgentStateManagementSchema = z.enum(["server", "client"]); const AgentMetadataSchema = z.object({ /** Who owns session state for this agent. */ stateManagement: AgentStateManagementSchema, /** * Whether the agent's invocations can be aborted. True only when the * configured store implements the abort lifecycle. */ abortable: z.boolean(), /** * JSON schema for the agent's custom session state (the `custom` field of * `SessionState`), inferred from the agent's state type. Omitted when the * state type carries no schema to infer. */ stateSchema: z.record(z.any()).optional() }); export { AgentAbortRequestSchema, AgentAbortResponseSchema, AgentFinishReasonSchema, AgentInitSchema, AgentInputSchema, AgentMetadataSchema, AgentOutputSchema, AgentResultSchema, AgentStateManagementSchema, AgentStreamChunkSchema, ArtifactSchema, GetSnapshotRequestSchema, JsonPatchOpSchema, JsonPatchOperationSchema, JsonPatchSchema, RuntimeErrorSchema, SessionSnapshotSchema, SessionStateSchema, SnapshotStatusSchema, TurnEndSchema }; //# sourceMappingURL=agent-types.mjs.map