Class StringHandle

java.lang.Object
uno.anahata.asi.agi.resource.handle.AbstractResourceHandle
uno.anahata.asi.agi.resource.handle.StringHandle
All Implemented Interfaces:
ResourceHandle, Rebindable

public class StringHandle extends AbstractResourceHandle
A memory-backed resource handle for virtual or proposed content.

This handle allows arbitrary strings to be managed as V2 Resources. It is primarily used for chat code blocks and tool parameters, enabling them to use the same high-fidelity viewer pipeline as physical files.

Author:
anahata
  • Field Details

    • uri

      private final URI uri
      The memory URI of the snippet.
    • name

      private final String name
      The display name of the snippet.
    • contextPath

      private String contextPath
      Optional context path to hint IDEs about the project/location this snippet belongs to.
    • content

      private String content
      The actual text content held in memory.
    • lastModified

      private long lastModified
      The last modification timestamp.
    • attributes

      private final Map<String,Object> attributes
      Custom attributes for host-aware context propagation (e.g. classpath hints).
  • Constructor Details

    • StringHandle

      public StringHandle(String name, String content)
      Constructs a new StringHandle.
      Parameters:
      name - The display name (e.g., "proposed.java").
      content - The initial text content.
  • Method Details

    • setAttribute

      public void setAttribute(String key, Object value)
      Sets a custom attribute on this virtual handle.
      Parameters:
      key - the key
      value - the value
    • getLastModified

      public long getLastModified()
      Returns the last modified timestamp in milliseconds.

      Returns the mutable modification timestamp.

      Returns:
      The timestamp, or 0 if unknown.
    • exists

      public boolean exists()
      Checks if the resource physically or virtually exists.

      Memory snippets always exist while the handle is alive.

      Returns:
      true if the source is available.
    • openStream

      public InputStream openStream() throws IOException
      Opens a fresh input stream to the resource content.

      Opens a stream over the internal UTF-8 byte array.

      Returns:
      A new InputStream instance.
      Throws:
      IOException - if the stream cannot be opened.
    • isWritable

      public boolean isWritable()
      Determines if the resource is writable in the current environment.

      Virtual snippets are always writable in memory.

      Returns:
      true if the handle supports the ResourceHandle.write(String) operation.
    • write

      public void write(String content) throws IOException
      Agnostically writes text content back to the resource.

      Purity Note: Read-only handles should throw UnsupportedOperationException.

      Updates the internal string and notifies the owner orchestrator. Implements an equality gate to prevent redundant interpretation cycles.

      Parameters:
      content - The text to write.
      Throws:
      IOException - if the write fails.
    • isVirtual

      public boolean isVirtual()
      Determines if this is a virtual, memory-backed handle.

      High-Fidelity Strategy: Virtual handles (snippets) typically trigger the 'Preview-as-Editor' mode in viewers to ensure constant IDE fidelity without card-swapping.

      Implementation details: returns true as this is a memory handle.

      Returns:
      true if virtual (in-memory content).
    • isTextual

      public boolean isTextual()
      Determines if the resource is textual and suitable for a TextView.

      This method provides a centralized capability check. It whitelists common structured data formats that may be classified under the 'application/' hierarchy but are inherently textual.

      Memory handles are always considered textual in the current platform version.

      Returns:
      true if the resource should be handled by a TextView.
    • getMimeType

      public String getMimeType()
      Returns the detected MIME type of the resource.

      Memory handles default to text/plain. The viewer is responsible for environment-specific language detection via the resource name.

      Returns:
      The MIME type string (e.g., "text/plain", "image/png").
    • length

      public long length()
      Returns the total length of the resource content in characters/bytes.

      Returns the length of the in-memory string.

      Returns:
      The length, or -1 if the length is unknown or streaming.