@allternit/gizzi-code 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/WORKSPACE_INTEGRATION.md +290 -0
  2. package/chat-sessions/sessions-index.json +98 -0
  3. package/package.json +43 -0
  4. package/package.json.original +31 -0
  5. package/package.json.publish +47 -0
  6. package/src/bootstrap/state.d.ts +169 -0
  7. package/src/bootstrap/state.js +261 -0
  8. package/src/bootstrap/state.ts +371 -0
  9. package/src/context/pack-builder.d.ts +24 -0
  10. package/src/context/pack-builder.js +281 -0
  11. package/src/context/pack-builder.ts +334 -0
  12. package/src/hooks/useExitOnCtrlCD.d.ts +86 -0
  13. package/src/hooks/useExitOnCtrlCD.js +130 -0
  14. package/src/hooks/useExitOnCtrlCD.ts +202 -0
  15. package/src/hooks/useKeybinding.d.ts +89 -0
  16. package/src/hooks/useKeybinding.js +163 -0
  17. package/src/hooks/useKeybinding.ts +226 -0
  18. package/src/hooks/useTerminalSize.d.ts +41 -0
  19. package/src/hooks/useTerminalSize.js +90 -0
  20. package/src/hooks/useTerminalSize.ts +117 -0
  21. package/src/index.d.ts +23 -0
  22. package/src/index.js +24 -0
  23. package/src/index.ts +132 -0
  24. package/src/runtime/brand/brand.d.ts +488 -0
  25. package/src/runtime/brand/brand.js +464 -0
  26. package/src/runtime/brand/brand.ts +667 -0
  27. package/src/runtime/services/tokenEstimation.d.ts +187 -0
  28. package/src/runtime/services/tokenEstimation.js +423 -0
  29. package/src/runtime/services/tokenEstimation.ts +547 -0
  30. package/src/runtime/tools/Tool.d.ts +239 -0
  31. package/src/runtime/tools/Tool.js +285 -0
  32. package/src/runtime/tools/Tool.ts +524 -0
  33. package/src/scheduler/TaskScheduler.d.ts +98 -0
  34. package/src/scheduler/TaskScheduler.js +201 -0
  35. package/src/scheduler/TaskScheduler.ts +259 -0
  36. package/src/state/AppStateStore.d.ts +347 -0
  37. package/src/state/AppStateStore.js +439 -0
  38. package/src/state/AppStateStore.ts +658 -0
  39. package/src/state/WorkspaceStateStore.d.ts +43 -0
  40. package/src/state/WorkspaceStateStore.js +224 -0
  41. package/src/state/WorkspaceStateStore.ts +297 -0
  42. package/src/types/message.d.ts +190 -0
  43. package/src/types/message.js +77 -0
  44. package/src/types/message.ts +277 -0
  45. package/src/utils/format.d.ts +126 -0
  46. package/src/utils/format.js +230 -0
  47. package/src/utils/format.ts +336 -0
  48. package/src/utils/fullscreen.d.ts +161 -0
  49. package/src/utils/fullscreen.js +277 -0
  50. package/src/utils/fullscreen.ts +382 -0
  51. package/src/utils/sessionStorage.d.ts +210 -0
  52. package/src/utils/sessionStorage.js +435 -0
  53. package/src/utils/sessionStorage.ts +559 -0
  54. package/src/watcher/WorkspaceWatcher.d.ts +70 -0
  55. package/src/watcher/WorkspaceWatcher.js +191 -0
  56. package/src/watcher/WorkspaceWatcher.ts +251 -0
  57. package/src/workspace/loader.d.ts +30 -0
  58. package/src/workspace/loader.js +117 -0
  59. package/src/workspace/loader.ts +171 -0
  60. package/src/workspace/types.d.ts +91 -0
  61. package/src/workspace/types.js +7 -0
  62. package/src/workspace/types.ts +122 -0
  63. package/threads/thread_1775361175022.json +9 -0
  64. package/tsconfig.json +14 -0
