Showing the changes to apps/server/src (2 of 4 files). Show all
Unified · Split
| @@ -8,9 +8,10 @@ · 7 unmodified lines | |||
| 8 | import { readFileSync } from "node:fs"; | 8 | import { readFileSync } from "node:fs"; |
| 9 | import { createApp } from "./routes.js"; | 9 | import { createApp } from "./routes.js"; |
| 10 | import { createPiAiModel } from "@openassistant/core"; | 10 | import { createPiAiModel } from "@openassistant/core"; |
| 11 | import { mergeToolTrees, type ToolTree } from "@openassistant/core/tools"; | 11 | import { defineTool, mergeToolTrees, type ToolTree } from "@openassistant/core/tools"; |
| 12 | import { generateMcpTools } from "@openassistant/tool-gen/mcp"; | 12 | import { generateMcpTools } from "@openassistant/tool-gen/mcp"; |
| 13 | import type { McpToolSource } from "@openassistant/tool-gen/mcp"; | 13 | import type { McpToolSource } from "@openassistant/tool-gen/mcp"; |
| 14 | import { z } from "zod"; | ||
| 14 | 15 | ||
| 15 | // --------------------------------------------------------------------------- | 16 | // --------------------------------------------------------------------------- |
| 16 | // API key resolution | 17 | // 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 | |||
| 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 | |||
| 56 | // --------------------------------------------------------------------------- | 107 | // --------------------------------------------------------------------------- |
| 57 | // Tool loading | 108 | // Tool loading |
| 58 | // --------------------------------------------------------------------------- | 109 | // --------------------------------------------------------------------------- |
| 59 | 110 | ||
| 60 | async function loadTools(): Promise<ToolTree> { | 111 | async 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 { |
| @@ -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 | }, |