happy-imou-cloud 2.0.12 → 2.0.13

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.
Files changed (35) hide show
  1. package/bin/happy-cloud.mjs +1 -1
  2. package/dist/ConversationHistory-V3VLmjJf.cjs +868 -0
  3. package/dist/ConversationHistory-_ciJNIgH.mjs +856 -0
  4. package/dist/{api-BxXBKBUy.mjs → api-D1meoL-9.mjs} +2 -2
  5. package/dist/{api-B4g8VLUn.cjs → api-DH5-IqeM.cjs} +2 -2
  6. package/dist/{command-CHiLfBa4.mjs → command-CMvWClny.mjs} +3 -3
  7. package/dist/{command-DVt_YmE6.cjs → command-Ch8Dgidj.cjs} +3 -3
  8. package/dist/createKeepAliveController-C5cQlDRr.mjs +51 -0
  9. package/dist/createKeepAliveController-DO8H6d5E.cjs +54 -0
  10. package/dist/{index-CWom7mSf.cjs → index-CryJfCh5.cjs} +10 -11
  11. package/dist/{index-DaAkW0VN.mjs → index-Cxrx9m5D.mjs} +9 -9
  12. package/dist/index.cjs +3 -3
  13. package/dist/index.mjs +3 -3
  14. package/dist/lib.cjs +1 -1
  15. package/dist/lib.mjs +1 -1
  16. package/dist/{persistence-8pNEvzaq.mjs → persistence-9Iu0wGNM.mjs} +1 -1
  17. package/dist/{persistence-DScOANDE.cjs → persistence-Bl3FYvwd.cjs} +1 -1
  18. package/dist/{registerKillSessionHandler-CNNguWyD.mjs → registerKillSessionHandler-BElGmD1E.mjs} +5 -541
  19. package/dist/{registerKillSessionHandler-Dr1inhTc.cjs → registerKillSessionHandler-BjkY-oUn.cjs} +4 -549
  20. package/dist/{runClaude-h-8llTrI.cjs → runClaude-CDZxAF3l.cjs} +129 -630
  21. package/dist/{runClaude-BcvOkIwh.mjs → runClaude-D7dF4RDM.mjs} +126 -627
  22. package/dist/{runCodex-CA58KUHf.cjs → runCodex-Cik8VzFs.cjs} +224 -17
  23. package/dist/{runCodex-ClJUgipy.mjs → runCodex-DnGz1XES.mjs} +213 -6
  24. package/dist/{runGemini-dAr7Gcn8.mjs → runGemini-B8tXMHeL.mjs} +5 -5
  25. package/dist/{runGemini-IFHhFMSU.cjs → runGemini-BM2BQ4I7.cjs} +13 -13
  26. package/package.json +9 -9
  27. package/scripts/build.mjs +66 -66
  28. package/scripts/devtools/README.md +9 -9
  29. package/scripts/e2e/fake-codex-acp-agent.mjs +139 -139
  30. package/scripts/e2e/local-server-session-roundtrip.mjs +1063 -1063
  31. package/scripts/release-smoke.mjs +202 -202
  32. package/dist/BaseReasoningProcessor-BrKUKAOr.cjs +0 -323
  33. package/dist/BaseReasoningProcessor-DrHf5B98.mjs +0 -320
  34. package/dist/ProviderSelectionHandler-BCDvmifJ.cjs +0 -265
  35. package/dist/ProviderSelectionHandler-BuZarTDc.mjs +0 -261
