@oh-my-pi/pi-coding-agent 15.5.13 → 15.5.15

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.
Files changed (39) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/dist/types/config/model-registry.d.ts +1 -1
  3. package/dist/types/config/models-config-schema.d.ts +2 -0
  4. package/dist/types/config/settings-schema.d.ts +1 -10
  5. package/dist/types/eval/__tests__/llm-bridge.test.d.ts +1 -0
  6. package/dist/types/eval/llm-bridge.d.ts +25 -0
  7. package/dist/types/export/html/template.generated.d.ts +1 -1
  8. package/dist/types/extensibility/plugins/legacy-pi-compat.d.ts +15 -0
  9. package/dist/types/modes/theme/theme.d.ts +2 -1
  10. package/dist/types/session/agent-session.d.ts +2 -0
  11. package/dist/types/tools/index.d.ts +0 -1
  12. package/package.json +8 -8
  13. package/src/config/model-registry.ts +89 -5
  14. package/src/config/models-config-schema.ts +1 -1
  15. package/src/config/settings-schema.ts +1 -10
  16. package/src/eval/__tests__/llm-bridge.test.ts +297 -0
  17. package/src/eval/js/shared/prelude.txt +8 -0
  18. package/src/eval/js/tool-bridge.ts +4 -0
  19. package/src/eval/llm-bridge.ts +181 -0
  20. package/src/eval/py/prelude.py +52 -31
  21. package/src/export/html/template.generated.ts +1 -1
  22. package/src/export/html/template.js +0 -13
  23. package/src/extensibility/plugins/legacy-pi-compat.ts +60 -23
  24. package/src/internal-urls/docs-index.generated.ts +3 -4
  25. package/src/main.ts +4 -0
  26. package/src/modes/components/model-selector.ts +119 -22
  27. package/src/modes/components/status-line/presets.ts +1 -0
  28. package/src/modes/components/status-line/segments.ts +23 -0
  29. package/src/modes/interactive-mode.ts +22 -87
  30. package/src/modes/theme/theme.ts +7 -0
  31. package/src/prompts/tools/eval.md +2 -0
  32. package/src/session/agent-session.ts +19 -0
  33. package/src/session/session-manager.ts +47 -0
  34. package/src/tools/eval.ts +24 -48
  35. package/src/tools/index.ts +0 -4
  36. package/src/tools/renderers.ts +0 -2
  37. package/dist/types/tools/calculator.d.ts +0 -77
  38. package/src/prompts/tools/calculator.md +0 -10
  39. package/src/tools/calculator.ts +0 -541