@@ -0,0 +1,239 @@
1
+ /**
2
+ * @fileoverview Runtime tool base class and interfaces
3
+ * @module runtime/tools/Tool
4
+ *
5
+ * Defines the abstract base class for all runtime tools, providing
6
+ * a standardized interface for tool execution, validation, and schema generation.
7
+ * All tools in the gizzi-code runtime must extend this base class.
8
+ */
9
+ import type { Message } from '../../types/message.js';
10
+ /**
11
+ * Context passed to tools during execution
12
+ * @interface ToolContext
13
+ */
14
+ export interface ToolContext {
15
+ /** Unique identifier for the current execution session */
16
+ readonly sessionId: string;
17
+ /** Current user identifier */
18
+ readonly userId?: string;
19
+ /** Messages in the current conversation context */
20
+ readonly messages: readonly Message[];
21
+ /** Abort signal for cancellation support */
22
+ readonly signal?: AbortSignal;
23
+ /** Additional metadata for the execution context */
24
+ readonly metadata?: Readonly<Record<string, unknown>>;
25
+ /** Callback for reporting progress during long-running operations */
26
+ readonly onProgress?: (progress: ToolProgress) => void;
27
+ }
28
+ /**
29
+ * Progress information for long-running tool operations
30
+ * @interface ToolProgress
31
+ */
32
+ export interface ToolProgress {
33
+ /** Current step number */
34
+ readonly current: number;
35
+ /** Total number of steps */
36
+ readonly total: number;
37
+ /** Human-readable status message */
38
+ readonly message: string;
39
+ /** Optional percentage completion (0-100) */
40
+ readonly percent?: number;
41
+ }
42
+ /**
43
+ * JSON Schema property definition for tool parameters
44
+ * @interface ToolParameterSchema
45
+ */
46
+ export interface ToolParameterSchema {
47
+ /** Parameter type */
48
+ readonly type: 'string' | 'number' | 'boolean' | 'array' | 'object' | 'integer';
49
+ /** Human-readable description of the parameter */
50
+ readonly description: string;
51
+ /** Whether the parameter is required */
52
+ readonly required?: boolean;
53
+ /** Default value for the parameter */
54
+ readonly default?: unknown;
55
+ /** Enum values for constrained parameters */
56
+ readonly enum?: readonly unknown[];
57
+ /** Array item schema for array types */
58
+ readonly items?: ToolParameterSchema;
59
+ /** Object properties schema for object types */
60
+ readonly properties?: Readonly<Record<string, ToolParameterSchema>>;
61
+ /** Format hint for string parameters (e.g., 'date', 'email', 'uri') */
62
+ readonly format?: string;
63
+ }
64
+ /**
65
+ * Complete tool schema definition for JSON Schema output
66
+ * @interface ToolSchema
67
+ */
68
+ export interface ToolSchema {
69
+ /** Tool name */
70
+ readonly name: string;
71
+ /** Tool description */
72
+ readonly description: string;
73
+ /** JSON Schema version */
74
+ readonly schema: 'http://json-schema.org/draft-07/schema#';
75
+ /** Parameter definitions */
76
+ readonly parameters: {
77
+ readonly type: 'object';
78
+ readonly properties: Readonly<Record<string, ToolParameterSchema>>;
79
+ readonly required: readonly string[];
80
+ readonly additionalProperties: boolean;
81
+ };
82
+ }
83
+ /**
84
+ * Result of a tool execution
85
+ * @interface ToolResult
86
+ */
87
+ export interface ToolResult<T = unknown> {
88
+ /** Whether the execution was successful */
89
+ readonly success: boolean;
90
+ /** Result data (only present on success) */
91
+ readonly data?: T;
92
+ /** Error information (only present on failure) */
93
+ readonly error?: ToolError;
94
+ /** Execution metadata */
95
+ readonly metadata?: {
96
+ /** Execution duration in milliseconds */
97
+ readonly duration: number;
98
+ /** Timestamp when execution completed */
99
+ readonly timestamp: Date;
100
+ /** Additional metadata */
101
+ readonly [key: string]: unknown;
102
+ };
103
+ }
104
+ /**
105
+ * Tool error information
106
+ * @interface ToolError
107
+ */
108
+ export interface ToolError {
109
+ /** Error code for programmatic handling */
110
+ readonly code: ToolErrorCode;
111
+ /** Human-readable error message */
112
+ readonly message: string;
113
+ /** Additional error details */
114
+ readonly details?: Readonly<Record<string, unknown>>;
115
+ /** Original error if wrapped */
116
+ readonly cause?: Error;
117
+ }
118
+ /**
119
+ * Standard tool error codes
120
+ * @typedef {'validation_error' | 'execution_error' | 'timeout_error' | 'cancelled_error' | 'permission_error' | 'not_found_error' | 'rate_limit_error' | 'internal_error'} ToolErrorCode
121
+ */
122
+ export type ToolErrorCode = 'validation_error' | 'execution_error' | 'timeout_error' | 'cancelled_error' | 'permission_error' | 'not_found_error' | 'rate_limit_error' | 'internal_error';
123
+ /**
124
+ * Tool parameter definitions mapping
125
+ * @type ToolParameters
126
+ */
127
+ export type ToolParameters = Readonly<Record<string, ToolParameterSchema>>;
128
+ /**
129
+ * Abstract base class for all runtime tools
130
+ * @abstract
131
+ * @class Tool
132
+ *
133
+ * @example
134
+ * ```typescript
135
+ * class MyTool extends Tool<{ input: string }, { output: string }> {
136
+ * name = 'my_tool';
137
+ * description = 'A sample tool';
138
+ * parameters = {
139
+ * input: {
140
+ * type: 'string',
141
+ * description: 'Input text',
142
+ * required: true
143
+ * }
144
+ * };
145
+ *
146
+ * async execute(args: { input: string }, context: ToolContext): Promise<ToolResult<{ output: string }>> {
147
+ * return {
148
+ * success: true,
149
+ * data: { output: args.input.toUpperCase() },
150
+ * metadata: { duration: 0, timestamp: new Date() }
151
+ * };
152
+ * }
153
+ * }
154
+ * ```
155
+ */
156
+ export declare abstract class Tool<TArgs = Record<string, unknown>, TResult = unknown> {
157
+ /**
158
+ * Unique tool identifier (kebab-case or snake_case)
159
+ * @abstract
160
+ * @readonly
161
+ */
162
+ abstract readonly name: string;
163
+ /**
164
+ * Human-readable description of what the tool does
165
+ * @abstract
166
+ * @readonly
167
+ */
168
+ abstract readonly description: string;
169
+ /**
170
+ * Parameter schema definitions for validation and documentation
171
+ * @abstract
172
+ * @readonly
173
+ */
174
+ abstract readonly parameters: ToolParameters;
175
+ /**
176
+ * Execute the tool with the given arguments and context
177
+ * @abstract
178
+ * @param {TArgs} args - Validated tool arguments
179
+ * @param {ToolContext} context - Execution context
180
+ * @returns {Promise<ToolResult<TResult>>} Tool execution result
181
+ * @throws {never} Should never throw; errors should be returned as ToolResult with success: false
182
+ */
183
+ abstract execute(args: TArgs, context: ToolContext): Promise<ToolResult<TResult>>;
184
+ /**
185
+ * Validate tool arguments against the parameter schema
186
+ * @param {unknown} args - Arguments to validate
187
+ * @returns {{ valid: true } | { valid: false; errors: readonly string[] }} Validation result
188
+ */
189
+ validate(args: unknown): {
190
+ valid: true;
191
+ } | {
192
+ valid: false;
193
+ errors: readonly string[];
194
+ };
195
+ /**
196
+ * Get the JSON Schema representation of this tool
197
+ * @returns {ToolSchema} Complete JSON Schema for the tool
198
+ */
199
+ getSchema(): ToolSchema;
200
+ /**
201
+ * Validate a single parameter value against its schema
202
+ * @private
203
+ * @param {string} key - Parameter name
204
+ * @param {unknown} value - Parameter value
205
+ * @param {ToolParameterSchema} schema - Parameter schema
206
+ * @returns {string | null} Error message or null if valid
207
+ */
208
+ private validateType;
209
+ /**
210
+ * Execute with automatic validation (helper method)
211
+ * @param {unknown} args - Raw arguments to validate and execute
212
+ * @param {ToolContext} context - Execution context
213
+ * @returns {Promise<ToolResult<TResult>>} Tool execution result
214
+ */
215
+ executeWithValidation(args: unknown, context: ToolContext): Promise<ToolResult<TResult>>;
216
+ }
217
+ /**
218
+ * Type guard to check if a value is a ToolError
219
+ * @param {unknown} value - Value to check
220
+ * @returns {boolean} True if the value is a ToolError
221
+ */
222
+ export declare function isToolError(value: unknown): value is ToolError;
223
+ /**
224
+ * Create a successful tool result
225
+ * @template T
226
+ * @param {T} data - Result data
227
+ * @param {Partial<ToolResult<T>['metadata']>} metadata - Optional metadata
228
+ * @returns {ToolResult<T>} Successful tool result
229
+ */
230
+ export declare function createSuccessResult<T>(data: T, metadata?: Partial<ToolResult<T>['metadata']>): ToolResult<T>;
231
+ /**
232
+ * Create an error tool result
233
+ * @param {ToolErrorCode} code - Error code
234
+ * @param {string} message - Error message
235
+ * @param {Partial<ToolError>} details - Additional error details
236
+ * @returns {ToolResult<never>} Error tool result
237
+ */
238
+ export declare function createErrorResult(code: ToolErrorCode, message: string, details?: Partial<ToolError>): ToolResult<never>;
239
+ export default Tool;
@@ -0,0 +1,285 @@
1
+ /**
2
+ * @fileoverview Runtime tool base class and interfaces
3
+ * @module runtime/tools/Tool
4
+ *
5
+ * Defines the abstract base class for all runtime tools, providing
6
+ * a standardized interface for tool execution, validation, and schema generation.
7
+ * All tools in the gizzi-code runtime must extend this base class.
8
+ */
9
+ /**
10
+ * Abstract base class for all runtime tools
11
+ * @abstract
12
+ * @class Tool
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * class MyTool extends Tool<{ input: string }, { output: string }> {
17
+ * name = 'my_tool';
18
+ * description = 'A sample tool';
19
+ * parameters = {
20
+ * input: {
21
+ * type: 'string',
22
+ * description: 'Input text',
23
+ * required: true
24
+ * }
25
+ * };
26
+ *
27
+ * async execute(args: { input: string }, context: ToolContext): Promise<ToolResult<{ output: string }>> {
28
+ * return {
29
+ * success: true,
30
+ * data: { output: args.input.toUpperCase() },
31
+ * metadata: { duration: 0, timestamp: new Date() }
32
+ * };
33
+ * }
34
+ * }
35
+ * ```
36
+ */
37
+ export class Tool {
38
+ /**
39
+ * Validate tool arguments against the parameter schema
40
+ * @param {unknown} args - Arguments to validate
41
+ * @returns {{ valid: true } | { valid: false; errors: readonly string[] }} Validation result
42
+ */
43
+ validate(args) {
44
+ if (args === null || typeof args !== 'object') {
45
+ return { valid: false, errors: ['Arguments must be an object'] };
46
+ }
47
+ const errors = [];
48
+ const argsRecord = args;
49
+ for (const [key, schema] of Object.entries(this.parameters)) {
50
+ const value = argsRecord[key];
51
+ // Check required fields
52
+ if (schema.required !== false && value === undefined) {
53
+ errors.push(`Missing required parameter: ${key}`);
54
+ continue;
55
+ }
56
+ // Skip validation for undefined optional fields
57
+ if (value === undefined) {
58
+ continue;
59
+ }
60
+ // Type validation
61
+ const typeError = this.validateType(key, value, schema);
62
+ if (typeError) {
63
+ errors.push(typeError);
64
+ }
65
+ // Enum validation
66
+ if (schema.enum && !schema.enum.includes(value)) {
67
+ errors.push(`Parameter ${key} must be one of: ${schema.enum.join(', ')}`);
68
+ }
69
+ }
70
+ // Check for unknown parameters
71
+ const knownKeys = Object.keys(this.parameters);
72
+ const unknownKeys = Object.keys(argsRecord).filter((k) => !knownKeys.includes(k));
73
+ if (unknownKeys.length > 0) {
74
+ errors.push(`Unknown parameters: ${unknownKeys.join(', ')}`);
75
+ }
76
+ return errors.length === 0 ? { valid: true } : { valid: false, errors };
77
+ }
78
+ /**
79
+ * Get the JSON Schema representation of this tool
80
+ * @returns {ToolSchema} Complete JSON Schema for the tool
81
+ */
82
+ getSchema() {
83
+ const required = Object.entries(this.parameters)
84
+ .filter(([, schema]) => schema.required !== false)
85
+ .map(([key]) => key);
86
+ return {
87
+ name: this.name,
88
+ description: this.description,
89
+ schema: 'http://json-schema.org/draft-07/schema#',
90
+ parameters: {
91
+ type: 'object',
92
+ properties: this.parameters,
93
+ required,
94
+ additionalProperties: false,
95
+ },
96
+ };
97
+ }
98
+ /**
99
+ * Validate a single parameter value against its schema
100
+ * @private
101
+ * @param {string} key - Parameter name
102
+ * @param {unknown} value - Parameter value
103
+ * @param {ToolParameterSchema} schema - Parameter schema
104
+ * @returns {string | null} Error message or null if valid
105
+ */
106
+ validateType(key, value, schema) {
107
+ const expectedType = schema.type;
108
+ switch (expectedType) {
109
+ case 'string':
110
+ if (typeof value !== 'string') {
111
+ return `Parameter ${key} must be a string`;
112
+ }
113
+ break;
114
+ case 'number':
115
+ case 'integer':
116
+ if (typeof value !== 'number' || Number.isNaN(value)) {
117
+ return `Parameter ${key} must be a number`;
118
+ }
119
+ if (expectedType === 'integer' && !Number.isInteger(value)) {
120
+ return `Parameter ${key} must be an integer`;
121
+ }
122
+ break;
123
+ case 'boolean':
124
+ if (typeof value !== 'boolean') {
125
+ return `Parameter ${key} must be a boolean`;
126
+ }
127
+ break;
128
+ case 'array':
129
+ if (!Array.isArray(value)) {
130
+ return `Parameter ${key} must be an array`;
131
+ }
132
+ if (schema.items) {
133
+ for (let i = 0; i < value.length; i++) {
134
+ const itemError = this.validateType(`${key}[${i}]`, value[i], schema.items);
135
+ if (itemError) {
136
+ return itemError;
137
+ }
138
+ }
139
+ }
140
+ break;
141
+ case 'object':
142
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
143
+ return `Parameter ${key} must be an object`;
144
+ }
145
+ if (schema.properties) {
146
+ const objValue = value;
147
+ for (const [propKey, propSchema] of Object.entries(schema.properties)) {
148
+ const propValue = objValue[propKey];
149
+ if (propValue !== undefined) {
150
+ const propError = this.validateType(`${key}.${propKey}`, propValue, propSchema);
151
+ if (propError) {
152
+ return propError;
153
+ }
154
+ }
155
+ }
156
+ }
157
+ break;
158
+ default:
159
+ return `Unknown type for parameter ${key}: ${expectedType}`;
160
+ }
161
+ return null;
162
+ }
163
+ /**
164
+ * Execute with automatic validation (helper method)
165
+ * @param {unknown} args - Raw arguments to validate and execute
166
+ * @param {ToolContext} context - Execution context
167
+ * @returns {Promise<ToolResult<TResult>>} Tool execution result
168
+ */
169
+ async executeWithValidation(args, context) {
170
+ const startTime = performance.now();
171
+ const timestamp = new Date();
172
+ // Validate arguments
173
+ const validation = this.validate(args);
174
+ if (!validation.valid) {
175
+ return {
176
+ success: false,
177
+ error: {
178
+ code: 'validation_error',
179
+ message: `Validation failed: ${validation.errors.join(', ')}`,
180
+ details: { errors: validation.errors },
181
+ },
182
+ metadata: {
183
+ duration: Math.round(performance.now() - startTime),
184
+ timestamp,
185
+ },
186
+ };
187
+ }
188
+ // Check for cancellation before execution
189
+ if (context.signal?.aborted) {
190
+ return {
191
+ success: false,
192
+ error: {
193
+ code: 'cancelled_error',
194
+ message: 'Tool execution was cancelled',
195
+ },
196
+ metadata: {
197
+ duration: Math.round(performance.now() - startTime),
198
+ timestamp,
199
+ },
200
+ };
201
+ }
202
+ try {
203
+ const result = await this.execute(args, context);
204
+ // Add metadata if not present
205
+ if (!result.metadata) {
206
+ return {
207
+ ...result,
208
+ metadata: {
209
+ duration: Math.round(performance.now() - startTime),
210
+ timestamp,
211
+ },
212
+ };
213
+ }
214
+ return result;
215
+ }
216
+ catch (error) {
217
+ return {
218
+ success: false,
219
+ error: {
220
+ code: 'execution_error',
221
+ message: error instanceof Error ? error.message : 'Unknown error occurred',
222
+ cause: error instanceof Error ? error : undefined,
223
+ },
224
+ metadata: {
225
+ duration: Math.round(performance.now() - startTime),
226
+ timestamp,
227
+ },
228
+ };
229
+ }
230
+ }
231
+ }
232
+ /**
233
+ * Type guard to check if a value is a ToolError
234
+ * @param {unknown} value - Value to check
235
+ * @returns {boolean} True if the value is a ToolError
236
+ */
237
+ export function isToolError(value) {
238
+ return (typeof value === 'object' &&
239
+ value !== null &&
240
+ 'code' in value &&
241
+ 'message' in value &&
242
+ typeof value.code === 'string' &&
243
+ typeof value.message === 'string');
244
+ }
245
+ /**
246
+ * Create a successful tool result
247
+ * @template T
248
+ * @param {T} data - Result data
249
+ * @param {Partial<ToolResult<T>['metadata']>} metadata - Optional metadata
250
+ * @returns {ToolResult<T>} Successful tool result
251
+ */
252
+ export function createSuccessResult(data, metadata) {
253
+ return {
254
+ success: true,
255
+ data,
256
+ metadata: {
257
+ duration: 0,
258
+ timestamp: new Date(),
259
+ ...metadata,
260
+ },
261
+ };
262
+ }
263
+ /**
264
+ * Create an error tool result
265
+ * @param {ToolErrorCode} code - Error code
266
+ * @param {string} message - Error message
267
+ * @param {Partial<ToolError>} details - Additional error details
268
+ * @returns {ToolResult<never>} Error tool result
269
+ */
270
+ export function createErrorResult(code, message, details) {
271
+ return {
272
+ success: false,
273
+ error: {
274
+ code,
275
+ message,
276
+ ...details,
277
+ },
278
+ metadata: {
279
+ duration: 0,
280
+ timestamp: new Date(),
281
+ },
282
+ };
283
+ }
284
+ // Default export
285
+ export default Tool;