UNPKG

@genkit-ai/ai

Version:

Genkit AI framework generative AI APIs.

159 lines 4.27 kB
import { action, GenkitError, isAction, z } from "@genkit-ai/core"; import uriTemplate from "uri-templates"; import { PartSchema } from "./model-types.js"; const ResourceInputSchema = z.object({ uri: z.string() }); const ResourceOutputSchema = z.object({ content: z.array(PartSchema) }); async function resolveResources(registry, resources) { if (!resources || resources.length === 0) { return []; } return await Promise.all( resources.map(async (ref) => { if (typeof ref === "string") { return await lookupResourceByName(registry, ref); } else if (isAction(ref)) { return ref; } throw new Error("Resources must be strings, or actions"); }) ); } async function lookupResourceByName(registry, name) { const resource2 = await registry.lookupAction(name) || await registry.lookupAction(`/resource/${name}`) || await registry.lookupAction(`/dynamic-action-provider/${name}`); if (!resource2) { throw new Error(`Resource ${name} not found`); } return resource2; } function defineResource(registry, opts, fn) { const action2 = dynamicResource(opts, fn); action2.matches = createMatcher(opts.uri, opts.template); action2.__action.metadata.dynamic = false; registry.registerAction("resource", action2); return action2; } async function findMatchingResource(registry, resources, input) { for (const res of resources) { if (res.matches(input)) { return res; } } for (const registryKey of Object.keys( await registry.listResolvableActions() )) { if (registryKey.startsWith("/resource/")) { const resource2 = await registry.lookupAction( registryKey ); if (resource2.matches(input)) { return resource2; } } } return void 0; } function isDynamicResourceAction(t) { return isAction(t) && t.__action?.metadata?.dynamic === true; } function resource(opts, fn) { return dynamicResource(opts, fn); } function dynamicResource(opts, fn) { const uri = opts.uri ?? opts.template; if (!uri) { throw new GenkitError({ status: "INVALID_ARGUMENT", message: `must specify either url or template options` }); } const matcher = createMatcher(opts.uri, opts.template); const act = action( { actionType: "resource", name: opts.name ?? uri, description: opts.description, inputSchema: ResourceInputSchema, outputSchema: ResourceOutputSchema, metadata: { resource: { uri: opts.uri, template: opts.template }, ...opts.metadata, type: "resource", dynamic: true } }, async (input, ctx) => { const templateMatch = matcher(input); if (!templateMatch) { throw new GenkitError({ status: "INVALID_ARGUMENT", message: `input ${input} did not match template ${uri}` }); } const parts = await fn(input, ctx); parts.content.map((p) => { if (!p.metadata) { p.metadata = {}; } if (p.metadata?.resource) { if (!p.metadata.resource.parent) { p.metadata.resource.parent = { uri: input.uri }; if (opts.template) { p.metadata.resource.parent.template = opts.template; } } } else { p.metadata.resource = { uri: input.uri }; if (opts.template) { p.metadata.resource.template = opts.template; } } return p; }); return parts; } ); act.matches = matcher; act.attach = (_) => act; return act; } function createMatcher(uriOpt, templateOpt) { if (uriOpt) { return (input) => input.uri === uriOpt; } if (templateOpt) { const template = uriTemplate(templateOpt); return (input) => template.fromUri(input.uri) !== void 0; } throw new GenkitError({ status: "INVALID_ARGUMENT", message: "must specify either url or template options" }); } export { ResourceInputSchema, ResourceOutputSchema, defineResource, dynamicResource, findMatchingResource, isDynamicResourceAction, lookupResourceByName, resolveResources, resource }; //# sourceMappingURL=resource.mjs.map