@qlairoslabs/acp-client 0.11.1 → 0.12.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.
- package/CHANGELOG-consumer-fixes.md +55 -0
- package/CHANGELOG-operation-lifecycle.md +64 -0
- package/CONSUMER_GUIDE.md +287 -12
- package/MIGRATION-0.10-to-0.12.md +189 -0
- package/README.md +6 -3
- package/package.json +5 -2
- package/src/adapters.ts +3 -1
- package/src/agent/adoption/replay.ts +1 -0
- package/src/agent/config.ts +4 -0
- package/src/agent/conversation-manager.ts +178 -150
- package/src/agent/error-record-writer.ts +77 -0
- package/src/agent/persisted-lifecycle.ts +152 -35
- package/src/agent/record-capture.ts +18 -18
- package/src/agent-adapter-registry.ts +2 -0
- package/src/agent-client.ts +9 -3
- package/src/agent-conversation-contracts.ts +6 -0
- package/src/agent-conversation-owner.ts +31 -6
- package/src/agent-conversation-presentation.ts +12 -1
- package/src/agent-conversation-queue.ts +3 -2
- package/src/agent-session.ts +535 -94
- package/src/agent.ts +46 -23
- package/src/audit/config.ts +4 -0
- package/src/audit/projection.ts +3 -0
- package/src/checkpointing/recovery.ts +5 -0
- package/src/checkpointing/strategy.ts +2 -0
- package/src/client.ts +120 -30
- package/src/controller.ts +116 -7
- package/src/degraded/pty-client.ts +25 -1
- package/src/index.ts +17 -0
- package/src/internal-inference/config.ts +7 -0
- package/src/internal-inference/contracts.ts +3 -0
- package/src/internal-inference/coordination/handles.ts +11 -2
- package/src/internal-inference/coordination/manager.ts +20 -3
- package/src/internal-inference/index.ts +2 -0
- package/src/internal-inference/persistence/records.ts +3 -1
- package/src/internal-inference/persistence/sqlite-repository.ts +4 -1
- package/src/internal-inference/runtime/disposable-session-env.ts +84 -0
- package/src/internal-inference/runtime/harness-runtime.ts +19 -2
- package/src/media/acp.ts +118 -0
- package/src/media/capture.ts +15 -0
- package/src/media/content.ts +142 -0
- package/src/media/files.ts +28 -0
- package/src/media/parts.ts +145 -0
- package/src/media/pending.ts +51 -0
- package/src/media/redact.ts +14 -0
- package/src/media/store.ts +127 -0
- package/src/metering/provider.ts +8 -1
- package/src/operations/activity-tracker.ts +31 -0
- package/src/operations/contracts.ts +63 -0
- package/src/operations/errors.ts +31 -0
- package/src/operations/index.ts +6 -0
- package/src/operations/stale-policy.ts +51 -0
- package/src/operations/supervisor.ts +209 -0
- package/src/operations/timers.ts +12 -0
- package/src/out-of-band-messages.ts +49 -32
- package/src/persistence/audit-runtime.ts +14 -2
- package/src/persistence/backend.ts +108 -12
- package/src/persistence/error-records.ts +27 -0
- package/src/persistence/index.ts +8 -0
- package/src/persistence/presentation.ts +9 -1
- package/src/persistence/projection.ts +20 -0
- package/src/persistence/records.ts +45 -6
- package/src/persistence/sqlite-backend.ts +207 -16
- package/src/persistence/sqlite-conversation-records.ts +31 -4
- package/src/persistence/sqlite-files.ts +95 -0
- package/src/persistence/sqlite-media-store.ts +67 -0
- package/src/persistence/sqlite-schema.ts +86 -10
- package/src/persistence/state-runtime.ts +2 -0
- package/src/persistence/timeline-reconcile.ts +21 -4
- package/src/persistence/transcript-reader.ts +11 -4
- package/src/persistence/transcript-runtime.ts +105 -12
- package/src/repl/repl.ts +1 -1
- package/src/response.ts +40 -8
- package/src/steering/combine.ts +11 -4
- package/src/steering/contracts.ts +14 -4
- package/src/steering/controller.ts +96 -21
- package/src/steering/index.ts +1 -0
- package/src/transport/acp-transport.ts +18 -7
- package/src/types.ts +7 -0
- package/src/updates.ts +4 -4
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { writeConversationError } from "./error-record-writer.ts";
|
|
1
2
|
import { randomUUID } from "node:crypto";
|
|
2
3
|
import {
|
|
3
4
|
createAgentConversationId,
|
|
@@ -41,6 +42,7 @@ import type { ManagerSettings } from "./manager-settings.ts";
|
|
|
41
42
|
import {
|
|
42
43
|
updateConversationIdentity,
|
|
43
44
|
persistConversationObservations,
|
|
45
|
+
persistObservationsInTransaction,
|
|
44
46
|
normalizeParticipants,
|
|
45
47
|
internalInferenceMeteringKind,
|
|
46
48
|
appendCanonicalRecord,
|
|
@@ -84,6 +86,10 @@ import {
|
|
|
84
86
|
} from "./adoption/index.ts";
|
|
85
87
|
import { NoopAgentIdentityRuntime } from "../identity.ts";
|
|
86
88
|
import type { ConversationPersistenceContext } from "./persistence-policy/index.ts";
|
|
89
|
+
import { forgetPendingParts, pendingOutOfBandParts } from "../media/pending.ts";
|
|
90
|
+
import { resolveParts } from "../media/store.ts";
|
|
91
|
+
import { persistParts } from "../media/content.ts";
|
|
92
|
+
import { isMediaPart, promptParts, partsText } from "../media/parts.ts";
|
|
87
93
|
|
|
88
94
|
export class DefaultAgentConversationManager implements AgentConversationManager {
|
|
89
95
|
private readonly live = new Map<AgentConversationId, AgentConversation>();
|
|
@@ -452,107 +458,119 @@ export class DefaultAgentConversationManager implements AgentConversationManager
|
|
|
452
458
|
} catch (error) {
|
|
453
459
|
// No recorded mapping means this open was creating a thread, not loading one.
|
|
454
460
|
if (!active) return await this.throwCreateFailure(id, attemptId, error, settings);
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
461
|
+
return await this.recoverAfterLoadFailure(id, active.internalSessionId, error, settings, config,
|
|
462
|
+
async () => await this.start(id, summary.participants, { ...startConfig, providerSession: "new", settings }),
|
|
463
|
+
async (conversation) => {
|
|
464
|
+
this.live.delete(id);
|
|
465
|
+
await conversation.stop().catch(() => undefined);
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/** Shared recovery policy for opening and reconnecting the same logical conversation. */
|
|
471
|
+
private async recoverAfterLoadFailure(
|
|
472
|
+
id: AgentConversationId, previousInternalSessionId: string, error: unknown, settings: ManagerSettings,
|
|
473
|
+
config: Pick<OpenAgentConversationConfig, "onRecoveryProgress">,
|
|
474
|
+
fresh: () => Promise<AgentConversation>, dispose: (conversation: AgentConversation) => Promise<void>,
|
|
475
|
+
): Promise<OpenOwnedAgentConversationResult> {
|
|
476
|
+
const summary = await this.agent.persistence.conversations.get(id);
|
|
477
|
+
if (!summary) throw new Error(`unknown conversation: ${id}`);
|
|
478
|
+
const attemptId = `provider-load-${randomUUID()}`;
|
|
479
|
+
const recovery = settings.checkpointing?.recovery;
|
|
480
|
+
const classification = classifyProviderSessionLoadError(error, recovery?.isMissingProviderSession);
|
|
481
|
+
const recorded = await this.recordConversationError({
|
|
482
|
+
settings,
|
|
483
|
+
agentConversationId: id,
|
|
484
|
+
attemptId,
|
|
485
|
+
source: "provider_session",
|
|
486
|
+
code: classification.code,
|
|
487
|
+
recoverability: classification.recoverability,
|
|
488
|
+
message: "The provider session could not be loaded.",
|
|
489
|
+
transitionId: attemptId,
|
|
490
|
+
internalSessionId: previousInternalSessionId,
|
|
491
|
+
details: providerSessionErrorDetails(error),
|
|
492
|
+
});
|
|
493
|
+
const structuredError = (): ProviderSessionError => new ProviderSessionError({
|
|
494
|
+
agentSessionId: this.agent.id,
|
|
495
|
+
agentConversationId: id,
|
|
496
|
+
attemptId,
|
|
497
|
+
internalSessionId: previousInternalSessionId,
|
|
498
|
+
code: classification.code,
|
|
499
|
+
source: "provider_session",
|
|
500
|
+
recoverability: classification.recoverability,
|
|
501
|
+
...(recorded.record ? { recordId: recorded.record.recordId } : {}),
|
|
502
|
+
...(recorded.recordingError ? { recordingError: recorded.recordingError } : {}),
|
|
503
|
+
cause: error,
|
|
504
|
+
});
|
|
505
|
+
if (recovery?.onLoadFailure !== "recover" || !classification.missing) throw structuredError();
|
|
506
|
+
config.onRecoveryProgress?.({ stage: "load_failed" });
|
|
507
|
+
config.onRecoveryProgress?.({ stage: "loading_checkpoint" });
|
|
508
|
+
const checkpoint = await this.agent.persistence.recoveryCheckpoints?.latest(id);
|
|
509
|
+
if (!checkpoint) throw structuredError();
|
|
510
|
+
config.onRecoveryProgress?.({ stage: "loading_checkpoint", checkpointId: checkpoint.checkpointId });
|
|
511
|
+
const recoveryAttemptId = `recovery-${randomUUID()}`;
|
|
512
|
+
try {
|
|
513
|
+
const seed = await buildRecoverySeed(
|
|
514
|
+
this.agent.persistence.messages,
|
|
515
|
+
id,
|
|
516
|
+
checkpoint,
|
|
517
|
+
summary.latestSequence,
|
|
518
|
+
recovery,
|
|
519
|
+
);
|
|
520
|
+
config.onRecoveryProgress?.({ stage: "creating_session", checkpointId: checkpoint.checkpointId });
|
|
521
|
+
const conversation = await fresh();
|
|
522
|
+
try {
|
|
523
|
+
config.onRecoveryProgress?.({ stage: "seeding", checkpointId: checkpoint.checkpointId });
|
|
524
|
+
await conversation.seedRecoveryContext(seed.prompt);
|
|
525
|
+
const acknowledgement = await this.persistProviderSession(conversation, id, {
|
|
526
|
+
transitionId: recoveryAttemptId,
|
|
527
|
+
metadata: JSON.stringify({
|
|
528
|
+
recoveryCheckpointId: checkpoint.checkpointId,
|
|
529
|
+
previousInternalSessionId,
|
|
530
|
+
recoveredThroughSequence: seed.recoveredThroughSequence,
|
|
531
|
+
}),
|
|
532
|
+
}, true);
|
|
533
|
+
conversation.acknowledgeProviderAttachment(acknowledgement);
|
|
534
|
+
} catch (recoveryError) {
|
|
535
|
+
await dispose(conversation);
|
|
536
|
+
throw recoveryError;
|
|
537
|
+
}
|
|
538
|
+
const result: ConversationRecoveryResult = Object.freeze({
|
|
539
|
+
checkpointId: checkpoint.checkpointId,
|
|
540
|
+
coveredSequence: checkpoint.coveredSequence,
|
|
541
|
+
recoveredThroughSequence: seed.recoveredThroughSequence,
|
|
542
|
+
previousInternalSessionId,
|
|
543
|
+
newInternalSessionId: conversation.internalSessionId!,
|
|
544
|
+
});
|
|
545
|
+
config.onRecoveryProgress?.({ stage: "completed", checkpointId: checkpoint.checkpointId });
|
|
546
|
+
return Object.freeze({ conversation, providerSession: "recovered", recovery: result });
|
|
547
|
+
} catch (recoveryError) {
|
|
548
|
+
// A recovery failure is a distinct root cause from the load failure already
|
|
549
|
+
// recorded above; it prevents the logical conversation from reopening.
|
|
550
|
+
const recoveryRecorded = await this.recordConversationError({
|
|
458
551
|
settings,
|
|
459
552
|
agentConversationId: id,
|
|
460
|
-
attemptId,
|
|
461
|
-
source: "
|
|
462
|
-
code:
|
|
463
|
-
recoverability:
|
|
464
|
-
message: "The
|
|
465
|
-
transitionId:
|
|
466
|
-
internalSessionId:
|
|
467
|
-
details: providerSessionErrorDetails(error),
|
|
553
|
+
attemptId: recoveryAttemptId,
|
|
554
|
+
source: "recovery",
|
|
555
|
+
code: "recovery_failed",
|
|
556
|
+
recoverability: "recoverable",
|
|
557
|
+
message: "The conversation could not be recovered from its checkpoint.",
|
|
558
|
+
transitionId: recoveryAttemptId,
|
|
559
|
+
internalSessionId: previousInternalSessionId,
|
|
468
560
|
});
|
|
469
|
-
|
|
561
|
+
throw new ProviderSessionError({
|
|
470
562
|
agentSessionId: this.agent.id,
|
|
471
563
|
agentConversationId: id,
|
|
472
|
-
attemptId,
|
|
473
|
-
internalSessionId:
|
|
474
|
-
code:
|
|
475
|
-
source: "
|
|
476
|
-
recoverability:
|
|
477
|
-
|
|
478
|
-
...(
|
|
479
|
-
|
|
564
|
+
attemptId: recoveryAttemptId,
|
|
565
|
+
internalSessionId: previousInternalSessionId,
|
|
566
|
+
code: "recovery_failed",
|
|
567
|
+
source: "recovery",
|
|
568
|
+
recoverability: "recoverable",
|
|
569
|
+
message: "The conversation could not be recovered from its checkpoint.",
|
|
570
|
+
...(recoveryRecorded.record ? { recordId: recoveryRecorded.record.recordId } : {}),
|
|
571
|
+
...(recoveryRecorded.recordingError ? { recordingError: recoveryRecorded.recordingError } : {}),
|
|
572
|
+
cause: recoveryError,
|
|
480
573
|
});
|
|
481
|
-
if (recovery?.onLoadFailure !== "recover" || !classification.missing) throw structuredError();
|
|
482
|
-
config.onRecoveryProgress?.({ stage: "load_failed" });
|
|
483
|
-
config.onRecoveryProgress?.({ stage: "loading_checkpoint" });
|
|
484
|
-
const checkpoint = await this.agent.persistence.recoveryCheckpoints?.latest(id);
|
|
485
|
-
if (!checkpoint) throw structuredError();
|
|
486
|
-
config.onRecoveryProgress?.({ stage: "loading_checkpoint", checkpointId: checkpoint.checkpointId });
|
|
487
|
-
const recoveryAttemptId = `recovery-${randomUUID()}`;
|
|
488
|
-
try {
|
|
489
|
-
const seed = await buildRecoverySeed(
|
|
490
|
-
this.agent.persistence.messages,
|
|
491
|
-
id,
|
|
492
|
-
checkpoint,
|
|
493
|
-
summary.latestSequence,
|
|
494
|
-
recovery,
|
|
495
|
-
);
|
|
496
|
-
config.onRecoveryProgress?.({ stage: "creating_session", checkpointId: checkpoint.checkpointId });
|
|
497
|
-
const conversation = await this.start(id, summary.participants, {
|
|
498
|
-
...startConfig,
|
|
499
|
-
providerSession: "new",
|
|
500
|
-
settings,
|
|
501
|
-
});
|
|
502
|
-
try {
|
|
503
|
-
config.onRecoveryProgress?.({ stage: "seeding", checkpointId: checkpoint.checkpointId });
|
|
504
|
-
await conversation.seedRecoveryContext(seed.prompt);
|
|
505
|
-
const acknowledgement = await this.persistProviderSession(conversation, id, {
|
|
506
|
-
transitionId: recoveryAttemptId,
|
|
507
|
-
metadata: JSON.stringify({
|
|
508
|
-
recoveryCheckpointId: checkpoint.checkpointId,
|
|
509
|
-
previousInternalSessionId: active.internalSessionId,
|
|
510
|
-
recoveredThroughSequence: seed.recoveredThroughSequence,
|
|
511
|
-
}),
|
|
512
|
-
}, true);
|
|
513
|
-
conversation.acknowledgeProviderAttachment(acknowledgement);
|
|
514
|
-
} catch (recoveryError) {
|
|
515
|
-
this.live.delete(id);
|
|
516
|
-
await conversation.stop().catch(() => undefined);
|
|
517
|
-
throw recoveryError;
|
|
518
|
-
}
|
|
519
|
-
const result: ConversationRecoveryResult = Object.freeze({
|
|
520
|
-
checkpointId: checkpoint.checkpointId,
|
|
521
|
-
coveredSequence: checkpoint.coveredSequence,
|
|
522
|
-
recoveredThroughSequence: seed.recoveredThroughSequence,
|
|
523
|
-
previousInternalSessionId: active.internalSessionId,
|
|
524
|
-
newInternalSessionId: conversation.internalSessionId!,
|
|
525
|
-
});
|
|
526
|
-
config.onRecoveryProgress?.({ stage: "completed", checkpointId: checkpoint.checkpointId });
|
|
527
|
-
return Object.freeze({ conversation, providerSession: "recovered", recovery: result });
|
|
528
|
-
} catch (recoveryError) {
|
|
529
|
-
// A recovery failure is a distinct root cause from the load failure already
|
|
530
|
-
// recorded above; it prevents the logical conversation from reopening.
|
|
531
|
-
const recoveryRecorded = await this.recordConversationError({
|
|
532
|
-
settings,
|
|
533
|
-
agentConversationId: id,
|
|
534
|
-
attemptId: recoveryAttemptId,
|
|
535
|
-
source: "recovery",
|
|
536
|
-
code: "recovery_failed",
|
|
537
|
-
recoverability: "recoverable",
|
|
538
|
-
message: "The conversation could not be recovered from its checkpoint.",
|
|
539
|
-
transitionId: recoveryAttemptId,
|
|
540
|
-
internalSessionId: active.internalSessionId,
|
|
541
|
-
});
|
|
542
|
-
throw new ProviderSessionError({
|
|
543
|
-
agentSessionId: this.agent.id,
|
|
544
|
-
agentConversationId: id,
|
|
545
|
-
attemptId: recoveryAttemptId,
|
|
546
|
-
internalSessionId: active.internalSessionId,
|
|
547
|
-
code: "recovery_failed",
|
|
548
|
-
source: "recovery",
|
|
549
|
-
recoverability: "recoverable",
|
|
550
|
-
message: "The conversation could not be recovered from its checkpoint.",
|
|
551
|
-
...(recoveryRecorded.record ? { recordId: recoveryRecorded.record.recordId } : {}),
|
|
552
|
-
...(recoveryRecorded.recordingError ? { recordingError: recoveryRecorded.recordingError } : {}),
|
|
553
|
-
cause: recoveryError,
|
|
554
|
-
});
|
|
555
|
-
}
|
|
556
574
|
}
|
|
557
575
|
}
|
|
558
576
|
|
|
@@ -613,6 +631,8 @@ export class DefaultAgentConversationManager implements AgentConversationManager
|
|
|
613
631
|
? (await this.agent.persistence.recoveryCheckpoints?.deleteForConversation?.(id)) ?? 0
|
|
614
632
|
: 0;
|
|
615
633
|
await this.agent.persistence.conversations.delete(id, deletedAt);
|
|
634
|
+
// A deleted conversation runs no next turn, so its waiting out-of-band parts would never leave.
|
|
635
|
+
forgetPendingParts(this.agent, id);
|
|
616
636
|
let providerDeleted: boolean | "unsupported" = false;
|
|
617
637
|
if (active) {
|
|
618
638
|
providerDeleted = await deleteProviderSession({
|
|
@@ -637,46 +657,81 @@ export class DefaultAgentConversationManager implements AgentConversationManager
|
|
|
637
657
|
if (!this.agent.persistence.records.deleteForConversation) {
|
|
638
658
|
throw new Error("this persistence backend cannot clear a transcript");
|
|
639
659
|
}
|
|
640
|
-
|
|
660
|
+
const result = await this.agent.persistence.transaction(async (persistence) => {
|
|
641
661
|
const recordsDeleted = await persistence.records.deleteForConversation!(id);
|
|
642
662
|
const checkpointsDeleted = options.checkpoints
|
|
643
663
|
? (await persistence.recoveryCheckpoints?.deleteForConversation?.(id)) ?? 0
|
|
644
664
|
: 0;
|
|
645
665
|
return Object.freeze({ recordsDeleted, checkpointsDeleted });
|
|
646
666
|
});
|
|
667
|
+
// The backend store's copies went with the records; a consumer store is cleaned here.
|
|
668
|
+
if (this.agent.media.store) await this.agent.deleteMediaForConversation(id);
|
|
669
|
+
else forgetPendingParts(this.agent, id);
|
|
670
|
+
return result;
|
|
647
671
|
}
|
|
648
672
|
|
|
649
673
|
async appendAssistantMessage(
|
|
650
674
|
id: AgentConversationId,
|
|
651
675
|
input: AppendAssistantMessageInput,
|
|
652
676
|
): Promise<MessageRecord> {
|
|
653
|
-
|
|
677
|
+
const parts = promptParts(input) ?? [{ kind: "text" as const, text: input.text ?? "" }];
|
|
678
|
+
const hasMedia = parts.some((part) => part.kind !== "text" && part.kind !== "pasted");
|
|
679
|
+
if (!partsText(parts).trim() && !hasMedia) {
|
|
654
680
|
throw new Error("assistant message text cannot be blank");
|
|
655
681
|
}
|
|
656
682
|
const summary = await this.agent.persistence.conversations.get(id);
|
|
657
683
|
if (!summary || summary.deletedAt) throw new Error(`unknown active conversation: ${id}`);
|
|
658
684
|
const settings = this.writeSettings(summary);
|
|
659
685
|
const level = settings.capture.message;
|
|
660
|
-
if (level !== "content" && level !== "raw") {
|
|
686
|
+
if (!hasMedia && level !== "content" && level !== "raw") {
|
|
661
687
|
throw new Error(
|
|
662
688
|
`appending an assistant message requires message capture "content" or "raw", not "${level}"`,
|
|
663
689
|
);
|
|
664
690
|
}
|
|
691
|
+
// No agent reads the parts yet: every kind is accepted now, and delivery names what the agent of
|
|
692
|
+
// the next turn cannot take.
|
|
693
|
+
const resolved = await resolveParts(parts, {
|
|
694
|
+
cwd: this.live.get(id)?.cwd ?? this.settings.defaults.cwd ?? process.cwd(),
|
|
695
|
+
capabilities: { image: true, audio: true, embeddedContext: true },
|
|
696
|
+
thresholds: this.agent.media.thresholds,
|
|
697
|
+
owner: id,
|
|
698
|
+
store: this.agent.media.store ?? this.agent.persistence.media,
|
|
699
|
+
});
|
|
665
700
|
const appendId = `append-${randomUUID()}`;
|
|
666
|
-
const [
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
701
|
+
const persisted: ConversationRecord[] = [];
|
|
702
|
+
const recordId = `${appendId}:assistant`;
|
|
703
|
+
await this.agent.persistence.transaction(async (scope) => {
|
|
704
|
+
const content = await persistParts(resolved, id, this.agent.media.store ?? scope.media, settings.capture.media);
|
|
705
|
+
await persistObservationsInTransaction(settings, scope, [{
|
|
706
|
+
recordId,
|
|
707
|
+
agentSessionId: this.agent.id,
|
|
708
|
+
agentConversationId: id,
|
|
709
|
+
kind: "message",
|
|
710
|
+
timestamp: new Date().toISOString(),
|
|
711
|
+
// A standalone message at its observed sequence, without a provider operation.
|
|
712
|
+
role: "assistant",
|
|
713
|
+
logicalMessageId: `message:${appendId}:assistant`,
|
|
714
|
+
...(input.consumerMetadata !== undefined ? { consumerMetadata: input.consumerMetadata } : {}),
|
|
715
|
+
content,
|
|
716
|
+
status: "completed",
|
|
717
|
+
metadata: JSON.stringify({ origin: OUT_OF_BAND_MESSAGE_ORIGIN, appendId }),
|
|
718
|
+
}], true, persisted);
|
|
719
|
+
});
|
|
720
|
+
const record = persisted[0];
|
|
679
721
|
if (record?.kind !== "message") throw new Error("assistant message was not persisted");
|
|
722
|
+
// The next turn delivers the message from its record. When the record cannot restore it (a part
|
|
723
|
+
// not recorded, media left out by capture, a copy not kept, text not captured), its parts wait in
|
|
724
|
+
// memory instead.
|
|
725
|
+
const blocks = Array.isArray(record.content) ? record.content : [];
|
|
726
|
+
const media = resolved.filter(isMediaPart);
|
|
727
|
+
const restorable = (level === "content" || level === "raw")
|
|
728
|
+
&& resolved.every((part) => part.record !== false)
|
|
729
|
+
&& blocks.filter((block) => block.type === "media").length === media.length
|
|
730
|
+
&& blocks.filter((block) => block.type === "media" && block.ref).length
|
|
731
|
+
=== media.filter((part) => part.mode === "copy").length;
|
|
732
|
+
if (!restorable) pendingOutOfBandParts(this.agent).set(recordId, { owner: id, parts: resolved });
|
|
733
|
+
this.agent.publishCommittedRecords(persisted);
|
|
734
|
+
if (settings.auditLog) this.agent.audit?.notifyPending();
|
|
680
735
|
await updateConversationIdentity(this.agent, settings, id, [record]);
|
|
681
736
|
return record;
|
|
682
737
|
}
|
|
@@ -821,6 +876,17 @@ export class DefaultAgentConversationManager implements AgentConversationManager
|
|
|
821
876
|
id,
|
|
822
877
|
participants,
|
|
823
878
|
config.settings,
|
|
879
|
+
async (loadError) => {
|
|
880
|
+
const current = conversation!;
|
|
881
|
+
await this.recoverAfterLoadFailure(id, current.internalSessionId!, loadError, config.settings, {},
|
|
882
|
+
async () => {
|
|
883
|
+
await current.restartProviderFresh();
|
|
884
|
+
return current;
|
|
885
|
+
},
|
|
886
|
+
async () => {
|
|
887
|
+
await current.connection.forceStop();
|
|
888
|
+
});
|
|
889
|
+
},
|
|
824
890
|
);
|
|
825
891
|
this.liveSettings.set(id, config.settings);
|
|
826
892
|
conversation = await AgentConversation.create({
|
|
@@ -917,45 +983,7 @@ export class DefaultAgentConversationManager implements AgentConversationManager
|
|
|
917
983
|
readonly internalSessionId?: string;
|
|
918
984
|
readonly details?: ConversationErrorDetails;
|
|
919
985
|
}): Promise<{ readonly record?: ConversationRecord; readonly recordingError?: Error }> {
|
|
920
|
-
|
|
921
|
-
recordId: `${input.attemptId}:error`,
|
|
922
|
-
agentSessionId: this.agent.id,
|
|
923
|
-
agentConversationId: input.agentConversationId,
|
|
924
|
-
kind: "error",
|
|
925
|
-
timestamp: new Date().toISOString(),
|
|
926
|
-
source: input.source,
|
|
927
|
-
code: input.code,
|
|
928
|
-
recoverability: input.recoverability,
|
|
929
|
-
...(input.message === undefined ? {} : { message: input.message }),
|
|
930
|
-
...(input.operationId === undefined ? {} : { operationId: input.operationId }),
|
|
931
|
-
...(input.transitionId === undefined ? {} : { transitionId: input.transitionId }),
|
|
932
|
-
...(input.internalSessionId === undefined ? {} : { internalSessionId: input.internalSessionId }),
|
|
933
|
-
...(input.details === undefined ? {} : { details: input.details }),
|
|
934
|
-
};
|
|
935
|
-
const settings = input.settings ?? this.settings;
|
|
936
|
-
const level = settings.capture.error;
|
|
937
|
-
const audit = settings.auditLog;
|
|
938
|
-
let persisted: ConversationRecord | undefined;
|
|
939
|
-
try {
|
|
940
|
-
await this.agent.persistence.transaction(async (persistence) => {
|
|
941
|
-
if (level !== "none") {
|
|
942
|
-
persisted = await persistence.records.append(captureRuntimeRecord(record, level));
|
|
943
|
-
}
|
|
944
|
-
if (audit) {
|
|
945
|
-
await enqueueAuditObservation(persistence, {
|
|
946
|
-
scope: "conversation",
|
|
947
|
-
sourceObservationId: record.recordId,
|
|
948
|
-
...(input.operationId === undefined ? {} : { operationId: input.operationId }),
|
|
949
|
-
record,
|
|
950
|
-
}, audit);
|
|
951
|
-
}
|
|
952
|
-
});
|
|
953
|
-
} catch (error) {
|
|
954
|
-
return { recordingError: error instanceof Error ? error : new Error(String(error)) };
|
|
955
|
-
}
|
|
956
|
-
if (persisted) this.agent.publishCommittedRecords([persisted]);
|
|
957
|
-
if (audit) this.agent.audit?.notifyPending();
|
|
958
|
-
return persisted ? { record: persisted } : {};
|
|
986
|
+
return writeConversationError(this.agent, this.settings, input);
|
|
959
987
|
}
|
|
960
988
|
|
|
961
989
|
/**
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { Agent } from "../agent.ts";
|
|
2
|
+
import type { AgentConversationId } from "../agent-ids.ts";
|
|
3
|
+
import type {
|
|
4
|
+
ConversationErrorDetails,
|
|
5
|
+
ConversationErrorRecoverability,
|
|
6
|
+
ConversationErrorSource,
|
|
7
|
+
ConversationRecord,
|
|
8
|
+
NewConversationRecord,
|
|
9
|
+
} from "../persistence/backend.ts";
|
|
10
|
+
import type { ManagerSettings } from "./manager-settings.ts";
|
|
11
|
+
import { captureRuntimeRecord, enqueueAuditObservation } from "./record-capture.ts";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Write one conversation error record, and its audit observation when audit logging is on, then
|
|
15
|
+
* publish it. A failure to record is returned, not thrown, so the caller's own error still surfaces.
|
|
16
|
+
*/
|
|
17
|
+
export async function writeConversationError(
|
|
18
|
+
agent: Agent,
|
|
19
|
+
defaults: ManagerSettings,
|
|
20
|
+
input: {
|
|
21
|
+
/**
|
|
22
|
+
* The conversation's own settings; the Agent's when the record is not about one conversation's
|
|
23
|
+
* start.
|
|
24
|
+
*/
|
|
25
|
+
readonly settings?: ManagerSettings;
|
|
26
|
+
readonly agentConversationId: AgentConversationId;
|
|
27
|
+
readonly attemptId: string;
|
|
28
|
+
readonly source: ConversationErrorSource;
|
|
29
|
+
readonly code: string;
|
|
30
|
+
readonly recoverability: ConversationErrorRecoverability;
|
|
31
|
+
readonly message?: string;
|
|
32
|
+
readonly operationId?: string;
|
|
33
|
+
readonly transitionId?: string;
|
|
34
|
+
readonly internalSessionId?: string;
|
|
35
|
+
readonly details?: ConversationErrorDetails;
|
|
36
|
+
},
|
|
37
|
+
): Promise<{ readonly record?: ConversationRecord; readonly recordingError?: Error }> {
|
|
38
|
+
const record: NewConversationRecord = {
|
|
39
|
+
recordId: `${input.attemptId}:error`,
|
|
40
|
+
agentSessionId: agent.id,
|
|
41
|
+
agentConversationId: input.agentConversationId,
|
|
42
|
+
kind: "error",
|
|
43
|
+
timestamp: new Date().toISOString(),
|
|
44
|
+
source: input.source,
|
|
45
|
+
code: input.code,
|
|
46
|
+
recoverability: input.recoverability,
|
|
47
|
+
...(input.message === undefined ? {} : { message: input.message }),
|
|
48
|
+
...(input.operationId === undefined ? {} : { operationId: input.operationId }),
|
|
49
|
+
...(input.transitionId === undefined ? {} : { transitionId: input.transitionId }),
|
|
50
|
+
...(input.internalSessionId === undefined ? {} : { internalSessionId: input.internalSessionId }),
|
|
51
|
+
...(input.details === undefined ? {} : { details: input.details }),
|
|
52
|
+
};
|
|
53
|
+
const settings = input.settings ?? defaults;
|
|
54
|
+
const level = settings.capture.error;
|
|
55
|
+
const audit = settings.auditLog;
|
|
56
|
+
let persisted: ConversationRecord | undefined;
|
|
57
|
+
try {
|
|
58
|
+
await agent.persistence.transaction(async (persistence) => {
|
|
59
|
+
if (level !== "none") {
|
|
60
|
+
persisted = await persistence.records.append(captureRuntimeRecord(record, level));
|
|
61
|
+
}
|
|
62
|
+
if (audit) {
|
|
63
|
+
await enqueueAuditObservation(persistence, {
|
|
64
|
+
scope: "conversation",
|
|
65
|
+
sourceObservationId: record.recordId,
|
|
66
|
+
...(input.operationId === undefined ? {} : { operationId: input.operationId }),
|
|
67
|
+
record,
|
|
68
|
+
}, audit);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
} catch (error) {
|
|
72
|
+
return { recordingError: error instanceof Error ? error : new Error(String(error)) };
|
|
73
|
+
}
|
|
74
|
+
if (persisted) agent.publishCommittedRecords([persisted]);
|
|
75
|
+
if (audit) agent.audit?.notifyPending();
|
|
76
|
+
return persisted ? { record: persisted } : {};
|
|
77
|
+
}
|