@forloop-cc/forloop-opencode-plugin-planner 2.2.11 → 2.2.12

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@forloop-cc/forloop-opencode-plugin-planner",
4
- "version": "2.2.11",
4
+ "version": "2.2.12",
5
5
  "description": "ForLoop integration plugin for opencode — Sprint, story, and agent tools for the ForLoop platform",
6
6
  "type": "module",
7
7
  "license": "MIT",
@@ -521,6 +521,10 @@ export class ForLoopAPIClient {
521
521
  });
522
522
  }
523
523
 
524
+ /**
525
+ * @deprecated Use writeConversationEvent() for new integrations.
526
+ * Kept for backward compatibility with existing plugin hooks.
527
+ */
524
528
  async recordMessage(data: MessageRecord): Promise<any> {
525
529
  return this.request('/api/opencode/messages', {
526
530
  method: 'POST',
@@ -535,6 +539,13 @@ export class ForLoopAPIClient {
535
539
  });
536
540
  }
537
541
 
542
+ async writeConversationEvent(data: WriteConversationEventInput): Promise<any> {
543
+ return this.request('/api/opencode/conversations/events', {
544
+ method: 'POST',
545
+ body: JSON.stringify(data),
546
+ });
547
+ }
548
+
538
549
  async sendStreamChunk(data: {
539
550
  taskId: string;
540
551
  trackingId: string;
@@ -556,6 +567,21 @@ export interface AgentSuggestionRequest {
556
567
  query?: string;
557
568
  }
558
569
 
570
+ export interface WriteConversationEventInput {
571
+ operation: 'append_turn' | 'start_turn' | 'complete_turn' | 'delete_turn';
572
+ sprintId: number;
573
+ targetAgent: string;
574
+ actor: { senderType: string; senderId: string };
575
+ conversationId?: string;
576
+ sessionId?: string;
577
+ turnId?: string;
578
+ messageId?: string;
579
+ requestId?: string;
580
+ userMessage?: string;
581
+ agentResponse?: string;
582
+ metadata?: Record<string, unknown>;
583
+ }
584
+
559
585
  export interface ConversationHistoryParams {
560
586
  sprintId?: number;
561
587
  limit?: number;
@@ -1,29 +1,31 @@
1
1
  import { ForLoopAPIClient } from '../capabilities/api-client';
2
- import type { MessageRecord } from '../capabilities/api-client';
2
+ import type { WriteConversationEventInput } from '../capabilities/api-client';
3
3
  import { isLambdaExecution } from '../capabilities/config';
4
4
  import fs from 'fs';
5
5
  import path from 'path';
6
6
 
7
+ interface PendingUserMessage {
8
+ messageId: string;
9
+ content: string;
10
+ agent: string;
11
+ model: any;
12
+ timestamp: number;
13
+ }
14
+
7
15
  function normalizeAgentKey(agent: string): string {
8
- if (/^forLoop[A-Z]/.test(agent)) {
9
- return (
10
- 'forloop-' +
11
- agent
12
- .slice(7)
13
- .replace(/([a-z])([A-Z])/g, '$1-$2')
14
- .toLowerCase()
15
- );
16
- }
17
- return agent
18
- .replace(/([a-z])([A-Z])/g, '$1-$2')
19
- .replace(/([A-Z]+)([A-Z][a-z])/g, '$1-$2')
20
- .toLowerCase();
16
+ return agent;
17
+ }
18
+
19
+ function getActor(): { senderType: string; senderId: string } {
20
+ return {
21
+ senderType: process.env.FORLOOP_SENDER_TYPE || 'user',
22
+ senderId: process.env.FORLOOP_SENDER_ID || 'unknown',
23
+ };
21
24
  }
22
25
 
23
26
  function buildConversationId(sprintId: number, agent: string, sessionId: string): string {
24
- const senderType = process.env.FORLOOP_SENDER_TYPE || 'user';
25
- const senderId = process.env.FORLOOP_SENDER_ID || 'unknown';
26
- return `sprint:${sprintId}:agent:${agent}:${senderType}:${senderId}`;
27
+ const actor = getActor();
28
+ return `sprint:${sprintId}:agent:${agent}:${actor.senderType}:${actor.senderId}`;
27
29
  }
28
30
 
29
31
  function readActiveSprintId(): number | null {
@@ -40,9 +42,11 @@ function readActiveSprintId(): number | null {
40
42
  }
41
43
  }
42
44
 
43
- async function recordAssistantMessage(
45
+ const userMessageBuffer = new Map<string, PendingUserMessage>();
46
+
47
+ async function writeConversationTurn(
44
48
  client: ForLoopAPIClient,
45
- data: MessageRecord,
49
+ input: WriteConversationEventInput,
46
50
  attempts: number
47
51
  ): Promise<void> {
48
52
  for (let i = 0; i < attempts; i++) {
@@ -50,20 +54,15 @@ async function recordAssistantMessage(
50
54
  if (i > 0) {
51
55
  await new Promise((resolve) => setTimeout(resolve, 600 * i));
52
56
  }
53
- const result = await client.recordMessage(data);
54
- if (result?.message === 'no matching user message to update') {
55
- console.log(`[ForLoop] No matching user message — user message may not be recorded yet`);
56
- return;
57
+ const result = await client.writeConversationEvent(input);
58
+ if (!result?.ok) {
59
+ console.warn(`[ForLoop] Write event returned not ok: ${result?.error || 'unknown'}`);
57
60
  }
58
- // After recording, try summarization (no-op if < 30 rounds)
59
- client.summarizeConversation(data.sprintId).catch((err: Error) => {
60
- console.warn('[ForLoop] Summarization trigger failed:', err.message);
61
- });
62
61
  return;
63
62
  } catch (err: any) {
64
- console.warn(`[ForLoop] Attempt ${i + 1}/${attempts} failed:`, err.message);
63
+ console.warn(`[ForLoop] Write event attempt ${i + 1}/${attempts} failed:`, err.message);
65
64
  if (i === attempts - 1) {
66
- console.warn('[ForLoop] Failed to record assistant message after all retries');
65
+ console.warn('[ForLoop] Failed to write conversation event after all retries');
67
66
  }
68
67
  }
69
68
  }
@@ -85,7 +84,6 @@ export function createChatMessageHook(client: ForLoopAPIClient) {
85
84
  const { message, parts } = output;
86
85
  const content = extractTextFromParts(parts);
87
86
 
88
- // Log incoming user message for debug visibility (Lambda)
89
87
  if (content && messageID) {
90
88
  console.log(`[ForLoop] REQUEST — user message (${content.length} chars)`, {
91
89
  sprintId,
@@ -95,25 +93,15 @@ export function createChatMessageHook(client: ForLoopAPIClient) {
95
93
  });
96
94
  }
97
95
 
98
- // Only record messages with a model (indicates actual LLM processing,
99
- // not system events like context compaction or summary generation)
100
96
  if (!model || !model.providerID) return;
97
+ if (!content) return;
101
98
 
102
- const conversationId = buildConversationId(sprintId, agent || 'unknown', sessionID);
103
-
104
- // Fire-and-forget: don't await - must not block the LLM from receiving the message
105
- client.recordMessage({
106
- sprintId,
107
- sessionId: sessionID,
99
+ userMessageBuffer.set(sessionID, {
108
100
  messageId: messageID || message.id,
109
- conversationId,
110
- role: 'user',
111
101
  content,
112
- agent: agent ? normalizeAgentKey(agent) : undefined,
102
+ agent: agent || 'unknown',
113
103
  model,
114
104
  timestamp: message.time?.created ?? Date.now(),
115
- }).catch((err: Error) => {
116
- console.warn('[ForLoop] Failed to record user message:', err.message);
117
105
  });
118
106
  };
119
107
  }
@@ -198,21 +186,36 @@ export function createEventHook(client: ForLoopAPIClient) {
198
186
  preview: bufferedText.substring(0, 500),
199
187
  });
200
188
 
189
+ const pendingUserMsg = userMessageBuffer.get(info.sessionID);
190
+ userMessageBuffer.delete(info.sessionID);
191
+
192
+ if (!pendingUserMsg) {
193
+ console.log('[ForLoop] Skipping write — no buffered user message for this session (stream handler handles the save)');
194
+ break;
195
+ }
196
+
201
197
  const conversationId = buildConversationId(
202
198
  sprintId,
203
199
  info.agent || 'unknown',
204
200
  info.sessionID
205
201
  );
206
- recordAssistantMessage(client, {
202
+ writeConversationTurn(client, {
203
+ operation: 'append_turn',
207
204
  sprintId,
205
+ targetAgent: info.agent ? normalizeAgentKey(info.agent) : 'unknown',
206
+ actor: getActor(),
207
+ conversationId,
208
208
  sessionId: info.sessionID,
209
209
  messageId: info.id,
210
- conversationId,
211
- role: 'assistant',
212
- content: bufferedText,
213
- agent: info.agent ? normalizeAgentKey(info.agent) : undefined,
214
- model: info.model,
215
- timestamp: info.time?.created ?? Date.now(),
210
+ userMessage: pendingUserMsg.content,
211
+ agentResponse: bufferedText,
212
+ metadata: {
213
+ source: 'opencode-plugin',
214
+ agent: info.agent ? normalizeAgentKey(info.agent) : null,
215
+ model: info.model || null,
216
+ userMessageId: pendingUserMsg.messageId || null,
217
+ recordedAt: info.time?.created ?? Date.now(),
218
+ },
216
219
  }, 3);
217
220
  break;
218
221
  }
@@ -220,8 +223,17 @@ export function createEventHook(client: ForLoopAPIClient) {
220
223
  const { sessionID, messageID } = event.properties || {};
221
224
  if (!sessionID || !messageID) break;
222
225
  textBuffer.delete(messageID);
223
- client.removeMessage(messageID, sprintId, sessionID).catch((err: Error) => {
224
- console.warn('[ForLoop] Failed to remove message:', err.message);
226
+ const conversationId = buildConversationId(sprintId, 'unknown', sessionID);
227
+ client.writeConversationEvent({
228
+ operation: 'delete_turn',
229
+ sprintId,
230
+ targetAgent: 'forLoopPlanner',
231
+ actor: getActor(),
232
+ conversationId,
233
+ messageId: messageID,
234
+ metadata: { source: 'opencode-plugin' },
235
+ }).catch((err: Error) => {
236
+ console.warn('[ForLoop] Failed to delete conversation turn:', err.message);
225
237
  });
226
238
  break;
227
239
  }
@@ -243,21 +255,36 @@ export function createEventHook(client: ForLoopAPIClient) {
243
255
  if (!info || info.role !== 'assistant') return;
244
256
  const bufferedText = textBuffer.get(info.id);
245
257
  if (!bufferedText) return;
258
+ const pendingUserMsg = userMessageBuffer.get(info.sessionID);
259
+ userMessageBuffer.delete(info.sessionID);
260
+
261
+ if (!pendingUserMsg) {
262
+ console.log('[ForLoop] Skipping write — no buffered user message for this session');
263
+ break;
264
+ }
265
+
246
266
  const conversationId = buildConversationId(
247
267
  sprintId,
248
268
  info.agent || 'unknown',
249
269
  info.sessionID
250
270
  );
251
- recordAssistantMessage(client, {
271
+ writeConversationTurn(client, {
272
+ operation: 'append_turn',
252
273
  sprintId,
274
+ targetAgent: info.agent ? normalizeAgentKey(info.agent) : 'unknown',
275
+ actor: getActor(),
276
+ conversationId,
253
277
  sessionId: info.sessionID,
254
278
  messageId: info.id,
255
- conversationId,
256
- role: 'assistant',
257
- content: bufferedText,
258
- agent: info.agent ? normalizeAgentKey(info.agent) : undefined,
259
- model: info.model,
260
- timestamp: info.time?.created ?? Date.now(),
279
+ userMessage: pendingUserMsg.content,
280
+ agentResponse: bufferedText,
281
+ metadata: {
282
+ source: 'opencode-plugin',
283
+ agent: info.agent ? normalizeAgentKey(info.agent) : null,
284
+ model: info.model || null,
285
+ userMessageId: pendingUserMsg.messageId || null,
286
+ recordedAt: info.time?.created ?? Date.now(),
287
+ },
261
288
  }, 3);
262
289
  break;
263
290
  }
@@ -265,8 +292,17 @@ export function createEventHook(client: ForLoopAPIClient) {
265
292
  const { sessionID, messageID } = event.properties || {};
266
293
  if (!sessionID || !messageID) return;
267
294
  textBuffer.delete(messageID);
268
- client.removeMessage(messageID, sprintId, sessionID).catch((err: Error) => {
269
- console.warn('[ForLoop] Failed to remove message:', err.message);
295
+ const conversationId = buildConversationId(sprintId, 'unknown', sessionID);
296
+ client.writeConversationEvent({
297
+ operation: 'delete_turn',
298
+ sprintId,
299
+ targetAgent: 'forLoopPlanner',
300
+ actor: getActor(),
301
+ conversationId,
302
+ messageId: messageID,
303
+ metadata: { source: 'opencode-plugin' },
304
+ }).catch((err: Error) => {
305
+ console.warn('[ForLoop] Failed to delete conversation turn:', err.message);
270
306
  });
271
307
  break;
272
308
  }