quick-acp 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/LICENSE +21 -0
- package/README.md +405 -2
- package/dist/acp/agent.js +371 -0
- package/dist/acp-smoke.js +163 -0
- package/dist/concurrent-test.js +101 -0
- package/dist/doctor.js +86 -0
- package/dist/e2e.js +206 -0
- package/dist/fresh-machine-e2e.js +151 -0
- package/dist/inject-turn-test.js +119 -0
- package/dist/login-cli.js +86 -0
- package/dist/main.js +77 -0
- package/dist/no-outpost-e2e.js +86 -0
- package/dist/probe.js +308 -0
- package/dist/quick/auth.js +371 -0
- package/dist/quick/codec.js +499 -0
- package/dist/quick/credstore.js +439 -0
- package/dist/quick/gateway-contract.js +104 -0
- package/dist/quick/login.js +522 -0
- package/dist/quick/oidc.js +152 -0
- package/dist/quick/session-direct.js +200 -0
- package/dist/quick/session.js +385 -0
- package/dist/quick/transport.js +343 -0
- package/dist/tools/registry.js +411 -0
- package/dist/tools/strategies.js +188 -0
- package/dist/wss-smoke.js +230 -0
- package/package.json +53 -4
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quick session acquisition.
|
|
3
|
+
*
|
|
4
|
+
* THREE SOURCES, IN ORDER OF PREFERENCE
|
|
5
|
+
* -------------------------------------
|
|
6
|
+
* 1. Cached session (in memory, until the token nears expiry).
|
|
7
|
+
* 2. Refresh it ourselves (oidc.ts) when a stored login exists. This WORKS —
|
|
8
|
+
* an earlier note in this file claimed it did not, and that was wrong. The
|
|
9
|
+
* two real mistakes were the host (`oidc.<region>.api.aws`, not
|
|
10
|
+
* `.amazonaws.com`) and putting the client secret in the body instead of
|
|
11
|
+
* HTTP Basic. Both are fixed and verified live.
|
|
12
|
+
* 3. Ask Quick's own outpost (auth.ts `startOutpost` + `/auth/sncs-session`).
|
|
13
|
+
* Needed not for the token but for what the token does not carry:
|
|
14
|
+
* `ws_host`, `actor_arn`, `tenant_url`.
|
|
15
|
+
*
|
|
16
|
+
* A machine that has never run the desktop app has no stored login. For that
|
|
17
|
+
* case `login.ts` performs the full OAuth authorization-code + PKCE flow in a
|
|
18
|
+
* browser and writes a `profiles.json` the outpost accepts, so the outpost can
|
|
19
|
+
* be started against a home directory that this bridge created.
|
|
20
|
+
*
|
|
21
|
+
* The outpost's /auth/sncs-session needs the instance token, which the app
|
|
22
|
+
* holds in memory. `startOutpost()` handles the supported way to get one.
|
|
23
|
+
*/
|
|
24
|
+
import { spawn } from 'node:child_process';
|
|
25
|
+
import { randomBytes, randomUUID } from 'node:crypto';
|
|
26
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
27
|
+
import { join } from 'node:path';
|
|
28
|
+
import { homedir } from 'node:os';
|
|
29
|
+
import { activeProfileId, readStoredLogin } from './credstore.js';
|
|
30
|
+
import { jwtExpiry } from './oidc.js';
|
|
31
|
+
/**
|
|
32
|
+
* Path used for a new session.
|
|
33
|
+
*
|
|
34
|
+
* VERIFIED: the working path for this tenant family is
|
|
35
|
+
* `/streaming/quickwork-chat-bidirectional-stream`. The
|
|
36
|
+
* `/ws/service/SpaceNeedleConversationService/...` form 404s.
|
|
37
|
+
*/
|
|
38
|
+
export const DEFAULT_WS_PATH = '/streaming/quickwork-chat-bidirectional-stream';
|
|
39
|
+
/** The renderer's path constant; 404s here, kept for diagnostics. */
|
|
40
|
+
export const ALT_WS_PATH = '/ws/service/SpaceNeedleConversationService/operation/ChatBidirectionalStream';
|
|
41
|
+
export function surfaceForPlatform(platform = process.platform) {
|
|
42
|
+
return platform === 'win32' ? 'QUICK_WORK_DESKTOP_WINDOWS' : 'QUICK_WORK_DESKTOP_MAC';
|
|
43
|
+
}
|
|
44
|
+
/** Read the outpost port the app uses. Observed default 8780. */
|
|
45
|
+
export function outpostPort() {
|
|
46
|
+
const raw = process.env.QUICKWORK_OUTPOST_PORT ?? '8780';
|
|
47
|
+
const n = Number.parseInt(raw, 10);
|
|
48
|
+
return Number.isInteger(n) && n > 0 && n <= 65535 ? n : 8780;
|
|
49
|
+
}
|
|
50
|
+
export function quickworkHome() {
|
|
51
|
+
return process.env.QUICK_ACP_QUICKWORK_HOME ?? join(homedir(), '.quickwork');
|
|
52
|
+
}
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
// Session cache
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
let cached = null;
|
|
57
|
+
/** Refresh this long before expiry so a token never dies mid-turn. */
|
|
58
|
+
const EXPIRY_SKEW_MS = 120_000;
|
|
59
|
+
function cachedIsFresh(s) {
|
|
60
|
+
if (!s)
|
|
61
|
+
return false;
|
|
62
|
+
if (s.expiresAt === null)
|
|
63
|
+
return true; // unknown expiry: trust until the server says no
|
|
64
|
+
return Date.now() + EXPIRY_SKEW_MS < s.expiresAt;
|
|
65
|
+
}
|
|
66
|
+
export function clearSessionCache() {
|
|
67
|
+
cached = null;
|
|
68
|
+
}
|
|
69
|
+
function hostFromTenantUrl(tenantUrl) {
|
|
70
|
+
try {
|
|
71
|
+
return new URL(tenantUrl).host;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return tenantUrl.replace(/^https?:\/\//, '').replace(/\/.*$/, '');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** The profile's device id, which is the closest durable outpost identity. */
|
|
78
|
+
function readDeviceId() {
|
|
79
|
+
const id = activeProfileId();
|
|
80
|
+
if (!id)
|
|
81
|
+
return '';
|
|
82
|
+
try {
|
|
83
|
+
return readFileSync(join(quickworkHome(), 'profiles', id, 'device_id'), 'utf8').trim();
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return '';
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// ---------------------------------------------------------------------------
|
|
90
|
+
// Outpost: the supported token source
|
|
91
|
+
// ---------------------------------------------------------------------------
|
|
92
|
+
/** Is something listening on the outpost port and answering /health? */
|
|
93
|
+
export async function outpostIsUp() {
|
|
94
|
+
try {
|
|
95
|
+
const resp = await fetch(`http://127.0.0.1:${outpostPort()}/health`, {
|
|
96
|
+
signal: AbortSignal.timeout(1_500),
|
|
97
|
+
});
|
|
98
|
+
return resp.ok;
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async function sessionFromOutpost() {
|
|
105
|
+
const token = process.env.QUICK_ACP_INSTANCE_TOKEN;
|
|
106
|
+
if (!token)
|
|
107
|
+
return null;
|
|
108
|
+
const url = `http://localhost:${outpostPort()}/auth/sncs-session`;
|
|
109
|
+
let resp;
|
|
110
|
+
try {
|
|
111
|
+
resp = await fetch(url, {
|
|
112
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
113
|
+
signal: AbortSignal.timeout(15_000),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
if (!resp.ok)
|
|
120
|
+
return null;
|
|
121
|
+
let body;
|
|
122
|
+
try {
|
|
123
|
+
body = (await resp.json());
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
const jwt = typeof body['id_token'] === 'string' ? body['id_token'] : '';
|
|
129
|
+
const wsHost = typeof body['ws_host'] === 'string' ? body['ws_host'] : '';
|
|
130
|
+
if (!jwt || !wsHost)
|
|
131
|
+
return null;
|
|
132
|
+
const tenantUrl = typeof body['tenant_url'] === 'string' && body['tenant_url']
|
|
133
|
+
? body['tenant_url']
|
|
134
|
+
: `https://${wsHost}`;
|
|
135
|
+
return {
|
|
136
|
+
jwt,
|
|
137
|
+
wsHost,
|
|
138
|
+
wsPath: typeof body['ws_path'] === 'string' && body['ws_path'] ? body['ws_path'] : DEFAULT_WS_PATH,
|
|
139
|
+
tenantUrl,
|
|
140
|
+
surface: surfaceForPlatform(),
|
|
141
|
+
outpostId: typeof body['outpost_device_id'] === 'string' ? body['outpost_device_id'] : readDeviceId(),
|
|
142
|
+
workingSessionId: typeof body['working_session_id'] === 'string'
|
|
143
|
+
? body['working_session_id']
|
|
144
|
+
: randomUUID(),
|
|
145
|
+
actorArn: typeof body['actor_arn'] === 'string' ? body['actor_arn'] : '',
|
|
146
|
+
provider: typeof body['provider'] === 'string' ? body['provider'] : '',
|
|
147
|
+
source: 'outpost',
|
|
148
|
+
expiresAt: jwtExpiry(jwt),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Locate the bundled agent binary the app would run as its outpost.
|
|
153
|
+
* Returns null when the install layout is not recognised.
|
|
154
|
+
*/
|
|
155
|
+
export function findAgentBinary() {
|
|
156
|
+
const explicit = process.env.QUICK_ACP_AGENT_BIN;
|
|
157
|
+
if (explicit && existsSync(explicit))
|
|
158
|
+
return explicit;
|
|
159
|
+
const candidates = [
|
|
160
|
+
// The path this app uses on Windows.
|
|
161
|
+
join(process.env['LOCALAPPDATA'] ?? '', 'quickwork', 'agent', 'quickwork-agent.exe'),
|
|
162
|
+
join(homedir(), 'AppData', 'Local', 'quickwork', 'agent', 'quickwork-agent.exe'),
|
|
163
|
+
// A caller-supplied install root.
|
|
164
|
+
join(process.env['QUICK_ACP_QUICKWORK_DIR'] ?? '', 'resources', 'agent.zip'),
|
|
165
|
+
];
|
|
166
|
+
for (const c of candidates) {
|
|
167
|
+
if (c && existsSync(c) && c.endsWith('.exe'))
|
|
168
|
+
return c;
|
|
169
|
+
}
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Start the app's own outpost so it refreshes the login for us.
|
|
174
|
+
*
|
|
175
|
+
* The outpost reads the instance token from STDIN as its first line (the app
|
|
176
|
+
* does exactly this: `process.stdin.write(token + '\n')`). We mint the token
|
|
177
|
+
* ourselves 鈥?it is a shared secret between us and the child, so we get to
|
|
178
|
+
* choose it 鈥?then hand the same value to /auth/sncs-session.
|
|
179
|
+
*
|
|
180
|
+
* This is the supported path: it uses the app's own signed binary, its own
|
|
181
|
+
* stored login, and its own refresh logic. No credential re-implementation.
|
|
182
|
+
*/
|
|
183
|
+
export async function startOutpost(opts = {}) {
|
|
184
|
+
if (await outpostIsUp()) {
|
|
185
|
+
const existing = process.env.QUICK_ACP_INSTANCE_TOKEN;
|
|
186
|
+
return {
|
|
187
|
+
started: true,
|
|
188
|
+
instanceToken: existing ?? null,
|
|
189
|
+
reason: existing
|
|
190
|
+
? 'an outpost is already running and QUICK_ACP_INSTANCE_TOKEN is set'
|
|
191
|
+
: 'an outpost is already running but we do not know its instance token; ' +
|
|
192
|
+
'set QUICK_ACP_INSTANCE_TOKEN, or stop the app and let this bridge start its own outpost',
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
const bin = findAgentBinary();
|
|
196
|
+
if (!bin) {
|
|
197
|
+
return {
|
|
198
|
+
started: false,
|
|
199
|
+
instanceToken: null,
|
|
200
|
+
reason: 'could not find quickwork-agent.exe. Set QUICK_ACP_AGENT_BIN to its full path ' +
|
|
201
|
+
'(usually %LOCALAPPDATA%\\quickwork\\agent\\quickwork-agent.exe).',
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
// We choose the token; the child authenticates its callers with it.
|
|
205
|
+
const token = randomBytes(32).toString('hex');
|
|
206
|
+
const child = spawn(bin, ['--outpost-only', '--manifest', 'desktop', '--port', String(outpostPort())], {
|
|
207
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
208
|
+
windowsHide: true,
|
|
209
|
+
detached: false,
|
|
210
|
+
env: {
|
|
211
|
+
...process.env,
|
|
212
|
+
QUICKWORK_OUTPOST_PORT: String(outpostPort()),
|
|
213
|
+
// The app always injects the build stage; without it the bundled agent
|
|
214
|
+
// aborts in get_profile_stage with
|
|
215
|
+
// "QUICKWORK_BUILD_STAGE is not set". prod matches the shipping build.
|
|
216
|
+
QUICKWORK_BUILD_STAGE: process.env.QUICKWORK_BUILD_STAGE ?? 'prod',
|
|
217
|
+
QUICKWORK_CLOUD_MODE: process.env.QUICKWORK_CLOUD_MODE ?? '1',
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
// First line on stdin is the instance token, as the app does.
|
|
221
|
+
try {
|
|
222
|
+
child.stdin?.write(`${token}\n`);
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
/* the child will exit and we report below */
|
|
226
|
+
}
|
|
227
|
+
let stderrTail = '';
|
|
228
|
+
child.stderr?.on('data', (c) => {
|
|
229
|
+
stderrTail = (stderrTail + c.toString('utf8')).slice(-2000);
|
|
230
|
+
});
|
|
231
|
+
// Keep the process alive for the lifetime of this agent; do not leak it.
|
|
232
|
+
const cleanup = () => {
|
|
233
|
+
try {
|
|
234
|
+
child.kill();
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
/* already gone */
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
process.once('exit', cleanup);
|
|
241
|
+
process.once('SIGINT', cleanup);
|
|
242
|
+
process.once('SIGTERM', cleanup);
|
|
243
|
+
const deadline = Date.now() + (opts.waitMs ?? 120_000);
|
|
244
|
+
while (Date.now() < deadline) {
|
|
245
|
+
if (child.exitCode !== null) {
|
|
246
|
+
return {
|
|
247
|
+
started: false,
|
|
248
|
+
instanceToken: null,
|
|
249
|
+
reason: `the outpost exited with code ${child.exitCode}.` +
|
|
250
|
+
(stderrTail ? ` Its stderr: ${stderrTail.slice(-400)}` : ''),
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
if (await outpostIsUp()) {
|
|
254
|
+
process.env.QUICK_ACP_INSTANCE_TOKEN = token;
|
|
255
|
+
return { started: true, instanceToken: token };
|
|
256
|
+
}
|
|
257
|
+
await new Promise((r) => setTimeout(r, 500));
|
|
258
|
+
}
|
|
259
|
+
cleanup();
|
|
260
|
+
return {
|
|
261
|
+
started: false,
|
|
262
|
+
instanceToken: null,
|
|
263
|
+
reason: `the outpost did not become healthy within ${opts.waitMs ?? 120_000}ms`,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
// ---------------------------------------------------------------------------
|
|
267
|
+
// Public API
|
|
268
|
+
// ---------------------------------------------------------------------------
|
|
269
|
+
/**
|
|
270
|
+
* Resolve a usable session.
|
|
271
|
+
*
|
|
272
|
+
* Order of preference:
|
|
273
|
+
* 1. cached (until near expiry)
|
|
274
|
+
* 2. the stored login, refreshed by us (session-direct.ts — no outpost needed)
|
|
275
|
+
* 3. the app's outpost (needs its instance token)
|
|
276
|
+
*
|
|
277
|
+
* Direct first is deliberate: it works without the desktop app running, without
|
|
278
|
+
* an instance token, and without the 93 MB agent binary — verified end to end
|
|
279
|
+
* against the live gateway. The outpost stays as a fallback for deployments
|
|
280
|
+
* whose login blob lacks `tenant_url`.
|
|
281
|
+
*
|
|
282
|
+
* `force` bypasses the in-memory cache, which a token refresh needs.
|
|
283
|
+
*/
|
|
284
|
+
export async function resolveSession(opts = {}) {
|
|
285
|
+
if (!opts.force && cachedIsFresh(cached)) {
|
|
286
|
+
return cached;
|
|
287
|
+
}
|
|
288
|
+
const failures = [];
|
|
289
|
+
// 1. Straight from the credential store: no outpost, no instance token.
|
|
290
|
+
try {
|
|
291
|
+
const { sessionDirect } = await import('./session-direct.js');
|
|
292
|
+
const direct = await sessionDirect();
|
|
293
|
+
if (direct.ok && direct.session) {
|
|
294
|
+
cached = direct.session;
|
|
295
|
+
return direct.session;
|
|
296
|
+
}
|
|
297
|
+
failures.push(`stored login: ${direct.reason}`);
|
|
298
|
+
}
|
|
299
|
+
catch (e) {
|
|
300
|
+
failures.push(`stored login: ${e instanceof Error ? e.message : String(e)}`);
|
|
301
|
+
}
|
|
302
|
+
// 2. The app's outpost.
|
|
303
|
+
try {
|
|
304
|
+
const s = await sessionFromOutpost();
|
|
305
|
+
if (s) {
|
|
306
|
+
cached = s;
|
|
307
|
+
return s;
|
|
308
|
+
}
|
|
309
|
+
if (process.env.QUICK_ACP_INSTANCE_TOKEN) {
|
|
310
|
+
failures.push('outpost: it did not return a usable session');
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
failures.push('outpost: QUICK_ACP_INSTANCE_TOKEN is not set, so its session route refuses us');
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
catch (e) {
|
|
317
|
+
failures.push(`outpost: ${e instanceof Error ? e.message : String(e)}`);
|
|
318
|
+
}
|
|
319
|
+
const profile = activeProfileId();
|
|
320
|
+
const stored = await readStoredLogin();
|
|
321
|
+
throw new Error([
|
|
322
|
+
'Could not obtain a Quick session.',
|
|
323
|
+
'',
|
|
324
|
+
...failures.map((f) => ` - ${f}`),
|
|
325
|
+
'',
|
|
326
|
+
profile
|
|
327
|
+
? `Profile "${profile}" exists and its login is ${stored.ok ? 'READABLE' : 'NOT readable'}.`
|
|
328
|
+
: `No Quick profile found under ${quickworkHome()}.`,
|
|
329
|
+
'',
|
|
330
|
+
'How to fix:',
|
|
331
|
+
' A. Quick desktop app signed in at least once? Then just let this bridge start',
|
|
332
|
+
' the outpost:',
|
|
333
|
+
' node dist/doctor.js --start-outpost',
|
|
334
|
+
' B. No Quick install at all? Sign in without it:',
|
|
335
|
+
' node dist/login-cli.js --start-url https://ssoins-xxxxx.awsapps.com/start',
|
|
336
|
+
' (the address of your AWS access portal; see README "没有官方客户端也能登录")',
|
|
337
|
+
].join('\n'));
|
|
338
|
+
}
|
|
339
|
+
/** Report what credentials are discoverable, without revealing any secret. */
|
|
340
|
+
export async function describeCredentialState() {
|
|
341
|
+
const profile = activeProfileId();
|
|
342
|
+
const state = {
|
|
343
|
+
platform: process.platform,
|
|
344
|
+
quickworkHome: quickworkHome(),
|
|
345
|
+
activeProfile: profile,
|
|
346
|
+
outpostPort: outpostPort(),
|
|
347
|
+
outpostUp: await outpostIsUp(),
|
|
348
|
+
agentBinary: findAgentBinary(),
|
|
349
|
+
hasInstanceTokenEnv: Boolean(process.env.QUICK_ACP_INSTANCE_TOKEN),
|
|
350
|
+
};
|
|
351
|
+
const read = await readStoredLogin();
|
|
352
|
+
if (read.ok) {
|
|
353
|
+
const exp = read.login['access_token_expiry'];
|
|
354
|
+
state['storedLogin'] = {
|
|
355
|
+
ok: true,
|
|
356
|
+
source: read.source,
|
|
357
|
+
hasRefreshToken: typeof read.login.refresh_token === 'string',
|
|
358
|
+
hasClientId: typeof read.login.client_id === 'string',
|
|
359
|
+
hasClientSecret: typeof read.login.client_secret === 'string',
|
|
360
|
+
issuer: read.login.issuer_url ?? null,
|
|
361
|
+
tenant: read.login.tenant_url ?? null,
|
|
362
|
+
accessTokenExpiry: typeof exp === 'number' ? new Date(exp * 1000).toISOString() : null,
|
|
363
|
+
accessTokenExpired: typeof exp === 'number' ? exp * 1000 < Date.now() : null,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
state['storedLogin'] = { ok: false, reason: read.reason };
|
|
368
|
+
}
|
|
369
|
+
return state;
|
|
370
|
+
}
|
|
371
|
+
//# sourceMappingURL=auth.js.map
|