<span id="管理-workitem-服务"></span>

# Manage WorkItem Services

Use the AI Studio SDK to manage an API service for a specific WorkItem node and version. Publishing, enabling, invoking, inspecting, and disabling the service all target the same node ID and version.

(sdk-ai-studio-workitem-api-service-flow)=
<span id="任务流程"></span>

## Task flow

Obtain a service handle for the target node ID and version, publish the service configuration, and enable the service. Once enabled, invoke the service and inspect business execution fields; when retiring access, disable the service and read back its metadata to confirm the state transition.

```text
Node ID + Version + Compute resource ID
                   │
                   ▼
       Publish and enable service
                   │
                   ▼
   Invoke service and inspect status
                   │
                   ▼
  Disable service and verify status
```

(sdk-ai-studio-workitem-api-service-prepare)=
<span id="准备"></span>

## Prerequisites

1. Complete the endpoint, personal access token, and workspace setup in [Use the SDK](../../../quickstart/ai-studio/sdk.md).
2. **Node ID and version**: use the node ID and version of a custom operator created and tested in [Manage Custom Operators and Templates](自定义算子和工作流模板.md#sdk-ai-studio-workflow-design-create-operator), or query the catalog in [Inspect Workflow Node Definitions](查看工作流节点定义.md#sdk-ai-studio-workflow-nodes-s1).
3. **Compute resource ID and invocation parameters**: obtain a compute resource ID from [Manage Compute Resources](../资源中心/管理计算实例.md) or [Read Resource Inventories](../资源中心/读取资源清单.md), and prepare the service name, service type, and non-empty input payload matching the operator's input schema.

(sdk-ai-studio-workitem-api-service-publish)=
<span id="发布并启用服务"></span>

## Publish and enable a service

The example below publishes a service with a compute resource ID, timeout, concurrency limit, and per-minute rate limit, then enables it. Before routing production traffic, inspect the returned service status or run an application readiness check.

:::::{tab-set}
:sync-group: sdk-language

::::{tab-item} Python
:sync: python

```python
import os

import moi_product_sdk as sdk

client = sdk.new_with_personal_access_token(
    os.environ["PRODUCT_API_BASE_URL"],
    os.environ["PRODUCT_API_KEY"],
)
workspace = client.workspace(os.environ["WORKSPACE_ID"])
service = workspace.workitem_api_service("<node-id>", "<version>")
published = service.publish(
    sdk.WorkitemAPIServicePublishSpec(
        version="<version>",
        compute_resource_id="<compute-resource-id>",
        timeout_seconds=30,
        max_concurrency=1,
        rate_limit_per_min=60,
    )
)
enabled = service.enable()

print(published.service.status)
print(enabled.service.status)
```

::::

::::{tab-item} Go
:sync: go

```go
package main

import (
	"context"
	"fmt"
	"os"

	sdk "github.com/matrixorigin/matrixflow/sdk/go-sdk"
)

func main() {
	ctx := context.Background()
	client, err := sdk.NewWithPersonalAccessToken(
		os.Getenv("PRODUCT_API_BASE_URL"),
		os.Getenv("PRODUCT_API_KEY"),
	)
	if err != nil {
		panic(err)
	}
	workspace, err := client.Workspace(os.Getenv("WORKSPACE_ID"))
	if err != nil {
		panic(err)
	}
	service, err := workspace.WorkitemAPIService("<node-id>", "<version>")
	if err != nil {
		panic(err)
	}
	published, err := service.Publish(ctx, sdk.WorkitemAPIServicePublishSpec{
		Version:           "<version>",
		ComputeResourceID: "<compute-resource-id>",
		TimeoutSeconds:    30,
		MaxConcurrency:    1,
		RateLimitPerMin:   60,
	})
	if err != nil {
		panic(err)
	}
	enabled, err := service.Enable(ctx)
	if err != nil {
		panic(err)
	}

	fmt.Println(published.GetService().GetStatus())
	fmt.Println(enabled.GetService().GetStatus())
}
```

::::

:::::

(sdk-ai-studio-workitem-api-service-invoke)=
<span id="调用服务"></span>

## Invoke a service

Reuse the same service handle and provide the service name, service type, version, and a non-empty input payload. Receiving an HTTP response means the call completed at the transport layer; check the status, output payload, and error fields inside the returned result to determine business execution outcome.

:::::{tab-set}
:sync-group: sdk-language

::::{tab-item} Python
:sync: python

```python
response = service.invoke(
    sdk.WorkitemAPIServiceInvokeSpec(
        "<service-name>",
        "<service-type>",
        "<version>",
        {"<input-name>": "<input-value>"},
    )
)

print(response.result.status)
print(response.result.result)
print(response.result.error)
```

::::

::::{tab-item} Go
:sync: go

```go
response, err := service.Invoke(ctx, sdk.WorkitemAPIServiceInvokeSpec{
	ServiceName: "<service-name>",
	Type:        "<service-type>",
	Version:     "<version>",
	Payload:     map[string]any{"<input-name>": "<input-value>"},
})
if err != nil {
	panic(err)
}

fmt.Println(response.GetResult().GetStatus())
fmt.Println(response.GetResult().GetResult())
fmt.Println(response.GetResult().GetError())
```

::::

:::::

(sdk-ai-studio-workitem-api-service-disable)=
<span id="停用服务"></span>

## Disable a service

When retiring external invocations, disable the service and read back service details to confirm the updated status. The service handle controls API service enablement only; update or delete the underlying custom operator itself through [Manage Custom Operators and Templates](自定义算子和工作流模板.md).

:::::{tab-set}
:sync-group: sdk-language

::::{tab-item} Python
:sync: python

```python
disabled = service.disable()
current = service.info()

print(disabled.service.status)
print(current.service.status)
```

::::

::::{tab-item} Go
:sync: go

```go
disabled, err := service.Disable(ctx)
if err != nil {
	panic(err)
}
current, err := service.Info(ctx)
if err != nil {
	panic(err)
}

fmt.Println(disabled.GetService().GetStatus())
fmt.Println(current.GetService().GetStatus())
```

::::

:::::

(sdk-ai-studio-workitem-api-service-next)=
<span id="下一步"></span>

## Next steps

- [Publish Workflow API Services](发布工作流API服务.md) (publish a full workflow as a dynamic service bound to an interactive page)
- [Run and Track Workflows](运行并跟踪工作流.md)
- [Inspect Workflow Artifacts and Lineage](查看工作流产物和数据血缘.md)
