-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathartifact_delivery_presigned.py
More file actions
382 lines (337 loc) · 16.2 KB
/
Copy pathartifact_delivery_presigned.py
File metadata and controls
382 lines (337 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
"""Example: publisher-hosted artifact delivery with signed download URLs.
This demonstrates Model B from docs/artifact-delivery.md:
1. render an artifact inside the action call;
2. store bytes in publisher-owned object storage;
3. return both output.download_url and ExecutionArtifact.external_url;
4. allow a later free get_artifact call to reissue a fresh URL, scoped by
(owner_user_id, artifact_id).
The object store below is an in-memory stand-in with boto3-compatible method
names so this example runs offline. In production, replace DemoObjectStore with
`boto3.client("s3")`, Cloudflare R2, GCS, Azure Blob, or another HTTPS object
store.
"""
from __future__ import annotations
import hashlib
import sys
from pathlib import Path
from urllib.parse import quote
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from siglume_api_sdk import ( # noqa: E402
AppAdapter,
AppCategory,
AppManifest,
AppTestHarness,
ApprovalMode,
ExecutionArtifact,
ExecutionContext,
ExecutionKind,
ExecutionResult,
PermissionClass,
PriceModel,
ToolManual,
ToolManualPermissionClass,
validate_tool_manual,
)
BUCKET = "demo-artifacts"
SIGNED_URL_TTL_SECONDS = 3600
class DemoObjectStore:
"""Tiny offline object store with the boto3 methods used in the docs."""
def __init__(self) -> None:
self.objects: dict[tuple[str, str], dict[str, object]] = {}
def put_object(self, *, Bucket: str, Key: str, Body: bytes, ContentType: str) -> None:
self.objects[(Bucket, Key)] = {"body": Body, "content_type": ContentType}
def generate_presigned_url(self, ClientMethod: str, *, Params: dict[str, str], ExpiresIn: int) -> str:
if ClientMethod != "get_object":
raise ValueError("Only get_object is supported in this example")
bucket = Params["Bucket"]
key = Params["Key"]
if (bucket, key) not in self.objects:
raise KeyError(f"missing object: {bucket}/{key}")
signature = hashlib.sha256(f"{bucket}:{key}:{ExpiresIn}".encode("utf-8")).hexdigest()[:16]
return (
f"https://object-store.example/{quote(bucket)}/{quote(key)}"
f"?X-Amz-Expires={ExpiresIn}&X-Amz-Signature={signature}"
)
class ArtifactDeliveryPresignedApp(AppAdapter):
def __init__(self, store: DemoObjectStore | None = None) -> None:
super().__init__()
self.store = store or DemoObjectStore()
self._artifacts: dict[tuple[str, str], dict[str, object]] = {}
def manifest(self) -> AppManifest:
return AppManifest(
capability_key="artifact-delivery-presigned",
name="Artifact Delivery Presigned",
job_to_be_done="Render a small report, store it in publisher object storage, and return a signed download link.",
category=AppCategory.DOCUMENT,
store_vertical="api",
permission_class=PermissionClass.ACTION,
approval_mode=ApprovalMode.ALWAYS_ASK,
dry_run_supported=True,
required_connected_accounts=[],
price_model=PriceModel.FREE,
currency="USD",
allow_free_trial=False,
jurisdiction="US",
short_description="Return publisher-hosted artifacts with signed URLs.",
example_prompts=[
"Render a report and give me the download link.",
"Refresh the download link for artifact art_demo_123.",
],
)
async def execute(self, ctx: ExecutionContext) -> ExecutionResult:
params = ctx.input_params or {}
operation = str(params.get("operation") or ("get_artifact" if params.get("artifact_id") else "render"))
if operation == "get_artifact" or ctx.task_type == "get_artifact":
return self._get_artifact(ctx, str(params.get("artifact_id") or ""))
return self._render_artifact(ctx)
def supported_task_types(self) -> list[str]:
return ["render_artifact", "get_artifact"]
def _render_artifact(self, ctx: ExecutionContext) -> ExecutionResult:
if not self._valid_owner(ctx.owner_user_id):
return self._identity_error(ctx)
title = str(ctx.input_params.get("title") or "Weekly artifact report")
if ctx.execution_kind == ExecutionKind.DRY_RUN:
return ExecutionResult(
success=True,
execution_kind=ctx.execution_kind,
output={"summary": f"Would render '{title}' and return a signed download link.", "status": "ready"},
needs_approval=True,
approval_prompt=f"Render '{title}' and create a one-hour signed download link.",
)
body = self._render_report(title=title, owner_user_id=ctx.owner_user_id)
artifact_id = self._artifact_id(ctx.owner_user_id, title, body)
owner_hash = hashlib.sha256(ctx.owner_user_id.encode("utf-8")).hexdigest()[:16]
key = f"artifacts/{owner_hash}/{artifact_id}.md"
content_type = "text/markdown; charset=utf-8"
self.store.put_object(Bucket=BUCKET, Key=key, Body=body, ContentType=content_type)
self._artifacts[(ctx.owner_user_id, artifact_id)] = {
"bucket": BUCKET,
"key": key,
"title": title,
"content_type": content_type,
"status": "ready",
}
download_url = self._signed_url(BUCKET, key)
artifact = ExecutionArtifact(
artifact_type="document",
external_id=artifact_id,
external_url=download_url,
title=title,
metadata={"content_type": content_type, "expires_in_seconds": SIGNED_URL_TTL_SECONDS},
)
return ExecutionResult(
success=True,
execution_kind=ctx.execution_kind,
output={
"summary": f"Rendered '{title}'.",
"status": "found",
"artifact_id": artifact_id,
"download_url": download_url,
"download_expires_in_seconds": SIGNED_URL_TTL_SECONDS,
},
artifacts=[artifact],
receipt_summary={"action": "artifact_rendered", "artifact_id": artifact_id},
)
def _get_artifact(self, ctx: ExecutionContext, artifact_id: str) -> ExecutionResult:
if not self._valid_owner(ctx.owner_user_id):
return self._identity_error(ctx)
free_receipt = {"operation": "get_artifact", "amount_minor": 0, "currency": "USD"}
record = self._artifacts.get((ctx.owner_user_id, artifact_id))
if record is None:
# Unknown and wrong-owner ids return the same empty shape.
return ExecutionResult(
success=False,
execution_kind=ctx.execution_kind,
output={"summary": "Artifact is unavailable or expired.", "status": "expired", "artifact_id": artifact_id},
units_consumed=0,
amount_minor=0,
currency="USD",
receipt_summary=free_receipt,
)
if record.get("status") == "not_ready":
return ExecutionResult(
success=True,
execution_kind=ctx.execution_kind,
output={"summary": "Artifact is not ready yet.", "status": "not_ready", "artifact_id": artifact_id},
units_consumed=0,
amount_minor=0,
currency="USD",
receipt_summary=free_receipt,
)
if record.get("status") == "expired":
return ExecutionResult(
success=False,
execution_kind=ctx.execution_kind,
output={"summary": "Artifact retention expired.", "status": "expired", "artifact_id": artifact_id},
units_consumed=0,
amount_minor=0,
currency="USD",
receipt_summary=free_receipt,
)
download_url = self._signed_url(str(record["bucket"]), str(record["key"]))
artifact = ExecutionArtifact(
artifact_type="document",
external_id=artifact_id,
external_url=download_url,
title=str(record["title"]),
metadata={
"content_type": str(record["content_type"]),
"expires_in_seconds": SIGNED_URL_TTL_SECONDS,
},
)
return ExecutionResult(
success=True,
execution_kind=ctx.execution_kind,
output={
"summary": "Fresh download link issued.",
"status": "found",
"artifact_id": artifact_id,
"download_url": download_url,
"download_expires_in_seconds": SIGNED_URL_TTL_SECONDS,
},
units_consumed=0,
amount_minor=0,
currency="USD",
artifacts=[artifact],
receipt_summary=free_receipt,
)
def _signed_url(self, bucket: str, key: str) -> str:
return self.store.generate_presigned_url(
"get_object",
Params={"Bucket": bucket, "Key": key},
ExpiresIn=SIGNED_URL_TTL_SECONDS,
)
@staticmethod
def _render_report(*, title: str, owner_user_id: str) -> bytes:
return f"# {title}\n\nOwner-scoped artifact for {owner_user_id}.\n".encode("utf-8")
@staticmethod
def _artifact_id(owner_user_id: str, title: str, body: bytes) -> str:
digest = hashlib.sha256(owner_user_id.encode("utf-8") + title.encode("utf-8") + body).hexdigest()[:16]
return f"art_{digest}"
@staticmethod
def _valid_owner(owner_user_id: str | None) -> bool:
return bool(owner_user_id and owner_user_id.strip() and owner_user_id != "siglume")
@staticmethod
def _identity_error(ctx: ExecutionContext) -> ExecutionResult:
return ExecutionResult(
success=False,
execution_kind=ctx.execution_kind,
output={"summary": "Missing platform user identity.", "status": "unauthorized"},
units_consumed=0,
amount_minor=0,
currency="USD",
receipt_summary={"operation": "identity_check", "amount_minor": 0, "currency": "USD"},
)
def build_tool_manual() -> ToolManual:
return ToolManual(
tool_name="artifact_delivery_presigned",
job_to_be_done="Render a report artifact, return a signed HTTPS download link, and refresh that link later for the same owner.",
summary_for_model=(
"Renders a publisher-hosted report and returns both output.download_url and "
"ExecutionArtifact.external_url. Use get_artifact with artifact_id to refresh the "
"signed URL for free. The API scopes retrieval by owner_user_id plus artifact_id."
),
trigger_conditions=[
"owner asks to generate, render, export, or download a file-like report",
"agent has an artifact_id from this API and needs a fresh download link",
"owner needs a publisher-hosted output file rather than inline text",
],
do_not_use_when=[
"the requested result is short plain text that should be returned inline",
"the owner is asking to upload a file into the API rather than download an output artifact",
],
permission_class=ToolManualPermissionClass.ACTION,
dry_run_supported=True,
requires_connected_accounts=[],
input_schema={
"type": "object",
"properties": {
"operation": {
"type": "string",
"enum": ["render", "get_artifact"],
"description": "render creates a new artifact; get_artifact reissues a signed URL for an existing artifact_id.",
"default": "render",
},
"title": {"type": "string", "description": "Report title when operation=render."},
"artifact_id": {"type": "string", "description": "Publisher-issued artifact id when operation=get_artifact."},
},
"additionalProperties": False,
},
output_schema={
"type": "object",
"properties": {
"summary": {"type": "string", "description": "Human-readable result summary."},
"status": {"type": "string", "description": "ready | found | not_ready | expired | unauthorized."},
"artifact_id": {"type": "string", "description": "Durable publisher artifact id."},
"download_url": {"type": "string", "description": "Short-lived HTTPS GET URL to bytes hosted by the publisher."},
"download_expires_in_seconds": {"type": "integer", "description": "Signed URL TTL, default 3600 seconds."},
},
"required": ["summary", "status"],
"additionalProperties": False,
},
usage_hints=[
"Use operation=render to create a new artifact and return a signed URL.",
"Use operation=get_artifact with artifact_id to refresh a signed URL for free.",
"Never ask the user for owner_user_id; Siglume supplies it as runtime identity.",
],
result_hints=[
"Show download_url to the owner as the link to fetch the artifact.",
"If status=expired, explain that the artifact id is unavailable or outside retention.",
"If status=unauthorized, fail closed; do not expose any artifact details.",
],
error_hints=[
"Unknown and wrong-owner artifact ids both return expired, so do not infer ownership.",
"If a signed URL expires, call get_artifact again instead of reusing the stale URL.",
],
approval_summary_template="Render report artifact '{title}' and return a signed download link.",
preview_schema={
"type": "object",
"properties": {"summary": {"type": "string", "description": "Preview of the artifact render."}},
"required": ["summary"],
"additionalProperties": False,
},
idempotency_support=True,
side_effect_summary="Writes a rendered report into publisher-owned object storage and returns a short-lived HTTPS download link.",
jurisdiction="US",
)
async def run_artifact_delivery_example() -> list[str]:
app = ArtifactDeliveryPresignedApp()
harness = AppTestHarness(app)
ok, issues = validate_tool_manual(build_tool_manual())
output = [f"tool_manual_valid: {ok} {len(issues)}", f"manifest_issues: {len(harness.validate_manifest())}"]
dry_run = await harness.dry_run(task_type="render_artifact", input_params={"title": "Demo report"})
output.append(f"dry_run: {dry_run.success} {dry_run.output['status']}")
action = await harness.execute_action(task_type="render_artifact", input_params={"title": "Demo report"})
artifact_id = action.output["artifact_id"]
output.append(f"action: {action.success} {action.output['status']} artifacts={len(action.artifacts)}")
output.append(f"download_url_present: {str(action.output['download_url']).startswith('https://')}")
reissue = await harness.execute_action(task_type="get_artifact", input_params={"operation": "get_artifact", "artifact_id": artifact_id})
output.append(f"reissue: {reissue.success} {reissue.output['status']} amount={reissue.amount_minor}")
from siglume_api_sdk import ExecutionContext # noqa: WPS433
wrong_owner = await app.execute(
ExecutionContext(
agent_id="test-agent-001",
owner_user_id="other-owner-001",
task_type="get_artifact",
input_params={"artifact_id": artifact_id},
execution_kind=ExecutionKind.ACTION,
)
)
output.append(f"wrong_owner: {wrong_owner.success} {wrong_owner.output['status']}")
sentinel_owner = await app.execute(
ExecutionContext(
agent_id="test-agent-001",
owner_user_id="siglume",
task_type="get_artifact",
input_params={"artifact_id": artifact_id},
execution_kind=ExecutionKind.ACTION,
)
)
output.append(f"sentinel_owner: {sentinel_owner.success} {sentinel_owner.output['status']}")
return output
async def main() -> None:
for line in await run_artifact_delivery_example():
print(line)
if __name__ == "__main__":
import asyncio
asyncio.run(main())