Using C# Attributes and Generators in VL

Recently I addressed this to @Elias and he advised me better to share it here:

Is there a pattern or a way to use Code Generators in VL or a way to “compose/decompose” Attributes and build a method or a class which can be seen as it would be decorated in VL context ?

(edit: Specifically for generators I discovered that as soon as I build my csproj I can access them without any issue in VL, nonetheless the question if it would be possible to use them in the VL runtime remains)

Code generator: are you referring to the C# source generator?

Not sure what you mean by “compose/decompose” Attributes

method or a class which can be seen as it would be decorated in VL context

We have attributes to decorate whole assemblies (ImportAsIsAttribute), namespaces (ImportNamespaceAttribute) or selected types (ImportTypeAttribute) for direct consumption by VL - are you referring to that? You can find example usages of those attributes in our standard libs.

hello @Elias , yes I am referring to C# source generator.

regarding Attributes, I am not sure if this is a solution for external libs.

In particular, my question raised when I tried to implement some Ollama tools in OllamaSharp.

I found a workaround, but I would prefer to use the functionalities they provide.
For instance, they are decorating a class with a certain attribute like [OllamaTool] then the code generator, picks that one (this is what I understand) and creates a Tool Object with the appropriate properties and methods.

What I ask for, or what I would like to have per se, is to avoid using VS and compiling every time my newly made tools but instead, patching them in VL.

(My workaround was to create a CustomTool class decorated with the OllamaTool and use a delegate to set its Tool Function - but using Func<> and Delegates prohibits me from doing stateful operations)

I am not sure how clear are all of these, but if you need more info from my behalf and my quest let me know.

Thanks!

Ah ok, you want their code generator to run on the VL generated code. I fear this is not possible as of now. It’s also not anywhere on the roadmap to my knowledge.

You can however (with a bit of work) build regions with stateful content. Check custom regions section in the gray book for some guidance and options. Maybe that helps you for your workaround.

2 Likes

I already did an attempt, but I can’t figure out how to pass it in a wrapped async method (all tool methods in OllamaSharp are async), but I ll spend some more time on it and get back with some feedback, from your sayings I understand that I have to rephrase my question, and better ask, how to pass stateful regions as arguments to async methods :P but this would be a different topic.

It’s actually kind of working that way, at list from a description, basically it’s something that allows to wrap a patch and Invoke it as delegate. So likely you can pass it to async method… Last one we did was Avalonia DataTemplate (it’s in VL.Avalonia.vl) maybe you can check that one…

There are also a cases witch introduce side effects with this approach…

1 Like