@shanepadgett/tau-agent 0.45.0 → 0.46.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/shared/events.ts CHANGED
@@ -85,10 +85,15 @@ interface TauEventSubscription {
85
85
  stop(): void;
86
86
  }
87
87
 
88
- const tauEventSubscriptions = new WeakMap<
88
+ type TauEventSubscriptionRegistry = WeakMap<
89
89
  ExtensionAPI["events"],
90
90
  Map<string, Map<keyof TauAgentEvents, TauEventSubscription>>
91
- >();
91
+ >;
92
+
93
+ // Global and project installs load separate module instances but share one pi.events bus.
94
+ const registryKey = Symbol.for("tau-agent.eventSubscriptions");
95
+ const registryHost = globalThis as typeof globalThis & { [registryKey]?: TauEventSubscriptionRegistry };
96
+ const tauEventSubscriptions: TauEventSubscriptionRegistry = (registryHost[registryKey] ??= new WeakMap());
92
97
 
93
98
  export function emitTauEvent<Name extends keyof TauAgentEvents>(
94
99
  pi: EmitEventAPI,
@@ -24,6 +24,7 @@ interface GenerationContext {
24
24
  }
25
25
 
26
26
  interface ModelFallbackOptions {
27
+ sessionId?: string;
27
28
  statusKey?: string;
28
29
  notifyOnFallback?: boolean;
29
30
  maxAttempts?: number;
@@ -78,12 +79,13 @@ export async function generateValidated<T>(
78
79
  export async function generateToolValidated<T>(
79
80
  ctx: GenerationContext,
80
81
  candidates: readonly ModelCandidate[],
81
- prompt: string,
82
+ prompt: string | readonly Message[],
82
83
  tool: Tool,
83
84
  validate: (input: unknown) => T,
84
85
  correctionPrompt?: (error: Error, output: string) => string,
85
86
  options?: ModelFallbackOptions,
86
87
  ): Promise<{ value: T; candidate: ModelCandidate }> {
88
+ const sessionId = options?.sessionId ?? randomUUID();
87
89
  return withModelFallback(ctx, candidates, options, (candidate) =>
88
90
  requestToolValidated(
89
91
  ctx,
@@ -91,6 +93,7 @@ export async function generateToolValidated<T>(
91
93
  prompt,
92
94
  tool,
93
95
  validate,
96
+ sessionId,
94
97
  correctionPrompt,
95
98
  options?.maxAttempts ?? MAX_TOOL_ATTEMPTS,
96
99
  ),
@@ -207,14 +210,17 @@ function validateSingleToolCall(tool: Tool, toolCalls: readonly { name: string;
207
210
  async function requestToolValidated<T>(
208
211
  ctx: GenerationContext,
209
212
  candidate: ModelCandidate,
210
- prompt: string,
213
+ prompt: string | readonly Message[],
211
214
  tool: Tool,
212
215
  validate: (input: unknown) => T,
216
+ sessionId: string,
213
217
  correctionPrompt?: (error: Error, output: string) => string,
214
218
  maxAttempts = MAX_TOOL_ATTEMPTS,
215
219
  ): Promise<T> {
216
- const messages: Message[] = [{ role: "user", content: [{ type: "text", text: prompt }], timestamp: Date.now() }];
217
- const sessionId = randomUUID();
220
+ const messages: Message[] =
221
+ typeof prompt === "string"
222
+ ? [{ role: "user", content: [{ type: "text", text: prompt }], timestamp: Date.now() }]
223
+ : [...prompt];
218
224
 
219
225
  for (let attempt = 1; attempt <= maxAttempts; attempt++) {
220
226
  const response = await completeCandidate(ctx, candidate, messages, sessionId, [tool]);