History

approval flow works

Rhys Sullivan committed 9 months ago

Commit 98faa991 parent 378b5704 files · +196 −249Browse files

Showing the changes to apps/server/src (2 of 4 files). Show all

Unified · Split

2 files
  1. apps/server/src/index.ts +53 −2
  2. apps/server/src/task-runner.ts +18 −2
apps/server/src/index.ts+53−2
@@ -8,9 +8,10 @@ · 7 unmodified lines
88 import { readFileSync } from "node:fs";
99 import { createApp } from "./routes.js";
1010 import { createPiAiModel } from "@openassistant/core";
11−import { mergeToolTrees, type ToolTree } from "@openassistant/core/tools";
11+import { defineTool, mergeToolTrees, type ToolTree } from "@openassistant/core/tools";
1212 import { generateMcpTools } from "@openassistant/tool-gen/mcp";
1313 import type { McpToolSource } from "@openassistant/tool-gen/mcp";
14+import { z } from "zod";
1415
1516 // ---------------------------------------------------------------------------
1617 // API key resolution
@@ -53,12 +54,62 @@ const mcpSources: McpToolSource[] = [ · 36 unmodified lines
5354 },
5455 ];
5556
57+// ---------------------------------------------------------------------------
58+// Hand-written tools (approval testing)
59+// ---------------------------------------------------------------------------
60+
61+const builtinTools: ToolTree = {
62+ admin: {
63+ /** Requires approval — lets you test the approval UI flow. */
64+ send_announcement: defineTool({
65+ description: "Send an announcement message to a channel. Requires approval because it posts publicly.",
66+ approval: "required",
67+ args: z.object({
68+ channel: z.string().describe("Channel name to post in"),
69+ message: z.string().describe("The announcement text"),
70+ }),
71+ returns: z.object({ sent: z.boolean(), channel: z.string(), message: z.string() }),
72+ run: async (input) => ({ sent: true, channel: input.channel, message: input.message }),
73+ formatApproval: (input) => ({
74+ title: `Post to #${input.channel}`,
75+ details: `Message: "${input.message}"`,
76+ }),
77+ }),
78+
79+ /** Requires approval — destructive action. */
80+ delete_data: defineTool({
81+ description: "Delete stored data by key. Requires approval because it's destructive.",
82+ approval: "required",
83+ args: z.object({
84+ key: z.string().describe("The data key to delete"),
85+ }),
86+ returns: z.object({ deleted: z.boolean(), key: z.string() }),
87+ run: async (input) => ({ deleted: true, key: input.key }),
88+ formatApproval: (input) => ({
89+ title: `Delete "${input.key}"`,
90+ details: "This action cannot be undone.",
91+ }),
92+ }),
93+ },
94+
95+ utils: {
96+ /** Auto-approved — safe read-only tool. */
97+ get_time: defineTool({
98+ description: "Get the current date and time.",
99+ approval: "auto",
100+ args: z.object({}),
101+ returns: z.object({ iso: z.string(), unix: z.number() }),
102+ run: async () => ({ iso: new Date().toISOString(), unix: Date.now() }),
103+ }),
104+ },
105+};
106+
56107 // ---------------------------------------------------------------------------
57108 // Tool loading
58109 // ---------------------------------------------------------------------------
59110
60111 async function loadTools(): Promise<ToolTree> {
61− const trees: ToolTree[] = [];
112+ const trees: ToolTree[] = [builtinTools];
62113
63114 for (const source of mcpSources) {
64115 try {
apps/server/src/task-runner.ts+18−2
@@ -37,14 +37,30 @@ export async function runTask( · 36 unmodified lines
3737 tools: options.tools,
3838 model: options.model,
3939 requestApproval: (request: ApprovalRequest): Promise<ApprovalDecision> => {
40+ // Emit the approval_request event so SSE subscribers see it
41+ emitTaskEvent(taskId, {
42+ type: "approval_request",
43+ id: request.callId,
44+ toolPath: request.toolPath,
45+ input: request.input,
46+ preview: request.preview,
47+ });
48+
4049 return new Promise<ApprovalDecision>((resolve) => {
41− // Register the pending approval so REST API can resolve it
4250 registerApproval({
4351 callId: request.callId,
4452 taskId,
4553 toolPath: request.toolPath,
4654 input: request.input,
47− resolve,
55+ resolve: (decision: ApprovalDecision) => {
56+ // Emit resolved event before unblocking the runner
57+ emitTaskEvent(taskId, {
58+ type: "approval_resolved",
59+ id: request.callId,
60+ decision,
61+ });
62+ resolve(decision);
63+ },
4864 });
4965 });
5066 },

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t