@lincoln504/pi-research 1.3.12 → 1.4.1

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 (71) hide show
  1. package/README.md +2 -2
  2. package/dist/cli.mjs +5495 -4645
  3. package/dist/prompts/research-tool-usage.md +1 -1
  4. package/dist/prompts/researcher.md +1 -0
  5. package/dist/prompts/system-lead-router.md +122 -0
  6. package/dist/prompts/system-lead-synthesizer.md +46 -0
  7. package/dist/thread-worker.mjs +94 -19
  8. package/docs/ARCHITECTURE.md +18 -10
  9. package/docs/CHANGELOG.md +149 -0
  10. package/docs/CONFIGURATION.md +106 -7
  11. package/package.json +2 -2
  12. package/scripts/build.cjs +8 -1
  13. package/skills/pi-research/SKILL.md +1 -1
  14. package/src/cli.ts +5 -1
  15. package/src/config.ts +12 -8
  16. package/src/constants.ts +1 -1
  17. package/src/core/interfaces/observer-interfaces.ts +3 -3
  18. package/src/core/interfaces/orchestration-interfaces.ts +2 -0
  19. package/src/core/interfaces/planning-interfaces.ts +7 -1
  20. package/src/core/interfaces/scheduler-interfaces.ts +18 -2
  21. package/src/core/llm/agentic-repair.ts +7 -2
  22. package/src/core/llm/inject-date.ts +4 -1
  23. package/src/core/llm/llm-utils.ts +7 -0
  24. package/src/core/planning-service.ts +719 -91
  25. package/src/core/planning-utils.ts +21 -5
  26. package/src/core/service-initialization.ts +19 -3
  27. package/src/healthcheck/index.ts +11 -2
  28. package/src/index.ts +4 -3
  29. package/src/infrastructure/browser/browser-client.ts +68 -34
  30. package/src/infrastructure/browser/browser-task-scheduler.ts +220 -34
  31. package/src/infrastructure/browser/config.ts +41 -1
  32. package/src/infrastructure/browser/priority-task-queue.ts +55 -1
  33. package/src/infrastructure/browser/scheduler-factory.ts +12 -1
  34. package/src/infrastructure/browser/task-execution-service.ts +5 -5
  35. package/src/infrastructure/browser/thread-worker-browser.ts +32 -2
  36. package/src/infrastructure/browser/thread-worker-messaging.ts +38 -6
  37. package/src/infrastructure/browser/thread-worker.mjs +94 -19
  38. package/src/infrastructure/browser/thread-worker.ts +2 -2
  39. package/src/infrastructure/browser/worker-pool-manager.ts +127 -12
  40. package/src/infrastructure/embedding/embedding-client.ts +14 -4
  41. package/src/infrastructure/embedding/embedding-factory.ts +83 -15
  42. package/src/infrastructure/file-lock-service.ts +214 -19
  43. package/src/infrastructure/knowledge-store-service.ts +14 -0
  44. package/src/infrastructure/research-run-semaphore.ts +1 -1
  45. package/src/infrastructure/service-initialization.ts +7 -0
  46. package/src/knowledge/embedder-init.ts +43 -17
  47. package/src/logger.ts +11 -0
  48. package/src/orchestration/deep-research-orchestrator.ts +141 -87
  49. package/src/orchestration/quick-research-orchestrator.ts +11 -2
  50. package/src/orchestration/research-orchestration-service.ts +8 -0
  51. package/src/orchestration/research-synthesis-service.ts +53 -1
  52. package/src/orchestration/researcher-executor.ts +53 -2
  53. package/src/orchestration/researcher.ts +15 -2
  54. package/src/prompts/research-tool-usage.md +1 -1
  55. package/src/prompts/researcher.md +1 -0
  56. package/src/prompts/system-lead-router.md +122 -0
  57. package/src/prompts/system-lead-synthesizer.md +46 -0
  58. package/src/tools/research-knowledge-search.ts +11 -3
  59. package/src/tools/scrape.ts +6 -0
  60. package/src/tools/search.ts +7 -0
  61. package/src/tools/security.ts +27 -9
  62. package/src/tools/stackexchange.ts +13 -7
  63. package/src/tools/youtube-transcript.ts +26 -12
  64. package/src/utils/cancellation.ts +36 -0
  65. package/src/utils/coverage-digest.ts +204 -0
  66. package/src/utils/llm-usage.ts +33 -6
  67. package/src/utils/metrics-summary.ts +20 -4
  68. package/src/utils/metrics.ts +27 -1
  69. package/src/web-research/browser-search.ts +93 -20
  70. package/dist/prompts/system-lead-evaluator.md +0 -118
  71. package/src/prompts/system-lead-evaluator.md +0 -118
