The Open Framework for Building Agents
refactor(eve): share non-empty channel string parsing (#1489)
vercel-gh-bot-2[bot]8c125d5358d2
diff --git a/packages/eve/src/public/channels/discord/discordChannel.ts b/packages/eve/src/public/channels/discord/discordChannel.tsindex da5392438..6a110c916 100644--- a/packages/eve/src/public/channels/discord/discordChannel.ts+++ b/packages/eve/src/public/channels/discord/discordChannel.ts@@ -47,6 +47,7 @@ import {} from "#public/channels/discord/responses.js";import { type DiscordWebhookVerifier } from "#public/channels/discord/verify.js";import { verifyDiscordInbound } from "#public/channels/discord/verifyInbound.js";+import { readNonEmptyString } from "#shared/guards.js";import { parseJsonObject, type JsonObject } from "#shared/json.js";import { defineChannel, POST, type Channel, type SendFn } from "#public/definitions/channel.js";@@ -260,11 +261,11 @@ export function discordChannel(config: DiscordChannelConfig = {}): DiscordChanneasync receive(input, { send }) {const receiveTarget = input.target as Partial<DiscordReceiveTarget>;- const channelId = readString(receiveTarget.channelId);+ const channelId = readNonEmptyString(receiveTarget.channelId);if (!channelId) {throw new Error("discordChannel().receive requires target.channelId.");}- const requestedConversationId = readString(receiveTarget.conversationId);+ const requestedConversationId = readNonEmptyString(receiveTarget.conversationId);const initialMessage = receiveTarget.initialMessage;if (initialMessage !== undefined && requestedConversationId !== undefined) {throw new Error(@@ -701,7 +702,3 @@ function expandPostBodies(body: DiscordMessageBody): readonly DiscordMessageBody};});}--function readString(value: unknown): string | undefined {- return typeof value === "string" && value.length > 0 ? value : undefined;-}diff --git a/packages/eve/src/public/channels/github/githubChannel.ts b/packages/eve/src/public/channels/github/githubChannel.tsindex 06c7bbec7..e2d91fed7 100644--- a/packages/eve/src/public/channels/github/githubChannel.ts+++ b/packages/eve/src/public/channels/github/githubChannel.ts@@ -47,6 +47,7 @@ import {import type { GitHubPullRequestContextConfig } from "#public/channels/github/pr-context.js";import { verifyGitHubRequest } from "#public/channels/github/verify.js";import { defineChannel, POST, type Channel } from "#public/definitions/channel.js";+import { readNonEmptyString } from "#shared/guards.js";const log = createLogger("github.channel");@@ -453,10 +454,6 @@ async function verifyInbound(}}-function readNonEmptyString(value: unknown): string | undefined {- return typeof value === "string" && value.length > 0 ? value : undefined;-}-function missingGitHubWebhookHeaders(headers: Headers): readonly string[] {return ["x-github-event", "x-github-delivery"].filter((name) => {const value = headers.get(name);diff --git a/packages/eve/src/public/channels/linear/inbound.ts b/packages/eve/src/public/channels/linear/inbound.tsindex 2de103542..1afddf9f1 100644--- a/packages/eve/src/public/channels/linear/inbound.ts+++ b/packages/eve/src/public/channels/linear/inbound.ts@@ -1,6 +1,6 @@import type { UserContent } from "ai";-import { isObject } from "#shared/guards.js";+import { isObject, readNonEmptyString } from "#shared/guards.js";import { parseJsonObject, type JsonObject } from "#shared/json.js";/** Linear Agent Session webhook actions supported by the channel. */@@ -102,34 +102,34 @@ export function parseLinearWebhookEvent(input: {event: input.headers.get("linear-event") ?? undefined,id: input.headers.get("linear-delivery") ?? undefined,};- const type = readString(raw.type);+ const type = readNonEmptyString(raw.type);if (!type) return null;if (type === "AgentSessionEvent") {const agentSession = readAgentSession(raw.agentSession);if (agentSession === null) return null;return {- action: readString(raw.action) ?? "created",+ action: readNonEmptyString(raw.action) ?? "created",agentActivity: readAgentActivity(raw.agentActivity),agentSession,- appUserId: readString(raw.appUserId),- createdAt: readString(raw.createdAt),+ appUserId: readNonEmptyString(raw.appUserId),+ createdAt: readNonEmptyString(raw.createdAt),delivery,- guidance: readString(raw.guidance),+ guidance: readNonEmptyString(raw.guidance),kind: "agent_session",- oauthClientId: readString(raw.oauthClientId),- organizationId: readString(raw.organizationId),+ oauthClientId: readNonEmptyString(raw.oauthClientId),+ organizationId: readNonEmptyString(raw.organizationId),previousComments: readPreviousComments(raw.previousComments),- promptContext: readString(raw.promptContext),+ promptContext: readNonEmptyString(raw.promptContext),raw,};}return {- action: readString(raw.action),+ action: readNonEmptyString(raw.action),delivery,kind: "data",- organizationId: readString(raw.organizationId),+ organizationId: readNonEmptyString(raw.organizationId),raw,type,};@@ -225,7 +225,7 @@ function readAgentActivity(value: unknown): LinearAgentActivityRef | undefined {function readActivityBody(content: unknown): string | undefined {if (!isObject(content)) return undefined;- return readString(content.body);+ return readNonEmptyString(content.body);}function readIssue(value: unknown): LinearIssueRef | null | undefined {@@ -258,7 +258,3 @@ function readPreviousComments(value: unknown): readonly string[] {}).filter((entry): entry is string => entry !== null);}--function readString(value: unknown): string | undefined {- return typeof value === "string" && value.length > 0 ? value : undefined;-}diff --git a/packages/eve/src/public/channels/linear/linearChannel.ts b/packages/eve/src/public/channels/linear/linearChannel.tsindex 0ba6e6c7f..81dcf31dd 100644--- a/packages/eve/src/public/channels/linear/linearChannel.ts+++ b/packages/eve/src/public/channels/linear/linearChannel.ts@@ -34,7 +34,7 @@ import {type ChannelSessionOps,type SendFn,} from "#public/definitions/channel.js";-import { isObject } from "#shared/guards.js";+import { isObject, readNonEmptyString } from "#shared/guards.js";import type { JsonObject } from "#shared/json.js";const log = createLogger("linear.channel");@@ -256,7 +256,7 @@ export function linearChannel(config: LinearChannelConfig = {}): LinearChannel {const target = input.target as Record<string, unknown>;const session = await resolveReceiveSession(target, config);- const initialActivity = readString(target.initialActivity);+ const initialActivity = readNonEmptyString(target.initialActivity);if (initialActivity !== undefined) {await createLinearAgentActivity({api: config.api,@@ -377,7 +377,7 @@ async function resolveReceiveSession(return createLinearAgentSessionOnIssue({api: config.api,credentials: config.credentials,- externalLink: readString(target.externalLink),+ externalLink: readNonEmptyString(target.externalLink),externalUrls: readExternalUrls(target.externalUrls),issueId: target.issueId,});@@ -387,7 +387,7 @@ async function resolveReceiveSession(api: config.api,credentials: config.credentials,commentId: target.commentId,- externalLink: readString(target.externalLink),+ externalLink: readNonEmptyString(target.externalLink),externalUrls: readExternalUrls(target.externalUrls),});}@@ -449,10 +449,6 @@ function hasString<T extends string>(return typeof value[key] === "string" && value[key].length > 0;}-function readString(value: unknown): string | undefined {- return typeof value === "string" && value.length > 0 ? value : undefined;-}-function readExternalUrls(value: unknown,): readonly { readonly label: string; readonly url: string }[] | undefined {diff --git a/packages/eve/src/public/channels/teams/teamsChannel.ts b/packages/eve/src/public/channels/teams/teamsChannel.tsindex 9abcc8eb4..c79367ba2 100644--- a/packages/eve/src/public/channels/teams/teamsChannel.ts+++ b/packages/eve/src/public/channels/teams/teamsChannel.ts@@ -54,6 +54,7 @@ import {type TeamsMessageActivity,} from "#public/channels/teams/inbound.js";import { verifyTeamsRequest, type TeamsWebhookVerifier } from "#public/channels/teams/verify.js";+import { readNonEmptyString } from "#shared/guards.js";import { parseJsonObject, type JsonObject } from "#shared/json.js";import { defineChannel, POST, type Channel, type SendFn } from "#public/definitions/channel.js";@@ -343,16 +344,16 @@ export function teamsChannel(config: TeamsChannelConfig = {}): TeamsChannel {async receive(input, { send }) {const receiveTarget = input.target as Partial<TeamsReceiveTarget>;- const serviceUrl = readString(receiveTarget.serviceUrl);- const conversationId = readString(receiveTarget.conversationId);+ const serviceUrl = readNonEmptyString(receiveTarget.serviceUrl);+ const conversationId = readNonEmptyString(receiveTarget.conversationId);if (!serviceUrl || !conversationId) {throw new Error("teamsChannel().receive requires target.serviceUrl and target.conversationId.",);}- const conversationType = readString(receiveTarget.conversationType) ?? null;- let replyToActivityId = readString(receiveTarget.replyToActivityId) ?? null;+ const conversationType = readNonEmptyString(receiveTarget.conversationType) ?? null;+ let replyToActivityId = readNonEmptyString(receiveTarget.replyToActivityId) ?? null;const initialMessage = receiveTarget.initialMessage;if (initialMessage !== undefined && replyToActivityId !== null) {throw new Error(@@ -362,13 +363,13 @@ export function teamsChannel(config: TeamsChannelConfig = {}): TeamsChannel {const state: TeamsChannelState = {...initialTeamsState(),- channelId: readString(receiveTarget.channelId) ?? null,+ channelId: readNonEmptyString(receiveTarget.channelId) ?? null,conversationId,conversationType,replyToActivityId,serviceUrl,- teamId: readString(receiveTarget.teamId) ?? null,- tenantId: readString(receiveTarget.tenantId) ?? null,+ teamId: readNonEmptyString(receiveTarget.teamId) ?? null,+ tenantId: readNonEmptyString(receiveTarget.tenantId) ?? null,};if (initialMessage !== undefined) {@@ -786,7 +787,3 @@ function mergeChannelData(function teamsOk(): Response {return new Response("ok", { status: 200 });}--function readString(value: unknown): string | undefined {- return typeof value === "string" && value.length > 0 ? value : undefined;-}diff --git a/packages/eve/src/public/channels/twilio/twilioChannel.ts b/packages/eve/src/public/channels/twilio/twilioChannel.tsindex 41b4278a3..dac1dedbc 100644--- a/packages/eve/src/public/channels/twilio/twilioChannel.ts+++ b/packages/eve/src/public/channels/twilio/twilioChannel.ts@@ -42,6 +42,7 @@ import {type TwilioRoutes,} from "#public/channels/twilio/routing.js";import { type TwilioAuthToken, type TwilioWebhookUrl } from "#public/channels/twilio/verify.js";+import { readNonEmptyString } from "#shared/guards.js";import {defineChannel,GET,@@ -328,11 +329,11 @@ export function twilioChannel(config: TwilioChannelConfig): TwilioChannel {],async receive(input, { send }) {- const phoneNumber = readString(input.target.phoneNumber);+ const phoneNumber = readNonEmptyString(input.target.phoneNumber);if (!phoneNumber) {throw new Error("twilioChannel().receive requires target.phoneNumber.");}- const from = readString(input.target.from) ?? config.messaging?.from ?? null;+ const from = readNonEmptyString(input.target.from) ?? config.messaging?.from ?? null;return send(input.message, {auth: input.auth,continuationToken: twilioContinuationToken(phoneNumber, from ?? undefined),@@ -661,7 +662,3 @@ async function isAllowed(from: string, allowFrom: TwilioAllowFrom): Promise<boolif (resolved === "*") return true;return typeof resolved === "string" ? resolved === from : resolved.includes(from);}--function readString(value: unknown): string | undefined {- return typeof value === "string" && value.length > 0 ? value : undefined;-}diff --git a/packages/eve/src/shared/guards.test.ts b/packages/eve/src/shared/guards.test.tsindex cdcda8fda..4faa3ec9f 100644--- a/packages/eve/src/shared/guards.test.ts+++ b/packages/eve/src/shared/guards.test.ts@@ -1,6 +1,12 @@import { describe, expect, it } from "vitest";-import { isNonEmptyString, isObject, isPlainRecord, isThenable } from "#shared/guards.js";+import {+ isNonEmptyString,+ isObject,+ isPlainRecord,+ isThenable,+ readNonEmptyString,+} from "#shared/guards.js";describe("isObject", () => {it("accepts plain objects", () => {@@ -59,6 +65,7 @@ describe("isNonEmptyString", () => {expect(isNonEmptyString("a")).toBe(true);expect(isNonEmptyString(" ")).toBe(true);expect(isNonEmptyString("multi word")).toBe(true);+ expect(readNonEmptyString("value")).toBe("value");});it("rejects empty strings, non-strings, and nullish values", () => {@@ -67,5 +74,6 @@ describe("isNonEmptyString", () => {expect(isNonEmptyString(undefined)).toBe(false);expect(isNonEmptyString(0)).toBe(false);expect(isNonEmptyString(["a"])).toBe(false);+ expect(readNonEmptyString("")).toBeUndefined();});});diff --git a/packages/eve/src/shared/guards.ts b/packages/eve/src/shared/guards.tsindex f779c0c09..711290ccb 100644--- a/packages/eve/src/shared/guards.ts+++ b/packages/eve/src/shared/guards.ts@@ -36,6 +36,11 @@ export function isNonEmptyString(value: unknown): value is string {return typeof value === "string" && value.length > 0;}+/** Returns a non-empty string or `undefined` for any other value. */+export function readNonEmptyString(value: unknown): string | undefined {+ return isNonEmptyString(value) ? value : undefined;+}+/*** Returns `true` when `value` is a thenable — an object with a `then`* function. Used to reject async instrumentation metadata projectors