opencode-pilot 0.20.0 → 0.20.2
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/package.json +1 -1
- package/service/actions.js +175 -68
- package/service/presets/github.yaml +1 -0
- package/test/unit/actions.test.js +337 -0
- package/test/unit/poller.test.js +25 -0
package/package.json
CHANGED
package/service/actions.js
CHANGED
|
@@ -14,6 +14,84 @@ import { resolveWorktreeDirectory, getProjectInfo, getProjectInfoForDirectory }
|
|
|
14
14
|
import path from "path";
|
|
15
15
|
import os from "os";
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Parse a slash command from the beginning of a prompt
|
|
19
|
+
* Returns null if the prompt doesn't start with a slash command
|
|
20
|
+
*
|
|
21
|
+
* @param {string} prompt - The prompt text
|
|
22
|
+
* @returns {object|null} { command, arguments, rest } or null
|
|
23
|
+
*/
|
|
24
|
+
export function parseSlashCommand(prompt) {
|
|
25
|
+
if (!prompt || typeof prompt !== 'string') {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Match /command at the start, followed by optional arguments on the same line
|
|
30
|
+
// Command names can contain letters, numbers, hyphens, and underscores
|
|
31
|
+
const match = prompt.match(/^\/([a-zA-Z0-9_-]+)(?:\s+(.*))?$/m);
|
|
32
|
+
|
|
33
|
+
if (!match) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const command = match[1];
|
|
38
|
+
const firstLineArgs = match[2]?.trim() || '';
|
|
39
|
+
|
|
40
|
+
// Find where the first line ends to get the "rest" of the prompt
|
|
41
|
+
const firstNewline = prompt.indexOf('\n');
|
|
42
|
+
const rest = firstNewline >= 0 ? prompt.slice(firstNewline + 1).trim() : '';
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
command,
|
|
46
|
+
arguments: firstLineArgs,
|
|
47
|
+
rest,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Send a command to a session via the /command endpoint
|
|
53
|
+
*
|
|
54
|
+
* @param {string} serverUrl - Server URL
|
|
55
|
+
* @param {string} sessionId - Session ID
|
|
56
|
+
* @param {string} directory - Working directory
|
|
57
|
+
* @param {object} parsedCommand - Parsed command from parseSlashCommand
|
|
58
|
+
* @param {object} [options] - Options
|
|
59
|
+
* @param {string} [options.agent] - Agent to use
|
|
60
|
+
* @param {string} [options.model] - Model to use
|
|
61
|
+
* @param {function} [options.fetch] - Custom fetch function (for testing)
|
|
62
|
+
* @returns {Promise<Response>} The fetch response
|
|
63
|
+
*/
|
|
64
|
+
async function sendCommand(serverUrl, sessionId, directory, parsedCommand, options = {}) {
|
|
65
|
+
const fetchFn = options.fetch || fetch;
|
|
66
|
+
|
|
67
|
+
const commandUrl = new URL(`/session/${sessionId}/command`, serverUrl);
|
|
68
|
+
commandUrl.searchParams.set('directory', directory);
|
|
69
|
+
|
|
70
|
+
// Build command body per OpenCode API schema
|
|
71
|
+
const commandBody = {
|
|
72
|
+
command: parsedCommand.command,
|
|
73
|
+
arguments: parsedCommand.arguments,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// Add agent if specified
|
|
77
|
+
if (options.agent) {
|
|
78
|
+
commandBody.agent = options.agent;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Add model if specified (the /command endpoint takes model as a single string)
|
|
82
|
+
if (options.model) {
|
|
83
|
+
commandBody.model = options.model;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
debug(`sendCommand: POST ${commandUrl} command=${parsedCommand.command} args=${parsedCommand.arguments}`);
|
|
87
|
+
|
|
88
|
+
return fetchFn(commandUrl.toString(), {
|
|
89
|
+
method: 'POST',
|
|
90
|
+
headers: { 'Content-Type': 'application/json' },
|
|
91
|
+
body: JSON.stringify(commandBody),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
17
95
|
/**
|
|
18
96
|
* Get running opencode server ports by parsing lsof output
|
|
19
97
|
* @returns {Promise<number[]>} Array of port numbers
|
|
@@ -455,50 +533,65 @@ export async function sendMessageToSession(serverUrl, sessionId, directory, prom
|
|
|
455
533
|
debug(`sendMessageToSession: updated title for session ${sessionId}`);
|
|
456
534
|
}
|
|
457
535
|
|
|
458
|
-
// Step 2:
|
|
459
|
-
const
|
|
460
|
-
messageUrl.searchParams.set('directory', directory);
|
|
461
|
-
|
|
462
|
-
const messageBody = {
|
|
463
|
-
parts: [{ type: 'text', text: prompt }],
|
|
464
|
-
};
|
|
465
|
-
|
|
466
|
-
if (options.agent) {
|
|
467
|
-
messageBody.agent = options.agent;
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
if (options.model) {
|
|
471
|
-
const [providerID, modelID] = options.model.includes('/')
|
|
472
|
-
? options.model.split('/', 2)
|
|
473
|
-
: ['anthropic', options.model];
|
|
474
|
-
messageBody.providerID = providerID;
|
|
475
|
-
messageBody.modelID = modelID;
|
|
476
|
-
}
|
|
536
|
+
// Step 2: Check if the prompt starts with a slash command
|
|
537
|
+
const parsedCommand = parseSlashCommand(prompt);
|
|
477
538
|
|
|
478
539
|
// Use AbortController with timeout (same pattern as createSessionViaApi)
|
|
479
540
|
const controller = new AbortController();
|
|
480
541
|
const timeoutId = setTimeout(() => controller.abort(), 10000);
|
|
481
542
|
|
|
482
543
|
try {
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
544
|
+
let response;
|
|
545
|
+
|
|
546
|
+
if (parsedCommand) {
|
|
547
|
+
// Use the /command endpoint for slash commands
|
|
548
|
+
debug(`sendMessageToSession: detected command /${parsedCommand.command}`);
|
|
549
|
+
response = await sendCommand(serverUrl, sessionId, directory, parsedCommand, {
|
|
550
|
+
agent: options.agent,
|
|
551
|
+
model: options.model,
|
|
552
|
+
fetch: (url, opts) => fetchFn(url, { ...opts, signal: controller.signal }),
|
|
553
|
+
});
|
|
554
|
+
} else {
|
|
555
|
+
// Use the /message endpoint for regular prompts
|
|
556
|
+
const messageUrl = new URL(`/session/${sessionId}/message`, serverUrl);
|
|
557
|
+
messageUrl.searchParams.set('directory', directory);
|
|
558
|
+
|
|
559
|
+
const messageBody = {
|
|
560
|
+
parts: [{ type: 'text', text: prompt }],
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
if (options.agent) {
|
|
564
|
+
messageBody.agent = options.agent;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if (options.model) {
|
|
568
|
+
const [providerID, modelID] = options.model.includes('/')
|
|
569
|
+
? options.model.split('/', 2)
|
|
570
|
+
: ['anthropic', options.model];
|
|
571
|
+
messageBody.providerID = providerID;
|
|
572
|
+
messageBody.modelID = modelID;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
response = await fetchFn(messageUrl.toString(), {
|
|
576
|
+
method: 'POST',
|
|
577
|
+
headers: { 'Content-Type': 'application/json' },
|
|
578
|
+
body: JSON.stringify(messageBody),
|
|
579
|
+
signal: controller.signal,
|
|
580
|
+
});
|
|
581
|
+
}
|
|
489
582
|
|
|
490
583
|
clearTimeout(timeoutId);
|
|
491
584
|
|
|
492
|
-
if (!
|
|
493
|
-
const errorText = await
|
|
494
|
-
throw new Error(`Failed to send message: ${
|
|
585
|
+
if (!response.ok) {
|
|
586
|
+
const errorText = await response.text();
|
|
587
|
+
throw new Error(`Failed to send ${parsedCommand ? 'command' : 'message'}: ${response.status} ${errorText}`);
|
|
495
588
|
}
|
|
496
589
|
|
|
497
|
-
debug(`sendMessageToSession: sent message to session ${sessionId}`);
|
|
590
|
+
debug(`sendMessageToSession: sent ${parsedCommand ? 'command' : 'message'} to session ${sessionId}`);
|
|
498
591
|
} catch (abortErr) {
|
|
499
592
|
clearTimeout(timeoutId);
|
|
500
593
|
if (abortErr.name === 'AbortError') {
|
|
501
|
-
debug(`sendMessageToSession:
|
|
594
|
+
debug(`sendMessageToSession: request started for session ${sessionId} (response aborted as expected)`);
|
|
502
595
|
} else {
|
|
503
596
|
throw abortErr;
|
|
504
597
|
}
|
|
@@ -610,58 +703,72 @@ export async function createSessionViaApi(serverUrl, directory, prompt, options
|
|
|
610
703
|
});
|
|
611
704
|
}
|
|
612
705
|
|
|
613
|
-
// Step 3:
|
|
614
|
-
const
|
|
615
|
-
messageUrl.searchParams.set('directory', directory);
|
|
616
|
-
|
|
617
|
-
// Build message body
|
|
618
|
-
const messageBody = {
|
|
619
|
-
parts: [{ type: 'text', text: prompt }],
|
|
620
|
-
};
|
|
621
|
-
|
|
622
|
-
// Add agent if specified
|
|
623
|
-
if (options.agent) {
|
|
624
|
-
messageBody.agent = options.agent;
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
// Add model if specified (format: provider/model)
|
|
628
|
-
if (options.model) {
|
|
629
|
-
const [providerID, modelID] = options.model.includes('/')
|
|
630
|
-
? options.model.split('/', 2)
|
|
631
|
-
: ['anthropic', options.model];
|
|
632
|
-
messageBody.providerID = providerID;
|
|
633
|
-
messageBody.modelID = modelID;
|
|
634
|
-
}
|
|
706
|
+
// Step 3: Check if the prompt starts with a slash command
|
|
707
|
+
const parsedCommand = parseSlashCommand(prompt);
|
|
635
708
|
|
|
636
|
-
// Use AbortController with timeout for the
|
|
637
|
-
// The
|
|
638
|
-
//
|
|
639
|
-
// request was accepted (2xx status), not wait for the full response.
|
|
709
|
+
// Use AbortController with timeout for the request
|
|
710
|
+
// The endpoints return a chunked/streaming response that stays open until
|
|
711
|
+
// the agent completes. We only need to verify the request was accepted.
|
|
640
712
|
const controller = new AbortController();
|
|
641
713
|
const timeoutId = setTimeout(() => controller.abort(), 10000); // 10 second timeout
|
|
642
714
|
|
|
643
715
|
try {
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
716
|
+
let response;
|
|
717
|
+
|
|
718
|
+
if (parsedCommand) {
|
|
719
|
+
// Use the /command endpoint for slash commands
|
|
720
|
+
debug(`createSessionViaApi: detected command /${parsedCommand.command}`);
|
|
721
|
+
response = await sendCommand(serverUrl, session.id, directory, parsedCommand, {
|
|
722
|
+
agent: options.agent,
|
|
723
|
+
model: options.model,
|
|
724
|
+
fetch: (url, opts) => fetchFn(url, { ...opts, signal: controller.signal }),
|
|
725
|
+
});
|
|
726
|
+
} else {
|
|
727
|
+
// Use the /message endpoint for regular prompts
|
|
728
|
+
const messageUrl = new URL(`/session/${session.id}/message`, serverUrl);
|
|
729
|
+
messageUrl.searchParams.set('directory', directory);
|
|
730
|
+
|
|
731
|
+
// Build message body
|
|
732
|
+
const messageBody = {
|
|
733
|
+
parts: [{ type: 'text', text: prompt }],
|
|
734
|
+
};
|
|
735
|
+
|
|
736
|
+
// Add agent if specified
|
|
737
|
+
if (options.agent) {
|
|
738
|
+
messageBody.agent = options.agent;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
// Add model if specified (format: provider/model)
|
|
742
|
+
if (options.model) {
|
|
743
|
+
const [providerID, modelID] = options.model.includes('/')
|
|
744
|
+
? options.model.split('/', 2)
|
|
745
|
+
: ['anthropic', options.model];
|
|
746
|
+
messageBody.providerID = providerID;
|
|
747
|
+
messageBody.modelID = modelID;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
response = await fetchFn(messageUrl.toString(), {
|
|
751
|
+
method: 'POST',
|
|
752
|
+
headers: { 'Content-Type': 'application/json' },
|
|
753
|
+
body: JSON.stringify(messageBody),
|
|
754
|
+
signal: controller.signal,
|
|
755
|
+
});
|
|
756
|
+
}
|
|
650
757
|
|
|
651
758
|
clearTimeout(timeoutId);
|
|
652
759
|
|
|
653
|
-
if (!
|
|
654
|
-
const errorText = await
|
|
655
|
-
throw new Error(`Failed to send message: ${
|
|
760
|
+
if (!response.ok) {
|
|
761
|
+
const errorText = await response.text();
|
|
762
|
+
throw new Error(`Failed to send ${parsedCommand ? 'command' : 'message'}: ${response.status} ${errorText}`);
|
|
656
763
|
}
|
|
657
764
|
|
|
658
|
-
debug(`createSessionViaApi: sent message to session ${session.id}`);
|
|
765
|
+
debug(`createSessionViaApi: sent ${parsedCommand ? 'command' : 'message'} to session ${session.id}`);
|
|
659
766
|
} catch (abortErr) {
|
|
660
767
|
clearTimeout(timeoutId);
|
|
661
768
|
// AbortError is expected - we intentionally abort after verifying the request started
|
|
662
|
-
// The server accepted our
|
|
769
|
+
// The server accepted our request, we just don't need to wait for the response
|
|
663
770
|
if (abortErr.name === 'AbortError') {
|
|
664
|
-
debug(`createSessionViaApi:
|
|
771
|
+
debug(`createSessionViaApi: request started for session ${session.id} (response aborted as expected)`);
|
|
665
772
|
} else {
|
|
666
773
|
throw abortErr;
|
|
667
774
|
}
|
|
@@ -10,6 +10,7 @@ _provider:
|
|
|
10
10
|
html_url: url
|
|
11
11
|
repository_full_name: repository.nameWithOwner
|
|
12
12
|
updated_at: updatedAt
|
|
13
|
+
comments: commentsCount
|
|
13
14
|
# Reprocess items when state changes (e.g., reopened issues)
|
|
14
15
|
# Note: updated_at is NOT included because our own changes would trigger reprocessing
|
|
15
16
|
reprocess_on:
|
|
@@ -97,6 +97,96 @@ describe('actions.js', () => {
|
|
|
97
97
|
});
|
|
98
98
|
});
|
|
99
99
|
|
|
100
|
+
describe('parseSlashCommand', () => {
|
|
101
|
+
test('parses /review command with URL argument', async () => {
|
|
102
|
+
const { parseSlashCommand } = await import('../../service/actions.js');
|
|
103
|
+
|
|
104
|
+
const result = parseSlashCommand('/review https://github.com/org/repo/pull/123');
|
|
105
|
+
|
|
106
|
+
assert.strictEqual(result.command, 'review');
|
|
107
|
+
assert.strictEqual(result.arguments, 'https://github.com/org/repo/pull/123');
|
|
108
|
+
assert.strictEqual(result.rest, '');
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('parses /devcontainer command with URL argument', async () => {
|
|
112
|
+
const { parseSlashCommand } = await import('../../service/actions.js');
|
|
113
|
+
|
|
114
|
+
const result = parseSlashCommand('/devcontainer https://github.com/org/repo/issues/456');
|
|
115
|
+
|
|
116
|
+
assert.strictEqual(result.command, 'devcontainer');
|
|
117
|
+
assert.strictEqual(result.arguments, 'https://github.com/org/repo/issues/456');
|
|
118
|
+
assert.strictEqual(result.rest, '');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('parses command with multiline rest content', async () => {
|
|
122
|
+
const { parseSlashCommand } = await import('../../service/actions.js');
|
|
123
|
+
|
|
124
|
+
const prompt = `/review https://github.com/org/repo/pull/123
|
|
125
|
+
|
|
126
|
+
Review this pull request:
|
|
127
|
+
|
|
128
|
+
Check for bugs and security issues.`;
|
|
129
|
+
|
|
130
|
+
const result = parseSlashCommand(prompt);
|
|
131
|
+
|
|
132
|
+
assert.strictEqual(result.command, 'review');
|
|
133
|
+
assert.strictEqual(result.arguments, 'https://github.com/org/repo/pull/123');
|
|
134
|
+
assert.ok(result.rest.includes('Review this pull request'));
|
|
135
|
+
assert.ok(result.rest.includes('Check for bugs'));
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test('parses command without arguments', async () => {
|
|
139
|
+
const { parseSlashCommand } = await import('../../service/actions.js');
|
|
140
|
+
|
|
141
|
+
const result = parseSlashCommand('/help');
|
|
142
|
+
|
|
143
|
+
assert.strictEqual(result.command, 'help');
|
|
144
|
+
assert.strictEqual(result.arguments, '');
|
|
145
|
+
assert.strictEqual(result.rest, '');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test('parses command with hyphen in name', async () => {
|
|
149
|
+
const { parseSlashCommand } = await import('../../service/actions.js');
|
|
150
|
+
|
|
151
|
+
const result = parseSlashCommand('/my-custom-command arg1 arg2');
|
|
152
|
+
|
|
153
|
+
assert.strictEqual(result.command, 'my-custom-command');
|
|
154
|
+
assert.strictEqual(result.arguments, 'arg1 arg2');
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test('returns null for regular prompt without slash', async () => {
|
|
158
|
+
const { parseSlashCommand } = await import('../../service/actions.js');
|
|
159
|
+
|
|
160
|
+
const result = parseSlashCommand('Fix the bug in the login page');
|
|
161
|
+
|
|
162
|
+
assert.strictEqual(result, null);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test('returns null for prompt with slash in middle', async () => {
|
|
166
|
+
const { parseSlashCommand } = await import('../../service/actions.js');
|
|
167
|
+
|
|
168
|
+
const result = parseSlashCommand('Check the path/to/file for issues');
|
|
169
|
+
|
|
170
|
+
assert.strictEqual(result, null);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test('returns null for empty string', async () => {
|
|
174
|
+
const { parseSlashCommand } = await import('../../service/actions.js');
|
|
175
|
+
|
|
176
|
+
assert.strictEqual(parseSlashCommand(''), null);
|
|
177
|
+
assert.strictEqual(parseSlashCommand(null), null);
|
|
178
|
+
assert.strictEqual(parseSlashCommand(undefined), null);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test('returns null for just a slash', async () => {
|
|
182
|
+
const { parseSlashCommand } = await import('../../service/actions.js');
|
|
183
|
+
|
|
184
|
+
const result = parseSlashCommand('/');
|
|
185
|
+
|
|
186
|
+
assert.strictEqual(result, null);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
100
190
|
describe('getActionConfig', () => {
|
|
101
191
|
test('merges source, repo, and defaults', async () => {
|
|
102
192
|
const { getActionConfig } = await import('../../service/actions.js');
|
|
@@ -903,6 +993,253 @@ describe('actions.js', () => {
|
|
|
903
993
|
assert.ok(result.warning, 'Should include warning about message failure');
|
|
904
994
|
assert.ok(result.warning.includes('Failed to send message'), 'Warning should mention message failure');
|
|
905
995
|
});
|
|
996
|
+
|
|
997
|
+
test('uses /command endpoint for slash commands', async () => {
|
|
998
|
+
const { createSessionViaApi } = await import('../../service/actions.js');
|
|
999
|
+
|
|
1000
|
+
const mockSessionId = 'ses_cmd123';
|
|
1001
|
+
let commandCalled = false;
|
|
1002
|
+
let commandUrl = null;
|
|
1003
|
+
let commandBody = null;
|
|
1004
|
+
let messageCalled = false;
|
|
1005
|
+
|
|
1006
|
+
const mockFetch = async (url, opts) => {
|
|
1007
|
+
const urlObj = new URL(url);
|
|
1008
|
+
|
|
1009
|
+
if (urlObj.pathname === '/session' && opts?.method === 'POST') {
|
|
1010
|
+
return {
|
|
1011
|
+
ok: true,
|
|
1012
|
+
json: async () => ({ id: mockSessionId }),
|
|
1013
|
+
};
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
if (urlObj.pathname.includes('/command') && opts?.method === 'POST') {
|
|
1017
|
+
commandCalled = true;
|
|
1018
|
+
commandUrl = url;
|
|
1019
|
+
commandBody = JSON.parse(opts.body);
|
|
1020
|
+
return {
|
|
1021
|
+
ok: true,
|
|
1022
|
+
json: async () => ({ success: true }),
|
|
1023
|
+
};
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
if (urlObj.pathname.includes('/message') && opts?.method === 'POST') {
|
|
1027
|
+
messageCalled = true;
|
|
1028
|
+
return {
|
|
1029
|
+
ok: true,
|
|
1030
|
+
json: async () => ({ success: true }),
|
|
1031
|
+
};
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
// PATCH for title update
|
|
1035
|
+
if (opts?.method === 'PATCH') {
|
|
1036
|
+
return { ok: true, json: async () => ({}) };
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
return { ok: false, text: async () => 'Not found' };
|
|
1040
|
+
};
|
|
1041
|
+
|
|
1042
|
+
const result = await createSessionViaApi(
|
|
1043
|
+
'http://localhost:4096',
|
|
1044
|
+
'/path/to/project',
|
|
1045
|
+
'/review https://github.com/org/repo/pull/123',
|
|
1046
|
+
{ fetch: mockFetch }
|
|
1047
|
+
);
|
|
1048
|
+
|
|
1049
|
+
assert.ok(result.success, 'Should succeed');
|
|
1050
|
+
assert.ok(commandCalled, 'Should call /command endpoint');
|
|
1051
|
+
assert.ok(!messageCalled, 'Should NOT call /message endpoint');
|
|
1052
|
+
assert.ok(commandUrl.includes('/command'), 'URL should be /command endpoint');
|
|
1053
|
+
assert.strictEqual(commandBody.command, 'review', 'Should pass command name');
|
|
1054
|
+
assert.strictEqual(commandBody.arguments, 'https://github.com/org/repo/pull/123', 'Should pass arguments');
|
|
1055
|
+
});
|
|
1056
|
+
|
|
1057
|
+
test('uses /message endpoint for regular prompts (not slash commands)', async () => {
|
|
1058
|
+
const { createSessionViaApi } = await import('../../service/actions.js');
|
|
1059
|
+
|
|
1060
|
+
const mockSessionId = 'ses_msg123';
|
|
1061
|
+
let commandCalled = false;
|
|
1062
|
+
let messageCalled = false;
|
|
1063
|
+
let messageBody = null;
|
|
1064
|
+
|
|
1065
|
+
const mockFetch = async (url, opts) => {
|
|
1066
|
+
const urlObj = new URL(url);
|
|
1067
|
+
|
|
1068
|
+
if (urlObj.pathname === '/session' && opts?.method === 'POST') {
|
|
1069
|
+
return {
|
|
1070
|
+
ok: true,
|
|
1071
|
+
json: async () => ({ id: mockSessionId }),
|
|
1072
|
+
};
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
if (urlObj.pathname.includes('/command') && opts?.method === 'POST') {
|
|
1076
|
+
commandCalled = true;
|
|
1077
|
+
return {
|
|
1078
|
+
ok: true,
|
|
1079
|
+
json: async () => ({ success: true }),
|
|
1080
|
+
};
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
if (urlObj.pathname.includes('/message') && opts?.method === 'POST') {
|
|
1084
|
+
messageCalled = true;
|
|
1085
|
+
messageBody = JSON.parse(opts.body);
|
|
1086
|
+
return {
|
|
1087
|
+
ok: true,
|
|
1088
|
+
json: async () => ({ success: true }),
|
|
1089
|
+
};
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
return { ok: false, text: async () => 'Not found' };
|
|
1093
|
+
};
|
|
1094
|
+
|
|
1095
|
+
const result = await createSessionViaApi(
|
|
1096
|
+
'http://localhost:4096',
|
|
1097
|
+
'/path/to/project',
|
|
1098
|
+
'Fix the bug in the login page',
|
|
1099
|
+
{ fetch: mockFetch }
|
|
1100
|
+
);
|
|
1101
|
+
|
|
1102
|
+
assert.ok(result.success, 'Should succeed');
|
|
1103
|
+
assert.ok(!commandCalled, 'Should NOT call /command endpoint');
|
|
1104
|
+
assert.ok(messageCalled, 'Should call /message endpoint');
|
|
1105
|
+
assert.strictEqual(messageBody.parts[0].text, 'Fix the bug in the login page', 'Should pass prompt as message text');
|
|
1106
|
+
});
|
|
1107
|
+
});
|
|
1108
|
+
|
|
1109
|
+
describe('sendMessageToSession slash command routing', () => {
|
|
1110
|
+
test('uses /command endpoint for slash commands', async () => {
|
|
1111
|
+
const { sendMessageToSession } = await import('../../service/actions.js');
|
|
1112
|
+
|
|
1113
|
+
let commandCalled = false;
|
|
1114
|
+
let commandBody = null;
|
|
1115
|
+
let messageCalled = false;
|
|
1116
|
+
|
|
1117
|
+
const mockFetch = async (url, opts) => {
|
|
1118
|
+
const urlObj = new URL(url);
|
|
1119
|
+
|
|
1120
|
+
if (urlObj.pathname.includes('/command') && opts?.method === 'POST') {
|
|
1121
|
+
commandCalled = true;
|
|
1122
|
+
commandBody = JSON.parse(opts.body);
|
|
1123
|
+
return {
|
|
1124
|
+
ok: true,
|
|
1125
|
+
json: async () => ({ success: true }),
|
|
1126
|
+
};
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
if (urlObj.pathname.includes('/message') && opts?.method === 'POST') {
|
|
1130
|
+
messageCalled = true;
|
|
1131
|
+
return {
|
|
1132
|
+
ok: true,
|
|
1133
|
+
json: async () => ({ success: true }),
|
|
1134
|
+
};
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
// PATCH for title update
|
|
1138
|
+
if (opts?.method === 'PATCH') {
|
|
1139
|
+
return { ok: true, json: async () => ({}) };
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
return { ok: false, text: async () => 'Not found' };
|
|
1143
|
+
};
|
|
1144
|
+
|
|
1145
|
+
const result = await sendMessageToSession(
|
|
1146
|
+
'http://localhost:4096',
|
|
1147
|
+
'ses_existing',
|
|
1148
|
+
'/path/to/project',
|
|
1149
|
+
'/devcontainer https://github.com/org/repo/issues/456',
|
|
1150
|
+
{ fetch: mockFetch }
|
|
1151
|
+
);
|
|
1152
|
+
|
|
1153
|
+
assert.ok(result.success, 'Should succeed');
|
|
1154
|
+
assert.ok(commandCalled, 'Should call /command endpoint');
|
|
1155
|
+
assert.ok(!messageCalled, 'Should NOT call /message endpoint');
|
|
1156
|
+
assert.strictEqual(commandBody.command, 'devcontainer', 'Should pass command name');
|
|
1157
|
+
assert.strictEqual(commandBody.arguments, 'https://github.com/org/repo/issues/456', 'Should pass arguments');
|
|
1158
|
+
});
|
|
1159
|
+
|
|
1160
|
+
test('uses /message endpoint for regular prompts', async () => {
|
|
1161
|
+
const { sendMessageToSession } = await import('../../service/actions.js');
|
|
1162
|
+
|
|
1163
|
+
let commandCalled = false;
|
|
1164
|
+
let messageCalled = false;
|
|
1165
|
+
let messageBody = null;
|
|
1166
|
+
|
|
1167
|
+
const mockFetch = async (url, opts) => {
|
|
1168
|
+
const urlObj = new URL(url);
|
|
1169
|
+
|
|
1170
|
+
if (urlObj.pathname.includes('/command') && opts?.method === 'POST') {
|
|
1171
|
+
commandCalled = true;
|
|
1172
|
+
return {
|
|
1173
|
+
ok: true,
|
|
1174
|
+
json: async () => ({ success: true }),
|
|
1175
|
+
};
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
if (urlObj.pathname.includes('/message') && opts?.method === 'POST') {
|
|
1179
|
+
messageCalled = true;
|
|
1180
|
+
messageBody = JSON.parse(opts.body);
|
|
1181
|
+
return {
|
|
1182
|
+
ok: true,
|
|
1183
|
+
json: async () => ({ success: true }),
|
|
1184
|
+
};
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
return { ok: false, text: async () => 'Not found' };
|
|
1188
|
+
};
|
|
1189
|
+
|
|
1190
|
+
const result = await sendMessageToSession(
|
|
1191
|
+
'http://localhost:4096',
|
|
1192
|
+
'ses_existing',
|
|
1193
|
+
'/path/to/project',
|
|
1194
|
+
'Please fix the bug',
|
|
1195
|
+
{ fetch: mockFetch }
|
|
1196
|
+
);
|
|
1197
|
+
|
|
1198
|
+
assert.ok(result.success, 'Should succeed');
|
|
1199
|
+
assert.ok(!commandCalled, 'Should NOT call /command endpoint');
|
|
1200
|
+
assert.ok(messageCalled, 'Should call /message endpoint');
|
|
1201
|
+
assert.strictEqual(messageBody.parts[0].text, 'Please fix the bug', 'Should pass prompt as message text');
|
|
1202
|
+
});
|
|
1203
|
+
|
|
1204
|
+
test('passes agent and model to /command endpoint', async () => {
|
|
1205
|
+
const { sendMessageToSession } = await import('../../service/actions.js');
|
|
1206
|
+
|
|
1207
|
+
let commandBody = null;
|
|
1208
|
+
|
|
1209
|
+
const mockFetch = async (url, opts) => {
|
|
1210
|
+
const urlObj = new URL(url);
|
|
1211
|
+
|
|
1212
|
+
if (urlObj.pathname.includes('/command') && opts?.method === 'POST') {
|
|
1213
|
+
commandBody = JSON.parse(opts.body);
|
|
1214
|
+
return {
|
|
1215
|
+
ok: true,
|
|
1216
|
+
json: async () => ({ success: true }),
|
|
1217
|
+
};
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
// PATCH for title update
|
|
1221
|
+
if (opts?.method === 'PATCH') {
|
|
1222
|
+
return { ok: true, json: async () => ({}) };
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
return { ok: false, text: async () => 'Not found' };
|
|
1226
|
+
};
|
|
1227
|
+
|
|
1228
|
+
await sendMessageToSession(
|
|
1229
|
+
'http://localhost:4096',
|
|
1230
|
+
'ses_existing',
|
|
1231
|
+
'/path/to/project',
|
|
1232
|
+
'/review https://github.com/org/repo/pull/123',
|
|
1233
|
+
{
|
|
1234
|
+
fetch: mockFetch,
|
|
1235
|
+
agent: 'code',
|
|
1236
|
+
model: 'anthropic/claude-sonnet-4-20250514',
|
|
1237
|
+
}
|
|
1238
|
+
);
|
|
1239
|
+
|
|
1240
|
+
assert.strictEqual(commandBody.agent, 'code', 'Should pass agent');
|
|
1241
|
+
assert.strictEqual(commandBody.model, 'anthropic/claude-sonnet-4-20250514', 'Should pass model as string');
|
|
1242
|
+
});
|
|
906
1243
|
});
|
|
907
1244
|
|
|
908
1245
|
describe('session reuse', () => {
|
package/test/unit/poller.test.js
CHANGED
|
@@ -786,6 +786,31 @@ describe('poller.js', () => {
|
|
|
786
786
|
|
|
787
787
|
assert.strictEqual(mapped.number, undefined);
|
|
788
788
|
});
|
|
789
|
+
|
|
790
|
+
test('maps commentsCount to comments for GitHub PR enrichment', async () => {
|
|
791
|
+
const { applyMappings } = await import('../../service/poller.js');
|
|
792
|
+
|
|
793
|
+
// gh search prs returns commentsCount, but enrichItemsWithComments checks for 'comments'
|
|
794
|
+
const item = {
|
|
795
|
+
number: 123,
|
|
796
|
+
title: 'Fix mobile overflow',
|
|
797
|
+
commentsCount: 4,
|
|
798
|
+
repository: { nameWithOwner: 'anomalyco/opencode' }
|
|
799
|
+
};
|
|
800
|
+
const mappings = {
|
|
801
|
+
comments: 'commentsCount',
|
|
802
|
+
repository_full_name: 'repository.nameWithOwner'
|
|
803
|
+
};
|
|
804
|
+
|
|
805
|
+
const mapped = applyMappings(item, mappings);
|
|
806
|
+
|
|
807
|
+
// comments field should be set from commentsCount
|
|
808
|
+
assert.strictEqual(mapped.comments, 4);
|
|
809
|
+
// Original commentsCount preserved
|
|
810
|
+
assert.strictEqual(mapped.commentsCount, 4);
|
|
811
|
+
// Other mappings work too
|
|
812
|
+
assert.strictEqual(mapped.repository_full_name, 'anomalyco/opencode');
|
|
813
|
+
});
|
|
789
814
|
});
|
|
790
815
|
|
|
791
816
|
describe('fetchGitHubComments', () => {
|