@xuda.ai/cli 0.1.0 → 0.1.2
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/package.json +1 -1
- package/src/auth.mjs +11 -1
- package/src/config.mjs +3 -3
- package/src/session.mjs +14 -8
package/package.json
CHANGED
package/src/auth.mjs
CHANGED
|
@@ -49,7 +49,17 @@ export const login = async () => {
|
|
|
49
49
|
if (start.code < 0) throw new Error(`auth start failed: ${start.data}`);
|
|
50
50
|
const { device_code, user_code, verification_uri, interval = 3, expires_in = 600 } = start.data;
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
let resolvedVerificationUri = verification_uri;
|
|
53
|
+
if (process.env.XUDA_WEB_URL) {
|
|
54
|
+
try {
|
|
55
|
+
const orig = new URL(verification_uri);
|
|
56
|
+
const override = new URL(process.env.XUDA_WEB_URL);
|
|
57
|
+
orig.protocol = override.protocol;
|
|
58
|
+
orig.host = override.host;
|
|
59
|
+
resolvedVerificationUri = orig.toString();
|
|
60
|
+
} catch {}
|
|
61
|
+
}
|
|
62
|
+
const fullUri = `${resolvedVerificationUri}?code=${encodeURIComponent(user_code)}`;
|
|
53
63
|
console.log('');
|
|
54
64
|
console.log(' Sign in to Xuda to continue.');
|
|
55
65
|
console.log('');
|
package/src/config.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import os from 'node:os';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
|
|
4
|
-
export const API_URL = process.env.XUDA_API_URL || 'https://
|
|
5
|
-
export const WEB_URL = process.env.XUDA_WEB_URL ||
|
|
6
|
-
export const WS_URL = (process.env.XUDA_WS_URL || API_URL).replace(/^http/, 'ws')
|
|
4
|
+
export const API_URL = process.env.XUDA_API_URL || 'https://xuda.ai';
|
|
5
|
+
export const WEB_URL = process.env.XUDA_WEB_URL || API_URL;
|
|
6
|
+
export const WS_URL = (process.env.XUDA_WS_URL || API_URL).replace(/^http/, 'ws');
|
|
7
7
|
|
|
8
8
|
export const CRED_DIR = path.join(os.homedir(), '.xuda');
|
|
9
9
|
export const CRED_FILE = path.join(CRED_DIR, 'credentials.json');
|
package/src/session.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import readline from 'node:readline';
|
|
|
2
2
|
import { io } from 'socket.io-client';
|
|
3
3
|
import { API_URL } from './config.mjs';
|
|
4
4
|
|
|
5
|
-
const CLI_VERSION = '0.1.
|
|
5
|
+
const CLI_VERSION = '0.1.2';
|
|
6
6
|
|
|
7
7
|
export const runAgentSession = async ({ agentId, agentName, agentVersion, credentials }) => {
|
|
8
8
|
const socket = io(API_URL, {
|
|
@@ -17,13 +17,14 @@ export const runAgentSession = async ({ agentId, agentName, agentVersion, creden
|
|
|
17
17
|
},
|
|
18
18
|
});
|
|
19
19
|
|
|
20
|
-
let
|
|
20
|
+
let conversationId = null;
|
|
21
21
|
const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: process.stdin.isTTY });
|
|
22
22
|
const prompt = () => {
|
|
23
23
|
if (!process.stdin.isTTY) return;
|
|
24
24
|
rl.setPrompt('› ');
|
|
25
25
|
rl.prompt();
|
|
26
26
|
};
|
|
27
|
+
const isOurs = (msg) => !conversationId || !msg?.conversation_id || msg.conversation_id === conversationId;
|
|
27
28
|
|
|
28
29
|
await new Promise((resolve, reject) => {
|
|
29
30
|
const t = setTimeout(() => reject(new Error('handshake timeout')), 15000);
|
|
@@ -34,7 +35,7 @@ export const runAgentSession = async ({ agentId, agentName, agentVersion, creden
|
|
|
34
35
|
|
|
35
36
|
socket.on('handshake_ok', (msg) => {
|
|
36
37
|
clearTimeout(t);
|
|
37
|
-
|
|
38
|
+
conversationId = msg.conversation_id || msg.session_id;
|
|
38
39
|
const name = msg.agent_name || agentName || 'agent';
|
|
39
40
|
console.log(`Connected to ${name}. (ctrl-c to exit)`);
|
|
40
41
|
if (msg.welcome) console.log(`\n${msg.welcome}\n`);
|
|
@@ -53,9 +54,14 @@ export const runAgentSession = async ({ agentId, agentName, agentVersion, creden
|
|
|
53
54
|
});
|
|
54
55
|
});
|
|
55
56
|
|
|
56
|
-
socket.on('stream_start', () => process.stdout.write('\n'));
|
|
57
|
-
socket.on('
|
|
58
|
-
socket.on('
|
|
57
|
+
socket.on('stream_start', (msg) => { if (isOurs(msg)) process.stdout.write('\n'); });
|
|
58
|
+
socket.on('response_start', (msg) => { if (isOurs(msg)) process.stdout.write('\n'); });
|
|
59
|
+
socket.on('stream_delta', (msg) => {
|
|
60
|
+
if (!isOurs(msg)) return;
|
|
61
|
+
const chunk = msg?.delta ?? msg?.text;
|
|
62
|
+
if (chunk) process.stdout.write(String(chunk));
|
|
63
|
+
});
|
|
64
|
+
socket.on('stream_end', (msg) => { if (isOurs(msg)) { process.stdout.write('\n\n'); prompt(); } });
|
|
59
65
|
socket.on('error', (msg) => { console.error(`\nerror: ${msg?.message || 'unknown'}`); prompt(); });
|
|
60
66
|
|
|
61
67
|
socket.on('disconnect', () => {
|
|
@@ -66,11 +72,11 @@ export const runAgentSession = async ({ agentId, agentName, agentVersion, creden
|
|
|
66
72
|
rl.on('line', (line) => {
|
|
67
73
|
const text = line.trim();
|
|
68
74
|
if (!text) { prompt(); return; }
|
|
69
|
-
socket.emit('prompt', {
|
|
75
|
+
socket.emit('prompt', { conversation_id: conversationId, text });
|
|
70
76
|
});
|
|
71
77
|
|
|
72
78
|
rl.on('SIGINT', () => {
|
|
73
|
-
try { socket.emit('close', {
|
|
79
|
+
try { socket.emit('close', { conversation_id: conversationId }); } catch {}
|
|
74
80
|
socket.disconnect();
|
|
75
81
|
process.exit(0);
|
|
76
82
|
});
|