Skip to content

Commit

Permalink
feat: add completion step
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminshafii committed Jan 6, 2025
1 parent 07c6c54 commit 62387c7
Show file tree
Hide file tree
Showing 5 changed files with 730 additions and 517 deletions.
65 changes: 45 additions & 20 deletions pipes/auto-pay/src/agents/payment-detector-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ import { useAgentStepsStore } from '@/stores/agent-steps-store';
import { toast } from '@/components/ui/use-toast';
import { useCallback, useState, useRef, useEffect } from 'react';
import { z } from 'zod';
import type { PaymentInfo } from '@/types/wise';

// Zod schemas
const bankDetailsSchema = z.object({
accountNumber: z.string().optional().nullable(),
routingNumber: z.string().optional().nullable(),
iban: z.string().optional().nullable(),
accountNumber: z.string().optional(),
routingNumber: z.string().optional(),
iban: z.string().optional(),
}).describe('Bank account details for the payment');

const paymentDetailsSchema = z.object({
amount: z.string().optional(),
currency: z.string().optional(),
recipient: z.string().optional().nullable(),
amount: z.string(),
currency: z.string(),
recipient: z.string(),
dueDate: z.string().optional(),
bankDetails: bankDetailsSchema.optional(),
reference: z.string().optional(),
Expand Down Expand Up @@ -51,6 +52,7 @@ export interface DetectedPayment {
window: string;
};
details: PaymentDetails;
paymentInfo: PaymentInfo;
}

export interface PaymentDetectionResult {
Expand Down Expand Up @@ -87,6 +89,17 @@ const paymentAnswer = {
parameters: paymentAnswerSchema,
};

function paymentDetailsToPaymentInfo(details: PaymentDetails): PaymentInfo {
return {
amount: details.amount || '0',
currency: details.currency || 'USD',
recipientName: details.recipient || 'Unknown Recipient',
accountNumber: details.bankDetails?.accountNumber || '',
routingNumber: details.bankDetails?.routingNumber || '',
reference: details.reference,
};
}

export async function runPaymentDetector(
recognizedItemId: string,
onProgress?: (message: string) => void,
Expand Down Expand Up @@ -124,23 +137,33 @@ export async function runPaymentDetector(
- Payment amounts and currencies
- Payment deadlines or due dates
Stop as soon as you found 1
Stop as soon as you found 1 payment.
Follow these steps:
1. Start with broad searches for payment-related terms:
make sure queries are single elements that are will be matched as if they were between double quotes
- Use terms like "invoice", "payment", "transfer", "IBAN", "due", "amount"
- Make sure queries are single elements that will be matched exactly
2. When you find something, do focused searches to gather context:
- General informatino that can be used to identify the payment
4. Once you have gathered all information:
- Look for specific amounts and currencies
- Search for recipient information
- Find any payment references or notes
- Check for due dates or deadlines
3. For each potential payment:
- Extract a clear summary
- Note the source context
- Calculate confidence based on completeness
- Explain your confidence reasoning
4. Once you have gathered all information:
- Call paymentAnswer with the structured payment data
- Include all found details (amount, recipient, due date, etc.)
- Provide clear summaries and confidence scores
- Explain your confidence reasoning
BE THOROUGH BUT EFFICIENT
FOCUS ON ACCURACY OVER SPEED
`,
prompt: `
Search through recent screen activity to find any payments that need to be made.
Expand All @@ -162,28 +185,29 @@ export async function runPaymentDetector(
const stepId = crypto.randomUUID();
const humanAction = getHumanActionFromToolCall(toolCall);

// Add the step with the action
// Add the step with all information
addStep(recognizedItemId, {
text,
toolCalls: [toolCall],
toolResults: toolResults ? [toolResults[index]] : undefined,
finishReason,
usage,
humanAction,
tokenCount: usage?.totalTokens || 0,
});

// If we have results, update with human result
if (toolResults?.[index]) {
const humanResult = getHumanResultFromToolCall(toolCall, toolResults[index]);
updateStepResult(recognizedItemId, stepId, humanResult);
}
});

// Notify progress
if (toolCalls?.length && onProgress) {
const toolNames = toolCalls.map(t => 'toolName' in t ? t.toolName : 'unknown').join(', ');
onProgress(`Using tools: ${toolNames}`);
}
// Notify progress
if (onProgress) {
const toolName = 'toolName' in toolCall ? toolCall.toolName : 'unknown';
onProgress(`Using tool: ${toolName}`);
}
});
},
});

Expand All @@ -208,7 +232,8 @@ export async function runPaymentDetector(
app: '',
window: '',
},
details: payment.details
details: payment.details,
paymentInfo: paymentDetailsToPaymentInfo(payment.details)
}));

// Sort by confidence (most confident first)
Expand Down
45 changes: 38 additions & 7 deletions pipes/auto-pay/src/agents/payment-preparer-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useAgentStepsStore } from '@/stores/agent-steps-store';
import { toast } from '@/components/ui/use-toast';
import { useCallback, useState, useRef, useEffect } from 'react';
import { z } from 'zod';
import type { PaymentInfo } from '@/types/wise';

// Zod schemas for Wise transfer data
const transferDetailsSchema = z.object({
Expand Down Expand Up @@ -125,6 +126,18 @@ export async function runPaymentPreparer(
- Validate bank details where possible
- Include clear references
4. Calculate confidence based on:
- Completeness of required fields
- Clarity of the information
- Validation of bank details
- Consistency across sources
5. Provide detailed explanation of:
- Why certain details were chosen
- What might be missing
- Any assumptions made
- Validation results
BE THOROUGH BUT EFFICIENT
FOCUS ON ACCURACY OVER SPEED
`,
Expand All @@ -148,28 +161,29 @@ export async function runPaymentPreparer(
const stepId = crypto.randomUUID();
const humanAction = getHumanActionFromToolCall(toolCall);

// Add the step with the action
// Add the step with all information
addStep(recognizedItemId, {
text,
toolCalls: [toolCall],
toolResults: toolResults ? [toolResults[index]] : undefined,
finishReason,
usage,
humanAction,
tokenCount: usage?.totalTokens || 0,
});

// If we have results, update with human result
if (toolResults?.[index]) {
const humanResult = getHumanResultFromToolCall(toolCall, toolResults[index]);
updateStepResult(recognizedItemId, stepId, humanResult);
}
});

// Notify progress
if (toolCalls?.length && onProgress) {
const toolNames = toolCalls.map(t => 'toolName' in t ? t.toolName : 'unknown').join(', ');
onProgress(`Using tools: ${toolNames}`);
}
// Notify progress
if (onProgress) {
const toolName = 'toolName' in toolCall ? toolCall.toolName : 'unknown';
onProgress(`Using tool: ${toolName}`);
}
});
},
});

Expand Down Expand Up @@ -317,4 +331,21 @@ export function usePaymentPreparer(recognizedItemId: string) {
isProcessing,
abort,
};
}

function transferDetailsToPaymentInfo(details: TransferDetails): PaymentInfo {
// Ensure we have non-null values for required fields
if (!details.amount || !details.currency || !details.targetAccount.accountHolderName) {
throw new Error('Missing required transfer details');
}

return {
amount: details.amount,
currency: details.currency,
recipientName: details.targetAccount.accountHolderName,
accountNumber: details.targetAccount.accountNumber || '',
routingNumber: details.targetAccount.routingNumber || '',
reference: details.reference || undefined,
recipientEmail: undefined, // We don't have this in transfer details
};
}
Loading

0 comments on commit 62387c7

Please sign in to comment.