File size: 4,835 Bytes
4e23b01 | 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 | import type { AgentId, AttachmentId, InteractionId, PromptId, TaskId, TodoId, TurnId } from '../model/ids';
import type { TranscriptAttachment } from '../model/attachment';
import type { TranscriptInteraction } from '../model/interaction';
import type { TranscriptItem } from '../model/item';
import type { TranscriptMeta } from '../model/meta';
import type { TranscriptPrompt } from '../model/prompt';
import type { TranscriptTask } from '../model/task';
import type { TranscriptTodo } from '../model/todo';
import type { TranscriptTurn } from '../model/turn';
import {
EMPTY_AGENT_STATE,
applyOperation,
type AgentState,
} from '../ops/apply';
import type {
AgentTranscriptSnapshot,
AppendTarget,
AppliedOps,
TranscriptChangeEvent,
TranscriptOperation,
} from '../ops/operation';
export type TranscriptListener = (event: TranscriptChangeEvent) => void;
export interface Disposable {
dispose(): void;
}
export class AgentTranscript {
#state: AgentState = EMPTY_AGENT_STATE;
readonly #listeners = new Set<TranscriptListener>();
constructor(readonly agentId: AgentId) {}
receive(ops: readonly TranscriptOperation[]): AppliedOps {
return this.apply(ops);
}
apply(ops: readonly TranscriptOperation[]): AppliedOps {
const accepted: TranscriptOperation[] = [];
let gap: AppliedOps['gap'];
let state = this.#state;
for (const op of ops) {
const result = applyOperation(state, op);
if (result.gap) {
gap = { target: (op as { target: AppendTarget }).target, ...result.gap };
continue;
}
if (!result.changed) continue;
state = result.state;
accepted.push(op);
}
this.#state = state;
if (accepted.length > 0) {
const event: TranscriptChangeEvent = { agentId: this.agentId, ops: accepted };
for (const listener of this.#listeners) listener(event);
}
return { accepted, gap };
}
onChange(listener: TranscriptListener): Disposable {
this.#listeners.add(listener);
return { dispose: () => void this.#listeners.delete(listener) };
}
getItems(): readonly TranscriptItem[] {
return this.#state.items;
}
getTurn(turnId: TurnId): TranscriptTurn | undefined {
const item = this.#state.items.find(
(entry) => entry.kind === 'turn' && entry.turnId === turnId,
);
return item?.kind === 'turn' ? item : undefined;
}
getTasks(): ReadonlyMap<TaskId, TranscriptTask> {
return this.#state.tasks;
}
getTask(taskId: TaskId): TranscriptTask | undefined {
return this.#state.tasks.get(taskId);
}
getInteractions(): ReadonlyMap<InteractionId, TranscriptInteraction> {
return this.#state.interactions;
}
getInteraction(interactionId: InteractionId): TranscriptInteraction | undefined {
return this.#state.interactions.get(interactionId);
}
getAttachments(): ReadonlyMap<AttachmentId, TranscriptAttachment> {
return this.#state.attachments;
}
getAttachment(attachmentId: AttachmentId): TranscriptAttachment | undefined {
return this.#state.attachments.get(attachmentId);
}
getTodos(): ReadonlyMap<TodoId, TranscriptTodo> {
return this.#state.todos;
}
getTodo(todoId: TodoId): TranscriptTodo | undefined {
return this.#state.todos.get(todoId);
}
getPrompts(): ReadonlyMap<PromptId, TranscriptPrompt> {
return this.#state.prompts;
}
getPrompt(promptId: PromptId): TranscriptPrompt | undefined {
return this.#state.prompts.get(promptId);
}
getMeta(): TranscriptMeta {
return this.#state.meta;
}
listPendingInteractions(): readonly InteractionId[] {
return [...this.#state.pendingInteractions];
}
get hasMoreOlder(): boolean {
return this.#state.hasMoreOlder;
}
snapshot(window?: { tailTurns: number }): AgentTranscriptSnapshot {
let items = this.#state.items;
let hasMoreOlder = this.#state.hasMoreOlder;
if (window !== undefined) {
const turnCount = items.reduce((n, entry) => (entry.kind === 'turn' ? n + 1 : n), 0);
if (turnCount > window.tailTurns) {
const skip = turnCount - window.tailTurns;
const kept: TranscriptItem[] = [];
let seen = 0;
for (const entry of items) {
if (entry.kind === 'turn') {
seen += 1;
if (seen <= skip) continue;
kept.push(entry);
} else if (seen > skip) {
kept.push(entry);
}
}
items = kept;
hasMoreOlder = true;
}
}
return {
items,
tasks: [...this.#state.tasks.values()],
interactions: [...this.#state.interactions.values()],
attachments: [...this.#state.attachments.values()],
todos: [...this.#state.todos.values()],
prompts: [...this.#state.prompts.values()],
meta: this.#state.meta,
hasMoreOlder,
};
}
}
|