forked from Blendman974/faceclaw
Add app tool registration for voice assistant; voice assistant can read and send to terminals
This commit is contained in:
1 parent
0aeb24ed00
commit
fe205c0164
9 files changed
+533
-16
No files matched your search
@@ -26,7 +26,12 @@ export type DirectTurnOptions = {
|
||||
system: string;
|
||||
/** Owned by the session; this loop appends assistant + tool_result messages. */
|
||||
messages: AnthropicMessage[];
|
||||
tools: AnthropicToolDefinition[];
|
||||
/**
|
||||
* Build the tool list for the next request. Called once per loop iteration so
|
||||
* the set reflects apps opening/closing and foreground changes mid-turn; it
|
||||
* also refreshes the session's api-name map that resolveToolName reads.
|
||||
*/
|
||||
buildTools: () => AnthropicToolDefinition[];
|
||||
registry: ToolRegistry;
|
||||
/** Map an API tool name (as the model calls it) back to its registry name. */
|
||||
resolveToolName: (apiName: string) => string;
|
||||
@@ -62,7 +67,7 @@ export class DirectAnthropicBackend {
|
||||
system: options.system,
|
||||
effort: options.effort,
|
||||
messages: options.messages,
|
||||
tools: options.tools,
|
||||
tools: options.buildTools(),
|
||||
onTextDelta: (delta, textSoFar) => {
|
||||
if (cancelled) return;
|
||||
options.callbacks.onTextDelta(delta, textSoFar);
|
||||
|
||||
@@ -60,7 +60,6 @@ export class AssistantSession {
|
||||
this.messages.push({ role: "user", content: text });
|
||||
this.trimHistory();
|
||||
|
||||
const tools = this.buildToolDefinitions();
|
||||
const system = `${SYSTEM_PROMPT_BASE}\n\n${describeContext(ctx)}`;
|
||||
|
||||
const finish = () => {
|
||||
@@ -74,7 +73,7 @@ export class AssistantSession {
|
||||
effort: "low",
|
||||
system,
|
||||
messages: this.messages,
|
||||
tools,
|
||||
buildTools: () => this.buildToolDefinitions(),
|
||||
registry: this.registry,
|
||||
resolveToolName: (apiName) => this.toolNameMap.get(apiName) ?? apiName,
|
||||
callbacks: {
|
||||
|
||||
@@ -70,14 +70,28 @@ export type CallToolOptions = {
|
||||
|
||||
const DEFAULT_TOOL_TIMEOUT_MS = 10_000;
|
||||
|
||||
/** How the shell reaches a window's tool handler and gates its foreground tools. */
|
||||
export type AppToolProvider = {
|
||||
windowId: string;
|
||||
appId: string;
|
||||
/** The window's declared specs, with UNPREFIXED names (registry adds the prefix). */
|
||||
specs: ToolSpec[];
|
||||
/** Invoke a tool on the window by its unprefixed name. */
|
||||
invoke: (toolName: string, args: unknown) => Promise<ToolResult> | ToolResult;
|
||||
/** Whether the window currently owns the foreground (gates `foreground` tools). */
|
||||
isForeground: () => boolean;
|
||||
};
|
||||
|
||||
export class ToolRegistry {
|
||||
private readonly registrations = new Map<string, ToolRegistration>();
|
||||
private readonly changeListeners = new Set<() => void>();
|
||||
// windowId -> the canonical (prefixed) tool names it contributed, for bulk
|
||||
// removal when the window closes or re-declares.
|
||||
private readonly windowToolNames = new Map<string, string[]>();
|
||||
|
||||
/** Register (or replace) a tool. Names are unique across all tiers. */
|
||||
register(registration: ToolRegistration): void {
|
||||
const { name } = registration.spec;
|
||||
this.registrations.set(name, registration);
|
||||
this.addRegistration(registration);
|
||||
this.fireToolsChanged();
|
||||
}
|
||||
|
||||
@@ -95,6 +109,52 @@ export class ToolRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Declare (replacing any prior set) the tools contributed by one app window.
|
||||
* Names are prefixed `app.<appId>.` so windows can't shadow system tools or
|
||||
* each other. `open` tools stay live while the window exists; `foreground`
|
||||
* tools are gated on `isForeground()` at both list and call time.
|
||||
*/
|
||||
setAppTools(provider: AppToolProvider): void {
|
||||
this.clearWindowTools(provider.windowId, false);
|
||||
const names: string[] = [];
|
||||
for (const spec of provider.specs) {
|
||||
if (spec.availability !== "open" && spec.availability !== "foreground") {
|
||||
console.warn(`app tool ${spec.name} has non-app availability ${spec.availability}; skipping`);
|
||||
continue;
|
||||
}
|
||||
const canonical = `app.${provider.appId}.${spec.name}`;
|
||||
const toolName = spec.name;
|
||||
this.addRegistration({
|
||||
spec: { ...spec, name: canonical },
|
||||
handler: (args) => provider.invoke(toolName, args),
|
||||
isAvailable: spec.availability === "foreground" ? () => provider.isForeground() : undefined,
|
||||
});
|
||||
names.push(canonical);
|
||||
}
|
||||
this.windowToolNames.set(provider.windowId, names);
|
||||
this.fireToolsChanged();
|
||||
}
|
||||
|
||||
/** Remove all tools contributed by a window (its worker closed or the window did). */
|
||||
removeAppTools(windowId: string): void {
|
||||
this.clearWindowTools(windowId, true);
|
||||
}
|
||||
|
||||
private clearWindowTools(windowId: string, fire: boolean): void {
|
||||
const names = this.windowToolNames.get(windowId);
|
||||
if (!names) return;
|
||||
for (const name of names) {
|
||||
this.registrations.delete(name);
|
||||
}
|
||||
this.windowToolNames.delete(windowId);
|
||||
if (fire) this.fireToolsChanged();
|
||||
}
|
||||
|
||||
private addRegistration(registration: ToolRegistration): void {
|
||||
this.registrations.set(registration.spec.name, registration);
|
||||
}
|
||||
|
||||
/** Specs for the tools callable right now, given the availability tiers. */
|
||||
listTools(options: ListToolsOptions = {}): ToolSpec[] {
|
||||
const specs: ToolSpec[] = [];
|
||||
|
||||
Reference in new issue
Block a user