@shumkov/orchestra 0.4.2 → 0.5.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.
@@ -122,6 +122,9 @@ const ToolAckMessageSchema = z.object({
122
122
  tool_call_id: ToolCallId,
123
123
  ok: z.boolean(),
124
124
  error: z.string().optional(),
125
+ attempt_id: NonEmptyString.optional(),
126
+ delivery: z.enum(['sent', 'replayed']).optional(),
127
+ replay_of: NonEmptyString.optional(),
125
128
  // 0.13: the delivered Telegram message_id, surfaced back to claude so it can
126
129
  // `edit_message` that bubble for progressive status. Present on a successful
127
130
  // `reply`/`edit_message` ack; absent on errors / re-acks.
@@ -173,6 +176,48 @@ function parseDaemonToBridgeMessage(raw) {
173
176
  return { ok: false, error: zodErrorBrief(r.error, raw?.kind) };
174
177
  }
175
178
 
179
+ function formatToolAckResult({ ack, error, toolCallId }) {
180
+ if (error) {
181
+ return {
182
+ content: [{
183
+ type: 'text',
184
+ text: JSON.stringify({
185
+ ok: false,
186
+ error: error.message,
187
+ timeout: error.code === 'TOOL_ACK_TIMEOUT',
188
+ attempt_id: toolCallId,
189
+ }),
190
+ }],
191
+ isError: true,
192
+ };
193
+ }
194
+ if (ack?.ok !== true) {
195
+ return {
196
+ content: [{
197
+ type: 'text',
198
+ text: JSON.stringify({
199
+ ok: false,
200
+ error: ack?.error || 'daemon rejected delivery',
201
+ attempt_id: ack?.attempt_id || toolCallId,
202
+ }),
203
+ }],
204
+ isError: true,
205
+ };
206
+ }
207
+ return {
208
+ content: [{
209
+ type: 'text',
210
+ text: JSON.stringify({
211
+ ok: true,
212
+ message_id: ack.message_id ?? null,
213
+ attempt_id: ack.attempt_id,
214
+ delivery: ack.delivery,
215
+ ...(ack.replay_of ? { replay_of: ack.replay_of } : {}),
216
+ }),
217
+ }],
218
+ };
219
+ }
220
+
176
221
  function zodErrorBrief(err, kindHint) {
177
222
  const issues = (err?.issues || []).slice(0, 3).map(i => `${i.path.join('.')}: ${i.message}`);
178
223
  return `kind=${kindHint || '?'} — ${issues.join('; ') || 'unknown'}`;
@@ -197,4 +242,5 @@ module.exports = {
197
242
  // helpers
198
243
  parseBridgeToDaemonMessage,
199
244
  parseDaemonToBridgeMessage,
245
+ formatToolAckResult,
200
246
  };
@@ -30,7 +30,10 @@ import { ListToolsRequestSchema, CallToolRequestSchema } from '@modelcontextprot
30
30
  // string "undefined" into Claude's prompt; a malformed tool_ack with
31
31
  // null tool_call_id silently no-op'd and the bridge timed out on
32
32
  // awaitToolAck → isError → Claude retry.
33
- import { parseDaemonToBridgeMessage } from './channels-bridge-protocol.js'
33
+ import {
34
+ formatToolAckResult,
35
+ parseDaemonToBridgeMessage,
36
+ } from './channels-bridge-protocol.js'
34
37
  import { z } from 'zod'
35
38
  import { connect } from 'node:net'
36
39
  import { randomUUID } from 'node:crypto'
@@ -103,20 +106,29 @@ function awaitToolAck(toolCallId) {
103
106
  return new Promise((resolve, reject) => {
104
107
  const timer = setTimeout(() => {
105
108
  pendingToolCalls.delete(toolCallId)
106
- reject(new Error('daemon ack timeout'))
109
+ const error = new Error('daemon ack timeout')
110
+ error.code = 'TOOL_ACK_TIMEOUT'
111
+ reject(error)
107
112
  }, TOOL_ACK_TIMEOUT_MS)
108
113
  pendingToolCalls.set(toolCallId, { resolve, reject, timer })
109
114
  })
110
115
  }
111
116
 
112
- function resolveToolAck(toolCallId, ok, error, messageId) {
113
- const p = pendingToolCalls.get(toolCallId)
117
+ function resolveToolAck(msg) {
118
+ const p = pendingToolCalls.get(msg.tool_call_id)
114
119
  if (!p) return
115
- pendingToolCalls.delete(toolCallId)
120
+ pendingToolCalls.delete(msg.tool_call_id)
116
121
  clearTimeout(p.timer)
117
122
  // 0.13: resolve with the delivered message_id so the CallTool handler can hand
118
123
  // it back to claude (for edit_message). null when the daemon didn't carry one.
119
- ok ? p.resolve({ message_id: messageId ?? null }) : p.reject(new Error(error || 'daemon rejected delivery'))
124
+ p.resolve({
125
+ ok: msg.ok,
126
+ error: msg.error,
127
+ message_id: msg.message_id ?? null,
128
+ attempt_id: msg.attempt_id,
129
+ delivery: msg.delivery,
130
+ replay_of: msg.replay_of,
131
+ })
120
132
  }
121
133
 
122
134
  // ─── 0.12 interactive questions: `ask` blocks for the user's answer ──
@@ -251,7 +263,7 @@ function handleDaemonMessage(msg) {
251
263
  break
252
264
 
253
265
  case 'tool_ack':
254
- resolveToolAck(msg.tool_call_id, msg.ok, msg.error, msg.message_id)
266
+ resolveToolAck(msg)
255
267
  break
256
268
 
257
269
  case 'question_answer':
@@ -321,7 +333,7 @@ mcp.setRequestHandler(ListToolsRequestSchema, async () => {
321
333
  'ALWAYS set consumed_turn_ids to the turn_id of EVERY <channel> message this ' +
322
334
  'reply answers or absorbs (including mid-turn follow-ups) — it is how polygram ' +
323
335
  'confirms delivery of follow-ups. ' +
324
- 'Returns {ok, message_id}: keep the message_id to update that bubble in place ' +
336
+ 'Returns {ok, message_id, attempt_id, delivery, replay_of?}: keep the message_id to update that bubble in place ' +
325
337
  'with `edit_message` (progressive status on long tasks).',
326
338
  inputSchema: {
327
339
  type: 'object',
@@ -440,12 +452,9 @@ mcp.setRequestHandler(CallToolRequestSchema, async req => {
440
452
  }) + '\n')
441
453
  try {
442
454
  const ack = await ackP
443
- // Return {ok, message_id} as JSON so claude can read the delivered bubble's
444
- // id and `edit_message` it later (progressive status). For a plain reply with
445
- // no id (solo sticker/reaction) message_id is null.
446
- return { content: [{ type: 'text', text: JSON.stringify({ ok: true, message_id: ack?.message_id ?? null }) }] }
455
+ return formatToolAckResult({ ack, toolCallId })
447
456
  } catch (err) {
448
- return { content: [{ type: 'text', text: `delivery failed: ${err.message}` }], isError: true }
457
+ return formatToolAckResult({ error: err, toolCallId })
449
458
  }
450
459
  })
451
460