| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import { computed, inject, Injectable, signal } from '@angular/core';
- import { FmodeApi } from './api/fmode-api';
- import { LoadState, Task, TaskStatus } from './api/models';
- import { LiveService } from './live.service';
- /**
- * 任务看板服务 —— 任务域的**唯一 owner**(FR-07b)
- *
- * 依据 SPEC FR-07b 的依赖声明与《数据访问层设计.md》§五:
- * · **一期只读**:无派发 / 评论 / 修改入口(AC-07b.6)
- * · 云函数接收端**未就绪**时,抽象层返回空数组 → 界面显示空态,
- * **禁止伪造数据**、禁止把「未接入」伪装成「没有任务」
- * · 双端走**同一服务**,口径一致(AC-07b.4)
- */
- @Injectable({ providedIn: 'root' })
- export class TasksService {
- private readonly api = inject(FmodeApi);
- private readonly live = inject(LiveService);
- /** 已按 agent 加载的任务缓存 */
- private readonly _byAgent = signal<Record<string, Task[]>>({});
- private readonly _state = signal<LoadState>('idle');
- private readonly _error = signal<string | null>(null);
- /** 当前选中的数字生命(任务按 agent 过滤,AC-07b.1) */
- readonly agentId = signal<string>('');
- /** 当前状态筛选 */
- readonly statusFilter = signal<TaskStatus | 'all'>('all');
- /** 当前选中任务 */
- readonly selectedId = signal<string | null>(null);
- readonly state = this._state.asReadonly();
- readonly error = this._error.asReadonly();
- /** 全部已加载任务(供事件派生用) */
- readonly all = computed(() => Object.values(this._byAgent()).flat());
- /** 当前数字生命的任务,按更新时间倒序 */
- readonly current = computed(() => {
- const list = this._byAgent()[this.agentId()] ?? [];
- return [...list].sort((a, b) => (b.updatedAt ?? '').localeCompare(a.updatedAt ?? ''));
- });
- readonly filtered = computed(() => {
- const f = this.statusFilter();
- const list = this.current();
- return f === 'all' ? list : list.filter((t) => t.status === f);
- });
- readonly selected = computed(() => {
- const id = this.selectedId();
- return id ? this.all().find((t) => t.id === id) : undefined;
- });
- /** 七态计数(筛选 chip 上的数字) */
- readonly counts = computed<Record<TaskStatus | 'all', number>>(() => {
- const list = this.current();
- const base = {
- all: list.length, triage: 0, todo: 0, scheduled: 0, ready: 0,
- running: 0, blocked: 0, done: 0,
- } as Record<TaskStatus | 'all', number>;
- for (const t of list) base[t.status] += 1;
- return base;
- });
- /** 阻塞任务(工作台/首页上浮用) */
- readonly blocked = computed(() => this.all().filter((t) => t.status === 'blocked'));
- /**
- * 强制重新拉取全部任务(手动「刷新」用);跳过缓存。
- * 事件派生的「执行」类事件依赖任务数据,故刷新须带上本方法。
- */
- async refresh(): Promise<void> {
- this._byAgent.set({});
- this._state.set('idle');
- await this.ensureAllLoaded();
- }
- /** 确保**全部**数字生命的任务已加载(事件派生需要全量) */
- async ensureAllLoaded(): Promise<void> {
- // empty 也算已加载,避免空列表反复请求
- if (this._state() === 'ready' || this._state() === 'empty' || this._state() === 'loading') return;
- this._state.set('loading');
- this._error.set(null);
- try {
- await this.live.ensureLoaded();
- const agents = this.live.agents();
- const results = await Promise.all(
- agents.map(async (a) => [a.id, await this.api.fetchTasks(a.id)] as const),
- );
- const map: Record<string, Task[]> = {};
- for (const [id, tasks] of results) map[id] = tasks;
- this._byAgent.set(map);
- const total = Object.values(map).flat().length;
- this._state.set(total === 0 ? 'empty' : 'ready');
- } catch (e) {
- this._state.set('error');
- this._error.set(e instanceof Error ? e.message : '任务数据加载失败');
- }
- }
- /** 单数字生命任务加载(进入任务看板时) */
- async loadFor(agentId: string): Promise<void> {
- this.agentId.set(agentId);
- if (this._byAgent()[agentId]) return;
- this._state.set('loading');
- try {
- const tasks = await this.api.fetchTasks(agentId);
- this._byAgent.update((m) => ({ ...m, [agentId]: tasks }));
- this._state.set(tasks.length === 0 ? 'empty' : 'ready');
- } catch (e) {
- this._state.set('error');
- this._error.set(e instanceof Error ? e.message : '任务数据加载失败');
- }
- }
- select(id: string | null): void {
- this.selectedId.set(id);
- }
- setStatusFilter(f: TaskStatus | 'all'): void {
- this.statusFilter.set(f);
- }
- selectFirstIfNeeded(): void {
- if (!this.selectedId() && this.filtered().length > 0) {
- this.selectedId.set(this.filtered()[0].id);
- }
- }
- }
|