Skip to content

Commit a192ab7

Browse files
Merge pull request #96 from restackio/examplesAgent
Refactor agent examples
2 parents 2595104 + 88daecc commit a192ab7

File tree

13 files changed

+57
-43
lines changed

13 files changed

+57
-43
lines changed

agent-chat/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"event-agent": "tsx eventAgent.ts"
1111
},
1212
"dependencies": {
13-
"@restackio/ai": "^0.0.103",
13+
"@restackio/ai": "^0.0.104",
1414
"@temporalio/workflow": "1.11.6",
1515
"openai": "^4.80.1"
1616
},

child-workflows/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
"restack-web-ui": "docker run -d --pull always --name restack -p 5233:5233 -p 6233:6233 -p 7233:7233 ghcr.io/restackio/restack:main"
1010
},
1111
"dependencies": {
12-
"@restackio/ai": "^0.0.103",
12+
"@restackio/ai": "^0.0.104",
1313
"@temporalio/workflow": "1.11.6",
1414
"dotenv": "16.4.5",
1515
"zod": "3.23.8",
1616
"zod-to-json-schema": "3.23.3"
1717
},
1818
"devDependencies": {
1919
"@types/node": "20.16.9",
20-
"dotenv-cli": "^7.4.2",
20+
"dotenv-cli": "^7.4.4",
2121
"prettier": "3.3.3",
2222
"tsx": "4.19.2",
2323
"typescript": "5.6.3"

child-workflows/src/client.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ export const connectionOptions = {
66
engineId: process.env.RESTACK_ENGINE_ID!,
77
address: process.env.RESTACK_ENGINE_ADDRESS!,
88
apiKey: process.env.RESTACK_ENGINE_API_KEY!,
9+
apiAddress: process.env.RESTACK_ENGINE_API_ADDRESS!,
910
};
1011

1112
export const client = new Restack(
12-
process.env.RESTACK_ENGINE_API_KEY ? connectionOptions : undefined
13+
process.env.RESTACK_ENGINE_API_KEY ? connectionOptions : undefined,
1314
);

child-workflows/src/workflows/parent.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import { startChild, executeChild } from "@restackio/ai/workflow";
1+
import { childStart, childExecute } from "@restackio/ai/workflow";
22
import { childWorkflow } from "./child";
33

44
interface Input {
55
name: string;
66
}
77
export async function parentWorkflow({ name }: Input) {
8-
const startedChild = await startChild(childWorkflow, {
9-
workflowId: `startChild-workflow`,
10-
args: [{ name }],
8+
const startedChild = await childStart({
9+
child: childWorkflow,
10+
childId: "startChild-workflow",
11+
input: { name },
1112
});
1213

13-
const executedChild = await executeChild(childWorkflow, {
14-
workflowId: `executeChild-workflow`,
15-
args: [{ name }],
14+
const executedChild = await childExecute({
15+
child: childWorkflow,
16+
childId: "executeChild-workflow",
17+
input: { name },
1618
});
1719

1820
return {

human-loop/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"dev": "pnpm start.watch",
1010
"build": "tsc --build",
1111
"clean": "rm -rf node_modules",
12-
"schedule": "ts-node ./scheduleWorkflow.ts"
12+
"schedule": "ts-node ./schedule.ts"
1313
},
1414
"nodemonConfig": {
1515
"execMap": {
@@ -21,7 +21,7 @@
2121
]
2222
},
2323
"dependencies": {
24-
"@restackio/ai": "^0.0.85",
24+
"@restackio/ai": "^0.0.104",
2525
"@temporalio/workflow": "^1.11.6"
2626
},
2727
"devDependencies": {

human-loop/readme.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
# Restack AI - Human in the loop example
1+
# Restack AI - Agent with Human in the loop
22

33
This example will illustrate how you can handle events that will receive feedback as input and have a function that will acknowledge the feedback was received.
4-
It will also showcase how a workflow can be left running indefinitely until a condition is met by the usage of the `condition`.
4+
It will also showcase how an agent is left running indefinitely until a condition is met by the usage of the `condition`.
55

6-
Idea is the humanLoop workflow will have two events:
6+
The Agent with Human in the loop will have two events:
77

88
1. feedback. This event will be triggered whenever you want to send feedback to your desired AI. For simplicity purposes, the function called on this event will just reply with a message that the feedback was received.
9-
2. end. This event will be triggered when you are done with interactions and you want your workflow to be done and exit. Right now it sets the `endWorkflow` local variable on the workflow to true, which will make the `condition` set to resolve successfully and exit the workflow. You can use this example as guidance on how you can keep a workflow running until the end event is sent and how it will handle the events you have defined for the time it is running.
9+
10+
2. end. This event will be triggered when you are done with interactions and you want your agent to be done and exit. Right now it sets the `endEvent` local variable on the agent to true, which will make the `condition` set to resolve successfully and exit the agent. You can use this example as guidance on how you can keep an agent running until the end event is sent and how it will handle the events you have defined for the time it is running.
1011

1112
# Requirements
1213

@@ -29,25 +30,24 @@ docker run -d --pull always --name restack -p 5233:5233 -p 6233:6233 -p 7233:723
2930

3031
# Start services
3132

32-
Where all your code is defined, including workflow steps.
33+
Where all your code is defined, including agent steps.
3334

3435
```bash
3536
pnpm i
3637
pnpm dev
3738
```
3839

39-
Your code will be running and syncing with Restack engine to execute workflows or functions.
40+
Your code will be running and syncing with Restack engine to execute agents or functions.
4041

41-
# Schedule a workflow
42+
# Schedule an agent
4243

4344
In another shell run following command:
4445

4546
```bash
4647
pnpm schedule
4748
```
4849

49-
Will schedule to start example workflow immediately. This runs the `scheduleWorkflow` file.
50-
50+
Will schedule to start example agent immediately. This runs the `scheduleAgent` file.
5151

5252
## Deploy on Restack Cloud
5353

human-loop/scheduleWorkflow.ts human-loop/schedule.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import { client } from "./src/client";
22

33
import { endEvent, feedbackEvent } from "./src/events";
44

5-
async function scheduleWorkflow() {
5+
async function scheduleAgent() {
66
try {
7-
const workflowId = `${Date.now()}-HumanLoopWorkflow`;
8-
const runId = await client.scheduleWorkflow({
9-
workflowName: "humanLoopWorkflow",
10-
workflowId,
7+
const agentId = `${Date.now()}-HumanLoopAgent`;
8+
const runId = await client.scheduleAgent({
9+
agentName: "humanLoopAgent",
10+
agentId,
1111
});
1212

13-
const feedback = await client.sendWorkflowEvent({
14-
workflow: {
15-
workflowId,
13+
const feedback = await client.sendAgentEvent({
14+
agent: {
15+
agentId,
1616
runId,
1717
},
1818
event: {
@@ -23,9 +23,9 @@ async function scheduleWorkflow() {
2323

2424
console.log("Feedback:", feedback);
2525

26-
const end = await client.sendWorkflowEvent({
27-
workflow: {
28-
workflowId,
26+
const end = await client.sendAgentEvent({
27+
agent: {
28+
agentId,
2929
runId,
3030
},
3131
event: { name: endEvent.name, input: { end: true } },
@@ -35,9 +35,9 @@ async function scheduleWorkflow() {
3535

3636
process.exit(0); // Exit the process successfully
3737
} catch (error) {
38-
console.error("Error scheduling workflow:", error);
38+
console.error("Error scheduling agent:", error);
3939
process.exit(1); // Exit the process with an error code
4040
}
4141
}
4242

43-
scheduleWorkflow();
43+
scheduleAgent();

human-loop/src/workflows/humanLoop.ts human-loop/src/agents/humanLoop.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { log, step, condition } from "@restackio/ai/workflow";
2-
import { onEvent } from "@restackio/ai/event";
1+
import { log, step, condition } from "@restackio/ai/agent";
2+
import { onEvent } from "@restackio/ai/agent";
33

44
import { feedbackEvent, FeedbackEvent, endEvent, EndEvent } from "../events";
55
import * as functions from "../functions";
66

7-
export async function humanLoopWorkflow() {
7+
export async function humanLoopAgent() {
88
let endWorkflow = false;
99

1010
onEvent(feedbackEvent, async (event: FeedbackEvent) => {
File renamed without changes.

human-loop/src/client.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
import Restack from "@restackio/ai";
22

3-
export const client = new Restack();
3+
import "dotenv/config";
4+
5+
export const connectionOptions = {
6+
engineId: process.env.RESTACK_ENGINE_ID!,
7+
address: process.env.RESTACK_ENGINE_ADDRESS!,
8+
apiKey: process.env.RESTACK_ENGINE_API_KEY!,
9+
apiAddress: process.env.RESTACK_ENGINE_API_ADDRESS!,
10+
};
11+
12+
export const client = new Restack(
13+
process.env.RESTACK_ENGINE_API_KEY ? connectionOptions : undefined
14+
);

human-loop/src/events/endEvent.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineEvent } from "@restackio/ai/event";
1+
import { defineEvent } from "@restackio/ai/agent";
22

33
export type EndEvent = {
44
end: boolean;

human-loop/src/events/feedbackEvent.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineEvent } from "@restackio/ai/event";
1+
import { defineEvent } from "@restackio/ai/agent";
22

33
export type FeedbackEvent = {
44
feedback: string;

human-loop/src/services.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { feedback, goodbye } from "./functions";
22
import { client } from "./client";
33

44
async function services() {
5-
const workflowsPath = require.resolve("./workflows");
5+
const agentsPath = require.resolve("./agents");
66
try {
77
await Promise.all([
88
// Start service with current workflows and functions
99
client.startService({
10-
workflowsPath,
10+
agentsPath,
1111
functions: { feedback, goodbye },
1212
}),
1313
]);

0 commit comments

Comments
 (0)