@qlairoslabs/acp-client 0.10.0 → 0.10.1
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/CONSUMER_GUIDE.md +85 -0
- package/package.json +4 -2
- package/src/agent-session.ts +2 -0
- package/src/agent.ts +380 -86
- package/src/audit/config.ts +4 -0
- package/src/audit/projection.ts +11 -0
- package/src/checkpointing/recovery.ts +23 -1
- package/src/index.ts +23 -0
- package/src/out-of-band-messages.ts +187 -0
- package/src/persistence/backend.ts +46 -1
- package/src/persistence/error-records.ts +104 -0
- package/src/persistence/sqlite-conversation-records.ts +38 -0
- package/src/persistence/sqlite-schema.ts +128 -47
package/src/index.ts
CHANGED
|
@@ -45,6 +45,12 @@ export type {
|
|
|
45
45
|
OpenAgentConversationConfig,
|
|
46
46
|
OpenOwnedAgentConversationResult,
|
|
47
47
|
} from "./agent.ts";
|
|
48
|
+
export {
|
|
49
|
+
OUT_OF_BAND_MESSAGE_ORIGIN,
|
|
50
|
+
OUT_OF_BAND_PROMPT_MAX_CHARS,
|
|
51
|
+
isOutOfBandMessage,
|
|
52
|
+
} from "./out-of-band-messages.ts";
|
|
53
|
+
export type { AppendAssistantMessageInput } from "./out-of-band-messages.ts";
|
|
48
54
|
export { AgentAdapterRegistry } from "./agent-adapter-registry.ts";
|
|
49
55
|
export type { AgentAdapterRegistration } from "./agent-adapter-registry.ts";
|
|
50
56
|
export { NoopAgentIdentityRuntime, composeIdentityPrompt } from "./identity.ts";
|
|
@@ -75,6 +81,17 @@ export {
|
|
|
75
81
|
shouldCaptureProtocolMethod,
|
|
76
82
|
searchableText,
|
|
77
83
|
} from "./persistence/backend.ts";
|
|
84
|
+
export {
|
|
85
|
+
CONVERSATION_ERROR_CODES,
|
|
86
|
+
CONVERSATION_ERROR_RECOVERABILITIES,
|
|
87
|
+
CONVERSATION_ERROR_SOURCES,
|
|
88
|
+
buildConversationErrorDetails,
|
|
89
|
+
sanitizeConversationErrorMessage,
|
|
90
|
+
} from "./persistence/error-records.ts";
|
|
91
|
+
export type {
|
|
92
|
+
ConversationErrorCode,
|
|
93
|
+
ConversationErrorDetailInput,
|
|
94
|
+
} from "./persistence/error-records.ts";
|
|
78
95
|
export {
|
|
79
96
|
AgentDatabaseInUseError,
|
|
80
97
|
SQLITE_AGENT_SCHEMA_VERSION,
|
|
@@ -96,6 +113,10 @@ export type {
|
|
|
96
113
|
ConversationRecordCapturePolicy,
|
|
97
114
|
ConversationRecordEnvelope,
|
|
98
115
|
ConversationRecordKind,
|
|
116
|
+
ConversationErrorDetails,
|
|
117
|
+
ConversationErrorRecoverability,
|
|
118
|
+
ConversationErrorSource,
|
|
119
|
+
ErrorRecord,
|
|
99
120
|
RecentRecordPage,
|
|
100
121
|
RecentRecordPageOptions,
|
|
101
122
|
IdentityMemoryObservation,
|
|
@@ -158,7 +179,9 @@ export {
|
|
|
158
179
|
AdapterMismatchError,
|
|
159
180
|
ContinuationUnavailableError,
|
|
160
181
|
DirectAuditPersistenceUnsupportedError,
|
|
182
|
+
ProviderSessionError,
|
|
161
183
|
} from "./agent-session.ts";
|
|
184
|
+
export type { ProviderSessionErrorDetails } from "./agent-session.ts";
|
|
162
185
|
export type {
|
|
163
186
|
AgentConversationConfig,
|
|
164
187
|
AgentConversationOpenConfig,
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import type { AgentConversationId } from "./agent-ids.ts";
|
|
2
|
+
import type {
|
|
3
|
+
ConversationRecord,
|
|
4
|
+
MessageRecord,
|
|
5
|
+
PersistenceBackend,
|
|
6
|
+
PersistenceJsonValue,
|
|
7
|
+
} from "./persistence/backend.ts";
|
|
8
|
+
|
|
9
|
+
/** Input for an assistant message a consumer appends without running a provider turn. */
|
|
10
|
+
export interface AppendAssistantMessageInput {
|
|
11
|
+
readonly text: string;
|
|
12
|
+
/** Opaque JSON stored on the record for the consumer's own use. */
|
|
13
|
+
readonly consumerMetadata?: PersistenceJsonValue;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Record `metadata.origin` marking an assistant message the provider did not produce. */
|
|
17
|
+
export const OUT_OF_BAND_MESSAGE_ORIGIN = "out_of_band";
|
|
18
|
+
|
|
19
|
+
/** Upper bound on the provider-only block; older messages are dropped first. */
|
|
20
|
+
export const OUT_OF_BAND_PROMPT_MAX_CHARS = 8_000;
|
|
21
|
+
|
|
22
|
+
const RECENT_PAGE_SIZE = 50;
|
|
23
|
+
|
|
24
|
+
export function isOutOfBandMessage(record: ConversationRecord): record is MessageRecord {
|
|
25
|
+
return record.kind === "message" && parseMetadata(record.metadata).origin === OUT_OF_BAND_MESSAGE_ORIGIN;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Out-of-band assistant messages the provider context has not received yet.
|
|
30
|
+
*
|
|
31
|
+
* Everything appended before the user record of the last completed turn was composed into
|
|
32
|
+
* that turn's provider text, and a recovery seed carries every canonical message through
|
|
33
|
+
* `recoveredThroughSequence`. So the window starts after the later of those two points and
|
|
34
|
+
* ends before the current user record, which must already be persisted: an append that
|
|
35
|
+
* races this read lands either inside the window or after the current user record.
|
|
36
|
+
*/
|
|
37
|
+
export async function pendingOutOfBandMessages(
|
|
38
|
+
persistence: PersistenceBackend,
|
|
39
|
+
conversationId: AgentConversationId,
|
|
40
|
+
targetProviderContextId: string | undefined,
|
|
41
|
+
currentUserSequence: number,
|
|
42
|
+
): Promise<readonly MessageRecord[]> {
|
|
43
|
+
const watermark = Math.max(
|
|
44
|
+
await recoveredThroughSequence(persistence, conversationId, targetProviderContextId),
|
|
45
|
+
await lastCompletedTurnUserSequence(persistence, conversationId, currentUserSequence),
|
|
46
|
+
);
|
|
47
|
+
if (watermark >= currentUserSequence - 1) return [];
|
|
48
|
+
const pending: MessageRecord[] = [];
|
|
49
|
+
let cursor: string | undefined;
|
|
50
|
+
do {
|
|
51
|
+
const page = await persistence.messages.list(conversationId, {
|
|
52
|
+
afterSequence: watermark,
|
|
53
|
+
throughSequence: currentUserSequence - 1,
|
|
54
|
+
limit: 500,
|
|
55
|
+
...(cursor ? { cursor } : {}),
|
|
56
|
+
});
|
|
57
|
+
for (const message of page.messages) {
|
|
58
|
+
if (message.role === "assistant" && isOutOfBandMessage(message) && textOf(message)) {
|
|
59
|
+
pending.push(message);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
cursor = page.nextCursor;
|
|
63
|
+
} while (cursor);
|
|
64
|
+
return pending;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Provider text for one turn: identity preamble, then out-of-band messages, then the user's
|
|
69
|
+
* own text. Only the user's text is persisted; everything else reaches the provider alone.
|
|
70
|
+
*/
|
|
71
|
+
export function composeProviderPrompt(
|
|
72
|
+
prompt: string,
|
|
73
|
+
identityPreamble: string | undefined,
|
|
74
|
+
outOfBand: readonly MessageRecord[],
|
|
75
|
+
): string {
|
|
76
|
+
const sections = [identityPreamble?.trim(), formatOutOfBandBlock(outOfBand)]
|
|
77
|
+
.filter((section): section is string => Boolean(section));
|
|
78
|
+
if (sections.length === 0) return prompt;
|
|
79
|
+
return `${sections.join("\n\n")}\n\n<current-message>\n${prompt}\n</current-message>`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function formatOutOfBandBlock(messages: readonly MessageRecord[]): string | undefined {
|
|
83
|
+
if (messages.length === 0) return undefined;
|
|
84
|
+
const header = "Messages you sent in this conversation through another channel since your last turn."
|
|
85
|
+
+ " The user has seen them; you have not.";
|
|
86
|
+
const entries = messages.map((message) => `[${message.timestamp}] ${textOf(message)}`);
|
|
87
|
+
let budget = OUT_OF_BAND_PROMPT_MAX_CHARS - header.length;
|
|
88
|
+
let kept = 0;
|
|
89
|
+
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
90
|
+
const cost = entries[index]!.length + 1;
|
|
91
|
+
if (cost > budget && kept > 0) break;
|
|
92
|
+
budget -= cost;
|
|
93
|
+
kept += 1;
|
|
94
|
+
}
|
|
95
|
+
const omitted = entries.length - kept;
|
|
96
|
+
const body = [
|
|
97
|
+
...(omitted > 0 ? [`(${omitted} earlier message${omitted === 1 ? "" : "s"} omitted)`] : []),
|
|
98
|
+
...entries.slice(omitted),
|
|
99
|
+
];
|
|
100
|
+
return `<messages-you-sent>\n${header}\n${body.join("\n")}\n</messages-you-sent>`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async function recoveredThroughSequence(
|
|
104
|
+
persistence: PersistenceBackend,
|
|
105
|
+
conversationId: AgentConversationId,
|
|
106
|
+
targetProviderContextId: string | undefined,
|
|
107
|
+
): Promise<number> {
|
|
108
|
+
if (!targetProviderContextId) return 0;
|
|
109
|
+
const active = await persistence.providerSessions.active(conversationId);
|
|
110
|
+
if (active?.internalSessionId !== targetProviderContextId) return 0;
|
|
111
|
+
const value = parseMetadata(active.metadata).recoveredThroughSequence;
|
|
112
|
+
return typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Sequence of the user record whose turn most recently completed, or 0 when none did. */
|
|
116
|
+
async function lastCompletedTurnUserSequence(
|
|
117
|
+
persistence: PersistenceBackend,
|
|
118
|
+
conversationId: AgentConversationId,
|
|
119
|
+
beforeSequence: number,
|
|
120
|
+
): Promise<number> {
|
|
121
|
+
const records = persistence.records;
|
|
122
|
+
if (records.listRecent) {
|
|
123
|
+
let olderCursor: string | undefined;
|
|
124
|
+
do {
|
|
125
|
+
const page = await records.listRecent(conversationId, {
|
|
126
|
+
kinds: ["message"],
|
|
127
|
+
limit: RECENT_PAGE_SIZE,
|
|
128
|
+
...(olderCursor ? { olderCursor } : {}),
|
|
129
|
+
});
|
|
130
|
+
for (let index = page.records.length - 1; index >= 0; index -= 1) {
|
|
131
|
+
const found = await completedTurnUserSequence(persistence, page.records[index]!, beforeSequence);
|
|
132
|
+
if (found !== undefined) return found;
|
|
133
|
+
}
|
|
134
|
+
olderCursor = page.olderCursor;
|
|
135
|
+
} while (olderCursor);
|
|
136
|
+
return 0;
|
|
137
|
+
}
|
|
138
|
+
// Custom backends without newest-first history pay a forward scan.
|
|
139
|
+
let latest = 0;
|
|
140
|
+
let cursor: string | undefined;
|
|
141
|
+
do {
|
|
142
|
+
const page = await records.list(conversationId, {
|
|
143
|
+
kinds: ["message"],
|
|
144
|
+
throughSequence: beforeSequence - 1,
|
|
145
|
+
...(cursor ? { cursor } : {}),
|
|
146
|
+
});
|
|
147
|
+
for (const record of page.records) {
|
|
148
|
+
const found = await completedTurnUserSequence(persistence, record, beforeSequence);
|
|
149
|
+
if (found !== undefined) latest = Math.max(latest, found);
|
|
150
|
+
}
|
|
151
|
+
cursor = page.nextCursor;
|
|
152
|
+
} while (cursor);
|
|
153
|
+
return latest;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async function completedTurnUserSequence(
|
|
157
|
+
persistence: PersistenceBackend,
|
|
158
|
+
record: ConversationRecord,
|
|
159
|
+
beforeSequence: number,
|
|
160
|
+
): Promise<number | undefined> {
|
|
161
|
+
if (record.kind !== "message" || record.sequence >= beforeSequence) return undefined;
|
|
162
|
+
if (record.role !== "assistant" || record.status !== "completed") return undefined;
|
|
163
|
+
const metadata = parseMetadata(record.metadata);
|
|
164
|
+
if (metadata.origin === OUT_OF_BAND_MESSAGE_ORIGIN) return undefined;
|
|
165
|
+
const operationId = metadata.operationId;
|
|
166
|
+
if (typeof operationId !== "string" || !operationId) return undefined;
|
|
167
|
+
const user = await persistence.records.get(`${operationId}:user`);
|
|
168
|
+
return user && user.sequence < beforeSequence ? user.sequence : undefined;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function textOf(message: MessageRecord): string {
|
|
172
|
+
if (typeof message.content === "string") return message.content.trim();
|
|
173
|
+
return message.content
|
|
174
|
+
.map((block) => block.type === "text" ? block.text : "")
|
|
175
|
+
.join("")
|
|
176
|
+
.trim();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function parseMetadata(metadata: string | undefined): Record<string, unknown> {
|
|
180
|
+
if (!metadata) return {};
|
|
181
|
+
try {
|
|
182
|
+
const value: unknown = JSON.parse(metadata);
|
|
183
|
+
return typeof value === "object" && value !== null ? value as Record<string, unknown> : {};
|
|
184
|
+
} catch {
|
|
185
|
+
return {};
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -33,6 +33,7 @@ export const CONVERSATION_RECORD_KINDS = [
|
|
|
33
33
|
"thinking",
|
|
34
34
|
"activity",
|
|
35
35
|
"metering",
|
|
36
|
+
"error",
|
|
36
37
|
] as const;
|
|
37
38
|
|
|
38
39
|
export type ConversationRecordKind = (typeof CONVERSATION_RECORD_KINDS)[number];
|
|
@@ -150,6 +151,44 @@ export interface MeteringReferenceRecord extends ConversationRecordEnvelope<"met
|
|
|
150
151
|
readonly status: "available" | "partial" | "unavailable";
|
|
151
152
|
}
|
|
152
153
|
|
|
154
|
+
/** Operational boundary a conversation error was observed at. Closed taxonomy. */
|
|
155
|
+
export type ConversationErrorSource =
|
|
156
|
+
| "provider_session"
|
|
157
|
+
| "transport"
|
|
158
|
+
| "operation"
|
|
159
|
+
| "recovery";
|
|
160
|
+
|
|
161
|
+
/** What a consumer may reasonably do about a failed operation. */
|
|
162
|
+
export type ConversationErrorRecoverability =
|
|
163
|
+
| "retryable"
|
|
164
|
+
| "recoverable"
|
|
165
|
+
| "terminal"
|
|
166
|
+
| "unknown";
|
|
167
|
+
|
|
168
|
+
/** Allow-listed provider detail, retained only at raw capture. */
|
|
169
|
+
export interface ConversationErrorDetails {
|
|
170
|
+
readonly providerCode?: string | number;
|
|
171
|
+
readonly rpcCode?: number;
|
|
172
|
+
readonly service?: string;
|
|
173
|
+
readonly exitCode?: number;
|
|
174
|
+
readonly signal?: string;
|
|
175
|
+
readonly transportState?: string;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Immutable, sanitized diagnosis at a meaningful conversation boundary. */
|
|
179
|
+
export interface ErrorRecord extends ConversationRecordEnvelope<"error"> {
|
|
180
|
+
readonly source: ConversationErrorSource;
|
|
181
|
+
/** Stable machine-readable value. The type stays open for future codes. */
|
|
182
|
+
readonly code: string;
|
|
183
|
+
readonly recoverability: ConversationErrorRecoverability;
|
|
184
|
+
/** Sanitized fallback text intended for display, not a raw exception dump. */
|
|
185
|
+
readonly message?: string;
|
|
186
|
+
readonly operationId?: string;
|
|
187
|
+
readonly transitionId?: string;
|
|
188
|
+
readonly internalSessionId?: string;
|
|
189
|
+
readonly details?: ConversationErrorDetails;
|
|
190
|
+
}
|
|
191
|
+
|
|
153
192
|
export type ConversationRecord =
|
|
154
193
|
| MessageRecord
|
|
155
194
|
| ToolCallRecord
|
|
@@ -160,7 +199,8 @@ export type ConversationRecord =
|
|
|
160
199
|
| AgentActionRecord
|
|
161
200
|
| ThinkingRecord
|
|
162
201
|
| ActivityRecord
|
|
163
|
-
| MeteringReferenceRecord
|
|
202
|
+
| MeteringReferenceRecord
|
|
203
|
+
| ErrorRecord;
|
|
164
204
|
|
|
165
205
|
type WithoutSequence<T> = T extends ConversationRecord ? Omit<T, "sequence"> : never;
|
|
166
206
|
export type NewConversationRecord = WithoutSequence<ConversationRecord>;
|
|
@@ -209,6 +249,7 @@ export const DEFAULT_CONVERSATION_RECORD_CAPTURE: ConversationRecordCapturePolic
|
|
|
209
249
|
thinking: "none",
|
|
210
250
|
activity: "metadata",
|
|
211
251
|
metering: "metadata",
|
|
252
|
+
error: "content",
|
|
212
253
|
});
|
|
213
254
|
|
|
214
255
|
export interface AgentPersistenceIdentity {
|
|
@@ -822,6 +863,10 @@ export function searchableText(record: ConversationRecord): string {
|
|
|
822
863
|
return [record.activityType, contentText(record.content), jsonText(record.data)].filter(Boolean).join("\n");
|
|
823
864
|
case "metering":
|
|
824
865
|
return [record.operationId, record.meteringRecordId, record.status].join("\n");
|
|
866
|
+
case "error":
|
|
867
|
+
// Errors are operational history, not searchable conversation content; v1 keeps them
|
|
868
|
+
// out of full-text search.
|
|
869
|
+
return "";
|
|
825
870
|
}
|
|
826
871
|
}
|
|
827
872
|
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ConversationErrorDetails,
|
|
3
|
+
ConversationErrorRecoverability,
|
|
4
|
+
ConversationErrorSource,
|
|
5
|
+
} from "./backend.ts";
|
|
6
|
+
|
|
7
|
+
/** Machine-readable codes the library owns. Readers must tolerate unknown future values. */
|
|
8
|
+
export const CONVERSATION_ERROR_CODES = Object.freeze([
|
|
9
|
+
"provider_session_create_failed",
|
|
10
|
+
"provider_session_load_failed",
|
|
11
|
+
"provider_session_missing",
|
|
12
|
+
"provider_process_exited",
|
|
13
|
+
"connection_lost",
|
|
14
|
+
"prompt_interrupted",
|
|
15
|
+
"recovery_failed",
|
|
16
|
+
"unknown",
|
|
17
|
+
] as const);
|
|
18
|
+
|
|
19
|
+
export type ConversationErrorCode = (typeof CONVERSATION_ERROR_CODES)[number];
|
|
20
|
+
|
|
21
|
+
export const CONVERSATION_ERROR_SOURCES: readonly ConversationErrorSource[] = Object.freeze([
|
|
22
|
+
"provider_session",
|
|
23
|
+
"transport",
|
|
24
|
+
"operation",
|
|
25
|
+
"recovery",
|
|
26
|
+
]);
|
|
27
|
+
|
|
28
|
+
export const CONVERSATION_ERROR_RECOVERABILITIES: readonly ConversationErrorRecoverability[] =
|
|
29
|
+
Object.freeze(["retryable", "recoverable", "terminal", "unknown"]);
|
|
30
|
+
|
|
31
|
+
const MAX_ERROR_MESSAGE_CHARS = 512;
|
|
32
|
+
const MAX_ERROR_DETAIL_CHARS = 128;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Reduce an arbitrary thrown value to a short, safe, single-line display string.
|
|
36
|
+
*
|
|
37
|
+
* The ordinary message must never carry a stack trace, credentials, a prompt, environment
|
|
38
|
+
* values, filesystem contents, or a raw provider payload. Producers normally supply a
|
|
39
|
+
* curated message; this is the safety net when a raw error is passed instead. Anything it
|
|
40
|
+
* cannot reduce to a short non-blank string is dropped rather than persisted.
|
|
41
|
+
*/
|
|
42
|
+
export function sanitizeConversationErrorMessage(value: unknown): string | undefined {
|
|
43
|
+
const raw = typeof value === "string"
|
|
44
|
+
? value
|
|
45
|
+
: value instanceof Error
|
|
46
|
+
? value.message
|
|
47
|
+
: undefined;
|
|
48
|
+
if (raw === undefined) return undefined;
|
|
49
|
+
const firstLine = raw.split(/\r?\n/, 1)[0]?.replace(/\s+/g, " ").trim() ?? "";
|
|
50
|
+
if (!firstLine) return undefined;
|
|
51
|
+
return firstLine.slice(0, MAX_ERROR_MESSAGE_CHARS);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface ConversationErrorDetailInput {
|
|
55
|
+
readonly providerCode?: unknown;
|
|
56
|
+
readonly rpcCode?: unknown;
|
|
57
|
+
readonly service?: unknown;
|
|
58
|
+
readonly exitCode?: unknown;
|
|
59
|
+
readonly signal?: unknown;
|
|
60
|
+
readonly transportState?: unknown;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Build the raw-capture detail bag from allow-listed fields only.
|
|
65
|
+
*
|
|
66
|
+
* Unknown provider fields are discarded rather than recursively copied or heuristically
|
|
67
|
+
* redacted. Returns undefined when no allow-listed field is present, so `details` is absent
|
|
68
|
+
* rather than an empty object.
|
|
69
|
+
*/
|
|
70
|
+
export function buildConversationErrorDetails(
|
|
71
|
+
input: ConversationErrorDetailInput,
|
|
72
|
+
): ConversationErrorDetails | undefined {
|
|
73
|
+
const providerCode = allowCode(input.providerCode);
|
|
74
|
+
const rpcCode = allowInteger(input.rpcCode);
|
|
75
|
+
const service = allowLabel(input.service);
|
|
76
|
+
const exitCode = allowInteger(input.exitCode);
|
|
77
|
+
const signal = allowLabel(input.signal);
|
|
78
|
+
const transportState = allowLabel(input.transportState);
|
|
79
|
+
const details: ConversationErrorDetails = Object.freeze({
|
|
80
|
+
...(providerCode === undefined ? {} : { providerCode }),
|
|
81
|
+
...(rpcCode === undefined ? {} : { rpcCode }),
|
|
82
|
+
...(service === undefined ? {} : { service }),
|
|
83
|
+
...(exitCode === undefined ? {} : { exitCode }),
|
|
84
|
+
...(signal === undefined ? {} : { signal }),
|
|
85
|
+
...(transportState === undefined ? {} : { transportState }),
|
|
86
|
+
});
|
|
87
|
+
return Object.keys(details).length > 0 ? details : undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function allowCode(value: unknown): string | number | undefined {
|
|
91
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
92
|
+
return allowLabel(value);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function allowInteger(value: unknown): number | undefined {
|
|
96
|
+
return typeof value === "number" && Number.isSafeInteger(value) ? value : undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function allowLabel(value: unknown): string | undefined {
|
|
100
|
+
if (typeof value !== "string") return undefined;
|
|
101
|
+
const trimmed = value.trim();
|
|
102
|
+
if (!trimmed) return undefined;
|
|
103
|
+
return trimmed.slice(0, MAX_ERROR_DETAIL_CHARS);
|
|
104
|
+
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { Database } from "bun:sqlite";
|
|
2
2
|
import type { AgentConversationId, AgentSessionId } from "../agent-ids.ts";
|
|
3
3
|
import type {
|
|
4
|
+
ConversationErrorDetails,
|
|
5
|
+
ConversationErrorRecoverability,
|
|
6
|
+
ConversationErrorSource,
|
|
4
7
|
ConversationRecord,
|
|
5
8
|
ConversationRecordKind,
|
|
6
9
|
NewConversationRecord,
|
|
@@ -24,6 +27,7 @@ type SqlValue = string | number | bigint | boolean | Uint8Array | null;
|
|
|
24
27
|
|
|
25
28
|
export function operationIdFromRecord(record: NewConversationRecord): string | null {
|
|
26
29
|
if (record.kind === "metering") return record.operationId;
|
|
30
|
+
if (record.kind === "error") return record.operationId ?? null;
|
|
27
31
|
if (!record.metadata) return null;
|
|
28
32
|
try {
|
|
29
33
|
const metadata = JSON.parse(record.metadata) as { operationId?: unknown };
|
|
@@ -194,6 +198,22 @@ export function insertConversationRecordDetail(
|
|
|
194
198
|
record.status,
|
|
195
199
|
);
|
|
196
200
|
return;
|
|
201
|
+
case "error":
|
|
202
|
+
run(database,
|
|
203
|
+
`INSERT INTO conversation_error_records(
|
|
204
|
+
record_row_id, source, code, recoverability, message, transition_id,
|
|
205
|
+
internal_session_id, details_json
|
|
206
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
207
|
+
rowId,
|
|
208
|
+
record.source,
|
|
209
|
+
record.code,
|
|
210
|
+
record.recoverability,
|
|
211
|
+
record.message ?? null,
|
|
212
|
+
record.transitionId ?? null,
|
|
213
|
+
record.internalSessionId ?? null,
|
|
214
|
+
encodeJson(record.details as PersistenceJsonValue | undefined),
|
|
215
|
+
);
|
|
216
|
+
return;
|
|
197
217
|
}
|
|
198
218
|
}
|
|
199
219
|
|
|
@@ -240,6 +260,7 @@ const DETAIL_TABLES: Readonly<Record<ConversationRecordKind, string>> = Object.f
|
|
|
240
260
|
thinking: "conversation_thinking_events",
|
|
241
261
|
activity: "conversation_activity_events",
|
|
242
262
|
metering: "conversation_metering_references",
|
|
263
|
+
error: "conversation_error_records",
|
|
243
264
|
});
|
|
244
265
|
|
|
245
266
|
function recordFromRows(
|
|
@@ -360,6 +381,23 @@ function recordFromRows(
|
|
|
360
381
|
meteringRecordId: string(detail.metering_record_id),
|
|
361
382
|
status: string(detail.status) as "available" | "partial" | "unavailable",
|
|
362
383
|
});
|
|
384
|
+
case "error":
|
|
385
|
+
return Object.freeze({
|
|
386
|
+
...envelope,
|
|
387
|
+
kind: "error",
|
|
388
|
+
source: string(detail.source) as ConversationErrorSource,
|
|
389
|
+
code: string(detail.code),
|
|
390
|
+
recoverability: string(detail.recoverability) as ConversationErrorRecoverability,
|
|
391
|
+
...(detail.message !== null ? { message: string(detail.message) } : {}),
|
|
392
|
+
...(row.operation_id !== null ? { operationId: string(row.operation_id) } : {}),
|
|
393
|
+
...(detail.transition_id !== null ? { transitionId: string(detail.transition_id) } : {}),
|
|
394
|
+
...(detail.internal_session_id !== null
|
|
395
|
+
? { internalSessionId: string(detail.internal_session_id) }
|
|
396
|
+
: {}),
|
|
397
|
+
...(detail.details_json !== null
|
|
398
|
+
? { details: decodeJson(detail.details_json) as ConversationErrorDetails }
|
|
399
|
+
: {}),
|
|
400
|
+
});
|
|
363
401
|
}
|
|
364
402
|
}
|
|
365
403
|
|