Skip to content

Commit fe7c99f

Browse files
vfssantosVinicius Santos
authored and
Vinicius Santos
committed
stable version
1 parent f00fdb8 commit fe7c99f

File tree

51 files changed

+1036
-1278
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1036
-1278
lines changed

.gitignore

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ coverage
1111
dist
1212

1313
# Deno logs
14-
logs
14+
logs/
1515

1616
# .env file
1717
.env
@@ -21,7 +21,7 @@ data
2121
cache
2222

2323
# Deno lock file
24-
lock.json
24+
deno-lock.json
2525

2626
# Deno dependencies
2727
deps
@@ -33,4 +33,10 @@ deps
3333
test-migrations.js
3434

3535
# passwords
36-
passwords.txt
36+
passwords.txt
37+
38+
# Cursor rules
39+
.cursorrules
40+
41+
# test-data
42+
test-data

deno.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"tasks": {
3-
"start": "DENO_DIR=./data/local/cache/.deno ENV=production deno run -A --importmap=deno.json --reload=http://localhost --no-lock --unstable-sloppy-imports --no-prompt --unstable https://raw.githubusercontent.com/AxionCompany/functions/develop/main.ts",
4-
"dev": "DENO_DIR=./data/local/cache/.deno ENV=development WATCH=true deno run --importmap=deno.json --reload=http://localhost,https://raw.githubusercontent.com/AxionCompany/functions/develop -A --no-lock --unstable-sloppy-imports --no-prompt --unstable https://raw.githubusercontent.com/AxionCompany/functions/develop/main.ts",
5-
"dev:local": "DENO_DIR=./data/local/cache/.deno ENV=development WATCH=true deno run --importmap=deno.json --reload=http://localhost -A --no-lock --unstable-sloppy-imports --no-prompt --unstable http://localhost:9000"
3+
"start": "DENO_DIR=./data/local/cache/.deno ENV=production deno run -A --importmap=deno.json --reload=http://http://local@localhost:8000 --no-lock --unstable-sloppy-imports --no-prompt --unstable https://raw.githubusercontent.com/AxionCompany/functions/develop/main.ts",
4+
"dev": "DENO_DIR=./data/local/cache/.deno ENV=development WATCH=true deno run --importmap=deno.json --reload=http://local@localhost:8000,https://raw.githubusercontent.com/AxionCompany/functions/release -A --no-lock --unstable-sloppy-imports --no-prompt --unstable https://raw.githubusercontent.com/AxionCompany/functions/release/main.ts",
5+
"dev:local": "DENO_DIR=./data/local/cache/.deno ENV=development WATCH=true deno run --importmap=deno.json --reload=http://local@localhost:9000 -A --no-lock --unstable-sloppy-imports --no-prompt --unstable http://localhost:9000"
66
},
77
"imports": {
88
"axion-components/": "http://AxionCompany--axions-web--main:{{GIT_API_KEY}}@localhost:9000/src/components/",

deno.lock

-586
This file was deleted.

load-test.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Function to make one round of parallel requests
2+
async function makeRequests(round) {
3+
const url = "http://localhost:8001/api/agents/functionCall";
4+
const data = {
5+
copilotzId: 1,
6+
input: "sim, por favor",
7+
user: {
8+
id: "user123",
9+
name: "John Doe"
10+
},
11+
thread: {
12+
extId: "thread456"
13+
}
14+
};
15+
16+
const headers = {
17+
"Content-Type": "application/json"
18+
};
19+
20+
// Array of parallel fetch requests
21+
const requests = Array.from({ length: 5 }, () =>
22+
fetch(url, {
23+
method: "POST",
24+
headers,
25+
body: JSON.stringify(data)
26+
})
27+
);
28+
29+
// Execute all requests in parallel
30+
console.log(`Starting round ${round}`);
31+
const responses = await Promise.all(requests);
32+
33+
// Log response status for each request
34+
responses.forEach((response, i) => {
35+
console.log(`Request ${i + 1} in round ${round}: Status ${response.status}`);
36+
});
37+
}
38+
39+
// Function to make Y rounds with X seconds interval
40+
async function makeRounds(x, y) {
41+
for (let i = 0; i < y; i++) {
42+
await makeRequests(i + 1);
43+
if (i < y - 1) {
44+
console.log(`Waiting for ${x} seconds before next round...`);
45+
await new Promise(resolve => setTimeout(resolve, x * 1000)); // Wait for X seconds
46+
}
47+
}
48+
}
49+
50+
// Usage: X = interval in seconds, Y = number of rounds
51+
makeRounds(3, 2);

src/api/_middlewares/getConfig.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const getConfig = async (req) => {
2+
3+
const { models, resources } = getConfig;
4+
5+
const { copilotz } = resources;
6+
7+
// Get the Copilotz's configuration
8+
const configsArr = await models.configs.find({ owner: copilotz._id, type: 'copilotz' });
9+
10+
// Convert the configuration array to an object
11+
const config = configsArr.reduce((obj, c) => {
12+
obj[c.name] = c.value;
13+
return obj;
14+
}, {});
15+
16+
getConfig.resources.config = config;
17+
18+
return req;
19+
}
20+
21+
export default getConfig;

src/api/_middlewares/getCopilotz.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
const getCopilotz = async (req) => {
3+
4+
const { extId, copilotzId, channel } = req.params;
5+
const { models } = getCopilotz;
6+
7+
let copilotzDoc;
8+
9+
if (copilotzId || extId) {
10+
11+
if (copilotzId) {
12+
// Firts, try to find the Copilotz by its id
13+
copilotzDoc = await models.copilotz.findOne({
14+
_id: copilotzId,
15+
});
16+
}
17+
// If not found, try to find the Copilotz by its external id
18+
else if (extId) {
19+
const mapper = await models.mappings.findOne({
20+
extId,
21+
resource: 'copilotz',
22+
type: channel,
23+
}, { populate: ['copilotz'] });
24+
25+
copilotzDoc = mapper.copilotz
26+
};
27+
28+
// If no Copilotz found, return an error
29+
if (!copilotzDoc) {
30+
throw { status: 404, message: 'Copilotz not found' };
31+
}
32+
33+
const actions = await Promise.all(copilotzDoc?.actions?.map(async (action) => {
34+
return await models.actions.findOne({ _id: action });
35+
}) || []);
36+
37+
copilotzDoc.actions = actions;
38+
39+
getCopilotz.resources.copilotz = copilotzDoc;
40+
41+
return req;
42+
}
43+
}
44+
45+
export default getCopilotz;

src/api/_middlewares/getJob.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
const getJob = async (req) => {
3+
const { resources, models } = getJob;
4+
5+
const { copilotz } = resources;
6+
7+
if (!copilotz.job) return;
8+
9+
const job = await models.jobs.findOne({ _id: copilotz.job }, { populate: ['actions'] });
10+
11+
12+
const workflow = await models.workflows.findOne({ _id: job.defaultWorkflow }, { populate: ['steps'] });
13+
14+
job.defaultWorkflow = workflow;
15+
16+
copilotz.job = job;
17+
18+
getJob.resources.copilotz = copilotz;
19+
20+
return req;
21+
}
22+
23+
export default getJob;
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const getSubscription = async (req) => {
2+
3+
const { models, payments } = getSubscription;
4+
5+
const { resources } = getSubscription;
6+
7+
const { user, copilotz } = resources;
8+
9+
const subscriptionDoc = await models.subscriptions.findOne(
10+
{ user: user?._id, copilotz: copilotz?._id }
11+
);
12+
13+
if (!subscriptionDoc || !payments[subscriptionDoc?.paymentProvider]) return;
14+
15+
const subscriptionData = await payments[subscriptionDoc.paymentProvider].subscriptions.get(
16+
{ subscriptionId: subscriptionDoc.subscriptionId }
17+
);
18+
19+
getSubscription.resources.subscription = subscriptionData;
20+
21+
return req;
22+
}
23+
24+
export default getSubscription;

src/api/_middlewares/getUser.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const getUsers = async (req) => {
2+
3+
const { models } = getUsers;
4+
5+
if (!req?.params.user.name) return;
6+
7+
let usersPhonePromise, usersEmailPromise;
8+
if (req.params?.user?.phone) {
9+
usersPhonePromise = models.users.findOne({ phone: req.params?.user?.phone });
10+
}
11+
if (req.params?.user?.email) {
12+
usersEmailPromise = models.users.findOne({ email: req.params?.user?.email });
13+
}
14+
15+
const usersResolved = await Promise.all([usersPhonePromise, usersEmailPromise]);
16+
let user = usersResolved[0] || usersResolved[1];
17+
18+
if (!user) {
19+
20+
user = await models.users.create({
21+
phone: req.params?.user?.phone,
22+
email: req.params?.user?.email,
23+
name: req.params?.user?.name || 'Guest',
24+
context: req.params?.user?.context || {},
25+
});
26+
27+
}
28+
29+
getUsers.resources.user = user;
30+
31+
return req;
32+
33+
}
34+
35+
export default getUsers;

src/api/_middlewares/main.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export { default as getUser } from './getUser.js';
2+
export { default as getCopilotz } from './getCopilotz.js';
3+
export { default as getConfig } from './getConfig.js';
4+
export { default as getJob } from './getJob.js';
5+
export { default as getSubscription } from './getSubscription.js';
6+
7+
File renamed without changes.

src/api/actions/specParser/openapi3-yaml/main.js src/api/_specParser/openapi3-yaml/main.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,15 @@ const ParseOpenApiSpec = ({ spec, module: request }) => {
6363
// Combine all the parts
6464
const spec = `(${summary}):${formattedArgs}->${responses["200"].description}`;
6565

66-
const request = ({ _user, ...params }) => (
66+
actions[operationId] = ({ _user, ...params }) => request(
6767
new URL(path, baseUrl).href,
6868
{
6969
method,
7070
headers: request?.globals?.headers,
71-
data: { ...validate(schema.body, { ...request?.globals?.body, ...params }), _user },
71+
body: { ...validate(schema.body, { ...request?.globals?.body, ...params }), _user },
7272
query: validate(schema.query, { ...request?.globals?.query, ...params }),
7373
}
7474
);
75-
76-
actions[operationId] = request;
7775
actions[operationId].spec = spec;
7876
}
7977
}

src/api/actions/main.js

-2
This file was deleted.

0 commit comments

Comments
 (0)