Share an RPC method
Let your plugin's browser part, a terminal or another plugin call methods on its engine part, and follow its events.
An RPC method is a function your engine part answers. Anything else calls it by name: your browser part, a terminal,
or another plugin. This guide builds a plant shelf, with a count method and a sell method that can fail.
The contract file imports @conciv/protocol, which the tutorial installed only for the Effect style.
npm install -D @conciv/protocolpnpm add -D @conciv/protocolyarn add --dev @conciv/protocolbun add --dev @conciv/protocolCopy this prompt into your agent. It builds this in your project from this page.
Build this in my project with conciv. conciv is a dev-only AI agent widget, and it already runs in my app. 1. Read https://conciv.dev/docs/plugins/guides/rpc.md and follow it. It is the source of truth for the API. 2. If the project has the conciv-develop skill (conciv init installs it with the @conciv/skills docs pack), load it too. 3. Share an RPC method between my plugin's engine part and its browser part: the contract in rpc.ts, the method in server.ts, and the call in the browser part. 4. If I did not say what it should do, ask me before you write code. 5. Write the browser part in the UI framework my app uses. For React or Preact, put it in my app with @conciv/react or @conciv/preact and give it to <ConcivWidget>, as the page shows. For anything else, use Solid in the plugin folder and build it as the page shows. 6. Install only the packages the page lists, with the package manager this project uses. 7. While the dev server runs, call the method with `npx @conciv/cli plugin call <plugin> <method> --data <json>` and show me the output. 8. When you are done, tell me what you changed and how to see it.
Declare the contract
Declare the contract in its own file, so the engine part and the browser part both import it:
import * as from '@conciv/protocol/schema/rpc'
import {} from 'zod'
const = .({: .()})
export const = .({: .(), : .()})
export const = .({
: 'plant-shelf', // <- the name callers use
: {
: {: , : },
: {: , : , : {'sold-out': }}, // <- a failure callers can tell apart
},
: {: {: }}, // <- sent to everyone listening
})The contract imports Rpc from @conciv/protocol/schema/rpc because the widget provides that module to browser
parts. Then answer every method in the engine part:
import {Plugin, type } from '@conciv/backend-plugin-api'
import {} from './rpc'
export default Plugin.({
: 'plant-shelf',
async () {
let = 3
const : <typeof > = await ..(, {
: async ({}) => ({, }),
: async ({}, ) => {
if ( === 0) return .('sold-out', `${} is sold out`, {}) // <- a declared error
-= 1
await .
.('sold', {, })
.((: unknown)
return {, }
},
})
},
})import {, } from 'effect'
import {Plugin} from '@conciv/backend-plugin-api/effect'
import
A few things to notice:
- Each handler gets the checked input. What it returns is checked against
outputbefore it goes back. call.errormakes one of the method's declared errors: its name, a message and its data.- In both tabs the plugin logs a failed
emit. The sale still succeeds. Effect.catchCausecatches any failure of the emit.Effect.orDiemakes a failedregisterstop the plugin load, as a rejectedregisterdoes in the Promise tab.shelfhas its type written out, because a handler uses the registration it belongs to.
Call it from the browser part
ctx.client.rpc(Shelf) gives one typed function per method. You write the browser part in Solid, React, Preact or
plain DOM. This code draws nothing, so it is the same in every tab. Only where it lives changes:
import {, Plugin} from '@conciv/frontend-plugin-api'
import {
The code lives in your app. Give it to <ConcivWidget> as in
Build your first plugin.
import {
The code lives in your app. Give it to <ConcivWidget> as in
Build your first plugin.
import {
import {
Read ctx.client where you call it, because the widget makes a new client after the engine restarts. Build or wire it
as in Build or wire the browser part, then reload.
Follow the events
Subscribe to an event by its full name, rpc.<contract id>.<event>:
import {Plugin} from '@conciv/frontend-plugin-api'
import {} from './rpc'
export default Plugin.
import type {Plugin} from '@conciv/frontend-plugin-api'
import {
import type {Plugin} from '@conciv/frontend-plugin-api'
import {
import {Plugin} from '@conciv/frontend-plugin-api'
import {
A few things to notice:
subscribealways letsserver.connectedthrough, at the start and after each reconnect. Read your data again then. You can have missed events in between.event.datais what the engine part sent. Check it with your schema before you use it.ctx.signalends the loop when the plugin reloads or stops.
Build or wire it as in Build or wire the browser part, then reload.
Call it from a terminal
The same methods answer from your shell, while the dev server runs:
npx @conciv/cli plugin call plant-shelf count --data '{"plant": "fern"}'{"ok": true, "data": {"plant": "fern", "count": 3}}Learn more in Call your plugin from a terminal.
Next steps
- Add a panel: show the data in a tab next to the chat.
- Call your plugin from a terminal: every flag and every error.