Skip to content

Commit ef10ce7

Browse files
refactor: enhance type definitions and error handling in Snowflake provider
- Updated the return type of `executeCortexCli` to include `exitCode` as nullable and `signal`. - Improved the `grepTool` to accurately reflect truncation status based on match limits. - Refactored the `normalizeModelId` function to return an empty string for invalid inputs. - Enhanced the handling of deeply nested Zod objects in the conversion process. - Removed deprecated integration test file and updated related documentation.
1 parent f65b26d commit ef10ce7

File tree

11 files changed

+164
-1882
lines changed

11 files changed

+164
-1882
lines changed

packages/ai-sdk-provider-snowflake/src/cli/language-model.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class CliLanguageModel implements LanguageModelV2 {
131131
private async executeCortexCli(
132132
args: string[],
133133
options: { timeout?: number } = {}
134-
): Promise<{ stdout: string; stderr: string; exitCode: number }> {
134+
): Promise<{ stdout: string; stderr: string; exitCode: number | null; signal: NodeJS.Signals | null }> {
135135
const timeout = options.timeout ?? this.settings.timeout ?? 60000;
136136

137137
return new Promise((resolve, reject) => {
@@ -176,7 +176,7 @@ export class CliLanguageModel implements LanguageModelV2 {
176176
});
177177

178178
// Handle process completion
179-
child.on('close', (code) => {
179+
child.on('close', (code, signal) => {
180180
if (timeoutId) {
181181
clearTimeout(timeoutId);
182182
}
@@ -189,7 +189,8 @@ export class CliLanguageModel implements LanguageModelV2 {
189189
resolve({
190190
stdout,
191191
stderr,
192-
exitCode: code ?? 0
192+
exitCode: code,
193+
signal
193194
});
194195
});
195196

packages/ai-sdk-provider-snowflake/src/tools/file-operations.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,22 @@ export const grepTool: ToolDefinition<GrepInput, GrepResult> = {
326326

327327
const files = await findFiles(targetDir, filePattern);
328328
const allMatches: GrepMatch[] = [];
329+
let hitLimit = false;
329330

330331
for (const filePath of files) {
331-
if (allMatches.length >= maxMatches) break;
332+
if (allMatches.length >= maxMatches) {
333+
hitLimit = true;
334+
break;
335+
}
332336

333337
const matches = await grepFile(filePath, regex, contextLines);
334338
for (const match of matches) {
335339
match.file = path.relative(targetDir, filePath);
336340
allMatches.push(match);
337-
if (allMatches.length >= maxMatches) break;
341+
if (allMatches.length >= maxMatches) {
342+
hitLimit = true;
343+
break;
344+
}
338345
}
339346
}
340347

@@ -343,7 +350,7 @@ export const grepTool: ToolDefinition<GrepInput, GrepResult> = {
343350
directory: path.relative(projectRoot, targetDir) || '.',
344351
matches: allMatches.slice(0, maxMatches),
345352
totalMatches: allMatches.length,
346-
truncated: allMatches.length > maxMatches
353+
truncated: hitLimit
347354
};
348355
}
349356
};

packages/ai-sdk-provider-snowflake/src/utils/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export const STREAMING_PREFIXED_MODEL_IDS = STREAMING_MODEL_IDS.map(
296296
*/
297297
export function normalizeModelId(modelId: SnowflakeModelId): string {
298298
if (!modelId || typeof modelId !== 'string') {
299-
return modelId;
299+
return '';
300300
}
301301
return modelId.replace(/^cortex\//, '').toLowerCase();
302302
}

packages/ai-sdk-provider-snowflake/src/utils/tool-helpers.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,39 @@ function convertZodType(def: Record<string, unknown>): Record<string, unknown> {
118118
};
119119
}
120120

121-
case 'ZodObject':
122-
return zodToJsonSchema(def as Record<string, unknown>);
121+
case 'ZodObject': {
122+
// def is already _def, so handle shape directly
123+
if (def.shape) {
124+
const shape = def.shape as
125+
| (() => Record<string, unknown>)
126+
| Record<string, unknown>;
127+
const properties: Record<string, unknown> = {};
128+
const required: string[] = [];
129+
const shapeObj = typeof shape === 'function' ? shape() : shape;
130+
131+
for (const [key, value] of Object.entries(shapeObj)) {
132+
const propDef =
133+
(value as { _def?: Record<string, unknown> })?._def || {};
134+
properties[key] = convertZodType(propDef);
135+
136+
// Check if required (not optional)
137+
if (
138+
propDef.typeName !== 'ZodOptional' &&
139+
propDef.typeName !== 'ZodDefault'
140+
) {
141+
required.push(key);
142+
}
143+
}
144+
145+
return {
146+
type: 'object',
147+
properties,
148+
description: def.description as string | undefined,
149+
required: required.length > 0 ? required : undefined
150+
};
151+
}
152+
return { type: 'object', properties: {} };
153+
}
123154

124155
default:
125156
return { type: 'string' };

0 commit comments

Comments
 (0)