@yuridulin/pointer 0.0.0-stage → 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/README.md +54 -2
- package/package.json +51 -4
- package/server/acp/catalog.ts +111 -0
- package/server/acp/connection.ts +233 -0
- package/server/acp/fake-agent.ts +408 -0
- package/server/acp/protocol.ts +167 -0
- package/server/acp/run.ts +139 -0
- package/server/acp/session.ts +1249 -0
- package/server/attachments.ts +115 -0
- package/server/changes.ts +239 -0
- package/server/cli.ts +362 -0
- package/server/compact.ts +361 -0
- package/server/config.ts +176 -0
- package/server/db.ts +462 -0
- package/server/dev.ts +8 -0
- package/server/diff.ts +190 -0
- package/server/engines.ts +63 -0
- package/server/files.ts +254 -0
- package/server/git.ts +319 -0
- package/server/history.ts +425 -0
- package/server/http/attachments.ts +63 -0
- package/server/http/browse.ts +108 -0
- package/server/http/chats.ts +627 -0
- package/server/http/closing.ts +14 -0
- package/server/http/files.ts +115 -0
- package/server/http/git.ts +84 -0
- package/server/http/preview.ts +21 -0
- package/server/http/providers.ts +200 -0
- package/server/http/reply.ts +38 -0
- package/server/hub.ts +521 -0
- package/server/install.ts +169 -0
- package/server/jobs.ts +664 -0
- package/server/linkPreview.ts +172 -0
- package/server/loop.ts +328 -0
- package/server/main.ts +353 -0
- package/server/probe.ts +34 -0
- package/server/process.ts +333 -0
- package/server/prompt.ts +196 -0
- package/server/providers/fake.ts +100 -0
- package/server/providers/opencode.ts +468 -0
- package/server/providers/registry.ts +149 -0
- package/server/providers/store.ts +210 -0
- package/server/segments.ts +248 -0
- package/server/sse.ts +93 -0
- package/server/tools/bash.ts +222 -0
- package/server/tools/edit.ts +254 -0
- package/server/tools/glob.ts +63 -0
- package/server/tools/grep.ts +122 -0
- package/server/tools/ignore.ts +54 -0
- package/server/tools/index.ts +129 -0
- package/server/tools/jail.ts +85 -0
- package/server/tools/jobs.ts +293 -0
- package/server/tools/read.ts +248 -0
- package/server/tools/write.ts +70 -0
- package/server/tray.ps1 +191 -0
- package/server/turn.ts +663 -0
- package/server/watch.ts +94 -0
- package/shared/acpConfig.ts +113 -0
- package/shared/links.ts +122 -0
- package/shared/types.ts +392 -0
- package/web/dist/app.css +1 -0
- package/web/dist/assets/codicon-mxdk0fnd.ttf +0 -0
- package/web/dist/index.html +28 -0
- package/web/dist/main.css +1 -0
- package/web/dist/main.js +1366 -0
- package/web/dist/tokens.css +1 -0
- package/web/dist/workers/css.worker.js +97 -0
- package/web/dist/workers/editor.worker.js +30 -0
- package/web/dist/workers/html.worker.js +300 -0
- package/web/dist/workers/json.worker.js +62 -0
- package/web/dist/workers/ts.worker.js +67733 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { readFileSync, statSync } from 'node:fs'
|
|
2
|
+
import type { ToolDef } from '../../shared/types'
|
|
3
|
+
import { BLOB_LIMIT } from '../changes'
|
|
4
|
+
import { SPILL_LIMIT } from '../jobs'
|
|
5
|
+
import { isInsideRoot, resolveInRoots, type JailedPath } from './jail'
|
|
6
|
+
import type { ToolContext, ToolResult } from './index'
|
|
7
|
+
|
|
8
|
+
export const MAX_LINES = 2000
|
|
9
|
+
export const MAX_BYTES = 60 * 1024
|
|
10
|
+
/** One call reads at most this many files; the parallel tool cap is the same number. */
|
|
11
|
+
export const MAX_BATCH_FILES = 10
|
|
12
|
+
const TEXT_SNIFF = 8 * 1024
|
|
13
|
+
|
|
14
|
+
export type ReadArgs = { path?: string; paths?: string[]; offset?: number; limit?: number; raw?: boolean }
|
|
15
|
+
|
|
16
|
+
/** One entry of `details.files` for a batch read; `error` means this path alone failed. */
|
|
17
|
+
export type ReadFileDetails = {
|
|
18
|
+
path: string
|
|
19
|
+
from?: number
|
|
20
|
+
to?: number
|
|
21
|
+
total?: number
|
|
22
|
+
bom?: TextShape['bom']
|
|
23
|
+
eol?: LineEnding
|
|
24
|
+
error?: string
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const readDef: ToolDef = {
|
|
28
|
+
name: 'read',
|
|
29
|
+
description:
|
|
30
|
+
'Read text files with line numbers. Paths are relative to the workspace root or absolute inside it; anything outside the folder is rejected. Pass path for one file, or paths for several files in one call (up to 10): each file starts with a "=== path ===" line, and one failed file does not cancel the others. Use offset (1-based) and limit to read a range of a big file; in a batch both apply to every file. The answer notes the byte order mark and line endings when they differ from plain UTF-8 and LF, so `cat -A` and `xxd` are not needed to see them. With raw=true, CRLF stays in the lines. A BOM is reported in that note and is not copied into line 1.',
|
|
31
|
+
parameters: {
|
|
32
|
+
type: 'object',
|
|
33
|
+
properties: {
|
|
34
|
+
path: {
|
|
35
|
+
type: 'string',
|
|
36
|
+
description: 'One file path inside the workspace folder. Omit when paths is set',
|
|
37
|
+
},
|
|
38
|
+
paths: {
|
|
39
|
+
type: 'array',
|
|
40
|
+
items: { type: 'string' },
|
|
41
|
+
description: 'Several file paths inside the workspace folder to read in one call, up to 10. Omit when path is set',
|
|
42
|
+
},
|
|
43
|
+
offset: { type: 'integer', description: 'First line to read, 1-based. Default 1' },
|
|
44
|
+
limit: { type: 'integer', description: 'How many lines to read. Default: to the end of the file' },
|
|
45
|
+
raw: {
|
|
46
|
+
type: 'boolean',
|
|
47
|
+
description:
|
|
48
|
+
'Keep CRLF in the returned lines. A BOM is reported in the note either way, not inserted into line 1. Default false normalizes CRLF to LF for editing',
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
additionalProperties: false,
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type LineEnding = 'lf' | 'crlf' | 'mixed'
|
|
56
|
+
|
|
57
|
+
/** What the file carries besides its text: a BOM, and which line endings it uses. */
|
|
58
|
+
export type TextShape = { bom: 'utf-8' | null; eol: LineEnding }
|
|
59
|
+
|
|
60
|
+
export type LoadedText = { jailed: JailedPath; bytes: Uint8Array; text: string; shape: TextShape }
|
|
61
|
+
|
|
62
|
+
const BOM_BYTES = [0xef, 0xbb, 0xbf] as const
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The text as the model should see it, plus the byte-level facts the decoder would hide.
|
|
66
|
+
*
|
|
67
|
+
* `TextDecoder` drops a UTF-8 BOM unless `ignoreBOM` is set, and editing compares LF text,
|
|
68
|
+
* so both are normalized here and reported separately. Without the report a model asked
|
|
69
|
+
* about CRLF or a BOM has to leave for `bash` and run `cat -A`, which is what happened in
|
|
70
|
+
* every benchmark run that read files through the shell.
|
|
71
|
+
*/
|
|
72
|
+
export function decodeText(bytes: Uint8Array, options?: { keepBom?: boolean }): { text: string; shape: TextShape } {
|
|
73
|
+
const bom = BOM_BYTES.every((value, index) => bytes[index] === value) ? 'utf-8' : null
|
|
74
|
+
// `ignoreBOM: true` makes the decoder keep the mark as U+FEFF instead of consuming it.
|
|
75
|
+
const text = new TextDecoder('utf-8', { fatal: true, ignoreBOM: options?.keepBom === true }).decode(bytes)
|
|
76
|
+
const crlf = countCrlf(bytes)
|
|
77
|
+
const lfOnly = countLf(bytes) - crlf
|
|
78
|
+
const eol: LineEnding = crlf === 0 ? 'lf' : lfOnly === 0 ? 'crlf' : 'mixed'
|
|
79
|
+
return { text, shape: { bom, eol } }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function countLf(bytes: Uint8Array): number {
|
|
83
|
+
let total = 0
|
|
84
|
+
for (const byte of bytes) if (byte === 0x0a) total++
|
|
85
|
+
return total
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function countCrlf(bytes: Uint8Array): number {
|
|
89
|
+
let total = 0
|
|
90
|
+
for (let index = 1; index < bytes.length; index++) {
|
|
91
|
+
if (bytes[index] === 0x0a && bytes[index - 1] === 0x0d) total++
|
|
92
|
+
}
|
|
93
|
+
return total
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** A one-line note for the model, or '' when the file is plain UTF-8 with LF. */
|
|
97
|
+
export function shapeNote(shape: TextShape): string {
|
|
98
|
+
if (shape.bom === null && shape.eol === 'lf') return ''
|
|
99
|
+
const parts: string[] = []
|
|
100
|
+
parts.push(`BOM: ${shape.bom ?? 'none'}`)
|
|
101
|
+
parts.push(`EOL: ${shape.eol.toUpperCase()}`)
|
|
102
|
+
return `[${parts.join(', ')}]`
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Resolves the path inside the root and returns its bytes plus LF-normalized UTF-8 text. */
|
|
106
|
+
export function loadText(root: string, input: string, extraRoots: string[] = []): LoadedText {
|
|
107
|
+
const jailed = resolveInRoots([root, ...extraRoots], input)
|
|
108
|
+
const label = jailed.rel || '.'
|
|
109
|
+
let stat
|
|
110
|
+
try {
|
|
111
|
+
stat = statSync(jailed.abs)
|
|
112
|
+
} catch (error) {
|
|
113
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') throw new Error(`File not found: ${label}`)
|
|
114
|
+
throw error
|
|
115
|
+
}
|
|
116
|
+
if (stat.isDirectory()) throw new Error(`${label} is a directory`)
|
|
117
|
+
const limit = byteLimit(jailed.abs, extraRoots)
|
|
118
|
+
if (stat.size > limit) throw new Error(tooLarge(label, limit))
|
|
119
|
+
const bytes = readFileSync(jailed.abs)
|
|
120
|
+
if (bytes.byteLength > limit) throw new Error(tooLarge(label, limit))
|
|
121
|
+
if (bytes.subarray(0, TEXT_SNIFF).includes(0)) throw new Error(`${label} is a binary file`)
|
|
122
|
+
let decoded: { text: string; shape: TextShape }
|
|
123
|
+
try {
|
|
124
|
+
decoded = decodeText(bytes)
|
|
125
|
+
} catch {
|
|
126
|
+
throw new Error(`${label} is not valid UTF-8`)
|
|
127
|
+
}
|
|
128
|
+
return { jailed, bytes, text: decoded.text.replaceAll('\r\n', '\n'), shape: decoded.shape }
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function byteLimit(abs: string, extraRoots: string[]): number {
|
|
132
|
+
for (const root of extraRoots) {
|
|
133
|
+
if (root && isInsideRoot(root, abs)) return SPILL_LIMIT
|
|
134
|
+
}
|
|
135
|
+
return BLOB_LIMIT
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function tooLarge(label: string, limit: number): string {
|
|
139
|
+
const mb = Math.round(limit / (1024 * 1024))
|
|
140
|
+
return `File ${label} is larger than the ${mb} MB limit; narrow the range of the work`
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export async function runRead(args: ReadArgs, ctx: ToolContext): Promise<ToolResult> {
|
|
144
|
+
const offset = args.offset ?? 1
|
|
145
|
+
if (!Number.isInteger(offset) || offset < 1) throw new Error('offset must be an integer of at least 1')
|
|
146
|
+
if (args.limit !== undefined && (!Number.isInteger(args.limit) || args.limit < 1)) {
|
|
147
|
+
throw new Error('limit must be an integer of at least 1')
|
|
148
|
+
}
|
|
149
|
+
if (args.path !== undefined && args.paths !== undefined) {
|
|
150
|
+
throw new Error('read: pass path or paths, not both')
|
|
151
|
+
}
|
|
152
|
+
if (args.paths !== undefined) return readBatch(args.paths, offset, args.limit, args.raw === true, ctx)
|
|
153
|
+
if (typeof args.path !== 'string') throw new Error('read: pass path or paths')
|
|
154
|
+
const one = readOne(args.path, offset, args.limit, args.raw === true, ctx)
|
|
155
|
+
return { text: one.text, isError: false, details: one.details }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** One file, formatted exactly as a single `read` call has always answered. */
|
|
159
|
+
function readOne(
|
|
160
|
+
input: string,
|
|
161
|
+
offset: number,
|
|
162
|
+
limit: number | undefined,
|
|
163
|
+
raw: boolean,
|
|
164
|
+
ctx: ToolContext,
|
|
165
|
+
): { text: string; details: ReadFileDetails } {
|
|
166
|
+
const loaded = loadText(ctx.root, input, ctx.extraRoots)
|
|
167
|
+
const rel = loaded.jailed.rel
|
|
168
|
+
// `raw` keeps CRLF. The BOM stays out of line 1: a line-number prefix would shift it,
|
|
169
|
+
// and an edit that copied that line would then miss because edits compare text without the mark.
|
|
170
|
+
const stored = raw ? decodeText(loaded.bytes, { keepBom: true }).text : loaded.text
|
|
171
|
+
const lines = splitLines(stored.startsWith('\uFEFF') ? stored.slice(1) : stored)
|
|
172
|
+
const total = lines.length
|
|
173
|
+
const note = shapeNote(loaded.shape)
|
|
174
|
+
if (total === 0) {
|
|
175
|
+
return {
|
|
176
|
+
text: `File ${rel || '.'} is empty${note ? ` ${note}` : ''}`,
|
|
177
|
+
details: { path: rel, from: 1, to: 0, total, ...loaded.shape },
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (offset > total) {
|
|
181
|
+
throw new Error(`offset ${offset} is past the end of ${rel} (${total} lines)`)
|
|
182
|
+
}
|
|
183
|
+
const last = limit === undefined ? total : Math.min(total, offset + limit - 1)
|
|
184
|
+
|
|
185
|
+
const out: string[] = []
|
|
186
|
+
let used = 0
|
|
187
|
+
let to = offset - 1
|
|
188
|
+
for (let no = offset; no <= last; no++) {
|
|
189
|
+
const line = `${no}\t${lines[no - 1]}`
|
|
190
|
+
const size = Buffer.byteLength(line, 'utf8') + 1
|
|
191
|
+
if (out.length > 0 && (out.length >= MAX_LINES || used + size > MAX_BYTES)) break
|
|
192
|
+
out.push(line)
|
|
193
|
+
used += size
|
|
194
|
+
to = no
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const head = note ? `${note}\n` : ''
|
|
198
|
+
const body = out.join('\n')
|
|
199
|
+
const tail = to < last ? `\n[lines ${offset}-${to} of ${total}; continue with offset=${to + 1}]` : ''
|
|
200
|
+
return {
|
|
201
|
+
text: `${head}${body}${tail}`,
|
|
202
|
+
details: { path: rel, from: offset, to, total, ...loaded.shape },
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Several files in one result, one header per file. A missing or binary file reports in its
|
|
208
|
+
* own section instead of cancelling the rest; `isError` is true only when nothing was read.
|
|
209
|
+
* This removes the loop `for f in ...; do cat -n "$f"; done` that batching used to need.
|
|
210
|
+
*/
|
|
211
|
+
function readBatch(
|
|
212
|
+
paths: string[],
|
|
213
|
+
offset: number,
|
|
214
|
+
limit: number | undefined,
|
|
215
|
+
raw: boolean,
|
|
216
|
+
ctx: ToolContext,
|
|
217
|
+
): ToolResult {
|
|
218
|
+
if (paths.length === 0) throw new Error('read: path array is empty')
|
|
219
|
+
if (paths.length > MAX_BATCH_FILES) {
|
|
220
|
+
throw new Error(`read: at most ${MAX_BATCH_FILES} paths per call, got ${paths.length}`)
|
|
221
|
+
}
|
|
222
|
+
if (paths.some((path) => typeof path !== 'string')) {
|
|
223
|
+
throw new Error('read: paths must be strings')
|
|
224
|
+
}
|
|
225
|
+
const sections: string[] = []
|
|
226
|
+
const files: ReadFileDetails[] = []
|
|
227
|
+
let read = 0
|
|
228
|
+
for (const input of paths) {
|
|
229
|
+
try {
|
|
230
|
+
const one = readOne(input, offset, limit, raw, ctx)
|
|
231
|
+
sections.push(`=== ${one.details.path || input} ===\n${one.text}`)
|
|
232
|
+
files.push(one.details)
|
|
233
|
+
read++
|
|
234
|
+
} catch (error) {
|
|
235
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
236
|
+
sections.push(`=== ${input} ===\n${message}`)
|
|
237
|
+
files.push({ path: input, error: message })
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return { text: sections.join('\n\n'), isError: read === 0, details: { files } }
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function splitLines(text: string): string[] {
|
|
244
|
+
if (text === '') return []
|
|
245
|
+
const lines = text.split('\n')
|
|
246
|
+
if (lines[lines.length - 1] === '') lines.pop()
|
|
247
|
+
return lines
|
|
248
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { readFileSync, statSync } from 'node:fs'
|
|
2
|
+
import type { ToolDef } from '../../shared/types'
|
|
3
|
+
import { BLOB_LIMIT, writeAtomic, type Revision } from '../changes'
|
|
4
|
+
import { diffLines } from '../diff'
|
|
5
|
+
import { resolveInRoot } from './jail'
|
|
6
|
+
import type { ToolContext, ToolResult } from './index'
|
|
7
|
+
|
|
8
|
+
export type WriteArgs = { path: string; content: string }
|
|
9
|
+
|
|
10
|
+
export const writeDef: ToolDef = {
|
|
11
|
+
name: 'write',
|
|
12
|
+
description:
|
|
13
|
+
'Write a whole file, creating it if needed. Paths are relative to the workspace root or absolute inside it; anything outside the folder is rejected. Content is written as UTF-8, byte for byte, atomically.',
|
|
14
|
+
parameters: {
|
|
15
|
+
type: 'object',
|
|
16
|
+
properties: {
|
|
17
|
+
path: { type: 'string', description: 'File path inside the workspace folder' },
|
|
18
|
+
content: { type: 'string', description: 'Full new content of the file' },
|
|
19
|
+
},
|
|
20
|
+
required: ['path', 'content'],
|
|
21
|
+
additionalProperties: false,
|
|
22
|
+
},
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function runWrite(args: WriteArgs, ctx: ToolContext): Promise<ToolResult> {
|
|
26
|
+
const jailed = resolveInRoot(ctx.root, args.path)
|
|
27
|
+
const rel = jailed.rel
|
|
28
|
+
if (!rel) throw new Error('Cannot write to the workspace root; give a file path')
|
|
29
|
+
const before = previous(jailed.abs, rel)
|
|
30
|
+
const after = new TextEncoder().encode(args.content)
|
|
31
|
+
if (after.byteLength > BLOB_LIMIT) {
|
|
32
|
+
throw new Error(`New content of ${rel} is larger than the 2 MB limit; split the work into smaller files`)
|
|
33
|
+
}
|
|
34
|
+
const beforeText = before === null ? null : new TextDecoder('utf-8').decode(before)
|
|
35
|
+
const details = { path: rel, created: before === null, diff: diffLines(beforeText, args.content) }
|
|
36
|
+
if (before !== null && sameBytes(before, after)) {
|
|
37
|
+
return { text: 'No changes', isError: false, details }
|
|
38
|
+
}
|
|
39
|
+
writeAtomic(jailed.abs, after)
|
|
40
|
+
const revisions: Revision[] = [{ path: rel, before, after }]
|
|
41
|
+
const lines = countLines(args.content)
|
|
42
|
+
const verb = before === null ? 'Created' : 'Wrote'
|
|
43
|
+
return { text: `${verb} ${rel} (${lines} ${lines === 1 ? 'line' : 'lines'})`, isError: false, details, revisions }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function previous(abs: string, rel: string): Uint8Array | null {
|
|
47
|
+
try {
|
|
48
|
+
if (statSync(abs).isDirectory()) throw new Error(`${rel} is a directory`)
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return null
|
|
51
|
+
throw error
|
|
52
|
+
}
|
|
53
|
+
const bytes = readFileSync(abs)
|
|
54
|
+
if (bytes.byteLength > BLOB_LIMIT) {
|
|
55
|
+
throw new Error(`Existing file ${rel} is larger than the 2 MB limit; narrow the range of the work`)
|
|
56
|
+
}
|
|
57
|
+
return bytes
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function sameBytes(a: Uint8Array, b: Uint8Array): boolean {
|
|
61
|
+
if (a.byteLength !== b.byteLength) return false
|
|
62
|
+
for (let i = 0; i < a.byteLength; i++) if (a[i] !== b[i]) return false
|
|
63
|
+
return true
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function countLines(text: string): number {
|
|
67
|
+
if (text === '') return 0
|
|
68
|
+
const lines = text.split('\n')
|
|
69
|
+
return lines[lines.length - 1] === '' ? lines.length - 1 : lines.length
|
|
70
|
+
}
|
package/server/tray.ps1
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
param(
|
|
2
|
+
[Parameter(Mandatory = $true)][string]$Shim
|
|
3
|
+
)
|
|
4
|
+
|
|
5
|
+
$ErrorActionPreference = 'Stop'
|
|
6
|
+
Add-Type -AssemblyName System.Windows.Forms
|
|
7
|
+
Add-Type -AssemblyName System.Drawing
|
|
8
|
+
Add-Type @"
|
|
9
|
+
using System;
|
|
10
|
+
using System.Text;
|
|
11
|
+
using System.Runtime.InteropServices;
|
|
12
|
+
public class PointerWindow {
|
|
13
|
+
public delegate bool EnumProc(IntPtr h, IntPtr l);
|
|
14
|
+
[DllImport("user32.dll")] static extern bool EnumWindows(EnumProc cb, IntPtr l);
|
|
15
|
+
[DllImport("user32.dll")] static extern bool IsWindowVisible(IntPtr h);
|
|
16
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern int GetClassName(IntPtr h, StringBuilder s, int n);
|
|
17
|
+
[DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr h, out uint pid);
|
|
18
|
+
[DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr h);
|
|
19
|
+
[DllImport("user32.dll")] static extern void SwitchToThisWindow(IntPtr h, bool altTab);
|
|
20
|
+
[DllImport("user32.dll")] static extern bool ShowWindow(IntPtr h, int cmd);
|
|
21
|
+
[DllImport("user32.dll")] static extern bool IsIconic(IntPtr h);
|
|
22
|
+
public static IntPtr Find(uint[] pids) {
|
|
23
|
+
IntPtr found = IntPtr.Zero;
|
|
24
|
+
EnumWindows((h, l) => {
|
|
25
|
+
if (!IsWindowVisible(h)) return true;
|
|
26
|
+
var name = new StringBuilder(64);
|
|
27
|
+
GetClassName(h, name, name.Capacity);
|
|
28
|
+
if (name.ToString() != "Chrome_WidgetWin_1") return true;
|
|
29
|
+
uint pid;
|
|
30
|
+
GetWindowThreadProcessId(h, out pid);
|
|
31
|
+
if (Array.IndexOf(pids, pid) < 0) return true;
|
|
32
|
+
found = h;
|
|
33
|
+
return false;
|
|
34
|
+
}, IntPtr.Zero);
|
|
35
|
+
return found;
|
|
36
|
+
}
|
|
37
|
+
public static void Focus(IntPtr h) {
|
|
38
|
+
if (IsIconic(h)) ShowWindow(h, 9);
|
|
39
|
+
if (!SetForegroundWindow(h)) SwitchToThisWindow(h, false);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
"@
|
|
43
|
+
|
|
44
|
+
$PointerHome = Join-Path $env:USERPROFILE '.pointer'
|
|
45
|
+
$PidFile = Join-Path $PointerHome 'tray.pid'
|
|
46
|
+
$ProfileDir = Join-Path $PointerHome 'window'
|
|
47
|
+
|
|
48
|
+
function Test-Alive([int]$ProcId) {
|
|
49
|
+
if ($ProcId -le 0) { return $false }
|
|
50
|
+
return $null -ne (Get-Process -Id $ProcId -ErrorAction SilentlyContinue)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (Test-Path $PidFile) {
|
|
54
|
+
$existing = 0
|
|
55
|
+
[void][int]::TryParse((Get-Content -Raw $PidFile), [ref]$existing)
|
|
56
|
+
if (Test-Alive $existing) { exit 0 }
|
|
57
|
+
}
|
|
58
|
+
Set-Content -Path $PidFile -Value $PID -Encoding ascii
|
|
59
|
+
|
|
60
|
+
function Read-Config {
|
|
61
|
+
$file = Join-Path $PointerHome 'config.json'
|
|
62
|
+
if (-not (Test-Path $file)) { return $null }
|
|
63
|
+
try { return Get-Content -Raw $file | ConvertFrom-Json } catch { return $null }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function Find-Edge {
|
|
67
|
+
foreach ($path in @(
|
|
68
|
+
"$env:ProgramFiles\Microsoft\Edge\Application\msedge.exe",
|
|
69
|
+
"${env:ProgramFiles(x86)}\Microsoft\Edge\Application\msedge.exe"
|
|
70
|
+
)) {
|
|
71
|
+
if (Test-Path $path) { return $path }
|
|
72
|
+
}
|
|
73
|
+
return $null
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function Find-Window {
|
|
77
|
+
$pids = @(Get-CimInstance Win32_Process -Filter "Name='msedge.exe'" -ErrorAction SilentlyContinue |
|
|
78
|
+
Where-Object { $_.CommandLine -like "*$ProfileDir*" } |
|
|
79
|
+
ForEach-Object { [uint32]$_.ProcessId })
|
|
80
|
+
if ($pids.Count -eq 0) { return [IntPtr]::Zero }
|
|
81
|
+
return [PointerWindow]::Find($pids)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function Focus-Window {
|
|
85
|
+
$window = Find-Window
|
|
86
|
+
if ($window -eq [IntPtr]::Zero) { return $false }
|
|
87
|
+
[PointerWindow]::Focus($window)
|
|
88
|
+
return $true
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function Open-Window($config, [string]$hash) {
|
|
92
|
+
if (Focus-Window) { return }
|
|
93
|
+
$token = [uri]::EscapeDataString([string]$config.token)
|
|
94
|
+
$url = "http://127.0.0.1:$($config.port)/?token=$token$hash"
|
|
95
|
+
$edge = Find-Edge
|
|
96
|
+
if (-not $edge) {
|
|
97
|
+
Start-Process $url
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
New-Item -ItemType Directory -Force -Path $ProfileDir | Out-Null
|
|
101
|
+
Start-Process -FilePath $edge -ArgumentList @(
|
|
102
|
+
"--app=$url",
|
|
103
|
+
"--user-data-dir=$ProfileDir",
|
|
104
|
+
'--start-maximized',
|
|
105
|
+
'--no-first-run'
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function New-Mark([System.Drawing.Color]$color) {
|
|
110
|
+
$bitmap = New-Object System.Drawing.Bitmap 16, 16
|
|
111
|
+
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
|
|
112
|
+
$brush = New-Object System.Drawing.SolidBrush $color
|
|
113
|
+
$graphics.FillEllipse($brush, 2, 2, 12, 12)
|
|
114
|
+
$graphics.Dispose()
|
|
115
|
+
$brush.Dispose()
|
|
116
|
+
[System.Drawing.Icon]::FromHandle($bitmap.GetHicon())
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
$icons = @{
|
|
120
|
+
stopped = New-Mark ([System.Drawing.Color]::FromArgb(140, 140, 140))
|
|
121
|
+
ready = New-Mark ([System.Drawing.Color]::FromArgb(40, 160, 70))
|
|
122
|
+
busy = New-Mark ([System.Drawing.Color]::FromArgb(220, 140, 20))
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
$icon = New-Object System.Windows.Forms.NotifyIcon
|
|
126
|
+
$icon.Icon = $icons.stopped
|
|
127
|
+
$icon.Visible = $true
|
|
128
|
+
$icon.Text = 'Pointer'
|
|
129
|
+
|
|
130
|
+
$menu = New-Object System.Windows.Forms.ContextMenuStrip
|
|
131
|
+
[void]$menu.Items.Add('Открыть').Add_Click({
|
|
132
|
+
$config = Read-Config
|
|
133
|
+
if ($config) { Open-Window $config '' }
|
|
134
|
+
})
|
|
135
|
+
[void]$menu.Items.Add('Перезапустить').Add_Click({
|
|
136
|
+
Start-Process -FilePath $Shim -ArgumentList 'restart' -WindowStyle Hidden
|
|
137
|
+
})
|
|
138
|
+
[void]$menu.Items.Add('Остановить').Add_Click({
|
|
139
|
+
Start-Process -FilePath $Shim -ArgumentList 'stop' -WindowStyle Hidden
|
|
140
|
+
})
|
|
141
|
+
[void]$menu.Items.Add('Обновить').Add_Click({
|
|
142
|
+
Start-Process -FilePath $Shim -ArgumentList 'update' -WindowStyle Hidden
|
|
143
|
+
})
|
|
144
|
+
$icon.ContextMenuStrip = $menu
|
|
145
|
+
|
|
146
|
+
$script:seen = -1
|
|
147
|
+
$timer = New-Object System.Windows.Forms.Timer
|
|
148
|
+
$timer.Interval = 1000
|
|
149
|
+
$timer.Add_Tick({
|
|
150
|
+
$config = Read-Config
|
|
151
|
+
if (-not $config) {
|
|
152
|
+
$icon.Icon = $icons.stopped
|
|
153
|
+
$icon.Text = 'Pointer — остановлен'
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
try {
|
|
157
|
+
$headers = @{ Authorization = "Bearer $($config.token)" }
|
|
158
|
+
$status = Invoke-RestMethod -Uri "http://127.0.0.1:$($config.port)/api/status" -Headers $headers -TimeoutSec 2
|
|
159
|
+
} catch {
|
|
160
|
+
$icon.Icon = $icons.stopped
|
|
161
|
+
$icon.Text = 'Pointer — остановлен'
|
|
162
|
+
return
|
|
163
|
+
}
|
|
164
|
+
if ($status.busy) {
|
|
165
|
+
$icon.Icon = $icons.busy
|
|
166
|
+
$icon.Text = 'Pointer — идёт ход'
|
|
167
|
+
} else {
|
|
168
|
+
$icon.Icon = $icons.ready
|
|
169
|
+
$icon.Text = 'Pointer — готов'
|
|
170
|
+
}
|
|
171
|
+
if ($null -ne $status.show -and [int]$status.show.seq -ne $script:seen) {
|
|
172
|
+
$script:seen = [int]$status.show.seq
|
|
173
|
+
if ([int]$status.clients -gt 0) {
|
|
174
|
+
[void](Focus-Window)
|
|
175
|
+
return
|
|
176
|
+
}
|
|
177
|
+
$hash = ''
|
|
178
|
+
if ($status.show.workspaceId) { $hash = "#workspace=$($status.show.workspaceId)" }
|
|
179
|
+
Open-Window $config $hash
|
|
180
|
+
}
|
|
181
|
+
})
|
|
182
|
+
$timer.Start()
|
|
183
|
+
|
|
184
|
+
try {
|
|
185
|
+
[System.Windows.Forms.Application]::Run()
|
|
186
|
+
} finally {
|
|
187
|
+
$timer.Stop()
|
|
188
|
+
$icon.Visible = $false
|
|
189
|
+
$icon.Dispose()
|
|
190
|
+
Remove-Item $PidFile -ErrorAction SilentlyContinue
|
|
191
|
+
}
|