@@ -0,0 +1,868 @@
1
+ 'use strict';
2
+
3
+ var index = require('./index-CryJfCh5.cjs');
4
+ var api = require('./api-DH5-IqeM.cjs');
5
+ var registerKillSessionHandler = require('./registerKillSessionHandler-BjkY-oUn.cjs');
6
+ var node_events = require('node:events');
7
+ var node_crypto = require('node:crypto');
8
+
9
+ function createOfflineSessionStub(sessionTag) {
10
+ const emitter = new node_events.EventEmitter();
11
+ let metadata = null;
12
+ let agentState = null;
13
+ const stub = {
14
+ sessionId: `offline-${sessionTag}`,
15
+ sendCodexMessage: () => {
16
+ },
17
+ sendAgentMessage: () => {
18
+ },
19
+ sendClaudeSessionMessage: () => {
20
+ },
21
+ keepAlive: () => {
22
+ },
23
+ sendSessionEvent: () => {
24
+ },
25
+ sendSessionDeath: () => {
26
+ },
27
+ updateLifecycleState: () => {
28
+ },
29
+ requestControlTransfer: async () => {
30
+ },
31
+ flush: async () => {
32
+ },
33
+ close: async () => {
34
+ },
35
+ updateMetadata: (handler) => {
36
+ metadata = handler(metadata || {});
37
+ emitter.emit("metadata-updated", metadata);
38
+ },
39
+ updateAgentState: (handler) => {
40
+ agentState = handler(agentState || {});
41
+ emitter.emit("agent-state-updated", agentState);
42
+ },
43
+ getMetadataSnapshot: () => metadata,
44
+ getAgentStateSnapshot: () => agentState,
45
+ waitForMetadataUpdate: async () => metadata,
46
+ onUserMessage: () => {
47
+ },
48
+ rpcHandlerManager: {
49
+ registerHandler: () => {
50
+ }
51
+ },
52
+ on: emitter.on.bind(emitter),
53
+ once: emitter.once.bind(emitter),
54
+ off: emitter.off.bind(emitter),
55
+ emit: emitter.emit.bind(emitter)
56
+ };
57
+ return stub;
58
+ }
59
+
60
+ function setupOfflineReconnection(opts) {
61
+ const { api: api$1, sessionTag, metadata, state, response, onSessionSwap } = opts;
62
+ let session;
63
+ let reconnectionHandle = null;
64
+ if (!response) {
65
+ session = createOfflineSessionStub(sessionTag);
66
+ reconnectionHandle = api.startOfflineReconnection({
67
+ serverUrl: api.configuration.serverUrl,
68
+ onReconnected: async () => {
69
+ const resp = await api$1.getOrCreateSession({ tag: sessionTag, metadata, state });
70
+ if (!resp) throw new Error("Server unavailable");
71
+ const realSession = api$1.sessionSyncClient(resp);
72
+ onSessionSwap(realSession);
73
+ return realSession;
74
+ },
75
+ onNotify: (msg) => {
76
+ console.log(msg);
77
+ }
78
+ });
79
+ return { session, reconnectionHandle, isOffline: true };
80
+ } else {
81
+ session = api$1.sessionSyncClient(response);
82
+ return { session, reconnectionHandle: null, isOffline: false };
83
+ }
84
+ }
85
+
86
+ async function bootstrapManagedProviderSession(opts) {
87
+ const { state, metadata } = registerKillSessionHandler.createSessionMetadata({
88
+ flavor: opts.flavor,
89
+ machineId: opts.machineId,
90
+ startedBy: opts.startedBy
91
+ });
92
+ let response = null;
93
+ try {
94
+ response = await opts.api.getOrCreateSession({
95
+ tag: opts.sessionTag,
96
+ metadata,
97
+ state
98
+ });
99
+ } catch (error) {
100
+ if (!api.isAuthenticationRequiredError(error)) {
101
+ throw error;
102
+ }
103
+ api.logger.debug(opts.authFallbackLogMessage);
104
+ }
105
+ const { session, reconnectionHandle } = setupOfflineReconnection({
106
+ api: opts.api,
107
+ sessionTag: opts.sessionTag,
108
+ metadata,
109
+ state,
110
+ response,
111
+ onSessionSwap: (newSession) => {
112
+ opts.onSessionSwap?.(newSession, metadata);
113
+ void index.publishSessionRegistration(newSession.sessionId, metadata);
114
+ }
115
+ });
116
+ if (response) {
117
+ await index.publishSessionRegistration(response.id, metadata);
118
+ }
119
+ return {
120
+ state,
121
+ metadata,
122
+ response,
123
+ session,
124
+ reconnectionHandle
125
+ };
126
+ }
127
+
128
+ async function launchRuntimeHandleWithFactoryResult(opts) {
129
+ const shell = opts.shell ?? new index.RuntimeShell();
130
+ let factoryResult;
131
+ const session = await shell.launch({
132
+ provider: opts.provider,
133
+ cwd: opts.cwd,
134
+ env: opts.env,
135
+ createBackend: (factoryOpts) => {
136
+ factoryResult = opts.createBackendResult(factoryOpts);
137
+ return factoryResult.backend;
138
+ }
139
+ });
140
+ if (factoryResult === void 0) {
141
+ throw new Error(`Runtime provider "${opts.provider}" did not create a backend result`);
142
+ }
143
+ return {
144
+ session,
145
+ factoryResult
146
+ };
147
+ }
148
+
149
+ function inferToolResultError(result) {
150
+ if (!result || typeof result !== "object") {
151
+ return false;
152
+ }
153
+ const record = result;
154
+ if (record.isError === true || record.is_error === true) {
155
+ return true;
156
+ }
157
+ if (typeof record.error === "string" && record.error.trim().length > 0) {
158
+ return true;
159
+ }
160
+ if (record.success === false) {
161
+ return true;
162
+ }
163
+ const status = typeof record.status === "string" ? record.status.toLowerCase() : "";
164
+ return status === "failed" || status === "cancelled" || status === "error" || status === "denied" || status === "aborted";
165
+ }
166
+
167
+ function buildToolHappierMetaV2(input) {
168
+ return {
169
+ ...input,
170
+ v: 2
171
+ };
172
+ }
173
+ function attachToolHappierMetaV2(value, meta) {
174
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
175
+ return value;
176
+ }
177
+ return {
178
+ ...value,
179
+ _happier: buildToolHappierMetaV2(meta)
180
+ };
181
+ }
182
+
183
+ function getDefaultExecToolName(provider) {
184
+ switch (provider) {
185
+ case "claude":
186
+ return "ClaudeBash";
187
+ case "codex":
188
+ return "CodexBash";
189
+ case "gemini":
190
+ return "GeminiBash";
191
+ }
192
+ }
193
+ function getDefaultPatchToolName(provider) {
194
+ switch (provider) {
195
+ case "claude":
196
+ return "ClaudePatch";
197
+ case "codex":
198
+ return "CodexPatch";
199
+ case "gemini":
200
+ return "GeminiPatch";
201
+ }
202
+ }
203
+ function attachToolMeta(provider, rawToolName, value) {
204
+ const canonicalToolName = index.resolveCanonicalToolNameV2(rawToolName);
205
+ return attachToolHappierMetaV2(value, {
206
+ v: 2,
207
+ protocol: "acp",
208
+ provider,
209
+ rawToolName,
210
+ canonicalToolName
211
+ });
212
+ }
213
+ function forwardAgentMessageToProviderSession(msg, options) {
214
+ const createId = options.createId ?? node_crypto.randomUUID;
215
+ const toolResultType = options.toolResultType ?? "tool-result";
216
+ switch (msg.type) {
217
+ case "tool-call": {
218
+ options.send({
219
+ type: "tool-call",
220
+ name: index.resolveCanonicalToolNameV2(msg.toolName),
221
+ callId: msg.callId,
222
+ input: attachToolMeta(options.provider, msg.toolName, msg.args),
223
+ id: createId()
224
+ });
225
+ return true;
226
+ }
227
+ case "tool-result": {
228
+ options.send({
229
+ type: toolResultType,
230
+ callId: msg.callId,
231
+ output: attachToolMeta(options.provider, msg.toolName, msg.result),
232
+ id: createId(),
233
+ isError: inferToolResultError(msg.result)
234
+ });
235
+ return true;
236
+ }
237
+ case "fs-edit": {
238
+ options.send({
239
+ type: "file-edit",
240
+ description: msg.description,
241
+ diff: msg.diff,
242
+ filePath: msg.path || "unknown",
243
+ id: createId()
244
+ });
245
+ return true;
246
+ }
247
+ case "terminal-output": {
248
+ options.send({
249
+ type: "terminal-output",
250
+ data: msg.data,
251
+ callId: msg.callId ?? createId()
252
+ });
253
+ return true;
254
+ }
255
+ case "permission-request": {
256
+ const payload = msg.payload && typeof msg.payload === "object" ? msg.payload : {};
257
+ options.send({
258
+ type: "permission-request",
259
+ permissionId: msg.id,
260
+ toolName: typeof payload.toolName === "string" ? payload.toolName : msg.reason || "unknown",
261
+ description: msg.reason || (typeof payload.toolName === "string" ? payload.toolName : ""),
262
+ options: payload
263
+ });
264
+ return true;
265
+ }
266
+ case "exec-approval-request": {
267
+ const rawToolName = options.execToolName ?? getDefaultExecToolName(options.provider);
268
+ const { call_id, type: _type, ...inputs } = msg;
269
+ options.send({
270
+ type: "tool-call",
271
+ name: index.resolveCanonicalToolNameV2(rawToolName),
272
+ callId: call_id,
273
+ input: attachToolMeta(options.provider, rawToolName, inputs),
274
+ id: createId()
275
+ });
276
+ return true;
277
+ }
278
+ case "patch-apply-begin": {
279
+ const rawToolName = options.patchToolName ?? getDefaultPatchToolName(options.provider);
280
+ options.send({
281
+ type: "tool-call",
282
+ name: index.resolveCanonicalToolNameV2(rawToolName),
283
+ callId: msg.call_id,
284
+ input: attachToolMeta(options.provider, rawToolName, {
285
+ auto_approved: msg.auto_approved,
286
+ changes: msg.changes
287
+ }),
288
+ id: createId()
289
+ });
290
+ return true;
291
+ }
292
+ case "patch-apply-end": {
293
+ const rawToolName = options.patchToolName ?? getDefaultPatchToolName(options.provider);
294
+ options.send({
295
+ type: toolResultType,
296
+ callId: msg.call_id,
297
+ output: attachToolMeta(options.provider, rawToolName, {
298
+ stdout: msg.stdout,
299
+ stderr: msg.stderr,
300
+ success: msg.success
301
+ }),
302
+ id: createId(),
303
+ isError: !msg.success
304
+ });
305
+ return true;
306
+ }
307
+ case "token-count": {
308
+ const { type: _type, ...payload } = msg;
309
+ options.send({
310
+ type: "token_count",
311
+ ...payload,
312
+ id: createId()
313
+ });
314
+ return true;
315
+ }
316
+ default:
317
+ return false;
318
+ }
319
+ }
320
+
321
+ function createAbortError() {
322
+ const error = new Error("Operation aborted");
323
+ error.name = "AbortError";
324
+ return error;
325
+ }
326
+ async function waitForResponseCompleteWithAbort(backend, signal, timeoutMs = 12e4) {
327
+ if (!backend.waitForResponseComplete) {
328
+ return;
329
+ }
330
+ if (signal.aborted) {
331
+ throw createAbortError();
332
+ }
333
+ await new Promise((resolve, reject) => {
334
+ const onAbort = () => reject(createAbortError());
335
+ signal.addEventListener("abort", onAbort, { once: true });
336
+ backend.waitForResponseComplete(timeoutMs).then(resolve).catch(reject).finally(() => {
337
+ signal.removeEventListener("abort", onAbort);
338
+ });
339
+ });
340
+ }
341
+
342
+ const INTERACTION_SUPERSEDED_ERROR = "Interaction superseded by new user message";
343
+ const INTERACTION_TIMED_OUT_ERROR = "Interaction timed out waiting for user response";
344
+ const DEFAULT_INTERACTION_TIMEOUT_MS = 2 * 60 * 1e3;
345
+ function getPendingInteractionTimeoutMs() {
346
+ const raw = Number(process.env.HAPPY_INTERACTION_TIMEOUT_MS);
347
+ if (Number.isFinite(raw) && raw > 0) {
348
+ return raw;
349
+ }
350
+ return DEFAULT_INTERACTION_TIMEOUT_MS;
351
+ }
352
+ class BasePermissionHandler {
353
+ pendingRequests = /* @__PURE__ */ new Map();
354
+ session;
355
+ isResetting = false;
356
+ constructor(session) {
357
+ this.session = session;
358
+ this.setupRpcHandler();
359
+ }
360
+ /**
361
+ * Update the session reference (used after offline reconnection swaps sessions).
362
+ * This is critical for avoiding stale session references after onSessionSwap.
363
+ */
364
+ updateSession(newSession) {
365
+ api.logger.debug(`${this.getLogPrefix()} Session reference updated`);
366
+ this.session = newSession;
367
+ this.setupRpcHandler();
368
+ }
369
+ /**
370
+ * Setup RPC handler for permission responses.
371
+ */
372
+ setupRpcHandler() {
373
+ this.session.rpcHandlerManager.registerHandler(
374
+ "permission",
375
+ async (response) => {
376
+ const pending = this.pendingRequests.get(response.id);
377
+ if (!pending) {
378
+ api.logger.debug(`${this.getLogPrefix()} Permission request not found or already resolved`);
379
+ return;
380
+ }
381
+ this.pendingRequests.delete(response.id);
382
+ this.clearPendingRequestTimeout(pending);
383
+ const result = response.approved ? { decision: response.decision === "approved_for_session" ? "approved_for_session" : "approved" } : { decision: response.decision === "denied" ? "denied" : "abort" };
384
+ pending.resolve(result);
385
+ this.session.updateAgentState((currentState) => {
386
+ const request = currentState.requests?.[response.id];
387
+ if (!request) return currentState;
388
+ const { [response.id]: _, ...remainingRequests } = currentState.requests || {};
389
+ let res = {
390
+ ...currentState,
391
+ requests: remainingRequests,
392
+ completedRequests: {
393
+ ...currentState.completedRequests,
394
+ [response.id]: {
395
+ ...request,
396
+ completedAt: Date.now(),
397
+ status: response.approved ? "approved" : "denied",
398
+ decision: result.decision
399
+ }
400
+ }
401
+ };
402
+ return res;
403
+ });
404
+ api.logger.debug(`${this.getLogPrefix()} Permission ${response.approved ? "approved" : "denied"} for ${pending.toolName}`);
405
+ }
406
+ );
407
+ }
408
+ /**
409
+ * Add a pending request to the agent state.
410
+ */
411
+ addPendingRequestToState(toolCallId, toolName, input) {
412
+ this.session.updateAgentState((currentState) => ({
413
+ ...currentState,
414
+ requests: {
415
+ ...currentState.requests,
416
+ [toolCallId]: {
417
+ tool: toolName,
418
+ arguments: input,
419
+ createdAt: Date.now()
420
+ }
421
+ }
422
+ }));
423
+ }
424
+ registerPendingRequest(toolCallId, toolName, input, logSuffix = "") {
425
+ return new Promise((resolve, reject) => {
426
+ const pending = {
427
+ resolve,
428
+ reject,
429
+ toolName,
430
+ input
431
+ };
432
+ pending.timeoutHandle = setTimeout(() => {
433
+ this.handlePendingRequestTimeout(toolCallId, pending);
434
+ }, getPendingInteractionTimeoutMs());
435
+ this.pendingRequests.set(toolCallId, pending);
436
+ this.addPendingRequestToState(toolCallId, toolName, input);
437
+ api.logger.debug(`${this.getLogPrefix()} Permission request sent for tool: ${toolName} (${toolCallId})${logSuffix}`);
438
+ });
439
+ }
440
+ hasPendingRequests() {
441
+ return this.pendingRequests.size > 0;
442
+ }
443
+ supersedePendingRequests(reason = INTERACTION_SUPERSEDED_ERROR) {
444
+ const pendingSnapshot = Array.from(this.pendingRequests.entries());
445
+ if (pendingSnapshot.length === 0) {
446
+ return 0;
447
+ }
448
+ this.pendingRequests.clear();
449
+ const completedAt = Date.now();
450
+ for (const [, pending] of pendingSnapshot) {
451
+ this.clearPendingRequestTimeout(pending);
452
+ pending.resolve({ decision: "abort" });
453
+ }
454
+ this.session.updateAgentState((currentState) => {
455
+ const requests = { ...currentState.requests || {} };
456
+ const completedRequests = { ...currentState.completedRequests || {} };
457
+ for (const [id, request] of Object.entries(requests)) {
458
+ if (request.requestKind === "selection") {
459
+ continue;
460
+ }
461
+ completedRequests[id] = {
462
+ ...request,
463
+ completedAt,
464
+ status: "denied",
465
+ reason,
466
+ decision: "abort",
467
+ requestKind: request.requestKind || "permission"
468
+ };
469
+ delete requests[id];
470
+ }
471
+ return {
472
+ ...currentState,
473
+ requests,
474
+ completedRequests
475
+ };
476
+ });
477
+ api.logger.debug(`${this.getLogPrefix()} Superseded ${pendingSnapshot.length} pending permission request(s)`);
478
+ return pendingSnapshot.length;
479
+ }
480
+ /**
481
+ * Reset state for new sessions.
482
+ * This method is idempotent - safe to call multiple times.
483
+ */
484
+ reset() {
485
+ if (this.isResetting) {
486
+ api.logger.debug(`${this.getLogPrefix()} Reset already in progress, skipping`);
487
+ return;
488
+ }
489
+ this.isResetting = true;
490
+ try {
491
+ const pendingSnapshot = Array.from(this.pendingRequests.entries());
492
+ this.pendingRequests.clear();
493
+ for (const [id, pending] of pendingSnapshot) {
494
+ try {
495
+ this.clearPendingRequestTimeout(pending);
496
+ pending.reject(new Error("Session reset"));
497
+ } catch (err) {
498
+ api.logger.debug(`${this.getLogPrefix()} Error rejecting pending request ${id}:`, err);
499
+ }
500
+ }
501
+ this.session.updateAgentState((currentState) => {
502
+ const pendingRequests = currentState.requests || {};
503
+ const completedRequests = { ...currentState.completedRequests };
504
+ for (const [id, request] of Object.entries(pendingRequests)) {
505
+ completedRequests[id] = {
506
+ ...request,
507
+ completedAt: Date.now(),
508
+ status: "canceled",
509
+ reason: "Session reset"
510
+ };
511
+ }
512
+ return {
513
+ ...currentState,
514
+ requests: {},
515
+ completedRequests
516
+ };
517
+ });
518
+ api.logger.debug(`${this.getLogPrefix()} Permission handler reset`);
519
+ } finally {
520
+ this.isResetting = false;
521
+ }
522
+ }
523
+ clearPendingRequestTimeout(pending) {
524
+ if (pending?.timeoutHandle) {
525
+ clearTimeout(pending.timeoutHandle);
526
+ pending.timeoutHandle = void 0;
527
+ }
528
+ }
529
+ handlePendingRequestTimeout(toolCallId, pending) {
530
+ const active = this.pendingRequests.get(toolCallId);
531
+ if (!active || active !== pending) {
532
+ return;
533
+ }
534
+ this.pendingRequests.delete(toolCallId);
535
+ this.clearPendingRequestTimeout(active);
536
+ active.resolve({ decision: "abort" });
537
+ this.session.updateAgentState((currentState) => {
538
+ const request = currentState.requests?.[toolCallId] || {
539
+ tool: active.toolName,
540
+ arguments: active.input,
541
+ createdAt: Date.now(),
542
+ requestKind: "permission"
543
+ };
544
+ const { [toolCallId]: _, ...remainingRequests } = currentState.requests || {};
545
+ return {
546
+ ...currentState,
547
+ requests: remainingRequests,
548
+ completedRequests: {
549
+ ...currentState.completedRequests,
550
+ [toolCallId]: {
551
+ ...request,
552
+ completedAt: Date.now(),
553
+ status: "canceled",
554
+ reason: INTERACTION_TIMED_OUT_ERROR,
555
+ decision: "abort",
556
+ requestKind: request.requestKind || "permission"
557
+ }
558
+ }
559
+ };
560
+ });
561
+ this.session.sendSessionEvent({
562
+ type: "message",
563
+ message: "Pending interaction timed out waiting for a response. Send a new message to continue."
564
+ });
565
+ api.logger.debug(`${this.getLogPrefix()} Permission request timed out for ${active.toolName} (${toolCallId})`);
566
+ }
567
+ }
568
+
569
+ class BaseReasoningProcessor {
570
+ accumulator = "";
571
+ inTitleCapture = false;
572
+ titleBuffer = "";
573
+ contentBuffer = "";
574
+ hasTitle = false;
575
+ currentCallId = null;
576
+ toolCallStarted = false;
577
+ currentTitle = null;
578
+ onMessage = null;
579
+ constructor(onMessage) {
580
+ this.onMessage = onMessage || null;
581
+ this.reset();
582
+ }
583
+ /**
584
+ * Set the message callback for sending messages directly.
585
+ */
586
+ setMessageCallback(callback) {
587
+ this.onMessage = callback;
588
+ }
589
+ /**
590
+ * Process a reasoning section break - indicates a new reasoning section is starting.
591
+ */
592
+ handleSectionBreak() {
593
+ this.finishCurrentToolCall("canceled");
594
+ this.resetState();
595
+ api.logger.debug(`${this.getLogPrefix()} Section break - reset state`);
596
+ }
597
+ /**
598
+ * Process a reasoning delta/chunk and accumulate content.
599
+ */
600
+ processInput(input) {
601
+ this.accumulator += input;
602
+ if (!this.inTitleCapture && !this.hasTitle && !this.contentBuffer) {
603
+ if (this.accumulator.startsWith("**")) {
604
+ this.inTitleCapture = true;
605
+ this.titleBuffer = this.accumulator.substring(2);
606
+ api.logger.debug(`${this.getLogPrefix()} Started title capture`);
607
+ } else if (this.accumulator.length > 0) {
608
+ this.contentBuffer = this.accumulator;
609
+ }
610
+ } else if (this.inTitleCapture) {
611
+ this.titleBuffer = this.accumulator.substring(2);
612
+ const titleEndIndex = this.titleBuffer.indexOf("**");
613
+ if (titleEndIndex !== -1) {
614
+ const title = this.titleBuffer.substring(0, titleEndIndex);
615
+ const afterTitle = this.titleBuffer.substring(titleEndIndex + 2);
616
+ this.hasTitle = true;
617
+ this.inTitleCapture = false;
618
+ this.currentTitle = title;
619
+ this.contentBuffer = afterTitle;
620
+ this.currentCallId = node_crypto.randomUUID();
621
+ api.logger.debug(`${this.getLogPrefix()} Title captured: "${title}"`);
622
+ this.sendToolCallStart(title);
623
+ }
624
+ } else if (this.hasTitle) {
625
+ const titleStartIndex = this.accumulator.indexOf("**");
626
+ if (titleStartIndex !== -1) {
627
+ this.contentBuffer = this.accumulator.substring(
628
+ titleStartIndex + 2 + this.currentTitle.length + 2
629
+ );
630
+ }
631
+ } else {
632
+ this.contentBuffer = this.accumulator;
633
+ }
634
+ }
635
+ /**
636
+ * Send the tool call start message.
637
+ */
638
+ sendToolCallStart(title) {
639
+ if (!this.currentCallId || this.toolCallStarted) {
640
+ return;
641
+ }
642
+ const toolCall = {
643
+ type: "tool-call",
644
+ name: this.getToolName(),
645
+ callId: this.currentCallId,
646
+ input: {
647
+ title
648
+ },
649
+ id: node_crypto.randomUUID()
650
+ };
651
+ api.logger.debug(`${this.getLogPrefix()} Sending tool call start for: "${title}"`);
652
+ this.onMessage?.(toolCall);
653
+ this.toolCallStarted = true;
654
+ }
655
+ /**
656
+ * Complete the reasoning section.
657
+ * Returns true if reasoning was completed, false if there was nothing to complete.
658
+ */
659
+ completeReasoning(fullText) {
660
+ const text = fullText ?? this.accumulator;
661
+ if (!text.trim() && !this.toolCallStarted) {
662
+ api.logger.debug(`${this.getLogPrefix()} Complete called but no content accumulated, skipping`);
663
+ return false;
664
+ }
665
+ let title;
666
+ let content = text;
667
+ if (text.startsWith("**")) {
668
+ const titleEndIndex = text.indexOf("**", 2);
669
+ if (titleEndIndex !== -1) {
670
+ title = text.substring(2, titleEndIndex);
671
+ content = text.substring(titleEndIndex + 2).trim();
672
+ }
673
+ }
674
+ api.logger.debug(`${this.getLogPrefix()} Complete reasoning - Title: "${title}", Has content: ${content.length > 0}`);
675
+ if (title && !this.toolCallStarted) {
676
+ this.currentCallId = this.currentCallId || node_crypto.randomUUID();
677
+ this.sendToolCallStart(title);
678
+ }
679
+ if (this.toolCallStarted && this.currentCallId) {
680
+ const toolResult = {
681
+ type: "tool-call-result",
682
+ callId: this.currentCallId,
683
+ output: {
684
+ content,
685
+ status: "completed"
686
+ },
687
+ id: node_crypto.randomUUID()
688
+ };
689
+ api.logger.debug(`${this.getLogPrefix()} Sending tool call result`);
690
+ this.onMessage?.(toolResult);
691
+ } else if (content.trim()) {
692
+ const reasoningMessage = {
693
+ type: "reasoning",
694
+ message: content,
695
+ id: node_crypto.randomUUID()
696
+ };
697
+ api.logger.debug(`${this.getLogPrefix()} Sending reasoning message`);
698
+ this.onMessage?.(reasoningMessage);
699
+ }
700
+ this.resetState();
701
+ return true;
702
+ }
703
+ /**
704
+ * Abort the current reasoning section.
705
+ */
706
+ abort() {
707
+ api.logger.debug(`${this.getLogPrefix()} Abort called`);
708
+ this.finishCurrentToolCall("canceled");
709
+ this.resetState();
710
+ }
711
+ /**
712
+ * Reset the processor state.
713
+ */
714
+ reset() {
715
+ this.finishCurrentToolCall("canceled");
716
+ this.resetState();
717
+ }
718
+ /**
719
+ * Finish current tool call if one is in progress.
720
+ */
721
+ finishCurrentToolCall(status) {
722
+ if (this.toolCallStarted && this.currentCallId) {
723
+ const toolResult = {
724
+ type: "tool-call-result",
725
+ callId: this.currentCallId,
726
+ output: {
727
+ content: this.contentBuffer || "",
728
+ status
729
+ },
730
+ id: node_crypto.randomUUID()
731
+ };
732
+ api.logger.debug(`${this.getLogPrefix()} Sending tool call result with status: ${status}`);
733
+ this.onMessage?.(toolResult);
734
+ }
735
+ }
736
+ /**
737
+ * Reset internal state.
738
+ */
739
+ resetState() {
740
+ this.accumulator = "";
741
+ this.inTitleCapture = false;
742
+ this.titleBuffer = "";
743
+ this.contentBuffer = "";
744
+ this.hasTitle = false;
745
+ this.currentCallId = null;
746
+ this.toolCallStarted = false;
747
+ this.currentTitle = null;
748
+ }
749
+ /**
750
+ * Get the current call ID for tool result matching.
751
+ */
752
+ getCurrentCallId() {
753
+ return this.currentCallId;
754
+ }
755
+ /**
756
+ * Check if a tool call has been started.
757
+ */
758
+ hasStartedToolCall() {
759
+ return this.toolCallStarted;
760
+ }
761
+ }
762
+
763
+ let ConversationHistory$1 = class ConversationHistory {
764
+ messages = [];
765
+ maxMessages;
766
+ maxCharacters;
767
+ constructor(options = {}) {
768
+ this.maxMessages = options.maxMessages ?? 20;
769
+ this.maxCharacters = options.maxCharacters ?? 5e4;
770
+ }
771
+ isDuplicate(role, content) {
772
+ if (this.messages.length === 0) {
773
+ return false;
774
+ }
775
+ for (let index = this.messages.length - 1; index >= 0; index -= 1) {
776
+ const message = this.messages[index];
777
+ if (message.role !== role) {
778
+ continue;
779
+ }
780
+ const normalizedIncoming = content.trim().replace(/\s+/g, " ");
781
+ const normalizedExisting = message.content.replace(/\s+/g, " ");
782
+ return normalizedIncoming === normalizedExisting;
783
+ }
784
+ return false;
785
+ }
786
+ addUserMessage(content) {
787
+ this.addMessage("user", content);
788
+ }
789
+ addAssistantMessage(content) {
790
+ this.addMessage("assistant", content);
791
+ }
792
+ hasHistory() {
793
+ return this.messages.length > 0;
794
+ }
795
+ size() {
796
+ return this.messages.length;
797
+ }
798
+ clear() {
799
+ this.messages = [];
800
+ api.logger.debug("[ConversationHistory] History cleared");
801
+ }
802
+ getContextForNewSession(prefixMessage = "Continue from the prior session using the conversation below as context.") {
803
+ if (this.messages.length === 0) {
804
+ return "";
805
+ }
806
+ const formattedMessages = this.messages.map((message) => {
807
+ const role = message.role === "user" ? "User" : "Assistant";
808
+ const content = message.content.length > 2e3 ? `${message.content.slice(0, 2e3)}... [truncated]` : message.content;
809
+ return `${role}: ${content}`;
810
+ }).join("\n\n");
811
+ return [
812
+ "[PREVIOUS CONVERSATION CONTEXT]",
813
+ prefixMessage,
814
+ "",
815
+ formattedMessages,
816
+ "",
817
+ "[END OF PREVIOUS CONTEXT]",
818
+ ""
819
+ ].join("\n");
820
+ }
821
+ getSummary() {
822
+ const totalChars = this.messages.reduce((sum, message) => sum + message.content.length, 0);
823
+ const userCount = this.messages.filter((message) => message.role === "user").length;
824
+ const assistantCount = this.messages.filter((message) => message.role === "assistant").length;
825
+ return `${this.messages.length} messages (${userCount} user, ${assistantCount} assistant), ${totalChars} chars`;
826
+ }
827
+ addMessage(role, content) {
828
+ const trimmedContent = content.trim();
829
+ if (!trimmedContent) {
830
+ return;
831
+ }
832
+ if (this.isDuplicate(role, trimmedContent)) {
833
+ api.logger.debug(`[ConversationHistory] Skipping duplicate ${role} message (${trimmedContent.length} chars)`);
834
+ return;
835
+ }
836
+ this.messages.push({
837
+ role,
838
+ content: trimmedContent,
839
+ timestamp: Date.now()
840
+ });
841
+ this.trimHistory();
842
+ api.logger.debug(`[ConversationHistory] Added ${role} message (${trimmedContent.length} chars), total: ${this.messages.length}`);
843
+ }
844
+ trimHistory() {
845
+ while (this.messages.length > this.maxMessages) {
846
+ this.messages.shift();
847
+ }
848
+ let totalChars = this.messages.reduce((sum, message) => sum + message.content.length, 0);
849
+ while (totalChars > this.maxCharacters && this.messages.length > 1) {
850
+ const removed = this.messages.shift();
851
+ if (removed) {
852
+ totalChars -= removed.content.length;
853
+ }
854
+ }
855
+ }
856
+ };
857
+
858
+ exports.BasePermissionHandler = BasePermissionHandler;
859
+ exports.BaseReasoningProcessor = BaseReasoningProcessor;
860
+ exports.ConversationHistory = ConversationHistory$1;
861
+ exports.INTERACTION_SUPERSEDED_ERROR = INTERACTION_SUPERSEDED_ERROR;
862
+ exports.INTERACTION_TIMED_OUT_ERROR = INTERACTION_TIMED_OUT_ERROR;
863
+ exports.bootstrapManagedProviderSession = bootstrapManagedProviderSession;
864
+ exports.forwardAgentMessageToProviderSession = forwardAgentMessageToProviderSession;
865
+ exports.getPendingInteractionTimeoutMs = getPendingInteractionTimeoutMs;
866
+ exports.inferToolResultError = inferToolResultError;
867
+ exports.launchRuntimeHandleWithFactoryResult = launchRuntimeHandleWithFactoryResult;
868
+ exports.waitForResponseCompleteWithAbort = waitForResponseCompleteWithAbort;