@meetopenbot/openbot 0.1.14 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,137 +0,0 @@
1
- import { randomUUID } from "node:crypto";
2
- import { z } from "zod";
3
- /**
4
- * `delegation` — allows agents to delegate tasks to other agents.
5
- *
6
- * Only the 'system' agent is allowed to delegate by default.
7
- * It uses runAgent to execute the delegated agent in its own isolated runtime,
8
- * bridging events back to the caller's stream.
9
- */
10
- const delegationToolDefinitions = {
11
- delegate_task: {
12
- description: "Delegate a specific task or question to another specialized agent.",
13
- inputSchema: z.object({
14
- agentId: z
15
- .string()
16
- .describe('The ID of the agent to delegate to (e.g., "researcher", "coder").'),
17
- prompt: z
18
- .string()
19
- .describe("The instructions or question for the delegated agent."),
20
- }),
21
- },
22
- };
23
- export const delegationPlugin = {
24
- id: "delegation",
25
- name: "Delegation",
26
- description: "Allows agents to call upon other agents to solve sub-tasks.",
27
- toolDefinitions: delegationToolDefinitions,
28
- factory: (pluginContext) => (builder) => {
29
- // Handle the tool execution
30
- builder.on("action:delegate_task", async function* (event, context) {
31
- const delegateEvent = event;
32
- // POLICY: Only the 'system' agent can delegate
33
- if (context.state.agentId !== pluginContext.host.orchestratorAgentId) {
34
- yield {
35
- type: "action:delegate_task:result",
36
- data: {
37
- success: false,
38
- error: "Only the system agent can delegate.",
39
- },
40
- meta: delegateEvent.meta,
41
- };
42
- return;
43
- }
44
- const { agentId, prompt } = delegateEvent.data;
45
- const toolCallId = delegateEvent.meta?.toolCallId;
46
- if (!toolCallId)
47
- return;
48
- const runAgent = pluginContext.host.runAgent;
49
- const runId = `dg_${randomUUID()}`;
50
- let lastAgentOutput = "";
51
- // Queue to bridge the async onEvent callback to this generator
52
- const eventQueue = [];
53
- let resolveNext = null;
54
- let isFinished = false;
55
- // Start the delegated agent in its own runtime.
56
- // We don't await this immediately so we can yield events as they arrive.
57
- const runPromise = runAgent({
58
- runId,
59
- agentId,
60
- event: {
61
- type: "agent:invoke",
62
- data: {
63
- role: "user",
64
- content: prompt,
65
- agentId: agentId,
66
- },
67
- meta: {
68
- channelId: context.state.channelId,
69
- threadId: context.state.threadId,
70
- parentAgentId: context.state.agentId,
71
- parentToolCallId: toolCallId,
72
- },
73
- },
74
- publicBaseUrl: pluginContext.publicBaseUrl,
75
- // Child events are re-yielded to the parent harness, which persists them once.
76
- persistEvents: false,
77
- onEvent: async (outEvent) => {
78
- // Enrich events with parent metadata so the UI can track the hierarchy
79
- const enrichedEvent = {
80
- ...outEvent,
81
- meta: {
82
- ...outEvent.meta,
83
- parentAgentId: context.state.agentId,
84
- parentToolCallId: toolCallId,
85
- },
86
- };
87
- eventQueue.push(enrichedEvent);
88
- if (outEvent.type === "agent:output") {
89
- lastAgentOutput = outEvent.data.content;
90
- }
91
- // Wake up the generator loop if it's waiting
92
- if (resolveNext) {
93
- resolveNext();
94
- resolveNext = null;
95
- }
96
- },
97
- })
98
- .catch((error) => {
99
- console.error(`[delegation] Error in delegated run ${runId}:`, error);
100
- })
101
- .finally(() => {
102
- isFinished = true;
103
- if (resolveNext) {
104
- resolveNext();
105
- resolveNext = null;
106
- }
107
- });
108
- // Yield events from the delegated agent as they arrive
109
- while (!isFinished || eventQueue.length > 0) {
110
- if (eventQueue.length === 0) {
111
- await new Promise((r) => {
112
- resolveNext = r;
113
- });
114
- }
115
- while (eventQueue.length > 0) {
116
- yield eventQueue.shift();
117
- }
118
- }
119
- // Ensure the run is fully complete (though isFinished already implies this)
120
- await runPromise;
121
- // Yield the result back to our own LLM runtime.
122
- yield {
123
- type: "action:delegate_task:result",
124
- data: {
125
- success: true,
126
- output: lastAgentOutput,
127
- },
128
- meta: {
129
- ...delegateEvent.meta,
130
- agentId: context.state.agentId,
131
- toolCallId: toolCallId,
132
- },
133
- };
134
- });
135
- },
136
- };
137
- export default delegationPlugin;