Loading...
Loading...
This skill should be used when working with Restate durable execution framework, building workflows, virtual objects, or services that need automatic retries, state management, and fault tolerance. Use when the user mentions "restate", "durable execution", "durable workflows", needs to build resilient distributed systems, or when they invoke /restate.
npx skill4agent add schpet/toolbox restaterunctx.run()ctx.get/set/clearctx.sleep()ctx.serviceClient()npm install @restatedev/restate-sdkimport * as restate from "@restatedev/restate-sdk";
const myService = restate.service({
name: "MyService",
handlers: {
greet: async (ctx: restate.Context, name: string) => {
return "Hello, " + name + "!";
},
},
});
restate.endpoint().bind(myService).listen(9080);const counter = restate.object({
name: "Counter",
handlers: {
add: async (ctx: restate.ObjectContext, value: number) => {
const current = (await ctx.get<number>("count")) ?? 0;
ctx.set("count", current + value);
return current + value;
},
get: restate.handlers.object.shared(
async (ctx: restate.ObjectSharedContext) => {
return (await ctx.get<number>("count")) ?? 0;
}
),
},
});const paymentWorkflow = restate.workflow({
name: "PaymentWorkflow",
handlers: {
run: async (ctx: restate.WorkflowContext, payment: Payment) => {
// Step 1: Reserve funds
const reservation = await ctx.run("reserve", () =>
reserveFunds(payment)
);
// Step 2: Wait for approval (awakeable)
const approved = await ctx.promise<boolean>("approval");
if (!approved) {
await ctx.run("cancel", () => cancelReservation(reservation));
return { status: "cancelled" };
}
// Step 3: Complete payment
await ctx.run("complete", () => completePayment(reservation));
return { status: "completed" };
},
approve: async (ctx: restate.WorkflowSharedContext) => {
ctx.promise<boolean>("approval").resolve(true);
},
reject: async (ctx: restate.WorkflowSharedContext) => {
ctx.promise<boolean>("approval").resolve(false);
},
},
});// Journaled action - result persisted, replayed on retry
const result = await ctx.run("action-name", async () => {
return await callExternalApi();
});
// Durable timer - survives restarts
await ctx.sleep(60_000); // 60 seconds
// Call another service
const client = ctx.serviceClient(OtherService);
const response = await client.handler(input);
// Async call (fire and forget)
ctx.serviceSendClient(OtherService).handler(input);
// Delayed call
ctx.serviceSendClient(OtherService, { delay: 60_000 }).handler(input);
// Awakeable - wait for external signal
const { id, promise } = ctx.awakeable<string>();
// Give `id` to external system, then:
const result = await promise;
// Random (deterministic)
const value = ctx.rand.random();
const uuid = ctx.rand.uuidv4();npx @restatedev/restate-servernpx ts-node src/app.tsnpx @restatedev/restate deployments register http://localhost:9080# Service handler
curl localhost:8080/MyService/greet -H 'content-type: application/json' -d '"World"'
# Virtual Object handler (with key)
curl localhost:8080/Counter/user123/add -H 'content-type: application/json' -d '5'
# Start workflow
curl localhost:8080/PaymentWorkflow/order-456/run -H 'content-type: application/json' -d '{"amount": 100}'ctx.run()ctx.run()ctx.randrunrestate deployments registerscripts/sync-docs.shreferences/