Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions packages/core/src/middlewares/chat/rollback_chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ function getTargetConversation(context: ChainMiddlewareContext) {
)
}

function getConversationId(context: ChainMiddlewareContext) {
return (
context.options.conversation?.conversationId ??
context.options.conversation?.conversation?.id
)
}

export function apply(ctx: Context, config: Config, chain: ChatChain) {
chain
.middleware('rollback_chat', async (session, context) => {
Expand All @@ -37,23 +30,21 @@ export function apply(ctx: Context, config: Config, chain: ChatChain) {

const rollbackRound = context.options.rollback_round ?? 1
const targetConversation = getTargetConversation(context)
const conversationId =
targetConversation == null
? getConversationId(context)
: undefined
const resolved =
await ctx.chatluna.conversation.resolveConversation(session, {
targetConversation,
conversationId,
presetLane: context.options.presetLane,
allPresetLanes: context.options.allPresetLanes,
permission: 'manage',
useRoutePresetLane:
context.options.presetLane == null &&
targetConversation == null &&
conversationId == null,
mode: 'target'
})
targetConversation == null
? context.options.conversation
: await ctx.chatluna.conversation.resolveConversation(
session,
{
targetConversation,
presetLane: context.options.presetLane,
allPresetLanes: context.options.allPresetLanes,
permission: 'manage',
useRoutePresetLane:
context.options.presetLane == null,
mode: 'target'
}
)
const conversation = resolved.conversation

if (conversation == null) {
Expand Down
24 changes: 14 additions & 10 deletions packages/core/src/middlewares/chat/stop_chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ export function apply(ctx: Context, config: Config, chain: ChatChain) {

const targetConversation = getTargetConversation(context)
const resolved =
await ctx.chatluna.conversation.resolveConversation(session, {
targetConversation,
presetLane: context.options.presetLane,
allPresetLanes: context.options.allPresetLanes,
permission: 'manage',
useRoutePresetLane:
context.options.presetLane == null &&
targetConversation == null,
mode: 'target'
})
targetConversation == null
? context.options.conversation
: await ctx.chatluna.conversation.resolveConversation(
session,
{
targetConversation,
presetLane: context.options.presetLane,
allPresetLanes: context.options.allPresetLanes,
permission: 'manage',
useRoutePresetLane:
context.options.presetLane == null,
mode: 'target'
}
)
const conversation = resolved.conversation

if (conversation == null) {
Expand Down
9 changes: 6 additions & 3 deletions packages/core/src/services/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,12 +706,12 @@ export class ConversationService {
const merged = new Set(filtered.map((c) => c.bindingKey)).size > 1

return filtered.sort((a, b) => {
const seq = (a.seq ?? 0) - (b.seq ?? 0)
if (seq !== 0) return seq
if (merged) {
const key = a.bindingKey.localeCompare(b.bindingKey)
if (key !== 0) return key
}
const seq = (a.seq ?? 0) - (b.seq ?? 0)
if (seq !== 0) return seq
const created = a.createdAt.getTime() - b.createdAt.getTime()
if (created !== 0) return created
return a.id.localeCompare(b.id)
Expand Down Expand Up @@ -2038,7 +2038,10 @@ function formatToolCalls(toolCalls: unknown[]): string[] {
}

async function formatMessage(message: MessageRecord) {
const content = JSON.parse(await gzipDecode(message.content))
const content =
message.content == null
? null
: JSON.parse(await gzipDecode(message.content))

const text =
content == null
Expand Down
1 change: 1 addition & 0 deletions packages/core/tests/conversation-runtime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ it('ConversationRuntime chat preserves additional kwargs metadata', async () =>
}
})
},
resolveCallbacks: async (input) => input.callbacks,
ctx: {
root: {
parallel: async () => {}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/tests/conversation-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1660,11 +1660,13 @@ it('ConversationService emits conversation lifecycle events for switch archive r
events.map((item) => item.name),
[
'chatluna/before-conversation-switch',
'chatluna/after-binding-update',
'chatluna/after-conversation-switch',
'chatluna/conversation-compressed',
'chatluna/before-conversation-archive',
'chatluna/after-conversation-archive',
'chatluna/before-conversation-restore',
'chatluna/after-binding-update',
'chatluna/after-conversation-restore',
'chatluna/before-conversation-delete',
'chatluna/after-conversation-delete'
Expand Down
1 change: 0 additions & 1 deletion packages/core/tests/conversation-source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ it('conversation cleanup listeners in downstream packages use conversation lifec
'../../extension-long-memory/src/service/memory.ts'
),
path.resolve(__dirname, '../../extension-agent/src/service/skills.ts'),
path.resolve(__dirname, '../../extension-agent/src/cli/service.ts'),
path.resolve(__dirname, '../../extension-tools/src/plugins/todos.ts')
]

Expand Down
19 changes: 19 additions & 0 deletions packages/core/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@ export class FakeDatabase {
}
}

async set(
table: string,
query: Record<string, unknown>,
update: Record<string, unknown>
) {
const target = (this.tables[table] ??= [])

for (let idx = 0; idx < target.length; idx += 1) {
if (
Object.entries(query).every(
([key, expected]) => target[idx][key] === expected
)
) {
target[idx] = { ...target[idx], ...update }
}
}
}

async remove(table: string, query: Record<string, unknown>) {
const target = (this.tables[table] ??= [])
this.tables[table] = target.filter(
Expand Down Expand Up @@ -384,6 +402,7 @@ export async function createMemoryService(
app.plugin(memory)
app.plugin(ChatLunaService, createConfig(options.config))
await app.start()
app.chatluna.platform.registerChatChain('plugin', {}, () => ({}) as never)
;(
app.chatluna.platform as unknown as {
_models: Record<string, unknown[]>
Expand Down
Loading
Loading