-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
796c9c0
commit ad6b2be
Showing
9 changed files
with
150 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,73 +125,29 @@ export const updateDocument = ({ | |
|
||
dataStream.writeData({ type: 'finish', content: '' }); | ||
} else if (document.kind === 'sheet') { | ||
// Parse the current content as spreadsheet data | ||
let currentSpreadsheetData = { headers: [], rows: [] }; | ||
try { | ||
if (currentContent) { | ||
currentSpreadsheetData = JSON.parse(currentContent); | ||
} | ||
} catch { | ||
// Keep default empty structure | ||
} | ||
|
||
const { fullStream } = streamObject({ | ||
model: customModel(model.apiIdentifier), | ||
system: `You are a spreadsheet manipulation assistant. The current spreadsheet has the following structure: | ||
Headers: ${JSON.stringify(currentSpreadsheetData.headers)} | ||
Current rows: ${JSON.stringify(currentSpreadsheetData.rows)} | ||
When modifying the spreadsheet: | ||
1. You can add, remove, or modify columns (headers) | ||
2. When adding columns, add empty values to existing rows for the new columns | ||
3. When removing columns, remove the corresponding values from all rows | ||
4. Return the COMPLETE spreadsheet data including ALL headers and rows | ||
5. Format response as valid JSON with 'headers' and 'rows' arrays | ||
Example response format: | ||
{"headers":["Name","Email","Phone"],"rows":[["John","[email protected]","123-456-7890"],["Jane","[email protected]","098-765-4321"]]}`, | ||
system: updateDocumentPrompt(currentContent, 'sheet'), | ||
prompt: description, | ||
schema: z.object({ | ||
headers: z | ||
.array(z.string()) | ||
.describe('Column headers for the spreadsheet'), | ||
rows: z.array(z.array(z.string())).describe('Sample data rows'), | ||
csv: z.string(), | ||
}), | ||
}); | ||
|
||
draftText = JSON.stringify(currentSpreadsheetData); | ||
|
||
for await (const delta of fullStream) { | ||
const { type } = delta; | ||
|
||
if (type === 'object') { | ||
const { object } = delta; | ||
if ( | ||
object && | ||
Array.isArray(object.headers) && | ||
Array.isArray(object.rows) | ||
) { | ||
// Validate and normalize the data | ||
const headers = object.headers.map((h: any) => String(h || '')); | ||
const rows = object.rows.map( | ||
(row: (string | undefined)[] | undefined) => { | ||
const normalizedRow = (row || []).map((cell: any) => | ||
String(cell || ''), | ||
); | ||
// Ensure row length matches new headers length | ||
while (normalizedRow.length < headers.length) { | ||
normalizedRow.push(''); | ||
} | ||
return normalizedRow.slice(0, headers.length); | ||
}, | ||
); | ||
|
||
const newData = { headers, rows }; | ||
draftText = JSON.stringify(newData); | ||
const { csv } = object; | ||
|
||
if (csv) { | ||
dataStream.writeData({ | ||
type: 'sheet-delta', | ||
content: draftText, | ||
content: csv, | ||
}); | ||
|
||
draftText = csv; | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.