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
2 changes: 1 addition & 1 deletion .claude-plugin/marketplace.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
"name": "omni",
"description": "Full Omni platform control — multichannel messaging, automations, events, batch ops via the omni CLI",
"version": "2.260603.1",
"version": "2.260603.3",
"author": {
"name": "Automagik"
},
Expand Down
2 changes: 1 addition & 1 deletion apps/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@omni/ui",
"version": "2.260603.1",
"version": "2.260603.3",
"private": true,
"type": "module",
"scripts": {
Expand Down
36 changes: 18 additions & 18 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "omni-v2",
"version": "2.260603.1",
"version": "2.260603.3",
"private": true,
"type": "module",
"workspaces": ["packages/*", "apps/*"],
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@omni/api",
"version": "2.260603.1",
"version": "2.260603.3",
"type": "module",
"exports": {
".": {
Expand Down
47 changes: 47 additions & 0 deletions packages/api/src/plugins/__tests__/session-cleaner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, expect, it } from 'bun:test';
import { clearAgentSession } from '../session-cleaner';

function makeDbWithAgentProvider(providerId: string) {
return {
select: () => ({
from: () => ({
where: () => ({
limit: async () => [{ agentProviderId: providerId }],
}),
}),
}),
} as never;
}

describe('session-cleaner canonical KHAL reset guard', () => {
it('fails closed for Agno/KHAL resets when canonical person identity cannot be resolved', async () => {
const services = {
agentRunner: {
getInstanceWithProvider: async () => ({
id: 'inst-1',
agentId: 'agent-1',
channel: 'whatsapp-gupshup',
agentSessionStrategy: 'per_chat',
}),
},
providers: {
getById: async () => ({
id: 'provider-1',
schema: 'agno',
baseUrl: 'http://agno.invalid',
apiKey: '',
defaultTimeout: 1,
}),
},
chats: {
findByExternalIdSmart: async () => null,
},
} as never;

await expect(
clearAgentSession(services, makeDbWithAgentProvider('provider-1'), 'inst-1', '5547996094523', '5547996094523', {
rawPayload: { headers: { 'x-khal-env': 'hml' } },
}),
).rejects.toThrow('Canonical KHAL session resolution failed');
});
});
47 changes: 38 additions & 9 deletions packages/api/src/plugins/agent-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {
getSplitDelayConfig,
shouldAgentReply,
} from '../services/agent-runner';
import { resolveKhalSessionId } from '../services/agent-session-identity';
import { buildWhatsAppMessageContext, extractPhoneFromJid } from '../services/message-context';
import type { ResolvedRoute } from '../services/route-resolver';
import { publishTurnOpen } from '../services/turn-events';
Expand Down Expand Up @@ -1776,11 +1777,20 @@ async function dispatchViaStreamingProvider(
if (!messageTexts.length && mediaFiles.length) messageTexts.push('[Media message]');

const rawThreadId = extractThreadId(messages);
const explicitKhalSessionId = extractKhalSessionId(messages);
const sessionId =
explicitKhalSessionId ??
computeSessionId(instance.agentSessionStrategy ?? 'per_chat', senderId, chatId, rawThreadId);
const rawPl = (messages[0]?.payload.rawPayload ?? {}) as Record<string, unknown>;
const sessionIdentity = resolveKhalSessionId({
providerSchema: resolved.provider.schema,
sessionStrategy: instance.agentSessionStrategy ?? 'per_chat',
from: senderId,
chatId,
channel,
instanceId: instance.id,
personId,
rawPayload: rawPl,
threadId: rawThreadId,
});
const sessionId = sessionIdentity.sessionId;
const explicitKhalSessionId = sessionIdentity.canonicalSessionId;
const replyToId = messages[0]?.payload.replyToId ?? messages[0]?.payload.externalId;

const currentMessageIds = messages.map((msg) => msg.payload.externalId).filter((id): id is string => !!id);
Expand Down Expand Up @@ -2205,10 +2215,20 @@ async function dispatchViaProvider(
}

const rawThreadId = extractThreadId(messages);
const explicitKhalSessionId = extractKhalSessionId(messages);
const sessionId =
explicitKhalSessionId ??
computeSessionId(instance.agentSessionStrategy ?? 'per_chat', senderId, chatId, rawThreadId);
const rawPl = (messages[0]?.payload.rawPayload ?? {}) as Record<string, unknown>;
const sessionIdentity = resolveKhalSessionId({
providerSchema: provider.schema,
sessionStrategy: instance.agentSessionStrategy ?? 'per_chat',
from: senderId,
chatId,
channel,
instanceId: instance.id,
personId,
rawPayload: rawPl,
threadId: rawThreadId,
});
const sessionId = sessionIdentity.sessionId;
const explicitKhalSessionId = sessionIdentity.canonicalSessionId;

// Build context messages for group and DM conversations (messages since last bot response)
const currentMessageIds = messages.map((msg) => msg.payload.externalId).filter((id): id is string => !!id);
Expand Down Expand Up @@ -3917,7 +3937,16 @@ async function processReactionTrigger(
// Build AgentTrigger for the provider
const effectivePersonId = reactionPersonId ?? metadata.personId;
const senderName = await services.agentRunner.getSenderName(effectivePersonId, undefined);
const sessionId = computeSessionId(instance.agentSessionStrategy ?? 'per_chat', payload.from, externalChatId);
const sessionId = resolveKhalSessionId({
providerSchema: provider.schema,
sessionStrategy: instance.agentSessionStrategy ?? 'per_chat',
from: payload.from,
chatId: externalChatId,
channel,
instanceId: instance.id,
personId: effectivePersonId,
rawPayload: payload.rawPayload as Record<string, unknown> | undefined,
}).sessionId;
const customerContext = await resolveCustomerContext(services, effectivePersonId);

const trigger: AgentTrigger = {
Expand Down
Loading
Loading