AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
feat(localai): list models, create embeddings, generate speech and transcribe audio on your LocalAI server (#16296)
Odai Ahmad81e85abc41ea
diff --git a/bun.lock b/bun.lockindex 781fdf2f88f..a70cd76c2c7 100644--- a/bun.lock+++ b/bun.lock@@ -5295,7 +5295,7 @@},"packages/pieces/community/localai": {"name": "@activepieces/piece-localai",- "version": "0.2.0",+ "version": "0.3.0","dependencies": {"@activepieces/core-piece-types": "workspace:*","@activepieces/core-utils": "workspace:*",diff --git a/packages/pieces/community/localai/package.json b/packages/pieces/community/localai/package.jsonindex 3a4c6adcc66..2927406ca32 100644--- a/packages/pieces/community/localai/package.json+++ b/packages/pieces/community/localai/package.json@@ -1,6 +1,6 @@{"name": "@activepieces/piece-localai",- "version": "0.2.0",+ "version": "0.3.0","main": "./dist/src/index.js","types": "./dist/src/index.d.ts","dependencies": {diff --git a/packages/pieces/community/localai/src/i18n/translation.json b/packages/pieces/community/localai/src/i18n/translation.jsonindex 5c3878d7e01..10f1899ef5c 100644--- a/packages/pieces/community/localai/src/i18n/translation.json+++ b/packages/pieces/community/localai/src/i18n/translation.json@@ -2,11 +2,19 @@"The free, Self-hosted, community-driven and local-first. Drop-in replacement for OpenAI running on consumer-grade hardware. No GPU required.": "The free, Self-hosted, community-driven and local-first. Drop-in replacement for OpenAI running on consumer-grade hardware. No GPU required.","Server URL": "Server URL","Access Token": "Access Token",- "LocalAI Instance URL": "LocalAI Instance URL",- "LocalAI Access Token": "LocalAI Access Token",+ "The address of your LocalAI server, for example http://localhost:8080/v1. It must be reachable from Activepieces.": "The address of your LocalAI server, for example http://localhost:8080/v1. It must be reachable from Activepieces.",+ "One of the API keys your server was started with (--api-keys or API_KEY). Leave empty if the server has no keys.": "One of the API keys your server was started with (--api-keys or API_KEY). Leave empty if the server has no keys.","Ask LocalAI": "Ask LocalAI",+ "List Models": "List Models",+ "Create Embedding": "Create Embedding",+ "Text to Speech": "Text to Speech",+ "Transcribe Audio": "Transcribe Audio","Custom API Call": "Custom API Call","Ask LocalAI anything you want!": "Ask LocalAI anything you want!",+ "List the models installed on your LocalAI server.": "List the models installed on your LocalAI server.",+ "Turn text into a vector for semantic search or clustering.": "Turn text into a vector for semantic search or clustering.",+ "Turn text into a spoken audio file.": "Turn text into a spoken audio file.",+ "Turn speech in an audio file into text.": "Turn speech in an audio file into text.","Make a custom API call to a specific endpoint": "Make a custom API call to a specific endpoint","Model": "Model","Question": "Question",@@ -16,32 +24,54 @@"Frequency penalty": "Frequency penalty","Presence penalty": "Presence penalty","Roles": "Roles",+ "Name Contains": "Name Contains",+ "Text": "Text",+ "Voice": "Voice",+ "Audio Format": "Audio Format",+ "File Name": "File Name",+ "Audio": "Audio",+ "Language": "Language","Method": "Method","Headers": "Headers","Query Parameters": "Query Parameters","Body Type": "Body Type","Body": "Body",- "Response is Binary ?": "Response is Binary ?",- "No Error on Failure": "No Error on Failure",- "Timeout (in seconds)": "Timeout (in seconds)",+ "Response is Binary": "Response is Binary",+ "Return Error as Output": "Return Error as Output",+ "Timeout": "Timeout","Follow redirects": "Follow redirects",- "The model which will generate the completion. Some models are suitable for natural language tasks, others specialize in code.": "The model which will generate the completion. Some models are suitable for natural language tasks, others specialize in code.",- "Controls randomness: Lowering results in less random completions. As the temperature approaches zero, the model will become deterministic and repetitive.": "Controls randomness: Lowering results in less random completions. As the temperature approaches zero, the model will become deterministic and repetitive.",- "The maximum number of tokens to generate. Requests can use up to 2,048 or 4,096 tokens shared between prompt and completion, don't set the value to maximum and leave some tokens for the input. The exact limit varies by model. (One token is roughly 4 characters for normal English text)": "The maximum number of tokens to generate. Requests can use up to 2,048 or 4,096 tokens shared between prompt and completion, don't set the value to maximum and leave some tokens for the input. The exact limit varies by model. (One token is roughly 4 characters for normal English text)",- "An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.": "An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.",- "Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.": "Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.",- "Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the mode's likelihood to talk about new topics.": "Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the mode's likelihood to talk about new topics.",- "Array of roles to specify more accurate response": "Array of roles to specify more accurate response",+ "The chat model on your LocalAI server that will write the answer.": "The chat model on your LocalAI server that will write the answer.",+ "Higher is more random, lower is more focused. Defaults to 0.9.": "Higher is more random, lower is more focused. Defaults to 0.9.",+ "Longest answer to generate, in tokens. Defaults to 2048.": "Longest answer to generate, in tokens. Defaults to 2048.",+ "Nucleus sampling, from 0 to 1. Defaults to 1.": "Nucleus sampling, from 0 to 1. Defaults to 1.",+ "From -2 to 2. Higher repeats lines less. Defaults to 0.": "From -2 to 2. Higher repeats lines less. Defaults to 0.",+ "From -2 to 2. Higher moves to new topics. Defaults to 0.6.": "From -2 to 2. Higher moves to new topics. Defaults to 0.6.",+ "Messages sent before the question, each with a role and content.": "Messages sent before the question, each with a role and content.",+ "Only return models whose id includes this text.": "Only return models whose id includes this text.",+ "An embedding model installed on your LocalAI server.": "An embedding model installed on your LocalAI server.",+ "The text to turn into a vector.": "The text to turn into a vector.",+ "A text-to-speech model installed on your LocalAI server.": "A text-to-speech model installed on your LocalAI server.",+ "The text to read aloud.": "The text to read aloud.",+ "For models with several voices. Empty uses the default.": "For models with several voices. Empty uses the default.",+ "Not every LocalAI backend supports every format.": "Not every LocalAI backend supports every format.",+ "Name for the audio file, without the extension.": "Name for the audio file, without the extension.",+ "A speech-recognition model (for example whisper) installed on your LocalAI server.": "A speech-recognition model (for example whisper) installed on your LocalAI server.",+ "The recording to transcribe.": "The recording to transcribe.",+ "Language code of the speech. Empty auto-detects.": "Language code of the speech. Empty auto-detects.","Authorization headers are injected automatically from your connection.": "Authorization headers are injected automatically from your connection.",+ "Appended to the URL as ?key=value.": "Appended to the URL as ?key=value.","Enable for files like PDFs, images, etc.": "Enable for files like PDFs, images, etc.",+ "On a failed request, output the error instead of failing the step.": "On a failed request, output the error instead of failing the step.",+ "Seconds to wait for a response. Empty: up to the flow limit (10 min).": "Seconds to wait for a response. Empty: up to the flow limit (10 min).",+ "Follow 3xx redirects instead of returning them as the response.": "Follow 3xx redirects instead of returning them as the response.",+ "WAV": "WAV",+ "MP3": "MP3",+ "OGG": "OGG",+ "FLAC": "FLAC","GET": "GET","POST": "POST","PATCH": "PATCH","PUT": "PUT","DELETE": "DELETE",- "HEAD": "HEAD",- "None": "None",- "JSON": "JSON",- "Form Data": "Form Data",- "Raw": "Raw"+ "HEAD": "HEAD"}\ No newline at end of filediff --git a/packages/pieces/community/localai/src/index.ts b/packages/pieces/community/localai/src/index.tsindex b3f1f2500d8..994d51c805e 100644--- a/packages/pieces/community/localai/src/index.ts+++ b/packages/pieces/community/localai/src/index.ts@@ -1,27 +1,14 @@import { createCustomApiCallAction } from '@activepieces/pieces-common';-import {- PieceAuth,- Property,- createPiece,-} from '@activepieces/pieces-framework';+import { createPiece } from '@activepieces/pieces-framework';import { PieceCategory } from '@activepieces/pieces-framework';import { askLocalAI } from './lib/actions/send-prompt';+import { listModels } from './lib/actions/list-models';+import { createEmbedding } from './lib/actions/create-embedding';+import { textToSpeech } from './lib/actions/text-to-speech';+import { transcribeAudio } from './lib/actions/transcribe-audio';+import { localaiAuth } from './lib/auth';+import { localaiCommon } from './lib/common';-export const localaiAuth = PieceAuth.CustomAuth({- props: {- base_url: Property.ShortText({- displayName: 'Server URL',- description: 'LocalAI Instance URL',- required: true,- }),- access_token: Property.ShortText({- displayName: 'Access Token',- description: 'LocalAI Access Token',- required: false,- }),- },- required: true,-});export const openai = createPiece({displayName: 'LocalAI',description:@@ -32,16 +19,18 @@ export const openai = createPiece({auth: localaiAuth,actions: [askLocalAI,+ listModels,+ createEmbedding,+ textToSpeech,+ transcribeAudio,createCustomApiCallAction({- baseUrl: (auth) => (auth)?.props.base_url ?? '',+ baseUrl: (auth) => (auth ? localaiCommon.baseUrl(auth) : ''),auth: localaiAuth,- authMapping: async (auth) => ({- Authorization: `Bearer ${- auth.props.access_token || ''- }`,- }),+ authMapping: async (auth) => localaiCommon.authHeaders(auth),}),],authors: ["hkboujrida","kishanprmr","MoShizzle","abuaboud"],triggers: [],});++export { localaiAuth };diff --git a/packages/pieces/community/localai/src/lib/actions/create-embedding.ts b/packages/pieces/community/localai/src/lib/actions/create-embedding.tsnew file mode 100644index 00000000000..d9c7156cb31--- /dev/null+++ b/packages/pieces/community/localai/src/lib/actions/create-embedding.ts@@ -0,0 +1,42 @@+import { createAction, Property } from '@activepieces/pieces-framework';+import { localaiAuth } from '../auth';+import { localaiCommon } from '../common';+import { createEmbeddingOutputSchema } from '../output-schemas';++export const createEmbedding = createAction({+ audience: 'both',+ auth: localaiAuth,+ name: 'create_embedding',+ classification: 'READ',+ displayName: 'Create Embedding',+ description: 'Turn text into a vector for semantic search or clustering.',+ aiMetadata: { description: 'Converts one block of text into a numeric embedding vector with an embedding model installed on the connected self-hosted LocalAI server, for storing in a vector database or for semantic search, clustering, and RAG, without the text leaving your infrastructure. The model must be an embedding model (for example mxbai-embed-large or nomic-embed), not a chat model: call list_models when unsure. Returns the vector and its length; one input per call, so loop to embed several texts. Deterministic for the same model and text, so repeat calls are idempotent.', idempotent: true },+ props: {+ model: localaiCommon.modelDropdown({+ displayName: 'Model',+ description: 'An embedding model installed on your LocalAI server.',+ }),+ input: Property.LongText({+ displayName: 'Text',+ description: 'The text to turn into a vector.',+ required: true,+ }),+ },+ outputSchema: createEmbeddingOutputSchema,+ async run({ auth, propsValue }) {+ try {+ const response = await localaiCommon.client(auth).embeddings.create({+ model: propsValue.model,+ input: propsValue.input,+ });+ const embedding = response.data[0]?.embedding ?? [];+ return {+ model: response.model,+ dimensions: embedding.length,+ embedding,+ };+ } catch (error) {+ throw localaiCommon.friendlyError(error);+ }+ },+});diff --git a/packages/pieces/community/localai/src/lib/actions/list-models.ts b/packages/pieces/community/localai/src/lib/actions/list-models.tsnew file mode 100644index 00000000000..997400a955c--- /dev/null+++ b/packages/pieces/community/localai/src/lib/actions/list-models.ts@@ -0,0 +1,35 @@+import { createAction, Property } from '@activepieces/pieces-framework';+import { localaiAuth } from '../auth';+import { localaiCommon } from '../common';+import { listModelsOutputSchema } from '../output-schemas';++export const listModels = createAction({+ audience: 'both',+ auth: localaiAuth,+ name: 'list_models',+ classification: 'SEARCH',+ displayName: 'List Models',+ description: 'List the models installed on your LocalAI server.',+ aiMetadata: { description: 'Returns the ids of the models installed on the connected self-hosted LocalAI server, optionally only those whose id contains a case-insensitive text such as llama, embed, or whisper. The list covers every kind of model the server has (chat, embedding, speech, transcription) and LocalAI does not say which is which, so pick by name. Use it to confirm a model id exists before calling ask_localai, create_embedding, text_to_speech, or transcribe_audio. Read-only and idempotent.', idempotent: true },+ props: {+ contains: Property.ShortText({+ displayName: 'Name Contains',+ description: 'Only return models whose id includes this text.',+ placeholder: 'e.g. llama',+ required: false,+ }),+ },+ outputSchema: listModelsOutputSchema,+ async run({ auth, propsValue }) {+ const filter = propsValue.contains?.trim().toLowerCase();+ try {+ const response = await localaiCommon.client(auth).models.list();+ const models = response.data+ .filter((model) => !filter || model.id.toLowerCase().includes(filter))+ .map((model) => ({ id: model.id }));+ return { count: models.length, models };+ } catch (error) {+ throw localaiCommon.friendlyError(error);+ }+ },+});diff --git a/packages/pieces/community/localai/src/lib/actions/send-prompt.ts b/packages/pieces/community/localai/src/lib/actions/send-prompt.tsindex 48b05a1ff47..e02d57bfa19 100644--- a/packages/pieces/community/localai/src/lib/actions/send-prompt.ts+++ b/packages/pieces/community/localai/src/lib/actions/send-prompt.ts@@ -1,28 +1,7 @@-import {- AppConnectionValueForAuthProperty,- createAction,- Property,-} from '@activepieces/pieces-framework';+import { createAction, Property } from '@activepieces/pieces-framework';import OpenAI from 'openai';-import {- AuthenticationType,- httpClient,- HttpMethod,-} from '@activepieces/pieces-common';-import { localaiAuth } from '../..';--const billingIssueMessage = `Error Occurred: 429 \n--1. Set LocalAI API Url. \n-2. Generate a new API key (optional). \n-3. Attempt the process again. \n--For guidance, visit: https://localai.io/`;--const unaurthorizedMessage = `Error Occurred: 401 \n--Ensure that your API key is valid. \n-`;+import { localaiAuth } from '../auth';+import { localaiCommon } from '../common';export const askLocalAI = createAction({audience: 'both',@@ -31,54 +10,12 @@ export const askLocalAI = createAction({classification: 'READ',displayName: 'Ask LocalAI',description: 'Ask LocalAI anything you want!',- aiMetadata: { description: 'Sends a prompt to a chat model running on your own self-hosted LocalAI server, through the OpenAI-compatible completions endpoint at the server URL stored on the connection, with optional sampling controls and a roles array for system or assistant priming; unlike the hosted-vendor pieces it keeps no conversation memory, so every call is stateless. Pick it only when inference must stay on your own LocalAI instance rather than a cloud provider such as OpenAI, Groq, or DeepSeek, and use the piece\'s Custom API Call action for LocalAI endpoints other than chat completions, such as embeddings, images, or audio. Requires a question and a model id that actually exists on that instance, since the dropdown lists only what the server reports and the default gpt-3.5-turbo may not be installed; not idempotent: each call produces a fresh completion.', idempotent: false },+ aiMetadata: { description: 'Sends a prompt to a chat model running on your own self-hosted LocalAI server, through the OpenAI-compatible chat completions endpoint at the server URL stored on the connection, with optional sampling controls and a roles array for system or assistant priming, and returns the reply as plain text. It keeps no conversation memory, so every call is stateless. Pick it only when inference must stay on your own LocalAI instance rather than a cloud provider such as OpenAI, Groq, or DeepSeek. Requires a question and a model id installed on that instance: call list_models first when unsure which ids exist. Use create_embedding for vectors, text_to_speech or transcribe_audio for audio, and the Custom API Call action for other LocalAI endpoints. Not idempotent: each call produces a fresh completion.', idempotent: false },props: {- model: Property.Dropdown({- auth: localaiAuth,+ model: localaiCommon.modelDropdown({displayName: 'Model',- required: true,description:- 'The model which will generate the completion. Some models are suitable for natural language tasks, others specialize in code.',- refreshers: [],- defaultValue: 'gpt-3.5-turbo',- options: async ({ auth }) => {- if (!auth) {- return {- disabled: true,- placeholder: 'Enter your api key first',- options: [],- };- }-- const authValue = auth as AppConnectionValueForAuthProperty<typeof localaiAuth>;- try {- const response = await httpClient.sendRequest<{- data: { id: string }[];- }>({- url: authValue.props.base_url + '/models',- method: HttpMethod.GET,- authentication: {- type: AuthenticationType.BEARER_TOKEN,- token: authValue.props.access_token as string,- },- });- return {- disabled: false,- options: response.body.data.map((model) => {- return {- label: model.id,- value: model.id,- };- }),- };- } catch (error) {- return {- disabled: true,- options: [],- placeholder: "Couldn't Load Models",- };- }- },+ 'The chat model on your LocalAI server that will write the answer.',}),prompt: Property.LongText({displayName: 'Question',@@ -87,144 +24,161 @@ export const askLocalAI = createAction({temperature: Property.Number({displayName: 'Temperature',required: false,- description:- 'Controls randomness: Lowering results in less random completions. As the temperature approaches zero, the model will become deterministic and repetitive.',+ description: 'Higher is more random, lower is more focused. Defaults to 0.9.',}),maxTokens: Property.Number({displayName: 'Maximum Tokens',required: false,- description:- "The maximum number of tokens to generate. Requests can use up to 2,048 or 4,096 tokens shared between prompt and completion, don't set the value to maximum and leave some tokens for the input. The exact limit varies by model. (One token is roughly 4 characters for normal English text)",+ description: 'Longest answer to generate, in tokens. Defaults to 2048.',}),topP: Property.Number({displayName: 'Top P',required: false,- description:- 'An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.',+ description: 'Nucleus sampling, from 0 to 1. Defaults to 1.',}),frequencyPenalty: Property.Number({displayName: 'Frequency penalty',required: false,- description:- "Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.",+ description: 'From -2 to 2. Higher repeats lines less. Defaults to 0.',}),presencePenalty: Property.Number({displayName: 'Presence penalty',required: false,- description:- "Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the mode's likelihood to talk about new topics.",+ description: 'From -2 to 2. Higher moves to new topics. Defaults to 0.6.',}),roles: Property.Json({displayName: 'Roles',required: false,- description: 'Array of roles to specify more accurate response',+ description: 'Messages sent before the question, each with a role and content.',defaultValue: [{ role: 'system', content: 'You are a helpful assistant.' },],}),},async run({ auth, propsValue }) {- const openai = new OpenAI({- baseURL: auth.props.base_url,- apiKey: auth.props.access_token,- });- let billingIssue = false;- let unaurthorized = false;- let model = 'gpt-3.5-turbo';- if (propsValue.model) {- model = propsValue.model;- }- let temperature = 0.9;- if (propsValue.temperature) {- temperature = Number(propsValue.temperature);- }- let maxTokens = 2048;- if (propsValue.maxTokens) {- maxTokens = Number(propsValue.maxTokens);- }- let topP = 1;- if (propsValue.topP) {- topP = Number(propsValue.topP);- }- let frequencyPenalty = 0.0;- if (propsValue.frequencyPenalty) {- frequencyPenalty = Number(propsValue.frequencyPenalty);- }- let presencePenalty = 0.6;- if (propsValue.presencePenalty) {- presencePenalty = Number(propsValue.presencePenalty);- }-- const rolesArray = propsValue.roles- ? (propsValue.roles as unknown as any[])- : [];- const roles = rolesArray.map((item) => {- const rolesEnum = ['system', 'user', 'assistant'];- if (!rolesEnum.includes(item.role)) {- throw new Error(- 'The only available roles are: [system, user, assistant]'- );- }-- return {- role: item.role,- content: item.content,- };- });+ const roles = parseRoles(propsValue.roles);- const maxRetries = 4;- let retries = 0;- let response: string | undefined;- while (retries < maxRetries) {- try {- response = (- await openai.chat.completions.create({- model: model,- messages: [- ...roles,- {- role: 'user',- content: propsValue['prompt'],- },- ],- temperature: temperature,- max_tokens: maxTokens,- top_p: topP,- frequency_penalty: frequencyPenalty,- presence_penalty: presencePenalty,- })- )?.choices[0]?.message?.content?.trim();- break; // Break out of the loop if the request is successful- } catch (error: any) {- if (error?.message?.includes('code 429')) {- billingIssue = true;- if (retries + 1 === maxRetries) {- throw error;- }- // Calculate the time delay for the next retry using exponential backoff- const delay = Math.pow(6, retries) * 1000;- console.log(`Retrying in ${delay} milliseconds...`);- await sleep(delay); // Wait for the calculated delay- retries++;- break;- } else {- if (error?.message?.includes('code 401')) {- unaurthorized = true;- }- throw error;- }- }- }- if (billingIssue) {- throw new Error(billingIssueMessage);- }- if (unaurthorized) {- throw new Error(unaurthorizedMessage);+ try {+ const completion = await localaiCommon+ .client(auth)+ .chat.completions.create({+ model: propsValue.model,+ messages: [...roles, { role: 'user', content: propsValue.prompt }],+ temperature: propsValue.temperature ?? 0.9,+ max_tokens: propsValue.maxTokens ?? 2048,+ top_p: propsValue.topP ?? 1,+ frequency_penalty: propsValue.frequencyPenalty ?? 0,+ presence_penalty: propsValue.presencePenalty ?? 0.6,+ });+ return completion.choices[0]?.message?.content?.trim();+ } catch (error) {+ throw localaiCommon.friendlyError(error);}- return response;},});-function sleep(ms: number) {- return new Promise((resolve) => setTimeout(resolve, ms));+function parseRoles(value: unknown): ChatMessage[] {+ if (value === undefined || value === null || value === '') {+ return [];+ }+ if (!Array.isArray(value)) {+ throw new Error(+ 'Roles must be an array of { "role": "system" | "user" | "assistant", "content": "..." }.'+ );+ }+ return value.map((item: unknown) => toMessage(item));}++function toMessage(item: unknown): ChatMessage {+ if (+ typeof item !== 'object' ||+ item === null ||+ !('role' in item) ||+ !isRole(item.role)+ ) {+ throw new Error('The only available roles are: [system, user, assistant]');+ }+ const content = 'content' in item ? item.content : '';+ if (typeof content === 'string') {+ return { role: item.role, content };+ }+ if (!Array.isArray(content)) {+ throw new Error(+ 'Each role content must be text or an array of content parts.'+ );+ }+ if (item.role === 'user') {+ return { role: 'user', content: content.map(toUserPart) };+ }+ return { role: item.role, content: content.map(toTextPart) };+}++function toUserPart(part: unknown): ChatCompletionContentPart {+ if (isImagePart(part)) {+ return {+ type: 'image_url',+ image_url: {+ url: part.image_url.url,+ ...(part.image_url.detail ? { detail: part.image_url.detail } : {}),+ },+ };+ }+ return toTextPart(part);+}++function toTextPart(part: unknown): ChatCompletionContentPartText {+ if (+ typeof part === 'object' &&+ part !== null &&+ 'type' in part &&+ part.type === 'text' &&+ 'text' in part &&+ typeof part.text === 'string'+ ) {+ return { type: 'text', text: part.text };+ }+ throw new Error(+ 'Content parts must be { "type": "text", "text": "..." }, or { "type": "image_url", "image_url": { "url": "..." } } in user messages.'+ );+}++function isImagePart(part: unknown): part is ImagePart {+ if (+ typeof part !== 'object' ||+ part === null ||+ !('type' in part) ||+ part.type !== 'image_url' ||+ !('image_url' in part)+ ) {+ return false;+ }+ const image = part.image_url;+ if (+ typeof image !== 'object' ||+ image === null ||+ !('url' in image) ||+ typeof image.url !== 'string'+ ) {+ return false;+ }+ return (+ !('detail' in image) ||+ image.detail === undefined ||+ image.detail === 'auto' ||+ image.detail === 'low' ||+ image.detail === 'high'+ );+}++function isRole(role: unknown): role is 'system' | 'user' | 'assistant' {+ return role === 'system' || role === 'user' || role === 'assistant';+}++type ChatMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam;+type ChatCompletionContentPart = OpenAI.Chat.Completions.ChatCompletionContentPart;+type ChatCompletionContentPartText =+ OpenAI.Chat.Completions.ChatCompletionContentPartText;+type ImagePart = {+ type: 'image_url';+ image_url: { url: string; detail?: 'auto' | 'low' | 'high' };+};diff --git a/packages/pieces/community/localai/src/lib/actions/text-to-speech.ts b/packages/pieces/community/localai/src/lib/actions/text-to-speech.tsnew file mode 100644index 00000000000..ca794e2801a--- /dev/null+++ b/packages/pieces/community/localai/src/lib/actions/text-to-speech.ts@@ -0,0 +1,101 @@+import { createAction, Property } from '@activepieces/pieces-framework';+import { httpClient, HttpMethod } from '@activepieces/pieces-common';+import { localaiAuth } from '../auth';+import { localaiCommon } from '../common';+import { textToSpeechOutputSchema } from '../output-schemas';++export const textToSpeech = createAction({+ audience: 'both',+ auth: localaiAuth,+ name: 'text_to_speech',+ classification: 'READ',+ displayName: 'Text to Speech',+ description: 'Turn text into a spoken audio file.',+ aiMetadata: { description: 'Converts text into a spoken audio file with a text-to-speech model installed on the connected self-hosted LocalAI server (for example a piper, kokoro, or bark voice), and saves it as a file in the flow, returning the file reference, name, format, and size. The model must be a speech model, not a chat model: call list_models when unsure. Voice is optional and model-specific, and only some backends honor the output format, so WAV is the safe choice. Each call writes a new file, so it is not idempotent.', idempotent: false },+ props: {+ model: localaiCommon.modelDropdown({+ displayName: 'Model',+ description: 'A text-to-speech model installed on your LocalAI server.',+ }),+ text: Property.LongText({+ displayName: 'Text',+ description: 'The text to read aloud.',+ required: true,+ }),+ voice: Property.ShortText({+ displayName: 'Voice',+ description: 'For models with several voices. Empty uses the default.',+ required: false,+ }),+ format: Property.StaticDropdown({+ displayName: 'Audio Format',+ description: 'Not every LocalAI backend supports every format.',+ required: true,+ defaultValue: 'wav',+ options: {+ options: [+ { label: 'WAV', value: 'wav' },+ { label: 'MP3', value: 'mp3' },+ { label: 'OGG', value: 'ogg' },+ { label: 'FLAC', value: 'flac' },+ ],+ },+ }),+ fileName: Property.ShortText({+ displayName: 'File Name',+ description: 'Name for the audio file, without the extension.',+ required: false,+ defaultValue: 'speech',+ }),+ },+ outputSchema: textToSpeechOutputSchema,+ async run({ auth, propsValue, files }) {+ const { model, text, voice, format } = propsValue;+ const { data, error } = await sendSpeechRequest({+ url: `${localaiCommon.baseUrl(auth)}/audio/speech`,+ headers: localaiCommon.authHeaders(auth),+ body: {+ model,+ input: text,+ response_format: format,+ ...(voice ? { voice } : {}),+ },+ });+ if (data === null) {+ throw localaiCommon.friendlyError(error);+ }+ const fileName = `${propsValue.fileName?.trim() || 'speech'}.${format}`;+ const file = await files.write({ fileName, data });+ return {+ file,+ file_name: fileName,+ format,+ size_bytes: data.length,+ model,+ };+ },+});++async function sendSpeechRequest({+ url,+ headers,+ body,+}: {+ url: string;+ headers: Record<string, string>;+ body: Record<string, unknown>;+}): Promise<{ data: Buffer; error: null } | { data: null; error: unknown }> {+ try {+ const response = await httpClient.sendRequest<ArrayBuffer>({+ method: HttpMethod.POST,+ url,+ headers: { ...headers, 'Content-Type': 'application/json' },+ body,+ responseType: 'arraybuffer',+ timeout: localaiCommon.requestTimeoutMs * 2,+ });+ return { data: Buffer.from(response.body), error: null };+ } catch (error) {+ return { data: null, error };+ }+}diff --git a/packages/pieces/community/localai/src/lib/actions/transcribe-audio.ts b/packages/pieces/community/localai/src/lib/actions/transcribe-audio.tsnew file mode 100644index 00000000000..8414ed7ebe7--- /dev/null+++ b/packages/pieces/community/localai/src/lib/actions/transcribe-audio.ts@@ -0,0 +1,60 @@+import { createAction, Property } from '@activepieces/pieces-framework';+import { toFile } from 'openai';+import { localaiAuth } from '../auth';+import { localaiCommon } from '../common';+import { transcribeAudioOutputSchema } from '../output-schemas';++export const transcribeAudio = createAction({+ audience: 'both',+ auth: localaiAuth,+ name: 'transcribe_audio',+ classification: 'READ',+ displayName: 'Transcribe Audio',+ description: 'Turn speech in an audio file into text.',+ aiMetadata: { description: 'Transcribes the speech in an audio file into text with a speech-recognition model installed on the connected self-hosted LocalAI server (for example a whisper model), so the audio never leaves your infrastructure. Takes a file from an earlier step (wav, mp3, ogg, m4a and other formats ffmpeg can read) and an optional language hint, and returns the full text plus timed segments. The model must be a transcription model, not a chat model: call list_models when unsure. Read-only; repeat calls on the same file return the same text.', idempotent: true },+ props: {+ model: localaiCommon.modelDropdown({+ displayName: 'Model',+ description:+ 'A speech-recognition model (for example whisper) installed on your LocalAI server.',+ }),+ audio: Property.File({+ displayName: 'Audio',+ description: 'The recording to transcribe.',+ required: true,+ }),+ language: Property.ShortText({+ displayName: 'Language',+ description: 'Language code of the speech. Empty auto-detects.',+ placeholder: 'e.g. en',+ required: false,+ }),+ },+ outputSchema: transcribeAudioOutputSchema,+ async run({ auth, propsValue }) {+ const { model, audio, language } = propsValue;+ try {+ const file = await toFile(audio.data, audio.filename);+ const response = await localaiCommon+ .client(auth)+ .audio.transcriptions.create({+ model,+ file,+ response_format: 'verbose_json',+ ...(language?.trim() ? { language: language.trim() } : {}),+ });+ return {+ text: response.text.trim(),+ duration: response.duration ?? null,+ segments: (response.segments ?? []).map((segment) => ({+ id: segment.id,+ start: segment.start,+ end: segment.end,+ text: segment.text.trim(),+ })),+ };+ } catch (error) {+ throw localaiCommon.friendlyError(error);+ }+ },+});diff --git a/packages/pieces/community/localai/src/lib/auth.ts b/packages/pieces/community/localai/src/lib/auth.tsnew file mode 100644index 00000000000..e38b5ea88f5--- /dev/null+++ b/packages/pieces/community/localai/src/lib/auth.ts@@ -0,0 +1,75 @@+import {+ AuthenticationType,+ httpClient,+ HttpMethod,+} from '@activepieces/pieces-common';+import { PieceAuth, Property } from '@activepieces/pieces-framework';++export const localaiAuth = PieceAuth.CustomAuth({+ props: {+ base_url: Property.ShortText({+ displayName: 'Server URL',+ description:+ 'The address of your LocalAI server, for example http://localhost:8080/v1. It must be reachable from Activepieces.',+ required: true,+ }),+ access_token: Property.ShortText({+ displayName: 'Access Token',+ description:+ 'One of the API keys your server was started with (--api-keys or API_KEY). Leave empty if the server has no keys.',+ required: false,+ }),+ },+ required: true,+ validate: async ({ auth }) => {+ const baseUrl = auth.base_url.trim().replace(/\/+$/, '');+ const token = auth.access_token?.trim();+ try {+ await httpClient.sendRequest({+ method: HttpMethod.GET,+ url: `${baseUrl}/models`,+ timeout: 15_000,+ ...(token+ ? {+ authentication: {+ type: AuthenticationType.BEARER_TOKEN,+ token,+ },+ }+ : {}),+ });+ return { valid: true };+ } catch (error) {+ const status = readStatus(error);+ if (status === 401 || status === 403) {+ return {+ valid: false,+ error:+ "LocalAI rejected the access token. Use one of the server's API keys, or leave it empty if the server has none.",+ };+ }+ return {+ valid: false,+ error: status+ ? `LocalAI answered ${status} at ${baseUrl}/models. Check the Server URL (it usually ends in /v1).`+ : `Could not reach LocalAI at ${baseUrl}. Check the Server URL and that the server is running and reachable from Activepieces.`,+ };+ }+ },+});++function readStatus(error: unknown): number | undefined {+ if (typeof error !== 'object' || error === null || !('response' in error)) {+ return undefined;+ }+ const response = error.response;+ if (+ typeof response === 'object' &&+ response !== null &&+ 'status' in response &&+ typeof response.status === 'number'+ ) {+ return response.status;+ }+ return undefined;+}diff --git a/packages/pieces/community/localai/src/lib/common.ts b/packages/pieces/community/localai/src/lib/common.tsnew file mode 100644index 00000000000..a0f05023b07--- /dev/null+++ b/packages/pieces/community/localai/src/lib/common.ts@@ -0,0 +1,169 @@+import {+ AppConnectionValueForAuthProperty,+ Property,+} from '@activepieces/pieces-framework';+import OpenAI from 'openai';+import { localaiAuth } from './auth';++type LocalaiAuthValue = AppConnectionValueForAuthProperty<typeof localaiAuth>;++function baseUrl(auth: LocalaiAuthValue): string {+ return auth.props.base_url.trim().replace(/\/+$/, '');+}++function accessToken(auth: LocalaiAuthValue): string {+ return auth.props.access_token?.trim() ?? '';+}++function client(auth: LocalaiAuthValue): OpenAI {+ return new OpenAI({+ baseURL: baseUrl(auth),+ apiKey: accessToken(auth),+ timeout: REQUEST_TIMEOUT_MS,+ maxRetries: 1,+ });+}++function authHeaders(auth: LocalaiAuthValue): Record<string, string> {+ const token = accessToken(auth);+ return token ? { Authorization: `Bearer ${token}` } : {};+}++function friendlyError(error: unknown): Error {+ if (error instanceof OpenAI.APIError) {+ return describeFailure({ status: error.status, detail: error.message });+ }+ const httpFailure = readHttpFailure(error);+ if (httpFailure) {+ return describeFailure(httpFailure);+ }+ if (error instanceof Error && error.message === 'fetch failed') {+ return describeFailure({ status: undefined, detail: error.message });+ }+ return error instanceof Error ? error : new Error(String(error));+}++function describeFailure({+ status,+ detail,+}: {+ status: number | undefined;+ detail: string;+}): Error {+ if (status === 401 || status === 403) {+ return new Error(+ `LocalAI rejected the access token (${status}). Check the Access Token on the connection matches one of the server's API keys. ${detail}`+ );+ }+ if (status === 404) {+ return new Error(+ `LocalAI returned 404. Check the Server URL and that the model is installed on the server. ${detail}`+ );+ }+ if (status === undefined) {+ return new Error(+ `Could not reach the LocalAI server. Check the Server URL and that the server is running and reachable from Activepieces. ${detail}`+ );+ }+ return new Error(`LocalAI error ${status}: ${detail}`);+}++function readHttpFailure(+ error: unknown+): { status: number; detail: string } | null {+ if (typeof error !== 'object' || error === null || !('response' in error)) {+ return null;+ }+ const response = error.response;+ if (+ typeof response !== 'object' ||+ response === null ||+ !('status' in response) ||+ typeof response.status !== 'number'+ ) {+ return null;+ }+ const body = 'body' in response ? response.body : undefined;+ return { status: response.status, detail: extractMessage(body) };+}++function extractMessage(body: unknown): string {+ const text =+ body instanceof ArrayBuffer+ ? Buffer.from(body).toString('utf8')+ : Buffer.isBuffer(body)+ ? body.toString('utf8')+ : typeof body === 'string'+ ? body+ : JSON.stringify(body ?? '');+ try {+ const parsed: unknown = JSON.parse(text);+ if (+ typeof parsed === 'object' &&+ parsed !== null &&+ 'error' in parsed &&+ typeof parsed.error === 'object' &&+ parsed.error !== null &&+ 'message' in parsed.error &&+ typeof parsed.error.message === 'string'+ ) {+ return parsed.error.message;+ }+ } catch {+ return text.slice(0, 500);+ }+ return text.slice(0, 500);+}++function modelDropdown({+ displayName,+ description,+}: {+ displayName: string;+ description: string;+}) {+ return Property.Dropdown({+ auth: localaiAuth,+ displayName,+ description,+ required: true,+ refreshers: [],+ options: async ({ auth }) => {+ if (!auth) {+ return {+ disabled: true,+ placeholder: 'Connect your LocalAI server first',+ options: [],+ };+ }+ try {+ const response = await client(auth).models.list();+ return {+ disabled: false,+ options: response.data.map((model) => ({+ label: model.id,+ value: model.id,+ })),+ };+ } catch {+ return {+ disabled: true,+ options: [],+ placeholder:+ "Couldn't load models. Check the Server URL and Access Token.",+ };+ }+ },+ });+}++const REQUEST_TIMEOUT_MS = 240_000;++export const localaiCommon = {+ requestTimeoutMs: REQUEST_TIMEOUT_MS,+ baseUrl,+ client,+ authHeaders,+ friendlyError,+ modelDropdown,+};diff --git a/packages/pieces/community/localai/src/lib/output-schemas.ts b/packages/pieces/community/localai/src/lib/output-schemas.tsnew file mode 100644index 00000000000..dd3cfa721a4--- /dev/null+++ b/packages/pieces/community/localai/src/lib/output-schemas.ts@@ -0,0 +1,49 @@+import { OutputSchema } from '@activepieces/pieces-framework';++export const listModelsOutputSchema: OutputSchema = {+ fields: [+ { key: 'count', label: 'Count', format: 'number' },+ {+ key: 'models',+ label: 'Models',+ labelKey: 'id',+ listItems: [{ key: 'id', label: 'Model ID' }],+ },+ ],+};++export const createEmbeddingOutputSchema: OutputSchema = {+ fields: [+ { key: 'model', label: 'Model' },+ { key: 'dimensions', label: 'Dimensions', format: 'number' },+ { key: 'embedding', label: 'Embedding' },+ ],+};++export const textToSpeechOutputSchema: OutputSchema = {+ fields: [+ { key: 'file', label: 'Audio File', format: 'url' },+ { key: 'file_name', label: 'File Name' },+ { key: 'format', label: 'Format' },+ { key: 'size_bytes', label: 'Size', format: 'filesize' },+ { key: 'model', label: 'Model' },+ ],+};++export const transcribeAudioOutputSchema: OutputSchema = {+ fields: [+ { key: 'text', label: 'Text' },+ { key: 'duration', label: 'Duration (Seconds)', format: 'number' },+ {+ key: 'segments',+ label: 'Segments',+ labelKey: 'text',+ listItems: [+ { key: 'id', label: 'ID', format: 'number' },+ { key: 'start', label: 'Start (Seconds)', format: 'number' },+ { key: 'end', label: 'End (Seconds)', format: 'number' },+ { key: 'text', label: 'Text' },+ ],+ },+ ],+};