@@ -1,541 +0,0 @@
1
- import type { AgentTool, AgentToolResult } from "@oh-my-pi/pi-agent-core";
2
- import type { Component } from "@oh-my-pi/pi-tui";
3
- import { Text } from "@oh-my-pi/pi-tui";
4
- import { prompt, untilAborted } from "@oh-my-pi/pi-utils";
5
- import * as z from "zod/v4";
6
- import type { RenderResultOptions } from "../extensibility/custom-tools/types";
7
- import type { Theme } from "../modes/theme/theme";
8
- import calculatorDescription from "../prompts/tools/calculator.md" with { type: "text" };
9
- import { Ellipsis, Hasher, type RenderCache, renderStatusLine, renderTreeList, truncateToWidth } from "../tui";
10
- import type { ToolSession } from ".";
11
- import { formatCount, formatEmptyMessage, formatErrorMessage, PREVIEW_LIMITS, TRUNCATE_LENGTHS } from "./render-utils";
12
-
13
- // =============================================================================
14
- // Token Types
15
- // =============================================================================
16
-
17
- /** Supported arithmetic operators (** is exponentiation). */
18
- type Operator = "+" | "-" | "*" | "/" | "%" | "**";
19
-
20
- /**
21
- * Lexer token variants:
22
- * - number: parsed numeric value with original string for error messages
23
- * - operator: arithmetic operator
24
- * - paren: grouping parenthesis
25
- */
26
- type Token =
27
- | { type: "number"; value: number; raw: string }
28
- | { type: "operator"; value: Operator }
29
- | { type: "paren"; value: "(" | ")" };
30
-
31
- const calculatorSchema = z.object({
32
- calculations: z
33
- .array(
34
- z.object({
35
- expression: z.string().describe("math expression"),
36
- prefix: z.string().describe("prefix text"),
37
- suffix: z.string().describe("suffix text"),
38
- }),
39
- )
40
- .describe("calculations to evaluate"),
41
- });
42
-
43
- export interface CalculatorToolDetails {
44
- results: Array<{ expression: string; value: number; output: string }>;
45
- }
46
-
47
- // =============================================================================
48
- // Character classification helpers for numeric literal parsing
49
- // =============================================================================
50
-
51
- function isDigit(ch: string): boolean {
52
- return ch >= "0" && ch <= "9";
53
- }
54
-
55
- function isHexDigit(ch: string): boolean {
56
- return (ch >= "0" && ch <= "9") || (ch >= "a" && ch <= "f") || (ch >= "A" && ch <= "F");
57
- }
58
-
59
- function isBinaryDigit(ch: string): boolean {
60
- return ch === "0" || ch === "1";
61
- }
62
-
63
- function isOctalDigit(ch: string): boolean {
64
- return ch >= "0" && ch <= "7";
65
- }
66
-
67
- // =============================================================================
68
- // Tokenizer
69
- // =============================================================================
70
-
71
- /**
72
- * Tokenize a math expression into numbers, operators, and parentheses.
73
- *
74
- * Number formats supported:
75
- * - Decimal: 123, 3.14, .5
76
- * - Scientific: 1e10, 2.5E-3
77
- * - Hexadecimal: 0xFF
78
- * - Binary: 0b1010
79
- * - Octal: 0o755
80
- */
81
- function tokenizeExpression(expression: string): Token[] {
82
- const tokens: Token[] = [];
83
- let i = 0;
84
-
85
- while (i < expression.length) {
86
- const ch = expression[i];
87
-
88
- // Skip whitespace
89
- if (ch.trim() === "") {
90
- i += 1;
91
- continue;
92
- }
93
-
94
- if (ch === "(" || ch === ")") {
95
- tokens.push({ type: "paren", value: ch });
96
- i += 1;
97
- continue;
98
- }
99
-
100
- // Check ** before single * to handle exponentiation
101
- if (ch === "*" && expression[i + 1] === "*") {
102
- tokens.push({ type: "operator", value: "**" });
103
- i += 2;
104
- continue;
105
- }
106
-
107
- if (ch === "+" || ch === "-" || ch === "*" || ch === "/" || ch === "%") {
108
- tokens.push({ type: "operator", value: ch });
109
- i += 1;
110
- continue;
111
- }
112
-
113
- // Number parsing: starts with digit or decimal point followed by digit
114
- const next = expression[i + 1];
115
- const numberStart = isDigit(ch) || (ch === "." && next !== undefined && isDigit(next));
116
- if (!numberStart) {
117
- throw new Error(`Invalid character "${ch}" in expression`);
118
- }
119
-
120
- const start = i;
121
-
122
- // Handle prefixed literals (0x, 0b, 0o)
123
- if (ch === "0" && next !== undefined) {
124
- const prefix = next.toLowerCase();
125
- if (prefix === "x" || prefix === "b" || prefix === "o") {
126
- i += 2; // Skip "0x" / "0b" / "0o"
127
- let hasDigit = false;
128
- while (i < expression.length) {
129
- const digit = expression[i];
130
- const valid =
131
- prefix === "x" ? isHexDigit(digit) : prefix === "b" ? isBinaryDigit(digit) : isOctalDigit(digit);
132
- if (!valid) break;
133
- hasDigit = true;
134
- i += 1;
135
- }
136
-
137
- if (!hasDigit) {
138
- throw new Error(`Invalid numeric literal starting at "${expression.slice(start, i)}"`);
139
- }
140
-
141
- const raw = expression.slice(start, i);
142
- const value = Number(raw); // JS Number() handles 0x/0b/0o natively
143
- if (!Number.isFinite(value)) {
144
- throw new Error(`Invalid number "${raw}"`);
145
- }
146
- tokens.push({ type: "number", value, raw });
147
- continue;
148
- }
149
- }
150
-
151
- // Parse decimal number: integer part
152
- let hasDigits = false;
153
- while (i < expression.length && isDigit(expression[i])) {
154
- hasDigits = true;
155
- i += 1;
156
- }
157
-
158
- // Fractional part
159
- if (expression[i] === ".") {
160
- i += 1;
161
- while (i < expression.length && isDigit(expression[i])) {
162
- hasDigits = true;
163
- i += 1;
164
- }
165
- }
166
-
167
- if (!hasDigits) {
168
- throw new Error(`Invalid number starting at "${expression.slice(start, i + 1)}"`);
169
- }
170
-
171
- // Scientific notation exponent (e.g., 1e10, 2.5E-3)
172
- if (expression[i] === "e" || expression[i] === "E") {
173
- i += 1;
174
- if (expression[i] === "+" || expression[i] === "-") {
175
- i += 1;
176
- }
177
-
178
- let hasExponentDigits = false;
179
- while (i < expression.length && isDigit(expression[i])) {
180
- hasExponentDigits = true;
181
- i += 1;
182
- }
183
-
184
- if (!hasExponentDigits) {
185
- throw new Error(`Invalid exponent in "${expression.slice(start, i)}"`);
186
- }
187
- }
188
-
189
- const raw = expression.slice(start, i);
190
- const value = Number(raw);
191
- if (!Number.isFinite(value)) {
192
- throw new Error(`Invalid number "${raw}"`);
193
- }
194
- tokens.push({ type: "number", value, raw });
195
- }
196
-
197
- return tokens;
198
- }
199
-
200
- // =============================================================================
201
- // Recursive Descent Parser
202
- // =============================================================================
203
-
204
- /**
205
- * Recursive descent parser for arithmetic expressions.
206
- *
207
- * Operator precedence (lowest to highest):
208
- * 1. Addition, subtraction (+, -)
209
- * 2. Multiplication, division, modulo (*, /, %)
210
- * 3. Unary plus/minus (+x, -x)
211
- * 4. Exponentiation (**)
212
- * 5. Parentheses and literals
213
- *
214
- * Each precedence level has its own parse method. Lower precedence methods
215
- * call higher precedence methods, building the AST implicitly through
216
- * the call stack.
217
- */
218
- class ExpressionParser {
219
- #index = 0;
220
-
221
- constructor(private readonly tokens: Token[]) {}
222
-
223
- /** Parse the full expression and ensure all tokens are consumed. */
224
- parse(): number {
225
- const value = this.#parseExpression();
226
- if (this.#index < this.tokens.length) {
227
- throw new Error("Unexpected token in expression");
228
- }
229
- return value;
230
- }
231
-
232
- /**
233
- * Parse addition and subtraction (lowest precedence).
234
- * Left-associative: 1 - 2 - 3 = (1 - 2) - 3
235
- */
236
- #parseExpression(): number {
237
- let value = this.#parseTerm();
238
- while (true) {
239
- if (this.#matchOperator("+")) {
240
- value += this.#parseTerm();
241
- continue;
242
- }
243
- if (this.#matchOperator("-")) {
244
- value -= this.#parseTerm();
245
- continue;
246
- }
247
- break;
248
- }
249
- return value;
250
- }
251
-
252
- /**
253
- * Parse multiplication, division, and modulo.
254
- * Left-associative: 8 / 4 / 2 = (8 / 4) / 2
255
- */
256
- #parseTerm(): number {
257
- let value = this.#parseUnary();
258
- while (true) {
259
- if (this.#matchOperator("*")) {
260
- value *= this.#parseUnary();
261
- continue;
262
- }
263
- if (this.#matchOperator("/")) {
264
- value /= this.#parseUnary();
265
- continue;
266
- }
267
- if (this.#matchOperator("%")) {
268
- value %= this.#parseUnary();
269
- continue;
270
- }
271
- break;
272
- }
273
- return value;
274
- }
275
-
276
- /**
277
- * Parse unary + and - operators.
278
- * Recursive to handle chained unary: --x, +-x
279
- */
280
- #parseUnary(): number {
281
- if (this.#matchOperator("+")) {
282
- return this.#parseUnary();
283
- }
284
- if (this.#matchOperator("-")) {
285
- return -this.#parseUnary();
286
- }
287
- return this.#parsePower();
288
- }
289
-
290
- /**
291
- * Parse exponentiation operator.
292
- * Right-associative: 2 ** 3 ** 2 = 2 ** (3 ** 2) = 512
293
- * Achieved by recursive call to parsePower for the right operand.
294
- */
295
- #parsePower(): number {
296
- let value = this.#parsePrimary();
297
- if (this.#matchOperator("**")) {
298
- value = value ** this.#parsePower(); // Right-associative via recursion
299
- }
300
- return value;
301
- }
302
-
303
- /**
304
- * Parse primary expressions: number literals and parenthesized subexpressions.
305
- * Parentheses restart parsing at lowest precedence (parseExpression).
306
- */
307
- #parsePrimary(): number {
308
- const token = this.#peek();
309
- if (!token) {
310
- throw new Error("Unexpected end of expression");
311
- }
312
-
313
- if (token.type === "number") {
314
- this.#index += 1;
315
- return token.value;
316
- }
317
-
318
- if (token.type === "paren" && token.value === "(") {
319
- this.#index += 1;
320
- const value = this.#parseExpression(); // Reset to lowest precedence
321
- if (!this.#matchParen(")")) {
322
- throw new Error("Missing closing parenthesis");
323
- }
324
- return value;
325
- }
326
-
327
- throw new Error("Unexpected token in expression");
328
- }
329
-
330
- /** Consume operator if it matches, advancing the token index. */
331
- #matchOperator(value: Operator): boolean {
332
- const token = this.tokens[this.#index];
333
- if (token && token.type === "operator" && token.value === value) {
334
- this.#index += 1;
335
- return true;
336
- }
337
- return false;
338
- }
339
-
340
- /** Consume parenthesis if it matches, advancing the token index. */
341
- #matchParen(value: "(" | ")"): boolean {
342
- const token = this.tokens[this.#index];
343
- if (token && token.type === "paren" && token.value === value) {
344
- this.#index += 1;
345
- return true;
346
- }
347
- return false;
348
- }
349
-
350
- /** Look at current token without consuming it. */
351
- #peek(): Token | undefined {
352
- return this.tokens[this.#index];
353
- }
354
- }
355
-
356
- // =============================================================================
357
- // Expression Evaluator
358
- // =============================================================================
359
-
360
- /**
361
- * Evaluate a math expression string and return the numeric result.
362
- *
363
- * Pipeline: expression string -> tokens -> parse tree (implicit) -> value
364
- *
365
- * @throws Error on syntax errors, empty expressions, or non-finite results (Infinity, NaN)
366
- */
367
- function evaluateExpression(expression: string): number {
368
- const tokens = tokenizeExpression(expression);
369
- if (tokens.length === 0) {
370
- throw new Error("Expression is empty");
371
- }
372
- const parser = new ExpressionParser(tokens);
373
- const value = parser.parse();
374
- if (!Number.isFinite(value)) {
375
- throw new Error("Expression result is not a finite number");
376
- }
377
- // Normalize -0 to 0 for consistent output
378
- return Object.is(value, -0) ? 0 : value;
379
- }
380
-
381
- function formatResult(value: number): string {
382
- return String(value);
383
- }
384
-
385
- // ═══════════════════════════════════════════════════════════════════════════
386
- // Tool Class
387
- // ═══════════════════════════════════════════════════════════════════════════
388
-
389
- type CalculatorParams = z.infer<typeof calculatorSchema>;
390
-
391
- /**
392
- * Calculator tool for evaluating mathematical expressions.
393
- *
394
- * Supports decimal, hex (0x), binary (0b), octal (0o) literals,
395
- * standard arithmetic operators, and parentheses.
396
- */
397
- export class CalculatorTool implements AgentTool<typeof calculatorSchema, CalculatorToolDetails> {
398
- readonly name = "calc";
399
- readonly approval = "read" as const;
400
- readonly label = "Calc";
401
- readonly summary = "Evaluate a mathematical expression";
402
- readonly loadMode = "discoverable";
403
- readonly description: string;
404
- readonly parameters = calculatorSchema;
405
- readonly strict = true;
406
-
407
- constructor(_session: ToolSession) {
408
- this.description = prompt.render(calculatorDescription);
409
- }
410
-
411
- async execute(
412
- _toolCallId: string,
413
- { calculations }: CalculatorParams,
414
- signal?: AbortSignal,
415
- ): Promise<AgentToolResult<CalculatorToolDetails>> {
416
- return untilAborted(signal, async () => {
417
- const results = calculations.map(calc => {
418
- const value = evaluateExpression(calc.expression);
419
- const output = `${calc.prefix}${formatResult(value)}${calc.suffix}`;
420
- return { expression: calc.expression, value, output };
421
- });
422
-
423
- const outputText = results.map(result => result.output).join("\n");
424
- return {
425
- content: [{ type: "text", text: outputText }],
426
- details: { results },
427
- };
428
- });
429
- }
430
- }
431
-
432
- // =============================================================================
433
- // TUI Renderer
434
- // =============================================================================
435
-
436
- interface CalculatorRenderArgs {
437
- calculations?: Array<{ expression: string; prefix?: string; suffix?: string }>;
438
- }
439
-
440
- const COLLAPSED_LIST_LIMIT = PREVIEW_LIMITS.COLLAPSED_ITEMS;
441
-
442
- /**
443
- * TUI renderer for calculator tool calls and results.
444
- * Handles both collapsed (preview) and expanded (full) display modes.
445
- */
446
- export const calculatorToolRenderer = {
447
- /**
448
- * Render the tool call header showing the first expression and count.
449
- * Format: "Calc <expression> (N calcs)"
450
- */
451
- renderCall(args: CalculatorRenderArgs, _options: RenderResultOptions, uiTheme: Theme): Component {
452
- const count = args.calculations?.length ?? 0;
453
- const firstExpression = args.calculations?.[0]?.expression;
454
- const description = firstExpression ? truncateToWidth(firstExpression, TRUNCATE_LENGTHS.TITLE) : undefined;
455
- const meta = count > 0 ? [formatCount("calc", count)] : [];
456
- const text = renderStatusLine({ icon: "pending", title: "Calc", description, meta }, uiTheme);
457
- return new Text(text, 0, 0);
458
- },
459
-
460
- /**
461
- * Render calculation results as a tree list.
462
- * Collapsed mode shows first N items with expand hint; expanded shows all.
463
- */
464
- renderResult(
465
- result: { content: Array<{ type: string; text?: string }>; details?: CalculatorToolDetails; isError?: boolean },
466
- options: RenderResultOptions,
467
- uiTheme: Theme,
468
- args?: CalculatorRenderArgs,
469
- ): Component {
470
- const details = result.details;
471
- const textContent = result.content?.find(c => c.type === "text")?.text ?? "";
472
- if (result.isError) {
473
- const header = renderStatusLine({ icon: "error", title: "Calc" }, uiTheme);
474
- const renderedLines = [header, formatErrorMessage(textContent, uiTheme)];
475
- return {
476
- render() {
477
- return renderedLines;
478
- },
479
- invalidate() {},
480
- };
481
- }
482
-
483
- // Prefer structured details; fall back to parsing text content
484
- let outputs = details?.results?.map(entry => `${entry.expression} = ${entry.output}`) ?? [];
485
- if (outputs.length === 0 && textContent.trim()) {
486
- const rawOutputs = textContent.split("\n").filter(line => line.trim().length > 0);
487
- const expressions = args?.calculations?.map(calc => calc.expression) ?? [];
488
- if (expressions.length === rawOutputs.length && expressions.length > 0) {
489
- outputs = rawOutputs.map((output, index) => `${expressions[index]} = ${output}`);
490
- } else {
491
- outputs = rawOutputs;
492
- }
493
- }
494
-
495
- if (outputs.length === 0) {
496
- const header = renderStatusLine({ icon: "warning", title: "Calc" }, uiTheme);
497
- const renderedLines = [header, formatEmptyMessage("No results", uiTheme)];
498
- return {
499
- render() {
500
- return renderedLines;
501
- },
502
- invalidate() {},
503
- };
504
- }
505
-
506
- const description = args?.calculations?.[0]?.expression
507
- ? truncateToWidth(args.calculations[0].expression, TRUNCATE_LENGTHS.TITLE)
508
- : undefined;
509
- const header = renderStatusLine(
510
- { icon: "success", title: "Calc", description, meta: [formatCount("result", outputs.length)] },
511
- uiTheme,
512
- );
513
-
514
- let cached: RenderCache | undefined;
515
-
516
- return {
517
- render(width) {
518
- const { expanded } = options;
519
- const key = new Hasher().bool(expanded).u32(width).digest();
520
- if (cached?.key === key) return cached.lines;
521
- const treeLines = renderTreeList(
522
- {
523
- items: outputs,
524
- expanded,
525
- maxCollapsed: COLLAPSED_LIST_LIMIT,
526
- itemType: "result",
527
- renderItem: output => uiTheme.fg("toolOutput", output),
528
- },
529
- uiTheme,
530
- );
531
- const lines = [header, ...treeLines].map(l => truncateToWidth(l, width, Ellipsis.Omit));
532
- cached = { key, lines };
533
- return lines;
534
- },
535
- invalidate() {
536
- cached = undefined;
537
- },
538
- };
539
- },
540
- mergeCallAndResult: true,
541
- };