-
Notifications
You must be signed in to change notification settings - Fork 488
Expand file tree
/
Copy pathindex.ts
More file actions
210 lines (175 loc) · 6.98 KB
/
Copy pathindex.ts
File metadata and controls
210 lines (175 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* knock-knock — Real LLM Knock-Knock Joke Exchange
*
* Two Copilot sessions trade knock-knock jokes forever.
* Demonstrates: SquadClientWithPool, CastingEngine, StreamingPipeline,
* and live LLM-generated comedy.
*
* GITHUB_TOKEN required.
*/
import { CastingEngine, StreamingPipeline } from '@bradygaster/squad-sdk';
import type { StreamDelta } from '@bradygaster/squad-sdk';
import { SquadClientWithPool } from '@bradygaster/squad-sdk/client';
// ── Agent Setup ──────────────────────────────────────────────────────
interface AgentInfo {
name: string;
role: string;
systemPrompt: string;
sessionId?: string;
}
const TELLER_PROMPT = `You are a comedian performing knock-knock jokes. When prompted, tell ONE knock-knock joke. Keep the format: "Knock knock!" then wait for the response, then deliver the setup and punchline. Be creative and funny. Keep responses short — just the joke, no commentary.`;
const RESPONDER_PROMPT = `You are the audience for a knock-knock joke. Respond naturally to each part. Say "Who's there?" after "Knock knock!" and "[setup] who?" after the setup line. After the punchline, react with a short genuine response (laugh, groan, or witty comeback). Keep responses to one line.`;
// ── Main Loop ────────────────────────────────────────────────────────
async function main(): Promise<void> {
// Auth check
if (!process.env.GITHUB_TOKEN) {
console.error('\n❌ Missing GITHUB_TOKEN environment variable.\n');
console.error('Setup instructions:');
console.error(' 1. Generate a token at https://github.com/settings/tokens');
console.error(' 2. Set GITHUB_TOKEN in your environment:');
console.error(' export GITHUB_TOKEN=ghp_...\n');
process.exit(1);
}
const casting = new CastingEngine();
const team = casting.castTeam({
universe: 'usual-suspects',
requiredRoles: ['developer', 'tester'],
teamSize: 2,
});
const [agentA, agentB] = team;
const agents: AgentInfo[] = [
{
name: agentA.name,
role: agentA.role,
systemPrompt: TELLER_PROMPT,
},
{
name: agentB.name,
role: agentB.role,
systemPrompt: RESPONDER_PROMPT,
},
];
console.log('\n🎭 Knock-Knock Comedy Hour (Live LLM Edition)\n');
console.log(` ${agents[0].name} (Teller) vs. ${agents[1].name} (Responder)\n`);
console.log(' Connecting to Copilot...\n');
// Connect to Copilot
const client = new SquadClientWithPool({ githubToken: process.env.GITHUB_TOKEN });
try {
await client.connect();
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
console.error(`\n❌ Connection failed: ${msg}\n`);
console.error('Verify your GITHUB_TOKEN is valid and has Copilot access.\n');
process.exit(1);
}
// Create sessions
const pipeline = new StreamingPipeline();
pipeline.onDelta((event) => {
process.stdout.write(event.content);
});
for (const agent of agents) {
const session = await client.createSession({
streaming: true,
systemMessage: { mode: 'append', content: agent.systemPrompt },
onPermissionRequest: () => ({ kind: 'approve-once' }),
});
agent.sessionId = session.sessionId;
pipeline.attachToSession(session.sessionId);
}
console.log(' ✓ Connected. Let the jokes begin!\n');
// Infinite joke loop — full 5-turn knock-knock exchange
let jokeCount = 0;
while (true) {
const teller = agents[jokeCount % 2];
const responder = agents[(jokeCount + 1) % 2];
const pause = (ms: number) => new Promise((r) => setTimeout(r, ms));
// Turn 1: Teller opens with "Knock knock!"
process.stdout.write(`🎭 ${teller.name}: `);
const opener = await sendAndCapture(client, pipeline, teller, 'Start a new knock-knock joke. Just say "Knock knock!"');
console.log();
await pause(800);
// Turn 2: Responder says "Who's there?"
process.stdout.write(`🎭 ${responder.name}: `);
const whoseThere = await sendAndCapture(client, pipeline, responder, opener);
console.log();
await pause(800);
// Turn 3: Teller gives the setup name
process.stdout.write(`🎭 ${teller.name}: `);
const setup = await sendAndCapture(client, pipeline, teller, whoseThere);
console.log();
await pause(800);
// Turn 4: Responder says "[setup] who?"
process.stdout.write(`🎭 ${responder.name}: `);
const setupWho = await sendAndCapture(client, pipeline, responder, setup);
console.log();
await pause(800);
// Turn 5: Teller delivers the punchline
process.stdout.write(`🎭 ${teller.name}: `);
await sendAndCapture(client, pipeline, teller, setupWho);
console.log('\n');
// Swap roles for next joke
agents.reverse();
jokeCount++;
await pause(3000);
}
}
// ── Helper: Send message and capture full response ──────────────────
async function sendAndCapture(
client: SquadClientWithPool,
pipeline: StreamingPipeline,
agent: AgentInfo,
message: string,
): Promise<string> {
const sessionId = agent.sessionId!;
let captured = '';
pipeline.markMessageStart(sessionId);
const session = await client.resumeSession(sessionId, {
onPermissionRequest: () => ({ kind: 'approve-once' }),
});
const handler = (event: { type: string; [key: string]: unknown }) => {
if (event.type === 'message_delta') {
const content =
(event['deltaContent'] as string) ??
(event['delta'] as string) ??
(event['content'] as string) ??
'';
if (content) {
captured += content;
void pipeline.processEvent({
type: 'message_delta',
sessionId,
agentName: agent.name,
content,
index: typeof event['index'] === 'number' ? event['index'] : 0,
timestamp: new Date(),
});
}
}
};
session.on('message_delta', handler);
try {
let fallback = '';
if (session.sendAndWait) {
const result = await session.sendAndWait({ prompt: message }, 30_000);
// Extract content from sendAndWait result (same pattern as shell)
const data = (result as Record<string, unknown> | undefined)?.['data'] as Record<string, unknown> | undefined;
fallback = typeof data?.['content'] === 'string' ? (data['content'] as string) : '';
// If result itself is a string, use that
if (!fallback && typeof result === 'string') fallback = result;
} else {
await session.sendMessage({ prompt: message });
}
// Use streaming content if captured, otherwise fall back to sendAndWait result
if (!captured && fallback) {
captured = fallback;
process.stdout.write(captured);
}
} finally {
session.off('message_delta', handler);
}
return captured.trim();
}
main().catch((err) => {
console.error('❌ Fatal error:', err);
process.exit(1);
});