Skip to content

TypeError inextractAgentNameFromSessionTitle when sessionTitle is non-string #57

@de-abreu

Description

@de-abreu

Bug Description

When running opencode with the notifier plugin, opencode fails to launch with the following error:

{
  "name": "UnknownError",
  "data": {
    "message": "TypeError: undefined is not a function (near '...sessionTitle.match(/\\s*\\(@([^\\s)]+)\\s+subagent\\)\\s*$/)...')\n    at extractAgentNameFromSessionTitle (/home/abreu/.cache/opencode/node_modules/@mohak34/opencode-notifier/dist/index.js:849:30)"
  }
}

Root Cause

The extractAgentNameFromSessionTitle function in dist/index.js:845-851 has a type safety bug:

function extractAgentNameFromSessionTitle(sessionTitle) {
  if (!sessionTitle) {
    return "";
  }
  const match = sessionTitle.match(/\s*\(@([^\s)]+)\s+subagent\)\s*$/);
  return match ? match[1] : "";
}

Problem: The guard if (!sessionTitle) only catches falsy values (null, undefined, "", 0, false). If sessionTitle is a truthy non-string value (e.g., anumber, object, or array), it passes the guard, but .match() doesn't exist on non-string types, causing undefined is not a function.

Data flow:

  1. getSessionInfo() calls client.session.get() API
  2. Returns { title: response.data?.title ?? null } (line973)
  3. The API response title might not be a string
  4. This non-string value gets passed to extractAgentNameFromSessionTitle() (line1027)

Suggested Fix

Add a type check before calling .match():

function extractAgentNameFromSessionTitle(sessionTitle) {
  if (!sessionTitle || typeof sessionTitle !== 'string') {
    return "";
  }
  const match = sessionTitle.match(/\s*\(@([^\s)]+)\s+subagent\)\s*$/);
  return match ? match[1] : "";
}

Or convert to string safely:

function extractAgentNameFromSessionTitle(sessionTitle) {
  const title = String(sessionTitle ?? "");
  const match = title.match(/\s*\(@([^\s)]+)\s+subagent\)\s*$/);
  return match ? match[1] : "";
}

##Environment

  • Plugin version: (installed via npm from @mohak34/opencode-notifier)
  • OpenCode version: Latest
  • OS: NixOS Linux

Workaround

Currently removing the plugin from opencode configuration to avoid the crash.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions