Json Schema for LLMs

This commit is contained in:
Michael Schimmel
2026-01-06 20:55:11 +01:00
parent 4618573d6c
commit 8a29cf7f74
8 changed files with 263 additions and 179 deletions
+17 -11
View File
@@ -60,6 +60,7 @@ type
public
DynamicWrapper: TDataValue.TFunc;
Signatures: TList<IMethodSignature>;
Doc: string; // Documentation string from AstDoc attribute
constructor Create;
destructor Destroy; override;
end;
@@ -145,6 +146,7 @@ uses
System.TypInfo,
System.Hash,
System.StrUtils,
Myc.Ast.Attributes,
Myc.Ast.RTL.Core;
type
@@ -228,6 +230,7 @@ begin
inherited Create;
Signatures := TList<IMethodSignature>.Create;
DynamicWrapper := nil;
Doc := '';
end;
destructor TRtlFunctionInfo.Destroy;
@@ -262,6 +265,7 @@ var
rttiParams: TArray<TRttiParameter>;
isDynamic: Boolean;
i: Integer;
docString: string;
begin
// 1. Create global caches
FStaticBootstrap := TStaticBootstrapCache.Create(TStaticSignatureKeyComparer.Create);
@@ -278,13 +282,14 @@ begin
continue;
exportAttr := nil;
docString := '';
for attribute in method.GetAttributes do
begin
if attribute is TRtlExportAttribute then
begin
exportAttr := attribute as TRtlExportAttribute;
break;
end;
exportAttr := attribute as TRtlExportAttribute
else if attribute is AstDocAttribute then
docString := AstDocAttribute(attribute).Description;
end;
if not Assigned(exportAttr) then
@@ -298,6 +303,10 @@ begin
FStaticFuncMap.Add(rtlName, funcInfo);
end;
// Update documentation (if not already set or override)
if (funcInfo.Doc = '') and (docString <> '') then
funcInfo.Doc := docString;
// Check Signature Type (Dynamic vs Static)
rttiParams := method.GetParameters;
isDynamic :=
@@ -346,10 +355,6 @@ begin
if (not Assigned(funcInfo.DynamicWrapper)) and (Length(argTypes) = 1) and (argTypes[0].Kind = stUnknown) then
begin
funcInfo.DynamicWrapper := wrapper;
end
else if (not Assigned(funcInfo.DynamicWrapper)) and (Length(argTypes) = 2) and (argTypes[0].Kind = stUnknown) then
begin
// Potentially handle 2-arg scalar wrappers here too if needed
end;
end
else
@@ -492,7 +497,8 @@ begin
var wrapper := funcInfo.DynamicWrapper; // Default is Void
AScope.Define(rtlName, wrapper, staticType);
// Pass documentation to scope definition
AScope.Define(rtlName, wrapper, staticType, funcInfo.Doc);
end;
end;
@@ -766,8 +772,8 @@ end;
procedure RegisterRtlFunctions(const AScope: IExecutionScope);
begin
AScope.Define('true', TDataValue(TScalar.FromBoolean(True)), TTypes.Boolean);
AScope.Define('false', TDataValue(TScalar.FromBoolean(False)), TTypes.Boolean);
AScope.Define('true', TDataValue(TScalar.FromBoolean(True)), TTypes.Boolean, 'Boolean true.');
AScope.Define('false', TDataValue(TScalar.FromBoolean(False)), TTypes.Boolean, 'Boolean false.');
TRtlRegistry.RegisterAll(AScope);
end;