UNPKG

genkit

Version:

Genkit AI framework

1 lines 9.13 kB
{"version":3,"sources":["../src/genkit-beta.ts"],"sourcesContent":["/**\n * Copyright 2025 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n GenerateOptions,\n GenerateResponseData,\n GenerationCommonConfigSchema,\n SessionRunner,\n defineAgent,\n defineCustomAgent,\n defineInterrupt,\n definePromptAgent,\n defineResource,\n generateOperation,\n type Agent,\n type AgentConfig,\n type AgentFn,\n type AgentStreamChunk,\n type ClientTransform,\n type InterruptConfig,\n type PromptConfig,\n type ResourceAction,\n type ResourceFn,\n type ResourceOptions,\n type ToolAction,\n} from '@genkit-ai/ai';\n\nimport { defineFormat } from '@genkit-ai/ai/formats';\n\nimport {\n type SessionSnapshot,\n type SessionSnapshotInput,\n type SessionState,\n type SessionStore,\n type SessionStoreOptions,\n type SnapshotMutator,\n} from '@genkit-ai/ai/session';\nimport {\n FileSessionStore,\n InMemorySessionStore,\n} from '@genkit-ai/ai/session-stores';\n\nimport { applyPatch, diff } from '@genkit-ai/ai/json-patch';\nimport { type Operation, type z } from '@genkit-ai/core';\nimport type { Formatter } from './formats.mjs';\nimport { Genkit, type GenkitOptions } from './genkit.mjs';\n\nexport type { JsonPatch, JsonPatchOperation } from '@genkit-ai/ai/json-patch';\nexport {\n FileSessionStore,\n InMemorySessionStore,\n SessionRunner,\n applyPatch,\n diff,\n};\nexport type {\n Agent,\n AgentFn,\n AgentStreamChunk,\n ClientTransform,\n GenkitOptions as GenkitBetaOptions,\n PromptConfig,\n SessionSnapshot,\n SessionSnapshotInput,\n SessionState,\n SessionStore,\n SessionStoreOptions,\n SnapshotMutator,\n};\n\n/**\n * WARNING: these APIs are considered unstable and subject to frequent breaking changes that may not honor semver.\n *\n * Initializes Genkit BETA APIs with a set of options.\n *\n * This will create a new Genkit registry, register the provided plugins, stores, and other configuration. This\n * should be called before any flows are registered.\n *\n * @beta\n */\nexport function genkit(options: GenkitOptions): GenkitBeta {\n return new GenkitBeta(options);\n}\n\n/**\n * Genkit BETA APIs.\n *\n * @beta\n */\nexport class GenkitBeta extends Genkit {\n constructor(options?: GenkitOptions) {\n super(options);\n this.registry.apiStability = 'beta';\n }\n\n /**\n * Defines and registers a custom agent with a custom handler function.\n *\n * @beta\n */\n defineCustomAgent<State = unknown>(\n config: {\n name: string;\n description?: string;\n stateSchema?: z.ZodType<State>;\n store?: SessionStore<State>;\n },\n fn: AgentFn<State>\n ) {\n return defineCustomAgent<State>(this.registry, config, fn);\n }\n\n /**\n * Defines and registers an agent from an existing Prompt template.\n *\n * @beta\n */\n definePromptAgent<\n State = unknown,\n I extends z.ZodTypeAny = z.ZodTypeAny,\n >(config: {\n promptName: string;\n /**\n * Input values for the referenced prompt's input variables. Lets a single\n * prompt be reused/customized across multiple agents.\n */\n promptInput?: z.infer<I>;\n stateSchema?: z.ZodType<State>;\n store?: SessionStore<State>;\n }) {\n return definePromptAgent<State, I>(this.registry, config);\n }\n\n /**\n * Defines and registers an agent by creating a prompt and wiring it into a\n * multi-turn agent in one step.\n *\n * This is a convenience shortcut that combines `definePrompt` and\n * `definePromptAgent` into a single call.\n *\n * ```ts\n * const myAgent = ai.defineAgent({\n * name: 'myAgent',\n * model: 'googleai/gemini-2.5-flash',\n * system: 'Talk like a pirate.',\n * tools: [weatherTool],\n * store: new FileSessionStore('./.snapshots'),\n * });\n * ```\n *\n * @beta\n */\n defineAgent<State = unknown, I extends z.ZodTypeAny = z.ZodTypeAny>(\n config: AgentConfig<State, I>\n ) {\n return defineAgent<State, I>(this.registry, config);\n }\n\n /**\n * Defines and registers a custom model output formatter.\n *\n * Here's an example of a custom JSON output formatter:\n *\n * ```ts\n * import { extractJson } from 'genkit/extract';\n *\n * ai.defineFormat(\n * { name: 'customJson' },\n * (schema) => {\n * let instructions: string | undefined;\n * if (schema) {\n * instructions = `Output should be in JSON format and conform to the following schema:\n * \\`\\`\\`\n * ${JSON.stringify(schema)}\n * \\`\\`\\`\n * `;\n * }\n * return {\n * parseChunk: (chunk) => extractJson(chunk.accumulatedText),\n * parseMessage: (message) => extractJson(message.text),\n * instructions,\n * };\n * }\n * );\n *\n * const { output } = await ai.generate({\n * prompt: 'Invent a menu item for a pirate themed restaurant.',\n * output: { format: 'customJson', schema: MenuItemSchema },\n * });\n * ```\n *\n * @beta\n */\n defineFormat(\n options: {\n name: string;\n } & Formatter['config'],\n handler: Formatter['handler']\n ): { config: Formatter['config']; handler: Formatter['handler'] } {\n return defineFormat(this.registry, options, handler);\n }\n\n /**\n * Defines and registers an interrupt.\n *\n * Interrupts are special tools that halt model processing and return control back to the caller. Interrupts make it simpler to implement\n * \"human-in-the-loop\" and out-of-band processing patterns that require waiting on external actions to complete.\n *\n * @beta\n */\n defineInterrupt<I extends z.ZodTypeAny, O extends z.ZodTypeAny>(\n config: InterruptConfig<I, O>\n ): ToolAction<I, O> {\n return defineInterrupt(this.registry, config);\n }\n\n /**\n * Starts a generate operation for long running generation models, typically for\n * video and complex audio generation.\n *\n * See {@link GenerateOptions} for detailed information about available options.\n *\n * ```ts\n * const operation = await ai.generateOperation({\n * model: googleAI.model('veo-2.0-generate-001'),\n * prompt: 'A banana riding a bicycle.',\n * });\n * ```\n *\n * The status of the operation and final result can be obtained using {@link Genkit.checkOperation}.\n */\n generateOperation<\n O extends z.ZodTypeAny = z.ZodTypeAny,\n CustomOptions extends z.ZodTypeAny = typeof GenerationCommonConfigSchema,\n >(\n opts:\n | GenerateOptions<O, CustomOptions>\n | PromiseLike<GenerateOptions<O, CustomOptions>>\n ): Promise<Operation<GenerateResponseData>> {\n return generateOperation(this.registry, opts);\n }\n\n /**\n * Defines a resource. Resources can then be accessed from a generate call.\n *\n * ```ts\n * ai.defineResource({\n * uri: 'my://resource/{param}',\n * description: 'provides my resource',\n * }, async ({param}) => {\n * return [{ text: `resource ${param}` }]\n * });\n *\n * await ai.generate({\n * prompt: [{ resource: 'my://resource/value' }]\n * })\n * ```\n */\n defineResource(opts: ResourceOptions, fn: ResourceFn): ResourceAction {\n return defineResource(this.registry, opts, fn);\n }\n}\n"],"mappings":"AAgBA;AAAA,EAIE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAYK;AAEP,SAAS,oBAAoB;AAU7B;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAEP,SAAS,YAAY,YAAY;AAGjC,SAAS,cAAkC;AAmCpC,SAAS,OAAO,SAAoC;AACzD,SAAO,IAAI,WAAW,OAAO;AAC/B;AAOO,MAAM,mBAAmB,OAAO;AAAA,EACrC,YAAY,SAAyB;AACnC,UAAM,OAAO;AACb,SAAK,SAAS,eAAe;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,kBACE,QAMA,IACA;AACA,WAAO,kBAAyB,KAAK,UAAU,QAAQ,EAAE;AAAA,EAC3D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,kBAGE,QASC;AACD,WAAO,kBAA4B,KAAK,UAAU,MAAM;AAAA,EAC1D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBA,YACE,QACA;AACA,WAAO,YAAsB,KAAK,UAAU,MAAM;AAAA,EACpD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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