Creates an instance of Shuriken.
The name of the shuriken, used as the function name in OpenAI tool calls. Must contain only alphanumeric characters, underscores, and hyphens.
A brief description of what the shuriken does. This helps the AI decide when to use it.
A Zod schema defining the expected parameters for the shuriken's implementation.
The actual JavaScript/TypeScript function that performs the shuriken's logic.
const calculator = new Shuriken(
'calculate',
'Perform basic mathematical operations',
z.object({
operation: z.enum(['add', 'subtract', 'multiply', 'divide']),
a: z.number(),
b: z.number()
}),
(params) => {
switch (params.operation) {
case 'add': return params.a + params.b;
case 'subtract': return params.a - params.b;
case 'multiply': return params.a * params.b;
case 'divide': return params.a / params.b;
}
}
);
1.0.0
Generates the OpenAI tool definition for this shuriken. This definition is used by OpenAI models to understand and call the shuriken.
An OpenAI ChatCompletionTool object that can be used in API calls.
const weatherShuriken = new Shuriken(
'get_weather',
'Get current weather information',
z.object({ city: z.string() }),
async (params) => ({ temperature: 25, condition: 'Sunny' })
);
const toolDefinition = weatherShuriken.forge();
// toolDefinition will be:
// {
// type: 'function',
// function: {
// name: 'get_weather',
// description: 'Get current weather information',
// parameters: { type: 'object', properties: { city: { type: 'string' } }, required: ['city'] }
// }
// }
1.0.0
Validates the given parameters against the shuriken's defined Zod schema.
The parameters to validate.
An object indicating success or failure, with data or error details.
Optional
data?: anyOptional
error?: stringconst validation = shuriken.validate({ city: 'Paris' });
if (validation.success) {
console.log('Valid parameters:', validation.data);
} else {
console.error('Validation error:', validation.error);
}
1.0.0
Executes the shuriken's underlying implementation function with the provided parameters. Parameters are validated against the schema before execution.
The parameters to pass to the shuriken's implementation.
A promise that resolves to an ExecutionResult containing the result and execution time.
If parameter validation fails or if the shuriken's implementation throws an error.
const calculator = new Shuriken(
'add',
'Adds two numbers',
z.object({ a: z.number(), b: z.number() }),
(params) => params.a + params.b
);
try {
const result = await calculator.execute({ a: 5, b: 3 });
console.log('Result:', result.result); // 8
console.log('Execution time:', result.executionTime); // e.g., 2
} catch (error) {
console.error('Execution failed:', error.message);
}
1.0.0
Example: Basic Shuriken Creation
Example: Using a Shuriken
Since
1.0.0