@asterism-app/sdk 1.13.1 → 1.13.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/events.ts +40 -10
- package/package.json +1 -1
- package/src/contracts.ts +1 -0
- package/src/events.ts +0 -19
- package/src/module-contract.ts +1 -1
- package/src/provider-usage.ts +2 -1
- package/src/resource-activity.ts +29 -0
- package/src/runtime-execution.ts +8 -0
- package/src/runtime-shell.ts +5 -0
package/examples/events.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import {
|
|
3
|
+
pluginNamespace, ReadResourceActivities, ResourceActivitiesChanged, ResourceNoticeRaised,
|
|
4
|
+
type ResourceActivity,
|
|
5
|
+
} from "@asterism-app/sdk/contracts";
|
|
6
|
+
import { defineModule, type ExecutionServices, type ObjectExecutor } from "@asterism-app/sdk/runtime";
|
|
7
|
+
import type { RendererModule, RendererPrepareContext } from "@asterism-app/sdk/renderer";
|
|
5
8
|
|
|
6
9
|
const ns = pluginNamespace("example.downloads");
|
|
7
10
|
export const Progress = ns.event("progress", {
|
|
@@ -19,19 +22,46 @@ export const backend = defineModule(plugin => {
|
|
|
19
22
|
export function report(object: ObjectExecutor): void {
|
|
20
23
|
object.events.publish(Progress, { job: "file", completed: 10, total: 100 });
|
|
21
24
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
object.
|
|
25
|
+
|
|
26
|
+
/** One scope covers the whole batch; result notification is an explicit, separate decision. */
|
|
27
|
+
export async function downloadFiles(execution: ExecutionServices, object: ObjectExecutor,
|
|
28
|
+
urls: readonly string[], signal?: AbortSignal): Promise<ArrayBuffer[]> {
|
|
29
|
+
const files = await execution.withActivity(object.ref, async ({ signal, progress }) => {
|
|
30
|
+
const downloaded: ArrayBuffer[] = [];
|
|
31
|
+
progress(null);
|
|
32
|
+
for (const url of urls) {
|
|
33
|
+
const response = await fetch(url, { signal });
|
|
34
|
+
if (!response.ok) throw new Error(`Download failed: HTTP ${response.status}`);
|
|
35
|
+
downloaded.push(await response.arrayBuffer());
|
|
36
|
+
progress(downloaded.length / urls.length);
|
|
37
|
+
}
|
|
38
|
+
signal.throwIfAborted();
|
|
39
|
+
return downloaded;
|
|
40
|
+
}, { signal });
|
|
41
|
+
// A rejected or cancelled scope never reaches this success notification.
|
|
42
|
+
object.events.publish(ResourceNoticeRaised, { title: "Downloads ready", kind: "success" });
|
|
43
|
+
return files;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Invoke on each connection, including reconnect: events invalidate, while the call reads live state. */
|
|
47
|
+
export async function observeActivities({ calls, events, signal }: RendererPrepareContext,
|
|
48
|
+
receive: (activities: readonly ResourceActivity[]) => void) {
|
|
49
|
+
const read = async () => receive(await calls.invoke(ReadResourceActivities, {}, { signal }));
|
|
50
|
+
const subscription = events.subscribe(ResourceActivitiesChanged, { delivery: "latest", signal }, read);
|
|
51
|
+
await subscription.ready;
|
|
52
|
+
await read();
|
|
53
|
+
return subscription;
|
|
27
54
|
}
|
|
55
|
+
|
|
56
|
+
export let activities: readonly ResourceActivity[] = [];
|
|
28
57
|
export const renderer: RendererModule = {
|
|
29
58
|
resources: [], cards: [], tools: [],
|
|
30
|
-
async prepare(
|
|
31
|
-
const subscription = events.subscribe(Progress, { delivery: "latest" }, event => {
|
|
59
|
+
async prepare(context) {
|
|
60
|
+
const subscription = context.events.subscribe(Progress, { delivery: "latest" }, event => {
|
|
32
61
|
const job: string = event.data.job;
|
|
33
62
|
void job;
|
|
34
63
|
});
|
|
35
64
|
await subscription.ready;
|
|
65
|
+
await observeActivities(context, snapshot => { activities = snapshot; });
|
|
36
66
|
},
|
|
37
67
|
};
|
package/package.json
CHANGED
package/src/contracts.ts
CHANGED
|
@@ -8,6 +8,7 @@ export * from "./plugin.ts";
|
|
|
8
8
|
export * from "./records.ts";
|
|
9
9
|
export * from "./resource-records.ts";
|
|
10
10
|
export * from "./events.ts";
|
|
11
|
+
export * from "./resource-activity.ts";
|
|
11
12
|
export * from "./calls.ts";
|
|
12
13
|
export * from "./channels.ts";
|
|
13
14
|
export * from "./tool-result.ts";
|
package/src/events.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import type { Json } from "./json.ts";
|
|
3
3
|
import type { Source } from "./workspace.ts";
|
|
4
|
-
import { uiTextSchema } from "./i18n.ts";
|
|
5
4
|
|
|
6
5
|
export type EventSourceKind = "plugin" | "resource" | "any" | "host";
|
|
7
6
|
export interface EventEnvelope<D extends Json = Json> {
|
|
@@ -78,21 +77,3 @@ export const ResourcePresenceChanged = defineEvent("asterism/resource-presence",
|
|
|
78
77
|
source: "host",
|
|
79
78
|
semantics: { kind: "occurrence" },
|
|
80
79
|
});
|
|
81
|
-
|
|
82
|
-
/** Each publication fully replaces resource status; running remains visible, while other states are consumed when the session is viewed. */
|
|
83
|
-
export const ResourceStatusChanged = defineEvent("asterism/resource-status", {
|
|
84
|
-
schema: z.discriminatedUnion("status", [
|
|
85
|
-
z.strictObject({
|
|
86
|
-
title: uiTextSchema,
|
|
87
|
-
status: z.literal("running"),
|
|
88
|
-
progress: z.strictObject({ value: z.number().min(0).max(1) }).optional(),
|
|
89
|
-
}),
|
|
90
|
-
z.strictObject({
|
|
91
|
-
title: uiTextSchema,
|
|
92
|
-
status: z.enum(["failed", "awaiting", "completed"]),
|
|
93
|
-
}),
|
|
94
|
-
]),
|
|
95
|
-
source: "resource",
|
|
96
|
-
semantics: { kind: "snapshot", key: [] },
|
|
97
|
-
});
|
|
98
|
-
export type ResourceStatus = z.infer<typeof ResourceStatusChanged.schema>;
|
package/src/module-contract.ts
CHANGED
|
@@ -22,7 +22,7 @@ export interface ToolContract<A extends ToolInput = ToolInput> extends ContractR
|
|
|
22
22
|
requiresInteraction?: boolean;
|
|
23
23
|
/**
|
|
24
24
|
* Each validated invocation requests user attention once when execution starts, independently of interaction endpoint requirements.
|
|
25
|
-
* The host
|
|
25
|
+
* The host raises an attention notification for the caller and exposes a transient live-call highlight. Viewing consumes the reminder;
|
|
26
26
|
* the notice does not change execution state or track unresolved business work.
|
|
27
27
|
*/
|
|
28
28
|
requiresNotice?: boolean;
|
package/src/provider-usage.ts
CHANGED
|
@@ -44,7 +44,8 @@ export const usageItemsSchema = z.array(usageItemSchema).refine(items => new Set
|
|
|
44
44
|
export type UsageItem = z.infer<typeof usageItemSchema>;
|
|
45
45
|
export type QuotaItem = Extract<UsageItem, { type: "quota" }>;
|
|
46
46
|
export type BalanceItem = Extract<UsageItem, { type: "balance" }>;
|
|
47
|
-
|
|
47
|
+
/** configuration identifies a connection rejected before querying; unsupported identifies an unavailable provider operation. */
|
|
48
|
+
export const providerUsageErrorSchema = z.enum(["configuration", "unavailable", "credential", "authentication", "unsupported", "rate_limit", "network", "timeout", "protocol", "server"]);
|
|
48
49
|
export type ProviderUsageError = z.infer<typeof providerUsageErrorSchema>;
|
|
49
50
|
export const usageProviderInfoSchema = z.strictObject({ id: providerUsageIdSchema, label: uiTextSchema, owner: z.string(),
|
|
50
51
|
source: z.enum(["query", "headers"]).default("query") });
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { defineCall } from "./calls.ts";
|
|
3
|
+
import { defineEvent } from "./events.ts";
|
|
4
|
+
import { uiTextSchema } from "./i18n.ts";
|
|
5
|
+
|
|
6
|
+
/** One live work scope, independent of timeline occupancy and notification consumption. */
|
|
7
|
+
export const resourceActivitySchema = z.strictObject({
|
|
8
|
+
id: z.string(),
|
|
9
|
+
resource: z.strictObject({ sessionId: z.string(), id: z.string(), kind: z.string() }),
|
|
10
|
+
title: uiTextSchema,
|
|
11
|
+
progress: z.strictObject({ value: z.number().min(0).max(1) }).optional(),
|
|
12
|
+
});
|
|
13
|
+
export type ResourceActivity = z.infer<typeof resourceActivitySchema>;
|
|
14
|
+
|
|
15
|
+
/** Read live scopes without loading session bodies. Subscribe to changes before reading, including after reconnection. */
|
|
16
|
+
export const ReadResourceActivities = defineCall("asterism/resource-activities.read", {
|
|
17
|
+
scope: "plugin", input: z.strictObject({}), output: z.array(resourceActivitySchema),
|
|
18
|
+
});
|
|
19
|
+
/** Invalidation only; the host retains current activities, while the event bus retains no state. */
|
|
20
|
+
export const ResourceActivitiesChanged = defineEvent("asterism/resource-activities", {
|
|
21
|
+
schema: z.strictObject({}), source: "host", semantics: { kind: "snapshot", key: [] },
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
/** A one-shot notification; viewing consumes the reminder without changing activity or resolving business interactions. */
|
|
25
|
+
export const ResourceNoticeRaised = defineEvent("asterism/resource-notice", {
|
|
26
|
+
schema: z.strictObject({ title: uiTextSchema, kind: z.enum(["attention", "success", "error"]) }),
|
|
27
|
+
source: "resource", semantics: { kind: "occurrence" },
|
|
28
|
+
});
|
|
29
|
+
export type ResourceNotice = z.infer<typeof ResourceNoticeRaised.schema>;
|
package/src/runtime-execution.ts
CHANGED
|
@@ -21,6 +21,14 @@ export interface ExecutionContentOptions {
|
|
|
21
21
|
output?: RetainedContentScope;
|
|
22
22
|
}
|
|
23
23
|
export interface ExecutionServices {
|
|
24
|
+
/**
|
|
25
|
+
* Register visible activity for this owned resource until work settles, including waits and cleanup.
|
|
26
|
+
* Work receives a lifetime-bound signal; resource removal and plugin shutdown cancel and await it.
|
|
27
|
+
* Each call owns an independent scope. Progress accepts a fraction in [0, 1], or null to clear it.
|
|
28
|
+
* Viewing notifications does not consume activity. Registration neither changes timeline occupancy nor emits result notifications.
|
|
29
|
+
*/
|
|
30
|
+
withActivity<T>(resource: ResourceRef, work: (context: { signal: AbortSignal; progress(value: number | null): void }) => Promise<T>,
|
|
31
|
+
options?: { signal?: AbortSignal }): Promise<T>;
|
|
24
32
|
/** Capture the owner's environment and hold resources through one request and tool batch. Callers own any timeline busy scope. */
|
|
25
33
|
run(request: ResourceModelRequest, options?: ExecutionContentOptions): Promise<ModelRunResult>;
|
|
26
34
|
/** Optional workbench output sink; the caller decides which execution events to send to it. */
|
package/src/runtime-shell.ts
CHANGED
|
@@ -13,4 +13,9 @@ export interface ShellExecution {
|
|
|
13
13
|
export interface ShellServices extends ShellExecution {
|
|
14
14
|
readonly profiles: ShellProfiles;
|
|
15
15
|
readonly terminals: TerminalSessions;
|
|
16
|
+
/** Synchronously snapshots shell selection, profile arguments and environment overrides.
|
|
17
|
+
* Executions use that configuration with the same plugin/host lifetime and caller cancellation.
|
|
18
|
+
* Executable discovery and external files remain execution-time OS inputs; direct service execution reads current settings.
|
|
19
|
+
*/
|
|
20
|
+
capture(): ShellExecution;
|
|
16
21
|
}
|