LibreChat/packages/api/src/utils
Danny Avila 3d5e5348a4
🧵 fix: Include Code Outputs in Thread File Lookup (#13023)
Code-execution outputs land on `messages.attachments` (set by
`processCodeOutput`), while user uploads land on `messages.files`.
The threadFileIds switch (#13004) walked only `files`, so on a
single linear thread:

  Turn 1: assistant produces sample.xlsx → attachment with codeEnvRef
  Turn 2: user says "add 2 rows"
          → primeCodeFiles: file_ids=0 resourceFiles=0
          → /exec sent files=[]
          → sandbox: FileNotFoundError: 'sample.xlsx'

The `getThreadData` walk found zero file_ids because the assistant's
codeEnvRef was on `attachments`, not `files`. Compounded by the
DB select string `'messageId parentMessageId files'` which didn't
pull `attachments` into memory in the first place — so even fixing
the walk in isolation wouldn't have surfaced them.

Both layers fixed:
  - `ThreadMessage` type adds `attachments?: Array<{ file_id?: string }>`
  - `getThreadData` walks both arrays, dedups via the same Set
  - `initialize.ts` selects `'messageId parentMessageId files attachments'`

## Test plan

`packages/api/src/utils/message.spec.ts` (+6 cases):
- collects file_ids from `attachments`
- walks both `files` and `attachments` on the same message
- regression: linear thread with code-output attachments across
  user→assistant→user→assistant produces the right file_ids
- dedupes shared ids that appear in both arrays
- skips attachments without file_id (mirrors `files` behavior)
- empty `attachments` array

`packages/api/src/agents/__tests__/initialize.test.ts` (+1 case):
- locks the DB select string includes `attachments` alongside
  `files` / `messageId` / `parentMessageId`

- [x] `npx jest src/utils/message.spec.ts` — 39/39 pass
- [x] `npx jest src/agents/__tests__/initialize.test.ts` — 33/33 pass
- [x] lint clean on all four touched files
2026-05-08 12:29:46 -04:00
..
__tests__ 📡 feat: Support Unauthenticated SMTP Relays (#12322) 2026-03-20 13:07:39 -04:00
axios.spec.ts 👁️ feat: Azure Mistral OCR Strategy (#7888) 2025-06-13 15:14:57 -04:00
axios.ts 🧱 refactor: typed CodeEnvRef + kind discriminator + principal-aware sandbox cache (#12960) 2026-05-08 12:29:43 -04:00
azure.spec.ts 🧠 feat: User Memories for Conversational Context (#7760) 2025-06-07 18:52:22 -04:00
azure.ts 🎙️ fix: Azure OpenAI Speech-to-Text 400 Bad Request Error (#10355) 2025-11-05 10:27:34 -05:00
code.ts 🔌 fix: Isolate Code-Server HTTP Agents to Prevent Socket Pool Contamination (#12311) 2026-03-19 16:16:57 -04:00
common.spec.ts 🧠 feat: User Memories for Conversational Context (#7760) 2025-06-07 18:52:22 -04:00
common.ts 📦 refactor: Consolidate DB models, encapsulating Mongoose usage in data-schemas (#11830) 2026-03-21 14:28:53 -04:00
content.spec.ts 🧬 chore: Align LibreChat With Agents LangChain Upgrade (#12922) 2026-05-03 12:46:01 -04:00
content.ts 🔧 fix: Await MCP Instructions and Filter Malformed Tool Calls (#10485) 2025-11-13 14:17:47 -05:00
email.ts 📡 feat: Support Unauthenticated SMTP Relays (#12322) 2026-03-20 13:07:39 -04:00
env.spec.ts 🧯 fix: Prevent Env-Variable Exfil. via Placeholder Injection (#12260) 2026-03-16 08:48:24 -04:00
env.ts 🐛 fix: Propagate User Identity to Subagent MCP Tool Calls (#12950) 2026-05-05 12:19:50 +09:00
events.ts 🛰️ fix: Cross-Replica Created Event Delivery (#12231) 2026-03-15 11:11:10 -04:00
files.spec.ts 🌐 fix: Preserve Unicode Filenames (#12977) 2026-05-06 14:57:38 -04:00
files.ts 🔐 feat: Add Signed CloudFront File Downloads (#12970) 2026-05-06 19:48:30 -04:00
generators.ts 🧠 feat: User Memories for Conversational Context (#7760) 2025-06-07 18:52:22 -04:00
graph.spec.ts 🔬 ci: Add TypeScript Type Checks to Backend Workflow and Fix All Type Errors (#12451) 2026-03-28 21:06:39 -04:00
graph.ts 🪪 feat: Microsoft Graph Access Token Placeholder for MCP Servers (#10867) 2026-01-28 17:44:33 -05:00
http.ts 🔧 refactor: customUserVar Error Normalization (#8950) 2025-08-08 15:53:04 -04:00
import.ts 📏 refactor: Add File Size Limits to Conversation Imports (#12221) 2026-03-14 03:06:29 -04:00
index.ts ⚗️ feat: Agent Context Compaction/Summarization (#12287) 2026-03-21 14:28:56 -04:00
key.test.ts 📂 refactor: File Read Operations (#9747) 2025-09-20 10:17:24 -04:00
key.ts 🧵 refactor: Migrate Endpoint Initialization to TypeScript (#10794) 2025-12-11 16:37:16 -05:00
latex.spec.ts 🔢 fix: Unescape LaTeX Numbers in Artifact Content Edit (#10476) 2025-11-13 08:19:19 -05:00
latex.ts 🔢 fix: Unescape LaTeX Numbers in Artifact Content Edit (#10476) 2025-11-13 08:19:19 -05:00
llm.test.ts 📎 feat: Upload as Text Support for Plaintext, STT, RAG, and Token Limits (#8868) 2025-08-27 03:44:39 -04:00
llm.ts 📎 feat: Upload as Text Support for Plaintext, STT, RAG, and Token Limits (#8868) 2025-08-27 03:44:39 -04:00
math.integration.spec.ts 🧮 refactor: Replace Eval with Safe Math Expression Parser (#11098) 2025-12-25 12:25:41 -05:00
math.spec.ts 🧮 refactor: Replace Eval with Safe Math Expression Parser (#11098) 2025-12-25 12:25:41 -05:00
math.ts 🧮 refactor: Replace Eval with Safe Math Expression Parser (#11098) 2025-12-25 12:25:41 -05:00
memory.ts 🪣 fix: Prevent Memory Retention from AsyncLocalStorage Context Propagation (#11942) 2026-02-25 17:41:23 -05:00
message.spec.ts 🧵 fix: Include Code Outputs in Thread File Lookup (#13023) 2026-05-08 12:29:46 -04:00
message.ts 🧵 fix: Include Code Outputs in Thread File Lookup (#13023) 2026-05-08 12:29:46 -04:00
oidc.spec.ts 🔬 ci: Add TypeScript Type Checks to Backend Workflow and Fix All Type Errors (#12451) 2026-03-28 21:06:39 -04:00
oidc.ts 🔬 ci: Add TypeScript Type Checks to Backend Workflow and Fix All Type Errors (#12451) 2026-03-28 21:06:39 -04:00
openid.ts 🔍 refactor: OpenID Fetch Handling and Logging (#7790) 2025-06-09 11:27:23 -04:00
path.spec.ts 🧭 fix: Add Base Path Support for Login/Register and Image Paths (#10116) 2025-11-21 11:25:14 -05:00
path.ts 🧭 fix: Add Base Path Support for Login/Register and Image Paths (#10116) 2025-11-21 11:25:14 -05:00
ports.spec.ts 🪤 fix: Avoid express-rate-limit v8 ERR_ERL_KEY_GEN_IPV6 False Positive (#12333) 2026-03-20 12:32:55 -04:00
ports.ts 🪤 fix: Avoid express-rate-limit v8 ERR_ERL_KEY_GEN_IPV6 False Positive (#12333) 2026-03-20 12:32:55 -04:00
promise.spec.ts refactor: Bound Concurrent Office-HTML Rendering for Code Artifacts (#12951) 2026-05-05 08:53:21 -04:00
promise.ts refactor: Bound Concurrent Office-HTML Rendering for Code Artifacts (#12951) 2026-05-05 08:53:21 -04:00
sanitizeTitle.spec.ts ✂️ fix: Unicode-Safe Title Truncation and Shared View Layout Polish (#12003) 2026-03-01 16:44:57 -05:00
sanitizeTitle.ts ✂️ fix: Unicode-Safe Title Truncation and Shared View Layout Polish (#12003) 2026-03-01 16:44:57 -05:00
text.spec.ts refactor: Replace tiktoken with ai-tokenizer (#12175) 2026-03-10 23:14:52 -04:00
text.ts refactor: Optimize & Standardize Tokenizer Usage (#10777) 2025-12-02 12:22:04 -05:00
tokenizer.spec.ts refactor: Replace tiktoken with ai-tokenizer (#12175) 2026-03-10 23:14:52 -04:00
tokenizer.ts refactor: Replace tiktoken with ai-tokenizer (#12175) 2026-03-10 23:14:52 -04:00
tokenMap.ts 🧬 chore: Align LibreChat With Agents LangChain Upgrade (#12922) 2026-05-03 12:46:01 -04:00
tokens.ts 🧮 feat: Add GPT-5.5 Token Definitions (#12973) 2026-05-06 10:50:16 -04:00
tracing.ts 🪣 fix: Prevent Memory Retention from AsyncLocalStorage Context Propagation (#11942) 2026-02-25 17:41:23 -05:00
url.spec.ts 🧵 refactor: Migrate Endpoint Initialization to TypeScript (#10794) 2025-12-11 16:37:16 -05:00
url.ts 🧵 refactor: Migrate Endpoint Initialization to TypeScript (#10794) 2025-12-11 16:37:16 -05:00
yaml.ts 🕐 feat: Configurable Retention Period for Temporary Chats (#8056) 2025-06-25 17:16:26 -04:00