Azure / typespec-azure

About TypeSpec Azure Libraries
https://azure.github.io/typespec-azure/
MIT License
8 stars 28 forks source link

feat.: ability for projects like OpenAI to customize anonymous models #1094

Closed trrwilson closed 4 days ago

trrwilson commented 1 week ago

Clear and concise description of the problem

OpenAI's API specification has a plethora of anonymous models -- in-line objects and other constructs defined within the structure of top-level named type.

Concrete example: the 'code_interpreter' property, as anonymously defined within a 'tool_resources' property that in turn is part of the 'CreateAssistantRequest' component schema.

Many of these end up with... "human-unfriendly" emitter-generated names (speaking to .NET here), owing to being nested many layers deep -- exhibit A, RunStepDeltaStepDetailsToolCallsCodeObjectCodeInterpreterOutputsObject (a real emitted name, further concatenated for things like UnknownRunStepDeltaStepDetailsToolCallsCodeObjectCodeInterpreterOutputsObject.cs.

Problems include:

Enter the request: It would be nice to be able to rename these directly in .tsp with something like the following.

Using this as the tsp

//main.tsp
model Foo {
  bar: {
    baz: {
      x: int;
    }
  }
}

We could name both the bar and baz model like this

//client.tsp
@@clientName(Foo.bar, "MyBarName", "csharp", true);
@@clientName(Foo.bar.baz, "MyBazName", "csharp", true);

The clientName decorator would take a new parameter to indicate if it is targeting the anonymous model resulting from the property that is passed in or false (the default) if it is targeting the property itself just like before.

Checklist

m-nash commented 1 week ago

One item for additional context.

Another option we can consider here is if tsp has something like Foo.bar.baz._model where we could directly target the anonymous object. This would be better for 2 reasons.

  1. We don't need an extra parameter on all of our decorators
  2. It will create the opportunity to give IntelliSense compilation errors if you try to attach to something that isn't an anonymous object such as Foo.bar.baz.x.
allenjzhang commented 4 days ago

::type can be used to reference the anonymous types and its properties. Here using @doc as example

model Foo {
  bar: {
    baz: {
      x: int32;
    };
  };
}

@@doc(Foo.bar, "MyBarName");
@@doc(Foo.bar::type.baz::type.x, "MyBazName");