Milestone 0: import zappier billing, Verae middleware, and Zapier research
Compose-ready workspace: packages/zappier (rate card, portal, Stripe), packages/verae-zapier-middleware (timestamp + NATS), packages/verae-zapier (CLI app), vendor/zapier-platform, and research/zapier vendor corpus. Gate 0 structure checks pass. Product code and research are not yet wired.
This commit is contained in:
commit
b4150c8250
1364 changed files with 6814366 additions and 0 deletions
62
vendor/zapier-platform/example-apps/openai/.gitignore
vendored
Normal file
62
vendor/zapier-platform/example-apps/openai/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
build/
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# Typescript v1 declaration files
|
||||
typings/
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
.eslintcache
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
.yarn-integrity
|
||||
|
||||
# environment variables file
|
||||
.env
|
||||
.environment
|
||||
|
||||
# next.js build output
|
||||
.next
|
||||
24
vendor/zapier-platform/example-apps/openai/README.md
vendored
Normal file
24
vendor/zapier-platform/example-apps/openai/README.md
vendored
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# OpenAI
|
||||
|
||||
This Zapier integration project is generated by the `zapier-platform init` CLI command. This integration in particular is using the OpenAI API to generate responses to prompts from users. For this integration, there is a `constants.js` file that will allow you to swap out the base URL and version of the API to swap if you are using an OpenAI compatible API to get started.
|
||||
|
||||
These are what you normally do next:
|
||||
|
||||
```bash
|
||||
# Install dependencies
|
||||
npm install # or you can use yarn
|
||||
|
||||
# Run tests
|
||||
zapier-platform test
|
||||
|
||||
# Register the integration on Zapier if you haven't
|
||||
zapier-platform register "App Title"
|
||||
|
||||
# Or you can link to an existing integration on Zapier
|
||||
zapier-platform link
|
||||
|
||||
# Push it to Zapier
|
||||
zapier-platform push
|
||||
```
|
||||
|
||||
Find out more on the latest docs: https://github.com/zapier/zapier-platform/blob/main/packages/cli/README.md.
|
||||
46
vendor/zapier-platform/example-apps/openai/authentication.js
vendored
Normal file
46
vendor/zapier-platform/example-apps/openai/authentication.js
vendored
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
const { API_URL } = require('./constants');
|
||||
// You want to make a request to an endpoint that is either specifically designed
|
||||
// to test auth, or one that every user will have access to. eg: `/me`.
|
||||
// By returning the entire request object, you have access to the request and
|
||||
// response data for testing purposes. Your connection label can access any data
|
||||
// from the returned response using the `json.` prefix. eg: `{{json.username}}`.
|
||||
const test = (z, bundle) => z.request({ url: `${API_URL}/me` });
|
||||
|
||||
module.exports = {
|
||||
// "custom" is the catch-all auth type. The user supplies some info and Zapier can
|
||||
// make authenticated requests with it
|
||||
type: 'custom',
|
||||
|
||||
// Define any input app's auth requires here. The user will be prompted to enter
|
||||
// this info when they connect their account.
|
||||
fields: [
|
||||
{
|
||||
key: 'api_key',
|
||||
label: 'API Key',
|
||||
required: true,
|
||||
helpText:
|
||||
'Generate an API Key in your [Platform settings page](https://platform.openai.com/api-keys).',
|
||||
},
|
||||
// This field is optional and can be removed if not needed
|
||||
{
|
||||
key: 'organization_id',
|
||||
required: false,
|
||||
label: 'Organization ID',
|
||||
helpText:
|
||||
'**Optional** Only required if your OpenAI account belongs to multiple organizations. If not using OpenAI, this field will be disregarded. If your OpenAI account belongs to multiple organizations, optionally add the [Organization ID](https://platform.openai.com/account/org-settings) that this connection should use. If left blank, your [default organization](https://platform.openai.com/account/api-keys) will be used.',
|
||||
},
|
||||
],
|
||||
|
||||
// The test method allows Zapier to verify that the credentials a user provides
|
||||
// are valid. We'll execute this method whenever a user connects their account for
|
||||
// the first time.
|
||||
test,
|
||||
|
||||
// This template string can access all the data returned from the auth test. If
|
||||
// you return the test object, you'll access the returned data with a label like
|
||||
// `{{json.X}}`. If you return `response.data` from your test, then your label can
|
||||
// be `{{X}}`. This can also be a function that returns a label. That function has
|
||||
// the standard args `(z, bundle)` and data returned from the test can be accessed
|
||||
// in `bundle.inputData.X`.
|
||||
connectionLabel: '{{json.email}}',
|
||||
};
|
||||
10
vendor/zapier-platform/example-apps/openai/constants.js
vendored
Normal file
10
vendor/zapier-platform/example-apps/openai/constants.js
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
const BASE_URL = 'https://api.openai.com';
|
||||
const VERSION = 'v1';
|
||||
const API_URL = `${BASE_URL}/${VERSION}`;
|
||||
|
||||
const DEFAULT_MODEL = 'gpt-4o-mini';
|
||||
|
||||
module.exports = {
|
||||
API_URL,
|
||||
DEFAULT_MODEL,
|
||||
};
|
||||
164
vendor/zapier-platform/example-apps/openai/creates/chat_completion.js
vendored
Normal file
164
vendor/zapier-platform/example-apps/openai/creates/chat_completion.js
vendored
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
/* eslint-disable camelcase */
|
||||
const { API_URL, DEFAULT_MODEL } = require('../constants');
|
||||
|
||||
const sample = require('../samples/chat.json');
|
||||
|
||||
async function getAdvancedFields(_z, bundle) {
|
||||
if (bundle.inputData.show_advanced === true) {
|
||||
return [
|
||||
{
|
||||
key: 'info_advanced',
|
||||
type: 'copy',
|
||||
helpText:
|
||||
"The following fields are for advanced users and should be used with caution as they may affect performance. In most cases, the default options are sufficient. If you'd like to explore these options further, you can [learn more here](https://help.zapier.com/hc/en-us/articles/22497191078797).",
|
||||
},
|
||||
{
|
||||
key: 'developer_message',
|
||||
label: 'Developer/System Message',
|
||||
type: 'text',
|
||||
helpText:
|
||||
'Instructions to the model that are prioritized ahead of user messages, following [chain of command](https://cdn.openai.com/spec/model-spec-2024-05-08.html#follow-the-chain-of-command).',
|
||||
},
|
||||
{
|
||||
key: 'temperature',
|
||||
label: 'Temperature',
|
||||
type: 'number',
|
||||
helpText:
|
||||
'Higher values mean the model will take more risks. Try 0.9 for more creative applications, and 0 for ones with a well-defined answer.\n\nUse a decimal between 0 and 1.',
|
||||
},
|
||||
{
|
||||
key: 'max_completion_tokens',
|
||||
label: 'Maximum Length',
|
||||
type: 'integer',
|
||||
helpText: 'The maximum number of tokens for the completion.',
|
||||
},
|
||||
];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
async function perform(z, bundle) {
|
||||
const {
|
||||
user_message,
|
||||
model,
|
||||
files,
|
||||
developer_message,
|
||||
temperature,
|
||||
max_completion_tokens,
|
||||
} = bundle.inputData;
|
||||
|
||||
const developerMessage = {
|
||||
role: 'developer',
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: developer_message || 'You are a helpful assistant.',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const userMessage = {
|
||||
role: 'user',
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: user_message,
|
||||
},
|
||||
...(files
|
||||
? files.map((file) => ({
|
||||
type: 'image_url',
|
||||
image_url: {
|
||||
url: file,
|
||||
},
|
||||
}))
|
||||
: []),
|
||||
],
|
||||
};
|
||||
|
||||
const messages = [developerMessage, userMessage];
|
||||
|
||||
const response = await z.request({
|
||||
url: `${API_URL}/chat/completions`,
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
model,
|
||||
messages,
|
||||
temperature,
|
||||
max_completion_tokens,
|
||||
}),
|
||||
});
|
||||
return response.data;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
key: 'chat_completion',
|
||||
noun: 'Chat',
|
||||
display: {
|
||||
label: 'Chat Completion',
|
||||
description: 'Sends a Chat to OpenAI and generates a Completion.',
|
||||
},
|
||||
operation: {
|
||||
perform,
|
||||
inputFields: [
|
||||
{
|
||||
key: 'info_data_usage',
|
||||
type: 'copy',
|
||||
helpText:
|
||||
"Data sent to OpenAI through this Zap is via an API. Under OpenAI's [API data usage policy](https://openai.com/policies/api-data-usage-policies), OpenAI will not use API-submitted data to train or improve their models unless you explicitly decide to share your data with them for that purpose (such as by opting in). For more information, please review OpenAI's article about [when/how data may be used to improve model performance](https://help.openai.com/en/articles/5722486-how-your-data-is-used-to-improve-model-performance).",
|
||||
},
|
||||
{
|
||||
key: 'user_message',
|
||||
label: 'User Message',
|
||||
type: 'text',
|
||||
helpText:
|
||||
"Instructions that request some output from the model. Similar to messages you'd type in [ChatGPT](https://chatgpt.com) as an end user.",
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
key: 'files',
|
||||
label: 'Images',
|
||||
type: 'file',
|
||||
helpText: 'Images to include along with your message.',
|
||||
list: true,
|
||||
},
|
||||
{
|
||||
key: 'model',
|
||||
label: 'Model',
|
||||
type: 'string',
|
||||
required: true,
|
||||
default: DEFAULT_MODEL, // Optional to default to a specific model for most users
|
||||
dynamic: 'list_models.id.name',
|
||||
altersDynamicFields: false,
|
||||
},
|
||||
{
|
||||
key: 'show_advanced',
|
||||
label: 'Show Advanced Options',
|
||||
type: 'boolean',
|
||||
default: 'false',
|
||||
altersDynamicFields: true,
|
||||
},
|
||||
getAdvancedFields,
|
||||
],
|
||||
// Rename some of the output fields to be more descriptive for a user
|
||||
outputFields: [
|
||||
{ key: 'id', type: 'string', label: 'Completion ID' },
|
||||
{ key: 'model', type: 'string', label: 'Model' },
|
||||
{
|
||||
key: 'usage__prompt_tokens',
|
||||
type: 'number',
|
||||
label: 'Usage: Prompt Tokens',
|
||||
},
|
||||
{
|
||||
key: 'usage__completion_tokens',
|
||||
type: 'number',
|
||||
label: 'Usage: Completion Tokens',
|
||||
},
|
||||
{
|
||||
key: 'usage__total_tokens',
|
||||
type: 'number',
|
||||
label: 'Usage: Total Tokens',
|
||||
},
|
||||
],
|
||||
sample,
|
||||
},
|
||||
};
|
||||
7
vendor/zapier-platform/example-apps/openai/creates/index.js
vendored
Normal file
7
vendor/zapier-platform/example-apps/openai/creates/index.js
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
/* eslint-disable camelcase */
|
||||
const chat_completion = require('./chat_completion');
|
||||
|
||||
// If you add a new create, make sure it is exported here to display in the Zapier Editor
|
||||
module.exports = {
|
||||
[chat_completion.key]: chat_completion,
|
||||
};
|
||||
7
vendor/zapier-platform/example-apps/openai/dynamic_dropdowns/index.js
vendored
Normal file
7
vendor/zapier-platform/example-apps/openai/dynamic_dropdowns/index.js
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
/* eslint-disable camelcase */
|
||||
const list_models = require('./list_models.js');
|
||||
|
||||
// If you add a new Dynamic Dropdown, make sure it is exported here to display in the Zapier Editor
|
||||
module.exports = {
|
||||
[list_models.key]: list_models,
|
||||
};
|
||||
24
vendor/zapier-platform/example-apps/openai/dynamic_dropdowns/list_models.js
vendored
Normal file
24
vendor/zapier-platform/example-apps/openai/dynamic_dropdowns/list_models.js
vendored
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
const { API_URL } = require('../constants');
|
||||
|
||||
const perform = async (z, bundle) => {
|
||||
const response = await z.request({ url: `${API_URL}/models` });
|
||||
|
||||
const responseData = response.data;
|
||||
|
||||
return responseData.data.map((model) => ({
|
||||
id: model.id,
|
||||
name: model.id,
|
||||
}));
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
key: 'list_models',
|
||||
noun: 'Model',
|
||||
display: {
|
||||
label: 'List of Models',
|
||||
description:
|
||||
'This is a hidden trigger, and is used in a Dynamic Dropdown of another trigger.',
|
||||
hidden: true,
|
||||
},
|
||||
operation: { perform },
|
||||
};
|
||||
33
vendor/zapier-platform/example-apps/openai/index.js
vendored
Normal file
33
vendor/zapier-platform/example-apps/openai/index.js
vendored
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/* eslint-disable camelcase */
|
||||
const authentication = require('./authentication');
|
||||
const middleware = require('./middleware');
|
||||
const dynamic_dropdowns = require('./dynamic_dropdowns');
|
||||
const creates = require('./creates');
|
||||
|
||||
module.exports = {
|
||||
// This is just shorthand to reference the installed dependencies you have.
|
||||
// Zapier will need to know these before we can upload.
|
||||
version: require('./package.json').version,
|
||||
platformVersion: require('zapier-platform-core').version,
|
||||
|
||||
authentication,
|
||||
|
||||
beforeRequest: [...middleware.befores],
|
||||
|
||||
afterResponse: [...middleware.afters],
|
||||
|
||||
// If you want your trigger to show up, you better include it here!
|
||||
triggers: {
|
||||
...dynamic_dropdowns,
|
||||
},
|
||||
|
||||
// If you want your searches to show up, you better include it here!
|
||||
searches: {},
|
||||
|
||||
// If you want your creates to show up, you better include it here!
|
||||
creates: {
|
||||
...creates,
|
||||
},
|
||||
|
||||
resources: {},
|
||||
};
|
||||
50
vendor/zapier-platform/example-apps/openai/middleware.js
vendored
Normal file
50
vendor/zapier-platform/example-apps/openai/middleware.js
vendored
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/* eslint-disable camelcase */
|
||||
// This function runs after every outbound request. You can use it to check for
|
||||
// errors or modify the response. You can have as many as you need. They'll need
|
||||
// to each be registered in your index.js file.
|
||||
const handleBadResponses = (response, z, bundle) => {
|
||||
if (response.data.error) {
|
||||
throw new z.errors.Error(
|
||||
response.data.error.message,
|
||||
response.data.error.code,
|
||||
response.status,
|
||||
);
|
||||
}
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
const includeOrgId = (request, z, bundle) => {
|
||||
const { organization_id } = bundle.authData;
|
||||
if (organization_id) {
|
||||
request.headers['OpenAI-Organization'] = organization_id;
|
||||
}
|
||||
return request;
|
||||
};
|
||||
|
||||
// This function runs before every outbound request. You can have as many as you
|
||||
// need. They'll need to each be registered in your index.js file.
|
||||
const includeApiKey = (request, z, bundle) => {
|
||||
const { api_key } = bundle.authData;
|
||||
if (api_key) {
|
||||
// Use these lines to include the API key in the querystring
|
||||
// request.params = request.params || {};
|
||||
// request.params.api_key = api_key;
|
||||
|
||||
// If you want to include the API key in the header:
|
||||
request.headers.Authorization = `Bearer ${api_key}`;
|
||||
}
|
||||
|
||||
return request;
|
||||
};
|
||||
|
||||
const jsonHeaders = (request) => {
|
||||
request.headers['Content-Type'] = 'application/json';
|
||||
request.headers.Accept = 'application/json';
|
||||
return request;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
befores: [includeApiKey, includeOrgId, jsonHeaders],
|
||||
afters: [handleBadResponses],
|
||||
};
|
||||
16
vendor/zapier-platform/example-apps/openai/package.json
vendored
Normal file
16
vendor/zapier-platform/example-apps/openai/package.json
vendored
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"name": "openai",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "jest --testTimeout 10000"
|
||||
},
|
||||
"dependencies": {
|
||||
"zapier-platform-core": "19.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"jest": "^25.5.3"
|
||||
},
|
||||
"private": true
|
||||
}
|
||||
29
vendor/zapier-platform/example-apps/openai/samples/chat.json
vendored
Normal file
29
vendor/zapier-platform/example-apps/openai/samples/chat.json
vendored
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"id": "chatcmpl-123",
|
||||
"object": "chat.completion",
|
||||
"created": 1677652288,
|
||||
"model": "gpt-4o-mini",
|
||||
"system_fingerprint": "fp_44709d6fcb",
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": "\n\nHello there, how may I assist you today?"
|
||||
},
|
||||
"logprobs": null,
|
||||
"finish_reason": "stop"
|
||||
}
|
||||
],
|
||||
"service_tier": "default",
|
||||
"usage": {
|
||||
"prompt_tokens": 9,
|
||||
"completion_tokens": 12,
|
||||
"total_tokens": 21,
|
||||
"completion_tokens_details": {
|
||||
"reasoning_tokens": 0,
|
||||
"accepted_prediction_tokens": 0,
|
||||
"rejected_prediction_tokens": 0
|
||||
}
|
||||
}
|
||||
}
|
||||
66
vendor/zapier-platform/example-apps/openai/test/authentication.test.js
vendored
Normal file
66
vendor/zapier-platform/example-apps/openai/test/authentication.test.js
vendored
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
/* globals describe, it, expect */
|
||||
/* eslint-disable no-undef */
|
||||
|
||||
const App = require('../index');
|
||||
|
||||
describe('custom auth', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('passes authentication and returns json', async () => {
|
||||
const bundle = {
|
||||
authData: {
|
||||
api_key: 'secret',
|
||||
},
|
||||
};
|
||||
|
||||
// Mock successful response
|
||||
const mockResponse = {
|
||||
status: 200,
|
||||
data: {
|
||||
email: 'test@example.com',
|
||||
},
|
||||
};
|
||||
|
||||
// Mock the request client
|
||||
const mockRequest = jest.fn().mockResolvedValue(mockResponse);
|
||||
const z = {
|
||||
request: mockRequest,
|
||||
};
|
||||
|
||||
const response = await App.authentication.test(z, bundle);
|
||||
|
||||
expect(mockRequest).toHaveBeenCalledTimes(1);
|
||||
expect(mockRequest).toHaveBeenCalledWith({
|
||||
url: expect.stringContaining('/me'),
|
||||
});
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.data).toHaveProperty('email', 'test@example.com');
|
||||
});
|
||||
|
||||
it('fails on bad auth', async () => {
|
||||
const bundle = {
|
||||
authData: {
|
||||
api_key: 'bad',
|
||||
},
|
||||
};
|
||||
|
||||
// Mock failed response
|
||||
const mockRequest = jest
|
||||
.fn()
|
||||
.mockRejectedValue(new Error('Incorrect API key provided'));
|
||||
const z = {
|
||||
request: mockRequest,
|
||||
};
|
||||
|
||||
try {
|
||||
await App.authentication.test(z, bundle);
|
||||
} catch (error) {
|
||||
expect(mockRequest).toHaveBeenCalledTimes(1);
|
||||
expect(error.message).toContain('Incorrect API key provided');
|
||||
return;
|
||||
}
|
||||
throw new Error('appTester should have thrown');
|
||||
});
|
||||
});
|
||||
150
vendor/zapier-platform/example-apps/openai/test/chat_completion.test.js
vendored
Normal file
150
vendor/zapier-platform/example-apps/openai/test/chat_completion.test.js
vendored
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
/* globals describe, it, expect */
|
||||
/* eslint-disable no-undef */
|
||||
|
||||
const chatCompletion = require('../creates/chat_completion');
|
||||
const { DEFAULT_MODEL } = require('../constants');
|
||||
|
||||
describe('chat_completion', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('creates a basic chat completion', async () => {
|
||||
const bundle = {
|
||||
inputData: {
|
||||
user_message: 'Hello, how are you?',
|
||||
model: DEFAULT_MODEL,
|
||||
},
|
||||
};
|
||||
|
||||
const mockResponse = {
|
||||
data: {
|
||||
id: 'chatcmpl-123',
|
||||
model: DEFAULT_MODEL,
|
||||
usage: {
|
||||
prompt_tokens: 20,
|
||||
completion_tokens: 15,
|
||||
total_tokens: 35,
|
||||
},
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
content: 'I am doing well, thank you for asking!',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const mockRequest = jest.fn().mockResolvedValue(mockResponse);
|
||||
const z = { request: mockRequest };
|
||||
|
||||
const result = await chatCompletion.operation.perform(z, bundle);
|
||||
|
||||
expect(mockRequest).toHaveBeenCalledTimes(1);
|
||||
expect(mockRequest).toHaveBeenCalledWith({
|
||||
url: expect.stringContaining('/chat/completions'),
|
||||
method: 'POST',
|
||||
body: expect.stringContaining(bundle.inputData.user_message),
|
||||
});
|
||||
|
||||
expect(result).toEqual(mockResponse.data);
|
||||
});
|
||||
|
||||
it('creates a chat completion with advanced options', async () => {
|
||||
const bundle = {
|
||||
inputData: {
|
||||
user_message: 'Write a story',
|
||||
model: DEFAULT_MODEL,
|
||||
developer_message: 'You are a creative writer',
|
||||
temperature: 0.9,
|
||||
max_completion_tokens: 100,
|
||||
},
|
||||
};
|
||||
|
||||
const mockResponse = {
|
||||
data: {
|
||||
id: 'chatcmpl-456',
|
||||
model: DEFAULT_MODEL,
|
||||
usage: {
|
||||
prompt_tokens: 25,
|
||||
completion_tokens: 50,
|
||||
total_tokens: 75,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const mockRequest = jest.fn().mockResolvedValue(mockResponse);
|
||||
const z = { request: mockRequest };
|
||||
|
||||
const result = await chatCompletion.operation.perform(z, bundle);
|
||||
|
||||
expect(mockRequest).toHaveBeenCalledTimes(1);
|
||||
expect(mockRequest).toHaveBeenCalledWith({
|
||||
url: expect.stringContaining('/chat/completions'),
|
||||
method: 'POST',
|
||||
body: expect.stringMatching(/temperature.*0.9/),
|
||||
});
|
||||
|
||||
expect(result).toEqual(mockResponse.data);
|
||||
});
|
||||
|
||||
it('creates a chat completion with image', async () => {
|
||||
const bundle = {
|
||||
inputData: {
|
||||
user_message: 'Describe this image',
|
||||
model: DEFAULT_MODEL,
|
||||
files: ['https://example.com/image.jpg'],
|
||||
},
|
||||
};
|
||||
|
||||
const mockResponse = {
|
||||
data: {
|
||||
id: 'chatcmpl-789',
|
||||
model: DEFAULT_MODEL,
|
||||
usage: {
|
||||
prompt_tokens: 30,
|
||||
completion_tokens: 20,
|
||||
total_tokens: 50,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const mockRequest = jest.fn().mockResolvedValue(mockResponse);
|
||||
const z = { request: mockRequest };
|
||||
|
||||
const result = await chatCompletion.operation.perform(z, bundle);
|
||||
|
||||
expect(mockRequest).toHaveBeenCalledTimes(1);
|
||||
expect(mockRequest).toHaveBeenCalledWith({
|
||||
url: expect.stringContaining('/chat/completions'),
|
||||
method: 'POST',
|
||||
body: expect.stringMatching(/image_url.*example.com/),
|
||||
});
|
||||
|
||||
expect(result).toEqual(mockResponse.data);
|
||||
});
|
||||
|
||||
it('handles API errors', async () => {
|
||||
const bundle = {
|
||||
inputData: {
|
||||
user_message: 'Hello',
|
||||
model: DEFAULT_MODEL,
|
||||
},
|
||||
};
|
||||
|
||||
const mockRequest = jest
|
||||
.fn()
|
||||
.mockRejectedValue(new Error('Invalid request'));
|
||||
const z = { request: mockRequest };
|
||||
|
||||
try {
|
||||
await chatCompletion.operation.perform(z, bundle);
|
||||
} catch (error) {
|
||||
expect(mockRequest).toHaveBeenCalledTimes(1);
|
||||
expect(error.message).toContain('Invalid request');
|
||||
return;
|
||||
}
|
||||
throw new Error('Should have thrown an error');
|
||||
});
|
||||
});
|
||||
51
vendor/zapier-platform/example-apps/openai/test/list_models.test.js
vendored
Normal file
51
vendor/zapier-platform/example-apps/openai/test/list_models.test.js
vendored
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/* globals describe, it, expect */
|
||||
/* eslint-disable no-undef */
|
||||
|
||||
const listModels = require('../dynamic_dropdowns/list_models');
|
||||
|
||||
describe('list_models', () => {
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('returns formatted list of models', async () => {
|
||||
const mockResponse = {
|
||||
data: {
|
||||
data: [{ id: 'gpt-4' }, { id: 'gpt-3.5-turbo' }],
|
||||
},
|
||||
};
|
||||
|
||||
const mockRequest = jest.fn().mockResolvedValue(mockResponse);
|
||||
const z = { request: mockRequest };
|
||||
const bundle = {};
|
||||
|
||||
const results = await listModels.operation.perform(z, bundle);
|
||||
|
||||
expect(mockRequest).toHaveBeenCalledTimes(1);
|
||||
expect(mockRequest).toHaveBeenCalledWith({
|
||||
url: expect.stringContaining('/models'),
|
||||
});
|
||||
|
||||
expect(results).toEqual([
|
||||
{ id: 'gpt-4', name: 'gpt-4' },
|
||||
{ id: 'gpt-3.5-turbo', name: 'gpt-3.5-turbo' },
|
||||
]);
|
||||
});
|
||||
|
||||
it('handles API errors', async () => {
|
||||
const mockRequest = jest
|
||||
.fn()
|
||||
.mockRejectedValue(new Error('Failed to fetch models'));
|
||||
const z = { request: mockRequest };
|
||||
const bundle = {};
|
||||
|
||||
try {
|
||||
await listModels.operation.perform(z, bundle);
|
||||
} catch (error) {
|
||||
expect(mockRequest).toHaveBeenCalledTimes(1);
|
||||
expect(error.message).toContain('Failed to fetch models');
|
||||
return;
|
||||
}
|
||||
throw new Error('Should have thrown an error');
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue