-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Expand file tree
/
Copy pathconversation.ts
More file actions
458 lines (414 loc) · 13.7 KB
/
conversation.ts
File metadata and controls
458 lines (414 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { UserIdAndName } from '../base/users';
import type { ToolOrigin } from '../tools/definition';
import type { ToolResult } from '../tools/tool_result';
import type { ExecutionStatus, SerializedExecutionError } from '../agents/execution_status';
import type {
Attachment,
VersionedAttachment,
AttachmentInput,
AttachmentVersionRef,
} from '../attachments';
import type { PromptRequest, PromptResponse, PromptStorageState } from '../agents/prompts';
import type { RuntimeAgentConfigurationOverrides } from '../agents/definition';
import type { RoundState } from './round_state';
/**
* Represents the input that initiated a conversation round.
*/
export interface RoundInput {
/**
* A text message from the user.
*/
message: string;
/**
* Optional attachments to provide to the agent.
* @deprecated Use attachment_refs with conversation-level attachments instead
*/
attachments?: Attachment[];
/**
* References to versioned conversation-level attachments.
*/
attachment_refs?: AttachmentVersionRef[];
}
/**
* Represents the input used to interact with an agent (new round, resume round)
*/
export interface ConverseInput {
/**
* A text message from the user.
*/
message?: string;
/**
* Optional attachments to provide to the agent.
* Use `origin` without `data` for by-reference types that implement `resolve`.
* @deprecated Use attachment_refs with conversation-level attachments instead
*/
attachments?: AttachmentInput[];
/**
* References to versioned conversation-level attachments.
*/
attachment_refs?: AttachmentVersionRef[];
/**
* Response from the user to prompt requests.
*/
prompts?: Record<string, PromptResponse>;
}
/**
* Represents the final answer from the agent in a conversation round.
*/
export interface AssistantResponse {
/**
* The text message from the assistant.
*/
message: string;
structured_output?: object;
}
export enum ConversationRoundStepType {
toolCall = 'tool_call',
reasoning = 'reasoning',
compaction = 'compaction',
backgroundAgentComplete = 'background_agent_complete',
updateTodos = 'update_todos',
}
// tool call step
export type ConversationRoundStepMixin<TType extends ConversationRoundStepType, TData> = TData & {
type: TType;
};
/**
* Tool call progress which were emitted during the tool execution
*/
export interface ToolCallProgress {
/**
* The full text message
*/
message: string;
/**
* Optional structured metadata attached to this progress event.
*/
metadata?: Record<string, string>;
}
/**
* Represents a tool call with the corresponding result.
*/
export interface ToolCallWithResult {
/**
* Id of the tool call, as returned by the LLM
*/
tool_call_id: string;
/**
* Identifier of the tool.
*/
tool_id: string;
/**
* Arguments the tool was called with.
*/
params: Record<string, any>;
/**
* List of progress message which were send during that tool call
*/
progression?: ToolCallProgress[];
/**
* Result of the tool
*/
results: ToolResult[];
/**
* Optional group ID shared by tool calls that were executed in parallel from the same LLM response
*/
tool_call_group_id?: string;
tool_origin?: ToolOrigin;
}
export type ToolCallStep = ConversationRoundStepMixin<
ConversationRoundStepType.toolCall,
ToolCallWithResult
>;
export const createToolCallStep = (toolCallWithResult: ToolCallWithResult): ToolCallStep => {
return {
type: ConversationRoundStepType.toolCall,
...toolCallWithResult,
};
};
export const isToolCallStep = (step: ConversationRoundStep): step is ToolCallStep => {
return step.type === ConversationRoundStepType.toolCall;
};
export const createReasoningStep = (reasoningStepWithResult: ReasoningStepData): ReasoningStep => {
return {
type: ConversationRoundStepType.reasoning,
...reasoningStepWithResult,
};
};
export interface ReasoningStepData {
/** plain text reasoning content */
reasoning: string;
/** if true, will not be displayed in the thinking panel, only used as "current thinking" **/
transient?: boolean;
/** when reasoning is bound to a tool call, the corresponding tool call ID */
tool_call_id?: string;
/** when reasoning is bound to a tool call group, the corresponding tool call group ID */
tool_call_group_id?: string;
}
export type ReasoningStep = ConversationRoundStepMixin<
ConversationRoundStepType.reasoning,
ReasoningStepData
>;
export const isReasoningStep = (step: ConversationRoundStep): step is ReasoningStep => {
return step.type === ConversationRoundStepType.reasoning;
};
/**
* Defines all possible types for round steps.
*/
// compaction step
export interface CompactionStepData {
/** Number of conversation rounds that were summarized into a compact form */
summarized_round_count: number;
/** Estimated token count of the conversation before compaction */
token_count_before: number;
/** Estimated token count of the conversation after compaction */
token_count_after: number;
}
export type CompactionStep = ConversationRoundStepMixin<
ConversationRoundStepType.compaction,
CompactionStepData
>;
export const isCompactionStep = (step: ConversationRoundStep): step is CompactionStep => {
return step.type === ConversationRoundStepType.compaction;
};
export type BackgroundAgentCompleteStep = ConversationRoundStepMixin<
ConversationRoundStepType.backgroundAgentComplete,
BackgroundExecutionState
>;
export const isBackgroundAgentCompleteStep = (
step: ConversationRoundStep
): step is BackgroundAgentCompleteStep => {
return step.type === ConversationRoundStepType.backgroundAgentComplete;
};
export interface TodosStepData {
todos: TodoItem[];
/** True when todos were inherited from the previous round, not written by the agent this round */
carried_over?: boolean;
}
export type TodosStep = ConversationRoundStepMixin<
ConversationRoundStepType.updateTodos,
TodosStepData
>;
export const isTodosStep = (step: ConversationRoundStep): step is TodosStep => {
return step.type === ConversationRoundStepType.updateTodos;
};
/**
* Returns the (single) todos step from a list of steps, if present.
* A round only ever has at most one todos step, which is updated in place.
*/
export const findTodosStep = (
steps: ConversationRoundStep[] | undefined
): TodosStep | undefined => {
return steps?.find(isTodosStep);
};
/**
* Returns the todo list to carry over from the previous round, or undefined if nothing should carry over.
* Carryover only happens when at least one item is still incomplete (pending / in_progress).
* When carried over, both complete and incomplete items are included so the full plan is visible.
*/
export const carriedOverTodos = (todos: TodoItem[] | undefined): TodoItem[] | undefined => {
if (!todos?.length) return undefined;
const hasIncomplete = todos.some((t) => t.status !== 'completed' && t.status !== 'cancelled');
return hasIncomplete ? todos : undefined;
};
export type ConversationRoundStep =
| ToolCallStep
| ReasoningStep
| CompactionStep
| BackgroundAgentCompleteStep
| TodosStep;
export enum ConversationRoundStatus {
/** round is currently being processed */
inProgress = 'in_progress',
/** the round is completed */
completed = 'completed',
/** round has been interrupted and is awaiting user input */
awaitingPrompt = 'awaiting_prompt',
}
/**
* Represents a round in a conversation, containing all the information
* related to this particular round.
*/
export interface ConversationRound {
/** unique id for this round */
id: string;
/** current status of the round */
status: ConversationRoundStatus;
/** persisted state to resume interrupted states */
state?: RoundState;
/** if status is awaiting_prompt, contains the current prompt requests */
pending_prompts?: PromptRequest[];
/** The user input that initiated the round */
input: RoundInput;
/** List of intermediate steps before the end result, such as tool calls */
steps: ConversationRoundStep[];
/** The final response from the assistant */
response: AssistantResponse;
/** when the round was started */
started_at: string;
/** time it took to first token, in ms */
time_to_first_token: number;
/** time it took to last token, in ms */
time_to_last_token: number;
/** Model Usage statistics for this round */
model_usage: RoundModelUsageStats;
/** when tracing is enabled, contains the traceId associated with this round */
trace_id?: string | string[];
/** Runtime configuration overrides that were applied to this round */
configuration_overrides?: RuntimeAgentConfigurationOverrides;
}
export interface RoundModelUsageStats {
/**
* Id of the connector used for this round
*/
connector_id: string;
/**
* Number of LLM calls which were done during this round.
*/
llm_calls: number;
/**
* Total number of input tokens sent this round.
*/
input_tokens: number;
/**
* Total number of output tokens received this round.
*/
output_tokens: number;
/**
* Model identifier from the provider response, if available.
*/
model?: string;
}
/**
* Main structure representing a conversation with an agent.
*/
export interface Conversation {
/** unique id for this conversation */
id: string;
/** id of the agent this conversation is bound to */
agent_id: string;
/** info of the owner of the discussion */
user: UserIdAndName;
/** title of the conversation */
title: string;
/** creation date */
created_at: string;
/** update date */
updated_at: string;
/** list of round for this conversation */
rounds: ConversationRound[];
/**
* Conversation-level versioned attachments.
* These attachments are shared across all rounds and can be referenced via attachment_refs.
*/
attachments?: VersionedAttachment[];
/**
* Internal representation of the prompt storage state for the conversation.
* Keeps track of which prompts have been answered and the response.
*/
state?: ConversationInternalState;
/**
* Whether the conversation has been marked as read.
* Any new or updated conversation has `read` set to `false` by default
*/
read?: boolean;
/** current status of the conversation */
status?: ConversationRoundStatus;
}
export type TodoStatus = 'pending' | 'in_progress' | 'completed' | 'cancelled';
export interface TodoItem {
content: string;
status: TodoStatus;
}
/**
* Internal storage for the conversation's arbitrary state.
* Used for example to keep track of the prompt responses.
*/
export interface ConversationInternalState {
prompt?: PromptStorageState;
/**
* Dynamic tool IDs that were added during conversation rounds.
* These tools are persisted across rounds so they remain available.
*/
dynamic_tool_ids?: string[];
/**
* Summary of compacted older conversation rounds.
* Generated when the conversation approaches the model's context window limit.
* Reused across rounds until regeneration is needed.
*/
compaction_summary?: CompactionSummary;
/** Background sub-agent executions keyed by execution ID. */
background_executions?: Record<string, BackgroundExecutionState>;
/** Active todo list for the current conversation. Replaced wholesale on each write. */
todos?: TodoItem[];
}
export interface BackgroundExecutionCompletedAt {
/** The round it was completed at. */
round_id: string;
/** If completion was resolved inside a round, the last tool call group ID before completion. */
tool_call_group_id?: string;
}
export interface BackgroundExecutionState {
/** The execution ID of the background sub-agent. */
execution_id: string;
/** Current status of the execution. */
status: ExecutionStatus;
/** The sub-agent's response, present when status is 'completed'. */
response?: AssistantResponse;
/** Error details, present when status is 'failed'. */
error?: SerializedExecutionError;
/** When and where the execution completed, for positioning the notification. */
completed_at?: BackgroundExecutionCompletedAt;
}
export type ConversationWithoutRounds = Omit<Conversation, 'rounds'>;
export type ConversationAction = 'regenerate';
// Compaction summary types
/** Compact representation of a tool call in a compaction summary */
export interface CompactionToolCallSummary {
tool_id: string;
/** Short stringified summary of the params the tool was called with */
params_summary: string;
}
/** Structured entity extracted during compaction */
export interface CompactionEntity {
type: string;
name: string;
}
/**
* Structured data produced by the compaction pipeline.
* Semantic fields (discussion_summary, user_intent, key_topics,
* outcomes_and_decisions, unanswered_questions, entities) are LLM-generated.
* Deterministic fields (tool_calls_summary, agent_actions)
* are extracted programmatically from the round data.
*/
export interface CompactionStructuredData {
discussion_summary: string;
user_intent: string;
key_topics: string[];
outcomes_and_decisions: string[];
agent_actions: string[];
entities: CompactionEntity[];
unanswered_questions: string[];
tool_calls_summary: CompactionToolCallSummary[];
}
/**
* Summary of compacted conversation rounds.
* Stored at the conversation level and reused across rounds
* until the context window fills up again and regeneration is needed.
*/
export interface CompactionSummary {
/** Number of rounds that were summarized */
summarized_round_count: number;
/** When the summary was generated */
created_at: string;
/** Estimated token count of the serialized summary */
token_count: number;
/** Structured summary data */
structured_data: CompactionStructuredData;
}