-
Notifications
You must be signed in to change notification settings - Fork 2
Add parameterized AI tool instances #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f503823
Add parameterized AI tool instances
MikeAlhayek 60ab9ad
Rename tool instances to definitions and introduce AIToolSource
MikeAlhayek fcc6285
Rename tool definitions to tool instances and add pluggable registry
MikeAlhayek a3bfe1a
Reference tool instances by name, add OAuth2 token caching, decouple …
MikeAlhayek 3b79683
Address PR #112 review and harden parameterized tool instances
MikeAlhayek 4485a16
docs: document EntityCore store and address PR #112 doc comments
MikeAlhayek b19d8ce
samples: add source dropdown to AI tool instance UI and move menu ite…
MikeAlhayek 56d19e2
ai: namespace tool-instance function names to avoid collisions with c…
MikeAlhayek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/Abstractions/CrestApps.Core.AI.Abstractions/Tooling/AIProfileToolInstanceMetadata.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| namespace CrestApps.Core.AI.Tooling; | ||
|
|
||
| /// <summary> | ||
| /// Profile metadata that records which configured <see cref="AIToolInstance"/> entries are attached to | ||
| /// an AI profile (or other tool-bearing resource). Stored in the resource's properties bag. | ||
| /// </summary> | ||
| public sealed class AIProfileToolInstanceMetadata | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the identifiers of the configured tool instances available to the resource. | ||
| /// </summary> | ||
| public string[] InstanceIds { get; set; } | ||
|
MikeAlhayek marked this conversation as resolved.
Outdated
|
||
| } | ||
79 changes: 79 additions & 0 deletions
79
src/Abstractions/CrestApps.Core.AI.Abstractions/Tooling/AIToolInstance.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| using CrestApps.Core.Models; | ||
| using CrestApps.Core.Services; | ||
|
|
||
| namespace CrestApps.Core.AI.Tooling; | ||
|
|
||
| /// <summary> | ||
| /// Represents a user-configured, model-invokable tool instance created from a registered | ||
| /// <see cref="IAIToolInstanceSource"/> blueprint. Unlike a plain <c>AITool</c> whose arguments are | ||
| /// always supplied by the model, a tool instance binds developer-defined behavior to user-provided | ||
| /// settings (endpoints, credentials, headers, etc.) captured up front. The AI model still decides when | ||
| /// to invoke the resulting function, but the user-provided settings are applied at invocation time. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// The <see cref="SourceCatalogEntry.Source"/> property holds the registered name of the owning tool | ||
| /// instance source, while <see cref="Name"/> is a unique technical name used to derive the function name | ||
| /// exposed to the AI model. Multiple instances may be created from the same source, each with different | ||
| /// settings and a distinct <see cref="Description"/> so the model can tell them apart. | ||
| /// </remarks> | ||
| public sealed class AIToolInstance : SourceCatalogEntry, INameAwareModel, IDisplayTextAwareModel, IModifiedUtcAwareModel, ICloneable<AIToolInstance> | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the unique technical name for this tool instance. This value is the basis for the | ||
| /// function name exposed to the AI model, so it must be unique across all configured instances. | ||
| /// </summary> | ||
| public string Name { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the human-readable display text shown in management and selection surfaces. | ||
| /// </summary> | ||
| public string DisplayText { get; set; } | ||
|
MikeAlhayek marked this conversation as resolved.
Outdated
|
||
|
|
||
| /// <summary> | ||
| /// Gets or sets the natural-language description presented to the AI model. This is the primary | ||
| /// signal the model uses to distinguish between multiple instances built from the same source, so it | ||
| /// should clearly explain what this specific instance does (for example, which API it calls). | ||
| /// </summary> | ||
| public string Description { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the UTC timestamp when this instance was created. | ||
| /// </summary> | ||
| public DateTime CreatedUtc { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the UTC timestamp when this instance was last modified. | ||
| /// </summary> | ||
| public DateTime? ModifiedUtc { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the display name of the user that authored this instance. | ||
| /// </summary> | ||
| public string Author { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets the identifier of the user that owns this instance. | ||
| /// </summary> | ||
| public string OwnerId { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Creates a shallow copy of this instance, sharing the same <see cref="ExtensibleEntity.Properties"/> reference. | ||
| /// </summary> | ||
| /// <returns>A new <see cref="AIToolInstance"/> with the same values.</returns> | ||
| public AIToolInstance Clone() | ||
| { | ||
| return new AIToolInstance | ||
| { | ||
| ItemId = ItemId, | ||
| Source = Source, | ||
| Name = Name, | ||
| DisplayText = DisplayText, | ||
| Description = Description, | ||
| CreatedUtc = CreatedUtc, | ||
| ModifiedUtc = ModifiedUtc, | ||
| Author = Author, | ||
| OwnerId = OwnerId, | ||
| Properties = Properties, | ||
| }; | ||
| } | ||
| } | ||
63 changes: 63 additions & 0 deletions
63
src/Abstractions/CrestApps.Core.AI.Abstractions/Tooling/AIToolInstanceExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| namespace CrestApps.Core.AI.Tooling; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for <see cref="AIToolInstance"/>, including production of stable, model-safe | ||
| /// function names so that multiple instances built from the same source are exposed to the AI model as | ||
| /// distinct callable functions. | ||
| /// </summary> | ||
| public static class AIToolInstanceExtensions | ||
| { | ||
| private const int MaxFunctionNameLength = 64; | ||
|
|
||
| /// <summary> | ||
| /// Builds the unique function name presented to the AI model for the supplied instance. The name is | ||
| /// derived from the instance's unique <see cref="AIToolInstance.Name"/> (falling back to its | ||
| /// identifier) and is sanitized to the characters allowed by chat-completion providers (letters, | ||
| /// digits, underscores, and hyphens), truncated to 64 characters. | ||
| /// </summary> | ||
| /// <param name="instance">The configured tool instance.</param> | ||
| /// <returns>A deterministic, provider-safe function name.</returns> | ||
| public static string GetFunctionName(this AIToolInstance instance) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(instance); | ||
|
|
||
| var name = Sanitize(instance.Name); | ||
|
|
||
| if (string.IsNullOrEmpty(name)) | ||
| { | ||
| name = Sanitize(instance.ItemId); | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(name)) | ||
| { | ||
| name = "tool_instance"; | ||
| } | ||
|
|
||
| if (name.Length > MaxFunctionNameLength) | ||
| { | ||
| name = name[..MaxFunctionNameLength]; | ||
| } | ||
|
|
||
| return name; | ||
| } | ||
|
|
||
| private static string Sanitize(string value) | ||
| { | ||
| if (string.IsNullOrEmpty(value)) | ||
| { | ||
| return string.Empty; | ||
| } | ||
|
|
||
| var buffer = new char[value.Length]; | ||
| var length = 0; | ||
|
|
||
| foreach (var c in value) | ||
| { | ||
| buffer[length++] = char.IsAsciiLetterOrDigit(c) || c == '_' || c == '-' | ||
| ? c | ||
| : '_'; | ||
| } | ||
|
|
||
| return new string(buffer, 0, length); | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
src/Abstractions/CrestApps.Core.AI.Abstractions/Tooling/AIToolInstanceSourceContext.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| namespace CrestApps.Core.AI.Tooling; | ||
|
|
||
| /// <summary> | ||
| /// Carries the information required to materialize an <see cref="AITool"/> for a configured | ||
| /// <see cref="AIToolInstance"/>. Passed to <see cref="IAIToolInstanceSource.CreateTool"/>. | ||
| /// </summary> | ||
| public sealed class AIToolInstanceSourceContext | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AIToolInstanceSourceContext"/> class. | ||
| /// </summary> | ||
| /// <param name="instance">The configured tool instance.</param> | ||
| /// <param name="functionName">The unique function name to expose to the AI model.</param> | ||
| /// <param name="description">The description to expose to the AI model.</param> | ||
| public AIToolInstanceSourceContext(AIToolInstance instance, string functionName, string description) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(instance); | ||
| ArgumentException.ThrowIfNullOrEmpty(functionName); | ||
|
|
||
| Instance = instance; | ||
| FunctionName = functionName; | ||
| Description = description; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the configured tool instance whose settings should be bound to the produced tool. | ||
| /// </summary> | ||
| public AIToolInstance Instance { get; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the unique function name to expose to the AI model. This is derived per instance so that | ||
| /// multiple instances of the same source surface as distinct callable functions. | ||
| /// </summary> | ||
| public string FunctionName { get; } | ||
|
MikeAlhayek marked this conversation as resolved.
Outdated
|
||
|
|
||
| /// <summary> | ||
| /// Gets the description to expose to the AI model, taken from the instance so the model can | ||
| /// distinguish between instances of the same source. | ||
| /// </summary> | ||
| public string Description { get; } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
src/Abstractions/CrestApps.Core.AI.Abstractions/Tooling/IAIToolInstanceSource.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| namespace CrestApps.Core.AI.Tooling; | ||
|
|
||
| /// <summary> | ||
| /// A developer-authored, parameterized tool blueprint that end users configure one or more times as | ||
| /// <see cref="AIToolInstance"/> catalog entries. A source is registered under a unique name (stored as | ||
| /// the <see cref="CrestApps.Core.Models.SourceCatalogEntry.Source"/> of every <see cref="AIToolInstance"/> | ||
| /// created from it) and is responsible for turning a configured instance into a concrete | ||
| /// <see cref="AITool"/> whose behavior is bound to the user's settings. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Sources are registered with <c>AddAIToolInstanceSource<TSource>(name, configure)</c>, which | ||
| /// records the source's display metadata (display name, description, category) in | ||
| /// <c>AIOptions.ToolInstanceSources</c> and registers the behavior as a keyed service. A source | ||
| /// typically ships a settings model that it persists in <see cref="AIToolInstance.Properties"/> (via | ||
| /// <c>.Put()</c>/<c>.TryGet()</c>) and reads back inside the produced tool. The classic example is a | ||
| /// generic "call any HTTP API" source where the user provides the endpoint, authentication, and | ||
| /// headers, while the model only supplies the remaining open arguments (if any). | ||
| /// </remarks> | ||
| public interface IAIToolInstanceSource | ||
|
MikeAlhayek marked this conversation as resolved.
|
||
| { | ||
| /// <summary> | ||
| /// Creates the concrete <see cref="AITool"/> that the AI model can invoke for the supplied | ||
| /// configured instance. Implementations must apply the instance's user-provided settings and use the | ||
| /// supplied <see cref="AIToolInstanceSourceContext.FunctionName"/> and | ||
| /// <see cref="AIToolInstanceSourceContext.Description"/> so the instance surfaces distinctly. | ||
| /// </summary> | ||
| /// <param name="context">The context describing the instance and the function metadata to expose.</param> | ||
| /// <returns>The tool to expose to the AI model, or <see langword="null"/> to skip this instance.</returns> | ||
| AITool CreateTool(AIToolInstanceSourceContext context); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.