Index of every page on this site: llms.txt
All skills
genkit-ai avatar

/developing-genkit-dart

@d12c988
by Genkitgenkit-ai/skills40 stars
13
Run it yourself

Provides guidance on developing AI-powered applications using the Genkit Dart SDK. Use when the user asks to build AI agents in Dart, use Genkit flows, or integrate LLMs into Dart/Flutter applications. Don't use for Genkit in JavaScript/TypeScript, Go, or Python.

Use this Skill: https://skilld.dev/gh/genkit-ai/skills/developing-genkit-dart

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesgenkit_shelf.md

≈1.2k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Serving over HTTP (GenkitRouter, genkit_shelf)

HTTP serving lives in core package:genkit/io.dart (GenkitRouter, plain dart:io, no web framework). genkit_shelf is only an adapter that mounts a GenkitRouter into a shelf app.

There is no startFlowServer and no FlowWithContextProvider. Do not add shelf_cors_headers; CORS is CorsOptions.


Standalone server (no shelf)

import 'package:genkit/genkit.dart';
import 'package:genkit/io.dart';
import 'package:genkit_google_genai/genkit_google_genai.dart';

void main() async {
  final ai = Genkit(plugins: [googleAI()]);

  final flow = ai.defineFlow(
    name: 'myFlow',
    inputSchema: .string(),
    outputSchema: .string(),
    fn: (String input, _) async => 'Hello $input',
  );

  // Any action can be served, including models (here for remote clients
  // using defineRemoteModel).
  final geminiFlash = googleAI().model('gemini-flash-latest');

  final genkit = GenkitRouter()
    ..addAction(flow) // POST /myFlow (stream with ?stream=true)
    ..addAction(geminiFlash, path: '/v1/gemini');

  await genkit.serve(
    port: 8080, // default: $PORT, then 3400. Host defaults to 0.0.0.0.
    cors: const CorsOptions(allowedOrigins: ['https://myapp.dev']), // default: no CORS
  );
}

addAction returns void; use cascades (..), not method chaining. serve logs its address via package:logging (Logger.root.onRecord.listen(print) to see it).

Existing shelf app

import 'dart:io';

import 'package:genkit/io.dart';
import 'package:genkit_shelf/genkit_shelf.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_router/shelf_router.dart';

void main() async {
  // `flow`, `geminiFlash`: as above. `bearerAuth`: see Auth below.
  final genkit = GenkitRouter()
    ..addAction(flow) // served at /api/myFlow below
    ..addAction(geminiFlash, contextProvider: bearerAuth);

  final app = Router()
    ..get('/health', (Request request) => Response.ok('OK'))
    ..mount('/api/', genkit.asShelfHandler(cors: const CorsOptions()));

  await io.serve(
    const Pipeline().addMiddleware(logRequests()).addHandler(app.call),
    InternetAddress.anyIPv4,
    8080,
  );
}

Unknown paths get a 404, so it also works in a shelf Cascade. To serve one action on a route of your choosing: router.post('/myFlow', shelfHandler(flow, contextProvider: bearerAuth)).

Own dart:io server

final server = await HttpServer.bind(InternetAddress.anyIPv4, 8080);
await for (final request in server) {
  if (await genkit.handleHttpRequest(request, basePath: '/api')) continue;
  // ... your own routes; or ioHandler(action) for a single action
  request.response
    ..statusCode = HttpStatus.notFound
    ..close();
}

Other frameworks: adapt the framework-neutral GenkitRouter.handle / actionHandler (GenkitHttpRequest -> GenkitHttpResponse), plus withCors.

Auth: contextProvider

A ContextProvider gets a framework-neutral RequestData (lowercased headers, method, parsed input) and returns the action context (ctx.context in the flow). A thrown GenkitException is answered with its status; anything else with 403.

Future<Map<String, dynamic>> bearerAuth(RequestData request) async {
  final user = await checkUserToken(request.headers['authorization']);
  if (user == null) {
    throw GenkitException('Unauthorized', status: StatusCode.unauthenticated); // 401
  }
  return {'userId': user.id};
}

Agents

addAgent (from package:genkit/experimental_io.dart) mounts the turn route plus the /getSnapshot and /abort companions the agent supports. See agents-deployment.md.

CORS

CorsOptions() defaults: any origin, Content-Type + Authorization request headers, exposes x-genkit-trace-id / x-genkit-span-id. Browser streaming clients (JS remoteAgent/streamFlow) also send X-Genkit-Stream-Id, so add it to allowedHeaders for those.

Older clients

Streamed failures end with a data: {"error": ...} frame. Dart/Flutter clients on package:genkit 0.17 or earlier only understand the older frame; while such clients are in the field, use GenkitRouter(sendLegacyErrorFrame: true) (also on shelfHandler/ioHandler).

Consume served endpoints with defineRemoteAction / defineRemoteModel (package:genkit/client.dart), remoteAgent, or the JS client.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 alert2d4 checks · Risk SAFE
  • Gen Agent Trust Hub3d

    The Genkit Dart skill provides comprehensive documentation and examples for building AI agents and flows using the Genkit framework. It includes guidance on using A2UI for interactive interfaces, managing agent sessions, and implementing middleware for filesystem access and multi-agent orchestration. While the skill includes instructions for installing the Genkit CLI from a vendor-controlled domain, its primary security considerations involve the attack surface for indirect prompt injection created by agents that ingest external data while possessing capabilities such as file modification and command execution.

  • Socket2d

    No alerts

  • Snyk3d

    Risk: CRITICAL · 2 issues

  • Runlayer8mo

    2/11 files flagged

Signed by skilld at d12c988. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 days ago.

Activeupdated 5 days ago
metadata
{
  "version": "1.0.0",
  "category": "AiAndMachineLearning"
}

README badge

README badge for genkit-ai/skills/developing-genkit-dart