@sortlist/cli 0.29.0 → 0.31.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 +1 -48
- package/package.json +2 -10
- package/src/commands/dynamic.ts +95 -7
- package/src/graphql/generated/gql.ts +3 -33
- package/src/graphql/generated/graphql.ts +420 -559
- package/src/graphql/operations/common.ts +0 -82
- package/src/index.ts +23 -12
- package/src/commands/chat/chat.ts +0 -302
- package/src/commands/chat/interactive/App.tsx +0 -119
- package/src/commands/chat/interactive/components/ChatInput.tsx +0 -115
- package/src/commands/chat/interactive/components/ChoicePrompt.tsx +0 -35
- package/src/commands/chat/interactive/components/MessageView.tsx +0 -63
- package/src/commands/chat/interactive/components/ThinkingBox.tsx +0 -54
- package/src/commands/chat/interactive/connection.ts +0 -111
- package/src/commands/chat/interactive/debug-log.ts +0 -19
- package/src/commands/chat/interactive/hooks/useChat.ts +0 -440
- package/src/commands/chat/interactive/hooks/useTerminalSize.ts +0 -23
- package/src/commands/chat/interactive/index.tsx +0 -26
- package/src/commands/chat/interactive/markdown.ts +0 -29
- package/src/commands/chat/interactive/types.ts +0 -34
- package/src/types/marked-terminal.d.ts +0 -20
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { Select } from '@inkjs/ui';
|
|
2
|
-
import { Box, Text, useInput } from 'ink';
|
|
3
|
-
import React from 'react';
|
|
4
|
-
|
|
5
|
-
import type { PendingChoice } from '../types.ts';
|
|
6
|
-
|
|
7
|
-
interface ChoicePromptProps {
|
|
8
|
-
choice: PendingChoice;
|
|
9
|
-
onSelect: (value: string) => void;
|
|
10
|
-
onCancel: () => void;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function ChoicePrompt({ choice, onSelect, onCancel }: ChoicePromptProps) {
|
|
14
|
-
const options = choice.options.map((option) => ({ label: option, value: option }));
|
|
15
|
-
|
|
16
|
-
useInput((_input, key) => {
|
|
17
|
-
if (key.escape) {
|
|
18
|
-
onCancel();
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
return (
|
|
23
|
-
<Box flexDirection="column" borderStyle="round" borderColor="yellow" paddingX={1} flexShrink={0}>
|
|
24
|
-
<Box marginBottom={1}>
|
|
25
|
-
<Text color="yellow" bold>
|
|
26
|
-
? {choice.question}
|
|
27
|
-
</Text>
|
|
28
|
-
</Box>
|
|
29
|
-
<Select options={options} onChange={onSelect} />
|
|
30
|
-
<Box marginTop={1}>
|
|
31
|
-
<Text dimColor>↑↓ move · ↵ select · esc cancel</Text>
|
|
32
|
-
</Box>
|
|
33
|
-
</Box>
|
|
34
|
-
);
|
|
35
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { Box, Text } from 'ink';
|
|
2
|
-
import React from 'react';
|
|
3
|
-
|
|
4
|
-
import { renderMarkdown } from '../markdown.ts';
|
|
5
|
-
import type { Message } from '../types.ts';
|
|
6
|
-
|
|
7
|
-
export function MessageView({ msg }: { msg: Message }) {
|
|
8
|
-
if (msg.role === 'user') {
|
|
9
|
-
return (
|
|
10
|
-
<Box marginBottom={1}>
|
|
11
|
-
<Text backgroundColor="#333333">
|
|
12
|
-
<Text color="green" bold>
|
|
13
|
-
{'❯ '}
|
|
14
|
-
</Text>
|
|
15
|
-
{msg.content}
|
|
16
|
-
</Text>
|
|
17
|
-
</Box>
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return (
|
|
22
|
-
<Box flexDirection="column" marginBottom={1}>
|
|
23
|
-
<Text color="cyan" bold>
|
|
24
|
-
Chat
|
|
25
|
-
</Text>
|
|
26
|
-
<Box marginLeft={0}>
|
|
27
|
-
<Text>{renderMarkdown(msg.content)}</Text>
|
|
28
|
-
</Box>
|
|
29
|
-
</Box>
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function estimateMessageLines(msg: Message, width: number): number {
|
|
34
|
-
if (msg.role === 'user') {
|
|
35
|
-
return Math.ceil((msg.content.length + 2) / width) + 1;
|
|
36
|
-
}
|
|
37
|
-
const rendered = renderMarkdown(msg.content);
|
|
38
|
-
const lines = rendered.split('\n').reduce((sum, line) => {
|
|
39
|
-
return sum + Math.max(1, Math.ceil((line.length || 1) / width));
|
|
40
|
-
}, 0);
|
|
41
|
-
return 1 + lines + 1;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function getVisibleMessages(
|
|
45
|
-
messages: Message[],
|
|
46
|
-
availableHeight: number,
|
|
47
|
-
width: number,
|
|
48
|
-
scrollOffset: number,
|
|
49
|
-
): Message[] {
|
|
50
|
-
const endIdx = messages.length - scrollOffset;
|
|
51
|
-
if (endIdx <= 0) return messages.slice(0, 1);
|
|
52
|
-
|
|
53
|
-
let usedLines = 0;
|
|
54
|
-
let startIdx = endIdx;
|
|
55
|
-
for (let i = endIdx - 1; i >= 0; i--) {
|
|
56
|
-
const lines = estimateMessageLines(messages[i]!, width);
|
|
57
|
-
if (usedLines + lines > availableHeight && usedLines > 0) break;
|
|
58
|
-
// Always include at least the most recent message, even if it overflows
|
|
59
|
-
usedLines += lines;
|
|
60
|
-
startIdx = i;
|
|
61
|
-
}
|
|
62
|
-
return messages.slice(startIdx, endIdx);
|
|
63
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { Spinner } from '@inkjs/ui';
|
|
2
|
-
import { Box, Text } from 'ink';
|
|
3
|
-
import React from 'react';
|
|
4
|
-
|
|
5
|
-
import type { ThinkingEntry, ToolCallInfo } from '../types.ts';
|
|
6
|
-
|
|
7
|
-
interface ThinkingBoxProps {
|
|
8
|
-
thinkingLog: ThinkingEntry[];
|
|
9
|
-
streamContent: string | null;
|
|
10
|
-
height: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function ToolCallLine({ tool }: { tool: ToolCallInfo }) {
|
|
14
|
-
const pending = !tool.resultLabel;
|
|
15
|
-
|
|
16
|
-
if (pending) {
|
|
17
|
-
return (
|
|
18
|
-
<Box>
|
|
19
|
-
<Spinner label={tool.label} />
|
|
20
|
-
</Box>
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return (
|
|
25
|
-
<Box>
|
|
26
|
-
<Text color={tool.isError ? 'red' : 'green'}>{tool.isError ? '✗' : '✓'} </Text>
|
|
27
|
-
<Text color="white">{tool.label}</Text>
|
|
28
|
-
</Box>
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function ThinkingBox({ thinkingLog, streamContent, height }: ThinkingBoxProps) {
|
|
33
|
-
const maxLines = height - 3;
|
|
34
|
-
const visible = thinkingLog.slice(-maxLines);
|
|
35
|
-
|
|
36
|
-
return (
|
|
37
|
-
<Box
|
|
38
|
-
flexDirection="column"
|
|
39
|
-
height={height}
|
|
40
|
-
flexShrink={0}
|
|
41
|
-
borderStyle="single"
|
|
42
|
-
borderColor="cyan"
|
|
43
|
-
paddingX={1}
|
|
44
|
-
overflow="hidden">
|
|
45
|
-
<Text color="cyan" bold dimColor>
|
|
46
|
-
{streamContent !== null ? 'Typing...' : 'Thinking...'}
|
|
47
|
-
</Text>
|
|
48
|
-
{visible.map((entry, i) => (
|
|
49
|
-
// eslint-disable-next-line react/no-array-index-key -- append-only log
|
|
50
|
-
<ToolCallLine key={i} tool={entry.tool} />
|
|
51
|
-
))}
|
|
52
|
-
</Box>
|
|
53
|
-
);
|
|
54
|
-
}
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { print } from 'graphql';
|
|
2
|
-
|
|
3
|
-
import { ConversationEvent as ConversationEventSubscriptionDocument } from '../../../graphql/operations/common.ts';
|
|
4
|
-
import type { ChatConnection, ChatEvent } from './types.ts';
|
|
5
|
-
|
|
6
|
-
const EVENT_STREAM = 'text/event-stream';
|
|
7
|
-
|
|
8
|
-
// Subscribes to `conversationEvent` over GraphQL-SSE (distinct connection
|
|
9
|
-
// mode). hive-router negotiates the response format from the client's
|
|
10
|
-
// `Accept` header; we explicitly ask for text/event-stream. The router
|
|
11
|
-
// keeps the HTTP response open and pushes `event: next` frames as the
|
|
12
|
-
// chat subgraph POSTs them via the callback protocol.
|
|
13
|
-
export async function connectChatStream(
|
|
14
|
-
endpoint: string,
|
|
15
|
-
token: string,
|
|
16
|
-
conversationId: string,
|
|
17
|
-
onDebug?: (line: string) => void,
|
|
18
|
-
): Promise<ChatConnection> {
|
|
19
|
-
const abort = new AbortController();
|
|
20
|
-
const response = await fetch(endpoint, {
|
|
21
|
-
method: 'POST',
|
|
22
|
-
signal: abort.signal,
|
|
23
|
-
headers: {
|
|
24
|
-
'Content-Type': 'application/json',
|
|
25
|
-
Authorization: `Bearer ${token}`,
|
|
26
|
-
Accept: EVENT_STREAM,
|
|
27
|
-
},
|
|
28
|
-
body: JSON.stringify({
|
|
29
|
-
query: print(ConversationEventSubscriptionDocument),
|
|
30
|
-
variables: { conversationId },
|
|
31
|
-
}),
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
if (!response.ok || !response.body) {
|
|
35
|
-
throw new Error(`Chat stream failed: HTTP ${response.status} ${response.statusText}`);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const contentType = response.headers.get('content-type') ?? '';
|
|
39
|
-
if (!contentType.includes(EVENT_STREAM)) {
|
|
40
|
-
throw new Error(`Expected ${EVENT_STREAM}, got "${contentType}"`);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const messageHandlers: Array<(event: ChatEvent) => void> = [];
|
|
44
|
-
const closeHandlers: Array<() => void> = [];
|
|
45
|
-
|
|
46
|
-
void (async () => {
|
|
47
|
-
const reader = response.body!.getReader();
|
|
48
|
-
const decoder = new TextDecoder();
|
|
49
|
-
let buffer = '';
|
|
50
|
-
let currentEvent: string | null = null;
|
|
51
|
-
let currentData: string[] = [];
|
|
52
|
-
|
|
53
|
-
const dispatch = () => {
|
|
54
|
-
if (currentEvent === 'next' && currentData.length > 0) {
|
|
55
|
-
try {
|
|
56
|
-
const payload = JSON.parse(currentData.join('\n'));
|
|
57
|
-
const conversationEvent = payload?.data?.conversationEvent;
|
|
58
|
-
if (conversationEvent) {
|
|
59
|
-
for (const handler of messageHandlers) handler(conversationEvent);
|
|
60
|
-
}
|
|
61
|
-
} catch (err) {
|
|
62
|
-
onDebug?.(`[sse] parse error: ${err instanceof Error ? err.message : err}`);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
currentEvent = null;
|
|
66
|
-
currentData = [];
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
try {
|
|
70
|
-
while (true) {
|
|
71
|
-
const { value, done } = await reader.read();
|
|
72
|
-
if (done) break;
|
|
73
|
-
buffer += decoder.decode(value, { stream: true });
|
|
74
|
-
|
|
75
|
-
let newlineIndex: number;
|
|
76
|
-
while ((newlineIndex = buffer.indexOf('\n')) !== -1) {
|
|
77
|
-
const line = buffer.slice(0, newlineIndex).replace(/\r$/, '');
|
|
78
|
-
buffer = buffer.slice(newlineIndex + 1);
|
|
79
|
-
|
|
80
|
-
if (line === '') {
|
|
81
|
-
dispatch();
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
if (line.startsWith(':')) continue;
|
|
85
|
-
if (line.startsWith('event:')) {
|
|
86
|
-
currentEvent = line.slice(6).trim();
|
|
87
|
-
} else if (line.startsWith('data:')) {
|
|
88
|
-
currentData.push(line.slice(5).replace(/^ /, ''));
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
dispatch();
|
|
93
|
-
} catch (err) {
|
|
94
|
-
onDebug?.(`[sse] stream error: ${err instanceof Error ? err.message : err}`);
|
|
95
|
-
} finally {
|
|
96
|
-
for (const handler of closeHandlers) handler();
|
|
97
|
-
}
|
|
98
|
-
})();
|
|
99
|
-
|
|
100
|
-
return {
|
|
101
|
-
onMessage(fn) {
|
|
102
|
-
messageHandlers.push(fn);
|
|
103
|
-
},
|
|
104
|
-
onClose(fn) {
|
|
105
|
-
closeHandlers.push(fn);
|
|
106
|
-
},
|
|
107
|
-
close() {
|
|
108
|
-
abort.abort();
|
|
109
|
-
},
|
|
110
|
-
};
|
|
111
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
|
|
4
|
-
const LOG_DIR = path.resolve(import.meta.dirname, '../../../..');
|
|
5
|
-
const DEBUG_LOG_PATH = path.join(LOG_DIR, 'debug.log');
|
|
6
|
-
const TOOL_LOG_PATH = path.join(LOG_DIR, 'debug-tools.log');
|
|
7
|
-
|
|
8
|
-
export function clearDebugLogs() {
|
|
9
|
-
fs.writeFileSync(DEBUG_LOG_PATH, '');
|
|
10
|
-
fs.writeFileSync(TOOL_LOG_PATH, '');
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function appendDebugLog(line: string) {
|
|
14
|
-
fs.appendFileSync(DEBUG_LOG_PATH, `[${new Date().toISOString()}] ${line}\n`);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function appendToolLog(line: string) {
|
|
18
|
-
fs.appendFileSync(TOOL_LOG_PATH, `[${new Date().toISOString()}] ${line}\n`);
|
|
19
|
-
}
|