Skip to content

Commit 1a45b8e

Browse files
committed
address comment
1 parent 6bf67c4 commit 1a45b8e

2 files changed

Lines changed: 24 additions & 17 deletions

File tree

apps/sim/lib/copilot/request/tools/files.test.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,18 @@ describe('serializeOutputForFile (json / txt / md)', () => {
7272
})
7373

7474
describe('extractTabularData', () => {
75-
it('extracts rows from function_execute { result: [...] } via unwrap', () => {
76-
const rows = extractTabularData({
77-
result: [{ a: 1 }, { a: 2 }],
78-
stdout: '',
79-
})
80-
expect(rows).toEqual([{ a: 1 }, { a: 2 }])
75+
it('extracts rows directly from an array input', () => {
76+
expect(extractTabularData([{ a: 1 }, { a: 2 }])).toEqual([{ a: 1 }, { a: 2 }])
8177
})
8278

83-
it('returns null when function_execute result is a non-tabular string', () => {
84-
expect(extractTabularData({ result: 'name,age\nAlice,30', stdout: '' })).toBeNull()
79+
it('does NOT unwrap function_execute envelopes on its own (callers must pre-unwrap)', () => {
80+
// Caller is responsible for unwrapping { result, stdout } envelopes first.
81+
// Keeping that concern out of this function prevents a double unwrap when
82+
// the user's payload itself happens to have matching keys.
83+
expect(extractTabularData({ result: [{ a: 1 }], stdout: '' })).toBeNull()
8584
})
8685

87-
it('still extracts rows from the user_table query_rows shape', () => {
86+
it('extracts rows from the user_table query_rows shape', () => {
8887
const rows = extractTabularData({
8988
data: {
9089
rows: [
@@ -96,4 +95,10 @@ describe('extractTabularData', () => {
9695
})
9796
expect(rows).toEqual([{ name: 'Alice' }, { name: 'Bob' }])
9897
})
98+
99+
it('returns null for non-tabular inputs', () => {
100+
expect(extractTabularData('plain string')).toBeNull()
101+
expect(extractTabularData(null)).toBeNull()
102+
expect(extractTabularData({ foo: 'bar' })).toBeNull()
103+
})
99104
})

apps/sim/lib/copilot/request/tools/files.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,23 @@ export function unwrapFunctionExecuteOutput(output: unknown): unknown {
4444
}
4545

4646
/**
47-
* Try to pull a flat array of row-objects out of the various shapes that
48-
* `function_execute` and `user_table` can return.
47+
* Try to pull a flat array of row-objects out of an already-unwrapped tool
48+
* payload. Callers are responsible for stripping any `function_execute`
49+
* envelope first (via {@link unwrapFunctionExecuteOutput}) — this function
50+
* does not re-unwrap, so a user payload that coincidentally has `result` and
51+
* `stdout` keys is not mistaken for another envelope.
4952
*/
5053
export function extractTabularData(output: unknown): Record<string, unknown>[] | null {
51-
const unwrapped = unwrapFunctionExecuteOutput(output)
52-
if (!unwrapped || typeof unwrapped !== 'object') return null
54+
if (!output || typeof output !== 'object') return null
5355

54-
if (Array.isArray(unwrapped)) {
55-
if (unwrapped.length > 0 && typeof unwrapped[0] === 'object' && unwrapped[0] !== null) {
56-
return unwrapped as Record<string, unknown>[]
56+
if (Array.isArray(output)) {
57+
if (output.length > 0 && typeof output[0] === 'object' && output[0] !== null) {
58+
return output as Record<string, unknown>[]
5759
}
5860
return null
5961
}
6062

61-
const obj = unwrapped as Record<string, unknown>
63+
const obj = output as Record<string, unknown>
6264

6365
// user_table query_rows shape: { data: { rows: [{ data: {...} }], totalCount } }
6466
if (obj.data && typeof obj.data === 'object' && !Array.isArray(obj.data)) {

0 commit comments

Comments
 (0)