pi-rlm 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 +110 -0
- package/index.ts +262 -0
- package/package.json +53 -0
- package/src/backends.ts +356 -0
- package/src/engine.ts +462 -0
- package/src/prompts.ts +83 -0
- package/src/runs.ts +128 -0
- package/src/schema.ts +29 -0
- package/src/types.ts +80 -0
- package/src/utils.ts +128 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export type RlmBackend = "sdk" | "cli" | "tmux";
|
|
2
|
+
export type RlmMode = "auto" | "solve" | "decompose";
|
|
3
|
+
export type RlmOp = "start" | "status" | "wait" | "cancel";
|
|
4
|
+
export type RlmToolsProfile = "coding" | "read-only";
|
|
5
|
+
|
|
6
|
+
export type RunStatus = "running" | "completed" | "failed" | "cancelled";
|
|
7
|
+
export type NodeStatus = "running" | "completed" | "failed" | "cancelled";
|
|
8
|
+
|
|
9
|
+
export interface StartRunInput {
|
|
10
|
+
task: string;
|
|
11
|
+
backend: RlmBackend;
|
|
12
|
+
mode: RlmMode;
|
|
13
|
+
async: boolean;
|
|
14
|
+
model?: string;
|
|
15
|
+
cwd: string;
|
|
16
|
+
toolsProfile: RlmToolsProfile;
|
|
17
|
+
maxDepth: number;
|
|
18
|
+
maxNodes: number;
|
|
19
|
+
maxBranching: number;
|
|
20
|
+
concurrency: number;
|
|
21
|
+
timeoutMs: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface RlmNode {
|
|
25
|
+
id: string;
|
|
26
|
+
depth: number;
|
|
27
|
+
task: string;
|
|
28
|
+
status: NodeStatus;
|
|
29
|
+
decision?: {
|
|
30
|
+
action: "solve" | "decompose";
|
|
31
|
+
reason: string;
|
|
32
|
+
raw?: string;
|
|
33
|
+
};
|
|
34
|
+
startedAt: number;
|
|
35
|
+
finishedAt?: number;
|
|
36
|
+
result?: string;
|
|
37
|
+
error?: string;
|
|
38
|
+
children: RlmNode[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface RunArtifacts {
|
|
42
|
+
dir: string;
|
|
43
|
+
eventsPath: string;
|
|
44
|
+
treePath: string;
|
|
45
|
+
outputPath: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface RunStats {
|
|
49
|
+
nodesVisited: number;
|
|
50
|
+
maxDepthSeen: number;
|
|
51
|
+
durationMs: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface RlmRunResult {
|
|
55
|
+
runId: string;
|
|
56
|
+
backend: RlmBackend;
|
|
57
|
+
final: string;
|
|
58
|
+
root: RlmNode;
|
|
59
|
+
artifacts: RunArtifacts;
|
|
60
|
+
stats: RunStats;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface RunRecord {
|
|
64
|
+
id: string;
|
|
65
|
+
input: StartRunInput;
|
|
66
|
+
status: RunStatus;
|
|
67
|
+
createdAt: number;
|
|
68
|
+
startedAt: number;
|
|
69
|
+
finishedAt?: number;
|
|
70
|
+
error?: string;
|
|
71
|
+
controller: AbortController;
|
|
72
|
+
promise: Promise<RlmRunResult>;
|
|
73
|
+
result?: RlmRunResult;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface PlannerDecision {
|
|
77
|
+
action: "solve" | "decompose";
|
|
78
|
+
reason: string;
|
|
79
|
+
subtasks?: string[];
|
|
80
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
|
|
3
|
+
const thinkingLevels = new Set(["off", "minimal", "low", "medium", "high", "xhigh"]);
|
|
4
|
+
|
|
5
|
+
export function createRunId(): string {
|
|
6
|
+
return crypto.randomUUID().slice(0, 8);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function truncateText(
|
|
10
|
+
text: string,
|
|
11
|
+
maxChars: number
|
|
12
|
+
): { text: string; truncated: boolean; originalChars: number } {
|
|
13
|
+
if (text.length <= maxChars) {
|
|
14
|
+
return { text, truncated: false, originalChars: text.length };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
text: `${text.slice(0, maxChars)}\n\n[truncated ${text.length - maxChars} chars]`,
|
|
19
|
+
truncated: true,
|
|
20
|
+
originalChars: text.length
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function normalizeTask(task: string): string {
|
|
25
|
+
return task.replace(/\s+/g, " ").trim().toLowerCase();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function shortTask(task: string, max = 80): string {
|
|
29
|
+
if (task.length <= max) return task;
|
|
30
|
+
return `${task.slice(0, max - 3)}...`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function extractFirstJsonObject(text: string): string | undefined {
|
|
34
|
+
const firstBrace = text.indexOf("{");
|
|
35
|
+
if (firstBrace === -1) return undefined;
|
|
36
|
+
|
|
37
|
+
let depth = 0;
|
|
38
|
+
let inString = false;
|
|
39
|
+
let escaped = false;
|
|
40
|
+
|
|
41
|
+
for (let i = firstBrace; i < text.length; i += 1) {
|
|
42
|
+
const ch = text[i];
|
|
43
|
+
|
|
44
|
+
if (inString) {
|
|
45
|
+
if (escaped) {
|
|
46
|
+
escaped = false;
|
|
47
|
+
} else if (ch === "\\") {
|
|
48
|
+
escaped = true;
|
|
49
|
+
} else if (ch === '"') {
|
|
50
|
+
inString = false;
|
|
51
|
+
}
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (ch === '"') {
|
|
56
|
+
inString = true;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (ch === "{") depth += 1;
|
|
61
|
+
if (ch === "}") {
|
|
62
|
+
depth -= 1;
|
|
63
|
+
if (depth === 0) {
|
|
64
|
+
return text.slice(firstBrace, i + 1);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function shellQuote(value: string): string {
|
|
73
|
+
return `'${value.replace(/'/g, `'"'"'`)}'`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function toErrorMessage(error: unknown): string {
|
|
77
|
+
if (error instanceof Error) return error.message;
|
|
78
|
+
try {
|
|
79
|
+
return JSON.stringify(error);
|
|
80
|
+
} catch {
|
|
81
|
+
return String(error);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function sleep(ms: number): Promise<void> {
|
|
86
|
+
return new Promise((resolve) => {
|
|
87
|
+
setTimeout(resolve, ms);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function parseModelPattern(model: string | undefined): {
|
|
92
|
+
provider: string;
|
|
93
|
+
id: string;
|
|
94
|
+
thinkingLevel?: "off" | "minimal" | "low" | "medium" | "high" | "xhigh";
|
|
95
|
+
} | undefined {
|
|
96
|
+
if (!model) return undefined;
|
|
97
|
+
|
|
98
|
+
const trimmed = model.trim();
|
|
99
|
+
if (!trimmed.includes("/")) return undefined;
|
|
100
|
+
|
|
101
|
+
const lastColon = trimmed.lastIndexOf(":");
|
|
102
|
+
const hasThinkingSuffix =
|
|
103
|
+
lastColon > -1 &&
|
|
104
|
+
thinkingLevels.has(trimmed.slice(lastColon + 1)) &&
|
|
105
|
+
!trimmed.slice(lastColon + 1).includes("/");
|
|
106
|
+
|
|
107
|
+
const modelPart = hasThinkingSuffix ? trimmed.slice(0, lastColon) : trimmed;
|
|
108
|
+
const thinkingPart = hasThinkingSuffix ? trimmed.slice(lastColon + 1) : undefined;
|
|
109
|
+
|
|
110
|
+
const slashIdx = modelPart.indexOf("/");
|
|
111
|
+
const provider = modelPart.slice(0, slashIdx).trim().toLowerCase();
|
|
112
|
+
const id = modelPart.slice(slashIdx + 1).trim();
|
|
113
|
+
|
|
114
|
+
if (!provider || !id) return undefined;
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
provider,
|
|
118
|
+
id,
|
|
119
|
+
thinkingLevel: thinkingPart as
|
|
120
|
+
| "off"
|
|
121
|
+
| "minimal"
|
|
122
|
+
| "low"
|
|
123
|
+
| "medium"
|
|
124
|
+
| "high"
|
|
125
|
+
| "xhigh"
|
|
126
|
+
| undefined
|
|
127
|
+
};
|
|
128
|
+
}
|