pretticlaw 0.1.0

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.
Files changed (158) hide show
  1. package/CONTRIBUTING.md +123 -0
  2. package/README.md +150 -0
  3. package/assets/logo.png +0 -0
  4. package/dist/agent/context.d.ts +22 -0
  5. package/dist/agent/context.js +85 -0
  6. package/dist/agent/loop.d.ts +63 -0
  7. package/dist/agent/loop.js +244 -0
  8. package/dist/agent/memory.d.ts +16 -0
  9. package/dist/agent/memory.js +98 -0
  10. package/dist/agent/skills.d.ts +18 -0
  11. package/dist/agent/skills.js +121 -0
  12. package/dist/agent/subagent.d.ts +30 -0
  13. package/dist/agent/subagent.js +92 -0
  14. package/dist/agent/tools/base.d.ts +10 -0
  15. package/dist/agent/tools/base.js +58 -0
  16. package/dist/agent/tools/cron.d.ts +43 -0
  17. package/dist/agent/tools/cron.js +83 -0
  18. package/dist/agent/tools/filesystem.d.ts +79 -0
  19. package/dist/agent/tools/filesystem.js +125 -0
  20. package/dist/agent/tools/message.d.ts +41 -0
  21. package/dist/agent/tools/message.js +55 -0
  22. package/dist/agent/tools/registry.d.ts +9 -0
  23. package/dist/agent/tools/registry.js +33 -0
  24. package/dist/agent/tools/shell.d.ts +26 -0
  25. package/dist/agent/tools/shell.js +78 -0
  26. package/dist/agent/tools/spawn.d.ts +27 -0
  27. package/dist/agent/tools/spawn.js +35 -0
  28. package/dist/agent/tools/web.d.ts +50 -0
  29. package/dist/agent/tools/web.js +119 -0
  30. package/dist/bus/async-queue.d.ts +7 -0
  31. package/dist/bus/async-queue.js +20 -0
  32. package/dist/bus/events.d.ts +19 -0
  33. package/dist/bus/events.js +3 -0
  34. package/dist/bus/queue.d.ts +12 -0
  35. package/dist/bus/queue.js +23 -0
  36. package/dist/channels/base.d.ts +22 -0
  37. package/dist/channels/base.js +35 -0
  38. package/dist/channels/discord.d.ts +24 -0
  39. package/dist/channels/discord.js +133 -0
  40. package/dist/channels/manager.d.ts +17 -0
  41. package/dist/channels/manager.js +67 -0
  42. package/dist/channels/stub.d.ts +10 -0
  43. package/dist/channels/stub.js +18 -0
  44. package/dist/channels/telegram.d.ts +20 -0
  45. package/dist/channels/telegram.js +93 -0
  46. package/dist/cli/commands.d.ts +2 -0
  47. package/dist/cli/commands.js +552 -0
  48. package/dist/config/loader.d.ts +5 -0
  49. package/dist/config/loader.js +55 -0
  50. package/dist/config/schema.d.ts +246 -0
  51. package/dist/config/schema.js +94 -0
  52. package/dist/cron/service.d.ts +33 -0
  53. package/dist/cron/service.js +195 -0
  54. package/dist/cron/types.d.ts +47 -0
  55. package/dist/cron/types.js +1 -0
  56. package/dist/dashboard/index.html +1567 -0
  57. package/dist/heartbeat/service.d.ts +21 -0
  58. package/dist/heartbeat/service.js +101 -0
  59. package/dist/index.d.ts +2 -0
  60. package/dist/index.js +5 -0
  61. package/dist/providers/base.d.ts +23 -0
  62. package/dist/providers/base.js +21 -0
  63. package/dist/providers/custom-provider.d.ts +16 -0
  64. package/dist/providers/custom-provider.js +49 -0
  65. package/dist/providers/litellm-provider.d.ts +19 -0
  66. package/dist/providers/litellm-provider.js +128 -0
  67. package/dist/providers/registry.d.ts +5 -0
  68. package/dist/providers/registry.js +45 -0
  69. package/dist/session/manager.d.ts +31 -0
  70. package/dist/session/manager.js +116 -0
  71. package/dist/skills/README.md +25 -0
  72. package/dist/skills/clawhub/SKILL.md +53 -0
  73. package/dist/skills/cron/SKILL.md +57 -0
  74. package/dist/skills/github/SKILL.md +48 -0
  75. package/dist/skills/memory/SKILL.md +31 -0
  76. package/dist/skills/skill-creator/SKILL.md +371 -0
  77. package/dist/skills/summarize/SKILL.md +67 -0
  78. package/dist/skills/tmux/SKILL.md +121 -0
  79. package/dist/skills/tmux/scripts/find-sessions.sh +112 -0
  80. package/dist/skills/tmux/scripts/wait-for-text.sh +83 -0
  81. package/dist/skills/weather/SKILL.md +49 -0
  82. package/dist/templates/AGENTS.md +23 -0
  83. package/dist/templates/HEARTBEAT.md +16 -0
  84. package/dist/templates/SOUL.md +21 -0
  85. package/dist/templates/TOOLS.md +15 -0
  86. package/dist/templates/USER.md +49 -0
  87. package/dist/templates/memory/MEMORY.md +23 -0
  88. package/dist/types.d.ts +4 -0
  89. package/dist/types.js +3 -0
  90. package/dist/utils/helpers.d.ts +5 -0
  91. package/dist/utils/helpers.js +53 -0
  92. package/dist/web/server.d.ts +15 -0
  93. package/dist/web/server.js +169 -0
  94. package/package.json +37 -0
  95. package/scripts/copy-assets.mjs +21 -0
  96. package/src/agent/context.ts +90 -0
  97. package/src/agent/loop.ts +291 -0
  98. package/src/agent/memory.ts +104 -0
  99. package/src/agent/skills.ts +121 -0
  100. package/src/agent/subagent.ts +96 -0
  101. package/src/agent/tools/base.ts +59 -0
  102. package/src/agent/tools/cron.ts +79 -0
  103. package/src/agent/tools/filesystem.ts +93 -0
  104. package/src/agent/tools/message.ts +57 -0
  105. package/src/agent/tools/registry.ts +36 -0
  106. package/src/agent/tools/shell.ts +69 -0
  107. package/src/agent/tools/spawn.ts +37 -0
  108. package/src/agent/tools/web.ts +108 -0
  109. package/src/bus/async-queue.ts +20 -0
  110. package/src/bus/events.ts +23 -0
  111. package/src/bus/queue.ts +31 -0
  112. package/src/channels/base.ts +36 -0
  113. package/src/channels/discord.ts +156 -0
  114. package/src/channels/manager.ts +70 -0
  115. package/src/channels/stub.ts +20 -0
  116. package/src/channels/telegram.ts +120 -0
  117. package/src/cli/commands.ts +581 -0
  118. package/src/config/loader.ts +58 -0
  119. package/src/config/schema.ts +144 -0
  120. package/src/cron/service.ts +190 -0
  121. package/src/cron/types.ts +36 -0
  122. package/src/dashboard/index.html +1567 -0
  123. package/src/heartbeat/service.ts +95 -0
  124. package/src/index.ts +6 -0
  125. package/src/providers/base.ts +43 -0
  126. package/src/providers/custom-provider.ts +46 -0
  127. package/src/providers/litellm-provider.ts +131 -0
  128. package/src/providers/registry.ts +48 -0
  129. package/src/session/manager.ts +129 -0
  130. package/src/skills/README.md +25 -0
  131. package/src/skills/clawhub/SKILL.md +53 -0
  132. package/src/skills/cron/SKILL.md +57 -0
  133. package/src/skills/github/SKILL.md +48 -0
  134. package/src/skills/memory/SKILL.md +31 -0
  135. package/src/skills/skill-creator/SKILL.md +371 -0
  136. package/src/skills/summarize/SKILL.md +67 -0
  137. package/src/skills/tmux/SKILL.md +121 -0
  138. package/src/skills/tmux/scripts/find-sessions.sh +112 -0
  139. package/src/skills/tmux/scripts/wait-for-text.sh +83 -0
  140. package/src/skills/weather/SKILL.md +49 -0
  141. package/src/templates/AGENTS.md +23 -0
  142. package/src/templates/HEARTBEAT.md +16 -0
  143. package/src/templates/SOUL.md +21 -0
  144. package/src/templates/TOOLS.md +15 -0
  145. package/src/templates/USER.md +49 -0
  146. package/src/templates/memory/MEMORY.md +23 -0
  147. package/src/types/prompts.d.ts +14 -0
  148. package/src/types/ws.d.ts +15 -0
  149. package/src/types.ts +5 -0
  150. package/src/utils/helpers.ts +55 -0
  151. package/src/web/server.ts +198 -0
  152. package/test/context.test.ts +27 -0
  153. package/test/cron-service.test.ts +31 -0
  154. package/test/message-tool.test.ts +10 -0
  155. package/test/providers.test.ts +43 -0
  156. package/test/tool-validation.test.ts +61 -0
  157. package/tsconfig.json +16 -0
  158. package/vitest.config.ts +8 -0
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ usage() {
5
+ cat <<'USAGE'
6
+ Usage: find-sessions.sh [-L socket-name|-S socket-path|-A] [-q pattern]
7
+
8
+ List tmux sessions on a socket (default tmux socket if none provided).
9
+
10
+ Options:
11
+ -L, --socket tmux socket name (passed to tmux -L)
12
+ -S, --socket-path tmux socket path (passed to tmux -S)
13
+ -A, --all scan all sockets under PRETTICLAW_TMUX_SOCKET_DIR
14
+ -q, --query case-insensitive substring to filter session names
15
+ -h, --help show this help
16
+ USAGE
17
+ }
18
+
19
+ socket_name=""
20
+ socket_path=""
21
+ query=""
22
+ scan_all=false
23
+ socket_dir="${PRETTICLAW_TMUX_SOCKET_DIR:-${TMPDIR:-/tmp}/pretticlaw-tmux-sockets}"
24
+
25
+ while [[ $# -gt 0 ]]; do
26
+ case "$1" in
27
+ -L|--socket) socket_name="${2-}"; shift 2 ;;
28
+ -S|--socket-path) socket_path="${2-}"; shift 2 ;;
29
+ -A|--all) scan_all=true; shift ;;
30
+ -q|--query) query="${2-}"; shift 2 ;;
31
+ -h|--help) usage; exit 0 ;;
32
+ *) echo "Unknown option: $1" >&2; usage; exit 1 ;;
33
+ esac
34
+ done
35
+
36
+ if [[ "$scan_all" == true && ( -n "$socket_name" || -n "$socket_path" ) ]]; then
37
+ echo "Cannot combine --all with -L or -S" >&2
38
+ exit 1
39
+ fi
40
+
41
+ if [[ -n "$socket_name" && -n "$socket_path" ]]; then
42
+ echo "Use either -L or -S, not both" >&2
43
+ exit 1
44
+ fi
45
+
46
+ if ! command -v tmux >/dev/null 2>&1; then
47
+ echo "tmux not found in PATH" >&2
48
+ exit 1
49
+ fi
50
+
51
+ list_sessions() {
52
+ local label="$1"; shift
53
+ local tmux_cmd=(tmux "$@")
54
+
55
+ if ! sessions="$("${tmux_cmd[@]}" list-sessions -F '#{session_name}\t#{session_attached}\t#{session_created_string}' 2>/dev/null)"; then
56
+ echo "No tmux server found on $label" >&2
57
+ return 1
58
+ fi
59
+
60
+ if [[ -n "$query" ]]; then
61
+ sessions="$(printf '%s\n' "$sessions" | grep -i -- "$query" || true)"
62
+ fi
63
+
64
+ if [[ -z "$sessions" ]]; then
65
+ echo "No sessions found on $label"
66
+ return 0
67
+ fi
68
+
69
+ echo "Sessions on $label:"
70
+ printf '%s\n' "$sessions" | while IFS=$'\t' read -r name attached created; do
71
+ attached_label=$([[ "$attached" == "1" ]] && echo "attached" || echo "detached")
72
+ printf ' - %s (%s, started %s)\n' "$name" "$attached_label" "$created"
73
+ done
74
+ }
75
+
76
+ if [[ "$scan_all" == true ]]; then
77
+ if [[ ! -d "$socket_dir" ]]; then
78
+ echo "Socket directory not found: $socket_dir" >&2
79
+ exit 1
80
+ fi
81
+
82
+ shopt -s nullglob
83
+ sockets=("$socket_dir"/*)
84
+ shopt -u nullglob
85
+
86
+ if [[ "${#sockets[@]}" -eq 0 ]]; then
87
+ echo "No sockets found under $socket_dir" >&2
88
+ exit 1
89
+ fi
90
+
91
+ exit_code=0
92
+ for sock in "${sockets[@]}"; do
93
+ if [[ ! -S "$sock" ]]; then
94
+ continue
95
+ fi
96
+ list_sessions "socket path '$sock'" -S "$sock" || exit_code=$?
97
+ done
98
+ exit "$exit_code"
99
+ fi
100
+
101
+ tmux_cmd=(tmux)
102
+ socket_label="default socket"
103
+
104
+ if [[ -n "$socket_name" ]]; then
105
+ tmux_cmd+=(-L "$socket_name")
106
+ socket_label="socket name '$socket_name'"
107
+ elif [[ -n "$socket_path" ]]; then
108
+ tmux_cmd+=(-S "$socket_path")
109
+ socket_label="socket path '$socket_path'"
110
+ fi
111
+
112
+ list_sessions "$socket_label" "${tmux_cmd[@]:1}"
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ usage() {
5
+ cat <<'USAGE'
6
+ Usage: wait-for-text.sh -t target -p pattern [options]
7
+
8
+ Poll a tmux pane for text and exit when found.
9
+
10
+ Options:
11
+ -t, --target tmux target (session:window.pane), required
12
+ -p, --pattern regex pattern to look for, required
13
+ -F, --fixed treat pattern as a fixed string (grep -F)
14
+ -T, --timeout seconds to wait (integer, default: 15)
15
+ -i, --interval poll interval in seconds (default: 0.5)
16
+ -l, --lines number of history lines to inspect (integer, default: 1000)
17
+ -h, --help show this help
18
+ USAGE
19
+ }
20
+
21
+ target=""
22
+ pattern=""
23
+ grep_flag="-E"
24
+ timeout=15
25
+ interval=0.5
26
+ lines=1000
27
+
28
+ while [[ $# -gt 0 ]]; do
29
+ case "$1" in
30
+ -t|--target) target="${2-}"; shift 2 ;;
31
+ -p|--pattern) pattern="${2-}"; shift 2 ;;
32
+ -F|--fixed) grep_flag="-F"; shift ;;
33
+ -T|--timeout) timeout="${2-}"; shift 2 ;;
34
+ -i|--interval) interval="${2-}"; shift 2 ;;
35
+ -l|--lines) lines="${2-}"; shift 2 ;;
36
+ -h|--help) usage; exit 0 ;;
37
+ *) echo "Unknown option: $1" >&2; usage; exit 1 ;;
38
+ esac
39
+ done
40
+
41
+ if [[ -z "$target" || -z "$pattern" ]]; then
42
+ echo "target and pattern are required" >&2
43
+ usage
44
+ exit 1
45
+ fi
46
+
47
+ if ! [[ "$timeout" =~ ^[0-9]+$ ]]; then
48
+ echo "timeout must be an integer number of seconds" >&2
49
+ exit 1
50
+ fi
51
+
52
+ if ! [[ "$lines" =~ ^[0-9]+$ ]]; then
53
+ echo "lines must be an integer" >&2
54
+ exit 1
55
+ fi
56
+
57
+ if ! command -v tmux >/dev/null 2>&1; then
58
+ echo "tmux not found in PATH" >&2
59
+ exit 1
60
+ fi
61
+
62
+ # End time in epoch seconds (integer, good enough for polling)
63
+ start_epoch=$(date +%s)
64
+ deadline=$((start_epoch + timeout))
65
+
66
+ while true; do
67
+ # -J joins wrapped lines, -S uses negative index to read last N lines
68
+ pane_text="$(tmux capture-pane -p -J -t "$target" -S "-${lines}" 2>/dev/null || true)"
69
+
70
+ if printf '%s\n' "$pane_text" | grep $grep_flag -- "$pattern" >/dev/null 2>&1; then
71
+ exit 0
72
+ fi
73
+
74
+ now=$(date +%s)
75
+ if (( now >= deadline )); then
76
+ echo "Timed out after ${timeout}s waiting for pattern: $pattern" >&2
77
+ echo "Last ${lines} lines from $target:" >&2
78
+ printf '%s\n' "$pane_text" >&2
79
+ exit 1
80
+ fi
81
+
82
+ sleep "$interval"
83
+ done
@@ -0,0 +1,49 @@
1
+ ---
2
+ name: weather
3
+ description: Get current weather and forecasts (no API key required).
4
+ homepage: https://wttr.in/:help
5
+ metadata: {"pretticlaw":{"emoji":"🌤️","requires":{"bins":["curl"]}}}
6
+ ---
7
+
8
+ # Weather
9
+
10
+ Two free services, no API keys needed.
11
+
12
+ ## wttr.in (primary)
13
+
14
+ Quick one-liner:
15
+ ```bash
16
+ curl -s "wttr.in/London?format=3"
17
+ # Output: London: ⛅️ +8°C
18
+ ```
19
+
20
+ Compact format:
21
+ ```bash
22
+ curl -s "wttr.in/London?format=%l:+%c+%t+%h+%w"
23
+ # Output: London: ⛅️ +8°C 71% ↙5km/h
24
+ ```
25
+
26
+ Full forecast:
27
+ ```bash
28
+ curl -s "wttr.in/London?T"
29
+ ```
30
+
31
+ Format codes: `%c` condition · `%t` temp · `%h` humidity · `%w` wind · `%l` location · `%m` moon
32
+
33
+ Tips:
34
+ - URL-encode spaces: `wttr.in/New+York`
35
+ - Airport codes: `wttr.in/JFK`
36
+ - Units: `?m` (metric) `?u` (USCS)
37
+ - Today only: `?1` · Current only: `?0`
38
+ - PNG: `curl -s "wttr.in/Berlin.png" -o /tmp/weather.png`
39
+
40
+ ## Open-Meteo (fallback, JSON)
41
+
42
+ Free, no key, good for programmatic use:
43
+ ```bash
44
+ curl -s "https://api.open-meteo.com/v1/forecast?latitude=51.5&longitude=-0.12&current_weather=true"
45
+ ```
46
+
47
+ Find coordinates for a city, then query. Returns JSON with temp, windspeed, weathercode.
48
+
49
+ Docs: https://open-meteo.com/en/docs
@@ -0,0 +1,23 @@
1
+ # Agent Instructions
2
+
3
+ You are a helpful AI assistant. Be concise, accurate, and friendly.
4
+
5
+ ## Scheduled Reminders
6
+
7
+ When user asks for a reminder at a specific time, use `exec` to run:
8
+ ```
9
+ pretticlaw cron add --name "reminder" --message "Your message" --at "YYYY-MM-DDTHH:MM:SS" --deliver --to "USER_ID" --channel "CHANNEL"
10
+ ```
11
+ Get USER_ID and CHANNEL from the current session (e.g., `8281248569` and `telegram` from `telegram:8281248569`).
12
+
13
+ **Do NOT just write reminders to MEMORY.md** — that won't trigger actual notifications.
14
+
15
+ ## Heartbeat Tasks
16
+
17
+ `HEARTBEAT.md` is checked every 30 minutes. Use file tools to manage periodic tasks:
18
+
19
+ - **Add**: `edit_file` to append new tasks
20
+ - **Remove**: `edit_file` to delete completed tasks
21
+ - **Rewrite**: `write_file` to replace all tasks
22
+
23
+ When the user asks for a recurring/periodic task, update `HEARTBEAT.md` instead of creating a one-time cron reminder.
@@ -0,0 +1,16 @@
1
+ # Heartbeat Tasks
2
+
3
+ This file is checked every 30 minutes by your pretticlaw agent.
4
+ Add tasks below that you want the agent to work on periodically.
5
+
6
+ If this file has no tasks (only headers and comments), the agent will skip the heartbeat.
7
+
8
+ ## Active Tasks
9
+
10
+ <!-- Add your periodic tasks below this line -->
11
+
12
+
13
+ ## Completed
14
+
15
+ <!-- Move completed tasks here or delete them -->
16
+
@@ -0,0 +1,21 @@
1
+ # Soul
2
+
3
+ I am pretticlaw 💅, a personal AI assistant.
4
+
5
+ ## Personality
6
+
7
+ - Helpful and friendly
8
+ - Concise and to the point
9
+ - Curious and eager to learn
10
+
11
+ ## Values
12
+
13
+ - Accuracy over speed
14
+ - User privacy and safety
15
+ - Transparency in actions
16
+
17
+ ## Communication Style
18
+
19
+ - Be clear and direct
20
+ - Explain reasoning when helpful
21
+ - Ask clarifying questions when needed
@@ -0,0 +1,15 @@
1
+ # Tool Usage Notes
2
+
3
+ Tool signatures are provided automatically via function calling.
4
+ This file documents non-obvious constraints and usage patterns.
5
+
6
+ ## exec — Safety Limits
7
+
8
+ - Commands have a configurable timeout (default 60s)
9
+ - Dangerous commands are blocked (rm -rf, format, dd, shutdown, etc.)
10
+ - Output is truncated at 10,000 characters
11
+ - `restrictToWorkspace` config can limit file access to the workspace
12
+
13
+ ## cron — Scheduled Reminders
14
+
15
+ - Please refer to cron skill for usage.
@@ -0,0 +1,49 @@
1
+ # User Profile
2
+
3
+ Information about the user to help personalize interactions.
4
+
5
+ ## Basic Information
6
+
7
+ - **Name**: (your name)
8
+ - **Timezone**: (your timezone, e.g., UTC+8)
9
+ - **Language**: (preferred language)
10
+
11
+ ## Preferences
12
+
13
+ ### Communication Style
14
+
15
+ - [ ] Casual
16
+ - [ ] Professional
17
+ - [ ] Technical
18
+
19
+ ### Response Length
20
+
21
+ - [ ] Brief and concise
22
+ - [ ] Detailed explanations
23
+ - [ ] Adaptive based on question
24
+
25
+ ### Technical Level
26
+
27
+ - [ ] Beginner
28
+ - [ ] Intermediate
29
+ - [ ] Expert
30
+
31
+ ## Work Context
32
+
33
+ - **Primary Role**: (your role, e.g., developer, researcher)
34
+ - **Main Projects**: (what you're working on)
35
+ - **Tools You Use**: (IDEs, languages, frameworks)
36
+
37
+ ## Topics of Interest
38
+
39
+ -
40
+ -
41
+ -
42
+
43
+ ## Special Instructions
44
+
45
+ (Any specific instructions for how the assistant should behave)
46
+
47
+ ---
48
+
49
+ *Edit this file to customize pretticlaw's behavior for your needs.*
@@ -0,0 +1,23 @@
1
+ # Long-term Memory
2
+
3
+ This file stores important information that should persist across sessions.
4
+
5
+ ## User Information
6
+
7
+ (Important facts about the user)
8
+
9
+ ## Preferences
10
+
11
+ (User preferences learned over time)
12
+
13
+ ## Project Context
14
+
15
+ (Information about ongoing projects)
16
+
17
+ ## Important Notes
18
+
19
+ (Things to remember)
20
+
21
+ ---
22
+
23
+ *This file is automatically updated by pretticlaw when important information should be remembered.*
@@ -0,0 +1,14 @@
1
+ declare module "prompts" {
2
+ export type PromptChoice = { title: string; value: string };
3
+ export type PromptQuestion = {
4
+ type: "select" | "password" | "text" | "confirm";
5
+ name: string;
6
+ message: string;
7
+ choices?: PromptChoice[];
8
+ initial?: string | boolean;
9
+ validate?: (value: string) => true | string;
10
+ };
11
+
12
+ const prompts: (question: PromptQuestion) => Promise<Record<string, unknown>>;
13
+ export default prompts;
14
+ }
@@ -0,0 +1,15 @@
1
+ declare module "ws" {
2
+ export type RawData = string | Buffer | ArrayBuffer | Buffer[];
3
+
4
+ type Listener = (...args: any[]) => void;
5
+
6
+ export default class WebSocket {
7
+ static readonly OPEN: number;
8
+ readonly readyState: number;
9
+ constructor(url: string);
10
+ on(event: "message", listener: (data: RawData) => void): this;
11
+ on(event: "close" | "error" | string, listener: Listener): this;
12
+ send(data: string | Buffer): void;
13
+ close(): void;
14
+ }
15
+ }
package/src/types.ts ADDED
@@ -0,0 +1,5 @@
1
+ export type Json = string | number | boolean | null | Json[] | { [k: string]: Json };
2
+
3
+ export function nowIso(): string {
4
+ return new Date().toISOString();
5
+ }
@@ -0,0 +1,55 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import os from "node:os";
4
+ import { fileURLToPath } from "node:url";
5
+
6
+ const UNSAFE = /[<>:"/\\|?*]/g;
7
+
8
+ export function ensureDir(dir: string): string {
9
+ fs.mkdirSync(dir, { recursive: true });
10
+ return dir;
11
+ }
12
+
13
+ export function getDataPath(): string {
14
+ return ensureDir(path.join(os.homedir(), ".pretticlaw"));
15
+ }
16
+
17
+ export function getWorkspacePath(workspace?: string): string {
18
+ const p = workspace ? workspace.replace(/^~(?=$|[\\/])/, os.homedir()) : path.join(os.homedir(), ".pretticlaw", "workspace");
19
+ return ensureDir(path.resolve(p));
20
+ }
21
+
22
+ export function safeFilename(name: string): string {
23
+ return name.replace(UNSAFE, "_").trim();
24
+ }
25
+
26
+ export function syncWorkspaceTemplates(workspace: string, silent = false): string[] {
27
+ const here = path.dirname(fileURLToPath(import.meta.url));
28
+ const candidates = [
29
+ path.resolve(here, "../templates"),
30
+ path.resolve(here, "../../src/templates"),
31
+ path.resolve(process.cwd(), "dist/templates"),
32
+ path.resolve(process.cwd(), "src/templates"),
33
+ ];
34
+ const templates = candidates.find((p) => fs.existsSync(path.join(p, "AGENTS.md")));
35
+ if (!templates) return [];
36
+ const created: string[] = [];
37
+ const writeIfMissing = (src: string | null, dest: string) => {
38
+ if (fs.existsSync(dest)) return;
39
+ ensureDir(path.dirname(dest));
40
+ if (src) fs.copyFileSync(src, dest);
41
+ else fs.writeFileSync(dest, "", "utf8");
42
+ created.push(path.relative(workspace, dest));
43
+ };
44
+
45
+ const md = ["AGENTS.md", "SOUL.md", "USER.md", "TOOLS.md", "HEARTBEAT.md"];
46
+ for (const f of md) writeIfMissing(path.join(templates, f), path.join(workspace, f));
47
+ writeIfMissing(path.join(templates, "memory", "MEMORY.md"), path.join(workspace, "memory", "MEMORY.md"));
48
+ writeIfMissing(null, path.join(workspace, "memory", "HISTORY.md"));
49
+ ensureDir(path.join(workspace, "skills"));
50
+
51
+ if (!silent) {
52
+ for (const item of created) console.log(` Created ${item}`);
53
+ }
54
+ return created;
55
+ }
@@ -0,0 +1,198 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
5
+ import type { AgentLoop } from "../agent/loop.js";
6
+ import type { CronService } from "../cron/service.js";
7
+ import type { Config } from "../config/schema.js";
8
+ import { loadConfig, saveConfig } from "../config/loader.js";
9
+ import type { SessionManager } from "../session/manager.js";
10
+
11
+ type JsonObj = Record<string, unknown>;
12
+
13
+ function dashboardDir(): string {
14
+ const here = path.dirname(fileURLToPath(import.meta.url));
15
+ const candidates = [
16
+ path.resolve(here, "../dashboard"),
17
+ path.resolve(here, "../../src/dashboard"),
18
+ path.resolve(process.cwd(), "dist/dashboard"),
19
+ path.resolve(process.cwd(), "src/dashboard"),
20
+ ];
21
+ const found = candidates.find((p) => fs.existsSync(path.join(p, "index.html")));
22
+ return found ?? candidates[candidates.length - 1];
23
+ }
24
+
25
+ function readBody(req: IncomingMessage): Promise<JsonObj> {
26
+ return new Promise((resolve) => {
27
+ let buf = "";
28
+ req.on("data", (c) => {
29
+ buf += c.toString();
30
+ if (buf.length > 2_000_000) req.destroy();
31
+ });
32
+ req.on("end", () => {
33
+ try {
34
+ resolve(buf ? (JSON.parse(buf) as JsonObj) : {});
35
+ } catch {
36
+ resolve({});
37
+ }
38
+ });
39
+ req.on("error", () => resolve({}));
40
+ });
41
+ }
42
+
43
+ function sendJson(res: ServerResponse, status: number, data: unknown): void {
44
+ res.statusCode = status;
45
+ res.setHeader("Content-Type", "application/json; charset=utf-8");
46
+ res.end(JSON.stringify(data));
47
+ }
48
+
49
+ function contentType(file: string): string {
50
+ if (file.endsWith(".html")) return "text/html; charset=utf-8";
51
+ if (file.endsWith(".js")) return "text/javascript; charset=utf-8";
52
+ if (file.endsWith(".css")) return "text/css; charset=utf-8";
53
+ if (file.endsWith(".json")) return "application/json; charset=utf-8";
54
+ if (file.endsWith(".svg")) return "image/svg+xml";
55
+ return "application/octet-stream";
56
+ }
57
+
58
+ export function startDashboardServer(input: {
59
+ agent: AgentLoop;
60
+ cron: CronService;
61
+ config: Config;
62
+ sessionManager: SessionManager;
63
+ sessionKey: string;
64
+ port?: number;
65
+ }): { close: () => Promise<void>; port: number } {
66
+ const staticDir = dashboardDir();
67
+
68
+ const server = createServer(async (req, res) => {
69
+ const method = req.method || "GET";
70
+ const url = new URL(req.url || "/", `http://${req.headers.host || "localhost"}`);
71
+ const pathname = url.pathname;
72
+
73
+ if (method === "GET" && pathname === "/api/status") {
74
+ const cfg = loadConfig();
75
+ return sendJson(res, 200, {
76
+ provider: cfg.agents.defaults.provider,
77
+ model: cfg.agents.defaults.model,
78
+ cron: input.cron.status(),
79
+ channels: cfg.channels,
80
+ gateway: cfg.gateway,
81
+ });
82
+ }
83
+
84
+ if (method === "GET" && pathname === "/api/history") {
85
+ const session = input.sessionManager.getOrCreate(input.sessionKey);
86
+ // Attach tool_calls (full objects) if present
87
+ type MsgWithTools = typeof session.messages[number] & { tool_calls?: any[] };
88
+ const messages: MsgWithTools[] = (session.messages || []).map(msg => {
89
+ if (msg && Array.isArray((msg as any).tool_calls)) {
90
+ return { ...msg, tool_calls: (msg as any).tool_calls };
91
+ }
92
+ return msg as MsgWithTools;
93
+ });
94
+ return sendJson(res, 200, { messages });
95
+ }
96
+
97
+ if (method === "POST" && pathname === "/api/chat") {
98
+ const body = await readBody(req);
99
+ const message = String(body.message ?? "").trim();
100
+ const session = String(body.session ?? "web:dashboard");
101
+ if (!message) return sendJson(res, 400, { error: "message required" });
102
+ const progress: Array<{ content: string; toolHint: boolean; tool_calls?: any[] }> = [];
103
+ try {
104
+ const response = await input.agent.processDirect(
105
+ message,
106
+ session,
107
+ "cli",
108
+ "dashboard",
109
+ async (content, meta) => {
110
+ // meta.tool_calls may exist (array of tool call objects)
111
+ progress.push({ content, toolHint: !!meta?.toolHint, tool_calls: meta?.tool_calls });
112
+ },
113
+ );
114
+ return sendJson(res, 200, { response, progress });
115
+ } catch (err) {
116
+ return sendJson(res, 500, { error: String(err) });
117
+ }
118
+ }
119
+
120
+ if (method === "GET" && pathname === "/api/config") {
121
+ return sendJson(res, 200, loadConfig());
122
+ }
123
+
124
+ if (method === "PUT" && pathname === "/api/config") {
125
+ const raw = await readBody(req);
126
+ const cfg = raw as unknown as Config;
127
+ saveConfig(cfg);
128
+ return sendJson(res, 200, { ok: true });
129
+ }
130
+
131
+ if (method === "GET" && pathname === "/api/cron/jobs") {
132
+ const includeDisabled = url.searchParams.get("all") === "1";
133
+ return sendJson(res, 200, input.cron.listJobs(includeDisabled));
134
+ }
135
+
136
+ if (method === "POST" && pathname === "/api/cron/jobs") {
137
+ const body = await readBody(req);
138
+ try {
139
+ const job = input.cron.addJob({
140
+ name: String(body.name ?? "job"),
141
+ schedule: body.schedule as any,
142
+ message: String(body.message ?? ""),
143
+ deliver: !!body.deliver,
144
+ channel: body.channel ? String(body.channel) : undefined,
145
+ to: body.to ? String(body.to) : undefined,
146
+ deleteAfterRun: !!body.deleteAfterRun,
147
+ });
148
+ return sendJson(res, 200, job);
149
+ } catch (err) {
150
+ return sendJson(res, 400, { error: String(err) });
151
+ }
152
+ }
153
+
154
+ const cronIdMatch = pathname.match(/^\/api\/cron\/jobs\/([^/]+)$/);
155
+ if (cronIdMatch && method === "DELETE") {
156
+ return sendJson(res, 200, { ok: input.cron.removeJob(cronIdMatch[1]) });
157
+ }
158
+
159
+ const cronEnableMatch = pathname.match(/^\/api\/cron\/jobs\/([^/]+)\/enable$/);
160
+ if (cronEnableMatch && method === "POST") {
161
+ const body = await readBody(req);
162
+ const enabled = body.enabled !== false;
163
+ const job = input.cron.enableJob(cronEnableMatch[1], enabled);
164
+ if (!job) return sendJson(res, 404, { error: "not found" });
165
+ return sendJson(res, 200, job);
166
+ }
167
+
168
+ const cronRunMatch = pathname.match(/^\/api\/cron\/jobs\/([^/]+)\/run$/);
169
+ if (cronRunMatch && method === "POST") {
170
+ const body = await readBody(req);
171
+ const ok = await input.cron.runJob(cronRunMatch[1], !!body.force);
172
+ return sendJson(res, 200, { ok });
173
+ }
174
+
175
+ let filePath = path.join(staticDir, pathname === "/" ? "index.html" : pathname.slice(1));
176
+ if (!filePath.startsWith(staticDir)) filePath = path.join(staticDir, "index.html");
177
+ if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory()) {
178
+ filePath = path.join(staticDir, "index.html");
179
+ }
180
+ if (!fs.existsSync(filePath)) {
181
+ res.statusCode = 404;
182
+ res.end("Dashboard assets not found. Run npm run build.");
183
+ return;
184
+ }
185
+
186
+ res.statusCode = 200;
187
+ res.setHeader("Content-Type", contentType(filePath));
188
+ fs.createReadStream(filePath).pipe(res);
189
+ });
190
+
191
+ const port = input.port ?? 6767;
192
+ server.listen(port);
193
+
194
+ return {
195
+ port,
196
+ close: () => new Promise<void>((resolve, reject) => server.close((err) => (err ? reject(err) : resolve()))),
197
+ };
198
+ }