Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | 2x 6x 33x 33x 2x 12x 16x 14x 19x 19x 19x 19x 19x 19x 19x 19x 10x 10x 19x 19x 19x 16x 16x 16x 16x 16x 6x 6x 6x 9x 9x 9x 9x 9x 9x 9x 2x 7x 6x 7x 2x 6x 1x 5x 11x 1x 10x 19x | import { z } from 'zod';
import { DEFAULT_BASE_URL } from './consts';
import { BaseTool } from './tool';
import type { ExecuteConfig, ExecuteOptions, JsonDict, ToolParameters } from './types';
import { StackOneError } from './utils/errors';
interface FeedbackToolOptions {
baseUrl?: string;
apiKey?: string;
accountId?: string;
}
const createNonEmptyTrimmedStringSchema = (fieldName: string) =>
z
.string()
.transform((value) => value.trim())
.refine((value) => value.length > 0, {
message: `${fieldName} must be a non-empty string.`,
});
const feedbackInputSchema = z.object({
feedback: createNonEmptyTrimmedStringSchema('Feedback'),
account_id: z
.union([
createNonEmptyTrimmedStringSchema('Account ID'),
z
.array(createNonEmptyTrimmedStringSchema('Account ID'))
.min(1, 'At least one account ID is required'),
])
.transform((value) => (Array.isArray(value) ? value : [value])),
tool_names: z
.array(z.string())
.min(1, 'At least one tool name is required')
.transform((value) => value.map((item) => item.trim()).filter((item) => item.length > 0))
.refine((value) => value.length > 0, {
message: 'Tool names must contain at least one non-empty string',
}),
});
export function createFeedbackTool(
apiKey?: string,
accountId?: string,
baseUrl = DEFAULT_BASE_URL,
): BaseTool {
const options: FeedbackToolOptions = {
apiKey,
accountId,
baseUrl,
};
const name = 'meta_collect_tool_feedback' as const;
const description =
'Collects user feedback on StackOne tool performance. First ask the user, "Are you ok with sending feedback to StackOne?" and mention that the LLM will take care of sending it. Call this tool only when the user explicitly answers yes.';
const parameters = {
type: 'object',
properties: {
account_id: {
oneOf: [
{
type: 'string',
description: 'Single account identifier (e.g., "acc_123456")',
},
{
type: 'array',
items: {
type: 'string',
},
description: 'Array of account identifiers (e.g., ["acc_123456", "acc_789012"])',
},
],
description: 'Account identifier(s) - can be a single string or array of strings',
},
feedback: {
type: 'string',
description: 'Verbatim feedback from the user about their experience with StackOne tools.',
},
tool_names: {
type: 'array',
items: {
type: 'string',
},
description: 'Array of tool names being reviewed',
},
},
required: ['feedback', 'account_id', 'tool_names'],
} as const satisfies ToolParameters;
const executeConfig = {
kind: 'http',
method: 'POST',
url: '/ai/tool-feedback',
bodyType: 'json',
params: [],
} as const satisfies ExecuteConfig;
// Get API key from environment or options
const resolvedApiKey = options.apiKey || process.env.STACKONE_API_KEY;
// Create authentication headers
const authHeaders: Record<string, string> = {};
if (resolvedApiKey) {
const authString = Buffer.from(`${resolvedApiKey}:`).toString('base64');
authHeaders.Authorization = `Basic ${authString}`;
}
const tool = new BaseTool(name, description, parameters, executeConfig, authHeaders);
const resolvedBaseUrl = options.baseUrl ?? DEFAULT_BASE_URL;
tool.execute = async function (
this: BaseTool,
inputParams?: JsonDict | string,
executeOptions?: ExecuteOptions,
): Promise<JsonDict> {
try {
const rawParams =
typeof inputParams === 'string' ? JSON.parse(inputParams) : inputParams || {};
const parsedParams = feedbackInputSchema.parse(rawParams);
const headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
...this.getHeaders(),
};
// Handle dry run - show what would be sent to each account
Iif (executeOptions?.dryRun) {
const dryRunResults = parsedParams.account_id.map((accountId: string) => ({
url: `${resolvedBaseUrl}${executeConfig.url}`,
method: executeConfig.method,
headers,
body: {
feedback: parsedParams.feedback,
account_id: accountId,
tool_names: parsedParams.tool_names,
},
}));
return {
multiple_requests: dryRunResults,
total_accounts: parsedParams.account_id.length,
} satisfies JsonDict;
}
// Send feedback to each account individually
const results = [];
const errors = [];
for (const accountId of parsedParams.account_id) {
try {
const requestBody = {
feedback: parsedParams.feedback,
account_id: accountId,
tool_names: parsedParams.tool_names,
};
const response = await fetch(`${resolvedBaseUrl}${executeConfig.url}`, {
method: executeConfig.method satisfies 'POST',
headers,
body: JSON.stringify(requestBody),
});
const text = await response.text();
let parsed: unknown;
try {
parsed = text ? JSON.parse(text) : {};
} catch {
parsed = { raw: text };
}
if (!response.ok) {
errors.push({
account_id: accountId,
status: response.status,
error:
typeof parsed === 'object' && parsed !== null
? JSON.stringify(parsed)
: String(parsed),
});
} else {
results.push({
account_id: accountId,
status: response.status,
response: parsed,
});
}
} catch (error) {
errors.push({
account_id: accountId,
error: error instanceof Error ? error.message : String(error),
});
}
}
// Return summary of all submissions in Python SDK format
const response: JsonDict = {
message: `Feedback sent to ${parsedParams.account_id.length} account(s)`,
total_accounts: parsedParams.account_id.length,
successful: results.length,
failed: errors.length,
results: [
...results.map((r) => ({
account_id: r.account_id,
status: 'success',
result: r.response,
})),
...errors.map((e) => ({
account_id: e.account_id,
status: 'error',
error: e.error,
})),
],
};
// If all submissions failed, throw an error
if (errors.length > 0 && results.length === 0) {
throw new StackOneError(
`Failed to submit feedback to any account. Errors: ${JSON.stringify(errors)}`,
);
}
return response;
} catch (error) {
if (error instanceof StackOneError) {
throw error;
}
throw new StackOneError('Error executing tool', { cause: error });
}
};
return tool;
}
|