Skip to content

Commit 0535642

Browse files
committed
Major plugins integration debugging
1 parent 88b2ee5 commit 0535642

1 file changed

Lines changed: 125 additions & 6 deletions

File tree

  • packages/usdk/packages/upstreet-agent/packages/react-agents/components/plugins
Lines changed: 125 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,132 @@
1+
import React from 'react';
2+
import { Action, useEnv } from 'react-agents';
3+
import { z } from 'zod';
14
import { ThreeDGenerationPlugin } from './plugin-3d-generation/src/index.ts';
5+
import util from 'util';
26

3-
// XXX finish this
4-
type IPlugin = {};
57

6-
const pluginWrap = (IPlugin: any) => (props: any) => {
7-
console.log('render plugin', ThreeDGenerationPlugin);
8-
return null;
8+
function generateZodSchema(obj: any): z.ZodTypeAny {
9+
if (typeof obj === "string") return z.string();
10+
if (typeof obj === "number") return z.number();
11+
if (typeof obj === "boolean") return z.boolean();
12+
if (Array.isArray(obj)) {
13+
return z.array(generateZodSchema(obj[0] || z.any()));
14+
}
15+
if (typeof obj === "object" && obj !== null) {
16+
const shape: Record<string, z.ZodTypeAny> = {};
17+
for (const key in obj) {
18+
shape[key] = generateZodSchema(obj[key]);
19+
}
20+
return z.object(shape);
21+
}
22+
return z.any();
23+
}
24+
25+
type IActionHandlerAttachment = {
26+
id: string;
27+
url: string;
28+
// title: "Generated 3D",
29+
// source: "ThreeDGeneration",
30+
// description: ThreeDPrompt,
31+
// text: ThreeDPrompt,
32+
};
33+
type IActionHandlerCallbackArgs = {
34+
text: string;
35+
error?: boolean;
36+
attachments?: IActionHandlerAttachment[],
37+
};
38+
type IActionHandler = (
39+
runtime: any,
40+
message: {
41+
content: any;
42+
},
43+
state: any,
44+
options: any,
45+
callback: (result: IActionHandlerCallbackArgs) => void,
46+
) => void;
47+
type IAction = {
48+
name: string;
49+
// similies?: string[];
50+
description: string;
51+
examples: any[];
52+
handler: IActionHandler;
53+
};
54+
type IPlugin = {
55+
actions: IAction[];
56+
};
57+
58+
const pluginWrap = (plugin: IPlugin) => (props: any) => {
59+
console.log('render plugin', util.inspect(plugin, {
60+
depth: 7,
61+
}));
62+
const env = useEnv();
63+
return (
64+
<>
65+
{(plugin.actions ?? []).map((action: any) => {
66+
const examples = action.examples.map(exampleMessages => {
67+
const agentMessages = exampleMessages.filter(message => {
68+
return /agentName/.test(message.user);
69+
});
70+
if (agentMessages.length > 0) {
71+
const agentMessage = agentMessages[0];
72+
const {
73+
action,
74+
...args
75+
} = agentMessage.content;
76+
return args;
77+
} else {
78+
return null;
79+
}
80+
}).filter(Boolean);
81+
console.log('got examples', examples);
82+
if (examples.length > 0) {
83+
const schema = generateZodSchema(examples[0]);
84+
// console.log('got schema', schema);
85+
return (
86+
<Action
87+
type={action.name}
88+
description={action.description}
89+
schema={schema}
90+
examples={examples}
91+
handler={async e => {
92+
const { args } = e.data.message;
93+
console.log('got handler', args);
94+
95+
await new Promise((resolve, reject) => {
96+
const runtime = {
97+
getSetting(key: string) {
98+
return env[key];
99+
},
100+
};
101+
const message = {
102+
content: args,
103+
}
104+
const state = {};
105+
const options = {};
106+
const callback = (result: IActionHandlerCallbackArgs) => {
107+
console.log('got callback result', result);
108+
const {
109+
text,
110+
error,
111+
attachments,
112+
} = result;
113+
resolve(null);
114+
};
115+
console.log('call action handler', action.handler);
116+
action.handler(runtime, message, state, options, callback);
117+
});
118+
}}
119+
key={action.name}
120+
/>
121+
);
122+
} else {
123+
return null;
124+
}
125+
}).filter(Boolean)}
126+
</>
127+
);
9128
};
10129

11130
export const plugins = {
12-
plugin3dGeneration: pluginWrap(ThreeDGenerationPlugin),
131+
'@elizaos/plugin-3d-generation': pluginWrap(ThreeDGenerationPlugin),
13132
};

0 commit comments

Comments
 (0)