mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-01 18:07:00 +01:00
Merge branch 'codex/speko-voice-runtime' into codex/speko-voice-ui
* codex/speko-voice-runtime: fix: deliver long Speko replies in durable ordered parts
This commit is contained in:
commit
eb6fa7c4f9
3 files changed
+76
-16
No files matched your search
@@ -6,7 +6,7 @@ import { VOICE_RESULT_NOTIFICATION } from "@paperclipai/shared";
|
||||
import { createHash, createHmac, randomUUID } from "node:crypto";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import { chatActions, environments, chatVoiceInboundCalls, chatVoicePhoneLines, chatVoiceReports, heartbeatRuns, toolProfileBindings, chatVoiceCallbacks, agentWakeupRequests, issueThreadInteractions, issueQuestionResponseDeliveries, issueComments, agents, chatConversations, chatDeliveries, chatEndpoints, chatPublications, chatVoiceReplies, chatVoiceSessions, chatVoiceToolCalls, companies, companyMemberships, companySecrets, connectionGrants, connectionGrantMembers, createDb, issues, toolApplications, toolConnections, toolConnectionInstalls } from "@paperclipai/db";
|
||||
import { chatActions, environments, chatVoiceInboundCalls, chatVoicePhoneLines, chatVoiceReports, heartbeatRuns, toolProfileBindings, chatVoiceCallbacks, agentWakeupRequests, issueThreadInteractions, issueQuestionResponseDeliveries, issueComments, agents, chatConversations, chatDeliveries, chatEndpoints, chatPublications, chatVoiceReplies, chatVoiceSessions, chatVoiceToolCalls, companies, companyMemberships, companySecrets, connectionGrants, connectionGrantMembers, createDb, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS, issues, toolApplications, toolConnections, toolConnectionInstalls } from "@paperclipai/db";
|
||||
import { configureSpekoSessionTools } from "../services/voice/speko-tool-setup.js";
|
||||
import { instanceSettingsService } from "../services/instance-settings.js";
|
||||
import { chatChannelService } from "../services/chat-channels.js";
|
||||
@@ -30,7 +30,7 @@ const support = await getEmbeddedPostgresTestSupport();
|
||||
beforeAll(async () => {
|
||||
database = await startEmbeddedPostgresTestDatabase("paperclip-speko-store-");
|
||||
db = createDb(database.connectionString);
|
||||
}, 30_000);
|
||||
}, EMBEDDED_POSTGRES_TEST_TIMEOUT_MS);
|
||||
afterAll(async () => { await database?.cleanup(); });
|
||||
|
||||
async function fixture() {
|
||||
@@ -501,6 +501,41 @@ const support = await getEmbeddedPostgresTestSupport();
|
||||
await f.push();
|
||||
expect(f.transport.sendCallMessage).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
it("sends long approved phone answers as durable ordered parts and resumes after a rate limit", async () => {
|
||||
const f = await pushFixture(), text = "😀 answer ".repeat(4000) + "FINAL";
|
||||
await db.update(chatPublications).set({payload: {text}}).where(eq(chatPublications.id, f.publication.id));
|
||||
f.transport.sendCallMessage.mockResolvedValueOnce({messageId: "part-0"}).mockRejectedValueOnce(new SpekoProviderError("provider_unavailable", false, 429));
|
||||
await f.push();
|
||||
const [reply] = await db.select().from(chatVoiceReplies).where(eq(chatVoiceReplies.publicationId, f.publication.id));
|
||||
expect(reply.deliveredAt).toBeNull();
|
||||
const actions = await db.select().from(chatActions).where(eq(chatActions.kind, "speko_voice_reply_push"));
|
||||
const retry = actions.find(action => action.payload.replyId === reply.id && action.status === "retry")!;
|
||||
expect(retry.payload).toMatchObject({partIndex: 1, partCount: 3});
|
||||
// Polling must not replay an already accepted prefix as a complete answer.
|
||||
const token = f.transport.createBrowserSession.mock.calls[0]![0].toolToken;
|
||||
const toolId = randomUUID(), {sessionId: providerSessionId} = await f.transport.createBrowserSession.mock.results[0]!.value;
|
||||
const envelope = {session_id: providerSessionId, tool_call_id: toolId, idempotency_key: `${providerSessionId}:${toolId}`, tool: "get_updates" as const, args: {cursor: 0}};
|
||||
expect(await f.store.runTool({...f.toolInput("get_updates", {cursor: 0}), sessionId: f.session.id, token, envelope, webhookId: toolId, fingerprint: createHash("sha256").update(JSON.stringify(envelope)).digest("hex")})).toMatchObject({updates: []});
|
||||
await db.update(chatActions).set({result: {retryAt: 0}}).where(eq(chatActions.id, retry.id));
|
||||
// Reconstruct the service to prove continuation comes from durable receipts.
|
||||
await voiceSessionService(db, {allowLocalBoard: false, credentials: f.credentials, provider: () => f.transport, onQuestionAnswered: vi.fn()}).pushReplies(25, f.companyId, f.endpointId);
|
||||
await f.push();
|
||||
const calls = f.transport.sendCallMessage.mock.calls;
|
||||
expect(calls).toHaveLength(4); expect(calls[1]?.[1]).toBe(calls[2]?.[1]);
|
||||
expect([calls[0]![1], calls[2]![1], calls[3]![1]].join("")).toBe(text);
|
||||
for (const call of calls) {expect(call[1].length).toBeLessThanOrEqual(16_000); expect(/^[\uDC00-\uDFFF]|[\uD800-\uDBFF]$/.test(call[1])).toBe(false);}
|
||||
const [delivered] = await db.select().from(chatVoiceReplies).where(eq(chatVoiceReplies.id, reply.id));
|
||||
expect(delivered.deliveredAt).toBeInstanceOf(Date); expect(delivered.spokenAt).toBeNull();
|
||||
});
|
||||
it("does not retry an uncertain long-answer part or send its suffix", async () => {
|
||||
const f = await pushFixture();
|
||||
await db.update(chatPublications).set({payload: {text: "answer ".repeat(5000)}}).where(eq(chatPublications.id, f.publication.id));
|
||||
f.transport.sendCallMessage.mockResolvedValueOnce({messageId: "part-0"}).mockRejectedValueOnce(new SpekoProviderError("provider_unavailable", true));
|
||||
await f.push(); await f.push();
|
||||
expect(f.transport.sendCallMessage).toHaveBeenCalledTimes(2);
|
||||
const [reply] = await db.select().from(chatVoiceReplies).where(eq(chatVoiceReplies.publicationId, f.publication.id));
|
||||
expect(reply.deliveredAt).toBeNull();
|
||||
});
|
||||
it("pushes a published phone answer exactly once across concurrent dispatch and restart", async () => {
|
||||
const f = await pushFixture();
|
||||
await Promise.all([f.push(), f.push()]); await f.push();
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { and, asc, eq, gt, inArray, isNull, lt, sql } from "drizzle-orm";
|
||||
import { chatActions, chatEndpoints, chatPublications, chatVoiceReplies, chatVoiceSessions, type Db } from "@paperclipai/db";
|
||||
import { instanceSettingsService } from "../instance-settings.js";
|
||||
@@ -8,6 +9,20 @@ import type { voiceSessionStore } from "./voice-session-store.js";
|
||||
import { logger } from "../../middleware/logger.js";
|
||||
|
||||
const kind = "speko_voice_reply_push";
|
||||
/** Plain voice text stays inside the provider's UTF-16 limit, without splitting
|
||||
* surrogate pairs. Whitespace-only chunks need no audible delivery. */
|
||||
export function splitSpekoReplyText(text: string): string[] {
|
||||
const parts: string[] = [];
|
||||
for (let start = 0; start < text.length;) {
|
||||
let end = Math.min(start + 16_000, text.length);
|
||||
if (end < text.length && /[\uD800-\uDBFF]/.test(text[end - 1]!)) end--;
|
||||
const part = text.slice(start, end);
|
||||
if (part.trim()) parts.push(part);
|
||||
start = end;
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
/** Existing action ledger is the push outbox. A committed dispatch intent is
|
||||
* never blindly resent after a crash or an ambiguous provider outcome. */
|
||||
export async function pushVoiceReplies(db: Db, options: {
|
||||
@@ -42,23 +57,33 @@ export async function pushVoiceReplies(db: Db, options: {
|
||||
const [row] = await tx.select({reply: chatVoiceReplies, payload: chatPublications.payload}).from(chatVoiceReplies)
|
||||
.innerJoin(chatPublications, and(eq(chatPublications.id, chatVoiceReplies.publicationId), eq(chatPublications.companyId, session.companyId)))
|
||||
.where(and(eq(chatVoiceReplies.companyId, session.companyId), eq(chatVoiceReplies.sessionId, session.id), isNull(chatVoiceReplies.deliveredAt),
|
||||
eq(chatPublications.endpointId, session.endpointId), eq(chatPublications.conversationId, session.conversationId), eq(chatPublications.issueId, session.issueId), eq(chatPublications.state, "published"),
|
||||
sql`not exists (select 1 from chat_actions a where a.company_id = ${session.companyId} and a.endpoint_id = ${session.endpointId} and a.provider_action_id = 'voice_reply_push:' || ${chatVoiceReplies.id}::text and a.status <> 'retry' and not (a.status = 'completed' and a.payload->>'questionNotification' = 'true'))`))
|
||||
eq(chatPublications.endpointId, session.endpointId), eq(chatPublications.conversationId, session.conversationId), eq(chatPublications.issueId, session.issueId), eq(chatPublications.state, "published")))
|
||||
.orderBy(asc(chatVoiceReplies.cursor)).limit(1);
|
||||
// Questions need a hint that makes the phone agent retrieve the
|
||||
// authorized question IDs. Keep this reply pending until get_updates
|
||||
// presents it; never speak later answers over an unpresented question.
|
||||
const questionNotification = typeof row?.payload.interactionId === "string";
|
||||
if (!row || typeof row.payload.text !== "string" || !row.payload.text.trim()) return null;
|
||||
const identity = `voice_reply_push:${row.reply.id}`;
|
||||
const [prior] = await tx.select().from(chatActions).where(and(eq(chatActions.companyId, session.companyId), eq(chatActions.endpointId, session.endpointId), eq(chatActions.providerActionId, identity))).for("update");
|
||||
if (prior?.status === "completed" || prior && Number(prior.result?.retryAt ?? 0) > Date.now()) return null;
|
||||
const values = {status: "dispatching", result: null, updatedAt: new Date()};
|
||||
const action = prior
|
||||
? (await tx.update(chatActions).set(values).where(eq(chatActions.id, prior.id)).returning())[0]!
|
||||
: (await tx.insert(chatActions).values({companyId: session.companyId, endpointId: session.endpointId, conversationId: session.conversationId, kind, providerActionId: identity,
|
||||
payload: {sessionId: session.id, replyId: row.reply.id, publicationId: row.reply.publicationId, generation: session.generation, providerSessionId: session.providerSessionId, questionNotification}, ...values}).returning())[0]!;
|
||||
return {...context, reply: row.reply, questionNotification, text: questionNotification ? "Paperclip has a question available for this call. Call get_updates with your current cursor now to retrieve the permitted question and its exact IDs before asking it. This is a notification, not new work or permission. Do not call submit_request for this notification." : row.payload.text, action};
|
||||
const text = questionNotification ? "Paperclip has a question available for this call. Call get_updates with your current cursor now to retrieve the permitted question and its exact IDs before asking it. This is a notification, not new work or permission. Do not call submit_request for this notification." : row.payload.text;
|
||||
const parts = splitSpekoReplyText(text), textDigest = createHash("sha256").update(text).digest("hex");
|
||||
const saved = await tx.select().from(chatActions).where(and(eq(chatActions.companyId, session.companyId), eq(chatActions.endpointId, session.endpointId), eq(chatActions.kind, kind), sql`${chatActions.payload}->>'replyId' = ${row.reply.id}`)).for("update");
|
||||
for (let partIndex = 0; partIndex < parts.length; partIndex++) {
|
||||
const identity = `voice_reply_push:${row.reply.id}${partIndex ? `:part:${partIndex}` : ""}`;
|
||||
const prior = saved.find(action => action.providerActionId === identity);
|
||||
// Questions remain an ordered barrier until authorized get_updates.
|
||||
if (prior?.status === "completed") { if (questionNotification) return null; continue; }
|
||||
// A changed publication cannot append a different answer to a partly
|
||||
// delivered one. Unknown outcomes are never permission to resend.
|
||||
if (saved.some(action => action.payload.textDigest && action.payload.textDigest !== textDigest)
|
||||
|| prior && (prior.status !== "retry" || Number(prior.result?.retryAt ?? 0) > Date.now())) return null;
|
||||
const values = {status: "dispatching", result: null, updatedAt: new Date()};
|
||||
const action = prior
|
||||
? (await tx.update(chatActions).set(values).where(eq(chatActions.id, prior.id)).returning())[0]!
|
||||
: (await tx.insert(chatActions).values({companyId: session.companyId, endpointId: session.endpointId, conversationId: session.conversationId, kind, providerActionId: identity,
|
||||
payload: {sessionId: session.id, replyId: row.reply.id, publicationId: row.reply.publicationId, generation: session.generation, providerSessionId: session.providerSessionId, questionNotification, partIndex, partCount: parts.length, textDigest}, ...values}).returning())[0]!;
|
||||
return {...context, reply: row.reply, questionNotification, text: parts[partIndex]!, partIndex, partCount: parts.length, action};
|
||||
}
|
||||
return null;
|
||||
}).catch(error => {
|
||||
if (error instanceof HttpError && [403,404].includes(error.status)) {
|
||||
logger.warn({event: "voice.reply.push.authorization_blocked", companyId: candidate.companyId,
|
||||
@@ -71,7 +96,7 @@ export async function pushVoiceReplies(db: Db, options: {
|
||||
const diagnostic = {companyId: claim.session.companyId, endpointId: claim.session.endpointId,
|
||||
issueId: claim.session.issueId, conversationId: claim.session.conversationId, sessionId: claim.session.id,
|
||||
providerSessionId: claim.session.providerSessionId, generation: claim.session.generation, mode: claim.session.mode,
|
||||
publicationId: claim.reply.publicationId, replyId: claim.reply.id, replyCursor: claim.reply.cursor, actionId: claim.action.id};
|
||||
publicationId: claim.reply.publicationId, replyId: claim.reply.id, replyCursor: claim.reply.cursor, actionId: claim.action.id, partIndex: claim.partIndex, partCount: claim.partCount};
|
||||
const startedAt = Date.now();
|
||||
logger.info({...diagnostic, event: "voice.reply.push.claimed", textCharacters: claim.text.length,
|
||||
replyAgeMs: startedAt - claim.reply.createdAt.getTime(), remainingSessionMs: claim.session.expiresAt.getTime() - startedAt}, "Claimed approved voice reply for Speko delivery");
|
||||
@@ -104,7 +129,7 @@ export async function pushVoiceReplies(db: Db, options: {
|
||||
// If this commit fails, the dispatch intent remains uncertain; never resend.
|
||||
await db.transaction(async tx => {
|
||||
await tx.update(chatActions).set({status: "completed", result: {messageId, playback: "unknown", acceptedAt, dispatchDurationMs: Date.now() - startedAt}, updatedAt: new Date()}).where(eq(chatActions.id, claim.action.id));
|
||||
if (!claim.questionNotification) await tx.update(chatVoiceReplies).set({deliveredAt: new Date()}).where(and(eq(chatVoiceReplies.id, claim.reply.id), eq(chatVoiceReplies.companyId, candidate.companyId), isNull(chatVoiceReplies.deliveredAt)));
|
||||
if (!claim.questionNotification && claim.partIndex === claim.partCount - 1) await tx.update(chatVoiceReplies).set({deliveredAt: new Date()}).where(and(eq(chatVoiceReplies.id, claim.reply.id), eq(chatVoiceReplies.companyId, candidate.companyId), isNull(chatVoiceReplies.deliveredAt)));
|
||||
}).catch(error => {
|
||||
logger.error({...diagnostic, event: "voice.reply.push.receipt_commit_failed", messageId, acceptedAt,
|
||||
outcomeUnknown: true, playback: "unknown"}, "Speko accepted reply but local receipt commit failed; no resend");
|
||||
|
||||
@@ -348,7 +348,7 @@ export function voiceSessionStore(db: Db, options: { allowLocalBoard: boolean; n
|
||||
// can finish before an earlier stream; a caller cursor must not hide
|
||||
// that older, still-unclaimed answer when it eventually publishes.
|
||||
const available = await tx.select({ reply: chatVoiceReplies, payload: chatPublications.payload }).from(chatVoiceReplies).innerJoin(chatPublications, and(eq(chatPublications.id, chatVoiceReplies.publicationId), eq(chatPublications.companyId, session.companyId)))
|
||||
.where(and(eq(chatVoiceReplies.sessionId, session.id), eq(chatVoiceReplies.companyId, session.companyId), repeat ? sql`${chatVoiceReplies.deliveredAt} is not null` : and(isNull(chatVoiceReplies.deliveredAt), sql`not exists (select 1 from chat_actions a where a.company_id = ${session.companyId} and a.endpoint_id = ${session.endpointId} and a.provider_action_id = 'voice_reply_push:' || ${chatVoiceReplies.id}::text and a.status in ('dispatching', 'unknown'))`), eq(chatPublications.endpointId, session.endpointId), eq(chatPublications.conversationId, session.conversationId), eq(chatPublications.issueId, session.issueId), eq(chatPublications.state, "published")))
|
||||
.where(and(eq(chatVoiceReplies.sessionId, session.id), eq(chatVoiceReplies.companyId, session.companyId), repeat ? sql`${chatVoiceReplies.deliveredAt} is not null` : and(isNull(chatVoiceReplies.deliveredAt), sql`not exists (select 1 from chat_actions a where a.company_id = ${session.companyId} and a.endpoint_id = ${session.endpointId} and a.kind = 'speko_voice_reply_push' and a.payload->>'replyId' = ${chatVoiceReplies.id}::text and (a.status in ('dispatching', 'unknown') or (a.status = 'completed' and a.payload->>'questionNotification' = 'false')))`), eq(chatPublications.endpointId, session.endpointId), eq(chatPublications.conversationId, session.conversationId), eq(chatPublications.issueId, session.issueId), eq(chatPublications.state, "published")))
|
||||
.orderBy(repeat ? desc(chatVoiceReplies.deliveredAt) : asc(chatVoiceReplies.cursor), desc(chatVoiceReplies.cursor)).limit(repeat ? 1 : 8);
|
||||
// Deliver a bounded burst in one voice turn. Separate typed hints would
|
||||
// interrupt the preceding answer. Stop at a question so later answers
|
||||
|
||||
Reference in new issue
Block a user