agent-state-machine 2.0.5 → 2.0.6
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.
- package/lib/runtime/agent.js +49 -12
- package/lib/runtime/runtime.js +4 -0
- package/package.json +1 -1
- package/vercel-server/api/submit/[token].js +3 -12
- package/vercel-server/ui/index.html +73 -24
package/lib/runtime/agent.js
CHANGED
|
@@ -26,26 +26,24 @@ export async function agent(name, params = {}) {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
console.log(` [Agent: ${name}] Starting...`);
|
|
29
|
-
runtime.prependHistory({
|
|
30
|
-
event: 'AGENT_STARTED',
|
|
31
|
-
agent: name
|
|
32
|
-
});
|
|
33
29
|
|
|
34
30
|
try {
|
|
35
31
|
const result = await executeAgent(runtime, name, params);
|
|
36
32
|
|
|
37
|
-
let prompt = undefined;
|
|
38
33
|
if (result && typeof result === 'object' && result._debug_prompt) {
|
|
39
|
-
prompt = result._debug_prompt;
|
|
40
34
|
delete result._debug_prompt;
|
|
41
35
|
}
|
|
42
36
|
|
|
43
37
|
console.log(` [Agent: ${name}] Completed`);
|
|
38
|
+
if (runtime._agentSuppressCompletion?.has(name)) {
|
|
39
|
+
runtime._agentSuppressCompletion.delete(name);
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
|
|
44
43
|
runtime.prependHistory({
|
|
45
44
|
event: 'AGENT_COMPLETED',
|
|
46
45
|
agent: name,
|
|
47
|
-
output: result
|
|
48
|
-
prompt: prompt
|
|
46
|
+
output: result
|
|
49
47
|
});
|
|
50
48
|
|
|
51
49
|
return result;
|
|
@@ -119,6 +117,8 @@ async function executeJSAgent(runtime, agentPath, name, params) {
|
|
|
119
117
|
throw new Error(`Agent ${name} does not export a function`);
|
|
120
118
|
}
|
|
121
119
|
|
|
120
|
+
logAgentStart(runtime, name);
|
|
121
|
+
|
|
122
122
|
// Build context
|
|
123
123
|
const context = {
|
|
124
124
|
...runtime._rawMemory,
|
|
@@ -135,7 +135,7 @@ async function executeJSAgent(runtime, agentPath, name, params) {
|
|
|
135
135
|
|
|
136
136
|
// Handle interaction response from JS agent
|
|
137
137
|
if (result && result._interaction) {
|
|
138
|
-
const interactionResponse = await handleInteraction(runtime, result._interaction);
|
|
138
|
+
const interactionResponse = await handleInteraction(runtime, result._interaction, name);
|
|
139
139
|
|
|
140
140
|
// Use the interaction response as the primary output if it exists
|
|
141
141
|
// This allows the workflow to receive the user's input directly
|
|
@@ -176,7 +176,7 @@ async function executeJSAgent(runtime, agentPath, name, params) {
|
|
|
176
176
|
* Execute a Markdown agent (prompt-based)
|
|
177
177
|
*/
|
|
178
178
|
async function executeMDAgent(runtime, agentPath, name, params) {
|
|
179
|
-
const { llm, parseJSON, parseInteractionRequest } = await import('../llm.js');
|
|
179
|
+
const { llm, buildPrompt, parseJSON, parseInteractionRequest } = await import('../llm.js');
|
|
180
180
|
|
|
181
181
|
const content = fs.readFileSync(agentPath, 'utf-8');
|
|
182
182
|
const { config, prompt } = parseMarkdownAgent(content);
|
|
@@ -201,6 +201,14 @@ async function executeMDAgent(runtime, agentPath, name, params) {
|
|
|
201
201
|
|
|
202
202
|
const model = config.model || 'fast';
|
|
203
203
|
|
|
204
|
+
const fullPrompt = buildPrompt(context, {
|
|
205
|
+
model,
|
|
206
|
+
prompt: interpolatedPrompt,
|
|
207
|
+
includeContext: config.includeContext !== 'false'
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
logAgentStart(runtime, name, fullPrompt);
|
|
211
|
+
|
|
204
212
|
console.log(` Using model: ${model}`);
|
|
205
213
|
|
|
206
214
|
const response = await llm(context, {
|
|
@@ -247,7 +255,7 @@ async function executeMDAgent(runtime, agentPath, name, params) {
|
|
|
247
255
|
slug,
|
|
248
256
|
targetKey,
|
|
249
257
|
content: interactionContent
|
|
250
|
-
});
|
|
258
|
+
}, name);
|
|
251
259
|
|
|
252
260
|
// Return the user's response as the agent result
|
|
253
261
|
return { [outputKey]: userResponse, _debug_prompt: response.fullPrompt };
|
|
@@ -334,7 +342,9 @@ function sanitizeSlug(input) {
|
|
|
334
342
|
/**
|
|
335
343
|
* Handle interaction (create file, wait for user, return response)
|
|
336
344
|
*/
|
|
337
|
-
async function handleInteraction(runtime, interaction) {
|
|
345
|
+
async function handleInteraction(runtime, interaction, agentName) {
|
|
346
|
+
const effectiveAgentName = typeof agentName === 'string' ? agentName : null;
|
|
347
|
+
|
|
338
348
|
const slug = sanitizeSlug(interaction.slug);
|
|
339
349
|
const targetKey = String(interaction.targetKey || slug);
|
|
340
350
|
const content = String(interaction.content || '').trim();
|
|
@@ -357,8 +367,35 @@ ${content}
|
|
|
357
367
|
question: content
|
|
358
368
|
});
|
|
359
369
|
|
|
370
|
+
if (effectiveAgentName) {
|
|
371
|
+
runtime._agentSuppressCompletion?.add(effectiveAgentName);
|
|
372
|
+
runtime._agentResumeFlags?.add(effectiveAgentName);
|
|
373
|
+
}
|
|
374
|
+
|
|
360
375
|
// Block and wait for user input (instead of throwing)
|
|
361
376
|
const response = await runtime.waitForInteraction(filePath, slug, targetKey);
|
|
362
377
|
|
|
363
378
|
return response;
|
|
364
379
|
}
|
|
380
|
+
|
|
381
|
+
function logAgentStart(runtime, name, prompt) {
|
|
382
|
+
if (runtime._agentResumeFlags?.has(name)) {
|
|
383
|
+
runtime._agentResumeFlags.delete(name);
|
|
384
|
+
runtime.prependHistory({
|
|
385
|
+
event: 'AGENT_RESUMED',
|
|
386
|
+
agent: name
|
|
387
|
+
});
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const entry = {
|
|
392
|
+
event: 'AGENT_STARTED',
|
|
393
|
+
agent: name
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
if (prompt) {
|
|
397
|
+
entry.prompt = prompt;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
runtime.prependHistory(entry);
|
|
401
|
+
}
|
package/lib/runtime/runtime.js
CHANGED
|
@@ -84,6 +84,10 @@ export class WorkflowRuntime {
|
|
|
84
84
|
this.remoteEnabled = false;
|
|
85
85
|
this.remoteUrl = null;
|
|
86
86
|
this.pendingRemoteInteraction = null; // { slug, targetKey, resolve, reject }
|
|
87
|
+
|
|
88
|
+
// Agent interaction tracking for history logging
|
|
89
|
+
this._agentResumeFlags = new Set();
|
|
90
|
+
this._agentSuppressCompletion = new Set();
|
|
87
91
|
}
|
|
88
92
|
|
|
89
93
|
ensureDirectories() {
|
package/package.json
CHANGED
|
@@ -6,8 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import {
|
|
8
8
|
getSession,
|
|
9
|
-
|
|
10
|
-
publishEvent,
|
|
9
|
+
addEvent,
|
|
11
10
|
redis,
|
|
12
11
|
KEYS,
|
|
13
12
|
} from '../../lib/redis.js';
|
|
@@ -68,22 +67,14 @@ export default async function handler(req, res) {
|
|
|
68
67
|
// Set TTL on pending list
|
|
69
68
|
await redis.expire(pendingKey, 300); // 5 minutes
|
|
70
69
|
|
|
71
|
-
// Log event to
|
|
72
|
-
|
|
70
|
+
// Log event to events list (single source of truth for UI)
|
|
71
|
+
await addEvent(token, {
|
|
73
72
|
timestamp: new Date().toISOString(),
|
|
74
73
|
event: 'INTERACTION_SUBMITTED',
|
|
75
74
|
slug,
|
|
76
75
|
targetKey: targetKey || `_interaction_${slug}`,
|
|
77
76
|
answer: response.substring(0, 200) + (response.length > 200 ? '...' : ''),
|
|
78
77
|
source: 'remote',
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
await addHistoryEvent(token, event);
|
|
82
|
-
|
|
83
|
-
// Notify other browsers
|
|
84
|
-
await publishEvent(token, {
|
|
85
|
-
type: 'event',
|
|
86
|
-
...event,
|
|
87
78
|
});
|
|
88
79
|
|
|
89
80
|
return res.status(200).json({ success: true });
|
|
@@ -674,11 +674,54 @@
|
|
|
674
674
|
|
|
675
675
|
// CENTERED inside card
|
|
676
676
|
if (item.event === "AGENT_STARTED") {
|
|
677
|
+
if (item.prompt) {
|
|
678
|
+
return wrapIO(
|
|
679
|
+
"left",
|
|
680
|
+
<section className="hairline rounded-2xl rounded-tl-none overflow-hidden io-in">
|
|
681
|
+
<div className="rtl-safe px-5 py-4 divider flex items-center justify-between">
|
|
682
|
+
<div>
|
|
683
|
+
<div className="text-[11px] tracking-[0.22em] font-semibold" style={{ color: "var(--muted)" }}>
|
|
684
|
+
AGENT STARTED
|
|
685
|
+
</div>
|
|
686
|
+
<div className="mt-1 text-[11px] tracking-[0.14em]" style={{ color: "var(--muted)" }}>
|
|
687
|
+
{(item.agent || "").toString()} • {time}
|
|
688
|
+
</div>
|
|
689
|
+
</div>
|
|
690
|
+
<CopyButton text={item.prompt} />
|
|
691
|
+
</div>
|
|
692
|
+
<div className="px-5 py-4">
|
|
693
|
+
<div className="markdown-body text-[13px] leading-relaxed overflow-x-auto">{item.prompt}</div>
|
|
694
|
+
</div>
|
|
695
|
+
</section>,
|
|
696
|
+
idx
|
|
697
|
+
);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
return wrapIO(
|
|
701
|
+
"left",
|
|
702
|
+
<section className="hairline rounded-2xl rounded-tl-none px-6 py-5 io-in">
|
|
703
|
+
<div className="rtl-safe flex items-center justify-between gap-4">
|
|
704
|
+
<div className="text-[11px] tracking-[0.22em] font-semibold" style={{ color: "var(--muted)" }}>
|
|
705
|
+
AGENT STARTED
|
|
706
|
+
</div>
|
|
707
|
+
<span className="text-[11px] tracking-[0.14em]" style={{ color: "var(--muted)" }}>
|
|
708
|
+
{time}
|
|
709
|
+
</span>
|
|
710
|
+
</div>
|
|
711
|
+
<div className="mt-3 text-[13px] leading-relaxed">
|
|
712
|
+
<span className="font-semibold">{item.agent}</span>
|
|
713
|
+
</div>
|
|
714
|
+
</section>,
|
|
715
|
+
idx
|
|
716
|
+
);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
if (item.event === "AGENT_RESUMED") {
|
|
677
720
|
return wrapIO(
|
|
678
721
|
"center",
|
|
679
722
|
<section className="meta-center">
|
|
680
723
|
<div className="text-[11px] tracking-[0.22em] font-semibold" style={{ color: "var(--muted)" }}>
|
|
681
|
-
AGENT
|
|
724
|
+
AGENT RESUMED
|
|
682
725
|
</div>
|
|
683
726
|
<div className="mt-2 text-[13px] leading-relaxed">
|
|
684
727
|
<span className="font-semibold">{item.agent}</span>
|
|
@@ -714,8 +757,8 @@
|
|
|
714
757
|
|
|
715
758
|
if (item.event === "INTERACTION_REQUESTED" || item.event === "PROMPT_REQUESTED") {
|
|
716
759
|
return wrapIO(
|
|
717
|
-
"
|
|
718
|
-
<section className="hairline rounded-2xl px-6 py-5
|
|
760
|
+
"center",
|
|
761
|
+
<section className="hairline rounded-2xl px-6 py-5">
|
|
719
762
|
<div className="rtl-safe flex items-center justify-between gap-4">
|
|
720
763
|
<div className="text-[11px] tracking-[0.22em] font-semibold" style={{ color: "var(--muted)" }}>
|
|
721
764
|
INTERVENTION REQUIRED
|
|
@@ -735,8 +778,8 @@
|
|
|
735
778
|
if (item.event === "PROMPT_ANSWERED" || item.event === "INTERACTION_SUBMITTED") {
|
|
736
779
|
const isManual = item.source === "remote";
|
|
737
780
|
return wrapIO(
|
|
738
|
-
"
|
|
739
|
-
<section className="hairline rounded-2xl px-6 py-5
|
|
781
|
+
"center",
|
|
782
|
+
<section className="hairline rounded-2xl px-6 py-5">
|
|
740
783
|
<div className="rtl-safe flex items-center justify-between gap-4">
|
|
741
784
|
<div className="text-[11px] tracking-[0.22em] font-semibold" style={{ color: "var(--muted)" }}>
|
|
742
785
|
{isManual ? "RESOLVED VIA BROWSER" : "USER ANSWERED"}
|
|
@@ -752,31 +795,37 @@
|
|
|
752
795
|
}
|
|
753
796
|
|
|
754
797
|
if (item.event === "AGENT_COMPLETED" || item.event === "INTERACTION_RESOLVED") {
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
<section className="meta-center">
|
|
798
|
+
if (item.event === "AGENT_COMPLETED") {
|
|
799
|
+
return wrapIO(
|
|
800
|
+
"right",
|
|
801
|
+
<section className="io-out">
|
|
802
|
+
<section className="meta-center mb-4">
|
|
760
803
|
<div className="text-[11px] tracking-[0.22em] font-semibold" style={{ color: "var(--muted)" }}>
|
|
761
804
|
DONE
|
|
762
805
|
</div>
|
|
763
806
|
<div className="mt-1 text-[11px] tracking-[0.14em]" style={{ color: "var(--muted)" }}>
|
|
764
|
-
{(item.agent ||
|
|
807
|
+
{(item.agent || "").toString()} • {time}
|
|
765
808
|
</div>
|
|
766
|
-
</section
|
|
767
|
-
|
|
768
|
-
)}
|
|
769
|
-
|
|
770
|
-
{(item.output || item.result) && wrapIO(
|
|
771
|
-
"right",
|
|
772
|
-
<section className="io-out">
|
|
809
|
+
</section>
|
|
810
|
+
{(item.output || item.result) && (
|
|
773
811
|
<JsonView data={item.output || item.result} label="OUTPUT / RESPONSE" align="right" />
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
812
|
+
)}
|
|
813
|
+
</section>,
|
|
814
|
+
idx
|
|
815
|
+
);
|
|
816
|
+
}
|
|
777
817
|
|
|
778
|
-
|
|
779
|
-
|
|
818
|
+
return wrapIO(
|
|
819
|
+
"center",
|
|
820
|
+
<section className="meta-center">
|
|
821
|
+
<div className="text-[11px] tracking-[0.22em] font-semibold" style={{ color: "var(--muted)" }}>
|
|
822
|
+
DONE
|
|
823
|
+
</div>
|
|
824
|
+
<div className="mt-1 text-[11px] tracking-[0.14em]" style={{ color: "var(--muted)" }}>
|
|
825
|
+
{(item.slug || "").toString()} • {time}
|
|
826
|
+
</div>
|
|
827
|
+
</section>,
|
|
828
|
+
idx
|
|
780
829
|
);
|
|
781
830
|
}
|
|
782
831
|
|
|
@@ -936,4 +985,4 @@
|
|
|
936
985
|
</script>
|
|
937
986
|
</body>
|
|
938
987
|
|
|
939
|
-
</html>
|
|
988
|
+
</html>
|