Files
OpenViking/examples/watch_resource_example.py
9eac8a6d3d feat(sdk): sync go/ts/python SDKs with server find/search, recall, an… (#3737)
* feat(sdk): sync go/ts/python SDKs with server find/search, recall, and admin changes

Server-side changes recently landed that the language SDKs had drifted from:

- find/search results now return `tags` and no longer return
  `category`/`match_reason`/`relations`/`overview` (#3730). Go's strict
  struct was the only one broken; update MatchedContext accordingly.
- new admin endpoints for agent-evolution and per-account settings (#3695).
- public `search/recall` endpoint was missing from all SDKs.

Changes:
- python: add `level`/`since`/`until`/`time_field` to find/search; add an
  `extra` escape hatch to find/search/add_resource/write/batch_write so new
  server fields can be passed without an SDK bump (only forwarded when set,
  preserving `level=0`); add `recall` and the four admin methods.
- go: fix MatchedContext (add Tags, drop removed fields), add Recall and the
  four admin methods.
- typescript: type MatchedContext/FindResult, add RecallOptions, add `recall`
  and the four admin methods.

Co-authored-by: TRAE CLI <noreply@bytedance.com>
Co-authored-by: TRAE CLI <traecli@bytedance.com>

feat(sdk): unify options APIs and sync latest server interfaces

- migrate complex Python SDK calls to typed options dictionaries
- add dedicated context search and consistent extra-field handling
- align Go and TypeScript options with omission-aware serialization
- support session config, event tags, Agent Evolution date filters,
  OpenViking Assets, batch write, downloads, and create_parent
- refresh SDK tests and examples across all three languages

Co-authored-by: TRAE CLI <noreply@bytedance.com>
Co-authored-by: TRAE CLI <traecli@bytedance.com>

fix(sdk): address options API review findings

- fix Go session extra merging and Python message precedence
- adapt LangChain calls to the Python options API
- migrate repository examples, tests, and documentation

Co-authored-by: TRAE CLI <noreply@bytedance.com>
Co-authored-by: TRAE CLI <traecli@bytedance.com>

fix(sdk): complete options migration and message parity

- migrate remaining Python SDK benchmarks to options dictionaries
- normalize empty parts consistently for single and batch messages
- add regression guards for repository SDK call sites

Co-authored-by: TRAE CLI <noreply@bytedance.com>
Co-authored-by: TRAE CLI <traecli@bytedance.com>

fix(sdk): align reindex options after main rebase

- preserve reindex tags in Python typed options
- add reindex extra support for Go and TypeScript
- reject official fields passed through extra across SDKs

Co-authored-by: TRAE CLI <noreply@bytedance.com>
Co-authored-by: TRAE CLI <traecli@bytedance.com>

feat(sdk): support legacy keyword options

Co-authored-by: TRAE CLI <noreply@bytedance.com>
Co-authored-by: TRAE CLI <traecli@bytedance.com>

docs(sdk): use explicit Python SDK arguments

Co-authored-by: TRAE CLI <noreply@bytedance.com>
Co-authored-by: TRAE CLI <traecli@bytedance.com>

fix(sdk): support set tags extra options

Co-authored-by: TRAE CLI <noreply@bytedance.com>
Co-authored-by: TRAE CLI <traecli@bytedance.com>

fix(sdk): expose Go add resource options

Expose AddType and ProcessingMode through Go AddResourceOptions and serialize them to the resources API. Add a regression test covering the resulting request payload.

Co-authored-by: TRAE CLI <noreply@bytedance.com>
Co-authored-by: TRAE CLI <traecli@bytedance.com>

feat(sdk): flatten core Python client options

Co-authored-by: TRAE CLI <traecli@bytedance.com>

docs(sdk): align Python examples with flattened options

Co-authored-by: TRAE CLI <traecli@bytedance.com>

fix(sdk): preserve core API compatibility

Co-authored-by: TRAE CLI <traecli@bytedance.com>

refactor(python-sdk): move resource hints to options

Co-authored-by: TRAE CLI <traecli@bytedance.com>

fix(sdk): align resource option callers

Co-authored-by: TRAE CLI <traecli@bytedance.com>

test(sdk): cover recursive reindex forwarding

Co-authored-by: TRAE CLI <traecli@bytedance.com>

fix(sdk): preserve Go options compatibility

Co-authored-by: TRAE CLI <traecli@bytedance.com>

feat(python-sdk): expose message peer id

Co-authored-by: TRAE CLI <traecli@bytedance.com>

test(python-sdk): consolidate options coverage

Co-authored-by: TRAE CLI <traecli@bytedance.com>

feat(python-sdk): add parts and flatten image search

Co-authored-by: TRAE CLI <traecli@bytedance.com>

* docs(sdk): align Python call examples

Co-authored-by: TRAE CLI <traecli@bytedance.com>

---------

Co-authored-by: TRAE CLI <traecli@bytedance.com>
Co-authored-by: Qin Haojie <qinhaojie.exe@bytedance.com>
2026-08-24 14:09:11 +08:00

154 lines
4.1 KiB
Python

#!/usr/bin/env python3
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: Apache-2.0
"""
Resource Watch Feature Example
This example demonstrates how to use the resource watch feature in OpenViking.
The watch feature allows you to automatically re-process resources at specified
intervals.
Key features:
- Create resources with watch enabled
- Update watch intervals (cancel then re-create)
- Cancel watch tasks
- Handle conflict errors
"""
import asyncio
from pathlib import Path
from openviking_sdk import AsyncHTTPClient, ConflictError
async def example_basic_watch():
client = AsyncHTTPClient(url="http://localhost:1933")
await client.initialize()
try:
test_file = Path("./test_resource.md")
test_file.write_text(
"""# Test Resource
## Content
This is a test resource for watch functionality.
## Version
Version: 1.0
"""
)
to_uri = "viking://resources/watched_resource"
print("\nAdding resource with watch_interval=60.0 minutes...")
result = await client.add_resource(
path=str(test_file),
to=to_uri,
options={
"reason": "Example: monitoring a document",
"instruction": "Check for updates and re-index",
"watch_interval": 60.0,
},
)
print("Resource added successfully!")
print(f" Root URI: {result['root_uri']}")
finally:
await client.close()
async def example_update_watch_interval():
client = AsyncHTTPClient(url="http://localhost:1933")
await client.initialize()
try:
test_file = Path("./test_resource.md")
to_uri = "viking://resources/watched_resource"
print("\nUpdating watch interval by canceling then re-creating...")
await client.add_resource(
path=str(test_file),
to=to_uri,
options={"watch_interval": 0},
)
await client.add_resource(
path=str(test_file),
to=to_uri,
options={
"reason": "Updated: more frequent monitoring",
"watch_interval": 120.0,
},
)
print("Watch task updated successfully!")
finally:
await client.close()
async def example_cancel_watch():
client = AsyncHTTPClient(url="http://localhost:1933")
await client.initialize()
try:
test_file = Path("./test_resource.md")
to_uri = "viking://resources/watched_resource"
print("\nCancelling watch by setting interval to 0...")
await client.add_resource(
path=str(test_file),
to=to_uri,
options={"watch_interval": 0},
)
print("Watch task cancelled successfully!")
finally:
await client.close()
async def example_handle_conflict():
client = AsyncHTTPClient(url="http://localhost:1933")
await client.initialize()
try:
test_file = Path("./test_resource.md")
to_uri = "viking://resources/conflict_example"
print("\nCreating first watch task...")
await client.add_resource(
path=str(test_file),
to=to_uri,
options={"watch_interval": 30.0},
)
print(" First watch task created successfully")
print("\nAttempting to create second watch task for same URI...")
try:
await client.add_resource(
path=str(test_file),
to=to_uri,
options={"watch_interval": 60.0},
)
print(" ERROR: This should not happen!")
except ConflictError as e:
print(" ConflictError caught as expected!")
print(f" Error message: {e}")
finally:
await client.close()
async def main():
print("\n" + "=" * 60)
print("OpenViking Resource Watch Examples")
print("=" * 60)
await example_basic_watch()
await example_update_watch_interval()
await example_cancel_watch()
await example_handle_conflict()
print("\n" + "=" * 60)
print("All examples completed!")
print("=" * 60)
if __name__ == "__main__":
asyncio.run(main())