bcbd1bdb22
Integ / changes (push) Has been skipped
Pre-commit / pre-commit (push) Failing after 1s
CLI exit codes / changes (push) Has been skipped
Test (Install) / changes (push) Has been skipped
Test (Python) / changes (push) Has been skipped
Test (TypeScript) / changes (push) Has been skipped
CLI exit codes / cli-gate (push) Has been cancelled
Test (Install) / test-install-gate (push) Has been cancelled
Integ / integ-gate (push) Has been cancelled
Test (Python) / test-python-gate (push) Has been cancelled
Test (TypeScript) / test-typescript-gate (push) Has been cancelled
Test (Install) / python-minimal (3.12) (push) Has been cancelled
Test (Install) / python-minimal (3.11) (push) Has been cancelled
Test (Install) / python-extra (agno, mirage.agents.agno) (push) Has been cancelled
Test (Install) / python-extra (chroma, mirage.resource.chroma) (push) Has been cancelled
Test (Install) / python-extra (pdf, mirage.core.filetype.pdf) (push) Has been cancelled
Integ / integ (push) Has been cancelled
Integ / integ-database (push) Has been cancelled
Integ / integ-database-ts (push) Has been cancelled
Integ / integ-data (push) Has been cancelled
Integ / integ-ssh (push) Has been cancelled
Integ / integ-ssh-ts (push) Has been cancelled
Test (Python) / audit (push) Has been cancelled
Test (TypeScript) / test (push) Has been cancelled
Test (TypeScript) / python-fs-shim (push) Has been cancelled
CLI exit codes / Python CLI (push) Has been cancelled
CLI exit codes / TypeScript CLI (push) Has been cancelled
CLI exit codes / Cross-language snapshot interop (push) Has been cancelled
Test (Python) / test (push) Has been cancelled
Test (Python) / import-isolation (deepagents, openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Python) / import-isolation (deepagents, pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Integ / integ-ts (push) Has been cancelled
Integ / integ-fuse (push) Has been cancelled
Test (Install) / python-extra (databricks, mirage.resource.databricks_volume) (push) Has been cancelled
Test (Install) / python-extra (deepagents, mirage.agents.langchain) (push) Has been cancelled
Test (Install) / python-extra (email, mirage.resource.email) (push) Has been cancelled
Test (Install) / python-extra (fuse, mirage.fuse.mount) (push) Has been cancelled
Test (Install) / python-extra (hdf5, mirage.core.filetype.hdf5) (push) Has been cancelled
Test (Install) / python-extra (hf, mirage.resource.hf_buckets) (push) Has been cancelled
Test (Install) / python-extra (lancedb, mirage.resource.lancedb) (push) Has been cancelled
Test (Install) / python-extra (langfuse, mirage.resource.langfuse) (push) Has been cancelled
Test (Install) / python-extra (mongodb, mirage.resource.mongodb) (push) Has been cancelled
Test (Install) / python-extra (nextcloud, mirage.resource.nextcloud) (push) Has been cancelled
Test (Install) / python-extra (openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Install) / python-extra (openhands, mirage.agents.openhands, 3.12) (push) Has been cancelled
Test (Install) / python-extra (parquet, mirage.core.filetype.parquet) (push) Has been cancelled
Test (Install) / python-extra (postgres, mirage.resource.postgres) (push) Has been cancelled
Test (Install) / python-extra (pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Test (Install) / python-extra (qdrant, mirage.resource.qdrant) (push) Has been cancelled
Test (Install) / python-extra (redis, mirage.resource.redis) (push) Has been cancelled
Test (Install) / python-extra (s3, mirage.resource.s3) (push) Has been cancelled
Test (Install) / python-extra (ssh, mirage.resource.ssh) (push) Has been cancelled
Test (Install) / ts-minimal (push) Has been cancelled
151 lines
5.4 KiB
Python
151 lines
5.4 KiB
Python
# ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
# ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
|
|
|
|
import json
|
|
from datetime import datetime, timezone
|
|
|
|
from mirage.core.slack.files import file_blob_name
|
|
from mirage.core.slack.scope import SlackScope
|
|
from mirage.utils.naming import make_id_name
|
|
from mirage.utils.sanitize import path_safe_name
|
|
|
|
|
|
def channel_dirname(ch: dict) -> str:
|
|
"""Compute the VFS dirname for a channel, of the form `name__C123`."""
|
|
return make_id_name(
|
|
ch.get("name", ch.get("id", "unknown")),
|
|
ch["id"],
|
|
path_safe=True,
|
|
)
|
|
|
|
|
|
def dm_dirname(dm: dict, user_map: dict[str, str]) -> str:
|
|
"""Compute the VFS dirname for a DM, of the form `username__D123`.
|
|
|
|
Args:
|
|
dm (dict): DM channel dict with id and user fields.
|
|
user_map (dict[str, str]): user_id -> name lookup.
|
|
|
|
Returns:
|
|
str: dirname; falls back to the user id when not in user_map.
|
|
"""
|
|
user_id = dm.get("user", "")
|
|
display = user_map.get(user_id, user_id)
|
|
return make_id_name(display, dm["id"], path_safe=True)
|
|
|
|
|
|
def user_filename(u: dict) -> str:
|
|
"""Compute the VFS filename for a user, of the form `name__U123.json`."""
|
|
name = u.get("name", u.get("id", "unknown"))
|
|
return f"{make_id_name(name, u['id'], path_safe=True)}.json"
|
|
|
|
|
|
def build_query(pattern: str, scope: SlackScope) -> str:
|
|
"""Compose a Slack search query string for a pushed-down grep/rg call.
|
|
|
|
Args:
|
|
pattern (str): user-supplied pattern.
|
|
scope (SlackScope): the Slack-side directory the grep is rooted at.
|
|
|
|
Returns:
|
|
str: query with optional in:#channel or in:@user prefix.
|
|
"""
|
|
if scope.container == "channels" and scope.channel_name:
|
|
return f"in:#{scope.channel_name} {pattern}"
|
|
if scope.container == "dms" and scope.channel_name:
|
|
return f"in:@{scope.channel_name} {pattern}"
|
|
return pattern
|
|
|
|
|
|
def format_grep_results(
|
|
raw: bytes,
|
|
scope: SlackScope,
|
|
prefix: str,
|
|
) -> list[str]:
|
|
"""Format a Slack search.messages JSON response as grep-style lines.
|
|
|
|
Args:
|
|
raw (bytes): JSON-encoded search.messages response.
|
|
scope (SlackScope): the rooted scope used to compute relative paths.
|
|
prefix (str): VFS mount prefix to prepend.
|
|
|
|
Returns:
|
|
list[str]: grep-formatted lines, one per match.
|
|
"""
|
|
payload = json.loads(raw.decode())
|
|
matches = payload.get("messages", {}).get("matches", []) or []
|
|
lines: list[str] = []
|
|
for msg in matches:
|
|
ch = msg.get("channel", {}) or {}
|
|
ch_name = ch.get("name") or scope.channel_name or ""
|
|
ch_id = ch.get("id") or scope.channel_id or ""
|
|
container = scope.container or "channels"
|
|
ts_raw = msg.get("ts", "0")
|
|
try:
|
|
ts_float = float(ts_raw)
|
|
date_str = datetime.fromtimestamp(
|
|
ts_float, tz=timezone.utc).date().isoformat()
|
|
except (TypeError, ValueError):
|
|
date_str = ""
|
|
safe_name = path_safe_name(ch_name)
|
|
dirname = f"{safe_name}__{ch_id}" if ch_id else safe_name
|
|
path = (f"{prefix}/{container}/{dirname}/{date_str}/chat.jsonl"
|
|
if date_str else f"{prefix}/{container}/{dirname}")
|
|
author = msg.get("username") or msg.get("user") or "?"
|
|
text = (msg.get("text") or "").replace("\n", " ")
|
|
lines.append(f"{path}:[{author}] {text}")
|
|
return lines
|
|
|
|
|
|
def format_file_grep_results(
|
|
raw: bytes,
|
|
scope: SlackScope,
|
|
prefix: str,
|
|
) -> list[str]:
|
|
"""Format a Slack search.files JSON response as grep-style lines.
|
|
|
|
Args:
|
|
raw (bytes): JSON-encoded search.files response.
|
|
scope (SlackScope): the rooted scope (must have channel_id).
|
|
prefix (str): VFS mount prefix to prepend.
|
|
|
|
Returns:
|
|
list[str]: grep-formatted lines, one per file match.
|
|
"""
|
|
payload = json.loads(raw.decode())
|
|
matches = payload.get("files", {}).get("matches", []) or []
|
|
lines: list[str] = []
|
|
for f in matches:
|
|
fid = f.get("id", "")
|
|
title = (f.get("title") or f.get("name") or fid)
|
|
blob_name = file_blob_name(f)
|
|
ts = f.get("timestamp", 0)
|
|
try:
|
|
date_str = datetime.fromtimestamp(
|
|
float(ts), tz=timezone.utc).date().isoformat()
|
|
except (TypeError, ValueError):
|
|
date_str = ""
|
|
if not scope.channel_id:
|
|
continue
|
|
ch_id = scope.channel_id
|
|
ch_name = scope.channel_name or ""
|
|
safe_name = path_safe_name(ch_name) if ch_name else ""
|
|
dirname = f"{safe_name}__{ch_id}" if safe_name else ch_id
|
|
container = scope.container or "channels"
|
|
path = (f"{prefix}/{container}/{dirname}/{date_str}/files/{blob_name}"
|
|
if date_str else
|
|
f"{prefix}/{container}/{dirname}/files/{blob_name}")
|
|
lines.append(f"{path}:[file] {title}")
|
|
return lines
|