Skip to content

Commit

Permalink
Update proto, support allow-host(s) param
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Jun 6, 2024
1 parent 3efc6bb commit 2d3191b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 59 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"grpc-tools": "^1.12.4",
"json5": "^2.2.3",
"nostr-tools": "^2.3.2",
"openagents-grpc-proto": "https://github.com/OpenAgentsInc/openagents-grpc-proto/releases/download/v0.9/openagents_grpc_proto-JAVASCRIPT.tgz",
"openagents-grpc-proto": "https://github.com/OpenAgentsInc/openagents-grpc-proto/releases/download/v0.10.0/openagents_grpc_proto-JAVASCRIPT.tgz",
"ts-protoc-gen": "^0.15.0",
"uuidv4": "^6.2.13",
"ws": "^8.16.0"
Expand Down
113 changes: 60 additions & 53 deletions src/runtime/ExtismJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,36 @@ export default class ExtismJob {
looping: boolean = false;
jobId: string;
hostFunctions: { [key: string]: ExtismFunction };
executionQueue: Promise<string|undefined> = Promise.resolve(undefined);
executionQueue: Promise<string | undefined> = Promise.resolve(undefined);
allowedHosts: string[] = ["*"];

constructor(
jobId: string,
mainPluginPath: string,
expiration: number,
hostFunctions: { [key: string]: ExtismFunction }
hostFunctions: { [key: string]: ExtismFunction },
allowedHosts?: string[]
) {
// TODO: check origins
this.mainPluginPath = mainPluginPath;
this.expiration = expiration;
this.jobId = jobId;
this.hostFunctions = hostFunctions;

if(allowedHosts!==undefined) this.allowedHosts = allowedHosts;

// autodestruct
setTimeout(async () => {
await this.destroy();
},1000*60*5);
setTimeout(
async () => {
await this.destroy();
},
1000 * 60 * 5
);
}

async init() {
if (this.initialized) throw new Error("Already initialized");
this.main = await this._loadFragment(this.mainPluginPath);


this.initialized = true;
for (const plugin of [this.main, ...this.dependencies]) {
Expand All @@ -64,7 +70,7 @@ export default class ExtismJob {
functions: {
"extism:host/user": this.hostFunctions,
},
allowedHosts:["*"]
allowedHosts: this.allowedHosts,
};
const plugin = await Extism.createPlugin(path, options);
let meta: any;
Expand All @@ -86,7 +92,7 @@ export default class ExtismJob {
return fragment;
}

async _callPlugin(plugin: Extism.Plugin, method: string, jobId?: string, inputData?: string){
async _callPlugin(plugin: Extism.Plugin, method: string, jobId?: string, inputData?: string) {
return await this._callPluginMaybe(plugin, method, jobId, inputData, true);
}
async _callPluginMaybe(
Expand All @@ -97,41 +103,42 @@ export default class ExtismJob {
required?: boolean
): Promise<string | undefined> {
if (!this.initialized) throw new Error("Not initialized");
const q=this.executionQueue.then(async () => {
try{
const fexists = async (name) => {
return await plugin.functionExists(name);
};
let out: Extism.PluginOutput;
if (jobId && (await fexists(method + "ForJob"))) {
out = await plugin.call(
method + "ForJob",
JSON.stringify({
jobId: jobId,
args: inputData,
})
);
} else if (await fexists(method)) {
out = await plugin.call(method, inputData);
} else {
if (required) {
throw new Error("No method " + method + " found in plugin " + this);
const q = this.executionQueue
.then(async () => {
try {
const fexists = async (name) => {
return await plugin.functionExists(name);
};
let out: Extism.PluginOutput;
if (jobId && (await fexists(method + "ForJob"))) {
out = await plugin.call(
method + "ForJob",
JSON.stringify({
jobId: jobId,
args: inputData,
})
);
} else if (await fexists(method)) {
out = await plugin.call(method, inputData);
} else {
return "";
if (required) {
throw new Error("No method " + method + " found in plugin " + this);
} else {
return "";
}
}
return out ? out.text() : undefined;
} catch (e) {
console.error("Error calling plugin in execution queue", e);
throw e;
}
return out?out.text():undefined;
}catch(e){
console.error("Error calling plugin in execution queue",e);
})
.catch((e) => {
console.error("Error in execution queue", e);
throw e;
}
}).catch((e)=>{
console.error("Error in execution queue",e);
throw e;
});


this.executionQueue= q;
});

this.executionQueue = q;
return await q;
}

Expand All @@ -147,26 +154,26 @@ export default class ExtismJob {

async run(inputData: string): Promise<string | undefined> {
if (!this.initialized) throw new Error("Not initialized");
try{
try {
return await this._callPlugin(this.main.plugin, "run", this.jobId, inputData);
}catch(e){
console.error("Error running plugin",e);
} catch (e) {
console.error("Error running plugin", e);
throw e;
}
}

// async loop() {
// if (!this.initialized) throw new Error("Not initialized");
// if (this.looping) return;
// this.looping = true;
// await this._callPluginMaybe(this.main.plugin, "loop", this.jobId, "{}")
// .finally(() => {
// this.looping = false;
// })
// .catch((e) => {
// console.error(e);
// });
// if (!this.initialized) throw new Error("Not initialized");

// if (this.looping) return;
// this.looping = true;
// await this._callPluginMaybe(this.main.plugin, "loop", this.jobId, "{}")
// .finally(() => {
// this.looping = false;
// })
// .catch((e) => {
// console.error(e);
// });
// }

private destructing: boolean = false;
Expand Down
14 changes: 13 additions & 1 deletion src/runtime/JobManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default class JobManager {
filterByRunOn: "openagents/extism-runtime",
wait: 60000,
excludeId: this.failedJobs.map((j) => j.jobId),
filterByBids: [],
})
).response;

Expand All @@ -72,6 +73,17 @@ export default class JobManager {
const expiration = Math.min(Date.now() + maxExecutionTime, job.expiration);
const pluginMainSHAHash = Crypto.createHash("sha256").update(pluginMain).digest("hex");

let allowedHosts = undefined;
for(const p of job.param){
if (p.key == "allow-host") {
if (!allowedHosts) allowedHosts = [];
allowedHosts.push(...p.value);
} else if (p.key == "allow-hosts") {
if (!allowedHosts) allowedHosts = [];
allowedHosts.push(...p.value);
}
}

if(this.secrets){
const secrets0 = this.secrets.namespace(pluginMainSHAHash);
const secrets1 = this.secrets.namespace(pluginMain);
Expand Down Expand Up @@ -101,7 +113,7 @@ export default class JobManager {
}
}
console.log("Running plugin", pluginMain);
const plugin = new ExtismJob(job.id, pluginMain, expiration, mergedHostFunctions);
const plugin = new ExtismJob(job.id, pluginMain, expiration, mergedHostFunctions, allowedHosts);
const res = (await this.conn.acceptJob({ jobId: job.id })).response;
console.log("Accepted job", res);
// console.log("Initializing plugin", pluginMain, pluginMainSHAHash,inputData);
Expand Down
14 changes: 14 additions & 0 deletions src/runtime/PluginRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,38 @@ export default class PluginRepo{
const miniTemplate = plugin["mini-template"];
const main = miniTemplate.main;
const input=miniTemplate.input;
const allowedHosts = miniTemplate["allowed-hosts"];


if(!main || !input) throw new Error("Invalid mini-template");
const escapeString = (str)=>{
return str.replace(/"/g,`\\"`);
};

let allowedHostsTemplate="";
if(allowedHosts){
for(const host of allowedHosts){
allowedHostsTemplate+=`["param", "allow-host", "${escapeString(host)}"],\n`;
}
}


template = `
{
"kind": {{{meta.kind}}},
"created_at": {{{sys.timestamp_seconds}}},
"tags": [
["param","run-on", "openagents/extism-runtime" ],
["param","main","${escapeString(main)}"],
${allowedHostsTemplate.trim()}
["i", "${escapeString(input)}", "text", ""],
["expiration", "{{{sys.expiration_timestamp_seconds}}}"]
],
"content":""
}
`;
}

if(!template) throw new Error("Invalid plugin template");
if(!sockets) throw new Error("Invalid plugin sockets");
if(!meta) throw new Error("Invalid plugin meta");
Expand Down

0 comments on commit 2d3191b

Please sign in to comment.