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.

referencesagents-custom.md

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

Advanced Custom Agents — defineCustomAgent

ai.defineCustomAgent is experimental and comes from package:genkit/experimental.dart. Read agents.md and agent state first.

defineAgent runs a single prompt + tool loop. When you need full control of the turn — multiple sequential model calls, custom logic between them, manual message/state management, or custom progress streaming — use ai.defineCustomAgent. You provide the handler that runs the turn.


When to use it

Reach for defineCustomAgent when a turn needs to:

  • make multiple model calls with your own orchestration between them;
  • run multi-step workflows (decompose → research → synthesize);
  • manually manage messages and custom state;
  • stream custom status updates to the client mid-turn.

Otherwise prefer defineAgent (simpler; custom state still works — see agent state).

Signature

Agent<State> defineCustomAgent<State>({
  required String name,
  String? description,
  SchemanticType<State>? stateSchema,
  SessionStore? store,
  ClientTransform? clientTransform,
  required AgentFn fn, // (SessionRunner sess, AgentFnOptions options) => Future<AgentResult>
});

The handler receives a session runner sess and AgentFnOptions options (options.sendChunk(...) streams chunks to the client; options.context holds the ambient per-turn context — e.g. auth, derived server-side for remote agents). It returns an AgentResult, typically carrying the final message for the turn (AgentResult.message is nullable — pass message: null when the turn produced no message).

Key sess methods:

  • sess.run((input, ctx) async {...}) — runs the turn; adds input.message (the incoming user message) to the session before calling your callback, so sess.getMessages() includes it. input.message?.content holds the parts.
  • sess.getMessages() — the full message history.
  • sess.addMessages([...]) — append messages (e.g. your final model response).
  • sess.updateCustom(mutator) / sess.getCustom() — read and mutate typed custom state directly on the runner (no ai.currentSession() needed). The mutator is (State? state) => State; each call auto-emits a customPatch chunk. Its typing follows stateSchema — with the map schema below the state is Map<String, dynamic>?; with a generated .$schema it's your typed class.

Example: multi-step research agent

import 'package:genkit/genkit.dart';
import 'package:genkit/experimental.dart'; // defineCustomAgent, SessionRunner
import 'package:schemantic/schemantic.dart';

import 'genkit.dart';

Map<String, dynamic> _state(Map<String, dynamic>? custom) {
  if (custom == null) {
    return {'subQuestions': <dynamic>[], 'subAnswers': <dynamic>[]};
  }
  return custom;
}

final researchAgent = ai.defineCustomAgent(
  name: 'researchAgent',
  stateSchema: .map(.string(), .dynamicSchema()),
  fn: (sess, options) async {
    Message? lastMessage;

    await sess.run((input, ctx) async {
      final userText = input.message?.content.firstOrNull?.text ?? '';

      // Step 1: decompose (a fast model). Mutating custom state auto-emits a
      // `customPatch` chunk so the client's tracked state stays live.
      sess.updateCustom((s) {
        final state = _state(s);
        state['status'] = 'Decomposing question into sub-topics…';
        return state;
      });

      final decompose = await ai.generate(
        model: liteModel,
        prompt: 'Break this into 2-3 sub-questions. Return ONLY a JSON array '
            'of strings.\nUser question: "$userText"',
        use: [retry()],
        outputFormat: 'json',
        outputSchema: SchemanticType.list(SchemanticType.string()),
      );
      final subQuestions =
          (decompose.output ?? [userText]).map((e) => e.toString()).toList();

      sess.updateCustom((s) {
        final state = _state(s);
        state['subQuestions'] = subQuestions;
        state['subAnswers'] = <dynamic>[];
        return state;
      });

      // Step 2: research each sub-question (main model).
      final subAnswers = <Map<String, dynamic>>[];
      for (var i = 0; i < subQuestions.length; i++) {
        sess.updateCustom((s) {
          final state = _state(s);
          state['status'] =
              'Researching (${i + 1}/${subQuestions.length})';
          return state;
        });
        final research = await ai.generate(
          use: [retry()],
          prompt: 'Answer concisely in 2-3 paragraphs.\n\n'
              'Question: ${subQuestions[i]}',
        );
        subAnswers.add({'question': subQuestions[i], 'answer': research.text});
      }
      sess.updateCustom((s) {
        final state = _state(s);
        state['subAnswers'] = subAnswers;
        return state;
      });

      // Step 3: synthesize and STREAM the final answer to the client.
      sess.updateCustom((s) {
        final state = _state(s);
        state['status'] = 'Synthesizing final response…';
        return state;
      });

      final synthesis = ai.generateStream(
        use: [retry()],
        prompt: 'Synthesize a unified markdown answer to "$userText" from:\n'
            '${subAnswers.map((a) => '${a['question']}: ${a['answer']}').join('\n\n')}',
      );
      await for (final chunk in synthesis) {
        options.sendChunk(AgentStreamChunk(modelChunk: chunk.modelChunk));
      }
      final finalResponse = await synthesis.onResult;
      lastMessage = finalResponse.message;
      if (lastMessage != null) sess.addMessages([lastMessage!]);

      sess.updateCustom((s) {
        final state = _state(s);
        state['status'] = 'Done';
        return state;
      });

      return null;
    });

    return AgentResult(
      message: lastMessage ??
          Message(
            role: Role.model,
            content: [TextPart(text: 'Research complete.')],
          ),
    );
  },
);

Custom status streaming

Calling sess.updateCustom(...) during the turn automatically emits a customPatch chunk, so the remoteAgent client's tracked custom state (e.g. the status field) stays live mid-stream without any extra wiring. Stream model output separately with options.sendChunk(AgentStreamChunk(modelChunk: ...)).

Seed and run a custom agent exactly like a regular one:

final chat = researchAgent.chat(
  state: SessionState(
    custom: {'subQuestions': <dynamic>[], 'subAnswers': <dynamic>[]},
    messages: [],
    artifacts: [],
  ),
);
final turn = chat.sendStream(text: 'Impacts of electric vehicles?');
await for (final chunk in turn.stream) {
  // chunk.text for model output; chunk.custom['status'] for live progress
}
await turn.response;

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