Repository navigation
Expand file tree
/
Copy pathlib.ts
More file actions
149 lines (136 loc) Β· 4.41 KB
/
Copy pathlib.ts
File metadata and controls
149 lines (136 loc) Β· 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import chalk from "chalk";
export interface ThoughtData {
thought: string;
thoughtNumber: number;
totalThoughts: number;
isRevision?: boolean;
revisesThought?: number;
branchFromThought?: number;
branchId?: string;
needsMoreThoughts?: boolean;
nextThoughtNeeded: boolean;
}
export class SequentialThinkingServer {
private thoughtHistory: ThoughtData[] = [];
// Null prototype, so any string branchId (even "constructor" or "__proto__")
// is an ordinary own key rather than an inherited Object.prototype member.
private branches: Record<string, ThoughtData[]> = Object.create(null);
private disableThoughtLogging: boolean;
constructor() {
this.disableThoughtLogging =
(process.env.DISABLE_THOUGHT_LOGGING || "").toLowerCase() === "true";
}
private formatThought(thoughtData: ThoughtData): string {
const {
thoughtNumber,
totalThoughts,
thought,
isRevision,
revisesThought,
branchFromThought,
branchId,
} = thoughtData;
// The label is coloured only after the header is measured, so the border
// and the header's padding are sized from the visible text, not from
// chalk's escape codes. A missing
// revisesThought or branchId is left out of the header rather than printed
// as "undefined".
let label: string;
let colour: (text: string) => string;
let context = "";
if (isRevision) {
label = "π Revision";
colour = chalk.yellow;
if (revisesThought !== undefined) {
context = ` (revising thought ${revisesThought})`;
}
} else if (branchFromThought) {
label = "πΏ Branch";
colour = chalk.green;
context =
branchId !== undefined
? ` (from thought ${branchFromThought}, ID: ${branchId})`
: ` (from thought ${branchFromThought})`;
} else {
label = "π Thought";
colour = chalk.blue;
}
const counts = ` ${thoughtNumber}/${totalThoughts}${context}`;
const visibleHeader = `${label}${counts}`;
const border = "β".repeat(
Math.max(visibleHeader.length, thought.length) + 4,
);
// The header row is padded to the border's width like the thought row, by
// the visible text's length. Each label's emoji is one surrogate pair, so
// its length of 2 is also its display width in a terminal.
const padding = " ".repeat(border.length - 2 - visibleHeader.length);
const header = `${colour(label)}${counts}${padding}`;
return `
β${border}β
β ${header} β
β${border}β€
β ${thought.padEnd(border.length - 2)} β
β${border}β`;
}
public processThought(input: ThoughtData): {
content: Array<{ type: "text"; text: string }>;
isError?: boolean;
} {
try {
// Validation happens at the tool registration layer via Zod
// Adjust totalThoughts if thoughtNumber exceeds it
if (input.thoughtNumber > input.totalThoughts) {
input.totalThoughts = input.thoughtNumber;
}
// Log before touching any state: drawing and writing the box are the
// steps that can throw, so a failed call leaves thoughtHistory and
// branches as they were.
if (!this.disableThoughtLogging) {
const formattedThought = this.formatThought(input);
console.error(formattedThought);
}
this.thoughtHistory.push(input);
if (input.branchFromThought && input.branchId) {
if (!this.branches[input.branchId]) {
this.branches[input.branchId] = [];
}
this.branches[input.branchId].push(input);
}
return {
content: [
{
type: "text" as const,
text: JSON.stringify(
{
thoughtNumber: input.thoughtNumber,
totalThoughts: input.totalThoughts,
nextThoughtNeeded: input.nextThoughtNeeded,
branches: Object.keys(this.branches),
thoughtHistoryLength: this.thoughtHistory.length,
},
null,
2,
),
},
],
};
} catch (error) {
return {
content: [
{
type: "text" as const,
text: JSON.stringify(
{
error: error instanceof Error ? error.message : String(error),
status: "failed",
},
null,
2,
),
},
],
isError: true,
};
}
}
}