From 315a13290a056e6b712edf537e43d739562c1edf Mon Sep 17 00:00:00 2001 From: KacperKoza343 Date: Tue, 21 Jan 2025 11:47:26 +0100 Subject: [PATCH] refactor: improve code accroding best practises --- .env.example | 6 +- packages/client-linkedin/package.json | 7 +- .../src/helpers/get-random-integer.ts | 14 +++ .../src/helpers/get-random-number.ts | 3 - .../helpers/prepare-axios-error-message.ts | 14 +++ .../src/helpers/validate-config.ts | 86 +++++++++++++------ packages/client-linkedin/src/index.ts | 14 ++- packages/client-linkedin/src/interfaces.ts | 43 ++++++---- .../src/repositories/LinkedinFileUploader.ts | 65 ++++++++++++++ .../src/repositories/LinkedinPostPublisher.ts | 54 ++++++++++++ .../repositories/LinkedinUserInfoFetcher.ts | 20 +++++ ...stsManager.ts => LinkedInPostScheduler.ts} | 45 ++++++---- .../src/services/LinkedinFileUploader.ts | 40 --------- .../src/services/LinkedinPostPublisher.ts | 40 --------- .../src/services/LinkedinUserInfoFetcher.ts | 11 --- .../src/services/PostContentCreator.ts | 35 ++++---- 16 files changed, 315 insertions(+), 182 deletions(-) create mode 100644 packages/client-linkedin/src/helpers/get-random-integer.ts delete mode 100644 packages/client-linkedin/src/helpers/get-random-number.ts create mode 100644 packages/client-linkedin/src/helpers/prepare-axios-error-message.ts create mode 100644 packages/client-linkedin/src/repositories/LinkedinFileUploader.ts create mode 100644 packages/client-linkedin/src/repositories/LinkedinPostPublisher.ts create mode 100644 packages/client-linkedin/src/repositories/LinkedinUserInfoFetcher.ts rename packages/client-linkedin/src/services/{PostsManager.ts => LinkedInPostScheduler.ts} (58%) delete mode 100644 packages/client-linkedin/src/services/LinkedinFileUploader.ts delete mode 100644 packages/client-linkedin/src/services/LinkedinPostPublisher.ts delete mode 100644 packages/client-linkedin/src/services/LinkedinUserInfoFetcher.ts diff --git a/.env.example b/.env.example index c640fd5f551..4f7548b164d 100644 --- a/.env.example +++ b/.env.example @@ -619,6 +619,6 @@ INSTAGRAM_MAX_ACTIONS=1 # Maximum number of actions to process at once # LinkedIn Configuration LINKEDIN_ACCESS_TOKEN= # LinkedIn access token -LINKEDIN_POST_INTERVAL_MIN=1 # Default: 60 minutes -LINKEDIN_POST_INTERVAL_MAX=2 # Default: 120 minutes - +LINKEDIN_POST_INTERVAL_MIN=60 # Optional, default: 60 minutes +LINKEDIN_POST_INTERVAL_MAX=120 # Optional, default: 120 minutes +LINKEDIN_API_URL=https://api.linkedin.com # Optional, default: https://api.linkedin.com diff --git a/packages/client-linkedin/package.json b/packages/client-linkedin/package.json index 0c801d1748c..5f3c3d6fe6e 100644 --- a/packages/client-linkedin/package.json +++ b/packages/client-linkedin/package.json @@ -19,12 +19,13 @@ "dist" ], "dependencies": { - "@elizaos/core": "workspace:*" + "@elizaos/core": "workspace:*", + "axios": "^1.7.9" }, "devDependencies": { + "@vitest/coverage-v8": "1.1.3", "tsup": "8.3.5", - "vitest": "1.1.3", - "@vitest/coverage-v8": "1.1.3" + "vitest": "1.1.3" }, "scripts": { "build": "tsup --format esm --dts", diff --git a/packages/client-linkedin/src/helpers/get-random-integer.ts b/packages/client-linkedin/src/helpers/get-random-integer.ts new file mode 100644 index 00000000000..629272cdfba --- /dev/null +++ b/packages/client-linkedin/src/helpers/get-random-integer.ts @@ -0,0 +1,14 @@ +export const getRandomInteger = (min: number, max: number) => { + if (Number.isNaN(min) || Number.isNaN(max)) { + throw new Error("Invalid range: min and max must be valid numbers"); + } + + if (min > max) { + throw new Error("Min value cannot be greater than max value"); + } + + const lower = Math.floor(min); + const upper = Math.floor(max); + + return Math.floor(Math.random() * (upper - lower + 1)) + lower; +}; diff --git a/packages/client-linkedin/src/helpers/get-random-number.ts b/packages/client-linkedin/src/helpers/get-random-number.ts deleted file mode 100644 index a8895d3511c..00000000000 --- a/packages/client-linkedin/src/helpers/get-random-number.ts +++ /dev/null @@ -1,3 +0,0 @@ -export const getRandomNumber = (min: number, max: number) => { - return Math.floor(Math.random() * (max - min + 1)) + min; -} diff --git a/packages/client-linkedin/src/helpers/prepare-axios-error-message.ts b/packages/client-linkedin/src/helpers/prepare-axios-error-message.ts new file mode 100644 index 00000000000..bcf7131599d --- /dev/null +++ b/packages/client-linkedin/src/helpers/prepare-axios-error-message.ts @@ -0,0 +1,14 @@ +import { AxiosError } from "axios"; + +export const prepareAxiosErrorMessage = (error: AxiosError) => { + return JSON.stringify( + { + message: error.message, + status: error.response?.status, + data: error.response?.data, + code: error.code, + }, + null, + 2 + ); +}; diff --git a/packages/client-linkedin/src/helpers/validate-config.ts b/packages/client-linkedin/src/helpers/validate-config.ts index b064a95884a..e89a078e788 100644 --- a/packages/client-linkedin/src/helpers/validate-config.ts +++ b/packages/client-linkedin/src/helpers/validate-config.ts @@ -1,10 +1,18 @@ import { z, ZodError } from "zod"; import { IAgentRuntime } from "@elizaos/core"; -const checkIfIsNumber = (val: string | number | null, ctx: z.RefinementCtx, path: string) => { +const DEFAULT_LINKEDIN_API_URL = "https://api.linkedin.com"; +const DEFAULT_LINKEDIN_POST_INTERVAL_MIN = 60; +const DEFAULT_LINKEDIN_POST_INTERVAL_MAX = 120; + +const parseNumber = ( + val: string | number | null, + ctx: z.RefinementCtx, + path: string +) => { const num = Number(val); - if(Number.isNaN(num)) { + if (Number.isNaN(num)) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: `Invalid number: ${val}`, @@ -13,43 +21,69 @@ const checkIfIsNumber = (val: string | number | null, ctx: z.RefinementCtx, path } return num; -} - -export const configSchema = z.object({ - LINKEDIN_ACCESS_TOKEN: z.string(), - LINKEDIN_POST_INTERVAL_MIN: z.union([z.number(), z.string()]) - .nullable() - .optional() - .default(60) - .transform((val, ctx) => checkIfIsNumber(val, ctx, 'LINKEDIN_POST_INTERVAL_MIN')), - LINKEDIN_POST_INTERVAL_MAX: z.union([z.number(), z.string()]) - .nullable() - .optional() - .default(120) - .transform((val, ctx) => checkIfIsNumber(val, ctx, 'LINKEDIN_POST_INTERVAL_MAX')), -}); +}; + +export const configSchema = z + .object({ + LINKEDIN_ACCESS_TOKEN: z.string(), + LINKEDIN_POST_INTERVAL_MIN: z + .union([z.number(), z.string(), z.null(), z.undefined()]) + .transform((val, ctx) => { + if (val === null || val === undefined) { + return DEFAULT_LINKEDIN_POST_INTERVAL_MIN; + } + return parseNumber(val, ctx, "LINKEDIN_POST_INTERVAL_MIN"); + }), + LINKEDIN_POST_INTERVAL_MAX: z + .union([z.number(), z.string(), z.null(), z.undefined()]) + .transform((val, ctx) => { + if (val === null || val === undefined) { + return DEFAULT_LINKEDIN_POST_INTERVAL_MAX; + } + return parseNumber(val, ctx, "LINKEDIN_POST_INTERVAL_MAX"); + }), + LINKEDIN_API_URL: z + .union([z.string(), z.null(), z.undefined()]) + .transform((val) => val ?? DEFAULT_LINKEDIN_API_URL), + }) + .superRefine((data, ctx) => { + if (data.LINKEDIN_POST_INTERVAL_MIN > data.LINKEDIN_POST_INTERVAL_MAX) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: "Min value cannot be greater than max value", + path: ["LINKEDIN_POST_INTERVAL_MIN"], + }); + } + }); export const validateConfig = (runtime: IAgentRuntime) => { const LINKEDIN_ACCESS_TOKEN = runtime.getSetting("LINKEDIN_ACCESS_TOKEN"); - const LINKEDIN_POST_INTERVAL_MIN = runtime.getSetting('LINKEDIN_POST_INTERVAL_MIN'); - const LINKEDIN_POST_INTERVAL_MAX = runtime.getSetting("LINKEDIN_POST_INTERVAL_MAX"); + const LINKEDIN_POST_INTERVAL_MIN = runtime.getSetting( + "LINKEDIN_POST_INTERVAL_MIN" + ); + const LINKEDIN_POST_INTERVAL_MAX = runtime.getSetting( + "LINKEDIN_POST_INTERVAL_MAX" + ); + const LINKEDIN_API_URL = runtime.getSetting("LINKEDIN_API_URL"); try { const envs = configSchema.parse({ LINKEDIN_ACCESS_TOKEN, LINKEDIN_POST_INTERVAL_MIN, LINKEDIN_POST_INTERVAL_MAX, + LINKEDIN_API_URL, }); return envs; } catch (error) { - if(error instanceof ZodError) { - throw new Error(`Invalid environment variables. Validating envs failed with error: ${ - error.issues.map(issue => issue.path.join('.') + ': ' + issue.message).join(' , ') - }`); + if (error instanceof ZodError) { + throw new Error( + `Invalid environment variables. Validating envs failed with error: ${error.issues + .map((issue) => issue.path.join(".") + ": " + issue.message) + .join(" , ")}` + ); } else { - throw new Error('Invalid environment variables.'); + throw new Error("Invalid environment variables."); } - } -} +}; diff --git a/packages/client-linkedin/src/index.ts b/packages/client-linkedin/src/index.ts index d0c750e2cb8..0b68c5e0e64 100644 --- a/packages/client-linkedin/src/index.ts +++ b/packages/client-linkedin/src/index.ts @@ -1,32 +1,30 @@ import { Client, elizaLogger, IAgentRuntime } from "@elizaos/core"; import { validateConfig } from "./helpers/validate-config"; import axios from "axios"; -import { LinkedInUserInfoFetcher } from "./services/LinkedinUserInfoFetcher"; -import { PostsManager } from "./services/PostsManager"; - -const LINKEDIN_API_URL = "https://api.linkedin.com"; +import { LinkedInUserInfoFetcher } from "./repositories/LinkedinUserInfoFetcher"; +import { LinkedInPostScheduler } from "./services/LinkedInPostScheduler"; export const LinkedInClient: Client = { async start(runtime: IAgentRuntime) { const envs = validateConfig(runtime); const axiosInstance = axios.create({ - baseURL: LINKEDIN_API_URL, + baseURL: envs.LINKEDIN_API_URL, headers: { "Authorization": `Bearer ${envs.LINKEDIN_ACCESS_TOKEN}`, }, }); - const postManager = await PostsManager.create({ + const linkedInPostScheduler = await LinkedInPostScheduler.createPostScheduler({ axiosInstance, userInfoFetcher: new LinkedInUserInfoFetcher(axiosInstance), - runtime: this.runtime, + runtime, config: { LINKEDIN_POST_INTERVAL_MIN: envs.LINKEDIN_POST_INTERVAL_MIN, LINKEDIN_POST_INTERVAL_MAX: envs.LINKEDIN_POST_INTERVAL_MAX, } }); - postManager.createPostPublicationLoop(); + linkedInPostScheduler.createPostPublicationLoop(); return this; diff --git a/packages/client-linkedin/src/interfaces.ts b/packages/client-linkedin/src/interfaces.ts index 278561f40d8..b64b6ced1b3 100644 --- a/packages/client-linkedin/src/interfaces.ts +++ b/packages/client-linkedin/src/interfaces.ts @@ -2,23 +2,23 @@ import { configSchema } from "./helpers/validate-config"; import { z } from "zod"; export interface UserInfo { - sub: string; - email_verified: boolean; - name: string; - locale: { country: string; language: string }; - given_name: string; - family_name: string, - email: string, - picture?: string; -}; + sub: string; + email_verified: boolean; + name: string; + locale: { country: string; language: string }; + given_name: string; + family_name: string; + email: string; + picture?: string; +} export interface MediaUploadUrl { - value: { - uploadUrlExpiresAt: number, - uploadUrl: string, - image: string - } -}; + value: { + uploadUrlExpiresAt: number; + uploadUrl: string; + image: string; + }; +} export interface BasePostRequest { author: string; @@ -33,6 +33,14 @@ export interface BasePostRequest { isReshareDisabledByAuthor: boolean; } +export interface PublishPostParams { + postText: string; + media?: { + title: string; + id: string; + }; +} + export interface PostRequestWithMedia extends BasePostRequest { content?: { media: { @@ -45,4 +53,7 @@ export interface PostRequestWithMedia extends BasePostRequest { export const API_VERSION_HEADER = "LinkedIn-Version"; export const API_VERSION = "202411"; export type Envs = z.infer; -export type IntervalsConfig = Omit; +export type IntervalsConfig = Pick< + Envs, + "LINKEDIN_POST_INTERVAL_MAX" | "LINKEDIN_POST_INTERVAL_MIN" +>; diff --git a/packages/client-linkedin/src/repositories/LinkedinFileUploader.ts b/packages/client-linkedin/src/repositories/LinkedinFileUploader.ts new file mode 100644 index 00000000000..63b69a97117 --- /dev/null +++ b/packages/client-linkedin/src/repositories/LinkedinFileUploader.ts @@ -0,0 +1,65 @@ +import { AxiosInstance, AxiosError } from "axios"; +import { API_VERSION, API_VERSION_HEADER, MediaUploadUrl } from "../interfaces"; +import { prepareAxiosErrorMessage } from "../helpers/prepare-axios-error-message"; + +export class LinkedInFileUploader { + constructor( + private readonly axios: AxiosInstance, + readonly userId: string + ) {} + + async uploadAsset(imageBlob: Blob) { + const { uploadUrl, imageId } = await this.createMediaUploadUrl(); + await this.uploadMedia(uploadUrl, imageBlob); + + return imageId; + } + + async createMediaUploadUrl() { + try { + const initResponse = await this.axios.post( + "/rest/images", + { + initializeUploadRequest: { + owner: `urn:li:person:${this.userId}`, + }, + }, + { + headers: { + [API_VERSION_HEADER]: [API_VERSION], + }, + params: { + action: "initializeUpload", + }, + } + ); + + return { + uploadUrl: initResponse.data.value.uploadUrl, + imageId: initResponse.data.value.image, + }; + } catch (error) { + const isAxiosError = error instanceof AxiosError; + + throw new Error( + `Failed create media upload url: ${isAxiosError ? prepareAxiosErrorMessage(error) : error}` + ); + } + } + + async uploadMedia(uploadUrl: string, imageBlob: Blob) { + try { + await this.axios.put(uploadUrl, imageBlob, { + headers: { + "Content-Type": "application/octet-stream", + }, + }); + } catch (error) { + const isAxiosError = error instanceof AxiosError; + + throw new Error( + `Failed to upload media: ${isAxiosError ? prepareAxiosErrorMessage(error) : error}` + ); + } + } +} diff --git a/packages/client-linkedin/src/repositories/LinkedinPostPublisher.ts b/packages/client-linkedin/src/repositories/LinkedinPostPublisher.ts new file mode 100644 index 00000000000..48d03c9fa59 --- /dev/null +++ b/packages/client-linkedin/src/repositories/LinkedinPostPublisher.ts @@ -0,0 +1,54 @@ +import { AxiosInstance, AxiosError } from "axios"; +import { + BasePostRequest, + PostRequestWithMedia, + API_VERSION_HEADER, + API_VERSION, + PublishPostParams, +} from "../interfaces"; +import { prepareAxiosErrorMessage } from "../helpers/prepare-axios-error-message"; + +export class LinkedInPostPublisher { + constructor( + private readonly axios: AxiosInstance, + readonly userId: string + ) {} + + async publishPost({ postText, media }: PublishPostParams) { + const requestBody = this.buildPostRequest({ postText, media }); + + try { + await this.axios.post("/rest/posts", requestBody, { + headers: { + [API_VERSION_HEADER]: [API_VERSION], + }, + }); + } catch (error) { + const isAxiosError = error instanceof AxiosError; + + throw new Error( + `Failed to publish LinkedIn post: ${isAxiosError ? prepareAxiosErrorMessage(error) : error}` + ); + } + } + + private buildPostRequest({ + postText, + media, + }: PublishPostParams): BasePostRequest | PostRequestWithMedia { + const baseRequest: BasePostRequest = { + author: `urn:li:person:${this.userId}`, + commentary: postText, + visibility: "PUBLIC", + distribution: { + feedDistribution: "MAIN_FEED", + targetEntities: [], + thirdPartyDistributionChannels: [], + }, + lifecycleState: "PUBLISHED", + isReshareDisabledByAuthor: false, + }; + + return media ? { ...baseRequest, content: { media } } : baseRequest; + } +} diff --git a/packages/client-linkedin/src/repositories/LinkedinUserInfoFetcher.ts b/packages/client-linkedin/src/repositories/LinkedinUserInfoFetcher.ts new file mode 100644 index 00000000000..1bd93675e2a --- /dev/null +++ b/packages/client-linkedin/src/repositories/LinkedinUserInfoFetcher.ts @@ -0,0 +1,20 @@ +import { AxiosInstance, AxiosError } from "axios"; +import { UserInfo } from "../interfaces"; +import { prepareAxiosErrorMessage } from "../helpers/prepare-axios-error-message"; + +export class LinkedInUserInfoFetcher { + constructor(private readonly axios: AxiosInstance) {} + + async getUserInfo() { + try { + const response = await this.axios.get("/v2/userinfo"); + return response.data; + } catch (error) { + const isAxiosError = error instanceof AxiosError; + + throw new Error( + `Failed to fetch user info: ${isAxiosError ? prepareAxiosErrorMessage(error) : error}` + ); + } + } +} diff --git a/packages/client-linkedin/src/services/PostsManager.ts b/packages/client-linkedin/src/services/LinkedInPostScheduler.ts similarity index 58% rename from packages/client-linkedin/src/services/PostsManager.ts rename to packages/client-linkedin/src/services/LinkedInPostScheduler.ts index 1ea65789cd3..365dd313a15 100644 --- a/packages/client-linkedin/src/services/PostsManager.ts +++ b/packages/client-linkedin/src/services/LinkedInPostScheduler.ts @@ -1,12 +1,12 @@ import { AxiosInstance } from "axios"; -import { LinkedInUserInfoFetcher } from "./LinkedinUserInfoFetcher"; +import { LinkedInUserInfoFetcher } from "../repositories/LinkedinUserInfoFetcher"; import { PostContentCreator } from "./PostContentCreator"; import { IntervalsConfig } from "../interfaces"; -import { LinkedInPostPublisher } from "./LinkedinPostPublisher"; +import { LinkedInPostPublisher } from "../repositories/LinkedinPostPublisher"; import { elizaLogger, IAgentRuntime } from "@elizaos/core"; -import { getRandomNumber } from "../helpers/get-random-number"; +import { getRandomInteger } from "../helpers/get-random-integer"; -export class PostsManager { +export class LinkedInPostScheduler { constructor( private runtime: IAgentRuntime, private postPublisher: LinkedInPostPublisher, @@ -15,22 +15,32 @@ export class PostsManager { readonly intervalsConfig: IntervalsConfig ) {} - static async create({ + static async createPostScheduler({ axiosInstance, userInfoFetcher, runtime, - config + config, }: { - axiosInstance: AxiosInstance, - userInfoFetcher: LinkedInUserInfoFetcher, - runtime: IAgentRuntime, - config: IntervalsConfig + axiosInstance: AxiosInstance; + userInfoFetcher: LinkedInUserInfoFetcher; + runtime: IAgentRuntime; + config: IntervalsConfig; }) { const userInfo = await userInfoFetcher.getUserInfo(); - const postPublisher = new LinkedInPostPublisher(axiosInstance, userInfo.sub); + const postPublisher = new LinkedInPostPublisher( + axiosInstance, + userInfo.sub + ); const postContentCreator = new PostContentCreator(runtime); - return new PostsManager(runtime, postPublisher, postContentCreator, userInfo.sub, config); + + return new LinkedInPostScheduler( + runtime, + postPublisher, + postContentCreator, + userInfo.sub, + config + ); } async createPostPublicationLoop() { @@ -42,13 +52,18 @@ export class PostsManager { const minMinutes = this.intervalsConfig.LINKEDIN_POST_INTERVAL_MIN; const maxMinutes = this.intervalsConfig.LINKEDIN_POST_INTERVAL_MAX; - const randomMinutes = getRandomNumber(minMinutes, maxMinutes); + const randomMinutes = getRandomInteger(minMinutes, maxMinutes); const delay = randomMinutes * 60 * 1000; if (Date.now() > lastPostTimestamp + delay) { - const postText = await this.postContentCreator.createPostContent(this.userId); + const postText = await this.postContentCreator.createPostContent( + this.userId + ); await this.postPublisher.publishPost({ postText }); - await this.runtime.cacheManager.set("linkedin/" + this.userId + "/lastPost", { timestamp: Date.now() }); + await this.runtime.cacheManager.set( + "linkedin/" + this.userId + "/lastPost", + { timestamp: Date.now() } + ); elizaLogger.info("Published post"); } diff --git a/packages/client-linkedin/src/services/LinkedinFileUploader.ts b/packages/client-linkedin/src/services/LinkedinFileUploader.ts deleted file mode 100644 index 4287697210b..00000000000 --- a/packages/client-linkedin/src/services/LinkedinFileUploader.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { AxiosInstance } from "axios"; -import { API_VERSION, API_VERSION_HEADER, MediaUploadUrl } from "../interfaces"; - -export class LinkedInFileUploader { - constructor(private readonly axios: AxiosInstance, readonly userId: string) {} - - async uploadAsset(imageBlob: Blob) { - const { uploadUrl, imageId } = await this.createMediaUploadUrl(); - await this.uploadMedia(uploadUrl, imageBlob); - - return imageId; - } - - async createMediaUploadUrl() { - const initResponse = await this.axios.post('/rest/images', { - initializeUploadRequest: { - owner: `urn:li:person:${this.userId}` - } - }, { - headers: { - [API_VERSION_HEADER]: [API_VERSION], - }, - params: { - 'action': 'initializeUpload' - } - }); - return { - uploadUrl: initResponse.data.value.uploadUrl, - imageId: initResponse.data.value.image - }; - } - - async uploadMedia(uploadUrl: string, imageBlob: Blob) { - await this.axios.put(uploadUrl, imageBlob, { - headers: { - 'Content-Type': 'application/octet-stream', - } - }); - } -} diff --git a/packages/client-linkedin/src/services/LinkedinPostPublisher.ts b/packages/client-linkedin/src/services/LinkedinPostPublisher.ts deleted file mode 100644 index 6b76ccb24d4..00000000000 --- a/packages/client-linkedin/src/services/LinkedinPostPublisher.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { AxiosInstance } from "axios"; -import { BasePostRequest, PostRequestWithMedia, API_VERSION_HEADER, API_VERSION } from "../interfaces"; - -export class LinkedInPostPublisher { - constructor(private readonly axios: AxiosInstance, readonly userId: string) {} - - async publishPost({ - postText, - media - }: { - postText: string, - media?: { - title: string, - id: string - } - }) { - const baseRequest: BasePostRequest = { - author: `urn:li:person:${this.userId}`, - commentary: postText, - visibility: "PUBLIC", - distribution: { - feedDistribution: "MAIN_FEED", - targetEntities: [], - thirdPartyDistributionChannels: [] - }, - lifecycleState: "PUBLISHED", - isReshareDisabledByAuthor: false - }; - - const requestBody: BasePostRequest | PostRequestWithMedia = media - ? { ...baseRequest, content: { media } } - : baseRequest; - - await this.axios.post("/rest/posts", requestBody, { - headers: { - [API_VERSION_HEADER]: [API_VERSION], - }, - }); - } -} diff --git a/packages/client-linkedin/src/services/LinkedinUserInfoFetcher.ts b/packages/client-linkedin/src/services/LinkedinUserInfoFetcher.ts deleted file mode 100644 index add65f29803..00000000000 --- a/packages/client-linkedin/src/services/LinkedinUserInfoFetcher.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { AxiosInstance } from "axios"; -import { UserInfo } from "../interfaces"; - -export class LinkedInUserInfoFetcher { - constructor(private readonly axios: AxiosInstance) {} - - async getUserInfo() { - const response = await this.axios.get("/v2/userinfo"); - return response.data; - } -} diff --git a/packages/client-linkedin/src/services/PostContentCreator.ts b/packages/client-linkedin/src/services/PostContentCreator.ts index 62302679926..25864b372ae 100644 --- a/packages/client-linkedin/src/services/PostContentCreator.ts +++ b/packages/client-linkedin/src/services/PostContentCreator.ts @@ -1,30 +1,31 @@ -import { composeContext, generateText, IAgentRuntime, ModelClass, stringToUuid } from "@elizaos/core"; +import { + composeContext, + generateText, + IAgentRuntime, + ModelClass, + stringToUuid, +} from "@elizaos/core"; export class PostContentCreator { constructor(public runtime: IAgentRuntime) {} async createPostContent(userId: string) { - const roomId = stringToUuid( - "linkedin_generate_room-" + userId - ); + const roomId = stringToUuid("linkedin_generate_room-" + userId); const topics = this.runtime.character.topics.join(", "); - const state = await this.runtime.composeState( - { - userId: this.runtime.agentId, - roomId: roomId, - agentId: this.runtime.agentId, - content: { - text: topics || "", - action: "LINKEDIN_POST", - }, - } - ); + const state = await this.runtime.composeState({ + userId: this.runtime.agentId, + roomId: roomId, + agentId: this.runtime.agentId, + content: { + text: topics || "", + action: "LINKEDIN_POST", + }, + }); const context = composeContext({ state, - template: - 'post template', + template: "post template", }); return await generateText({