feat(core): compact v2 session context (#30986)
This commit is contained in:
@@ -10,10 +10,8 @@ export type MemoryState = {
|
||||
export interface Adapter {
|
||||
readonly getCurrentAssistant: () => Effect.Effect<SessionMessage.Assistant | undefined>
|
||||
readonly getAssistant: (messageID: SessionMessage.ID) => Effect.Effect<SessionMessage.Assistant | undefined>
|
||||
readonly getCurrentCompaction: () => Effect.Effect<SessionMessage.Compaction | undefined>
|
||||
readonly getCurrentShell: (callID: string) => Effect.Effect<SessionMessage.Shell | undefined>
|
||||
readonly updateAssistant: (assistant: SessionMessage.Assistant) => Effect.Effect<void>
|
||||
readonly updateCompaction: (compaction: SessionMessage.Compaction) => Effect.Effect<void>
|
||||
readonly updateShell: (shell: SessionMessage.Shell) => Effect.Effect<void>
|
||||
readonly appendMessage: (message: SessionMessage.Message) => Effect.Effect<void>
|
||||
}
|
||||
@@ -23,7 +21,6 @@ export function memory(state: MemoryState): Adapter {
|
||||
state.messages.findLastIndex((message) => message.id === messageID)
|
||||
// A newer turn supersedes stale incomplete rows; never resume an older assistant projection.
|
||||
const latestAssistantIndex = () => state.messages.findLastIndex((message) => message.type === "assistant")
|
||||
const activeCompactionIndex = () => state.messages.findLastIndex((message) => message.type === "compaction")
|
||||
const activeShellIndex = (callID: string) =>
|
||||
state.messages.findLastIndex((message) => message.type === "shell" && message.callID === callID)
|
||||
|
||||
@@ -44,14 +41,6 @@ export function memory(state: MemoryState): Adapter {
|
||||
return assistant?.type === "assistant" ? assistant : undefined
|
||||
})
|
||||
},
|
||||
getCurrentCompaction() {
|
||||
return Effect.sync(() => {
|
||||
const index = activeCompactionIndex()
|
||||
if (index < 0) return
|
||||
const compaction = state.messages[index]
|
||||
return compaction?.type === "compaction" ? compaction : undefined
|
||||
})
|
||||
},
|
||||
getCurrentShell(callID) {
|
||||
return Effect.sync(() => {
|
||||
const index = activeShellIndex(callID)
|
||||
@@ -69,15 +58,6 @@ export function memory(state: MemoryState): Adapter {
|
||||
state.messages[index] = assistant
|
||||
})
|
||||
},
|
||||
updateCompaction(compaction) {
|
||||
return Effect.sync(() => {
|
||||
const index = activeCompactionIndex()
|
||||
if (index < 0) return
|
||||
const current = state.messages[index]
|
||||
if (current?.type !== "compaction") return
|
||||
state.messages[index] = compaction
|
||||
})
|
||||
},
|
||||
updateShell(shell) {
|
||||
return Effect.sync(() => {
|
||||
const index = activeShellIndex(shell.callID)
|
||||
@@ -387,43 +367,21 @@ export function update(adapter: Adapter, event: SessionEvent.Event) {
|
||||
})
|
||||
},
|
||||
"session.next.retried": () => Effect.void,
|
||||
"session.next.compaction.started": (event) => {
|
||||
"session.next.compaction.started": () => Effect.void,
|
||||
"session.next.compaction.delta": () => Effect.void,
|
||||
"session.next.compaction.ended": (event) => {
|
||||
return adapter.appendMessage(
|
||||
new SessionMessage.Compaction({
|
||||
id: event.data.messageID,
|
||||
type: "compaction",
|
||||
metadata: event.metadata,
|
||||
reason: event.data.reason,
|
||||
summary: "",
|
||||
summary: event.data.text,
|
||||
recent: event.data.recent,
|
||||
time: { created: event.data.timestamp },
|
||||
}),
|
||||
)
|
||||
},
|
||||
"session.next.compaction.delta": (event) => {
|
||||
return Effect.gen(function* () {
|
||||
const currentCompaction = yield* adapter.getCurrentCompaction()
|
||||
if (currentCompaction) {
|
||||
yield* adapter.updateCompaction(
|
||||
produce(currentCompaction, (draft) => {
|
||||
draft.summary += event.data.text
|
||||
}),
|
||||
)
|
||||
}
|
||||
})
|
||||
},
|
||||
"session.next.compaction.ended": (event) => {
|
||||
return Effect.gen(function* () {
|
||||
const currentCompaction = yield* adapter.getCurrentCompaction()
|
||||
if (currentCompaction) {
|
||||
yield* adapter.updateCompaction(
|
||||
produce(currentCompaction, (draft) => {
|
||||
draft.summary = event.data.text
|
||||
draft.include = event.data.include
|
||||
}),
|
||||
)
|
||||
}
|
||||
})
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user