Skip to content

Commit

Permalink
feat: add functions apis
Browse files Browse the repository at this point in the history
  • Loading branch information
nzambello committed May 3, 2024
1 parent 07a4573 commit 9a9a8ba
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import expertReferences from './engine/expertReferences';
import knownFacts from './engine/knownFacts';
import user from './engine/user';
import topics from './engine/topics';
import functions from './engine/functions';

export default (apiUrl: string) => ({
correlationPairs: correlationPairs(apiUrl),
Expand Down Expand Up @@ -57,4 +58,6 @@ export default (apiUrl: string) => ({
...user(apiUrl),
topics: topics(apiUrl),
...topics(apiUrl),
functions: functions(apiUrl),
...functions(apiUrl),
});
23 changes: 23 additions & 0 deletions src/engine/functions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import memori from '../index';

const client = memori('https://backend.memori.ai');

describe('engine/functions api', () => {
it('works on functions apis', async () => {
expect(
await client.functions.getFunction(
'768b9654-e781-4c3c-81fa-ae1529d1bfbe',
'768b9654-e781-4c3c-81fa-ae1529d1bfbe'
)
).not.toBeNull();
});

it('works on functions apis with shorthand version', async () => {
expect(
await client.getFunction(
'768b9654-e781-4c3c-81fa-ae1529d1bfbe',
'768b9654-e781-4c3c-81fa-ae1529d1bfbe'
)
).not.toBeNull();
});
});
116 changes: 116 additions & 0 deletions src/engine/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import type { ResponseSpec, Function } from '../types';
import { apiFetcher } from '../apiFetcher';

/*********************
* *
* Functions *
* *
*********************/

export default (apiUrl: string) => ({
/**
* Lists all Functions objects.
* @param {string} sessionId The session ID
*/
getAllFunctions: async (sessionId: string) =>
apiFetcher(`/Functions/${sessionId}`, {
method: 'GET',
apiUrl,
}) as Promise<
ResponseSpec & {
/**
* Total number of Function objects.
*/
count: number;
/**
* List of Function objects. May be empty.
*/
functions: Function[];
}
>,

/**
* Lists Function objects with pagination.
* @param {string} sessionId The session ID
* @param {number} from The 0-based index of the first Function object to list
* @param {number} howMany The number of the Function objects to list
*/
getFunctionsPaginated: async (
sessionId: string,
from: number,
howMany: number
) =>
apiFetcher(`/Functions/${sessionId}/${from}/${howMany}`, {
method: 'GET',
apiUrl,
}) as Promise<
ResponseSpec & {
/**
* Total number of Function objects.
*/
count: number;
/**
* List of Function objects. May be empty.
*/
functions: Function[];
}
>,

/**
* Gets the details of a Function object.
* @param {string} sessionId The session ID
* @param {string} functionID The Function object ID
*/
getFunction: async (sessionId: string, functionID: string) =>
apiFetcher(`/Function/${sessionId}/${functionID}`, {
method: 'GET',
apiUrl,
}) as Promise<
ResponseSpec & {
function: Function;
}
>,

/**
* Updates an existing Function object.
* Only non-null (present) fields are considered for update. Null or absent fields are left unaltered.
* @param {string} sessionId The session ID
* @param {Function} func The Function object
*/
patchFunction: async (
sessionId: string,
func: Partial<Function> & { functionID: string }
) =>
apiFetcher(`/Function/${sessionId}/${func.functionID}`, {
method: 'PATCH',
apiUrl,
body: func,
}) as Promise<ResponseSpec>,

/**
* Removes an existing Function object.
* @param {string} sessionId The session ID
* @param {string} functionID The Function object ID
*/
deleteFunction: async (sessionId: string, functionID: string) =>
apiFetcher(`/Function/${sessionId}/${functionID}`, {
method: 'DELETE',
apiUrl,
}) as Promise<ResponseSpec>,

/**
* Creates a new Function object.
* @param {string} sessionId The session ID
* @param {Function} func The Function object
*/
createFunction: async (sessionId: string, func: Function) =>
apiFetcher(`/Function/${sessionId}`, {
method: 'POST',
apiUrl,
body: func,
}) as Promise<
ResponseSpec & {
functionID: string;
}
>,
});
86 changes: 86 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,92 @@ export type IntentSlot = {
lastChangeSessionID?: string;
};

export type FunctionParameter = {
/**
* Function Parameter object ID.
*/
parameterID?: string;
/**
* Name of the Function Parameter object.
*/
name: string;
/**
* Description of the Function Parameter object. The description is provided to the generative AI to help it understand the purpose of the parameter.
*/
description: string;
/**
* Parameter type, e.g. "string", "number", "integer", "boolean".
* Currently supported types are:
* - string: string value
* - number: floating point value
* - integer: integer value
* - boolean: boolean value
*/
parameterType: 'string' | 'number' | 'integer' | 'boolean';
/**
* Optional list of possible values of the parameter. If empty or null, the parameter accepts any value of the specified type.
*/
possibleValues?: string[];
/**
* Whether the parameter is required or not. Default is not required.
*/
required?: boolean;
};

export type Function = {
/**
* Function object ID.
*/
functionID?: string;
/**
* Function type, e.g. Internal or WebHook.
* Internal functions are a limited subset implemented internally,
* while WebHook intents perform an external HTTP call to the specified web hook, using the specified templates.
*/
functionType: 'Internal' | 'WebHook';
/**
* Name of the Function object.
*/
name: string;
/**
* Description of the Function object. The description is provided to the generative AI to help it understand the purpose of the function.
*/
description: string;
/**
* List of its Function Parameter objects. May be empty.
*/
parameters?: FunctionParameter[];
/**
* HTTP URL of the web hook to be called when the function is invoked, up to the path part.
* The query string part is specified separately.
* It may include the value of function parameters using the syntax {parameter}, where "parameter" is the parameter name.
* E.g.: http://example.com/{param}/something.
*/
webHook: string;
/**
* HTTP method to be used when calling the web hook, e.g. "GET", "POST" etc.
*/
httpMethod?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
/**
* Optional set of HTTP headers to be passed to the web hook.
*/
httpHeaders?: { [key: string]: string };
/**
* Template of the HTTP query string to be passed to the web hook.
* It may include the value of function parameters using the syntax {parameter}, where "parameter" is the parameter name.
* E.g.: param1={param1}&param2={param2}.
*/
httpQueryStringTemplate?: string;
/**
* Template of the HTTP request body to be passed to the web hook.
* It may be in any format (JSON, XML etc.), and may include the value of function parameters using the syntax {parameter},
* where "parameter" is the parameter name.
* E.g.: { "param1": "{param1}", "param2": "{param2}" },
* or: <request><param1>{param1}</param1><param2>{param2}</param2></request>.
*/
httpBodyTemplate?: string;
};

export type CustomWord = {
customWordID: string;
word: string;
Expand Down

0 comments on commit 9a9a8ba

Please sign in to comment.