( toolName: string, params: Record<string, unknown> | undefined, result: ToolCallResult, context: ExecutionContext )
| 52 | } |
| 53 | |
| 54 | export async function maybeWriteOutputToTable( |
| 55 | toolName: string, |
| 56 | params: Record<string, unknown> | undefined, |
| 57 | result: ToolCallResult, |
| 58 | context: ExecutionContext |
| 59 | ): Promise<ToolCallResult> { |
| 60 | if (toolName !== FunctionExecute.id) return result |
| 61 | if (!result.success || !result.output) return result |
| 62 | if (!context.workspaceId || !context.userId) return result |
| 63 | |
| 64 | const outputTable = params?.outputTable as string | undefined |
| 65 | if (!outputTable) return result |
| 66 | |
| 67 | const denied = denyOutputWriteWithoutWritePermission(context) |
| 68 | if (denied) return denied |
| 69 | |
| 70 | return withCopilotSpan( |
| 71 | TraceSpan.CopilotToolsWriteOutputTable, |
| 72 | { |
| 73 | [TraceAttr.ToolName]: toolName, |
| 74 | [TraceAttr.CopilotTableId]: outputTable, |
| 75 | [TraceAttr.WorkspaceId]: context.workspaceId, |
| 76 | }, |
| 77 | async (span) => { |
| 78 | try { |
| 79 | const table = await getTableById(outputTable) |
| 80 | if (!table || table.workspaceId !== context.workspaceId) { |
| 81 | span.setAttribute(TraceAttr.CopilotTableOutcome, CopilotTableOutcome.TableNotFound) |
| 82 | return { |
| 83 | success: false, |
| 84 | error: `Table "${outputTable}" not found`, |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | const rawOutput = result.output |
| 89 | let rows: Array<Record<string, unknown>> |
| 90 | |
| 91 | if (rawOutput && typeof rawOutput === 'object' && 'result' in rawOutput) { |
| 92 | const inner = (rawOutput as Record<string, unknown>).result |
| 93 | if (Array.isArray(inner)) { |
| 94 | rows = inner |
| 95 | } else { |
| 96 | span.setAttribute(TraceAttr.CopilotTableOutcome, CopilotTableOutcome.InvalidShape) |
| 97 | return { |
| 98 | success: false, |
| 99 | error: 'outputTable requires the code to return an array of objects', |
| 100 | } |
| 101 | } |
| 102 | } else if (Array.isArray(rawOutput)) { |
| 103 | rows = rawOutput |
| 104 | } else { |
| 105 | span.setAttribute(TraceAttr.CopilotTableOutcome, CopilotTableOutcome.InvalidShape) |
| 106 | return { |
| 107 | success: false, |
| 108 | error: 'outputTable requires the code to return an array of objects', |
| 109 | } |
| 110 | } |
| 111 |
no test coverage detected