os-context 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.
- package/LICENSE +110 -0
- package/README.md +99 -0
- package/dist/index.js +811 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-MIT
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright (c) 2025 Treadie, Inc
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the MIT license that is effective on the second anniversary of the date we make
|
|
91
|
+
the Software available. On or after that date, you may use the Software under
|
|
92
|
+
the MIT license, in which case the following will apply:
|
|
93
|
+
|
|
94
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
95
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
96
|
+
the Software without restriction, including without limitation the rights to
|
|
97
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
98
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
99
|
+
so, subject to the following conditions:
|
|
100
|
+
|
|
101
|
+
The above copyright notice and this permission notice shall be included in all
|
|
102
|
+
copies or substantial portions of the Software.
|
|
103
|
+
|
|
104
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
105
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
106
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
107
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
108
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
109
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
110
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# OS Context
|
|
2
|
+
|
|
3
|
+
A fast macOS & Linux CLI that prints a single JSON object describing your current local context for agents. Privacy-respecting, read-only, no network calls to external services.
|
|
4
|
+
|
|
5
|
+
OS Context gives AI agents a quick, local snapshot of your machine (app, window, clipboard, battery, network, etc.) so they can give context-aware answers without screenshots or daemons. Sensitive data is opt-in; default output is minimal and private.
|
|
6
|
+
|
|
7
|
+
## Goals
|
|
8
|
+
|
|
9
|
+
- **Single command**: `context` outputs JSON to stdout.
|
|
10
|
+
- **Fast**: Target <300ms typical; local-only, no external network.
|
|
11
|
+
- **Read-only**: No daemon, no always-on logging.
|
|
12
|
+
- **Privacy**: No screenshots, keystrokes, microphone/camera, or recording.
|
|
13
|
+
- **Clear permissions**: Only request macOS permissions when you use the matching flag (e.g. `--frontmost-window`, `--calendar`).
|
|
14
|
+
- **Feature flags**: Sensitive data (clipboard, window title, calendar, reminders) is opt-in.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install
|
|
20
|
+
npm run build
|
|
21
|
+
# Or link globally: npm link
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
**Default (safe core context only):**
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
context
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
**Pretty-print with frontmost window and clipboard, redact sensitive fields:**
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
context --pretty --frontmost-window --clipboard --redact
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**All optional features and debug timings:**
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
context --pretty --clipboard --frontmost-window --apps \
|
|
42
|
+
--battery --network --calendar --reminders \
|
|
43
|
+
--redact --debug
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Options
|
|
47
|
+
|
|
48
|
+
| Flag | Description |
|
|
49
|
+
|------|-------------|
|
|
50
|
+
| `--pretty` | Pretty-print JSON. |
|
|
51
|
+
| `--clipboard` | Include clipboard text (and types). |
|
|
52
|
+
| `--frontmost-window` | Include frontmost window title (requires Accessibility). |
|
|
53
|
+
| `--apps` | Include list of running apps (name, bundle_id, pid). |
|
|
54
|
+
| `--battery` | Include battery percentage and charging state. |
|
|
55
|
+
| `--network` | Include primary interface, SSID, local reachability. |
|
|
56
|
+
| `--calendar` | Include next calendar events (requires Calendar permission). |
|
|
57
|
+
| `--reminders` | Include reminders (requires Reminders permission). |
|
|
58
|
+
| `--redact` | Redact sensitive strings; output SHA-256 + length only. |
|
|
59
|
+
| `--timeout-ms <n>` | Per-module timeout in ms (default 250). |
|
|
60
|
+
| `--debug` | Include per-module timings in `_debug.timings_ms`. |
|
|
61
|
+
|
|
62
|
+
## Exit codes
|
|
63
|
+
|
|
64
|
+
- **0** — Success.
|
|
65
|
+
- **2** — Required permission missing (e.g. Accessibility or Calendar denied).
|
|
66
|
+
- **3** — Timeout.
|
|
67
|
+
- **4** — Other error.
|
|
68
|
+
|
|
69
|
+
JSON is always printed, even when exit code is non-zero.
|
|
70
|
+
|
|
71
|
+
## Platform support
|
|
72
|
+
|
|
73
|
+
- **macOS**: Full support. Calendar and Reminders are macOS-only (AppleScript).
|
|
74
|
+
- **Linux**: Host, frontmost (X11 via `xdotool`), apps (`ps`), clipboard (`xclip` or `xsel`), battery (`/sys/class/power_supply`), network (`ip`, `iwgetid`). Window title on Linux requires X11; on Wayland `xdotool` may not work. Calendar/Reminders return empty on Linux.
|
|
75
|
+
- **Optional Linux deps**: `xdotool` (frontmost/window), `xclip` or `xsel` (clipboard), `iwgetid` (WiFi SSID).
|
|
76
|
+
|
|
77
|
+
## Permissions
|
|
78
|
+
|
|
79
|
+
- **Default run** does not require Accessibility, Calendar, or Reminders. It only reads OS version, machine, locale, timezone, and frontmost app name/bundle ID.
|
|
80
|
+
- **Accessibility** (macOS) is only needed for `--frontmost-window` (to read window titles).
|
|
81
|
+
- **Calendar / Reminders** (macOS) are only used when you pass `--calendar` or `--reminders`.
|
|
82
|
+
|
|
83
|
+
See [docs/permissions.md](docs/permissions.md) for details and troubleshooting.
|
|
84
|
+
|
|
85
|
+
## Output schema
|
|
86
|
+
|
|
87
|
+
See [docs/schema.md](docs/schema.md) for the full JSON schema. Top-level keys include `schema_version`, `generated_at`, `host`, `frontmost`, optional `apps`, `clipboard`, `battery`, `network`, `calendar`, `reminders`, `permissions`, `warnings`, `errors`, and optionally `_debug`.
|
|
88
|
+
|
|
89
|
+
## Development
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
npm run dev -- --pretty
|
|
93
|
+
npm run build
|
|
94
|
+
npm test
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
[FSL-1.1-MIT](LICENSE)
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,811 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { Command } from "commander";
|
|
5
|
+
|
|
6
|
+
// src/schema.ts
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
var SCHEMA_VERSION = "0.1.0";
|
|
9
|
+
var HostSchema = z.object({
|
|
10
|
+
os: z.enum(["macos", "linux"]),
|
|
11
|
+
os_version: z.string(),
|
|
12
|
+
machine: z.string(),
|
|
13
|
+
locale: z.string(),
|
|
14
|
+
timezone: z.string()
|
|
15
|
+
});
|
|
16
|
+
var FrontmostSchema = z.object({
|
|
17
|
+
app_name: z.string(),
|
|
18
|
+
bundle_id: z.string(),
|
|
19
|
+
window_title: z.string().optional(),
|
|
20
|
+
window_title_sha256: z.string().optional(),
|
|
21
|
+
window_title_length: z.number().optional()
|
|
22
|
+
});
|
|
23
|
+
var AppEntrySchema = z.object({
|
|
24
|
+
name: z.string(),
|
|
25
|
+
bundle_id: z.string(),
|
|
26
|
+
pid: z.number()
|
|
27
|
+
});
|
|
28
|
+
var ClipboardSchema = z.object({
|
|
29
|
+
available: z.boolean(),
|
|
30
|
+
types: z.array(z.string()),
|
|
31
|
+
text: z.string().optional(),
|
|
32
|
+
text_sha256: z.string().optional(),
|
|
33
|
+
text_length: z.number().optional()
|
|
34
|
+
});
|
|
35
|
+
var BatterySchema = z.object({
|
|
36
|
+
percentage: z.number(),
|
|
37
|
+
is_charging: z.boolean(),
|
|
38
|
+
power_source: z.enum(["ac", "battery", "unknown"])
|
|
39
|
+
});
|
|
40
|
+
var NetworkSchema = z.object({
|
|
41
|
+
primary_interface: z.string(),
|
|
42
|
+
ssid: z.string().optional(),
|
|
43
|
+
has_internet: z.boolean()
|
|
44
|
+
});
|
|
45
|
+
var CalendarEventSchema = z.object({
|
|
46
|
+
start: z.string(),
|
|
47
|
+
end: z.string(),
|
|
48
|
+
title: z.string(),
|
|
49
|
+
location: z.string().optional()
|
|
50
|
+
});
|
|
51
|
+
var ReminderSchema = z.object({
|
|
52
|
+
title: z.string(),
|
|
53
|
+
due: z.string().optional(),
|
|
54
|
+
list: z.string().optional()
|
|
55
|
+
});
|
|
56
|
+
var PermissionStateSchema = z.enum([
|
|
57
|
+
"granted",
|
|
58
|
+
"denied",
|
|
59
|
+
"not_requested",
|
|
60
|
+
"unknown"
|
|
61
|
+
]);
|
|
62
|
+
var PermissionsSchema = z.object({
|
|
63
|
+
accessibility: PermissionStateSchema,
|
|
64
|
+
calendar: PermissionStateSchema,
|
|
65
|
+
reminders: PermissionStateSchema
|
|
66
|
+
});
|
|
67
|
+
var StructuredErrorSchema = z.object({
|
|
68
|
+
module: z.string(),
|
|
69
|
+
message: z.string(),
|
|
70
|
+
code: z.string().optional()
|
|
71
|
+
});
|
|
72
|
+
var DebugSchema = z.object({
|
|
73
|
+
timings_ms: z.record(z.string(), z.number())
|
|
74
|
+
});
|
|
75
|
+
var OutputSchema = z.object({
|
|
76
|
+
schema_version: z.string(),
|
|
77
|
+
generated_at: z.string(),
|
|
78
|
+
host: HostSchema.optional(),
|
|
79
|
+
frontmost: FrontmostSchema.optional(),
|
|
80
|
+
apps: z.array(AppEntrySchema).optional(),
|
|
81
|
+
clipboard: ClipboardSchema.optional(),
|
|
82
|
+
battery: BatterySchema.optional(),
|
|
83
|
+
network: NetworkSchema.optional(),
|
|
84
|
+
calendar: z.array(CalendarEventSchema).optional(),
|
|
85
|
+
reminders: z.array(ReminderSchema).optional(),
|
|
86
|
+
permissions: PermissionsSchema,
|
|
87
|
+
warnings: z.array(z.string()).optional(),
|
|
88
|
+
errors: z.array(StructuredErrorSchema).optional(),
|
|
89
|
+
_debug: DebugSchema.optional()
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// src/modules/permissions.ts
|
|
93
|
+
var DEFAULT = "not_requested";
|
|
94
|
+
function createPermissionsState() {
|
|
95
|
+
return {
|
|
96
|
+
accessibility: DEFAULT,
|
|
97
|
+
calendar: DEFAULT,
|
|
98
|
+
reminders: DEFAULT
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function updatePermission(state, key, value) {
|
|
102
|
+
state[key] = value;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/util/exec.ts
|
|
106
|
+
import { execa } from "execa";
|
|
107
|
+
async function run(cmd, args, options) {
|
|
108
|
+
try {
|
|
109
|
+
const result = await execa(cmd, args, {
|
|
110
|
+
timeout: options.timeoutMs,
|
|
111
|
+
reject: false,
|
|
112
|
+
all: false
|
|
113
|
+
});
|
|
114
|
+
return {
|
|
115
|
+
stdout: result.stdout ?? "",
|
|
116
|
+
stderr: result.stderr ?? "",
|
|
117
|
+
ok: result.exitCode === 0,
|
|
118
|
+
timedOut: result.timedOut
|
|
119
|
+
};
|
|
120
|
+
} catch (err) {
|
|
121
|
+
const e = err;
|
|
122
|
+
return {
|
|
123
|
+
stdout: e.stdout ?? "",
|
|
124
|
+
stderr: e.stderr ?? "",
|
|
125
|
+
ok: false,
|
|
126
|
+
timedOut: e.timedOut ?? false
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/util/time.ts
|
|
132
|
+
function elapsedMs(start) {
|
|
133
|
+
return Math.round(performance.now() - start);
|
|
134
|
+
}
|
|
135
|
+
function timer() {
|
|
136
|
+
const start = performance.now();
|
|
137
|
+
return {
|
|
138
|
+
elapsed: () => elapsedMs(start)
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/util/platform.ts
|
|
143
|
+
function getPlatform() {
|
|
144
|
+
const p = process.platform;
|
|
145
|
+
if (p === "darwin") return "darwin";
|
|
146
|
+
if (p === "linux") return "linux";
|
|
147
|
+
return "unsupported";
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// src/modules/host.ts
|
|
151
|
+
async function collectHostDarwin(timeoutMs) {
|
|
152
|
+
const t = timer();
|
|
153
|
+
const warnings = [];
|
|
154
|
+
try {
|
|
155
|
+
const [osVersion, machine] = await Promise.all([
|
|
156
|
+
run("sw_vers", ["-productVersion"], { timeoutMs }),
|
|
157
|
+
run("uname", ["-m"], { timeoutMs })
|
|
158
|
+
]);
|
|
159
|
+
if (osVersion.timedOut || machine.timedOut) {
|
|
160
|
+
return {
|
|
161
|
+
warnings: [...warnings, "host: timeout"],
|
|
162
|
+
error: { module: "host", message: "Timeout", code: "timeout" },
|
|
163
|
+
timingMs: t.elapsed()
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
const os_version = osVersion.ok ? osVersion.stdout.trim() || "unknown" : "unknown";
|
|
167
|
+
const machine_arch = machine.ok ? machine.stdout.trim() || "unknown" : "unknown";
|
|
168
|
+
const locale = typeof Intl !== "undefined" && Intl.DateTimeFormat ? new Intl.DateTimeFormat().resolvedOptions().locale : "en-US";
|
|
169
|
+
const tz = typeof Intl !== "undefined" && Intl.DateTimeFormat ? new Intl.DateTimeFormat().resolvedOptions().timeZone : "UTC";
|
|
170
|
+
const data = {
|
|
171
|
+
os: "macos",
|
|
172
|
+
os_version,
|
|
173
|
+
machine: machine_arch,
|
|
174
|
+
locale,
|
|
175
|
+
timezone: tz
|
|
176
|
+
};
|
|
177
|
+
return { data, warnings: warnings.length ? warnings : void 0, timingMs: t.elapsed() };
|
|
178
|
+
} catch (err) {
|
|
179
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
180
|
+
return {
|
|
181
|
+
warnings,
|
|
182
|
+
error: { module: "host", message, code: "error" },
|
|
183
|
+
timingMs: t.elapsed()
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function parseOsRelease(stdout) {
|
|
188
|
+
const version = stdout.match(/^VERSION_ID="?([^"\n]+)"?/m)?.[1] ?? stdout.match(/^PRETTY_NAME="?([^"\n]+)"?/m)?.[1] ?? "unknown";
|
|
189
|
+
return { version };
|
|
190
|
+
}
|
|
191
|
+
async function collectHostLinux(timeoutMs) {
|
|
192
|
+
const t = timer();
|
|
193
|
+
const warnings = [];
|
|
194
|
+
try {
|
|
195
|
+
const [osRelease, machine] = await Promise.all([
|
|
196
|
+
run("cat", ["/etc/os-release"], { timeoutMs }),
|
|
197
|
+
run("uname", ["-m"], { timeoutMs })
|
|
198
|
+
]);
|
|
199
|
+
if (osRelease.timedOut || machine.timedOut) {
|
|
200
|
+
return {
|
|
201
|
+
warnings: [...warnings, "host: timeout"],
|
|
202
|
+
error: { module: "host", message: "Timeout", code: "timeout" },
|
|
203
|
+
timingMs: t.elapsed()
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
const { version } = osRelease.ok ? parseOsRelease(osRelease.stdout) : { version: "unknown" };
|
|
207
|
+
const machine_arch = machine.ok ? machine.stdout.trim() || "unknown" : "unknown";
|
|
208
|
+
const locale = typeof Intl !== "undefined" && Intl.DateTimeFormat ? new Intl.DateTimeFormat().resolvedOptions().locale : "en-US";
|
|
209
|
+
const tz = typeof Intl !== "undefined" && Intl.DateTimeFormat ? new Intl.DateTimeFormat().resolvedOptions().timeZone : "UTC";
|
|
210
|
+
const data = {
|
|
211
|
+
os: "linux",
|
|
212
|
+
os_version: version,
|
|
213
|
+
machine: machine_arch,
|
|
214
|
+
locale,
|
|
215
|
+
timezone: tz
|
|
216
|
+
};
|
|
217
|
+
return { data, warnings: warnings.length ? warnings : void 0, timingMs: t.elapsed() };
|
|
218
|
+
} catch (err) {
|
|
219
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
220
|
+
return {
|
|
221
|
+
warnings,
|
|
222
|
+
error: { module: "host", message, code: "error" },
|
|
223
|
+
timingMs: t.elapsed()
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
async function collectHost(timeoutMs) {
|
|
228
|
+
const platform = getPlatform();
|
|
229
|
+
if (platform === "darwin") return collectHostDarwin(timeoutMs);
|
|
230
|
+
if (platform === "linux") return collectHostLinux(timeoutMs);
|
|
231
|
+
return {
|
|
232
|
+
error: { module: "host", message: `Unsupported platform: ${process.platform}`, code: "unsupported" },
|
|
233
|
+
timingMs: 0
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// src/util/redact.ts
|
|
238
|
+
import { createHash } from "crypto";
|
|
239
|
+
function sha256(text) {
|
|
240
|
+
return createHash("sha256").update(text, "utf8").digest("hex");
|
|
241
|
+
}
|
|
242
|
+
function redactString(value) {
|
|
243
|
+
return {
|
|
244
|
+
sha256: sha256(value),
|
|
245
|
+
length: value.length
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// src/modules/frontmost.ts
|
|
250
|
+
var ACCESSIBILITY_DENIED_PATTERNS = [
|
|
251
|
+
/not authorised to send apple events/i,
|
|
252
|
+
/accessibility/i,
|
|
253
|
+
/not allowed to send/i
|
|
254
|
+
];
|
|
255
|
+
function isAccessibilityDenied(stderr) {
|
|
256
|
+
return ACCESSIBILITY_DENIED_PATTERNS.some((p) => p.test(stderr));
|
|
257
|
+
}
|
|
258
|
+
async function collectFrontmostDarwin(options) {
|
|
259
|
+
const t = timer();
|
|
260
|
+
const timeoutMs = options.timeoutMs;
|
|
261
|
+
const includeWindow = options.includeFrontmostWindow;
|
|
262
|
+
const redact = options.redact;
|
|
263
|
+
const warnings = [];
|
|
264
|
+
try {
|
|
265
|
+
const nameResult = await run("osascript", [
|
|
266
|
+
"-e",
|
|
267
|
+
'tell application "System Events" to get name of first application process whose frontmost is true'
|
|
268
|
+
], { timeoutMs });
|
|
269
|
+
if (nameResult.timedOut) {
|
|
270
|
+
return {
|
|
271
|
+
warnings: [...warnings, "frontmost: timeout"],
|
|
272
|
+
error: { module: "frontmost", message: "Timeout", code: "timeout" },
|
|
273
|
+
timingMs: t.elapsed()
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
if (!nameResult.ok) {
|
|
277
|
+
if (isAccessibilityDenied(nameResult.stderr)) {
|
|
278
|
+
return {
|
|
279
|
+
permission: "denied",
|
|
280
|
+
error: {
|
|
281
|
+
module: "frontmost",
|
|
282
|
+
message: "Accessibility permission required for frontmost app",
|
|
283
|
+
code: "permission_denied"
|
|
284
|
+
},
|
|
285
|
+
timingMs: t.elapsed()
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
error: {
|
|
290
|
+
module: "frontmost",
|
|
291
|
+
message: nameResult.stderr.trim() || "Failed to get frontmost app",
|
|
292
|
+
code: "error"
|
|
293
|
+
},
|
|
294
|
+
timingMs: t.elapsed()
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
const appName = nameResult.stdout.trim() || "Unknown";
|
|
298
|
+
if (!appName) {
|
|
299
|
+
return {
|
|
300
|
+
data: { app_name: "Unknown", bundle_id: "unknown" },
|
|
301
|
+
timingMs: t.elapsed()
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
const idResult = await run("osascript", [
|
|
305
|
+
"-e",
|
|
306
|
+
`id of application "${appName.replace(/"/g, '\\"')}"`
|
|
307
|
+
], { timeoutMs });
|
|
308
|
+
const bundle_id = idResult.ok ? idResult.stdout.trim() || "unknown" : "unknown";
|
|
309
|
+
const frontmost = { app_name: appName, bundle_id };
|
|
310
|
+
if (includeWindow) {
|
|
311
|
+
const windowResult = await run("osascript", [
|
|
312
|
+
"-e",
|
|
313
|
+
'tell application "System Events" to tell (first application process whose frontmost is true) to get name of front window'
|
|
314
|
+
], { timeoutMs });
|
|
315
|
+
if (windowResult.timedOut) {
|
|
316
|
+
warnings.push("frontmost window: timeout");
|
|
317
|
+
} else if (!windowResult.ok) {
|
|
318
|
+
if (isAccessibilityDenied(windowResult.stderr)) {
|
|
319
|
+
return {
|
|
320
|
+
data: frontmost,
|
|
321
|
+
permission: "denied",
|
|
322
|
+
error: {
|
|
323
|
+
module: "frontmost",
|
|
324
|
+
message: "Accessibility permission required for window title",
|
|
325
|
+
code: "permission_denied"
|
|
326
|
+
},
|
|
327
|
+
warnings: warnings.length ? warnings : void 0,
|
|
328
|
+
timingMs: t.elapsed()
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
warnings.push("frontmost window: " + (windowResult.stderr.trim() || "unknown"));
|
|
332
|
+
} else {
|
|
333
|
+
const rawTitle = windowResult.stdout.trim() ?? "";
|
|
334
|
+
if (redact) {
|
|
335
|
+
const r = redactString(rawTitle);
|
|
336
|
+
frontmost.window_title_sha256 = r.sha256;
|
|
337
|
+
frontmost.window_title_length = r.length;
|
|
338
|
+
} else {
|
|
339
|
+
frontmost.window_title = rawTitle;
|
|
340
|
+
frontmost.window_title_length = rawTitle.length;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return {
|
|
345
|
+
data: frontmost,
|
|
346
|
+
warnings: warnings.length ? warnings : void 0,
|
|
347
|
+
timingMs: t.elapsed()
|
|
348
|
+
};
|
|
349
|
+
} catch (err) {
|
|
350
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
351
|
+
return {
|
|
352
|
+
warnings,
|
|
353
|
+
error: { module: "frontmost", message, code: "error" },
|
|
354
|
+
timingMs: t.elapsed()
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
async function collectFrontmostLinux(options) {
|
|
359
|
+
const t = timer();
|
|
360
|
+
const timeoutMs = options.timeoutMs;
|
|
361
|
+
const includeWindow = options.includeFrontmostWindow;
|
|
362
|
+
const redact = options.redact;
|
|
363
|
+
const warnings = [];
|
|
364
|
+
try {
|
|
365
|
+
const pidResult = await run("xdotool", ["getactivewindow", "getwindowpid"], { timeoutMs });
|
|
366
|
+
const titleResult = includeWindow ? await run("xdotool", ["getactivewindow", "getwindowname"], { timeoutMs }) : { ok: false, stdout: "", stderr: "", timedOut: false };
|
|
367
|
+
if (pidResult.timedOut) {
|
|
368
|
+
return {
|
|
369
|
+
warnings: [...warnings, "frontmost: timeout"],
|
|
370
|
+
error: { module: "frontmost", message: "Timeout", code: "timeout" },
|
|
371
|
+
timingMs: t.elapsed()
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
let appName = "Unknown";
|
|
375
|
+
let bundle_id = "unknown";
|
|
376
|
+
if (pidResult.ok && pidResult.stdout.trim()) {
|
|
377
|
+
const pid = pidResult.stdout.trim();
|
|
378
|
+
const commResult = await run("cat", [`/proc/${pid}/comm`], { timeoutMs });
|
|
379
|
+
if (commResult.ok && commResult.stdout.trim()) {
|
|
380
|
+
appName = commResult.stdout.trim().replace(/\n$/, "");
|
|
381
|
+
bundle_id = appName;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
const frontmost = { app_name: appName, bundle_id };
|
|
385
|
+
if (includeWindow && titleResult.ok) {
|
|
386
|
+
const rawTitle = titleResult.stdout.trim() ?? "";
|
|
387
|
+
if (redact) {
|
|
388
|
+
const r = redactString(rawTitle);
|
|
389
|
+
frontmost.window_title_sha256 = r.sha256;
|
|
390
|
+
frontmost.window_title_length = r.length;
|
|
391
|
+
} else {
|
|
392
|
+
frontmost.window_title = rawTitle;
|
|
393
|
+
frontmost.window_title_length = rawTitle.length;
|
|
394
|
+
}
|
|
395
|
+
} else if (includeWindow && !pidResult.ok) {
|
|
396
|
+
warnings.push("frontmost: xdotool not available or no X11 (e.g. Wayland); install xdotool for X11");
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
data: frontmost,
|
|
400
|
+
warnings: warnings.length ? warnings : void 0,
|
|
401
|
+
timingMs: t.elapsed()
|
|
402
|
+
};
|
|
403
|
+
} catch (err) {
|
|
404
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
405
|
+
return {
|
|
406
|
+
warnings,
|
|
407
|
+
error: { module: "frontmost", message, code: "error" },
|
|
408
|
+
timingMs: t.elapsed()
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
async function collectFrontmost(options) {
|
|
413
|
+
const platform = getPlatform();
|
|
414
|
+
if (platform === "darwin") return collectFrontmostDarwin(options);
|
|
415
|
+
if (platform === "linux") return collectFrontmostLinux(options);
|
|
416
|
+
return {
|
|
417
|
+
data: { app_name: "Unknown", bundle_id: "unknown" },
|
|
418
|
+
error: { module: "frontmost", message: `Unsupported platform: ${process.platform}`, code: "unsupported" },
|
|
419
|
+
timingMs: 0
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// src/modules/apps.ts
|
|
424
|
+
var MAX_APPS = 50;
|
|
425
|
+
async function collectAppsDarwin(_timeoutMs) {
|
|
426
|
+
return { data: [], timingMs: 0 };
|
|
427
|
+
}
|
|
428
|
+
async function collectAppsLinux(timeoutMs) {
|
|
429
|
+
const t = timer();
|
|
430
|
+
try {
|
|
431
|
+
const result = await run("ps", ["-e", "-o", "pid=", "-o", "comm="], { timeoutMs });
|
|
432
|
+
if (result.timedOut || !result.ok) {
|
|
433
|
+
return {
|
|
434
|
+
data: [],
|
|
435
|
+
error: result.timedOut ? { module: "apps", message: "Timeout", code: "timeout" } : { module: "apps", message: result.stderr.trim() || "Failed to list processes", code: "error" },
|
|
436
|
+
timingMs: t.elapsed()
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
const lines = result.stdout.trim().split("\n").filter(Boolean).slice(0, MAX_APPS);
|
|
440
|
+
const apps = lines.map((line) => {
|
|
441
|
+
const match = line.trim().match(/^\s*(\d+)\s+(.+)$/);
|
|
442
|
+
const pid = match ? parseInt(match[1], 10) : 0;
|
|
443
|
+
const name = match ? match[2].trim() || "unknown" : "unknown";
|
|
444
|
+
return { name, bundle_id: name, pid: Number.isNaN(pid) ? 0 : pid };
|
|
445
|
+
}).filter((a) => a.pid > 0);
|
|
446
|
+
return { data: apps, timingMs: t.elapsed() };
|
|
447
|
+
} catch (err) {
|
|
448
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
449
|
+
return {
|
|
450
|
+
data: [],
|
|
451
|
+
error: { module: "apps", message, code: "error" },
|
|
452
|
+
timingMs: t.elapsed()
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
async function collectApps(timeoutMs) {
|
|
457
|
+
const platform = getPlatform();
|
|
458
|
+
if (platform === "darwin") return collectAppsDarwin(timeoutMs);
|
|
459
|
+
if (platform === "linux") return collectAppsLinux(timeoutMs);
|
|
460
|
+
return { data: [], timingMs: 0 };
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// src/modules/clipboard.ts
|
|
464
|
+
async function collectClipboardDarwin(_options) {
|
|
465
|
+
return {
|
|
466
|
+
data: { available: false, types: [] },
|
|
467
|
+
timingMs: 0
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
async function collectClipboardLinux(options) {
|
|
471
|
+
const t = timer();
|
|
472
|
+
const timeoutMs = options.timeoutMs;
|
|
473
|
+
const redact = options.redact;
|
|
474
|
+
try {
|
|
475
|
+
let result = await run("xclip", ["-selection", "clipboard", "-o"], { timeoutMs });
|
|
476
|
+
if (!result.ok) {
|
|
477
|
+
result = await run("xsel", ["--clipboard", "--output"], { timeoutMs });
|
|
478
|
+
}
|
|
479
|
+
if (!result.ok) {
|
|
480
|
+
return {
|
|
481
|
+
data: { available: false, types: [] },
|
|
482
|
+
timingMs: t.elapsed()
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
const text = result.stdout ?? "";
|
|
486
|
+
const types = text.length > 0 ? ["text/plain"] : [];
|
|
487
|
+
const data = {
|
|
488
|
+
available: true,
|
|
489
|
+
types
|
|
490
|
+
};
|
|
491
|
+
if (text.length > 0) {
|
|
492
|
+
if (redact) {
|
|
493
|
+
const r = redactString(text);
|
|
494
|
+
data.text_sha256 = r.sha256;
|
|
495
|
+
data.text_length = r.length;
|
|
496
|
+
} else {
|
|
497
|
+
data.text = text;
|
|
498
|
+
data.text_length = text.length;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return { data, timingMs: t.elapsed() };
|
|
502
|
+
} catch {
|
|
503
|
+
return {
|
|
504
|
+
data: { available: false, types: [] },
|
|
505
|
+
timingMs: t.elapsed()
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
async function collectClipboard(options) {
|
|
510
|
+
const platform = getPlatform();
|
|
511
|
+
if (platform === "darwin") return collectClipboardDarwin(options);
|
|
512
|
+
if (platform === "linux") return collectClipboardLinux(options);
|
|
513
|
+
return {
|
|
514
|
+
data: { available: false, types: [] },
|
|
515
|
+
timingMs: 0
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// src/modules/battery.ts
|
|
520
|
+
import { readFile } from "fs/promises";
|
|
521
|
+
async function collectBatteryDarwin(_timeoutMs) {
|
|
522
|
+
return {
|
|
523
|
+
data: { percentage: 0, is_charging: false, power_source: "unknown" },
|
|
524
|
+
timingMs: 0
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
async function collectBatteryLinux(_timeoutMs) {
|
|
528
|
+
const t = timer();
|
|
529
|
+
try {
|
|
530
|
+
const base = "/sys/class/power_supply";
|
|
531
|
+
const batDir = "BAT0";
|
|
532
|
+
const capacityPath = `${base}/${batDir}/capacity`;
|
|
533
|
+
const statusPath = `${base}/${batDir}/status`;
|
|
534
|
+
const capacity = await readFile(capacityPath, "utf8").then((s) => parseInt(s.trim(), 10)).catch(() => NaN);
|
|
535
|
+
const status = await readFile(statusPath, "utf8").then((s) => s.trim().toLowerCase()).catch(() => "");
|
|
536
|
+
const percentage = Number.isNaN(capacity) ? 0 : Math.min(1, Math.max(0, capacity / 100));
|
|
537
|
+
const is_charging = status === "charging" || status === "full";
|
|
538
|
+
const power_source = status === "charging" || status === "full" ? "ac" : status === "discharging" ? "battery" : "unknown";
|
|
539
|
+
const data = { percentage, is_charging, power_source };
|
|
540
|
+
return { data, timingMs: t.elapsed() };
|
|
541
|
+
} catch {
|
|
542
|
+
return {
|
|
543
|
+
data: { percentage: 0, is_charging: false, power_source: "unknown" },
|
|
544
|
+
timingMs: t.elapsed()
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
async function collectBattery(timeoutMs) {
|
|
549
|
+
const platform = getPlatform();
|
|
550
|
+
if (platform === "darwin") return collectBatteryDarwin(timeoutMs);
|
|
551
|
+
if (platform === "linux") return collectBatteryLinux(timeoutMs);
|
|
552
|
+
return {
|
|
553
|
+
data: { percentage: 0, is_charging: false, power_source: "unknown" },
|
|
554
|
+
timingMs: 0
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// src/modules/network.ts
|
|
559
|
+
import { readFile as readFile2 } from "fs/promises";
|
|
560
|
+
async function collectNetworkDarwin(_timeoutMs) {
|
|
561
|
+
return {
|
|
562
|
+
data: { primary_interface: "", has_internet: false },
|
|
563
|
+
timingMs: 0
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
async function collectNetworkLinux(timeoutMs) {
|
|
567
|
+
const t = timer();
|
|
568
|
+
try {
|
|
569
|
+
const routeResult = await run("ip", ["route", "show", "default"], { timeoutMs });
|
|
570
|
+
const defaultLine = routeResult.ok ? routeResult.stdout.trim().split("\n")[0] : "";
|
|
571
|
+
const match = defaultLine.match(/dev\s+(\S+)/);
|
|
572
|
+
const primary_interface = match ? match[1] : "";
|
|
573
|
+
let ssid;
|
|
574
|
+
if (primary_interface) {
|
|
575
|
+
const iwResult = await run("iwgetid", ["-r", primary_interface], { timeoutMs });
|
|
576
|
+
if (iwResult.ok && iwResult.stdout.trim()) ssid = iwResult.stdout.trim();
|
|
577
|
+
}
|
|
578
|
+
let has_internet = false;
|
|
579
|
+
if (primary_interface) {
|
|
580
|
+
try {
|
|
581
|
+
const operstate = await readFile2(
|
|
582
|
+
`/sys/class/net/${primary_interface}/operstate`,
|
|
583
|
+
"utf8"
|
|
584
|
+
).then((s) => s.trim()).catch(() => "");
|
|
585
|
+
const hasCarrier = operstate === "up";
|
|
586
|
+
if (hasCarrier) {
|
|
587
|
+
const addrResult = await run("ip", ["-4", "addr", "show", primary_interface], { timeoutMs });
|
|
588
|
+
has_internet = addrResult.ok && /inet\s+\d+\.\d+\.\d+\.\d+/.test(addrResult.stdout);
|
|
589
|
+
}
|
|
590
|
+
} catch {
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
const data = { primary_interface, ssid, has_internet };
|
|
594
|
+
return { data, timingMs: t.elapsed() };
|
|
595
|
+
} catch (err) {
|
|
596
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
597
|
+
return {
|
|
598
|
+
data: { primary_interface: "", has_internet: false },
|
|
599
|
+
error: { module: "network", message, code: "error" },
|
|
600
|
+
timingMs: t.elapsed()
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
async function collectNetwork(timeoutMs) {
|
|
605
|
+
const platform = getPlatform();
|
|
606
|
+
if (platform === "darwin") return collectNetworkDarwin(timeoutMs);
|
|
607
|
+
if (platform === "linux") return collectNetworkLinux(timeoutMs);
|
|
608
|
+
return {
|
|
609
|
+
data: { primary_interface: "", has_internet: false },
|
|
610
|
+
timingMs: 0
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// src/modules/calendar.ts
|
|
615
|
+
async function collectCalendar(_options) {
|
|
616
|
+
if (getPlatform() === "linux") {
|
|
617
|
+
return { data: [], timingMs: 0 };
|
|
618
|
+
}
|
|
619
|
+
return { data: [], timingMs: 0 };
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// src/modules/reminders.ts
|
|
623
|
+
async function collectReminders(_options) {
|
|
624
|
+
if (getPlatform() === "linux") {
|
|
625
|
+
return { data: [], timingMs: 0 };
|
|
626
|
+
}
|
|
627
|
+
return { data: [], timingMs: 0 };
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// src/util/json.ts
|
|
631
|
+
function stableStringify(obj, pretty = false) {
|
|
632
|
+
return JSON.stringify(obj, stableReplacer, pretty ? 2 : 0);
|
|
633
|
+
}
|
|
634
|
+
function stableReplacer(_key, value) {
|
|
635
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
636
|
+
return Object.keys(value).sort().reduce((acc, k) => {
|
|
637
|
+
acc[k] = value[k];
|
|
638
|
+
return acc;
|
|
639
|
+
}, {});
|
|
640
|
+
}
|
|
641
|
+
return value;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// src/index.ts
|
|
645
|
+
var DEFAULT_TIMEOUT_MS = 250;
|
|
646
|
+
function parseArgs() {
|
|
647
|
+
const program = new Command();
|
|
648
|
+
program.name("context").description(
|
|
649
|
+
"Print a single JSON object describing your current local context (for agents). Supports macOS and Linux. Privacy-respecting, read-only, no network calls to external services. Sensitive data (clipboard, window title, calendar, reminders) is opt-in via flags."
|
|
650
|
+
).option("--pretty", "Pretty-print JSON").option("--clipboard", "Include clipboard text (may prompt for pasteboard access)").option(
|
|
651
|
+
"--frontmost-window",
|
|
652
|
+
"Include frontmost window title (requires Accessibility permission)"
|
|
653
|
+
).option("--apps", "Include list of running apps (name, bundle_id, pid)").option("--battery", "Include battery percentage and charging state").option("--network", "Include primary interface, SSID, local reachability").option("--calendar", "Include next calendar events (requires Calendar permission)").option("--reminders", "Include reminders (requires Reminders permission)").option(
|
|
654
|
+
"--redact",
|
|
655
|
+
"Redact sensitive string fields (clipboard, window title, event/reminder titles); output sha256 + length"
|
|
656
|
+
).option("--timeout-ms <n>", "Per-module timeout in ms", (v) => parseInt(v, 10), DEFAULT_TIMEOUT_MS).option("--debug", "Include per-module timings in JSON under _debug").addHelpText(
|
|
657
|
+
"after",
|
|
658
|
+
`
|
|
659
|
+
Privacy & permissions:
|
|
660
|
+
By default, context only reads safe system info (OS, machine, locale, frontmost app name).
|
|
661
|
+
It does NOT read clipboard, window titles, calendar, or reminders unless you pass the
|
|
662
|
+
corresponding flag (--clipboard, --frontmost-window, etc.). Accessibility is only required for --frontmost-window.
|
|
663
|
+
Calendar/Reminders permissions are only requested when --calendar or --reminders
|
|
664
|
+
is used. No screenshots, keystrokes, or recording of any kind.
|
|
665
|
+
`
|
|
666
|
+
);
|
|
667
|
+
program.parse();
|
|
668
|
+
const opts = program.opts();
|
|
669
|
+
return {
|
|
670
|
+
pretty: opts.pretty ?? false,
|
|
671
|
+
includeClipboard: opts.clipboard ?? false,
|
|
672
|
+
includeFrontmostWindow: opts.frontmostWindow ?? false,
|
|
673
|
+
includeApps: opts.apps ?? false,
|
|
674
|
+
includeBattery: opts.battery ?? false,
|
|
675
|
+
includeNetwork: opts.network ?? false,
|
|
676
|
+
includeCalendar: opts.calendar ?? false,
|
|
677
|
+
includeReminders: opts.reminders ?? false,
|
|
678
|
+
redact: opts.redact ?? false,
|
|
679
|
+
timeoutMs: opts.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
680
|
+
debug: opts.debug ?? false
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
async function collectAll(options) {
|
|
684
|
+
const permissions = createPermissionsState();
|
|
685
|
+
const warnings = [];
|
|
686
|
+
const errors = [];
|
|
687
|
+
const timingsMs = {};
|
|
688
|
+
let exitCode = 0;
|
|
689
|
+
function addError(module, message, code) {
|
|
690
|
+
errors.push({ module, message, code });
|
|
691
|
+
}
|
|
692
|
+
function setExit(code) {
|
|
693
|
+
if (code === 2) exitCode = 2;
|
|
694
|
+
else if (code === 3 && exitCode !== 2) exitCode = 3;
|
|
695
|
+
else if (code === 4 && exitCode !== 2 && exitCode !== 3) exitCode = 4;
|
|
696
|
+
}
|
|
697
|
+
function recordTiming(name, ms) {
|
|
698
|
+
timingsMs[name] = ms;
|
|
699
|
+
}
|
|
700
|
+
const hostResult = await collectHost(options.timeoutMs);
|
|
701
|
+
if (hostResult.timingMs != null) recordTiming("host", hostResult.timingMs);
|
|
702
|
+
if (hostResult.error) {
|
|
703
|
+
addError("host", hostResult.error.message, hostResult.error.code);
|
|
704
|
+
if (hostResult.error.code === "timeout") setExit(3);
|
|
705
|
+
else setExit(4);
|
|
706
|
+
}
|
|
707
|
+
if (hostResult.warnings) warnings.push(...hostResult.warnings);
|
|
708
|
+
const frontmostResult = await collectFrontmost(options);
|
|
709
|
+
if (frontmostResult.timingMs != null) recordTiming("frontmost", frontmostResult.timingMs);
|
|
710
|
+
if (frontmostResult.permission) updatePermission(permissions, "accessibility", frontmostResult.permission);
|
|
711
|
+
if (frontmostResult.error) {
|
|
712
|
+
addError("frontmost", frontmostResult.error.message, frontmostResult.error.code);
|
|
713
|
+
if (frontmostResult.permission === "denied") setExit(2);
|
|
714
|
+
else if (frontmostResult.error.code === "timeout") setExit(3);
|
|
715
|
+
else setExit(4);
|
|
716
|
+
}
|
|
717
|
+
if (frontmostResult.warnings) warnings.push(...frontmostResult.warnings);
|
|
718
|
+
let appsResult = {};
|
|
719
|
+
if (options.includeApps) {
|
|
720
|
+
appsResult = await collectApps(options.timeoutMs);
|
|
721
|
+
if (appsResult.timingMs != null) recordTiming("apps", appsResult.timingMs);
|
|
722
|
+
if (appsResult.error) {
|
|
723
|
+
addError("apps", appsResult.error.message, appsResult.error.code);
|
|
724
|
+
if (appsResult.error.code === "timeout") setExit(3);
|
|
725
|
+
}
|
|
726
|
+
if (appsResult.warnings) warnings.push(...appsResult.warnings);
|
|
727
|
+
}
|
|
728
|
+
let clipboardResult = {};
|
|
729
|
+
if (options.includeClipboard) {
|
|
730
|
+
clipboardResult = await collectClipboard(options);
|
|
731
|
+
if (clipboardResult.timingMs != null) recordTiming("clipboard", clipboardResult.timingMs);
|
|
732
|
+
if (clipboardResult.error) {
|
|
733
|
+
addError("clipboard", clipboardResult.error.message, clipboardResult.error.code);
|
|
734
|
+
if (clipboardResult.error.code === "timeout") setExit(3);
|
|
735
|
+
}
|
|
736
|
+
if (clipboardResult.warnings) warnings.push(...clipboardResult.warnings);
|
|
737
|
+
}
|
|
738
|
+
let batteryResult = {};
|
|
739
|
+
if (options.includeBattery) {
|
|
740
|
+
batteryResult = await collectBattery(options.timeoutMs);
|
|
741
|
+
if (batteryResult.timingMs != null) recordTiming("battery", batteryResult.timingMs);
|
|
742
|
+
if (batteryResult.error) {
|
|
743
|
+
addError("battery", batteryResult.error.message, batteryResult.error.code);
|
|
744
|
+
if (batteryResult.error.code === "timeout") setExit(3);
|
|
745
|
+
}
|
|
746
|
+
if (batteryResult.warnings) warnings.push(...batteryResult.warnings);
|
|
747
|
+
}
|
|
748
|
+
let networkResult = {};
|
|
749
|
+
if (options.includeNetwork) {
|
|
750
|
+
networkResult = await collectNetwork(options.timeoutMs);
|
|
751
|
+
if (networkResult.timingMs != null) recordTiming("network", networkResult.timingMs);
|
|
752
|
+
if (networkResult.error) {
|
|
753
|
+
addError("network", networkResult.error.message, networkResult.error.code);
|
|
754
|
+
if (networkResult.error.code === "timeout") setExit(3);
|
|
755
|
+
}
|
|
756
|
+
if (networkResult.warnings) warnings.push(...networkResult.warnings);
|
|
757
|
+
}
|
|
758
|
+
let calendarResult = {};
|
|
759
|
+
if (options.includeCalendar) {
|
|
760
|
+
calendarResult = await collectCalendar(options);
|
|
761
|
+
if (calendarResult.timingMs != null) recordTiming("calendar", calendarResult.timingMs);
|
|
762
|
+
if (calendarResult.permission) updatePermission(permissions, "calendar", calendarResult.permission);
|
|
763
|
+
if (calendarResult.error) {
|
|
764
|
+
addError("calendar", calendarResult.error.message, calendarResult.error.code);
|
|
765
|
+
if (calendarResult.permission === "denied") setExit(2);
|
|
766
|
+
else if (calendarResult.error.code === "timeout") setExit(3);
|
|
767
|
+
else setExit(4);
|
|
768
|
+
}
|
|
769
|
+
if (calendarResult.warnings) warnings.push(...calendarResult.warnings);
|
|
770
|
+
}
|
|
771
|
+
let remindersResult = {};
|
|
772
|
+
if (options.includeReminders) {
|
|
773
|
+
remindersResult = await collectReminders(options);
|
|
774
|
+
if (remindersResult.timingMs != null) recordTiming("reminders", remindersResult.timingMs);
|
|
775
|
+
if (remindersResult.permission) updatePermission(permissions, "reminders", remindersResult.permission);
|
|
776
|
+
if (remindersResult.error) {
|
|
777
|
+
addError("reminders", remindersResult.error.message, remindersResult.error.code);
|
|
778
|
+
if (remindersResult.permission === "denied") setExit(2);
|
|
779
|
+
else if (remindersResult.error.code === "timeout") setExit(3);
|
|
780
|
+
else setExit(4);
|
|
781
|
+
}
|
|
782
|
+
if (remindersResult.warnings) warnings.push(...remindersResult.warnings);
|
|
783
|
+
}
|
|
784
|
+
const output = {
|
|
785
|
+
schema_version: SCHEMA_VERSION,
|
|
786
|
+
generated_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
787
|
+
permissions,
|
|
788
|
+
warnings: warnings.length ? warnings : void 0,
|
|
789
|
+
errors: errors.length ? errors : void 0
|
|
790
|
+
};
|
|
791
|
+
if (hostResult.data != null) output.host = hostResult.data;
|
|
792
|
+
if (frontmostResult.data != null) output.frontmost = frontmostResult.data;
|
|
793
|
+
if (appsResult.data != null) output.apps = appsResult.data;
|
|
794
|
+
if (clipboardResult.data != null) output.clipboard = clipboardResult.data;
|
|
795
|
+
if (batteryResult.data != null) output.battery = batteryResult.data;
|
|
796
|
+
if (networkResult.data != null) output.network = networkResult.data;
|
|
797
|
+
if (calendarResult.data != null) output.calendar = calendarResult.data;
|
|
798
|
+
if (remindersResult.data != null) output.reminders = remindersResult.data;
|
|
799
|
+
if (options.debug) output._debug = { timings_ms: timingsMs };
|
|
800
|
+
return { output, exitCode };
|
|
801
|
+
}
|
|
802
|
+
async function main() {
|
|
803
|
+
const options = parseArgs();
|
|
804
|
+
const { output, exitCode } = await collectAll(options);
|
|
805
|
+
console.log(stableStringify(output, options.pretty));
|
|
806
|
+
process.exit(exitCode);
|
|
807
|
+
}
|
|
808
|
+
main();
|
|
809
|
+
export {
|
|
810
|
+
collectAll
|
|
811
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "os-context",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Fast macOS & Linux CLI that prints a single JSON object describing your current local context for agents",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"context": "dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": ["dist"],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsup src/index.ts --format esm --clean",
|
|
13
|
+
"dev": "tsx src/index.ts",
|
|
14
|
+
"test": "vitest run",
|
|
15
|
+
"test:watch": "vitest"
|
|
16
|
+
},
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=20"
|
|
19
|
+
},
|
|
20
|
+
"keywords": ["macos", "cli", "context", "agent"],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"commander": "^12.1.0",
|
|
24
|
+
"execa": "^9.5.2",
|
|
25
|
+
"zod": "^3.23.8"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^22.10.1",
|
|
29
|
+
"tsup": "^8.3.5",
|
|
30
|
+
"tsx": "^4.19.2",
|
|
31
|
+
"typescript": "^5.7.2",
|
|
32
|
+
"vitest": "^2.1.6"
|
|
33
|
+
}
|
|
34
|
+
}
|