packages
Loading
Loading
packages
@loop-engine/adapter-gemini wraps Google's Gemini API as a Loop Engine AI actor using the @google/generative-ai SDK. Unlike OpenAI-compatible adapters, Gemini uses Google's native SDK and response shape. The adapter applies the same governance model as other Loop Engine AI adapters: guard enforcement in runtime and consistent actor audit fields.
1npm install @loop-engine/adapter-gemini @google/generative-ai1@google/generative-ai ^0.21.01"cmt">// @no-typecheck2import { createGeminiActorAdapter } from '@loop-engine/adapter-gemini'3 4const adapter = createGeminiActorAdapter(process.env.GOOGLE_AI_API_KEY!, {5 modelId: 'gemini-1.5-pro',6 confidenceThreshold: 0.75,7})8 9const { actor, decision } = await adapter.createSubmission({10 loopId: 'procurement',11 loopName: 'SCM Procurement',12 currentState: 'pending_analysis',13 availableSignals: [14 {15 signalId: 'submit_recommendation',16 name: 'Submit Recommendation',17 allowedActors: ['ai-agent'],18 },19 ],20 instruction: 'Analyze demand data and recommend a purchase order decision.',21 evidence: { demandForecast: 0.87, currentStock: 45 },22})| Option | Type | Default | Description |
|--------|------|---------|-------------|
| modelId | string | gemini-1.5-pro | Gemini model (gemini-1.5-pro, gemini-1.5-flash, gemini-2.0-flash) |
| maxOutputTokens | number | 1024 | Max tokens in response |
| systemPrompt | string | — | Prepended to the system instruction |
| confidenceThreshold | number | 0.7 | Minimum confidence required (0-1) |
The adapter returns an AIAgentActor with:
type: "ai-agent"provider: "gemini"modelId — the model usedconfidence — extracted from the model responsepromptHash — SHA-256 of the prompt sent (for audit trail)Gemini 1.5 occasionally wraps JSON responses in markdown code fences despite system instructions. The adapter strips these automatically before parsing. This cleanup becomes unnecessary in a later version that switches to responseMimeType: "application/json".
The confidence guard runs at the runtime level — not inside the adapter. If you configure a confidence-threshold guard on the transition, the runtime will block the transition if the model's confidence falls below the threshold regardless of what the adapter returns.
1"cmt">// In your loop definition — this guard is structural, not prompt-based2{3 guardId: 'confidence-threshold',4 severity: 'hard',5 evaluatedBy: 'runtime',6 parameters: { threshold: 0.75 },7}The adapter throws ActorDecisionError with one of these codes:
INVALID_SIGNAL — model returned a signalId not in availableSignalsINVALID_CONFIDENCE — confidence value outside 0–1 rangePARSE_FAILED — model response could not be parsed as JSONAPI_ERROR — Gemini API returned an error (message prefixed with [loop-engine/adapter-gemini])All four adapters (adapter-anthropic, adapter-openai, adapter-grok, and adapter-gemini) return the same AIAgentActor shape. You can use different providers for different transitions in the same loop, while provider and modelId distinguish them in the audit trail.