AskTim 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- asktim/__init__.py +72 -0
- asktim/__main__.py +6 -0
- asktim/agent.py +490 -0
- asktim/api.py +41 -0
- asktim/chat.py +106 -0
- asktim/cli.py +577 -0
- asktim/config.py +316 -0
- asktim/context.py +156 -0
- asktim/fileinfo.py +593 -0
- asktim/history.py +214 -0
- asktim/inventory.py +379 -0
- asktim/logbook.py +159 -0
- asktim/net.py +151 -0
- asktim/notebook.py +1010 -0
- asktim/offline.py +110 -0
- asktim/paths.py +81 -0
- asktim/privacy.py +804 -0
- asktim/prompts.py +226 -0
- asktim/providers/__init__.py +173 -0
- asktim/providers/anthropic.py +227 -0
- asktim/providers/gemini.py +215 -0
- asktim/providers/openai.py +135 -0
- asktim/records.py +319 -0
- asktim/render.py +511 -0
- asktim/runner.py +308 -0
- asktim/safety.py +1495 -0
- asktim/shell_setup.py +183 -0
- asktim/ui.py +303 -0
- asktim/viewers.py +97 -0
- asktim-0.1.0.data/scripts/AskTim! +8 -0
- asktim-0.1.0.dist-info/METADATA +458 -0
- asktim-0.1.0.dist-info/RECORD +36 -0
- asktim-0.1.0.dist-info/WHEEL +5 -0
- asktim-0.1.0.dist-info/entry_points.txt +2 -0
- asktim-0.1.0.dist-info/licenses/LICENSE +21 -0
- asktim-0.1.0.dist-info/top_level.txt +1 -0
asktim/offline.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""Help without an AI: what common error messages and exit statuses mean.
|
|
2
|
+
|
|
3
|
+
Used when no key is set up, when the AI service cannot be reached, and with --offline.
|
|
4
|
+
The explanations are adapted from the offline help of the bioinformatics workbench.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import re
|
|
8
|
+
|
|
9
|
+
SHELL_HELP = [
|
|
10
|
+
(r"command not found|not found$|: not found", "The shell has no program of that name. Check the spelling (names are case-sensitive), "
|
|
11
|
+
"and whether it is installed: `command -v NAME` prints its path if it is. `AskTim! tools` lists what is installed here. "
|
|
12
|
+
"A script in the current folder runs as `bash script.sh` or `./script.sh`; on a cluster you may need `module load NAME` first."),
|
|
13
|
+
(r"No such file or directory|cannot open|[Cc]ould not open|[Ff]ailed to open|does not exist|cannot access", "A file or folder in the command "
|
|
14
|
+
"is not where the command looked. Check the folder you are in (`pwd`) and what is there (`ls`). A path without a leading `/` or `~` "
|
|
15
|
+
"starts from the current folder; `~` inside quotes is not your home folder."),
|
|
16
|
+
(r"Permission denied|Read-only file system|Operation not permitted", "That file or folder cannot be changed or run by you. To run a script, "
|
|
17
|
+
"use `bash script.sh` or make it executable (`chmod +x script.sh`). Shared course data is usually read-only: copy what you need into "
|
|
18
|
+
"your own folder and work on the copy."),
|
|
19
|
+
(r"unrecognized option|unrecognised option|invalid option|unknown option|illegal option|unknown argument|no such option", "The program does "
|
|
20
|
+
"not have that option (in this version). Check its real options with `PROGRAM --help` or `man PROGRAM` - AI tools often invent options "
|
|
21
|
+
"that sound right."),
|
|
22
|
+
(r"syntax error|unexpected token|unexpected EOF|unexpected end of file|unterminated", "The shell could not read the command: look for an "
|
|
23
|
+
"unmatched quote or bracket, a `do` without `done`, or an `if` without `fi`. Characters such as `(`, `)`, `;`, `&`, `|`, `<`, `>` must "
|
|
24
|
+
"be quoted when they are part of a name."),
|
|
25
|
+
(r"Is a directory", "The command expected a file but got a folder. Give the path of a file in it, or use the option for folders (`cp -r`, `rm -r`)."),
|
|
26
|
+
(r"[Nn]ot a directory", "Part of the path is a file, not a folder."),
|
|
27
|
+
(r"out of memory|Cannot allocate memory|MemoryError|std::bad_alloc|Killed", "The program ran out of memory. Work on a smaller input "
|
|
28
|
+
"(`seqtk sample`, `head -n 400000 reads.fastq > small.fastq`), one region or chromosome at a time, or ask for more memory "
|
|
29
|
+
"(on a cluster: the --mem option of your job)."),
|
|
30
|
+
(r"truncated file|EOF marker is absent|invalid BGZF|not compressed with bgzip|fail to read the header|unexpected end of file", "An input file "
|
|
31
|
+
"is incomplete or not in the format the program expects. Check it with `file NAME`, `ls -lh` (is it empty?) or `samtools quickcheck`. "
|
|
32
|
+
"A .gz file that is to be indexed must be compressed with `bgzip`, not `gzip`."),
|
|
33
|
+
(r"No space left on device|Disk quota exceeded", "The disk (or your quota) is full. `df -h .` shows the free space and `du -sh *` what is "
|
|
34
|
+
"using it; delete intermediate files you can make again (SAM files are large: keep the BAM)."),
|
|
35
|
+
(r"[Ss]egmentation fault|core dumped", "The program crashed. Often an input is in the wrong format or the reference and the reads do not match; "
|
|
36
|
+
"check the inputs, and try a small test first."),
|
|
37
|
+
(r"Argument list too long", "The wildcard matched too many files. Use `find . -name '*.fq' -print0 | xargs -0 ...` or a loop."),
|
|
38
|
+
(r"[Ii]ndex.*(not found|missing|could not)|could not load index|fail to locate the index", "The index the program needs is missing: build "
|
|
39
|
+
"it first (`bwa index ref.fa`, `samtools faidx ref.fa`, `samtools index aln.bam`, `bowtie2-build ref.fa ref`, `tabix -p vcf x.vcf.gz`)."),
|
|
40
|
+
(r"sort.*(not|un)sorted|must be sorted|is not sorted|unsorted positions", "The input must be sorted by position first: `samtools sort -o sorted.bam in.bam` "
|
|
41
|
+
"(then `samtools index sorted.bam`)."),
|
|
42
|
+
(r"[Cc]onnection (refused|timed out)|Could not resolve host|Name or service not known|Network is unreachable", "The computer could not reach "
|
|
43
|
+
"the server: check the address and your internet connection (clusters' compute nodes often have no internet)."),
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
PY_HELP = {
|
|
47
|
+
"NameError": "Python does not know that name: it is misspelt (names are case-sensitive), or the cell that creates it has not been run since "
|
|
48
|
+
"the kernel started. After a restart, run the cells again from the top.",
|
|
49
|
+
"SyntaxError": "Python could not read the code: look for a missing bracket, quote, comma or colon on the line shown - or just above it.",
|
|
50
|
+
"IndentationError": "Python groups lines by their indentation: the lines inside `for`, `if`, `def` or `with` must all be indented the same (4 spaces).",
|
|
51
|
+
"KeyError": "That key is not in the dictionary - or that column is not in the table. Keys are case-sensitive: print `df.columns` to see what is there.",
|
|
52
|
+
"IndexError": "An index is out of range: a list of 3 items has positions 0, 1 and 2.",
|
|
53
|
+
"TypeError": "A value of the wrong type was used - a number added to a string, or something called that is not a function.",
|
|
54
|
+
"FileNotFoundError": "The file is not where Python looked. `import os; os.getcwd()` shows the folder your code runs in: give the path from there, or the full path.",
|
|
55
|
+
"ModuleNotFoundError": "That package is not installed in this Python. Check the name (Biopython is imported as `Bio`, scikit-learn as `sklearn`); "
|
|
56
|
+
"ask your instructor before installing anything.",
|
|
57
|
+
"ImportError": "The package is there, but that name is not in it (or not in this version): check the spelling and the package's documentation.",
|
|
58
|
+
"AttributeError": "The object has no attribute or method of that name - often a typo, or a method of another kind of object (`type(x)` shows what it is).",
|
|
59
|
+
"ValueError": "The value has the right type but cannot be used - for example `int('NA12878')`, or two lists of different lengths.",
|
|
60
|
+
"ZeroDivisionError": "Something was divided by zero.",
|
|
61
|
+
"PermissionError": "That file cannot be written: save results in your own folders.",
|
|
62
|
+
"MemoryError": "Python ran out of memory. Work on part of the data, or use a format and types that need less (for example `dtype` in pandas).",
|
|
63
|
+
"UnicodeDecodeError": "The file is not text in the encoding Python expected - it may be compressed (`.gz`) or binary (`.bam`), or need `encoding='latin-1'`.",
|
|
64
|
+
"ParserError": "pandas could not read the table: check the separator (`sep='\\t'` for tab-separated files) and whether the file has comment lines (`comment='#'`).",
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
R_HELP = [
|
|
68
|
+
(r"could not find function", "R does not know that function: load its package first (`library(dplyr)`), or check the spelling - names are case-sensitive."),
|
|
69
|
+
(r"object '.*' not found", "R does not know that name: it is misspelt (names are case-sensitive), or the line that creates it has not been run."),
|
|
70
|
+
(r"there is no package called", "That package is not installed in this R. Check the name, and ask your instructor before installing anything."),
|
|
71
|
+
(r"cannot open (file|the connection)", "The file is not where R looked: `getwd()` shows R's working folder; give the path from there, or the full path."),
|
|
72
|
+
(r"unexpected (symbol|string constant|numeric constant|'\)'|'\}'|','|'='|input|end of input)", "R could not read the code: look for a missing or extra bracket, comma, quote or operator on that line, or the line before."),
|
|
73
|
+
(r"non-numeric argument to (binary operator|mathematical function)", "A calculation got text instead of numbers - often a column that was read as text. Look at it with `str(df)`."),
|
|
74
|
+
(r"arguments imply differing number of rows", "The vectors or columns have different lengths, so they cannot form one table."),
|
|
75
|
+
(r"subscript out of bounds|undefined columns selected", "A row, column or element that does not exist was asked for: check `names(df)` and `dim(df)`."),
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
EXIT_HELP = {
|
|
79
|
+
127: "Exit status 127 means the command was not found: a program that is not installed (or not on PATH), or a typo.",
|
|
80
|
+
126: "Exit status 126 means the file was found but could not be run: make it executable (`chmod +x`) or run it with `bash`.",
|
|
81
|
+
130: "Exit status 130 means the command was stopped with Ctrl+C.",
|
|
82
|
+
137: "Exit status 137 means the program was killed - often because it ran out of memory.",
|
|
83
|
+
139: "Exit status 139 means the program crashed (segmentation fault): check that its inputs are in the right format.",
|
|
84
|
+
141: "Exit status 141 means a pipe closed early (for example with `| head`) - usually harmless.",
|
|
85
|
+
2: "Exit status 2 usually means the command was used wrongly, or a file was missing.",
|
|
86
|
+
1: "Exit status 1 is a general error: the program's own message says more.",
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def explain_text(text):
|
|
91
|
+
"""Explanations for an error message (shell, Python or R); [] if nothing matches."""
|
|
92
|
+
out = []
|
|
93
|
+
t = text or ""
|
|
94
|
+
m = re.search(r"\b([A-Z][A-Za-z]*(?:Error|Exception))\b", t)
|
|
95
|
+
if m and m.group(1) in PY_HELP:
|
|
96
|
+
out.append(PY_HELP[m.group(1)])
|
|
97
|
+
for pat, msg in R_HELP if re.search(r"\bError in\b|^Error:|could not find function|object '", t, re.M) else []:
|
|
98
|
+
if re.search(pat, t):
|
|
99
|
+
out.append(msg)
|
|
100
|
+
for pat, msg in SHELL_HELP:
|
|
101
|
+
if re.search(pat, t, re.M) and msg not in out:
|
|
102
|
+
out.append(msg)
|
|
103
|
+
return out[:3]
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def explain_command(cmd):
|
|
107
|
+
"""For a command from the history: what its exit status means."""
|
|
108
|
+
if cmd is None or cmd.status in (None, 0):
|
|
109
|
+
return []
|
|
110
|
+
return [EXIT_HELP.get(cmd.status, "Exit status %d: the command did not succeed; its own message (above it in your terminal) says why." % cmd.status)]
|
asktim/paths.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""Where AskTim! keeps its files.
|
|
2
|
+
|
|
3
|
+
config ~/.config/asktim settings (config.json) and keys (keys.json, readable by you only)
|
|
4
|
+
cache ~/.cache/asktim the list of installed programs, the current conversation
|
|
5
|
+
state ~/.local/state/asktim the log of your questions, the commands the shell hook records
|
|
6
|
+
|
|
7
|
+
ASKTIM_HOME=/some/folder puts all three under that folder (useful for tests and shared machines).
|
|
8
|
+
The XDG_* variables are respected; on Windows %APPDATA% and %LOCALAPPDATA% are used.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import os
|
|
12
|
+
import sys
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _home():
|
|
16
|
+
return os.path.expanduser("~")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _base(kind):
|
|
20
|
+
override = os.environ.get("ASKTIM_HOME")
|
|
21
|
+
if override:
|
|
22
|
+
return os.path.join(os.path.expanduser(override), kind)
|
|
23
|
+
if sys.platform.startswith("win"):
|
|
24
|
+
root = os.environ.get("APPDATA" if kind == "config" else "LOCALAPPDATA") or os.path.join(_home(), "AppData", "Roaming")
|
|
25
|
+
return os.path.join(root, "asktim", kind if kind != "config" else "")
|
|
26
|
+
env = {"config": "XDG_CONFIG_HOME", "cache": "XDG_CACHE_HOME", "state": "XDG_STATE_HOME"}[kind]
|
|
27
|
+
default = {"config": ".config", "cache": ".cache", "state": os.path.join(".local", "state")}[kind]
|
|
28
|
+
root = os.environ.get(env) or os.path.join(_home(), default)
|
|
29
|
+
return os.path.join(root, "asktim")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def config_dir():
|
|
33
|
+
return _base("config")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def cache_dir():
|
|
37
|
+
return _base("cache")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def state_dir():
|
|
41
|
+
return _base("state")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def ensure(path, private=False):
|
|
45
|
+
"""Make a folder (and its parents); private=True makes it readable by the owner only."""
|
|
46
|
+
os.makedirs(path, exist_ok=True)
|
|
47
|
+
if private and not sys.platform.startswith("win"):
|
|
48
|
+
try:
|
|
49
|
+
os.chmod(path, 0o700)
|
|
50
|
+
except OSError:
|
|
51
|
+
pass
|
|
52
|
+
return path
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def site_config_file():
|
|
56
|
+
"""A settings file that an instructor can put on a shared machine (read before the user's)."""
|
|
57
|
+
p = os.environ.get("ASKTIM_SITE_CONFIG")
|
|
58
|
+
if p:
|
|
59
|
+
return p
|
|
60
|
+
if sys.platform.startswith("win"):
|
|
61
|
+
return os.path.join(os.environ.get("PROGRAMDATA", r"C:\ProgramData"), "asktim", "config.json")
|
|
62
|
+
return "/etc/asktim/config.json"
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def commands_log():
|
|
66
|
+
"""The file the shell hook appends to: time, exit status, folder, command (one per line)."""
|
|
67
|
+
return os.path.join(state_dir(), "commands.tsv")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def pretty(path):
|
|
71
|
+
"""A path as the student would write it: ~ for the home folder."""
|
|
72
|
+
try:
|
|
73
|
+
path = os.path.abspath(path)
|
|
74
|
+
home = _home()
|
|
75
|
+
if path == home:
|
|
76
|
+
return "~"
|
|
77
|
+
if path.startswith(home + os.sep):
|
|
78
|
+
return "~" + path[len(home):]
|
|
79
|
+
except Exception:
|
|
80
|
+
pass
|
|
81
|
+
return path
|