-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-usage.ts
More file actions
88 lines (75 loc) · 2.22 KB
/
example-usage.ts
File metadata and controls
88 lines (75 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Example usage of @crewdle/ai-firebase-functions
*
* This example shows how to use the package in your Firebase Functions project
*/
import { onCall } from 'firebase-functions/v2/https';
// Example index.ts for your Firebase Functions project
import {
runWorkflow,
streamWorkflow,
// Import types for better TypeScript support
type WorkflowMessage,
type WorkflowFile,
type WorkflowRequest
} from '@crewdle/ai-firebase-functions';
// Export all functions for deployment
export {
runWorkflow,
streamWorkflow
};
// Example: Custom wrapper function for batch processing
export const processBatchWorkflow = onCall({
region: "northamerica-northeast1",
timeoutSeconds: 540, // 9 minutes
}, async (request) => {
const { workflowId, batchMessages } = request.data;
try {
const results = [];
// Process multiple message sets with the same workflow
for (const messageSet of batchMessages) {
const result = await runWorkflow({
workflowId,
messages: messageSet.messages,
files: messageSet.files || []
});
results.push({
id: messageSet.id,
result: result.choices[0].message.content
});
}
return {
results,
totalProcessed: results.length,
success: true
};
} catch (error) {
console.error('Batch processing failed:', error);
throw new Error(`Failed to process batch: ${error.message}`);
}
});
// Example: Streaming workflow with custom handling
export const streamWorkflowWithLogging = onCall({
region: "northamerica-northeast1",
timeoutSeconds: 300,
}, async (request, response) => {
const { workflowId, messages, files, userId } = request.data;
try {
console.log(`Starting stream workflow for user: ${userId}`);
const streamResult = await streamWorkflow({
workflowId,
messages,
files: files || []
});
console.log(`Stream workflow completed for user: ${userId}`);
return {
streamOutput: streamResult,
userId,
timestamp: new Date().toISOString(),
success: true
};
} catch (error) {
console.error(`Stream workflow failed for user ${userId}:`, error);
throw new Error(`Failed to stream workflow: ${error.message}`);
}
});