mirror of
https://github.com/simstudioai/sim.git
synced 2026-01-10 23:48:09 -05:00
added legit urls to the metadata (#1665)
Co-authored-by: Adam Gough <adamgough@Adams-MacBook-Pro.local>
This commit is contained in:
@@ -3,7 +3,10 @@ import type {
|
||||
MicrosoftExcelReadResponse,
|
||||
MicrosoftExcelToolParams,
|
||||
} from '@/tools/microsoft_excel/types'
|
||||
import { trimTrailingEmptyRowsAndColumns } from '@/tools/microsoft_excel/utils'
|
||||
import {
|
||||
getSpreadsheetWebUrl,
|
||||
trimTrailingEmptyRowsAndColumns,
|
||||
} from '@/tools/microsoft_excel/utils'
|
||||
import type { ToolConfig } from '@/tools/types'
|
||||
|
||||
export const readTool: ToolConfig<MicrosoftExcelToolParams, MicrosoftExcelReadResponse> = {
|
||||
@@ -126,10 +129,13 @@ export const readTool: ToolConfig<MicrosoftExcelToolParams, MicrosoftExcelReadRe
|
||||
|
||||
const values = trimTrailingEmptyRowsAndColumns(rawValues)
|
||||
|
||||
// Fetch the browser-accessible web URL
|
||||
const webUrl = await getSpreadsheetWebUrl(spreadsheetIdFromUrl, accessToken)
|
||||
|
||||
const metadata = {
|
||||
spreadsheetId: spreadsheetIdFromUrl,
|
||||
properties: {},
|
||||
spreadsheetUrl: `https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetIdFromUrl}`,
|
||||
spreadsheetUrl: webUrl,
|
||||
}
|
||||
|
||||
const result: MicrosoftExcelReadResponse = {
|
||||
@@ -155,10 +161,17 @@ export const readTool: ToolConfig<MicrosoftExcelToolParams, MicrosoftExcelReadRe
|
||||
const urlParts = response.url.split('/drive/items/')
|
||||
const spreadsheetId = urlParts[1]?.split('/')[0] || ''
|
||||
|
||||
// Fetch the browser-accessible web URL
|
||||
const accessToken = params?.accessToken
|
||||
if (!accessToken) {
|
||||
throw new Error('Access token is required')
|
||||
}
|
||||
const webUrl = await getSpreadsheetWebUrl(spreadsheetId, accessToken)
|
||||
|
||||
const metadata = {
|
||||
spreadsheetId,
|
||||
properties: {},
|
||||
spreadsheetUrl: `https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetId}`,
|
||||
spreadsheetUrl: webUrl,
|
||||
}
|
||||
|
||||
const address: string = data.address || data.addressLocal || data.range || ''
|
||||
|
||||
@@ -2,6 +2,7 @@ import type {
|
||||
MicrosoftExcelTableAddResponse,
|
||||
MicrosoftExcelTableToolParams,
|
||||
} from '@/tools/microsoft_excel/types'
|
||||
import { getSpreadsheetWebUrl } from '@/tools/microsoft_excel/utils'
|
||||
import type { ToolConfig } from '@/tools/types'
|
||||
|
||||
export const tableAddTool: ToolConfig<
|
||||
@@ -101,15 +102,22 @@ export const tableAddTool: ToolConfig<
|
||||
},
|
||||
},
|
||||
|
||||
transformResponse: async (response: Response) => {
|
||||
transformResponse: async (response: Response, params?: MicrosoftExcelTableToolParams) => {
|
||||
const data = await response.json()
|
||||
|
||||
const urlParts = response.url.split('/drive/items/')
|
||||
const spreadsheetId = urlParts[1]?.split('/')[0] || ''
|
||||
|
||||
// Fetch the browser-accessible web URL
|
||||
const accessToken = params?.accessToken
|
||||
if (!accessToken) {
|
||||
throw new Error('Access token is required')
|
||||
}
|
||||
const webUrl = await getSpreadsheetWebUrl(spreadsheetId, accessToken)
|
||||
|
||||
const metadata = {
|
||||
spreadsheetId,
|
||||
spreadsheetUrl: `https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetId}`,
|
||||
spreadsheetUrl: webUrl,
|
||||
}
|
||||
|
||||
const result = {
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { createLogger } from '@/lib/logs/console/logger'
|
||||
import type { ExcelCellValue } from '@/tools/microsoft_excel/types'
|
||||
|
||||
const logger = createLogger('MicrosoftExcelUtils')
|
||||
|
||||
export function trimTrailingEmptyRowsAndColumns(matrix: ExcelCellValue[][]): ExcelCellValue[][] {
|
||||
if (!Array.isArray(matrix) || matrix.length === 0) return []
|
||||
|
||||
@@ -32,3 +35,41 @@ export function trimTrailingEmptyRowsAndColumns(matrix: ExcelCellValue[][]): Exc
|
||||
|
||||
return trimmedRows.map((row) => (row || []).slice(0, lastNonEmptyColIndex + 1))
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the browser-accessible web URL for an Excel spreadsheet.
|
||||
* This URL can be opened in a browser if the user is logged into OneDrive/Microsoft,
|
||||
* unlike the Graph API URL which requires an access token.
|
||||
*/
|
||||
export async function getSpreadsheetWebUrl(
|
||||
spreadsheetId: string,
|
||||
accessToken: string
|
||||
): Promise<string> {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetId}?$select=id,webUrl`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!response.ok) {
|
||||
logger.warn('Failed to fetch spreadsheet webUrl, using Graph API URL as fallback', {
|
||||
spreadsheetId,
|
||||
status: response.status,
|
||||
})
|
||||
return `https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetId}`
|
||||
}
|
||||
|
||||
const data = await response.json()
|
||||
return data.webUrl || `https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetId}`
|
||||
} catch (error) {
|
||||
logger.warn('Error fetching spreadsheet webUrl, using Graph API URL as fallback', {
|
||||
spreadsheetId,
|
||||
error,
|
||||
})
|
||||
return `https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetId}`
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import type {
|
||||
MicrosoftExcelToolParams,
|
||||
MicrosoftExcelWriteResponse,
|
||||
} from '@/tools/microsoft_excel/types'
|
||||
import { getSpreadsheetWebUrl } from '@/tools/microsoft_excel/utils'
|
||||
import type { ToolConfig } from '@/tools/types'
|
||||
|
||||
export const writeTool: ToolConfig<MicrosoftExcelToolParams, MicrosoftExcelWriteResponse> = {
|
||||
@@ -131,16 +132,23 @@ export const writeTool: ToolConfig<MicrosoftExcelToolParams, MicrosoftExcelWrite
|
||||
},
|
||||
},
|
||||
|
||||
transformResponse: async (response: Response) => {
|
||||
transformResponse: async (response: Response, params?: MicrosoftExcelToolParams) => {
|
||||
const data = await response.json()
|
||||
|
||||
const urlParts = response.url.split('/drive/items/')
|
||||
const spreadsheetId = urlParts[1]?.split('/')[0] || ''
|
||||
|
||||
// Fetch the browser-accessible web URL
|
||||
const accessToken = params?.accessToken
|
||||
if (!accessToken) {
|
||||
throw new Error('Access token is required')
|
||||
}
|
||||
const webUrl = await getSpreadsheetWebUrl(spreadsheetId, accessToken)
|
||||
|
||||
const metadata = {
|
||||
spreadsheetId,
|
||||
properties: {},
|
||||
spreadsheetUrl: `https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetId}`,
|
||||
spreadsheetUrl: webUrl,
|
||||
}
|
||||
|
||||
const result = {
|
||||
|
||||
Reference in New Issue
Block a user