@@ -34,6 +34,10 @@ export interface JsonRepairOptions {
34
34
  maxTokens?: number;
35
35
  /** Thinking level for the repair pass (default 'off' — repair emits JSON, not reasoning). */
36
36
  thinkingLevel?: ModelThinkingLevel;
37
+ /** Research session id, forwarded to the provider as its prompt-cache / session-affinity
38
+ * key (see llm-utils.buildSafeOptions). Keeps the repair pass on the same upstream
39
+ * replica as the call it is repairing. */
40
+ sessionId?: string;
37
41
  /** Usage sink invoked once per billed LLM attempt with the raw usage object from
38
42
  * the response. Lets callers attribute the repair pass's tokens/cost to the run
39
43
  * (metrics + observer) exactly as they do for the primary call. Every attempt that
@@ -70,7 +74,7 @@ export async function repairJsonWithLlm<T = any>(
70
74
  auth: { apiKey: string; headers?: Record<string, string | null> },
71
75
  options: JsonRepairOptions
72
76
  ): Promise<T | null> {
73
- const { model, context, schema, serviceName = 'RepairService', signal, maxTokens = 16384, thinkingLevel = 'off' } = options;
77
+ const { model, context, schema, serviceName = 'RepairService', signal, maxTokens = 16384, thinkingLevel = 'off', sessionId } = options;
74
78
 
75
79
  logger.warn(`[${serviceName}] JSON parse failed; attempting agentic salvage`);
76
80
 
@@ -118,7 +122,8 @@ Return ONLY the valid JSON object. No prose before or after.`;
118
122
  ],
119
123
  }, buildSafeOptions(model, {
120
124
  ...auth,
121
- signal
125
+ signal,
126
+ ...(sessionId ? { sessionId } : {}),
122
127
  }, maxTokens, thinkingLevel)),
123
128
  llmTimeout, `agentic-repair-${serviceName}`,
124
129
  );
@@ -35,7 +35,10 @@ function getCurrentDateString(): string {
35
35
  * training-cutoff defaults). The full date follows for precision. Applies to
36
36
  * coordinator, researcher, and evaluator across all modes.
37
37
  */
38
- export function injectCurrentDate(prompt: string, _agentType: 'coordinator' | 'researcher' | 'evaluator'): string {
38
+ export function injectCurrentDate(
39
+ prompt: string,
40
+ _agentType: 'coordinator' | 'researcher' | 'router' | 'synthesizer',
41
+ ): string {
39
42
  const monthYear = getCurrentMonthYearString();
40
43
  const fullDate = getCurrentDateString();
41
44
  // Derive the stale-year examples from the current year so the guidance stays correct in any
@@ -22,6 +22,13 @@ import { logger } from '../../logger.ts';
22
22
  * thinking block only consumes the output-token budget (often truncating before the
23
23
  * text block is emitted) for at most a marginal quality gain that does not justify the cost.
24
24
  *
25
+ * Callers should also pass the research `sessionId` in `options`. pi-ai forwards it as the
26
+ * provider's prompt-cache / session-affinity key — OpenAI's `prompt_cache_key`, OpenRouter's
27
+ * `x-session-id` — which keeps a run's calls on the replica that warmed their cache. Without
28
+ * it OpenRouter falls back to hashing the first system and user messages, which every round
29
+ * of a research run mutates, so it re-routes and re-warms. Providers that do not implement
30
+ * session affinity ignore the field, so passing it is unconditionally safe.
31
+ *
25
32
  * @param model - The model being called
26
33
  * @param options - Caller-provided options (an explicit `reasoning` here wins)
27
34
  * @param defaultCap - Default maxTokens cap (clamped to model.maxTokens)