Skip to content

Commit 64ede7f

Browse files
aadamgoughAdam Gough
andauthored
added legit urls to the metadata (#1665)
Co-authored-by: Adam Gough <[email protected]>
1 parent 0fbbbe0 commit 64ede7f

File tree

4 files changed

+77
-7
lines changed

4 files changed

+77
-7
lines changed

apps/sim/tools/microsoft_excel/read.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import type {
33
MicrosoftExcelReadResponse,
44
MicrosoftExcelToolParams,
55
} from '@/tools/microsoft_excel/types'
6-
import { trimTrailingEmptyRowsAndColumns } from '@/tools/microsoft_excel/utils'
6+
import {
7+
getSpreadsheetWebUrl,
8+
trimTrailingEmptyRowsAndColumns,
9+
} from '@/tools/microsoft_excel/utils'
710
import type { ToolConfig } from '@/tools/types'
811

912
export const readTool: ToolConfig<MicrosoftExcelToolParams, MicrosoftExcelReadResponse> = {
@@ -126,10 +129,13 @@ export const readTool: ToolConfig<MicrosoftExcelToolParams, MicrosoftExcelReadRe
126129

127130
const values = trimTrailingEmptyRowsAndColumns(rawValues)
128131

132+
// Fetch the browser-accessible web URL
133+
const webUrl = await getSpreadsheetWebUrl(spreadsheetIdFromUrl, accessToken)
134+
129135
const metadata = {
130136
spreadsheetId: spreadsheetIdFromUrl,
131137
properties: {},
132-
spreadsheetUrl: `https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetIdFromUrl}`,
138+
spreadsheetUrl: webUrl,
133139
}
134140

135141
const result: MicrosoftExcelReadResponse = {
@@ -155,10 +161,17 @@ export const readTool: ToolConfig<MicrosoftExcelToolParams, MicrosoftExcelReadRe
155161
const urlParts = response.url.split('/drive/items/')
156162
const spreadsheetId = urlParts[1]?.split('/')[0] || ''
157163

164+
// Fetch the browser-accessible web URL
165+
const accessToken = params?.accessToken
166+
if (!accessToken) {
167+
throw new Error('Access token is required')
168+
}
169+
const webUrl = await getSpreadsheetWebUrl(spreadsheetId, accessToken)
170+
158171
const metadata = {
159172
spreadsheetId,
160173
properties: {},
161-
spreadsheetUrl: `https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetId}`,
174+
spreadsheetUrl: webUrl,
162175
}
163176

164177
const address: string = data.address || data.addressLocal || data.range || ''

apps/sim/tools/microsoft_excel/table_add.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
MicrosoftExcelTableAddResponse,
33
MicrosoftExcelTableToolParams,
44
} from '@/tools/microsoft_excel/types'
5+
import { getSpreadsheetWebUrl } from '@/tools/microsoft_excel/utils'
56
import type { ToolConfig } from '@/tools/types'
67

78
export const tableAddTool: ToolConfig<
@@ -101,15 +102,22 @@ export const tableAddTool: ToolConfig<
101102
},
102103
},
103104

104-
transformResponse: async (response: Response) => {
105+
transformResponse: async (response: Response, params?: MicrosoftExcelTableToolParams) => {
105106
const data = await response.json()
106107

107108
const urlParts = response.url.split('/drive/items/')
108109
const spreadsheetId = urlParts[1]?.split('/')[0] || ''
109110

111+
// Fetch the browser-accessible web URL
112+
const accessToken = params?.accessToken
113+
if (!accessToken) {
114+
throw new Error('Access token is required')
115+
}
116+
const webUrl = await getSpreadsheetWebUrl(spreadsheetId, accessToken)
117+
110118
const metadata = {
111119
spreadsheetId,
112-
spreadsheetUrl: `https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetId}`,
120+
spreadsheetUrl: webUrl,
113121
}
114122

115123
const result = {

apps/sim/tools/microsoft_excel/utils.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { createLogger } from '@/lib/logs/console/logger'
12
import type { ExcelCellValue } from '@/tools/microsoft_excel/types'
23

4+
const logger = createLogger('MicrosoftExcelUtils')
5+
36
export function trimTrailingEmptyRowsAndColumns(matrix: ExcelCellValue[][]): ExcelCellValue[][] {
47
if (!Array.isArray(matrix) || matrix.length === 0) return []
58

@@ -32,3 +35,41 @@ export function trimTrailingEmptyRowsAndColumns(matrix: ExcelCellValue[][]): Exc
3235

3336
return trimmedRows.map((row) => (row || []).slice(0, lastNonEmptyColIndex + 1))
3437
}
38+
39+
/**
40+
* Fetches the browser-accessible web URL for an Excel spreadsheet.
41+
* This URL can be opened in a browser if the user is logged into OneDrive/Microsoft,
42+
* unlike the Graph API URL which requires an access token.
43+
*/
44+
export async function getSpreadsheetWebUrl(
45+
spreadsheetId: string,
46+
accessToken: string
47+
): Promise<string> {
48+
try {
49+
const response = await fetch(
50+
`https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetId}?$select=id,webUrl`,
51+
{
52+
headers: {
53+
Authorization: `Bearer ${accessToken}`,
54+
},
55+
}
56+
)
57+
58+
if (!response.ok) {
59+
logger.warn('Failed to fetch spreadsheet webUrl, using Graph API URL as fallback', {
60+
spreadsheetId,
61+
status: response.status,
62+
})
63+
return `https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetId}`
64+
}
65+
66+
const data = await response.json()
67+
return data.webUrl || `https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetId}`
68+
} catch (error) {
69+
logger.warn('Error fetching spreadsheet webUrl, using Graph API URL as fallback', {
70+
spreadsheetId,
71+
error,
72+
})
73+
return `https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetId}`
74+
}
75+
}

apps/sim/tools/microsoft_excel/write.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
MicrosoftExcelToolParams,
33
MicrosoftExcelWriteResponse,
44
} from '@/tools/microsoft_excel/types'
5+
import { getSpreadsheetWebUrl } from '@/tools/microsoft_excel/utils'
56
import type { ToolConfig } from '@/tools/types'
67

78
export const writeTool: ToolConfig<MicrosoftExcelToolParams, MicrosoftExcelWriteResponse> = {
@@ -131,16 +132,23 @@ export const writeTool: ToolConfig<MicrosoftExcelToolParams, MicrosoftExcelWrite
131132
},
132133
},
133134

134-
transformResponse: async (response: Response) => {
135+
transformResponse: async (response: Response, params?: MicrosoftExcelToolParams) => {
135136
const data = await response.json()
136137

137138
const urlParts = response.url.split('/drive/items/')
138139
const spreadsheetId = urlParts[1]?.split('/')[0] || ''
139140

141+
// Fetch the browser-accessible web URL
142+
const accessToken = params?.accessToken
143+
if (!accessToken) {
144+
throw new Error('Access token is required')
145+
}
146+
const webUrl = await getSpreadsheetWebUrl(spreadsheetId, accessToken)
147+
140148
const metadata = {
141149
spreadsheetId,
142150
properties: {},
143-
spreadsheetUrl: `https://graph.microsoft.com/v1.0/me/drive/items/${spreadsheetId}`,
151+
spreadsheetUrl: webUrl,
144152
}
145153

146154
const result = {

0 commit comments

Comments
 (0)