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
8import { readFileSync } from "node:fs";8import { readFileSync } from "node:fs";
9import { createApp } from "./routes.js";9import { createApp } from "./routes.js";
10import { createPiAiModel } from "@openassistant/core";10import { createPiAiModel } from "@openassistant/core";
11import { mergeToolTrees, type ToolTree } from "@openassistant/core/tools";11import { defineTool, mergeToolTrees, type ToolTree } from "@openassistant/core/tools";
12import { generateMcpTools } from "@openassistant/tool-gen/mcp";12import { generateMcpTools } from "@openassistant/tool-gen/mcp";
13import type { McpToolSource } from "@openassistant/tool-gen/mcp";13import type { McpToolSource } from "@openassistant/tool-gen/mcp";
14import { z } from "zod";
14 15
15// ---------------------------------------------------------------------------16// ---------------------------------------------------------------------------
16// API key resolution17// API key resolution
@@ -53,12 +54,62 @@ const mcpSources: McpToolSource[] = [ · 36 unmodified lines
53 },54 },
54];55];
55 56
57// ---------------------------------------------------------------------------
58// Hand-written tools (approval testing)
59// ---------------------------------------------------------------------------
60
61const 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
56// ---------------------------------------------------------------------------107// ---------------------------------------------------------------------------
57// Tool loading108// Tool loading
58// ---------------------------------------------------------------------------109// ---------------------------------------------------------------------------
59 110
60async function loadTools(): Promise<ToolTree> {111async function loadTools(): Promise<ToolTree> {
61 const trees: ToolTree[] = [];112 const trees: ToolTree[] = [builtinTools];
62 113
63 for (const source of mcpSources) {114 for (const source of mcpSources) {
64 try {115 try {
apps/server/src/task-runner.ts+18−2
@@ -37,14 +37,30 @@ export async function runTask( · 36 unmodified lines
37 tools: options.tools,37 tools: options.tools,
38 model: options.model,38 model: options.model,
39 requestApproval: (request: ApprovalRequest): Promise<ApprovalDecision> => {39 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
40 return new Promise<ApprovalDecision>((resolve) => {49 return new Promise<ApprovalDecision>((resolve) => {
41 // Register the pending approval so REST API can resolve it
42 registerApproval({50 registerApproval({
43 callId: request.callId,51 callId: request.callId,
44 taskId,52 taskId,
45 toolPath: request.toolPath,53 toolPath: request.toolPath,
46 input: request.input,54 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 },
48 });64 });
49 });65 });
50 },66 },

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