# Artifacts: durable, markdown-first agent output.

type Artifact {
  id: ID!
  tenantId: ID!
  agentId: ID
  threadId: ID
  title: String!
  type: ArtifactType!
  status: ArtifactStatus!
  content: String
  s3Key: String
  summary: String
  sourceMessageId: ID
  metadata: AWSJSON
  favoritedAt: AWSDateTime
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

type Applet {
  artifact: Artifact!
  appId: ID!
  name: String!
  version: Int!
  tenantId: ID!
  threadId: ID
  prompt: String
  agentVersion: String
  modelId: String
  generatedAt: AWSDateTime!
  stdlibVersionAtGeneration: String!
}

type AppletPayload {
  applet: Applet!
  files: AWSJSON!
  source: String!
  metadata: AWSJSON!
  themeCss: String
}

input AdminUpdateAppletSourceInput {
  appId: ID!
  source: String!
}

type AppletConnection {
  nodes: [Applet!]!
  nextCursor: String
}

type SaveAppletPayload {
  ok: Boolean!
  appId: ID
  version: Int
  validated: Boolean!
  persisted: Boolean!
  errors: [AWSJSON!]!
}

type AppletState {
  appId: ID!
  instanceId: ID!
  key: String!
  value: AWSJSON
  updatedAt: AWSDateTime!
}

enum ArtifactType {
  DATA_VIEW
  APPLET
  APPLET_STATE
  NOTE
  REPORT
  PLAN
  DRAFT
  DIGEST
}

enum ArtifactStatus {
  DRAFT
  FINAL
  SUPERSEDED
}

input CreateArtifactInput {
  tenantId: ID!
  agentId: ID
  threadId: ID
  title: String!
  type: ArtifactType!
  status: ArtifactStatus
  content: String
  s3Key: String
  summary: String
  sourceMessageId: ID
  metadata: AWSJSON
}

input UpdateArtifactInput {
  title: String
  type: ArtifactType
  status: ArtifactStatus
  content: String
  s3Key: String
  summary: String
  metadata: AWSJSON
  # Nullable AWSDateTime. Set an ISO 8601 timestamp to mark the artifact
  # as a favorite (drives the apps/computer sidebar Favorites section);
  # pass `null` to clear the flag. Other fields are unchanged either way.
  favoritedAt: AWSDateTime
}

input SaveAppletInput {
  appId: ID
  name: String!
  files: AWSJSON!
  metadata: AWSJSON
}

input PromoteDraftAppletInput {
  draftId: ID!
  computerId: ID!
  threadId: ID!
  name: String!
  files: AWSJSON!
  metadata: AWSJSON
  sourceDigest: String!
  promotionProof: String!
  promotionProofExpiresAt: AWSDateTime!
}

input SaveAppletStateInput {
  appId: ID!
  instanceId: ID!
  key: String!
  value: AWSJSON
}

extend type Query {
  artifacts(
    tenantId: ID!
    threadId: ID
    agentId: ID
    type: ArtifactType
    status: ArtifactStatus
    # When true, return only artifacts with a non-null favoritedAt,
    # ordered favoritedAt DESC. Drives the apps/computer sidebar
    # Favorites section.
    favoritedOnly: Boolean
    limit: Int
    cursor: String
  ): [Artifact!]!
  artifact(id: ID!): Artifact
  applet(appId: ID!): AppletPayload
  applets(cursor: String, limit: Int): AppletConnection!
  appletState(appId: ID!, instanceId: ID!, key: String!): AppletState
  adminApplet(appId: ID!): AppletPayload
  adminApplets(
    tenantId: ID!
    userId: ID
    cursor: String
    limit: Int
  ): AppletConnection!
}

extend type Mutation {
  createArtifact(input: CreateArtifactInput!): Artifact!
  updateArtifact(id: ID!, input: UpdateArtifactInput!): Artifact!
  deleteArtifact(id: ID!): Boolean!
  saveApplet(input: SaveAppletInput!): SaveAppletPayload!
  promoteDraftApplet(input: PromoteDraftAppletInput!): SaveAppletPayload!
  regenerateApplet(input: SaveAppletInput!): SaveAppletPayload!
  adminUpdateAppletSource(input: AdminUpdateAppletSourceInput!): SaveAppletPayload!
  saveAppletState(input: SaveAppletStateInput!): AppletState!
}
